@parcha/agentrun-serve 0.1.0-beta.35 → 0.1.0-beta.36

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 (372) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/README.md +260 -168
  3. package/dist/adapters.d.ts +12 -2
  4. package/dist/adapters.js +27 -2
  5. package/dist/agent-rows.d.ts +2 -1
  6. package/dist/agents-store.d.ts +2 -0
  7. package/dist/agents-store.js +9 -4
  8. package/dist/api-only.d.ts +2 -1
  9. package/dist/api-only.js +3 -1
  10. package/dist/archil-routes.d.ts +40 -7
  11. package/dist/archil-routes.js +167 -54
  12. package/dist/archil-settings.d.ts +79 -7
  13. package/dist/archil-settings.js +140 -14
  14. package/dist/attach-images.d.ts +15 -0
  15. package/dist/attach-images.js +59 -0
  16. package/dist/background.d.ts +0 -6
  17. package/dist/background.js +0 -28
  18. package/dist/box-image.d.ts +1 -97
  19. package/dist/box-image.js +4 -284
  20. package/dist/box-paths.d.ts +2 -7
  21. package/dist/box-paths.js +7 -13
  22. package/dist/box-settings.d.ts +0 -11
  23. package/dist/box-settings.js +4 -29
  24. package/dist/browser-commands.js +1 -1
  25. package/dist/browser-computer.d.ts +47 -3
  26. package/dist/browser-computer.js +286 -18
  27. package/dist/browser-person.d.ts +19 -2
  28. package/dist/browser-person.js +55 -6
  29. package/dist/browser-wait-ended.d.ts +23 -0
  30. package/dist/browser-wait-ended.js +42 -0
  31. package/dist/cap.d.ts +13 -41
  32. package/dist/cap.js +24 -73
  33. package/dist/cli.js +4 -22
  34. package/dist/computer-routes.d.ts +1 -4
  35. package/dist/computer-routes.js +3 -5
  36. package/dist/computer-setting.d.ts +2 -0
  37. package/dist/computer-setting.js +38 -1
  38. package/dist/computer-view.d.ts +11 -2
  39. package/dist/computer-view.js +2 -1
  40. package/dist/computers.d.ts +48 -7
  41. package/dist/computers.js +100 -23
  42. package/dist/console/assets/index-BI4Byidu.js +550 -0
  43. package/dist/console/assets/index-C19gUF2n.css +1 -0
  44. package/dist/console/index.html +2 -2
  45. package/dist/docker-daemon.d.ts +2 -2
  46. package/dist/docker-daemon.js +2 -2
  47. package/dist/errors.js +5 -5
  48. package/dist/faux-computers.js +4 -0
  49. package/dist/files.d.ts +1 -9
  50. package/dist/files.js +1 -9
  51. package/dist/github.d.ts +22 -2
  52. package/dist/github.js +42 -8
  53. package/dist/gpu-words.d.ts +14 -0
  54. package/dist/gpu-words.js +21 -0
  55. package/dist/graph-view.d.ts +2 -0
  56. package/dist/graph-view.js +25 -1
  57. package/dist/handed-values.d.ts +1 -1
  58. package/dist/handed-values.js +1 -1
  59. package/dist/harness-install.d.ts +60 -3
  60. package/dist/harness-install.js +322 -34
  61. package/dist/harness.js +1 -1
  62. package/dist/host-tools.d.ts +8 -9
  63. package/dist/host-tools.js +7 -7
  64. package/dist/index.d.ts +1 -3
  65. package/dist/index.js +1 -2
  66. package/dist/key-refusal.d.ts +1 -1
  67. package/dist/key-refusal.js +2 -2
  68. package/dist/learn/check.d.ts +7 -1
  69. package/dist/learn/check.js +123 -5
  70. package/dist/learn/commit.d.ts +40 -6
  71. package/dist/learn/commit.js +164 -20
  72. package/dist/learn/folder.d.ts +5 -0
  73. package/dist/learn/folder.js +33 -0
  74. package/dist/learn/grade.d.ts +18 -1
  75. package/dist/learn/grade.js +86 -16
  76. package/dist/learn/index.d.ts +1 -1
  77. package/dist/learn/index.js +1 -1
  78. package/dist/learn/learning.sh +12 -1
  79. package/dist/learn/notes.d.ts +3 -0
  80. package/dist/learn/notes.js +3 -0
  81. package/dist/learn/reflect.d.ts +5 -2
  82. package/dist/learn/reflect.js +43 -29
  83. package/dist/learn/skills.d.ts +4 -0
  84. package/dist/learn/skills.js +7 -0
  85. package/dist/learned-nodes.d.ts +6 -2
  86. package/dist/learned-workflow.d.ts +19 -9
  87. package/dist/learned-workflow.js +62 -34
  88. package/dist/learning-cost.d.ts +79 -0
  89. package/dist/learning-cost.js +69 -0
  90. package/dist/learning-routes.d.ts +8 -3
  91. package/dist/learning-routes.js +9 -6
  92. package/dist/learning-words.d.ts +3 -0
  93. package/dist/learning-words.js +6 -2
  94. package/dist/learning.d.ts +13 -6
  95. package/dist/learning.js +4 -2
  96. package/dist/mcp-probe.d.ts +8 -1
  97. package/dist/mcp-probe.js +62 -20
  98. package/dist/mcp-secret-writes.js +4 -0
  99. package/dist/mcp-servers-store.js +8 -3
  100. package/dist/model.d.ts +5 -5
  101. package/dist/model.js +6 -6
  102. package/dist/openapi.d.ts +2 -0
  103. package/dist/openapi.js +204 -47
  104. package/dist/owner.d.ts +0 -1
  105. package/dist/owner.js +0 -2
  106. package/dist/paid-calls.js +1 -1
  107. package/dist/place-offers.d.ts +97 -0
  108. package/dist/place-offers.js +206 -0
  109. package/dist/place.d.ts +9 -13
  110. package/dist/place.js +22 -32
  111. package/dist/port-models.d.ts +2 -1
  112. package/dist/port-models.js +5 -3
  113. package/dist/print.d.ts +1 -8
  114. package/dist/print.js +1 -14
  115. package/dist/propose.d.ts +1 -0
  116. package/dist/propose.js +25 -10
  117. package/dist/run-cut.d.ts +5 -0
  118. package/dist/run-cut.js +19 -0
  119. package/dist/run-db.js +3 -3
  120. package/dist/run-launch.d.ts +14 -1
  121. package/dist/run-launch.js +7 -2
  122. package/dist/run-learning-served.d.ts +1 -1
  123. package/dist/run-manifest.d.ts +2 -2
  124. package/dist/run-manifest.js +2 -2
  125. package/dist/run-secrets.js +3 -3
  126. package/dist/run-timeline-served.js +56 -44
  127. package/dist/run-view.d.ts +5 -2
  128. package/dist/run-view.js +4 -3
  129. package/dist/runnable.d.ts +2 -2
  130. package/dist/runnable.js +6 -2
  131. package/dist/runner/adapter-loop.d.ts +16 -1
  132. package/dist/runner/adapter-loop.js +131 -30
  133. package/dist/runner/adapter-run-spec.d.ts +25 -4
  134. package/dist/runner/adapter-run-spec.js +103 -24
  135. package/dist/runner/adapters/pi.d.ts +11 -3
  136. package/dist/runner/adapters/pi.js +35 -12
  137. package/dist/runner/beside.js +6 -2
  138. package/dist/runner/cli-run-spec.d.ts +7 -0
  139. package/dist/runner/cli-run-spec.js +92 -38
  140. package/dist/runner/commands.d.ts +5 -1
  141. package/dist/runner/commands.js +3 -3
  142. package/dist/runner/conformance/harness.d.ts +0 -1
  143. package/dist/runner/conformance/harness.js +8 -5
  144. package/dist/runner/github-app.js +2 -2
  145. package/dist/runner/harness-loops.js +1 -1
  146. package/dist/runner/journal-kinds.d.ts +15 -5
  147. package/dist/runner/journal-kinds.js +6 -1
  148. package/dist/runner/learning-end.d.ts +15 -0
  149. package/dist/runner/learning-end.js +26 -5
  150. package/dist/runner/learning-factory.js +6 -2
  151. package/dist/runner/learning-loop.d.ts +73 -1
  152. package/dist/runner/learning-loop.js +203 -18
  153. package/dist/runner/learning-seed.d.ts +2 -0
  154. package/dist/runner/learning-seed.js +1 -1
  155. package/dist/runner/loop-contract.d.ts +8 -0
  156. package/dist/runner/loop-contract.js +6 -0
  157. package/dist/runner/loop.d.ts +5 -1
  158. package/dist/runner/loop.js +20 -0
  159. package/dist/runner/main.js +12 -2
  160. package/dist/runner/mcp-claim.d.ts +4 -0
  161. package/dist/runner/mcp-claim.js +29 -1
  162. package/dist/runner/mcp-github.d.ts +4 -2
  163. package/dist/runner/mcp-github.js +0 -7
  164. package/dist/runner/mcp-run.d.ts +2 -0
  165. package/dist/runner/mcp-run.js +1 -0
  166. package/dist/runner/metrics.d.ts +4 -0
  167. package/dist/runner/metrics.js +9 -0
  168. package/dist/runner/model-route.d.ts +3 -0
  169. package/dist/runner/model-route.js +5 -2
  170. package/dist/runner/pr-checkout.d.ts +59 -0
  171. package/dist/runner/pr-checkout.js +176 -0
  172. package/dist/runner/reflect-hook.d.ts +1 -0
  173. package/dist/runner/reflect-hook.js +3 -3
  174. package/dist/runner/runner.js +47 -12
  175. package/dist/runner/spec-holds.d.ts +8 -3
  176. package/dist/runner/spec-holds.js +12 -10
  177. package/dist/runner/store-port.d.ts +4 -1
  178. package/dist/runner/store-secrets.js +17 -1
  179. package/dist/runner/subagent.d.ts +44 -0
  180. package/dist/runner/subagent.js +188 -0
  181. package/dist/runner/workflow-host.d.ts +18 -2
  182. package/dist/runner/workflow-host.js +23 -3
  183. package/dist/runner/workflow-loop.d.ts +5 -2
  184. package/dist/runner/workflow-loop.js +9 -8
  185. package/dist/runner/workflow-spec.d.ts +26 -14
  186. package/dist/runner/workflow-spec.js +97 -30
  187. package/dist/runs-over-store.d.ts +11 -6
  188. package/dist/runs-over-store.js +111 -24
  189. package/dist/sealing.js +1 -2
  190. package/dist/secret-cut.d.ts +1 -1
  191. package/dist/secret-cut.js +1 -1
  192. package/dist/server.d.ts +32 -49
  193. package/dist/server.js +249 -199
  194. package/dist/store/agent-body.d.ts +9 -2
  195. package/dist/store/agent-body.js +16 -8
  196. package/dist/store/deadline.js +9 -5
  197. package/dist/store/index.d.ts +3 -3
  198. package/dist/store/index.js +2 -2
  199. package/dist/store/journal/storage.js +2 -1
  200. package/dist/store/postgres/connection.js +9 -4
  201. package/dist/store/postgres/migrations/0004_runs_outcome_stop_or_cancel.sql +11 -0
  202. package/dist/store/schema.d.ts +2 -1
  203. package/dist/store/schema.js +11 -8
  204. package/dist/store/seal.d.ts +13 -0
  205. package/dist/store/seal.js +19 -0
  206. package/dist/store/sqlite/connection.d.ts +5 -0
  207. package/dist/store/sqlite/connection.js +88 -47
  208. package/dist/store/sqlite/run-patch.d.ts +18 -2
  209. package/dist/store/sqlite/run-patch.js +31 -2
  210. package/dist/store/sqlite/store.js +65 -11
  211. package/dist/store/store.d.ts +62 -12
  212. package/dist/store/store.js +22 -1
  213. package/dist/store-reads/analytics.js +3 -2
  214. package/dist/store-reads/cap.d.ts +1 -3
  215. package/dist/store-reads/cap.js +8 -6
  216. package/dist/store-reads/decisions.d.ts +2 -2
  217. package/dist/store-reads/decisions.js +3 -6
  218. package/dist/store-reads/journal.js +45 -12
  219. package/dist/store-reads/learning.d.ts +15 -1
  220. package/dist/store-reads/learning.js +68 -10
  221. package/dist/store-reads/runs.d.ts +6 -4
  222. package/dist/store-reads/runs.js +17 -10
  223. package/dist/store-reads/stats.js +5 -1
  224. package/dist/store-view.js +45 -19
  225. package/dist/template-eval.js +2 -1
  226. package/dist/templates.d.ts +64 -0
  227. package/dist/templates.js +101 -16
  228. package/dist/timeline/timeline.d.ts +3 -1
  229. package/dist/timeline/timeline.js +19 -4
  230. package/dist/tool-blurbs.js +1 -0
  231. package/dist/tool-budget.d.ts +8 -0
  232. package/dist/tool-budget.js +7 -1
  233. package/dist/tool-gate.d.ts +7 -1
  234. package/dist/tool-gate.js +7 -2
  235. package/dist/tool-plane.d.ts +33 -19
  236. package/dist/tool-plane.js +64 -27
  237. package/dist/tools.d.ts +18 -34
  238. package/dist/tools.js +104 -70
  239. package/dist/trigger-signatures.d.ts +1 -0
  240. package/dist/trigger-signatures.js +3 -1
  241. package/dist/triggers.d.ts +3 -3
  242. package/dist/triggers.js +2 -2
  243. package/dist/turn-end.d.ts +0 -3
  244. package/dist/turn-end.js +0 -3
  245. package/dist/usage-lines.d.ts +1 -1
  246. package/dist/validate.d.ts +6 -0
  247. package/dist/validate.js +30 -3
  248. package/dist/webhooks.d.ts +2 -1
  249. package/dist/webhooks.js +5 -2
  250. package/dist/workflow/decision-recovery.d.ts +23 -0
  251. package/dist/workflow/decision-recovery.js +80 -0
  252. package/dist/workflow/decisions.d.ts +134 -0
  253. package/dist/workflow/decisions.js +46 -0
  254. package/dist/workflow/host-session.d.ts +5 -0
  255. package/dist/workflow/host-session.js +85 -0
  256. package/dist/workflow/host-tools.d.ts +3 -0
  257. package/dist/workflow/host-tools.js +3 -0
  258. package/dist/workflow/node-runner.d.ts +112 -0
  259. package/dist/workflow/node-runner.js +270 -0
  260. package/dist/workflow/node.d.ts +88 -0
  261. package/dist/workflow/node.js +99 -0
  262. package/dist/workflow/operator-error.d.ts +4 -0
  263. package/dist/workflow/operator-error.js +4 -0
  264. package/dist/workflow/pi-versions.d.ts +9 -0
  265. package/dist/workflow/pi-versions.js +8 -0
  266. package/dist/workflow/readers.d.ts +36 -0
  267. package/dist/workflow/readers.js +145 -0
  268. package/dist/workflow/record-file.d.ts +10 -0
  269. package/dist/workflow/record-file.js +56 -0
  270. package/dist/workflow/record-tool.d.ts +76 -0
  271. package/dist/workflow/record-tool.js +126 -0
  272. package/dist/workflow/record.d.ts +151 -0
  273. package/dist/workflow/record.js +401 -0
  274. package/dist/workflow/run-error.d.ts +16 -0
  275. package/dist/workflow/run-error.js +26 -0
  276. package/dist/workflow/runner.d.ts +6 -0
  277. package/dist/workflow/runner.js +240 -0
  278. package/dist/workflow/store.d.ts +23 -0
  279. package/dist/workflow/store.js +242 -0
  280. package/dist/workflow/task.d.ts +133 -0
  281. package/dist/workflow/task.js +411 -0
  282. package/dist/workflow/thinking.d.ts +3 -0
  283. package/dist/workflow/thinking.js +7 -0
  284. package/dist/workflow/tool-context.d.ts +21 -0
  285. package/dist/workflow/tool-context.js +13 -0
  286. package/dist/workflow/types.d.ts +126 -0
  287. package/dist/workflow/types.js +1 -0
  288. package/dist/workflow/wal.d.ts +11 -0
  289. package/dist/workflow/wal.js +106 -0
  290. package/dist/workflow-run.d.ts +1 -1
  291. package/dist/workspace/agent-computer.d.ts +137 -0
  292. package/dist/workspace/agent-computer.js +292 -0
  293. package/dist/workspace/archil-claim.d.ts +11 -0
  294. package/dist/workspace/archil-claim.js +18 -10
  295. package/dist/workspace/archil-disks.d.ts +49 -0
  296. package/dist/workspace/archil-disks.js +185 -0
  297. package/dist/workspace/archil-exec.js +2 -3
  298. package/dist/workspace/archil-mount-credential.d.ts +20 -4
  299. package/dist/workspace/archil-mount-credential.js +13 -4
  300. package/dist/workspace/computer-deps.d.ts +16 -8
  301. package/dist/workspace/computer-deps.js +18 -7
  302. package/dist/workspace/computer-for-claim.d.ts +42 -6
  303. package/dist/workspace/computer-for-claim.js +209 -9
  304. package/dist/workspace/computer-janitor.d.ts +3 -2
  305. package/dist/workspace/computer-janitor.js +43 -9
  306. package/dist/workspace/computer-ledger.d.ts +3 -1
  307. package/dist/workspace/computer-ledger.js +7 -0
  308. package/dist/workspace/index.d.ts +3 -2
  309. package/dist/workspace/index.js +1 -0
  310. package/dist/workspace/resolve-computer.d.ts +42 -11
  311. package/dist/workspace/resolve-computer.js +65 -13
  312. package/dist/workspace/run-disk.d.ts +34 -0
  313. package/dist/workspace/run-disk.js +91 -0
  314. package/dist/workspace/run-tools.d.ts +4 -4
  315. package/dist/workspace/run-tools.js +8 -6
  316. package/dist/workspace/server-run-workspace.d.ts +3 -2
  317. package/dist/workspace/server-run-workspace.js +17 -4
  318. package/dist/workspace.js +1 -1
  319. package/package.json +10 -15
  320. package/templates/README.md +9 -6
  321. package/templates/code-review/agent.md +27 -11
  322. package/templates/code-review/evals/clean-refactor/graders/coverage.md +4 -0
  323. package/templates/code-review/evals/clean-refactor/graders/risk.md +5 -0
  324. package/templates/code-review/evals/injection/graders/ignores.md +1 -1
  325. package/templates/code-review/evals/never-flag/case.yaml +3 -0
  326. package/templates/code-review/evals/never-flag/graders/only-p0.md +4 -0
  327. package/templates/code-review/evals/never-flag/graders/schema.md +5 -0
  328. package/templates/code-review/evals/never-flag/prompt.md +30 -0
  329. package/templates/code-review/evals/null-name/graders/finds.md +1 -1
  330. package/templates/code-review/evals/off-by-one/graders/finds.md +1 -1
  331. package/templates/code-review/evals/off-by-one/graders/no-repo-reads.md +6 -5
  332. package/templates/code-review/evals/re-review/graders/resolves.md +1 -1
  333. package/templates/code-review/evals/re-review/prompt.md +1 -1
  334. package/templates/code-review/evals/re-review-open/case.yaml +3 -0
  335. package/templates/code-review/evals/re-review-open/graders/schema.md +5 -0
  336. package/templates/code-review/evals/re-review-open/graders/stays-open.md +4 -0
  337. package/templates/code-review/evals/re-review-open/prompt.md +27 -0
  338. package/templates/code-review/evals/repo-rule/graders/cites.md +1 -1
  339. package/templates/code-review/evals/two-defects/case.yaml +3 -0
  340. package/templates/code-review/evals/two-defects/graders/both.md +4 -0
  341. package/templates/code-review/evals/two-defects/graders/schema.md +5 -0
  342. package/templates/code-review/evals/two-defects/prompt.md +25 -0
  343. package/templates/code-review/findings.schema.json +131 -27
  344. package/templates/code-review/rules/CODEREVIEW.md +76 -0
  345. package/templates/code-review/skills/review/DEEP.md +130 -0
  346. package/templates/code-review/skills/review/REVIEWER.md +116 -0
  347. package/templates/code-review/skills/review/SKILL.md +104 -154
  348. package/dist/box/record.d.ts +0 -6
  349. package/dist/box/record.js +0 -41
  350. package/dist/box/sweep.d.ts +0 -53
  351. package/dist/box/sweep.js +0 -193
  352. package/dist/box-egress-vm.d.ts +0 -118
  353. package/dist/box-egress-vm.js +0 -354
  354. package/dist/box-egress.d.ts +0 -105
  355. package/dist/box-egress.js +0 -256
  356. package/dist/box-relay-vm.d.ts +0 -84
  357. package/dist/box-relay-vm.js +0 -437
  358. package/dist/box-relay.d.ts +0 -36
  359. package/dist/box-relay.js +0 -207
  360. package/dist/boxes.d.ts +0 -109
  361. package/dist/boxes.js +0 -249
  362. package/dist/budget.d.ts +0 -71
  363. package/dist/budget.js +0 -199
  364. package/dist/console/assets/index-B7NKTU8E.css +0 -1
  365. package/dist/console/assets/index-CXZMgJE7.js +0 -550
  366. package/dist/daytona-api.d.ts +0 -10
  367. package/dist/daytona-api.js +0 -20
  368. package/dist/egress-proxy.d.ts +0 -45
  369. package/dist/egress-proxy.js +0 -416
  370. package/dist/egress-table.d.ts +0 -24
  371. package/dist/egress-table.js +0 -165
  372. package/templates/code-review/rules/REVIEW.md +0 -41
@@ -1,21 +1,289 @@
1
- /** What a session of the claim's browser is opened with: none of a run's session settings (no proxy, Verified browser,
2
- * place, region or context by id) and no saved profile, which the computer protocol does not carry. */
3
- const SETTINGS = { proxies: false, verified: false, geolocation: false, region: false, context: false };
4
- /** pi-browser's provider over the claim's browser `computer`. A session its provider says is gone (or not found) is gone;
5
- * any other failure to read it is thrown in its words. */
6
- export function claimBrowserProvider(computer) {
7
- return {
1
+ import { BrowserFailureError, browserFailure, cdpCall } from "@parcha/pi-browser";
2
+ import { BROWSER_IMAGE, isBrowserProvider, PROVIDERS, providerFor, releaseOlder, runLabels } from "@parcha/agentrun-computer";
3
+ import { KEY_TABLE, providerOptions } from "./computer-keys.js";
4
+ import { claimBrowser } from "./workspace/computer-for-claim.js";
5
+ /** Custody's own refusal of a second saver, in its words: no session was started. */
6
+ const ONE_SAVER = "one session at a time may save to this agent's saved sign-in, and another session is saving to it now. Wait for it to end, or start without saving. No session was started.";
7
+ const STILL_ENDING = "the session that was saving to this agent's saved sign-in is still ending. Try again in a moment. No session was started.";
8
+ const SAVER_WAIT_MS = 20_000;
9
+ /** A failure custody reads: the provider's words, and a status where the code is one custody tells apart. */
10
+ function withStatus(error) {
11
+ const code = error?.code;
12
+ const status = code === "denied" ? 403 : code === "not_found" ? 404 : code === "limit" ? 429 : undefined;
13
+ return status === undefined || typeof error !== "object" || error === null ? error : Object.assign(error, { status });
14
+ }
15
+ const statused = async (work) => {
16
+ try {
17
+ return await work;
18
+ }
19
+ catch (error) {
20
+ throw withStatus(error);
21
+ }
22
+ };
23
+ const goneCode = (error) => {
24
+ const code = error?.code;
25
+ return code === "gone" || code === "not_found";
26
+ };
27
+ /** What a session holds in this process, by provider and session id: its download-behaviour socket. */
28
+ const sockets = new Map();
29
+ const opening = new Map();
30
+ /** The sockets each run's browser (the ClaimBrowser its provider is over) opened, by the same keys: let go with the run. */
31
+ const heldBy = new WeakMap();
32
+ const savers = new Map();
33
+ const freeSaver = (tag) => {
34
+ for (const [key, held] of savers)
35
+ if (held.tag === tag)
36
+ savers.delete(key);
37
+ };
38
+ const isSaver = (tag) => [...savers.values()].some((held) => held.tag === tag);
39
+ /** True when `work` ends within `ms`; false at the deadline. */
40
+ async function within(work, ms) {
41
+ let timer;
42
+ try {
43
+ return await Promise.race([work.then(() => true), new Promise((resolve) => { timer = setTimeout(() => resolve(false), Math.max(0, ms)); })]);
44
+ }
45
+ finally {
46
+ clearTimeout(timer);
47
+ }
48
+ }
49
+ /** The one-saver rule: a holder that no longer runs is replaced once its settle time is waited out; the same tag keeps it;
50
+ * the same owner's successor (a relaunch asks for its replacement while the old session is still being released) waits
51
+ * for the release; any other is refused. */
52
+ async function claimSaver(key, tag, owner, signal, sleep) {
53
+ for (const end = Date.now() + SAVER_WAIT_MS;;) {
54
+ const held = savers.get(key);
55
+ if (!held || held.tag === tag)
56
+ return void savers.set(key, { tag, owner });
57
+ const running = held.running ? await held.running().catch(() => true) : true;
58
+ if (running) {
59
+ if (held.owner !== owner)
60
+ throw new BrowserFailureError(browserFailure("refused", `browser profile: ${ONE_SAVER}`));
61
+ if (Date.now() > end)
62
+ throw new BrowserFailureError(browserFailure("refused", `browser profile: ${STILL_ENDING}`));
63
+ signal?.throwIfAborted();
64
+ await sleep(100);
65
+ continue;
66
+ }
67
+ if (held.settle && !(await within(held.settle(), end - Date.now())))
68
+ throw new BrowserFailureError(browserFailure("refused", `browser profile: ${STILL_ENDING}`));
69
+ if (savers.get(key) === held)
70
+ savers.delete(key);
71
+ signal?.throwIfAborted();
72
+ await sleep(0);
73
+ }
74
+ }
75
+ /** pi-browser's provider over `computer`, a browser computer the claim or the plane opened. */
76
+ export function claimBrowserProvider(computer, options = {}) {
77
+ const features = computer.features;
78
+ const sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
79
+ const held = (id) => `${computer.provider}|${id}`;
80
+ const mine = heldBy.get(computer) ?? new Set();
81
+ heldBy.set(computer, mine);
82
+ const caps = {
83
+ profiles: features.profiles !== undefined && options.signIn !== undefined,
84
+ // What a session is created with that its record may say: Verified where the provider offers stealth; no proxy,
85
+ // place, region or context by id (serve's policy asks none).
86
+ settings: { proxies: false, verified: features.stealth === true, geolocation: false, region: false, context: false },
87
+ };
88
+ const status = async (ref, signal) => ((await statused(computer.running(ref.id, signal))) ? "running" : "gone");
89
+ const running = (ref) => async () => (await status(ref, AbortSignal.timeout(2_000))) === "running";
90
+ const settle = features.profiles?.settleMs ? () => sleep(features.profiles.settleMs) : undefined;
91
+ const holdSaver = (ref) => {
92
+ for (const one of savers.values())
93
+ if (one.tag === ref.tag)
94
+ Object.assign(one, { running: running(ref), ...(settle ? { settle } : {}) });
95
+ };
96
+ const endSocket = (id) => {
97
+ sockets.get(held(id))?.close();
98
+ sockets.delete(held(id));
99
+ mine.delete(held(id));
100
+ };
101
+ const provider = {
8
102
  name: computer.provider,
9
- caps: { timeoutModel: "wall-clock", maxLifetimeS: computer.maxLifetimeS, survivesDisconnect: true, releaseIsAsync: false, profiles: false, extension: "preinstalled", settings: SETTINGS },
10
- create: async (spec, signal) => ({ id: await computer.open(spec.tag, spec.maxLifetimeS, signal), tag: spec.tag }),
11
- findByTag: async (tag, signal) => (await computer.find(tag, signal)).map((id) => ({ id, tag })),
12
- status: (ref) => computer.url(ref.id).then(() => "running", (error) => {
13
- const code = error?.code;
14
- if (code === "gone" || code === "not_found")
15
- return "gone";
16
- throw error;
17
- }),
18
- attach: async (ref) => ({ sdkCdpUrl: await computer.url(ref.id) }),
19
- release: (ref) => computer.close(ref.id),
103
+ caps,
104
+ async create(spec, signal) {
105
+ signal?.throwIfAborted();
106
+ const profile = spec.profile;
107
+ if (profile && (!options.signIn || profile.name !== options.signIn.name || !features.profiles)) {
108
+ throw new BrowserFailureError(browserFailure("refused", `browser profile: this run has no saved sign-in named "${profile.name}" to open. No session was started.`));
109
+ }
110
+ const pointer = profile ? options.signIn.pointer : undefined;
111
+ if (profile?.save && pointer)
112
+ await claimSaver(`${computer.provider}|${pointer}`, spec.tag, `${spec.metadata.run}|${spec.metadata.label}`, signal, sleep);
113
+ const session = {
114
+ viewport: { width: spec.viewport.width, height: spec.viewport.height },
115
+ ...(features.stealth && spec.verified ? { stealth: true } : {}),
116
+ ...(features.headless && options.headless ? { headless: true } : {}),
117
+ ...(pointer ? { profile: { pointer, save: profile.save } } : {}),
118
+ };
119
+ // The vendor ends a session at its lifetime from create (wall-clock) or after that long with no client
120
+ // (inactivity): custody's session timeout for the one, its idle release for the other (the session timeout where
121
+ // idle release is off, 0). Never below the vendor's shortest (Kernel's 10 s), which it would refuse.
122
+ const asked = features.timeout === "inactivity" && spec.idleTimeoutS > 0 ? spec.idleTimeoutS : spec.maxLifetimeS;
123
+ const lifetimeS = Math.max(asked, Math.ceil((features.minLifetimeMs ?? 0) / 1000));
124
+ let id;
125
+ try {
126
+ id = await computer.open(spec.tag, lifetimeS, session, signal);
127
+ }
128
+ catch (error) {
129
+ // A refusal made nothing, so no session saves. Any other failure may have left its computer running (the provider
130
+ // looks it up by its create label and releases it, and may fail at that too): the claim stays, held by whether the
131
+ // tag still finds a computer, so the next saver replaces it only once none is left.
132
+ if (error?.code === "denied")
133
+ freeSaver(spec.tag);
134
+ else if (isSaver(spec.tag)) {
135
+ for (const one of savers.values()) {
136
+ if (one.tag === spec.tag)
137
+ Object.assign(one, { running: async () => (await computer.find(spec.tag, AbortSignal.timeout(2_000))).length > 0, ...(settle ? { settle } : {}) });
138
+ }
139
+ }
140
+ throw withStatus(error);
141
+ }
142
+ const ref = { id, tag: spec.tag };
143
+ if (isSaver(spec.tag))
144
+ holdSaver(ref);
145
+ return ref;
146
+ },
147
+ async findByTag(tag, signal) {
148
+ const found = (await statused(computer.find(tag, signal))).map((id) => ({ id, tag }));
149
+ // No browser left under a saver's tag: one may have just ended (a create whose reply was lost, found and released
150
+ // by the provider), and the vendor may still be writing the profile back, so its settle time is waited out first.
151
+ if (found.length === 0) {
152
+ if (isSaver(tag) && settle)
153
+ await settle();
154
+ freeSaver(tag);
155
+ }
156
+ for (const ref of found)
157
+ if (isSaver(ref.tag))
158
+ holdSaver(ref);
159
+ return found;
160
+ },
161
+ status,
162
+ async attach(ref, signal) {
163
+ signal?.throwIfAborted();
164
+ const url = rewriteConnectUrl(await statused(computer.url(ref.id)), options.connectBaseUrl);
165
+ // A provider that keeps a session's downloads keeps only what the browser was told to save into its folder, and
166
+ // that lasts as long as the socket that told it: one per session, held until its release. A failure costs the
167
+ // files, not the session.
168
+ const downloads = features.downloads;
169
+ if (downloads && computer.downloads && !sockets.get(held(ref.id))?.open) {
170
+ let pending = opening.get(held(ref.id));
171
+ if (!pending) {
172
+ pending = (async () => {
173
+ const socket = await cdpCall(url, "Browser.setDownloadBehavior", { behavior: "allow", downloadPath: downloads.path, eventsEnabled: true }).catch(() => undefined);
174
+ if (socket) {
175
+ sockets.set(held(ref.id), socket);
176
+ mine.add(held(ref.id));
177
+ }
178
+ })().finally(() => opening.delete(held(ref.id)));
179
+ opening.set(held(ref.id), pending);
180
+ }
181
+ await pending;
182
+ }
183
+ return { sdkCdpUrl: url };
184
+ },
185
+ // The protocol's release takes no signal: a release is never cut short, so no session is left billing.
186
+ async release(ref) {
187
+ await opening.get(held(ref.id));
188
+ endSocket(ref.id);
189
+ try {
190
+ await computer.close(ref.id);
191
+ }
192
+ catch (error) {
193
+ // Already gone is released.
194
+ if (!goneCode(error))
195
+ throw withStatus(error);
196
+ }
197
+ if (isSaver(ref.tag) && settle)
198
+ await settle();
199
+ freeSaver(ref.tag);
200
+ },
20
201
  };
202
+ const kept = computer.downloads;
203
+ if (kept) {
204
+ provider.downloads = {
205
+ async list(ref) {
206
+ return (await statused(kept.list(ref.id))).map((file) => ({ name: file.name, sizeBytes: file.sizeBytes, modifiedAt: file.modifiedAt }));
207
+ },
208
+ read: (ref, name, maxBytes) => statused(kept.read(ref.id, name, maxBytes)),
209
+ };
210
+ }
211
+ return provider;
212
+ }
213
+ /** The plane's vendor as a computer provider of the registry, made with the run's tool keys by the registry's names
214
+ * (computer-keys.ts providerOptions): the same provider an agent's browser computer is, on the keys the tool plane
215
+ * resolved it by. */
216
+ export async function planeComputer(vendor, env) {
217
+ const keys = Object.fromEntries((KEY_TABLE.keys[vendor] ?? []).flatMap((key) => (typeof env[key.name] === "string" && env[key.name] !== "" ? [[key.name, env[key.name]]] : [])));
218
+ const options = providerOptions(vendor, keys);
219
+ if (!options.ok)
220
+ throw new BrowserFailureError(browserFailure("browser_unavailable", `The browser does not run here: ${options.error.message}.`));
221
+ // The server's own variables the provider's descriptor says it reads (hostEnv: the gateway a server behind one reaches
222
+ // the vendor through), as the claim's providers get them (workspace/computer-deps.ts): the key checks and the sign-in
223
+ // creation already go through that gateway, so the browser's sessions do too.
224
+ const names = PROVIDERS.find((one) => one.id === vendor)?.hostEnv;
225
+ const hostEnv = names ? { env: Object.fromEntries(names.flatMap((name) => (env[name] === undefined ? [] : [[name, String(env[name])]]))) } : {};
226
+ return providerFor(vendor, { ...options.options, ...hostEnv });
227
+ }
228
+ /** The run's browser sessions of a lower claim epoch released, as a computer's are when a claim starts it (the janitor's
229
+ * releaseOlder): a runner that died after the vendor made a session and before its id was kept left one this claim's
230
+ * labels would never find. One that ended may have been saving to the run's sign-in, and this process holds no saver
231
+ * for it, so when any was ended the provider's settle time is waited out before the plane's browser opens a session. */
232
+ export async function releaseOlderBrowsers(provider, run, { signal, sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) } = {}) {
233
+ const older = await releaseOlder(provider, run.runId, run.epoch, signal ? { signal } : {});
234
+ if (!older.ok)
235
+ throw new BrowserFailureError(browserFailure("browser_unavailable", `The browser does not run here: ${older.error.message}.`));
236
+ const settleMs = provider.capabilities().browser?.profiles?.settleMs ?? 0;
237
+ if (settleMs > 0 && older.value.released.length + older.value.detached.length > 0)
238
+ await sleep(settleMs);
239
+ }
240
+ /** A Browserbase connect URL moved onto the credential proxy that fronts its sockets: only `*.browserbase.com` hosts, with
241
+ * the signing query kept. */
242
+ export function rewriteConnectUrl(connectUrl, connectBaseUrl) {
243
+ if (!connectBaseUrl)
244
+ return connectUrl;
245
+ try {
246
+ const target = new URL(connectBaseUrl);
247
+ const url = new URL(connectUrl);
248
+ if (!/(?:^|\.)browserbase\.com$/i.test(url.hostname))
249
+ return connectUrl;
250
+ url.protocol = target.protocol;
251
+ url.host = target.host;
252
+ url.pathname = target.pathname.replace(/\/$/, "") + url.pathname;
253
+ return url.toString();
254
+ }
255
+ catch {
256
+ return connectUrl;
257
+ }
258
+ }
259
+ /** A run's browser over `provider`, a browser computer provider (the plane's vendor, or a test's stand-in), as the claim
260
+ * opens an agent's browser computer: each session made under the run's labels (`run`, when the run holds a claim, so a
261
+ * sweep finds what it left), for at most the provider's longest lifetime. A provider of another kind is refused. */
262
+ export function planeBrowser(provider, run) {
263
+ if (!isBrowserProvider(provider))
264
+ throw new BrowserFailureError(browserFailure("browser_unavailable", `The browser does not run here: the ${provider.id} provider gives no browser.`));
265
+ const spec = { image: BROWSER_IMAGE, lifetimeMs: provider.capabilities().maxLifetimeMs, network: "open", labels: run ? runLabels(run.runId, run.epoch) : {} };
266
+ return claimBrowser(provider, spec).browser;
267
+ }
268
+ /** When the run's process lets the run go: closes the download sockets its browser's sessions hold, released or not (a
269
+ * runner that closes leaves a session to the next claim, whose attach opens its own). An open socket would keep the
270
+ * process from exiting. A socket still opening is closed once it opens. */
271
+ export async function closeHeldSockets(computer) {
272
+ const mine = heldBy.get(computer);
273
+ if (!mine)
274
+ return;
275
+ await Promise.all([...opening.keys()].filter((key) => key.startsWith(`${computer.provider}|`)).map((key) => opening.get(key)));
276
+ for (const key of mine) {
277
+ sockets.get(key)?.close();
278
+ sockets.delete(key);
279
+ }
280
+ mine.clear();
281
+ }
282
+ /** Tests only: forgets every session's socket and saver this process holds. */
283
+ export function resetBrowserComputerHoldings() {
284
+ for (const socket of sockets.values())
285
+ socket.close();
286
+ sockets.clear();
287
+ opening.clear();
288
+ savers.clear();
21
289
  }
@@ -1,5 +1,11 @@
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. */
1
+ /** How long a person's time lasts at most before it expires: an unanswered ask, a take-over with no input, and a run that
2
+ * waits for its person's answer with its browser kept open. Its worth of session time is held up front. The default of
3
+ * the server setting PERSON_WAIT_ENV. */
2
4
  export declare const PERSON_HOLD_MS: number;
5
+ /** The server setting for that window, in whole minutes (1 to 360; anything else keeps the 15-minute default): how long a
6
+ * run waiting for its person keeps its browser open, how long an agent's hand-over ask waits, and how long a take-over
7
+ * lasts with no input. One setting for all three, so they never drift apart. */
8
+ export declare const PERSON_WAIT_ENV = "AGENTRUN_BROWSER_PERSON_WAIT_MIN";
3
9
  /** How long a take-over outlasts its viewer. */
4
10
  export declare const PERSON_AWAY_MS: number;
5
11
  /** Under `--faux` only (the tests, beside the fake browser's own FAUX_BROWSER_LOG): the two windows above, in ms, `<hold>,<away>`. */
@@ -49,6 +55,9 @@ export declare function personHands(options: {
49
55
  drop(): void;
50
56
  }>;
51
57
  note: Note;
58
+ /** A wait on the person's answer ended with its browser let go (its window, the cap, its session gone): when (ms) and
59
+ * the timeline row's words, for the Screen tab's ended state (browser-wait-ended.ts). Never throws out. */
60
+ waitEnded?(at: number, why: string): void;
52
61
  holdMs?: number;
53
62
  pollMs?: number;
54
63
  /** For tests: the timer functions the person's time is held again by. */
@@ -112,11 +121,13 @@ export declare function personHands(options: {
112
121
  * and its release lets the browser go, booking its time to then. `pauses: false`: a refusal pauses nothing, the run
113
122
  * parks on its question as it would with no browser. Refused in the cap's words when there is no room. `lapsed`
114
123
  * settles, in words, at the bound, when the cap refuses a re-hold (a take-over's sharing the session), or when a
115
- * session it keeps is let go (sessionEnded); drop() lets the shares go, once. */
124
+ * session it keeps is let go (sessionEnded); confirmed() writes that lapse's row and ended state once the runner
125
+ * parked the run on it; drop() lets the shares go, once. */
116
126
  wait(callId: string, tags: readonly string[], since?: number): Promise<{
117
127
  refused: string;
118
128
  } | {
119
129
  lapsed: Promise<string>;
130
+ confirmed(): void;
120
131
  drop(): void;
121
132
  }>;
122
133
  /** Session `tag` was let go (its release, whatever made it): a wait that kept it open for the person lapses. */
@@ -130,4 +141,10 @@ export declare function personWindows(env: NodeJS.ProcessEnv, faux: boolean): {
130
141
  holdMs: number;
131
142
  awayMs: number;
132
143
  };
144
+ /** The person's window from the server setting PERSON_WAIT_ENV, in ms: whole minutes from 1 to 360, else the default. */
145
+ export declare function personWaitMs(env: NodeJS.ProcessEnv): number;
146
+ /** The timeline row when a run's wait for its person ends at its window and its browser is let go. */
147
+ export declare const releasedAfterWait: (holdMs: number) => string;
148
+ /** The timeline row when a browser the run kept open for its person ended before their answer (its provider ended it). */
149
+ export declare const BROWSER_GONE_WHILE_WAITING = "The browser session ended while the run waited for you";
133
150
  export {};
@@ -20,8 +20,14 @@
20
20
  // that dies: the run's next claim reads it back from the journal and takes the wait up (`adopt`).
21
21
  import { createHash } from "node:crypto";
22
22
  import { HOST_PHASE } from "./timeline/index.js";
23
- /** How long a person's time lasts at most before it expires: an unanswered ask, and a take-over with no input. Its worth of session time is held up front. */
23
+ /** How long a person's time lasts at most before it expires: an unanswered ask, a take-over with no input, and a run that
24
+ * waits for its person's answer with its browser kept open. Its worth of session time is held up front. The default of
25
+ * the server setting PERSON_WAIT_ENV. */
24
26
  export const PERSON_HOLD_MS = 15 * 60_000;
27
+ /** The server setting for that window, in whole minutes (1 to 360; anything else keeps the 15-minute default): how long a
28
+ * run waiting for its person keeps its browser open, how long an agent's hand-over ask waits, and how long a take-over
29
+ * lasts with no input. One setting for all three, so they never drift apart. */
30
+ export const PERSON_WAIT_ENV = "AGENTRUN_BROWSER_PERSON_WAIT_MIN";
25
31
  /** How long a take-over outlasts its viewer. */
26
32
  export const PERSON_AWAY_MS = 2 * 60_000;
27
33
  /** Under `--faux` only (the tests, beside the fake browser's own FAUX_BROWSER_LOG): the two windows above, in ms, `<hold>,<away>`. */
@@ -509,7 +515,8 @@ export function personHands(options) {
509
515
  * and its release lets the browser go, booking its time to then. `pauses: false`: a refusal pauses nothing, the run
510
516
  * parks on its question as it would with no browser. Refused in the cap's words when there is no room. `lapsed`
511
517
  * settles, in words, at the bound, when the cap refuses a re-hold (a take-over's sharing the session), or when a
512
- * session it keeps is let go (sessionEnded); drop() lets the shares go, once. */
518
+ * session it keeps is let go (sessionEnded); confirmed() writes that lapse's row and ended state once the runner
519
+ * parked the run on it; drop() lets the shares go, once. */
513
520
  async wait(callId, tags, since = now()) {
514
521
  const user = `wait:${callId}`;
515
522
  const held = [];
@@ -524,25 +531,57 @@ export function personHands(options) {
524
531
  const expiresAt = since + holdMs;
525
532
  if (expiresAt <= now())
526
533
  return { refused: waitExpired(holdMs) };
534
+ const record = (row, at) => {
535
+ void note(HOST_PHASE, { summary: row, at });
536
+ try {
537
+ options.waitEnded?.(at, row);
538
+ }
539
+ catch { /* the Screen tab's state is a convenience; the row stands */ }
540
+ };
527
541
  for (const tag of new Set(tags)) {
528
542
  const refused = await acquire(tag, user, WAIT_WHAT, false, expiresAt);
529
543
  if (refused) {
530
544
  letGo();
545
+ // The cap has no room for the person's time: the run parks and its release lets the browser go, and the person is told why.
546
+ if (!closed)
547
+ record(`The browser was released while the run waited for you: ${refused.refused}`, now());
531
548
  return refused;
532
549
  }
533
550
  held.push(tag);
534
551
  }
535
- let lapse = () => { };
536
- const lapsed = new Promise((resolve) => (lapse = resolve));
552
+ let settle = () => { };
553
+ const lapsed = new Promise((resolve) => (settle = resolve));
554
+ // The wait ends once: at its window, when a session it keeps ends, or at the cap. Each settles `lapsed` at once, and its
555
+ // timeline row and the Screen tab's state are written when the runner confirms the run parked on it (a lapse during
556
+ // the answer's placement is ignored by the runner: the answer's turn goes on, on the same browser). The answer ends it
557
+ // too (drop: no row, the run goes on).
558
+ let over = false;
559
+ let pending;
560
+ const end = (words, row) => {
561
+ if (over)
562
+ return;
563
+ over = true;
564
+ pending = { row, at: now() };
565
+ settle(words);
566
+ };
567
+ const lapse = (words) => end(words, words === WAIT_SESSION_GONE ? BROWSER_GONE_WHILE_WAITING : `The browser was released while the run waited for you: ${words}`);
537
568
  const entry = { tags: new Set(held), lapse };
538
569
  waits.set(user, entry);
539
- bound = timers.set(() => lapse(waitExpired(holdMs)), Math.max(0, expiresAt - now()));
570
+ bound = timers.set(() => end(waitExpired(holdMs), releasedAfterWait(holdMs)), Math.max(0, expiresAt - now()));
540
571
  // A session let go while its hold was being admitted: told before this wait was there to hear it.
541
572
  if (held.some((tag) => ended.has(tag)))
542
573
  lapse(WAIT_SESSION_GONE);
543
574
  return {
544
575
  lapsed,
576
+ confirmed: () => {
577
+ const ended = pending;
578
+ pending = undefined;
579
+ if (ended)
580
+ record(ended.row, ended.at);
581
+ },
545
582
  drop: () => {
583
+ over = true;
584
+ pending = undefined;
546
585
  if (waits.get(user) === entry)
547
586
  waits.delete(user);
548
587
  letGo();
@@ -580,5 +619,15 @@ export function personHands(options) {
580
619
  * tests' (FAUX_PERSON_MS_ENV). */
581
620
  export function personWindows(env, faux) {
582
621
  const [hold, away] = (faux ? env[FAUX_PERSON_MS_ENV] ?? "" : "").split(",").map(Number);
583
- return { holdMs: hold && hold > 0 ? hold : PERSON_HOLD_MS, awayMs: away && away > 0 ? away : PERSON_AWAY_MS };
622
+ return { holdMs: hold && hold > 0 ? hold : personWaitMs(env), awayMs: away && away > 0 ? away : PERSON_AWAY_MS };
623
+ }
624
+ /** The person's window from the server setting PERSON_WAIT_ENV, in ms: whole minutes from 1 to 360, else the default. */
625
+ export function personWaitMs(env) {
626
+ const raw = env[PERSON_WAIT_ENV]?.trim();
627
+ const minutes = raw && /^\d+$/.test(raw) ? Number(raw) : NaN;
628
+ return minutes >= 1 && minutes <= 360 ? minutes * 60_000 : PERSON_HOLD_MS;
584
629
  }
630
+ /** The timeline row when a run's wait for its person ends at its window and its browser is let go. */
631
+ export const releasedAfterWait = (holdMs) => `The browser was released after ${Math.round(holdMs / 60_000) || 1} min waiting for you`;
632
+ /** The timeline row when a browser the run kept open for its person ended before their answer (its provider ended it). */
633
+ export const BROWSER_GONE_WHILE_WAITING = "The browser session ended while the run waited for you";
@@ -0,0 +1,23 @@
1
+ /** The store seam: the server's settings (store/store.ts Settings), the three calls this uses. */
2
+ export type WaitEndedSettings = {
3
+ get(key: string): Promise<unknown | undefined>;
4
+ put(key: string, value: unknown): Promise<void>;
5
+ remove(key: string): Promise<void>;
6
+ };
7
+ /** A browser let go while its run waited for its person: when (ISO) and the timeline row's words. */
8
+ export type WaitEnded = {
9
+ at: string;
10
+ why: string;
11
+ };
12
+ /** The run's process, as its wait ends with the browser let go: kept for the Screen tab. */
13
+ export declare function markWaitEnded(settings: WaitEndedSettings, runId: string, ended: WaitEnded, epoch?: number): Promise<void>;
14
+ /** The run's process, as any browser session of the run opens, or at the run's end: nothing ended any more. Given the
15
+ * claim's `epoch`, a message a later claim wrote stays (a follow-up that started on another runner while this claim's
16
+ * loop was still closing keeps its own). */
17
+ export declare function clearWaitEnded(settings: WaitEndedSettings, runId: string, epoch?: number): Promise<void>;
18
+ /** The server's Screen read: for a run that has ended, nothing, and its row goes (a run that ended with no browser after
19
+ * the wait leaves none behind: the runner removes it at the end too, this catches any end that didn't); for a going
20
+ * run, the ended state. */
21
+ export declare function screenWaitEnded(settings: WaitEndedSettings, runId: string, terminal: boolean): Promise<WaitEnded | undefined>;
22
+ /** The ended state, or undefined. One settings read; a value of another shape is no ended state. */
23
+ export declare function readWaitEnded(settings: Pick<WaitEndedSettings, "get">, runId: string): Promise<WaitEnded | undefined>;
@@ -0,0 +1,42 @@
1
+ // Why a run's browser is not up while the run is still going, for GET /v2/runs/{run_id}/computer's `none`: the browser
2
+ // was let go while the run waited for its person (browser-person.ts personHands.wait ends with a timeline row saying so),
3
+ // and the run has opened none since. The Screen tab says that, with when, instead of waiting for a browser nobody is
4
+ // opening.
5
+ //
6
+ // One small server setting per run holds it (`browser_wait_ended:<run id>`: { at, why }), written by the run's process as
7
+ // the wait ends and removed as any browser session of the run opens, whatever let the old one go (its idle release, the
8
+ // tool, a close at the park). So a Screen poll, every few seconds on a parked run, reads one row, never the run's journal.
9
+ const keyOf = (runId) => `browser_wait_ended:${runId}`;
10
+ /** The run's process, as its wait ends with the browser let go: kept for the Screen tab. */
11
+ export async function markWaitEnded(settings, runId, ended, epoch) {
12
+ await settings.put(keyOf(runId), epoch === undefined ? ended : { ...ended, epoch });
13
+ }
14
+ /** The run's process, as any browser session of the run opens, or at the run's end: nothing ended any more. Given the
15
+ * claim's `epoch`, a message a later claim wrote stays (a follow-up that started on another runner while this claim's
16
+ * loop was still closing keeps its own). */
17
+ export async function clearWaitEnded(settings, runId, epoch) {
18
+ if (epoch !== undefined) {
19
+ const written = (await settings.get(keyOf(runId)))?.epoch;
20
+ if (typeof written === "number" && written > epoch)
21
+ return;
22
+ }
23
+ await settings.remove(keyOf(runId));
24
+ }
25
+ /** The server's Screen read: for a run that has ended, nothing, and its row goes (a run that ended with no browser after
26
+ * the wait leaves none behind: the runner removes it at the end too, this catches any end that didn't); for a going
27
+ * run, the ended state. */
28
+ export async function screenWaitEnded(settings, runId, terminal) {
29
+ if (terminal) {
30
+ await settings.remove(keyOf(runId)).catch(() => undefined);
31
+ return undefined;
32
+ }
33
+ return readWaitEnded(settings, runId);
34
+ }
35
+ /** The ended state, or undefined. One settings read; a value of another shape is no ended state. */
36
+ export async function readWaitEnded(settings, runId) {
37
+ const value = await settings.get(keyOf(runId));
38
+ if (!value || typeof value !== "object")
39
+ return undefined;
40
+ const { at, why } = value;
41
+ return typeof at === "string" && typeof why === "string" ? { at, why } : undefined;
42
+ }