Skip to content

The agent that grows with you

fix(bot-mode): keep authored queued DMs out of the inflight dedup

Erosikab30898cb659c
diff --git a/tests/test_tui_gateway_queue_on_busy.py b/tests/test_tui_gateway_queue_on_busy.py
index 86edf8aa598..9959daadd74 100644
--- a/tests/test_tui_gateway_queue_on_busy.py
+++ b/tests/test_tui_gateway_queue_on_busy.py
@@ -206,6 +206,30 @@ def test_drop_rewrites_merged_inflight_prefix_to_followup_only():
assert session.get("queued_prompt") == {"text": "Q", "transport": "ws-1"}
+def test_enqueue_keeps_an_authored_copy_of_the_inflight_text():
+ """A relayed DM with the live prompt's text is another sender's message, not a self-duplicate."""
+ author = {"id": "bot:scout", "name": "scout", "is_bot": True}
+ session = _session()
+ session["inflight_turn"] = {"user": "status?", "assistant": "", "streaming": True, "error": ""}
+
+ server._enqueue_prompt(session, "status?", "ws-1", turn_author=author)
+
+ assert session.get("queued_prompt") == {"text": "status?", "transport": "ws-1", "turn_author": author}
+
+
+def test_drop_leaves_an_authored_entry_that_shares_the_inflight_prefix_intact():
+ """Authored envelopes never went through the text merge, so a shared prefix is the sender's own words."""
+ author = {"id": "bot:scout", "name": "scout", "is_bot": True}
+ text = "P\n\nand the rollback plan"
+ session = _session()
+ session["inflight_turn"] = {"user": "P", "assistant": "", "streaming": True, "error": ""}
+ session["queued_prompt"] = {"text": text, "transport": "ws-1", "turn_author": author}
+
+ server._drop_queued_duplicates_of_inflight_user(session)
+
+ assert session.get("queued_prompt") == {"text": text, "transport": "ws-1", "turn_author": author}
+
+
def test_hard_interrupt_queue_path_scrubs_stale_inflight_self_duplicate(monkeypatch):
"""#84417: interrupt+queue of Q must not leave P ahead of Q in the FIFO."""
monkeypatch.setattr(server, "_load_busy_input_mode", lambda: "interrupt")
diff --git a/tui_gateway/session_auto_continue.py b/tui_gateway/session_auto_continue.py
index f9981a66ef8..a8bb332edc8 100644
--- a/tui_gateway/session_auto_continue.py
+++ b/tui_gateway/session_auto_continue.py
@@ -137,8 +137,9 @@ def _enqueue_prompt(session: dict, text: Any, transport: Any, image_paths: list[
# See #84417.
_drop_queued_duplicates_of_inflight_user(session)
text_only = not image_paths and isinstance(text, str)
- # Never queue a text-only self-copy of the live prompt: draining it would restart it.
- if text_only and text.strip() == _ac_inflight_original(session) != "":
+ # Never queue a text-only self-copy of the live prompt: draining it would restart it. Another sender's
+ # identical text is their message, not a copy.
+ if text_only and not turn_author and text.strip() == _ac_inflight_original(session) != "":
return
queued = {"text": text, "transport": transport, **({"image_paths": image_paths} if image_paths else {}),
**({"turn_author": turn_author} if turn_author else {})}
@@ -157,7 +158,7 @@ def _enqueue_prompt(session: dict, text: Any, transport: Any, image_paths: list[
def _sanitize_queued_entry_vs_inflight_user(entry: Any, original: str) -> dict | None:
"""Drop (``None``) a text-only self-duplicate of the live user text, or rewrite a merged slot
``"{original}\\n\\n{later}"`` to ``later`` so the correction survives without re-firing the original. Image-bearing
- envelopes are left alone (chronology is load-bearing).
+ and authored envelopes are left alone: chronology and the sender's own words are kept.
Returns ``None`` to drop the envelope, or a (possibly rewritten) dict to keep. A merged slot
``"{original}\\n\\n{later}"`` (from ``_enqueue_prompt``'s consecutive text merge) is rewritten to just
@@ -166,7 +167,7 @@ def _sanitize_queued_entry_vs_inflight_user(entry: Any, original: str) -> dict |
if not isinstance(entry, dict):
return None
text = entry.get("text")
- if not original or entry.get("image_paths") or not isinstance(text, str):
+ if not original or entry.get("image_paths") or entry.get("turn_author") or not isinstance(text, str):
return entry
# A lossless text-merge may have glued the live original onto a later follow-up: keep the remainder.
rest = next((text[len(original + sep):] for sep in ("\n\n", "\n") if text.startswith(original + sep)), text).strip()