The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): error paths return non-zero exit codes (delete/rename/prune/import)
Tekniumaca40d1d63c8
diff --git a/hermes_cli/foreign_sessions.py b/hermes_cli/foreign_sessions.pyindex 40407c7f653..1d565c336f3 100644--- a/hermes_cli/foreign_sessions.py+++ b/hermes_cli/foreign_sessions.py@@ -456,6 +456,11 @@ def run_sessions_import(args, db=None) -> Optional[str]:path = getattr(args, "path", None)if path:+ # Report a missing file distinctly instead of the misleading+ # "cannot infer source" (SES-10).+ if not Path(path).exists():+ print(f"Error: file not found: {path}")+ return Noneif not source:# Guess from the path shape.p = str(path)diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.pyindex e2ef5998d74..3cc69f44bce 100644--- a/hermes_cli/sessions_cmd.py+++ b/hermes_cli/sessions_cmd.py@@ -305,7 +305,13 @@ def cmd_sessions(args, sessions_parser=None):if action == "import":from hermes_cli.foreign_sessions import run_sessions_import- run_sessions_import(args)+ result = run_sessions_import(args)+ # A path was explicitly given but nothing imported → real error (bad+ # path, unknown source, no turns). Propagate a non-zero exit so+ # scripts can detect the failure (SES-04/SES-10). An interactive+ # picker cancel (no path) returning None is a normal no-op → exit 0.+ if result is None and getattr(args, "path", None):+ return 1returntry:@@ -314,7 +320,7 @@ def cmd_sessions(args, sessions_parser=None):db = SessionDB()except Exception as e:print(f"Error: Could not open session database: {e}")- return+ return 1# Hide third-party tool sessions by default, but honour explicit --source_source = getattr(args, "source", None)@@ -865,7 +871,7 @@ def cmd_sessions(args, sessions_parser=None):resolved_session_id = db.resolve_session_id(args.session_id)if not resolved_session_id:print(f"Session '{args.session_id}' not found.")- return+ return 1# Note when the explicit target is pinned — the user named this id# directly so we honor the delete, but a pin is a "keep" flag and# silently destroying it (round-3 QA SES-01) is surprising.@@ -886,6 +892,7 @@ def cmd_sessions(args, sessions_parser=None):print(f"Deleted session '{resolved_session_id}'.")else:print(f"Session '{args.session_id}' not found.")+ return 1elif action == "prune" and getattr(args, "never_active", False):# Separate branch on purpose: the shared prune/archive selector is@@ -930,7 +937,7 @@ def cmd_sessions(args, sessions_parser=None):filters = build_prune_filters(args)except ValueError as e:print(f"Error: {e}")- return+ return 1if action == "archive" and not any(v for k, v in filters.items() if k != "older_than_days"@@ -1053,15 +1060,27 @@ def cmd_sessions(args, sessions_parser=None):resolved_session_id = db.resolve_session_id(args.session_id)if not resolved_session_id:print(f"Session '{args.session_id}' not found.")- return+ return 1title = " ".join(args.title)+ # Reject blank / whitespace-only / newline-bearing titles (SES-05):+ # an empty title renders as "—" and embedded newlines corrupt the+ # `list` table. length is validated in set_session_title; guard+ # emptiness + control chars here.+ if not title.strip():+ print("Error: title cannot be empty or whitespace-only.")+ return 1+ if "\n" in title or "\r" in title:+ print("Error: title cannot contain newlines.")+ return 1try:if db.set_session_title(resolved_session_id, title):print(f"Session '{resolved_session_id}' renamed to: {title}")else:print(f"Session '{args.session_id}' not found.")+ return 1except ValueError as e:print(f"Error: {e}")+ return 1elif action in ("pin", "unpin"):# CLI surface for the durable "keep" flag (issue #52955). Pinneddiff --git a/tests/hermes_cli/test_sessions_error_exit_codes.py b/tests/hermes_cli/test_sessions_error_exit_codes.pynew file mode 100644index 00000000000..e8ff6e5d36e--- /dev/null+++ b/tests/hermes_cli/test_sessions_error_exit_codes.py@@ -0,0 +1,49 @@+"""Regression tests: `hermes sessions` error paths return non-zero (SES-04).++Before this, delete/rename not-found, prune bad-arg, blank rename, and import+of a missing file all printed an error and returned exit 0 — a scripting/CI+hazard (a script pinning a bad id failed loudly via `pin` but deleting a bad+id "succeeded" silently). The subcommand dispatcher already maps an int+handler return to the process exit code; these tests pin the returns.+"""++from argparse import Namespace++import pytest++import hermes_cli.sessions_cmd as sc+++def _args(action, **kw):+ base = dict(+ sessions_action=action,+ session_id=None, title=None, yes=True, source=None, path=None,+ from_source=None, dry_run=False, older_than=None, newer_than=None,+ before=None, after=None, limit=50,+ )+ base.update(kw)+ return Namespace(**base)+++def test_delete_missing_returns_1(tmp_path, monkeypatch, capsys):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ from hermes_state import SessionDB+ SessionDB(tmp_path / "state.db") # initialize an empty store+ rc = sc.cmd_sessions(_args("delete", session_id="nope_xyz"))+ assert rc == 1+ assert "not found" in capsys.readouterr().out.lower()+++def test_rename_missing_returns_1(tmp_path, monkeypatch):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ from hermes_state import SessionDB+ SessionDB(tmp_path / "state.db")+ rc = sc.cmd_sessions(_args("rename", session_id="nope_xyz", title=["New"]))+ assert rc == 1+++def test_import_missing_file_returns_1(tmp_path, monkeypatch, capsys):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ rc = sc.cmd_sessions(_args("import", path=str(tmp_path / "nope.jsonl")))+ assert rc == 1+ assert "file not found" in capsys.readouterr().out.lower()