Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat: farm mode (stock) refuses a run that cannot use the sandbox

ogsamrat5fa5e0251e8c
diff --git a/apps/server/src/box/box-required-service.test.ts b/apps/server/src/box/box-required-service.test.ts
index 0a4755b9..ead08946 100644
--- a/apps/server/src/box/box-required-service.test.ts
+++ b/apps/server/src/box/box-required-service.test.ts
@@ -392,3 +392,28 @@ describe("required sandbox: a stopped send", () => {
expect((Reflect.get(boxes, "nativeChats") as Map<string, string>).has("ses_req_undo_old")).toBe(true);
});
});
+
+describe("farm runs (stock mode): the box or no prompt", () => {
+ test("box off, host-only project, no backend, a remote workspace: refused with one line; other runs never", async () => {
+ const state = await temp("farm-state");
+ rememberServerDefault(state, { local_box: { default: "on", required: true } });
+ const root = await temp("farm-proj");
+ const local: BoxWorkspace = { id: "w1", path: root, workspaceType: "local" };
+ const line = (why: string) => `farm mode runs only in the sandbox (it keeps the session offline); this machine has no usable sandbox (${why})`;
+ const farm = { ...NO_RUNTIME, OMNIRUSH_FARM: "1" };
+ expect(await service({ stateDir: state, workspaces: [local], env: { ...farm, OMNIRUSH_BOX: "0" } }).farmRefusal(local)).toBe(line("box off: OMNIRUSH_BOX=0"));
+ const none = await service({ stateDir: state, workspaces: [local], env: { ...farm, OMNIRUSH_FARM: "true" } }).farmRefusal(local);
+ expect(none).toStartWith(line("no backend that keeps it offline").slice(0, -1));
+ const win = await temp("farm-win");
+ await writeFile(join(win, "app.vcxproj"), "<Project/>");
+ const winWs: BoxWorkspace = { id: "w2", path: win, workspaceType: "local" };
+ expect(await service({ stateDir: state, workspaces: [winWs], env: farm, platform: "win32" }).farmRefusal(winWs)).toBe(line("host-only project"));
+ const remote: BoxWorkspace = { id: "w3", path: "/remote", workspaceType: "remote" };
+ expect(await service({ stateDir: state, workspaces: [remote], env: farm }).farmRefusal(remote)).toBe(line("not a local project"));
+ // Not a farm run: nothing refused, whatever the box can do here.
+ for (const env of [{ ...NO_RUNTIME, OMNIRUSH_BOX: "0" }, NO_RUNTIME, { ...NO_RUNTIME, OMNIRUSH_FARM: "0" }]) {
+ expect(await service({ stateDir: state, workspaces: [local, remote], env }).farmRefusal(local)).toBeNull();
+ expect(await service({ stateDir: state, workspaces: [local, remote], env }).farmRefusal(remote)).toBeNull();
+ }
+ });
+});
diff --git a/apps/server/src/box/service.ts b/apps/server/src/box/service.ts
index e2175682..a5d9158d 100644
--- a/apps/server/src/box/service.ts
+++ b/apps/server/src/box/service.ts
@@ -39,6 +39,7 @@ import { desktopText, hostOnlyProject, requiredLaunch, shadowsApply, shadowsNoti
import { cachedWindowsChecks, classifySetup, linuxAllow, resumeWinSetup, saveSetupState, setupState, windowsInstall, windowsRestart, windowsUpdate, type BoxSetup } from "./setup.js";
import type { BoxInstallLogPaths } from "./install-log.js";
import { DESKTOP_PROJECTS_FILE } from "./plugin-lib.js";
+import { stockMode } from "../stock-mode/stock-mode.mjs";
import { BUILDATHON_OFFER_NOTICE, buildathonBoxFor, buildathonProjectsFile, LESS_REPRO_NOTICE } from "../buildathon-repro.js";
import { layerSizes, pullProgress, type PullProgress } from "./pull-progress.js";
import { detectRuntime, type BackendAlternative, type DetectOptions, type RuntimeInfo, type RuntimeResult } from "./runtime.js";
@@ -1018,6 +1019,29 @@ export class BoxService {
* that cannot be had), or null: it keeps the box even when its project's
* box was turned off since, and is refused only when the box truly cannot run.
*/
+ /**
+ * Farm runs (stock mode) run only in the box, which keeps them offline (boxd egress.go): a prompt of a
+ * workspace that cannot use it is refused (the line to refuse with), before any model call or upload.
+ * Null: not a farm run, or the box is there. CLI: src/box/launch.js farmRefusal / farmGate.
+ */
+ async farmRefusal(workspace: BoxWorkspace): Promise<string | null> {
+ if (!stockMode(this.env)) return null;
+ if (workspace.workspaceType !== "local") return farmLine("not a local project");
+ const mode = await this.mode(workspace).catch(() => null);
+ if (mode && !mode.on) {
+ if (["0", "off", "false"].includes(String(this.env.OMNIRUSH_BOX ?? "").trim().toLowerCase())) return farmLine("box off: OMNIRUSH_BOX=0");
+ return farmLine(mode.source === "host-only" ? "host-only project" : `box off: ${mode.source}`);
+ }
+ const run = await this.ensure(workspace).catch(() => null);
+ if (run && !run.native) return null;
+ const project = this.projects.get(boxKey(boxRoot(workspace)));
+ const why = project?.why;
+ const reason = project?.lastRuntime && !project.lastRuntime.ok ? project.lastRuntime.reason : null;
+ if (why?.reason === "no_runtime") return farmLine(`no backend that keeps it offline${reason ? `: ${String(reason).slice(0, 200)}` : ""}`);
+ if (why?.reason === "downloading") return farmLine("the sandbox image is still downloading; try again when it is done");
+ return farmLine(project?.notice ? String(project.notice).slice(0, 200) : "the sandbox cannot start on this computer right now");
+ }
+
async pinnedBoxUnavailable(sessionId: string, workspace: BoxWorkspace): Promise<string | null> {
if (workspace.workspaceType !== "local" || !this.chatPin(sessionId)?.box) return null;
const run = await this.ensure(workspace, { keep: true }).catch(() => null);
@@ -1098,3 +1122,8 @@ export class BoxService {
return done;
}
}
+
+/** The one line a farm run that cannot use the box is refused with. CLI: src/box/launch.js farmLine. */
+export function farmLine(why: string): string {
+ return `farm mode runs only in the sandbox (it keeps the session offline); this machine has no usable sandbox (${why})`;
+}
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 1ce30cb1..634cee7f 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -2255,6 +2255,12 @@ export async function proxyOpencodeV2Request(input: {
const sessionMatch = routePath.match(/^\/api\/session\/([^/]+)(?:\/|$)/);
const sessionId = sessionMatch?.[1] ? decodeEngineRouteParam(sessionMatch[1]) : null;
const promptDispatch = isUploadV2PromptDispatch(method, routePath);
+ // Farm runs (stock mode) only in the box, which keeps them offline: refused before any upload or model call.
+ const farmRefused = promptDispatch ? await boxServiceFor(input.config).farmRefusal(input.workspace) : null;
+ if (farmRefused) {
+ await input.request.arrayBuffer().catch(() => undefined);
+ return jsonResponse({ error: "farm_needs_sandbox", message: `omnirush: ${farmRefused}` }, 409);
+ }
const collectible = input.workspace.workspaceType !== "remote";
if (promptDispatch && collectible) {
if (uploadDispatchRefused(input.config, input.workspace)) {
@@ -2793,6 +2799,12 @@ export async function proxyOpencodeRequest(input: {
await input.request.arrayBuffer().catch(() => undefined);
return jsonResponse({ error: "engine_v1_retired", message: "Chats now run on the new engine. Update OmniRush.ai or reopen the chat." }, 409);
}
+ // Farm runs (stock mode) only in the box, which keeps them offline: refused before any upload or model call.
+ const farmRefused = workspace && isUploadPromptDispatch(method, proxyPath) ? await boxServiceFor(input.config).farmRefusal(workspace) : null;
+ if (farmRefused) {
+ await input.request.arrayBuffer().catch(() => undefined);
+ return jsonResponse({ error: "farm_needs_sandbox", message: `omnirush: ${farmRefused}` }, 409);
+ }
if (workspace && workspace.workspaceType !== "remote" && isUploadPromptDispatch(method, proxyPath)) {
if (uploadDispatchRefused(input.config, workspace)) {
// Drain the unread body so the client's keep-alive connection stays usable.
diff --git a/apps/server/src/session-uploader.server.e2e.test.ts b/apps/server/src/session-uploader.server.e2e.test.ts
index 46de0aba..8777a387 100644
--- a/apps/server/src/session-uploader.server.e2e.test.ts
+++ b/apps/server/src/session-uploader.server.e2e.test.ts
@@ -885,6 +885,35 @@ describe("collection gaps: the v2 mount and local workflow steps", () => {
expect(v2.prompts).toHaveLength(1);
}, 30_000);
+ test("a farm run (stock mode) whose box is off: the v2 prompt is refused with one line, nothing reaches the engine", async () => {
+ const { root, stateDir } = await createWorkspace();
+ const engine = startMockEngine(provider);
+ const v2 = startMockV2Engine(provider);
+ const config = serverConfig({ root, stateDir, engineBaseUrl: engine.baseUrl });
+ await startOmniRush(config);
+ preserveGateEnv();
+ setGateEnv("1", "1");
+ const saved = { farm: process.env.OMNIRUSH_FARM, box: process.env.OMNIRUSH_BOX };
+ try {
+ process.env.OMNIRUSH_FARM = "1";
+ process.env.OMNIRUSH_BOX = "0";
+ const response = await promptV2(config, v2.connection);
+ expect(response.status).toBe(409);
+ expect(await response.json()).toEqual({ error: "farm_needs_sandbox", message: "omnirush: farm mode runs only in the sandbox (it keeps the session offline); this machine has no usable sandbox (box off: OMNIRUSH_BOX=0)" });
+ expect(v2.received).toEqual([]);
+ expect(v2.prompts).toEqual([]);
+ // The same machine, not a farm run: the prompt goes through.
+ delete process.env.OMNIRUSH_FARM;
+ expect((await promptV2(config, v2.connection)).status).toBe(200);
+ expect(v2.prompts).toEqual([{ text: "Do the task" }]);
+ } finally {
+ for (const [key, value] of [["OMNIRUSH_FARM", saved.farm], ["OMNIRUSH_BOX", saved.box]] as const) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ }
+ }, 30_000);
+
test("captures a v2 prompt dispatch exactly like a v1 one: session start, prompt milestone, request/response trace and the settled turn", async () => {
const { root, stateDir } = await createWorkspace();
const engine = startMockEngine(provider);