diff --git a/packages/plugins/sandbox-providers/daytona/src/plugin.test.ts b/packages/plugins/sandbox-providers/daytona/src/plugin.test.ts index 1620ca96f3f..f594a07e272 100644 --- a/packages/plugins/sandbox-providers/daytona/src/plugin.test.ts +++ b/packages/plugins/sandbox-providers/daytona/src/plugin.test.ts @@ -40,6 +40,7 @@ function createMockSandbox(overrides: { start: vi.fn().mockResolvedValue(undefined), stop: vi.fn().mockResolvedValue(undefined), recover: vi.fn().mockResolvedValue(undefined), + resize: vi.fn().mockResolvedValue(undefined), delete: vi.fn().mockResolvedValue(undefined), fs: { createFolder: vi.fn().mockResolvedValue(undefined), @@ -209,6 +210,149 @@ describe("Daytona sandbox provider plugin", () => { ); }); + it("passes configured resources to Daytona for image-based creation", async () => { + process.env.DAYTONA_API_KEY = "host-key"; + const sandbox = createMockSandbox(); + mockCreate.mockResolvedValue(sandbox); + + await plugin.definition.onEnvironmentAcquireLease?.({ + driverKey: "daytona", + companyId: "company-1", + environmentId: "env-1", + runId: "run-1", + agentId: "agent-1", + executionWorkspaceId: "workspace-1", + adapterType: "codex_local", + config: { + image: "node:20", + cpu: 4, + memory: 8, + disk: 20, + timeoutMs: 300000, + reuseLease: true, + }, + }); + + expect(mockCreate).toHaveBeenCalledTimes(1); + const [createParams] = mockCreate.mock.calls[0] as [Record]; + expect(createParams).toMatchObject({ + image: "node:20", + resources: { cpu: 4, memory: 8, disk: 20, gpu: undefined }, + }); + expect(createParams).not.toHaveProperty("snapshot"); + expect(sandbox.resize).not.toHaveBeenCalled(); + }); + + it("rejects resource settings for snapshot-backed creation", async () => { + process.env.DAYTONA_API_KEY = "host-key"; + const result = await plugin.definition.onEnvironmentValidateConfig?.({ + driverKey: "daytona", + config: { + snapshot: "base-snapshot", + cpu: 4, + memory: 8, + disk: 20, + timeoutMs: 300000, + reuseLease: true, + }, + }); + + expect(result).toEqual({ + ok: false, + errors: [ + "Daytona resource settings require image-backed sandbox creation; snapshot/default sandbox creation cannot override CPU, memory, disk, or GPU.", + ], + }); + }); + + it("rejects resource settings for default snapshot creation before creating a sandbox", async () => { + process.env.DAYTONA_API_KEY = "host-key"; + + await expect(plugin.definition.onEnvironmentAcquireLease?.({ + driverKey: "daytona", + companyId: "company-1", + environmentId: "env-1", + runId: "run-1", + agentId: "agent-1", + executionWorkspaceId: "workspace-1", + adapterType: "codex_local", + config: { + cpu: 4, + memory: 4, + timeoutMs: 300000, + reuseLease: false, + }, + })).rejects.toThrow( + "Daytona resource settings require image-backed sandbox creation; snapshot/default sandbox creation cannot override CPU, memory, disk, or GPU.", + ); + + expect(mockCreate).not.toHaveBeenCalled(); + }); + + it("records requested resources in lease metadata", async () => { + process.env.DAYTONA_API_KEY = "host-key"; + const sandbox = createMockSandbox(); + mockCreate.mockResolvedValue(sandbox); + + const lease = await plugin.definition.onEnvironmentAcquireLease?.({ + driverKey: "daytona", + companyId: "company-1", + environmentId: "env-1", + runId: "run-1", + agentId: "agent-1", + executionWorkspaceId: "workspace-1", + adapterType: "codex_local", + config: { + image: "daytonaio/sandbox:0.8.0", + cpu: 4, + memory: 8, + timeoutMs: 300000, + reuseLease: true, + }, + }); + + expect(lease?.metadata).toMatchObject({ cpu: 4, memory: 8 }); + expect(lease?.metadata).not.toHaveProperty("disk"); + expect(lease?.metadata).not.toHaveProperty("gpu"); + }); + + it("changes reusable-lease sentinel identity when resources change", async () => { + process.env.DAYTONA_API_KEY = "host-key"; + + const acquireWithCpu = async (cpu: number): Promise => { + const sandbox = createMockSandbox(); + mockCreate.mockResolvedValueOnce(sandbox); + await plugin.definition.onEnvironmentAcquireLease?.({ + driverKey: "daytona", + companyId: "company-1", + environmentId: "env-1", + runId: "run-1", + agentId: "agent-1", + executionWorkspaceId: "workspace-1", + adapterType: "codex_local", + config: { + image: "daytonaio/sandbox:0.8.0", + cpu, + timeoutMs: 300000, + reuseLease: true, + }, + }); + const uploadCall = sandbox.fs.uploadFile.mock.calls.find( + (call) => typeof call[1] === "string" && call[1].endsWith("reusable-sandbox-lease.json"), + ) as [Buffer, string, number] | undefined; + expect(uploadCall).toBeTruthy(); + const parsed = JSON.parse((uploadCall as [Buffer, string, number])[0].toString("utf8")) as { token: string }; + return parsed.token; + }; + + const tokenCpu1 = await acquireWithCpu(1); + const tokenCpu4 = await acquireWithCpu(4); + + expect(tokenCpu1).toBeTruthy(); + expect(tokenCpu4).toBeTruthy(); + expect(tokenCpu1).not.toEqual(tokenCpu4); + }); + it("deletes the sandbox if lease setup throws after sandbox creation", async () => { process.env.DAYTONA_API_KEY = "host-key"; const sandbox = createMockSandbox(); diff --git a/packages/plugins/sandbox-providers/daytona/src/plugin.ts b/packages/plugins/sandbox-providers/daytona/src/plugin.ts index cc5b294c1e0..fb367debe2f 100644 --- a/packages/plugins/sandbox-providers/daytona/src/plugin.ts +++ b/packages/plugins/sandbox-providers/daytona/src/plugin.ts @@ -145,6 +145,15 @@ function buildCreateParams( }; } +function hasResourceRequest(config: DaytonaDriverConfig): boolean { + return config.cpu != null || config.memory != null || config.disk != null || config.gpu != null; +} + +function validateResourceRequest(config: DaytonaDriverConfig): string | null { + if (!hasResourceRequest(config) || config.image) return null; + return "Daytona resource settings require image-backed sandbox creation; snapshot/default sandbox creation cannot override CPU, memory, disk, or GPU."; +} + function buildSandboxLabels(input: { companyId: string; environmentId: string; @@ -250,6 +259,13 @@ function workspaceSentinelToken(input: { image: input.config.image, snapshot: input.config.snapshot, target: input.config.target, + // Include resource-shaping inputs so changing the requested allocation + // expires old reusable leases and forces a fresh sandbox instead of + // reusing a previously provisioned (e.g. one-CPU) sandbox. + cpu: input.config.cpu, + memory: input.config.memory, + disk: input.config.disk, + gpu: input.config.gpu, })) .digest("hex"); } @@ -349,6 +365,12 @@ function leaseMetadata(input: { reuseLease: input.config.reuseLease, remoteCwd: input.remoteCwd, resumedLease: input.resumedLease, + // Record the resources Paperclip attempted to request so future diagnosis + // can compare requested allocation against what Daytona provisioned. + ...(input.config.cpu != null ? { cpu: input.config.cpu } : {}), + ...(input.config.memory != null ? { memory: input.config.memory } : {}), + ...(input.config.disk != null ? { disk: input.config.disk } : {}), + ...(input.config.gpu != null ? { gpu: input.config.gpu } : {}), ...(input.workspaceSentinel ? { workspaceSentinel: input.workspaceSentinel } : {}), }; } @@ -414,6 +436,10 @@ async function createSandbox( params: PluginEnvironmentAcquireLeaseParams | PluginEnvironmentProbeParams, config: DaytonaDriverConfig, ): Promise { + const resourceRequestError = validateResourceRequest(config); + if (resourceRequestError) { + throw new Error(resourceRequestError); + } const client = createDaytonaClient(config); const createParams = buildCreateParams(config, buildSandboxLabels({ companyId: params.companyId, @@ -421,9 +447,10 @@ async function createSandbox( runId: "runId" in params ? params.runId : undefined, reuseLease: config.reuseLease, })); - return await client.create(createParams, { + const sandbox = await client.create(createParams, { timeout: toTimeoutSeconds(config.timeoutMs), }); + return sandbox; } async function getSandbox(config: DaytonaDriverConfig, sandboxId: string): Promise { @@ -546,6 +573,10 @@ const plugin = definePlugin({ if (!config.apiKey && !(process.env.DAYTONA_API_KEY?.trim())) { errors.push("Daytona sandbox environments require an API key in config or DAYTONA_API_KEY."); } + const resourceRequestError = validateResourceRequest(config); + if (resourceRequestError) { + errors.push(resourceRequestError); + } for (const [key, value] of Object.entries({ cpu: config.cpu, memory: config.memory,