@vndv/pi-codegraph 0.1.0 → 0.1.2

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/README.md CHANGED
@@ -1,42 +1,65 @@
1
1
  # pi-codegraph
2
+ ### CodeGraph tools for pi
2
3
 
3
- Pi package that adds CodeGraph tools to Pi Agent.
4
+ Install · Usage · How it works
4
5
 
5
- ## Requirements
6
+ Ask pi structural questions about your codebase without falling back to slow grep/read loops.
6
7
 
7
- Node.js 22 LTS is recommended. CodeGraph blocks Node.js 25 because of a V8 WASM JIT bug in tree-sitter grammar compilation.
8
+ An extension for [pi](https://pi.dev) that gives the agent access to [CodeGraph](https://github.com/colbymchenry/codegraph) tools. CodeGraph indexes your project with tree-sitter, then pi can query symbols, callers, callees, dependency impact, files, and call paths through native extension tools.
8
9
 
9
- Pi provides the package extension runtime and core libraries. This package declares `@earendil-works/pi-coding-agent` and `typebox` as peer dependencies, as required by Pi package loading.
10
+ ---
10
11
 
11
- CodeGraph must already be installed and available on `PATH`:
12
+ ## Quick start
12
13
 
13
14
  ```bash
14
15
  npm install -g @colbymchenry/codegraph
16
+ cd /path/to/project
17
+ codegraph init -i
18
+ pi install npm:@vndv/pi-codegraph@0.1.1
19
+ pi
15
20
  ```
16
21
 
17
- Projects must be indexed before Pi can query them:
22
+ Then ask:
18
23
 
19
- ```bash
20
- cd /path/to/project
21
- codegraph init -i
24
+ ```text
25
+ Use CodeGraph. Show project structure and main entry points.
22
26
  ```
23
27
 
28
+ ---
29
+
30
+ ## What's included
31
+
32
+ Extension tools only. There is no MCP setup for pi users to maintain.
33
+
34
+ | Tool | Description |
35
+ | --- | --- |
36
+ | `codegraph_context` | Broad task context: entry points, related symbols, callers, callees, and key code |
37
+ | `codegraph_search` | Symbol search by name |
38
+ | `codegraph_node` | One symbol's signature, location, source, callers, and callees |
39
+ | `codegraph_files` | Indexed file tree |
40
+ | `codegraph_callers` | Functions or methods that call a symbol |
41
+ | `codegraph_callees` | Functions or methods called by a symbol |
42
+ | `codegraph_trace` | Static call path from one symbol to another |
43
+ | `codegraph_impact` | Impact radius for changing a symbol |
44
+ | `codegraph_explore` | Source for several related symbols grouped by file |
45
+ | `codegraph_status` | Index health and pending sync status |
46
+
47
+ ---
48
+
24
49
  ## Install
25
50
 
26
- From GitHub:
51
+ From npm:
27
52
 
28
53
  ```bash
29
- pi install https://github.com/vndv/pi-codegraph
54
+ pi install npm:@vndv/pi-codegraph@0.1.1
30
55
  ```
31
56
 
32
- From npm:
57
+ From GitHub:
33
58
 
34
59
  ```bash
35
- pi install npm:@vndv/pi-codegraph@0.1.0
60
+ pi install https://github.com/vndv/pi-codegraph
36
61
  ```
37
62
 
38
- This works only after `@vndv/pi-codegraph@0.1.0` has been published to npm. If npm returns `404 Not Found`, use the GitHub or local development install until the first npm publish is complete.
39
-
40
63
  Local development install:
41
64
 
42
65
  ```bash
@@ -45,62 +68,141 @@ cd pi-codegraph
45
68
  pi install "$(pwd)"
46
69
  ```
47
70
 
48
- Verify Pi sees the package:
71
+ Then `/reload` in pi, or restart pi.
72
+
73
+ Verify pi sees the package:
49
74
 
50
75
  ```bash
51
76
  pi list
52
77
  ```
53
78
 
54
- ## Uninstall
79
+ ---
55
80
 
56
- Remove the package using the same source shown by `pi list`:
81
+ ## Requirements
82
+
83
+ Node.js 22 LTS is recommended. CodeGraph blocks Node.js 25 because that Node line has a V8 WASM JIT issue that can crash while compiling tree-sitter grammars.
84
+
85
+ CodeGraph must be installed and available on `PATH`:
57
86
 
58
87
  ```bash
59
- pi remove https://github.com/vndv/pi-codegraph
88
+ npm install -g @colbymchenry/codegraph
60
89
  ```
61
90
 
62
- If you installed from npm or a local path, remove that exact entry instead:
91
+ Each project must be indexed before pi can query it:
63
92
 
64
93
  ```bash
65
- pi remove npm:@vndv/pi-codegraph@0.1.0
66
- pi remove /path/to/pi-codegraph
94
+ cd /path/to/project
95
+ codegraph init -i
67
96
  ```
68
97
 
69
- Then start Pi inside an indexed project:
98
+ This package declares `@earendil-works/pi-coding-agent` and `typebox` as peer dependencies because pi provides the extension runtime.
99
+
100
+ ---
101
+
102
+ ## Usage
103
+
104
+ ### 1. Start pi inside an indexed project
70
105
 
71
106
  ```bash
72
107
  cd /path/to/project
73
108
  pi
74
109
  ```
75
110
 
76
- Example prompt:
111
+ ### 2. Ask structural questions
112
+
113
+ Good prompts:
77
114
 
78
115
  ```text
79
- Use CodeGraph. Show project structure and main entry points.
116
+ Use CodeGraph. Explain how authentication reaches the request handler.
117
+ Use CodeGraph. What calls PlanBoostSession?
118
+ Use CodeGraph. What would break if I change UserRepository?
119
+ Use CodeGraph. Show files under internal/services and important symbols.
80
120
  ```
81
121
 
82
- ## Tools
122
+ ### 3. Prefer the right tool
123
+
124
+ Use `codegraph_context` for broad "how does this work?" questions.
125
+
126
+ Use `codegraph_node` when you already know the symbol name.
127
+
128
+ Use `codegraph_trace` for "how does X reach Y?" flow questions.
83
129
 
84
- This package registers:
130
+ Use `codegraph_search` for declarations and symbols, not arbitrary text or constant values.
85
131
 
86
- - `codegraph_search`
87
- - `codegraph_context`
88
- - `codegraph_callers`
89
- - `codegraph_callees`
90
- - `codegraph_impact`
91
- - `codegraph_explore`
92
- - `codegraph_node`
93
- - `codegraph_status`
94
- - `codegraph_files`
95
- - `codegraph_trace`
132
+ ---
96
133
 
97
- Each tool proxies to:
134
+ ## How it works
135
+
136
+ pi extensions are not MCP configuration files. This package registers native pi tools, and each tool proxies one request to CodeGraph's MCP server internally:
98
137
 
99
138
  ```bash
100
139
  codegraph serve --mcp --path <project>
101
140
  ```
102
141
 
103
- For broad code questions, Pi should prefer `codegraph_context`. For known symbols, use `codegraph_node`. Use `codegraph_search` for declaration/symbol lookup, not literal constants or arbitrary text.
142
+ The flow is:
143
+
144
+ ```text
145
+ pi agent
146
+ -> pi-codegraph extension tool
147
+ -> local CodeGraph MCP process
148
+ -> .codegraph/codegraph.db in the current project
149
+ -> structured result back to pi
150
+ ```
151
+
152
+ That means another developer only needs the npm package, the `codegraph` CLI, and an initialized `.codegraph` index in their project. They do not need to edit pi MCP config.
153
+
154
+ ---
155
+
156
+ ## Uninstall
157
+
158
+ Remove the package using the same source shown by `pi list`:
159
+
160
+ ```bash
161
+ pi remove npm:@vndv/pi-codegraph@0.1.1
162
+ ```
163
+
164
+ If you installed from GitHub or a local path, remove that exact entry instead:
165
+
166
+ ```bash
167
+ pi remove https://github.com/vndv/pi-codegraph
168
+ pi remove /path/to/pi-codegraph
169
+ ```
170
+
171
+ Then `/reload` in pi, or restart pi.
172
+
173
+ ---
174
+
175
+ ## Troubleshooting
176
+
177
+ ### `codegraph_*` tools are missing
178
+
179
+ Check that pi installed the package:
180
+
181
+ ```bash
182
+ pi list
183
+ ```
184
+
185
+ Then reload or restart pi.
186
+
187
+ ### CodeGraph says the project is not initialized
188
+
189
+ Run:
190
+
191
+ ```bash
192
+ cd /path/to/project
193
+ codegraph init -i
194
+ ```
195
+
196
+ ### Node.js version is unsupported
197
+
198
+ Use Node.js 22 LTS:
199
+
200
+ ```bash
201
+ nvm install 22
202
+ nvm use 22
203
+ ```
204
+
205
+ ---
104
206
 
105
207
  ## Development
106
208
 
@@ -109,34 +211,64 @@ npm ci
109
211
  npm run ci
110
212
  ```
111
213
 
112
- ## Release
214
+ Install the local checkout into pi:
215
+
216
+ ```bash
217
+ pi install /Users/vndv/Documents/programming/open-source/pi-codegraph
218
+ ```
113
219
 
114
- Run the full local package check before publishing:
220
+ Before opening a pull request:
115
221
 
116
222
  ```bash
117
223
  npm run ci
118
- npm pack --dry-run
119
224
  ```
120
225
 
121
- First npm publish:
226
+ ---
227
+
228
+ ## Release
229
+
230
+ The package is published to npm as `@vndv/pi-codegraph`.
231
+
232
+ Releases are automated with Changesets. Any package update, including README changes shipped to npm, needs a changeset so the release workflow can bump the version and deploy a new npm package.
233
+
234
+ For every user-facing change, add a changeset in the feature branch:
235
+
236
+ ```bash
237
+ npx changeset
238
+ ```
239
+
240
+ Choose the bump type:
241
+
242
+ - `patch` for fixes and docs that affect package users
243
+ - `minor` for new tools or behavior
244
+ - `major` for breaking changes
245
+
246
+ After the PR is merged to `main`, the release workflow opens or updates a `chore: version packages` pull request. That PR contains the version bump and changelog update.
247
+
248
+ Merge the version PR to publish to npm automatically.
249
+
250
+ The workflow runs:
122
251
 
123
252
  ```bash
124
- npm login
125
- npm publish --access public
253
+ npm run version-packages
254
+ npm run publish-packages
126
255
  ```
127
256
 
128
- Future releases use Changesets:
257
+ `publish-packages` runs `npm publish --access public --provenance`.
258
+
259
+ The workflow uses npm trusted publishing through GitHub Actions OIDC.
260
+
261
+ Local release commands are still available when needed:
129
262
 
130
263
  ```bash
131
- npx changeset
264
+ npm run ci
132
265
  npm run local-release
133
266
  ```
134
267
 
135
- GitHub publishing is also supported:
268
+ GitHub Actions needs npm trusted publishing configured for `.github/workflows/publish.yml`.
269
+
270
+ ---
136
271
 
137
- 1. Add `NPM_TOKEN` repository secret in GitHub.
138
- 2. Update the package version with Changesets or by editing `package.json`.
139
- 3. Commit and push to `main`.
140
- 4. Create a GitHub release for the version tag.
272
+ ## License
141
273
 
142
- The publish workflow runs `npm publish --provenance`.
274
+ MIT
@@ -1,4 +1,6 @@
1
1
  import { spawn } from "node:child_process";
2
+ import { stat } from "node:fs/promises";
3
+ import path from "node:path";
2
4
  import { pathToFileURL } from "node:url";
3
5
  import type { ChildProcessWithoutNullStreams } from "node:child_process";
4
6
  import type { Static } from "typebox";
@@ -134,6 +136,8 @@ type ToolName = (typeof ToolDefinitions)[number]["name"];
134
136
  type ToolParams = Record<string, unknown> & { projectPath?: string };
135
137
  type JsonRpcRequest = (method: string, params: Record<string, unknown>) => Promise<any>;
136
138
 
139
+ const MaxDiagnosticLength = 1000;
140
+
137
141
  export const codegraphToolNames = ToolDefinitions.map((tool) => tool.name);
138
142
 
139
143
  export async function withCodeGraphMcp<T>(
@@ -141,7 +145,7 @@ export async function withCodeGraphMcp<T>(
141
145
  signal: AbortSignal | undefined,
142
146
  fn: (request: JsonRpcRequest) => Promise<T>,
143
147
  ): Promise<T> {
144
- const cwd = projectPath || process.cwd();
148
+ const cwd = await resolveProjectCwd(projectPath);
145
149
  const child = spawn("codegraph", ["serve", "--mcp", "--path", cwd], {
146
150
  cwd,
147
151
  env: process.env,
@@ -151,6 +155,39 @@ export async function withCodeGraphMcp<T>(
151
155
  return runJsonRpcSession(child, cwd, signal, fn);
152
156
  }
153
157
 
158
+ export async function resolveProjectCwd(projectPath: string | undefined): Promise<string> {
159
+ const cwd = projectPath || process.cwd();
160
+
161
+ if (!path.isAbsolute(cwd)) {
162
+ throw new Error("CodeGraph projectPath must be an absolute path.");
163
+ }
164
+
165
+ let info;
166
+ try {
167
+ info = await stat(cwd);
168
+ } catch {
169
+ throw new Error("CodeGraph projectPath does not exist or is not accessible.");
170
+ }
171
+
172
+ if (!info.isDirectory()) {
173
+ throw new Error("CodeGraph projectPath must point to a directory.");
174
+ }
175
+
176
+ return cwd;
177
+ }
178
+
179
+ export function sanitizeDiagnostic(value: string): string {
180
+ const withoutAnsi = value.replace(/\u001b\[[0-9;]*m/g, "");
181
+ const redacted = withoutAnsi
182
+ .replace(/\b([A-Z0-9_]*(?:TOKEN|SECRET|PASSWORD|API_KEY|APIKEY|AUTH)[A-Z0-9_]*=)\S+/gi, "$1[redacted]")
183
+ .replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, "Bearer [redacted]")
184
+ .replace(/--(?:token|secret|password|api-key|apikey|otp)(?:=|\s+)\S+/gi, "--[redacted]");
185
+
186
+ return redacted.length > MaxDiagnosticLength
187
+ ? `${redacted.slice(0, MaxDiagnosticLength)}...`
188
+ : redacted;
189
+ }
190
+
154
191
  async function runJsonRpcSession<T>(
155
192
  child: ChildProcessWithoutNullStreams,
156
193
  cwd: string,
@@ -211,7 +248,8 @@ async function runJsonRpcSession<T>(
211
248
 
212
249
  child.on("exit", (code) => {
213
250
  if (pending.size === 0) return;
214
- const msg = stderr.trim() || `CodeGraph MCP process exited with code ${code}`;
251
+ const diagnostic = sanitizeDiagnostic(stderr.trim());
252
+ const msg = diagnostic || `CodeGraph MCP process exited with code ${code}`;
215
253
  for (const entry of pending.values()) entry.reject(new Error(msg));
216
254
  pending.clear();
217
255
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vndv/pi-codegraph",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "CodeGraph tools for Pi Agent.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -8,7 +8,7 @@
8
8
  "homepage": "https://github.com/vndv/pi-codegraph#readme",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/vndv/pi-codegraph.git"
11
+ "url": "https://github.com/vndv/pi-codegraph"
12
12
  },
13
13
  "bugs": {
14
14
  "url": "https://github.com/vndv/pi-codegraph/issues"
@@ -37,10 +37,12 @@
37
37
  "scripts": {
38
38
  "ci": "npm run typecheck && npm test && npm pack --dry-run",
39
39
  "local-release": "changeset version && changeset publish",
40
+ "publish-packages": "npm publish --access public --provenance",
40
41
  "prepack": "npm run typecheck && npm test",
41
42
  "prepublishOnly": "npm run ci",
42
43
  "typecheck": "tsc --noEmit",
43
- "test": "vitest run"
44
+ "test": "vitest run",
45
+ "version-packages": "changeset version && npm install --package-lock-only --ignore-scripts"
44
46
  },
45
47
  "engines": {
46
48
  "node": ">=22.19.0 <25"