The Open Framework for Building Agents
fix(eve): stop microsandboxes on dev shutdown (#887)
Colton Padden46b78b8f1aef
diff --git a/.changeset/clean-dev-microsandboxes.md b/.changeset/clean-dev-microsandboxes.mdnew file mode 100644index 000000000..e1057c749--- /dev/null+++ b/.changeset/clean-dev-microsandboxes.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Stop development microsandboxes on `eve dev` shutdown by discovering resources from their run labels and loading the application's installed microsandbox version during cleanup.diff --git a/packages/eve/src/execution/sandbox/bindings/docker.ts b/packages/eve/src/execution/sandbox/bindings/docker.tsindex e382eb6b1..6d64c1c62 100644--- a/packages/eve/src/execution/sandbox/bindings/docker.ts+++ b/packages/eve/src/execution/sandbox/bindings/docker.ts@@ -26,7 +26,6 @@ import {} from "#execution/sandbox/bindings/docker-templates.js";import { expectDockerSuccess } from "#execution/sandbox/bindings/docker-utils.js";import { writeSandboxSeedFiles } from "#execution/sandbox/bindings/local-backend-utils.js";-import { markDevelopmentSandboxBackendInitialized } from "#execution/sandbox/development-run.js";import { createLoggingSandboxSession } from "#execution/sandbox/logging-session.js";import { buildSandboxSession } from "#execution/sandbox/session.js";import type {@@ -189,7 +188,6 @@ export function createDockerSandboxBackend(},async create(createInput: SandboxBackendCreateInput): Promise<SandboxBackendHandle> {await ensureDaemon();- markDevelopmentSandboxBackendInitialized(DOCKER_BACKEND_NAME);const containerName =getDockerContainerName(createInput.existingMetadata) ?? createInput.sessionKey;diff --git a/packages/eve/src/execution/sandbox/bindings/microsandbox-runtime.ts b/packages/eve/src/execution/sandbox/bindings/microsandbox-runtime.tsindex 5c3142279..4894fa8d5 100644--- a/packages/eve/src/execution/sandbox/bindings/microsandbox-runtime.ts+++ b/packages/eve/src/execution/sandbox/bindings/microsandbox-runtime.ts@@ -24,6 +24,7 @@ import {} from "#execution/sandbox/bindings/microsandbox-platform.js";import { adaptMicrosandboxExecToSandboxProcess } from "#execution/sandbox/bindings/microsandbox-process.js";import {+ importInstalledEnginePackage,isEveDevEnvironment,loadOptionalEnginePackage,} from "#internal/application/optional-package-install.js";@@ -526,9 +527,14 @@ async function withProgressHeartbeat<T>(* Loads microsandbox only when its package and runtime are already* present — used by cleanup paths that must never trigger installs.*/-export async function loadMicrosandboxWithoutInstall(): Promise<MicrosandboxModule | null> {+export async function loadMicrosandboxWithoutInstall(+ appRoot: string,+): Promise<MicrosandboxModule | null> {try {- const module: MicrosandboxModule = await import("microsandbox");+ const module = await importInstalledEnginePackage<MicrosandboxModule>({+ appRoot,+ packageName: MICROSANDBOX_PACKAGE_NAME,+ });return module.isInstalled() ? module : null;} catch {return null;diff --git a/packages/eve/src/execution/sandbox/bindings/microsandbox-templates.ts b/packages/eve/src/execution/sandbox/bindings/microsandbox-templates.tsindex 4d837f395..66d207c25 100644--- a/packages/eve/src/execution/sandbox/bindings/microsandbox-templates.ts+++ b/packages/eve/src/execution/sandbox/bindings/microsandbox-templates.ts@@ -48,7 +48,7 @@ export async function pruneMicrosandboxTemplates(input: {if (index < retainCount || now - template.mtimeMs <= recentWindowMs) {return;}- await removeTemplateDirectory(template.path, template.metadata);+ await removeTemplateDirectory(input.appRoot, template.path, template.metadata);}),...directories.filter((directory) => directory.isTemporary)@@ -56,7 +56,7 @@ export async function pruneMicrosandboxTemplates(input: {if (now - directory.mtimeMs <= recentWindowMs) {return;}- await removeTemplateDirectory(directory.path, directory.metadata);+ await removeTemplateDirectory(input.appRoot, directory.path, directory.metadata);}),]);}@@ -123,6 +123,7 @@ async function readMicrosandboxTemplateDirectories(templatesDirectory: string):}async function removeTemplateDirectory(+ appRoot: string,path: string,metadata: MicrosandboxTemplateMetadata | null,): Promise<void> {@@ -130,7 +131,7 @@ async function removeTemplateDirectory(if (metadata === null) {return;}- const module = await loadMicrosandboxWithoutInstall();+ const module = await loadMicrosandboxWithoutInstall(appRoot);if (module !== null) {await removeSnapshotIfExists(module, metadata.snapshotName);}diff --git a/packages/eve/src/execution/sandbox/development-cleanup.test.ts b/packages/eve/src/execution/sandbox/development-cleanup.test.tsindex 8e6a90039..cecc29614 100644--- a/packages/eve/src/execution/sandbox/development-cleanup.test.ts+++ b/packages/eve/src/execution/sandbox/development-cleanup.test.ts@@ -42,9 +42,13 @@ const state = vi.hoisted(() => {};});-vi.mock("#execution/sandbox/bindings/docker-cli.js", () => ({- createDockerCli: () => state.dockerCli,-}));+vi.mock("#execution/sandbox/bindings/docker-cli.js", async (importOriginal) => {+ const actual = await importOriginal<typeof import("#execution/sandbox/bindings/docker-cli.js")>();+ return {+ ...actual,+ createDockerCli: () => state.dockerCli,+ };+});vi.mock("#execution/sandbox/bindings/microsandbox-runtime.js", () => ({createProviderName: (prefix: string, key: string) => `${prefix}-${key}`,@@ -84,7 +88,7 @@ describe("stopDevelopmentSandboxResources", () => {version: 2,});- await stopDevelopmentSandboxResources({ devRunId: "run-123" });+ await stopDevelopmentSandboxResources({ appRoot: "/tmp/eve-test", devRunId: "run-123" });expect(state.dockerCli.run).toHaveBeenCalledWith(["ps",@@ -124,25 +128,20 @@ describe("stopDevelopmentSandboxResources", () => {expect(state.microsandboxStopWithTimeout).toHaveBeenCalledWith(10_000);});- it("skips every backend when no development sandbox was initialized", async () => {- await stopDevelopmentSandboxResources({- backendNames: [],- devRunId: "run-123",- });-- expect(state.dockerCli.run).not.toHaveBeenCalled();- expect(state.loadMicrosandboxWithoutInstall).not.toHaveBeenCalled();- });-- it("does not probe Docker when only microsandbox was initialized", async () => {+ it("continues microsandbox cleanup when the Docker CLI is unavailable", async () => {+ const { DockerUnavailableError } = await import("#execution/sandbox/bindings/docker-cli.js");+ state.dockerCli.run.mockRejectedValue(new DockerUnavailableError());state.microsandboxModule.Sandbox.listWith.mockResolvedValueOnce([]);+ const log = vi.fn();await stopDevelopmentSandboxResources({- backendNames: ["microsandbox"],+ appRoot: "/tmp/eve-test",devRunId: "run-123",+ log,});- expect(state.dockerCli.run).not.toHaveBeenCalled();+ expect(log).not.toHaveBeenCalled();+ expect(state.loadMicrosandboxWithoutInstall).toHaveBeenCalledWith("/tmp/eve-test");expect(state.microsandboxModule.Sandbox.listWith).toHaveBeenCalledWith({labels: {"eve.backend": "microsandbox",diff --git a/packages/eve/src/execution/sandbox/development-cleanup.ts b/packages/eve/src/execution/sandbox/development-cleanup.tsindex 30aadc230..f87c5b303 100644--- a/packages/eve/src/execution/sandbox/development-cleanup.ts+++ b/packages/eve/src/execution/sandbox/development-cleanup.ts@@ -4,8 +4,7 @@ import {DOCKER_SANDBOX_LABEL,stopDockerContainerIfRunning,} from "#execution/sandbox/bindings/docker-container.js";-import { DOCKER_BACKEND_NAME } from "#execution/sandbox/bindings/docker.js";-import { createDockerCli } from "#execution/sandbox/bindings/docker-cli.js";+import { createDockerCli, DockerUnavailableError } from "#execution/sandbox/bindings/docker-cli.js";import {MICROSANDBOX_METADATA_VERSION,readSessionMetadata,@@ -17,7 +16,6 @@ import {removeSnapshotIfExists,stopAndSnapshotMicrosandboxSandbox,} from "#execution/sandbox/bindings/microsandbox-runtime.js";-import { MICROSANDBOX_BACKEND_NAME } from "#execution/sandbox/bindings/microsandbox.js";import {EVE_DEVELOPMENT_SANDBOX_METADATA_PATH_TAG,EVE_DEVELOPMENT_SANDBOX_RUN_ID_TAG,@@ -25,22 +23,14 @@ import {import { toErrorMessage } from "#shared/errors.js";export async function stopDevelopmentSandboxResources(input: {- readonly backendNames?: readonly string[];+ readonly appRoot: string;readonly devRunId: string;readonly log?: (message: string) => void;}): Promise<void> {- const backendNames = input.backendNames === undefined ? null : new Set(input.backendNames);- const cleanupTasks: Promise<void>[] = [];-- if (backendNames === null || backendNames.has(DOCKER_BACKEND_NAME)) {- cleanupTasks.push(stopDevelopmentDockerResources(input.devRunId));- }-- if (backendNames === null || backendNames.has(MICROSANDBOX_BACKEND_NAME)) {- cleanupTasks.push(stopDevelopmentMicrosandboxResources(input.devRunId, input.log));- }-- const errors = await Promise.allSettled(cleanupTasks);+ const errors = await Promise.allSettled([+ stopDevelopmentDockerResources(input.devRunId),+ stopDevelopmentMicrosandboxResources(input.appRoot, input.devRunId, input.log),+ ]);for (const error of errors) {if (error.status === "rejected") {@@ -55,11 +45,19 @@ async function stopDevelopmentDockerResources(devRunId: string): Promise<void> {`label=${DOCKER_SANDBOX_LABEL}=1`,`label=${DOCKER_SANDBOX_LABEL}.tag.${EVE_DEVELOPMENT_SANDBOX_RUN_ID_TAG}=${devRunId}`,];- const running = await cli.run([- "ps",- "-q",- ...labelFilters.flatMap((filter) => ["--filter", filter]),- ]);+ let running;+ try {+ running = await cli.run([+ "ps",+ "-q",+ ...labelFilters.flatMap((filter) => ["--filter", filter]),+ ]);+ } catch (error) {+ if (error instanceof DockerUnavailableError) {+ return;+ }+ throw error;+ }if (running.exitCode !== 0) {return;}@@ -71,10 +69,11 @@ async function stopDevelopmentDockerResources(devRunId: string): Promise<void> {}async function stopDevelopmentMicrosandboxResources(+ appRoot: string,devRunId: string,log: ((message: string) => void) | undefined,): Promise<void> {- const module = await loadMicrosandboxWithoutInstall();+ const module = await loadMicrosandboxWithoutInstall(appRoot);if (module === null) {return;}diff --git a/packages/eve/src/execution/sandbox/development-run.ts b/packages/eve/src/execution/sandbox/development-run.tsindex 4be43dc9d..75c19cbd9 100644--- a/packages/eve/src/execution/sandbox/development-run.ts+++ b/packages/eve/src/execution/sandbox/development-run.ts@@ -6,8 +6,6 @@ export const EVE_DEVELOPMENT_SANDBOX_RUN_ID_ENV = "EVE_DEVELOPMENT_SANDBOX_RUN_Iexport const EVE_DEVELOPMENT_SANDBOX_METADATA_PATH_TAG = "eve.metadataPath";export const EVE_DEVELOPMENT_SANDBOX_RUN_ID_TAG = "devRunId";-const initializedBackendsByRunId = new Map<string, Set<string>>();-export function createDevelopmentSandboxRunId(): string {return randomUUID();}@@ -42,25 +40,3 @@ export function withDevelopmentSandboxMetadataPathTag([EVE_DEVELOPMENT_SANDBOX_METADATA_PATH_TAG]: metadataPath,};}--export function markDevelopmentSandboxBackendInitialized(backendName: string): void {- const runId = getDevelopmentSandboxRunId();- if (runId === undefined) {- return;- }-- let initializedBackends = initializedBackendsByRunId.get(runId);- if (initializedBackends === undefined) {- initializedBackends = new Set();- initializedBackendsByRunId.set(runId, initializedBackends);- }- initializedBackends.add(backendName);-}--export function getInitializedDevelopmentSandboxBackendNames(runId: string): readonly string[] {- return [...(initializedBackendsByRunId.get(runId) ?? [])];-}--export function clearInitializedDevelopmentSandboxBackendNames(runId: string): void {- initializedBackendsByRunId.delete(runId);-}diff --git a/packages/eve/src/execution/sandbox/ensure.test.ts b/packages/eve/src/execution/sandbox/ensure.test.tsindex db07d14fc..e5b0c8c94 100644--- a/packages/eve/src/execution/sandbox/ensure.test.ts+++ b/packages/eve/src/execution/sandbox/ensure.test.ts@@ -21,11 +21,6 @@ import {countActiveSandboxHandles,shutdownActiveSandboxHandles,} from "#execution/sandbox/active-handles.js";-import {- clearInitializedDevelopmentSandboxBackendNames,- EVE_DEVELOPMENT_SANDBOX_RUN_ID_ENV,- getInitializedDevelopmentSandboxBackendNames,-} from "#execution/sandbox/development-run.js";import { ensureSandboxAccess } from "#execution/sandbox/ensure.js";import type { SandboxState } from "#sandbox/state.js";@@ -387,22 +382,6 @@ describe("ensureSandboxAccess", () => {await shutdownActiveSandboxHandles();expect(countActiveSandboxHandles()).toBe(0);});-- it("records the backend after a development sandbox is initialized", async () => {- process.env[EVE_DEVELOPMENT_SANDBOX_RUN_ID_ENV] = "dev-run-test";- const backend = createBackend();- const registry = createTestRegistry({}, backend);-- try {- const access = await ensure({ registry });- await access.get();-- expect(getInitializedDevelopmentSandboxBackendNames("dev-run-test")).toEqual(["test"]);- } finally {- clearInitializedDevelopmentSandboxBackendNames("dev-run-test");- delete process.env[EVE_DEVELOPMENT_SANDBOX_RUN_ID_ENV];- }- });});function createDeferred<T>() {diff --git a/packages/eve/src/execution/sandbox/ensure.ts b/packages/eve/src/execution/sandbox/ensure.tsindex 6e63e3065..3659a74f6 100644--- a/packages/eve/src/execution/sandbox/ensure.ts+++ b/packages/eve/src/execution/sandbox/ensure.ts@@ -13,7 +13,6 @@ import {} from "#runtime/compiled-artifacts-source.js";import { trackActiveSandboxHandle } from "#execution/sandbox/active-handles.js";import { waitForDevelopmentSandboxPrewarm } from "#execution/sandbox/development-prewarm.js";-import { markDevelopmentSandboxBackendInitialized } from "#execution/sandbox/development-run.js";import { prewarmAppSandboxes } from "#execution/sandbox/prewarm.js";import { waitForSandboxTemplatePrewarmLock } from "#execution/sandbox/template-prewarm-lock.js";import { buildCallbackContext } from "#context/build-callback-context.js";@@ -139,7 +138,6 @@ export async function ensureSandboxAccess(input: EnsureSandboxAccessInput): PromcreateInput,}),);- markDevelopmentSandboxBackendInitialized(backend.name);trackActiveSandboxHandle({backendName: backend.name,handle,diff --git a/packages/eve/src/internal/application/optional-package-install.ts b/packages/eve/src/internal/application/optional-package-install.tsindex 1b347f5dc..91442143e 100644--- a/packages/eve/src/internal/application/optional-package-install.ts+++ b/packages/eve/src/internal/application/optional-package-install.ts@@ -170,7 +170,8 @@ export async function loadOptionalEnginePackage<T>(input: {}}-async function importInstalledEnginePackage<T>(input: {+/** Imports an optional engine from the application without installing it. */+export async function importInstalledEnginePackage<T>(input: {readonly appRoot: string;readonly packageName: string;}): Promise<T> {diff --git a/packages/eve/src/internal/nitro/host/start-development-server.test.ts b/packages/eve/src/internal/nitro/host/start-development-server.test.tsindex ac7e6e83b..6e574da88 100644--- a/packages/eve/src/internal/nitro/host/start-development-server.test.ts+++ b/packages/eve/src/internal/nitro/host/start-development-server.test.ts@@ -407,7 +407,7 @@ describe("createDevelopmentServer", () => {await server.close();expect(mocks.stopDevelopmentSandboxResources).toHaveBeenCalledWith({- backendNames: [],+ appRoot: "/tmp/eve-test",devRunId: expect.any(String),log: expect.any(Function),});diff --git a/packages/eve/src/internal/nitro/host/start-development-server.ts b/packages/eve/src/internal/nitro/host/start-development-server.tsindex f3065863b..17b565b43 100644--- a/packages/eve/src/internal/nitro/host/start-development-server.ts+++ b/packages/eve/src/internal/nitro/host/start-development-server.ts@@ -27,10 +27,8 @@ import {} from "#execution/sandbox/bindings/local.js";import { startDevelopmentSandboxPrewarmInBackground } from "#execution/sandbox/development-prewarm.js";import {- clearInitializedDevelopmentSandboxBackendNames,createDevelopmentSandboxRunId,EVE_DEVELOPMENT_SANDBOX_RUN_ID_ENV,- getInitializedDevelopmentSandboxBackendNames,} from "#execution/sandbox/development-run.js";import type {DevelopmentServer,@@ -225,6 +223,7 @@ function addDevelopmentControlHandler(input: {}async function closeDevelopmentServerResources(input: {+ readonly appRoot: string;readonly authoredSourceWatcher: AuthoredSourceWatcherHandle | undefined;readonly devServer: NitroDevelopmentServer | undefined;readonly developmentSandboxRunId: string;@@ -258,7 +257,7 @@ async function closeDevelopmentServerResources(input: {}await attempt(() =>stopDevelopmentSandboxResources({- backendNames: getInitializedDevelopmentSandboxBackendNames(input.developmentSandboxRunId),+ appRoot: input.appRoot,devRunId: input.developmentSandboxRunId,log: (message) => console.warn(`[eve:dev] ${message}`),}),@@ -513,6 +512,7 @@ async function startNitroDevelopmentServer(const close = (): Promise<void> => {closePromise ??= (async () => {const cleanup = await closeDevelopmentServerResources({+ appRoot: project.appRoot,authoredSourceWatcher: authoredSourceWatcherOnClose,devServer: devServerOnClose,developmentSandboxRunId,@@ -529,7 +529,6 @@ async function startNitroDevelopmentServer(throw cleanupError;}} finally {- clearInitializedDevelopmentSandboxBackendNames(developmentSandboxRunId);restoreWorkflowLocalQueueEnvironmentOnClose();restoreWorkflowTransportEnvironmentOnClose?.();restoreDevelopmentSandboxRunId(previousDevelopmentSandboxRunId);@@ -543,6 +542,7 @@ async function startNitroDevelopmentServer(};} catch (error) {const cleanup = await closeDevelopmentServerResources({+ appRoot: project.appRoot,authoredSourceWatcher,devServer,developmentSandboxRunId,@@ -566,7 +566,6 @@ async function startNitroDevelopmentServer(}restoreWorkflowLocalQueueEnvironment?.();restoreWorkflowTransportEnvironment?.();- clearInitializedDevelopmentSandboxBackendNames(developmentSandboxRunId);if (cleanup.listenerClosed) {await state.remove().catch(() => {});}