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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (388) hide show
  1. package/CHANGELOG.md +231 -0
  2. package/README.md +580 -320
  3. package/dist/acp-package.d.ts +28 -0
  4. package/dist/acp-package.js +23 -0
  5. package/dist/adapter-facts.d.ts +8 -1
  6. package/dist/adapter-facts.js +9 -1
  7. package/dist/adapters.d.ts +5 -0
  8. package/dist/adapters.js +36 -3
  9. package/dist/agent-delete.d.ts +50 -0
  10. package/dist/agent-delete.js +125 -0
  11. package/dist/agents-store.d.ts +9 -3
  12. package/dist/agents-store.js +22 -8
  13. package/dist/agents.d.ts +5 -1
  14. package/dist/agents.js +8 -4
  15. package/dist/api-only.d.ts +8 -3
  16. package/dist/api-only.js +10 -4
  17. package/dist/archil-routes.d.ts +5 -1
  18. package/dist/archil-routes.js +57 -2
  19. package/dist/browser-wait-ended.d.ts +2 -0
  20. package/dist/browser-wait-ended.js +2 -0
  21. package/dist/calendar/ics-faux.d.ts +3 -1
  22. package/dist/calendar/ics-faux.js +12 -2
  23. package/dist/calendar/tools.js +5 -2
  24. package/dist/chatgpt-plan.js +3 -1
  25. package/dist/claude-login.d.ts +135 -0
  26. package/dist/claude-login.js +244 -0
  27. package/dist/cli.js +92 -22
  28. package/dist/composio-preset.d.ts +4 -3
  29. package/dist/composio-preset.js +81 -11
  30. package/dist/composio-triggers.d.ts +34 -2
  31. package/dist/composio-triggers.js +261 -31
  32. package/dist/computer-attach.d.ts +40 -5
  33. package/dist/computer-attach.js +130 -15
  34. package/dist/computer-keys.d.ts +3 -0
  35. package/dist/computer-keys.js +7 -1
  36. package/dist/computer-live.js +3 -2
  37. package/dist/computer-lost.d.ts +11 -0
  38. package/dist/computer-lost.js +19 -0
  39. package/dist/computer-record.d.ts +10 -2
  40. package/dist/computer-record.js +50 -18
  41. package/dist/computer-setting.d.ts +1 -0
  42. package/dist/computer-setting.js +2 -2
  43. package/dist/computers.d.ts +21 -10
  44. package/dist/computers.js +39 -28
  45. package/dist/console/assets/index-3O2Yl6hZ.js +701 -0
  46. package/dist/console/assets/index-DaFsZuzQ.css +1 -0
  47. package/dist/console/index.html +2 -2
  48. package/dist/decisions.d.ts +1 -1
  49. package/dist/dial-relay.d.ts +29 -0
  50. package/dist/dial-relay.js +94 -0
  51. package/dist/email/tools.js +10 -5
  52. package/dist/env-proxy.d.ts +2 -0
  53. package/dist/env-proxy.js +35 -0
  54. package/dist/errors.d.ts +4 -1
  55. package/dist/errors.js +10 -4
  56. package/dist/faux-computers.d.ts +8 -2
  57. package/dist/faux-computers.js +83 -6
  58. package/dist/harness-archive.d.ts +67 -0
  59. package/dist/harness-archive.js +177 -0
  60. package/dist/harness-goose.d.ts +34 -0
  61. package/dist/harness-goose.js +87 -0
  62. package/dist/harness-install.d.ts +21 -0
  63. package/dist/harness-install.js +24 -11
  64. package/dist/harness-opencode/adapter.js +1 -1
  65. package/dist/harness-table.d.ts +59 -3
  66. package/dist/harness-table.js +119 -14
  67. package/dist/harness.d.ts +17 -1
  68. package/dist/harness.js +22 -11
  69. package/dist/host.d.ts +12 -10
  70. package/dist/host.js +19 -16
  71. package/dist/import-worker.d.ts +3 -0
  72. package/dist/import-worker.js +7 -5
  73. package/dist/import.d.ts +22 -1
  74. package/dist/import.js +141 -28
  75. package/dist/key-check.js +3 -0
  76. package/dist/learn/audit.d.ts +6 -6
  77. package/dist/learn/audit.js +3 -7
  78. package/dist/learn/check.d.ts +26 -89
  79. package/dist/learn/check.js +58 -336
  80. package/dist/learn/commit.d.ts +63 -90
  81. package/dist/learn/commit.js +124 -316
  82. package/dist/learn/compile.d.ts +53 -50
  83. package/dist/learn/compile.js +88 -103
  84. package/dist/learn/corrections.d.ts +13 -0
  85. package/dist/learn/corrections.js +23 -0
  86. package/dist/learn/delete.d.ts +17 -0
  87. package/dist/learn/delete.js +42 -0
  88. package/dist/learn/folder.d.ts +35 -47
  89. package/dist/learn/folder.js +118 -134
  90. package/dist/learn/index.d.ts +3 -5
  91. package/dist/learn/index.js +6 -9
  92. package/dist/learn/learning.sh +45 -15
  93. package/dist/learn/promote.d.ts +20 -2
  94. package/dist/learn/promote.js +17 -4
  95. package/dist/learn/{skills.d.ts → workflow-draft.d.ts} +34 -38
  96. package/dist/learn/{skills.js → workflow-draft.js} +65 -59
  97. package/dist/learn/workflow-nodes.d.ts +22 -1
  98. package/dist/learn/workflow-nodes.js +29 -10
  99. package/dist/learned-workflow.js +2 -2
  100. package/dist/learning-cost.d.ts +127 -13
  101. package/dist/learning-cost.js +142 -10
  102. package/dist/learning-routes.d.ts +99 -170
  103. package/dist/learning-routes.js +199 -420
  104. package/dist/learning-run.d.ts +19 -40
  105. package/dist/learning-run.js +23 -93
  106. package/dist/learning-setting.d.ts +4 -4
  107. package/dist/learning-setting.js +6 -6
  108. package/dist/learning-words.d.ts +28 -74
  109. package/dist/learning-words.js +64 -162
  110. package/dist/learning.d.ts +62 -393
  111. package/dist/learning.js +101 -651
  112. package/dist/listen-first.d.ts +30 -0
  113. package/dist/listen-first.js +115 -0
  114. package/dist/live-reach.d.ts +25 -2
  115. package/dist/live-reach.js +46 -4
  116. package/dist/llms.txt +2 -1
  117. package/dist/local-models.d.ts +3 -11
  118. package/dist/local-models.js +5 -6
  119. package/dist/mcp-servers-store.d.ts +16 -16
  120. package/dist/model-form.js +6 -5
  121. package/dist/model.d.ts +12 -4
  122. package/dist/model.js +41 -16
  123. package/dist/native/binding.js +1 -1
  124. package/dist/native/connection.js +1 -1
  125. package/dist/native/discovery.js +1 -1
  126. package/dist/native/instructions.d.ts +3 -0
  127. package/dist/native/instructions.js +5 -3
  128. package/dist/native/remote-worker-bridge.js +9 -43
  129. package/dist/native/remote.d.ts +8 -15
  130. package/dist/native/remote.js +3 -3
  131. package/dist/native/schema.js +1 -1
  132. package/dist/native/types.d.ts +2 -2
  133. package/dist/openapi.js +375 -347
  134. package/dist/owner.d.ts +8 -0
  135. package/dist/owner.js +11 -0
  136. package/dist/paid-calls.d.ts +14 -0
  137. package/dist/paid-calls.js +31 -1
  138. package/dist/particle-provider.d.ts +11 -0
  139. package/dist/particle-provider.js +135 -0
  140. package/dist/place.d.ts +8 -3
  141. package/dist/place.js +13 -9
  142. package/dist/port-models.d.ts +12 -4
  143. package/dist/port-models.js +28 -11
  144. package/dist/print.d.ts +27 -0
  145. package/dist/print.js +21 -0
  146. package/dist/provider.d.ts +7 -0
  147. package/dist/provider.js +30 -2
  148. package/dist/reflect-range.js +2 -3
  149. package/dist/run-launch.d.ts +14 -28
  150. package/dist/run-launch.js +9 -12
  151. package/dist/run-learning-served.d.ts +16 -90
  152. package/dist/run-listing.d.ts +34 -0
  153. package/dist/run-listing.js +128 -0
  154. package/dist/run-model-key.d.ts +64 -0
  155. package/dist/run-model-key.js +147 -0
  156. package/dist/run-timeline-served.js +5 -3
  157. package/dist/run-trace.d.ts +97 -0
  158. package/dist/run-trace.js +253 -0
  159. package/dist/run-view.d.ts +15 -0
  160. package/dist/run-view.js +9 -0
  161. package/dist/runnable.js +2 -1
  162. package/dist/runner/adapter-loop.d.ts +14 -6
  163. package/dist/runner/adapter-loop.js +25 -14
  164. package/dist/runner/adapter-run-spec.d.ts +22 -7
  165. package/dist/runner/adapter-run-spec.js +130 -77
  166. package/dist/runner/adapters/index.d.ts +4 -2
  167. package/dist/runner/adapters/index.js +35 -4
  168. package/dist/runner/adapters/pi.d.ts +4 -12
  169. package/dist/runner/adapters/pi.js +2 -2
  170. package/dist/runner/audit.d.ts +5 -1
  171. package/dist/runner/audit.js +31 -12
  172. package/dist/runner/beside.d.ts +7 -2
  173. package/dist/runner/beside.js +41 -9
  174. package/dist/runner/claim-secrets.d.ts +66 -1
  175. package/dist/runner/claim-secrets.js +136 -3
  176. package/dist/runner/cli-run-spec.d.ts +2 -1
  177. package/dist/runner/cli-run-spec.js +82 -59
  178. package/dist/runner/commands.d.ts +3 -6
  179. package/dist/runner/conformance/harness.d.ts +6 -0
  180. package/dist/runner/conformance/harness.js +16 -5
  181. package/dist/runner/dispatch.d.ts +2 -2
  182. package/dist/runner/end-of-run.d.ts +6 -14
  183. package/dist/runner/end-of-run.js +8 -33
  184. package/dist/runner/feedback-round.d.ts +5 -4
  185. package/dist/runner/feedback-round.js +14 -10
  186. package/dist/runner/github-app.js +3 -1
  187. package/dist/runner/harness-loops.d.ts +0 -8
  188. package/dist/runner/harness-loops.js +30 -56
  189. package/dist/runner/held.d.ts +170 -0
  190. package/dist/runner/held.js +57 -0
  191. package/dist/runner/journal-kinds.d.ts +7 -3
  192. package/dist/runner/journal-kinds.js +2 -3
  193. package/dist/runner/learned-first.d.ts +40 -0
  194. package/dist/runner/learned-first.js +32 -0
  195. package/dist/runner/learning-end.d.ts +77 -66
  196. package/dist/runner/learning-end.js +250 -299
  197. package/dist/runner/learning-seed.d.ts +15 -33
  198. package/dist/runner/learning-seed.js +24 -45
  199. package/dist/runner/loop-contract.d.ts +8 -3
  200. package/dist/runner/loop-contract.js +4 -0
  201. package/dist/runner/loop.js +6 -2
  202. package/dist/runner/main.d.ts +18 -9
  203. package/dist/runner/main.js +82 -22
  204. package/dist/runner/mcp-run.d.ts +11 -0
  205. package/dist/runner/mcp-run.js +67 -0
  206. package/dist/runner/memory-claim.d.ts +14 -6
  207. package/dist/runner/memory-claim.js +18 -9
  208. package/dist/runner/model-route.d.ts +7 -4
  209. package/dist/runner/model-route.js +22 -10
  210. package/dist/runner/native-loop.d.ts +0 -7
  211. package/dist/runner/native-loop.js +9 -101
  212. package/dist/runner/promotion.d.ts +13 -6
  213. package/dist/runner/promotion.js +63 -17
  214. package/dist/runner/reflect-hook.d.ts +64 -91
  215. package/dist/runner/reflect-hook.js +112 -206
  216. package/dist/runner/run-channel.d.ts +43 -0
  217. package/dist/runner/run-channel.js +467 -0
  218. package/dist/runner/run-child-host.d.ts +169 -0
  219. package/dist/runner/run-child-host.js +1222 -0
  220. package/dist/runner/run-child.d.ts +1 -0
  221. package/dist/runner/run-child.js +254 -0
  222. package/dist/runner/run-errors.d.ts +2 -0
  223. package/dist/runner/run-errors.js +20 -0
  224. package/dist/runner/runner.js +58 -9
  225. package/dist/runner/sdk-ask.d.ts +78 -0
  226. package/dist/runner/sdk-ask.js +158 -0
  227. package/dist/runner/sdk-loop.d.ts +2 -4
  228. package/dist/runner/sdk-loop.js +29 -49
  229. package/dist/runner/store-secrets.d.ts +5 -3
  230. package/dist/runner/store-secrets.js +14 -6
  231. package/dist/runner/workflow-first.d.ts +5 -0
  232. package/dist/runner/workflow-first.js +4 -0
  233. package/dist/runner/workflow-spec.js +17 -4
  234. package/dist/runs-over-store.d.ts +21 -32
  235. package/dist/runs-over-store.js +44 -41
  236. package/dist/sdk/runs.d.ts +6 -11
  237. package/dist/sdk/runs.js +20 -41
  238. package/dist/sdk/schema.js +3 -4
  239. package/dist/sdk/workflows.d.ts +1 -1
  240. package/dist/seal-key.d.ts +5 -0
  241. package/dist/seal-key.js +7 -0
  242. package/dist/secrets.d.ts +3 -0
  243. package/dist/secrets.js +16 -3
  244. package/dist/server.d.ts +119 -83
  245. package/dist/server.js +1121 -419
  246. package/dist/sign-ins-store.d.ts +8 -0
  247. package/dist/sign-ins-store.js +36 -1
  248. package/dist/skills.js +1 -1
  249. package/dist/store/index.d.ts +3 -3
  250. package/dist/store/index.js +2 -2
  251. package/dist/store/journal/storage.js +3 -0
  252. package/dist/store/postgres/migrations/0017_run_listing.sql +83 -0
  253. package/dist/store/postgres/migrations/0018_trigger_template.sql +6 -0
  254. package/dist/store/postgres/migrations/0019_runner_relays.sql +28 -0
  255. package/dist/store/schema.d.ts +53 -4
  256. package/dist/store/schema.js +137 -6
  257. package/dist/store/seal.d.ts +20 -2
  258. package/dist/store/seal.js +24 -8
  259. package/dist/store/sqlite/connection.d.ts +2 -0
  260. package/dist/store/sqlite/connection.js +3 -1
  261. package/dist/store/sqlite/run-launch.d.ts +4 -1
  262. package/dist/store/sqlite/run-launch.js +12 -0
  263. package/dist/store/sqlite/run-patch.d.ts +0 -17
  264. package/dist/store/sqlite/run-patch.js +4 -33
  265. package/dist/store/sqlite/store.d.ts +3 -2
  266. package/dist/store/sqlite/store.js +360 -75
  267. package/dist/store/sqlite/worker.js +25 -0
  268. package/dist/store/store.d.ts +231 -39
  269. package/dist/store/store.js +25 -3
  270. package/dist/store-reads/analytics.js +11 -1
  271. package/dist/store-reads/learning.d.ts +131 -194
  272. package/dist/store-reads/learning.js +292 -301
  273. package/dist/store-reads/loop.d.ts +0 -1
  274. package/dist/store-reads/loop.js +1 -1
  275. package/dist/store-reads/stats.js +13 -3
  276. package/dist/store-view.d.ts +34 -2
  277. package/dist/store-view.js +108 -41
  278. package/dist/telemetry.d.ts +59 -0
  279. package/dist/telemetry.js +153 -0
  280. package/dist/template-eval.d.ts +3 -6
  281. package/dist/template-eval.js +22 -36
  282. package/dist/templates.d.ts +2 -1
  283. package/dist/templates.js +6 -3
  284. package/dist/timeline/index.d.ts +1 -1
  285. package/dist/timeline/index.js +1 -1
  286. package/dist/timeline/timeline.d.ts +5 -1
  287. package/dist/timeline/timeline.js +16 -0
  288. package/dist/tool-gate.d.ts +0 -7
  289. package/dist/tool-gate.js +1 -15
  290. package/dist/tool-keys-store.d.ts +1 -1
  291. package/dist/tool-keys.d.ts +6 -4
  292. package/dist/tool-keys.js +12 -6
  293. package/dist/tool-plane.d.ts +12 -12
  294. package/dist/tools.d.ts +13 -13
  295. package/dist/tools.js +29 -9
  296. package/dist/trigger-adapter.d.ts +3 -0
  297. package/dist/trigger-adapters.d.ts +4 -0
  298. package/dist/trigger-adapters.js +13 -0
  299. package/dist/trigger-composio.d.ts +58 -1
  300. package/dist/trigger-composio.js +160 -6
  301. package/dist/triggers.d.ts +64 -5
  302. package/dist/triggers.js +141 -17
  303. package/dist/validate.d.ts +11 -7
  304. package/dist/validate.js +9 -8
  305. package/dist/webhooks.d.ts +4 -1
  306. package/dist/webhooks.js +11 -4
  307. package/dist/workspace/agent-computer.d.ts +2 -0
  308. package/dist/workspace/agent-computer.js +2 -0
  309. package/dist/workspace/archil-claim.d.ts +7 -0
  310. package/dist/workspace/archil-claim.js +29 -0
  311. package/dist/workspace/archil.d.ts +9 -1
  312. package/dist/workspace/archil.js +165 -3
  313. package/dist/workspace/attach.d.ts +3 -1
  314. package/dist/workspace/attach.js +2 -0
  315. package/dist/workspace/attached-computers.d.ts +16 -5
  316. package/dist/workspace/attached-computers.js +25 -9
  317. package/dist/workspace/computer-deps.js +1 -1
  318. package/dist/workspace/computer-for-claim.d.ts +22 -8
  319. package/dist/workspace/computer-for-claim.js +339 -46
  320. package/dist/workspace/computer-hold.js +2 -1
  321. package/dist/workspace/computer-kept.d.ts +11 -1
  322. package/dist/workspace/computer-kept.js +24 -3
  323. package/dist/workspace/resolve-computer.d.ts +41 -21
  324. package/dist/workspace/resolve-computer.js +172 -64
  325. package/dist/workspace/revision-skills.d.ts +1 -2
  326. package/dist/workspace/revision-skills.js +3 -4
  327. package/dist/workspace/run-workspace.d.ts +1 -1
  328. package/dist/workspace/run-workspace.js +5 -0
  329. package/dist/workspace/synced-folder.d.ts +85 -0
  330. package/dist/workspace/synced-folder.js +230 -0
  331. package/dist/workspace/workspace.d.ts +4 -0
  332. package/launcher/agentrun-serve +1 -1
  333. package/package.json +10 -9
  334. package/templates/README.md +7 -7
  335. package/templates/inbound-lead/agent.md +3 -3
  336. package/templates/inbound-lead/evals/learned-correction/case.yaml +3 -3
  337. package/templates/inbound-lead/evals/learned-correction/fixtures/corrections.md +1 -0
  338. package/templates/inbound-lead/evals/learned-correction/graders/applies-correction.md +22 -0
  339. package/templates/inbound-lead/evals/learned-correction/graders/learned-origin.md +1 -1
  340. package/templates/inbound-lead/evals/learned-correction/prompt.md +1 -1
  341. package/templates/inbound-lead/lead.schema.json +6 -5
  342. package/templates/inbound-lead/rules/RUBRIC.md +4 -4
  343. package/templates/inbound-lead/skills/lead/SKILL.md +4 -4
  344. package/templates/market-monitor/evals/learned-preference/case.yaml +4 -4
  345. package/templates/market-monitor/evals/learned-preference/fixtures/corrections.md +1 -0
  346. package/templates/market-monitor/evals/learned-preference/graders/omits-junior-post.md +3 -3
  347. package/templates/market-monitor/evals/learned-preference/prompt.md +1 -1
  348. package/templates/release-notes/evals/learned-style/case.yaml +4 -4
  349. package/templates/release-notes/evals/learned-style/fixtures/corrections.md +1 -0
  350. package/templates/release-notes/evals/learned-style/graders/same-changes.md +2 -2
  351. package/templates/release-notes/evals/learned-style/prompt.md +1 -1
  352. package/dist/console/assets/index-BrcDSBD8.css +0 -1
  353. package/dist/console/assets/index-Bz6IWncl.js +0 -711
  354. package/dist/learn/grade.d.ts +0 -138
  355. package/dist/learn/grade.js +0 -682
  356. package/dist/learn/notes.d.ts +0 -81
  357. package/dist/learn/notes.js +0 -178
  358. package/dist/learn/reflect.d.ts +0 -77
  359. package/dist/learn/reflect.js +0 -228
  360. package/dist/learn/relevance.d.ts +0 -24
  361. package/dist/learn/relevance.js +0 -91
  362. package/dist/learn/seed-check.d.ts +0 -74
  363. package/dist/learn/seed-check.js +0 -103
  364. package/dist/learning-batch.d.ts +0 -291
  365. package/dist/learning-batch.js +0 -600
  366. package/dist/learning-evals.d.ts +0 -33
  367. package/dist/learning-evals.js +0 -98
  368. package/dist/native/context.d.ts +0 -6
  369. package/dist/native/context.js +0 -22
  370. package/dist/propose.d.ts +0 -52
  371. package/dist/propose.js +0 -224
  372. package/dist/runner/learning-ask.d.ts +0 -47
  373. package/dist/runner/learning-ask.js +0 -128
  374. package/dist/runner/learning-dispatch.d.ts +0 -45
  375. package/dist/runner/learning-dispatch.js +0 -99
  376. package/dist/runner/learning-factory.d.ts +0 -16
  377. package/dist/runner/learning-factory.js +0 -50
  378. package/dist/runner/learning-loop.d.ts +0 -273
  379. package/dist/runner/learning-loop.js +0 -521
  380. package/dist/usage.d.ts +0 -11
  381. package/dist/usage.js +0 -11
  382. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/MEMORY.md +0 -1
  383. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/no-agencies.md +0 -14
  384. package/templates/inbound-lead/evals/learned-correction/graders/applies-note.md +0 -21
  385. package/templates/market-monitor/evals/learned-preference/fixtures/learning/MEMORY.md +0 -1
  386. package/templates/market-monitor/evals/learned-preference/fixtures/learning/skip-junior-job-posts.md +0 -8
  387. package/templates/release-notes/evals/learned-style/fixtures/learning/MEMORY.md +0 -1
  388. package/templates/release-notes/evals/learned-style/fixtures/learning/say-agent-not-bot.md +0 -13
@@ -1,11 +1,11 @@
1
1
  // The shadow audit of a serving learned workflow, at a run's claim and its end (learn/audit.ts keeps the ledger).
2
- // startAudit (withAudit: at the served run's end, before the end commits): a run the promoted workflow served first is
3
- // drawn at 1 in k (auditDrawn, from its id), and a drawn one appends a `started` line to audits.jsonl (with the
2
+ // startAudit (withAudit: at the served run's end, before the end commits): a run the promoted workflow answered (path
3
+ // `workflow`; a run it handed back is never drawn and never counts as served) is drawn at 1 in k (auditDrawn, from its id), and a drawn one appends a `started` line to audits.jsonl (with the
4
4
  // promotion serving it and the answer it audits, `turn`: the id of the run's newest answer row now), then starts its
5
- // shadow run: the same agent and revision on the same input with learning off (no seed, no look-back), marked as the
5
+ // shadow run: the same agent and revision on the same input with learning off (no seed, no compile turn), marked as the
6
6
  // audit of the served run (launch.audit), no row of the run list, billed to the agent as learning cost. Its cap is
7
7
  // what the served run's cap left (the two together never pass it; the served run's later gates count it beside them,
8
- // learning-end.ts besideOf); none left, no shadow (`skipped: "no allowance"`). A shadow never waits on a person: where
8
+ // learning-end.ts shadowBesideOf); none left, no shadow (`skipped: "no allowance"`). A shadow never waits on a person: where
9
9
  // it would park (an approval its owner's setting asks, a raise of that cap, a question) it ends at once in
10
10
  // AUDIT_PARKED's words (AdapterRunSpec.neverWaits, the gate untouched: nothing is let through, no one is asked to
11
11
  // answer), and its row is `parked`. Only a run started over the API (no trigger: its context is never rebuilt for a
@@ -25,10 +25,9 @@
25
25
  import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
26
26
  import { overCapMessage } from "@parcha/agentrun-kernel/kernel/cap";
27
27
  import { auditLoopOf } from "../learning-setting.js";
28
- import { RUN_CAP_MIN_USD } from "../learning-batch.js";
29
- import { SERVED_PATHS } from "../learning-cost.js";
30
28
  import { launchBody, readLaunchBody } from "../run-launch.js";
31
29
  import { runEntries } from "../store-reads/journal.js";
30
+ import { runPathOverStore } from "../store-reads/learning.js";
32
31
  import { FINAL_STATES, StoreConstraint } from "../store/store.js";
33
32
  import { openRunMcpAuth, sealRunMcpAuth } from "../store/seal.js";
34
33
  import { isObject } from "../validate.js";
@@ -40,13 +39,19 @@ import { excerptShown } from "../learn/secrets.js";
40
39
  import { auditRoomOf } from "./learning-end.js";
41
40
  import { answerOf, answerRowOf } from "./promotion.js";
42
41
  import { servedOf } from "./workflow-first.js";
42
+ import { held, heldEndOfRun } from "./held.js";
43
+ /** The least cap a run can take a step under (cap.ts: the next step needs at least a cent): a shadow with less left
44
+ * is never started. */
45
+ const RUN_CAP_MIN_USD = 0.01;
43
46
  /** The words a second hold within stop_window leaves on its record: what the owner reads. */
44
47
  export const STOPPED_TWICE = "stopped: the workflow disagreed with the agent twice";
45
48
  const iso = (ms) => new Date(ms).toISOString();
46
49
  const ended = (row) => row !== undefined && FINAL_STATES.includes(row.state) && row.runner === null;
47
50
  const inputText = (input) => (typeof input === "string" ? input : JSON.stringify(input) ?? "");
48
51
  const metricsOf = (row) => (isObject(row?.metrics) ? row.metrics : {});
49
- const served = (row) => SERVED_PATHS.includes(metricsOf(row).path);
52
+ /** A run the audit counts as served: one its learned workflow answered (path `workflow`). One it handed back (path
53
+ * `handed_back`) is the agent's answer: never drawn, never counted. */
54
+ const served = (row) => metricsOf(row).path === "workflow";
50
55
  /** A line's `turn` (AuditRow.turn): the answer row's id, none when the run had no answer. */
51
56
  const turnOf = (id) => (id === undefined ? {} : { turn: id });
52
57
  /** The answer an audit row is of: its `turn`'s, read from the run by that row's id; none when it names none. */
@@ -75,26 +80,34 @@ export const AUDIT_PARKED = "the audit ended where it would wait on a person";
75
80
  /** The served run's end-of-run step `step` (none for a run that only uses what its agent learned), its audit started
76
81
  * after it (startAudit) on an end the run reached on its own (a cut run starts none): the learned workflow that served
77
82
  * it, read from its journal whatever claim it served on (servedOf), and, for a capped run, what its cap leaves once this
78
- * end is counted (auditRoomOf: its job, its look-back, the grades beside it and the one this end queues). A run that
83
+ * end is counted (auditRoomOf: its job, its follow-ups, its compile turns). A run that
79
84
  * could never be audited (no learning start, or not auditableLaunch) keeps `step` as it is. */
80
85
  export function withAudit(step, deps) {
81
86
  if (!deps.launch.learning || !auditableLaunch(deps.launch))
82
87
  return step;
88
+ // In a run's own process the whole step is its runner's (held.ts endOfRun), the audit's start with it: it reads the
89
+ // run's journal and writes the ledger and a run, which no run's process may.
90
+ if (held())
91
+ return heldEndOfRun();
83
92
  return async (input) => {
84
93
  const rows = step ? await step(input) : undefined;
85
94
  if (input.cut === undefined)
86
- await auditAtEnd(deps, input.runId, rows?.also ?? []);
95
+ await auditAtEnd(deps, input.runId);
87
96
  return rows;
88
97
  };
89
98
  }
90
- async function auditAtEnd(deps, runId, queued) {
99
+ /** Only a run the workflow answered is drawn: its path `workflow` by the one classifier its record keeps
100
+ * (store-reads/learning.ts runPathOverStore, with the run's `redact`), never one it handed back. */
101
+ async function auditAtEnd(deps, runId) {
91
102
  try {
92
103
  const entries = await runEntries(deps.store, runId, BACKGROUND_CONTEXT);
93
104
  const served = servedOf(entries);
94
105
  if (!served?.digest)
95
106
  return;
107
+ if ((await runPathOverStore(deps.store, runId, deps.launch.learning, deps.redact)).path !== "workflow")
108
+ return;
96
109
  const capUsd = deps.launch.maxCostUsd;
97
- await startAudit({ ...deps, runId, ...(capUsd !== undefined ? { room: () => auditRoomOf(deps.store, runId, capUsd, entries, queued) } : {}) }, { name: served.workflow, digest: served.digest });
110
+ await startAudit({ ...deps, runId, ...(capUsd !== undefined ? { room: () => auditRoomOf(deps.store, runId, capUsd, entries) } : {}) }, { name: served.workflow, digest: served.digest });
98
111
  }
99
112
  catch (error) {
100
113
  deps.log(`workflow-first: the audit of this run did not start (${error?.message ?? error})`);
@@ -112,6 +125,11 @@ export async function startAudit(deps, serve) {
112
125
  const { store, agentId, runId, launch, log } = deps;
113
126
  if (!serve || serve.replay || launch.replay || launch.audit || !launch.learning || !auditableLaunch(launch))
114
127
  return;
128
+ // A shadow is a run of its own the runner starts, on no caller's key: skipped where the served run brought its own key
129
+ // or the server holds none (claimSpendOff).
130
+ const off = deps.spendOff?.("A shadow audit");
131
+ if (off)
132
+ return void log(`workflow-first: ${off}`);
115
133
  try {
116
134
  // Read after the claim pinned the revision's MCP tools (mcp-claim.ts pinFirst): the pin a revision saved without one
117
135
  // takes at its first run is what its tools are classed by.
@@ -182,8 +200,9 @@ export async function startAudit(deps, serve) {
182
200
  }
183
201
  /** The per_run MCP credentials run `from` was handed, sealed for shadow run `id` alone (as a replay run's are). */
184
202
  async function handedTo(deps, from, id) {
203
+ // A run's own process holds no seal key: its runner seals them again, from its own run alone (held.ts handOn).
185
204
  if (!deps.sealKey)
186
- return undefined;
205
+ return held()?.handOn(from, id);
187
206
  const sealed = await deps.store.runs.launch(from).then((one) => one.sealed ?? null, () => null);
188
207
  const handed = sealed === null ? undefined : openRunMcpAuth(deps.sealKey, from, sealed);
189
208
  return handed && Object.keys(handed).length ? sealRunMcpAuth(deps.sealKey, id, handed) : undefined;
@@ -9,6 +9,7 @@ export declare function besideRunnerOptions(serve: {
9
9
  drainTimeout?: string;
10
10
  }, env: NodeJS.ProcessEnv): {
11
11
  loops: number;
12
+ loopsTold: boolean;
12
13
  drainMs: number;
13
14
  };
14
15
  /** What the CLI's stop waits on (stop-signals.ts StopDeps.stopping) with a runner beside the server: the server's runs
@@ -29,12 +30,15 @@ export declare function besideStopping(server: {
29
30
  * too (workspace/computer-janitor.ts), as the CLI's start asks. `hub`: the server's AttachHub (Serve.attachHub), which
30
31
  * holds each paired computer's socket in this process, so a run here takes one. `dial`: the server's (Serve.dial), which
31
32
  * a remote provider's create takes, so a machine that dials the server lands in this process's hub. Returns the runner
32
- * and a close() that stops both and closes its store. */
33
- export declare function runnerBeside(dataDir: string, { loops, drainMs, env, janitor, hub, dial, mcpLoopback, mcpOwn, evalFakes, harnessInstall, onLine }?: {
33
+ * and a close() that stops both and closes its store. `loopsTold`: `loops` was given (besideRunnerOptions), so on uid
34
+ * isolation a number past the helper's slots is refused rather than cut. */
35
+ export declare function runnerBeside(dataDir: string, { loops, loopsTold, drainMs, env, janitor, ownProcesses, hub, dial, mcpLoopback, mcpOwn, evalFakes, harnessInstall, onLine, onRunEnded }?: {
34
36
  loops?: number;
37
+ loopsTold?: boolean;
35
38
  drainMs?: number;
36
39
  env?: NodeJS.ProcessEnv;
37
40
  janitor?: boolean;
41
+ ownProcesses?: boolean;
38
42
  hub?: import("@parcha/agentrun-computer").AttachHub;
39
43
  dial?: import("@parcha/agentrun-computer").HostedDial;
40
44
  mcpLoopback?: boolean;
@@ -42,6 +46,7 @@ export declare function runnerBeside(dataDir: string, { loops, drainMs, env, jan
42
46
  evalFakes?: (agentId: string) => import("./adapter-run-spec.js").EvalFakes | undefined;
43
47
  harnessInstall?: import("../harness-install.js").HarnessInstaller;
44
48
  onLine?: (line: string) => void;
49
+ onRunEnded?: (status: string) => void;
45
50
  }): Promise<{
46
51
  runner: Awaited<ReturnType<typeof import("./runner.js")["startRunner"]>>;
47
52
  close: () => Promise<void>;
@@ -14,7 +14,8 @@ export function besideRunnerOptions(serve, env) {
14
14
  const drainMs = durationMs(drainTimeout);
15
15
  if (drainMs === undefined)
16
16
  throw new Error(`the drain timeout ${drainTimeout} is not a duration such as 8s`);
17
- return { loops: maxRunsOf(env, serve.maxRuns), drainMs };
17
+ // `loopsTold`: the number was given (--max-runs, AGENTRUN_MAX_RUNS), not the host's memory's count.
18
+ return { loops: maxRunsOf(env, serve.maxRuns), loopsTold: serve.maxRuns !== undefined || Boolean(env.AGENTRUN_MAX_RUNS), drainMs };
18
19
  }
19
20
  /** What the CLI's stop waits on (stop-signals.ts StopDeps.stopping) with a runner beside the server: the server's runs
20
21
  * and the runs the runner holds, and the longer of the server's bound and the runner's whole close (its drain, then
@@ -30,8 +31,9 @@ export function besideStopping(server, held, drainMs) {
30
31
  * too (workspace/computer-janitor.ts), as the CLI's start asks. `hub`: the server's AttachHub (Serve.attachHub), which
31
32
  * holds each paired computer's socket in this process, so a run here takes one. `dial`: the server's (Serve.dial), which
32
33
  * a remote provider's create takes, so a machine that dials the server lands in this process's hub. Returns the runner
33
- * and a close() that stops both and closes its store. */
34
- export async function runnerBeside(dataDir, { loops, drainMs, env, janitor, hub, dial, mcpLoopback, mcpOwn, evalFakes, harnessInstall, onLine } = {}) {
34
+ * and a close() that stops both and closes its store. `loopsTold`: `loops` was given (besideRunnerOptions), so on uid
35
+ * isolation a number past the helper's slots is refused rather than cut. */
36
+ export async function runnerBeside(dataDir, { loops, loopsTold, drainMs, env, janitor, ownProcesses, hub, dial, mcpLoopback, mcpOwn, evalFakes, harnessInstall, onLine, onRunEnded } = {}) {
35
37
  const { runnerName, startRunner } = await import("./runner.js");
36
38
  const { dispatchRunner } = await import("./harness-loops.js");
37
39
  const { routeOf } = await import("./model-route.js");
@@ -69,9 +71,34 @@ export async function runnerBeside(dataDir, { loops, drainMs, env, janitor, hub,
69
71
  // email), by the agent each of its runs is of; only the eval, in its own process, hands them. `harnessInstall`: what a
70
72
  // claim's first-use install installs with (the eval's sharedInstaller, which the server beside it takes too).
71
73
  const { installForClaim } = await import("../harness-install.js");
72
- const createLoop = dispatchRunner({ env: runs, data: dataDir, journal: (claim) => store.journal(claim), store, sealKey: seal.key, routeOf, computerForClaim: computers.computerForClaim, computerDeps: computers.computerDeps, ...(mcpLoopback ? { mcpLoopback: true } : {}), ...(mcpOwn ? { mcpOwn } : {}), ...(evalFakes ? { evalFakes } : {}), ...(harnessInstall ? { installForClaim: (entry, dir, pins, given) => installForClaim(entry, dir, pins, { ...given, install: harnessInstall }) } : {}), log: (line) => console.error(`agentrun-serve runner: ${line}`) });
73
- // It claims by the host's memory as well as by --max-runs (memory-claim.ts): a heavy harness's run waits queued for room.
74
- const { runnerMemory } = await import("./memory-claim.js");
74
+ const deps = { env: runs, data: dataDir, journal: (claim) => store.journal(claim), store, sealKey: seal.key, routeOf, computerForClaim: computers.computerForClaim, computerDeps: computers.computerDeps, ...(mcpLoopback ? { mcpLoopback: true } : {}), ...(mcpOwn ? { mcpOwn } : {}), ...(evalFakes ? { evalFakes } : {}), ...(harnessInstall ? { installForClaim: ((entry, dir, pins, given) => installForClaim(entry, dir, pins, { ...given, install: harnessInstall })) } : {}), log: (line) => console.error(`agentrun-serve runner: ${line}`) };
75
+ // `ownProcesses`: each run a process of its own (run-child-host.ts), as a user of its own where the image's helper can
76
+ // switch, as the CLI's start runs them; a helper that cannot is refused in words naming the setting. The template eval's
77
+ // runner (its fakes are functions of this process) runs its runs in this one.
78
+ let isolation;
79
+ let held = loops;
80
+ if (ownProcesses && !evalFakes && !harnessInstall) {
81
+ const { isolationLoops, runIsolationOf } = await import("./run-child-host.js");
82
+ try {
83
+ isolation = await runIsolationOf(process.env, loopsTold ? loops : undefined, deps.log);
84
+ }
85
+ catch (error) {
86
+ await store.close();
87
+ seal.release();
88
+ throw error;
89
+ }
90
+ if (loops !== undefined && isolation.kind === "uid" && loops > isolation.slots)
91
+ deps.log(`each run runs as a user of its own, and the helper reaches ${isolation.slots} slots from slot ${isolation.first}: this runner holds at most ${isolation.slots} runs at once`);
92
+ if (loops !== undefined)
93
+ held = isolationLoops(isolation, loops);
94
+ else if (isolation.kind === "uid")
95
+ held = isolation.slots;
96
+ }
97
+ const { childLoops } = await import("./run-child-host.js");
98
+ const createLoop = isolation ? childLoops({ deps, isolation, log: deps.log }) : dispatchRunner(deps);
99
+ // It claims by the host's memory as well as by --max-runs (memory-claim.ts): a heavy harness's run waits queued for room,
100
+ // each run's own process weighed with it.
101
+ const { runnerMemory, RUN_PROCESS_MIB } = await import("./memory-claim.js");
75
102
  // Before it claims: the scratch folders of runs that ended after their runner died (scratch-sweep.ts).
76
103
  const { sweepEndedScratch } = await import("./scratch-sweep.js");
77
104
  await sweepEndedScratch(store, dataDir, { log: (line) => console.error(`agentrun-serve runner: ${line}`) });
@@ -88,11 +115,14 @@ export async function runnerBeside(dataDir, { loops, drainMs, env, janitor, hub,
88
115
  const line = runLine(event);
89
116
  if (line)
90
117
  (onLine ?? console.log)(line);
118
+ // The server's anonymous counts (telemetry.ts) hear a run's end: its state alone, never which run or agent.
119
+ if (event.type === "ended")
120
+ onRunEnded?.(event.status);
91
121
  };
92
122
  // A run's end lets go of what its earlier claims' computers left, with the same computers (run-end-computers.ts).
93
123
  const { runEndComputers } = await import("../workspace/run-end-computers.js");
94
124
  const runEnded = runEndComputers({ store, computers: computers.computerDeps, log: (line) => console.error(`agentrun-serve runner: ${line}`) });
95
- const runner = startRunner({ store, name, createLoop, memory: runnerMemory(store), onEvent: said, runEnded, ...(loops !== undefined ? { loops } : {}), ...(drainMs !== undefined ? { drainMs } : {}) });
125
+ const runner = startRunner({ store, name, createLoop, memory: runnerMemory(store, undefined, runs, isolation ? RUN_PROCESS_MIB : 0), onEvent: said, runEnded, ...(held !== undefined ? { loops: held } : {}), ...(drainMs !== undefined ? { drainMs } : {}) });
96
126
  // The computer janitor over the same computers, when the start asks for it: never under --faux. Its lines are the
97
127
  // runner's, on stderr (a stderr whose reader closed loses them, never the process: cli.ts).
98
128
  const { startJanitor } = await import("../workspace/computer-janitor.js");
@@ -109,9 +139,11 @@ export async function runnerBeside(dataDir, { loops, drainMs, env, janitor, hub,
109
139
  export async function serveWithRunner(options) {
110
140
  const { fauxEnvOf, startServe } = await import("../server.js");
111
141
  const { besideRunEnv } = await import("./main.js");
112
- const { loops, drainMs } = besideRunnerOptions(options, process.env);
142
+ const { loops, loopsTold, drainMs } = besideRunnerOptions(options, process.env);
113
143
  const serve = await startServe(options);
114
- const beside = await runnerBeside(options.data, { loops, drainMs, env: besideRunEnv(process.env, fauxEnvOf(options)), hub: serve.attachHub, dial: serve.dial, mcpLoopback: serve.localInstall, mcpOwn: serve.own, ...(options.log ? { onLine: options.log } : {}) }).catch(async (error) => {
144
+ // Each run a process of its own, as a user of its own where the image's helper can switch, as the CLI's start runs
145
+ // them (cli.ts): the isolation the server's capabilities say (runIsolationOf over the same helper) is the runner's.
146
+ const beside = await runnerBeside(options.data, { loops, loopsTold, drainMs, env: besideRunEnv(process.env, fauxEnvOf(options)), ownProcesses: true, hub: serve.attachHub, dial: serve.dial, mcpLoopback: serve.localInstall, mcpOwn: serve.own, ...(options.log ? { onLine: options.log } : {}) }).catch(async (error) => {
115
147
  await serve.close();
116
148
  throw error;
117
149
  });
@@ -3,6 +3,9 @@ import { type Tier } from "../model.js";
3
3
  import type { RunLaunch } from "../run-launch.js";
4
4
  import type { SealedKeys } from "../store/store.js";
5
5
  import type { Revision } from "../agent-rows.js";
6
+ import type { HeldRedact } from "./redact.js";
7
+ import { type HeldModelKey } from "../run-model-key.js";
8
+ import { type RunModelKey } from "../store/seal.js";
6
9
  /** What claimSecrets reads the keys from: the one store (its sealed keys) and the runner's seal key, and the runner's base
7
10
  * environment (its own, with no server secret in it: those are the store's). */
8
11
  export interface ClaimSecretsDeps {
@@ -46,6 +49,68 @@ export declare const MODEL_KEY_NAMES: readonly string[];
46
49
  * and a provider the runner holds any key of gets none of its saved ones.
47
50
  * The keys added are never enumerable either: a spread of the result, or a spawn given it, carries none of them. */
48
51
  export declare function withModelKeys(env: NodeJS.ProcessEnv, opened: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
49
- /** Every model key the server holds, for a model route with no revision to scope it by (a learning run's grade): each
52
+ /** Every model key the server holds, for a model route with no revision to scope it by (an SDK run's compile turn): each
50
53
  * by its name from `deps.env` first, then the store's sealed value, as claimSecrets reads them. */
51
54
  export declare function savedModelKeys(deps: ClaimSecretsDeps): Promise<NodeJS.ProcessEnv>;
55
+ /** A run's own model key, held by its claim (run-model-key.ts): `fresh()` opens it from the run's launch again (a message's
56
+ * new key replaces it, or a run that started on the server's key takes its first); `everHeld()`, whether the claim ever
57
+ * held one, so spend after the turn never falls to the server's key for a run that brought its own. */
58
+ export interface ClaimModelKey extends HeldModelKey {
59
+ fresh(): Promise<void>;
60
+ everHeld(): boolean;
61
+ /** Calls `listener` with the key now and with each new one fresh() opens: the run's redactors hold every key it held. */
62
+ watch(listener: (key: string) => void): void;
63
+ }
64
+ /** Run `runId`'s own model key as its launch's sealed column holds it, opened under `deps.sealKey`: none for a launch that
65
+ * holds none (or no launch row: a test's, an import's). Throws, in words, where the launch does not open. */
66
+ export declare function openRunModelKey(deps: {
67
+ readonly store: {
68
+ readonly runs: {
69
+ launch(runId: string): Promise<{
70
+ readonly sealed: Uint8Array | null;
71
+ }>;
72
+ };
73
+ };
74
+ readonly sealKey: Buffer | undefined;
75
+ }, runId: string): Promise<RunModelKey | undefined>;
76
+ /**
77
+ * The run's own model key for claim `runId`, opened from its launch's sealed column under the runner's seal key (the
78
+ * server sealed it there with mcp_auth, store/seal.ts). Undefined only where the server has no gateway and the run holds
79
+ * no key: a claim on a gateway server holds one even before the run's first key, so a message that hands it one reaches
80
+ * the next request (fresh()). Read at every claim, a reclaim after a runner's crash among them, so a run's key outlives no
81
+ * process but its own launch row. Throws, in words, when its launch does not open, or when the key was handed for another
82
+ * gateway than AGENT_MODEL_BASE_URL's now: it is never sent where it was not handed for.
83
+ */
84
+ export declare function claimModelKey(deps: {
85
+ readonly store: {
86
+ readonly runs: {
87
+ launch(runId: string): Promise<{
88
+ readonly sealed: Uint8Array | null;
89
+ }>;
90
+ };
91
+ };
92
+ readonly sealKey: Buffer | undefined;
93
+ readonly env?: NodeJS.ProcessEnv;
94
+ }, runId: string): Promise<ClaimModelKey | undefined>;
95
+ /** The run's own model key held by `redact` (one of the run's redactors), the key now and each one a message hands it. */
96
+ export declare function holdRunKey(redact: HeldRedact, runKey: ClaimModelKey): void;
97
+ /** Whether the claim opened a key of the run's own at its start: its routes then carry that key alone, its tools hold no
98
+ * model key, and no saved key is read for it. */
99
+ export declare const keyedAtClaim: (runKey: ClaimModelKey | undefined) => boolean;
100
+ /** Whether the server has a gateway (AGENT_MODEL_BASE_URL) and holds no key for it, in the runner's environment or saved:
101
+ * claimSpendOff's spend with no key to pay it. */
102
+ export declare function serverKeyless(deps: ClaimSecretsDeps): Promise<boolean>;
103
+ /** Why the claim's model spend after the run's turn, or beside it on runs the runner starts (a shadow audit, a replay
104
+ * check), is skipped: `what` the work, asked when that spend would start. A run that brought
105
+ * its own key, at its start or with a message since (everHeld): that key is erased with its turn's end and the server's
106
+ * never pays for it (runKeyEndedFor). Else, a server that holds no gateway key (the runner's environment or
107
+ * Settings → Model keys) has none to spend (noServerKeyWords). Undefined where it runs. */
108
+ export declare function claimSpendOff(deps: ClaimSecretsDeps, runKey: ClaimModelKey | undefined): Promise<(what: string) => string | undefined>;
109
+ /** The env a claimed run's model routes read: the run's own key alone where the claim opened one (withRunModelKey); else
110
+ * the runner's environment with the keys `opened` for it (withModelKeys), whose gateway variables read the run's key
111
+ * once a message hands it one (withRunModelKeyLater). */
112
+ export declare function claimModelEnv(env: NodeJS.ProcessEnv, opened: NodeJS.ProcessEnv, runKey: ClaimModelKey | undefined): NodeJS.ProcessEnv;
113
+ /** `toolEnv` with no model key in it, for a run that holds its own: a tool's environment never carries a model key the
114
+ * run's model routes do not send. The decision providers' keys stay (DECISION_KEY_NAMES): a keyed workflow's judge,
115
+ * pick, sift, route and ask nodes run on them as any run's do. */
116
+ export declare function withoutModelKeys(toolEnv: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
@@ -6,13 +6,17 @@
6
6
  // providers they name, read once the claim's seed lay chose them (learnedDecisionKeys). So a run that names no monid never gets the monid key, and two runs on
7
7
  // one runner never see each other's keys: each claim reads its own, into its own environment. (MCP credentials are the
8
8
  // other per-claim secret; they come through mcp-claim.ts, which already opens them from the store under the claim.)
9
- import { decisionKeysFor, providersNamed } from "../decisions.js";
9
+ import { decisionKeysFor, JEV_KEY_ENV, JEV_OPENROUTER_KEY_ENV, PARALLEL_KEY_ENV, providersNamed } from "../decisions.js";
10
10
  import { GATEWAY_KEY_ENV, hostModel, modelKeys, PROVIDER_KEYS, tierModelOf, TIERS, workflowNodes } from "../model.js";
11
11
  import { keysFor, needsOf, RUN_SECRETS } from "../run-secrets.js";
12
12
  import { DECISION_KEYS, withoutSecrets } from "../secrets.js";
13
13
  import { getSealedKey } from "../server-keys.js";
14
14
  import { PLAN_PROVIDER, planOf, planReaderOf, withPlan } from "../chatgpt-plan.js";
15
15
  import { handsParallel } from "../tool-plane.js";
16
+ import { held, held as runnerSeam } from "./held.js";
17
+ import { noServerKeyWords, runKeyEndedFor, runModelOrigin, withRunModelKey, withRunModelKeyLater } from "../run-model-key.js";
18
+ import { openRunLaunch } from "../store/seal.js";
19
+ import { StoreLaunchMissing } from "../store/store.js";
16
20
  /** Every server secret a run might be handed (runs.ts RUN_SECRETS is the tool and model keys; the decision keys go beside
17
21
  * them). Read from the store into a working environment, then scoped to the run below. */
18
22
  const CANDIDATES = [...new Set([...RUN_SECRETS, ...DECISION_KEYS])];
@@ -100,6 +104,10 @@ export async function learnedDecisionKeys(deps, workflows, allowed) {
100
104
  export async function hostDecisionKeys(deps, providers) {
101
105
  if (!providers.length)
102
106
  return {};
107
+ // A run's own process holds no seal key: its runner reads them (held.ts).
108
+ const at = deps.sealKey ? undefined : held();
109
+ if (at)
110
+ return at.decisionKeys(providers);
103
111
  return decisionKeysFor(await workingEnv(deps), { providers, parallelWeb: false });
104
112
  }
105
113
  /** The model keys the host's models of `tiers` need (model.ts modelKeys: each one's provider's, or the gateway's), read as
@@ -109,6 +117,12 @@ export async function hostDecisionKeys(deps, providers) {
109
117
  export async function tierModelKeys(deps, tiers) {
110
118
  if (!tiers.length)
111
119
  return {};
120
+ // A run's own process holds no seal key: its runner reads them, the plan's reader with them (held.ts).
121
+ const at = deps.sealKey ? undefined : held();
122
+ if (at) {
123
+ const given = await at.tierModelKeys(tiers);
124
+ return given.plan ? withPlan({ ...given.keys }, given.plan) : { ...given.keys };
125
+ }
112
126
  const working = await workingEnv(deps);
113
127
  const out = {};
114
128
  for (const name of modelKeys(working, undefined, tiers))
@@ -154,10 +168,12 @@ export function withModelKeys(env, opened) {
154
168
  // The claim's ChatGPT sign-in (claimSecrets), else the runner's own.
155
169
  return withPlan(out, planOf(opened) ?? planOf(env));
156
170
  }
157
- /** Every model key the server holds, for a model route with no revision to scope it by (a learning run's grade): each
171
+ /** Every model key the server holds, for a model route with no revision to scope it by (an SDK run's compile turn): each
158
172
  * by its name from `deps.env` first, then the store's sealed value, as claimSecrets reads them. */
159
173
  export async function savedModelKeys(deps) {
160
- const out = {};
174
+ // A run's own process holds no seal key: what its runner hands a claim whose routes read them (held.ts savedModelKeys).
175
+ const at = deps.sealKey ? undefined : held();
176
+ const out = at ? { ...(await at.savedModelKeys()) } : {};
161
177
  for (const name of MODEL_KEY_NAMES) {
162
178
  const value = deps.env?.[name] || (deps.sealKey ? await getSealedKey(deps.store, deps.sealKey, name) : undefined);
163
179
  if (value !== undefined)
@@ -165,3 +181,120 @@ export async function savedModelKeys(deps) {
165
181
  }
166
182
  return out;
167
183
  }
184
+ /** How many times fresh() reads a run's launch before it gives up, and the first wait between reads (doubled each time). */
185
+ const FRESH_TRIES = 3;
186
+ const FRESH_WAIT_MS = 50;
187
+ /** Run `runId`'s own model key as its launch's sealed column holds it, opened under `deps.sealKey`: none for a launch that
188
+ * holds none (or no launch row: a test's, an import's). Throws, in words, where the launch does not open. */
189
+ export async function openRunModelKey(deps, runId) {
190
+ const sealed = await deps.store.runs.launch(runId).then((launch) => launch.sealed, (error) => {
191
+ if (error instanceof StoreLaunchMissing)
192
+ return null;
193
+ throw error;
194
+ });
195
+ if (sealed === null)
196
+ return undefined;
197
+ const opened = deps.sealKey ? openRunLaunch(deps.sealKey, runId, sealed) : undefined;
198
+ if (!opened)
199
+ throw new Error("This run's launch secrets do not open with this runner's seal key: its model key is not sent.");
200
+ return opened.model;
201
+ }
202
+ /**
203
+ * The run's own model key for claim `runId`, opened from its launch's sealed column under the runner's seal key (the
204
+ * server sealed it there with mcp_auth, store/seal.ts). Undefined only where the server has no gateway and the run holds
205
+ * no key: a claim on a gateway server holds one even before the run's first key, so a message that hands it one reaches
206
+ * the next request (fresh()). Read at every claim, a reclaim after a runner's crash among them, so a run's key outlives no
207
+ * process but its own launch row. Throws, in words, when its launch does not open, or when the key was handed for another
208
+ * gateway than AGENT_MODEL_BASE_URL's now: it is never sent where it was not handed for.
209
+ */
210
+ export async function claimModelKey(deps, runId) {
211
+ const origin = runModelOrigin(deps.env ?? {});
212
+ // A run's own process holds no seal key: its runner opens the claim's own launch, whatever run id this one names (held.ts).
213
+ const at = deps.sealKey ? undefined : runnerSeam();
214
+ const open = () => (at ? at.runModelKey() : openRunModelKey(deps, runId));
215
+ let held = await open();
216
+ if (held && held.origin !== origin)
217
+ throw new Error(`This run's model key was handed for the gateway at ${held.origin}, and AGENT_MODEL_BASE_URL names ${origin ?? "none"} here now: the key is never sent anywhere else.`);
218
+ if (!origin)
219
+ return undefined;
220
+ let ever = held !== undefined;
221
+ const listeners = [];
222
+ return {
223
+ origin,
224
+ key: () => held?.key,
225
+ everHeld: () => ever,
226
+ // A message that hands no key keeps the run's (the server's write keeps it), so none here is a run whose launch holds
227
+ // none (yet, or no longer). A key for another origin is never taken. Fail-closed: a launch that cannot be read is read
228
+ // again, FRESH_TRIES in all with a growing wait, and then fresh() throws, so the command that asked is not handed to
229
+ // the session (the runner leaves its row untaken and delivers it again at its next poll). A message's key is never
230
+ // replaced by the key the claim held before it, nor by the server's.
231
+ fresh: async () => {
232
+ let now;
233
+ for (let attempt = 1;; attempt++) {
234
+ try {
235
+ now = await open();
236
+ break;
237
+ }
238
+ catch (error) {
239
+ if (attempt >= FRESH_TRIES)
240
+ throw new Error(`the run's model key could not be read from its launch (${FRESH_TRIES} tries): the message is held until it can be, never sent on another key: ${error?.message ?? error}`);
241
+ await new Promise((resolve) => setTimeout(resolve, FRESH_WAIT_MS * 2 ** (attempt - 1)));
242
+ }
243
+ }
244
+ held = now && now.origin === origin ? now : undefined;
245
+ if (held) {
246
+ ever = true;
247
+ for (const listener of listeners)
248
+ listener(held.key);
249
+ }
250
+ },
251
+ watch: (listener) => {
252
+ listeners.push(listener);
253
+ if (held)
254
+ listener(held.key);
255
+ },
256
+ };
257
+ }
258
+ /** The run's own model key held by `redact` (one of the run's redactors), the key now and each one a message hands it. */
259
+ export function holdRunKey(redact, runKey) {
260
+ runKey.watch((key) => redact.hold([key]));
261
+ }
262
+ /** Whether the claim opened a key of the run's own at its start: its routes then carry that key alone, its tools hold no
263
+ * model key, and no saved key is read for it. */
264
+ export const keyedAtClaim = (runKey) => runKey?.key() !== undefined;
265
+ /** Whether the server has a gateway (AGENT_MODEL_BASE_URL) and holds no key for it, in the runner's environment or saved:
266
+ * claimSpendOff's spend with no key to pay it. */
267
+ export async function serverKeyless(deps) {
268
+ return runModelOrigin(deps.env ?? {}) !== undefined && !(await savedModelKeys(deps))[GATEWAY_KEY_ENV];
269
+ }
270
+ /** Why the claim's model spend after the run's turn, or beside it on runs the runner starts (a shadow audit, a replay
271
+ * check), is skipped: `what` the work, asked when that spend would start. A run that brought
272
+ * its own key, at its start or with a message since (everHeld): that key is erased with its turn's end and the server's
273
+ * never pays for it (runKeyEndedFor). Else, a server that holds no gateway key (the runner's environment or
274
+ * Settings → Model keys) has none to spend (noServerKeyWords). Undefined where it runs. */
275
+ export async function claimSpendOff(deps, runKey) {
276
+ // A run's own process holds no seal key, nor every saved key: its runner says whether the server holds a gateway key.
277
+ const at = deps.sealKey ? undefined : held();
278
+ const keyless = at ? await at.serverKeyless() : await serverKeyless(deps);
279
+ return (what) => (runKey?.everHeld() ? runKeyEndedFor(what) : keyless ? noServerKeyWords(what) : undefined);
280
+ }
281
+ /** The env a claimed run's model routes read: the run's own key alone where the claim opened one (withRunModelKey); else
282
+ * the runner's environment with the keys `opened` for it (withModelKeys), whose gateway variables read the run's key
283
+ * once a message hands it one (withRunModelKeyLater). */
284
+ export function claimModelEnv(env, opened, runKey) {
285
+ if (!runKey)
286
+ return withModelKeys(env, opened);
287
+ return keyedAtClaim(runKey) ? withRunModelKey(env, runKey) : withRunModelKeyLater(withModelKeys(env, opened), runKey);
288
+ }
289
+ /** The decision providers' own keys (decisions.ts: Jev's TypeSafe key, OpenRouter's, Parallel's): some are also pi-ai
290
+ * provider names, but a run's model key never replaces them, so withoutModelKeys keeps them. */
291
+ const DECISION_KEY_NAMES = new Set([JEV_KEY_ENV, JEV_OPENROUTER_KEY_ENV, PARALLEL_KEY_ENV]);
292
+ /** `toolEnv` with no model key in it, for a run that holds its own: a tool's environment never carries a model key the
293
+ * run's model routes do not send. The decision providers' keys stay (DECISION_KEY_NAMES): a keyed workflow's judge,
294
+ * pick, sift, route and ask nodes run on them as any run's do. */
295
+ export function withoutModelKeys(toolEnv) {
296
+ for (const name of MODEL_KEY_NAMES)
297
+ if (!DECISION_KEY_NAMES.has(name))
298
+ delete toolEnv[name];
299
+ return toolEnv;
300
+ }
@@ -4,6 +4,7 @@ import { type HarnessEntry } from "../harness-table.js";
4
4
  import { type RunLaunch } from "../run-launch.js";
5
5
  import { type AdapterRun, type AdapterRunSpec } from "./adapter-loop.js";
6
6
  import { type AdapterSpecDeps } from "./adapter-run-spec.js";
7
+ import { type ClaimModelKey } from "./claim-secrets.js";
7
8
  import type { Claim } from "./loop-contract.js";
8
9
  /** The shared claim-time computer types live on the base spec (adapter-run-spec.ts), re-exported for this module's API. */
9
10
  export type { ClaimComputer, ClaimDeps, ClaimLaunch, ComputerForClaim } from "./adapter-run-spec.js";
@@ -50,7 +51,7 @@ export declare function cliCapModel(entry: HarnessEntry & {
50
51
  * run whose plane resolves it to native gets the harness's own search (#2458), this run's alone; whatever the runner's
51
52
  * own environment says of a plane is never the run's. Whether each search runs is the tool's setting at that search
52
53
  * (SessionIO.permission, the gate), never this plane, which is frozen when the run is made. */
53
- export declare function cliRunEnv(env: NodeJS.ProcessEnv | undefined, data: string, claimed?: NodeJS.ProcessEnv, plane?: RunLaunch["plane"]): NodeJS.ProcessEnv;
54
+ export declare function cliRunEnv(env: NodeJS.ProcessEnv | undefined, data: string, claimed?: NodeJS.ProcessEnv, plane?: RunLaunch["plane"], runKey?: ClaimModelKey): NodeJS.ProcessEnv;
54
55
  /** `tools` in the order the agent grants them (`granted`, catalog names; a tool is listed by its catalog name's wire
55
56
  * name), as main listed a CLI run's tools to its harness: each granted tool where its grant is, and a tool the grant
56
57
  * does not name (one a granted tool or the run's computer brings beside it) right after the tool listed before it. Ties