From d6e235cbcfb8fe292d55388b14b209dc77225190 Mon Sep 17 00:00:00 2001 From: Nicky Leach Date: Wed, 29 Jul 2026 12:40:16 -0700 Subject: [PATCH] perf(sandbox-providers): drop nvm sourcing from exec wrappers (#10443) ## Thinking Path > - Paperclip keeps agent work on a controlled execution plane. > - Sandbox exec wrappers run on the hot path for agent commands. > - The current change removes the explicit `nvm.sh` load step from those wrappers. > - The sandbox image already restores PATH through profile startup. > - This pull request keeps profile sourcing where the wrapper still needs it and drops only the `nvm.sh` load step. > - The result is a smaller command path with the same node and agent CLI resolution. ## Linked Issues or Issue Description No public GitHub issue exists for this change. Problem: The sandbox exec wrappers spent extra time sourcing `nvm.sh` before each command. The sandbox image already restores PATH in `/etc/profile.d/00-restore-env.sh`, so that explicit `nvm.sh` work was redundant. Proposed solution: Remove the `nvm.sh` source step from all six wrappers. Keep the profile sourcing that the provider still needs for PATH setup. Alternatives considered: Keep the existing shell setup and accept the launch cost. That keeps the current behavior, but it leaves the hot path slower than needed. Roadmap alignment: This change keeps the sandbox command path small and predictable. It does not change the adapter contract or the node resolution rules. ## What Changed - Removed `nvm.sh` sourcing from all six sandbox exec wrappers. - Kept profile sourcing where the provider still needs it for PATH setup. - Switched Modal to a non-login shell because the script now sources profiles itself. - Updated wrapper tests to assert that built commands do not source `nvm.sh`. ## Verification - Local TypeScript typecheck passed in each changed package. - Focused provider tests passed for Daytona, E2B, Modal, exe-dev, Cloudflare bridge, and adapter-utils. - One Daytona test failure is pre-existing and unrelated to this change. ## Risks - This change alters shell startup for sandbox exec paths. - A provider that depends on implicit shell setup may need a follow-up. - The current tests cover command shape, but they do not cover every runtime shell path. ## Model Used OpenAI Codex, GPT-5, tool use. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used with version and capability details - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal or instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip --- .../adapter-utils/src/ssh-fixture.test.ts | 43 +++++++++++ packages/adapter-utils/src/ssh.ts | 34 ++++++--- .../sandbox-providers/SANDBOX-REQUIREMENTS.md | 70 ++++++++++++++++++ .../bridge-template/src/exec.test.ts | 3 + .../cloudflare/bridge-template/src/exec.ts | 5 +- .../daytona/src/plugin.test.ts | 72 +++++++------------ .../sandbox-providers/daytona/src/plugin.ts | 51 ++++++------- .../sandbox-providers/e2b/src/plugin.test.ts | 3 + .../sandbox-providers/e2b/src/plugin.ts | 16 ++--- .../exe-dev/src/plugin.test.ts | 8 ++- .../sandbox-providers/exe-dev/src/plugin.ts | 5 +- .../modal/src/plugin.test.ts | 7 +- .../sandbox-providers/modal/src/plugin.ts | 16 ++--- 13 files changed, 225 insertions(+), 108 deletions(-) create mode 100644 packages/plugins/sandbox-providers/SANDBOX-REQUIREMENTS.md diff --git a/packages/adapter-utils/src/ssh-fixture.test.ts b/packages/adapter-utils/src/ssh-fixture.test.ts index ba2da73222..0b8a45a0bf 100644 --- a/packages/adapter-utils/src/ssh-fixture.test.ts +++ b/packages/adapter-utils/src/ssh-fixture.test.ts @@ -170,6 +170,49 @@ describe("ssh env-lab fixture", () => { await stopSshEnvLabFixture(statePath); }, SSH_FIXTURE_TEST_TIMEOUT_MS); + it("builds a remote script that sources login profiles but no nvm", async () => { + const target = await buildSshSpawnTarget({ + spec: { + host: "ssh.example.test", + port: 22, + username: "ssh-user", + remoteCwd: "/srv/paperclip/workspace", + remoteWorkspacePath: "/srv/paperclip/workspace", + privateKey: null, + knownHosts: null, + strictHostKeyChecking: true, + }, + command: "node", + args: ["--version"], + env: { FOO: "bar" }, + }); + + // The remote script rides the last ssh argument. The SSH target is an + // operator-configured host that can expose `node` only through a login + // profile, so the wrapper sources the profiles. It no longer sources + // `nvm.sh`; a profile that adds nvm still runs. + const remoteScript = String(target.args.at(-1) ?? ""); + expect(remoteScript).not.toContain("nvm.sh"); + expect(remoteScript).not.toContain("NVM_DIR"); + // Source /etc/profile so a host that exposes the PATH through + // /etc/profile.d scripts still resolves node and the agent CLI. + expect(remoteScript).toContain("/etc/profile"); + expect(remoteScript).toContain(".profile"); + expect(remoteScript).toContain(".bash_profile"); + expect(remoteScript).toContain(".zprofile"); + // Fall back to .bashrc when no .bash_profile exists, so a host that adds + // nvm in .bashrc still resolves node under a non-login SSH command. + expect(remoteScript).toContain(".bashrc"); + // The last ssh argument wraps the script as `sh -c '...'`, so the inner + // quotes are escaped. Assert the command still runs: cd, env, and the argv. + expect(remoteScript).toContain("cd "); + expect(remoteScript).toContain("/srv/paperclip/workspace"); + expect(remoteScript).toContain("exec env "); + expect(remoteScript).toContain("node"); + expect(remoteScript).toContain("--version"); + await target.cleanup(); + }); + it("rejects invalid environment variable keys when constructing SSH spawn targets", async () => { await expect( buildSshSpawnTarget({ diff --git a/packages/adapter-utils/src/ssh.ts b/packages/adapter-utils/src/ssh.ts index d3efffd725..b2aa909c9a 100644 --- a/packages/adapter-utils/src/ssh.ts +++ b/packages/adapter-utils/src/ssh.ts @@ -1166,14 +1166,22 @@ export async function runSshCommand( } } - // Mirror buildSshSpawnTarget: source login profiles first, then run - // `env KEY=VAL cmd` so user-supplied identity overrides win over anything - // a profile re-exports. Without this, a remote profile that resets HOME - // / NVM_DIR / etc. would silently undo the explicit env passed in here. + // Mirror buildSshSpawnTarget: source the login profiles first, then run + // `env KEY=VAL cmd` so user-supplied identity overrides win over anything a + // profile re-exports. The SSH target is an operator-configured host, not a + // Paperclip sandbox image, so it can expose `node` or an agent CLI only + // through a login profile; a non-login SSH command would miss that PATH. + // Source `/etc/profile` first so a host that exposes the PATH through + // `/etc/profile.d` scripts still resolves node and the agent CLI. + // The script no longer sources `nvm.sh`; a profile that adds nvm still runs. + // .bash_profile typically sources .bashrc itself; only source .bashrc + // directly when no .bash_profile exists, so a host that adds nvm in + // .bashrc still resolves node without a double-run of the setup. const envArgs = envEntries.map(([key, value]) => `${key}=${shellQuote(value)}`); const remoteScript = [ + 'if [ -f /etc/profile ]; then . /etc/profile >/dev/null 2>&1 || true; fi', 'if [ -f "$HOME/.profile" ]; then . "$HOME/.profile" >/dev/null 2>&1 || true; fi', - 'if [ -f "$HOME/.bash_profile" ]; then . "$HOME/.bash_profile" >/dev/null 2>&1 || true; fi', + 'if [ -f "$HOME/.bash_profile" ]; then . "$HOME/.bash_profile" >/dev/null 2>&1 || true; elif [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" >/dev/null 2>&1 || true; fi', 'if [ -f "$HOME/.zprofile" ]; then . "$HOME/.zprofile" >/dev/null 2>&1 || true; fi', envArgs.length > 0 ? `exec env ${envArgs.join(" ")} sh -c ${shellQuote(remoteCommand)}` @@ -1223,12 +1231,22 @@ export async function buildSshSpawnTarget(input: { .filter((entry): entry is [string, string] => typeof entry[1] === "string") .map(([key, value]) => `${key}=${shellQuote(value)}`); const remoteCommandParts = [shellQuote(input.command), ...input.args.map((arg) => shellQuote(arg))].join(" "); + // Source the login profiles first, then run `env KEY=VAL cmd` so + // user-supplied identity overrides win over anything a profile re-exports. + // The SSH target is an operator-configured host, not a Paperclip sandbox + // image, so it can expose `node` or an agent CLI only through a login + // profile; a non-login SSH command would miss that PATH. Source + // `/etc/profile` first so a host that exposes the PATH through + // `/etc/profile.d` scripts still resolves node and the agent CLI. The script + // no longer sources `nvm.sh`; a profile that adds nvm still runs. + // .bash_profile typically sources .bashrc itself; only source .bashrc + // directly when no .bash_profile exists, so a host that adds nvm in + // .bashrc still resolves node without a double-run of the setup. const remoteScript = [ + 'if [ -f /etc/profile ]; then . /etc/profile >/dev/null 2>&1 || true; fi', 'if [ -f "$HOME/.profile" ]; then . "$HOME/.profile" >/dev/null 2>&1 || true; fi', - 'if [ -f "$HOME/.bash_profile" ]; then . "$HOME/.bash_profile" >/dev/null 2>&1 || true; fi', + 'if [ -f "$HOME/.bash_profile" ]; then . "$HOME/.bash_profile" >/dev/null 2>&1 || true; elif [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" >/dev/null 2>&1 || true; fi', 'if [ -f "$HOME/.zprofile" ]; then . "$HOME/.zprofile" >/dev/null 2>&1 || true; fi', - 'export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"', - '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" >/dev/null 2>&1 || true', `cd ${shellQuote(input.spec.remoteCwd)}`, envArgs.length > 0 ? `exec env ${envArgs.join(" ")} ${remoteCommandParts}` diff --git a/packages/plugins/sandbox-providers/SANDBOX-REQUIREMENTS.md b/packages/plugins/sandbox-providers/SANDBOX-REQUIREMENTS.md new file mode 100644 index 0000000000..1d762085a3 --- /dev/null +++ b/packages/plugins/sandbox-providers/SANDBOX-REQUIREMENTS.md @@ -0,0 +1,70 @@ +# Sandbox Runtime Requirements + +This document states the sandbox environment as a contract. The sandbox owner +must meet this contract. The Paperclip runtime does not build the environment at +exec time. The environment is a requirement, not a build step. + +This document states requirements. It does not state build steps. + +## Required on PATH + +- `node` must be installed and on the PATH. +- Each agent CLI that the run uses must be installed and on the PATH. The set of + agent CLIs includes `claude`, `codex`, `gemini`, and similar CLIs. +- The owner installs only the CLIs that the run uses. The owner does not need to + install a CLI that no run uses. + +## Runtime dependencies + +The sandbox execution and synchronization paths need more than `node` and the +agent CLIs. The owner must also supply these: + +- A POSIX shell as `sh`, normally `/bin/sh`. The runtime runs each command with + `sh -c