Skip to content

feat(sandbox): golden node_modules cache via reflink (skip install on hit) - #4357

Merged
pedrofrxncx merged 4 commits into
mainfrom
feat/sandbox-golden-nodemodules
Jul 7, 2026
Merged

pedrofrxncx merged 4 commits into
mainfrom
feat/sandbox-golden-nodemodules

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 7, 2026 •

Copy link
Copy Markdown
Collaborator

What

The reflink "last mile" on top of the deps cache (#4352): a per-(repo, packageManager, lockfile) golden node_modules on the shared node-local hostPath. On a hit, cp -a --reflink=always clones it into the repo — a copy-on-write clone that's near-instant regardless of tree size and skips bun install entirely. On a miss, the normal install runs and publishes its result as the golden for next time.

Proven on prod xfs earlier this session: reflink clone of a 686MB node_modules took ~1s, and it's write-safe (writing the clone left the source intact — CoW).

Why reflink, not hardlink

Hardlinking from a shared cache would hit the same ~1s but is unsafe here: shared inodes + uid-1000 pods running untrusted code + bun-doesn't-re-verify (see #4356) = one repo can poison another's packages. reflink is copy-on-write, so a pod's writes never reach the shared golden. It's the fast and safe option.

Safety

  • Per-repo key (credential-stripped cloneUrl) — the same cross-repo isolation boundary the deps cache uses; repos never share a golden. Within a repo it's one trust domain (sandbox access implies repo write).
  • Same-filesystem gate (st_dev match) before any reflink — else skip and install normally.
  • CoW isolation — verified.
  • Fail-safe everywhere — a partial/failed restore rms and falls back to bun install; publish is background, idempotent, atomic (temp + rename). Worst case: a no-op, never a broken boot.

Changes

  • setup/golden-cache.ts — keying (lockfileHash, goldenNodeModulesPath), sameFilesystem, tryRestoreGolden, publishGolden. Uses child_process.spawn("cp", [...]) (args array, no shell). Independent of install.ts so it doesn't conflict with docs(sandbox): correct deps-cache isolation comment — per-repo key is load-bearing #4356.
  • orchestrator.stepInstall — restore before install (hit → skip + mark succeeded), publish after a successful miss (async, best-effort).

Testing / validation status

  • 11 unit tests (keying, lockfile hashing, per-PM separation, token-refresh stability, same-fs). Orchestrator suite green, typecheck clean, daemon bundle builds.
  • ⚠️ The daemon lifecycle wiring is not yet exercised end-to-end in a pod. The reflink mechanism is proven on prod xfs, but the hit→skip→publish cycle in a live boot is not. kind can't test the fast path (ext4 → reflink fails → fallback); validating the fallback on kind and the hit path on a prod-xfs pod is the pre-merge gate. Do not merge before that.

Follow-up (not in this PR): golden GC (stale lockfile dirs accrue until node recycle, same as the deps cache).

🤖 Generated with Claude Code


Summary by cubic

Adds a golden node_modules cache using reflink to skip installs on cache hits. Publishing is now gated on a healthy dev server and the cache is pruned to prevent disk growth.

  • New Features
    • Per-(repo, package manager, lockfile) golden path under DEPS_CACHE_ROOT/golden/... keyed by a credential‑stripped repo URL.
    • Restore before install via cp -a --reflink=always when cache and workdir share a filesystem; on hit, skip bun install. cp exit code is authoritative; any failure falls back to a normal install.
    • Publish only after the dev server is healthy; the snapshot strips .vite/.cache, uses atomic temp+rename, and is best‑effort. After each publish, pruneGoldens() drops entries older than 7d and keeps the newest 5 per repo; restore touches mtime to keep active ones fresh.
    • Kill switch: off by default; enable with GOLDEN_CACHE_ENABLED=1|true (Helm depsCache.golden: true). Requires a reflink‑capable filesystem shared with /app; otherwise it falls back to a normal install.

Written for commit 0c69ccc. Summary will update on new commits.

Review in cubic

… hit)

A fresh sandbox pod runs a full `bun install` for every new branch. This
keeps a per-(repo, packageManager, lockfile) "golden" node_modules on the
shared node-local hostPath (DEPS_CACHE_ROOT) and, on a hit,
`cp --reflink=always`s it into the repo — a copy-on-write clone that's
near-instant regardless of tree size (~1s for 700MB on prod xfs) and skips
`bun install` entirely. On a miss the normal install runs, then publishes
its result as the golden for next time.

Safety mirrors the depsCache per-repo key (the sole cross-repo isolation
boundary — bun does not re-verify cache content): goldens are keyed by the
credential-stripped cloneUrl, so repos never share one. reflink is CoW, so a
pod mutating its own node_modules never writes through to the shared golden.
Only used when golden and node_modules share a filesystem (reflink needs it);
every failure path falls back to a normal install, so worst case it's a no-op.

- setup/golden-cache.ts: keying, same-fs gate, reflink restore/publish
  (atomic temp+rename), all best-effort.
- orchestrator.stepInstall: restore before install, publish after a miss.

Validation status: the reflink mechanism (speed + CoW isolation) is verified
on prod xfs; the daemon lifecycle wiring is unit-tested but not yet exercised
end-to-end in a pod. Kind can't test the fast path (ext4 → reflink fails →
fallback); a prod-xfs pod is needed for the hit path.
@pedrofrxncx

Copy link
Copy Markdown
Collaborator Author

Prod cross-mount reflink confirmed — the one real open risk is closed.

The concern was that /deps-cache (hostPath) and /app (emptyDir) might be separate mounts that can't reflink across — which is the case on kind (EXDEV, same dev number but reflink fails across Docker Desktop's overlay bind-mounts). On prod it works:

cp -a --reflink=always /deps-cache/.xtest /app/.xtest_dst   → OK
/deps-cache dev=10311   /app dev=10311

Verified on a live studio-sandbox-prod pod. So the fast path (reflink → skip install) is real on the actual prod filesystem. Kind is a false negative → code falls back to normal bun install, no regression.

Remaining gate before merge: exercise the daemon lifecycle (hit→skip→publish) through a live prod boot.

…e dev starts

Addresses the two pre-merge risks on the golden node_modules cache:

1. Independent off-switch. Golden touches the boot install path, so it no
   longer activates just because DEPS_CACHE_ROOT is set — it requires
   GOLDEN_CACHE_ENABLED (chart: `depsCache.golden`, default false). Ships
   dormant; toggle without redeploying or rolling back the daemon image.

2. Publish/dev-server race. `publishGolden` is now awaited before stepStart
   spawns the dev server, so the reflink snapshot is taken while node_modules
   is quiescent — previously it raced Vite writing node_modules/.vite and
   could capture an inconsistent tree. Miss-path only (~1s), no effect on
   warm boots.

Also corrects the depsCache comment in values.yaml (same false "bun's
lockfile integrity check catches tampering" claim fixed in install.ts by the
sibling PR — bun does not re-verify; the per-repo key is the sole boundary)
and drops the now-stale "near-instant needs a separate design" note (this is
that design).
@pedrofrxncx

Copy link
Copy Markdown
Collaborator Author

Addressed the two pre-merge risks (kill switch + publish race):

  1. Independent off-switch. Golden no longer activates just because DEPS_CACHE_ROOT is set — it now requires GOLDEN_CACHE_ENABLED (chart value depsCache.golden, default false). It ships dormant and can be toggled without redeploying or rolling back the daemon image. So merging + deploying does not turn it on; flip the flag when a target env is validated.

  2. Publish/dev-server race fixed. publishGolden is now awaited before stepStart spawns the dev server, so the reflink snapshot is taken while node_modules is quiescent (previously it raced Vite writing node_modules/.vite). Miss-path only (~1s), no effect on warm boots.

Also corrected the same false "bun re-verifies cache integrity" claim in the depsCache values.yaml comment (bun does not; the per-repo key is the sole boundary).

Remaining, documented as follow-ups (not blockers now that it's opt-in + dormant):

  • No golden GC — a golden ≈ a full node_modules on the node hostPath, unreaped until node recycle. Monitor disk before enabling widely.
  • Sticky bad-golden — a corrupt golden for an unchanged lockfile would be reused until node recycle. Low residual risk now that publish snapshots a quiescent tree via an atomic (temp+rename, cp-exit-checked) reflink, but there's no per-golden self-heal on dev-start failure.
  • Non-hoisted workspace/monorepo repos — only the install-root node_modules is cloned; untested for nested-node_modules layouts.

Still gated on a live prod-xfs boot before enabling (depsCache.golden=true).

…ky bad goldens)

Addresses the two residual risks on the golden node_modules cache.

Sticky bad golden — a golden built from a broken-but-exit-0 install used to be
publishable and would then be reused by every later branch. Publishing now
happens only after the dev server is confirmed healthy: stepInstall records a
pending publish, and the probe's `running` transition (entry.ts) triggers
publishPendingGolden(). A boot that never comes up never publishes, so a bad
install can't become a sticky golden. Snapshot also strips pod-local runtime
caches (.vite/.cache) — they churn and are per-pod — which also removes the
publish/dev-server write race (publish no longer needs to beat dev startup).

GC — goldens (≈ a full node_modules each) accrued on the node hostPath
unreaped. pruneGoldens() drops goldens past a TTL (7d) then caps per repo
(newest 5), run opportunistically after each publish; restore touches a
golden's mtime so an active lockfile never ages out. Safe to race a restore:
an in-flight reflink from a pruned golden just fails → fallback install, and
completed CoW clones are independent of the source.

19 unit tests (keying, lockfile hash, kill switch, TTL/cap/tmp-skip/per-repo
prune). Orchestrator suite green, typecheck clean, daemon builds.
@pedrofrxncx

Copy link
Copy Markdown
Collaborator Author

GC + sticky-bad-golden addressed.

Sticky bad golden — killed at the source. Publishing no longer happens right after install; it's deferred to when the dev server is confirmed healthy. stepInstall records a pending publish, and the probe's running transition (entry.ts) calls publishPendingGolden(). A boot that never comes up never publishes → a broken-but-exit-0 install can't become a golden that later branches reuse. No reactive-invalidation / oscillation, no cross-pod poison markers. The snapshot also strips .vite/.cache (pod-local, churny), which as a bonus removes the earlier publish-vs-dev-startup race entirely.

GC. pruneGoldens() bounds the store: drop goldens past a TTL (7d), then cap per repo (newest 5), run opportunistically after each publish. Restore touches the golden's mtime so an actively-used lockfile never ages out. Safe against a concurrent restore — an in-flight reflink from a pruned golden just fails → fallback install; completed CoW clones are independent of the source.

Tests: 19 unit (keying, lockfile hash, kill switch, and TTL / per-repo-cap / .tmp-skip / per-repo-independence for prune). Orchestrator suite green, typecheck clean, daemon builds.

Remaining follow-up (minor): non-hoisted workspace/monorepo repos — only the install-root node_modules is cloned; untested for nested layouts. Still gated behind depsCache.golden (default off) + a live prod-xfs boot before enabling.

@pedrofrxncx
pedrofrxncx merged commit beee678 into main Jul 7, 2026
18 of 19 checks passed
@pedrofrxncx
pedrofrxncx deleted the feat/sandbox-golden-nodemodules branch July 7, 2026 20:50
pedrofrxncx added a commit that referenced this pull request Jul 7, 2026
…emplate (#4360)

The golden node_modules changes (#4357) added the depsCache.golden env to the
SandboxTemplate but left Chart.yaml at 0.9.1. The release-sandbox-charts
workflow skips republishing an existing version, so OCI 0.9.1 still lacks the
template. Bump to 0.9.2 so it publishes; consumers then move targetRevision to
0.9.2 to pick it up (golden stays off by default).
pedrofrxncx added a commit that referenced this pull request Jul 7, 2026
…#4361)

The release-tagging job pushes a `[release]:` version-bump commit straight to
main. Two merges landing seconds apart (e.g. #4357 and #4360) each triggered a
run, and the second lost the push with `! [rejected] (fetch first)`. Re-running
can't recover it: the job checks out the original trigger commit, which is now
behind main, so the push is a permanent non-fast-forward.

Two fixes:
- concurrency group (cancel-in-progress: false) serializes runs so bumps queue
  instead of racing.
- the push rebases its bump onto the latest main and retries (up to 5x) instead
  of failing on non-fast-forward. The bump only touches version manifests, so it
  rebases cleanly over unrelated merges (the actual failure here: #4360 touched
  a Helm chart, not package.json). A genuine same-manifest conflict still fails
  loudly rather than guessing.
tlgimenes pushed a commit that referenced this pull request Jul 16, 2026
…TS.md (#4475)

Mined ~300 merged PRs for the delta between the first PR proposal and the
work added before merge. The dominant shape: a first draft ships the happy
path, then a hardening pass (often a different engineer who takes over the
branch and merges — the vibecoder→engineer handoff) adds the same categories
every time. Encoded those 9 categories as an actionable first-pass checklist,
each citing a real PR (#4008, #4230, #4365, #4409, #4426, #4445, #4357, #4355,
#4449, #4350, #4373, #4416, #4446, #4461, #4134, #4469).

Covers: variant/edge-case handling, tenant/permission scoping, concurrency &
silent-data-loss, test-per-fix + invert-bug-tests + real-PG, dead-code/knip,
lifecycle symmetry + state reset, kill-switch/GC/git-exclude for risky infra,
input validation vs over-engineering, compile-time type-safety.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
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