Skip to content

The agent that grows with you

fix(tui): repeating the last message no longer hides the unflushed turn on a warm switch

Teknium7a560c4ca906
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index 4bd174b6086..e2d95e2a4c7 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -3452,6 +3452,28 @@ def test_reconcile_display_with_live_trusts_db_when_tail_absent():
assert server._reconcile_display_with_live(db_display, []) == db_display
+def test_live_visible_history_keeps_an_unflushed_repeat_of_the_last_turn_real_db(tmp_path):
+ """The live tail is anchored on the last DB row's durable id, not its text: sending the same words again
+ ("ok" -> "done") before the flush must not fold the new turn into the persisted one."""
+ from hermes_state import SessionDB
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ db.create_session("s1", source="tui")
+ db.append_message("s1", role="user", content="ok")
+ db.append_message("s1", role="assistant", content="done")
+ live = db.get_messages_as_conversation("s1") + [
+ {"role": "user", "content": "ok"}, {"role": "assistant", "content": "done"}]
+
+ merged = server._live_visible_history({"session_key": "s1"}, db, live)
+ assert [m["content"] for m in merged] == ["ok", "done", "ok", "done"]
+ # Fully flushed: the same four rows, never doubled.
+ for msg in live[2:]:
+ db.append_message("s1", role=msg["role"], content=msg["content"])
+ flushed = db.get_messages_as_conversation("s1")
+ assert [m["content"] for m in server._live_visible_history({"session_key": "s1"}, db, flushed)] == [
+ "ok", "done", "ok", "done"]
+
+
def test_live_visible_history_matches_eager_resume_with_real_db(tmp_path):
"""E2E cross-builder consistency against a real SessionDB.
diff --git a/tui_gateway/session_registry.py b/tui_gateway/session_registry.py
index 7be5efa70c6..9a89f2fd6bc 100644
--- a/tui_gateway/session_registry.py
+++ b/tui_gateway/session_registry.py
@@ -689,21 +689,35 @@ def _fallback_session_info(session: dict) -> dict:
}
+def _durable_ids(msg) -> set:
+ """A row's durable identities: its ``message_uid`` (and the uids a merged turn absorbed), else ``_row_id``."""
+ if not isinstance(msg, dict):
+ return set()
+ from agent.message_metadata import ABSORBED_MESSAGE_UIDS, MESSAGE_UID
+ ids = {("uid", uid) for uid in [msg.get(MESSAGE_UID), *(msg.get(ABSORBED_MESSAGE_UIDS) or ())] if isinstance(uid, str) and uid}
+ row_id = msg.get("_row_id")
+ return ids | ({("row", row_id)} if isinstance(row_id, int) and not isinstance(row_id, bool) else set())
+
+
def _reconcile_display_with_live(db_display: list[dict], in_memory: list[dict]) -> list[dict]:
"""Merge the persisted DISPLAY lineage with the in-memory live history: ``db_display`` is verbatim and
candidate-inclusive (verification rows the model history collapses out) but can lag by a flush;
``in_memory`` is the recency authority but the collapsed *model* projection. Keep the DB display as base,
- append only the in-memory tail past the last DB row's ``(role, text)`` anchor — the verification answer
- survives a warm switch AND a not-yet-flushed live turn is kept."""
+ append only the in-memory tail past the live copy of the last DB row — the verification answer survives a
+ warm switch AND a not-yet-flushed live turn is kept. The anchor is the row's durable identity: matching on
+ ``(role, text)`` picked the LAST equal text, so a repeated message ("ok" again) hid the unflushed turn.
+ Content is the anchor only when no live row carries that identity (rows that never got one)."""
if not db_display:
return in_memory
if not in_memory:
return db_display
- def _key(msg: dict) -> tuple:
- return (msg.get("role"), _coerce_message_text(msg.get("content")))
- anchor = _key(db_display[-1])
- last_shared = max((idx for idx, msg in enumerate(in_memory) if isinstance(msg, dict) and _key(msg) == anchor), default=-1)
+ anchor_ids = _durable_ids(db_display[-1])
+ last_shared = max((idx for idx, msg in enumerate(in_memory) if _durable_ids(msg) & anchor_ids), default=-1)
+ if last_shared == -1:
+ anchor = (db_display[-1].get("role"), _coerce_message_text(db_display[-1].get("content")))
+ last_shared = max((idx for idx, msg in enumerate(in_memory) if isinstance(msg, dict)
+ and (msg.get("role"), _coerce_message_text(msg.get("content"))) == anchor), default=-1)
if last_shared == -1:
return db_display # DB tail not in memory (DB ahead, or diverged) — trust it over duplicating
return list(db_display) + list(in_memory[last_shared + 1 :])