Skip to content

The agent that grows with you

fix(kanban): stale-claim reclaim advances consecutive_failures

Kevin Rajan48bcdf94e949
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index da9826f33d3..71653c03bac 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -2353,8 +2353,14 @@ def release_stale_claims(conn: sqlite3.Connection, *, signal_fn=None) -> int:
with write_txn(conn):
retry_status = _retry_status_for_run(conn, row["id"])
cur = conn.execute(
+ # The reclaim had no live worker to keep the claim for, so the
+ # run failed without a verdict — count it against the
+ # failure_threshold breaker (consecutive_failures), else a
+ # claim-without-spawn spins forever with the counter at 0
+ # (#111306).
"UPDATE tasks SET status = ?, claim_lock = NULL, "
- "claim_expires = NULL, worker_pid = NULL "
+ "claim_expires = NULL, worker_pid = NULL, "
+ "consecutive_failures = consecutive_failures + 1 "
"WHERE id = ? AND status = 'running' AND claim_lock IS ? "
"AND claim_expires IS NOT NULL AND claim_expires < ?",
(retry_status, row["id"], row["claim_lock"], now),
diff --git a/tests/hermes_cli/test_kanban_db.py b/tests/hermes_cli/test_kanban_db.py
index 8a6053da395..ccf8f74ed4a 100644
--- a/tests/hermes_cli/test_kanban_db.py
+++ b/tests/hermes_cli/test_kanban_db.py
@@ -248,6 +248,86 @@ def test_stale_claim_reclaim_event_records_diagnostic_payload(
assert payload["host_local"] is True
+def test_stale_claim_reclaim_without_spawn_counts_failure(kanban_home):
+ """Automatic reclaim of a claim that was never spawned (``worker_pid``
+ NULL) must increment ``consecutive_failures`` (#111306): otherwise a
+ claim-without-spawn reclaims forever, ``consecutive_failures`` stays 0,
+ and the failure_threshold breaker can never trip."""
+ with kbc.connect() as conn:
+ t = kb.create_task(conn, title="never spawned", assignee="a")
+ host = kb._claimer_id().split(":", 1)[0]
+ kb.claim_task(conn, t, claimer=f"{host}:worker")
+ # No _set_worker_pid: the claimer never spawned a worker.
+ old_expires = int(time.time()) - 3600
+ conn.execute(
+ "UPDATE tasks SET claim_expires = ? WHERE id = ?",
+ (old_expires, t),
+ )
+ assert kb.release_stale_claims(conn, signal_fn=lambda _p, _s: None) == 1
+ row = conn.execute(
+ "SELECT status, consecutive_failures FROM tasks WHERE id = ?",
+ (t,),
+ ).fetchone()
+ assert row["status"] == "ready"
+ assert row["consecutive_failures"] == 1
+
+
+def test_stale_claim_reclaim_dead_worker_counts_failure(
+ kanban_home, monkeypatch,
+):
+ """Automatic reclaim of a claim whose worker already died must increment
+ ``consecutive_failures`` (#111306): the reclaim had no live worker to
+ terminate, so it counts against the failure_threshold breaker."""
+ import hermes_cli.kanban_db as _kb
+
+ with kbc.connect() as conn:
+ t = kb.create_task(conn, title="dead worker", assignee="a")
+ host = _kb._claimer_id().split(":", 1)[0]
+ kb.claim_task(conn, t, claimer=f"{host}:worker")
+ kbd._set_worker_pid(conn, t, 12345)
+ old_expires = int(time.time()) - 3600
+ conn.execute(
+ "UPDATE tasks SET claim_expires = ? WHERE id = ?",
+ (old_expires, t),
+ )
+ monkeypatch.setattr(_kb, "_pid_alive", lambda _pid: False)
+ assert kb.release_stale_claims(conn, signal_fn=lambda _p, _s: None) == 1
+ row = conn.execute(
+ "SELECT status, consecutive_failures FROM tasks WHERE id = ?",
+ (t,),
+ ).fetchone()
+ assert row["status"] == "ready"
+ assert row["consecutive_failures"] == 1
+
+
+def test_stale_claim_extend_live_worker_does_not_count_failure(
+ kanban_home, monkeypatch,
+):
+ """The live-worker extend path must NOT increment ``consecutive_failures``
+ (#111306): extending a still-alive worker's claim is not a failure."""
+ import hermes_cli.kanban_db as _kb
+
+ with kbc.connect() as conn:
+ t = kb.create_task(conn, title="live worker", assignee="a")
+ host = _kb._claimer_id().split(":", 1)[0]
+ kb.claim_task(conn, t, claimer=f"{host}:worker")
+ kbd._set_worker_pid(conn, t, 12345)
+ old_expires = int(time.time()) - 3600
+ conn.execute(
+ "UPDATE tasks SET claim_expires = ? WHERE id = ?",
+ (old_expires, t),
+ )
+ monkeypatch.setattr(_kb, "_pid_alive", lambda _pid: True)
+ # Nothing reclaimed — the live claim is extended instead.
+ assert kb.release_stale_claims(conn, signal_fn=lambda _p, _s: None) == 0
+ row = conn.execute(
+ "SELECT status, consecutive_failures FROM tasks WHERE id = ?",
+ (t,),
+ ).fetchone()
+ assert row["status"] == "running"
+ assert row["consecutive_failures"] == 0
+
+