# delivery-3: AI-native development with Claude Code

Two planning labs for the session page `delivery/03-ai-native-dev.html` (topics: planning and execution). Both are deterministic. They need no model, no network and no `claude` binary, and they read the sample repo `common/samples/tiny-app` without writing to it. Output goes to `delivery-3/out/`.

| Lab | What you build | Run (from `labs/`) |
|---|---|---|
| `risk_map.py` | A risk register for a diff or a plan. It combines path tiers, content signals, an `ast` import-graph blast radius and plan drift. Each mitigation is a Claude Code control: an ask/deny rule, a hook, a test, a review command or a human. | `python3 delivery-3/risk_map.py` |
| `parallel_plan.py` | A plan for parallel sessions built from file footprints: an overlap matrix, waves, the reason each delayed task waits, what-ifs and merge points. Each task gets a mechanism (`claude --worktree`, a subagent, the main checkout, `/batch`, agent teams). | `python3 delivery-3/parallel_plan.py` |

Inputs live in `samples/`:

- `change.diff`: a `git diff` against tiny-app for the case-insensitive discount task. It makes the change, but it also does three things the plan forbade.
- `plan.md`: the plan-mode plan for that task.
- `risk_rules.json`: path tiers, content signals and the control catalog.
- `parallel_tasks.json`: nine tiny-app tasks with write/read footprints.

## risk_map.py

```bash
python3 delivery-3/risk_map.py                                       # sample diff + sample plan
python3 delivery-3/risk_map.py --plan delivery-3/samples/plan.md      # plan only, before any code exists
python3 delivery-3/risk_map.py --strict                              # exit 1 if a high risk has no gate or deterministic check
python3 delivery-3/risk_map.py --json                                # machine-readable report
git diff main... > /tmp/change.diff && python3 delivery-3/risk_map.py --diff /tmp/change.diff --repo . --rules my_rules.json
```

Excerpt from a real run (`NO_COLOR=1 python3 delivery-3/risk_map.py`):

```text
[2] Content signals in added/removed lines
where                           tier    signal             line
──────────────────────────────  ──────  ─────────────────  ────────────────────────────────────────────────
.claude/settings.json:5         high    broad-allow        + "Bash(*)"
tests/test_cart.py:20 (before)  high    removed-assertion  - self.assertEqual(cart.tax(), 65)  # 900 * 7.2…
tinyapp/pricing.py:32           high    float-money        + rate = pct / 100 (+1 more: line 33)
.claude/settings.json:7         medium  default-mode       + "defaultMode": "acceptEdits"
[3] Blast radius (import graph of common/samples/tiny-app)
file                dependents  tests that import it           targeted test command
──────────────────  ──────────  ─────────────────────────────  ──────────────────────────────────────────────────────
tinyapp/pricing.py  5           test_cart.py, test_pricing.py  python3 -m unittest tests.test_cart tests.test_pricing
[4] Plan vs diff
  out of scope but changed: .claude/settings.json, tests/test_cart.py
  changed, not in the plan: CLAUDE.md
  planned, not changed: README.md, tinyapp/__init__.py
```

```text
[6] Mitigations as Claude Code controls
  .claude/settings.json fragment (review it before merging; written to out/settings_snippet.json):
    {
      "$schema": "https://json.schemastore.org/claude-code-settings.json",
      "permissions": {
        "ask": [
          "Edit(/tinyapp/pricing.py)"
        ],
        "deny": [
          "Edit(/.claude/settings.json)"
        ]
      }
    }
  • ask rules prompt in every mode that prompts, auto mode included (dontAsk denies them); deny rules block in every mode, bypassPermissions included
  • protected paths (.claude/settings.json): default/acceptEdits prompt, auto -> classifier, dontAsk denies, bypassPermissions allows; only a deny rule holds everywhere
  • Edit rules also cover sed/tee and redirections in Bash, not a script that opens the file itself: pair with the sandbox
[7] Review route (deterministic first; review commands advise, people and checks gate)
  1. python3 -m unittest tests.test_cart tests.test_pricing   (then the full suite: python3 -m unittest) [check]
  2. /code-review high [advisory]
  3. /security-review [advisory]
  4. send the plan drift back to Claude [check]
  5. human approval (CODEOWNERS / required reviewer) [gate]
```

What it shows:

- **The suite still passes on this diff**, so tests alone don't catch it. Content signals find a deleted assertion and float math (`pct / 100`, `round()`) in money code. CLAUDE.md already says "Never introduce floats for money". A prose rule that's still violated is a sign the rule should become a check: a test plus a hook.
- **Agent config is code.** `.claude/settings.json` is high risk. Of the two Edit rules in the snippet:
  - `deny` is the one that holds in every mode, bypassPermissions included. `.claude/` is already a protected path, but protected paths go to the classifier in auto mode and are allowed in bypassPermissions.
  - `ask` prompts even in auto mode.
- **Review commands are advisory.** `/code-review` and `/security-review` report findings. Managed Code Review never approves or blocks a pull request. `--strict` therefore requires a gate (a rule, a hook or a human) or a deterministic check (a test) on every high risk.
- **Rule anchoring.** In project settings, `Edit(/tinyapp/pricing.py)` anchors at the project root. Only `Edit(...)` and `Read(...)` path rules are consulted.

## parallel_plan.py

```bash
python3 delivery-3/parallel_plan.py                     # at most 3 writer sessions at once
python3 delivery-3/parallel_plan.py --max-parallel 6    # more seats: does it help?
python3 delivery-3/parallel_plan.py --tasks my_tasks.json --repo /path/to/repo --json
```

Excerpt from a real run (`NO_COLOR=1 python3 delivery-3/parallel_plan.py`):

```text
[3] Waves (at most 3 writers at once)
  wave 1
    codes-ci        worktree session      claude --worktree codes-ci
    receipt-json    worktree session      claude --worktree receipt-json
    parse-money     worktree session      claude --worktree parse-money
    float-audit     subagent (read-only)  in the main session: "Use the Explore subagent (very thorough) to audit tinyapp/ for float math in money paths; report file:line"
  wave 2
    region-wa       worktree session      claude --worktree region-wa
    remove-qty      worktree session      claude --worktree remove-qty
    export-receipt  worktree session      claude --worktree export-receipt
  wave 3
    docs-refresh    main checkout         claude   (one writer in this wave: no isolation needed)
  wave 4
    version-bump    main checkout         claude   (one writer in this wave: no isolation needed)
  waves: 4 (lower bound 3: codes-ci -> docs-refresh -> version-bump); 9 tasks one by one
  why some tasks wait:
task            earliest by deps  wave  reason
──────────────  ────────────────  ────  ──────────────────────────────────────────────────────────────
region-wa       1                 2     writes tests/test_pricing.py, tinyapp/pricing.py like codes-ci
remove-qty      1                 2     writes tests/test_cart.py like receipt-json
export-receipt  1                 2     writes tinyapp/__init__.py like parse-money
docs-refresh    2                 3     waits for region-wa (wave 2)
version-bump    3                 4     waits for docs-refresh (wave 3)
```

```text
[4] What-ifs: is it seats or footprints?
scenario                                                     waves  widest wave  wave 1
───────────────────────────────────────────────────────────  ─────  ───────────  ────────────────────────────────────────────────────────────────
as planned (max 3 writers)                                   4      3            codes-ci, receipt-json, parse-money, float-audit
unlimited writers                                            4      3            codes-ci, receipt-json, parse-money, float-audit
hotspot edits (tinyapp/__init__.py) applied at merge points  4      3            codes-ci, receipt-json, parse-money, float-audit
both                                                         4      4            codes-ci, receipt-json, parse-money, export-receipt, float-audit
[5] Merge points (merge in this order, run the tests after each merge)
  after wave 1: codes-ci -> parse-money -> receipt-json
    re-check receipt-json against codes-ci (pricing.py): it was built on the old version (a stale read git can't see)
    re-check parse-money against codes-ci (pricing.py): it was built on the old version (a stale read git can't see)
    re-run float-audit on the merged tree: it read the base, before codes-ci (pricing.py), receipt-json (cli.py), parse-money (__init__.py)
    hotspot tinyapp/__init__.py: waves 2 and 4 edit it again; keep this change small and merge it first
    git merge --no-ff worktree-codes-ci && python3 -m unittest   (and so on; cleanup: git worktree unlock .claude/worktrees/<id>; git worktree remove .claude/worktrees/<id>)
    before wave 2: push the merged default branch (or set worktree.baseRef to "head");
    new worktrees branch from the remote default branch by default and would miss this merge
```

What it shows:

- **Footprints cap parallelism, not seats.** Unlimited writers still take 4 waves, because one write-write conflict (`tinyapp/pricing.py`, codes-ci and region-wa) delays three tasks. "Split parallel tasks along file boundaries": move the WA region into `tinyapp/regions.py` with its own test file (exercise 1), and unlimited writers reach the lower bound of 3 waves. With 3 seats it's still 4 waves, but region-wa's delay reason becomes "no free seat". Splitting moved the constraint from footprints to seats.
- **Semantic conflicts are invisible to git.** receipt-json reads `pricing.py` while codes-ci changes it. Merge codes-ci first, then rerun the tests on each later merge.
- **The base branch matters between waves.** `claude --worktree <id>` branches from the remote default branch unless `worktree.baseRef` is `"head"`. Push the merged branch before the next wave starts, or wave 2 won't contain wave 1.
- **Mechanisms:**
  - read-only tasks go to a subagent, which returns only a summary
  - single-writer waves stay in the main checkout
  - `/batch` fits 5-30 similar `"kind": "unit"` tasks in one `"group"`
  - `"kind": "coordinate"` points to agent teams, which are experimental and off by default
- Edge cases exit 2 with a clear message: a dependency cycle (`a -> b -> c -> a`), an unknown dependency, a duplicate id, or an id that can't be a worktree name. An empty task list prints "nothing to plan" and exits 0.

## Exercises

Each script's docstring has four. Start with these:

1. `risk_map.py`: drop "test" and "hook" from the `float-money` signal in a copy of `risk_rules.json`, then run with `--strict --rules <copy>`. Why does a review command alone fail the gate?
2. `parallel_plan.py`: in a copy of `parallel_tasks.json`, make region-wa write `tinyapp/regions.py` and `tests/test_regions.py`. Compare `--max-parallel 3` with `--max-parallel 4`. What does the delay table say about region-wa in each run?

## Docs behind the encoded formats

- Permission rules, `/path` anchoring in project settings, the scope of Edit rules: https://code.claude.com/docs/en/permissions
- Protected paths per mode; ask rules prompt even in auto mode: https://code.claude.com/docs/en/permission-modes
- `/code-review`, `/security-review`, `/batch`: https://code.claude.com/docs/en/commands
- Managed Code Review (findings only, never approves or blocks): https://code.claude.com/docs/en/code-review
- Worktrees (`--worktree`, `worktree-<name>` branches, `worktree.baseRef`, `.worktreeinclude`, cleanup): https://code.claude.com/docs/en/worktrees
- Ways to run work in parallel: https://code.claude.com/docs/en/agents. Subagents: https://code.claude.com/docs/en/sub-agents. Agent teams: https://code.claude.com/docs/en/agent-teams
- "Split parallel tasks along file boundaries, merge the branches in a sensible order" (official blog): https://claude.dev/blog/claude-code-in-the-cloud/
- Human approval as a gate and CODEOWNERS (official blog): https://claude.dev/blog/solving-code-review-at-anthropic/
