Skip to content

The agent that grows with you

fix(kanban): apply max_in_progress constraint before both ready and review queues

Dusk1e1f8c057832e4
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index ba2ad6a6d87..1fe97f76b8d 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -9778,13 +9778,33 @@ def _dispatch_once_locked(
# they sit in status='running' until the worker calls
# kanban_complete/kanban_block (or the dispatcher TTL-reclaims them).
running_count = 0
- if max_spawn is not None:
+ spawn_budget: Optional[int] = None
+ if max_spawn is not None or max_in_progress is not None:
running_count = int(
conn.execute(
"SELECT COUNT(*) FROM tasks WHERE status = 'running'"
).fetchone()[0]
)
+ # Convert any concurrency caps into a shared additional-spawns budget
+ # for this tick. Both ready and review loops consume from the same
+ # budget so the total number of new workers stays bounded.
+ if max_spawn is not None:
+ if running_count >= max_spawn:
+ return result
+ spawn_budget = max_spawn - running_count
+
+ # Honour kanban.max_in_progress across both ready and review queues: if
+ # the board already has enough running tasks, skip this tick entirely.
+ # When there is room left, intersect the remaining in-progress budget
+ # with any explicit max_spawn cap above.
+ if max_in_progress is not None:
+ if running_count >= max_in_progress:
+ return result
+ remaining = max_in_progress - running_count
+ if spawn_budget is None or spawn_budget > remaining:
+ spawn_budget = remaining
+
ready_rows = conn.execute(
"SELECT id, assignee FROM tasks "
"WHERE status = 'ready' AND claim_lock IS NULL "
@@ -9828,7 +9848,7 @@ def _dispatch_once_locked(
# there, with the existing diagnostic.
_default_assignee_resolved = True
for row in ready_rows:
- if max_spawn is not None and running_count + spawned >= max_spawn:
+ if spawn_budget is not None and spawned >= spawn_budget:
break
row_assignee = row["assignee"]
if not row_assignee:
@@ -10033,7 +10053,7 @@ def _dispatch_once_locked(
"ORDER BY priority DESC, created_at ASC"
).fetchall()
for row in review_rows:
- if max_spawn is not None and running_count + spawned >= max_spawn:
+ if spawn_budget is not None and spawned >= spawn_budget:
break
if not row["assignee"]:
result.skipped_unassigned.append(row["id"])
diff --git a/tests/hermes_cli/test_kanban_db.py b/tests/hermes_cli/test_kanban_db.py
index ce254bf9f5d..06f2aa9976e 100644
--- a/tests/hermes_cli/test_kanban_db.py
+++ b/tests/hermes_cli/test_kanban_db.py
@@ -1246,6 +1246,29 @@ def _make_task(**overrides) -> "kb.Task":
# ---------------------------------------------------------------------------
+def test_dispatch_max_in_progress_blocks_review_when_at_limit(
+ kanban_home, all_assignees_spawnable,
+):
+ """Review-only backlog must still respect max_in_progress."""
+ spawns = []
+
+ def fake_spawn(task, workspace, board=None):
+ spawns.append(task.id)
+ return 42
+
+ with kb.connect() as conn:
+ running = kb.create_task(conn, title="running", assignee="alice")
+ kb.claim_task(conn, running)
+ review = kb.create_task(conn, title="review", assignee="bob")
+ _set_task_status(conn, review, "review")
+ res = kb.dispatch_once(conn, spawn_fn=fake_spawn, max_in_progress=1)
+ review_task = kb.get_task(conn, review)
+
+ assert not res.spawned
+ assert not spawns
+ assert review_task is not None
+ assert review_task.status == "review"
+
# Review column dispatch
# ---------------------------------------------------------------------------