When dealing with a broad diff with multiple reviewer findings, use a three-stage chain to keep the worktree safe and avoid multiple writers competing in the same space.
The Three Stages:
- Planning (Parallel Read-Only): Multiple
reviewer agents (one per issue cluster) inspect the diff and return proposed fixes/validation plans. They must not edit. - Implementation (Single Writer): One
worker receives the planning summaries via {previous} and is the sole agent allowed to edit the active worktree. - Validation (Parallel Read-Only): Multiple
reviewer agents inspect the worker's diff from fresh context to report pass/fail and remaining blockers.
Best Practices:
- Use
async: true and context: "fresh" for planners/validators. - Use
outputMode: "file-only" for large summaries. - Use
as and {outputs.name} to pass specific results between stages instead of the entire {previous} blob. - Use
phase and label to make the async status readable.
subagent({
async: true,
context: "fresh",
chain: [
{ parallel: [
{ agent: "reviewer", phase: "Planning", label: "Deploy docs", as: "deployPlan", task: "Plan fixes for deploy docs/workflow. Inspect the current diff. Do not modify project/source files; returning findings via the configured output artifact is allowed.", output: "plans/deploy.md", outputMode: "file-only" },
{ agent: "reviewer", phase: "Planning", label: "Scheduler contract", as: "schedulerPlan", task: "Plan fixes for scheduler contract. Inspect the current diff. Do not modify project/source files; returning findings via the configured output artifact is allowed.", output: "plans/scheduler.md", outputMode: "file-only" },
{ agent: "reviewer", phase: "Planning", label: "Sandbox/security", as: "sandboxPlan", task: "Plan fixes for sandbox/security. Inspect the current diff. Do not modify project/source files; returning findings via the configured output artifact is allowed.", output: "plans/sandbox.md", outputMode: "file-only" }
], concurrency: 3 },
{ agent: "worker", phase: "Implementation", label: "Apply accepted fixes", as: "workerResult", task: "Apply only the accepted fixes from these planning summaries. You are the sole writer for the active worktree. Run focused validation and report changed files, commands, failures, and remaining issues.\n\nDeploy plan:\n{outputs.deployPlan}\n\nScheduler plan:\n{outputs.schedulerPlan}\n\nSandbox plan:\n{outputs.sandboxPlan}", output: "worker/fixes.md", outputMode: "file-only", progress: true },
{ parallel: [
{ agent: "reviewer", phase: "Validation", label: "Deploy/scheduler validation", task: "Validate the post-worker diff for deploy and scheduler fixes. Start from the worker result: {outputs.workerResult}. Do not modify project/source files; returning findings via the configured output artifact is allowed.", output: "validation/deploy-scheduler.md", outputMode: "file-only" },
{ agent: "reviewer", phase: "Validation", label: "Sandbox validation", task: "Validate the post-worker diff for sandbox/security fixes. Start from the worker result: {outputs.workerResult}. Do not modify project/source files; returning findings via the configured output artifact is allowed.", output: "validation/sandbox.md", outputMode: "file-only" }
], concurrency: 2 }
]
})