Skip to content

The agent that grows with you

fix(sessions): repair, optimize and refused bulk actions exit non-zero on failure

John Paul Solivac479245a60bb
diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.py
index 152d636bd0d..81ef40d88a3 100644
--- a/hermes_cli/sessions_cmd.py
+++ b/hermes_cli/sessions_cmd.py
@@ -156,6 +156,7 @@ def _cmd_repair(args):
f" hermes sessions recover --source {source_hint} \\\n"
" --output recovered-state.db"
)
+ return 1
def _cmd_recover(args):
@@ -679,7 +680,7 @@ def _prune_never_active_keyed(db, args):
if seconds is None:
print(f"Error: --older-than '{older_than}' is not a duration. "
"Use a bare number of days or a form like '2d' / '1w'.")
- return
+ return 1
days = seconds / 86400.0
candidates = db.list_never_active_keyed_sessions(older_than_days=days)
if not candidates:
@@ -753,7 +754,7 @@ def _cmd_prune_or_archive(db, args, action):
if not prune and not any(v for k, v in filters.items() if k != "older_than_days"):
print("Refusing to archive every ended session: pass at least one "
"filter (e.g. --newer-than 5h, --source cli, --title codex).")
- return
+ return 1
# Prune skips archived rows unless --include-archived; archive only targets not-yet-archived rows.
filters["archived"] = None if prune and getattr(args, "include_archived", False) else False
@@ -962,7 +963,7 @@ def _cmd_optimize(db, args):
n = db.vacuum() # merges FTS5 segments then VACUUMs; returns indexes merged
except Exception as e:
print(f"Error: optimization failed: {e}")
- return
+ return 1
print(f"Optimized {n} FTS index(es).")
_print_size_change(db, before_mb)
diff --git a/tests/hermes_cli/test_sessions_failure_exit_codes.py b/tests/hermes_cli/test_sessions_failure_exit_codes.py
new file mode 100644
index 00000000000..a68523ec477
--- /dev/null
+++ b/tests/hermes_cli/test_sessions_failure_exit_codes.py
@@ -0,0 +1,45 @@
+"""`hermes sessions` exit status tracks the outcome: a maintenance subcommand that reports a failure
+exits non-zero, so cron/CI wrappers and `hermes sessions repair && ...` chains can tell."""
+
+import sys
+
+import pytest
+
+
+def _run_cli(monkeypatch, argv_tail):
+ import hermes_cli.main as main_mod
+
+ monkeypatch.setattr(sys, "argv", ["hermes", "sessions", *argv_tail])
+ try:
+ main_mod.main()
+ except SystemExit as exc:
+ return exc.code or 0
+ return 0
+
+
+def test_sessions_repair_that_fails_exits_nonzero(monkeypatch, capsys):
+ import hermes_state
+ from hermes_constants import get_hermes_home
+
+ db_path = get_hermes_home() / "state.db"
+ db_path.write_bytes(b"not a sqlite database" * 200)
+ monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", db_path)
+
+ code = _run_cli(monkeypatch, ["repair"])
+
+ assert "Repair failed" in capsys.readouterr().out
+ assert code != 0
+
+
[email protected]("refused, accepted", [
+ (["archive"], ["archive", "--source", "cli", "--yes"]),
+ (["prune", "--never-active", "--older-than", "not-a-duration"],
+ ["prune", "--never-active", "--older-than", "2d", "--yes"]),
+])
+def test_refused_bulk_action_exits_nonzero_where_the_valid_form_exits_zero(monkeypatch, refused, accepted):
+ from hermes_state import SessionDB
+
+ SessionDB().close()
+
+ assert _run_cli(monkeypatch, accepted) == 0
+ assert _run_cli(monkeypatch, refused) != 0