<!-- SPDX-FileCopyrightText: Copyright (c) RapierCraft Studios -->
<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->

# /work-on runtime architecture: phases, forks and nesting depth

This document defines how the `/work-on` router runs its phases on Claude Code, and why.
It is the source of truth for which skills fork, who may invoke them, and where sub-agents
can be spawned. `commands/work-on.md` (Hard Rules, Spawn-Decision Policy, Depth Budget) must
agree with it.

## 1. Runtime facts

| # | Fact | Source |
|---|------|--------|
| F1 | A sub-agent can spawn sub-agents up to **3 layers below the main conversation** (default since Claude Code 2.1.219). At the limit, the `Agent` tool is withheld. `CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH` overrides the limit. Earlier defaults: 2.1.172–2.1.216 = 5 layers, 2.1.217–2.1.218 = 1 layer. | code.claude.com/docs/en/sub-agents |
| F2 | A skill with `context: fork` starts a **new sub-agent** (type from `agent:`, default `general-purpose`) with the skill body as its prompt. It counts as a layer, sees no caller history, and returns only its final reply. | code.claude.com/docs/en/skills |
| F3 | A forked skill runs **in the background by default**: the invoking turn does not wait and the result arrives later as a notification to the main conversation. `background: false` makes the invoking turn wait for the result. | code.claude.com/docs/en/skills |
| F4 | Measured on 2.1.294 with plain agents: layer 1 and layer 2 have `Agent`; layer 3 does not. | probe, 2026-10-08 (forge#3398) |
| F5 | Measured on 2.1.294 with forked skills: a fork **always runs**, at any layer (a fork invoked from layer 3 ran at layer 4). Only its `Agent` tool follows F1: a fork at layer 2 has `Agent`, a fork at layer 3 or deeper does not. | probe, 2026-10-08 (forge#3398) |
| F6 | Measured on 2.1.294 / 2.1.295 (forge#3437): an `Agent` launched **directly** by a sub-agent reports its completion to that sub-agent, but an `Agent` launched from inside a **forked skill** is attributed to the **root session**, so its completion notification goes to the root and never reaches the worker. Inside a sub-agent the spawn surface is `Agent(description, effort, isolation, prompt, subagent_type)` only: async-only (no `run_in_background`/wait parameter) and no `Task` tool. | orchestrate batch 20261008T170649, probe |

So the depth limit constrains exactly one thing: **where a sub-agent can be spawned**. Forking
for context isolation is free at any depth; spawning `Agent(...)` sub-agents only works from layer 2
or shallower (under the default limit of 3).

F3 explains two field symptoms: phases returning "running"/empty results to the router, and phase
completion notifications arriving in the orchestrator's root session instead of the worker. F6 adds
the spawn-site ownership rule (R6): reviewers that `/review-pr` launches inside a forked phase are
owned by the root, so their completion is read from GitHub, not from notifications.

## 2. Observed failure (forge#3391, AlterLab#34442)

```
orchestrator (main, L0)
 └─ worker Agent: /work-on router                L1
     └─ work-on:review (fork)                     L2
         └─ work-on:remediate (fork, nested)      L3  ← fork runs, but has no Agent tool
             └─ review-pr domain reviewers        L4  ← cannot be spawned
```

`commands/work-on/review.md` invoked `work-on:remediate` from inside the review fork (the CI-gate and
in-PR-fix paths). Remediation still ran, but at L3 its re-review could not spawn reviewers, so it
posted `REREVIEW-REQUIRED` and stranded the issue. The normal path (worker L1 → review L2 → reviewers
L3) works, which is why most PRs are reviewed normally.

## 3. Design rules

**R1 — Dispatching phases are invoked only by the router.** A *dispatching* phase is one that spawns
`Agent(...)` sub-agents directly or through `/review-pr`: `work-on:review`, `work-on:remediate` and, since forge#3971,
`work-on:build` (its B5.6 self-review dispatches the risk-tier roster's domain reviewers on the staged diff).
Only the router may invoke them, so they always run at router layer + 1 (L2 under `/orchestrate`,
L1 solo). When another phase needs one, it returns a handoff (`status: NEXT`, `next: <phase>`) and
the router invokes it. Concretely: review returns `next: remediate` instead of calling remediation.

The same rule gives the `PHASE_TRAIL_FAILED` re-dispatch a single owner: `work-on:review` Phase R4 alone
re-runs the missing phases. `/review-pr` (Phase 8, via `scripts/review-finalize.sh merge-gate`) and
`work-on:remediate` (Phase M6) only report the status with its `MISSING:` lines and never re-dispatch. When
`/review-pr` finds neither `Task` nor `Agent` ("neither tool available"), the usual cause is this depth limit
(F1/F5): the review was invoked from inside a fork instead of by the router, so `Agent` was withheld.

**R2 — Non-dispatching forks may nest.** Phases and children that never spawn sub-agents may invoke
further forks at any depth (F5): build → context/architect/implement/validate → quality-gate,
review → quality-gate / missing-phase re-dispatch, remediate → close. The build children never spawn: the
self-review reviewers are spawned by `work-on:build` itself, and its repair round is an `implement` fork. Forking them keeps each
context small. If one of them ever needs to spawn sub-agents, it becomes a dispatching phase and
R1 applies.

**R3 — Phases run synchronously.** Every forked phase and child declares `background: false`, so a
`Skill(...)` call returns the `*_RESULT` block in the caller's turn. The "running/empty return"
handling in the router's Hard Rule 3a stays as a defensive fallback only (see R6 for reviewers
spawned inside those phases).

**R4 — Depth preflight, fail loud.** Router Phase 0 checks that a dispatching phase will have the
`Agent` tool: router layer + 2 ≤ effective spawn depth (router layer 1 with `--under-orchestration`,
else 0; depth from `CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`, else the Claude Code version default).
If not, the router stops before any work with a clear blocker instead of discovering it after the
code is written (`scripts/spawn-depth-check.sh`).

**R5 — Fallbacks stay fallbacks.** `REREVIEW_REQUIRED` (forge#3240) and the stranded-handoff
handling from forge#3391 / PR #3395 remain as safety nets for an unexpected runtime, not as the
normal remediation path.

**R6 — Spawn-site ownership: completion is read from GitHub, not notifications.** Who receives a
child's completion notification depends on where it was spawned (F6): directly by the worker, the
worker; from inside a forked phase (the `/review-pr` domain reviewers launched in `work-on:review` /
`work-on:remediate`), the root session. Because the sub-agent `Agent` tool is async-only, a forked
phase hosting `/review-pr` cannot wait on a notification. Therefore:
- `/review-pr` treats each selected reviewer's `FORGE:REVIEW-AGENT:{domain}` PR comment, carrying a
  `Reviewed-SHA:` line equal to the current head, as the completion signal and polls for it with a
  bounded wait (`REVIEWER_WAIT_SECS`) when its dispatch tool is `Agent`. A missing notification is
  never a missing review. On timeout it keeps the `review-degraded` hard stop.
- A re-invocation of review or remediate is idempotent: a complete current-SHA panel is reused, only
  missing domains are re-dispatched, and the router consumes an existing current-SHA panel and
  verdict instead of counting the re-invoke as a stall.
- Dispatching phases remain router-only (R1); this rule adds no nesting and does not raise the
  router's 2-retry cap. Reviewer notifications still reach the root session under `/orchestrate`
  (noise only); fully nesting them needs an upstream Claude Code change.

## 4. Layer map

| Skill | Fork | Dispatches sub-agents | Invoked by | Layer solo / orchestrated |
|-------|------|-----------------------|------------|---------------------------|
| `work-on` (router) | — | no | user / orchestrator worker | 0 / 1 |
| `work-on:investigate`, `work-on:decompose`, `work-on:close` | yes | no | router | 1 / 2 |
| `work-on:build` (B5.6 self-review) | yes | **yes** (self-review reviewers; degrades to a recorded skip when `Agent` is withheld) | router only (R1) | 1 / 2 |
| `work-on:build:{context,architect,implement,validate}` | yes | no | `work-on:build` (R2) | 2 / 3 |
| `quality-gate` | yes | no | validate (R2) | 3 / 4 |
| `quality-gate` (stale-review re-gate) | yes | no | review (R2) | 2 / 3 |
| `work-on:review` → `/review-pr` | yes | **yes** | router only (R1) | 1 / 2 |
| `work-on:remediate` → `/review-pr` | yes | **yes** | router only (R1) | 1 / 2 |
| `/review-pr` domain reviewers | Agent | no (leaves) | `/review-pr` | 2 / 3 |
| build self-review reviewers (`FORGE:SELFREVIEW-AGENT:*`) | Agent | no (leaves) | `work-on:build` B5.6 | 2 / 3 |

`/orchestrate` Step 4B item 6.4 dispatches a worker that runs `work-on <pr> --remediate`: worker L1,
remediate L2, reviewers L3, same as the table.

### Pre-handoff base sync and union registries (forge#3969)

`work-on:review` merges `origin/<base>` into the branch before the first push, so a PR is never born stale and a conflict is resolved while the build context is warm. Hand-maintained append-only registries (test lists, cron targets) are the usual conflict source; `scripts/detect-append-registries.sh` ranks them from history and proves a `.gitattributes` `merge=union` entry safe (`--check-union`). Union is allowed only for line-independent files, never YAML/JSON maps or ordered lists (see `.gitattributes`). `scripts/review-delta.sh` still treats a hand-resolved merge as not `BASE_SYNC_ONLY`, but accepts a merge whose tree is exactly what a mechanical `git merge-tree` of the same parents produces (a clean or union merge), so union registries take the no-panel fast path.

### Build-time self-review (forge#3971)

`work-on:build` Phase B5.6 runs the risk-tier roster (`scripts/selfreview-plan.sh`, which calls `scripts/review-roster.sh` on the staged diff) before validate, with the same personas as `/review-pr` in SELF-REVIEW MODE (`commands/review-pr-agents/protocols.md`). It is not the official review: reviewers post `FORGE:SELFREVIEW-AGENT:{domain}` issue comments pinned to the staged tree (`Tree:`), never `FORGE:REVIEW-AGENT`, and `review-finalize.sh` / `verify-phase-trail.sh` ignore them. Like R6, completion is read from GitHub with a bounded wait, because reviewers spawned inside a fork report to the root session. A build at a layer where `Agent` is withheld, or any self-review failure, degrades to a recorded skip (`build_selfreview skipped=<reason>`) and never blocks the build.

## 5. Router routing

| Phase result | Router action |
|--------------|---------------|
| `REVIEW_RESULT: status: NEXT, next: remediate` | Phase 4R: post the bound marker (`CI_REMEDIATION` / `INPR_REMEDIATION` / `BASESYNC_REMEDIATION`, the last for `remediation: base-sync`, a merge-only base sync) immediately before invoking `work-on:remediate`, so an interrupted handoff is retried, not counted. `AUTO-LANDED` → done (remediation already ran close). `REREVIEW_REQUIRED` → fallback re-review from the router. `ALREADY_DONE` (single-attempt guard) → `inpr-fix`: re-invoke review (waive); `ci-gate`: ensure `needs-human`, stop. `BLOCKED` with a `deferred-live-run:` blocker (a live earlier remediation owns the PR) → no `needs-human`, keep `workflow:remediating`, stop. Any other `BLOCKED` → ensure `needs-human`, stop. Resume after an interrupted remediation (no `needs-human`, no later `REMEDIATION:COMPLETE`) re-adds `needs-human` and returns to 4R, not to review. Other outcome: `inpr-fix` → re-invoke review once (it waives the in-PR gate); `ci-gate` → terminal. |
| `REMEDIATE_RESULT` from `--remediate` entry | unchanged (Phase 0A.1) |

## 6. What does not change

- Build still sequences its own children (worktree, then context / architect / implement / validate). The headless engine (`bin/engine/phases.mjs`) is **not** exempt from the argument contract: every phase sub-skill takes all inputs as arguments, so each engine phase builds them via `buildArgs` (`--repo`, `--gh-flag`, `--base`, `--worktree`, `--branch`, `--pr`, `--terminal-state` as the target spec's `argument-hint` requires). The engine's phase table is investigate, decompose, build, review, remediate, close; `context` and `architect` are no longer engine phases because they need the worktree build creates. `bin/engine/phases-args.test.mjs` fails CI when a phase's args drift from its spec's `argument-hint`.
- `/orchestrate` still dispatches one worker `Agent` per issue (L1).
- Phases keep their specs, `*_RESULT` contracts and GitHub markers; phase-trail verification is unchanged.
- Solo `/work-on` gets one extra layer of headroom.

## 7. Pipeline cost report

`bash scripts/cost-report.sh [--runs-dir DIR] [--last N] [--versions a,b] [--min-n N]` reads the engine run logs (`~/.forge/runs/*.jsonl`) and prints a markdown table per ForgeDock version and repo: finished-run count `n`, mean and median cache-read tokens per issue, mean per phase, remediated %, merged %, and cache-read tokens per merged issue. Groups with `n` below `--min-n` (default 10) are flagged `low-n`. The staging review (`commands/review-pr-staging.md` Phase 8) embeds it for the current version and the previous two (`--last 3`).

- Only finished runs count (a `RUN_TERMINAL` after any `RUN_REOPEN` / `RUN_NEXT_CYCLE`). Unfinished runs and hydrated runs (reconstructed from `FORGE:STATE`, no usage data) are excluded and counted in the footer.
- Attribution comes from `RUN_START`, which now records `version` (from `package.json`) and `repo` (`owner/repo`) on all three writers: the engine's fresh start, the hydrate path (`hydrated: true`), and the interactive hook. Both are best-effort and null when unresolved; `deriveState` ignores them.
- Older logs have neither: the repo shows `unknown` and the version is estimated from the log's mtime against release times (git tags `v*`, or `--release-times FILE` with `<version> <epoch>` lines), marked `~`.
- Fixture test: `bash scripts/cost-report.test.sh` (fixtures in `scripts/fixtures/cost-report/`).

## 8. Outcome codes

Every `PHASE_FAILED`, blocked `PHASE_COMMIT` and `RUN_TERMINAL` run-log event carries a machine-readable `code` from a closed, versioned enum (`bin/engine/outcome-codes.mjs`), and `FORGE:STATE` records the causing code as `outcomeCode`. Codes come from structured evidence only, never from the prose `detail`; an unmapped outcome is `unclassified` with the raw reason kept. See `docs/OUTCOME-CODES.md`.

## Remediation re-gate outcomes and spawn retries (forge#4021)

The `**Re-gate outcome**` token in a `FORGE:REMEDIATION:COMPLETE` trail is a closed vocabulary the engine parses:
`AUTO-LANDED`, `HELD-AWAITING-MERGE`, `RE-ESCALATED`, `UNFIXABLE`, `REREVIEW-REQUIRED` and `INPR-FIX-REQUESTED`.
`INPR-FIX-REQUESTED` (with `**INPR round**: N`) means the re-review approved but posted a `FORGE:INPR_FIX` work
order. The engine commits `remediate` with `outputs.remediation = "inpr-fix"`, re-arms it once, and re-reviews. The
`INPR_REMEDIATION` marker count bounds it: a second request escalates to `needs-human`.

A CLI spawn failure (`ETIMEDOUT`, `EAGAIN`, `ENOMEM`, `EMFILE`) carries `code = "CLI_SPAWN_TRANSIENT"`. The engine
retries it after a 10s/30s/60s backoff on a separate counter (max 3) that does not consume a phase attempt; once
spent, normal attempt accounting applies. `ETIMEDOUT` (a `FORGEDOCK_CLI_TIMEOUT_MS` kill) is in this class by design,
so a phase that times out every time can run up to `maxAttempts + 3` full-length CLI sessions; lower the timeout or
max attempts if that cost is unacceptable.
