Skip to content

The agent that grows with you

fix(cli): start-and-quit leaves no session row, as on main

Tekniumfd2e0015b192
diff --git a/hermes_cli/cli_tui_runtime_mixin.py b/hermes_cli/cli_tui_runtime_mixin.py
index fe574ed129f..2ac9d30fc54 100644
--- a/hermes_cli/cli_tui_runtime_mixin.py
+++ b/hermes_cli/cli_tui_runtime_mixin.py
@@ -464,6 +464,13 @@ class CLITuiRuntimeMixin:
_aio_probe.set_event_loop_policy(_SelectEventLoopPolicy())
return True
+ def _closing_session_id(self):
+ """The session quit closes: the agent's, or (gateway) the session opened at the banner before
+ any agent was built. Main created its row lazily at the first turn, so with no agent it had none."""
+ if self.agent:
+ return self.agent.session_id
+ return self.session_id if getattr(self, "_gateway_session_open", False) else None
+
def _tui_shutdown(self):
"""Teardown after the app exits: interrupt agent, stop voice/pet, persist + close session, cleanup, exit summary."""
from cli import _DIM, _RST, _cprint, _invoke_interrupted_session_end, _run_cleanup, set_approval_callback, set_secret_capture_callback, set_sudo_password_callback
@@ -491,21 +498,22 @@ class CLITuiRuntimeMixin:
# On SIGHUP/SIGTERM the agent thread may be reaped before its own persistence runs.
self._persist_active_session_before_close()
- if self._session_db and self.agent:
+ _closing_sid = self._closing_session_id()
+ if self._session_db and _closing_sid:
try:
- self._session_db.end_session(self.agent.session_id, "cli_close")
+ self._session_db.end_session(_closing_sid, "cli_close")
except (Exception, KeyboardInterrupt) as e:
logger.debug("Could not close session in DB: %s", e)
if not self._delete_session_on_exit:
# Drop the empty row of a start-and-quit session so /resume stays clean.
try:
- self._discard_session_if_empty(self.agent.session_id)
+ self._discard_session_if_empty(_closing_sid)
except (Exception, KeyboardInterrupt) as e:
logger.debug("Could not prune empty session: %s", e)
else:
# /exit --delete: remove transcripts + SQLite history.
try:
- _sid = self.agent.session_id
+ _sid = _closing_sid
if self._session_db.delete_session(_sid, sessions_dir=get_hermes_home() / "sessions"):
_cprint(f" {_DIM}{t('cli.tui.session_deleted', session_id=_escape(_sid))}{_RST}")
else:
diff --git a/tests/hermes_cli/test_gateway_session_store.py b/tests/hermes_cli/test_gateway_session_store.py
index 6e4d42d9deb..cce01bd4289 100644
--- a/tests/hermes_cli/test_gateway_session_store.py
+++ b/tests/hermes_cli/test_gateway_session_store.py
@@ -367,3 +367,15 @@ def test_bare_continue_finds_the_last_session_the_cli_created(tmp_path, monkeypa
from hermes_cli.main import _resolve_last_session
assert _resolve_last_session("cli") == "local-cli"
assert _resolve_last_session("tui") == "local-tui"
+
+
+def test_start_and_quit_closes_the_gateway_session_with_no_agent():
+ """The gateway session exists from the banner on, before any agent is built (main created its row
+ lazily at the first turn): quit-immediately still ends and discards it, as main leaves no row."""
+ from hermes_cli.cli_tui_runtime_mixin import CLITuiRuntimeMixin
+ host = types.SimpleNamespace(agent=None, session_id="local-new", _gateway_session_open=True)
+ assert CLITuiRuntimeMixin._closing_session_id(host) == "local-new"
+ host._gateway_session_open = False
+ assert CLITuiRuntimeMixin._closing_session_id(host) is None
+ host.agent = types.SimpleNamespace(session_id="local-agent")
+ assert CLITuiRuntimeMixin._closing_session_id(host) == "local-agent"