Skip to content

The agent that grows with you

fix(cli): adopt a started /queue admission by its id, never by its text

Teknium5f39eb52216d
diff --git a/cli.py b/cli.py
index 7c7d80058a0..73d015f08de 100644
--- a/cli.py
+++ b/cli.py
@@ -829,11 +829,14 @@ class _VoiceInputMessage:
class _SeededQueryMessage:
"""Sentinel for a ``-q`` prompt seeded into an interactive session; treated LITERALLY (no slash/!/file-drop)."""
- __slots__ = ("images", "text")
+ __slots__ = ("admission_id", "images", "text")
- def __init__(self, text: str, images=None):
+ def __init__(self, text: str, images=None, admission_id=None):
self.text = text or ""
self.images = list(images or [])
+ # A ``/queue`` admission the gateway is running (#134889 N5): ``run_conversation`` pumps that
+ # admission by this id, never by comparing the (sanitized) text.
+ self.admission_id = admission_id
def __str__(self) -> str:
return self.text
diff --git a/hermes_cli/cli_process_notifications.py b/hermes_cli/cli_process_notifications.py
index 49321751549..ccfc2c9394c 100644
--- a/hermes_cli/cli_process_notifications.py
+++ b/hermes_cli/cli_process_notifications.py
@@ -90,6 +90,8 @@ class CLIProcessNotificationsMixin:
user_input = user_input.text
is_seeded_query = isinstance(user_input, _SeededQueryMessage)
if is_seeded_query:
+ from hermes_cli.gateway_tools_notify import arm_adopted
+ arm_adopted(self, user_input)
user_input = (user_input.text, user_input.images) if user_input.images else user_input.text
return user_input, is_voice_input, is_seeded_query
diff --git a/hermes_cli/gateway_connection.py b/hermes_cli/gateway_connection.py
index 8ffa0a34e9f..e53fd9312d3 100644
--- a/hermes_cli/gateway_connection.py
+++ b/hermes_cli/gateway_connection.py
@@ -66,6 +66,10 @@ class SessionState:
# runs for one is painted like a typed turn (``gateway_tools_notify.queue_admitted_turn``).
self.pending: list = []
self.queued_inputs: dict = {}
+ # Started ``/queue`` admissions waiting for their turn (``admission_id -> receipt-like``), and the
+ # one whose item the input loop just took (``gateway_tools_notify.arm_adopted``).
+ self.adopted: dict = {}
+ self.armed_admission: str | None = None
# ``session.replay_gap``s seen, and how many a re-attach covered: the owner retired this viewer's
# subscription on overflow, so its frames stop until ``GatewayConnection.reattach``.
self.gaps = self.attached_gaps = 0
diff --git a/hermes_cli/gateway_tools_notify.py b/hermes_cli/gateway_tools_notify.py
index 57bcda914d7..e792c4ed248 100644
--- a/hermes_cli/gateway_tools_notify.py
+++ b/hermes_cli/gateway_tools_notify.py
@@ -70,10 +70,12 @@ def queue_admitted_turn(cli, store) -> None:
if queued_text is not None:
# A ``/queue`` prompt this viewer admitted (#134889 N5): the owner runs it now, and main's
# input path paints and pumps it as a typed turn. Literal: it is a prompt, never a command.
+ # Adopted by admission id (the item carries it), never by text: the input path may rewrite
+ # the text, and a second ``prompt.submit`` would run the prompt twice.
from cli import _SeededQueryMessage
- state.admitted_turn = {"admission_id": params["admission_id"], "text": queued_text,
- "execution_generation": params.get("execution_generation")}
- _put_first(cli._pending_input, _SeededQueryMessage(queued_text))
+ state.adopted[params["admission_id"]] = {"admission_id": params["admission_id"],
+ "execution_generation": params.get("execution_generation")}
+ _put_first(cli._pending_input, _SeededQueryMessage(queued_text, admission_id=params["admission_id"]))
return
card = _card(params)
if card is None:
@@ -95,10 +97,24 @@ def _put_first(pending, item):
pending.not_empty.notify()
+def arm_adopted(cli, item) -> None:
+ """The input loop took ``item``: when it is an adopted ``/queue`` admission, the next
+ ``run_conversation`` pumps exactly that admission (``take_admitted_turn``)."""
+ admission = getattr(item, "admission_id", None)
+ store = gateway_store(cli) if admission else None
+ if store is not None:
+ store.connection.state(getattr(cli, "session_id", None)).armed_admission = admission
+
+
def take_admitted_turn(agent, text):
"""The admitted turn ``run_conversation(text)`` replays instead of submitting; None (and the
- stale adoption dropped) for any other turn."""
+ stale notification adoption dropped) for any other turn. A ``/queue`` admission is matched by the
+ id ``arm_adopted`` recorded, a notification turn by its model text."""
state = agent._state()
+ armed, state.armed_admission = getattr(state, "armed_admission", None), None
+ adopted = state.adopted.pop(armed, None) if armed else None
+ if adopted is not None:
+ return adopted
adopted = getattr(state, "admitted_turn", None)
state.admitted_turn = None
if not adopted or adopted.get("text") != text:
diff --git a/tests/hermes_cli/test_cli_queue_gateway_fifo.py b/tests/hermes_cli/test_cli_queue_gateway_fifo.py
index 70a92b469af..30afd51b32c 100644
--- a/tests/hermes_cli/test_cli_queue_gateway_fifo.py
+++ b/tests/hermes_cli/test_cli_queue_gateway_fifo.py
@@ -166,6 +166,7 @@ def test_the_owner_claim_of_a_queued_prompt_runs_as_a_typed_turn_without_a_secon
item = cli._pending_input.get_nowait()
assert str(item) == TEXT and type(item).__name__ == "_SeededQueryMessage" # literal, never a command
assert conn.state(SID).queued_inputs == {}
+ cli._tui_unwrap_input(item) # the input loop takes it: its admission is armed by id
agent = GatewayAgent(connection=conn, session_id=SID)
agent._pending_cli_user_message = {"role": "user", "content": TEXT}
result = agent.run_conversation(TEXT, conversation_history=[])
@@ -173,3 +174,56 @@ def test_the_owner_claim_of_a_queued_prompt_runs_as_a_typed_turn_without_a_secon
assert result["final_response"] == "ran"
finally:
conn.close()
+
+
+def test_an_adopted_queued_prompt_never_submits_a_second_admission():
+ """Review N5-F3: the adopted item is matched to its admission by id, not by text. A queued text the
+ input path's cleanup rewrites (a leaked CPR, a bracketed-paste marker) must still be pumped as the
+ admission the owner is running, never sent again as a new ``prompt.submit``."""
+ from cli import _strip_leaked_bracketed_paste_wrappers, _strip_leaked_terminal_responses_with_meta
+ from hermes_cli.gateway_agent import GatewayAgent
+ from tests.hermes_cli.test_gateway_agent import SID, frame
+ from tests.hermes_cli.test_gateway_tools_notify import _connected, _idle_cli, _play
+ text = "compare cursor log ^[[12;40R with the expected rows [200~"
+ conn, client = _connected()
+ try:
+ cli = _idle_cli(conn)
+ conn.state(SID).queued_inputs["adm-q"] = text
+ _play(conn, client, [frame("message.start", {}, admission="adm-q"),
+ frame("message.complete", {"text": "ran", "status": "complete"}, admission="adm-q")])
+ cli._drain_process_notifications("cli-idle")
+ client.script = [frame("message.complete", {"text": "again", "status": "complete"}, admission="adm-1")]
+ item = cli._pending_input.get_nowait()
+ user_input, _, seeded = cli._tui_unwrap_input(item)
+ assert seeded and user_input == text
+ # Even if a sanitizer still rewrote it (chat() strips lone surrogates), identity decides.
+ user_input = _strip_leaked_bracketed_paste_wrappers(user_input)
+ user_input, _ = _strip_leaked_terminal_responses_with_meta(user_input)
+ agent = GatewayAgent(connection=conn, session_id=SID)
+ agent._pending_cli_user_message = {"role": "user", "content": user_input}
+ result = agent.run_conversation(user_input, conversation_history=[])
+ assert "prompt.submit" not in [m for m, _ in client.calls], "second admission submitted"
+ assert result["final_response"] == "ran"
+ finally:
+ conn.close()
+
+
+def test_a_typed_turn_with_the_same_text_is_never_taken_for_the_adopted_admission():
+ """Identity cuts both ways: a typed input that happens to equal an adopted queued text, run before the
+ adopted item was unwrapped, is its own new submit."""
+ from hermes_cli.gateway_agent import GatewayAgent
+ from tests.hermes_cli.test_gateway_agent import SID, frame
+ from tests.hermes_cli.test_gateway_tools_notify import _connected, _idle_cli, _play
+ conn, client = _connected()
+ try:
+ cli = _idle_cli(conn)
+ conn.state(SID).queued_inputs["adm-q"] = TEXT
+ _play(conn, client, [frame("message.start", {}, admission="adm-q")])
+ cli._drain_process_notifications("cli-idle")
+ client.script = [frame("message.complete", {"text": "typed", "status": "complete"}, admission="adm-1")]
+ agent = GatewayAgent(connection=conn, session_id=SID)
+ agent._pending_cli_user_message = {"role": "user", "content": TEXT}
+ agent.run_conversation(TEXT, conversation_history=[])
+ assert "prompt.submit" in [m for m, _ in client.calls]
+ finally:
+ conn.close()