---
name: writing-plans
description: Use when you have a spec or requirements for a multi-step task, before touching code
---

# Writing Plans

## Overview

Write comprehensive implementation plans assuming the engineer has zero context for our codebase and questionable taste. Document everything they need to know: which files to touch for each task, code, testing, docs they might need to check, how to test it. Give them the whole plan as bite-sized tasks. DRY. YAGNI. TDD. Frequent commits.

Assume they are a skilled developer, but know almost nothing about our toolset or problem domain. Assume they don't know good test design very well.

**Announce at start:** "I'm using the writing-plans skill to create the implementation plan."

**Context:** If working in an isolated worktree, it should have been created via the `using-git-worktrees` skill at execution time.

**Save plans to:** `research-wiki/plans/YYYY-MM-DD-<feature-name>.md`
- (User preferences for plan location override this default)

## Scope Check

If the spec covers multiple independent subsystems, it should have been broken into sub-project specs during brainstorming. If it wasn't, suggest breaking this into separate plans — one per subsystem. Each plan should produce working, testable software on its own.

## File Structure

**代码检索（graphify）— 开始前先做：** 在映射文件结构、定位改动点之前，先跑 `/graphify . --update` 刷新知识图谱，再优先用 `/graphify query "<问题>"` / `graphify path` / `graphify explain` / `graphify affected` 看清现有架构与依赖，而非盲读文件。若 `graphify-out/graph.json` 不存在，提示人类先手动 `/graphify .`；未建图前降级常规文件阅读、不阻断。

Before defining tasks, map out which files will be created or modified and what each one is responsible for. This is where decomposition decisions get locked in.

- Design units with clear boundaries and well-defined interfaces. Each file should have one clear responsibility.
- You reason best about code you can hold in context at once, and your edits are more reliable when files are focused. Prefer smaller, focused files over large ones that do too much.
- Files that change together should live together. Split by responsibility, not by technical layer.
- In existing codebases, follow established patterns. If the codebase uses large files, don't unilaterally restructure - but if a file you're modifying has grown unwieldy, including a split in the plan is reasonable.

This structure informs the task decomposition. Each task should produce self-contained changes that make sense independently.

## Bite-Sized Task Granularity

**Each step is one action (2-5 minutes):**
- "Write the failing test" - step
- "Run it to make sure it fails" - step
- "Implement the minimal code to make the test pass" - step
- "Run the tests and make sure they pass" - step
- "Commit" - step

## Plan Document Header

**Every plan MUST start with this header:**

```markdown
# [Feature Name] Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use subagent-driven-development to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** [One sentence describing what this builds]

**Architecture:** [2-3 sentences about approach]

**Tech Stack:** [Key technologies/libraries]

---
```

## Task Structure

````markdown
### Task N: [Component Name]

**Files:**
- Create: `exact/path/to/file.py`
- Modify: `exact/path/to/existing.py:123-145`
- Test: `tests/exact/path/to/test.py`

- [ ] **Step 1: Write the failing test**

```python
def test_specific_behavior():
    result = function(input)
    assert result == expected
```

- [ ] **Step 2: Run test to verify it fails**

Run: `pytest tests/path/test.py::test_name -v`
Expected: FAIL with "function not defined"

- [ ] **Step 3: Write minimal implementation**

```python
def function(input):
    return expected
```

- [ ] **Step 4: Run test to verify it passes**

Run: `pytest tests/path/test.py::test_name -v`
Expected: PASS

- [ ] **Step 5: Commit**

```bash
git add tests/path/test.py src/path/file.py
git commit -m "feat: add specific feature"
```
````

## No Placeholders

Every step must contain the actual content an engineer needs. These are **plan failures** — never write them:
- "TBD", "TODO", "implement later", "fill in details"
- "Add appropriate error handling" / "add validation" / "handle edge cases"
- "Write tests for the above" (without actual test code)
- "Similar to Task N" (repeat the code — the engineer may be reading tasks out of order)
- Steps that describe what to do without showing how (code blocks required for code steps)
- References to types, functions, or methods not defined in any task

## Remember
- Exact file paths always
- Complete code in every step — if a step changes code, show the code
- Exact commands with expected output
- DRY, YAGNI, TDD, frequent commits

## Self-Review

After writing the complete plan, look at the spec with fresh eyes and check the plan against it. This is a checklist you run yourself — not a subagent dispatch.

**1. Spec coverage:** Skim each section/requirement in the spec. Can you point to a task that implements it? List any gaps.

**2. Placeholder scan:** Search your plan for red flags — any of the patterns from the "No Placeholders" section above. Fix them.

**3. Type consistency:** Do the types, method signatures, and property names you used in later tasks match what you defined in earlier tasks? A function called `clearLayers()` in Task 3 but `clearFullLayers()` in Task 7 is a bug.

If you find issues, fix them inline. No need to re-review — just fix and move on. If you find a spec requirement with no task, add the task.

## 核心算法保真校验

计划编写完成、Self-Review 通过后，**必须**执行以下校验：

对照 `research-wiki/ARCHITECTURE.md §6 核心算法保真清单` 中列出的 13 项关键算法，逐一检查：

1. 计划中是否涉及该算法的迁移/重写？
2. 若涉及，计划中的实现是否与参考代码的核心逻辑一致？
3. 是否存在简化、省略、或改变算法行为的步骤？

**参考代码路径**：

| 算法 | 参考文件 |
|------|---------|
| L2 轴心建树策略 | `reference/video_tree_trm/video_tree_builder.py` |
| VLM 批量帧描述 + JSON fallback | `reference/video_tree_trm/video_tree_builder.py` |
| 断点续跑机制 | `reference/video_tree_trm/video_tree_builder.py` |
| RecursiveRetriever | `reference/docs/architecture.md §5` |
| CE-Gate e-process | `/home/iomgaa/Projects/Video-Tree-TRM4/core/harness/eprocess.py` |
| 信息阶梯 | `/home/iomgaa/Projects/Video-Tree-TRM4/core/harness/gate_ladder.py` |
| 块顺序验证 | `/home/iomgaa/Projects/Video-Tree-TRM4/core/harness/validate.py` |
| 诊断瀑布 | `/home/iomgaa/Projects/Video-Tree-TRM4/core/harness/diagnose.py` |
| 进化 patch 引擎 | `/home/iomgaa/Projects/Video-Tree-TRM4/core/harness/evolve.py` + `patch.py` |
| Mini-batch 构建 | `/home/iomgaa/Projects/Video-Tree-TRM4/core/harness/batching.py` |
| Agent Loop | `/home/iomgaa/Projects/Video-Tree-TRM4/core/loop.py` |
| 树环境语义搜索 | `/home/iomgaa/Projects/Video-Tree-TRM4/core/tree/environment.py` |
| 训练循环编排 | `/home/iomgaa/Projects/Video-Tree-TRM4/core/harness/runner.py` |

**若发现任何简化**：在计划中明确标注该步骤需要逐行比对参考代码，并添加"保真校验"检查点。

**若计划不涉及任何核心算法**：记录"本计划不涉及核心算法迁移，保真校验不适用"即可。

## Codex Plan Review

After self-review, get an independent second opinion before execution:

> 用 `/codex:rescue --fresh --wait` 把"设计 + 计划全文"交 Codex 审查（只读），核对设计覆盖、每步可执行性（路径/锚点/命令/顺序）、TDD 红绿正确性。

Codex 的意见**仅供参考**：逐条核验后，Claude 自行判断采纳与否并就地修订。无需人类批准。

## Execution Handoff

After self-review and Codex plan review pass, proceed **directly** to execution with subagent-driven-development. Do not pause to ask the user which execution mode to use.

- **REQUIRED SUB-SKILL:** Use subagent-driven-development

## Wiki Integration

**Precondition**: `research-wiki/` directory exists (skip this section entirely if it does not).

**Trigger**: When a plan is being written or when the plan is approved.

**Output path**: Plans are saved to `research-wiki/plans/` instead of `docs/superpowers/plans/`.

**Steps**:
1. Run `.claude/tools/research_wiki.py add_entity research-wiki/ --type plan --id <slug> --title "<plan title>"` to create the plan entity
2. Append the implementation steps, milestones, risks, and acceptance criteria to the generated page
3. If the plan implements a design, run `.claude/tools/research_wiki.py add_edge research-wiki/ --from "plan:<id>" --to "design:<id>" --type implements --evidence "..."`
4. Run `.claude/tools/research_wiki.py rebuild_index research-wiki/`
