@opencode/codemode 0.0.0-dev-972 → 0.0.0-dev-973

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/dist/tool.js CHANGED
@@ -1,5 +1,23 @@
1
1
  import { Effect, Schema } from "effect";
2
2
  export const isTool = (value) => value !== undefined && Object.hasOwn(value, "_tag") && value._tag === "CodeModeTool";
3
+ /** Marks a tool's `execute` function as safe to interrupt on suspend and re-run on resume with the same key. */
4
+ export const idempotent = (execute) => Object.assign((input, context) => execute(input, context), {
5
+ _tag: "CodeModeIdempotent",
6
+ execute,
7
+ });
8
+ /**
9
+ * Wraps a tool's execution as `start`, `wait`, and `cancel` so work started before a suspend keeps running and
10
+ * reconnects through its JSON data on resume.
11
+ */
12
+ export const durable = (options) => {
13
+ const wait = options.wait;
14
+ return Object.assign((input, context) => Effect.flatMap(options.start(input, context), (data) => wait(data, context)), {
15
+ _tag: "CodeModeDurable",
16
+ start: options.start,
17
+ wait: options.wait,
18
+ cancel: options.cancel,
19
+ });
20
+ };
3
21
  /**
4
22
  * Declares one schema-described tool available to a CodeMode program through `tools.*`.
5
23
  *
@@ -7,10 +25,15 @@ export const isTool = (value) => value !== undefined && Object.hasOwn(value, "_t
7
25
  * Without `output`, results are exposed as `void`. Hosts remain responsible for authorization
8
26
  * and durable side effects.
9
27
  */
10
- export const make = (options) => ({
11
- _tag: "CodeModeTool",
12
- description: options.description,
13
- input: options.input,
14
- output: options.output,
15
- execute: (input) => options.execute(input),
16
- });
28
+ export const make = (options) => {
29
+ const spec = options.execute;
30
+ const mode = "_tag" in spec ? spec : undefined;
31
+ return {
32
+ _tag: "CodeModeTool",
33
+ description: options.description,
34
+ input: options.input,
35
+ output: options.output,
36
+ ...(mode === undefined ? {} : { mode }),
37
+ execute: (input, context = { key: crypto.randomUUID() }) => spec(input, context),
38
+ };
39
+ };
package/dist/vm/host.d.ts CHANGED
@@ -41,6 +41,8 @@ export declare const VISIT_BUDGET = 2048;
41
41
  export declare class HostCalls {
42
42
  private readonly ctx;
43
43
  readonly promises: Promises;
44
+ /** Names this run's host calls (`<runId>:<callId>`). */
45
+ runId: string;
44
46
  queued: Array<HostCall>;
45
47
  readonly running: Map<number, HostCall>;
46
48
  readonly inbox: Array<Delivery>;
@@ -52,8 +54,9 @@ export declare class HostCalls {
52
54
  readonly call: HostCall;
53
55
  readonly completion: Completion;
54
56
  } | undefined;
55
- /** The synchronous extension call the program is in the middle of: see `Engine.activeExtension`. */
57
+ /** The synchronous extension call the program is in the middle of: see `Vm.activeExtension`. */
56
58
  extension: {
59
+ readonly key: string;
57
60
  readonly path: string;
58
61
  } | undefined;
59
62
  extensionResume: ((completion: Completion) => HostResult) | undefined;
package/dist/vm/host.js CHANGED
@@ -19,6 +19,8 @@ export const VISIT_BUDGET = 2048;
19
19
  export class HostCalls {
20
20
  ctx;
21
21
  promises;
22
+ /** Names this run's host calls (`<runId>:<callId>`). */
23
+ runId = "";
22
24
  queued = [];
23
25
  running = new Map();
24
26
  inbox = [];
@@ -27,7 +29,7 @@ export class HostCalls {
27
29
  blocked = undefined;
28
30
  /** A completed blocking call's outcome, which continues the job on the stack before anything else runs. */
29
31
  input = undefined;
30
- /** The synchronous extension call the program is in the middle of: see `Engine.activeExtension`. */
32
+ /** The synchronous extension call the program is in the middle of: see `Vm.activeExtension`. */
31
33
  extension = undefined;
32
34
  extensionResume = undefined;
33
35
  constructor(ctx, promises) {
@@ -44,7 +46,9 @@ export class HostCalls {
44
46
  }
45
47
  /** Queues blocking extension work: the job stops until it completes, then continues with `resume`. */
46
48
  blockExtension(call, resume) {
47
- return this.block({ kind: "extension", call }, "extension", resume);
49
+ this.block({ kind: "extension", call }, "extension", resume);
50
+ this.extension = { key: this.blocked.key, path: call.label };
51
+ return BLOCK;
48
52
  }
49
53
  block(request, label, resume) {
50
54
  this.blocked = this.queue(request, label, true, "value", undefined, undefined);
@@ -53,7 +57,7 @@ export class HostCalls {
53
57
  }
54
58
  queue(request, label, blocking, result, resolving, site) {
55
59
  const id = this.nextID++;
56
- const call = new HostCall(id, request, label, blocking, result, resolving, site);
60
+ const call = new HostCall(id, `${this.runId}:${id}`, request, label, blocking, result, resolving, site);
57
61
  this.queued.push(call);
58
62
  return call;
59
63
  }
@@ -82,6 +86,7 @@ export class HostCalls {
82
86
  return;
83
87
  this.running.delete(id);
84
88
  // Its outcome is all that is left: the host function, its arguments, and what it returned are dropped.
89
+ call.data = undefined;
85
90
  const request = call.request;
86
91
  if (request.kind === "extension") {
87
92
  request.call.invocation = undefined;
@@ -185,10 +190,10 @@ export const callExtension = (ctx, hosts, slots, site) => {
185
190
  args: slots[1],
186
191
  label,
187
192
  invocation: slots[3],
193
+ extension: slots[4],
188
194
  };
189
195
  if (call.host instanceof Lost)
190
196
  throw lostError(`The function ${label}`);
191
- hosts.extension = { path: label };
192
197
  const run = () => {
193
198
  const started = startExtensionCall(ctx, call.host, call.args, call, site);
194
199
  if (started instanceof PromiseObj) {
@@ -11,8 +11,10 @@ export declare const SUSPENDED: unique symbol;
11
11
  * calls the VM queued, as fibers with the caller's services, reporting each outcome back with `vm.complete`.
12
12
  * Fibers live only here, by host call id, and never become part of the VM's state.
13
13
  *
14
- * The VM and tool bridge can be swapped for restored ones between stops (the round-trip test mode does); a fiber's
15
- * outcome goes to whichever VM is current, by host call id.
14
+ * A durable tool's call runs as two fibers, `start` then `wait`, so suspending can detach the second and keep the JSON
15
+ * data `start` gave. An idempotent tool's running fiber is detached and interrupted on suspend, and its call is saved
16
+ * as queued to run again on resume. The VM and tool bridge can be swapped for restored ones between stops (the
17
+ * round-trip test mode does); a fiber's outcome goes to whichever VM is current, by host call id.
16
18
  */
17
19
  export declare class Driver<R> {
18
20
  vm: Vm;
@@ -20,6 +22,7 @@ export declare class Driver<R> {
20
22
  /** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */
21
23
  private readonly services;
22
24
  private readonly fibers;
25
+ private readonly detached;
23
26
  private wake;
24
27
  private stopping;
25
28
  private suspending;
@@ -36,21 +39,28 @@ export declare class Driver<R> {
36
39
  */
37
40
  run(): Effect.Effect<Completion | typeof SUSPENDED, unknown, R>;
38
41
  /**
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.
42
+ * The host calls that block a suspend now: every call in progress except an idempotent tool call or a durable tool
43
+ * call in `started`, plus any queued or synchronous extension call the program is in the middle of, which holds live
44
+ * host values. A tool call queued but not started starts on resume.
41
45
  */
42
46
  blocking(): Array<{
47
+ readonly key: string;
43
48
  readonly path: string;
44
49
  }>;
45
50
  /** Whether the run holds live host values: an extension call in progress. */
46
51
  get holdsHostValues(): boolean;
47
- /** Stops the run to suspend it, once its state is saved: `run` returns `SUSPENDED` at its next step. */
52
+ /**
53
+ * Stops the run to suspend it, once its state is saved: `run` returns `SUSPENDED` at its next step, and running
54
+ * idempotent or durable calls' fibers are detached, so ending them reports nothing.
55
+ */
48
56
  detach(): void;
49
- /** Stops host work still running, waiting for its cleanup. */
57
+ /** Stops host work still running, waiting for its cleanup; a durable tool's work is cancelled unless detached. */
50
58
  interrupt(): Effect.Effect<void>;
51
- /** Starts any tool calls that were queued when the VM was saved. */
59
+ /** Starts tool calls that were queued (including idempotent calls) and waits on durable calls in `started`. */
52
60
  resume(): void;
61
+ private admit;
53
62
  private launch;
63
+ private wait;
54
64
  private fork;
55
65
  private get settled();
56
66
  }
@@ -11,14 +11,18 @@ export const SUSPENDED = Symbol("codemode.suspended");
11
11
  * calls the VM queued, as fibers with the caller's services, reporting each outcome back with `vm.complete`.
12
12
  * Fibers live only here, by host call id, and never become part of the VM's state.
13
13
  *
14
- * The VM and tool bridge can be swapped for restored ones between stops (the round-trip test mode does); a fiber's
15
- * outcome goes to whichever VM is current, by host call id.
14
+ * A durable tool's call runs as two fibers, `start` then `wait`, so suspending can detach the second and keep the JSON
15
+ * data `start` gave. An idempotent tool's running fiber is detached and interrupted on suspend, and its call is saved
16
+ * as queued to run again on resume. The VM and tool bridge can be swapped for restored ones between stops (the
17
+ * round-trip test mode does); a fiber's outcome goes to whichever VM is current, by host call id.
16
18
  */
17
19
  export class Driver {
18
20
  vm;
19
21
  tools;
20
22
  services;
21
23
  fibers = new Map();
24
+ // Calls whose fibers were interrupted to suspend or end the run: their outcomes are not reported.
25
+ detached = new Set();
22
26
  wake;
23
27
  stopping = false;
24
28
  suspending = false;
@@ -78,13 +82,22 @@ export class Driver {
78
82
  });
79
83
  }
80
84
  /**
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.
85
+ * The host calls that block a suspend now: every call in progress except an idempotent tool call or a durable tool
86
+ * call in `started`, plus any queued or synchronous extension call the program is in the middle of, which holds live
87
+ * host values. A tool call queued but not started starts on resume.
83
88
  */
84
89
  blocking() {
85
90
  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) }));
91
+ const calls = vm.calls().filter((call) => {
92
+ if (call.request.kind === "extension")
93
+ return true;
94
+ if (call.status === "queued" || call.status === "started")
95
+ return false;
96
+ if (call.request.kind === "tool" && this.tools.mode(call.request.path)?._tag === "CodeModeIdempotent")
97
+ return false;
98
+ return true;
99
+ });
100
+ const running = calls.map((call) => ({ key: call.key, path: callPath(call) }));
88
101
  const active = vm.activeExtension;
89
102
  if (active !== undefined && !calls.some((call) => call.blocking && call.request.kind === "extension")) {
90
103
  running.push(active);
@@ -95,49 +108,106 @@ export class Driver {
95
108
  get holdsHostValues() {
96
109
  return this.vm.calls().some((call) => call.request.kind === "extension") || this.vm.activeExtension !== undefined;
97
110
  }
98
- /** Stops the run to suspend it, once its state is saved: `run` returns `SUSPENDED` at its next step. */
111
+ /**
112
+ * Stops the run to suspend it, once its state is saved: `run` returns `SUSPENDED` at its next step, and running
113
+ * idempotent or durable calls' fibers are detached, so ending them reports nothing.
114
+ */
99
115
  detach() {
100
116
  this.suspending = true;
117
+ for (const id of this.fibers.keys())
118
+ this.detached.add(id);
101
119
  this.wake?.();
102
120
  }
103
- /** Stops host work still running, waiting for its cleanup. */
121
+ /** Stops host work still running, waiting for its cleanup; a durable tool's work is cancelled unless detached. */
104
122
  interrupt() {
105
123
  const self = this;
106
- return Effect.gen(function* () {
124
+ return Effect.uninterruptible(Effect.gen(function* () {
107
125
  self.stopping = true;
108
126
  while (self.fibers.size > 0)
109
127
  yield* Fiber.interruptAll([...self.fibers.values()]);
110
- });
128
+ // A suspended run's durable work keeps running, to be waited on at resume.
129
+ if (self.suspending)
130
+ return;
131
+ for (const call of self.vm.calls()) {
132
+ const request = call.request;
133
+ if (call.status !== "started" || request.kind !== "tool")
134
+ continue;
135
+ call.status = "completed";
136
+ yield* Effect.ignore(self.tools.cancel(request.path, call.data)).pipe(Effect.provideContext(self.services));
137
+ }
138
+ }));
111
139
  }
112
- /** Starts any tool calls that were queued when the VM was saved. */
140
+ /** Starts tool calls that were queued (including idempotent calls) and waits on durable calls in `started`. */
113
141
  resume() {
142
+ if (this.suspending)
143
+ return;
114
144
  for (const call of this.vm.calls()) {
115
145
  if (call.status === "queued")
116
146
  this.launch(call);
147
+ else if (call.status === "started")
148
+ this.wait(call);
117
149
  }
118
150
  }
151
+ admit(id, name) {
152
+ const call = this.vm.call(id);
153
+ if (call?.admitted)
154
+ return;
155
+ this.tools.record(name);
156
+ if (call !== undefined)
157
+ call.admitted = true;
158
+ }
119
159
  // Starts host work now; its outcome goes back to the VM: a blocking call's continues the job that made it, a
120
160
  // concurrent call's waits in the inbox and settles its promise in a later job, in completion order.
121
161
  launch(call) {
122
162
  call.status = "running";
123
163
  const request = call.request;
124
- if (request.kind === "tool")
125
- return this.fork(call.id, this.tools.execute(request.path, request.args));
126
- if (request.kind === "search")
127
- return this.fork(call.id, this.tools.search(request.args));
164
+ if (request.kind === "tool") {
165
+ if (this.tools.mode(request.path)?._tag === "CodeModeDurable") {
166
+ const started = this.tools.start(request.path, request.args, call.key, (name) => this.admit(call.id, name));
167
+ return this.fork(call.id, started, (data) => {
168
+ const current = this.vm.call(call.id);
169
+ if (current === undefined)
170
+ return;
171
+ current.status = "started";
172
+ current.data = data;
173
+ if (this.stopping || this.detached.has(call.id))
174
+ return;
175
+ this.wait(current);
176
+ });
177
+ }
178
+ return this.fork(call.id, this.tools.execute(request.path, request.args, {
179
+ key: call.key,
180
+ admit: (name) => this.admit(call.id, name),
181
+ detaching: () => this.suspending,
182
+ }));
183
+ }
184
+ if (request.kind === "search") {
185
+ return this.fork(call.id, this.tools.search(request.args, {
186
+ key: call.key,
187
+ admit: (name) => this.admit(call.id, name),
188
+ }));
189
+ }
128
190
  // An extension's work, made by the extension bridge with the program's services in its requirements.
129
191
  return this.fork(call.id, this.vm.ctx.extensionWork(request.call));
130
192
  }
193
+ wait(call) {
194
+ const request = call.request;
195
+ if (request.kind !== "tool")
196
+ return;
197
+ this.fork(call.id, this.tools.wait(request.path, request.args, call.key, call.data, () => this.suspending));
198
+ }
131
199
  // Forks `work` with the caller's services; it starts synchronously, so admission and hooks happen before the VM
132
- // runs another job.
133
- fork(id, work) {
200
+ // runs another job. A success goes to `next` when given, otherwise to the VM.
201
+ fork(id, work, next) {
134
202
  const fiber = Effect.runForkWith(this.services)(work);
135
203
  this.fibers.set(id, fiber);
136
204
  fiber.addObserver((exit) => {
137
205
  if (this.fibers.get(id) === fiber)
138
206
  this.fibers.delete(id);
139
- // Interrupted to end the run: nothing is left to settle.
140
- if (this.stopping)
207
+ if (next !== undefined && Exit.isSuccess(exit))
208
+ return next(exit.value);
209
+ // Interrupted to end or suspend the run: nothing is left to settle.
210
+ if (this.stopping || this.detached.has(id))
141
211
  return;
142
212
  const call = this.vm.call(id);
143
213
  if (call === undefined)
@@ -25,7 +25,7 @@ export declare const finishExecution: <E, R>(outcome: Effect.Effect<Outcome, E,
25
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>;
26
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
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>;
28
+ export declare const startProgram: <R>(setup: Setup<R>, code: string, bindings?: Globals<R>, runId?: string) => Running<R>;
29
29
  /**
30
30
  * Restores a saved run into this runtime, under the limits it was saved with, ready to run as `startProgram`'s.
31
31
  * Fails with a `SnapshotError` when the state can't be read or belongs to another build, other host globals, or other
@@ -9,7 +9,7 @@ import { extensionGlobals } from "./extensions.js";
9
9
  import { createBuiltins } from "./intrinsics.js";
10
10
  import { Interpreter } from "./interpreter.js";
11
11
  import { formatLocation, sourceLocation } from "./model.js";
12
- import { Driver, SUSPENDED } from "./driver.js";
12
+ import { callPath, Driver, SUSPENDED } from "./driver.js";
13
13
  import { Interruption } from "./signals.js";
14
14
  import { Source } from "./source.js";
15
15
  /** Turns a suspended outcome into a failure, for callers that only run programs to their end. */
@@ -25,10 +25,10 @@ export const finishExecution = (outcome) => Effect.map(outcome, (outcome) => {
25
25
  /** Runs a program on the VM with `CodeMode.make`'s extensions and JSON bindings. */
26
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
27
  ? undefined
28
- : (ctx) => Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)]), extensions.length > 0);
28
+ : (ctx) => Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)]), extensions.some((extension) => extension.snapshot === undefined));
29
29
  export const executeProgram = (code, prepared, limits, hooks, globals, bindings, lossy = false) => finishExecution(startProgram({ prepared, limits, hooks, globals, lossy }, code, bindings).run());
30
30
  /** A program ready to run on the VM: nothing runs until `run`. */
31
- export const startProgram = (setup, code, bindings) => new Running(setup, 0, () => {
31
+ export const startProgram = (setup, code, bindings, runId = crypto.randomUUID()) => new Running(setup, 0, () => {
32
32
  if (code.trim().length === 0) {
33
33
  return { ok: false, error: { kind: "ParseError", message: "Code cannot be empty." }, toolCalls: [] };
34
34
  }
@@ -36,6 +36,7 @@ export const startProgram = (setup, code, bindings) => new Running(setup, 0, ()
36
36
  if (!parsed.ok)
37
37
  return { ok: false, error: parsed.error, toolCalls: [] };
38
38
  const runtime = instantiate(setup, bindings);
39
+ runtime.vm.hosts.runId = runId;
39
40
  runtime.vm.start(parsed.source);
40
41
  return runtime;
41
42
  });
@@ -58,6 +59,13 @@ export const resumeProgram = (setup, saved) => Effect.try({
58
59
  };
59
60
  const fresh = instantiate(resumed);
60
61
  const snapshot = restoreInto(state, fresh.vm);
62
+ for (const call of fresh.vm.calls()) {
63
+ if (call.status !== "started")
64
+ continue;
65
+ if (call.request.kind !== "tool" || fresh.tools.mode(call.request.path)?._tag !== "CodeModeDurable") {
66
+ throw new SnapshotError(`The pending durable call ${call.key} (${callPath(call)}) cannot resume because that tool is no longer a durable tool in this runtime.`);
67
+ }
68
+ }
61
69
  return new Running(resumed, snapshot.header.budget.activeMs, () => fresh, true);
62
70
  },
63
71
  catch: (error) => (error instanceof SnapshotError ? error : new SnapshotError(String(error))),
@@ -142,7 +150,6 @@ export class Running {
142
150
  if (roundTrip)
143
151
  self.driver.checkpoint = Effect.sync(() => self.trip());
144
152
  self.startedAt = Date.now();
145
- yield* Deferred.succeed(self.started, undefined);
146
153
  return yield* self.execute();
147
154
  }).pipe(Effect.onExit((exit) => Effect.andThen(Deferred.succeed(self.started, undefined), Deferred.done(self.done, exit))));
148
155
  }
@@ -177,6 +184,7 @@ export class Running {
177
184
  const driver = self.driver;
178
185
  if (self.resuming)
179
186
  driver.resume();
187
+ yield* Deferred.succeed(self.started, undefined);
180
188
  const outcome = yield* driver.run();
181
189
  if (outcome === SUSPENDED)
182
190
  return self.suspended;
@@ -248,6 +256,14 @@ export class Running {
248
256
  running,
249
257
  });
250
258
  }
259
+ // Idempotent calls in flight are saved as queued so they run again on resume under the same key.
260
+ const idempotent = vm.calls().filter((call) => call.status === "running");
261
+ for (const call of idempotent)
262
+ call.status = "queued";
263
+ const restoreRunning = () => {
264
+ for (const call of idempotent)
265
+ call.status = "running";
266
+ };
251
267
  const limits = this.setup.limits;
252
268
  const saved = attempt(() => vm.capture({
253
269
  limits: {
@@ -259,6 +275,7 @@ export class Running {
259
275
  strict: false,
260
276
  }));
261
277
  if (!saved.ok) {
278
+ restoreRunning();
262
279
  const error = saved.error;
263
280
  return new SuspendError({
264
281
  message: `The run's state couldn't be saved: ${error instanceof Error ? error.message : String(error)}`,
@@ -269,6 +286,7 @@ export class Running {
269
286
  if (maxBytes !== undefined) {
270
287
  const size = toBytes(snapshot).byteLength;
271
288
  if (size > maxBytes) {
289
+ restoreRunning();
272
290
  return new SuspendError({
273
291
  message: `The run's state takes ${size} bytes, more than the ${maxBytes}-byte limit, so it couldn't be suspended.`,
274
292
  running: [],
@@ -278,8 +296,8 @@ export class Running {
278
296
  return { ok: "suspended", state: snapshot };
279
297
  }
280
298
  // 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.
299
+ // hold live host values (an extension call in flight, a function an extension without `snapshot` hooks returned)
300
+ // can't round-trip, so they are skipped; a lost value anywhere else is a codec gap and fails the run.
283
301
  trip() {
284
302
  const driver = this.driver;
285
303
  if (this.trips >= ROUND_TRIP_MAX) {
@@ -293,13 +311,23 @@ export class Running {
293
311
  if (driver.holdsHostValues)
294
312
  return;
295
313
  this.trips++;
296
- const saved = this.ctx.vm.capture({ strict: !this.setup.lossy });
314
+ const attempted = attempt(() => this.ctx.vm.capture({ strict: !this.setup.lossy }));
315
+ // An extension's `snapshot` hooks may refuse to save a value, which fails a suspend, not the run.
316
+ if (!attempted.ok) {
317
+ const error = attempted.error;
318
+ if (error instanceof SnapshotError && error.message.startsWith("The extension "))
319
+ return;
320
+ throw error;
321
+ }
322
+ const saved = attempted.value;
297
323
  const bytes = toBytes(saved.snapshot);
298
324
  this.skip = Math.floor(saved.snapshot.records.length / 200 + bytes.byteLength / 100_000);
299
325
  if (saved.lost.length > 0)
300
326
  return;
301
327
  const fresh = instantiate(this.setup);
302
328
  restoreInto(fromBytes(bytes), fresh.vm);
329
+ for (const [key, invocation] of this.ctx.tools.invocations)
330
+ fresh.tools.invocations.set(key, invocation);
303
331
  this.ctx = fresh;
304
332
  driver.vm = fresh.vm;
305
333
  driver.tools = fresh.tools;
@@ -13,4 +13,4 @@ export declare const extensionGlobals: <R>(ctx: Interpreter<R>, extensions: Read
13
13
  * Calls a synchronous extension function's host function, once its `extension.before` hook allowed it: a promise for
14
14
  * a result that settles later (an Effect or a Promise), or the request that settles what it returned or threw now.
15
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;
16
+ export declare const startExtensionCall: <R>(ctx: Interpreter<R>, host: Function, args: ReadonlyArray<unknown>, call: Pick<ExtensionRequest, "label" | "extension" | "invocation">, site: AstNode | undefined) => PromiseObj | ExtensionRequest;