Skip to content

Commit ad91633

Browse files
committed
feat(tools): implement dynamic timeout configuration for codebase tools
1 parent 0d3ce3d commit ad91633

7 files changed

Lines changed: 918 additions & 19 deletions

File tree

docs/codebase-map-briefing.md

Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
# codebase_map Tool Briefing
2+
3+
> A comprehensive reference for the `codebase_map` MCP tool — understanding its inputs, outputs, modes, and adaptive compression system.
4+
5+
---
6+
7+
## Purpose
8+
9+
`codebase_map` is the **single tool for understanding what EXISTS in a codebase**. It provides structural maps at any granularity — from a simple list of files to full API details with type signatures and JSDoc documentation.
10+
11+
---
12+
13+
## Operating Modes
14+
15+
The tool has **two distinct modes** determined by the `path` parameter:
16+
17+
| Path Value | Mode | Output |
18+
|------------|------|--------|
19+
| Omitted or directory | **Directory/Workspace Mode** | File tree with symbols |
20+
| Points to a file | **File Mode** | Detailed exports with signatures |
21+
22+
### Directory/Workspace Mode
23+
Returns a hierarchical view of the codebase structure with symbols at each file.
24+
25+
### File Mode
26+
Returns detailed export analysis for a single file: function signatures, type definitions, JSDoc comments, and re-export chains.
27+
28+
---
29+
30+
## Input Parameters
31+
32+
### Core Parameters
33+
34+
| Parameter | Type | Default | Description |
35+
|-----------|------|---------|-------------|
36+
| `path` | `string?` | `undefined` | File, directory, or glob to map. Omit for entire workspace. |
37+
| `depth` | `number` | `1` | Detail level (0-6). See depth explanation below. |
38+
| `filter` | `string?` | `undefined` | Glob pattern to include matching files (e.g., `"src/tools/**"`). |
39+
| `kind` | `enum` | `'all'` | Filter symbols by kind. Options: `all`, `functions`, `classes`, `interfaces`, `types`, `constants`, `enums` |
40+
41+
### Depth Levels
42+
43+
```
44+
depth: 0 → File tree only (directories and filenames)
45+
depth: 1 → Top-level symbols per file (functions, classes, interfaces)
46+
depth: 2 → Symbols with type signatures (class members, method params)
47+
depth: 3+ → Full detail including JSDoc documentation
48+
```
49+
50+
### Content Toggles
51+
52+
| Parameter | Type | Default | Effect |
53+
|-----------|------|---------|--------|
54+
| `includeTypes` | `boolean` | `true` | Include type signatures (at depth ≥ 2) |
55+
| `includeJSDoc` | `boolean` | `true` | Include JSDoc descriptions (at depth ≥ 3) |
56+
| `includeImports` | `boolean` | `false` | Include import specifiers per file |
57+
| `includeStats` | `boolean` | `false` | Include line counts and diagnostic counts |
58+
| `includeGraph` | `boolean` | `false` | Include module dependency graph with circular dependency detection |
59+
60+
### Pattern Filters
61+
62+
| Parameter | Type | Description |
63+
|-----------|------|-------------|
64+
| `includePatterns` | `string[]?` | Glob patterns to restrict to matching files only |
65+
| `excludePatterns` | `string[]?` | Glob patterns to exclude files (applied on top of `.devtoolsignore`) |
66+
67+
---
68+
69+
## Output Formats
70+
71+
### Directory/Workspace Mode Output
72+
73+
```json
74+
{
75+
"projectRoot": "C:/path/to/project",
76+
"files": [ ... ],
77+
"summary": {
78+
"totalFiles": 42,
79+
"totalDirectories": 10,
80+
"totalSymbols": 156
81+
},
82+
"graph": { ... }, // Only if includeGraph: true
83+
"outputScaling": { ... }, // Only if adaptive compression was applied
84+
"ignoredBy": { ... } // Only if totalFiles == 0
85+
}
86+
```
87+
88+
#### `files` Array Formats
89+
90+
The `files` array can take **three different shapes** depending on compression level:
91+
92+
**1. Full Objects (with symbols)**
93+
```json
94+
{
95+
"path": "src/tools/codebase-map.ts",
96+
"symbols": [
97+
{ "name": "estimateTokens", "kind": "function", "range": { "start": 30, "end": 32 } },
98+
{ "name": "flattenTree", "kind": "function", "range": { "start": 48, "end": 63 } }
99+
],
100+
"imports": ["./client-pipe.js", "zod"],
101+
"lines": 400
102+
}
103+
```
104+
105+
**2. Flat Path Strings (no symbols)**
106+
```json
107+
[
108+
"src/tools/codebase-map.ts",
109+
"src/tools/codebase-trace.ts",
110+
"src/services/CdpService.ts"
111+
]
112+
```
113+
114+
**3. Directory Summary (most compressed)**
115+
```json
116+
{
117+
"src/tools": 12,
118+
"src/services": 5,
119+
"src/tools/codebase": 4,
120+
"extension": 8
121+
}
122+
```
123+
124+
### File Mode Output
125+
126+
```json
127+
{
128+
"module": "src/client-pipe.ts",
129+
"exports": [
130+
{
131+
"name": "runTerminal",
132+
"kind": "function",
133+
"signature": "(name: string, cwd: string, command: string, ...) => Promise<TerminalRunResult>",
134+
"jsdoc": "Run a command in the terminal and return the result.",
135+
"line": 245,
136+
"isDefault": false,
137+
"isReExport": false
138+
}
139+
],
140+
"reExports": [
141+
{ "name": "zod", "from": "./third_party/index.js" }
142+
],
143+
"summary": "15 exports from src/client-pipe.ts"
144+
}
145+
```
146+
147+
---
148+
149+
## Adaptive Compression System
150+
151+
`codebase_map` includes a **6-level progressive compression system** to keep output within a 3,000 token budget while preserving maximum useful information.
152+
153+
### Compression Phases
154+
155+
**Phase 1: Depth Reduction Loop**
156+
```
157+
depth N → depth (N-1) → ... → depth 0
158+
```
159+
If the flattened file list exceeds the token budget, depth is progressively reduced until it fits or reaches 0.
160+
161+
**Phase 2: Format Compression**
162+
```
163+
[{path, symbols}] → ["path", "path"] → {directory: count}
164+
```
165+
1. If no symbols remain (depth 0), switch from objects to flat path strings
166+
2. If paths still exceed budget, collapse to directory summary
167+
168+
### Output Scaling Metadata
169+
170+
When compression is applied, the output includes:
171+
172+
```json
173+
"outputScaling": {
174+
"requestedDepth": 3,
175+
"effectiveDepth": 0,
176+
"reductionsApplied": ["depth-3-to-2", "depth-2-to-1", "depth-1-to-0", "flat-paths", "directory-summary"],
177+
"estimatedTokens": 1116,
178+
"tokenLimit": 3000,
179+
"suggestions": [
180+
"Use filter or path to narrow scope for depth 3",
181+
"Use kind param to reduce symbol count",
182+
"Use includePatterns to select specific files"
183+
]
184+
}
185+
```
186+
187+
### Token Estimation
188+
189+
Tokens are estimated using: `Math.ceil(JSON.stringify(result).length / 4)`
190+
191+
---
192+
193+
## Best Practices for Efficient Queries
194+
195+
### Get Full Detail on Small Scope
196+
```json
197+
{ "path": "src/tools/codebase", "depth": 3 }
198+
```
199+
Narrow the path, increase the depth.
200+
201+
### Quick Overview of Large Codebase
202+
```json
203+
{ "depth": 0 }
204+
```
205+
Returns file tree only — fast and compact.
206+
207+
### Find Specific Symbol Types
208+
```json
209+
{ "kind": "functions", "depth": 1 }
210+
```
211+
Filter to only functions across the workspace.
212+
213+
### Check API Surface of a Module
214+
```json
215+
{ "path": "src/client-pipe.ts" }
216+
```
217+
File mode returns all exports with signatures.
218+
219+
### Include Dependency Graph
220+
```json
221+
{ "includeGraph": true, "path": "src/services" }
222+
```
223+
See module dependencies and circular imports (only if output has room).
224+
225+
---
226+
227+
## Symbol Kinds Reference
228+
229+
| `kind` Value | Matches |
230+
|--------------|---------|
231+
| `functions` | function declarations |
232+
| `classes` | class declarations |
233+
| `interfaces` | interface declarations |
234+
| `types` | type aliases |
235+
| `constants` | const declarations, variables |
236+
| `enums` | enum declarations |
237+
| `all` | everything (default) |
238+
239+
---
240+
241+
## Edge Cases
242+
243+
### Empty Results
244+
If `totalFiles == 0`, the output includes an `ignoredBy` object explaining which patterns excluded all files:
245+
246+
```json
247+
"ignoredBy": {
248+
"rootDir": "C:/path/to/project",
249+
"ignoreFilePath": "C:/path/to/project/.devtoolsignore",
250+
"ignoreFileExists": true,
251+
"activePatterns": ["node_modules/**", "dist/**", "*.test.ts"]
252+
}
253+
```
254+
255+
### File Not Found (File Mode)
256+
```json
257+
{
258+
"error": "File not found",
259+
"path": "src/nonexistent.ts",
260+
"suggestions": [
261+
"Check the file path for typos",
262+
"Use codebase_map with no path to see all files",
263+
"Use filter param to search by pattern"
264+
]
265+
}
266+
```
267+
268+
### Import Graph Skipped
269+
If `includeGraph: true` but output already uses >50% of token budget, the graph is silently skipped to avoid exceeding limits.
270+
271+
---
272+
273+
## Internal Architecture
274+
275+
```
276+
┌─────────────────────────┐
277+
│ codebase_map │
278+
│ (MCP tool) │
279+
└───────────┬─────────────┘
280+
│ RPC via client-pipe
281+
282+
┌─────────────────────────┐
283+
│ Extension Host │
284+
│ client-handlers.ts │
285+
└───────────┬─────────────┘
286+
│ Uses ts-morph / VS Code APIs
287+
288+
┌─────────────────────────┐
289+
│ Codebase Service │
290+
│ (TypeScript analysis) │
291+
└─────────────────────────┘
292+
```
293+
294+
### Key Functions
295+
296+
| Function | Purpose |
297+
|----------|---------|
298+
| `flattenTree()` | Convert nested tree to flat file list |
299+
| `makePathsRelative()` | Strip projectRoot prefix for compact paths |
300+
| `buildDirectorySummary()` | Collapse files to `{dir: count}` pairs |
301+
| `filterSymbolsByKind()` | Apply kind filter to symbol arrays |
302+
| `estimateTokens()` | Estimate output token count |
303+
304+
---
305+
306+
## Configuration
307+
308+
### Timeout
309+
- Tool-level timeout: **120 seconds** (2 minutes)
310+
- Sufficient for large codebases with deep analysis
311+
312+
### Token Budget
313+
- `OUTPUT_TOKEN_LIMIT`: **3,000 tokens**
314+
- `CHARS_PER_TOKEN`: **4** (estimation constant)
315+
316+
---
317+
318+
## Example Queries
319+
320+
```json
321+
// 1. Full workspace overview (depth 1)
322+
{}
323+
324+
// 2. Narrow scope with full detail
325+
{ "path": "src/services", "depth": 3 }
326+
327+
// 3. File tree only
328+
{ "depth": 0 }
329+
330+
// 4. Functions in tools directory
331+
{ "path": "src/tools", "kind": "functions", "depth": 2 }
332+
333+
// 5. With import graph
334+
{ "includeGraph": true, "path": "src" }
335+
336+
// 6. TypeScript files only
337+
{ "filter": "**/*.ts" }
338+
339+
// 7. Exclude tests
340+
{ "excludePatterns": ["**/*.test.ts", "**/__tests__/**"] }
341+
342+
// 8. Single file exports
343+
{ "path": "src/main.ts" }
344+
```
345+
346+
---
347+
348+
## Comparison with Other Tools
349+
350+
| Tool | Purpose | When to Use |
351+
|------|---------|-------------|
352+
| `codebase_map` | What EXISTS | Discover files, symbols, APIs |
353+
| `codebase_trace` | How symbols CONNECT | Track references, calls, type flows |
354+
| `codebase_lint` | What's WRONG | Find dead code, errors, duplicates |
355+
356+
---
357+
358+
*Last updated: Session where 6-level compression system was implemented*

0 commit comments

Comments
 (0)