The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): /stop typed mid-turn shows its interrupt and stop results
kshitijk4poord87e7536e042
diff --git a/hermes_cli/cli_commands_session_tools.py b/hermes_cli/cli_commands_session_tools.pyindex e67a2b7a8a5..ce85178e216 100644--- a/hermes_cli/cli_commands_session_tools.py+++ b/hermes_cli/cli_commands_session_tools.py@@ -119,7 +119,7 @@ class CLICommandsSessionToolsMixin:return _stop_background_work()if getattr(self, "agent", None):from agent.interrupt_compat import request_hard_interrupt- print(f" {t('cli.tui.interrupting')}")+ _cp(f" {t('cli.tui.interrupting')}")request_hard_interrupt(self.agent)from agent.memory_provider import spawn_context_threadspawn_context_thread(_stop_background_work, name="cli-stop").start()@@ -296,7 +296,8 @@ class CLICommandsSessionToolsMixin:def _stop_background_work() -> None:"""``/stop``'s cleanup: kill the background processes, then interrupt the background (async)- delegations. Mid-turn it runs on a worker thread and prints its result from there."""+ delegations. Mid-turn it runs on a worker thread, so it prints through ``_cprint``: a plain+ print() from off the input loop is buried by the prompt_toolkit redraw."""from hermes_cli.gateway_tools import process_registry as _registry, process_viewprocess_registry, view = _registry(), process_view()if view is not None:@@ -305,17 +306,17 @@ def _stop_background_work() -> None:# Background subagents live in their own registry, not the process registry.n_async = view.active_delegations() if view is not None else _probe("tools.async_delegation", "active_count", 0)if not running and not n_async:- return print(f" {_t('stop.none_running')}")+ return _cp(f" {_t('stop.none_running')}")if running:- print(f" {_t('stop.stopping', count=len(running))}")- print(f" {_t('stop.stopped', count=process_registry.kill_all(source='cli.stop'))}")+ _cp(f" {_t('stop.stopping', count=len(running))}")+ _cp(f" {_t('stop.stopped', count=process_registry.kill_all(source='cli.stop'))}")if n_async:if view is not None:interrupted = view.interrupt_delegations() # the gateway interrupts this session's ownelse:from tools.async_delegation import interrupt_allinterrupted = interrupt_all(reason='/stop')- print(f" {_t('stop.interrupted_delegations', count=interrupted)}")+ _cp(f" {_t('stop.interrupted_delegations', count=interrupted)}")def print_session_diff(cli, result: dict, stat_only: bool) -> None:diff --git a/tests/hermes_cli/test_gateway_tools.py b/tests/hermes_cli/test_gateway_tools.pyindex 9e003f447d9..06db58780e0 100644--- a/tests/hermes_cli/test_gateway_tools.py+++ b/tests/hermes_cli/test_gateway_tools.py@@ -133,14 +133,19 @@ def test_agents_before_the_first_turn_reads_the_opened_sessions_processes(gatewaassert ("process.list", {"session_id": SID}) in client.calls-def test_stop_typed_mid_turn_interrupts_the_turn_then_stops_processes(gateway, monkeypatch, capsys):+def test_stop_typed_mid_turn_interrupts_the_turn_then_stops_processes(gateway, monkeypatch):"""/stop typed while a turn runs is dispatched inline and interrupts it like Ctrl+C (tracker#134889 N4), instead of being queued until the turn it should stop has finished. Only theinterrupt runs on the UI thread: ``kill_all`` waits out each process's stop grace period, so it- runs on a worker and the input is back at once (Greptile 4277259986)."""+ runs on a worker and the input is back at once (Greptile 4277259986). Both its notices go+ through ``_cprint``: a plain print() from the Enter handler or the worker is buried by the+ prompt_toolkit redraw, so a mid-turn /stop printed nothing (tracker #134889 CLI-27)."""import threadingfrom types import SimpleNamespace+ import cli as cli_modulefrom cli import HermesCLI+ painted = []+ monkeypatch.setattr(cli_module, "_cprint", painted.append)_agent, client = gatewayview = gateway_tools.process_view()assert view is not None@@ -168,7 +173,7 @@ def test_stop_typed_mid_turn_interrupts_the_turn_then_stops_processes(gateway, mrelease.set()for worker in [t for t in threading.enumerate() if t.name == "cli-stop"]:worker.join(5)- out = capsys.readouterr().out+ out = "\n".join(painted)assert stopped.is_set() and "Interrupting agent" in out and "Stopped 1 process(es)." in outassert any(method == "process.stop" for method, _ in client.calls)