The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): a slash command typed after /queue X runs after X's turn
Teknium95dfcd5cdb43
diff --git a/hermes_cli/cli_tui_runtime_mixin.py b/hermes_cli/cli_tui_runtime_mixin.pyindex 75d9af97534..5dc81cd048a 100644--- a/hermes_cli/cli_tui_runtime_mixin.py+++ b/hermes_cli/cli_tui_runtime_mixin.py@@ -30,15 +30,24 @@ class CLITuiRuntimeMixin:def _tui_process_loop(self):"""REPL worker thread: drain ``_pending_input``, run idle housekeeping, dispatch each input."""+ held = Nonewhile not self._should_exit:try:+ if held is not None:+ held, user_input = self._tui_hold_step(held)+ if user_input is not None:+ self._tui_process_one_input(user_input)+ continuetry:user_input = self._pending_input.get(timeout=0.1)except queue.Empty:if not self._agent_running:self._tui_idle_tick()continue- self._tui_process_one_input(user_input)+ from hermes_cli.gateway_queue import OwnQueueHold+ held = OwnQueueHold.begin(self, user_input)+ if held is None:+ self._tui_process_one_input(user_input)except Exception as e:if isinstance(e, OSError) and e.errno == errno.EIO:self._mark_terminal_io_broken("process_loop")@@ -46,6 +55,20 @@ class CLITuiRuntimeMixin:continuelogger.warning("process_loop unhandled error (msg may be lost): %s", e)+ def _tui_hold_step(self, hold):+ """One step of holding a local input behind this viewer's own ``/queue`` admission (#134889 N5):+ ``(hold or None, input to run now or None)``. A turn the owner runs now goes first; the held+ input runs once that admission was taken, left the queue, or the bounded wait ran out."""+ with suppress(Exception):+ self._drain_process_notifications("cli-idle") # adopts the admission on its message.start+ item = hold.take_gateway_turn(self._pending_input)+ if item is not None:+ return hold, item+ if hold.done():+ return None, hold.item+ time.sleep(0.05)+ return hold, None+def _tui_idle_tick(self):"""Idle housekeeping between inputs (agent not running)."""self._check_config_mcp_changes() # auto-reload MCP on mcp_servers changediff --git a/hermes_cli/gateway_queue.py b/hermes_cli/gateway_queue.pyindex def8dd43fea..f55d20d3f6b 100644--- a/hermes_cli/gateway_queue.py+++ b/hermes_cli/gateway_queue.py@@ -107,6 +107,95 @@ def published_queue(cli, store) -> list[str]:return [str(row.get("text") or "") for row in rows if row.get("status") == "queued"] + fresh+# How long the input loop holds a local input behind this viewer's own ``/queue`` admission at most.+OWN_QUEUE_HOLD_S = 2.0+++def _own_head(state):+ """This viewer's ``/queue`` admission at the head of the owner's queue, not yet adopted; None when+ the head is someone else's, nothing is queued, or the FIFO is paused on an ``unknown`` row."""+ rows = [row for row in state.pending or () if isinstance(row, dict)]+ if any(row.get("status") == "unknown" for row in rows):+ return None # paused until the owner resolves that row: never hold typing behind it+ queued = sorted((row for row in rows if row.get("status") == "queued" and row.get("admission_id")),+ key=lambda row: int(row.get("sequence") or 0))+ if queued:+ head = queued[0]["admission_id"]+ else: # submitted after the last published list (``published_queue``'s fresh rows)+ listed = {row.get("admission_id") for row in rows}+ head = next((a for a in state.queued_inputs+ if a not in listed and state.queued_at.get(a, -1) >= state.pending_seq), None)+ return head if head in state.queued_inputs and head not in state.adopted else None+++class OwnQueueHold:+ """A local input (a slash command, a typed turn) taken while this viewer's own ``/queue`` admission+ is the owner's next turn. On main both sat in ``_pending_input`` in entry order; now the admission+ is in the owner's FIFO and its turn is adopted only when the owner's ``message.start`` arrives, so+ the input loop holds the local item until that admission was taken (or left the queue).++ Bounded: an ``unknown`` row (a paused FIFO) never starts a hold; an owner that publishes an idle+ list still queuing it (beyond the one a just-settled turn publishes before its claim) releases it;+ and ``OWN_QUEUE_HOLD_S`` caps the wait. The admission itself is never touched.+ """++ def __init__(self, item, state, admission):+ import time+ self.item, self.state, self.admission = item, state, admission+ self.seq = state.pending_seq+ # A turn the owner reports running settles with one idle list (still queuing this admission)+ # before its drain claims it: that list is expected, a further one means the FIFO is paused.+ self.allowed_idle_lists = 1 if state.info.get("running") else 0+ self.idle_lists = 0+ self.deadline = time.monotonic() + OWN_QUEUE_HOLD_S+ self.taken = False++ @classmethod+ def begin(cls, cli, item):+ from cli import _SeededQueryMessage+ from hermes_cli.gateway_tools_notify import gateway_store+ from tools.process_registry_notifications import TimelineNotification+ if getattr(cli, "_agent_running", False) or isinstance(item, (_SeededQueryMessage, TimelineNotification)):+ return None # a turn the owner already runs (or a seeded prompt) is never held+ store = gateway_store(cli) if getattr(cli, "session_id", None) else None+ if store is None:+ return None+ state = _state(cli, store)+ admission = _own_head(state)+ return cls(item, state, admission) if admission else None++ def take_gateway_turn(self, pending):+ """Pop a turn the owner is running now (an adopted admission, a notification header) from the+ head of ``_pending_input``: those run before the held item, typed items stay behind it."""+ from tools.process_registry_notifications import TimelineNotification+ with pending.mutex:+ if not pending.queue:+ return None+ head = pending.queue[0]+ if not (getattr(head, "admission_id", None) or isinstance(head, TimelineNotification)):+ return None+ pending.queue.popleft()+ pending.unfinished_tasks = max(0, pending.unfinished_tasks - 1)+ if getattr(head, "admission_id", None) == self.admission:+ self.taken = True+ return head++ def done(self) -> bool:+ import time+ if self.taken or time.monotonic() >= self.deadline:+ return True+ state = self.state+ if _own_head(state) != self.admission:+ # Adopted: its item is at the head of ``_pending_input``, taken on the next step. Otherwise+ # it started unseen (the idle tick recovers it), was cancelled, or is no longer next.+ return self.admission not in state.adopted+ if state.pending_seq != self.seq:+ self.seq = state.pending_seq+ if not state.info.get("running"):+ self.idle_lists += 1+ return self.idle_lists > self.allowed_idle_lists # idle and still not claiming it: stop waiting++STARTED_REFUSALS = frozenset({"stale_generation", "admission_started"})diff --git a/tests/hermes_cli/test_cli_queue_gateway_fifo.py b/tests/hermes_cli/test_cli_queue_gateway_fifo.pyindex f8ef594e43c..d9f8b1f1502 100644--- a/tests/hermes_cli/test_cli_queue_gateway_fifo.py+++ b/tests/hermes_cli/test_cli_queue_gateway_fifo.py@@ -590,3 +590,94 @@ def test_a_steer_that_falls_back_to_queue_is_a_gateway_admission_and_images_stay_busy_enter(local, "queue", "plain")assert local._pending_input.get_nowait() == "plain"+++def _run_loop(cli, until):+ """Run main's ``_tui_process_loop`` in a thread; returns ``(processed, stop)``."""+ import threading+ import time+ processed = []++ def one(item):+ processed.append((time.monotonic(), item))+ if len(processed) >= until:+ cli._should_exit = True+ cli._should_exit = False+ cli._tui_process_one_input = one+ cli._tui_idle_tick = lambda: cli._drain_process_notifications("cli-idle")+ thread = threading.Thread(target=cli._tui_process_loop, daemon=True)+ thread.start()++ def stop(timeout):+ thread.join(timeout)+ cli._should_exit = True+ thread.join(2)+ return processed, stop+++def test_probe_slash_command_typed_after_queue_runs_after_it():+ """Review round 2, major 2: ``/queue X`` then ``/new`` typed while busy. On main both sat in+ ``_pending_input`` in that order; with X in the owner's FIFO the input loop must still run X first,+ not let ``/new`` detach the session before the owner claims X."""+ from tests.waiting import wait_until+ from tests.hermes_cli.test_gateway_agent import SID, frame+ from tests.hermes_cli.test_gateway_tools_notify import _connected, _idle_cli+ conn, client = _connected()+ try:+ cli = _idle_cli(conn)+ state = conn.state(SID)+ state.queued_inputs["adm-q"] = TEXT+ state.apply_event("session.info", {"running": True, "pending": [+ {"admission_id": "adm-1", "status": "started", "sequence": 1, "text": "turn"},+ {"admission_id": "adm-q", "status": "queued", "sequence": 2, "text": TEXT}]})+ cli._pending_input.put("/new") # typed while the CLI's turn ran; that turn just returned+ processed, stop = _run_loop(cli, 2)+ wait_until(lambda: cli._pending_input.empty(), timeout=5) # the loop took /new and holds it+ # The owner settles the turn, then claims X.+ frames = [frame("session.info", {"running": False, "pending": [+ {"admission_id": "adm-q", "status": "queued", "sequence": 2, "text": TEXT}]}, admission=None),+ frame("message.start", {}, admission="adm-q")]++ async def put(): # not _play: the held loop consumes the frames as they land+ for item in frames:+ await client.events.put(item)+ asyncio.run_coroutine_threadsafe(put(), conn.loop).result(5)+ stop(5)+ order = [str(item) for _, item in processed]+ assert order == [TEXT, "/new"], f"input loop runs {order[0]!r} before the /queue prompt"+ assert getattr(processed[0][1], "admission_id", None) == "adm-q"+ finally:+ conn.close()++[email protected]("pending, running", [+ ([{"admission_id": "adm-0", "status": "unknown", "sequence": 1, "text": "lost"},+ {"admission_id": "adm-q", "status": "queued", "sequence": 2, "text": TEXT}], False),+ ([{"admission_id": "adm-q", "status": "queued", "sequence": 2, "text": TEXT}], False),+ ([{"admission_id": "adm-q", "status": "queued", "sequence": 2, "text": TEXT}], True),+])+def test_typing_stays_responsive_when_the_owner_queue_is_paused(pending, running):+ """The hold is bounded: a FIFO paused on an ``unknown`` row runs a typed command at once, and an owner+ that never claims this viewer's admission (idle, or busy with someone else's turn) delays it by at+ most ``OWN_QUEUE_HOLD_S``; the admission stays queued, never dropped."""+ import time+ from hermes_cli.gateway_queue import OWN_QUEUE_HOLD_S+ from tests.hermes_cli.test_gateway_agent import SID+ from tests.hermes_cli.test_gateway_tools_notify import _connected, _idle_cli+ assert OWN_QUEUE_HOLD_S >= 2.0+ conn, _client = _connected()+ try:+ cli = _idle_cli(conn)+ state = conn.state(SID)+ state.queued_inputs["adm-q"] = TEXT+ state.apply_event("session.info", {"running": running, "pending": pending})+ started = time.monotonic()+ cli._pending_input.put("/help")+ processed, stop = _run_loop(cli, 1)+ stop(OWN_QUEUE_HOLD_S + 2)+ assert [str(item) for _, item in processed] == ["/help"]+ waited = processed[0][0] - started+ assert waited < (0.5 if pending[0]["status"] == "unknown" else OWN_QUEUE_HOLD_S + 1.0), waited+ assert state.queued_inputs == {"adm-q": TEXT}+ finally:+ conn.close()