fix(natives): resolve better-sqlite3 by fresh fs walk, not cached resolver - #25
Merged
Merged
Conversation
…olver `ak sync` intermittently ended with a false "[natives] 1/1 agentdb location(s) on WASM fallback (data-loss writes)" even though the heal step had just reported the location native — and a fresh process confirmed it native. Root cause: bsq3Root() used createRequire().resolve(), whose process-wide resolution cache (Module._pathCache/_realpathCache) goes stale after an in-process `npm install` (the aidefence heal / a version upgrade) dedupes the tree. The cached root pointed at a nested better-sqlite3 that npm had just removed, so the binding check on that dead path failed — a false negative in sync's convergence proof. agentdbLocations() reads with plain fs and was already correct; only the resolver-backed lookup lied. Fix: bsq3Root() now walks up the node_modules chain with fs.existsSync on every call — the node-resolution equivalent, but reading disk fresh, immune to in-process tree mutation. Same semantics for the resolvable/native/WASM cases (existing tests unchanged); adds coverage for hoisted resolution and the nested→hoisted mid-process move that triggered the bug.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ak synccould end with a false failure even when everything was actually healthy:The heal step reported the location native, yet the final convergence proof reported WASM fallback — and a fresh
nodeprocess confirmed the on-disk state was native and healthy. So the end state was fine; the proof lied.Root cause
bsq3Root()resolved better-sqlite3 viacreateRequire().resolve(). Node's resolution cache (Module._pathCache/_realpathCache) is process-wide and goes stale after an in-processnpm installreshapesnode_modules. During onesync:healNatives()resolves + builds native better-sqlite3 (priming the cache; the tree has a nestedagentic-flow/node_modules/agentdb).healAidefence()runsnpm install @claude-flow/aidefenceinto the ruflo tree → dedupes the nested agentdb away.bsq3Root()again → gets the cached root pointing at the now-deleted nested path → binding check fails → false WASM.agentdbLocations()uses plainfs.existsSyncand correctly saw the tree shrink 2→1; only the resolver-backed lookup was stale.Fix
bsq3Root()now walks up thenode_moduleschain withfs.existsSyncon every call — the Node-resolution equivalent, but reading disk fresh, so it's immune to in-process tree mutation (matching howagentdbLocations()already reads).ruflo/node_modules/agentdb→ruflo/node_modules/better-sqlite3layout) and the nested→hoisted mid-process move that triggered the bug.Verification
npm run checkgreen (exit 0): typecheck, eslint, markdownlint, build, 76 kit + 20 statusline tests. Verified live on the reporting machine:ak statusshows✓ natives native better-sqlite3 in 1 agentdb location(s)andak syncnow reports✓ converged — no failing subsystems.🤖 Generated with Claude Code