The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): preserve blocking controls in finite output
Brian Fernstrom954638ede23e
diff --git a/hermes_cli/gateway_chat_view.py b/hermes_cli/gateway_chat_view.pyindex 000f1618b55..cadf804a97f 100644--- a/hermes_cli/gateway_chat_view.py+++ b/hermes_cli/gateway_chat_view.py@@ -7,6 +7,11 @@ import uuidfrom hermes_cli.gateway_client import GatewayClientError+_BLOCKING_CONTROL_EVENTS = frozenset({+ "approval.request", "approval.settled", "clarify.request", "clarify.settled",+})++class GatewayChatView:def __init__(self, client, snapshot, *, quiet=False, emitter=None, usage_file=None):self.usage_file = usage_file@@ -71,10 +76,17 @@ class GatewayChatView:self.changed.set()returnadmission = params.get("admission_id") or payload.get("admission_id")+ if self.finite and kind in _BLOCKING_CONTROL_EVENTS:+ # Approval/clarification gates block the session FIFO, not merely one+ # admission's output. Track them even when another admission owns the+ # event so a queued one-shot can detach instead of waiting forever.+ self._dispatch_event(kind, admission, payload)+ self.changed.set()+ continueif self.finite and admission:if self.finite_admission is None:# The owner can publish before prompt.submit's receipt reaches this client.- # Hold admission-scoped events until we know which admission this invocation owns.+ # Hold admission-scoped output until we know which admission this invocation owns.self._finite_events.append((kind, admission, payload))continueif admission != self.finite_admission:@@ -145,7 +157,10 @@ class GatewayChatView:def _request(self, admission, payload):self.prompts[payload["prompt_id"]] = payload- self.show_prompt(payload)+ # Finite invocations only need the session-blocking state so they can+ # detach cleanly; do not print another admission's control details.+ if not self.finite:+ self.show_prompt(payload)def _settled(self, admission, payload):self.prompts.pop(payload["prompt_id"], None)diff --git a/tests/hermes_cli/test_gateway_chat_blocking_controls.py b/tests/hermes_cli/test_gateway_chat_blocking_controls.pynew file mode 100644index 00000000000..bd8de20370a--- /dev/null+++ b/tests/hermes_cli/test_gateway_chat_blocking_controls.py@@ -0,0 +1,149 @@+"""Finite gateway chat tracks session-blocking controls without leaking foreign output."""++import asyncio++import pytest++from hermes_cli.gateway_chat_view import GatewayChatView+++def _control_event(kind, *, admission="neighbor", prompt_id="p1"):+ control = "approval" if kind.startswith("approval") else "clarify"+ payload = {+ "prompt_id": prompt_id,+ "kind": control,+ "execution_generation": 7,+ }+ if kind.endswith(".request"):+ if control == "approval":+ payload.update(command="secret command", choices=["yes", "no"])+ else:+ payload.update(question="secret question")+ return {+ "method": "event",+ "params": {+ "type": kind,+ "session_id": "stored",+ "admission_id": admission,+ "payload": payload,+ },+ }++[email protected]("kind", ["approval.request", "clarify.request"])[email protected]("timing", ["before_receipt", "after_receipt"])+async def test_oneshot_tracks_foreign_blocking_control_and_detaches(+ capsys, kind, timing+):+ class Peer:+ events = asyncio.Queue()++ async def _after_receipt(self):+ await asyncio.sleep(0)+ self.events.put_nowait({+ "method": "event",+ "params": {+ "type": "message.delta",+ "session_id": "stored",+ "admission_id": "neighbor",+ "payload": {"text": "neighbor output"},+ },+ })+ self.events.put_nowait(_control_event(kind))++ async def rpc(self, method, **params):+ assert method == "prompt.submit"+ if timing == "before_receipt":+ self.events.put_nowait({+ "method": "event",+ "params": {+ "type": "message.delta",+ "session_id": "stored",+ "admission_id": "neighbor",+ "payload": {"text": "neighbor output"},+ },+ })+ self.events.put_nowait(_control_event(kind))+ await asyncio.sleep(0)+ else:+ asyncio.create_task(self._after_receipt())+ return {"admission_id": "mine"}++ view = GatewayChatView(Peer(), {"stored_session_id": "stored"}, quiet=True)+ assert await asyncio.wait_for(view.run("query", oneshot=True), 2) == 3++ captured = capsys.readouterr()+ assert "neighbor output" not in captured.out+ assert "secret command" not in captured.err+ assert "secret question" not in captured.err+ assert "Input required; detached without cancelling" in captured.err++[email protected]("kind", ["approval.settled", "clarify.settled"])+async def test_oneshot_applies_foreign_control_settlement_before_receipt(kind):+ control = "approval" if kind.startswith("approval") else "clarify"+ pending = {+ "prompt_id": "p1",+ "kind": control,+ "execution_generation": 7,+ **({"command": "old command", "choices": ["yes", "no"]}+ if control == "approval" else {"question": "old question"}),+ }++ class Peer:+ events = asyncio.Queue()++ async def rpc(self, method, **params):+ assert method == "prompt.submit"+ self.events.put_nowait(_control_event(kind))+ self.events.put_nowait({+ "method": "event",+ "params": {+ "type": "message.complete",+ "session_id": "stored",+ "admission_id": "mine",+ "payload": {"text": "done", "outcome": "completed"},+ },+ })+ await asyncio.sleep(0)+ return {"admission_id": "mine"}++ snapshot = {"stored_session_id": "stored", "prompts": [pending]}+ view = GatewayChatView(Peer(), snapshot, quiet=True)+ assert await asyncio.wait_for(view.run("query", oneshot=True), 2) == 0+ assert "p1" not in view.prompts++[email protected]("kind", ["approval.settled", "clarify.settled"])+async def test_finite_renderer_applies_foreign_settlement_after_receipt(kind):+ control = "approval" if kind.startswith("approval") else "clarify"++ class Peer:+ events = asyncio.Queue()++ view = GatewayChatView(+ Peer(),+ {+ "stored_session_id": "stored",+ "prompts": [{+ "prompt_id": "p1",+ "kind": control,+ "execution_generation": 7,+ }],+ },+ quiet=True,+ )+ view.finite = True+ view.finite_admission = "mine"+ renderer = asyncio.create_task(view.render())+ try:+ view.client.events.put_nowait(_control_event(kind))+ await asyncio.wait_for(view.changed.wait(), 2)+ assert "p1" not in view.prompts+ finally:+ renderer.cancel()+ with pytest.raises(asyncio.CancelledError):+ await renderer