@opencode/codemode 2.2.6 → 2.3.0

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.
Files changed (197) hide show
  1. package/README.md +115 -29
  2. package/dist/{vm/async.d.ts → async.d.ts} +1 -1
  3. package/dist/codemode.d.ts +13 -22
  4. package/dist/codemode.js +18 -39
  5. package/dist/data.d.ts +4 -19
  6. package/dist/data.js +1 -82
  7. package/dist/extension.d.ts +20 -1
  8. package/dist/extension.js +14 -1
  9. package/dist/{vm/host.d.ts → host.d.ts} +5 -2
  10. package/dist/{vm/host.js → host.js} +9 -4
  11. package/dist/index.d.ts +1 -1
  12. package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
  13. package/dist/{vm/prelude → prelude}/helpers.js +1 -1
  14. package/dist/{vm/runtime → runtime}/driver.d.ts +18 -8
  15. package/dist/{vm/runtime → runtime}/driver.js +90 -20
  16. package/dist/{vm/runtime → runtime}/errors.js +2 -2
  17. package/dist/{vm/runtime → runtime}/execute.d.ts +18 -9
  18. package/dist/{vm/runtime → runtime}/execute.js +45 -15
  19. package/dist/{vm/runtime → runtime}/extensions.d.ts +2 -2
  20. package/dist/{vm/runtime → runtime}/extensions.js +121 -19
  21. package/dist/{vm/runtime → runtime}/globals.js +1 -1
  22. package/dist/{vm/runtime → runtime}/interpreter.d.ts +4 -1
  23. package/dist/{vm/runtime → runtime}/interpreter.js +2 -0
  24. package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
  25. package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
  26. package/dist/{vm/runtime → runtime}/objects.js +1 -1
  27. package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
  28. package/dist/{vm/runtime → runtime}/operations.js +1 -1
  29. package/dist/scan.js +2 -2
  30. package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +16 -4
  31. package/dist/{vm/scheduler.js → scheduler.js} +13 -2
  32. package/dist/{vm/snapshot → snapshot}/codec.d.ts +37 -6
  33. package/dist/{vm/snapshot → snapshot}/codec.js +112 -8
  34. package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +13 -6
  35. package/dist/{vm/snapshot → snapshot}/snapshot.js +19 -4
  36. package/dist/{vm/snapshot.js → snapshot.js} +5 -2
  37. package/dist/stdlib/array.d.ts +14 -9
  38. package/dist/stdlib/array.js +89 -276
  39. package/dist/stdlib/bytes.d.ts +11 -7
  40. package/dist/stdlib/bytes.js +112 -79
  41. package/dist/stdlib/collections.d.ts +7 -7
  42. package/dist/stdlib/collections.js +30 -339
  43. package/dist/stdlib/console.d.ts +2 -2
  44. package/dist/stdlib/console.js +3 -3
  45. package/dist/stdlib/date.d.ts +2 -2
  46. package/dist/stdlib/date.js +21 -30
  47. package/dist/stdlib/headers.d.ts +2 -2
  48. package/dist/stdlib/headers.js +15 -33
  49. package/dist/stdlib/iterator.d.ts +2 -2
  50. package/dist/stdlib/iterator.js +10 -303
  51. package/dist/stdlib/json.d.ts +2 -2
  52. package/dist/stdlib/json.js +15 -31
  53. package/dist/stdlib/math.d.ts +2 -7
  54. package/dist/stdlib/math.js +6 -30
  55. package/dist/stdlib/number.d.ts +3 -3
  56. package/dist/stdlib/number.js +8 -8
  57. package/dist/stdlib/object.d.ts +4 -4
  58. package/dist/stdlib/object.js +12 -30
  59. package/dist/stdlib/regexp.d.ts +6 -5
  60. package/dist/stdlib/regexp.js +18 -13
  61. package/dist/stdlib/string.d.ts +2 -2
  62. package/dist/stdlib/string.js +53 -53
  63. package/dist/stdlib/url.d.ts +21 -9
  64. package/dist/stdlib/url.js +55 -55
  65. package/dist/stdlib/value.d.ts +3 -4
  66. package/dist/stdlib/value.js +5 -6
  67. package/dist/stdlib/web.d.ts +4 -4
  68. package/dist/stdlib/web.js +7 -7
  69. package/dist/tool-runtime.d.ts +34 -12
  70. package/dist/tool-runtime.js +350 -68
  71. package/dist/tool-schema.d.ts +5 -0
  72. package/dist/tool-schema.js +36 -16
  73. package/dist/tool.d.ts +60 -2
  74. package/dist/tool.js +17 -1
  75. package/dist/{vm/vm.d.ts → vm.d.ts} +4 -3
  76. package/dist/{vm/vm.js → vm.js} +1 -1
  77. package/package.json +5 -7
  78. package/dist/interpreter/callback.d.ts +0 -24
  79. package/dist/interpreter/callback.js +0 -86
  80. package/dist/interpreter/errors.js +0 -173
  81. package/dist/interpreter/execute.d.ts +0 -14
  82. package/dist/interpreter/execute.js +0 -207
  83. package/dist/interpreter/extensions.d.ts +0 -9
  84. package/dist/interpreter/extensions.js +0 -248
  85. package/dist/interpreter/generators.d.ts +0 -3
  86. package/dist/interpreter/generators.js +0 -168
  87. package/dist/interpreter/globals.js +0 -144
  88. package/dist/interpreter/interpreter.d.ts +0 -48
  89. package/dist/interpreter/interpreter.js +0 -2253
  90. package/dist/interpreter/intrinsics.d.ts +0 -15
  91. package/dist/interpreter/intrinsics.js +0 -105
  92. package/dist/interpreter/model.d.ts +0 -68
  93. package/dist/interpreter/model.js +0 -54
  94. package/dist/interpreter/native.d.ts +0 -17
  95. package/dist/interpreter/native.js +0 -35
  96. package/dist/interpreter/objects.d.ts +0 -301
  97. package/dist/interpreter/objects.js +0 -592
  98. package/dist/interpreter/promises.d.ts +0 -18
  99. package/dist/interpreter/promises.js +0 -292
  100. package/dist/interpreter/runner.d.ts +0 -188
  101. package/dist/interpreter/runner.js +0 -492
  102. package/dist/interpreter/scope.d.ts +0 -16
  103. package/dist/interpreter/scope.js +0 -79
  104. package/dist/suspend-error.d.ts +0 -23
  105. package/dist/suspend-error.js +0 -25
  106. package/dist/vm/data.d.ts +0 -7
  107. package/dist/vm/data.js +0 -13
  108. package/dist/vm/index.d.ts +0 -6
  109. package/dist/vm/index.js +0 -6
  110. package/dist/vm/runtime/errors.d.ts +0 -23
  111. package/dist/vm/runtime/globals.d.ts +0 -6
  112. package/dist/vm/runtime/limits.d.ts +0 -10
  113. package/dist/vm/runtime/limits.js +0 -19
  114. package/dist/vm/runtime/modules.d.ts +0 -4
  115. package/dist/vm/runtime/modules.js +0 -33
  116. package/dist/vm/runtime/references.d.ts +0 -8
  117. package/dist/vm/runtime/references.js +0 -51
  118. package/dist/vm/stdlib/array.d.ts +0 -16
  119. package/dist/vm/stdlib/array.js +0 -311
  120. package/dist/vm/stdlib/bytes.d.ts +0 -11
  121. package/dist/vm/stdlib/bytes.js +0 -222
  122. package/dist/vm/stdlib/collections.d.ts +0 -7
  123. package/dist/vm/stdlib/collections.js +0 -188
  124. package/dist/vm/stdlib/console.d.ts +0 -6
  125. package/dist/vm/stdlib/console.js +0 -93
  126. package/dist/vm/stdlib/date.d.ts +0 -2
  127. package/dist/vm/stdlib/date.js +0 -123
  128. package/dist/vm/stdlib/headers.d.ts +0 -2
  129. package/dist/vm/stdlib/headers.js +0 -92
  130. package/dist/vm/stdlib/iterator.d.ts +0 -2
  131. package/dist/vm/stdlib/iterator.js +0 -30
  132. package/dist/vm/stdlib/json.d.ts +0 -3
  133. package/dist/vm/stdlib/json.js +0 -42
  134. package/dist/vm/stdlib/math.d.ts +0 -3
  135. package/dist/vm/stdlib/math.js +0 -73
  136. package/dist/vm/stdlib/number.d.ts +0 -3
  137. package/dist/vm/stdlib/number.js +0 -72
  138. package/dist/vm/stdlib/object.d.ts +0 -6
  139. package/dist/vm/stdlib/object.js +0 -201
  140. package/dist/vm/stdlib/regexp.d.ts +0 -8
  141. package/dist/vm/stdlib/regexp.js +0 -119
  142. package/dist/vm/stdlib/string.d.ts +0 -2
  143. package/dist/vm/stdlib/string.js +0 -272
  144. package/dist/vm/stdlib/url.d.ts +0 -25
  145. package/dist/vm/stdlib/url.js +0 -255
  146. package/dist/vm/stdlib/value.d.ts +0 -6
  147. package/dist/vm/stdlib/value.js +0 -32
  148. package/dist/vm/stdlib/web.d.ts +0 -5
  149. package/dist/vm/stdlib/web.js +0 -97
  150. /package/dist/{vm/async.js → async.js} +0 -0
  151. /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
  152. /package/dist/{vm/code.js → code.js} +0 -0
  153. /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
  154. /package/dist/{vm/compiler → compiler}/compile.js +0 -0
  155. /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
  156. /package/dist/{vm/compiler → compiler}/scope.js +0 -0
  157. /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
  158. /package/dist/{vm/generators.js → generators.js} +0 -0
  159. /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
  160. /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
  161. /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
  162. /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
  163. /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
  164. /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
  165. /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
  166. /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
  167. /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
  168. /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
  169. /package/dist/{vm/prelude → prelude}/source.js +0 -0
  170. /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
  171. /package/dist/{vm/runtime → runtime}/callback.js +0 -0
  172. /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
  173. /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
  174. /package/dist/{vm/runtime → runtime}/generators.js +0 -0
  175. /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
  176. /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
  177. /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
  178. /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
  179. /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
  180. /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
  181. /package/dist/{interpreter → runtime}/limits.js +0 -0
  182. /package/dist/{vm/runtime → runtime}/model.js +0 -0
  183. /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
  184. /package/dist/{interpreter → runtime}/modules.js +0 -0
  185. /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
  186. /package/dist/{vm/runtime → runtime}/native.js +0 -0
  187. /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
  188. /package/dist/{vm/runtime → runtime}/promises.js +0 -0
  189. /package/dist/{interpreter → runtime}/references.d.ts +0 -0
  190. /package/dist/{interpreter → runtime}/references.js +0 -0
  191. /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
  192. /package/dist/{vm/runtime → runtime}/signals.js +0 -0
  193. /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
  194. /package/dist/{vm/runtime → runtime}/source.js +0 -0
  195. /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
  196. /package/dist/{vm/runtime → runtime}/table.js +0 -0
  197. /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
@@ -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) {
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 { fromBytes, searchSignature, SnapshotError, SuspendError, toBytes, toolExpression, type Outcome, type Program, type Snapshot, type SnapshotHeader, type Suspended, type SuspendOptions, type VmRuntime, } from "./codemode.js";
6
+ export { fromBytes, searchSignature, SnapshotError, SuspendError, toBytes, toolExpression, type Outcome, type Program, type ProgramOptions, type Runtime, type Snapshot, type SnapshotHeader, type Suspended, type SuspendOptions, } from "./codemode.js";
7
7
  export { ToolError, toolError } from "./tool-error.js";
@@ -1,4 +1,4 @@
1
- import type { DiagnosticKind } from "../codemode.js";
1
+ import type { DiagnosticKind } from "./codemode.js";
2
2
  import type { ErrorType } from "./runtime/intrinsics.js";
3
3
  /** Uninitialized lexical binding sentinel (temporal dead zone). */
4
4
  export declare const HOLE: unique symbol;
@@ -9,7 +9,7 @@ import { native, receiver } from "../runtime/native.js";
9
9
  import { Arr, Bytes, coerceToString, data, define, hidden, hasPrototype, keys, getOwn, IteratorObj, positionIterator, coerceToNumber, get, MapObj, Obj, PromiseObj, SetObj, WeakMapObj, WeakSetObj, } from "../runtime/objects.js";
10
10
  import { builtinCursor, requireIteratorObject } from "../runtime/operations.js";
11
11
  import { containsOpaqueReference, describeValue, isOpaque } from "../runtime/references.js";
12
- import { compareText } from "../../tool-runtime.js";
12
+ import { compareText } from "../tool-runtime.js";
13
13
  import { Invoke } from "../runtime/signals.js";
14
14
  const classes = { Array: Arr, Uint8Array: Bytes, Map: MapObj, Set: SetObj };
15
15
  export const helpers = (ctx) => {
@@ -1,6 +1,6 @@
1
1
  import { type Context, Effect } from "effect";
2
2
  import type { HostCall } from "../scheduler.js";
3
- import { type ToolRuntime } from "../../tool-runtime.js";
3
+ import { type ToolRuntime } from "../tool-runtime.js";
4
4
  import type { Vm } from "../vm.js";
5
5
  import { type Completion } from "./signals.js";
6
6
  /** What `Driver.run` returns once the run was suspended: its state is saved, and the VM stopped. */
@@ -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
  }
@@ -1,5 +1,5 @@
1
1
  import { Cause, Effect, Exit, Fiber } from "effect";
2
- import { toolExpression } from "../../tool-runtime.js";
2
+ import { toolExpression } from "../tool-runtime.js";
3
3
  import { locate } from "./errors.js";
4
4
  import { PendingThrow } from "./model.js";
5
5
  import { abrupt, Interruption, normal } from "./signals.js";
@@ -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)
@@ -1,5 +1,5 @@
1
- import { ToolError } from "../../tool-error.js";
2
- import { ToolRuntimeError } from "../../tool-runtime.js";
1
+ import { ToolError } from "../tool-error.js";
2
+ import { ToolRuntimeError } from "../tool-runtime.js";
3
3
  import { formatLocation, PendingThrow, Throw, sourceLocation } from "./model.js";
4
4
  import { containsRuntimeReference } from "./references.js";
5
5
  import { createErrorValue, isErrorType } from "./intrinsics.js";
@@ -1,13 +1,25 @@
1
1
  import { type Program } from "acorn";
2
- import { Effect } from "effect";
3
- import type { DataValue, Diagnostic, Outcome, ResolvedExecutionLimits, Result } from "../../codemode.js";
4
- import type { Extension } from "../../extension.js";
2
+ import { Cause, Effect, Schema } from "effect";
3
+ import type { Diagnostic, Outcome, ResolvedExecutionLimits, Result } from "../codemode.js";
5
4
  import { type Snapshot, SnapshotError } from "../snapshot/snapshot.js";
6
- import { ToolRuntime } from "../../tool-runtime.js";
7
- import { SuspendError } from "../../suspend-error.js";
5
+ import { ToolRuntime } from "../tool-runtime.js";
8
6
  import { Interpreter } from "./interpreter.js";
9
7
  import type { Value } from "./objects.js";
10
8
  import { Source } from "./source.js";
9
+ declare const SuspendError_base: Schema.Class<SuspendError, Schema.TaggedStruct<"SuspendError", {
10
+ readonly message: Schema.String;
11
+ readonly running: Schema.$Array<Schema.Struct<{
12
+ readonly key: Schema.String;
13
+ readonly path: Schema.String;
14
+ }>>;
15
+ }>, Cause.YieldableError>;
16
+ /**
17
+ * A suspend that failed, leaving the run running: `running` names each host call that blocked it by its key and its
18
+ * path (`tools.x.y`, `search`, or an extension function's path such as `fetch`). Empty when the state couldn't be
19
+ * saved or was too large.
20
+ */
21
+ export declare class SuspendError extends SuspendError_base {
22
+ }
11
23
  type Globals<R> = (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>;
12
24
  /** How a runtime runs programs: its tools, limits, hooks, and host globals, shared by every run it starts. */
13
25
  export type Setup<R> = {
@@ -21,11 +33,8 @@ export type Setup<R> = {
21
33
  };
22
34
  /** Turns a suspended outcome into a failure, for callers that only run programs to their end. */
23
35
  export declare const finishExecution: <E, R>(outcome: Effect.Effect<Outcome, E, R>) => Effect.Effect<Result, E, R>;
24
- /** Runs a program on the VM with `CodeMode.make`'s extensions and JSON bindings. */
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
- 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
36
  /** 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>;
37
+ export declare const startProgram: <R>(setup: Setup<R>, code: string, bindings?: Globals<R>, runId?: string) => Running<R>;
29
38
  /**
30
39
  * Restores a saved run into this runtime, under the limits it was saved with, ready to run as `startProgram`'s.
31
40
  * Fails with a `SnapshotError` when the state can't be read or belongs to another build, other host globals, or other
@@ -1,17 +1,24 @@
1
1
  import { parse } from "acorn";
2
2
  import { Cause, Deferred, Effect, Option, Schema } from "effect";
3
- import { fromJson } from "../data.js";
4
3
  import { fromBytes, header, SnapshotError, toBytes } from "../snapshot/snapshot.js";
5
- import { ToolRuntime } from "../../tool-runtime.js";
6
- import { SuspendError } from "../../suspend-error.js";
4
+ import { ToolRuntime } from "../tool-runtime.js";
7
5
  import { normalizeError } from "./errors.js";
8
- import { extensionGlobals } from "./extensions.js";
9
6
  import { createBuiltins } from "./intrinsics.js";
10
7
  import { Interpreter } from "./interpreter.js";
11
8
  import { formatLocation, sourceLocation } from "./model.js";
12
- import { Driver, SUSPENDED } from "./driver.js";
9
+ import { callPath, Driver, SUSPENDED } from "./driver.js";
13
10
  import { Interruption } from "./signals.js";
14
11
  import { Source } from "./source.js";
12
+ /**
13
+ * A suspend that failed, leaving the run running: `running` names each host call that blocked it by its key and its
14
+ * path (`tools.x.y`, `search`, or an extension function's path such as `fetch`). Empty when the state couldn't be
15
+ * saved or was too large.
16
+ */
17
+ export class SuspendError extends Schema.TaggedError()("SuspendError", {
18
+ message: Schema.String,
19
+ running: Schema.Array(Schema.Struct({ key: Schema.String, path: Schema.String })),
20
+ }) {
21
+ }
15
22
  /** Turns a suspended outcome into a failure, for callers that only run programs to their end. */
16
23
  export const finishExecution = (outcome) => Effect.map(outcome, (outcome) => {
17
24
  if (outcome.ok !== "suspended")
@@ -22,13 +29,8 @@ export const finishExecution = (outcome) => Effect.map(outcome, (outcome) => {
22
29
  toolCalls: outcome.state.header.toolCalls,
23
30
  };
24
31
  });
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
32
  /** A program ready to run on the VM: nothing runs until `run`. */
31
- export const startProgram = (setup, code, bindings) => new Running(setup, 0, () => {
33
+ export const startProgram = (setup, code, bindings, runId = crypto.randomUUID()) => new Running(setup, 0, () => {
32
34
  if (code.trim().length === 0) {
33
35
  return { ok: false, error: { kind: "ParseError", message: "Code cannot be empty." }, toolCalls: [] };
34
36
  }
@@ -36,6 +38,7 @@ export const startProgram = (setup, code, bindings) => new Running(setup, 0, ()
36
38
  if (!parsed.ok)
37
39
  return { ok: false, error: parsed.error, toolCalls: [] };
38
40
  const runtime = instantiate(setup, bindings);
41
+ runtime.vm.hosts.runId = runId;
39
42
  runtime.vm.start(parsed.source);
40
43
  return runtime;
41
44
  });
@@ -58,6 +61,13 @@ export const resumeProgram = (setup, saved) => Effect.try({
58
61
  };
59
62
  const fresh = instantiate(resumed);
60
63
  const snapshot = restoreInto(state, fresh.vm);
64
+ for (const call of fresh.vm.calls()) {
65
+ if (call.status !== "started")
66
+ continue;
67
+ if (call.request.kind !== "tool" || fresh.tools.mode(call.request.path)?._tag !== "CodeModeDurable") {
68
+ 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.`);
69
+ }
70
+ }
61
71
  return new Running(resumed, snapshot.header.budget.activeMs, () => fresh, true);
62
72
  },
63
73
  catch: (error) => (error instanceof SnapshotError ? error : new SnapshotError(String(error))),
@@ -142,7 +152,6 @@ export class Running {
142
152
  if (roundTrip)
143
153
  self.driver.checkpoint = Effect.sync(() => self.trip());
144
154
  self.startedAt = Date.now();
145
- yield* Deferred.succeed(self.started, undefined);
146
155
  return yield* self.execute();
147
156
  }).pipe(Effect.onExit((exit) => Effect.andThen(Deferred.succeed(self.started, undefined), Deferred.done(self.done, exit))));
148
157
  }
@@ -177,6 +186,7 @@ export class Running {
177
186
  const driver = self.driver;
178
187
  if (self.resuming)
179
188
  driver.resume();
189
+ yield* Deferred.succeed(self.started, undefined);
180
190
  const outcome = yield* driver.run();
181
191
  if (outcome === SUSPENDED)
182
192
  return self.suspended;
@@ -248,6 +258,14 @@ export class Running {
248
258
  running,
249
259
  });
250
260
  }
261
+ // Idempotent calls in flight are saved as queued so they run again on resume under the same key.
262
+ const idempotent = vm.calls().filter((call) => call.status === "running");
263
+ for (const call of idempotent)
264
+ call.status = "queued";
265
+ const restoreRunning = () => {
266
+ for (const call of idempotent)
267
+ call.status = "running";
268
+ };
251
269
  const limits = this.setup.limits;
252
270
  const saved = attempt(() => vm.capture({
253
271
  limits: {
@@ -259,6 +277,7 @@ export class Running {
259
277
  strict: false,
260
278
  }));
261
279
  if (!saved.ok) {
280
+ restoreRunning();
262
281
  const error = saved.error;
263
282
  return new SuspendError({
264
283
  message: `The run's state couldn't be saved: ${error instanceof Error ? error.message : String(error)}`,
@@ -269,6 +288,7 @@ export class Running {
269
288
  if (maxBytes !== undefined) {
270
289
  const size = toBytes(snapshot).byteLength;
271
290
  if (size > maxBytes) {
291
+ restoreRunning();
272
292
  return new SuspendError({
273
293
  message: `The run's state takes ${size} bytes, more than the ${maxBytes}-byte limit, so it couldn't be suspended.`,
274
294
  running: [],
@@ -278,8 +298,8 @@ export class Running {
278
298
  return { ok: "suspended", state: snapshot };
279
299
  }
280
300
  // 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.
301
+ // hold live host values (an extension call in flight, a function an extension without `snapshot` hooks returned)
302
+ // can't round-trip, so they are skipped; a lost value anywhere else is a codec gap and fails the run.
283
303
  trip() {
284
304
  const driver = this.driver;
285
305
  if (this.trips >= ROUND_TRIP_MAX) {
@@ -293,13 +313,23 @@ export class Running {
293
313
  if (driver.holdsHostValues)
294
314
  return;
295
315
  this.trips++;
296
- const saved = this.ctx.vm.capture({ strict: !this.setup.lossy });
316
+ const attempted = attempt(() => this.ctx.vm.capture({ strict: !this.setup.lossy }));
317
+ // An extension's `snapshot` hooks may refuse to save a value, which fails a suspend, not the run.
318
+ if (!attempted.ok) {
319
+ const error = attempted.error;
320
+ if (error instanceof SnapshotError && error.message.startsWith("The extension "))
321
+ return;
322
+ throw error;
323
+ }
324
+ const saved = attempted.value;
297
325
  const bytes = toBytes(saved.snapshot);
298
326
  this.skip = Math.floor(saved.snapshot.records.length / 200 + bytes.byteLength / 100_000);
299
327
  if (saved.lost.length > 0)
300
328
  return;
301
329
  const fresh = instantiate(this.setup);
302
330
  restoreInto(fromBytes(bytes), fresh.vm);
331
+ for (const [key, invocation] of this.ctx.tools.invocations)
332
+ fresh.tools.invocations.set(key, invocation);
303
333
  this.ctx = fresh;
304
334
  driver.vm = fresh.vm;
305
335
  driver.tools = fresh.tools;
@@ -1,4 +1,4 @@
1
- import { type Extension } from "../../extension.js";
1
+ import { type Extension } from "../extension.js";
2
2
  import type { Interpreter } from "./interpreter.js";
3
3
  import { type AstNode } from "./model.js";
4
4
  import { PromiseObj, type Value } from "./objects.js";
@@ -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;