The Open Framework for Building Agents
fix(eve): resolve just-bash templates under the runtime sandbox cache (#4607)
roboeve[bot]88edd75aabd8
diff --git a/.changeset/just-bash-portable-template.md b/.changeset/just-bash-portable-template.mdnew file mode 100644index 000000000..dab368c3c--- /dev/null+++ b/.changeset/just-bash-portable-template.md@@ -0,0 +1,5 @@+---+"eve": patch+---++just-bash sandboxes now start when the app runs from a different path than the one it was built in. The prepared artifact stores the template key, and eve resolves it under the runtime sandbox cache, so copying `.output` and `.eve/sandbox-cache` to a new root no longer fails with `SandboxTemplateNotProvisionedError`.diff --git a/packages/eve/src/execution/sandbox/bindings/just-bash.scenario.test.ts b/packages/eve/src/execution/sandbox/bindings/just-bash.scenario.test.tsindex 8f2353366..55b9dfd7f 100644--- a/packages/eve/src/execution/sandbox/bindings/just-bash.scenario.test.ts+++ b/packages/eve/src/execution/sandbox/bindings/just-bash.scenario.test.ts@@ -1,5 +1,5 @@import { existsSync } from "node:fs";-import { mkdir, readFile, writeFile } from "node:fs/promises";+import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";import { join } from "node:path";import { decodeBytesToUtf8, defineCommand, MountableFs, ReadWriteFs } from "just-bash";@@ -444,6 +444,36 @@ describe("just-bash provider", () => {expect(result.stdout.trim().split("\n")).toEqual(["/workspace/skills/weather.md"]);});+ it("opens a prewarmed template after the app moves to a different root", async () => {+ const buildRoot = await createTemporaryCacheDirectory("build-root");+ const runtimeRoot = await createTemporaryCacheDirectory("runtime-root");+ const provider = createJustBashProvider();++ const prepared = await provider.prepare({+ appRoot: buildRoot,+ seedFiles: [+ {+ content: "# Weather skill\n",+ path: "/workspace/skills/weather.md",+ },+ ],+ });+ // Build in CI, then ship the cache to a different app root (#4606).+ await cp(buildRoot, runtimeRoot, { recursive: true });+ await rm(buildRoot, { force: true, recursive: true });++ const handle = await provider.openSession({+ appRoot: runtimeRoot,+ prepared,+ sandboxName: "session-after-move",+ });+ const result = await handle.sandbox.run({+ command: "find /workspace -maxdepth 3 -type f | sort",+ });++ expect(result.stdout.trim().split("\n")).toEqual(["/workspace/skills/weather.md"]);+ });+it("rejects existing session state from a different prepared artifact", async () => {const appRoot = await createTemporaryCacheDirectory("seed-session");const provider = createJustBashProvider();diff --git a/packages/eve/src/execution/sandbox/bindings/just-bash.ts b/packages/eve/src/execution/sandbox/bindings/just-bash.tsindex f9d2e7f22..aaa9e0702 100644--- a/packages/eve/src/execution/sandbox/bindings/just-bash.ts+++ b/packages/eve/src/execution/sandbox/bindings/just-bash.ts@@ -42,7 +42,8 @@ import { SandboxTemplateNotProvisionedError } from "#shared/sandbox-template-errconst JUST_BASH_CACHE_DIRECTORY_NAME = "just-bash";export const JUST_BASH_PROVIDER_NAME = "just-bash";-type JustBashPreparedArtifact = { readonly templateRootPath: string };+// Holds the template key, not its path: the build and runtime app roots can differ.+type JustBashPreparedArtifact = { readonly templateKey: string };type JustBashSessionState = {readonly generation: string;readonly rootPath: string;@@ -70,16 +71,16 @@ export function createJustBashSandboxProvider(// can reuse it. The roots are local files, so there is nothing to sweep.const implementation: ReturnType<typeof createJustBashSandboxProvider> = {async prepare(context) {- const templateIdentity = createSandboxProviderIdentity({+ const templateKey = createSandboxProviderIdentity({...environmentIdentity,resources: sandboxProviderResourceIdentity(context.resources),sourceRevision: context.sourceRevision,}).slice(0, 24);- const templateRootPath = resolveTemplateRootPath(context.storagePath, templateIdentity);+ const templateRootPath = resolveTemplateRootPath(context.storagePath, templateKey);if (await pathExists(templateRootPath)) {await touchDirectory(templateRootPath);context.log?.("reusing cached template filesystem");- return { templateRootPath };+ return { templateKey };}const temporaryTemplateRootPath = `${templateRootPath}.${randomUUID()}.tmp`;@@ -88,7 +89,7 @@ export function createJustBashSandboxProvider(autoInstall,host: context.host,rootPath: temporaryTemplateRootPath,- sessionKey: `prepare-${templateIdentity}`,+ sessionKey: `prepare-${templateKey}`,storagePath: context.storagePath,});const templateSession = buildSandboxSession(@@ -112,14 +113,14 @@ export function createJustBashSandboxProvider(await rename(temporaryTemplateRootPath, templateRootPath);published = true;} catch (error) {- if (await pathExists(templateRootPath)) return { templateRootPath };+ if (await pathExists(templateRootPath)) return { templateKey };throw error;}} finally {await templateSandbox.dispose();if (!published) await rm(temporaryTemplateRootPath, { force: true, recursive: true });}- return { templateRootPath };+ return { templateKey };},async resume(context, artifactValue, stateValue) {const artifact = requirePreparedJustBashArtifact(artifactValue);@@ -137,7 +138,10 @@ export function createJustBashSandboxProvider(async start(context, _openOptions, artifactValue) {const artifact = requirePreparedJustBashArtifact(artifactValue);const rootPath = sessionRootPath(context, artifact);- await ensureSessionRoot(artifact, rootPath);+ await ensureSessionRoot(+ resolveTemplateRootPath(context.storagePath, artifact.templateKey),+ rootPath,+ );return {handle: await openHandle(context, rootPath, options),state: {@@ -167,18 +171,15 @@ function sessionRootPath();}-async function ensureSessionRoot(- artifact: JustBashPreparedArtifact,- sessionRootPath: string,-): Promise<void> {+async function ensureSessionRoot(templateRootPath: string, sessionRootPath: string): Promise<void> {if (await pathExists(sessionRootPath)) return;- if (!(await pathExists(artifact.templateRootPath))) {+ if (!(await pathExists(templateRootPath))) {throw new SandboxTemplateNotProvisionedError({providerName: JUST_BASH_PROVIDER_NAME,- templateKey: artifact.templateRootPath,+ templateKey: templateRootPath,});}- await copyDirectoryAtomically(artifact.templateRootPath, sessionRootPath);+ await copyDirectoryAtomically(templateRootPath, sessionRootPath);}async function openHandle(@@ -248,10 +249,10 @@ export async function pruneJustBashSandboxTemplates(input: {function requirePreparedJustBashArtifact(artifact: SandboxPreparedArtifact,): JustBashPreparedArtifact {- if (!isSandboxPreparedArtifactRecord(artifact) || typeof artifact.templateRootPath !== "string") {+ if (!isSandboxPreparedArtifactRecord(artifact) || typeof artifact.templateKey !== "string") {throw new Error("Invalid prepared just-bash artifact.");}- return { templateRootPath: artifact.templateRootPath };+ return { templateKey: artifact.templateKey };}function requireJustBashSessionState(state: SandboxPreparedArtifact): JustBashSessionState {diff --git a/packages/eve/src/sandbox/providers/just-bash.ts b/packages/eve/src/sandbox/providers/just-bash.tsindex 3397b1454..635aaf098 100644--- a/packages/eve/src/sandbox/providers/just-bash.ts+++ b/packages/eve/src/sandbox/providers/just-bash.ts@@ -8,7 +8,7 @@ export type JustBashEnvironmentOptions = JustBashSandboxCreateOptions;export const JustBashSandbox = defineSandboxProvider<JustBashEnvironmentOptions,undefined,- { readonly templateRootPath: string },+ { readonly templateKey: string },{ readonly generation: string; readonly rootPath: string; readonly version: 2 },SandboxSession>({