The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(tui_gateway): ship the deep-cut gate warn-only for one release
kshitijk4poor10cf19374542
diff --git a/tests/tui_gateway/test_tui_gateway_server_deep_truncate.py b/tests/tui_gateway/test_tui_gateway_server_deep_truncate.pyindex e10fcae5328..629bc22c5ae 100644--- a/tests/tui_gateway/test_tui_gateway_server_deep_truncate.py+++ b/tests/tui_gateway/test_tui_gateway_server_deep_truncate.py@@ -19,6 +19,8 @@ def _session(agent=None, **extra):def test_prompt_submit_refuses_deep_truncation_without_confirm(monkeypatch):+ # register() rebinds the handlers onto the server namespace, so the flag is read there.+ monkeypatch.setattr(server, "DEEP_TRUNCATE_ENFORCED", True) # the next release's behaviourreplaced = []class _FakeDB:@@ -69,6 +71,7 @@ def test_prompt_submit_refuses_deep_truncation_without_confirm(monkeypatch):def test_deep_truncate_counts_every_durable_row_a_merged_carrier_holds(monkeypatch):"""A user;user run repaired into one live carrier is several durable user turns: cutting atits first row archives all of them, so it needs confirm_deep_truncate like any deep cut."""+ monkeypatch.setattr(server, "DEEP_TRUNCATE_ENFORCED", True)replaced = []physical = [{"_row_id": 101, "role": "user", "content": "a"}, {"_row_id": 102, "role": "user", "content": "b"},@@ -198,3 +201,34 @@ def test_archived_user_turns_counts_a_carrier_without_its_own_row_id():finally:methods_prompt._load_durable_truncation_history = original++def test_unconfirmed_deep_truncation_is_warn_only_this_release(monkeypatch, caplog):+ """Rollout (G15-B): an older client without confirm_deep_truncate keeps working for one release;+ the cut runs and the would-be refusal is logged."""+ import logging++ replaced = []++ class _FakeDB:+ def replace_messages(self, key, messages, **_kwargs):+ replaced.append(list(messages))++ history = []+ for turn in range(3):+ history += [{"_row_id": 100 + 2 * turn, "role": "user", "content": f"u{turn}"},+ {"_row_id": 101 + 2 * turn, "role": "assistant", "content": f"a{turn}"}]+ server._sessions["warn-only-sid"] = _session(history=list(history))+ monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())+ monkeypatch.setattr(server, "_start_agent_build", lambda *a, **k: None)+ monkeypatch.setattr(server.threading, "Thread", lambda *a, **k: types.SimpleNamespace(start=lambda: None))+ try:+ with caplog.at_level(logging.WARNING):+ result = server.handle_request({"id": "1", "method": "prompt.submit", "params": {+ "session_id": "warn-only-sid", "text": "u1", "truncate_before_row_id": 102,+ "confirm_truncate": True}})+ assert result.get("error") is None+ assert replaced == [history[:2]]+ assert "ALLOWED (warn-only release) unconfirmed deep truncation" in caplog.text+ finally:+ server._sessions.pop("warn-only-sid", None)+diff --git a/tui_gateway/contracts/prompt_voice.py b/tui_gateway/contracts/prompt_voice.pyindex f0919f23c9c..022fc6ca1de 100644--- a/tui_gateway/contracts/prompt_voice.py+++ b/tui_gateway/contracts/prompt_voice.py@@ -43,7 +43,7 @@ class PromptSubmitParams(SessionParams):truncate_before_message_id: str | None = Noneconfirm_truncate: bool | None = Noneconfirm_empty_truncate: bool | None = None- confirm_deep_truncate: bool | None = None # required when the cut archives >1 user turn+ confirm_deep_truncate: bool | None = None # send when the cut archives >1 user turn (enforced next release)rebind_survivor_row_ids: list[int] | None = None# In-process only: injected by the hosted-room / bot-relay handlers, never accepted from a# client (a client dict for ``_turn_author`` answers 4124). Excluded from the rendered wire.diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.pyindex 4fd5a55e0d1..f4b0b80c943 100644--- a/tui_gateway/methods_prompt.py+++ b/tui_gateway/methods_prompt.py@@ -436,6 +436,12 @@ def _row_ids_of(messages) -> set:return {row_id for message in messages if isinstance((row_id := _message_row_id(message)), int)}+# Rollout (#133716, maintainer decision G15-B): one release warn-only, so an older Desktop or a+# third-party client that predates confirm_deep_truncate keeps working while upgraded clients ask+# before a deep cut on their own. The next release flips this to refuse with 4033.+DEEP_TRUNCATE_ENFORCED = False++def _archived_user_turns(session, sid, history, cut_index, survivor_ids) -> int:"""User turns a cut at *cut_index* drops. A repaired live carrier stands for its whole mergedrun (own id + ``_absorbed_row_ids``), which can mix real user rows with display markers@@ -501,15 +507,17 @@ def _truncate_history_for_submit(rid, sid, session, params, requested_rebind_idsarchived_messages = len(history) - (cut_index if durable_prefix is not None else len(truncated))logger.warning(- "prompt.submit: REFUSED deep truncation of session %s (%d messages / %d user "- "turns would be archived; ordinal=%d).",+ "prompt.submit: %s unconfirmed deep truncation of session %s (%d messages / %d user "+ "turns; ordinal=%d).",+ "REFUSED" if DEEP_TRUNCATE_ENFORCED else "ALLOWED (warn-only release)",sid, archived_messages, archived_user_turns, ordinal)- return _err(- rid, 4033,- "truncation would archive later user turns; resubmit with "- "confirm_deep_truncate=true if this is intended",- data={"archived_messages": archived_messages,- "archived_user_turns": archived_user_turns}), {}+ if DEEP_TRUNCATE_ENFORCED:+ return _err(+ rid, 4033,+ "truncation would archive later user turns; resubmit with "+ "confirm_deep_truncate=true if this is intended",+ data={"archived_messages": archived_messages,+ "archived_user_turns": archived_user_turns}), {}log_fn = logger.warning if not truncated else logger.infolog_fn("prompt.submit: truncating session %s history %d -> %d messages (ordinal=%d)",diff --git a/website/docs/developer-guide/programmatic-integration.md b/website/docs/developer-guide/programmatic-integration.mdindex ba53268be24..3856f28082b 100644--- a/website/docs/developer-guide/programmatic-integration.md+++ b/website/docs/developer-guide/programmatic-integration.md@@ -73,7 +73,7 @@ A rewind / edit / regenerate is a `prompt.submit` that drops part of the stored| `truncate_before_row_id` | Integer SQLite row ID (`messages.id` / `row_id`) of the target user turn to cut at. Preferred durable address. When both ordinal and row ID are provided, gateway verifies they match (returning `4030` on mismatch). An unknown/stale row ID is refused with `4018` — it does **not** fall back to the ordinal. || `confirm_truncate` | Required whenever an ordinal, message ID, or row ID is sent. Declares that this submit really is a rewind, not an ordinary send that happens to carry leftover parameters. Sending it without a target is refused with code `4004`. || `confirm_empty_truncate` | Additionally required when the cut would leave the transcript empty (ordinal `0`). |-| `confirm_deep_truncate` | Additionally required when the cut would archive more than one user turn (anything deeper than regenerating or editing the last turn). Without it the submit is refused with code `4033`; `error.data` carries `archived_messages` and `archived_user_turns`, and nothing is written. |+| `confirm_deep_truncate` | Send it when the cut would archive more than one user turn (anything deeper than regenerating or editing the last turn), after asking the user. This release logs an unconfirmed deep cut and still runs it, so older clients keep working. The next release will refuse it with code `4033` (`error.data` carries `archived_messages` and `archived_user_turns`, and nothing is written). |A truncation parameter without `confirm_truncate` is refused with code `4004` or `4029` and nothing is written. Hosts that implement rewind must set the flag at the moment the user asks for it, and must never keep truncation parameters in state across ordinary submits. Prefer `truncate_before_row_id` (from resume `row_id` / `_row_id`) over ordinals; keep the ordinal as a back-compat / optimistic-row path only when no durable id is available yet.