Public: anyone can read this; the crew can change it.
The real-cell test builds the template's hello app with the OpenAI-compatible runner against a stub endpoint #12517
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/12125-the-real-cell-test-builds-the-template-s → main 1 commit fork
2 files
changed +137
−0
test/fixtures/openai-stub.ts added +44
| @@ -0,0 +1,44 @@ | ||
| 1 | /** | |
| 2 | * A scripted OpenAI-compatible endpoint (#12125): `POST /v1/chat/completions` | |
| 3 | * answers the n-th request with the n-th step of a script, as a model that | |
| 4 | * already knows what it will do. A step is a list of tool calls and/or text; | |
| 5 | * it may read the conversation so far (the tool results the runner sent back). | |
| 6 | * No network beyond loopback, no key, no cost, the same answers every time. | |
| 7 | * The runner under test is `src/crew/openai_runner.ts`; this is its model. | |
| 8 | */ | |
| 9 | ||
| 10 | export interface ToolUse { readonly name: string; readonly args: Record<string, unknown> } | |
| 11 | export interface Step { readonly say?: string; readonly use?: readonly ToolUse[] } | |
| 12 | export type Message = { role: string; content?: string | null; tool_call_id?: string }; | |
| 13 | ||
| 14 | export interface Stub { | |
| 15 | readonly base: string; | |
| 16 | /** Every request body the runner sent, in order. */ | |
| 17 | readonly requests: { messages: Message[]; tools: unknown[]; model: string }[]; | |
| 18 | stop(): void; | |
| 19 | } | |
| 20 | ||
| 21 | /** Starts the stub on a free loopback port; a request past the script's end is answered 500. */ | |
| 22 | export function scriptedEndpoint(script: readonly (Step | ((messages: Message[]) => Step))[]): Stub { | |
| 23 | const requests: Stub["requests"] = []; | |
| 24 | const server = Bun.serve({ | |
| 25 | port: 0, | |
| 26 | hostname: "127.0.0.1", | |
| 27 | async fetch(req) { | |
| 28 | if (req.method !== "POST" || new URL(req.url).pathname !== "/v1/chat/completions") return new Response("not found", { status: 404 }); | |
| 29 | const body = (await req.json()) as Stub["requests"][number]; | |
| 30 | requests.push(body); | |
| 31 | const next = script[requests.length - 1]; | |
| 32 | if (next === undefined) return new Response("script ended", { status: 500 }); | |
| 33 | const step = typeof next === "function" ? next(body.messages) : next; | |
| 34 | const calls = (step.use ?? []).map((u, i) => ({ | |
| 35 | id: `call_${requests.length}_${i}`, | |
| 36 | type: "function", | |
| 37 | function: { name: u.name, arguments: JSON.stringify(u.args) }, | |
| 38 | })); | |
| 39 | const message = { role: "assistant", content: step.say ?? null, ...(calls.length > 0 ? { tool_calls: calls } : {}) }; | |
| 40 | return Response.json({ choices: [{ message }], usage: { prompt_tokens: 10, completion_tokens: 5 } }); | |
| 41 | }, | |
| 42 | }); | |
| 43 | return { base: `http://127.0.0.1:${server.port}/v1`, requests, stop: () => void server.stop(true) }; | |
| 44 | } |
test/openai-real-cell.test.ts added +93
| @@ -0,0 +1,93 @@ | ||
| 1 | /** | |
| 2 | * Proves the second runner builds on the template for real (#12125): the | |
| 3 | * OpenAI-compatible runner, driven by a scripted stub endpoint, adds a hello | |
| 4 | * page to the app the template hands out, runs the app's own tests through its | |
| 5 | * Bash tool inside a real bubblewrap cell, and the app then boots and `/` | |
| 6 | * answers (and so does the new page). Nothing is mocked but the model. | |
| 7 | * | |
| 8 | * It runs where bwrap and socat are and says it skipped where they are not | |
| 9 | * (under FORGE_TEST_SOCKET, as `spawn-cell-e2e.test.ts` does); the forge's | |
| 10 | * check runs it. | |
| 11 | */ | |
| 12 | import { afterAll, expect, test } from "bun:test"; | |
| 13 | import { mkdtempSync, readFileSync, rmSync } from "node:fs"; | |
| 14 | import { tmpdir } from "node:os"; | |
| 15 | import { join } from "node:path"; | |
| 16 | import { sandboxAvailable } from "../src/crew/sandbox.ts"; | |
| 17 | import { openaiRunner } from "../src/crew/openai_runner.ts"; | |
| 18 | import type { AgentRun } from "../src/crew/agent_runner.ts"; | |
| 19 | import { portInsteadOfSocket } from "../src/git/hook-address.ts"; | |
| 20 | import { ANCHOR } from "./helpers/build-standin.ts"; | |
| 21 | import { writeTemplate } from "./helpers/template-app.ts"; | |
| 22 | import { scriptedEndpoint } from "./fixtures/openai-stub.ts"; | |
| 23 | ||
| 24 | const cells = portInsteadOfSocket() ? { ok: false as const, reason: "FORGE_TEST_SOCKET=port: no unix sockets" } : await sandboxAvailable(); | |
| 25 | if (!cells.ok) console.log(`SKIPPING: no real cell here (${cells.reason})`); | |
| 26 | ||
| 27 | const dirs: string[] = []; | |
| 28 | afterAll(() => { for (const d of dirs) rmSync(d, { recursive: true, force: true }); }); | |
| 29 | ||
| 30 | const ROUTE = ` { method: "GET", path: "/hello", handle: () => text("hello from the second runner") },\n\n`; | |
| 31 | const TEST = `import { afterAll, expect, test } from "bun:test"; | |
| 32 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 33 | import { tmpdir } from "node:os"; | |
| 34 | import { join } from "node:path"; | |
| 35 | import { serve } from "./server.ts"; | |
| 36 | ||
| 37 | const dataDir = mkdtempSync(join(tmpdir(), "hello-")); | |
| 38 | const server = serve({ port: 0, dataDir, identityKey: "a-key-only-the-forge-holds" }); | |
| 39 | afterAll(() => { | |
| 40 | server.stop(true); | |
| 41 | rmSync(dataDir, { recursive: true, force: true }); | |
| 42 | }); | |
| 43 | ||
| 44 | test("/hello says hello", async () => { | |
| 45 | const res = await fetch(\`http://\${server.hostname}:\${server.port}/hello\`); | |
| 46 | expect(res.status).toBe(200); | |
| 47 | expect(await res.text()).toBe("hello from the second runner"); | |
| 48 | }); | |
| 49 | `; | |
| 50 | ||
| 51 | test.skipIf(!cells.ok)("the OpenAI-compatible runner builds the template's hello app in a cell, and it boots with / answering", async () => { | |
| 52 | const root = writeTemplate(); | |
| 53 | const tmp = mkdtempSync(join(process.env["TMPDIR"] ?? tmpdir(), "oai-cell-")); | |
| 54 | dirs.push(root, tmp); | |
| 55 | const stub = scriptedEndpoint([ | |
| 56 | { say: "Reading the routes.", use: [{ name: "Read", args: { file_path: "src/routes.ts" } }] }, | |
| 57 | { use: [{ name: "Edit", args: { file_path: "src/routes.ts", old_string: ANCHOR, new_string: ROUTE + ANCHOR } }, { name: "Write", args: { file_path: "src/hello.test.ts", content: TEST } }] }, | |
| 58 | { use: [{ name: "Bash", args: { command: "bun test src/hello.test.ts 2>&1" } }] }, | |
| 59 | { say: "Added /hello; its test passes." }, | |
| 60 | ]); | |
| 61 | try { | |
| 62 | const run: AgentRun = { | |
| 63 | binary: "", cwd: root, prompt: "add a hello page", appendSystemPrompt: "you are a crew member", model: "stub-model", budgetUsd: 1, | |
| 64 | env: { OPENAI_BASE_URL: stub.base, OPENAI_API_KEY: "k-test", HOME: tmp }, | |
| 65 | sandbox: { allowHosts: [], denyRead: [], enforced: true, readAllow: [], tmpDir: tmp }, | |
| 66 | timeoutMs: 120_000, log: () => {}, signal: new AbortController().signal, | |
| 67 | }; | |
| 68 | const out = await openaiRunner.run(run); | |
| 69 | expect(out).toMatchObject({ ok: true, reason: "completed", turns: 4, result: "Added /hello; its test passes." }); | |
| 70 | ||
| 71 | // The shell ran confined and the app's own test passed in it. | |
| 72 | const bash = stub.requests[3]!.messages.at(-1)!.content ?? ""; | |
| 73 | expect(bash).toMatch(/\b1 pass\b/); | |
| 74 | expect(bash).not.toContain("[exit"); | |
| 75 | expect(readFileSync(join(root, "src/routes.ts"), "utf8")).toContain('path: "/hello"'); | |
| 76 | ||
| 77 | // The app the runner built boots, and `/` answers. | |
| 78 | const { serve } = (await import(join(root, "src/server.ts"))) as { serve: (o: { port: number; dataDir: string; identityKey: string }) => { hostname: string; port: number; stop(force?: boolean): void } }; | |
| 79 | const dataDir = mkdtempSync(join(tmp, "data-")); | |
| 80 | const server = serve({ port: 0, dataDir, identityKey: "a-key-only-the-forge-holds" }); | |
| 81 | try { | |
| 82 | const at = `http://${server.hostname}:${server.port}`; | |
| 83 | expect((await fetch(`${at}/`)).status).toBe(200); | |
| 84 | const hello = await fetch(`${at}/hello`); | |
| 85 | expect(hello.status).toBe(200); | |
| 86 | expect(await hello.text()).toBe("hello from the second runner"); | |
| 87 | } finally { | |
| 88 | server.stop(true); | |
| 89 | } | |
| 90 | } finally { | |
| 91 | stub.stop(); | |
| 92 | } | |
| 93 | }, 180_000); |
new above
offline