Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: normalize admitted image references and portable prompt assertions
Codex91fa19d7ad4d
diff --git a/docs/verification/chat-turns.md b/docs/verification/chat-turns.mdindex 619d0179c6..f1ce753507 100644--- a/docs/verification/chat-turns.md+++ b/docs/verification/chat-turns.md@@ -50,7 +50,8 @@ remaining `<attached-image>` tag. Read the referenced file and compare its byteswith 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.+with confinement on and off and reads the resulting path in a real child process, including an+admitted path spelled with a nonexistent `missing/../` segment.It checks that Codex still receives the same inline image bytes and that unrelateduploads are not copied into the conversation's uploads directory. This is not akernel sandbox test; the Linux bubblewrap test supplies that coverage when the@@ -58,7 +59,7 @@ 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+The focused suite passed 74 tests, and both selected server image workflow testspassed. The bubblewrap test was skipped because this container cannot run its sandbox. No real model or marketplace upload was used.## Gotchasdiff --git a/server/box-attachments.test.ts b/server/box-attachments.test.tsindex f7c0f78bbd..b74b3f853d 100644--- a/server/box-attachments.test.ts+++ b/server/box-attachments.test.ts@@ -37,13 +37,14 @@ 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) => {+ it.each([[false, false], [false, true], [true, false], [true, true]])("keeps an image usable by a file CLI and by vision (boxed=%s, nonCanonical=%s)", async (boxed, nonCanonical) => {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 inputPath = nonCanonical ? `${uploads}/missing/../${name}` : upload;+ const extracted = extractTurnImages(`Upload this photo\n\n<attached-image path="${inputPath}" name="mug.png" />`, { attachmentsDir: uploads });const text = run(extracted.text, cwd, boxed);const file = /<attached-file path="([^"]+)" name="mug.png" \/>/.exec(text)?.[1];diff --git a/server/index.test.ts b/server/index.test.tsindex 65854f8025..10cc2db4b4 100644--- a/server/index.test.ts+++ b/server/index.test.ts@@ -5293,6 +5293,11 @@ describe("harness HTTP API", () => {expect(Buffer.from(await imageWithRange.arrayBuffer()).equals(png)).toBe(true);});+ const imagePromptText = (prompt: { message: { content: string | Array<{ type: string; text?: string }> } }) =>+ typeof prompt.message.content === "string"+ ? prompt.message.content+ : prompt.message.content.filter((part) => part.type === "text").map((part) => part.text ?? "").join("\n");+it("keeps a channel image in its transcript while sending native pixels to the responder", async () => {const created = await api("POST", "/api/bots", {modelSelection: { instanceId: "claude", model: "claude-sonnet-5" },@@ -5337,8 +5342,9 @@ describe("harness HTTP API", () => {text: expect.stringContaining("Describe this image"),});expect(JSON.stringify(dump.prompt)).not.toContain("attached-image");- expect(JSON.stringify(dump.prompt)).toContain("<attached-file");- expect(JSON.stringify(dump.prompt)).toContain(imagePath);+ const channelMessage = imagePromptText(dump.prompt).split("\n").find((line) => line.startsWith("User: "));+ expect(channelMessage).toBeDefined();+ expect(JSON.parse(channelMessage!.slice("User: ".length))).toContain(`<attached-file path="${imagePath}" name="tiny.png" />`);const current = (await api("GET", "/api/bots?messages=20")).body.groups.find((candidate: { id: string }) => candidate.id === room.id,@@ -5372,7 +5378,7 @@ describe("harness HTTP API", () => {});expect(uploaded.status).toBe(201);const { path: imagePath } = await uploaded.json() as { path: string };- type Dump = { prompt: { message: { content: string | Array<{ type: string; source?: { data?: string } }> } } };+ type Dump = { prompt: { message: { content: string | Array<{ type: string; text?: string; source?: { data?: string } }> } } };const imageBlocks = (dump: Dump) => {const { content } = dump.prompt.message;return typeof content === "string" ? [] : content.filter((part) => part.type === "image");@@ -5401,8 +5407,7 @@ 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)).toContain("<attached-file");- expect(JSON.stringify(rewound.prompt)).toContain(imagePath);+ expect(imagePromptText(rewound.prompt)).toContain(`<attached-file path="${imagePath}" name="tiny.png" />`);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 4b545d2119..341b88f3ae 100644--- a/server/turn-images.test.ts+++ b/server/turn-images.test.ts@@ -62,6 +62,17 @@ describe("turn image admission", () => {expect(result.text).toContain(root);});+ it("emits the admitted path while preserving the encoded display name", () => {+ const root = store("&uploads");+ const path = image(root, 0);+ const encoded = path.replaceAll("&", "&");+ const raw = `${root}/missing/../${IDS[0]}.png`.replaceAll("&", "&");+ const result = extractTurnImages(`<attached-image path="${raw}" name="mug & "cup".png" />`, { attachmentsDir: root });++ expect(result.text).toBe(`<attached-file path="${encoded}" name="mug & "cup".png" />`);+ expect(result.images).toEqual([{ path, mime: "image/png", bytes: 4 }]);+ });+it("leaves inline, fenced, missing, outside-store, and non-generated tags as text", () => {const root = store();const outside = store("-outside");diff --git a/server/turn-images.ts b/server/turn-images.tsindex 7e901a245e..eb4fb3c6ff 100644--- a/server/turn-images.ts+++ b/server/turn-images.ts@@ -6,6 +6,7 @@ import { fromMarkdown } from "mdast-util-from-markdown";import { ATTACHMENTS_DIR, FILE_MAX_BYTES, IMAGE_MAX_BYTES } from "./attachments.ts";import type { TurnImageInput, TurnVideoInput } from "./contracts.ts";import { decodeAttachmentAttribute } from "./message-file.ts";+import { escapeAttribute } from "../src/lib/composer-attachments.ts";import { videoFrameImages } from "./turn-video.ts";export const TURN_IMAGE_MAX_COUNT = 10;@@ -166,9 +167,12 @@ export function extractTurnImages(.slice(0, maxVideoCount);let providerText = text;- 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 imageTags = [...admittedImages].sort((a, b) => b.tag.start - a.tag.start);+ for (const { tag, image } of imageTags) {+ const fileTag = text.slice(tag.start, tag.end).replace(+ /^<attached-image[\t ]+path="[^"\r\n]*"/,+ () => `<attached-file path="${escapeAttribute(image.path)}"`,+ );const reference = options.imageNote ? `${options.imageNote}\n\n${fileTag}` : fileTag;providerText = providerText.slice(0, tag.start) + reference + providerText.slice(tag.end);}