Skip to content

The agent that grows with you

fix(kanban): the protocol-violation streak never matches a worker's quoted output; docs match the release rule

kshitijk4poor0431192d69d4
diff --git a/gateway/session_kanban.py b/gateway/session_kanban.py
index 31ab6a1b617..711fb7a06a3 100644
--- a/gateway/session_kanban.py
+++ b/gateway/session_kanban.py
@@ -319,7 +319,7 @@ def record_worker_loss(policy, reason, *, pid=None, bound=False):
Never raises: the caller's own loss handling must not depend on the board write.
"""
try:
- context = json.loads(getattr(policy, 'kanban_json', None) or 'null')
+ context = json.loads(policy.kanban_json or 'null')
if not context:
return
from hermes_cli.kanban_db_connect import connect_closing
diff --git a/hermes_cli/kanban_db_dispatch.py b/hermes_cli/kanban_db_dispatch.py
index 5e5d8ff47ba..3a37b9ebbf1 100644
--- a/hermes_cli/kanban_db_dispatch.py
+++ b/hermes_cli/kanban_db_dispatch.py
@@ -960,15 +960,16 @@ def _protocol_violation_streak(conn: sqlite3.Connection, task_id: str) -> int:
outcome = row["outcome"] or ""
if outcome == "rate_limited":
continue
- metadata = _kb._json_dict(row["metadata"]) # a lost worker's run quotes its output: no text match
- if outcome == "crashed" and not metadata.get("worker_lost") and (
- metadata.get("protocol_violation") or "protocol violation" in (row["error"] or "")):
+ head = (row["error"] or "").partition(WORKER_OUTPUT_SEP)[0] # never the worker's own words
+ if outcome == "crashed" and (
+ _kb._json_dict(row["metadata"]).get("protocol_violation") or "protocol violation" in head):
streak += 1
continue
break
return streak
+WORKER_OUTPUT_SEP = " Worker's last output: " # dead-worker error text: our words, then the worker's
_PROTOCOL_VIOLATION_ERROR = (
# Worker subprocess returned 0 but its task is still ``running`` in the DB — it exited without calling
# ``kanban_complete`` / ``kanban_block`` / ``kanban_request_review``. Overwhelmingly the work itself succeeded and only the
@@ -1070,7 +1071,7 @@ def _classify_dead_worker(
if task_id and not dead.rate_limited:
worker_output = _worker_final_output(task_id, board=board)
if worker_output:
- dead.error_text += f" Worker's last output: {worker_output!r}"
+ dead.error_text += f"{WORKER_OUTPUT_SEP}{worker_output!r}"
dead.event_payload["worker_output"] = worker_output
return dead
diff --git a/hermes_cli/kanban_owner_recovery.py b/hermes_cli/kanban_owner_recovery.py
index 59e30a604ad..5c12945d7c1 100644
--- a/hermes_cli/kanban_owner_recovery.py
+++ b/hermes_cli/kanban_owner_recovery.py
@@ -50,7 +50,8 @@ def owner_reclaim_paused(conn, task_id):
try:
row = _admission_row(receipt)
if row is not None and row[0] == 'terminal':
- # Discard acknowledges uncertainty, not permission to repeat effects.
+ # Interrupted still pauses the generic reclaim paths; owner_attempt_provably_over decides
+ # when nothing can still be running it.
return row[1] == 'interrupted'
if row is None or row[0] in {'queued', 'unknown'}:
return True
@@ -131,12 +132,11 @@ def owner_attempt_provably_over(conn, task_id) -> bool:
"""True when an unresolved owner receipt cannot still be executing this attempt.
``owner_reclaim_paused`` is about effects: an admission that may still be in flight is
- never repeated. It must not also mean the run keeps its claim forever. The attempts it
- pauses in practice are ones whose owner was killed (restart, OOM), and such a run may
- never have written a ``worker_bound`` event at all, so ``bound_interpreter_gone`` cannot
- speak for it — 76 ``owner_admitted`` against 74 ``worker_bound`` rows on one board means
- runs in exactly that shape. This asks the question that matters — can anything still be
- executing this attempt? — from evidence the board already keeps:
+ never repeated. It must not also mean the run keeps its claim forever. A paused run whose
+ owner died, or whose owner settled the admission after losing its worker, may never have
+ written a ``worker_bound`` event, so ``bound_interpreter_gone`` cannot speak for it. This
+ asks the question that matters — can anything still be executing this attempt? — from
+ evidence the board already keeps:
* 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
@@ -180,7 +180,7 @@ 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
- # No pid was ever recorded: only a lapsed claim lets the owner's verdict (or its death) speak.
+ # A lapsed claim: a settled admission is proof; an unsettled one needs recorded pids and a dead owner.
return settled or (bool(pids) and _owner_process_gone(receipt))
@@ -237,7 +237,8 @@ def apply_attempt_note(conn, task_id, pid, dead) -> None:
dead.error_text = (f"pid {pid} exited cleanly after its managed worker was lost."
+ dead.error_text.removeprefix(_PROTOCOL_VIOLATION_ERROR))
# Before the worker's own output: the stored error is capped, and the output may be long.
- head, sep, output = dead.error_text.partition(" Worker's last output: ")
+ from hermes_cli.kanban_db_dispatch import WORKER_OUTPUT_SEP
+ head, sep, output = dead.error_text.partition(WORKER_OUTPUT_SEP)
dead.error_text = f"{head} [{note}]{sep}{output}"
diff --git a/tests/hermes_cli/test_kanban_managed_worker_loss.py b/tests/hermes_cli/test_kanban_managed_worker_loss.py
index de107e37ae9..b0ff931c522 100644
--- a/tests/hermes_cli/test_kanban_managed_worker_loss.py
+++ b/tests/hermes_cli/test_kanban_managed_worker_loss.py
@@ -193,6 +193,20 @@ def test_a_lost_worker_that_exited_cleanly_still_charges_the_breaker(board):
assert kbd._protocol_violation_streak(board.conn, "t_clean") == 0
+def test_a_crash_whose_output_quotes_protocol_violation_is_not_one(board):
+ """The streak's text match reads our own error head, never the worker's quoted output."""
+ run_id = board.running("t_quote", claim_expires_in=900, heartbeat_age=30, owner=False)
+ log = kb.worker_log_path("t_quote")
+ log.parent.mkdir(parents=True, exist_ok=True)
+ from hermes_cli.quiet_single_query import KANBAN_WORKER_EXIT_TRAILER
+ log.write_text(f"Retrying after the earlier protocol violation.\n{KANBAN_WORKER_EXIT_TRAILER}1\n",
+ encoding="utf-8")
+ board.tick(failure_limit=5)
+ run = board.run(run_id)
+ assert run["outcome"] == "crashed" and "protocol violation" in run["error"]
+ assert kbd._protocol_violation_streak(board.conn, "t_quote") == 0
+
+
# --- A2: an elapsed claim lock is never "still running" after one tick ---------------------
@pytest.mark.parametrize("shape, error", [
diff --git a/website/docs/user-guide/features/kanban.md b/website/docs/user-guide/features/kanban.md
index dacb7c60466..0a5555bdfdc 100644
--- a/website/docs/user-guide/features/kanban.md
+++ b/website/docs/user-guide/features/kanban.md
@@ -1354,11 +1354,16 @@ ledger also pauses reclaim. Once that bound interpreter is itself dead, nothing
still be running the attempt, so the crash sweep reclaims it like any dead worker:
the run closes as crashed and the card returns to `ready` (or `review`) under the
normal crash budget, which can re-execute effects the lost attempt already had.
-Resolve an unknown admission explicitly using `prompt.resolve_unknown` with its
-admission ID and execution generation. This acknowledges uncertainty; it does
-**not** authorize another Kanban attempt. Inspect the effects, then explicitly
-complete or block the card; unblock a blocked card only when a new attempt is
-intended. Confirmed timeouts and provider rate-limit results retain their ordinary
+A run that never bound an interpreter is released the same way once its owner has
+settled the admission (a lost card attempt's admission settles `interrupted`):
+when every worker pid recorded for the run is dead, or, with none recorded, once
+its claim has expired, the run closes as crashed (reclaimed if its claim had already
+lapsed) and the card returns to `ready`
+under the same crash budget. Resolving an unknown admission explicitly
+(`prompt.resolve_unknown` with its admission ID and execution generation) settles
+it the same way, so it can lead to another attempt. To avoid that, inspect the
+effects and complete or block the card first; unblock a blocked card only when a
+new attempt is intended. Confirmed timeouts and provider rate-limit results retain their ordinary
retry/failure-accounting behavior.
A task is a logical unit of work; a **run** is one attempt to execute it. When the dispatcher claims a ready task it creates a row in `task_runs` and points `tasks.current_run_id` at it. When that attempt ends — completed, blocked, crashed, timed out, spawn-failed, reclaimed — the run row closes with an `outcome` and the task's pointer clears. A task that's been attempted three times has three `task_runs` rows.