Skip to content

The agent that grows with you

fix(kanban): classify only an untyped, run-free breaker block; restore sort order

teknium19850010ff5c1
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index e56f607832b..21e4017ce8d 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -1507,6 +1507,7 @@ VALID_SORT_ORDERS: dict[str, str] = {
"assignee": "assignee ASC, created_at ASC",
"title": "title ASC, id ASC",
"updated": "started_at DESC NULLS LAST, created_at DESC",
+ "completed-desc": "completed_at DESC NULLS LAST, id DESC",
}
@@ -3127,10 +3128,12 @@ def block_task(
toward the loop breaker so a forever-flaky task escalates. True on any
transition.
- When the card is already ``blocked`` (e.g. the circuit breaker parked it
- untyped) and *kind* is supplied, the call classifies the existing block
- instead of refusing it: ``block_kind`` is set and a ``block_classified``
- event is appended without touching status or recurrence accounting.
+ An already-``blocked`` card that the failure breaker parked UNTYPED
+ (``block_kind IS NULL``, no live run) is classified in place when *kind*
+ is supplied: ``block_kind``/``block_recurrences`` are set and a ``blocked``
+ audit event is appended, while status, failure evidence and the terminal
+ runs stay exactly as the breaker left them. A typed block, a card with a
+ live run, or a kind-less call on a blocked card are still refused.
"""
if kind is not None and kind not in VALID_BLOCK_KINDS:
raise ValueError(f"block kind must be one of {sorted(VALID_BLOCK_KINDS)} or None")
@@ -3140,25 +3143,24 @@ def block_task(
).fetchone()
if cur_row is None:
return False
- # --- classify an already-blocked card --------------------------------
- # The circuit breaker (enforce_max_runtime) parks cards as blocked with
- # block_kind IS NULL. A supervisor that reacts to that needs to attach
- # a classification (e.g. "needs_input") but the WHERE clause below only
- # matches running/ready, so the call silently fails. Detect this case
- # and update block_kind in-place without touching status or recurrences.
- if cur_row["status"] == "blocked" and kind is not None:
- prev = _row_get(cur_row, "block_kind")
- if prev == kind:
- # Already classified with the requested kind — idempotent.
- return True
- conn.execute(
- "UPDATE tasks SET block_kind = ? WHERE id = ? AND status = 'blocked'",
+ # The breaker (``_record_task_failure``) parks cards ``blocked`` with no
+ # ``block_kind`` and no ``blocked`` event -- the policy is the
+ # supervisor's, not the kernel's -- but the transition guard below only
+ # matches running/ready, so that policy could never be attached later
+ # (#117363). Classify in place; never re-type or flap status.
+ if cur_row["status"] == "blocked":
+ if kind is None or _row_get(cur_row, "block_kind") is not None:
+ return False
+ classified = conn.execute(
+ "UPDATE tasks SET block_kind = ?, block_recurrences = 1 "
+ "WHERE id = ? AND status = 'blocked' AND block_kind IS NULL "
+ "AND current_run_id IS NULL",
(kind, task_id),
- )
- _append_event(conn, task_id, "block_classified", {
- "kind": kind,
- "previous_kind": prev,
- "reason": reason,
+ ).rowcount
+ if classified != 1:
+ return False
+ _append_event(conn, task_id, "blocked", {
+ "kind": kind, "reason": reason, "classified_in_place": True,
})
return True
source_status = _retry_status_for_run(conn, task_id) if cur_row["status"] == "running" else "ready"
@@ -3206,6 +3208,7 @@ def block_task(
_fire_task_hook("kanban_task_blocked", blocked_task, task_id, run_id, reason=reason)
return True
+
def _route_block(
kind: Optional[str], reason: Optional[str], source_status: str, *,
prev_kind: Optional[str], prev_recurrences: int,
diff --git a/tests/hermes_cli/test_kanban_block_classify.py b/tests/hermes_cli/test_kanban_block_classify.py
index d16c5d94bd2..dd5fb543161 100644
--- a/tests/hermes_cli/test_kanban_block_classify.py
+++ b/tests/hermes_cli/test_kanban_block_classify.py
@@ -1,16 +1,13 @@
-"""Tests for block_task() classification of already-blocked cards.
-
-Reproduces issue #117363: the circuit breaker parks cards untyped, and
-block_task() refuses to classify them because the WHERE clause only
-matches running/ready.
-"""
+"""block_task() classifies an untyped breaker block in place (#117363)."""
import os
+import time
from pathlib import Path
import pytest
from hermes_cli import kanban_db as kb
from hermes_cli import kanban_db_connect as kbc
+from hermes_cli import kanban_db_dispatch as kbd
@pytest.fixture
@@ -23,84 +20,56 @@ def kanban_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
return home
-def test_classify_already_blocked_untyped_card(kanban_home):
- """A card parked blocked with block_kind=NULL should accept a kind."""
- with kbc.connect() as conn:
- task_id = kb.create_task(conn, title="breaker-parked card")
- # Simulate the circuit breaker: set status=blocked without block_kind.
+def _time_out_once(conn, tid: str) -> None:
+ kb.claim_task(conn, tid)
+ kbd._set_worker_pid(conn, tid, os.getpid())
+ started = int(time.time()) - 30
+ with kb.write_txn(conn):
+ conn.execute("UPDATE tasks SET started_at = ? WHERE id = ?", (started, tid))
conn.execute(
- "UPDATE tasks SET status = 'blocked', block_kind = NULL WHERE id = ?",
- (task_id,),
+ "UPDATE task_runs SET started_at = ? "
+ "WHERE id = (SELECT current_run_id FROM tasks WHERE id = ?)",
+ (started, tid),
)
- assert kb.get_task(conn, task_id).block_kind is None
-
- result = kb.block_task(conn, task_id, kind="needs_input", reason="supervisor classifying breaker block")
- assert result is True
-
- task = kb.get_task(conn, task_id)
- assert task.status == "blocked"
- assert task.block_kind == "needs_input"
-
- events = [e for e in kb.list_events(conn, task_id) if e.kind == "block_classified"]
- assert len(events) == 1
- assert events[-1].payload["kind"] == "needs_input"
- assert events[-1].payload["previous_kind"] is None
-
-
-def test_classify_already_blocked_typed_card(kanban_home):
- """Re-classifying a typed blocked card should update block_kind."""
- with kbc.connect() as conn:
- task_id = kb.create_task(conn, title="typed block")
- kb.block_task(conn, task_id, kind="transient", reason="flaky")
-
- task = kb.get_task(conn, task_id)
- assert task.status == "blocked"
- assert task.block_kind == "transient"
-
- result = kb.block_task(conn, task_id, kind="needs_input", reason="escalated")
- assert result is True
-
- task = kb.get_task(conn, task_id)
- assert task.block_kind == "needs_input"
+ assert tid in kbd.enforce_max_runtime(conn, signal_fn=lambda pid, sig: None)
- events = [e for e in kb.list_events(conn, task_id) if e.kind == "block_classified"]
- assert len(events) == 1
- assert events[-1].payload["previous_kind"] == "transient"
-
-def test_classify_already_blocked_same_kind_is_idempotent(kanban_home):
- """Classifying with the same kind should be a no-op (idempotent)."""
+def test_typed_block_classifies_untyped_breaker_block(kanban_home, monkeypatch):
+ """Two timeouts trip the breaker untyped; the supervisor's typed block then
+ attaches the kind without a status flap, a synthetic run, or lost evidence."""
+ monkeypatch.setattr(kb, "_pid_alive", lambda pid: False)
with kbc.connect() as conn:
- task_id = kb.create_task(conn, title="already classified")
- kb.block_task(conn, task_id, kind="needs_input", reason="initial")
-
- result = kb.block_task(conn, task_id, kind="needs_input", reason="duplicate")
- assert result is True
-
- events = [e for e in kb.list_events(conn, task_id) if e.kind == "block_classified"]
- assert len(events) == 0, "idempotent classify must not emit an event"
-
-
-def test_block_task_still_blocks_running_card(kanban_home):
- """Normal blocking of a running card should still work."""
+ tid = kb.create_task(conn, title="ceiling", assignee="worker", max_runtime_seconds=1)
+ _time_out_once(conn, tid)
+ assert kb.get_task(conn, tid).status == "ready"
+ _time_out_once(conn, tid)
+ parked = kb.get_task(conn, tid)
+ assert (parked.status, parked.block_kind, parked.block_recurrences) == ("blocked", None, 0)
+
+ assert kb.block_task(conn, tid, reason="needs a human decision", kind="needs_input") is True
+
+ after = kb.get_task(conn, tid)
+ assert (after.status, after.block_kind, after.block_recurrences) == ("blocked", "needs_input", 1)
+ assert after.consecutive_failures == parked.consecutive_failures
+ assert after.last_failure_error == parked.last_failure_error
+ assert after.current_run_id is None
+ kinds = [e.kind for e in kb.list_events(conn, tid)]
+ assert (kinds.count("blocked"), kinds.count("gave_up"), kinds.count("timed_out")) == (1, 1, 2)
+ assert len(kb.list_runs(conn, tid)) == 2
+
+
+def test_typed_block_still_refuses_typed_or_live_blocked_cards(kanban_home):
with kbc.connect() as conn:
- task_id = kb.create_task(conn, title="normal card")
- kb.claim_task(conn, task_id, worker_pid=12345)
-
- result = kb.block_task(conn, task_id, kind="needs_input", reason="waiting for input")
- assert result is True
-
- task = kb.get_task(conn, task_id)
- assert task.status == "blocked"
- assert task.block_kind == "needs_input"
-
-
-def test_classify_blocked_without_kind_returns_false(kanban_home):
- """block_task() on an already-blocked card without kind should return False."""
- with kbc.connect() as conn:
- task_id = kb.create_task(conn, title="blocked card")
- kb.block_task(conn, task_id, kind="transient", reason="flaky")
-
- # Without kind, this should fail (no-op on already blocked)
- result = kb.block_task(conn, task_id, reason="no kind given")
- assert result is False
+ typed = kb.create_task(conn, title="typed once", assignee="worker")
+ kb.claim_task(conn, typed)
+ assert kb.block_task(conn, typed, reason="decision", kind="needs_input") is True
+ assert kb.block_task(conn, typed, reason="again", kind="capability") is False
+ assert kb.get_task(conn, typed).block_kind == "needs_input"
+
+ live = kb.create_task(conn, title="live run", assignee="worker")
+ kb.claim_task(conn, live)
+ with kb.write_txn(conn):
+ conn.execute("UPDATE tasks SET status = 'blocked' WHERE id = ?", (live,))
+ assert kb.block_task(conn, live, reason="race", kind="needs_input") is False
+ assert kb.block_task(conn, live, reason="no kind") is False
+ assert kb.get_task(conn, live).block_kind is None