The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): re-hydrate an unstamped record after a foreign compaction
Austin Pickett389849347a75
diff --git a/tests/tui_gateway/test_out_of_band_history_adoption.py b/tests/tui_gateway/test_out_of_band_history_adoption.pyindex 89f021f86ef..3d47af10ab7 100644--- a/tests/tui_gateway/test_out_of_band_history_adoption.py+++ b/tests/tui_gateway/test_out_of_band_history_adoption.py@@ -191,6 +191,65 @@ def test_unstamped_live_record_is_caught_up_from_the_store(tmp_path, monkeypatchassert len(session["history"]) == 44 and session["history_version"] == 1+def _foreign_compaction(db) -> None:+ """Another surface compacts the session under the live record, then keeps talking on it."""+ db.archive_and_compact("s1", [+ {"role": "assistant", "content": "summary of ask0/ask1", "_compressed_summary": True},+ {"role": "user", "content": "KIWI"}, {"role": "assistant", "content": "MANGO"}], tail_count=2)+++def test_unstamped_record_is_rehydrated_after_a_foreign_compaction(tmp_path, monkeypatch):+ """The store was compacted by another surface: an unstamped pre-compaction history is stale from the+ root and must be re-hydrated from the DB exactly like the stamped path does, not kept because the+ compacted store is shorter."""+ db = SessionDB(tmp_path / "state.db")+ db.create_session("s1", source="desktop")+ _seed_exchange(db, 0)+ _seed_exchange(db, 1)+ stamped, unstamped = _hydrated_record(db, stamped=True), _hydrated_record(db, stamped=False)+ _foreign_compaction(db)+ _bind_db(monkeypatch, db)++ for session in (stamped, unstamped):+ server._adopt_out_of_band_turns(session)+ assert [m["content"] for m in session["history"]] == ["summary of ask0/ask1", "KIWI", "MANGO"]+ assert session["history"][0].get("_compressed_summary") and session["history_version"] == 1+++def test_unstamped_prompt_after_a_foreign_compaction_sends_the_compacted_transcript(tmp_path, monkeypatch):+ """Through ``prompt.submit``: the model is given the compacted transcript, not the stale pre-compaction+ one, and this turn's own user row is not adopted."""+ db = SessionDB(tmp_path / "state.db")+ db.create_session("s1", source="desktop")+ _seed_exchange(db, 0)+ _seed_exchange(db, 1)+ session = _hydrated_record(db, stamped=False)+ _foreign_compaction(db)++ contents = [m.get("content") for m in _desktop_prompt(db, session, monkeypatch)]++ assert contents == ["summary of ask0/ask1", "KIWI", "MANGO"]+++def test_unstamped_record_already_holding_the_summary_only_appends(tmp_path, monkeypatch):+ """An unstamped record that already carries the compaction (its own) is not re-hydrated: rows the+ gateway appended after it are folded in by the prefix path."""+ db = SessionDB(tmp_path / "state.db")+ db.create_session("s1", source="desktop")+ _seed_exchange(db, 0)+ _foreign_compaction(db)+ session = _hydrated_record(db, stamped=False)+ session["history"][1]["local_marker"] = True # identity survives only if the prefix is kept+ _bind_db(monkeypatch, db)+ db.append_message("s1", "user", "PEAR")+ db.append_message("s1", "assistant", "OK")++ server._adopt_out_of_band_turns(session)++ assert [m["content"] for m in session["history"]] == ["summary of ask0/ask1", "KIWI", "MANGO", "PEAR", "OK"]+ assert session["history"][1].get("local_marker") and session["history_version"] == 1++def test_unstamped_record_that_diverged_from_the_store_is_left_alone(tmp_path, monkeypatch):"""A local rewrite the store has not caught up with must never be clobbered by the store tail."""db = SessionDB(tmp_path / "state.db")diff --git a/tui_gateway/prompt_turn.py b/tui_gateway/prompt_turn.pyindex aeb2c38736f..fcf46b7e569 100644--- a/tui_gateway/prompt_turn.py+++ b/tui_gateway/prompt_turn.py@@ -561,11 +561,26 @@ def _adopt_store_tail_without_row_ids(session: dict, history: list, version: intappends itself) must be STRICTLY longer and its head must equal the in-memory view entry for entry.A diverged view (prefix mismatch, e.g. a rewind that has not landed locally), a store that is notahead (an unflushed local tail is the fresher record) and an empty in-memory history (no head to- prove against) are left alone, the same guards that keep the stamped path safe.+ prove against) are left alone.++ A compaction by another surface is the one rewrite that is NOT an append: the store then holds a+ ``_compressed_summary`` row the in-memory view has never seen, so the view is stale from the root and+ is re-hydrated from the DB like the stamped path does — a positional check alone would keep it because+ the compacted store is shorter."""rows = [m for m in _load_durable_truncation_history(session, repair_alternation=True) or []if below_ceiling(_message_row_id(m))]- if not history or len(rows) <= len(history):+ if not history:+ return+ known = {m.get("content") for m in history if isinstance(m, dict) and m.get("_compressed_summary")}+ if any(m.get("_compressed_summary") and m.get("content") not in known for m in rows):+ tail = canonicalize_replay_history(rows)+ with session["history_lock"]:+ if tail and int(session.get("history_version", 0)) == version:+ session["history"] = tail+ session["history_version"] = version + 1+ return+ if len(rows) <= len(history):returnif not all(isinstance(mem, dict) and mem.get("role") == stored.get("role")and mem.get("content") == stored.get("content")