Skip to content

The agent that grows with you

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.py
index 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 None
if not source:
# Guess from the path shape.
p = str(path)
diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.py
index 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 1
return
try:
@@ -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 1
elif 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 1
if 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 1
title = " ".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 1
try:
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 1
except ValueError as e:
print(f"Error: {e}")
+ return 1
elif action in ("pin", "unpin"):
# CLI surface for the durable "keep" flag (issue #52955). Pinned
diff --git a/tests/hermes_cli/test_sessions_error_exit_codes.py b/tests/hermes_cli/test_sessions_error_exit_codes.py
new file mode 100644
index 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()