Skip to content

The agent that grows with you

fix(tui_gateway): file the model-switch pivot in the live session too (#123545)

Halldrix8aba7c1b1d11
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index 30d485769e9..17a1780abcc 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -10519,7 +10519,10 @@ def test_config_set_model_switches_agent_without_touching_env(monkeypatch):
assert session["history"][-1]["role"] == "user"
assert "changed to anthropic/claude-sonnet-4.6" in session["history"][-1]["content"]
assert db.messages[-1] == {
- "session_id": "session-key",
+ # The agent's own db handle belongs to the agent's live session id, not the gateway's
+ # session_key — the same `getattr(agent, "session_id", None) or session_key` the sibling
+ # system-prompt persist uses one screen up (#123545: these two must not diverge).
+ "session_id": agent.session_id,
"role": "user",
"content": session["history"][-1]["content"],
}
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index 0b8ce58e8b0..0a24fe2c18c 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -1803,10 +1803,13 @@ def _append_model_switch_marker(session: dict | None, *, model: str, provider: s
with (contextlib.nullcontext(db) if db is not None else _session_db(session)) as db:
if db is not None:
from agent.context_compressor import _DB_PERSISTED_MARKER
+ # Same stale-key hazard as the submit row: this durable pivot must land in the session the
+ # live agent writes to, or a model switch between turns on a rotated session files the notice
+ # under a parent the conversation no longer reads from (#123545).
from agent.message_metadata import stamp_message_uid
entry["_row_id"] = db.append_message(
- session_id=session_key, role="user", content=marker, display_kind="model_switch",
- message_uid=stamp_message_uid(entry))
+ session_id=_submit_row_target_key(session), role="user", content=marker,
+ display_kind="model_switch", message_uid=stamp_message_uid(entry))
entry[_DB_PERSISTED_MARKER] = True
except Exception:
logger.debug("failed to persist model switch marker", exc_info=True)
diff --git a/tui_gateway/session_workdir.py b/tui_gateway/session_workdir.py
index 231c9fd2b22..8d5a40142d2 100644
--- a/tui_gateway/session_workdir.py
+++ b/tui_gateway/session_workdir.py
@@ -546,17 +546,16 @@ def _submit_row_target_key(session: dict) -> str:
to the stale key splits one turn's rows across two sessions: the user row in the parent, every tool
row and the final text in the child (#123545, evidence A + B).
- The live id IS the authority — it is the value the turn will flush under — so it wins whenever it
- is set. Do NOT re-resolve through the lineage here: ``resolve_resume_session_id`` returns the
- deepest node that has MESSAGES, so the freshly-minted child of a just-published rotation resolves
- back to the parent and the fix would no-op exactly when it is needed. The choice is made ONCE here
- and recorded on the staged dict (``_session_id``) so every later addresser of that row — the
- @-expansion rewrite, the queue merge, the drain deactivation — reads the same key instead of
- re-deriving one that a rotation can invalidate mid-turn.
+ The live id IS the authority — it is the value the turn will flush under, and the same
+ ``getattr(agent, "session_id", None) or session_key`` the sibling system-prompt persist already uses
+ against this handle (``_persist_live_session_system_prompt``). Do NOT re-resolve through the lineage
+ here: ``resolve_resume_session_id`` returns the deepest node that has MESSAGES, so the freshly-minted
+ child of a just-published rotation resolves back to the parent and the fix would no-op exactly when
+ it is needed. The choice is made ONCE here and recorded on the staged dict (``_session_id``) so
+ every later addresser of that row — the @-expansion rewrite, the queue merge, the drain deactivation
+ — reads the same key instead of re-deriving one that a rotation can invalidate mid-turn.
"""
- live = str(getattr(session.get("agent"), "session_id", None) or "")
- key = str(session.get("session_key") or "")
- return live or key
+ return str(getattr(session.get("agent"), "session_id", None) or "") or str(session.get("session_key") or "")
# Wire-sanitizer-safe key carrying the session the submit row was actually written under. Mirrors