@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
@@ -1,21 +1,22 @@
1
1
  import { ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
2
- import { TOOL_COST } from "@parcha/pi-durable-agent/timeline";
3
- import { BARRIER, syncNow } from "@parcha/pi-durable-disk";
4
- import { addSpend, keepHold, paidCall, runCap, SPEND_CAP_REACHED } from "./cap.js";
5
- import { paidStep, runJournal } from "./paid-calls.js";
2
+ import { TOOL_COST } from "./timeline/index.js";
3
+ import { addSpend, BROWSER_CHARGED, keepHold, paidCall, runCap, SPEND_CAP_REACHED } from "./cap.js";
4
+ import { paidStep } from "./paid-calls.js";
6
5
  import { logLine } from "./print.js";
7
6
  import { forModel } from "./result.js";
8
7
  /** The run journal's record of a browser session about to be created at its provider: its tag (pi-browser's, which a
9
8
  * reconcile finds the vendor's session by), its conversation and the hold its call keeps for it (`held_usd`, null with
10
- * no cap). Written with pi-browser's own `creating` record and synced (barriers.ts browserSessionOpen) before the vendor
11
- * is asked, so a box loss never leaves a session billed that no record in the store names. */
9
+ * no cap). Written with pi-browser's own `creating` record and flushed before the vendor is asked, so a lost box never
10
+ * leaves a session billed that no record in the store names. */
12
11
  export const BROWSER_SESSION_OPEN = "agentrun.browser_session_open";
13
12
  /** List prices by pi-browser provider name (`provider.name`): Browserbase's (pi-browser's LIST_PRICES), $0.12 an hour with
14
- * a one-minute minimum; Kernel's headless browser, about $0.06 an hour billed by the second; a local Chrome (`cdp`), free. */
13
+ * a one-minute minimum; Kernel's headless browser, about $0.06 an hour billed by the second; a local Chrome (`cdp`, and
14
+ * the `local` computer provider's), free. */
15
15
  export const BROWSER_PRICES = Object.freeze({
16
16
  browserbase: { usdPerHour: 0.12, minimumS: 60 },
17
17
  kernel: { usdPerHour: 0.06, minimumS: 0 },
18
18
  cdp: { usdPerHour: 0, minimumS: 0 },
19
+ local: { usdPerHour: 0, minimumS: 0 },
19
20
  });
20
21
  /** An operator's price for a provider, in USD an hour: this prefix and the provider's name in capitals
21
22
  * (AGENTRUN_BROWSER_USD_PER_HOUR_BROWSERBASE, _KERNEL, _CDP). */
@@ -36,34 +37,43 @@ export function browserPrice(provider, env) {
36
37
  }
37
38
  /** Up to the millionth of a dollar, so the cap's words name a figure never below the hold. */
38
39
  const microUsd = (usd) => Math.ceil(Math.round(usd * 1e12) / 1e6) / 1e6;
40
+ export { BROWSER_CHARGED };
41
+ /** The tool a browser step on the run's one ledger names (main's charge's `tool`, the timeline's BROWSER_CHARGE): the run
42
+ * view folds its charge into its call's row, as main's. */
43
+ const BROWSER_TOOL = "browser";
39
44
  /** One run's browser bookings, for its one provider at `price` with pi-browser's idle window `idleS`. `release` lets go of
40
45
  * a conversation's session (pi-browser's release, "close"). `log` takes the line that says a charge went uncounted
41
46
  * (default: the server's stderr). */
42
47
  export function browserSpend(provider, price, idleS, release, log = (line) => void process.stderr.write(`agentrun-serve: ${line}\n`), ledger = {}) {
43
- /** The runner's one ledger (the kernel cap's paidCalls), when the run has one: each browser call is one paid request on
44
- * it, held at the hold main takes for it and settled at the charges booked under it; a charge under no call (a
45
- * session's release, its top-up to the minute) is one more, settled at that charge. In place of this process's spend. */
48
+ /** The runner's one ledger (the kernel cap's paidCalls), when the run has one. Its holds are hold-only steps (`hold`:
49
+ * money reserved, never spent, given back when let go or when a claim is lost): each browser call's, held at the hold
50
+ * main takes for it, a session's reserve, a person's time. Its money is its charges: each one paid step of its own,
51
+ * owed (its time is spent at the provider: never refused, however the run's spend stands), settled at the charge,
52
+ * named by its session and time (`ref`) so it is booked once, and booked once its commit is in (booked); a reopen books
53
+ * any the root holds and the ledger lacks (reopened). In place of this process's spend. Every step names its tool
54
+ * ("browser") and, where it has them, its call and session, so the run view folds it as main's charge. */
46
55
  const paid = ledger.paid;
47
- /** Each browser call under way on the ledger: what has been charged under it (null: a charge with no price). */
48
- const chargedUnder = new Map();
49
- /** The ledger steps of charges under no call, until they are written (ledgerIdle). */
56
+ /** The ledger steps of charges and holds, until they are written (ledgerIdle). */
50
57
  const pending = new Set();
51
58
  let ledgerSeq = 0;
52
- /** A hold of `usd` under `name`: on the run's one ledger when it is given (a held paid step, settled at nothing when the
53
- * hold is let go, the time it held being booked by the session's own charge), else in this process's holds (keepHold).
54
- * On the ledger nothing is written to this process's holds or spend. `admitted`: the ledger's answer (refused in the
55
- * cap's words when it has no room). */
56
- const holdOf = (name, usd, basis) => {
59
+ /** A hold of `usd` under `name`: on the run's one ledger when it is given (a held paid step, given back when the hold is
60
+ * let go, the time it held being booked by the session's own charge), else in this process's holds (keepHold). On the
61
+ * ledger nothing is written to this process's holds or spend. `admitted`: the ledger's answer (refused in the cap's
62
+ * words when it has no room). `owed`: time its provider is already owed, run outside any call (a person's carried
63
+ * time, a session held from a reopen, a delayed release), held as main's keepHold holds it: never refused. A fresh
64
+ * session's reserve and a person's time to come (personTime) are not owed: the cap may refuse them. */
65
+ const holdOf = (name, usd, basis, options = {}) => {
57
66
  if (!paid)
58
67
  return { hold: keepHold(usd), admitted: Promise.resolve({}) };
59
68
  const booking = paid;
60
69
  const id = `browser:${name}#${(ledgerSeq += 1)}`;
61
- const admitted = booking.admit({ id, name, holdUsd: usd, basis, chargedBy: provider }).then(() => ({}), (error) => {
70
+ const admitted = booking.admit({ id, name, holdUsd: usd, basis, chargedBy: provider, tool: BROWSER_TOOL, hold: true, ...(options.tag ? { tag: options.tag } : {}), ...(options.owed ? { owed: true } : {}) }).then(() => ({}), (error) => {
62
71
  const capped = error?.code === "spend_cap_reached";
63
72
  const refused = capped ? String(error.message) : `${name} could not be held: ${String(error?.message ?? error)}`;
64
73
  // The cap's own refusal, as the run's pause reads it (a room refusal's figures; a refusal for good).
65
74
  const given = error;
66
- return { refused, ...(capped ? { refusal: { refuse: refused, forGood: given.forGood ?? true, ...(given.figures ? { figures: given.figures } : {}) } } : {}) };
75
+ // Its code rides with it (Refusal.code): the model request the pause refuses next is the cap's refusal.
76
+ return { refused, ...(capped ? { refusal: { refuse: refused, code: SPEND_CAP_REACHED, forGood: given.forGood ?? true, ...(given.figures ? { figures: given.figures } : {}) } } : {}) };
67
77
  });
68
78
  pending.add(admitted);
69
79
  void admitted.finally(() => pending.delete(admitted));
@@ -74,7 +84,8 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
74
84
  if (dropped)
75
85
  return;
76
86
  dropped = true;
77
- const settled = admitted.then((answer) => (answer.refused === undefined ? booking.settle(id, 0) : undefined)).catch(() => undefined);
87
+ // Given back: a hold is no charge (its time, when it ran, is booked by the session's own charge), never a $0 one.
88
+ const settled = admitted.then((answer) => (answer.refused === undefined ? booking.settle(id, 0, { givenBack: true }) : undefined)).catch(() => undefined);
78
89
  pending.add(settled);
79
90
  void settled.finally(() => pending.delete(settled));
80
91
  },
@@ -135,32 +146,37 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
135
146
  const calls = new Map();
136
147
  const take = (hold, usd) => void (hold && (hold.usd = Math.max(0, hold.usd - usd)));
137
148
  /** Booked in the commit that bills it: a TOOL_COST under the call that ran it (a release's, under its session's last
138
- * call), what it cost or null when its time has no price, and the seconds it was for, with what `booked` needs. A free
139
- * provider's time books nothing and holds nothing. */
149
+ * call; on the runner's ledger, a BROWSER_CHARGED, its money the ledger step's), what it cost or null when its time
150
+ * has no price, and the seconds it was for, with what `booked` needs. A free provider's time books nothing and holds
151
+ * nothing. */
140
152
  async function onCharge(charge, tx, call) {
141
153
  if (rate === 0)
142
154
  return;
143
155
  const usd = charge.state === "priced" ? charge.usd : null;
144
- if (paid) {
145
- const under = call?.callId;
146
- if (under !== undefined && chargedUnder.has(under)) {
147
- const so = chargedUnder.get(under) ?? null;
148
- chargedUnder.set(under, so === null || usd === null ? null : so + usd);
149
- }
150
- else {
151
- // A charge under no call under way (the release's, its top-up to the minute): one more paid request on the ledger,
152
- // held at nothing (its time is spent already) and settled at the charge.
153
- const step = paidStep(paid, `${charge.tag}${charge.final ? "@release" : "@charge"}`, { name: "this browser session's time", holdUsd: 0, basis: `${provider}'s session time, billed as it was charged`, by: charge.provider }, async () => undefined, () => usd).catch(() => undefined);
154
- pending.add(step);
155
- void step.finally(() => pending.delete(step));
156
- }
157
- }
158
156
  const data = { tool: "browser", charged_by: charge.provider, call: call?.callId ?? sessions.get(charge.tag)?.call ?? null, usd, seconds: charge.seconds, tag: charge.tag, conversation: charge.conversationId, at: charge.at, final: charge.final };
159
- await tx.appendEntry(ROOT_CONVERSATION_ID, { kind: TOOL_COST, data });
157
+ // On the ledger, its money is the ledger's, booked once this commit is in (booked): the root keeps the record under a
158
+ // kind no cost reads.
159
+ await tx.appendEntry(ROOT_CONVERSATION_ID, { kind: paid ? BROWSER_CHARGED : TOOL_COST, data });
160
+ }
161
+ /** A charge on the run's one ledger, once its commit is in: one paid step of its own, owed (its time is spent at the
162
+ * provider, so the cap never refuses it, however the run's spend stands), settled at the charge, under its call (the
163
+ * one that ran it, else its session's last; none after a restart that lost it) and named by its session and time, so
164
+ * it is booked once whoever books it (this, or a reopen's: reopened). One that cannot be booked (its claim lost, a
165
+ * write that failed) is said in the log, and booked by the next claim's reopen. */
166
+ function bookOnLedger(booking, data) {
167
+ // Held at its charge (known: it was billed): a claim lost before its settle leaves it booked at that, never unknown
168
+ // (the kernel's bookCut books an owed step at its hold). A charge of an unknown amount holds nothing.
169
+ const owes = typeof data.usd === "number" && Number.isFinite(data.usd) && data.usd > 0 ? data.usd : 0;
170
+ const step = paidStep(booking, data.call ?? undefined, { name: "this browser session's time", holdUsd: owes, basis: `${provider}'s session time, billed as it was charged`, by: data.charged_by, tool: BROWSER_TOOL, tag: data.tag, owed: true, ref: chargeRef(data) }, async () => undefined, () => data.usd).then((stepped) => void ("refused" in stepped && log(logLine("browser_spend_unbooked", { said: `a browser charge of ${data.tag} was not booked on the run's ledger (${stepped.refused.slice(0, 200)}): the next claim's reopen books it` }))), (error) => void log(logLine("browser_spend_unbooked", { said: `a browser charge of ${data.tag} was not booked on the run's ledger (${String(error?.message ?? error).slice(0, 200)}): the next claim's reopen books it` })));
171
+ pending.add(step);
172
+ void step.finally(() => pending.delete(step));
173
+ return step;
160
174
  }
161
175
  /** A charge whose commit is in (watch): it joins what the run's process counts as spent, and moves the holds. Never
162
176
  * inside the commit, so one that fails changes neither. */
163
177
  function booked(data) {
178
+ if (paid)
179
+ bookOnLedger(paid, data);
164
180
  const usd = data.usd ?? 0;
165
181
  const session = sessions.get(data.tag);
166
182
  spendIn(data.usd);
@@ -191,8 +207,31 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
191
207
  }
192
208
  else {
193
209
  take(data.call === null ? undefined : calls.get(data.call)?.hold, usd + stretch);
194
- sessions.set(data.tag, { cid: data.conversation, reserve: holdOf("this browser session's reserve", stretch, `${provider}'s next stretch of session time, held until its release is booked`).hold, at: through, call: data.call });
210
+ sessions.set(data.tag, { cid: data.conversation, reserve: holdOf("this browser session's reserve", stretch, `${provider}'s next stretch of session time, held until its release is booked`, { tag: data.tag }).hold, at: through, call: data.call });
211
+ }
212
+ }
213
+ /** The one booking a charge names on the ledger: its session, its time, and whether it was the session's last. */
214
+ const chargeRef = (data) => `browser:${data.tag}@${data.at}${data.final ? "/final" : ""}`;
215
+ /** At a reopen, on the runner's one ledger: every charge the root holds (BROWSER_CHARGED) is booked there, once by its
216
+ * ref, so one whose commit landed and whose booking did not (its process lost between the two) is counted now; one
217
+ * booked already books nothing. */
218
+ async function bookRootCharges(harness, context) {
219
+ if (!paid)
220
+ return;
221
+ const root = await harness.conversation(ROOT_CONVERSATION_ID, context);
222
+ if (!root)
223
+ return;
224
+ const charges = [];
225
+ for (let cursor = undefined;;) {
226
+ const page = await root.entries({ order: "ascending" }, 500, cursor, context);
227
+ for (const entry of page.items)
228
+ if (entry.kind === BROWSER_CHARGED)
229
+ charges.push(entry.data);
230
+ if (page.next === undefined)
231
+ break;
232
+ cursor = page.next;
195
233
  }
234
+ await Promise.all(charges.map((data) => bookOnLedger(paid, data)));
196
235
  }
197
236
  /** At a reopen, after pi-browser's reconcile: every session its custody still tracks (each row of its inventory: kept,
198
237
  * unreached, still releasing, lost) is held from here as if opened in this process, at a stretch beside what it ran
@@ -201,6 +240,8 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
201
240
  async function reopened(harness, docs, context) {
202
241
  if (rate === 0)
203
242
  return;
243
+ if (harness.conversation)
244
+ await bookRootCharges(harness, context);
204
245
  const rows = Object.values((await harness.snapshot(docs.Inventory, context))?.rows ?? {});
205
246
  for (const row of rows.filter((r) => !sessions.has(r.tag))) {
206
247
  const record = (await harness.snapshot(docs.Sessions, row.conversationId, context))?.sessions?.find((s) => s.tag === row.tag);
@@ -210,7 +251,7 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
210
251
  continue;
211
252
  // Its reserve carries the down time, so its calls count what it runs from here (`at` now), not the down time again.
212
253
  const now = Date.now();
213
- sessions.set(row.tag, { cid: row.conversationId, reserve: holdOf("this browser session's reserve", stretch + usdOf(Math.max(0, now - at) / 1000), `${provider}'s session time since its last charge and its next stretch, held from the reopen`).hold, at: now, call: null });
254
+ sessions.set(row.tag, { cid: row.conversationId, reserve: holdOf("this browser session's reserve", stretch + usdOf(Math.max(0, now - at) / 1000), `${provider}'s session time since its last charge and its next stretch, held from the reopen`, { owed: true, tag: row.tag }).hold, at: now, call: null });
214
255
  }
215
256
  }
216
257
  /** The open-session inventory as committed: a session it no longer holds lets go of its reserve (released, billed or
@@ -229,7 +270,7 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
229
270
  continue;
230
271
  const usd = usdOf(CREATE_DEADLINE_S + stretchS);
231
272
  take([...calls.values()].find((c) => c.cid === row.conversationId)?.hold, usd);
232
- sessions.set(row.tag, { cid: row.conversationId, reserve: holdOf("this browser session's delayed release", usd, `${provider}'s session time for its create's deadline and a stretch, until its release is billed`).hold, at: Date.now(), call: null });
273
+ sessions.set(row.tag, { cid: row.conversationId, reserve: holdOf("this browser session's delayed release", usd, `${provider}'s session time for its create's deadline and a stretch, until its release is billed`, { owed: true, tag: row.tag }).hold, at: Date.now(), call: null });
233
274
  }
234
275
  }
235
276
  /** Applies each browser charge `harness` commits, and each change of pi-browser's inventory (the document of kind
@@ -257,7 +298,7 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
257
298
  advance: (publication) => {
258
299
  try {
259
300
  for (const change of publication.changes) {
260
- const data = change.type === "entry" && change.value.kind === TOOL_COST ? change.value.data : undefined;
301
+ const data = change.type === "entry" && (change.value.kind === TOOL_COST || change.value.kind === BROWSER_CHARGED) ? change.value.data : undefined;
261
302
  if (data?.tool === "browser" && typeof data.tag === "string")
262
303
  booked(data);
263
304
  }
@@ -296,18 +337,19 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
296
337
  const usd = microUsd(Math.max(0, usdOf(ranS) - carriedUsd) + 2 * stretch);
297
338
  const basis = `${provider}'s session time at $${rate} an hour: the ${Math.round(ranS)} s its session ran since its last charge, and twice the next stretch of ${stretchS} s (its ${price.minimumS} s minimum, the ${idleS} s idle window and ${RELEASE_ALLOWANCE_S} s for its release)`;
298
339
  if (ledger.paid) {
299
- // On the runner's one ledger: one paid request, held at that same figure, settled at the charges booked under
300
- // this call (a call with none charged nothing; one with an unpriced charge at its hold).
301
- chargedUnder.set(api.callId, 0);
340
+ // On the runner's one ledger: one hold-only step, held at that same figure and given back when the call returns
341
+ // (or its claim is lost): the call's charges are each their own step (booked).
302
342
  calls.set(api.callId, { cid: api.conversationId, hold: { usd, drop: () => { } } });
303
343
  try {
304
344
  // A provider whose time has no price is held at no figure: the cap's rule refuses it for good, as main does.
305
- const stepped = await paidStep(ledger.paid, api.callId, { name: "this browser call", holdUsd: rate === null ? Number.NaN : usd, basis, by: provider }, () => tool.execute(args, api, context), () => chargedUnder.get(api.callId) ?? null);
345
+ // One refused for room pauses the run (ledger.pause, the run's own: a native tool task runs in no host tool
346
+ // call's ledger), in the cap's words and figures, as an ask's person time does: its next request is refused
347
+ // and the run parks for a raise.
348
+ const stepped = await paidStep(ledger.paid, api.callId, { name: "this browser call", holdUsd: rate === null ? Number.NaN : usd, basis, by: provider, tool: BROWSER_TOOL, hold: true }, () => tool.execute(args, api, context), () => 0, ledger.pause);
306
349
  return "refused" in stepped ? await refuse(stepped.refused, api, context) : stepped.result;
307
350
  }
308
351
  finally {
309
352
  calls.delete(api.callId);
310
- chargedUnder.delete(api.callId);
311
353
  }
312
354
  }
313
355
  const paid = await paidCall("this browser call", async () => keepHold(usd), () => undefined, { usd, basis });
@@ -326,13 +368,14 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
326
368
  };
327
369
  }
328
370
  /** pi-browser's `beforeCreate`: the session's open recorded in the run's journal (BROWSER_SESSION_OPEN) beside its
329
- * `creating` record, and the journal synced (barriers.ts browserSessionOpen) before the vendor's session is created.
330
- * A sync that fails throws, and pi-browser creates nothing. */
371
+ * `creating` record, then flushed to disk (`ledger.flush`, the runner's store flush) before the vendor's session is
372
+ * created. The runner's flush before a tool call comes before the call opens its session, so it never covers this
373
+ * record. A write or a flush that fails throws, and pi-browser creates nothing. */
331
374
  const opening = async (open) => {
332
375
  const hold = [...calls.values()].find((c) => c.cid === open.conversationId)?.hold;
333
376
  const data = { tag: open.tag, conversation: open.conversationId, provider, held_usd: hold ? hold.usd : null };
334
377
  await open.commit(async (tx) => void (await tx.appendEntry(ROOT_CONVERSATION_ID, { kind: BROWSER_SESSION_OPEN, data })));
335
- await syncNow(runJournal(), BARRIER.browserSessionOpen);
378
+ await ledger.flush?.();
336
379
  };
337
380
  /** The cap's refusal as the call's error, and the conversation's session let go, so it runs no longer. */
338
381
  async function refuse(why, api, context) {
@@ -404,7 +447,7 @@ export function browserSpend(provider, price, idleS, release, log = (line) => vo
404
447
  if (!(to > from))
405
448
  return;
406
449
  // Spent at the provider already: held on the run's ledger (or this process's holds) until a charge books it.
407
- const { hold } = holdOf("a person's time in the browser", microUsd(usdOf((to - from) / 1000)), `${provider}'s session time a person used, until the session's next charge books it`);
450
+ const { hold } = holdOf("a person's time in the browser", microUsd(usdOf((to - from) / 1000)), `${provider}'s session time a person used, until the session's next charge books it`, { owed: true, tag });
408
451
  carried.set(tag, [...(carried.get(tag) ?? []), { hold, from, to }]);
409
452
  }
410
453
  /** Once every ledger step of a charge under no call is written (the runner's one ledger; nothing to wait for without). */
@@ -1,10 +1,8 @@
1
1
  import { type IncomingMessage, type ServerResponse } from "node:http";
2
2
  import type { Duplex } from "node:stream";
3
3
  import type { Claim, Store, ViewerKind } from "./store/store.js";
4
- /** The hand-over directory in the run's directory (never in work/): one file per open session. */
5
- export declare const BROWSER_LIVE_DIR = "browser-live";
6
4
  /** What the run's process hands the server for one session's viewer; `at`, when (the newest is the one shown). `kind`, the
7
- * computer it views, as the store's row says it (a hand-over file is the browser's). */
5
+ * computer it views, as the store's row says it. */
8
6
  export type LiveHandover = {
9
7
  port: number;
10
8
  id: string;
@@ -16,18 +14,6 @@ export type LiveHandover = {
16
14
  /** The viewer `fullscreen` URL pi-browser's live view gives (an http URL on the loopback address, /v/<id>#t=<token>) for session `tag`, as
17
15
  * a hand-over; undefined for any other shape (a vendor's own URL never is one). */
18
16
  export declare function handoverOf(fullscreen: string, tag: string): LiveHandover | undefined;
19
- /** Writes `handover` into `runDir` as its session's own file: whole, 0600, under a name of its own first, then renamed
20
- * into place. The time it was handed over goes with it. */
21
- export declare function writeHandover(runDir: string, handover: LiveHandover): Promise<void>;
22
- /** Removes session `tag`'s hand-over, and no other's; with no tag, every one (the run's process let its sessions go). */
23
- export declare function dropHandover(runDir: string, tag?: string): Promise<void>;
24
- /** Every hand-over in `runDir`, newest first: each a plain file of this user's alone in a directory of this user's alone
25
- * (not a link), and one; anything else is left out. */
26
- export declare function readHandovers(runDir: string): Promise<LiveHandover[]>;
27
- /** The newest hand-over in `runDir`, or undefined. */
28
- export declare const readHandover: (runDir: string) => Promise<LiveHandover | undefined>;
29
- /** The hand-over of the viewer whose path id is `id`, or undefined. */
30
- export declare const handoverById: (runDir: string, id: string) => Promise<LiveHandover | undefined>;
31
17
  /** The store's live viewers (store.liveViewers: put and drop under the runner's claim, of() for the server) carry the
32
18
  * hand-over across processes, once serve and the runner share only the store. The token is never plaintext there: the
33
19
  * runner seals it under the seal key it holds, and the server opens it for the owner's answer alone. */
@@ -75,11 +61,12 @@ export type ViewerSeam = {
75
61
  put(handover: LiveHandover): Promise<unknown>;
76
62
  drop(tag?: string): Promise<void>;
77
63
  };
78
- /** Where the browser tool hands its sessions' viewers over: through `seam` on the runner (the store, under its claim),
79
- * else, on the old per-run process, the run directory `root`'s files. `changed`: a session launched (its viewer put) or
80
- * released (its own hand-over dropped, never another's); `reopened`: a kept session's viewer opened again after a
81
- * restart; `released`: the run let its sessions go, every hand-over of it dropped. */
82
- export declare function sessionHandovers(root: string, seam?: ViewerSeam): {
64
+ /** Where the browser tool hands its sessions' viewers over: through `seam` (the store, under the run's claim), and nowhere
65
+ * else: with no seam no viewer is handed over, and nothing is written (never a file in the run's folder, the agent's
66
+ * workspace). `changed`: a session launched (its viewer put) or released (its own hand-over dropped, never another's);
67
+ * `reopened`: a kept session's viewer opened again after a restart; `released`: the run let its sessions go, every
68
+ * hand-over of it dropped. */
69
+ export declare function sessionHandovers(seam: ViewerSeam | undefined): {
83
70
  changed: (row: {
84
71
  session: {
85
72
  tag: string;
@@ -1,10 +1,11 @@
1
1
  // The owner watches a run's browser live, and takes it over (browser-person.ts). The run's process runs pi-browser's own
2
2
  // viewer (createLiveView, take-over) on an ephemeral loopback port of its own, for every provider: the vendors' own live-view URLs never leave that process.
3
- // As each session launches, the process hands its viewer to the server in a file of that session's own, in a directory in
4
- // the run's directory, outside work/ (BROWSER_LIVE_DIR/<tag>.json, 0600 in 0700, written whole and renamed into place):
5
- // the port, the viewer's path id, its token, the session's tag and when. Its own file goes when the session is released,
6
- // and the token dies with it (pi-browser kills it then); a session's release never touches another's file, so an old
7
- // session's release that crosses a new one's launch (a relaunch) leaves the new one handed over.
3
+ // As each session launches, the run's runner hands its viewer to the server through the store, under the run's claim (a
4
+ // row of that session's own, its token sealed): the port, the viewer's path id, its token, the session's tag and when.
5
+ // No file holds it: on the runner the run's folder is its workspace, which the agent and the run's file list reach. Its
6
+ // own row goes when the session is released, and the token dies with it (pi-browser kills it then); a session's release
7
+ // never touches another's row, so an old session's release that crosses a new one's launch (a relaunch) leaves the new
8
+ // one handed over.
8
9
  //
9
10
  // The server reads the newest for the owner alone (GET /v2/runs/{id}/browser, and GET /v2/runs/{id}/computer, the computer
10
11
  // family's, the browser as kind browser there) and answers the viewer under the run's path (/v2/runs/{id}/browser/live/*,
@@ -12,16 +13,10 @@
12
13
  // the pi-browser it has installed, never anything a run's port answers: a run's port only ever carries the viewer's
13
14
  // WebSocket, data and no code, proxied to that port alone and only from this server's own origin. The page is served
14
15
  // under a CSP whose frame-ancestors is 'self' (the console's own origin). The token is never in a record, a timeline row, a run's view, its files (work/ alone is served), the model's
15
- // context, a home, an export or an archive: only this file and the owner's answer hold it.
16
- import { randomBytes } from "node:crypto";
17
- import { constants } from "node:fs";
18
- import { lstat, mkdir, open, readdir, rename, rm, writeFile } from "node:fs/promises";
16
+ // context, a home, an export or an archive: only its sealed row and the owner's answer hold it.
19
17
  import { request as httpRequest } from "node:http";
20
18
  import { connect } from "node:net";
21
- import { join } from "node:path";
22
19
  import { sealColumn, sealedAt, unsealColumn } from "./store/seal.js";
23
- /** The hand-over directory in the run's directory (never in work/): one file per open session. */
24
- export const BROWSER_LIVE_DIR = "browser-live";
25
20
  /** The computer kinds a store row may name. */
26
21
  const VIEWER_KINDS = ["browser", "desktop", "phone"];
27
22
  /** Where the run's own viewer listens: its process's loopback address alone. */
@@ -47,80 +42,6 @@ export function handoverOf(fullscreen, tag) {
47
42
  return undefined;
48
43
  return { port, id, token, tag };
49
44
  }
50
- /** Whether `at` is this user's alone, and not a link: a directory (0700) or a file (0600). */
51
- const ownAlone = (at, dir) => (dir ? at.isDirectory() : at.isFile()) && !at.isSymbolicLink() && (at.mode & 0o077) === 0 && (!process.getuid || at.uid === process.getuid());
52
- /** Writes `handover` into `runDir` as its session's own file: whole, 0600, under a name of its own first, then renamed
53
- * into place. The time it was handed over goes with it. */
54
- export async function writeHandover(runDir, handover) {
55
- if (!TAG.test(handover.tag))
56
- throw new Error("a hand-over's tag is not a session's tag");
57
- const dir = join(runDir, BROWSER_LIVE_DIR);
58
- await mkdir(dir, { mode: 0o700 }).catch((error) => {
59
- if (error.code !== "EEXIST")
60
- throw error;
61
- });
62
- if (!ownAlone(await lstat(dir), true))
63
- throw new Error("the run's live view directory is not this user's alone");
64
- const making = join(dir, `.${randomBytes(6).toString("hex")}.tmp`);
65
- await writeFile(making, `${JSON.stringify({ ...handover, at: handover.at ?? Date.now() })}\n`, { mode: 0o600, flag: "wx" });
66
- try {
67
- await rename(making, join(dir, `${handover.tag}.json`));
68
- }
69
- catch (error) {
70
- await rm(making, { force: true });
71
- throw error;
72
- }
73
- }
74
- /** Removes session `tag`'s hand-over, and no other's; with no tag, every one (the run's process let its sessions go). */
75
- export async function dropHandover(runDir, tag) {
76
- if (tag === undefined)
77
- return void (await rm(join(runDir, BROWSER_LIVE_DIR), { recursive: true, force: true }));
78
- if (TAG.test(tag))
79
- await rm(join(runDir, BROWSER_LIVE_DIR, `${tag}.json`), { force: true });
80
- }
81
- /** Every hand-over in `runDir`, newest first: each a plain file of this user's alone in a directory of this user's alone
82
- * (not a link), and one; anything else is left out. */
83
- export async function readHandovers(runDir) {
84
- const dir = join(runDir, BROWSER_LIVE_DIR);
85
- try {
86
- if (!ownAlone(await lstat(dir), true))
87
- return [];
88
- const found = [];
89
- for (const name of await readdir(dir)) {
90
- if (!name.endsWith(".json") || !TAG.test(name.slice(0, -5)))
91
- continue;
92
- const file = join(dir, name);
93
- try {
94
- // Opened without following a link, and checked on the handle it read: a name swapped meanwhile reads nothing.
95
- const handle = await open(file, constants.O_RDONLY | constants.O_NOFOLLOW);
96
- let text;
97
- try {
98
- if (!ownAlone(await handle.stat(), false))
99
- continue;
100
- text = await handle.readFile("utf8");
101
- }
102
- finally {
103
- await handle.close();
104
- }
105
- const value = JSON.parse(text);
106
- const ok = Number.isInteger(value.port) && value.port > 0 && value.port < 65_536 && VIEWER_ID.test(String(value.id)) && TOKEN.test(String(value.token)) && value.tag === name.slice(0, -5) && Number.isFinite(value.at);
107
- if (ok)
108
- found.push({ port: value.port, id: value.id, token: value.token, tag: value.tag, at: value.at });
109
- }
110
- catch {
111
- // gone meanwhile, or not one
112
- }
113
- }
114
- return found.sort((a, b) => b.at - a.at || b.tag.localeCompare(a.tag));
115
- }
116
- catch {
117
- return [];
118
- }
119
- }
120
- /** The newest hand-over in `runDir`, or undefined. */
121
- export const readHandover = async (runDir) => (await readHandovers(runDir))[0];
122
- /** The hand-over of the viewer whose path id is `id`, or undefined. */
123
- export const handoverById = async (runDir, id) => (await readHandovers(runDir)).find((handover) => handover.id === id);
124
45
  /** The runner hands session `handover.tag`'s viewer over, under `claim`, its token sealed under `sealKey`: false when the
125
46
  * claim is stale and nothing was written. The time is the store's. */
126
47
  export async function putLiveViewer(store, sealKey, claim, handover) {
@@ -204,13 +125,14 @@ export async function computerViewerLink(store, sealKey, runId, answers = viewer
204
125
  const handover = await answeringViewer(store, sealKey, runId, answers);
205
126
  return handover && { kind: handover.kind ?? "browser", viewer: viewerLink(runId, handover, "computer") };
206
127
  }
207
- /** Where the browser tool hands its sessions' viewers over: through `seam` on the runner (the store, under its claim),
208
- * else, on the old per-run process, the run directory `root`'s files. `changed`: a session launched (its viewer put) or
209
- * released (its own hand-over dropped, never another's); `reopened`: a kept session's viewer opened again after a
210
- * restart; `released`: the run let its sessions go, every hand-over of it dropped. */
211
- export function sessionHandovers(root, seam) {
212
- const put = (handover) => (seam ? seam.put(handover) : writeHandover(root, handover));
213
- const drop = (tag) => (seam ? seam.drop(tag) : dropHandover(root, tag));
128
+ /** Where the browser tool hands its sessions' viewers over: through `seam` (the store, under the run's claim), and nowhere
129
+ * else: with no seam no viewer is handed over, and nothing is written (never a file in the run's folder, the agent's
130
+ * workspace). `changed`: a session launched (its viewer put) or released (its own hand-over dropped, never another's);
131
+ * `reopened`: a kept session's viewer opened again after a restart; `released`: the run let its sessions go, every
132
+ * hand-over of it dropped. */
133
+ export function sessionHandovers(seam) {
134
+ const put = async (handover) => seam?.put(handover);
135
+ const drop = async (tag) => seam?.drop(tag);
214
136
  return {
215
137
  changed: async (row, change) => {
216
138
  if (change === "released")
@@ -1,18 +1,11 @@
1
- /** pi-browser's tool for the agent's ask. */
2
- export declare const HANDOFF_TOOL = "browser_request_handoff";
3
- /** The ask's directory in the run's directory (never in work/). */
4
- export declare const BROWSER_ASK_DIR = "browser-ask";
5
1
  /** 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. */
6
2
  export declare const PERSON_HOLD_MS: number;
7
3
  /** How long a take-over outlasts its viewer. */
8
4
  export declare const PERSON_AWAY_MS: number;
9
5
  /** Under `--faux` only (the tests, beside the fake browser's own FAUX_BROWSER_LOG): the two windows above, in ms, `<hold>,<away>`. */
10
6
  export declare const FAUX_PERSON_MS_ENV = "FAUX_BROWSER_PERSON_MS";
11
- /** For tests (under `--faux`): a file's path; the run's process dies once (making the file) right after it writes an ask's
12
- * record and before it commits the ask's row, as a crash there would. */
13
- export declare const FAUX_ASK_DIES_ENV = "FAUX_BROWSER_ASK_DIES";
14
7
  export type AskOutcome = "done" | "cancelled" | "expired";
15
- /** An ask's pending record, as the server reads it. */
8
+ /** An ask, as its waiting entry in the run's journal holds it. */
16
9
  export type PendingAsk = {
17
10
  id: string;
18
11
  tag: string;
@@ -27,46 +20,20 @@ export type Answer = {
27
20
  at: string;
28
21
  reason?: string;
29
22
  };
30
- /** 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
23
+ /** 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
31
24
  * session's tag with it, since two conversations of one run (each its own session) may pick the same request id, and
32
25
  * each ask is its session's own: its instruction, its answer, its take-over. */
33
26
  export declare const askIdOf: (sessionTag: string, requestId: string) => string;
34
- /** The ask the run waits on: the newest unanswered one that has not expired, or undefined. Read where a run's status is
35
- * read (runs.ts statusOf), so a run with no ask costs one existence check. */
36
- export declare function openAsk(runDir: string, now?: number): PendingAsk | undefined;
37
- /** The newest unanswered ask, expired or not: one a process must still take up (answer it, or write its expiry), as a
38
- * restarted run's process does. */
39
- export declare function waitingAsk(runDir: string): PendingAsk | undefined;
40
- /** Whether the run's directory still holds an ask's record, answered or not: one its agent's call may still be waiting
41
- * on (the records go when the run ends, or when its process lets its sessions go). */
42
- export declare function asked(runDir: string): boolean;
43
- /** The ask the run waits on (`which`: "open", unexpired, as its status reads it; "waiting", expired or not, as a process
44
- * must still take up) once its own waiting row is in `rows`: the run's process writes the record first, then commits the
45
- * 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
46
- * session has n waiting rows, whatever other asks (another conversation's, answered or not) wrote meanwhile. */
47
- export declare function askShown(runDir: string, rows: readonly {
48
- kind: string;
49
- session?: string;
50
- }[], which?: "open" | "waiting"): PendingAsk | undefined;
51
- /** The owner's answer to the run's ask: the newest one still waiting, as `openAsk` reads it (two conversations may each
52
- * ask), written when it is the first (`first`); with none waiting, the answer the newest one has. An ask past its expiry
53
- * has `expired`. Undefined when the run has asked nothing. */
54
- export declare function answerAsk(runDir: string, outcome: "done" | "cancelled", now?: number): {
55
- outcome: AskOutcome;
56
- first: boolean;
57
- } | undefined;
58
- /** Removes every ask: the run's process that made them is gone. */
59
- export declare function dropAsks(runDir: string): void;
60
27
  /** Why a take-over ended, as pi-browser's gate says it. */
61
28
  type HoldEnd = "given" | "answered" | "cap" | "idle" | "away" | "released";
62
29
  /** A root entry of the run's: a timeline row's source. A phase's carries `at` (ms), when what it says happened, so its row
63
30
  * shows that time, not the time of the log before it (a model request's start, a call's result). */
64
31
  type Note = (kind: string, data: Record<string, string | number>) => Promise<void>;
65
- /** The run's process's side, for the run at `root`: `take` for the live view's takeOver, `handoff` for pi-browser's
66
- * HandoffPort. `gates` is the live view's registry (pi-browser's driving gates); `personTime` holds the person's time
67
- * under the cap (browser-cost.ts); `note` commits a root entry, which reaches the run's timeline. */
32
+ /** The run's process's side: `take` for the live view's takeOver, `handoff` for pi-browser's HandoffPort. `gates` is the
33
+ * live view's registry (pi-browser's driving gates); `personTime` holds the person's time under the cap
34
+ * (browser-cost.ts); `note` commits a root entry, which reaches the run's timeline. Every ask and every answer is an
35
+ * entry of the run's journal, read back through `asks` and `answered`. */
68
36
  export declare function personHands(options: {
69
- root: string;
70
37
  gates: (tag: string) => {
71
38
  give?(why: "answered" | "cap", message?: string): boolean;
72
39
  };
@@ -96,27 +63,25 @@ export declare function personHands(options: {
96
63
  /** The person's time on a session that just ended, `seconds` of it, to hold until a charge books it (browser-cost.ts
97
64
  * carry): the up-front holds go as it ends, the time it took stays counted. */
98
65
  carry?(tag: string, seconds: number): void;
99
- /** How many waiting rows session `tag` has in the run's timeline (each ask on it writes one, in the order asked). */
100
- waiting?(tag: string): Promise<number>;
101
- /** On the runner's path: ask `id`'s answer as the run's journal has it (its first answered hand-over entry), or
102
- * undefined. Given, an ask's answers are journal entries: the person's (a browser_hand_back row the runner applies),
103
- * and those this process writes through `note` (the live view's hand-back, the ask's expiry at the expiry's time, its
104
- * end at the cap). The answer files are neither read nor written. */
105
- answered?(id: string): Promise<Answer | undefined>;
106
- /** On the runner's path: writes ask `id`'s answer (the hand-over entry `data`) unless the run's journal has one, read
107
- * and written in one commit (runner/hand-back.ts answerOnce), and gives back the answer that stands, so an ask has
108
- * exactly one answered entry whoever writes it (a person's hand-back row the runner applies, this process's give or
109
- * expiry). Without it, the answer is written through `note` after a read. */
66
+ /** Every ask the run's journal holds: its waiting hand-over entries (each the ask's one record, committed through
67
+ * `note`), as asks. */
68
+ asks(): Promise<readonly PendingAsk[]>;
69
+ /** Ask `id`'s answer as the run's journal has it (its first answered hand-over entry), or undefined: the person's (a
70
+ * browser_hand_back row the runner applies), and those this process writes (the live view's hand-back, the ask's
71
+ * expiry at the expiry's time, its end at the cap). */
72
+ answered(id: string): Promise<Answer | undefined>;
73
+ /** Writes ask `id`'s answer (the hand-over entry `data`) unless the run's journal has one, read and written in one
74
+ * commit (runner/hand-back.ts answerOnce), and gives back the answer that stands, so an ask has exactly one answered
75
+ * entry whoever writes it (a person's hand-back row the runner applies, this process's give or expiry). Without it,
76
+ * the answer is written through `note` after a read. */
110
77
  answer?(id: string, data: Record<string, string>): Promise<Answer>;
111
- /** For tests: called between an ask's record and its row. */
112
- afterRecord?: () => void;
113
78
  }): {
114
79
  /** The live view's take: the person's time held, unless an ask open on the same session holds it already (its answer
115
80
  * ends the take); refused in the cap's words when there is no room. */
116
81
  take(tag: string): Promise<{
117
82
  refused: string;
118
83
  } | {
119
- end(why: HoldEnd): void;
84
+ end(why: HoldEnd): Promise<void>;
120
85
  started(): void;
121
86
  cancel(): void;
122
87
  }>;