Skip to content

The agent that grows with you

fix(gateway): reject boolean ordinals and bare confirm_truncate on prompt.submit

StanleyStetson4d79bd3d0256
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..19bf61873f2
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+StanleyStetson <[email protected]>
diff --git a/tests/test_tui_gateway_server.py b/tests/test_tui_gateway_server.py
index 5ec9a0d1429..d614cb39c2c 100644
--- a/tests/test_tui_gateway_server.py
+++ b/tests/test_tui_gateway_server.py
@@ -4394,6 +4394,89 @@ def test_prompt_submit_rejects_negative_truncate_ordinal(monkeypatch):
server._sessions.pop("trunc-sid", None)
+def test_prompt_submit_refuses_boolean_ordinal(monkeypatch):
+ """A JSON `true` ordinal must return 4004, not coerce to turn 1.
+
+ bool is an int subclass, so `int(True) == 1`: a client bug that sends
+ `truncate_before_user_ordinal: true` with confirm_truncate would aim a
+ confirmed rewind at the SECOND user turn and hard-truncate everything
+ after the first — the same silent-loss class as #82756.
+ """
+ history = [
+ {"role": "user", "content": "first"},
+ {"role": "assistant", "content": "reply 1"},
+ {"role": "user", "content": "second"},
+ {"role": "assistant", "content": "reply 2"},
+ ]
+ server._sessions["bool-trunc-sid"] = _session(history=list(history))
+ monkeypatch.setattr(
+ server, "_start_agent_build", lambda *a, **k: pytest.fail("must not start a turn")
+ )
+ monkeypatch.setattr(
+ server, "_start_inflight_turn", lambda *a, **k: pytest.fail("must not start a turn")
+ )
+
+ try:
+ resp = server.handle_request(
+ {
+ "id": "1",
+ "method": "prompt.submit",
+ "params": {
+ "session_id": "bool-trunc-sid",
+ "text": "new turn",
+ "truncate_before_user_ordinal": True,
+ "confirm_truncate": True,
+ },
+ }
+ )
+ assert resp.get("error") is not None
+ assert resp["error"]["code"] == 4004
+ assert "must be an integer" in resp["error"]["message"]
+ assert server._sessions["bool-trunc-sid"]["history"] == history
+ finally:
+ server._sessions.pop("bool-trunc-sid", None)
+
+
+def test_prompt_submit_refuses_confirm_truncate_without_target(monkeypatch):
+ """confirm_truncate with no ordinal is leaked rewind state — fail fast.
+
+ The desktop auto-attaches confirm_truncate whenever it builds truncation
+ params (#82756); a bare flag on an ordinary submit means the client's
+ composer state is corrupted. Refusing loudly surfaces the client bug
+ instead of quietly ignoring the flag.
+ """
+ history = [
+ {"role": "user", "content": "first"},
+ {"role": "assistant", "content": "reply 1"},
+ ]
+ server._sessions["bare-confirm-sid"] = _session(history=list(history))
+ monkeypatch.setattr(
+ server, "_start_agent_build", lambda *a, **k: pytest.fail("must not start a turn")
+ )
+ monkeypatch.setattr(
+ server, "_start_inflight_turn", lambda *a, **k: pytest.fail("must not start a turn")
+ )
+
+ try:
+ resp = server.handle_request(
+ {
+ "id": "1",
+ "method": "prompt.submit",
+ "params": {
+ "session_id": "bare-confirm-sid",
+ "text": "new turn",
+ "confirm_truncate": True,
+ },
+ }
+ )
+ assert resp.get("error") is not None
+ assert resp["error"]["code"] == 4004
+ assert "confirm_truncate requires" in resp["error"]["message"]
+ assert server._sessions["bare-confirm-sid"]["history"] == history
+ finally:
+ server._sessions.pop("bare-confirm-sid", None)
+
+
def test_prompt_submit_refuses_unconfirmed_nonempty_truncation(monkeypatch):
"""An ordinal without confirm_truncate must not drop the session tail.
diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.py
index 9cede691413..d0efe89f9f0 100644
--- a/tui_gateway/methods_prompt.py
+++ b/tui_gateway/methods_prompt.py
@@ -162,7 +162,21 @@ def _(rid, params: dict) -> dict:
# the upgrade resumes the child's transcript as a normal conversation.
if session.get("lazy") and _child_run_active(str(session.get("session_key") or "")):
return _err(rid, 4009, "subagent still running — wait for it to finish")
+ # confirm_truncate with no target is malformed: the flag is consent for
+ # a specific cut, and a client that sends it bare has leaked rewind
+ # state onto an ordinary submit (#82756). Fail fast instead of quietly
+ # ignoring the flag so the broken client state is surfaced.
+ if is_truthy_value(params.get("confirm_truncate")) and truncate_user_ordinal is None:
+ return _err(
+ rid,
+ 4004,
+ "confirm_truncate requires truncate_before_user_ordinal",
+ )
if truncate_user_ordinal is not None:
+ # bool is an int subclass: a JSON `true` would coerce via int() to
+ # ordinal 1 and aim a confirmed rewind at the second user turn.
+ if isinstance(truncate_user_ordinal, bool):
+ return _err(rid, 4004, "truncate_before_user_ordinal must be an integer")
try:
ordinal = int(truncate_user_ordinal)
except (TypeError, ValueError):