---
description: Find deepening opportunities in codebase, informed by domain language in CONTEXT.md and decisions in docs/adr/. Use when user wants to improve architecture, find refactoring opportunities, consolidate tightly-coupled modules, or make codebase more testable and AI-navigable.
allowed-tools: Bash, Read, Write, Edit, Glob, Grep, Agent
---


# Improve Codebase Architecture

Surface architectural friction. Propose **deepening opportunities** — refactors turning shallow modules into deep ones. Aim: testability and AI-navigability.

## Glossary

Use these terms exact in every suggestion. Consistent language is point — don't drift into "component," "service," "API," or "boundary." Full definitions in [LANGUAGE.md](LANGUAGE.md).

- **Module** — anything with interface and implementation (function, class, package, slice).
- **Interface** — everything caller must know to use module: types, invariants, error modes, ordering, config. Not just type signature.
- **Implementation** — code inside.
- **Depth** — leverage at interface: lots of behaviour behind small interface. **Deep** = high leverage. **Shallow** = interface nearly as complex as implementation.
- **Seam** — where interface lives; place behaviour can be altered without editing in place. (Use this, not "boundary.")
- **Adapter** — concrete thing satisfying interface at seam.
- **Leverage** — what callers get from depth.
- **Locality** — what maintainers get from depth: change, bugs, knowledge concentrated in one place.

Key principles (full list: [LANGUAGE.md](LANGUAGE.md)):

- **Deletion test**: imagine deleting module. Complexity vanishes → pass-through. Complexity reappears across N callers → was earning its keep.
- **The interface is the test surface.**
- **One adapter = hypothetical seam. Two adapters = real seam.**

Skill _informed_ by project domain model. Domain language names good seams; ADRs record decisions — don't re-litigate.

## Process

### 1. Explore

Read project domain glossary + ADRs in area first.

Use Agent tool with `subagent_type=Explore` to walk codebase. No rigid heuristics — explore organic, note friction:

- Where does understanding one concept require bouncing between many small modules?
- Where are modules **shallow** — interface nearly as complex as implementation?
- Where have pure functions been extracted just for testability, but real bugs hide in how they're called (no **locality**)?
- Where do tightly-coupled modules leak across seams?
- Which parts of codebase untested, or hard to test through current interface?

Apply **deletion test** to anything suspect shallow: would deleting concentrate complexity, or just move it? "Yes, concentrates" = signal wanted.

### 2. Present candidates

Present numbered list of deepening opportunities. For each:

- **Files** — which files/modules involved
- **Problem** — why current architecture causes friction
- **Solution** — plain description of what changes
- **Benefits** — in terms of locality and leverage, and how tests improve

**Use CONTEXT.md vocab for domain, [LANGUAGE.md](LANGUAGE.md) vocab for architecture.** If `CONTEXT.md` defines "Order," talk "the Order intake module" — not "the FooBarHandler," not "the Order service."

**ADR conflicts**: surface only when friction real enough to warrant revisiting ADR. Mark clearly (e.g. _"contradicts ADR-0007 — but worth reopening because…"_). Don't list every theoretical refactor ADR forbids.

Don't propose interfaces yet. Ask: "Which of these would you like to explore?"

### 3. Grilling loop

User picks candidate → grilling conversation. Walk design tree — constraints, dependencies, shape of deepened module, what sits behind seam, what tests survive.

Side effects inline as decisions crystallize:

- **Naming deepened module after concept not in `CONTEXT.md`?** Add term to `CONTEXT.md` — same discipline as `/grill-with-docs` (see [CONTEXT-FORMAT.md](../grill-with-docs/CONTEXT-FORMAT.md)). Create file lazily if not exist.
- **Sharpening fuzzy term during conversation?** Update `CONTEXT.md` right there.
- **User rejects candidate with load-bearing reason?** Offer ADR, framed: _"Want me to record this as an ADR so future architecture reviews don't re-suggest it?"_ Only offer when reason actually needed by future explorer — skip ephemeral reasons ("not worth it right now") and self-evident ones. See [ADR-FORMAT.md](../grill-with-docs/ADR-FORMAT.md).
- **Want to explore alternative interfaces for deepened module?** See [INTERFACE-DESIGN.md](INTERFACE-DESIGN.md).

$ARGUMENTS
