clanka 0.8.5 → 0.9.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 (92) hide show
  1. package/dist/Acp.d.ts.map +1 -1
  2. package/dist/Acp.js +9 -3
  3. package/dist/Acp.js.map +1 -1
  4. package/dist/Acp.test.js +72 -1
  5. package/dist/Acp.test.js.map +1 -1
  6. package/dist/AcpImage.test.js +1 -0
  7. package/dist/AcpImage.test.js.map +1 -1
  8. package/dist/Agent.d.ts +65 -8
  9. package/dist/Agent.d.ts.map +1 -1
  10. package/dist/Agent.js +481 -113
  11. package/dist/Agent.js.map +1 -1
  12. package/dist/Agent.test.js +77 -4
  13. package/dist/Agent.test.js.map +1 -1
  14. package/dist/AgentCallerConfig.test.d.ts +2 -0
  15. package/dist/AgentCallerConfig.test.d.ts.map +1 -0
  16. package/dist/AgentCallerConfig.test.js +327 -0
  17. package/dist/AgentCallerConfig.test.js.map +1 -0
  18. package/dist/AgentExecutor.d.ts +254 -54
  19. package/dist/AgentExecutor.d.ts.map +1 -1
  20. package/dist/AgentExecutor.js +416 -99
  21. package/dist/AgentExecutor.js.map +1 -1
  22. package/dist/AgentExecutor.test.js +201 -31
  23. package/dist/AgentExecutor.test.js.map +1 -1
  24. package/dist/AgentExecutorImage.test.js +2 -1
  25. package/dist/AgentExecutorImage.test.js.map +1 -1
  26. package/dist/AgentImage.test.js +5 -1
  27. package/dist/AgentImage.test.js.map +1 -1
  28. package/dist/AgentOutput.d.ts +54 -2
  29. package/dist/AgentOutput.d.ts.map +1 -1
  30. package/dist/AgentOutput.js +52 -0
  31. package/dist/AgentOutput.js.map +1 -1
  32. package/dist/AgentSkills.d.ts +22 -0
  33. package/dist/AgentSkills.d.ts.map +1 -1
  34. package/dist/AgentSkills.js +36 -0
  35. package/dist/AgentSkills.js.map +1 -1
  36. package/dist/AgentSkills.test.js +2 -0
  37. package/dist/AgentSkills.test.js.map +1 -1
  38. package/dist/AgentSubagents.test.d.ts +2 -0
  39. package/dist/AgentSubagents.test.d.ts.map +1 -0
  40. package/dist/AgentSubagents.test.js +452 -0
  41. package/dist/AgentSubagents.test.js.map +1 -0
  42. package/dist/AgentTools.d.ts +356 -14
  43. package/dist/AgentTools.d.ts.map +1 -1
  44. package/dist/AgentTools.js +213 -34
  45. package/dist/AgentTools.js.map +1 -1
  46. package/dist/CompactionTransport.test.js +1 -1
  47. package/dist/CompactionTransport.test.js.map +1 -1
  48. package/dist/OutputFormatter.d.ts.map +1 -1
  49. package/dist/OutputFormatter.js +16 -2
  50. package/dist/OutputFormatter.js.map +1 -1
  51. package/dist/ToolkitRenderer.d.ts.map +1 -1
  52. package/dist/ToolkitRenderer.js +6 -1
  53. package/dist/ToolkitRenderer.js.map +1 -1
  54. package/dist/bin/{Effect-BEUn4I5E.mjs → Effect-2jPlJ8eu.mjs} +399 -207
  55. package/dist/bin/Effect-2jPlJ8eu.mjs.map +1 -0
  56. package/dist/bin/{McpClient-C0FVuDj2.mjs → McpClient-1W508adc.mjs} +4 -4
  57. package/dist/bin/{McpClient-C0FVuDj2.mjs.map → McpClient-1W508adc.mjs.map} +1 -1
  58. package/dist/bin/{OutputFormatter-DffD5oxX.mjs → OutputFormatter-DXXKo4E8.mjs} +16 -5
  59. package/dist/bin/{OutputFormatter-DffD5oxX.mjs.map → OutputFormatter-DXXKo4E8.mjs.map} +1 -1
  60. package/dist/bin/{Schema-CjUGw2Xh.mjs → Schema-D-7VuCiC.mjs} +640 -87
  61. package/dist/bin/Schema-D-7VuCiC.mjs.map +1 -0
  62. package/dist/bin/{SemanticSearch-CFB73IPb.mjs → SemanticSearch-Cb8aqvGv.mjs} +89 -56
  63. package/dist/bin/SemanticSearch-Cb8aqvGv.mjs.map +1 -0
  64. package/dist/bin/{Stream-Cu6P7ZHo.mjs → Stream-DEu2tfCV.mjs} +81 -51
  65. package/dist/bin/Stream-DEu2tfCV.mjs.map +1 -0
  66. package/dist/bin/cli.mjs +3632 -5538
  67. package/dist/bin/cli.mjs.map +1 -1
  68. package/dist/bin/{turndown.cjs-usmCKZ4b.mjs → turndown.cjs-DK29wZw3.mjs} +2 -2
  69. package/dist/bin/{turndown.cjs-usmCKZ4b.mjs.map → turndown.cjs-DK29wZw3.mjs.map} +1 -1
  70. package/package.json +7 -7
  71. package/src/Acp.test.ts +121 -2
  72. package/src/Acp.ts +10 -3
  73. package/src/AcpImage.test.ts +1 -0
  74. package/src/Agent.test.ts +100 -3
  75. package/src/Agent.ts +752 -142
  76. package/src/AgentCallerConfig.test.ts +514 -0
  77. package/src/AgentExecutor.test.ts +297 -37
  78. package/src/AgentExecutor.ts +707 -159
  79. package/src/AgentExecutorImage.test.ts +2 -1
  80. package/src/AgentImage.test.ts +11 -2
  81. package/src/AgentOutput.ts +74 -1
  82. package/src/AgentSkills.test.ts +2 -0
  83. package/src/AgentSkills.ts +43 -0
  84. package/src/AgentSubagents.test.ts +758 -0
  85. package/src/AgentTools.ts +376 -52
  86. package/src/CompactionTransport.test.ts +1 -1
  87. package/src/OutputFormatter.ts +16 -2
  88. package/src/ToolkitRenderer.ts +7 -1
  89. package/dist/bin/Effect-BEUn4I5E.mjs.map +0 -1
  90. package/dist/bin/Schema-CjUGw2Xh.mjs.map +0 -1
  91. package/dist/bin/SemanticSearch-CFB73IPb.mjs.map +0 -1
  92. package/dist/bin/Stream-Cu6P7ZHo.mjs.map +0 -1
@@ -5,15 +5,26 @@ import * as NodeConsole from "node:console"
5
5
  import * as NodeOs from "node:os"
6
6
  import * as NodeVm from "node:vm"
7
7
  import { Writable } from "node:stream"
8
+ import { Worker } from "node:worker_threads"
9
+ import type * as NodeWorkerThreads from "node:worker_threads"
8
10
  import {
9
11
  AgentToolHandlers,
12
+ type DelegateResult,
13
+ type SubagentInfo,
14
+ type SubagentResult,
10
15
  AgentTools,
11
16
  AgentToolsWithSearch,
17
+ CommandLimits,
12
18
  CurrentDirectory,
19
+ defaultBashTimeout,
13
20
  DirectoryChanger,
21
+ type ExcludableTool,
14
22
  ImageAttacher,
15
23
  type ImageAttachment,
24
+ makeBashTool,
16
25
  SubagentExecutor,
26
+ type Subagents,
27
+ subagentsUnavailable,
17
28
  TaskCompleter,
18
29
  } from "./AgentTools.ts"
19
30
  import * as Image from "./Image.ts"
@@ -22,6 +33,8 @@ import * as AgentSkills from "./AgentSkills.ts"
22
33
  import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"
23
34
  import type * as HttpClient from "effect/http/HttpClient"
24
35
  import * as Context from "effect/Context"
36
+ import * as Deferred from "effect/Deferred"
37
+ import * as Duration from "effect/Duration"
25
38
  import * as Effect from "effect/Effect"
26
39
  import * as Option from "effect/Option"
27
40
  import * as Stream from "effect/Stream"
@@ -55,15 +68,7 @@ export class AgentExecutor extends Context.Service<
55
68
  AgentExecutor,
56
69
  {
57
70
  readonly capabilities: Effect.Effect<Capabilities>
58
- execute(options: {
59
- readonly script: string
60
- readonly onTaskComplete: (summary: string) => Effect.Effect<void>
61
- readonly onSubagent: (message: string) => Effect.Effect<string>
62
- /**
63
- * Receives every image `readFile` attached while the script ran.
64
- */
65
- readonly onImage: (image: ImageAttachment) => Effect.Effect<void>
66
- }): Stream.Stream<string>
71
+ execute(options: ExecuteOptions): Stream.Stream<string>
67
72
  executeUnsafe<Tool extends keyof typeof AgentTools.tools>(options: {
68
73
  readonly tool: Tool
69
74
  readonly params: (typeof AgentTools.tools)[Tool]["parametersSchema"]["Encoded"]
@@ -71,6 +76,44 @@ export class AgentExecutor extends Context.Service<
71
76
  }
72
77
  >()("clanka/AgentExecutor") {}
73
78
 
79
+ /**
80
+ * @since 1.0.0
81
+ * @category Models
82
+ */
83
+ export interface ExecuteOptions {
84
+ readonly script: string
85
+ /**
86
+ * Fails with a message for the script when the task cannot be completed
87
+ * yet, for example while sub-agents are still running.
88
+ */
89
+ readonly onTaskComplete: (summary: string) => Effect.Effect<void, string>
90
+ /**
91
+ * The sub-agents of the agent running the script.
92
+ */
93
+ readonly subagents: Subagents
94
+ /**
95
+ * Receives every image `readFile` attached while the script ran.
96
+ */
97
+ readonly onImage: (image: ImageAttachment) => Effect.Effect<void>
98
+ /**
99
+ * Functions added to the script sandbox whose handlers run on the loop
100
+ * side, next to the Agent.
101
+ */
102
+ readonly loopTools?: LoopToolHandlers | undefined
103
+ }
104
+
105
+ /**
106
+ * Loop-side tool handlers by function name. A handler receives the script's
107
+ * argument undecoded; a failure rejects the script's call with the string as
108
+ * the error message.
109
+ *
110
+ * @since 1.0.0
111
+ * @category Models
112
+ */
113
+ export type LoopToolHandlers = Readonly<
114
+ Record<string, (params: unknown) => Effect.Effect<unknown, string>>
115
+ >
116
+
74
117
  /**
75
118
  * @since 1.0.0
76
119
  * @category Models
@@ -79,6 +122,10 @@ export class Capabilities extends Schema.Class<Capabilities>("Capabilities")({
79
122
  toolsDts: Schema.String,
80
123
  agentsMd: Schema.Option(Schema.String),
81
124
  supportsSearch: Schema.Boolean,
125
+ /**
126
+ * The names of the script functions the executor offers.
127
+ */
128
+ tools: Schema.Array(Schema.String),
82
129
  /**
83
130
  * Skills available on the executor filesystem.
84
131
  */
@@ -88,16 +135,67 @@ export class Capabilities extends Schema.Class<Capabilities>("Capabilities")({
88
135
  ),
89
136
  }) {}
90
137
 
138
+ /**
139
+ * Limits applied by the executor that runs the tools. With `layerRpcServer`
140
+ * they are set on the server, next to the commands they bound.
141
+ *
142
+ * @since 1.0.0
143
+ * @category Models
144
+ */
145
+ export interface Limits {
146
+ /**
147
+ * The longest a single `bash` or `gh` command may run. For `bash` it is
148
+ * also the default when the model passes no `timeoutMs`, and the `bash`
149
+ * description states it.
150
+ *
151
+ * Defaults to 10 minutes for `bash` and no limit for `gh`, as before.
152
+ */
153
+ readonly commandTimeout?: Duration.Input | undefined
154
+ /**
155
+ * The longest a single `execute` script may run, including the tool calls
156
+ * it awaits. A script that runs out of time is interrupted, and its output
157
+ * so far ends with a line saying it timed out.
158
+ *
159
+ * Defaults to no limit.
160
+ */
161
+ readonly scriptTimeout?: Duration.Input | undefined
162
+ /**
163
+ * The most characters of `bash` and `gh` output held in memory per command.
164
+ * Longer output keeps its start and end, with a marker in place of the
165
+ * middle.
166
+ *
167
+ * Defaults to no limit.
168
+ */
169
+ readonly commandOutputChars?: number | undefined
170
+ }
171
+
172
+ /**
173
+ * Options for an executor that runs scripts in this process.
174
+ *
175
+ * @since 1.0.0
176
+ * @category Models
177
+ */
178
+ export interface LocalOptions<Toolkit extends Toolkit.Any = never> {
179
+ readonly directory: string
180
+ readonly tools?: Toolkit | undefined
181
+ readonly limits?: Limits | undefined
182
+ /**
183
+ * Built-in tools to leave out of the sandbox and the type declarations.
184
+ * Leaving out `delegate` also leaves out the tools that address its
185
+ * sub-agents.
186
+ */
187
+ readonly excludeTools?: ReadonlyArray<ExcludableTool> | undefined
188
+ }
189
+
91
190
  /**
92
191
  * @since 1.0.0
93
192
  * @category Constructors
94
193
  */
95
194
  export const makeLocal = Effect.fnUntraced(function* <
96
195
  Toolkit extends Toolkit.Any = never,
97
- >(options: {
98
- readonly directory: string
99
- readonly tools?: Toolkit | undefined
100
- }): Effect.fn.Return<
196
+ >(
197
+ options: LocalOptions<Toolkit>,
198
+ ): Effect.fn.Return<
101
199
  AgentExecutor["Service"],
102
200
  never,
103
201
  | ToolkitRenderer
@@ -128,10 +226,33 @@ export const makeLocal = Effect.fnUntraced(function* <
128
226
  currentDirectory = target
129
227
  return target
130
228
  }, Effect.orDie)
229
+ const commandTimeout = options.limits?.commandTimeout
230
+ const commandLimits: typeof CommandLimits.Service = {
231
+ timeout:
232
+ commandTimeout === undefined
233
+ ? undefined
234
+ : Duration.fromInputUnsafe(commandTimeout),
235
+ outputChars: options.limits?.commandOutputChars,
236
+ }
237
+ const scriptTimeout = Option.map(
238
+ Option.fromUndefinedOr(options.limits?.scriptTimeout),
239
+ Duration.fromInputUnsafe,
240
+ )
241
+ const excluded = excludedToolNames(options.excludeTools)
131
242
  const search = yield* Effect.serviceOption(SemanticSearch)
132
- const hasSearch = Option.isSome(search)
243
+ const BuiltInTools = Toolkit.make(
244
+ ...Object.values(
245
+ Toolkit.merge(
246
+ Option.isSome(search) ? AgentToolsWithSearch : AgentTools,
247
+ // Same tool id, so the handler is unchanged. Only the description,
248
+ // which states the timeout, differs.
249
+ Toolkit.make(makeBashTool(commandLimits.timeout ?? defaultBashTimeout)),
250
+ ).tools as Record<string, Tool.Any>,
251
+ ).filter((tool) => !excluded.has(tool.name)),
252
+ ) as unknown as typeof AgentToolsWithSearch
253
+ const hasSearch = "search" in BuiltInTools.tools
133
254
  const AllTools = Toolkit.merge(
134
- hasSearch ? AgentToolsWithSearch : AgentTools,
255
+ BuiltInTools,
135
256
  (options.tools as unknown as Toolkit.Toolkit<{}>) ?? Toolkit.empty,
136
257
  )
137
258
  const tools = yield* AllTools
@@ -139,25 +260,25 @@ export const makeLocal = Effect.fnUntraced(function* <
139
260
 
140
261
  const services = yield* Effect.context()
141
262
 
142
- const toolEntries = Object.entries(tools.tools).map(([name, tool]) => {
143
- const handler = services.mapUnsafe.get(tool.id) as Tool.Handler<string>
144
- return {
145
- name,
146
- services: Context.merge(services, handler.context),
147
- handler: handler.handler,
148
- }
149
- })
263
+ const toolEntries = new Map(
264
+ Object.entries(tools.tools).map(([name, tool]) => {
265
+ const handler = services.mapUnsafe.get(tool.id) as Tool.Handler<string>
266
+ return [
267
+ name,
268
+ {
269
+ services: Context.merge(services, handler.context),
270
+ handler: handler.handler,
271
+ },
272
+ ] as const
273
+ }),
274
+ )
275
+ const toolNames = Array.from(toolEntries.keys())
150
276
 
151
277
  if (typeof process !== "undefined") {
152
278
  process.on("uncaughtException", () => {})
153
279
  }
154
280
 
155
- const execute = Effect.fnUntraced(function* (opts: {
156
- readonly script: string
157
- readonly onTaskComplete: (summary: string) => Effect.Effect<void>
158
- readonly onSubagent: (message: string) => Effect.Effect<string>
159
- readonly onImage: (image: ImageAttachment) => Effect.Effect<void>
160
- }) {
281
+ const execute = Effect.fnUntraced(function* (opts: ExecuteOptions) {
161
282
  const output = yield* Queue.unbounded<string, Cause.Done>()
162
283
  const console = yield* makeConsole(output)
163
284
  const handlerScope = Scope.makeUnsafe("parallel")
@@ -166,8 +287,9 @@ export const makeLocal = Effect.fnUntraced(function* <
166
287
  const taskServices = Context.empty().pipe(
167
288
  Context.add(TaskCompleter, opts.onTaskComplete),
168
289
  Context.add(DirectoryChanger, changeDirectory),
169
- Context.add(SubagentExecutor, opts.onSubagent),
290
+ Context.add(SubagentExecutor, opts.subagents),
170
291
  Context.add(ImageAttacher, opts.onImage),
292
+ Context.add(CommandLimits, commandLimits),
171
293
  Context.add(Console.Console, console),
172
294
  Context.add(References.CurrentLogAnnotations, {}),
173
295
  Option.isSome(search)
@@ -175,51 +297,115 @@ export const makeLocal = Effect.fnUntraced(function* <
175
297
  : identity,
176
298
  )
177
299
 
300
+ // The script runs in its own worker thread, so a script that is busy in
301
+ // JavaScript can still be stopped. Tool calls are sent back here and run
302
+ // as before.
178
303
  yield* Effect.gen(function* () {
179
- const console = yield* Console.Console
180
- let running = 0
181
-
182
- const vmScript = compileScript(opts.script)
183
- const sandbox: ScriptSandbox = {
184
- main: defaultMain,
185
- console,
186
- fetch,
187
- process: undefined,
188
- }
304
+ const source = prepareScript(opts.script)
305
+ const settled = yield* Deferred.make<void>()
306
+ const worker = yield* Effect.acquireRelease(
307
+ Effect.sync(
308
+ () =>
309
+ new Worker(workerSource, {
310
+ eval: true,
311
+ workerData: {
312
+ source,
313
+ tools: [...toolNames, ...Object.keys(opts.loopTools ?? {})],
314
+ } satisfies WorkerData,
315
+ }),
316
+ ),
317
+ (worker) => Effect.promise(() => worker.terminate()),
318
+ )
189
319
 
190
- for (let i = 0; i < toolEntries.length; i++) {
191
- const { name, handler, services } = toolEntries[i]!
192
- const toolServices = Context.merge(services, taskServices)
193
-
194
- // oxlint-disable-next-line typescript/no-explicit-any
195
- sandbox[name] = function (params: any) {
196
- const runFork = Effect.runForkWith(
197
- Context.add(toolServices, CurrentDirectory, currentDirectory),
198
- )
199
- running++
200
- const fiber = trackFiber(runFork(handler(params, {})))
201
- return new Promise((resolve, reject) => {
202
- fiber.addObserver((exit) => {
203
- running--
204
- if (exit._tag === "Success") {
205
- return resolve(exit.value)
206
- }
207
- if (Cause.hasInterruptsOnly(exit.cause)) return
208
- reject(Cause.squash(exit.cause))
209
- })
210
- })
320
+ // Every call is answered, interrupted ones included: the worker only
321
+ // reports Done once nothing is outstanding. While the script is being
322
+ // stopped the worker is already gone and the answer is dropped.
323
+ const respond = (id: number, exit: Exit.Exit<unknown, unknown>) => {
324
+ const message: ToWorker = Exit.isSuccess(exit)
325
+ ? { id, ok: true, value: exit.value }
326
+ : { id, ok: false, failure: toFailure(Cause.squash(exit.cause)) }
327
+ try {
328
+ // oxlint-disable-next-line unicorn/require-post-message-target-origin
329
+ worker.postMessage(message)
330
+ } catch (error) {
331
+ // The value cannot be copied to the worker.
332
+ // oxlint-disable-next-line unicorn/require-post-message-target-origin
333
+ worker.postMessage({ id, ok: false, failure: toFailure(error) })
211
334
  }
212
335
  }
213
336
 
214
- vmScript.runInNewContext(sandbox, {
215
- timeout: 1000,
216
- })
217
- yield* Effect.promise(sandbox.main)
218
- while (true) {
219
- yield* Effect.yieldNow
220
- if (running === 0) break
337
+ const call = (message: Extract<FromWorker, { _tag: "Call" }>) => {
338
+ const entry = toolEntries.get(message.name)
339
+ const runFork = Effect.runForkWith(
340
+ Context.add(
341
+ entry ? Context.merge(entry.services, taskServices) : taskServices,
342
+ CurrentDirectory,
343
+ currentDirectory,
344
+ ),
345
+ )
346
+ const fiber = trackFiber(
347
+ runFork(
348
+ entry
349
+ ? entry.handler(message.params, {})
350
+ : // A loop-side tool: its failure is the script's error message.
351
+ opts.loopTools![message.name]!(message.params).pipe(
352
+ Effect.catch((message) => Effect.die(new Error(message))),
353
+ ),
354
+ ),
355
+ )
356
+ fiber.addObserver((exit) => respond(message.id, exit))
221
357
  }
358
+
359
+ worker.on("message", (message: FromWorker) => {
360
+ switch (message._tag) {
361
+ case "Output":
362
+ Queue.offerUnsafe(output, message.text)
363
+ break
364
+ case "Call":
365
+ call(message)
366
+ break
367
+ case "Done":
368
+ Deferred.doneUnsafe(settled, Effect.void)
369
+ break
370
+ case "Failed":
371
+ Deferred.doneUnsafe(
372
+ settled,
373
+ Effect.die(fromFailure(message.failure)),
374
+ )
375
+ break
376
+ }
377
+ })
378
+ // Bridge failures end the script instead of leaving it pending.
379
+ worker.on("error", (error) => {
380
+ Deferred.doneUnsafe(settled, Effect.die(error))
381
+ })
382
+ worker.on("messageerror", (error) => {
383
+ Deferred.doneUnsafe(settled, Effect.die(error))
384
+ })
385
+ worker.on("exit", (code) => {
386
+ Deferred.doneUnsafe(
387
+ settled,
388
+ Effect.die(
389
+ new Error(`The script worker exited with code ${code} early`),
390
+ ),
391
+ )
392
+ })
393
+
394
+ yield* Deferred.await(settled)
222
395
  }).pipe(
396
+ // Stops the worker on every exit: done, failed, timed out, interrupted.
397
+ Effect.scoped,
398
+ Option.isSome(scriptTimeout)
399
+ ? Effect.timeoutOrElse({
400
+ duration: scriptTimeout.value,
401
+ orElse: () =>
402
+ Queue.offer(
403
+ output,
404
+ `\nScript timed out: it reached its ${Duration.toMillis(scriptTimeout.value)}ms limit and was stopped. Pending tool calls were cancelled.\n`,
405
+ ),
406
+ })
407
+ : identity,
408
+ // Interrupts the tool calls still running, and the processes they own.
223
409
  Effect.ensuring(Scope.close(handlerScope, Exit.void)),
224
410
  Effect.catchCause(Effect.logFatal),
225
411
  Effect.provideService(Console.Console, console),
@@ -235,18 +421,23 @@ export const makeLocal = Effect.fnUntraced(function* <
235
421
  const agentsMd = yield* Effect.option(
236
422
  fs.readFileString(pathService.join(options.directory, "AGENTS.md")),
237
423
  )
238
- const skills = yield* AgentSkills.discover({
239
- directory: options.directory,
240
- homeDirectory: homeDirectory(),
241
- hermesHome: Option.fromNullishOr(process.env.HERMES_HOME),
242
- }).pipe(
243
- Effect.provideService(FileSystem.FileSystem, fs),
244
- Effect.provideService(Path.Path, pathService),
245
- )
424
+ // Skills are read with readFile, so there are none without it.
425
+ const skills =
426
+ "readFile" in tools.tools
427
+ ? yield* AgentSkills.discover({
428
+ directory: options.directory,
429
+ homeDirectory: homeDirectory(),
430
+ hermesHome: Option.fromNullishOr(process.env.HERMES_HOME),
431
+ }).pipe(
432
+ Effect.provideService(FileSystem.FileSystem, fs),
433
+ Effect.provideService(Path.Path, pathService),
434
+ )
435
+ : []
246
436
  return new Capabilities({
247
437
  toolsDts,
248
438
  agentsMd,
249
439
  supportsSearch: hasSearch,
440
+ tools: Object.keys(tools.tools),
250
441
  skills,
251
442
  })
252
443
  }),
@@ -266,9 +457,10 @@ export const makeLocal = Effect.fnUntraced(function* <
266
457
  Context.add(TaskCompleter, () => Effect.void),
267
458
  Context.add(CurrentDirectory, currentDirectory),
268
459
  Context.add(DirectoryChanger, changeDirectory),
269
- Context.add(SubagentExecutor, () => Effect.succeed("")),
460
+ Context.add(SubagentExecutor, subagentsUnavailable),
270
461
  // Direct tool calls have no model turn to attach images to.
271
462
  Context.add(ImageAttacher, () => Effect.void),
463
+ Context.add(CommandLimits, commandLimits),
272
464
  Context.add(References.CurrentLogAnnotations, {}),
273
465
  Option.isSome(search)
274
466
  ? Context.add(SemanticSearch, search.value)
@@ -301,39 +493,44 @@ export const makeRpc = Effect.gen(function* () {
301
493
  capabilities: Effect.orDie(client.capabilities()),
302
494
  execute: (opts) =>
303
495
  Scope.Scope.useSync((scope) =>
304
- client.execute({ script: opts.script }).pipe(
305
- Stream.tap((part) => {
306
- switch (part._tag) {
307
- case "Text": {
308
- return Effect.void
309
- }
310
- case "TaskComplete": {
311
- return opts.onTaskComplete(part.summary)
312
- }
313
- case "Subagent": {
314
- const id = part.id
315
- return pipe(
316
- opts.onSubagent(part.prompt),
317
- Effect.flatMap((output) =>
318
- client.subagentOutput({ id, output }),
319
- ),
320
- Effect.forkIn(scope),
321
- )
322
- }
323
- case "Image": {
324
- return opts.onImage({
325
- fileName: part.fileName,
326
- mediaType: part.mediaType,
327
- data: part.data,
328
- })
496
+ client
497
+ .execute({
498
+ script: opts.script,
499
+ loopTools: Object.keys(opts.loopTools ?? {}),
500
+ })
501
+ .pipe(
502
+ Stream.tap((part) => {
503
+ switch (part._tag) {
504
+ case "Text": {
505
+ return Effect.void
506
+ }
507
+ case "Request": {
508
+ const id = part.id
509
+ return pipe(
510
+ handleRequest(opts, part.request),
511
+ Effect.exit,
512
+ Effect.flatMap((exit) =>
513
+ client.respond({ id, response: exitToResponse(exit) }),
514
+ ),
515
+ Effect.forkIn(scope),
516
+ )
517
+ }
518
+ case "Image": {
519
+ return opts.onImage({
520
+ fileName: part.fileName,
521
+ mediaType: part.mediaType,
522
+ data: part.data,
523
+ })
524
+ }
329
525
  }
330
- }
331
- }),
332
- Stream.orDie,
333
- Stream.filterMap((part) =>
334
- part._tag === "Text" ? Result.succeed(part.text) : Result.failVoid,
526
+ }),
527
+ Stream.orDie,
528
+ Stream.filterMap((part) =>
529
+ part._tag === "Text"
530
+ ? Result.succeed(part.text)
531
+ : Result.failVoid,
532
+ ),
335
533
  ),
336
- ),
337
534
  ).pipe(Stream.unwrap),
338
535
  executeUnsafe(options) {
339
536
  return client.executeUnsafe(options).pipe(Effect.orDie)
@@ -345,10 +542,9 @@ export const makeRpc = Effect.gen(function* () {
345
542
  * @since 1.0.0
346
543
  * @category Layers
347
544
  */
348
- export const layerLocal = <Toolkit extends Toolkit.Any = never>(options: {
349
- readonly directory: string
350
- readonly tools?: Toolkit | undefined
351
- }): Layer.Layer<
545
+ export const layerLocal = <Toolkit extends Toolkit.Any = never>(
546
+ options: LocalOptions<Toolkit>,
547
+ ): Layer.Layer<
352
548
  AgentExecutor,
353
549
  never,
354
550
  | FileSystem.FileSystem
@@ -366,8 +562,8 @@ export const layerLocal = <Toolkit extends Toolkit.Any = never>(options: {
366
562
  | ImageAttacher
367
563
  >
368
564
  > =>
369
- Layer.effect(AgentExecutor, makeLocal(options)).pipe(
370
- Layer.provide([AgentToolHandlers, ToolkitRenderer.layer]),
565
+ Layer.effect(AgentExecutor, withBuiltInHandlers(makeLocal(options))).pipe(
566
+ Layer.provide(ToolkitRenderer.layer),
371
567
  )
372
568
 
373
569
  /**
@@ -388,10 +584,9 @@ export const layerRpc: Layer.Layer<AgentExecutor, never, RpcClient.Protocol> =
388
584
  * @since 1.0.0
389
585
  * @category Layers
390
586
  */
391
- export const layerRpcServer = <Toolkit extends Toolkit.Any = never>(options: {
392
- readonly directory: string
393
- readonly tools?: Toolkit | undefined
394
- }): Layer.Layer<
587
+ export const layerRpcServer = <Toolkit extends Toolkit.Any = never>(
588
+ options: LocalOptions<Toolkit>,
589
+ ): Layer.Layer<
395
590
  never,
396
591
  never,
397
592
  | RpcServer.Protocol
@@ -417,50 +612,78 @@ export const layerRpcServer = <Toolkit extends Toolkit.Any = never>(options: {
417
612
  Layer.provide(
418
613
  Rpcs.toLayer(
419
614
  Effect.gen(function* () {
420
- const local = yield* makeLocal(options)
421
- const subagentResumes = new Map<
615
+ const local = yield* withBuiltInHandlers(makeLocal(options))
616
+ const requestResumes = new Map<
422
617
  number,
423
- (effect: Effect.Effect<string>) => void
618
+ (effect: Effect.Effect<unknown, string>) => void
424
619
  >()
425
- let subagentId = 0
620
+ let requestId = 0
426
621
 
427
622
  return Rpcs.of({
428
623
  capabilities: () => local.capabilities,
429
- subagentOutput: ({ id, output }) => {
430
- const resume = subagentResumes.get(id)
624
+ respond: ({ id, response }) => {
625
+ const resume = requestResumes.get(id)
431
626
  if (resume) {
432
- resume(Effect.succeed(output))
433
- subagentResumes.delete(id)
627
+ requestResumes.delete(id)
628
+ resume(
629
+ response._tag === "Success"
630
+ ? Effect.succeed(response.value)
631
+ : Effect.fail(response.message),
632
+ )
434
633
  }
435
634
  return Effect.void
436
635
  },
437
- execute: Effect.fnUntraced(function* ({ script }) {
636
+ execute: Effect.fnUntraced(function* ({ script, loopTools }) {
438
637
  const queue = yield* Queue.unbounded<
439
638
  typeof ExecuteOutput.Type,
440
639
  Cause.Done
441
640
  >()
641
+ // Sends a request to the client and waits for its response.
642
+ // IDs come from a server-wide counter, so concurrent and nested
643
+ // execute streams never collide.
644
+ // The client answers with the value of the matching callback.
645
+ const request = <A>(request: typeof AgentRequest.Type) =>
646
+ Effect.callback<A, string>((resume) => {
647
+ const id = requestId++
648
+ requestResumes.set(
649
+ id,
650
+ resume as (effect: Effect.Effect<unknown, string>) => void,
651
+ )
652
+ Queue.offerUnsafe(queue, { _tag: "Request", id, request })
653
+ return Effect.sync(() => {
654
+ requestResumes.delete(id)
655
+ })
656
+ })
442
657
  yield* pipe(
443
658
  local.execute({
444
659
  script,
445
- onTaskComplete(summary) {
446
- return Queue.offer(queue, {
447
- _tag: "TaskComplete",
448
- summary,
449
- })
450
- },
451
- onSubagent(prompt) {
452
- const id = subagentId++
453
- return Effect.callback((resume) => {
454
- subagentResumes.set(id, resume)
455
- Queue.offerUnsafe(queue, {
456
- _tag: "Subagent",
457
- id,
458
- prompt,
459
- })
460
- return Effect.sync(() => {
461
- subagentResumes.delete(id)
462
- })
463
- })
660
+ onTaskComplete: (summary) =>
661
+ request<void>({ _tag: "TaskComplete", summary }),
662
+ subagents: {
663
+ delegate: (options) =>
664
+ Effect.orDie(
665
+ request<DelegateResult>({
666
+ _tag: "Delegate",
667
+ ...options,
668
+ }),
669
+ ),
670
+ waitAgents: (ids) =>
671
+ request<ReadonlyArray<SubagentResult>>({
672
+ _tag: "WaitAgents",
673
+ ids,
674
+ }),
675
+ messageAgent: (options) =>
676
+ request<SubagentResult>({
677
+ _tag: "MessageAgent",
678
+ ...options,
679
+ }),
680
+ stopAgent: (id) =>
681
+ request<SubagentResult>({ _tag: "StopAgent", id }),
682
+ listAgents: Effect.orDie(
683
+ request<ReadonlyArray<SubagentInfo>>({
684
+ _tag: "ListAgents",
685
+ }),
686
+ ),
464
687
  },
465
688
  onImage(image) {
466
689
  return Queue.offer(queue, {
@@ -470,6 +693,21 @@ export const layerRpcServer = <Toolkit extends Toolkit.Any = never>(options: {
470
693
  data: image.data,
471
694
  })
472
695
  },
696
+ loopTools: Object.fromEntries(
697
+ loopTools.map((name) => [
698
+ name,
699
+ (params: unknown) =>
700
+ toJson(name, params).pipe(
701
+ Effect.flatMap((params) =>
702
+ request<unknown>({
703
+ _tag: "LoopTool",
704
+ name,
705
+ params,
706
+ }),
707
+ ),
708
+ ),
709
+ ]),
710
+ ),
473
711
  }),
474
712
  Stream.runForEachArray((parts) => {
475
713
  for (const part of parts) {
@@ -497,17 +735,42 @@ export const layerRpcServer = <Toolkit extends Toolkit.Any = never>(options: {
497
735
  }),
498
736
  ),
499
737
  ),
500
- Layer.provide([AgentToolHandlers, ToolkitRenderer.layer]),
738
+ Layer.provide(ToolkitRenderer.layer),
501
739
  )
502
740
 
741
+ /**
742
+ * @since 1.0.0
743
+ * @category Rpcs
744
+ */
745
+ export const AgentRequest = Schema.TaggedUnion({
746
+ TaskComplete: { summary: Schema.String },
747
+ Delegate: { prompt: Schema.String, background: Schema.Boolean },
748
+ WaitAgents: { ids: Schema.UndefinedOr(Schema.Array(Schema.String)) },
749
+ MessageAgent: { id: Schema.String, message: Schema.String },
750
+ StopAgent: { id: Schema.String },
751
+ ListAgents: {},
752
+ LoopTool: { name: Schema.String, params: Schema.optional(Schema.Json) },
753
+ })
754
+
755
+ /**
756
+ * The client's answer to a `Request`. `Failure` carries a message for the
757
+ * script; a failed foreground `delegate` is raised there as a defect.
758
+ *
759
+ * @since 1.0.0
760
+ * @category Rpcs
761
+ */
762
+ export const AgentResponse = Schema.TaggedUnion({
763
+ Success: { value: Schema.Json },
764
+ Failure: { message: Schema.String },
765
+ })
766
+
503
767
  /**
504
768
  * @since 1.0.0
505
769
  * @category Rpcs
506
770
  */
507
771
  export const ExecuteOutput = Schema.TaggedUnion({
508
772
  Text: { text: Schema.String },
509
- TaskComplete: { summary: Schema.String },
510
- Subagent: { id: Schema.Finite, prompt: Schema.String },
773
+ Request: { id: Schema.Finite, request: AgentRequest },
511
774
  Image: {
512
775
  fileName: Schema.String,
513
776
  mediaType: Image.ImageMediaType,
@@ -523,15 +786,21 @@ export class Rpcs extends RpcGroup.make(
523
786
  Rpc.make("capabilities", {
524
787
  success: Capabilities,
525
788
  }),
526
- Rpc.make("subagentOutput", {
789
+ Rpc.make("respond", {
527
790
  payload: {
528
791
  id: Schema.Finite,
529
- output: Schema.String,
792
+ response: AgentResponse,
530
793
  },
531
794
  }),
532
795
  Rpc.make("execute", {
533
796
  payload: Schema.Struct({
534
797
  script: Schema.String,
798
+ /**
799
+ * The loop-side tools to add to the script sandbox.
800
+ */
801
+ loopTools: Schema.Array(Schema.String).pipe(
802
+ Schema.withDecodingDefaultKey(Effect.succeed([])),
803
+ ),
535
804
  }),
536
805
  success: ExecuteOutput,
537
806
  stream: true,
@@ -549,19 +818,116 @@ export class Rpcs extends RpcGroup.make(
549
818
  // Internal
550
819
  // -------------------------------------------
551
820
 
552
- interface ScriptSandbox {
553
- main: () => Promise<void>
554
- console: Console.Console
555
- [toolName: string]: unknown
821
+ const handleRequest = (
822
+ opts: ExecuteOptions,
823
+ request: typeof AgentRequest.Type,
824
+ ): Effect.Effect<unknown, string> => {
825
+ switch (request._tag) {
826
+ case "TaskComplete":
827
+ return opts.onTaskComplete(request.summary)
828
+ case "Delegate":
829
+ return opts.subagents.delegate(request)
830
+ case "WaitAgents":
831
+ return opts.subagents.waitAgents(request.ids)
832
+ case "MessageAgent":
833
+ return opts.subagents.messageAgent(request)
834
+ case "StopAgent":
835
+ return opts.subagents.stopAgent(request.id)
836
+ case "ListAgents":
837
+ return opts.subagents.listAgents
838
+ case "LoopTool": {
839
+ const handler = opts.loopTools?.[request.name]
840
+ return handler
841
+ ? handler(request.params)
842
+ : Effect.fail(`Unknown loop-side tool: ${request.name}`)
843
+ }
844
+ }
845
+ }
846
+
847
+ // Script arguments cross the RPC boundary as JSON.
848
+ const toJson = (
849
+ name: string,
850
+ params: unknown,
851
+ ): Effect.Effect<Schema.Json | undefined, string> =>
852
+ Effect.try({
853
+ try: () => {
854
+ const json = JSON.stringify(params)
855
+ return json === undefined ? undefined : (JSON.parse(json) as Schema.Json)
856
+ },
857
+ catch: (error) => `Invalid arguments for ${name}: ${errorMessage(error)}`,
858
+ })
859
+
860
+ // The built-in handlers sit underneath the caller's context, so a caller tool
861
+ // named like a built-in keeps its own handler.
862
+ const withBuiltInHandlers = <A, E, R>(
863
+ effect: Effect.Effect<A, E, R>,
864
+ ): Effect.Effect<
865
+ A,
866
+ E,
867
+ | Exclude<R, Tool.HandlersFor<typeof AgentToolsWithSearch.tools>>
868
+ | Scope.Scope
869
+ | FileSystem.FileSystem
870
+ | Path.Path
871
+ | ChildProcessSpawner.ChildProcessSpawner
872
+ | HttpClient.HttpClient
873
+ > =>
874
+ Effect.flatMap(Layer.build(AgentToolHandlers), (builtIns) =>
875
+ Effect.updateContext(
876
+ effect,
877
+ (context: Context.Context<never>) =>
878
+ Context.merge(builtIns, context) as Context.Context<R>,
879
+ ),
880
+ ) as any
881
+
882
+ const exitToResponse = (
883
+ exit: Exit.Exit<unknown, string>,
884
+ ): typeof AgentResponse.Type => {
885
+ if (Exit.isSuccess(exit)) {
886
+ return { _tag: "Success", value: (exit.value ?? null) as Schema.Json }
887
+ }
888
+ return { _tag: "Failure", message: errorMessage(Cause.squash(exit.cause)) }
889
+ }
890
+
891
+ const errorMessage = (error: unknown): string =>
892
+ typeof error === "string"
893
+ ? error
894
+ : error instanceof Error
895
+ ? error.message
896
+ : String(error)
897
+
898
+ // Leaving out `delegate` leaves out the tools that address its sub-agents.
899
+ const excludedToolNames = (
900
+ excludeTools: ReadonlyArray<ExcludableTool> | undefined,
901
+ ): ReadonlySet<string> => {
902
+ const names = new Set<string>(excludeTools)
903
+ if (names.has("delegate")) {
904
+ for (const name of [
905
+ "waitAgents",
906
+ "messageAgent",
907
+ "stopAgent",
908
+ "listAgents",
909
+ ]) {
910
+ names.add(name)
911
+ }
912
+ }
913
+ return names
556
914
  }
557
915
 
558
916
  const wrapScript = (script: string): string => `async function main() {
559
917
  ${script}
560
918
  }`
561
919
 
562
- const compileScript = (script: string): NodeVm.Script => {
920
+ /**
921
+ * Returns source that compiles, preprocessing the script if it has syntax
922
+ * errors. Throws the original syntax error if neither version compiles.
923
+ */
924
+ const prepareScript = (script: string): string => {
925
+ const source = wrapScript(script)
563
926
  try {
564
- return new NodeVm.Script(wrapScript(script))
927
+ // Compiled only to check the syntax. The worker compiles it again.
928
+ // oxlint-disable-next-line eslint/no-new
929
+ new NodeVm.Script(source)
930
+ return source
565
931
  } catch (error) {
566
932
  if (!(error instanceof SyntaxError)) {
567
933
  throw error
@@ -572,15 +938,197 @@ const compileScript = (script: string): NodeVm.Script => {
572
938
  throw error
573
939
  }
574
940
 
941
+ const preprocessedSource = wrapScript(preprocessed)
575
942
  try {
576
- return new NodeVm.Script(wrapScript(preprocessed))
943
+ // oxlint-disable-next-line eslint/no-new
944
+ new NodeVm.Script(preprocessedSource)
945
+ return preprocessedSource
577
946
  } catch {
578
947
  throw error
579
948
  }
580
949
  }
581
950
  }
582
951
 
583
- const defaultMain = () => Promise.resolve()
952
+ interface WorkerData {
953
+ readonly source: string
954
+ readonly tools: ReadonlyArray<string>
955
+ }
956
+
957
+ /**
958
+ * A rejection crossing the worker boundary. Errors keep their name, message
959
+ * and stack. Other values are copied, or turned into a string when they
960
+ * cannot be.
961
+ */
962
+ type Failure =
963
+ | {
964
+ readonly _tag: "Error"
965
+ readonly name: string
966
+ readonly message: string
967
+ readonly stack: string | undefined
968
+ }
969
+ | { readonly _tag: "Value"; readonly value: unknown }
970
+
971
+ type FromWorker =
972
+ | { readonly _tag: "Output"; readonly text: string }
973
+ | {
974
+ readonly _tag: "Call"
975
+ readonly id: number
976
+ readonly name: string
977
+ readonly params: unknown
978
+ }
979
+ | { readonly _tag: "Done" }
980
+ | { readonly _tag: "Failed"; readonly failure: Failure }
981
+
982
+ type ToWorker =
983
+ | { readonly id: number; readonly ok: true; readonly value: unknown }
984
+ | { readonly id: number; readonly ok: false; readonly failure: Failure }
985
+
986
+ // -------------------------------------------
987
+ // Script worker
988
+ //
989
+ // The worker runs from source text (`new Worker(source, { eval: true })`), so
990
+ // it needs no separate file in the package, the CLI bundle or a consumer's
991
+ // bundle. The source is built from the function declarations below with
992
+ // `Function.prototype.toString`, which keeps them type-checked and shared
993
+ // with the parent instead of copied into a string.
994
+ //
995
+ // Constraints, because the text of each function must run on its own:
996
+ // - Each must be a `function` declaration, so its text carries its name.
997
+ // - They may reference only each other, their parameters and globals: no
998
+ // imports, module-level values or closures.
999
+ // - Node modules are loaded with the `load` parameter, which the source text
1000
+ // binds to the worker's `require`. Bundlers rewrite a bare `require` (the
1001
+ // CLI bundle turns it into `__require`), but leave a parameter alone.
1002
+ // - A bundler must keep their bodies whole. Renaming identifiers
1003
+ // consistently is fine. Transforms that move code out of a body, such as
1004
+ // coverage instrumentation or helper hoisting, are not. Only the
1005
+ // library build and the CLI bundle are checked.
1006
+ // The executor tests run every script through this worker, so they catch a
1007
+ // broken constraint in the source. They run the source, not a bundle, so a
1008
+ // bundler's rewrites only show up when the built output is run.
1009
+ // -------------------------------------------
1010
+
1011
+ function toFailure(value: unknown): Failure {
1012
+ if (Object.prototype.toString.call(value) === "[object Error]") {
1013
+ const error = value as Error
1014
+ return {
1015
+ _tag: "Error",
1016
+ name: error.name,
1017
+ message: error.message,
1018
+ stack: error.stack,
1019
+ }
1020
+ }
1021
+ try {
1022
+ return { _tag: "Value", value: structuredClone(value) }
1023
+ } catch {
1024
+ return { _tag: "Value", value: String(value) }
1025
+ }
1026
+ }
1027
+
1028
+ function fromFailure(failure: Failure): unknown {
1029
+ if (failure._tag === "Value") return failure.value
1030
+ const error = new Error(failure.message)
1031
+ error.name = failure.name
1032
+ if (failure.stack !== undefined) error.stack = failure.stack
1033
+ return error
1034
+ }
1035
+
1036
+ /**
1037
+ * Runs one script inside a vm context with the same globals as before, and
1038
+ * forwards console output and tool calls to the parent.
1039
+ */
1040
+ function runScriptWorker(load: (id: string) => unknown): void {
1041
+ // Type-only references: the casts are erased, so the text stays
1042
+ // self-contained.
1043
+ const { parentPort, workerData } = load(
1044
+ "node:worker_threads",
1045
+ ) as typeof NodeWorkerThreads
1046
+ const vm = load("node:vm") as typeof NodeVm
1047
+ const { Console } = load("node:console") as typeof NodeConsole
1048
+ const { Writable: WorkerWritable } = load("node:stream") as {
1049
+ readonly Writable: typeof Writable
1050
+ }
1051
+ const port = parentPort!
1052
+ const data = workerData as WorkerData
1053
+
1054
+ // Like the parent process: a stray rejection must not end the script.
1055
+ process.on("uncaughtException", () => {})
1056
+ process.on("unhandledRejection", () => {})
1057
+
1058
+ const post = (message: FromWorker) => port.postMessage(message)
1059
+
1060
+ const pending = new Map<
1061
+ number,
1062
+ {
1063
+ readonly resolve: (value: unknown) => void
1064
+ readonly reject: (error: unknown) => void
1065
+ }
1066
+ >()
1067
+ let nextId = 0
1068
+ let mainSettled = false
1069
+ let done = false
1070
+ // Done once main() has settled and no tool call is outstanding. The check
1071
+ // runs after the microtasks a result triggers, so a continuation that
1072
+ // calls another tool is waited for too. Done is sent once.
1073
+ const maybeDone = () => {
1074
+ if (done || !mainSettled || pending.size > 0) return
1075
+ setImmediate(() => {
1076
+ if (done || pending.size > 0) return
1077
+ done = true
1078
+ post({ _tag: "Done" })
1079
+ })
1080
+ }
1081
+ port.on("message", (message: ToWorker) => {
1082
+ const call = pending.get(message.id)
1083
+ if (call === undefined) return
1084
+ pending.delete(message.id)
1085
+ if (message.ok) call.resolve(message.value)
1086
+ else call.reject(fromFailure(message.failure))
1087
+ maybeDone()
1088
+ })
1089
+
1090
+ const stdout = new WorkerWritable({
1091
+ write(chunk, _encoding, callback) {
1092
+ post({ _tag: "Output", text: String(chunk) })
1093
+ callback()
1094
+ },
1095
+ })
1096
+
1097
+ const sandbox: Record<string, unknown> = {
1098
+ console: new Console({ stdout, colorMode: false }),
1099
+ fetch,
1100
+ process: undefined,
1101
+ }
1102
+ for (const name of data.tools) {
1103
+ sandbox[name] = (params: unknown) =>
1104
+ new Promise((resolve, reject) => {
1105
+ const id = nextId++
1106
+ pending.set(id, { resolve, reject })
1107
+ try {
1108
+ post({ _tag: "Call", id, name, params })
1109
+ } catch (error) {
1110
+ // The params cannot be copied to the parent.
1111
+ pending.delete(id)
1112
+ reject(error)
1113
+ maybeDone()
1114
+ }
1115
+ })
1116
+ }
1117
+
1118
+ new vm.Script(data.source).runInNewContext(sandbox)
1119
+ ;(sandbox.main as () => Promise<void>)().then(
1120
+ () => {
1121
+ mainSettled = true
1122
+ maybeDone()
1123
+ },
1124
+ (error) => post({ _tag: "Failed", failure: toFailure(error) }),
1125
+ )
1126
+ }
1127
+
1128
+ const workerSource = `${toFailure}
1129
+ ${fromFailure}
1130
+ (${runScriptWorker})(require)
1131
+ `
584
1132
 
585
1133
  const homeDirectory = (): Option.Option<string> => {
586
1134
  const fromEnv = process.env.HOME