The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): target the submit row at the session the turn will write (#123545)
Halldrixdc8e54498637
diff --git a/tui_gateway/session_workdir.py b/tui_gateway/session_workdir.pyindex fa44fbb6eb5..10fa605eff3 100644--- a/tui_gateway/session_workdir.py+++ b/tui_gateway/session_workdir.py@@ -534,6 +534,37 @@ def _persist_branch_seed(session: dict) -> None:_workdir_reraise_disk_full(exc, "branch seed persist failed")+def _submit_row_target_key(session: dict, db) -> str:+ """The session row THIS turn's durable user row must land in: the agent's live id when it has+ rotated off ``session_key``, else ``session_key`` itself.++ The turn's own transcript is flushed under ``agent.session_id`` (``_db_flush_write``), while an+ off-turn write has only ``session_key`` to go on — and those two diverge for the whole of a turn+ that starts after a rotation: a lease-wait re-resolve (``turn_facade_lease``), a compression+ publish, or an adopted continuation tip all move ``agent.session_id`` while ``session_key`` is+ re-anchored only at TURN END (``_absorb_turn_result`` -> ``_sync_session_key_after_compress``).+ Writing the submit row 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).+ Prefer the live id so this row lands where the rest of the turn will. ``resolve_resume_session_id``+ is the same lineage walk the lease admission uses, so a rotation that has not reached+ ``agent.session_id`` yet still resolves here.+ """+ key = str(session.get("session_key") or "")+ if not key or db is None:+ return key+ live = str(getattr(session.get("agent"), "session_id", None) or "")+ if live == key:+ return key+ resolver = getattr(type(db), "resolve_resume_session_id", None)+ if not callable(resolver):+ return live+ try:+ return str(resolver(db, key) or live or key)+ except Exception:+ logger.debug("submit-row lineage resolve failed; using the live agent session id", exc_info=True)+ return live or key++def _write_submit_user_row(session: dict, text: Any, display_kind: str | None,accept_metadata: dict | None = None) -> dict | None:"""Write the submitted user turn to the transcript and RETURN the durable dict (stamped@@ -558,7 +589,8 @@ def _write_submit_user_row(session: dict, text: Any, display_kind: str | None,return Nonetry:staged["_row_id"] = db.append_message(- key, "user", content=text, display_kind=display_kind, timestamp=staged["timestamp"],+ _submit_row_target_key(session, db), "user", content=text, display_kind=display_kind,+ timestamp=staged["timestamp"],message_uid=stamp_message_uid(staged), # the live dict the turn adopts carries the row's uiddisplay_metadata=staged.get("display_metadata"))except Exception as exc:@@ -598,8 +630,11 @@ def _adopt_submit_user_row(session: dict, agent, persist_user_message: Any, textif db is None:returntry:+ # The row landed under the same key ``_write_submit_user_row`` targeted; addressing it by the+ # rotated-away ``session_key`` would miss that row's session_id and silently skip the rewrite+ # (the UPDATE is qualified by session_id), leaving the transcript replaying the raw keystrokes.db.set_user_message_content(- session["session_key"], staged["_row_id"], _durable_content(persist_user_message))+ _submit_row_target_key(session, db), staged["_row_id"], _durable_content(persist_user_message))except Exception:logger.debug("submit-time user row update failed; the turn writes its own row", exc_info=True)return