@igorjpimenta/manifold 0.0.0-bootstrap.1 → 0.0.0-stage

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/README.md +2 -1583
  2. package/package.json +4 -49
  3. package/.migrations/ops/0000_futuristic_karma.sql +0 -111
  4. package/.migrations/ops/0001_low_maximus.sql +0 -7
  5. package/.migrations/ops/meta/0000_snapshot.json +0 -763
  6. package/.migrations/ops/meta/0001_snapshot.json +0 -173
  7. package/.migrations/ops/meta/_journal.json +0 -20
  8. package/.migrations/runtime/0000_real_boomer.sql +0 -180
  9. package/.migrations/runtime/0001_hot_dakota_north.sql +0 -2
  10. package/.migrations/runtime/0002_breezy_vertigo.sql +0 -8
  11. package/.migrations/runtime/0003_dashing_jack_flag.sql +0 -5
  12. package/.migrations/runtime/0004_panoramic_ego.sql +0 -1
  13. package/.migrations/runtime/0005_tidy_ben_grimm.sql +0 -10
  14. package/.migrations/runtime/0006_wet_nightmare.sql +0 -57
  15. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +0 -8
  16. package/.migrations/runtime/0008_wealthy_thundra.sql +0 -301
  17. package/.migrations/runtime/0009_lying_shocker.sql +0 -96
  18. package/.migrations/runtime/0010_solid_phantom_reporter.sql +0 -160
  19. package/.migrations/runtime/0011_windy_demogoblin.sql +0 -175
  20. package/.migrations/runtime/0012_restore_harness_fks.sql +0 -103
  21. package/.migrations/runtime/meta/0000_snapshot.json +0 -1181
  22. package/.migrations/runtime/meta/0001_snapshot.json +0 -1209
  23. package/.migrations/runtime/meta/0002_snapshot.json +0 -1261
  24. package/.migrations/runtime/meta/0003_snapshot.json +0 -1292
  25. package/.migrations/runtime/meta/0004_snapshot.json +0 -1240
  26. package/.migrations/runtime/meta/0006_snapshot.json +0 -1310
  27. package/.migrations/runtime/meta/0007_snapshot.json +0 -1362
  28. package/.migrations/runtime/meta/0008_snapshot.json +0 -1742
  29. package/.migrations/runtime/meta/0009_snapshot.json +0 -2413
  30. package/.migrations/runtime/meta/0010_snapshot.json +0 -2562
  31. package/.migrations/runtime/meta/0011_snapshot.json +0 -2812
  32. package/.migrations/runtime/meta/0012_snapshot.json +0 -2812
  33. package/.migrations/runtime/meta/_journal.json +0 -97
  34. package/RELEASING.md +0 -89
  35. package/agent/fleet.js +0 -100
  36. package/bin/cli.js +0 -854
  37. package/bin/manifold.js +0 -4
  38. package/boot/flow-revival.js +0 -536
  39. package/budget/index.js +0 -495
  40. package/budget/lib.js +0 -59
  41. package/dashboard/cost.js +0 -528
  42. package/dashboard/dist/assets/index-CUfp63sj.css +0 -1
  43. package/dashboard/dist/assets/index-Qy9JMpma.js +0 -371
  44. package/dashboard/dist/index.html +0 -13
  45. package/dashboard/event-log.js +0 -267
  46. package/dashboard/index.html +0 -12
  47. package/dashboard/package-lock.json +0 -2877
  48. package/dashboard/package.json +0 -37
  49. package/dashboard/pipeline-graph.js +0 -131
  50. package/dashboard/postcss.config.js +0 -6
  51. package/dashboard/search.js +0 -183
  52. package/dashboard/server.js +0 -1015
  53. package/dashboard/src/App.tsx +0 -42
  54. package/dashboard/src/api/client.ts +0 -118
  55. package/dashboard/src/api/types.ts +0 -510
  56. package/dashboard/src/api/ws.tsx +0 -112
  57. package/dashboard/src/components/CancelButton.tsx +0 -63
  58. package/dashboard/src/components/ClickToCopy.tsx +0 -68
  59. package/dashboard/src/components/CopyButton.tsx +0 -61
  60. package/dashboard/src/components/DailyCostTrend.tsx +0 -85
  61. package/dashboard/src/components/DailyTimeTrend.tsx +0 -94
  62. package/dashboard/src/components/Funnel.tsx +0 -149
  63. package/dashboard/src/components/GateForm.tsx +0 -158
  64. package/dashboard/src/components/GraphView.tsx +0 -302
  65. package/dashboard/src/components/GroupPathsSummary.tsx +0 -47
  66. package/dashboard/src/components/Histogram.tsx +0 -292
  67. package/dashboard/src/components/Layout.tsx +0 -28
  68. package/dashboard/src/components/NeedsAttention.tsx +0 -98
  69. package/dashboard/src/components/NodeContextCard.tsx +0 -132
  70. package/dashboard/src/components/PathTrail.tsx +0 -35
  71. package/dashboard/src/components/PipelineCostBreakdown.tsx +0 -277
  72. package/dashboard/src/components/RerunButton.tsx +0 -46
  73. package/dashboard/src/components/RunComparison.tsx +0 -53
  74. package/dashboard/src/components/Sparkline.tsx +0 -94
  75. package/dashboard/src/components/StatStrip.tsx +0 -147
  76. package/dashboard/src/components/StateTree.tsx +0 -84
  77. package/dashboard/src/components/StatusBadge.tsx +0 -18
  78. package/dashboard/src/components/SubflowPathPills.tsx +0 -38
  79. package/dashboard/src/components/SubflowSummary.tsx +0 -24
  80. package/dashboard/src/components/SubflowsCard.tsx +0 -327
  81. package/dashboard/src/components/TodayStrip.tsx +0 -86
  82. package/dashboard/src/components/TranscriptPane.tsx +0 -99
  83. package/dashboard/src/components/TranscriptRow.tsx +0 -69
  84. package/dashboard/src/components/ui/badge.tsx +0 -38
  85. package/dashboard/src/components/ui/button.tsx +0 -28
  86. package/dashboard/src/components/ui/card.tsx +0 -42
  87. package/dashboard/src/components/ui/skeleton.tsx +0 -9
  88. package/dashboard/src/components/ui/table.tsx +0 -62
  89. package/dashboard/src/index.css +0 -26
  90. package/dashboard/src/lib/anomaly.ts +0 -42
  91. package/dashboard/src/lib/format.ts +0 -30
  92. package/dashboard/src/lib/step-history.ts +0 -13
  93. package/dashboard/src/lib/subflow.ts +0 -154
  94. package/dashboard/src/lib/use-click-outside.ts +0 -28
  95. package/dashboard/src/lib/use-ignored-clusters.ts +0 -67
  96. package/dashboard/src/lib/utils.ts +0 -6
  97. package/dashboard/src/main.tsx +0 -10
  98. package/dashboard/src/types/shims.d.ts +0 -4
  99. package/dashboard/src/views/Home.tsx +0 -478
  100. package/dashboard/src/views/PipelineDetail.tsx +0 -1185
  101. package/dashboard/src/views/PipelinesList.tsx +0 -111
  102. package/dashboard/src/views/RunDetail.tsx +0 -248
  103. package/dashboard/src/views/Search.tsx +0 -196
  104. package/dashboard/stats.js +0 -668
  105. package/dashboard/tailwind.config.ts +0 -43
  106. package/dashboard/tree-stats.js +0 -12
  107. package/dashboard/tsconfig.json +0 -25
  108. package/dashboard/views.js +0 -231
  109. package/dashboard/vite.config.ts +0 -34
  110. package/drizzle.config.js +0 -12
  111. package/drizzle.config.ops.js +0 -12
  112. package/elevation.js +0 -116
  113. package/engine/admission-promotion-resume.js +0 -127
  114. package/engine/agent-dispatch.js +0 -64
  115. package/engine/child-resume-trigger.js +0 -315
  116. package/engine/executor-budget.js +0 -87
  117. package/engine/executor-cancel.js +0 -83
  118. package/engine/executor-errors.js +0 -122
  119. package/engine/executor-helpers.js +0 -254
  120. package/engine/executor-nodes.js +0 -292
  121. package/engine/executor-persist.js +0 -212
  122. package/engine/executor-subflow-children.js +0 -275
  123. package/engine/executor.js +0 -1013
  124. package/engine/global-concurrency.js +0 -33
  125. package/engine/loader.js +0 -528
  126. package/engine/needs-resolver.js +0 -229
  127. package/engine/pipelines-watcher.js +0 -133
  128. package/engine/result-replay.js +0 -34
  129. package/engine/revival-messages.js +0 -11
  130. package/engine/spawn-child.js +0 -55
  131. package/engine/wait-for-flow-settlement.js +0 -109
  132. package/events/catalog.js +0 -26
  133. package/events/emit.js +0 -44
  134. package/events/kinds.js +0 -118
  135. package/events/serve-ws.js +0 -74
  136. package/events/stream.js +0 -35
  137. package/events/ws-dialog.js +0 -257
  138. package/events/ws-frame.js +0 -194
  139. package/events/ws-run-events.js +0 -70
  140. package/events.js +0 -32
  141. package/gates/gate-resolver.js +0 -576
  142. package/gates/paused-flows.js +0 -55
  143. package/graphql/columns.js +0 -67
  144. package/graphql/schema.js +0 -333
  145. package/index.js +0 -49
  146. package/mcp/http.js +0 -159
  147. package/mcp/identity.js +0 -94
  148. package/mcp/stdio.js +0 -80
  149. package/mcp/tools.js +0 -245
  150. package/mcp/wire-ops.js +0 -178
  151. package/routes/bearer-auth.js +0 -57
  152. package/routes/discuss-audio.js +0 -87
  153. package/routes/discuss-component.js +0 -39
  154. package/routes/discuss-draft.js +0 -159
  155. package/routes/discuss-helpers.js +0 -20
  156. package/routes/discuss.js +0 -848
  157. package/routes/failures.js +0 -104
  158. package/routes/graphql.js +0 -61
  159. package/routes/health.js +0 -54
  160. package/routes/operator-discuss.js +0 -193
  161. package/routes/operator-resume.js +0 -32
  162. package/routes/operator-retry.js +0 -301
  163. package/routes/operator.js +0 -539
  164. package/routes/pipelines.js +0 -118
  165. package/routes/runs.js +0 -1129
  166. package/scripts/ci/import-check.mjs +0 -50
  167. package/scripts/link-core-self.mjs +0 -31
  168. package/scripts/with-guard.sh +0 -24
  169. package/server/app.js +0 -503
  170. package/server/registry.js +0 -53
  171. package/server/serve.js +0 -163
  172. package/state/admission-queue-store.js +0 -325
  173. package/state/attribution-registries.js +0 -102
  174. package/state/batch-writer.js +0 -162
  175. package/state/db-guard.js +0 -63
  176. package/state/db-health.js +0 -27
  177. package/state/db.js +0 -122
  178. package/state/discuss-store.js +0 -299
  179. package/state/failure-store.js +0 -427
  180. package/state/migrate.js +0 -104
  181. package/state/node-cost.js +0 -67
  182. package/state/node-executions.js +0 -775
  183. package/state/node-health-store.js +0 -124
  184. package/state/ops-merge.js +0 -93
  185. package/state/paths.js +0 -146
  186. package/state/pipeline-dirs.js +0 -99
  187. package/state/pipeline-registry.js +0 -449
  188. package/state/run-store.js +0 -697
  189. package/state/schema.js +0 -438
  190. package/state/schema.ops.js +0 -44
  191. package/state/session-index.js +0 -177
  192. package/state/session-keys.js +0 -39
  193. package/state/state-store.js +0 -311
  194. package/state/subflow-state.js +0 -126
  195. package/state/tool-handoff.js +0 -169
  196. package/telemetry/budget-alert.js +0 -353
  197. package/telemetry/cli.js +0 -331
  198. package/telemetry/grammar.js +0 -119
  199. package/telemetry/http-budget.js +0 -132
  200. package/telemetry/http-cost.js +0 -670
  201. package/telemetry/http-helpers.js +0 -134
  202. package/telemetry/http-tree.js +0 -349
  203. package/telemetry/http.js +0 -44
  204. package/telemetry/index.js +0 -307
  205. package/telemetry/ingest.js +0 -134
  206. package/telemetry/labels.js +0 -115
  207. package/telemetry/pricer.js +0 -77
  208. package/telemetry/resolver.js +0 -314
  209. package/telemetry/store.js +0 -147
  210. package/utils/run-patch.js +0 -43
@@ -1,668 +0,0 @@
1
- // stats.js — aggregations over flow_runs.
2
- //
3
- // All computations are pure functions taking the array of rows returned by
4
- // store.list(). At ~60 rows this runs in well under a millisecond, so the
5
- // endpoint recomputes on every request — no caching needed.
6
-
7
- import { getSubflowGroup } from "../state/subflow-state.js";
8
- //
9
- // Funnel logic (computePipelineStats):
10
- // 1. If the pipeline declares `funnel: [...]` in its index.js, use it.
11
- // Stage shape:
12
- // { id, label, key, size: "array" | "objectKeys" | "objectKeysNoError" }
13
- // { id, label, size: (state) => number } // subflow-derived
14
- // The function form lets in-flight progress show up in the funnel
15
- // before a terminal step rolls per-subflow state into top-level slots.
16
- // 2. Otherwise, auto-derive a node-visit funnel from pipeline.nodes
17
- // in declaration order — each stage counts how many runs reached
18
- // that node, per a batched visitedByFlow map
19
- // (flow_node_invocations, not state._stepHistory). Generic, agnostic
20
- // to pipeline content; the cost breakdown carries the same per-node
21
- // calls with cost/latency, so the auto funnel adds the per-stage
22
- // attrition story for pipelines without a custom declaration.
23
-
24
- const TERMINAL = new Set(["succeeded", "failed", "cancelled"]);
25
- // "waiting" is never actually written anywhere (confirmed repo-wide) — gate
26
- // pauses are always persisted as "blocked".
27
- const ACTIVE = new Set(["running", "blocked"]);
28
-
29
- /**
30
- * Resolve the "display step" for a run — the node the run was actually
31
- * at when it terminated / is currently at.
32
- *
33
- * The registry's `last_step` field (exposed here as `run.lastStep`) is
34
- * the last node that successfully completed (executor.js's
35
- * persistRunningRetrying writes it AFTER node.run() resolves). So for
36
- * any non-succeeded run it points to the PRIOR node, not the
37
- * throwing/running/blocking one — the executor relies on this
38
- * invariant for resume (it walks `edges[lastStep]` to find the next
39
- * node on unblock). This function only reads `lastStep`; the separate
40
- * `current_step` field (node currently in flight, written BEFORE
41
- * execution) is a different column and isn't consulted here.
42
- *
43
- * For failed runs, use the LAST entry in `runErrors` (sourced from
44
- * flow_node_executions, not the old state.errors[]) — the executor
45
- * records the throw site there. Handles cascade sub-nodes and other
46
- * subflow-internal failures the registry's lastStep can't see (e.g.
47
- * ENSURE_LOGIN_CODE fails, the cascade routes to ENSURE_LOGIN_AGENT,
48
- * that times out — the error entry has `nodeId: "ENSURE_LOGIN_AGENT"`
49
- * even though `lastStep` registry value is still `ENSURE_LOGIN_CODE`).
50
- *
51
- * Falls back to the registry value for every other case (succeeded /
52
- * cancelled / blocked / running, or a failed run with no runErrors
53
- * entry) — same as any client's own RunsAPI, which reads currentStep/lastStep
54
- * directly off the run row rather than walking a pipeline's edge
55
- * function against stateJson (dashboard/server.js has no direct DB
56
- * access to state that would make that walk correct here either —
57
- * state._subflowGroups is dispatch-scoped, never persisted).
58
- *
59
- * Presentation-only — the executor still reads the raw registry value.
60
- */
61
- export function resolveDisplayStep(run, runErrors = []) {
62
- if (!run) return null;
63
- if (run.status === "succeeded") return run.lastStep ?? null;
64
-
65
- if (run.status === "failed" && runErrors.length > 0) {
66
- const last = runErrors[runErrors.length - 1];
67
- if (last?.nodeId) return last.nodeId;
68
- }
69
-
70
- return run.lastStep ?? null;
71
- }
72
-
73
- // Shallow-copy runs with `lastStep` replaced by the resolved value
74
- // so downstream aggregations see the corrected semantics without each
75
- // site having to call the resolver itself. errorsByFlow is the
76
- // Map<flowId, ErrorEntry[]> callers fetch once via the gateway's
77
- // GET /manifold/errors (failureStore.queryErrorsByFlowIds, Phase 1.5) —
78
- // this function stays DB-free, just threading the lookup through.
79
- function withResolvedSteps(runs, errorsByFlow = null) {
80
- return runs.map((r) => {
81
- const resolved = resolveDisplayStep(r, errorsByFlow?.get(r.flowId) ?? []);
82
- if (resolved === r.lastStep) return r;
83
- return { ...r, lastStep: resolved };
84
- });
85
- }
86
-
87
- export function computeGlobalStats(runs, errorsByFlow = null) {
88
- runs = withResolvedSteps(runs, errorsByFlow);
89
- const counts = countByStatus(runs);
90
- const total = runs.length;
91
- const succeeded = counts.succeeded ?? 0;
92
- const failed = counts.failed ?? 0;
93
- const cancelled = counts.cancelled ?? 0;
94
- const active = (counts.running ?? 0) + (counts.waiting ?? 0) + (counts.blocked ?? 0);
95
- const terminated = succeeded + failed + cancelled;
96
- const successRate = terminated > 0 ? succeeded / terminated : null;
97
-
98
- const succeededRuns = runs.filter((r) => r.status === "succeeded");
99
- const durations = succeededRuns
100
- .map((r) => durationMs(r))
101
- .filter((d) => d != null && d >= 0);
102
-
103
- // Per-day count over the last 30 days, only days that actually had
104
- // activity. Matches the cost section's window (30d) and the cost
105
- // chart's behaviour (gaps not rendered as empty bars — the chart
106
- // expands to fill available width across the active days only).
107
- const now = Date.now();
108
- const dayMs = 86_400_000;
109
- const buckets = [];
110
- for (let i = 29; i >= 0; i--) {
111
- const start = startOfDay(now - i * dayMs);
112
- const end = start + dayMs;
113
- const day = runs.filter((r) => (r.createdAt ?? 0) >= start && (r.createdAt ?? 0) < end);
114
- if (day.length === 0) continue;
115
- buckets.push({
116
- day: new Date(start).toISOString().slice(0, 10),
117
- total: day.length,
118
- succeeded: day.filter((r) => r.status === "succeeded").length,
119
- failed: day.filter((r) => r.status === "failed").length,
120
- cancelled: day.filter((r) => r.status === "cancelled").length,
121
- });
122
- }
123
-
124
- // Failure-step histogram (where do failed/cancelled runs end?). Each
125
- // entry tracks p50 block-time alongside the count so the operator can
126
- // sort by total impact, not just by frequency.
127
- const failureStops = stepImpactHistogram(
128
- runs.filter((r) => r.status === "failed" || r.status === "cancelled"), {}, errorsByFlow,
129
- );
130
-
131
- return {
132
- total,
133
- statusCounts: counts,
134
- succeeded, failed, cancelled, active, terminated,
135
- successRate,
136
- duration: {
137
- n: durations.length,
138
- p50Ms: percentile(durations, 0.5),
139
- p95Ms: percentile(durations, 0.95),
140
- meanMs: mean(durations),
141
- },
142
- perDay: buckets,
143
- failureStops,
144
- pipelineBreakdown: pipelineBreakdown(runs),
145
- };
146
- }
147
-
148
- export async function computePipelineStats(runs, pipelineId, pipeline = null, errorsByFlow = null, runStore = null, nodeExecutionsStore = null, visitedByFlow = null, lineageByNodeId = null) {
149
- runs = withResolvedSteps(runs, errorsByFlow);
150
- const mine = runs.filter((r) => (r.stateJson?.pipelineId ?? "") === pipelineId);
151
- const counts = countByStatus(mine);
152
- const total = mine.length;
153
- const succeeded = counts.succeeded ?? 0;
154
- const failed = counts.failed ?? 0;
155
- const cancelled = counts.cancelled ?? 0;
156
- const terminated = succeeded + failed + cancelled;
157
- const successRate = terminated > 0 ? succeeded / terminated : null;
158
-
159
- const durations = mine.filter((r) => r.status === "succeeded")
160
- .map(durationMs).filter((d) => d != null && d >= 0);
161
-
162
- const failureStops = stepImpactHistogram(
163
- mine.filter((r) => r.status === "failed" || r.status === "cancelled"), {}, errorsByFlow,
164
- );
165
-
166
- const revStats = mine.filter((r) => r.status === "succeeded").map((r) => r.revision ?? 0);
167
-
168
- // Funnel samples succeeded + active runs (running/waiting/blocked). Active
169
- // runs' branches contribute their in-progress counts; failed and cancelled
170
- // are excluded since their attrition is captured by the failure histogram.
171
- const funnelDef = Array.isArray(pipeline?.funnel) && pipeline.funnel.length > 0
172
- ? pipeline.funnel
173
- : autoFunnelFromNodes(pipeline, visitedByFlow, lineageByNodeId);
174
- const funnel = funnelDef && funnelDef.length > 0
175
- ? await computeFunnel(mine.filter((r) => r.status === "succeeded" || ACTIVE.has(r.status)), funnelDef, runStore, nodeExecutionsStore)
176
- : null;
177
-
178
- // Recent runs ordered by createdAt desc (for sparkline / table on detail page).
179
- const recent = [...mine].sort((a, b) => (b.createdAt ?? 0) - (a.createdAt ?? 0)).slice(0, 30);
180
-
181
- // Last-24h rollup for the per-pipeline TodayStrip — same window /
182
- // status breakdown as the home overview, just scoped to this
183
- // pipeline. updatedAt is the canonical "last touched" timestamp
184
- // (createdAt would miss a long-running flow that started before
185
- // the window but is still active).
186
- const since = Date.now() - 86_400_000;
187
- const last24h = mine.filter((r) => (r.updatedAt ?? 0) >= since);
188
- const recent24h = {
189
- total: last24h.length,
190
- ok: last24h.filter((r) => r.status === "succeeded").length,
191
- failed: last24h.filter((r) => r.status === "failed").length,
192
- active: last24h.filter((r) => r.status === "running" || r.status === "blocked").length,
193
- };
194
-
195
- return {
196
- pipelineId,
197
- recent24h,
198
- total,
199
- statusCounts: counts,
200
- succeeded, failed, cancelled, terminated,
201
- successRate,
202
- duration: {
203
- n: durations.length,
204
- p50Ms: percentile(durations, 0.5),
205
- p95Ms: percentile(durations, 0.95),
206
- meanMs: mean(durations),
207
- },
208
- revision: {
209
- n: revStats.length,
210
- p50: percentile(revStats, 0.5),
211
- p95: percentile(revStats, 0.95),
212
- },
213
- failureStops,
214
- funnel,
215
- recentRuns: recent.map(summariseForChart),
216
- };
217
- }
218
-
219
- export function compareRunToMedian(run, allRunsOfSamePipeline) {
220
- const peers = allRunsOfSamePipeline.filter((r) => r.flowId !== run.flowId && r.status === "succeeded");
221
- if (peers.length === 0) return null;
222
- const peerDurations = peers.map(durationMs).filter((d) => d != null && d >= 0);
223
- const medianMs = percentile(peerDurations, 0.5);
224
- const peerRevs = peers.map((r) => r.revision ?? 0);
225
- const medianRev = percentile(peerRevs, 0.5);
226
- return {
227
- peerCount: peers.length,
228
- durationMs: durationMs(run),
229
- durationMedianMs: medianMs,
230
- durationRatio: durationMs(run) != null && medianMs ? durationMs(run) / medianMs : null,
231
- revision: run.revision ?? 0,
232
- revisionMedian: medianRev,
233
- };
234
- }
235
-
236
- // ─── helpers ─────────────────────────────────────────────────────────────────
237
-
238
- function countByStatus(runs) {
239
- const out = {};
240
- for (const r of runs) out[r.status] = (out[r.status] ?? 0) + 1;
241
- return out;
242
- }
243
-
244
- function pipelineBreakdown(runs) {
245
- const map = new Map();
246
- for (const r of runs) {
247
- const id = r.stateJson?.pipelineId ?? "?";
248
- if (!map.has(id)) map.set(id, { pipelineId: id, total: 0, succeeded: 0, failed: 0, cancelled: 0, active: 0 });
249
- const entry = map.get(id);
250
- entry.total++;
251
- if (r.status === "succeeded") entry.succeeded++;
252
- else if (r.status === "failed") entry.failed++;
253
- else if (r.status === "cancelled") entry.cancelled++;
254
- else if (ACTIVE.has(r.status)) entry.active++;
255
- }
256
- return [...map.values()].sort((a, b) => b.total - a.total);
257
- }
258
-
259
- function durationMs(r) {
260
- if (!r) return null;
261
- const end = r.endedAt ?? r.updatedAt ?? null;
262
- const start = r.createdAt ?? null;
263
- if (end == null || start == null) return null;
264
- return end - start;
265
- }
266
-
267
- /**
268
- * Failure histogram clustered by (step, normalized error signature).
269
- * Each row is one bug or one failure mode — two failures at SCAN_CODE
270
- * for "login broken" cluster together even when the underlying messages
271
- * differ in identifier noise; two SCAN_CODE failures for "DOM regression"
272
- * are a separate row. This converts the chart from a "which step" report
273
- * into a "which bug" triage queue.
274
- *
275
- * Each entry carries:
276
- * - p50BlockMs: median wall time from run creation to terminal stop
277
- * - impactMs: count × p50BlockMs (operator-hours-lost ranking)
278
- * - lastFailedAt: timestamp of most recent failure in the cluster
279
- * - perDay: failure count per day across a fixed N-day window (for
280
- * the sparkline / recency read)
281
- * - signature: the normalized error message excerpt
282
- *
283
- * Falls back to plain count-sort when no block-time signal exists yet.
284
- */
285
- function stepImpactHistogram(failedRuns, { windowDays = 14 } = {}, errorsByFlow = null) {
286
- /** clusterKey ("STEP::SIG") → cluster acc */
287
- const acc = new Map();
288
- const now = Date.now();
289
- const dayMs = 86_400_000;
290
- const windowStart = startOfDay(now - (windowDays - 1) * dayMs);
291
-
292
- for (const r of failedRuns) {
293
- const step = r.lastStep ?? "?";
294
- const pipelineId = r.stateJson?.pipelineId ?? "?";
295
- const { signature, raw } = extractError(r, step, errorsByFlow?.get(r.flowId) ?? []);
296
- const sig = signature ?? "(no error message)";
297
- // Top-level cluster = (pipeline, step). Different errors at the
298
- // same step are tracked as SUB-clusters inside `errors` rather
299
- // than separate rows — keeps the overview compact and surfaces
300
- // the breakdown only on tap-to-expand.
301
- const clusterKey = `${pipelineId}::${step}`;
302
- if (!acc.has(clusterKey)) {
303
- acc.set(clusterKey, {
304
- pipelineId, step,
305
- count: 0, blockMsSamples: [], lastFailedAt: 0,
306
- perDay: new Map(),
307
- errors: new Map(), // signature → sub-cluster
308
- });
309
- }
310
- const e = acc.get(clusterKey);
311
- e.count++;
312
- const wallMs = durationMs(r);
313
- if (wallMs != null && wallMs >= 0) e.blockMsSamples.push(wallMs);
314
- const at = r.endedAt ?? r.updatedAt ?? 0;
315
- if (at > e.lastFailedAt) e.lastFailedAt = at;
316
- if (at >= windowStart) {
317
- const day = new Date(startOfDay(at)).toISOString().slice(0, 10);
318
- e.perDay.set(day, (e.perDay.get(day) ?? 0) + 1);
319
- }
320
- // Sub-cluster: tracks each distinct error signature within the
321
- // (pipeline, step) cluster. Exemplar message updates to the most
322
- // recent occurrence; recentRuns is sorted at rollup.
323
- if (!e.errors.has(sig)) {
324
- e.errors.set(sig, {
325
- signature: sig, count: 0, lastFailedAt: 0,
326
- exemplarMessage: null, recentRuns: [],
327
- });
328
- }
329
- const sub = e.errors.get(sig);
330
- sub.count++;
331
- if (at >= sub.lastFailedAt && raw) sub.exemplarMessage = raw;
332
- if (at > sub.lastFailedAt) sub.lastFailedAt = at;
333
- sub.recentRuns.push({ flowId: r.flowId, status: r.status, at });
334
- }
335
-
336
- // Build the fixed-width day axis up front so every cluster's
337
- // sparkline shares the same N-day canvas (gaps render as zeroes).
338
- const dayAxis = [];
339
- for (let i = windowDays - 1; i >= 0; i--) {
340
- dayAxis.push(new Date(startOfDay(now - i * dayMs)).toISOString().slice(0, 10));
341
- }
342
-
343
- const out = [...acc.values()].map((e) => {
344
- const p50BlockMs = percentile(e.blockMsSamples, 0.5);
345
- // Sort sub-clusters by recency desc — the freshest error first
346
- // when the operator expands. Each carries its own top-5 most
347
- // recent flow ids for one-tap deep-link.
348
- const errors = [...e.errors.values()]
349
- .sort((a, b) => (b.lastFailedAt ?? 0) - (a.lastFailedAt ?? 0))
350
- .map((sub) => ({
351
- signature: sub.signature,
352
- count: sub.count,
353
- lastFailedAt: sub.lastFailedAt || null,
354
- exemplarMessage: sub.exemplarMessage,
355
- recentRuns: sub.recentRuns
356
- .sort((a, b) => (b.at ?? 0) - (a.at ?? 0))
357
- .slice(0, 5),
358
- }));
359
- return {
360
- key: e.step,
361
- pipelineId: e.pipelineId,
362
- count: e.count,
363
- p50BlockMs,
364
- impactMs: e.count * (p50BlockMs ?? 0),
365
- lastFailedAt: e.lastFailedAt || null,
366
- perDay: dayAxis.map((day) => ({ day, count: e.perDay.get(day) ?? 0 })),
367
- errors,
368
- };
369
- });
370
- // Primary sort: impact desc. Secondary: count desc.
371
- out.sort((a, b) => (b.impactMs - a.impactMs) || (b.count - a.count));
372
- return out;
373
- }
374
-
375
- /**
376
- * Extract two views of the failure's error message: a normalized
377
- * clustering-friendly signature (UUIDs / numeric runs replaced), and
378
- * the raw untruncated text for the expanded detail block. Prefers
379
- * the error entry matching the failing step; falls back to the last
380
- * error in the array. runErrors is the run's ErrorEntry[] from
381
- * flow_node_executions (sourced by the caller via queryErrorsByFlow/
382
- * queryErrorsByFlowIds), not the old state.errors[].
383
- */
384
- function extractError(run, step, runErrors = []) {
385
- const stepErr = runErrors.find((e) => e?.nodeId === step);
386
- const lastErr = runErrors[runErrors.length - 1];
387
- const raw = (stepErr ?? lastErr)?.message;
388
- if (!raw || typeof raw !== "string") return { signature: null, raw: null };
389
- const signature = raw
390
- .replace(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/gi, "<uuid>")
391
- .replace(/\b\d{2,}\b/g, "<n>")
392
- .replace(/\s+/g, " ")
393
- .trim()
394
- .slice(0, 80);
395
- return { signature, raw };
396
- }
397
-
398
- function percentile(arr, p) {
399
- if (!arr.length) return null;
400
- const s = [...arr].sort((a, b) => a - b);
401
- // Linear interpolation between the two nearest ranks.
402
- const idx = (s.length - 1) * p;
403
- const lo = Math.floor(idx);
404
- const hi = Math.ceil(idx);
405
- if (lo === hi) return s[lo];
406
- return s[lo] + (s[hi] - s[lo]) * (idx - lo);
407
- }
408
-
409
- function mean(arr) {
410
- if (!arr.length) return null;
411
- return arr.reduce((a, b) => a + b, 0) / arr.length;
412
- }
413
-
414
- function startOfDay(ms) {
415
- // UTC-anchored midnight — matches cost.js's dayKey convention so
416
- // every per-day bucket on the dashboard (cost, runs/day, failure
417
- // sparkline) shares the same time boundaries. Local-midnight would
418
- // misalign with the UTC-formatted day keys used downstream (e.g.
419
- // .toISOString().slice(0, 10) reports the UTC date).
420
- const d = new Date(ms);
421
- return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
422
- }
423
-
424
- /**
425
- * Default node-visit funnel for pipelines without a custom `funnel: [...]`
426
- * declaration. One stage per top-level node in pipeline declaration order
427
- * (start/end skipped); per-run "size" is 1 when the node was ever visited,
428
- * per `visitedByFlow` — a Map<flowId, Set<nodeId>> the caller fetches in
429
- * one batched round-trip (GET /manifold/tree-summary, backed by
430
- * flow_node_invocations) rather than this function reading
431
- * state._stepHistory or making one DB/HTTP call per (run, stage) itself.
432
- * `visitedByFlow` may be null/undefined (caller couldn't fetch it, e.g. no
433
- * nodeExecutionsStore reachable) — every stage then reports 0, same as an
434
- * unvisited node, rather than throwing. Counts how many runs reached each
435
- * node — the attrition story without the pipeline having to declare
436
- * anything.
437
- *
438
- * A cascade sub-node (a subflow's own flow.nodes entry) lives inside its
439
- * parent subflow node's own child flow, not as a separate top-level node,
440
- * so it needs no special collapsing here — `visitedByFlow` records the
441
- * outer node id regardless of which cascade sub-node resolved it, same as
442
- * any other node.
443
- */
444
- function autoFunnelFromNodes(pipeline, visitedByFlow = null, lineageByNodeId = null) {
445
- if (!pipeline) return null;
446
- // Node-side pipelinesRef stores `nodes` as an object keyed by id;
447
- // the dashboard's serialized pipeline ships it as an array. Handle
448
- // both shapes so this helper works in either context.
449
- const nodes = Array.isArray(pipeline.nodes)
450
- ? pipeline.nodes
451
- : (pipeline.nodes && typeof pipeline.nodes === "object")
452
- ? Object.entries(pipeline.nodes).map(([id, n]) => ({ ...n, id }))
453
- : null;
454
- if (!nodes) return null;
455
- const out = [];
456
- for (const n of nodes) {
457
- if (!n?.id || n.id === "__start__" || n.id === "__end__") continue;
458
- const nodeId = n.id;
459
- // lineageByNodeId lets an older run's invocation, recorded under
460
- // whatever node id was live when the node was later renamed, still
461
- // count toward THIS (current) stage — see pipeline-registry.js's
462
- // getNodeLineageBatch. Falls back to exact-match-only when the caller
463
- // doesn't supply it (e.g. tests, or a pipeline never touched by a
464
- // declared rename).
465
- const stage = {
466
- id: nodeId,
467
- label: nodeId,
468
- size: (state, flowId) => {
469
- const ids = lineageByNodeId?.get(nodeId) ?? new Set([nodeId]);
470
- const visited = visitedByFlow?.get(flowId);
471
- if (!visited) return 0;
472
- for (const id of ids) if (visited.has(id)) return 1;
473
- return 0;
474
- },
475
- };
476
- // subflow stages get a sub-funnel definition derived from
477
- // the subflow's nodes, in declaration order. Per-run sub-stage
478
- // size = number of branches that visited this sub-step (read off
479
- // each subflow child's own data/step via getSubflowGroup below).
480
- // This is the per-subflow attrition story — "of 7610 branches
481
- // probed, only 6 had updates worth persisting" — that the top-level
482
- // "POLL: visited" can't show.
483
- if (n.kind === "subflow" && n.flow) {
484
- const groupId = n.id;
485
- const subNodes = Array.isArray(n.flow?.nodes)
486
- ? n.flow.nodes
487
- : (n.flow?.nodes && typeof n.flow.nodes === "object")
488
- ? Object.entries(n.flow.nodes).map(([id, sn]) => ({ ...sn, id }))
489
- : [];
490
- const subStageDefs = subNodes
491
- .filter((sn) => sn?.id && sn.id !== "__start__" && sn.id !== "__end__")
492
- .map((sn) => ({
493
- id: `${groupId}.${sn.id}`,
494
- label: sn.id,
495
- size: async (state, flowId, runStore, nodeExecutionsStore) => {
496
- // A sub-step counts as "visited" when its key exists in branch.data
497
- // (executor writes return value there on successful completion) OR
498
- // when the branch's current `step` pointer is parked at it.
499
- const branches = await getSubflowGroup({ flowId, groupId, runStore, nodeExecutionsStore });
500
- if (!branches || typeof branches !== "object") return 0;
501
- let count = 0;
502
- for (const branch of Object.values(branches)) {
503
- if (!branch || typeof branch !== "object") continue;
504
- const data = branch.data;
505
- if (data && typeof data === "object" && sn.id in data) { count++; continue; }
506
- if (branch.step === sn.id) count++;
507
- }
508
- return count;
509
- },
510
- }));
511
- if (subStageDefs.length > 0) stage.subStages = subStageDefs;
512
- }
513
- out.push(stage);
514
- }
515
- return out;
516
- }
517
-
518
- async function computeFunnel(succeededRuns, def, runStore = null, nodeExecutionsStore = null) {
519
- // Per stage: list of measured sizes (one per run), then derive p50, sum, conversion.
520
- const stages = def.map((stage) => ({ ...stage, samples: [] }));
521
- for (const run of succeededRuns) {
522
- const state = run.stateJson ?? {};
523
- for (const stage of stages) {
524
- let size = 0;
525
- if (typeof stage.size === "function") {
526
- // Subflow-derived (or any other custom computation). Function returns
527
- // the per-run count for this stage. flowId/branchService/runStore/
528
- // nodeExecutionsStore are extra args auto-funnel's subflow sub-stages
529
- // use to read subflow-group data (dual-mode, see state/subflow-state.js);
530
- // pipeline-declared custom size(state) functions just ignore them.
531
- try { size = Number(await stage.size(state, run.flowId, runStore, nodeExecutionsStore)) || 0; } catch { size = 0; }
532
- } else {
533
- const v = state[stage.key];
534
- if (stage.size === "array") size = Array.isArray(v) ? v.length : 0;
535
- else if (stage.size === "objectKeys") size = (v && typeof v === "object" && !Array.isArray(v)) ? Object.keys(v).length : 0;
536
- else if (stage.size === "objectKeysNoError") {
537
- size = (v && typeof v === "object" && !Array.isArray(v))
538
- ? Object.values(v).filter((d) => d && !d._error).length
539
- : 0;
540
- }
541
- }
542
- stage.samples.push(size);
543
- }
544
- }
545
- // Backward pass: a run/subflow that reached stage N necessarily passed
546
- // every earlier stage, even when it never dispatched that node's own
547
- // invocation (e.g. ENSURE_LOGIN_* runs that shortcut when the session
548
- // is already valid — the "ensure login" stage PASSED, just with no
549
- // work to do). For visit-only samples (0/1) the bump is 0→1; for
550
- // subflow-count samples the prior stage takes the max of its own
551
- // count vs. the next stage's count, since each downstream subflow
552
- // implies an upstream visit.
553
- for (let i = stages.length - 2; i >= 0; i--) {
554
- for (let j = 0; j < stages[i].samples.length; j++) {
555
- const next = stages[i + 1].samples[j];
556
- if (next > stages[i].samples[j]) stages[i].samples[j] = next;
557
- }
558
- }
559
- const out = await Promise.all(stages.map(async (s) => ({
560
- id: s.id,
561
- label: s.label,
562
- key: s.key,
563
- n: s.samples.length,
564
- total: s.samples.reduce((a, b) => a + b, 0),
565
- p50: percentile(s.samples, 0.5),
566
- // Sub-funnel for subflow parents: recurse with the same
567
- // run set so each sub-stage gets its own per-subflow attrition
568
- // figures. The same backward-propagation kicks in inside the
569
- // recursive call.
570
- subStages: Array.isArray(s.subStages) && s.subStages.length > 0
571
- ? await computeFunnel(succeededRuns, s.subStages)
572
- : undefined,
573
- // p95: a stage's worst-realistic per-run yield. Pairs with min below
574
- // to catch "stage was healthy on average but bottomed out one day."
575
- p95: percentile(s.samples, 0.95),
576
- // min: surfaces the ran-dry days — a shortlist whose median was 5
577
- // but had two days at 0 has actually broken; the p50 line won't show it.
578
- min: s.samples.length > 0 ? Math.min(...s.samples) : null,
579
- mean: mean(s.samples),
580
- })));
581
- // Stage-to-stage conversion: total at stage i / total at stage i-1.
582
- for (let i = 1; i < out.length; i++) {
583
- const prev = out[i - 1].total;
584
- out[i].conversionFromPrev = prev > 0 ? out[i].total / prev : null;
585
- }
586
- // Overall: last / first.
587
- if (out.length > 1 && out[0].total > 0) {
588
- out[out.length - 1].conversionFromStart = out[out.length - 1].total / out[0].total;
589
- }
590
- return out;
591
- }
592
-
593
- function summariseForChart(r) {
594
- return {
595
- flowId: r.flowId,
596
- status: r.status,
597
- lastStep: r.lastStep,
598
- createdAt: r.createdAt,
599
- endedAt: r.endedAt,
600
- durationMs: durationMs(r),
601
- revision: r.revision,
602
- };
603
- }
604
-
605
- /**
606
- * Per-node aggregation across a pipeline's runs. Used by the graph-view
607
- * click panel to show:
608
- * - visits: how many runs visited this node at any point, per
609
- * `visitedByFlow` — a Map<flowId, Set<nodeId>> the caller fetches in
610
- * one batched round-trip (GET /manifold/tree-summary), same as
611
- * computePipelineStats' auto-funnel. May be null/undefined (caller
612
- * couldn't fetch it) — visits then falls back to lastStep === nodeId
613
- * only, same degrade-safe shape as everywhere else this map is
614
- * threaded through.
615
- * - failedHere / blockedHere: how many runs were terminal-failed /
616
- * currently-blocked AT this node (lastStep === nodeId at terminal /
617
- * non-terminal state respectively)
618
- * - terminatedHere: failed + cancelled with lastStep === nodeId
619
- * - recentRuns: the most-recent 10 runs that touched this node, sorted
620
- * by updatedAt desc, with their final status + step
621
- *
622
- * A node id may be either top-level ("SCORE") or qualified
623
- * ("PROPOSE.DRAFT") for subflow children. Subflow-internal step
624
- * histories live in the branch store (not top-level state since the
625
- * branch-store migration), and we don't currently aggregate those —
626
- * top-level visits is the source of truth for v1.
627
- */
628
- export function computeNodeStats(runs, pipelineId, nodeId, errorsByFlow = null, visitedByFlow = null, lineageByNodeId = null) {
629
- runs = withResolvedSteps(runs, errorsByFlow);
630
- const mine = runs.filter((r) => (r.stateJson?.pipelineId ?? "") === pipelineId);
631
- // Same lineage fallback as autoFunnelFromNodes — an older run recorded
632
- // under a since-renamed node id still counts as having visited nodeId.
633
- const lineageIds = lineageByNodeId?.get(nodeId) ?? new Set([nodeId]);
634
- let visits = 0;
635
- let failedHere = 0;
636
- let blockedHere = 0;
637
- let terminatedHere = 0;
638
- const touched = [];
639
- for (const r of mine) {
640
- const flowVisited = visitedByFlow?.get(r.flowId);
641
- const visited = (flowVisited && [...lineageIds].some((id) => flowVisited.has(id))) || r.lastStep === nodeId;
642
- if (!visited) continue;
643
- visits++;
644
- if (r.lastStep === nodeId) {
645
- if (r.status === "failed") { failedHere++; terminatedHere++; }
646
- else if (r.status === "cancelled") terminatedHere++;
647
- else if (r.status === "blocked") blockedHere++;
648
- }
649
- touched.push({
650
- flowId: r.flowId,
651
- status: r.status,
652
- lastStep: r.lastStep,
653
- updatedAt: r.updatedAt,
654
- revision: r.revision,
655
- });
656
- }
657
- touched.sort((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0));
658
- return {
659
- pipelineId,
660
- nodeId,
661
- visits,
662
- failedHere,
663
- blockedHere,
664
- terminatedHere,
665
- recentRuns: touched.slice(0, 10),
666
- totalRuns: mine.length,
667
- };
668
- }