@igorjpimenta/manifold 0.0.0-bootstrap.1
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 +1584 -0
- 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 +51 -0
- 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
package/routes/runs.js
ADDED
|
@@ -0,0 +1,1129 @@
|
|
|
1
|
+
import { getNodeExecutionsStore } from "../state/node-executions.js";
|
|
2
|
+
import { getAllSubflowGroups } from "../state/subflow-state.js";
|
|
3
|
+
import { assembleState } from "../engine/executor-helpers.js";
|
|
4
|
+
import { authorizeBearerRequest, sendUnauthorized } from "./bearer-auth.js";
|
|
5
|
+
|
|
6
|
+
export function registerRunRoutes({
|
|
7
|
+
api,
|
|
8
|
+
readStore,
|
|
9
|
+
costForAllPipelines,
|
|
10
|
+
runStore,
|
|
11
|
+
pipelinesRef,
|
|
12
|
+
pipelineRegistryStore,
|
|
13
|
+
invokeElevated,
|
|
14
|
+
discussStore,
|
|
15
|
+
failureStore,
|
|
16
|
+
sessionIndex,
|
|
17
|
+
// discuss handlers — destructured from registerDiscussHandlers() return value
|
|
18
|
+
handleDiscuss,
|
|
19
|
+
handleDiscussHistory,
|
|
20
|
+
handleDiscussApplyDraft,
|
|
21
|
+
handleDiscussAudio,
|
|
22
|
+
handleDiscussAudioGet,
|
|
23
|
+
handleDiscussAudioRetry,
|
|
24
|
+
}) {
|
|
25
|
+
|
|
26
|
+
// ── GET /manifold/cost ─────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
api.registerHttpRoute({
|
|
29
|
+
path: "/manifold/cost",
|
|
30
|
+
auth: "plugin",
|
|
31
|
+
match: "exact",
|
|
32
|
+
handler: async (req, res) => {
|
|
33
|
+
if (req.method !== "GET") {
|
|
34
|
+
res.statusCode = 405;
|
|
35
|
+
res.setHeader("Allow", "GET");
|
|
36
|
+
res.end("method not allowed");
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
const authz = authorizeBearerRequest(req, { logger: api.logger });
|
|
40
|
+
if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
|
|
41
|
+
const url = new URL(req.url, `http://${req.headers.host || "localhost"}`);
|
|
42
|
+
const sinceMsRaw = url.searchParams.get("sinceMs");
|
|
43
|
+
const sinceMs = sinceMsRaw ? Number(sinceMsRaw) : null;
|
|
44
|
+
if (sinceMsRaw && (!Number.isFinite(sinceMs) || sinceMs < 0)) {
|
|
45
|
+
res.statusCode = 400;
|
|
46
|
+
res.setHeader("Content-Type", "application/json");
|
|
47
|
+
res.end(JSON.stringify({ ok: false, error: "invalid sinceMs" }));
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
const runs = await readStore.listCostSnapshot();
|
|
52
|
+
const cost = await costForAllPipelines(runs, { sinceMs });
|
|
53
|
+
const total = cost.pipelines.reduce((s, p) => s + (p.total || 0), 0);
|
|
54
|
+
res.statusCode = 200;
|
|
55
|
+
res.setHeader("Content-Type", "application/json");
|
|
56
|
+
res.end(JSON.stringify({
|
|
57
|
+
sinceMs: cost.sinceMs,
|
|
58
|
+
untilMs: Date.now(),
|
|
59
|
+
total,
|
|
60
|
+
pipelines: cost.pipelines,
|
|
61
|
+
}));
|
|
62
|
+
} catch (err) {
|
|
63
|
+
api.logger.warn(`manifold cost handler error: ${err?.message ?? err}`);
|
|
64
|
+
res.statusCode = 500;
|
|
65
|
+
res.setHeader("Content-Type", "application/json");
|
|
66
|
+
res.end(JSON.stringify({ ok: false, error: err?.message ?? String(err) }));
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// ── GET /manifold/runs ─────────────────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
api.registerHttpRoute({
|
|
75
|
+
path: "/manifold/runs",
|
|
76
|
+
auth: "plugin",
|
|
77
|
+
match: "exact",
|
|
78
|
+
handler: async (req, res) => {
|
|
79
|
+
if (req.method !== "GET") {
|
|
80
|
+
res.statusCode = 405;
|
|
81
|
+
res.setHeader("Allow", "GET");
|
|
82
|
+
res.end("method not allowed");
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
const authz = authorizeBearerRequest(req, { logger: api.logger });
|
|
86
|
+
if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
|
|
87
|
+
const url = new URL(req.url, `http://${req.headers.host || "localhost"}`);
|
|
88
|
+
const limitRaw = url.searchParams.get("limit");
|
|
89
|
+
const limit = Math.max(1, Math.min(500, Number(limitRaw) || 50));
|
|
90
|
+
const pipelineIdFilter = url.searchParams.get("pipelineId");
|
|
91
|
+
const beforeMs = url.searchParams.has("before_ms")
|
|
92
|
+
? parseInt(url.searchParams.get("before_ms"), 10) : null;
|
|
93
|
+
const beforeId = url.searchParams.get("before_id") ?? null;
|
|
94
|
+
try {
|
|
95
|
+
// "waiting" is never actually written anywhere (confirmed repo-wide)
|
|
96
|
+
// — gate pauses are always persisted as "blocked" — so no special-
|
|
97
|
+
// case expansion is needed for that filter value.
|
|
98
|
+
const statusParam = url.searchParams.get("status");
|
|
99
|
+
// Two targeted queries: GROUP BY for tab counts (unfiltered), and a
|
|
100
|
+
// paginated fetch for the current page (sorted + cursor in SQL).
|
|
101
|
+
const statusCounts = await readStore.listStatusCounts({ pipelineId: pipelineIdFilter });
|
|
102
|
+
const totalKnown = statusParam
|
|
103
|
+
? (statusCounts[statusParam] ?? 0)
|
|
104
|
+
: Object.values(statusCounts).reduce((s, n) => s + n, 0);
|
|
105
|
+
// Fetch limit+1 so hasMore detection needs no extra COUNT query.
|
|
106
|
+
const page = await readStore.listRunsPage({
|
|
107
|
+
pipelineId: pipelineIdFilter,
|
|
108
|
+
status: statusParam,
|
|
109
|
+
limit: limit + 1,
|
|
110
|
+
beforeMs,
|
|
111
|
+
beforeId,
|
|
112
|
+
});
|
|
113
|
+
const hasMore = page.length > limit;
|
|
114
|
+
if (hasMore) page.pop();
|
|
115
|
+
const last = page[page.length - 1];
|
|
116
|
+
const discussSummaries = await discussStore.getBulkSummaries(page.map((r) => r.flowId));
|
|
117
|
+
// Dual-mode (Phase 2): a flow's subflows are either old-model
|
|
118
|
+
// (flow_branches) or new-model (real flow_runs children) never
|
|
119
|
+
// both — see state/subflow-state.js's own header comment. Prefer the
|
|
120
|
+
// childFlows summary; a flow with no real children (the common
|
|
121
|
+
// case: hasn't reached a subflow node yet, or predates the
|
|
122
|
+
// migration) naturally falls through to the old-model map, then
|
|
123
|
+
// to EMPTY_SUBFLOWS. Without this, every childFlows-migrated run
|
|
124
|
+
// in the list (i.e. every run post-migration) would report
|
|
125
|
+
// subflowTotal:0 even though its own /subflows endpoint has real
|
|
126
|
+
// data — the exact mismatch that made RunDetailView's Subflows
|
|
127
|
+
// section populate while PipelineDetailView's inline subflow peek
|
|
128
|
+
// silently never offered itself.
|
|
129
|
+
const subflowSummaryMap = new Map();
|
|
130
|
+
const childSummaryMap = await getNodeExecutionsStore().summarizeChildrenByFlowIds(page.map((r) => r.flowId));
|
|
131
|
+
const EMPTY_SUBFLOWS = { total: 0, pending: 0, failed: 0, skipped: 0, succeeded: 0 };
|
|
132
|
+
// listRunsPage already delivers final field names and extracted
|
|
133
|
+
// display fields — route only merges in the secondary-service data.
|
|
134
|
+
const runs = page.map((r) => {
|
|
135
|
+
const discuss = discussSummaries[r.flowId] ?? null;
|
|
136
|
+
const subflows = childSummaryMap.get(r.flowId) ?? subflowSummaryMap.get(r.flowId) ?? EMPTY_SUBFLOWS;
|
|
137
|
+
return {
|
|
138
|
+
...r,
|
|
139
|
+
subflowTotal: subflows.total,
|
|
140
|
+
subflowPending: subflows.pending,
|
|
141
|
+
subflowFailed: subflows.failed,
|
|
142
|
+
subflowSkipped: subflows.skipped,
|
|
143
|
+
subflowSucceeded: subflows.succeeded,
|
|
144
|
+
pendingDraftItemIds: discuss?.pendingItemIds ?? [],
|
|
145
|
+
};
|
|
146
|
+
});
|
|
147
|
+
res.statusCode = 200;
|
|
148
|
+
res.setHeader("Content-Type", "application/json");
|
|
149
|
+
res.end(JSON.stringify({
|
|
150
|
+
runs,
|
|
151
|
+
hasMore,
|
|
152
|
+
totalKnown,
|
|
153
|
+
statusCounts,
|
|
154
|
+
nextBeforeMs: last?.createdAtMs ?? null,
|
|
155
|
+
nextBeforeId: last?.flowId ?? null,
|
|
156
|
+
}));
|
|
157
|
+
} catch (err) {
|
|
158
|
+
api.logger.warn(`manifold runs handler error: ${err?.message ?? err}`);
|
|
159
|
+
res.statusCode = 500;
|
|
160
|
+
res.setHeader("Content-Type", "application/json");
|
|
161
|
+
res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// ── GET /manifold/errors?flowIds=id1,id2,... ──────────────────────────
|
|
168
|
+
// Returns error entries for the given flow IDs.
|
|
169
|
+
// Used by the dashboard to enrich its run lists and stats without direct
|
|
170
|
+
// DB access. flowIds is a comma-separated list; max 500 entries.
|
|
171
|
+
|
|
172
|
+
api.registerHttpRoute({
|
|
173
|
+
path: "/manifold/errors",
|
|
174
|
+
auth: "plugin",
|
|
175
|
+
match: "exact",
|
|
176
|
+
handler: async (req, res) => {
|
|
177
|
+
if (req.method !== "GET") {
|
|
178
|
+
res.statusCode = 405;
|
|
179
|
+
res.setHeader("Allow", "GET");
|
|
180
|
+
res.end("method not allowed");
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
const authz = authorizeBearerRequest(req, { logger: api.logger });
|
|
184
|
+
if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
|
|
185
|
+
const url = new URL(req.url, `http://${req.headers.host || "localhost"}`);
|
|
186
|
+
const raw = url.searchParams.get("flowIds") ?? "";
|
|
187
|
+
const flowIds = raw.split(",").map((s) => s.trim()).filter(Boolean).slice(0, 500);
|
|
188
|
+
if (!flowIds.length) {
|
|
189
|
+
res.statusCode = 400;
|
|
190
|
+
res.setHeader("Content-Type", "application/json");
|
|
191
|
+
res.end(JSON.stringify({ ok: false, error: "flowIds required" }));
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
const errorsMap = await failureStore.queryErrorsByFlowIds(flowIds);
|
|
196
|
+
const errors = {};
|
|
197
|
+
for (const [flowId, entries] of errorsMap) errors[flowId] = entries;
|
|
198
|
+
res.statusCode = 200;
|
|
199
|
+
res.setHeader("Content-Type", "application/json");
|
|
200
|
+
res.end(JSON.stringify({ errors }));
|
|
201
|
+
} catch (err) {
|
|
202
|
+
api.logger.warn(`manifold errors handler error: ${err?.message ?? err}`);
|
|
203
|
+
res.statusCode = 500;
|
|
204
|
+
res.setHeader("Content-Type", "application/json");
|
|
205
|
+
res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
|
|
206
|
+
}
|
|
207
|
+
return true;
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// ── GET /manifold/queue ────────────────────────────────────────────────
|
|
212
|
+
|
|
213
|
+
api.registerHttpRoute({
|
|
214
|
+
path: "/manifold/queue",
|
|
215
|
+
auth: "plugin",
|
|
216
|
+
match: "exact",
|
|
217
|
+
handler: async (req, res) => {
|
|
218
|
+
if (req.method !== "GET") {
|
|
219
|
+
res.statusCode = 405;
|
|
220
|
+
res.setHeader("Allow", "GET");
|
|
221
|
+
res.end("method not allowed");
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
const authz = authorizeBearerRequest(req, { logger: api.logger });
|
|
225
|
+
if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
|
|
226
|
+
try {
|
|
227
|
+
// Lean, purpose-built mapper — deliberately not the /runs list's
|
|
228
|
+
// .map() above, which pulls in bulk discuss/subflow-summary fetches
|
|
229
|
+
// that mean nothing for a queued item's glance view and would slow
|
|
230
|
+
// this endpoint down for no benefit.
|
|
231
|
+
const items = (await readStore.listQueued()).map((r) => ({
|
|
232
|
+
flowId: r.flowId,
|
|
233
|
+
pipelineId: r.pipelineId,
|
|
234
|
+
parentFlowId: r.parentFlowId,
|
|
235
|
+
nodeId: r.nodeId,
|
|
236
|
+
subflowKey: r.subflowKey,
|
|
237
|
+
status: "queued",
|
|
238
|
+
currentStep: r.currentStep,
|
|
239
|
+
lastStep: r.lastStep,
|
|
240
|
+
statusReason: r.statusReason,
|
|
241
|
+
createdAtMs: r.createdAt,
|
|
242
|
+
updatedAtMs: r.updatedAt,
|
|
243
|
+
}));
|
|
244
|
+
res.statusCode = 200;
|
|
245
|
+
res.setHeader("Content-Type", "application/json");
|
|
246
|
+
res.end(JSON.stringify({ items }));
|
|
247
|
+
} catch (err) {
|
|
248
|
+
api.logger.warn(`manifold queue handler error: ${err?.message ?? err}`);
|
|
249
|
+
res.statusCode = 500;
|
|
250
|
+
res.setHeader("Content-Type", "application/json");
|
|
251
|
+
res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
|
|
252
|
+
}
|
|
253
|
+
return true;
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// ── GET /manifold/tree-summary?flowIds=id1,id2,... ──────────────────────
|
|
258
|
+
// Batched "which top-level nodes did each of these flows visit" —
|
|
259
|
+
// backing the dashboard auto-funnel's per-node visit check without one
|
|
260
|
+
// round-trip per run. Same shape/convention as /manifold/errors above
|
|
261
|
+
// (comma-separated flowIds, capped at 500, sibling top-level resource
|
|
262
|
+
// rather than nested under /manifold/runs/ — that path is a
|
|
263
|
+
// prefix-matched route below and this avoids any ambiguity with it).
|
|
264
|
+
|
|
265
|
+
api.registerHttpRoute({
|
|
266
|
+
path: "/manifold/tree-summary",
|
|
267
|
+
auth: "plugin",
|
|
268
|
+
match: "exact",
|
|
269
|
+
handler: async (req, res) => {
|
|
270
|
+
if (req.method !== "GET") {
|
|
271
|
+
res.statusCode = 405;
|
|
272
|
+
res.setHeader("Allow", "GET");
|
|
273
|
+
res.end("method not allowed");
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
const authz = authorizeBearerRequest(req, { logger: api.logger });
|
|
277
|
+
if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
|
|
278
|
+
const url = new URL(req.url, `http://${req.headers.host || "localhost"}`);
|
|
279
|
+
const raw = url.searchParams.get("flowIds") ?? "";
|
|
280
|
+
const flowIds = raw.split(",").map((s) => s.trim()).filter(Boolean).slice(0, 500);
|
|
281
|
+
if (!flowIds.length) {
|
|
282
|
+
res.statusCode = 400;
|
|
283
|
+
res.setHeader("Content-Type", "application/json");
|
|
284
|
+
res.end(JSON.stringify({ ok: false, error: "flowIds required" }));
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
const visitedMap = await getNodeExecutionsStore().listVisitedNodeIdsByFlow(flowIds);
|
|
289
|
+
const summaries = {};
|
|
290
|
+
for (const [flowId, nodeIds] of visitedMap) summaries[flowId] = { nodeIds: [...nodeIds] };
|
|
291
|
+
res.statusCode = 200;
|
|
292
|
+
res.setHeader("Content-Type", "application/json");
|
|
293
|
+
res.end(JSON.stringify({ summaries }));
|
|
294
|
+
} catch (err) {
|
|
295
|
+
api.logger.warn(`manifold tree-summary handler error: ${err?.message ?? err}`);
|
|
296
|
+
res.statusCode = 500;
|
|
297
|
+
res.setHeader("Content-Type", "application/json");
|
|
298
|
+
res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
|
|
299
|
+
}
|
|
300
|
+
return true;
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// ── GET+POST /manifold/runs/<flowId>/... ────────────────────────────────
|
|
305
|
+
|
|
306
|
+
api.registerHttpRoute({
|
|
307
|
+
path: "/manifold/runs/",
|
|
308
|
+
auth: "plugin",
|
|
309
|
+
match: "prefix",
|
|
310
|
+
handler: async (req, res) => {
|
|
311
|
+
if (req.method !== "GET" && req.method !== "POST") {
|
|
312
|
+
res.statusCode = 405;
|
|
313
|
+
res.setHeader("Allow", "GET, POST");
|
|
314
|
+
res.end("method not allowed");
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
const authz = authorizeBearerRequest(req, { logger: api.logger });
|
|
318
|
+
if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
|
|
319
|
+
const url = new URL(req.url, `http://${req.headers.host || "localhost"}`);
|
|
320
|
+
const tail = url.pathname.slice("/manifold/runs/".length);
|
|
321
|
+
const segments = tail.split("/").filter(Boolean);
|
|
322
|
+
const flowId = segments[0];
|
|
323
|
+
if (!flowId) {
|
|
324
|
+
res.statusCode = 400;
|
|
325
|
+
res.setHeader("Content-Type", "application/json");
|
|
326
|
+
res.end(JSON.stringify({ ok: false, error: "flowId required" }));
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
const flow = await readStore.get(flowId);
|
|
330
|
+
if (!flow) {
|
|
331
|
+
res.statusCode = 404;
|
|
332
|
+
res.setHeader("Content-Type", "application/json");
|
|
333
|
+
res.end(JSON.stringify({ ok: false, error: "flow not found", flowId }));
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
try {
|
|
337
|
+
if (segments.length === 1 && req.method === "GET") {
|
|
338
|
+
return await sendFullRun(res, flow);
|
|
339
|
+
}
|
|
340
|
+
if (segments[1] === "artifacts" && segments.length === 2 && req.method === "GET") {
|
|
341
|
+
return await sendArtifactsList(res, flow);
|
|
342
|
+
}
|
|
343
|
+
if (segments[1] === "artifacts" && segments.length >= 3 && req.method === "GET") {
|
|
344
|
+
const name = decodeURIComponent(segments.slice(2).join("/"));
|
|
345
|
+
return await sendArtifactFile(res, flow, name);
|
|
346
|
+
}
|
|
347
|
+
if (segments[1] === "subflows" && segments.length === 2 && req.method === "GET") {
|
|
348
|
+
return await sendSubflowsList(res, flow);
|
|
349
|
+
}
|
|
350
|
+
if (segments[1] === "tree" && segments.length === 2 && req.method === "GET") {
|
|
351
|
+
return await sendTreeRoot(res, flow);
|
|
352
|
+
}
|
|
353
|
+
if (segments[1] === "tree" && segments[2] === "expand" && segments.length === 3 && req.method === "GET") {
|
|
354
|
+
return await sendTreeExpand(res, flow, url);
|
|
355
|
+
}
|
|
356
|
+
if (segments[1] === "tree" && segments[2] === "invocation" && segments.length === 4 && req.method === "GET") {
|
|
357
|
+
return await sendInvocationDetail(res, flow, decodeURIComponent(segments[3]));
|
|
358
|
+
}
|
|
359
|
+
if (segments[1] === "transcripts" && segments.length === 2 && req.method === "GET") {
|
|
360
|
+
return await sendTranscriptsList(res, flow, url);
|
|
361
|
+
}
|
|
362
|
+
if (segments[1] === "compare" && segments.length === 2 && req.method === "GET") {
|
|
363
|
+
return await sendCompareToPeers(res, flow);
|
|
364
|
+
}
|
|
365
|
+
if (segments[1] === "rerun" && segments.length === 2 && req.method === "POST") {
|
|
366
|
+
return await handleRerun(req, res, flow);
|
|
367
|
+
}
|
|
368
|
+
if (segments[1] === "discuss" && segments.length === 2 && req.method === "POST") {
|
|
369
|
+
return await handleDiscuss(req, res, flow);
|
|
370
|
+
}
|
|
371
|
+
if (segments[1] === "discuss" && segments.length === 2 && req.method === "GET") {
|
|
372
|
+
return await handleDiscussHistory(req, res, flow);
|
|
373
|
+
}
|
|
374
|
+
if (segments[1] === "discuss" && segments[2] === "apply-draft"
|
|
375
|
+
&& segments.length === 3 && req.method === "POST") {
|
|
376
|
+
return await handleDiscussApplyDraft(req, res, flow);
|
|
377
|
+
}
|
|
378
|
+
if (segments[1] === "discuss" && segments[2] === "audio"
|
|
379
|
+
&& segments.length === 3 && req.method === "POST") {
|
|
380
|
+
return await handleDiscussAudio(req, res, flow);
|
|
381
|
+
}
|
|
382
|
+
if (segments[1] === "discuss" && segments[2] === "audio"
|
|
383
|
+
&& segments.length === 4 && req.method === "GET") {
|
|
384
|
+
return await handleDiscussAudioGet(req, res, flow, decodeURIComponent(segments[3]));
|
|
385
|
+
}
|
|
386
|
+
if (segments[1] === "discuss" && segments[2] === "audio" && segments[3] === "retry"
|
|
387
|
+
&& segments.length === 4 && req.method === "POST") {
|
|
388
|
+
return await handleDiscussAudioRetry(req, res, flow);
|
|
389
|
+
}
|
|
390
|
+
res.statusCode = 404;
|
|
391
|
+
res.setHeader("Content-Type", "application/json");
|
|
392
|
+
res.end(JSON.stringify({ ok: false, error: "unknown subpath or method" }));
|
|
393
|
+
return true;
|
|
394
|
+
} catch (err) {
|
|
395
|
+
api.logger.warn(`manifold run-detail handler error: ${err?.message ?? err}`);
|
|
396
|
+
res.statusCode = 500;
|
|
397
|
+
res.setHeader("Content-Type", "application/json");
|
|
398
|
+
res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
// ── sendFullRun ───────────────────────────────────────────────────────────
|
|
405
|
+
|
|
406
|
+
async function sendFullRun(res, flow) {
|
|
407
|
+
const nodeExecutionsStore = getNodeExecutionsStore();
|
|
408
|
+
// B2.5 — enriched via assembleState() (each node's own resultJson
|
|
409
|
+
// replayed on top of the still-live stateJson) instead of trusting the
|
|
410
|
+
// raw stateJson blob directly, matching how the engine's own dispatch
|
|
411
|
+
// loop and routes/discuss.js already read state.
|
|
412
|
+
const enrichedState = await assembleState(flow.flowId, {
|
|
413
|
+
runStore, nodeExecutionsStore, baseState: flow.stateJson ?? {},
|
|
414
|
+
});
|
|
415
|
+
// .gates must NEVER come from assembleState()'s own generic per-key
|
|
416
|
+
// merge — every human-gate node writes a FULL SNAPSHOT of state.gates
|
|
417
|
+
// into its own resultJson (executor-nodes.js: `gates: {
|
|
418
|
+
// ...(state.gates ?? {}), [nodeId]: record }`), so replaying rows in
|
|
419
|
+
// invokedAt order can resolve to a stale snapshot if a later gate opens
|
|
420
|
+
// out of the order assembleState() assumes. gate_records (a dedicated
|
|
421
|
+
// per-flow accessor, one row per gate, no merge) is the staleness-safe
|
|
422
|
+
// source — same fix already applied to the engine's own human-gate
|
|
423
|
+
// reads when B2 landed.
|
|
424
|
+
const gates = await nodeExecutionsStore.listGateRecordsForFlow(flow.flowId);
|
|
425
|
+
// Strip `loop` from the stateJson payload — subflow data is served via
|
|
426
|
+
// GET /manifold/runs/:flowId/subflows (childFlows, not old-model branchService).
|
|
427
|
+
// Strip `errors` too: failure_events is now the sole store for error
|
|
428
|
+
// history ("Eradicate soft errors" Phase 1.5) — errorEntries below is
|
|
429
|
+
// the normalized replacement. Only historical rows persisted before
|
|
430
|
+
// that redirect still carry a baked-in errors[] in their stored
|
|
431
|
+
// stateJson; stripping it here keeps the wire contract clean for those
|
|
432
|
+
// too.
|
|
433
|
+
const { loop: _loop, errors: _errors, gates: _staleGates, ...stateWithoutLegacy } = enrichedState;
|
|
434
|
+
// A subflow child is now just a flow_runs row with parent_invocation_id set —
|
|
435
|
+
// resolve it back to the parent's flowId/nodeId/subflowKey so clients can
|
|
436
|
+
// render a "Parent run" link. Also resolves this child's REAL top-level
|
|
437
|
+
// pipelineId (its own stateJson.pipelineId is the synthetic
|
|
438
|
+
// "parent::NODE_ID" form loader.js registers subflow nodes under) —
|
|
439
|
+
// same fix listQueued() already applies for the Queues feature.
|
|
440
|
+
let parentFlowId = null, parentNodeId = null, parentSubflowKey = null;
|
|
441
|
+
let resolvedPipelineId = flow.stateJson?.pipelineId ?? null;
|
|
442
|
+
if (flow.parentInvocationId) {
|
|
443
|
+
const invocation = await nodeExecutionsStore.getInvocation(flow.parentInvocationId);
|
|
444
|
+
if (invocation) {
|
|
445
|
+
parentFlowId = invocation.flowId;
|
|
446
|
+
parentNodeId = invocation.nodeId;
|
|
447
|
+
parentSubflowKey = invocation.subflowKey;
|
|
448
|
+
const parentFlow = await readStore.get(parentFlowId);
|
|
449
|
+
if (parentFlow) resolvedPipelineId = parentFlow.pipelineId ?? resolvedPipelineId;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
const payload = {
|
|
453
|
+
flowId: flow.flowId,
|
|
454
|
+
pipelineId: resolvedPipelineId,
|
|
455
|
+
runId: flow.stateJson?.runId ?? null,
|
|
456
|
+
status: flow.status,
|
|
457
|
+
currentStep: flow.currentStep ?? null,
|
|
458
|
+
lastStep: flow.lastStep,
|
|
459
|
+
revision: flow.revision,
|
|
460
|
+
statusReason: flow.statusReason ?? null,
|
|
461
|
+
stateJson: { ...stateWithoutLegacy, gates },
|
|
462
|
+
waitJson: flow.waitJson ?? null,
|
|
463
|
+
// Normalized error history sourced from failure_events — sibling to
|
|
464
|
+
// stateJson (not nested inside it) so flow-level and subflow-level
|
|
465
|
+
// shapes stay symmetric.
|
|
466
|
+
errorEntries: await failureStore.queryErrorsByFlow(flow.flowId),
|
|
467
|
+
createdAtMs: flow.createdAt ?? null,
|
|
468
|
+
updatedAtMs: flow.updatedAt ?? null,
|
|
469
|
+
endedAtMs: flow.endedAt ?? null,
|
|
470
|
+
parentFlowId,
|
|
471
|
+
nodeId: parentNodeId,
|
|
472
|
+
subflowKey: parentSubflowKey,
|
|
473
|
+
};
|
|
474
|
+
res.statusCode = 200;
|
|
475
|
+
res.setHeader("Content-Type", "application/json");
|
|
476
|
+
res.end(JSON.stringify(payload));
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// ── sendArtifactsList ─────────────────────────────────────────────────────
|
|
481
|
+
|
|
482
|
+
async function sendArtifactsList(res, flow) {
|
|
483
|
+
const { readdir, stat } = await import("node:fs/promises");
|
|
484
|
+
const pathMod = await import("node:path");
|
|
485
|
+
const pipelineId = flow.stateJson?.pipelineId;
|
|
486
|
+
const runId = flow.stateJson?.runId;
|
|
487
|
+
if (!pipelineId || !runId) {
|
|
488
|
+
res.statusCode = 200;
|
|
489
|
+
res.setHeader("Content-Type", "application/json");
|
|
490
|
+
res.end(JSON.stringify({ artifacts: [] }));
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
const pipeline = pipelinesRef.value.get(pipelineId);
|
|
494
|
+
if (!pipeline?._dir) {
|
|
495
|
+
res.statusCode = 200;
|
|
496
|
+
res.setHeader("Content-Type", "application/json");
|
|
497
|
+
res.end(JSON.stringify({ artifacts: [] }));
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
const runDir = pathMod.join(pipeline._dir, "runs", runId);
|
|
501
|
+
try {
|
|
502
|
+
const entries = await readdir(runDir, { withFileTypes: true });
|
|
503
|
+
const out = [];
|
|
504
|
+
for (const ent of entries) {
|
|
505
|
+
if (!ent.isFile()) continue;
|
|
506
|
+
const full = pathMod.join(runDir, ent.name);
|
|
507
|
+
const s = await stat(full);
|
|
508
|
+
out.push({ name: ent.name, size: s.size, mtimeMs: s.mtimeMs });
|
|
509
|
+
}
|
|
510
|
+
out.sort((a, b) => a.name.localeCompare(b.name));
|
|
511
|
+
res.statusCode = 200;
|
|
512
|
+
res.setHeader("Content-Type", "application/json");
|
|
513
|
+
res.end(JSON.stringify({ artifacts: out }));
|
|
514
|
+
} catch (err) {
|
|
515
|
+
if (err.code === "ENOENT") {
|
|
516
|
+
res.statusCode = 200;
|
|
517
|
+
res.setHeader("Content-Type", "application/json");
|
|
518
|
+
res.end(JSON.stringify({ artifacts: [] }));
|
|
519
|
+
return true;
|
|
520
|
+
}
|
|
521
|
+
throw err;
|
|
522
|
+
}
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// ── sendArtifactFile ──────────────────────────────────────────────────────
|
|
527
|
+
|
|
528
|
+
async function sendArtifactFile(res, flow, name) {
|
|
529
|
+
const { readFile, stat } = await import("node:fs/promises");
|
|
530
|
+
const pathMod = await import("node:path");
|
|
531
|
+
const pipelineId = flow.stateJson?.pipelineId;
|
|
532
|
+
const runId = flow.stateJson?.runId;
|
|
533
|
+
if (!pipelineId || !runId) {
|
|
534
|
+
res.statusCode = 404;
|
|
535
|
+
res.setHeader("Content-Type", "application/json");
|
|
536
|
+
res.end(JSON.stringify({ ok: false, error: "no run artifacts" }));
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
const pipeline = pipelinesRef.value.get(pipelineId);
|
|
540
|
+
if (!pipeline?._dir) {
|
|
541
|
+
res.statusCode = 404;
|
|
542
|
+
res.setHeader("Content-Type", "application/json");
|
|
543
|
+
res.end(JSON.stringify({ ok: false, error: "pipeline dir unknown" }));
|
|
544
|
+
return true;
|
|
545
|
+
}
|
|
546
|
+
const runDir = pathMod.join(pipeline._dir, "runs", runId);
|
|
547
|
+
const resolved = pathMod.resolve(runDir, name);
|
|
548
|
+
if (!resolved.startsWith(runDir + pathMod.sep)) {
|
|
549
|
+
res.statusCode = 403;
|
|
550
|
+
res.setHeader("Content-Type", "application/json");
|
|
551
|
+
res.end(JSON.stringify({ ok: false, error: "forbidden" }));
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
try {
|
|
555
|
+
const s = await stat(resolved);
|
|
556
|
+
if (!s.isFile()) {
|
|
557
|
+
res.statusCode = 404;
|
|
558
|
+
res.setHeader("Content-Type", "application/json");
|
|
559
|
+
res.end(JSON.stringify({ ok: false, error: "not a file" }));
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
if (s.size > 5 * 1024 * 1024) {
|
|
563
|
+
res.statusCode = 413;
|
|
564
|
+
res.setHeader("Content-Type", "application/json");
|
|
565
|
+
res.end(JSON.stringify({ ok: false, error: "artifact too large", size: s.size }));
|
|
566
|
+
return true;
|
|
567
|
+
}
|
|
568
|
+
const body = await readFile(resolved);
|
|
569
|
+
const ext = pathMod.extname(resolved).toLowerCase();
|
|
570
|
+
const mime =
|
|
571
|
+
ext === ".json" ? "application/json" :
|
|
572
|
+
ext === ".jsonl" ? "application/x-ndjson" :
|
|
573
|
+
ext === ".md" ? "text/markdown" :
|
|
574
|
+
ext === ".txt" ? "text/plain" :
|
|
575
|
+
ext === ".log" ? "text/plain" :
|
|
576
|
+
ext === ".html" ? "text/html" :
|
|
577
|
+
ext === ".csv" ? "text/csv" :
|
|
578
|
+
"application/octet-stream";
|
|
579
|
+
res.statusCode = 200;
|
|
580
|
+
res.setHeader("Content-Type", mime);
|
|
581
|
+
res.setHeader("Content-Length", String(s.size));
|
|
582
|
+
res.end(body);
|
|
583
|
+
} catch (err) {
|
|
584
|
+
if (err.code === "ENOENT") {
|
|
585
|
+
res.statusCode = 404;
|
|
586
|
+
res.setHeader("Content-Type", "application/json");
|
|
587
|
+
res.end(JSON.stringify({ ok: false, error: "not found" }));
|
|
588
|
+
return true;
|
|
589
|
+
}
|
|
590
|
+
throw err;
|
|
591
|
+
}
|
|
592
|
+
return true;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// ── sendSubflowsList ─────────────────────────────────────────────────────
|
|
596
|
+
|
|
597
|
+
async function sendSubflowsList(res, flow) {
|
|
598
|
+
const grouped = await getAllSubflowGroups({
|
|
599
|
+
flowId: flow.flowId, runStore, nodeExecutionsStore: getNodeExecutionsStore(),
|
|
600
|
+
});
|
|
601
|
+
// Strip the legacy `errors` column (Phase 1.6) — errorEntries (Part B's
|
|
602
|
+
// enrichment) is the sole normalized replacement now that
|
|
603
|
+
// executor-branch.js no longer writes this field.
|
|
604
|
+
const subflows = Object.values(grouped).flatMap((group) =>
|
|
605
|
+
Object.values(group).map(({ errors: _errors, ...rest }) => rest)
|
|
606
|
+
);
|
|
607
|
+
res.statusCode = 200;
|
|
608
|
+
res.setHeader("Content-Type", "application/json");
|
|
609
|
+
res.end(JSON.stringify({ subflows }));
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// ── node tree ─────────────────────────────────────────────────────────────
|
|
614
|
+
//
|
|
615
|
+
// Unifies the old Steps (this flow's own node timeline) and Subflows
|
|
616
|
+
// (child-flow fan-out) sections into one recursively-expandable tree.
|
|
617
|
+
// Two row shapes, one recursive expand operation:
|
|
618
|
+
// type:"node" — one row per invocation (not per execution attempt — see
|
|
619
|
+
// buildTreeNodeRows) for a single flow's own dispatches. Expandable
|
|
620
|
+
// only when kind==="subflow" and it has spawned at least one child.
|
|
621
|
+
// type:"flow" — one row per child flow_runs spawned from a subflow-kind
|
|
622
|
+
// node's invocation. Expandable once it has dispatched at least one
|
|
623
|
+
// node of its own.
|
|
624
|
+
// Expanding a "node" row returns "flow" children; expanding a "flow" row
|
|
625
|
+
// returns that flow's own "node" list (same shape /tree itself returns).
|
|
626
|
+
|
|
627
|
+
// Builds this flow's own top-level node list — the payload for both the
|
|
628
|
+
// /tree root call and a /tree/expand?type=flow call (a descendant flow's
|
|
629
|
+
// own dispatches use the exact same shape as the root).
|
|
630
|
+
async function buildTreeNodeRows(flow) {
|
|
631
|
+
const pipelineId = flow.stateJson?.pipelineId;
|
|
632
|
+
const nodeExecutionsStore = getNodeExecutionsStore();
|
|
633
|
+
const invocations = await nodeExecutionsStore.listInvocationsForFlow(flow.flowId);
|
|
634
|
+
// A node's own status can only be told apart from a failed one by
|
|
635
|
+
// cross-referencing failure_events (Phase 1.5 — flow_node_executions no
|
|
636
|
+
// longer stores error content). Every failure this flow ever recorded,
|
|
637
|
+
// not just root-level ones, but only executionId matters here, and a
|
|
638
|
+
// subflow item's own errors live under ITS OWN flow_id, not this one's —
|
|
639
|
+
// so this set naturally only ever matches root-level invocations.
|
|
640
|
+
const failedExecutionIds = new Set(
|
|
641
|
+
(await failureStore.queryErrorsByFlow(flow.flowId)).map((e) => e.executionId).filter(Boolean)
|
|
642
|
+
);
|
|
643
|
+
|
|
644
|
+
// Point-in-time kind/agent per invocation — replaces reading
|
|
645
|
+
// pipelinesRef.value directly. A node whose pipeline definition has
|
|
646
|
+
// since changed, or whose whole pipeline was deleted, still renders its
|
|
647
|
+
// ORIGINAL kind/agent, keyed by this invocation's own invokedAt rather
|
|
648
|
+
// than whatever the live, single-copy definition currently says (or no
|
|
649
|
+
// longer has at all).
|
|
650
|
+
// Batched in one call, not one query per invocation.
|
|
651
|
+
const versionRequests = invocations.map((inv) => ({ pipelineId, nodeName: inv.nodeId, atMs: inv.invokedAt }));
|
|
652
|
+
const versions = await pipelineRegistryStore.getNodeVersionsBatch(versionRequests);
|
|
653
|
+
const versionByInvocationId = new Map(invocations.map((inv, i) => [inv.invocationId, versions[i]]));
|
|
654
|
+
|
|
655
|
+
// A subflow-kind row's OWN invocation (subflow_key=null) is never a
|
|
656
|
+
// child's parent_invocation_id — each branch item gets its OWN separate
|
|
657
|
+
// invocation, keyed by the item's own subflow_key (see
|
|
658
|
+
// listInvocationsForFlow's doc comment). Resolve those per-nodeId via
|
|
659
|
+
// listInvocationIdsForNode first, batched once per distinct nodeId
|
|
660
|
+
// rather than per row.
|
|
661
|
+
//
|
|
662
|
+
// Known limitation: if the same nodeId is visited more than once (a
|
|
663
|
+
// loop revisits it — confirmed to happen, e.g. PROPOSE), there is no
|
|
664
|
+
// schema-level way to partition which items belong to which visit —
|
|
665
|
+
// listInvocationIdsForNode returns every item ever spawned under that
|
|
666
|
+
// (flowId, nodeId) pair, full stop (a real item invocation's own
|
|
667
|
+
// visit_seq/forceNew is never set — see engine/spawn-child.js). All
|
|
668
|
+
// rows sharing that nodeId therefore see the SAME cumulative child
|
|
669
|
+
// list. Rare in practice (most subflow nodes dispatch once per flow);
|
|
670
|
+
// not solved here — would need a schema change (tying an item
|
|
671
|
+
// invocation to its spawning visit) to fix properly.
|
|
672
|
+
const subflowNodeIds = [
|
|
673
|
+
...new Set(invocations.filter((inv) => versionByInvocationId.get(inv.invocationId)?.kind === "subflow").map((inv) => inv.nodeId)),
|
|
674
|
+
];
|
|
675
|
+
const childrenByNodeId = new Map();
|
|
676
|
+
if (subflowNodeIds.length > 0) {
|
|
677
|
+
const invIdsByNodeId = await nodeExecutionsStore.listInvocationIdsForNodes(flow.flowId, subflowNodeIds);
|
|
678
|
+
const allInvIds = [...invIdsByNodeId.values()].flat();
|
|
679
|
+
const allChildren = await runStore.listForParentInvocations(allInvIds);
|
|
680
|
+
const nodeIdByInvId = new Map();
|
|
681
|
+
for (const [nodeId, ids] of invIdsByNodeId) {
|
|
682
|
+
for (const id of ids) nodeIdByInvId.set(id, nodeId);
|
|
683
|
+
}
|
|
684
|
+
for (const child of allChildren) {
|
|
685
|
+
const nodeId = nodeIdByInvId.get(child.parentInvocationId);
|
|
686
|
+
if (!nodeId) continue;
|
|
687
|
+
if (!childrenByNodeId.has(nodeId)) childrenByNodeId.set(nodeId, []);
|
|
688
|
+
childrenByNodeId.get(nodeId).push(child);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const lastIdx = invocations.length - 1;
|
|
693
|
+
return invocations.map((inv, idx) => {
|
|
694
|
+
const version = versionByInvocationId.get(inv.invocationId);
|
|
695
|
+
const kind = version?.kind ?? null;
|
|
696
|
+
const isSubflowNode = kind === "subflow";
|
|
697
|
+
const kids = isSubflowNode ? (childrenByNodeId.get(inv.nodeId) ?? []) : [];
|
|
698
|
+
// A node's own execution row carries no success/failure signal at all
|
|
699
|
+
// (flow_node_executions is a pure identity/timing ledger;
|
|
700
|
+
// failedExecutionIds above is the only way to know a given attempt
|
|
701
|
+
// failed). Absent that, the only way to recover status is by
|
|
702
|
+
// cross-referencing the FLOW's own status — which only ever applies
|
|
703
|
+
// to the LAST invocation in this list (a flow can't be mid-dispatch
|
|
704
|
+
// or have skipped/blocked at an earlier, already-superseded node).
|
|
705
|
+
// invocations is already ordered ascending by invoked_at
|
|
706
|
+
// (listInvocationsForFlow), so idx === lastIdx already and only ever
|
|
707
|
+
// means "the most recently invoked node for this flow" — no extra
|
|
708
|
+
// currentStep/isTerminal gate: comparing a dropped field is always
|
|
709
|
+
// false, and gating on !isTerminal would drop a terminal flow's real
|
|
710
|
+
// status from its own last node whenever that node's execution
|
|
711
|
+
// recorded no error, defaulting a failed flow to "succeeded".
|
|
712
|
+
let status;
|
|
713
|
+
if (inv.latestExecutionId && failedExecutionIds.has(inv.latestExecutionId)) {
|
|
714
|
+
status = "failed";
|
|
715
|
+
} else if (idx === lastIdx) {
|
|
716
|
+
status = flow.status; // live: "running" | "blocked" | "queued"; terminal: whatever flow.status is
|
|
717
|
+
} else {
|
|
718
|
+
status = "succeeded";
|
|
719
|
+
}
|
|
720
|
+
const row = {
|
|
721
|
+
type: "node",
|
|
722
|
+
invocationId: inv.invocationId,
|
|
723
|
+
nodeId: inv.nodeId,
|
|
724
|
+
kind,
|
|
725
|
+
agent: version?.agent ?? null,
|
|
726
|
+
status,
|
|
727
|
+
enteredAt: inv.enteredAt,
|
|
728
|
+
exitedAt: inv.exitedAt,
|
|
729
|
+
execKind: inv.kind,
|
|
730
|
+
executionCount: inv.executionCount,
|
|
731
|
+
// execKind above is the LATEST execution only, which for any
|
|
732
|
+
// subflow node with finished children is always "recheck" — masking
|
|
733
|
+
// a real "retry"/"revival" earlier in the same invocation's
|
|
734
|
+
// history. Independent signal, not a replacement for execKind.
|
|
735
|
+
everRetriedOrRevived: inv.everRetriedOrRevived,
|
|
736
|
+
hasChildren: isSubflowNode && kids.length > 0,
|
|
737
|
+
};
|
|
738
|
+
if (isSubflowNode) row.rollup = bucketChildFlowsByStatus(kids);
|
|
739
|
+
return row;
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// Shallow rollup (immediate children only, not a recursive subtree walk) —
|
|
744
|
+
// same status-bucket shape summarizeChildrenByFlowIds already uses.
|
|
745
|
+
function bucketChildFlowsByStatus(childFlowRows) {
|
|
746
|
+
const rollup = { succeeded: 0, failed: 0, pending: 0, skipped: 0, cancelled: 0 };
|
|
747
|
+
for (const c of childFlowRows) {
|
|
748
|
+
if (c.status === "succeeded") rollup.succeeded++;
|
|
749
|
+
else if (c.status === "failed") rollup.failed++;
|
|
750
|
+
else if (c.status === "skipped") rollup.skipped++;
|
|
751
|
+
else if (c.status === "cancelled") rollup.cancelled++;
|
|
752
|
+
else rollup.pending++; // running | blocked | queued
|
|
753
|
+
}
|
|
754
|
+
return rollup;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// The human-readable "why" behind a non-succeeded child flow, for the
|
|
758
|
+
// node tree's attention banner. A human-gate wait carries a real,
|
|
759
|
+
// pipeline-authored title in
|
|
760
|
+
// waitJson.items[0].requestPayload.title (e.g. "Cyrano needs 2 things
|
|
761
|
+
// to draft <listing>") — confirmed against live data, far more useful
|
|
762
|
+
// than the generic statusReason ("awaiting 1 approval at GAP_GATE")
|
|
763
|
+
// sitting next to it. Every other wait kind, and failures, fall back to
|
|
764
|
+
// statusReason, which the executor already writes as a short human
|
|
765
|
+
// summary (shortSummary() calls throughout executor.js).
|
|
766
|
+
function extractAttentionReason(child) {
|
|
767
|
+
if (child.waitJson?.kind === "human-gate") {
|
|
768
|
+
const title = child.waitJson.items?.[0]?.requestPayload?.title;
|
|
769
|
+
if (typeof title === "string" && title) return title;
|
|
770
|
+
}
|
|
771
|
+
return child.statusReason ?? null;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
// Maps child flow_runs rows (spawned from one subflow-kind node's
|
|
775
|
+
// invocation) into the tree's type:"flow" row shape.
|
|
776
|
+
async function buildTreeFlowRows(childFlowRows) {
|
|
777
|
+
const nodeExecutionsStore = getNodeExecutionsStore();
|
|
778
|
+
const withInvocations = await nodeExecutionsStore.hasInvocations(childFlowRows.map((c) => c.flowId));
|
|
779
|
+
const derived = await Promise.all(childFlowRows.map((child) => perChildDerivedFields(child, nodeExecutionsStore)));
|
|
780
|
+
return childFlowRows.map((child, idx) => ({
|
|
781
|
+
type: "flow",
|
|
782
|
+
flowId: child.flowId,
|
|
783
|
+
status: child.status,
|
|
784
|
+
startedAt: child.createdAt,
|
|
785
|
+
updatedAt: child.updatedAt,
|
|
786
|
+
endedAt: child.endedAt,
|
|
787
|
+
display: child.stateJson?._display ?? null,
|
|
788
|
+
reason: child.status === "succeeded" ? null : extractAttentionReason(child),
|
|
789
|
+
hasChildren: withInvocations.has(child.flowId),
|
|
790
|
+
cascadeAttempt: derived[idx].cascadeAttempt,
|
|
791
|
+
// A spawned child flow can independently retry at its own dispatch
|
|
792
|
+
// level without the SUBFLOW NODE that spawned it (buildTreeNodeRows'
|
|
793
|
+
// own row, above) ever retrying — different table (flow_runs, not
|
|
794
|
+
// flow_node_invocations), no execKind at that level at all.
|
|
795
|
+
// Deliberately per-child, not rolled up onto the parent's own row —
|
|
796
|
+
// matches this file's "shallow, immediate-children only" rollup rule.
|
|
797
|
+
everRetriedOrRevived: derived[idx].everRetriedOrRevived,
|
|
798
|
+
}));
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// One query per child (nodeExecutionsStore.listInvocationsForFlow),
|
|
802
|
+
// shared by both derived fields below rather than each querying
|
|
803
|
+
// independently — cascadeAttempt only used it when pipeline.kind ===
|
|
804
|
+
// "cascade" (short-circuiting otherwise), everRetriedOrRevived needs it
|
|
805
|
+
// unconditionally, so combining costs nothing extra for cascade children
|
|
806
|
+
// and adds exactly the one necessary query for non-cascade ones.
|
|
807
|
+
async function perChildDerivedFields(child, nodeExecutionsStore) {
|
|
808
|
+
const invocations = await nodeExecutionsStore.listInvocationsForFlow(child.flowId);
|
|
809
|
+
return {
|
|
810
|
+
cascadeAttempt: await cascadeAttemptFor(child, invocations),
|
|
811
|
+
everRetriedOrRevived: invocations.some((inv) => inv.everRetriedOrRevived),
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// A cascade-kind pipeline (README.md's "retry-on-failure as a flow
|
|
816
|
+
// characteristic") tries its own root nodes in sequence, one throw at a
|
|
817
|
+
// time, until one succeeds or all are exhausted — every attempt still
|
|
818
|
+
// gets its own ordinary flow_node_invocations row (see
|
|
819
|
+
// buildTreeNodeRows), so "how many root nodes did this child flow
|
|
820
|
+
// dispatch" is a direct, non-heuristic count of those rows. > 1 means
|
|
821
|
+
// the first node did NOT resolve it — something worth flagging, even on
|
|
822
|
+
// an eventual success. Non-cascade pipelines always return null here;
|
|
823
|
+
// ordinary sequential nodes look identical in shape and would false-
|
|
824
|
+
// positive if this weren't gated on the pipeline's own declared kind.
|
|
825
|
+
//
|
|
826
|
+
// Point-in-time (state/pipeline-registry.js) — keyed by this child
|
|
827
|
+
// flow's own createdAt, not the live pipelinesRef.value.
|
|
828
|
+
async function cascadeAttemptFor(child, invocations) {
|
|
829
|
+
const version = await pipelineRegistryStore.getPipelineVersionAt({
|
|
830
|
+
pipelineId: child.stateJson?.pipelineId, atMs: child.createdAt,
|
|
831
|
+
});
|
|
832
|
+
if (version?.kind !== "cascade") return null;
|
|
833
|
+
const attempts = invocations.length;
|
|
834
|
+
return attempts > 1 ? attempts : null;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
async function sendTreeRoot(res, flow) {
|
|
838
|
+
const items = await buildTreeNodeRows(flow);
|
|
839
|
+
res.statusCode = 200;
|
|
840
|
+
res.setHeader("Content-Type", "application/json");
|
|
841
|
+
res.end(JSON.stringify({ items }));
|
|
842
|
+
return true;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
async function sendTreeExpand(res, flow, url) {
|
|
846
|
+
const type = url.searchParams.get("type");
|
|
847
|
+
if (type === "node") {
|
|
848
|
+
const invocationId = url.searchParams.get("invocationId");
|
|
849
|
+
if (!invocationId) {
|
|
850
|
+
res.statusCode = 400;
|
|
851
|
+
res.setHeader("Content-Type", "application/json");
|
|
852
|
+
res.end(JSON.stringify({ ok: false, error: "invocationId required for type=node" }));
|
|
853
|
+
return true;
|
|
854
|
+
}
|
|
855
|
+
const invocation = await getNodeExecutionsStore().getInvocation(invocationId);
|
|
856
|
+
if (!invocation || invocation.flowId !== flow.flowId) {
|
|
857
|
+
res.statusCode = 404;
|
|
858
|
+
res.setHeader("Content-Type", "application/json");
|
|
859
|
+
res.end(JSON.stringify({ ok: false, error: "invocation not found on this flow" }));
|
|
860
|
+
return true;
|
|
861
|
+
}
|
|
862
|
+
// invocationId here is the subflow-kind node's OWN dispatch (subflow_
|
|
863
|
+
// key=null) — never a child's parent_invocation_id, same reasoning as
|
|
864
|
+
// buildTreeNodeRows. Resolve the real per-item invocations for this
|
|
865
|
+
// nodeId first (same known multi-visit limitation documented there).
|
|
866
|
+
const itemInvocationIds = await getNodeExecutionsStore().listInvocationIdsForNode(flow.flowId, invocation.nodeId);
|
|
867
|
+
const children = await runStore.listForParentInvocations(itemInvocationIds);
|
|
868
|
+
res.statusCode = 200;
|
|
869
|
+
res.setHeader("Content-Type", "application/json");
|
|
870
|
+
res.end(JSON.stringify({ items: await buildTreeFlowRows(children) }));
|
|
871
|
+
return true;
|
|
872
|
+
}
|
|
873
|
+
if (type === "flow") {
|
|
874
|
+
const childFlowId = url.searchParams.get("childFlowId");
|
|
875
|
+
if (!childFlowId) {
|
|
876
|
+
res.statusCode = 400;
|
|
877
|
+
res.setHeader("Content-Type", "application/json");
|
|
878
|
+
res.end(JSON.stringify({ ok: false, error: "childFlowId required for type=flow" }));
|
|
879
|
+
return true;
|
|
880
|
+
}
|
|
881
|
+
const childFlow = await readStore.get(childFlowId);
|
|
882
|
+
if (!childFlow) {
|
|
883
|
+
res.statusCode = 404;
|
|
884
|
+
res.setHeader("Content-Type", "application/json");
|
|
885
|
+
res.end(JSON.stringify({ ok: false, error: "child flow not found", childFlowId }));
|
|
886
|
+
return true;
|
|
887
|
+
}
|
|
888
|
+
res.statusCode = 200;
|
|
889
|
+
res.setHeader("Content-Type", "application/json");
|
|
890
|
+
res.end(JSON.stringify({ items: await buildTreeNodeRows(childFlow) }));
|
|
891
|
+
return true;
|
|
892
|
+
}
|
|
893
|
+
res.statusCode = 400;
|
|
894
|
+
res.setHeader("Content-Type", "application/json");
|
|
895
|
+
res.end(JSON.stringify({ ok: false, error: `invalid type "${type}" — expected "node" or "flow"` }));
|
|
896
|
+
return true;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// Full execution-attempt history for one invocation — the tree's
|
|
900
|
+
// per-node drill-in (a row with executionCount > 1 links here).
|
|
901
|
+
async function sendInvocationDetail(res, flow, invocationId) {
|
|
902
|
+
const invocation = await getNodeExecutionsStore().getInvocation(invocationId);
|
|
903
|
+
if (!invocation || invocation.flowId !== flow.flowId) {
|
|
904
|
+
res.statusCode = 404;
|
|
905
|
+
res.setHeader("Content-Type", "application/json");
|
|
906
|
+
res.end(JSON.stringify({ ok: false, error: "invocation not found on this flow" }));
|
|
907
|
+
return true;
|
|
908
|
+
}
|
|
909
|
+
// Point-in-time (state/pipeline-registry.js) — same reasoning as
|
|
910
|
+
// buildTreeNodeRows, keyed by this invocation's own invokedAt rather
|
|
911
|
+
// than the live pipelinesRef.value.
|
|
912
|
+
const version = await pipelineRegistryStore.getNodeVersionAt({
|
|
913
|
+
pipelineId: flow.stateJson?.pipelineId, nodeName: invocation.nodeId, atMs: invocation.invokedAt,
|
|
914
|
+
});
|
|
915
|
+
const rawExecutions = await getNodeExecutionsStore().listExecutionsForInvocation(invocationId);
|
|
916
|
+
// Per-attempt error content lives in failure_events now (Phase 1.5) —
|
|
917
|
+
// cross-reference by executionId. A given execution_id gets at most one
|
|
918
|
+
// failure row (upsertFailureRow's conflict target), so this is a clean
|
|
919
|
+
// 1:1 lookup, not a dedup problem.
|
|
920
|
+
const messageByExecutionId = new Map(
|
|
921
|
+
(await failureStore.queryErrorsByFlow(flow.flowId))
|
|
922
|
+
.filter((e) => e.invocationId === invocationId)
|
|
923
|
+
.map((e) => [e.executionId, e.message])
|
|
924
|
+
);
|
|
925
|
+
const executions = rawExecutions.map((e) => ({ ...e, error: messageByExecutionId.get(e.executionId) ?? null }));
|
|
926
|
+
res.statusCode = 200;
|
|
927
|
+
res.setHeader("Content-Type", "application/json");
|
|
928
|
+
res.end(JSON.stringify({
|
|
929
|
+
invocationId: invocation.id,
|
|
930
|
+
nodeId: invocation.nodeId,
|
|
931
|
+
kind: version?.kind ?? null,
|
|
932
|
+
agent: version?.agent ?? null,
|
|
933
|
+
subflowKey: invocation.subflowKey,
|
|
934
|
+
visitSeq: invocation.visitSeq,
|
|
935
|
+
executions,
|
|
936
|
+
}));
|
|
937
|
+
return true;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// ── sendTranscriptsList ───────────────────────────────────────────────────
|
|
941
|
+
|
|
942
|
+
// Transcripts read manifold's own stores only. Discuss threads come from
|
|
943
|
+
// discuss-store (both directions of every dialog-door turn, keyed by
|
|
944
|
+
// (flowId, itemId)); dispatch agent turns come from node-executions
|
|
945
|
+
// resultJson (a single assistant turn when the stored result carries
|
|
946
|
+
// reply text, metadata-only otherwise — content is never invented).
|
|
947
|
+
// Attribution uses stored columns (flow_sessions node/branch columns,
|
|
948
|
+
// discuss itemId); the session passes through opaquely and is never
|
|
949
|
+
// parsed here.
|
|
950
|
+
|
|
951
|
+
// lite=1 → return turnCount only, no turn text (for list views)
|
|
952
|
+
// session=X → return a single transcript with full turns (for transcript drill-in)
|
|
953
|
+
async function sendTranscriptsList(res, flow, url) {
|
|
954
|
+
const lite = url.searchParams.get("lite") === "1";
|
|
955
|
+
const singleSession = url.searchParams.get("session") ?? null;
|
|
956
|
+
|
|
957
|
+
// Session roster for this flow: one row per dispatch attempt plus one
|
|
958
|
+
// per discuss thread, each carrying its stored node/item attribution.
|
|
959
|
+
const sessionRows = await Promise.resolve(sessionIndex?.listForFlow?.(flow.flowId)).catch(() => []) ?? [];
|
|
960
|
+
const discussThreads = await Promise.resolve(discussStore?.listForFlow?.(flow.flowId)).catch(() => []) ?? [];
|
|
961
|
+
const nodeExecutionsStore = getNodeExecutionsStore();
|
|
962
|
+
const results = await Promise.resolve(nodeExecutionsStore.listResultsForFlow(flow.flowId)).catch(() => []);
|
|
963
|
+
const resultByNode = new Map((results ?? []).map((r) => [r?.nodeId, r]));
|
|
964
|
+
// Agent display name resolves from the loaded pipeline's own node def;
|
|
965
|
+
// null when the pipeline is not loaded — never guessed from key shape.
|
|
966
|
+
const pipelineNodes = pipelinesRef?.value?.get?.(flow.stateJson?.pipelineId)?.nodes ?? {};
|
|
967
|
+
|
|
968
|
+
const transcripts = [];
|
|
969
|
+
for (const row of sessionRows) {
|
|
970
|
+
// Discuss identity rows (branchItemId set) are covered by the discuss
|
|
971
|
+
// loop below, which joins them to full turns from discuss-store.
|
|
972
|
+
if (row.branchItemId != null) continue;
|
|
973
|
+
if (singleSession && row.session !== singleSession) continue;
|
|
974
|
+
const result = row.nodeId != null ? resultByNode.get(row.nodeId) : null;
|
|
975
|
+
const turns = lite ? null : dispatchTurns(result);
|
|
976
|
+
transcripts.push({
|
|
977
|
+
session: row.session, agent: pipelineNodes?.[row.nodeId]?.agent ?? null,
|
|
978
|
+
nodeId: row.nodeId ?? null, itemKey: row.branchItemId ?? null,
|
|
979
|
+
startedAt: row.spawnedAt ?? null,
|
|
980
|
+
updatedAt: result?.resultWrittenAt ?? row.spawnedAt ?? null,
|
|
981
|
+
...(lite ? { turnCount: turnsForCount(result) } : { turns }),
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
for (const thread of discussThreads) {
|
|
985
|
+
// A thread predating session-index recording has turns but no stored
|
|
986
|
+
// identity row — it still lists, with null identity fields.
|
|
987
|
+
let identity = null;
|
|
988
|
+
for (const row of sessionRows) {
|
|
989
|
+
if (row.branchItemId === thread.itemId && (!identity || (row.spawnedAt ?? 0) > (identity.spawnedAt ?? 0))) identity = row;
|
|
990
|
+
}
|
|
991
|
+
const session = identity?.session ?? null;
|
|
992
|
+
if (singleSession && session !== singleSession) continue;
|
|
993
|
+
const turns = lite ? null : discussTurns(thread.messages);
|
|
994
|
+
transcripts.push({
|
|
995
|
+
session, agent: null,
|
|
996
|
+
nodeId: identity?.nodeId ?? "discuss", itemKey: thread.itemId ?? null,
|
|
997
|
+
startedAt: thread.messages?.[0]?.ts ?? thread.updatedAt ?? null,
|
|
998
|
+
updatedAt: thread.updatedAt ?? null,
|
|
999
|
+
...(lite ? { turnCount: Array.isArray(thread.messages) ? thread.messages.length : 0 } : { turns }),
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
transcripts.sort((a, b) => (a.startedAt ?? 0) - (b.startedAt ?? 0));
|
|
1003
|
+
res.statusCode = 200;
|
|
1004
|
+
res.setHeader("Content-Type", "application/json");
|
|
1005
|
+
res.end(JSON.stringify({ transcripts }));
|
|
1006
|
+
return true;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
// Stored discuss messages use operator/agent roles; the transcript wire
|
|
1010
|
+
// shape (and its dashboard renderer) uses user/assistant.
|
|
1011
|
+
function discussTurns(messages) {
|
|
1012
|
+
const turns = [];
|
|
1013
|
+
if (!Array.isArray(messages)) return turns;
|
|
1014
|
+
for (const m of messages) {
|
|
1015
|
+
const { text, truncated } = capText(typeof m?.text === "string" ? m.text : "");
|
|
1016
|
+
turns.push({
|
|
1017
|
+
role: m?.role === "agent" ? "assistant" : "user",
|
|
1018
|
+
text, thinking: null, truncated, timestamp: m?.ts ?? null,
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
return turns;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// The only dispatch content manifold observes durably is the node's own
|
|
1025
|
+
// resultJson: a bare string, or an object carrying reply text in `text`.
|
|
1026
|
+
// Anything else yields no turn — the entry lists with metadata only.
|
|
1027
|
+
function dispatchTurns(result) {
|
|
1028
|
+
const text = dispatchTurnText(result?.resultJson);
|
|
1029
|
+
if (text == null) return [];
|
|
1030
|
+
const capped = capText(text);
|
|
1031
|
+
return [{
|
|
1032
|
+
role: "assistant", text: capped.text, thinking: null,
|
|
1033
|
+
truncated: capped.truncated, timestamp: result?.resultWrittenAt ?? null,
|
|
1034
|
+
}];
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
function turnsForCount(result) {
|
|
1038
|
+
return dispatchTurnText(result?.resultJson) == null ? 0 : 1;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
function dispatchTurnText(resultJson) {
|
|
1042
|
+
if (typeof resultJson !== "string") return null;
|
|
1043
|
+
let parsed = resultJson;
|
|
1044
|
+
try { parsed = JSON.parse(resultJson); } catch { return null; }
|
|
1045
|
+
if (typeof parsed === "string") return parsed;
|
|
1046
|
+
if (parsed && typeof parsed === "object" && typeof parsed.text === "string") return parsed.text;
|
|
1047
|
+
return null;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
function capText(text) {
|
|
1051
|
+
const cap = 8 * 1024;
|
|
1052
|
+
if (text.length <= cap) return { text, truncated: false };
|
|
1053
|
+
return { text: text.slice(0, cap), truncated: true };
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// ── sendCompareToPeers ────────────────────────────────────────────────────
|
|
1057
|
+
|
|
1058
|
+
async function sendCompareToPeers(res, flow) {
|
|
1059
|
+
const pipelineId = flow.stateJson?.pipelineId ?? "";
|
|
1060
|
+
const peers = await readStore.listPeers({ pipelineId, excludeFlowId: flow.flowId });
|
|
1061
|
+
const comparison = peers.length === 0 ? null : computeCompareToMedian(flow, peers);
|
|
1062
|
+
res.statusCode = 200;
|
|
1063
|
+
res.setHeader("Content-Type", "application/json");
|
|
1064
|
+
res.end(JSON.stringify({ ok: true, comparison }));
|
|
1065
|
+
return true;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
function computeCompareToMedian(run, peers) {
|
|
1069
|
+
const dur = (r) => {
|
|
1070
|
+
const end = r?.endedAt ?? r?.updatedAt ?? null;
|
|
1071
|
+
const start = r?.createdAt ?? null;
|
|
1072
|
+
return end != null && start != null ? end - start : null;
|
|
1073
|
+
};
|
|
1074
|
+
const median = (arr) => {
|
|
1075
|
+
const xs = arr.filter((v) => v != null && v >= 0).sort((a, b) => a - b);
|
|
1076
|
+
if (xs.length === 0) return null;
|
|
1077
|
+
const mid = Math.floor(xs.length / 2);
|
|
1078
|
+
return xs.length % 2 ? xs[mid] : (xs[mid - 1] + xs[mid]) / 2;
|
|
1079
|
+
};
|
|
1080
|
+
const peerDurations = peers.map(dur);
|
|
1081
|
+
const medianMs = median(peerDurations);
|
|
1082
|
+
const peerRevs = peers.map((r) => r.revision ?? 0);
|
|
1083
|
+
const medianRev = median(peerRevs);
|
|
1084
|
+
const runDur = dur(run);
|
|
1085
|
+
return {
|
|
1086
|
+
peerCount: peers.length,
|
|
1087
|
+
durationMs: runDur,
|
|
1088
|
+
durationMedianMs: medianMs,
|
|
1089
|
+
durationRatio: runDur != null && medianMs ? runDur / medianMs : null,
|
|
1090
|
+
revision: run.revision ?? 0,
|
|
1091
|
+
revisionMedian: medianRev,
|
|
1092
|
+
revisionRatio: medianRev != null && medianRev > 0 ? (run.revision ?? 0) / medianRev : null,
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
// ── handleRerun ───────────────────────────────────────────────────────────
|
|
1097
|
+
|
|
1098
|
+
async function handleRerun(req, res, sourceFlow) {
|
|
1099
|
+
let raw = "";
|
|
1100
|
+
for await (const chunk of req) raw += chunk;
|
|
1101
|
+
let body;
|
|
1102
|
+
try { body = raw ? JSON.parse(raw) : {}; }
|
|
1103
|
+
catch { res.statusCode = 400; res.end("invalid json"); return true; }
|
|
1104
|
+
const argsOverride = (body && typeof body === "object" && "args" in body) ? body.args : undefined;
|
|
1105
|
+
if (argsOverride !== undefined && (argsOverride === null || typeof argsOverride !== "object" || Array.isArray(argsOverride))) {
|
|
1106
|
+
res.statusCode = 400;
|
|
1107
|
+
res.setHeader("Content-Type", "application/json");
|
|
1108
|
+
res.end(JSON.stringify({ ok: false, error: "args must be a JSON object" }));
|
|
1109
|
+
return true;
|
|
1110
|
+
}
|
|
1111
|
+
try {
|
|
1112
|
+
const elev = await invokeElevated("/manifold/_elev/rerun", {
|
|
1113
|
+
sourceFlowId: sourceFlow.flowId,
|
|
1114
|
+
args: argsOverride,
|
|
1115
|
+
session: body.session,
|
|
1116
|
+
agentId: body.agentId,
|
|
1117
|
+
});
|
|
1118
|
+
res.statusCode = elev.status || 500;
|
|
1119
|
+
res.setHeader("Content-Type", "application/json");
|
|
1120
|
+
res.end(elev.body || JSON.stringify({ ok: false, code: "elevation_no_body" }));
|
|
1121
|
+
} catch (err) {
|
|
1122
|
+
api.logger.warn(`manifold rerun handler error: ${err?.message ?? err}`);
|
|
1123
|
+
res.statusCode = 500;
|
|
1124
|
+
res.setHeader("Content-Type", "application/json");
|
|
1125
|
+
res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
|
|
1126
|
+
}
|
|
1127
|
+
return true;
|
|
1128
|
+
}
|
|
1129
|
+
}
|