code-survey 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Logan Price
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,191 @@
1
+ # Code Survey
2
+
3
+ **Code Survey** is a deterministic codebase surveying tool designed to summarize repository structures for AI coding agents. It scans the files in your project, parses their ASTs using Tree-sitter to extract imports, exports, and symbols, and maps their interdependencies.
4
+
5
+ By default, Code Survey generates a token-optimized representation of the codebase, ensuring that your agent receives the necessary architectural context without wasting context window space on redundant syntactic overhead or internal local variables.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - **Multi-Language Support:** AST parsing for TypeScript/JavaScript, Python, Go, Java, C#, and Rust.
12
+ - **Exclusion of Internal Variables:** Excludes internal function-scoped variables by default to reduce noise (toggleable via flag).
13
+ - **Multiple Formats:** Support for `JSON`, `YAML`, a custom token-dense `Markdown`, and `Mermaid` flowcharts.
14
+ - **Compacted Line Locations:** Encodes coordinates into a compact `"Ln X-Y"` format to save character space.
15
+ - **Deterministic Output:** Recursively sorts files, namespaces, imports, and exports alphabetically, keeping git diffs clean and maximizing LLM context-cache hit rates.
16
+ - **Git-Delta Maps:** Map only the files that have changed in git (committed since a branch/hash, or uncommitted changes).
17
+ - **Traversal Depth Limits:** Traversal depth restriction for scaling up to large repositories or monorepos.
18
+
19
+ ---
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install @loganprice/code-survey
25
+ ```
26
+
27
+ *Note: Requires Node.js or Deno installed on the system.*
28
+
29
+ ---
30
+
31
+ ## CLI Usage
32
+
33
+ Run `code-survey` from the terminal:
34
+
35
+ ```bash
36
+ code-survey [options]
37
+ ```
38
+
39
+ ### Options
40
+
41
+ | Option | Alias | Description |
42
+ | :--- | :--- | :--- |
43
+ | `--root <dir>` | `-r` | The root directory of the codebase to map (default: current directory). |
44
+ | `--output <file>` | `-o` | The output path for the mapped artifact (default: `<root>/code-survey.json`). |
45
+ | `--config <file>` | `-c` | Path to a configuration file containing survey option profiles (JSON or YAML). |
46
+ | `--exclude <list>` | `-e` | Comma-separated list of additional file/folder patterns to exclude. |
47
+ | `--format <type>` | `-f` | Output format: `json`, `yaml`, `markdown`, or `mermaid` (default: inferred from output path). |
48
+ | `--internal-vars` | | Include internal/local variables in the mapping (default: `false`). |
49
+ | `--include-docs` | | Extract docstring/JSDoc summaries for class/function symbols (default: `false`). |
50
+ | `--include-signatures` | | Extract function/method parameters and return types (default: `false`). |
51
+ | `--include-toc` | | Generate a Table of Contents and navigation links in Markdown format (default: `false`). |
52
+ | `--split` | | Split output into multiple modular files (default: `false`). If enabled, `--output` is treated as a directory. |
53
+ | `--max-depth <n>` | | Maximum directory traversal depth (default: unlimited). |
54
+ | `--symbols-filter <list>`| | Comma-separated list of symbol types to keep (e.g. `class,method`) (default: all). |
55
+ | `--diff <target>` | | Generate map only for files changed since `<target>` (e.g. `main` or `HEAD`). |
56
+ | `--watch` | `-w` | Watch for file changes and automatically regenerate surveys (default: `false`). |
57
+ | `--mcp` | | Run the built-in MCP (Model Context Protocol) stdio server (default: `false`). |
58
+ | `--help` | `-h` | Show CLI help message. |
59
+
60
+ ---
61
+
62
+ ## Use Cases & Recipe Matrix
63
+
64
+ Depending on the task, you can mix and match output formats and options. Below is a guide on when to use each configuration:
65
+
66
+ ### 1. Repository Onboarding & Architecture Discovery
67
+ * **Use Case:** Feeding an AI agent a high-level structure of a new codebase for the first time.
68
+ * **Goal:** Maximize high-level layout context while minimizing token cost.
69
+ * **Recommended Options:** `--format markdown --include-toc`
70
+ * **CLI Example:**
71
+ ```bash
72
+ code-survey -f markdown --include-toc -o code-survey.md
73
+ ```
74
+ * **Why it works:** The custom Markdown format strips away structural JSON syntax (braces, quotes, commas), resulting in the lowest baseline token footprint (~2k tokens). Adding `--include-toc` generates a clickable table of contents and "Back to Top" links, allowing the agent to navigate modular files directly using markdown anchor targets.
75
+
76
+ ### 2. Deep Refactoring & Function Editing
77
+ * **Use Case:** The agent is writing or modifying functions and needs a detailed map of all local variables, parameters, and API docstrings.
78
+ * **Goal:** High fidelity internal information.
79
+ * **Recommended Options:** `--format yaml --internal-vars --include-docs`
80
+ * **CLI Example:**
81
+ ```bash
82
+ code-survey -f yaml --internal-vars --include-docs -o code-survey.yaml
83
+ ```
84
+ * **Why it works:** YAML is machine-parsable for the agent while remaining 36% smaller than JSON. Toggling `--internal-vars` maps block-scoped local variables, and `--include-docs` injects comments so the agent understands implementation context.
85
+
86
+ ### 3. Laser-Focused Feature Development (Git Diffs)
87
+ * **Use Case:** The agent is fixing a bug on a branch or preparing a PR, and only needs context on files they are changing.
88
+ * **Goal:** Zero token wastage on unrelated files.
89
+ * **Recommended Options:** `--diff main --format markdown` (or `--diff HEAD`)
90
+ * **CLI Example:**
91
+ ```bash
92
+ code-survey --diff main -f markdown -o code-survey.md
93
+ ```
94
+ * **Why it works:** This command queries Git and maps only modified or staged files. Instead of a 15 KB map, it creates a targeted map (often under 1 KB), drastically keeping the agent's context window clean.
95
+
96
+ ### 4. Dependency & Module Coupling Analysis
97
+ * **Use Case:** Visualizing module dependencies or helping the agent analyze architectural coupling.
98
+ * **Goal:** Clear visual depiction of imports.
99
+ * **Recommended Options:** `--format mermaid`
100
+ * **CLI Example:**
101
+ ```bash
102
+ code-survey -f mermaid -o code-survey.mermaid
103
+ ```
104
+ * **Why it works:** Generates standard Mermaid.js syntax that the agent can read to identify circular imports or module boundaries. It can also be pasted directly into markdown viewers to render visual flowcharts.
105
+
106
+ ### 5. Large Codebases & Monorepos
107
+ * **Use Case:** Mapping massive repositories where parsing everything would exceed token limits.
108
+ * **Goal:** Limit size while maintaining structure.
109
+ * **Recommended Options:** `--max-depth 2 --symbols-filter class,interface --format markdown`
110
+ * **CLI Example:**
111
+ ```bash
112
+ code-survey --max-depth 2 --symbols-filter class,interface -f markdown -o code-survey.md
113
+ ```
114
+ * **Why it works:** Restricting `--max-depth 2` stops traversal deep inside folders (like third-party directories or deep utility folders). Limiting `--symbols-filter class,interface` removes functions and methods, mapping out only the top-level architecture skeleton.
115
+
116
+ ---
117
+
118
+ ## Programmatic Usage
119
+
120
+ You can also import and run `createCodeSurvey` directly in your TypeScript/JavaScript projects:
121
+
122
+ ```typescript
123
+ import { createCodeSurvey } from '@loganprice/code-survey';
124
+
125
+ await createCodeSurvey({
126
+ root: './my-project',
127
+ output: './my-project/code-survey.md',
128
+ excludes: ['node_modules', 'dist'],
129
+ format: 'markdown',
130
+ includeInternalVars: false,
131
+ includeDocs: true,
132
+ includeToc: true,
133
+ maxDepth: 3,
134
+ symbolsFilter: ['class', 'interface', 'method', 'function'],
135
+ diff: 'main'
136
+ });
137
+ ```
138
+
139
+ ---
140
+
141
+ ## Benchmarks & Token Efficiency
142
+
143
+ To optimize feeding maps into LLM coding agents, we analyzed the token usage and file size differences of maps generated on this codebase using different formatting options and symbol boundaries:
144
+
145
+ | Output Format | Location Format | Docs? | Internal Vars Included? | File Size | Est. Tokens | Size Savings |
146
+ | :--- | :--- | :--- | :--- | :--- | :--- | :--- |
147
+ | **JSON** | Object (`{start, end}`) | No | Yes (original baseline) | `73.2 KB` | ~21,000 – 24,000 | Baseline |
148
+ | **YAML** | Object (`{start, end}`) | No | Yes | `46.7 KB` | ~11,000 – 13,000 | **-36.2%** |
149
+ | **Markdown** | String (`Ln X-Y`) | No | Yes | `20.7 KB` | ~4,200 – 4,700 | **-71.7%** |
150
+ | **YAML** | String (`Ln X-Y`) | No | No (optimized default) | `15.8 KB` | ~3,900 – 4,300 | **-78.4%** |
151
+ | **Markdown** | String (`Ln X-Y`) | No | No (optimized default) | **`10.1 KB`** | **~2,200 – 2,500** | **-86.2%** |
152
+ | **Markdown** | String (`Ln X-Y`) | **Yes** | No | **`10.5 KB`** | **~2,400 – 2,600** | **-85.6%** |
153
+ | **Mermaid** | Node IDs | No | No | **`1.3 KB`** | **~300 – 400** | **-98.2%** |
154
+
155
+ ### Key Takeaways
156
+ 1. **JSON to YAML Conversion:** Saves around **36%** in raw characters.
157
+ 2. **Compact Locations (`Ln X-Y`) & Variable Pruning:** By removing internal function variables, we strip away redundant implementation details, cutting size by over **70%**.
158
+ 3. **Custom Markdown Format:** Offers the ultimate token density. By stripping away structural syntax (braces, quotes, commas), a Markdown map uses **nearly 7x fewer tokens** than the original JSON output, saving upwards of **20,000 tokens** per LLM call!
159
+
160
+ ---
161
+
162
+ ## MCP Server Integration
163
+
164
+ `code-survey` includes a built-in Model Context Protocol (MCP) server that conforms to the Model Context Protocol stdio transport specification. This allows coding assistants (such as Claude Desktop) to dynamically query the codebase rather than feeding in large static files.
165
+
166
+ ### Configuration
167
+
168
+ Add `code-survey` to your Claude Desktop configuration file (typically located at `~/Library/Application Support/Claude/claude_desktop_config.json` on macOS):
169
+
170
+ ```json
171
+ {
172
+ "mcpServers": {
173
+ "code-survey": {
174
+ "command": "node",
175
+ "args": [
176
+ "--experimental-strip-types",
177
+ "/absolute/path/to/code-survey/bin/code-survey.ts",
178
+ "--mcp"
179
+ ]
180
+ }
181
+ }
182
+ }
183
+ ```
184
+
185
+ ### Exposed Tools
186
+
187
+ - `get_codebase_survey`: Generates and returns the complete codebase survey data, including project details, file mappings, dependencies, and syntax symbols.
188
+ - `search_symbols`: Searches for matching symbols (classes, functions, methods, variables, types) across the codebase.
189
+ - `get_file_symbols`: Retrieves parsed symbol structure, imports, and exports for a specific file relative to the codebase root.
190
+ - `get_dependencies`: Retrieves project-level external/npm and package dependencies.
191
+