@animalabs/agent-framework 0.23.1 → 0.24.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 (338) hide show
  1. package/README.md +212 -42
  2. package/dist/src/agent.d.ts +60 -9
  3. package/dist/src/agent.d.ts.map +1 -1
  4. package/dist/src/agent.js +92 -12
  5. package/dist/src/agent.js.map +1 -1
  6. package/dist/src/api/server.d.ts.map +1 -1
  7. package/dist/src/api/server.js +14 -12
  8. package/dist/src/api/server.js.map +1 -1
  9. package/dist/src/code-execution/engine.d.ts +177 -0
  10. package/dist/src/code-execution/engine.d.ts.map +1 -0
  11. package/dist/src/code-execution/engine.js +494 -0
  12. package/dist/src/code-execution/engine.js.map +1 -0
  13. package/dist/src/code-execution/index.d.ts +5 -10
  14. package/dist/src/code-execution/index.d.ts.map +1 -1
  15. package/dist/src/code-execution/index.js +5 -9
  16. package/dist/src/code-execution/index.js.map +1 -1
  17. package/dist/src/code-execution/isolate/calls.d.ts +114 -0
  18. package/dist/src/code-execution/isolate/calls.d.ts.map +1 -0
  19. package/dist/src/code-execution/isolate/calls.js +145 -0
  20. package/dist/src/code-execution/isolate/calls.js.map +1 -0
  21. package/dist/src/code-execution/isolate/child-entry.d.ts +2 -0
  22. package/dist/src/code-execution/isolate/child-entry.d.ts.map +1 -0
  23. package/dist/src/code-execution/isolate/child-entry.js +5 -0
  24. package/dist/src/code-execution/isolate/child-entry.js.map +1 -0
  25. package/dist/src/code-execution/isolate/child-main.d.ts +26 -0
  26. package/dist/src/code-execution/isolate/child-main.d.ts.map +1 -0
  27. package/dist/src/code-execution/isolate/child-main.js +132 -0
  28. package/dist/src/code-execution/isolate/child-main.js.map +1 -0
  29. package/dist/src/code-execution/isolate/child.d.ts +44 -0
  30. package/dist/src/code-execution/isolate/child.d.ts.map +1 -0
  31. package/dist/src/code-execution/isolate/child.js +315 -0
  32. package/dist/src/code-execution/isolate/child.js.map +1 -0
  33. package/dist/src/code-execution/isolate/frames.d.ts +80 -0
  34. package/dist/src/code-execution/isolate/frames.d.ts.map +1 -0
  35. package/dist/src/code-execution/isolate/frames.js +210 -0
  36. package/dist/src/code-execution/isolate/frames.js.map +1 -0
  37. package/dist/src/code-execution/isolate/output.d.ts +68 -0
  38. package/dist/src/code-execution/isolate/output.d.ts.map +1 -0
  39. package/dist/src/code-execution/isolate/output.js +143 -0
  40. package/dist/src/code-execution/isolate/output.js.map +1 -0
  41. package/dist/src/code-execution/isolate/prelude.d.ts +109 -0
  42. package/dist/src/code-execution/isolate/prelude.d.ts.map +1 -0
  43. package/dist/src/code-execution/isolate/prelude.js +401 -0
  44. package/dist/src/code-execution/isolate/prelude.js.map +1 -0
  45. package/dist/src/code-execution/isolate/protocol.d.ts +151 -0
  46. package/dist/src/code-execution/isolate/protocol.d.ts.map +1 -0
  47. package/dist/src/code-execution/isolate/protocol.js +150 -0
  48. package/dist/src/code-execution/isolate/protocol.js.map +1 -0
  49. package/dist/src/code-execution/isolate/render.d.ts +57 -0
  50. package/dist/src/code-execution/isolate/render.d.ts.map +1 -0
  51. package/dist/src/code-execution/isolate/render.js +214 -0
  52. package/dist/src/code-execution/isolate/render.js.map +1 -0
  53. package/dist/src/code-execution/isolate/runner.d.ts +250 -0
  54. package/dist/src/code-execution/isolate/runner.d.ts.map +1 -0
  55. package/dist/src/code-execution/isolate/runner.js +854 -0
  56. package/dist/src/code-execution/isolate/runner.js.map +1 -0
  57. package/dist/src/code-execution/isolate/scripts.d.ts +175 -0
  58. package/dist/src/code-execution/isolate/scripts.d.ts.map +1 -0
  59. package/dist/src/code-execution/isolate/scripts.js +357 -0
  60. package/dist/src/code-execution/isolate/scripts.js.map +1 -0
  61. package/dist/src/code-execution/quickjs.wasm +0 -0
  62. package/dist/src/code-execution/tool-definition.d.ts +93 -43
  63. package/dist/src/code-execution/tool-definition.d.ts.map +1 -1
  64. package/dist/src/code-execution/tool-definition.js +176 -142
  65. package/dist/src/code-execution/tool-definition.js.map +1 -1
  66. package/dist/src/deadline-timer.d.ts +3 -0
  67. package/dist/src/deadline-timer.d.ts.map +1 -1
  68. package/dist/src/deadline-timer.js +1 -1
  69. package/dist/src/deadline-timer.js.map +1 -1
  70. package/dist/src/framework.d.ts +1036 -440
  71. package/dist/src/framework.d.ts.map +1 -1
  72. package/dist/src/framework.js +4131 -2228
  73. package/dist/src/framework.js.map +1 -1
  74. package/dist/src/gate/event-gate.d.ts +28 -1
  75. package/dist/src/gate/event-gate.d.ts.map +1 -1
  76. package/dist/src/gate/event-gate.js +88 -10
  77. package/dist/src/gate/event-gate.js.map +1 -1
  78. package/dist/src/held-writes.d.ts +16 -3
  79. package/dist/src/held-writes.d.ts.map +1 -1
  80. package/dist/src/held-writes.js +16 -3
  81. package/dist/src/held-writes.js.map +1 -1
  82. package/dist/src/index.d.ts +6 -6
  83. package/dist/src/index.d.ts.map +1 -1
  84. package/dist/src/index.js +9 -8
  85. package/dist/src/index.js.map +1 -1
  86. package/dist/src/mcpl/channel-registry.d.ts +471 -75
  87. package/dist/src/mcpl/channel-registry.d.ts.map +1 -1
  88. package/dist/src/mcpl/channel-registry.js +1673 -337
  89. package/dist/src/mcpl/channel-registry.js.map +1 -1
  90. package/dist/src/mcpl/conversation-router.d.ts +12 -7
  91. package/dist/src/mcpl/conversation-router.d.ts.map +1 -1
  92. package/dist/src/mcpl/conversation-router.js +12 -7
  93. package/dist/src/mcpl/conversation-router.js.map +1 -1
  94. package/dist/src/mcpl/convert-block.d.ts.map +1 -1
  95. package/dist/src/mcpl/convert-block.js +9 -5
  96. package/dist/src/mcpl/convert-block.js.map +1 -1
  97. package/dist/src/mcpl/inbound-source.d.ts +50 -0
  98. package/dist/src/mcpl/inbound-source.d.ts.map +1 -1
  99. package/dist/src/mcpl/inbound-source.js +130 -0
  100. package/dist/src/mcpl/inbound-source.js.map +1 -1
  101. package/dist/src/mcpl/index.d.ts +2 -1
  102. package/dist/src/mcpl/index.d.ts.map +1 -1
  103. package/dist/src/mcpl/index.js +3 -2
  104. package/dist/src/mcpl/index.js.map +1 -1
  105. package/dist/src/mcpl/log-value.d.ts +6 -0
  106. package/dist/src/mcpl/log-value.d.ts.map +1 -1
  107. package/dist/src/mcpl/log-value.js +8 -0
  108. package/dist/src/mcpl/log-value.js.map +1 -1
  109. package/dist/src/mcpl/modern-connection.d.ts +15 -6
  110. package/dist/src/mcpl/modern-connection.d.ts.map +1 -1
  111. package/dist/src/mcpl/modern-connection.js +168 -53
  112. package/dist/src/mcpl/modern-connection.js.map +1 -1
  113. package/dist/src/mcpl/prose-grammar.d.ts +4 -5
  114. package/dist/src/mcpl/prose-grammar.d.ts.map +1 -1
  115. package/dist/src/mcpl/prose-grammar.js +4 -5
  116. package/dist/src/mcpl/prose-grammar.js.map +1 -1
  117. package/dist/src/mcpl/protocol-family.d.ts +97 -10
  118. package/dist/src/mcpl/protocol-family.d.ts.map +1 -1
  119. package/dist/src/mcpl/protocol-family.js +173 -16
  120. package/dist/src/mcpl/protocol-family.js.map +1 -1
  121. package/dist/src/mcpl/push-coalescer.d.ts +132 -14
  122. package/dist/src/mcpl/push-coalescer.d.ts.map +1 -1
  123. package/dist/src/mcpl/push-coalescer.js +227 -50
  124. package/dist/src/mcpl/push-coalescer.js.map +1 -1
  125. package/dist/src/mcpl/push-handler.d.ts +6 -1
  126. package/dist/src/mcpl/push-handler.d.ts.map +1 -1
  127. package/dist/src/mcpl/push-handler.js +4 -1
  128. package/dist/src/mcpl/push-handler.js.map +1 -1
  129. package/dist/src/mcpl/reference-fetcher.d.ts +2 -2
  130. package/dist/src/mcpl/reference-fetcher.d.ts.map +1 -1
  131. package/dist/src/mcpl/reference-fetcher.js +4 -4
  132. package/dist/src/mcpl/reference-fetcher.js.map +1 -1
  133. package/dist/src/mcpl/server-connection.d.ts +50 -21
  134. package/dist/src/mcpl/server-connection.d.ts.map +1 -1
  135. package/dist/src/mcpl/server-connection.js +158 -74
  136. package/dist/src/mcpl/server-connection.js.map +1 -1
  137. package/dist/src/mcpl/tags.d.ts +24 -0
  138. package/dist/src/mcpl/tags.d.ts.map +1 -1
  139. package/dist/src/mcpl/tags.js +33 -0
  140. package/dist/src/mcpl/tags.js.map +1 -1
  141. package/dist/src/mcpl/tool-result-normalize.d.ts +2 -1
  142. package/dist/src/mcpl/tool-result-normalize.d.ts.map +1 -1
  143. package/dist/src/mcpl/tool-result-normalize.js.map +1 -1
  144. package/dist/src/mcpl/transport.d.ts +55 -7
  145. package/dist/src/mcpl/transport.d.ts.map +1 -1
  146. package/dist/src/mcpl/transport.js +163 -34
  147. package/dist/src/mcpl/transport.js.map +1 -1
  148. package/dist/src/mcpl/types.d.ts +22 -6
  149. package/dist/src/mcpl/types.d.ts.map +1 -1
  150. package/dist/src/mcpl/types.js.map +1 -1
  151. package/dist/src/module-registry.d.ts +4 -2
  152. package/dist/src/module-registry.d.ts.map +1 -1
  153. package/dist/src/module-registry.js +100 -13
  154. package/dist/src/module-registry.js.map +1 -1
  155. package/dist/src/modules/discord/index.d.ts.map +1 -1
  156. package/dist/src/modules/discord/index.js +8 -13
  157. package/dist/src/modules/discord/index.js.map +1 -1
  158. package/dist/src/modules/history/index.d.ts.map +1 -1
  159. package/dist/src/modules/history/index.js +27 -4
  160. package/dist/src/modules/history/index.js.map +1 -1
  161. package/dist/src/modules/history/semantic.d.ts +15 -3
  162. package/dist/src/modules/history/semantic.d.ts.map +1 -1
  163. package/dist/src/modules/history/semantic.js +27 -5
  164. package/dist/src/modules/history/semantic.js.map +1 -1
  165. package/dist/src/modules/index.d.ts +1 -1
  166. package/dist/src/modules/index.d.ts.map +1 -1
  167. package/dist/src/modules/index.js.map +1 -1
  168. package/dist/src/modules/unknown-tool.d.ts +4 -1
  169. package/dist/src/modules/unknown-tool.d.ts.map +1 -1
  170. package/dist/src/modules/unknown-tool.js +4 -1
  171. package/dist/src/modules/unknown-tool.js.map +1 -1
  172. package/dist/src/modules/workspace/branch-intent.d.ts +3 -1
  173. package/dist/src/modules/workspace/branch-intent.d.ts.map +1 -1
  174. package/dist/src/modules/workspace/branch-intent.js +3 -1
  175. package/dist/src/modules/workspace/branch-intent.js.map +1 -1
  176. package/dist/src/modules/workspace/edit-plan.d.ts +265 -0
  177. package/dist/src/modules/workspace/edit-plan.d.ts.map +1 -0
  178. package/dist/src/modules/workspace/edit-plan.js +794 -0
  179. package/dist/src/modules/workspace/edit-plan.js.map +1 -0
  180. package/dist/src/modules/workspace/edit-text.d.ts +117 -0
  181. package/dist/src/modules/workspace/edit-text.d.ts.map +1 -0
  182. package/dist/src/modules/workspace/edit-text.js +584 -0
  183. package/dist/src/modules/workspace/edit-text.js.map +1 -0
  184. package/dist/src/modules/workspace/effects.d.ts +5 -4
  185. package/dist/src/modules/workspace/effects.d.ts.map +1 -1
  186. package/dist/src/modules/workspace/effects.js +78 -25
  187. package/dist/src/modules/workspace/effects.js.map +1 -1
  188. package/dist/src/modules/workspace/index.d.ts +484 -61
  189. package/dist/src/modules/workspace/index.d.ts.map +1 -1
  190. package/dist/src/modules/workspace/index.js +2173 -645
  191. package/dist/src/modules/workspace/index.js.map +1 -1
  192. package/dist/src/modules/workspace/observe.d.ts +213 -38
  193. package/dist/src/modules/workspace/observe.d.ts.map +1 -1
  194. package/dist/src/modules/workspace/observe.js +416 -36
  195. package/dist/src/modules/workspace/observe.js.map +1 -1
  196. package/dist/src/modules/workspace/reconcile.d.ts.map +1 -1
  197. package/dist/src/modules/workspace/reconcile.js +15 -8
  198. package/dist/src/modules/workspace/reconcile.js.map +1 -1
  199. package/dist/src/modules/workspace/text.d.ts +32 -0
  200. package/dist/src/modules/workspace/text.d.ts.map +1 -0
  201. package/dist/src/modules/workspace/text.js +67 -0
  202. package/dist/src/modules/workspace/text.js.map +1 -0
  203. package/dist/src/modules/workspace/types.d.ts +210 -6
  204. package/dist/src/modules/workspace/types.d.ts.map +1 -1
  205. package/dist/src/modules/workspace/types.js.map +1 -1
  206. package/dist/src/modules/workspace/watcher.d.ts +3 -2
  207. package/dist/src/modules/workspace/watcher.d.ts.map +1 -1
  208. package/dist/src/modules/workspace/watcher.js.map +1 -1
  209. package/dist/src/operator-log.d.ts +52 -24
  210. package/dist/src/operator-log.d.ts.map +1 -1
  211. package/dist/src/operator-log.js.map +1 -1
  212. package/dist/src/recovery/discord-awareness-outbox.d.ts +496 -72
  213. package/dist/src/recovery/discord-awareness-outbox.d.ts.map +1 -1
  214. package/dist/src/recovery/discord-awareness-outbox.js +1196 -289
  215. package/dist/src/recovery/discord-awareness-outbox.js.map +1 -1
  216. package/dist/src/recovery/offline-branch.d.ts +26 -1
  217. package/dist/src/recovery/offline-branch.d.ts.map +1 -1
  218. package/dist/src/recovery/offline-branch.js +164 -39
  219. package/dist/src/recovery/offline-branch.js.map +1 -1
  220. package/dist/src/recovery/recover-cli.js +23 -4
  221. package/dist/src/recovery/recover-cli.js.map +1 -1
  222. package/dist/src/send-tools.d.ts +11 -0
  223. package/dist/src/send-tools.d.ts.map +1 -0
  224. package/dist/src/send-tools.js +11 -0
  225. package/dist/src/send-tools.js.map +1 -0
  226. package/dist/src/tool-result-guard.d.ts +4 -3
  227. package/dist/src/tool-result-guard.d.ts.map +1 -1
  228. package/dist/src/tool-result-guard.js +5 -4
  229. package/dist/src/tool-result-guard.js.map +1 -1
  230. package/dist/src/tool-result-history.d.ts +1 -1
  231. package/dist/src/tool-result-history.js +1 -1
  232. package/dist/src/tune-out/coordinator.d.ts +21 -0
  233. package/dist/src/tune-out/coordinator.d.ts.map +1 -1
  234. package/dist/src/tune-out/coordinator.js +139 -37
  235. package/dist/src/tune-out/coordinator.js.map +1 -1
  236. package/dist/src/types/agent.d.ts +4 -0
  237. package/dist/src/types/agent.d.ts.map +1 -1
  238. package/dist/src/types/events.d.ts +62 -9
  239. package/dist/src/types/events.d.ts.map +1 -1
  240. package/dist/src/types/framework.d.ts +62 -58
  241. package/dist/src/types/framework.d.ts.map +1 -1
  242. package/dist/src/types/module.d.ts +40 -8
  243. package/dist/src/types/module.d.ts.map +1 -1
  244. package/dist/src/types/trace.d.ts +111 -10
  245. package/dist/src/types/trace.d.ts.map +1 -1
  246. package/dist/src/usage/types.d.ts +16 -0
  247. package/dist/src/usage/types.d.ts.map +1 -1
  248. package/dist/src/usage/usage-tracker.d.ts +14 -1
  249. package/dist/src/usage/usage-tracker.d.ts.map +1 -1
  250. package/dist/src/usage/usage-tracker.js +81 -41
  251. package/dist/src/usage/usage-tracker.js.map +1 -1
  252. package/package.json +6 -5
  253. package/src/agent.ts +93 -12
  254. package/src/api/server.ts +14 -12
  255. package/src/code-execution/engine.ts +595 -0
  256. package/src/code-execution/index.ts +5 -17
  257. package/src/code-execution/isolate/calls.ts +199 -0
  258. package/src/code-execution/isolate/child-entry.ts +5 -0
  259. package/src/code-execution/isolate/child-main.ts +132 -0
  260. package/src/code-execution/isolate/child.ts +327 -0
  261. package/src/code-execution/isolate/frames.ts +209 -0
  262. package/src/code-execution/isolate/output.ts +161 -0
  263. package/src/code-execution/isolate/prelude.ts +409 -0
  264. package/src/code-execution/isolate/protocol.ts +240 -0
  265. package/src/code-execution/isolate/render.ts +230 -0
  266. package/src/code-execution/isolate/runner.ts +911 -0
  267. package/src/code-execution/isolate/scripts.ts +479 -0
  268. package/src/code-execution/quickjs.wasm +0 -0
  269. package/src/code-execution/quickjs.wasm.LICENSES +149 -0
  270. package/src/code-execution/tool-definition.ts +236 -167
  271. package/src/deadline-timer.ts +1 -1
  272. package/src/framework.ts +4507 -2406
  273. package/src/gate/event-gate.ts +78 -11
  274. package/src/held-writes.ts +17 -3
  275. package/src/index.ts +30 -22
  276. package/src/mcpl/channel-registry.ts +1825 -368
  277. package/src/mcpl/conversation-router.ts +12 -7
  278. package/src/mcpl/convert-block.ts +9 -5
  279. package/src/mcpl/inbound-source.ts +131 -0
  280. package/src/mcpl/index.ts +4 -2
  281. package/src/mcpl/log-value.ts +9 -0
  282. package/src/mcpl/modern-connection.ts +162 -53
  283. package/src/mcpl/prose-grammar.ts +4 -5
  284. package/src/mcpl/protocol-family.ts +199 -21
  285. package/src/mcpl/push-coalescer.ts +306 -62
  286. package/src/mcpl/push-handler.ts +10 -2
  287. package/src/mcpl/reference-fetcher.ts +4 -4
  288. package/src/mcpl/server-connection.ts +180 -79
  289. package/src/mcpl/tags.ts +36 -0
  290. package/src/mcpl/tool-result-normalize.ts +2 -1
  291. package/src/mcpl/transport.ts +162 -33
  292. package/src/mcpl/types.ts +23 -6
  293. package/src/module-registry.ts +92 -13
  294. package/src/modules/discord/index.ts +8 -13
  295. package/src/modules/history/index.ts +25 -3
  296. package/src/modules/history/semantic.ts +29 -6
  297. package/src/modules/index.ts +1 -0
  298. package/src/modules/unknown-tool.ts +4 -1
  299. package/src/modules/workspace/branch-intent.ts +3 -1
  300. package/src/modules/workspace/edit-plan.ts +954 -0
  301. package/src/modules/workspace/edit-text.ts +618 -0
  302. package/src/modules/workspace/effects.ts +70 -19
  303. package/src/modules/workspace/index.ts +2247 -642
  304. package/src/modules/workspace/observe.ts +465 -56
  305. package/src/modules/workspace/reconcile.ts +14 -7
  306. package/src/modules/workspace/text.ts +68 -0
  307. package/src/modules/workspace/types.ts +211 -6
  308. package/src/modules/workspace/watcher.ts +3 -2
  309. package/src/operator-log.ts +47 -19
  310. package/src/recovery/discord-awareness-outbox.ts +1500 -356
  311. package/src/recovery/offline-branch.ts +189 -55
  312. package/src/recovery/recover-cli.ts +25 -4
  313. package/src/send-tools.ts +11 -0
  314. package/src/tool-result-guard.ts +6 -5
  315. package/src/tool-result-history.ts +1 -1
  316. package/src/tune-out/coordinator.ts +163 -42
  317. package/src/types/agent.ts +4 -0
  318. package/src/types/events.ts +62 -9
  319. package/src/types/framework.ts +62 -58
  320. package/src/types/module.ts +40 -8
  321. package/src/types/trace.ts +121 -15
  322. package/src/usage/types.ts +16 -0
  323. package/src/usage/usage-tracker.ts +86 -43
  324. package/dist/src/code-execution/py-runner.d.ts +0 -191
  325. package/dist/src/code-execution/py-runner.d.ts.map +0 -1
  326. package/dist/src/code-execution/py-runner.js +0 -655
  327. package/dist/src/code-execution/py-runner.js.map +0 -1
  328. package/dist/src/code-execution/runtime-py.d.ts +0 -50
  329. package/dist/src/code-execution/runtime-py.d.ts.map +0 -1
  330. package/dist/src/code-execution/runtime-py.js +0 -677
  331. package/dist/src/code-execution/runtime-py.js.map +0 -1
  332. package/dist/src/mcpl/prose-stream-router.d.ts +0 -88
  333. package/dist/src/mcpl/prose-stream-router.d.ts.map +0 -1
  334. package/dist/src/mcpl/prose-stream-router.js +0 -291
  335. package/dist/src/mcpl/prose-stream-router.js.map +0 -1
  336. package/src/code-execution/py-runner.ts +0 -791
  337. package/src/code-execution/runtime-py.ts +0 -677
  338. package/src/mcpl/prose-stream-router.ts +0 -268
@@ -0,0 +1,250 @@
1
+ /**
2
+ * The parent's side of one script: its child process, the calls it makes, its
3
+ * output, and how it ends. One `ScriptRun` per script, and one child per run,
4
+ * which ends with it, so nothing a script held stays after it.
5
+ *
6
+ * Every frame from the child is decoded and checked before the parent acts on
7
+ * it, since the child is untrusted. A frame that breaks the protocol, a child
8
+ * that exits without saying how the script ended, or one that doesn't stop
9
+ * when asked, ends the run as crashed. A tool call goes to `onToolCall`, the
10
+ * bridge to the framework's dispatcher, and its outcome goes into the run's
11
+ * call record at dispatch, as running until it's answered.
12
+ *
13
+ * What the host holds for a script is bounded: at most MAX_CALLS_IN_FLIGHT of
14
+ * its calls wait for answers at once, and at most MAX_CALLS_IN_FLIGHT are out
15
+ * in the host, timed-out ones whose tools still run included, with any past
16
+ * those waiting here as their text. Each call's arguments are within
17
+ * MAX_ARGUMENT_BYTES and MAX_ARGUMENT_VALUES, and each answer is at most
18
+ * `maxResultBytes`. Answers wait for the child as their frames, written while
19
+ * its stdin takes them and after its 'drain' once it hasn't, so each is held
20
+ * once, in bytes.
21
+ */
22
+ import { type OutputBounds, type OutputWindow } from './output.js';
23
+ import { type PreludeTool } from './prelude.js';
24
+ import { type CallSummary } from './calls.js';
25
+ import { MAX_CALLS_IN_FLIGHT, type GuestError, type StopReason } from './protocol.js';
26
+ /** A call's answer for the guest: `ok` resolves its promise with the parsed JSON, otherwise rejects with it. */
27
+ export interface CallAnswer {
28
+ ok: boolean;
29
+ json: string;
30
+ }
31
+ /** A tool call's answer from the bridge. */
32
+ export interface ToolAnswer extends CallAnswer {
33
+ /** The result was marked as an error. */
34
+ isError?: boolean;
35
+ /** The start of what the tool answered, as an agent would read it, for naming a call the script stopped waiting for. */
36
+ brief?: string;
37
+ /**
38
+ * No result came from the host in time, so the bridge answered for it: the
39
+ * tool may still be running, holding its work, or its result was lost, and
40
+ * the host can't tell which. Its place stays taken until the run ends.
41
+ */
42
+ held?: boolean;
43
+ }
44
+ /** How a script ended. */
45
+ export type Ending = {
46
+ how: 'finished';
47
+ value?: string;
48
+ } | {
49
+ how: 'failed';
50
+ error: GuestError;
51
+ stalled?: boolean;
52
+ }
53
+ /** `short` when the child had to be ended, so its output so far may be short. */
54
+ | {
55
+ how: 'stopped';
56
+ reason: StopReason;
57
+ short?: boolean;
58
+ why?: string;
59
+ } | {
60
+ how: 'crashed';
61
+ detail: string;
62
+ };
63
+ /** A run as it stands: its output since the last return, its calls, and its ending once it has one. */
64
+ export interface RunSnapshot {
65
+ output: string;
66
+ calls: CallSummary;
67
+ ending: Ending | null;
68
+ }
69
+ /** How many script children a host runs at once, counted until each child's process has exited. */
70
+ export declare class ChildCapacity {
71
+ readonly max: number;
72
+ private live;
73
+ constructor(max: number);
74
+ get running(): number;
75
+ /** Take a place, or false when the host is full. */
76
+ take(): boolean;
77
+ release(): void;
78
+ }
79
+ /**
80
+ * The child's entry, beside this module and with its extension: `.ts` where
81
+ * the host runs this package's source, as a Bun host does through the
82
+ * package's `bun` export, and `.js` where it runs the build. So the runtime
83
+ * is given a file that exists: given a `.js` path that it maps to the `.ts`
84
+ * beside it, Bun runs it as `bun run` would, with npm_* variables and a PATH
85
+ * of its own in the child's environment.
86
+ */
87
+ export declare const CHILD_ENTRY: string;
88
+ export interface ScriptRunOptions {
89
+ /** The child's entry: a script the runtime runs as the child process (CHILD_ENTRY, but for tests). */
90
+ childEntry: string;
91
+ /** Arguments after the entry, if any. */
92
+ childArgs?: readonly string[];
93
+ /** The runtime to run it with: by default the parent's own. */
94
+ execPath?: string;
95
+ memoryMaxBytes: number;
96
+ maxStackBytes?: number;
97
+ output: OutputBounds;
98
+ /** How often, at most, the child flushes output while its guest computes. */
99
+ flushMs: number;
100
+ /** The script's limit: at this age it's stopped, as "stopped at its limit". */
101
+ lifetimeMs: number;
102
+ /** How long a call may go unanswered before the script is told it timed out. */
103
+ toolCallTimeoutMs: number;
104
+ /** How long a stopped child has to report its ending before it's ended. */
105
+ stopGraceMs: number;
106
+ /** The least time between two of the script's wakes: an early one waits for its turn. */
107
+ wakeMinIntervalMs: number;
108
+ /** The most wakes one script may send; past it, a wake is refused. */
109
+ maxWakes: number;
110
+ /** The bridge: dispatch one tool call. Never rejects; a refusal is `ok: false`. */
111
+ onToolCall(name: string, argsJson: string): Promise<ToolAnswer>;
112
+ /** Send `wake_agent(payload)`, the script's `n`th wake, once its turn has come: answers the guest's promise. */
113
+ onWake(payloadJson: string, n: number): Promise<CallAnswer>;
114
+ /** Called once, when the run has an ending. */
115
+ onEnd?(ending: Ending): void;
116
+ }
117
+ export { MAX_CALLS_IN_FLIGHT };
118
+ /**
119
+ * The largest result the parent sends a script: a third of its linear memory,
120
+ * since parsing one takes about three times its size there (a heap run under
121
+ * Bun 1.4.3 and Node 24.19.0). A larger one would fail in the guest anyway,
122
+ * after growing the child past its budget, so it's refused before it's sent,
123
+ * and the script's ResultTooLarge says the call took effect.
124
+ */
125
+ export declare function maxResultBytes(memoryMaxBytes: number): number;
126
+ export declare class ScriptRun {
127
+ private readonly options;
128
+ private child;
129
+ private dir;
130
+ private readonly output;
131
+ private readonly log;
132
+ /** The ids of the guest's calls the child named, reserved until it says the script has their answer. */
133
+ private readonly pendingIds;
134
+ /** The calls answered and not yet seen: only these may the child say the script has. */
135
+ private readonly answeredIds;
136
+ /** The script's tools, the only names its calls may have. */
137
+ private toolNames;
138
+ /** Results sent to the guest and not yet seen: each is counted once the child says the script has it, and whether it fit. */
139
+ private readonly delivered;
140
+ private ending;
141
+ private readonly waiters;
142
+ private stderr;
143
+ private lifetimeTimer;
144
+ private killTimer;
145
+ private processExited;
146
+ private stopAsked;
147
+ /** Each unanswered call's timeout, cleared when it's answered or the run ends. */
148
+ private readonly callTimers;
149
+ /** The calls holding a place among those in flight, by id, until the child says the script has their answer. */
150
+ private readonly inFlight;
151
+ /**
152
+ * How many calls the bridge holds, until it answers each: a call that timed
153
+ * out in the script, and so left its place there, may still hold its
154
+ * arguments in the host. Counted, not keyed by id, since the child may name
155
+ * a timed-out call's id again once the script has seen its TimeoutError.
156
+ * At most MAX_CALLS_IN_FLIGHT; a call past them waits in `dispatchWaiting`,
157
+ * as its text, until one is answered. One the bridge answered for (`held`)
158
+ * keeps its place until the run ends.
159
+ */
160
+ private hostCalls;
161
+ /** Calls waiting for the bridge to hold fewer: each resolved with true when its turn comes, false when it won't be sent. */
162
+ private readonly dispatchWaiting;
163
+ /** Wakes the child has sent, which the cap counts: those refused once a stop was asked included, so a child that ignores the stop is still bounded. */
164
+ private wakeFrames;
165
+ /** Wakes handed to the host to deliver, whatever it answered: a wake's number, which the host is given. */
166
+ private wakes;
167
+ /** Wakes the host delivered, its answer neither a refusal nor a failure. */
168
+ private delivered_wakes;
169
+ /** Wakes waiting for their turn, which the run's end counts as never sent. */
170
+ private wakesWaiting;
171
+ /** When the next wake may go out, on the monotonic clock: the spacing counts from when the last wake's delivery ended. */
172
+ private nextWakeAt;
173
+ /** Settles once the latest wake has gone out, or won't: wakes go out in the order they were made. */
174
+ private wakeChain;
175
+ /** Wakes waiting out the spacing, each released at its time or when the run ends. */
176
+ private readonly wakeWaiters;
177
+ /**
178
+ * Results waiting for the child's stdin to drain, as their frames: each holds
179
+ * its JSON's bytes, which the result bound counts. A result's id becomes one
180
+ * the child may say it has seen once its frame is written.
181
+ */
182
+ private readonly resultQueue;
183
+ private draining;
184
+ private exitResolve;
185
+ private endedResolve;
186
+ /** Resolves when the run ends: a wake waiting behind another's delivery stops waiting then. */
187
+ private readonly endedSignal;
188
+ /** Resolves when the child's process has exited, or at once if it never started. */
189
+ readonly exited: Promise<void>;
190
+ constructor(options: ScriptRunOptions);
191
+ /**
192
+ * Spawn the child and send it the script. A start frame too long for a frame
193
+ * throws before anything is spawned, with `exited` resolved, so nothing is
194
+ * left running and the place it took frees at once. A child that can't be
195
+ * spawned crashes the run.
196
+ */
197
+ start(script: string, tools: readonly PreludeTool[]): void;
198
+ /** The run as it stands, its output since the last return included. */
199
+ snapshot(): RunSnapshot;
200
+ /** The output since the last return, as it stands. */
201
+ outputWindow(): OutputWindow;
202
+ /** The output since the last return, starting a fresh stretch from here. */
203
+ takeOutput(): OutputWindow;
204
+ get ended(): boolean;
205
+ /** End the child's process now if it's still alive, whatever the run's state. Resolves once it has exited. */
206
+ kill(): Promise<void>;
207
+ /** The wakes the host delivered to the script's agent: those it answered ok and not as an error. A refused wake isn't one, nor a failed one, though a failure may have taken effect. */
208
+ get wakesDelivered(): number;
209
+ /** Resolves when the script has ended, or after `ms`, whichever comes first. */
210
+ wait(ms: number): Promise<RunSnapshot>;
211
+ /**
212
+ * Ask the script to stop: the stop file for a computing guest, a frame for an
213
+ * idle one. If the child hasn't said how it ended within the grace, it's
214
+ * ended, and the ending says its output so far may be short. The reason is
215
+ * the one asked here, whatever the child says.
216
+ */
217
+ stop(reason: StopReason, why?: string): Promise<RunSnapshot>;
218
+ private send;
219
+ /** A call's result, as a result frame once its turn comes: the JSON's bytes go to the engine as they are. */
220
+ private sendResult;
221
+ /** Write waiting results, one frame at a time: after a write the pipe didn't take whole, the next waits for 'drain'. */
222
+ private pump;
223
+ private write;
224
+ private handle;
225
+ private call;
226
+ /**
227
+ * A call no longer holds a place in the host: the bridge answered it, or it
228
+ * won't be sent. The first call waiting for room takes the place, in the
229
+ * same step, so no later call can take it first. Whatever lets a waiting
230
+ * call go takes it off the queue. One given its place once the run has
231
+ * ended or its process has exited isn't sent (the call's own check), and
232
+ * the end lets the calls still waiting go.
233
+ */
234
+ private releasePlace;
235
+ /** An answer to the call the child named `id`, sent once its turn comes. The id stays reserved until the child says the script has it. */
236
+ private answer;
237
+ /** A call or wake that comes once a stop has been asked isn't sent: the script is ending. */
238
+ private stopRefusal;
239
+ /** Resolves once the spacing since the last wake has passed, or at once when the run has ended. */
240
+ private wakeTurn;
241
+ /**
242
+ * Once the process has exited and the run has ended, nothing more goes to
243
+ * the child: an ended run kept for a later wait holds none of it. Whichever
244
+ * of the two comes second calls this.
245
+ */
246
+ private release;
247
+ private crash;
248
+ private end;
249
+ }
250
+ //# sourceMappingURL=runner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../../../src/code-execution/isolate/runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AASH,OAAO,EAAiC,KAAK,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAClG,OAAO,EAAkB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAwD,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AACpG,OAAO,EAOL,mBAAmB,EAKnB,KAAK,UAAU,EACf,KAAK,UAAU,EAChB,MAAM,eAAe,CAAC;AAGvB,gHAAgH;AAChH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,OAAO,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;CACd;AAED,4CAA4C;AAC5C,MAAM,WAAW,UAAW,SAAQ,UAAU;IAC5C,yCAAyC;IACzC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,wHAAwH;IACxH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,0BAA0B;AAC1B,MAAM,MAAM,MAAM,GACd;IAAE,GAAG,EAAE,UAAU,CAAC;IAAC,KAAK,CAAC,EAAE,MA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