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