Skip to content

The agent that grows with you

fix(cli): exit non-zero from `sessions repair --check-only` on an unhealthy store

jango5a2f51239069
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..e75f7b7ef01
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1,2 @@
+jangomango76
+# PR #103321 salvage
diff --git a/hermes_cli/console_engine.py b/hermes_cli/console_engine.py
index 6d6b23380e3..160e76e85ec 100644
--- a/hermes_cli/console_engine.py
+++ b/hermes_cli/console_engine.py
@@ -705,7 +705,7 @@ def _sessions_optimize(_engine: HermesConsoleEngine, args: list[str]) -> None:
@_captured
-def _sessions_repair(_engine: HermesConsoleEngine, args: list[str]) -> None:
+def _sessions_repair(_engine: HermesConsoleEngine, args: list[str]) -> int | None:
ns = _parse(
"sessions repair", args, (("--check-only",), dict(action="store_true")),
(("--no-backup",), dict(action="store_true")))
@@ -721,7 +721,7 @@ def _sessions_repair(_engine: HermesConsoleEngine, args: list[str]) -> None:
return
print(f"{db_path} does not open cleanly: {reason}")
if ns.check_only:
- return
+ return 1 # _capture_output turns a non-zero status into a ConsoleCommandError carrying the printed reason
report = repair_state_db_schema(db_path, backup=not ns.no_backup)
if not report.get("repaired"):
raise ConsoleCommandError(f"Repair failed: {report.get('error')}")
diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.py
index ba3b226e769..37b95fd7b67 100644
--- a/hermes_cli/sessions_cmd.py
+++ b/hermes_cli/sessions_cmd.py
@@ -97,7 +97,7 @@ def _cmd_repair(args):
return
print(f"✗ {db_path} does not open cleanly: {reason}")
if getattr(args, "check_only", False):
- return
+ return 1
print("Repairing (a backup copy is made first)…")
report = repair_state_db_schema(db_path, backup=not getattr(args, "no_backup", False))
if report.get("repaired"):
diff --git a/tests/hermes_cli/test_sessions_repair_check_only_exit.py b/tests/hermes_cli/test_sessions_repair_check_only_exit.py
new file mode 100644
index 00000000000..834a40ea641
--- /dev/null
+++ b/tests/hermes_cli/test_sessions_repair_check_only_exit.py
@@ -0,0 +1,28 @@
+"""`hermes sessions repair --check-only` must fail (non-zero) when the store is unhealthy.
+
+Automation gates on the exit status; printing the reason and exiting 0 read as "healthy"
+(#63386, PR #103321).
+"""
+import argparse
+from pathlib import Path
+
+import hermes_state
+from hermes_cli import sessions_cmd
+from hermes_state import SessionDB
+
+
+def test_check_only_exit_status_tracks_probe_verdict(tmp_path, monkeypatch):
+ db_path = tmp_path / "state.db"
+ SessionDB(db_path=db_path).close()
+ monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", db_path)
+ args = argparse.Namespace(check_only=True, no_backup=False)
+
+ assert not sessions_cmd._cmd_repair(args)
+ # Destroy the schema header so the probe fails on its first statement.
+ with open(db_path, "r+b") as f:
+ f.seek(100)
+ f.write(b"\xff" * (4096 - 100))
+ for side in ("-wal", "-shm"):
+ Path(str(db_path) + side).unlink(missing_ok=True)
+ assert sessions_cmd._cmd_repair(args) == 1
+ assert db_path.stat().st_size > 0, "check-only must not touch the file"