@sdsrs/code-graph 0.177.0 → 0.179.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/claude-plugin/.claude-plugin/plugin.json +1 -1
- package/claude-plugin/scripts/pre-edit-guide.js +124 -9
- package/claude-plugin/scripts/pre-grep-guide.js +3 -2
- package/claude-plugin/scripts/pre-read-guide.js +4 -3
- package/claude-plugin/scripts/project-root.js +39 -1
- package/claude-plugin/scripts/stop-impact.js +79 -16
- package/claude-plugin/templates/code-graph-snapshot.yml +1 -1
- package/package.json +6 -6
|
@@ -22,12 +22,13 @@ const fs = require('fs');
|
|
|
22
22
|
const path = require('path');
|
|
23
23
|
const { findBinary } = require('./find-binary');
|
|
24
24
|
const { cgTmpDir, cwdHash } = require('./tmp-dir');
|
|
25
|
-
const { resolveProjectRoot, indexBuildInProgress } = require('./project-root');
|
|
25
|
+
const { resolveProjectRoot, indexKey, indexBuildInProgress } = require('./project-root');
|
|
26
26
|
const { recordRecommendation } = require('./recommendation-log');
|
|
27
27
|
const { formatCoveringTests } = require('./covering-tests');
|
|
28
28
|
const { emitPreToolContext } = require('./hook-emit');
|
|
29
29
|
const { hidden } = require('./proc-opts');
|
|
30
30
|
const { remainingMs } = require('./hook-fail-open');
|
|
31
|
+
const { definitionHeaders } = require('./stop-impact');
|
|
31
32
|
|
|
32
33
|
// v0.49 — walk up from the shell cwd (subdir-cwd fix). The per-cwd index.db
|
|
33
34
|
// gate kept this hook dark for entire sessions after `cd backend/` — daagu
|
|
@@ -135,19 +136,21 @@ const scanned = oldStr.length > 8192 ? oldStr.slice(0, 8192) : oldStr;
|
|
|
135
136
|
// matches — 0.163.0's rule. Three replacements (the earliest match, then an
|
|
136
137
|
// allow-list of what may precede a method name, then keyword-anchored arms
|
|
137
138
|
// first) each fixed shapes and broke others, and three review rounds found a
|
|
138
|
-
// regression in each (pre-tag review 2026-09-29), so
|
|
139
|
-
//
|
|
139
|
+
// regression in each (pre-tag review 2026-09-29), so this rule still names the
|
|
140
|
+
// pick; the Stop check's per-language reader decides below, where it reads the
|
|
141
|
+
// language, which definition the edit changed.
|
|
140
142
|
//
|
|
141
143
|
// Where the definition starts is its name's line: `// TODO: make this
|
|
142
144
|
// async\nfn f(` matches from the comment line, and the same-file pick below
|
|
143
145
|
// counts lines from here. That moves no name, only the line.
|
|
144
146
|
let symbol = null;
|
|
145
147
|
let symbolAt = -1;
|
|
148
|
+
let nameAt = -1;
|
|
146
149
|
for (const pat of fnPatterns) {
|
|
147
150
|
const m = scanned.match(new RegExp(pat.source, `${pat.flags}d`));
|
|
148
151
|
if (m) {
|
|
149
152
|
symbol = m[1] || m[2];
|
|
150
|
-
|
|
153
|
+
nameAt = (m.indices[1] || m.indices[2])[0];
|
|
151
154
|
symbolAt = Math.max(m.index, scanned.lastIndexOf('\n', nameAt - 1) + 1);
|
|
152
155
|
break;
|
|
153
156
|
}
|
|
@@ -179,7 +182,107 @@ function headerAt(text, at) {
|
|
|
179
182
|
.filter((i) => i >= 0);
|
|
180
183
|
return ends.length ? rest.slice(0, Math.min(...ends)) : rest.split('\n', 1)[0];
|
|
181
184
|
}
|
|
182
|
-
|
|
185
|
+
const bodyOnly = newStr.includes(headerAt(scanned, symbolAt));
|
|
186
|
+
|
|
187
|
+
// Which definition the edit changed, where the Stop check's per-language reader
|
|
188
|
+
// (stop-impact.js LANGS) reads this file: a definition of the hunk whose
|
|
189
|
+
// signature key the new text no longer has. The pick above sent 35 of its 103
|
|
190
|
+
// impact queries for names that are no definition there (a Rust `match f(…)
|
|
191
|
+
// {`, a comment's "function already", a string) and missed 24 changed headers
|
|
192
|
+
// (a later definition, a `{` inside the parameters, an old header still inside
|
|
193
|
+
// the new one), measured on every Edit hunk in this machine's transcripts
|
|
194
|
+
// (tasks/specs/pre-edit-pick-by-reader.md).
|
|
195
|
+
// - a changed definition of the picked name → it, placed where it starts;
|
|
196
|
+
// - the pick's header changed as the first-match rule reads it, and the pick
|
|
197
|
+
// is not proven no definition → the pick, as before. The reader may not
|
|
198
|
+
// see it (a method whose parameters span lines, `#[inline] fn` on one
|
|
199
|
+
// line); where it reads it unchanged, the old reading can have run into
|
|
200
|
+
// the next definition, which 0.178.0 did too;
|
|
201
|
+
// - else the first changed definition, logged for the Stop check too;
|
|
202
|
+
// - else none.
|
|
203
|
+
// The pick is no definition when its line opens a comment that has not closed
|
|
204
|
+
// before it, or, in Rust and Python, when its name does not follow `fn` /
|
|
205
|
+
// `def`. A comment here is not stop-impact.js's COMMENT_LINE, which takes any
|
|
206
|
+
// line opening with `*` or `#`: `*entries(prefix) {` is a JS generator method,
|
|
207
|
+
// `#[inline] pub fn` a Rust definition; a doc comment's `*` is followed by a
|
|
208
|
+
// space, a `/` or the line's end.
|
|
209
|
+
//
|
|
210
|
+
// Only in the extensions of the reader's languages that corpus had hunks in
|
|
211
|
+
// (not C: 2 hunks). Elsewhere the reader misread shapes no hunk there had
|
|
212
|
+
// (pre-tag review of 0.179.0: Kotlin's `fun Config.validate(` reads as
|
|
213
|
+
// `Config`, PHP's `function &name(` has no keyword before the name, a C++
|
|
214
|
+
// statement `Type name(args);` reads as a definition), so the pick keeps its
|
|
215
|
+
// own verdict, as in 0.178.0; so does a hunk the reader has no reading of (a
|
|
216
|
+
// header past its cap).
|
|
217
|
+
const READER_MEASURED = new Set(['.rs', '.py', '.js', '.mjs', '.cjs', '.ts']);
|
|
218
|
+
const OPENS_A_COMMENT = /^\s*(?:\/\/|\/\*|\*(?=\s|\/|$)|#(?!\[)|--|;)/;
|
|
219
|
+
function onComment(line, nameCol) {
|
|
220
|
+
const closed = /^\s*(?:\/\*.*?\*\/\s*)+/.exec(line.slice(0, nameCol));
|
|
221
|
+
return OPENS_A_COMMENT.test(closed ? line.slice(closed[0].length) : line);
|
|
222
|
+
}
|
|
223
|
+
function changedDefinition() {
|
|
224
|
+
const ext = path.extname(editedAbs).toLowerCase();
|
|
225
|
+
const before = READER_MEASURED.has(ext) ? definitionHeaders(oldStr, ext) : null;
|
|
226
|
+
const after = before && definitionHeaders(newStr, ext);
|
|
227
|
+
if (!before || !after) return bodyOnly ? null : { name: symbol, at: symbolAt };
|
|
228
|
+
const count = (defs) => {
|
|
229
|
+
const m = new Map();
|
|
230
|
+
for (const d of defs) m.set(`${d.name}\0${d.key}`, (m.get(`${d.name}\0${d.key}`) || 0) + 1);
|
|
231
|
+
return m;
|
|
232
|
+
};
|
|
233
|
+
const has = count(after.defs);
|
|
234
|
+
const left = new Map(has);
|
|
235
|
+
let changed = [];
|
|
236
|
+
for (const d of before.defs) {
|
|
237
|
+
const k = `${d.name}\0${d.key}`;
|
|
238
|
+
if (left.get(k) > 0) left.set(k, left.get(k) - 1);
|
|
239
|
+
else changed.push(d);
|
|
240
|
+
}
|
|
241
|
+
// The count matches headers, not definitions: of two definitions of one name
|
|
242
|
+
// with one old header, one kept, it marks the later, whichever the edit
|
|
243
|
+
// changed. Such a name's definitions pair in order when the hunk keeps their
|
|
244
|
+
// number and pairing finds as many changed as the count; otherwise which one
|
|
245
|
+
// changed is not in the text (one went or came, or the hunk moved them), and
|
|
246
|
+
// the reader names none of that name (the rule below can still keep the
|
|
247
|
+
// pick).
|
|
248
|
+
const twinned = new Set(changed
|
|
249
|
+
.filter((c) => (has.get(`${c.name}\0${c.key}`) || 0) > 0)
|
|
250
|
+
.map((c) => c.name));
|
|
251
|
+
if (twinned.size) {
|
|
252
|
+
const counted = new Map();
|
|
253
|
+
for (const d of changed) if (twinned.has(d.name)) counted.set(d.name, (counted.get(d.name) || 0) + 1);
|
|
254
|
+
const byName = (defs) => {
|
|
255
|
+
const m = new Map();
|
|
256
|
+
for (const d of defs) if (twinned.has(d.name)) (m.get(d.name) || m.set(d.name, []).get(d.name)).push(d);
|
|
257
|
+
return m;
|
|
258
|
+
};
|
|
259
|
+
const olds = byName(before.defs);
|
|
260
|
+
const news = byName(after.defs);
|
|
261
|
+
changed = changed.filter((d) => !twinned.has(d.name));
|
|
262
|
+
for (const [name, o] of olds) {
|
|
263
|
+
const n = news.get(name) || [];
|
|
264
|
+
const paired = o.length === n.length ? o.filter((d, i) => d.key !== n[i].key) : [];
|
|
265
|
+
if (paired.length === counted.get(name)) changed.push(...paired);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
changed.sort((x, y) => x.at - y.at);
|
|
269
|
+
const same = changed.find((d) => d.name === symbol);
|
|
270
|
+
if (same) return { name: symbol, at: same.at };
|
|
271
|
+
const lineStart = scanned.lastIndexOf('\n', nameAt - 1) + 1;
|
|
272
|
+
const lineEnd = scanned.indexOf('\n', nameAt);
|
|
273
|
+
const pickLine = scanned.slice(lineStart, lineEnd === -1 ? scanned.length : lineEnd);
|
|
274
|
+
const notADefinition = onComment(pickLine, nameAt - lineStart)
|
|
275
|
+
|| (before.keywordOnly && !before.keywordLeads(scanned.slice(lineStart, nameAt)));
|
|
276
|
+
if (!bodyOnly && !notADefinition) return { name: symbol, at: symbolAt };
|
|
277
|
+
return changed.length ? { name: changed[0].name, at: changed[0].at } : null;
|
|
278
|
+
}
|
|
279
|
+
const edited = changedDefinition();
|
|
280
|
+
if (!edited) process.exit(0);
|
|
281
|
+
if (edited.name !== symbol) {
|
|
282
|
+
symbol = edited.name;
|
|
283
|
+
logEdit(symbol);
|
|
284
|
+
}
|
|
285
|
+
symbolAt = edited.at;
|
|
183
286
|
|
|
184
287
|
if (process.env.CODE_GRAPH_QUIET_HOOKS === '1') process.exit(0);
|
|
185
288
|
|
|
@@ -203,8 +306,10 @@ if (indexBuildInProgress(cwd)) process.exit(0);
|
|
|
203
306
|
// short generic symbol names (open, new, create, parse, from, init) resolve
|
|
204
307
|
// to a unique node instead of triggering the CLI's "Ambiguous symbol" error
|
|
205
308
|
// path, which previously caused silent exits for the most common edit cases.
|
|
309
|
+
// D#560 — in a linked worktree the index knows the file by its path from the
|
|
310
|
+
// worktree, not from the main checkout `cwd` resolved to.
|
|
206
311
|
const editedFile = (input.tool_input && input.tool_input.file_path) || '';
|
|
207
|
-
const relFile = editedFile ? path.relative(cwd, editedFile) : '';
|
|
312
|
+
const relFile = editedFile ? indexKey(cwd, path.relative(cwd, editedFile)) : '';
|
|
208
313
|
// Whatever is left of the registered 4 s, capped at this call's own 2500 ms.
|
|
209
314
|
// `null` = the candidate loop above already spent the budget; running anyway is
|
|
210
315
|
// what got the hook killed by Claude Code mid-Edit (audit 2026-09-05 JS-03).
|
|
@@ -234,16 +339,26 @@ function runImpact(args) {
|
|
|
234
339
|
// the old_string is not in the file (the Edit will fail) or no candidate fits.
|
|
235
340
|
// The CLI lists at most five, in source order, and says how many exist in
|
|
236
341
|
// `total`: past the last listed one an unlisted definition may start, so an
|
|
237
|
-
// edit there is not guessed at (pre-tag review 2026-09-29).
|
|
342
|
+
// edit there is not guessed at (pre-tag review 2026-09-29). The line is
|
|
343
|
+
// counted in the copy the index describes, whose start lines the candidates
|
|
344
|
+
// carry: in a worktree, the main checkout's. Counted in the worktree's copy, a
|
|
345
|
+
// header whose lines had moved named another same-named definition's callers
|
|
346
|
+
// (review of D#560). A copy without the edited text gets no guess, and in a
|
|
347
|
+
// worktree nor does one that holds it twice: the main checkout's copy is not
|
|
348
|
+
// the edited one, so which occurrence matches the edited definition cannot be
|
|
349
|
+
// told (review of 0.178.0). In the main checkout that is a `replace_all` Edit,
|
|
350
|
+
// and the first occurrence is one of the definitions it changes.
|
|
238
351
|
function editedNodeId(suggestions, fileKey, total) {
|
|
352
|
+
const indexedKey = indexKey(cwd, fileKey);
|
|
239
353
|
let text;
|
|
240
|
-
try { text = fs.readFileSync(path.join(cwd,
|
|
354
|
+
try { text = fs.readFileSync(path.join(cwd, indexedKey), 'utf8'); } catch { return null; }
|
|
241
355
|
const at = text.indexOf(oldStr);
|
|
242
356
|
if (at === -1) return null;
|
|
357
|
+
if (indexedKey !== fileKey && text.indexOf(oldStr, at + 1) !== -1) return null;
|
|
243
358
|
const line = text.slice(0, at + symbolAt).split('\n').length;
|
|
244
359
|
let best = null;
|
|
245
360
|
for (const c of suggestions) {
|
|
246
|
-
if (!c || c.file_path !==
|
|
361
|
+
if (!c || c.file_path !== indexedKey || !Number.isInteger(c.node_id) || !Number.isInteger(c.start_line)) continue;
|
|
247
362
|
if (c.start_line <= line && (best === null || c.start_line > best.start_line)) best = c;
|
|
248
363
|
}
|
|
249
364
|
const lastListed = Math.max(...suggestions.map((c) => (c && Number.isInteger(c.start_line) ? c.start_line : -Infinity)));
|
|
@@ -751,7 +751,7 @@ function rootOnlyInSearchPath(cmd, cwd, target) {
|
|
|
751
751
|
|
|
752
752
|
// v0.48 — subdir-cwd fix; v0.49 — extracted to project-root.js so the read
|
|
753
753
|
// hook shares it. Re-exported below for test/back-compat.
|
|
754
|
-
const { resolveProjectRoot } = require('./project-root');
|
|
754
|
+
const { resolveProjectRoot, indexKey } = require('./project-root');
|
|
755
755
|
|
|
756
756
|
// v0.48 — companion to resolveProjectRoot: when the shell sits in a subdir,
|
|
757
757
|
// bare relative path args (`app --include=*.py` from backend/) are
|
|
@@ -2293,7 +2293,8 @@ function runMain() {
|
|
|
2293
2293
|
for (const t of sedTargets) {
|
|
2294
2294
|
if (!readGuide.isSourceFile(t)) continue;
|
|
2295
2295
|
const abs = path.isAbsolute(t) ? t : path.resolve(shellCwd, t);
|
|
2296
|
-
|
|
2296
|
+
// D#560 — a linked worktree's copy counts toward the indexed directory.
|
|
2297
|
+
const dir = readGuide.trackRead(root, indexKey(root, path.relative(root, abs)));
|
|
2297
2298
|
if (dir !== null) firedDirs.push(dir); // a dir fires once: markHint starts its cooldown
|
|
2298
2299
|
}
|
|
2299
2300
|
if (firedDirs.length > 0) {
|
|
@@ -36,7 +36,7 @@ const path = require('path');
|
|
|
36
36
|
const crypto = require('crypto');
|
|
37
37
|
const { cgTmpDir } = require('./tmp-dir');
|
|
38
38
|
const { recordRecommendation } = require('./recommendation-log');
|
|
39
|
-
const { resolveProjectRoot, indexBuildInProgress } = require('./project-root');
|
|
39
|
+
const { resolveProjectRoot, indexKey, indexBuildInProgress } = require('./project-root');
|
|
40
40
|
const { runOverviewAnswer } = require('./cg-answer');
|
|
41
41
|
const { emitPreToolAllowContext } = require('./hook-emit');
|
|
42
42
|
|
|
@@ -255,10 +255,11 @@ function runMain() {
|
|
|
255
255
|
if (!isSourceFile(filePath)) return;
|
|
256
256
|
|
|
257
257
|
// Normalize to a root-relative path. Read sends absolute paths; files
|
|
258
|
-
// outside the project (other repos, ~/.claude/) stay silent.
|
|
258
|
+
// outside the project (other repos, ~/.claude/) stay silent. A linked
|
|
259
|
+
// worktree's copy counts toward the directory the index knows (D#560).
|
|
259
260
|
let rel;
|
|
260
261
|
try {
|
|
261
|
-
rel = path.relative(root, filePath);
|
|
262
|
+
rel = indexKey(root, path.relative(root, filePath));
|
|
262
263
|
} catch { return; }
|
|
263
264
|
|
|
264
265
|
const hint = trackReadAndMaybeHint(root, rel);
|
|
@@ -124,6 +124,44 @@ function resolveProjectRoot(startDir, opts = {}) {
|
|
|
124
124
|
return null;
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
+
function samePath(a, b) {
|
|
128
|
+
if (path.resolve(a) === path.resolve(b)) return true;
|
|
129
|
+
try { return fs.realpathSync(a) === fs.realpathSync(b); } catch { return false; }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// The part of `key` (a path from `root`) that leads to the top of a linked
|
|
133
|
+
// worktree of `root` the key is in, separator included; '' when there is none.
|
|
134
|
+
// Only a worktree under a hidden directory counts (Claude Code's
|
|
135
|
+
// `.claude/worktrees/<slug>`): the indexer skips hidden directories, so the
|
|
136
|
+
// index holds none of its files under their path from `root`. A worktree
|
|
137
|
+
// elsewhere in the project is indexed under that path unless ignored, and
|
|
138
|
+
// stripping it asked for the main checkout's copy instead (review of D#560).
|
|
139
|
+
// Only worktrees inside `root` are seen: a key leaving it (`../wt/…`) is
|
|
140
|
+
// outside the project to the hooks.
|
|
141
|
+
function worktreePrefix(root, key) {
|
|
142
|
+
if (!root || !key || key.startsWith('..') || path.isAbsolute(key)) return '';
|
|
143
|
+
const sep = path.sep === '\\' ? /[\\/]/g : /\//g;
|
|
144
|
+
const ends = [...key.matchAll(sep)].map((m) => m.index).concat(key.length);
|
|
145
|
+
for (const end of ends) {
|
|
146
|
+
const dir = key.slice(0, end);
|
|
147
|
+
if (!dir.split(sep).some((part) => part.startsWith('.'))) continue;
|
|
148
|
+
const main = worktreeMainRoot(path.join(root, dir));
|
|
149
|
+
if (main && samePath(main, root)) return end < key.length ? key.slice(0, end + 1) : `${key}/`;
|
|
150
|
+
}
|
|
151
|
+
return '';
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// The key the index knows a file by, given its key from `root`. A worktree
|
|
155
|
+
// with no index of its own resolves to its main checkout (above), whose index
|
|
156
|
+
// stores the main checkout's layout, so a worktree's copy is known by its path
|
|
157
|
+
// from the worktree's top. Built from `root`, every such key carried the
|
|
158
|
+
// worktree's prefix: `impact --file .claude/worktrees/x/src/a.js` was refused,
|
|
159
|
+
// and the pre-edit impact went silent for every edit in an isolated agent's
|
|
160
|
+
// worktree (D#560). Any other key comes back unchanged, separators included.
|
|
161
|
+
function indexKey(root, key) {
|
|
162
|
+
return key ? key.slice(worktreePrefix(root, key).length) : key;
|
|
163
|
+
}
|
|
164
|
+
|
|
127
165
|
// True while the MCP server's startup index is still being written for `root`.
|
|
128
166
|
// The server writes `.code-graph/indexing-status.json` ({s, d, t}) and heartbeats
|
|
129
167
|
// it per batch and per finalize phase, then removes it. Until then the index is
|
|
@@ -163,4 +201,4 @@ function writerAlive(p) {
|
|
|
163
201
|
}
|
|
164
202
|
}
|
|
165
203
|
|
|
166
|
-
module.exports = { resolveProjectRoot, indexBuildInProgress, writerAlive, INDEXING_STALE_MS };
|
|
204
|
+
module.exports = { resolveProjectRoot, indexKey, worktreePrefix, indexBuildInProgress, writerAlive, INDEXING_STALE_MS };
|
|
@@ -89,12 +89,17 @@ const NOT_A_TYPE = new Set([
|
|
|
89
89
|
// initializers, C# `: base(…)` — body, not signature)
|
|
90
90
|
// closeParen the parameter list's `)` ends it (Ruby, Lua: no return types,
|
|
91
91
|
// and a one-line body may follow on the same line)
|
|
92
|
+
// keywordOnly every function definition's name follows `keyword`, so a
|
|
93
|
+
// name without it is none (pre-edit-guide reads that; Rust and
|
|
94
|
+
// Python only, the languages it was measured on. These rules do not
|
|
95
|
+
// find every such definition: a line opening with `#[` or `/* … */`,
|
|
96
|
+
// a second one on a line)
|
|
92
97
|
const LANGS = new Map();
|
|
93
98
|
function defineLang(exts, profile) {
|
|
94
99
|
for (const e of exts) LANGS.set(e, profile);
|
|
95
100
|
}
|
|
96
|
-
defineLang(['.rs'], { keyword: 'fn', ends: '{;' });
|
|
97
|
-
defineLang(['.py', '.pyi'], { keyword: 'def', ends: ':' });
|
|
101
|
+
defineLang(['.rs'], { keyword: 'fn', ends: '{;', keywordOnly: true });
|
|
102
|
+
defineLang(['.py', '.pyi'], { keyword: 'def', ends: ':', keywordOnly: true });
|
|
98
103
|
defineLang(['.go'], { keyword: 'func', receiver: true, ends: '{;\n' });
|
|
99
104
|
defineLang(['.js', '.jsx', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts'],
|
|
100
105
|
{ keyword: 'function\\*?', jsForms: true, ends: '{;', arrowEnds: true, angles: true });
|
|
@@ -110,12 +115,18 @@ function escapeRe(s) {
|
|
|
110
115
|
return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
111
116
|
}
|
|
112
117
|
|
|
118
|
+
// What comes before a definition's name: the keyword, Go's receiver, Kotlin's
|
|
119
|
+
// type parameters.
|
|
120
|
+
function keywordLead(lang) {
|
|
121
|
+
const receiver = lang.receiver ? '(?:\\([^)\\n]*\\)\\s*)?' : '';
|
|
122
|
+
const typeParams = lang.typeParams ? '(?:<[^>\\n]*>\\s*)?' : '';
|
|
123
|
+
return `\\b(?:${lang.keyword})\\s+${receiver}${typeParams}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
113
126
|
function definitionPatterns(S, lang) {
|
|
114
127
|
const out = [];
|
|
115
128
|
if (lang.keyword) {
|
|
116
|
-
|
|
117
|
-
const typeParams = lang.typeParams ? '(?:<[^>\\n]*>\\s*)?' : '';
|
|
118
|
-
out.push({ re: new RegExp(`\\b(?:${lang.keyword})\\s+${receiver}${typeParams}${S}\\b`, 'g') });
|
|
129
|
+
out.push({ re: new RegExp(`${keywordLead(lang)}${S}\\b`, 'g') });
|
|
119
130
|
}
|
|
120
131
|
if (lang.jsForms) {
|
|
121
132
|
out.push({ re: new RegExp(`\\b(?:const|let|var)\\s+${S}\\s*=\\s*(?:async\\s+)?(?:function\\b|\\([^)\\n]*\\)\\s*=>|[A-Za-z_$][\\w$]*\\s*=>)`, 'g') });
|
|
@@ -183,6 +194,40 @@ function allSignatures(text, ext = '') {
|
|
|
183
194
|
return out;
|
|
184
195
|
}
|
|
185
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Every definition in `text`, in text order, with the line it starts on and
|
|
199
|
+
* its signature key (as allSignatures reads it) — for pre-edit-guide, which
|
|
200
|
+
* must tell which of a hunk's definitions an edit changed and where it sits.
|
|
201
|
+
* `keywordOnly` (LANGS) with `keywordLeads`, which tests the text before a
|
|
202
|
+
* name on its line: where both hold, a name it is false for is no definition.
|
|
203
|
+
* null when there is no exact reading of the whole text: the language is not
|
|
204
|
+
* in LANGS, a definition sits on an over-long line, or any header has no
|
|
205
|
+
* reading.
|
|
206
|
+
* @returns {{ defs: {name:string, at:number, key:string}[], keywordOnly: boolean,
|
|
207
|
+
* keywordLeads: (before: string) => boolean }|null}
|
|
208
|
+
*/
|
|
209
|
+
function definitionHeaders(text, ext = '') {
|
|
210
|
+
const lang = LANGS.get(String(ext).toLowerCase());
|
|
211
|
+
if (!lang || typeof text !== 'string') return null;
|
|
212
|
+
const starts = definitionStarts(text, lang, ANY_NAME, null);
|
|
213
|
+
if (starts === null) return null;
|
|
214
|
+
const defs = [];
|
|
215
|
+
for (const [name, lineStarts] of starts) {
|
|
216
|
+
for (const at of lineStarts) {
|
|
217
|
+
const header = readHeader(text, at, lang);
|
|
218
|
+
if (header === null) return null;
|
|
219
|
+
defs.push({ name, at, key: signatureKey(header) });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
defs.sort((x, y) => x.at - y.at);
|
|
223
|
+
const lead = lang.keyword ? new RegExp(`${keywordLead(lang)}$`) : null;
|
|
224
|
+
return {
|
|
225
|
+
defs,
|
|
226
|
+
keywordOnly: lang.keywordOnly === true,
|
|
227
|
+
keywordLeads: (before) => lead !== null && lead.test(before),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
186
231
|
/**
|
|
187
232
|
* Where definitions start: Map name → Set of line-start offsets. With `symbol`
|
|
188
233
|
* the patterns carry it escaped in `S`; without, `S` is ANY_NAME and the name
|
|
@@ -413,8 +458,14 @@ function findCallSiteLine(text, symbol, startLine) {
|
|
|
413
458
|
* @param {(file:string)=>string|null} a.workText file in the working tree
|
|
414
459
|
* @param {(symbol:string,file:string)=>({file:string,line:number,name:string}[]|null)} a.callers
|
|
415
460
|
* @param {(file:string)=>number|null} a.mtimeMs
|
|
461
|
+
* @param {(file:string)=>string} [a.indexKeyOf] the key the index knows a
|
|
462
|
+
* file by: a linked worktree's copy drops the worktree's prefix (D#560)
|
|
463
|
+
* @param {(file:string)=>string} [a.displayOf] a file as the reader names it:
|
|
464
|
+
* a session working in a worktree reads paths from there (D#560 review)
|
|
416
465
|
*/
|
|
417
|
-
function computeStopReport({
|
|
466
|
+
function computeStopReport({
|
|
467
|
+
edits, state, now, workText, callers, mtimeMs, indexKeyOf = (f) => f, displayOf = (f) => f,
|
|
468
|
+
}) {
|
|
418
469
|
const lastStopAt = state.lastStopAt;
|
|
419
470
|
const turn = edits.filter((e) => lastStopAt === null || e.ts > lastStopAt);
|
|
420
471
|
const next = { lastStopAt: now, reported: [...state.reported] };
|
|
@@ -471,12 +522,12 @@ function computeStopReport({ edits, state, now, workText, callers, mtimeMs }) {
|
|
|
471
522
|
// text from the repository, and the model may paste it into a command.
|
|
472
523
|
const shown = untouched.slice(0, MAX_CALLERS_LISTED).map((r) => {
|
|
473
524
|
const name = r.name && r.name !== '<module>' ? ' (' + shellQuoteArg(r.name) + ')' : '';
|
|
474
|
-
return shellQuoteArg(r.file) + ':' + r.line + name;
|
|
525
|
+
return shellQuoteArg(displayOf(r.file)) + ':' + r.line + name;
|
|
475
526
|
});
|
|
476
|
-
let line = ' ' + shellQuoteArg(c.symbol) + '() in ' + shellQuoteArg(c.file) + ': ' + shown.join(', ');
|
|
527
|
+
let line = ' ' + shellQuoteArg(c.symbol) + '() in ' + shellQuoteArg(displayOf(c.file)) + ': ' + shown.join(', ');
|
|
477
528
|
if (untouched.length > MAX_CALLERS_LISTED) {
|
|
478
529
|
line += ', and ' + (untouched.length - MAX_CALLERS_LISTED) + ' more (' +
|
|
479
|
-
formatCgCommand(['refs', c.symbol, '--file', c.file]) + ')';
|
|
530
|
+
formatCgCommand(['refs', c.symbol, '--file', indexKeyOf(c.file)]) + ')';
|
|
480
531
|
}
|
|
481
532
|
lines.push(line);
|
|
482
533
|
next.reported.push(c.key);
|
|
@@ -488,7 +539,7 @@ function computeStopReport({ edits, state, now, workText, callers, mtimeMs }) {
|
|
|
488
539
|
const unchecked = changed.slice(MAX_SYMBOLS_CHECKED);
|
|
489
540
|
if (unchecked.length > 0) {
|
|
490
541
|
lines.push(' ' + unchecked.length + ' more changed, callers not checked: ' +
|
|
491
|
-
unchecked.map((c) => shellQuoteArg(c.symbol) + '() in ' + shellQuoteArg(c.file)).join(', '));
|
|
542
|
+
unchecked.map((c) => shellQuoteArg(c.symbol) + '() in ' + shellQuoteArg(displayOf(c.file))).join(', '));
|
|
492
543
|
}
|
|
493
544
|
// `symbols` / `files`: what the report names, for its telemetry and its
|
|
494
545
|
// follow-up (D#163). Not part of `state`.
|
|
@@ -562,9 +613,13 @@ function runMain() {
|
|
|
562
613
|
const input = readStdinJson();
|
|
563
614
|
if (!input || !input.session_id) return;
|
|
564
615
|
|
|
565
|
-
const { resolveProjectRoot } = require('./project-root');
|
|
566
|
-
const
|
|
616
|
+
const { resolveProjectRoot, indexKey, worktreePrefix } = require('./project-root');
|
|
617
|
+
const cwd = typeof input.cwd === 'string' && input.cwd ? input.cwd : process.cwd();
|
|
618
|
+
const root = resolveProjectRoot(cwd);
|
|
567
619
|
if (root === null) return;
|
|
620
|
+
// A session working in a linked worktree (EnterWorktree) reads the report
|
|
621
|
+
// from there; files in that worktree are named from its top.
|
|
622
|
+
const shownFrom = worktreePrefix(root, path.relative(root, cwd).split(path.sep).join('/'));
|
|
568
623
|
|
|
569
624
|
const sessionEdits = require('./session-edits');
|
|
570
625
|
const { recordRecommendation } = require('./recommendation-log');
|
|
@@ -620,8 +675,13 @@ function runMain() {
|
|
|
620
675
|
workText: (file) => {
|
|
621
676
|
try { return fs.readFileSync(path.join(root, file), 'utf8'); } catch { return null; }
|
|
622
677
|
},
|
|
678
|
+
// D#560 — an isolated agent's edits are logged by their path through its
|
|
679
|
+
// worktree. The index is asked by the key it knows, and the callers it
|
|
680
|
+
// names are that worktree's copies: the ones the agent's change reaches.
|
|
623
681
|
callers: (symbol, file) => {
|
|
624
|
-
const
|
|
682
|
+
const key = indexKey(root, file);
|
|
683
|
+
const worktree = file.slice(0, file.length - key.length);
|
|
684
|
+
const raw = run(binary, ['refs', symbol, '--file', key, '--relation', 'calls',
|
|
625
685
|
'--min-confidence', 'inferred', '--json'], 1500);
|
|
626
686
|
if (raw === null) return null;
|
|
627
687
|
let parsed;
|
|
@@ -630,12 +690,15 @@ function runMain() {
|
|
|
630
690
|
return parsed.references
|
|
631
691
|
.filter((r) => r && typeof r.file_path === 'string')
|
|
632
692
|
.map((r) => {
|
|
693
|
+
const caller = worktree + r.file_path;
|
|
633
694
|
let text = null;
|
|
634
|
-
try { text = fs.readFileSync(path.join(root,
|
|
635
|
-
return { file:
|
|
695
|
+
try { text = fs.readFileSync(path.join(root, caller), 'utf8'); } catch { /* gone */ }
|
|
696
|
+
return { file: caller, name: r.name, line: findCallSiteLine(text, symbol, r.start_line) };
|
|
636
697
|
});
|
|
637
698
|
},
|
|
638
699
|
mtimeMs,
|
|
700
|
+
indexKeyOf: (file) => indexKey(root, file),
|
|
701
|
+
displayOf: (file) => (shownFrom && file.startsWith(shownFrom) ? file.slice(shownFrom.length) : file),
|
|
639
702
|
});
|
|
640
703
|
}
|
|
641
704
|
const rec = reportRecords(report);
|
|
@@ -652,6 +715,6 @@ function runMain() {
|
|
|
652
715
|
if (require.main === module) runMain();
|
|
653
716
|
|
|
654
717
|
module.exports = {
|
|
655
|
-
extractSignatures, allSignatures, signatureChanged, findCallSiteLine, computeStopReport, formatStopContext,
|
|
718
|
+
extractSignatures, allSignatures, definitionHeaders, signatureChanged, findCallSiteLine, computeStopReport, formatStopContext,
|
|
656
719
|
followUpOf, reportRecords, MAX_SYMBOLS_CHECKED, MAX_CALLERS_LISTED, MAX_FOLLOWUP_FILES,
|
|
657
720
|
};
|
|
@@ -41,7 +41,7 @@ jobs:
|
|
|
41
41
|
# refuses a snapshot that has no checksum sidecar.
|
|
42
42
|
- name: Build snapshot
|
|
43
43
|
run: |
|
|
44
|
-
npx -y -p @sdsrs/code-graph@0.
|
|
44
|
+
npx -y -p @sdsrs/code-graph@0.179.0 code-graph-mcp snapshot create --out "code-graph-snapshot-${GITHUB_SHA:0:7}.db.zst"
|
|
45
45
|
- name: Upload to release
|
|
46
46
|
env:
|
|
47
47
|
GH_TOKEN: ${{ github.token }}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdsrs/code-graph",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.179.0",
|
|
4
4
|
"description": "MCP server that indexes codebases into an AST knowledge graph with semantic search, call graph traversal, and HTTP route tracing",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,10 +35,10 @@
|
|
|
35
35
|
"node": ">=16"
|
|
36
36
|
},
|
|
37
37
|
"optionalDependencies": {
|
|
38
|
-
"@sdsrs/code-graph-linux-x64": "0.
|
|
39
|
-
"@sdsrs/code-graph-linux-arm64": "0.
|
|
40
|
-
"@sdsrs/code-graph-darwin-x64": "0.
|
|
41
|
-
"@sdsrs/code-graph-darwin-arm64": "0.
|
|
42
|
-
"@sdsrs/code-graph-win32-x64": "0.
|
|
38
|
+
"@sdsrs/code-graph-linux-x64": "0.179.0",
|
|
39
|
+
"@sdsrs/code-graph-linux-arm64": "0.179.0",
|
|
40
|
+
"@sdsrs/code-graph-darwin-x64": "0.179.0",
|
|
41
|
+
"@sdsrs/code-graph-darwin-arm64": "0.179.0",
|
|
42
|
+
"@sdsrs/code-graph-win32-x64": "0.179.0"
|
|
43
43
|
}
|
|
44
44
|
}
|