The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): retain repeated turns while reconciling legacy history
David Dudok de Witb4aa2519bfe9
diff --git a/tests/tui_gateway/test_history_identity.py b/tests/tui_gateway/test_history_identity.pynew file mode 100644index 00000000000..c593703a6f0--- /dev/null+++ b/tests/tui_gateway/test_history_identity.py@@ -0,0 +1,17 @@+"""Repeated content cannot stand in for persisted transcript identity."""+from tui_gateway.server import _reconcile_display_with_live+++def test_repeated_answer_keeps_later_unflushed_turn_by_row_identity():+ first = {'role': 'assistant', 'content': 'OK', '_row_id': 10}+ fresh = [{'role': 'user', 'content': 'again', '_row_id': 11},+ {'role': 'assistant', 'content': 'OK'}]+ assert _reconcile_display_with_live([first], [first, *fresh]) == [first, *fresh]+ other = dict(first, _row_id=99)+ assert _reconcile_display_with_live([first], [other]) == [first]+++def test_legacy_projection_uses_ordered_prefix_not_last_equal_answer():+ first = [{'role': 'user', 'content': 'continue'}, {'role': 'assistant', 'content': 'OK'}]+ second = [{'role': 'user', 'content': 'continue'}, {'role': 'assistant', 'content': 'OK'}]+ assert _reconcile_display_with_live(first, first + second) == first + seconddiff --git a/tui_gateway/session_registry.py b/tui_gateway/session_registry.pyindex 9a89f2fd6bc..9f99bdf1da7 100644--- a/tui_gateway/session_registry.py+++ b/tui_gateway/session_registry.py@@ -699,6 +699,24 @@ def _durable_ids(msg) -> set:return ids | ({("row", row_id)} if isinstance(row_id, int) and not isinstance(row_id, bool) else set())+def _legacy_display_anchor(display: list[dict], memory: list[dict]) -> int:+ """Align an identity-free model prefix without consuming later repeated turns."""+ prefix = [(msg.get('role'), _coerce_message_text(msg.get('content'))) for msg in display]+ cursor = 0+ for index, msg in enumerate(memory):+ if not isinstance(msg, dict):+ continue+ value = (msg.get('role'), _coerce_message_text(msg.get('content')))+ while cursor < len(prefix) and prefix[cursor] != value:+ cursor += 1 # Verification candidates may be absent from the model projection.+ if cursor == len(prefix):+ return -1+ cursor += 1+ if cursor == len(prefix):+ return index+ return -1++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 andcandidate-inclusive (verification rows the model history collapses out) but can lag by a flush;@@ -706,7 +724,7 @@ def _reconcile_display_with_live(db_display: list[dict], in_memory: list[dict])append only the in-memory tail past the live copy of the last DB row — the verification answer survives awarm 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)."""+ Identity-free projections align their ordered prefix; conflicting durable IDs never match by text."""if not db_display:return in_memoryif not in_memory:@@ -715,9 +733,9 @@ def _reconcile_display_with_live(db_display: list[dict], in_memory: list[dict])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 anchor_ids and any(_durable_ids(msg) for msg in in_memory):+ return db_display+ last_shared = _legacy_display_anchor(db_display, in_memory)if last_shared == -1:return db_display # DB tail not in memory (DB ahead, or diverged) — trust it over duplicatingreturn list(db_display) + list(in_memory[last_shared + 1 :])