Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat: macOS sandbox VM backend

ogsamrat39159f218d96
diff --git a/apps/server/src/box/backends/index.ts b/apps/server/src/box/backends/index.ts
index 2eeecf51..b2de74e5 100644
--- a/apps/server/src/box/backends/index.ts
+++ b/apps/server/src/box/backends/index.ts
@@ -10,8 +10,9 @@
// `__sandbox__` format, so the converter and the rebuild do not change.
//
// Linux: bwrap (no daemon, no root) > rootless Podman > Docker
-// macOS: OrbStack > colima > Docker Desktop > Rancher > Podman machine
-// (Apple container is opt-in only: never picked on its own, never started unasked)
+// macOS: OmniRush's own Lima VM (macOS 13+, set up by itself) > OrbStack > colima
+// > Docker Desktop > Rancher > Podman machine, the runtimes only when already
+// running (Apple container is opt-in only: never picked on its own, never started unasked)
// Windows: WSL containers (wslc, WSL 3.0.1+) > OmniRush's WSL distro > Docker Desktop > Podman machine
// > Docker in the default WSL distro
//
@@ -27,6 +28,7 @@ import { CACHE_FAIL_MS, CACHE_OK_MS, candidates, isWsl, probe as probeContainer,
import { appleDriver, probeApple } from "./apple.js";
import { bwrapDriver, probeBwrap } from "./bwrap.js";
import { containerDriver } from "./container.js";
+import { limaDriver, probeLima } from "./lima.js";
import type { BoxDriver } from "./types.js";
import { probeWslDistro, wslDriver } from "./wsl.js";
import { probeWslc, wslcDriver } from "./wslc.js";
@@ -35,7 +37,7 @@ type Env = NodeJS.ProcessEnv;
export const ORDER: Record<"linux" | "darwin" | "win32", string[]> = {
linux: ["bwrap", "podman", "docker"],
- darwin: ["orbstack", "colima", "docker-desktop", "rancher", "podman", "docker"],
+ darwin: ["lima", "orbstack", "colima", "docker-desktop", "rancher", "podman", "docker"],
win32: ["wslc", "wsl", "docker-desktop", "podman", "docker", "wsl-engine"],
};
@@ -49,10 +51,10 @@ export const ORDER: Record<"linux" | "darwin" | "win32", string[]> = {
export const OPT_IN: Partial<Record<NodeJS.Platform, string[]>> = { darwin: ["apple"] };
/** What the backend setting takes (and what uploads record as sandbox.backend). */
-export const BACKENDS = ["bwrap", "apple", "wslc", "wsl", "orbstack", "colima", "docker-desktop", "rancher", "docker", "podman", "wsl-engine"] as const;
+export const BACKENDS = ["bwrap", "lima", "apple", "wslc", "wsl", "orbstack", "colima", "docker-desktop", "rancher", "docker", "podman", "wsl-engine"] as const;
export type BackendId = (typeof BACKENDS)[number];
export const LABELS: Record<string, string> = {
- bwrap: "bubblewrap (no daemon)", apple: "Apple container", wslc: "WSL containers (wslc)", wsl: "OmniRush WSL distro", orbstack: "OrbStack", colima: "colima",
+ bwrap: "bubblewrap (no daemon)", lima: "OmniRush VM (Lima)", apple: "Apple container", wslc: "WSL containers (wslc)", wsl: "OmniRush WSL distro", orbstack: "OrbStack", colima: "colima",
"docker-desktop": "Docker Desktop", rancher: "Rancher Desktop", docker: "Docker", podman: "Podman", "wsl-engine": "Docker/Podman in WSL",
};
@@ -66,6 +68,7 @@ export function driverFor(runtime: { backend?: string } | null | undefined): Box
switch (runtime?.backend) {
case "bwrap": return bwrapDriver;
case "apple": return appleDriver;
+ case "lima": return limaDriver;
case "wsl": return wslDriver;
case "wslc": return wslcDriver;
default: return containerDriver;
@@ -91,8 +94,9 @@ export function runtimeBackend(value: unknown, platform: NodeJS.Platform = proce
if (base === "wslc") return "wslc";
if (base === "container" && platform !== "win32") return "apple";
if (base === "boxrun" || base === "bwrap") return "bwrap";
+ if (base === "limactl") return "lima";
const lower = String(value || "").toLowerCase();
- if (["bwrap", "apple", "wslc", "wsl"].includes(lower)) return lower;
+ if (["bwrap", "lima", "apple", "wslc", "wsl"].includes(lower)) return lower;
return null;
}
@@ -104,7 +108,7 @@ function isBackendId(value: string): value is BackendId {
export function normalizeBackend(value: unknown): BackendId | "auto" | "native" | null {
const v = String(value ?? "").trim().toLowerCase();
if (!v || v === "auto" || v === "default") return "auto";
- const alias: Record<string, string> = { bubblewrap: "bwrap", "apple-container": "apple", container: "apple", "wsl-containers": "wslc", "wsl-container": "wslc", desktop: "docker-desktop", "docker desktop": "docker-desktop", "podman-machine": "podman", off: "native", none: "native", host: "native" };
+ const alias: Record<string, string> = { bubblewrap: "bwrap", "omnirush-vm": "lima", vm: "lima", "apple-container": "apple", container: "apple", "wsl-containers": "wslc", "wsl-container": "wslc", desktop: "docker-desktop", "docker desktop": "docker-desktop", "podman-machine": "podman", off: "native", none: "native", host: "native" };
const id = alias[v] ?? v;
if (id === "native") return "native";
return isBackendId(id) ? id : null;
@@ -193,7 +197,7 @@ function writeCache(boxHome: string, key: string, result: RuntimeResult, now: nu
/** One line when nothing works, with the free option to install. */
export function noneReason(platform: NodeJS.Platform): string {
if (platform === "linux") return "no sandbox backend works here (install bubblewrap, Podman or Docker)";
- if (platform === "darwin") return "no container runtime found (free: `brew install colima docker && colima start`; also OrbStack, Docker Desktop, Podman; Apple container only when chosen in Settings)";
+ if (platform === "darwin") return "the OmniRush VM cannot run here and no container runtime found (free: `brew install colima docker && colima start`; also OrbStack, Docker Desktop, Podman; Apple container only when chosen in Settings)";
return "no sandbox backend found (turn on WSL 2: `wsl --install --no-distribution` as administrator, then `wsl --update` for WSL containers; or Docker Desktop, Podman)";
}
@@ -228,9 +232,9 @@ export async function detectBackend({ boxHome, env = process.env, platform = pro
if (wanted === "native") return { ok: false, reason: "the sandbox backend is set to native (this machine)", tried: [], native: true };
let order: string[] = backendsFor(platform);
// OMNIRUSH_BOX_RUNTIME (docker, podman or a path) keeps meaning "this container CLI".
- if (legacyRuntime) order = order.filter((id) => !["bwrap", "apple", "wslc", "wsl", "wsl-engine"].includes(id));
+ if (legacyRuntime) order = order.filter((id) => !["bwrap", "lima", "apple", "wslc", "wsl", "wsl-engine"].includes(id));
// Under the required sandbox on macOS/Windows: the backends whose boxes get their own build
- // folders (driver.shadows: boxrun as root, the WSL distro) before the others.
+ // folders (driver.shadows: boxrun as root, the WSL distro, the Lima VM) before the others.
if (preferShadows && wanted === "auto") order = [...order.filter((id) => driverFor({ backend: id }).shadows), ...order.filter((id) => !driverFor({ backend: id }).shadows)];
let why = `the first that works here, in order: ${order.join(" > ")}`;
if (wanted !== "auto") {
@@ -251,6 +255,7 @@ export async function detectBackend({ boxHome, env = process.env, platform = pro
for (const id of order) {
let found: Probed[];
if (id === "bwrap") found = [{ backend: "bwrap", ...(await probeBwrap({ env, boxHome: boxHome ?? path.join(home, ".omnirush", "box"), run, exists, platform, arch, ...limit })) }];
+ else if (id === "lima") found = [{ backend: "lima", ...(await probeLima({ env, platform, arch, release, run, exists, home: boxHome ?? path.join(home, ".omnirush", "box"), explicit: wanted === "lima", ...limit })) }];
else if (id === "apple") found = [{ backend: "apple", ...(await probeApple({ env, platform, arch, release, run, exists, ...limit })) }];
else if (id === "wsl") found = [{ backend: "wsl", ...(await probeWslDistro({ env, platform, run, exists, arch, ...limit })) }];
else if (id === "wslc") found = [{ backend: "wslc", ...(await probeWslc({ env, platform, run, exists, arch, explicit: wanted === "wslc", ...limit })) }];
diff --git a/apps/server/src/box/backends/lima-idle.ts b/apps/server/src/box/backends/lima-idle.ts
new file mode 100644
index 00000000..9e6d2a09
--- /dev/null
+++ b/apps/server/src/box/backends/lima-idle.ts
@@ -0,0 +1,36 @@
+// Ported from the CLI (omnirush-cli src/box/backends/lima-idle.js); keep the two
+// in step. The sandbox VM's idle watcher (lima.ts keepIdleWatch starts it,
+// detached; Desktop: with the app's own binary as node, ELECTRON_RUN_AS_NODE=1):
+// lima-idle.js <box home> <limactl> <vm name>
+// Once a minute: while a live lease is there (a running box of any app, the
+// CLI's too) the VM is busy; after setup.darwin.vm.idle_stop_min with none, it
+// stops the VM under the owner lock and ends. It ends too when the VM is
+// stopped by someone else, or another watcher took over (idle.json names
+// another pid: the CLI's watcher and this one never run side by side).
+import { readFileSync } from "node:fs";
+import path from "node:path";
+
+import { idleStep, idleStopMinutes, limaDir, stopIfIdle } from "./lima.js";
+
+const [home = "", bin = "", instance = ""] = process.argv.slice(2);
+const runtime = { home, bin, instance };
+const idleMs = idleStopMinutes() * 60_000;
+const tickMs = Math.max(1_000, Math.min(60_000, Math.floor(idleMs / 5) || 60_000));
+const mine = (): boolean => {
+ try { return Number(JSON.parse(readFileSync(path.join(limaDir(home), "idle.json"), "utf8")).pid) === process.pid; } catch { return false; }
+};
+
+let busyAt = Date.now();
+if (!home || !bin || !instance || idleMs <= 0) process.exit(0);
+for (;;) {
+ await new Promise((resolve) => setTimeout(resolve, tickMs));
+ if (!mine()) process.exit(0);
+ const step = await idleStep(runtime, { busyAt, idleMs }).catch(() => ({ busyAt, stop: undefined, done: undefined }));
+ if (step.done) process.exit(0);
+ busyAt = step.busyAt;
+ if (step.stop) {
+ const done = await stopIfIdle(runtime).catch(() => ({ stopped: false, reason: undefined }));
+ if (done.stopped) process.exit(0);
+ if (done.reason === "busy") busyAt = Date.now();
+ }
+}
diff --git a/apps/server/src/box/backends/lima.ts b/apps/server/src/box/backends/lima.ts
new file mode 100644
index 00000000..f1cffae1
--- /dev/null
+++ b/apps/server/src/box/backends/lima.ts
@@ -0,0 +1,1188 @@
+// Ported from the CLI (omnirush-cli src/box/backends/lima.js, feat/box-required);
+// keep the two in step: the CLI and the desktop app SHARE one VM, so the VM's
+// name, LIMA_HOME, the owner lock, the leases, the idle watcher's file, the
+// contract in the VM and the helpers' folders are the same files in the same
+// formats. Desktop differences, each marked "Desktop:": types; limactl bundled
+// in the app is <sidecars>/lima/bin/limactl (signed with the virtualization
+// entitlement); the download (Lima, the VM image, the box image) runs in the
+// app's own process (pullCommand null) and says how far it is in setup lines
+// ({"setup":{"step","bytes","total"}}) next to boxrun's own progress lines
+// (pull-progress.ts reads both); the idle watcher is lima-idle.ts run with the
+// app's own binary (ELECTRON_RUN_AS_NODE=1); the contract names client "desktop".
+//
+// macOS: OmniRush's own Linux VM as the box backend (backend id `lima`), so no
+// Docker, OrbStack or colima is needed. One VM per user ("omnirush-box"),
+// run by a pinned Lima (limactl, Apple's Virtualization.framework through the
+// `vz` driver, no admin). Its disk is a pinned Debian image (box/vm), its
+// LIMA_HOME is <box home>/vm (short: Lima keeps sockets there), and the user's
+// home, /Volumes and /private/tmp are shared into it at the same paths
+// (virtiofs, writable). Each project's box runs inside it exactly like the WSL
+// distro's: boxrun as root (unshare + overlay) from the same pinned images, so
+// a Mac box is the same environment as a Linux or Windows one. Boxes bind
+// only their project.
+//
+// boxd listens on the VM's loopback at a port chosen here; Lima's host agent
+// forwards guest loopback ports to this Mac's 127.0.0.1 (within ~3 s).
+//
+// One VM for the CLI and the desktop app:
+// lima/vm.lock the owner lock for create/start/stop/edit ({pid, nonce, at}; stale when the pid is gone)
+// lima/leases/<key>-<pid>-<n>.json one per running box of a live process
+// lima/idle.json the idle watcher (lima-idle.ts / the CLI's lima-idle.js): stops the VM after
+// setup.darwin.vm.idle_stop_min with no live lease
+// /opt/omnirush/vm.json (in the VM) the contract the VM was made with: a newer
+// client never replaces the disk under an older one's box
+// /opt/omnirush/h/<stamp> (in the VM) each install's boxrun and boxd
+
+import { spawn, type ChildProcess } from "node:child_process";
+import { createHash, randomUUID } from "node:crypto";
+import { closeSync, copyFileSync, createReadStream, existsSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+import serverPackage from "../../../package.json" with { type: "json" };
+import { ping } from "../client.js";
+import { GOARCH, containerName, firstLine, freePort, helperPath, newToken, readJson, toolsRoot, writeJson, type BoxLog, type BoxRuntime } from "../common.js";
+import { REQUIRED_RULES } from "../required.js";
+import { YOUNG_IMAGE_MS, removableImage, removeTree, staleEntries } from "../retention.mjs";
+import { runCommand, type RunCommand, type RunOptions, type RunResult } from "../run.js";
+import type { RuntimeResult } from "../runtime.js";
+import { macosMajor } from "./apple.js";
+import { IDLE_MINUTES, newMeta, upArgs, type BoxMeta } from "./bwrap.js";
+import { boxshExec, num, parseOut, str } from "./shared.js";
+import type { BoxDriver, ListedBox, OpenContext, Opened, PresentImage, Removed, SpawnLike } from "./types.js";
+import { download, type DownloadOptions } from "./wsl.js";
+
+type Env = NodeJS.ProcessEnv;
+type OnLine = RunOptions["onLine"];
+
+const HERE = path.dirname(fileURLToPath(import.meta.url));
+export const STORE = "/var/lib/omnirush/store";
+export const BOXES = "/var/lib/omnirush/boxes";
+export const OPT = "/opt/omnirush";
+/** Where a box's own build folders (shadows) live in the VM: one folder per box, on the VM's disk. */
+export const SHADOW_STORES = "/var/lib/omnirush/shadows";
+/** The VM layout this client makes and understands (/opt/omnirush/vm.json `contract`). */
+export const VM_CONTRACT = 1;
+/** Lima's own name for an architecture. */
+export const LIMA_ARCH: Record<string, string> = { arm64: "aarch64", amd64: "x86_64", x64: "x86_64" };
+/** The folders of this Mac the VM shares (same path, writable), besides a project outside them. */
+export const BASE_MOUNTS = ["/Volumes", "/private/tmp"];
+/** A VM that failed to start is not picked again for this long (a running OrbStack/Docker is used meanwhile). */
+export const FAILED_MS = 60 * 60_000;
+const GIB = 1024 ** 3;
+
+type Asset = { url: string | null; sha256: string | null };
+export type DarwinSetup = {
+ backend?: string;
+ min_macos?: string;
+ lima?: { version?: string; assets?: Record<string, Asset | undefined> };
+ vm?: {
+ name?: string; cpus_max?: number; memory_min_gib?: number; memory_fraction?: number; disk_gib?: number; idle_stop_min?: number; contract?: number;
+ image?: {
+ custom?: { version?: string | null; arm64?: Asset; x64?: Asset };
+ fallback?: { version?: string | null; arm64?: { url: string | null; sha512: string | null }; x64?: { url: string | null; sha512: string | null } };
+ };
+ };
+};
+
+/** required.json's setup.darwin (lima pins, vm resources and image pins). */
+export function loadDarwinSetup(): DarwinSetup {
+ return REQUIRED_RULES.setup.darwin as DarwinSetup;
+}
+
+export function vmName(setup: DarwinSetup = loadDarwinSetup(), env: Env = process.env): string {
+ return String(env.OMNIRUSH_BOX_LIMA_VM || "").trim() || setup.vm?.name || "omnirush-box";
+}
+
+// --- where things live ---------------------------------------------------------------
+
+export const limaDir = (home: string): string => path.join(home, "lima");
+/** macOS' limit for a unix socket path, and the longest one Lima makes in an instance folder. */
+const SUN_PATH_MAX = 104;
+const sockPath = (lh: string, instance: string) => path.join(lh, instance, "ssh.sock.1234567890123456");
+/**
+ * LIMA_HOME: OMNIRUSH_BOX_LIMA_HOME, else <box home>/vm, else (a long box home
+ * or user name: Lima keeps unix sockets in it and macOS allows 104 bytes)
+ * ~/.omnirush-vm. The CLI and the desktop app work it out the same way, so
+ * they find the same VM.
+ */
+export function limaHome(home: string, env: Env = process.env, userHome = os.homedir(), instance = vmName(loadDarwinSetup(), env)): string {
+ const asked = String(env.OMNIRUSH_BOX_LIMA_HOME || "").trim();
+ if (asked) return asked;
+ const own = path.join(home, "vm");
+ if (Buffer.byteLength(sockPath(own, instance)) < SUN_PATH_MAX) return own;
+ return path.join(userHome, ".omnirush-vm");
+}
+/** Whether Lima can keep its sockets under this LIMA_HOME. */
+export function limaHomeFits(lh: string, instance = vmName()): boolean {
+ return Buffer.byteLength(sockPath(lh, instance)) < SUN_PATH_MAX;
+}
+const metaFile = (home: string, key: string) => path.join(limaDir(home), "boxes", `${key}.json`);
+const leaseDir = (home: string) => path.join(limaDir(home), "leases");
+const lockFile = (home: string) => path.join(limaDir(home), "vm.lock");
+const idleFile = (home: string) => path.join(limaDir(home), "idle.json");
+const failedFile = (home: string) => path.join(limaDir(home), "failed.json");
+const presentFile = (home: string) => path.join(limaDir(home), "images.json");
+const pendingFile = (home: string) => path.join(limaDir(home), "pending-rm.json");
+const boxPath = (key: string) => `${BOXES}/${key}`;
+
+/** The environment every limactl call gets: its home, never the user's LIMA_INSTANCE. */
+export function limaEnv(home: string, env: Env = process.env): Env {
+ const { LIMA_INSTANCE: _instance, LIMA_SHELL: _shell, LIMA_WORKDIR: _workdir, ...rest } = env;
+ return { ...rest, LIMA_HOME: limaHome(home, env), PATH: rest.PATH || "/usr/bin:/bin:/usr/sbin:/sbin" };
+}
+
+/** What the VM's calls need of a runtime (the probe's answer, or the parts of one a caller has). */
+export type LimaRuntime = Pick<BoxRuntime, "arch" | "installed" | "limactlSource" | "helpers"> & { bin: string; home: string; instance: string };
+
+/**
+ * limactl, best first: OMNIRUSH_BOX_LIMACTL; the one this app ships
+ * (<sidecars>/lima/bin/limactl, signed with the virtualization entitlement);
+ * the pinned release downloaded earlier (<box home>/lima/dist/<version>/bin/limactl,
+ * the CLI's download too). { bin, source }, bin null: not here yet.
+ */
+export function findLimactl({ env = process.env, home, exists = existsSync, setup = loadDarwinSetup(), tools = toolsRoot() }: { env?: Env; home?: string; exists?: (file: string) => boolean; setup?: DarwinSetup; tools?: string | null } = {}): { bin: string | null; source: "env" | "bundled" | "download" } {
+ const asked = String(env.OMNIRUSH_BOX_LIMACTL || "").trim();
+ if (asked) return { bin: exists(asked) ? asked : null, source: "env" };
+ if (tools) {
+ const bundled = path.join(tools, "lima", "bin", "limactl");
+ if (exists(bundled)) return { bin: bundled, source: "bundled" };
+ }
+ const version = setup.lima?.version;
+ if (home && version) {
+ const cached = path.join(limaDir(home), "dist", version, "bin", "limactl");
+ if (exists(cached)) return { bin: cached, source: "download" };
+ }
+ return { bin: null, source: "download" };
+}
+
+/** The pinned limactl release asset for this Mac ({ url, sha256 }) or null. */
+export function limaAsset(setup: DarwinSetup = loadDarwinSetup(), arch: string = process.arch): { url: string; sha256: string } | null {
+ const asset = setup.lima?.assets?.[arch === "arm64" ? "arm64" : "x64"];
+ return asset?.url && /^[0-9a-f]{64}$/.test(String(asset.sha256 || "")) ? { url: asset.url, sha256: String(asset.sha256) } : null;
+}
+
+export type VmImage = { url: string | null; file: string | null; algo: "sha256" | "sha512" | null; digest: string | null; kind: "local" | "custom" | "debian"; version: string | null };
+
+/**
+ * The VM disk image to use: OMNIRUSH_BOX_LIMA_IMAGE (a local qcow2/raw file),
+ * else OmniRush's own image when published, else the pinned official Debian
+ * one. Null: none for this Mac.
+ */
+export function vmImage({ env = process.env, setup = loadDarwinSetup(), arch = process.arch, home }: { env?: Env; setup?: DarwinSetup; arch?: string; home?: string } = {}): VmImage | null {
+ const asked = String(env.OMNIRUSH_BOX_LIMA_IMAGE || "").trim();
+ if (asked) return { url: null, file: asked, algo: null, digest: null, kind: "local", version: path.basename(asked) };
+ const a = arch === "arm64" ? "arm64" : "x64";
+ const image = setup.vm?.image ?? {};
+ const custom = image.custom?.[a];
+ const fallback = image.fallback?.[a];
+ const pick: Omit<VmImage, "file"> | null = custom?.url && /^[0-9a-f]{64}$/.test(String(custom.sha256 || ""))
+ ? { url: custom.url, algo: "sha256", digest: String(custom.sha256), kind: "custom", version: image.custom?.version ?? null }
+ : fallback?.url && /^[0-9a-f]{128}$/.test(String(fallback.sha512 || ""))
+ ? { url: fallback.url, algo: "sha512", digest: String(fallback.sha512), kind: "debian", version: image.fallback?.version ?? null }
+ : null;
+ if (!pick?.digest || !pick.url) return null;
+ const ext = /\.raw(\.|$)/.test(pick.url) ? "raw" : "qcow2";
+ return { ...pick, file: home ? path.join(limaDir(home), "images", `${pick.digest.slice(0, 32)}.${ext}`) : null };
+}
+
+/** A file's hex digest (sha256 or sha512). */
+export function hashFile(file: string, algo: "sha256" | "sha512" = "sha256"): Promise<string> {
+ return new Promise((resolve, reject) => {
+ const hash = createHash(algo);
+ createReadStream(file).on("error", reject).on("data", (chunk) => hash.update(chunk)).on("end", () => resolve(hash.digest("hex")));
+ });
+}
+
+export type FetchFile = (url: string, dest: string, sha256: string | null, options?: DownloadOptions) => Promise<string>;
+
+/** Download `url` to `dest` and check it (any algorithm); nothing is left at `dest` on a mismatch. */
+export async function fetchChecked(url: string, dest: string, { algo = "sha256", digest, fetchFile = download, hash = hashFile, stallMs = 90_000, tries = 3, onProgress }: { algo?: "sha256" | "sha512"; digest: string; fetchFile?: FetchFile; hash?: typeof hashFile; stallMs?: number; tries?: number; onProgress?: DownloadOptions["onProgress"] }): Promise<string> {
+ if (existsSync(dest) && (await hash(dest, algo)) === digest) return dest;
+ const part = `${dest}.dl`;
+ mkdirSync(path.dirname(dest), { recursive: true });
+ // A stalled or dropped connection is tried again (three tries); a wrong file is not.
+ for (let attempt = 1; ; attempt += 1) {
+ try {
+ await fetchFile(url, part, algo === "sha256" ? digest : null, { stallMs, ...(onProgress ? { onProgress } : {}) });
+ break;
+ } catch (error) {
+ rmSync(part, { force: true });
+ if (attempt >= tries || /mismatch|https only|HTTP 4\d\d/.test(String(error instanceof Error ? error.message : error))) throw error;
+ }
+ }
+ const got = await hash(part, algo);
+ if (got !== digest) { rmSync(part, { force: true }); throw new Error(`download: ${algo} mismatch (${got.slice(0, 12)}…)`); }
+ renameSync(part, dest);
+ return dest;
+}
+
+// --- setup progress (Desktop) --------------------------------------------------------
+
+/** The VM setup's steps, in order, as the setup panel shows them. */
+export const SETUP_STEPS = ["lima", "vm_image", "vm_boot", "image"] as const;
+export type SetupStep = (typeof SETUP_STEPS)[number];
+
+/** One setup line (pull-progress.ts parseSetupLine reads it back). */
+export function setupLine(step: SetupStep, bytes: number | null = null, total: number | null = null): string {
+ return JSON.stringify({ setup: { step, ...(bytes !== null ? { bytes } : {}), ...(total !== null ? { total } : {}) } });
+}
+
+/** Byte progress for a step, at most every 500 ms (and the last byte). */
+function stepProgress(step: SetupStep, onLine: OnLine | undefined): DownloadOptions["onProgress"] {
+ if (!onLine) return undefined;
+ let lastAt = 0;
+ return (done, total) => {
+ const now = Date.now();
+ if (now - lastAt < 500 && done !== total) return;
+ lastAt = now;
+ try { onLine("stdout", setupLine(step, done, total)); } catch { /* a listener's problem */ }
+ };
+}
+function say(onLine: OnLine | undefined, step: SetupStep): void {
+ try { onLine?.("stdout", setupLine(step)); } catch { /* a listener's problem */ }
+}
+
+// --- the probe -----------------------------------------------------------------------
+
+export type LimaRow = { name?: string; status?: string; dir?: string; config?: { mounts?: Array<{ location: string; mountPoint?: string; writable?: boolean }> } };
+
+/** `limactl list --json`: the instance's row ({ name, status, dir, config, ... }) or null. */
+export function parseList(stdout: unknown, name: string): LimaRow | null {
+ for (const line of String(stdout ?? "").split("\n")) {
+ const text = line.trim();
+ if (!text.startsWith("{")) continue;
+ try {
+ const row: unknown = JSON.parse(text);
+ if (row && typeof row === "object" && Reflect.get(row, "name") === name) return row as LimaRow;
+ } catch { /* next */ }
+ }
+ return null;
+}
+
+/** The VM's row from `limactl list` (null: not made yet). */
+export async function vmRow(runtime: Pick<LimaRuntime, "bin" | "home" | "instance">, { run = runCommand, env = process.env, timeoutMs = 15_000 }: { run?: RunCommand; env?: Env; timeoutMs?: number } = {}): Promise<{ row: LimaRow | null; res: RunResult; missing: boolean }> {
+ const res = await run(runtime.bin, ["list", "--json", runtime.instance], { env: limaEnv(runtime.home, env), timeoutMs });
+ const row = res.code === 0 ? parseList(res.stdout, runtime.instance) : null;
+ // limactl 2.x: a name with no instance is "No instance matching ... found" + "unmatched instances", exit 1.
+ const missing = !row && (res.code === 0 || /No instance matching|unmatched instances/i.test(String(res.stderr ?? "")));
+ return { row, res, missing };
+}
+
+export async function probeLima({ env = process.env, platform = process.platform, arch = process.arch, release = os.release(), run = runCommand, exists = existsSync, home, timeoutMs = 10_000, setup = loadDarwinSetup(), now = Date.now(), tools = toolsRoot(), explicit = false }: { env?: Env; platform?: NodeJS.Platform; arch?: string; release?: string; run?: RunCommand; exists?: (file: string) => boolean; home?: string; timeoutMs?: number; setup?: DarwinSetup; now?: number; tools?: string | null; explicit?: boolean } = {}): Promise<RuntimeResult> {
+ const base = { name: "lima", backend: "lima" };
+ if (platform !== "darwin") return { ...base, ok: false, reason: "the OmniRush VM is for macOS" };
+ if (env.OMNIRUSH_BOX_LIMA === "0") return { ...base, ok: false, reason: "turned off (OMNIRUSH_BOX_LIMA=0)" };
+ const major = macosMajor(release);
+ const min = Number.parseInt(String(setup.min_macos ?? "13"), 10) || 13;
+ if (major !== null && major < min) return { ...base, ok: false, reason: `the OmniRush VM needs macOS ${min} or later (this is macOS ${major})`, code: "old_macos" };
+ const boxHomeDir = home ?? path.join(os.homedir(), ".omnirush", "box");
+ // A VM that failed to start an hour ago is not tried again yet: a runtime that works is used instead.
+ const failed = readJson<{ at?: number; reason?: string }>(failedFile(boxHomeDir));
+ if (!explicit && failed && now - Number(failed.at) >= 0 && now - Number(failed.at) < FAILED_MS) {
+ return { ...base, ok: false, reason: `the OmniRush VM did not start (${failed.reason}); tried again within the hour, or now by choosing it in Settings (sandbox backend)`, code: "failed" };
+ }
+ const found = findLimactl({ env, home: boxHomeDir, exists, setup, tools });
+ if (!found.bin && found.source === "env") return { ...base, ok: false, reason: `OMNIRUSH_BOX_LIMACTL: ${env.OMNIRUSH_BOX_LIMACTL} is missing` };
+ if (!found.bin && !limaAsset(setup, arch)) return { ...base, ok: false, reason: "the OmniRush VM (Lima) is not published for this Mac yet" };
+ if (!vmImage({ env, setup, arch, home: boxHomeDir })) return { ...base, ok: false, reason: "the OmniRush VM image is not published yet (OMNIRUSH_BOX_LIMA_IMAGE=<qcow2> to use one)" };
+ const instance = vmName(setup, env);
+ const lh = limaHome(boxHomeDir, env, undefined, instance);
+ if (!limaHomeFits(lh, instance)) return { ...base, ok: false, reason: `the OmniRush VM's folder path is too long for macOS (${lh}); set OMNIRUSH_BOX_LIMA_HOME to a shorter folder` };
+ const goarch = GOARCH[arch] || "arm64";
+ const ready = { ...base, ok: true as const, limactlSource: found.source, home: boxHomeDir, instance, os: "linux", arch: goarch, flavor: "omnirush-vm", rootless: false, vm: true };
+ // Desktop: bin "" until limactl is here (the first setup downloads it; RuntimeInfo.bin is a string).
+ if (!found.bin) return { ...ready, bin: "", installed: false, state: null, version: setup.lima?.version ?? "" };
+ const ver = await run(found.bin, ["--version"], { env: limaEnv(boxHomeDir, env), timeoutMs });
+ if (ver.code !== 0) return { ...base, ok: false, bin: found.bin, reason: `limactl does not run here: ${firstLine(ver.stderr) || ver.error || ver.code}` };
+ const version = (/(\d+\.\d+\.\d+)/.exec(ver.stdout) ?? [])[1] ?? "";
+ const { row } = await vmRow({ bin: found.bin, home: boxHomeDir, instance }, { run, env, timeoutMs });
+ return { ...ready, bin: found.bin, installed: Boolean(row), state: row?.status ?? null, version };
+}
+
+// --- the VM --------------------------------------------------------------------------
+
+/** cpus, memory (GiB) and disk (GiB) for this Mac, from required.json setup.darwin.vm. */
+export function vmResources({ setup = loadDarwinSetup(), cpus = os.cpus().length, memBytes = os.totalmem(), env = process.env }: { setup?: DarwinSetup; cpus?: number; memBytes?: number; env?: Env } = {}): { cpus: number; memory: number; disk: number } {
+ const vm = setup.vm ?? {};
+ const cpuMax = Number(vm.cpus_max) > 0 ? Number(vm.cpus_max) : 4;
+ const wantCpus = Number(env.OMNIRUSH_BOX_LIMA_CPUS) > 0 ? Number(env.OMNIRUSH_BOX_LIMA_CPUS) : Math.min(cpuMax, Math.max(2, Math.floor(cpus / 2)));
+ const floor = Number(vm.memory_min_gib) > 0 ? Number(vm.memory_min_gib) : 3;
+ const fraction = Number(vm.memory_fraction) > 0 ? Number(vm.memory_fraction) : 0.25;
+ const memory = Number(env.OMNIRUSH_BOX_LIMA_MEMORY_GIB) > 0 ? Number(env.OMNIRUSH_BOX_LIMA_MEMORY_GIB) : Math.max(floor, Math.floor((memBytes / GIB) * fraction));
+ const disk = Number(vm.disk_gib) > 0 ? Number(vm.disk_gib) : 30;
+ return { cpus: wantCpus, memory, disk };
+}
+
+/** The idle stop in minutes (0: never). */
+export function idleStopMinutes({ setup = loadDarwinSetup(), env = process.env }: { setup?: DarwinSetup; env?: Env } = {}): number {
+ if (env.OMNIRUSH_BOX_LIMA_IDLE_MIN !== undefined && env.OMNIRUSH_BOX_LIMA_IDLE_MIN !== "") return Math.max(0, Number(env.OMNIRUSH_BOX_LIMA_IDLE_MIN) || 0);
+ return Number(setup.vm?.idle_stop_min) >= 0 ? Number(setup.vm?.idle_stop_min) : 30;
+}
+
+/** Whether `p` is `dir` or inside it. */
+function under(p: string, dir: string): boolean {
+ const rel = path.relative(dir, p);
+ return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
+}
+
+/** The folders the VM shares: this user's home, /Volumes, /private/tmp, plus `extra` (projects outside them). */
+export function vmMounts({ userHome = os.homedir(), extra = [] }: { userHome?: string; extra?: string[] } = {}): string[] {
+ const list = [userHome, ...BASE_MOUNTS];
+ for (const dir of extra) if (dir && !list.some((m) => under(dir, m))) list.push(dir);
+ return list;
+}
+
+/** Folders of `paths` that none of `mounts` covers (each is shared in at its own path). */
+export function uncovered(paths: Array<string | null | undefined>, mounts: string[]): string[] {
+ const out: string[] = [];
+ for (const p of paths) {
+ if (!p || !path.isAbsolute(p)) continue;
+ if (mounts.some((m) => under(p, m)) || out.some((m) => under(p, m))) continue;
+ out.push(p);
+ }
+ return out;
+}
+
+/**
+ * The provision script (as root, every boot): swap (the 3 GiB VM keeps a
+ * build from being killed), the store and box folders, nothing else. The
+ * same text as the CLI's (a VM made by either app is the same VM).
+ */
+export const PROVISION = [
+ "#!/bin/sh",
+ "set -eu",
+ "mkdir -p /opt/omnirush /var/lib/omnirush/store /var/lib/omnirush/boxes /var/lib/omnirush/shadows",
+ "if [ ! -f /var/lib/omnirush/swap ]; then fallocate -l 2G /var/lib/omnirush/swap 2>/dev/null || dd if=/dev/zero of=/var/lib/omnirush/swap bs=1M count=2048; chmod 600 /var/lib/omnirush/swap; mkswap /var/lib/omnirush/swap >/dev/null; fi",
+ "swapon --show=NAME --noheadings | grep -qx /var/lib/omnirush/swap || swapon /var/lib/omnirush/swap || true",
+ "sysctl -qw vm.swappiness=10 || true",
+ "systemctl enable --now systemd-timesyncd >/dev/null 2>&1 || true",
+ "",
+].join("\n");
+
+/**
+ * The Lima config of the VM (YAML is a superset of JSON, so it is written as
+ * JSON). `image`: a local file (downloaded and checked here first: Lima's own
+ * cache is never used).
+ */
+export function vmConfig({ image, arch = process.arch, cpus, memory, disk, mounts }: { image: string; arch?: string; cpus: number; memory: number; disk: number; mounts: string[] }): Record<string, unknown> {
+ const limaArch = LIMA_ARCH[GOARCH[arch] || arch] || "aarch64";
+ return {
+ vmType: "vz",
+ os: "Linux",
+ arch: limaArch,
+ images: [{ location: image, arch: limaArch }],
+ cpus,
+ memory: `${memory}GiB`,
+ disk: `${disk}GiB`,
+ mountType: "virtiofs",
+ mounts: mounts.map((location) => ({ location, mountPoint: location, writable: true })),
+ containerd: { system: false, user: false },
+ ssh: { loadDotSSHPubKeys: false, forwardAgent: false },
+ upgradePackages: false,
+ provision: [{ mode: "system", script: PROVISION }],
+ // Guest loopback ports reach this Mac's 127.0.0.1 (boxd's, and a dev server's in a box).
+ portForwards: [{ guestIP: "127.0.0.1", guestPortRange: [1024, 65535], hostIP: "127.0.0.1" }],
+ };
+}
+
+// --- the owner lock and the leases ---------------------------------------------------
+
+function alive(pid: number): boolean {
+ if (!pid) return false;
+ try { process.kill(pid, 0); return true; } catch (error) { return typeof error === "object" && error !== null && Reflect.get(error, "code") === "EPERM"; }
+}
+
+/**
+ * The owner lock on the VM (create, start, stop, edit): one process at a
+ * time, CLI or desktop. Stale when its pid is gone or after `staleMs`.
+ * Waits up to `waitMs`; returns release() or null.
+ */
+export async function lockVm(home: string, { waitMs = 5 * 60_000, staleMs = 15 * 60_000, pidAlive = alive, sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms)), now = () => Date.now(), what = "vm" }: { waitMs?: number; staleMs?: number; pidAlive?: (pid: number) => boolean; sleep?: (ms: number) => Promise<void>; now?: () => number; what?: string } = {}): Promise<(() => void) | null> {
+ const file = lockFile(home);
+ mkdirSync(path.dirname(file), { recursive: true });
+ const deadline = now() + waitMs;
+ const nonce = randomUUID();
+ for (;;) {
+ try {
+ const fd = openSync(file, "wx", 0o600);
+ try { writeSync(fd, JSON.stringify({ pid: process.pid, nonce, at: now(), what })); } finally { closeSync(fd); }
+ return () => {
+ try { if (JSON.parse(readFileSync(file, "utf8"))?.nonce === nonce) unlinkSync(file); } catch { /* gone */ }
+ };
+ } catch (error) {
+ if (!(typeof error === "object" && error !== null && Reflect.get(error, "code") === "EEXIST")) return null;
+ let held: { pid?: number } | null = null;
+ try { held = JSON.parse(readFileSync(file, "utf8")); } catch { /* half written */ }
+ let age = 0;
+ try { age = now() - statSync(file).mtimeMs; } catch { continue; }
+ if ((held && !pidAlive(Number(held.pid))) || (!held && age > 5_000) || age > staleMs) { try { unlinkSync(file); } catch { /* raced */ } continue; }
+ if (now() > deadline) return null;
+ await sleep(200);
+ }
+ }
+}
+
+/** A lease: this process runs box `key` in the VM. Returns release(). */
+let leaseCount = 0;
+export function takeLease(home: string, key: string, { pid = process.pid, client = "desktop" }: { pid?: number; client?: string } = {}): () => void {
+ // Its own file per lease (a keeper being replaced releases its old one later).
+ leaseCount += 1;
+ const file = path.join(leaseDir(home), `${key}-${pid}-${leaseCount}.json`);
+ mkdirSync(path.dirname(file), { recursive: true });
+ writeJson(file, { pid, key, client, at: new Date().toISOString() });
+ return () => { try { unlinkSync(file); } catch { /* gone */ } };
+}
+
+export type Lease = { pid: number; key: string; client: string; file: string };
+
+/** The live leases; a lease of a gone process is removed. */
+export function liveLeases(home: string, { pidAlive = alive, except = null }: { pidAlive?: (pid: number) => boolean; except?: number | null } = {}): Lease[] {
+ let names: string[] = [];
+ try { names = readdirSync(leaseDir(home)).filter((n) => n.endsWith(".json")); } catch { return []; }
+ const out: Lease[] = [];
+ for (const name of names) {
+ const file = path.join(leaseDir(home), name);
+ const lease = readJson<{ pid?: number; key?: string; client?: string }>(file);
+ if (!lease?.pid || !pidAlive(Number(lease.pid))) { try { unlinkSync(file); } catch { /* raced */ } continue; }
+ if (except && Number(lease.pid) === except) continue;
+ out.push({ pid: Number(lease.pid), key: String(lease.key ?? ""), client: String(lease.client ?? ""), file });
+ }
+ return out;
+}
+
+// --- limactl calls -------------------------------------------------------------------
+
+/** Whether a `limactl shell` failure is the VM being away (stopped, asleep, ssh gone), not the command's own. */
+export function vmAway(res: Pick<RunResult, "code" | "stderr"> & { stdout?: string; error?: string | undefined }): boolean {
+ if (res.error === "timeout") return true;
+ const said = `${res.stderr ?? ""}\n${res.stdout ?? ""}`;
+ return res.code === 255 || /is not running|not running|instance .* does not exist|ssh: connect|Connection refused|Connection reset|Broken pipe|kex_exchange|ControlSocket|mux_client|exit status 255/i.test(said);
+}
+
+/** A command in the VM as root, no shell in between. */
+export function inVm(runtime: Pick<LimaRuntime, "instance">, argv: string[]): string[] {
+ return ["shell", "--workdir", "/", runtime.instance, "sudo", "-n", ...argv];
+}
+
+type Ensure = (() => Promise<{ ok: boolean }>) | null;
+
+/**
+ * Run a command in the VM as root. After a failure that says the VM is away
+ * (sleep, a stopped VM, a dead ssh master), the VM is started and the command
+ * tried once more.
+ */
+export async function vmCall(runtime: LimaRuntime, argv: string[], { run = runCommand, env = process.env, timeoutMs = 60_000, input, ensure = null, sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms)), tries = 3, onLine }: { run?: RunCommand; env?: Env; timeoutMs?: number; input?: string; ensure?: Ensure; sleep?: (ms: number) => Promise<void>; tries?: number; onLine?: OnLine } = {}): Promise<RunResult> {
+ const call = () => run(runtime.bin, inVm(runtime, argv), { env: limaEnv(runtime.home, env), timeoutMs, ...(input !== undefined ? { input } : {}), ...(onLine ? { onLine } : {}) });
+ let res = await call();
+ if (res.code === 0 || !vmAway(res) || !ensure) return res;
+ const started = await ensure();
+ if (!started?.ok) return res;
+ // A VM that is up but whose sshd drops the first connections (just woken, many at once): a few more tries.
+ for (let attempt = 1; attempt <= tries; attempt += 1) {
+ res = await call();
+ if (res.code === 0 || !vmAway(res) || attempt === tries) return res;
+ await sleep(1_000 * attempt);
+ }
+ return res;
+}
+
+/** Start the VM (made before). */
+async function startVm(runtime: LimaRuntime, { run = runCommand, env = process.env, log = () => {} }: { run?: RunCommand; env?: Env; log?: BoxLog } = {}): Promise<{ ok: true } | { ok: false; error: string }> {
+ const res = await run(runtime.bin, ["--tty=false", "start", runtime.instance, "--timeout", "5m"], { env: limaEnv(runtime.home, env), timeoutMs: 6 * 60_000 });
+ writeLog(runtime.home, "start", res);
+ if (res.code !== 0) return { ok: false, error: `the VM did not start: ${limaError(res)}` };
+ log("info", "box vm started", { vm: runtime.instance });
+ return { ok: true };
+}
+
+/** Stop the VM (`limactl stop`; forced after a minute). */
+export async function stopVm(runtime: LimaRuntime, { run = runCommand, env = process.env }: { run?: RunCommand; env?: Env } = {}): Promise<{ ok: boolean }> {
+ let res = await run(runtime.bin, ["--tty=false", "stop", runtime.instance], { env: limaEnv(runtime.home, env), timeoutMs: 90_000 });
+ if (res.code !== 0 && !/not running/i.test(`${res.stderr}`)) res = await run(runtime.bin, ["--tty=false", "stop", "--force", runtime.instance], { env: limaEnv(runtime.home, env), timeoutMs: 60_000 });
+ return { ok: res.code === 0 || /not running/i.test(`${res.stderr}`) };
+}
+
+/** The line of limactl's output that says why (its fatal/error line, else the last). */
+export function limaError(res: Pick<RunResult, "code" | "stderr"> & { stdout?: string; error?: string | undefined }): string {
+ const lines = `${res.stderr ?? ""}\n${res.stdout ?? ""}`.split("\n").map((l) => l.trim()).filter(Boolean);
+ const pick = [...lines].reverse().find((l) => /level=(fatal|error)/.test(l)) ?? [...lines].reverse().find((l) => /error|failed|denied|not supported/i.test(l)) ?? lines.at(-1) ?? String(res.error || res.code);
+ const msg = /msg="((?:[^"\\]|\\.)*)"/.exec(pick);
+ return (msg ? msg[1]!.replace(/\\"/g, '"') : pick).slice(0, 300);
+}
+
+function writeLog(home: string, what: string, res: RunResult): void {
+ try {
+ mkdirSync(limaDir(home), { recursive: true });
+ writeFileSync(path.join(limaDir(home), `${what}.log`), `${new Date().toISOString()} code=${res.code}${res.error ? ` error=${res.error}` : ""}\n${String(res.stdout ?? "").slice(-20_000)}\n${String(res.stderr ?? "").slice(-40_000)}`);
+ } catch { /* best effort */ }
+}
+
+type EnsureOptions = { env?: Env; arch?: string; setup?: DarwinSetup; run?: RunCommand; fetchFile?: FetchFile; log?: BoxLog; onLine?: OnLine };
+
+/** limactl itself: downloaded once (pinned, sha256-checked) when this app ships none. */
+export async function ensureLimactl(runtime: LimaRuntime, { env = process.env, arch = process.arch, setup = loadDarwinSetup(), run = runCommand, fetchFile = download, log = () => {}, onLine }: EnsureOptions = {}): Promise<{ ok: true; bin: string } | { ok: false; error: string }> {
+ if (runtime.bin && existsSync(runtime.bin)) return { ok: true, bin: runtime.bin };
+ const asset = limaAsset(setup, arch);
+ const version = setup.lima?.version;
+ if (!asset || !version) return { ok: false, error: "the OmniRush VM (Lima) is not published for this Mac yet" };
+ const dir = limaDir(runtime.home);
+ const tarball = path.join(dir, "cache", `lima-${asset.sha256.slice(0, 16)}.tar.gz`);
+ say(onLine, "lima");
+ try {
+ await fetchChecked(asset.url, tarball, { algo: "sha256", digest: asset.sha256, fetchFile, onProgress: stepProgress("lima", onLine) });
+ } catch (error) {
+ return { ok: false, error: `could not download Lima: ${error instanceof Error ? error.message : String(error)}` };
+ }
+ const dest = path.join(dir, "dist", version);
+ const tmp = `${dest}.${process.pid}.tmp`;
+ rmSync(tmp, { recursive: true, force: true });
+ mkdirSync(tmp, { recursive: true });
+ const res = await run("/usr/bin/tar", ["-xzf", tarball, "-C", tmp], { env, timeoutMs: 120_000 });
+ if (res.code !== 0 || !existsSync(path.join(tmp, "bin", "limactl"))) { rmSync(tmp, { recursive: true, force: true }); return { ok: false, error: `could not unpack Lima: ${firstLine(res.stderr) || res.code}` }; }
+ // Only limactl and the Linux guest agent are used (vz is built in): the other drivers, the macOS guest agent and the wrappers go.
+ const ls = (d: string) => { try { return readdirSync(d); } catch { return [] as string[]; } };
+ rmSync(path.join(tmp, "libexec"), { recursive: true, force: true });
+ for (const name of ls(path.join(tmp, "bin"))) if (name !== "limactl") rmSync(path.join(tmp, "bin", name), { force: true });
+ for (const name of ls(path.join(tmp, "share", "lima"))) if (/^lima-guestagent\.Darwin/.test(name)) rmSync(path.join(tmp, "share", "lima", name), { force: true });
+ rmSync(dest, { recursive: true, force: true });
+ renameSync(tmp, dest);
+ rmSync(tarball, { force: true });
+ runtime.bin = path.join(dest, "bin", "limactl");
+ runtime.limactlSource = "download";
+ log("info", "box vm lima downloaded", { version });
+ return { ok: true, bin: runtime.bin };
+}
+
+/** The VM disk image, downloaded once and checked (a local OMNIRUSH_BOX_LIMA_IMAGE is used as it is). */
+export async function ensureImage(runtime: Pick<LimaRuntime, "home">, { env = process.env, arch = process.arch, setup = loadDarwinSetup(), fetchFile = download, onLine }: EnsureOptions = {}): Promise<{ ok: true; image: VmImage & { file: string } } | { ok: false; error: string }> {
+ const image = vmImage({ env, setup, arch, home: runtime.home });
+ if (!image?.file) return { ok: false, error: "the OmniRush VM image is not published yet" };
+ const file = image.file;
+ if (image.kind === "local") return existsSync(file) ? { ok: true, image: { ...image, file } } : { ok: false, error: `OMNIRUSH_BOX_LIMA_IMAGE: ${file} is missing` };
+ mkdirSync(path.dirname(file), { recursive: true });
+ say(onLine, "vm_image");
+ try {
+ await fetchChecked(String(image.url), file, { algo: image.algo ?? "sha256", digest: String(image.digest), fetchFile, onProgress: stepProgress("vm_image", onLine) });
+ } catch (error) {
+ return { ok: false, error: `could not download the VM image: ${error instanceof Error ? error.message : String(error)}` };
+ }
+ return { ok: true, image: { ...image, file } };
+}
+
+export type VmContract = { contract: number; image: { kind: string; version: string | null; digest: string | null } | null; client: string; client_version: string; lima: string; created_at: string };
+
+/** /opt/omnirush/vm.json in the VM (null: none yet, or the VM is away). */
+async function readContract(runtime: LimaRuntime, opts: { run?: RunCommand; env?: Env }): Promise<VmContract | null> {
+ const res = await vmCall(runtime, ["cat", `${OPT}/vm.json`], { ...opts, timeoutMs: 30_000 });
+ if (res.code !== 0) return null;
+ try { return JSON.parse(res.stdout); } catch { return null; }
+}
+
+/** Desktop: this app's version (the server package's, the app's own in a packaged build). */
+function ownVersion(env: Env = process.env): string {
+ return String(env.OMNIRUSH_APP_VERSION || serverPackage.version || "");
+}
+
+/** What this client would write as the VM's contract. */
+export function contractFor(image: Pick<VmImage, "kind" | "version" | "digest" | "algo"> | null, { client = "desktop", clientVersion = ownVersion(), lima = "" }: { client?: string; clientVersion?: string; lima?: string } = {}): VmContract {
+ return { contract: VM_CONTRACT, image: image ? { kind: image.kind, version: image.version, digest: image.digest ? `${image.algo}:${image.digest}` : null } : null, client, client_version: clientVersion, lima, created_at: new Date().toISOString() };
+}
+
+/**
+ * Whether this client may use a VM made under `have` (its /opt/omnirush/vm.json):
+ * "use" (same contract), "replace" (an older contract: only when no box runs), "newer" (made by a newer client: used as it is,
+ * never changed).
+ */
+export function contractVerdict(have: { contract?: unknown } | null | undefined): "use" | "replace" | "newer" {
+ const got = Number(have?.contract);
+ if (!Number.isFinite(got) || got === VM_CONTRACT) return "use";
+ return got < VM_CONTRACT ? "replace" : "newer";
+}
+
+export type EnsureVmOptions = {
+ env?: Env; run?: RunCommand; log?: BoxLog; paths?: Array<string | null | undefined>; arch?: string; setup?: DarwinSetup; fetchFile?: FetchFile; userHome?: string;
+ spawnImpl?: SpawnLike; clientVersion?: string; client?: string; resources?: { cpus: number; memory: number; disk: number } | null; now?: () => number; selfKey?: string | null; onLine?: OnLine;
+};
+export type EnsureVmResult = { ok: true; started: boolean; created?: boolean; contract?: VmContract | null; verdict?: "use" | "replace" | "newer" } | { ok: false; error: string };
+
+/**
+ * Make sure the VM runs and can take `paths` (the folders a box binds):
+ * limactl and the image downloaded, the VM made (or started), a folder outside
+ * the shared ones added (one restart, only while no other box runs), the
+ * contract written. Holds the owner lock while it changes the VM.
+ */
+export async function ensureVm(runtime: LimaRuntime, { env = process.env, run = runCommand, log = () => {}, paths = [], arch = process.arch, setup = loadDarwinSetup(), fetchFile = download, userHome = os.homedir(), spawnImpl, clientVersion = ownVersion(env), client = "desktop", resources = null, now = Date.now, selfKey = null, onLine }: EnsureVmOptions = {}): Promise<EnsureVmResult> {
+ const lima = await ensureLimactl(runtime, { env, arch, setup, run, fetchFile, log, onLine });
+ if (!lima.ok) return lima;
+ const first = await vmRow(runtime, { run, env });
+ let { row } = first;
+ if (!row && !first.missing) return { ok: false, error: `limactl does not answer: ${limaError(first.res)}` };
+ const wanted = (r: LimaRow | null) => uncovered(paths, (r?.config?.mounts ?? []).map((m) => m.mountPoint || m.location));
+ if (row?.status === "Running" && !wanted(row).length) {
+ keepIdleWatch(runtime, { env, ...(spawnImpl ? { spawnImpl } : {}) });
+ return { ok: true, started: false };
+ }
+ let image: (VmImage & { file: string }) | null = null;
+ if (!row) {
+ const got = await ensureImage(runtime, { env, arch, setup, fetchFile, onLine });
+ if (!got.ok) return got;
+ image = got.image;
+ }
+ let madeNow = false;
+ say(onLine, "vm_boot");
+ const release = await lockVm(runtime.home, { what: row ? "start" : "create" });
+ if (!release) return { ok: false, error: "another OmniRush app is setting up the sandbox VM; try again in a minute" };
+ try {
+ ({ row } = await vmRow(runtime, { run, env }));
+ const missing = row ? wanted(row) : [];
+ if (row && missing.length) {
+ // A project outside the shared folders: the VM gets it on its next start.
+ if (row.status === "Running") {
+ // Every running box counts, this process' other ones too (the desktop runs many); not the one starting now.
+ const others = liveLeases(runtime.home).filter((l) => !(l.pid === process.pid && (l.key === selfKey || l.key === "pull")));
+ if (others.length) return { ok: false, error: `${missing[0]} is outside the folders the sandbox VM shares, and it can only add it while no other chat uses the sandbox (move the project under your home folder, or close the other chats)` };
+ log("info", "box vm restarting to share a folder", { folders: missing });
+ await stopVm(runtime, { run, env });
+ }
+ const mounts = [...(row.config?.mounts ?? []).map((m) => ({ location: m.location, mountPoint: m.mountPoint || m.location, writable: m.writable !== false })), ...missing.map((location) => ({ location, mountPoint: location, writable: true }))];
+ const edit = await run(runtime.bin, ["--tty=false", "edit", runtime.instance, "--set", `.mounts = ${JSON.stringify(mounts)}`], { env: limaEnv(runtime.home, env), timeoutMs: 60_000 });
+ if (edit.code !== 0) return { ok: false, error: `could not share ${missing[0]} with the sandbox VM: ${limaError(edit)}` };
+ row = { ...row, status: "Stopped" };
+ }
+ if (!row) {
+ if (!image) {
+ // Deleted by someone else meanwhile: made again here.
+ const got = await ensureImage(runtime, { env, arch, setup, fetchFile, onLine });
+ if (!got.ok) return got;
+ image = got.image;
+ }
+ const res = resources ?? vmResources({ setup, env });
+ const config = vmConfig({ image: image.file, arch, ...res, mounts: vmMounts({ userHome, extra: uncovered(paths, vmMounts({ userHome })) }) });
+ const file = path.join(limaDir(runtime.home), `${runtime.instance}.yaml`);
+ mkdirSync(path.dirname(file), { recursive: true });
+ writeFileSync(file, `# Written by OmniRush for its sandbox VM (made once; \`limactl edit\` changes it).\n${JSON.stringify(config, null, 2)}\n`);
+ const started = now();
+ const made = await run(runtime.bin, ["--tty=false", "start", "--name", runtime.instance, "--timeout", "8m", file], { env: limaEnv(runtime.home, env), timeoutMs: 10 * 60_000 });
+ writeLog(runtime.home, "create", made);
+ if (made.code !== 0) {
+ markFailed(runtime.home, limaError(made));
+ return { ok: false, error: `could not set up the sandbox VM: ${limaError(made)}` };
+ }
+ runtime.installed = true;
+ madeNow = true;
+ // A new VM has none of the old one's boxes or images (one deleted with limactl, a reset).
+ for (const f of [presentFile(runtime.home), pendingFile(runtime.home)]) rmSync(f, { force: true });
+ rmSync(path.join(limaDir(runtime.home), "boxes"), { recursive: true, force: true });
+ log("info", "box vm created", { vm: runtime.instance, image: image.kind, version: image.version, ms: now() - started });
+ const contract = contractFor(image, { client, clientVersion, lima: (await limaVersion(runtime, { run, env })) || "" });
+ const wrote = await vmCall(runtime, ["/bin/sh", "-c", `mkdir -p ${OPT} && cat > ${OPT}/vm.json.new && mv ${OPT}/vm.json.new ${OPT}/vm.json`], { run, env, input: JSON.stringify(contract) });
+ if (wrote.code !== 0) log("warn", "box vm contract not written", { error: firstLine(wrote.stderr) });
+ } else if (row.status !== "Running") {
+ const started = await startVm(runtime, { run, env, log });
+ if (!started.ok) {
+ markFailed(runtime.home, started.error);
+ return started;
+ }
+ }
+ clearFailed(runtime.home);
+ } finally {
+ release();
+ }
+ const have = await readContract(runtime, { run, env });
+ const verdict = contractVerdict(have);
+ if (verdict === "replace") log("info", "box vm made by an older OmniRush; kept until no box uses it", { contract: have?.contract });
+ await runPendingRemovals(runtime, { run, env });
+ keepIdleWatch(runtime, { env, ...(spawnImpl ? { spawnImpl } : {}) });
+ return { ok: true, started: true, created: madeNow, contract: have, verdict };
+}
+
+async function limaVersion(runtime: LimaRuntime, { run = runCommand, env = process.env }: { run?: RunCommand; env?: Env } = {}): Promise<string> {
+ const res = await run(runtime.bin, ["--version"], { env: limaEnv(runtime.home, env), timeoutMs: 10_000 });
+ return (/(\d+\.\d+\.\d+)/.exec(res.stdout ?? "") ?? [])[1] ?? "";
+}
+
+function markFailed(home: string, reason: string): void {
+ try { writeJson(failedFile(home), { at: Date.now(), reason: String(reason).slice(0, 200) }); } catch { /* best effort */ }
+}
+function clearFailed(home: string): void {
+ try { rmSync(failedFile(home), { force: true }); } catch { /* none */ }
+}
+
+/**
+ * Desktop: the idle watcher's script, next to this module (lima-idle.js in a
+ * build; lima-idle.ts when the server runs from source under bun).
+ */
+export function idleMain(dir = HERE, exists: (file: string) => boolean = existsSync): string | null {
+ return [path.join(dir, "lima-idle.js"), path.join(dir, "lima-idle.ts")].find((file) => exists(file)) ?? null;
+}
+
+/**
+ * The idle watcher, one per VM (the CLI's or this app's; idle.json names its
+ * pid): started detached when none runs. Desktop: run with this app's own
+ * binary as node (ELECTRON_RUN_AS_NODE=1), so it outlives the app.
+ */
+export function keepIdleWatch(runtime: Pick<LimaRuntime, "home" | "bin" | "instance">, { env = process.env, spawnImpl = spawn, node = process.execPath, pidAlive = alive, script = idleMain() }: { env?: Env; spawnImpl?: SpawnLike; node?: string; pidAlive?: (pid: number) => boolean; script?: string | null } = {}): number | null {
+ if (idleStopMinutes({ env }) <= 0 || !script) return null;
+ const held = readJson<{ pid?: number }>(idleFile(runtime.home));
+ if (held?.pid && pidAlive(Number(held.pid))) return Number(held.pid);
+ try {
+ const child: ChildProcess = spawnImpl(node, [script, runtime.home, runtime.bin, runtime.instance], {
+ env: { ...env, ...(process.versions.electron ? { ELECTRON_RUN_AS_NODE: "1" } : {}), OMNIRUSH_BOX_LIMA_IDLE_PARENT: String(process.pid) },
+ detached: true,
+ stdio: "ignore",
+ });
+ child.unref?.();
+ if (child.pid) writeJson(idleFile(runtime.home), { pid: child.pid, at: new Date().toISOString() });
+ return child.pid ?? null;
+ } catch {
+ return null;
+ }
+}
+
+/**
+ * One step of the idle watcher: { stop: true } when the VM runs, no lease is
+ * live and it has been idle for `idleMs` since `busyAt` (the caller stops it
+ * under the owner lock); { done: true } when the VM is not running.
+ */
+export async function idleStep(runtime: LimaRuntime, { busyAt, now = Date.now(), idleMs, run = runCommand, env = process.env, pidAlive = alive }: { busyAt: number; now?: number; idleMs: number; run?: RunCommand; env?: Env; pidAlive?: (pid: number) => boolean }): Promise<{ busyAt: number; stop?: true; done?: true }> {
+ const { row, missing } = await vmRow(runtime, { run, env });
+ if (missing || (row && row.status !== "Running")) return { done: true, busyAt };
+ if (liveLeases(runtime.home, { pidAlive }).length) return { busyAt: now };
+ return now - busyAt >= idleMs ? { stop: true, busyAt } : { busyAt };
+}
+
+/** Stop the VM when it is still idle, under the owner lock (the idle watcher's last step). */
+export async function stopIfIdle(runtime: LimaRuntime, { run = runCommand, env = process.env, pidAlive = alive, lock = lockVm }: { run?: RunCommand; env?: Env; pidAlive?: (pid: number) => boolean; lock?: typeof lockVm } = {}): Promise<{ stopped: boolean; reason?: string }> {
+ const release = await lock(runtime.home, { waitMs: 30_000, what: "idle-stop" });
+ if (!release) return { stopped: false, reason: "locked" };
+ try {
+ if (liveLeases(runtime.home, { pidAlive }).length) return { stopped: false, reason: "busy" };
+ const done = await stopVm(runtime, { run, env });
+ return { stopped: done.ok };
+ } finally {
+ release();
+ }
+}
+
+// --- the helpers in the VM -----------------------------------------------------------
+
+/** A stamp of the helpers this install ships (the CLI's formula: the same helpers get the same folder). */
+function helperStamp(files: string[]): string {
+ const hash = createHash("sha256");
+ for (const f of files) { try { const s = statSync(f); hash.update(`${s.size}-${Math.floor(s.mtimeMs)};`); } catch { hash.update("0;"); } }
+ return hash.digest("hex").slice(0, 16);
+}
+
+/** The install script for the helpers in the VM: the same text as the CLI's (the same folders, the same newest-five rule). */
+export const HELPER_SCRIPT = 'set -e; d="$1"; if [ ! -x "$d/boxd" ]; then mkdir -p "$d.tmp"; install -m 0755 "$2/boxrun" "$d.tmp/boxrun"; install -m 0755 "$2/boxd" "$d.tmp/boxd"; rm -rf "$d"; mv "$d.tmp" "$d"; fi; touch "$d"; ln -sfn "$d/boxrun" /opt/omnirush/boxrun; ln -sfn "$d/boxd" /opt/omnirush/boxd; ls -1dt /opt/omnirush/h/*/ 2>/dev/null | tail -n +6 | xargs -r rm -rf';
+
+/**
+ * Copy this install's boxrun and boxd into the VM, each install's into its
+ * own folder (/opt/omnirush/h/<stamp>), so the CLI and the desktop app (each
+ * with its own helpers) never swap them under each other. Staged through the
+ * box home (shared into the VM).
+ */
+export async function syncHelpers(runtime: LimaRuntime, { env = process.env, run = runCommand, ensure = null }: { env?: Env; run?: RunCommand; ensure?: Ensure } = {}): Promise<{ ok: true; dir: string } | { ok: false; error: string }> {
+ const goarch = runtime.arch || "arm64";
+ const boxrun = helperPath("boxrun", { goos: "linux", goarch, env });
+ const boxd = helperPath("boxd", { goos: "linux", goarch, env });
+ if (!boxrun || !boxd) return { ok: false, error: "the sandbox helpers (linux boxrun, boxd) are missing from this install" };
+ const stamp = helperStamp([boxrun, boxd]);
+ const dir = `${OPT}/h/${stamp}`;
+ if (runtime.helpers === dir) return { ok: true, dir };
+ const stage = path.join(limaDir(runtime.home), "helpers", stamp);
+ if (!existsSync(path.join(stage, "boxd"))) {
+ mkdirSync(stage, { recursive: true });
+ copyFileSync(boxrun, path.join(stage, "boxrun"));
+ copyFileSync(boxd, path.join(stage, "boxd"));
+ }
+ const res = await vmCall(runtime, ["/bin/sh", "-c", HELPER_SCRIPT, "sh", dir, stage], { run, env, timeoutMs: 120_000, ensure });
+ if (res.code !== 0) return { ok: false, error: `could not update the helpers in the sandbox VM: ${firstLine(res.stderr) || res.code}` };
+ runtime.helpers = dir;
+ // The staged copies are only a way in.
+ try { for (const name of readdirSync(path.dirname(stage))) if (name !== stamp) rmSync(path.join(path.dirname(stage), name), { recursive: true, force: true }); } catch { /* best effort */ }
+ return { ok: true, dir };
+}
+
+async function boxrun(runtime: LimaRuntime, args: string[], { run = runCommand, env = process.env, timeoutMs = 60_000, ensure = null, onLine }: { run?: RunCommand; env?: Env; timeoutMs?: number; ensure?: Ensure; onLine?: OnLine } = {}): Promise<{ res: RunResult; out: Record<string, unknown> | null }> {
+ const res = await vmCall(runtime, [`${runtime.helpers || OPT}/boxrun`, ...args], { run, env, timeoutMs, ensure, ...(onLine ? { onLine } : {}) });
+ return { res, out: parseOut(res.stdout) };
+}
+
+// --- images --------------------------------------------------------------------------
+
+function readPresent(home: string): Record<string, PresentImage & { at?: string }> {
+ const data = readJson<Record<string, PresentImage & { at?: string }>>(presentFile(home));
+ return data && typeof data === "object" ? data : {};
+}
+function notePresent(home: string, ref: string, value: PresentImage | null): void {
+ try {
+ const data = readPresent(home);
+ if (value) data[ref] = { ...value, at: new Date().toISOString() };
+ else delete data[ref];
+ writeJson(presentFile(home), data);
+ } catch { /* best effort */ }
+}
+
+/** The runtime as the VM's calls need it (the box home and the VM's name filled in; limactl when it is here). */
+function withHome(runtime: BoxRuntime | LimaRuntime, home: string | undefined): LimaRuntime {
+ const rt = runtime as BoxRuntime & Partial<LimaRuntime>;
+ if (!rt.home && home) rt.home = home;
+ if (!rt.instance) rt.instance = vmName();
+ if (!rt.bin && rt.home) rt.bin = findLimactl({ home: rt.home }).bin ?? "";
+ return rt as LimaRuntime;
+}
+
+/** The VM, running: is it? (no start) */
+async function running(runtime: LimaRuntime, { run = runCommand, env = process.env }: { run?: RunCommand; env?: Env } = {}): Promise<boolean> {
+ if (!runtime.bin || !existsSync(runtime.bin)) return false;
+ const { row } = await vmRow(runtime, { run, env });
+ return row?.status === "Running";
+}
+
+/**
+ * The image in the VM's store: asked of boxrun while the VM runs; while it is
+ * stopped, what the last look found (images.json): a status never boots the VM to ask.
+ */
+async function imagePresent(runtime: BoxRuntime | LimaRuntime, image: { pull: string }, { run = runCommand, env = process.env, home }: { run?: RunCommand; env?: Env; home?: string } = {}): Promise<PresentImage | null> {
+ const rt = withHome(runtime, home);
+ if (!rt.bin || !existsSync(rt.bin)) return null;
+ const { row } = await vmRow(rt, { run, env });
+ if (!row) return null;
+ if (row.status !== "Running") return readPresent(rt.home)[image.pull] ?? null;
+ const synced = await syncHelpers(rt, { env, run });
+ if (!synced.ok) return readPresent(rt.home)[image.pull] ?? null;
+ const { out } = await boxrun(rt, ["image", "--store", STORE, "--ref", image.pull, "--arch", rt.arch || "arm64"], { run, env });
+ const present = out?.present ? { id: str(out.id), digest: str(out.digest) || null, arch: str(out.arch), config: str(out.config) } : null;
+ notePresent(rt.home, image.pull, present);
+ return present;
+}
+
+type PullOptions = { run?: RunCommand; env?: Env; home?: string; log?: BoxLog; onLine?: OnLine; ensureOpts?: EnsureVmOptions };
+
+/** "Download": Lima and the VM (once), the helpers, then the image inside it. */
+async function pull(runtime: BoxRuntime | LimaRuntime, image: { pull: string }, opts: PullOptions = {}): Promise<{ ok: true } | { ok: false; error: string }> {
+ // A lease for the download: the idle stop never takes the VM away under a long first download.
+ const release = takeLease(opts.home || String((runtime as Partial<LimaRuntime>).home ?? ""), "pull");
+ try { return await pullNow(runtime, image, opts); } finally { release(); }
+}
+
+async function pullNow(runtime: BoxRuntime | LimaRuntime, image: { pull: string }, { run = runCommand, env = process.env, home, log = () => {}, onLine, ensureOpts = {} }: PullOptions = {}): Promise<{ ok: true } | { ok: false; error: string }> {
+ const rt = withHome(runtime, home);
+ const base = { env, run, log, paths: [rt.home], ...ensureOpts, ...(onLine ? { onLine } : {}) };
+ const vm = await ensureVm(rt, base);
+ if (!vm.ok) return { ok: false, error: vm.error };
+ const ensure = () => ensureVm(rt, base);
+ const synced = await syncHelpers(rt, { env, run, ensure });
+ if (!synced.ok) return { ok: false, error: synced.error };
+ say(onLine, "image");
+ const { res, out } = await boxrun(rt, ["pull", "--store", STORE, "--ref", image.pull, "--arch", rt.arch || "arm64"], { run, env, timeoutMs: 60 * 60_000, ensure, ...(onLine ? { onLine } : {}) });
+ if (!out?.ok) return { ok: false, error: String(str(out?.error) || firstLine(res.stderr) || res.code).slice(0, 300) };
+ await imagePresent(rt, image, { run, env });
+ return { ok: true };
+}
+
+// --- boxes ---------------------------------------------------------------------------
+
+type Keeper = { child: ChildProcess; endpoint: { host: string; port: number; token: string }; release: () => void };
+
+/** Keepers (`limactl shell ... boxrun up --foreground`) of this process, per box, with their leases. */
+const keepers = new Map<string, Keeper>();
+let exitHook = false;
+function releaseAllOnExit(): void {
+ if (exitHook) return;
+ exitHook = true;
+ process.once("exit", () => { for (const k of keepers.values()) try { k.release(); } catch { /* gone */ } });
+}
+
+export type KeeperResult = { ok: true; out: Record<string, unknown>; child: ChildProcess } | { ok: false; error: string; code?: number | null; away?: boolean };
+
+/** Start the keeper and read boxrun's first JSON line (the port). */
+export function startKeeper(runtime: LimaRuntime, args: string[], { env = process.env, spawnImpl = spawn, timeoutMs = 180_000 }: { env?: Env; spawnImpl?: SpawnLike; timeoutMs?: number } = {}): Promise<KeeperResult> {
+ return new Promise((resolve) => {
+ let child: ChildProcess;
+ try {
+ child = spawnImpl(runtime.bin, inVm(runtime, [`${runtime.helpers || OPT}/boxrun`, ...args, "--foreground"]), { env: limaEnv(runtime.home, env), stdio: ["pipe", "pipe", "pipe"] });
+ } catch (error) {
+ resolve({ ok: false, error: error instanceof Error ? error.message : String(error) });
+ return;
+ }
+ let buffered = "";
+ let errText = "";
+ let done = false;
+ const finish = (value: { ok: true; out: Record<string, unknown> } | { ok: false; error: string; code?: number | null }) => {
+ if (done) return;
+ done = true;
+ clearTimeout(timer);
+ if (value.ok) {
+ child.stdout?.removeAllListeners("data");
+ child.stdout?.resume();
+ child.unref();
+ for (const handle of [child.stdin, child.stdout, child.stderr]) {
+ if (handle && "unref" in handle && typeof handle.unref === "function") handle.unref();
+ }
+ resolve({ ...value, child });
+ } else {
+ try { child.kill(); } catch { /* gone */ }
+ resolve({ ...value, away: vmAway({ code: value.code ?? -1, stderr: errText }) });
+ }
+ };
+ const timer = setTimeout(() => finish({ ok: false, error: `the sandbox VM did not start the box in ${Math.round(timeoutMs / 1000)} s` }), timeoutMs);
+ child.stdout?.on("data", (chunk: Buffer) => {
+ buffered += String(chunk);
+ const out = parseOut(buffered.split("\n").filter((line) => line.trim().startsWith("{")).join("\n"));
+ if (out) finish(out.ok ? { ok: true, out } : { ok: false, error: str(out.error) || "boxrun up failed" });
+ });
+ child.stderr?.on("data", (chunk: Buffer) => { if (errText.length < 4000) errText += String(chunk); });
+ child.on("error", (error) => finish({ ok: false, error: error.message }));
+ child.on("close", (code) => finish({ ok: false, code, error: `the sandbox VM's keeper exited (${code}): ${firstLine(errText)}` }));
+ });
+}
+
+type LimaMeta = BoxMeta & { shadows?: string[]; started_at?: string };
+
+/** The host folders a box binds (each must be shared into the VM). */
+export function bindSources({ root, runFolder, shims, pkglog }: { root: string; runFolder: string; shims?: string | null; pkglog?: { root: string } | null }): string[] {
+ return [root, runFolder, shims, pkglog?.root].filter((p): p is string => Boolean(p));
+}
+
+/** Desktop: what the tests swap in (the VM's own calls, the idle watcher's spawn, the downloads). */
+export type LimaOpenExtras = { idleSpawn?: SpawnLike; fetchFile?: FetchFile; userHome?: string; resources?: { cpus: number; memory: number; disk: number }; freePort?: () => Promise<number> };
+
+async function open(ctx: OpenContext & LimaOpenExtras): Promise<Opened> {
+ const { key, root, runtime: given, home, log } = ctx;
+ const runtime = withHome(given, home);
+ const file = metaFile(home, key);
+ const meta = readJson<LimaMeta>(file);
+ if (meta && meta.root !== root) return { ok: false, error: `box ${key} belongs to another folder` };
+ const live = keepers.get(key);
+ if (live && live.child.exitCode === null && meta) {
+ // After sleep the forward of the VM's loopback (or boxd, at its idle limit) can be gone: reused only when boxd answers.
+ if (await (ctx.ping ?? ping)(live.endpoint, { timeoutMs: 1_500 })) {
+ return { ok: true, created: null, endpoint: live.endpoint, uid: 0, gid: 0, containerId: `lima_${key}_${meta.instance}`, imageId: meta.config || meta.image, mounts: meta.mounts ?? [], shadows: meta.shadows ?? [] };
+ }
+ log("info", "box keeper running but boxd not answering; starting it again", { key });
+ try { live.child.kill(); } catch { /* gone */ }
+ live.release();
+ keepers.delete(key);
+ }
+ // The lease from the start (a first start downloads for minutes): the idle stop leaves the VM up meanwhile.
+ const release = takeLease(home, key);
+ try {
+ const opened = await openWithLease(ctx, runtime, meta, release);
+ if (!opened.ok) release();
+ return opened;
+ } catch (error) {
+ release();
+ throw error;
+ }
+}
+
+async function openWithLease(ctx: OpenContext & LimaOpenExtras, runtime: LimaRuntime, given: LimaMeta | null, release: () => void): Promise<Opened> {
+ const { key, root, croot, run, home, runFolder, image, pull: mayPull, shims, pkglog, shadows = null, env, log, onPulling, onLine, spawnImpl } = ctx;
+ let meta = given;
+ const file = metaFile(home, key);
+ const paths = bindSources({ root, runFolder, shims, pkglog });
+ const ensureOpts: EnsureVmOptions = { ...(ctx.idleSpawn ? { spawnImpl: ctx.idleSpawn } : {}), ...(ctx.fetchFile ? { fetchFile: ctx.fetchFile } : {}), ...(ctx.userHome ? { userHome: ctx.userHome } : {}), ...(ctx.resources ? { resources: ctx.resources } : {}), selfKey: key, ...(onLine ? { onLine } : {}) };
+ const ensure = () => ensureVm(runtime, { env, run, log, paths, ...ensureOpts });
+ const made = runtime.bin && existsSync(runtime.bin) ? Boolean((await vmRow(runtime, { run, env })).row) : false;
+ if (!made) {
+ // Making the VM is the "download" of this backend (Lima, the VM image, then the box image).
+ if (!mayPull) return { ok: false, error: "the OmniRush sandbox VM is not set up yet" };
+ onPulling();
+ }
+ const vm = await ensure();
+ if (!vm.ok) return { ok: false, error: `could not start the sandbox VM: ${vm.error}` };
+ const synced = await syncHelpers(runtime, { env, run, ensure });
+ if (!synced.ok) return { ok: false, error: synced.error };
+ if (vm.created) meta = null;
+ let present = await imagePresent(runtime, image, { run, env });
+ // (a box whose image went from the store, e.g. a VM made again, gets it again when it may download)
+ if (!present && (!meta || mayPull)) {
+ if (!mayPull) return { ok: false, error: "the sandbox image is not downloaded yet" };
+ onPulling();
+ const pulled = await pull(runtime, image, { run, env, home, log, ...(onLine ? { onLine } : {}), ensureOpts: { ...ensureOpts, paths } });
+ if (!pulled.ok) return { ok: false, error: `could not set up the sandbox: ${pulled.error}` };
+ present = await imagePresent(runtime, image, { run, env });
+ if (!present) return { ok: false, error: "the sandbox image is not there after the download" };
+ }
+ let created = null;
+ if (!meta) {
+ if (!present) return { ok: false, error: "the sandbox image is not downloaded yet" };
+ meta = newMeta({ key, root, croot, present, image });
+ mkdirSync(path.dirname(file), { recursive: true });
+ writeJson(file, meta);
+ created = { ref: image.ref, pull: image.pull, digest: meta.digest, id: present.config || present.id, stack: image.stack };
+ log("info", "box created", { key, image: image.pull, stack: image.stack, backend: "lima" });
+ }
+ const idle = Number(env.OMNIRUSH_BOX_IDLE_MIN) > 0 ? Number(env.OMNIRUSH_BOX_IDLE_MIN) : IDLE_MINUTES;
+ // A port free on this Mac, so Lima's forward of the VM's loopback lands at the same number.
+ const port = await (ctx.freePort ?? freePort)();
+ const args = upArgs({
+ box: boxPath(key), store: STORE, image: meta.image, boxd: `${runtime.helpers}/boxd`, portFile: path.join(runFolder, "boxd.port"),
+ root, croot, runFolder, shims, pkglog, token: meta.token, idleMinutes: idle, listen: `127.0.0.1:${port}`, platform: "darwin",
+ shadows, shadowStore: shadows ? `${SHADOW_STORES}/${key}` : null,
+ });
+ releaseAllOnExit();
+ let kept = await startKeeper(runtime, args, { env, ...(spawnImpl ? { spawnImpl } : {}) });
+ if (!kept.ok && kept.away) {
+ // The VM went away between the check and the start (sleep, an idle stop): started once more.
+ const again = await ensure();
+ if (again.ok) kept = await startKeeper(runtime, args, { env, ...(spawnImpl ? { spawnImpl } : {}) });
+ }
+ if (!kept.ok) return { ok: false, error: `could not start the sandbox: ${kept.error}` };
+ const endpoint = { host: "127.0.0.1", port: num(kept.out.port) ?? 0, token: meta.token };
+ const mounts = [croot, "/run/omnirush", ...(shims ? ["/opt/omnirush/pkglog-src"] : []), ...(pkglog ? [pkglog.root] : [])];
+ // The folders boxrun shadowed (a box it found running keeps the set it started with).
+ const reported = Array.isArray(kept.out.shadows) ? kept.out.shadows.filter((item): item is string => typeof item === "string") : null;
+ const shadowed = reported ?? (kept.out.reused ? (meta.shadows ?? []) : []);
+ if (!kept.out.reused) writeJson(file, { ...meta, mounts, shadows: shadowed, started_at: new Date().toISOString() });
+ const entry: Keeper = { child: kept.child, endpoint, release };
+ kept.child.on?.("close", () => { release(); if (keepers.get(key) === entry) keepers.delete(key); });
+ keepers.set(key, entry);
+ return { ok: true, created, endpoint, uid: 0, gid: 0, containerId: `lima_${key}_${meta.instance}`, imageId: meta.config || meta.image, mounts: kept.out.reused ? (meta.mounts ?? mounts) : mounts, shadows: shadowed, timing: { ...(num(kept.out.ms) !== null ? { ms: Number(kept.out.ms) } : {}), reused: Boolean(kept.out.reused) } };
+}
+
+function keeperAlive(key: string): boolean {
+ const live = keepers.get(key);
+ return Boolean(live && live.child.exitCode === null);
+}
+
+async function list(runtime: BoxRuntime | LimaRuntime, { home = "" }: { home?: string } = {}): Promise<ListedBox[]> {
+ const dir = path.join(limaDir(home), "boxes");
+ let names: string[] = [];
+ try { names = readdirSync(dir).filter((n) => n.endsWith(".json")); } catch { return []; }
+ void runtime;
+ const leases = new Set(liveLeases(home).map((l) => l.key));
+ return names.map((n) => readJson<LimaMeta>(path.join(dir, n))).filter((m): m is LimaMeta => Boolean(m?.key)).map((m) => ({ key: m.key, name: containerName(m.key), root: m.root, running: leases.has(m.key) || keeperAlive(m.key), image: m.pull || m.ref, created: m.created_at || "" }));
+}
+
+/** Box removals asked for while the VM was stopped: done at its next start (never a start of their own). */
+async function runPendingRemovals(runtime: LimaRuntime, { run = runCommand, env = process.env }: { run?: RunCommand; env?: Env } = {}): Promise<void> {
+ const keys = readJson<unknown[]>(pendingFile(runtime.home));
+ if (!Array.isArray(keys) || !keys.length) return;
+ const left: string[] = [];
+ for (const key of keys) {
+ if (!/^[0-9a-zA-Z_-]+$/.test(String(key))) continue;
+ if (!runtime.helpers) await syncHelpers(runtime, { env, run });
+ const { out } = await boxrun(runtime, ["rm", "--box", boxPath(String(key))], { run, env, timeoutMs: 120_000 });
+ if (!out?.ok) left.push(String(key));
+ }
+ if (left.length) writeJson(pendingFile(runtime.home), left);
+ else rmSync(pendingFile(runtime.home), { force: true });
+}
+
+export const limaDriver: BoxDriver = {
+ kind: "lima",
+ execFallback: false,
+ /** boxrun runs as root in the VM: a box here gets its own build folders on this Mac (required.ts). */
+ shadows: true,
+ /** Lima forwards the VM's loopback ports to this Mac's 127.0.0.1 itself (the boxes share the VM's network). */
+ forwardsPorts: true,
+ imagePresent,
+ pull,
+ /** Desktop: Lima, the VM and the image download run in this process (launch.ts pullInBackground), as for the WSL distro. */
+ pullCommand() {
+ return null;
+ },
+ /** The box's record, in a VM that is still there (one deleted with limactl takes its boxes along). */
+ async exists(runtime, key, { home = "", run = runCommand, env = process.env } = {}) {
+ if (!existsSync(metaFile(home, key))) return false;
+ const rt = withHome(runtime, home);
+ if (!rt.bin || !existsSync(rt.bin)) return false;
+ return !(await vmRow(rt, { run, env })).missing;
+ },
+ open,
+ list,
+ async remove(runtime, key, { run = runCommand, env = process.env, home = "" } = {}) {
+ const rt = withHome(runtime, home);
+ const k = keepers.get(key);
+ try { k?.child.kill(); } catch { /* gone */ }
+ k?.release();
+ keepers.delete(key);
+ const had = existsSync(metaFile(home, key));
+ let ok = false;
+ if (await running(rt, { run, env })) {
+ if (!rt.helpers) await syncHelpers(rt, { env, run });
+ ok = Boolean((await boxrun(rt, ["rm", "--box", boxPath(key)], { run, env, timeoutMs: 120_000 })).out?.ok);
+ } else if (had) {
+ const pending = readJson<unknown[]>(pendingFile(home));
+ writeJson(pendingFile(home), [...new Set([...(Array.isArray(pending) ? pending.map(String) : []), key])]);
+ ok = true;
+ }
+ rmSync(metaFile(home, key), { force: true });
+ return { removed: had && ok };
+ },
+ async stop(runtime, key, { run = runCommand, env = process.env, home = "" } = {}) {
+ const rt = withHome(runtime, home);
+ const k = keepers.get(key);
+ try { k?.child.kill(); } catch { /* gone */ }
+ k?.release();
+ keepers.delete(key);
+ if (!(await running(rt, { run, env }))) return { stopped: true };
+ if (!rt.helpers) await syncHelpers(rt, { env, run });
+ const { out } = await boxrun(rt, ["stop", "--box", boxPath(key)], { run, env });
+ return { stopped: Boolean(out?.ok) };
+ },
+ /**
+ * Box images in the VM's store that no box uses and that are neither the
+ * current nor the previous pins (only while the VM runs: never started for
+ * this), downloads of other Lima versions and VM images on this Mac.
+ */
+ async gcImages(runtime, { run = runCommand, env = process.env, home = "", current, used, keep = { refs: current, digests: new Set<string>() }, dryRun = false, now = Date.now(), setup = loadDarwinSetup() }: Parameters<BoxDriver["gcImages"]>[1] & { setup?: DarwinSetup }) {
+ const rt = withHome(runtime, home);
+ const removed: Removed[] = [];
+ if (home) {
+ const drop = (item: { path: string }, reason: string) => {
+ if (dryRun) removed.push({ kind: "download", name: item.path, reason, dryRun: true });
+ else if (removeTree(item.path)) removed.push({ kind: "download", name: item.path, reason });
+ };
+ const version = setup.lima?.version;
+ // Lima of other pins: unused once a newer one is here (the one in use is never removed).
+ const inUse = rt.bin ? path.resolve(rt.bin, "..", "..") : null;
+ for (const item of staleEntries(path.join(limaDir(home), "dist"), { now, maxAgeMs: -1, match: (name: string) => name !== version && !name.endsWith(".tmp") })) if (item.path !== inUse) drop(item, "Lima of other pins");
+ // The VM image of other pins (a made VM no longer reads its image; the current one stays for a re-make).
+ const image = vmImage({ env, setup, home });
+ for (const item of staleEntries(path.join(limaDir(home), "images"), { now, maxAgeMs: -1, match: (name: string) => /\.(qcow2|raw)$/.test(name) && (!image?.file || name !== path.basename(image.file)) })) drop(item, "VM image of other pins");
+ for (const dir of ["images", "cache", "dist"]) for (const item of staleEntries(path.join(limaDir(home), dir), { now, match: (name: string) => /\.dl$|\.tmp$|\.part$/.test(name) })) drop(item, "left by a stopped download");
+ }
+ if (!(await running(rt, { run, env }))) return removed;
+ if (!rt.helpers) await syncHelpers(rt, { env, run });
+ const script = `for f in ${STORE}/images/*/image.json; do [ -f "$f" ] || continue; d=\${f%/image.json}; printf '%s\\t%s\\t' "\${d##*/}" "$(stat -c %Y "$f" 2>/dev/null || echo 0)"; tr -d '\\n\\r' < "$f"; printf '\\n'; done`;
+ const res = await vmCall(rt, ["/bin/sh", "-c", script], { run, env, timeoutMs: 60_000 });
+ if (res.code !== 0) return removed;
+ const usedIds = new Set<string>(used);
+ for (const box of await list(rt, { home })) {
+ const meta = readJson<LimaMeta>(metaFile(home, box.key));
+ if (meta?.image) usedIds.add(meta.image);
+ }
+ for (const line of String(res.stdout || "").split(/\r?\n/)) {
+ const [rawId = "", mtime = "", ...rest] = line.split("\t");
+ const id = rawId.trim();
+ let img: { ref?: string; digest?: string } | null = null;
+ try { img = JSON.parse(rest.join("\t")); } catch { continue; }
+ const unpackedAt = Number(mtime) * 1000;
+ if (!Number.isFinite(unpackedAt) || unpackedAt <= 0 || now - unpackedAt < YOUNG_IMAGE_MS) continue;
+ if (!/^[0-9a-zA-Z._-]+$/.test(id) || !removableImage({ ref: img?.ref, digest: img?.digest, id }, { keep, used: usedIds })) continue;
+ if (dryRun) { removed.push({ kind: "image", name: String(img?.ref), reason: "not current", dryRun: true }); continue; }
+ const { out } = await boxrun(rt, ["rmimage", "--store", STORE, "--id", id, "--boxes", BOXES], { run, env, timeoutMs: 120_000 });
+ if (out?.ok) { removed.push({ kind: "image", name: String(img?.ref), reason: "not current" }); notePresent(home, String(img?.ref), null); }
+ }
+ return removed;
+ },
+ async upperBytes(runtime, key, { run = runCommand, env = process.env, home = "" } = {}) {
+ const rt = withHome(runtime, home);
+ if (!(await running(rt, { run, env }))) return null;
+ if (!rt.helpers) await syncHelpers(rt, { env, run });
+ const { out } = await boxrun(rt, ["status", "--box", boxPath(key)], { run, env });
+ return num(out?.upper_bytes);
+ },
+ shellCommand(runtime, { key, croot, home }) {
+ const rt = withHome(runtime, home);
+ // LIMA_HOME rides on the command line: the terminal's own environment is passed as it is.
+ return { bin: "/usr/bin/env", args: [`LIMA_HOME=${limaHome(rt.home)}`, rt.bin, ...inVm(rt, [`${OPT}/boxrun`, "enter", "--box", boxPath(key), "--wd", croot, "--", "/bin/bash", "-l"])] };
+ },
+ execCommand: (_runtime, { key, home }) => boxshExec({ key, home, platform: "darwin" }),
+};
diff --git a/apps/server/src/box/backends/types.ts b/apps/server/src/box/backends/types.ts
index 2a52e8ae..cf61fbdc 100644
--- a/apps/server/src/box/backends/types.ts
+++ b/apps/server/src/box/backends/types.ts
@@ -68,16 +68,16 @@ export type Removed = { kind: string; name: string; reason: string; size?: strin
export type DriverOptions = { run?: RunCommand; env?: Env; home?: string; timeoutMs?: number; log?: BoxLog; onLine?: RunOptions["onLine"] };
export type BoxDriver = {
- kind: "container" | "bwrap" | "apple" | "wsl" | "wslc";
+ kind: "container" | "bwrap" | "apple" | "wsl" | "wslc" | "lima";
/** boxsh may fall back to `<runtime> exec` when boxd does not answer (container engines and wslc). */
execFallback: boolean;
- /** boxrun runs as root here (the WSL distro): a box on a macOS/Windows host gets its own build folders (required.ts). */
+ /** boxrun runs as root here (the WSL distro, the Lima VM): a box on a macOS/Windows host gets its own build folders (required.ts). */
shadows?: boolean;
/** false: the box has its own network; its servers reach this computer's 127.0.0.1 through boxd (ports.ts). */
forwardsPorts?: boolean;
imagePresent: (runtime: BoxRuntime, image: BoxImage, opts?: DriverOptions) => Promise<PresentImage | null>;
pull: (runtime: BoxRuntime, image: BoxImage, opts?: DriverOptions) => Promise<{ ok: true } | { ok: false; error: string }>;
- /** The detached download; null: the download runs in this process (Desktop: the WSL distro). */
+ /** The detached download; null: the download runs in this process (Desktop: the WSL distro, the Lima VM). */
pullCommand: (runtime: BoxRuntime, image: BoxImage, home: string) => { bin: string; args: string[] } | null;
exists: (runtime: BoxRuntime, key: string, opts?: DriverOptions) => Promise<boolean>;
open: (ctx: OpenContext) => Promise<Opened>;
diff --git a/apps/server/src/box/backends/wsl.ts b/apps/server/src/box/backends/wsl.ts
index 8f60acc8..970500e6 100644
--- a/apps/server/src/box/backends/wsl.ts
+++ b/apps/server/src/box/backends/wsl.ts
@@ -166,22 +166,49 @@ export function sha256File(file: string): Promise<string> {
});
}
+export type DownloadOptions = {
+ fetcher?: (input: string, init?: RequestInit) => Promise<Response>;
+ /** A download that sends nothing for this long fails (0: never; the CLI's download has the same option). */
+ stallMs?: number;
+ /** Bytes so far and the total (null when the server does not say), as they come. */
+ onProgress?: (done: number, total: number | null) => void;
+};
+
/** Download over https (redirects followed, through externalFetch: the app's proxy and CA settings) to `dest`, checked against `sha256`. */
-export async function download(url: string, dest: string, sha256: string | null, { fetcher = externalFetch }: { fetcher?: (input: string, init?: RequestInit) => Promise<Response> } = {}): Promise<string> {
+export async function download(url: string, dest: string, sha256: string | null, { fetcher = externalFetch, stallMs = 0, onProgress }: DownloadOptions = {}): Promise<string> {
if (!/^https:\/\//.test(url)) throw new Error("download: https only");
mkdirSync(path.dirname(dest), { recursive: true });
const tmp = `${dest}.${process.pid}.part`;
- const res = await fetcher(url, { headers: { "user-agent": "omnirush" }, redirect: "follow" });
- if (res.status !== 200 || !res.body) {
- await res.body?.cancel().catch(() => undefined);
- throw new Error(`download: HTTP ${res.status}`);
- }
- if (res.url && !res.url.startsWith("https://")) throw new Error("download: https only");
+ const controller = new AbortController();
+ let timer: ReturnType<typeof setTimeout> | null = null;
+ const arm = () => {
+ if (!stallMs) return;
+ if (timer) clearTimeout(timer);
+ timer = setTimeout(() => controller.abort(new Error(`download: no data for ${Math.round(stallMs / 1000)} s`)), stallMs);
+ };
+ arm();
try {
- await pipeline(Readable.fromWeb(res.body as unknown as NodeReadableStream<Uint8Array>), createWriteStream(tmp));
+ const res = await fetcher(url, { headers: { "user-agent": "omnirush" }, redirect: "follow", signal: controller.signal });
+ if (res.status !== 200 || !res.body) {
+ await res.body?.cancel().catch(() => undefined);
+ throw new Error(`download: HTTP ${res.status}`);
+ }
+ if (res.url && !res.url.startsWith("https://")) throw new Error("download: https only");
+ const total = Number(res.headers.get("content-length")) > 0 ? Number(res.headers.get("content-length")) : null;
+ let done = 0;
+ const source = Readable.fromWeb(res.body as unknown as NodeReadableStream<Uint8Array>);
+ source.on("data", (chunk: Buffer) => {
+ arm();
+ done += chunk.length;
+ try { onProgress?.(done, total); } catch { /* a listener's problem */ }
+ });
+ await pipeline(source, createWriteStream(tmp));
} catch (error) {
rmSync(tmp, { force: true });
- throw error;
+ const reason = controller.signal.aborted ? controller.signal.reason : null;
+ throw reason instanceof Error ? reason : error;
+ } finally {
+ if (timer) clearTimeout(timer);
}
const got = await sha256File(tmp);
if (sha256 && got !== sha256) { rmSync(tmp, { force: true }); throw new Error(`download: sha256 mismatch (${got.slice(0, 12)}…)`); }
diff --git a/apps/server/src/box/box-backends.test.ts b/apps/server/src/box/box-backends.test.ts
index 16c2386a..b65fcfb2 100644
--- a/apps/server/src/box/box-backends.test.ts
+++ b/apps/server/src/box/box-backends.test.ts
@@ -55,7 +55,7 @@ function helperBin(): string {
describe("box backends: ranking and overrides", () => {
test("the ranked order per OS", () => {
expect(ORDER.linux).toEqual(["bwrap", "podman", "docker"]);
- expect(ORDER.darwin).toEqual(["orbstack", "colima", "docker-desktop", "rancher", "podman", "docker"]);
+ expect(ORDER.darwin).toEqual(["lima", "orbstack", "colima", "docker-desktop", "rancher", "podman", "docker"]);
expect(OPT_IN.darwin).toEqual(["apple"]);
expect(ORDER.win32).toEqual(["wslc", "wsl", "docker-desktop", "podman", "docker", "wsl-engine"]);
for (const list of Object.values(ORDER)) for (const id of list) expect(BACKENDS.some((b) => b === id)).toBe(true);
@@ -270,11 +270,12 @@ describe("box backends: macOS", () => {
return fail("no");
};
const home = tmp();
- const found = await detectBackend({ boxHome: home, platform: "darwin", arch: "arm64", release: "24.0.0", home: "/Users/sam", env: { PATH: "/usr/local/bin" }, exists: set(docker), run, fresh: true, all: true });
+ // (the OmniRush VM off: these are the runtimes it falls back to)
+ const found = await detectBackend({ boxHome: home, platform: "darwin", arch: "arm64", release: "24.0.0", home: "/Users/sam", env: { PATH: "/usr/local/bin", OMNIRUSH_BOX_LIMA: "0" }, exists: set(docker), run, fresh: true, all: true });
expect(found).toMatchObject({ ok: true, backend: "colima", prefix: ["--context", "colima"] });
expect(found.alternatives?.find((a) => a.backend === "apple")?.reason).toMatch(/opt-in only/);
expect(found.alternatives?.some((a) => a.backend === "docker-desktop" && a.ok)).toBe(true);
- const none = await detectBackend({ boxHome: home, platform: "darwin", home: "/Users/sam", env: { PATH: "/usr/bin" }, exists: () => false, run, fresh: true });
+ const none = await detectBackend({ boxHome: home, platform: "darwin", home: "/Users/sam", env: { PATH: "/usr/bin", OMNIRUSH_BOX_LIMA: "0" }, exists: () => false, run, fresh: true });
expect(none).toMatchObject({ ok: false, hint: "colima" });
});
@@ -285,7 +286,7 @@ describe("box backends: macOS", () => {
if (bin === APPLE_BIN) return args[1] === "version" ? ok(appleVersions) : ok("apiserver is running");
return fail("Cannot connect to the Docker daemon");
};
- const base = { boxHome: tmp(), platform: "darwin" as const, arch: "arm64", release: "26.0.0", home: "/Users/sam", env: { PATH: "/usr/local/bin" }, exists: set(APPLE_BIN), run, fresh: true };
+ const base = { boxHome: tmp(), platform: "darwin" as const, arch: "arm64", release: "26.0.0", home: "/Users/sam", env: { PATH: "/usr/local/bin", OMNIRUSH_BOX_LIMA: "0" }, exists: set(APPLE_BIN), run, fresh: true };
const auto = await detectBackend(base);
expect(auto.ok).toBe(false);
expect(calls.some(([bin]) => bin === APPLE_BIN)).toBe(false);
diff --git a/apps/server/src/box/box-lima.test.ts b/apps/server/src/box/box-lima.test.ts
new file mode 100644
index 00000000..c85e1195
--- /dev/null
+++ b/apps/server/src/box/box-lima.test.ts
@@ -0,0 +1,462 @@
+// backends/lima.ts (ported from the CLI's src/box/backends/lima.js): OmniRush's own
+// Lima VM on macOS, against limactl's real output, mocked (no VM is started): the
+// probe and its place first in the macOS order, the VM's config, the owner lock
+// and the leases the one VM shared by the CLI and the desktop app needs, the idle
+// stop (the watcher run for real against a stub limactl), the retry after a VM
+// that went away (sleep), the contract, the setup's progress lines, and a box
+// start through the keeper.
+import { describe, expect, test } from "bun:test";
+import { execFileSync, spawn } from "node:child_process";
+import { createHash } from "node:crypto";
+import { EventEmitter } from "node:events";
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { PassThrough } from "node:stream";
+
+import { detectBackend, driverFor, normalizeBackend, runtimeBackend } from "./backends/index.js";
+import {
+ BASE_MOUNTS, HELPER_SCRIPT, LIMA_ARCH, PROVISION, VM_CONTRACT, contractFor, contractVerdict, ensureLimactl, ensureVm, fetchChecked, findLimactl, idleMain, idleStep, idleStopMinutes,
+ inVm, keepIdleWatch, limaAsset, limaDir, limaDriver, limaEnv, limaError, limaHome, liveLeases, loadDarwinSetup, lockVm, parseList, probeLima, setupLine, startKeeper, stopIfIdle,
+ syncHelpers, takeLease, uncovered, vmAway, vmCall, vmConfig, vmImage, vmMounts, vmResources, vmRow, type LimaRuntime,
+} from "./backends/lima.js";
+import type { SpawnLike } from "./backends/types.js";
+import { inProcessLog, pullLog, setupWait } from "./launch.js";
+import { parseSetupLines, pullProgress } from "./pull-progress.js";
+import type { RunCommand, RunResult } from "./run.js";
+
+const ok = (stdout: unknown = ""): RunResult => ({ code: 0, stdout: typeof stdout === "string" ? stdout : JSON.stringify(stdout), stderr: "" });
+const fail = (stderr: string, code = 1): RunResult => ({ code, stdout: "", stderr });
+const tmp = () => mkdtempSync(path.join(os.tmpdir(), "box-lima-"));
+const GiB = 1024 ** 3;
+const DEAD = 2 ** 22 + 12345;
+const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
+
+function fakeLimactl(dir: string): string {
+ const bin = path.join(dir, "limactl");
+ writeFileSync(bin, "#!/bin/sh\n", { mode: 0o755 });
+ return bin;
+}
+
+type VmState = { status: string | null; mounts?: string[]; wantMounts?: string[]; startFails?: string };
+type Call = { bin: string; args: string[]; env?: NodeJS.ProcessEnv; input?: string | Buffer };
+
+/** A limactl stub: `list` answers from `state` ({ status, mounts }), shell calls are recorded. */
+function limaRun(state: VmState, { onShell = () => ok(""), calls = [] as Call[] }: { onShell?: (argv: string[], opts: { input?: string | Buffer; onLine?: (s: "stdout" | "stderr", l: string) => void }) => RunResult; calls?: Call[] } = {}): RunCommand {
+ return async (bin, args, opts = {}) => {
+ calls.push({ bin, args, env: opts.env, input: opts.input });
+ if (args[0] === "--version") return ok("limactl version 2.2.1\n");
+ if (args[0] === "list") return state.status ? ok(`${JSON.stringify({ name: "omnirush-box", status: state.status, config: { mounts: (state.mounts ?? []).map((m) => ({ location: m, mountPoint: m, writable: true })) } })}\n`) : ok("");
+ if (args.includes("start")) { if (state.startFails) return fail(`time="x" level=fatal msg="${state.startFails}"`); state.status = "Running"; if (!state.mounts) state.mounts = state.wantMounts ?? []; return ok(""); }
+ if (args.includes("stop")) { state.status = "Stopped"; return ok(""); }
+ if (args.includes("edit")) { const set = args[args.indexOf("--set") + 1]!; state.mounts = JSON.parse(set.replace(/^\.mounts = /, "")).map((m: { location: string }) => m.location); return ok(""); }
+ if (args.includes("delete")) { state.status = null; return ok(""); }
+ if (args[0] === "shell") return onShell(args.slice(args.indexOf("-n") + 1), opts);
+ return fail("unexpected");
+ };
+}
+
+describe("lima: the probe and the order", () => {
+ test("macOS only, 13 or later, off by OMNIRUSH_BOX_LIMA=0; nothing downloaded yet is still the pick (it sets itself up)", async () => {
+ const home = tmp();
+ expect((await probeLima({ platform: "linux", home }) as { reason: string }).reason).toMatch(/for macOS/);
+ expect((await probeLima({ platform: "darwin", env: { OMNIRUSH_BOX_LIMA: "0" }, home }) as { reason: string }).reason).toMatch(/OMNIRUSH_BOX_LIMA=0/);
+ const old = await probeLima({ platform: "darwin", release: "21.6.0", env: {}, home, tools: null });
+ expect(old).toMatchObject({ ok: false, code: "old_macos" });
+ const fresh = await probeLima({ platform: "darwin", arch: "arm64", release: "25.0.0", env: {}, home, tools: null, run: async () => { throw new Error("nothing is run before limactl is here"); } });
+ expect(fresh).toMatchObject({ ok: true, backend: "lima", installed: false, bin: "", arch: "arm64", instance: "omnirush-box", home });
+ expect((await probeLima({ platform: "darwin", release: "25.0.0", env: { OMNIRUSH_BOX_LIMACTL: "/nope/limactl" }, home, tools: null }) as { reason: string }).reason).toMatch(/OMNIRUSH_BOX_LIMACTL/);
+ });
+
+ test("a limactl here reports the VM's state; a VM that failed to start is skipped for an hour unless chosen by name", async () => {
+ const home = tmp();
+ const bin = fakeLimactl(home);
+ const state: VmState = { status: "Stopped" };
+ const calls: Call[] = [];
+ const probed = await probeLima({ platform: "darwin", release: "25.0.0", env: { OMNIRUSH_BOX_LIMACTL: bin }, home, run: limaRun(state, { calls }) });
+ expect(probed).toMatchObject({ ok: true, installed: true, state: "Stopped", version: "2.2.1", bin });
+ expect(calls.find((c) => c.args[0] === "list")?.env?.LIMA_HOME).toBe(limaHome(home));
+ mkdirSync(limaDir(home), { recursive: true });
+ writeFileSync(path.join(limaDir(home), "failed.json"), JSON.stringify({ at: Date.now() - 60_000, reason: "vz: not supported" }));
+ expect(await probeLima({ platform: "darwin", release: "25.0.0", env: { OMNIRUSH_BOX_LIMACTL: bin }, home, run: limaRun(state) })).toMatchObject({ ok: false, reason: expect.stringMatching(/did not start \(vz: not supported\)/) });
+ expect((await probeLima({ platform: "darwin", release: "25.0.0", env: { OMNIRUSH_BOX_LIMACTL: bin }, home, run: limaRun(state), explicit: true })).ok).toBe(true);
+ expect((await probeLima({ platform: "darwin", release: "25.0.0", env: { OMNIRUSH_BOX_LIMACTL: bin }, home, run: limaRun(state), now: Date.now() + 2 * 3_600_000 })).ok).toBe(true);
+ });
+
+ test("macOS order: the OmniRush VM first, even with OrbStack running; a running runtime when the VM cannot run", async () => {
+ const docker = "/usr/local/bin/docker";
+ const run: RunCommand = async (_bin, args) => {
+ if (args[0] === "context") return ok(JSON.stringify({ Name: "orbstack", DockerEndpoint: "unix:///x/orb.sock" }));
+ if (args[0] === "--context") return ok({ OperatingSystem: "OrbStack", Name: "orbstack", ServerVersion: "27.0", OSType: "linux", Architecture: "aarch64" });
+ return fail("no");
+ };
+ const base = { platform: "darwin" as const, arch: "arm64", release: "25.0.0", home: "/Users/sam", exists: (f: string) => f === docker, run, fresh: true };
+ const vm = await detectBackend({ ...base, boxHome: tmp(), env: { PATH: "/usr/local/bin" }, all: true });
+ expect(vm.ok && vm.backend).toBe("lima");
+ expect(vm.alternatives?.some((a) => a.backend === "orbstack" && a.ok)).toBe(true);
+ const old = await detectBackend({ ...base, boxHome: tmp(), release: "21.0.0", env: { PATH: "/usr/local/bin" } });
+ expect(old.ok && old.backend).toBe("orbstack");
+ expect(normalizeBackend("omnirush-vm")).toBe("lima");
+ expect(runtimeBackend("/Applications/omnirush.app/Contents/Resources/sidecars/lima/bin/limactl")).toBe("lima");
+ expect(driverFor({ backend: "lima" })).toBe(limaDriver);
+ expect(limaDriver.shadows).toBe(true);
+ expect(limaDriver.forwardsPorts).toBe(true);
+ expect(limaDriver.pullCommand({ name: "lima", bin: "" }, { ref: "x", pull: "x", digest: null, stack: "base" } as never, "/h")).toBeNull();
+ });
+});
+
+describe("lima: pins, paths, config (the same as the CLI's)", () => {
+ test("Lima per arch by sha256; the VM image falls back to the official Debian one by sha512 until ours is published", () => {
+ const setup = loadDarwinSetup();
+ expect(setup.backend).toBe("lima");
+ expect(limaAsset(setup, "arm64")?.url).toMatch(/lima-2\.2\.1-Darwin-arm64\.tar\.gz$/);
+ expect(limaAsset(setup, "x64")?.url).toMatch(/Darwin-x86_64/);
+ const home = "/Users/sam/.omnirush/box";
+ const image = vmImage({ setup, arch: "arm64", home, env: {} });
+ expect(image).toMatchObject({ kind: "debian", algo: "sha512" });
+ expect(image?.url).toMatch(/genericcloud-arm64-.*\.qcow2$/);
+ expect(path.dirname(String(image?.file))).toBe(path.join(home, "lima", "images"));
+ expect(vmImage({ env: { OMNIRUSH_BOX_LIMA_IMAGE: "/tmp/vm.qcow2" } })?.file).toBe("/tmp/vm.qcow2");
+ });
+
+ test("limactl: the env's, else the one the app ships (sidecars/lima), else the CLI's or an earlier download in the shared box home", () => {
+ const setup = loadDarwinSetup();
+ const home = "/Users/sam/.omnirush/box";
+ expect(findLimactl({ env: {}, home, exists: (f) => f === "/App/Resources/sidecars/lima/bin/limactl", setup, tools: "/App/Resources/sidecars" })).toEqual({ bin: "/App/Resources/sidecars/lima/bin/limactl", source: "bundled" });
+ expect(findLimactl({ env: {}, home, exists: (f) => f === path.join(home, "lima", "dist", "2.2.1", "bin", "limactl"), setup, tools: null }).source).toBe("download");
+ expect(findLimactl({ env: { OMNIRUSH_BOX_LIMACTL: "/x/limactl" }, home, exists: () => true, setup, tools: "/T" })).toEqual({ bin: "/x/limactl", source: "env" });
+ });
+
+ test("LIMA_HOME: <box home>/vm, ~/.omnirush-vm when that is too long for macOS' 104-byte sockets (the CLI's rule: one VM for both)", () => {
+ expect(limaHome("/Users/sam/.omnirush/box", {}, "/Users/sam")).toBe("/Users/sam/.omnirush/box/vm");
+ const deep = "/Users/sam/some/very/long/folder/for/the/box/home/.omnirush/box";
+ expect(limaHome(deep, {}, "/Users/sam")).toBe("/Users/sam/.omnirush-vm");
+ expect(limaHome(deep, { OMNIRUSH_BOX_LIMA_HOME: "/v" }, "/Users/sam")).toBe("/v");
+ const { LIMA_HOME, LIMA_INSTANCE } = limaEnv("/h", { LIMA_INSTANCE: "default", LIMA_HOME: "/elsewhere" });
+ expect({ LIMA_HOME, LIMA_INSTANCE }).toEqual({ LIMA_HOME: "/h/vm", LIMA_INSTANCE: undefined });
+ });
+
+ test("VM size and config: 3 GiB floor, vz, virtiofs, the home + /Volumes + /private/tmp writable at their paths", () => {
+ const setup = loadDarwinSetup();
+ expect(vmResources({ setup, cpus: 8, memBytes: 8 * GiB, env: {} })).toEqual({ cpus: 4, memory: 3, disk: 30 });
+ expect(vmResources({ setup, cpus: 10, memBytes: 24 * GiB, env: {} })).toEqual({ cpus: 4, memory: 6, disk: 30 });
+ expect(idleStopMinutes({ setup, env: {} })).toBe(30);
+ const mounts = vmMounts({ userHome: "/Users/sam", extra: ["/Users/sam/x", "/opt/work/p"] });
+ expect(mounts).toEqual(["/Users/sam", ...BASE_MOUNTS, "/opt/work/p"]);
+ const config = vmConfig({ image: "/i.qcow2", arch: "arm64", cpus: 2, memory: 3, disk: 30, mounts }) as { vmType: string; mountType: string; arch: string; mounts: Array<{ writable: boolean; mountPoint: string; location: string }>; provision: Array<{ script: string }>; containerd: unknown };
+ expect([config.vmType, config.mountType, config.arch]).toEqual(["vz", "virtiofs", LIMA_ARCH.arm64!]);
+ expect(config.mounts.every((m) => m.writable && m.mountPoint === m.location)).toBe(true);
+ expect(config.containerd).toEqual({ system: false, user: false });
+ expect(config.provision[0]?.script).toBe(PROVISION);
+ expect(uncovered(["/Users/sam/p", "/private/tmp/q", "/opt/a/b", "/opt/a/b/c", "rel"], ["/Users/sam", ...BASE_MOUNTS])).toEqual(["/opt/a/b"]);
+ });
+
+ test("the scripts the VM runs are the CLI's text (a VM made by either app is the same VM; sha256 pinned here, checked against the CLI's in the evidence)", () => {
+ const sha = (text: string) => createHash("sha256").update(text).digest("hex").slice(0, 16);
+ expect({ provision: sha(PROVISION), helpers: sha(HELPER_SCRIPT) }).toEqual({ provision: "113559f9703cd87e", helpers: "4a5ed8fc2e5f40b1" });
+ });
+});
+
+describe("lima: one VM for the CLI and the desktop", () => {
+ test("owner lock: one holder; a dead holder's lock is taken over; a live one makes the next wait, then give up", async () => {
+ const home = tmp();
+ const release = await lockVm(home, { waitMs: 0 });
+ expect(release).toBeTruthy();
+ const t = { now: 0 };
+ expect(await lockVm(home, { waitMs: 1_000, sleep: async (ms) => { t.now += ms; }, now: () => t.now })).toBeNull();
+ release!();
+ mkdirSync(limaDir(home), { recursive: true });
+ writeFileSync(path.join(limaDir(home), "vm.lock"), JSON.stringify({ pid: DEAD, nonce: "x", at: 1 }));
+ const taken = await lockVm(home, { waitMs: 0, pidAlive: (pid) => pid !== DEAD });
+ expect(taken).toBeTruthy();
+ taken!();
+ expect(existsSync(path.join(limaDir(home), "vm.lock"))).toBe(false);
+ });
+
+ test("leases: the CLI's file format (pid, key, client, at); a gone process' lease is dropped", () => {
+ const home = tmp();
+ const release = takeLease(home, "k1");
+ takeLease(home, "k2", { pid: DEAD, client: "cli" });
+ const file = readdirSync(path.join(limaDir(home), "leases")).find((n) => n.startsWith("k1-"))!;
+ expect(file).toMatch(new RegExp(`^k1-${process.pid}-\\d+\\.json$`));
+ expect(Object.keys(JSON.parse(readFileSync(path.join(limaDir(home), "leases", file), "utf8"))).sort()).toEqual(["at", "client", "key", "pid"]);
+ const live = liveLeases(home, { pidAlive: (pid) => pid !== DEAD });
+ expect(live.map((l) => [l.key, l.client])).toEqual([["k1", "desktop"]]);
+ expect(readdirSync(path.join(limaDir(home), "leases")).length).toBe(1);
+ release();
+ expect(liveLeases(home)).toEqual([]);
+ });
+
+ test("idle stop: busy while a lease lives; stopped after the idle time with none, under the lock; done when the VM is stopped", async () => {
+ const home = tmp();
+ const bin = fakeLimactl(home);
+ const state: VmState = { status: "Running" };
+ const runtime = { bin, home, instance: "omnirush-box" };
+ const release = takeLease(home, "k");
+ expect(await idleStep(runtime, { busyAt: 0, now: 10_000_000, idleMs: 60_000, run: limaRun(state) })).toEqual({ busyAt: 10_000_000 });
+ release();
+ expect(await idleStep(runtime, { busyAt: 10_000_000, now: 10_030_000, idleMs: 60_000, run: limaRun(state) })).toEqual({ busyAt: 10_000_000 });
+ expect((await idleStep(runtime, { busyAt: 10_000_000, now: 10_060_000, idleMs: 60_000, run: limaRun(state) })).stop).toBe(true);
+ const again = takeLease(home, "k");
+ expect(await stopIfIdle(runtime, { run: limaRun(state) })).toEqual({ stopped: false, reason: "busy" });
+ again();
+ expect(await stopIfIdle(runtime, { run: limaRun(state) })).toEqual({ stopped: true });
+ expect(state.status).toBe("Stopped");
+ expect((await idleStep(runtime, { busyAt: 0, now: 1, idleMs: 60_000, run: limaRun(state) })).done).toBe(true);
+ });
+
+ test("the idle watcher for real: started once (idle.json), stops a VM with no lease after the idle time and ends", async () => {
+ const home = tmp();
+ const log = path.join(home, "limactl.log");
+ const bin = path.join(home, "limactl");
+ // A limactl that says the VM runs until it is stopped.
+ writeFileSync(bin, `#!/bin/sh\necho "$@" >> ${log}\ncase "$*" in\n *stop*) touch ${home}/stopped; exit 0;;\n list*) [ -f ${home}/stopped ] && echo '{"name":"omnirush-box","status":"Stopped"}' || echo '{"name":"omnirush-box","status":"Running"}'; exit 0;;\nesac\nexit 0\n`, { mode: 0o755 });
+ const runtime = { home, bin, instance: "omnirush-box" };
+ const script = idleMain();
+ expect(script).toMatch(/lima-idle\.(ts|js)$/);
+ const env = { ...process.env, OMNIRUSH_BOX_LIMA_IDLE_MIN: "0.02" };
+ const pid = keepIdleWatch(runtime, { env, spawnImpl: spawn as unknown as SpawnLike });
+ expect(pid).toBeGreaterThan(0);
+ expect(JSON.parse(readFileSync(path.join(limaDir(home), "idle.json"), "utf8")).pid).toBe(pid);
+ expect(keepIdleWatch(runtime, { env, spawnImpl: () => { throw new Error("a second watcher"); } })).toBe(pid);
+ for (let i = 0; i < 100 && !existsSync(path.join(home, "stopped")); i += 1) await sleep(100);
+ expect(readFileSync(log, "utf8")).toMatch(/--tty=false stop omnirush-box/);
+ expect(existsSync(path.join(limaDir(home), "vm.lock"))).toBe(false);
+ }, 20_000);
+
+ test("contract: client desktop; same is used, an older VM is kept (never replaced under a box), a newer client's VM is used as it is", () => {
+ const c = contractFor({ kind: "debian", version: "v", digest: "ab", algo: "sha512" }, { clientVersion: "3.8.0", lima: "2.2.1" });
+ expect(c).toMatchObject({ contract: VM_CONTRACT, client: "desktop", client_version: "3.8.0", lima: "2.2.1", image: { kind: "debian", version: "v", digest: "sha512:ab" } });
+ expect(Object.keys(c).sort()).toEqual(["client", "client_version", "contract", "created_at", "image", "lima"]);
+ expect(contractVerdict({ contract: VM_CONTRACT })).toBe("use");
+ expect(contractVerdict(null)).toBe("use");
+ expect(contractVerdict({ contract: VM_CONTRACT - 1 })).toBe("replace");
+ expect(contractVerdict({ contract: VM_CONTRACT + 1 })).toBe("newer");
+ });
+});
+
+describe("lima: limactl calls", () => {
+ test("commands in the VM: root, no shell in between; a VM that went away (sleep) is started and the call tried once more", async () => {
+ const runtime: LimaRuntime = { bin: "/x/limactl", home: "/h", instance: "omnirush-box" };
+ expect(inVm(runtime, ["/opt/omnirush/boxrun", "status"])).toEqual(["shell", "--workdir", "/", "omnirush-box", "sudo", "-n", "/opt/omnirush/boxrun", "status"]);
+ expect(vmAway(fail("ssh: connect to host 127.0.0.1 port 50022: Connection refused", 255))).toBe(true);
+ expect(vmAway(fail("boxrun: image x is not unpacked"))).toBe(false);
+ let n = 0;
+ let ensured = 0;
+ const res = await vmCall(runtime, ["true"], { run: async () => (++n === 1 ? fail("kex_exchange_identification: read: Connection reset by peer", 255) : ok("fine")), ensure: async () => { ensured += 1; return { ok: true }; } });
+ expect({ code: res.code, n, ensured }).toEqual({ code: 0, n: 2, ensured: 1 });
+ expect(limaError(fail('time="2026" level=fatal msg="failed to start: \\"vz\\" requires macOS 13"'))).toBe('failed to start: "vz" requires macOS 13');
+ const gone = await vmRow(runtime, { run: async () => fail('level=warning msg="No instance matching omnirush-box found."\nlevel=fatal msg="unmatched instances"') });
+ expect({ row: gone.row, missing: gone.missing }).toEqual({ row: null, missing: true });
+ expect(parseList(`${JSON.stringify({ name: "other" })}\n${JSON.stringify({ name: "omnirush-box", status: "Running" })}\n`, "omnirush-box")?.status).toBe("Running");
+ });
+
+ test("downloads: checked by digest (sha256 or sha512); a stall is tried again; byte progress is passed on", async () => {
+ const dir = tmp();
+ const body = Buffer.from("vm image");
+ const sha512 = createHash("sha512").update(body).digest("hex");
+ let fetched = 0;
+ const seen: Array<[number, number | null]> = [];
+ const fetchFile = async (_url: string, dest: string, _sha: string | null, opts?: { onProgress?: (d: number, t: number | null) => void }) => { fetched += 1; opts?.onProgress?.(body.length, body.length); writeFileSync(dest, body); return dest; };
+ const dest = path.join(dir, "img.qcow2");
+ await fetchChecked("https://x/img", dest, { algo: "sha512", digest: sha512, fetchFile, onProgress: (d, t) => seen.push([d, t]) });
+ await fetchChecked("https://x/img", dest, { algo: "sha512", digest: sha512, fetchFile });
+ expect(fetched).toBe(1);
+ expect(seen).toEqual([[8, 8]]);
+ await expect(fetchChecked("https://x/img", path.join(dir, "bad"), { algo: "sha512", digest: "0".repeat(128), fetchFile })).rejects.toThrow(/sha512 mismatch/);
+ let calls = 0;
+ const flaky = async (_url: string, d: string) => { calls += 1; if (calls < 3) throw new Error("download: no data for 90 s"); writeFileSync(d, body); return d; };
+ await fetchChecked("https://x/img", path.join(dir, "flaky"), { algo: "sha512", digest: sha512, fetchFile: flaky });
+ expect(calls).toBe(3);
+ });
+
+ test("Lima itself: the pinned tarball, sha256-checked, unpacked to the shared box home (the CLI's place) with limactl and the Linux guest agent only", async () => {
+ const home = tmp();
+ const src = tmp();
+ for (const f of ["bin/limactl", "bin/docker.lima", "libexec/lima/lima-driver-qemu", "share/lima/lima-guestagent.Linux-aarch64.gz", "share/lima/lima-guestagent.Darwin-aarch64.gz", "share/lima/templates/default.yaml"]) {
+ mkdirSync(path.join(src, path.dirname(f)), { recursive: true });
+ writeFileSync(path.join(src, f), f);
+ }
+ const tarball = path.join(tmp(), "lima.tar.gz");
+ execFileSync("tar", ["-czf", tarball, "-C", src, "bin", "libexec", "share"]);
+ const sha256 = createHash("sha256").update(readFileSync(tarball)).digest("hex");
+ const setup = { lima: { version: "2.2.1", assets: { arm64: { url: "https://x/lima.tar.gz", sha256 } } } };
+ const runtime: LimaRuntime = { bin: "", home, instance: "omnirush-box" };
+ const run: RunCommand = async (_bin, args) => { try { execFileSync("tar", args); return ok(""); } catch (e) { return fail(String(e)); } };
+ const lines: string[] = [];
+ const got = await ensureLimactl(runtime, { arch: "arm64", setup, run, fetchFile: async (_url, dest, _s, opts) => { opts?.onProgress?.(10, 20); writeFileSync(dest, readFileSync(tarball)); return dest; }, onLine: (_s, l) => lines.push(l) });
+ expect(got.ok).toBe(true);
+ const dist = path.join(limaDir(home), "dist", "2.2.1");
+ expect(runtime.bin).toBe(path.join(dist, "bin", "limactl"));
+ expect(readdirSync(path.join(dist, "bin"))).toEqual(["limactl"]);
+ expect(readdirSync(path.join(dist, "share", "lima")).sort()).toEqual(["lima-guestagent.Linux-aarch64.gz", "templates"]);
+ expect(lines).toEqual([setupLine("lima"), setupLine("lima", 10, 20)]);
+ });
+});
+
+function vmFixture({ status = null, mounts }: { status?: string | null; mounts?: string[] } = {}) {
+ const home = tmp();
+ const bin = fakeLimactl(home);
+ const image = path.join(home, "vm.qcow2");
+ writeFileSync(image, "qcow2");
+ const env = { OMNIRUSH_BOX_LIMA_IMAGE: image, PATH: "/usr/bin" };
+ const state: VmState = { status, ...(mounts ? { mounts } : {}), wantMounts: ["/Users/sam", ...BASE_MOUNTS] };
+ const shells: Array<{ argv: string[]; input?: string | Buffer | undefined }> = [];
+ const calls: Call[] = [];
+ const run = limaRun(state, { calls, onShell: (argv, opts) => { shells.push({ argv, input: opts.input }); return argv[0] === "cat" ? ok(JSON.stringify({ contract: VM_CONTRACT })) : ok(""); } });
+ const spawned: string[][] = [];
+ const spawnImpl = ((_bin: string, args: string[]) => { spawned.push(args); return { pid: DEAD, unref() {} }; }) as unknown as SpawnLike;
+ return { home, bin, env, state, shells, calls, run, spawnImpl, spawned, runtime: { bin, home, instance: "omnirush-box", arch: "arm64" } as LimaRuntime };
+}
+
+describe("lima: the VM's life", () => {
+ test("first use: the VM made from the config file, its contract written (client desktop), the idle watcher started, setup lines said", async () => {
+ const f = vmFixture();
+ const lines: string[] = [];
+ const res = await ensureVm(f.runtime, { env: f.env, run: f.run, paths: ["/Users/sam/proj"], userHome: "/Users/sam", spawnImpl: f.spawnImpl, resources: { cpus: 2, memory: 3, disk: 30 }, clientVersion: "3.8.0", onLine: (_s, l) => lines.push(l) });
+ expect(res.ok).toBe(true);
+ const start = f.calls.find((c) => c.args.includes("start"))!;
+ expect(start.args.slice(0, 4)).toEqual(["--tty=false", "start", "--name", "omnirush-box"]);
+ const config = JSON.parse(readFileSync(start.args.at(-1)!, "utf8").replace(/^#.*\n/, ""));
+ expect(config.vmType).toBe("vz");
+ expect(config.images[0].location).toBe(f.env.OMNIRUSH_BOX_LIMA_IMAGE);
+ const contract = f.shells.find((s) => /vm\.json/.test(s.argv.join(" ")) && s.input);
+ expect(JSON.parse(String(contract?.input))).toMatchObject({ contract: VM_CONTRACT, client: "desktop", client_version: "3.8.0", image: { kind: "local" } });
+ expect(f.spawned.some((args) => args.some((a) => /lima-idle\.(ts|js)$/.test(a)))).toBe(true);
+ expect(existsSync(path.join(limaDir(f.home), "vm.lock"))).toBe(false);
+ expect(lines).toEqual([setupLine("vm_boot")]);
+ f.calls.length = 0;
+ const again = await ensureVm(f.runtime, { env: f.env, run: f.run, paths: ["/Users/sam/proj"], userHome: "/Users/sam", spawnImpl: f.spawnImpl });
+ expect(again).toMatchObject({ ok: true, started: false });
+ expect(f.calls.some((c) => c.args.includes("start"))).toBe(false);
+ });
+
+ test("a stopped VM is started; one that cannot start says why and is not picked again for a while", async () => {
+ const f = vmFixture({ status: "Stopped", mounts: ["/Users/sam", ...BASE_MOUNTS] });
+ expect((await ensureVm(f.runtime, { env: f.env, run: f.run, paths: ["/Users/sam/p"], spawnImpl: f.spawnImpl })).ok).toBe(true);
+ expect(f.state.status).toBe("Running");
+ const g = vmFixture({ status: "Stopped", mounts: ["/Users/sam"] });
+ g.state.startFails = "vz: Virtualization is not available on this hardware";
+ const res = await ensureVm(g.runtime, { env: g.env, run: g.run, paths: ["/Users/sam/p"], spawnImpl: g.spawnImpl });
+ expect(res.ok).toBe(false);
+ expect(readFileSync(path.join(limaDir(g.home), "failed.json"), "utf8")).toMatch(/Virtualization/);
+ });
+
+ test("a project outside the shared folders: added with one VM restart, never while another app's box runs", async () => {
+ const f = vmFixture({ status: "Running", mounts: ["/Users/sam", ...BASE_MOUNTS] });
+ const lease = takeLease(f.home, "other", { pid: process.ppid || 1, client: "cli" });
+ const busy = await ensureVm(f.runtime, { env: f.env, run: f.run, paths: ["/opt/work/p"], spawnImpl: f.spawnImpl });
+ expect(busy).toMatchObject({ ok: false, error: expect.stringMatching(/outside the folders the sandbox VM shares/) });
+ expect(f.state.status).toBe("Running");
+ lease();
+ expect((await ensureVm(f.runtime, { env: f.env, run: f.run, paths: ["/opt/work/p"], spawnImpl: f.spawnImpl })).ok).toBe(true);
+ expect(f.state.mounts).toEqual(["/Users/sam", ...BASE_MOUNTS, "/opt/work/p"]);
+ });
+
+ test("helpers: this install's boxrun/boxd in its own folder in the VM (the CLI's never swapped), staged via the box home", async () => {
+ const f = vmFixture({ status: "Running", mounts: ["/Users/sam"] });
+ const bins = tmp();
+ mkdirSync(path.join(bins, "linux-arm64"), { recursive: true });
+ for (const n of ["boxrun", "boxd"]) writeFileSync(path.join(bins, "linux-arm64", n), n);
+ const res = await syncHelpers(f.runtime, { env: { OMNIRUSH_BOX_BIN_DIR: bins }, run: f.run });
+ expect(res.ok && res.dir).toMatch(/^\/opt\/omnirush\/h\/[0-9a-f]{16}$/);
+ const call = f.shells.at(-1)!.argv;
+ expect(call.slice(0, 3)).toEqual(["/bin/sh", "-c", HELPER_SCRIPT]);
+ expect(readFileSync(path.join(call.at(-1)!, "boxd"), "utf8")).toBe("boxd");
+ });
+});
+
+function fakeChild(line: string | null) {
+ const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough; stdin: PassThrough; exitCode: number | null; kill: () => void; unref: () => void };
+ child.stdout = new PassThrough();
+ child.stderr = new PassThrough();
+ child.stdin = new PassThrough();
+ child.exitCode = null;
+ child.kill = () => { child.exitCode = 0; setImmediate(() => child.emit("close", 0)); };
+ child.unref = () => {};
+ if (line) setTimeout(() => child.stdout.write(`${line}\n`), 5);
+ return child;
+}
+
+describe("lima: boxes", () => {
+ test("keeper: `limactl shell ... sudo boxrun up --foreground`, the first JSON line gives the port; a keeper that lost the VM says so", async () => {
+ let seen: { args: string[]; env?: NodeJS.ProcessEnv } | null = null;
+ const kept = await startKeeper({ bin: "/x/limactl", home: "/h", instance: "omnirush-box", helpers: "/opt/omnirush/h/abc" }, ["up"], {
+ spawnImpl: ((_bin: string, args: string[], opts: { env?: NodeJS.ProcessEnv }) => { seen = { args, env: opts.env }; return fakeChild('{"ok":true,"reused":false,"port":40123}'); }) as unknown as SpawnLike, timeoutMs: 2_000,
+ });
+ expect(kept.ok && kept.out.port).toBe(40123);
+ expect(seen!.args.slice(0, 7)).toEqual(["shell", "--workdir", "/", "omnirush-box", "sudo", "-n", "/opt/omnirush/h/abc/boxrun"]);
+ expect(seen!.env?.LIMA_HOME).toBe("/h/vm");
+ const lost = await startKeeper({ bin: "/x/limactl", home: "/h", instance: "omnirush-box" }, ["up"], {
+ spawnImpl: (() => { const c = fakeChild(null); setTimeout(() => { c.stderr.write("ssh: connect to host 127.0.0.1 port 5022: Connection refused\n"); c.emit("close", 255); }, 5); return c; }) as unknown as SpawnLike, timeoutMs: 2_000,
+ });
+ expect(lost).toMatchObject({ ok: false, away: true });
+ });
+
+ test("a box start: the VM ensured, the box up on a loopback port free on this Mac, a lease (client desktop) while the keeper lives", async () => {
+ const f = vmFixture({ status: "Running", mounts: ["/Users/sam", ...BASE_MOUNTS] });
+ const bins = tmp();
+ mkdirSync(path.join(bins, "linux-arm64"), { recursive: true });
+ for (const n of ["boxrun", "boxd"]) writeFileSync(path.join(bins, "linux-arm64", n), n);
+ const root = "/Users/sam/proj";
+ const runFolder = path.join(f.home, "projects", "k");
+ mkdirSync(runFolder, { recursive: true });
+ const image = { ref: "omnirush/box-base:1", pull: `ghcr.io/x/box-base@sha256:${"b".repeat(64)}`, digest: `sha256:${"b".repeat(64)}`, stack: "base" };
+ const run = limaRun(f.state, { calls: f.calls, onShell: (argv) => (argv.includes("image") ? ok('{"present":true,"id":"img1","digest":"sha256:bb","arch":"arm64","config":"cfg"}') : ok("")) });
+ let child: ReturnType<typeof fakeChild> | null = null;
+ let up: string[] = [];
+ const opened = await limaDriver.open({
+ key: "k", name: "omnirush-box-k", root, croot: root, runtime: { ...f.runtime, name: "lima", backend: "lima" }, run, home: f.home, runFolder, image: image as never, pull: false, shims: null, pkglog: null,
+ env: { ...f.env, OMNIRUSH_BOX_BIN_DIR: bins }, platform: "darwin", log: () => {}, onPulling: () => {},
+ shadows: { binds: [{ id: "0123456789ab", path: "node_modules", target: `${root}/node_modules` }], env: {} } as never,
+ spawnImpl: ((_bin: string, args: string[]) => { up = args; child = fakeChild('{"ok":true,"reused":false,"port":41999}'); return child; }) as unknown as SpawnLike,
+ idleSpawn: f.spawnImpl, freePort: async () => 41999,
+ } as never);
+ expect(opened.ok).toBe(true);
+ if (!opened.ok) return;
+ expect(opened.endpoint.port).toBe(41999);
+ expect(up).toContain("BOXD_LISTEN=127.0.0.1:41999");
+ expect(up).toContain(`${root}=${root}`);
+ expect(up[up.indexOf("--shadow-store") + 1]).toBe("/var/lib/omnirush/shadows/k");
+ expect(liveLeases(f.home).map((l) => [l.key, l.client])).toEqual([["k", "desktop"]]);
+ expect((await limaDriver.list({ ...f.runtime, name: "lima" }, { home: f.home }))[0]?.running).toBe(true);
+ child!.kill();
+ await sleep(20);
+ expect(liveLeases(f.home)).toEqual([]);
+ });
+
+ test("sandbox shell: limactl with the box home's LIMA_HOME, root in the box at the project", () => {
+ const cmd = limaDriver.shellCommand({ name: "lima", bin: "/x/limactl", home: "/h", instance: "omnirush-box" }, { key: "k", croot: "/Users/sam/p", home: "/h" });
+ expect(cmd.bin).toBe("/usr/bin/env");
+ expect(cmd.args.slice(0, 3)).toEqual(["LIMA_HOME=/h/vm", "/x/limactl", "shell"]);
+ expect(cmd.args.slice(-8)).toEqual(["enter", "--box", "/var/lib/omnirush/boxes/k", "--wd", "/Users/sam/p", "--", "/bin/bash", "-l"]);
+ });
+});
+
+describe("lima: the first setup's progress and the wait for it", () => {
+ test("setup lines: the step the VM setup is at, with that step's bytes; the image's own bytes after it", () => {
+ const text = [setupLine("lima"), setupLine("lima", 1_000, 38_000_000), setupLine("vm_image"), setupLine("vm_image", 84_000_000, 336_000_000)].join("\n");
+ expect(parseSetupLines(text)).toEqual({ step: "vm_image", bytesDone: 84_000_000, bytesTotal: 336_000_000 });
+ expect(parseSetupLines(`${text}\n${setupLine("vm_boot")}`)).toEqual({ step: "vm_boot", bytesDone: null, bytesTotal: null });
+ expect(parseSetupLines('{"progress":{"bytes":1,"total":2}}')).toBeNull();
+ const dir = tmp();
+ const marker = path.join(dir, "pulls", "lima-x.pid");
+ const progress = inProcessLog(marker);
+ progress.onLine("stdout", setupLine("vm_image", 10, 100));
+ expect(pullProgress(pullLog(marker), marker, null)).toMatchObject({ bytesDone: null, bytesTotal: null, setup: { step: "vm_image", bytesDone: 10, bytesTotal: 100 } });
+ progress.onLine("stdout", setupLine("image"));
+ progress.onLine("stderr", '{"progress":{"bytes":50,"total":200}}');
+ expect(pullProgress(pullLog(marker), marker, null)).toMatchObject({ bytesDone: 50, bytesTotal: 200, setup: { step: "image" } });
+ progress.close();
+ expect(existsSync(marker)).toBe(false);
+ rmSync(dir, { recursive: true, force: true });
+ });
+
+ test("the required wait goes on while the setup moves, stops after the no-progress time or the cap", () => {
+ const base = { startedAt: 0, idleMs: 15 * 60_000, maxMs: 60 * 60_000 };
+ expect(setupWait({ ...base, lastProgressAt: 0, now: 14 * 60_000 }).over).toBeNull();
+ expect(setupWait({ ...base, lastProgressAt: 0, now: 15 * 60_000 }).over).toBe("idle");
+ expect(setupWait({ ...base, lastProgressAt: 40 * 60_000, now: 50 * 60_000 }).over).toBeNull();
+ expect(setupWait({ ...base, lastProgressAt: 59 * 60_000, now: 60 * 60_000 }).over).toBe("max");
+ });
+});
diff --git a/apps/server/src/box/common.ts b/apps/server/src/box/common.ts
index 9113eb3a..c125d17b 100644
--- a/apps/server/src/box/common.ts
+++ b/apps/server/src/box/common.ts
@@ -125,6 +125,11 @@ export function setToolsRoot(dir: string | null | undefined): void {
platformTools = dir || null;
}
+/** The sidecars folder set at start (null: none; tests, a dev server). */
+export function toolsRoot(): string | null {
+ return platformTools;
+}
+
/** The helper's file, or null when no folder has it (the box is then unavailable). */
export function helperPath(name: string, { goos, goarch, env = process.env, toolsRoot = platformTools }: { goos: string | undefined; goarch: string | undefined; env?: Env; toolsRoot?: string | null }): string | null {
if (!goos || !goarch) return null;
diff --git a/apps/server/src/box/launch.ts b/apps/server/src/box/launch.ts
index 0b4798b7..7da34c65 100644
--- a/apps/server/src/box/launch.ts
+++ b/apps/server/src/box/launch.ts
@@ -18,7 +18,7 @@
// background, and a shell call waits for it (boxsh).
import { spawn, type ChildProcess } from "node:child_process";
-import { closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
+import { closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from "node:fs";
import path from "node:path";
import * as client from "./client.js";
@@ -205,6 +205,23 @@ export function pullLog(marker: string): string {
return marker.replace(/\.pid$/, ".log");
}
+/** When a download's log last moved (ms since the epoch), or null: no log. */
+export function pullActivityAt(home: string, runtime: Pick<BoxRuntime, "backend" | "name">, image: string): number | null {
+ try { return Math.round(statSync(pullLog(pullMarker(home, runtime, image))).mtimeMs); } catch { return null; }
+}
+
+/**
+ * Whether a wait for the first sandbox setup is over: `idle` (no progress
+ * for `idleMs`: nothing moved in the download's log) or `max` (`maxMs` in all);
+ * null while it goes on. The CLI's run limit counts the setup the same way
+ * (src/box/setup-wait.js).
+ */
+export function setupWait({ startedAt, lastProgressAt, now, idleMs, maxMs }: { startedAt: number; lastProgressAt: number; now: number; idleMs: number; maxMs: number }): { over: "idle" | "max" | null } {
+ if (now - startedAt >= maxMs) return { over: "max" };
+ if (now - Math.max(startedAt, lastProgressAt) >= idleMs) return { over: "idle" };
+ return { over: null };
+}
+
/** Downloads running in this process (a backend with no detached command: the WSL distro). */
const inProcess = new Set<string>();
@@ -222,6 +239,31 @@ export function pulling(home: string, runtime: Pick<BoxRuntime, "backend" | "nam
}
}
+/**
+ * The log of a download in this process (the WSL distro, the Lima VM): its
+ * lines appended as they come, for the setup panel's progress; the marker
+ * (this process's pid) is touched so the start time reads right.
+ */
+export function inProcessLog(marker: string): { onLine: (stream: "stdout" | "stderr", line: string) => void; close: () => void } {
+ let fd: number | null = null;
+ try {
+ mkdirSync(path.dirname(marker), { recursive: true });
+ writeFileSync(marker, String(process.pid));
+ fd = openSync(pullLog(marker), "w", 0o600);
+ } catch { /* no progress then */ }
+ return {
+ onLine: (_stream, line) => {
+ if (fd === null) return;
+ try { writeSync(fd, `${line}\n`); } catch { /* best effort */ }
+ },
+ close: () => {
+ if (fd !== null) { try { closeSync(fd); } catch { /* closed */ } fd = null; }
+ // The marker names this (still running) process: gone once the download ends, so it never reads as running.
+ try { rmSync(marker, { force: true }); } catch { /* none */ }
+ },
+ };
+}
+
/**
* Start the backend's download detached (survives this run); one at a time
* per image. A backend without a detached command (the WSL distro, whose
@@ -239,10 +281,13 @@ export function pullInBackground(runtime: BoxRuntime, image: BoxImage, home: str
const cmd = driver.pullCommand(runtime, image, home);
if (!cmd) {
inProcess.add(marker);
- void driver.pull(runtime, image, { home, env, log })
+ // Its progress lines (the Lima VM's setup steps, boxrun's bytes) go to the same log a detached
+ // download writes (pull-progress.ts reads it); the marker's time is when it started.
+ const progress = inProcessLog(marker);
+ void driver.pull(runtime, image, { home, env, log, onLine: progress.onLine })
.then((res) => { if (!res.ok) log("warn", "box download failed", { backend: runtime.backend, error: res.error }); })
.catch((error) => log("warn", "box download failed", { backend: runtime.backend, error: String(error?.message || error) }))
- .finally(() => inProcess.delete(marker));
+ .finally(() => { progress.close(); inProcess.delete(marker); });
return { started: true, pid: process.pid };
}
// Its output goes to a log next to the marker (the Settings card reads its progress there).
@@ -313,7 +358,7 @@ export async function planBox({ env = process.env, home, root, log = () => {}, r
const present = await bounded(driver.imagePresent(runtime, image, { run, home, env }), probeMs, null);
if (!present) {
pullInBackground(runtime, image, home, { env, log });
- return { box: null, notice: `Sandbox is downloading (${image.approxMb ? `about ${image.approxMb} MB, ` : ""}once${runtime.backend === "wsl" && !runtime.installed ? ", with the OmniRush WSL distro" : ""}); chats here run on this machine until it is ready.`, runtime, image, downloading: true };
+ return { box: null, notice: `Sandbox is downloading (${image.approxMb ? `about ${image.approxMb} MB, ` : ""}once${runtime.backend === "wsl" && !runtime.installed ? ", with the OmniRush WSL distro" : runtime.backend === "lima" && !runtime.installed ? ", with the OmniRush VM" : ""}); chats here run on this machine until it is ready.`, runtime, image, downloading: true };
}
}
// The pins this box starts on are remembered: the next box version keeps their images as the previous ones.
diff --git a/apps/server/src/box/pull-progress.ts b/apps/server/src/box/pull-progress.ts
index c329a726..414c92a9 100644
--- a/apps/server/src/box/pull-progress.ts
+++ b/apps/server/src/box/pull-progress.ts
@@ -27,8 +27,38 @@ export type PullProgress = {
layersTotal: number;
/** When the download started (ms since the epoch), when known. */
startedAt: number | null;
+ /**
+ * The OmniRush VM's first setup (macOS, backends/lima.ts setup lines): the step it is at
+ * ("lima", "vm_image", "vm_boot", "image") and that step's bytes; null for other backends.
+ */
+ setup?: SetupProgress | null;
};
+export type SetupProgress = { step: string; bytesDone: number | null; bytesTotal: number | null };
+
+/** The last setup step a log names (`{"setup":{"step","bytes","total"}}`), with its bytes; null: none. */
+export function parseSetupLines(text: string): SetupProgress | null {
+ let last: SetupProgress | null = null;
+ for (const raw of text.split(/\r?\n|\r/)) {
+ const line = raw.trim();
+ if (!line.startsWith("{\"setup\"")) continue;
+ try {
+ const parsed: { setup?: { step?: unknown; bytes?: unknown; total?: unknown } } | null = JSON.parse(line);
+ const step = typeof parsed?.setup?.step === "string" ? parsed.setup.step : null;
+ if (!step) continue;
+ const bytes = Number(parsed?.setup?.bytes);
+ const total = Number(parsed?.setup?.total);
+ const same: boolean = last !== null && last.step === step;
+ last = {
+ step,
+ bytesDone: Number.isFinite(bytes) ? bytes : same ? last!.bytesDone : null,
+ bytesTotal: total > 0 ? total : same ? last!.bytesTotal : null,
+ };
+ } catch { /* a torn line */ }
+ }
+ return last;
+}
+
const DOWNLOADED = /^(Download complete|Extracting|Pull complete|Already exists)\b/;
/** At most this much of the log's end is read (a pull writes a few lines per layer). */
const LOG_TAIL = 256 << 10;
@@ -144,10 +174,14 @@ export function pullProgress(log: string, marker: string, sizes: LayerSizes | nu
let startedAt: number | null = null;
try { startedAt = Math.round(statSync(marker).mtimeMs); } catch { /* unknown */ }
const { seen, done, bytes, layers } = parsePullLog(text);
- if (bytes?.total) return { bytesDone: Math.min(bytes.done, bytes.total), bytesTotal: bytes.total, layersDone: done.size, layersTotal: layers, startedAt };
+ const setup = parseSetupLines(text);
+ // The VM's setup before the box image: its own step's bytes, none of the image's yet.
+ if (setup && setup.step !== "image") return { bytesDone: null, bytesTotal: null, layersDone: 0, layersTotal: 0, startedAt, setup };
+ const extra = setup ? { setup } : {};
+ if (bytes?.total) return { bytesDone: Math.min(bytes.done, bytes.total), bytesTotal: bytes.total, layersDone: done.size, layersTotal: layers, startedAt, ...extra };
// Bytes only when every layer the runtime named has a size (else the sizes are another image's or platform's).
const sized = sizes && seen.length && seen.every((id) => sizes.has(id)) ? sizes : null;
const bytesTotal = sized ? seen.reduce((sum, id) => sum + (sized.get(id) ?? 0), 0) : null;
const bytesDone = sized ? [...done].reduce((sum, id) => sum + (sized.get(id) ?? 0), 0) : null;
- return { bytesDone, bytesTotal, layersDone: done.size, layersTotal: layers, startedAt };
+ return { bytesDone, bytesTotal, layersDone: done.size, layersTotal: layers, startedAt, ...extra };
}
diff --git a/apps/server/src/box/runtime.ts b/apps/server/src/box/runtime.ts
index 3114eb64..f106860c 100644
--- a/apps/server/src/box/runtime.ts
+++ b/apps/server/src/box/runtime.ts
@@ -61,6 +61,11 @@ export type RuntimeInfo = {
idmap?: string;
launcher?: string;
kernel?: string;
+ /** The OmniRush VM (macOS, backends/lima.ts): the box home, the VM's name, where limactl came from, the helpers' folder in it. */
+ home?: string;
+ instance?: string;
+ limactlSource?: "env" | "bundled" | "download";
+ helpers?: string | null;
cached?: boolean;
alternatives?: BackendAlternative[];
};