|
| 1 | +--- |
| 2 | +name: improve-codebase-architecture |
| 3 | +description: Find deepening opportunities in the codebase, informed by the domain language in docs/glossary.md and the architecture in docs/architecture.md. Use when the user wants to improve architecture, find refactoring opportunities, consolidate tightly-coupled modules, or make a codebase more testable and AI-navigable. |
| 4 | +--- |
| 5 | + |
| 6 | +# Improve Codebase Architecture |
| 7 | + |
| 8 | +Surface architectural friction and propose **deepening opportunities** — refactors that turn shallow modules into deep ones. The aim is testability and AI-navigability. |
| 9 | + |
| 10 | +## Glossary |
| 11 | + |
| 12 | +Use these terms exactly in every suggestion. Consistent language is the point — don't drift into "component," "service," "API," or "boundary." Full definitions in [LANGUAGE.md](LANGUAGE.md). |
| 13 | + |
| 14 | +- **Module** — anything with an interface and an implementation (function, class, package, slice). |
| 15 | +- **Interface** — everything a caller must know to use the module: types, invariants, error modes, ordering, config. Not just the type signature. |
| 16 | +- **Implementation** — the code inside. |
| 17 | +- **Depth** — leverage at the interface: a lot of behaviour behind a small interface. **Deep** = high leverage. **Shallow** = interface nearly as complex as the implementation. |
| 18 | +- **Seam** — where an interface lives; a place behaviour can be altered without editing in place. (Use this, not "boundary.") |
| 19 | +- **Adapter** — a concrete thing satisfying an interface at a seam. |
| 20 | +- **Leverage** — what callers get from depth. |
| 21 | +- **Locality** — what maintainers get from depth: change, bugs, knowledge concentrated in one place. |
| 22 | + |
| 23 | +Key principles (see [LANGUAGE.md](LANGUAGE.md) for the full list): |
| 24 | + |
| 25 | +- **Deletion test**: imagine deleting the module. If complexity vanishes, it was a pass-through. If complexity reappears across N callers, it was earning its keep. |
| 26 | +- **The interface is the test surface.** |
| 27 | +- **One adapter = hypothetical seam. Two adapters = real seam.** |
| 28 | + |
| 29 | +This skill is _informed_ by the project's domain model. The domain language in [`docs/glossary.md`](../../../docs/glossary.md) gives names to good seams; the layering described in [`docs/architecture.md`](../../../docs/architecture.md) records the structural decisions the skill should not re-litigate. |
| 30 | + |
| 31 | +## Process |
| 32 | + |
| 33 | +### 1. Explore |
| 34 | + |
| 35 | +Read [`docs/glossary.md`](../../../docs/glossary.md) (canonical domain terms) and the relevant section of [`docs/architecture.md`](../../../docs/architecture.md) (canonical layering / wiring) first. |
| 36 | + |
| 37 | +Then walk the codebase via [`codemap`](../codemap/SKILL.md) — the structural SQLite index. Per the [`codemap` rule](../../rules/codemap.md), querying the index beats grepping for symbol-shaped questions: |
| 38 | + |
| 39 | +```bash |
| 40 | +codemap query --json "SELECT name, signature, file_path FROM symbols WHERE file_path LIKE 'src/cli/%' AND kind = 'function'" |
| 41 | +codemap query --json "SELECT from_path, COUNT(*) AS deps FROM dependencies GROUP BY from_path ORDER BY deps DESC LIMIT 10" |
| 42 | +codemap query --json -r barrel-files |
| 43 | +``` |
| 44 | + |
| 45 | +Don't follow rigid heuristics — explore organically and note where you experience friction: |
| 46 | + |
| 47 | +- Where does understanding one concept require bouncing between many small modules? |
| 48 | +- Where are modules **shallow** — interface nearly as complex as the implementation? |
| 49 | +- Where have pure functions been extracted just for testability, but the real bugs hide in how they're called (no **locality**)? |
| 50 | +- Where do tightly-coupled modules leak across their seams? |
| 51 | +- Which parts of the codebase are untested, or hard to test through their current interface? |
| 52 | + |
| 53 | +Apply the **deletion test** to anything you suspect is shallow: would deleting it concentrate complexity, or just move it? A "yes, concentrates" is the signal you want. |
| 54 | + |
| 55 | +### 2. Present candidates |
| 56 | + |
| 57 | +Present a numbered list of deepening opportunities. For each candidate: |
| 58 | + |
| 59 | +- **Files** — which files/modules are involved |
| 60 | +- **Problem** — why the current architecture is causing friction |
| 61 | +- **Solution** — plain English description of what would change |
| 62 | +- **Benefits** — explained in terms of locality and leverage, and also in how tests would improve |
| 63 | + |
| 64 | +**Use [`docs/glossary.md`](../../../docs/glossary.md) vocabulary for the domain, and [LANGUAGE.md](LANGUAGE.md) vocabulary for the architecture.** If the glossary defines `barrel file`, talk about "the barrel-file detection module" — not "the FooBarHandler," and not "the barrel service." |
| 65 | + |
| 66 | +**Architecture conflicts**: if a candidate contradicts [`docs/architecture.md` § Layering](../../../docs/architecture.md#layering), only surface it when the friction is real enough to warrant revisiting that layering. Mark it clearly (e.g. _"contradicts architecture.md § Layering — but worth reopening because…"_). Don't list every theoretical refactor the layering forbids. |
| 67 | + |
| 68 | +Do NOT propose interfaces yet. Ask the user: "Which of these would you like to explore?" |
| 69 | + |
| 70 | +### 3. Grilling loop |
| 71 | + |
| 72 | +Once the user picks a candidate, drop into a grilling conversation (per [`grill-me`](../grill-me/SKILL.md)). Walk the design tree with them — constraints, dependencies, the shape of the deepened module, what sits behind the seam, what tests survive. |
| 73 | + |
| 74 | +Side effects happen inline as decisions crystallize: |
| 75 | + |
| 76 | +- **Naming a deepened module after a concept not in `docs/glossary.md`?** Add the term to the glossary right there per [`docs/README.md` Rule 9](../../../docs/README.md). Disambiguations (TS shape vs SQL table, etc.) take priority. |
| 77 | +- **Sharpening a fuzzy term during the conversation?** Update `docs/glossary.md` right there. |
| 78 | +- **Surfacing a structural decision worth recording?** If the candidate becomes a planned refactor, draft `docs/plans/<topic>.md` per [`docs/README.md` Rule 3](../../../docs/README.md). Codemap doesn't ship ADRs — decisions of record lift into [`docs/architecture.md`](../../../docs/architecture.md) on ship per [`docs/README.md` Rule 2](../../../docs/README.md), and the plan file is deleted. |
| 79 | +- **Want to explore alternative interfaces for the deepened module?** See [INTERFACE-DESIGN.md](INTERFACE-DESIGN.md). |
| 80 | +- **Sub-rules for what counts as a "deepening" candidate**: see [DEEPENING.md](DEEPENING.md). |
0 commit comments