fix(desktop): a voice turn's reasoning-off no longer leaks into new chats
A voice turn runs on auxiliary.voice_chat (default reasoning_effort: none, applied to the main model when the slot is on auto). While it ran, session.info, config.get reasoning, the warm-reattach resume and the session-row persist all read the agent's live voice route as the session's own. Desktop adopts session.info's effort into the sticky composer effort that seeds every new chat, and the end-of-turn settle repairs only the chat on screen, so a chat opened mid voice turn was created with reasoning off. On a model that loops when thinking is disabled (GLM-5.3-Flash on a custom endpoint) every new Desktop chat died on the repetition guard while the CLI worked. session_runtime_view answers model/provider/endpoint/effort from the state begin_voice_turn_route already captured; the voice turn itself still sends its own route and effort.
This commit is contained in:
@@ -172,6 +172,30 @@ def begin_voice_turn_route(agent: Any, messages: List[Dict[str, Any]], system_pr
|
||||
return agent._cached_system_prompt or system_prompt
|
||||
|
||||
|
||||
class _SessionRuntimeView:
|
||||
"""``agent`` with the session's own route and effort in place of a live voice turn's."""
|
||||
|
||||
def __init__(self, agent: Any, state: Dict[str, Any]) -> None:
|
||||
snapshot = state.get("snapshot") or {}
|
||||
own = {name: snapshot[name] for name in ("model", "provider", "base_url", "api_mode") if name in snapshot}
|
||||
if "anthropic_base_url" in snapshot:
|
||||
own["_anthropic_base_url"] = snapshot["anthropic_base_url"]
|
||||
own["reasoning_config"] = state.get("reasoning_config")
|
||||
self.__dict__.update(_agent=agent, _own=own)
|
||||
|
||||
def __getattr__(self, name: str) -> Any:
|
||||
own = self.__dict__["_own"]
|
||||
return own[name] if name in own else getattr(self.__dict__["_agent"], name)
|
||||
|
||||
|
||||
def session_runtime_view(agent: Any) -> Any:
|
||||
"""What a surface reports or persists as the SESSION's model and effort. Mid voice turn the agent
|
||||
runs on the voice route (often reasoning off); reported as the chat's own setting, Desktop adopted
|
||||
it into the composer, and every new chat it opened inherited reasoning off."""
|
||||
state = getattr(agent, "_voice_route_state", None)
|
||||
return _SessionRuntimeView(agent, state) if state else agent
|
||||
|
||||
|
||||
def end_voice_turn_route(agent: Any) -> None:
|
||||
"""Undo ``begin_voice_turn_route`` (idempotent; every turn exit calls it)."""
|
||||
state = getattr(agent, "_voice_route_state", None)
|
||||
|
||||
@@ -86,3 +86,33 @@ def test_voice_usage_never_becomes_the_session_route(tmp_path):
|
||||
assert row["input_tokens"] == 10
|
||||
assert db.auxiliary_usage_by_task("s1")["voice_chat"]["input_tokens"] == 10
|
||||
assert db.get_recent_session_model_route("s1") is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("separate_voice_model", [False, True])
|
||||
def test_mid_voice_turn_reports_the_sessions_own_route(tmp_path, monkeypatch, separate_voice_model):
|
||||
"""Desktop adopts session.info's effort/model into the composer that seeds new chats: a live
|
||||
voice turn's reasoning-off (and voice model) must never be reported or persisted as the chat's."""
|
||||
from agent.voice_turn_route import begin_voice_turn_route, end_voice_turn_route
|
||||
from tui_gateway.server import _runtime_model_config, _session_info
|
||||
|
||||
with FakeLLMServer([]) as main, FakeLLMServer([]) as voice:
|
||||
extra = ("auxiliary:\n voice_chat:\n provider: custom\n"
|
||||
f" base_url: {voice.base_url}\n model: voice-model\n api_key: sk-fake-voice\n"
|
||||
if separate_voice_model else "")
|
||||
home = write_hermes_home(tmp_path / ".hermes", main.base_url, extra_config=extra)
|
||||
monkeypatch.setenv("HERMES_HOME", str(home))
|
||||
agent = _agent(home, main.base_url)
|
||||
agent.reasoning_config = {"enabled": True, "effort": "low"}
|
||||
agent._voice_turn_pending = True
|
||||
begin_voice_turn_route(agent, [{"role": "user", "content": "hi"}], "system")
|
||||
try:
|
||||
assert agent.reasoning_config == {"enabled": False} # the voice turn itself still runs off
|
||||
assert agent.model == ("voice-model" if separate_voice_model else "fake-model")
|
||||
info = _session_info(agent, {})
|
||||
persisted = _runtime_model_config(agent)
|
||||
assert (info["reasoning_effort"], info["model"]) == ("low", "fake-model")
|
||||
assert persisted["reasoning_config"] == {"enabled": True, "effort": "low"}
|
||||
assert persisted["model"] == "fake-model"
|
||||
finally:
|
||||
end_voice_turn_route(agent)
|
||||
assert _session_info(agent, {})["reasoning_effort"] == "low"
|
||||
|
||||
@@ -208,7 +208,8 @@ def _cfg_get_reasoning(params):
|
||||
session = _sessions.get(params.get("session_id", "")) or {}
|
||||
reasoning_config = session.get("create_reasoning_override")
|
||||
if session and not isinstance(reasoning_config, dict):
|
||||
reasoning_config = getattr(session.get("agent"), "reasoning_config", None)
|
||||
from agent.voice_turn_route import session_runtime_view
|
||||
reasoning_config = getattr(session_runtime_view(session.get("agent")), "reasoning_config", None)
|
||||
if isinstance(reasoning_config, dict):
|
||||
enabled = reasoning_config.get("enabled") is not False
|
||||
effort = str(reasoning_config.get("effort") or "medium") if enabled else "none"
|
||||
|
||||
@@ -34,6 +34,7 @@ from tools.environments.local import hermes_subprocess_env
|
||||
from agent.fast_mode import STATIC_TIERS
|
||||
from agent.replay_cleanup import canonicalize_replay_history
|
||||
from agent.reasoning_effort import clamp_effort, route_supported_efforts
|
||||
from agent.voice_turn_route import session_runtime_view
|
||||
from agent.compaction_display import project_compaction_message_for_display # noqa: F401
|
||||
from agent.skill_commands import describe_skill_invocation # noqa: F401
|
||||
from agent.conversation_loop import INTERRUPT_WAITING_FOR_MODEL_PREFIX # noqa: F401
|
||||
@@ -1718,6 +1719,7 @@ def _runtime_model_config(agent, existing: dict | None = None) -> dict:
|
||||
attributes DELETE the key rather than skip the write: resume reads provider/endpoint from this JSON
|
||||
(model column written separately), so a stale provider would route the resumed chat to the wrong endpoint."""
|
||||
config = dict(existing or {})
|
||||
agent = session_runtime_view(agent)
|
||||
attr = lambda k: str(getattr(agent, k, "") or "").strip()
|
||||
model, provider, base_url = attr("model"), attr("provider"), attr("base_url")
|
||||
if provider.lower() == "custom":
|
||||
@@ -1759,7 +1761,7 @@ def _persist_live_session_runtime(session: dict | None) -> None:
|
||||
if (tier_override := session.get("create_service_tier_override")) is not None:
|
||||
# agent.service_tier is None for explicit normal; without this the distinction is erased on every persist.
|
||||
model_config["service_tier"] = tier_override or "normal"
|
||||
model = str(getattr(agent, "model", "") or "").strip()
|
||||
model = str(model_config.get("model") or "").strip()
|
||||
if hasattr(db, "update_session_meta"):
|
||||
db.update_session_meta(session_key, json.dumps(model_config), model or None)
|
||||
elif model and hasattr(db, "update_session_model"):
|
||||
@@ -2324,7 +2326,7 @@ def _live_session_identity(session: dict) -> tuple[str, str]:
|
||||
carries. The profile default is the LAST resort, never the answer for a chat that made its own pick."""
|
||||
pending = session.get("pending_model_switch") or {}
|
||||
mirror = _metadata_mirror(session)
|
||||
agent = session.get("agent")
|
||||
agent = session_runtime_view(session.get("agent"))
|
||||
override = session.get("model_override") or {}
|
||||
model = (str(pending.get("display_model") or "").strip() or mirror.get("model")
|
||||
or getattr(agent, "model", "") or override.get("model"))
|
||||
@@ -2354,6 +2356,7 @@ def _fast_tier_applies(agent, model: str, provider: str, *, route_known: bool, t
|
||||
def _session_info(agent, session: dict | None = None) -> dict:
|
||||
if session is None:
|
||||
session = next((c for c in _sessions.values() if c.get("agent") is agent), None)
|
||||
agent = session_runtime_view(agent)
|
||||
sess = session or {}
|
||||
mirror = _metadata_mirror(session)
|
||||
cwd = _display_session_cwd(session)
|
||||
|
||||
Reference in New Issue
Block a user