Skip to main content
Guide

Phase 3 - Execute

Run waves, delegate to specialists, verify results, and persist state.

Purpose

Execute the wave plan through ordered waves with parallel task execution, scoped delegation, and verification gates.


Wave Loop (Single Loop for All Planned Work)

Load plan.yaml
       │
       ▼
┌──────────────────┐
│  Lowest Incomplete │──► Schedule eligible tasks
│      Wave          │     (plan order, max_concurrent)
└────────┬───────────┘
         │
         ▼
┌──────────────────┐
│  Delegate Each   │──► Specialist receives:
│     Task         │     plan_id, config_snapshot,
└────────┬───────────┘     cluster shared_context,
         │                 task_id, task_definition,
         ▼                 handoff (cache-lifetime order)
┌────────────────__┐
│  Persist State   │──► Update status.yaml
│   & Evidence     │     Track task and gate state
└────────┬──────────┘
         │
         ▼
┌──────────────────┐
│  Gate & Accept   │──► Persist eligible gate state
│     Results      │     Delegate selected reviewer if eligible
└────────┬──────────┘
         │
         ▼
   Next wave or Done
WAVE BARRIERSEach wave completes fully before the next starts. Within a wave, independent tasks run in parallel (up to max_concurrent_agents).

Delegation Payload (Cache-Lifetime Order)

Every task receives fields in cache-lifetime order for maximum prefix reuse:

# Plan-constant (same for all tasks in plan)
plan_id: "20261121-user-search-api"
config_snapshot: { ... }

# Cluster-shared (same for tasks in cluster)
context_cluster: "api-search"
shared_context: "REST patterns: ...; Pagination: src/utils/pagination.ts"

# Per-task (differs per task)
task_id: "implement-search"
task_definition:
  objective: "Implement GET /api/users/search"
  acceptance_criteria: [...]
  handoff:
    constraints: [...]
    relevant_context:
      - "research-api-patterns: patterns=REST, pagination=src/utils/pagination.ts"
    plan_context:
      architectural_boundaries: ["api layer", "user module"]
      risk_decisions: ["stateless search", "cursor pagination"]
    global_findings:
      - finding: "Prisma case-insensitive: contains + mode: insensitive"
        confidence: 0.98
        source_task: "research-api-patterns"
    memory_injection:
      repo: ["API conventions: Zod for query validation"]
      session: []

# Per-attempt (only on retry)
retries_used: 1

Scheduling Rules

RuleDescription
Plan orderWaves execute sequentially (1 → 2 → 3)
Parallel within waveUp to orchestrator.max_concurrent_agents (default: 2)
Cluster contiguityTasks sharing context_cluster run back-to-back
Ownership protectionOverlapping ownership → never parallel (serialized)
Retries countEach retry consumes a concurrency slot

Specialist Delegation

AgentWhen DelegatedKey Handoff Fields
gem-implementerFeatures, fixes, refactors (TDD)constraints, relevant_context, plan_context
gem-debuggerBug fixes (after diagnosis)failure, expected_behavior, evidence
gem-researcherExploration tasksexploration_mode, handoff
gem-reviewerReview gatesreview_mode, review_target, review_scope, evidence
gem-browser-testerUI/E2E testingTest scope, visual regression config
gem-devopsInfrastructure, CI/CDDeployment target, approval gates
gem-documentation-writerDocs, API docs, diagramsTarget audience, format, scope
gem-code-simplifierRefactoring, dead code removalTarget files, preservation rules

All delegation via Orchestrator - never direct agent-to-agent.


Verification Gates

Orchestrator-Selected Review Mode

The Orchestrator selects the review mode, target, scope, and gate eligibility. Reviewer invocation is not automatic for every task. A reviewer is delegated when the Orchestrator selects an eligible plan review, post-wave gate, or final review and provides the matching mode, target, scope, criteria, and evidence.

Pre-execution plan review is eligible only when HIGH complexity, high-risk signals, critic signals, or an explicit review request is present. The Orchestrator selects high mode for HIGH/high-risk work, critic mode for architecture, breaking-change, or cross-domain signals, and standard mode for an explicit review request that has no risk-specific signal.

Review Handoff

review_mode: "standard" # standard | high | critic
review_target: "integration" # plan | task | code | decision | docs | config | integration
review_scope: "changed" # changed | affected | full
handoff:
  target_reference: "src/api/users/search.ts"
  criteria:
    - "API conventions followed"
    - "Contract integrity (request/response types)"
    - "Security: input validation"
  risk_ref: "api_change, contract_change"
  evidence:
    - "tests/api/users/search.test.ts"
    - "src/api/conventions.md"

Reviewer evaluates what it's handed - plan/task/decision reviews don't gather more; code/config/integration reviews read the target as subject.

Post-Wave Quality Gate

The post-wave gate runs after all tasks in a wave complete and before the next wave starts. It is required only when the wave changes code or other non-documentation artifacts and the wave or plan complexity is MEDIUM or HIGH, or a high-risk signal applies.

  • Read-only-only waves (inspection and research with no file or state change) skip the gate.
  • Documentation-only waves (changes limited to documentation artifacts) skip the gate.
  • MEDIUM/HIGH or high-risk work that is not read-only-only or documentation-only runs the gate.

For an eligible wave, run at most two parallel gem-reviewer tasks, never exceeding orchestrator.max_concurrent_agents:

ReviewTargetScopePurpose
IntegrationintegrationchangedFull test suite across wave outputs
Code reviewcodechangedReview all modified files in wave

review_mode follows risk/complexity:

  • standard - MEDIUM, no high-risk signals
  • high - HIGH complexity or high-risk signals
  • critic - architecture, breaking_change, cross_domain_impact signals

The gate passes only when both required reviewers pass. A warning verdict passes only when the reviewer reports no completion-blocking finding or blocker. Reviewers collect every actionable finding in one pass rather than stopping at the first blocking finding.

Failure routing depends on the review target. The Orchestrator records both the repair target and the owner in the durable gate state:

  • code or integration -> target matching the review target, owner gem-debugger, then gem-implementer
  • documentation or configuration -> target matching the review target, owner is the owning specialist
  • plan -> target plan, owner gem-planner

The next wave is blocked until the gate passes or the gate is blocked after its final attempt.

Final Review Gate

The final review runs after all waves complete and before Phase 4 output. It is required when plan complexity is MEDIUM or HIGH, or any high-risk signal is present.

Run at most two parallel final reviewers with review_scope: full:

ReviewTargetScopePurpose
IntegrationintegrationfullFull test suite across all plan outputs
Code reviewcodefullReview all plan-modified files

There is no routine post-execution plan review. Add a review_target: plan reviewer only for HIGH or critic-risk work, after the code and integration reviewers finish, and limit its scope to acceptance-criteria coverage. The plan reviewer is not run in parallel with the code and integration reviewers.

Failure routing uses the same target-based rules as the post-wave gate: code or integration -> gem-debugger -> gem-implementer; documentation or configuration -> the owning specialist; plan -> gem-planner. Phase 4 output is blocked until the gate passes or the gate is blocked after its final attempt.

Gate Attempts, Ordering, and Persistence

Each gate has its own durable attempt counter, separate from the gate's eligibility and status. One gate cycle is one attempt, regardless of how many reviewers run in that cycle.

  • A gate has at most two total attempts across failures and interruptions.
  • Before invoking any reviewer, persist the completed task and wave state, gate eligibility and status, and the incremented gate attempt counter.
  • If attempt one fails, persist the routed repair work before attempt two.
  • If attempt two fails, persist the blocked state, failure reason, and findings. Do not invoke that gate a third time.
  • Persisting state before the gate preserves completed task and wave records if a gate fails or the run is interrupted.

Task Retries Are Independent

A task result of needs_retry permits at most three total task attempts. Increment retries_used before retrying the same task with unchanged scope and evidence.

Task retry attempts are independent from quality-gate attempts: they do not consume, reset, or increment the gate counter, and gate attempts do not consume, reset, or increment retries_used.


Persistence & State Tracking

Persistent Plans (MEDIUM/HIGH)

docs/plan/{plan_id}/
├── plan.yaml              # Authoritative (never modified after creation)
├── status.yaml            # Mutable execution state
├── wave-1/
│   ├── task-a/
│   │   ├── output.md      # Agent output
│   │   ├── handoff.yaml   # Typed handoff for downstream
│   │   └── artifacts/     # Logs, traces, screenshots
│   └── ...
└── wave-2/
    └── ...

Status.yaml (Updated Each Wave)

The mutable execution state is persisted at docs/plan/{plan_id}/status.yaml. Gate state is durable and survives interruption. Each gate record includes its identifier, attempt count, eligibility, status, findings, reason, and repair state.

plan_id: "20261121-user-search-api"
current_wave: 2
completed_waves: [1]
completed_tasks:
  - task_id: "research-api-patterns"
    status: "completed"
    output_path: "wave-1/research-api-patterns/output.md"
    handoff_path: "wave-1/research-api-patterns/handoff.yaml"
pending_tasks:
  - task_id: "implement-search"
    status: "in_progress"
    retries_used: 0
blocked_tasks: []
gates:
  - gate_id: "wave-1-post-wave"
    attempt_count: 1
    eligibility: "eligible"
    status: "failed"
    findings:
      - "Integration check failed: missing contract assertion"
    reason: "Integration reviewer reported a completion-blocking finding"
    repair_state:
      state: "routed"
      target: "integration"
      owner: "gem-debugger"
      task_id: "repair-wave-1-integration"

Ephemeral Plans (TRIVIAL/LOW)

  • No docs/plan/ artifacts
  • State tracked in memory only
  • plan_id used for correlation

Result Classification & Routing

After each task, Orchestrator classifies result:

ClassificationMeaningHandler
completedSuccess, criteria metContinue to next task
needs_retryTransient failureRetry same task (max 3 total task attempts; increment retries_used)
needs_revision + clarification_neededScope ambiguousAsk user, no retry
needs_revision (plan)Plan issuePlanner with revision_findings
needs_revision (code)Code issueOwning specialist with revision_findings
failedExecution failureInfer: fixable (code) or needs_replan (analysis)
fixable / regression / new_failureCode bugDebugger → Implementer
needs_replanScope/dependencies wrongPlanner (bounded replan)
flakyIntermittentSpecialist re-runs once
platform_specificPlatform issueSpecialist verifies criteria
test_bugTest defectDebugger → Implementer
blocked / escalateExternal dependencyStop path, escalate to user

Evidence Handling

Task-Scoped Artifacts (Detailed Evidence)

wave-3/implement-search/
├── output.md              # Summary for Orchestrator
├── handoff.yaml           # Typed handoff for next wave
├── artifacts/
│   ├── test-results.xml   # Full test output
│   ├── lint-report.json   # Lint results
│   ├── typecheck.log      # TypeScript output
│   └── coverage/          # Coverage reports
└── handoff-relevant/      # Referenced by downstream tasks
    └── api-contract.json  # OpenAPI spec for reviewer

Handoff References (Not Inlined)

# In next task's handoff.relevant_context
- "implement-search: handoff=wave-3/implement-search/handoff.yaml"
- "implement-search: test_results=wave-3/implement-search/artifacts/test-results.xml"

Exception: shared_context findings (consumed by ≥2 tasks) inlined verbatim.


Learning & Memory Promotion

After final success (all waves complete):

  1. Evaluate findings for:
    • Stability (stable: true)
    • Reusability (applies beyond this task)
    • Repetition (seen in multiple tasks)
    • Persistence (durable pattern)
  2. Promote if confidence ≥ 0.95 to:
    • Repo memory (workspace-scoped, persists with repository)
    • Session memory (conversation-scoped, clears after)
  3. Package as skill if workflow pattern is reusable:
    • .apm/skills/new-skill/SKILL.md
    • Scripts, references, assets as needed

Scheduling Example: Wave 2 with Two Implementer Tasks

TaskAgentFilesCan Run Parallel?
implement-searchgem-implementersearch.tsYes (with write-search-tests)
write-search-testsgem-implementersearch.test.tsYes (same agent, overlapping files) → serialized

Result: Both coalesced into one task, single invocation, one context copy.

Concurrency Math

With max_concurrent_agents: 2:

WaveTasksParallel Slots UsedWall-Clock
Wave 11 researcher11 task duration
Wave 22 implementers (coalesced to 1)11 task duration
Wave 31 reviewer11 task duration

Total: 3 sequential waves = 3 task durations (no parallelism possible due to wave barriers).

With max_concurrent_agents: 4 and independent tasks in a wave:

WaveTasksParallel Slots UsedWall-Clock
Wave 12 researchers (independent)21 task duration
Wave 22 implementers (different files)21 task duration

Total: 2 sequential waves = 2 task durations.

Post-Wave Quality Gate: Concrete Example

Wave 2 completes (implement-search + write-search-tests). Plan complexity is MEDIUM with risk signal api_change, and the wave changes code, so the gate is eligible.

Two reviewers run in parallel:

ReviewTargetScopeModePurpose
IntegrationintegrationchangedstandardFull test suite across wave outputs
Code reviewcodechangedstandardReview all modified files in wave

Before either reviewer runs, the completed task and wave state plus gate attempt 1 are persisted.

If integration review fails: route to gem-debugger -> gem-implementer, persist the repair work, then run gate attempt 2. Wave 3 remains blocked.

If code review fails: route to gem-debugger -> gem-implementer, persist the repair work, then run gate attempt 2. Wave 3 remains blocked.

If attempt 2 fails: persist the blocked state, reason, and findings. Do not run attempt 3.

A read-only research wave or a documentation-only wave would skip this gate, even when the overall plan is MEDIUM.


Final Review Gate: Concrete Example

All waves complete. Plan complexity is MEDIUM with risk signal api_change.

Two final reviewers run in parallel with review_scope: full:

ReviewTargetScopeModePurpose
IntegrationintegrationfullstandardFull test suite across all plan outputs
Code reviewcodefullstandardReview all plan-modified files

There is no routine plan review for MEDIUM work. If the work were HIGH or critic-risk, a review_target: plan reviewer would run after these reviewers and check only acceptance-criteria coverage.

If a review fails: persist the routed repair work and run final gate attempt 2. Phase 4 output remains blocked. If attempt 2 fails, persist the blocked state, reason, and findings without a third attempt.


Evidence Handling Example

Implementer output (minimal):

status: "completed"
handoff:
  implemented_files:
    - "src/api/users/search.ts"
  test_files:
    - "tests/api/users/search.test.ts"
  api_contract_path: "wave-3/implement-search/artifacts/api-contract.json"
  test_results_path: "wave-3/implement-search/artifacts/test-results.xml"

Next task's handoff.relevant_context:

relevant_context:
  - "implement-search: handoff=wave-3/implement-search/handoff.yaml"
  - "implement-search: test_results=wave-3/implement-search/artifacts/test-results.xml"

Exception: shared_context findings (consumed by ≥2 tasks) inlined verbatim, not referenced by path.


Learning & Memory Promotion Example

After final success for plan 20261121-user-search-api:

FindingConfidenceSource TaskMemory Tier
Pagination helper pattern reusable across endpoints0.98implement-searchrepo
Zod query validation convention for search params0.96implement-searchrepo
Prisma case-insensitive search: contains + mode: insensitive0.99research-api-patternsrepo

Skill extraction: If pagination pattern used in ≥2 tasks → gem-skill-creator packages .apm/skills/api-pagination/SKILL.md.


Key Rules

  1. One wave loop - Ordered waves, parallel within, clustered contiguously
  2. Cache-lifetime payload order - Plan-constant → cluster → per-task → per-attempt
  3. Scoped delegation - Each agent gets only what it needs
  4. Verification gates - Reviewer only when triggered. Read-only-only and documentation-only waves skip post-wave gates; other MEDIUM/HIGH or high-risk work runs them. The final review runs at most two parallel code and integration reviewers, with a plan reviewer only for HIGH or critic-risk work.
  5. Persist before gate - Task and wave state, evidence, gate status, and the incremented gate attempt are persisted before reviewers run.
  6. Classify every result - No blind retries, route to correct handler
  7. Evidence by reference - Artifacts in task paths, handoffs reference by path
  8. Learn after success - Confidence-gated, stable findings only

Next Phase

→ Phase 4: Output - Status reports, learnings, memory promotion