@parcha/agentrun-serve 0.1.0-beta.43 → 0.1.0-beta.45

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 (388) hide show
  1. package/CHANGELOG.md +231 -0
  2. package/README.md +580 -320
  3. package/dist/acp-package.d.ts +28 -0
  4. package/dist/acp-package.js +23 -0
  5. package/dist/adapter-facts.d.ts +8 -1
  6. package/dist/adapter-facts.js +9 -1
  7. package/dist/adapters.d.ts +5 -0
  8. package/dist/adapters.js +36 -3
  9. package/dist/agent-delete.d.ts +50 -0
  10. package/dist/agent-delete.js +125 -0
  11. package/dist/agents-store.d.ts +9 -3
  12. package/dist/agents-store.js +22 -8
  13. package/dist/agents.d.ts +5 -1
  14. package/dist/agents.js +8 -4
  15. package/dist/api-only.d.ts +8 -3
  16. package/dist/api-only.js +10 -4
  17. package/dist/archil-routes.d.ts +5 -1
  18. package/dist/archil-routes.js +57 -2
  19. package/dist/browser-wait-ended.d.ts +2 -0
  20. package/dist/browser-wait-ended.js +2 -0
  21. package/dist/calendar/ics-faux.d.ts +3 -1
  22. package/dist/calendar/ics-faux.js +12 -2
  23. package/dist/calendar/tools.js +5 -2
  24. package/dist/chatgpt-plan.js +3 -1
  25. package/dist/claude-login.d.ts +135 -0
  26. package/dist/claude-login.js +244 -0
  27. package/dist/cli.js +92 -22
  28. package/dist/composio-preset.d.ts +4 -3
  29. package/dist/composio-preset.js +81 -11
  30. package/dist/composio-triggers.d.ts +34 -2
  31. package/dist/composio-triggers.js +261 -31
  32. package/dist/computer-attach.d.ts +40 -5
  33. package/dist/computer-attach.js +130 -15
  34. package/dist/computer-keys.d.ts +3 -0
  35. package/dist/computer-keys.js +7 -1
  36. package/dist/computer-live.js +3 -2
  37. package/dist/computer-lost.d.ts +11 -0
  38. package/dist/computer-lost.js +19 -0
  39. package/dist/computer-record.d.ts +10 -2
  40. package/dist/computer-record.js +50 -18
  41. package/dist/computer-setting.d.ts +1 -0
  42. package/dist/computer-setting.js +2 -2
  43. package/dist/computers.d.ts +21 -10
  44. package/dist/computers.js +39 -28
  45. package/dist/console/assets/index-3O2Yl6hZ.js +701 -0
  46. package/dist/console/assets/index-DaFsZuzQ.css +1 -0
  47. package/dist/console/index.html +2 -2
  48. package/dist/decisions.d.ts +1 -1
  49. package/dist/dial-relay.d.ts +29 -0
  50. package/dist/dial-relay.js +94 -0
  51. package/dist/email/tools.js +10 -5
  52. package/dist/env-proxy.d.ts +2 -0
  53. package/dist/env-proxy.js +35 -0
  54. package/dist/errors.d.ts +4 -1
  55. package/dist/errors.js +10 -4
  56. package/dist/faux-computers.d.ts +8 -2
  57. package/dist/faux-computers.js +83 -6
  58. package/dist/harness-archive.d.ts +67 -0
  59. package/dist/harness-archive.js +177 -0
  60. package/dist/harness-goose.d.ts +34 -0
  61. package/dist/harness-goose.js +87 -0
  62. package/dist/harness-install.d.ts +21 -0
  63. package/dist/harness-install.js +24 -11
  64. package/dist/harness-opencode/adapter.js +1 -1
  65. package/dist/harness-table.d.ts +59 -3
  66. package/dist/harness-table.js +119 -14
  67. package/dist/harness.d.ts +17 -1
  68. package/dist/harness.js +22 -11
  69. package/dist/host.d.ts +12 -10
  70. package/dist/host.js +19 -16
  71. package/dist/import-worker.d.ts +3 -0
  72. package/dist/import-worker.js +7 -5
  73. package/dist/import.d.ts +22 -1
  74. package/dist/import.js +141 -28
  75. package/dist/key-check.js +3 -0
  76. package/dist/learn/audit.d.ts +6 -6
  77. package/dist/learn/audit.js +3 -7
  78. package/dist/learn/check.d.ts +26 -89
  79. package/dist/learn/check.js +58 -336
  80. package/dist/learn/commit.d.ts +63 -90
  81. package/dist/learn/commit.js +124 -316
  82. package/dist/learn/compile.d.ts +53 -50
  83. package/dist/learn/compile.js +88 -103
  84. package/dist/learn/corrections.d.ts +13 -0
  85. package/dist/learn/corrections.js +23 -0
  86. package/dist/learn/delete.d.ts +17 -0
  87. package/dist/learn/delete.js +42 -0
  88. package/dist/learn/folder.d.ts +35 -47
  89. package/dist/learn/folder.js +118 -134
  90. package/dist/learn/index.d.ts +3 -5
  91. package/dist/learn/index.js +6 -9
  92. package/dist/learn/learning.sh +45 -15
  93. package/dist/learn/promote.d.ts +20 -2
  94. package/dist/learn/promote.js +17 -4
  95. package/dist/learn/{skills.d.ts → workflow-draft.d.ts} +34 -38
  96. package/dist/learn/{skills.js → workflow-draft.js} +65 -59
  97. package/dist/learn/workflow-nodes.d.ts +22 -1
  98. package/dist/learn/workflow-nodes.js +29 -10
  99. package/dist/learned-workflow.js +2 -2
  100. package/dist/learning-cost.d.ts +127 -13
  101. package/dist/learning-cost.js +142 -10
  102. package/dist/learning-routes.d.ts +99 -170
  103. package/dist/learning-routes.js +199 -420
  104. package/dist/learning-run.d.ts +19 -40
  105. package/dist/learning-run.js +23 -93
  106. package/dist/learning-setting.d.ts +4 -4
  107. package/dist/learning-setting.js +6 -6
  108. package/dist/learning-words.d.ts +28 -74
  109. package/dist/learning-words.js +64 -162
  110. package/dist/learning.d.ts +62 -393
  111. package/dist/learning.js +101 -651
  112. package/dist/listen-first.d.ts +30 -0
  113. package/dist/listen-first.js +115 -0
  114. package/dist/live-reach.d.ts +25 -2
  115. package/dist/live-reach.js +46 -4
  116. package/dist/llms.txt +2 -1
  117. package/dist/local-models.d.ts +3 -11
  118. package/dist/local-models.js +5 -6
  119. package/dist/mcp-servers-store.d.ts +16 -16
  120. package/dist/model-form.js +6 -5
  121. package/dist/model.d.ts +12 -4
  122. package/dist/model.js +41 -16
  123. package/dist/native/binding.js +1 -1
  124. package/dist/native/connection.js +1 -1
  125. package/dist/native/discovery.js +1 -1
  126. package/dist/native/instructions.d.ts +3 -0
  127. package/dist/native/instructions.js +5 -3
  128. package/dist/native/remote-worker-bridge.js +9 -43
  129. package/dist/native/remote.d.ts +8 -15
  130. package/dist/native/remote.js +3 -3
  131. package/dist/native/schema.js +1 -1
  132. package/dist/native/types.d.ts +2 -2
  133. package/dist/openapi.js +375 -347
  134. package/dist/owner.d.ts +8 -0
  135. package/dist/owner.js +11 -0
  136. package/dist/paid-calls.d.ts +14 -0
  137. package/dist/paid-calls.js +31 -1
  138. package/dist/particle-provider.d.ts +11 -0
  139. package/dist/particle-provider.js +135 -0
  140. package/dist/place.d.ts +8 -3
  141. package/dist/place.js +13 -9
  142. package/dist/port-models.d.ts +12 -4
  143. package/dist/port-models.js +28 -11
  144. package/dist/print.d.ts +27 -0
  145. package/dist/print.js +21 -0
  146. package/dist/provider.d.ts +7 -0
  147. package/dist/provider.js +30 -2
  148. package/dist/reflect-range.js +2 -3
  149. package/dist/run-launch.d.ts +14 -28
  150. package/dist/run-launch.js +9 -12
  151. package/dist/run-learning-served.d.ts +16 -90
  152. package/dist/run-listing.d.ts +34 -0
  153. package/dist/run-listing.js +128 -0
  154. package/dist/run-model-key.d.ts +64 -0
  155. package/dist/run-model-key.js +147 -0
  156. package/dist/run-timeline-served.js +5 -3
  157. package/dist/run-trace.d.ts +97 -0
  158. package/dist/run-trace.js +253 -0
  159. package/dist/run-view.d.ts +15 -0
  160. package/dist/run-view.js +9 -0
  161. package/dist/runnable.js +2 -1
  162. package/dist/runner/adapter-loop.d.ts +14 -6
  163. package/dist/runner/adapter-loop.js +25 -14
  164. package/dist/runner/adapter-run-spec.d.ts +22 -7
  165. package/dist/runner/adapter-run-spec.js +130 -77
  166. package/dist/runner/adapters/index.d.ts +4 -2
  167. package/dist/runner/adapters/index.js +35 -4
  168. package/dist/runner/adapters/pi.d.ts +4 -12
  169. package/dist/runner/adapters/pi.js +2 -2
  170. package/dist/runner/audit.d.ts +5 -1
  171. package/dist/runner/audit.js +31 -12
  172. package/dist/runner/beside.d.ts +7 -2
  173. package/dist/runner/beside.js +41 -9
  174. package/dist/runner/claim-secrets.d.ts +66 -1
  175. package/dist/runner/claim-secrets.js +136 -3
  176. package/dist/runner/cli-run-spec.d.ts +2 -1
  177. package/dist/runner/cli-run-spec.js +82 -59
  178. package/dist/runner/commands.d.ts +3 -6
  179. package/dist/runner/conformance/harness.d.ts +6 -0
  180. package/dist/runner/conformance/harness.js +16 -5
  181. package/dist/runner/dispatch.d.ts +2 -2
  182. package/dist/runner/end-of-run.d.ts +6 -14
  183. package/dist/runner/end-of-run.js +8 -33
  184. package/dist/runner/feedback-round.d.ts +5 -4
  185. package/dist/runner/feedback-round.js +14 -10
  186. package/dist/runner/github-app.js +3 -1
  187. package/dist/runner/harness-loops.d.ts +0 -8
  188. package/dist/runner/harness-loops.js +30 -56
  189. package/dist/runner/held.d.ts +170 -0
  190. package/dist/runner/held.js +57 -0
  191. package/dist/runner/journal-kinds.d.ts +7 -3
  192. package/dist/runner/journal-kinds.js +2 -3
  193. package/dist/runner/learned-first.d.ts +40 -0
  194. package/dist/runner/learned-first.js +32 -0
  195. package/dist/runner/learning-end.d.ts +77 -66
  196. package/dist/runner/learning-end.js +250 -299
  197. package/dist/runner/learning-seed.d.ts +15 -33
  198. package/dist/runner/learning-seed.js +24 -45
  199. package/dist/runner/loop-contract.d.ts +8 -3
  200. package/dist/runner/loop-contract.js +4 -0
  201. package/dist/runner/loop.js +6 -2
  202. package/dist/runner/main.d.ts +18 -9
  203. package/dist/runner/main.js +82 -22
  204. package/dist/runner/mcp-run.d.ts +11 -0
  205. package/dist/runner/mcp-run.js +67 -0
  206. package/dist/runner/memory-claim.d.ts +14 -6
  207. package/dist/runner/memory-claim.js +18 -9
  208. package/dist/runner/model-route.d.ts +7 -4
  209. package/dist/runner/model-route.js +22 -10
  210. package/dist/runner/native-loop.d.ts +0 -7
  211. package/dist/runner/native-loop.js +9 -101
  212. package/dist/runner/promotion.d.ts +13 -6
  213. package/dist/runner/promotion.js +63 -17
  214. package/dist/runner/reflect-hook.d.ts +64 -91
  215. package/dist/runner/reflect-hook.js +112 -206
  216. package/dist/runner/run-channel.d.ts +43 -0
  217. package/dist/runner/run-channel.js +467 -0
  218. package/dist/runner/run-child-host.d.ts +169 -0
  219. package/dist/runner/run-child-host.js +1222 -0
  220. package/dist/runner/run-child.d.ts +1 -0
  221. package/dist/runner/run-child.js +254 -0
  222. package/dist/runner/run-errors.d.ts +2 -0
  223. package/dist/runner/run-errors.js +20 -0
  224. package/dist/runner/runner.js +58 -9
  225. package/dist/runner/sdk-ask.d.ts +78 -0
  226. package/dist/runner/sdk-ask.js +158 -0
  227. package/dist/runner/sdk-loop.d.ts +2 -4
  228. package/dist/runner/sdk-loop.js +29 -49
  229. package/dist/runner/store-secrets.d.ts +5 -3
  230. package/dist/runner/store-secrets.js +14 -6
  231. package/dist/runner/workflow-first.d.ts +5 -0
  232. package/dist/runner/workflow-first.js +4 -0
  233. package/dist/runner/workflow-spec.js +17 -4
  234. package/dist/runs-over-store.d.ts +21 -32
  235. package/dist/runs-over-store.js +44 -41
  236. package/dist/sdk/runs.d.ts +6 -11
  237. package/dist/sdk/runs.js +20 -41
  238. package/dist/sdk/schema.js +3 -4
  239. package/dist/sdk/workflows.d.ts +1 -1
  240. package/dist/seal-key.d.ts +5 -0
  241. package/dist/seal-key.js +7 -0
  242. package/dist/secrets.d.ts +3 -0
  243. package/dist/secrets.js +16 -3
  244. package/dist/server.d.ts +119 -83
  245. package/dist/server.js +1121 -419
  246. package/dist/sign-ins-store.d.ts +8 -0
  247. package/dist/sign-ins-store.js +36 -1
  248. package/dist/skills.js +1 -1
  249. package/dist/store/index.d.ts +3 -3
  250. package/dist/store/index.js +2 -2
  251. package/dist/store/journal/storage.js +3 -0
  252. package/dist/store/postgres/migrations/0017_run_listing.sql +83 -0
  253. package/dist/store/postgres/migrations/0018_trigger_template.sql +6 -0
  254. package/dist/store/postgres/migrations/0019_runner_relays.sql +28 -0
  255. package/dist/store/schema.d.ts +53 -4
  256. package/dist/store/schema.js +137 -6
  257. package/dist/store/seal.d.ts +20 -2
  258. package/dist/store/seal.js +24 -8
  259. package/dist/store/sqlite/connection.d.ts +2 -0
  260. package/dist/store/sqlite/connection.js +3 -1
  261. package/dist/store/sqlite/run-launch.d.ts +4 -1
  262. package/dist/store/sqlite/run-launch.js +12 -0
  263. package/dist/store/sqlite/run-patch.d.ts +0 -17
  264. package/dist/store/sqlite/run-patch.js +4 -33
  265. package/dist/store/sqlite/store.d.ts +3 -2
  266. package/dist/store/sqlite/store.js +360 -75
  267. package/dist/store/sqlite/worker.js +25 -0
  268. package/dist/store/store.d.ts +231 -39
  269. package/dist/store/store.js +25 -3
  270. package/dist/store-reads/analytics.js +11 -1
  271. package/dist/store-reads/learning.d.ts +131 -194
  272. package/dist/store-reads/learning.js +292 -301
  273. package/dist/store-reads/loop.d.ts +0 -1
  274. package/dist/store-reads/loop.js +1 -1
  275. package/dist/store-reads/stats.js +13 -3
  276. package/dist/store-view.d.ts +34 -2
  277. package/dist/store-view.js +108 -41
  278. package/dist/telemetry.d.ts +59 -0
  279. package/dist/telemetry.js +153 -0
  280. package/dist/template-eval.d.ts +3 -6
  281. package/dist/template-eval.js +22 -36
  282. package/dist/templates.d.ts +2 -1
  283. package/dist/templates.js +6 -3
  284. package/dist/timeline/index.d.ts +1 -1
  285. package/dist/timeline/index.js +1 -1
  286. package/dist/timeline/timeline.d.ts +5 -1
  287. package/dist/timeline/timeline.js +16 -0
  288. package/dist/tool-gate.d.ts +0 -7
  289. package/dist/tool-gate.js +1 -15
  290. package/dist/tool-keys-store.d.ts +1 -1
  291. package/dist/tool-keys.d.ts +6 -4
  292. package/dist/tool-keys.js +12 -6
  293. package/dist/tool-plane.d.ts +12 -12
  294. package/dist/tools.d.ts +13 -13
  295. package/dist/tools.js +29 -9
  296. package/dist/trigger-adapter.d.ts +3 -0
  297. package/dist/trigger-adapters.d.ts +4 -0
  298. package/dist/trigger-adapters.js +13 -0
  299. package/dist/trigger-composio.d.ts +58 -1
  300. package/dist/trigger-composio.js +160 -6
  301. package/dist/triggers.d.ts +64 -5
  302. package/dist/triggers.js +141 -17
  303. package/dist/validate.d.ts +11 -7
  304. package/dist/validate.js +9 -8
  305. package/dist/webhooks.d.ts +4 -1
  306. package/dist/webhooks.js +11 -4
  307. package/dist/workspace/agent-computer.d.ts +2 -0
  308. package/dist/workspace/agent-computer.js +2 -0
  309. package/dist/workspace/archil-claim.d.ts +7 -0
  310. package/dist/workspace/archil-claim.js +29 -0
  311. package/dist/workspace/archil.d.ts +9 -1
  312. package/dist/workspace/archil.js +165 -3
  313. package/dist/workspace/attach.d.ts +3 -1
  314. package/dist/workspace/attach.js +2 -0
  315. package/dist/workspace/attached-computers.d.ts +16 -5
  316. package/dist/workspace/attached-computers.js +25 -9
  317. package/dist/workspace/computer-deps.js +1 -1
  318. package/dist/workspace/computer-for-claim.d.ts +22 -8
  319. package/dist/workspace/computer-for-claim.js +339 -46
  320. package/dist/workspace/computer-hold.js +2 -1
  321. package/dist/workspace/computer-kept.d.ts +11 -1
  322. package/dist/workspace/computer-kept.js +24 -3
  323. package/dist/workspace/resolve-computer.d.ts +41 -21
  324. package/dist/workspace/resolve-computer.js +172 -64
  325. package/dist/workspace/revision-skills.d.ts +1 -2
  326. package/dist/workspace/revision-skills.js +3 -4
  327. package/dist/workspace/run-workspace.d.ts +1 -1
  328. package/dist/workspace/run-workspace.js +5 -0
  329. package/dist/workspace/synced-folder.d.ts +85 -0
  330. package/dist/workspace/synced-folder.js +230 -0
  331. package/dist/workspace/workspace.d.ts +4 -0
  332. package/launcher/agentrun-serve +1 -1
  333. package/package.json +10 -9
  334. package/templates/README.md +7 -7
  335. package/templates/inbound-lead/agent.md +3 -3
  336. package/templates/inbound-lead/evals/learned-correction/case.yaml +3 -3
  337. package/templates/inbound-lead/evals/learned-correction/fixtures/corrections.md +1 -0
  338. package/templates/inbound-lead/evals/learned-correction/graders/applies-correction.md +22 -0
  339. package/templates/inbound-lead/evals/learned-correction/graders/learned-origin.md +1 -1
  340. package/templates/inbound-lead/evals/learned-correction/prompt.md +1 -1
  341. package/templates/inbound-lead/lead.schema.json +6 -5
  342. package/templates/inbound-lead/rules/RUBRIC.md +4 -4
  343. package/templates/inbound-lead/skills/lead/SKILL.md +4 -4
  344. package/templates/market-monitor/evals/learned-preference/case.yaml +4 -4
  345. package/templates/market-monitor/evals/learned-preference/fixtures/corrections.md +1 -0
  346. package/templates/market-monitor/evals/learned-preference/graders/omits-junior-post.md +3 -3
  347. package/templates/market-monitor/evals/learned-preference/prompt.md +1 -1
  348. package/templates/release-notes/evals/learned-style/case.yaml +4 -4
  349. package/templates/release-notes/evals/learned-style/fixtures/corrections.md +1 -0
  350. package/templates/release-notes/evals/learned-style/graders/same-changes.md +2 -2
  351. package/templates/release-notes/evals/learned-style/prompt.md +1 -1
  352. package/dist/console/assets/index-BrcDSBD8.css +0 -1
  353. package/dist/console/assets/index-Bz6IWncl.js +0 -711
  354. package/dist/learn/grade.d.ts +0 -138
  355. package/dist/learn/grade.js +0 -682
  356. package/dist/learn/notes.d.ts +0 -81
  357. package/dist/learn/notes.js +0 -178
  358. package/dist/learn/reflect.d.ts +0 -77
  359. package/dist/learn/reflect.js +0 -228
  360. package/dist/learn/relevance.d.ts +0 -24
  361. package/dist/learn/relevance.js +0 -91
  362. package/dist/learn/seed-check.d.ts +0 -74
  363. package/dist/learn/seed-check.js +0 -103
  364. package/dist/learning-batch.d.ts +0 -291
  365. package/dist/learning-batch.js +0 -600
  366. package/dist/learning-evals.d.ts +0 -33
  367. package/dist/learning-evals.js +0 -98
  368. package/dist/native/context.d.ts +0 -6
  369. package/dist/native/context.js +0 -22
  370. package/dist/propose.d.ts +0 -52
  371. package/dist/propose.js +0 -224
  372. package/dist/runner/learning-ask.d.ts +0 -47
  373. package/dist/runner/learning-ask.js +0 -128
  374. package/dist/runner/learning-dispatch.d.ts +0 -45
  375. package/dist/runner/learning-dispatch.js +0 -99
  376. package/dist/runner/learning-factory.d.ts +0 -16
  377. package/dist/runner/learning-factory.js +0 -50
  378. package/dist/runner/learning-loop.d.ts +0 -273
  379. package/dist/runner/learning-loop.js +0 -521
  380. package/dist/usage.d.ts +0 -11
  381. package/dist/usage.js +0 -11
  382. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/MEMORY.md +0 -1
  383. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/no-agencies.md +0 -14
  384. package/templates/inbound-lead/evals/learned-correction/graders/applies-note.md +0 -21
  385. package/templates/market-monitor/evals/learned-preference/fixtures/learning/MEMORY.md +0 -1
  386. package/templates/market-monitor/evals/learned-preference/fixtures/learning/skip-junior-job-posts.md +0 -8
  387. package/templates/release-notes/evals/learned-style/fixtures/learning/MEMORY.md +0 -1
  388. package/templates/release-notes/evals/learned-style/fixtures/learning/say-agent-not-bot.md +0 -13
@@ -17,6 +17,12 @@
17
17
  // journal says an earlier claim made, hadComputer), the ready line says so (and that the kept one's time had passed,
18
18
  // `lapsed`), and the run's tools and model are told (run-tools.ts, the adapter loop's computer note).
19
19
  //
20
+ // A sandbox the claim made can be lost mid-run (its vendor deleted it, or no longer has it): the call that finds it lost
21
+ // fails, and the next call of the run's tools starts a new one in its place under this claim (lostAware), its folder
22
+ // mounted again (Mode 2: a new mount credential, the lost one's removed), said on the run's timeline, up to `recreates`
23
+ // times a claim; the loss after that ends the run, code computer_unavailable. A provider error that is not a loss (a 5xx,
24
+ // a timeout) starts nothing. A desktop, a phone, a paired computer and the agent's own are never started again so.
25
+ //
20
26
  // A provider is made with its keys (the server's environment's, else the store's sealed keys, through the composer's
21
27
  // keyOf: computer-deps.ts), by the names the registry declares, through serve's own
22
28
  // map of key names to options (computer-keys.ts providerOptions); a provider that keeps sessions with no labels of their
@@ -34,14 +40,17 @@ import { randomUUID } from "node:crypto";
34
40
  import { isBrowserProvider, ownerFilter, releaseOlder, RUN_LABEL, runLabels } from "@parcha/agentrun-computer";
35
41
  import { startRecording } from "../computer-record.js";
36
42
  import { computerOfPlace } from "../computer-setting.js";
37
- import { KEY_TABLE, providerOptions } from "../computer-keys.js";
43
+ import { KEY_TABLE, providerOptions, settingOptions } from "../computer-keys.js";
44
+ import { LOST_RECREATES, lostEnd, lostRecreatedWords } from "../computer-lost.js";
45
+ import { inToolCall, stopRun } from "../paid-calls.js";
38
46
  import { getSealedKey } from "../server-keys.js";
39
47
  import { agentComputerOf, forgetAgentComputer, holdAgentComputer, keepAgentComputer, liveHolderOf, reconcileAgentComputer, serverIdOf } from "./agent-computer.js";
40
48
  import { pairedProvider, withPaired } from "./attached-computers.js";
41
49
  import { computerHold, heldHands } from "./computer-hold.js";
42
- import { deleteKept, endKept, KEEP_MS, readKept, takeKept, writeKept } from "./computer-kept.js";
50
+ import { deleteKept, endKept, KEEP_MS, KEPT_DELETE_MS, KEPT_PULL_MS, keptEnded, readKept, takeKept, writeKept } from "./computer-kept.js";
43
51
  import { storeLedger } from "./computer-ledger.js";
44
- import { providerKeys, resolveComputer } from "./resolve-computer.js";
52
+ import { providerKeys, registryHere, resolveComputer } from "./resolve-computer.js";
53
+ import { claimHolds, fencedStore, pullFolder, pushFolder, storedFolder } from "./synced-folder.js";
45
54
  /** A computer that cannot be had, with the code the run fails with and the server's words. */
46
55
  const unavailable = (words) => Object.assign(new Error(words), { code: "computer_unavailable" });
47
56
  /** The claim was lost (its live view's registration was fenced): the run's process holds it no more. */
@@ -245,6 +254,138 @@ async function providerLaunch(provider, claim, spec, signal) {
245
254
  }
246
255
  return { ...providerComputer(provider, ref, connected.value), releasedOlder };
247
256
  }
257
+ /** The words a hosted provider's connection answers every call in once its vendor deleted the machine (the computer
258
+ * package's hosted deletedSaid: "the computer <name> (<id>) was deleted: <vendor> no longer has it"). */
259
+ const DELETED_SAID = /^the computer \S+ \(\S+\) was deleted: /;
260
+ /**
261
+ * `first`, a sandbox the claim made through its provider (or took back from the run's park), held so its loss is not the
262
+ * run's: a call it answers deleted (the hosted connection's own words, DELETED_SAID), or gone with its provider
263
+ * confirming it no longer has it (attach answers not_found or gone), marks it lost, and that call answers as it did. The
264
+ * next call made inside one of the run's tool calls starts a new one in its place (`renewal.remake`, one at a time
265
+ * however many calls ask) and goes to it: the lost one let go, the run's Timeline told (`started`). Each new one is the
266
+ * claim's, under its epoch: a later claim lets it go as it lets any of the run's older computers go. A start that fails
267
+ * answers its words and counts for nothing: the next call tries again under the same request, so a retry finds what an
268
+ * earlier try made. Gone with the provider still reporting the machine, or its read failing (a 5xx, a timeout), is not
269
+ * lost: the call's own answer stands, and so does a read that comes back after another call already replaced the one it
270
+ * asked about. A loss once `renewal.max` new ones were started ends the run at once (`renewal.stop`, computer-lost.ts
271
+ * lostEnd's words, code computer_unavailable), the call that found it answering those words, as every later call does.
272
+ * A call outside a tool call (the run's end sweeping its scratch, the owner's computer channel) gets the loss's own
273
+ * answer and starts nothing.
274
+ */
275
+ function lostAware(first, renewal) {
276
+ let current = first;
277
+ /** Why the current one is lost, until a new one replaces it. */
278
+ let lost;
279
+ /** How many new ones this claim started. */
280
+ let made = 0;
281
+ /** The current one was let go since it was lost (a failed start's retry does not let it go again). */
282
+ let letGo = false;
283
+ /** The bound was passed: every call answers this. */
284
+ let spent;
285
+ let renewing;
286
+ let released = false;
287
+ /** Whether `error`, answered by `held`, says its provider no longer has it. */
288
+ const goneFrom = async (held, error) => {
289
+ if (error.code === "not_found")
290
+ return DELETED_SAID.test(error.message);
291
+ if (error.code !== "gone")
292
+ return false;
293
+ const found = await renewal.provider.attach({ id: held.ref.id }).catch(() => undefined);
294
+ return found !== undefined && !found.ok && (found.error.code === "not_found" || found.error.code === "gone");
295
+ };
296
+ /** `why`, the current one's loss: past the bound, the run's end, at once. */
297
+ const lose = async (why) => {
298
+ if (made < renewal.max) {
299
+ lost = why;
300
+ letGo = false;
301
+ return why;
302
+ }
303
+ spent = { code: "not_found", message: lostEnd(renewal.max, why.message) };
304
+ await renewal.stop(spent.message);
305
+ return spent;
306
+ };
307
+ /** A new one in place of the lost one, or why the run has none now. */
308
+ const renew = async (why) => {
309
+ // The lost one's resources go first: a vendor's delete of one it no longer has is done already.
310
+ if (!letGo) {
311
+ await current.release().catch(() => undefined);
312
+ letGo = true;
313
+ }
314
+ let fresh;
315
+ try {
316
+ fresh = await renewal.remake(made + 1);
317
+ }
318
+ catch (error) {
319
+ return { code: "gone", message: `${why.message}; a new sandbox could not be started: ${error instanceof Error ? error.message : String(error)}` };
320
+ }
321
+ if (released) {
322
+ await fresh.release().catch(() => undefined);
323
+ return why;
324
+ }
325
+ made += 1;
326
+ current = fresh;
327
+ lost = undefined;
328
+ renewal.started(fresh.ref.name);
329
+ return undefined;
330
+ };
331
+ const client = {
332
+ async call(method, params, options) {
333
+ if (spent)
334
+ return { ok: false, error: spent };
335
+ if (lost) {
336
+ if (!renewal.may())
337
+ return { ok: false, error: lost };
338
+ const why = lost;
339
+ const failed = await (renewing ??= renew(why).finally(() => (renewing = undefined)));
340
+ if (failed)
341
+ return { ok: false, error: failed };
342
+ }
343
+ const held = current;
344
+ const answered = await held.client.call(method, params, options);
345
+ if (answered.ok || held !== current || lost || spent)
346
+ return answered;
347
+ const gone = await goneFrom(held, answered.error);
348
+ // Read back after another call replaced it, or found it lost, or the run ended: this answer alone stands.
349
+ if (!gone || held !== current || lost || spent)
350
+ return answered;
351
+ return { ok: false, error: await lose(answered.error) };
352
+ },
353
+ close() {
354
+ current.client.close();
355
+ },
356
+ };
357
+ return {
358
+ get id() {
359
+ return current.id;
360
+ },
361
+ get ref() {
362
+ return current.ref;
363
+ },
364
+ groups: first.groups,
365
+ machine: first.machine,
366
+ hello: first.hello,
367
+ client,
368
+ readBack: () => current.readBack(),
369
+ close: () => current.close(),
370
+ async release() {
371
+ released = true;
372
+ await current.release();
373
+ },
374
+ };
375
+ }
376
+ /** A kept computer's synced folder read back whole onto `store` (the run's disk) through a connection of its own, within
377
+ * KEPT_PULL_MS, before its end deletes it. Throws in words where it can't be. */
378
+ async function pullKept(provider, ref, store) {
379
+ const connected = await provider.connect(ref);
380
+ if (!connected.ok)
381
+ throw new Error(connected.error.message);
382
+ try {
383
+ await pullFolder(store, connected.value.client, AbortSignal.timeout(KEPT_PULL_MS));
384
+ }
385
+ finally {
386
+ await connected.value.close();
387
+ }
388
+ }
248
389
  /** The computer an earlier claim of the run kept at its park (`kept`), attached again through `provider` and connected,
249
390
  * or undefined when its provider no longer has it or it does not connect (one that does not is deleted). */
250
391
  async function keptLaunch(provider, kept, signal) {
@@ -400,12 +541,24 @@ async function ownedStart(provider, claim, spec, context, hold, signal) {
400
541
  /** The run's computer for this claim, started and registered; `{ computer: undefined }` for none. Throws with code
401
542
  * computer_unavailable and the server's words when the agent's computer cannot be had here. */
402
543
  export async function computerForClaim(launch,
403
- // `settings` and `runs`: the agent's own computer's record and its holder's run row, for a persistent setting alone.
544
+ // `settings` and `runs`: the agent's own computer's record and its holder's run row, for a persistent setting; and the
545
+ // run's own row, whose epoch and runner fence every write of the run's folder to its disk.
404
546
  store, deps) {
547
+ // The run's disk as this claim writes the folder on it: each write refused once a later claim has taken the run (Q1).
548
+ const runs = store.runs;
549
+ const holds = runs ? claimHolds((runId) => runs.get(runId), launch) : undefined;
550
+ const diskOf = () => {
551
+ const disk = deps.workspace.syncStore?.();
552
+ return disk && holds ? fencedStore(disk, holds) : disk;
553
+ };
405
554
  const table = deps.keyTable ?? KEY_TABLE;
406
555
  // The paired computers, each one more provider of the registry (attached-computers.ts): a setting names one by its id.
407
556
  const paired = deps.attached ? await deps.attached.rows() : [];
408
- const kinds = withPaired(deps.kinds, paired);
557
+ const keyOf = deps.keyOf ?? (async (name) => (deps.sealKey ? await getSealedKey(store, deps.sealKey, name) : undefined));
558
+ // Each provider as this server has it (registryHere: the mounts its keys turn on, whether the operator names its
559
+ // image, and no Archil mount where its settings make its machines dial this server), as a run's start reads it, so the
560
+ // claim holds the folder the start said it would.
561
+ const kinds = await registryHere(withPaired(deps.kinds, paired), keyOf, (provider) => settingOptions(provider, deps.settings ?? {}, table));
409
562
  /** The computer this claim started (one per claim): its ref id tags its live view, and its watch feeds it. */
410
563
  let made;
411
564
  /** The provider entry it was started from: a phone's platform where the computer did not say it. */
@@ -435,34 +588,159 @@ store, deps) {
435
588
  throw new Error(options.error.message);
436
589
  return make({ ...options.options, ...configuration.options }, { ledger: storeLedger(store.computerLedger, entry.id), ...(launch.boxAllow === undefined ? {} : { boxAllow: launch.boxAllow }) });
437
590
  };
591
+ /** What a computer of `kind` from `entry` is made with under this claim, but its mount: the deps' spec, within what
592
+ * `provider` declares it makes, from the image it prepares (readyImage) or the spec's, under the claim's labels. */
593
+ const specOf = async (entry, provider, kind, configuration, signal, gpu) => {
594
+ // What this provider makes, as it declares it: the spec asks for one of its networks, within its lifetime
595
+ // (computer-deps.ts computerSpecOf).
596
+ const { network: networks, maxLifetimeMs } = provider.capabilities();
597
+ const run = {
598
+ ...(launch.boxAllow === undefined ? {} : { boxAllow: launch.boxAllow }),
599
+ ...(launch.boxAllowSet ? { boxAllowSet: true } : {}),
600
+ ...(networks ? { networks } : {}),
601
+ ...(typeof maxLifetimeMs === "number" ? { maxLifetimeMs } : {}),
602
+ };
603
+ // A provider that declares it prepares its own image (its prepareImage(): its recipe built when the engine lacks it,
604
+ // an image by name pulled) does so first, and the computer starts from the tag it answers: the recipe's tag is in no
605
+ // registry, so a run of it unbuilt would be refused. The spec comes first, so a setting it refuses builds nothing. A
606
+ // configuration that names its image starts from it, else the kind's default.
607
+ const asked = deps.spec(entry, kind, run, configuration.image);
608
+ const ready = await readyImage(entry, provider, signal, deps.onImageWait);
609
+ const image = ready ?? asked.image;
610
+ if (image === undefined)
611
+ throw new Error(`${entry.label} names no image for its ${kind} computer and prepares none`);
612
+ return { ...asked, image, labels: runLabels(launch.runId, launch.epoch), ...(gpu ? { gpu } : {}) };
613
+ };
614
+ /** How a sandbox of `provider` this claim holds is started again once its provider lost it (lostAware): made from
615
+ * `specNow` (the lost one's spec, or for one taken back from the park this claim's own, read when it is needed), under
616
+ * this claim's labels and its own request (the claim's, then which new one), so a retry of it under this epoch finds
617
+ * the one it made. Where it holds the run's folder (`folder`), the new one holds it too, read back before it serves a
618
+ * call: a mount's credential of the lost one removed (Mode 2) and a new one asked for; a synced folder pushed in again
619
+ * from the disk (synced-folder.ts), whose sync goes on from there (the claim's FolderSync lists the new one). */
620
+ const renewalOf = (provider, entry, specNow, folder) => ({
621
+ provider,
622
+ max: deps.recreates ?? LOST_RECREATES,
623
+ async remake(times) {
624
+ deps.onStarting?.({ provider: entry.label, kind: "sandbox" });
625
+ const spec = await specNow();
626
+ let mount;
627
+ // The store is read whole before anything is made, so a folder over a cap rents nothing.
628
+ const store = folder === "sync" ? diskOf() : undefined;
629
+ if (folder === "sync" && !store)
630
+ throw new Error("this runner keeps no synced folder for the run");
631
+ const stored = store ? await storedFolder(store) : undefined;
632
+ if (folder === "sync")
633
+ mount = { kind: "sync" };
634
+ else if (folder !== undefined) {
635
+ await deps.revokeMount?.();
636
+ mount = await deps.workspace.mountSpec();
637
+ }
638
+ const call = deps.signal ? { signal: deps.signal } : {};
639
+ const created = await provider.create({ ...spec, ...(mount ? { mount } : {}) }, { requestId: `${claimRequestId(launch)}#lost-${times}`, ...call });
640
+ if (!created.ok)
641
+ throw new Error(created.error.message);
642
+ const ref = { id: created.value.id, name: created.value.name };
643
+ const connected = await provider.connect(ref);
644
+ if (!connected.ok) {
645
+ await provider.release(ref, "delete").catch(() => undefined);
646
+ throw new Error(connected.error.message);
647
+ }
648
+ const again = providerComputer(provider, ref, connected.value);
649
+ if (mount) {
650
+ try {
651
+ if (store && stored)
652
+ await pushFolder(store, again.client, stored, deps.signal);
653
+ await deps.workspace.attach({ id: ref.id, client: again.client, readBack: () => again.readBack() }, { ...deps.check, epoch: deps.check.epoch ?? launch.epoch, ...(store ? { sync: true } : {}) });
654
+ }
655
+ catch (error) {
656
+ await again.release().catch(() => undefined);
657
+ throw error;
658
+ }
659
+ }
660
+ return again;
661
+ },
662
+ started: (name) => {
663
+ // A park after this keeps the new one, whose labels and mount are this claim's epoch's (a kept one taken back
664
+ // carried its park's).
665
+ if (keepable)
666
+ keepable.epoch = launch.epoch;
667
+ deps.onComputerNote?.(lostRecreatedWords(name));
668
+ },
669
+ stop: (words) => (deps.stopRun ?? stopRun)(words),
670
+ may: () => inToolCall() && deps.signal?.aborted !== true,
671
+ });
672
+ /** A kept synced computer this claim does not take back, ended before anything of this claim reads the run's folder off
673
+ * the disk, so no write of the old one lands after: one whose end is under way (the parking runner's timer, reading
674
+ * it back) is waited for (keptEnded); one this claim reaches (`providerOf`, its own provider's) it ends, reading it
675
+ * back first; one it doesn't is left ending, read back by no one, for the janitor's next pass to delete. */
676
+ const settleSynced = async (record, providerOf) => {
677
+ const settings = store.settings;
678
+ if (record.folder !== "sync" || !settings)
679
+ return;
680
+ const row = await readKept(settings, launch.runId).catch(() => undefined);
681
+ if (!row || row.ended || row.id !== record.id)
682
+ return;
683
+ const wait = () => keptEnded(settings, launch.runId, KEPT_PULL_MS + KEPT_DELETE_MS, deps.signal);
684
+ if (row.ending)
685
+ return wait();
686
+ const disk = diskOf();
687
+ const ended = await endKept(settings, launch.runId, row, async () => {
688
+ const provider = providerOf ? await providerOf() : undefined;
689
+ if (!provider)
690
+ throw new Error("left to the janitor");
691
+ if (disk)
692
+ await pullKept(provider, row, disk).catch(() => undefined);
693
+ await deleteKept(provider, row);
694
+ }).catch(() => true);
695
+ if (!ended)
696
+ await wait();
697
+ };
438
698
  const side = {
439
- keysOf: providerKeys(kinds, deps.keyOf ?? (async (name) => (deps.sealKey ? await getSealedKey(store, deps.sealKey, name) : undefined))),
699
+ keysOf: providerKeys(kinds, keyOf),
440
700
  answers: deps.answers ?? (async () => false),
441
701
  ...(deps.daemon ? { daemon: deps.daemon } : {}),
442
702
  // The computer an earlier claim kept at the run's park, when it is this setting's (its provider and kind) and its
443
703
  // time is not over: taken off its row first (computer-kept.ts takeKept), so its end and this claim never both have it,
444
704
  // then attached and connected again. Its mount, where it has one, stays the one it was made with: its credential is
445
705
  // this claim's from here (inheritMount). Never the agent's own computer, a paired one or --faux's.
446
- async reattach(entry, { kind, keys, configuration, persistent, signal }) {
706
+ async reattach(entry, { kind, keys, configuration, folder, gpu, persistent, signal }) {
447
707
  const record = kept;
448
708
  kept = undefined;
449
- if (!record || !store.settings || persistent || paired.some((row) => row.id === entry.id))
709
+ if (!record || !store.settings)
450
710
  return undefined;
451
- if (deps.launch && (deps.launches?.(entry.id) ?? true))
452
- return undefined;
453
- if (record.provider !== entry.id || record.kind !== kind)
711
+ if (persistent || paired.some((row) => row.id === entry.id) || (deps.launch && (deps.launches?.(entry.id) ?? true)) || record.provider !== entry.id || record.kind !== kind) {
712
+ await settleSynced(record);
454
713
  return undefined;
714
+ }
455
715
  try {
456
716
  // Its time over, or its end took it first (ending or ended): a new one, said so.
457
717
  if (record.ending || record.ended || now() >= record.until || !(await takeKept(store.settings, launch.runId, record))) {
458
718
  lapsed = true;
719
+ await settleSynced(record, () => vendorProvider(entry, keys, configuration));
459
720
  return undefined;
460
721
  }
461
722
  const provider = await vendorProvider(entry, keys, configuration);
462
- const again = await keptLaunch(provider, record, signal);
463
- if (!again)
723
+ // One kept holding the run's folder another way than this claim holds it (its provider's keys changed since the
724
+ // park: an Archil mount where the run now takes a synced folder) is not taken back: it goes now (a synced folder
725
+ // read back first), its mount's token with it, so nothing holds the folder but the new one.
726
+ if (record.folder !== folder) {
727
+ const disk = record.folder === "sync" ? diskOf() : undefined;
728
+ if (disk)
729
+ await pullKept(provider, record, disk).catch(() => undefined);
730
+ await deleteKept(provider, record).catch(() => undefined);
731
+ if (record.folder === "archil") {
732
+ deps.inheritMount?.();
733
+ await deps.revokeMount?.().catch(() => undefined);
734
+ }
735
+ return undefined;
736
+ }
737
+ const taken = await keptLaunch(provider, record, signal);
738
+ if (!taken)
464
739
  return undefined;
465
740
  deps.inheritMount?.();
741
+ // A sandbox taken back is started again when it is lost as one this claim made (lostAware): the new one is this
742
+ // claim's, made from this claim's spec, its folder mounted.
743
+ const again = record.kind === "sandbox" ? lostAware(taken, renewalOf(provider, entry, () => specOf(entry, provider, kind, configuration, deps.signal, gpu), folder)) : taken;
466
744
  keepable = { provider, entry: entry.id, kind: record.kind, computer: again, epoch: record.epoch };
467
745
  ownKind = record.kind;
468
746
  const screen = record.kind === "sandbox" ? undefined : record.kind;
@@ -485,32 +763,14 @@ store, deps) {
485
763
  // which stands in for the vendors', is not its.
486
764
  const pairedRow = paired.find((row) => row.id === entry.id);
487
765
  if (ownLaunch && !pairedRow) {
488
- const own = await ownLaunch(entry, { kind, keys, configuration, ...(mount ? { mount } : {}), ...(gpu ? { gpu } : {}), ...(signal ? { signal } : {}) });
766
+ const own = await ownLaunch(entry, { kind, keys, configuration, run: launch.runId, ...(mount ? { mount } : {}), ...(gpu ? { gpu } : {}), ...(signal ? { signal } : {}) });
489
767
  const ownWatch = own.watch;
490
768
  made = { id: own.id, ...(ownWatch ? { watch: ownWatch } : {}) };
491
769
  madeBy = entry;
492
770
  return own;
493
771
  }
494
772
  const provider = pairedRow ? pairedProvider(pairedRow, deps.attached?.hub) : await vendorProvider(entry, keys, configuration);
495
- // What this provider makes, as it declares it: the spec asks for one of its networks, within its lifetime
496
- // (computer-deps.ts computerSpecOf).
497
- const { network: networks, maxLifetimeMs } = provider.capabilities();
498
- const run = {
499
- ...(launch.boxAllow === undefined ? {} : { boxAllow: launch.boxAllow }),
500
- ...(launch.boxAllowSet ? { boxAllowSet: true } : {}),
501
- ...(networks ? { networks } : {}),
502
- ...(typeof maxLifetimeMs === "number" ? { maxLifetimeMs } : {}),
503
- };
504
- // A provider that declares it prepares its own image (its prepareImage(): its recipe built when the engine lacks it,
505
- // an image by name pulled) does so first, and the computer starts from the tag it answers: the recipe's tag is in no
506
- // registry, so a run of it unbuilt would be refused. The spec comes first, so a setting it refuses builds nothing. A
507
- // configuration that names its image starts from it, else the kind's default.
508
- const asked = deps.spec(entry, kind, run, configuration.image);
509
- const ready = await readyImage(entry, provider, signal, deps.onImageWait);
510
- const image = ready ?? asked.image;
511
- if (image === undefined)
512
- throw new Error(`${entry.label} names no image for its ${kind} computer and prepares none`);
513
- const spec = { ...asked, image, labels: runLabels(launch.runId, launch.epoch), ...(mount ? { mount } : {}), ...(gpu ? { gpu } : {}) };
773
+ const spec = { ...(await specOf(entry, provider, kind, configuration, signal, gpu)), ...(mount ? { mount } : {}) };
514
774
  // A browser makes nothing here: the run's browser tool opens each session through it, so a session lives (and is
515
775
  // booked) only while the tool holds it.
516
776
  if (kind === "browser") {
@@ -545,12 +805,16 @@ store, deps) {
545
805
  // go. A paired computer is the same one at every claim, and is no computer of the run's own.
546
806
  const journalSays = !pairedRow && launch.epoch > 1 && (await deps.hadComputer?.()) === true;
547
807
  const fresh = await providerLaunch(provider, launch, spec, signal);
548
- launched = fresh;
808
+ // A sandbox the claim made is started again when its provider loses it mid-run (lostAware): never a paired
809
+ // computer, nor a desktop or a phone (a person may hold or watch it), nor the agent's own (persistent, above).
810
+ const { releasedOlder } = fresh;
811
+ const held = kind === "sandbox" && !pairedRow ? lostAware(fresh, renewalOf(provider, entry, async () => spec, mount?.kind)) : fresh;
812
+ launched = held;
549
813
  if (!pairedRow)
550
814
  ownKind = kind;
551
815
  if (!pairedRow && (kind === "desktop" || kind === "phone" || kind === "sandbox"))
552
- keepable = { provider, entry: entry.id, kind, computer: fresh, epoch: launch.epoch };
553
- if (!pairedRow && (fresh.releasedOlder || journalSays))
816
+ keepable = { provider, entry: entry.id, kind, computer: held, epoch: launch.epoch };
817
+ if (!pairedRow && (releasedOlder || journalSays))
554
818
  replaced = { kind: kind, folderKept: deps.mode === 2 && mount !== undefined, ...(lapsed ? { lapsed: true } : {}) };
555
819
  }
556
820
  made = { id: launched.id, machine: launched.machine, ...(launched.watch ? { watch: launched.watch, ...(kind === "desktop" || kind === "phone" ? { gone: goneOf(provider, launched.id, listedUnder, kind) } : {}) } : {}) };
@@ -564,6 +828,13 @@ store, deps) {
564
828
  const resolved = await resolveComputer(setting, deps.mode, kinds, side);
565
829
  if ("refused" in resolved)
566
830
  throw unavailable(resolved.refused);
831
+ // A kept computer this setting can't take back (no computer now, or another kind or provider: reattach sees only its
832
+ // own) is settled before anything reads the run's folder off the disk (settleSynced: a synced one's alone).
833
+ if (kept && (resolved.kind !== kept.kind || resolved.provider !== kept.provider)) {
834
+ const record = kept;
835
+ kept = undefined;
836
+ await settleSynced(record);
837
+ }
567
838
  if (resolved.kind === "none")
568
839
  return { computer: undefined, release: noop };
569
840
  const refusedKind = deps.refuseKind?.(resolved.kind);
@@ -579,7 +850,7 @@ store, deps) {
579
850
  let started;
580
851
  try {
581
852
  // The mount check carries the claim's epoch (a disk read-back under another epoch is refused) unless it names one.
582
- started = await resolved.start({ workspace: deps.workspace, check: { ...deps.check, epoch: deps.check.epoch ?? launch.epoch }, ...(deps.signal ? { signal: deps.signal } : {}) });
853
+ started = await resolved.start({ workspace: deps.workspace, check: { ...deps.check, epoch: deps.check.epoch ?? launch.epoch }, ...(holds ? { holds } : {}), ...(deps.signal ? { signal: deps.signal } : {}) });
583
854
  }
584
855
  catch (error) {
585
856
  await revoke().catch(() => undefined);
@@ -592,7 +863,17 @@ store, deps) {
592
863
  let hold;
593
864
  let held;
594
865
  let view;
595
- let recording;
866
+ // The agent asks for a video of the screen (computer.record): the desktop's or phone's, through a watch of the
867
+ // recorder's own while this claim holds it; each browser session's, through its live view's (the run's browser tool).
868
+ const recordings = [];
869
+ const record = deps.recordings && setting?.record === true
870
+ ? (watch, tag, kind) => {
871
+ const one = startRecording({ ...deps.recordings, watch, tag, kind });
872
+ recordings.push(one);
873
+ return one;
874
+ }
875
+ : undefined;
876
+ const runComputer = computer?.kind === "browser" && record ? { ...computer, browser: { ...computer.browser, record: (id, watch) => record(watch, id, "browser") } } : computer;
596
877
  if (computer && (computer.kind === "desktop" || computer.kind === "phone")) {
597
878
  const theHold = computerHold();
598
879
  if (deps.openComputerView && made?.watch) {
@@ -608,9 +889,8 @@ store, deps) {
608
889
  }
609
890
  view = opened.view;
610
891
  }
611
- // The agent asks for a video of the screen (computer.record): a watch of the recorder's own, while this claim holds it.
612
- if (deps.recordings && made?.watch && setting?.record === true)
613
- recording = startRecording({ ...deps.recordings, watch: made.watch, tag: made.id, kind: computer.kind });
892
+ if (record && made?.watch)
893
+ record(made.watch, made.id, computer.kind);
614
894
  // The run's end closes the hold (workspace/computer-hold.ts close()): a take in force ends `released` (the computer
615
895
  // goes; the composer's `ended` ViewEnd maps onto it), the agent's open ask is let go at once, and nothing is taken after.
616
896
  endHold = () => theHold.close();
@@ -619,7 +899,7 @@ store, deps) {
619
899
  }
620
900
  let released;
621
901
  const release = (why = "released") => (released ??= (async () => {
622
- await recording?.stop(typeof why === "object" ? "parked" : why);
902
+ await Promise.all(recordings.map((one) => one.stop(typeof why === "object" ? "parked" : why)));
623
903
  await Promise.resolve(view?.close(why)).catch(() => undefined);
624
904
  endHold();
625
905
  try {
@@ -634,7 +914,7 @@ store, deps) {
634
914
  const keeps = computer && keepable && store.settings ? keepable : undefined;
635
915
  const settings = store.settings;
636
916
  return {
637
- ...(computer ? { computer } : {}),
917
+ ...(runComputer ? { computer: runComputer } : {}),
638
918
  ...(hold ? { hold } : {}),
639
919
  ...(held ? { held } : {}),
640
920
  ...(replaced ? { replaced } : {}),
@@ -647,9 +927,18 @@ store, deps) {
647
927
  return false;
648
928
  }
649
929
  const ref = keeps.computer.ref;
930
+ // A synced folder's delta lands on the disk before the computer is kept (an Archil mount's modes and links are
931
+ // recorded); one that does not land lets it go as the run's end does, and the park throws the release's words. The
932
+ // timer's read-back below writes after this claim is gone: the kept row's end, which a later claim waits for or
933
+ // takes first, is its fence, not the claim's.
934
+ const synced = resolved.folder === "sync" ? deps.workspace.syncStore?.() : undefined;
935
+ if (started.sync && !(await started.sync()).ok) {
936
+ await release();
937
+ return false;
938
+ }
650
939
  let row;
651
940
  try {
652
- row = await writeKept(settings, launch.runId, { provider: keeps.entry, kind: keeps.kind, id: ref.id, name: ref.name, epoch: keeps.epoch, until: now() + KEEP_MS });
941
+ row = await writeKept(settings, launch.runId, { provider: keeps.entry, kind: keeps.kind, id: ref.id, name: ref.name, epoch: keeps.epoch, until: now() + KEEP_MS, ...(resolved.folder ? { folder: resolved.folder } : {}) });
653
942
  }
654
943
  catch {
655
944
  // No row, nothing to find it by: it goes as at the run's end.
@@ -660,15 +949,19 @@ store, deps) {
660
949
  // kept, never that it ended; the hold with it; the machine and its mount stay.
661
950
  released = (async () => {
662
951
  // The recording pauses: the claim that resumes the run goes on in a new file.
663
- await recording?.stop("parked");
952
+ await Promise.all(recordings.map((one) => one.stop("parked")));
664
953
  await Promise.resolve(view?.close({ parked: cause, until: row.until })).catch(() => undefined);
665
954
  endHold();
666
955
  await keeps.computer.close();
667
956
  })();
668
957
  await released;
669
- // Its end at its time, by this process; the janitor's (computer-kept.ts sweepKept) whatever becomes of it.
958
+ // Its end at its time, by this process; the janitor's (computer-kept.ts sweepKept) whatever becomes of it. A synced
959
+ // folder is read back whole first, through a connection of its own: what its programs wrote since the park. Its
960
+ // row reads ending meanwhile, so a claim that comes then waits for it (keptEnded) before it reads the disk.
670
961
  const timer = setTimeout(() => {
671
962
  void endKept(settings, launch.runId, row, async () => {
963
+ if (synced)
964
+ await pullKept(keeps.provider, ref, synced).catch(() => undefined);
672
965
  await deleteKept(keeps.provider, ref);
673
966
  await revoke().catch(() => undefined);
674
967
  }).catch(() => undefined);
@@ -223,7 +223,8 @@ function heldPort(port, hold) {
223
223
  const what = actOf(port, op, args);
224
224
  if (what === undefined && !LOOKS.has(op))
225
225
  return port.call(op, args, signal);
226
- const parked = hold.state() !== "agent";
226
+ // Awaited: in a run's own process the hold is its runner's, and its state an answer over the channel (run-child.ts).
227
+ const parked = (await hold.state()) !== "agent";
227
228
  const leave = await hold.enter(signal);
228
229
  if (parked && what !== undefined) {
229
230
  leave();
@@ -1,4 +1,4 @@
1
- import type { ComputerProvider } from "@parcha/agentrun-computer";
1
+ import type { ComputerProvider, MountKind } from "@parcha/agentrun-computer";
2
2
  import { type Settings, type Store } from "../store/index.js";
3
3
  /** How long a park keeps the run's computer for its resume: 10 minutes. */
4
4
  export declare const KEEP_MS: number;
@@ -33,12 +33,22 @@ export type Kept = {
33
33
  readonly name: string;
34
34
  readonly epoch: number;
35
35
  readonly until: number;
36
+ /** The kind of the run's folder it holds (a bind, an Archil mount or a synced folder); none: it holds none. A claim
37
+ * takes it back only to hold the folder the same way (computer-for-claim.ts reattach). */
38
+ readonly folder?: MountKind;
36
39
  readonly ending?: true;
37
40
  readonly ended?: true;
38
41
  };
39
42
  /** How long one delete of a kept computer may take at the janitor's pass: past it the pass goes on, the row is left
40
43
  * ending, and the next pass deletes it again (a Docker command has no timeout of its own). */
41
44
  export declare const KEPT_DELETE_MS = 60000;
45
+ /** How long an end's read-back of a kept synced folder may take: past it the read stops, the delete goes on, and a
46
+ * claim that waits on the end (keptEnded) goes on too. */
47
+ export declare const KEPT_PULL_MS = 120000;
48
+ /** Waits while `runId`'s kept row reads ending (its end under way: a synced folder read back onto the disk, then the
49
+ * delete), at most `ms` of real time or until `signal` aborts. A claim does so before its computer reads the run's
50
+ * folder off the disk, so no write of the old one lands after. A row that can't be read is not waited on. */
51
+ export declare function keptEnded(settings: Pick<KeptSettings, "get">, runId: string, ms: number, signal?: AbortSignal): Promise<void>;
42
52
  /** The settings the kept rows live in. */
43
53
  export type KeptSettings = Pick<Settings, "get" | "put" | "replaceIf" | "removeIf" | "remove" | "all">;
44
54
  /** A row's value as a Kept, or undefined for anything else. */
@@ -25,22 +25,43 @@ export const keptKeyOf = (runId) => `${KEPT_PREFIX}${runId}`;
25
25
  /** How long one delete of a kept computer may take at the janitor's pass: past it the pass goes on, the row is left
26
26
  * ending, and the next pass deletes it again (a Docker command has no timeout of its own). */
27
27
  export const KEPT_DELETE_MS = 60_000;
28
+ /** How long an end's read-back of a kept synced folder may take: past it the read stops, the delete goes on, and a
29
+ * claim that waits on the end (keptEnded) goes on too. */
30
+ export const KEPT_PULL_MS = 120_000;
31
+ /** How often a claim waiting on a kept computer's end reads its row again. */
32
+ const KEPT_POLL_MS = 250;
33
+ /** Waits while `runId`'s kept row reads ending (its end under way: a synced folder read back onto the disk, then the
34
+ * delete), at most `ms` of real time or until `signal` aborts. A claim does so before its computer reads the run's
35
+ * folder off the disk, so no write of the old one lands after. A row that can't be read is not waited on. */
36
+ export async function keptEnded(settings, runId, ms, signal) {
37
+ const until = Date.now() + ms;
38
+ while (Date.now() < until && signal?.aborted !== true) {
39
+ const row = await readKept(settings, runId).catch(() => undefined);
40
+ if (!row?.ending)
41
+ return;
42
+ await new Promise((resolve) => setTimeout(resolve, KEPT_POLL_MS));
43
+ }
44
+ }
28
45
  /** A row's value as a Kept, or undefined for anything else. */
29
46
  export function keptOf(value) {
30
47
  if (!value || typeof value !== "object")
31
48
  return undefined;
32
- const { provider, kind, id, name, epoch, until, ending, ended } = value;
49
+ const { provider, kind, id, name, epoch, until, folder, ending, ended } = value;
33
50
  if (typeof provider !== "string" || (kind !== "desktop" && kind !== "phone" && kind !== "sandbox") || typeof id !== "string" || typeof name !== "string")
34
51
  return undefined;
35
52
  if (typeof epoch !== "number" || !Number.isSafeInteger(epoch) || typeof until !== "number" || !Number.isFinite(until))
36
53
  return undefined;
54
+ if (folder !== undefined && folder !== "bind" && folder !== "archil" && folder !== "sync")
55
+ return undefined;
56
+ const held = folder === undefined ? {} : { folder: folder };
37
57
  if (ended === true)
38
- return { provider, kind, id, name, epoch, until, ended: true };
39
- return { provider, kind, id, name, epoch, until, ...(ending === true ? { ending: true } : {}) };
58
+ return { provider, kind, id, name, epoch, until, ...held, ended: true };
59
+ return { provider, kind, id, name, epoch, until, ...held, ...(ending === true ? { ending: true } : {}) };
40
60
  }
41
61
  /** The row as written: its fields in one order, so a compare-and-set reads the same text. */
42
62
  const rowOf = (kept, state) => ({
43
63
  provider: kept.provider, kind: kept.kind, id: kept.id, name: kept.name, epoch: kept.epoch, until: kept.until,
64
+ ...(kept.folder === undefined ? {} : { folder: kept.folder }),
44
65
  ...(state === "ending" ? { ending: true } : state === "ended" ? { ended: true } : {}),
45
66
  });
46
67
  /** `runId`'s kept computer, as its row reads now; undefined when there is none. */