The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(tui_gateway): rewind consumers pin the stable-prefix contract
kshitijk4poor96fcdfa73dcd
diff --git a/tests/tui_gateway/test_session_profile_db.py b/tests/tui_gateway/test_session_profile_db.pyindex 1c2489bd95e..28a37dd2525 100644--- a/tests/tui_gateway/test_session_profile_db.py+++ b/tests/tui_gateway/test_session_profile_db.py@@ -293,12 +293,11 @@ def test_truncation_does_not_copy_rows_into_the_launch_profile(def test_truncation_surfaces_the_profile_dbs_new_row_ids(server, launch_db, profile_db, monkeypatch):- """``survivor_user_row_ids`` must carry the profile db's post-rewrite ids.+ """``survivor_user_row_ids`` must carry the profile db's live ids.- ``replace_messages`` re-inserts the surviving prefix as NEW rows and the- client rebinds its cached stamps from this payload, so the ids have to come- from the db that actually did the rewrite. Ids minted anywhere else address- nothing in the profile's transcript, and the next rewind is refused 4018.+ The client rebinds its cached stamps from this payload, so the ids have to+ come from the db that did the rewrite. Since #82956 the kept prefix keeps its+ rows (only the dropped suffix is archived), so those ids are the ORIGINAL ones."""profile_home, pdb = profile_dbuser_row_ids: list = []@@ -325,8 +324,8 @@ def test_truncation_surfaces_the_profile_dbs_new_row_ids(if row["role"] == "user"]assert resp["result"]["survivor_user_row_ids"] == surviving- # Fresh rows, not the pre-rewind ids the client sent in.- assert user_row_ids[0] not in surviving+ # The kept prefix keeps its rows: the pre-rewind id stays valid (#82956).+ assert surviving == [user_row_ids[0]]def test_truncation_without_a_profile_uses_the_shared_handle(server, launch_db, monkeypatch):diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.pyindex af4e4aa4f97..ccdb4961631 100644--- a/tests/tui_gateway/test_tui_gateway_server.py+++ b/tests/tui_gateway/test_tui_gateway_server.py@@ -22413,7 +22413,7 @@ def test_prompt_submit_consecutive_rewinds_with_returned_survivor_row_ids(try:# Rewind 1: cut before "third" (last user turn). Survivors: turns- # "first" + "second" (+ assistant replies) — re-inserted as NEW rows.+ # "first" + "second" (+ assistant replies) keep their rows (#82956).resp1 = server.handle_request({"id": "1",@@ -22436,46 +22436,27 @@ def test_prompt_submit_consecutive_rewinds_with_returned_survivor_row_ids(row_id_map[str(original_row_ids[0])],row_id_map[str(original_row_ids[2])],]- # They must be NEW rows — the old ids are archived (active=0) now.- assert set(survivors).isdisjoint(set(original_row_ids))+ # Only the dropped suffix is archived: the kept rows keep their ids and+ # the map says so; the two cut turns map to None.+ assert survivors == [original_row_ids[0], original_row_ids[2]]assert row_id_map == {- str(original_row_ids[0]): survivors[0],- str(original_row_ids[1]): sess["history"][1]["_row_id"],- str(original_row_ids[2]): survivors[1],- str(original_row_ids[3]): sess["history"][3]["_row_id"],+ **{str(rid): rid for rid in original_row_ids[:4]},str(original_row_ids[4]): None,str(original_row_ids[5]): None,}assert "999999" not in row_id_mapsess["running"] = False- # Rewind 2a: the STALE pre-rewind id for "second" must fail closed.- stale_resp = server.handle_request(- {- "id": "2",- "method": "prompt.submit",- "params": {- "session_id": sid,- "text": "rewound second (stale id)",- "truncate_before_row_id": original_row_ids[2],- "truncate_before_user_ordinal": 1,- "confirm_truncate": True,- },- }- )- assert stale_resp.get("error") is not None- assert stale_resp["error"]["code"] == 4018- assert len(sess["history"]) == 4 # nothing cut-- # Rewind 2b: the RETURNED survivor id for "second" must succeed.+ # Rewind 2: the id the client cached BEFORE rewind 1 is still the live row+ # (no 4018 refusal, no rebind dance) — the user-facing point of #82956.resp2 = server.handle_request({- "id": "3",+ "id": "2","method": "prompt.submit","params": {"session_id": sid,"text": "rewound second (fresh id)",- "truncate_before_row_id": survivors[1],+ "truncate_before_row_id": original_row_ids[2],"truncate_before_user_ordinal": 1,"confirm_truncate": True,},diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.pyindex 5b40e80dfbf..093aa906a8a 100644--- a/tui_gateway/methods_prompt.py+++ b/tui_gateway/methods_prompt.py@@ -418,9 +418,9 @@ def _truncate_history_for_submit(rid, sid, session, params, requested_rebind_ids"turn so memory and DB stay aligned: %s",sid, ordinal, exc, exc_info=True)return _err(rid, 5008, f"failed to persist history truncation: {exc}"), {}- # Survivors were re-inserted as NEW rows: surface the fresh ids so the client- # rebinds its cached rowIds (else a second rewind refuses with 4018). None- # entries: the client must drop its cached id for that turn.+ # Surface the survivors' live ids so the client rebinds its cached rowIds+ # (a strict-prefix cut keeps them unchanged since #82956; a divergent+ # rewrite mints new rows). None entries: the client must drop that turn's id.if requested_rebind_ids is None:fields["survivor_user_row_ids"] = [_message_row_id(truncated[i]) for i in _history_user_indices(truncated)]