mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
feat(codemode): start programs with execute in the current scope, replacing start
This commit is contained in:
+29
-14
@@ -42,14 +42,20 @@ const runtime = CodeMode.make({
|
||||
})
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runtime.execute(`
|
||||
const order = await tools.orders.lookup({ id: "order_42" })
|
||||
return { id: order.id, needsAttention: order.status !== "complete" }
|
||||
`),
|
||||
Effect.scoped(
|
||||
Effect.flatMap(
|
||||
runtime.execute(`
|
||||
const order = await tools.orders.lookup({ id: "order_42" })
|
||||
return { id: order.id, needsAttention: order.status !== "complete" }
|
||||
`),
|
||||
(execution) => execution.result,
|
||||
),
|
||||
),
|
||||
)
|
||||
```
|
||||
|
||||
`result` is always a [`CodeMode.Result`](#results).
|
||||
`result` is a [`CodeMode.Result`](#results), or a suspended run's saved state when something
|
||||
[suspends](#suspending-and-resuming-a-run) it.
|
||||
|
||||
## API
|
||||
|
||||
@@ -86,9 +92,14 @@ bracket notation, such as `tools.context7["resolve-library-id"](...)`.
|
||||
const runtime = CodeMode.make({ tools, limits: { timeoutMs: 30_000 } })
|
||||
|
||||
runtime.catalog() // structured tool descriptions
|
||||
runtime.execute(source) // Effect<CodeMode.Result, never, ToolServices>
|
||||
const execution = yield * runtime.execute(source) // Effect<CodeMode.Execution, never, ToolServices | Scope>
|
||||
const result = yield * execution.result // CodeMode.Result | CodeMode.Suspended
|
||||
```
|
||||
|
||||
`execute` starts the program in the current scope and returns at once. `execution.result` waits for its outcome, and
|
||||
`execution.suspend` saves its state (see below). Closing the scope interrupts the program and cancels its pending
|
||||
durable work.
|
||||
|
||||
The Effect environment is inferred from the supplied tools. `hooks` surround every call the program makes into the
|
||||
host: `tool.before`/`tool.after` receive `{ name, input }` with decoded input, `extension.before`/`extension.after`
|
||||
receive `{ extension, name, args }`. An `after` hook also receives how the call ended (`success` with its value,
|
||||
@@ -99,8 +110,8 @@ 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:
|
||||
`runtime.execute(code, bindings?, { runId? })` starts a run that can be suspended: `execution.suspend` stops it at the
|
||||
next step boundary and saves its state. It and `execution.result` give the same outcome, a `Result` or:
|
||||
|
||||
```ts
|
||||
type Suspended = {
|
||||
@@ -109,11 +120,11 @@ type Suspended = {
|
||||
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
|
||||
const execution = yield * runtime.execute(source)
|
||||
const suspended = yield * execution.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
|
||||
const resumed = yield * runtime.resume(suspended.state, { [key]: { ok: true, value } })
|
||||
const result = yield * resumed.result
|
||||
```
|
||||
|
||||
Suspending waits briefly (`suspend.graceMs`, 50 ms by default) for running tool calls to finish, so their outcomes are
|
||||
@@ -122,7 +133,7 @@ outcome: on resume it runs again if its tool declares `retry: "safe"`, and other
|
||||
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, `<runId>:<id>`.
|
||||
|
||||
A state resumes only on the same build of this package, with the same extensions; `resume` throws a
|
||||
A state resumes only on the same build of this package, with the same extensions; `resume` fails with 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.
|
||||
@@ -186,7 +197,11 @@ const web = Extension.make({
|
||||
),
|
||||
},
|
||||
})
|
||||
const result = yield* CodeMode.make({ extensions: [web] }).execute(code).pipe(Effect.provideService(Session, session))
|
||||
const execution =
|
||||
yield *
|
||||
CodeMode.make({ extensions: [web] })
|
||||
.execute(code)
|
||||
.pipe(Effect.provideService(Session, session))
|
||||
```
|
||||
|
||||
Every value crossing in either direction is converted, never shared: arguments come in as copies, results go out as
|
||||
|
||||
@@ -95,15 +95,17 @@ const median = (values: Array<number>) => {
|
||||
return sorted[Math.floor(sorted.length / 2)]!
|
||||
}
|
||||
|
||||
const run = (code: string) => Effect.scoped(Effect.flatMap(runtime.execute(code), (execution) => execution.result))
|
||||
|
||||
for (const [name, code] of cases) {
|
||||
if (filter !== undefined && !name.includes(filter)) continue
|
||||
// One warm-up run, which also checks the program succeeds.
|
||||
const warm = await Effect.runPromise(runtime.execute(code))
|
||||
if (!warm.ok) throw new Error(`${name} failed: ${warm.error.message}`)
|
||||
const warm = await Effect.runPromise(run(code))
|
||||
if (warm.ok !== true) throw new Error(`${name} failed: ${JSON.stringify(warm)}`)
|
||||
const times = Array.from({ length: iterations }, () => 0)
|
||||
for (const index of times.keys()) {
|
||||
const start = performance.now()
|
||||
await Effect.runPromise(runtime.execute(code))
|
||||
await Effect.runPromise(run(code))
|
||||
times[index] = performance.now() - start
|
||||
}
|
||||
console.log(
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Effect, Schema } from "effect"
|
||||
import { Effect, Schema, type Scope } from "effect"
|
||||
import { fromJson } from "./data.js"
|
||||
import type { Extension } from "./extension.js"
|
||||
import type { Json } from "./data.js"
|
||||
import type { Outcome } from "./interpreter/driver.js"
|
||||
import { executeSetup, resumeProgram, type Run, type Setup, startProgram } from "./interpreter/execute.js"
|
||||
import { type Execution, resumeProgram, type Setup, startProgram } from "./interpreter/execute.js"
|
||||
import type { Snapshot } from "./snapshot/snapshot.js"
|
||||
import { extensionGlobals } from "./interpreter/extensions.js"
|
||||
import { globalNames } from "./interpreter/globals.js"
|
||||
@@ -13,8 +13,9 @@ import type { Tools } from "./tools.js"
|
||||
|
||||
export { scanTools }
|
||||
export { fromBytes, SnapshotError, toBytes } from "./snapshot/snapshot.js"
|
||||
import type { SnapshotError } from "./snapshot/snapshot.js"
|
||||
export type { Header as SnapshotHeader, Snapshot } from "./snapshot/snapshot.js"
|
||||
export type { Outcome, Run }
|
||||
export type { Execution, Outcome }
|
||||
|
||||
/** A tool call admitted during an execution. */
|
||||
export type {
|
||||
@@ -60,7 +61,7 @@ export type Options<Provided extends Record<string, unknown> = {}, Extensions ex
|
||||
extensions?: Extensions
|
||||
/** Resource limits enforced on each execution. Time counts active time only, across every resume of a run. */
|
||||
limits?: ExecutionLimits
|
||||
/** How `run.suspend` saves a run; see `Runtime.start`. */
|
||||
/** How `execution.suspend` saves a run; see `Runtime.execute`. */
|
||||
suspend?: SuspendOptions
|
||||
}
|
||||
|
||||
@@ -172,22 +173,25 @@ export type Suspended = {
|
||||
/** Reusable confined runtime over explicit tools. */
|
||||
export type Runtime<R = never> = {
|
||||
readonly catalog: ReadonlyArray<ToolDescription>
|
||||
readonly execute: (code: string, bindings?: Readonly<Record<string, DataValue>>) => Effect.Effect<Result, never, R>
|
||||
/**
|
||||
* Starts a program as a run that can be suspended: `run.result` runs it, and `run.suspend` stops it at the next step
|
||||
* boundary and saves its state. `runId` names its host calls' idempotency keys; a random one by default.
|
||||
* Starts a program in the current scope and returns at once: `execution.result` waits for its outcome, and
|
||||
* `execution.suspend` saves its state. Closing the scope interrupts the program and cancels its pending durable work.
|
||||
* `runId` names its host calls' idempotency keys; a random one by default.
|
||||
*/
|
||||
readonly start: (
|
||||
readonly execute: (
|
||||
code: string,
|
||||
bindings?: Readonly<Record<string, DataValue>>,
|
||||
options?: { readonly runId?: string },
|
||||
) => Run<R>
|
||||
) => Effect.Effect<Execution, never, R | Scope.Scope>
|
||||
/**
|
||||
* Continues a suspended run (its `state`, or that state as bytes) in this runtime, which must be the same build with
|
||||
* the same extensions. `outcomes` settles pending calls whose outcome the host knows, by key. Throws a
|
||||
* `SnapshotError` when the state can't be resumed here.
|
||||
* the same extensions, as `execute` starts one. `outcomes` settles pending calls whose outcome the host knows, by
|
||||
* key. Fails with a `SnapshotError` when the state can't be resumed here.
|
||||
*/
|
||||
readonly resume: (state: Snapshot | Uint8Array, outcomes?: Readonly<Record<string, Outcome>>) => Run<R>
|
||||
readonly resume: (
|
||||
state: Snapshot | Uint8Array,
|
||||
outcomes?: Readonly<Record<string, Outcome>>,
|
||||
) => Effect.Effect<Execution, SnapshotError, R | Scope.Scope>
|
||||
}
|
||||
|
||||
const validateLimit = (name: keyof ExecutionLimits, value: number | undefined, minimum: number): number | undefined => {
|
||||
@@ -203,13 +207,13 @@ const resolveExecutionLimits = (limits?: ExecutionLimits): ResolvedExecutionLimi
|
||||
maxOutputBytes: validateLimit("maxOutputBytes", limits?.maxOutputBytes, 0),
|
||||
})
|
||||
|
||||
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
||||
/** Starts one Effect-native CodeMode program without constructing a reusable runtime; see `Runtime.execute`. */
|
||||
export const execute = <
|
||||
const Provided extends Record<string, unknown>,
|
||||
const Extensions extends ReadonlyArray<Extension> = [],
|
||||
>(
|
||||
options: ExecuteOptions<Provided, Extensions>,
|
||||
): Effect.Effect<Result, never, Environment<Provided, Extensions>> =>
|
||||
): Effect.Effect<Execution, never, Environment<Provided, Extensions> | Scope.Scope> =>
|
||||
make(options).execute(options.code, options.bindings)
|
||||
|
||||
/** Creates an Effect-native runtime over explicit, schema-described tools and extensions. */
|
||||
@@ -253,8 +257,7 @@ export const make = <
|
||||
get catalog() {
|
||||
return prepared.catalog
|
||||
},
|
||||
execute: (code, bindings) => executeSetup(setup, code, data(bindings)),
|
||||
start: (code, bindings, runOptions) => startProgram(setup, code, data(bindings), runOptions?.runId),
|
||||
execute: (code, bindings, runOptions) => startProgram(setup, code, data(bindings), runOptions?.runId),
|
||||
resume: (state, outcomes) => resumeProgram(setup, state, outcomes),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
import { parse, type Program } from "acorn"
|
||||
import { Cause, Deferred, Effect, Option, Schema } from "effect"
|
||||
import type {
|
||||
DataValue,
|
||||
Diagnostic,
|
||||
Pending,
|
||||
ResolvedExecutionLimits,
|
||||
Result,
|
||||
Suspended,
|
||||
} from "../codemode.js"
|
||||
import { Cause, Deferred, Effect, Option, Schema, type Scope } from "effect"
|
||||
import type { DataValue, Diagnostic, Pending, ResolvedExecutionLimits, Result, Suspended } from "../codemode.js"
|
||||
import type { Json } from "../data.js"
|
||||
import { capture, fromBytes, restore, type Snapshot, SnapshotError, toBytes } from "../snapshot/snapshot.js"
|
||||
import { ToolRuntime } from "../tool-runtime.js"
|
||||
@@ -35,10 +28,10 @@ export type Setup<R> = {
|
||||
readonly suspend?: { readonly graceMs?: number | undefined; readonly maxBytes?: number | undefined }
|
||||
}
|
||||
|
||||
/** A running execution: its outcome, and a way to suspend it. */
|
||||
export type Run<R> = {
|
||||
/** Runs the program: its result, or its saved state once suspended. Run it once. */
|
||||
readonly result: Effect.Effect<Result | Suspended, never, R>
|
||||
/** A started program: its outcome, and a way to suspend it. */
|
||||
export type Execution = {
|
||||
/** Waits for the program's result, or its saved state once suspended. */
|
||||
readonly result: Effect.Effect<Result | Suspended>
|
||||
/**
|
||||
* Stops the run at the next step boundary, settles its host calls, and saves its state; what `result` gives. A run
|
||||
* that already ended gives its result.
|
||||
@@ -56,53 +49,59 @@ export const executeProgram = <R>(
|
||||
globals?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>,
|
||||
bindings?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>,
|
||||
): Effect.Effect<Result, never, R> =>
|
||||
executeSetup({ prepared, limits, hooks, globals }, code, bindings)
|
||||
Effect.scoped(
|
||||
Effect.flatMap(
|
||||
startProgram({ prepared, limits, hooks, globals }, code, bindings),
|
||||
// Nothing suspends this run, so it ends with a result.
|
||||
(execution) => execution.result as Effect.Effect<Result>,
|
||||
),
|
||||
)
|
||||
|
||||
/** Runs one program to its end; nothing can suspend it. */
|
||||
export const executeSetup = <R>(
|
||||
setup: Setup<R>,
|
||||
code: string,
|
||||
bindings?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>,
|
||||
): Effect.Effect<Result, never, R> =>
|
||||
// Allocate execution state inside suspension so reused Effects never share it.
|
||||
Effect.suspend(() => startProgram(setup, code, bindings).result as Effect.Effect<Result, never, R>)
|
||||
|
||||
/** Starts a program: a handle whose `result` runs it, and whose `suspend` saves its state. */
|
||||
/** Starts a program in the current scope; closing the scope interrupts it. */
|
||||
export const startProgram = <R>(
|
||||
setup: Setup<R>,
|
||||
code: string,
|
||||
bindings?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>,
|
||||
runId: string = crypto.randomUUID(),
|
||||
): Run<R> => {
|
||||
if (code.trim().length === 0) {
|
||||
return ended({ ok: false, error: { kind: "ParseError", message: "Code cannot be empty." }, toolCalls: [] })
|
||||
}
|
||||
return new Execution(setup, runId, 0, () => {
|
||||
const parsed = parseProgram(code)
|
||||
if (!parsed.ok) return { ok: false, error: parsed.error, toolCalls: [] }
|
||||
const ctx = instantiate(setup, bindings)
|
||||
ctx.machine.runId = runId
|
||||
ctx.start(parsed.source)
|
||||
return ctx
|
||||
}).run
|
||||
}
|
||||
): Effect.Effect<Execution, never, R | Scope.Scope> =>
|
||||
Effect.suspend(() => {
|
||||
if (code.trim().length === 0) {
|
||||
return Effect.succeed(
|
||||
ended({ ok: false, error: { kind: "ParseError", message: "Code cannot be empty." }, toolCalls: [] }),
|
||||
)
|
||||
}
|
||||
return new Running(setup, runId, 0, () => {
|
||||
const parsed = parseProgram(code)
|
||||
if (!parsed.ok) return { ok: false, error: parsed.error, toolCalls: [] }
|
||||
const ctx = instantiate(setup, bindings)
|
||||
ctx.machine.runId = runId
|
||||
ctx.start(parsed.source)
|
||||
return ctx
|
||||
}).start()
|
||||
})
|
||||
|
||||
/**
|
||||
* Resumes a saved run in this runtime: a handle like `startProgram`'s. `outcomes` completes pending host calls by
|
||||
* key. Throws a `SnapshotError` when the state can't be read or belongs to another build, other host globals, or
|
||||
* other sources.
|
||||
* Resumes a saved run in this runtime, in the current scope, as `startProgram` starts one. `outcomes` completes
|
||||
* pending host calls by key. Fails with a `SnapshotError` when the state can't be read or belongs to another build,
|
||||
* other host globals, or other sources.
|
||||
*/
|
||||
export const resumeProgram = <R>(
|
||||
setup: Setup<R>,
|
||||
saved: Snapshot | Uint8Array | unknown,
|
||||
outcomes: Readonly<Record<string, Outcome>> = {},
|
||||
): Run<R> => {
|
||||
const fresh = instantiate(setup)
|
||||
const snapshot = restoreInto(setup, saved instanceof Uint8Array ? fromBytes(saved) : saved, fresh)
|
||||
return new Execution(setup, snapshot.header.runId, snapshot.header.budget.activeMs, () => fresh, outcomes).run
|
||||
}
|
||||
): Effect.Effect<Execution, SnapshotError, R | Scope.Scope> =>
|
||||
Effect.suspend(() => {
|
||||
const fresh = instantiate(setup)
|
||||
const snapshot = Effect.try({
|
||||
try: () => restoreInto(setup, saved instanceof Uint8Array ? fromBytes(saved) : saved, fresh),
|
||||
catch: (error) => (error instanceof SnapshotError ? error : new SnapshotError(String(error))),
|
||||
})
|
||||
return Effect.flatMap(snapshot, (restored) =>
|
||||
new Running(setup, restored.header.runId, restored.header.budget.activeMs, () => fresh, outcomes).start(),
|
||||
)
|
||||
})
|
||||
|
||||
const ended = (result: Result): Run<never> => ({ result: Effect.succeed(result), suspend: Effect.succeed(result) })
|
||||
const ended = (result: Result): Execution => ({ result: Effect.succeed(result), suspend: Effect.succeed(result) })
|
||||
|
||||
// Sources restored recently, by text: the AST is never changed, so runs restored from the same program share it.
|
||||
const restoredSources = new Map<string, Source>()
|
||||
@@ -140,7 +139,8 @@ const instantiate = <R>(
|
||||
|
||||
let bareRuntime: Interpreter<unknown> | undefined
|
||||
// The intrinsics alone, which the build id is taken from.
|
||||
const bare = () => (bareRuntime ??= instantiate<unknown>({ prepared: ToolRuntime.prepare({}), limits: noLimits, hooks: {} }))
|
||||
const bare = () =>
|
||||
(bareRuntime ??= instantiate<unknown>({ prepared: ToolRuntime.prepare({}), limits: noLimits, hooks: {} }))
|
||||
const noLimits: ResolvedExecutionLimits = { timeoutMs: undefined, maxToolCalls: undefined, maxOutputBytes: undefined }
|
||||
|
||||
const pristines = new WeakMap<object, Interpreter<unknown>>()
|
||||
@@ -174,11 +174,10 @@ if (roundTrip) {
|
||||
}
|
||||
|
||||
/** One run's state: the runtime it is in now (restoring swaps it), its driver, and how much active time it used. */
|
||||
class Execution<R> {
|
||||
class Running<R> {
|
||||
private ctx: Interpreter<R> | undefined
|
||||
private driver: Driver<R> | undefined
|
||||
private suspendAsked = false
|
||||
private started = false
|
||||
private trips = 0
|
||||
// Checkpoints to let pass before the next round trip, so a large state is not saved at every one.
|
||||
private skip = 0
|
||||
@@ -193,19 +192,16 @@ class Execution<R> {
|
||||
private readonly outcomes?: Readonly<Record<string, Outcome>>,
|
||||
) {}
|
||||
|
||||
get run(): Run<R> {
|
||||
return {
|
||||
result: Effect.suspend(() => {
|
||||
if (this.started) return Deferred.await(this.done)
|
||||
this.started = true
|
||||
return this.execute().pipe(Effect.onExit((exit) => Deferred.done(this.done, exit)))
|
||||
}),
|
||||
// Runs the program in a fiber of the current scope.
|
||||
start(): Effect.Effect<Execution, never, R | Scope.Scope> {
|
||||
return Effect.as(Effect.forkScoped(this.execute().pipe(Effect.onExit((exit) => Deferred.done(this.done, exit)))), {
|
||||
result: Deferred.await(this.done),
|
||||
suspend: Effect.suspend(() => {
|
||||
this.suspendAsked = true
|
||||
this.driver?.suspend()
|
||||
return Deferred.await(this.done)
|
||||
}),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private execute(): Effect.Effect<Result | Suspended, never, R> {
|
||||
|
||||
@@ -57,9 +57,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -70,9 +70,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { CodeMode, Tool } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// Callback acceptance is one gate shared by array methods, sort, string replacers,
|
||||
// Array.from mappers, Map/Set/URLSearchParams forEach, and promise reactions:
|
||||
// interpreter functions, coercion/URI builtins, resolver capabilities, and built-in
|
||||
// method references are callable; tools and other opaque callables get a wrap hint.
|
||||
const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const run = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
const value = async (code: string) => {
|
||||
const result = await run(code)
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
@@ -29,7 +30,7 @@ const echo = Tool.make({
|
||||
output: Schema.Number,
|
||||
execute: (input: { id: number }) => Effect.succeed(input.id),
|
||||
})
|
||||
const withTool = (code: string) => Effect.runPromise(CodeMode.make({ tools: { host: { echo } } }).execute(code))
|
||||
const withTool = (code: string) => Effect.runPromise(finished(CodeMode.make({ tools: { host: { echo } } }).execute(code)))
|
||||
const toolError = async (code: string) => {
|
||||
const result = await withTool(code)
|
||||
if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Cause, Effect, Schema } from "effect"
|
||||
import { CodeMode, Extension, Tool, toolError } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const run = (tool: Tool.Tool<never>) =>
|
||||
Effect.runPromise(CodeMode.make({ tools: { host: { call: tool } } }).execute("return await tools.host.call({})"))
|
||||
Effect.runPromise(finished(CodeMode.make({ tools: { host: { call: tool } } }).execute("return await tools.host.call({})")))
|
||||
|
||||
class HostError extends Schema.TaggedError<HostError>()("HostError", {
|
||||
message: Schema.String,
|
||||
@@ -112,7 +113,7 @@ describe("CodeMode host failure boundary", () => {
|
||||
|
||||
test("caught tool failures are Error values in-program", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.make({
|
||||
finished(CodeMode.make({
|
||||
tools: {
|
||||
host: {
|
||||
call: Tool.make({
|
||||
@@ -130,7 +131,7 @@ describe("CodeMode host failure boundary", () => {
|
||||
} catch (e) {
|
||||
return { isError: e instanceof Error, message: e.message }
|
||||
}
|
||||
`),
|
||||
`)),
|
||||
)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
@@ -139,7 +140,7 @@ describe("CodeMode host failure boundary", () => {
|
||||
|
||||
test("propagates host interruption instead of returning a diagnostic", async () => {
|
||||
const exit = await Effect.runPromiseExit(
|
||||
CodeMode.make({
|
||||
finished(CodeMode.make({
|
||||
tools: {
|
||||
host: {
|
||||
call: Tool.make({
|
||||
@@ -150,7 +151,7 @@ describe("CodeMode host failure boundary", () => {
|
||||
}),
|
||||
},
|
||||
},
|
||||
}).execute("return await tools.host.call({})"),
|
||||
}).execute("return await tools.host.call({})")),
|
||||
)
|
||||
|
||||
expect(exit._tag).toBe("Failure")
|
||||
@@ -185,13 +186,13 @@ describe("CodeMode call hooks", () => {
|
||||
const runtime = CodeMode.make({ tools: { context: { lookup } }, hooks: observe(events) })
|
||||
|
||||
const success = await Effect.runPromise(
|
||||
runtime.execute(`
|
||||
finished(runtime.execute(`
|
||||
if (false) await tools.context.lookup({ query: "not called" })
|
||||
return await tools.context.lookup({ query: "deployment failure" })
|
||||
`),
|
||||
`)),
|
||||
)
|
||||
expect(success).toMatchObject({ ok: true, value: "deployment failure" })
|
||||
const failure = await Effect.runPromise(runtime.execute(`return await tools.context.lookup({ query: "boom" })`))
|
||||
const failure = await Effect.runPromise(finished(runtime.execute(`return await tools.context.lookup({ query: "boom" })`)))
|
||||
expect(failure.ok).toBe(false)
|
||||
expect(events).toEqual([
|
||||
'tool.before {"name":"context.lookup","input":{"query":"deployment failure"}}',
|
||||
@@ -222,12 +223,12 @@ describe("CodeMode call hooks", () => {
|
||||
hooks: observe(events),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
runtime.execute(`
|
||||
finished(runtime.execute(`
|
||||
const res = await fetch("https://a.test/", { method: "POST" })
|
||||
res.json()
|
||||
try { fail() } catch {}
|
||||
return res.url
|
||||
`),
|
||||
`)),
|
||||
)
|
||||
expect(result).toMatchObject({ ok: true, value: "https://a.test/", toolCalls: [] })
|
||||
expect(events).toEqual([
|
||||
@@ -253,13 +254,13 @@ describe("CodeMode call hooks", () => {
|
||||
})
|
||||
expect(
|
||||
await Effect.runPromise(
|
||||
runtime.execute(`
|
||||
finished(runtime.execute(`
|
||||
const denied = await fetch("https://a.test/").catch((e) => [e instanceof Error, e.message])
|
||||
return [denied, await tools.lookup({})]
|
||||
`),
|
||||
`)),
|
||||
),
|
||||
).toMatchObject({ ok: true, value: [[true, "fetch is not allowed"], "ok"] })
|
||||
expect(await Effect.runPromise(runtime.execute(`await fetch("https://a.test/")`))).toMatchObject({
|
||||
expect(await Effect.runPromise(finished(runtime.execute(`await fetch("https://a.test/")`)))).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "ExecutionFailure", message: "Error: fetch is not allowed" },
|
||||
})
|
||||
@@ -276,10 +277,10 @@ describe("CodeMode call hooks", () => {
|
||||
})
|
||||
expect(
|
||||
await Effect.runPromise(
|
||||
refused.execute(`try { await tools.lookup({}) } catch (e) { return [e.name, e.message] }`),
|
||||
finished(refused.execute(`try { await tools.lookup({}) } catch (e) { return [e.name, e.message] }`)),
|
||||
),
|
||||
).toMatchObject({ ok: true, value: ["Error", "lookup is not allowed"], toolCalls: [{ name: "lookup" }] })
|
||||
expect(await Effect.runPromise(refused.execute(`await tools.lookup({})`))).toMatchObject({
|
||||
expect(await Effect.runPromise(finished(refused.execute(`await tools.lookup({})`)))).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "ToolFailure", message: "lookup is not allowed" },
|
||||
})
|
||||
@@ -305,7 +306,7 @@ describe("CodeMode call hooks", () => {
|
||||
],
|
||||
hooks: { "extension.before": (call) => Effect.fail(new Error(`${call.name} is not allowed`)) },
|
||||
})
|
||||
const run = async (code: string) => (await Effect.runPromise(runtime.execute(code))) as { value?: unknown }
|
||||
const run = async (code: string) => (await Effect.runPromise(finished(runtime.execute(code)))) as { value?: unknown }
|
||||
|
||||
// A denied call fails the way the global would: an async global rejects its promise, a sync one throws.
|
||||
expect(
|
||||
@@ -328,8 +329,8 @@ describe("CodeMode call hooks", () => {
|
||||
extensions: [Extension.make({ name: "slow", globals: { forever: () => new Promise(() => {}) } })],
|
||||
hooks: observe(events),
|
||||
})
|
||||
expect(await Effect.runPromise(runtime.execute(`await tools.broken({})`))).toMatchObject({ ok: false })
|
||||
expect(await Effect.runPromise(runtime.execute(`forever(); return "done"`))).toMatchObject({
|
||||
expect(await Effect.runPromise(finished(runtime.execute(`await tools.broken({})`)))).toMatchObject({ ok: false })
|
||||
expect(await Effect.runPromise(finished(runtime.execute(`forever(); return "done"`)))).toMatchObject({
|
||||
ok: true,
|
||||
value: "done",
|
||||
})
|
||||
@@ -350,7 +351,7 @@ describe("CodeMode call hooks", () => {
|
||||
execute: () => Effect.interrupt,
|
||||
})
|
||||
const exit = await Effect.runPromiseExit(
|
||||
CodeMode.make({ tools: { host: { call } }, hooks: observe(events) }).execute("return await tools.host.call({})"),
|
||||
finished(CodeMode.make({ tools: { host: { call } }, hooks: observe(events) }).execute("return await tools.host.call({})")),
|
||||
)
|
||||
|
||||
expect(exit._tag).toBe("Failure")
|
||||
@@ -366,9 +367,9 @@ describe("CodeMode call hooks", () => {
|
||||
execute: () => Effect.never,
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.make({ tools: { host: { call } }, hooks: observe(events) }).execute(
|
||||
finished(CodeMode.make({ tools: { host: { call } }, hooks: observe(events) }).execute(
|
||||
'tools.host.call({}); return "done"',
|
||||
),
|
||||
)),
|
||||
)
|
||||
|
||||
expect(result).toMatchObject({ ok: true, value: "done" })
|
||||
@@ -384,9 +385,9 @@ describe("CodeMode call hooks", () => {
|
||||
execute: () => Effect.never,
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.make({ tools: { host: { call } }, limits: { timeoutMs: 10 }, hooks: observe(events) }).execute(
|
||||
finished(CodeMode.make({ tools: { host: { call } }, limits: { timeoutMs: 10 }, hooks: observe(events) }).execute(
|
||||
"return await tools.host.call({})",
|
||||
),
|
||||
)),
|
||||
)
|
||||
|
||||
expect(result).toMatchObject({ ok: false, error: { kind: "TimeoutExceeded" } })
|
||||
@@ -397,13 +398,13 @@ describe("CodeMode call hooks", () => {
|
||||
describe("CodeMode console capture", () => {
|
||||
test("captures console output as bounded result logs", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
const returned = console.log("Thread info:", { name: "Demo", count: 2 })
|
||||
console.warn("careful")
|
||||
return returned
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result).toStrictEqual({
|
||||
@@ -417,12 +418,12 @@ describe("CodeMode console capture", () => {
|
||||
|
||||
test("keeps logs captured before failures", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
console.log("before failure")
|
||||
throw new Error("boom")
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result.ok ? undefined : result.logs).toStrictEqual(["before failure"])
|
||||
@@ -431,14 +432,14 @@ describe("CodeMode console capture", () => {
|
||||
|
||||
test("prints NaN and Infinity literally instead of the JSON null", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
console.log(NaN)
|
||||
console.log(Infinity, -Infinity)
|
||||
console.log({ ratio: NaN, bounds: [Infinity] })
|
||||
return null
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
@@ -447,13 +448,13 @@ describe("CodeMode console capture", () => {
|
||||
|
||||
test("renders CodeMode values nested inside logged containers", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
console.log({ m: new Map([["a", 1]]), when: new Date(0), r: /ab/g, s: new Set([1, 2]) })
|
||||
console.log([new Date(0)])
|
||||
return null
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
@@ -465,7 +466,7 @@ describe("CodeMode console capture", () => {
|
||||
|
||||
test("console formatting is total: cycles and opaque references render as markers", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
const m = new Map()
|
||||
m.set("self", m)
|
||||
@@ -473,7 +474,7 @@ describe("CodeMode console capture", () => {
|
||||
console.log({ fn: (x) => x, ok: 1 })
|
||||
return null
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
@@ -482,12 +483,12 @@ describe("CodeMode console capture", () => {
|
||||
|
||||
test("console.table renders CodeMode value cells", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
console.table([{ when: new Date(0), n: NaN }])
|
||||
return null
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
@@ -496,7 +497,7 @@ describe("CodeMode console capture", () => {
|
||||
|
||||
test("captures console.dir and console.table output", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
console.dir({ nested: { ok: true } })
|
||||
console.table([
|
||||
@@ -505,7 +506,7 @@ describe("CodeMode console capture", () => {
|
||||
], ["name", "count"])
|
||||
return "done"
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result).toStrictEqual({
|
||||
@@ -520,9 +521,9 @@ describe("CodeMode console capture", () => {
|
||||
describe("CodeMode output budget", () => {
|
||||
test("absent maxOutputBytes means no truncation at all", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `console.log("z".repeat(50_000)); return "x".repeat(100_000)`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
@@ -535,10 +536,10 @@ describe("CodeMode output budget", () => {
|
||||
test("truncates an oversized result value with a marker instead of failing", async () => {
|
||||
const limits: CodeMode.ExecutionLimits = { maxOutputBytes: 40 }
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `return { data: "${"x".repeat(200)}" }`,
|
||||
limits,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
@@ -554,14 +555,14 @@ describe("CodeMode output budget", () => {
|
||||
test("keeps leading logs within the remaining budget and marks the cut", async () => {
|
||||
const limits: CodeMode.ExecutionLimits = { maxOutputBytes: 40 }
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
console.log("first line")
|
||||
console.log("${"y".repeat(200)}")
|
||||
return "ok"
|
||||
`,
|
||||
limits,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
@@ -573,12 +574,12 @@ describe("CodeMode output budget", () => {
|
||||
|
||||
test("does not mark results within the budget", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `
|
||||
console.log("fits")
|
||||
return { fits: true }
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(result).toStrictEqual({
|
||||
ok: true,
|
||||
@@ -616,7 +617,7 @@ describe("CodeMode schema flexibility", () => {
|
||||
])
|
||||
|
||||
// JSON Schema is render-only: mistyped input passes through unvalidated.
|
||||
const result = await Effect.runPromise(runtime.execute(`return await tools.adapter.call({ id: 42 })`))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(`return await tools.adapter.call({ id: 42 })`)))
|
||||
expect(result.ok).toBe(true)
|
||||
if (result.ok) expect(result.value).toBeNull()
|
||||
expect(observed).toStrictEqual([{ id: 42 }])
|
||||
@@ -636,9 +637,9 @@ describe("CodeMode schema flexibility", () => {
|
||||
const runtime = CodeMode.make({ tools: { adapter: { call } } })
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runtime.execute(
|
||||
finished(runtime.execute(
|
||||
`return await tools.adapter.call({ q: undefined, limit: 0 / 0, rate: 1 / 0, items: [1, undefined, 2], holes: [1, , 3] })`,
|
||||
),
|
||||
)),
|
||||
)
|
||||
expect(result.ok).toBe(true)
|
||||
const received = observed[0] as Record<string, unknown>
|
||||
@@ -663,12 +664,12 @@ describe("CodeMode schema flexibility", () => {
|
||||
// The `cond ? value : undefined` idiom: optionalKey rejects a present undefined, so the
|
||||
// JSON boundary must drop the key before the schema decodes.
|
||||
const result = await Effect.runPromise(
|
||||
runtime.execute(`return await tools.things.find({ query: undefined, limit: 5 })`),
|
||||
finished(runtime.execute(`return await tools.things.find({ query: undefined, limit: 5 })`)),
|
||||
)
|
||||
expect(result.ok).toBe(true)
|
||||
expect(observed).toStrictEqual([{ limit: 5 }])
|
||||
|
||||
const search = await Effect.runPromise(runtime.execute(`return (await search({ query: undefined })).items.length`))
|
||||
const search = await Effect.runPromise(finished(runtime.execute(`return (await search({ query: undefined })).items.length`)))
|
||||
expect(search.ok).toBe(true)
|
||||
})
|
||||
|
||||
@@ -698,7 +699,7 @@ describe("CodeMode schema flexibility", () => {
|
||||
},
|
||||
])
|
||||
|
||||
const result = await Effect.runPromise(runtime.execute(`return await tools.users.lookup({ login: "kit" })`))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(`return await tools.users.lookup({ login: "kit" })`)))
|
||||
expect(result.ok).toBe(true)
|
||||
if (result.ok) expect(result.value).toStrictEqual({ login: "kit", id: 7 })
|
||||
})
|
||||
@@ -712,7 +713,7 @@ describe("CodeMode schema flexibility", () => {
|
||||
const runtime = CodeMode.make({ tools: { net: { ping } } })
|
||||
expect(runtime.catalog[0]?.signature).toBe("tools.net.ping({\n host: string,\n}): Promise<void>")
|
||||
|
||||
const result = await Effect.runPromise(runtime.execute(`return await tools.net.ping({ host: "example.test" })`))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(`return await tools.net.ping({ host: "example.test" })`)))
|
||||
expect(result.ok).toBe(true)
|
||||
if (result.ok) expect(result.value).toBeNull()
|
||||
})
|
||||
@@ -731,8 +732,8 @@ describe("CodeMode public contract", () => {
|
||||
test("keeps one-shot and reusable execution equivalent", async () => {
|
||||
const runtime = CodeMode.make({ tools })
|
||||
const [oneShot, reusable] = await Promise.all([
|
||||
Effect.runPromise(CodeMode.execute({ tools, code: source })),
|
||||
Effect.runPromise(runtime.execute(source)),
|
||||
Effect.runPromise(finished(CodeMode.execute({ tools, code: source }))),
|
||||
Effect.runPromise(finished(runtime.execute(source))),
|
||||
])
|
||||
|
||||
expect(reusable).toStrictEqual(oneShot)
|
||||
@@ -748,11 +749,11 @@ describe("CodeMode public contract", () => {
|
||||
output: Schema.Number,
|
||||
execute: () => Effect.succeed(1),
|
||||
})
|
||||
const effect = CodeMode.execute({
|
||||
const effect = finished(CodeMode.execute({
|
||||
tools: { host: { echo } },
|
||||
code: `console.log("hi"); return await tools.host.echo({})`,
|
||||
limits: { maxToolCalls: 1 },
|
||||
})
|
||||
}))
|
||||
const first = await Effect.runPromise(effect)
|
||||
const second = await Effect.runPromise(effect)
|
||||
// Per-execution state (tool-call budget and audit list, logs, timeout bookkeeping) must
|
||||
@@ -771,7 +772,7 @@ describe("CodeMode public contract", () => {
|
||||
},
|
||||
])
|
||||
|
||||
const result = await Effect.runPromise(runtime.execute(`return search({ query: "order" })`))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(`return search({ query: "order" })`)))
|
||||
expect(result.ok).toBe(true)
|
||||
if (result.ok) {
|
||||
expect(result.value).toStrictEqual({
|
||||
@@ -825,7 +826,7 @@ describe("CodeMode public contract", () => {
|
||||
},
|
||||
])
|
||||
|
||||
const search = await Effect.runPromise(runtime.execute(`return search({ query: "resolve library id" })`))
|
||||
const search = await Effect.runPromise(finished(runtime.execute(`return search({ query: "resolve library id" })`)))
|
||||
expect(search.ok).toBe(true)
|
||||
if (search.ok) {
|
||||
expect(search.value).toStrictEqual({
|
||||
@@ -842,13 +843,13 @@ describe("CodeMode public contract", () => {
|
||||
}
|
||||
|
||||
const call = await Effect.runPromise(
|
||||
runtime.execute(`return await tools.context7["resolve-library-id"]({ libraryName: "TypeScript" })`),
|
||||
finished(runtime.execute(`return await tools.context7["resolve-library-id"]({ libraryName: "TypeScript" })`)),
|
||||
)
|
||||
expect(call.ok).toBe(true)
|
||||
if (call.ok) expect(call.value).toBe("/resolved/TypeScript")
|
||||
|
||||
const exact = await Effect.runPromise(
|
||||
runtime.execute(`return search({ query: 'tools.context7["resolve-library-id"]' })`),
|
||||
finished(runtime.execute(`return search({ query: 'tools.context7["resolve-library-id"]' })`)),
|
||||
)
|
||||
expect(exact.ok).toBe(true)
|
||||
if (exact.ok) expect(exact.value).toMatchObject({ remaining: 0, next: null })
|
||||
@@ -872,12 +873,12 @@ describe("CodeMode public contract", () => {
|
||||
})
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runtime.execute(`
|
||||
finished(runtime.execute(`
|
||||
return search({
|
||||
query: "send message attachment upload file to current Discord thread",
|
||||
limit: 2
|
||||
})
|
||||
`),
|
||||
`)),
|
||||
)
|
||||
expect(result.ok).toBe(true)
|
||||
if (!result.ok) return
|
||||
@@ -900,12 +901,12 @@ describe("CodeMode public contract", () => {
|
||||
expect(result.toolCalls).toStrictEqual([{ name: "search" }])
|
||||
|
||||
const variants = await Effect.runPromise(
|
||||
runtime.execute(`
|
||||
finished(runtime.execute(`
|
||||
return [
|
||||
search({ query: "file" }),
|
||||
search({ query: "image" })
|
||||
]
|
||||
`),
|
||||
`)),
|
||||
)
|
||||
expect(variants.ok).toBe(true)
|
||||
if (variants.ok) {
|
||||
@@ -925,7 +926,7 @@ describe("CodeMode public contract", () => {
|
||||
limits: { maxToolCalls: 1 },
|
||||
hooks: { "tool.before": (call) => Effect.sync(() => void observed.push(call)) },
|
||||
})
|
||||
const result = await Effect.runPromise(limited.execute(`search({}); return search({})`))
|
||||
const result = await Effect.runPromise(finished(limited.execute(`search({}); return search({})`)))
|
||||
expect(result.ok).toBe(false)
|
||||
if (!result.ok) expect(result.error.kind).toBe("ToolCallLimitExceeded")
|
||||
expect(observed).toEqual([{ name: "search", input: {}, key: expect.any(String) }])
|
||||
@@ -933,13 +934,13 @@ describe("CodeMode public contract", () => {
|
||||
|
||||
test("search is an opaque, shadowable global like other built-ins", async () => {
|
||||
const runtime = CodeMode.make({ tools })
|
||||
expect(await Effect.runPromise(runtime.execute(`return typeof search`))).toMatchObject({ value: "function" })
|
||||
expect(await Effect.runPromise(finished(runtime.execute(`return typeof search`)))).toMatchObject({ value: "function" })
|
||||
// A program-level declaration shadows the global, as JS module scope does.
|
||||
const shadowed = await Effect.runPromise(runtime.execute(`const search = () => "local"; return search()`))
|
||||
const shadowed = await Effect.runPromise(finished(runtime.execute(`const search = () => "local"; return search()`)))
|
||||
expect(shadowed.ok).toBe(true)
|
||||
if (shadowed.ok) expect(shadowed.value).toBe("local")
|
||||
// The reference itself vanishes at the data boundary, as functions do in JSON.stringify.
|
||||
const escaped = await Effect.runPromise(runtime.execute(`return { search }`))
|
||||
const escaped = await Effect.runPromise(finished(runtime.execute(`return { search }`)))
|
||||
expect(escaped).toMatchObject({ ok: true, value: {} })
|
||||
})
|
||||
|
||||
@@ -957,7 +958,7 @@ describe("CodeMode public contract", () => {
|
||||
},
|
||||
})
|
||||
|
||||
const browse = await Effect.runPromise(runtime.execute(`return search({})`))
|
||||
const browse = await Effect.runPromise(finished(runtime.execute(`return search({})`)))
|
||||
expect(browse.ok).toBe(true)
|
||||
if (browse.ok) {
|
||||
const value = browse.value as {
|
||||
@@ -971,7 +972,7 @@ describe("CodeMode public contract", () => {
|
||||
}
|
||||
|
||||
for (const query of ["many.tool13", "tools.many.tool13"]) {
|
||||
const exact = await Effect.runPromise(runtime.execute(`return search({ query: ${JSON.stringify(query)} })`))
|
||||
const exact = await Effect.runPromise(finished(runtime.execute(`return search({ query: ${JSON.stringify(query)} })`)))
|
||||
expect(exact.ok).toBe(true)
|
||||
if (exact.ok) {
|
||||
expect(exact.value).toStrictEqual({
|
||||
@@ -1005,7 +1006,7 @@ describe("CodeMode public contract", () => {
|
||||
})
|
||||
|
||||
// Empty query + namespace browses just that namespace, alphabetical by path.
|
||||
const browse = await Effect.runPromise(runtime.execute(`return search({ query: "", namespace: "github" })`))
|
||||
const browse = await Effect.runPromise(finished(runtime.execute(`return search({ query: "", namespace: "github" })`)))
|
||||
expect(browse.ok).toBe(true)
|
||||
if (browse.ok) {
|
||||
const value = browse.value as { items: Array<{ path: string }>; remaining: number }
|
||||
@@ -1017,7 +1018,7 @@ describe("CodeMode public contract", () => {
|
||||
}
|
||||
|
||||
// A query + namespace ranks within that namespace only.
|
||||
const scoped = await Effect.runPromise(runtime.execute(`return search({ query: "issues", namespace: "linear" })`))
|
||||
const scoped = await Effect.runPromise(finished(runtime.execute(`return search({ query: "issues", namespace: "linear" })`)))
|
||||
expect(scoped.ok).toBe(true)
|
||||
if (scoped.ok) {
|
||||
const value = scoped.value as { items: Array<{ path: string }>; remaining: number }
|
||||
@@ -1025,11 +1026,11 @@ describe("CodeMode public contract", () => {
|
||||
expect(value.items[0]?.path).toBe("tools.linear.list_issues")
|
||||
}
|
||||
|
||||
const prefixed = await Effect.runPromise(runtime.execute(`return search({ query: "", namespace: "tools.github" })`))
|
||||
const prefixed = await Effect.runPromise(finished(runtime.execute(`return search({ query: "", namespace: "tools.github" })`)))
|
||||
expect(prefixed.ok).toBe(true)
|
||||
if (prefixed.ok) expect((prefixed.value as { items: Array<unknown> }).items).toHaveLength(2)
|
||||
|
||||
const invalid = await Effect.runPromise(runtime.execute(`return search({ query: "issues", namespace: 7 })`))
|
||||
const invalid = await Effect.runPromise(finished(runtime.execute(`return search({ query: "issues", namespace: 7 })`)))
|
||||
expect(invalid.ok).toBe(false)
|
||||
if (!invalid.ok) expect(invalid.error.kind).toBe("InvalidToolInput")
|
||||
})
|
||||
@@ -1054,7 +1055,7 @@ describe("CodeMode public contract", () => {
|
||||
|
||||
// "attachment" appears in neither path nor description - only in the input schema's
|
||||
// property names, which the searchable text includes.
|
||||
const byParameter = await Effect.runPromise(runtime.execute(`return search({ query: "attachment" })`))
|
||||
const byParameter = await Effect.runPromise(finished(runtime.execute(`return search({ query: "attachment" })`)))
|
||||
expect(byParameter.ok).toBe(true)
|
||||
if (byParameter.ok) {
|
||||
const value = byParameter.value as { items: Array<{ path: string }>; remaining: number }
|
||||
@@ -1063,7 +1064,7 @@ describe("CodeMode public contract", () => {
|
||||
}
|
||||
|
||||
// Substring matching: a partial word ("docum") still hits the description.
|
||||
const bySubstring = await Effect.runPromise(runtime.execute(`return search({ query: "docum" })`))
|
||||
const bySubstring = await Effect.runPromise(finished(runtime.execute(`return search({ query: "docum" })`)))
|
||||
expect(bySubstring.ok).toBe(true)
|
||||
if (bySubstring.ok) {
|
||||
const value = bySubstring.value as { items: Array<{ path: string }>; remaining: number }
|
||||
@@ -1087,7 +1088,7 @@ describe("CodeMode public contract", () => {
|
||||
},
|
||||
})
|
||||
|
||||
const ranked = await Effect.runPromise(runtime.execute(`return search({ query: "zones" })`))
|
||||
const ranked = await Effect.runPromise(finished(runtime.execute(`return search({ query: "zones" })`)))
|
||||
expect(ranked.ok).toBe(true)
|
||||
if (ranked.ok) {
|
||||
const value = ranked.value as { items: Array<{ path: string }> }
|
||||
@@ -1116,7 +1117,7 @@ describe("CodeMode public contract", () => {
|
||||
})
|
||||
|
||||
// "issues" still finds the singular-only tool (term OR singular(term) per field)...
|
||||
const plural = await Effect.runPromise(runtime.execute(`return search({ query: "issues", namespace: "tracker" })`))
|
||||
const plural = await Effect.runPromise(finished(runtime.execute(`return search({ query: "issues", namespace: "tracker" })`)))
|
||||
expect(plural.ok).toBe(true)
|
||||
if (plural.ok) {
|
||||
const value = plural.value as { items: Array<{ path: string }>; remaining: number }
|
||||
@@ -1125,7 +1126,7 @@ describe("CodeMode public contract", () => {
|
||||
}
|
||||
|
||||
// ...while a true "issues" path match still outranks the singular-only description match.
|
||||
const ranked = await Effect.runPromise(runtime.execute(`return search({ query: "issues" })`))
|
||||
const ranked = await Effect.runPromise(finished(runtime.execute(`return search({ query: "issues" })`)))
|
||||
expect(ranked.ok).toBe(true)
|
||||
if (ranked.ok) {
|
||||
const value = ranked.value as { items: Array<{ path: string }>; remaining: number }
|
||||
@@ -1152,7 +1153,7 @@ describe("CodeMode public contract", () => {
|
||||
alpha: { beta: simple("Middle"), aardvark: simple("First") },
|
||||
},
|
||||
})
|
||||
const browse = await Effect.runPromise(runtime.execute(`return search({})`))
|
||||
const browse = await Effect.runPromise(finished(runtime.execute(`return search({})`)))
|
||||
expect(browse.ok).toBe(true)
|
||||
if (browse.ok) {
|
||||
const value = browse.value as { items: Array<{ path: string }>; remaining: number; next: unknown }
|
||||
@@ -1165,7 +1166,7 @@ describe("CodeMode public contract", () => {
|
||||
expect(value.next).toBeNull()
|
||||
}
|
||||
|
||||
const middle = await Effect.runPromise(runtime.execute(`return search({ limit: 1, offset: 1 })`))
|
||||
const middle = await Effect.runPromise(finished(runtime.execute(`return search({ limit: 1, offset: 1 })`)))
|
||||
expect(middle.ok).toBe(true)
|
||||
if (middle.ok) {
|
||||
expect(middle.value).toMatchObject({
|
||||
@@ -1175,7 +1176,7 @@ describe("CodeMode public contract", () => {
|
||||
})
|
||||
}
|
||||
|
||||
const exhausted = await Effect.runPromise(runtime.execute(`return search({ limit: 1, offset: 3 })`))
|
||||
const exhausted = await Effect.runPromise(finished(runtime.execute(`return search({ limit: 1, offset: 3 })`)))
|
||||
expect(exhausted.ok).toBe(true)
|
||||
if (exhausted.ok) expect(exhausted.value).toStrictEqual({ items: [], remaining: 0, next: null })
|
||||
})
|
||||
@@ -1197,11 +1198,11 @@ describe("CodeMode public contract", () => {
|
||||
hooks: { "tool.before": (call) => Effect.sync(() => void observed.push(call.input)) },
|
||||
})
|
||||
|
||||
const success = await Effect.runPromise(runtime.execute(`return await tools.math.double({ value: "21" })`))
|
||||
const success = await Effect.runPromise(finished(runtime.execute(`return await tools.math.double({ value: "21" })`)))
|
||||
expect(success).toStrictEqual({ ok: true, value: 42, toolCalls: [{ name: "math.double" }] })
|
||||
expect(observed).toStrictEqual([{ value: 21 }, 21])
|
||||
|
||||
const invalid = await Effect.runPromise(runtime.execute(`return await tools.math.double({ value: 21 })`))
|
||||
const invalid = await Effect.runPromise(finished(runtime.execute(`return await tools.math.double({ value: 21 })`)))
|
||||
expect(invalid.ok).toBe(false)
|
||||
if (invalid.ok) return
|
||||
expect(invalid.error.kind).toBe("InvalidToolInput")
|
||||
@@ -1210,47 +1211,47 @@ describe("CodeMode public contract", () => {
|
||||
|
||||
test("returns JSON-safe data: undefined vanishes as in JSON.stringify, and a bare undefined is null", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: `return { top: undefined, nested: [1, undefined] }`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(result).toStrictEqual({
|
||||
ok: true,
|
||||
value: { nested: [1, null] },
|
||||
toolCalls: [],
|
||||
})
|
||||
expect(await Effect.runPromise(CodeMode.execute({ code: `return undefined` }))).toMatchObject({ value: null })
|
||||
expect(await Effect.runPromise(finished(CodeMode.execute({ code: `return undefined` })))).toMatchObject({ value: null })
|
||||
expect(Schema.decodeUnknownSync(CodeMode.Result)(JSON.parse(JSON.stringify(result)))).toStrictEqual(result)
|
||||
})
|
||||
|
||||
test("returns the final top-level expression when return is omitted", async () => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code: `1; 2` }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code: `1; 2` })))
|
||||
|
||||
expect(result).toStrictEqual({ ok: true, value: 2, toolCalls: [] })
|
||||
})
|
||||
|
||||
test("does not implicitly return expressions nested in control flow", async () => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code: `if (true) { 2 }` }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code: `if (true) { 2 }` })))
|
||||
|
||||
expect(result).toStrictEqual({ ok: true, value: null, toolCalls: [] })
|
||||
})
|
||||
|
||||
test("returns null when the final top-level statement is not an expression", async () => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code: `1; const value = 2` }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code: `1; const value = 2` })))
|
||||
|
||||
expect(result).toStrictEqual({ ok: true, value: null, toolCalls: [] })
|
||||
})
|
||||
|
||||
test("rejects invalid configuration and search limits", async () => {
|
||||
expect(() => CodeMode.execute({ code: "return 1", limits: { timeoutMs: 0 } })).toThrow(RangeError)
|
||||
expect(() => CodeMode.execute({ code: "return 1", limits: { timeoutMs: Number.POSITIVE_INFINITY } })).toThrow(
|
||||
expect(() => finished(CodeMode.execute({ code: "return 1", limits: { timeoutMs: 0 } }))).toThrow(RangeError)
|
||||
expect(() => finished(CodeMode.execute({ code: "return 1", limits: { timeoutMs: Number.POSITIVE_INFINITY } }))).toThrow(
|
||||
RangeError,
|
||||
)
|
||||
expect(() => CodeMode.execute({ code: "return 1", limits: { maxToolCalls: -1 } })).toThrow(RangeError)
|
||||
expect(() => CodeMode.execute({ code: "return 1", limits: { maxOutputBytes: -1 } })).toThrow(RangeError)
|
||||
expect(() => finished(CodeMode.execute({ code: "return 1", limits: { maxToolCalls: -1 } }))).toThrow(RangeError)
|
||||
expect(() => finished(CodeMode.execute({ code: "return 1", limits: { maxOutputBytes: -1 } }))).toThrow(RangeError)
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.make({ tools }).execute(`return search({ query: "order", limit: 0.5 })`),
|
||||
finished(CodeMode.make({ tools }).execute(`return search({ query: "order", limit: 0.5 })`)),
|
||||
)
|
||||
expect(result.ok).toBe(false)
|
||||
if (result.ok) return
|
||||
@@ -1258,7 +1259,7 @@ describe("CodeMode public contract", () => {
|
||||
|
||||
for (const offset of [-1, 0.5, Number.MAX_SAFE_INTEGER + 1, "1"]) {
|
||||
const invalidOffset = await Effect.runPromise(
|
||||
CodeMode.make({ tools }).execute(`return search({ query: "order", offset: ${JSON.stringify(offset)} })`),
|
||||
finished(CodeMode.make({ tools }).execute(`return search({ query: "order", offset: ${JSON.stringify(offset)} })`)),
|
||||
)
|
||||
expect(invalidOffset.ok).toBe(false)
|
||||
if (!invalidOffset.ok) expect(invalidOffset.error.kind).toBe("InvalidToolInput")
|
||||
@@ -1266,7 +1267,7 @@ describe("CodeMode public contract", () => {
|
||||
})
|
||||
|
||||
test("enforces the tool-call limit as a diagnostic", async () => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ tools, code: source, limits: { maxToolCalls: 0 } }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ tools, code: source, limits: { maxToolCalls: 0 } })))
|
||||
expect(result.ok).toBe(false)
|
||||
if (!result.ok) expect(result.error.kind).toBe("ToolCallLimitExceeded")
|
||||
})
|
||||
@@ -1281,14 +1282,14 @@ describe("CodeMode public contract", () => {
|
||||
execute: () => Effect.succeed(1),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
tools: { host: { count: counter } },
|
||||
code: `
|
||||
let total = 0
|
||||
for (let i = 0; i < 150; i += 1) total += await tools.host.count({})
|
||||
return total
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(result).toMatchObject({ ok: true, value: 150 })
|
||||
if (result.ok) expect(result.toolCalls.length).toBe(150)
|
||||
@@ -1299,7 +1300,7 @@ describe("CodeMode public contract", () => {
|
||||
// The Effect fiber runtime auto-yields between interpreter steps, so a pure `while
|
||||
// (true) {}` loop is interrupted by `timeoutMs` alone.
|
||||
const startedAt = Date.now()
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code: "while (true) {}", limits: { timeoutMs: 200 } }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code: "while (true) {}", limits: { timeoutMs: 200 } })))
|
||||
const elapsedMs = Date.now() - startedAt
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
|
||||
@@ -15,9 +15,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -45,9 +45,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { CodeMode, Tool } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// Key enumeration: Object.keys and for...in share one surface over plain objects, arrays
|
||||
// (index strings), and tool references (namespace/tool names from the supplied tools), so a
|
||||
@@ -22,7 +23,7 @@ const tools = {
|
||||
playwright: { navigate: echo("Navigate somewhere") },
|
||||
}
|
||||
|
||||
const run = (code: string) => Effect.runPromise(CodeMode.execute({ tools, code }))
|
||||
const run = (code: string) => Effect.runPromise(finished(CodeMode.execute({ tools, code })))
|
||||
const value = async (code: string) => {
|
||||
const result = await run(code)
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// One failure is one program error object, and rethrowing it keeps the diagnostic it started with.
|
||||
const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const run = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
const value = async (code: string) => {
|
||||
const result = await run(code)
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Context, Effect, Schema } from "effect"
|
||||
import { CodeMode, Extension, Tool } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const held: Array<unknown> = []
|
||||
const config = { retries: 3, nested: { deep: true } }
|
||||
@@ -34,13 +35,13 @@ const extension = Extension.make({
|
||||
const runtime = CodeMode.make({ tools: {}, extensions: [extension] })
|
||||
|
||||
const value = async (code: string, target = runtime) => {
|
||||
const result = await Effect.runPromise(target.execute(code))
|
||||
const result = await Effect.runPromise(finished(target.execute(code)))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
const failure = async (code: string, target = runtime) => {
|
||||
const result = await Effect.runPromise(target.execute(code))
|
||||
const result = await Effect.runPromise(finished(target.execute(code)))
|
||||
if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`)
|
||||
return result.error
|
||||
}
|
||||
@@ -441,8 +442,8 @@ describe("Effect globals", () => {
|
||||
test("an Effect global is a promise that runs with the execution's services", async () => {
|
||||
sent.length = 0
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.make({ extensions: [web] })
|
||||
.execute(`const pending = fetch("https://a.test/"); return [pending instanceof Promise, await pending]`)
|
||||
finished(CodeMode.make({ extensions: [web] })
|
||||
.execute(`const pending = fetch("https://a.test/"); return [pending instanceof Promise, await pending]`))
|
||||
.pipe(Effect.provideService(Session, { id: "ses_1" })),
|
||||
)
|
||||
expect(result).toMatchObject({ ok: true, value: [true, { url: "https://a.test/", session: "ses_1" }] })
|
||||
@@ -451,8 +452,8 @@ describe("Effect globals", () => {
|
||||
|
||||
test("a failing Effect rejects with the host's error", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.make({ extensions: [web] })
|
||||
.execute(`try { await fail() } catch (e) { return [e instanceof RangeError, e.message] }`)
|
||||
finished(CodeMode.make({ extensions: [web] })
|
||||
.execute(`try { await fail() } catch (e) { return [e instanceof RangeError, e.message] }`))
|
||||
.pipe(Effect.provideService(Session, { id: "ses_1" })),
|
||||
)
|
||||
expect(result).toMatchObject({ ok: true, value: [true, "denied"] })
|
||||
@@ -465,8 +466,8 @@ describe("Effect globals", () => {
|
||||
hooks: { "extension.before": (call) => Effect.fail(new Error(`${call.name} is not allowed`)) },
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
runtime
|
||||
.execute(`try { await fetch("https://a.test/") } catch (e) { return e.message }`)
|
||||
finished(runtime
|
||||
.execute(`try { await fetch("https://a.test/") } catch (e) { return e.message }`))
|
||||
.pipe(Effect.provideService(Session, { id: "ses_1" })),
|
||||
)
|
||||
expect(result).toMatchObject({ ok: true, value: "fetch is not allowed" })
|
||||
@@ -616,7 +617,7 @@ describe("configuration", () => {
|
||||
test("extension calls are not tool calls", async () => {
|
||||
const limited = CodeMode.make({ extensions: [extension], limits: { maxToolCalls: 0 } })
|
||||
const result = await Effect.runPromise(
|
||||
limited.execute(`(await fetch("https://a.test/")).json(); return await later(1)`),
|
||||
finished(limited.execute(`(await fetch("https://a.test/")).json(); return await later(1)`)),
|
||||
)
|
||||
expect(result.ok).toBe(true)
|
||||
expect(result.toolCalls).toEqual([])
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Effect, type Scope } from "effect"
|
||||
import type { CodeMode } from "../src/index.js"
|
||||
|
||||
/** Runs a started program to its end in a scope of its own: its result. */
|
||||
export const finished = <E, R>(
|
||||
started: Effect.Effect<CodeMode.Execution, E, R>,
|
||||
): Effect.Effect<CodeMode.Result, E, Exclude<R, Scope.Scope>> =>
|
||||
Effect.scoped(
|
||||
Effect.flatMap(started, (execution) =>
|
||||
Effect.map(execution.result, (outcome) => {
|
||||
if (outcome.ok === "suspended") throw new Error("The program suspended.")
|
||||
return outcome
|
||||
}),
|
||||
),
|
||||
)
|
||||
+3
-2
@@ -2,6 +2,7 @@
|
||||
// while a prelude frame was on the stack.
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../../src/index.js"
|
||||
import { finished } from "../finished.js"
|
||||
import { inPrelude } from "../../src/interpreter/source.js"
|
||||
import { stats } from "../../src/machine/task.js"
|
||||
|
||||
@@ -12,13 +13,13 @@ stats.inspect = (stack, task) => {
|
||||
if (task.slice && stack.some((frame) => frame.node !== undefined && inPrelude(frame.node))) saved++
|
||||
}
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
tools: {},
|
||||
code: `
|
||||
const groups = Object.groupBy([1, 2, 3, 4, 5, 6], (x) => (x % 2 ? "odd" : "even"))
|
||||
const union = new Set([1, 2, 3]).union({ size: 2, has: () => true, keys: () => [7, 8][Symbol.iterator]() })
|
||||
const parsed = JSON.parse('{"a":[1,2,{"b":3}]}', (k, v) => (typeof v === "number" ? v * 10 : v))
|
||||
return [groups, [...union], parsed, [1, 2, 3].values().reduce((a, b) => a + b)]`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
console.log(JSON.stringify({ result, saved }))
|
||||
|
||||
+4
-2
@@ -12,5 +12,7 @@ const slow = Tool.make({
|
||||
})
|
||||
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)))
|
||||
const resumed = CodeMode.make({ tools: { slow } }).resume(bytes, { [key!]: { ok: true, value: { value: 20 } } })
|
||||
console.log(
|
||||
JSON.stringify(await Effect.runPromise(Effect.scoped(Effect.flatMap(resumed, (execution) => execution.result)))),
|
||||
)
|
||||
|
||||
@@ -15,8 +15,9 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const execute = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await execute(code)
|
||||
|
||||
@@ -13,8 +13,9 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const execute = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await execute(code)
|
||||
|
||||
@@ -31,8 +31,9 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const execute = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await execute(code)
|
||||
|
||||
@@ -29,9 +29,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -13,8 +13,9 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const execute = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await execute(code)
|
||||
|
||||
@@ -27,9 +27,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -2,17 +2,18 @@ import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Fiber } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { MAX_ARRAY_LENGTH, MAX_PENDING_PROMISES, MAX_STRING_LENGTH } from "../src/interpreter/limits.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const runtime = CodeMode.make({ tools: {} })
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(runtime.execute(code))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(code)))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
const failure = async (code: string) => {
|
||||
const result = await Effect.runPromise(runtime.execute(code))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(code)))
|
||||
if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`)
|
||||
return result.error.message
|
||||
}
|
||||
@@ -85,7 +86,7 @@ describe("the host stays in control", () => {
|
||||
),
|
||||
),
|
||||
)
|
||||
const result = yield* runtime.execute(`let t = 0; for (let i = 0; i < 1_000_000; i++) t += i % 3; return t`)
|
||||
const result = yield* finished(runtime.execute(`let t = 0; for (let i = 0; i < 1_000_000; i++) t += i % 3; return t`))
|
||||
yield* Fiber.interrupt(counter)
|
||||
return result
|
||||
}),
|
||||
@@ -109,7 +110,7 @@ describe("the host stays in control", () => {
|
||||
`const a = Array(10_000).fill(0); for (let i = 0; i < 20_000; i++) Promise.reject(a); return 1`,
|
||||
],
|
||||
])("%s still times out", async (_, code) => {
|
||||
const result = await Effect.runPromise(CodeMode.make({ limits: { timeoutMs: 100 } }).execute(code))
|
||||
const result = await Effect.runPromise(finished(CodeMode.make({ limits: { timeoutMs: 100 } }).execute(code)))
|
||||
expect(result).toMatchObject({ ok: false, error: { kind: "TimeoutExceeded" } })
|
||||
})
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import { ScopeStack } from "../src/interpreter/scope.js"
|
||||
import { AsyncFromSyncIterator } from "../src/machine/generators.js"
|
||||
import { stats } from "../src/machine/task.js"
|
||||
import { ToolRuntime } from "../src/tool-runtime.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// The machine (`src/machine`) runs programs as explicit frames, and keeps the jobs, promises, and host calls they wait
|
||||
// on as data. The Effect driver only does what the machine's stops ask.
|
||||
@@ -28,7 +29,7 @@ const runtimeTools = {
|
||||
|
||||
const runtime = CodeMode.make({ tools: runtimeTools })
|
||||
|
||||
const run = (code: string) => Effect.runPromise(runtime.execute(code))
|
||||
const run = (code: string) => Effect.runPromise(finished(runtime.execute(code)))
|
||||
|
||||
const value = async (code: string) => {
|
||||
const before = stats.steps
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { CodeMode, Tool } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// `new` is supported syntax; only the callee decides whether construction succeeds. A callee without
|
||||
// construction support is a TypeError naming it, like JS, rather than an unsupported-syntax diagnostic
|
||||
@@ -13,7 +14,7 @@ const tools = {
|
||||
execute: () => Effect.succeed({}),
|
||||
}),
|
||||
}
|
||||
const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools }))
|
||||
const run = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools })))
|
||||
const value = async (code: string) => {
|
||||
const result = await run(code)
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
|
||||
@@ -38,9 +38,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Effect, Layer, Option } from "effect"
|
||||
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"
|
||||
import { CodeMode, OpenAPI, Tool } from "../src/index.js"
|
||||
import { inputTypeScript, outputTypeScript } from "../src/tool-schema.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const baseUrl = "http://localhost:4096"
|
||||
type Document = OpenAPI.Document
|
||||
@@ -162,7 +163,7 @@ describe("OpenAPI.fromSpec", () => {
|
||||
expect(outputTypeScript(remove)).toBe("null")
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.make({ tools: { api: api.tools } })
|
||||
finished(CodeMode.make({ tools: { api: api.tools } })
|
||||
.execute(
|
||||
`
|
||||
const user = await tools.api.users.get({
|
||||
@@ -183,7 +184,7 @@ describe("OpenAPI.fromSpec", () => {
|
||||
const removed = await tools.api.users.remove({ userId: "user-1" })
|
||||
return { user, created, summary, removed }
|
||||
`,
|
||||
)
|
||||
))
|
||||
.pipe(Effect.provide(client.layer)),
|
||||
)
|
||||
|
||||
@@ -971,12 +972,12 @@ describe("OpenAPI.fromSpec", () => {
|
||||
tools: { api: OpenAPI.fromSpec({ spec: await happyPathSpec(), baseUrl }).tools },
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
runtime
|
||||
finished(runtime
|
||||
.execute(
|
||||
`
|
||||
return search({ query: "get a user", namespace: "api", limit: 1 })
|
||||
`,
|
||||
)
|
||||
))
|
||||
.pipe(Effect.provide(layer)),
|
||||
)
|
||||
|
||||
@@ -1402,7 +1403,7 @@ describe("OpenAPI.fromSpec", () => {
|
||||
})
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runtime.execute("return await tools.api.records.get({})").pipe(Effect.provide(layer)),
|
||||
finished(runtime.execute("return await tools.api.records.get({})")).pipe(Effect.provide(layer)),
|
||||
)
|
||||
|
||||
expect(result).toMatchObject({ ok: false })
|
||||
@@ -1468,14 +1469,14 @@ describe("OpenAPI.fromSpec", () => {
|
||||
|
||||
const runtime = CodeMode.make({ tools })
|
||||
const result = await Effect.runPromise(
|
||||
runtime
|
||||
finished(runtime
|
||||
.execute(
|
||||
`
|
||||
const updated = await tools.things.update({ path_id: "path", query_id: "query", path_id_2: "literal", header_id: "header", body_id: "body" })
|
||||
const echoed = await tools.echo({ body: "hello" })
|
||||
return { updated, echoed }
|
||||
`,
|
||||
)
|
||||
))
|
||||
.pipe(Effect.provide(layer)),
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// Runs a CodeMode program with no host tools and returns the CodeMode.Result. These tests pin the
|
||||
// JS-parity behaviors for the "99% of ordinary defensive JavaScript just works" goal: cases where
|
||||
@@ -9,7 +10,7 @@ import { CodeMode } from "../src/index.js"
|
||||
// Note on the result boundary: this package normalizes a bare `undefined` result to `null` when
|
||||
// it crosses out of CodeMode (results are JSON data), so tests asserting an in-CodeMode
|
||||
// `undefined` read check `=== undefined` inside the program and `null` at the boundary.
|
||||
const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const run = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
const value = async (code: string) => {
|
||||
const result = await run(code)
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
|
||||
@@ -15,8 +15,9 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const execute = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {}, limits: { timeoutMs: 1_000 } }))
|
||||
const execute = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {}, limits: { timeoutMs: 1_000 } })))
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await execute(code)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Cause, Deferred, Effect, Exit, Schema } from "effect"
|
||||
import { CodeMode, Tool, toolError } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// Wave 5 acceptance suite: first-class promise values. Un-awaited tool calls start eagerly as
|
||||
// host work, `await` settles them, Promise.all/allSettled/race/resolve/reject are
|
||||
@@ -149,7 +150,7 @@ const run = (
|
||||
return created
|
||||
}
|
||||
return Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
tools: {
|
||||
host: {
|
||||
echo: echoTool(trace),
|
||||
@@ -164,7 +165,7 @@ const run = (
|
||||
},
|
||||
code,
|
||||
...(options.limits ? { limits: options.limits } : {}),
|
||||
}),
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1649,7 +1650,7 @@ describe("host results", () => {
|
||||
|
||||
test("an awaited interruption passes catch and finally and interrupts the program", async () => {
|
||||
const exit = await Effect.runPromiseExit(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
tools: { host: { interrupt: interruptedTool } },
|
||||
code: `
|
||||
const inner = async () => {
|
||||
@@ -1657,7 +1658,7 @@ describe("host results", () => {
|
||||
}
|
||||
return await inner().catch(() => "handled")
|
||||
`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true)
|
||||
})
|
||||
|
||||
@@ -42,9 +42,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import { normalizeError } from "../src/interpreter/errors.js"
|
||||
import { Effect } from "effect"
|
||||
import { ToolRuntime } from "../src/tool-runtime.js"
|
||||
import { nodeId, sourceOf } from "../src/interpreter/source.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// Groundwork for suspending and resuming a run: everything the state refers to has a stable name.
|
||||
|
||||
@@ -137,7 +138,7 @@ describe("natives only at intrinsic creation", () => {
|
||||
})
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(result.error.message)
|
||||
return result.value
|
||||
}
|
||||
@@ -260,7 +261,7 @@ describe("heap as data", () => {
|
||||
for (const [name, code] of Object.entries(programs)) {
|
||||
test(name, async () => {
|
||||
const expected: unknown = await new Function(`return (async () => {${code}})()`)()
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(result.error.message)
|
||||
expect(result.value).toEqual(JSON.parse(JSON.stringify(expected)))
|
||||
})
|
||||
@@ -292,7 +293,7 @@ describe("prelude", () => {
|
||||
expect(names.all()).toContain("%PreludeHelpers%.$Call")
|
||||
})
|
||||
test("programs can't see the helpers or internal functions, and failures inside are located at the call", async () => {
|
||||
const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const run = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
expect(await run("return [typeof $Call, typeof $Throw, typeof fillCollection]")).toMatchObject({
|
||||
value: ["undefined", "undefined", "undefined"],
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Result } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
describe("CodeMode.scanTools", () => {
|
||||
test("extracts dotted, bracketed and nested static tool calls in first-seen order", () => {
|
||||
@@ -41,18 +42,18 @@ describe("CodeMode.scanTools", () => {
|
||||
|
||||
test("binds outer globals that programs can read or shadow while keeping line numbers", async () => {
|
||||
const read = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: "return { prev: state.count, runs, same: input.state.count }",
|
||||
bindings: { state: { count: 4 }, runs: 2, input: { state: { count: 4 } } },
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(read).toMatchObject({ ok: true, value: { prev: 4, runs: 2, same: 4 } })
|
||||
|
||||
const shadowed = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
code: "const state = 9\nreturn { wake: true, state }",
|
||||
bindings: { state: { count: 4 } },
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(shadowed).toMatchObject({ ok: true, value: { wake: true, state: 9 } })
|
||||
})
|
||||
|
||||
@@ -72,9 +72,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
jsonSchemaToTypeScript,
|
||||
outputTypeScript,
|
||||
} from "../src/tool-schema.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// A raw JSON Schema tool in the shape an MCP adapter produces: render-only input schema
|
||||
// whose property descriptions and constraints must surface as JSDoc in pretty signatures.
|
||||
@@ -863,7 +864,7 @@ describe("JSDoc signatures in catalogs and search results", () => {
|
||||
].join("\n")
|
||||
const signature = `tools.constrained(${type}): Promise<${type}>`
|
||||
expect(runtime.catalog[0]?.signature).toBe(signature)
|
||||
const result = await Effect.runPromise(runtime.execute('return search({ query: "tools.constrained" })'))
|
||||
const result = await Effect.runPromise(finished(runtime.execute('return search({ query: "tools.constrained" })')))
|
||||
expect(result.ok).toBe(true)
|
||||
if (!result.ok) throw new Error("search failed")
|
||||
expect(result.value).toMatchObject({ items: [{ signature }] })
|
||||
@@ -872,7 +873,7 @@ describe("JSDoc signatures in catalogs and search results", () => {
|
||||
const runtime = CodeMode.make({ tools: { github: { list_issues: listIssues }, orders: { lookup: lookupOrder } } })
|
||||
|
||||
const search = async (query: string) => {
|
||||
const result = await Effect.runPromise(runtime.execute(`return search({ query: ${JSON.stringify(query)} })`))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(`return search({ query: ${JSON.stringify(query)} })`)))
|
||||
expect(result.ok).toBe(true)
|
||||
if (!result.ok) throw new Error("search failed")
|
||||
return result.value as { items: Array<{ path: string; signature: string }>; remaining: number }
|
||||
@@ -955,11 +956,11 @@ describe("empty input signatures agree with decoding", () => {
|
||||
})
|
||||
const signature = "tools.ping(): Promise<string>"
|
||||
expect(runtime.catalog[0]?.signature).toBe(signature)
|
||||
const result = await Effect.runPromise(runtime.execute('return search({ query: "tools.ping" })'))
|
||||
const result = await Effect.runPromise(finished(runtime.execute('return search({ query: "tools.ping" })')))
|
||||
expect(result.ok).toBe(true)
|
||||
if (!result.ok) throw new Error("search failed")
|
||||
expect(result.value).toMatchObject({ items: [{ signature }] })
|
||||
expect(await Effect.runPromise(runtime.execute("return await tools.ping()"))).toMatchObject({
|
||||
expect(await Effect.runPromise(finished(runtime.execute("return await tools.ping()")))).toMatchObject({
|
||||
ok: true,
|
||||
value: "pong",
|
||||
})
|
||||
@@ -981,7 +982,7 @@ describe("empty input signatures agree with decoding", () => {
|
||||
},
|
||||
})
|
||||
expect(runtime.catalog[0]?.signature).not.toBe("tools.take(): Promise<string>")
|
||||
expect(await Effect.runPromise(runtime.execute(`return await tools.take(${call})`))).toMatchObject({
|
||||
expect(await Effect.runPromise(finished(runtime.execute(`return await tools.take(${call})`)))).toMatchObject({
|
||||
ok: true,
|
||||
value: "taken",
|
||||
})
|
||||
@@ -1003,15 +1004,15 @@ describe("empty input signatures agree with decoding", () => {
|
||||
const signature = "tools.consume({\n token: string,\n}): Promise<string>"
|
||||
expect(() => decodeInput(consume, {})).toThrow()
|
||||
expect(runtime.catalog[0]?.signature).toBe(signature)
|
||||
const result = await Effect.runPromise(runtime.execute('return search({ query: "tools.consume" })'))
|
||||
const result = await Effect.runPromise(finished(runtime.execute('return search({ query: "tools.consume" })')))
|
||||
expect(result.ok).toBe(true)
|
||||
if (!result.ok) throw new Error("search failed")
|
||||
expect(result.value).toMatchObject({ items: [{ signature }] })
|
||||
expect(await Effect.runPromise(runtime.execute("return await tools.consume()"))).toMatchObject({
|
||||
expect(await Effect.runPromise(finished(runtime.execute("return await tools.consume()")))).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "InvalidToolInput" },
|
||||
})
|
||||
expect(await Effect.runPromise(runtime.execute('return await tools.consume({ token: "t" })'))).toMatchObject({
|
||||
expect(await Effect.runPromise(finished(runtime.execute('return await tools.consume({ token: "t" })')))).toMatchObject({
|
||||
ok: true,
|
||||
value: "consumed",
|
||||
})
|
||||
@@ -1074,7 +1075,7 @@ describe("non-identifier tool paths", () => {
|
||||
})
|
||||
|
||||
test("search results return callable bracket-notation paths and signatures", async () => {
|
||||
const result = await Effect.runPromise(runtime.execute(`return search({ query: "resolve library" })`))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(`return search({ query: "resolve library" })`)))
|
||||
expect(result.ok).toBe(true)
|
||||
if (!result.ok) throw new Error("search failed")
|
||||
|
||||
|
||||
@@ -17,13 +17,14 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { CodeMode, Tool } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
// Standard-library value types: Date, RegExp, Map, Set. Programs use them as ordinary JS;
|
||||
// intra-CodeMode checkpoints (Object.* helpers, spread, coercion inputs) preserve the live
|
||||
// values. JSON.stringify keeps Date -> ISO string (invalid -> null), URL -> href, and
|
||||
// RegExp/Map/Set/URLSearchParams -> {}. The host boundary matches that except URLSearchParams,
|
||||
// which cross as their query string, and Set, which crosses as an array.
|
||||
const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const run = (code: string) => Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
const value = async (code: string) => {
|
||||
const result = await run(code)
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
@@ -427,18 +428,18 @@ describe("RegExp", () => {
|
||||
execute: (input) => Effect.succeed(`[${input}]`),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
tools: { host: { decorate } },
|
||||
code: `return "a1b22".replace(/\\d+/g, async (match) => await tools.host.decorate(match))`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(result.ok && result.value).toBe("a[object Promise]b[object Promise]")
|
||||
|
||||
const missingAwait = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
tools: { host: { decorate } },
|
||||
code: `return "a1".replace(/\\d/, (match) => tools.host.decorate(match))`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(missingAwait.ok && missingAwait.value).toBe("a[object Promise]")
|
||||
})
|
||||
@@ -1975,10 +1976,10 @@ describe("CodeMode values at intra-CodeMode checkpoints", () => {
|
||||
}),
|
||||
})
|
||||
const result = await Effect.runPromise(
|
||||
CodeMode.execute({
|
||||
finished(CodeMode.execute({
|
||||
tools: { host: { capture } },
|
||||
code: `return await tools.host.capture({ when: new Date(0), tags: new Map([["a", 1]]) })`,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
expect(result.ok).toBe(true)
|
||||
expect(observed).toStrictEqual([{ when: "1970-01-01T00:00:00.000Z", tags: {} }])
|
||||
|
||||
@@ -152,6 +152,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
type Argument = string | number | undefined
|
||||
type Outcome = "undefined" | "length" | "RangeError"
|
||||
@@ -170,7 +171,7 @@ type Vector = {
|
||||
}
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -25,9 +25,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
type Vector = {
|
||||
readonly path: string
|
||||
@@ -91,7 +92,7 @@ type Vector = {
|
||||
}
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -103,9 +103,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -1,13 +1,26 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { tmpdir } from "node:os"
|
||||
import path from "node:path"
|
||||
import { Effect, Fiber, Schema } from "effect"
|
||||
import { Effect, Fiber, Schema, Scope } 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 })
|
||||
|
||||
/** Starts a program, or resumes one, in a scope of its own that outlives the test's Effects. */
|
||||
const start = <E>(started: Effect.Effect<CodeMode.Execution, E, Scope.Scope>) => {
|
||||
const execution = Effect.runFork(Scope.provide(Scope.makeUnsafe())(started))
|
||||
return {
|
||||
result: Effect.flatMap(Fiber.join(execution), (running) => running.result),
|
||||
suspend: Effect.flatMap(Fiber.join(execution), (running) => running.suspend),
|
||||
}
|
||||
}
|
||||
|
||||
/** Why resuming a state fails. */
|
||||
const refusal = (resumed: Effect.Effect<CodeMode.Execution, CodeMode.SnapshotError, Scope.Scope>) =>
|
||||
Effect.runPromise(Effect.flip(Effect.scoped(resumed)))
|
||||
|
||||
/** A promise and its resolver, for host work a test finishes by hand. */
|
||||
const latch = <A = void>() => {
|
||||
let resolve!: (value: A) => void
|
||||
@@ -106,19 +119,24 @@ describe("suspend and resume", () => {
|
||||
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 run = start(CodeMode.make({ tools: { slow: first.tool } }).execute(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" }])
|
||||
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,
|
||||
start(
|
||||
CodeMode.make({ tools: { slow: first.tool } }).resume(state, {
|
||||
"run-1:0": { ok: true, value: { value: 41 } },
|
||||
}),
|
||||
).result,
|
||||
)
|
||||
expect(outcome).toEqual({
|
||||
ok: true,
|
||||
@@ -131,20 +149,25 @@ describe("suspend and resume", () => {
|
||||
|
||||
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 run = start(
|
||||
CodeMode.make({ tools: { slow: first.tool } }).execute(`
|
||||
const seen = new Map([["first", await tools.slow({ value: 10 })]])
|
||||
const second = await tools.slow({ value: 21 })
|
||||
return [seen.get("first").value, second.value]`)
|
||||
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 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" }] })
|
||||
@@ -155,10 +178,18 @@ describe("suspend and resume", () => {
|
||||
|
||||
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)))
|
||||
const suspended = suspendedOf(
|
||||
await Effect.runPromise(
|
||||
Effect.scoped(
|
||||
Effect.flatMap(
|
||||
runtime.execute(`let n = 0; for (let i = 0; i < 10; i++) n += i; return n`),
|
||||
(run) => run.suspend,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
expect(suspended.pending).toEqual([])
|
||||
expect(await Effect.runPromise(runtime.resume(suspended.state).result)).toEqual({
|
||||
expect(await Effect.runPromise(start(runtime.resume(suspended.state)).result)).toEqual({
|
||||
ok: true,
|
||||
value: 45,
|
||||
toolCalls: [],
|
||||
@@ -168,9 +199,11 @@ describe("suspend and resume", () => {
|
||||
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",
|
||||
})
|
||||
const run = start(
|
||||
runtime.execute(`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))
|
||||
@@ -179,7 +212,7 @@ describe("suspend and resume", () => {
|
||||
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,
|
||||
start(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"])
|
||||
@@ -188,10 +221,12 @@ describe("suspend and resume", () => {
|
||||
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" },
|
||||
const run = start(
|
||||
CodeMode.make({ tools: { job: jobs.tool }, suspend: { graceMs: 0 } }).execute(
|
||||
`return (await tools.job({ value: 3 })).value`,
|
||||
undefined,
|
||||
{ runId: "c" },
|
||||
),
|
||||
)
|
||||
Effect.runFork(run.result)
|
||||
await jobs.started()
|
||||
@@ -202,7 +237,7 @@ describe("suspend and resume", () => {
|
||||
const fresh = durableJobs()
|
||||
fresh.jobs.set("c:0", jobs.jobs.get("c:0") ?? { input: 3, result: latch<number>(), cancelled: false })
|
||||
if (!found) fresh.jobs.clear()
|
||||
const resumed = CodeMode.make({ tools: { job: fresh.tool } }).resume(suspended.state)
|
||||
const resumed = start(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)
|
||||
@@ -220,15 +255,19 @@ describe("suspend and resume", () => {
|
||||
}`
|
||||
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",
|
||||
})
|
||||
const run = start(
|
||||
CodeMode.make({ tools: { work: first.tool }, suspend: { graceMs: 0 } }).execute(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)
|
||||
const result = Effect.runPromise(
|
||||
start(CodeMode.make({ tools: { work: second.tool } }).resume(suspended.state)).result,
|
||||
)
|
||||
if (retry === "safe") {
|
||||
await second.called()
|
||||
expect(second.calls[0]!.input).toBe(4)
|
||||
@@ -267,7 +306,7 @@ describe("suspend and resume", () => {
|
||||
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" })
|
||||
const run = start(CodeMode.make({ tools, suspend: { graceMs: 20 } }).execute(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))
|
||||
@@ -278,7 +317,7 @@ describe("suspend and resume", () => {
|
||||
["job", "started"],
|
||||
])
|
||||
safe.reset()
|
||||
const result = Effect.runPromise(CodeMode.make({ tools }).resume(CodeMode.toBytes(suspended.state)).result)
|
||||
const result = Effect.runPromise(start(CodeMode.make({ tools }).resume(CodeMode.toBytes(suspended.state))).result)
|
||||
await safe.called()
|
||||
safe.calls[1]!.answer(30)
|
||||
jobs.finish("m:3", 40)
|
||||
@@ -296,13 +335,15 @@ describe("suspend and resume", () => {
|
||||
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(`
|
||||
const run = start(
|
||||
runtime.execute(`
|
||||
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`)
|
||||
return n`),
|
||||
)
|
||||
Effect.runFork(run.result)
|
||||
await first.called()
|
||||
const suspended = suspendedOf(await Effect.runPromise(run.suspend))
|
||||
@@ -310,34 +351,45 @@ describe("suspend and resume", () => {
|
||||
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,
|
||||
start(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 spin = start(
|
||||
busy.execute(`
|
||||
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"`)
|
||||
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,
|
||||
start(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 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 run = start(
|
||||
runtime.execute(`
|
||||
const inc = make()
|
||||
const before = inc(1)
|
||||
await tools.slow({ value: 0 })
|
||||
@@ -345,14 +397,17 @@ describe("suspend and resume", () => {
|
||||
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,
|
||||
start(
|
||||
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,
|
||||
@@ -367,9 +422,11 @@ describe("suspend and resume", () => {
|
||||
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,
|
||||
start(
|
||||
CodeMode.make({ tools: { job: jobs.tool } }).execute(`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)
|
||||
@@ -391,9 +448,15 @@ describe("suspend and resume", () => {
|
||||
"tool.after": (call) => Effect.sync(() => void keys.push(`after ${call.name} ${call.key}`)),
|
||||
},
|
||||
})
|
||||
const run = runtime.start(`const job = tools.job({ value: 1 }); const key = await tools.echo({}); return [key, (await job).value]`, undefined, {
|
||||
runId: "k",
|
||||
})
|
||||
const run = start(
|
||||
runtime.execute(
|
||||
`const job = tools.job({ value: 1 }); const key = await tools.echo({}); return [key, (await job).value]`,
|
||||
undefined,
|
||||
{
|
||||
runId: "k",
|
||||
},
|
||||
),
|
||||
)
|
||||
const fiber = Effect.runFork(run.result)
|
||||
await jobs.started()
|
||||
jobs.finish("k:0", 7)
|
||||
@@ -409,18 +472,26 @@ describe("suspend and resume", () => {
|
||||
Effect.sync(() => void events.push(`${call.key} ${result.status}`)),
|
||||
}
|
||||
const ended = await Effect.runPromise(
|
||||
CodeMode.make({ tools: { job: jobs.tool }, hooks }).start(`tools.job({ value: 1 }); return 1`, undefined, {
|
||||
runId: "a",
|
||||
}).result,
|
||||
start(
|
||||
CodeMode.make({ tools: { job: jobs.tool }, hooks }).execute(`tools.job({ value: 1 }); return 1`, undefined, {
|
||||
runId: "a",
|
||||
}),
|
||||
).result,
|
||||
)
|
||||
expect(ended).toMatchObject({ ok: true })
|
||||
expect(events).toEqual(["a:0 interrupted"])
|
||||
|
||||
events.length = 0
|
||||
jobs.reset()
|
||||
const run = CodeMode.make({ tools: { job: jobs.tool }, hooks }).start(`return (await tools.job({ value: 2 })).value`, undefined, {
|
||||
runId: "b",
|
||||
})
|
||||
const run = start(
|
||||
CodeMode.make({ tools: { job: jobs.tool }, hooks }).execute(
|
||||
`return (await tools.job({ value: 2 })).value`,
|
||||
undefined,
|
||||
{
|
||||
runId: "b",
|
||||
},
|
||||
),
|
||||
)
|
||||
Effect.runFork(run.result)
|
||||
await jobs.started()
|
||||
await Bun.sleep(5)
|
||||
@@ -438,28 +509,34 @@ describe("suspend and resume", () => {
|
||||
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`)
|
||||
const run = start(runtime.execute(`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(
|
||||
expect((await refusal(runtime.resume({ ...state, header: { ...header, build: "0.0.0+0" } }))).message).toMatch(
|
||||
/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")
|
||||
expect((await refusal(other.resume(state))).message).toMatch("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")
|
||||
expect(
|
||||
(
|
||||
await refusal(
|
||||
runtime.resume({ ...state, header: { ...header, sources: [{ ...source, code: `${source.code} ` }] } }),
|
||||
)
|
||||
).message,
|
||||
).toBe("Source main does not match its hash.")
|
||||
expect(await refusal(runtime.resume(new TextEncoder().encode("{")))).toBeInstanceOf(CodeMode.SnapshotError)
|
||||
expect((await refusal(runtime.resume({ ...state, records: [{ t: "Nope" }] }))).message).toMatch("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`,
|
||||
const run = start(
|
||||
CodeMode.make({ tools: { slow: first.tool }, suspend: { maxBytes: 100 } }).execute(
|
||||
`const big = "x".repeat(1000); await tools.slow({ value: 1 }); return big.length`,
|
||||
),
|
||||
)
|
||||
Effect.runFork(run.result)
|
||||
await first.called()
|
||||
@@ -512,14 +589,16 @@ describe("suspend and resume", () => {
|
||||
extra: [].extra,
|
||||
negative: Object.is(-0, -0 * 1),
|
||||
}`
|
||||
const run = CodeMode.make({ tools: { slow: first.tool } }).start(code)
|
||||
const run = start(CodeMode.make({ tools: { slow: first.tool } }).execute(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,
|
||||
start(
|
||||
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,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { CodeMode, Namespace, Tool } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const echo = (description: string, result: string) =>
|
||||
Tool.make({
|
||||
@@ -11,13 +12,13 @@ const echo = (description: string, result: string) =>
|
||||
})
|
||||
|
||||
const value = async (runtime: CodeMode.Runtime, code: string) => {
|
||||
const result = await Effect.runPromise(runtime.execute(code))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(code)))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
const failure = async (runtime: CodeMode.Runtime, code: string) => {
|
||||
const result = await Effect.runPromise(runtime.execute(code))
|
||||
const result = await Effect.runPromise(finished(runtime.execute(code)))
|
||||
if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`)
|
||||
return result.error
|
||||
}
|
||||
|
||||
@@ -22,9 +22,10 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -14,13 +14,14 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { CodeMode } from "../src/index.js"
|
||||
import { finished } from "./finished.js"
|
||||
|
||||
const base64Cases = (await Bun.file(new URL("./fixtures/wpt-base64.json", import.meta.url)).json()) as Array<
|
||||
[string, Array<number> | null]
|
||||
>
|
||||
|
||||
const value = async (code: string) => {
|
||||
const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
|
||||
const result = await Effect.runPromise(finished(CodeMode.execute({ code, tools: {} })))
|
||||
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
@@ -118,36 +118,32 @@ export const create = (
|
||||
yield* Effect.sleep(wait).pipe(Effect.andThen(lock.withPermit(publish)), Effect.forkIn(scope))
|
||||
}),
|
||||
)
|
||||
const result = yield* runtime(inventory)
|
||||
.execute(code)
|
||||
.pipe(
|
||||
Effect.provideService(Run, {
|
||||
executeTool: (name, tool, input) =>
|
||||
Effect.gen(function* () {
|
||||
const index = yield* Ref.getAndUpdate(callIndex, (index) => index + 1)
|
||||
// The program sees the same failure text the model gets for a direct call.
|
||||
const executed = yield* executeTool(name, tool, input, {
|
||||
...context,
|
||||
// Progress replaces the execute call's own, so an inner tool's updates would hide the call rows.
|
||||
progress: () => Effect.void,
|
||||
}).pipe(
|
||||
Effect.mapError((error) => new ToolFailure({ message: toSessionError(error).message, error })),
|
||||
)
|
||||
const content = resultContent(executed)
|
||||
const outputFileParts = outputFiles(content)
|
||||
if (outputFileParts.length > 0)
|
||||
yield* Ref.update(files, (items) => [...items, { index, files: outputFileParts }])
|
||||
return resultValue(executed)
|
||||
}),
|
||||
hooks: progressHooks(record),
|
||||
}),
|
||||
// A failed or interrupted call keeps its last progress, so send pending rows before it settles.
|
||||
Effect.onError(() =>
|
||||
lock.withPermit(Ref.get(trailing).pipe(Effect.flatMap((pending) => (pending ? publish : Effect.void)))),
|
||||
),
|
||||
Effect.provideService(CodeModeExecution.Service, context),
|
||||
Effect.provideService(Permission.Service, permission),
|
||||
)
|
||||
const result = yield* finish(runtime(inventory).execute(code)).pipe(
|
||||
Effect.provideService(Run, {
|
||||
executeTool: (name, tool, input) =>
|
||||
Effect.gen(function* () {
|
||||
const index = yield* Ref.getAndUpdate(callIndex, (index) => index + 1)
|
||||
// The program sees the same failure text the model gets for a direct call.
|
||||
const executed = yield* executeTool(name, tool, input, {
|
||||
...context,
|
||||
// Progress replaces the execute call's own, so an inner tool's updates would hide the call rows.
|
||||
progress: () => Effect.void,
|
||||
}).pipe(Effect.mapError((error) => new ToolFailure({ message: toSessionError(error).message, error })))
|
||||
const content = resultContent(executed)
|
||||
const outputFileParts = outputFiles(content)
|
||||
if (outputFileParts.length > 0)
|
||||
yield* Ref.update(files, (items) => [...items, { index, files: outputFileParts }])
|
||||
return resultValue(executed)
|
||||
}),
|
||||
hooks: progressHooks(record),
|
||||
}),
|
||||
// A failed or interrupted call keeps its last progress, so send pending rows before it settles.
|
||||
Effect.onError(() =>
|
||||
lock.withPermit(Ref.get(trailing).pipe(Effect.flatMap((pending) => (pending ? publish : Effect.void)))),
|
||||
),
|
||||
Effect.provideService(CodeModeExecution.Service, context),
|
||||
Effect.provideService(Permission.Service, permission),
|
||||
)
|
||||
const toolCalls = yield* Ref.get(calls)
|
||||
const collected = (yield* Ref.get(files))
|
||||
.toSorted((left, right) => left.index - right.index)
|
||||
@@ -287,14 +283,26 @@ export const check = (
|
||||
limits: CodeMode.ExecutionLimits,
|
||||
executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<Result, Error>,
|
||||
) =>
|
||||
CodeMode.make({ tools: inventoryTools(inventory), hooks: runHooks, limits })
|
||||
.execute(code, bindings)
|
||||
.pipe(
|
||||
Effect.provideService(Run, {
|
||||
executeTool: (name, tool, input) => executeTool(name, tool, input).pipe(Effect.map(resultValue)),
|
||||
hooks: {},
|
||||
}),
|
||||
)
|
||||
finish(CodeMode.make({ tools: inventoryTools(inventory), hooks: runHooks, limits }).execute(code, bindings)).pipe(
|
||||
Effect.provideService(Run, {
|
||||
executeTool: (name, tool, input) => executeTool(name, tool, input).pipe(Effect.map(resultValue)),
|
||||
hooks: {},
|
||||
}),
|
||||
)
|
||||
|
||||
// Runs a program to its end. No core tool suspends a run yet, so a suspended one is an unsupported failure.
|
||||
function finish<R>(started: Effect.Effect<CodeMode.Execution, never, R>) {
|
||||
return Effect.scoped(Effect.flatMap(started, (execution) => execution.result)).pipe(
|
||||
Effect.map((outcome): CodeMode.Result => {
|
||||
if (outcome.ok !== "suspended") return outcome
|
||||
return {
|
||||
ok: false,
|
||||
error: { kind: "ExecutionFailure", message: "The program suspended, which isn't supported here." },
|
||||
toolCalls: outcome.state.header.toolCalls,
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
function inventoryTools(inventory: Inventory) {
|
||||
// A path may carry namespace metadata, a callable tool, child tools, or all three.
|
||||
|
||||
Reference in New Issue
Block a user