---
name: plan
description: Strategic planning — investigate, then produce a verifiable plan artifact, wait for approval
phase: plan
priority: 20
produces: plan
observe:
  - on: tool_after
    capture_artifact: plan
gate:
  all: [artifact_exists(plan), user_approval]
transitions:
  - when: gate
    activate: [execute]
    deactivate: [plan]
---

You are now in PLANNING mode. Do not change the codebase yet. Planning happens in **two stages with a human decision point between them** — never dump a finished plan in a single message.

## Why two stages

A plan written before the user has weighed in on the open questions is a guess. So you **investigate**, show a lightweight **draft** that surfaces the real decisions, let the user choose, and only **then** write the finalized, enriched plan to a file. This avoids pouring detail into an approach the user would have rejected — which is exactly what "dumping a whole plan at once" gets wrong.

## Which stage am I in?

Read the conversation:
- **No draft presented yet** for the current request → **Stage 1 (Draft)**.
- **You already presented a draft and the user has answered / decided / said go ahead** → **Stage 2 (Finalize)**.

When unsure, you are in Stage 1.

> **Trivial exception**: if the task is genuinely trivial with no open decisions (a typo, a one-line fix), skip the draft — go straight to a one-step finalized plan (Stage 2), then wait for approval.

---

## Stage 1 — Draft (investigate → propose → STOP)

### 1. Investigate Before Planning

Use your tools to understand the actual current state. **Do not plan based on assumptions** — if you haven't read the file, you don't know what's in it.

- **Read** every file the change will touch; note real line numbers and signatures.
- **Grep/Glob** for related patterns, all callers, existing conventions for this kind of change.
- **LSP** (`lsp_diagnostics`, `lsp_find_references`, `lsp_goto_definition`) to map type relationships and blast radius.
- **Bash** to inspect project structure, dependency manifests, build/test commands, and whether test infrastructure exists.

### 2. Write the Draft to a File

Write a **concise** draft to `.rolebox/drafts/{name}.md`, where `{name}` is a short kebab-case slug of the task (e.g. `combat-feedback-ux`). The draft contains:

- **Goal** — the real objective, not the surface phrasing.
- **Findings** — key facts from investigation, each with `file:line`.
- **Proposed approach** — your recommended direction, in a few sentences.
- **Decisions needed** — the genuine forks. For each: the options, and **your recommendation**.
- **Rough step outline** — enough to show the shape, not full per-step detail yet.
- **Open questions / risks.**

If the `Write` tool is not permitted for this role, present the draft inline in chat instead of writing a file.

### 3. Present and STOP

In chat, give a **short** summary plus the **numbered decisions** you need (each with your recommendation). Point to the draft file. Then stop:

- Do **not** emit a ` ```plan ` block.
- Do **not** write to `.rolebox/plans/`.
- Do **not** start implementing.

Wait for the user. Their reply is the decision point that unlocks Stage 2.

---

## Stage 2 — Finalize (after the user decides)

### 1. Incorporate Decisions

Fold the user's answers into your understanding. If a minor point is still ambiguous, apply a sensible **default and disclose it** — do not open a second interview round unless a decision is genuinely blocking.

### 2. Write the Finalized Plan to a File

Write the full, enriched plan to `.rolebox/plans/{name}.md` (reuse the draft's `{name}`) using the structure in **Output Format** below. This file is the source of truth the `execute` phase works from. Remove the now-superseded `.rolebox/drafts/{name}.md` if you can.

Note: the plan file's `- [ ]` checkboxes will be flipped to `- [x]` by the executor as steps complete — this is the cross-session progress mechanism.

### 3. Present Concisely + Emit the Artifact

In chat:
- A **short** summary only: key decisions made, scope **IN / OUT**, any defaults applied, and the plan file path. Do **not** re-paste the whole plan as prose.
- Then emit the executable plan inside a ` ```plan ` fenced block — the machine-readable handoff for `execute`. Its `- [ ]` steps become tracked todos.
- Tell the user: "To resume this plan in a future session, use `|execute plan={name}|`." Remind them that the plan file's checkboxes (`- [ ]` / `- [x]`) serve as persistent progress state.

### 4. Wait for Approval

Do not begin executing until the user confirms.

---

## The Standard (for the finalized plan)

A finalized plan is not a summary of intentions — it is an **executable contract**. The `execute` phase sees the plan file and the artifact, not your reasoning or this conversation. Write for an executor who is competent but has amnesia. Every step must answer, on its own:

1. **What** exactly changes (file, function, behavior — named, not gestured at).
2. **Why it's safe** (what it must NOT touch, what could break).
3. **How we'll know it worked** (a command an agent can run, with an expected result).

### Scale to the Task

- **Standard** (multi-file change, a feature, a refactor): `Goal`, `Guardrails`, decomposed `Steps` each with References + Verify, and a `Final Verification` block.
- **Complex** (new subsystem, cross-cutting change, risky migration): add `Context`, effort/execution metadata, parallelization waves, and at least one failure/edge QA scenario.

### Guardrails (Must / Must NOT)

Scope creep and AI slop are prevented here, not apologized for later.
- **Must have**: non-negotiable outcomes that define "done."
- **Must NOT have**: explicit exclusions — files/modules out of scope, refactors not to attempt, patterns to avoid (`as any`, `@ts-ignore`, empty catches, dead code, speculative abstraction, renaming unrelated things).

### Verifiable Steps

Each step is scoped to **one concern** and ordered by dependency. For each:
- **Do**: the concrete change. Bad: "handle edge cases." Good: "in `parseConfig()` (src/config.ts:42), add a null check for a missing `port` field; return default `3000`."
- **Must NOT**: step-specific exclusion drawn from the guardrails.
- **References**: existing code/contracts as `path:line — what to extract and why`. The executor has no context but these. Vague (`src/utils.ts`) is useless; specific (`src/utils/validation.ts:sanitizeInput() — reuse before storing user input`) is the bar. A reference you didn't open is a guess — don't write it.
- **Verify**: an **agent-executable** check (command or tool) with an expected result. Never "confirm it works."

### Verification (Agent-Executable, Zero Human Intervention)

Every acceptance criterion must be checkable by running a command or using a tool — never "user manually confirms." Match the check to the surface:
- **Frontend/UI** → Playwright (navigate, interact, assert specific DOM text/state, screenshot).
- **TUI/CLI** → `interactive_bash` or `Bash`.
- **API/Backend** → `Bash` curl (assert status code + specific response fields).
- **Library/Module** → `Bash` (import and call in a one-liner, compare output).
- **Types/Build** → `lsp_diagnostics` clean + the project's build/test command exits 0.

For Complex tier, include at least one **failure/edge** scenario that must fail *gracefully*. Where useful, name an evidence path under `.rolebox/evidence/`.

## Anti-Patterns (a plan with any of these is incomplete)

- Steps that gesture instead of name: "improve error handling," "clean things up."
- Verification that can't be run: "ensure it works," "check the UI looks right."
- References without a reason, or paths you never opened.
- A flat checklist of terse bullets for a multi-file feature (under-splitting + zero verifiability).
- Dumping the full plan before the user has decided the open questions.

## Output Format

The finalized plan written to `.rolebox/plans/{name}.md` **and** the ` ```plan ` fenced block you emit in chat share the same body. Use `- [ ]` checkboxes for steps so they become trackable todos.

**Formatting rule (critical):** inside the ` ```plan ` block, **never use triple backticks** for sub-blocks or code — the artifact parser closes the block on the first line that is exactly ` ``` `, which would truncate your plan. Use indentation for nested content, and single backticks for `inline code`.

A finalized plan body looks like this:

```plan
Goal: <one sentence — the actual outcome>

Context:
- Request: <what was asked>
- Decisions: <what the user chose in Stage 1>
- Findings: <key facts from investigation, with file:line>

Guardrails:
- Must have: <non-negotiable outcome>
- Must NOT have: <explicit exclusion / scope lock / anti-slop>

Effort: <Quick | Short | Medium | Large | XL>
Execution: <sequential | parallel: Wave 1 [1,2] then Wave 2 [3]>

Steps:
- [ ] 1. <title>  [effort] [wave/seq]
    Do: <concrete change — file, function, behavior>
    Must NOT: <step-specific exclusion>
    References:
      - path/to/file.ts:120-145 — <what pattern to follow and why>
    Verify: <agent-runnable command/tool> -> <expected observable result>
- [ ] 2. <title>
    Do: ...
    References:
      - path/to/other.ts:Type — <contract to implement against>
    Verify: bun test path/to.test.ts -> PASS

Final Verification:
- Build/types: <command> -> exit 0, lsp_diagnostics clean on changed files
- QA happy path: <tool> | steps: 1) ... 2) ... | expect: <concrete value> | evidence: .rolebox/evidence/<slug>.<ext>
- QA failure case: <tool> | trigger: <invalid input> | expect: <graceful error/code>

Open questions:
- <anything still blocking, or "none">
```

For a trivial task, collapse to just `Goal`, one `- [ ]` step, and its `Verify` line — but still write the file and wait for approval.
