# delivery-5 labs: products that learn after release

Two offline labs for the last step of the loop. The first sorts post-release signals into the Claude Code artifact that fixes each one. The second records product decisions so that Claude Code can find them before it undoes one. Both use the `tiny-app` sample (`labs/common/samples/tiny-app`), read only that sample, and write only to `delivery-5/out/` or a temp dir.

```bash
cd labs
python3 delivery-5/signals_to_context.py      # signals -> clusters -> proposals in out/proposals/
python3 delivery-5/decision_log.py            # check + recommend + render into out/decisions/, out/rules/
```

Both labs need only Python 3.9+ and the stdlib. They make no model calls, need no network and have no `--real` mode. A real version would pull signals through MCP servers for your analytics, error tracker or issue tracker (https://code.claude.com/docs/en/mcp), then feed the same JSONL shape into these scripts.

| Lab | What you build | Writes |
|---|---|---|
| `signals_to_context.py` | Deterministic clustering of 27 signals (support, incident, log, PR review, agent session), confidence from volume, source diversity and recency, and a route per theme: CLAUDE.md line, path-scoped rule, skill, hook, eval case, issue, or investigate. Each proposal is checked against the documented format, and the proposed hook and evals actually run on a temp copy of tiny-app. | `out/proposals/` |
| `decision_log.py` | A JSONL decision log (expand / revise / simplify / revert / investigate / stop). `check` validates it, `recommend` compares it with release outcomes, and `render` writes one record per decision, a CLAUDE.md pointer and a path-scoped rule. | `out/decisions/`, `out/rules/decisions.md`, `out/claude_md_pointer.md`, `out/decision_drafts.jsonl` |

## signals_to_context.py

```
python3 delivery-5/signals_to_context.py [--signals FILE] [--threshold 0.2] [--min-confidence 0.45] [--today 2026-10-20] [--json]
```

An input line looks like `{"id": "S-003", "source": "agent_session", "date": "2026-10-02", "severity": "high", "paths": ["tinyapp/pricing.py"], "text": "..."}`. The `id`, `source`, `date` and `text` fields are required. Sources `agent_session` and `pr_review` count as signals about how Claude worked. Everything else counts as a signal about the product.

Real output (Python 3.9.6, default flags):

```
[2] Clusters (single-link, Jaccard >= 0.2); confidence = 0.4 volume + 0.3 diversity + 0.3 recency
id  theme (top shared terms)    n  sources                  confidence   about
──  ──────────────────────────  ─  ───────────────────────  ───────────  ───────
C1  case · code · lower         4  log,support              0.82 high    product
C2  card · charge · total       4  incident,log,support     0.92 high    product
C3  float · integer · math      4  agent_session,pr_review  0.82 high    agent
C4  assertion · failing · test  3  agent_session,pr_review  0.74 high    agent
C5  export · init · list        3  agent_session,pr_review  0.74 high    agent
C6  rate · region · add         3  agent_session,pr_review  0.74 high    agent
C7  tax · discount · computed   3  incident,log,support     0.84 high    product
C8  commit · id · message       2  agent_session,pr_review  0.66 medium  agent
C9  client · email · font       1  support                  0.18 low     product
[3] Routing (first matching rule wins)
  C3 float · integer · math -> hook + eval case (R2)
     why: CLAUDE.md line 19 already says 'All amounts are integer cents. Never introduce floats for money.' and Claude still breaks it. CLAUDE.md is context, not enforced configuration; a deterministic check is the next step. A permission rule can gate a tool or a path, not what the edit contains, so this is a hook; high-severity signals, so an eval case locks the fix
  C4 assertion · failing · test -> path-scoped rule + eval case (R3)
     why: every signal points under tests/: a rule with paths: loads only when Claude works there, instead of costing context in every session; ...
  C6 rate · region · add -> skill (R1)
     why: people keep asking how to do this: a procedure Claude loads when the task comes up
  C8 commit · id · message -> CLAUDE.md line (R4)
  C9 client · email · font -> investigate (R0)
     why: 1 signal(s), confidence 0.18 < 0.45
```

The routing rules, in order (the first match wins):

| Rule | When | Route |
|---|---|---|
| R0 | one signal, or confidence below `--min-confidence` | investigate (no proposal) |
| R1 | agent signals, and at least half read like "how do I…" / "steps to…" | skill (`.claude/skills/<name>/SKILL.md`) |
| R2 | agent signals, and tiny-app's CLAUDE.md already states the rule | hook (CLAUDE.md is advisory; hooks are deterministic) |
| R3 | agent signals that all point under one directory | path-scoped rule (`.claude/rules/<topic>.md`, `paths:` only) |
| R4 | other agent signals | one CLAUDE.md line, with its evidence in an HTML comment |
| R5 | product signals that mention a regression | eval case + issue |
| R6 | other product signals | issue (+ eval case when any signal is high severity) |

Step 5 validates each artifact it wrote:

```
ok  CLAUDE.md.patch                   CLAUDE.md stays under 200 lines                          27 -> 29 lines
ok  rules/tests.md                    only `paths:` frontmatter                                paths only
ok  skills/add-tax-region/SKILL.md    description+when_to_use <= 1536                          205 chars
ok  hooks/settings_snippet.json       command hook with a timeout                              matcher 'Edit|Write', timeout 30s
ok  hooks/context_checks.py           exports_in_sync: broken file -> block                    tinyapp/pricing.py: discount_label is public but mis
ok  hooks/context_checks.py           no_float_money: broken file -> block                     tinyapp/pricing.py:32: true division '/' (use // on
ok  hooks/context_checks.py           edit outside checked paths -> quiet                      PostToolUse: success | proceeds
ok  hooks/context_checks.py           exports_in_sync: git repo via a symlinked path -> block  PostToolUse: success | decision=block | proceeds | C
ok  evals/float-money.json            grader: no float math in pricing or cart (hook -         context checks: clean
ok  evals/tax-after-discount.json     grader: tax applies after the discount (CLAUDE.m         passes on today's code

8 theme(s) with proposals, 1 to investigate; 34/34 validation checks passed. Review delivery-5/out/proposals/PROPOSALS.md
```

The hook checks run through `common.hooks.run_hook` with a documented `PostToolUse` payload, on a temp copy of tiny-app. The clean file passes. The same file with `/` in `apply_discount` gets `decision: "block"`, and so does a file with a new public function that's missing from `__all__`. On PostToolUse that block is feedback for Claude: the edit has already happened, so Claude reads the reason and fixes it (https://code.claude.com/docs/en/hooks). Every eval grader passes on today's code, which is what a regression lock should do.

What it writes to `out/proposals/`. Nothing is applied: `PROPOSALS.md` says why each file exists and how to install it.

- `CLAUDE.md.patch`: a unified diff that adds one line, plus `<!-- evidence: S-025, S-026 · confidence 0.66 · ... -->`. Block-level HTML comments are stripped before CLAUDE.md reaches Claude, so the evidence costs no context.
- `rules/tests.md`: `paths: ["tests/**/*.py"]`, the only frontmatter a rule reads.
- `skills/add-tax-region/SKILL.md`: `name`, `description`, `when_to_use`, and `metadata` with the evidence. `metadata` is a map Claude Code doesn't act on.
- `hooks/context_checks.py` + `hooks/settings_snippet.json`: a PostToolUse `Edit|Write` command hook. It also has a `--scan FILE...` mode for evals and CI.
- `evals/*.json`: a task plus graders, in this lab's minimal format. Adapt it to your harness, such as agents-5 `eval_harness.py`.
- `issues/*.md`: the evidence, the suspected files and draft acceptance criteria. Lint the brief you write from one with `delivery-2/brief_lint.py`.

Try these:

- `--threshold 0.15`: the float-money and tax-region themes merge through `pr`, `pricing` and `tax`, and the tax-region skill disappears.
- `--min-confidence 0.7`: the commit-prefix theme (0.66) drops to investigate.
- `--today 2026-12-31`: recency goes to 0 for every theme, and four more themes drop to investigate (five in all).

## decision_log.py

```
python3 delivery-5/decision_log.py [check|recommend|render|all] [--log FILE] [--outcomes FILE] [--signals FILE] [--today 2026-10-20] [--strict] [--json]
```

A log entry has `id`, `date`, `feature`, `title`, `decision` (one of expand, revise, simplify, revert, investigate, stop), `status` (proposed, accepted, superseded, rejected), `owner`, `context`, `evidence`, `kill_criterion`, `review_by`, `supersedes`, `topics` and `paths`. The sample log has deliberate mistakes in it.

Real output (Python 3.9.6):

```
[1] Check 8 decisions in decisions.jsonl (today = 2026-10-20)
  WARNING D-0001: superseded by D-0006 but its status still says accepted
  WARNING D-0002: 3 newer signals match its topics (S-001, S-005, S-019): re-check the kill criterion: More than 3 support tickets a month about lower-case codes being rejected.
  WARNING D-0003: 4 newer agent signals match its topics (S-003, S-009, S-015, S-021): Claude keeps undoing it, so enforce it (see signals_to_context.py)
  ERROR D-0004: supersedes D-0009, which isn't in the log
  ERROR D-0005: no evidence: name the signals or metrics this decision rests on
  ERROR D-0005: no kill criterion: say what would make you reverse it
  ERROR D-0007: decision 'pause' isn't one of expand, revise, simplify, revert, investigate, stop (did you mean 'stop'?)
  ERROR D-0007: no kill criterion: say what would make you reverse it
  ERROR D-0007: no review_by date
  summary: 6 error(s), 3 warning(s)
[2] Recommend from outcomes.json (release 0.3.0); first matching rule wins
feature             rule  recommend    confidence  the log
──────────────────  ────  ───────────  ──────────  ──────────────────────────────────────────────────────────
bulk-import         R1    investigate  medium      no decision recorded: see decision_drafts.jsonl
cart                R4    revise       high        disagrees with D-0004 (simplify): record why, or re-decide
discount-banner     R6    simplify     high        agrees with D-0008 (still proposed: accept it)
loyalty-points      R8    stop         high        D-0007 uses 'pause', not a decision type
receipt-cli         R3    revert       high        disagrees with D-0006 (revise): record why, or re-decide
region-tax-display  R5    expand       high        no decision recorded: see decision_drafts.jsonl
wa-tax-region       R2    investigate  low         disagrees with D-0005 (expand): record why, or re-decide
[3] Render to delivery-5/out/
  decisions/0006-limit-the-receipt-cli-to-ca-stores-until-the.md      D-0006 accepted
  decisions/README.md                                                 index
  decision_drafts.jsonl                                               6 draft(s) to review, then append to the log
  claude_md_pointer.md                                                4 lines for CLAUDE.md
  rules/decisions.md                                                  paths: 3 file(s); frontmatter ok
```

The recommendation rules come in order:

1. Data quality comes first (R1). `bulk-import` has the best numbers and still gets "investigate".
2. Then exposure (R2).
3. Then a critical guardrail, which means revert (R3), and a non-critical one, which means revise (R4).
4. Only then the primary metric: expand (R5), simplify when it partly worked but is costly (R6), revise (R7), stop when it's flat after 28 days (R8).

The numbers in `samples/outcomes.json` are illustrative.

Claude Code should see the decisions in two places:

- `out/claude_md_pointer.md` (4 lines): a CLAUDE.md section that points at `docs/decisions/`. It doesn't `@`-import the folder, because "Imports help you organize a long file but don't reduce its context cost, because imported files load together with the CLAUDE.md that imports them" (https://code.claude.com/docs/en/memory).
- `out/rules/decisions.md`: a `.claude/rules/` file with `paths:` for `tinyapp/cart.py`, `cli.py` and `pricing.py`. It lists the current decisions that cover each file, so the pointers load only when Claude reads or edits one of them.

With `--strict`, `check` exits 1 when the log has errors (6 in the sample). Use that as a CI gate on the log file.

## Files

```
delivery-5/
├── signals_to_context.py
├── decision_log.py          # imports the tokenizer and signal loader from signals_to_context.py
├── samples/
│   ├── signals.jsonl        # 27 post-release signals for tiny-app (illustrative)
│   ├── decisions.jsonl      # 8 decisions, some deliberately flawed
│   └── outcomes.json        # intended vs actual metrics for release 0.3.0 (illustrative)
└── out/                     # everything the labs write
```

## Docs used (fetched 2026-10-20)

- https://code.claude.com/docs/en/memory covers CLAUDE.md locations, the 200-line target, "context, not enforced configuration", imports and their cost, block-level HTML comments, and `.claude/rules/` with `paths:`.
- https://code.claude.com/docs/en/skills covers the frontmatter fields (`name`, `description`, `when_to_use`, `metadata`, `disable-model-invocation`), the 1,536-character listing cap, and "Keep SKILL.md under 500 lines".
- https://code.claude.com/docs/en/hooks and https://code.claude.com/docs/en/hooks-guide cover the PostToolUse input, `decision: "block"` as feedback, `Edit|Write` matchers, and settings registration.
- https://code.claude.com/docs/en/permissions: rules match tools and paths, not file content.
- https://www.anthropic.com/engineering/demystifying-evals-for-ai-agents: build evals from real failures.
