The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(sessions): one table per failure class for the exit-code contract
Teknium61e86c74ca85
diff --git a/tests/hermes_cli/test_sessions_failure_exit_codes.py b/tests/hermes_cli/test_sessions_failure_exit_codes.pyindex 36ef188f33d..faa8a4c38cf 100644--- a/tests/hermes_cli/test_sessions_failure_exit_codes.py+++ b/tests/hermes_cli/test_sessions_failure_exit_codes.py@@ -1,7 +1,9 @@-"""`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."""+"""`hermes sessions` exit status tracks the outcome: a subcommand that fails, refuses, or could not ask for+its confirmation exits non-zero, so cron/CI wrappers and `hermes sessions repair && ...` chains can tell."""+import sqlite3import sys+import timeimport pytest@@ -17,32 +19,102 @@ def _run_cli(monkeypatch, argv_tail):return 0-def test_sessions_repair_that_fails_exits_nonzero(monkeypatch, capsys):+def _old_ended_session(db_path):+ from hermes_state import SessionDB++ with SessionDB(db_path=db_path) as db:+ db.create_session("s-old", "cli")+ db.append_message("s-old", "user", "hello")+ db.end_session("s-old", "user_exit")+ old = time.time() - 100 * 86400+ raw = sqlite3.connect(str(db_path))+ with raw:+ raw.execute("UPDATE sessions SET started_at = ?, ended_at = ?", (old, old))+ raw.execute("UPDATE messages SET timestamp = ?", (old,))+ raw.close()+++def _garbage_store(db_path):+ db_path.write_bytes(b"not a sqlite database" * 200)+++def _legacy_inline_fts_store(db_path):+ from hermes_state_common import LEGACY_FTS_SQL, LEGACY_FTS_TRIGRAM_SQL, SCHEMA_SQL++ raw = sqlite3.connect(str(db_path))+ with raw:+ raw.executescript(SCHEMA_SQL + LEGACY_FTS_SQL + LEGACY_FTS_TRIGRAM_SQL)+ raw.execute("INSERT INTO sessions (id, source, started_at) VALUES ('s-old', 'cli', ?)", (time.time(),))+ raw.execute("INSERT INTO messages (session_id, timestamp, role, content) VALUES ('s-old', ?, 'user', 'hi')",+ (time.time(),))+ raw.close()+++def _vacuum_fails(monkeypatch):+ from hermes_state import SessionDB++ def vacuum(self):+ raise sqlite3.OperationalError("database is locked")+ monkeypatch.setattr(SessionDB, "vacuum", vacuum)+++def _store(monkeypatch, tmp_path, build):import hermes_statefrom hermes_constants import get_hermes_homedb_path = get_hermes_home() / "state.db"- db_path.write_bytes(b"not a sqlite database" * 200)+ build(db_path)monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", db_path)+ monkeypatch.chdir(tmp_path)+ return db_path- code = _run_cli(monkeypatch, ["repair"])- assert "Repair failed" in capsys.readouterr().out- assert code != 0[email protected]("build, failing, valid, break_it", [+ (_garbage_store, ["repair"], None, None),+ (_old_ended_session, ["optimize"], ["optimize"], _vacuum_fails),+ (_old_ended_session, ["archive"], ["archive", "--source", "cli", "--yes"], None),+ (_old_ended_session, ["prune", "--never-active", "--older-than", "not-a-duration"],+ ["prune", "--never-active", "--older-than", "2d", "--yes"], None),+ (_old_ended_session, ["export", "exports", "--format", "md"],+ ["export", "exports", "--format", "md", "--older-than", "90"], None),+], ids=["repair-failed", "optimize-vacuum-fails", "archive-no-filter", "never-active-bad-duration",+ "export-md-no-filter"])+def test_failed_or_refused_subcommand_exits_1_where_the_valid_form_exits_0(+ monkeypatch, tmp_path, build, failing, valid, break_it):+ _store(monkeypatch, tmp_path, build)+ if valid:+ assert _run_cli(monkeypatch, valid) == 0+ if break_it:+ break_it(monkeypatch)+ assert _run_cli(monkeypatch, failing) == 1[email protected]("refused, accepted", [- (["archive"], ["archive", "--source", "cli", "--yes"]),- (["prune", "--never-active", "--older-than", "not-a-duration"],- ["prune", "--never-active", "--older-than", "2d", "--yes"]),- (["export", "exports", "--format", "md"], ["export", "exports", "--format", "md", "--older-than", "90"]),- (["export", "out.jsonl", "--session-id", "no-such-session"], ["export", "out.jsonl"]),-])-def test_refused_bulk_action_exits_nonzero_where_the_valid_form_exits_zero(monkeypatch, tmp_path, refused, accepted):- from hermes_state import SessionDB[email protected]("build, argv", [+ (_legacy_inline_fts_store, ["optimize-storage"]),+ (_old_ended_session, ["delete", "s-old"]),+ (_old_ended_session, ["archive", "--source", "cli"]),+ (_old_ended_session, ["prune", "--older-than", "30"]),+], ids=["optimize-storage", "delete", "archive", "prune"])+def test_confirmation_nobody_can_answer_exits_1_and_changes_nothing(monkeypatch, tmp_path, capsys, build, argv):+ """No TTY (cron, a wrapper): EOF at the prompt is not a decline. An explicit "n" still exits 0."""+ db_path = _store(monkeypatch, tmp_path, build)- SessionDB().close()- monkeypatch.chdir(tmp_path)+ def snapshot():+ raw = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)+ try:+ return (raw.execute("SELECT id, archived FROM sessions ORDER BY id").fetchall(),+ raw.execute("SELECT name FROM sqlite_master ORDER BY name").fetchall())+ finally:+ raw.close()++ monkeypatch.setattr("builtins.input", lambda _prompt="": "n")+ assert _run_cli(monkeypatch, argv) == 0+ before = snapshot() # after the first open: SessionDB's own schema reconcile is not the command's work- assert _run_cli(monkeypatch, accepted) == 0- assert _run_cli(monkeypatch, refused) != 0+ def no_stdin(_prompt=""):+ raise EOFError+ monkeypatch.setattr("builtins.input", no_stdin)+ capsys.readouterr()+ assert _run_cli(monkeypatch, argv) == 1+ assert "Pass --yes" in capsys.readouterr().out+ assert snapshot() == before