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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (444) hide show
  1. package/CHANGELOG.md +270 -0
  2. package/README.md +343 -246
  3. package/dist/adapters.d.ts +0 -7
  4. package/dist/adapters.js +11 -22
  5. package/dist/agent-rows.d.ts +98 -0
  6. package/dist/agent-rows.js +1 -0
  7. package/dist/agents-store.d.ts +6 -2
  8. package/dist/agents-store.js +60 -25
  9. package/dist/agents.d.ts +9 -125
  10. package/dist/agents.js +15 -305
  11. package/dist/answer-json.d.ts +23 -0
  12. package/dist/answer-json.js +58 -0
  13. package/dist/api-only.d.ts +14 -0
  14. package/dist/api-only.js +33 -0
  15. package/dist/archil-routes.d.ts +1 -1
  16. package/dist/bash-read-only.d.ts +22 -0
  17. package/dist/bash-read-only.js +701 -0
  18. package/dist/box-egress-vm.d.ts +0 -8
  19. package/dist/box-egress-vm.js +0 -10
  20. package/dist/box-egress.d.ts +0 -4
  21. package/dist/box-egress.js +1 -16
  22. package/dist/box-paths.d.ts +25 -2
  23. package/dist/box-paths.js +79 -11
  24. package/dist/box-relay.d.ts +0 -11
  25. package/dist/box-relay.js +0 -20
  26. package/dist/box-settings.d.ts +26 -0
  27. package/dist/box-settings.js +63 -0
  28. package/dist/boxes.d.ts +21 -1
  29. package/dist/boxes.js +27 -20
  30. package/dist/browser-commands.d.ts +5 -2
  31. package/dist/browser-commands.js +4 -4
  32. package/dist/browser-computer.d.ts +5 -0
  33. package/dist/browser-computer.js +21 -0
  34. package/dist/browser-cost.d.ts +9 -6
  35. package/dist/browser-cost.js +94 -51
  36. package/dist/browser-live.d.ts +7 -20
  37. package/dist/browser-live.js +15 -93
  38. package/dist/browser-person.d.ts +18 -53
  39. package/dist/browser-person.js +94 -279
  40. package/dist/budget.d.ts +0 -4
  41. package/dist/budget.js +0 -7
  42. package/dist/cap.d.ts +8 -12
  43. package/dist/cap.js +12 -27
  44. package/dist/cli.js +61 -20
  45. package/dist/closed-pipes.d.ts +2 -0
  46. package/dist/closed-pipes.js +13 -0
  47. package/dist/computer-commands.d.ts +2 -0
  48. package/dist/computer-commands.js +2 -0
  49. package/dist/computer-keys.js +5 -2
  50. package/dist/computer-setting.d.ts +20 -4
  51. package/dist/computer-setting.js +25 -5
  52. package/dist/computers.d.ts +31 -3
  53. package/dist/computers.js +70 -11
  54. package/dist/confine.d.ts +120 -0
  55. package/dist/confine.js +436 -0
  56. package/dist/console/assets/index-RPHGlXmM.js +540 -0
  57. package/dist/console/assets/index-opTN5Naq.css +1 -0
  58. package/dist/console/index.html +2 -2
  59. package/dist/daytona-api.d.ts +0 -11
  60. package/dist/daytona-api.js +1 -82
  61. package/dist/decision-rows.d.ts +1 -1
  62. package/dist/decision-rows.js +1 -1
  63. package/dist/docker-daemon.d.ts +31 -0
  64. package/dist/docker-daemon.js +56 -0
  65. package/dist/docker-host-addresses.d.ts +37 -0
  66. package/dist/docker-host-addresses.js +110 -0
  67. package/dist/effort-guidance.js +4 -3
  68. package/dist/environment.d.ts +0 -3
  69. package/dist/environment.js +3 -5
  70. package/dist/errors.d.ts +0 -2
  71. package/dist/errors.js +6 -9
  72. package/dist/filed-answer.d.ts +32 -0
  73. package/dist/filed-answer.js +160 -0
  74. package/dist/files.d.ts +1 -11
  75. package/dist/files.js +2 -21
  76. package/dist/github-connection.d.ts +2 -2
  77. package/dist/github-connection.js +1 -1
  78. package/dist/graph-view.d.ts +3 -5
  79. package/dist/graph-view.js +4 -14
  80. package/dist/handed-values.d.ts +9 -0
  81. package/dist/handed-values.js +26 -0
  82. package/dist/harness-install.d.ts +22 -5
  83. package/dist/harness-install.js +98 -20
  84. package/dist/harness-opencode/adapter.d.ts +7 -0
  85. package/dist/harness-opencode/adapter.js +23 -6
  86. package/dist/harness-opencode/index.d.ts +1 -1
  87. package/dist/harness-opencode/index.js +1 -1
  88. package/dist/harness-opencode/verify.d.ts +17 -0
  89. package/dist/harness-opencode/verify.js +30 -10
  90. package/dist/harness-table.js +6 -1
  91. package/dist/harness.d.ts +4 -3
  92. package/dist/harness.js +16 -1
  93. package/dist/heard.js +1 -1
  94. package/dist/held-fd.d.ts +5 -0
  95. package/dist/held-fd.js +14 -0
  96. package/dist/host-calls.d.ts +20 -0
  97. package/dist/host-calls.js +12 -0
  98. package/dist/host-tools.d.ts +4 -21
  99. package/dist/host-tools.js +4 -61
  100. package/dist/host.d.ts +2 -4
  101. package/dist/host.js +2 -7
  102. package/dist/import-summary.d.ts +1 -5
  103. package/dist/import-summary.js +0 -21
  104. package/dist/import.d.ts +6 -6
  105. package/dist/import.js +62 -24
  106. package/dist/key-refusal.d.ts +5 -1
  107. package/dist/key-refusal.js +19 -4
  108. package/dist/learn/check.d.ts +6 -2
  109. package/dist/learn/check.js +83 -29
  110. package/dist/learn/folder.d.ts +5 -1
  111. package/dist/learn/folder.js +10 -3
  112. package/dist/learn/grade.d.ts +14 -0
  113. package/dist/learn/grade.js +68 -12
  114. package/dist/learn/index.d.ts +4 -4
  115. package/dist/learn/index.js +4 -4
  116. package/dist/learn/reflect.d.ts +31 -2
  117. package/dist/learn/reflect.js +79 -4
  118. package/dist/learn/relevance.d.ts +24 -0
  119. package/dist/learn/relevance.js +91 -0
  120. package/dist/learn/secrets.d.ts +8 -1
  121. package/dist/learn/secrets.js +95 -15
  122. package/dist/learn/skills.d.ts +112 -14
  123. package/dist/learn/skills.js +556 -52
  124. package/dist/learned-workflow.d.ts +37 -5
  125. package/dist/learned-workflow.js +106 -12
  126. package/dist/learning-routes.d.ts +16 -49
  127. package/dist/learning-routes.js +46 -36
  128. package/dist/learning-run.d.ts +6 -250
  129. package/dist/learning-run.js +11 -665
  130. package/dist/learning-words.d.ts +12 -5
  131. package/dist/learning-words.js +18 -9
  132. package/dist/learning.d.ts +93 -77
  133. package/dist/learning.js +176 -304
  134. package/dist/live/stream.js +1 -1
  135. package/dist/live/translate.js +13 -3
  136. package/dist/local-models.d.ts +11 -5
  137. package/dist/local-models.js +13 -8
  138. package/dist/mcp-gateway.d.ts +11 -52
  139. package/dist/mcp-gateway.js +44 -148
  140. package/dist/mcp-oauth.d.ts +10 -14
  141. package/dist/mcp-oauth.js +14 -32
  142. package/dist/mcp-probe.d.ts +0 -7
  143. package/dist/mcp-probe.js +4 -10
  144. package/dist/mcp-servers-store.d.ts +6 -6
  145. package/dist/mcp-servers-store.js +13 -15
  146. package/dist/mcp-store.js +3 -4
  147. package/dist/mcp.js +4 -3
  148. package/dist/model.d.ts +6 -7
  149. package/dist/model.js +43 -27
  150. package/dist/monid.d.ts +0 -2
  151. package/dist/monid.js +3 -6
  152. package/dist/openapi.d.ts +8 -1
  153. package/dist/openapi.js +124 -50
  154. package/dist/page.d.ts +1 -3
  155. package/dist/page.js +9 -19
  156. package/dist/paid-calls.d.ts +21 -27
  157. package/dist/paid-calls.js +28 -62
  158. package/dist/pause.d.ts +0 -2
  159. package/dist/pause.js +1 -8
  160. package/dist/place.d.ts +0 -5
  161. package/dist/place.js +0 -10
  162. package/dist/print.d.ts +2 -1
  163. package/dist/print.js +4 -2
  164. package/dist/propose.js +83 -39
  165. package/dist/push-handler.d.ts +1 -1
  166. package/dist/reflect-range.d.ts +25 -0
  167. package/dist/reflect-range.js +30 -0
  168. package/dist/refusal-words.d.ts +5 -1
  169. package/dist/refusal-words.js +5 -1
  170. package/dist/rows.d.ts +1 -1
  171. package/dist/run-cut.d.ts +0 -6
  172. package/dist/run-cut.js +0 -6
  173. package/dist/run-db.d.ts +2 -13
  174. package/dist/run-db.js +2 -41
  175. package/dist/run-launch.d.ts +19 -2
  176. package/dist/run-launch.js +13 -5
  177. package/dist/run-learning-served.d.ts +46 -2
  178. package/dist/run-learning-served.js +26 -7
  179. package/dist/run-reads-over-store.d.ts +1 -1
  180. package/dist/run-reads-over-store.js +15 -6
  181. package/dist/run-secrets.d.ts +33 -0
  182. package/dist/run-secrets.js +53 -0
  183. package/dist/run-timeline-served.d.ts +5 -1
  184. package/dist/run-timeline-served.js +19 -5
  185. package/dist/run-view.d.ts +12 -17
  186. package/dist/run-view.js +29 -28
  187. package/dist/runnable.d.ts +18 -10
  188. package/dist/runnable.js +21 -10
  189. package/dist/runner/adapter-loop.d.ts +14 -3
  190. package/dist/runner/adapter-loop.js +464 -62
  191. package/dist/runner/adapter-run-spec.d.ts +29 -8
  192. package/dist/runner/adapter-run-spec.js +96 -37
  193. package/dist/runner/adapters/index.js +11 -1
  194. package/dist/runner/adapters/pi.d.ts +15 -1
  195. package/dist/runner/adapters/pi.js +19 -5
  196. package/dist/runner/beside.d.ts +30 -3
  197. package/dist/runner/beside.js +54 -12
  198. package/dist/runner/claim-secrets.d.ts +10 -1
  199. package/dist/runner/claim-secrets.js +49 -15
  200. package/dist/runner/cli-run-spec.d.ts +2 -0
  201. package/dist/runner/cli-run-spec.js +60 -17
  202. package/dist/runner/commands.js +1 -1
  203. package/dist/runner/computer-journal.d.ts +15 -2
  204. package/dist/runner/computer-journal.js +41 -5
  205. package/dist/runner/conformance/harness.js +118 -8
  206. package/dist/runner/conformance/subject.d.ts +4 -1
  207. package/dist/runner/dispatch.d.ts +8 -1
  208. package/dist/runner/dispatch.js +81 -7
  209. package/dist/runner/end-of-run.d.ts +8 -2
  210. package/dist/runner/end-of-run.js +10 -4
  211. package/dist/runner/github-app.js +1 -1
  212. package/dist/runner/harness-loops.d.ts +9 -1
  213. package/dist/runner/harness-loops.js +15 -2
  214. package/dist/runner/journal-kinds.d.ts +30 -6
  215. package/dist/runner/journal-kinds.js +20 -5
  216. package/dist/runner/journal-reader.d.ts +1 -1
  217. package/dist/runner/journal-reader.js +2 -2
  218. package/dist/runner/learning-ask.d.ts +4 -0
  219. package/dist/runner/learning-ask.js +15 -1
  220. package/dist/runner/learning-end.d.ts +18 -3
  221. package/dist/runner/learning-end.js +113 -11
  222. package/dist/runner/learning-loop.d.ts +5 -0
  223. package/dist/runner/learning-loop.js +26 -6
  224. package/dist/runner/live-ticker.d.ts +1 -1
  225. package/dist/runner/live-ticker.js +28 -4
  226. package/dist/runner/loop-contract.d.ts +3 -2
  227. package/dist/runner/main.d.ts +64 -10
  228. package/dist/runner/main.js +165 -27
  229. package/dist/runner/mcp-claim.d.ts +1 -1
  230. package/dist/runner/mcp-claim.js +2 -3
  231. package/dist/runner/mcp-run.d.ts +2 -0
  232. package/dist/runner/mcp-run.js +15 -5
  233. package/dist/runner/memory-claim.d.ts +9 -0
  234. package/dist/runner/memory-claim.js +25 -0
  235. package/dist/runner/metrics.d.ts +33 -1
  236. package/dist/runner/metrics.js +85 -1
  237. package/dist/runner/model-route.d.ts +13 -0
  238. package/dist/runner/model-route.js +154 -11
  239. package/dist/runner/redact.d.ts +8 -2
  240. package/dist/runner/redact.js +11 -2
  241. package/dist/runner/reflect-hook.d.ts +12 -1
  242. package/dist/runner/reflect-hook.js +33 -3
  243. package/dist/runner/runner.d.ts +20 -0
  244. package/dist/runner/runner.js +56 -10
  245. package/dist/runner/store-port.d.ts +1 -1
  246. package/dist/runner/store-secrets.d.ts +3 -3
  247. package/dist/runner/store-secrets.js +8 -12
  248. package/dist/runner/text-tools.d.ts +44 -0
  249. package/dist/runner/text-tools.js +237 -0
  250. package/dist/runner/workflow-loop.d.ts +6 -0
  251. package/dist/runner/workflow-loop.js +6 -4
  252. package/dist/runner/workflow-spec.d.ts +18 -0
  253. package/dist/runner/workflow-spec.js +68 -26
  254. package/dist/runs-over-store.d.ts +8 -3
  255. package/dist/runs-over-store.js +76 -19
  256. package/dist/seal-key.d.ts +54 -15
  257. package/dist/seal-key.js +232 -82
  258. package/dist/sealed-settings-store.d.ts +19 -4
  259. package/dist/sealed-settings-store.js +9 -9
  260. package/dist/sealing.d.ts +4 -0
  261. package/dist/sealing.js +20 -0
  262. package/dist/secret-cut.d.ts +1 -1
  263. package/dist/secret-cut.js +1 -1
  264. package/dist/secret-name.d.ts +9 -0
  265. package/dist/secret-name.js +15 -0
  266. package/dist/secret-writes.d.ts +0 -1
  267. package/dist/secret-writes.js +0 -1
  268. package/dist/secrets.d.ts +2 -10
  269. package/dist/secrets.js +21 -17
  270. package/dist/server-fs.d.ts +4 -0
  271. package/dist/server-fs.js +8 -2
  272. package/dist/server-lock.d.ts +0 -24
  273. package/dist/server-lock.js +1 -80
  274. package/dist/server-secrets-store.d.ts +4 -4
  275. package/dist/server-secrets-store.js +10 -32
  276. package/dist/server-secrets.d.ts +4 -9
  277. package/dist/server-secrets.js +12 -33
  278. package/dist/server.d.ts +26 -44
  279. package/dist/server.js +229 -142
  280. package/dist/sign-ins-store.d.ts +34 -36
  281. package/dist/sign-ins-store.js +27 -38
  282. package/dist/sign-ins.d.ts +2 -119
  283. package/dist/sign-ins.js +5 -311
  284. package/dist/standard-webhooks.d.ts +1 -1
  285. package/dist/standard-webhooks.js +1 -1
  286. package/dist/stats.d.ts +3 -2
  287. package/dist/stats.js +1 -1
  288. package/dist/store/agent-body.d.ts +8 -2
  289. package/dist/store/agent-body.js +16 -4
  290. package/dist/store/index.d.ts +3 -3
  291. package/dist/store/index.js +3 -3
  292. package/dist/store/open.d.ts +18 -5
  293. package/dist/store/open.js +23 -3
  294. package/dist/store/postgres/connection.d.ts +18 -2
  295. package/dist/store/postgres/connection.js +156 -32
  296. package/dist/store/postgres/migrate.d.ts +4 -0
  297. package/dist/store/postgres/migrate.js +27 -3
  298. package/dist/store/postgres/migrations/0003_commands_one_answer.check.sql +19 -0
  299. package/dist/store/postgres/migrations/0003_commands_one_answer.sql +13 -0
  300. package/dist/store/postgres/store.d.ts +9 -2
  301. package/dist/store/postgres/store.js +9 -0
  302. package/dist/store/schema.js +1 -1
  303. package/dist/store/seal.js +2 -2
  304. package/dist/store/sqlite/connection.js +49 -5
  305. package/dist/store/sqlite/run-patch.d.ts +3 -0
  306. package/dist/store/sqlite/run-patch.js +3 -0
  307. package/dist/store/sqlite/store.js +64 -20
  308. package/dist/store/store.d.ts +38 -6
  309. package/dist/store/store.js +12 -2
  310. package/dist/store-reads/cap.d.ts +1 -1
  311. package/dist/store-reads/cap.js +22 -4
  312. package/dist/store-reads/journal.d.ts +25 -6
  313. package/dist/store-reads/journal.js +173 -21
  314. package/dist/store-reads/learning.d.ts +26 -8
  315. package/dist/store-reads/learning.js +12 -4
  316. package/dist/store-reads/workflow.d.ts +1 -1
  317. package/dist/store-reads/workflow.js +1 -1
  318. package/dist/store-view.d.ts +5 -1
  319. package/dist/store-view.js +91 -23
  320. package/dist/template-eval.d.ts +60 -4
  321. package/dist/template-eval.js +120 -54
  322. package/dist/templates.d.ts +24 -10
  323. package/dist/templates.js +69 -38
  324. package/dist/timeline/group.d.ts +33 -0
  325. package/dist/timeline/group.js +77 -0
  326. package/dist/timeline/index.d.ts +7 -0
  327. package/dist/timeline/index.js +7 -0
  328. package/dist/timeline/labels.d.ts +79 -0
  329. package/dist/timeline/labels.js +150 -0
  330. package/dist/timeline/origin.d.ts +56 -0
  331. package/dist/timeline/origin.js +92 -0
  332. package/dist/timeline/render-text.d.ts +11 -0
  333. package/dist/timeline/render-text.js +62 -0
  334. package/dist/timeline/timeline.d.ts +274 -0
  335. package/dist/timeline/timeline.js +1308 -0
  336. package/dist/timeline/util.d.ts +17 -0
  337. package/dist/timeline/util.js +49 -0
  338. package/dist/tool-gate.d.ts +41 -216
  339. package/dist/tool-gate.js +79 -540
  340. package/dist/tool-keys-store.d.ts +11 -12
  341. package/dist/tool-keys-store.js +24 -31
  342. package/dist/tool-keys.d.ts +9 -104
  343. package/dist/tool-keys.js +15 -271
  344. package/dist/tool-plane.d.ts +14 -42
  345. package/dist/tool-plane.js +23 -81
  346. package/dist/tool-settings-store.d.ts +1 -2
  347. package/dist/tool-settings-store.js +5 -8
  348. package/dist/tools.d.ts +31 -19
  349. package/dist/tools.js +101 -108
  350. package/dist/trigger-adapter.d.ts +2 -2
  351. package/dist/trigger-schedule.js +2 -1
  352. package/dist/trigger-secrets-shown.js +5 -1
  353. package/dist/trigger-targets.d.ts +13 -1
  354. package/dist/trigger-targets.js +31 -1
  355. package/dist/triggers.d.ts +2 -2
  356. package/dist/triggers.js +3 -3
  357. package/dist/turn-end.d.ts +1 -1
  358. package/dist/turn-end.js +1 -1
  359. package/dist/turns.d.ts +0 -6
  360. package/dist/turns.js +0 -19
  361. package/dist/uploads.js +1 -1
  362. package/dist/usage.d.ts +0 -2
  363. package/dist/usage.js +0 -2
  364. package/dist/validate.d.ts +2 -0
  365. package/dist/validate.js +23 -14
  366. package/dist/webhooks.d.ts +102 -76
  367. package/dist/webhooks.js +266 -233
  368. package/dist/workflow-run.d.ts +114 -0
  369. package/dist/workflow-run.js +144 -0
  370. package/dist/workspace/beneath.d.ts +19 -0
  371. package/dist/workspace/beneath.js +106 -0
  372. package/dist/workspace/computer-deps.d.ts +52 -5
  373. package/dist/workspace/computer-deps.js +154 -19
  374. package/dist/workspace/computer-for-claim.d.ts +19 -14
  375. package/dist/workspace/computer-for-claim.js +77 -16
  376. package/dist/workspace/computer-hold.d.ts +6 -1
  377. package/dist/workspace/computer-hold.js +6 -1
  378. package/dist/workspace/computer-janitor.d.ts +47 -0
  379. package/dist/workspace/computer-janitor.js +182 -0
  380. package/dist/workspace/computer-providers.d.ts +6 -2
  381. package/dist/workspace/computer-providers.js +1 -1
  382. package/dist/workspace/grep-worker.d.ts +5 -0
  383. package/dist/workspace/grep-worker.js +9 -6
  384. package/dist/workspace/index.d.ts +4 -3
  385. package/dist/workspace/index.js +4 -4
  386. package/dist/workspace/local.d.ts +11 -2
  387. package/dist/workspace/local.js +161 -43
  388. package/dist/workspace/resolve-computer.d.ts +50 -10
  389. package/dist/workspace/resolve-computer.js +46 -11
  390. package/dist/workspace/run-tools.d.ts +13 -31
  391. package/dist/workspace/run-tools.js +10 -27
  392. package/dist/workspace-paths.d.ts +0 -17
  393. package/dist/workspace-paths.js +1 -41
  394. package/dist/workspace.d.ts +0 -17
  395. package/dist/workspace.js +3 -99
  396. package/launcher/agentrun-serve +6 -33
  397. package/package.json +14 -14
  398. package/templates/README.md +9 -3
  399. package/templates/inbound-lead/agent.md +1 -1
  400. package/templates/kyb/agent.md +1 -1
  401. package/templates/market-monitor/agent.md +2 -2
  402. package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
  403. package/dist/app.d.ts +0 -5
  404. package/dist/app.js +0 -514
  405. package/dist/bound-files.d.ts +0 -4
  406. package/dist/bound-files.js +0 -164
  407. package/dist/bounds.d.ts +0 -26
  408. package/dist/bounds.js +0 -137
  409. package/dist/coding-tools.d.ts +0 -47
  410. package/dist/coding-tools.js +0 -189
  411. package/dist/computer-adb.d.ts +0 -12
  412. package/dist/computer-adb.js +0 -14
  413. package/dist/console/assets/index-35-7aeIZ.js +0 -519
  414. package/dist/console/assets/index-Cejbov-Y.css +0 -1
  415. package/dist/content-filter.d.ts +0 -7
  416. package/dist/content-filter.js +0 -23
  417. package/dist/disk.d.ts +0 -33
  418. package/dist/disk.js +0 -85
  419. package/dist/door-line.d.ts +0 -23
  420. package/dist/door-line.js +0 -83
  421. package/dist/grade.d.ts +0 -63
  422. package/dist/grade.js +0 -150
  423. package/dist/harness-app.d.ts +0 -69
  424. package/dist/harness-app.js +0 -1162
  425. package/dist/mcp-servers.d.ts +0 -269
  426. package/dist/mcp-servers.js +0 -456
  427. package/dist/packs.d.ts +0 -71
  428. package/dist/packs.js +0 -180
  429. package/dist/request-start.d.ts +0 -27
  430. package/dist/request-start.js +0 -381
  431. package/dist/run-box.d.ts +0 -205
  432. package/dist/run-box.js +0 -661
  433. package/dist/run-holder.d.ts +0 -52
  434. package/dist/run-holder.js +0 -139
  435. package/dist/runs.d.ts +0 -1200
  436. package/dist/runs.js +0 -4103
  437. package/dist/sealed-settings.d.ts +0 -60
  438. package/dist/sealed-settings.js +0 -178
  439. package/dist/served-only.d.ts +0 -5
  440. package/dist/served-only.js +0 -12
  441. package/dist/sessions.d.ts +0 -6
  442. package/dist/sessions.js +0 -109
  443. package/dist/workspace/computer-serves.d.ts +0 -12
  444. package/dist/workspace/computer-serves.js +0 -15
package/dist/tool-gate.js CHANGED
@@ -1,60 +1,38 @@
1
1
  // The tool gate: one gate every tool call passes, on every harness (plan item 4, "approvals that pause and resume").
2
2
  // Each tool has a permission: allow (it runs), deny (it is refused, and the model reads why), or ask (a person approves
3
- // or denies it first). The run's tool paths call it: pi's tools, wrapped where the run's registry composes its agents'
4
- // tools (app.ts), the kernel's host tools on claude-agent and codex (host-tools.ts), and the MCP gateway, which takes
5
- // the same park (harness-app.ts) and registers an executor for its approved calls.
3
+ // or denies it first). The runner's loops ask it before each call's intent (RunGate.decide): pi's tools, the kernel's
4
+ // host tools on claude-agent, codex and OpenCode, and the MCP gateway's calls alike.
6
5
  //
7
6
  // An MCP tool's setting may also carry a pin (pinRefusal): top-level arguments its calls must carry, each a JSON scalar
8
7
  // or one of a list. A pin adds to a setting and is never a permission: the gate checks it after a deny and before an
9
8
  // allow or an ask, so an allowed call not as pinned is refused unsent, and one under ask is refused before anyone is
10
9
  // asked; an approved call is checked again before it runs. The agent's pin and the server's both hold.
11
10
  //
12
- // An ask has one way to wait, every tool's alike (the owner's ruling of 11-04): it PARKS. The gated call answers at once
13
- // with waitingText and the turn ends, as ask_person's does; the run goes needs_input with the pending approval. On an
14
- // answer the gate runs the approved call itself (exactly once, through the journaled path: the kernel's host tool; an
15
- // MCP call, the executor its gateway registers, which comes with the gateway) and resumes the session with a message
16
- // naming the call. No CLI waits inside a tool call, so no CLI tool timeout applies. One call waits at a time: another
17
- // asked meanwhile is refused.
11
+ // bash's setting in an agent's own tool settings may carry a policy the same way (policyRefusal): `policy: "read-only"`
12
+ // (bash-read-only.ts) reads each shell call's command and refuses one that could write, run code or read outside the
13
+ // run's folder, after a deny and a pin and before an allow or an ask, so approvals "never" lifts none of it; an approved
14
+ // call is decided again before it runs.
15
+ //
16
+ // An ask has one way to wait, every tool's alike (the owner's ruling of 11-04): it PARKS. The runner's loop answers the
17
+ // call with waitingText and ends the turn, as ask_person's does; the run goes needs_input with the pending approval. On
18
+ // an answer the loop runs the approved call once, through the journal, and resumes the session with a message naming the
19
+ // call (resumeText). One call waits at a time: another asked meanwhile is refused.
18
20
  //
19
21
  // The answer: POST /v2/runs/{id}/approvals/{approval_id} { decision: "allow" | "deny", note? } -> 204. The first answer
20
22
  // wins; the same answer again is 204, a conflicting one 409. Only the owner's password session answers (server.ts), never
21
- // a token. A cancel marks pending approvals canceled. There is no ask timeout; a run nobody attends (a trigger's,
22
- // later) is to deny what is set to ask, which is not built yet.
23
- //
24
- // In the run's process: the asked call's request is kept in the run's own store (an `agentrun.tool_approval`
25
- // entry on its root conversation), so a restart keeps it. The server keeps a person's answer in the run's serve.json
26
- // (runs.ts); the gate reads it there, runs an allowed call once through the kernel's journal (the intent-then-outcome
27
- // effect any host tool's call is, on a session key of its own: a call a crash cut off runs again only where it is
28
- // replay-safe, and is answered interrupted otherwise) on the run's own coding source, and resumes the session with one
29
- // message that names the call and carries its result, or the refusal. A result is cut as every host tool's is
30
- // (HOST_RESULT_MAX_CHARS); the gate spills nothing.
31
- //
32
- // A stop or a cancel wins over an answer (runs.ts supersede): the gate reads the answer again before it writes the
33
- // call's intent, cuts an approved call in flight when its answer is withdrawn, and reads it again once the call is
34
- // done. Two windows of milliseconds remain, the same class as a stop racing any tool call inside a turn:
35
- // - A stop that lands between the read before the intent and drive.effect's intent lets a short call that does not
36
- // heed its abort run. Its result is dropped (the read after the call), never resumed.
37
- // - A stop that lands between the read after the call and resume()'s commit can start the resume turn on a stopped
38
- // run: a stop, unlike a cancel, leaves no mark the resume is held to.
39
- import { createHash } from "node:crypto";
40
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
41
- import { join } from "node:path";
42
- import { defineExtension, ROOT_CONVERSATION_ID, wrapTool } from "@earendil-works/pi-durable";
43
- import { DESKTOP_TOOL_DEFS, EffectFailure, INTERRUPTED_TEXT, openDrive, PHONE_TOOL_DEFS } from "@parcha/agentrun-kernel/kernel";
44
- import { boundResult } from "@parcha/agentrun-kernel/process";
23
+ // a token. A cancel marks pending approvals canceled. Where no one can approve (accounts off), an ask is refused at once
24
+ // in words, never parked.
25
+ import { defaultPermissionOf, DESKTOP_TOOL_DEFS, PHONE_TOOL_DEFS } from "@parcha/agentrun-kernel/kernel";
45
26
  import { READ_SKILL_FILE } from "@parcha/agentrun-kernel";
46
- import { BARRIER, MESSAGE_ORIGIN_KIND, syncNow } from "@parcha/pi-durable-disk";
47
- import { dataOfRun, readJson, RUN_META } from "./files.js";
48
27
  import { LEARNED_DIR } from "./learning.js";
49
28
  import { COMPUTER_ROOT, portPath } from "./workspace/paths.js";
50
- import { forModel } from "./result.js";
51
29
  import { builtinHarness } from "./harness-table.js";
52
30
  import { DEFAULT_HARNESS } from "./harness.js";
53
- import { durableSessions } from "./sessions.js";
54
31
  import { SHELL_TOOLS } from "./background.js";
55
- import { readToolSettings, SERVE_OWN_TOOLS, TABLE_TOOLS } from "./tool-plane.js";
56
- import { deniedText, modelNodeAskText, NOT_RUN, RAN_UNKNOWN, refusedText } from "./refusal-words.js";
32
+ import { SERVE_OWN_TOOLS, TABLE_TOOLS } from "./tool-plane.js";
33
+ import { deniedText, NOT_RUN, RAN_UNKNOWN } from "./refusal-words.js";
57
34
  import { approvalsOf } from "./approvals.js";
35
+ import { BASH_POLICIES, readOnlyReason, readOnlyText } from "./bash-read-only.js";
58
36
  /** What the gate does with a tool's calls. */
59
37
  export const PERMISSIONS = ["allow", "ask", "deny"];
60
38
  /** An MCP tool's gate name: `mcp:<server>/<tool>`. */
@@ -63,8 +41,14 @@ export const mcpToolName = (server, tool) => `mcp:${server}/${tool}`;
63
41
  export const isMcpTool = (name) => /^mcp:[^/]+\/.+$/.test(name);
64
42
  /** A third-party catalog tool, by its name: monid's (`monid.<tool>`) or an MCP server's (`mcp:<server>/<tool>`). */
65
43
  export const isCatalogTool = (name) => name.startsWith("monid.") || isMcpTool(name);
66
- /** The owner's defaults (11-01): a built-in kernel tool allow; a third-party catalog tool ask. */
67
- export const defaultPermission = (name) => (isCatalogTool(name) ? "ask" : "allow");
44
+ /** The owner's defaults (11-01): a built-in kernel tool allow; a third-party catalog tool ask. On a run with no computer,
45
+ * the kernel's ruled rule (defaultPermissionOf): bash asks first, and the background tools, which need a computer, are
46
+ * refused. */
47
+ export const defaultPermission = (name, where = {}) => {
48
+ if (isCatalogTool(name))
49
+ return "ask";
50
+ return where.computer === false ? defaultPermissionOf(name, { computer: false }) : "allow";
51
+ };
68
52
  /** The tools that run a command in the run's box (background.ts SHELL_TOOLS), each held to bash's setting: bash: deny
69
53
  * refuses bash_background as it refuses bash, and none of them is a settings key of its own. Under bash: ask the
70
54
  * watchers (SHELL_WATCHERS) run unasked. */
@@ -108,8 +92,8 @@ const HOST_TOOLS_SERVER = "hands";
108
92
  * wire name) and a harness's own (its facts' toolAlias: OpenCode's `<server>_<tool>`), is `mcp:<server>/<tool>`; a catalog's wire name (`monid_run`) is its
109
93
  * catalog name; the browser's registrations are `browser`; an adapter's read_skill_file is `read` (but an agent's MCP tool of that name is its own). OpenCode's `<server>_<tool>` maps only for a server of
110
94
  * `mcpServers` (the run's configured MCP servers), never by its shape, since a host tool's name has the same shape.
111
- * Undefined: nothing maps it, and the gate asks. The key only: an MCP call enters the gate once, in the MCP gateway for a
112
- * registration it marks (GATED_BY_GATEWAY), else where its tool is wrapped. */
95
+ * Undefined: nothing maps it, and the gate asks. The key only: the runner's adapter loop asks the gate once per call,
96
+ * before its intent (RunGate.decide). */
113
97
  export function settingsKeyOf(harness, name, mcpServers = []) {
114
98
  // A tool that runs a command in the box (bash_background and the rest) is held to bash's key: before the table's own names.
115
99
  if (Object.hasOwn(SHELL_FAMILY, name))
@@ -215,14 +199,14 @@ export function pinRefusal(name, args, server, agent = {}, ran = "no") {
215
199
  * layer, nor an ask that stands in for a setting that does not read. Nor is anything RunGate.enter refuses before it
216
200
  * decides between running and parking ever lifted (a template's pin, among them). Without never, the answer is as it
217
201
  * always was. */
218
- export function permissionFrom(name, server, agent = {}, approvals = "ask") {
219
- const { permission, from } = settingOf(name, server, agent);
202
+ export function permissionFrom(name, server, agent = {}, approvals = "ask", where = {}) {
203
+ const { permission, from } = settingOf(name, server, agent, where);
220
204
  if (approvals !== "never" || permission !== "ask" || standsBehind(name, server, agent))
221
205
  return { permission, from };
222
206
  return { permission: "allow", from: "unattended" };
223
207
  }
224
- /** Marks the server's settings map when its file is present but does not read (tool-plane.ts ToolSettings.get): every
225
- * tool then takes its defaults, and the deny the file may hold can't be seen, so nothing lifts an ask over it. A symbol:
208
+ /** Marks the server's settings map when its stored value does not read (tool-plane.ts loadToolSettings): every
209
+ * tool then takes its defaults, and a deny it may hold can't be seen, so nothing lifts an ask over it. A symbol:
226
210
  * carried by a spread, never by JSON (it is never served) nor by Object.entries (no reader takes it for a tool). */
227
211
  export const SETTINGS_UNREAD = Symbol("agentrun.tool-settings.unread");
228
212
  /** `settings` marked unread (SETTINGS_UNREAD), a copy: what a gate holds to while its settings' read fails, its last good
@@ -244,15 +228,18 @@ function standsBehind(name, server, agent) {
244
228
  }));
245
229
  }
246
230
  /** permissionFrom before approvals: what the layers and the default say. */
247
- function settingOf(name, server, agent) {
231
+ function settingOf(name, server, agent, where) {
248
232
  // A shell tool's permission is bash's (SHELL_FAMILY), from bash's layer, wherever it is read: the gate, capabilities, an
249
233
  // agent's tools. A watcher (SHELL_WATCHERS) runs unasked only under an ask set as written in a layer: an ask that stands
250
234
  // in for anything else (a value not offered, nothing set) stays an ask. No key of its own is read, so none lifts bash's.
251
235
  if (Object.hasOwn(SHELL_FAMILY, name) && SHELL_FAMILY[name] !== name) {
252
- const bash = settingFrom(SHELL_FAMILY[name], server, agent);
236
+ const bash = settingFrom(SHELL_FAMILY[name], server, agent, where);
237
+ // Nothing set, on a run with no computer: the tool's own default (defaultPermissionOf: the background tools refused).
238
+ if (bash.from === "default" && where.computer === false)
239
+ return { permission: defaultPermission(name, where), from: "default" };
253
240
  return { permission: bash.written ? shellPermission(name, bash.permission) : bash.permission, from: bash.from };
254
241
  }
255
- const { permission, from } = settingFrom(name, server, agent);
242
+ const { permission, from } = settingFrom(name, server, agent, where);
256
243
  return { permission, from };
257
244
  }
258
245
  /** permissionFrom's own read, and whether the permission is `written`: a valid one set in a layer as it reads there, not
@@ -261,7 +248,7 @@ function settingOf(name, server, agent) {
261
248
  * wildcard) is deny, that is the answer, from the server, before the agent's own settings are read, so no ask or allow
262
249
  * of the agent's reaches past it. The server's own exact allow under its wildcard deny is its answer, as before.
263
250
  * Otherwise the agent's own setting comes first, then the server's, then the default. */
264
- function settingFrom(name, server, agent) {
251
+ function settingFrom(name, server, agent, where = {}) {
265
252
  const own = permissionKeys(name).map((key) => server[key]).find((setting) => setting?.permission !== undefined);
266
253
  if (own?.permission === "deny")
267
254
  return { permission: "deny", from: "server", written: own.fallback !== true };
@@ -276,67 +263,22 @@ function settingFrom(name, server, agent) {
276
263
  return { permission: set, from, written: setting.fallback !== true };
277
264
  }
278
265
  }
279
- return { permission: defaultPermission(name), from: "default", written: false };
266
+ return { permission: defaultPermission(name, where), from: "default", written: false };
280
267
  }
281
268
  /** `name`'s permission in force (permissionFrom): the one rule every path reads, the gate's, the tool plane's and a
282
269
  * workflow's call nodes'. */
283
270
  export const permissionOf = (name, server, agent = {}, approvals = "ask") => permissionFrom(name, server, agent, approvals).permission;
284
- /** The mark a tool registration carries when its calls are gated by whoever registered it (the MCP gateway, which marks
285
- * its registrations only when it holds this run's gate and park, and enters the gate in each call): RunGate.extension
286
- * does not wrap it again. A private symbol, never `Symbol.for`: a global-registry key could be recreated by any code
287
- * with the string alone. Only code that imports this export can set it (mcp-gateway.ts does). */
288
- export const GATED_BY_GATEWAY = Symbol("agentrun.gatedByGateway");
289
271
  // The refusal words (SettingFrom, DenyFrom, NOT_RUN, RAN_UNKNOWN, denyPlace, deniedText, refusedText) live in
290
272
  // refusal-words.ts, shared with the runner's loops.
291
273
  export { deniedText, denyPlace, NOT_RUN, RAN_UNKNOWN, refusedText } from "./refusal-words.js";
292
274
  /** The refusal of an ask where no one can approve it: a server with accounts off, whose answer route takes no token. */
293
275
  export const noApproverText = (tool, ran = "no") => `Permission denied: ${tool} needs the owner's approval, and this server has accounts off (AGENTRUN_ACCOUNTS=0), so no one can give it: set the tool to allow or deny in the agent's Tools. ${ran === "no" ? NOT_RUN : RAN_UNKNOWN}`;
294
- /** Set by the server in a run's environment when no one can approve here (accounts off): the gate refuses each ask. */
295
- export const NO_APPROVER_ENV = "AGENTRUN_SERVE_NO_APPROVER";
296
276
  /** A parked call's answer: the run pauses here. */
297
277
  export const waitingText = (tool) => `This ${tool} call waits for a person's approval; the run pauses here and continues with the result when they answer.`;
298
278
  /** The Timeline row of a call that waits for an approval. */
299
279
  export const APPROVAL_ROW = "tool_approval_requested";
300
280
  /** A call's arguments shown on its approval, at most this many bytes. */
301
281
  export const ARGS_PREVIEW_BYTES = 2048;
302
- /** A gate of `policy` (the run's permission of a tool now) with no run behind it: allow and deny are enforced and
303
- * executors are kept; an ask throws, since nothing here can park a call (a run's is RunGate), and spill returns
304
- * the text as it is. */
305
- export function gateOf(policy) {
306
- const executors = new Map();
307
- return {
308
- async enter(tool) {
309
- const permission = policy(tool);
310
- if (permission === "allow")
311
- return undefined;
312
- if (permission === "deny")
313
- return { text: deniedText(tool), isError: true };
314
- throw new Error(`A gate with no run cannot ask a person about ${tool}.`);
315
- },
316
- executor(prefix, run) {
317
- executors.set(prefix, run);
318
- },
319
- spill(_callId, text) {
320
- return text;
321
- },
322
- };
323
- }
324
- /** The file in a run's directory that names the origin of the turn it runs now: written by the server alone, as it sends
325
- * the learning lane's reflect turn (runs.ts's toLiveDoor), and gone before a person's turn is sent (runs.ts's personsTurn).
326
- * The gate refuses that turn's writes under work/.learning/ (NOTE_ONLY_TEXT). */
327
- export const TURN_ORIGIN_FILE = "turn-origin.json";
328
- /** The run's store's record of a call that waits for approval (`phase: "requested"`), and of its answer acted on
329
- * (`phase: "resolved"`), on its root conversation. */
330
- export const APPROVAL_ENTRY = "agentrun.tool_approval";
331
- /** The run's store's record that the gate let a replay-safe pi call that can bill through (`{ callId }`), written before
332
- * the tool runs. pi-durable reruns a cut replay-safe call with the same call id and no sign that it is a rerun: this mark
333
- * is how the gate knows an earlier attempt may have been sent, so a deny of the rerun never says nothing was charged.
334
- * Outside `agentrun.tool_`, the tool ledger's kinds (paid-calls.ts), which readers of a run's charges take by prefix. */
335
- export const GATE_PASSED_ENTRY = "agentrun.gate_passed";
336
- /** The replay-safe tools, by settings key, whose call can be charged (a provider's fee: Parallel's search or extract): the
337
- * only ones the gate marks, so the root conversation does not grow by a mark per free call (read, the shell watchers,
338
- * monid's discover and inspect), whose earlier attempt charged nothing. monid.run is never rerun (host-tools.ts REPLAY). */
339
- const BILLED_REPLAYS = new Set(["web_fetch", "web_search"]);
340
282
  /** The refusal of the end-of-run turn's write or edit under work/.learning/: its notes are kept only from its answer's
341
283
  * note blocks (learn/reflect.ts parseReflection), which the host merges in its own store; a file there is never kept. */
342
284
  export const NOTE_ONLY_TEXT = "A note is kept only from your answer: write it in a ```note block, not to a file.";
@@ -373,77 +315,51 @@ export function resumeText(request, shown, decision, result) {
373
315
  const head = resumeHead(request, shown);
374
316
  return decision === "allow" ? `${head} They allowed it, and it ran${result.isError ? " and failed" : ""}. Its result:\n${result.text}` : `${head}\n${result.text}`;
375
317
  }
376
- /** The gate of the run `runId` at `where` (its directory, under the data directory's runs/), on its pinned revision:
377
- * an agentic run's parks an asked call; a workflow's model node cannot. `serverSettings`: where the server's tool
378
- * settings are read from, read at each call as the gate reads them; absent, the file beside the run's data directory (the
379
- * old per-run process). The runner gives the one store's (tool-plane.ts loadToolSettings), read once at the claim.
380
- * `approvers`: whether anyone can answer an ask here. The runner gives the store's (server-approvers.ts, read at each
381
- * claim); absent, the old per-run process's environment says (NO_APPROVER_ENV, set for it by runs.ts). */
382
- export function runGate(where, runId, revision, serverSettings, files, approvers = process.env[NO_APPROVER_ENV] !== "1") {
383
- return new RunGate({ root: where.root, work: where.work, data: dataOfRun(where.root), runId, harness: revision.body.harness ?? DEFAULT_HARNESS }, (revision.body.toolSettings ?? {}), revision.kind === "agentic", approvers, approvalsOf(revision.body.approvals), serverSettings, files);
318
+ /** The gate of the run `runId` whose folder is `where`, on its pinned revision. `serverSettings`: the server's tool
319
+ * settings as the runner read them from the one store at the claim (tool-plane.ts loadToolSettings), read at each call as
320
+ * the gate reads them. `approvers`: whether anyone can answer an ask here, the store's (server-approvers.ts, read at each
321
+ * claim). The gate reads nothing of the run's own files: they are the agent's to write. */
322
+ export function runGate(where, runId, revision, serverSettings, approvers) {
323
+ return new RunGate({ root: where.root, work: where.work, runId, harness: revision.body.harness ?? DEFAULT_HARNESS, ...(where.computer === undefined ? {} : { computer: where.computer }) }, serverSettings, (revision.body.toolSettings ?? {}), approvers, approvalsOf(revision.body.approvals));
384
324
  }
385
- /** The gate in a run's process, park mode: its policy, the turn's origin, the calls that wait, and the answers acted on. */
325
+ /** A run's tool gate on the runner: its policy, asked before each call's intent (decide). */
386
326
  export class RunGate {
387
327
  where;
328
+ readServerSettings;
388
329
  overrides;
389
- parks;
390
330
  approvers;
391
331
  approvals;
392
- readServerSettings;
393
- files;
394
- requests = new Map();
395
- /** The replay-safe pi calls the gate let through (GATE_PASSED_ENTRY), by call id: a rerun of one is a replay. */
396
- passed = new Set();
397
- durable;
398
- /** The run's store, for the barrier before a call that is not replay-safe: from `prime` (before the resume) or `load`. */
399
- syncTarget;
400
- context;
401
- coding;
402
- /** The run's own tools beyond the table (its learned_workflow), on which an approved call of one runs. */
403
- extra = [];
404
- resume;
405
- /** Told just before an allowed call runs (harness-app's): the run is un-parked, so the call's own model requests (a
406
- * learned workflow's node) go out; the harness is idle then, as the park ended its turn. */
407
- unpark;
408
- drive;
409
- timer;
410
- settling = false;
411
- stop = new AbortController();
412
- /** `where.data`: the server's data directory, its tool settings beside the store; `overrides`: the pinned revision's
413
- * own; `parks`: whether an asked call parks the run (an agentic run's), which a workflow's model node cannot;
332
+ /** `readServerSettings`: the server's tool settings, read at each call; `overrides`: the pinned revision's own;
414
333
  * `approvers`: whether anyone can answer an ask here (the owner's password session: accounts on); `approvals`: the
415
- * pinned revision's (definition.approvals): "never" runs the agent unattended, an ask allowed (permissionFrom);
416
- * `files`: where the run's own files are read (RunFiles), the run's folder `where.root` when absent. */
417
- constructor(where, overrides = {}, parks = true, approvers = true, approvals = "ask", readServerSettings, files = where.root) {
334
+ * pinned revision's (definition.approvals): "never" runs the agent unattended, an ask allowed (permissionFrom). */
335
+ constructor(where, readServerSettings, overrides = {}, approvers = true, approvals = "ask") {
418
336
  this.where = where;
337
+ this.readServerSettings = readServerSettings;
419
338
  this.overrides = overrides;
420
- this.parks = parks;
421
339
  this.approvers = approvers;
422
340
  this.approvals = approvals;
423
- this.readServerSettings = readServerSettings;
424
- this.files = files;
425
341
  }
426
342
  /** A tool's permission now (permissionOf): the revision's own over the server's setting, read at each call, so a change
427
343
  * reaches a live run at its next call; under approvals "never", an ask is an allow. */
428
344
  policy = (tool) => this.setting(tool).permission;
429
345
  /** The gate's answer for one call before its intent, for a loop that parks and answers asks itself (the runner's adapter
430
- * loops, pi's and the CLI harnesses'): run it, ask a person, or refuse it with the words the model reads. `enter`'s
431
- * order: the end-of-run turn's write under .learning/ (learningRefusal), the deny (with where it was set, and that
432
- * nothing was sent or charged), a pin, then the allow, then the ask, so an allow that approvals "never" made is decided
346
+ * loops, pi's and the CLI harnesses'): run it, ask a person, or refuse it with the words the model reads. The order:
347
+ * the deny (with where it was set, and that nothing was sent or charged), a pin, bash's policy (policyRefusal), then
348
+ * the allow, then the ask, so an allow that approvals "never" made is decided
433
349
  * only after every refusal. Where no one can approve (accounts off: the store's approvers row a runner reads at each
434
350
  * claim, server-approvers.ts), the ask is refused at once in words, never parked, as `enter` refuses it. */
435
351
  async decide(name, args) {
436
352
  const key = settingsKeyOf(this.where.harness ?? DEFAULT_HARNESS, name, this.where.mcpServers) ?? name;
437
353
  const tool = this.askedAs(name);
438
- const learning = await this.learningRefusal(key, args);
439
- if (learning)
440
- return { deny: learning };
441
354
  const verdict = this.policy(name);
442
355
  if (verdict === "deny")
443
356
  return { deny: deniedText(tool, key, this.denyFrom(name), "no") };
444
357
  const pinned = this.pinned(key, args, "no");
445
358
  if (pinned)
446
359
  return { deny: pinned.text };
360
+ const policed = this.policyRefusal(key, name, args);
361
+ if (policed)
362
+ return { deny: policed };
447
363
  if (verdict === "allow")
448
364
  return "allow";
449
365
  return this.approvers ? "ask" : { deny: noApproverText(tool, "no") };
@@ -475,19 +391,37 @@ export class RunGate {
475
391
  // No settings ever read: every tool asks, the shell watchers too (fail closed: an ask nothing read is never lifted),
476
392
  // or keeps the revision's own deny. Approvals "never" lifts none of these asks: the server's settings may hold a deny
477
393
  // no read has seen.
394
+ const where = { computer: this.where.computer };
478
395
  if (server === undefined) {
479
- const own = permissionFrom(key, {}, this.overrides);
396
+ const own = permissionFrom(key, {}, this.overrides, "ask", where);
480
397
  return own.permission === "deny" ? own : { permission: "ask", from: "default" };
481
398
  }
482
399
  // A shell tool under every harness's name for it is read as its own name, by bash's key (permissionFrom): a watcher's
483
- // ask is allow only where bash's ask is set as written.
484
- return permissionFrom(shellToolOf(tool) ?? key, server, this.overrides, this.approvals);
400
+ // ask is allow only where bash's ask is set as written. Its default is where the run's shell is (defaultPermission).
401
+ return permissionFrom(shellToolOf(tool) ?? key, server, this.overrides, this.approvals, where);
485
402
  }
486
403
  /** Why a pin refuses the call of `key` with `args` now (pinRefusal): the revision's own pins and the server's, read at
487
404
  * each call as its permission is, so a pin set or changed reaches a live run at its next call. */
488
405
  pinned(key, args, ran = "no") {
489
406
  return pinRefusal(key, args, this.serverSettings() ?? {}, this.overrides, ran);
490
407
  }
408
+ /** Why bash's policy refuses the call `name` (held to `key`) with `args` now, or undefined: the revision's own setting
409
+ * (an agent's tool_settings.bash.policy), so every shell tool under every harness's name for it is held to it. A policy
410
+ * this server does not know refuses every shell call (fail closed), as a pin that does not read does. */
411
+ policyRefusal(key, name, args) {
412
+ if (key !== "bash")
413
+ return undefined;
414
+ const policy = this.overrides.bash?.policy;
415
+ if (policy === undefined)
416
+ return undefined;
417
+ // Its own name for a background tool (bash_background, under claude-agent's mcp__hands__ name too); bash for bash and
418
+ // a CLI's own shell (codex's shell, claude-agent's Bash).
419
+ const tool = shellToolOf(name) ?? "bash";
420
+ if (!BASH_POLICIES.includes(policy))
421
+ return `${tool} can't be called: this agent's bash policy ${JSON.stringify(policy).slice(0, 60)} is not one this server knows (${BASH_POLICIES.join(", ")}), so no command is let through until its tool_settings name one. ${NOT_RUN}`;
422
+ const reason = readOnlyReason(tool, args, [this.where.work, COMPUTER_ROOT]);
423
+ return reason === undefined ? undefined : readOnlyText(tool, reason, NOT_RUN);
424
+ }
491
425
  lastGood;
492
426
  told = new Set();
493
427
  /** The server's settings now: read at each call, so a change reaches a live run; settings that do not read keep the
@@ -495,7 +429,7 @@ export class RunGate {
495
429
  * while the read fails, as a deny may be in what did not read (undefined: none read yet, every tool asks). Each problem
496
430
  * is said once, by file and field, never a value. */
497
431
  serverSettings() {
498
- const read = this.readServerSettings ? this.readServerSettings() : readToolSettings(this.where.data);
432
+ const read = this.readServerSettings();
499
433
  for (const line of read.problems) {
500
434
  if (this.told.has(line))
501
435
  continue;
@@ -507,399 +441,4 @@ export class RunGate {
507
441
  this.lastGood = read.settings;
508
442
  return this.lastGood;
509
443
  }
510
- /** The origin of the turn running now: what the server's internal path wrote for it (TURN_ORIGIN_FILE in the run's
511
- * folder, read through `files`: the run's Workspace or the local folder's path); none for a person's turn, or a file
512
- * that is not there or does not read. */
513
- async turnOrigin() {
514
- try {
515
- const text = typeof this.files === "string" ? readFileSync(join(this.files, TURN_ORIGIN_FILE), "utf8") : new TextDecoder().decode(await this.files.read(TURN_ORIGIN_FILE));
516
- return JSON.parse(text)?.origin === "reflect" ? "reflect" : undefined;
517
- }
518
- catch {
519
- return undefined;
520
- }
521
- }
522
- /** The end-of-run turn's refusal of a write or edit under work/.learning/ (NOTE_ONLY_TEXT, the adapter loop's one text
523
- * too), whatever the tool's permission: its notes are kept only from its answer. Undefined for any other call or turn,
524
- * whose write there follows its permission, as anywhere. The first check of `enter` and `decide`. */
525
- async learningRefusal(key, args) {
526
- if (key !== "write" && key !== "edit")
527
- return undefined;
528
- // The folder's path a file tool resolves against: the local folder's, or a computer's name for it (the Workspace's paths
529
- // are in the run's folder, so a path there is relative or under COMPUTER_ROOT).
530
- const work = typeof this.files === "string" ? this.where.work : COMPUTER_ROOT;
531
- return underLearning(work, args) && (await this.turnOrigin()) === "reflect" ? NOTE_ONLY_TEXT : undefined;
532
- }
533
- /** One model-initiated call at the gate: undefined when it may run; else its answer, refused at once (deny) or parked
534
- * (ask). One call waits at a time: another asked meanwhile is answered so, not run. */
535
- async enter(name, args, callId, park, options = {}) {
536
- // Held to its settings key (settingsKeyOf), the name its request, a refusal and the approved call give; a shell tool
537
- // held to bash's (SHELL_FAMILY) keeps its own name, so an approved bash_background runs bash_background, never bash.
538
- const key = settingsKeyOf(this.where.harness ?? DEFAULT_HARNESS, name, this.where.mcpServers) ?? name;
539
- const tool = shellToolOf(name) ?? key;
540
- // The end-of-run turn writes no file under work/.learning/, whatever the tool's permission (learningRefusal).
541
- const learning = await this.learningRefusal(key, args);
542
- if (learning)
543
- return { text: learning, isError: true };
544
- // Every refusal that does not come from the permission (the reflect turn's above, the deny and a pin's below) stays
545
- // ABOVE the allow's return further down. An allow here may be an ask the agent's approvals "never" turned into one
546
- // (permissionFrom's last step), and an allowed call runs at once: nothing after that return refuses it.
547
- const verdict = this.policy(name);
548
- const ran = options.replayed ? "unknown" : "no";
549
- if (verdict === "deny")
550
- return { text: deniedText(tool, key, this.denyFrom(name), ran), isError: true };
551
- // A pinned MCP tool's call whose arguments are not the pinned ones: refused before it runs or asks anyone, so an
552
- // allowed or approved call goes only where its settings fix.
553
- const pinned = this.pinned(key, args, ran);
554
- if (pinned) {
555
- process.stderr.write(`agentrun-serve: the tool gate refused ${key} call ${callId}: ${pinned.mismatched.length ? `${pinned.mismatched.join(", ")} not as pinned` : "its pin does not read"}\n`);
556
- return { text: pinned.text, isError: true };
557
- }
558
- if (verdict === "allow")
559
- return undefined;
560
- // No one can answer here: refused at once with why, never parked until a cancel.
561
- if (!this.approvers)
562
- return { text: noApproverText(tool, ran), isError: true };
563
- if (!this.parks)
564
- return { text: modelNodeAskText(tool), isError: true };
565
- // Named by the call, so a call run again after a crash parks as the same request.
566
- const id = `ap_${createHash("sha256").update(JSON.stringify([callId, tool, args ?? null])).digest("hex").slice(0, 16)}`;
567
- const other = [...this.requests.values()].find((held) => !held.resolved && held.id !== id);
568
- if (other)
569
- return { text: `This ${tool} call was not run: the ${other.tool} call ${other.call_id} already waits for a person's approval. Call it again once that one is answered.`, isError: true };
570
- if (!this.requests.has(id)) {
571
- const turn = park.turn?.();
572
- const request = { id, tool, call_id: callId, args: args ?? null, args_preview: argsPreview(args), requested_at: new Date().toISOString(), ...(turn !== undefined ? { turn } : {}), ...(options.price !== undefined ? { price: options.price } : {}), ...(options.replayed ? { replayed: true } : {}) };
573
- // Held before the commit's await, in the tick of the look above: pi runs a turn's calls in parallel, and an ask
574
- // entered meanwhile finds this one and is refused. A commit that fails lets it go.
575
- this.requests.set(id, request);
576
- try {
577
- await park.commit((tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: APPROVAL_ENTRY, data: { phase: "requested", ...request } }));
578
- }
579
- catch (error) {
580
- this.requests.delete(id);
581
- throw error;
582
- }
583
- }
584
- park.parked?.(callId);
585
- this.watch();
586
- return { text: waitingText(tool), isError: false, parked: true };
587
- }
588
- /** pi's `tools` behind the gate: wraps, by name, for the run's registry, which apply wherever it is selected (every
589
- * agent selects it: the root by default, a workflow's model node by name, app.ts). */
590
- extension(tools) {
591
- // Every tool is wrapped, an mcp__ name too, except a registration marked GATED_BY_GATEWAY: the MCP gateway marks the
592
- // ones whose calls it takes to this gate itself (gate.enter in its call), so such a call passes one gate, once. Nothing
593
- // is skipped by its name: an unmarked mcp__ tool is gated here as any tool is.
594
- const named = [...new Map(tools.filter((tool) => tool[GATED_BY_GATEWAY] !== true).map((tool) => [tool.name, tool])).values()];
595
- return defineExtension({ name: "tool-gate", wraps: named.map((tool) => wrapTool(tool, (inner) => this.wrap(inner))) });
596
- }
597
- wrap(tool) {
598
- const gate = this;
599
- const billed = tool.replay === "safe" && BILLED_REPLAYS.has(settingsKeyOf(this.where.harness ?? DEFAULT_HARNESS, tool.name, this.where.mcpServers) ?? "");
600
- return {
601
- ...tool,
602
- async execute(args, api, context) {
603
- const commit = (change) => api.commit(change, context);
604
- // A rerun of a replay-safe call the gate let through before (pi-durable reruns it with the same call id after a
605
- // crash, with no sign of it): an earlier attempt may have been sent, which the gate's refusal must not deny. The
606
- // marks were read before the run resumed (prime), so a rerun the resume dispatches finds them.
607
- const answer = await gate.enter(tool.name, args, api.callId, { commit }, gate.passed.has(api.callId) ? { replayed: true } : {});
608
- // A parked call ends the turn, as ask_person's does.
609
- if (answer)
610
- return forModel(answer.text, answer.parked ? { control: { terminate: true } } : { isError: true });
611
- // Let through: a call that can bill is marked in the store before the tool runs, so a crash after it leaves a
612
- // rerun the gate knows for one. A mark that fails to commit stops the call (nothing has been sent).
613
- if (billed && !gate.passed.has(api.callId)) {
614
- await commit((tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: GATE_PASSED_ENTRY, data: { callId: api.callId } }));
615
- gate.passed.add(api.callId);
616
- }
617
- // pi-durable committed the call's intent before it reached execute: durable before the call is sent, so a lost
618
- // box never sends it twice (a resumed call with its intent is reported interrupted, not run again).
619
- if (tool.replay !== "safe" || billed)
620
- await syncNow(gate.syncTarget, BARRIER.piToolIntent);
621
- return tool.execute(args, api, context);
622
- },
623
- };
624
- }
625
- /** Takes the open run, as the run's process opens (its app's onOpen), reading what its store holds of the gate unless
626
- * `prime` read it before the resume. `coding`: the run's coding source (coding-tools.ts), on which an approved coding
627
- * call runs, as the run's own would; `extra`, the run's own tools beyond the table (its learned_workflow), on which an
628
- * approved call of one runs. */
629
- async load(durable, context, coding, extra = []) {
630
- this.durable = durable;
631
- this.syncTarget = durable.store;
632
- this.context = context;
633
- this.coding = coding;
634
- this.extra = extra;
635
- if (!this.primed)
636
- await this.prime(durable.harness, context);
637
- }
638
- primed = false;
639
- /** Reads what the run's store holds of the gate (the calls that wait, the answers acted on, the calls it let through)
640
- * before the run resumes: its app's beforeResume hook, ahead of harness.resume(), which dispatches the calls a crash
641
- * cut. Read here, not in onOpen, which a recovered call races. A workflow's run calls only this; load reads nothing more. */
642
- async prime(harness, context, syncTarget) {
643
- if (syncTarget)
644
- this.syncTarget = syncTarget;
645
- const root = await harness.conversation(ROOT_CONVERSATION_ID, context);
646
- for (let cursor; root;) {
647
- const page = await root.entries({ order: "ascending" }, 500, cursor, context);
648
- for (const entry of page.items)
649
- this.note(entry);
650
- if (page.next === undefined)
651
- break;
652
- cursor = page.next;
653
- }
654
- this.primed = true;
655
- }
656
- /** Whether a call waits for its answer, or for its answer to be acted on: a harness keeps the turn it ended closed. */
657
- waiting() {
658
- return [...this.requests.values()].some((held) => !held.resolved);
659
- }
660
- /** Whether a call of the turn that took `message` was parked, answered or not: that turn ended at the park, though a
661
- * process that went before the next open settled it leaves it open in its session. */
662
- parkedIn(message) {
663
- return message !== undefined && [...this.requests.values()].some((held) => held.turn === message);
664
- }
665
- /** Acts on the answers from now on, resuming the session through `resume`; by default pi's root conversation. */
666
- start(resume, unpark) {
667
- this.resume = resume ?? this.submit;
668
- this.unpark = unpark;
669
- this.watch();
670
- }
671
- note(entry) {
672
- if (entry.kind === GATE_PASSED_ENTRY) {
673
- const callId = entry.data?.callId;
674
- if (typeof callId === "string")
675
- this.passed.add(callId);
676
- return;
677
- }
678
- const data = entry.data;
679
- if (entry.kind !== APPROVAL_ENTRY || typeof data?.id !== "string")
680
- return;
681
- const held = this.requests.get(data.id);
682
- if (data.phase === "requested" && !held)
683
- this.requests.set(data.id, data);
684
- else if (data.phase === "resolved" && held)
685
- held.resolved = true;
686
- }
687
- /** Reads the run's serve.json for answers while a call waits. */
688
- watch() {
689
- if (this.timer || !this.resume || !this.waiting())
690
- return;
691
- this.timer = setInterval(() => void this.settle(), 250);
692
- this.timer.unref();
693
- void this.settle();
694
- }
695
- /** pi's resume: an input to the root conversation under the request's own id, which pi takes once however often it
696
- * is sent, then the mark. */
697
- submit = async (id, content, mark, origin) => {
698
- const root = await this.durable.harness.root(this.context);
699
- const requestId = `approval-${id}`;
700
- // pi writes its user entry with no data, so the resume's origin goes first as an entry of its own, which the Timeline
701
- // matches to it by its text (pi-durable-disk's serve does the same). Once only: a resume tried again whose input is
702
- // in already writes no second marker, which would be left open for a later message with the same text.
703
- if (origin && (await root.commit((tx) => tx.submissionByRequest(root.id, requestId), this.context)) === undefined)
704
- await root.submit({ type: "write", requestId: `origin of ${requestId}`, entry: { kind: MESSAGE_ORIGIN_KIND, data: { origin, text: content } } }, this.context);
705
- await root.submit({ type: "input", requestId, content, whenBusy: "followUp" }, this.context);
706
- await this.durable.harness.commit(mark, this.context);
707
- };
708
- async settle() {
709
- // Acting on an answer already: an approved call may be in flight, which a stop or a cancel since cuts.
710
- if (this.settling)
711
- return void this.cutIfWithdrawn().catch(() => undefined);
712
- const open = [...this.requests.values()].filter((held) => !held.resolved);
713
- if (!open.length) {
714
- clearInterval(this.timer);
715
- this.timer = undefined;
716
- return;
717
- }
718
- this.settling = true;
719
- try {
720
- const answers = (await readJson(join(this.where.root, RUN_META)))?.approvals;
721
- for (const held of open) {
722
- const answer = answers?.[held.id];
723
- // Withdrawn with its run (a cancel, or a stop while it waited): never run and nothing resumed; resolved, so the
724
- // run waits on it no more.
725
- if (answer?.decision === "canceled" || answer?.decision === "withdrawn") {
726
- await this.resolveSilently(held, answer.decision);
727
- continue;
728
- }
729
- if (answer?.decision !== "allow" && answer?.decision !== "deny")
730
- continue;
731
- const decision = answer.decision;
732
- // Read again just before acting: a stop or a cancel since overrides the answer (runs.ts supersede), and then
733
- // nothing runs and nothing resumes. Once the call's intent is written, a stop takes its turn as any stop does.
734
- const now = (await readJson(join(this.where.root, RUN_META)))?.approvals?.[held.id];
735
- if (now?.decision !== decision) {
736
- if (now?.decision === "canceled" || now?.decision === "withdrawn")
737
- await this.resolveSilently(held, now.decision);
738
- continue;
739
- }
740
- // An allow runs the call, unless its tool was set to deny since it asked.
741
- // A request parked on a replay may follow an attempt that was sent: its refusal makes no "nothing was charged" claim.
742
- if (decision === "allow")
743
- this.unpark?.();
744
- // And a pin holds for an approved call too: one whose arguments are no longer as pinned (a pin set since it asked)
745
- // is refused as a deny since is.
746
- const result = decision === "deny" ? { text: refusedText(held.tool, answer.note, held.replayed ? "unknown" : "no"), isError: true } : this.policy(held.tool) === "deny" || this.pinned(held.tool, held.args) ? await this.ranOrDenied(held) : await this.run(held);
747
- // A test's seam, unset in a server (AGENTRUN_SERVE_TEST_CRASH_AFTER_CALL, a file that marks it once): an approved
748
- // call that ran and whose outcome is in the journal, then the process goes before its resume, once the test says go
749
- // (<file>.go), as a crash between the two would leave the run.
750
- const seam = process.env.AGENTRUN_SERVE_TEST_CRASH_AFTER_CALL;
751
- if (seam && decision === "allow" && !existsSync(seam)) {
752
- while (!existsSync(`${seam}.go`))
753
- await new Promise((done) => setTimeout(done, 50));
754
- writeFileSync(seam, "");
755
- process.kill(process.pid, "SIGKILL");
756
- }
757
- // And again once the call is done: a stop or a cancel while it ran drops its result, which never reaches the model.
758
- const after = (await readJson(join(this.where.root, RUN_META)))?.approvals?.[held.id];
759
- if (after?.decision === "canceled" || after?.decision === "withdrawn") {
760
- await this.resolveSilently(held, after.decision);
761
- continue;
762
- }
763
- // The arguments the message names are the server's redacted ones: the gate never writes a held value there. The
764
- // result is cut as every host tool's is (boundResult, HOST_RESULT_MAX_CHARS, in its words): an executor's (an
765
- // MCP call's) comes uncut, and its journal keeps it whole.
766
- // The server's message, never the person's: a declining message's words are theirs, beside it.
767
- const origin = decision === "deny" && answer.by === "message" && answer.note?.trim() ? { kind: "server", reason: "approval_declined", said: answer.note } : { kind: "server", reason: "approval" };
768
- await this.resume(held.id, resumeText(held, answer.args_shown ?? "(not shown)", decision, { ...result, text: boundResult(result.text).text }), (tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: APPROVAL_ENTRY, data: { phase: "resolved", id: held.id, decision } }), origin);
769
- held.resolved = true;
770
- }
771
- }
772
- catch (error) {
773
- // Tried again at the next tick: the call's effect answers from its journal, and the resume is taken once.
774
- process.stderr.write(`agentrun-serve: an answered approval is not acted on yet: ${error?.message ?? error}\n`);
775
- }
776
- finally {
777
- this.settling = false;
778
- }
779
- }
780
- /** The approved call in flight, and its own abort: a stop or a cancel of the run cuts it (cutIfWithdrawn). */
781
- inFlight;
782
- /** Cuts the approved call in flight when a stop or a cancel since withdrew its answer (runs.ts supersede). */
783
- async cutIfWithdrawn() {
784
- const flying = this.inFlight;
785
- if (!flying || flying.cut.signal.aborted)
786
- return;
787
- const now = (await readJson(join(this.where.root, RUN_META)))?.approvals?.[flying.id];
788
- if (now?.decision === "canceled" || now?.decision === "withdrawn")
789
- flying.cut.abort(new Error(INTERRUPTED_TEXT));
790
- }
791
- /** A call withdrawn with its run: resolved in the run's store with nothing run and nothing resumed. */
792
- async resolveSilently(held, decision) {
793
- await this.durable.harness.commit((tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: APPROVAL_ENTRY, data: { phase: "resolved", id: held.id, decision } }), this.context);
794
- held.resolved = true;
795
- }
796
- /** The run's own `approvals:` session, the journal every approved call runs through: opened once. */
797
- journal() {
798
- this.drive ??= durableSessions(this.durable.harness, this.context, this.durable.store)
799
- .open(`approvals:${this.where.runId}`)
800
- .then((writer) => openDrive({ writer, signal: this.stop.signal }))
801
- .catch((error) => {
802
- this.drive = undefined;
803
- throw error;
804
- });
805
- return this.drive;
806
- }
807
- /** An approved call whose tool was set to deny (or pinned so that its arguments no longer hold) before the gate acted
808
- * on the answer. A call whose outcome is in the
809
- * journal RAN (a crash came between it and its resume): the model gets that outcome, as any allowed call's, and nothing
810
- * runs again; a later deny can't un-run it. One with an intent and no outcome may have been sent: denied, with no claim
811
- * either way. One the journal never saw is denied, not run and not charged. */
812
- async ranOrDenied(held) {
813
- const drive = await this.journal();
814
- const key = `approval:${held.id}`;
815
- if (drive.isMemoized(key))
816
- return this.run(held);
817
- const heldTo = settingsKeyOf(this.where.harness ?? DEFAULT_HARNESS, held.tool, this.where.mcpServers);
818
- const ran = held.replayed || drive.state().effects.has(key) ? "unknown" : "no";
819
- if (this.policy(held.tool) !== "deny") {
820
- const pinned = this.pinned(heldTo ?? held.tool, held.args, ran);
821
- if (pinned)
822
- return { text: pinned.text, isError: true };
823
- }
824
- return { text: deniedText(held.tool, heldTo, this.denyFrom(held.tool), ran), isError: true };
825
- }
826
- /** The approved call, run once by the kernel through the journal any host tool's call goes through, as an effect of
827
- * the run's own `approvals:` session, on the run's coding source: its intent is durable before it runs, its outcome
828
- * after; a replay-safe call a crash cut off runs again, any other is answered interrupted and never runs again. */
829
- async run(held) {
830
- const runner = await this.runnerOf(held);
831
- if ("refused" in runner)
832
- return { text: runner.refused, isError: true };
833
- const drive = this.journal();
834
- try {
835
- // Its own abort beside the drive's: a stop or a cancel of the run cuts this call, which runs outside any turn.
836
- const cut = new AbortController();
837
- this.inFlight = { id: held.id, cut };
838
- try {
839
- return await (await drive).effect(`approval:${held.id}`, { name: held.tool, replay: runner.replay, input: held.args }, async ({ signal }) => {
840
- try {
841
- const answer = await runner.call(AbortSignal.any([signal, cut.signal]));
842
- // A host tool answers its own abort as an error result: still the stop's cut.
843
- if (cut.signal.aborted)
844
- throw new Error(INTERRUPTED_TEXT);
845
- return { text: answer.text, isError: answer.isError === true };
846
- }
847
- catch (error) {
848
- // Cut by a stop: its journal answers it interrupted, as a crash's cut call is.
849
- if (cut.signal.aborted)
850
- throw new Error(INTERRUPTED_TEXT);
851
- throw error;
852
- }
853
- });
854
- }
855
- finally {
856
- this.inFlight = undefined;
857
- }
858
- }
859
- catch (error) {
860
- // Its outcome (interrupted, or a throw), journaled; anything else (the drive fenced) is tried again.
861
- if (error instanceof EffectFailure)
862
- return { text: error.message, isError: true };
863
- throw error;
864
- }
865
- }
866
- executors = new Map();
867
- /** Registers how an approved call of tools named `prefix…` runs (the MCP gateway's, "mcp:"): one per prefix and run.
868
- * The gate runs it exactly once on its journaled path, as it runs a kernel host tool, with no deadline of its own: the
869
- * executor bounds the call (Executor). */
870
- executor(prefix, run) {
871
- this.executors.set(prefix, run);
872
- }
873
- /** What runs an approved call: the executor its tool's prefix names, which may have effects anywhere, so a call a
874
- * crash cut off is never run again; else the kernel's host tool of the name, as replay-safe as that tool is. Neither:
875
- * refused, in words, and never run. */
876
- async runnerOf(held) {
877
- const prefix = [...this.executors.keys()].find((given) => held.tool.startsWith(given));
878
- if (prefix) {
879
- const executor = this.executors.get(prefix);
880
- return { replay: "never", call: (signal) => executor(held.tool, held.args, held.call_id, signal) };
881
- }
882
- if (isMcpTool(held.tool))
883
- return { refused: `The approved ${held.tool} call was not run: nothing in this run runs mcp: calls.` };
884
- // Imported here, not at the top: host-tools reaches the web tools, which reach tool-plane, which reads this module's
885
- // permission rule, and a cycle of top-level imports leaves one of them uninitialized for whichever loads first.
886
- const { hostTools } = await import("./host-tools.js");
887
- const [tool] = hostTools(this.durable, [held.tool], this.context, new Map(), this.coding, undefined, this.extra);
888
- if (!tool)
889
- return { refused: `The approved ${held.tool} call is not one the kernel runs outside the model's turn.` };
890
- return { replay: tool.replay, call: (signal) => tool.call(held.args, { callId: held.call_id, signal }) };
891
- }
892
- }
893
- /** `value` as JSON with every object's keys sorted, so the same arguments are the same text however they were built. */
894
- const canonical = (value) => Array.isArray(value) ? `[${value.map(canonical).join(",")}]` : value !== null && typeof value === "object" ? `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(",")}}` : (JSON.stringify(value) ?? "null");
895
- /** The digest a park by tool and arguments is matched on: the sha256 of the arguments' canonical JSON, in hex. */
896
- export const argsDigest = (args) => createHash("sha256").update(canonical(args ?? {})).digest("hex");
897
- /** Whether the tool result `result` ends the turn `parked` waits in (harness-app.ts asks it at each one): the parked
898
- * call's own, by its id; or, for a park by tool and arguments, the next result of that tool with those arguments. */
899
- export function endsTurn(parked, result) {
900
- if (!parked)
901
- return false;
902
- if ("callId" in parked)
903
- return parked.callId === result.callId;
904
- return parked.tool === result.tool && parked.argsDigest === argsDigest(result.args);
905
444
  }