@parcha/agentrun-serve 0.1.0-beta.32 → 0.1.0-beta.34

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 (444) hide show
  1. package/CHANGELOG.md +270 -0
  2. package/README.md +343 -246
  3. package/dist/adapters.d.ts +0 -7
  4. package/dist/adapters.js +11 -22
  5. package/dist/agent-rows.d.ts +98 -0
  6. package/dist/agent-rows.js +1 -0
  7. package/dist/agents-store.d.ts +6 -2
  8. package/dist/agents-store.js +60 -25
  9. package/dist/agents.d.ts +9 -125
  10. package/dist/agents.js +15 -305
  11. package/dist/answer-json.d.ts +23 -0
  12. package/dist/answer-json.js +58 -0
  13. package/dist/api-only.d.ts +14 -0
  14. package/dist/api-only.js +33 -0
  15. package/dist/archil-routes.d.ts +1 -1
  16. package/dist/bash-read-only.d.ts +22 -0
  17. package/dist/bash-read-only.js +701 -0
  18. package/dist/box-egress-vm.d.ts +0 -8
  19. package/dist/box-egress-vm.js +0 -10
  20. package/dist/box-egress.d.ts +0 -4
  21. package/dist/box-egress.js +1 -16
  22. package/dist/box-paths.d.ts +25 -2
  23. package/dist/box-paths.js +79 -11
  24. package/dist/box-relay.d.ts +0 -11
  25. package/dist/box-relay.js +0 -20
  26. package/dist/box-settings.d.ts +26 -0
  27. package/dist/box-settings.js +63 -0
  28. package/dist/boxes.d.ts +21 -1
  29. package/dist/boxes.js +27 -20
  30. package/dist/browser-commands.d.ts +5 -2
  31. package/dist/browser-commands.js +4 -4
  32. package/dist/browser-computer.d.ts +5 -0
  33. package/dist/browser-computer.js +21 -0
  34. package/dist/browser-cost.d.ts +9 -6
  35. package/dist/browser-cost.js +94 -51
  36. package/dist/browser-live.d.ts +7 -20
  37. package/dist/browser-live.js +15 -93
  38. package/dist/browser-person.d.ts +18 -53
  39. package/dist/browser-person.js +94 -279
  40. package/dist/budget.d.ts +0 -4
  41. package/dist/budget.js +0 -7
  42. package/dist/cap.d.ts +8 -12
  43. package/dist/cap.js +12 -27
  44. package/dist/cli.js +61 -20
  45. package/dist/closed-pipes.d.ts +2 -0
  46. package/dist/closed-pipes.js +13 -0
  47. package/dist/computer-commands.d.ts +2 -0
  48. package/dist/computer-commands.js +2 -0
  49. package/dist/computer-keys.js +5 -2
  50. package/dist/computer-setting.d.ts +20 -4
  51. package/dist/computer-setting.js +25 -5
  52. package/dist/computers.d.ts +31 -3
  53. package/dist/computers.js +70 -11
  54. package/dist/confine.d.ts +120 -0
  55. package/dist/confine.js +436 -0
  56. package/dist/console/assets/index-RPHGlXmM.js +540 -0
  57. package/dist/console/assets/index-opTN5Naq.css +1 -0
  58. package/dist/console/index.html +2 -2
  59. package/dist/daytona-api.d.ts +0 -11
  60. package/dist/daytona-api.js +1 -82
  61. package/dist/decision-rows.d.ts +1 -1
  62. package/dist/decision-rows.js +1 -1
  63. package/dist/docker-daemon.d.ts +31 -0
  64. package/dist/docker-daemon.js +56 -0
  65. package/dist/docker-host-addresses.d.ts +37 -0
  66. package/dist/docker-host-addresses.js +110 -0
  67. package/dist/effort-guidance.js +4 -3
  68. package/dist/environment.d.ts +0 -3
  69. package/dist/environment.js +3 -5
  70. package/dist/errors.d.ts +0 -2
  71. package/dist/errors.js +6 -9
  72. package/dist/filed-answer.d.ts +32 -0
  73. package/dist/filed-answer.js +160 -0
  74. package/dist/files.d.ts +1 -11
  75. package/dist/files.js +2 -21
  76. package/dist/github-connection.d.ts +2 -2
  77. package/dist/github-connection.js +1 -1
  78. package/dist/graph-view.d.ts +3 -5
  79. package/dist/graph-view.js +4 -14
  80. package/dist/handed-values.d.ts +9 -0
  81. package/dist/handed-values.js +26 -0
  82. package/dist/harness-install.d.ts +22 -5
  83. package/dist/harness-install.js +98 -20
  84. package/dist/harness-opencode/adapter.d.ts +7 -0
  85. package/dist/harness-opencode/adapter.js +23 -6
  86. package/dist/harness-opencode/index.d.ts +1 -1
  87. package/dist/harness-opencode/index.js +1 -1
  88. package/dist/harness-opencode/verify.d.ts +17 -0
  89. package/dist/harness-opencode/verify.js +30 -10
  90. package/dist/harness-table.js +6 -1
  91. package/dist/harness.d.ts +4 -3
  92. package/dist/harness.js +16 -1
  93. package/dist/heard.js +1 -1
  94. package/dist/held-fd.d.ts +5 -0
  95. package/dist/held-fd.js +14 -0
  96. package/dist/host-calls.d.ts +20 -0
  97. package/dist/host-calls.js +12 -0
  98. package/dist/host-tools.d.ts +4 -21
  99. package/dist/host-tools.js +4 -61
  100. package/dist/host.d.ts +2 -4
  101. package/dist/host.js +2 -7
  102. package/dist/import-summary.d.ts +1 -5
  103. package/dist/import-summary.js +0 -21
  104. package/dist/import.d.ts +6 -6
  105. package/dist/import.js +62 -24
  106. package/dist/key-refusal.d.ts +5 -1
  107. package/dist/key-refusal.js +19 -4
  108. package/dist/learn/check.d.ts +6 -2
  109. package/dist/learn/check.js +83 -29
  110. package/dist/learn/folder.d.ts +5 -1
  111. package/dist/learn/folder.js +10 -3
  112. package/dist/learn/grade.d.ts +14 -0
  113. package/dist/learn/grade.js +68 -12
  114. package/dist/learn/index.d.ts +4 -4
  115. package/dist/learn/index.js +4 -4
  116. package/dist/learn/reflect.d.ts +31 -2
  117. package/dist/learn/reflect.js +79 -4
  118. package/dist/learn/relevance.d.ts +24 -0
  119. package/dist/learn/relevance.js +91 -0
  120. package/dist/learn/secrets.d.ts +8 -1
  121. package/dist/learn/secrets.js +95 -15
  122. package/dist/learn/skills.d.ts +112 -14
  123. package/dist/learn/skills.js +556 -52
  124. package/dist/learned-workflow.d.ts +37 -5
  125. package/dist/learned-workflow.js +106 -12
  126. package/dist/learning-routes.d.ts +16 -49
  127. package/dist/learning-routes.js +46 -36
  128. package/dist/learning-run.d.ts +6 -250
  129. package/dist/learning-run.js +11 -665
  130. package/dist/learning-words.d.ts +12 -5
  131. package/dist/learning-words.js +18 -9
  132. package/dist/learning.d.ts +93 -77
  133. package/dist/learning.js +176 -304
  134. package/dist/live/stream.js +1 -1
  135. package/dist/live/translate.js +13 -3
  136. package/dist/local-models.d.ts +11 -5
  137. package/dist/local-models.js +13 -8
  138. package/dist/mcp-gateway.d.ts +11 -52
  139. package/dist/mcp-gateway.js +44 -148
  140. package/dist/mcp-oauth.d.ts +10 -14
  141. package/dist/mcp-oauth.js +14 -32
  142. package/dist/mcp-probe.d.ts +0 -7
  143. package/dist/mcp-probe.js +4 -10
  144. package/dist/mcp-servers-store.d.ts +6 -6
  145. package/dist/mcp-servers-store.js +13 -15
  146. package/dist/mcp-store.js +3 -4
  147. package/dist/mcp.js +4 -3
  148. package/dist/model.d.ts +6 -7
  149. package/dist/model.js +43 -27
  150. package/dist/monid.d.ts +0 -2
  151. package/dist/monid.js +3 -6
  152. package/dist/openapi.d.ts +8 -1
  153. package/dist/openapi.js +124 -50
  154. package/dist/page.d.ts +1 -3
  155. package/dist/page.js +9 -19
  156. package/dist/paid-calls.d.ts +21 -27
  157. package/dist/paid-calls.js +28 -62
  158. package/dist/pause.d.ts +0 -2
  159. package/dist/pause.js +1 -8
  160. package/dist/place.d.ts +0 -5
  161. package/dist/place.js +0 -10
  162. package/dist/print.d.ts +2 -1
  163. package/dist/print.js +4 -2
  164. package/dist/propose.js +83 -39
  165. package/dist/push-handler.d.ts +1 -1
  166. package/dist/reflect-range.d.ts +25 -0
  167. package/dist/reflect-range.js +30 -0
  168. package/dist/refusal-words.d.ts +5 -1
  169. package/dist/refusal-words.js +5 -1
  170. package/dist/rows.d.ts +1 -1
  171. package/dist/run-cut.d.ts +0 -6
  172. package/dist/run-cut.js +0 -6
  173. package/dist/run-db.d.ts +2 -13
  174. package/dist/run-db.js +2 -41
  175. package/dist/run-launch.d.ts +19 -2
  176. package/dist/run-launch.js +13 -5
  177. package/dist/run-learning-served.d.ts +46 -2
  178. package/dist/run-learning-served.js +26 -7
  179. package/dist/run-reads-over-store.d.ts +1 -1
  180. package/dist/run-reads-over-store.js +15 -6
  181. package/dist/run-secrets.d.ts +33 -0
  182. package/dist/run-secrets.js +53 -0
  183. package/dist/run-timeline-served.d.ts +5 -1
  184. package/dist/run-timeline-served.js +19 -5
  185. package/dist/run-view.d.ts +12 -17
  186. package/dist/run-view.js +29 -28
  187. package/dist/runnable.d.ts +18 -10
  188. package/dist/runnable.js +21 -10
  189. package/dist/runner/adapter-loop.d.ts +14 -3
  190. package/dist/runner/adapter-loop.js +464 -62
  191. package/dist/runner/adapter-run-spec.d.ts +29 -8
  192. package/dist/runner/adapter-run-spec.js +96 -37
  193. package/dist/runner/adapters/index.js +11 -1
  194. package/dist/runner/adapters/pi.d.ts +15 -1
  195. package/dist/runner/adapters/pi.js +19 -5
  196. package/dist/runner/beside.d.ts +30 -3
  197. package/dist/runner/beside.js +54 -12
  198. package/dist/runner/claim-secrets.d.ts +10 -1
  199. package/dist/runner/claim-secrets.js +49 -15
  200. package/dist/runner/cli-run-spec.d.ts +2 -0
  201. package/dist/runner/cli-run-spec.js +60 -17
  202. package/dist/runner/commands.js +1 -1
  203. package/dist/runner/computer-journal.d.ts +15 -2
  204. package/dist/runner/computer-journal.js +41 -5
  205. package/dist/runner/conformance/harness.js +118 -8
  206. package/dist/runner/conformance/subject.d.ts +4 -1
  207. package/dist/runner/dispatch.d.ts +8 -1
  208. package/dist/runner/dispatch.js +81 -7
  209. package/dist/runner/end-of-run.d.ts +8 -2
  210. package/dist/runner/end-of-run.js +10 -4
  211. package/dist/runner/github-app.js +1 -1
  212. package/dist/runner/harness-loops.d.ts +9 -1
  213. package/dist/runner/harness-loops.js +15 -2
  214. package/dist/runner/journal-kinds.d.ts +30 -6
  215. package/dist/runner/journal-kinds.js +20 -5
  216. package/dist/runner/journal-reader.d.ts +1 -1
  217. package/dist/runner/journal-reader.js +2 -2
  218. package/dist/runner/learning-ask.d.ts +4 -0
  219. package/dist/runner/learning-ask.js +15 -1
  220. package/dist/runner/learning-end.d.ts +18 -3
  221. package/dist/runner/learning-end.js +113 -11
  222. package/dist/runner/learning-loop.d.ts +5 -0
  223. package/dist/runner/learning-loop.js +26 -6
  224. package/dist/runner/live-ticker.d.ts +1 -1
  225. package/dist/runner/live-ticker.js +28 -4
  226. package/dist/runner/loop-contract.d.ts +3 -2
  227. package/dist/runner/main.d.ts +64 -10
  228. package/dist/runner/main.js +165 -27
  229. package/dist/runner/mcp-claim.d.ts +1 -1
  230. package/dist/runner/mcp-claim.js +2 -3
  231. package/dist/runner/mcp-run.d.ts +2 -0
  232. package/dist/runner/mcp-run.js +15 -5
  233. package/dist/runner/memory-claim.d.ts +9 -0
  234. package/dist/runner/memory-claim.js +25 -0
  235. package/dist/runner/metrics.d.ts +33 -1
  236. package/dist/runner/metrics.js +85 -1
  237. package/dist/runner/model-route.d.ts +13 -0
  238. package/dist/runner/model-route.js +154 -11
  239. package/dist/runner/redact.d.ts +8 -2
  240. package/dist/runner/redact.js +11 -2
  241. package/dist/runner/reflect-hook.d.ts +12 -1
  242. package/dist/runner/reflect-hook.js +33 -3
  243. package/dist/runner/runner.d.ts +20 -0
  244. package/dist/runner/runner.js +56 -10
  245. package/dist/runner/store-port.d.ts +1 -1
  246. package/dist/runner/store-secrets.d.ts +3 -3
  247. package/dist/runner/store-secrets.js +8 -12
  248. package/dist/runner/text-tools.d.ts +44 -0
  249. package/dist/runner/text-tools.js +237 -0
  250. package/dist/runner/workflow-loop.d.ts +6 -0
  251. package/dist/runner/workflow-loop.js +6 -4
  252. package/dist/runner/workflow-spec.d.ts +18 -0
  253. package/dist/runner/workflow-spec.js +68 -26
  254. package/dist/runs-over-store.d.ts +8 -3
  255. package/dist/runs-over-store.js +76 -19
  256. package/dist/seal-key.d.ts +54 -15
  257. package/dist/seal-key.js +232 -82
  258. package/dist/sealed-settings-store.d.ts +19 -4
  259. package/dist/sealed-settings-store.js +9 -9
  260. package/dist/sealing.d.ts +4 -0
  261. package/dist/sealing.js +20 -0
  262. package/dist/secret-cut.d.ts +1 -1
  263. package/dist/secret-cut.js +1 -1
  264. package/dist/secret-name.d.ts +9 -0
  265. package/dist/secret-name.js +15 -0
  266. package/dist/secret-writes.d.ts +0 -1
  267. package/dist/secret-writes.js +0 -1
  268. package/dist/secrets.d.ts +2 -10
  269. package/dist/secrets.js +21 -17
  270. package/dist/server-fs.d.ts +4 -0
  271. package/dist/server-fs.js +8 -2
  272. package/dist/server-lock.d.ts +0 -24
  273. package/dist/server-lock.js +1 -80
  274. package/dist/server-secrets-store.d.ts +4 -4
  275. package/dist/server-secrets-store.js +10 -32
  276. package/dist/server-secrets.d.ts +4 -9
  277. package/dist/server-secrets.js +12 -33
  278. package/dist/server.d.ts +26 -44
  279. package/dist/server.js +229 -142
  280. package/dist/sign-ins-store.d.ts +34 -36
  281. package/dist/sign-ins-store.js +27 -38
  282. package/dist/sign-ins.d.ts +2 -119
  283. package/dist/sign-ins.js +5 -311
  284. package/dist/standard-webhooks.d.ts +1 -1
  285. package/dist/standard-webhooks.js +1 -1
  286. package/dist/stats.d.ts +3 -2
  287. package/dist/stats.js +1 -1
  288. package/dist/store/agent-body.d.ts +8 -2
  289. package/dist/store/agent-body.js +16 -4
  290. package/dist/store/index.d.ts +3 -3
  291. package/dist/store/index.js +3 -3
  292. package/dist/store/open.d.ts +18 -5
  293. package/dist/store/open.js +23 -3
  294. package/dist/store/postgres/connection.d.ts +18 -2
  295. package/dist/store/postgres/connection.js +156 -32
  296. package/dist/store/postgres/migrate.d.ts +4 -0
  297. package/dist/store/postgres/migrate.js +27 -3
  298. package/dist/store/postgres/migrations/0003_commands_one_answer.check.sql +19 -0
  299. package/dist/store/postgres/migrations/0003_commands_one_answer.sql +13 -0
  300. package/dist/store/postgres/store.d.ts +9 -2
  301. package/dist/store/postgres/store.js +9 -0
  302. package/dist/store/schema.js +1 -1
  303. package/dist/store/seal.js +2 -2
  304. package/dist/store/sqlite/connection.js +49 -5
  305. package/dist/store/sqlite/run-patch.d.ts +3 -0
  306. package/dist/store/sqlite/run-patch.js +3 -0
  307. package/dist/store/sqlite/store.js +64 -20
  308. package/dist/store/store.d.ts +38 -6
  309. package/dist/store/store.js +12 -2
  310. package/dist/store-reads/cap.d.ts +1 -1
  311. package/dist/store-reads/cap.js +22 -4
  312. package/dist/store-reads/journal.d.ts +25 -6
  313. package/dist/store-reads/journal.js +173 -21
  314. package/dist/store-reads/learning.d.ts +26 -8
  315. package/dist/store-reads/learning.js +12 -4
  316. package/dist/store-reads/workflow.d.ts +1 -1
  317. package/dist/store-reads/workflow.js +1 -1
  318. package/dist/store-view.d.ts +5 -1
  319. package/dist/store-view.js +91 -23
  320. package/dist/template-eval.d.ts +60 -4
  321. package/dist/template-eval.js +120 -54
  322. package/dist/templates.d.ts +24 -10
  323. package/dist/templates.js +69 -38
  324. package/dist/timeline/group.d.ts +33 -0
  325. package/dist/timeline/group.js +77 -0
  326. package/dist/timeline/index.d.ts +7 -0
  327. package/dist/timeline/index.js +7 -0
  328. package/dist/timeline/labels.d.ts +79 -0
  329. package/dist/timeline/labels.js +150 -0
  330. package/dist/timeline/origin.d.ts +56 -0
  331. package/dist/timeline/origin.js +92 -0
  332. package/dist/timeline/render-text.d.ts +11 -0
  333. package/dist/timeline/render-text.js +62 -0
  334. package/dist/timeline/timeline.d.ts +274 -0
  335. package/dist/timeline/timeline.js +1308 -0
  336. package/dist/timeline/util.d.ts +17 -0
  337. package/dist/timeline/util.js +49 -0
  338. package/dist/tool-gate.d.ts +41 -216
  339. package/dist/tool-gate.js +79 -540
  340. package/dist/tool-keys-store.d.ts +11 -12
  341. package/dist/tool-keys-store.js +24 -31
  342. package/dist/tool-keys.d.ts +9 -104
  343. package/dist/tool-keys.js +15 -271
  344. package/dist/tool-plane.d.ts +14 -42
  345. package/dist/tool-plane.js +23 -81
  346. package/dist/tool-settings-store.d.ts +1 -2
  347. package/dist/tool-settings-store.js +5 -8
  348. package/dist/tools.d.ts +31 -19
  349. package/dist/tools.js +101 -108
  350. package/dist/trigger-adapter.d.ts +2 -2
  351. package/dist/trigger-schedule.js +2 -1
  352. package/dist/trigger-secrets-shown.js +5 -1
  353. package/dist/trigger-targets.d.ts +13 -1
  354. package/dist/trigger-targets.js +31 -1
  355. package/dist/triggers.d.ts +2 -2
  356. package/dist/triggers.js +3 -3
  357. package/dist/turn-end.d.ts +1 -1
  358. package/dist/turn-end.js +1 -1
  359. package/dist/turns.d.ts +0 -6
  360. package/dist/turns.js +0 -19
  361. package/dist/uploads.js +1 -1
  362. package/dist/usage.d.ts +0 -2
  363. package/dist/usage.js +0 -2
  364. package/dist/validate.d.ts +2 -0
  365. package/dist/validate.js +23 -14
  366. package/dist/webhooks.d.ts +102 -76
  367. package/dist/webhooks.js +266 -233
  368. package/dist/workflow-run.d.ts +114 -0
  369. package/dist/workflow-run.js +144 -0
  370. package/dist/workspace/beneath.d.ts +19 -0
  371. package/dist/workspace/beneath.js +106 -0
  372. package/dist/workspace/computer-deps.d.ts +52 -5
  373. package/dist/workspace/computer-deps.js +154 -19
  374. package/dist/workspace/computer-for-claim.d.ts +19 -14
  375. package/dist/workspace/computer-for-claim.js +77 -16
  376. package/dist/workspace/computer-hold.d.ts +6 -1
  377. package/dist/workspace/computer-hold.js +6 -1
  378. package/dist/workspace/computer-janitor.d.ts +47 -0
  379. package/dist/workspace/computer-janitor.js +182 -0
  380. package/dist/workspace/computer-providers.d.ts +6 -2
  381. package/dist/workspace/computer-providers.js +1 -1
  382. package/dist/workspace/grep-worker.d.ts +5 -0
  383. package/dist/workspace/grep-worker.js +9 -6
  384. package/dist/workspace/index.d.ts +4 -3
  385. package/dist/workspace/index.js +4 -4
  386. package/dist/workspace/local.d.ts +11 -2
  387. package/dist/workspace/local.js +161 -43
  388. package/dist/workspace/resolve-computer.d.ts +50 -10
  389. package/dist/workspace/resolve-computer.js +46 -11
  390. package/dist/workspace/run-tools.d.ts +13 -31
  391. package/dist/workspace/run-tools.js +10 -27
  392. package/dist/workspace-paths.d.ts +0 -17
  393. package/dist/workspace-paths.js +1 -41
  394. package/dist/workspace.d.ts +0 -17
  395. package/dist/workspace.js +3 -99
  396. package/launcher/agentrun-serve +6 -33
  397. package/package.json +14 -14
  398. package/templates/README.md +9 -3
  399. package/templates/inbound-lead/agent.md +1 -1
  400. package/templates/kyb/agent.md +1 -1
  401. package/templates/market-monitor/agent.md +2 -2
  402. package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
  403. package/dist/app.d.ts +0 -5
  404. package/dist/app.js +0 -514
  405. package/dist/bound-files.d.ts +0 -4
  406. package/dist/bound-files.js +0 -164
  407. package/dist/bounds.d.ts +0 -26
  408. package/dist/bounds.js +0 -137
  409. package/dist/coding-tools.d.ts +0 -47
  410. package/dist/coding-tools.js +0 -189
  411. package/dist/computer-adb.d.ts +0 -12
  412. package/dist/computer-adb.js +0 -14
  413. package/dist/console/assets/index-35-7aeIZ.js +0 -519
  414. package/dist/console/assets/index-Cejbov-Y.css +0 -1
  415. package/dist/content-filter.d.ts +0 -7
  416. package/dist/content-filter.js +0 -23
  417. package/dist/disk.d.ts +0 -33
  418. package/dist/disk.js +0 -85
  419. package/dist/door-line.d.ts +0 -23
  420. package/dist/door-line.js +0 -83
  421. package/dist/grade.d.ts +0 -63
  422. package/dist/grade.js +0 -150
  423. package/dist/harness-app.d.ts +0 -69
  424. package/dist/harness-app.js +0 -1162
  425. package/dist/mcp-servers.d.ts +0 -269
  426. package/dist/mcp-servers.js +0 -456
  427. package/dist/packs.d.ts +0 -71
  428. package/dist/packs.js +0 -180
  429. package/dist/request-start.d.ts +0 -27
  430. package/dist/request-start.js +0 -381
  431. package/dist/run-box.d.ts +0 -205
  432. package/dist/run-box.js +0 -661
  433. package/dist/run-holder.d.ts +0 -52
  434. package/dist/run-holder.js +0 -139
  435. package/dist/runs.d.ts +0 -1200
  436. package/dist/runs.js +0 -4103
  437. package/dist/sealed-settings.d.ts +0 -60
  438. package/dist/sealed-settings.js +0 -178
  439. package/dist/served-only.d.ts +0 -5
  440. package/dist/served-only.js +0 -12
  441. package/dist/sessions.d.ts +0 -6
  442. package/dist/sessions.js +0 -109
  443. package/dist/workspace/computer-serves.d.ts +0 -12
  444. package/dist/workspace/computer-serves.js +0 -15
@@ -8,205 +8,34 @@
8
8
  // as session time always is. The run stays working; the timeline says "You took over the browser" and how it came back.
9
9
  //
10
10
  // The agent asks: pi-browser holds the session for a person and calls `request`, which holds the person's time the same
11
- // way (refused, the tool's result says why), writes the ask's pending record in the run's directory, outside work/
12
- // (BROWSER_ASK_DIR/<id>.json, its id a digest of the session's tag and the model's request id, 0600 in 0700: the session's tag, the instruction, the done-when condition, when it was asked
13
- // and when it expires; never the live view's link or token), and commits a waiting row ("Waiting for you: <instruction>").
14
- // The server reads the record: the run is input_required/waiting with `pending.kind` browser, and run.needs_input fires.
15
- // The owner answers POST /v2/runs/{id}/browser/hand-back, which writes `<id>.answer.json` once (the first answer wins);
16
- // unanswered by `expires_at`, the run's process writes `expired` the same way. pi-browser's wait reads it through
17
- // `outcome`; a poll here ends any take-over at once and drops the hold. The records outlive a process that dies (the
18
- // run is started again and its next process takes the wait up, `adopt`), and go when the run ends, or when its process
19
- // lets its sessions go.
20
- import { createHash, randomBytes } from "node:crypto";
21
- import { closeSync, constants, existsSync, fstatSync, linkSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
22
- import { join } from "node:path";
23
- import { HOST_PHASE } from "@parcha/pi-durable-agent/timeline";
24
- /** pi-browser's tool for the agent's ask. */
25
- export const HANDOFF_TOOL = "browser_request_handoff";
26
- /** The ask's directory in the run's directory (never in work/). */
27
- export const BROWSER_ASK_DIR = "browser-ask";
11
+ // way (refused, the tool's result says why) and commits the ask's waiting entry to the run's journal (an agent.handover
12
+ // note, its id a digest of the session's tag and the model's request id: the session's tag, the instruction, the
13
+ // done-when condition, when it was asked and when it expires; never the live view's link or token). That entry is the
14
+ // ask's one record: no file holds it, so nothing the run's agent reaches (its workspace, its file tools, the run's file
15
+ // list) can read, change or plant an ask. The server reads the journal (browser-commands.ts): the run is
16
+ // input_required/waiting with `pending.kind` browser, and run.needs_input fires. The owner answers POST
17
+ // /v2/runs/{id}/browser/hand-back, a command row the runner applies as the ask's answered entry (the first answer wins);
18
+ // the live view's hand-back and the ask's expiry are answered entries this process writes the same way. pi-browser's wait
19
+ // reads the answer through `outcome`; a poll here ends any take-over at once and drops the hold. An ask outlives a process
20
+ // that dies: the run's next claim reads it back from the journal and takes the wait up (`adopt`).
21
+ import { createHash } from "node:crypto";
22
+ import { HOST_PHASE } from "./timeline/index.js";
28
23
  /** How long a person's time lasts at most before it expires: an unanswered ask, and a take-over with no input. Its worth of session time is held up front. */
29
24
  export const PERSON_HOLD_MS = 15 * 60_000;
30
25
  /** How long a take-over outlasts its viewer. */
31
26
  export const PERSON_AWAY_MS = 2 * 60_000;
32
27
  /** Under `--faux` only (the tests, beside the fake browser's own FAUX_BROWSER_LOG): the two windows above, in ms, `<hold>,<away>`. */
33
28
  export const FAUX_PERSON_MS_ENV = "FAUX_BROWSER_PERSON_MS";
34
- /** For tests (under `--faux`): a file's path; the run's process dies once (making the file) right after it writes an ask's
35
- * record and before it commits the ask's row, as a crash there would. */
36
- export const FAUX_ASK_DIES_ENV = "FAUX_BROWSER_ASK_DIES";
37
- /** An ask's id: 32 characters (askIdOf). A record named otherwise (an earlier build's 22, named by the request id alone,
38
- * shared across sessions) is no ask: never read, shown, answered or taken up. */
29
+ /** An ask's id: 32 characters (askIdOf). An id of another shape (an earlier build's 22, named by the request id alone,
30
+ * shared across sessions) is no ask: never read, answered or taken up. */
39
31
  const ID = /^[A-Za-z0-9_-]{32}$/;
40
32
  const TAG = /^[A-Za-z0-9._:-]{1,200}$/;
41
- const OUTCOMES = ["done", "cancelled", "expired"];
42
33
  /** What the person sees of an instruction or a condition, at most. */
43
34
  const MAX_TEXT = 2000;
44
- /** The ask's id: pi-browser's request id is the model's, so the file is named by a digest of it, never by it; and of the
35
+ /** The ask's id: pi-browser's request id is the model's, so the ask is named by a digest of it, never by it; and of the
45
36
  * session's tag with it, since two conversations of one run (each its own session) may pick the same request id, and
46
37
  * each ask is its session's own: its instruction, its answer, its take-over. */
47
38
  export const askIdOf = (sessionTag, requestId) => createHash("sha256").update(`${sessionTag}\n${requestId}`).digest("base64url").slice(0, 32);
48
- /** Whether `at` is this user's alone, and not a link: a directory (0700) or a file (0600). */
49
- const ownAlone = (at, dir) => (dir ? at.isDirectory() : at.isFile()) && !at.isSymbolicLink() && (at.mode & 0o077) === 0 && (!process.getuid || at.uid === process.getuid());
50
- /** The run's ask directory when it is this user's alone; made (0700) when `make`. */
51
- function askDir(runDir, make = false) {
52
- const dir = join(runDir, BROWSER_ASK_DIR);
53
- if (make) {
54
- try {
55
- mkdirSync(dir, { mode: 0o700 });
56
- }
57
- catch (error) {
58
- if (error.code !== "EEXIST")
59
- throw error;
60
- }
61
- }
62
- try {
63
- return ownAlone(lstatSync(dir), true) ? dir : undefined;
64
- }
65
- catch {
66
- return undefined;
67
- }
68
- }
69
- /** A JSON file of this user's alone in the ask directory, or undefined: opened without following a link, and checked on
70
- * the handle it reads. */
71
- function readOwn(dir, name) {
72
- let fd;
73
- try {
74
- fd = openSync(join(dir, name), constants.O_RDONLY | constants.O_NOFOLLOW);
75
- return ownAlone(fstatSync(fd), false) ? JSON.parse(readFileSync(fd, "utf8")) : undefined;
76
- }
77
- catch {
78
- return undefined;
79
- }
80
- finally {
81
- if (fd !== undefined)
82
- closeSync(fd);
83
- }
84
- }
85
- /** Writes `value` as `name`, whole, 0600: under a name of its own first, then into place, replacing (`once` false) or only
86
- * when there is none yet (`once`, by a hard link, which never replaces). False when `once` found one there. */
87
- function writeOwn(dir, name, value, once) {
88
- const making = join(dir, `.${randomBytes(6).toString("hex")}.tmp`);
89
- writeFileSync(making, `${JSON.stringify(value)}\n`, { mode: 0o600, flag: "wx" });
90
- try {
91
- if (!once) {
92
- renameSync(making, join(dir, name));
93
- return true;
94
- }
95
- try {
96
- linkSync(making, join(dir, name));
97
- return true;
98
- }
99
- catch (error) {
100
- if (error.code === "EEXIST")
101
- return false;
102
- throw error;
103
- }
104
- }
105
- finally {
106
- try {
107
- unlinkSync(making);
108
- }
109
- catch {
110
- // renamed into place, or gone
111
- }
112
- }
113
- }
114
- const pendingOf = (value, id) => {
115
- const v = value;
116
- const ok = v?.id === id && typeof v.tag === "string" && TAG.test(v.tag) && typeof v.instruction === "string" && typeof v.completion_condition === "string" && Number.isFinite(Date.parse(String(v.asked_at))) && Number.isFinite(Date.parse(String(v.expires_at)));
117
- return ok ? { id, tag: v.tag, instruction: v.instruction, completion_condition: v.completion_condition, asked_at: v.asked_at, expires_at: v.expires_at } : undefined;
118
- };
119
- const answerOf = (value) => {
120
- const v = value;
121
- return v && OUTCOMES.includes(String(v.outcome)) && typeof v.at === "string" ? { outcome: v.outcome, at: v.at, ...(typeof v.reason === "string" ? { reason: v.reason } : {}) } : undefined;
122
- };
123
- /** Every ask the run's directory holds, newest first, each with its answer when it has one. */
124
- function asksIn(runDir) {
125
- if (!existsSync(join(runDir, BROWSER_ASK_DIR)))
126
- return [];
127
- const dir = askDir(runDir);
128
- if (!dir)
129
- return [];
130
- const found = [];
131
- for (const name of readdirSync(dir)) {
132
- const id = /^([A-Za-z0-9_-]{32})\.json$/.exec(name)?.[1];
133
- const ask = id ? pendingOf(readOwn(dir, name), id) : undefined;
134
- if (!ask)
135
- continue;
136
- const answer = answerOf(readOwn(dir, `${ask.id}.answer.json`));
137
- found.push({ ask, ...(answer ? { answer } : {}) });
138
- }
139
- return found.sort((a, b) => Date.parse(b.ask.asked_at) - Date.parse(a.ask.asked_at) || b.ask.id.localeCompare(a.ask.id));
140
- }
141
- /** The ask the run waits on: the newest unanswered one that has not expired, or undefined. Read where a run's status is
142
- * read (runs.ts statusOf), so a run with no ask costs one existence check. */
143
- export function openAsk(runDir, now = Date.now()) {
144
- const waiting = waitingAsk(runDir);
145
- return waiting && Date.parse(waiting.expires_at) > now ? waiting : undefined;
146
- }
147
- /** The newest unanswered ask, expired or not: one a process must still take up (answer it, or write its expiry), as a
148
- * restarted run's process does. */
149
- export function waitingAsk(runDir) {
150
- try {
151
- return asksIn(runDir).find(({ answer }) => !answer)?.ask;
152
- }
153
- catch {
154
- return undefined;
155
- }
156
- }
157
- /** Whether the run's directory still holds an ask's record, answered or not: one its agent's call may still be waiting
158
- * on (the records go when the run ends, or when its process lets its sessions go). */
159
- export function asked(runDir) {
160
- try {
161
- return asksIn(runDir).length > 0;
162
- }
163
- catch {
164
- return false;
165
- }
166
- }
167
- /** The ask the run waits on (`which`: "open", unexpired, as its status reads it; "waiting", expired or not, as a process
168
- * must still take up) once its own waiting row is in `rows`: the run's process writes the record first, then commits the
169
- * row. Each ask on a session writes one waiting row, in the order asked, so the nth ask on a session is in once that
170
- * session has n waiting rows, whatever other asks (another conversation's, answered or not) wrote meanwhile. */
171
- export function askShown(runDir, rows, which = "open") {
172
- const ask = which === "open" ? openAsk(runDir) : waitingAsk(runDir);
173
- if (!ask)
174
- return undefined;
175
- const on = asksIn(runDir).map(({ ask: each }) => each).filter((each) => each.tag === ask.tag).reverse();
176
- const nth = on.findIndex((each) => each.id === ask.id) + 1;
177
- return rows.filter((row) => row.kind === "waiting" && row.session === ask.tag).length >= nth ? ask : undefined;
178
- }
179
- /** The answer to ask `id`, or the first answer written now (at `at`, ms): `outcome` unless one is there already. */
180
- function answerOnce(dir, id, outcome, reason, at = Date.now()) {
181
- if (writeOwn(dir, `${id}.answer.json`, { outcome, at: new Date(at).toISOString(), ...(reason ? { reason } : {}) }, true))
182
- return outcome;
183
- return answerOf(readOwn(dir, `${id}.answer.json`))?.outcome ?? outcome;
184
- }
185
- /** The owner's answer to the run's ask: the newest one still waiting, as `openAsk` reads it (two conversations may each
186
- * ask), written when it is the first (`first`); with none waiting, the answer the newest one has. An ask past its expiry
187
- * has `expired`. Undefined when the run has asked nothing. */
188
- export function answerAsk(runDir, outcome, now = Date.now()) {
189
- const asks = asksIn(runDir);
190
- const target = asks.find(({ answer }) => !answer) ?? asks[0];
191
- const dir = askDir(runDir);
192
- if (!target || !dir)
193
- return undefined;
194
- if (target.answer)
195
- return { outcome: target.answer.outcome, first: false };
196
- if (Date.parse(target.ask.expires_at) <= now)
197
- return { outcome: answerOnce(dir, target.ask.id, "expired", undefined, now), first: false };
198
- const answered = answerOnce(dir, target.ask.id, outcome, undefined, now);
199
- return { outcome: answered, first: answered === outcome };
200
- }
201
- /** Removes every ask: the run's process that made them is gone. */
202
- export function dropAsks(runDir) {
203
- try {
204
- rmSync(join(runDir, BROWSER_ASK_DIR), { recursive: true, force: true });
205
- }
206
- catch {
207
- // left for the next start's, or the next exit's
208
- }
209
- }
210
39
  /** The rows a person's turns leave, beside the waiting row an ask writes. */
211
40
  const TAKEN = "You took over the browser";
212
41
  const GIVEN_BACK = "You handed the browser back";
@@ -240,9 +69,10 @@ const WAIT_WHAT = "keeping the browser open while you answer";
240
69
  const WAIT_SESSION_GONE = "the browser session it kept open for the person was let go";
241
70
  /** Why such a wait ends at its bound: the one a browser hand-over's ask waits (holdMs, PERSON_HOLD_MS by default). */
242
71
  const waitExpired = (holdMs) => `the person did not answer within ${holdMs >= 60_000 ? `${Math.round(holdMs / 60_000)} minutes` : `${Math.round(holdMs / 1000)} seconds`}, the bound a browser hand-over waits`;
243
- /** The run's process's side, for the run at `root`: `take` for the live view's takeOver, `handoff` for pi-browser's
244
- * HandoffPort. `gates` is the live view's registry (pi-browser's driving gates); `personTime` holds the person's time
245
- * under the cap (browser-cost.ts); `note` commits a root entry, which reaches the run's timeline. */
72
+ /** The run's process's side: `take` for the live view's takeOver, `handoff` for pi-browser's HandoffPort. `gates` is the
73
+ * live view's registry (pi-browser's driving gates); `personTime` holds the person's time under the cap
74
+ * (browser-cost.ts); `note` commits a root entry, which reaches the run's timeline. Every ask and every answer is an
75
+ * entry of the run's journal, read back through `asks` and `answered`. */
246
76
  export function personHands(options) {
247
77
  const holdMs = options.holdMs ?? PERSON_HOLD_MS;
248
78
  const timers = options.timers ?? { set: (run, ms) => setTimeout(run, ms).unref(), clear: (handle) => clearTimeout(handle) };
@@ -255,24 +85,40 @@ export function personHands(options) {
255
85
  let closed = false;
256
86
  const CLOSED = { refused: "the run's process is letting go of the browser" };
257
87
  const note = (kind, data) => options.note(kind, data).catch(() => undefined);
258
- /** The run's journal holds the answers (options.answered): the runner's path. */
259
- const journal = options.answered;
88
+ /** Each ask as the journal has it (its waiting entry), once read or made here: an ask never changes. */
89
+ const known = new Map();
90
+ /** The asks the journal holds, read into `known`; a read that fails leaves what is known. */
91
+ const readAsks = async () => {
92
+ for (const ask of await options.asks().catch(() => []))
93
+ if (ID.test(ask.id) && TAG.test(ask.tag))
94
+ known.set(ask.id, ask);
95
+ };
96
+ /** Ask `id` as the journal has it, read when it is not known yet. */
97
+ const recall = async (id) => {
98
+ if (!known.has(id))
99
+ await readAsks();
100
+ return known.get(id);
101
+ };
260
102
  /** Each ask's answer as the journal has it, once read: an answer never changes. */
261
103
  const fromJournal = new Map();
262
104
  /** The answer this process is writing for an ask, if any: a read waits for it. */
263
105
  const writing = new Map();
264
106
  /** Ask `id`'s answer from the journal, read through. */
265
107
  const journalRead = async (id) => {
266
- const known = fromJournal.get(id);
267
- if (known || !journal)
268
- return known;
269
- const found = await journal(id).catch(() => undefined);
108
+ const had = fromJournal.get(id);
109
+ if (had)
110
+ return had;
111
+ const found = await options.answered(id).catch(() => undefined);
270
112
  if (found)
271
113
  fromJournal.set(id, found);
272
114
  return found;
273
115
  };
116
+ /** The live view's hand-backs still finding the ask they answer (givenDone): a read of an answer waits for them, so the
117
+ * agent's wait hears the hand-back as soon as the person gave it. */
118
+ const giving = new Set();
274
119
  /** Ask `id`'s answer from the journal, once any this process is writing is in. */
275
120
  const readJournal = async (id) => {
121
+ await Promise.all([...giving]);
276
122
  await writing.get(id);
277
123
  return journalRead(id);
278
124
  };
@@ -445,48 +291,40 @@ export function personHands(options) {
445
291
  };
446
292
  /** Ask `id` ended at the run's cap (`words`): expired, written as its answer (the first, as any is), its wait told why. */
447
293
  const capAsk = (id, words) => {
448
- const dir = askDir(options.root);
449
- if (!dir || settled.has(id))
294
+ if (settled.has(id))
450
295
  return;
451
296
  capped.set(id, words);
452
- if (journal) {
453
- // The runner's path: the end is the journal's answer, now, unless the ask has one there already.
454
- const tag = open.get(id)?.tag ?? pendingOf(readOwn(dir, `${id}.json`), id)?.tag;
455
- if (tag)
456
- void answerInJournal(id, tag, "expired", new Date(now()).toISOString(), words).then(() => settle(id), () => undefined);
457
- return;
458
- }
459
- answerOnce(dir, id, "expired", words, now());
460
- settle(id);
297
+ // The end is the journal's answer, now, unless the ask has one there already.
298
+ const tag = open.get(id)?.tag ?? known.get(id)?.tag;
299
+ if (tag)
300
+ void answerInJournal(id, tag, "expired", new Date(now()).toISOString(), words).then(() => settle(id), () => undefined);
461
301
  };
462
- /** Session `tag`'s newest ask still waiting (unanswered, unexpired), answered done: the person handed that session's
463
- * browser back. Its hold goes with it (settle), and another session's ask is never touched. True when an ask was
464
- * settled here, whose row (settle's) says the hand-back. */
465
- const givenDone = (tag) => {
466
- const dir = askDir(options.root);
467
- // On the runner's path an ask's answer is the journal's (an answer file is never read there).
468
- const unanswered = (ask, answer) => (journal ? !fromJournal.has(ask.id) : !answer);
469
- const waiting = dir ? asksIn(options.root).find(({ ask, answer }) => unanswered(ask, answer) && ask.tag === tag && Date.parse(ask.expires_at) > Date.now())?.ask : undefined;
470
- if (!dir || !waiting || settled.has(waiting.id))
471
- return false;
472
- if (journal) {
302
+ /** Session `tag`'s newest ask still waiting (unanswered, unexpired), as the journal has it, answered done: the person
303
+ * handed that session's browser back. Its hold goes with it (settle), and another session's ask is never touched. True
304
+ * when an ask was answered here, whose row (settle's) says the hand-back. `at` (ms) is when the person handed it back:
305
+ * an ask open then is answered then, though the journal's read finishes after its expiry. */
306
+ const givenDone = async (tag, at) => {
307
+ await readAsks();
308
+ const newest = [...known.values()].filter((ask) => ask.tag === tag).sort((a, b) => Date.parse(b.asked_at) - Date.parse(a.asked_at) || b.id.localeCompare(a.id));
309
+ for (const ask of newest) {
310
+ await writing.get(ask.id);
311
+ if (settled.has(ask.id) || Date.parse(ask.expires_at) <= at || (await journalRead(ask.id)))
312
+ continue;
473
313
  // Written as the journal's answer, now, unless the ask has one there already (a person's answer that came first
474
314
  // writes its own row, settle's, as this one would).
475
- void answerInJournal(waiting.id, tag, "done", new Date(now()).toISOString()).then(() => settle(waiting.id), () => undefined);
315
+ await answerInJournal(ask.id, tag, "done", new Date(at).toISOString()).then(() => settle(ask.id), () => undefined);
476
316
  return true;
477
317
  }
478
- answerOnce(dir, waiting.id, "done", undefined, now());
479
- return settle(waiting.id) !== undefined;
318
+ return false;
480
319
  };
481
- /** Ask `id` as its record on disk has it, taken up by this process: one an earlier process of the run made, whose wait
482
- * this process resumes (pi-browser polls `outcome` again, or reruns `request`). Its time is no longer held (that process
483
- * is gone, and its holds with it). Undefined when there is no such record. */
320
+ /** Ask `id` as the run's journal has it (read into `known` first), taken up by this process: one an earlier process of the
321
+ * run made, whose wait this process resumes (pi-browser polls `outcome` again, or reruns `request`). Its time is no
322
+ * longer held (that process is gone, and its holds with it). Undefined when the journal holds no such ask. */
484
323
  const adopt = (id) => {
485
- const known = open.get(id);
486
- if (known)
487
- return known;
488
- const dir = askDir(options.root);
489
- const kept = dir ? pendingOf(readOwn(dir, `${id}.json`), id) : undefined;
324
+ const taken = open.get(id);
325
+ if (taken)
326
+ return taken;
327
+ const kept = known.get(id);
490
328
  if (!kept)
491
329
  return undefined;
492
330
  const ask = { tag: kept.tag, expiresAt: Date.parse(kept.expires_at), drop() { } };
@@ -511,18 +349,6 @@ export function personHands(options) {
511
349
  }, () => undefined);
512
350
  return ask;
513
351
  };
514
- /** Ask `id`, taken up from an earlier process, whose row that process never committed (it ended between the record and
515
- * the row): the row is committed now, so the run shows the ask and its notice goes out. The nth ask on a session is in
516
- * once the session has n waiting rows, as askShown reads it. An ask answered or expired needs none. */
517
- const reshow = async (id) => {
518
- const dir = askDir(options.root);
519
- const kept = dir && options.waiting ? pendingOf(readOwn(dir, `${id}.json`), id) : undefined;
520
- if (!dir || !kept || settled.has(id) || (journal ? await readJournal(id) : answerOf(readOwn(dir, `${id}.answer.json`))) || Date.parse(kept.expires_at) <= Date.now())
521
- return;
522
- const nth = asksIn(options.root).map(({ ask: each }) => each).filter((each) => each.tag === kept.tag).reverse().findIndex((each) => each.id === id) + 1;
523
- if (nth > 0 && (await options.waiting(kept.tag)) < nth)
524
- await note("agent.handover", waitingEntry(kept));
525
- };
526
352
  /** Ask `id`'s answer, once there is one (the person's, or `expired` once it is due, written here): its hold dropped, any
527
353
  * take-over of its session ended, and its row written, once. An ask this process did not make is taken up first. */
528
354
  const settle = (id) => {
@@ -530,12 +356,11 @@ export function personHands(options) {
530
356
  if (known)
531
357
  return known;
532
358
  const ask = adopt(id);
533
- const dir = askDir(options.root);
534
- if (!ask || !dir)
359
+ if (!ask)
535
360
  return undefined;
536
- // On the runner's path the answer is the journal's, as last read (refresh writes a lapsed ask's expiry there first).
537
- const written = journal ? fromJournal.get(id) : answerOf(readOwn(dir, `${id}.answer.json`));
538
- const answered = written?.outcome ?? (!journal && Date.now() >= ask.expiresAt ? answerOnce(dir, id, "expired", undefined, now()) : undefined);
361
+ // The answer is the journal's, as last read (refresh writes a lapsed ask's expiry there first).
362
+ const written = fromJournal.get(id);
363
+ const answered = written?.outcome;
539
364
  if (!answered)
540
365
  return undefined;
541
366
  // The cap's words, kept with the answer: a process that takes the ask up after a restart tells them too.
@@ -553,10 +378,10 @@ export function personHands(options) {
553
378
  (clearInterval(poll), (poll = undefined));
554
379
  return answered;
555
380
  };
556
- /** On the runner's path, before settle reads ask `id`: its answer read from the journal, or, once it lapsed with none,
557
- * its expiry written there at the expiry's time (the first answer stands, whoever's it is). */
381
+ /** Before settle reads ask `id`: its answer read from the journal, or, once it lapsed with none, its expiry written there
382
+ * at the expiry's time (the first answer stands, whoever's it is). */
558
383
  const refresh = async (id) => {
559
- if (!journal || settled.has(id) || (await readJournal(id)))
384
+ if (settled.has(id) || (await readJournal(id)))
560
385
  return;
561
386
  const ask = adopt(id);
562
387
  if (ask && Date.now() >= ask.expiresAt)
@@ -593,23 +418,30 @@ export function personHands(options) {
593
418
  },
594
419
  end: (why) => {
595
420
  if (ended)
596
- return;
421
+ return Promise.resolve();
597
422
  ended = true;
598
423
  const words = ledgers.get(tag)?.refused;
599
424
  release(tag, TAKE_USER);
600
425
  // Ended before it went live (the agent's call still running): the person never had the browser, and no row says
601
426
  // they took it, so none says how it came back. An answered ask writes its own row.
602
427
  if (!live)
603
- return;
428
+ return Promise.resolve();
604
429
  // Handed back by the person (the viewer's Hand back): their word that they are done, so the agent's open ask on
605
430
  // this session is answered done, as the hand-back route would answer it (first answer wins). Never on any other
606
431
  // end (cap, idle, away, released, answered): the person did not say they were done. The answer's own row
607
432
  // ("You handed the browser back", settle's) is then the hand-back's one row: none is written here as well.
608
- const answeredAsk = why === "given" && givenDone(tag);
433
+ const at = now();
609
434
  if (why === "cap")
610
- void note(HOST_PHASE, { summary: `${CAPPED}${words ? `: ${words}` : ""}`, at: now() });
611
- else if (why !== "answered" && !answeredAsk)
612
- void note(HOST_PHASE, { summary: TAKE_ENDED[why], at: now() });
435
+ return note(HOST_PHASE, { summary: `${CAPPED}${words ? `: ${words}` : ""}`, at });
436
+ if (why === "answered")
437
+ return Promise.resolve();
438
+ if (why !== "given")
439
+ return note(HOST_PHASE, { summary: TAKE_ENDED[why], at });
440
+ // The session's open ask is read from the run's journal first; settled once the hand-back's row is written.
441
+ const given = givenDone(tag, at).then((answeredAsk) => (answeredAsk ? undefined : note(HOST_PHASE, { summary: TAKE_ENDED.given, at })), () => undefined);
442
+ giving.add(given);
443
+ void given.finally(() => giving.delete(given));
444
+ return given;
613
445
  },
614
446
  };
615
447
  },
@@ -622,15 +454,10 @@ export function personHands(options) {
622
454
  const id = askIdOf(ask.sessionTag, ask.requestId);
623
455
  if (settled.has(id) || open.has(id))
624
456
  return { interactionId: id };
625
- const dir = askDir(options.root, true);
626
- if (!dir)
627
- return { refused: "the run's directory for a hand-over is not this server's alone" };
628
- // An ask an earlier process of this run made: waited on again, its time no longer held (that process is gone), and
629
- // its row committed if that process ended before it could.
630
- if (adopt(id)) {
631
- await reshow(id);
457
+ // An ask an earlier process of this run made (its waiting entry is in the journal): waited on again, its time no
458
+ // longer held (that process is gone).
459
+ if ((await recall(id)) && adopt(id))
632
460
  return { interactionId: id };
633
- }
634
461
  // A take-over of the same session holds the person's time already: the ask shares it.
635
462
  const user = `ask:${id}`;
636
463
  const refused = await acquire(ask.sessionTag, user, "handing the browser to you", true, now() + holdMs);
@@ -641,19 +468,11 @@ export function personHands(options) {
641
468
  // Redacted whole, then cut to their bound: a held value the cut would split never leaves its head in the record.
642
469
  const shown = (text) => (options.redact ? options.redact(text) : text).slice(0, MAX_TEXT);
643
470
  const record = { id, tag: ask.sessionTag, instruction: shown(ask.instruction), completion_condition: shown(ask.completionCondition), asked_at: new Date(at).toISOString(), expires_at: new Date(at + holdMs).toISOString() };
644
- try {
645
- writeOwn(dir, `${id}.json`, record, false);
646
- }
647
- catch (error) {
648
- hold.drop();
649
- throw error;
650
- }
651
471
  open.set(id, { tag: ask.sessionTag, expiresAt: at + holdMs, drop: () => hold.drop() });
652
472
  watch();
653
- options.afterRecord?.();
654
- // The record is in place before the row: the row's commit is what makes the server read the run's status again.
655
- // The row is what shows the person the ask (askShown): one that cannot be written fails the request, plainly, and
656
- // leaves nothing behind (its hold, its open entry, its record), so the agent's next request makes it afresh.
473
+ // The ask's waiting entry is its one record and its row: its commit is what shows the person the ask and makes the
474
+ // server read the run's status again. One that cannot be written fails the request, plainly, and leaves nothing
475
+ // behind (its hold, its open entry), so the agent's next request makes it afresh.
657
476
  try {
658
477
  await options.note("agent.handover", waitingEntry(record));
659
478
  }
@@ -663,14 +482,9 @@ export function personHands(options) {
663
482
  open.delete(id);
664
483
  if (!open.size && poll)
665
484
  (clearInterval(poll), (poll = undefined));
666
- try {
667
- unlinkSync(join(dir, `${id}.json`));
668
- }
669
- catch {
670
- // gone already
671
- }
672
485
  return { refused: "the hand-over could not be shown to the person (its request was not written to the run's timeline): nothing is held for it, and it can be asked again" };
673
486
  }
487
+ known.set(id, record);
674
488
  return { interactionId: id };
675
489
  },
676
490
  async outcome(interactionId) {
@@ -678,6 +492,7 @@ export function personHands(options) {
678
492
  // the agent's wait ends (it may ask again), never polled forever or matched to another session's record.
679
493
  if (!ID.test(interactionId))
680
494
  return "expired";
495
+ await recall(interactionId);
681
496
  await refresh(interactionId);
682
497
  const answered = settle(interactionId) || "pending";
683
498
  // Ended at the cap: the agent is told so, in the cap's words.
package/dist/budget.d.ts CHANGED
@@ -4,8 +4,6 @@ export declare const CAP_RAISED = "cap.json";
4
4
  export declare const CAP_STOPPED = "cap-stopped.json";
5
5
  /** How long a workflow run's pause keeps its process before it lets it go (the lead's ruling: 10 minutes). */
6
6
  export declare const BUDGET_HOLD_MS: number;
7
- /** The process's environment variable naming another hold, in ms (the server's `budgetHold` option, for its tests). */
8
- export declare const BUDGET_HOLD_ENV = "AGENTRUN_SERVE_BUDGET_HOLD_MS";
9
7
  /** Where a step stands in its workflow: its node's label and execution path. */
10
8
  export type StepAt = {
11
9
  label: string;
@@ -40,8 +38,6 @@ export declare class CapStop extends Error {
40
38
  }
41
39
  /** The pause the run at `root` stands at, as its process wrote it; undefined when it waits at none. */
42
40
  export declare const budgetPausedIn: (root: string) => BudgetMark | undefined;
43
- /** The refusal that ended the run at `root` for good, as its process wrote it; undefined when none did. */
44
- export declare const capStoppedIn: (root: string) => CapStopped | undefined;
45
41
  /** A raise's cap, for the run's process to read (the server writes it after serve.json keeps it). */
46
42
  export declare const writeRaisedCap: (root: string, usd: number) => void;
47
43
  /** The pause's mark taken away: a raise sent its run on, here when no process waits at it (it was let go). */
package/dist/budget.js CHANGED
@@ -24,8 +24,6 @@ export const CAP_RAISED = "cap.json";
24
24
  export const CAP_STOPPED = "cap-stopped.json";
25
25
  /** How long a workflow run's pause keeps its process before it lets it go (the lead's ruling: 10 minutes). */
26
26
  export const BUDGET_HOLD_MS = 10 * 60_000;
27
- /** The process's environment variable naming another hold, in ms (the server's `budgetHold` option, for its tests). */
28
- export const BUDGET_HOLD_ENV = "AGENTRUN_SERVE_BUDGET_HOLD_MS";
29
27
  const POLL_MS = 250;
30
28
  /** A step its cap refused for good: thrown where the step stood, never an item's failure (it is no EffectFailure and
31
29
  * declares no itemFailure), so the workflow fails on it and the server reads `cap-stopped.json`. */
@@ -57,11 +55,6 @@ export const budgetPausedIn = (root) => {
57
55
  const mark = readJson(join(root, BUDGET_PAUSE));
58
56
  return mark && typeof mark.words === "string" && typeof mark.cap_usd === "number" ? mark : undefined;
59
57
  };
60
- /** The refusal that ended the run at `root` for good, as its process wrote it; undefined when none did. */
61
- export const capStoppedIn = (root) => {
62
- const stop = readJson(join(root, CAP_STOPPED));
63
- return stop && typeof stop.words === "string" ? stop : undefined;
64
- };
65
58
  /** A raise's cap, for the run's process to read (the server writes it after serve.json keeps it). */
66
59
  export const writeRaisedCap = (root, usd) => writeWhole(join(root, CAP_RAISED), { max_cost_usd: usd });
67
60
  /** The pause's mark taken away: a raise sent its run on, here when no process waits at it (it was let go). */
package/dist/cap.d.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { type Ended } from "@parcha/agentrun-kernel";
3
- import { type EntryLike } from "@parcha/pi-durable-agent/timeline";
3
+ import { type EntryLike } from "./timeline/index.js";
4
4
  import type { Api, AssistantMessage, Context, Model, Models } from "@earendil-works/pi-ai";
5
- /** A run's cap in USD, passed to its process. */
6
- export declare const CAP_ENV = "AGENTRUN_SERVE_MAX_COST_USD";
7
5
  /** How a request the cap refused begins its error, which the server ends the run with. */
8
6
  export declare const SPEND_CAP_REACHED = "spend_cap_reached";
9
7
  /** How near two dollar figures are the same figure: a sum a float puts a hair past the cap (0.0028 + 0.3072 is
@@ -27,10 +25,6 @@ export declare const BOUND_FIELD = "boundUsd";
27
25
  * case, so no request can pass it. */
28
26
  export declare const CAP_MODES: readonly ["expected", "strict"];
29
27
  export type CapMode = (typeof CAP_MODES)[number];
30
- /** A run's cap mode, passed to its process; absent is "expected". */
31
- export declare const CAP_MODE_ENV = "AGENTRUN_SERVE_CAP_MODE";
32
- /** The run's cap mode from its process's environment. */
33
- export declare function capModeOf(env: NodeJS.ProcessEnv): CapMode;
34
28
  /** How long after a request ended the next one in its conversation is expected to read its cache: 4 minutes, inside
35
29
  * Anthropic's 5 (a cache kept that long from its last use; OpenAI's lasts longer). A request later than that is priced
36
30
  * with nothing cached. */
@@ -192,8 +186,6 @@ export type SideRequest = {
192
186
  feeUsd(): number;
193
187
  };
194
188
  export declare const sideRequests: WeakMap<object, SideRequest>;
195
- /** The run's cap from its process's environment; absent for a run without one. */
196
- export declare function capOf(env: NodeJS.ProcessEnv): number | undefined;
197
189
  /** The code a cap's refusal begins with: `spend_cap_reached: `, or with a note after it, as a request it limited refuses
198
190
  * in (CAP_REFUSAL_CODE, "spend_cap_reached (out of budget): "). Every reader of a refusal's text (a run's end, its
199
191
  * learning, the spend read back) reads the code through this, never a bare prefix. */
@@ -269,6 +261,13 @@ export declare function meterFiguresIn(storeFile: string): Meters & {
269
261
  others: number;
270
262
  };
271
263
  };
264
+ /** A browser charge's record on the run's root when the run books its money on the runner's one ledger (browser-cost.ts
265
+ * browserSpend's `paid`): the same record a browser TOOL_COST is, for what the browser's process holds and the seconds
266
+ * it was for, under a kind no cost reads. The charge's money is the ledger's paid step alone, which the cap counts and
267
+ * the run's cost (adapter-run-spec.ts endPatch), its timeline and its meters (store-reads/journal.ts withPaidCosts) read,
268
+ * as every paid tool's on the runner: a TOOL_COST beside it would count the same charge twice. Its seconds are the
269
+ * browser_time meter's (meterFiguresFrom). */
270
+ export declare const BROWSER_CHARGED = "agentrun.browser_charged";
272
271
  /** meterFiguresIn's figures from a run's entries by conversation, in log order: the one computation, shared by
273
272
  * meterFiguresIn (reading a settled run's own database) and the one-store reader (store-reads/cap.ts metersOverStore, over
274
273
  * A's journal). Only where the entries come from changes; the figures do not (the money-parity rule), as spentFrom is to
@@ -414,9 +413,6 @@ export type RefusalFigures = {
414
413
  leastCapUsd: number;
415
414
  boundUsd?: number;
416
415
  };
417
- /** A refusal's figures as its start mark keeps them (`spentUsd`, `needsUsd`, `leastCapUsd`, and `boundUsd` when above 0);
418
- * none without. */
419
- export declare const refusalFields: (figures: RefusalFigures | undefined) => Record<string, number>;
420
416
  /** The words of a refusal for room (CAP_REFUSAL_CODE: what is left of the cap cannot buy the request's least), which a
421
417
  * higher cap would send; undefined for any other text, a refusal no cap could fix included (an unknown spend, no price,
422
418
  * no output ceiling, an input not measured: the bare `spend_cap_reached: ` code). The one reader of that difference. */