Skip to content

fix(heal): deterministic native better-sqlite3 via escalation ladder - #19

Merged
pacphi merged 1 commit into
mainfrom
deterministic-native-heal
Jul 14, 2026
Merged

pacphi merged 1 commit into
mainfrom
deterministic-native-heal

Conversation

@pacphi

@pacphi pacphi commented Jul 14, 2026

Copy link
Copy Markdown
Owner

Problem

healNatives did one plain npm install and hoped. Investigation showed a plain install only yields a native binding when npm's content store already holds a built copy of that exact version (verified: a fresh scratch install materialized a binding whose timestamps predate the project) — cache history, not scripts. On a cold store, npm ≥11.17's allow-scripts skips better-sqlite3's build script, leaving the location on WASM fallback and sync reporting transient FAILED noise that later self-resolves nondeterministically.

Fix

ensureNativeBsq3(dir) — an escalation ladder, verifying bsq3IsNative after each rung and stopping at the first binding:

  1. Plain install — sufficient on npm <11.17 or a warm content store.
  2. npm approve-scripts better-sqlite3 + npm rebuild better-sqlite3 — npm's own sanctioned path (what its warning tells you to run); pins the exact version into the location's package.json, harmless no-op on older npm. Rebuild also recovers a stale half-built build/ dir.
  3. npm run install in the resolved package root — explicit npm run is user-invoked, never gated by allow-scripts; fully deterministic.

The detail string names which rung succeeded; FAILED (with stderr tail) only if all three miss. bsq3Root() is extracted from bsq3IsNative() in natives.mjs for reuse.

Verification

  • pnpm test — 32 node:test pass (incl. new hermetic fixture tests for bsq3Root/bsq3IsNative) + statusline 20/20
  • E2E against a genuinely script-skipped install (--ignore-scripts, binding absent): rung 1 no-ops (package already present), rung 2 builds it — {"ok":true,"how":"native rebuilt (scripts approved)"}, binding verified on disk
  • Rung 3 proven independently from a fresh broken state (npm run install → binding built)
  • Live healNatives() on a healthy machine: already native everywhere (idempotent)

🤖 Generated with Claude Code

Plain 'npm install' only yields a native binding when npm's content store
happens to hold an already-built copy of the exact version — cache history,
not scripts. On a cold store, npm >=11.17's allow-scripts skips the build
script and the heal raced whatever eventually built the binding (transient
FAILED noise, WASM-fallback windows).

ensureNativeBsq3(dir) now escalates, verifying after each rung:
 1. plain install (npm <11.17, or warm content store)
 2. npm approve-scripts better-sqlite3 + npm rebuild — npm's sanctioned
    path; approve-scripts pins the exact version into the location's
    package.json (harmless no-op on older npm); rebuild also recovers a
    stale half-built build/ dir
 3. npm run install in the resolved package root — explicit 'npm run' is
    user-invoked and never gated by allow-scripts

natives.mjs: extract bsq3Root() from bsq3IsNative() for reuse.
tests/kit/natives.test.mjs: hermetic fixture tests for both helpers.

E2E-verified against a script-skipped install: pre-state no binding,
ladder returns 'native rebuilt (scripts approved)', binding present.
@pacphi
pacphi merged commit a28822a into main Jul 14, 2026
9 checks passed
@pacphi
pacphi deleted the deterministic-native-heal branch July 14, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant