一键导入
mikado-method
Systematic approach for complex refactoring using dependency graphs, leaf-first execution, and revert-on-failure to keep the codebase always shippable
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Systematic approach for complex refactoring using dependency graphs, leaf-first execution, and revert-on-failure to keep the codebase always shippable
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | mikado-method |
| description | Systematic approach for complex refactoring using dependency graphs, leaf-first execution, and revert-on-failure to keep the codebase always shippable |
| license | MIT |
| compatibility | claude-code, opencode, agents-md |
| metadata | {"version":"1.2.0","audience":"developer","workflow":"refactoring"} |
Gate 1 — Goal. Before any file change, present the Goal reformulated in business-value terms:
"Proposed Goal: [goal]" — A) Confirm B) Modify
Do not create .mikado.md, do not start naive attempts, until the user selects A.
Gate 2 — Patterns (per exploration cycle). After each naive attempt reveals new failures, identify the refactoring pattern that the new nodes call for (e.g. Strategy, Repository, Registry) and propose it before adding the node to the tree:
"This failure suggests the [Pattern] pattern. A) Apply B) Use a different approach"
Present one pattern per cycle, not all upfront. If the user selects B, adjust and re-ask before updating the tree.
Gate 3 — Delegation level. After exploration is complete and the tree is stable, present the
full tree, run validate-mikado.sh (must exit 0 — its Pass 8 output lists all true leaves,
golden master gates included with [GOLDEN MASTER GATE] tag), then ask:
"Exploration complete — N true leaves. Delegation level? (1 / 2 / 3)"
Do not write any production code until the user answers.
During exploration the only allowed write is the .mikado.md file.
Never create source files. Never modify production code.
Revert every naive attempt with git checkout -- . (not git stash, not git clean -fd).
The .mikado.md is committed immediately after creation so git checkout -- . cannot delete it.
Every later tree update must also be committed before reverting — once tracked, uncommitted
.mikado.md edits are discarded by git checkout -- . just like code.
After every revert: git status must show zero modified or untracked production files.
When a leaf is implemented and tests are green:
[x] in .mikado.md before staging.validate-mikado.sh — must exit 0.git add <implementation files> <test files> docs/mikado/<goal>.mikado.mdgit commit -m "feat: [NodeDescription]"The [x] mark and the code are always in the same commit.
Every child node is a prerequisite of its parent — it must be implemented before the parent. Read: "To complete [Parent], [Child] must exist first." The deeper the node, the earlier it is implemented. Leaves (deepest) always go first.
discovered-by invariant: the SHA of a child's discovery commit must be equal to or a descendant
of the SHA of the parent's discovery commit. A child discovered before its parent is a graph error.
Trap: If you think "I need X before Y", then X is the child (deeper), Y is the parent.
Run bash {{SKILL_DIR}}/validate-mikado.sh docs/mikado/<goal>.mikado.md:
Exit ≠ 0 = stop. Fix the graph before continuing.
Use Mikado when: refactoring spans multiple modules, failures cascade, dependencies are unclear, or you need to interrupt and resume safely.
Use simple refactoring when: the change fits in one file, dependencies are obvious, and one commit suffices.
Frame goals in business value, not technical tasks.
| Avoid | Prefer |
|---|---|
| "Inject a notification gateway into BillingService" | "Invoices can be issued without the billing logic knowing how customers are notified, so billing tests run without an SMTP server" |
| "Replace the hardcoded SMTP client with an interface" | "A customer notification failure never blocks invoice issuance" |
Ask once, after the stable tree and validation gate (Rule 1 Gate 3).
| Level | Who implements | Agent behaviour |
|---|---|---|
| 1 | Human | Agent lists true leaves + offers: A) human implements, agent reviews on return · B) agent writes a guide per leaf (no code). Stops here. |
| 2 | Agent, supervised | Leaf-by-leaf TDD. Pauses after each green commit: "Leaf done: [N]. Reply ok to continue." Never moves to next leaf without explicit approval. If the developer requests a change, update the tree, commit it, re-validate, then wait for "ok". |
| 3 | Agent, autonomous | Full graph without interruption. Single review request when root is [x]. |
Experiment in real files → failure → capture prereqs → update tree → commit tree → validate
↑ │
└─────────────────── git checkout -- . ───────────────────────────────────────┘
Before any naive attempt, scan for references to concepts being introduced:
grep -r "<new_concept>" tests/ src/
Hidden fixtures that use the new concept as an "invalid" stand-in are guaranteed regressions. Add them to the tree as nodes immediately.
Golden master — mandatory gate, resolved BEFORE step 1 below (initial commit):
validate-mikado.sh errors if neither a {GM} node nor a [no-golden-master: <reason>]
declaration is present. You must declare one before the first commit.
npx jest --coverage --coverageReporters=text-summary 2>/dev/null | grep -E "All files|<module>"
# Python: python -m pytest --cov=<module> --cov-report=term-missing 2>/dev/null | tail -5
Insufficient = no test file for the module, OR line coverage < 80%, OR no test framework configured.[ ] {GM} Golden master on <module> to the tree immediately.
jest, approvaltests, vitest).{GM}, and they are the true leaves.{GM} is a child of the first refacto node that touches the under-tested module.
Every other refacto node targeting that same module must declare requires: {GM}.[no-golden-master: coverage X% on <module>] as a standalone line anywhere in the .mikado.md
file (outside the ```text fence). This declares the skip explicitly.validate-mikado.sh — must exit 0.[GOLDEN MASTER GATE].
If {GM} is absent from that list when it has no pending children — stop and fix before proceeding.docs/mikado/<goal>.mikado.md with root only (graph wrapped in a ```text fence —
see Dependency Tree Format).git add docs/mikado/<goal>.mikado.md && git commit -m "mikado-graph: initial graph for <goal> (root only)"
— commit immediately so git checkout -- . cannot delete the file.git rev-parse HEAD — this is discovered-by for all nodes this cycle./tmp).[discovered-by: <sha>] and [parent-error: <file:line:msg>].git add docs/mikado/<goal>.mikado.md && git commit -m "mikado-graph: [target] requires [prereq] in file:line"
— commit the tree before reverting: .mikado.md is tracked, so git checkout -- . would
discard uncommitted node additions along with the code.git checkout -- . — revert all code. Never git stash.git status — confirm zero modified production files.bash {{SKILL_DIR}}/validate-mikado.sh docs/mikado/<goal>.mikado.md — must exit 0.Termination: every apparent leaf attempted, no new nodes, tree stable, git status clean.
git checkout -- ., add the regression as a new child node, commit tree, validate, fix first.[x] in .mikado.md before staging (Rule 3).validate-mikado.sh — must exit 0.git add <files> docs/mikado/<goal>.mikado.md && git commit -m "feat: [NodeDescription]"[x].Late discovery: if implementing a leaf reveals a new dependency, revert immediately, add the dependency as a child, commit the tree, validate, then implement the new node first.
Location: docs/mikado/<goal-name>.mikado.md
Inside the .mikado.md file, wrap the graph in a ```text fenced block. Without the fence,
GitHub's Markdown preview merges consecutive lines into one paragraph and the │ rails — the
tree structure — are lost. The validator parses line by line and ignores fence and prose lines.
[ ] Goal: [business-value statement] ← root: no rail, no {Nid}
│ [ ] {N1} [Change description] (file:line) ← direct dependency of goal (depth 1)
│ [discovered-by: <sha>]
│ [parent-error: <file:line: error message>]
│ │ [ ] {N2} [Prerequisite] (file:line) ← prerequisite of N1, implemented first (depth 2)
│ │ requires: {N3}, {N4}
│ │ [discovered-by: <sha>]
│ │ [parent-error: <file:line: error message>]
│ rails — one rail per nesting level, no list bullet.
Annotation lines keep their node's rails. State markers: [ ] pending, [x] done.{Nid} — unique short identifier; used by requires: to express shared prerequisites (DAG).[discovered-by: <sha>] — HEAD SHA captured at the start of the cycle (step 3 above).[parent-error: ...] — exact compiler/test error that made this node necessary.requires: {N3}, {N4} — optional cross-references when two parents share a prerequisite.validate-mikado.sh.| Event | Format |
|---|---|
| Initial graph | mikado-graph: initial graph for <goal> (root only) |
| New dependency | mikado-graph: [Node] requires [Prereq] in file:line |
| False leaf | mikado-graph: false leaf - [Node] blocked by [Dep] |
| Leaf implemented | feat: [NodeDescription] |
See EXAMPLE.md for complete worked example (fil rouge, Java/TS/Python TDD cycles, team workflow).
| Situation | Action |
|---|---|
git stash used for revert | Drop immediately (git stash drop). Use git checkout -- .. |
| New dependency during execution | Revert leaf. Add child node. Commit tree. Validate. Fix dependency first. |
| Test breaks in unrelated area | That failure is a new tree node. Revert. Do not continue the leaf. |
| Tree direction challenged | Re-read Rule 4. Child = prerequisite = deeper = implemented first. Verify before accepting. |
| Merge conflict between leaf branches | Add coordination node above both. Revert both. Implement coordination node first. |
[ ] {GM} Golden master on <module> as child of the first
refacto node; other nodes targeting that module declare requires: {GM}. Commit. Validate.[no-golden-master: coverage X% on <module>] anywhere
in the .mikado.md file. Commit. Validate.
Do not start naive attempts without one of these present in the file.docs/mikado/<goal>.mikado.md (root only) → commit immediately.git rev-parse HEAD).[discovered-by] + [parent-error].git checkout -- . would discard uncommitted tree edits).git checkout -- . → git status (must be clean).validate-mikado.sh — must exit 0.[x] before staging.validate-mikado.sh.git add <files + .mikado.md> && git commit -m "feat: [Node]".[x].