Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/domain/graph/builder/incremental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import path from 'node:path';
import { bulkNodeIdsByFile, purgeFileData } from '../../../db/index.js';
import { PROPAGATION_HOP_PENALTY } from '../../../extractors/javascript.js';
import { debug, warn } from '../../../infrastructure/logger.js';
import { getOrCreatePerDbChunkStmt } from '../../../shared/chunked-stmt-cache.js';
import { normalizePath, TS_NATIVE_CONFIDENCE_FLOOR } from '../../../shared/constants.js';
import { FLAG_ONLY_DYNAMIC_KINDS, isTypeErasedImportTarget } from '../../../shared/kinds.js';
import type {
Expand Down Expand Up @@ -1184,6 +1185,19 @@ function buildCallEdges(
* files), not a full-table scan. Chunked to stay within SQLite's
* SQLITE_LIMIT_VARIABLE_NUMBER (999 on older builds).
*/
// Chunk-size-keyed statement caches for backfillIncrementalEdgeTechniques'
// technique/confidence-floor UPDATEs below, scoped per db like the
// techniqueBackfillStmtCache/confidenceFloorStmtCache pair in
// stages/build-edges.ts's applyEdgeTechniquesAfterNativeInsert (#1768, #1952).
const incrementalTechniqueBackfillStmtCache = new WeakMap<
BetterSqlite3Database,
Map<number, SqliteStatement>
>();
const incrementalConfidenceFloorStmtCache = new WeakMap<
BetterSqlite3Database,
Map<number, SqliteStatement>
>();

function backfillIncrementalEdgeTechniques(
db: BetterSqlite3Database,
touchedFiles: readonly string[],
Expand All @@ -1193,20 +1207,30 @@ function backfillIncrementalEdgeTechniques(
const tx = db.transaction(() => {
for (let i = 0; i < touchedFiles.length; i += CHUNK_SIZE) {
const chunk = touchedFiles.slice(i, i + CHUNK_SIZE);
const placeholders = chunk.map(() => '?').join(',');
db.prepare(
`UPDATE edges SET technique = 'ts-native'
WHERE kind = 'calls' AND technique IS NULL AND dynamic_kind IS NULL
AND source_id IN (SELECT id FROM nodes WHERE file IN (${placeholders}))`,
).run(...chunk);
const chunkSize = chunk.length;
const techniqueStmt = getOrCreatePerDbChunkStmt(
incrementalTechniqueBackfillStmtCache,
db,
chunkSize,
(n) =>
`UPDATE edges SET technique = 'ts-native'
WHERE kind = 'calls' AND technique IS NULL AND dynamic_kind IS NULL
AND source_id IN (SELECT id FROM nodes WHERE file IN (${Array.from({ length: n }, () => '?').join(',')}))`,
);
techniqueStmt.run(...chunk);
// Lift resolved ts-native edges below the confidence floor for this
// chunk, matching the floor lift the full-build native paths apply.
db.prepare(
`UPDATE edges SET confidence = ?
WHERE kind = 'calls' AND technique = 'ts-native'
AND confidence > 0 AND confidence < ?
AND source_id IN (SELECT id FROM nodes WHERE file IN (${placeholders}))`,
).run(TS_NATIVE_CONFIDENCE_FLOOR, TS_NATIVE_CONFIDENCE_FLOOR, ...chunk);
const confidenceStmt = getOrCreatePerDbChunkStmt(
incrementalConfidenceFloorStmtCache,
db,
chunkSize,
(n) =>
`UPDATE edges SET confidence = ?
WHERE kind = 'calls' AND technique = 'ts-native'
AND confidence > 0 AND confidence < ?
AND source_id IN (SELECT id FROM nodes WHERE file IN (${Array.from({ length: n }, () => '?').join(',')}))`,
);
confidenceStmt.run(TS_NATIVE_CONFIDENCE_FLOOR, TS_NATIVE_CONFIDENCE_FLOOR, ...chunk);
}
});
tx();
Expand Down
78 changes: 56 additions & 22 deletions src/domain/graph/builder/stages/native-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { debug, info, warn } from '../../../../infrastructure/logger.js';
import { loadNative } from '../../../../infrastructure/native.js';
import { semverCompare } from '../../../../infrastructure/update-check.js';
import { getOrCreatePerDbChunkStmt } from '../../../../shared/chunked-stmt-cache.js';
import { normalizePath, TS_NATIVE_CONFIDENCE_FLOOR } from '../../../../shared/constants.js';
import { toErrorMessage } from '../../../../shared/errors.js';
import { CODEGRAPH_VERSION } from '../../../../shared/version.js';
Expand All @@ -34,6 +35,7 @@ import type {
DataflowResult,
Definition,
ExtractorOutput,
SqliteStatement,
} from '../../../../types.js';
import {
classifyNativeDrops,
Expand Down Expand Up @@ -2063,20 +2065,29 @@ async function backfillNativeDroppedFiles(
* Chunked to stay within SQLite's SQLITE_LIMIT_VARIABLE_NUMBER (999 on older
* builds).
*/
// Chunk-size-keyed statement cache for findOneHopReverseDepFiles' SELECT below,
// scoped per db like the technique/confidence-floor caches further down —
// avoids recompiling the same query on every chunk of a large reverse-dep
// scan (#1952).
const oneHopReverseDepStmtCache = new WeakMap<
BetterSqlite3Database,
Map<number, SqliteStatement<{ file: string }>>
>();

function findOneHopReverseDepFiles(db: BetterSqlite3Database, files: readonly string[]): string[] {
const found = new Set<string>();
const CHUNK_SIZE = 500;
for (let i = 0; i < files.length; i += CHUNK_SIZE) {
const chunk = files.slice(i, i + CHUNK_SIZE);
const placeholders = chunk.map(() => '?').join(',');
const rows = db
.prepare(
`SELECT DISTINCT n_src.file AS file FROM edges e
const chunkSize = chunk.length;
const stmt = getOrCreatePerDbChunkStmt(oneHopReverseDepStmtCache, db, chunkSize, (n) => {
const placeholders = Array.from({ length: n }, () => '?').join(',');
return `SELECT DISTINCT n_src.file AS file FROM edges e
JOIN nodes n_src ON e.source_id = n_src.id
JOIN nodes n_tgt ON e.target_id = n_tgt.id
WHERE e.kind = 'calls' AND n_tgt.file IN (${placeholders}) AND n_src.kind != 'directory'`,
)
.all(...chunk) as Array<{ file: string }>;
WHERE e.kind = 'calls' AND n_tgt.file IN (${placeholders}) AND n_src.kind != 'directory'`;
});
const rows = stmt.all(...chunk);
for (const r of rows) found.add(r.file);
}
return [...found];
Expand All @@ -2094,6 +2105,19 @@ function findOneHopReverseDepFiles(db: BetterSqlite3Database, files: readonly st
* their one-hop reverse dependents (#1744) — are updated to avoid overwriting
* previously-set technique values on unchanged edges.
*/
// Chunk-size-keyed statement caches for backfillEdgeTechniquesAfterNativeOrchestrator's
// incremental-path technique/confidence-floor UPDATEs below, scoped per db like the
// techniqueBackfillStmtCache/confidenceFloorStmtCache pair in
// stages/build-edges.ts's applyEdgeTechniquesAfterNativeInsert (#1768, #1952).
const nativeOrchestratorTechniqueBackfillStmtCache = new WeakMap<
BetterSqlite3Database,
Map<number, SqliteStatement>
>();
const nativeOrchestratorConfidenceFloorStmtCache = new WeakMap<
BetterSqlite3Database,
Map<number, SqliteStatement>
>();

function backfillEdgeTechniquesAfterNativeOrchestrator(
db: BetterSqlite3Database,
isFullBuild: boolean,
Expand Down Expand Up @@ -2129,23 +2153,33 @@ function backfillEdgeTechniquesAfterNativeOrchestrator(
const tx = db.transaction(() => {
for (let i = 0; i < scopeFiles.length; i += CHUNK_SIZE) {
const chunk = scopeFiles.slice(i, i + CHUNK_SIZE);
const placeholders = chunk.map(() => '?').join(',');
db.prepare(
`UPDATE edges SET technique = 'ts-native'
WHERE kind = 'calls' AND technique IS NULL
AND source_id IN (
SELECT id FROM nodes WHERE file IN (${placeholders})
)`,
).run(...chunk);
// Lift resolved ts-native edges below the confidence floor for this chunk.
db.prepare(
`UPDATE edges SET confidence = ?
WHERE kind = 'calls' AND technique = 'ts-native'
AND confidence > 0 AND confidence < ?
const chunkSize = chunk.length;
const techniqueStmt = getOrCreatePerDbChunkStmt(
nativeOrchestratorTechniqueBackfillStmtCache,
db,
chunkSize,
(n) =>
`UPDATE edges SET technique = 'ts-native'
WHERE kind = 'calls' AND technique IS NULL
AND source_id IN (
SELECT id FROM nodes WHERE file IN (${placeholders})
SELECT id FROM nodes WHERE file IN (${Array.from({ length: n }, () => '?').join(',')})
)`,
).run(TS_NATIVE_CONFIDENCE_FLOOR, TS_NATIVE_CONFIDENCE_FLOOR, ...chunk);
);
techniqueStmt.run(...chunk);
// Lift resolved ts-native edges below the confidence floor for this chunk.
const confidenceStmt = getOrCreatePerDbChunkStmt(
nativeOrchestratorConfidenceFloorStmtCache,
db,
chunkSize,
(n) =>
`UPDATE edges SET confidence = ?
WHERE kind = 'calls' AND technique = 'ts-native'
AND confidence > 0 AND confidence < ?
AND source_id IN (
SELECT id FROM nodes WHERE file IN (${Array.from({ length: n }, () => '?').join(',')})
)`,
);
confidenceStmt.run(TS_NATIVE_CONFIDENCE_FLOOR, TS_NATIVE_CONFIDENCE_FLOOR, ...chunk);
}
});
tx();
Expand Down
Loading