Skip to content

The agent that grows with you

fix(tui): a refused image send no longer wedges later text sends on an owned chat

Teknium9b14af776426
diff --git a/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py b/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
index cc3bf758103..141bec39e52 100644
--- a/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
+++ b/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
@@ -316,3 +316,41 @@ def test_a_prompt_the_owner_cannot_queue_is_refused_with_why_and_when(monkeypatc
finally:
server._sessions.pop(sid, None)
db.close()
+
+
+def test_a_refused_image_send_does_not_wedge_later_text_sends(monkeypatch, tmp_path):
+ """RT-37c live finding: an image send refused during a gateway turn left the image staged in the
+ legacy session, so every later text-only send was refused as "a rewind or attachment" too."""
+ db = SessionDB(db_path=tmp_path / "state.db")
+ sid, key, turns, _emitted = _legacy(monkeypatch, db)
+ try:
+ db.create_session(key, "test")
+ epoch = begin_runtime_epoch(db, instance_id="gateway")
+ first = admit_session_input(db, epoch=epoch, principal_id="bot", session_id=key, request_id="dm-1",
+ payload={"text": "a DM"})
+ claimed = claim_session_input(db, epoch=epoch, session_id=key)
+ _owner(monkeypatch, db, key, epoch)
+ image = tmp_path / "red.png"
+ image.write_bytes(b"\x89PNG\r\n\x1a\n")
+ server._sessions[sid]["attached_images"] = [str(image)] # what image.attach stages
+
+ refused = server.handle_request({"id": "i", "method": "prompt.submit",
+ "params": {"session_id": sid, "text": "look at this"}})
+ assert refused["error"]["data"] == {"reason": "SESSION_OWNER_BUSY"}
+ assert "image" in refused["error"]["message"] and "removed" in refused["error"]["message"] # never silent
+ assert server._sessions[sid]["attached_images"] == []
+
+ # The bot turn ends and the next one starts; the user types a plain follow-up.
+ settle_session_input(db, epoch=epoch, admission_id=first["admission_id"], generation=claimed["generation"],
+ outcome="completed")
+ admit_session_input(db, epoch=epoch, principal_id="bot", session_id=key, request_id="dm-2",
+ payload={"text": "another DM"})
+ claim_session_input(db, epoch=epoch, session_id=key)
+ reply = server.handle_request({"id": "t", "method": "prompt.submit",
+ "params": {"session_id": sid, "text": "just text"}})
+
+ assert "error" not in reply, reply # before: 4090 "a rewind or attachment cannot join its queue"
+ assert reply["result"]["forwarded"] is True and turns == []
+ finally:
+ server._sessions.pop(sid, None)
+ db.close()
diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.py
index 40e8c0a24e1..9b52995d44a 100644
--- a/tui_gateway/methods_prompt.py
+++ b/tui_gateway/methods_prompt.py
@@ -726,6 +726,8 @@ def _submit_to_ledger_owner(rid, sid, session, text, params, refusal) -> dict:
if not owner_forward.forwardable(refusal, session, text, params):
why = ("a rewind or attachment cannot join its queue" if isinstance(text, str) and text.strip()
else "this kind of message cannot join its queue")
+ if _unstage_refused_images(session):
+ why = "an image cannot join its queue, so it was removed from this message"
else:
try:
receipt = owner_forward.forward_prompt(sid, session, text)
@@ -747,6 +749,20 @@ def _submit_to_ledger_owner(rid, sid, session, text, params, refusal) -> dict:
{"reason": SESSION_OWNER_BUSY})
+def _unstage_refused_images(session) -> int:
+ """Drop the images staged for a submit the owner's FIFO cannot take (RT-37c live finding).
+
+ Every other refusal leaves ``attached_images`` staged because the turn that ends the refusal (the
+ legacy turn once the slot frees) consumes them. Here no legacy turn comes: while the gateway owns the
+ chat, a staged image makes every later text-only send unforwardable too, so the chat stays refused
+ behind an image the composer may no longer show. The refusal names the removal, so the user re-attaches
+ it knowingly instead of losing it silently."""
+ with session["history_lock"]:
+ dropped = len(session.get("attached_images") or ())
+ session["attached_images"] = []
+ return dropped
+
+
def _lock_in_submit_turn(
rid, sid, session, text, params, has_truncation, requested_rebind_ids, hosted_task, display_kind):
"""Under ``history_lock``: refuse watch-child races / malformed truncation, apply the