Skip to content

The agent that grows with you

fix(kanban): only a handoff to a different profile lifts the active_pr guard

teknium1cdc4fc4c8f20
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index 6bb73d7c040..03d7b1f2719 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -1559,7 +1559,11 @@ def assign_task(conn: sqlite3.Connection, task_id: str, profile: Optional[str])
)
else:
conn.execute("UPDATE tasks SET assignee = ? WHERE id = ?", (profile, task_id))
- _append_event(conn, task_id, "assigned", {"assignee": profile})
+ # ``from`` lets the respawn guard tell a real handoff (dev→closer) from
+ # a no-op re-assign or an unassign, which must not lift ``active_pr``.
+ _append_event(
+ conn, task_id, "assigned", {"assignee": profile, "from": row["assignee"]},
+ )
# Observer fires AFTER commit so subscribers see durable state.
notify_task_updated(conn, task_id, ("assignee",))
return True
diff --git a/hermes_cli/kanban_db_dispatch.py b/hermes_cli/kanban_db_dispatch.py
index bad0f6326e1..ba561f56aff 100644
--- a/hermes_cli/kanban_db_dispatch.py
+++ b/hermes_cli/kanban_db_dispatch.py
@@ -1459,19 +1459,36 @@ def check_respawn_guard(
).fetchall():
if not (c["body"] and _RESPAWN_GUARD_PR_URL_RE.search(c["body"])):
continue
- handed_off = conn.execute(
+ events = conn.execute(
# Strictly after: a same-second tie stays guarded (fail closed).
- "SELECT 1 FROM task_events "
+ "SELECT kind, payload FROM task_events "
"WHERE task_id = ? AND created_at > ? "
- "AND kind IN ('assigned', 'changes_requested', 'review_reopened') "
- "LIMIT 1",
+ "AND kind IN ('assigned', 'changes_requested', 'review_reopened')",
(task_id, int(c["created_at"] or 0)),
- ).fetchone()
- return None if handed_off else "active_pr"
+ ).fetchall()
+ if any(_is_handoff_event(e["kind"], e["payload"]) for e in events):
+ return None
+ return "active_pr"
return None
+def _is_handoff_event(kind: str, payload: Optional[str]) -> bool:
+ """Only an ``assigned`` event that moves the card to a DIFFERENT profile is
+ a handoff. A no-op re-assign (dev→dev via CLI/dashboard/``reassign
+ --reclaim``), an unassign, or the dispatcher's own
+ ``kanban.default_assignee`` write would otherwise lift ``active_pr`` for
+ the very implementer that opened the PR. Events without ``from`` (written
+ before it was recorded) are not trusted as handoffs — fail closed."""
+ if kind != "assigned":
+ return True
+ data = _kb._json_or(payload, {})
+ if not isinstance(data, dict) or data.get("source") == "kanban.default_assignee":
+ return False
+ to = data.get("assignee")
+ return bool(to) and "from" in data and data["from"] != to
+
+
def _profile_exists_fn() -> Optional[Callable[[str], bool]]:
"""``hermes_cli.profiles.profile_exists``, or ``None`` when it cannot be
imported (local import avoids a cycle; callers fall back to trusting the
diff --git a/tests/hermes_cli/test_kanban_review_lifecycle.py b/tests/hermes_cli/test_kanban_review_lifecycle.py
index 76b262ae3ef..16f42dafeb2 100644
--- a/tests/hermes_cli/test_kanban_review_lifecycle.py
+++ b/tests/hermes_cli/test_kanban_review_lifecycle.py
@@ -535,6 +535,44 @@ def test_active_pr_guard_lifts_for_profile_handed_the_card_after_the_pr(
assert kbd.check_respawn_guard(conn, closer_id) == "active_pr"
+def test_active_pr_guard_holds_through_same_profile_reassign_and_unassign(
+ kanban_home: Path, monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Only a handoff to a DIFFERENT profile lifts ``active_pr``.
+
+ A no-op ``assign dev -> dev`` (CLI, dashboard PATCH, ``reassign --reclaim``)
+ and an unassign both record an ``assigned`` event but change no owner; if
+ they counted as handoffs the implementer would be re-spawned against its own
+ PR — the duplicate-work protection #111910 says must survive.
+ """
+ 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: {})
+ pr_comment = "Opened https://github.com/example/repo/pull/44 for review."
+ with kbc.connect() as conn:
+ tid = kb.create_task(conn, title="same assign", assignee="dev")
+ kb.add_comment(conn, tid, author="dev", body=pr_comment)
+ _backdate_comments(conn, tid)
+ assert kb.assign_task(conn, tid, "dev") is True
+ assert kbd.check_respawn_guard(conn, tid) == "active_pr"
+ assert kb.reassign_task(conn, tid, "dev", reclaim_first=True) is True
+ assert kbd.check_respawn_guard(conn, tid) == "active_pr"
+
+ assert kb.assign_task(conn, tid, None) is True
+ assert kbd.check_respawn_guard(conn, tid) == "active_pr"
+ # The dispatcher's own default_assignee write is not an operator handoff.
+ res = kbd.dispatch_once(conn, dry_run=False, default_assignee="dev")
+ assert tid in res.auto_assigned_default
+ assert dict(res.respawn_guarded).get(tid) == "active_pr"
+ assert tid not in [s[0] for s in res.spawned]
+
+ # A real handoff after all of that still lifts the guard.
+ assert kb.assign_task(conn, tid, "closer") is True
+ assert kbd.check_respawn_guard(conn, tid) is None
+
+
def test_active_pr_guard_lifts_for_implementer_after_changes_requested(
kanban_home: Path,
) -> None:
diff --git a/website/docs/user-guide/features/kanban.md b/website/docs/user-guide/features/kanban.md
index 940e225a546..4c258163326 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, 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.
+`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. Only a change to a *different* profile counts: re-assigning the same profile, unassigning, or the dispatcher's own `kanban.default_assignee` fill-in does not lift the guard, so the assignee that opened the PR is still not re-spawned against it after a crash, reclaim or no-op reassign, 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)