Skip to content

Commit d47fd27

Browse files
authored
feat: CLI parity with MCP — 15 commands, shared core extraction (#16)
## Overview Adds full CLI parity with the MCP interface. All 15 MCP tools now have equivalent CLI commands with both human-readable and `--json` output. Extracts shared computation into `src/core/index.ts`, eliminating duplicated logic between CLI and MCP handlers. ## What changed **New CLI commands (10):** `dependents`, `modules`, `forces`, `dead-exports`, `groups`, `symbol`, `impact`, `rename`, `processes`, `clusters` **Shared core module:** 13 compute functions extracted to `src/core/index.ts`, consumed by both `src/mcp/index.ts` and `src/cli.ts`. This removed ~200 lines of duplicated logic from MCP handlers. **Performance:** Pre-built lookup maps (`nodeById`, `reverseAdjacency`) replace O(N) linear scans in hot paths like BFS traversal and cross-module dependency detection. ## Files | File | Change | |------|--------| | `src/core/index.ts` | New — 13 shared compute functions with typed result interfaces | | `src/cli.ts` | 10 new subcommands, each with formatted + JSON output | | `src/mcp/index.ts` | Refactored to delegate to core compute functions | | `tests/cli-commands.test.ts` | 47 integration tests covering all compute functions | | `docs/cli-reference.md` | Full 15-command reference with flags and examples | | `.github/workflows/ci.yml` | Node 18+22 matrix, concurrency groups, coverage best-effort | | `AGENTS.md`, `llms.txt`, `llms-full.txt` | Updated for CLI parity | ## CLI usage ```bash codebase-intelligence overview ./src codebase-intelligence hotspots ./src --metric coupling codebase-intelligence file ./src parser/index.ts codebase-intelligence search ./src "auth" codebase-intelligence changes ./src --json codebase-intelligence dependents ./src types.ts --depth 3 codebase-intelligence modules ./src codebase-intelligence forces ./src codebase-intelligence dead-exports ./src codebase-intelligence groups ./src codebase-intelligence symbol ./src parseCodebase codebase-intelligence impact ./src getUserById codebase-intelligence rename ./src oldName newName codebase-intelligence processes ./src --entry main codebase-intelligence clusters ./src --min-files 3 ``` All commands support `--json` for machine-readable output and `--force` to re-index. ## Testing - 286 tests passing (47 new for CLI core functions) - Tested against 3 real repositories with manual verification of output correctness - All quality gates green: lint, typecheck, build, test
1 parent 20b4972 commit d47fd27

File tree

13 files changed

+3099
-612
lines changed

13 files changed

+3099
-612
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
name: CI
22

33
on:
4+
push:
5+
branches: [main]
46
pull_request:
57
branches: [main]
68

79
concurrency:
8-
group: ci-${{ github.head_ref }}
10+
group: ci-${{ github.head_ref || github.ref_name }}
911
cancel-in-progress: true
1012

1113
jobs:
1214
quality:
1315
name: Quality Gates
1416
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
node-version: [18, 22]
1520
steps:
1621
- uses: actions/checkout@v4
1722

1823
- uses: pnpm/action-setup@v4
1924

2025
- uses: actions/setup-node@v4
2126
with:
22-
node-version: 22
27+
node-version: ${{ matrix.node-version }}
2328
cache: pnpm
2429

2530
- run: pnpm install --frozen-lockfile
@@ -35,3 +40,20 @@ jobs:
3540

3641
- name: Test
3742
run: pnpm test
43+
44+
# Coverage is best-effort: vitest v3 + v8 provider has a known worker
45+
# timeout bug (onTaskUpdate) on CI runners. Tests pass above; coverage
46+
# collection may timeout without affecting correctness.
47+
- name: Coverage
48+
if: matrix.node-version == 22
49+
continue-on-error: true
50+
run: pnpm vitest run --coverage --pool forks --no-file-parallelism
51+
52+
- name: Upload coverage
53+
if: matrix.node-version == 22 && always()
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: coverage-report
57+
path: coverage/
58+
retention-days: 14
59+
if-no-files-found: ignore

AGENTS.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Codebase Intelligence
2+
3+
TypeScript codebase analysis engine. Parses source, builds dependency graphs, computes architectural metrics.
4+
5+
## Setup
6+
7+
```bash
8+
npx codebase-intelligence@latest <path> # One-shot (no install)
9+
npm install -g codebase-intelligence # Global install
10+
```
11+
12+
## Interfaces
13+
14+
### MCP (for AI agents — preferred)
15+
16+
Start the MCP stdio server:
17+
18+
```bash
19+
codebase-intelligence ./path/to/project
20+
```
21+
22+
15 tools available: `codebase_overview`, `file_context`, `get_dependents`, `find_hotspots`, `get_module_structure`, `analyze_forces`, `find_dead_exports`, `get_groups`, `symbol_context`, `search`, `detect_changes`, `impact_analysis`, `rename_symbol`, `get_processes`, `get_clusters`.
23+
24+
2 prompts: `detect_impact`, `generate_map`.
25+
3 resources: `codebase://clusters`, `codebase://processes`, `codebase://setup`.
26+
27+
### CLI (for humans and CI)
28+
29+
15 commands — full parity with MCP tools:
30+
31+
```bash
32+
codebase-intelligence overview ./src # Codebase snapshot
33+
codebase-intelligence hotspots ./src # Rank files by metric
34+
codebase-intelligence file ./src auth/login.ts # File context
35+
codebase-intelligence search ./src "auth" # Keyword search
36+
codebase-intelligence changes ./src # Git diff analysis
37+
codebase-intelligence dependents ./src types.ts # File blast radius
38+
codebase-intelligence modules ./src # Module architecture
39+
codebase-intelligence forces ./src # Force analysis
40+
codebase-intelligence dead-exports ./src # Unused exports
41+
codebase-intelligence groups ./src # Directory groups
42+
codebase-intelligence symbol ./src parseCodebase # Symbol context
43+
codebase-intelligence impact ./src getUserById # Symbol blast radius
44+
codebase-intelligence rename ./src old new # Rename references
45+
codebase-intelligence processes ./src # Execution flows
46+
codebase-intelligence clusters ./src # File clusters
47+
```
48+
49+
Add `--json` for machine-readable output. All commands auto-cache the index.
50+
51+
### Tool Selection
52+
53+
| Question | MCP Tool | CLI Command |
54+
|----------|----------|-------------|
55+
| What does this codebase look like? | `codebase_overview` | `overview` |
56+
| Tell me about file X | `file_context` | `file` |
57+
| What are the riskiest files? | `find_hotspots` | `hotspots` |
58+
| Find files related to X | `search` | `search` |
59+
| What changed? | `detect_changes` | `changes` |
60+
| What breaks if I change file X? | `get_dependents` | `dependents` |
61+
| What breaks if I change function X? | `impact_analysis` | `impact` |
62+
| What's architecturally wrong? | `analyze_forces` | `forces` |
63+
| Who calls this function? | `symbol_context` | `symbol` |
64+
| Find all references for rename | `rename_symbol` | `rename` |
65+
| What files naturally group together? | `get_clusters` | `clusters` |
66+
| What can I safely delete? | `find_dead_exports` | `dead-exports` |
67+
| How are modules organized? | `get_module_structure` | `modules` |
68+
| What are the main areas? | `get_groups` | `groups` |
69+
| How does data flow? | `get_processes` | `processes` |
70+
71+
## Documentation
72+
73+
- `docs/architecture.md` — Pipeline, module map, data flow
74+
- `docs/data-model.md` — All TypeScript interfaces
75+
- `docs/metrics.md` — Per-file and module metrics, force analysis
76+
- `docs/mcp-tools.md` — 15 MCP tools with inputs, outputs, use cases
77+
- `docs/cli-reference.md` — CLI commands with examples
78+
- `llms.txt` — AI-consumable doc index
79+
- `llms-full.txt` — Full documentation for context injection
80+
81+
## Metrics
82+
83+
Key file metrics: PageRank, betweenness, fan-in/out, coupling, tension, churn, cyclomatic complexity, blast radius, dead exports, test coverage.
84+
85+
Module metrics: cohesion, escape velocity, verdict (LEAF/COHESIVE/MODERATE/JUNK_DRAWER).
86+
87+
Force analysis: tension files, bridge files, extraction candidates.
88+
89+
## Project Structure
90+
91+
```
92+
src/
93+
cli.ts Entry point + CLI commands
94+
core/index.ts Shared computation (used by MCP + CLI)
95+
types/index.ts All interfaces (single source of truth)
96+
parser/index.ts TypeScript AST parser
97+
graph/index.ts Dependency graph builder (graphology)
98+
analyzer/index.ts Metric computation engine
99+
mcp/index.ts MCP stdio server (15 tools)
100+
impact/index.ts Symbol-level impact analysis
101+
search/index.ts BM25 search engine
102+
process/index.ts Entry point + call chain tracing
103+
community/index.ts Louvain clustering
104+
persistence/index.ts Graph cache (.code-visualizer/)
105+
```

docs/architecture.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ Analyzer
1818
| computes: churn, complexity, blast radius, dead exports, test coverage
1919
| produces: ForceAnalysis (tension files, bridges, extraction candidates)
2020
v
21-
MCP (stdio)
22-
| exposes: 15 tools, 2 prompts, 3 resources for LLM agents
21+
Core (shared computation)
22+
| result builders used by both MCP and CLI
23+
v
24+
MCP (stdio) CLI (terminal/CI)
25+
| 15 tools, 2 prompts, | 5 commands: overview, hotspots,
26+
| 3 resources for LLMs | file, search, changes + --json
2327
```
2428

2529
## Module Map
@@ -30,6 +34,7 @@ src/
3034
parser/index.ts <- TS AST extraction + git churn + test detection
3135
graph/index.ts <- graphology graph + circular dep detection
3236
analyzer/index.ts <- All metric computation
37+
core/index.ts <- Shared result computation (MCP + CLI)
3338
mcp/index.ts <- 15 MCP tools for LLM integration
3439
mcp/hints.ts <- Next-step hints for MCP tool responses
3540
impact/index.ts <- Symbol-level impact analysis + rename planning
@@ -38,7 +43,7 @@ src/
3843
community/index.ts <- Louvain clustering
3944
persistence/index.ts <- Graph export/import to .code-visualizer/
4045
server/graph-store.ts <- Global graph state (shared by CLI + MCP)
41-
cli.ts <- Entry point, wires pipeline together
46+
cli.ts <- Entry point, CLI commands + MCP fallback
4247
```
4348

4449
## Data Flow
@@ -63,12 +68,12 @@ startMcpServer(codebaseGraph)
6368

6469
## Key Design Decisions
6570

66-
- **MCP-only**: No web UI or REST API. All interaction through MCP stdio for LLM agents.
71+
- **Dual interface**: MCP stdio for LLM agents, CLI subcommands for humans/CI. Both consume `src/core/`.
6772
- **graphology**: In-memory graph with O(1) neighbor lookup. PageRank and betweenness computed via graphology-metrics.
6873
- **Batch git churn**: Single `git log --all --name-only` call, parsed for all files. Avoids O(n) subprocess spawning.
6974
- **Dead export detection**: Cross-references parsed exports against edge symbol lists. May miss `import *` or re-exports (known limitation).
7075
- **Graceful degradation**: Non-git dirs get churn=0, no-test codebases get coverage=false. Never crashes.
71-
- **Graph persistence**: Optional `--index` flag caches parsed graph to `.code-visualizer/` for instant startup on unchanged HEAD.
76+
- **Graph persistence**: CLI commands always cache the graph index to `.code-visualizer/`. MCP mode (`codebase-intelligence <path>`) requires `--index` to persist the cache.
7277

7378
## Adding a New Metric
7479

docs/cli-reference.md

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# CLI Reference
2+
3+
15 commands for terminal and CI use. Full parity with MCP tools. All commands auto-cache the index to `.code-visualizer/`.
4+
5+
## Commands
6+
7+
### overview
8+
9+
High-level codebase snapshot.
10+
11+
```bash
12+
codebase-intelligence overview <path> [--json] [--force]
13+
```
14+
15+
**Output:** file count, function count, dependency count, modules (path, files, LOC, coupling, cohesion), top 5 depended files, avg LOC, max depth, circular dep count.
16+
17+
### hotspots
18+
19+
Rank files by metric.
20+
21+
```bash
22+
codebase-intelligence hotspots <path> [--metric <metric>] [--limit <n>] [--json] [--force]
23+
```
24+
25+
**Metrics:** `coupling` (default), `pagerank`, `fan_in`, `fan_out`, `betweenness`, `tension`, `churn`, `complexity`, `blast_radius`, `coverage`, `escape_velocity`.
26+
27+
### file
28+
29+
Detailed file context.
30+
31+
```bash
32+
codebase-intelligence file <path> <file> [--json] [--force]
33+
```
34+
35+
`<file>` is relative to the codebase root (e.g., `parser/index.ts`).
36+
37+
**Output:** LOC, exports, imports, dependents, all FileMetrics. Error: prints top 3 similar path suggestions.
38+
39+
### search
40+
41+
BM25 keyword search.
42+
43+
```bash
44+
codebase-intelligence search <path> <query> [--limit <n>] [--json] [--force]
45+
```
46+
47+
**Output:** Ranked results grouped by file, with symbol name, type, LOC, and relevance score.
48+
49+
### changes
50+
51+
Git diff analysis with risk metrics.
52+
53+
```bash
54+
codebase-intelligence changes <path> [--scope <scope>] [--json] [--force]
55+
```
56+
57+
**Scope:** `staged`, `unstaged`, `all` (default).
58+
59+
### dependents
60+
61+
File-level blast radius: direct + transitive dependents.
62+
63+
```bash
64+
codebase-intelligence dependents <path> <file> [--depth <n>] [--json] [--force]
65+
```
66+
67+
**Output:** direct dependents with symbols, transitive dependents with paths, total affected, risk level (LOW/MEDIUM/HIGH).
68+
69+
### modules
70+
71+
Module architecture with cross-module dependencies.
72+
73+
```bash
74+
codebase-intelligence modules <path> [--json] [--force]
75+
```
76+
77+
**Output:** modules with cohesion/escape velocity, cross-module deps, circular deps.
78+
79+
### forces
80+
81+
Architectural force analysis.
82+
83+
```bash
84+
codebase-intelligence forces <path> [--cohesion <n>] [--tension <n>] [--escape <n>] [--json] [--force]
85+
```
86+
87+
**Output:** module cohesion verdicts, tension files, bridge files, extraction candidates, summary.
88+
89+
### dead-exports
90+
91+
Find unused exports across the codebase.
92+
93+
```bash
94+
codebase-intelligence dead-exports <path> [--module <module>] [--limit <n>] [--json] [--force]
95+
```
96+
97+
**Output:** dead export count, files with unused exports, summary.
98+
99+
### groups
100+
101+
Top-level directory groups with aggregate metrics.
102+
103+
```bash
104+
codebase-intelligence groups <path> [--json] [--force]
105+
```
106+
107+
**Output:** groups ranked by importance with files, LOC, coupling.
108+
109+
### symbol
110+
111+
Function/class context with callers and callees.
112+
113+
```bash
114+
codebase-intelligence symbol <path> <name> [--json] [--force]
115+
```
116+
117+
**Output:** symbol metadata, fan-in/out, PageRank, betweenness, callers, callees.
118+
119+
### impact
120+
121+
Symbol-level blast radius with depth-grouped impact levels.
122+
123+
```bash
124+
codebase-intelligence impact <path> <symbol> [--json] [--force]
125+
```
126+
127+
**Output:** impact levels (WILL BREAK / LIKELY / MAY NEED TESTING), total affected.
128+
129+
### rename
130+
131+
Find all references for rename planning (read-only by default).
132+
133+
```bash
134+
codebase-intelligence rename <path> <oldName> <newName> [--no-dry-run] [--json] [--force]
135+
```
136+
137+
**Output:** references with file, symbol, and confidence level.
138+
139+
### processes
140+
141+
Entry point execution flows through the call graph.
142+
143+
```bash
144+
codebase-intelligence processes <path> [--entry <name>] [--limit <n>] [--json] [--force]
145+
```
146+
147+
**Output:** processes with entry point, steps, depth, modules touched.
148+
149+
### clusters
150+
151+
Community-detected file clusters (Louvain algorithm).
152+
153+
```bash
154+
codebase-intelligence clusters <path> [--min-files <n>] [--json] [--force]
155+
```
156+
157+
**Output:** clusters with files, file count, cohesion.
158+
159+
## Flags
160+
161+
| Flag | Available On | Description |
162+
|------|-------------|-------------|
163+
| `--json` | All commands | Output stable JSON to stdout |
164+
| `--force` | All commands | Re-parse even if cached index matches HEAD |
165+
| `--metric <m>` | hotspots | Metric to rank by (default: coupling) |
166+
| `--limit <n>` | hotspots, search, dead-exports, processes | Max results |
167+
| `--scope <s>` | changes | Git diff scope: staged, unstaged, all |
168+
| `--depth <n>` | dependents | Max traversal depth (default: 2) |
169+
| `--cohesion <n>` | forces | Min cohesion threshold (default: 0.6) |
170+
| `--tension <n>` | forces | Min tension threshold (default: 0.3) |
171+
| `--escape <n>` | forces | Min escape velocity threshold (default: 0.5) |
172+
| `--module <m>` | dead-exports | Filter by module path |
173+
| `--entry <name>` | processes | Filter by entry point name |
174+
| `--min-files <n>` | clusters | Min files per cluster |
175+
| `--no-dry-run` | rename | Actually perform the rename (default: dry run) |
176+
177+
## Behavior
178+
179+
**Auto-caching:** First CLI invocation parses the codebase and saves the index to `.code-visualizer/`. Subsequent commands use the cache if `git HEAD` hasn't changed. Add `.code-visualizer/` to `.gitignore`.
180+
181+
**stdout/stderr:** Results go to stdout. Progress messages go to stderr. Safe for piping (`| jq`, `> file.json`).
182+
183+
**Exit codes:**
184+
- `0` — success
185+
- `1` — runtime error (file not found, no TS files, git unavailable)
186+
- `2` — bad args or usage error
187+
188+
**MCP mode:** Running `codebase-intelligence <path>` without a subcommand starts the MCP stdio server (backward compatible). MCP-specific flags:
189+
- `--index` — persist graph index to `.code-visualizer/` (CLI auto-caches, MCP requires this flag)
190+
- `--status` — print index status and exit
191+
- `--clean` — remove `.code-visualizer/` index and exit
192+
- `--force` — re-index even if HEAD unchanged

0 commit comments

Comments
 (0)