The Open Framework for Building Agents
fix(sandbox): tolerate concurrent template commits (#3699)
Ben Pankow1b6366fd963d
diff --git a/.changeset/concurrent-docker-template-commits.md b/.changeset/concurrent-docker-template-commits.mdnew file mode 100644index 000000000..1f5dabdf9--- /dev/null+++ b/.changeset/concurrent-docker-template-commits.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Tolerate concurrent Docker sandbox template publication when another Eve application publishes the same deterministic image first.diff --git a/packages/eve/src/execution/sandbox/bindings/docker-templates.ts b/packages/eve/src/execution/sandbox/bindings/docker-templates.tsindex d9107fbca..947fde62c 100644--- a/packages/eve/src/execution/sandbox/bindings/docker-templates.ts+++ b/packages/eve/src/execution/sandbox/bindings/docker-templates.ts@@ -3,6 +3,7 @@ import { mkdir, readdir, rm, stat, utimes, writeFile } from "node:fs/promises";import { dirname, join } from "node:path";import { type DockerCli, createDockerCli } from "#execution/sandbox/bindings/docker-cli.js";+import { DOCKER_SANDBOX_LABEL } from "#execution/sandbox/bindings/docker-container.js";import type { ResolvedDockerSandboxOptions } from "#execution/sandbox/bindings/docker-options.js";import { expectDockerSuccess } from "#execution/sandbox/bindings/docker-utils.js";import {@@ -125,6 +126,38 @@ export async function dockerImageExists(cli: DockerCli, imageReference: string):return result.exitCode === 0;}+export async function commitDockerTemplateImage(input: {+ readonly cli: DockerCli;+ readonly containerIdentity: string;+ readonly imageReference: string;+ readonly templateKey: string;+}): Promise<"committed" | "reused"> {+ const result = await input.cli.run([+ "commit",+ "--change",+ `LABEL ${DOCKER_SANDBOX_LABEL}=1`,+ "--change",+ `LABEL ${DOCKER_SANDBOX_LABEL}.role=template`,+ "--change",+ `LABEL ${DOCKER_SANDBOX_LABEL}.template-key=${input.templateKey}`,+ input.containerIdentity,+ input.imageReference,+ ]);+ if (result.exitCode === 0) {+ return "committed";+ }++ // Template image tags are daemon-scoped while preparation locks are+ // app-scoped. Accept only the loser of that exact publication race.+ const publishedByPeer =+ /already(?:\s*|-)?exists/iu.test(`${result.stderr}\n${result.stdout}`) &&+ (await dockerImageExists(input.cli, input.imageReference));+ if (!publishedByPeer) {+ expectDockerSuccess(result, `commit sandbox template image "${input.imageReference}"`);+ }+ return "reused";+}+export async function ensureDockerBaseImage(cli: DockerCli,options: ResolvedDockerSandboxOptions,diff --git a/packages/eve/src/execution/sandbox/bindings/docker.integration.test.ts b/packages/eve/src/execution/sandbox/bindings/docker.integration.test.tsindex 953c25c3b..4ef8aa765 100644--- a/packages/eve/src/execution/sandbox/bindings/docker.integration.test.ts+++ b/packages/eve/src/execution/sandbox/bindings/docker.integration.test.ts@@ -248,6 +248,55 @@ describe("Docker provider prewarm", () => {expect(cleanup?.args.at(-1)).toBe(buildContainerName);});+ it("reuses a concurrently committed template image", async () => {+ const appRoot = await createScratchDirectory("eve-docker-sandbox-");+ let publishedReference: string | undefined;+ const { calls, cli } = createFakeDockerCli((args) => {+ if (args[0] === "image" && args[1] === "inspect") {+ return publishedReference === args.at(-1)+ ? { exitCode: 0, stdout: "sha256:peer\n" }+ : { exitCode: 1, stderr: "No such image" };+ }+ if (args[0] === "commit") {+ publishedReference = args.at(-1);+ return {+ exitCode: 1,+ stderr: `AlreadyExists: image "${publishedReference}" already exists`,+ };+ }+ return undefined;+ });++ const result = await createEngine({ cli }).prepare({ appRoot, seedFiles: [] });+ expect(result.imageReference).toBe(publishedReference);+ expect(+ calls.filter(({ args }) =>+ publishedReference === undefined ? false : isImageInspect(args, publishedReference),+ ),+ ).toHaveLength(2);+ expect(findCall(calls, (args) => args[0] === "rm" && args[1] === "-f")).toBeDefined();+ });++ it("preserves commit failures other than a concurrent publication", async () => {+ const appRoot = await createScratchDirectory("eve-docker-sandbox-");+ let committedReference: string | undefined;+ const { cli } = createFakeDockerCli((args) => {+ if (args[0] === "image" && args[1] === "inspect") {+ return { exitCode: 1, stderr: "No such image" };+ }+ if (args[0] === "commit") {+ committedReference = args.at(-1);+ return { exitCode: 1, stderr: "disk full" };+ }+ return undefined;+ });++ await expect(createEngine({ cli }).prepare({ appRoot, seedFiles: [] })).rejects.toThrow(+ /Failed to commit sandbox template image .*: disk full/u,+ );+ expect(committedReference).toMatch(/^eve-sandbox-template:/u);+ });+it("mounts compiled resources read-only while hydrating a writable template", async () => {const appRoot = await createScratchDirectory("eve-docker-sandbox-");const resourcesPath = join(appRoot, "compiled-resources");diff --git a/packages/eve/src/execution/sandbox/bindings/docker.ts b/packages/eve/src/execution/sandbox/bindings/docker.tsindex 7e59f8490..91147e485 100644--- a/packages/eve/src/execution/sandbox/bindings/docker.ts+++ b/packages/eve/src/execution/sandbox/bindings/docker.ts@@ -2,7 +2,6 @@ import type { MutableNetworkSandboxSession } from "#shared/sandbox-session.js";import { randomUUID } from "node:crypto";import {- DOCKER_SANDBOX_LABEL,runDockerBaseSetup,startDockerContainer,stopDockerContainerIfRunning,@@ -19,6 +18,7 @@ import {} from "#execution/sandbox/bindings/docker-options.js";import { createDockerInternalSession } from "#execution/sandbox/bindings/docker-session.js";import {+ commitDockerTemplateImage,dockerImageExists,dockerTemplateImageReference,ensureDockerBaseImage,@@ -276,20 +276,15 @@ export function createDockerSandboxProvider(`stop template build container "${buildContainerName}"`,);context.log?.(`committing template image "${imageReference}"`);- expectDockerSuccess(- await cli.run([- "commit",- "--change",- `LABEL ${DOCKER_SANDBOX_LABEL}=1`,- "--change",- `LABEL ${DOCKER_SANDBOX_LABEL}.role=template`,- "--change",- `LABEL ${DOCKER_SANDBOX_LABEL}.template-key=${templateKey}`,- buildContainerIdentity,- imageReference,- ]),- `commit sandbox template image "${imageReference}"`,- );+ const commit = await commitDockerTemplateImage({+ cli,+ containerIdentity: buildContainerIdentity,+ imageReference,+ templateKey,+ });+ if (commit === "reused") {+ context.log?.("reusing concurrently published template image");+ }await touchDockerTemplateMarker(markerPath, imageReference);} finally {await cli.run(["rm", "-f", buildContainerName]).catch(() => {});