Agents can hand work to agents: dispatch_agent

A schedule is a time trigger, and only the first step of a pipeline is really
waiting on time — every later step waits on the previous step's result. Modeling
"watch a source -> write a spec -> implement it" as three schedules made each fire
blind: on a quiet day the coding agent still spawned, paid a full model run to find
there was nothing to do, and left an empty run in Activity.

So an agent can start the next step itself. `dispatch_agent` (a tool on the bundled
MCP server, and on the pi bridge through the same --call seam) enqueues an ordinary
spawn command in the agent's own project, tagged requested_by=agent:<id> — so a
handoff is visible in Activity with no new surface to build.

- Project-scoped by construction: project_id is read from the spawn environment and
  never from the tool arguments.
- Bounded rather than gated: MAX_DISPATCH_PER_RUN counts the command rows the agent
  already wrote; MAX_DISPATCH_DEPTH rides in the spawn payload and is recovered by
  spawn._dispatch_depth, so a chain keeps its place across a resume and a cycle
  terminates instead of fanning out.
- New scout and planner roles, with built-in skills (handler-scout, handler-planner,
  handler-dispatch) carrying the judgment code can't: dedupe against a memory-note
  watermark, treat "nothing new" as a complete run, and write a task the receiving
  cold-start agent can act on.
- A scout ending on a clean tree skips the test gate and records the new
  tests_status='skipped' (migration 0017, additive CHECK widening). The gate promises
  `done` means tests passed for the work that shipped; nothing shipped.

Rejected a `condition` field on schedules: "is this paper new and does it matter
here?" is a semantic judgment, so it belongs to a model, not a scheduler column. The
scout is the condition; dispatch is how it reports true — one mechanism that covers
future pipelines too.

426 tests (14 new for dispatch, 3 for the gate exemption).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HcbDevyMcJWE6qPA56C7mZ
This commit is contained in:
Claude
2026-08-19 22:46:06 +00:00
parent 52167db085
commit 8541b4b7c0
23 changed files with 945 additions and 33 deletions
+45 -3
View File
@@ -2,16 +2,18 @@
from __future__ import annotations
import pytest
from handler.control import gitops, mise
from handler.db import repository as repo
from handler.hooks import checkpoint, verify
from handler.hooks.context import HookInput, Identity
def _seed(conn, mise_init=False):
def _seed(conn, mise_init=False, role=None):
repo.create_project(conn, "p", "/tmp/p")
a = repo.create_agent(conn, "p", "a", "/tmp/p/a")
return Identity(a["id"], "p", "a", "/tmp/p/a", mise_init=mise_init)
a = repo.create_agent(conn, "p", "a", "/tmp/p/a", role=role)
return Identity(a["id"], "p", "a", "/tmp/p/a", mise_init=mise_init, role=role)
def _fake_mise_state(monkeypatch, *, has_test, clean, ahead):
@@ -118,6 +120,46 @@ def test_stop_allows_done_when_working_dir_is_not_a_repo(conn, monkeypatch):
assert repo.get_checkmark(conn, ident.agent_id)["status"] == "done"
def test_stop_skips_tests_for_a_scout_that_shipped_nothing(conn, monkeypatch):
"""A watch run that found nothing has no work to verify — and most runs are that."""
ident = _seed(conn, role="scout")
monkeypatch.setattr(
verify, "run_test", lambda cwd: pytest.fail("the suite must not run here")
)
_fake_git_state(monkeypatch, clean=True)
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
assert result == {}
cm = repo.get_checkmark(conn, ident.agent_id)
assert cm["tests_status"] == "skipped"
assert cm["status"] == "done"
def test_stop_still_gates_a_scout_that_changed_files(conn, monkeypatch):
"""The exemption is clean-tree-only: a scout that edited something is gated."""
ident = _seed(conn, role="scout")
monkeypatch.setattr(verify, "run_test", lambda cwd: (False, "1 failed"))
_fake_git_state(monkeypatch, clean=False)
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
assert result["decision"] == "block"
assert "test suite is failing" in result["reason"]
assert repo.get_checkmark(conn, ident.agent_id)["tests_status"] == "fail"
def test_stop_gates_other_roles_on_a_clean_tree(conn, monkeypatch):
"""Only scouts are exempt — a junior with nothing to commit still runs the suite."""
ident = _seed(conn, role="junior")
ran = []
monkeypatch.setattr(verify, "run_test", lambda cwd: (ran.append(cwd), (True, "ok"))[1])
_fake_git_state(monkeypatch, clean=True)
assert checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop")) == {}
assert ran, "the suite must run for a non-scout role"
assert repo.get_checkmark(conn, ident.agent_id)["tests_status"] == "pass"
def test_stop_captures_final_message_as_checkpoint(conn, monkeypatch, tmp_path):
ident = _seed(conn)
monkeypatch.setattr(verify, "run_test", lambda cwd: (True, "ok"))