A desktop coding agent with free access to frontier models.
TypeScript
fix: name where each sandbox chat runs and why
ogsamratb26f503a5db9
diff --git a/apps/server/src/box/box.test.ts b/apps/server/src/box/box.test.tsindex d4023fe8..a63330eb 100644--- a/apps/server/src/box/box.test.ts+++ b/apps/server/src/box/box.test.ts@@ -11,7 +11,7 @@ import { boxRun, engineConfig, gcInBackground, mapToBox, pullInBackground, pullLimport { clearLayerSizes, layerSizes, parseManifestSizes, parsePullLog, pullProgress } from "./pull-progress.js";import { DESKTOP_PROJECTS_FILE } from "./plugin-lib.js";import { candidates, detectRuntime, explainFailure, probe, probeWsl, wslLauncher, type RunCommand } from "./runtime.js";-import { BoxService, type BoxWorkspace } from "./service.js";+import { BoxService, runtimeWhy, type BoxWorkspace } from "./service.js";import { OmniRushBox } from "../opencode-plugins/omnirush-box.js";const dirs: string[] = [];@@ -449,6 +449,41 @@ describe("box service", () => {expect(await listed()).toEqual(["ses_live_old1"]);});+ test("planOutcome: each chat says once where it runs and why (box.plan)", async () => {+ const stateDir = await temp("state");+ const home = await temp("boxhome");+ const root = await temp("project");+ const workspace: BoxWorkspace = { id: "ws_o", path: root, workspaceType: "local" };+ const run: BoxRun = {+ key: boxKey(root), container: "c", runtimeBin: "docker", runtimeArgs: [], backend: "docker", exec: null, root, native: false, box: null,+ ready: async () => null, label: () => null, setTurn: () => {},+ freeze: async (label, { turn = null }) => ({ label, turn, copied: 0, carried: 0, save: async () => ({ label, changed: 0, bytes: 0 }), discard: async () => null }),+ };+ rememberServerDefault(stateDir, { local_box: { default: "off" } });+ const boxes = service({ stateDir, workspaces: [workspace], settings: new Map(), env: { OMNIRUSH_BOX_HOME: home } });+ boxes.ensure = async () => null;+ await boxes.beforePrompt("ses_out_off1", workspace);+ expect(await boxes.planOutcome("ses_out_off1", workspace)).toEqual({ native: true, source: "default", reason: "off" });+ expect(await boxes.planOutcome("ses_out_off1", workspace)).toBeNull();+ rememberServerDefault(stateDir, { local_box: { default: "on" } });+ expect(await boxes.planOutcome("ses_out_wait1", workspace)).toEqual({ native: true, source: "server", reason: "planning" });+ boxes.ensure = async () => run;+ await boxes.beforePrompt("ses_out_box1", workspace);+ expect(await boxes.planOutcome("ses_out_box1", workspace)).toEqual({ native: false, source: "server", reason: "box" });+ // The chat that ran here before the switch stays (kept_old_chat).+ await boxes.beforePrompt("ses_out_off1", workspace);+ expect(await boxes.planOutcome("ses_out_off1", workspace)).toBeNull();+ await boxes.beforePrompt("ses_out_old2", workspace, { turnsBefore: 3 });+ expect(await boxes.planOutcome("ses_out_old2", workspace)).toEqual({ native: true, source: "chat", reason: "kept_old_chat" });+ expect(await boxes.planOutcome("ses_out_remote", { id: "ws_r", path: root, workspaceType: "remote" })).toBeNull();+ });++ test("runtimeWhy: the backend and its failure code, never its text or a path", () => {+ expect(runtimeWhy({ name: "bwrap", code: "apparmor", reason: "AppArmor blocks /usr/bin/bwrap" })).toEqual({ name: "bwrap", code: "apparmor" });+ expect(runtimeWhy({ name: "docker", reason: "the sandbox backend did not answer in time" })).toEqual({ name: "docker", code: "timeout" });+ expect(runtimeWhy({ name: "C:\\docker.exe", code: "Not A Code", reason: "x" })).toEqual({ name: null, code: null });+ });+test("watchServerDefault reads the server's default again and applies only a change", async () => {const stateDir = await temp("state");rememberServerDefault(stateDir, { local_box: { default: "off" } });diff --git a/apps/server/src/box/service.ts b/apps/server/src/box/service.tsindex 75f94c7c..f8711dd9 100644--- a/apps/server/src/box/service.ts+++ b/apps/server/src/box/service.ts@@ -127,7 +127,27 @@ export type BoxServiceOptions = {};/** `waitingImage`: planned while its image was downloading (planned again as soon as the image is here). */-type Project = { run: BoxRun | null; notice: string | null; planning: Promise<BoxRun | null> | null; nextPlanAt: number; waitingImage: boolean };+type Project = { run: BoxRun | null; notice: string | null; planning: Promise<BoxRun | null> | null; nextPlanAt: number; waitingImage: boolean; why?: PlanWhy | null };++/** Why the project's last plan has no box (the `box.plan` reason), with the runtime's failure in safe words. */+type PlanWhy = { reason: "no_runtime" | "helpers_missing" | "downloading"; runtime?: { name: string | null; code: string | null } };++/**+ * A chat's `box.plan` trace event (once per chat and app run): where its+ * prompts run and why, as the CLI's `client.launch` `box`. `reason`: box,+ * box_failed, off, offer, host_builds, kept_old_chat, no_runtime,+ * helpers_missing, downloading, planning; `source`: what turned the box on or+ * off (project, server, default, flag, host-builds, chat).+ */+export type BoxPlanOutcome = { native: boolean; source: string | null; reason: string; runtime?: { name: string | null; code: string | null } };++/** The backend tried and its failure code (bubblewrap's apparmor, no_bwrap, ...; `timeout`), never its text or a path. */+export function runtimeWhy(runtime: { name?: unknown; code?: unknown; reason?: unknown } | null | undefined): { name: string | null; code: string | null } {+ const code = typeof runtime?.code === "string" && /^[a-z_]{1,32}$/.test(runtime.code) ? runtime.code+ : /did not answer in time/.test(String(runtime?.reason ?? "")) ? "timeout" : null;+ const name = typeof runtime?.name === "string" && /^[a-z-]{1,16}$/.test(runtime.name) ? runtime.name : null;+ return { name, code };+}/** A project whose image is downloading is planned again at most this often. */const REPLAN_MS = 15_000;@@ -149,6 +169,8 @@ export class BoxService {private readonly nativeChats = new Map<string, string>();/** Chats sent to their project's box in this run (a failed checkpoint included). */private readonly boxedChats = new Set<string>();+ /** Chats whose box.plan event went out in this run. */+ private readonly outcomeSent = new Set<string>();private readonly home: string;private readonly env: NodeJS.ProcessEnv;private readonly platform: NodeJS.Platform;@@ -324,6 +346,10 @@ export class BoxService {if (Date.now() < project.nextPlanAt) return null;const plan = await planBox({ env: this.env, home: this.home, root, log: this.log, shims: shimsDir(this.env), ...this.detectOptions() });project.waitingImage = !plan.box && plan.downloading === true;+ project.why = plan.box ? null+ : plan.downloading === true ? { reason: "downloading" }+ : plan.runtime && !plan.runtime.ok ? { reason: "no_runtime", runtime: runtimeWhy(plan.runtime) }+ : { reason: "helpers_missing" };if (!plan.box) {project.run = null;project.notice = plan.notice;@@ -575,6 +601,22 @@ export class BoxService {return { key: run.key, root: run.root, croot: box.croot, container: box.container, runtimeBin: run.runtimeBin, runtimeArgs: run.runtimeArgs, backend: run.backend, exec: run.exec, label };}+ /** The chat's box.plan outcome, once per chat and app run (null after that, or for a folder not on this computer). */+ async planOutcome(sessionId: string, workspace: BoxWorkspace): Promise<BoxPlanOutcome | null> {+ if (workspace.workspaceType !== "local" || this.outcomeSent.has(sessionId)) return null;+ this.outcomeSent.add(sessionId);+ const mode = await this.mode(workspace).catch(() => null);+ if (!mode) return { native: true, source: null, reason: "plan_failed" };+ if (this.nativeChats.has(sessionId)) return { native: true, source: "chat", reason: "kept_old_chat" };+ if (this.boxedChats.has(sessionId)) return { native: false, source: mode.source, reason: "box" };+ if (!mode.on) return { native: true, source: mode.source, reason: mode.source === "host-builds" ? "host_builds" : mode.offer ? "offer" : "off" };+ const project = this.projects.get(boxKey(boxRoot(workspace)));+ if (project?.run?.native) return { native: true, source: mode.source, reason: "box_failed" };+ if (project?.why) return { native: true, source: mode.source, reason: project.why.reason, ...(project.why.runtime ? { runtime: project.why.runtime } : {}) };+ // Not planned yet (a plan still running, or waiting for its next try).+ return { native: true, source: mode.source, reason: "planning" };+ }+/** Whether chats ran in a box during this run (they get a `final` checkpoint at quit). */hasChats(): boolean {return this.turns.size > 0;diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 1eb65a53..1bd86cc8 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -1829,6 +1829,8 @@ export async function proxyOpencodeV2Request(input: {await capture.archiveStartGate(uploadedSessionId, input.workspace.path, lateWritesGate(input.workspace));capture.boxSession(uploadedSessionId, boxInfo);capture.startSession(uploadedSessionId, input.workspace.id, input.workspace.path, turnsBefore);+ const boxPlan = await boxServiceFor(input.config).planOutcome(uploadedSessionId, input.workspace).catch(() => null);+ if (boxPlan) capture.recordTrace(uploadedSessionId, "box.plan", boxPlan);capture.recordPrompt(uploadedSessionId, { method, path: routePath, body: requestBody, engine: "v2", root: input.workspace.path, attachments: true, dispatchedAt: Date.now() });capture.captureSnapshot(uploadedSessionId, "prompt");capture.archiveSessionStarted(uploadedSessionId, input.workspace.path, engine);@@ -2219,6 +2221,8 @@ export async function proxyOpencodeRequest(input: {if (promptDispatch) await capture.archiveStartGate(uploadedSessionId, workspace.path, lateWritesGate(workspace, pool?.snapshot().generations.map((generation) => generation.pid) ?? []));if (promptDispatch) capture.boxSession(uploadedSessionId, boxInfo);capture.startSession(uploadedSessionId, workspace.id, workspace.path, turnsBefore);+ const boxPlan = isUploadPromptDispatch(method, proxyPath) ? await boxServiceFor(input.config).planOutcome(uploadedSessionId, workspace).catch(() => null) : null;+ if (boxPlan) capture.recordTrace(uploadedSessionId, "box.plan", boxPlan);capture.recordPrompt(uploadedSessionId, {method,path: normalizeOpencodeProxyPath(proxyPath),