diff --git a/packages/codemode/src/codemode.ts b/packages/codemode/src/codemode.ts index 3e64f89b2fc..e02e9c6c92e 100644 --- a/packages/codemode/src/codemode.ts +++ b/packages/codemode/src/codemode.ts @@ -4,7 +4,14 @@ import { scanTools } from "./scan.js" import { type Services, type ToolDescription, ToolRuntime } from "./tool-runtime.js" import type { Tools } from "./tools.js" import { fromJson } from "./data.js" -import { finishExecution, resumeProgram, type Running, type Setup, startProgram, SuspendError } from "./runtime/execute.js" +import { + finishExecution, + resumeProgram, + type Running, + type Setup, + startProgram, + SuspendError, +} from "./runtime/execute.js" import { extensionGlobals } from "./runtime/extensions.js" import { globalNames } from "./runtime/globals.js" import { fromBytes, type Snapshot, SnapshotError, type SnapshotHeader, toBytes } from "./snapshot/snapshot.js" @@ -46,8 +53,18 @@ export type ResolvedExecutionLimits = { readonly maxOutputBytes: number | undefined } +/** Options for one run of a runtime's program: `Runtime.execute`, `Runtime.program`, and `Runtime.resume`. */ +export type RunOptions = { + /** + * Which of the runtime's extensions this run gets as globals; all of them by default. Each must be one the runtime + * was made with, so its services are already part of the runtime's environment. A resumed run needs the ones it + * started with. + */ + readonly extensions?: ReadonlyArray +} + /** Options for `Runtime.program`. */ -export type ProgramOptions = { +export type ProgramOptions = RunOptions & { /** Names this run's host calls (`:`); a random UUID by default. */ readonly runId?: string } @@ -157,7 +174,11 @@ export type Outcome = Result | Suspended /** Reusable confined runtime over explicit tools and extensions. */ export type Runtime = { readonly catalog: ReadonlyArray - readonly execute: (code: string, bindings?: Readonly>) => Effect.Effect + readonly execute: ( + code: string, + bindings?: Readonly>, + options?: RunOptions, + ) => Effect.Effect /** A program ready to run once; nothing runs until `program.run()`. */ readonly program: ( code: string, @@ -169,7 +190,7 @@ export type Runtime = { * same build with the same extensions. The state is checked when it runs: `run()` fails with a `SnapshotError` when * it can't be resumed here. */ - readonly resume: (state: Snapshot | Uint8Array) => Program + readonly resume: (state: Snapshot | Uint8Array, options?: RunOptions) => Program } /** @@ -217,7 +238,7 @@ export function make< type R = Environment const prepared = ToolRuntime.prepare((options.tools ?? {}) as Tools>) const limits = resolveExecutionLimits(options.limits) - const extensions = options.extensions ?? [] + const extensions: ReadonlyArray = options.extensions ?? [] const bound = new Set(globalNames) for (const extension of extensions) { for (const name of Object.keys(extension.globals)) { @@ -225,27 +246,43 @@ export function make< bound.add(name) } } - const setup: Setup = { - prepared, - limits, - hooks: options.hooks ?? {}, - globals: extensions.length === 0 ? undefined : (ctx) => extensionGlobals(ctx, extensions), - lossy: extensions.some((extension) => extension.snapshot === undefined), + const setupFor = (selected: ReadonlyArray = extensions): Setup => { + for (const extension of selected) { + if (!extensions.includes(extension)) { + throw new TypeError(`Extension "${extension.name}" isn't one this runtime was made with.`) + } + } + // In the runtime's order, so the same extensions picked in any order give the same globals, and resume alike. + const picked = extensions.filter((extension) => selected.includes(extension)) + return { + prepared, + limits, + hooks: options.hooks ?? {}, + globals: picked.length === 0 ? undefined : (ctx) => extensionGlobals(ctx, picked), + lossy: picked.some((extension) => extension.snapshot === undefined), + } } + const setup = setupFor() const data = (bindings: Readonly> | undefined) => bindings === undefined ? undefined : (ctx: Parameters[0]) => Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)] as const) - const program = (code: string, bindings?: Readonly>, programOptions?: ProgramOptions) => - makeProgram(() => Effect.succeed(startProgram(setup, code, data(bindings), programOptions?.runId))) + const program = (code: string, bindings?: Readonly>, programOptions?: ProgramOptions) => { + const run = programOptions?.extensions === undefined ? setup : setupFor(programOptions.extensions) + return makeProgram(() => Effect.succeed(startProgram(run, code, data(bindings), programOptions?.runId))) + } return { get catalog() { return prepared.catalog }, - execute: (code, bindings) => Effect.suspend(() => finishExecution(program(code, bindings).run())), + execute: (code, bindings, runOptions) => + Effect.suspend(() => finishExecution(program(code, bindings, runOptions).run())), program, - resume: (state) => makeProgram(() => resumeProgram(setup, state)), + resume: (state, runOptions) => { + const run = runOptions?.extensions === undefined ? setup : setupFor(runOptions.extensions) + return makeProgram(() => resumeProgram(run, state)) + }, } } diff --git a/packages/codemode/src/index.ts b/packages/codemode/src/index.ts index 9e5c45ff731..9ed61ecf60a 100644 --- a/packages/codemode/src/index.ts +++ b/packages/codemode/src/index.ts @@ -13,6 +13,7 @@ export { type Outcome, type Program, type ProgramOptions, + type RunOptions, type Runtime, type Snapshot, type SnapshotHeader, diff --git a/packages/codemode/test/extensions.test.ts b/packages/codemode/test/extensions.test.ts index 236a149fcac..ed24c49735e 100644 --- a/packages/codemode/test/extensions.test.ts +++ b/packages/codemode/test/extensions.test.ts @@ -659,6 +659,23 @@ describe("configuration", () => { }) }) + test("a run can get only some of the runtime's extensions", async () => { + const files = Extension.make({ name: "files", globals: { read: () => "text" } }) + const target = CodeMode.make({ extensions: [extension, files] }) + expect(await value(`return [typeof read, typeof later]`, target)).toEqual(["function", "function"]) + const only = await Effect.runPromise( + target.execute(`return [typeof read, typeof later, await later(1)]`, undefined, { extensions: [extension] }), + ) + expect(only).toMatchObject({ ok: true, value: ["undefined", "function", 2] }) + const none = await Effect.runPromise( + target.execute(`return [typeof read, typeof later]`, undefined, { extensions: [] }), + ) + expect(none).toMatchObject({ ok: true, value: ["undefined", "undefined"] }) + expect(() => + target.program(`1`, undefined, { extensions: [Extension.make({ name: "other", globals: { f: () => 1 } })] }), + ).toThrow(`Extension "other" isn't one this runtime was made with.`) + }) + test("a global must be a function or an object", () => { expect(() => Extension.make({ name: "bad", globals: { n: 1 as never } })).toThrow( 'Extension "bad" global "n" must be a function or an object.', diff --git a/packages/codemode/test/suspend.test.ts b/packages/codemode/test/suspend.test.ts index 7471f9b55d6..ef3aec35405 100644 --- a/packages/codemode/test/suspend.test.ts +++ b/packages/codemode/test/suspend.test.ts @@ -450,9 +450,7 @@ describe("suspend", () => { test("the snapshot codec does not import Effect", async () => { for (const file of ["codec.ts", "snapshot.ts"]) { - expect(await Bun.file(path.join(import.meta.dir, "../src/snapshot", file)).text()).not.toMatch( - /from "effect"/, - ) + expect(await Bun.file(path.join(import.meta.dir, "../src/snapshot", file)).text()).not.toMatch(/from "effect"/) } }) }) @@ -510,9 +508,7 @@ describe("resume", () => { ) const saved = suspendedOf(await Effect.runPromise(long.suspend)) expect( - await Effect.runPromise( - background(CodeMode.make({ tools: { step: step2.tool } }).resume(saved.state)).result, - ), + await Effect.runPromise(background(CodeMode.make({ tools: { step: step2.tool } }).resume(saved.state)).result), ).toMatchObject({ ok: true, truncated: true }) }) @@ -538,6 +534,31 @@ describe("resume", () => { expect(timedOut).toMatchObject({ ok: false, error: { kind: "TimeoutExceeded" }, toolCalls: [{ name: "step" }] }) }) + test("a run started with some of the runtime's extensions resumes with the same ones, in any order", async () => { + const now = Extension.make({ name: "now", globals: { now: () => 7 } }) + const later = Extension.make({ name: "later", globals: { later: () => 8 } }) + const unused = Extension.make({ name: "unused", globals: { unused: () => 1 } }) + const step = stepTool() + const runtime = CodeMode.make({ tools: { step: step.tool }, extensions: [now, later, unused] }) + const run = background( + runtime.program( + `await tools.step({ value: 1 }); const t = Date.now(); while (Date.now() - t < 300) {} return [now(), later(), typeof unused]`, + undefined, + { extensions: [later, now] }, + ), + ) + await step.called() + await Bun.sleep(10) + const state = suspendedOf(await Effect.runPromise(run.suspend)).state + expect((await refusal(runtime.resume(state))).message).toMatch("saved by a runtime with other host globals") + expect((await refusal(runtime.resume(state, { extensions: [now] }))).message).toMatch( + "saved by a runtime with other host globals", + ) + expect( + await Effect.runPromise(background(runtime.resume(state, { extensions: [now, later, now] })).result), + ).toMatchObject({ ok: true, value: [7, 8, "undefined"] }) + }) + test("a state is refused by another build, other extensions, or a changed source", async () => { const { suspended } = await suspendAt( `await tools.step({ value: 1 }); const t = Date.now(); while (Date.now() - t < 300) {} return 1`, @@ -587,9 +608,7 @@ describe("resume", () => { [extension], ) const outcome = await Effect.runPromise( - background( - CodeMode.make({ tools: { step: step.tool }, extensions: [extension] }).resume(suspended.state), - ).result, + background(CodeMode.make({ tools: { step: step.tool }, extensions: [extension] }).resume(suspended.state)).result, ) expect(outcome).toMatchObject({ ok: true, @@ -647,8 +666,7 @@ describe("resume", () => { }` const { step, suspended } = await suspendAt(code) const outcome = await Effect.runPromise( - background(CodeMode.make({ tools: { step: step.tool } }).resume(CodeMode.toBytes(suspended.state))) - .result, + background(CodeMode.make({ tools: { step: step.tool } }).resume(CodeMode.toBytes(suspended.state))).result, ) expect(outcome).toMatchObject({ ok: true, @@ -1295,9 +1313,9 @@ describe("idempotent and durable tools", () => { }) const toolSets: ReadonlyArray> = [{}, { job: plainTool }, { job: read.tool }] for (const tools of toolSets) { - expect( - (await refusal(CodeMode.make({ tools }).resume(suspendedWhileStarted.state))).message, - ).toContain("cannot resume because that tool is no longer a durable tool") + expect((await refusal(CodeMode.make({ tools }).resume(suspendedWhileStarted.state))).message).toContain( + "cannot resume because that tool is no longer a durable tool", + ) } // Complete `job` while `read` is still running: `job` moves to the inbox with its `data` cleared. @@ -1514,8 +1532,7 @@ describe("extension snapshot hooks", () => { const other = Extension.make({ name: "other", globals: extension.globals, snapshot: extension.snapshot }) for (const extensions of [[], [unhooked], [other]]) { expect( - (await refusal(CodeMode.make({ tools: { job: jobs.tool }, extensions }).resume(suspended.state))) - .message, + (await refusal(CodeMode.make({ tools: { job: jobs.tool }, extensions }).resume(suspended.state))).message, ).toBe("This state was saved by a runtime with other host globals (extensions); resume it with the same ones.") } })