Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: retain image attachment paths for file-based tools
Codexb8a6ba3f61a9
diff --git a/docs/verification/chat-turns.md b/docs/verification/chat-turns.mdindex f3150f0602..619d0179c6 100644--- a/docs/verification/chat-turns.md+++ b/docs/verification/chat-turns.md@@ -28,6 +28,39 @@ The wait result must be `settled`, and the messages result must contain thefake engine's bot response. Use `--dry-run` on `send` when checking a target orcommand without starting a turn.+## Uploaded images used by file tools++Upload a product photo in a chat, then ask the bot to pass that photo to a+media-upload CLI. The engine needs both native vision input and a file reference;+being able to describe the image does not prove that a file tool can read it.++```sh+pnpm exec vitest run server/turn-images.test.ts server/box-attachments.test.ts server/box-attachments.linux.test.ts server/bot-api.test.ts server/host/handoff-files.test.ts+pnpm exec vitest run server/index.test.ts -t "keeps a channel image|sends an earlier image"+```++For an end-to-end check, launch the isolated fixture above, upload a synthetic+PNG to its `POST /api/attachments` endpoint with `Content-Type: image/png`, then+send `Use this photo with a media-upload CLI` followed by a standalone+`<attached-image path="RETURNED_PATH" name="mug.png" />` through `control:omb send`.+Use only the fixture's explicit URL. Wait for `settled` and save `messages`.+In the fixture's `fake-claude-dump.json`, inspect only `prompt`: it must contain+native image content and an `<attached-file>` reference for `mug.png`, with no+remaining `<attached-image>` tag. Read the referenced file and compare its bytes+with the uploaded PNG.++The regression test also passes the extracted prompt through `attachmentsForBox`+with confinement on and off and reads the resulting path in a real child process.+It checks that Codex still receives the same inline image bytes and that unrelated+uploads are not copied into the conversation's uploads directory. This is not a+kernel sandbox test; the Linux bubblewrap test supplies that coverage when the+host supports it.++Last exercised on Linux with Node 24.22.3 against an isolated fake-engine server:+the turn settled with both native image content and a readable file reference.+The focused suite passed 71 tests, and both selected server image workflow tests+passed. The bubblewrap test was skipped because this container cannot run its sandbox. No real model or marketplace upload was used.+## Gotchas- Omitting `--task` snapshots the selected task before sending, waiting,diff --git a/server/box-attachments.test.ts b/server/box-attachments.test.tsindex 73dc1175cb..f7c0f78bbd 100644--- a/server/box-attachments.test.ts+++ b/server/box-attachments.test.ts@@ -1,9 +1,12 @@+import { execFileSync } from "node:child_process";import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, beforeEach, describe, expect, it } from "vitest";import { boxUploadsDir, planBox } from "./bot-box/plan.ts";import { attachmentsForBox } from "./box-attachments.ts";+import { extractTurnImages } from "./turn-images.ts";+import { codexImageInputs } from "./drivers/codex-images.ts";describe("attachmentsForBox", () => {let root: string, dataDir: string, uploads: string, cwd: string, shown: string;@@ -34,6 +37,24 @@ describe("attachmentsForBox", () => {expect(run(text)).toBe(out);});+ it.each([false, true])("keeps an image usable by a file CLI and by vision (boxed=%s)", async (boxed) => {+ const name = "123e4567-e89b-42d3-a456-426614174000.png";+ const upload = join(uploads, name);+ const bytes = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aL1sAAAAASUVORK5CYII=", "base64");+ writeFileSync(upload, bytes);+ writeFileSync(join(uploads, "other-conversation.png"), "private");+ const extracted = extractTurnImages(`Upload this photo\n\n<attached-image path="${upload}" name="mug.png" />`, { attachmentsDir: uploads });+ const text = run(extracted.text, cwd, boxed);+ const file = /<attached-file path="([^"]+)" name="mug.png" \/>/.exec(text)?.[1];++ expect(file).toBe(boxed ? join(shown, name) : upload);+ expect(execFileSync(process.execPath, ["-e", "process.stdout.write(require('node:fs').readFileSync(process.argv[1]))", file!])).toEqual(bytes);+ expect(await codexImageInputs(extracted.images)).toEqual([{ type: "image", url: `data:image/png;base64,${bytes.toString("base64")}` }]);+ expect(existsSync(join(shown, "other-conversation.png"))).toBe(false);+ expect(existsSync(join(boxUploadsDir(dataDir, "bot", "other"), name))).toBe(false);+ expect(extractTurnImages(extracted.text, { attachmentsDir: uploads }).images).toEqual([]);+ });+it("finds an upload whose path the tag escaped", () => {const dir = join(root, "a&b", "attachments");mkdirSync(dir, { recursive: true });diff --git a/server/index.test.ts b/server/index.test.tsindex eb21134836..65854f8025 100644--- a/server/index.test.ts+++ b/server/index.test.ts@@ -5337,7 +5337,8 @@ describe("harness HTTP API", () => {text: expect.stringContaining("Describe this image"),});expect(JSON.stringify(dump.prompt)).not.toContain("attached-image");- expect(JSON.stringify(dump.prompt)).not.toContain(imagePath);+ expect(JSON.stringify(dump.prompt)).toContain("<attached-file");+ expect(JSON.stringify(dump.prompt)).toContain(imagePath);const current = (await api("GET", "/api/bots?messages=20")).body.groups.find((candidate: { id: string }) => candidate.id === room.id,@@ -5400,7 +5401,8 @@ describe("harness HTTP API", () => {expect.objectContaining({ source: expect.objectContaining({ data: png.toString("base64") }) }),]);expect(JSON.stringify(rewound.prompt)).not.toContain("attached-image");- expect(JSON.stringify(rewound.prompt)).not.toContain(imagePath);+ expect(JSON.stringify(rewound.prompt)).toContain("<attached-file");+ expect(JSON.stringify(rewound.prompt)).toContain(imagePath);await releaseTurn(bot.id);} finally {await api("POST", `/api/bots/${bot.id}/interrupt`, {}).catch(() => undefined);diff --git a/server/turn-images.test.ts b/server/turn-images.test.tsindex 2078ba58c5..4b545d2119 100644--- a/server/turn-images.test.ts+++ b/server/turn-images.test.ts@@ -54,12 +54,12 @@ describe("turn image admission", () => {const result = extractTurnImages(source, { attachmentsDir: root });expect(source).toContain(png);- expect(result.text).toBe("compare these\n\n\n\n");+ expect(result.text).toBe(source.replaceAll("<attached-image", "<attached-file"));expect(result.images).toEqual([{ path: png, mime: "image/png", bytes: 7 },{ path: jpg, mime: "image/jpeg", bytes: 9 },]);- expect(result.text).not.toContain(root);+ expect(result.text).toContain(root);});it("leaves inline, fenced, missing, outside-store, and non-generated tags as text", () => {@@ -90,7 +90,7 @@ describe("turn image admission", () => {const encoded = path.replaceAll("&", "&");expect(extractTurnImages(`<attached-image path="${encoded}" />`, { attachmentsDir: root })).toEqual({- text: "",+ text: `<attached-file path="${encoded}" />`,images: [{ path, mime: "image/webp", bytes: 5 }],videos: [],});@@ -127,7 +127,7 @@ describe("turn image admission", () => {].join("\n\n");const result = extractTurnImages(source, { attachmentsDir: root });- expect(result.text).toBe(`\n\n<attached-file path="${clip}" name="clip.webm" />`);+ expect(result.text).toBe(source.replace("<attached-image", "<attached-file"));expect(result.images).toEqual([{ path: png, mime: "image/png", bytes: 4 }]);expect(result.videos).toEqual([{ path: clip, mime: "video/webm", bytes: 6 }]);});@@ -145,7 +145,7 @@ describe("turn image admission", () => {});});- it("rejects count and aggregate limits before partially stripping the prompt", () => {+ it("rejects count and aggregate limits before partially rewriting the prompt", () => {const root = store();const first = image(root, 0, ".png", 4);const second = image(root, 1, ".gif", 4);@@ -193,7 +193,7 @@ describe("turn image admission", () => {];expect(extractSteerImages(texts, { attachmentsDir: root })).toEqual({- texts: ["look\n\n", "plain words stay exactly as sent", "and this\n\n"],+ texts: texts.map((text) => text.replace("<attached-image", "<attached-file")),images: [{ path: first, mime: "image/png", bytes: 5 },{ path: second, mime: "image/webp", bytes: 6 },@@ -225,7 +225,7 @@ describe("turn image admission", () => {expect(result.images.map((entry) => entry.path)).toEqual([join(root, `${IDS[2]}.png`), join(root, `${IDS[3]}.png`)]);expect(result.transcript.map((entry) => entry.text)).toEqual([- transcript[0]!.text, transcript[1]!.text, `older\n\n${REPLAYED_IMAGE_NOTE}`, `newest\n\n${REPLAYED_IMAGE_NOTE}`,+ transcript[0]!.text, transcript[1]!.text, `older\n\n${REPLAYED_IMAGE_NOTE}\n\n${tag(2).replace("<attached-image", "<attached-file")}`, `newest\n\n${REPLAYED_IMAGE_NOTE}\n\n${tag(3).replace("<attached-image", "<attached-file")}`,]);expect(transcript[3]!.text).toContain("<attached-image");});@@ -242,7 +242,7 @@ describe("turn image admission", () => {const result = extractReplayImages(transcript, [], { attachmentsDir: root });expect(result.images.map((entry) => entry.path)).toEqual([join(root, `${IDS[0]}.png`)]);- expect(result.transcript[0]!.text).toBe(`login fails, see screenshot\n\n${REPLAYED_IMAGE_NOTE}`);+ expect(result.transcript[0]!.text).toBe(`login fails, see screenshot\n\n${REPLAYED_IMAGE_NOTE}\n\n${tag(0).replace("<attached-image", "<attached-file")}`);expect(REPLAYED_IMAGE_NOTE).toMatch(/earlier/i);expect(result.transcript[2]!.text).toBe("A");});@@ -250,7 +250,7 @@ describe("turn image admission", () => {it("keeps a current turn's own images unmarked", () => {const root = store();const result = extractTurnImages(`look\n\n<attached-image path="${image(root, 0)}" name="0.png" />`, { attachmentsDir: root });- expect(result.text).toBe("look\n\n");+ expect(result.text).toBe(`look\n\n<attached-file path="${join(root, `${IDS[0]}.png`)}" name="0.png" />`);});it("stops at the first replayed message whose images no longer fit", () => {diff --git a/server/turn-images.ts b/server/turn-images.tsindex a84a63fed0..7e901a245e 100644--- a/server/turn-images.ts+++ b/server/turn-images.ts@@ -116,7 +116,7 @@ const ownedVideo = (path: string, attachmentsDir: string): TurnVideoInput | nullownedAttachment(path, attachmentsDir, OWNED_VIDEO_NAME, VIDEO_MIME_BY_EXTENSION, FILE_MAX_BYTES);/**- * Left where a replayed message's image was: the image itself rides on the current turn, so without this the model+ * Added beside a replayed message's image file reference: the image itself rides on the current turn, so without this the model* reads an old screenshot as part of the latest message.*/export const REPLAYED_IMAGE_NOTE =@@ -127,13 +127,13 @@ export interface ExtractTurnImagesOptions {maxCount?: number;maxBytes?: number;maxVideoCount?: number;- /** Text left in place of each admitted image tag. */+ /** Text prepended to each admitted image file reference. */imageNote?: string;}/**- * Invalid tags stay visible as ordinary text and never become native image input. A video's <attached-file> tag- * always stays in the text, so an engine that reads files still gets its path; sampled frames only add to it.+ * Invalid tags stay visible as ordinary text and never become native image input. Admitted images retain an+ * attached-file reference for file tools; videos keep their existing reference alongside sampled frames.*/export function extractTurnImages(text: string,@@ -166,9 +166,12 @@ export function extractTurnImages(.slice(0, maxVideoCount);let providerText = text;- const removedTags = admittedImages.map((item) => item.tag).sort((a, b) => b.start - a.start);- const note = options.imageNote ?? "";- for (const tag of removedTags) providerText = providerText.slice(0, tag.start) + note + providerText.slice(tag.end);+ const imageTags = admittedImages.map((item) => item.tag).sort((a, b) => b.start - a.start);+ for (const tag of imageTags) {+ const fileTag = text.slice(tag.start, tag.end).replace("<attached-image", "<attached-file");+ const reference = options.imageNote ? `${options.imageNote}\n\n${fileTag}` : fileTag;+ providerText = providerText.slice(0, tag.start) + reference + providerText.slice(tag.end);+ }return { text: providerText, images: admittedImages.map((item) => item.image), videos };}