@parcha/agentrun-serve 0.1.0-beta.56 → 0.1.0-beta.57

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 (275) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/README.md +201 -52
  3. package/dist/acp-package.d.ts +2 -0
  4. package/dist/adapters.d.ts +15 -3
  5. package/dist/adapters.js +17 -6
  6. package/dist/agent-package.d.ts +3 -3
  7. package/dist/agent-package.js +17 -8
  8. package/dist/api-keys.d.ts +76 -0
  9. package/dist/api-keys.js +217 -0
  10. package/dist/api-only.d.ts +17 -5
  11. package/dist/api-only.js +34 -8
  12. package/dist/browser-hold.d.ts +23 -0
  13. package/dist/browser-hold.js +48 -0
  14. package/dist/browser-person.d.ts +3 -0
  15. package/dist/browser-person.js +4 -1
  16. package/dist/chatgpt-plan.d.ts +18 -8
  17. package/dist/chatgpt-plan.js +91 -34
  18. package/dist/claude-login.d.ts +74 -16
  19. package/dist/claude-login.js +215 -56
  20. package/dist/claude-sign-in.d.ts +17 -6
  21. package/dist/claude-sign-in.js +42 -39
  22. package/dist/cli.js +63 -6
  23. package/dist/composio-made.d.ts +4 -0
  24. package/dist/composio-made.js +4 -0
  25. package/dist/composio-preset.d.ts +43 -2
  26. package/dist/composio-preset.js +92 -4
  27. package/dist/computer-held-routes.d.ts +5 -4
  28. package/dist/computer-held-routes.js +29 -8
  29. package/dist/computer-held.d.ts +6 -1
  30. package/dist/computer-held.js +14 -0
  31. package/dist/computer-record.js +4 -2
  32. package/dist/computer-viewer.js +4 -1
  33. package/dist/console/assets/{backend-Boh7tC4i.js → backend-DVhHzJX2.js} +19 -19
  34. package/dist/console/assets/index-Bwn1oTem.css +1 -0
  35. package/dist/console/assets/index-okP-y8PK.js +747 -0
  36. package/dist/console/index.html +2 -2
  37. package/dist/decisions.d.ts +1 -1
  38. package/dist/decisions.js +1 -1
  39. package/dist/docker-host-addresses.d.ts +22 -11
  40. package/dist/docker-host-addresses.js +38 -26
  41. package/dist/env-switch.d.ts +10 -0
  42. package/dist/env-switch.js +33 -2
  43. package/dist/errors.d.ts +1 -0
  44. package/dist/errors.js +2 -0
  45. package/dist/fal-price.d.ts +58 -20
  46. package/dist/fal-price.js +341 -82
  47. package/dist/handed-values.d.ts +43 -1
  48. package/dist/handed-values.js +119 -2
  49. package/dist/harness-archive.d.ts +13 -10
  50. package/dist/harness-archive.js +60 -34
  51. package/dist/harness-check.d.ts +3 -1
  52. package/dist/harness-check.js +25 -46
  53. package/dist/harness-keys.d.ts +73 -27
  54. package/dist/harness-keys.js +194 -61
  55. package/dist/harness-recipe-entry.d.ts +8 -7
  56. package/dist/harness-recipe-entry.js +21 -32
  57. package/dist/harness-recipe.d.ts +38 -25
  58. package/dist/harness-recipe.js +49 -36
  59. package/dist/harness-sign-in.js +12 -23
  60. package/dist/harness-table.d.ts +10 -3
  61. package/dist/harness-table.js +26 -21
  62. package/dist/harness.d.ts +1 -0
  63. package/dist/harness.js +17 -5
  64. package/dist/hosted-search.d.ts +1 -1
  65. package/dist/hosted-search.js +1 -2
  66. package/dist/import-worker.d.ts +4 -2
  67. package/dist/import-worker.js +9 -2
  68. package/dist/in-flight.d.ts +26 -0
  69. package/dist/in-flight.js +52 -0
  70. package/dist/key-refusal.d.ts +9 -4
  71. package/dist/key-refusal.js +107 -25
  72. package/dist/learn/secrets.d.ts +13 -3
  73. package/dist/learn/secrets.js +118 -17
  74. package/dist/learning-cost.d.ts +6 -3
  75. package/dist/learning-cost.js +4 -2
  76. package/dist/link-check.js +1 -1
  77. package/dist/llms.txt +1 -1
  78. package/dist/marketplace.d.ts +77 -0
  79. package/dist/marketplace.js +224 -0
  80. package/dist/mcp-api-only-origins.d.ts +30 -0
  81. package/dist/mcp-api-only-origins.js +57 -0
  82. package/dist/mcp-gateway.d.ts +7 -5
  83. package/dist/mcp-gateway.js +9 -12
  84. package/dist/mcp-secret-writes.js +5 -3
  85. package/dist/mcp-servers-store.d.ts +28 -6
  86. package/dist/mcp-servers-store.js +53 -4
  87. package/dist/media-duration.d.ts +3 -0
  88. package/dist/media-duration.js +90 -9
  89. package/dist/model-form.d.ts +5 -4
  90. package/dist/model-form.js +11 -7
  91. package/dist/model.d.ts +5 -2
  92. package/dist/model.js +9 -5
  93. package/dist/ollama-provider.d.ts +7 -1
  94. package/dist/ollama-provider.js +14 -2
  95. package/dist/openapi.js +178 -27
  96. package/dist/otel/genai.d.ts +65 -0
  97. package/dist/otel/genai.js +116 -0
  98. package/dist/otel/tracer.d.ts +55 -0
  99. package/dist/otel/tracer.js +308 -0
  100. package/dist/page.d.ts +2 -0
  101. package/dist/page.js +3 -1
  102. package/dist/paid-calls.d.ts +1 -32
  103. package/dist/paid-calls.js +4 -39
  104. package/dist/provider.d.ts +9 -0
  105. package/dist/provider.js +50 -3
  106. package/dist/redact/match.d.ts +30 -3
  107. package/dist/redact/match.js +422 -41
  108. package/dist/refusal-words.d.ts +11 -0
  109. package/dist/refusal-words.js +11 -0
  110. package/dist/run-census.d.ts +67 -0
  111. package/dist/run-census.js +240 -0
  112. package/dist/run-computer.d.ts +6 -3
  113. package/dist/run-computer.js +4 -1
  114. package/dist/run-cut.d.ts +15 -6
  115. package/dist/run-cut.js +30 -9
  116. package/dist/run-files.d.ts +3 -0
  117. package/dist/run-files.js +3 -0
  118. package/dist/run-launch.d.ts +12 -1
  119. package/dist/run-launch.js +9 -10
  120. package/dist/run-learning-served.d.ts +2 -0
  121. package/dist/run-model-key.d.ts +8 -0
  122. package/dist/run-model-key.js +31 -1
  123. package/dist/run-reads-over-store.d.ts +24 -6
  124. package/dist/run-reads-over-store.js +67 -23
  125. package/dist/run-recordings.d.ts +5 -1
  126. package/dist/run-recordings.js +39 -14
  127. package/dist/run-timeline-served.d.ts +2 -0
  128. package/dist/run-timeline-served.js +2 -2
  129. package/dist/run-usage.d.ts +1 -0
  130. package/dist/runnable.d.ts +10 -1
  131. package/dist/runnable.js +20 -8
  132. package/dist/runner/adapter-loop.d.ts +11 -0
  133. package/dist/runner/adapter-loop.js +55 -10
  134. package/dist/runner/adapter-run-spec.d.ts +45 -8
  135. package/dist/runner/adapter-run-spec.js +150 -28
  136. package/dist/runner/adapters/pi.js +25 -3
  137. package/dist/runner/beside.js +4 -3
  138. package/dist/runner/big-writes.js +1 -10
  139. package/dist/runner/claim-secrets.d.ts +18 -3
  140. package/dist/runner/claim-secrets.js +35 -6
  141. package/dist/runner/cli-run-spec.d.ts +6 -0
  142. package/dist/runner/cli-run-spec.js +82 -35
  143. package/dist/runner/computer-journal.d.ts +9 -1
  144. package/dist/runner/computer-journal.js +26 -0
  145. package/dist/runner/conformance/harness.js +1 -1
  146. package/dist/runner/feedback-round.js +3 -0
  147. package/dist/runner/harness-loops.d.ts +6 -5
  148. package/dist/runner/harness-loops.js +7 -7
  149. package/dist/runner/held.d.ts +11 -5
  150. package/dist/runner/journal-kinds.d.ts +5 -0
  151. package/dist/runner/journal-kinds.js +5 -0
  152. package/dist/runner/main.js +7 -2
  153. package/dist/runner/mcp-claim.js +30 -3
  154. package/dist/runner/mcp-run.d.ts +12 -0
  155. package/dist/runner/mcp-run.js +12 -5
  156. package/dist/runner/model-route.d.ts +17 -0
  157. package/dist/runner/model-route.js +120 -2
  158. package/dist/runner/run-child-host.d.ts +3 -0
  159. package/dist/runner/run-child-host.js +69 -28
  160. package/dist/runner/run-child.js +5 -2
  161. package/dist/runner/runner.d.ts +14 -3
  162. package/dist/runner/runner.js +129 -33
  163. package/dist/runner/store-port.d.ts +1 -1
  164. package/dist/runner/store-secrets.d.ts +6 -0
  165. package/dist/runner/store-secrets.js +49 -7
  166. package/dist/runner/subagent.js +4 -40
  167. package/dist/runner/workflow-first.d.ts +11 -4
  168. package/dist/runner/workflow-first.js +14 -8
  169. package/dist/runner/workflow-spec.js +3 -2
  170. package/dist/runs-over-store.d.ts +15 -6
  171. package/dist/runs-over-store.js +33 -21
  172. package/dist/scrub-command.d.ts +73 -0
  173. package/dist/scrub-command.js +307 -0
  174. package/dist/secret-cut.d.ts +73 -0
  175. package/dist/secret-cut.js +291 -4
  176. package/dist/secret-rules.d.ts +143 -0
  177. package/dist/secret-rules.js +2564 -0
  178. package/dist/secrets.d.ts +7 -0
  179. package/dist/secrets.js +20 -0
  180. package/dist/server-keys.d.ts +20 -0
  181. package/dist/server-keys.js +25 -6
  182. package/dist/server.d.ts +52 -11
  183. package/dist/server.js +345 -118
  184. package/dist/skills/agentrun-serve/SKILL.md +4 -0
  185. package/dist/store/index.d.ts +1 -1
  186. package/dist/store/index.js +1 -1
  187. package/dist/store/journal/storage.js +1 -1
  188. package/dist/store/postgres/migrations/0027_run_launch_census.sql +31 -0
  189. package/dist/store/schema.d.ts +6 -0
  190. package/dist/store/schema.js +11 -1
  191. package/dist/store/seal.d.ts +6 -1
  192. package/dist/store/seal.js +5 -3
  193. package/dist/store/sqlite/run-launch.d.ts +9 -1
  194. package/dist/store/sqlite/run-launch.js +24 -13
  195. package/dist/store/sqlite/run-patch.d.ts +1 -1
  196. package/dist/store/sqlite/run-patch.js +13 -4
  197. package/dist/store/sqlite/store.js +34 -4
  198. package/dist/store/store.d.ts +43 -2
  199. package/dist/store/store.js +20 -0
  200. package/dist/store-reads/learning.d.ts +6 -3
  201. package/dist/store-reads/learning.js +22 -7
  202. package/dist/store-view.d.ts +3 -0
  203. package/dist/store-view.js +3 -3
  204. package/dist/suggestions.js +3 -2
  205. package/dist/template-eval.d.ts +20 -9
  206. package/dist/template-eval.js +115 -33
  207. package/dist/template-import.d.ts +10 -0
  208. package/dist/template-import.js +16 -7
  209. package/dist/templates.d.ts +10 -2
  210. package/dist/templates.js +28 -8
  211. package/dist/timeline/labels.d.ts +6 -4
  212. package/dist/timeline/labels.js +18 -6
  213. package/dist/timeline/timeline.d.ts +5 -0
  214. package/dist/timeline/timeline.js +14 -8
  215. package/dist/timeline/util.js +1 -1
  216. package/dist/timeline/web-calls.d.ts +26 -9
  217. package/dist/timeline/web-calls.js +124 -37
  218. package/dist/tool-gate.d.ts +42 -3
  219. package/dist/tool-gate.js +66 -5
  220. package/dist/tool-keys-store.d.ts +2 -4
  221. package/dist/tool-keys-store.js +5 -21
  222. package/dist/tool-keys.d.ts +2 -2
  223. package/dist/tool-keys.js +2 -2
  224. package/dist/tool-list.js +1 -1
  225. package/dist/tool-plane.d.ts +2 -2
  226. package/dist/tool-plane.js +35 -33
  227. package/dist/tools.js +13 -7
  228. package/dist/trigger-targets.d.ts +4 -0
  229. package/dist/trigger-targets.js +9 -2
  230. package/dist/triggers.d.ts +67 -4
  231. package/dist/triggers.js +171 -22
  232. package/dist/until.d.ts +3 -0
  233. package/dist/until.js +11 -0
  234. package/dist/web-fake.d.ts +1 -3
  235. package/dist/web-fake.js +7 -18
  236. package/dist/web-fetch.d.ts +6 -72
  237. package/dist/web-fetch.js +11 -265
  238. package/dist/web-run.d.ts +31 -0
  239. package/dist/web-run.js +90 -0
  240. package/dist/web-save.d.ts +6 -4
  241. package/dist/web-save.js +9 -12
  242. package/dist/web-search.d.ts +9 -127
  243. package/dist/web-search.js +23 -259
  244. package/dist/wires.d.ts +13 -0
  245. package/dist/wires.js +11 -0
  246. package/dist/workflow-run.d.ts +5 -4
  247. package/dist/workflow-run.js +5 -4
  248. package/dist/workspace/computer-deps.d.ts +2 -1
  249. package/dist/workspace/computer-deps.js +9 -2
  250. package/dist/workspace/computer-for-claim.d.ts +12 -1
  251. package/dist/workspace/computer-for-claim.js +77 -8
  252. package/dist/workspace/computer-kept.d.ts +3 -0
  253. package/dist/workspace/computer-kept.js +11 -2
  254. package/dist/workspace/index.d.ts +1 -1
  255. package/dist/workspace/index.js +1 -1
  256. package/dist/workspace/kind-skills.d.ts +4 -0
  257. package/dist/workspace/kind-skills.js +9 -0
  258. package/dist/workspace/synced-folder.js +3 -4
  259. package/dist/wrapped.d.ts +13 -5
  260. package/dist/wrapped.js +52 -26
  261. package/package.json +17 -11
  262. package/skills/agentrun-serve/SKILL.md +4 -0
  263. package/templates/README.md +30 -8
  264. package/dist/console/assets/index-CQ3cJRzI.js +0 -746
  265. package/dist/console/assets/index-DKkaS3a0.css +0 -1
  266. package/dist/exa-keyless.d.ts +0 -34
  267. package/dist/exa-keyless.js +0 -118
  268. package/dist/firecrawl-keyless.d.ts +0 -41
  269. package/dist/firecrawl-keyless.js +0 -152
  270. package/dist/keyless-mcp.d.ts +0 -36
  271. package/dist/keyless-mcp.js +0 -127
  272. package/dist/text-urls.d.ts +0 -38
  273. package/dist/text-urls.js +0 -170
  274. package/dist/web-fetch-direct.d.ts +0 -119
  275. package/dist/web-fetch-direct.js +0 -190
package/dist/triggers.js CHANGED
@@ -25,13 +25,15 @@
25
25
  // now. A failed firing queues a trigger.failed webhook message.
26
26
  // With the launch hook set (trigger-launch-hook.ts), a firing that would start a run goes the same way up to the start,
27
27
  // then is handed to the host instead: sent when the host takes it, a failed try (hook_failed and why) when it does not.
28
+ // A host that took a firing and then starts no run for it (its own job failed at launch) releases it: failed at once,
29
+ // host_released, so its trigger's room is free for the next firing without waiting out start_by.
28
30
  import { createHash, randomBytes } from "node:crypto";
29
31
  import { TRIGGER_LABELS } from "./timeline/index.js";
30
32
  import { agentDeleting, purgedAgent } from "./agents.js";
31
33
  import { ownedElsewhere } from "./agents-store.js";
32
34
  import { ApiError, invalid } from "./errors.js";
33
35
  import { EFFORTS } from "./model.js";
34
- import { AgentArchived, StoreConstraint, TriggerFull } from "./store/index.js";
36
+ import { AgentArchived, FiringMoved, StoreConstraint, TriggerFull } from "./store/index.js";
35
37
  import { carriesPrevious, PAYLOAD_KEPT_BYTES, second, validZone } from "./trigger-adapter.js";
36
38
  import { TRIGGER_ADAPTERS } from "./trigger-adapters.js";
37
39
  import { handToHost, HookFailed, LAUNCH_HOOK_LATE_START_MS } from "./trigger-launch-hook.js";
@@ -53,6 +55,17 @@ export const FIRING_STATUSES = ["due", "started", "sent", "skipped", "failed", "
53
55
  /** The reason of a firing the launch hook's host took (a 2xx) without starting its run here under the firing (RunCreate
54
56
  * trigger_firing). */
55
57
  export const HANDED_NO_RUN = "Handed to the host by the launch hook, which started no run here for it.";
58
+ /** The error code of a firing its launch hook's host released (Triggers.release): it took the firing (a 2xx) and said it
59
+ * will start no run for it. */
60
+ export const HOST_RELEASED = "host_released";
61
+ /** The reason of such a firing; its error's message is the host's own words. */
62
+ export const HOST_RELEASED_REASON = "Released by the launch hook's host, which took it and will start no run for it.";
63
+ /** The `details.reason` of a release refused because the firing's run started (Triggers.release), beside `details.run_id`:
64
+ * for a host to match without reading the words. A start refused because its host released the firing carries
65
+ * HOST_RELEASED there. */
66
+ export const RUN_STARTED = "run_started";
67
+ /** The most characters of the host's words a release keeps. */
68
+ export const RELEASE_WORDS_MAX = 500;
56
69
  /** The most of a webhook's payload a firing passes on: the rest is cut, at a character's start (trigger-adapter.ts). */
57
70
  /** How late a missed time may be and still fire once, by default: an hour. */
58
71
  export const CATCHUP_WINDOW_S = 3600;
@@ -218,6 +231,14 @@ const failedReason = (error) => (error instanceof HookFailed ? error.message : u
218
231
  /** A firing its launch hook's host took (a 2xx) with no run yet: sent with no run, and a startBy, which only such a
219
232
  * firing has. startBy is kept in the retry_at column: no column of its own, no migration. */
220
233
  const tookWithNoRun = (firing) => firing.status === "sent" && firing.runId === null && firing.startBy !== null;
234
+ /** Whether firing `firing` is one its launch hook's host released (Triggers.release). */
235
+ const released = (firing) => firing.status === "failed" && firing.error?.code === HOST_RELEASED;
236
+ /** The refusal of a host start of firing `firing`, which its host released: its words, and `details.reason`
237
+ * HOST_RELEASED for a host to match. */
238
+ const releasedRefusal = (firing) => {
239
+ const message = `Firing ${firing.key} starts no run: its host released it: ${firing.error?.message ?? ""}`;
240
+ return new ApiError("input_invalid", message, { fields: [{ field: "trigger_firing.firing_id", reason: "not_due", message }], details: { reason: HOST_RELEASED } });
241
+ };
221
242
  /** How long a host start of such a firing holds its trigger's room from its check, at least: a startBy sooner than this
222
243
  * is pushed out to it before the run is written (handedStart), so the window never closes while the run is written. */
223
244
  const START_WINDOW_MS = 60_000;
@@ -715,16 +736,17 @@ export class Triggers {
715
736
  for (const trigger of due)
716
737
  if (!unreadable.includes(trigger))
717
738
  await this.advance(trigger, now);
739
+ // One its host's start is writing is that start's until it ends, as in arm (startHold arms again if it wrote no run).
718
740
  for (const firing of await this.store.triggers.pending()) {
719
741
  if (firing.status === "failed")
720
742
  await this.notifyFailed(firing.triggerId, firing.key);
721
- else if (firing.waitsOn === null && !this.driving.has(firing.key) && (firing.retryAt === null || firing.retryAt <= now))
743
+ else if (firing.waitsOn === null && !this.driving.has(firing.key) && !this.starting.has(firing.key) && (firing.retryAt === null || firing.retryAt <= now))
722
744
  void this.inTurn(firing.triggerId, firing.key);
723
745
  }
724
746
  const waiting = await this.store.triggers.waiting(now);
725
747
  this.waiting = waiting.length > 0;
726
748
  for (const firing of waiting)
727
- if (firing.free && !this.driving.has(firing.key))
749
+ if (firing.free && !this.driving.has(firing.key) && !this.starting.has(firing.key))
728
750
  void this.inTurn(firing.triggerId, firing.key);
729
751
  }
730
752
  catch (error) {
@@ -1043,8 +1065,11 @@ export class Triggers {
1043
1065
  const archived = await this.archivedSkip(trigger.agentId);
1044
1066
  if (this.closed)
1045
1067
  return;
1068
+ // A host start since the reads above owns the firing: still writing here, or done (past due, so from: "due" holds).
1069
+ if (this.starting.has(key))
1070
+ return;
1046
1071
  if (archived)
1047
- return void (await this.store.triggers.settle(triggerId, key, { status: "skipped", reason: archived }));
1072
+ return void (await this.store.triggers.settle(triggerId, key, { status: "skipped", reason: archived, from: "due" }));
1048
1073
  if (trigger.target.kind === "message_run")
1049
1074
  return void (await this.send(trigger, firing));
1050
1075
  // Room found is held (handing) until what came of it is written: a host start meanwhile counts this firing. The
@@ -1061,7 +1086,7 @@ export class Triggers {
1061
1086
  if (this.closed || "gone" in claimed)
1062
1087
  return;
1063
1088
  if ("skipped" in claimed)
1064
- return void (await this.store.triggers.settle(triggerId, key, { status: "skipped", reason: claimed.skipped }));
1089
+ return void (await this.store.triggers.settle(triggerId, key, { status: "skipped", reason: claimed.skipped, from: "due" }));
1065
1090
  return void (await this.wait(triggerId, key, claimed.waitsOn, claimed.reason));
1066
1091
  }
1067
1092
  try {
@@ -1121,7 +1146,7 @@ export class Triggers {
1121
1146
  const { target } = trigger;
1122
1147
  if (!(await this.store.runs.get(target.run_id))) {
1123
1148
  await this.store.triggers.update(trigger.id, { enabled: false, nextAt: null, disabled: { at: iso(this.clock.now()), reason: "source_unreadable", detail: RUN_DELETED } });
1124
- await this.store.triggers.settle(trigger.id, firing.key, { status: "skipped", reason: RUN_DELETED });
1149
+ await this.store.triggers.settle(trigger.id, firing.key, { status: "skipped", reason: RUN_DELETED, from: "due" });
1125
1150
  this.deps.say({ condition: "trigger_source_unreadable", trigger: trigger.id, error: RUN_DELETED });
1126
1151
  await this.offThere([trigger]);
1127
1152
  return;
@@ -1139,8 +1164,8 @@ export class Triggers {
1139
1164
  if (this.closed)
1140
1165
  return;
1141
1166
  if (skipped)
1142
- return void (await this.store.triggers.settle(trigger.id, firing.key, { status: "skipped", reason: skipped }));
1143
- await this.store.triggers.settle(trigger.id, firing.key, { status: "sent", runId: target.run_id }, { reset: true });
1167
+ return void (await this.store.triggers.settle(trigger.id, firing.key, { status: "skipped", reason: skipped, from: "due" }));
1168
+ await this.store.triggers.settle(trigger.id, firing.key, { status: "sent", runId: target.run_id, from: "due" }, { reset: true });
1144
1169
  }
1145
1170
  /** The firing kept due, waiting for its trigger's room (no try counted, no retry time): a tick drives it once the store
1146
1171
  * says a run of its trigger ended. */
@@ -1311,13 +1336,29 @@ export class Triggers {
1311
1336
  let run;
1312
1337
  try {
1313
1338
  const attrs = room.firing.attrs;
1314
- run = await start({
1315
- key, trigger: { id: triggerId, firing_key: key, name: trigger.name, ...(attrs && Object.keys(attrs).length ? { attrs } : {}) }, from: this.originOf(trigger), title: this.titleOf(trigger, room.firing),
1316
- target: trigger.target, previous: carriesPrevious(this.adapter(trigger.source)),
1317
- });
1339
+ try {
1340
+ run = await start({
1341
+ key, trigger: { id: triggerId, firing_key: key, name: trigger.name, ...(attrs && Object.keys(attrs).length ? { attrs } : {}) }, from: this.originOf(trigger), title: this.titleOf(trigger, room.firing),
1342
+ target: trigger.target, previous: carriesPrevious(this.adapter(trigger.source)),
1343
+ });
1344
+ }
1345
+ catch (error) {
1346
+ // The run's write found the firing moved since its room was checked (its host released it, on any server):
1347
+ // nothing was written, and the start is refused in words that say what it is now.
1348
+ if (error instanceof FiringMoved)
1349
+ throw await this.movedRefusal(triggerId, key);
1350
+ throw error;
1351
+ }
1318
1352
  room.ran(run);
1319
1353
  try {
1320
- await this.handedRan(triggerId, key, run.run_id);
1354
+ // The run's write started the firing (store.runs.create `firing`): said here. One a start wrote without it is
1355
+ // started by handedRan, which says so. A firing released before that (a run written apart from it, by a server
1356
+ // from before the one write, that a release beat to the firing) stays released: the run is answered, never
1357
+ // refused, since it exists, and the log says it ran after the release.
1358
+ if (!(await this.handedRan(triggerId, key, run.run_id))) {
1359
+ const now = await this.store.triggers.firing(triggerId, key);
1360
+ this.deps.say({ condition: now && released(now) ? "trigger_run_after_release" : "trigger_fired", trigger: triggerId, firing: key, run: run.run_id });
1361
+ }
1321
1362
  }
1322
1363
  catch (error) {
1323
1364
  // Its run is written but the firing's start is not: the firing's next turn starts it with the run under its key,
@@ -1393,6 +1434,8 @@ export class Triggers {
1393
1434
  const firing = await this.store.triggers.firing(trigger.id, key);
1394
1435
  if (!firing)
1395
1436
  throw refused("not_found", `Trigger ${trigger.id} has no firing ${key}.`);
1437
+ if (released(firing))
1438
+ throw releasedRefusal(firing);
1396
1439
  const now = this.clock.now();
1397
1440
  const minutes = LAUNCH_HOOK_LATE_START_MS / 60_000;
1398
1441
  if (tookWithNoRun(firing)) {
@@ -1415,11 +1458,111 @@ export class Triggers {
1415
1458
  /** Run `runId`, which the host started for firing `key` (handedStart), started: a firing with no run yet, still due
1416
1459
  * (being handed, or waiting on its retry after the hook failed) or one its host took first, is made started here, as
1417
1460
  * handed() makes it when the run came first, so the overlap check, max_active_runs and last_run_id count it. A failed
1418
- * or settled firing is left as it is. */
1461
+ * or settled firing is left as it is, and so is one that moved between this read and the write (the write is only
1462
+ * while it is still in the status read, with no run: a release that failed it meanwhile stands). Whether it started
1463
+ * it here. */
1419
1464
  async handedRan(triggerId, key, runId) {
1420
1465
  const firing = await this.store.triggers.firing(triggerId, key);
1421
- if (firing && (firing.status === "due" ? firing.runId === null : tookWithNoRun(firing)))
1422
- await this.started(triggerId, key, runId);
1466
+ if (!firing || !(firing.status === "due" ? firing.runId === null : tookWithNoRun(firing)))
1467
+ return false;
1468
+ return this.started(triggerId, key, runId, { from: firing.status, noRun: true });
1469
+ }
1470
+ /** The refusal of a host start of firing `key` whose run's write found the firing moved (FiringMoved): released by
1471
+ * its host, or another not_due naming what it is now. */
1472
+ async movedRefusal(triggerId, key) {
1473
+ const firing = await this.store.triggers.firing(triggerId, key);
1474
+ if (firing && released(firing))
1475
+ return releasedRefusal(firing);
1476
+ const now = firing ? `${firing.status}${firing.reason ? ` (${firing.reason})` : ""}` : "gone";
1477
+ return invalid([{ field: "trigger_firing.firing_id", reason: "not_due", message: `Firing ${key} moved while its run was written, and is ${now}: no run started for it.` }]);
1478
+ }
1479
+ /** Firing `key` of trigger `triggerId` released by the host the launch hook handed it to: the host answered it 2xx and
1480
+ * says it will start no run for it (its job failed before any run started). `body` is `{ error }`, the host's words
1481
+ * (1 to RELEASE_WORDS_MAX characters), which `cut` takes the server's secrets out of before they are kept. A firing
1482
+ * sent with no run, before or after its start_by, fails at once: host_released with those words, one failure in its
1483
+ * trigger's streak and a trigger.failed, as any failed firing. Its trigger's room is free from that write, so a firing
1484
+ * waiting on it is driven on the next tick. The same call on a firing it released already answers that firing as it
1485
+ * is. Refused in words: 422 not_allowed on a server with no launch hook, not_due for a firing whose run started, one
1486
+ * still due once the engine's turn on it ends (not handed yet) or any other, with `details` { reason: RUN_STARTED,
1487
+ * run_id } when its run started; 404 for a trigger or firing this server does not have. A release during the engine's
1488
+ * own turn on the firing (its hook's answer read but not yet written) waits for that turn and reads the firing again,
1489
+ * so a 2xx it writes is released. A host start of the firing already writing its run on this server is waited for
1490
+ * first: a run it writes makes the firing started, and refused. Any other start, on this server (its room check
1491
+ * landing after that wait) or another, is decided by the store: its run's write and this release's are each
1492
+ * conditional on the firing being sent with no run, so the one that lands second finds it moved (a start, FiringMoved;
1493
+ * this release, the firing read again and refused). */
1494
+ async release(triggerId, key, body, cut) {
1495
+ if (!this.deps.launchHook)
1496
+ throw invalid([{ field: "firing_id", reason: "not_allowed", message: "This server has no launch hook: only a firing the hook handed to its host is released by that host." }]);
1497
+ await this.need(triggerId);
1498
+ const issues = unknownFields(body, ["error"]);
1499
+ const words = typeof body.error === "string" ? body.error.trim() : "";
1500
+ if (!words || words.length > RELEASE_WORDS_MAX)
1501
+ issues.unshift({ field: "error", reason: typeof body.error === "string" ? "invalid" : "wrong_type", message: `error is why the host starts no run for the firing, in plain words: 1 to ${RELEASE_WORDS_MAX} characters.` });
1502
+ if (issues.length)
1503
+ throw invalid(issues);
1504
+ const message = cut(words);
1505
+ for (;;) {
1506
+ const starting = this.starting.get(key);
1507
+ if (starting) {
1508
+ const run = await starting;
1509
+ if (run)
1510
+ throw this.ranRefusal(key, run.run_id);
1511
+ continue;
1512
+ }
1513
+ // The engine's own turn on the firing may have its hook's 2xx and not have written it yet: read once that turn ends.
1514
+ const turn = this.driving.get(key);
1515
+ if (turn) {
1516
+ this.deps.say({ condition: "trigger_release_waits", trigger: triggerId, firing: key });
1517
+ await turn.catch(() => { });
1518
+ continue;
1519
+ }
1520
+ const found = await this.checked(triggerId, () => this.releasable(triggerId, key));
1521
+ if ("firing" in found)
1522
+ return firingView(found.firing);
1523
+ if ("ran" in found) {
1524
+ await this.handedRan(triggerId, key, found.ran);
1525
+ throw this.ranRefusal(key, found.ran);
1526
+ }
1527
+ // Written out of the room checks, as drive writes what its check found (a check never waits on a hook, and a failed
1528
+ // firing's trigger.failed and auto-pause call out): only while the firing is still sent with no run and its start_by
1529
+ // is the one read, so a start that moved it meanwhile (on any server) leaves it as it is, and it is read again.
1530
+ const failed = await this.failed(triggerId, key, { code: HOST_RELEASED, message }, undefined, HOST_RELEASED_REASON, { from: "sent", startByWas: found.startBy });
1531
+ if (!failed)
1532
+ continue;
1533
+ this.deps.say({ condition: "trigger_firing_released", trigger: triggerId, firing: key });
1534
+ // Its room is free now: a firing waiting on it is driven on the next tick, not at start_by.
1535
+ void this.arm();
1536
+ return firingView(failed);
1537
+ }
1538
+ }
1539
+ /** What a release of firing `key` finds, read within its trigger's room checks (release): the firing, released already;
1540
+ * the run already under its key (`ran`, linked or not: a stop between its write and handedRan); or the start_by of a
1541
+ * firing its host took with no run, which the release then fails. Any other is refused in words. */
1542
+ async releasable(triggerId, key) {
1543
+ const firing = await this.store.triggers.firing(triggerId, key);
1544
+ if (!firing)
1545
+ throw new ApiError("not_found", `Trigger ${triggerId} has no firing ${key}.`);
1546
+ if (released(firing))
1547
+ return { firing };
1548
+ const ran = firing.status === "started" ? firing.runId : firing.status === "due" || tookWithNoRun(firing) ? (await this.deps.runOf(key))?.run_id : undefined;
1549
+ if (ran)
1550
+ return { ran };
1551
+ if (firing.status === "due")
1552
+ throw this.notDue(key, "it has not been handed to the launch hook's host yet");
1553
+ if (!tookWithNoRun(firing))
1554
+ throw this.notDue(key, `it is ${firing.status}${firing.reason ? ` (${firing.reason})` : ""}, not a firing the launch hook's host took with no run`);
1555
+ return { startBy: firing.startBy };
1556
+ }
1557
+ /** A release refused for firing `key`, `why` saying what it is. */
1558
+ notDue(key, why) {
1559
+ return invalid([{ field: "firing_id", reason: "not_due", message: `Firing ${key} is not released: ${why}.` }]);
1560
+ }
1561
+ /** A release refused for firing `key` because its run `runId` started: in words, and in `details` (RUN_STARTED and the
1562
+ * run) for a host to match. Never retryable: the run stays started. */
1563
+ ranRefusal(key, runId) {
1564
+ const refused = this.notDue(key, `its run ${runId} started`);
1565
+ return new ApiError(refused.code, refused.message, { ...refused.extra, details: { reason: RUN_STARTED, run_id: runId } });
1423
1566
  }
1424
1567
  /** Each sent firing whose host's run is in the store under its key, though the server stopped before the firing was
1425
1568
  * started with it (a late start writes the run, then handedRan): started with that run, so the overlap check and
@@ -1439,22 +1582,28 @@ export class Triggers {
1439
1582
  }
1440
1583
  }
1441
1584
  }
1442
- async started(triggerId, key, runId) {
1443
- await this.store.triggers.settle(triggerId, key, { status: "started", runId }, { reset: true });
1585
+ /** Firing `key` started with run `runId`, as `only` allows (FiringChange's guards); said when written. Whether it was. */
1586
+ async started(triggerId, key, runId, only = {}) {
1587
+ if (!(await this.store.triggers.settle(triggerId, key, { status: "started", runId, ...only }, { reset: true })))
1588
+ return false;
1444
1589
  this.deps.say({ condition: "trigger_fired", trigger: triggerId, firing: key, run: runId });
1590
+ return true;
1445
1591
  }
1446
1592
  /** A failed firing: one more in its trigger's streak, the trigger turned off at FAILURES_TO_PAUSE, and trigger.failed
1447
1593
  * queued (after the firing is written: a start finds one not yet queued and queues it, under the same webhook-id).
1448
- * Failed only while it is due: one its host's run started meanwhile is left started, with no failure and no message. */
1449
- async failed(triggerId, key, error, runId, reason) {
1450
- const settled = await this.store.triggers.settle(triggerId, key, { status: "failed", error, ...(runId ? { runId } : {}), ...(reason ? { reason } : {}), from: "due" }, { failure: true, pauseAt: FAILURES_TO_PAUSE });
1594
+ * Failed only while it is due (or as `only` says: a release's, while it is sent with no run and its start_by unmoved):
1595
+ * one its host's run started meanwhile is left started, with no failure and no message. The firing as written, or
1596
+ * undefined when nothing was. */
1597
+ async failed(triggerId, key, error, runId, reason, only = { from: "due" }) {
1598
+ const settled = await this.store.triggers.settle(triggerId, key, { status: "failed", error, ...(runId ? { runId } : {}), ...(reason ? { reason } : {}), ...only }, { failure: true, pauseAt: FAILURES_TO_PAUSE });
1451
1599
  if (!settled)
1452
- return;
1600
+ return undefined;
1453
1601
  await this.notifyFailed(triggerId, key);
1454
1602
  if (settled.paused) {
1455
1603
  this.deps.say({ condition: "trigger_auto_paused", trigger: triggerId, firing: key });
1456
1604
  await this.pausedThere(triggerId);
1457
1605
  }
1606
+ return settled.firing;
1458
1607
  }
1459
1608
  /** Trigger `triggerId`, turned off by its failure streak: off at its sender too (offThere). */
1460
1609
  async pausedThere(triggerId) {
@@ -0,0 +1,3 @@
1
+ /** `work`'s answer, or `signal`'s reason as soon as it aborts: a wait shared by callers (a connection, a piece being
2
+ * pulled) is never held by one that stopped. */
3
+ export declare function until<T>(work: Promise<T>, signal: AbortSignal): Promise<T>;
package/dist/until.js ADDED
@@ -0,0 +1,11 @@
1
+ /** `work`'s answer, or `signal`'s reason as soon as it aborts: a wait shared by callers (a connection, a piece being
2
+ * pulled) is never held by one that stopped. */
3
+ export function until(work, signal) {
4
+ if (signal.aborted)
5
+ return Promise.reject(signal.reason);
6
+ return new Promise((resolve, reject) => {
7
+ const abort = () => reject(signal.reason);
8
+ signal.addEventListener("abort", abort, { once: true });
9
+ work.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
10
+ });
11
+ }
@@ -1,6 +1,4 @@
1
- import { type Page, type Part } from "./web-fetch-direct.js";
2
- import { type WebPage } from "./web-fetch.js";
3
- import { type Hit, type Search } from "./web-search.js";
1
+ import { type Hit, type Page, type Part, type Search, type WebPage } from "@parcha/agentrun-web";
4
2
  import type { Plane } from "./tool-plane.js";
5
3
  import type { PublicFetchOptions } from "@parcha/pi-browser";
6
4
  export type SearchAnswer = {
package/dist/web-fake.js CHANGED
@@ -1,20 +1,8 @@
1
- // A template eval's fake of the web tools for one case (template-eval.ts, `web:` in a case's case.yaml): its runs'
2
- // web_search and web_fetch answer from the case, never from the network, in the shapes and through the tool names the
3
- // model sees live. `unavailable`: every search fails as keyless search past its free limit does (keyless-mcp.ts
4
- // RATE_LIMITED), and every page fails as a fetch whose connection is refused does (the guarded direct fetch's own words,
5
- // web-fetch-direct.ts readDirect). A fixture: each search answered by the first answer whose `query` (a regular
6
- // expression, case-insensitive) matches the query, else its default; each page by the answer for exactly its URL, else
7
- // its default, read through the guarded direct fetch's own path (its URL, port and status checks, its parts). web_save's
8
- // files (web-save.ts) answer from the fixture's optional `save`: each file by the answer for exactly its URL, else its
9
- // default, else a refused connection, downloaded through web_save's own guarded path (every hop checked, the cap, the type
10
- // checked against the bytes); `unavailable` refuses every connection.
11
- //
12
- // Only the eval's own in-process runner binds it (runner/beside.ts `evalFakes`, by the agent the eval made for the run):
13
- // no package field, API field, setting or environment variable reaches it, so no other server's runs ever see a fake.
14
- import { RATE_LIMITED } from "./keyless-mcp.js";
15
- import { clip, partProblem, TITLE_CAP, URL_MAX } from "./web-fetch-direct.js";
16
- import { webFetch } from "./web-fetch.js";
17
- import { searchArgs, snippetOf } from "./web-search.js";
1
+ // A template eval's fake of the web tools for one case (`web:` in its case.yaml): searches, pages and web_save's files
2
+ // answer from the fixture, in the tools' own shapes and through the guarded paths' own checks. Only the eval's own
3
+ // in-process runner binds it (runner/beside.ts), so no other server's runs ever see a fake.
4
+ import { GUARD, RATE_LIMITED } from "./web-run.js";
5
+ import { askedUrl, clip, partProblem, searchArgs, snippetOf, TITLE_CAP, webFetch } from "@parcha/agentrun-web";
18
6
  /** A public address the fake's lookup answers: the guard checks it is public, and no connection is ever made to it. */
19
7
  const PUBLIC_ADDRESS = "203.0.114.1";
20
8
  /** The statuses whose answer carries no body (a Response refuses one). */
@@ -96,10 +84,11 @@ export function fakeSearch(fake, args, provider) {
96
84
  export async function fakeFetch(fake, url, part = {}) {
97
85
  const problem = partProblem(part);
98
86
  if (problem)
99
- return { ok: false, url: typeof url === "string" ? clip(url, URL_MAX) : "", status: 0, title: "", text: "", links: [], error: problem };
87
+ return { ok: false, url: askedUrl(url), status: 0, title: "", text: "", links: [], error: problem };
100
88
  const answer = fake === "unavailable" ? undefined : (fake.fetch.answers.find((one) => one.url === url) ?? fake.fetch.default);
101
89
  const page = await webFetch(url, {
102
90
  ...part,
91
+ guard: GUARD,
103
92
  lookup: async () => [PUBLIC_ADDRESS],
104
93
  fetchImpl: async () => {
105
94
  // As undici's fetch rejects when a connection is refused: the guarded fetch says it in its own words.
@@ -1,79 +1,13 @@
1
1
  import type { JsonValue } from "@earendil-works/chord/json";
2
2
  import type { TSchema } from "typebox";
3
3
  import { type ToolRegistration } from "@earendil-works/pi-durable";
4
- import type { PublicFetchOptions } from "@parcha/pi-browser";
5
- import { type Page, type Part, type Read, type Unread } from "./web-fetch-direct.js";
6
- import type { KeylessFetch } from "./firecrawl-keyless.js";
4
+ import { type FetchOptions, type Page, type Part, type WebPage } from "@parcha/agentrun-web";
7
5
  export declare const WEB_FETCH_TOOL = "web_fetch";
8
- export declare const PARALLEL_EXTRACT_URL = "https://api.parallel.ai/v1/extract";
9
- /** How many pages a run keeps what it read of (Pages): each at most PAGE_READ_MAX_CHARS characters of text. */
10
- export declare const PAGES_HELD = 8;
11
- /** `parallel`, on a page fetched directly where Parallel was asked for one: why Parallel gave none. */
12
- export type WebPage = Page & {
13
- via: "parallel" | "firecrawl-keyless" | "direct" | "stopped";
14
- parallel?: string;
15
- };
16
- /** A page web_fetch read, and how (WebPage's `via` and `parallel`). */
17
- export type WebRead = Read & Pick<WebPage, "via" | "parallel">;
18
- /** A page web_fetch could not read, and how it tried. */
19
- type WebUnread = Unread & Pick<WebPage, "via" | "parallel">;
20
- /** The pages a run reads, by the URL asked for, each from its read's start (so calls at once share it), the last used
21
- * last: at most PAGES_HELD of them. */
22
- export type Pages = Map<string, Promise<WebRead | WebUnread>>;
23
- type GuardOptions = Omit<PublicFetchOptions, "maxBytes">;
24
- /** Parallel for one run, on one key: `read`, its extract of a page (one billed request), the page it read or, for any
25
- * failure, why it read none; `post`, one request to another of its endpoints (web_search's, web-search.ts). Neither
26
- * retries, and each stops at 60 s. `live` is false once a 401 or 403 to either said the key is not served. */
27
- export type Extract = {
28
- live(): boolean;
29
- read(url: string, signal?: AbortSignal): Promise<Read | string>;
30
- post(url: string, body: unknown, signal?: AbortSignal): Promise<Response>;
31
- };
32
- /** Parallel's extract on `apiKey`: one URL a request, each a billed extract, its full content to the most of a page
33
- * web_fetch reads as the page's text (markdown), its links read from it, status 200 (Parallel answers a page it could
34
- * not read in `errors`, with no content). */
35
- export declare function parallelExtract(apiKey: string, send?: typeof fetch): Extract;
36
- /** The part (`offset`, `maxChars`) of the page at `url` as a WebPage: from `pages`, the run's, where it holds the URL
37
- * (read, or being read), with no request of its own; else read once (readOnce), kept there from the read's start. A read
38
- * that fails leaves `pages`, so a later call reads again. The read is the run's, not a caller's: no caller's signal
39
- * reaches it (it keeps Parallel's and the fetch's own time limits), so one caller's stop never fails another's; each
40
- * caller stops waiting at its own signal (untilStopped). */
41
- export declare function webFetch(url: unknown, { parallel, firecrawl, pages, checkMs, offset, maxChars, signal, log, ...options }?: GuardOptions & Part & {
42
- parallel?: Extract | null;
43
- firecrawl?: KeylessFetch | null;
44
- pages?: Pages;
45
- checkMs?: number;
46
- log?: (line: string) => void;
47
- }): Promise<WebPage>;
48
- /** The pages the run whose calls are given `env` read (Pages): the agent's tool and a call node's web_fetch of that run
49
- * share them, another run's never. */
50
- export declare function pagesOf(env: NodeJS.ProcessEnv): Pages;
51
- /** The run's keyless Firecrawl scrape, made once for the environment its calls are given, so its breaker holds for the
52
- * rest of the run (firecrawlFetch). Under --faux it asks the fake whose URL FAUX_FIRECRAWL_ENV names, and with none
53
- * named it is null: a test never reaches the network. Its module loads at the first call (it reads web-search.ts,
54
- * which reads this module as it loads). */
55
- export declare function firecrawlOf(env: NodeJS.ProcessEnv): Promise<KeylessFetch | null>;
56
- /** The run's Parallel extract, made once for the environment its calls are given, so a refused key holds for the rest of
57
- * the run: on the Parallel key that environment holds (parallelKeyOf: the per-run pipe, or the claim's own), null
58
- * without one. */
59
- export declare function parallelOf(env: NodeJS.ProcessEnv): Extract | null;
60
- /** The part `options` names of the page at `url` by the provider the run's plane resolves web_fetch to: Parallel's
61
- * extract (with the direct fetch behind it) where it resolves to parallel, keyless Firecrawl's scrape (the same) where
62
- * it resolves to firecrawl-keyless, the guarded direct fetch otherwise, and a
63
- * failed Page saying why where it is unavailable or the part is one a call may not ask for (partProblem), before
64
- * anything is fetched. From `pages` (the run's, pagesOf) where they hold the URL. */
65
- export declare function fetchFor(env: NodeJS.ProcessEnv, url: unknown, options?: GuardOptions & Part & {
66
- pages?: Pages;
67
- }): Promise<WebPage | Page>;
68
- /** A page as the tool `name` answers the model: its JSON, then a line saying why Parallel gave none where it was picked,
69
- * and one saying where this part is in the page when it is not all of it. */
70
- export declare function webFetchText(page: WebPage | Page, name?: string): string;
71
- /** The agent's tool: the WebPage as JSON text (webFetchText), named `name`. A
72
- * run whose web_fetch resolves to Parallel reads its key as the tool is made, when the run opens. */
73
- /** Typed here, so its declaration names the schema type by a package serve declares (pi-ai), whatever else the build loads. */
74
- /** `fake`: what reads each page in the provider's place (a template eval's web fake, web-fake.ts), the tool otherwise
75
- * the same. */
6
+ /** The part `options` names of the page at `url`, through the run's Web; a failed Page saying why where web_fetch does
7
+ * not run here. A part a call may not ask for is refused first. */
8
+ export declare function fetchFor(env: NodeJS.ProcessEnv, url: unknown, options?: FetchOptions): Promise<WebPage | Page>;
9
+ /** The agent's tool, named `name`. A run whose web_fetch resolves to Parallel reads its key as the tool is made, when the
10
+ * run opens. `fake`: what reads each page in the provider's place (a template eval's web fake, web-fake.ts). */
76
11
  export declare function webFetchTool(env: NodeJS.ProcessEnv, name?: string, fake?: (url: unknown, part: Part & {
77
12
  signal?: AbortSignal;
78
13
  }) => Promise<WebPage | Page>): ToolRegistration<TSchema, JsonValue>;
79
- export {};