@opencode/codemode 2.2.4 → 2.2.6
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 +70 -1
- package/dist/codemode.d.ts +55 -4
- package/dist/codemode.js +63 -8
- package/dist/extension.d.ts +14 -9
- package/dist/extension.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interpreter/execute.d.ts +1 -1
- package/dist/interpreter/execute.js +16 -4
- package/dist/interpreter/extensions.d.ts +3 -3
- package/dist/interpreter/extensions.js +17 -11
- package/dist/interpreter/globals.js +10 -0
- package/dist/interpreter/interpreter.d.ts +11 -1
- package/dist/interpreter/interpreter.js +86 -2
- package/dist/interpreter/modules.d.ts +4 -0
- package/dist/interpreter/modules.js +33 -0
- package/dist/suspend-error.d.ts +23 -0
- package/dist/suspend-error.js +25 -0
- package/dist/tool-runtime.d.ts +4 -1
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +103 -0
- package/dist/vm/host.js +226 -0
- package/dist/vm/index.d.ts +6 -0
- package/dist/vm/index.js +6 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +58 -0
- package/dist/vm/runtime/driver.js +184 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +80 -0
- package/dist/vm/runtime/execute.js +414 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +270 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +50 -0
- package/dist/vm/runtime/interpreter.js +94 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +178 -0
- package/dist/vm/scheduler.js +63 -0
- package/dist/vm/snapshot/codec.d.ts +87 -0
- package/dist/vm/snapshot/codec.js +751 -0
- package/dist/vm/snapshot/snapshot.d.ts +49 -0
- package/dist/vm/snapshot/snapshot.js +70 -0
- package/dist/vm/snapshot.d.ts +32 -0
- package/dist/vm/snapshot.js +306 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +225 -0
- package/dist/vm/vm.js +2931 -0
- package/package.json +6 -3
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { parse } from "acorn";
|
|
2
|
+
import { Cause, Deferred, Effect, Option, Schema } from "effect";
|
|
3
|
+
import { fromJson } from "../data.js";
|
|
4
|
+
import { fromBytes, header, SnapshotError, toBytes } from "../snapshot/snapshot.js";
|
|
5
|
+
import { ToolRuntime } from "../../tool-runtime.js";
|
|
6
|
+
import { SuspendError } from "../../suspend-error.js";
|
|
7
|
+
import { normalizeError } from "./errors.js";
|
|
8
|
+
import { extensionGlobals } from "./extensions.js";
|
|
9
|
+
import { createBuiltins } from "./intrinsics.js";
|
|
10
|
+
import { Interpreter } from "./interpreter.js";
|
|
11
|
+
import { formatLocation, sourceLocation } from "./model.js";
|
|
12
|
+
import { Driver, SUSPENDED } from "./driver.js";
|
|
13
|
+
import { Interruption } from "./signals.js";
|
|
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
|
+
});
|
|
25
|
+
/** Runs a program on the VM with `CodeMode.make`'s extensions and JSON bindings. */
|
|
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
|
|
27
|
+
? undefined
|
|
28
|
+
: (ctx) => Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)]), extensions.length > 0);
|
|
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, () => {
|
|
32
|
+
if (code.trim().length === 0) {
|
|
33
|
+
return { ok: false, error: { kind: "ParseError", message: "Code cannot be empty." }, toolCalls: [] };
|
|
34
|
+
}
|
|
35
|
+
const parsed = parseProgram(code);
|
|
36
|
+
if (!parsed.ok)
|
|
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))),
|
|
64
|
+
});
|
|
65
|
+
// Sources restored recently, by text: the AST is never changed, so runs restored from the same program share it.
|
|
66
|
+
const restoredSources = new Map();
|
|
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
|
+
});
|
|
82
|
+
// A new runtime: intrinsics, the host globals, the program's bindings, a fresh tool bridge, and its VM.
|
|
83
|
+
const instantiate = (setup, bindings) => {
|
|
84
|
+
const ctx = new Interpreter({
|
|
85
|
+
tools: ToolRuntime.make(setup.prepared, setup.limits.maxToolCalls, setup.hooks),
|
|
86
|
+
builtins: createBuiltins(),
|
|
87
|
+
logs: [],
|
|
88
|
+
globals: setup.globals,
|
|
89
|
+
bindings,
|
|
90
|
+
});
|
|
91
|
+
if (roundTrip)
|
|
92
|
+
ctx.vm.tripEvery = ROUND_TRIP_TICKS;
|
|
93
|
+
return ctx;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* The round-trip test mode: at every host stop and every slice, which come every few loop back-edges and calls, a run's
|
|
97
|
+
* state is saved to bytes and restored into a brand-new runtime, which continues the run. It proves the VM's state is
|
|
98
|
+
* plain data at every step. Internal: only the test suite turns it on (`bun run test:roundtrip`).
|
|
99
|
+
*/
|
|
100
|
+
let roundTrip = false;
|
|
101
|
+
export const setRoundTrip = (enabled) => {
|
|
102
|
+
roundTrip = enabled;
|
|
103
|
+
};
|
|
104
|
+
const ROUND_TRIP_TICKS = 2;
|
|
105
|
+
const ROUND_TRIP_MAX = 200;
|
|
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 {
|
|
108
|
+
setup;
|
|
109
|
+
activeBefore;
|
|
110
|
+
prepare;
|
|
111
|
+
resuming;
|
|
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;
|
|
118
|
+
trips = 0;
|
|
119
|
+
// Checkpoints to let pass before the next round trip, so a large state is not saved at every one.
|
|
120
|
+
skip = 0;
|
|
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) {
|
|
126
|
+
this.setup = setup;
|
|
127
|
+
this.activeBefore = activeBefore;
|
|
128
|
+
this.prepare = prepare;
|
|
129
|
+
this.resuming = resuming;
|
|
130
|
+
}
|
|
131
|
+
/** Makes the run's runtime and driver, then runs the program in the current fiber until it ends or is suspended. */
|
|
132
|
+
run() {
|
|
133
|
+
const self = this;
|
|
134
|
+
return Effect.gen(function* () {
|
|
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());
|
|
142
|
+
if (roundTrip)
|
|
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
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
execute() {
|
|
166
|
+
const self = this;
|
|
167
|
+
const limits = this.setup.limits;
|
|
168
|
+
const logged = () => {
|
|
169
|
+
const logs = self.ctx.logs;
|
|
170
|
+
return logs.length > 0 ? { logs: [...logs] } : {};
|
|
171
|
+
};
|
|
172
|
+
const toolCalls = () => self.ctx.tools.calls;
|
|
173
|
+
const failed = (error) => ({ ok: false, error, ...logged(), toolCalls: toolCalls() });
|
|
174
|
+
// Set only after copy-out so timeouts cannot report invalid values as completed.
|
|
175
|
+
let returned;
|
|
176
|
+
const base = Effect.gen(function* () {
|
|
177
|
+
const driver = self.driver;
|
|
178
|
+
if (self.resuming)
|
|
179
|
+
driver.resume();
|
|
180
|
+
const outcome = yield* driver.run();
|
|
181
|
+
if (outcome === SUSPENDED)
|
|
182
|
+
return self.suspended;
|
|
183
|
+
self.finished = true;
|
|
184
|
+
if (!outcome.ok)
|
|
185
|
+
return failed(normalizeError(outcome.error));
|
|
186
|
+
const result = outcome.value ?? null;
|
|
187
|
+
const vm = self.ctx.vm;
|
|
188
|
+
returned = { value: result, warnings: () => vm.diagnostics() };
|
|
189
|
+
// Host work the program left running is interrupted, not awaited.
|
|
190
|
+
yield* driver.interrupt();
|
|
191
|
+
const warnings = vm.diagnostics();
|
|
192
|
+
return {
|
|
193
|
+
ok: true,
|
|
194
|
+
value: result,
|
|
195
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
196
|
+
...logged(),
|
|
197
|
+
toolCalls: toolCalls(),
|
|
198
|
+
};
|
|
199
|
+
}).pipe(Effect.ensuring(Effect.suspend(() => self.driver.interrupt())));
|
|
200
|
+
const timeoutMs = limits.timeoutMs;
|
|
201
|
+
const operation = timeoutMs === undefined
|
|
202
|
+
? base
|
|
203
|
+
: base.pipe(Effect.timeoutOrElse({
|
|
204
|
+
duration: Math.max(0, timeoutMs - this.activeBefore),
|
|
205
|
+
orElse: () => Effect.sync(() => {
|
|
206
|
+
self.finished = true;
|
|
207
|
+
if (self.suspended !== undefined)
|
|
208
|
+
return self.suspended;
|
|
209
|
+
if (returned === undefined) {
|
|
210
|
+
return failed({ kind: "TimeoutExceeded", message: `Execution timed out after ${timeoutMs}ms.` });
|
|
211
|
+
}
|
|
212
|
+
// Keep the timeout warning first so truncation preserves it.
|
|
213
|
+
return {
|
|
214
|
+
ok: true,
|
|
215
|
+
value: returned.value,
|
|
216
|
+
warnings: [
|
|
217
|
+
{
|
|
218
|
+
kind: "TimeoutExceeded",
|
|
219
|
+
message: `The program returned, but background work was still running at the ${timeoutMs}ms timeout and was interrupted. Await all started promises.`,
|
|
220
|
+
},
|
|
221
|
+
...returned.warnings(),
|
|
222
|
+
],
|
|
223
|
+
...logged(),
|
|
224
|
+
toolCalls: toolCalls(),
|
|
225
|
+
};
|
|
226
|
+
}),
|
|
227
|
+
}));
|
|
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 };
|
|
279
|
+
}
|
|
280
|
+
// The round-trip test mode's checkpoint: saves the state to bytes and continues in a brand-new runtime. States that
|
|
281
|
+
// hold live host values (an extension call in flight, a function an extension returned) can't round-trip, so they
|
|
282
|
+
// are skipped; a lost value anywhere else is a codec gap and fails the run.
|
|
283
|
+
trip() {
|
|
284
|
+
const driver = this.driver;
|
|
285
|
+
if (this.trips >= ROUND_TRIP_MAX) {
|
|
286
|
+
this.ctx.vm.tripEvery = 0;
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (this.skip > 0) {
|
|
290
|
+
this.skip--;
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (driver.holdsHostValues)
|
|
294
|
+
return;
|
|
295
|
+
this.trips++;
|
|
296
|
+
const saved = this.ctx.vm.capture({ strict: !this.setup.lossy });
|
|
297
|
+
const bytes = toBytes(saved.snapshot);
|
|
298
|
+
this.skip = Math.floor(saved.snapshot.records.length / 200 + bytes.byteLength / 100_000);
|
|
299
|
+
if (saved.lost.length > 0)
|
|
300
|
+
return;
|
|
301
|
+
const fresh = instantiate(this.setup);
|
|
302
|
+
restoreInto(fromBytes(bytes), fresh.vm);
|
|
303
|
+
this.ctx = fresh;
|
|
304
|
+
driver.vm = fresh.vm;
|
|
305
|
+
driver.tools = fresh.tools;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const attempt = (run) => {
|
|
309
|
+
try {
|
|
310
|
+
return { ok: true, value: run() };
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
return { ok: false, error };
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
export const parseProgram = (code) => {
|
|
317
|
+
try {
|
|
318
|
+
const program = parse(code, {
|
|
319
|
+
ecmaVersion: "latest",
|
|
320
|
+
sourceType: "script",
|
|
321
|
+
allowReturnOutsideFunction: true,
|
|
322
|
+
allowAwaitOutsideFunction: true,
|
|
323
|
+
// Imports bind globals by module name; scripts keep sloppy-mode semantics, which module source would make strict.
|
|
324
|
+
allowImportExportEverywhere: true,
|
|
325
|
+
locations: true,
|
|
326
|
+
});
|
|
327
|
+
const exported = program.body.find((statement) => statement.type.startsWith("Export"));
|
|
328
|
+
if (exported) {
|
|
329
|
+
const location = sourceLocation(exported);
|
|
330
|
+
return {
|
|
331
|
+
ok: false,
|
|
332
|
+
error: {
|
|
333
|
+
kind: "ParseError",
|
|
334
|
+
message: `SyntaxError: Exports aren't available. Return the result instead.${formatLocation(location)}`,
|
|
335
|
+
location,
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
return { ok: true, program, source: new Source("main", code, program) };
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
// Only acorn's own syntax errors become a located ParseError; anything else keeps the generic failure path.
|
|
343
|
+
const acorn = error instanceof SyntaxError ? decodeAcornError(error) : Option.none();
|
|
344
|
+
if (Option.isNone(acorn))
|
|
345
|
+
throw error;
|
|
346
|
+
return { ok: false, error: parseDiagnostic(acorn.value) };
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
// Acorn appends ` (line:column)` to its messages; its lines are 1-based and its columns 0-based.
|
|
350
|
+
const AcornError = Schema.Struct({
|
|
351
|
+
message: Schema.String,
|
|
352
|
+
loc: Schema.Struct({ line: Schema.Number, column: Schema.Number }),
|
|
353
|
+
});
|
|
354
|
+
const decodeAcornError = Schema.decodeUnknownOption(AcornError);
|
|
355
|
+
const parseDiagnostic = (error) => {
|
|
356
|
+
const location = { line: error.loc.line, column: error.loc.column + 1 };
|
|
357
|
+
const reason = error.message.replace(/ \(\d+:\d+\)$/, "");
|
|
358
|
+
const text = `${reason.endsWith(".") ? reason : `${reason}.`} Code must be plain JavaScript written as a program body.`;
|
|
359
|
+
return { kind: "ParseError", message: `SyntaxError: ${text}${formatLocation(location)}`, location };
|
|
360
|
+
};
|
|
361
|
+
const utf8ByteLength = (value) => new TextEncoder().encode(value).byteLength;
|
|
362
|
+
// Drop a replacement character produced by truncating inside a UTF-8 sequence.
|
|
363
|
+
const utf8Truncate = (value, maxBytes) => {
|
|
364
|
+
const bytes = new TextEncoder().encode(value);
|
|
365
|
+
if (bytes.byteLength <= maxBytes)
|
|
366
|
+
return value;
|
|
367
|
+
const text = new TextDecoder("utf-8").decode(bytes.slice(0, Math.max(0, maxBytes)));
|
|
368
|
+
return text.endsWith("\uFFFD") ? text.slice(0, -1) : text;
|
|
369
|
+
};
|
|
370
|
+
// The longest prefix of `items` whose sizes add up to at most `budget` bytes.
|
|
371
|
+
const fitting = (items, budget, size) => {
|
|
372
|
+
const kept = [];
|
|
373
|
+
let used = 0;
|
|
374
|
+
for (const item of items) {
|
|
375
|
+
used += size(item);
|
|
376
|
+
if (used > budget)
|
|
377
|
+
break;
|
|
378
|
+
kept.push(item);
|
|
379
|
+
}
|
|
380
|
+
return kept;
|
|
381
|
+
};
|
|
382
|
+
// Warnings have a separate budget so result data cannot starve diagnostics.
|
|
383
|
+
const boundOutput = (result, maxOutputBytes) => {
|
|
384
|
+
const serialized = result.ok ? (JSON.stringify(result.value) ?? "null") : "";
|
|
385
|
+
const valueBytes = result.ok ? utf8ByteLength(serialized) : 0;
|
|
386
|
+
const valueTruncated = valueBytes > maxOutputBytes;
|
|
387
|
+
const value = !result.ok
|
|
388
|
+
? null
|
|
389
|
+
: valueTruncated
|
|
390
|
+
? `${utf8Truncate(serialized, maxOutputBytes)} [result truncated: ${valueBytes} bytes exceeds the ${maxOutputBytes}-byte output limit; return a smaller value]`
|
|
391
|
+
: result.value;
|
|
392
|
+
const warnings = result.ok ? (result.warnings ?? []) : [];
|
|
393
|
+
const keptWarnings = fitting(warnings, maxOutputBytes, (warning) => utf8ByteLength(JSON.stringify(warning)) + 1);
|
|
394
|
+
const warningsCut = keptWarnings.length < warnings.length;
|
|
395
|
+
if (warningsCut) {
|
|
396
|
+
keptWarnings.push({
|
|
397
|
+
kind: "Truncated",
|
|
398
|
+
message: `${warnings.length - keptWarnings.length} additional warnings omitted by the output limit.`,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
const logs = result.logs ?? [];
|
|
402
|
+
const logBudget = Math.max(0, maxOutputBytes - Math.min(valueBytes, maxOutputBytes));
|
|
403
|
+
const kept = fitting(logs, logBudget, (line) => utf8ByteLength(line) + 1);
|
|
404
|
+
const logsCut = kept.length < logs.length;
|
|
405
|
+
if (logsCut)
|
|
406
|
+
kept.push(`[logs truncated: showing ${kept.length} of ${logs.length} lines]`);
|
|
407
|
+
if (!valueTruncated && !warningsCut && !logsCut)
|
|
408
|
+
return result;
|
|
409
|
+
const warningsPart = keptWarnings.length > 0 ? { warnings: keptWarnings } : {};
|
|
410
|
+
const logsPart = kept.length > 0 ? { logs: kept } : {};
|
|
411
|
+
return result.ok
|
|
412
|
+
? { ok: true, value, ...warningsPart, ...logsPart, truncated: true, toolCalls: result.toolCalls }
|
|
413
|
+
: { ok: false, error: result.error, ...logsPart, truncated: true, toolCalls: result.toolCalls };
|
|
414
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Extension } from "../../extension.js";
|
|
2
|
+
import type { Interpreter } from "./interpreter.js";
|
|
3
|
+
import { type AstNode } from "./model.js";
|
|
4
|
+
import { PromiseObj, type Value } from "./objects.js";
|
|
5
|
+
import type { ExtensionRequest } from "../scheduler.js";
|
|
6
|
+
/**
|
|
7
|
+
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and built-in
|
|
8
|
+
* wrappers are copied, a host function becomes a program function whose calls cross the same way, and a host Promise
|
|
9
|
+
* or Effect becomes a program promise.
|
|
10
|
+
*/
|
|
11
|
+
export declare const extensionGlobals: <R>(ctx: Interpreter<R>, extensions: ReadonlyArray<Extension>) => ReadonlyArray<readonly [string, Value]>;
|
|
12
|
+
/**
|
|
13
|
+
* Calls a synchronous extension function's host function, once its `extension.before` hook allowed it: a promise for
|
|
14
|
+
* a result that settles later (an Effect or a Promise), or the request that settles what it returned or threw now.
|
|
15
|
+
*/
|
|
16
|
+
export declare const startExtensionCall: <R>(ctx: Interpreter<R>, host: Function, args: ReadonlyArray<unknown>, call: Pick<ExtensionRequest, "label" | "invocation">, site: AstNode | undefined) => PromiseObj | ExtensionRequest;
|