Skip to content

The agent that grows with you

fix(gateway): signal busy instead of queueing a mid-turn edit/rewind

chelsealong00d41aa52c92
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index 2710ae8ca8d..43bc506e42e 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -13362,6 +13362,43 @@ def test_prompt_submit_can_truncate_before_user_ordinal(monkeypatch):
server._sessions.pop("sid", None)
+def test_prompt_submit_truncation_signals_busy_instead_of_queueing(monkeypatch):
+ """#113942: editing a message while the turn it belongs to is still running
+
+ must not be silently absorbed as a steered correction or a plain follow-up
+ queued to run AFTER the live turn — both would drop the truncation and
+ leave the original (un-edited) turn's reply intact, which reads to the
+ user as "my edit was rejected". It must surface as the same "session busy"
+ (4009) the desktop client's edit path already retries through (interrupt,
+ then poll) until the turn actually clears.
+ """
+ agent = types.SimpleNamespace() # no redirect/steer support -> would fall to queue+interrupt
+ server._sessions["sid"] = _session(agent=agent, running=True)
+
+ try:
+ resp = server.handle_request(
+ {
+ "id": "1",
+ "method": "prompt.submit",
+ "params": {
+ "session_id": "sid",
+ "text": "edited while thinking",
+ "truncate_before_user_ordinal": 0,
+ "confirm_truncate": True,
+ "confirm_empty_truncate": True,
+ },
+ }
+ )
+
+ assert resp.get("error", {}).get("code") == 4009
+ assert "session busy" in resp["error"]["message"]
+ # The edit must not have been silently accepted as a queued follow-up.
+ assert server._sessions["sid"].get("queued_prompt") is None
+ assert server._sessions["sid"]["running"] is True
+ finally:
+ server._sessions.pop("sid", None)
+
+
def test_prompt_submit_refuses_turn_when_truncate_persist_fails(monkeypatch):
"""If replace_messages fails during edit/regenerate truncate, do not run the turn.
diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.py
index 0b7fb57cebc..106b359404e 100644
--- a/tui_gateway/methods_prompt.py
+++ b/tui_gateway/methods_prompt.py
@@ -632,6 +632,14 @@ def _(rid, params: dict) -> dict:
if internal_hosted_submit:
return _err(rid, 4091, "hosted room member session is busy")
busy_transport = t or session.get("transport")
+ if has_truncation:
+ # A rewind/edit/restore/regenerate must land as a truncation, never as a
+ # steered correction or a plain follow-up queued to run after the live
+ # turn — either would silently drop the history cut the user asked for.
+ # Signal busy so the caller's own interrupt-then-retry loop (already
+ # built for exactly this race — see desktop's `runRewindSubmit`) waits
+ # for `running` to clear and resubmits with the truncation intact.
+ return _err(rid, 4009, "session busy")
busy_response = _handle_busy_submit(
rid, sid, session, text, busy_transport, queued=bool(params.get("queued")), turn_author=turn_author)
if busy_response is not None: