Codebase Pattern Analyzer
Systematically analyze a reference codebase and extract reusable architectural patterns into structured descriptions. These descriptions become the raw material for new skills (via the skill-template-generator skill).
When to Use
- You need to understand how a large codebase is structured before replicating it
- You want to extract a specific pattern (component architecture, service layer, API proxy, etc.) from a reference project
- You are bootstrapping a new project that should follow the conventions of an existing one
Step 1: Map the Directory Structure
Read the top-level directory and identify the major code zones:
list_dir <project_root>
list_dir <project_root>/src
Classify each directory into one of these roles:
| Role |
Typical Dirs |
What to Look For |
| Components |
src/components/ |
UI classes, base class inheritance, DOM manipulation |
| Services |
src/services/ |
Data fetching, API calls, circuit breakers, caching |
| Config |
src/config/ |
Constants, panel definitions, API endpoints, feature flags |
| Styles |
src/styles/ |
CSS custom properties, theme variables, grid layout |
| Utils |
src/utils/ |
Helper functions, formatting, DOM utilities |
| API Layer |
api/, server/ |
Serverless functions, route handlers, CORS, proxy logic |
| Entry Point |
src/main.ts |
Bootstrap, panel instantiation, scheduler setup |
Step 2: Identify the Component Pattern
Find the base component class (usually the most imported file in components/):
read_file <project_root>/src/components/Panel.ts
Extract these structural elements:
- Constructor options interface — what config each component accepts (id, title, className, etc.)
- DOM structure — how the element tree is built (header, content area, resize handles)
- Lifecycle methods — constructor, destroy, show/hide, refresh
- State management — loading/error/content states, fetching guards
- Data binding — how data flows from service → render → DOM
Record the pattern as:
COMPONENT PATTERN:
Base class: [name]
Options: [list of constructor params]
DOM tree: [element hierarchy]
States: [loading, error, content]
Lifecycle: [init → fetch → render → destroy]
Key methods: [getElement, showLoading, showError, setContent, refresh, destroy]
Step 3: Identify the Service Layer Pattern
Examine 2-3 service files to find the common structure:
read_file <project_root>/src/services/news.ts
read_file <project_root>/src/services/stock-market.ts
Look for:
- Circuit breaker / resilience — retry logic, cooldown, cached fallback
- Interface definitions — typed data shapes returned by each service
- Fetch pattern — how HTTP calls are made (direct fetch, proxy, batching)
- Caching strategy — TTLs, stale-while-revalidate, in-memory cache
- Error handling — what happens on failure, default return values
Record as:
SERVICE PATTERN:
Resilience: [circuit breaker with N failures → cooldown]
Cache: [in-memory, TTL=Xms]
Fetch: [browser fetch → /api/... proxy → external API]
Error: [catch → recordFailure → return default]
Exports: [async functions, not class instances]
Step 4: Identify the API Proxy Pattern
Read the server-side proxy layer:
list_dir <project_root>/api/
read_file <project_root>/api/_cors.js
read_file <project_root>/api/stocks.ts # or similar endpoint
Extract:
- File-per-endpoint structure — one file per API domain
- CORS handling — origin allowlist, preflight response
- API key isolation —
process.env.* usage, never exposed to frontend
- Cache headers —
Cache-Control, s-maxage, stale-while-revalidate
- Error wrapping — structured JSON errors, never raw upstream responses
- Input validation — query parameter checks before upstream call
Step 5: Identify the Styling System
read_file <project_root>/src/styles/main.css # first 100 lines for CSS variables
Extract:
- CSS custom properties — color tokens, font stacks, spacing
- Theme structure — dark/light mode switch mechanism
- Grid layout —
grid-template-columns, auto-fill, minmax() values
- Component styles — panel base styles, header, content, scrollbar
- Semantic colors — positive/negative, severity levels, status indicators
- Typography — font family, sizes, letter-spacing, text-transform
Step 6: Identify the Scheduling / Refresh Pattern
read_file <project_root>/src/app/refresh-scheduler.ts
Extract:
- Registration API — how panels register their refresh functions
- Interval management — per-panel configurable intervals
- Visibility awareness — pause when tab hidden, flush stale on return
- In-flight guards — prevent duplicate concurrent refreshes
- Stagger logic — avoid API burst when resuming
Step 7: Synthesize Into Pattern Descriptions
For each pattern discovered, produce a structured description:
## Pattern: [Name]
**Source file(s)**: [paths in reference codebase]
**Category**: component | service | api | style | scheduler | utility
### Structure
[Key structural elements — interfaces, classes, functions]
### Key Code
[Minimal representative code snippet, 20-40 lines]
### Conventions
[Naming, file organization, import paths]
### Dependencies
[Other patterns this depends on]
### Adaptation Notes
[What must change when applying to a different project]
Output
The final output should be a list of pattern descriptions, one per architectural concern. These become the input to the skill-template-generator skill, which turns each pattern into a standalone SKILL.md.
Tips
- Read broadly first, deeply second — scan directory listings before reading individual files
- Follow imports — if a component imports from
../services/, read that service next
- Count instances — if 10+ components extend the same base class, that's a core pattern
- Note what's NOT used — no framework (React, Vue) means vanilla DOM; no ORM means raw fetch
- Compare 2-3 examples of the same pattern to distinguish the template from the instance-specific parts
1---2name: codebase-pattern-analyzer3description: Analyze a reference codebase to discover and extract reusable architectural patterns. Produces structured pattern descriptions that can be turned into standalone skills. This is a meta-skill that bootstraps the skill evolution chain.4---5
6# Codebase Pattern Analyzer
7
8Systematically analyze a reference codebase and extract reusable architectural patterns into structured descriptions. These descriptions become the raw material for new skills (via the `skill-template-generator` skill).
9
10## When to Use
11
12- You need to understand how a large codebase is structured before replicating it
13- You want to extract a specific pattern (component architecture, service layer, API proxy, etc.) from a reference project
14- You are bootstrapping a new project that should follow the conventions of an existing one
15
16## Step 1: Map the Directory Structure
17
18Read the top-level directory and identify the major code zones:
19
20```
21list_dir <project_root>
22list_dir <project_root>/src
23```
24
25Classify each directory into one of these roles:
26
27| Role | Typical Dirs | What to Look For |
28|------|-------------|------------------|
29| **Components** | `src/components/` | UI classes, base class inheritance, DOM manipulation |
30| **Services** | `src/services/` | Data fetching, API calls, circuit breakers, caching |
31| **Config** | `src/config/` | Constants, panel definitions, API endpoints, feature flags |
32| **Styles** | `src/styles/` | CSS custom properties, theme variables, grid layout |
33| **Utils** | `src/utils/` | Helper functions, formatting, DOM utilities |
34| **API Layer** | `api/`, `server/` | Serverless functions, route handlers, CORS, proxy logic |
35| **Entry Point** | `src/main.ts` | Bootstrap, panel instantiation, scheduler setup |
36
37## Step 2: Identify the Component Pattern
38
39Find the base component class (usually the most imported file in `components/`):
40
41```
42read_file <project_root>/src/components/Panel.ts
43```
44
45Extract these structural elements:
46
471. **Constructor options interface** — what config each component accepts (id, title, className, etc.)
482. **DOM structure** — how the element tree is built (header, content area, resize handles)
493. **Lifecycle methods** — constructor, destroy, show/hide, refresh
504. **State management** — loading/error/content states, fetching guards
515. **Data binding** — how data flows from service → render → DOM
52
53Record the pattern as:
54```
55COMPONENT PATTERN:
56 Base class: [name]
57 Options: [list of constructor params]
58 DOM tree: [element hierarchy]
59 States: [loading, error, content]
60 Lifecycle: [init → fetch → render → destroy]
61 Key methods: [getElement, showLoading, showError, setContent, refresh, destroy]
62```
63
64## Step 3: Identify the Service Layer Pattern
65
66Examine 2-3 service files to find the common structure:
67
68```
69read_file <project_root>/src/services/news.ts
70read_file <project_root>/src/services/stock-market.ts
71```
72
73Look for:
74
751. **Circuit breaker / resilience** — retry logic, cooldown, cached fallback
762. **Interface definitions** — typed data shapes returned by each service
773. **Fetch pattern** — how HTTP calls are made (direct fetch, proxy, batching)
784. **Caching strategy** — TTLs, stale-while-revalidate, in-memory cache
795. **Error handling** — what happens on failure, default return values
80
81Record as:
82```
83SERVICE PATTERN:
84 Resilience: [circuit breaker with N failures → cooldown]
85 Cache: [in-memory, TTL=Xms]
86 Fetch: [browser fetch → /api/... proxy → external API]
87 Error: [catch → recordFailure → return default]
88 Exports: [async functions, not class instances]
89```
90
91## Step 4: Identify the API Proxy Pattern
92
93Read the server-side proxy layer:
94
95```
96list_dir <project_root>/api/
97read_file <project_root>/api/_cors.js
98read_file <project_root>/api/stocks.ts # or similar endpoint
99```
100
101Extract:
102
1031. **File-per-endpoint structure** — one file per API domain
1042. **CORS handling** — origin allowlist, preflight response
1053. **API key isolation** — `process.env.*` usage, never exposed to frontend
1064. **Cache headers** — `Cache-Control`, `s-maxage`, `stale-while-revalidate`
1075. **Error wrapping** — structured JSON errors, never raw upstream responses
1086. **Input validation** — query parameter checks before upstream call
109
110## Step 5: Identify the Styling System
111
112```
113read_file <project_root>/src/styles/main.css # first 100 lines for CSS variables
114```
115
116Extract:
117
1181. **CSS custom properties** — color tokens, font stacks, spacing
1192. **Theme structure** — dark/light mode switch mechanism
1203. **Grid layout** — `grid-template-columns`, `auto-fill`, `minmax()` values
1214. **Component styles** — panel base styles, header, content, scrollbar
1225. **Semantic colors** — positive/negative, severity levels, status indicators
1236. **Typography** — font family, sizes, letter-spacing, text-transform
124
125## Step 6: Identify the Scheduling / Refresh Pattern
126
127```
128read_file <project_root>/src/app/refresh-scheduler.ts
129```
130
131Extract:
132
1331. **Registration API** — how panels register their refresh functions
1342. **Interval management** — per-panel configurable intervals
1353. **Visibility awareness** — pause when tab hidden, flush stale on return
1364. **In-flight guards** — prevent duplicate concurrent refreshes
1375. **Stagger logic** — avoid API burst when resuming
138
139## Step 7: Synthesize Into Pattern Descriptions
140
141For each pattern discovered, produce a structured description:
142
143```markdown
144## Pattern: [Name]
145
146**Source file(s)**: [paths in reference codebase]
147**Category**: component | service | api | style | scheduler | utility
148
149### Structure
150[Key structural elements — interfaces, classes, functions]
151
152### Key Code
153[Minimal representative code snippet, 20-40 lines]
154
155### Conventions
156[Naming, file organization, import paths]
157
158### Dependencies
159[Other patterns this depends on]
160
161### Adaptation Notes
162[What must change when applying to a different project]
163```
164
165## Output
166
167The final output should be a list of pattern descriptions, one per architectural concern. These become the input to the `skill-template-generator` skill, which turns each pattern into a standalone SKILL.md.
168
169## Tips
170
171- **Read broadly first, deeply second** — scan directory listings before reading individual files
172- **Follow imports** — if a component imports from `../services/`, read that service next
173- **Count instances** — if 10+ components extend the same base class, that's a core pattern
174- **Note what's NOT used** — no framework (React, Vue) means vanilla DOM; no ORM means raw fetch
175- **Compare 2-3 examples** of the same pattern to distinguish the template from the instance-specific parts
176