# delivery-4 · Verifying what Claude Code ships

Three labs for the session on verification: an acceptance gate that reads evidence out of a transcript, a review matrix that routes a change to the right Claude Code reviewers, and a quality-gate kit (Stop and PreToolUse hooks, plus a CI check) you can copy into a repo. Everything runs offline on Python 3.9+ with the stdlib. Nothing calls a model.

| Lab | What you build or run | Run (from `labs/`) |
|---|---|---|
| `acceptance_gate.py` | criteria → evidence from a stream-json transcript, with one of five verdicts per criterion, an evidence-request prompt and a `/goal` line | `python3 delivery-4/acceptance_gate.py` |
| `review_matrix.py` | concerns × reviewers (`/code-review`, `/security-review`, `/simplify`, `/verify`, ultrareview, managed Code Review, the GitHub Action, subagents, deterministic checks, humans), the smallest covering route, and generated review subagents for the gaps | `python3 delivery-4/review_matrix.py` |
| `ci_gate/` | a kit with a Stop and PreToolUse gate hook (`gate.py`), its `settings.json`, the checks file and a GitHub Actions workflow, plus `ci_gate.py`, which validates the kit and runs 25 hook scenarios in temp copies of tiny-app | `python3 delivery-4/ci_gate/ci_gate.py` |

`run_all.txt` lists the three entries for `python3 run_all.py --only delivery-4`. The labs write only to `delivery-4/out/` and to temp dirs.

## acceptance_gate.py

This lab answers four questions about each acceptance criterion: does any tool call prove it, did that check run after the last edit it depends on, did anything contradict it, or did the agent only say so? The input is `samples/edit_session.jsonl`, a stream-json transcript of an edit session on tiny-app. It was built from real edits and real test runs (see `samples/README.md`). It is graded against `samples/criteria.json`.

```text
[1] Timeline: when each check ran and each file changed (step = event index)
step  tool  target                                              result  added
────  ────  ──────────────────────────────────────────────────  ──────  ────────
8     Edit  tinyapp/pricing.py                                  ok      +2 lines
10    Edit  tests/test_pricing.py                               ok      +4 lines
12    Bash  python3 -m unittest                                 ok
15    Edit  tinyapp/pricing.py                                  ok      +1 lines
17    Bash  python3 -m tinyapp.cli --code welcome10 apple:1000  ok

[2] Criteria → evidence
id   level   verdict       criterion
───  ──────  ────────────  ──────────────────────────────────────────────────────────────
AC1  MUST    VERIFIED      A unit test covers a lower-case discount code
AC2  MUST    STALE         The full test suite passes after the last code change
AC3  MUST    CONTRADICTED  Discount codes and their percentages are unchanged (no new co…
AC4  MUST    VERIFIED      TAX_BPS values are unchanged (CLAUDE.md: don't change them wi…
AC5  SHOULD  VERIFIED      The receipt CLI applies a lower-case code
AC6  SHOULD  CLAIMED       Codes with surrounding whitespace are accepted
AC7  MUST    MISSING       Unknown codes still get no discount

  AC2: stale: `python3 -m unittest` passed at step 12, then tinyapp/pricing.py changed at step 15
  AC3: contradicted: step 15 Edit added `DISCOUNTS: Dict[str, int] = {"WELCOME10": 10, "VIP25": 25, "SPRING15": 15}`
  AC6: missing: no edit to tests/*.py adds /apply_discount\([^)]*"\s+\w+|\w+\s+"\)/
  AC6: claim: "Done: discount codes are now case-insensitive and trimmed, so `welcome10`, `Vip25` and ` WELCOME10 ` all work."
  AC7: missing: `python3 -m unittest .*-v|python3 -m unittest -v|pytest .*-v` never ran

[3] Gate
  FAIL: 2/5 MUST criteria verified; not verified: AC2, AC3, AC7
  the agent's last sentence: All acceptance criteria are met.
```

The lab then prints the follow-ups. The evidence request asks for output, not reassurance. The `/goal` line follows the docs' advice: one measurable end state, a check Claude must show in the conversation (the evaluator doesn't run commands or read files), constraints, and a turn bound, all within the 4,000-character limit.

```text
[5] /goal for the gaps (414 of 4000 characters; the evaluator only reads the conversation)
  /goal All of these hold in output you show after your last edit: `python3 -m unittest` exits 0 and prints OK; a test calls apply_discount with " WELCOME10 " and `python3 -m unittest` passes; `python3 -m unittest -v` lists `test_unknown_code ... ok`. Constraints: `git diff tinyapp/pricing.py` shows the DISCOUNTS line unchanged; `git diff tinyapp/pricing.py` shows the TAX_BPS line unchanged; or stop after 8 turns
```

The lab also reads the shared fixtures. In `common/fixtures/stream/permission_denied.jsonl`, Bash was denied in `dontAsk` mode, so the test criterion comes out MISSING ("1 attempt(s) denied by permissions") rather than CONTRADICTED, because a denial isn't a failing test. Flags: `--transcript`, `--criteria`, `--goal-turns`, `--json`, and `--strict` (exits 1 unless every MUST is VERIFIED). Outputs: `out/acceptance_report.md` and `out/acceptance_report.json`.

## review_matrix.py

This lab crosses a change's concerns (`samples/change_profile.json`) with what each Claude Code reviewer is documented to look at. It picks a greedy set cover for the high and medium concerns and writes a review subagent for each concern that no built-in reviewer covers as its main job.

```text
[1] Matrix (● primary  ◐ partial  × covers it but unavailable here  + generated subagent  · no)
concern           risk    DET  SIMP  CR  SEC  VER  ULTRA  GHA  MCR  AGENT  HUM
────────────────  ──────  ───  ────  ──  ───  ───  ─────  ───  ───  ─────  ───
correctness       high    ·    ·     ●   ·    ·    ●      ◐    ×    ·      ◐
regression        high    ●    ·     ◐   ·    ·    ◐      ·    ×    ·      ·
spec_coverage     high    ●    ·     ·   ·    ◐    ·      ·    ·    ·      ·
security          medium  ·    ·     ·   ●    ·    ·      ·    ×    ·      ◐
api_compat        medium  ·    ·     ·   ·    ·    ·      ·    ·    +      ◐
test_adequacy     medium  ◐    ·     ·   ·    ·    ·      ·    ·    +      ◐
runtime_behavior  medium  ·    ·     ·   ·    ●    ·      ·    ·    ·      ·
product_judgment  medium  ·    ·     ·   ·    ·    ·      ·    ·    ·      ●
...
  MCR   Code Review (managed, GitHub App)  [unavailable: plan in team/enterprise (have pro)]

[2] Route: the fewest reviewers that cover every high/medium concern as their documented job, in run order
  1. python3 -m unittest; python3 .claude/hooks/gate.py ci (delivery-4/ci_gate) [blocking]  covers regression, spec_coverage
  2. /code-review high  covers correctness
  3. /security-review  covers security
  4. /verify  covers runtime_behavior
  5. @agent-api-compat-reviewer  covers api_compat (generated: delivery-4/out/agents/api-compat-reviewer.md)
  6. @agent-test-adequacy-reviewer  covers test_adequacy (generated: delivery-4/out/agents/test-adequacy-reviewer.md)
  7. human reviewer [blocking]  product_judgment; final approval (a high-risk concern is present)

  optional: /code-review ultra (alias /ultrareview) also covers correctness (a cloud sandbox; costs more)
```

Try `--plan team`: managed Code Review joins the route. The security docs describe it as "Multi-agent correctness and security review with full codebase context", so it replaces `/code-review high` and `/security-review` in the minimal cover. Those two stay listed as optional passes before you open the PR. The deterministic gate is still the blocker, because a managed review "never approves or blocks the pull request". Try `--no-origin`: `/security-review` needs an origin remote, so security falls through to the human reviewer. The generated subagents pass a frontmatter check: `name` and `description` are required, values containing `: ` are quoted, the tools are read-only, and the model is an alias. `--strict` exits 1 when a high or medium concern has no built-in primary reviewer.

## ci_gate/

See [`ci_gate/README.md`](ci_gate/README.md). The scenario run ends like this:

```text
6   Stop, red (first stop)                                 BLOCKED, reason names the test  BLOCKED (block)                     ok
9   Stop, red, budget spent                                proceeds + systemMessage        proceeds + systemMessage            ok
12  PreToolUse `unittest && git push`, red                 BLOCKED (deny)                  BLOCKED (deny)                      ok
18  broken gate.json, `git commit`, onFailure continue     BLOCKED (deny from gate.py)     BLOCKED (deny)                      ok
21  hook timeout, `git push`, onFailure continue           proceeds (fail open)            proceeds (hook failed, fail open)   ok
22  hook timeout, `git push`, onFailure block (kit)        BLOCKED (fail closed)           BLOCKED (hook failed, fail closed)  ok
23  worktree red, main green: Stop (cwd = worktree)        BLOCKED (checks the worktree)   BLOCKED (block)                     ok
24  worktree red, main green: `git push` (cwd = worktree)  BLOCKED (deny)                  BLOCKED (deny)                      ok

25/25 scenarios behaved as expected
```

## Docs used

- Stream-json events and tool input/output types: https://code.claude.com/docs/en/headless, https://code.claude.com/docs/en/agent-sdk/typescript
- `/goal`: https://code.claude.com/docs/en/goal
- Review commands: https://code.claude.com/docs/en/commands. Managed Code Review: https://code.claude.com/docs/en/code-review
- Subagent files: https://code.claude.com/docs/en/sub-agents
- Hooks: https://code.claude.com/docs/en/hooks and https://code.claude.com/docs/en/hooks-guide. GitHub Actions: https://code.claude.com/docs/en/github-actions
