@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
package/dist/box/sweep.js DELETED
@@ -1,193 +0,0 @@
1
- // What serve does with the boxes runs leave: the box a run's process recorded (its box.json), reaped once nothing owns
2
- // it, and the cgroups no record names. A box is let go only on the reap's own count: empty, its record is marked reaped
3
- // and its cgroup removed; anything else (left processes, a reap that throws) quarantines it, its record kept, and every
4
- // later sweep tries again. A record nothing could count (no pid and no cgroup: a crash before the box's first process
5
- // was known) is cleared only once the run process that wrote it is gone, by its pid and start time, so a box still
6
- // starting is never written off.
7
- import { readFileSync } from "node:fs";
8
- import { join } from "node:path";
9
- import { readJson, writeDurable } from "../files.js";
10
- import { BOX_RECORD } from "./record.js";
11
- /** The data directory's file of quarantined boxes. */
12
- export const BOX_QUARANTINE = "box-quarantine.json";
13
- /** A run cgroup removed, or already gone: a reap that empties a cgroup may remove it itself (bwrapBox's does), so a
14
- * missing one is the removal done. Any other failure is the caller's. */
15
- async function removeCgroup(cgroups, path) {
16
- try {
17
- await cgroups.remove(path);
18
- }
19
- catch (error) {
20
- if (error?.code !== "ENOENT")
21
- throw error;
22
- }
23
- }
24
- /** A process's start time as /proc/<pid>/stat gives it (clock ticks since boot), or undefined when it is not running. */
25
- export function startTimeOf(pid) {
26
- try {
27
- const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
28
- return stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19];
29
- }
30
- catch {
31
- return undefined;
32
- }
33
- }
34
- /** The record's writer is gone: no process has its pid, or the one that has it started at another time (a reused pid).
35
- * box-image.ts asks the same of the server that wrote the image's record. */
36
- export function writerGone(writer) {
37
- if (!writer || !Number.isSafeInteger(writer.pid) || writer.pid <= 0)
38
- return true;
39
- return startTimeOf(writer.pid) !== writer.startTime;
40
- }
41
- /** The boxes that a reap could not let go, kept across restarts: by run (or "" for a cgroup no record named). */
42
- export class Quarantine {
43
- file;
44
- entries;
45
- constructor(file, entries) {
46
- this.file = file;
47
- this.entries = entries;
48
- }
49
- static async open(file) {
50
- const kept = await readJson(file);
51
- const entries = new Map();
52
- if (Array.isArray(kept))
53
- for (const e of kept)
54
- if (e && typeof e.key === "string")
55
- entries.set(e.key, { record: e.record, why: e.why, at: e.at });
56
- return new Quarantine(file, entries);
57
- }
58
- has(key) {
59
- return this.entries.has(key);
60
- }
61
- list() {
62
- return [...this.entries].map(([key, e]) => ({ key, ...e }));
63
- }
64
- async add(key, record, why) {
65
- this.entries.set(key, { record, why, at: new Date().toISOString() });
66
- await this.save();
67
- }
68
- async clear(key) {
69
- if (this.entries.delete(key))
70
- await this.save();
71
- }
72
- // One save at a time, each writing the whole set as it is then: concurrent writes of the same file would share its
73
- // temporary file, and one could lose another's entries or fail its rename.
74
- saving = Promise.resolve();
75
- save() {
76
- const next = this.saving.catch(() => { }).then(async () => void (await writeDurable(this.file, `${JSON.stringify(this.list())}\n`)));
77
- this.saving = next;
78
- return next;
79
- }
80
- }
81
- /** The quarantine key of a cgroup no record named. */
82
- const leftoverKey = (path) => `cgroup:${path}`;
83
- /** The quarantine key of a run's box: by the box, so a run whose box is quarantined and that opens a new one keeps the
84
- * old one held, and the new one's reap never clears it. */
85
- export const boxKey = (runId, record) => `${runId}/${record.id}`;
86
- /** Whether a cgroup is still named by a box other than `key`'s that is not yet let go: a quarantined one, or (`current`)
87
- * the run's current box. A run's boxes share its cgroup, so one box's empty count never removes another's. */
88
- function namedElsewhere(quarantine, cgroup, key, current) {
89
- if (current && !current.reaped && current.cgroup === cgroup && current.id !== key.slice(key.lastIndexOf("/") + 1))
90
- return true;
91
- return quarantine.list().some((entry) => entry.key !== key && entry.record.cgroup === cgroup);
92
- }
93
- const left = (verdict) => `its reap left ${verdict.left} process${verdict.left === 1 ? "" : "es"} (counted by ${verdict.by})`;
94
- /**
95
- * The box the run in `runDir` recorded, let go if nothing owns it: none recorded, or already let go (`none`); its writer,
96
- * the run process, still alive, so the box is that process's (`owned`); else reaped. Empty: marked, its cgroup removed
97
- * (`reaped`). Nothing to count, with its writer gone: marked (`cleared`). Anything else: quarantined, its record kept.
98
- */
99
- export async function settleBox(host, quarantine, runId, runDir) {
100
- const path = join(runDir, BOX_RECORD);
101
- const stored = (await readJson(path));
102
- if (!stored || typeof stored.id !== "string" || stored.reaped)
103
- return "none";
104
- if (!writerGone(stored.writer))
105
- return "owned";
106
- const { reaped: _, ...record } = stored;
107
- let verdict;
108
- try {
109
- verdict = await host.reap(record);
110
- }
111
- catch (error) {
112
- await quarantine.add(boxKey(runId, record), record, `its reap failed: ${error.message}`);
113
- return "quarantined";
114
- }
115
- if (verdict.by === "none") {
116
- // Nothing named the box to count, and its writer is gone: --die-with-parent ended whatever had started.
117
- await writeDurable(path, `${JSON.stringify({ ...record, reaped: { at: new Date().toISOString(), by: "none" } })}\n`);
118
- await quarantine.clear(boxKey(runId, record));
119
- return "cleared";
120
- }
121
- if (!verdict.empty) {
122
- await quarantine.add(boxKey(runId, record), record, left(verdict));
123
- return "quarantined";
124
- }
125
- if (record.cgroup && host.cgroups && !namedElsewhere(quarantine, record.cgroup, boxKey(runId, record)))
126
- await removeCgroup(host.cgroups, record.cgroup);
127
- await writeDurable(path, `${JSON.stringify({ ...record, reaped: { at: new Date().toISOString(), by: verdict.by } })}\n`);
128
- await quarantine.clear(boxKey(runId, record));
129
- return "reaped";
130
- }
131
- /** At start, before any run opens a box: every run's box let go that nothing owns; then every box the quarantine still
132
- * holds that is no longer its run's current one (the run opened another since), tried again by its own record; then
133
- * every run cgroup no record names, reaped by the cgroup and removed when empty. */
134
- export async function sweepBoxes(host, quarantine, runs) {
135
- const settled = {};
136
- const named = new Set();
137
- for (const run of runs) {
138
- settled[run.id] = await settleBox(host, quarantine, run.id, run.dir);
139
- if (settled[run.id] === "owned" || settled[run.id] === "quarantined") {
140
- const cgroup = (await readJson(join(run.dir, BOX_RECORD)))?.cgroup;
141
- if (cgroup)
142
- named.add(cgroup);
143
- }
144
- }
145
- // A run's earlier box, still held: only its own reap's count lets it go. Its cgroup stays named while it is held.
146
- const current = new Set(runs.map((run) => run.id));
147
- for (const entry of quarantine.list()) {
148
- if (entry.key.startsWith("cgroup:"))
149
- continue;
150
- const runId = entry.key.slice(0, entry.key.lastIndexOf("/"));
151
- const now = current.has(runId) ? (await readJson(join(runs.find((r) => r.id === runId).dir, BOX_RECORD))) : null;
152
- if (now?.id === entry.record.id) {
153
- if (entry.record.cgroup && quarantine.has(entry.key))
154
- named.add(entry.record.cgroup);
155
- continue;
156
- }
157
- let verdict;
158
- if (!writerGone(entry.record.writer))
159
- verdict = undefined;
160
- else
161
- verdict = await host.reap(entry.record).catch(() => undefined);
162
- if (verdict && (verdict.empty || verdict.by === "none")) {
163
- const cgroup = entry.record.cgroup;
164
- if (verdict.empty && cgroup && host.cgroups && !namedElsewhere(quarantine, cgroup, entry.key, now))
165
- await removeCgroup(host.cgroups, cgroup);
166
- await quarantine.clear(entry.key);
167
- }
168
- else if (entry.record.cgroup)
169
- named.add(entry.record.cgroup);
170
- }
171
- if (!host.cgroups)
172
- return settled;
173
- for (const cgroup of await host.cgroups.list()) {
174
- if (named.has(cgroup))
175
- continue;
176
- const record = { id: leftoverKey(cgroup), cgroup, writer: { pid: 0, startTime: "" } };
177
- let verdict;
178
- try {
179
- verdict = await host.reap(record);
180
- }
181
- catch (error) {
182
- await quarantine.add(leftoverKey(cgroup), record, `its reap failed: ${error.message}`);
183
- continue;
184
- }
185
- if (verdict.empty) {
186
- await removeCgroup(host.cgroups, cgroup);
187
- await quarantine.clear(leftoverKey(cgroup));
188
- }
189
- else
190
- await quarantine.add(leftoverKey(cgroup), record, left(verdict));
191
- }
192
- return settled;
193
- }
@@ -1,118 +0,0 @@
1
- /** What `docker info` is asked on a Mac: the daemon's OS type, its kernel, the name it gives its OS, and its host name. */
2
- export declare const VM_INFO_FORMAT = "{{.OSType}}|{{.KernelVersion}}|{{.OperatingSystem}}|{{.Name}}";
3
- /** The VM runtimes a box's network goes on for, on a Mac. */
4
- export declare const VM_RUNTIMES: readonly ["Docker Desktop", "OrbStack", "colima"];
5
- export type VmRuntime = (typeof VM_RUNTIMES)[number];
6
- /** Where this Docker runs, by `docker info`'s answer to VM_INFO_FORMAT (`answer`), on a server with `platform` and
7
- * `kernel` (os.release()): a VM runtime this server knows, or why not. Pure. A runtime counts only when docker info
8
- * names it (OperatingSystem `Docker Desktop` or `OrbStack`, or a colima host Name) AND its kernel is not this
9
- * machine's: a VM in between. Anything else is a reason, never a guess. */
10
- export declare function vmDockerOf(answer: string, here: {
11
- platform: string;
12
- kernel: string;
13
- }): {
14
- runtime: VmRuntime;
15
- } | {
16
- reason: string;
17
- };
18
- /** `docker`, with `args`, in the server's environment through the cut a run's takes, nothing on its stdin, within
19
- * `timeoutMs`: its exit code (or `timeout`, or the spawn's error code) and its output. */
20
- export type DockerCall = (args: readonly string[], timeoutMs: number, signal?: AbortSignal) => Promise<{
21
- code: number | string;
22
- stdout: string;
23
- stderr: string;
24
- }>;
25
- export declare const dockerCall: (env: NodeJS.ProcessEnv) => DockerCall;
26
- /** Where this Mac's Docker runs (vmDockerOf), asked of `docker info` within its bound; a daemon the docker CLI does not
27
- * reach by a local unix socket (DOCKER_HOST, or a context, elsewhere) is not this Mac's VM, and gets no network. */
28
- export declare function vmDocker(env: NodeJS.ProcessEnv, options?: {
29
- platform?: string;
30
- kernel?: string;
31
- timeoutMs?: number;
32
- docker?: DockerCall;
33
- }): Promise<{
34
- runtime: VmRuntime;
35
- } | {
36
- reason: string;
37
- }>;
38
- /** The proxy container's image: `node:22-bookworm-slim` (the kernel needs Node 22.19 or later), pinned to the multi-arch
39
- * index digest Docker Hub served for that tag on 2026-11-05 (Node 22.23.4), as BOX_BASE is pinned (box-image.ts), so every Mac runs the
40
- * same Node. To move it, read the tag's index digest again the way BOX_BASE says, and put it here. */
41
- export declare const EGRESS_IMAGE = "node:22-bookworm-slim@sha256:4b150b48b4116e2a22f2c39cc27025fbf4f153e2078fd3b14d159fdb5c7279da";
42
- /** The label the proxy container and its volume carry: the server's key (egressKeyOf). */
43
- export declare const EGRESS_LABEL = "agentrun.egress";
44
- /** Where the volume is in the proxy container, and its code. */
45
- export declare const EGRESS_VOLUME_MOUNT = "/egress";
46
- export declare const EGRESS_CODE_MOUNT = "/code";
47
- /** What the entry logs once its socket listens. */
48
- export declare const EGRESS_READY = "egress ready";
49
- /** How long the start waits for EGRESS_READY once the container runs. */
50
- export declare const EGRESS_READY_MS = 10000;
51
- /** How often the server looks for its proxy container, to start it again when it is gone. */
52
- export declare const EGRESS_WATCH_MS = 5000;
53
- /** How long the start's pull of EGRESS_IMAGE may take. */
54
- export declare const EGRESS_PULL_MS: number;
55
- /** Connections open at once through the server's one proxy, every run's together. */
56
- export declare const VM_MAX_TUNNELS = 256;
57
- /** This server's key: its data root's hash, so two servers on one daemon get two proxies and two volumes. */
58
- export declare const egressKeyOf: (data: string) => string;
59
- /** The proxy container's name, and its volume's, for a key. */
60
- export declare const egressNameOf: (key: string) => string;
61
- /** The entry the proxy container runs (`node /code/main.js <socket>`): it refuses to start with no list, for a proxy with
62
- * none would reach every public host, and logs EGRESS_READY once its socket listens. Its decisions go to its log. */
63
- export declare const MAIN: string;
64
- /** The kernel's two proxy files, as built: they import only `node:` modules and each other. */
65
- export declare const PROXY_FILES: readonly ["egress-proxy.js", "egress-table.js"];
66
- /** The code the proxy container binds read-only: PROXY_FILES of this server's own build, and MAIN, written into
67
- * `<data>/egress/<12 hex of their hash>/` once (a directory per version, so a running container's code never changes
68
- * under it). Its path, real (Docker binds it by that). */
69
- export declare function egressCode(data: string): Promise<string>;
70
- /** `docker volume create` of the server's volume: tmpfs, so no stale socket outlives the VM, and owned by the server's
71
- * uid:gid with mode 0700, as the proxy's socket (0600) and the box (both run as that uid) need. */
72
- export declare const egressVolumeArgs: (key: string, uid: number, gid: number) => string[];
73
- /** `docker run` of the proxy container. Docker's default network, the only container with one; no published port, no
74
- * Docker socket, no variable but the list; a read-only root, no capability, no privilege to gain, the server's uid:gid;
75
- * bounded memory, processes and log. Pinned by a golden test. */
76
- export declare const egressRunArgs: (options: {
77
- key: string;
78
- uid: number;
79
- gid: number;
80
- code: string;
81
- allow: readonly string[];
82
- }) => string[];
83
- /** The proxy container's state: `ready` once it listens; `preparing` while it is pulled, started or started again;
84
- * `unavailable` when it could not start, with why. */
85
- export type EgressState = {
86
- state: "ready";
87
- } | {
88
- state: "preparing" | "unavailable";
89
- reason: string;
90
- };
91
- /** `volume`: the volume a run's box mounts, once it is made; absent when it could not be. */
92
- export type EgressContainer = {
93
- readonly volume?: string;
94
- state(): EgressState;
95
- close(): Promise<void>;
96
- };
97
- /**
98
- * The server's proxy container (see the head of this file): made ready now when Docker has its image (within
99
- * EGRESS_READY_MS), else pulled and started in the background; then watched every `watchMs`, and started again when it
100
- * is gone or stopped. `onChange` is told each new state. `close` stops the watch and removes the container; its volume
101
- * stays, for boxes still mounting it and the next start.
102
- */
103
- export declare function startEgressContainer(options: {
104
- env: NodeJS.ProcessEnv;
105
- data: string;
106
- allow: readonly string[];
107
- uid: number;
108
- gid: number;
109
- onChange?: (state: EgressState) => void;
110
- warn?: (message: string) => void;
111
- docker?: DockerCall;
112
- watchMs?: number;
113
- readyMs?: number;
114
- pullMs?: number;
115
- }): Promise<EgressContainer>;
116
- /** What the server hands a run's process on a Mac with the proxy container: its volume's name, which the run's Docker
117
- * boxes mount read-only at EGRESS_MOUNT. Unset on Linux, where the run's process starts its own proxy (box-egress.ts). */
118
- export declare const RUN_BOX_VOLUME_ENV = "AGENTRUN_RUN_BOX_VOLUME";