@sdsrs/code-graph 0.178.0 → 0.179.1

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.
@@ -4,7 +4,7 @@
4
4
  "author": {
5
5
  "name": "sdsrs"
6
6
  },
7
- "version": "0.178.0",
7
+ "version": "0.179.1",
8
8
  "keywords": [
9
9
  "code-graph",
10
10
  "ast",
@@ -28,6 +28,7 @@ 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 the released rule stays
139
- // until a per-language design with a corpus for each language replaces it.
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
- const nameAt = (m.indices[1] || m.indices[2])[0];
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,114 @@ 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
- if (newStr.includes(headerAt(scanned, symbolAt))) process.exit(0);
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) {` and StandardJS's
207
+ // `* entries (prefix) {` are JS generator methods, `#[inline] pub fn` and
208
+ // `#![allow(…)] fn` Rust definitions; a doc comment's `*` is followed by a
209
+ // space, a `/` or the line's end, and never directly by the pick. A JS line
210
+ // opening with `#` or `;` is taken for a comment too, though it opens none:
211
+ // the pick there is no definition the index knows by that name (it names a
212
+ // private method `#parse`, so asking for `parse` finds nothing or another
213
+ // `parse`; `;(function main (` names a function expression, which has no node).
214
+ //
215
+ // Only in the extensions of the reader's languages that corpus had hunks in
216
+ // (not C: 2 hunks). Elsewhere the reader misread shapes no hunk there had
217
+ // (pre-tag review of 0.179.0: Kotlin's `fun Config.validate(` reads as
218
+ // `Config`, PHP's `function &name(` has no keyword before the name, a C++
219
+ // statement `Type name(args);` reads as a definition), so the pick keeps its
220
+ // own verdict, as in 0.178.0; so does a hunk the reader has no reading of (a
221
+ // header past its cap).
222
+ const READER_MEASURED = new Set(['.rs', '.py', '.js', '.mjs', '.cjs', '.ts']);
223
+ const OPENS_A_COMMENT = /^\s*(?:\/\/|\/\*|\*(?=\s|\/|$)|#(?!!?\[)|--|;)/;
224
+ function onComment(line, nameCol) {
225
+ const closed = /^\s*(?:\/\*.*?\*\/\s*)+/.exec(line.slice(0, nameCol));
226
+ const from = closed ? closed[0].length : 0;
227
+ if (/^\s*\*\s+$/.test(line.slice(from, nameCol))) return false; // `* entries (`
228
+ return OPENS_A_COMMENT.test(line.slice(from));
229
+ }
230
+ function changedDefinition() {
231
+ const ext = path.extname(editedAbs).toLowerCase();
232
+ const before = READER_MEASURED.has(ext) ? definitionHeaders(oldStr, ext) : null;
233
+ const after = before && definitionHeaders(newStr, ext);
234
+ if (!before || !after) return bodyOnly ? null : { name: symbol, at: symbolAt };
235
+ const count = (defs) => {
236
+ const m = new Map();
237
+ for (const d of defs) m.set(`${d.name}\0${d.key}`, (m.get(`${d.name}\0${d.key}`) || 0) + 1);
238
+ return m;
239
+ };
240
+ const has = count(after.defs);
241
+ const left = new Map(has);
242
+ let changed = [];
243
+ for (const d of before.defs) {
244
+ const k = `${d.name}\0${d.key}`;
245
+ if (left.get(k) > 0) left.set(k, left.get(k) - 1);
246
+ else changed.push(d);
247
+ }
248
+ // The count matches headers, not definitions: of two definitions of one name
249
+ // with one old header, one kept, it marks the later, whichever the edit
250
+ // changed. Such a name's definitions pair in order when the hunk keeps their
251
+ // number and pairing finds as many changed as the count; otherwise which one
252
+ // changed is not in the text (one went or came, or the hunk moved them), and
253
+ // the reader names none of that name (the rule below can still keep the
254
+ // pick).
255
+ const twinned = new Set(changed
256
+ .filter((c) => (has.get(`${c.name}\0${c.key}`) || 0) > 0)
257
+ .map((c) => c.name));
258
+ if (twinned.size) {
259
+ const counted = new Map();
260
+ for (const d of changed) if (twinned.has(d.name)) counted.set(d.name, (counted.get(d.name) || 0) + 1);
261
+ const byName = (defs) => {
262
+ const m = new Map();
263
+ for (const d of defs) if (twinned.has(d.name)) (m.get(d.name) || m.set(d.name, []).get(d.name)).push(d);
264
+ return m;
265
+ };
266
+ const olds = byName(before.defs);
267
+ const news = byName(after.defs);
268
+ changed = changed.filter((d) => !twinned.has(d.name));
269
+ for (const [name, o] of olds) {
270
+ const n = news.get(name) || [];
271
+ const paired = o.length === n.length ? o.filter((d, i) => d.key !== n[i].key) : [];
272
+ if (paired.length === counted.get(name)) changed.push(...paired);
273
+ }
274
+ }
275
+ changed.sort((x, y) => x.at - y.at);
276
+ const same = changed.find((d) => d.name === symbol);
277
+ if (same) return { name: symbol, at: same.at };
278
+ const lineStart = scanned.lastIndexOf('\n', nameAt - 1) + 1;
279
+ const lineEnd = scanned.indexOf('\n', nameAt);
280
+ const pickLine = scanned.slice(lineStart, lineEnd === -1 ? scanned.length : lineEnd);
281
+ const notADefinition = onComment(pickLine, nameAt - lineStart)
282
+ || (before.keywordOnly && !before.keywordLeads(scanned.slice(lineStart, nameAt)));
283
+ if (!bodyOnly && !notADefinition) return { name: symbol, at: symbolAt };
284
+ return changed.length ? { name: changed[0].name, at: changed[0].at } : null;
285
+ }
286
+ const edited = changedDefinition();
287
+ if (!edited) process.exit(0);
288
+ if (edited.name !== symbol) {
289
+ symbol = edited.name;
290
+ logEdit(symbol);
291
+ }
292
+ symbolAt = edited.at;
183
293
 
184
294
  if (process.env.CODE_GRAPH_QUIET_HOOKS === '1') process.exit(0);
185
295
 
@@ -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,16 +115,36 @@ function escapeRe(s) {
110
115
  return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
111
116
  }
112
117
 
118
+ // Two kinds of run made a read quadratic and are bounded: a bracket's contents
119
+ // (`[^)\n]*`, and Kotlin's `<[^>\n]*>`, re-scanned the rest of a line from
120
+ // every `const a = (`, Go `func (` and Kotlin `fun <` on it), and a modifier
121
+ // run crossing newlines (it re-read every following line from each line of a
122
+ // run of `async` lines; D#572: 100 KB took 2.8 s per read; pre-edit-guide
123
+ // reads a hunk twice within a 4 s budget). A bracket's contents stop at
124
+ // MAX_SIGNATURE_CHARS, readHeader's own cap: a definition with a longer run is
125
+ // not found. What 0.179.0 read for it varied: no header, a header cut short
126
+ // before its name, or, on a line over MAX_DEFINITION_LINE_CHARS, nothing in
127
+ // the whole file.
128
+ // Methods take at most four modifiers, as many as a valid TypeScript method
129
+ // has (an access modifier, `static`, `override`, `async` or `get`/`set`).
130
+ const BRACKETED = `{0,${MAX_SIGNATURE_CHARS}}`;
131
+
132
+ // What comes before a definition's name: the keyword, Go's receiver, Kotlin's
133
+ // type parameters.
134
+ function keywordLead(lang) {
135
+ const receiver = lang.receiver ? `(?:\\([^)\\n]${BRACKETED}\\)\\s*)?` : '';
136
+ const typeParams = lang.typeParams ? `(?:<[^>\\n]${BRACKETED}>\\s*)?` : '';
137
+ return `\\b(?:${lang.keyword})\\s+${receiver}${typeParams}`;
138
+ }
139
+
113
140
  function definitionPatterns(S, lang) {
114
141
  const out = [];
115
142
  if (lang.keyword) {
116
- const receiver = lang.receiver ? '(?:\\([^)\\n]*\\)\\s*)?' : '';
117
- const typeParams = lang.typeParams ? '(?:<[^>\\n]*>\\s*)?' : '';
118
- out.push({ re: new RegExp(`\\b(?:${lang.keyword})\\s+${receiver}${typeParams}${S}\\b`, 'g') });
143
+ out.push({ re: new RegExp(`${keywordLead(lang)}${S}\\b`, 'g') });
119
144
  }
120
145
  if (lang.jsForms) {
121
- 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') });
122
- out.push({ re: new RegExp(`^[ \\t]*(?:(?:async|static|get|set|public|private|protected|readonly|override)\\s+)*\\*?${S}\\s*\\([^()\\n]*\\)\\s*(?::\\s*[^{;=\\n]+)?\\{[ \\t]*$`, 'gm') });
146
+ out.push({ re: new RegExp(`\\b(?:const|let|var)\\s+${S}\\s*=\\s*(?:async\\s+)?(?:function\\b|\\([^)\\n]${BRACKETED}\\)\\s*=>|[A-Za-z_$][\\w$]*\\s*=>)`, 'g') });
147
+ out.push({ re: new RegExp(`^[ \\t]*(?:(?:async|static|get|set|public|private|protected|readonly|override)\\s+){0,4}\\*?${S}\\s*\\([^()\\n]*\\)\\s*(?::\\s*[^{;=\\n]+)?\\{[ \\t]*$`, 'gm') });
123
148
  }
124
149
  if (lang.typed) {
125
150
  out.push({ re: new RegExp(`^[ \\t]*((?:[A-Za-z_][\\w<>\\[\\],.*&:?]*[ \\t]+)+)${S}[ \\t]*\\(`, 'gm'), typed: true });
@@ -183,6 +208,40 @@ function allSignatures(text, ext = '') {
183
208
  return out;
184
209
  }
185
210
 
211
+ /**
212
+ * Every definition in `text`, in text order, with the line it starts on and
213
+ * its signature key (as allSignatures reads it) — for pre-edit-guide, which
214
+ * must tell which of a hunk's definitions an edit changed and where it sits.
215
+ * `keywordOnly` (LANGS) with `keywordLeads`, which tests the text before a
216
+ * name on its line: where both hold, a name it is false for is no definition.
217
+ * null when there is no exact reading of the whole text: the language is not
218
+ * in LANGS, a definition sits on an over-long line, or any header has no
219
+ * reading.
220
+ * @returns {{ defs: {name:string, at:number, key:string}[], keywordOnly: boolean,
221
+ * keywordLeads: (before: string) => boolean }|null}
222
+ */
223
+ function definitionHeaders(text, ext = '') {
224
+ const lang = LANGS.get(String(ext).toLowerCase());
225
+ if (!lang || typeof text !== 'string') return null;
226
+ const starts = definitionStarts(text, lang, ANY_NAME, null);
227
+ if (starts === null) return null;
228
+ const defs = [];
229
+ for (const [name, lineStarts] of starts) {
230
+ for (const at of lineStarts) {
231
+ const header = readHeader(text, at, lang);
232
+ if (header === null) return null;
233
+ defs.push({ name, at, key: signatureKey(header) });
234
+ }
235
+ }
236
+ defs.sort((x, y) => x.at - y.at);
237
+ const lead = lang.keyword ? new RegExp(`${keywordLead(lang)}$`) : null;
238
+ return {
239
+ defs,
240
+ keywordOnly: lang.keywordOnly === true,
241
+ keywordLeads: (before) => lead !== null && lead.test(before),
242
+ };
243
+ }
244
+
186
245
  /**
187
246
  * Where definitions start: Map name → Set of line-start offsets. With `symbol`
188
247
  * the patterns carry it escaped in `S`; without, `S` is ANY_NAME and the name
@@ -670,6 +729,6 @@ function runMain() {
670
729
  if (require.main === module) runMain();
671
730
 
672
731
  module.exports = {
673
- extractSignatures, allSignatures, signatureChanged, findCallSiteLine, computeStopReport, formatStopContext,
732
+ extractSignatures, allSignatures, definitionHeaders, signatureChanged, findCallSiteLine, computeStopReport, formatStopContext,
674
733
  followUpOf, reportRecords, MAX_SYMBOLS_CHECKED, MAX_CALLERS_LISTED, MAX_FOLLOWUP_FILES,
675
734
  };
@@ -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.178.0 code-graph-mcp snapshot create --out "code-graph-snapshot-${GITHUB_SHA:0:7}.db.zst"
44
+ npx -y -p @sdsrs/code-graph@0.179.1 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.178.0",
3
+ "version": "0.179.1",
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.178.0",
39
- "@sdsrs/code-graph-linux-arm64": "0.178.0",
40
- "@sdsrs/code-graph-darwin-x64": "0.178.0",
41
- "@sdsrs/code-graph-darwin-arm64": "0.178.0",
42
- "@sdsrs/code-graph-win32-x64": "0.178.0"
38
+ "@sdsrs/code-graph-linux-x64": "0.179.1",
39
+ "@sdsrs/code-graph-linux-arm64": "0.179.1",
40
+ "@sdsrs/code-graph-darwin-x64": "0.179.1",
41
+ "@sdsrs/code-graph-darwin-arm64": "0.179.1",
42
+ "@sdsrs/code-graph-win32-x64": "0.179.1"
43
43
  }
44
44
  }