@opencode/codemode 2.2.6 → 2.2.7

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.
@@ -4,11 +4,13 @@ import { decodeInput as decodeToolInput, decodeOutput as decodeToolOutput, ident
4
4
  import { isNamespace } from "./namespace.js";
5
5
  import { isTool } from "./tool.js";
6
6
  export const compareText = (left, right) => (left < right ? -1 : left > right ? 1 : 0);
7
- /** Runs `before`, then `run`, then `after` with how it ended, including when interrupted. */
8
- export const hooked = (call, before, after, run) => {
7
+ /** Runs `before`, then `run`, then `after` with how it ended, including when interrupted (unless `detaching`). */
8
+ export const hooked = (call, before, after, run, detaching) => {
9
9
  const observed = after === undefined
10
10
  ? run
11
11
  : Effect.onExit(run, (exit) => {
12
+ if (detaching?.())
13
+ return Effect.void;
12
14
  if (Exit.isSuccess(exit))
13
15
  return after(call, { status: "success", value: exit.value });
14
16
  if (Cause.hasInterruptsOnly(exit.cause))
@@ -237,51 +239,139 @@ export class ToolRuntimeError extends Error {
237
239
  /** Per-execution call state over tools prepared once for the runtime. */
238
240
  export const make = (prepared, maxToolCalls, hooks = {}) => {
239
241
  const calls = [];
242
+ const invocations = new Map();
240
243
  const root = prepared.root;
244
+ const runId = crypto.randomUUID();
245
+ let nextCallId = 0;
241
246
  const recordCall = (call) => {
242
247
  if (maxToolCalls !== undefined && calls.length >= maxToolCalls) {
243
248
  throw new ToolRuntimeError("ToolCallLimitExceeded", `Execution exceeded its tool-call limit of ${maxToolCalls}.`);
244
249
  }
245
250
  calls.push(call);
246
251
  };
247
- const executeTool = (name, tool, args) => Effect.gen(function* () {
252
+ const decode = (name, tool, args) => Effect.gen(function* () {
248
253
  const normalized = args.length === 0 ? [{}] : args;
249
254
  if (normalized.length !== 1)
250
255
  throw new ToolRuntimeError("InvalidToolInput", `Tool '${name}' expects at most one input object.`);
251
- const input = yield* Effect.try({
256
+ return yield* Effect.try({
252
257
  try: () => decodeToolInput(tool, normalized[0]),
253
258
  catch: (cause) => new ToolRuntimeError("InvalidToolInput", `Invalid input for tool '${name}': ${String(cause)}`, name === "search" ? [] : ["The signature may have changed. Use search to get the current signature."]),
254
259
  });
255
- yield* Effect.sync(() => recordCall({ name }));
256
- return yield* hooked({ name, input }, hooks["tool.before"], hooks["tool.after"], Effect.gen(function* () {
257
- const raw = yield* Effect.suspend(() => tool.execute(input)).pipe(Effect.catchCause((cause) => {
258
- if (Cause.hasInterruptsOnly(cause))
259
- return Effect.interrupt;
260
- return Effect.fail(toolError(Cause.prettyErrors(cause)
261
- .map((error) => (error.cause ? Formatter.format(error) : error.message || error.name))
262
- .join("\n")));
263
- }));
264
- // The same round trip a tool result would take through text: exact JSON.stringify semantics.
265
- return yield* Effect.try({
266
- try: () => {
267
- const text = JSON.stringify(decodeToolOutput(tool, raw));
268
- return text === undefined ? undefined : JSON.parse(text);
269
- },
270
- catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`),
271
- });
272
- }));
273
260
  });
261
+ // A tool's own failure becomes a ToolError; interruption passes through.
262
+ const failure = (effect) => effect.pipe(Effect.catchCause((cause) => {
263
+ if (Cause.hasInterruptsOnly(cause))
264
+ return Effect.interrupt;
265
+ return Effect.fail(toolError(Cause.prettyErrors(cause)
266
+ .map((error) => (error.cause ? Formatter.format(error) : error.message || error.name))
267
+ .join("\n")));
268
+ }));
269
+ // The same round trip a tool result would take through text: exact JSON.stringify semantics.
270
+ const output = (name, tool, raw) => Effect.try({
271
+ try: () => {
272
+ const text = JSON.stringify(decodeToolOutput(tool, raw));
273
+ return text === undefined ? undefined : JSON.parse(text);
274
+ },
275
+ catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`),
276
+ });
277
+ const executeTool = (name, tool, args, options) => Effect.gen(function* () {
278
+ const key = options?.key ?? `${runId}:${nextCallId++}`;
279
+ const input = yield* decode(name, tool, args);
280
+ const admit = options?.admit;
281
+ yield* Effect.sync(() => (admit !== undefined ? admit(name) : recordCall({ name })));
282
+ return yield* hooked({ name, input, key }, hooks["tool.before"], hooks["tool.after"], Effect.gen(function* () {
283
+ const raw = yield* failure(Effect.suspend(() => tool.execute(input, { key })));
284
+ return yield* output(name, tool, raw);
285
+ }), options?.detaching);
286
+ });
287
+ const durableTool = (path) => {
288
+ const segments = canonicalSegments(path);
289
+ const name = segments.join(".");
290
+ const tool = lookup(root, segments)?.tool;
291
+ if (tool?.mode?._tag !== "CodeModeDurable") {
292
+ throw new TypeError(`Tool '${name}' is not a durable tool.`);
293
+ }
294
+ return { name, tool, durable: tool.mode };
295
+ };
274
296
  return {
275
297
  calls,
276
298
  hooks,
299
+ invocations,
300
+ record: (name) => recordCall({ name }),
277
301
  keys: (path) => namespaceKeys(root, path),
278
- search: (args) => Effect.suspend(() => executeTool("search", makeSearchTool(prepared.searchIndex), args)),
279
- execute: (path, args) => Effect.suspend(() => {
302
+ search: (args, options) => Effect.suspend(() => executeTool("search", makeSearchTool(prepared.searchIndex), args, options)),
303
+ execute: (path, args, options) => Effect.suspend(() => {
280
304
  const segments = canonicalSegments(path);
281
305
  // Models often write `tools.search(...)` for the bare `search(...)`; honor it unless a tool owns that path.
282
306
  if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined)
283
- return executeTool("search", makeSearchTool(prepared.searchIndex), args);
284
- return executeTool(segments.join("."), resolve(root, path, prepared.searchIndex), args);
307
+ return executeTool("search", makeSearchTool(prepared.searchIndex), args, options);
308
+ return executeTool(segments.join("."), resolve(root, path, prepared.searchIndex), args, options);
309
+ }),
310
+ mode: (path) => {
311
+ const segments = canonicalSegments(path);
312
+ if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined)
313
+ return undefined;
314
+ return lookup(root, segments)?.tool?.mode;
315
+ },
316
+ start: (path, args, key, admit) => Effect.suspend(() => {
317
+ const found = durableTool(path);
318
+ return Effect.gen(function* () {
319
+ const input = yield* decode(found.name, found.tool, args);
320
+ yield* Effect.sync(() => (admit !== undefined ? admit(found.name) : recordCall({ name: found.name })));
321
+ const invocation = { name: found.name, input, key };
322
+ invocations.set(key, invocation);
323
+ const before = hooks["tool.before"];
324
+ const after = hooks["tool.after"];
325
+ const run = failure(Effect.suspend(() => found.durable.start(input, { key }))).pipe(Effect.flatMap((raw) => Effect.try({
326
+ try: () => {
327
+ const text = JSON.stringify(raw);
328
+ return text === undefined ? null : JSON.parse(text);
329
+ },
330
+ catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid start data from tool '${found.name}': ${cause}`),
331
+ })));
332
+ const observed = after === undefined
333
+ ? run
334
+ : Effect.onError(run, (cause) => {
335
+ invocations.delete(key);
336
+ if (Cause.hasInterruptsOnly(cause))
337
+ return after(invocation, { status: "interrupted" });
338
+ return after(invocation, { status: "failure", error: Cause.squash(cause) });
339
+ });
340
+ return yield* before === undefined ? observed : Effect.andThen(before(invocation), observed);
341
+ });
342
+ }),
343
+ wait: (path, args, key, data, detaching) => Effect.suspend(() => {
344
+ const found = durableTool(path);
345
+ const wait = found.durable.wait;
346
+ const after = hooks["tool.after"];
347
+ const run = Effect.gen(function* () {
348
+ const raw = yield* failure(Effect.suspend(() => wait(data, { key })));
349
+ return yield* output(found.name, found.tool, raw);
350
+ });
351
+ if (after === undefined)
352
+ return run;
353
+ const known = invocations.get(key);
354
+ const callEffect = known !== undefined
355
+ ? Effect.succeed(known)
356
+ : Effect.map(decode(found.name, found.tool, args), (input) => ({
357
+ name: found.name,
358
+ input,
359
+ key,
360
+ }));
361
+ return Effect.flatMap(callEffect, (call) => Effect.onExit(run, (exit) => {
362
+ if (detaching())
363
+ return Effect.void;
364
+ invocations.delete(key);
365
+ if (Exit.isSuccess(exit))
366
+ return after(call, { status: "success", value: exit.value });
367
+ if (Cause.hasInterruptsOnly(exit.cause))
368
+ return after(call, { status: "interrupted" });
369
+ return after(call, { status: "failure", error: Cause.squash(exit.cause) });
370
+ }));
371
+ }),
372
+ cancel: (path, data) => Effect.suspend(() => {
373
+ const cancel = durableTool(path).durable.cancel;
374
+ return failure(cancel(data));
285
375
  }),
286
376
  };
287
377
  };
package/dist/tool.d.ts CHANGED
@@ -38,13 +38,49 @@ export type JsonSchema = {
38
38
  };
39
39
  /** Either a validating Effect Schema or a render-only JSON Schema document. */
40
40
  export type SchemaType = Schema.Decoder<unknown> | JsonSchema;
41
+ /** Context passed to a tool call: its stable idempotency key (`<runId>:<callId>`). */
42
+ export type ToolContext = {
43
+ readonly key: string;
44
+ };
45
+ export type ExecuteFn<Input = unknown, Output = unknown, R = never> = (input: Input, context: ToolContext) => Effect.Effect<Output, unknown, R>;
46
+ /**
47
+ * A tool execution wrapper for idempotent work that is safe to run again. If a suspend lands while the call is running
48
+ * (`engine: "vm"`), the VM interrupts it without firing `tool.after`, keeps the call queued in the snapshot, and
49
+ * re-runs it on resume with the same `key` without double-counting against `maxToolCalls`.
50
+ */
51
+ export type Idempotent<Input = unknown, Output = unknown, R = never> = {
52
+ (input: Input, context: ToolContext): Effect.Effect<Output, unknown, R>;
53
+ readonly _tag: "CodeModeIdempotent";
54
+ readonly execute: ExecuteFn<Input, Output, R>;
55
+ };
56
+ /**
57
+ * Options for `Tool.durable`: `start` begins the work under `key` and returns JSON `data` saved in the snapshot;
58
+ * `wait` connects to the running work and waits for its output; `cancel` stops the work when the run is interrupted
59
+ * for real or ends, never when it is suspended. `wait` is not tied to the program's JS `await`: the VM calls `wait`
60
+ * right after `start` returns, and again for every `started` call right at resume, whatever the program does with the
61
+ * promise.
62
+ */
63
+ export type DurableOptions<Data extends Schema.Json, Input = unknown, Output = unknown, RStart = never, RWait = never, RCancel = never> = {
64
+ readonly start: (input: Input, context: ToolContext) => Effect.Effect<Data, unknown, RStart>;
65
+ readonly wait: (data: NoInfer<Data>, context: ToolContext) => Effect.Effect<Output, unknown, RWait>;
66
+ readonly cancel: (data: NoInfer<Data>) => Effect.Effect<void, unknown, RCancel>;
67
+ };
68
+ /** A tool execution wrapper for work that keeps running across a suspend and reconnects through its JSON data. */
69
+ export type Durable<Input = unknown, Output = unknown, R = never> = {
70
+ (input: Input, context: ToolContext): Effect.Effect<Output, unknown, R>;
71
+ readonly _tag: "CodeModeDurable";
72
+ readonly start: (input: Input, context: ToolContext) => Effect.Effect<Schema.Json, unknown, R>;
73
+ readonly wait: (data: never, context: ToolContext) => Effect.Effect<Output, unknown, R>;
74
+ readonly cancel: (data: never) => Effect.Effect<void, unknown, R>;
75
+ };
41
76
  /** Executable tool exposed through CodeMode's `tools` object. */
42
77
  export type Tool<R = never> = {
43
78
  readonly _tag: "CodeModeTool";
44
79
  readonly description: string;
45
80
  readonly input: SchemaType;
46
81
  readonly output: SchemaType | undefined;
47
- readonly execute: (input: unknown) => Effect.Effect<unknown, unknown, R>;
82
+ readonly execute: (input: unknown, context?: ToolContext) => Effect.Effect<unknown, unknown, R>;
83
+ readonly mode?: Idempotent<unknown, unknown, R> | Durable<unknown, unknown, R> | undefined;
48
84
  };
49
85
  type InputType<S> = S extends Schema.Decoder<unknown> ? S["Type"] : unknown;
50
86
  type ResultType<S> = S extends undefined ? void : S extends Schema.Decoder<unknown> ? S["Encoded"] : unknown;
@@ -53,9 +89,16 @@ export type Options<I extends SchemaType, O extends SchemaType | undefined, R =
53
89
  readonly description: string;
54
90
  readonly input: I;
55
91
  readonly output?: O;
56
- readonly execute: (input: InputType<I>) => Effect.Effect<ResultType<O>, unknown, R>;
92
+ readonly execute: ExecuteFn<InputType<I>, ResultType<O>, R> | Idempotent<InputType<I>, ResultType<O>, R> | Durable<InputType<I>, ResultType<O>, R>;
57
93
  };
58
94
  export declare const isTool: <R = never>(value: Tool<R> | Namespace<R> | Tools<R> | undefined) => value is Tool<R>;
95
+ /** Marks a tool's `execute` function as safe to interrupt on suspend and re-run on resume with the same key. */
96
+ export declare const idempotent: <Input, Output, R = never>(execute: ExecuteFn<Input, Output, R>) => Idempotent<Input, Output, R>;
97
+ /**
98
+ * Wraps a tool's execution as `start`, `wait`, and `cancel` so work started before a suspend keeps running and
99
+ * reconnects through its JSON data on resume.
100
+ */
101
+ export declare const durable: <Data extends Schema.Json, Input = unknown, Output = unknown, RStart = never, RWait = never, RCancel = never>(options: DurableOptions<Data, Input, Output, RStart, RWait, RCancel>) => Durable<Input, Output, RStart | RWait | RCancel>;
59
102
  /**
60
103
  * Declares one schema-described tool available to a CodeMode program through `tools.*`.
61
104
  *
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