@optave/codegraph 3.9.0 → 3.9.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.
Files changed (104) hide show
  1. package/README.md +7 -6
  2. package/dist/ast-analysis/engine.d.ts.map +1 -1
  3. package/dist/ast-analysis/engine.js +78 -48
  4. package/dist/ast-analysis/engine.js.map +1 -1
  5. package/dist/ast-analysis/visitors/ast-store-visitor.d.ts.map +1 -1
  6. package/dist/ast-analysis/visitors/ast-store-visitor.js +15 -18
  7. package/dist/ast-analysis/visitors/ast-store-visitor.js.map +1 -1
  8. package/dist/db/connection.d.ts +1 -0
  9. package/dist/db/connection.d.ts.map +1 -1
  10. package/dist/db/connection.js +22 -4
  11. package/dist/db/connection.js.map +1 -1
  12. package/dist/db/repository/base.d.ts +35 -0
  13. package/dist/db/repository/base.d.ts.map +1 -1
  14. package/dist/db/repository/base.js +8 -0
  15. package/dist/db/repository/base.js.map +1 -1
  16. package/dist/db/repository/index.d.ts +1 -0
  17. package/dist/db/repository/index.d.ts.map +1 -1
  18. package/dist/db/repository/index.js.map +1 -1
  19. package/dist/db/repository/native-repository.d.ts +7 -1
  20. package/dist/db/repository/native-repository.d.ts.map +1 -1
  21. package/dist/db/repository/native-repository.js +46 -1
  22. package/dist/db/repository/native-repository.js.map +1 -1
  23. package/dist/domain/analysis/dependencies.d.ts +1 -28
  24. package/dist/domain/analysis/dependencies.d.ts.map +1 -1
  25. package/dist/domain/analysis/dependencies.js +12 -0
  26. package/dist/domain/analysis/dependencies.js.map +1 -1
  27. package/dist/domain/graph/builder/incremental.d.ts.map +1 -1
  28. package/dist/domain/graph/builder/incremental.js +18 -0
  29. package/dist/domain/graph/builder/incremental.js.map +1 -1
  30. package/dist/domain/graph/builder/pipeline.d.ts.map +1 -1
  31. package/dist/domain/graph/builder/pipeline.js +293 -296
  32. package/dist/domain/graph/builder/pipeline.js.map +1 -1
  33. package/dist/domain/graph/builder/stages/build-edges.d.ts.map +1 -1
  34. package/dist/domain/graph/builder/stages/build-edges.js +29 -2
  35. package/dist/domain/graph/builder/stages/build-edges.js.map +1 -1
  36. package/dist/domain/graph/builder/stages/resolve-imports.d.ts.map +1 -1
  37. package/dist/domain/graph/builder/stages/resolve-imports.js +19 -23
  38. package/dist/domain/graph/builder/stages/resolve-imports.js.map +1 -1
  39. package/dist/domain/graph/watcher.d.ts.map +1 -1
  40. package/dist/domain/graph/watcher.js +99 -95
  41. package/dist/domain/graph/watcher.js.map +1 -1
  42. package/dist/domain/parser.d.ts.map +1 -1
  43. package/dist/domain/parser.js +2 -0
  44. package/dist/domain/parser.js.map +1 -1
  45. package/dist/extractors/go.js +53 -35
  46. package/dist/extractors/go.js.map +1 -1
  47. package/dist/extractors/javascript.js +66 -27
  48. package/dist/extractors/javascript.js.map +1 -1
  49. package/dist/features/complexity.d.ts.map +1 -1
  50. package/dist/features/complexity.js +78 -58
  51. package/dist/features/complexity.js.map +1 -1
  52. package/dist/features/dataflow.d.ts.map +1 -1
  53. package/dist/features/dataflow.js +109 -118
  54. package/dist/features/dataflow.js.map +1 -1
  55. package/dist/features/structure.d.ts.map +1 -1
  56. package/dist/features/structure.js +147 -97
  57. package/dist/features/structure.js.map +1 -1
  58. package/dist/graph/algorithms/louvain.d.ts.map +1 -1
  59. package/dist/graph/algorithms/louvain.js +4 -2
  60. package/dist/graph/algorithms/louvain.js.map +1 -1
  61. package/dist/graph/classifiers/roles.d.ts +2 -0
  62. package/dist/graph/classifiers/roles.d.ts.map +1 -1
  63. package/dist/graph/classifiers/roles.js +13 -5
  64. package/dist/graph/classifiers/roles.js.map +1 -1
  65. package/dist/presentation/communities.d.ts.map +1 -1
  66. package/dist/presentation/communities.js +38 -34
  67. package/dist/presentation/communities.js.map +1 -1
  68. package/dist/presentation/manifesto.d.ts.map +1 -1
  69. package/dist/presentation/manifesto.js +31 -33
  70. package/dist/presentation/manifesto.js.map +1 -1
  71. package/dist/presentation/queries-cli/inspect.d.ts.map +1 -1
  72. package/dist/presentation/queries-cli/inspect.js +47 -46
  73. package/dist/presentation/queries-cli/inspect.js.map +1 -1
  74. package/dist/shared/file-utils.d.ts.map +1 -1
  75. package/dist/shared/file-utils.js +94 -72
  76. package/dist/shared/file-utils.js.map +1 -1
  77. package/dist/types.d.ts +81 -1
  78. package/dist/types.d.ts.map +1 -1
  79. package/package.json +7 -7
  80. package/src/ast-analysis/engine.ts +99 -55
  81. package/src/ast-analysis/visitors/ast-store-visitor.ts +19 -21
  82. package/src/db/connection.ts +24 -5
  83. package/src/db/repository/base.ts +43 -0
  84. package/src/db/repository/index.ts +1 -0
  85. package/src/db/repository/native-repository.ts +67 -1
  86. package/src/domain/analysis/dependencies.ts +13 -0
  87. package/src/domain/graph/builder/incremental.ts +21 -0
  88. package/src/domain/graph/builder/pipeline.ts +392 -362
  89. package/src/domain/graph/builder/stages/build-edges.ts +30 -1
  90. package/src/domain/graph/builder/stages/resolve-imports.ts +20 -20
  91. package/src/domain/graph/watcher.ts +118 -98
  92. package/src/domain/parser.ts +2 -0
  93. package/src/extractors/go.ts +57 -32
  94. package/src/extractors/javascript.ts +67 -27
  95. package/src/features/complexity.ts +94 -58
  96. package/src/features/dataflow.ts +153 -132
  97. package/src/features/structure.ts +167 -95
  98. package/src/graph/algorithms/louvain.ts +5 -2
  99. package/src/graph/classifiers/roles.ts +14 -5
  100. package/src/presentation/communities.ts +44 -39
  101. package/src/presentation/manifesto.ts +35 -38
  102. package/src/presentation/queries-cli/inspect.ts +48 -46
  103. package/src/shared/file-utils.ts +116 -77
  104. package/src/types.ts +85 -0
@@ -227,6 +227,290 @@ function refreshJsDb(ctx) {
227
227
  }
228
228
  ctx.db = openDb(ctx.dbPath);
229
229
  }
230
+ // ── Native orchestrator helpers ───────────────────────────────────────
231
+ /** Determine whether the native orchestrator should be skipped. Returns a reason string, or null if it should run. */
232
+ function shouldSkipNativeOrchestrator(ctx) {
233
+ if (process.env.CODEGRAPH_FORCE_JS_PIPELINE === '1')
234
+ return 'CODEGRAPH_FORCE_JS_PIPELINE=1';
235
+ if (ctx.forceFullRebuild)
236
+ return 'forceFullRebuild';
237
+ const orchestratorBuggy = !!ctx.engineVersion && semverCompare(ctx.engineVersion, '3.10.0') < 0;
238
+ if (orchestratorBuggy)
239
+ return `buggy addon ${ctx.engineVersion}`;
240
+ if (ctx.engineName !== 'native')
241
+ return `engine=${ctx.engineName}`;
242
+ return null;
243
+ }
244
+ /** Checkpoint WAL through rusqlite, close nativeDb, and reopen better-sqlite3.
245
+ * Returns false if the DB reopen fails (caller should return partial result). */
246
+ function handoffWalAfterNativeBuild(ctx) {
247
+ closeNativeDb(ctx, 'post-native-build');
248
+ try {
249
+ ctx.db.close();
250
+ }
251
+ catch (e) {
252
+ debug(`handoffWal JS db close failed: ${toErrorMessage(e)}`);
253
+ }
254
+ try {
255
+ ctx.db = openDb(ctx.dbPath);
256
+ return true;
257
+ }
258
+ catch (reopenErr) {
259
+ warn(`Failed to reopen DB after native build: ${reopenErr.message}`);
260
+ return false;
261
+ }
262
+ }
263
+ /**
264
+ * Reconstruct fileSymbols from the DB after a native orchestrator build.
265
+ * When `scopeFiles` is provided, only loads those files (for analysis-only).
266
+ * When omitted, loads all files (needed for structure rebuilds).
267
+ */
268
+ function reconstructFileSymbolsFromDb(ctx, scopeFiles) {
269
+ let query = 'SELECT file, name, kind, line, end_line as endLine FROM nodes WHERE file IS NOT NULL';
270
+ const params = [];
271
+ if (scopeFiles && scopeFiles.length > 0) {
272
+ const placeholders = scopeFiles.map(() => '?').join(',');
273
+ query += ` AND file IN (${placeholders})`;
274
+ params.push(...scopeFiles);
275
+ }
276
+ query += ' ORDER BY file, line';
277
+ const rows = ctx.db.prepare(query).all(...params);
278
+ const fileSymbols = new Map();
279
+ for (const row of rows) {
280
+ let entry = fileSymbols.get(row.file);
281
+ if (!entry) {
282
+ entry = {
283
+ definitions: [],
284
+ calls: [],
285
+ imports: [],
286
+ classes: [],
287
+ exports: [],
288
+ typeMap: new Map(),
289
+ };
290
+ fileSymbols.set(row.file, entry);
291
+ }
292
+ entry.definitions.push({
293
+ name: row.name,
294
+ kind: row.kind,
295
+ line: row.line,
296
+ endLine: row.endLine ?? undefined,
297
+ });
298
+ }
299
+ // Populate import/export counts from DB edges so buildStructure
300
+ // computes correct import_count/export_count in node_metrics.
301
+ // The extractor arrays aren't persisted to the DB, so we derive
302
+ // counts from edge data instead (#804).
303
+ const importCountRows = ctx.db
304
+ .prepare(`SELECT n.file, COUNT(*) AS cnt
305
+ FROM edges e JOIN nodes n ON e.source_id = n.id
306
+ WHERE e.kind IN ('imports', 'imports-type', 'dynamic-imports')
307
+ AND n.file IS NOT NULL
308
+ GROUP BY n.file`)
309
+ .all();
310
+ for (const row of importCountRows) {
311
+ const entry = fileSymbols.get(row.file);
312
+ if (entry)
313
+ entry.imports = new Array(row.cnt);
314
+ }
315
+ const exportCountRows = ctx.db
316
+ .prepare(`SELECT n_tgt.file, COUNT(DISTINCT n_tgt.id) AS cnt
317
+ FROM edges e
318
+ JOIN nodes n_tgt ON e.target_id = n_tgt.id
319
+ JOIN nodes n_src ON e.source_id = n_src.id
320
+ WHERE e.kind IN ('imports', 'imports-type', 'reexports')
321
+ AND n_tgt.file IS NOT NULL
322
+ AND n_src.file != n_tgt.file
323
+ GROUP BY n_tgt.file`)
324
+ .all();
325
+ for (const row of exportCountRows) {
326
+ const entry = fileSymbols.get(row.file);
327
+ if (entry)
328
+ entry.exports = new Array(row.cnt);
329
+ }
330
+ return fileSymbols;
331
+ }
332
+ /**
333
+ * Run JS buildStructure() after native orchestrator to fill directory nodes + contains edges.
334
+ * For full builds, passes changedFiles=null (full rebuild).
335
+ * For incremental builds, passes the changed file list to scope the update.
336
+ */
337
+ async function runPostNativeStructure(ctx, allFileSymbols, isFullBuild, changedFiles) {
338
+ const structureStart = performance.now();
339
+ try {
340
+ const directories = new Set();
341
+ for (const relPath of allFileSymbols.keys()) {
342
+ const parts = relPath.split('/');
343
+ for (let i = 1; i < parts.length; i++) {
344
+ directories.add(parts.slice(0, i).join('/'));
345
+ }
346
+ }
347
+ const lineCountMap = new Map();
348
+ const cachedLineCounts = ctx.db
349
+ .prepare(`SELECT n.name AS file, m.line_count
350
+ FROM node_metrics m JOIN nodes n ON m.node_id = n.id
351
+ WHERE n.kind = 'file'`)
352
+ .all();
353
+ for (const row of cachedLineCounts) {
354
+ lineCountMap.set(row.file, row.line_count);
355
+ }
356
+ // Full builds need null (rebuild everything). Incremental builds pass the
357
+ // changed file list so buildStructure only updates those files' metrics
358
+ // and contains edges — matching the JS pipeline's medium-incremental path.
359
+ const changedFilePaths = isFullBuild || !changedFiles?.length ? null : changedFiles;
360
+ const { buildStructure: buildStructureFn } = (await import('../../../features/structure.js'));
361
+ buildStructureFn(ctx.db, allFileSymbols, ctx.rootDir, lineCountMap, directories, changedFilePaths);
362
+ debug(`Structure phase completed after native orchestrator${changedFilePaths ? ` (${changedFilePaths.length} files)` : ' (full)'}`);
363
+ }
364
+ catch (err) {
365
+ warn(`Structure phase failed after native build: ${toErrorMessage(err)}`);
366
+ }
367
+ return performance.now() - structureStart;
368
+ }
369
+ /** Run AST/complexity/CFG/dataflow analysis after native orchestrator. */
370
+ async function runPostNativeAnalysis(ctx, allFileSymbols, changedFiles) {
371
+ const timing = { astMs: 0, complexityMs: 0, cfgMs: 0, dataflowMs: 0 };
372
+ // Scope analysis fileSymbols to changed files only
373
+ let analysisFileSymbols;
374
+ if (changedFiles && changedFiles.length > 0) {
375
+ analysisFileSymbols = new Map();
376
+ for (const f of changedFiles) {
377
+ const entry = allFileSymbols.get(f);
378
+ if (entry)
379
+ analysisFileSymbols.set(f, entry);
380
+ }
381
+ }
382
+ else {
383
+ analysisFileSymbols = allFileSymbols;
384
+ }
385
+ // Reopen nativeDb for analysis features (suspend/resume WAL pattern).
386
+ const native = loadNative();
387
+ if (native?.NativeDatabase) {
388
+ try {
389
+ ctx.nativeDb = native.NativeDatabase.openReadWrite(ctx.dbPath);
390
+ if (ctx.engineOpts)
391
+ ctx.engineOpts.nativeDb = ctx.nativeDb;
392
+ }
393
+ catch {
394
+ ctx.nativeDb = undefined;
395
+ if (ctx.engineOpts)
396
+ ctx.engineOpts.nativeDb = undefined;
397
+ }
398
+ }
399
+ try {
400
+ const { runAnalyses: runAnalysesFn } = await import('../../../ast-analysis/engine.js');
401
+ const result = await runAnalysesFn(ctx.db, analysisFileSymbols, ctx.rootDir, ctx.opts, ctx.engineOpts);
402
+ timing.astMs = result.astMs ?? 0;
403
+ timing.complexityMs = result.complexityMs ?? 0;
404
+ timing.cfgMs = result.cfgMs ?? 0;
405
+ timing.dataflowMs = result.dataflowMs ?? 0;
406
+ }
407
+ catch (err) {
408
+ warn(`Analysis phases failed after native build: ${toErrorMessage(err)}`);
409
+ }
410
+ // Close nativeDb after analyses
411
+ if (ctx.nativeDb) {
412
+ try {
413
+ ctx.nativeDb.exec('PRAGMA wal_checkpoint(TRUNCATE)');
414
+ }
415
+ catch {
416
+ /* ignore checkpoint errors */
417
+ }
418
+ try {
419
+ ctx.nativeDb.close();
420
+ }
421
+ catch {
422
+ /* ignore close errors */
423
+ }
424
+ ctx.nativeDb = undefined;
425
+ if (ctx.engineOpts)
426
+ ctx.engineOpts.nativeDb = undefined;
427
+ }
428
+ return timing;
429
+ }
430
+ /** Format timing result from native orchestrator phases + JS post-processing. */
431
+ function formatNativeTimingResult(p, structurePatchMs, analysisTiming) {
432
+ return {
433
+ phases: {
434
+ setupMs: +((p.setupMs ?? 0) + (p.collectMs ?? 0) + (p.detectMs ?? 0)).toFixed(1),
435
+ parseMs: +(p.parseMs ?? 0).toFixed(1),
436
+ insertMs: +(p.insertMs ?? 0).toFixed(1),
437
+ resolveMs: +(p.resolveMs ?? 0).toFixed(1),
438
+ edgesMs: +(p.edgesMs ?? 0).toFixed(1),
439
+ structureMs: +((p.structureMs ?? 0) + structurePatchMs).toFixed(1),
440
+ rolesMs: +(p.rolesMs ?? 0).toFixed(1),
441
+ astMs: +(analysisTiming.astMs ?? 0).toFixed(1),
442
+ complexityMs: +(analysisTiming.complexityMs ?? 0).toFixed(1),
443
+ cfgMs: +(analysisTiming.cfgMs ?? 0).toFixed(1),
444
+ dataflowMs: +(analysisTiming.dataflowMs ?? 0).toFixed(1),
445
+ finalizeMs: +(p.finalizeMs ?? 0).toFixed(1),
446
+ },
447
+ };
448
+ }
449
+ /** Try the native build orchestrator. Returns a BuildResult on success, undefined to fall through to JS pipeline. */
450
+ async function tryNativeOrchestrator(ctx) {
451
+ const skipReason = shouldSkipNativeOrchestrator(ctx);
452
+ if (skipReason) {
453
+ debug(`Skipping native orchestrator: ${skipReason}`);
454
+ return undefined;
455
+ }
456
+ if (!ctx.nativeDb?.buildGraph)
457
+ return undefined;
458
+ const resultJson = ctx.nativeDb.buildGraph(ctx.rootDir, JSON.stringify(ctx.config), JSON.stringify(ctx.aliases), JSON.stringify(ctx.opts));
459
+ const result = JSON.parse(resultJson);
460
+ if (result.earlyExit) {
461
+ info('No changes detected');
462
+ closeDbPair({ db: ctx.db, nativeDb: ctx.nativeDb });
463
+ return 'early-exit';
464
+ }
465
+ // Log incremental status to match JS pipeline output
466
+ const changed = result.changedCount ?? 0;
467
+ const removed = result.removedCount ?? 0;
468
+ if (!result.isFullBuild && (changed > 0 || removed > 0)) {
469
+ info(`Incremental: ${changed} changed, ${removed} removed`);
470
+ }
471
+ const p = result.phases;
472
+ // Sync build_meta so JS-side version/engine checks work on next build.
473
+ setBuildMeta(ctx.db, {
474
+ engine: ctx.engineName,
475
+ engine_version: ctx.engineVersion || '',
476
+ codegraph_version: CODEGRAPH_VERSION,
477
+ schema_version: String(ctx.schemaVersion),
478
+ built_at: new Date().toISOString(),
479
+ node_count: String(result.nodeCount ?? 0),
480
+ edge_count: String(result.edgeCount ?? 0),
481
+ });
482
+ info(`Native build orchestrator completed: ${result.nodeCount ?? 0} nodes, ${result.edgeCount ?? 0} edges, ${result.fileCount ?? 0} files`);
483
+ // ── Post-native structure + analysis ──────────────────────────────
484
+ let analysisTiming = { astMs: 0, complexityMs: 0, cfgMs: 0, dataflowMs: 0 };
485
+ let structurePatchMs = 0;
486
+ const needsAnalysis = ctx.opts.ast !== false ||
487
+ ctx.opts.complexity !== false ||
488
+ ctx.opts.cfg !== false ||
489
+ ctx.opts.dataflow !== false;
490
+ // Skip JS structure when the Rust pipeline's small-incremental fast path
491
+ // already handled it. For full builds and large incrementals where Rust
492
+ // skipped structure, we must run the JS fallback.
493
+ const needsStructure = !result.structureHandled;
494
+ if (needsAnalysis || needsStructure) {
495
+ if (!handoffWalAfterNativeBuild(ctx)) {
496
+ // DB reopen failed — return partial result
497
+ return formatNativeTimingResult(p, 0, analysisTiming);
498
+ }
499
+ // When structure was handled by Rust, we only need changed files for
500
+ // analysis — no need to load the entire graph from DB. When structure
501
+ // was NOT handled, we need all files to build the complete directory tree.
502
+ const scopeFiles = needsStructure ? undefined : result.changedFiles;
503
+ const fileSymbols = reconstructFileSymbolsFromDb(ctx, scopeFiles);
504
+ if (needsStructure) {
505
+ structurePatchMs = await runPostNativeStructure(ctx, fileSymbols, !!result.isFullBuild, result.structureScope ?? result.changedFiles);
506
+ }
507
+ if (needsAnalysis) {
508
+ analysisTiming = await runPostNativeAnalysis(ctx, fileSymbols, result.changedFiles);
509
+ }
510
+ }
511
+ closeDbPair({ db: ctx.db, nativeDb: ctx.nativeDb });
512
+ return formatNativeTimingResult(p, structurePatchMs, analysisTiming);
513
+ }
230
514
  // ── Pipeline stages execution ───────────────────────────────────────────
231
515
  async function runPipelineStages(ctx) {
232
516
  // Prevent dual-connection WAL corruption during pipeline stages: when both
@@ -296,303 +580,16 @@ export async function buildGraph(rootDir, opts = {}) {
296
580
  // When available, run the entire build pipeline in Rust with zero
297
581
  // napi crossings (eliminates WAL dual-connection dance). Falls back
298
582
  // to the JS pipeline on failure or when native is unavailable.
299
- //
300
- // Native addon ≤3.8.0 has a path bug: file_symbols keys are absolute
301
- // paths but known_files are relative, causing zero import/call edges.
302
- // Native addon ≤3.8.1 has an incremental barrel bug: the Rust pipeline
303
- // doesn't re-parse barrel files that are imported by changed files,
304
- // causing missing barrel import edges and lost analysis data for
305
- // reverse-dep files during incremental builds.
306
- // Skip the orchestrator for affected versions (fixed in 3.9.0+).
307
- const orchestratorBuggy = !!ctx.engineVersion && semverCompare(ctx.engineVersion, '3.8.1') <= 0;
308
- const forceJs = process.env.CODEGRAPH_FORCE_JS_PIPELINE === '1' ||
309
- ctx.forceFullRebuild ||
310
- orchestratorBuggy ||
311
- ctx.engineName !== 'native';
312
- if (forceJs) {
313
- const reason = process.env.CODEGRAPH_FORCE_JS_PIPELINE === '1'
314
- ? 'CODEGRAPH_FORCE_JS_PIPELINE=1'
315
- : ctx.forceFullRebuild
316
- ? 'forceFullRebuild'
317
- : orchestratorBuggy
318
- ? `buggy addon ${ctx.engineVersion}`
319
- : `engine=${ctx.engineName}`;
320
- debug(`Skipping native orchestrator: ${reason}`);
583
+ try {
584
+ const nativeResult = await tryNativeOrchestrator(ctx);
585
+ if (nativeResult === 'early-exit')
586
+ return;
587
+ if (nativeResult)
588
+ return nativeResult;
321
589
  }
322
- if (!forceJs && ctx.nativeDb?.buildGraph) {
323
- try {
324
- const resultJson = ctx.nativeDb.buildGraph(ctx.rootDir, JSON.stringify(ctx.config), JSON.stringify(ctx.aliases), JSON.stringify(opts));
325
- const result = JSON.parse(resultJson);
326
- if (result.earlyExit) {
327
- info('No changes detected');
328
- closeDbPair({ db: ctx.db, nativeDb: ctx.nativeDb });
329
- return;
330
- }
331
- // Log incremental status to match JS pipeline output
332
- const changed = result.changedCount ?? 0;
333
- const removed = result.removedCount ?? 0;
334
- if (!result.isFullBuild && (changed > 0 || removed > 0)) {
335
- info(`Incremental: ${changed} changed, ${removed} removed`);
336
- }
337
- // Map Rust timing fields to the JS BuildResult format.
338
- // Rust handles collect+detect+parse+insert+resolve+edges+structure+roles.
339
- const p = result.phases;
340
- // Sync build_meta so JS-side version/engine checks work on next build.
341
- // Note: the Rust orchestrator also writes codegraph_version (using
342
- // CARGO_PKG_VERSION). We intentionally overwrite it here with the npm
343
- // package version so that the JS-side "version changed → full rebuild"
344
- // detection (line ~97) compares against the authoritative JS version.
345
- // The two versions are kept in lockstep by the release process.
346
- setBuildMeta(ctx.db, {
347
- engine: ctx.engineName,
348
- engine_version: ctx.engineVersion || '',
349
- codegraph_version: CODEGRAPH_VERSION,
350
- schema_version: String(ctx.schemaVersion),
351
- built_at: new Date().toISOString(),
352
- node_count: String(result.nodeCount ?? 0),
353
- edge_count: String(result.edgeCount ?? 0),
354
- });
355
- info(`Native build orchestrator completed: ${result.nodeCount ?? 0} nodes, ${result.edgeCount ?? 0} edges, ${result.fileCount ?? 0} files`);
356
- // ── Run structure + analysis phases after native orchestrator ──
357
- // Structure (directory nodes, contains edges, metrics) is not fully
358
- // ported to Rust — the native pipeline only handles the small
359
- // incremental fast path (≤5 changed files). For full builds and
360
- // larger incremental builds, run JS buildStructure() to fill the gap.
361
- // Analysis phases (AST, complexity, CFG, dataflow) are also not yet
362
- // ported; run via JS engine after reconstructing fileSymbols from DB.
363
- let analysisTiming = { astMs: 0, complexityMs: 0, cfgMs: 0, dataflowMs: 0 };
364
- let structurePatchMs = 0;
365
- const needsAnalysis = opts.ast !== false ||
366
- opts.complexity !== false ||
367
- opts.cfg !== false ||
368
- opts.dataflow !== false;
369
- // The native fast path only runs structure for small incremental
370
- // builds: !isFullBuild && changedCount <= 5 && existingFileCount > 20.
371
- // For all other cases (full builds, large incrementals), we must
372
- // run JS buildStructure() to create directory nodes + contains edges (#804).
373
- // Always run JS structure — the native fast-path has an additional
374
- // existingFileCount > 20 guard that isn't reflected in the result JSON,
375
- // so we can't reliably detect whether native actually ran structure.
376
- const nativeHandledStructure = false;
377
- const needsStructure = !nativeHandledStructure;
378
- if (needsAnalysis || needsStructure) {
379
- // WAL handoff: checkpoint through rusqlite, close nativeDb,
380
- // reopen better-sqlite3 with a fresh page cache (#715, #736).
381
- try {
382
- ctx.nativeDb.exec('PRAGMA wal_checkpoint(TRUNCATE)');
383
- }
384
- catch {
385
- /* ignore checkpoint errors */
386
- }
387
- try {
388
- ctx.nativeDb.close();
389
- }
390
- catch {
391
- /* ignore close errors */
392
- }
393
- ctx.nativeDb = undefined;
394
- try {
395
- ctx.db.close();
396
- }
397
- catch {
398
- /* ignore close errors */
399
- }
400
- ctx.db = null; // avoid closeDbPair operating on a stale handle
401
- try {
402
- ctx.db = openDb(ctx.dbPath);
403
- }
404
- catch (reopenErr) {
405
- warn(`Failed to reopen DB after native build: ${reopenErr.message}`);
406
- // Native build succeeded but we can't run post-processing — return partial result
407
- return {
408
- phases: {
409
- setupMs: +((p.setupMs ?? 0) + (p.collectMs ?? 0) + (p.detectMs ?? 0)).toFixed(1),
410
- parseMs: +(p.parseMs ?? 0).toFixed(1),
411
- insertMs: +(p.insertMs ?? 0).toFixed(1),
412
- resolveMs: +(p.resolveMs ?? 0).toFixed(1),
413
- edgesMs: +(p.edgesMs ?? 0).toFixed(1),
414
- structureMs: +(p.structureMs ?? 0).toFixed(1),
415
- rolesMs: +(p.rolesMs ?? 0).toFixed(1),
416
- astMs: 0,
417
- complexityMs: 0,
418
- cfgMs: 0,
419
- dataflowMs: 0,
420
- finalizeMs: +(p.finalizeMs ?? 0).toFixed(1),
421
- },
422
- };
423
- }
424
- // Reconstruct fileSymbols from DB. For structure we need ALL files
425
- // (to build complete directory tree); for analysis we scope to
426
- // changed files only. Load all files, then scope analysis later.
427
- const allFileRows = ctx.db
428
- .prepare('SELECT file, name, kind, line, end_line as endLine FROM nodes WHERE file IS NOT NULL ORDER BY file, line')
429
- .all();
430
- const allFileSymbols = new Map();
431
- for (const row of allFileRows) {
432
- let entry = allFileSymbols.get(row.file);
433
- if (!entry) {
434
- entry = {
435
- definitions: [],
436
- calls: [],
437
- imports: [],
438
- classes: [],
439
- exports: [],
440
- typeMap: new Map(),
441
- };
442
- allFileSymbols.set(row.file, entry);
443
- }
444
- entry.definitions.push({
445
- name: row.name,
446
- kind: row.kind,
447
- line: row.line,
448
- endLine: row.endLine ?? undefined,
449
- });
450
- }
451
- // Populate import/export counts from DB edges so buildStructure
452
- // computes correct import_count/export_count in node_metrics.
453
- // The extractor arrays aren't persisted to the DB, so we derive
454
- // counts from edge data instead (#804).
455
- const importCountRows = ctx.db
456
- .prepare(`SELECT n.file, COUNT(*) AS cnt
457
- FROM edges e JOIN nodes n ON e.source_id = n.id
458
- WHERE e.kind IN ('imports', 'imports-type', 'dynamic-imports')
459
- AND n.file IS NOT NULL
460
- GROUP BY n.file`)
461
- .all();
462
- for (const row of importCountRows) {
463
- const entry = allFileSymbols.get(row.file);
464
- if (entry)
465
- entry.imports = new Array(row.cnt);
466
- }
467
- // Export count: definitions in this file that are imported by other files
468
- const exportCountRows = ctx.db
469
- .prepare(`SELECT n_tgt.file, COUNT(DISTINCT n_tgt.id) AS cnt
470
- FROM edges e
471
- JOIN nodes n_tgt ON e.target_id = n_tgt.id
472
- JOIN nodes n_src ON e.source_id = n_src.id
473
- WHERE e.kind IN ('imports', 'imports-type', 'reexports')
474
- AND n_tgt.file IS NOT NULL
475
- AND n_src.file != n_tgt.file
476
- GROUP BY n_tgt.file`)
477
- .all();
478
- for (const row of exportCountRows) {
479
- const entry = allFileSymbols.get(row.file);
480
- if (entry)
481
- entry.exports = new Array(row.cnt);
482
- }
483
- // ── Structure phase: directory nodes + contains edges (#804) ──
484
- if (needsStructure) {
485
- const structureStart = performance.now();
486
- try {
487
- // Derive directories from file paths
488
- const directories = new Set();
489
- for (const relPath of allFileSymbols.keys()) {
490
- const parts = relPath.split('/');
491
- for (let i = 1; i < parts.length; i++) {
492
- directories.add(parts.slice(0, i).join('/'));
493
- }
494
- }
495
- // Build line count map from DB metrics or file content
496
- const lineCountMap = new Map();
497
- const cachedLineCounts = ctx.db
498
- .prepare(`SELECT n.name AS file, m.line_count
499
- FROM node_metrics m JOIN nodes n ON m.node_id = n.id
500
- WHERE n.kind = 'file'`)
501
- .all();
502
- for (const row of cachedLineCounts) {
503
- lineCountMap.set(row.file, row.line_count);
504
- }
505
- // Native ran no structure at all — always do a full rebuild so
506
- // every directory gets nodes + contains edges (#804).
507
- const changedFilePaths = null;
508
- const { buildStructure: buildStructureFn } = (await import('../../../features/structure.js'));
509
- buildStructureFn(ctx.db, allFileSymbols, ctx.rootDir, lineCountMap, directories, changedFilePaths);
510
- debug('Structure phase completed after native orchestrator');
511
- }
512
- catch (err) {
513
- warn(`Structure phase failed after native build: ${toErrorMessage(err)}`);
514
- }
515
- structurePatchMs = performance.now() - structureStart;
516
- }
517
- // ── Analysis phase ──
518
- if (needsAnalysis) {
519
- // Scope analysis fileSymbols to changed files only
520
- const changedFiles = result.changedFiles;
521
- let analysisFileSymbols;
522
- if (changedFiles && changedFiles.length > 0) {
523
- analysisFileSymbols = new Map();
524
- for (const f of changedFiles) {
525
- const entry = allFileSymbols.get(f);
526
- if (entry)
527
- analysisFileSymbols.set(f, entry);
528
- }
529
- }
530
- else {
531
- analysisFileSymbols = allFileSymbols;
532
- }
533
- // Reopen nativeDb for analysis features (suspend/resume WAL pattern).
534
- const native = loadNative();
535
- if (native?.NativeDatabase) {
536
- try {
537
- ctx.nativeDb = native.NativeDatabase.openReadWrite(ctx.dbPath);
538
- if (ctx.engineOpts)
539
- ctx.engineOpts.nativeDb = ctx.nativeDb;
540
- }
541
- catch {
542
- ctx.nativeDb = undefined;
543
- if (ctx.engineOpts)
544
- ctx.engineOpts.nativeDb = undefined;
545
- }
546
- }
547
- try {
548
- const { runAnalyses: runAnalysesFn } = await import('../../../ast-analysis/engine.js');
549
- analysisTiming = await runAnalysesFn(ctx.db, analysisFileSymbols, ctx.rootDir, opts, ctx.engineOpts);
550
- }
551
- catch (err) {
552
- warn(`Analysis phases failed after native build: ${toErrorMessage(err)}`);
553
- }
554
- // Close nativeDb after analyses
555
- if (ctx.nativeDb) {
556
- try {
557
- ctx.nativeDb.exec('PRAGMA wal_checkpoint(TRUNCATE)');
558
- }
559
- catch {
560
- /* ignore checkpoint errors */
561
- }
562
- try {
563
- ctx.nativeDb.close();
564
- }
565
- catch {
566
- /* ignore close errors */
567
- }
568
- ctx.nativeDb = undefined;
569
- if (ctx.engineOpts)
570
- ctx.engineOpts.nativeDb = undefined;
571
- }
572
- }
573
- }
574
- closeDbPair({ db: ctx.db, nativeDb: ctx.nativeDb });
575
- return {
576
- phases: {
577
- setupMs: +((p.setupMs ?? 0) + (p.collectMs ?? 0) + (p.detectMs ?? 0)).toFixed(1),
578
- parseMs: +(p.parseMs ?? 0).toFixed(1),
579
- insertMs: +(p.insertMs ?? 0).toFixed(1),
580
- resolveMs: +(p.resolveMs ?? 0).toFixed(1),
581
- edgesMs: +(p.edgesMs ?? 0).toFixed(1),
582
- structureMs: +((p.structureMs ?? 0) + structurePatchMs).toFixed(1),
583
- rolesMs: +(p.rolesMs ?? 0).toFixed(1),
584
- astMs: +(analysisTiming.astMs ?? 0).toFixed(1),
585
- complexityMs: +(analysisTiming.complexityMs ?? 0).toFixed(1),
586
- cfgMs: +(analysisTiming.cfgMs ?? 0).toFixed(1),
587
- dataflowMs: +(analysisTiming.dataflowMs ?? 0).toFixed(1),
588
- finalizeMs: +(p.finalizeMs ?? 0).toFixed(1),
589
- },
590
- };
591
- }
592
- catch (err) {
593
- warn(`Native build orchestrator failed, falling back to JS pipeline: ${toErrorMessage(err)}`);
594
- // Fall through to JS pipeline
595
- }
590
+ catch (err) {
591
+ warn(`Native build orchestrator failed, falling back to JS pipeline: ${toErrorMessage(err)}`);
592
+ // Fall through to JS pipeline
596
593
  }
597
594
  await runPipelineStages(ctx);
598
595
  }