ensure-deps: fast path re-copies and re-codesigns better_sqlite3.node on every hook call (macOS disk churn)

Author: simonrowlandCreated Sep 23, 2026Updated Sep 30, 2026

Summary

In hooks/ensure-deps.mjs, the fast path in ensureNativeCompat() calls replaceActiveNativeBinaryFromCache() whenever better_sqlite3.abi<N>.node exists. It never checks whether the active better_sqlite3.node is already the right one.

So every hook invocation (every tool call, in every session) does all of the following:

  • copies the ~1.9 MB binary to better_sqlite3.node.staging-<pid>-<ts>;
  • runs codesign --sign - --force on it in a child process;
  • renames it over the active binary.

This is in 1.0.162 and 1.0.169, and the same code is on main.

Impact (macOS, measured)

This was on a machine running ~20 agent sessions plus ~18 codex workers with context-mode hooks.

  • An fs_usage -f diskio capture showed a restage roughly every 2 s. That was ~3 MB/s of writes from the copies and codesign, plus the APFS metadata churn from creating, renaming and unlinking the staging files.
  • Each swap also replaces the inode of a binary that other processes may have mapped (the scenario of #622).
  • After the patch below, the active binary's inode stayed unchanged over a 60 s window with the same workload.

Why a byte comparison doesn't work

Ad-hoc codesigning rewrites the file, so the active binary never matches its ABI cache byte for byte.

Proposed fix

After a successful swap, record a stamp next to the binary: the cache file's path, size and mtime, plus the installed binary's size, mtime and inode. On later calls, skip the copy, codesign and rename while the stamp still matches.

Anything else that replaces the active binary changes its stat and forces a fresh swap. That includes an npm rebuild or another ABI's swap. Failing to write the stamp only means the next call swaps again, which is today's behaviour.

Tested on 1.0.169 (Node ABI 141, macOS arm64):

  • the first run swaps and writes the stamp;
  • the second run skips (no rewrite, no codesign);
  • touching the ABI cache swaps again;
  • replacing the active binary from outside swaps again;
  • the steady state skips;
  • no .staging-* files are left behind, and better-sqlite3 still loads.
diff
--- a/hooks/ensure-deps.mjs
+++ b/hooks/ensure-deps.mjs
@@ -19,7 +19,7 @@
  * @see https://github.com/mksglu/context-mode/issues/203
  */
 
-import { existsSync, copyFileSync, renameSync, unlinkSync } from "node:fs";
+import { existsSync, copyFileSync, renameSync, unlinkSync, readFileSync, writeFileSync, statSync } from "node:fs";
 import { execSync } from "node:child_process";
 import { resolve, dirname } from "node:path";
 import { fileURLToPath, pathToFileURL } from "node:url";
@@ -124,10 +124,33 @@
     return true;
   } catch {
     return false;
+  }
+}
+
+// Identity of the cache file and of the active binary it was installed as.
+// The active binary cannot be byte-compared with the cache (codesigning
+// rewrites it), so the swap records both stats in a stamp file and skips
+// the copy + codesign + rename while they still match. Anything else that
+// replaces the active binary (npm rebuild, another ABI's swap) changes its
+// stat and forces a fresh swap.
+function nativeSwapStamp(abiCachePath, binaryPath) {
+  try {
+    const c = statSync(abiCachePath);
+    const b = statSync(binaryPath);
+    return `${abiCachePath}:${c.size}:${c.mtimeMs}|${b.size}:${b.mtimeMs}:${b.ino}`;
+  } catch {
+    return null;
   }
 }
 
 function replaceActiveNativeBinaryFromCache(abiCachePath, binaryPath) {
+  const stampPath = `${binaryPath}.swap-stamp`;
+  const current = nativeSwapStamp(abiCachePath, binaryPath);
+  if (current !== null) {
+    try {
+      if (readFileSync(stampPath, "utf8") === current) return;
+    } catch { /* no stamp yet — swap below */ }
+  }
   const tmpPath = `${binaryPath}.staging-${process.pid}-${Date.now()}`;
   try {
     copyFileSync(abiCachePath, tmpPath);
@@ -137,6 +160,15 @@
     try { unlinkSync(tmpPath); } catch { /* best effort cleanup */ }
     throw err;
   }
+  const installed = nativeSwapStamp(abiCachePath, binaryPath);
+  if (installed === null) return;
+  const stampTmp = `${stampPath}.${process.pid}-${Date.now()}`;
+  try {
+    writeFileSync(stampTmp, installed);
+    renameSync(stampTmp, stampPath);
+  } catch {
+    try { unlinkSync(stampTmp); } catch { /* best effort cleanup */ }
+  }
 }
 
 export function ensureNativeCompat(pluginRoot) {

Related: #622 (the staging + rename was introduced there to avoid AMFI SIGKILL on in-place overwrite; this change keeps that and just avoids redoing it when nothing changed).