# Plan

The one artifact a human reads to understand the work and object to it, before any code. Two
halves: what must be true, then what is being built. They share a file because they are one
review. The builder and the reviewer hold the work against this file, so what it leaves out
reaches nobody: the questions asked here decide more of the result than anything after.

Skip this file when the change is under roughly three files with no one-way door (SKILL.md,
Artifacts).

## Before asking anything

- Read `.specs/STATE.md` `## Decisions`. Every `active` `AD-NNN` constrains the shape: conform, or
  append an entry that supersedes it and say why.
- Open every binding source in full - every capture, frame and state - before the first
  criterion.

Facts you look up; decisions you ask. Ask only what is the user's: scope, priority, product
behaviour, a trade-off.

An open architectural bet (job or request cycle, which of two architectures) is settled first in
an ADR, RFC or spike and linked. This file records the shape that won.

# What must be true

## Recover the problem

Write the problem in the present tense with no solution inside it: what is true today that should
not be, who pays for it, what it costs. "We have no Stripe integration" is the absence of an
answer; "anyone evaluating the product has to enter a card first" is a problem someone can
disagree with. Copy the evidence the source gives literally, or say it gives none.

Close the section with what is different for a user when this ships. That line is the goal the
build and the review are held against.

## How to ask

A gray area is a decision that is the user's, has more than one defensible answer, and is not
settled by the code. Anything else is an assumption you state and move on from.

Ask every gray area before presenting the plan, until the requirements leave no product decision
to the builder. A gray area does not become an `Assumptions` row to save a question.

- "You decide" is an answer: record it in `Assumptions` with `Confirmed? y` and rationale
  `user delegated`. Write `user delegated` only when the user said it in the conversation.
- Asking clarifies how, not whether to add a capability. A new capability that surfaces goes in
  `Out of scope` with its reason.
- Removing, renaming or changing something a binding source draws is the user's call. Ask it as
  a `blocks` question with your recommendation, even when the system lacks the concept. It is
  never resolved by an `Out of scope` row alone. The answer becomes an `AD-NNN` in
  `.specs/STATE.md` whose rationale quotes the question and the option chosen; each departure it
  approves lands in `## Differences from the source` citing that id.

An assumption lands in `## Assumptions` with your default, the rationale and `Confirmed? n`. `y`
means a human said so, so a `y` needs a quoted reply in `## Answers` behind it. An `n` row is a question not yet asked: name it on the `**Open questions:**` line and
ask it, your default as the recommendation, in the message that presents the plan - a user approves
what they were asked, not a row they scrolled past. No `n` row survives the approval: each becomes
`y`, answered or delegated, or moves to `Out of scope`.

Each answer goes into the plan in the turn it arrives, twice: the user's words, verbatim, under
`## Answers`, and what they change - a criterion, an `Assumptions` row with `y`, or an `AD-NNN`.
Each clause of a reply - a rule, a code, a refusal, a value - is carried by a criterion named on its
`## Answers` line; a clause no criterion carries is a gap, never a summary. The reviewer reads the
quotes, not your reading of them. A compaction keeps the file, not the chat.

**Ask what must never happen.** Once per plan, before the criteria, ask "what must never happen?"
with your own list as the recommendation: harm to a user (an email they should not get, access they
should not have, data lost), to the business (a charge, a token another job needs, a vendor's ban),
to a third party. Each item is a `## Must never` row with the mechanism that prevents it - a guard
in the tested layer, a constraint, a `Landing` door - and the criterion whose test fails if the
mechanism is gone. A harm with no mechanism is a question, not a row. `None - user: "<their words>"`
only when the user said nothing can go wrong.

## Verify what the outside does

A plan that calls a third party rests on how that party behaves, and a behaviour nobody checked is
what a review finds last. List in `## External behavior` each one the plan relies on: what a call
does and what it does besides - an email, a notification, access granted, a record created
elsewhere; what a read hides or pages away; what a repeat, a retry or a call on a removed record
does; the rate limit and who else spends the token. Each row says how it is known:

| Verified by | Means |
| --- | --- |
| `sandbox` | you made the call in a sandbox and saw the result - date it |
| `test` | a test in the repo exercises the real behaviour (vendor test mode, a contract test) |
| `production` | a read-only observation of production data - date it |
| `doc` | the vendor's reference says so - link it |
| `vendor support` | the vendor said so in writing - quote it |
| `assumed` | nobody checked |

A doc or a support answer covers what the vendor promises; what a call does besides is seen in a
sandbox. An `assumed` row is settled before the plan is presented: a spike - the sandbox call, a
throwaway script - turns it into `sandbox`; when you cannot run one, it is an open question marked
`blocks`, and the plan waits on it. `None - <why no third party is touched>` is complete.

## Walk the surfaces

Each kind of surface carries the same decisions every time. Walk the row for every surface this
feature exposes; each item becomes a criterion, an existing behaviour you name, or nothing to
decide. There is no table for it in the plan.

| Surface | The decisions it always has |
| --- | --- |
| a screen or view | empty, loading, error and unauthorised states; ordering of content; the window widths it must work at - the captures' sizes, or asked when no capture settles it; what a long list or a wide table does - what scrolls and what stays in view; what a destructive action confirms; what each control does when used - who acts, which control, what the page shows afterwards |
| an API or webhook | response shape, error shape with its codes, who may call it, versioning, rate limit behaviour |
| a command or scheduled task | output format, every flag and its default, exit codes, what it prints when it fails halfway; how it is stopped mid-run and what that leaves; the shared resources it can starve - an API token, a rate limit, a queue, a pool - and who else spends them; what a rerun or a restart after a partial run does a second time; two copies running at once; the first run's volume - the backlog it sweeps on day one - and who receives it |
| a write to a third-party system | what else the call does (emails, notifications, access granted); what a repeat does; what the vendor's reads hide afterwards; what state the record is in when the vendor or a person changes it later - each is an `## External behavior` row |
| a document or copy | structure, tone, depth, what the reader does next |
| a collection being organised | grouping, naming, ordering, duplicates, the exception that does not fit |

The two that hide best: the error shape (decided by whoever writes the first handler) and the empty
state (invisible until a new account opens the page). State, persistence and contracts go in
`Shape`, `Surface` and `Landing`, not this list.

## Write the criteria in EARS

| Pattern | Template | Use for |
| --- | --- | --- |
| Ubiquitous | The [system] SHALL [response] | always-on invariants |
| Event-driven | WHEN [trigger] THEN the [system] SHALL [response] | a response to a discrete trigger |
| State-driven | WHILE [state] the [system] SHALL [response] | behaviour that holds during a state |
| Optional-feature | WHERE [feature is present] the [system] SHALL [response] | behaviour behind a flag |
| Unwanted-behaviour | IF [condition] THEN the [system] SHALL [response] | errors, invalid input, timeouts |
| Complex | WHILE [state], WHEN [trigger] the [system] SHALL [response] | combinations |

One requirement per criterion, with a concrete value - a status code, a field, a bound - never
"quickly" or "properly". Edge cases are criteria, usually IF/THEN.

**A control owes a gesture.** Every control this feature puts in front of a user owes a criterion
that names who acts, which control, and what the page shows afterwards. A screenshot shows the
button, never what it does. Name the use as it really happens: several files where several are
allowed, editing what exists and not only creating it.

**A screen with a binding source points at it.** Each screen or state the source draws gets one
criterion naming the gesture that reaches it and its capture files - the image, and the structural
capture beside it where there is one:

```
27. WHEN bob uses "Add cover" THEN the system SHALL show the dialog in
    `.specs/features/<f>/captures/cover.png` (`.specs/features/<f>/captures/cover.aria.yml`)
```

The builder matches the image for composition and look. Do not retype the screen into criteria;
add criteria only for what the source cannot show: what a control does, states nobody captured,
the rules behind a form. Labels and sentences a capture draws are copied from it, character for
character, in criteria and Assumptions alike; a state the capture does not draw reuses them, not a
paraphrase. Its sample data - names, quantities, dates - is not transcribed: the builder recreates
it with the capture open. Whether the running app should hold that sample data when the work is
handed over, so the user can open the captured states, is the user's call: ask it. A binding source
is also the measure of scope: build what it draws, minus what `## Differences from the source`
lists.

**A capture is a viewport, not the whole request.** Read each sentence of the request against the
captures. A column past the right edge, a row below the fold, a state no capture draws, a total the
request names and the frame cuts off - each is still the request's, and gets a criterion or a
question. For a screen, the criterion says what the user sees, not only what a response carries: a
value computed and sorted on but never shown is a requirement that went missing.

**Which references bind.** A capture, screenshot, design frame or live site that comes with the
request binds: it is the result the user expects, and the user does not have to say so. It is
`non-binding` only when the user called it inspiration - a mood board, "something like X", a
product cited for its feel - and its `Sources` line quotes them: `non-binding - user: "<their
words>"`. Something the source draws that the stored model cannot hold, such as a code or a field,
is a departure asked as above, never a reason to make the source non-binding. A binding source
lives in files: export each frame or state, open ones included, to
`.specs/features/<f>/captures/` with its structure beside it where the tool gives one, keep the link
in `Sources` with the date, and ask for the files as a `blocks` question when you cannot reach it.
Each capture's `Sources` line shows the image, `![<name>](captures/<file>)`, so whoever approves
the plan sees the screen beside the criteria.

**A screen with no reference gets outcomes, not layout.** Say what a user can see and do, never
where things sit. Layout is the builder's call, held to the design system.

**A backend criterion is a rule with examples under it.** One example is the strong case - the
boundary, or the input where an implementation fitted to the obvious sample breaks.

```
12. IF a coupon has expired THEN checkout SHALL reject it with 422 `coupon_expired`
    - expired yesterday → 422; expires today at 23:59 → accepted; no expiry date → accepted
```

**One run must settle it.** A percentile, an average or an uptime is a service target, not a
criterion. Split it: the behaviour is the criterion, the target goes to observability.

**A guarantee that something will not happen needs a mechanism.** "A retry does not create a second
subscription" points at the code that enforces it, or becomes a `Landing` door, and is a
`## Must never` row. Without one it is a question.

# What is being built

Three sections, each with a rule about what stays out: no component catalogue, no columns, no
types. Those are reversible and settled in the diff.

## Shape - the path and what it disturbs

Open with one sentence on what this reuses instead of duplicating, and one on where the rules that
change an outcome run: in a layer the repo's tests can reach, as its `AGENTS.md` or test docs say.
Filtering, searching, sorting and counting decide what a user sees, so they are rules too, not
presentation.

**The path.** One numbered line per hop: what enters, which module it crosses, what it hands on,
what persists or goes out at the end. Name only modules that exist today, marked `(exists)`, or
that a `Landing` door creates, marked `(door <n>)`; a new module with no door is `(new)` and its
placement is the builder's. `single module - <name>` is complete.

**The drawing.** The person approving the plan reads a drawing before a list, so draw the path in
mermaid: a `sequenceDiagram` for a request and its answer, a `flowchart` when it branches, fans out
or hands off to a queue or job. Number each arrow or node with its line in the list and name the
same modules in both: the list says what each hop decides, the drawing shows the shape. Both stay
true through the build.

**The data.** An `erDiagram` of the entities this reads or writes and how they relate, then one
line, `New: <entities and relations this adds>` (`New: nothing - read only` is complete). No
attribute blocks. `Data: none - <why>` when nothing stored is touched.

**States**, when an entity or a screen moves through states: a `stateDiagram-v2` with each
transition and what triggers it. A missing transition shows in a drawing where it hides in a list
of criteria. An entity the feature reads and does not own - a membership, an attendance, a
subscription a third party holds - is drawn too when the feature decides on its state: every state
its owner can put it in, the ones its API hides included, and the transitions someone else
triggers - removed, re-added, cancelled, refunded. With `## External behavior` rows and nothing that
moves through states, write `States: none - <why>`.

**Changes underneath.** A table of what already exists and changes: a domain term that changes
meaning (name who branches on it today), stored data (backfill, migrate on read, or nothing), and
each reader that will see the new data. Each reader named gets a criterion or an `Out of scope`
row. Code this changes that something else calls - a method, a class, a schema, a serializer, a job
argument - is a `shared code` row: grep for it and name every caller; each caller gets a criterion
or an `Out of scope` row. `none - nothing existing changes` is complete; a missing row is a grep
nobody ran.

When stored data changes, the `erDiagram` also carries the one-way constraints `Landing` settled.

## Surface - the signature

Only a route this adds or whose signature changes: route, what goes in, what comes out, statuses,
and who may call it. The statuses include what a caller who may not call gets (redirect, 401, 403
or 404); `Who: anyone` owes none. No criterion or check numbers here. `None - nothing consumed outside` is
complete.

## Landing - the one-way doors

A door is one-way when reversing it costs more than a refactor: a persisted schema, a contract
someone consumes, a new dependency, a backfill, a pattern the codebase does not have yet. Each row
gives the literal shape the next person copies and the alternative rejected, with the property
that disqualified it. Scope and rules with no persisted mechanism are not doors. Identifier width,
uniqueness, nullability over a populated table and a field a consumer binds to are doors.
`None - <why nothing here is one-way>` is complete. A door that reaches past this feature also goes
in `.specs/STATE.md` `## Decisions` ([memory.md](memory.md)).

# Before presenting

```bash
python3 <skill-dir>/scripts/validate_plan.py <feature>
```

It checks the structure. What it cannot see stays yours: a hop marked `(exists)` only stands if
that module already does what the criteria assign it. And read the request again, one sentence at a
time: each one lands on a criterion, an `Out of scope` row or an open question. A sentence that lands
nowhere is the requirement that goes missing, and nobody downstream reads the request again.

Every unresolved decision is a criterion, an `Assumptions` row, or an open question marked
`blocks`, `blocks go-live` or `open`.

## Break the plan

A plan with a write to a third-party system or a `Landing` door is broken before a human reads it.
The author cannot see the gap it left, and a reader who finds it after approval costs a build. Start
a fresh sub-agent - never yourself, never one from an earlier round - with the prompt below, filled
in and with nothing of your own added.

```
Try to BREAK a feature plan before it is built. Someone else wrote it for <repository path>
(branch <branch>); it is not approved. The user's request is quoted at the end
and the plan is .specs/features/<feature>/plan.md. Read both, and the code the plan names.

Find how, built exactly as written, it would harm a user, the business or a third party, or fail
to deliver what the request asks. Look hardest where plans go wrong:
- a third-party behaviour the plan relies on that nobody verified, or that changes in a state the
  plan did not consider;
- a harm `## Must never` does not list, or a mechanism there that does not hold on every path;
- running it: stopped mid-run, restarted, two copies at once, the first run's volume, a shared
  token or rate limit it can starve;
- a state of an external entity it reads that the plan does not draw - removed, re-added,
  cancelled, hidden from the API;
- existing code the plan changes and a caller it does not name.

Return ONLY findings that are critical (harm to a user, data lost, access granted or removed
wrongly, a one-way door shut the wrong way, the request not delivered) or high-impact (an outcome
the plan promises that fails in a plausible case). No nits, no style, no "add tests", no restating
the plan. For each: the scenario step by step, the line of the plan it breaks, and the evidence -
file:line, the doc, or what you ran. "Nothing critical" is a valid answer.

Change nothing: no file, no commit, no write to any external system. Reply with
`critical: <n>, high: <n>` on the first line, then the findings.

The user's request:
<verbatim>
```

Each critical finding changes the plan - a criterion, a `Must never` row, an `External behavior`
row, a door - or becomes a question for the user, asked with the others. A high one does the same,
or stays with your reason on the round's line. Run `validate_plan.py` again. Record the round as it
returns:

`**Break the plan:** round 1 - critical: 2 - <one line each, and what changed>`

One round, before the questions are asked, and no second: what it leaves open goes to the user as
a question with the others. With no third-party write and no door, the line reads
`not owed - <why>`. This is not the review: that still runs after the build ([review.md](review.md)).

## Template: `.specs/features/<feature>/plan.md`

`````markdown
# <Feature>

**Approved**: <written when the user approves - YYYY-MM-DD - user: "their words">
**Settled mid-build:** <what the user settles while it is built, in their words; or none>
**Review:** <written when the review is sent - sent at <HEAD short hash>>
**Fix pass:** <written when the fix pass is made - done - <what it changed, or no changes needed>>
**Break the plan:** <written when the round returns - round 1 - critical: <n> - <what changed>; or not owed - <why>>

## Problem

<Present tense, no solution inside it: what is true today, who pays, what it costs. The evidence
the source gives, or a note that it gives none.>

<What is different for a user when this ships.>

## Must never

| Must never | Mechanism | Criterion |
| --- | --- | --- |
| <the harm, as the user said it> | <the guard, constraint or door that prevents it> | <the criterion whose test fails without it> |

## External behavior

| Behavior relied on | Verified by | Evidence |
| --- | --- | --- |
| <what the third party does, or does besides> | <sandbox / test / production / doc / vendor support / assumed> | <date and what ran, the link, the quote; for assumed, the `blocks` question> |

## Shape

<One sentence: what this reuses instead of duplicating. One sentence: where the rules that change an outcome run, in a layer the repo's tests reach.>

1. <what enters> -> `<Module>` (exists) - <what it does, what it hands on>
2. `<Module>` (exists) - <what it does>, persists `<Entity>` (door <n>)
3. `<Module>` (new) - <what it decides>
4. out: `<response>`, and `<Module>` (exists) reads `<field>` on the next request

```mermaid
sequenceDiagram
    participant A as <Module>
    participant B as <Module>
    A->>B: 1. <what enters>
    B-->>A: 4. <response>
```

**Changes underneath**

| Front | What changes |
| --- | --- |
| domain | `<Name>` meant <x>, now means <y> - <who branches on it today> |
| stored data | <backfill now / migrate on read / nothing to migrate> |
| readers | <screen or job that shows the new data> |
| shared code | `<Class#method>` <what changes> - callers (`grep -rn "<name>"`): `<path>`, `<path>`; or none - nothing existing changes |

```mermaid
erDiagram
    <Entity> ||--o{ <Entity> : "<verb>"
```

New: <entities and relations this adds, or `nothing - read only`>. One-way constraints: <field>
unique (door <n>).

<When something moves through states, an external entity it reads included; or `States: none - <why>`:>

```mermaid
stateDiagram-v2
    [*] --> <State>
    <State> --> <State> : <trigger>
```

## Surface

| Route | In | Out | Status | Who |
| --- | --- | --- | --- | --- |
| `<METHOD> /<path>` | `<field>` | `<field>` | `<code>`, `<denied code>` | <who may call it, and what the rest get> |

## Landing

| One-way door | Literal shape | Alternative rejected |
| --- | --- | --- |
| <what> | <the literal a reader copies> | <the option, and the property that disqualified it> |

- Nothing else in this change is hard to reverse

## Criteria

### S1: <slice - the outcome someone can watch> (P1)

<One line: the outcome this slice is.>

**Acceptance Criteria**

1. WHEN <trigger> THEN the system SHALL <observable outcome with the concrete value>
2. IF <undesired condition> THEN the system SHALL <response>
   - <example> → <result>; <the strong case> → <result>
3. WHEN <who> <gesture> THEN the system SHALL show the <screen> in `<image>` (`<structural capture>`)

## Out of scope

| Excluded | Why |
| --- | --- |
| <capability> | <reason> |

## Assumptions

| Assumption | Chosen default | Rationale | Confirmed? |
| --- | --- | --- | --- |
| <ambiguity> | <what we will do> | <why> | y/n |

**Open questions:** none - all resolved or logged above.

## Answers

- Q<n> <the question, in a few words>: "<the user's reply, verbatim>" - criteria <n, n> / AD-NNN / Assumptions / Out of scope
<or: none - no question was asked>

## Differences from the source

<Only when a source binds. Every way the product departs from what the source draws, each one
approved by the user.>

| Source shows | Product shows | Confirmed |
| --- | --- | --- |
| <label, control or state in the capture> | <what the product shows instead, or nothing> | y - <AD-NNN holding the question and the answer> |

<Or:> None - the product matches the source

## Sources

- <ticket or binding design> - binding - <what it settles>
- ![<capture>](captures/<capture>.png) - binding - <what it draws>
- <what the user called inspiration> - non-binding - user: "<their words>" - <what it informs>
`````

## Then confirm

Present the plan and stop: this is the one default stop for a human. Name `Shape`, any `Landing`
door with a live alternative, anything that changes existing data or a consumed contract, and what
the break round changed.
Answers to your questions are not the approval: they change the plan, so write them in, present
the plan that holds them, and stop again - do not offer to start building once they arrive, and
never quote them on the `**Approved**:` line. When the user approves, write
`**Approved**: <YYYY-MM-DD> - user: "<their words>"` under the title; one the user did not give is
never written, and nothing is built without it. Then continue into [build.md](build.md).

## Notes

- A slice is an outcome a user can watch, cut through every layer it needs - route, rule, tests,
  screen - and the build takes them in order, each committed. "The API" or "the screen" is a
  layer, not a slice. S1 is the thinnest path a user can walk end to end; the next slices widen it.
  P1 is a vertical slice too. Priority orders the work and never scales down the review.
- A criterion you cannot imagine a test for is not ready.
