Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: required chats wait for a slow backend

ogsamrat16a394a0a609
diff --git a/apps/server/src/box/box-required-service.test.ts b/apps/server/src/box/box-required-service.test.ts
index 60da7f62..30fc6a10 100644
--- a/apps/server/src/box/box-required-service.test.ts
+++ b/apps/server/src/box/box-required-service.test.ts
@@ -4,7 +4,8 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { boxKey, setToolsRoot } from "./box.js";
-import { rememberServerDefault, resolveMode, serverRequired, type BoxRun } from "./launch.js";
+import { planBox, rememberServerDefault, REQUIRED_REDETECT_MS, resolveMode, serverRequired, type BoxRun } from "./launch.js";
+import type { RunCommand } from "./run.js";
import { DESKTOP_PROJECTS_FILE } from "./plugin-lib.js";
import { BoxRequiredError, BoxService, type BoxWorkspace } from "./service.js";
@@ -105,6 +106,30 @@ describe("required sandbox: the desktop's projects", () => {
expect(await boxes.planOutcome("ses_req_fail1", workspace)).toEqual({ native: true, source: "required", reason: "box_failed", required: true, fallback: "native_uncounted" });
});
+ test("planBox under required: a backend slow to answer (a VM booting) is waited for, not \"did not answer in time\"", async () => {
+ if (process.platform !== "linux") return;
+ const home = await temp("boxhome");
+ const root = await temp("project");
+ const bin = await temp("bin");
+ await mkdir(join(bin, `linux-${process.arch === "arm64" ? "arm64" : "amd64"}`), { recursive: true });
+ await writeFile(join(bin, `linux-${process.arch === "arm64" ? "arm64" : "amd64"}`, "boxsh"), "#!/bin/sh\n");
+ const dockerDir = await temp("dockerbin");
+ await writeFile(join(dockerDir, "docker"), "#!/bin/sh\nexit 0\n", { mode: 0o755 });
+ const env = { OMNIRUSH_BOX_HOME: home, OMNIRUSH_BOX_BIN_DIR: bin, PATH: dockerDir, OMNIRUSH_BOX_RUNTIME: join(dockerDir, "docker"), OMNIRUSH_BOX_GC: "off" };
+ const run: RunCommand = async (cmd, args) => {
+ if (/docker$/.test(String(cmd)) && args[0] === "info") {
+ await new Promise((resolve) => setTimeout(resolve, 300));
+ return { code: 0, stdout: JSON.stringify({ OSType: "linux", ServerVersion: "29.0.0", Architecture: "x86_64" }), stderr: "" };
+ }
+ return { code: 1, stdout: "", stderr: "Error: No such object" };
+ };
+ const slow = await planBox({ env, home, root, run, asked: "docker", probeMs: 100, platform: "linux" });
+ expect(slow.box ? slow.box.runtime : slow.runtime).toMatchObject({ ok: false, reason: "the sandbox backend did not answer in time" });
+ const waited = await planBox({ env, home: await temp("boxhome2"), root, run, asked: "docker", probeMs: 100, platform: "linux", required: true });
+ expect(waited.box !== null || waited.runtime?.ok === true).toBe(true);
+ expect(REQUIRED_REDETECT_MS).toBe(45_000);
+ });
+
test("a new chat goes in the box; a reopened native chat stays here without waiting (kept_old_chat)", async () => {
const stateDir = await temp("state");
const home = await temp("boxhome");
diff --git a/apps/server/src/box/launch.ts b/apps/server/src/box/launch.ts
index 7da34c65..9e8ba0bb 100644
--- a/apps/server/src/box/launch.ts
+++ b/apps/server/src/box/launch.ts
@@ -331,15 +331,20 @@ export type ReadyPlan = {
export type NativePlan = { box: null; notice: string | null; runtime: RuntimeResult | null; image?: BoxImage; downloading?: boolean };
export type BoxPlan = ReadyPlan | NativePlan;
+/** How long a plan under required waits for the backend to answer before it is "cannot run" (cold WSL / VM start). */
+export const REQUIRED_REDETECT_MS = 45_000;
+
/**
* Plan the box of the project at `root` (the project's box is on). Returns
* { box: null, notice } when its chats run on this machine for now (no
* runtime, no helpers, the image still downloading), else a ready plan.
*/
-export async function planBox({ env = process.env, home, root, log = () => {}, run = runCommand, probeMs = 4_000, shims = null, asked = null, allow = null, platform = process.platform, stateDir = null, preferShadows = false }: { env?: NodeJS.ProcessEnv; home: string; root: string; log?: BoxLog; run?: RunCommand; probeMs?: number; shims?: string | null; asked?: string | null; allow?: string[] | null; platform?: NodeJS.Platform; stateDir?: string | null; preferShadows?: boolean }): Promise<BoxPlan> {
+export async function planBox({ env = process.env, home, root, log = () => {}, run = runCommand, probeMs = 4_000, shims = null, asked = null, allow = null, platform = process.platform, stateDir = null, preferShadows = false, required = false }: { env?: NodeJS.ProcessEnv; home: string; root: string; log?: BoxLog; run?: RunCommand; probeMs?: number; shims?: string | null; asked?: string | null; allow?: string[] | null; platform?: NodeJS.Platform; stateDir?: string | null; preferShadows?: boolean; required?: boolean }): Promise<BoxPlan> {
const key = boxKey(root);
const bounded = <T>(promise: Promise<T>, ms: number, fallback: T): Promise<T> => Promise.race([promise, new Promise<T>((resolve) => setTimeout(() => resolve(fallback), ms))]);
- const runtime = await bounded<RuntimeResult>(detectRuntime({ boxHome: home, env, run, ...(asked ? { asked } : {}), allow, ...(preferShadows ? { preferShadows } : {}) }), probeMs, { ok: false, reason: "the sandbox backend did not answer in time" });
+ // Under required the chat waits for the box anyway: a backend slow to answer (a WSL VM booting) is not "cannot run".
+ const detectMs = required ? Math.max(probeMs, REQUIRED_REDETECT_MS) : probeMs;
+ const runtime = await bounded<RuntimeResult>(detectRuntime({ boxHome: home, env, run, ...(asked ? { asked } : {}), allow, ...(preferShadows ? { preferShadows } : {}) }), detectMs, { ok: false, reason: "the sandbox backend did not answer in time" });
if (!runtime.ok) {
log("info", "box unavailable", { reason: runtime.reason });
return { box: null, notice: `Sandbox off for now: ${runtime.reason}.`, runtime };
diff --git a/apps/server/src/box/service.ts b/apps/server/src/box/service.ts
index 589ca132..6b089c1a 100644
--- a/apps/server/src/box/service.ts
+++ b/apps/server/src/box/service.ts
@@ -432,7 +432,7 @@ export class BoxService {
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), platform: this.platform, stateDir: this.options.stateDir, ...this.detectOptions() });
+ const plan = await planBox({ env: this.env, home: this.home, root, log: this.log, shims: shimsDir(this.env), platform: this.platform, stateDir: this.options.stateDir, ...this.detectOptions(), required: this.required().required });
project.waitingImage = !plan.box && plan.downloading === true;
project.lastRuntime = plan.box ? null : plan.runtime;
project.pull = !plan.box && plan.downloading === true && plan.image && plan.runtime?.ok ? { runtime: plan.runtime, image: plan.image } : null;