Skip to content

The agent that grows with you

fix(kanban): reject negative retention at the argparse boundary (from #118615)

kshitijk4poorbf8b2d802609
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index ec0da1aec00..d38c36c7657 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -4273,6 +4273,7 @@ def task_age(task: Task) -> dict:
def gc_events(conn: sqlite3.Connection, *, older_than_seconds: int = 30 * 24 * 3600) -> int:
"""Prune old done/archived events, retaining decomposition identity until task deletion."""
+ older_than_seconds = int(older_than_seconds)
if older_than_seconds < 0:
raise ValueError(
f"older_than_seconds must be >= 0, got {older_than_seconds!r}: a negative "
@@ -4288,6 +4289,7 @@ def gc_events(conn: sqlite3.Connection, *, older_than_seconds: int = 30 * 24 * 3
def gc_worker_logs(*, older_than_seconds: int = 30 * 24 * 3600, board: Optional[str] = None) -> int:
"""Delete worker log files older than the cutoff on one board; returns the count."""
+ older_than_seconds = int(older_than_seconds)
if older_than_seconds < 0:
raise ValueError(
f"older_than_seconds must be >= 0, got {older_than_seconds!r}: a negative "
diff --git a/hermes_cli/kanban_parser.py b/hermes_cli/kanban_parser.py
index a5937f6284b..3d9aab07759 100644
--- a/hermes_cli/kanban_parser.py
+++ b/hermes_cli/kanban_parser.py
@@ -42,6 +42,18 @@ def _reason(help: str):
return _arg("--reason", help=help)
+def _nonnegative_int(value: str) -> int:
+ """argparse type for retention days: a negative window builds a future cutoff
+ that matches every row, so reject it at the CLI boundary before any sweep."""
+ try:
+ parsed = int(value)
+ except (TypeError, ValueError) as exc:
+ raise argparse.ArgumentTypeError("must be an integer") from exc
+ if parsed < 0:
+ raise argparse.ArgumentTypeError("retention days must be >= 0 (0 disables that sweep)")
+ return parsed
+
+
def _run_state_args(type_help: str):
return (
_arg("--state-type", choices=("status", "outcome"), help=f"With --state-name: {type_help}"),
@@ -423,9 +435,9 @@ _SPECS = [
"to specify-style single-task promotion when the task "
"doesn't benefit from fan-out. Uses auxiliary.kanban_decomposer."),
_cmd("gc", [
- _arg("--event-retention-days", type=int, default=30,
+ _arg("--event-retention-days", type=_nonnegative_int, default=30,
help="Delete task_events older than N days for terminal tasks (default: 30; 0 disables)"),
- _arg("--log-retention-days", type=int, default=30,
+ _arg("--log-retention-days", type=_nonnegative_int, default=30,
help="Delete worker log files older than N days (default: 30; 0 disables)"),
], help="Garbage-collect archived-task workspaces, old events, and old logs"),
_cmd("repair", [_json_flag(help="Emit the repair report as JSON")],
diff --git a/tests/hermes_cli/test_kanban_gc_retention.py b/tests/hermes_cli/test_kanban_gc_retention.py
index 7d212d322af..c9456ec990a 100644
--- a/tests/hermes_cli/test_kanban_gc_retention.py
+++ b/tests/hermes_cli/test_kanban_gc_retention.py
@@ -132,3 +132,14 @@ def test_slash_kanban_gc_zero_disables(board):
with kbc.connect_closing() as conn:
assert _event_rows(conn, tid) > 0
assert log.exists()
+
+
+def test_gc_parser_rejects_negative_retention_days():
+ """``hermes kanban gc --event-retention-days -1`` is a usage error before
+ ``_cmd_gc`` runs; ``0`` still parses (the CLI treats it as 'disabled')."""
+ from hermes_cli.kanban_parser import _nonnegative_int
+ assert _nonnegative_int("0") == 0
+ with pytest.raises(argparse.ArgumentTypeError, match=">= 0"):
+ _nonnegative_int("-1")
+ with pytest.raises(argparse.ArgumentTypeError, match="integer"):
+ _nonnegative_int("thirty")