Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(bot-box): make an own-folder turn's uploads folder before its box starts

AdityaUmalef8df8699a960
diff --git a/server/bot-box/box.test.ts b/server/bot-box/box.test.ts
index a04c252ef6..9ab759c0db 100644
--- a/server/bot-box/box.test.ts
+++ b/server/bot-box/box.test.ts
@@ -6,7 +6,7 @@ import { afterEach, describe, expect, it } from "vitest";
import { BotBox, BoxRunner, BOX_EGRESS_PORT, flyApiPrivate, writeSeccompFile } from "./box.ts";
import { processStart, seesProcess } from "./box-probe.ts";
import { procHidepid } from "./hidepid.ts";
-import { visibleInBox } from "./plan.ts";
+import { boxUploadsDir, visibleInBox } from "./plan.ts";
import { DENIED_SYSCALLS, seccompBytes } from "./seccomp.ts";
import { engineInstallRoots, npmEnginesPrefix } from "../engine-roots.ts";
import { spawnCli } from "../procs.ts";
@@ -54,6 +54,18 @@ describe("BoxRunner.wrap", () => {
expect(wrapped.plan.botId).toBe("b1");
});
+ it("makes the uploads folder of a turn in its bot's own folder before the box starts, so a later turn's upload shows in a running box", () => {
+ const dataDir = mkdtempSync(join(tmpdir(), "omb-box-uploads-"));
+ try {
+ const runner = new BoxRunner({ bwrap: "/usr/bin/bwrap", bridge, setup: { ...setup, dataDir }, denyEnv: new Set() });
+ runner.wrap({ command: "/usr/local/bin/claude", args: [], cwd: join(dataDir, "workspaces", "b1"), botId: "b1", conversationFolder: join(dataDir, "task-workspaces", "b1", "t1"), env: { PATH: "/usr/bin" } });
+ expect(existsSync(boxUploadsDir(dataDir, "b1", "t1"))).toBe(true);
+ expect(existsSync(boxUploadsDir(dataDir, "b1", "t2"))).toBe(false);
+ } finally {
+ rmSync(dataDir, { recursive: true, force: true });
+ }
+ });
+
it("a shared-network box gets no proxy and no bridge", () => {
const runner = new BoxRunner({ bwrap: "/usr/bin/bwrap", bridge, setup, denyEnv: new Set() });
const wrapped = runner.wrap({ command: "/opt/chrome", args: ["--headless"], env: { PATH: "/usr/bin" }, net: "shared" });
diff --git a/server/bot-box/box.ts b/server/bot-box/box.ts
index eb77c27308..6d845d5af5 100644
--- a/server/bot-box/box.ts
+++ b/server/bot-box/box.ts
@@ -19,7 +19,7 @@ import { openHarnessGate, type HarnessGate } from "./harness-gate.ts";
import { distrustHomePrograms } from "./program-trust.ts";
import { procHidepid } from "./hidepid.ts";
import { DENIED_SYSCALLS, seccompBytes, seccompSupported } from "./seccomp.ts";
-import { RELAY_SOCKET_NAME, TEAM_FOLDER_KEPT, boxUploadsDir, teamFolderOf, under, bwrapArgs, inBoxSocket, planBox, taskWorkspaceIds, type BoxAudience, type BoxBridge, type BoxPlan, type BoxRequest, type BoxSetup } from "./plan.ts";
+import { RELAY_SOCKET_NAME, TEAM_FOLDER_KEPT, boxUploadsConversation, boxUploadsDir, teamFolderOf, under, bwrapArgs, inBoxSocket, planBox, taskWorkspaceIds, type BoxAudience, type BoxBridge, type BoxPlan, type BoxRequest, type BoxSetup } from "./plan.ts";
import { openBoxRelay, type BoxRelay } from "./relay.ts";
/** The loopback port inside a box that reaches the egress gate. The harness gate sits at the server's own port. */
@@ -114,12 +114,12 @@ export class BoxRunner {
}
wrap(given: BoxRequest): WrappedSpawn {
- const ids = given.cwd ? taskWorkspaceIds(given.cwd, this.options.setup.dataDir) : null;
+ const uploads = boxUploadsConversation(given, resolve(this.options.setup.dataDir));
const { botId, threadId } = this.conversation(given);
const lent = botId && threadId ? this.lentTo(botId, threadId) : [];
const audience = this.audienceFor(given);
// Made before the box starts, so an upload a later turn adds shows up in a box that is still running.
- if (ids) try { mkdirSync(boxUploadsDir(this.options.setup.dataDir, ids.botId, ids.threadId), { recursive: true, mode: 0o700 }); } catch (error) {
+ if (uploads) try { mkdirSync(boxUploadsDir(this.options.setup.dataDir, uploads.botId, uploads.threadId), { recursive: true, mode: 0o700 }); } catch (error) {
// The bind stays optional, but an upload named later would then be unreadable in this box: say so.
console.error(`bot box: could not make the uploads folder, so uploads stay unreadable in this box: ${error instanceof Error ? error.message : String(error)}`);
}
diff --git a/server/bot-box/plan.ts b/server/bot-box/plan.ts
index b7f5669106..10d90593f4 100644
--- a/server/bot-box/plan.ts
+++ b/server/bot-box/plan.ts
@@ -156,6 +156,17 @@ export function uploadsConversation(cwd: string, dataDir: string, turn?: { botId
return cwd === join(dataDir, "workspaces", turn.botId) ? { botId: turn.botId, threadId: turn.threadId } : null;
}
+/** The conversation whose uploads a box for `request` sees: its conversation folder's, or for a turn in its bot's own
+ * folder, that turn's (`request.conversationFolder`, of the same bot). Null for any other box. */
+export function boxUploadsConversation(request: BoxRequest, dataDir: string): { botId: string; threadId: string } | null {
+ const cwd = request.cwd ? resolve(request.cwd) : null;
+ if (!cwd) return null;
+ const ids = taskWorkspaceIds(cwd, dataDir);
+ if (ids) return ids;
+ const turn = request.conversationFolder ? taskWorkspaceIds(resolve(request.conversationFolder), dataDir) : null;
+ return turn && turn.botId === request.botId ? uploadsConversation(cwd, dataDir, turn) : null;
+}
+
/** The server-owned folder holding a conversation's uploads for its boxes (server/box-attachments.ts), bound read-only. */
export function boxUploadsDir(dataDir: string, botId: string, threadId: string): string {
return join(dataDir, "box-uploads", botId, threadId);
@@ -291,8 +302,7 @@ export function planBox(setup: BoxSetup, request: BoxRequest): BoxPlan {
// The person's uploads for this conversation, after every read-write line: readable, but never writable or swappable.
// A turn in its bot's own folder sees only its own conversation's: a process there is never shared with another
// conversation (procs.ts sharedBoxScope), so this holds for one started for several of them too.
- const turnIds = !ids && cwd && request.conversationFolder ? taskWorkspaceIds(resolve(request.conversationFolder), dataDir) : null;
- const uploadsOf = ids ?? (turnIds && turnIds.botId === botId ? uploadsConversation(cwd!, dataDir, turnIds) : null);
+ const uploadsOf = boxUploadsConversation(request, dataDir);
if (uploadsOf) {
const shown = scoped ? [...scoped.rw, ...scoped.ro].filter((id) => ID.test(id)).map((id) => boxUploadsDir(dataDir, threads!, id))
: [threads ? join(dataDir, "box-uploads", threads) : boxUploadsDir(dataDir, uploadsOf.botId, uploadsOf.threadId)];