Skip to content

The agent that grows with you

fix(kanban): a bound run stays the crash sweep's; no recorded pid is no proof inside the claim TTL

kshitijk4poorf10386a58f17
diff --git a/hermes_cli/kanban_owner_recovery.py b/hermes_cli/kanban_owner_recovery.py
index acefb4b886c..ecb6e780925 100644
--- a/hermes_cli/kanban_owner_recovery.py
+++ b/hermes_cli/kanban_owner_recovery.py
@@ -37,7 +37,7 @@ def _admission_row(receipt):
with closing(sqlite3.connect(Path(receipt['db']).as_uri() + '?mode=ro', uri=True)) as owner:
row = owner.execute('SELECT status,outcome FROM session_admissions WHERE target_session_id=? AND request_id=?',
(receipt['session_id'], receipt['request_id'])).fetchone()
- except (OSError, KeyError, sqlite3.Error):
+ except (OSError, KeyError, TypeError, ValueError, sqlite3.Error):
return None
return (row[0], row[1]) if row is not None else None
@@ -62,7 +62,6 @@ def owner_reclaim_paused(conn, task_id):
return True
-
def _latest_run_event(conn, task_id, kind):
"""The newest ``kind`` payload on the task's CURRENT run, or ``{}``.
@@ -141,27 +140,26 @@ def owner_attempt_provably_over(conn, task_id) -> bool:
runs in exactly that shape. This asks the question that matters — can anything still be
executing this attempt? — from evidence the board already keeps:
- * no worker pid recorded for the run is alive (``tasks.worker_pid``, its ``spawned``
- and ``worker_bound`` events);
- * and the attempt is over by the owner's own verdict — the admission it recorded is
- SETTLED (``status='terminal'``) — or, when the owner left no readable verdict, the
- host-local claim has EXPIRED with no heartbeat inside
- ``DEFAULT_CLAIM_HEARTBEAT_MAX_STALE_SECONDS`` and the owner's own process is gone.
-
- The last clause used to demand the owner process be gone even when the owner had
- already settled the admission. On a live install the owner is the long-lived gateway,
- which is alive by design, so a manager-worker loss that settled the admission
- ``terminal/interrupted`` could never be reclaimed: 2026-10-22, card t_f5fad63f held
- ``running`` for 49 minutes after its managed worker died at ``worker.adopt`` 24 s in
- (claim expired 34 minutes earlier, ``last_heartbeat_at`` NULL, every recorded pid dead).
- A settled admission is the owner stating the attempt ended; it outranks the liveness of
- the process that recorded it.
-
- Missing evidence is never proof: no receipt, no live-or-dead pid evidence, a live pid whose
- identity cannot be compared, or an unreadable clock all return False and the pause holds.
+ * the run never bound an interpreter (a bound run is the crash sweep's to book);
+ * every worker pid recorded for the run (``tasks.worker_pid``, its ``spawned`` and
+ ``worker_bound`` events) is dead or recycled;
+ * and the owner's own admission is SETTLED (``status='terminal'``) with at least one
+ recorded pid, or the host-local claim has EXPIRED with no heartbeat inside
+ ``DEFAULT_CLAIM_HEARTBEAT_MAX_STALE_SECONDS`` and the admission is settled or (pids
+ recorded) the owner's process is gone.
+
+ A settled admission outranks the owner's liveness: the owner is usually the long-lived
+ gateway, alive by design, and it settles a lost managed worker's admission without
+ extending the claim.
+
+ Missing evidence is never proof: no receipt, a live pid whose identity cannot be
+ compared, no recorded pid inside the claim's TTL, or an unreadable clock all return False
+ and the pause holds.
"""
from hermes_cli import kanban_db as kb
from hermes_cli.kanban_db_dispatch import _worker_alive
+ if _latest_run_event(conn, task_id, 'worker_bound'):
+ return False # a bound run is the crash sweep's to book (bound_interpreter_gone)
receipt = _owner_receipt(conn, task_id)
if receipt is None:
return False # no receipt: this pause did not come from an owner attempt
@@ -172,19 +170,11 @@ def owner_attempt_provably_over(conn, task_id) -> bool:
return False
pids = _run_worker_pids(conn, row, task_id)
if any(_worker_alive(pid, fingerprint) for pid, fingerprint in pids):
- return False # a recorded interpreter is still live: it may still be executing
- # Nothing recorded can still be executing. A SETTLED admission (the owner's own verdict that
- # the attempt ended) is proof on its own — the owner process is a gateway, alive by design,
- # and the claim may still be inside its TTL simply because nothing extended it.
- try:
- admission = _admission_row(receipt)
- except (OSError, ValueError, KeyError, sqlite3.Error):
- admission = None
- if admission is not None and admission[0] == 'terminal':
- return True
- # Without an owner verdict, only an expired, unprogressing claim plus a gone owner proves it.
- if not pids:
return False
+ admission = _admission_row(receipt)
+ settled = admission is not None and admission[0] == 'terminal'
+ if settled and pids:
+ return True
now = int(time.time())
expires = kb._to_epoch(row['claim_expires'])
if expires is None or expires >= now:
@@ -192,7 +182,8 @@ def owner_attempt_provably_over(conn, task_id) -> bool:
heartbeat = kb._to_epoch(row['last_heartbeat_at'])
if heartbeat is not None and now - heartbeat <= kb.DEFAULT_CLAIM_HEARTBEAT_MAX_STALE_SECONDS:
return False
- return _owner_process_gone(receipt)
+ # No pid was ever recorded: only a lapsed claim lets the owner's verdict (or its death) speak.
+ return settled or (bool(pids) and _owner_process_gone(receipt))
def owner_attempt_note(conn, task_id) -> str:
@@ -208,16 +199,12 @@ def owner_attempt_note(conn, task_id) -> str:
receipt = _owner_receipt(conn, task_id)
if receipt is None:
return ""
- try:
- admission = _admission_row(receipt)
- except (OSError, ValueError, KeyError, sqlite3.Error):
- return ""
+ admission = _admission_row(receipt)
if admission is not None and admission[0] == 'terminal':
return f"owner admission settled {admission[1] or 'terminal'}"
return ""
-
def owner_reclaim_blocked(conn, task_id, *, crash_sweep=False) -> bool:
"""True when a reclaim path must leave this task alone.
diff --git a/tests/hermes_cli/test_kanban_owner_receipt_reclaim.py b/tests/hermes_cli/test_kanban_owner_receipt_reclaim.py
index c7c87dd1fb4..d12f27ae76e 100644
--- a/tests/hermes_cli/test_kanban_owner_receipt_reclaim.py
+++ b/tests/hermes_cli/test_kanban_owner_receipt_reclaim.py
@@ -185,16 +185,16 @@ def test_dead_attempt_behind_an_unresolved_receipt_is_named_and_reclaimed(board)
dict(worker_pid=os.getpid(), worker_fingerprint=kbd._process_fingerprint(os.getpid())), # live worker
dict(worker_bound=True, worker_pid=os.getpid(),
worker_fingerprint=kbd._process_fingerprint(os.getpid())), # live bound interpreter
- # Each remaining case models an attempt that MAY still be executing, so its admission is
- # UNSETTLED (``started``) — the shape a live attempt really has. A settled admission is the
- # owner's own verdict that the attempt ended and is proof on its own; see
- # test_a_settled_admission_is_proof_even_while_the_owner_gateway_is_alive below.
+ # A settled admission is proof on its own (see the next test), so these model an attempt
+ # that may still be executing: its admission is still ``started``.
dict(heartbeat_age=300, admission=("started", None)), # fresh heartbeat
dict(expires_age_seconds=-3600, admission=("started", None)), # live claim
dict(lock_host="some-other-host"), # foreign host
dict(owner_pid=os.getpid(), owner_birth=None, admission=("started", None)), # live owner in flight
dict(record_worker_pid=False, admission=("started", None)), # no pid recorded
-], ids=["live-worker", "live-bound", "fresh-heartbeat", "live-claim", "foreign-host", "live-owner", "no-pid"])
+ dict(record_worker_pid=False, expires_age_seconds=-3600), # no pid yet, settled
+], ids=["live-worker", "live-bound", "fresh-heartbeat", "live-claim", "foreign-host", "live-owner", "no-pid",
+ "no-pid-live-claim"])
def test_the_pause_still_holds_every_attempt_that_may_be_alive(board, shape):
"""Missing evidence is never proof: reclaiming beside a possibly-live attempt repeats its effects."""
board.dead_attempt("t_ghost", **shape)
@@ -205,19 +205,16 @@ def test_the_pause_still_holds_every_attempt_that_may_be_alive(board, shape):
def test_a_settled_admission_is_proof_even_while_the_owner_gateway_is_alive(board):
- """The 2026-10-22 stall: owner = the long-lived gateway, admission settled ``interrupted``.
-
- Card t_f5fad63f's managed worker died at ``worker.adopt`` 24 s in. The gateway settled the
- admission ``terminal/interrupted`` and recorded nothing on the board, the claim expired
- 34 minutes before anyone noticed, ``last_heartbeat_at`` was NULL and every recorded pid was
- dead — yet ``owner_attempt_provably_over`` demanded the OWNER process be gone. The owner is
- the gateway, which is alive by design, so every reclaim path declined for 49 minutes.
- """
+ """The owner is the long-lived gateway (alive by design): its settled admission, not its
+ death, proves a lost managed worker's attempt over. A bound run stays the crash sweep's."""
board.dead_attempt("t_ghost2") # admission=("terminal", "interrupted"), dead owner pid
board.dead_attempt("t_gateway", owner_pid=os.getpid(), owner_birth=None,
admission=("terminal", "interrupted"))
+ board.dead_attempt("t_bound", worker_bound=True, owner_pid=os.getpid(), owner_birth=None,
+ admission=("terminal", "interrupted"))
assert owner_reclaim_paused(board.conn, "t_gateway") is True # effects still uncertain
assert owner_attempt_provably_over(board.conn, "t_gateway") is True
+ assert owner_attempt_provably_over(board.conn, "t_bound") is False
assert kb.release_stale_claims(board.conn) == 2
row = board.state("t_gateway")
assert (row["status"], row["claim_lock"], row["worker_pid"]) == ("ready", None, None)
@@ -227,7 +224,7 @@ def test_a_settled_admission_is_proof_even_while_the_owner_gateway_is_alive(boar
assert "owner admission settled interrupted" in run["error"]
# Booked against the breaker like any other non-completing attempt (A3).
assert board.state("t_gateway")["consecutive_failures"] == 1
-
+ assert board.state("t_bound")["status"] == "running"
# --- the gate is decided under the same write lock as the release ---------------------------