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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (381) hide show
  1. package/CHANGELOG.md +233 -0
  2. package/README.md +266 -201
  3. package/dist/adapters.d.ts +0 -4
  4. package/dist/adapters.js +11 -15
  5. package/dist/agent-rows.d.ts +98 -0
  6. package/dist/agent-rows.js +1 -0
  7. package/dist/agents-store.d.ts +6 -2
  8. package/dist/agents-store.js +60 -25
  9. package/dist/agents.d.ts +9 -125
  10. package/dist/agents.js +15 -305
  11. package/dist/answer-json.d.ts +23 -0
  12. package/dist/answer-json.js +58 -0
  13. package/dist/api-only.d.ts +1 -1
  14. package/dist/api-only.js +2 -2
  15. package/dist/bash-read-only.d.ts +22 -0
  16. package/dist/bash-read-only.js +701 -0
  17. package/dist/box-settings.d.ts +3 -23
  18. package/dist/box-settings.js +7 -51
  19. package/dist/boxes.d.ts +17 -0
  20. package/dist/boxes.js +27 -20
  21. package/dist/browser-commands.d.ts +5 -2
  22. package/dist/browser-commands.js +4 -4
  23. package/dist/browser-computer.d.ts +5 -0
  24. package/dist/browser-computer.js +21 -0
  25. package/dist/browser-cost.d.ts +9 -6
  26. package/dist/browser-cost.js +92 -50
  27. package/dist/browser-live.d.ts +7 -20
  28. package/dist/browser-live.js +15 -93
  29. package/dist/browser-person.d.ts +18 -51
  30. package/dist/browser-person.js +94 -277
  31. package/dist/cap.d.ts +8 -5
  32. package/dist/cap.js +12 -5
  33. package/dist/cli.js +39 -13
  34. package/dist/closed-pipes.d.ts +2 -0
  35. package/dist/closed-pipes.js +13 -0
  36. package/dist/computer-commands.d.ts +2 -0
  37. package/dist/computer-commands.js +2 -0
  38. package/dist/computer-keys.js +5 -2
  39. package/dist/computer-setting.d.ts +20 -4
  40. package/dist/computer-setting.js +25 -5
  41. package/dist/computers.d.ts +30 -2
  42. package/dist/computers.js +70 -11
  43. package/dist/confine.d.ts +120 -0
  44. package/dist/confine.js +436 -0
  45. package/dist/console/assets/index-RPHGlXmM.js +540 -0
  46. package/dist/console/assets/index-opTN5Naq.css +1 -0
  47. package/dist/console/index.html +2 -2
  48. package/dist/decision-rows.d.ts +1 -1
  49. package/dist/decision-rows.js +1 -1
  50. package/dist/docker-daemon.d.ts +31 -0
  51. package/dist/docker-daemon.js +56 -0
  52. package/dist/effort-guidance.js +4 -3
  53. package/dist/environment.d.ts +0 -3
  54. package/dist/environment.js +3 -5
  55. package/dist/errors.d.ts +0 -2
  56. package/dist/errors.js +6 -9
  57. package/dist/filed-answer.d.ts +32 -0
  58. package/dist/filed-answer.js +160 -0
  59. package/dist/files.d.ts +1 -3
  60. package/dist/files.js +2 -4
  61. package/dist/github-connection.d.ts +1 -1
  62. package/dist/graph-view.d.ts +3 -5
  63. package/dist/graph-view.js +4 -14
  64. package/dist/handed-values.d.ts +9 -0
  65. package/dist/handed-values.js +26 -0
  66. package/dist/harness-install.d.ts +1 -7
  67. package/dist/harness-install.js +1 -18
  68. package/dist/harness-opencode/adapter.d.ts +7 -0
  69. package/dist/harness-opencode/adapter.js +23 -6
  70. package/dist/harness-opencode/index.d.ts +1 -1
  71. package/dist/harness-opencode/index.js +1 -1
  72. package/dist/harness-opencode/verify.d.ts +17 -0
  73. package/dist/harness-opencode/verify.js +30 -10
  74. package/dist/harness-table.js +6 -1
  75. package/dist/heard.js +1 -1
  76. package/dist/held-fd.d.ts +5 -0
  77. package/dist/held-fd.js +14 -0
  78. package/dist/host-calls.d.ts +20 -0
  79. package/dist/host-calls.js +12 -0
  80. package/dist/host-tools.d.ts +4 -21
  81. package/dist/host-tools.js +4 -61
  82. package/dist/host.d.ts +2 -4
  83. package/dist/host.js +2 -7
  84. package/dist/import-summary.d.ts +1 -5
  85. package/dist/import-summary.js +0 -21
  86. package/dist/import.d.ts +6 -6
  87. package/dist/import.js +14 -15
  88. package/dist/key-refusal.d.ts +5 -1
  89. package/dist/key-refusal.js +19 -4
  90. package/dist/learn/check.d.ts +6 -2
  91. package/dist/learn/check.js +83 -29
  92. package/dist/learn/folder.d.ts +5 -1
  93. package/dist/learn/folder.js +10 -3
  94. package/dist/learn/grade.d.ts +14 -0
  95. package/dist/learn/grade.js +68 -12
  96. package/dist/learn/index.d.ts +4 -4
  97. package/dist/learn/index.js +4 -4
  98. package/dist/learn/reflect.d.ts +31 -2
  99. package/dist/learn/reflect.js +79 -4
  100. package/dist/learn/relevance.d.ts +24 -0
  101. package/dist/learn/relevance.js +91 -0
  102. package/dist/learn/secrets.d.ts +7 -1
  103. package/dist/learn/secrets.js +86 -19
  104. package/dist/learn/skills.d.ts +112 -14
  105. package/dist/learn/skills.js +556 -52
  106. package/dist/learned-workflow.d.ts +32 -1
  107. package/dist/learned-workflow.js +100 -9
  108. package/dist/learning-routes.d.ts +8 -4
  109. package/dist/learning-routes.js +23 -9
  110. package/dist/learning-run.d.ts +9 -26
  111. package/dist/learning-run.js +22 -32
  112. package/dist/learning-words.d.ts +12 -5
  113. package/dist/learning-words.js +18 -9
  114. package/dist/learning.d.ts +93 -77
  115. package/dist/learning.js +176 -304
  116. package/dist/live/stream.js +1 -1
  117. package/dist/live/translate.js +12 -2
  118. package/dist/local-models.d.ts +11 -5
  119. package/dist/local-models.js +13 -8
  120. package/dist/mcp-gateway.d.ts +11 -49
  121. package/dist/mcp-gateway.js +44 -135
  122. package/dist/mcp-oauth.d.ts +10 -14
  123. package/dist/mcp-oauth.js +14 -32
  124. package/dist/mcp-probe.d.ts +0 -7
  125. package/dist/mcp-probe.js +4 -10
  126. package/dist/mcp-servers-store.d.ts +6 -6
  127. package/dist/mcp-servers-store.js +12 -14
  128. package/dist/mcp-store.js +3 -4
  129. package/dist/mcp.js +4 -3
  130. package/dist/model.d.ts +6 -7
  131. package/dist/model.js +43 -27
  132. package/dist/monid.d.ts +0 -2
  133. package/dist/monid.js +3 -6
  134. package/dist/openapi.d.ts +8 -1
  135. package/dist/openapi.js +121 -47
  136. package/dist/page.d.ts +1 -3
  137. package/dist/page.js +6 -18
  138. package/dist/paid-calls.d.ts +21 -27
  139. package/dist/paid-calls.js +25 -60
  140. package/dist/propose.js +83 -39
  141. package/dist/push-handler.d.ts +1 -1
  142. package/dist/reflect-range.d.ts +25 -0
  143. package/dist/reflect-range.js +30 -0
  144. package/dist/refusal-words.d.ts +5 -1
  145. package/dist/refusal-words.js +5 -1
  146. package/dist/rows.d.ts +1 -1
  147. package/dist/run-cut.d.ts +0 -6
  148. package/dist/run-cut.js +0 -6
  149. package/dist/run-db.d.ts +2 -11
  150. package/dist/run-db.js +2 -37
  151. package/dist/run-launch.d.ts +19 -2
  152. package/dist/run-launch.js +13 -5
  153. package/dist/run-learning-served.d.ts +10 -4
  154. package/dist/run-learning-served.js +23 -13
  155. package/dist/run-reads-over-store.d.ts +1 -1
  156. package/dist/run-reads-over-store.js +15 -6
  157. package/dist/run-secrets.d.ts +1 -1
  158. package/dist/run-timeline-served.d.ts +5 -1
  159. package/dist/run-timeline-served.js +15 -4
  160. package/dist/run-view.d.ts +7 -8
  161. package/dist/run-view.js +26 -7
  162. package/dist/runnable.d.ts +18 -10
  163. package/dist/runnable.js +21 -10
  164. package/dist/runner/adapter-loop.d.ts +6 -1
  165. package/dist/runner/adapter-loop.js +423 -53
  166. package/dist/runner/adapter-run-spec.d.ts +21 -8
  167. package/dist/runner/adapter-run-spec.js +90 -32
  168. package/dist/runner/adapters/index.js +11 -1
  169. package/dist/runner/adapters/pi.d.ts +12 -2
  170. package/dist/runner/adapters/pi.js +16 -5
  171. package/dist/runner/beside.d.ts +30 -3
  172. package/dist/runner/beside.js +36 -5
  173. package/dist/runner/claim-secrets.d.ts +10 -1
  174. package/dist/runner/claim-secrets.js +48 -14
  175. package/dist/runner/cli-run-spec.d.ts +2 -0
  176. package/dist/runner/cli-run-spec.js +45 -16
  177. package/dist/runner/commands.js +1 -1
  178. package/dist/runner/computer-journal.d.ts +15 -2
  179. package/dist/runner/computer-journal.js +41 -5
  180. package/dist/runner/conformance/harness.js +118 -8
  181. package/dist/runner/conformance/subject.d.ts +4 -1
  182. package/dist/runner/dispatch.d.ts +8 -1
  183. package/dist/runner/dispatch.js +81 -7
  184. package/dist/runner/end-of-run.d.ts +8 -2
  185. package/dist/runner/end-of-run.js +10 -4
  186. package/dist/runner/github-app.js +1 -1
  187. package/dist/runner/harness-loops.d.ts +9 -1
  188. package/dist/runner/harness-loops.js +15 -2
  189. package/dist/runner/journal-kinds.d.ts +24 -6
  190. package/dist/runner/journal-kinds.js +20 -5
  191. package/dist/runner/journal-reader.d.ts +1 -1
  192. package/dist/runner/journal-reader.js +2 -2
  193. package/dist/runner/learning-ask.d.ts +4 -0
  194. package/dist/runner/learning-ask.js +15 -1
  195. package/dist/runner/learning-end.d.ts +18 -3
  196. package/dist/runner/learning-end.js +113 -11
  197. package/dist/runner/learning-loop.d.ts +5 -0
  198. package/dist/runner/learning-loop.js +26 -6
  199. package/dist/runner/main.d.ts +56 -9
  200. package/dist/runner/main.js +127 -18
  201. package/dist/runner/mcp-claim.js +1 -2
  202. package/dist/runner/mcp-run.d.ts +2 -0
  203. package/dist/runner/mcp-run.js +15 -5
  204. package/dist/runner/memory-claim.d.ts +9 -0
  205. package/dist/runner/memory-claim.js +25 -0
  206. package/dist/runner/metrics.d.ts +33 -1
  207. package/dist/runner/metrics.js +85 -1
  208. package/dist/runner/model-route.d.ts +13 -0
  209. package/dist/runner/model-route.js +133 -9
  210. package/dist/runner/reflect-hook.d.ts +12 -1
  211. package/dist/runner/reflect-hook.js +33 -3
  212. package/dist/runner/runner.d.ts +20 -0
  213. package/dist/runner/runner.js +56 -10
  214. package/dist/runner/store-port.d.ts +1 -1
  215. package/dist/runner/store-secrets.d.ts +3 -3
  216. package/dist/runner/store-secrets.js +8 -12
  217. package/dist/runner/text-tools.d.ts +44 -0
  218. package/dist/runner/text-tools.js +237 -0
  219. package/dist/runner/workflow-loop.d.ts +6 -0
  220. package/dist/runner/workflow-loop.js +6 -4
  221. package/dist/runner/workflow-spec.d.ts +18 -0
  222. package/dist/runner/workflow-spec.js +63 -17
  223. package/dist/runs-over-store.d.ts +7 -2
  224. package/dist/runs-over-store.js +60 -14
  225. package/dist/seal-key.js +5 -43
  226. package/dist/sealed-settings-store.d.ts +19 -4
  227. package/dist/sealed-settings-store.js +9 -9
  228. package/dist/sealing.d.ts +4 -0
  229. package/dist/sealing.js +20 -0
  230. package/dist/secret-cut.d.ts +1 -1
  231. package/dist/secret-cut.js +1 -1
  232. package/dist/secret-name.d.ts +9 -0
  233. package/dist/secret-name.js +15 -0
  234. package/dist/secret-writes.d.ts +0 -1
  235. package/dist/secret-writes.js +0 -1
  236. package/dist/server-fs.d.ts +4 -0
  237. package/dist/server-fs.js +8 -2
  238. package/dist/server-lock.d.ts +0 -24
  239. package/dist/server-lock.js +1 -80
  240. package/dist/server-secrets-store.d.ts +4 -4
  241. package/dist/server-secrets-store.js +9 -31
  242. package/dist/server-secrets.d.ts +4 -9
  243. package/dist/server-secrets.js +12 -36
  244. package/dist/server.d.ts +16 -38
  245. package/dist/server.js +145 -106
  246. package/dist/sign-ins-store.d.ts +34 -36
  247. package/dist/sign-ins-store.js +27 -38
  248. package/dist/sign-ins.d.ts +2 -119
  249. package/dist/sign-ins.js +5 -311
  250. package/dist/standard-webhooks.d.ts +1 -1
  251. package/dist/standard-webhooks.js +1 -1
  252. package/dist/stats.d.ts +3 -2
  253. package/dist/stats.js +1 -1
  254. package/dist/store/agent-body.d.ts +8 -2
  255. package/dist/store/agent-body.js +16 -4
  256. package/dist/store/index.d.ts +2 -2
  257. package/dist/store/index.js +2 -2
  258. package/dist/store/open.d.ts +6 -3
  259. package/dist/store/open.js +2 -2
  260. package/dist/store/postgres/connection.d.ts +18 -2
  261. package/dist/store/postgres/connection.js +156 -32
  262. package/dist/store/postgres/migrate.d.ts +4 -0
  263. package/dist/store/postgres/migrate.js +9 -2
  264. package/dist/store/postgres/migrations/0003_commands_one_answer.check.sql +19 -0
  265. package/dist/store/postgres/migrations/0003_commands_one_answer.sql +13 -0
  266. package/dist/store/postgres/store.d.ts +9 -2
  267. package/dist/store/postgres/store.js +9 -0
  268. package/dist/store/schema.js +1 -1
  269. package/dist/store/seal.js +2 -2
  270. package/dist/store/sqlite/connection.js +49 -5
  271. package/dist/store/sqlite/run-patch.d.ts +3 -0
  272. package/dist/store/sqlite/run-patch.js +3 -0
  273. package/dist/store/sqlite/store.js +41 -12
  274. package/dist/store/store.d.ts +35 -6
  275. package/dist/store/store.js +12 -2
  276. package/dist/store-reads/cap.d.ts +1 -1
  277. package/dist/store-reads/cap.js +22 -4
  278. package/dist/store-reads/journal.d.ts +9 -6
  279. package/dist/store-reads/journal.js +93 -17
  280. package/dist/store-reads/learning.d.ts +26 -8
  281. package/dist/store-reads/learning.js +12 -4
  282. package/dist/store-reads/workflow.d.ts +1 -1
  283. package/dist/store-reads/workflow.js +1 -1
  284. package/dist/store-view.d.ts +5 -1
  285. package/dist/store-view.js +53 -18
  286. package/dist/template-eval.d.ts +60 -4
  287. package/dist/template-eval.js +120 -54
  288. package/dist/templates.d.ts +24 -10
  289. package/dist/templates.js +69 -38
  290. package/dist/timeline/group.d.ts +33 -0
  291. package/dist/timeline/group.js +77 -0
  292. package/dist/timeline/index.d.ts +7 -0
  293. package/dist/timeline/index.js +7 -0
  294. package/dist/timeline/labels.d.ts +79 -0
  295. package/dist/timeline/labels.js +150 -0
  296. package/dist/timeline/origin.d.ts +56 -0
  297. package/dist/timeline/origin.js +92 -0
  298. package/dist/timeline/render-text.d.ts +11 -0
  299. package/dist/timeline/render-text.js +62 -0
  300. package/dist/timeline/timeline.d.ts +274 -0
  301. package/dist/timeline/timeline.js +1308 -0
  302. package/dist/timeline/util.d.ts +17 -0
  303. package/dist/timeline/util.js +49 -0
  304. package/dist/tool-gate.d.ts +41 -216
  305. package/dist/tool-gate.js +79 -540
  306. package/dist/tool-keys-store.d.ts +11 -12
  307. package/dist/tool-keys-store.js +24 -31
  308. package/dist/tool-keys.d.ts +5 -101
  309. package/dist/tool-keys.js +8 -267
  310. package/dist/tool-plane.d.ts +14 -42
  311. package/dist/tool-plane.js +23 -81
  312. package/dist/tool-settings-store.d.ts +1 -2
  313. package/dist/tool-settings-store.js +5 -8
  314. package/dist/tools.d.ts +30 -18
  315. package/dist/tools.js +97 -107
  316. package/dist/trigger-adapter.d.ts +2 -2
  317. package/dist/trigger-targets.d.ts +13 -1
  318. package/dist/trigger-targets.js +31 -1
  319. package/dist/triggers.d.ts +2 -2
  320. package/dist/triggers.js +3 -3
  321. package/dist/turn-end.d.ts +1 -1
  322. package/dist/turn-end.js +1 -1
  323. package/dist/turns.d.ts +0 -6
  324. package/dist/turns.js +0 -19
  325. package/dist/uploads.js +1 -1
  326. package/dist/usage.d.ts +0 -2
  327. package/dist/usage.js +0 -2
  328. package/dist/validate.d.ts +2 -0
  329. package/dist/validate.js +23 -14
  330. package/dist/webhooks.d.ts +102 -76
  331. package/dist/webhooks.js +266 -233
  332. package/dist/workflow-run.d.ts +114 -0
  333. package/dist/workflow-run.js +144 -0
  334. package/dist/workspace/beneath.d.ts +19 -0
  335. package/dist/workspace/beneath.js +106 -0
  336. package/dist/workspace/computer-deps.d.ts +24 -6
  337. package/dist/workspace/computer-deps.js +108 -20
  338. package/dist/workspace/computer-for-claim.d.ts +12 -11
  339. package/dist/workspace/computer-for-claim.js +76 -15
  340. package/dist/workspace/computer-hold.d.ts +6 -1
  341. package/dist/workspace/computer-hold.js +6 -1
  342. package/dist/workspace/computer-janitor.d.ts +47 -0
  343. package/dist/workspace/computer-janitor.js +182 -0
  344. package/dist/workspace/grep-worker.d.ts +5 -0
  345. package/dist/workspace/grep-worker.js +9 -6
  346. package/dist/workspace/index.d.ts +3 -2
  347. package/dist/workspace/index.js +3 -3
  348. package/dist/workspace/local.d.ts +11 -2
  349. package/dist/workspace/local.js +161 -43
  350. package/dist/workspace/resolve-computer.d.ts +50 -10
  351. package/dist/workspace/resolve-computer.js +46 -11
  352. package/dist/workspace/run-tools.d.ts +13 -31
  353. package/dist/workspace/run-tools.js +10 -27
  354. package/dist/workspace-paths.d.ts +0 -17
  355. package/dist/workspace-paths.js +1 -41
  356. package/dist/workspace.d.ts +0 -14
  357. package/dist/workspace.js +3 -96
  358. package/package.json +13 -13
  359. package/templates/README.md +9 -3
  360. package/templates/market-monitor/agent.md +2 -2
  361. package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
  362. package/dist/bound-files.d.ts +0 -4
  363. package/dist/bound-files.js +0 -164
  364. package/dist/coding-tools.d.ts +0 -47
  365. package/dist/coding-tools.js +0 -189
  366. package/dist/computer-adb.d.ts +0 -12
  367. package/dist/computer-adb.js +0 -14
  368. package/dist/console/assets/index-Dn0ngwG9.js +0 -535
  369. package/dist/console/assets/index-DvW6Aled.css +0 -1
  370. package/dist/grade.d.ts +0 -63
  371. package/dist/grade.js +0 -150
  372. package/dist/mcp-servers.d.ts +0 -269
  373. package/dist/mcp-servers.js +0 -456
  374. package/dist/packs.d.ts +0 -71
  375. package/dist/packs.js +0 -180
  376. package/dist/sealed-settings.d.ts +0 -60
  377. package/dist/sealed-settings.js +0 -171
  378. package/dist/sessions.d.ts +0 -6
  379. package/dist/sessions.js +0 -109
  380. package/dist/workspace/computer-serves.d.ts +0 -12
  381. package/dist/workspace/computer-serves.js +0 -15
@@ -16,23 +16,76 @@
16
16
  // with names no key, no account key and no database or Redis credential, and carries no environment at all.
17
17
  import { isIP } from "node:net";
18
18
  import { posix } from "node:path";
19
- import { DEFAULT_DOCKER_IMAGE, DOCKER_RECIPE, DOCKER_RECIPE_HAS, PROVIDERS, providerFor, readBackOf, servingRefusals, SETTING_KINDS } from "@parcha/agentrun-computer";
19
+ import { DEFAULT_DOCKER_IMAGE, DOCKER_RECIPE, DOCKER_RECIPE_HAS, DockerProvider, PROVIDERS, providerFor, readBackOf, servingRefusals, SETTING_KINDS } from "@parcha/agentrun-computer";
20
20
  import { DOCKER_IMAGE_ENV } from "../box-image.js";
21
+ import { dockerDaemonCheck } from "../docker-daemon.js";
21
22
  import { unionOf } from "../docker-host-addresses.js";
22
23
  import { envValue, keyReader } from "../key-meta.js";
24
+ import { boxAllowOf, boxAllowSetIn } from "../box-settings.js";
23
25
  import { readLaunchBody } from "../run-launch.js";
24
26
  import { computerForClaim } from "./computer-for-claim.js";
25
27
  import { storeLedger } from "./computer-ledger.js";
26
28
  import { computerProviderFactories, computerProviderMakers } from "./computer-providers.js";
29
+ import { needsNetwork, noNetworkRefusal, vendorNetworkOnly } from "./resolve-computer.js";
27
30
  /** How long a run's computer lives whatever happens to the server: a bound on a leaked one, never its clean end (the old
28
31
  * box's lifetime). The clean end is the run's release. */
29
32
  export const COMPUTER_LIFETIME_MS = 24 * 60 * 60_000;
30
33
  /** A computer this server cannot make here, with the code the run fails with and its words. */
31
34
  const refused = (words) => Object.assign(new Error(words), { code: "computer_unavailable" });
35
+ /** The settings that size a Docker computer (a sandbox or a desktop): its CPUs, its memory (swap included) and how many
36
+ * processes and threads it may hold at once. Unset, the Docker provider's own defaults hold. A vendor's computer sizes
37
+ * itself and is never given them. */
38
+ export const BOX_CPUS_ENV = "AGENTRUN_BOX_CPUS";
39
+ export const BOX_MEMORY_ENV = "AGENTRUN_BOX_MEMORY";
40
+ export const BOX_PIDS_ENV = "AGENTRUN_BOX_PIDS";
41
+ /** Each setting's spec field and what it takes, in an operator's words. */
42
+ const LIMIT_SETTINGS = [
43
+ { name: BOX_CPUS_ENV, field: "cpus", form: "a number of CPUs, such as 2 or 1.5" },
44
+ { name: BOX_MEMORY_ENV, field: "memoryMb", form: "a size with its unit (b, k, m or g), such as 512m or 4g" },
45
+ { name: BOX_PIDS_ENV, field: "pids", form: "a whole number of processes and threads, such as 1024" },
46
+ ];
47
+ /** A size's units as Docker reads them, each 1024 of the one before, in MiB. */
48
+ const SIZE_MIB = { b: 1 / (1024 * 1024), k: 1 / 1024, m: 1, g: 1024 };
49
+ /** One setting's value as its spec field: a number where it reads as one, else NaN, which the provider's check refuses. */
50
+ const LIMIT_VALUE = {
51
+ cpus: (value) => (/^\d+(\.\d+)?$/.test(value) ? Number(value) : Number.NaN),
52
+ // Its MiB, the fraction dropped, as Docker reads a size: 0.5g is 512, 6.5m is 6.
53
+ memoryMb: (value) => {
54
+ const size = /^(\d+(?:\.\d+)?)([bkmg])$/i.exec(value);
55
+ return size ? Math.floor(Number(size[1]) * SIZE_MIB[size[2].toLowerCase()]) : Number.NaN;
56
+ },
57
+ pids: (value) => (/^\d+$/.test(value) ? Number(value) : Number.NaN),
58
+ };
59
+ /** Asked only check(): its constructor runs nothing and reads no Docker, and check() reads only the spec. */
60
+ let checker;
61
+ /** A Docker computer's size from `env`'s AGENTRUN_BOX_CPUS, _MEMORY and _PIDS: each one set, as its spec field; none set,
62
+ * none, so the provider's defaults hold. A value the Docker provider would refuse at every create throws, in its own
63
+ * words, naming the setting and what it takes: the server's start reads them (startServe), so no run meets one. */
64
+ export function dockerLimitsOf(env) {
65
+ const limits = {};
66
+ for (const { name, field } of LIMIT_SETTINGS) {
67
+ const value = envValue(env, name)?.trim();
68
+ if (value !== undefined)
69
+ limits[field] = LIMIT_VALUE[field](value);
70
+ }
71
+ if (Object.keys(limits).length === 0)
72
+ return {};
73
+ checker ??= new DockerProvider({ daemonPath: "computerd" });
74
+ const refusals = checker.check({ image: DEFAULT_DOCKER_IMAGE, lifetimeMs: COMPUTER_LIFETIME_MS, network: "none", ...limits });
75
+ for (const { name, field, form } of LIMIT_SETTINGS) {
76
+ const refusal = refusals.find((one) => one.field === field);
77
+ if (refusal)
78
+ throw new Error(`${name} ${JSON.stringify(envValue(env, name).trim())} is refused by the Docker provider: ${refusal.message} (${name} is ${form})`);
79
+ }
80
+ return limits;
81
+ }
32
82
  /** The Docker host's own addresses, for a Docker provider whose interfaces aren't that host's (the server in a
33
83
  * container): a comma list of IP addresses. The Docker provider's egress proxy refuses them as the host's, so a
34
84
  * computer's allow-list never reaches the host itself; without them it refuses any allow-list there. */
35
85
  export const DOCKER_HOST_ADDRESSES_ENV = "AGENTRUN_DOCKER_HOST_ADDRESSES";
86
+ /** The variables the local browser's provider finds its Chrome by (the computer package's findChrome): the server's own
87
+ * values are handed to it, and no other variable of the server's. */
88
+ export const CHROME_LOOKUP = ["CHROME_PATH", "PI_BROWSER_CHROME", "PATH"];
36
89
  /** `env`'s AGENTRUN_DOCKER_HOST_ADDRESSES: its addresses, each once; undefined when unset or empty, never one made up.
37
90
  * An entry that is not an IP address throws, naming it: the server's start checks the setting, so no run meets one. */
38
91
  export function dockerHostAddressesOf(env) {
@@ -57,12 +110,15 @@ export function dockerVolumeOf(paths) {
57
110
  /** The spec a computer of `kind` on `entry` is made with, besides its labels and its mount: its image (Docker's: the
58
111
  * owner's AGENTRUN_DOCKER_IMAGE when set, else for a sandbox the package's recipe; any other provider's: its registry
59
112
  * entry's defaultImage for the kind, the computer package's PROVIDERS[].defaultImage; a kind with none refused in
60
- * words, a Docker desktop's saying it needs an image with an X server, which the recipe lacks), the leak bound for its lifetime (its clean end is the run's release), and its network. box_allow is a
61
- * sandbox's egress list: a sandbox with none, or "none", has no network; a browser, desktop or phone with none has its
62
- * provider's own ("open"); an explicit list or "none" applies to any kind, narrowed to "none" for a provider that makes
63
- * no other (networkOf). No key and no environment. */
113
+ * words, a Docker desktop's saying it needs an image with an X server, which the recipe lacks), the leak bound for its lifetime (its clean end is the run's release), and its network
114
+ * (networkOf): the run's box_allow, or this server's own (AGENTRUN_BOX_ALLOW, read from `env`) for a launch that names
115
+ * none; and a Docker computer's size, AGENTRUN_BOX_CPUS, _MEMORY and _PIDS (dockerLimitsOf, read once here), on
116
+ * Docker's alone. No key and no environment. */
64
117
  export function computerSpecOf(env) {
65
118
  const named = envValue(env, DOCKER_IMAGE_ENV);
119
+ // This server's allow setting, as a launch is stamped with it (box-settings.ts boxAllowOf; its start checks the setting).
120
+ const current = () => boxAllowOf(env);
121
+ const currentSet = () => boxAllowSetIn(env);
66
122
  const imageOf = (entry, kind) => {
67
123
  if (entry.id === "docker" && named !== undefined)
68
124
  return named;
@@ -76,24 +132,40 @@ export function computerSpecOf(env) {
76
132
  throw refused("A Docker desktop needs an image with an X server; set one for desktops.");
77
133
  throw refused(`${entry.label} has no image for a ${kind} computer here: pick another provider for this agent's computer in Settings → Computers.`);
78
134
  };
135
+ const limits = dockerLimitsOf(env);
79
136
  return (entry, kind, run) => ({
80
137
  image: imageOf(entry, kind),
81
138
  // The leak bound, within the longest the provider makes (E2B's is an hour): a longer one it refuses at create.
82
139
  lifetimeMs: Math.min(COMPUTER_LIFETIME_MS, run.maxLifetimeMs ?? COMPUTER_LIFETIME_MS),
83
- network: networkOf(kind, run),
140
+ network: networkOf(entry, kind, run, current, currentSet),
141
+ // A Docker computer's size, the operator's (dockerLimitsOf); a vendor's sizes itself.
142
+ ...(entry.id === "docker" ? limits : {}),
84
143
  });
85
144
  }
86
- /** The network a computer of `kind` asks for: as the run allows (box_allow), narrowed to "none" when its provider makes
87
- * "none" but not what the run allows. A remote computer (Daytona's or E2B's, each one whose capabilities' network is
88
- * ["none"]) runs its programs in a network of their own, so an open or listed network is one it refuses at create;
89
- * "none" reaches less than the run allows, never more. The one exception is a vendor-hosted computer that runs only on
90
- * the vendor's own network (its capabilities' network is ["open"]: a cloud phone, a hosted browser): it is "open"
91
- * whatever the run allows, since this server's allow-list can't reach it, and it is never refused for a list. */
92
- function networkOf(kind, run) {
93
- if (run.networks && run.networks.length > 0 && run.networks.every((one) => one === "open"))
145
+ /** The network a computer of `kind` on `entry` asks for: what the run allows (its box_allow), narrowed to "none" when its
146
+ * provider makes "none" but not what the run allows. A launch that names no box_allow (a row from before every launch
147
+ * was stamped with it) allows what this server allows now (`current`), never "open"; a sandbox with none still has no
148
+ * network, as it always had. A remote computer (Daytona's or E2B's, each one whose capabilities' network is ["none"])
149
+ * runs its programs in a network of their own, so an open or listed network is one it refuses at create; "none" reaches
150
+ * less than the run allows, never more. Under "none", the operator's word for no network at all, a computer whose
151
+ * provider makes no "none" (a cloud phone, a hosted browser, Local Chrome) is refused in words (noNetworkRefusal), as
152
+ * the run's start refuses it (resolve-computer.ts computerReadiness). A vendor-hosted computer that runs only on the
153
+ * vendor's own network (its capabilities' network is ["open"]) is "open" under a list, the default or the operator's,
154
+ * since this server's list can't reach it, and Settings says so (computers.ts networkWords). A browser whose provider
155
+ * makes both "open" and "allow" (Local Chrome, behind its own egress proxy) is held to the operator's list
156
+ * (`boxAllowSet`, the launch's, or this server's now for a launch that names no box_allow) and is "open" under the
157
+ * default list, which is a sandbox's package-install list and no browsing policy. */
158
+ function networkOf(entry, kind, run, current, currentSet) {
159
+ const boxAllow = run.boxAllow ?? (kind === "sandbox" ? "none" : current());
160
+ const set = run.boxAllow !== undefined ? run.boxAllowSet === true : currentSet();
161
+ if (boxAllow === "none" && needsNetwork(run.networks))
162
+ throw refused(noNetworkRefusal(entry.label, kind));
163
+ if (vendorNetworkOnly(run.networks))
94
164
  return "open";
95
- const allowed = run.boxAllow === undefined ? (kind === "sandbox" ? "none" : "open") : run.boxAllow === "none" ? "none" : { allow: [...run.boxAllow] };
96
- const form = allowed === "none" || allowed === "open" ? allowed : "allow";
165
+ if (kind === "browser" && boxAllow !== "none" && run.networks?.includes("open") && run.networks.includes("allow"))
166
+ return set ? { allow: [...boxAllow] } : "open";
167
+ const allowed = boxAllow === "none" ? "none" : { allow: [...boxAllow] };
168
+ const form = allowed === "none" ? "none" : "allow";
97
169
  return run.networks && !run.networks.includes(form) && run.networks.includes("none") ? "none" : allowed;
98
170
  }
99
171
  /** The run's timeline phase while its claim waits on its computer's image (computerForClaim's onImageWait): main's box
@@ -104,8 +176,9 @@ export const imageWaitWords = (making, image) => making === "build" ? `Preparing
104
176
  export const claimComputer = async (launch, store, deps) => {
105
177
  if (launch.boxAllow !== undefined || !store.runs)
106
178
  return computerForClaim(launch, store, deps);
107
- const boxAllow = readLaunchBody((await store.runs.launch(launch.runId)).body)?.boxAllow;
108
- return computerForClaim(boxAllow === undefined ? launch : { ...launch, boxAllow }, store, deps);
179
+ const body = readLaunchBody((await store.runs.launch(launch.runId)).body);
180
+ const boxAllow = body?.boxAllow;
181
+ return computerForClaim(boxAllow === undefined ? launch : { ...launch, boxAllow, ...(body?.boxAllowSet ? { boxAllowSet: true } : {}) }, store, deps);
109
182
  };
110
183
  /** The kind registry a claim composed with `options` resolves a setting against: --faux's when it is given faux, else a
111
184
  * test's own, else the computer package's. The one choice: computerDepsFrom makes it, and the server's start check
@@ -114,6 +187,12 @@ export function claimKinds(options) {
114
187
  // The faux registry is Settings' shape (computers.ts); the claim reads the same data as resolve-computer.ts's.
115
188
  return options.faux?.kinds ?? options.kinds ?? { SETTING_KINDS, PROVIDERS, servingRefusals };
116
189
  }
190
+ /** This machine's Docker daemon as the claim asks it once a Docker launch fails: one check per process (its answer
191
+ * kept a while, docker-daemon.ts), in `env`. */
192
+ const dockerDaemonOf = (env) => {
193
+ const check = dockerDaemonCheck({ env: { ...env } });
194
+ return () => check();
195
+ };
117
196
  /** computerForClaim and its process-wide deps, composed once; and Settings → Computers' factories from the same makers. */
118
197
  export function computerDepsFrom(options) {
119
198
  const { env, store, sealKey, faux } = options;
@@ -138,12 +217,20 @@ export function computerDepsFrom(options) {
138
217
  const dockerOptions = async (boxAllow) => {
139
218
  const where = options.dockerPaths ? await (paths ??= options.dockerPaths()) : { here: true };
140
219
  const volume = dockerVolumeOf(where);
141
- const read = volume && Array.isArray(boxAllow) && options.hostAddresses ? await options.hostAddresses() : undefined;
220
+ // A launch that names no box_allow (a row from before every launch was stamped) allows what this server allows now,
221
+ // as its computer's network does (networkOf), so the provider's options follow the same setting.
222
+ const allow = boxAllow ?? boxAllowOf(env);
223
+ const read = volume && Array.isArray(allow) && options.hostAddresses ? await options.hostAddresses() : undefined;
142
224
  const addresses = read ? unionOf(read, namedAddresses).addresses : (namedAddresses ?? []);
143
225
  return { image: dockerImage, ...(volume ?? {}), ...(addresses.length > 0 ? { egress: { hostAddresses: addresses } } : {}) };
144
226
  };
145
227
  const load = options.load ?? providerFor;
146
- const providers = computerProviderMakers({ load: async (id, given, claim) => load(id, id === "docker" ? { ...given, ...(await dockerOptions(claim?.boxAllow)) } : given) });
228
+ // The local browser finds its Chrome where the server's environment names one (CHROME_PATH, PI_BROWSER_CHROME, else
229
+ // PATH): those three alone, for the claim and Settings → Computers' test alike.
230
+ const chromeEnv = { env: Object.fromEntries(CHROME_LOOKUP.flatMap((name) => (env[name] === undefined ? [] : [[name, env[name]]]))) };
231
+ const providers = computerProviderMakers({
232
+ load: async (id, given, claim) => load(id, id === "docker" ? { ...given, ...(await dockerOptions(claim?.boxAllow)) } : id === "local" ? { ...given, ...chromeEnv } : given),
233
+ });
147
234
  const computerDeps = {
148
235
  kinds,
149
236
  providers,
@@ -153,6 +240,7 @@ export function computerDepsFrom(options) {
153
240
  // Under --faux, its launch makes the computer, with its key table: no provider of the package is built.
154
241
  ...(faux ? { keyTable: faux.keyTable, launch: (entry, spec) => faux.launch(entry, spec) } : {}),
155
242
  ...(options.answers ? { answers: options.answers } : {}),
243
+ ...(options.daemon ? { daemon: options.daemon } : faux ? {} : { daemon: dockerDaemonOf(env) }),
156
244
  ...(options.openComputerView ? { openComputerView: options.openComputerView } : {}),
157
245
  };
158
246
  const factories = faux?.factories ?? computerProviderFactories(providers, (id) => storeLedger(store.computerLedger, id));
@@ -1,4 +1,4 @@
1
- import type { ComputerProvider, ComputerSpec, Result, Swept } from "@parcha/agentrun-computer";
1
+ import type { ComputerProvider, ComputerSpec } from "@parcha/agentrun-computer";
2
2
  import { type PlaceView } from "../computer-setting.js";
3
3
  import { type KeyTable, type ProviderSettings } from "../computer-keys.js";
4
4
  import type { Store } from "../store/index.js";
@@ -20,12 +20,15 @@ export interface ClaimLaunch {
20
20
  /** What the run's computer may reach, as its launch resolved it (run-launch.ts boxAllow): "none" or absent is no
21
21
  * network, a list an allow-list. The computer's spec takes it. */
22
22
  readonly boxAllow?: readonly string[] | "none";
23
+ /** The operator set that (run-launch.ts boxAllowSet), not the default. */
24
+ readonly boxAllowSet?: true;
23
25
  }
24
- /** What of the run a computer's spec is made from, besides its kind and provider: what it may reach; and what the
25
- * provider declares it makes (its capabilities()): its networks and its longest lifetime. Absent, the spec asks as the
26
- * run allows. */
26
+ /** What of the run a computer's spec is made from, besides its kind and provider: what it may reach, and whether the
27
+ * operator set that; and what the provider declares it makes (its capabilities()): its networks and its longest
28
+ * lifetime. Absent, the spec asks as the run allows. */
27
29
  export interface ComputerRun {
28
30
  readonly boxAllow?: readonly string[] | "none";
31
+ readonly boxAllowSet?: true;
29
32
  readonly networks?: readonly ("none" | "open" | "allow")[];
30
33
  readonly maxLifetimeMs?: number;
31
34
  }
@@ -81,6 +84,8 @@ export interface ComputerForClaimDeps {
81
84
  readonly settings?: ProviderSettings;
82
85
  /** Whether a provider that binds a folder of this machine answers here now (Docker): the Mode 1 default asks it. */
83
86
  readonly answers?: (provider: string) => Promise<boolean>;
87
+ /** Its daemon's answer (docker-daemon.ts), asked when a launch on such a provider fails (ClaimSide.daemon). */
88
+ readonly daemon?: ClaimSide["daemon"];
84
89
  /** A launch of the server's own in place of the providers' (--faux's desktop). */
85
90
  readonly launch?: ClaimSide["launch"];
86
91
  /** The run's folder (mounted only where the provider mounts it) and the mount check attach runs. */
@@ -95,6 +100,9 @@ export interface ComputerForClaimDeps {
95
100
  * a host that lacks it, or a named image pulled); never when the host has it. The run's caller says it on the run's
96
101
  * timeline. Absent: nothing is said. */
97
102
  readonly onImageWait?: ImageWait;
103
+ /** Why this run's harness takes no computer of `kind`, in words (a CLI harness's of a browser), or undefined: a kind
104
+ * it refuses is refused with code computer_unavailable before anything is made. Absent: every kind. */
105
+ readonly refuseKind?: (kind: Exclude<ComputerSettingKind, "none">) => string | undefined;
98
106
  readonly signal?: AbortSignal;
99
107
  }
100
108
  /** The run's computer at its claim: what runTools takes, the person's hold on a desktop's or phone's ports, and its
@@ -107,13 +115,6 @@ export interface ClaimComputer {
107
115
  }
108
116
  /** Told once, before the claim waits on its computer's image being built or pulled; never when the host has it. */
109
117
  export type ImageWait = (making: "build" | "pull", tag: string) => void;
110
- /** The runner's start: every computer of `providers` with all of `labels` whose deadline (its agentrun.computer.deadline
111
- * label) has passed is ended, once (the janitor's sweep), each provider apart. A provider whose vendor ends its own
112
- * computers answers its refusal in words; nothing throws. */
113
- export declare function sweepComputers(providers: readonly ComputerProvider[], labels: Readonly<Record<string, string>>, now?: Date): Promise<{
114
- readonly provider: string;
115
- readonly swept: Result<Swept>;
116
- }[]>;
117
118
  /** The run's computer for this claim, started and registered; `{ computer: undefined }` for none. Throws with code
118
119
  * computer_unavailable and the server's words when the agent's computer cannot be had here. */
119
120
  export declare function computerForClaim(launch: ClaimLaunch, store: Pick<Store, "agents" | "sealedKeys" | "computerLedger">, deps: ComputerForClaimDeps): Promise<ClaimComputer>;
@@ -1,4 +1,4 @@
1
- import { releaseOlder, runLabels, sweep } from "@parcha/agentrun-computer";
1
+ import { releaseOlder, runLabels } from "@parcha/agentrun-computer";
2
2
  import { computerOfPlace } from "../computer-setting.js";
3
3
  import { KEY_TABLE, providerOptions } from "../computer-keys.js";
4
4
  import { getSealedKey } from "../server-keys.js";
@@ -39,6 +39,62 @@ async function readyImage(provider, signal, onWait) {
39
39
  return ready.value;
40
40
  }
41
41
  const watching = (provider) => typeof provider.watch === "function" ? provider : undefined;
42
+ /** The browser computer `ref`'s CDP URL now: the provider's connect (its signed URL), its words and code when the session
43
+ * has ended. The connection itself is let go once read. */
44
+ async function cdpUrlOf(provider, ref) {
45
+ const connected = await provider.connect(ref);
46
+ if (!connected.ok)
47
+ throw Object.assign(new Error(connected.error.message), { code: connected.error.code });
48
+ const url = connected.value.cdp?.url;
49
+ await connected.value.close().catch(() => undefined);
50
+ if (!url)
51
+ throw new Error(`the ${provider.id} computer ${ref.id} gave no CDP connection`);
52
+ return url;
53
+ }
54
+ /** The label a browser session's computer carries its browser tool's tag in. */
55
+ const SESSION_TAG = "agentrun.session";
56
+ /** A label value of letters, digits and `_.:-` alone, as every provider takes one. */
57
+ const labelValue = (value) => value.replace(/[^A-Za-z0-9_.:-]+/g, "_").slice(0, 63);
58
+ /** A provider failure as an error with the provider's words and code. */
59
+ const failed = (error) => Object.assign(new Error(error.message), { code: error.code });
60
+ /** A browser computer's provider as the run's browser tool opens its sessions through it (ClaimBrowser): nothing is made
61
+ * at the claim. A session's computer is made at the tool's call, under the run's labels (`spec`'s) and the tool's tag;
62
+ * its release lets go of every one the tool left open. */
63
+ function claimBrowser(provider, spec) {
64
+ const open = new Set();
65
+ const release = async (id) => {
66
+ const released = await provider.release({ id, name: id }, "delete");
67
+ if (!released.ok)
68
+ throw failed(released.error);
69
+ open.delete(id);
70
+ };
71
+ return {
72
+ browser: {
73
+ provider: provider.id,
74
+ maxLifetimeS: Math.floor(spec.lifetimeMs / 1000),
75
+ async open(tag, lifetimeS, signal) {
76
+ const lifetimeMs = Math.min(spec.lifetimeMs, Math.max(1, Math.round(lifetimeS)) * 1000);
77
+ const created = await provider.create({ ...spec, lifetimeMs, labels: { ...spec.labels, [SESSION_TAG]: labelValue(tag) } }, signal ? { signal } : {});
78
+ if (!created.ok)
79
+ throw failed(created.error);
80
+ open.add(created.value.id);
81
+ return created.value.id;
82
+ },
83
+ async find(tag) {
84
+ const listed = await provider.list({ ...spec.labels, [SESSION_TAG]: labelValue(tag) });
85
+ if (!listed.ok)
86
+ throw failed(listed.error);
87
+ return listed.value.map((one) => one.ref.id);
88
+ },
89
+ url: (id) => cdpUrlOf(provider, { id, name: id }),
90
+ close: release,
91
+ },
92
+ async release() {
93
+ for (const id of [...open])
94
+ await release(id).catch(() => undefined);
95
+ },
96
+ };
97
+ }
42
98
  /** `entry`'s computer started through its provider: the run's computers of a lower claim epoch released first (the
43
99
  * janitor's releaseOlder: one claim's computer at a time), then made with the run's labels (and the run's folder, when
44
100
  * it mounts), connected, its groups from its hello; its read-back is the provider's own; its release deletes it. */
@@ -81,17 +137,6 @@ async function providerLaunch(provider, claim, spec, signal) {
81
137
  },
82
138
  };
83
139
  }
84
- /** The runner's start: every computer of `providers` with all of `labels` whose deadline (its agentrun.computer.deadline
85
- * label) has passed is ended, once (the janitor's sweep), each provider apart. A provider whose vendor ends its own
86
- * computers answers its refusal in words; nothing throws. */
87
- export async function sweepComputers(providers, labels, now = new Date()) {
88
- const out = [];
89
- for (const provider of providers) {
90
- const swept = await sweep(provider, { labels: { ...labels }, now }).catch((error) => ({ ok: false, error: { code: "refused", message: error instanceof Error ? error.message : String(error) } }));
91
- out.push({ provider: provider.id, swept });
92
- }
93
- return out;
94
- }
95
140
  /** The run's computer for this claim, started and registered; `{ computer: undefined }` for none. Throws with code
96
141
  * computer_unavailable and the server's words when the agent's computer cannot be had here. */
97
142
  export async function computerForClaim(launch, store, deps) {
@@ -103,9 +148,10 @@ export async function computerForClaim(launch, store, deps) {
103
148
  const side = {
104
149
  keysOf: providerKeys(deps.kinds, deps.keyOf ?? (async (name) => (deps.sealKey ? await getSealedKey(store, deps.sealKey, name) : undefined))),
105
150
  answers: deps.answers ?? (async () => false),
106
- async launch(entry, { kind, keys, mount, signal }) {
151
+ ...(deps.daemon ? { daemon: deps.daemon } : {}),
152
+ async launch(entry, { kind, keys, mount, gpu, signal }) {
107
153
  if (deps.launch) {
108
- const own = await deps.launch(entry, { kind, keys, ...(mount ? { mount } : {}), ...(signal ? { signal } : {}) });
154
+ const own = await deps.launch(entry, { kind, keys, ...(mount ? { mount } : {}), ...(gpu ? { gpu } : {}), ...(signal ? { signal } : {}) });
109
155
  const ownWatch = own.watch;
110
156
  made = { id: own.id, ...(ownWatch ? { watch: ownWatch } : {}) };
111
157
  madeBy = entry;
@@ -123,6 +169,7 @@ export async function computerForClaim(launch, store, deps) {
123
169
  const { network: networks, maxLifetimeMs } = provider.capabilities();
124
170
  const run = {
125
171
  ...(launch.boxAllow === undefined ? {} : { boxAllow: launch.boxAllow }),
172
+ ...(launch.boxAllowSet ? { boxAllowSet: true } : {}),
126
173
  ...(networks ? { networks } : {}),
127
174
  ...(typeof maxLifetimeMs === "number" ? { maxLifetimeMs } : {}),
128
175
  };
@@ -130,7 +177,18 @@ export async function computerForClaim(launch, store, deps) {
130
177
  // pulled) does so first, and the computer starts from the tag it answers: the recipe's tag is in no registry, so a
131
178
  // run of it unbuilt would be refused.
132
179
  const ready = await readyImage(provider, signal, deps.onImageWait);
133
- const spec = { ...deps.spec(entry, kind, run), ...(ready ? { image: ready } : {}), labels: runLabels(launch.runId, launch.epoch), ...(mount ? { mount } : {}) };
180
+ const spec = { ...deps.spec(entry, kind, run), ...(ready ? { image: ready } : {}), labels: runLabels(launch.runId, launch.epoch), ...(mount ? { mount } : {}), ...(gpu ? { gpu } : {}) };
181
+ // A browser makes nothing here: the run's browser tool opens each session through it, so a session lives (and is
182
+ // booked) only while the tool holds it.
183
+ if (kind === "browser") {
184
+ // The run's sessions of a lower claim epoch go first, as a computer's do (the janitor's releaseOlder).
185
+ const older = await releaseOlder(provider, launch.runId, launch.epoch, signal ? { signal } : {});
186
+ if (!older.ok)
187
+ throw new Error(older.error.message);
188
+ const opened = claimBrowser(provider, spec);
189
+ madeBy = entry;
190
+ return { id: entry.id, groups: ["cdp"], browser: opened.browser, release: opened.release };
191
+ }
134
192
  const launched = await providerLaunch(provider, launch, spec, signal);
135
193
  made = { id: launched.id, machine: launched.machine, ...(launched.watch ? { watch: launched.watch } : {}) };
136
194
  madeBy = entry;
@@ -142,6 +200,9 @@ export async function computerForClaim(launch, store, deps) {
142
200
  throw unavailable(resolved.refused);
143
201
  if (resolved.kind === "none")
144
202
  return { computer: undefined, release: noop };
203
+ const refusedKind = deps.refuseKind?.(resolved.kind);
204
+ if (refusedKind)
205
+ throw unavailable(refusedKind);
145
206
  // The run's mount credential (Mode 2), minted when the mount was made: removed with the computer, or at once when the
146
207
  // start fails after it was minted.
147
208
  const revoke = async () => { await deps.revokeMount?.(); };
@@ -65,10 +65,15 @@ export type ComputerHold = {
65
65
  /** The current take's name, while one is pending or held. */
66
66
  current(): string | undefined;
67
67
  /** The person takes it: pending while an agent call is in flight, then theirs. False (nothing changes) when a take is
68
- * pending or held already, or the hold is closed. `by`: the command row that asked. */
68
+ * pending or held already, or the run is ending, or the hold is closed. `by`: the command row that asked. */
69
69
  take(options?: {
70
70
  readonly by?: number;
71
71
  }): boolean;
72
+ /** The run is ending (its agent is done): nothing new is taken from now, and a take in force goes on until it ends, so
73
+ * the run's end can wait for it and no later take is cut by the release (#2134). */
74
+ finish(): void;
75
+ /** Whether the run is ending (finish). */
76
+ finishing(): boolean;
72
77
  /** The take ends (`given` by default): false when there was none. A hand-back while the agent's ask is open answers it
73
78
  * done, and the take ends `answered`. */
74
79
  give(why?: HoldEnd, options?: {
@@ -38,6 +38,7 @@ export function computerHold(options = {}) {
38
38
  let inFlight = 0;
39
39
  let epoch = 0;
40
40
  let closed = false;
41
+ let ending = false;
41
42
  let open;
42
43
  const waiters = new Set();
43
44
  const listeners = new Set();
@@ -93,7 +94,7 @@ export function computerHold(options = {}) {
93
94
  state: () => state,
94
95
  current: () => take,
95
96
  take({ by } = {}) {
96
- if (closed || state !== "agent")
97
+ if (closed || ending || state !== "agent")
97
98
  return false;
98
99
  take = by !== undefined ? `c${by}` : `v${now()}-${randomBytes(3).toString("hex")}`;
99
100
  epoch++;
@@ -105,6 +106,10 @@ export function computerHold(options = {}) {
105
106
  toPerson(by);
106
107
  return true;
107
108
  },
109
+ finish() {
110
+ ending = true;
111
+ },
112
+ finishing: () => ending,
108
113
  give,
109
114
  away: () => void give("away"),
110
115
  touched() {
@@ -0,0 +1,47 @@
1
+ import { type Result, type Swept } from "@parcha/agentrun-computer";
2
+ import { type KeyTable, type ProviderSettings } from "../computer-keys.js";
3
+ import type { Store } from "../store/index.js";
4
+ import type { ComputerDeps } from "./computer-deps.js";
5
+ /** How often the janitor sweeps after its first sweep at the runner's start. */
6
+ export declare const JANITOR_EVERY_MS: number;
7
+ /** The longest close() waits for a sweep it cut: a vendor call that ignores the cut is left to finish on its own. */
8
+ export declare const JANITOR_CLOSE_MS = 5000;
9
+ /** The longest one provider's sweep may take: past it the sweep is cut and left, said as a timeout, and the next provider
10
+ * is swept, so a stuck vendor (a Docker command has no timeout of its own) never stops the others or later sweeps. */
11
+ export declare const JANITOR_PROVIDER_MS: number;
12
+ export interface JanitorDeps {
13
+ /** The store: whose runs a computer's run label names, and the phone provider's ledger. */
14
+ readonly store: Pick<Store, "runs" | "computerLedger">;
15
+ /** The run's computers as the runner composed them (computer-deps.ts computerDepsFrom): the registry, the provider
16
+ * makers and the key reader. */
17
+ readonly computers: Pick<ComputerDeps, "kinds" | "providers" | "keyOf"> & {
18
+ readonly settings?: ProviderSettings;
19
+ readonly keyTable?: KeyTable;
20
+ };
21
+ /** The runs' environment: under --faux (AGENTRUN_SERVE_FAUX) the janitor never starts. */
22
+ readonly env: NodeJS.ProcessEnv;
23
+ /** Where a sweep's outcome and a provider's failure are said. */
24
+ readonly log: (line: string) => void;
25
+ readonly everyMs?: number;
26
+ /** The longest close() waits for a sweep it cut. Default JANITOR_CLOSE_MS. */
27
+ readonly closeMs?: number;
28
+ /** The longest one provider's sweep may take. Default JANITOR_PROVIDER_MS. */
29
+ readonly providerMs?: number;
30
+ /** The clock a computer's deadline is read by. Default Date.now. */
31
+ readonly now?: () => number;
32
+ }
33
+ /** One provider's sweep: what it ended, or why it could not sweep, in words. */
34
+ export type ProviderSweep = {
35
+ readonly provider: string;
36
+ readonly swept: Result<Swept>;
37
+ };
38
+ /** One sweep of every provider this server can list: each provider whose keys it has and whose computers the janitor
39
+ * ends, its computers past their deadline that are this server's and unheld ended. A provider that fails is answered
40
+ * in its words; nothing throws. */
41
+ export declare function sweepServerComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now" | "providerMs">, signal?: AbortSignal): Promise<ProviderSweep[]>;
42
+ /** The janitor: one sweep now, then one every `everyMs`, never two at once. `close()` stops it, cuts a sweep under way
43
+ * and waits for it at most `closeMs`. Under --faux it does nothing. */
44
+ export declare function startJanitor(deps: JanitorDeps): {
45
+ readonly first: Promise<void>;
46
+ close(): Promise<void>;
47
+ };