Skip to content

Commit 6c4aca5

Browse files
authored
chore: add benchmark npm script and stale embeddings warning (#604)
* chore: add benchmark npm script and stale embeddings warning Add `npm run benchmark` script to make benchmark execution discoverable instead of requiring manual `node --import ./scripts/ts-resolve-loader.js` invocation. Warn users when embeddings predate the last graph rebuild so they know to re-run `codegraph embed` for fresh search results. Impact: 1 functions changed, 8 affected * fix: correct stale-embeddings off-by-one and simplify guards (#604) - Move setBuildMeta before stale-embeddings check so built_at reflects the current build, fixing the off-by-one where the warning fired one rebuild cycle late - Fix NaN-check order: validate embedTime before comparing - Remove redundant embedTime < now guard (always true for real timestamps) - Use single buildNow timestamp for both metadata and comparison Impact: 1 functions changed, 8 affected
1 parent ecc6f81 commit 6c4aca5

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true});require('fs').rmSync('.tsbuildinfo',{force:true})\"",
4444
"prepare": "npm run build:wasm && npm run build && husky && npm run deps:tree",
4545
"deps:tree": "node scripts/node-ts.js scripts/gen-deps.ts",
46+
"benchmark": "node --experimental-strip-types --import ./scripts/ts-resolve-loader.js scripts/benchmark.ts",
4647
"release": "commit-and-tag-version",
4748
"release:dry-run": "commit-and-tag-version --dry-run",
4849
"version": "node scripts/node-ts.js scripts/sync-native-versions.ts && git add package.json crates/codegraph-core/Cargo.toml"

src/domain/graph/builder/stages/finalize.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export async function finalize(ctx: PipelineContext): Promise<void> {
3737
symbols._langId = undefined;
3838
}
3939

40+
// Capture a single wall-clock timestamp for the current build — used for
41+
// both the stale-embeddings comparison and the persisted built_at metadata.
42+
const buildNow = new Date();
43+
4044
const nodeCount = (db.prepare('SELECT COUNT(*) as c FROM nodes').get() as { c: number }).c;
4145
const actualEdgeCount = (db.prepare('SELECT COUNT(*) as c FROM edges').get() as { c: number }).c;
4246
info(`Graph built: ${nodeCount} nodes, ${actualEdgeCount} edges`);
@@ -63,6 +67,22 @@ export async function finalize(ctx: PipelineContext): Promise<void> {
6367
}
6468
}
6569

70+
// Persist build metadata early so downstream checks (e.g. stale-embeddings)
71+
// can read the *current* build's built_at rather than the previous one.
72+
try {
73+
setBuildMeta(db, {
74+
engine: ctx.engineName,
75+
engine_version: ctx.engineVersion || '',
76+
codegraph_version: CODEGRAPH_VERSION,
77+
schema_version: String(schemaVersion),
78+
built_at: buildNow.toISOString(),
79+
node_count: nodeCount,
80+
edge_count: actualEdgeCount,
81+
});
82+
} catch (err) {
83+
warn(`Failed to write build metadata: ${(err as Error).message}`);
84+
}
85+
6686
// Orphaned embeddings warning
6787
if (hasEmbeddings) {
6888
try {
@@ -83,6 +103,27 @@ export async function finalize(ctx: PipelineContext): Promise<void> {
83103
}
84104
}
85105

106+
// Stale embeddings warning (built before current graph rebuild)
107+
if (hasEmbeddings) {
108+
try {
109+
const embedBuiltAt = (
110+
db.prepare("SELECT value FROM embedding_meta WHERE key = 'built_at'").get() as
111+
| { value: string }
112+
| undefined
113+
)?.value;
114+
if (embedBuiltAt) {
115+
const embedTime = new Date(embedBuiltAt).getTime();
116+
if (!Number.isNaN(embedTime) && embedTime < buildNow.getTime()) {
117+
warn(
118+
'Embeddings were built before the last graph rebuild. Run "codegraph embed" to update.',
119+
);
120+
}
121+
}
122+
} catch {
123+
/* ignore - embedding_meta table may not exist */
124+
}
125+
}
126+
86127
// Unused exports warning
87128
try {
88129
const unusedCount = (
@@ -108,21 +149,6 @@ export async function finalize(ctx: PipelineContext): Promise<void> {
108149
/* exported column may not exist on older DBs */
109150
}
110151

111-
// Persist build metadata
112-
try {
113-
setBuildMeta(db, {
114-
engine: ctx.engineName,
115-
engine_version: ctx.engineVersion || '',
116-
codegraph_version: CODEGRAPH_VERSION,
117-
schema_version: String(schemaVersion),
118-
built_at: new Date().toISOString(),
119-
node_count: nodeCount,
120-
edge_count: actualEdgeCount,
121-
});
122-
} catch (err) {
123-
warn(`Failed to write build metadata: ${(err as Error).message}`);
124-
}
125-
126152
closeDb(db);
127153

128154
// Write journal header after successful build

0 commit comments

Comments
 (0)