The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): classify repaired rows in rewind rebinds
poisdahld67583ac66ee
diff --git a/tests/test_tui_gateway_server.py b/tests/test_tui_gateway_server.pyindex c44326cb376..487d1380f8f 100644--- a/tests/test_tui_gateway_server.py+++ b/tests/test_tui_gateway_server.py@@ -19735,6 +19735,66 @@ def test_prompt_submit_consecutive_rewinds_with_returned_survivor_row_ids(server._sessions.pop(sid, None)+def test_prompt_submit_rebind_map_clears_active_row_hidden_by_sequence_repair(+ monkeypatch, tmp_path+):+ """The bounded map classifies physical active IDs before user;user repair."""+ from hermes_state import SessionDB++ db = SessionDB(db_path=tmp_path / "rowid-repaired-wedge.db")+ session_key = "real-db-rowid-repaired-wedge"+ db.create_session(session_key, "cli")+ physical = [+ {"role": "user", "content": "first fragment"},+ {"role": "user", "content": "second fragment"},+ {"role": "assistant", "content": "combined reply"},+ {"role": "user", "content": "target"},+ {"role": "assistant", "content": "target reply"},+ ]+ with db._lock:+ db._insert_message_rows(db._conn, session_key, physical)+ db._conn.commit()+ physical_ids = [message["_row_id"] for message in physical]+ repaired = db.get_messages_as_conversation(+ session_key, repair_alternation=True, include_row_ids=True+ )+ # Provider repair merges the wedge and necessarily drops the second+ # physical user's row identity from the replay view.+ assert physical_ids[1] not in {+ server._message_row_id(message) for message in repaired+ }++ sess = _session(+ history=[dict(message) for message in repaired], session_key=session_key+ )+ sid = "rowid-repaired-wedge-sid"+ server._sessions[sid] = sess+ monkeypatch.setattr(server, "_get_db", lambda: db)+ monkeypatch.setattr(server, "_start_agent_build", lambda *a, **k: None)+ monkeypatch.setattr(server, "_start_inflight_turn", lambda *a, **k: None)++ try:+ response = server.handle_request(+ {+ "id": "1",+ "method": "prompt.submit",+ "params": {+ "session_id": sid,+ "text": "retry target",+ "truncate_before_row_id": physical_ids[3],+ "rebind_survivor_row_ids": [*physical_ids, 999_999],+ "confirm_truncate": True,+ },+ }+ )+ assert response.get("error") is None, response+ row_id_map = response["result"]["survivor_row_id_map"]+ assert row_id_map[str(physical_ids[1])] is None+ assert "999999" not in row_id_map+ finally:+ server._sessions.pop(sid, None)++def test_prompt_submit_unconfirmed_truncation_refuses_before_target_resolution(monkeypatch,):diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.pyindex 8d45f0092c0..8eb898ff712 100644--- a/tui_gateway/methods_prompt.py+++ b/tui_gateway/methods_prompt.py@@ -85,7 +85,11 @@ def _find_user_turn_by_row_id(history: list, target_row_id: int):return None-def _load_durable_truncation_history(session: dict, fallback_sid: str = ""):+def _load_durable_truncation_history(+ session: dict,+ fallback_sid: str = "",+ repair_alternation: bool = True,+):"""Load the durable live-replay transcript, or None when it cannot be proven safe."""session_key = str(session.get("session_key") or fallback_sid or "")if not session_key:@@ -96,7 +100,9 @@ def _load_durable_truncation_history(session: dict, fallback_sid: str = ""):if not callable(get_conv):return Nonehistory = get_conv(- session_key, repair_alternation=True, include_row_ids=True+ session_key,+ repair_alternation=repair_alternation,+ include_row_ids=True,)except Exception:logger.debug(@@ -695,16 +701,18 @@ def _(rid, params: dict) -> dict:for message in historyif isinstance((row_id := _message_row_id(message)), int)}- if requested_rebind_ids is not None and any(- _message_row_id(message) is None for message in history- ):+ if requested_rebind_ids is not None:# Row-id fallback can resolve a durable target even- # when the live list is too misaligned to stamp safely.- # Read the authoritative pre-write active-id set so a- # rewritten row is never mistaken for an untouched+ # when the live list is too misaligned to stamp safely,+ # and alternation repair can merge a physical user;user+ # pair while preserving only the first row id. Read the+ # authoritative un-repaired pre-write active-id set so+ # a rewritten row is never mistaken for an untouched# archived/ancestor row by the bounded client map.durable_rebind_history = _load_durable_truncation_history(- session, truncation_key+ session,+ truncation_key,+ repair_alternation=False,)if durable_rebind_history is None:raise RuntimeError(