# AI decomposition

`tkt plan begin` plans an epic and stories from docs; **you**, the coding agent, do the AI work, one step at a time.

`tkt` checks, gates and writes only what the user approved; it never calls an LLM.

```console
$ tkt plan begin export-spec -g payments --out step.json
```

Do each step in `step.json`, then `tkt plan submit <dcmp> --step <id> FILE --out step.json`. Kinds:

- `llm`: do it to the `schema`; `question`: see [questions](/en/docs/guides/plan/questions.md#plan-questions).
- `fix`: fix every `errors[]` item, resubmit the whole step under its new id (`ERR_DECOMPOSITION_SUBMIT_INVALID`; 3rd invalid: `failed`).
- `ready`: show the preview (tickets, assumptions, excluded and shared sections, digest); `tkt plan apply <dcmp> --approve <digest>` only after approval.
- `stopped`: a source or the method changed (`ERR_DECOMPOSITION_STALE`): only `tkt plan discard <dcmp>`.

Lost: `tkt plan list`, then `tkt plan step <dcmp> --out step.json`. A doc costs about 2 agent runs and 15k-28k output tokens; doc text per decomposition is at most 400 KB.

**Known limit:** Re-planning a doc that already has tickets (`--replan`) writes new ones beside them (#168)

## Details

- `begin`: doc ids or globs, `-g G`, `--skip GLOB`, `--method` ([methods](/en/docs/guides/plan/methods.md#plan-methods)); the group check runs first. Refused: too much text (`MAX_DOC_BYTES`, `ERR_PAYLOAD_TOO_LARGE`: begin per doc), no H2s (`ERR_BODY_SECTION_MISSING`), unknown method (`ERR_NOT_FOUND`), planned or open docs without `--replan` (`ERR_DECOMPOSITION_APPROVAL`). Creates `dcmp-<ULID>`; `from-docs` then skips the doc.
- Always `--out` (`begin`, `step`, `submit`): steps hold the whole doc and method; stdout is `step slice-1 llm → step.json`. Wrong step: `ERR_DECOMPOSITION_STEP_MISMATCH`, no strike.
- Stages: `slice` per doc (`aitodo.plan.slice.v1`: each H2, or a split H2's H3s, once as source or excluded; ≤ 20 units), `write` by 4 (`aitodo.plan.write.v1`: ≤ 2 sentences per section, Given-When-Then, body < 600 tokens, no 20-word copy, refs `{ref, anchor?, lineRange?, why}`, a repo `lineRange` `N-M` stored as `L<N>-L<M>`), `check` (strict gate), repairs, round 2. Exact rules come in the step.
- `tkt plan show <dcmp>`: preview (with `excluded[]`, `shared[]`) or progress; `--out` (ready) writes an import file bypassing gate and approval. `apply` (ready; bad digest, or assumptions without `--accept-assumptions`: `ERR_DECOMPOSITION_APPROVAL`) regates (`--force`), then imports all or nothing: epic per doc, `feature` per story, `task`/`bug`/`chore` per sibling, provenance refs with `methodRev` (`story-writer@1.10.0#<hash12>`), `note: method` refs, a `Planned by dcmp-…` note. `--approve`, `--accept-assumptions`, `--force` only on the user's say; `discard` deletes only the state.
- State `.aitodo/decompositions/` (ignored); on a server store forwarded, any writer continues it. `tkt plan list`: `createdBy`, `lastBy`, `updatedAt`, age. Names: token name; repo store: `user.name`, else `$USER`, else `unknown`. `tkt store move` refuses while one is open (`ERR_CONFIRM_REQUIRED`; `--force-decompositions` drops them).
- Measured once: 17-67 KB doc, 2.5-4.5 minutes, about $2-3.5 API-equivalent (subscription: usage limits). Permission: `"ask": ["Bash(tkt plan apply:*)"]`.

- A step file (`tkt plan step --out`): `{decomposition, step: {id, stage, kind, attempt, instructions, method, inputs, schema, submit}}`; `fix` adds `errors[] {unit?, path, rule, evidence}`, `previous`; `failed` `{reason, lastErrors, next}`.
- `tkt plan show --json`: `submissions[]` (`step`, `by`, `at`, `valid`).

## Commands on this page

- [`tkt plan begin`](/en/docs/reference/commands/plan.md#tkt-plan-begin): Begin an AI decomposition of docs into an epic and stories, driven by your coding agent
- [`tkt plan submit`](/en/docs/reference/commands/plan.md#tkt-plan-submit): Submit a step's result; prints the next step, or a fix step with the errors (exit 1)
- [`tkt plan step`](/en/docs/reference/commands/plan.md#tkt-plan-step): Print the current step again, in full with the method text (after a lost context)
- [`tkt plan list`](/en/docs/reference/commands/plan.md#tkt-plan-list): List the open decompositions: id, docs, state (stale when a source changed), step, age, and who began it and acted last (on a server store any member can continue one)
- [`tkt plan show`](/en/docs/reference/commands/plan.md#tkt-plan-show): Show the preview of a ready decomposition (tickets, gate, assumptions, doc gaps, the approval digest), else its progress; also when stopped or stale (read-only)
- [`tkt plan apply`](/en/docs/reference/commands/plan.md#tkt-plan-apply): Write a ready decomposition the user approved: the strict gate runs again on the current store, then the all-or-nothing import; the decomposition's state is deleted
- [`tkt plan discard`](/en/docs/reference/commands/plan.md#tkt-plan-discard): Discard a decomposition (its state only; nothing was written to the store)

## Related

- Guide: [Questions in a decomposition](/en/docs/guides/plan/questions.md): Put every question of a question step to the user verbatim, with its options and assumption, then tkt plan answer \<dcmp\> --answers - --answered-by user --out…
- Guide: [Decomposition methods](/en/docs/guides/plan/methods.md): How docs are cut is swappable: a method folder (SKILL.md, optionally decomposer.yaml) is prompt text that tkt serves in the steps, never runs; the ticket…
- Error code · 422: [`ERR_DECOMPOSITION_SUBMIT_INVALID`](/en/docs/reference/errors.md#ERR_DECOMPOSITION_SUBMIT_INVALID): A decomposition step's submission fails validation; a fix step follows
- Error code · 409: [`ERR_DECOMPOSITION_STALE`](/en/docs/reference/errors.md#ERR_DECOMPOSITION_STALE): A source doc or the method changed since plan begin; only discard works
- Error code · 409: [`ERR_DECOMPOSITION_STEP_MISMATCH`](/en/docs/reference/errors.md#ERR_DECOMPOSITION_STEP_MISMATCH): Not the current step, or a command the decomposition's state does not take
- Error code · 409: [`ERR_DECOMPOSITION_APPROVAL`](/en/docs/reference/errors.md#ERR_DECOMPOSITION_APPROVAL): apply without the preview's digest or the accepted assumptions; begin without --replan
