Skip to content

The agent that grows with you

fix(cli): Ctrl+D mid-turn detaches from the gateway instead of interrupting

Tekniumff2bf493d736
diff --git a/hermes_cli/cli_tui_mixin.py b/hermes_cli/cli_tui_mixin.py
index 3f2d293f034..a456e92ac6b 100644
--- a/hermes_cli/cli_tui_mixin.py
+++ b/hermes_cli/cli_tui_mixin.py
@@ -1140,13 +1140,13 @@ class CLITuiMixin:
run_in_terminal(_suspend)
def _tui_handle_ctrl_d(self, event):
- """Ctrl+D deletes under the cursor (readline); exits only on empty input, like bash/zsh.
- Pending attached images count as input so the user doesn't lose them silently."""
+ """Ctrl+D deletes under the cursor (readline); exits only on empty input (pending images count),
+ like bash/zsh. Mid-turn it detaches: the gateway finishes the turn, Ctrl+C stops it (LC-6b)."""
buf = event.app.current_buffer
if buf.text:
buf.delete()
elif not self._attached_images:
- self._should_exit = True
+ self._should_exit, self._detach_on_exit = True, bool(self._agent_running)
event.app.exit()
def _tui_recall_without_recollapse(self, buf, move):
diff --git a/hermes_cli/cli_tui_runtime_mixin.py b/hermes_cli/cli_tui_runtime_mixin.py
index 34a277761de..b8fcca01cac 100644
--- a/hermes_cli/cli_tui_runtime_mixin.py
+++ b/hermes_cli/cli_tui_runtime_mixin.py
@@ -492,7 +492,11 @@ class CLITuiRuntimeMixin:
# Without this line the terminal sits silent through the whole cleanup window.
with suppress(Exception):
print(f"{_DIM}{t('cli.tui.shutting_down')}{_RST}", flush=True)
- if self.agent and self._agent_running:
+ from hermes_cli.cli_shutdown import _gateway_owns_session_end
+ if self.agent and self._agent_running and not (
+ getattr(self, "_detach_on_exit", False) and _gateway_owns_session_end(self.agent)):
+ # A gateway turn left by Ctrl+D is not interrupted: ``end_session`` below detaches this
+ # viewer and the owner closes the row once the turn settles (gateway/session_viewer_close).
with suppress(Exception):
request_hard_interrupt(self.agent)
if self._voice_recorder:
@@ -535,7 +539,6 @@ class CLITuiRuntimeMixin:
logger.debug("Could not delete session on exit: %s", e)
# run_conversation() fires on_session_end on normal completion; only fire here mid-turn. A gateway
# turn fires it in the gateway when the interrupted turn finalizes.
- from hermes_cli.cli_shutdown import _gateway_owns_session_end
if self.agent and self._agent_running and not _gateway_owns_session_end(self.agent):
_invoke_interrupted_session_end(self.agent, self.agent.session_id, "shutdown")
_run_cleanup()
diff --git a/tests/hermes_cli/test_gateway_session_end_ownership.py b/tests/hermes_cli/test_gateway_session_end_ownership.py
index 96fa8a74511..164a95ce217 100644
--- a/tests/hermes_cli/test_gateway_session_end_ownership.py
+++ b/tests/hermes_cli/test_gateway_session_end_ownership.py
@@ -89,3 +89,41 @@ def test_interrupted_one_shot_leaves_on_session_end_to_the_gateway_turn(mock_inv
one_shot = SimpleNamespace(agent=_gateway_agent("gw-session"), session_id="gw-session")
cli_mod._emit_interrupted_session_end(one_shot)
assert not [c for c in mock_invoke_hook.call_args_list if c.args == ("on_session_end",)]
+
+
+def _ctrl_d(cli):
+ buf = SimpleNamespace(text="")
+ event = SimpleNamespace(app=SimpleNamespace(current_buffer=buf, exit=MagicMock()))
+ cli._tui_handle_ctrl_d(event)
+ event.app.exit.assert_called_once()
+
+
+def _shutdown_mid_turn(cli_mod, cli):
+ cli._session_db = MagicMock()
+ with patch.object(cli_mod, "_run_cleanup"), patch.object(cli, "_print_exit_summary"), \
+ patch.object(cli, "_release_active_session"):
+ cli._tui_shutdown()
+
+
+@patch("hermes_cli.lifecycle.invoke_hook")
+def test_ctrl_d_mid_turn_detaches_without_interrupting_the_gateway_turn(_hook, cli_mod):
+ """#134889 LC-6b: EOF while a gateway turn runs leaves it running; the detach closes the row later."""
+ cli = cli_mod.HermesCLI()
+ cli.agent = _gateway_agent("gw-session")
+ cli._agent_running = True
+ cli._attached_images = []
+ _ctrl_d(cli)
+ _shutdown_mid_turn(cli_mod, cli)
+ calls = [c.args[0] for c in cli.agent.connection.call.call_args_list]
+ assert "session.interrupt" not in calls
+ cli._session_db.end_session.assert_called_once_with("gw-session", "cli_close")
+
+
+@patch("hermes_cli.lifecycle.invoke_hook")
+def test_ctrl_c_force_exit_mid_turn_still_interrupts_the_gateway_turn(_hook, cli_mod):
+ cli = cli_mod.HermesCLI()
+ cli.agent = _gateway_agent("gw-session")
+ cli._agent_running = True
+ _shutdown_mid_turn(cli_mod, cli)
+ calls = [c.args[0] for c in cli.agent.connection.call.call_args_list]
+ assert "session.interrupt" in calls