melchizedek-agents 0.18.0 → 0.20.0

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 (293) hide show
  1. package/.env.example +12 -0
  2. package/CHANGELOG.md +1001 -0
  3. package/DOCUMENTATION.md +252 -65
  4. package/QUICKSTART.md +9 -6
  5. package/README.md +42 -4
  6. package/config/agents/examples/harness.yaml +3 -1
  7. package/config/agents/examples/model_zoo.yaml +10 -9
  8. package/config/agents/syndicateSchema.yaml +53 -16
  9. package/config/agents/templates/account_memory.yaml +2 -6
  10. package/config/agents/templates/case_desk.yaml +3 -9
  11. package/config/agents/templates/draft_review.yaml +3 -9
  12. package/config/agents/templates/intake_extractor.yaml +1 -3
  13. package/config/agents/templates/research_brief.yaml +3 -9
  14. package/config/agents/templates/research_desk.yaml +3 -9
  15. package/config/agents/templates/review_panel.yaml +4 -12
  16. package/config/agents/templates/support_triage.yaml +4 -12
  17. package/config/agents/templates/systems_operator.yaml +2 -6
  18. package/db/hardening.sql +7 -1
  19. package/db/migrations/0013_tool_credentials.sql +260 -0
  20. package/dist/lib/a2a/app.d.ts +70 -6
  21. package/dist/lib/a2a/app.js +107 -2
  22. package/dist/lib/a2a/executor.d.ts +20 -3
  23. package/dist/lib/a2a/executor.js +66 -7
  24. package/dist/lib/a2a/remoteAgent.d.ts +24 -9
  25. package/dist/lib/a2a/remoteAgent.js +31 -14
  26. package/dist/lib/adkPeer.d.ts +63 -0
  27. package/dist/lib/adkPeer.js +240 -0
  28. package/dist/lib/compile.d.ts +159 -30
  29. package/dist/lib/compile.js +215 -134
  30. package/dist/lib/compileAdk.d.ts +32 -0
  31. package/dist/lib/compileAdk.js +142 -0
  32. package/dist/lib/compileNative.d.ts +111 -0
  33. package/dist/lib/compileNative.js +278 -0
  34. package/dist/lib/config.d.ts +6 -5
  35. package/dist/lib/config.js +6 -5
  36. package/dist/lib/doctor.d.ts +28 -1
  37. package/dist/lib/doctor.js +64 -2
  38. package/dist/lib/index.d.ts +6 -1
  39. package/dist/lib/index.js +7 -1
  40. package/dist/lib/loadSyndicate.d.ts +18 -0
  41. package/dist/lib/memory/erase.d.ts +5 -3
  42. package/dist/lib/memory/erase.js +5 -2
  43. package/dist/lib/memory/namespace.d.ts +8 -1
  44. package/dist/lib/memory/namespace.js +12 -6
  45. package/dist/lib/memory/providers.js +2 -1
  46. package/dist/lib/memory/supabaseMemoryService.d.ts +37 -3
  47. package/dist/lib/memory/supabaseMemoryService.js +42 -2
  48. package/dist/lib/model.d.ts +47 -0
  49. package/dist/lib/model.js +37 -0
  50. package/dist/lib/models/adapterResolver.d.ts +115 -0
  51. package/dist/lib/models/adapterResolver.js +137 -0
  52. package/dist/lib/models/adkGeminiAdapter.d.ts +90 -0
  53. package/dist/lib/models/adkGeminiAdapter.js +369 -0
  54. package/dist/lib/models/adkShim.d.ts +111 -0
  55. package/dist/lib/models/adkShim.js +136 -0
  56. package/dist/lib/models/capabilities.d.ts +3 -0
  57. package/dist/lib/models/capabilities.js +33 -23
  58. package/dist/lib/models/chatCompletionsAdapter.d.ts +279 -0
  59. package/dist/lib/models/chatCompletionsAdapter.js +854 -0
  60. package/dist/lib/models/circuitBreaker.d.ts +42 -0
  61. package/dist/lib/models/circuitBreaker.js +70 -0
  62. package/dist/lib/models/claudeAdapter.d.ts +103 -0
  63. package/dist/lib/models/claudeAdapter.js +669 -0
  64. package/dist/lib/models/claudeLlm.d.ts +39 -62
  65. package/dist/lib/models/claudeLlm.js +45 -350
  66. package/dist/lib/models/claudeModels.d.ts +127 -0
  67. package/dist/lib/models/claudeModels.js +237 -0
  68. package/dist/lib/models/contract.d.ts +395 -0
  69. package/dist/lib/models/contract.js +39 -0
  70. package/dist/lib/models/endpoints.js +2 -1
  71. package/dist/lib/models/errorResponse.d.ts +50 -0
  72. package/dist/lib/models/errorResponse.js +48 -0
  73. package/dist/lib/models/fallback.d.ts +18 -11
  74. package/dist/lib/models/fallback.js +54 -43
  75. package/dist/lib/models/fallbackAdapter.d.ts +55 -0
  76. package/dist/lib/models/fallbackAdapter.js +62 -0
  77. package/dist/lib/models/gateway.js +2 -1
  78. package/dist/lib/models/gatewayAdapter.d.ts +51 -0
  79. package/dist/lib/models/gatewayAdapter.js +81 -0
  80. package/dist/lib/models/gatewayLlm.d.ts +12 -33
  81. package/dist/lib/models/gatewayLlm.js +16 -77
  82. package/dist/lib/models/geminiAdapter.d.ts +125 -0
  83. package/dist/lib/models/geminiAdapter.js +854 -0
  84. package/dist/lib/models/geminiState.d.ts +46 -0
  85. package/dist/lib/models/geminiState.js +51 -0
  86. package/dist/lib/models/genaiMapping.d.ts +283 -0
  87. package/dist/lib/models/genaiMapping.js +965 -0
  88. package/dist/lib/models/gptAdapter.d.ts +240 -0
  89. package/dist/lib/models/gptAdapter.js +804 -0
  90. package/dist/lib/models/gptLlm.d.ts +63 -123
  91. package/dist/lib/models/gptLlm.js +89 -603
  92. package/dist/lib/models/grokAdapter.d.ts +86 -0
  93. package/dist/lib/models/grokAdapter.js +142 -0
  94. package/dist/lib/models/grokLlm.d.ts +22 -45
  95. package/dist/lib/models/grokLlm.js +24 -69
  96. package/dist/lib/models/kimiAdapter.d.ts +157 -0
  97. package/dist/lib/models/kimiAdapter.js +203 -0
  98. package/dist/lib/models/kimiLlm.d.ts +15 -107
  99. package/dist/lib/models/kimiLlm.js +19 -155
  100. package/dist/lib/models/ollamaAdapter.d.ts +125 -0
  101. package/dist/lib/models/ollamaAdapter.js +155 -0
  102. package/dist/lib/models/ollamaLlm.d.ts +11 -107
  103. package/dist/lib/models/ollamaLlm.js +15 -153
  104. package/dist/lib/models/openAiCompatibleLlm.d.ts +73 -216
  105. package/dist/lib/models/openAiCompatibleLlm.js +109 -671
  106. package/dist/lib/models/providerState.d.ts +63 -0
  107. package/dist/lib/models/providerState.js +61 -0
  108. package/dist/lib/models/reasoning.d.ts +26 -0
  109. package/dist/lib/models/reasoning.js +59 -0
  110. package/dist/lib/models/registry.d.ts +62 -35
  111. package/dist/lib/models/registry.js +100 -124
  112. package/dist/lib/models/retry.d.ts +2 -1
  113. package/dist/lib/models/retry.js +3 -2
  114. package/dist/lib/models/schemaNormalize.d.ts +98 -0
  115. package/dist/lib/models/schemaNormalize.js +321 -1
  116. package/dist/lib/models/tracedGemini.d.ts +41 -0
  117. package/dist/lib/models/tracedGemini.js +98 -0
  118. package/dist/lib/models/urls.d.ts +11 -0
  119. package/dist/lib/models/urls.js +16 -0
  120. package/dist/lib/net/redirects.d.ts +21 -2
  121. package/dist/lib/net/redirects.js +11 -34
  122. package/dist/lib/observability/audit.d.ts +5 -1
  123. package/dist/lib/observability/lineage.d.ts +14 -0
  124. package/dist/lib/observability/lineage.js +33 -0
  125. package/dist/lib/observability/supabaseSpanExporter.d.ts +10 -4
  126. package/dist/lib/observability/supabaseSpanExporter.js +17 -9
  127. package/dist/lib/observability/tracer.d.ts +26 -8
  128. package/dist/lib/observability/tracer.js +43 -18
  129. package/dist/lib/runtime/adkMemoryBridge.d.ts +46 -0
  130. package/dist/lib/runtime/adkMemoryBridge.js +68 -0
  131. package/dist/lib/runtime/adkSessionBridge.d.ts +78 -0
  132. package/dist/lib/runtime/adkSessionBridge.js +176 -0
  133. package/dist/lib/runtime/approvals.d.ts +5 -0
  134. package/dist/lib/runtime/approvals.js +39 -8
  135. package/dist/lib/runtime/credentials.d.ts +61 -0
  136. package/dist/lib/runtime/credentials.js +67 -0
  137. package/dist/lib/runtime/events.d.ts +229 -0
  138. package/dist/lib/runtime/events.js +274 -0
  139. package/dist/lib/runtime/logging.d.ts +32 -0
  140. package/dist/lib/runtime/logging.js +55 -0
  141. package/dist/lib/runtime/memoryService.d.ts +72 -0
  142. package/dist/lib/runtime/memoryService.js +25 -0
  143. package/dist/lib/runtime/native/agentLoop.d.ts +209 -0
  144. package/dist/lib/runtime/native/agentLoop.js +705 -0
  145. package/dist/lib/runtime/native/compaction.d.ts +103 -0
  146. package/dist/lib/runtime/native/compaction.js +191 -0
  147. package/dist/lib/runtime/native/delegate.d.ts +151 -0
  148. package/dist/lib/runtime/native/delegate.js +256 -0
  149. package/dist/lib/runtime/native/history.d.ts +83 -0
  150. package/dist/lib/runtime/native/history.js +312 -0
  151. package/dist/lib/runtime/native/interrupts.d.ts +129 -0
  152. package/dist/lib/runtime/native/interrupts.js +310 -0
  153. package/dist/lib/runtime/native/request.d.ts +215 -0
  154. package/dist/lib/runtime/native/request.js +585 -0
  155. package/dist/lib/runtime/native/selfCorrection.d.ts +164 -0
  156. package/dist/lib/runtime/native/selfCorrection.js +327 -0
  157. package/dist/lib/runtime/native/step.d.ts +156 -0
  158. package/dist/lib/runtime/native/step.js +263 -0
  159. package/dist/lib/runtime/native/taskMode.d.ts +64 -0
  160. package/dist/lib/runtime/native/taskMode.js +126 -0
  161. package/dist/lib/runtime/native/telemetry.d.ts +75 -0
  162. package/dist/lib/runtime/native/telemetry.js +299 -0
  163. package/dist/lib/runtime/native/tempState.d.ts +31 -0
  164. package/dist/lib/runtime/native/tempState.js +56 -0
  165. package/dist/lib/runtime/nativeTurn.d.ts +86 -0
  166. package/dist/lib/runtime/nativeTurn.js +107 -0
  167. package/dist/lib/runtime/questions.d.ts +29 -7
  168. package/dist/lib/runtime/questions.js +47 -32
  169. package/dist/lib/runtime/runtimeFlag.d.ts +38 -0
  170. package/dist/lib/runtime/runtimeFlag.js +58 -0
  171. package/dist/lib/runtime/sessions.d.ts +166 -0
  172. package/dist/lib/runtime/sessions.js +201 -0
  173. package/dist/lib/runtime/syndicateTurn.d.ts +46 -5
  174. package/dist/lib/runtime/syndicateTurn.js +233 -36
  175. package/dist/lib/runtime/valueDepth.d.ts +23 -0
  176. package/dist/lib/runtime/valueDepth.js +41 -0
  177. package/dist/lib/session/supabaseSessionService.d.ts +65 -18
  178. package/dist/lib/session/supabaseSessionService.js +148 -105
  179. package/dist/lib/session/transcript.d.ts +69 -23
  180. package/dist/lib/session/transcript.js +105 -29
  181. package/dist/lib/skills.js +33 -7
  182. package/dist/lib/storage/postgres/credentialStore.d.ts +20 -0
  183. package/dist/lib/storage/postgres/credentialStore.js +82 -0
  184. package/dist/lib/storage/postgres/index.d.ts +12 -0
  185. package/dist/lib/storage/postgres/index.js +6 -1
  186. package/dist/lib/storage/postgres/sessionService.d.ts +44 -16
  187. package/dist/lib/storage/postgres/sessionService.js +128 -66
  188. package/dist/lib/syndicateSchema.d.ts +49 -0
  189. package/dist/lib/syndicateSchema.js +99 -9
  190. package/dist/lib/toolRegistry.d.ts +26 -2
  191. package/dist/lib/toolRegistry.js +73 -17
  192. package/dist/lib/tools/adkTool.d.ts +81 -0
  193. package/dist/lib/tools/adkTool.js +268 -0
  194. package/dist/lib/tools/auth.d.ts +118 -0
  195. package/dist/lib/tools/auth.js +89 -0
  196. package/dist/lib/tools/collectionsSearchTool.d.ts +8 -7
  197. package/dist/lib/tools/collectionsSearchTool.js +12 -27
  198. package/dist/lib/tools/credentialCipher.d.ts +58 -0
  199. package/dist/lib/tools/credentialCipher.js +116 -0
  200. package/dist/lib/tools/credentialStore.d.ts +77 -0
  201. package/dist/lib/tools/credentialStore.js +246 -0
  202. package/dist/lib/tools/examples.d.ts +43 -0
  203. package/dist/lib/tools/examples.js +62 -0
  204. package/dist/lib/tools/generateImageTool.d.ts +15 -6
  205. package/dist/lib/tools/generateImageTool.js +19 -29
  206. package/dist/lib/tools/inspectImageTool.d.ts +9 -6
  207. package/dist/lib/tools/inspectImageTool.js +13 -17
  208. package/dist/lib/tools/mcpToolFactory.d.ts +42 -1
  209. package/dist/lib/tools/mcpToolFactory.js +46 -47
  210. package/dist/lib/tools/memoryTools.d.ts +61 -0
  211. package/dist/lib/tools/memoryTools.js +119 -0
  212. package/dist/lib/tools/nativeTools.d.ts +38 -0
  213. package/dist/lib/tools/nativeTools.js +43 -0
  214. package/dist/lib/tools/oauthConsent.d.ts +181 -0
  215. package/dist/lib/tools/oauthConsent.js +330 -0
  216. package/dist/lib/tools/openapi/call.d.ts +108 -0
  217. package/dist/lib/tools/openapi/call.js +295 -0
  218. package/dist/lib/tools/openapi/parse.d.ts +147 -0
  219. package/dist/lib/tools/openapi/parse.js +570 -0
  220. package/dist/lib/tools/openapiTools.d.ts +25 -17
  221. package/dist/lib/tools/openapiTools.js +124 -121
  222. package/dist/lib/tools/scienceTools.d.ts +14 -14
  223. package/dist/lib/tools/skillToolset.d.ts +58 -87
  224. package/dist/lib/tools/skillToolset.js +61 -142
  225. package/dist/lib/tools/skills/env.d.ts +58 -0
  226. package/dist/lib/tools/skills/env.js +165 -0
  227. package/dist/lib/tools/skills/executor.d.ts +114 -0
  228. package/dist/lib/tools/skills/executor.js +331 -0
  229. package/dist/lib/tools/skills/frontmatter.d.ts +79 -0
  230. package/dist/lib/tools/skills/frontmatter.js +215 -0
  231. package/dist/lib/tools/skills/loader.d.ts +91 -0
  232. package/dist/lib/tools/skills/loader.js +257 -0
  233. package/dist/lib/tools/skills/tools.d.ts +187 -0
  234. package/dist/lib/tools/skills/tools.js +386 -0
  235. package/dist/lib/tools/taskTools.d.ts +15 -15
  236. package/dist/lib/tools/tool.d.ts +354 -0
  237. package/dist/lib/tools/tool.js +304 -0
  238. package/dist/lib/tools/toolContract.d.ts +89 -33
  239. package/dist/lib/tools/toolContract.js +104 -82
  240. package/dist/lib/tools/urlContextTool.d.ts +4 -1
  241. package/dist/lib/tools/urlContextTool.js +6 -2
  242. package/dist/lib/tools/webExtractTool.d.ts +3 -3
  243. package/dist/lib/tools/webExtractTool.js +2 -1
  244. package/dist/lib/tools/webSearchTool.d.ts +7 -10
  245. package/dist/lib/tools/webSearchTool.js +15 -60
  246. package/dist/lib/tools/wikiTools.d.ts +19 -19
  247. package/dist/lib/tools/wikiTools.js +13 -9
  248. package/dist/lib/tools/xApiSearchTool.d.ts +3 -3
  249. package/dist/lib/tools/xApiSearchTool.js +2 -1
  250. package/dist/lib/tools/xSearchTool.d.ts +8 -10
  251. package/dist/lib/tools/xSearchTool.js +12 -63
  252. package/dist/lib/tools/xaiSearchParams.d.ts +41 -0
  253. package/dist/lib/tools/xaiSearchParams.js +126 -0
  254. package/dist/lib/wiki/agentRun.d.ts +12 -0
  255. package/dist/lib/wiki/agentRun.js +74 -35
  256. package/dist/lib/wiki/entities.d.ts +12 -0
  257. package/dist/lib/wiki/entities.js +45 -3
  258. package/dist/lib/workflow/agentNode.d.ts +153 -0
  259. package/dist/lib/workflow/agentNode.js +365 -0
  260. package/dist/lib/workflow/graph.d.ts +178 -0
  261. package/dist/lib/workflow/graph.js +468 -0
  262. package/dist/lib/workflow/nodeEvents.d.ts +50 -0
  263. package/dist/lib/workflow/nodeEvents.js +101 -0
  264. package/dist/lib/workflow/pause.d.ts +122 -0
  265. package/dist/lib/workflow/pause.js +180 -0
  266. package/dist/lib/workflow/resume.d.ts +166 -0
  267. package/dist/lib/workflow/resume.js +441 -0
  268. package/dist/lib/workflow/route.d.ts +58 -0
  269. package/dist/lib/workflow/route.js +69 -0
  270. package/dist/lib/workflow/scheduler.d.ts +418 -0
  271. package/dist/lib/workflow/scheduler.js +756 -0
  272. package/dist/lib/workflow/toolNode.d.ts +127 -0
  273. package/dist/lib/workflow/toolNode.js +236 -0
  274. package/dist/lib/workflow/turn.d.ts +118 -0
  275. package/dist/lib/workflow/turn.js +216 -0
  276. package/dist/lib/workflow.d.ts +33 -11
  277. package/dist/lib/workflow.js +49 -27
  278. package/dist/lib/workflowConfig.d.ts +7 -2
  279. package/dist/lib/workflowConfig.js +7 -12
  280. package/dist/scripts/a2a_server.js +4 -3
  281. package/dist/scripts/assistant_worker.d.ts +2 -1
  282. package/dist/scripts/assistant_worker.js +9 -4
  283. package/dist/scripts/doctor.d.ts +3 -1
  284. package/dist/scripts/doctor.js +4 -2
  285. package/dist/scripts/syndicate_chat.js +19 -10
  286. package/package.json +9 -3
  287. package/skills/briefs/melchizedek-author.md +3 -2
  288. package/skills/briefs/melchizedek-models.md +13 -9
  289. package/skills/briefs/melchizedek.md +3 -3
  290. package/skills/melchizedek/SKILL.md +2 -1
  291. package/skills/melchizedek-author/SKILL.md +2 -1
  292. package/skills/melchizedek-author/assets/minimal.yaml +2 -0
  293. package/skills/melchizedek-models/SKILL.md +23 -6
@@ -0,0 +1,418 @@
1
+ /**
2
+ * lib/workflow/scheduler.ts — the engine's own walk of a workflow graph.
3
+ *
4
+ * `runWorkflowGraph(graph, options)` runs the `WorkflowGraph` that
5
+ * `buildWorkflowGraph` (lib/workflow/graph.ts) builds: a node runs when a
6
+ * predecessor's completion triggers it, a node with several successors fans
7
+ * out, a `join` waits until every predecessor has completed, a `map` runs its
8
+ * agent once per item under `maxParallel`, and each node's output is the
9
+ * input its successors receive. It imports nothing from ADK.
10
+ *
11
+ * What a node DOES is not this module's business. Agent, tool and ask_user
12
+ * nodes, and each item of a map, are run by the `runNode` function the caller
13
+ * passes in (WS4-3, WS4-5 and WS4-4a supply them). The scheduler runs the
14
+ * kinds that are pure graph mechanics itself: the start node seeds the walk,
15
+ * a `join` outputs `{ <predecessor>: <output> }`, a `route` step re-emits its
16
+ * input with the route `routeOf` reads from it, and a `map` is the worker
17
+ * pool around its items.
18
+ *
19
+ * ── The same walk as ADK's Workflow ──────────────────────────────────────
20
+ * The loop is ADK 2.2's (`Workflow.runLoop`, workflow/workflow.js), step for
21
+ * step, so a graph completes in the order ADK records for it
22
+ * (tests/workflowScheduler.test.ts runs both and compares):
23
+ *
24
+ * - Triggers are buffered per node, in the order they were pushed. Each
25
+ * pass starts every buffered node that is not already running, in the
26
+ * buffer's order, until `maxConcurrency` nodes are pending.
27
+ * - The loop then waits for the first pending node to settle, by
28
+ * `Promise.race` over the pending runs in the order they started, so of
29
+ * two runs already settled the one started first is handled first.
30
+ * - A completed node pushes one trigger to each successor its route
31
+ * selects (`always` edges, the edges keyed to its route compared as
32
+ * strings in ADK's spelling of the key, else the `default` edge). A join
33
+ * is triggered only once every predecessor has COMPLETED, with their
34
+ * outputs keyed in edge order. A node triggered while it runs runs again
35
+ * afterwards, once per trigger, as ADK does: only a join waits for all.
36
+ * - Branches and node paths are ADK's (`Planner@1`, `Six.Fan.Agent@0`), so
37
+ * the events a node runner writes can carry the values ADK writes.
38
+ * - The workflow's output is the one terminal node's output; two terminal
39
+ * outputs fail the run with ADK's message.
40
+ *
41
+ * ── The controls (ADK's node_runner.js, ADR 0089) ─────────────────────────
42
+ * Every node run goes through `executeNode`, and every attempt of it through
43
+ * `withControls`, which is ADK's `runChildNode` loop:
44
+ *
45
+ * - **Timeout.** A node with `timeout` races each attempt against a timer;
46
+ * the runner's signal aborts when it fires and the attempt fails with
47
+ * `NodeTimeoutError`. Without a timeout the attempt is awaited, as ADK
48
+ * awaits it: the runner gets the signal and must settle when it aborts.
49
+ * - **Retry.** A failed attempt is retried while the node's `retry` allows
50
+ * (`max_attempts`, default 5; an `exceptions` list of error names when
51
+ * one is given), after ADK's backoff with its jitter. An abort and a map
52
+ * item's failure are never retried; an abort cuts a backoff short.
53
+ * - **Node errors.** A runner reports an error by returning `error`: the
54
+ * walk emits it (`node_error`, source `node`) and, when the result has no
55
+ * output and no route, the attempt fails with `NodeReportedError`. A node
56
+ * that gives up fails the walk; the walk emits that error once (source
57
+ * `workflow`) unless the node already reported it or the walk was
58
+ * aborted, then aborts the other runs, lets them settle and rethrows the
59
+ * error unchanged. Every `node_error` is collected in `nodeErrors`, in
60
+ * the shape runSyndicateTurn's drain collects ADK's error events.
61
+ * - **Abort and deadline.** The walk's signal is the caller's, else the
62
+ * current turn's (lib/runtime/turnControl.ts), so a cancel or a deadline
63
+ * reaches every run. Once it fires no node starts, and the walk rejects
64
+ * with `InvocationAbortedError` when nothing is left running.
65
+ * - **max_concurrency** counts every pending run, a retrying one included.
66
+ *
67
+ * A map item runs under its agent's own modifiers (`nodes.<agent>`), and an
68
+ * item that gives up fails the map with `DynamicNodeFailError`, as ADK's
69
+ * ParallelWorker does; the map entry's own `retry` and `timeout` are not
70
+ * applied, because ADK's compile does not apply them.
71
+ *
72
+ * ── Interrupts (ADK's handleCompletion and finalize, ADR 0092) ───────────
73
+ * A runner that pauses its node on a person returns `interruptIds` (the ids
74
+ * of the input requests its events carry; lib/workflow/pause.ts raises them
75
+ * for an ask_user node). The node then WAITS, as ADK's does: it is not
76
+ * completed, records no output, triggers no successor, and is not started
77
+ * again by a later trigger in the same walk. The rest of the graph runs on.
78
+ * A walk that ends with a node waiting resolves with every open id in
79
+ * `interruptIds`, in the order the nodes paused, and no output (ADK skips
80
+ * the terminal check). A reported error with an interrupt is not the node's
81
+ * failure. A map item's interrupts are not carried (an agent node's own
82
+ * pause does not run here yet).
83
+ *
84
+ * ── Resume (ADK's startNodeTask on rehydrated state, ADR 0094) ───────────
85
+ * A walk given `resume` (lib/workflow/resume.ts builds it from the stored
86
+ * events) starts from START as ADK's does, and each node's first activation
87
+ * takes that node's next prior run:
88
+ *
89
+ * - a prior run with an output or a route and no open interrupt is done:
90
+ * the node completes at once with that output, route and branch, emits
91
+ * `node_resumed` (not `node_start` or `node_end`; ADK writes no event
92
+ * for it) and triggers its successors as a completion does;
93
+ * - a paused prior run of a node that does not rerun on resume
94
+ * (`rerunsOnResume`) completes with its answers, the same way;
95
+ * - any other node runs, a paused one on the input it recorded, with the
96
+ * answers in `NodeRun.resumeInputs` (none on a repeat activation in the
97
+ * same walk). Neither shortcut counts as a run of the node.
98
+ *
99
+ * A task-mode node that waits for its output (WS4-3) is a later ticket.
100
+ *
101
+ * ── Tracing (ADR 0095) ───────────────────────────────────────────────────
102
+ * The scheduler opens no span. A caller that passes `traceNode` has each
103
+ * node run (every attempt and backoff inside it) and each map item's run
104
+ * wrapped, as ADK's node runner opens `execute_node` around them; the
105
+ * native turn (lib/workflow/turn.ts) passes the engine's `node.execute`
106
+ * span, so an agent node's own spans nest under it.
107
+ */
108
+ import type { TurnEvent } from '../runtime/events.ts';
109
+ import type { AgentNode, AskUserNode, GraphNodeKind, MapNode, ToolNode, WorkflowGraph } from './graph.ts';
110
+ import type { RetryYaml } from '../workflowConfig.ts';
111
+ import type { ResumeState } from './resume.ts';
112
+ /** ADK's ParallelWorker pool size when `max_parallel` is not set. */
113
+ export declare const DEFAULT_MAX_PARALLEL = 8;
114
+ /** A graph node the caller's runner executes. */
115
+ export type RunnableNode = AgentNode | ToolNode | AskUserNode;
116
+ /** One item of a map: the map's agent run on `items[index]`. */
117
+ export interface MapItem {
118
+ kind: 'map_item';
119
+ /** The agent run on the item. */
120
+ agent: string;
121
+ /** The map node. */
122
+ map: MapNode;
123
+ index: number;
124
+ }
125
+ /** One run of a node, as the scheduler hands it to the runner. */
126
+ export interface NodeRun {
127
+ target: RunnableNode | MapItem;
128
+ /** The predecessor's output; a join's `{ <predecessor>: <output> }`; the workflow input after START; a map's item. */
129
+ input: unknown;
130
+ /** The node's run counter as a string ("1", "2", ...); a map item's index. */
131
+ runId: string;
132
+ /** ADK's node path: `<workflow>.<node>`, or `<workflow>.<map>.<agent>@<index>` for a map item. */
133
+ path: string;
134
+ /** ADK's branch, undefined at the workflow's own branch. */
135
+ branch: string | undefined;
136
+ /**
137
+ * Aborted when this attempt must stop: the walk's signal fired (a cancel,
138
+ * the turn's deadline, another node failed) or the node's timeout did.
139
+ * A runner settles promptly once it aborts.
140
+ */
141
+ signal: AbortSignal;
142
+ /** Which attempt this is, from 1; above 1 only for a node with `retry`. */
143
+ attempt: number;
144
+ /**
145
+ * A resumed walk's answers, by interrupt id (ADK's ctx.resumeInputs):
146
+ * every answer, on a node's first activation; empty on a repeat one.
147
+ * Absent in a walk that is not a resume.
148
+ */
149
+ resumeInputs?: Readonly<Record<string, unknown>>;
150
+ /**
151
+ * The interrupts this node's prior run paused on, when this run resumes it
152
+ * (a resumed walk's first activation of a node that waited). An agent node
153
+ * resumes its own run on them instead of starting afresh (ADR 0098).
154
+ */
155
+ resumedInterruptIds?: readonly string[];
156
+ }
157
+ /** An error a node reports instead of throwing, as an ADK event's errorCode and errorMessage. */
158
+ export interface ReportedNodeError {
159
+ code: string;
160
+ message: string;
161
+ }
162
+ /** What a node run produced. */
163
+ export interface NodeResult {
164
+ /** The node's output, handed to its successors as their input. */
165
+ output?: unknown;
166
+ /** The route(s) the node emitted; only keyed edges read it. */
167
+ route?: unknown;
168
+ /**
169
+ * An error the node reported. It is emitted and collected; with no output,
170
+ * no route and no interrupt the attempt fails (`NodeReportedError`) and
171
+ * may be retried.
172
+ */
173
+ error?: ReportedNodeError;
174
+ /**
175
+ * The ids of the input requests the node paused on (ADK's
176
+ * `longRunningToolIds` on its events). Non-empty: the node waits, and its
177
+ * output and route are not acted on.
178
+ */
179
+ interruptIds?: string[];
180
+ }
181
+ export type NodeRunner = (run: NodeRun) => NodeResult | Promise<NodeResult>;
182
+ /** One collected node error, in the shape runSyndicateTurn's `answer.nodeErrors` holds. */
183
+ export interface CollectedNodeError {
184
+ node: string;
185
+ code: string;
186
+ message: string;
187
+ }
188
+ /** What the walk reports as it goes. */
189
+ export type SchedulerEvent = {
190
+ type: 'node_start';
191
+ node: string;
192
+ kind: GraphNodeKind;
193
+ runId: string;
194
+ path: string;
195
+ branch: string | undefined;
196
+ input: unknown;
197
+ } | {
198
+ type: 'node_end';
199
+ node: string;
200
+ kind: GraphNodeKind;
201
+ runId: string;
202
+ path: string;
203
+ branch: string | undefined;
204
+ output: unknown;
205
+ route?: unknown;
206
+ } | {
207
+ type: 'node_waiting';
208
+ node: string;
209
+ kind: GraphNodeKind;
210
+ runId: string;
211
+ path: string;
212
+ branch: string | undefined;
213
+ interruptIds: string[];
214
+ }
215
+ /**
216
+ * A resumed walk completed the node from its stored run without running
217
+ * it: `stored` (its prior output), or `answers` (a node that does not
218
+ * rerun, completed with the answers to its interrupts). ADK writes no
219
+ * event for either.
220
+ */
221
+ | {
222
+ type: 'node_resumed';
223
+ node: string;
224
+ kind: GraphNodeKind;
225
+ path: string;
226
+ branch: string | undefined;
227
+ output: unknown;
228
+ route?: unknown;
229
+ from: 'stored' | 'answers';
230
+ } | {
231
+ type: 'item_start';
232
+ node: string;
233
+ agent: string;
234
+ index: number;
235
+ path: string;
236
+ branch: string | undefined;
237
+ input: unknown;
238
+ } | {
239
+ type: 'item_end';
240
+ node: string;
241
+ agent: string;
242
+ index: number;
243
+ path: string;
244
+ branch: string | undefined;
245
+ output: unknown;
246
+ } | {
247
+ type: 'node_error';
248
+ /**
249
+ * `node`: the node reported it on an attempt (ADK: the node's own event
250
+ * carrying an errorCode). `workflow`: the node gave up and the walk
251
+ * reports why (ADK: the workflow's node-error event).
252
+ */
253
+ source: 'node' | 'workflow';
254
+ /** The node, or a map item's agent, as ADK's event author. */
255
+ node: string;
256
+ path: string;
257
+ /** The run's branch for `node`; the workflow's own branch for `workflow`, as ADK writes it. */
258
+ branch: string | undefined;
259
+ code: string;
260
+ message: string;
261
+ /** The error's class name (ADK's errorType); `workflow` only. */
262
+ errorType?: string;
263
+ /** The attempt it happened on; for `workflow`, the attempts made. */
264
+ attempt: number;
265
+ };
266
+ export interface RunWorkflowOptions {
267
+ /** The workflow's input: what the nodes after START receive. */
268
+ input: unknown;
269
+ /** Runs agent, tool and ask_user nodes and map items. */
270
+ runNode: NodeRunner;
271
+ /** Aborts every run in flight when it fires. Default: the current turn's signal (turnControl), if any. */
272
+ signal?: AbortSignal;
273
+ /** Called synchronously for every event, in order. */
274
+ onEvent?: (event: SchedulerEvent) => void;
275
+ /** The workflow's own node path; default the graph's name. */
276
+ nodePath?: string;
277
+ /** The workflow's own branch; default none. */
278
+ branch?: string;
279
+ /**
280
+ * Resume a paused walk: the prior runs and answers workflowResume
281
+ * (lib/workflow/resume.ts) rebuilt from the stored events. The scheduler
282
+ * consumes `priorRuns`.
283
+ */
284
+ resume?: ResumeState;
285
+ /**
286
+ * Wraps each node's run (every attempt of it, the backoffs included) and
287
+ * each map item's, as ADK's node runner opens its `execute_node` span
288
+ * around them. The turn runner passes the engine's span here
289
+ * (lib/runtime/native/telemetry.ts, ADR 0095); the scheduler itself
290
+ * knows nothing of tracing. A node completed from its stored run on a
291
+ * resume runs nothing and is not wrapped. Default: none.
292
+ */
293
+ traceNode?: NodeTracer;
294
+ }
295
+ /** What a node tracer learns about the run it wraps. */
296
+ export interface TracedNode {
297
+ /** The node, or a map item's agent. */
298
+ name: string;
299
+ kind: GraphNodeKind | 'map_item';
300
+ path: string;
301
+ /** The run id (a map item's index). */
302
+ runId: string;
303
+ /** The run's attempt counter: read it once the run settles for the attempts made. */
304
+ attempts: {
305
+ readonly count: number;
306
+ };
307
+ }
308
+ /** Wraps one node run; must return what `run` returns, or rethrow what it throws. */
309
+ export type NodeTracer = (node: TracedNode, run: () => Promise<NodeResult>) => Promise<NodeResult>;
310
+ export interface WorkflowRun {
311
+ /** The terminal node's output, or undefined when no terminal node produced one. */
312
+ output: unknown;
313
+ /** Every completed node's latest output, by name (a node whose output was undefined is absent, as in ADK). */
314
+ outputs: Map<string, unknown>;
315
+ /** Node names in the order they completed (a node that ran twice appears twice; a resumed walk's `node_resumed` nodes included). */
316
+ order: string[];
317
+ /** Every node error the walk emitted, in order: the attempts that failed and were retried or survived. */
318
+ nodeErrors: CollectedNodeError[];
319
+ /**
320
+ * The input requests the walk ended waiting on, in the order their nodes
321
+ * paused. Non-empty: the walk is paused (input-required) and `output` is
322
+ * undefined.
323
+ */
324
+ interruptIds: string[];
325
+ }
326
+ /** A node ran past its `timeout`. */
327
+ export declare class NodeTimeoutError extends Error {
328
+ readonly nodeName: string;
329
+ readonly timeout: number;
330
+ constructor(options: {
331
+ nodeName: string;
332
+ timeout: number;
333
+ });
334
+ }
335
+ /** A node reported an error and produced no output and no route. */
336
+ export declare class NodeReportedError extends Error {
337
+ readonly code: string;
338
+ readonly nodeName: string;
339
+ constructor(options: {
340
+ nodeName: string;
341
+ errorCode?: string;
342
+ errorMessage?: string;
343
+ });
344
+ }
345
+ /** The walk, or one node's run, stopped because its signal fired. */
346
+ export declare class InvocationAbortedError extends Error {
347
+ constructor(message?: string, options?: {
348
+ cause?: unknown;
349
+ });
350
+ }
351
+ /** A map item gave up; the map fails with this. */
352
+ export declare class DynamicNodeFailError extends Error {
353
+ readonly error: Error;
354
+ readonly errorNodePath: string;
355
+ constructor(options: {
356
+ message: string;
357
+ error: Error;
358
+ errorNodePath: string;
359
+ });
360
+ }
361
+ /** ADK's defaults when a `retry` block leaves a field out. */
362
+ export declare const RETRY_DEFAULTS: {
363
+ readonly maxAttempts: 5;
364
+ readonly initialDelay: 1;
365
+ readonly maxDelay: 60;
366
+ readonly backoffFactor: 2;
367
+ readonly jitter: 1;
368
+ };
369
+ /**
370
+ * A node's retry, as the YAML spells it: ADK's fields, `exceptions` (the
371
+ * error names that may be retried, all when absent) and `jitter` (the
372
+ * random spread, default 1) included.
373
+ */
374
+ export type RetrySettings = RetryYaml;
375
+ /** The names an error answers to: its class's and its `name`, as ADK matches `exceptions`. */
376
+ export declare function errorNames(error: unknown): string[];
377
+ /** ADK's errorType: the class name, or the assigned name of a plain Error. */
378
+ export declare function errorName(error: unknown): string;
379
+ /** Whether a node that has made `attempts` attempts and failed with `error` runs again. */
380
+ export declare function shouldRetry(error: unknown, retry: RetrySettings, attempts: number): boolean;
381
+ /** Seconds to wait before the attempt after `attempts`: exponential backoff, capped, with ADK's jitter. */
382
+ export declare function retryDelaySeconds(retry: RetrySettings, attempts: number, random?: () => number): number;
383
+ /**
384
+ * Run a workflow graph to its end. Resolves with the workflow's output, every
385
+ * node's output and the completion order; rejects with the first node error,
386
+ * after every run still pending has settled.
387
+ */
388
+ export declare function runWorkflowGraph(graph: WorkflowGraph, options: RunWorkflowOptions): Promise<WorkflowRun>;
389
+ /** The event ADK's workflow writes for a node that gave up (`createNodeErrorEvent`). */
390
+ export type NodeErrorTurnEvent = TurnEvent & {
391
+ isNodeError: true;
392
+ errorType: string;
393
+ errorCode: string;
394
+ errorMessage: string;
395
+ attemptCount: number;
396
+ };
397
+ /**
398
+ * The event ADK stores for a `node_error` the walk reported (source
399
+ * `workflow`): ADK's `createNodeErrorEvent`, field for field and in its key
400
+ * order, so a turn runner can write it where ADK writes it. An error a node
401
+ * reported itself (source `node`) is already on the event its runner wrote.
402
+ */
403
+ export declare function nodeErrorEvent(event: Extract<SchedulerEvent, {
404
+ type: 'node_error';
405
+ }>, invocationId: string): NodeErrorTurnEvent;
406
+ /**
407
+ * The walk as a stream: every event as it happens, then the run as the
408
+ * generator's return value. The walk does not wait for the consumer, so a
409
+ * slow reader never changes the order.
410
+ */
411
+ export declare function streamWorkflowGraph(graph: WorkflowGraph, options: Omit<RunWorkflowOptions, 'onEvent'>): AsyncGenerator<SchedulerEvent, WorkflowRun>;
412
+ /**
413
+ * The successors a completed node triggers, as ADK's `getNextPendingNodes`
414
+ * selects them: every `always` edge; every keyed edge whose key (in ADK's
415
+ * spelling) equals the emitted route, or one of them, compared as strings;
416
+ * and the `default` edge when no keyed edge matched.
417
+ */
418
+ export declare function nextNodes(graph: WorkflowGraph, name: string, route: unknown): string[];