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