# Workflow Label State Machine

**Canonical reference** — all pipeline commands and docs point here instead of restating this table inline.

See also: [FORGE Annotation Protocol §6](forge-protocol-v1.md#6-label-state-machine) for the protocol-level specification.

---

## States

| Label | Meaning | Set by |
|-------|---------|--------|
| `workflow:investigating` | Investigation phase active | Investigator agent |
| `workflow:ready-to-build` | Investigation complete, build not started | Investigator agent |
| `workflow:building` | Build phase active | Builder agent |
| `workflow:in-review` | PR created, review active | Orchestrator agent |
| `workflow:remediating` | Autonomous remediation pending: review handed a ci-gate refusal, an in-PR fix request or a pure base conflict to `work-on:remediate`, which fixes it with no human action. NOT a human gate, NOT terminal; orchestrate classifies it IN_PROGRESS (never GATED) and `work-on:remediate` clears it on every exit | Review phase (`work-on/review.md` R4); cleared by `work-on/remediate.md` |
| `workflow:awaiting-merge` | Remediated + re-reviewed, awaiting a human merge decision | Review-pr Phase 8 (auto-merge guard) |
| `workflow:merged` | PR merged, issue closed | Close phase agent |
| `workflow:invalid` | Issue closed as invalid | Investigator agent |
| `workflow:decomposed` | Issue decomposed into sub-issues | Decomposer agent |
| `needs-human` | Pipeline blocked, human intervention required | Any agent on error |
| `workflow:engine-error` | Pipeline blocked by the engine/tool itself breaking (CLI crash, missing SDK/API key, exhausted retries with no attempt ever reaching a real outcome) — not a genuine content-level judgment call | `terminate()` in `bin/engine.mjs` (also removes a stacked `workflow:remediating`, and adds `needs-human` when the run was remediating) |

`needs-human:external` vs `parked:<code>` (forge#4656): bare `needs-human` conflated two states. `needs-human:external` is a real external prerequisite (credential, account, sign-off); its structured comment must name an owner and an unblock condition. `parked:<terminal-code>` (plus `remediation-queue`) is a mechanical park (`ci.red` after the last round, `conformance.*`, `review_incomplete`): the comment carries the code and the PR, the batch runner still sees the issue, and once recovery attempts run out it waits in the single `/work-on:remediate` queue instead of the human queue. Migrating a legacy `needs-human` issue: `classifyLegacyNeedsHuman` reads its last FORGE terminal annotation; an unclassifiable one defaults to `external`. A gate (hold list, settle, `/milestone ship` pre-flight) that waits on an issue the scheduler will not admit emits `GATE_DEADLOCK issue=#N gate=G age=Nm` (`bin/lean/gatewatch.mjs`) within one pass.

Milestone lifecycle (forge#4657): `building -> shipped:staging -> deployed:default -> closed`, recorded as `<!-- FORGE:LIFECYCLE {json} -->` in the milestone description (`bin/lean/lifecycle.mjs`); forge.yaml `milestones.close_on` (`staging|default`, default `default`) picks where it closes, and a milestone with open issues never closes. `/milestone ship` requires every open issue to be closed or carried over with a target milestone and a class (`engineering|external|post-deploy|wont-do`); `needs-human:external`, flag/legal-gated and off-base-dependent issues are never demoted to fast lane. `pipeline-status` lists `SHIPPED_WITH_OPEN` and `EMPTY_BUT_OPEN`.

`gates:` (forge#4660): a `needs-human:external` issue names what it blocks in its body, `gates: preview|staging-deploy|prod-deploy|feature-flag:<name>` (comma separated), with an `owner:` line; `Depends on #N` between prerequisites orders the digest. `scripts/readiness.mjs` turns open prerequisites into the `pipeline-status` Readiness section (per gate: prerequisites with owner and age, code milestones needed), a dependency-ordered human-action digest, and the `deploy-info` verdict: a target (preview, staging, prod) is NOT DEPLOYABLE while its gate has open prerequisites. A `feature-flag:<name>` gate stays red while open but never blocks merging flag-gated features or any deploy target.

`workflow:remediating` vs `needs-human` (forge#3541): `needs-human` is reserved for genuine human decisions and external actions (an exhausted remediation bound, an unresolvable conflict, a policy gate, spend, real-environment validation). Work the pipeline fixes by itself (a pure base-sync, a red CI gate, an in-PR finding round) is marked `workflow:remediating` instead, so operators are not alerted and orchestrate does not hold DAG dependents as GATED. `work-on:remediate` accepts either label (a bare `needs-human` from an older run still works), swaps it for `workflow:in-review` on a FIXABLE run, and on every BLOCKED/UNFIXABLE exit clears `workflow:remediating` and asserts `needs-human`.

`workflow:awaiting-merge` vs `needs-human`: both are terminal (the pipeline stops advancing the issue automatically), but they mean different things. `needs-human` means the pipeline hit a condition it cannot resolve on its own (genuinely blocked — conflicting PR, failed verdict, calibration/trust escalation, etc.) and a human must diagnose and act. `workflow:awaiting-merge` means the opposite: the PR was previously escalated to `needs-human`, has since been remediated and re-reviewed to a clean `APPROVED` verdict with no mergeability blockers, but does not yet meet the automated auto-land bar (see forge#1809 Q1) — a human only needs to click merge, not diagnose a problem. `scripts/transition-label.sh` only clears `needs-human` (best-effort) when the target state is `workflow:awaiting-merge` — every other forward transition leaves a pre-existing `needs-human` label untouched, preserving its sticky/terminal semantics.

`workflow:engine-error` vs `needs-human` (taxonomy split — commit `90376f5` / #2261): before this commit, `terminate()` wrote `needs-human` for *every* exhausted-retry blocked outcome, including cases where the engine/CLI tool itself was breaking (crash, missing API key, no SDK) rather than a genuine human-judgment call. As of `90376f5`, `runPhaseWithRetry()` tags an outcome `engine-error` when every exhausted attempt failed via a thrown runner exception (never reached `detectOutcome()`) — the engine/tool is broken, not the issue content — and `terminate()` writes `workflow:engine-error` instead of `needs-human` for that case. A genuine content-level block (unmerged PR, unresolved branch, a fixed-point zero-commits case) still writes `needs-human` unchanged. **Consequence for downstream consumers**: any dashboard, query, or health metric that counts `needs-human` issues computed a different, larger population before `90376f5` than after — a subset of what used to be `needs-human` now shows up as `workflow:engine-error` instead. This is a one-time taxonomy discontinuity, not a real change in pipeline reliability; consumers tracking `needs-human` counts over time should also track `workflow:engine-error` from this commit forward to get a like-for-like comparison. `bin/labels.json` already includes `workflow:engine-error` in this repo; syncing it to consumer/satellite repos is tracked separately (see forge#2346).

## Transitions

```
(open)
  → workflow:investigating      [Phase 1: Investigate]
  → workflow:ready-to-build     [Phase 2: Investigation complete]
  → workflow:building           [Phase 3: Build started]
  → workflow:in-review          [Phase 4: PR created]
  → workflow:remediating        [Review R4: autonomous ci-gate / in-pr-fix / base-sync handoff; no human needed]
  → workflow:in-review          [Remediate M1: fix under way]  (workflow:in-review → workflow:remediating → workflow:in-review)
  → workflow:awaiting-merge [TERMINAL] [Phase 5/review-pr: re-reviewed after needs-human, awaiting human merge]
  → workflow:merged  [TERMINAL] [Phase 5: PR merged]
  → workflow:invalid [TERMINAL] [Any phase: closed as invalid]
  → workflow:decomposed [TERMINAL] [Phase 2: split into sub-issues]
  → needs-human     [TERMINAL]  [Any phase: pipeline blocked]
  → workflow:engine-error [TERMINAL] [Any phase: the engine/tool itself broke — CLI crash, missing API key, no SDK — not a content-level judgment call]
```

**`workflow:invalid` after `ready-to-build`/`building`/`in-review`** (#2326): the enforcement hook (`bin/hooks/pre-tool-use.mjs`) allows `workflow:invalid` as a successor of these three states — not only of `workflow:investigating` — because invalidity is sometimes only discovered during architecture planning or build, once the actual code/tests are read (see #2312). This is gated, not unconditional: the hook requires a posted reversal comment already on the issue — a second `FORGE:INVESTIGATOR` annotation carrying `**Verdict**: INVALID` — before it allows the transition through. A bare relabel with no evidence trail is still blocked. `workflow:investigating → workflow:invalid` remains evidence-free (Phase 1D's normal path).

The reversal comment must also carry a trusted `authorAssociation` — `OWNER`, `MEMBER`, or `COLLABORATOR` (#2332). Marker+verdict text alone is forgeable by any commenter on a public issue with no repo write access; the hook additionally requires GitHub's own `authorAssociation` classification on that comment (returned by `gh issue view --json comments` at no extra cost) to be one of those three tiers. This check is deliberately identity-agnostic rather than a hardcoded bot-login allowlist, so it survives the pipeline's `gh` identity rotating between accounts (#1722) without needing to be updated.

## Terminal Labels

Processing stops when any of these labels is set:

- `workflow:merged`
- `workflow:invalid`
- `workflow:decomposed`
- `workflow:awaiting-merge`
- `needs-human`
- `workflow:engine-error`

## Label Exclusivity

At most one `workflow:*` label should be active on an issue at any time. When transitioning, always remove all other `workflow:*` labels:

```bash
gh issue edit {NUMBER} -R {REPO} \
  --add-label "workflow:building" \
  --remove-label "workflow:investigating,workflow:ready-to-build,workflow:in-review,workflow:remediating,workflow:awaiting-merge,workflow:merged,workflow:invalid,workflow:decomposed"
```

This is enforced by `scripts/transition-label.sh` — use `resolve_script 'transition-label'` rather than calling `gh issue edit` directly when possible.
