fix(native): keep nativeDb open through finalize for correct build_meta#784
fix(native): keep nativeDb open through finalize for correct build_meta#784carlos-alm merged 3 commits intomainfrom
Conversation
…ta (#751) nativeDb was closed before finalize(), making persistBuildMetadata fall back to the JS (better-sqlite3) path. After native WAL truncation the JS connection's page cache was stale, causing the setBuildMeta write to silently fail and leaving stale version/engine metadata. This broke incremental builds by triggering a spurious "version changed" full rebuild on every subsequent run. Keep nativeDb alive through finalize so all metadata writes, advisory checks, and count queries use the native path. Refresh the JS DB beforehand so it has a valid page cache if finalize falls back to JS.
|
Claude finished @carlos-alm's task —— View job PR Review CompleteReviewing PR #784: fix(native): keep nativeDb open through finalize for correct build_meta Review Checklist
Root Cause AnalysisProblem Being Solved: Issue #751 where Root Cause:
Why This Approach: The fix keeps Risk Assessment: Very low risk - this is a targeted fix for a specific timing issue with proper fallback handling. Critical ConcernsNone identified. This is a clean fix addressing a real database consistency bug. Test Integrity ✅New Test Added: The PR adds a comprehensive test
Test Quality: The test is robust and directly validates the fix. No existing tests were weakened or removed. Code Quality Review ✅Implementation: The change is minimal and surgical:
No Security Issues: No command injection, SQL injection, or credential exposure risks. Performance: Actually improves performance by avoiding unnecessary connection close/reopen cycles. Backlog Compliance
Tier: 1 (zero-dep ✓ + foundation-aligned ✓) Final Recommendation
The fix directly addresses the stated problem (silent metadata write failures) with a proper solution (keeping native connection alive during finalize) rather than working around symptoms. Code quality is excellent with clear documentation of the WAL cache issue. |
engine_version was set to ctx.engineVersion which comes from the Rust crate's CARGO_PKG_VERSION via native.engineVersion(). When the Rust crate version drifts from the npm package version (e.g. Cargo.toml not bumped during release), build_meta stores the wrong version. Use CODEGRAPH_VERSION (the npm package.json version) for both codegraph_version and engine_version since the native engine is distributed as part of the npm package. Also strengthens the #751 regression test to assert engine_version equals the npm version and uses a full rebuild to ensure metadata is written for the small test fixture.
Greptile SummaryThis PR fixes a silent Confidence Score: 5/5This PR is safe to merge; the fix is targeted, well-understood, and the regression test exercises the exact failure path. The change is a minimal 3-line deletion + 4-line addition in the JS pipeline path. The root cause analysis is accurate: the WAL truncation inside No files require special attention. Important Files Changed
Sequence DiagramsequenceDiagram
participant P as pipeline.ts (runPipelineStages)
participant N as nativeDb (rusqlite)
participant J as ctx.db (better-sqlite3)
participant F as finalize.ts
Note over P: Before fix
P->>N: runAnalyses (suspend/resume WAL pattern)
P->>N: closeNativeDb → PRAGMA wal_checkpoint(TRUNCATE)
Note over J: WAL index cache invalidated ⚠️
N-->>P: ctx.nativeDb = undefined
P->>F: finalize()
F->>J: persistBuildMetadata (falls back to JS path)
Note over J: Stale WAL cache → setBuildMeta silently fails ❌
Note over P: After fix (this PR)
P->>N: runAnalyses (suspend/resume WAL pattern)
P->>J: refreshJsDb() → close + reopen better-sqlite3
Note over J: Fresh page cache ✅
P->>F: finalize() with nativeDb still open
F->>N: persistBuildMetadata (native path via ctx.nativeDb)
Note over N: setBuildMeta written correctly ✅
F->>N: closeDbPair → close nativeDb
F->>J: closeDbPair → close ctx.db
Reviews (1): Last reviewed commit: "fix(native): use npm version for engine_..." | Re-trigger Greptile |
Summary
Fixes #751
Two bugs fixed:
Stale WAL page cache on metadata write (commit
f45389e):closeNativeDb(ctx, 'post-analyses')closed the native DB beforefinalize(), forcingpersistBuildMetadatato fall back to the JS (better-sqlite3) path which had a stale WAL page cache after native WAL truncation — causingsetBuildMetawrites to silently fail. Fixed by keeping nativeDb alive through finalize and callingrefreshJsDbbeforehand.engine_versionused Rust crate version (commit5b67d11):engine_versionwas set toctx.engineVersionwhich comes fromCARGO_PKG_VERSIONvianative.engineVersion(). When the Rust crate version drifts from the npm package version (Cargo.toml not bumped during release),build_metastores the wrong version. Fixed by usingCODEGRAPH_VERSION(npm package.json version) forengine_versionin both nativeDb and JS DB write paths.Test plan
build_meta reflects actual engine and version after build (#751)— assertscodegraph_version,engine_version,engine,built_at, and countsengine_versionassertion verifies it equals the npm package version (not the Rust crate version)tsc --noEmit)