Skip to content

feat(sandbox): node-local package cache to speed up dependency install - #4352

Merged
pedrofrxncx merged 3 commits into
mainfrom
feat/sandbox-deps-cache
Jul 7, 2026
Merged

pedrofrxncx merged 3 commits into
mainfrom
feat/sandbox-deps-cache

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Every new branch provisions a fresh sandbox pod with an empty emptyDir at /app, so each one re-downloads and re-extracts its entire dependency tree from the registry. This adds an opt-in, node-local package cache (a hostPath shared across sandbox pods on a node) so bun install reuses an already-populated store.

Off by default — enable per environment with depsCache.enabled=true.

Changes

  • chart (sandbox-env): depsCache block mounts a hostPath at /deps-cache (default /var/cache/studio-sandbox-deps), with a small chown init container (hostPath dirs ignore fsGroup) and a DEPS_CACHE_ROOT env var on the sandbox container.
  • daemon (setup/install.ts): depsCacheEnv() derives BUN_INSTALL_CACHE_DIR=<root>/bun/<sha256(cloneUrl)> per repo. The cloneUrl is credential-stripped before hashing, so repos never share cache entries — a sandbox running untrusted code can only touch its own repo's cache (which that code could already reach via the repo itself).

Measurements (kind, idle node, casaevideo-tanstack ~703MB node_modules)

Install path Time
Cold (empty cache) ~6s
Warm (shared cache) ~3.4s

Roughly halves install and eliminates registry egress on the warm path.

Honest caveats

  • No hardlinking under K8s. bun hardlinks from its cache into node_modules when they share a filesystem, which would make install near-instant. But the hostPath and the pod's emptyDir are separate mounts, so link() returns EXDEV and bun falls back to copyfile. The win is the saved download + extract, not a zero-copy link. Getting the last mile needs a prebaked read-only node_modules overlay, which is a separate design.
  • This is not the dominant cost of "new branch". Testing showed dependency install is ~6s on an idle node; the large install times seen in practice are mostly node CPU/memory contention (the sandbox pod caps at cpu limit 1, and install's extraction burst gets throttled). Vite/deco dev-server startup (~32–44s) remains the single biggest contributor and is untouched here by design.
  • Cache growth is unbounded for now; node-image GC recycles nodes long before it matters.

Testing

  • 6 unit tests for depsCacheEnv (cache-root/cloneUrl absence, per-repo keying, credential stripping, non-URL cloneUrls).
  • helm template verified across guard combinations (netinit on/off × depsCache on/off).
  • Deployed to local kind cluster; verified the mount, chown init, env var, per-repo cache dir population by the daemon, and the warm-vs-cold install split end-to-end.

🤖 Generated with Claude Code


Summary by cubic

Adds a node-local dependency cache for sandbox pods so bun install reuses a shared store on the same node, cutting warm install time and reducing registry egress.

  • New Features

    • Helm: depsCache hostPath at /deps-cache (default /var/cache/studio-sandbox-deps) with a chown init, plus DEPS_CACHE_ROOT on the sandbox container.
    • Daemon: depsCacheEnv() sets per-repo BUN_INSTALL_CACHE_DIR=<root>/bun/<hash> from a credential‑stripped cloneUrl; an explicit BUN_INSTALL_CACHE_DIR in config still wins. kind: cold ~6s → warm ~3.4s. No hardlinking under Kubernetes (hostPath and /app emptyDir are separate mounts), so bun copies; the win is skipping download + extract.
  • Migration

    • Enable with depsCache.enabled=true (optionally set depsCache.hostPath).
    • No app changes. Cache growth is unbounded; hostPath has no size limit and a runaway pod can fill node disk, so enable with care.

Written for commit 6fa9b80. Summary will update on new commits.

Review in cubic

New branches provision fresh pods with an empty `emptyDir`, so every
sandbox re-downloads and re-extracts its full dependency tree. Add an
opt-in node-local package cache (hostPath, shared across pods on a node)
so `bun install` reuses an already-populated store instead of hitting the
registry each time.

- chart: `depsCache` (off by default) mounts a hostPath at `/deps-cache`
  with a chown init container and exposes `DEPS_CACHE_ROOT`.
- daemon: `depsCacheEnv()` derives `BUN_INSTALL_CACHE_DIR` per repo, keyed
  by the credential-stripped cloneUrl so repos never share cache entries.

Measured on kind (idle node): cold install ~6s, warm (shared cache) ~3.4s.
Hardlinking from the cache into node_modules is not available under K8s
(the hostPath and the pod emptyDir are separate mounts → link() EXDEV), so
bun falls back to copyfile; the win is the saved download + extract, not a
zero-copy link.
values.yaml claimed hardlinks work under readOnlyRootFilesystem:true, but
the hostPath cache and /app emptyDir are separate mounts (link() EXDEV →
copyfile always). Fix the numbers and flag the hostPath DiskPressure/DoS
surface. Soften the install.ts poisoning claim to name the cross-tenant
public/template case and lean on bun's lockfile integrity check.
@pedrofrxncx
pedrofrxncx merged commit 1f9655c into main Jul 7, 2026
17 checks passed
@pedrofrxncx
pedrofrxncx deleted the feat/sandbox-deps-cache branch July 7, 2026 17:48
decocms Bot pushed a commit that referenced this pull request Jul 7, 2026
PR: #4352 feat(sandbox): node-local package cache to speed up dependency install
Bump type: minor

- @decocms/sandbox (packages/sandbox/package.json): 1.9.8 -> 1.10.0

Deploy-Scope: both
pedrofrxncx added a commit that referenced this pull request Jul 7, 2026
#4352 added the depsCache template + values but did not bump the chart;
the publish workflow skips existing versions, so 0.9.0 would never pick
up the new template. Patch bump: additive, opt-in, default off.
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