Skip to content

The agent that grows with you

fix(kanban): attribute synthesized review-handoff run to the implementer

Kevin Rajanff8f725c5ac6
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index 966f5f42dee..e19ec2013bd 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -1922,28 +1922,38 @@ def _current_run_id(conn: sqlite3.Connection, task_id: str) -> Optional[int]:
def _end_or_synthesize_run(
conn: sqlite3.Connection, task_id: str, *, outcome: str, status: str,
summary: Optional[str] = None, metadata: Optional[dict] = None, synthesize: bool,
+ profile: Optional[str] = None,
) -> Optional[int]:
""":func:`_end_run`; when no run was active and ``synthesize`` holds, record a
- zero-duration run instead so the handoff fields survive in attempt history."""
+ zero-duration run instead so the handoff fields survive in attempt history.
+ ``profile`` overrides the profile read off the task row for the synthesized
+ run — transitions that reassign the task (e.g. review handoff) pass the
+ acting profile captured before the rewrite."""
run_id = _end_run(conn, task_id, outcome=outcome, status=status, summary=summary, metadata=metadata)
if run_id is None and synthesize:
- run_id = _synthesize_ended_run(conn, task_id, outcome=outcome, summary=summary, metadata=metadata)
+ run_id = _synthesize_ended_run(conn, task_id, outcome=outcome, summary=summary, metadata=metadata, profile=profile)
return run_id
def _synthesize_ended_run(
conn: sqlite3.Connection, task_id: str, *, outcome: str, summary: Optional[str] = None,
error: Optional[str] = None, metadata: Optional[dict] = None,
+ profile: Optional[str] = None,
) -> int:
"""Zero-duration closed run for a terminal transition on a never-claimed
task, so the handoff fields aren't silently dropped (``_end_run`` is a
no-op then). ``started_at == ended_at`` keeps elapsed stats honest. Does
- NOT touch the tasks row."""
+ NOT touch the tasks row.
+
+ ``profile`` overrides the profile read off the task row: transitions that
+ reassign the task (e.g. review handoff) pass the acting profile captured
+ before the rewrite, so the run names the actor, not the new assignee."""
now = int(time.time())
trow = conn.execute(
"SELECT assignee, current_step_key FROM tasks WHERE id = ?", (task_id,),
).fetchone()
- profile = trow["assignee"] if trow else None
+ if profile is None:
+ profile = trow["assignee"] if trow else None
step_key = trow["current_step_key"] if trow else None
cur = conn.execute(
"""
@@ -3125,6 +3135,7 @@ def request_review(
run_id = _end_or_synthesize_run(
conn, task_id, outcome="review_requested", status="review",
summary=summary, metadata=metadata, synthesize=bool(summary or metadata),
+ profile=implementer,
)
payload: dict = {
"summary": _first_line(summary, 400) or None,
diff --git a/tests/hermes_cli/test_kanban_review_handoff_attribution.py b/tests/hermes_cli/test_kanban_review_handoff_attribution.py
new file mode 100644
index 00000000000..84a9c6a019b
--- /dev/null
+++ b/tests/hermes_cli/test_kanban_review_handoff_attribution.py
@@ -0,0 +1,90 @@
+"""Regression: synthesized review-handoff run attributed to the implementer (#111064).
+
+``request_review`` captures the acting profile (the implementer) and then
+rewrites ``tasks.assignee`` to the reviewer in the same UPDATE. The
+zero-duration run synthesized for a never-claimed card must name the
+implementer — the actor who performed the handoff — not the reviewer who
+received it.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from hermes_cli import kanban_db as kb
+from hermes_cli import kanban_db_connect as kbc
+
+
+def kanban_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("HERMES_PROFILE", "builder")
+ monkeypatch.delenv("HERMES_DELEGATED_CHILD_CONTEXT", raising=False)
+ # request_review rejects reviewers that are not installed profiles (#106163).
+ (home / "profiles" / "reviewer").mkdir(parents=True)
+ kb._INITIALIZED_PATHS.clear()
+ kb.init_db()
+ return home
+
+
+def test_synthesized_review_handoff_run_names_implementer_not_reviewer(
+ kanban_home: Path,
+) -> None:
+ """The review_requested handoff row belongs to the implementer.
+
+ Repro from #111064: an unclaimed card (assignee=builder) is handed off
+ via request_review(reviewer=reviewer). The task is reassigned, but the
+ synthesized zero-duration run must still record profile="builder".
+ """
+ with kbc.connect() as conn:
+ task_id = kb.create_task(conn, title="Unclaimed card", assignee="builder")
+ ok = kb.request_review(
+ conn, task_id, summary="ready for review", reviewer="reviewer"
+ )
+ assert ok is True
+
+ task = kb.get_task(conn, task_id)
+ assert task is not None
+ assert task.status == "review"
+ assert task.assignee == "reviewer"
+
+ handoff = kb.latest_run(conn, task_id)
+ assert handoff is not None
+ assert handoff.outcome == "review_requested"
+ assert handoff.profile == "builder"
+
+
+def test_synthesized_review_handoff_run_after_live_implementer_run(
+ kanban_home: Path,
+) -> None:
+ """Same attribution when the implementer had a live run first.
+
+ The implementer's own run keeps profile="builder"; the handoff row must
+ too — otherwise per-profile tallies mix the two roles.
+ """
+ with kbc.connect() as conn:
+ task_id = kb.create_task(conn, title="Claimed card", assignee="builder")
+ claimed = kb.claim_task(conn, task_id, claimer="builder:1")
+ assert claimed is not None
+
+ ok = kb.request_review(
+ conn,
+ task_id,
+ summary="done",
+ reviewer="reviewer",
+ expected_run_id=claimed.current_run_id,
+ )
+ assert ok is True
+
+ rows = conn.execute(
+ "SELECT profile, outcome FROM task_runs WHERE task_id = ? ORDER BY id",
+ (task_id,),
+ ).fetchall()
+ handoffs = [r for r in rows if r["outcome"] == "review_requested"]
+ assert len(handoffs) == 1
+ assert handoffs[0]["profile"] == "builder"