Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: restart a sandbox whose folder is replaced while the app runs

ogsamratc606eefefc85
diff --git a/apps/server/src/box/box.test.ts b/apps/server/src/box/box.test.ts
index 47e63ef4..2c9b354f 100644
--- a/apps/server/src/box/box.test.ts
+++ b/apps/server/src/box/box.test.ts
@@ -427,6 +427,38 @@ describe("box service", () => {
expect(await boxes.listedProjects()).toEqual([]);
});
+ test("a running box whose folder was replaced (a new clone) is planned again at the next prompt", async () => {
+ const stateDir = await temp("state");
+ const home = await temp("boxhome");
+ const root = await temp("project");
+ const workspace: BoxWorkspace = { id: "ws_c", path: root, workspaceType: "local" };
+ let replaced = false;
+ const asked: string[] = [];
+ const boxes = new BoxService({
+ stateDir,
+ readSettings: async () => ({ mode: "on", offered: true }),
+ writeSettings: async () => {},
+ workspaces: () => [workspace],
+ pluginPath: () => "/plugins/omnirush-box.js",
+ env: { OMNIRUSH_BOX_HOME: home, PATH: "", OMNIRUSH_BOX_RUNTIME: "/nonexistent/docker" },
+ folderReplaced: async ({ croot }) => { asked.push(croot); return replaced; },
+ });
+ const box = new Box({ key: boxKey(root), name: "n", root, croot: root, runtime: { name: "docker", bin: "docker" }, home, runFolder: await temp("run"), run: async () => ({ code: 0, stdout: "", stderr: "" }), log: () => {}, platform: "linux", env: {} });
+ box.connect({ port: 1, token: "t", uid: 0, gid: 0, containerId: "c", imageId: "i" });
+ box.setState("ready", null);
+ const run: BoxRun = {
+ key: boxKey(root), container: "c", runtimeBin: "docker", runtimeArgs: [], backend: "docker", exec: null, root, native: false, box,
+ ready: async () => box, label: () => null, setTurn: () => {}, freeze: async () => null,
+ };
+ boxes["projects"].set(boxKey(root), { run, notice: null, planning: null, nextPlanAt: 0, waitingImage: false });
+ expect(await boxes.ensure(workspace)).toBe(run);
+ replaced = true;
+ // Planned again (no runtime here, so no box this time): the old run is dropped, never handed out again.
+ expect(await boxes.ensure(workspace)).toBeNull();
+ expect(boxes["projects"].get(boxKey(root))?.run).toBeNull();
+ expect(asked).toEqual([root, root]);
+ });
+
test("a remote workspace never gets a box", async () => {
const stateDir = await temp("state");
const workspace: BoxWorkspace = { id: "ws_r", path: "/remote", workspaceType: "remote" };
diff --git a/apps/server/src/box/service.ts b/apps/server/src/box/service.ts
index 4850e36c..7a9ebc97 100644
--- a/apps/server/src/box/service.ts
+++ b/apps/server/src/box/service.ts
@@ -24,9 +24,10 @@
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
import { join } from "node:path";
-import { boxHome, boxKey, boxSnapshotsDir, containerName, installShell, listBoxes, readRecord, resetBox, savedTurns, type BoxLabel, type BoxLog } from "./box.js";
+import { boxHome, boxKey, boxSnapshotsDir, containerName, installShell, listBoxes, mountReplaced, readRecord, resetBox, savedTurns, type BoxLabel, type BoxLog } from "./box.js";
import { LABELS, OPT_IN, backendsFor, driverFor } from "./backends/index.js";
import type { BoxExec } from "./backends/types.js";
+import type { BoxEndpoint } from "./client.js";
import { detectStack, imageFor } from "./images.js";
import { askedBackend, boxRun, planBox, pullLog, pullMarker, pulling, pullInBackground, resolveMode, saveBackend, serverBackends, serverDefault, shimsDir, type BoxRun, type ProjectBoxMode, type ServerBoxDefault } from "./launch.js";
import type { BoxInstallLogPaths } from "./install-log.js";
@@ -109,6 +110,8 @@ export type BoxServiceOptions = {
pluginPath: () => string;
log?: BoxLog;
env?: NodeJS.ProcessEnv;
+ /** Whether a running box's project folder was deleted and made again (box.ts mountReplaced; tests replace it). */
+ folderReplaced?: (box: { endpoint: BoxEndpoint; croot: string }) => Promise<boolean>;
};
/** `waitingImage`: planned while its image was downloading (planned again as soon as the image is here). */
@@ -139,7 +142,10 @@ export class BoxService {
private installLog: BoxInstallLogPaths | null = null;
private alternatives: { at: number; key: string; list: BackendAlternative[] } | null = null;
+ private readonly folderReplaced: NonNullable<BoxServiceOptions["folderReplaced"]>;
+
constructor(private readonly options: BoxServiceOptions) {
+ this.folderReplaced = options.folderReplaced ?? (({ endpoint, croot }) => mountReplaced({ endpoint }, { croot, readyMs: 1_000 }));
this.env = options.env ?? process.env;
this.home = boxHome(this.env);
this.log = options.log ?? (() => {});
@@ -261,7 +267,15 @@ export class BoxService {
await this.writeProjects();
return null;
}
- if (project.run && !project.run.native) return project.run;
+ if (project.run && !project.run.native) {
+ const box = project.run.box;
+ // The folder was deleted and made again (a new clone) while its box ran: the box still has the deleted
+ // one, where every command fails. Planned again, the box restarts on the new folder (openBox).
+ if (!box?.ready || !box.endpoint || !(await this.folderReplaced({ endpoint: box.endpoint, croot: box.croot }).catch(() => false))) return project.run;
+ this.log("info", "box folder replaced while it ran: starting it again", { key });
+ project.run = null;
+ project.nextPlanAt = 0;
+ }
if (Date.now() < project.nextPlanAt) return null;
const plan = await planBox({ env: this.env, home: this.home, root, log: this.log, shims: shimsDir(this.env), ...this.detectOptions() });
project.waitingImage = !plan.box && plan.downloading === true;