A desktop coding agent with free access to frontier models.
TypeScript
fix: windows sandbox setup resume and hints
ogsamratd2798e7b3b47
diff --git a/apps/server/src/box/backends/wsl.ts b/apps/server/src/box/backends/wsl.tsindex 970500e6..e1a00041 100644--- a/apps/server/src/box/backends/wsl.ts+++ b/apps/server/src/box/backends/wsl.ts@@ -104,6 +104,16 @@ export function parseWslList(text: unknown): WslRow[] {return rows;}+/**+ * `wsl --status` without its WSL 1 notes: Windows 11 with only Virtual+ * Machine Platform on (what `wsl --install --no-distribution` turns on) says+ * "WSL1 is not supported ... enable the "Windows Subsystem for Linux"+ * optional component to use WSL1", while WSL 2 works.+ */+export function withoutWsl1Notes(text: unknown): string {+ return String(text ?? "").split(/\r?\n/).filter((line) => !/\bWSL ?1\b/i.test(line)).join("\n");+}+const NOT_ENABLED = /not installed|is not enabled|optional component|virtual machine platform|HCS_E_HYPERV_NOT_INSTALLED|WSL_E_WSL_OPTIONAL_COMPONENT_REQUIRED|0x8007019e|0x80370102|wsl\.exe --install|enable the .*feature/i;const NO_DISTROS = /no installed distributions|WSL_E_DEFAULT_DISTRO_NOT_FOUND|has no installed distributions/i;@@ -130,7 +140,7 @@ export async function probeWslDistro({ env = process.env, platform = process.placonst bin = wslExe({ env, exists });if (!bin) return { ...base, ok: false, reason: ENABLE_HINT };const status = await wslCall(bin, ["--status"], { run, env, timeoutMs });- const said = `${status.stdout}\n${status.stderr}`;+ const said = withoutWsl1Notes(`${status.stdout}\n${status.stderr}`);if (NOT_ENABLED.test(said) || (status.code !== 0 && !NO_DISTROS.test(said))) {return { ...base, ok: false, bin, reason: NOT_ENABLED.test(said) || status.error === "ENOENT" ? ENABLE_HINT : `\`wsl --status\` failed: ${firstLine(said) || status.code}` };}diff --git a/apps/server/src/box/box-setup.test.ts b/apps/server/src/box/box-setup.test.tsindex 6834ec11..e6881c22 100644--- a/apps/server/src/box/box-setup.test.ts+++ b/apps/server/src/box/box-setup.test.ts@@ -1,10 +1,11 @@import { afterEach, describe, expect, test } from "bun:test";-import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";+import { mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import type { RunCommand } from "./run.js";-import { classifySetup, elevatedArgs, linuxAllow, linuxAllowScript, parseInstallLog, recentlyDeclined, resumeWinSetup, runOnceArgs, saveSetupState, setupDecision, setupState, windowsBuild, windowsInstall } from "./setup.js";+import { ENABLE_HINT, probeWslDistro, withoutWsl1Notes } from "./backends/wsl.js";+import { blockedWhy, classifySetup, elevatedArgs, installLogFile, linuxAllow, linuxAllowScript, parseInstallLog, recentlyDeclined, resumeWinSetup, runOnceArgs, saveSetupState, setupDecision, setupState, windowsBuild, windowsInstall } from "./setup.js";const dirs: string[] = [];afterEach(async () => {@@ -21,6 +22,11 @@ const checks = { build: 22631, hypervisor: false, firmware: true, model: "Dell Iconst OFF = "WSL 2 is not enabled on this Windows. One time, in an administrator terminal: `wsl --install --no-distribution`, then restart Windows";const ok: Awaited<ReturnType<RunCommand>> = { code: 0, stdout: "", stderr: "" };+// Windows 11 24H2 after `wsl --install --no-distribution` and the restart (seen on the CLI's VM rig, 2026-10-22).+const STATUS_WSL1_NOTE = "Default Version: 2\r\n\r\nWSL1 is not supported with your current machine configuration.\r\nPlease enable the \"Windows Subsystem for Linux\" optional component to use WSL1.\r\n";+// The same PC before the restart (and a PC that cannot start a VM after it).+const STATUS_NO_VM = `${STATUS_WSL1_NOTE}WSL2 is unable to start since virtualization is not enabled on this machine.\r\nPlease ensure the "Virtual Machine Platform" optional component is enabled and virtualization is turned on in your computer's firmware settings.\r\n`;+describe("sandbox setup: what is needed", () => {test("Linux: AppArmor is the one-click allow; a container without user namespaces cannot run; otherwise the usual next step", () => {expect(classifySetup({ runtime: fail("no backend", ["bubblewrap is blocked; one-time admin fix: Allow the sandbox (one password prompt), or `sudo omnirush sandbox enable` (OmniRush CLI)"]), platform: "linux", fallback: "native" }).need).toBe("linux_allow");@@ -117,6 +123,63 @@ describe("sandbox setup: what is needed", () => {expect(await resumeWinSetup({ home, run, bin: "wsl.exe" })).toEqual({ ok: false, state: "none" });});+ test("resume: a WSL 1 note alone is WSL 2 ready; no VM before the restart waits, after it is blocked with why", async () => {+ const reply = (stdout: string): RunCommand => async () => ({ code: 0, stdout, stderr: "" });+ const home = await temp("home");+ saveSetupState(home, "win", { state: "restart" }, { now: 1_000 });+ expect(await resumeWinSetup({ home, bin: "wsl.exe", bootedAt: 2_000, run: reply(STATUS_WSL1_NOTE) })).toEqual({ ok: true, state: "ready" });+ expect(setupState(home).win).toBeUndefined();+ saveSetupState(home, "win", { state: "restart" }, { now: 5_000 });+ expect((await resumeWinSetup({ home, bin: "wsl.exe", bootedAt: 2_000, now: 6_000, run: reply(STATUS_NO_VM) })).state).toBe("restart");+ expect(setupState(home).win?.state).toBe("restart");+ expect(await resumeWinSetup({ home, bin: "wsl.exe", bootedAt: 7_000, now: 8_000, checks: { model: "QEMU Standard PC (Q35 + ICH9, 2009)" }, run: reply(STATUS_NO_VM) })).toEqual({ ok: false, state: "blocked", why: "nested_vm" });+ expect(setupState(home).win?.why).toBe("nested_vm");+ saveSetupState(home, "win", { state: "restart" }, { now: 5_000 });+ expect((await resumeWinSetup({ home, bin: "wsl.exe", bootedAt: 7_000, checks: { model: "LENOVO 20XW" }, run: reply(STATUS_NO_VM) })).why).toBe("firmware_off");+ // Still off after the restart (the optional component): the marker stays.+ saveSetupState(home, "win", { state: "restart" }, { now: 5_000 });+ expect((await resumeWinSetup({ home, bin: "wsl.exe", bootedAt: 7_000, run: async () => ({ code: 1, stdout: "The Windows Subsystem for Linux optional component is not enabled.\n", stderr: "" }) })).state).toBe("restart");+ // A virtualization error code after the restart on a PC: firmware_off.+ expect(await resumeWinSetup({ home, bin: "wsl.exe", bootedAt: 7_000, checks: { model: "Dell Inc. XPS 13" }, run: async () => ({ code: 1, stdout: "Error code: Wsl/Service/CreateInstance/CreateVm/HCS/HCS_E_HYPERV_NOT_INSTALLED\n", stderr: "" }) })).toEqual({ ok: false, state: "blocked", why: "firmware_off" });+ });++ test("the WSL probe: a WSL 1 note in wsl --status is not \"WSL 2 is not enabled\"", async () => {+ expect(withoutWsl1Notes(STATUS_WSL1_NOTE).trim()).toBe("Default Version: 2");+ const run: RunCommand = async (_bin, args) => (args[0] === "--status" ? { code: 0, stdout: STATUS_WSL1_NOTE, stderr: "" } : ok);+ const probe = await probeWslDistro({ platform: "win32", env: { SystemRoot: "C:\\Windows" }, exists: () => true, run });+ expect(probe.ok ? null : probe.reason).not.toBe(ENABLE_HINT);+ const off = await probeWslDistro({ platform: "win32", env: { SystemRoot: "C:\\Windows" }, exists: () => true, run: async () => ({ code: 0, stdout: STATUS_NO_VM, stderr: "" }) });+ expect(off.ok ? null : off.reason).toBe(ENABLE_HINT);+ });++ test("windows install: the log is kept in the box home; a blocked install in a VM says nested_vm; a failed one names the log", async () => {+ const home = await temp("home");+ const tmpDir = await temp("tmp");+ const install = (log: string): RunCommand => async (_bin, args) => {+ const m = /-File "([^"]+)" "([^"]+)"/.exec((args.at(-1) ?? "").replace(/''/g, "'"));+ if (m?.[2]) await writeFile(m[2], log);+ return ok;+ };+ const vm = { ...checks, build: 26100, hypervisor: true, model: "QEMU Standard PC (Q35 + ICH9, 2009)" };+ expect(await windowsInstall({ home, run: install("Error code: Wsl/0x80370102\r\ndone\r\n"), exe: null, tmpDir, checks: vm })).toMatchObject({ state: "blocked", why: "nested_vm" });+ expect(await readFile(installLogFile(home), "utf8")).toContain("0x80370102");+ expect(installLogFile(home)).toBe(join(home, "setup-win.log"));+ expect(await readdir(tmpDir)).toEqual([]);+ expect(await windowsInstall({ home, run: install("Error code: Wsl/0x80370102\r\ndone\r\n"), exe: null, tmpDir, checks: { ...vm, model: "LENOVO 20XW" } })).toMatchObject({ state: "blocked", why: "firmware_off" });+ const failed = await windowsInstall({ home, run: install("Error: access denied\r\n"), exe: null, tmpDir, checks: vm });+ expect(failed).toMatchObject({ state: "failed" });+ expect(failed.message).toBe(`The WSL install did not finish: Error: access denied (log: ${installLogFile(home)})`);+ expect(await readFile(installLogFile(home), "utf8")).toContain("access denied");+ });++ test("a virtualization error from the probe: nested_vm in a VM guest, else firmware_off", () => {+ const vmError = fail("WSL 2 could not start: Wsl/Service/CreateInstance/CreateVm/HCS/HCS_E_HYPERV_NOT_INSTALLED");+ expect(blockedWhy({ model: "VMware, Inc. VMware7,1" })).toBe("nested_vm");+ expect(blockedWhy(null)).toBe("firmware_off");+ expect(classifySetup({ runtime: vmError, platform: "win32", checks: { ...checks, hypervisor: true, model: "QEMU Standard PC" }, fallback: "native" })).toMatchObject({ need: "cannot_run", why: "nested_vm" });+ expect(classifySetup({ runtime: vmError, platform: "win32", checks: { ...checks, hypervisor: true }, fallback: "native" })).toMatchObject({ need: "cannot_run", why: "firmware_off" });+ });+test("Linux allow: pkexec runs the enable step with the staged bubblewrap and its sha256; dismissed is declined", async () => {const home = await temp("home");const source = join(await temp("bin"), "bwrap");diff --git a/apps/server/src/box/setup.ts b/apps/server/src/box/setup.tsindex f1b0c1b9..7608b5c9 100644--- a/apps/server/src/box/setup.ts+++ b/apps/server/src/box/setup.ts@@ -33,7 +33,7 @@ import path from "node:path";import { GOARCH, helperPath } from "./common.js";import { APPARMOR_PROFILE, ENABLED_BWRAP, apparmorProfile } from "./backends/bwrap.js";-import { wslCall, wslExe } from "./backends/wsl.js";+import { withoutWsl1Notes, wslCall, wslExe } from "./backends/wsl.js";import { REQUIRED_RULES, whyText, type CannotRunWhy, type RequiredFallback } from "./required.js";import { runCommand, type RunCommand, type RuntimeResult } from "./runtime.js";@@ -41,6 +41,18 @@ const WIN = REQUIRED_RULES.setup.win32;const VIRTUALIZATION = new RegExp(WIN.virtualization_errors, "i");const POLICY = /group policy|blocked by (?:your|the) (?:administrator|organi[sz]ation)|0x800f0950|0x800f0922|0x80070422/i;const VM_MODEL = /virtual machine|vmware|virtualbox|kvm|qemu|xen|parallels|hyper-v|bochs/i;+// `wsl --status` on a PC whose WSL 2 cannot start a VM (after the restart).+const NO_VM_START = /unable to start since virtualization is not enabled|virtualization is not enabled on this machine/i;++/** The install log kept for support (<box home>/setup-win.log, the last run's). */+export function installLogFile(home: string): string {+ return path.join(home, "setup-win.log");+}++/** Why a PC that cannot start WSL 2's VM can't: inside a VM (its model says so), else the firmware switch. */+export function blockedWhy(checks: Partial<WindowsChecks> | null | undefined): "nested_vm" | "firmware_off" {+ return VM_MODEL.test(String(checks?.model ?? "")) ? "nested_vm" : "firmware_off";+}// --- setup.json (CLI: setup-state.js) --------------------------------------------------@@ -132,7 +144,7 @@ export function classifySetup({ runtime, platform, checks = null, state = {}, fa}if (platform === "win32") {if (entry?.state === "blocked" && entry.why) return cannot(entry.why);- if (said.some((reason) => VIRTUALIZATION.test(reason))) return cannot("firmware_off");+ if (said.some((reason) => VIRTUALIZATION.test(reason))) return cannot(blockedWhy(checks));const decision = checks ? setupDecision(checks) : { ok: true as const };if (!decision.ok) return cannot(decision.why);if (said.some((reason) => /wsl --update/.test(reason))) return { ...base, need: "win_update" };@@ -213,7 +225,7 @@ export function setupDecision(checks: Partial<WindowsChecks> | null, { minBuildif (Number.isFinite(checks?.build) && Number(checks?.build) < minBuild) return { ok: false, why: "old_windows" };if (checks?.s_mode === true) return { ok: false, why: "s_mode" };if (checks?.hypervisor === true) return { ok: true };- if (checks?.firmware === false) return { ok: false, why: VM_MODEL.test(String(checks.model ?? "")) ? "nested_vm" : "firmware_off" };+ if (checks?.firmware === false) return { ok: false, why: blockedWhy(checks) };return { ok: true };}@@ -266,6 +278,7 @@ export async function windowsInstall({ home, env = process.env, run = runCommandtry {res = await run(powershellExe(env), elevatedArgs(ps1, logFile), { env, timeoutMs: 30 * 60_000 });try { text = readFileSync(logFile, "utf8"); } catch { text = ""; }+ try { if (text.trim()) writeFileSync(installLogFile(home), text, "utf8"); } catch { /* best effort */ }} finally {rmSync(ps1, { force: true });rmSync(logFile, { force: true });@@ -273,8 +286,8 @@ export async function windowsInstall({ home, env = process.env, run = runCommand// No log at all and a failed Start-Process: the admin prompt was answered "No" (or never shown).if (!text.trim() && res?.code !== 0) return saved({ state: "declined", message: "The administrator prompt was cancelled; nothing changed." });const parsed = parseInstallLog(text);- if (parsed.result === "blocked") return saved({ state: "blocked", why: parsed.why });- if (parsed.result === "failed") return saved({ state: "failed", message: `The WSL install did not finish: ${parsed.line}` });+ if (parsed.result === "blocked") return saved({ state: "blocked", why: parsed.why === "firmware_off" ? blockedWhy(checks) : parsed.why });+ if (parsed.result === "failed") return saved({ state: "failed", message: `The WSL install did not finish: ${parsed.line} (log: ${installLogFile(home)})` });if (exe) await run("reg.exe", runOnceArgs(exe), { env, timeoutMs: 15_000 });return saved({ state: "restart", message: "WSL 2 is installed. Restart Windows to finish." });}@@ -288,25 +301,37 @@ export function windowsRestart({ run = runCommand }: { run?: RunCommand } = {}):* The start after the restart (the resume marker): `wsl --update` when WSL* asks for it, then the marker goes (the box start imports the distro and* downloads the image). WSL still off: the marker stays (no restart yet);- * virtualization errors: the machine is marked as unable.+ * WSL 2 unable to start a VM: before the restart (Windows booted before the+ * marker) that is the same text, so it waits; after it the machine is marked+ * as unable, with why (nested_vm when this PC is a VM, else firmware_off).+ * WSL 1 notes in `wsl --status` are not "WSL 2 off".*/-export async function resumeWinSetup({ home, env = process.env, run = runCommand, log = () => {}, now = Date.now(), bin: given = null }: { home: string; env?: NodeJS.ProcessEnv; run?: RunCommand; log?: (level: "info" | "warn", message: string) => void; now?: number; bin?: string | null }): Promise<{ ok: boolean; state: "none" | "restart" | "blocked" | "ready" }> {- if (setupState(home).win?.state !== "restart") return { ok: false, state: "none" };+export async function resumeWinSetup({ home, env = process.env, run = runCommand, log = () => {}, now = Date.now(), bin: given = null, checks = null, bootedAt = now - os.uptime() * 1000 }: { home: string; env?: NodeJS.ProcessEnv; run?: RunCommand; log?: (level: "info" | "warn", message: string) => void; now?: number; bin?: string | null; checks?: Partial<WindowsChecks> | null; bootedAt?: number }): Promise<{ ok: boolean; state: "none" | "restart" | "blocked" | "ready"; why?: CannotRunWhy }> {+ const marker = setupState(home).win;+ if (marker?.state !== "restart") return { ok: false, state: "none" };+ // Before the restart WSL says the same as on a PC that cannot run a VM: only after it does that mean "cannot".+ const restarted = !Number.isFinite(Number(marker.at)) || bootedAt > Number(marker.at);const bin = given ?? wslExe({ env });if (!bin) return { ok: false, state: "restart" };let status = await wslCall(bin, ["--status"], { run, env, timeoutMs: 30_000 });- let said = `${status.stdout}\n${status.stderr}`;+ let said = withoutWsl1Notes(`${status.stdout}\n${status.stderr}`);if (/wsl --update|requires an update|kernel (?:file|component)|update the wsl/i.test(said)) {log("info", "sandbox setup resume: wsl --update");await wslCall(bin, ["--update", "--web-download"], { run, env, timeoutMs: 10 * 60_000 });status = await wslCall(bin, ["--status"], { run, env, timeoutMs: 30_000 });- said = `${status.stdout}\n${status.stderr}`;+ said = withoutWsl1Notes(`${status.stdout}\n${status.stderr}`);+ }+ const noVm = VIRTUALIZATION.test(said) || NO_VM_START.test(said);+ if (restarted && noVm) {+ const why = blockedWhy(checks ?? await windowsChecks({ run, env }).catch(() => null));+ saveSetupState(home, "win", { state: "blocked", why }, { now });+ log("info", `sandbox setup resume: WSL 2 cannot start a VM here (${why})`);+ return { ok: false, state: "blocked", why };}- if (VIRTUALIZATION.test(said)) {- saveSetupState(home, "win", { state: "blocked", why: "firmware_off" }, { now });- return { ok: false, state: "blocked" };+ if ((!restarted && noVm) || /not installed|is not enabled|optional component|WSL_E_WSL_OPTIONAL_COMPONENT_REQUIRED/i.test(said)) {+ log("info", "sandbox setup resume: WSL 2 not on yet (restart pending)");+ return { ok: false, state: "restart" };}- if (/not installed|is not enabled|optional component|WSL_E_WSL_OPTIONAL_COMPONENT_REQUIRED/i.test(said)) return { ok: false, state: "restart" };saveSetupState(home, "win", null, { now });await run("reg.exe", ["delete", "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce", "/v", WIN.resume_run_once, "/f"], { env, timeoutMs: 15_000 });log("info", "sandbox setup resumed: WSL 2 ready");