@igorjpimenta/manifold 0.0.0-stage → 2026.11.22-beta.3

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 (210) hide show
  1. package/.migrations/ops/0000_futuristic_karma.sql +111 -0
  2. package/.migrations/ops/0001_low_maximus.sql +7 -0
  3. package/.migrations/ops/meta/0000_snapshot.json +763 -0
  4. package/.migrations/ops/meta/0001_snapshot.json +173 -0
  5. package/.migrations/ops/meta/_journal.json +20 -0
  6. package/.migrations/runtime/0000_real_boomer.sql +180 -0
  7. package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
  8. package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
  9. package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
  10. package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
  11. package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
  12. package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
  13. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
  14. package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
  15. package/.migrations/runtime/0009_lying_shocker.sql +96 -0
  16. package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
  17. package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
  18. package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
  19. package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
  20. package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
  21. package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
  22. package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
  23. package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
  24. package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
  25. package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
  26. package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
  27. package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
  28. package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
  29. package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
  30. package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
  31. package/.migrations/runtime/meta/_journal.json +97 -0
  32. package/README.md +1583 -2
  33. package/RELEASING.md +89 -0
  34. package/agent/fleet.js +100 -0
  35. package/bin/cli.js +854 -0
  36. package/bin/manifold.js +4 -0
  37. package/boot/flow-revival.js +536 -0
  38. package/budget/index.js +495 -0
  39. package/budget/lib.js +59 -0
  40. package/dashboard/cost.js +528 -0
  41. package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
  42. package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
  43. package/dashboard/dist/index.html +13 -0
  44. package/dashboard/event-log.js +267 -0
  45. package/dashboard/index.html +12 -0
  46. package/dashboard/package-lock.json +2877 -0
  47. package/dashboard/package.json +37 -0
  48. package/dashboard/pipeline-graph.js +131 -0
  49. package/dashboard/postcss.config.js +6 -0
  50. package/dashboard/search.js +183 -0
  51. package/dashboard/server.js +1015 -0
  52. package/dashboard/src/App.tsx +42 -0
  53. package/dashboard/src/api/client.ts +118 -0
  54. package/dashboard/src/api/types.ts +510 -0
  55. package/dashboard/src/api/ws.tsx +112 -0
  56. package/dashboard/src/components/CancelButton.tsx +63 -0
  57. package/dashboard/src/components/ClickToCopy.tsx +68 -0
  58. package/dashboard/src/components/CopyButton.tsx +61 -0
  59. package/dashboard/src/components/DailyCostTrend.tsx +85 -0
  60. package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
  61. package/dashboard/src/components/Funnel.tsx +149 -0
  62. package/dashboard/src/components/GateForm.tsx +158 -0
  63. package/dashboard/src/components/GraphView.tsx +302 -0
  64. package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
  65. package/dashboard/src/components/Histogram.tsx +292 -0
  66. package/dashboard/src/components/Layout.tsx +28 -0
  67. package/dashboard/src/components/NeedsAttention.tsx +98 -0
  68. package/dashboard/src/components/NodeContextCard.tsx +132 -0
  69. package/dashboard/src/components/PathTrail.tsx +35 -0
  70. package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
  71. package/dashboard/src/components/RerunButton.tsx +46 -0
  72. package/dashboard/src/components/RunComparison.tsx +53 -0
  73. package/dashboard/src/components/Sparkline.tsx +94 -0
  74. package/dashboard/src/components/StatStrip.tsx +147 -0
  75. package/dashboard/src/components/StateTree.tsx +84 -0
  76. package/dashboard/src/components/StatusBadge.tsx +18 -0
  77. package/dashboard/src/components/SubflowPathPills.tsx +38 -0
  78. package/dashboard/src/components/SubflowSummary.tsx +24 -0
  79. package/dashboard/src/components/SubflowsCard.tsx +327 -0
  80. package/dashboard/src/components/TodayStrip.tsx +86 -0
  81. package/dashboard/src/components/TranscriptPane.tsx +99 -0
  82. package/dashboard/src/components/TranscriptRow.tsx +69 -0
  83. package/dashboard/src/components/ui/badge.tsx +38 -0
  84. package/dashboard/src/components/ui/button.tsx +28 -0
  85. package/dashboard/src/components/ui/card.tsx +42 -0
  86. package/dashboard/src/components/ui/skeleton.tsx +9 -0
  87. package/dashboard/src/components/ui/table.tsx +62 -0
  88. package/dashboard/src/index.css +26 -0
  89. package/dashboard/src/lib/anomaly.ts +42 -0
  90. package/dashboard/src/lib/format.ts +30 -0
  91. package/dashboard/src/lib/step-history.ts +13 -0
  92. package/dashboard/src/lib/subflow.ts +154 -0
  93. package/dashboard/src/lib/use-click-outside.ts +28 -0
  94. package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
  95. package/dashboard/src/lib/utils.ts +6 -0
  96. package/dashboard/src/main.tsx +10 -0
  97. package/dashboard/src/types/shims.d.ts +4 -0
  98. package/dashboard/src/views/Home.tsx +478 -0
  99. package/dashboard/src/views/PipelineDetail.tsx +1185 -0
  100. package/dashboard/src/views/PipelinesList.tsx +111 -0
  101. package/dashboard/src/views/RunDetail.tsx +248 -0
  102. package/dashboard/src/views/Search.tsx +196 -0
  103. package/dashboard/stats.js +668 -0
  104. package/dashboard/tailwind.config.ts +43 -0
  105. package/dashboard/tree-stats.js +12 -0
  106. package/dashboard/tsconfig.json +25 -0
  107. package/dashboard/views.js +231 -0
  108. package/dashboard/vite.config.ts +34 -0
  109. package/drizzle.config.js +12 -0
  110. package/drizzle.config.ops.js +12 -0
  111. package/elevation.js +116 -0
  112. package/engine/admission-promotion-resume.js +127 -0
  113. package/engine/agent-dispatch.js +64 -0
  114. package/engine/child-resume-trigger.js +315 -0
  115. package/engine/executor-budget.js +87 -0
  116. package/engine/executor-cancel.js +83 -0
  117. package/engine/executor-errors.js +122 -0
  118. package/engine/executor-helpers.js +254 -0
  119. package/engine/executor-nodes.js +292 -0
  120. package/engine/executor-persist.js +212 -0
  121. package/engine/executor-subflow-children.js +275 -0
  122. package/engine/executor.js +1013 -0
  123. package/engine/global-concurrency.js +33 -0
  124. package/engine/loader.js +528 -0
  125. package/engine/needs-resolver.js +229 -0
  126. package/engine/pipelines-watcher.js +133 -0
  127. package/engine/result-replay.js +34 -0
  128. package/engine/revival-messages.js +11 -0
  129. package/engine/spawn-child.js +55 -0
  130. package/engine/wait-for-flow-settlement.js +109 -0
  131. package/events/catalog.js +26 -0
  132. package/events/emit.js +44 -0
  133. package/events/kinds.js +118 -0
  134. package/events/serve-ws.js +74 -0
  135. package/events/stream.js +35 -0
  136. package/events/ws-dialog.js +257 -0
  137. package/events/ws-frame.js +194 -0
  138. package/events/ws-run-events.js +70 -0
  139. package/events.js +32 -0
  140. package/gates/gate-resolver.js +576 -0
  141. package/gates/paused-flows.js +55 -0
  142. package/graphql/columns.js +67 -0
  143. package/graphql/schema.js +333 -0
  144. package/index.js +49 -0
  145. package/mcp/http.js +159 -0
  146. package/mcp/identity.js +94 -0
  147. package/mcp/stdio.js +80 -0
  148. package/mcp/tools.js +245 -0
  149. package/mcp/wire-ops.js +178 -0
  150. package/package.json +49 -4
  151. package/routes/bearer-auth.js +57 -0
  152. package/routes/discuss-audio.js +87 -0
  153. package/routes/discuss-component.js +39 -0
  154. package/routes/discuss-draft.js +159 -0
  155. package/routes/discuss-helpers.js +20 -0
  156. package/routes/discuss.js +848 -0
  157. package/routes/failures.js +104 -0
  158. package/routes/graphql.js +61 -0
  159. package/routes/health.js +54 -0
  160. package/routes/operator-discuss.js +193 -0
  161. package/routes/operator-resume.js +32 -0
  162. package/routes/operator-retry.js +301 -0
  163. package/routes/operator.js +539 -0
  164. package/routes/pipelines.js +118 -0
  165. package/routes/runs.js +1129 -0
  166. package/scripts/ci/import-check.mjs +50 -0
  167. package/scripts/link-core-self.mjs +31 -0
  168. package/scripts/with-guard.sh +24 -0
  169. package/server/app.js +503 -0
  170. package/server/registry.js +53 -0
  171. package/server/serve.js +163 -0
  172. package/state/admission-queue-store.js +325 -0
  173. package/state/attribution-registries.js +102 -0
  174. package/state/batch-writer.js +162 -0
  175. package/state/db-guard.js +63 -0
  176. package/state/db-health.js +27 -0
  177. package/state/db.js +122 -0
  178. package/state/discuss-store.js +299 -0
  179. package/state/failure-store.js +427 -0
  180. package/state/migrate.js +104 -0
  181. package/state/node-cost.js +67 -0
  182. package/state/node-executions.js +775 -0
  183. package/state/node-health-store.js +124 -0
  184. package/state/ops-merge.js +93 -0
  185. package/state/paths.js +146 -0
  186. package/state/pipeline-dirs.js +99 -0
  187. package/state/pipeline-registry.js +449 -0
  188. package/state/run-store.js +697 -0
  189. package/state/schema.js +438 -0
  190. package/state/schema.ops.js +44 -0
  191. package/state/session-index.js +177 -0
  192. package/state/session-keys.js +39 -0
  193. package/state/state-store.js +311 -0
  194. package/state/subflow-state.js +126 -0
  195. package/state/tool-handoff.js +169 -0
  196. package/telemetry/budget-alert.js +353 -0
  197. package/telemetry/cli.js +331 -0
  198. package/telemetry/grammar.js +119 -0
  199. package/telemetry/http-budget.js +132 -0
  200. package/telemetry/http-cost.js +670 -0
  201. package/telemetry/http-helpers.js +134 -0
  202. package/telemetry/http-tree.js +349 -0
  203. package/telemetry/http.js +44 -0
  204. package/telemetry/index.js +307 -0
  205. package/telemetry/ingest.js +134 -0
  206. package/telemetry/labels.js +115 -0
  207. package/telemetry/pricer.js +77 -0
  208. package/telemetry/resolver.js +314 -0
  209. package/telemetry/store.js +147 -0
  210. package/utils/run-patch.js +43 -0
@@ -0,0 +1,64 @@
1
+ import { usesNamedParams } from "./executor-helpers.js";
2
+ import { clearPendingResult } from "../state/tool-handoff.js";
3
+
4
+ // agent-dispatch.js — the engine's agent-node dispatch: run one turn
5
+ // through the injected harness runtime, then reduce the result with the
6
+ // node's apply().
7
+ //
8
+ // The turn itself is harness work — which runtime, which call shape, how a
9
+ // reply comes back — so core injects it as `runTurn` and only ever sees
10
+ // { text, runId, toolResult? }. A harness that cannot run a turn throws
11
+ // GatewayHandoffNeeded from there, and core's failure path records it like
12
+ // any other.
13
+
14
+ export function createAgentDispatch({ runTurn, sessionIndex, logger, emit }) {
15
+ /**
16
+ * Run an `agent`-kind dispatch — a single attempt, no automatic retry.
17
+ *
18
+ * A failed dispatch throws immediately, gets recorded as a normal failure
19
+ * event the same way every other failure class does, and the operator
20
+ * retries manually via the flow's existing Retry action once whatever was
21
+ * wrong is actually fixed — same rule for every agent node, no exceptions.
22
+ * `node.retry` config, if still present on any node, is inert and ignored.
23
+ */
24
+ async function dispatchAgentWithRetry({ node, nodeId, session, initialMessage, applyCtx, applyExtraArg, applyMeta, flowId, needsCtx }) {
25
+ // The harness-owned session id stores verbatim.
26
+ if (sessionIndex?.record) await sessionIndex.record({ flowId, session, nodeId, agent: node.agent ?? null });
27
+ if (typeof runTurn !== "function") {
28
+ throw new Error("manifold: no agent runtime bound — the harness must inject runTurn");
29
+ }
30
+ // `session` is core's whole contract here — the harness adapter
31
+ // (run-turn.js) maps it to the runtime's own `sessionKey` field.
32
+ const output = await runTurn({
33
+ session,
34
+ message: initialMessage,
35
+ skill: node.skill,
36
+ agentId: node.agent,
37
+ });
38
+ // Transcript event: without it the reply is visible only inside apply().
39
+ // The tool-fired path carries no reply text — skipped, since the
40
+ // contract requires text and an empty event helps nobody.
41
+ // Nullable like sessionIndex: callers without an emitter (CLI, tests)
42
+ // dispatch exactly as before.
43
+ if (emit && output?.text) {
44
+ try { await emit("session.transcript.completed", { scope: "node", nodeId, session, text: output.text }); } catch {}
45
+ }
46
+ // needsCtx is threaded through both calling conventions as a new field /
47
+ // 5th positional arg. Additive: every existing apply() function ignores
48
+ // an argument/key it doesn't ask for, so this is a no-op for every apply()
49
+ // that hasn't opted in yet. output.toolResult carries the delivered row
50
+ // (when a tool fired); the row is consumed here so a retried turn never
51
+ // rereads a stale result.
52
+ try {
53
+ return usesNamedParams(node.apply)
54
+ ? await Promise.resolve(node.apply({ state: applyCtx, output, item: applyExtraArg ?? undefined, meta: applyMeta, needs: needsCtx }))
55
+ : applyExtraArg !== undefined
56
+ ? await Promise.resolve(node.apply(applyCtx, output, applyExtraArg, applyMeta, needsCtx))
57
+ : await Promise.resolve(node.apply(applyCtx, output, undefined, applyMeta, needsCtx));
58
+ } finally {
59
+ if (output?.toolResult) await clearPendingResult(session).catch(() => {});
60
+ }
61
+ }
62
+
63
+ return { dispatchAgentWithRetry };
64
+ }
@@ -0,0 +1,315 @@
1
+ // child-resume-trigger.js — wakes a parent flow once every child spawned by
2
+ // one subflow-node firing has gone terminal.
3
+ //
4
+ // Hooked into state/state-store.js's finish/skip/fail/cancel methods
5
+ // themselves (not engine/executor-persist.js's wrappers) — boot/flow-
6
+ // revival.js's excess-orphan-displacement path calls store.fail(...)
7
+ // directly, bypassing the persist wrappers entirely, so hooking at the
8
+ // wrapper level would silently miss waking a parent whose child got
9
+ // displaced at boot.
10
+ //
11
+ // Not wired into the live executor yet (real loop-node dispatch hasn't
12
+ // been cut over to it) — tested here against synthetic parent/child pairs.
13
+
14
+ import { isTerminal } from "./executor-helpers.js";
15
+ import { wakePromotedRelease } from "./admission-promotion-resume.js";
16
+
17
+ // Shared by maybeResumeParent (below) and boot/flow-revival.js's
18
+ // children-pending reconciliation pass — the same "is this subflow node's
19
+ // whole fan-out done" check, entered from two different directions (from a
20
+ // child walking up, or from the parent scanning down at boot). Returns
21
+ // `children` too (not just the boolean) so callers can also check
22
+ // per-child outcome — see subflowOutcomeSatisfied below.
23
+ export async function areSiblingsAllTerminal({ flowId, nodeId, runStore, nodeExecutionsStore }) {
24
+ const siblingIds = await nodeExecutionsStore.listInvocationIdsForNode(flowId, nodeId);
25
+ if (siblingIds.length === 0) return { allTerminal: false, reason: "no_siblings", children: [] };
26
+
27
+ const children = await runStore.listForParentInvocations(siblingIds);
28
+ // Eager creation spawns every child in one pass, but a resume check can
29
+ // race that pass (a sibling invocation exists with no child row yet) —
30
+ // bail rather than resume against an incomplete fan-out.
31
+ if (children.length !== siblingIds.length) return { allTerminal: false, reason: "not_all_spawned", children: [] };
32
+
33
+ const allTerminal = children.every((c) => isTerminal(c.status));
34
+ return { allTerminal, reason: allTerminal ? "ok" : "siblings_pending", children };
35
+ }
36
+
37
+ // A subflow node's `mode` ("any" | "all") decides whether its
38
+ // now-fully-terminal children satisfy the fan-out, checked once
39
+ // areSiblingsAllTerminal confirms every sibling is terminal. "any" only
40
+ // needs one success (correct for a genuine multi-item fan-out, where
41
+ // per-item failure is normal); "all" needs every child to have succeeded
42
+ // (correct for a single-item subflow like ENSURE_LOGIN, where the one
43
+ // attempt either worked or the whole point of gating on it failed). An
44
+ // unrecognized/missing mode fails open (never blocks the resume) —
45
+ // defensive only, the loader requires a valid mode on every subflow node,
46
+ // so this should never actually happen.
47
+ export function subflowOutcomeSatisfied(mode, children) {
48
+ if (mode === "any") return children.some((c) => c.status === "succeeded");
49
+ if (mode === "all") return children.every((c) => c.status === "succeeded");
50
+ return true;
51
+ }
52
+
53
+ // Shared by maybeResumeParent (below) and boot/flow-revival.js's
54
+ // children-pending reconciliation pass. `resume()` is just `runFlow(flowId)`
55
+ // (engine/executor.js) — and runFlow's own first check is "if this flow's
56
+ // status is still 'blocked', return immediately" (that guard exists so a
57
+ // stray/duplicate resume() call on an already-blocked flow is a safe no-op).
58
+ // Every OTHER caller of resume() in this codebase (gates/gate-resolver.js's
59
+ // decision-commit path) already flips status to 'running' via
60
+ // store.setRunning() BEFORE calling resume() — this mirrors that, using the
61
+ // lower-level runStore CAS directly since this module only depends on
62
+ // runStore/nodeExecutionsStore, not the full state-store. A concurrent
63
+ // caller racing the same flip loses the CAS (revision mismatch) and simply
64
+ // doesn't resume — never a double-dispatch.
65
+ //
66
+ // Repair propagation: a parent can already be terminal 'failed' here — the
67
+ // mode-not-satisfied path (maybeResumeParent below) fails a parent the
68
+ // moment its fan-out looks unsatisfiable, but a later retry of one of the
69
+ // surviving children can still turn that same fan-out satisfied afterward.
70
+ // When that happens and the caller supplies `repairGuard` (only
71
+ // maybeResumeParent does — the boot/flow-revival.js and
72
+ // engine/admission-promotion-resume.js callers don't, and get the
73
+ // pre-repair legacy behavior: `flip_failed:not_terminal`), a 'failed'
74
+ // parent is force-unblocked back to 'blocked' first, then the normal
75
+ // casSetRunning flip proceeds against the freshly-bumped revision.
76
+ // 'succeeded'/'cancelled'/'skipped' are never repaired — only 'failed' is
77
+ // eligible (a deliberate business outcome or an operator's cancel must
78
+ // stay final).
79
+ // Which admission-pool kind a flow's OWN admission (not any node inside
80
+ // it) belongs to — a root flow (no parent) competes as "flow-start"; a
81
+ // subflow-spawned child competes as "child-dispatch". Shared by every
82
+ // caller that needs to re-request admission for a flow it's about to
83
+ // resume — this file's own flipToRunningAndResume below, and gates/
84
+ // gate-resolver.js's, routes/operator.js's, and routes/operator-retry.js's
85
+ // own independent resume sequences (none of which share this file's
86
+ // low-level runStore-CAS shape, so they use admitAndSetRunning instead).
87
+ export function admissionKindForFlow(flow) {
88
+ return flow.parentInvocationId ? "child-dispatch" : "flow-start";
89
+ }
90
+
91
+ // Re-requests a flow's own admission (see admissionKindForFlow) before
92
+ // flipping it back to "running" through the FULL state-store (not
93
+ // runStore's raw CAS — used by every live resume path OUTSIDE this file:
94
+ // gates/gate-resolver.js, routes/operator.js's budget-gate resume, and
95
+ // routes/operator-retry.js). Necessary because the flow's admission was
96
+ // released the moment it left "running" (state-store.js's setWaiting/
97
+ // terminal hooks) — resuming must re-claim a slot, not assume one is
98
+ // still held. Returns a setRunning/setQueued-shaped result
99
+ // ({applied, code}) plus `queued: true` when the pool was full: the flow
100
+ // is left (or newly set) at status:"queued" instead of resumed, and the
101
+ // caller must skip whatever resume-dispatch it would otherwise do —
102
+ // admission-queue-store's own release()/promotion cycle resumes it later,
103
+ // same as any other queued admission.
104
+ export async function admitAndSetRunning({ flowId, expectedRevision, lastStep, stateJson, parentInvocationId, store, admissionQueueStore, runStore, resume, logger, api }) {
105
+ const kind = admissionKindForFlow({ parentInvocationId });
106
+ const admission = await admissionQueueStore.admit({ kind, flowId });
107
+ if (admission.status === "queued") {
108
+ const result = await store.setQueued({
109
+ flowId, expectedRevision, lastStep, stateJson,
110
+ waitJson: { kind },
111
+ statusReason: `queued: waiting for a shared admission slot to resume (${kind})`,
112
+ });
113
+ // Claimed a queue position but didn't end up using it (CAS lost) — a
114
+ // caller's own retry loop will call admit() fresh on its next attempt,
115
+ // so this one must be given back or it leaks a permanent, invisible
116
+ // pool-capacity hole (a row with no flow_runs counterpart ever pointing
117
+ // back at it). This admission's own status is 'queued', not 'admitted',
118
+ // so releasing it can never itself promote anything (admission-queue-
119
+ // store.js's release() only promotes on a genuinely admitted row) — no
120
+ // resumePromotedAdmission call needed here, unlike the setRunning branch
121
+ // below.
122
+ if (!result.applied) await admissionQueueStore.release(admission.id);
123
+ return { applied: result.applied, queued: true, code: result.code };
124
+ }
125
+ const result = await store.setRunning({ flowId, expectedRevision, lastStep, stateJson });
126
+ if (!result.applied) {
127
+ // Claimed a slot but didn't end up using it (CAS lost to a concurrent
128
+ // writer) — give it back rather than leak it. This admission WAS
129
+ // genuinely 'admitted', so releasing it can promote a different flow's
130
+ // queued row from the shared combined pool — drive wakePromotedRelease
131
+ // on that release the same as every other release call site in this
132
+ // codebase, or the promoted flow is stranded exactly like the orphaned-
133
+ // promotion incidents this whole mechanism exists to prevent.
134
+ const released = await admissionQueueStore.release(admission.id);
135
+ await wakePromotedRelease(released, {
136
+ runStore, resume, logger, api, context: "child-resume-trigger: admitAndSetRunning CAS-loss release",
137
+ });
138
+ }
139
+ return { applied: result.applied, queued: false, code: result.code };
140
+ }
141
+
142
+ // `admissionQueueStore`, when provided, re-requests the flow's own
143
+ // admission before flipping it running (see admitAndSetRunning above,
144
+ // same rationale — just against runStore's raw CAS instead of the full
145
+ // state-store, matching this function's existing low-level shape). Omit
146
+ // it for a caller that already KNOWS it holds a live admission and is
147
+ // just acting on a promotion (e.g. engine/admission-promotion-resume.js) —
148
+ // passing it there would claim a SECOND, duplicate admission for the same
149
+ // flow+kind.
150
+ export async function flipToRunningAndResume({ parentFlowId, runStore, resume, logger, repairGuard, admissionQueueStore, api }) {
151
+ const parentRow = await runStore.get(parentFlowId);
152
+ if (!parentRow) return { resumed: false, reason: "parent_not_found" };
153
+
154
+ let expectedRevision = parentRow.revision;
155
+
156
+ if (parentRow.status === "failed" && repairGuard) {
157
+ const { nodeExecutionsStore, nodeId, subflowKey, expectedInvocationId } = repairGuard;
158
+ const latest = await nodeExecutionsStore.getLatestInvocationForNode(parentFlowId, nodeId, subflowKey);
159
+ if (!latest || latest.id !== expectedInvocationId) {
160
+ return { resumed: false, reason: "repair_stale_invocation" };
161
+ }
162
+ const unblock = await runStore.forceBlock(parentFlowId, {
163
+ expectedRevision,
164
+ stateJson: parentRow.stateJson,
165
+ statusReason: `repaired: subflow "${nodeId}" outcome later satisfied by a retried child`,
166
+ ownerKey: parentRow.ownerKey,
167
+ });
168
+ if (!unblock.applied) return { resumed: false, reason: `repair_forceBlock_failed:${unblock.code}` };
169
+ expectedRevision += 1;
170
+ }
171
+
172
+ let admissionId = null;
173
+ if (admissionQueueStore) {
174
+ const kind = admissionKindForFlow(parentRow);
175
+ const admission = await admissionQueueStore.admit({ kind, flowId: parentFlowId });
176
+ if (admission.status === "queued") {
177
+ const queued = await runStore.casSetQueued(parentFlowId, {
178
+ expectedRevision,
179
+ lastStep: parentRow.lastStep,
180
+ stateJson: parentRow.stateJson,
181
+ waitJson: { kind },
182
+ statusReason: `queued: waiting for a shared admission slot to resume (${kind})`,
183
+ });
184
+ if (!queued.applied) await admissionQueueStore.release(admission.id);
185
+ return { resumed: false, reason: queued.applied ? "queued_for_admission" : `queue_failed:${queued.code}` };
186
+ }
187
+ admissionId = admission.id;
188
+ }
189
+
190
+ const flip = await runStore.casSetRunning(parentFlowId, {
191
+ expectedRevision,
192
+ lastStep: parentRow.lastStep,
193
+ stateJson: parentRow.stateJson,
194
+ });
195
+ if (!flip.applied) {
196
+ if (admissionId) {
197
+ // This admission WAS genuinely 'admitted' (the branch above only
198
+ // reaches here once admit() returned status:"admitted"), so releasing
199
+ // it can promote a DIFFERENT flow's queued row — drive
200
+ // wakePromotedRelease on that release, same as every other release
201
+ // call site in this codebase, or the promoted flow is stranded
202
+ // exactly like the orphaned-promotion incidents this mechanism exists
203
+ // to prevent.
204
+ const released = await admissionQueueStore.release(admissionId);
205
+ await wakePromotedRelease(released, {
206
+ runStore, resume, logger, api, context: "child-resume-trigger: flipToRunningAndResume CAS-loss release",
207
+ });
208
+ }
209
+ return { resumed: false, reason: `flip_failed:${flip.code}` };
210
+ }
211
+
212
+ try {
213
+ await resume(parentFlowId);
214
+ return { resumed: true, parentFlowId };
215
+ } catch (err) {
216
+ logger?.warn?.(`child-resume-trigger: resume(${parentFlowId}) failed: ${err?.message ?? err}`);
217
+ return { resumed: false, reason: "resume_threw", error: err?.message ?? String(err) };
218
+ }
219
+ }
220
+
221
+ // maybeResumeParent checks whether `flowId` (a flow that just went terminal)
222
+ // is a subflow-spawned child, and if so, whether every sibling under the same
223
+ // (parent flowId, nodeId) is also terminal. If so — and the subflow node's
224
+ // `mode` is satisfied (see subflowOutcomeSatisfied above) —
225
+ // resumes the parent. If `mode` is NOT satisfied, fails the parent directly
226
+ // at this node via `failFlow` instead of resuming it, so a mode-mismatched
227
+ // fan-out never silently continues into its normal next step. `pipelinesRef`
228
+ // is optional — if omitted (or the node/pipeline can't be resolved), mode
229
+ // enforcement is skipped and behavior is the pre-mode-check unconditional
230
+ // resume; only real callers wire it, tests may omit it deliberately.
231
+ // Fire-and-forget by design — callers should not await this inline with a
232
+ // CAS write; call it after, uninstrumented failures are logged and swallowed.
233
+ //
234
+ // `failureStore` is likewise optional (tests may omit it) — when provided,
235
+ // a mode-not-satisfied failure ALSO gets a real failure_events row.
236
+ // Without this, `failFlow` alone is a bare status write with no
237
+ // invocationId/executionId, so it never reaches recordEngineFailure through
238
+ // any other path — the parent flow fails with no Inbox entry and no retry
239
+ // button.
240
+ export async function maybeResumeParent({ flowId, runStore, nodeExecutionsStore, pipelinesRef, resume, failFlow, failureStore, admissionQueueStore, logger, api }) {
241
+ if (typeof resume !== "function") return { resumed: false, reason: "no_resume_fn" };
242
+
243
+ const row = await runStore.get(flowId);
244
+ if (!row?.parentInvocationId) return { resumed: false, reason: "not_a_child" };
245
+
246
+ const invocation = await nodeExecutionsStore.getInvocation(row.parentInvocationId);
247
+ if (!invocation) return { resumed: false, reason: "invocation_not_found" };
248
+
249
+ const { allTerminal, reason, children } = await areSiblingsAllTerminal({
250
+ flowId: invocation.flowId, nodeId: invocation.nodeId, runStore, nodeExecutionsStore,
251
+ });
252
+ if (!allTerminal) return { resumed: false, reason };
253
+
254
+ const parentFlow = await runStore.get(invocation.flowId);
255
+ if (!parentFlow) return { resumed: false, reason: "parent_not_found" };
256
+
257
+ const mode = pipelinesRef?.value?.get(parentFlow.pipelineId)?.nodes?.[invocation.nodeId]?.mode;
258
+ if (mode && !subflowOutcomeSatisfied(mode, children)) {
259
+ if (typeof failFlow !== "function") {
260
+ logger?.warn?.(`child-resume-trigger: subflow "${invocation.nodeId}" mode:"${mode}" not satisfied but no failFlow provided — resuming anyway`);
261
+ } else {
262
+ const statusReason = `subflow "${invocation.nodeId}" (mode:"${mode}") not satisfied — ${mode === "any" ? "every child failed" : "at least one child did not succeed"}`;
263
+ const result = await failFlow({
264
+ flowId: invocation.flowId,
265
+ expectedRevision: parentFlow.revision,
266
+ stateJson: parentFlow.stateJson,
267
+ statusReason,
268
+ });
269
+ if (result?.applied && typeof failureStore?.recordEngineFailure === "function") {
270
+ // `invocation` above is the CHILD's own parentInvocationId — per
271
+ // spawn-child.js's resolveOrCreateChild, that's the PER-ITEM
272
+ // invocation (subflowKey = this specific item's key), which
273
+ // deliberately never gets a flow_node_executions row at all (see
274
+ // state/failure-store.js's computeResolvedAt comment on the same
275
+ // dead end). The subflow NODE's own dispatch — the one
276
+ // routes/runs.js's buildTreeNodeRows actually renders a row for —
277
+ // is a DIFFERENT invocation, keyed by subflowKey:null. Must look
278
+ // that one up separately; reusing invocation.id/.subflowKey here
279
+ // would silently record against an invocation with no executions.
280
+ const ownInvocation = await nodeExecutionsStore.getLatestInvocationForNode(
281
+ invocation.flowId, invocation.nodeId, null,
282
+ );
283
+ const latest = await nodeExecutionsStore.getLatestExecutionForNode({
284
+ flowId: invocation.flowId, nodeId: invocation.nodeId, subflowKey: null,
285
+ });
286
+ await failureStore.recordEngineFailure({
287
+ flowId: invocation.flowId,
288
+ pipelineId: parentFlow.pipelineId,
289
+ failureNode: invocation.nodeId,
290
+ message: statusReason,
291
+ state: parentFlow.stateJson,
292
+ invocationId: ownInvocation?.id ?? null,
293
+ executionId: latest?.executionId ?? null,
294
+ errorType: "subflow-mode-not-satisfied",
295
+ });
296
+ }
297
+ return { resumed: false, reason: "mode_not_satisfied", failed: !!result?.applied };
298
+ }
299
+ }
300
+
301
+ return flipToRunningAndResume({
302
+ parentFlowId: invocation.flowId,
303
+ runStore,
304
+ resume,
305
+ logger,
306
+ admissionQueueStore,
307
+ api,
308
+ repairGuard: {
309
+ nodeExecutionsStore,
310
+ nodeId: invocation.nodeId,
311
+ subflowKey: invocation.subflowKey,
312
+ expectedInvocationId: row.parentInvocationId,
313
+ },
314
+ });
315
+ }
@@ -0,0 +1,87 @@
1
+ import { nodeCallStats } from "../state/node-cost.js";
2
+
3
+ export function createBudgetGate({ getApprovalQueue, logger, nodeCallStats: nodeCallStatsFn = nodeCallStats }) {
4
+ async function maybeBudgetGate({ flowId, nodeId, pipelineId, pipelineName, node, itemCount = 1, items = [] }) {
5
+ const aq = getApprovalQueue?.();
6
+ if (!aq || typeof aq.isGateLocked !== "function") return { proceed: true };
7
+
8
+ // isGateLocked() awaits its own store attachment in place (see
9
+ // budget/index.js) rather than throwing a special "not ready" error —
10
+ // any throw reaching here is a genuine failure and must propagate, not
11
+ // be silently treated as "not locked".
12
+ const locked = await aq.isGateLocked();
13
+ if (!locked) return { proceed: true };
14
+
15
+ const decision = typeof aq.lookupNodeDecision === "function"
16
+ ? await aq.lookupNodeDecision({ flowId, nodeId })
17
+ : null;
18
+ if (decision === "approve") return { proceed: true };
19
+ if (decision === "reject") return { skip: true };
20
+ if (decision === "pending") {
21
+ return { pause: true, blockedInfo: budgetGateBlockedInfo(nodeId, null) };
22
+ }
23
+
24
+ // No item yet — create one and pause
25
+ const estimate = await estimateNodeCost(nodeCallStatsFn, node, itemCount, pipelineId, nodeId);
26
+ const itemLabel = formatNodeLabel(nodeId, node, itemCount);
27
+ const brief = formatNodeBrief(node, items, itemCount);
28
+ let itemId = null;
29
+ try {
30
+ itemId = await aq.enqueueNodeApproval({
31
+ flowId,
32
+ nodeId,
33
+ pipeline: pipelineName,
34
+ pipelineId,
35
+ label: itemLabel,
36
+ agent: node?.agent ?? "manifold",
37
+ agentEmoji: agentEmojiFor(node?.agent),
38
+ trigger: "cron",
39
+ brief,
40
+ costLow: estimate.low,
41
+ costHigh: estimate.high,
42
+ modelChain: [],
43
+ tokenEstimate: null,
44
+ });
45
+ logger?.info?.(`manifold [${flowId.slice(0,8)}] ${nodeId}: budget gate locked — queued item=${itemId} (~$${estimate.low.toFixed(4)}-$${estimate.high.toFixed(4)})`);
46
+ } catch (err) {
47
+ logger?.warn?.(`manifold [${flowId.slice(0,8)}] ${nodeId}: enqueue failed: ${err?.message ?? err}`);
48
+ }
49
+ return { pause: true, blockedInfo: budgetGateBlockedInfo(nodeId, itemId) };
50
+ }
51
+
52
+ return { maybeBudgetGate };
53
+ }
54
+
55
+ function budgetGateBlockedInfo(_nodeId, _itemId) {
56
+ return {
57
+ _kind: "budget-gate-approval",
58
+ reason: "daily budget cap reached — node held for operator approval",
59
+ hint: "approve or reject from your companion app",
60
+ };
61
+ }
62
+
63
+ async function estimateNodeCost(nodeCallStatsFn, _node, itemCount, pipelineId, nodeId) {
64
+ const stats = await nodeCallStatsFn(pipelineId, nodeId);
65
+ if (stats) {
66
+ return { low: stats.p25 * itemCount, high: stats.p75 * itemCount };
67
+ }
68
+ return { low: 0.001 * itemCount, high: 0.05 * itemCount };
69
+ }
70
+
71
+ function formatNodeLabel(nodeId, _node, itemCount) {
72
+ if (itemCount > 1) return `${nodeId} × ${itemCount}`;
73
+ return nodeId;
74
+ }
75
+
76
+ function formatNodeBrief(_node, items, itemCount) {
77
+ if (!items?.length) return `${itemCount} call(s) awaiting approval`;
78
+ const preview = items.slice(0, 3).map((it) => it?.id ?? it?.listingId ?? it?.title ?? String(it)).join(", ");
79
+ return `${itemCount} item(s): ${preview}${items.length > 3 ? "…" : ""}`;
80
+ }
81
+
82
+ function agentEmojiFor(agent) {
83
+ return ({
84
+ scorer: "⚖️", "content-writer": "✍️", "proposal-sender": "📤",
85
+ "marketplace-scout": "🔭", "agent-forger": "🔨",
86
+ })[agent] ?? "🤖";
87
+ }
@@ -0,0 +1,83 @@
1
+ // Shared cancel-completion logic for both executor.cancel() and flow-revival.js.
2
+ //
3
+ // Precondition: cancelRequestedAt is already written on the flow (step 1 done).
4
+ //
5
+ // Sequence:
6
+ // step 2d — cascade-cancel non-terminal childFlows children (Phase 2),
7
+ // recursive: a child's own childFlows subflow cascades too.
8
+ // step 3 — store.cancel() → writes status='cancelled' to DB
9
+ //
10
+ // KNOWN GAP (not fixed here — CANCEL_SESSION_ABORT.design.md): neither step
11
+ // actually interrupts a `kind:"agent"` node that's currently mid-dispatch,
12
+ // root or subflow. Cancel only flips `flow_runs.status`; the in-flight LLM
13
+ // call keeps running to natural completion and its result is silently
14
+ // discarded once it tries to persist against an already-terminal flow (a
15
+ // harmless CAS rejection, no corruption — but no abort, no record of what
16
+ // the agent did in the meantime either). See the design doc above for what
17
+ // a real fix would need.
18
+
19
+ import { getNodeExecutionsStore } from "../state/node-executions.js";
20
+
21
+ const TERMINAL_STATUSES = new Set(["succeeded", "failed", "cancelled", "skipped"]);
22
+
23
+ // Phase 2: find every non-terminal childFlows child of `flowId` and cancel
24
+ // each one — recursively, so a child that itself has a childFlows subflow
25
+ // cascades further down. Without this, cancelling a parent would leave live
26
+ // children (and any in-flight agent session they're running — e.g. an
27
+ // actual outbound send) completely untouched. Walks every subflow node this
28
+ // flow has real children under
29
+ // (nodeExecutionsStore.listSubflowNodeIdsForFlow), not just one — a flow
30
+ // can have more than one childFlows subflow node.
31
+ async function cascadeCancelChildren({ store, flowId, cfg, logger, nodeExecutions, runStore }) {
32
+ if (!runStore || !nodeExecutions) return;
33
+ let subflowNodeIds;
34
+ try {
35
+ subflowNodeIds = await nodeExecutions.listSubflowNodeIdsForFlow(flowId);
36
+ } catch {
37
+ return;
38
+ }
39
+ if (!subflowNodeIds || subflowNodeIds.length === 0) return;
40
+
41
+ // The reason is unambiguous here (unlike the top-level cancel, where the
42
+ // caller may or may not have one) — every cascaded child is cancelled for
43
+ // exactly one cause, so it's always worth recording rather than left NULL.
44
+ const childReason = `parent flow ${flowId.slice(0, 8)} was cancelled`;
45
+
46
+ for (const nodeId of subflowNodeIds) {
47
+ const invocationIds = await nodeExecutions.listInvocationIdsForNode(flowId, nodeId);
48
+ const children = await runStore.listForParentInvocations(invocationIds);
49
+ for (const child of children) {
50
+ if (TERMINAL_STATUSES.has(child.status)) continue;
51
+ try {
52
+ await store.requestCancel({ flowId: child.flowId });
53
+ await completeCancellation({
54
+ store, flowId: child.flowId, stateJson: child.stateJson,
55
+ lastStep: child.lastStep, revision: child.revision, cfg, logger,
56
+ nodeExecutionsStore: nodeExecutions, runStore, reason: childReason,
57
+ });
58
+ } catch (err) {
59
+ logger?.warn?.(`completeCancellation: cascade-cancel of child [${child.flowId.slice(0, 8)}] failed: ${err?.message ?? err}`);
60
+ }
61
+ }
62
+ }
63
+ }
64
+
65
+ export async function completeCancellation({ store, flowId, stateJson, lastStep, revision, cfg = {}, logger = null, nodeExecutionsStore, runStore, reason = null }) {
66
+ // Injectable for tests (defaults to the real singleton in production) —
67
+ // same DI pattern as engine/executor.js.
68
+ const nodeExecutions = nodeExecutionsStore ?? getNodeExecutionsStore();
69
+
70
+ // Step 2d: cascade-cancel non-terminal childFlows children, if any — see
71
+ // cascadeCancelChildren's own comment. Best-effort: a failure here (or a
72
+ // caller that doesn't pass runStore, e.g. older call sites) shouldn't
73
+ // block this flow's own terminal status flip (step 3) below.
74
+ try {
75
+ await cascadeCancelChildren({ store, flowId, cfg, logger, nodeExecutions, runStore });
76
+ } catch (err) {
77
+ logger?.warn?.(`completeCancellation: cascadeCancelChildren(${flowId.slice(0, 8)}) failed: ${err?.message ?? err}`);
78
+ }
79
+
80
+ // Step 3: flip flow status to 'cancelled'.
81
+ const result = await store.cancel({ flowId, cfg, reason });
82
+ return result;
83
+ }