Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

test(openai-codex): cover the QuickJS sandbox limits and script stores

Jack Rudenkoa8bfaa659799
diff --git a/packages/cli/src/code-mode/sandbox/quickjs-loader.test.ts b/packages/cli/src/code-mode/sandbox/quickjs-loader.test.ts
new file mode 100644
index 00000000..65188e00
--- /dev/null
+++ b/packages/cli/src/code-mode/sandbox/quickjs-loader.test.ts
@@ -0,0 +1,206 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import { ToolNameTable } from "../tool-names";
+import {
+ type LeasedRuntime,
+ MAX_LIVE_RUNTIMES,
+ type QuickJSModuleLease,
+ RECYCLE_WASM_BYTES,
+ WASM_CEILING_BYTES,
+ currentQuickJSModule,
+ loadQuickJSModule,
+ resetQuickJSLoaderForTests,
+} from "./quickjs-loader";
+import { type ScriptRuntime, createScriptRuntime } from "./script-runtime";
+
+const MiB = 1024 * 1024;
+const runtimes: ScriptRuntime[] = [];
+const participants: Array<{ lease: QuickJSModuleLease; runtime: LeasedRuntime }> = [];
+
+async function lease() {
+ const module = await loadQuickJSModule();
+ if (!module) throw new Error("The isolated QuickJS module must be available");
+ return module;
+}
+
+afterEach(() => {
+ for (const runtime of runtimes.splice(0)) runtime.dispose();
+ for (const item of participants.splice(0)) item.lease.release(item.runtime);
+ resetQuickJSLoaderForTests();
+});
+
+describe("T3 module leases", () => {
+ test("REQ-14 retirement immediately evicts real suspended runtimes and notifies owners", async () => {
+ const module = await lease();
+ const notifications: number[] = [];
+ for (let i = 0; i < 3; i++) {
+ const runtime = createScriptRuntime({
+ module,
+ tools: new ToolNameTable(["X"]),
+ seed: `retire-${i}`,
+ onEvicted: () => {
+ notifications.push(i);
+ },
+ });
+ runtimes.push(runtime);
+ expect(runtime.start("await tools.X({});").kind).toBe("blocked");
+ }
+ expect(module.liveCount).toBe(3);
+
+ module.retire("T3 public lease retirement");
+
+ expect(notifications.sort()).toEqual([0, 1, 2]);
+ for (const runtime of runtimes) {
+ expect(runtime.alive).toBe(false);
+ expect(runtime.lost).toBe(true);
+ }
+ expect(module.liveCount).toBe(0);
+ expect(module.usable).toBe(false);
+ const next = await lease();
+ expect(next).not.toBe(module);
+ expect(next.usable).toBe(true);
+ expect(currentQuickJSModule()).toBe(next);
+ }, 20_000);
+
+ test("REQ-15 retirement preserves a public participant while it is running", async () => {
+ const module = await lease();
+ const notifications: string[] = [];
+ // A public LeasedRuntime participant, not a fabricated wasm module or private-state probe.
+ const running: LeasedRuntime = {
+ lastActive: 1,
+ running: true,
+ evict() {
+ notifications.push("running-evicted");
+ },
+ abandon() {
+ notifications.push("running-abandoned");
+ },
+ };
+ participants.push({ lease: module, runtime: running });
+ expect(module.admit(running)).toBeUndefined();
+
+ module.retire("T3 retire during a running slice");
+
+ expect(notifications).toEqual([]);
+ expect(module.liveCount).toBe(1);
+ expect(module.usable).toBe(false);
+ running.running = false;
+ module.release(running);
+ expect(module.liveCount).toBe(0);
+ }, 20_000);
+
+ test("REQ-15 refuses admission with a string when all 32 slots are running", async () => {
+ const module = await lease();
+ const notifications: string[] = [];
+ const makeParticipant = (i: number): LeasedRuntime => ({
+ lastActive: i,
+ running: true,
+ evict() {
+ notifications.push(`evicted-${i}`);
+ },
+ abandon() {
+ notifications.push(`abandoned-${i}`);
+ },
+ });
+ for (let i = 0; i < 32; i++) {
+ const runtime = makeParticipant(i);
+ participants.push({ lease: module, runtime });
+ expect(module.admit(runtime)).toBeUndefined();
+ }
+
+ const refusal = module.admit(makeParticipant(32));
+
+ expect(typeof refusal).toBe("string");
+ expect(refusal!.length).toBeGreaterThan(0);
+ expect(module.liveCount).toBe(32);
+ expect(notifications).toEqual([]);
+ }, 20_000);
+
+ test("REQ-14 evicts the least-recently-active suspended runtime at the live cap", async () => {
+ const module = await lease();
+ let time = 1;
+ const notifications: number[] = [];
+ for (let i = 0; i < 33; i++) {
+ const runtime = createScriptRuntime({
+ module,
+ tools: new ToolNameTable(["X"]),
+ seed: `cap-${i}`,
+ now: () => time++,
+ onEvicted: () => {
+ notifications.push(i);
+ },
+ });
+ runtimes.push(runtime);
+ expect(runtime.start("await tools.X({});").kind).toBe("blocked");
+ }
+
+ expect(notifications).toEqual([0]);
+ expect(runtimes[0]!.lost).toBe(true);
+ expect(runtimes[0]!.alive).toBe(false);
+ expect(runtimes.slice(1).every((runtime) => runtime.alive)).toBe(true);
+ expect(module.liveCount).toBe(32);
+ }, 20_000);
+
+ test("REQ-16 R2-3b completes an innocent allocation beside holders at the wasm ceiling", async () => {
+ const module = await lease();
+ expect(module.wasmBytes()).toBeGreaterThan(0);
+ const notifications: number[] = [];
+ let holders = 0;
+ // No public wasm-ceiling injection exists. Real holders force the real 256 MiB boundary.
+ while (module.wasmBytes() < WASM_CEILING_BYTES - 32 * MiB && holders < 31) {
+ const id = holders++;
+ const runtime = createScriptRuntime({
+ module,
+ tools: new ToolNameTable(["X"]),
+ seed: `holder-${id}`,
+ onEvicted: () => {
+ notifications.push(id);
+ },
+ });
+ runtimes.push(runtime);
+ expect(
+ runtime.start(`
+ const held = [];
+ for (let i=0; i<8; i++) held.push("h".repeat(1024*1024-32) + i);
+ await tools.X({});
+ text(String(held[0].length));
+ `).kind
+ ).toBe("blocked");
+ expect(module.usable).toBe(true);
+ }
+ expect(module.wasmBytes()).toBeGreaterThanOrEqual(WASM_CEILING_BYTES - 32 * MiB);
+ expect(holders).toBeGreaterThan(1);
+
+ const innocent = createScriptRuntime({
+ module,
+ tools: new ToolNameTable([]),
+ seed: "innocent-allocation",
+ limits: { memoryBytes: 64 * MiB },
+ });
+ runtimes.push(innocent);
+ const step = innocent.start(`
+ const value = "i".repeat(56 * 1024 * 1024);
+ text(String(value.length));
+ `);
+
+ expect(step.kind).toBe("done");
+ if (step.kind !== "done") throw new Error("The allocation must complete without tools");
+ expect(step.output.status).toBe("completed");
+ expect(step.output.parts).toEqual([{ type: "text", text: String(56 * MiB) }]);
+ expect(step.output.error).toBeUndefined();
+ expect(module.wasmBytes()).toBeGreaterThan(WASM_CEILING_BYTES);
+ expect(module.usable).toBe(false);
+ expect(module.liveCount).toBe(0);
+ expect(notifications.sort((a, b) => a - b)).toEqual(
+ Array.from({ length: holders }, (_, i) => i)
+ );
+ const next = await lease();
+ expect(next).not.toBe(module);
+ expect(next.usable).toBe(true);
+ }, 20_000);
+
+ test("REQ-28 exposes the current live cap and wasm retirement limits", () => {
+ expect(MAX_LIVE_RUNTIMES).toBe(32);
+ expect(WASM_CEILING_BYTES).toBe(256 * MiB);
+ expect(RECYCLE_WASM_BYTES).toBe(256 * MiB);
+ }, 20_000);
+});
diff --git a/packages/cli/src/code-mode/sandbox/script-runtime.test.ts b/packages/cli/src/code-mode/sandbox/script-runtime.test.ts
new file mode 100644
index 00000000..91f7915a
--- /dev/null
+++ b/packages/cli/src/code-mode/sandbox/script-runtime.test.ts
@@ -0,0 +1,497 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import { ToolNameTable } from "../tool-names";
+import {
+ currentQuickJSModule,
+ loadQuickJSModule,
+ resetQuickJSLoaderForTests,
+} from "./quickjs-loader";
+import {
+ DEFAULT_SANDBOX_LIMITS,
+ type SandboxLimits,
+ type ScriptRuntime,
+ type ScriptStep,
+ createScriptRuntime,
+} from "./script-runtime";
+
+const MiB = 1024 * 1024;
+const runtimes: ScriptRuntime[] = [];
+
+async function runtime(limits: Partial<SandboxLimits> = {}, seed = "T3-seed") {
+ const module = await loadQuickJSModule();
+ if (!module) throw new Error("REQ-12: the real isolated QuickJS module must load");
+ const result = createScriptRuntime({
+ tools: new ToolNameTable(["X", "Y", "Z"]),
+ limits,
+ seed,
+ module,
+ });
+ runtimes.push(result);
+ return result;
+}
+
+function completed(step: ScriptStep) {
+ expect(step.kind).toBe("done");
+ if (step.kind !== "done") throw new Error("Expected a terminal script step");
+ expect(step.output.status).toBe("completed");
+ return step.output;
+}
+
+function outputText(step: ScriptStep): string {
+ return completed(step)
+ .parts.map((part) => (part.type === "text" ? part.text : "[image]"))
+ .join("");
+}
+
+function blocked(step: ScriptStep) {
+ expect(step.kind).toBe("blocked");
+ if (step.kind !== "blocked") throw new Error("Expected pending tool calls");
+ return step.calls;
+}
+
+function failure(step: ScriptStep) {
+ expect(step.kind).toBe("done");
+ if (step.kind !== "done") throw new Error("Expected a failed script, not a tool batch");
+ expect(step.output.status).toBe("failed");
+ return step.output.error ?? "";
+}
+
+afterEach(() => {
+ for (const item of runtimes.splice(0)) item.dispose();
+ resetQuickJSLoaderForTests();
+});
+
+describe("T3 runtime relay", () => {
+ test("REQ-1 returns three ordered concurrent calls and completes with their results", async () => {
+ const script = await runtime();
+ const calls = blocked(
+ script.start(`
+ const results = await Promise.all([
+ tools.X({position: 0}), tools.Y({position: 1}), tools.Z({position: 2})
+ ]);
+ text(results.join("|"));
+ `)
+ );
+
+ expect(calls).toEqual([
+ { seq: 0, ident: "X", name: "X", input: { position: 0 } },
+ { seq: 1, ident: "Y", name: "Y", input: { position: 1 } },
+ { seq: 2, ident: "Z", name: "Z", input: { position: 2 } },
+ ]);
+ expect(
+ outputText(
+ script.resume([
+ { seq: 2, ok: true, text: "third" },
+ { seq: 0, ok: true, text: "first" },
+ { seq: 1, ok: true, text: "second" },
+ ])
+ )
+ ).toBe("first|second|third");
+ expect(script.alive).toBe(false);
+ }, 20_000);
+
+ test("REQ-2 emits only one call at each sequential await", async () => {
+ const script = await runtime();
+ expect(
+ blocked(
+ script.start(`
+ const first = await tools.X({});
+ const second = await tools.Y({first});
+ text(first + ":" + second);
+ `)
+ )
+ ).toEqual([{ seq: 0, ident: "X", name: "X", input: {} }]);
+
+ expect(blocked(script.resume([{ seq: 0, ok: true, text: "A" }]))).toEqual([
+ { seq: 1, ident: "Y", name: "Y", input: { first: "A" } },
+ ]);
+ expect(outputText(script.resume([{ seq: 1, ok: true, text: "B" }]))).toBe("A:B");
+ }, 20_000);
+
+ test("REQ-3 exposes is_error as a plain-string allSettled rejection", async () => {
+ const script = await runtime();
+ blocked(
+ script.start(`
+ const result = (await Promise.allSettled([tools.X({})]))[0];
+ text(JSON.stringify({status: result.status, reason: result.reason, type: typeof result.reason}));
+ `)
+ );
+
+ expect(
+ JSON.parse(outputText(script.resume([{ seq: 0, ok: false, text: "denied by user" }])))
+ ).toEqual({
+ status: "rejected",
+ reason: "denied by user",
+ type: "string",
+ });
+ }, 20_000);
+
+ test("REQ-3 rejects missing results with the contract's plain string", async () => {
+ const script = await runtime();
+ blocked(script.start("text(JSON.stringify(await Promise.allSettled([tools.X({})])));"));
+
+ expect(JSON.parse(outputText(script.resume([])))).toEqual([
+ { status: "rejected", reason: "no tool_result returned" },
+ ]);
+ }, 20_000);
+
+ test.each(["null", "42", '"value"', "true", "undefined", "[]"])(
+ "REQ-4 rejects non-object argument %s without relaying it",
+ async (argument) => {
+ const script = await runtime();
+ const result = script.start(`
+ const results = await Promise.allSettled([tools.X(${argument})]);
+ text(JSON.stringify(results));
+ `);
+
+ expect(JSON.parse(outputText(result))).toEqual([
+ {
+ status: "rejected",
+ reason: "claudish: tools.X takes an object as its argument; this call was not run",
+ },
+ ]);
+ },
+ 20_000
+ );
+
+ test.each([
+ "new Promise(() => {})",
+ "Promise.resolve({value: 1})",
+ 'Promise.reject("already rejected")',
+ ])(
+ "REQ-4 M8 rejects promise argument %s instead of serializing it",
+ async (argument) => {
+ const script = await runtime();
+ const result = JSON.parse(
+ outputText(
+ script.start(`
+ const results = await Promise.allSettled([tools.X(${argument})]);
+ text(JSON.stringify(results));
+ `)
+ )
+ );
+
+ expect(result).toHaveLength(1);
+ expect(result[0].status).toBe("rejected");
+ expect(typeof result[0].reason).toBe("string");
+ expect(result[0].reason).toContain("was given a promise; await it");
+ },
+ 20_000
+ );
+
+ test("REQ-5 rejects the 33rd concurrent call while the other 32 finish", async () => {
+ const script = await runtime();
+ const calls = blocked(
+ script.start(`
+ const results = await Promise.allSettled(Array.from({length: 33}, (_, i) => tools.X({i})));
+ text(JSON.stringify(results));
+ `)
+ );
+ expect(calls).toHaveLength(32);
+ expect(calls.map((call) => call.seq)).toEqual(Array.from({ length: 32 }, (_, i) => i));
+
+ const results = JSON.parse(
+ outputText(
+ script.resume(
+ calls.map((call) => ({ seq: call.seq, ok: true, text: `result-${call.seq}` }))
+ )
+ )
+ );
+ expect(results.slice(0, 32)).toEqual(
+ Array.from({ length: 32 }, (_, i) => ({
+ status: "fulfilled",
+ value: `result-${i}`,
+ }))
+ );
+ expect(results[32]).toEqual({
+ status: "rejected",
+ reason: "claudish: more than 32 tool calls in one batch; this call was not run",
+ });
+ }, 20_000);
+
+ test("REQ-5 rejects call 257 after 256 sequential calls without wedging", async () => {
+ const script = await runtime();
+ let step = script.start(`
+ for (let i = 0; i < 256; i++) await tools.X({i});
+ try { await tools.X({i: 256}); } catch (reason) { text(reason); }
+ `);
+ for (let seq = 0; seq < 256; seq++) {
+ expect(blocked(step)).toEqual([{ seq, ident: "X", name: "X", input: { i: seq } }]);
+ step = script.resume([{ seq, ok: true, text: "ok" }]);
+ }
+
+ expect(outputText(step)).toBe(
+ "claudish: more than 256 tool calls in one script; this call was not run"
+ );
+ }, 20_000);
+
+ test.each(['tools.X({}); text("finished");', 'tools.X({}); text("finished"); exit();'])(
+ "REQ-6 discards unawaited calls when the script settles: %s",
+ async (source) => {
+ const script = await runtime();
+ expect(outputText(script.start(source))).toBe("finished");
+ expect(script.alive).toBe(false);
+ },
+ 20_000
+ );
+
+ test("REQ-7 fails an await with no tool calls using the exact deadlock text", async () => {
+ const script = await runtime();
+ expect(failure(script.start("await new Promise(() => {});"))).toBe(
+ "deadlocked: the script awaits a promise that can never settle (no tool calls are pending)"
+ );
+ expect(script.alive).toBe(false);
+ }, 20_000);
+});
+
+describe("T3 runtime resource limits", () => {
+ test("REQ-8 interrupts a CPU loop at the default budget", async () => {
+ const script = await runtime();
+ expect(failure(script.start("while (true) {}"))).toContain(
+ "interrupted: exceeded the 2 s CPU budget"
+ );
+ expect(script.alive).toBe(false);
+ }, 20_000);
+
+ test("REQ-8 charges CPU cumulatively across successful tool suspensions", async () => {
+ const script = await runtime({ cpuMs: 200, callsPerScript: 1024 });
+ let step = script.start(`
+ for (let i=0; i<1000; i++) {
+ let total = 0;
+ for (let j=0; j<100000; j++) total += j;
+ await tools.X({i, total});
+ }
+ `);
+ expect(step.kind).toBe("blocked");
+ let resumed = 0;
+ while (step.kind === "blocked" && resumed < 1000) {
+ expect(step.calls).toHaveLength(1);
+ step = script.resume([{ seq: step.calls[0].seq, ok: true, text: "ok" }]);
+ resumed++;
+ }
+
+ expect(resumed).toBeGreaterThan(0);
+ expect(failure(step)).toMatch(/interrupted: exceeded .* CPU budget/);
+ expect(script.alive).toBe(false);
+ }, 20_000);
+
+ test.each([
+ ["toJSON", "throw {toJSON(){while(true){}}};"],
+ ["message getter", "throw {get message(){while(true){}}};"],
+ ["enumerable getter", "throw {get value(){while(true){}}};"],
+ ["Proxy ownKeys", "throw new Proxy({}, {ownKeys(){while(true){}}});"],
+ ["post-await toJSON", "await Promise.resolve(); throw {toJSON(){while(true){}}};"],
+ ])(
+ "REQ-8 K1 bounds hostile settlement through %s",
+ async (_name, source) => {
+ const script = await runtime({ cpuMs: 75 });
+ expect(failure(script.start(source))).toMatch(/interrupted: exceeded .* CPU budget/);
+ expect(script.alive).toBe(false);
+ },
+ 20_000
+ );
+
+ test("REQ-9 fails memory runaway without poisoning an innocent subsequent script", async () => {
+ const script = await runtime({ memoryBytes: 8 * MiB });
+ const error = failure(
+ script.start(`
+ const held = [];
+ while (true) held.push({i: held.length, value: "x".repeat(1024)});
+ `)
+ );
+ expect(error).toContain("out of memory");
+ expect(script.alive).toBe(false);
+
+ const innocent = await runtime();
+ expect(outputText(innocent.start('text("still usable");'))).toBe("still usable");
+ }, 20_000);
+
+ test("REQ-10 fails source beyond the injected source ceiling before running tools", async () => {
+ const script = await runtime({ sourceBytes: 32 });
+ expect(failure(script.start("await tools.X({}); // " + "x".repeat(64))).length).toBeGreaterThan(
+ 0
+ );
+ expect(script.alive).toBe(false);
+ }, 20_000);
+
+ test("REQ-10 rejects an oversized call input with a string instead of relaying it", async () => {
+ const script = await runtime({ callInputBytes: 32 });
+ expect(
+ outputText(
+ script.start(`
+ try { await tools.X({value: "x".repeat(64)}); }
+ catch (error) { text(typeof error); }
+ `)
+ )
+ ).toBe("string");
+ }, 20_000);
+
+ test("REQ-10 rejects just the call beyond a batch's input byte cap", async () => {
+ const script = await runtime({ callInputBytes: 256, batchInputBytes: 64 });
+ const calls = blocked(
+ script.start(`
+ const results = await Promise.allSettled([
+ tools.X({value: "x".repeat(40)}), tools.Y({value: "x".repeat(40)})
+ ]);
+ text(JSON.stringify(results.map(r => r.status)));
+ `)
+ );
+ expect(calls).toHaveLength(1);
+ expect(outputText(script.resume([{ seq: calls[0]!.seq, ok: true, text: "ok" }]))).toBe(
+ '["fulfilled","rejected"]'
+ );
+ }, 20_000);
+
+ test("REQ-10 truncates a too-large result and keeps a truncation marker", async () => {
+ const script = await runtime({ resultBytes: 64 });
+ blocked(script.start("text(await tools.X({}));"));
+ const text = outputText(script.resume([{ seq: 0, ok: true, text: "a".repeat(256) }]));
+ expect(text.startsWith("a")).toBe(true);
+ expect(text).toMatch(/truncat/i);
+ expect(text.length).toBeLessThan(256);
+ }, 20_000);
+
+ test("REQ-10 rejects delivery beyond the cumulative result budget", async () => {
+ const script = await runtime({ resultBytes: 64, resultBytesPerScript: 96 });
+ const calls = blocked(
+ script.start(`
+ const results = await Promise.allSettled([tools.X({}), tools.Y({})]);
+ text(JSON.stringify(results.map(r => r.status)));
+ `)
+ );
+ expect(
+ outputText(
+ script.resume(
+ calls.map((call) => ({
+ seq: call.seq,
+ ok: true,
+ text: "a".repeat(64),
+ }))
+ )
+ )
+ ).toBe('["fulfilled","rejected"]');
+ }, 20_000);
+
+ test("REQ-11 H3 repeated empty text adds no output parts", async () => {
+ const script = await runtime();
+ expect(completed(script.start('for(let i=0;i<4000;i++) text("");')).parts).toEqual([]);
+ }, 20_000);
+
+ test("REQ-11 H3 text beyond the 1024-part cap joins the last part without loss", async () => {
+ const script = await runtime();
+ const output = completed(script.start('for(let i=0;i<2000;i++) text("x");'));
+ expect(output.parts).toHaveLength(1024);
+ expect(output.parts.map((part) => (part.type === "text" ? part.text : "")).join("")).toBe(
+ "x".repeat(2000)
+ );
+ }, 20_000);
+
+ test.each([
+ ["repeated small text", 'for(let i=0;i<1025;i++) text("x".repeat(1024));'],
+ [
+ "tampered slice and apply",
+ `
+ String.prototype.slice = function(){ return String(this); };
+ Reflect.apply = function(){ return "y".repeat(2 * 1024 * 1024); };
+ text("x".repeat(2 * 1024 * 1024));
+ `,
+ ],
+ ])(
+ "REQ-11 enforces the host output ceiling with %s",
+ async (_name, source) => {
+ const script = await runtime();
+ const text = outputText(script.start(source));
+ expect(text.slice(0, MiB)).toBe("x".repeat(MiB));
+ const warning = text.slice(MiB);
+ expect(warning).toMatch(/Warning:.*output.*limit/i);
+ expect(warning.length).toBeLessThan(300);
+ },
+ 20_000
+ );
+
+ test("REQ-11 counts text in UTF-16 code units rather than UTF-8 bytes", async () => {
+ const script = await runtime({ outputBytes: 8 });
+ expect(outputText(script.start('text("éééééééé");'))).toBe("é".repeat(8));
+ }, 20_000);
+});
+
+describe("T3 runtime isolation and ownership", () => {
+ test("REQ-12 makes every ambient I/O and escape-list global undefined", async () => {
+ const script = await runtime();
+ const globals = JSON.parse(
+ outputText(
+ script.start(`
+ text(JSON.stringify([
+ typeof SharedArrayBuffer, typeof Atomics, typeof WeakRef, typeof FinalizationRegistry,
+ typeof setTimeout, typeof console, typeof process, typeof require, typeof fetch,
+ typeof Bun, typeof Deno, typeof XMLHttpRequest, typeof WebSocket
+ ]));
+ `)
+ )
+ );
+ expect(globals).toEqual(Array(13).fill("undefined"));
+ }, 20_000);
+
+ test("REQ-12 constructor escapes remain inside the guest realm and fs import fails", async () => {
+ const script = await runtime();
+ expect(
+ outputText(
+ script.start(`
+ text((function(){return this})().constructor.constructor("return typeof process")());
+ text(tools.X.constructor("return typeof Bun")());
+ try { await import("fs"); text("escaped"); } catch (_) { text("blocked"); }
+ `)
+ )
+ ).toBe("undefinedundefinedblocked");
+ }, 20_000);
+
+ test("REQ-12 produces identical random sequences for the same seed, different for another", async () => {
+ const source = "text(JSON.stringify(Array.from({length: 8}, () => Math.random())));";
+ const a = outputText((await runtime({}, "seed-A")).start(source));
+ const b = outputText((await runtime({}, "seed-A")).start(source));
+ const c = outputText((await runtime({}, "seed-B")).start(source));
+ expect(a).toBe(b);
+ expect(a).not.toBe(c);
+ for (const value of JSON.parse(a)) {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThan(1);
+ }
+ }, 20_000);
+
+ test("REQ-13 disposes 1000 suspended three-call scripts without poisoning the module", async () => {
+ const module = await loadQuickJSModule();
+ if (!module) throw new Error("Sandbox module unavailable");
+ for (let i = 0; i < 1000; i++) {
+ const script = await runtime();
+ expect(
+ blocked(
+ script.start(`
+ await Promise.all([tools.X({}), tools.Y({}), tools.Z({})]);
+ `)
+ )
+ ).toHaveLength(3);
+ script.dispose();
+ script.dispose();
+ expect(script.alive).toBe(false);
+ expect(module.liveCount).toBe(0);
+ }
+ expect(module.poisoned).toBe(false);
+ expect(currentQuickJSModule()).toBe(module);
+ expect(outputText((await runtime()).start('text("healthy");'))).toBe("healthy");
+ }, 20_000);
+
+ test("REQ-28 exposes the documented sandbox policy limits", () => {
+ expect(DEFAULT_SANDBOX_LIMITS).toMatchObject({
+ memoryBytes: 32 * MiB,
+ cpuMs: 2000,
+ sourceBytes: 256 * 1024,
+ callsPerBatch: 32,
+ callsPerScript: 256,
+ callInputBytes: 2 * MiB,
+ batchInputBytes: 8 * MiB,
+ resultBytes: MiB,
+ resultBytesPerScript: 8 * MiB,
+ outputBytes: MiB,
+ outputParts: 1024,
+ });
+ }, 20_000);
+});
diff --git a/packages/cli/src/code-mode/script-store.test.ts b/packages/cli/src/code-mode/script-store.test.ts
new file mode 100644
index 00000000..83bda4fb
--- /dev/null
+++ b/packages/cli/src/code-mode/script-store.test.ts
@@ -0,0 +1,350 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import type { ExecOutput } from "./output";
+import { loadQuickJSModule, resetQuickJSLoaderForTests } from "./sandbox/quickjs-loader";
+import { type ScriptRuntime, createScriptRuntime } from "./sandbox/script-runtime";
+import {
+ DEFAULT_SCRIPT_STORE_LIMITS,
+ type ExecEvent,
+ type ExecState,
+ ScriptStore,
+} from "./script-store";
+import { ToolNameTable } from "./tool-names";
+
+const stores: ScriptStore[] = [];
+const runtimes: ScriptRuntime[] = [];
+const accountKey = "T3-account";
+const sessionKey = "T3-owner";
+let nextId = 0;
+
+function store(options: ConstructorParameters<typeof ScriptStore>[0] = {}) {
+ const instance = new ScriptStore(options);
+ stores.push(instance);
+ return instance;
+}
+
+function record(instance: ScriptStore, state: ExecState = "running", source = "x") {
+ const id = `T3-exec-${nextId++}`;
+ return instance.create({
+ accountKey,
+ sessionKey,
+ execCallId: id,
+ originalCallId: id,
+ source,
+ shape: "classic",
+ state,
+ });
+}
+
+function output(text: string): ExecOutput {
+ return { status: "completed", wallMs: 0, parts: [{ type: "text", text }] };
+}
+
+afterEach(() => {
+ for (const instance of stores.splice(0)) {
+ instance.stopReaper();
+ instance.clear();
+ }
+ for (const runtime of runtimes.splice(0)) runtime.dispose();
+ resetQuickJSLoaderForTests();
+});
+
+const legal: Array<[ExecState, ExecEvent, ExecState]> = [
+ ["running", "block", "suspended"],
+ ["running", "finish", "completed"],
+ ["running", "fail", "failed"],
+ ["suspended", "resume", "running"],
+ ["suspended", "terminate", "terminated"],
+ ["suspended", "abandon", "terminated"],
+ ["suspended", "evict", "lost"],
+ ["suspended", "expire", "lost"],
+ ["lost", "answer", "failed"],
+ ["lost", "replay", "running"],
+ ["lost", "terminate", "terminated"],
+ ["completed", "answer", "completed"],
+ ["failed", "answer", "failed"],
+ ["terminated", "answer", "terminated"],
+];
+
+describe("T3 script store state machine", () => {
+ test.each(legal)("REQ-17 REQ-24 transitions %s + %s to %s", (state, event, destination) => {
+ const instance = store();
+ const item = record(instance, state);
+
+ expect(instance.transition(item, event)).toBe(destination);
+ expect(item.state).toBe(destination);
+ expect(instance.peek(accountKey, item.execCallId)?.state).toBe(destination);
+ });
+
+ test("REQ-18 coerces every illegal state/event pair to lost without throwing", () => {
+ const states: ExecState[] = [
+ "running",
+ "suspended",
+ "lost",
+ "completed",
+ "failed",
+ "terminated",
+ ];
+ const events: ExecEvent[] = [
+ "block",
+ "finish",
+ "fail",
+ "resume",
+ "terminate",
+ "abandon",
+ "evict",
+ "expire",
+ "answer",
+ "replay",
+ ];
+ const instance = store();
+ let checked = 0;
+ for (const state of states) {
+ for (const event of events) {
+ if (legal.some(([from, on]) => from === state && on === event)) continue;
+ const item = record(instance, state);
+ expect(() => instance.transition(item, event)).not.toThrow();
+ expect(item.state).toBe("lost");
+ checked++;
+ }
+ }
+ expect(checked).toBe(46);
+ });
+
+ test("REQ-19 H2 peek sees lost immediately after a real runtime is evicted", async () => {
+ const module = await loadQuickJSModule();
+ if (!module) throw new Error("Sandbox module unavailable");
+ const instance = store();
+ const item = record(instance);
+ const runtime = createScriptRuntime({
+ module,
+ seed: "immediate-eviction",
+ tools: new ToolNameTable(["X"]),
+ onEvicted: () => instance.loseIfRuntimeGone(item),
+ });
+ runtimes.push(runtime);
+ instance.attachRuntime(item, runtime);
+ expect(runtime.start("await tools.X({});").kind).toBe("blocked");
+ instance.transition(item, "block");
+
+ module.retire("T3 immediate store loss");
+
+ // peek is deliberately used: get/reap must not be needed to notice the eviction.
+ expect(instance.peek(accountKey, item.execCallId)?.state).toBe("lost");
+ expect(runtime.alive).toBe(false);
+ expect(runtime.lost).toBe(true);
+ expect(instance.bytes).toBe(item.bytes);
+ }, 20_000);
+
+ test("REQ-20 expires suspended state using an injected clock without sleeping", async () => {
+ const module = await loadQuickJSModule();
+ if (!module) throw new Error("Sandbox module unavailable");
+ let now = 100;
+ const instance = store({ limits: { suspendedTtlMs: 60 }, now: () => now });
+ const item = record(instance);
+ const runtime = createScriptRuntime({
+ module,
+ seed: "ttl",
+ tools: new ToolNameTable(["X"]),
+ });
+ runtimes.push(runtime);
+ instance.attachRuntime(item, runtime);
+ expect(runtime.start("await tools.X({});").kind).toBe("blocked");
+ instance.transition(item, "block");
+
+ now = 159;
+ instance.reap();
+ expect(instance.peek(accountKey, item.execCallId)?.state).toBe("suspended");
+ expect(runtime.alive).toBe(true);
+ now = 161;
+ instance.reap();
+ expect(instance.peek(accountKey, item.execCallId)?.state).toBe("lost");
+ expect(runtime.alive).toBe(false);
+ }, 20_000);
+});
+
+describe("T3 script store capacity and accounting", () => {
+ test("REQ-21 evicts settled records by LRU under a small byte cap", () => {
+ let now = 0;
+ const instance = store({ limits: { maxBytes: 25 }, now: () => now });
+ const first = record(instance, "completed", "a".repeat(10));
+ now++;
+ const second = record(instance, "completed", "b".repeat(10));
+ now++;
+ expect(instance.get(accountKey, first.execCallId)).toBe(first);
+ now++;
+ const third = record(instance, "completed", "c".repeat(10));
+ instance.reap();
+
+ expect(instance.peek(accountKey, first.execCallId)).toBe(first);
+ expect(instance.peek(accountKey, second.execCallId)).toBeUndefined();
+ expect(instance.peek(accountKey, third.execCallId)).toBe(third);
+ expect(instance.isEvicted(accountKey, second.execCallId)).toBe(true);
+ expect(instance.bytes).toBe(20);
+ expect(instance.bytes).toBeLessThanOrEqual(25);
+ });
+
+ test("REQ-21 enforces the real 32 MiB settled ceiling and preserves a recently touched output", () => {
+ let now = 0;
+ const instance = store({ now: () => now++ });
+ const items = [];
+ const answer = output("x".repeat(1024 * 1024));
+ for (let i = 0; i < 34; i++) {
+ const item = record(instance, "completed");
+ items.push(item);
+ instance.storeOutput(item, "completion", answer, "run", sessionKey);
+ expect(instance.bytes).toBeLessThanOrEqual(32 * 1024 * 1024);
+ if (i === 30) expect(instance.get(accountKey, items[0]!.execCallId)).toBe(items[0]);
+ }
+
+ expect(instance.size).toBe(31);
+ expect(instance.peek(accountKey, items[0]!.execCallId)).toBe(items[0]);
+ expect(instance.isEvicted(accountKey, items[1]!.execCallId)).toBe(true);
+ expect(
+ instance.lookupOutput(accountKey, items[33]!.execCallId, "completion", sessionKey)?.output
+ ).toEqual(answer);
+ });
+
+ test("REQ-21 enforces record count separately from bytes and bounds eviction ids", () => {
+ let now = 0;
+ const instance = store({
+ limits: { maxRecords: 1, maxBytes: 1000, maxEvictedIds: 2 },
+ now: () => now++,
+ });
+ const items = Array.from({ length: 5 }, () => record(instance, "completed"));
+ instance.reap();
+
+ expect(instance.size).toBe(1);
+ expect(instance.peek(accountKey, items[4]!.execCallId)).toBe(items[4]);
+ expect(instance.isEvicted(accountKey, items[0]!.execCallId)).toBe(false);
+ expect(instance.isEvicted(accountKey, items[2]!.execCallId)).toBe(true);
+ expect(instance.isEvicted(accountKey, items[3]!.execCallId)).toBe(true);
+ });
+
+ test("REQ-22 counts UTF-8 source/output and 64 bytes per output part", () => {
+ const instance = store();
+ const item = record(instance, "completed", "é");
+ const answer: ExecOutput = {
+ status: "completed",
+ wallMs: 0,
+ parts: [
+ { type: "text", text: "é" },
+ { type: "text", text: "" },
+ ],
+ };
+ instance.storeOutput(item, "completion", answer, "run", sessionKey);
+
+ expect(item.bytes).toBe(2 + 2 + 2 * 64);
+ expect(instance.bytes).toBe(item.bytes);
+ instance.clear();
+ expect(instance.bytes).toBe(0);
+ expect(instance.size).toBe(0);
+ });
+
+ test("REQ-22 R2-2 counts repair ids and both 64-character hashes after settlement and answer", () => {
+ const instance = store();
+ const item = record(instance, "running", "x");
+ const repairs = Object.fromEntries(
+ Array.from({ length: 10 }, (_, i) => [
+ `nested-${i}`,
+ { script: "a".repeat(64), relayed: "b".repeat(64) },
+ ])
+ );
+ item.batches.push({ ids: Object.keys(repairs), repairs });
+ const expected =
+ 1 + Object.keys(repairs).reduce((sum, id) => sum + Buffer.byteLength(id) + 128, 0);
+
+ instance.transition(item, "finish");
+ expect(item.bytes).toBe(expected);
+ expect(instance.bytes).toBe(expected);
+ instance.transition(item, "answer");
+ expect(item.bytes).toBe(expected);
+ expect(instance.bytes).toBe(expected);
+ });
+
+ test("REQ-22 repair bytes can evict a record instead of bypassing the settled byte ceiling", () => {
+ const instance = store({ limits: { maxBytes: 200 } });
+ const item = record(instance);
+ item.batches.push({
+ ids: ["nested-0", "nested-1"],
+ repairs: {
+ "nested-0": { script: "a".repeat(64), relayed: "b".repeat(64) },
+ "nested-1": { script: "c".repeat(64), relayed: "d".repeat(64) },
+ },
+ });
+
+ instance.transition(item, "finish");
+ instance.reap();
+
+ expect(instance.peek(accountKey, item.execCallId)).toBeUndefined();
+ expect(instance.isEvicted(accountKey, item.execCallId)).toBe(true);
+ expect(instance.bytes).toBeLessThanOrEqual(200);
+ });
+
+ test("REQ-28 exposes documented script-store limits", () => {
+ expect(DEFAULT_SCRIPT_STORE_LIMITS).toEqual({
+ maxRecords: 2000,
+ maxBytes: 32 * 1024 * 1024,
+ suspendedTtlMs: 6 * 60 * 60 * 1000,
+ maxEvictedIds: 20_000,
+ reapIntervalMs: 60_000,
+ });
+ });
+});
+
+describe("T3 stored output precedence", () => {
+ test("REQ-23 never overwrites a run output with a later tombstone", () => {
+ const instance = store();
+ const item = record(instance, "completed");
+ const run = output("run output");
+ instance.storeOutput(item, "same-history", run, "run", sessionKey);
+ const before = instance.bytes;
+
+ instance.storeOutput(item, "same-history", output("fork lost state"), "tombstone", "T3-fork");
+
+ for (const session of [sessionKey, "T3-fork", "T3-third-session"]) {
+ expect(instance.lookupOutput(accountKey, item.execCallId, "same-history", session)).toEqual({
+ output: run,
+ origin: "run",
+ sessionKey,
+ });
+ }
+ expect(instance.bytes).toBe(before);
+ });
+
+ test("REQ-23 replaces a tombstone with the eventual run output for every session", () => {
+ const instance = store();
+ const item = record(instance, "completed");
+ const run = output("owner completed");
+ instance.storeOutput(item, "same-history", output("fork tombstone"), "tombstone", "T3-fork");
+ instance.storeOutput(item, "same-history", run, "run", sessionKey);
+
+ expect(instance.lookupOutput(accountKey, item.execCallId, "same-history", "T3-fork")).toEqual({
+ output: run,
+ origin: "run",
+ sessionKey,
+ });
+ expect(
+ instance.lookupOutput(accountKey, item.execCallId, "same-history", sessionKey)?.output
+ ).toEqual(run);
+ });
+
+ test("REQ-23 scopes tombstones by session and records by account and completion key", () => {
+ const instance = store();
+ const item = record(instance, "failed");
+ const tombstone = output("lost state");
+ instance.storeOutput(item, "completion-A", tombstone, "tombstone", "T3-fork");
+
+ expect(
+ instance.lookupOutput(accountKey, item.execCallId, "completion-A", "T3-fork")?.output
+ ).toEqual(tombstone);
+ expect(
+ instance.lookupOutput(accountKey, item.execCallId, "completion-A", sessionKey)
+ ).toBeUndefined();
+ expect(
+ instance.lookupOutput("other-account", item.execCallId, "completion-A", "T3-fork")
+ ).toBeUndefined();
+ expect(
+ instance.lookupOutput(accountKey, item.execCallId, "completion-B", "T3-fork")
+ ).toBeUndefined();
+ });
+});
diff --git a/packages/cli/src/code-mode/stores.test.ts b/packages/cli/src/code-mode/stores.test.ts
new file mode 100644
index 00000000..21743fc3
--- /dev/null
+++ b/packages/cli/src/code-mode/stores.test.ts
@@ -0,0 +1,35 @@
+import { afterEach, expect, test } from "bun:test";
+import { getCodeModeStores, peekCodeModeStores, resetCodeModeStores } from "./stores";
+
+afterEach(() => resetCodeModeStores());
+
+test("REQ-27 creates stores lazily, reuses the active set and resets without retaining data", () => {
+ resetCodeModeStores();
+ expect(peekCodeModeStores()).toBeUndefined();
+ const first = getCodeModeStores();
+ expect(getCodeModeStores()).toBe(first);
+ expect(peekCodeModeStores()).toBe(first);
+ first.scripts.pin("T3-session");
+ first.scripts.create({
+ accountKey: "T3-account",
+ sessionKey: "T3-session",
+ execCallId: "T3-exec",
+ originalCallId: "T3-exec",
+ source: "x",
+ shape: "classic",
+ state: "completed",
+ });
+ expect(first.turns.put("T3-session", "T3-anchor", [])).toBe(true);
+
+ resetCodeModeStores();
+
+ expect(peekCodeModeStores()).toBeUndefined();
+ const second = getCodeModeStores();
+ expect(second).not.toBe(first);
+ expect(second.scripts.size).toBe(0);
+ expect(second.scripts.bytes).toBe(0);
+ expect(second.scripts.isPinned("T3-session")).toBe(false);
+ expect(second.turns.size).toBe(0);
+ expect(second.turns.bytes).toBe(0);
+ expect(second.turns.get("T3-session", "T3-anchor")).toBeUndefined();
+});
diff --git a/packages/cli/src/code-mode/turn-records.test.ts b/packages/cli/src/code-mode/turn-records.test.ts
new file mode 100644
index 00000000..3b4826fa
--- /dev/null
+++ b/packages/cli/src/code-mode/turn-records.test.ts
@@ -0,0 +1,210 @@
+import { describe, expect, test } from "bun:test";
+import { DEFAULT_TURN_RECORD_LIMITS, TurnRecords, turnAnchorFor } from "./turn-records";
+import type { ResponsesItem } from "./types";
+
+const KiB = 1024;
+const MiB = 1024 * KiB;
+
+// Deliberately opaque structural store values. These are not invented Responses events,
+// upstream request bodies or protocol captures: TurnRecords promises JSON byte storage.
+function payload(text: string): ResponsesItem[] {
+ return [{ type: "T3-opaque-store-value", text }];
+}
+const bytes = (items: ResponsesItem[]) => Buffer.byteLength(JSON.stringify(items), "utf8");
+
+describe("T3 turn record storage", () => {
+ test("REQ-25 counts exact UTF-8 JSON bytes and returns the stored record", () => {
+ const records = new TurnRecords();
+ const items = payload("é🙂");
+
+ expect(records.put("session", "anchor", items)).toBe(true);
+ expect(records.get("session", "anchor")).toEqual({
+ sessionKey: "session",
+ anchor: "anchor",
+ items,
+ bytes: bytes(items),
+ });
+ expect(records.bytes).toBe(bytes(items));
+ expect(records.size).toBe(1);
+ expect(records.get("other-session", "anchor")).toBeUndefined();
+ records.clear();
+ expect(records.bytes).toBe(0);
+ expect(records.size).toBe(0);
+ });
+
+ test("REQ-25 accepts exactly the per-record byte ceiling and refuses one extra byte", () => {
+ const exact = payload("x".repeat(128));
+ const limit = bytes(exact);
+ const records = new TurnRecords({ limits: { maxRecordBytes: limit, maxBytes: 4 * limit } });
+
+ expect(records.put("session", "exact", exact)).toBe(true);
+ expect(records.put("session", "oversize", payload("x".repeat(129)))).toBe(false);
+ expect(records.get("session", "oversize")).toBeUndefined();
+ expect(records.get("session", "exact")?.items).toEqual(exact);
+ expect(records.bytes).toBe(limit);
+ expect(records.size).toBe(1);
+ });
+
+ test("REQ-25 evicts the least recently rendered record under the byte cap", () => {
+ const items = payload("x".repeat(100));
+ const records = new TurnRecords({
+ limits: { maxBytes: 2 * bytes(items), maxRecordBytes: 1000 },
+ });
+ records.put("session", "first", items);
+ records.put("session", "second", items);
+ expect(records.get("session", "first")).toBeDefined();
+
+ records.put("session", "third", items);
+
+ expect(records.get("session", "first")).toBeDefined();
+ expect(records.get("session", "second")).toBeUndefined();
+ expect(records.get("session", "third")).toBeDefined();
+ expect(records.isEvicted("session", "second")).toBe(true);
+ expect(records.isEvicted("other-session", "second")).toBe(false);
+ expect(records.bytes).toBe(2 * bytes(items));
+ });
+
+ test("REQ-25 separately enforces record count and FIFO eviction-id retention", () => {
+ const records = new TurnRecords({ limits: { maxRecords: 1, maxEvictedIds: 2 } });
+ for (let i = 0; i < 5; i++) expect(records.put("session", `anchor-${i}`, [])).toBe(true);
+
+ expect(records.size).toBe(1);
+ expect(records.get("session", "anchor-4")).toBeDefined();
+ expect(records.isEvicted("session", "anchor-0")).toBe(false);
+ expect(records.isEvicted("session", "anchor-2")).toBe(true);
+ expect(records.isEvicted("session", "anchor-3")).toBe(true);
+ });
+
+ test("REQ-25 replacement updates byte totals rather than charging both versions", () => {
+ const records = new TurnRecords();
+ records.put("session", "same", payload("old"));
+ const replacement = payload("new and longer é");
+
+ expect(records.put("session", "same", replacement)).toBe(true);
+
+ expect(records.size).toBe(1);
+ expect(records.bytes).toBe(bytes(replacement));
+ expect(records.get("session", "same")?.items).toEqual(replacement);
+ });
+
+ test("REQ-25 keeps bytes bounded for 2000 records with six scaled interstitial outputs each", () => {
+ const items = Array.from({ length: 6 }, () => ({
+ type: "T3-opaque-store-value",
+ text: "x".repeat(KiB),
+ }));
+ const records = new TurnRecords({ limits: { maxBytes: 32 * KiB, maxRecordBytes: 8 * KiB } });
+ for (let i = 0; i < 2000; i++) {
+ expect(records.put("session", `anchor-${i}`, items)).toBe(true);
+ expect(records.bytes).toBeLessThanOrEqual(32 * KiB);
+ }
+
+ expect(records.get("session", "anchor-1999")).toBeDefined();
+ expect(records.isEvicted("session", "anchor-0")).toBe(true);
+ expect(records.size).toBeLessThan(6);
+ }, 20_000);
+
+ test("REQ-25 enforces the real 32 MiB byte cap on six-MiB output records", () => {
+ const items = Array.from({ length: 6 }, () => ({
+ type: "T3-opaque-store-value",
+ text: "x".repeat(MiB),
+ }));
+ const records = new TurnRecords();
+ for (let i = 0; i < 8; i++) {
+ expect(records.put("session", `anchor-${i}`, items)).toBe(true);
+ expect(records.bytes).toBeLessThanOrEqual(32 * MiB);
+ }
+
+ expect(records.isEvicted("session", "anchor-0")).toBe(true);
+ expect(records.get("session", "anchor-7")).toBeDefined();
+ expect(records.size).toBe(5);
+ }, 20_000);
+
+ test("REQ-25 refuses a default over-eight-MiB record without retaining its data", () => {
+ const records = new TurnRecords();
+
+ expect(records.put("session", "oversize", payload("x".repeat(8 * MiB)))).toBe(false);
+
+ expect(records.get("session", "oversize")).toBeUndefined();
+ expect(records.size).toBe(0);
+ expect(records.bytes).toBe(0);
+ }, 20_000);
+
+ test("REQ-28 exposes the documented turn-record limits", () => {
+ expect(DEFAULT_TURN_RECORD_LIMITS).toEqual({
+ maxRecords: 2000,
+ maxBytes: 32 * MiB,
+ maxRecordBytes: 8 * MiB,
+ maxEvictedIds: 20_000,
+ });
+ });
+});
+
+describe("T3 turn anchors", () => {
+ // Structural Claude Code message arrays, as specified in architecture.md §5.8.
+ const assistant = {
+ role: "assistant" as const,
+ content: [{ type: "text" as const, text: "same answer" }],
+ };
+ const previous = {
+ role: "user" as const,
+ content: [{ type: "text" as const, text: "question" }],
+ };
+
+ test("REQ-26 cache_control on the previous message does not change the anchor", () => {
+ const marked = {
+ ...previous,
+ cache_control: { type: "ephemeral" },
+ content: previous.content.map((block) => ({
+ ...block,
+ cache_control: { type: "ephemeral" },
+ })),
+ };
+
+ expect(turnAnchorFor("session", assistant, marked)).toBe(
+ turnAnchorFor("session", assistant, previous)
+ );
+ });
+
+ test("REQ-26 different previous prompts and different sessions do not share text anchors", () => {
+ const anchor = turnAnchorFor("session", assistant, previous);
+ expect(
+ turnAnchorFor("session", assistant, {
+ role: "user",
+ content: [{ type: "text", text: "another question" }],
+ })
+ ).not.toBe(anchor);
+ expect(turnAnchorFor("another-session", assistant, previous)).not.toBe(anchor);
+ expect(turnAnchorFor("session", assistant, undefined)).not.toBe(anchor);
+ });
+
+ test("REQ-26 uses the first tool_use id rather than text or the previous message", () => {
+ const message = {
+ role: "assistant" as const,
+ content: [
+ { type: "text" as const, text: "explanation" },
+ { type: "tool_use" as const, id: "T3-first-tool", name: "X", input: {} },
+ { type: "tool_use" as const, id: "T3-second-tool", name: "Y", input: {} },
+ ],
+ };
+
+ expect(turnAnchorFor("session", message, previous)).toBe("T3-first-tool");
+ });
+
+ test("REQ-26 canonicalizes trimmed text blocks joined by newlines", () => {
+ const split = {
+ role: "assistant" as const,
+ content: [
+ { type: "text" as const, text: " first " },
+ { type: "text" as const, text: " second " },
+ ],
+ };
+ const joined = {
+ role: "assistant" as const,
+ content: [{ type: "text" as const, text: "first\nsecond" }],
+ };
+
+ expect(turnAnchorFor("session", split, previous)).toBe(
+ turnAnchorFor("session", joined, previous)
+ );
+ });
+});