From 3eab346e2c75e7b094f0ae8767e525c42206d70b Mon Sep 17 00:00:00 2001 From: James Long Date: Wed, 4 Nov 2026 22:49:36 +0000 Subject: [PATCH] feat(codemode): suspend and resume runs, with durable tool calls and idempotency keys --- packages/codemode/README.md | 49 ++ packages/codemode/src/codemode.ts | 6 +- packages/codemode/src/interpreter/driver.ts | 8 +- packages/codemode/src/snapshot/codec.ts | 14 +- packages/codemode/src/tool-runtime.ts | 28 +- .../codemode/test/fixtures/resume-process.ts | 16 + packages/codemode/test/suspend.test.ts | 488 ++++++++++++++++++ 7 files changed, 591 insertions(+), 18 deletions(-) create mode 100644 packages/codemode/test/fixtures/resume-process.ts create mode 100644 packages/codemode/test/suspend.test.ts diff --git a/packages/codemode/README.md b/packages/codemode/README.md index 83e2408bd64..bfe506a2457 100644 --- a/packages/codemode/README.md +++ b/packages/codemode/README.md @@ -97,6 +97,55 @@ denies it: the host function is never called, and the program catches the failur fails the way the call itself would: a tool call or asynchronous global rejects its promise, and a synchronous global throws. +### Suspending and resuming a run + +`runtime.start(code, bindings?, { runId? })` starts a run that can be suspended: `run.result` runs it, and +`run.suspend` stops it at the next step boundary and saves its state. Both give the same outcome, a `Result` or: + +```ts +type Suspended = { + ok: "suspended" + state: CodeMode.Snapshot // JSON; CodeMode.toBytes / CodeMode.fromBytes convert it + pending: Array<{ id; path; args; key; status: "queued" | "starting" | "started" }> +} + +const run = runtime.start(source) +const fiber = yield* Effect.forkChild(run.result) +const suspended = yield* run.suspend +// later, in this process or another one built from the same version: +const result = yield* runtime.resume(suspended.state, { [key]: { ok: true, value } }).result +``` + +Suspending waits briefly (`suspend.graceMs`, 50 ms by default) for running tool calls to finish, so their outcomes are +saved. A durable tool's work keeps running and is reattached on resume; any other call still running has an unknown +outcome: on resume it runs again if its tool declares `retry: "safe"`, and otherwise its promise rejects with an error +the program can catch. `resume`'s second argument settles pending calls whose outcome the host already knows, by key. +Every call has an idempotency key, `:`. + +A state resumes only on the same build of this package, with the same extensions; `resume` throws a +`CodeMode.SnapshotError` otherwise, or when the state is malformed. A state larger than `suspend.maxBytes` fails the run +instead of suspending it. Built-ins are saved by name and only their changes are copied. Functions an extension returned have no data form: after a resume, calling one +throws a `TypeError`. `timeoutMs` counts active time across every resume, and `maxToolCalls` counts every call of the run. + +A durable tool runs its work under the call's key and returns a JSON handle: + +```ts +const build = Tool.make({ + description: "Run a build", + input: Schema.Struct({ target: Schema.String }), + output: Schema.Struct({ ok: Schema.Boolean }), + durable: { + start: (input, { key }) => jobs.start(key, input), // launch the work, return a handle quickly + attach: (handle) => jobs.wait(handle), // its output; interrupting this detaches, never cancels + lookup: (key) => jobs.find(key), // a handle for work started before the run was saved, if any + cancel: (handle) => jobs.stop(handle), // a run that ends with the call still running cancels it + }, +}) +``` + +Set `CODEMODE_ROUNDTRIP=1` (`bun run test:roundtrip`) to run the tests with every run saved to bytes and restored into a +brand-new runtime at every host call and every few steps. + ### `Extension.make` Extensions are host functions a program calls directly as globals, such as `fetch`. Unlike tools they are not in the diff --git a/packages/codemode/src/codemode.ts b/packages/codemode/src/codemode.ts index 20330cfd9e3..182be66b40b 100644 --- a/packages/codemode/src/codemode.ts +++ b/packages/codemode/src/codemode.ts @@ -231,7 +231,11 @@ export const make = < bound.add(name) } } - if (options.suspend?.graceMs !== undefined) validateLimit("graceMs" as keyof ExecutionLimits, options.suspend.graceMs, 0) + for (const [name, value] of Object.entries(options.suspend ?? {})) { + if (value !== undefined && (!Number.isSafeInteger(value) || value < 0)) { + throw new RangeError(`suspend.${name} must be a safe integer greater than or equal to 0.`) + } + } const setup: Setup> = { prepared, limits, diff --git a/packages/codemode/src/interpreter/driver.ts b/packages/codemode/src/interpreter/driver.ts index bd141da73cf..54df6df3443 100644 --- a/packages/codemode/src/interpreter/driver.ts +++ b/packages/codemode/src/interpreter/driver.ts @@ -173,10 +173,12 @@ export class Driver { // Starts host work now; its outcome goes back to the machine: a blocking call's continues the job that made it, a // concurrent call's waits in the inbox and settles its promise in a later job, in completion order. private launch(call: HostCall): void { + // A call that was running when its run was suspended was admitted then; running it again is not a new call. + const again = call.status !== "queued" call.status = "starting" const request = call.request if (request.kind === "tool" && this.tools.tool(request.path)?.durable !== undefined) { - return this.fork(call.id, this.tools.start(request.path, request.args, call.key), (handle) => { + return this.fork(call.id, this.tools.start(request.path, request.args, call.key, again), (handle) => { const current = this.machine.call(call.id) if (current === undefined) return current.status = "started" @@ -184,8 +186,8 @@ export class Driver { this.attach(current) }) } - if (request.kind === "tool") return this.fork(call.id, this.tools.execute(request.path, request.args)) - if (request.kind === "search") return this.fork(call.id, this.tools.search(request.args)) + if (request.kind === "tool") return this.fork(call.id, this.tools.execute(request.path, request.args, again)) + if (request.kind === "search") return this.fork(call.id, this.tools.search(request.args, again)) // An extension's work, made by the extension bridge with the program's services in its requirements. return this.fork(call.id, this.machine.ctx.extensionWork!(request.call) as Effect.Effect) } diff --git a/packages/codemode/src/snapshot/codec.ts b/packages/codemode/src/snapshot/codec.ts index 0bdeffc28fb..284a3dc49f9 100644 --- a/packages/codemode/src/snapshot/codec.ts +++ b/packages/codemode/src/snapshot/codec.ts @@ -447,7 +447,7 @@ class Encoder { } if (object instanceof Native) { // A host function made while the program ran: an extension's result. Its name says what was lost. - this.lose(`the function ${String(object.props.get("name") && "value" in object.props.get("name")! ? (object.props.get("name") as { value: unknown }).value : "")}`) + this.lose(`the function ${functionName(object)}`) return { t: "LostNative", ...this.fields(object, byName.get("Fn")!, ["length"]) } } const proto = Object.getPrototypeOf(object) @@ -733,11 +733,10 @@ class Decoder { case "LostNative": { this.fillClass(target, record, byName.get("Fn")!, false, ["length"]) const native = target as unknown as { tag: string; call: unknown; construct: unknown; callback: boolean } - const slot = (target as unknown as Obj).props.get("name") - const name = slot !== undefined && "value" in slot && typeof slot.value === "string" ? slot.value : "" + const name = functionName(target as unknown as Obj) native.tag = "Function" native.call = (_: unknown, __: unknown, site: AstNode | undefined) => { - throw lostError(`The function ${name || "(anonymous)"}`, site) + throw lostError(`The function ${name}`, site) } native.construct = undefined native.callback = true @@ -873,6 +872,13 @@ class Decoder { } } +const functionName = (fn: Obj): string => { + const slot = fn.props.get("name") + return slot !== undefined && "value" in slot && typeof slot.value === "string" && slot.value !== "" + ? slot.value + : "(anonymous)" +} + const base64 = (bytes: Uint8Array): string => { let binary = "" for (let at = 0; at < bytes.length; at += 0x8000) binary += String.fromCharCode(...bytes.subarray(at, at + 0x8000)) diff --git a/packages/codemode/src/tool-runtime.ts b/packages/codemode/src/tool-runtime.ts index ced33035338..0df1ca75713 100644 --- a/packages/codemode/src/tool-runtime.ts +++ b/packages/codemode/src/tool-runtime.ts @@ -375,16 +375,23 @@ export class ToolRuntimeError extends Error { export type ToolRuntime = { readonly calls: Array readonly hooks: Hooks + /** Runs a tool call. `again` runs one admitted before, re-run on resume: it is not counted a second time. */ readonly execute: ( path: ReadonlyArray, args: Array, + again?: boolean, ) => Effect.Effect - readonly search: (args: Array) => Effect.Effect + readonly search: (args: Array, again?: boolean) => Effect.Effect readonly keys: (path: ReadonlyArray) => ReadonlyArray /** The tool at `path`, if there is one, so a caller can see whether it is durable or safe to retry. */ readonly tool: (path: ReadonlyArray) => Tool | undefined /** Admits a call to a durable tool, as `execute` does, and starts its work under `key`: its handle. */ - readonly start: (path: ReadonlyArray, args: Array, key: string) => Effect.Effect + readonly start: ( + path: ReadonlyArray, + args: Array, + key: string, + again?: boolean, + ) => Effect.Effect /** Waits for a durable tool's work: its output, as `execute` gives it. Interrupting it detaches. */ readonly attach: ( path: ReadonlyArray, @@ -454,10 +461,10 @@ export const make = ( catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`), }) - const executeTool = (name: string, tool: Tool, args: Array) => + const executeTool = (name: string, tool: Tool, args: Array, again = false) => Effect.gen(function* () { const input = yield* decode(name, tool, args) - yield* Effect.sync(() => recordCall({ name })) + if (!again) yield* Effect.sync(() => recordCall({ name })) return yield* hooked( { name, input }, hooks["tool.before"], @@ -479,22 +486,23 @@ export const make = ( calls, hooks, keys: (path) => namespaceKeys(root, path), - search: (args) => Effect.suspend(() => executeTool("search", makeSearchTool(prepared.searchIndex), args)), - execute: (path, args) => + search: (args, again) => + Effect.suspend(() => executeTool("search", makeSearchTool(prepared.searchIndex), args, again)), + execute: (path, args, again) => Effect.suspend(() => { const segments = canonicalSegments(path) // Models often write `tools.search(...)` for the bare `search(...)`; honor it unless a tool owns that path. if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined) - return executeTool("search", makeSearchTool(prepared.searchIndex), args) - return executeTool(segments.join("."), resolve(root, path, prepared.searchIndex), args) + return executeTool("search", makeSearchTool(prepared.searchIndex), args, again) + return executeTool(segments.join("."), resolve(root, path, prepared.searchIndex), args, again) }), tool: (path) => lookup(root, canonicalSegments(path))?.tool, - start: (path, args, key) => + start: (path, args, key, again) => Effect.suspend(() => { const found = durableTool(path) return Effect.gen(function* () { const input = yield* decode(found.name, found.tool, args) - yield* Effect.sync(() => recordCall({ name: found.name })) + if (!again) yield* Effect.sync(() => recordCall({ name: found.name })) const before = hooks["tool.before"] if (before !== undefined) yield* before({ name: found.name, input }) return yield* failure(Effect.suspend(() => found.durable.start(input, { key }))) diff --git a/packages/codemode/test/fixtures/resume-process.ts b/packages/codemode/test/fixtures/resume-process.ts new file mode 100644 index 00000000000..43f90e23099 --- /dev/null +++ b/packages/codemode/test/fixtures/resume-process.ts @@ -0,0 +1,16 @@ +// Resumes a saved run in its own process: `bun resume-process.ts `. The `slow` tool answers +// `{ value: input.value * 2 }`; the pending call's outcome is given as `{ value: 20 }`. +import { Effect, Schema } from "effect" +import { CodeMode, Tool } from "../../src/index.js" + +const Value = Schema.Struct({ value: Schema.Number }) +const slow = Tool.make({ + description: "Doubles", + input: Value, + output: Value, + execute: (input) => Effect.succeed({ value: input.value * 2 }), +}) +const [file, key] = process.argv.slice(2) +const bytes = new Uint8Array(await Bun.file(file!).arrayBuffer()) +const run = CodeMode.make({ tools: { slow } }).resume(bytes, { [key!]: { ok: true, value: { value: 20 } } }) +console.log(JSON.stringify(await Effect.runPromise(run.result))) diff --git a/packages/codemode/test/suspend.test.ts b/packages/codemode/test/suspend.test.ts new file mode 100644 index 00000000000..a5db5ec20b7 --- /dev/null +++ b/packages/codemode/test/suspend.test.ts @@ -0,0 +1,488 @@ +import { describe, expect, test } from "bun:test" +import { tmpdir } from "node:os" +import path from "node:path" +import { Effect, Fiber, Schema } from "effect" +import { CodeMode, Extension, Tool } from "../src/index.js" + +// Suspending a run, saving its state, and resuming it in a brand-new runtime. + +const Value = Schema.Struct({ value: Schema.Number }) + +/** A promise and its resolver, for host work a test finishes by hand. */ +const latch = () => { + let resolve!: (value: A) => void + const promise = new Promise((done) => (resolve = done)) + return { promise, resolve } +} + +/** A tool that announces each call and waits for the test to answer it. */ +const manual = (options: { retry?: "safe" | "unsafe" } = {}) => { + const calls: Array<{ input: number; answer: (value: number) => void }> = [] + let called = latch() + const tool = Tool.make({ + description: "Waits for the test", + input: Value, + output: Value, + ...options, + execute: (input) => + Effect.promise(() => { + const answer = latch() + calls.push({ input: input.value, answer: answer.resolve }) + called.resolve() + return answer.promise.then((value) => ({ value })) + }), + }) + return { + tool, + calls, + called: () => called.promise, + reset: () => { + called = latch() + }, + } +} + +/** A durable tool over an in-memory job table that outlives any one run, as a process supervisor would. */ +const durableJobs = (options: { startHangs?: boolean } = {}) => { + const jobs = new Map>; cancelled: boolean }>() + const log: Array = [] + let started = latch() + const tool = Tool.make({ + description: "Runs a job", + input: Value, + output: Value, + durable: { + start: (input, { key }) => + Effect.gen(function* () { + log.push(`start ${key}`) + jobs.set(key, { input: input.value, result: latch(), cancelled: false }) + started.resolve() + if (options.startHangs) yield* Effect.never + return { job: key } + }), + attach: (handle) => + Effect.promise(() => { + const key = (handle as { job: string }).job + log.push(`attach ${key}`) + return jobs.get(key)!.result.promise.then((value) => ({ value })) + }), + lookup: (key) => + Effect.sync(() => { + log.push(`lookup ${key}`) + return jobs.has(key) ? { job: key } : undefined + }), + cancel: (handle) => + Effect.sync(() => { + const key = (handle as { job: string }).job + log.push(`cancel ${key}`) + jobs.get(key)!.cancelled = true + }), + }, + }) + return { + tool, + jobs, + log, + started: () => started.promise, + finish: (key: string, value: number) => jobs.get(key)!.result.resolve(value), + reset: () => { + started = latch() + }, + } +} + +const suspendedOf = (outcome: CodeMode.Result | CodeMode.Suspended): CodeMode.Suspended => { + if (outcome.ok !== "suspended") throw new Error(`expected a suspended run, got ${JSON.stringify(outcome)}`) + return outcome +} + +describe("suspend and resume", () => { + test("a run suspended at a tool call resumes in a fresh runtime, from the state or its bytes", async () => { + const first = manual() + const code = ` + const log = [] + log.push("before") + const a = await tools.slow({ value: 1 }) + log.push("after " + a.value) + console.log("resumed") + return { log, total: a.value + 1 }` + const run = CodeMode.make({ tools: { slow: first.tool } }).start(code, undefined, { runId: "run-1" }) + const fiber = Effect.runFork(run.result) + await first.called() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + // The run's own result is the same saved state. + expect(suspendedOf(await Effect.runPromise(Fiber.join(fiber)))).toBe(suspended) + expect(suspended.pending).toEqual([{ id: 0, path: "slow", args: [{ value: 1 }], key: "run-1:0", status: "starting" }]) + + const bytes = CodeMode.toBytes(suspended.state) + for (const state of [suspended.state, bytes] as const) { + const outcome = await Effect.runPromise( + CodeMode.make({ tools: { slow: first.tool } }).resume(state, { "run-1:0": { ok: true, value: { value: 41 } } }) + .result, + ) + expect(outcome).toEqual({ + ok: true, + value: { log: ["before", "after 41"], total: 42 }, + logs: ["resumed"], + toolCalls: [{ name: "slow" }], + }) + } + }) + + test("a run saved to a file resumes in another process", async () => { + const first = manual() + const run = CodeMode.make({ tools: { slow: first.tool } }).start(` + const seen = new Map([["first", await tools.slow({ value: 10 })]]) + const second = await tools.slow({ value: 21 }) + return [seen.get("first").value, second.value]`) + Effect.runFork(run.result) + await first.called() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + const file = path.join(tmpdir(), `codemode-${crypto.randomUUID()}.state`) + await Bun.write(file, CodeMode.toBytes(suspended.state)) + try { + const child = Bun.spawn(["bun", path.join(import.meta.dir, "fixtures/resume-process.ts"), file, suspended.pending[0]!.key], { + stdout: "pipe", + stderr: "inherit", + }) + const output = await new Response(child.stdout).text() + expect(await child.exited).toBe(0) + expect(JSON.parse(output)).toEqual({ ok: true, value: [20, 42], toolCalls: [{ name: "slow" }, { name: "slow" }] }) + } finally { + await Bun.file(file).delete() + } + }) + + test("a suspend before the run starts saves its initial state", async () => { + const runtime = CodeMode.make({ tools: {} }) + const run = runtime.start(`let n = 0; for (let i = 0; i < 10; i++) n += i; return n`) + const suspended = suspendedOf(await Effect.runPromise(Effect.andThen(Effect.forkChild(run.result), run.suspend))) + expect(suspended.pending).toEqual([]) + expect(await Effect.runPromise(runtime.resume(suspended.state).result)).toEqual({ + ok: true, + value: 45, + toolCalls: [], + }) + }) + + test("a suspended durable tool's work keeps running and is reattached by its handle", async () => { + const jobs = durableJobs() + const runtime = CodeMode.make({ tools: { job: jobs.tool } }) + const run = runtime.start(`const r = await tools.job({ value: 2 }); return r.value * 10`, undefined, { + runId: "d", + }) + Effect.runFork(run.result) + await jobs.started() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + expect(suspended.pending).toEqual([{ id: 0, path: "job", args: [{ value: 2 }], key: "d:0", status: "started" }]) + // Detached, not cancelled: the work finishes while nothing waits on it. + expect(jobs.jobs.get("d:0")!.cancelled).toBe(false) + jobs.finish("d:0", 7) + const outcome = await Effect.runPromise( + CodeMode.make({ tools: { job: jobs.tool } }).resume(CodeMode.toBytes(suspended.state)).result, + ) + expect(outcome).toEqual({ ok: true, value: 70, toolCalls: [{ name: "job" }] }) + expect(jobs.log).toEqual(["start d:0", "attach d:0", "attach d:0"]) + }) + + test("a durable call saved before its start returned is found by key, or started again", async () => { + for (const found of [true, false]) { + const jobs = durableJobs({ startHangs: true }) + const run = CodeMode.make({ tools: { job: jobs.tool }, suspend: { graceMs: 0 } }).start( + `return (await tools.job({ value: 3 })).value`, + undefined, + { runId: "c" }, + ) + Effect.runFork(run.result) + await jobs.started() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + expect(suspended.pending.map((call) => call.status)).toEqual(["starting"]) + // As if the host crashed after start launched the work but before the run was saved again. + if (!found) jobs.jobs.clear() + const fresh = durableJobs() + fresh.jobs.set("c:0", jobs.jobs.get("c:0") ?? { input: 3, result: latch(), cancelled: false }) + if (!found) fresh.jobs.clear() + const resumed = CodeMode.make({ tools: { job: fresh.tool } }).resume(suspended.state) + const result = Effect.runPromise(resumed.result) + if (!found) await fresh.started() + fresh.finish("c:0", found ? 5 : 6) + expect(await result).toEqual({ ok: true, value: found ? 5 : 6, toolCalls: [{ name: "job" }] }) + expect(fresh.log).toEqual(found ? ["lookup c:0", "attach c:0"] : ["lookup c:0", "start c:0", "attach c:0"]) + } + }) + + test("a call in flight when suspended re-runs when safe to retry, and otherwise rejects catchably", async () => { + const code = ` + try { + return { value: (await tools.work({ value: 4 })).value } + } catch (error) { + return { caught: String(error) } + }` + for (const retry of ["safe", "unsafe"] as const) { + const first = manual({ retry }) + const run = CodeMode.make({ tools: { work: first.tool }, suspend: { graceMs: 0 } }).start(code, undefined, { + runId: "r", + }) + Effect.runFork(run.result) + await first.called() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + expect(suspended.pending.map((call) => [call.key, call.status])).toEqual([["r:0", "starting"]]) + const second = manual({ retry }) + const result = Effect.runPromise(CodeMode.make({ tools: { work: second.tool } }).resume(suspended.state).result) + if (retry === "safe") { + await second.called() + expect(second.calls[0]!.input).toBe(4) + second.calls[0]!.answer(9) + expect(await result).toEqual({ ok: true, value: { value: 9 }, toolCalls: [{ name: "work" }] }) + continue + } + expect(second.calls).toEqual([]) + expect(await result).toEqual({ + ok: true, + value: { + caught: + "Error: The call to work was interrupted when the run was suspended and may have partially run; check its effects before calling it again.", + }, + toolCalls: [{ name: "work" }], + }) + } + }) + + test("calls of every kind settle at a suspend inside Promise.all", async () => { + const quick = Tool.make({ + description: "Answers at once", + input: Value, + output: Value, + execute: (input) => Effect.succeed({ value: input.value * 100 }), + }) + const slow = manual() + const safe = manual({ retry: "safe" }) + const jobs = durableJobs() + const tools = { quick, slow: slow.tool, safe: safe.tool, job: jobs.tool } + const code = ` + const settled = await Promise.allSettled([ + tools.quick({ value: 1 }), + tools.slow({ value: 2 }), + tools.safe({ value: 3 }), + tools.job({ value: 4 }), + ]) + return settled.map((item) => item.status === "fulfilled" ? item.value.value : String(item.reason))` + const run = CodeMode.make({ tools, suspend: { graceMs: 20 } }).start(code, undefined, { runId: "m" }) + Effect.runFork(run.result) + await Promise.all([slow.called(), safe.called(), jobs.started()]) + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + // The quick call finished within the grace period, so its outcome is part of the state. + expect(suspended.pending.map((call) => [call.path, call.status])).toEqual([ + ["slow", "starting"], + ["safe", "starting"], + ["job", "started"], + ]) + safe.reset() + const result = Effect.runPromise(CodeMode.make({ tools }).resume(CodeMode.toBytes(suspended.state)).result) + await safe.called() + safe.calls[1]!.answer(30) + jobs.finish("m:3", 40) + expect(await result).toMatchObject({ + ok: true, + value: [ + 100, + "Error: The call to slow was interrupted when the run was suspended and may have partially run; check its effects before calling it again.", + 30, + 40, + ], + }) + }) + + test("active time and tool calls count across resumes", async () => { + const first = manual() + const runtime = CodeMode.make({ tools: { slow: first.tool }, limits: { timeoutMs: 400, maxToolCalls: 1 } }) + const run = runtime.start(` + let n = 0 + const started = Date.now() + while (Date.now() - started < 150) n++ + await tools.slow({ value: 1 }) + await tools.slow({ value: 2 }) + return n`) + Effect.runFork(run.result) + await first.called() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + expect(suspended.state.header.budget.activeMs).toBeGreaterThanOrEqual(150) + expect(suspended.state.header.budget.toolCalls).toBe(1) + // The second call is over the limit the first one used up before the suspend. + const resumed = await Effect.runPromise( + runtime.resume(suspended.state, { [suspended.pending[0]!.key]: { ok: true, value: { value: 1 } } }).result, + ) + expect(resumed).toMatchObject({ ok: false, error: { kind: "ToolCallLimitExceeded" } }) + + const busy = CodeMode.make({ tools: { slow: first.tool }, limits: { timeoutMs: 300 } }) + first.reset() + const spin = busy.start(` + const started = Date.now() + while (Date.now() - started < 200) {} + await tools.slow({ value: 1 }) + const again = Date.now() + while (Date.now() - again < 200) {} + return "done"`) + Effect.runFork(spin.result) + await first.called() + const paused = suspendedOf(await Effect.runPromise(spin.suspend)) + // 200ms were used before the suspend; the next 200ms of work go past the 300ms limit. + const timedOut = await Effect.runPromise( + busy.resume(paused.state, { [paused.pending[0]!.key]: { ok: true, value: { value: 1 } } }).result, + ) + expect(timedOut).toMatchObject({ ok: false, error: { kind: "TimeoutExceeded" } }) + }) + + test("a function an extension returned is lost on resume, and calling it fails clearly", async () => { + const extension = Extension.make({ name: "maker", globals: { make: () => function inc(x: number) { return x + 1 } } }) + const first = manual() + const runtime = CodeMode.make({ tools: { slow: first.tool }, extensions: [extension] }) + const run = runtime.start(` + const inc = make() + const before = inc(1) + await tools.slow({ value: 0 }) + try { + return { before, after: inc(1) } + } catch (error) { + return { before, error: String(error) } + }`) + Effect.runFork(run.result) + await first.called() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + const outcome = await Effect.runPromise( + CodeMode.make({ tools: { slow: first.tool }, extensions: [extension] }).resume(suspended.state, { + [suspended.pending[0]!.key]: { ok: true, value: { value: 0 } }, + }).result, + ) + expect(outcome).toMatchObject({ + ok: true, + value: { + before: 2, + error: + "TypeError: The function inc came from the host and was lost when the run was suspended; get it again after resuming.", + }, + }) + }) + + test("a durable call the program leaves running is cancelled when it ends", async () => { + const jobs = durableJobs() + const outcome = await Effect.runPromise( + CodeMode.make({ tools: { job: jobs.tool } }) + .start(`tools.job({ value: 1 }); return "left"`, undefined, { runId: "e" }) + .result, + ) + expect(outcome).toMatchObject({ ok: true, value: "left" }) + expect(jobs.jobs.get("e:0")!.cancelled).toBe(true) + }) + + test("the snapshot codec does not import Effect", async () => { + for (const file of ["codec.ts", "snapshot.ts"]) { + expect(await Bun.file(`${import.meta.dir}/../src/snapshot/${file}`).text()).not.toMatch(/from "effect"/) + } + }) + + test("a state is refused by another build, other extensions, or a changed source", async () => { + const first = manual() + const runtime = CodeMode.make({ tools: { slow: first.tool } }) + const run = runtime.start(`await tools.slow({ value: 1 }); return 1`) + Effect.runFork(run.result) + await first.called() + const state = suspendedOf(await Effect.runPromise(run.suspend)).state + const header = state.header + expect(() => runtime.resume({ ...state, header: { ...header, build: "0.0.0+0" } })).toThrow( + /saved by another Code Mode build \(0\.0\.0\+0\)/, + ) + const other = CodeMode.make({ extensions: [Extension.make({ name: "x", globals: { f: () => 1 } })] }) + expect(() => other.resume(state)).toThrow("saved by a runtime with other host globals") + const source = header.sources[0]! + expect(() => + runtime.resume({ ...state, header: { ...header, sources: [{ ...source, code: `${source.code} ` }] } }), + ).toThrow("Source main does not match its hash.") + expect(() => runtime.resume(new TextEncoder().encode("{"))).toThrow(CodeMode.SnapshotError) + expect(() => runtime.resume({ ...state, records: [{ t: "Nope" }] })).toThrow("Invalid snapshot") + }) + + test("a state over the byte budget fails the run instead of suspending it", async () => { + const first = manual() + const run = CodeMode.make({ tools: { slow: first.tool }, suspend: { maxBytes: 100 } }).start( + `const big = "x".repeat(1000); await tools.slow({ value: 1 }); return big.length`, + ) + Effect.runFork(run.result) + await first.called() + expect(await Effect.runPromise(run.suspend)).toMatchObject({ + ok: false, + error: { kind: "ExecutionFailure", message: expect.stringContaining("more than the 100-byte limit") }, + }) + }) + + test("program state of every kind survives a suspend", async () => { + const first = manual() + const code = ` + const shared = { n: 1 } + const map = new Map([[shared, "a"], ["k", shared]]) + const set = new Set([1, shared]) + const it = map.keys() + it.next() + const url = new URL("https://example.com/a?x=1") + const params = url.searchParams + const bytes = new Uint8Array([1, 2, 3, 4]) + const view = bytes.subarray(1, 3) + const re = /a(b)/g + re.test("abab") + const date = new Date(0) + const weak = new WeakMap([[shared, 5]]) + function* counter() { let i = 0; while (true) yield i++ } + const gen = counter() + gen.next() + const bound = ((a, b) => a + b).bind(null, 10) + const sparse = [] + sparse[5000] = "far" + const pending = new Promise((resolve) => (globalResolve = resolve)) + var globalResolve + Array.prototype.extra = "patched" + await tools.slow({ value: 1 }) + params.append("y", "2") + view[0] = 9 + globalResolve("later") + return { + same: map.get("k") === shared && set.has(shared) && weak.get(shared) === 5, + next: it.next().value, + url: url.href, + bytes: [...bytes], + lastIndex: re.lastIndex, + date: date.getTime(), + gen: [gen.next().value, gen.next().value], + bound: bound(5), + sparse: [sparse.length, sparse[5000], 4999 in sparse], + pending: await pending, + extra: [].extra, + negative: Object.is(-0, -0 * 1), + }` + const run = CodeMode.make({ tools: { slow: first.tool } }).start(code) + Effect.runFork(run.result) + await first.called() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + const outcome = await Effect.runPromise( + CodeMode.make({ tools: { slow: first.tool } }).resume(CodeMode.toBytes(suspended.state), { + [suspended.pending[0]!.key]: { ok: true, value: { value: 1 } }, + }).result, + ) + expect(outcome).toMatchObject({ + ok: true, + value: { + same: true, + next: "k", + url: "https://example.com/a?x=1&y=2", + bytes: [1, 9, 3, 4], + lastIndex: 2, + date: 0, + gen: [1, 2], + bound: 15, + sparse: [5001, "far", false], + pending: "later", + extra: "patched", + negative: true, + }, + }) + }) +})