Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): share concurrent first sandbox start across sessions (#3873)

Andrew Barbaa6ae201c30a5
diff --git a/.changeset/share-concurrent-sandbox-start.md b/.changeset/share-concurrent-sandbox-start.md
new file mode 100644
index 000000000..e95190674
--- /dev/null
+++ b/.changeset/share-concurrent-sandbox-start.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Parallel tool calls from subagents that share a sandbox no longer race to start it. In one process they now wait for a single start, and a Docker sandbox that loses the container name race to another process attaches to the winner's container instead of failing with `Conflict. The container name ... is already in use`.
diff --git a/packages/eve/src/execution/sandbox/bindings/docker.integration.test.ts b/packages/eve/src/execution/sandbox/bindings/docker.integration.test.ts
index 4ef8aa765..ac2931ea5 100644
--- a/packages/eve/src/execution/sandbox/bindings/docker.integration.test.ts
+++ b/packages/eve/src/execution/sandbox/bindings/docker.integration.test.ts
@@ -492,6 +492,29 @@ describe("Docker provider create", () => {
expect(findCall(calls, (args) => args[0] === "run")).toBeUndefined();
});
+ it("attaches to the peer container when another process wins the create race", async () => {
+ const appRoot = await createScratchDirectory("eve-docker-sandbox-");
+ let peerCreated = false;
+ const { calls, cli } = createFakeDockerCli((args) => {
+ if (isContainerInspect(args)) {
+ return peerCreated ? { exitCode: 0, stdout: "true\n" } : { exitCode: 1 };
+ }
+ if (args[0] === "run") {
+ peerCreated = true;
+ return {
+ exitCode: 125,
+ stderr: `docker: Error response from daemon: Conflict. The container name "/${PROVIDER_CONTAINER_NAME}" is already in use by container "d6b4ad87".`,
+ };
+ }
+ return undefined;
+ });
+
+ await createEngine({ cli }).openSession({ appRoot, sandboxName: SESSION_KEY });
+
+ expect(calls.filter(({ args }) => args[0] === "run")).toHaveLength(1);
+ expect(findCall(calls, (args) => args[0] === "start")).toBeUndefined();
+ });
+
async function createRunningSessionHandle(input: {
readonly respond?: (args: readonly string[]) => FakeResponse | undefined;
readonly options?: DockerSandboxEnvironmentOptions;
diff --git a/packages/eve/src/execution/sandbox/bindings/docker.ts b/packages/eve/src/execution/sandbox/bindings/docker.ts
index 99549e3e7..f145c32e1 100644
--- a/packages/eve/src/execution/sandbox/bindings/docker.ts
+++ b/packages/eve/src/execution/sandbox/bindings/docker.ts
@@ -1,5 +1,6 @@
import type { SandboxNetworkPolicy } from "#shared/sandbox-network-policy.js";
import type { SandboxSession } from "#shared/sandbox-session.js";
+import { toErrorMessage } from "#shared/errors.js";
type NetworkPolicySandboxSession = SandboxSession & {
setNetworkPolicy(policy: SandboxNetworkPolicy): Promise<void>;
};
@@ -110,6 +111,17 @@ export function createDockerSandboxProvider(
return daemonCheck;
}
+ async function inspectContainerRunning(containerName: string): Promise<boolean | undefined> {
+ const inspect = await cli.run([
+ "container",
+ "inspect",
+ "--format",
+ "{{.State.Running}}",
+ containerName,
+ ]);
+ return inspect.exitCode === 0 ? inspect.stdout.trim() === "true" : undefined;
+ }
+
async function openDockerSession(
context: SandboxProviderSessionContext,
openOptions: Readonly<DockerSandboxRuntimeOptions> | undefined,
@@ -125,36 +137,39 @@ export function createDockerSandboxProvider(
templateKey: artifact.imageReference,
});
}
- const inspect = await cli.run([
- "container",
- "inspect",
- "--format",
- "{{.State.Running}}",
- containerName,
- ]);
- if (inspect.exitCode === 0) {
- if (inspect.stdout.trim() !== "true") {
- expectDockerSuccess(
- await cli.run(["start", containerName]),
- `restart sandbox session container "${containerName}"`,
- );
- }
- } else {
+ let running = await inspectContainerRunning(containerName);
+ if (running === undefined) {
if (!createIfMissing) {
throw new Error(`Docker sandbox session container "${containerName}" no longer exists.`);
}
- await startDockerContainer({
- cli,
- containerName,
- image: artifact.imageReference,
- initialNetworkPolicy: openOptions?.networkPolicy ?? "allow-all",
- options,
- role: "session",
- tags: {
- agent: context.session.id,
- sessionId: context.session.id,
- },
- });
+ try {
+ await startDockerContainer({
+ cli,
+ containerName,
+ image: artifact.imageReference,
+ initialNetworkPolicy: openOptions?.networkPolicy ?? "allow-all",
+ options,
+ role: "session",
+ tags: {
+ agent: context.session.id,
+ sessionId: context.session.id,
+ },
+ });
+ running = true;
+ } catch (error) {
+ // Session container names derive from the session, so another
+ // process can win the create race. Accept only that exact loss.
+ running = /is already in use/u.test(toErrorMessage(error))
+ ? await inspectContainerRunning(containerName)
+ : undefined;
+ if (running === undefined) throw error;
+ }
+ }
+ if (!running) {
+ expectDockerSuccess(
+ await cli.run(["start", containerName]),
+ `restart sandbox session container "${containerName}"`,
+ );
}
const containerIdentity = await resolveDockerHandleIdentity(cli, containerName);
const session = buildSandboxSession(
diff --git a/packages/eve/src/execution/sandbox/ensure.test.ts b/packages/eve/src/execution/sandbox/ensure.test.ts
index 79189a50b..84f881cf5 100644
--- a/packages/eve/src/execution/sandbox/ensure.test.ts
+++ b/packages/eve/src/execution/sandbox/ensure.test.ts
@@ -27,12 +27,13 @@ function fixture(setup?: () => void, returnCopy = false) {
onSessionStop: stopSandbox,
};
});
+ const start = vi.fn(async () => ({ handle: await create(), state: null }));
const provider = defineSandboxProvider({
name: "test",
environment: () => ({
prepare: async () => null,
resume: create,
- start: async () => ({ handle: await create(), state: null }),
+ start,
}),
});
const environment = provider.environment();
@@ -55,33 +56,24 @@ function fixture(setup?: () => void, returnCopy = false) {
workspaceResourceRoot: { logicalPath: "", rootEntries: [] },
},
};
- return { create, deleteSandbox, registry, shutdownSandbox, stopSandbox };
+ return { create, deleteSandbox, registry, shutdownSandbox, start, stopSandbox };
}
async function open(
registry: RuntimeSandboxRegistry,
id = "session-1",
state: Parameters<typeof ensureSandboxAccess>[0]["state"] = null,
- principalId?: string,
+ nodeId = "__root__",
) {
const context = new ContextContainer();
- const auth =
- principalId === undefined
- ? null
- : {
- attributes: {},
- authenticator: "test",
- principalId,
- principalType: "user",
- };
context.set(SessionKey, {
- auth: { current: auth, initiator: auth },
+ auth: { current: null, initiator: null },
sessionId: id,
turn: { id: "turn", sequence: 0 },
});
return await contextStorage.run(context, async () => {
const access = await ensureSandboxAccess({
compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
- nodeId: "__root__",
+ nodeId,
registry,
sessionId: id,
state,
@@ -89,6 +81,28 @@ async function open(
return { access, sandbox: await access.get() };
});
}
+function inheritingChildRegistry(rootRegistry: RuntimeSandboxRegistry): RuntimeSandboxRegistry {
+ const parent = rootRegistry.sandbox;
+ if (parent === null) throw new Error("Root registry has no sandbox.");
+ return {
+ sandbox: {
+ definition: {
+ kind: "parent",
+ logicalPath: "sandbox.ts",
+ selector: defineParentSandbox(),
+ revisionHash: "child-hash",
+ sourceId: "child-sandbox",
+ sourceKind: "module",
+ },
+ inheritance: {
+ definition: parent.definition,
+ nodeId: "__root__",
+ workspaceResourceRoot: parent.workspaceResourceRoot,
+ },
+ workspaceResourceRoot: { logicalPath: "", rootEntries: [] },
+ },
+ };
+}
afterEach(() => shutdownActiveSandboxHandles());
describe("ensureSandboxAccess", () => {
@@ -128,27 +142,8 @@ describe("ensureSandboxAccess", () => {
it("passes empty live options when a child inherits its parent sandbox", async () => {
const value = fixture();
- const parent = value.registry.sandbox;
- const registry: RuntimeSandboxRegistry = {
- sandbox: {
- definition: {
- kind: "parent",
- logicalPath: "sandbox.ts",
- selector: defineParentSandbox(),
- revisionHash: "child-hash",
- sourceId: "child-sandbox",
- sourceKind: "module",
- },
- inheritance: {
- definition: parent.definition,
- nodeId: "__root__",
- workspaceResourceRoot: parent.workspaceResourceRoot,
- },
- workspaceResourceRoot: { logicalPath: "", rootEntries: [] },
- },
- };
- await open(registry);
+ await open(inheritingChildRegistry(value.registry));
expect(value.create).toHaveBeenCalledOnce();
});
@@ -180,6 +175,40 @@ describe("ensureSandboxAccess", () => {
expect(value.create).toHaveBeenCalledOnce();
});
+ it("starts one sandbox when separate accesses to a shared session open concurrently", async () => {
+ const value = fixture();
+ const [owner, subagent] = await Promise.all([open(value.registry), open(value.registry)]);
+ expect(value.start).toHaveBeenCalledOnce();
+ expect(value.create).toHaveBeenCalledTimes(2);
+ expect(await subagent.access.captureState()).toEqual(await owner.access.captureState());
+ });
+
+ it("shares the parent's start with a subagent that inherits its sandbox", async () => {
+ const value = fixture();
+ const [owner, subagent] = await Promise.all([
+ open(value.registry),
+ open(inheritingChildRegistry(value.registry), "session-1", null, "child"),
+ ]);
+ expect(value.start).toHaveBeenCalledOnce();
+ expect(value.create).toHaveBeenCalledTimes(2);
+ expect(await subagent.access.captureState()).toEqual(await owner.access.captureState());
+ });
+
+ it("lets only one waiter retry after a shared start fails", async () => {
+ const value = fixture();
+ value.start.mockImplementationOnce(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ throw new Error("start failed");
+ });
+ const results = await Promise.allSettled([
+ open(value.registry),
+ open(value.registry),
+ open(value.registry),
+ ]);
+ expect(results.map(({ status }) => status)).toEqual(["rejected", "fulfilled", "fulfilled"]);
+ expect(value.start).toHaveBeenCalledTimes(2);
+ });
+
it("retries session setup after a selector failure", async () => {
let attempts = 0;
const value = fixture(() => {
diff --git a/packages/eve/src/execution/sandbox/ensure.ts b/packages/eve/src/execution/sandbox/ensure.ts
index dda63dd5d..cd9664ba5 100644
--- a/packages/eve/src/execution/sandbox/ensure.ts
+++ b/packages/eve/src/execution/sandbox/ensure.ts
@@ -44,6 +44,12 @@ interface OpenedSandbox {
readonly sandbox: RuntimeSandboxSession;
}
+// Parent and subagent sessions that share a sandbox each build their own
+// access, so concurrent first use in one process would otherwise race to
+// start the same provider sandbox. Late arrivals resume from the winner's
+// state instead.
+const pendingSandboxStarts = new Map<string, Promise<SandboxSessionState | null>>();
+
export async function ensureSandboxAccess(input: EnsureSandboxAccessInput): Promise<SandboxAccess> {
let persisted: SandboxSessionState | null = input.state?.session ?? null;
let opened: OpenedSandbox | undefined;
@@ -215,6 +221,35 @@ export async function ensureSandboxAccess(input: EnsureSandboxAccessInput): Prom
return await resumePersisted(definition, session);
}
+ const startKey = `${inherited?.nodeId ?? input.nodeId}\0${input.sessionId}`;
+ // A failed start wakes every waiter at once; re-check so only one retries.
+ let concurrentStart = pendingSandboxStarts.get(startKey);
+ while (concurrentStart !== undefined) {
+ persisted = await concurrentStart;
+ if (persisted !== null) return await resumePersisted(definition, session);
+ concurrentStart = pendingSandboxStarts.get(startKey);
+ }
+
+ const starting = startHandle(definition, session);
+ const sharedStart = starting.then(
+ () => persisted,
+ () => null,
+ );
+ pendingSandboxStarts.set(startKey, sharedStart);
+ try {
+ return await starting;
+ } finally {
+ if (pendingSandboxStarts.get(startKey) === sharedStart) {
+ pendingSandboxStarts.delete(startKey);
+ }
+ }
+ }
+
+ async function startHandle(
+ definition: Extract<typeof registered.definition, { readonly kind: "independent" }>,
+ session: SandboxProviderSessionContext["session"],
+ ): Promise<SandboxProviderHandle> {
+ const inherited = registered.inheritance;
if (inherited !== undefined) {
await open(
getSandboxEnvironmentRuntime(definition.environment),