@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.
- package/.migrations/ops/0000_futuristic_karma.sql +111 -0
- package/.migrations/ops/0001_low_maximus.sql +7 -0
- package/.migrations/ops/meta/0000_snapshot.json +763 -0
- package/.migrations/ops/meta/0001_snapshot.json +173 -0
- package/.migrations/ops/meta/_journal.json +20 -0
- package/.migrations/runtime/0000_real_boomer.sql +180 -0
- package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
- package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
- package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
- package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
- package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
- package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
- package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
- package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
- package/.migrations/runtime/0009_lying_shocker.sql +96 -0
- package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
- package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
- package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
- package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
- package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
- package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
- package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
- package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
- package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
- package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
- package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
- package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
- package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
- package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
- package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
- package/.migrations/runtime/meta/_journal.json +97 -0
- package/README.md +1583 -2
- package/RELEASING.md +89 -0
- package/agent/fleet.js +100 -0
- package/bin/cli.js +854 -0
- package/bin/manifold.js +4 -0
- package/boot/flow-revival.js +536 -0
- package/budget/index.js +495 -0
- package/budget/lib.js +59 -0
- package/dashboard/cost.js +528 -0
- package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
- package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
- package/dashboard/dist/index.html +13 -0
- package/dashboard/event-log.js +267 -0
- package/dashboard/index.html +12 -0
- package/dashboard/package-lock.json +2877 -0
- package/dashboard/package.json +37 -0
- package/dashboard/pipeline-graph.js +131 -0
- package/dashboard/postcss.config.js +6 -0
- package/dashboard/search.js +183 -0
- package/dashboard/server.js +1015 -0
- package/dashboard/src/App.tsx +42 -0
- package/dashboard/src/api/client.ts +118 -0
- package/dashboard/src/api/types.ts +510 -0
- package/dashboard/src/api/ws.tsx +112 -0
- package/dashboard/src/components/CancelButton.tsx +63 -0
- package/dashboard/src/components/ClickToCopy.tsx +68 -0
- package/dashboard/src/components/CopyButton.tsx +61 -0
- package/dashboard/src/components/DailyCostTrend.tsx +85 -0
- package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
- package/dashboard/src/components/Funnel.tsx +149 -0
- package/dashboard/src/components/GateForm.tsx +158 -0
- package/dashboard/src/components/GraphView.tsx +302 -0
- package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
- package/dashboard/src/components/Histogram.tsx +292 -0
- package/dashboard/src/components/Layout.tsx +28 -0
- package/dashboard/src/components/NeedsAttention.tsx +98 -0
- package/dashboard/src/components/NodeContextCard.tsx +132 -0
- package/dashboard/src/components/PathTrail.tsx +35 -0
- package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
- package/dashboard/src/components/RerunButton.tsx +46 -0
- package/dashboard/src/components/RunComparison.tsx +53 -0
- package/dashboard/src/components/Sparkline.tsx +94 -0
- package/dashboard/src/components/StatStrip.tsx +147 -0
- package/dashboard/src/components/StateTree.tsx +84 -0
- package/dashboard/src/components/StatusBadge.tsx +18 -0
- package/dashboard/src/components/SubflowPathPills.tsx +38 -0
- package/dashboard/src/components/SubflowSummary.tsx +24 -0
- package/dashboard/src/components/SubflowsCard.tsx +327 -0
- package/dashboard/src/components/TodayStrip.tsx +86 -0
- package/dashboard/src/components/TranscriptPane.tsx +99 -0
- package/dashboard/src/components/TranscriptRow.tsx +69 -0
- package/dashboard/src/components/ui/badge.tsx +38 -0
- package/dashboard/src/components/ui/button.tsx +28 -0
- package/dashboard/src/components/ui/card.tsx +42 -0
- package/dashboard/src/components/ui/skeleton.tsx +9 -0
- package/dashboard/src/components/ui/table.tsx +62 -0
- package/dashboard/src/index.css +26 -0
- package/dashboard/src/lib/anomaly.ts +42 -0
- package/dashboard/src/lib/format.ts +30 -0
- package/dashboard/src/lib/step-history.ts +13 -0
- package/dashboard/src/lib/subflow.ts +154 -0
- package/dashboard/src/lib/use-click-outside.ts +28 -0
- package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
- package/dashboard/src/lib/utils.ts +6 -0
- package/dashboard/src/main.tsx +10 -0
- package/dashboard/src/types/shims.d.ts +4 -0
- package/dashboard/src/views/Home.tsx +478 -0
- package/dashboard/src/views/PipelineDetail.tsx +1185 -0
- package/dashboard/src/views/PipelinesList.tsx +111 -0
- package/dashboard/src/views/RunDetail.tsx +248 -0
- package/dashboard/src/views/Search.tsx +196 -0
- package/dashboard/stats.js +668 -0
- package/dashboard/tailwind.config.ts +43 -0
- package/dashboard/tree-stats.js +12 -0
- package/dashboard/tsconfig.json +25 -0
- package/dashboard/views.js +231 -0
- package/dashboard/vite.config.ts +34 -0
- package/drizzle.config.js +12 -0
- package/drizzle.config.ops.js +12 -0
- package/elevation.js +116 -0
- package/engine/admission-promotion-resume.js +127 -0
- package/engine/agent-dispatch.js +64 -0
- package/engine/child-resume-trigger.js +315 -0
- package/engine/executor-budget.js +87 -0
- package/engine/executor-cancel.js +83 -0
- package/engine/executor-errors.js +122 -0
- package/engine/executor-helpers.js +254 -0
- package/engine/executor-nodes.js +292 -0
- package/engine/executor-persist.js +212 -0
- package/engine/executor-subflow-children.js +275 -0
- package/engine/executor.js +1013 -0
- package/engine/global-concurrency.js +33 -0
- package/engine/loader.js +528 -0
- package/engine/needs-resolver.js +229 -0
- package/engine/pipelines-watcher.js +133 -0
- package/engine/result-replay.js +34 -0
- package/engine/revival-messages.js +11 -0
- package/engine/spawn-child.js +55 -0
- package/engine/wait-for-flow-settlement.js +109 -0
- package/events/catalog.js +26 -0
- package/events/emit.js +44 -0
- package/events/kinds.js +118 -0
- package/events/serve-ws.js +74 -0
- package/events/stream.js +35 -0
- package/events/ws-dialog.js +257 -0
- package/events/ws-frame.js +194 -0
- package/events/ws-run-events.js +70 -0
- package/events.js +32 -0
- package/gates/gate-resolver.js +576 -0
- package/gates/paused-flows.js +55 -0
- package/graphql/columns.js +67 -0
- package/graphql/schema.js +333 -0
- package/index.js +49 -0
- package/mcp/http.js +159 -0
- package/mcp/identity.js +94 -0
- package/mcp/stdio.js +80 -0
- package/mcp/tools.js +245 -0
- package/mcp/wire-ops.js +178 -0
- package/package.json +49 -4
- package/routes/bearer-auth.js +57 -0
- package/routes/discuss-audio.js +87 -0
- package/routes/discuss-component.js +39 -0
- package/routes/discuss-draft.js +159 -0
- package/routes/discuss-helpers.js +20 -0
- package/routes/discuss.js +848 -0
- package/routes/failures.js +104 -0
- package/routes/graphql.js +61 -0
- package/routes/health.js +54 -0
- package/routes/operator-discuss.js +193 -0
- package/routes/operator-resume.js +32 -0
- package/routes/operator-retry.js +301 -0
- package/routes/operator.js +539 -0
- package/routes/pipelines.js +118 -0
- package/routes/runs.js +1129 -0
- package/scripts/ci/import-check.mjs +50 -0
- package/scripts/link-core-self.mjs +31 -0
- package/scripts/with-guard.sh +24 -0
- package/server/app.js +503 -0
- package/server/registry.js +53 -0
- package/server/serve.js +163 -0
- package/state/admission-queue-store.js +325 -0
- package/state/attribution-registries.js +102 -0
- package/state/batch-writer.js +162 -0
- package/state/db-guard.js +63 -0
- package/state/db-health.js +27 -0
- package/state/db.js +122 -0
- package/state/discuss-store.js +299 -0
- package/state/failure-store.js +427 -0
- package/state/migrate.js +104 -0
- package/state/node-cost.js +67 -0
- package/state/node-executions.js +775 -0
- package/state/node-health-store.js +124 -0
- package/state/ops-merge.js +93 -0
- package/state/paths.js +146 -0
- package/state/pipeline-dirs.js +99 -0
- package/state/pipeline-registry.js +449 -0
- package/state/run-store.js +697 -0
- package/state/schema.js +438 -0
- package/state/schema.ops.js +44 -0
- package/state/session-index.js +177 -0
- package/state/session-keys.js +39 -0
- package/state/state-store.js +311 -0
- package/state/subflow-state.js +126 -0
- package/state/tool-handoff.js +169 -0
- package/telemetry/budget-alert.js +353 -0
- package/telemetry/cli.js +331 -0
- package/telemetry/grammar.js +119 -0
- package/telemetry/http-budget.js +132 -0
- package/telemetry/http-cost.js +670 -0
- package/telemetry/http-helpers.js +134 -0
- package/telemetry/http-tree.js +349 -0
- package/telemetry/http.js +44 -0
- package/telemetry/index.js +307 -0
- package/telemetry/ingest.js +134 -0
- package/telemetry/labels.js +115 -0
- package/telemetry/pricer.js +77 -0
- package/telemetry/resolver.js +314 -0
- package/telemetry/store.js +147 -0
- package/utils/run-patch.js +43 -0
|
@@ -0,0 +1,668 @@
|
|
|
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
|
+
}
|