@opencode/codemode 0.0.0-dev-919 → 0.0.0-dev-927
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -2
- package/dist/codemode.d.ts +54 -5
- package/dist/codemode.js +60 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/suspend-error.d.ts +23 -0
- package/dist/suspend-error.js +25 -0
- package/dist/vm/index.d.ts +4 -1
- package/dist/vm/index.js +4 -1
- package/dist/vm/runtime/driver.d.ts +22 -5
- package/dist/vm/runtime/driver.js +46 -4
- package/dist/vm/runtime/execute.d.ts +59 -1
- package/dist/vm/runtime/execute.js +180 -44
- package/dist/vm/snapshot/codec.d.ts +2 -3
- package/dist/vm/snapshot/codec.js +2 -3
- package/dist/vm/snapshot/snapshot.d.ts +17 -7
- package/dist/vm/snapshot/snapshot.js +14 -13
- package/dist/vm/snapshot.d.ts +4 -1
- package/dist/vm/snapshot.js +2 -0
- package/dist/vm/vm.d.ts +3 -2
- package/dist/vm/vm.js +13 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -97,8 +97,59 @@ denies it: the host function is never called, and the program catches the failur
|
|
|
97
97
|
fails the way the call itself would: a tool call or asynchronous global rejects its promise, and a synchronous global
|
|
98
98
|
throws.
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
### Bytecode VM and suspending/resuming a run (experimental, `engine: "vm"`)
|
|
101
|
+
|
|
102
|
+
Pass `engine: "vm"` to `CodeMode.make` or `CodeMode.execute` to run programs on the bytecode VM instead of the default
|
|
103
|
+
`"legacy"` AST interpreter. `runtime.execute(code, bindings?)` keeps the same `Effect<CodeMode.Result>` signature on
|
|
104
|
+
both engines.
|
|
105
|
+
|
|
106
|
+
On `engine: "vm"`, `CodeMode.make` returns a `CodeMode.VmRuntime`, which can also run a program step by step:
|
|
107
|
+
`runtime.program(code, bindings?)` makes a `CodeMode.Program`, and nothing runs until `program.run()`. `run()` runs it in
|
|
108
|
+
the current fiber until it ends or is suspended, and gives a `CodeMode.Outcome`: a `Result`, or:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
type Suspended = {
|
|
112
|
+
ok: "suspended"
|
|
113
|
+
state: CodeMode.Snapshot // JSON; CodeMode.toBytes / CodeMode.fromBytes convert it
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const runtime = CodeMode.make({ tools, engine: "vm" })
|
|
117
|
+
|
|
118
|
+
// inline
|
|
119
|
+
const outcome = yield* runtime.program(code).run()
|
|
120
|
+
|
|
121
|
+
// in the background, suspended from another fiber
|
|
122
|
+
const program = runtime.program(code)
|
|
123
|
+
const fiber = yield* Effect.forkChild(program.run())
|
|
124
|
+
const suspended = yield* program.suspend() // the same Suspended that run() returns
|
|
125
|
+
const outcome = yield* Fiber.join(fiber)
|
|
126
|
+
|
|
127
|
+
// later, in this process or another one built from the same version
|
|
128
|
+
yield* runtime.resume(CodeMode.toBytes(suspended.state)).run()
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The default runtime has no `program` or `resume`. A program runs once: running it again is a defect, so make another
|
|
132
|
+
with `runtime.program` or `runtime.resume`. `run()` owns the run's host calls: interrupting it interrupts the program and
|
|
133
|
+
every call in progress.
|
|
134
|
+
|
|
135
|
+
`program.suspend(options?)` saves the run's state and stops it at once, and `run()` returns the same `Suspended`. It
|
|
136
|
+
can be suspended only while no host call (tool, `search`, `fetch`, or extension call) is running. When one is in
|
|
137
|
+
progress, `suspend` fails with a `CodeMode.SuspendError` naming each blocking call by its path (`tools.x.y`, `fetch`),
|
|
138
|
+
and the run goes on untouched. Tool calls queued but not started don't block: they start on resume. Outcomes already
|
|
139
|
+
waiting in the inbox are part of the state. A suspend after the run ended gives its result; one issued before `run()`
|
|
140
|
+
has started waits for it to start (and for ever if it never runs).
|
|
141
|
+
|
|
142
|
+
The suspend is immediate: the program shares the thread with the caller, so it is at a step boundary. Its checks, saving
|
|
143
|
+
the state, and checking it against `options.maxBytes` all happen before anything about the run changes, so a state that
|
|
144
|
+
can't be saved or is too large fails the suspend the same way.
|
|
145
|
+
|
|
146
|
+
A resumed run keeps the limits it was saved with: `timeoutMs` counts active time across every resume, and `maxToolCalls`
|
|
147
|
+
counts every call of the run. A state resumes only on the same build of this package and with the same extensions. The
|
|
148
|
+
state is checked when the resumed program runs: its `run()` fails with a `CodeMode.SnapshotError` when it can't be
|
|
149
|
+
resumed here or is malformed. Built-ins are saved by name and only their changes are copied.
|
|
150
|
+
|
|
151
|
+
Set `CODEMODE_ROUNDTRIP=1` (`bun run test:roundtrip`) to run the tests on the VM with every run saved to bytes and
|
|
152
|
+
restored into a brand-new runtime at every host call and every few steps.
|
|
102
153
|
|
|
103
154
|
### `Extension.make`
|
|
104
155
|
|
package/dist/codemode.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
import type { Extension } from "./extension.js";
|
|
3
3
|
import { scanTools } from "./scan.js";
|
|
4
|
+
import { fromBytes, SnapshotError, SuspendError, toBytes } from "./suspend-error.js";
|
|
4
5
|
import { type Services, type ToolDescription, ToolRuntime } from "./tool-runtime.js";
|
|
5
6
|
import type { Tools } from "./tools.js";
|
|
6
|
-
|
|
7
|
+
import type { VmEngine } from "./vm/index.js";
|
|
8
|
+
export { fromBytes, scanTools, SnapshotError, SuspendError, toBytes };
|
|
9
|
+
export type SnapshotHeader = VmEngine.SnapshotHeader;
|
|
10
|
+
export type Snapshot = VmEngine.Snapshot;
|
|
7
11
|
/** A tool call admitted during an execution. */
|
|
8
12
|
export type { CallResult, ExtensionInvocation, Hooks, ToolCall, ToolDescription, ToolInvocation, } from "./tool-runtime.js";
|
|
9
13
|
/** Signature-construction helpers for host-owned catalog instructions. */
|
|
@@ -28,6 +32,13 @@ export type ResolvedExecutionLimits = {
|
|
|
28
32
|
readonly maxToolCalls: number | undefined;
|
|
29
33
|
readonly maxOutputBytes: number | undefined;
|
|
30
34
|
};
|
|
35
|
+
/** Execution engine: the default AST generator interpreter (`"legacy"`) or the experimental bytecode VM (`"vm"`). */
|
|
36
|
+
export type Engine = "legacy" | "vm";
|
|
37
|
+
/** How `program.suspend` saves a run's state (`engine: "vm"` only). */
|
|
38
|
+
export type SuspendOptions = {
|
|
39
|
+
/** The most bytes a saved state may take; a larger one fails the suspend, and the run goes on. */
|
|
40
|
+
readonly maxBytes?: number;
|
|
41
|
+
};
|
|
31
42
|
/** Configuration shared by `CodeMode.make` and `CodeMode.execute`. */
|
|
32
43
|
export type Options<Provided extends Record<string, unknown> = {}, Extensions extends ReadonlyArray<Extension> = []> = {
|
|
33
44
|
/** Explicit tools exposed to the program as `tools`. */
|
|
@@ -36,10 +47,13 @@ export type Options<Provided extends Record<string, unknown> = {}, Extensions ex
|
|
|
36
47
|
hooks?: ToolRuntime.Hooks<Environment<Provided, Extensions>>;
|
|
37
48
|
/** Host functions and objects exposed as globals, also importable by Node module name; see `Extension.make`. */
|
|
38
49
|
extensions?: Extensions;
|
|
39
|
-
/** Resource limits enforced on each execution. */
|
|
50
|
+
/** Resource limits enforced on each execution. Time counts active time only, across every resume of a run. */
|
|
40
51
|
limits?: ExecutionLimits;
|
|
41
|
-
/**
|
|
42
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Interpreter that runs programs. `"vm"` is the experimental bytecode VM, whose runtime (`VmRuntime`) can also
|
|
54
|
+
* suspend and resume runs; the default is `"legacy"`.
|
|
55
|
+
*/
|
|
56
|
+
engine?: Engine;
|
|
43
57
|
};
|
|
44
58
|
/** The Effect environment an execution needs: what the tools and the extensions' Effect globals require. */
|
|
45
59
|
export type Environment<Provided extends Record<string, unknown>, Extensions extends ReadonlyArray<Extension>> = Services<Provided> | Extension.Services<Extensions[number]>;
|
|
@@ -147,12 +161,47 @@ export declare const Result: Schema.Union<readonly [Schema.Struct<{
|
|
|
147
161
|
}>]>;
|
|
148
162
|
/** Result of executing a CodeMode program. Program failures are data, not Effect failures. */
|
|
149
163
|
export type Result = typeof Result.Type;
|
|
164
|
+
/** A run stopped by `program.suspend`: its whole state, to resume with `VmRuntime.resume`. */
|
|
165
|
+
export type Suspended = {
|
|
166
|
+
readonly ok: "suspended";
|
|
167
|
+
readonly state: Snapshot;
|
|
168
|
+
};
|
|
169
|
+
/** How a run of a program ended: its result, or its saved state once suspended. */
|
|
170
|
+
export type Outcome = Result | Suspended;
|
|
150
171
|
/** Reusable confined runtime over explicit tools. */
|
|
151
172
|
export type Runtime<R = never> = {
|
|
152
173
|
readonly catalog: ReadonlyArray<ToolDescription>;
|
|
153
174
|
readonly execute: (code: string, bindings?: Readonly<Record<string, DataValue>>) => Effect.Effect<Result, never, R>;
|
|
154
175
|
};
|
|
176
|
+
/** A runtime on the bytecode VM (`engine: "vm"`, experimental): it can also suspend runs and resume them. */
|
|
177
|
+
export type VmRuntime<R = never> = Runtime<R> & {
|
|
178
|
+
/** A program ready to run once; nothing runs until `program.run()`. */
|
|
179
|
+
readonly program: (code: string, bindings?: Readonly<Record<string, DataValue>>) => Program<R>;
|
|
180
|
+
/**
|
|
181
|
+
* A suspended run (its `state`, or that state as bytes) as a program to continue in this runtime, which must be the
|
|
182
|
+
* same build with the same extensions. The state is checked when it runs: `run()` fails with a `SnapshotError` when
|
|
183
|
+
* it can't be resumed here.
|
|
184
|
+
*/
|
|
185
|
+
readonly resume: (state: Snapshot | Uint8Array) => Program<R, SnapshotError>;
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* One run of a program on the VM. `run()` runs it in the current fiber until it ends or is suspended, and owns its host
|
|
189
|
+
* calls: interrupting `run()` interrupts them. A program runs once: running it again is a defect.
|
|
190
|
+
*/
|
|
191
|
+
export type Program<R = never, E = never> = {
|
|
192
|
+
readonly run: () => Effect.Effect<Outcome, E, R>;
|
|
193
|
+
/**
|
|
194
|
+
* Saves the run's state and stops it, at once, from another fiber: `run()` then returns the same `Suspended`, and so
|
|
195
|
+
* does this. It fails with a `SuspendError`, leaving the run running, while a host call (tool, `search`, `fetch`, or
|
|
196
|
+
* extension call) is in progress, or when the state can't be saved or is over `options.maxBytes`. A run that already
|
|
197
|
+
* ended gives its result. Before `run()` starts, this waits for it to start.
|
|
198
|
+
*/
|
|
199
|
+
readonly suspend: (options?: SuspendOptions) => Effect.Effect<Outcome, SuspendError>;
|
|
200
|
+
};
|
|
155
201
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
156
202
|
export declare const execute: <const Provided extends Record<string, unknown>, const Extensions extends ReadonlyArray<Extension> = []>(options: ExecuteOptions<Provided, Extensions>) => Effect.Effect<Result, never, Environment<Provided, Extensions>>;
|
|
157
203
|
/** Creates an Effect-native runtime over explicit, schema-described tools and extensions. */
|
|
158
|
-
export declare
|
|
204
|
+
export declare function make<const Provided extends Record<string, unknown> = {}, const Extensions extends ReadonlyArray<Extension> = []>(options: Options<Provided, Extensions> & {
|
|
205
|
+
engine: "vm";
|
|
206
|
+
}): VmRuntime<Environment<Provided, Extensions>>;
|
|
207
|
+
export declare function make<const Provided extends Record<string, unknown> = {}, const Extensions extends ReadonlyArray<Extension> = []>(options?: Options<Provided, Extensions>): Runtime<Environment<Provided, Extensions>>;
|
package/dist/codemode.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
1
|
+
import { Deferred, Effect, Exit, Schema } from "effect";
|
|
2
2
|
import { fromJson } from "./data.js";
|
|
3
3
|
import { executeProgram } from "./interpreter/execute.js";
|
|
4
4
|
import { extensionGlobals } from "./interpreter/extensions.js";
|
|
5
5
|
import { globalNames } from "./interpreter/globals.js";
|
|
6
6
|
import { scanTools } from "./scan.js";
|
|
7
|
+
import { fromBytes, SnapshotError, SuspendError, toBytes } from "./suspend-error.js";
|
|
7
8
|
import { ToolRuntime } from "./tool-runtime.js";
|
|
8
|
-
export { scanTools };
|
|
9
|
+
export { fromBytes, scanTools, SnapshotError, SuspendError, toBytes };
|
|
9
10
|
/** Signature-construction helpers for host-owned catalog instructions. */
|
|
10
11
|
export { searchSignature, toolExpression } from "./tool-runtime.js";
|
|
11
12
|
/** Schema for a host tool input containing CodeMode source. */
|
|
@@ -60,8 +61,7 @@ const resolveExecutionLimits = (limits) => ({
|
|
|
60
61
|
});
|
|
61
62
|
/** Executes one Effect-native CodeMode program without constructing a reusable runtime. */
|
|
62
63
|
export const execute = (options) => make(options).execute(options.code, options.bindings);
|
|
63
|
-
|
|
64
|
-
export const make = (options = {}) => {
|
|
64
|
+
export function make(options = {}) {
|
|
65
65
|
const prepared = ToolRuntime.prepare((options.tools ?? {}));
|
|
66
66
|
const limits = resolveExecutionLimits(options.limits);
|
|
67
67
|
const extensions = options.extensions ?? [];
|
|
@@ -74,18 +74,67 @@ export const make = (options = {}) => {
|
|
|
74
74
|
bound.add(name);
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
+
if (options.engine !== "vm") {
|
|
78
|
+
return {
|
|
79
|
+
get catalog() {
|
|
80
|
+
return prepared.catalog;
|
|
81
|
+
},
|
|
82
|
+
execute: (code, bindings) => executeProgram(code, prepared, limits, options.hooks ?? {}, (ctx) => [
|
|
83
|
+
...Object.entries(bindings ?? {}).map(([name, value]) => [name, fromJson(ctx, value)]),
|
|
84
|
+
...extensionGlobals(ctx, extensions),
|
|
85
|
+
]),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
let setup;
|
|
89
|
+
const getSetup = (vm) => (setup ??= {
|
|
90
|
+
prepared,
|
|
91
|
+
limits,
|
|
92
|
+
hooks: options.hooks ?? {},
|
|
93
|
+
globals: extensions.length === 0 ? undefined : (ctx) => vm.extensionGlobals(ctx, extensions),
|
|
94
|
+
lossy: extensions.length > 0,
|
|
95
|
+
});
|
|
96
|
+
const data = (vm, bindings) => bindings === undefined
|
|
97
|
+
? undefined
|
|
98
|
+
: (ctx) => Object.entries(bindings).map(([name, value]) => [name, vm.fromJson(ctx, value)]);
|
|
99
|
+
const program = (code, bindings) => vmProgram((vm) => Effect.succeed(vm.startProgram(getSetup(vm), code, data(vm, bindings))));
|
|
77
100
|
return {
|
|
78
101
|
get catalog() {
|
|
79
102
|
return prepared.catalog;
|
|
80
103
|
},
|
|
81
|
-
execute: (code, bindings) =>
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
104
|
+
execute: (code, bindings) => Effect.flatMap(loadVm(), (vm) => vm.finishExecution(program(code, bindings).run())),
|
|
105
|
+
program,
|
|
106
|
+
resume: (state) => vmProgram((vm) => vm.resumeProgram(getSetup(vm), state)),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// A program's one run: `start` makes it once the VM is loaded, and a suspend waits for it.
|
|
110
|
+
const vmProgram = (start) => {
|
|
111
|
+
let claimed = false;
|
|
112
|
+
const started = Deferred.makeUnsafe();
|
|
113
|
+
return {
|
|
114
|
+
run: () => Effect.suspend(() => {
|
|
115
|
+
if (claimed) {
|
|
116
|
+
return Effect.die(new Error("A CodeMode program runs once; make another with runtime.program or runtime.resume."));
|
|
117
|
+
}
|
|
118
|
+
claimed = true;
|
|
119
|
+
return loadVm().pipe(Effect.flatMap(start), Effect.onExit((exit) => Exit.isSuccess(exit)
|
|
120
|
+
? Deferred.succeed(started, exit.value)
|
|
121
|
+
: Deferred.fail(started, new SuspendError({ message: "The program didn't start.", running: [] }))), Effect.flatMap((running) => running.run()));
|
|
122
|
+
}),
|
|
123
|
+
suspend: (options) => {
|
|
124
|
+
const maxBytes = options?.maxBytes;
|
|
125
|
+
if (maxBytes !== undefined && (!Number.isSafeInteger(maxBytes) || maxBytes < 0)) {
|
|
126
|
+
throw new RangeError("maxBytes must be a safe integer greater than or equal to 0.");
|
|
127
|
+
}
|
|
128
|
+
return Effect.flatMap(Deferred.await(started), (running) => running.suspend(maxBytes));
|
|
129
|
+
},
|
|
87
130
|
};
|
|
88
131
|
};
|
|
89
132
|
// The VM is loaded on first use so the default engine pulls in none of its code.
|
|
133
|
+
let vmLoaded;
|
|
90
134
|
let vmEngine;
|
|
91
|
-
const loadVm = () =>
|
|
135
|
+
const loadVm = () => vmLoaded !== undefined
|
|
136
|
+
? Effect.succeed(vmLoaded)
|
|
137
|
+
: Effect.promise(() => (vmEngine ??= import("./vm/index.js").then((mod) => {
|
|
138
|
+
vmLoaded = mod;
|
|
139
|
+
return mod;
|
|
140
|
+
})));
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ export * as Extension from "./extension.js";
|
|
|
3
3
|
export * as Namespace from "./namespace.js";
|
|
4
4
|
export * as Tool from "./tool.js";
|
|
5
5
|
export * as OpenAPI from "./openapi/index.js";
|
|
6
|
-
export { searchSignature, toolExpression } from "./codemode.js";
|
|
6
|
+
export { fromBytes, searchSignature, SnapshotError, SuspendError, toBytes, toolExpression, type Outcome, type Program, type Snapshot, type SnapshotHeader, type Suspended, type SuspendOptions, type VmRuntime, } from "./codemode.js";
|
|
7
7
|
export { ToolError, toolError } from "./tool-error.js";
|
package/dist/index.js
CHANGED
|
@@ -3,5 +3,5 @@ export * as Extension from "./extension.js";
|
|
|
3
3
|
export * as Namespace from "./namespace.js";
|
|
4
4
|
export * as Tool from "./tool.js";
|
|
5
5
|
export * as OpenAPI from "./openapi/index.js";
|
|
6
|
-
export { searchSignature, toolExpression } from "./codemode.js";
|
|
6
|
+
export { fromBytes, searchSignature, SnapshotError, SuspendError, toBytes, toolExpression, } from "./codemode.js";
|
|
7
7
|
export { ToolError, toolError } from "./tool-error.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { Snapshot } from "./vm/snapshot/snapshot.js";
|
|
3
|
+
declare const SuspendError_base: Schema.Class<SuspendError, Schema.TaggedStruct<"SuspendError", {
|
|
4
|
+
readonly message: Schema.String;
|
|
5
|
+
readonly running: Schema.$Array<Schema.Struct<{
|
|
6
|
+
readonly path: Schema.String;
|
|
7
|
+
}>>;
|
|
8
|
+
}>, import("effect/Cause").YieldableError>;
|
|
9
|
+
/**
|
|
10
|
+
* A suspend that failed, leaving the run running: `running` names each host call that blocked it by its path
|
|
11
|
+
* (`tools.x.y`, `search`, or an extension function's path such as `fetch`). Empty when the state couldn't be
|
|
12
|
+
* saved or was too large.
|
|
13
|
+
*/
|
|
14
|
+
export declare class SuspendError extends SuspendError_base {
|
|
15
|
+
}
|
|
16
|
+
export declare class SnapshotError extends Error {
|
|
17
|
+
readonly name = "SnapshotError";
|
|
18
|
+
}
|
|
19
|
+
/** A snapshot as bytes: UTF-8 JSON. */
|
|
20
|
+
export declare const toBytes: (snapshot: Snapshot) => Uint8Array;
|
|
21
|
+
/** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
|
|
22
|
+
export declare const fromBytes: (bytes: Uint8Array) => unknown;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/**
|
|
3
|
+
* A suspend that failed, leaving the run running: `running` names each host call that blocked it by its path
|
|
4
|
+
* (`tools.x.y`, `search`, or an extension function's path such as `fetch`). Empty when the state couldn't be
|
|
5
|
+
* saved or was too large.
|
|
6
|
+
*/
|
|
7
|
+
export class SuspendError extends Schema.TaggedError()("SuspendError", {
|
|
8
|
+
message: Schema.String,
|
|
9
|
+
running: Schema.Array(Schema.Struct({ path: Schema.String })),
|
|
10
|
+
}) {
|
|
11
|
+
}
|
|
12
|
+
export class SnapshotError extends Error {
|
|
13
|
+
name = "SnapshotError";
|
|
14
|
+
}
|
|
15
|
+
/** A snapshot as bytes: UTF-8 JSON. */
|
|
16
|
+
export const toBytes = (snapshot) => new TextEncoder().encode(JSON.stringify(snapshot));
|
|
17
|
+
/** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
|
|
18
|
+
export const fromBytes = (bytes) => {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
throw new SnapshotError(`Invalid snapshot: the bytes are not UTF-8 JSON (${String(error)}).`);
|
|
24
|
+
}
|
|
25
|
+
};
|
package/dist/vm/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export * as VmEngine from "./index.js";
|
|
2
|
-
export {
|
|
2
|
+
export { fromJson } from "./data.js";
|
|
3
|
+
export { execute, finishExecution, resumeProgram, type Running, type Setup, startProgram } from "./runtime/execute.js";
|
|
4
|
+
export { extensionGlobals } from "./runtime/extensions.js";
|
|
3
5
|
export { globalNames } from "./runtime/globals.js";
|
|
6
|
+
export { fromBytes, type Snapshot, SnapshotError, type SnapshotHeader, toBytes } from "./snapshot/snapshot.js";
|
package/dist/vm/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export * as VmEngine from "./index.js";
|
|
2
|
-
export {
|
|
2
|
+
export { fromJson } from "./data.js";
|
|
3
|
+
export { execute, finishExecution, resumeProgram, startProgram } from "./runtime/execute.js";
|
|
4
|
+
export { extensionGlobals } from "./runtime/extensions.js";
|
|
3
5
|
export { globalNames } from "./runtime/globals.js";
|
|
6
|
+
export { fromBytes, SnapshotError, toBytes } from "./snapshot/snapshot.js";
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { type Context, Effect } from "effect";
|
|
2
|
-
import type {
|
|
2
|
+
import type { HostCall } from "../scheduler.js";
|
|
3
|
+
import { type ToolRuntime } from "../../tool-runtime.js";
|
|
3
4
|
import type { Vm } from "../vm.js";
|
|
4
5
|
import { type Completion } from "./signals.js";
|
|
6
|
+
/** What `Driver.run` returns once the run was suspended: its state is saved, and the VM stopped. */
|
|
7
|
+
export declare const SUSPENDED: unique symbol;
|
|
5
8
|
/**
|
|
6
9
|
* Runs the VM (see `Vm`) in Effect: the only place program execution meets the host. It calls `vm.run`
|
|
7
10
|
* until the program ends, and does what each stop asks: yields to the Effect scheduler for a slice, and starts the host
|
|
@@ -19,6 +22,7 @@ export declare class Driver<R> {
|
|
|
19
22
|
private readonly fibers;
|
|
20
23
|
private wake;
|
|
21
24
|
private stopping;
|
|
25
|
+
private suspending;
|
|
22
26
|
private interrupted;
|
|
23
27
|
/** Called at every slice and host stop, before queued calls start: the round-trip test mode swaps the VM. */
|
|
24
28
|
checkpoint: Effect.Effect<void, unknown, R> | undefined;
|
|
@@ -26,16 +30,29 @@ export declare class Driver<R> {
|
|
|
26
30
|
/** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */
|
|
27
31
|
services: Context.Context<R>);
|
|
28
32
|
/**
|
|
29
|
-
* Runs the VM until the program's body completes, then returns how it ended
|
|
30
|
-
*
|
|
31
|
-
* work that is interrupted, interrupts the execution.
|
|
33
|
+
* Runs the VM until the program's body completes, then returns how it ended, or `SUSPENDED` once the run was
|
|
34
|
+
* suspended (see `detach`). Host work it started may still be running; `interrupt` stops it. Host work interrupted
|
|
35
|
+
* on its own that reaches the program's body, or blocking host work that is interrupted, interrupts the execution.
|
|
32
36
|
*/
|
|
33
|
-
run(): Effect.Effect<Completion, unknown, R>;
|
|
37
|
+
run(): Effect.Effect<Completion | typeof SUSPENDED, unknown, R>;
|
|
38
|
+
/**
|
|
39
|
+
* The host calls that block a suspend now: every call in progress, and a queued or synchronous extension call the
|
|
40
|
+
* program is in the middle of, which holds live host values. A tool call queued but not started starts on resume.
|
|
41
|
+
*/
|
|
42
|
+
blocking(): Array<{
|
|
43
|
+
readonly path: string;
|
|
44
|
+
}>;
|
|
34
45
|
/** Whether the run holds live host values: an extension call in progress. */
|
|
35
46
|
get holdsHostValues(): boolean;
|
|
47
|
+
/** Stops the run to suspend it, once its state is saved: `run` returns `SUSPENDED` at its next step. */
|
|
48
|
+
detach(): void;
|
|
36
49
|
/** Stops host work still running, waiting for its cleanup. */
|
|
37
50
|
interrupt(): Effect.Effect<void>;
|
|
51
|
+
/** Starts any tool calls that were queued when the VM was saved. */
|
|
52
|
+
resume(): void;
|
|
38
53
|
private launch;
|
|
39
54
|
private fork;
|
|
40
55
|
private get settled();
|
|
41
56
|
}
|
|
57
|
+
/** A host call as the program wrote it: `tools.x.y`, `search`, or an extension function's path. */
|
|
58
|
+
export declare const callPath: (call: HostCall) => string;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { Cause, Effect, Exit, Fiber } from "effect";
|
|
2
|
+
import { toolExpression } from "../../tool-runtime.js";
|
|
2
3
|
import { locate } from "./errors.js";
|
|
3
4
|
import { PendingThrow } from "./model.js";
|
|
4
5
|
import { abrupt, Interruption, normal } from "./signals.js";
|
|
6
|
+
/** What `Driver.run` returns once the run was suspended: its state is saved, and the VM stopped. */
|
|
7
|
+
export const SUSPENDED = Symbol("codemode.suspended");
|
|
5
8
|
/**
|
|
6
9
|
* Runs the VM (see `Vm`) in Effect: the only place program execution meets the host. It calls `vm.run`
|
|
7
10
|
* until the program ends, and does what each stop asks: yields to the Effect scheduler for a slice, and starts the host
|
|
@@ -18,6 +21,7 @@ export class Driver {
|
|
|
18
21
|
fibers = new Map();
|
|
19
22
|
wake;
|
|
20
23
|
stopping = false;
|
|
24
|
+
suspending = false;
|
|
21
25
|
// Blocking host work interrupted on its own, which interrupts the execution.
|
|
22
26
|
interrupted;
|
|
23
27
|
/** Called at every slice and host stop, before queued calls start: the round-trip test mode swaps the VM. */
|
|
@@ -30,9 +34,9 @@ export class Driver {
|
|
|
30
34
|
this.services = services;
|
|
31
35
|
}
|
|
32
36
|
/**
|
|
33
|
-
* Runs the VM until the program's body completes, then returns how it ended
|
|
34
|
-
*
|
|
35
|
-
* work that is interrupted, interrupts the execution.
|
|
37
|
+
* Runs the VM until the program's body completes, then returns how it ended, or `SUSPENDED` once the run was
|
|
38
|
+
* suspended (see `detach`). Host work it started may still be running; `interrupt` stops it. Host work interrupted
|
|
39
|
+
* on its own that reaches the program's body, or blocking host work that is interrupted, interrupts the execution.
|
|
36
40
|
*/
|
|
37
41
|
run() {
|
|
38
42
|
const self = this;
|
|
@@ -40,6 +44,8 @@ export class Driver {
|
|
|
40
44
|
while (true) {
|
|
41
45
|
if (self.interrupted !== undefined)
|
|
42
46
|
return yield* Effect.failCause(self.interrupted);
|
|
47
|
+
if (self.suspending)
|
|
48
|
+
return SUSPENDED;
|
|
43
49
|
const stop = self.vm.run();
|
|
44
50
|
if (stop.kind === "slice") {
|
|
45
51
|
if (self.checkpoint !== undefined)
|
|
@@ -54,6 +60,9 @@ export class Driver {
|
|
|
54
60
|
}
|
|
55
61
|
if (self.checkpoint !== undefined)
|
|
56
62
|
yield* self.checkpoint;
|
|
63
|
+
// Suspended now: the calls stay queued, and start on resume.
|
|
64
|
+
if (self.suspending)
|
|
65
|
+
continue;
|
|
57
66
|
for (const handed of stop.calls)
|
|
58
67
|
self.launch(self.vm.call(handed.id) ?? handed);
|
|
59
68
|
continue;
|
|
@@ -68,10 +77,29 @@ export class Driver {
|
|
|
68
77
|
}
|
|
69
78
|
});
|
|
70
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* The host calls that block a suspend now: every call in progress, and a queued or synchronous extension call the
|
|
82
|
+
* program is in the middle of, which holds live host values. A tool call queued but not started starts on resume.
|
|
83
|
+
*/
|
|
84
|
+
blocking() {
|
|
85
|
+
const vm = this.vm;
|
|
86
|
+
const calls = vm.calls().filter((call) => call.status !== "queued" || call.request.kind === "extension");
|
|
87
|
+
const running = calls.map((call) => ({ path: callPath(call) }));
|
|
88
|
+
const active = vm.activeExtension;
|
|
89
|
+
if (active !== undefined && !calls.some((call) => call.blocking && call.request.kind === "extension")) {
|
|
90
|
+
running.push(active);
|
|
91
|
+
}
|
|
92
|
+
return running;
|
|
93
|
+
}
|
|
71
94
|
/** Whether the run holds live host values: an extension call in progress. */
|
|
72
95
|
get holdsHostValues() {
|
|
73
96
|
return this.vm.calls().some((call) => call.request.kind === "extension") || this.vm.activeExtension !== undefined;
|
|
74
97
|
}
|
|
98
|
+
/** Stops the run to suspend it, once its state is saved: `run` returns `SUSPENDED` at its next step. */
|
|
99
|
+
detach() {
|
|
100
|
+
this.suspending = true;
|
|
101
|
+
this.wake?.();
|
|
102
|
+
}
|
|
75
103
|
/** Stops host work still running, waiting for its cleanup. */
|
|
76
104
|
interrupt() {
|
|
77
105
|
const self = this;
|
|
@@ -81,6 +109,13 @@ export class Driver {
|
|
|
81
109
|
yield* Fiber.interruptAll([...self.fibers.values()]);
|
|
82
110
|
});
|
|
83
111
|
}
|
|
112
|
+
/** Starts any tool calls that were queued when the VM was saved. */
|
|
113
|
+
resume() {
|
|
114
|
+
for (const call of this.vm.calls()) {
|
|
115
|
+
if (call.status === "queued")
|
|
116
|
+
this.launch(call);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
84
119
|
// Starts host work now; its outcome goes back to the VM: a blocking call's continues the job that made it, a
|
|
85
120
|
// concurrent call's waits in the inbox and settles its promise in a later job, in completion order.
|
|
86
121
|
launch(call) {
|
|
@@ -128,7 +163,7 @@ export class Driver {
|
|
|
128
163
|
}
|
|
129
164
|
get settled() {
|
|
130
165
|
return Effect.callback((resume) => {
|
|
131
|
-
if (this.vm.ready || this.interrupted !== undefined)
|
|
166
|
+
if (this.vm.ready || this.suspending || this.interrupted !== undefined)
|
|
132
167
|
return resume(Effect.void);
|
|
133
168
|
this.wake = () => {
|
|
134
169
|
this.wake = undefined;
|
|
@@ -140,3 +175,10 @@ export class Driver {
|
|
|
140
175
|
});
|
|
141
176
|
}
|
|
142
177
|
}
|
|
178
|
+
/** A host call as the program wrote it: `tools.x.y`, `search`, or an extension function's path. */
|
|
179
|
+
export const callPath = (call) => {
|
|
180
|
+
const request = call.request;
|
|
181
|
+
if (request.kind === "tool")
|
|
182
|
+
return toolExpression(request.path.flatMap((segment) => segment.split(".")).join("."));
|
|
183
|
+
return request.kind === "search" ? "search" : request.call.label;
|
|
184
|
+
};
|
|
@@ -1,16 +1,74 @@
|
|
|
1
1
|
import { type Program } from "acorn";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
|
-
import type { DataValue, Diagnostic, ResolvedExecutionLimits, Result } from "../../codemode.js";
|
|
3
|
+
import type { DataValue, Diagnostic, Outcome, ResolvedExecutionLimits, Result } from "../../codemode.js";
|
|
4
4
|
import type { Extension } from "../../extension.js";
|
|
5
|
+
import { type Snapshot, SnapshotError } from "../snapshot/snapshot.js";
|
|
5
6
|
import { ToolRuntime } from "../../tool-runtime.js";
|
|
7
|
+
import { SuspendError } from "../../suspend-error.js";
|
|
6
8
|
import { Interpreter } from "./interpreter.js";
|
|
7
9
|
import type { Value } from "./objects.js";
|
|
8
10
|
import { Source } from "./source.js";
|
|
9
11
|
type Globals<R> = (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>;
|
|
12
|
+
/** How a runtime runs programs: its tools, limits, hooks, and host globals, shared by every run it starts. */
|
|
13
|
+
export type Setup<R> = {
|
|
14
|
+
readonly prepared: ToolRuntime.Prepared<R>;
|
|
15
|
+
readonly limits: ResolvedExecutionLimits;
|
|
16
|
+
readonly hooks: ToolRuntime.Hooks<R>;
|
|
17
|
+
/** The host's globals, made again for a restored run; see `Interpreter`. */
|
|
18
|
+
readonly globals?: Globals<R> | undefined;
|
|
19
|
+
/** Whether the host globals can hand the program values with no data form (extensions can). */
|
|
20
|
+
readonly lossy?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/** Turns a suspended outcome into a failure, for callers that only run programs to their end. */
|
|
23
|
+
export declare const finishExecution: <E, R>(outcome: Effect.Effect<Outcome, E, R>) => Effect.Effect<Result, E, R>;
|
|
10
24
|
/** Runs a program on the VM with `CodeMode.make`'s extensions and JSON bindings. */
|
|
11
25
|
export declare const execute: <R>(code: string, prepared: ToolRuntime.Prepared<R>, limits: ResolvedExecutionLimits, hooks: ToolRuntime.Hooks<R>, extensions: ReadonlyArray<Extension>, bindings: Readonly<Record<string, DataValue>> | undefined) => Effect.Effect<Result, never, R>;
|
|
12
26
|
export declare const executeProgram: <R>(code: string, prepared: ToolRuntime.Prepared<R>, limits: ResolvedExecutionLimits, hooks: ToolRuntime.Hooks<R>, globals?: Globals<R>, bindings?: Globals<R>, lossy?: boolean) => Effect.Effect<Result, never, R>;
|
|
27
|
+
/** A program ready to run on the VM: nothing runs until `run`. */
|
|
28
|
+
export declare const startProgram: <R>(setup: Setup<R>, code: string, bindings?: Globals<R>) => Running<R>;
|
|
29
|
+
/**
|
|
30
|
+
* Restores a saved run into this runtime, under the limits it was saved with, ready to run as `startProgram`'s.
|
|
31
|
+
* Fails with a `SnapshotError` when the state can't be read or belongs to another build, other host globals, or other
|
|
32
|
+
* sources.
|
|
33
|
+
*/
|
|
34
|
+
export declare const resumeProgram: <R>(setup: Setup<R>, saved: Snapshot | Uint8Array | unknown) => Effect.Effect<Running<R>, SnapshotError>;
|
|
13
35
|
export declare const setRoundTrip: (enabled: boolean) => void;
|
|
36
|
+
/** One run's state: the runtime it is in now (restoring swaps it), its driver, and how much active time it used. */
|
|
37
|
+
export declare class Running<R> {
|
|
38
|
+
private readonly setup;
|
|
39
|
+
/** Active milliseconds used before this run, by earlier ones of the same execution. */
|
|
40
|
+
private readonly activeBefore;
|
|
41
|
+
private readonly prepare;
|
|
42
|
+
private readonly resuming;
|
|
43
|
+
private ctx;
|
|
44
|
+
private driver;
|
|
45
|
+
private startedAt;
|
|
46
|
+
private finished;
|
|
47
|
+
private suspended;
|
|
48
|
+
private trips;
|
|
49
|
+
private skip;
|
|
50
|
+
private readonly started;
|
|
51
|
+
private readonly done;
|
|
52
|
+
constructor(setup: Setup<R>,
|
|
53
|
+
/** Active milliseconds used before this run, by earlier ones of the same execution. */
|
|
54
|
+
activeBefore: number, prepare: () => Interpreter<R> | Result, resuming?: boolean);
|
|
55
|
+
/** Makes the run's runtime and driver, then runs the program in the current fiber until it ends or is suspended. */
|
|
56
|
+
run(): Effect.Effect<Outcome, never, R>;
|
|
57
|
+
/**
|
|
58
|
+
* Saves the run's state and stops it, at once: `run` then returns the same `Suspended`. It fails with a
|
|
59
|
+
* `SuspendError`, leaving the run running, while a host call is in progress. A run that already ended gives its result.
|
|
60
|
+
* Before the run has started, it waits for it.
|
|
61
|
+
*/
|
|
62
|
+
suspend(maxBytes: number | undefined): Effect.Effect<Outcome, SuspendError>;
|
|
63
|
+
private execute;
|
|
64
|
+
private stop;
|
|
65
|
+
/**
|
|
66
|
+
* Saves the run's state, with no effect on the run: the calls that block a suspend, or the state as it is now. The
|
|
67
|
+
* program shares this thread with the caller, so it is at a step boundary.
|
|
68
|
+
*/
|
|
69
|
+
private save;
|
|
70
|
+
private trip;
|
|
71
|
+
}
|
|
14
72
|
export declare const parseProgram: (code: string) => {
|
|
15
73
|
ok: true;
|
|
16
74
|
program: Program;
|
|
@@ -1,55 +1,84 @@
|
|
|
1
1
|
import { parse } from "acorn";
|
|
2
|
-
import { Cause, Effect, Option, Schema } from "effect";
|
|
2
|
+
import { Cause, Deferred, Effect, Option, Schema } from "effect";
|
|
3
3
|
import { fromJson } from "../data.js";
|
|
4
|
-
import { fromBytes, SnapshotError, toBytes } from "../snapshot/snapshot.js";
|
|
4
|
+
import { fromBytes, header, SnapshotError, toBytes } from "../snapshot/snapshot.js";
|
|
5
5
|
import { ToolRuntime } from "../../tool-runtime.js";
|
|
6
|
+
import { SuspendError } from "../../suspend-error.js";
|
|
6
7
|
import { normalizeError } from "./errors.js";
|
|
7
8
|
import { extensionGlobals } from "./extensions.js";
|
|
8
9
|
import { createBuiltins } from "./intrinsics.js";
|
|
9
10
|
import { Interpreter } from "./interpreter.js";
|
|
10
11
|
import { formatLocation, sourceLocation } from "./model.js";
|
|
11
|
-
import { Driver } from "./driver.js";
|
|
12
|
+
import { Driver, SUSPENDED } from "./driver.js";
|
|
12
13
|
import { Interruption } from "./signals.js";
|
|
13
14
|
import { Source } from "./source.js";
|
|
15
|
+
/** Turns a suspended outcome into a failure, for callers that only run programs to their end. */
|
|
16
|
+
export const finishExecution = (outcome) => Effect.map(outcome, (outcome) => {
|
|
17
|
+
if (outcome.ok !== "suspended")
|
|
18
|
+
return outcome;
|
|
19
|
+
return {
|
|
20
|
+
ok: false,
|
|
21
|
+
error: { kind: "ExecutionFailure", message: "The program suspended, which isn't supported here." },
|
|
22
|
+
toolCalls: outcome.state.header.toolCalls,
|
|
23
|
+
};
|
|
24
|
+
});
|
|
14
25
|
/** Runs a program on the VM with `CodeMode.make`'s extensions and JSON bindings. */
|
|
15
26
|
export const execute = (code, prepared, limits, hooks, extensions, bindings) => executeProgram(code, prepared, limits, hooks, extensions.length === 0 ? undefined : (ctx) => extensionGlobals(ctx, extensions), bindings === undefined
|
|
16
27
|
? undefined
|
|
17
28
|
: (ctx) => Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)]), extensions.length > 0);
|
|
18
|
-
export const executeProgram = (code, prepared, limits, hooks, globals, bindings, lossy = false) =>
|
|
29
|
+
export const executeProgram = (code, prepared, limits, hooks, globals, bindings, lossy = false) => finishExecution(startProgram({ prepared, limits, hooks, globals, lossy }, code, bindings).run());
|
|
30
|
+
/** A program ready to run on the VM: nothing runs until `run`. */
|
|
31
|
+
export const startProgram = (setup, code, bindings) => new Running(setup, 0, () => {
|
|
19
32
|
if (code.trim().length === 0) {
|
|
20
|
-
return
|
|
21
|
-
ok: false,
|
|
22
|
-
error: { kind: "ParseError", message: "Code cannot be empty." },
|
|
23
|
-
toolCalls: [],
|
|
24
|
-
});
|
|
33
|
+
return { ok: false, error: { kind: "ParseError", message: "Code cannot be empty." }, toolCalls: [] };
|
|
25
34
|
}
|
|
26
35
|
const parsed = parseProgram(code);
|
|
27
36
|
if (!parsed.ok)
|
|
28
|
-
return
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
37
|
+
return { ok: false, error: parsed.error, toolCalls: [] };
|
|
38
|
+
const runtime = instantiate(setup, bindings);
|
|
39
|
+
runtime.vm.start(parsed.source);
|
|
40
|
+
return runtime;
|
|
41
|
+
});
|
|
42
|
+
/**
|
|
43
|
+
* Restores a saved run into this runtime, under the limits it was saved with, ready to run as `startProgram`'s.
|
|
44
|
+
* Fails with a `SnapshotError` when the state can't be read or belongs to another build, other host globals, or other
|
|
45
|
+
* sources.
|
|
46
|
+
*/
|
|
47
|
+
export const resumeProgram = (setup, saved) => Effect.try({
|
|
48
|
+
try: () => {
|
|
49
|
+
const state = saved instanceof Uint8Array ? fromBytes(saved) : saved;
|
|
50
|
+
const limits = header(state).limits;
|
|
51
|
+
const resumed = {
|
|
52
|
+
...setup,
|
|
53
|
+
limits: {
|
|
54
|
+
timeoutMs: limits.timeoutMs ?? undefined,
|
|
55
|
+
maxToolCalls: limits.maxToolCalls ?? undefined,
|
|
56
|
+
maxOutputBytes: limits.maxOutputBytes ?? undefined,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
const fresh = instantiate(resumed);
|
|
60
|
+
const snapshot = restoreInto(state, fresh.vm);
|
|
61
|
+
return new Running(resumed, snapshot.header.budget.activeMs, () => fresh, true);
|
|
62
|
+
},
|
|
63
|
+
catch: (error) => (error instanceof SnapshotError ? error : new SnapshotError(String(error))),
|
|
33
64
|
});
|
|
34
65
|
// Sources restored recently, by text: the AST is never changed, so runs restored from the same program share it.
|
|
35
66
|
const restoredSources = new Map();
|
|
36
|
-
const restoreInto = (saved, vm) => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
};
|
|
67
|
+
const restoreInto = (saved, vm) => vm.restore(saved, {
|
|
68
|
+
parse: (code) => {
|
|
69
|
+
const known = restoredSources.get(code);
|
|
70
|
+
if (known !== undefined)
|
|
71
|
+
return known;
|
|
72
|
+
const parsed = parseProgram(code);
|
|
73
|
+
if (!parsed.ok)
|
|
74
|
+
throw new SnapshotError(`A saved source no longer parses: ${parsed.error.message}`);
|
|
75
|
+
if (restoredSources.size >= 16)
|
|
76
|
+
restoredSources.delete(restoredSources.keys().next().value);
|
|
77
|
+
restoredSources.set(code, parsed.source);
|
|
78
|
+
return parsed.source;
|
|
79
|
+
},
|
|
80
|
+
interruption: () => new Interruption(Cause.interrupt()),
|
|
81
|
+
});
|
|
53
82
|
// A new runtime: intrinsics, the host globals, the program's bindings, a fresh tool bridge, and its VM.
|
|
54
83
|
const instantiate = (setup, bindings) => {
|
|
55
84
|
const ctx = new Interpreter({
|
|
@@ -74,27 +103,66 @@ export const setRoundTrip = (enabled) => {
|
|
|
74
103
|
};
|
|
75
104
|
const ROUND_TRIP_TICKS = 2;
|
|
76
105
|
const ROUND_TRIP_MAX = 200;
|
|
77
|
-
/** One run: the runtime it is in now (
|
|
78
|
-
class Running {
|
|
106
|
+
/** One run's state: the runtime it is in now (restoring swaps it), its driver, and how much active time it used. */
|
|
107
|
+
export class Running {
|
|
79
108
|
setup;
|
|
109
|
+
activeBefore;
|
|
110
|
+
prepare;
|
|
111
|
+
resuming;
|
|
80
112
|
ctx;
|
|
113
|
+
driver;
|
|
114
|
+
startedAt = 0;
|
|
115
|
+
// Set once the program's body ends, and once a suspend saved the run: either way, `done` gets the outcome next.
|
|
116
|
+
finished = false;
|
|
117
|
+
suspended;
|
|
81
118
|
trips = 0;
|
|
82
119
|
// Checkpoints to let pass before the next round trip, so a large state is not saved at every one.
|
|
83
120
|
skip = 0;
|
|
84
|
-
|
|
121
|
+
started = Deferred.makeUnsafe();
|
|
122
|
+
done = Deferred.makeUnsafe();
|
|
123
|
+
constructor(setup,
|
|
124
|
+
/** Active milliseconds used before this run, by earlier ones of the same execution. */
|
|
125
|
+
activeBefore, prepare, resuming = false) {
|
|
85
126
|
this.setup = setup;
|
|
86
|
-
this.
|
|
127
|
+
this.activeBefore = activeBefore;
|
|
128
|
+
this.prepare = prepare;
|
|
129
|
+
this.resuming = resuming;
|
|
87
130
|
}
|
|
131
|
+
/** Makes the run's runtime and driver, then runs the program in the current fiber until it ends or is suspended. */
|
|
88
132
|
run() {
|
|
89
133
|
const self = this;
|
|
90
134
|
return Effect.gen(function* () {
|
|
91
|
-
const
|
|
135
|
+
const prepared = self.prepare();
|
|
136
|
+
if (!(prepared instanceof Interpreter)) {
|
|
137
|
+
self.finished = true;
|
|
138
|
+
return prepared;
|
|
139
|
+
}
|
|
140
|
+
self.ctx = prepared;
|
|
141
|
+
self.driver = new Driver(prepared.vm, prepared.tools, yield* Effect.context());
|
|
92
142
|
if (roundTrip)
|
|
93
|
-
driver.checkpoint = Effect.sync(() => self.trip(
|
|
94
|
-
|
|
95
|
-
|
|
143
|
+
self.driver.checkpoint = Effect.sync(() => self.trip());
|
|
144
|
+
self.startedAt = Date.now();
|
|
145
|
+
yield* Deferred.succeed(self.started, undefined);
|
|
146
|
+
return yield* self.execute();
|
|
147
|
+
}).pipe(Effect.onExit((exit) => Effect.andThen(Deferred.succeed(self.started, undefined), Deferred.done(self.done, exit))));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Saves the run's state and stops it, at once: `run` then returns the same `Suspended`. It fails with a
|
|
151
|
+
* `SuspendError`, leaving the run running, while a host call is in progress. A run that already ended gives its result.
|
|
152
|
+
* Before the run has started, it waits for it.
|
|
153
|
+
*/
|
|
154
|
+
suspend(maxBytes) {
|
|
155
|
+
return Effect.andThen(Deferred.await(this.started), Effect.suspend(() => {
|
|
156
|
+
if (this.finished)
|
|
157
|
+
return Deferred.await(this.done);
|
|
158
|
+
const saved = this.save(maxBytes);
|
|
159
|
+
if (saved instanceof SuspendError)
|
|
160
|
+
return Effect.fail(saved);
|
|
161
|
+
this.stop(saved);
|
|
162
|
+
return Deferred.await(this.done);
|
|
163
|
+
}));
|
|
96
164
|
}
|
|
97
|
-
execute(
|
|
165
|
+
execute() {
|
|
98
166
|
const self = this;
|
|
99
167
|
const limits = this.setup.limits;
|
|
100
168
|
const logged = () => {
|
|
@@ -106,7 +174,13 @@ class Running {
|
|
|
106
174
|
// Set only after copy-out so timeouts cannot report invalid values as completed.
|
|
107
175
|
let returned;
|
|
108
176
|
const base = Effect.gen(function* () {
|
|
177
|
+
const driver = self.driver;
|
|
178
|
+
if (self.resuming)
|
|
179
|
+
driver.resume();
|
|
109
180
|
const outcome = yield* driver.run();
|
|
181
|
+
if (outcome === SUSPENDED)
|
|
182
|
+
return self.suspended;
|
|
183
|
+
self.finished = true;
|
|
110
184
|
if (!outcome.ok)
|
|
111
185
|
return failed(normalizeError(outcome.error));
|
|
112
186
|
const result = outcome.value ?? null;
|
|
@@ -122,13 +196,16 @@ class Running {
|
|
|
122
196
|
...logged(),
|
|
123
197
|
toolCalls: toolCalls(),
|
|
124
198
|
};
|
|
125
|
-
}).pipe(Effect.ensuring(Effect.suspend(() => driver.interrupt())));
|
|
199
|
+
}).pipe(Effect.ensuring(Effect.suspend(() => self.driver.interrupt())));
|
|
126
200
|
const timeoutMs = limits.timeoutMs;
|
|
127
201
|
const operation = timeoutMs === undefined
|
|
128
202
|
? base
|
|
129
203
|
: base.pipe(Effect.timeoutOrElse({
|
|
130
|
-
duration: timeoutMs,
|
|
204
|
+
duration: Math.max(0, timeoutMs - this.activeBefore),
|
|
131
205
|
orElse: () => Effect.sync(() => {
|
|
206
|
+
self.finished = true;
|
|
207
|
+
if (self.suspended !== undefined)
|
|
208
|
+
return self.suspended;
|
|
132
209
|
if (returned === undefined) {
|
|
133
210
|
return failed({ kind: "TimeoutExceeded", message: `Execution timed out after ${timeoutMs}ms.` });
|
|
134
211
|
}
|
|
@@ -148,12 +225,63 @@ class Running {
|
|
|
148
225
|
};
|
|
149
226
|
}),
|
|
150
227
|
}));
|
|
151
|
-
return operation.pipe(Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.succeed(failed(normalizeError(Cause.squash(cause))))), Effect.map((result) =>
|
|
228
|
+
return operation.pipe(Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.succeed(failed(normalizeError(Cause.squash(cause))))), Effect.map((result) => result.ok === "suspended" || limits.maxOutputBytes === undefined
|
|
229
|
+
? result
|
|
230
|
+
: boundOutput(result, limits.maxOutputBytes)));
|
|
231
|
+
}
|
|
232
|
+
// Suspends the run once its state is saved: the driver stops.
|
|
233
|
+
stop(saved) {
|
|
234
|
+
this.finished = true;
|
|
235
|
+
this.suspended = saved;
|
|
236
|
+
this.driver.detach();
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Saves the run's state, with no effect on the run: the calls that block a suspend, or the state as it is now. The
|
|
240
|
+
* program shares this thread with the caller, so it is at a step boundary.
|
|
241
|
+
*/
|
|
242
|
+
save(maxBytes) {
|
|
243
|
+
const vm = this.ctx.vm;
|
|
244
|
+
const running = this.driver.blocking();
|
|
245
|
+
if (running.length > 0) {
|
|
246
|
+
return new SuspendError({
|
|
247
|
+
message: `The run can't be suspended while ${running.map((call) => call.path).join(", ")} ${running.length === 1 ? "is" : "are"} running.`,
|
|
248
|
+
running,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
const limits = this.setup.limits;
|
|
252
|
+
const saved = attempt(() => vm.capture({
|
|
253
|
+
limits: {
|
|
254
|
+
timeoutMs: limits.timeoutMs ?? null,
|
|
255
|
+
maxToolCalls: limits.maxToolCalls ?? null,
|
|
256
|
+
maxOutputBytes: limits.maxOutputBytes ?? null,
|
|
257
|
+
},
|
|
258
|
+
activeMs: this.activeBefore + Date.now() - this.startedAt,
|
|
259
|
+
strict: false,
|
|
260
|
+
}));
|
|
261
|
+
if (!saved.ok) {
|
|
262
|
+
const error = saved.error;
|
|
263
|
+
return new SuspendError({
|
|
264
|
+
message: `The run's state couldn't be saved: ${error instanceof Error ? error.message : String(error)}`,
|
|
265
|
+
running: [],
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
const snapshot = saved.value.snapshot;
|
|
269
|
+
if (maxBytes !== undefined) {
|
|
270
|
+
const size = toBytes(snapshot).byteLength;
|
|
271
|
+
if (size > maxBytes) {
|
|
272
|
+
return new SuspendError({
|
|
273
|
+
message: `The run's state takes ${size} bytes, more than the ${maxBytes}-byte limit, so it couldn't be suspended.`,
|
|
274
|
+
running: [],
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return { ok: "suspended", state: snapshot };
|
|
152
279
|
}
|
|
153
280
|
// The round-trip test mode's checkpoint: saves the state to bytes and continues in a brand-new runtime. States that
|
|
154
281
|
// hold live host values (an extension call in flight, a function an extension returned) can't round-trip, so they
|
|
155
282
|
// are skipped; a lost value anywhere else is a codec gap and fails the run.
|
|
156
|
-
trip(
|
|
283
|
+
trip() {
|
|
284
|
+
const driver = this.driver;
|
|
157
285
|
if (this.trips >= ROUND_TRIP_MAX) {
|
|
158
286
|
this.ctx.vm.tripEvery = 0;
|
|
159
287
|
return;
|
|
@@ -177,6 +305,14 @@ class Running {
|
|
|
177
305
|
driver.tools = fresh.tools;
|
|
178
306
|
}
|
|
179
307
|
}
|
|
308
|
+
const attempt = (run) => {
|
|
309
|
+
try {
|
|
310
|
+
return { ok: true, value: run() };
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
return { ok: false, error };
|
|
314
|
+
}
|
|
315
|
+
};
|
|
180
316
|
export const parseProgram = (code) => {
|
|
181
317
|
try {
|
|
182
318
|
const program = parse(code, {
|
|
@@ -22,9 +22,8 @@ export type Record = {
|
|
|
22
22
|
readonly [field: string]: Json;
|
|
23
23
|
};
|
|
24
24
|
/** A saved state can't be read, or doesn't fit this runtime. */
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
25
|
+
import { SnapshotError } from "../../suspend-error.js";
|
|
26
|
+
export { SnapshotError };
|
|
28
27
|
/** What the encoder asks of the runtime whose state it saves. */
|
|
29
28
|
export type EncodeEnv = {
|
|
30
29
|
/** The name of a built-in or host global object. */
|
|
@@ -9,9 +9,8 @@ import { TextDecoderObj } from "../stdlib/bytes.js";
|
|
|
9
9
|
import { ToolError, toolError } from "../../tool-error.js";
|
|
10
10
|
import { ToolReference, ToolRuntimeError } from "../../tool-runtime.js";
|
|
11
11
|
/** A saved state can't be read, or doesn't fit this runtime. */
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
12
|
+
import { SnapshotError } from "../../suspend-error.js";
|
|
13
|
+
export { SnapshotError };
|
|
15
14
|
// --- Registries ---
|
|
16
15
|
const symbols = new Map([
|
|
17
16
|
["iterator", IteratorSymbol],
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { Interpreter } from "../runtime/interpreter.js";
|
|
2
2
|
import type { ToolCall } from "../../tool-runtime.js";
|
|
3
|
+
import { fromBytes, SnapshotError, toBytes } from "../../suspend-error.js";
|
|
3
4
|
import type { Encoded, Record } from "./codec.js";
|
|
4
|
-
|
|
5
|
-
export { SnapshotError };
|
|
5
|
+
export { fromBytes, SnapshotError, toBytes };
|
|
6
6
|
/** The snapshot format this build writes and reads: the bytecode VM's (see `src/vm/snapshot.ts`). */
|
|
7
7
|
export declare const FORMAT = 2;
|
|
8
8
|
/**
|
|
9
|
-
* A run's whole state as JSON: a header that says which build and sources it belongs to
|
|
9
|
+
* A suspended run's whole state as JSON: a header that says which build and sources it belongs to and what budget it
|
|
10
|
+
* has used, and the graph of records (see codec.ts) rooted at the VM, the interpreter, and the global scope, then
|
|
10
11
|
* every built-in or host global the run changed.
|
|
11
12
|
*/
|
|
12
13
|
export type Snapshot = {
|
|
@@ -27,13 +28,22 @@ export type Header = {
|
|
|
27
28
|
readonly hash: string;
|
|
28
29
|
readonly bytecode: string;
|
|
29
30
|
}>;
|
|
31
|
+
readonly limits: {
|
|
32
|
+
readonly timeoutMs: number | null;
|
|
33
|
+
readonly maxToolCalls: number | null;
|
|
34
|
+
readonly maxOutputBytes: number | null;
|
|
35
|
+
};
|
|
36
|
+
/** What the run has used so far, across every resume: active milliseconds, and tool calls admitted. */
|
|
37
|
+
readonly budget: {
|
|
38
|
+
readonly activeMs: number;
|
|
39
|
+
readonly toolCalls: number;
|
|
40
|
+
};
|
|
30
41
|
/** The tool calls admitted so far. */
|
|
31
42
|
readonly toolCalls: ReadonlyArray<ToolCall>;
|
|
32
43
|
};
|
|
44
|
+
export type SnapshotHeader = Header;
|
|
33
45
|
export declare const hostsId: (ctx: Interpreter<unknown>) => string;
|
|
34
|
-
/** A
|
|
35
|
-
export declare const
|
|
36
|
-
/** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
|
|
37
|
-
export declare const fromBytes: (bytes: Uint8Array) => unknown;
|
|
46
|
+
/** A saved state's header, checked in full, before anything is restored. */
|
|
47
|
+
export declare const header: (saved: unknown) => Header;
|
|
38
48
|
export declare const validate: (saved: unknown) => Snapshot;
|
|
39
49
|
export declare const hash: (text: string) => string;
|
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
import { SnapshotError } from "
|
|
2
|
-
export { SnapshotError };
|
|
1
|
+
import { fromBytes, SnapshotError, toBytes } from "../../suspend-error.js";
|
|
2
|
+
export { fromBytes, SnapshotError, toBytes };
|
|
3
3
|
/** The snapshot format this build writes and reads: the bytecode VM's (see `src/vm/snapshot.ts`). */
|
|
4
4
|
export const FORMAT = 2;
|
|
5
5
|
export const hostsId = (ctx) => hash(ctx.intrinsics.hosts().join("\n"));
|
|
6
|
-
/** A snapshot as bytes: UTF-8 JSON. */
|
|
7
|
-
export const toBytes = (snapshot) => new TextEncoder().encode(JSON.stringify(snapshot));
|
|
8
|
-
/** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
|
|
9
|
-
export const fromBytes = (bytes) => {
|
|
10
|
-
try {
|
|
11
|
-
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
12
|
-
}
|
|
13
|
-
catch (error) {
|
|
14
|
-
throw new SnapshotError(`Invalid snapshot: the bytes are not UTF-8 JSON (${String(error)}).`);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
6
|
// --- Validation ---
|
|
18
7
|
const fail = (message) => {
|
|
19
8
|
throw new SnapshotError(`Invalid snapshot: ${message}`);
|
|
@@ -23,12 +12,18 @@ const object = (value, what) => typeof value === "object" && value !== null && !
|
|
|
23
12
|
: fail(`${what} is not an object.`);
|
|
24
13
|
const array = (value, what) => Array.isArray(value) ? value : fail(`${what} is not a list.`);
|
|
25
14
|
const string = (value, what) => typeof value === "string" ? value : fail(`${what} is not text.`);
|
|
15
|
+
const number = (value, what) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : fail(`${what} is not a number.`);
|
|
16
|
+
const limit = (value, what) => (value === null ? null : number(value, what));
|
|
17
|
+
/** A saved state's header, checked in full, before anything is restored. */
|
|
18
|
+
export const header = (saved) => validate(saved).header;
|
|
26
19
|
// The header is checked in full; records are checked as they are decoded.
|
|
27
20
|
export const validate = (saved) => {
|
|
28
21
|
const root = object(saved, "the snapshot");
|
|
29
22
|
if (root.format !== FORMAT)
|
|
30
23
|
fail(`format ${String(root.format)} is not ${FORMAT}.`);
|
|
31
24
|
const header = object(root.header, "the header");
|
|
25
|
+
const limits = object(header.limits, "the limits");
|
|
26
|
+
const budget = object(header.budget, "the budget");
|
|
32
27
|
const checked = {
|
|
33
28
|
build: string(header.build, "the build"),
|
|
34
29
|
hosts: string(header.hosts, "the host globals"),
|
|
@@ -41,6 +36,12 @@ export const validate = (saved) => {
|
|
|
41
36
|
bytecode: string(source.bytecode, "a bytecode hash"),
|
|
42
37
|
};
|
|
43
38
|
}),
|
|
39
|
+
limits: {
|
|
40
|
+
timeoutMs: limit(limits.timeoutMs, "timeoutMs"),
|
|
41
|
+
maxToolCalls: limit(limits.maxToolCalls, "maxToolCalls"),
|
|
42
|
+
maxOutputBytes: limit(limits.maxOutputBytes, "maxOutputBytes"),
|
|
43
|
+
},
|
|
44
|
+
budget: { activeMs: number(budget.activeMs, "the active time"), toolCalls: number(budget.toolCalls, "tool calls") },
|
|
44
45
|
toolCalls: array(header.toolCalls, "the tool calls").map((item) => ({
|
|
45
46
|
name: string(object(item, "a tool call").name, "a tool name"),
|
|
46
47
|
})),
|
package/dist/vm/snapshot.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { Interruption } from "./runtime/signals.js";
|
|
2
2
|
import { type Source } from "./runtime/source.js";
|
|
3
|
-
import { type Snapshot } from "./snapshot/snapshot.js";
|
|
3
|
+
import { type Header, type Snapshot } from "./snapshot/snapshot.js";
|
|
4
4
|
import { Vm } from "./vm.js";
|
|
5
5
|
export type CaptureOptions = {
|
|
6
|
+
readonly limits?: Header["limits"];
|
|
7
|
+
/** Active milliseconds used so far, across every resume. */
|
|
8
|
+
readonly activeMs?: number;
|
|
6
9
|
/** Throw on the first value with no data form, instead of reporting it in `lost`. */
|
|
7
10
|
readonly strict: boolean;
|
|
8
11
|
};
|
package/dist/vm/snapshot.js
CHANGED
|
@@ -134,6 +134,8 @@ export const captureVm = (vm, options) => {
|
|
|
134
134
|
hash: hash(source.code),
|
|
135
135
|
bytecode: vm.sourceCodes.get(source.id)[0].source.hash,
|
|
136
136
|
})),
|
|
137
|
+
limits: options.limits ?? { timeoutMs: null, maxToolCalls: null, maxOutputBytes: null },
|
|
138
|
+
budget: { activeMs: options.activeMs ?? 0, toolCalls: ctx.tools.calls.length },
|
|
137
139
|
toolCalls: ctx.tools.calls.map((call) => ({ name: call.name })),
|
|
138
140
|
},
|
|
139
141
|
roots: encoded.roots,
|
package/dist/vm/vm.d.ts
CHANGED
|
@@ -85,6 +85,7 @@ export declare class Vm {
|
|
|
85
85
|
readonly hosts: HostCalls;
|
|
86
86
|
depth: number;
|
|
87
87
|
fuel: number;
|
|
88
|
+
/** When the current slice began; -1 until the next `run` starts one. Host stops don't end a slice, only a yield does. */
|
|
88
89
|
sliceStart: number;
|
|
89
90
|
sliced: boolean;
|
|
90
91
|
outcome: Completion | undefined;
|
|
@@ -122,11 +123,11 @@ export declare class Vm {
|
|
|
122
123
|
} | undefined;
|
|
123
124
|
/** Saves the run's state as it is now. */
|
|
124
125
|
capture(options: CaptureOptions): {
|
|
125
|
-
readonly snapshot: import("./
|
|
126
|
+
readonly snapshot: import("./index.js").Snapshot;
|
|
126
127
|
readonly lost: ReadonlyArray<string>;
|
|
127
128
|
};
|
|
128
129
|
/** Restores a saved state into this fresh VM, after checking it belongs to this build and runtime. */
|
|
129
|
-
restore(saved: unknown, options: RestoreOptions): import("./
|
|
130
|
+
restore(saved: unknown, options: RestoreOptions): import("./index.js").Snapshot;
|
|
130
131
|
deliverAnswer(frame: Frame): void;
|
|
131
132
|
acquire(code: Code): Frame;
|
|
132
133
|
release(frame: Frame): void;
|
package/dist/vm/vm.js
CHANGED
|
@@ -124,7 +124,8 @@ export class Vm {
|
|
|
124
124
|
hosts;
|
|
125
125
|
depth = 0;
|
|
126
126
|
fuel = INITIAL_FUEL;
|
|
127
|
-
|
|
127
|
+
/** When the current slice began; -1 until the next `run` starts one. Host stops don't end a slice, only a yield does. */
|
|
128
|
+
sliceStart = -1;
|
|
128
129
|
sliced = false;
|
|
129
130
|
outcome = undefined;
|
|
130
131
|
/** Round-trip test mode (task C): slice after this many fuel ticks, so the run is saved and restored all through. */
|
|
@@ -196,7 +197,8 @@ export class Vm {
|
|
|
196
197
|
* other; the outcome; the next job; one inbox completion; wait for running calls; stuck.
|
|
197
198
|
*/
|
|
198
199
|
run() {
|
|
199
|
-
this.sliceStart
|
|
200
|
+
if (this.sliceStart < 0)
|
|
201
|
+
this.sliceStart = performance.now();
|
|
200
202
|
if (this.tripEvery > 0)
|
|
201
203
|
this.fuel = this.tripEvery;
|
|
202
204
|
const sched = this.scheduler;
|
|
@@ -206,6 +208,7 @@ export class Vm {
|
|
|
206
208
|
return { kind: "host", calls: hosts.handOver() };
|
|
207
209
|
if (this.sliced) {
|
|
208
210
|
this.sliced = false;
|
|
211
|
+
this.sliceStart = -1;
|
|
209
212
|
return { kind: "slice" };
|
|
210
213
|
}
|
|
211
214
|
const input = hosts.input;
|
|
@@ -217,8 +220,15 @@ export class Vm {
|
|
|
217
220
|
if (this.frames.length > 0) {
|
|
218
221
|
if (hosts.blocked !== undefined)
|
|
219
222
|
return { kind: "host", calls: [] };
|
|
220
|
-
|
|
223
|
+
// Fuel can run out on a call that then stopped for the host (a built-in or sync extension call costs fuel too):
|
|
224
|
+
// that's a slice point as well, or a loop of such calls would refill forever without yielding.
|
|
225
|
+
if (this.fuel <= 0) {
|
|
221
226
|
this.fuel = this.tripEvery || INITIAL_FUEL;
|
|
227
|
+
if (this.tripEvery > 0 || performance.now() - this.sliceStart >= SLICE_MS) {
|
|
228
|
+
this.sliced = true;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
222
232
|
let signal = EXEC_DONE;
|
|
223
233
|
try {
|
|
224
234
|
signal = this.exec();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@opencode/codemode",
|
|
4
|
-
"version": "0.0.0-dev-
|
|
4
|
+
"version": "0.0.0-dev-927",
|
|
5
5
|
"description": "Effect-native confined code execution over schema-described tools",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|