@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
package/dist/triggers.js CHANGED
@@ -23,6 +23,7 @@
23
23
  // now. A failed firing queues a trigger.failed webhook message.
24
24
  import { createHash, randomBytes } from "node:crypto";
25
25
  import { TRIGGER_LABELS } from "./timeline/index.js";
26
+ import { agentDeleting } from "./agents.js";
26
27
  import { ApiError, invalid } from "./errors.js";
27
28
  import { EFFORTS } from "./model.js";
28
29
  import { AgentArchived, StoreConstraint, TriggerFull } from "./store/index.js";
@@ -101,6 +102,8 @@ const REAL_CLOCK = {
101
102
  },
102
103
  };
103
104
  const iso = (ms) => new Date(ms).toISOString();
105
+ /** How many times a PATCH carrying a source is checked again when the trigger changed between its read and its write. */
106
+ const SOURCE_EDIT_TRIES = 3;
104
107
  export const firingKey = (by, triggerId, what) => `fir_${createHash("sha256").update(`${by}\n${triggerId}\n${what}`).digest("hex").slice(0, 32)}`;
105
108
  /** The adapter of `kind`, among `adapters`. */
106
109
  const adapterIn = (adapters, kind) => adapters.find((a) => a.id === kind);
@@ -215,6 +218,11 @@ export class Triggers {
215
218
  /** Each trigger's firings, driven one after another in the order asked: a firing's overlap check reads what the one
216
219
  * before it settled to. */
217
220
  turns = new Map();
221
+ /** Each trigger's removals and the edits that name a target (the one way it moves to another agent), one after another
222
+ * on this server: a move never lands between a template update's delete of its Composio instance and its conditional
223
+ * delete of the row (remove), so a moved trigger never stands on an instance that delete took. Across servers the
224
+ * store's conditional write holds the row. Removed from the map once idle. */
225
+ edits = new Map();
218
226
  /** Whether a firing waits for its trigger's room (max_active_runs): the timer then reads the store every WAIT_POLL_MS. */
219
227
  waiting = false;
220
228
  /** The triggers whose day-kept keys are at their cap, said once until they are under it again. */
@@ -268,7 +276,7 @@ export class Triggers {
268
276
  await this.need(id);
269
277
  return { firings: (await this.store.triggers.firings(id)).map(firingView) };
270
278
  }
271
- async create(body) {
279
+ async create(body, own = {}) {
272
280
  const now = this.clock.now();
273
281
  const { issues, source, target, settings } = readBody(this.adapters, body, now);
274
282
  // Checked as its runs are given it: the input, then the line saying what fired it (a name the trigger may yet take).
@@ -279,6 +287,9 @@ export class Triggers {
279
287
  const agentId = await this.agentOf(target);
280
288
  const id = `trg_${randomBytes(12).toString("hex")}`;
281
289
  const enabled = body.enabled !== false;
290
+ // None for an agent being deleted, off too, told before anything is minted or registered upstream (the store's own
291
+ // refusal is the backstop): the delete has listed its triggers already.
292
+ await this.needNotDeleting(agentId);
282
293
  if (enabled)
283
294
  await this.needAvailable(source.kind);
284
295
  // Never made on for an archived agent: told before anything is minted (the store's own refusal is the backstop).
@@ -292,7 +303,7 @@ export class Triggers {
292
303
  try {
293
304
  // Its secrets are kept before the trigger is: one whose secret cannot be kept makes none.
294
305
  const minted = await this.mint(kept, id);
295
- const row = await this.store.triggers.create({ id, agentId, name, enabled, source: kept, target, nextAt: enabled ? nextAfter(this.adapter(kept), kept, now) : null, catchupWindowS: settings.catchup_window_s ?? CATCHUP_WINDOW_S, ...(typeof settings.max_active_runs === "number" ? { maxActiveRuns: settings.max_active_runs } : {}) }, this.scopesOf(kept));
306
+ const row = await this.store.triggers.create({ id, agentId, name, enabled, source: kept, target, nextAt: enabled ? nextAfter(this.adapter(kept), kept, now) : null, catchupWindowS: settings.catchup_window_s ?? CATCHUP_WINDOW_S, ...(typeof settings.max_active_runs === "number" ? { maxActiveRuns: settings.max_active_runs } : {}), ...(own.templateId !== undefined ? { templateId: own.templateId } : {}) }, this.scopesOf(kept));
296
307
  return { row, minted };
297
308
  }
298
309
  catch (error) {
@@ -348,7 +359,7 @@ export class Triggers {
348
359
  /** Every trigger of source `kind`, on or off, with its name and source: what a source's sender keeps of them is shared
349
360
  * between some (composio-triggers.ts: one Composio instance per server, event and config). */
350
361
  async ofKind(kind) {
351
- return (await this.store.triggers.all()).map(asTrigger).filter((t) => t.source.kind === kind).map((t) => ({ id: t.id, name: t.name, enabled: t.enabled, source: t.source }));
362
+ return (await this.store.triggers.all()).map(asTrigger).filter((t) => t.source.kind === kind).map((t) => ({ id: t.id, name: t.name, enabled: t.enabled, source: t.source, updatedAt: t.updatedAt }));
352
363
  }
353
364
  /** Every trigger of source `kind` that is on, turned off because the connection it needs is gone (`disabled: {reason:
354
365
  * "connection_lost", detail}`), and said; how many. They are kept: on again once it is back. */
@@ -389,6 +400,13 @@ export class Triggers {
389
400
  if (agent?.archivedAt != null)
390
401
  throw new ApiError("agent_archived", archivedDetail(agent.name));
391
402
  }
403
+ /** Refused (409 agent_not_runnable, agents.ts agentDeleting) when agent `agentId` is being deleted: another system's
404
+ * agent with an archive time, which only its delete writes (agent-delete.ts). */
405
+ async needNotDeleting(agentId) {
406
+ const agent = await this.store.agents.get(agentId);
407
+ if (agent?.archivedAt != null && agent.externalRef != null)
408
+ throw agentDeleting(agentId);
409
+ }
392
410
  /** The store's refusal of a trigger left on for an archived agent (an archive that committed past needAgentOn) as the
393
411
  * same 409; undefined for any other error. */
394
412
  async archivedRefusal(error) {
@@ -439,8 +457,23 @@ export class Triggers {
439
457
  /** PATCH: any of name, enabled, source, target, catchup_window_s and max_active_runs. Enabled again, or its source changed, a trigger goes on from now: a
440
458
  * paused range is skipped, never caught up. Only the fields named change, applied to the trigger as it stands once
441
459
  * the target's check is done: an edit made meanwhile keeps its own. */
442
- async update(id, body) {
443
- const was = await this.need(id);
460
+ async update(id, body, own = {}) {
461
+ return isObject(body) && body.target !== undefined ? this.one(id, () => this.updating(id, body, own)) : this.updating(id, body, own);
462
+ }
463
+ async updating(id, body, own) {
464
+ // A source is written only on the row it was checked against (its updatedAt): another write meanwhile (the
465
+ // after-start seal's, a poll's next time) checks it again on the row as it is then, so a source read before a seal
466
+ // never writes the clear value back over it.
467
+ for (let tries = 1;; tries++) {
468
+ const done = await this.updateOnce(id, body, tries < SOURCE_EDIT_TRIES, own);
469
+ if (done)
470
+ return done;
471
+ }
472
+ }
473
+ /** One try of `update`: undefined when its source write was refused because the trigger changed since it was read and
474
+ * `retry` (try again); refused on the last try, precondition_failed. */
475
+ async updateOnce(id, body, retry, own) {
476
+ const was = await this.need(id, own.onAgent);
444
477
  const issues = unknownFields(body, WRITABLE);
445
478
  let source = body.source === undefined ? undefined : sourceOf(this.adapters, body.source, this.clock.now(), issues);
446
479
  const target = body.target === undefined ? undefined : targetOf(body.target, issues);
@@ -458,7 +491,7 @@ export class Triggers {
458
491
  issues.push(...(await this.deps.targetIssues(this.given(typeof body.name === "string" ? body.name.trim() : was.name, source ?? was.source, target, this.clock.now()))));
459
492
  if (issues.length || (body.source !== undefined && !source) || (body.target !== undefined && !target))
460
493
  throw invalid(issues);
461
- const trigger = await this.need(id);
494
+ const trigger = await this.need(id, own.onAgent);
462
495
  if (source && source.kind !== trigger.source.kind)
463
496
  throw invalid([{ field: "source.kind", reason: "not_allowed", message: "A trigger's source kind is fixed: make a new trigger for another." }]);
464
497
  // What its sender keeps of it stays as it was made (Composio's instance): only what reads it here changes.
@@ -484,6 +517,9 @@ export class Triggers {
484
517
  // A trigger belongs to its target's agent: aimed at another agent (or another agent's run), it moves to that agent,
485
518
  // whose one name per trigger and list of triggers it joins.
486
519
  const agentId = target ? await this.agentOf(target) : trigger.agentId;
520
+ // Moved to an agent being deleted, off too, or turned on for one: never (the store's own refusal is the backstop).
521
+ if (agentId !== trigger.agentId || (enabled && !trigger.enabled))
522
+ await this.needNotDeleting(agentId);
487
523
  // Turned on, or aimed elsewhere while on: never for an archived agent (the store's own refusal is the backstop).
488
524
  if (enabled && (!trigger.enabled || target))
489
525
  await this.needAgentOn(agentId);
@@ -491,15 +527,24 @@ export class Triggers {
491
527
  const polls = enabled && adapter.poll !== undefined && (!trigger.enabled || source !== undefined);
492
528
  if (polls && !trigger.enabled)
493
529
  this.restarts.set(id, (this.restarts.get(id) ?? 0) + 1);
530
+ // Moved to another agent, it is no package's (its mark cleared in the same write): a template update of the agent it
531
+ // left, or of the one it was moved back to, never takes it for one of its own. Kept in place by an apply, it is marked.
494
532
  const patch = {
495
- ...(agentId !== trigger.agentId ? { agentId } : {}),
533
+ ...(agentId !== trigger.agentId ? { agentId, templateId: null } : own.templateId !== undefined ? { templateId: own.templateId } : {}),
496
534
  ...(typeof body.name === "string" ? { name: body.name.trim() } : {}), ...(source ? { source } : {}), ...(target ? { target } : {}),
497
535
  ...(settings.catchup_window_s !== undefined ? { catchupWindowS: settings.catchup_window_s } : {}),
498
536
  ...(settings.max_active_runs !== undefined ? { maxActiveRuns: settings.max_active_runs } : {}), ...again, enabled, nextAt: next,
499
537
  };
500
- const row = await this.store.triggers.update(id, patch, enabled ? this.scopesOf(scopeSource) : []).catch(async (error) => {
538
+ const conditions = { ...(source ? { ifUpdatedAt: trigger.updatedAt, ifSource: trigger.source } : {}), ...(own.onAgent !== undefined ? { ifAgent: own.onAgent } : {}) };
539
+ const row = await this.store.triggers.update(id, patch, enabled ? this.scopesOf(scopeSource) : [], Object.keys(conditions).length ? conditions : undefined).catch(async (error) => {
501
540
  throw (await this.scopeTaken(error, scopeSource, trigger.id)) ?? (await this.archivedRefusal(error)) ?? nameTaken(error, patch.name ?? trigger.name);
502
541
  });
542
+ const still = !row && source ? await this.store.triggers.get(id) : undefined;
543
+ if (still && (own.onAgent === undefined || still.agentId === own.onAgent)) {
544
+ if (retry)
545
+ return undefined;
546
+ throw new ApiError("precondition_failed", "the trigger kept changing while this edit was checked: read it again, then send the edit again");
547
+ }
503
548
  if (!row)
504
549
  throw new ApiError("not_found", `no trigger ${id}`);
505
550
  // Turned on again, a polled source forgets its cursor and last check, so its first poll watches from now, and is
@@ -516,14 +561,78 @@ export class Triggers {
516
561
  const said = upstream && enabled !== trigger.enabled ? await upstream.turned(row.source, id, enabled) : undefined;
517
562
  return { ...(await this.view(asTrigger(row))), ...said };
518
563
  }
519
- /** The trigger deleted, and what its sender keeps of it removed after (said, when it could not be: the delete holds). */
520
- async remove(id) {
564
+ /** The trigger deleted, and what its sender keeps of it removed after (said, when it could not be: the delete holds).
565
+ * On its agent's condition (`onAgent`: a template update's removal), what its sender keeps goes first: one its sender
566
+ * did not remove is said, and the trigger is kept here as it was (its row, its package mark, its sealed config and
567
+ * whether it is on), for the next update to remove; once it is gone there, the trigger goes here while it is still that
568
+ * agent's, then what its sender kept here for it. */
569
+ async remove(id, own = {}) {
570
+ return this.one(id, () => this.removing(id, own));
571
+ }
572
+ async removing(id, own) {
573
+ const trigger = await this.need(id, own.onAgent);
574
+ const upstream = this.deps.upstream?.(trigger.source.kind);
575
+ if (own.onAgent === undefined) {
576
+ await this.dropHere(trigger);
577
+ return upstream?.removed(trigger.source, id);
578
+ }
579
+ const here = () => this.dropHere(trigger, own.onAgent);
580
+ if (!upstream)
581
+ return void (await here());
582
+ const said = await upstream.removedThere(trigger.source, id, here);
583
+ if (said !== undefined)
584
+ return said;
585
+ await upstream.forgotten(trigger.source, id);
586
+ return undefined;
587
+ }
588
+ /** `value` as a create keeps a source (its adapter's defaults filled in: a feed's every_s), or undefined when a create
589
+ * would refuse it. */
590
+ sourceAsKept(value) {
591
+ const issues = [];
592
+ const source = sourceOf(this.adapters, value, this.clock.now(), issues);
593
+ return issues.length ? undefined : source;
594
+ }
595
+ /** A deleted agent's trigger (agent-delete.ts), its first half: what its sender keeps of it removed there, the trigger
596
+ * kept here, so a failure, or a stop before the second half (removeHere), leaves the row that names what is left to
597
+ * remove for the next delete to find. Throws `unavailable` with what could not be done there. */
598
+ async removeThere(id) {
521
599
  const trigger = await this.need(id);
522
- await this.store.triggers.remove(id);
600
+ const said = await this.deps.upstream?.(trigger.source.kind)?.removed(trigger.source, id);
601
+ if (said === undefined)
602
+ return;
603
+ const notes = isObject(said) ? Object.values(said).flatMap((one) => (isObject(one) && typeof one.note === "string" ? [one.note] : [])) : [];
604
+ throw new ApiError("unavailable", `What ${trigger.source.kind} keeps of the trigger ${id} is not removed there yet${notes.length ? ` (${notes.join(" ")})` : ""}.`, { retry_after_ms: 5_000 });
605
+ }
606
+ /** The second half's first part, once removeThere is done: the trigger's adapter settings removed, its row kept. */
607
+ async removeSettings(id) {
608
+ await this.dropSettings(await this.need(id));
609
+ }
610
+ /** The second half, once removeThere is done: the trigger deleted here, its adapter settings and then its row. */
611
+ async removeHere(id) {
612
+ await this.dropHere(await this.need(id));
613
+ }
614
+ /** A trigger's adapter settings (a webhook's secret, a poll's cursor) removed; nothing else of it. */
615
+ async dropSettings(trigger) {
523
616
  if (this.adapter(trigger.source)?.settings?.scope === "trigger")
524
- await this.deps.settings(trigger.source.kind)?.remove(id);
617
+ await this.deps.settings(trigger.source.kind)?.remove(trigger.id);
618
+ }
619
+ /** The trigger's settings and row removed. Without `onAgent`, its settings first, then its row: the settings have no
620
+ * link to the row, so a failure or a stop between the two leaves the row, which the next remove finds, never settings
621
+ * with no trigger left to name them. With `onAgent` (a template update's removal), the row first, on its agent's
622
+ * condition compared in the delete, then its settings: a trigger moved to another agent meanwhile keeps both. A stop
623
+ * between the two there leaves settings no row names (keyed by an id never used again), never a moved trigger
624
+ * without its own. */
625
+ async dropHere(trigger, onAgent) {
626
+ if (onAgent === undefined) {
627
+ await this.dropSettings(trigger);
628
+ await this.store.triggers.remove(trigger.id);
629
+ }
630
+ else {
631
+ if (!(await this.store.triggers.remove(trigger.id, { ifAgent: onAgent })))
632
+ throw new ApiError("not_found", `no trigger ${trigger.id} on the agent ${onAgent}`);
633
+ await this.dropSettings(trigger);
634
+ }
525
635
  await this.arm();
526
- return this.deps.upstream?.(trigger.source.kind)?.removed(trigger.source, id);
527
636
  }
528
637
  /** Run now: one firing per Idempotency-Key, driven before the answer. `created` false: the key's firing, as it stands. */
529
638
  async fire(id, idempotencyKey) {
@@ -574,6 +683,21 @@ export class Triggers {
574
683
  }
575
684
  await this.arm();
576
685
  }
686
+ /** `work` on trigger `id` once every edit or removal of it asked before has settled (edits). Neither update nor remove
687
+ * calls the other, so a turn never waits on itself. Only a target edit takes a turn: any other edit runs beside one, as
688
+ * concurrent edits each keep their change (trigger-engine.test.mjs, two edits at once, one a target whose check is
689
+ * slow: the source edit lands while the target's check waits). One racing a removal writes its row while the row is
690
+ * there, or finds it gone (not_found): the store's update changes no row a delete took. */
691
+ one(id, work) {
692
+ const done = (this.edits.get(id) ?? Promise.resolve()).then(work);
693
+ const turn = done.then(() => undefined, () => undefined);
694
+ this.edits.set(id, turn);
695
+ void turn.then(() => {
696
+ if (this.edits.get(id) === turn)
697
+ this.edits.delete(id);
698
+ });
699
+ return done;
700
+ }
577
701
  /** `key`'s firing driven after every firing of its trigger asked before it. */
578
702
  inTurn(triggerId, key) {
579
703
  const known = this.driving.get(key);
@@ -1093,10 +1217,10 @@ export class Triggers {
1093
1217
  async all() {
1094
1218
  return (await this.store.triggers.all()).map(asTrigger);
1095
1219
  }
1096
- async need(id) {
1220
+ async need(id, onAgent) {
1097
1221
  const row = await this.store.triggers.get(id);
1098
- if (!row)
1099
- throw new ApiError("not_found", `no trigger ${id}`);
1222
+ if (!row || (onAgent !== undefined && row.agentId !== onAgent))
1223
+ throw new ApiError("not_found", onAgent !== undefined && row ? `no trigger ${id} on the agent ${onAgent}` : `no trigger ${id}`);
1100
1224
  return asTrigger(row);
1101
1225
  }
1102
1226
  /** The agent a trigger belongs to: its start_run target's, or the run its message_run target names. */
@@ -1120,7 +1244,7 @@ export class Triggers {
1120
1244
  return {
1121
1245
  ...(hook ? { hook_url: `${this.deps.publicUrl ?? ""}${hook}` } : {}),
1122
1246
  ...(adapter?.poll ? { last_check: check ? { at: check.at, ok: check.ok, message: check.message } : null } : {}),
1123
- id: trigger.id, name: trigger.name, enabled: trigger.enabled, source: trigger.source, target: trigger.target, catchup_window_s: trigger.catchupWindowS ?? CATCHUP_WINDOW_S,
1247
+ id: trigger.id, name: trigger.name, enabled: trigger.enabled, source: adapter?.shown ? adapter.shown(trigger.source) : trigger.source, target: trigger.target, catchup_window_s: trigger.catchupWindowS ?? CATCHUP_WINDOW_S,
1124
1248
  max_active_runs: trigger.maxActiveRuns ?? null, failures_in_a_row: trigger.failuresInARow, disabled: trigger.disabled ?? null, next_at: trigger.nextAt === null ? null : iso(trigger.nextAt), last_run_id: trigger.lastRunId,
1125
1249
  created_at: iso(trigger.createdAt), updated_at: iso(trigger.updatedAt), last_firing: last ? firingView(last) : null,
1126
1250
  };
@@ -28,10 +28,12 @@ export declare const skillNames: (files: Iterable<string>) => string[];
28
28
  /**
29
29
  * A definition as `POST /agents` and `PUT .../definition` take it: what one revision of an agent keeps. `instructions`
30
30
  * makes an agentic revision, `workflow` (an agentrun-dsl workflow) a workflow revision. `creating` asks for a name.
31
+ * `harnesses`: the ids `harness` may name, the server's table's (harness-table.ts harnessIds); the built-in ones by default.
31
32
  */
32
- export declare function definitionIssues(definition: unknown, { at, creating }?: {
33
- at?: string | undefined;
34
- creating?: boolean | undefined;
33
+ export declare function definitionIssues(definition: unknown, { at, creating, harnesses }?: {
34
+ at?: string;
35
+ creating?: boolean;
36
+ harnesses?: readonly string[];
35
37
  }): InputIssue[];
36
38
  /** A definition's skill names against the files its revision holds: each named skill has its SKILL.md, and each
37
39
  * SKILL.md is named. */
@@ -42,16 +44,18 @@ export type DefinitionBody = {
42
44
  note?: string;
43
45
  };
44
46
  /** What is wrong with `{definition, skills?, note?}`, as checkDefinitionBody refuses it: every field issue. */
45
- export declare function definitionBodyIssues(body: Json, { creating }?: {
46
- creating?: boolean | undefined;
47
+ export declare function definitionBodyIssues(body: Json, { creating, harnesses }?: {
48
+ creating?: boolean;
49
+ harnesses?: readonly string[];
47
50
  }): InputIssue[];
48
51
  /**
49
52
  * `{definition, skills?, note?}`, as `POST /agents` and `PUT .../definition` take it. `skills` are the revision's skill
50
53
  * files (absent on an edit: the current revision's); `note` says what the revision changes. Throws `422 input_invalid`
51
54
  * naming every field that is wrong.
52
55
  */
53
- export declare function checkDefinitionBody(body: Json, { creating }?: {
54
- creating?: boolean | undefined;
56
+ export declare function checkDefinitionBody(body: Json, { creating, harnesses }?: {
57
+ creating?: boolean;
58
+ harnesses?: readonly string[];
55
59
  }): DefinitionBody;
56
60
  /** The default input schema: `{content: string}`. */
57
61
  export declare const DEFAULT_INPUT_SCHEMA: {
package/dist/validate.js CHANGED
@@ -57,8 +57,9 @@ export const skillNames = (files) => [...files].filter((path) => path.endsWith(S
57
57
  /**
58
58
  * A definition as `POST /agents` and `PUT .../definition` take it: what one revision of an agent keeps. `instructions`
59
59
  * makes an agentic revision, `workflow` (an agentrun-dsl workflow) a workflow revision. `creating` asks for a name.
60
+ * `harnesses`: the ids `harness` may name, the server's table's (harness-table.ts harnessIds); the built-in ones by default.
60
61
  */
61
- export function definitionIssues(definition, { at = "definition", creating = true } = {}) {
62
+ export function definitionIssues(definition, { at = "definition", creating = true, harnesses = HARNESSES } = {}) {
62
63
  if (!isObject(definition))
63
64
  return [issue(at, definition === undefined ? "missing" : "wrong_type", `${at} is not an object`)];
64
65
  const out = [];
@@ -105,8 +106,8 @@ export function definitionIssues(definition, { at = "definition", creating = tru
105
106
  }
106
107
  }
107
108
  }
108
- if (d.harness !== undefined && !HARNESSES.includes(d.harness))
109
- out.push(issue(`${at}.harness`, "not_an_option", `${at}.harness is one of ${HARNESSES.join(", ")}`));
109
+ if (d.harness !== undefined && !harnesses.includes(d.harness))
110
+ out.push(issue(`${at}.harness`, "not_an_option", `${at}.harness is one of ${harnesses.join(", ")}`));
110
111
  if (d.place !== undefined)
111
112
  out.push(...placeIssues(d.place, `${at}.place`));
112
113
  if (d.computer !== undefined)
@@ -121,7 +122,7 @@ export function definitionIssues(definition, { at = "definition", creating = tru
121
122
  if (d.workflow !== undefined)
122
123
  out.push(issue(`${at}.learning`, "not_allowed", "A workflow revision does not learn: only an agentic run reflects on its work.", `Remove ${at}.learning.`));
123
124
  else {
124
- // true learns notes, skills and workflows, "notes" notes and skills alone; an object is on, with the loop's numbers.
125
+ // true learns (corrections and workflows); an object is on, with the loop's numbers.
125
126
  const wrong = learningSettingIssue(d.learning, `${at}.learning`);
126
127
  if (wrong)
127
128
  out.push(issue(`${at}.learning`, "wrong_type", wrong));
@@ -202,12 +203,12 @@ export function skillIssues(named, files) {
202
203
  ];
203
204
  }
204
205
  /** What is wrong with `{definition, skills?, note?}`, as checkDefinitionBody refuses it: every field issue. */
205
- export function definitionBodyIssues(body, { creating = true } = {}) {
206
+ export function definitionBodyIssues(body, { creating = true, harnesses } = {}) {
206
207
  const out = [];
207
208
  for (const key of Object.keys(body))
208
209
  if (!["definition", "skills", "note"].includes(key))
209
210
  out.push(issue(key, "not_allowed", `The body keeps no field ${key}.`, `Remove ${key}.`));
210
- out.push(...definitionIssues(body.definition, { creating }));
211
+ out.push(...definitionIssues(body.definition, { creating, ...(harnesses ? { harnesses } : {}) }));
211
212
  if (body.skills !== undefined || creating)
212
213
  skillFileIssues(body.skills ?? {}, out);
213
214
  if (body.note !== undefined && !(typeof body.note === "string" && body.note.length <= 2_000))
@@ -219,8 +220,8 @@ export function definitionBodyIssues(body, { creating = true } = {}) {
219
220
  * files (absent on an edit: the current revision's); `note` says what the revision changes. Throws `422 input_invalid`
220
221
  * naming every field that is wrong.
221
222
  */
222
- export function checkDefinitionBody(body, { creating = true } = {}) {
223
- const out = definitionBodyIssues(body, { creating });
223
+ export function checkDefinitionBody(body, { creating = true, harnesses } = {}) {
224
+ const out = definitionBodyIssues(body, { creating, ...(harnesses ? { harnesses } : {}) });
224
225
  if (out.length)
225
226
  throw invalid(out);
226
227
  return { definition: body.definition, ...(body.skills !== undefined || creating ? { skills: (body.skills ?? {}) } : {}), ...(typeof body.note === "string" ? { note: body.note } : {}) };
@@ -33,12 +33,14 @@ export declare function shownUrl(url: string): string;
33
33
  /** What the webhook takes beside its store: `retryMs`, the waits between tries; `privateTargets`, whether it may send to a
34
34
  * private network or this machine (the operator's AGENTRUN_WEBHOOK_PRIVATE_TARGETS, never the API's); `lookup`, every
35
35
  * address a name resolves to (the system resolver by default); `storeRetryMs`, the wait before a try the store failed is
36
- * tried again (STORE_RETRY_MS by default). */
36
+ * tried again (STORE_RETRY_MS by default); `timeoutMs`, the bound on one try (TIMEOUT_MS by default), under the claim a
37
+ * try holds its message by (CLAIM_MS), so no try outlives its claim. */
37
38
  export type WebhooksOptions = {
38
39
  retryMs?: readonly number[];
39
40
  privateTargets?: boolean;
40
41
  lookup?: (host: string) => Promise<string[]>;
41
42
  storeRetryMs?: number;
43
+ timeoutMs?: number;
42
44
  };
43
45
  /** What `emit` did with a notice: `queued`, in the store (or already there, or counted as sent); `unsaved`, the store
44
46
  * refused the write, so the caller tells it again; `off`, no webhook set, or one a 410 turned off, so nothing is to be
@@ -123,6 +125,7 @@ export declare class Webhooks {
123
125
  private readonly privateTargets;
124
126
  private readonly lookup;
125
127
  private readonly storeRetryMs;
128
+ private readonly timeoutMs;
126
129
  /** `key` is the server's seal key; `say` takes a log line's fields. */
127
130
  constructor(store: WebhooksStore, key: Buffer, say: (fields: Record<string, string | number>) => void, options?: WebhooksOptions);
128
131
  /** Reads the webhook, its secret and its ledger once, and goes on with the messages an earlier server left queued, each
package/dist/webhooks.js CHANGED
@@ -36,6 +36,7 @@ export const WEBHOOK_CHANNEL = "webhook-out";
36
36
  export const WEBHOOK_SETTING = "webhook-out";
37
37
  /** The waits before each try again, after the first at once: the Standard Webhooks schedule, cut at about a day. */
38
38
  export const RETRY_MS = [5_000, 300_000, 1_800_000, 7_200_000, 18_000_000, 36_000_000];
39
+ /** The bound on one try, its name's lookup included (WebhooksOptions.timeoutMs changes it). */
39
40
  const TIMEOUT_MS = 15_000;
40
41
  /** How far back an end's run.ended is still queued: past the retries (RETRY_MS, about a day), a notice would not be sent
41
42
  * any more. */
@@ -47,7 +48,8 @@ const MAX_URL = 2048;
47
48
  const SECRET_FIELD = "secret";
48
49
  /** Where its secret is kept in trigger_settings (adapter WEBHOOK_CHANNEL): the server's one scope, one minted field. */
49
50
  export const WEBHOOK_SECRET_SPEC = { scope: "server", fields: { [SECRET_FIELD]: { mint: true } } };
50
- /** How long a try's claim on its message holds (Store.channels.claim): past the try's own TIMEOUT_MS. */
51
+ /** How long a try's claim on its message holds (Store.channels.claim): past the try's own bound (TIMEOUT_MS, or the
52
+ * option's, which the constructor holds under this). */
51
53
  const CLAIM_MS = 60_000;
52
54
  /** Names that are this machine or its own network, whatever they resolve to. */
53
55
  const LOCAL_NAME = /^(localhost|.*\.localhost|.*\.local|.*\.internal)$/i;
@@ -148,10 +150,15 @@ export class Webhooks {
148
150
  privateTargets;
149
151
  lookup;
150
152
  storeRetryMs;
153
+ timeoutMs;
151
154
  /** `key` is the server's seal key; `say` takes a log line's fields. */
152
155
  constructor(store, key, say, options = {}) {
153
156
  this.store = store;
154
157
  this.say = say;
158
+ this.timeoutMs = options.timeoutMs ?? TIMEOUT_MS;
159
+ // Whole milliseconds: AbortSignal.timeout takes no other, and would throw at every send, after its claim.
160
+ if (!(Number.isInteger(this.timeoutMs) && this.timeoutMs > 0 && this.timeoutMs < CLAIM_MS))
161
+ throw new RangeError(`a webhook try's bound is whole milliseconds between 0 and ${CLAIM_MS} (its claim), not ${this.timeoutMs}`);
155
162
  this.secrets = sealedSettingsOverStore(store, WEBHOOK_CHANNEL, WEBHOOK_SECRET_SPEC, key, say);
156
163
  this.retryMs = options.retryMs ?? RETRY_MS;
157
164
  this.storeRetryMs = options.storeRetryMs ?? STORE_RETRY_MS;
@@ -480,7 +487,7 @@ export class Webhooks {
480
487
  catch (error) {
481
488
  // The try's own bound, cut or the server stopping: the lookup is given up on, never waited out.
482
489
  if (signal.aborted)
483
- return { refused: signal.reason?.name === "TimeoutError" ? `no answer in ${TIMEOUT_MS / 1000} s` : "not reached: the server is stopping" };
490
+ return { refused: signal.reason?.name === "TimeoutError" ? `no answer in ${this.timeoutMs / 1000} s` : "not reached: the server is stopping" };
484
491
  return { refused: `not reached: ${error.code ?? "its name does not resolve"}` };
485
492
  }
486
493
  if (!addresses.length || addresses.some(isPrivateAddress))
@@ -604,7 +611,7 @@ export class Webhooks {
604
611
  const started = performance.now();
605
612
  const target = new URL(config.url);
606
613
  // One bound for the whole try, its name's lookup included, and the server's stop cuts it all (close()).
607
- const signal = AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS), this.stop.signal]);
614
+ const signal = AbortSignal.any([AbortSignal.timeout(this.timeoutMs), this.stop.signal]);
608
615
  const dial = await this.dialled(target, signal);
609
616
  if ("refused" in dial)
610
617
  failure = dial.refused;
@@ -622,7 +629,7 @@ export class Webhooks {
622
629
  failure = res.status >= 300 && res.status < 400 ? `answered ${res.status}: a redirect is not followed` : `answered ${res.status}`;
623
630
  }
624
631
  catch (error) {
625
- failure = error.name === "TimeoutError" ? `no answer in ${TIMEOUT_MS / 1000} s` : `not reached: ${error.cause?.code ?? error.message}`;
632
+ failure = error.name === "TimeoutError" ? `no answer in ${this.timeoutMs / 1000} s` : `not reached: ${error.cause?.code ?? error.message}`;
626
633
  }
627
634
  finally {
628
635
  await dial.dispatcher?.close().catch(() => undefined);
@@ -51,6 +51,8 @@ export declare function forgetAgentComputer(store: {
51
51
  type Holding = {
52
52
  settings: Pick<Store["settings"], "get" | "putIfAbsent" | "replaceIf" | "removeIf">;
53
53
  };
54
+ /** The settings key of the hold on an agent's own computer (gone with the agent's delete: agent-delete.ts). */
55
+ export declare const agentComputerHoldKey: (agentId: string) => string;
54
56
  /** How long a hold that is letting go (its run detaching the computer at its end, or this server deleting it) keeps every
55
57
  * other run off: the most a provider's detach or delete is waited on. A holder that died while letting go is taken over
56
58
  * after it. */
@@ -55,6 +55,8 @@ export async function forgetAgentComputer(store, agentId, id) {
55
55
  await store.settings.removeIf(recordKey(agentId), record);
56
56
  }
57
57
  const holdKey = (agentId) => `agent_computer_hold:${agentId}`;
58
+ /** The settings key of the hold on an agent's own computer (gone with the agent's delete: agent-delete.ts). */
59
+ export const agentComputerHoldKey = holdKey;
58
60
  /** How long a hold that is letting go (its run detaching the computer at its end, or this server deleting it) keeps every
59
61
  * other run off: the most a provider's detach or delete is waited on. A holder that died while letting go is taken over
60
62
  * after it. */
@@ -3,6 +3,7 @@ import type { KeyEnv } from "../key-meta.js";
3
3
  import type { SealedKeys } from "../store/store.js";
4
4
  import { type ArchilDisk, type ArchilDiskOptions, type ArchilImage } from "./archil-disk.js";
5
5
  import type { LaunchDisk } from "../run-launch.js";
6
+ import type { Workspace } from "./workspace.js";
6
7
  import { type RunDisk, type RunDiskOptions } from "./run-disk.js";
7
8
  export { ARCHIL_API_KEY_ROW, type ArchilSettings };
8
9
  /** A claim this step refused, in words a person reads: no Archil call was made. */
@@ -68,4 +69,10 @@ export interface ArchilClaim {
68
69
  release(): void;
69
70
  }
70
71
  /** The Archil step of a claim: its disk made, its API key opened; the run's mount credential minted when a mount asks. */
72
+ /** The run's folder on its disk where its runner opened it (held.ts archil), by the claim that stands for it in a run's own
73
+ * process: openRunWorkspace opens this Workspace for it. */
74
+ export declare const HELD_ARCHIL: WeakMap<object, Workspace>;
75
+ /** archilClaim where the run is: with the runner's seal key, its own; in a run's own process, its runner's (held.ts), as
76
+ * a claim that holds no key, whose revoke, inherit and release its runner makes. */
77
+ export declare function archilWhereHeld(options: ArchilClaimOptions): Promise<ArchilClaim>;
71
78
  export declare function archilClaim(options: ArchilClaimOptions): Promise<ArchilClaim>;
@@ -21,6 +21,7 @@ import { archilApiKey } from "./archil-key.js";
21
21
  import { archilDisk, pinnedImage } from "./archil-disk.js";
22
22
  import { mintMountToken, nicknameOf, removeMountToken, removeOlderMountTokens, removeRunMountTokens } from "./archil-mount-credential.js";
23
23
  import { COMPUTER_LIFETIME_MS } from "./computer-deps.js";
24
+ import { heldOrThrow } from "../runner/held.js";
24
25
  import { runDiskOf } from "./run-disk.js";
25
26
  export { ARCHIL_API_KEY_ROW };
26
27
  /** A claim this step refused, in words a person reads: no Archil call was made. */
@@ -35,6 +36,34 @@ const RELEASED = "the run's Archil keys were released (its loop ended or its cla
35
36
  /** How long a revoke waits to read the users list again for a mint it never heard of: past Archil's list lag (mint-4). */
36
37
  const LOST_MINT_REREAD_MS = 2_000;
37
38
  /** The Archil step of a claim: its disk made, its API key opened; the run's mount credential minted when a mount asks. */
39
+ /** The run's folder on its disk where its runner opened it (held.ts archil), by the claim that stands for it in a run's own
40
+ * process: openRunWorkspace opens this Workspace for it. */
41
+ export const HELD_ARCHIL = new WeakMap();
42
+ /** archilClaim where the run is: with the runner's seal key, its own; in a run's own process, its runner's (held.ts), as
43
+ * a claim that holds no key, whose revoke, inherit and release its runner makes. */
44
+ export async function archilWhereHeld(options) {
45
+ if (options.sealKey)
46
+ return archilClaim(options);
47
+ const given = await heldOrThrow("a Mode 2 run's disk").archil();
48
+ if (!given)
49
+ throw new Error("the runner opened no Archil disk for this Mode 2 run");
50
+ const none = (what) => {
51
+ throw new Error(`a Mode 2 run's ${what} is its runner's, not its own process's`);
52
+ };
53
+ const claim = {
54
+ get disk() { return none("disk"); },
55
+ get diskKind() { return none("disk"); },
56
+ get region() { return none("region"); },
57
+ get image() { return none("image"); },
58
+ token: async () => none("mount credential"),
59
+ revoke: () => given.revoke(),
60
+ inherit: () => void given.inherit().catch(() => undefined),
61
+ secrets: new Set(),
62
+ release: () => void given.release().catch(() => undefined),
63
+ };
64
+ HELD_ARCHIL.set(claim, given.workspace);
65
+ return claim;
66
+ }
38
67
  export async function archilClaim(options) {
39
68
  const { settings, sealKey } = options;
40
69
  if (settings === undefined) {
@@ -1,6 +1,7 @@
1
- import type { MountSpec } from "@parcha/agentrun-computer";
1
+ import { type MountSpec } from "@parcha/agentrun-computer";
2
2
  import { type ArchilDisk, type ArchilImage } from "./archil-disk.js";
3
3
  import { type AttachableComputer, type MountCheck } from "./attach.js";
4
+ import type { FolderStore } from "./synced-folder.js";
4
5
  import { type Entry, type ExecOptions, type ExecResult, type GrepOptions, type GrepResult, type Stat, type Workspace } from "./workspace.js";
5
6
  /** The most bytes one append call takes. */
6
7
  export declare const APPEND_MAX: number;
@@ -53,7 +54,14 @@ declare class ArchilWorkspace implements Workspace {
53
54
  /** The Archil mount a computer makes of this folder: the disk, its region, the root as the folder, and the token. */
54
55
  mountSpec(): Promise<MountSpec>;
55
56
  attach(computer: AttachableComputer, check: MountCheck): Promise<Workspace>;
57
+ /** The folder as a synced folder's runner keeps it (synced-folder.ts): its files and directories through the file
58
+ * API, each write after this workspace's execs; its modes and links, which the file API keeps no record of, in
59
+ * sync.json beside it (syncMetaOf). A failed write answers in words, kind limit where the disk took too much. */
60
+ syncStore(): FolderStore;
56
61
  }
62
+ /** Where a synced folder's modes and links are kept: beside its root, never inside it, as its spill folder is. A run's
63
+ * ".../runs/<run>/work" keeps them in ".../runs/<run>/sync.json"; any other root in "<root>.sync.json". */
64
+ export declare const syncMetaOf: (root: string) => string;
57
65
  /** A Workspace on folder `root` of Archil disk `disk` (a relative key, e.g. agents/<agent>/runs/<run>/work). */
58
66
  export declare const archilWorkspace: (disk: ArchilDisk, root: string, options?: ArchilWorkspaceOptions) => ArchilWorkspace;
59
67
  export type { ArchilWorkspace };