deep-modules-audit
Skill0 starsChanged 7 months agoMITView on GitHub
Audit a codebase for "agent debt" — code structured for human IDE navigation instead of AI agent tool-call navigation. Scans file structure, applies four deep-module lenses (shallow modules, co-change sets, co-read sets, already deep), and produces a concrete refactoring plan with before/after file counts. Use when asked to "audit for deep modules", "check for agent debt", "optimize codebase for AI", or "reduce file count".
In this repository8 other files it gives its agents.
- autopilot-executeSkill
- close-planSkill
- docs-auditSkill
- execute-phaseSkill
- lint-fixSkill
- living-design-systemSkill
- phased-planSkill
- responsive-auditSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 5 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 19
For AI agents, every file read is a tool call: Every cross-file edit is a failure point.
- Distinctive: line 59
Look for files that score on multiple shallow indicators:
- Distinctive: line 147
Do NOT execute without explicit approval.
- Distinctive: line 151
If the user says "go" or approves specific groups:
- Very specific: line 155
Run typecheck (tsc --noEmit or equivalent)
- Distinctive: line 156
Run tests (unit tests at minimum)
≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Deep Modules Audit
Audit a codebase for agent debt — code that's well-structured for humans with IDEs but poorly structured for AI agents with tool calls.
Principles
From John Ousterhout's A Philosophy of Software Design, adapted for AI agent maintenance:
Deep module: Simple interface hiding significant implementation. A 400-line file that encapsulates an entire subsystem. One read, full understanding.
Shallow module: Interface nearly as complex as what it hides. A 20-line file that exists for "organization" — adds a tool call but no abstraction.
For AI agents, every file read is a tool call. Every cross-file edit is a failure point. The goal: minimize the number of files an agent must read and edit per task.
The Four Lenses
| Lens | Question | Action |
| Shallow modules | Does this file exist for organization, not abstraction? (<50 lines, trivial content, barrel re-exports, data-only constants) | Merge into nearest neighbor |
| Co-change sets | Do these files always change together? (Add a type → update barrel → update consumer) | Merge into one file |
| Co-read sets | Must you read all of these to understand one feature? (8 type files that define one wire protocol) | Merge into one file |
| Already deep | Does this file encapsulate significant complexity behind a simple interface? (300+ lines, real logic, self-contained) | Leave alone |
What NOT to merge
- Independent modules that don't co-change (e.g., 12 tool implementations — editing one never touches another)
- UI components that are self-contained (each has real rendering logic, consumed independently)
- Route handlers that map to different API domains (face routes never change with chat routes)
- Test files and documentation (never edited in the same task as each other)
- Files with many consumers where moving them creates more import churn than reads saved
Process
Step 1 — Scan
Map the entire source file structure. Exclude node_modules, .d.ts, lockfiles.
# File count and line count per directory
find <src-dirs> -name '*.ts' -o -name '*.tsx' -o -name '*.py' -o -name '*.js' -o -name '*.jsx' | \
grep -v node_modules | grep -v '.d.ts' | \
sort | while read f; do echo "$(wc -l < "$f" | tr -d ' ') $f"; done
# Quick summary by directory
find <src-dirs> -name '*.ts' -o -name '*.tsx' | grep -v node_modules | grep -v '.d.ts' | \
sed 's|/[^/]*$||' | sort | uniq -c | sort -rn | head -30Record the total source file count as the baseline.
Step 2 — Identify Shallow Modules
Look for files that score on multiple shallow indicators:
- < 50 lines with no real logic (just types, one constant, one re-export)
- Barrel/index files that only re-export from elsewhere
- Data-only files (a single object or array, no functions)
- Single-function utilities (< 20 lines)
- Directory + barrel pattern where a directory contains one file + an
index.tsthat re-exports it
# Find small files
find <src-dirs> -name '*.ts' -o -name '*.tsx' | grep -v node_modules | grep -v '.d.ts' | \
xargs wc -l | sort -n | head -40
# Find barrel re-exports
grep -rn "export.*from" --include='*.ts' <src-dirs> | grep 'index.ts' | head -30
# Find single-component directories (1 tsx + 1 index.ts)
for dir in <pattern-dirs>/*/; do
tsx_count=$(find "$dir" -name '*.tsx' | wc -l | tr -d ' ')
if [ "$tsx_count" -eq 1 ]; then
echo "$(basename $dir)/ — 1 component + barrel"
fi
doneStep 3 — Identify Co-change and Co-read Sets
For each package/module, ask:
- "If I add a new X, which files must I touch?" — those are co-change sets
- "If I want to understand feature Y, which files must I read?" — those are co-read sets
Common patterns:
- Type packages with many small files that all define one protocol → co-read set
- Constants split across files for "separation" (labels + options + styles used by the same 2 components) → co-change set
- A file + its barrel (always edited together when adding an export) → co-change set
# Check how many consumers each file has (high consumer count = leave alone)
for f in <candidate-files>; do
name=$(basename "$f" .ts)
count=$(grep -rn "$name" --include='*.ts' --include='*.tsx' <src-dirs> | grep -v node_modules | wc -l)
echo "$count consumers: $f"
doneStep 4 — Identify Already Deep Modules
Files that are correctly structured — leave them alone:
- 200+ lines with real business logic, state management, or orchestration
- Self-contained — understanding this file doesn't require reading 3 others
- Clean interface — exports a factory function, a hook, or a component; internals are hidden
- Data files that are large reference databases (product catalogs, channel lists)
Step 5 — Produce the Report
Output a structured audit with four sections:
🔴 Refactor — Clear wins
For each candidate:
- List every file with line count
- Explain which lens applies (shallow / co-change / co-read)
- State the merge target (e.g., "8 files → 1 file")
- Estimate effort (trivial / moderate / significant)
🟡 Borderline — Could go either way
Files that are small but independent, or slightly shallow but have many consumers. Explain the tradeoff.
🟢 Already deep — Don't touch
List the major deep modules with line counts and why they're correctly structured.
Summary Table
| Area | Before | After | Files Saved |
| {area} | {N} files | {N} files | {N} files |
| Total | {N} source files | {N} source files | {N} files ({%} reduction) |
Step 6 — Wait for Go
Present the report and ask:
Do you want me to execute any of these refactorings? I'll do them one group at a time, running typecheck + tests after each.
Do NOT execute without explicit approval. The audit is the deliverable. Refactoring is a separate decision.
Execution Protocol (when approved)
If the user says "go" or approves specific groups:
- One group at a time — complete one 🔴 item, verify, then move to the next
- After each group:
- Run typecheck (
tsc --noEmitor equivalent) - Run tests (unit tests at minimum)
- Report pass/fail before continuing
- Run typecheck (
- Preserve non-code files — if directories contain docs, specs, or configs alongside the code being moved, don't lose them (rename, relocate, or keep)
- Commit after each group with a descriptive message
- Skip if ROI is negative — if a merge would touch more consumer files than it saves, say so and skip
- Report the final count — total source files before vs. after, total tests passing
Anti-patterns to Flag
Beyond the four lenses, flag these if spotted:
- Barrel explosion — a directory tree of
index.tsfiles that each re-export from one adjacent file - Mirror hierarchies — a
types/directory that mirrors thesrc/directory structure 1:1 - Config archipelago — 5+ config/constant files under 30 lines each in the same directory
- Wrapper-only files — functions that just call another function with slightly different arguments
- Prop-drilling type files — separate files that exist only to define the props for one component
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

