@codepulse-oss/core 2.0.1 → 3.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.
Files changed (66) hide show
  1. package/dist/__tests__/all-wasm-languages.test.d.ts +2 -0
  2. package/dist/__tests__/all-wasm-languages.test.d.ts.map +1 -0
  3. package/dist/__tests__/all-wasm-languages.test.js +158 -0
  4. package/dist/__tests__/all-wasm-languages.test.js.map +1 -0
  5. package/dist/__tests__/v3-architecture.test.d.ts +2 -0
  6. package/dist/__tests__/v3-architecture.test.d.ts.map +1 -0
  7. package/dist/__tests__/v3-architecture.test.js +168 -0
  8. package/dist/__tests__/v3-architecture.test.js.map +1 -0
  9. package/dist/ai/privacy-firewall.d.ts +32 -0
  10. package/dist/ai/privacy-firewall.d.ts.map +1 -0
  11. package/dist/ai/privacy-firewall.js +127 -0
  12. package/dist/ai/privacy-firewall.js.map +1 -0
  13. package/dist/ai/provider.d.ts +50 -0
  14. package/dist/ai/provider.d.ts.map +1 -1
  15. package/dist/ai/provider.js +247 -24
  16. package/dist/ai/provider.js.map +1 -1
  17. package/dist/audit/archeology.d.ts +34 -0
  18. package/dist/audit/archeology.d.ts.map +1 -0
  19. package/dist/audit/archeology.js +162 -0
  20. package/dist/audit/archeology.js.map +1 -0
  21. package/dist/audit/baseline.d.ts +43 -0
  22. package/dist/audit/baseline.d.ts.map +1 -0
  23. package/dist/audit/baseline.js +140 -0
  24. package/dist/audit/baseline.js.map +1 -0
  25. package/dist/audit/main-scanner.d.ts.map +1 -1
  26. package/dist/audit/main-scanner.js +33 -1
  27. package/dist/audit/main-scanner.js.map +1 -1
  28. package/dist/audit/slop-detector.d.ts +14 -0
  29. package/dist/audit/slop-detector.d.ts.map +1 -0
  30. package/dist/audit/slop-detector.js +256 -0
  31. package/dist/audit/slop-detector.js.map +1 -0
  32. package/dist/config/codepulse-config.d.ts +2 -1
  33. package/dist/config/codepulse-config.d.ts.map +1 -1
  34. package/dist/config/codepulse-config.js +10 -2
  35. package/dist/config/codepulse-config.js.map +1 -1
  36. package/dist/db/database.d.ts +24 -0
  37. package/dist/db/database.d.ts.map +1 -1
  38. package/dist/db/database.js +61 -0
  39. package/dist/db/database.js.map +1 -1
  40. package/dist/graph/tree-sitter-indexer.d.ts +14 -1
  41. package/dist/graph/tree-sitter-indexer.d.ts.map +1 -1
  42. package/dist/graph/tree-sitter-indexer.js +415 -67
  43. package/dist/graph/tree-sitter-indexer.js.map +1 -1
  44. package/dist/index.d.ts +6 -0
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +6 -0
  47. package/dist/index.js.map +1 -1
  48. package/dist/output/sarif-writer.d.ts +12 -0
  49. package/dist/output/sarif-writer.d.ts.map +1 -0
  50. package/dist/output/sarif-writer.js +147 -0
  51. package/dist/output/sarif-writer.js.map +1 -0
  52. package/dist/review/engine.d.ts.map +1 -1
  53. package/dist/review/engine.js +20 -5
  54. package/dist/review/engine.js.map +1 -1
  55. package/dist/sast/ast-queries.d.ts +7 -0
  56. package/dist/sast/ast-queries.d.ts.map +1 -0
  57. package/dist/sast/ast-queries.js +701 -0
  58. package/dist/sast/ast-queries.js.map +1 -0
  59. package/dist/sast/runner.d.ts.map +1 -1
  60. package/dist/sast/runner.js +5 -2
  61. package/dist/sast/runner.js.map +1 -1
  62. package/dist/types/review.d.ts +12 -0
  63. package/dist/types/review.d.ts.map +1 -1
  64. package/dist/types/review.js +6 -0
  65. package/dist/types/review.js.map +1 -1
  66. package/package.json +1 -1
@@ -5,7 +5,8 @@ import Parser from 'web-tree-sitter';
5
5
  import ignore from 'ignore';
6
6
  import { resolveSafeDirectory, safeResolveInside, sha256, toPosixPath } from '../utils/platform.js';
7
7
  const require = createRequire(import.meta.url);
8
- const EXT_TO_LANGUAGE = {
8
+ export const EXT_TO_LANGUAGE = {
9
+ // TypeScript & JavaScript
9
10
  '.ts': { lang: 'typescript', wasm: 'tree-sitter-typescript.wasm' },
10
11
  '.mts': { lang: 'typescript', wasm: 'tree-sitter-typescript.wasm' },
11
12
  '.cts': { lang: 'typescript', wasm: 'tree-sitter-typescript.wasm' },
@@ -14,13 +15,71 @@ const EXT_TO_LANGUAGE = {
14
15
  '.mjs': { lang: 'javascript', wasm: 'tree-sitter-javascript.wasm' },
15
16
  '.cjs': { lang: 'javascript', wasm: 'tree-sitter-javascript.wasm' },
16
17
  '.jsx': { lang: 'javascript', wasm: 'tree-sitter-javascript.wasm' },
18
+ // Backend / Systems
17
19
  '.py': { lang: 'python', wasm: 'tree-sitter-python.wasm' },
18
20
  '.go': { lang: 'go', wasm: 'tree-sitter-go.wasm' },
19
21
  '.rs': { lang: 'rust', wasm: 'tree-sitter-rust.wasm' },
20
22
  '.java': { lang: 'java', wasm: 'tree-sitter-java.wasm' },
21
23
  '.cs': { lang: 'csharp', wasm: 'tree-sitter-c_sharp.wasm' },
22
- '.sql': { lang: 'sql', wasm: 'tree-sitter-sql.wasm' }
24
+ '.php': { lang: 'php', wasm: 'tree-sitter-php.wasm' },
25
+ '.kt': { lang: 'kotlin', wasm: 'tree-sitter-kotlin.wasm' },
26
+ '.kts': { lang: 'kotlin', wasm: 'tree-sitter-kotlin.wasm' },
27
+ '.swift': { lang: 'swift', wasm: 'tree-sitter-swift.wasm' },
28
+ '.dart': { lang: 'dart', wasm: 'tree-sitter-dart.wasm' },
29
+ '.rb': { lang: 'ruby', wasm: 'tree-sitter-ruby.wasm' },
30
+ '.c': { lang: 'c', wasm: 'tree-sitter-c.wasm' },
31
+ '.h': { lang: 'c', wasm: 'tree-sitter-c.wasm' },
32
+ '.cpp': { lang: 'cpp', wasm: 'tree-sitter-cpp.wasm' },
33
+ '.cc': { lang: 'cpp', wasm: 'tree-sitter-cpp.wasm' },
34
+ '.hpp': { lang: 'cpp', wasm: 'tree-sitter-cpp.wasm' },
35
+ '.cxx': { lang: 'cpp', wasm: 'tree-sitter-cpp.wasm' },
36
+ '.m': { lang: 'objc', wasm: 'tree-sitter-objc.wasm' },
37
+ '.mm': { lang: 'objc', wasm: 'tree-sitter-objc.wasm' },
38
+ '.zig': { lang: 'zig', wasm: 'tree-sitter-zig.wasm' },
39
+ // Functional / JVM / Web3 / Verification
40
+ '.scala': { lang: 'scala', wasm: 'tree-sitter-scala.wasm' },
41
+ '.ex': { lang: 'elixir', wasm: 'tree-sitter-elixir.wasm' },
42
+ '.exs': { lang: 'elixir', wasm: 'tree-sitter-elixir.wasm' },
43
+ '.elm': { lang: 'elm', wasm: 'tree-sitter-elm.wasm' },
44
+ '.ml': { lang: 'ocaml', wasm: 'tree-sitter-ocaml.wasm' },
45
+ '.mli': { lang: 'ocaml', wasm: 'tree-sitter-ocaml.wasm' },
46
+ '.lua': { lang: 'lua', wasm: 'tree-sitter-lua.wasm' },
47
+ '.el': { lang: 'elisp', wasm: 'tree-sitter-elisp.wasm' },
48
+ '.res': { lang: 'rescript', wasm: 'tree-sitter-rescript.wasm' },
49
+ '.resi': { lang: 'rescript', wasm: 'tree-sitter-rescript.wasm' },
50
+ '.sol': { lang: 'solidity', wasm: 'tree-sitter-solidity.wasm' },
51
+ '.ql': { lang: 'ql', wasm: 'tree-sitter-ql.wasm' },
52
+ '.qll': { lang: 'ql', wasm: 'tree-sitter-ql.wasm' },
53
+ '.tla': { lang: 'tlaplus', wasm: 'tree-sitter-tlaplus.wasm' },
54
+ '.rdl': { lang: 'systemrdl', wasm: 'tree-sitter-systemrdl.wasm' },
55
+ // Web / Templates / Config / Shell
56
+ '.vue': { lang: 'vue', wasm: 'tree-sitter-vue.wasm' },
57
+ '.erb': { lang: 'embedded_template', wasm: 'tree-sitter-embedded_template.wasm' },
58
+ '.ejs': { lang: 'embedded_template', wasm: 'tree-sitter-embedded_template.wasm' },
59
+ '.yaml': { lang: 'yaml', wasm: 'tree-sitter-yaml.wasm' },
60
+ '.yml': { lang: 'yaml', wasm: 'tree-sitter-yaml.wasm' },
61
+ '.sh': { lang: 'bash', wasm: 'tree-sitter-bash.wasm' },
62
+ '.bash': { lang: 'bash', wasm: 'tree-sitter-bash.wasm' },
63
+ '.html': { lang: 'html', wasm: 'tree-sitter-html.wasm' },
64
+ '.htm': { lang: 'html', wasm: 'tree-sitter-html.wasm' },
65
+ '.css': { lang: 'css', wasm: 'tree-sitter-css.wasm' },
66
+ '.scss': { lang: 'css', wasm: 'tree-sitter-css.wasm' },
67
+ '.json': { lang: 'json', wasm: 'tree-sitter-json.wasm' },
68
+ '.toml': { lang: 'toml', wasm: 'tree-sitter-toml.wasm' },
69
+ // SQL handled cleanly by extractSqlTables without attempting missing tree-sitter-sql.wasm
70
+ '.sql': { lang: 'sql', wasm: '' }
23
71
  };
72
+ // Grammars whose binary in tree-sitter-wasms@0.1.13 has ABI/Emscripten/V8-Isolate incompatibility
73
+ // on Node 24 / web-tree-sitter@0.24.x (swift triggers Zone OOM during Isolate teardown).
74
+ // Keep mapped in EXT_TO_LANGUAGE and parse cleanly via extractFallback() unless explicitly enabled.
75
+ const KNOWN_INCOMPAT_WASMS = new Set([
76
+ 'tree-sitter-dart.wasm',
77
+ 'tree-sitter-elm.wasm',
78
+ 'tree-sitter-ql.wasm',
79
+ 'tree-sitter-swift.wasm',
80
+ 'tree-sitter-yaml.wasm'
81
+ ]);
82
+ const MAX_CACHED_LANGUAGES = 14;
24
83
  const DEFAULT_IGNORE_DIRS = new Set([
25
84
  '.git',
26
85
  'node_modules',
@@ -39,22 +98,90 @@ const SKIP_EXTENSIONS = new Set([
39
98
  '.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.svg', '.woff', '.woff2',
40
99
  '.ttf', '.eot', '.mp4', '.mp3', '.zip', '.tar', '.gz', '.pdf', '.wasm', '.lock'
41
100
  ]);
101
+ const SYMBOL_NODE_TYPES = new Set([
102
+ 'function_declaration',
103
+ 'function_definition',
104
+ 'function_definition_statement',
105
+ 'local_function_definition_statement',
106
+ 'function_item',
107
+ 'function_signature',
108
+ 'method_definition',
109
+ 'method_declaration',
110
+ 'method_signature',
111
+ 'method',
112
+ 'singleton_method',
113
+ 'protocol_function_declaration',
114
+ 'operator_definition',
115
+ 'value_definition',
116
+ 'let_declaration',
117
+ 'class_declaration',
118
+ 'class_definition',
119
+ 'class_specifier',
120
+ 'class_interface',
121
+ 'class_implementation',
122
+ 'class',
123
+ 'interface_declaration',
124
+ 'protocol_declaration',
125
+ 'object_declaration',
126
+ 'object_definition',
127
+ 'contract_declaration',
128
+ 'library_declaration',
129
+ 'struct_item',
130
+ 'struct_specifier',
131
+ 'struct_declaration',
132
+ 'type_spec',
133
+ 'component_named_def',
134
+ 'module_declaration'
135
+ ]);
136
+ const IMPORT_NODE_TYPES = new Set([
137
+ 'import_statement',
138
+ 'import_from_statement',
139
+ 'import_declaration',
140
+ 'import_header',
141
+ 'import_directive',
142
+ 'import_clause',
143
+ 'using_directive',
144
+ 'namespace_use_declaration',
145
+ 'use_declaration',
146
+ 'preproc_include',
147
+ 'open_module',
148
+ 'open_statement',
149
+ 'extends'
150
+ ]);
151
+ const CALL_NODE_TYPES = new Set([
152
+ 'call_expression',
153
+ 'call',
154
+ 'invocation_expression',
155
+ 'method_invocation',
156
+ 'function_call_expression',
157
+ 'member_call_expression',
158
+ 'scoped_call_expression',
159
+ 'application_expression',
160
+ 'message_expression',
161
+ 'macro_invocation',
162
+ 'command',
163
+ 'builtin_function'
164
+ ]);
42
165
  export class TreeSitterGraphIndexer {
43
166
  db;
44
167
  static initialized = false;
45
168
  static initPromise = null;
46
169
  static languageCache = new Map();
170
+ static failedWasms = new Set();
47
171
  static repoLocks = new Map();
48
172
  constructor(db) {
49
173
  this.db = db;
50
174
  }
175
+ static async ensureInitialized() {
176
+ return TreeSitterGraphIndexer.ensureInit();
177
+ }
51
178
  static async ensureInit() {
52
179
  if (TreeSitterGraphIndexer.initialized)
53
180
  return;
54
181
  if (!TreeSitterGraphIndexer.initPromise) {
55
182
  TreeSitterGraphIndexer.initPromise = (async () => {
56
- const wasmDir = process.env.CODEPULSE_WASM_DIR;
57
- if (wasmDir) {
183
+ const wasmDir = TreeSitterGraphIndexer.resolveWasmDir();
184
+ if (wasmDir && process.env.CODEPULSE_WASM_DIR) {
58
185
  await Parser.init({
59
186
  locateFile: (scriptName) => path.join(wasmDir, scriptName)
60
187
  });
@@ -82,10 +209,29 @@ export class TreeSitterGraphIndexer {
82
209
  return null;
83
210
  }
84
211
  }
212
+ static async loadLanguageForExt(ext) {
213
+ const meta = EXT_TO_LANGUAGE[ext.toLowerCase()];
214
+ if (!meta || !meta.wasm)
215
+ return null;
216
+ await TreeSitterGraphIndexer.ensureInit();
217
+ const language = await TreeSitterGraphIndexer.loadLanguage(meta.wasm);
218
+ if (!language)
219
+ return null;
220
+ return { lang: meta.lang, language };
221
+ }
85
222
  static async loadLanguage(wasmName) {
223
+ if (TreeSitterGraphIndexer.failedWasms.has(wasmName))
224
+ return null;
225
+ if (KNOWN_INCOMPAT_WASMS.has(wasmName) && process.env.CODEPULSE_STRICT_ALL_WASM !== '1') {
226
+ return null;
227
+ }
86
228
  const cached = TreeSitterGraphIndexer.languageCache.get(wasmName);
87
- if (cached)
229
+ if (cached) {
230
+ // Refresh LRU ordering
231
+ TreeSitterGraphIndexer.languageCache.delete(wasmName);
232
+ TreeSitterGraphIndexer.languageCache.set(wasmName, cached);
88
233
  return cached;
234
+ }
89
235
  const wasmDir = TreeSitterGraphIndexer.resolveWasmDir();
90
236
  if (!wasmDir)
91
237
  return null;
@@ -93,10 +239,16 @@ export class TreeSitterGraphIndexer {
93
239
  try {
94
240
  await fs.access(wasmPath);
95
241
  const lang = await Parser.Language.load(wasmPath);
242
+ if (TreeSitterGraphIndexer.languageCache.size >= MAX_CACHED_LANGUAGES) {
243
+ const oldestKey = TreeSitterGraphIndexer.languageCache.keys().next().value;
244
+ if (oldestKey)
245
+ TreeSitterGraphIndexer.languageCache.delete(oldestKey);
246
+ }
96
247
  TreeSitterGraphIndexer.languageCache.set(wasmName, lang);
97
248
  return lang;
98
249
  }
99
250
  catch {
251
+ TreeSitterGraphIndexer.failedWasms.add(wasmName);
100
252
  return null;
101
253
  }
102
254
  }
@@ -133,7 +285,7 @@ export class TreeSitterGraphIndexer {
133
285
  const ext = path.extname(entry.name).toLowerCase();
134
286
  if (SKIP_EXTENSIONS.has(ext))
135
287
  continue;
136
- if (EXT_TO_LANGUAGE[ext] || ext === '.sql' || ext === '.prisma') {
288
+ if (EXT_TO_LANGUAGE[ext] || ext === '.prisma') {
137
289
  results.push(rel);
138
290
  }
139
291
  }
@@ -254,6 +406,36 @@ export class TreeSitterGraphIndexer {
254
406
  totalEdges: allEdges.length
255
407
  };
256
408
  }
409
+ resolveSymbolName(node) {
410
+ const direct = node.childForFieldName('name') ??
411
+ node.childForFieldName('declarator') ??
412
+ node.namedChildren.find((c) => c.type.includes('identifier') ||
413
+ c.type === 'constant' ||
414
+ c.type === 'value_name' ||
415
+ c.type === 'bare_key' ||
416
+ c.type === 'tag_name' ||
417
+ c.type === 'class_name' ||
418
+ c.type === 'id' ||
419
+ c.type === 'symbol');
420
+ if (direct) {
421
+ if (direct.type === 'function_declarator' || direct.type === 'let_binding') {
422
+ const nested = direct.childForFieldName('declarator') ?? direct.childForFieldName('pattern') ?? direct.namedChildren[0];
423
+ if (nested?.text)
424
+ return nested.text.split(/[(\s]/)[0].slice(0, 80);
425
+ }
426
+ const cleaned = direct.text.trim().replace(/^['"`]|['"`]$/g, '');
427
+ if (cleaned)
428
+ return cleaned.split(/[\s(]/)[0].slice(0, 80);
429
+ }
430
+ // Fallback for C/C++ declarators nested inside children
431
+ const fnDecl = node.namedChildren.find((c) => c.type === 'function_declarator' || c.type === 'let_binding');
432
+ if (fnDecl) {
433
+ const id = fnDecl.namedChildren.find((c) => c.type.includes('identifier') || c.type === 'value_name' || c.type === 'field_identifier');
434
+ if (id?.text)
435
+ return id.text.trim().slice(0, 80);
436
+ }
437
+ return null;
438
+ }
257
439
  extractFromSyntaxTree(repoId, file, language, content, tree) {
258
440
  const nodes = [];
259
441
  const edges = [];
@@ -271,52 +453,47 @@ export class TreeSitterGraphIndexer {
271
453
  language
272
454
  });
273
455
  const lines = content.split(/\r?\n/);
456
+ const addSymbolNode = (kind, name, lineStart, lineEnd) => {
457
+ const nodeId = `${repoId}:${file}:${name}:${lineStart}:${nodes.length}`;
458
+ nodes.push({
459
+ id: nodeId,
460
+ repoId,
461
+ file,
462
+ kind,
463
+ name,
464
+ qualifiedName: `${file}#${name}`,
465
+ lineStart,
466
+ lineEnd,
467
+ signature: lines[lineStart - 1]?.trim().slice(0, 160),
468
+ language
469
+ });
470
+ return nodeId;
471
+ };
274
472
  const visit = (node, currentScopeId) => {
275
473
  const type = node.type;
276
474
  let nextScopeId = currentScopeId;
277
- if (type === 'function_declaration' ||
278
- type === 'function_definition' ||
279
- type === 'method_definition' ||
280
- type === 'method_declaration' ||
281
- type === 'interface_declaration' ||
282
- type === 'class_declaration' ||
283
- type === 'class_definition' ||
284
- type === 'struct_item' ||
285
- type === 'function_item') {
286
- const nameNode = node.childForFieldName('name') ?? node.namedChildren.find((c) => c.type.includes('identifier'));
287
- const symbolName = nameNode?.text ?? 'anonymous';
475
+ if (SYMBOL_NODE_TYPES.has(type)) {
476
+ const symbolName = this.resolveSymbolName(node) ?? 'anonymous';
288
477
  const lineStart = node.startPosition.row + 1;
289
478
  const lineEnd = node.endPosition.row + 1;
290
- const kind = type.includes('class') || type.includes('struct')
479
+ const kind = type.includes('class') || type.includes('struct') || type.includes('contract') || type.includes('object') || type.includes('library') || type === 'component_named_def'
291
480
  ? 'class'
292
- : type.includes('interface')
481
+ : type.includes('interface') || type.includes('protocol')
293
482
  ? 'interface'
294
483
  : type.includes('method')
295
484
  ? 'method'
296
485
  : isTestFile
297
486
  ? 'test'
298
487
  : 'function';
299
- const nodeId = `${repoId}:${file}:${symbolName}:${lineStart}`;
300
- nodes.push({
301
- id: nodeId,
302
- repoId,
303
- file,
304
- kind,
305
- name: symbolName,
306
- qualifiedName: `${file}#${symbolName}`,
307
- lineStart,
308
- lineEnd,
309
- signature: lines[lineStart - 1]?.trim().slice(0, 160),
310
- language
311
- });
488
+ const nodeId = addSymbolNode(kind, symbolName, lineStart, lineEnd);
312
489
  nextScopeId = nodeId;
313
- // Check inheritance
314
- const superclass = node.childForFieldName('superclass') ?? node.namedChildren.find((c) => c.type === 'class_heritage' || c.type === 'extends_clause');
490
+ const superclass = node.childForFieldName('superclass') ??
491
+ node.namedChildren.find((c) => c.type === 'class_heritage' || c.type === 'extends_clause' || c.type === 'inheritance_specifier');
315
492
  if (superclass) {
316
- const target = superclass.text.replace(/^(extends|implements)\s+/, '').trim().split(/[\s,{<]/)[0];
493
+ const target = superclass.text.replace(/^(extends|implements|:)\s+/, '').trim().split(/[\s,{<]/)[0];
317
494
  if (target) {
318
495
  edges.push({
319
- id: `${nodeId}:inherits:${target}:${lineStart}`,
496
+ id: `${nodeId}:inherits:${target}:${lineStart}:${edges.length}`,
320
497
  repoId,
321
498
  sourceNodeId: nodeId,
322
499
  targetName: target,
@@ -329,50 +506,137 @@ export class TreeSitterGraphIndexer {
329
506
  }
330
507
  else if (type === 'lexical_declaration' || type === 'variable_declaration') {
331
508
  const declarator = node.namedChildren.find((c) => c.type === 'variable_declarator');
332
- const nameNode = declarator?.childForFieldName('name');
509
+ const nameNode = declarator?.childForFieldName('name') ?? node.namedChildren.find((c) => c.type === 'identifier');
333
510
  const valueNode = declarator?.childForFieldName('value');
334
511
  if (nameNode && valueNode && (valueNode.type === 'arrow_function' || valueNode.type === 'function_expression')) {
335
- const symbolName = nameNode.text;
336
- const lineStart = node.startPosition.row + 1;
337
- const lineEnd = node.endPosition.row + 1;
338
- const nodeId = `${repoId}:${file}:${symbolName}:${lineStart}`;
339
- nodes.push({
340
- id: nodeId,
512
+ nextScopeId = addSymbolNode(isTestFile ? 'test' : 'function', nameNode.text, node.startPosition.row + 1, node.endPosition.row + 1);
513
+ }
514
+ else if (language === 'zig' && nameNode && node.text.includes('@import')) {
515
+ edges.push({
516
+ id: `${moduleNodeId}:imports:${nameNode.text}:${node.startPosition.row + 1}:${edges.length}`,
341
517
  repoId,
518
+ sourceNodeId: moduleNodeId,
519
+ targetName: nameNode.text,
520
+ kind: 'imports',
342
521
  file,
343
- kind: isTestFile ? 'test' : 'function',
344
- name: symbolName,
345
- qualifiedName: `${file}#${symbolName}`,
346
- lineStart,
347
- lineEnd,
348
- signature: lines[lineStart - 1]?.trim().slice(0, 160),
349
- language
522
+ line: node.startPosition.row + 1
523
+ });
524
+ }
525
+ }
526
+ else if (type === 'list' && language === 'elisp') {
527
+ const sym = node.namedChildren[0];
528
+ if (sym?.text === 'defun' && node.namedChildren[1]) {
529
+ nextScopeId = addSymbolNode('function', node.namedChildren[1].text, node.startPosition.row + 1, node.endPosition.row + 1);
530
+ }
531
+ else if (sym?.text === 'require' && node.namedChildren[1]) {
532
+ edges.push({
533
+ id: `${moduleNodeId}:imports:${node.namedChildren[1].text}:${node.startPosition.row + 1}:${edges.length}`,
534
+ repoId,
535
+ sourceNodeId: moduleNodeId,
536
+ targetName: node.namedChildren[1].text.replace(/^'/, ''),
537
+ kind: 'imports',
538
+ file,
539
+ line: node.startPosition.row + 1
350
540
  });
351
- nextScopeId = nodeId;
352
541
  }
353
542
  }
354
- else if (type === 'import_statement' || type === 'import_from_statement' || type === 'import_declaration') {
355
- const strNode = node.namedChildren.find((c) => c.type.includes('string'));
356
- const importTarget = (strNode?.text ?? node.text).replace(/^['"]|['"]$/g, '');
543
+ else if (type === 'table' || type === 'pair') {
544
+ const keyNode = node.childForFieldName('key') ?? node.namedChildren[0];
545
+ if (keyNode && (language === 'json' || language === 'toml')) {
546
+ const cleanKey = keyNode.text.replace(/^['"]|['"]$/g, '').trim();
547
+ if (cleanKey && nodes.length < 60) {
548
+ addSymbolNode('interface', cleanKey, node.startPosition.row + 1, node.endPosition.row + 1);
549
+ }
550
+ }
551
+ }
552
+ else if (type === 'element' || type === 'script_element' || type === 'rule_set' || type === 'directive' || type === 'output_directive') {
553
+ const lineStart = node.startPosition.row + 1;
554
+ const lineEnd = node.endPosition.row + 1;
555
+ if (type === 'rule_set') {
556
+ const sel = node.namedChildren.find((c) => c.type === 'selectors')?.text.trim().slice(0, 60);
557
+ if (sel)
558
+ addSymbolNode('interface', sel, lineStart, lineEnd);
559
+ }
560
+ else if (type === 'element' || type === 'script_element') {
561
+ const startTag = node.namedChildren.find((c) => c.type === 'start_tag');
562
+ const tag = startTag?.namedChildren.find((c) => c.type === 'tag_name')?.text ?? (type === 'script_element' ? 'script' : 'element');
563
+ if (nodes.length < 80) {
564
+ addSymbolNode(type === 'script_element' ? 'module' : 'interface', tag, lineStart, lineEnd);
565
+ }
566
+ }
567
+ else if (type === 'directive' || type === 'output_directive') {
568
+ const codeNode = node.namedChildren.find((c) => c.type === 'code');
569
+ if (codeNode?.text) {
570
+ addSymbolNode('function', codeNode.text.trim().split(/[\s(]/)[0].slice(0, 60), lineStart, lineEnd);
571
+ }
572
+ }
573
+ }
574
+ else if (IMPORT_NODE_TYPES.has(type)) {
575
+ const strNode = node.namedChildren.find((c) => c.type.includes('string') || c.type === 'string_value');
576
+ const idNode = node.namedChildren.find((c) => c.type.includes('identifier') || c.type === 'module_path' || c.type === 'qualified_name' || c.type === 'dotted_name');
577
+ const importTarget = (strNode?.text ?? idNode?.text ?? node.text).replace(/^['"<]|['">]$/g, '').trim();
357
578
  if (importTarget) {
358
579
  edges.push({
359
- id: `${moduleNodeId}:imports:${importTarget}:${node.startPosition.row + 1}`,
580
+ id: `${moduleNodeId}:imports:${importTarget.slice(0, 80)}:${node.startPosition.row + 1}:${edges.length}`,
360
581
  repoId,
361
582
  sourceNodeId: moduleNodeId,
362
- targetName: importTarget,
583
+ targetName: importTarget.slice(0, 120),
363
584
  kind: 'imports',
364
585
  file,
365
586
  line: node.startPosition.row + 1
366
587
  });
367
588
  }
368
589
  }
369
- else if (type === 'call_expression' || type === 'call') {
370
- const fnNode = node.childForFieldName('function') ?? node.namedChildren[0];
590
+ else if (CALL_NODE_TYPES.has(type)) {
591
+ const fnNode = node.childForFieldName('function') ??
592
+ node.childForFieldName('name') ??
593
+ node.childForFieldName('target') ??
594
+ node.childForFieldName('receiver') ??
595
+ node.namedChildren[0];
371
596
  if (fnNode) {
372
597
  const rawCall = fnNode.text.trim();
373
- const callParts = rawCall.split('.');
374
- const callName = callParts[callParts.length - 1] ?? rawCall;
375
598
  const line = node.startPosition.row + 1;
599
+ const lineEnd = node.endPosition.row + 1;
600
+ // Elixir DSL calls: defmodule / def / defp / alias / import / use
601
+ if (language === 'elixir') {
602
+ const args = node.namedChildren.find((c) => c.type === 'arguments');
603
+ const firstArg = args?.namedChildren[0];
604
+ if (rawCall === 'defmodule' && firstArg) {
605
+ nextScopeId = addSymbolNode('class', firstArg.text.trim(), line, lineEnd);
606
+ }
607
+ else if ((rawCall === 'def' || rawCall === 'defp' || rawCall === 'defmacro') && firstArg) {
608
+ const fnName = (firstArg.namedChildren[0]?.text ?? firstArg.text).split('(')[0].trim();
609
+ nextScopeId = addSymbolNode('function', fnName, line, lineEnd);
610
+ }
611
+ else if ((rawCall === 'import' || rawCall === 'alias' || rawCall === 'use' || rawCall === 'require') && firstArg) {
612
+ edges.push({
613
+ id: `${moduleNodeId}:imports:${firstArg.text.trim()}:${line}:${edges.length}`,
614
+ repoId,
615
+ sourceNodeId: moduleNodeId,
616
+ targetName: firstArg.text.trim(),
617
+ kind: 'imports',
618
+ file,
619
+ line
620
+ });
621
+ }
622
+ }
623
+ // Ruby require('...') / require_relative('...')
624
+ if (language === 'ruby' && (rawCall === 'require' || rawCall === 'require_relative')) {
625
+ const argStr = node.text.match(/['"]([^'"]+)['"]/)?.[1];
626
+ if (argStr) {
627
+ edges.push({
628
+ id: `${moduleNodeId}:imports:${argStr}:${line}:${edges.length}`,
629
+ repoId,
630
+ sourceNodeId: moduleNodeId,
631
+ targetName: argStr,
632
+ kind: 'imports',
633
+ file,
634
+ line
635
+ });
636
+ }
637
+ }
638
+ const callParts = rawCall.split('.');
639
+ const callName = (callParts[callParts.length - 1] ?? rawCall).replace(/[^A-Za-z0-9_$-]/g, '');
376
640
  // Detect Express/Fastify/Hono route registrations
377
641
  if (callParts.length === 2 &&
378
642
  ['get', 'post', 'put', 'patch', 'delete', 'use'].includes(callName) &&
@@ -388,12 +652,12 @@ export class TreeSitterGraphIndexer {
388
652
  name: `${callName.toUpperCase()} ${routePath}`,
389
653
  qualifiedName: `${file}#${callName.toUpperCase()} ${routePath}`,
390
654
  lineStart: line,
391
- lineEnd: node.endPosition.row + 1,
655
+ lineEnd,
392
656
  language
393
657
  });
394
658
  }
395
659
  }
396
- if (callName && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(callName)) {
660
+ if (callName && /^[A-Za-z_$@][A-Za-z0-9_$?-]*$/.test(callName)) {
397
661
  edges.push({
398
662
  id: `${currentScopeId}:calls:${callName}:${line}:${edges.length}`,
399
663
  repoId,
@@ -411,7 +675,6 @@ export class TreeSitterGraphIndexer {
411
675
  }
412
676
  };
413
677
  visit(tree.rootNode, moduleNodeId);
414
- // SQL tables & RPC extraction if SQL or migrations
415
678
  if (language === 'sql' || /\.sql$/i.test(file)) {
416
679
  const sqlNodes = this.extractSqlTables(repoId, file, content);
417
680
  nodes.push(...sqlNodes);
@@ -438,7 +701,7 @@ export class TreeSitterGraphIndexer {
438
701
  language: 'sql'
439
702
  });
440
703
  }
441
- const fn = /CREATE\s+(?:OR\s+REPLACE\s+)?FUNCTION\s+([A-Za-z0-9_.]+)/i.exec(line);
704
+ const fn = /CREATE\s+(?:OR\s+REPLACE\s+)?(?:FUNCTION|PROCEDURE|VIEW)\s+([A-Za-z0-9_.]+)/i.exec(line);
442
705
  if (fn?.[1]) {
443
706
  nodes.push({
444
707
  id: `${repoId}:${file}:sql_rpc:${fn[1]}:${i + 1}`,
@@ -459,6 +722,7 @@ export class TreeSitterGraphIndexer {
459
722
  extractFallback(repoId, file, language, content) {
460
723
  const nodes = [];
461
724
  const edges = [];
725
+ const lines = content.split(/\r?\n/);
462
726
  const moduleNodeId = `${repoId}:${file}:module:1`;
463
727
  nodes.push({
464
728
  id: moduleNodeId,
@@ -468,11 +732,97 @@ export class TreeSitterGraphIndexer {
468
732
  name: path.basename(file),
469
733
  qualifiedName: file,
470
734
  lineStart: 1,
471
- lineEnd: Math.max(1, content.split(/\r?\n/).length),
735
+ lineEnd: Math.max(1, lines.length),
472
736
  language
473
737
  });
474
738
  if (language === 'sql' || /\.sql$/i.test(file)) {
475
739
  nodes.push(...this.extractSqlTables(repoId, file, content));
740
+ return { nodes, edges };
741
+ }
742
+ // Structured fallback symbol extraction (for ABI-incompatible grammars like dart/elm/ql/yaml)
743
+ for (let i = 0; i < lines.length; i++) {
744
+ const line = lines[i] ?? '';
745
+ const ln = i + 1;
746
+ const imp = /^\s*import\s+['"]?([A-Za-z0-9_./:-]+)['"]?/.exec(line);
747
+ if (imp?.[1]) {
748
+ edges.push({
749
+ id: `${moduleNodeId}:imports:${imp[1]}:${ln}:${edges.length}`,
750
+ repoId,
751
+ sourceNodeId: moduleNodeId,
752
+ targetName: imp[1],
753
+ kind: 'imports',
754
+ file,
755
+ line: ln
756
+ });
757
+ continue;
758
+ }
759
+ if (language === 'yaml') {
760
+ const yamlKey = /^([A-Za-z0-9_-]+):\s*(?:#.*)?$/.exec(line);
761
+ if (yamlKey?.[1]) {
762
+ nodes.push({
763
+ id: `${repoId}:${file}:key:${yamlKey[1]}:${ln}`,
764
+ repoId,
765
+ file,
766
+ kind: 'interface',
767
+ name: yamlKey[1],
768
+ qualifiedName: `${file}#${yamlKey[1]}`,
769
+ lineStart: ln,
770
+ lineEnd: ln,
771
+ signature: line.trim(),
772
+ language
773
+ });
774
+ }
775
+ continue;
776
+ }
777
+ const cls = /^\s*(?:public\s+|private\s+|internal\s+|open\s+|abstract\s+|final\s+)*(?:class|struct|protocol|extension|type|module|predicate|query)\s+([A-Za-z0-9_]+)/.exec(line);
778
+ if (cls?.[1]) {
779
+ const isProto = /\bprotocol\b/.test(line);
780
+ nodes.push({
781
+ id: `${repoId}:${file}:${isProto ? 'iface' : 'class'}:${cls[1]}:${ln}`,
782
+ repoId,
783
+ file,
784
+ kind: isProto ? 'interface' : 'class',
785
+ name: cls[1],
786
+ qualifiedName: `${file}#${cls[1]}`,
787
+ lineStart: ln,
788
+ lineEnd: ln,
789
+ signature: line.trim(),
790
+ language
791
+ });
792
+ continue;
793
+ }
794
+ const swiftFn = /^\s*(?:public\s+|private\s+|internal\s+|override\s+|static\s+|class\s+)*func\s+([A-Za-z0-9_]+)/.exec(line);
795
+ if (swiftFn?.[1]) {
796
+ nodes.push({
797
+ id: `${repoId}:${file}:fn:${swiftFn[1]}:${ln}`,
798
+ repoId,
799
+ file,
800
+ kind: 'function',
801
+ name: swiftFn[1],
802
+ qualifiedName: `${file}#${swiftFn[1]}`,
803
+ lineStart: ln,
804
+ lineEnd: ln,
805
+ signature: line.trim(),
806
+ language
807
+ });
808
+ continue;
809
+ }
810
+ const fnMatch = /^\s*(?:Future<[^>]+>\s+|Stream<[^>]+>\s+|void\s+|bool\s+|int\s+|String\s+)?([a-zA-Z_][A-Za-z0-9_]*)\s*\([^;{}]*\)\s*(?:async\s*)?\{/.exec(line) ??
811
+ /^\s*([a-z][A-Za-z0-9_]*)\s+[a-zA-Z0-9_\s]*=\s*/.exec(line);
812
+ if (fnMatch?.[1] && !['if', 'for', 'while', 'switch', 'catch'].includes(fnMatch[1])) {
813
+ nodes.push({
814
+ id: `${repoId}:${file}:fn:${fnMatch[1]}:${ln}`,
815
+ repoId,
816
+ file,
817
+ kind: 'function',
818
+ name: fnMatch[1],
819
+ qualifiedName: `${file}#${fnMatch[1]}`,
820
+ lineStart: ln,
821
+ lineEnd: ln,
822
+ signature: line.trim(),
823
+ language
824
+ });
825
+ }
476
826
  }
477
827
  return { nodes, edges };
478
828
  }
@@ -495,7 +845,6 @@ export class TreeSitterGraphIndexer {
495
845
  const dependentSet = new Set();
496
846
  const testSet = new Set();
497
847
  const contextSlices = [];
498
- // Direct callers: edges whose targetName matches a modified symbol OR imports a modified file
499
848
  for (const edge of edges) {
500
849
  const sourceNode = nodeById.get(edge.sourceNodeId);
501
850
  if (!sourceNode)
@@ -529,7 +878,6 @@ export class TreeSitterGraphIndexer {
529
878
  reason: `${edge.kind} -> ${edge.targetName}`
530
879
  });
531
880
  }
532
- // Risk score heuristic (0-100): weighs changed files, security/auth/payment paths, callers, and test coverage
533
881
  let riskScore = Math.min(35, changedFiles.length * 6);
534
882
  riskScore += Math.min(35, directCallers.length * 7);
535
883
  riskScore += Math.min(20, dependentSet.size * 4);