---
requirements:
  reasoning: High
  context: Medium
  speed: Low
audience: subagent
phase: validate
context:
  architecture: false
  prior_summaries: none
  persona: summary
  master_index: false
  diff_mode: true
deps:
  personas: [qa-engineer]
  skills: [qa-engineer, generic]
  templates: []
  sub_workflows: []
  kb_docs: [architecture/stack.md]
  config_fields: [commands.test, paths.engineering]
---

# 🍵 Meta-Workflow: Validate Task

## Purpose

The QA Engineer performs a final validation of the implementation against the acceptance criteria and the technical spec.

<!-- See _fragments/iron-laws.md for Iron Laws section structure guidance -->
## Iron Laws

- Validate against the acceptance criteria as written; do not soften, expand, or reinterpret them. The validator's job is to catch what the implementer optimistically considered "done".
- Announce your identity via `forge_banner` before any other tool use — `.forge/personas/qa-engineer.md` is already loaded as your system prompt; do not re-read it.
- All store I/O via `forge_store`. Never edit `.forge/store/*.json` directly.

## Store-Write Verification

On a store-write failure (non-zero exit or `PreToolUse` exit 2): fix the JSON and retry, up to 3 times, then halt and escalate. Never set `FORGE_SKIP_WRITE_VALIDATION=1`. Rules: `_fragments/store-write-verification.md`.

## Algorithm

```

0a. Pre-flight Gate Check:
   - **Entity-mode resolution:** from the kickoff args, `--task {id}` → `entity_kind="task"`; `--bug {id}` → `entity_kind="bug"`; either sets `record_id={id}`. Every call below uses those two.
   - Run: `forge_preflight({ phase: "validate", {entity_kind}: "{record_id}" })`
   - Exit 1 (gate failed) → print stderr and HALT. Do not proceed; do not attempt to produce the artifact.
   - Exit 2 (misconfiguration) → print stderr and HALT.
   - Exit 0 → continue.

0b. Pipeline Step Guard (user-invoked state check):
   - If `--force` is present in the invocation arguments, skip this step entirely.
   - If `entity_kind == "bug"`, skip this step entirely (bug state is managed by meta-fix-bug.md).
   - Read current task state:
     `forge_store({ command: "read", args: ["task", "{record_id}", "--fields"] }) status`
   - Extract the `status` field from the JSON output.
   - Allowed states for this phase: `implemented`, `review-approved`.
   - If the current status is NOT in the allowed set:
     Print the following and HALT (do not proceed):
     `× Task {record_id} is in state '{status}' — /forge:implement must complete first. To run the full pipeline: /forge:run-task {record_id}`

1. Read Review Loop Context:
   - Check the spawning prompt for a `### Review Loop Context` block.
   - If present, extract:
     - `Iteration: N of M` — current attempt number and the configured limit
     - `Is final iteration: true/false`
   - If absent (user-invoked): treat as `iteration 1`, no limit — never read a cap from
     config. The orchestrator owns loop budgets (forge-engineering#34).
   - Include `(iteration N of M)` (orchestrated) or `(standalone review)` in the opening line of the `VALIDATION_REPORT.md` artifact.
   - If this is the final iteration (`N == M`) and the verdict is `Revision Required`,
     append a `### Next Steps` section to the artifact showing:
     ```
     ### Next Steps
     - Force-approve (bypass remaining reviews): `/forge:approve --force {record_id}`
     - Increase iteration limit: edit `config.pipelines.{pipeline}.phases[validate].maxIterations`
     - Restart from validation: `/forge:validate {record_id}`
     ```

2. Load Context:
   - Read task prompt
   - Read SPRINT_REQUIREMENTS.md — the acceptance criteria live there. Resolve
     the sprint via the task's sprint FK, then read the sprint record's `path`;
     never reconstruct it. Absent → use the task prompt's AC, note the gap.
   - Read approved PLAN.md
   - Read the implementation
   - Read PROGRESS.md

3. Validation:
   - Validate against SPRINT_REQUIREMENTS.md's acceptance criteria. The plan's
     AC list is a cross-check only — it is the implementer's restatement. Where
     they diverge SPRINT_REQUIREMENTS.md wins, and that divergence is a finding.
   - Verify that all technical constraints (e.g., performance, security) are met
   - Check for any regressions in related functionality
   - Re-run the test suite with exactly: {{TEST_COMMAND}} — never a bare
     `python`/`python3`; the project interpreter is rarely on `$PATH`.

4. Verdict:
   - Write the validation report via:
     `forge_artifact({ command:"write", entity:"{entity_kind}", entityId:"{record_id}", artifact:"validation-report", content:"<markdown>" })`
     The markdown content must use the format:
     **Verdict:** [Approved | Revision Required]
     - If Revision Required: list the failed criteria and required fixes
     - If Approved: confirm the task is validated
     - See step 1 for iteration header and final-iteration Next Steps requirements.

5. Finalize:
   - Update task status: `forge_store({ command: "update-status", args: ["task", "{record_id}", "status", "<STATUS>"] })` where `<STATUS>` is `review-approved` if Approved, `code-revision-required` if Revision Required.
   - **Do NOT emit a phase event yourself.** The orchestrator owns event emission — it composes the canonical event from runtime telemetry (model, provider, tokens, wall times) plus the SUMMARY you write in the next step. Subagents that call `store-cli emit` for phase events hallucinate runtime facts (see Plan 11 / Slice 2). Write the SUMMARY and return.

6. Emit Summary Sidecar:
   - Write the validation summary via:
     `forge_artifact({ command:"write", entity:"{entity_kind}", entityId:"{record_id}", artifact:"validation-summary", content:"<JSON>" })`
     The JSON content must have the following shape:
     ```json
     {
       "objective":   "<one sentence — what acceptance criteria were validated>",
       "findings":    ["<up to 12 bullets, 200 chars each — pass/fail per criterion>"],
       "verdict":     "<approved | revision>",
       "artifact_ref":"VALIDATION_REPORT.md"
     }
     ```
   - Call `forge_store({ command: "set-summary", args: ["{record_id}", "validation"] })` — the
     sidecar path auto-resolves from the record's `path`; never pass it. Non-zero exit → fix
     the sidecar JSON and retry; do not proceed without a valid summary.
```

## Friction Emit
Emit `type:friction` `{workflow:validate, persona:qa-engineer, issue}` per `_fragments/friction-emit.md`.

<!-- See _fragments/generation-instructions.md for Generation Instructions template -->
## Generation Instructions

- **Workflow Structure:** The generated `validate_task.md` must follow the strict "Algorithm" block format.
- **Verdict Detection:** Instruct the QA engineer to write a literal `**Verdict:** [Approved | Revision Required]` line in VALIDATION_REPORT.md for human readability. Downstream gates read `summaries.validation.verdict` via read-verdict.cjs, not this markdown — the line remains a useful breadcrumb for operators reviewing artifacts.
- **Context Isolation:** Forbid inline execution of validation tests; use the `Agent` tool for sub-tasks.
- **Project Specifics:**
  - Reference project-specific validation tools or smoke tests.
- **Token Reporting:** See `_fragments/finalize.md` — wire via `file_ref:`.
- **Diff-mode:** Generated workflow MUST include the diff-first read mode instruction (see plan PLAN.md A6).
- **Event Emission:** Ensure the "complete" event includes the `eventId` passed by the orchestrator.
