Understand and refactor
First session on an unfamiliar product repo. Map, then change. Do not edit until the map names the highest-ROI targets — unless the user passed a path so narrow the map is that path.
$ARGUMENTS is the scope: a path or subsystem, and/or --report-only. Default scope is the current workspace. --report-only maps and ranks; it does not edit.
Hard gates
- Behavior stays the same unless the user asked for a behavior change. This skill is structure, not features.
- Prefer existing docs over re-exploring:
AGENTS.md, CLAUDE.md, CONTEXT.md, ADRs, handoffs/, README.
- A failed lookup is not a missing record. Prove the search can find something before reporting nothing.
- Do not invent product rules, betting thresholds, or deploy steps. Point at the file that already owns them.
- Do not add a library that duplicates one already in the repo. Search
package.json / imports first.
1. Scope
- Named path / subsystem → that tree only.
- No path → whole workspace, but still start at the product surface the user cares about (app entry, API, pipeline) rather than listing every folder.
--report-only present → stop after §4. No edits, no commits.
2. Map (write this down)
Read docs first. Then the repo:
- Top-level layout and what each tree is for.
- Runtime entry points (CLI, workers, HTTP, UI routes, schedulers).
- Data flow for the main job: who writes, who reads, which store/key, which combiner.
- Shared modules vs one-off copies.
- Test / verify command that actually runs.
Keep the map short. Names, paths, arrows. Not a tour.
If a code-graph / memory MCP is available for this repo, use it before a repo-wide grep for symbols. Grep for exact string literals.
3. Rank problems
Only four buckets. Drop anything that is taste, renaming-for-renaming, or a rewrite of a working seam:
| Bucket |
Look for |
| Structural |
God modules, inverted dependencies, business rules stuck in I/O |
| Duplication |
Two sources for one fact, copy-pasted gates, parallel formatters |
| Perf |
Hot path doing N+1, unbounded walks, extra renders, sync work on the request path |
| Maintainability |
Dead flags, undocumented invariants, tests that cannot fail, comments that lie |
Score each candidate:
- Blast radius — files/callers touched
- Payoff — fewer lies, fewer copies, cheaper hot path, safer change later
- Proof — how you will know it worked (test, metric, or both)
Highest ROI = high payoff, bounded blast radius, a proof. Skip “clean up the whole layer.”
4. Report
Print:
- Map — entries, data flow, stores.
- Ranked list — 3–7 items, ROI first, with proof.
- Do not touch — sacred files / hard rules found in docs.
- First move — the one change you would make this session.
--report-only ends here.
5. Apply (not --report-only)
Do the first move, then the next only if it is still the highest ROI after the first lands.
- One logical change per commit-sized chunk.
- Existing tests run; add a characterization test before moving a seam that has none.
- Hunt regressions on sibling readers of any fact you moved.
- Stop when the ranked list’s remaining items are speculative or out of scope.
Done
The map is still accurate. The edits match the ranked list. Proof ran. No drive-by refactors outside the list.
1---2name: understand-and-refactor3description: Join an unfamiliar product repo: map architecture and data flow, then apply the highest-ROI structural / duplication / perf / maintainability refactors.4---56# Understand and refactor78First session on an unfamiliar product repo. **Map, then change.** Do not edit until the map names the highest-ROI targets — unless the user passed a path so narrow the map is that path.910`$ARGUMENTS` is the scope: a path or subsystem, and/or `--report-only`. Default scope is the current workspace. `--report-only` maps and ranks; it does not edit.1112## Hard gates1314- Behavior stays the same unless the user asked for a behavior change. This skill is structure, not features.15- Prefer existing docs over re-exploring: `AGENTS.md`, `CLAUDE.md`, `CONTEXT.md`, ADRs, `handoffs/`, README.16- A failed lookup is not a missing record. Prove the search can find something before reporting nothing.17- Do not invent product rules, betting thresholds, or deploy steps. Point at the file that already owns them.18- Do not add a library that duplicates one already in the repo. Search `package.json` / imports first.1920## 1. Scope2122- Named path / subsystem → that tree only.23- No path → whole workspace, but still start at the product surface the user cares about (app entry, API, pipeline) rather than listing every folder.24- `--report-only` present → stop after §4. No edits, no commits.2526## 2. Map (write this down)2728Read docs first. Then the repo:29301. Top-level layout and what each tree is for.312. Runtime entry points (CLI, workers, HTTP, UI routes, schedulers).323. Data flow for the main job: who writes, who reads, which store/key, which combiner.334. Shared modules vs one-off copies.345. Test / verify command that actually runs.3536Keep the map short. Names, paths, arrows. Not a tour.3738If a code-graph / memory MCP is available for this repo, use it before a repo-wide grep for symbols. Grep for exact string literals.3940## 3. Rank problems4142Only four buckets. Drop anything that is taste, renaming-for-renaming, or a rewrite of a working seam:4344| Bucket | Look for |45|---|---|46| Structural | God modules, inverted dependencies, business rules stuck in I/O |47| Duplication | Two sources for one fact, copy-pasted gates, parallel formatters |48| Perf | Hot path doing N+1, unbounded walks, extra renders, sync work on the request path |49| Maintainability | Dead flags, undocumented invariants, tests that cannot fail, comments that lie |5051Score each candidate:5253- **Blast radius** — files/callers touched54- **Payoff** — fewer lies, fewer copies, cheaper hot path, safer change later55- **Proof** — how you will know it worked (test, metric, or both)5657Highest ROI = high payoff, bounded blast radius, a proof. Skip “clean up the whole layer.”5859## 4. Report6061Print:62631. **Map** — entries, data flow, stores.642. **Ranked list** — 3–7 items, ROI first, with proof.653. **Do not touch** — sacred files / hard rules found in docs.664. **First move** — the one change you would make this session.6768`--report-only` ends here.6970## 5. Apply (not `--report-only`)7172Do the first move, then the next only if it is still the highest ROI after the first lands.7374- One logical change per commit-sized chunk.75- Existing tests run; add a characterization test before moving a seam that has none.76- Hunt regressions on sibling readers of any fact you moved.77- Stop when the ranked list’s remaining items are speculative or out of scope.7879## Done8081The map is still accurate. The edits match the ranked list. Proof ran. No drive-by refactors outside the list.