Skip to content

The agent that grows with you

fix(kanban): active_pr respawn guard no longer holds a card handed to a closer or back to its implementer

teknium12e805005ea5f
diff --git a/hermes_cli/kanban_db_dispatch.py b/hermes_cli/kanban_db_dispatch.py
index f8e32c1c614..bad0f6326e1 100644
--- a/hermes_cli/kanban_db_dispatch.py
+++ b/hermes_cli/kanban_db_dispatch.py
@@ -1375,9 +1375,11 @@ def check_respawn_guard(
(quota/auth pattern; the breaker still trips eventually), then for the
ready lane only ``"recent_success"`` (completed run within the window, unless
a re-queue event arrived after it — a deliberate re-run) and ``"active_pr"``
- (PR URL in a recent comment; re-spawning risks a duplicate PR). The review
- lane skips the last two: they are the *inputs* to a review handoff. Stale /
- dead claim locks are NOT a guard reason — the reclaim passes own those.
+ (PR URL in a recent comment; re-spawning risks a duplicate PR — unless a
+ handoff event followed the comment: the named profile must work on that
+ PR). The review lane skips the last two: they are the *inputs* to a review
+ handoff. Stale / dead claim locks are NOT a guard reason — the reclaim
+ passes own those.
"""
row = conn.execute(
"SELECT last_failure_error FROM tasks WHERE id = ?",
@@ -1444,13 +1446,28 @@ def check_respawn_guard(
return "recent_success"
# 4. GitHub PR URL in a recent comment — prior worker already opened a PR.
+ # Exception: a handoff AFTER the newest PR comment (operator reassign,
+ # reviewer changes_requested, review reopen) names the profile that must
+ # now work on THAT PR — a closer or the implementer finishing it, not a
+ # duplicate implementation (#111910). A crash/reclaim is not a handoff,
+ # so the worker that opened the PR is still not re-spawned against it.
pr_cutoff = now - _RESPAWN_GUARD_PR_WINDOW
for c in conn.execute(
- "SELECT body FROM task_comments WHERE task_id = ? AND created_at >= ?",
+ "SELECT body, created_at FROM task_comments "
+ "WHERE task_id = ? AND created_at >= ? ORDER BY created_at DESC",
(task_id, pr_cutoff),
).fetchall():
- if c["body"] and _RESPAWN_GUARD_PR_URL_RE.search(c["body"]):
- return "active_pr"
+ if not (c["body"] and _RESPAWN_GUARD_PR_URL_RE.search(c["body"])):
+ continue
+ handed_off = conn.execute(
+ # Strictly after: a same-second tie stays guarded (fail closed).
+ "SELECT 1 FROM task_events "
+ "WHERE task_id = ? AND created_at > ? "
+ "AND kind IN ('assigned', 'changes_requested', 'review_reopened') "
+ "LIMIT 1",
+ (task_id, int(c["created_at"] or 0)),
+ ).fetchone()
+ return None if handed_off else "active_pr"
return None
diff --git a/tests/hermes_cli/test_kanban_review_lifecycle.py b/tests/hermes_cli/test_kanban_review_lifecycle.py
index db41074b753..76b262ae3ef 100644
--- a/tests/hermes_cli/test_kanban_review_lifecycle.py
+++ b/tests/hermes_cli/test_kanban_review_lifecycle.py
@@ -483,6 +483,90 @@ def test_active_pr_guard_skipped_for_review_lane_but_defers_ready_lane(
) == "rate_limit_cooldown"
+def _backdate_comments(conn, tid, seconds=60):
+ """Second-granularity timestamps: make the PR comment older than the
+ handoff that follows it in the same test."""
+ with kb.write_txn(conn):
+ conn.execute(
+ "UPDATE task_comments SET created_at = created_at - ? WHERE task_id = ?",
+ (seconds, tid),
+ )
+
+
+def test_active_pr_guard_lifts_for_profile_handed_the_card_after_the_pr(
+ kanban_home: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """A ready card whose PR is open spawns the profile it was handed to.
+
+ #111910: ``active_pr`` exists to stop the implementer from opening a
+ duplicate PR; it must not stop the closer/recovery profile an operator
+ assigned AFTER the PR comment — that handoff is why the PR must be worked.
+ The un-reassigned implementer stays guarded; a newer PR comment posted
+ after the handoff (the closer's own run) guards again.
+ """
+ import hermes_cli.config as cfgmod
+ import hermes_cli.profiles as profmod
+
+ monkeypatch.setattr(profmod, "profile_exists", lambda name: True)
+ monkeypatch.setattr(
+ cfgmod, "load_config", lambda *a, **k: {"kanban": {"review_dispatch": True}},
+ )
+ pr_comment = "Opened https://github.com/example/repo/pull/44 for review."
+
+ with kbc.connect() as conn:
+ dev_id = kb.create_task(conn, title="dev own pr", assignee="dev")
+ kb.add_comment(conn, dev_id, author="dev", body=pr_comment)
+ closer_id = kb.create_task(conn, title="closer recovery", assignee="dev")
+ kb.add_comment(conn, closer_id, author="dev", body=pr_comment)
+ _backdate_comments(conn, closer_id)
+ assert kb.assign_task(conn, closer_id, "closer") is True
+
+ assert kbd.check_respawn_guard(conn, dev_id) == "active_pr"
+ assert kbd.check_respawn_guard(conn, closer_id) is None
+
+ res = kbd.dispatch_once(conn, dry_run=True)
+ assert closer_id in [s[0] for s in res.spawned]
+ assert dict(res.respawn_guarded).get(dev_id) == "active_pr"
+
+ kb.add_comment(
+ conn, closer_id, author="closer",
+ body="Pushed to https://github.com/example/repo/pull/44",
+ )
+ assert kbd.check_respawn_guard(conn, closer_id) == "active_pr"
+
+
+def test_active_pr_guard_lifts_for_implementer_after_changes_requested(
+ kanban_home: Path,
+) -> None:
+ """Reviewer CHANGES_REQUESTED routes the card back to ``ready`` for the
+ implementer to fix the SAME PR; ``active_pr`` must not hold it (#111910).
+ ``recent_success`` is untouched by the handoff exemption."""
+ pr_comment = "Opened https://github.com/example/repo/pull/44 for review."
+ with kbc.connect() as conn:
+ tid = kb.create_task(conn, title="changes requested", assignee="dev")
+ claimed = kb.claim_task(conn, tid)
+ kb.add_comment(conn, tid, author="dev", body=pr_comment)
+ _backdate_comments(conn, tid)
+ assert kb.request_review(
+ conn, tid, summary="PR ready", reviewer="reviewer",
+ expected_run_id=claimed.current_run_id,
+ )
+ rclaim = kb.claim_review_task(conn, tid)
+ ok, implementer = kb.request_changes(
+ conn, tid, reason="fix tests", expected_run_id=rclaim.current_run_id,
+ )
+ assert (ok, implementer) == (True, "dev")
+ assert kb.get_task(conn, tid).status == "ready"
+ assert kbd.check_respawn_guard(conn, tid) is None
+
+ done_id = kb.create_task(conn, title="recent success", assignee="dev")
+ kb.claim_task(conn, done_id)
+ assert kb.complete_task(conn, done_id, summary="done") is True
+ with kb.write_txn(conn):
+ conn.execute("UPDATE tasks SET status = 'ready' WHERE id = ?", (done_id,))
+ assert kbd.check_respawn_guard(conn, done_id) == "recent_success"
+
+
def test_dispatch_json_exposes_suppression_reasons(
kanban_home: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
diff --git a/website/docs/user-guide/features/kanban.md b/website/docs/user-guide/features/kanban.md
index 8037c3acb04..940e225a546 100644
--- a/website/docs/user-guide/features/kanban.md
+++ b/website/docs/user-guide/features/kanban.md
@@ -938,7 +938,7 @@ The dispatcher refuses to re-spawn a ready task when it hit a quota/auth/429 err
To see why a ready card is not spawning, run `hermes kanban dispatch --dry-run` — the output lists `Guarded (<reason>): <task id>` per held card (and `respawn_guarded`, `rate_limited`, `skipped_locked`, `memory_pressure` with `--json`). The gateway's and the standalone daemon's "dispatcher stuck" warning also names what the last tick held back, e.g. `Last tick held back: active_pr=1`.
-`recent_success` and `active_pr` hold the **ready** lane only — they are the inputs to a review handoff, not signals against one. To have a reviewer, closer or other recovery profile pick up a card whose PR is already open, move it to the review lane with `hermes kanban request-review <id>` (accepted from `ready` as well as `running`); the review-lane spawn is not subject to either guard, and the Dev assignee is not re-spawned against its own PR. A deliberate re-queue after a success (drag `done→ready`, `unblock`, re-promotion) also lifts `recent_success`, so a manual re-run is never silently held for the whole window.
+`recent_success` and `active_pr` hold the **ready** lane only — they are the inputs to a review handoff, not signals against one. To have a reviewer, closer or other recovery profile pick up a card whose PR is already open, either move it to the review lane with `hermes kanban request-review <id>` (accepted from `ready` as well as `running`; the review-lane spawn is not subject to either guard) or hand the ready card to that profile with `hermes kanban assign <id> <profile>`: a handoff recorded *after* the PR comment — an operator reassign, a reviewer's changes-requested verdict, or a review reopen — lifts `active_pr` for the profile now named on the card, because that PR is exactly what it must work on. The assignee that opened the PR is still not re-spawned against it after a crash or reclaim, and a newer PR comment posted after the handoff guards again. A deliberate re-queue after a success (drag `done→ready`, `unblock`, re-promotion) also lifts `recent_success`, so a manual re-run is never silently held for the whole window.
### Drag-to-delete and bulk delete (dashboard)