The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): keep live reasoning after session switches and add /discard
unsupportedpastels4e35169b2bd8
diff --git a/hermes_cli/cli_chat_turn_mixin.py b/hermes_cli/cli_chat_turn_mixin.pyindex 94d49d3553b..174c38ca471 100644--- a/hermes_cli/cli_chat_turn_mixin.py+++ b/hermes_cli/cli_chat_turn_mixin.py@@ -497,6 +497,27 @@ class CLIChatTurnMixin:self._write_terminal_breadcrumb()self._pending_title = None+ def _handle_discard_command(self, cmd_original: str):+ """/discard <admission_id>: acknowledge a turn the gateway lost in a restart without replaying+ it (the owner's fenced ``prompt.resolve_unknown``), so prompts may run again."""+ from cli import _cprint+ from hermes_cli.gateway_chat_view import lost_admission+ from hermes_cli.gateway_client import GatewayClientError+ from hermes_cli.gateway_side import connection_for+ connection = connection_for(self)+ if connection is None:+ _cprint(" /discard: no gateway session is open")+ return+ try:+ lost = lost_admission(connection.reattach(self.session_id), cmd_original.partition(" ")[2].strip())+ connection.call("prompt.resolve_unknown", session_id=self.session_id,+ admission_id=lost["admission_id"], execution_generation=lost["execution_generation"])+ connection.reattach(self.session_id) # the cached pending list the next prompt checks+ except GatewayClientError as exc:+ _cprint(f" /discard: {exc}")+ return+ _cprint(f" Discarded {lost['admission_id']}; send your prompt again.")+def _chat_render_turn(self, turn, agent_thread, interrupt_msg):"""Post-turn display: errors, interrupt marker, reasoning/response panels, bell, re-queues.diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.pyindex 62b034c2070..bb10ea12af5 100644--- a/hermes_cli/gateway_agent.py+++ b/hermes_cli/gateway_agent.py@@ -406,6 +406,10 @@ class GatewayAgent:self._apply_info(state.info)if state.usage:self.apply_session_usage(state.usage)+ self._sync_show_reasoning()+ notice = self._lost_turn_notice()+ if notice:+ self._emit_print(notice)# -- reasoning display -------------------------------------------------------------------@property@@ -418,16 +422,21 @@ class GatewayAgent:sink's presence is what makes the agent ask the provider for reasoning. The gateway agentgets the same switch: the session's ``show_reasoning`` display flag, sent on each change."""self._reasoning_callback = callback- wanted = (self.session_id, callback is not None)- if wanted == self._show_reasoning_sent: # per session: a rotated id gets its own flag+ self._sync_show_reasoning()++ def _sync_show_reasoning(self):+ """Sent once per session, value and gateway life: /new, /resume and /branch rotate the session,+ and a restarted gateway starts without the flag (unset reads as off for a classic launch)."""+ show = self._reasoning_callback is not None+ if (self.session_id, show, self.connection.epoch) == self._show_reasoning_sent:returntry:self.connection.call("config.set", session_id=self.session_id, key="show_reasoning",- value="1" if wanted[1] else "0", timeout=10)+ value="1" if show else "0", timeout=10)except GatewayClientError as exc:logger.debug("config.set show_reasoning failed: %s", exc)return- self._show_reasoning_sent = wanted+ self._show_reasoning_sent = (self.session_id, show, self.connection.epoch)@propertydef service_tier(self):@@ -760,9 +769,9 @@ class GatewayAgent:try:# Inside the try: staging an image is a gateway call, and its failure is a failed turn.# Building the params consumes a ``/model --once`` pick (``_take_turn_route``).- state = self._state()- if state.gaps != state.attached_gaps: # a replay gap between turns retired the subscription- self.connection.reattach(turn.session_id, abandon=self._stop(turn))+ notice = self._prepare_submit(turn)+ if notice:+ return self._failed(history, user_message, notice, failure_reason="gateway_connection")params = self._submit_params(user_message, persist_user_message, turn_author)if self._stop(turn)():raise CallStopped("prompt.submit")@@ -840,6 +849,22 @@ class GatewayAgent:terminal stops them. Past Ctrl-C's settle bound they still run, bounded by their own timeouts."""return lambda: not self._owns(turn)+ def _prepare_submit(self, turn):+ """Before a prompt is sent: re-attach after a replay gap, give a redialled (restarted) owner the+ reasoning flag again, and return the notice of a turn lost in that restart: a prompt queued+ behind it would wait with no end, so it is refused until ``/discard``."""+ state = self._state()+ if state.gaps != state.attached_gaps: # a replay gap between turns retired the subscription+ self.connection.reattach(turn.session_id, abandon=self._stop(turn))+ self.connection.start()+ self._sync_show_reasoning()+ return self._lost_turn_notice()++ def _lost_turn_notice(self):+ from hermes_cli.gateway_chat_view import pending_lines+ pending = self._state().pending+ return "\n".join(pending_lines(pending)) if any(row["status"] == "unknown" for row in pending) else ""+def _pump(self, stream_callback, starts, turn):"""Dispatch frames until this admission's ``message.complete``; returns its payload, or thetransport error (disconnect)."""diff --git a/hermes_cli/gateway_chat_view.py b/hermes_cli/gateway_chat_view.pyindex 2782e6d77bf..2cbcbbb1124 100644--- a/hermes_cli/gateway_chat_view.py+++ b/hermes_cli/gateway_chat_view.py@@ -35,6 +35,33 @@ _BLOCKING_CONTROL_EVENTS = frozenset({})+def pending_lines(pending):+ """The owner's pending admissions as shown on attach (the classic CLI shows them before a turn)."""+ unknown = any(row["status"] == "unknown" for row in pending)+ lines = ["Execution outcome unknown after restart. Discard acknowledges the lost turn "+ "without replaying it; queued work may then continue."] if unknown else []+ for row in pending:+ admission = row["admission_id"]+ # A queued prompt is not in the transcript yet: show what is about to run (or was lost).+ preview = _pending_preview(row.get("text"))+ if row["status"] == "unknown":+ lines.append(f"Unknown admission: {admission}{preview}\n/discard {admission}")+ elif row["status"] == "queued":+ context = "waiting behind unknown work" if unknown else "waiting to run"+ lines.append(f"Queued admission: {admission} ({context}){preview}")+ return lines+++def lost_admission(snapshot, admission_id):+ """``/discard``'s row: the resume snapshot is the only source of the generation the authority+ stamped on a turn lost across an owner restart."""+ lost = next((row for row in snapshot.get("pending", [])+ if row["admission_id"] == admission_id and row["status"] == "unknown"), None)+ if lost is None:+ raise GatewayClientError("No unknown (lost) admission with that id; resume to refresh")+ return lost++class GatewayChatView:def __init__(self, client, snapshot, *, quiet=False, emitter=None, usage_file=None):self.usage_file = usage_file@@ -75,19 +102,8 @@ class GatewayChatView:return [row["admission_id"] for row in self.pending if row["status"] == "unknown"]def show_pending(self):- unknown = bool(self.unknown_admissions())- if unknown:- print("Execution outcome unknown after restart. Discard acknowledges the lost turn "- "without replaying it; queued work may then continue.", file=sys.stderr)- for row in self.pending:- admission = row["admission_id"]- # A queued prompt is not in the transcript yet: show what is about to run (or was lost).- preview = _pending_preview(row.get("text"))- if row["status"] == "unknown":- print(f"Unknown admission: {admission}{preview}\n/discard {admission}", file=sys.stderr)- elif row["status"] == "queued":- context = "waiting behind unknown work" if unknown else "waiting to run"- print(f"Queued admission: {admission} ({context}){preview}", file=sys.stderr)+ for line in pending_lines(self.pending):+ print(line, file=sys.stderr)def show_prompt(self, prompt):print(f"\n{prompt.get('description') or prompt.get('question') or 'Approval required'}", file=sys.stderr)@@ -340,13 +356,8 @@ class GatewayChatView:**({"choice": answer} if expected == "approval" else {"answer": answer}))return Trueif command == "/discard":- # Acknowledge a turn lost across an owner restart; the resume snapshot- # is the only source of the generation the authority stamped on it.snapshot = await self.client.rpc("session.resume", session_id=self.session_id)- lost = next((row for row in snapshot.get("pending", [])- if row["admission_id"] == rest.strip() and row["status"] == "unknown"), None)- if lost is None:- raise GatewayClientError("No unknown (lost) admission with that id; resume to refresh")+ lost = lost_admission(snapshot, rest.strip())await self.client.rpc("prompt.resolve_unknown", session_id=self.session_id,admission_id=lost["admission_id"], execution_generation=lost["execution_generation"])return Truediff --git a/tests/hermes_cli/test_classic_cli_gateway_session.py b/tests/hermes_cli/test_classic_cli_gateway_session.pynew file mode 100644index 00000000000..39c4e1a9ede--- /dev/null+++ b/tests/hermes_cli/test_classic_cli_gateway_session.py@@ -0,0 +1,105 @@+"""The classic CLI's gateway session across /new and a gateway restart: the reasoning flag follows the+session, and a turn lost in the restart is shown and discarded instead of blocking the next prompt."""+import asyncio+import threading+from contextlib import asynccontextmanager+from types import SimpleNamespace++import pytest++from hermes_cli.gateway_agent import GatewayAgent+from hermes_cli.gateway_client import GatewayClientError+from hermes_cli.gateway_connection import GatewayConnection++SID = "local-abc"+LOST = {"admission_id": "adm-lost", "status": "unknown", "execution_generation": 4, "text": "first ask"}+++class FakeClient:+ def __init__(self):+ self.events = asyncio.Queue()+ self.calls = []+ self.pending = []++ async def rpc(self, method, *, _timeout=None, **params):+ self.calls.append((method, params))+ if method == "session.resume":+ return {"session_id": params["session_id"], "revision": 1, "execution_generation": 7,+ "messages": [], "pending": list(self.pending)}+ if method == "prompt.resolve_unknown":+ self.pending = [row for row in self.pending if row["admission_id"] != params["admission_id"]]+ if method == "prompt.submit":+ if not self.pending: # the owner's FIFO: nothing runs behind an unknown turn+ done = {"type": "message.complete", "session_id": SID, "admission_id": "adm-1",+ "payload": {"text": "ok", "outcome": "completed"}}+ self.events.put_nowait({"jsonrpc": "2.0", "method": "event", "params": done})+ return {"admission_id": "adm-1", "status": "queued", "execution_generation": 7}+ return {}++ def methods(self):+ return [method for method, _ in self.calls]++ def reasoning_sets(self):+ return [(p["session_id"], p["value"]) for m, p in self.calls+ if m == "config.set" and p.get("key") == "show_reasoning"]+++def gateway():+ client = FakeClient()++ @asynccontextmanager+ async def connect():+ client.events = asyncio.Queue() # each gateway life is a new socket+ yield client+ conn = GatewayConnection(connect=connect)+ conn.start(5)+ yield conn, client+ conn.close()+++def run_bounded(agent, text):+ """``run_conversation`` on a thread: a prompt queued behind a lost turn would wait forever."""+ out = []+ thread = threading.Thread(target=lambda: out.append(agent.run_conversation(text)), daemon=True)+ thread.start()+ thread.join(10)+ agent._turn = None # ends a pump still waiting+ return out[0] if out else {"failed": False, "error": "still waiting"}+++def test_a_new_session_gets_the_show_reasoning_flag(gateway):+ conn, client = gateway+ agent = GatewayAgent(connection=conn, session_id=SID, reasoning_callback=print)+ agent.session_id = "local-new" # /new, /resume, /branch+ assert client.reasoning_sets() == [(SID, "1"), ("local-new", "1")]+++def test_a_restarted_gateway_gets_the_flag_again_before_the_next_turn(gateway):+ conn, client = gateway+ agent = GatewayAgent(connection=conn, session_id=SID, reasoning_callback=print)+ conn.loop.call_soon_threadsafe(client.events.put_nowait, GatewayClientError("Gateway disconnected"))+ conn._thread.join(5) # the owner hung up; the next call redials a fresh gateway+ assert run_bounded(agent, "after the restart")["final_response"] == "ok"+ assert client.reasoning_sets() == [(SID, "1"), (SID, "1")]+ methods = client.methods()+ assert max(i for i, m in enumerate(methods) if m == "config.set") < methods.index("prompt.submit")+++def test_a_prompt_behind_a_lost_turn_shows_the_discard_banner_and_discard_lets_it_run(gateway, monkeypatch):+ conn, client = gateway+ agent = GatewayAgent(connection=conn, session_id=SID)+ client.pending = [LOST]+ conn.reattach(SID) # the reconnect's resubscribe: the owner fenced the interrupted turn unknown+ refused = run_bounded(agent, "second ask")+ assert refused["failed"] and "/discard adm-lost" in refused["error"]+ assert "prompt.submit" not in client.methods() # never queued behind the lost turn++ from cli import HermesCLI+ monkeypatch.setattr("hermes_cli.gateway_side.connection_for", lambda cli: conn)+ method, _ = HermesCLI._slash_handler("discard") or (None, None)+ assert method is not None+ getattr(HermesCLI, method)(SimpleNamespace(session_id=SID), "/discard adm-lost")+ assert ("prompt.resolve_unknown", {"session_id": SID, "admission_id": "adm-lost",+ "execution_generation": 4}) in client.calls+ assert run_bounded(agent, "second ask")["final_response"] == "ok"diff --git a/website/docs/developer-guide/gateway-command-parity.md b/website/docs/developer-guide/gateway-command-parity.mdindex 3dbfe2e671b..ebf7c5ac9f7 100644--- a/website/docs/developer-guide/gateway-command-parity.md+++ b/website/docs/developer-guide/gateway-command-parity.md@@ -54,7 +54,7 @@ handler does; `client-local (cli_only)` commands are terminal/UI features of a c| `/debug` | control | HermesCLI._handle_debug_command | slash.exec | slash.exec | slash.exec | uploads logs to a public paste || `/deny` | control | messaging-only: unknown command | slash.exec | slash.exec | slash.exec | messaging-only || `/diff` | read | HermesCLI._handle_diff_command | slash.exec | slash.exec | slash.exec | |-| `/discard` | refused | messaging-only: unknown command | slash.exec: refused | slash.exec: refused | prompt.resolve_unknown | messaging-only |+| `/discard` | refused | HermesCLI._handle_discard_command | slash.exec: refused | slash.exec: refused | prompt.resolve_unknown | messaging-only || `/egress` | control | HermesCLI._cmd_egress | slash.exec | slash.exec | slash.exec | || `/export` | refused | HermesCLI._handle_export_command | slash.exec: refused | action: export | slash.exec: refused | client-local (cli_only) || `/fast` | control | HermesCLI._handle_fast_command | config.get, config.set | unavailable: advanced | slash.exec | per-session setting (gateway handler) |