@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.
Files changed (210) hide show
  1. package/.migrations/ops/0000_futuristic_karma.sql +111 -0
  2. package/.migrations/ops/0001_low_maximus.sql +7 -0
  3. package/.migrations/ops/meta/0000_snapshot.json +763 -0
  4. package/.migrations/ops/meta/0001_snapshot.json +173 -0
  5. package/.migrations/ops/meta/_journal.json +20 -0
  6. package/.migrations/runtime/0000_real_boomer.sql +180 -0
  7. package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
  8. package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
  9. package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
  10. package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
  11. package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
  12. package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
  13. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
  14. package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
  15. package/.migrations/runtime/0009_lying_shocker.sql +96 -0
  16. package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
  17. package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
  18. package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
  19. package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
  20. package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
  21. package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
  22. package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
  23. package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
  24. package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
  25. package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
  26. package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
  27. package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
  28. package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
  29. package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
  30. package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
  31. package/.migrations/runtime/meta/_journal.json +97 -0
  32. package/README.md +1584 -0
  33. package/RELEASING.md +89 -0
  34. package/agent/fleet.js +100 -0
  35. package/bin/cli.js +854 -0
  36. package/bin/manifold.js +4 -0
  37. package/boot/flow-revival.js +536 -0
  38. package/budget/index.js +495 -0
  39. package/budget/lib.js +59 -0
  40. package/dashboard/cost.js +528 -0
  41. package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
  42. package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
  43. package/dashboard/dist/index.html +13 -0
  44. package/dashboard/event-log.js +267 -0
  45. package/dashboard/index.html +12 -0
  46. package/dashboard/package-lock.json +2877 -0
  47. package/dashboard/package.json +37 -0
  48. package/dashboard/pipeline-graph.js +131 -0
  49. package/dashboard/postcss.config.js +6 -0
  50. package/dashboard/search.js +183 -0
  51. package/dashboard/server.js +1015 -0
  52. package/dashboard/src/App.tsx +42 -0
  53. package/dashboard/src/api/client.ts +118 -0
  54. package/dashboard/src/api/types.ts +510 -0
  55. package/dashboard/src/api/ws.tsx +112 -0
  56. package/dashboard/src/components/CancelButton.tsx +63 -0
  57. package/dashboard/src/components/ClickToCopy.tsx +68 -0
  58. package/dashboard/src/components/CopyButton.tsx +61 -0
  59. package/dashboard/src/components/DailyCostTrend.tsx +85 -0
  60. package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
  61. package/dashboard/src/components/Funnel.tsx +149 -0
  62. package/dashboard/src/components/GateForm.tsx +158 -0
  63. package/dashboard/src/components/GraphView.tsx +302 -0
  64. package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
  65. package/dashboard/src/components/Histogram.tsx +292 -0
  66. package/dashboard/src/components/Layout.tsx +28 -0
  67. package/dashboard/src/components/NeedsAttention.tsx +98 -0
  68. package/dashboard/src/components/NodeContextCard.tsx +132 -0
  69. package/dashboard/src/components/PathTrail.tsx +35 -0
  70. package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
  71. package/dashboard/src/components/RerunButton.tsx +46 -0
  72. package/dashboard/src/components/RunComparison.tsx +53 -0
  73. package/dashboard/src/components/Sparkline.tsx +94 -0
  74. package/dashboard/src/components/StatStrip.tsx +147 -0
  75. package/dashboard/src/components/StateTree.tsx +84 -0
  76. package/dashboard/src/components/StatusBadge.tsx +18 -0
  77. package/dashboard/src/components/SubflowPathPills.tsx +38 -0
  78. package/dashboard/src/components/SubflowSummary.tsx +24 -0
  79. package/dashboard/src/components/SubflowsCard.tsx +327 -0
  80. package/dashboard/src/components/TodayStrip.tsx +86 -0
  81. package/dashboard/src/components/TranscriptPane.tsx +99 -0
  82. package/dashboard/src/components/TranscriptRow.tsx +69 -0
  83. package/dashboard/src/components/ui/badge.tsx +38 -0
  84. package/dashboard/src/components/ui/button.tsx +28 -0
  85. package/dashboard/src/components/ui/card.tsx +42 -0
  86. package/dashboard/src/components/ui/skeleton.tsx +9 -0
  87. package/dashboard/src/components/ui/table.tsx +62 -0
  88. package/dashboard/src/index.css +26 -0
  89. package/dashboard/src/lib/anomaly.ts +42 -0
  90. package/dashboard/src/lib/format.ts +30 -0
  91. package/dashboard/src/lib/step-history.ts +13 -0
  92. package/dashboard/src/lib/subflow.ts +154 -0
  93. package/dashboard/src/lib/use-click-outside.ts +28 -0
  94. package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
  95. package/dashboard/src/lib/utils.ts +6 -0
  96. package/dashboard/src/main.tsx +10 -0
  97. package/dashboard/src/types/shims.d.ts +4 -0
  98. package/dashboard/src/views/Home.tsx +478 -0
  99. package/dashboard/src/views/PipelineDetail.tsx +1185 -0
  100. package/dashboard/src/views/PipelinesList.tsx +111 -0
  101. package/dashboard/src/views/RunDetail.tsx +248 -0
  102. package/dashboard/src/views/Search.tsx +196 -0
  103. package/dashboard/stats.js +668 -0
  104. package/dashboard/tailwind.config.ts +43 -0
  105. package/dashboard/tree-stats.js +12 -0
  106. package/dashboard/tsconfig.json +25 -0
  107. package/dashboard/views.js +231 -0
  108. package/dashboard/vite.config.ts +34 -0
  109. package/drizzle.config.js +12 -0
  110. package/drizzle.config.ops.js +12 -0
  111. package/elevation.js +116 -0
  112. package/engine/admission-promotion-resume.js +127 -0
  113. package/engine/agent-dispatch.js +64 -0
  114. package/engine/child-resume-trigger.js +315 -0
  115. package/engine/executor-budget.js +87 -0
  116. package/engine/executor-cancel.js +83 -0
  117. package/engine/executor-errors.js +122 -0
  118. package/engine/executor-helpers.js +254 -0
  119. package/engine/executor-nodes.js +292 -0
  120. package/engine/executor-persist.js +212 -0
  121. package/engine/executor-subflow-children.js +275 -0
  122. package/engine/executor.js +1013 -0
  123. package/engine/global-concurrency.js +33 -0
  124. package/engine/loader.js +528 -0
  125. package/engine/needs-resolver.js +229 -0
  126. package/engine/pipelines-watcher.js +133 -0
  127. package/engine/result-replay.js +34 -0
  128. package/engine/revival-messages.js +11 -0
  129. package/engine/spawn-child.js +55 -0
  130. package/engine/wait-for-flow-settlement.js +109 -0
  131. package/events/catalog.js +26 -0
  132. package/events/emit.js +44 -0
  133. package/events/kinds.js +118 -0
  134. package/events/serve-ws.js +74 -0
  135. package/events/stream.js +35 -0
  136. package/events/ws-dialog.js +257 -0
  137. package/events/ws-frame.js +194 -0
  138. package/events/ws-run-events.js +70 -0
  139. package/events.js +32 -0
  140. package/gates/gate-resolver.js +576 -0
  141. package/gates/paused-flows.js +55 -0
  142. package/graphql/columns.js +67 -0
  143. package/graphql/schema.js +333 -0
  144. package/index.js +49 -0
  145. package/mcp/http.js +159 -0
  146. package/mcp/identity.js +94 -0
  147. package/mcp/stdio.js +80 -0
  148. package/mcp/tools.js +245 -0
  149. package/mcp/wire-ops.js +178 -0
  150. package/package.json +51 -0
  151. package/routes/bearer-auth.js +57 -0
  152. package/routes/discuss-audio.js +87 -0
  153. package/routes/discuss-component.js +39 -0
  154. package/routes/discuss-draft.js +159 -0
  155. package/routes/discuss-helpers.js +20 -0
  156. package/routes/discuss.js +848 -0
  157. package/routes/failures.js +104 -0
  158. package/routes/graphql.js +61 -0
  159. package/routes/health.js +54 -0
  160. package/routes/operator-discuss.js +193 -0
  161. package/routes/operator-resume.js +32 -0
  162. package/routes/operator-retry.js +301 -0
  163. package/routes/operator.js +539 -0
  164. package/routes/pipelines.js +118 -0
  165. package/routes/runs.js +1129 -0
  166. package/scripts/ci/import-check.mjs +50 -0
  167. package/scripts/link-core-self.mjs +31 -0
  168. package/scripts/with-guard.sh +24 -0
  169. package/server/app.js +503 -0
  170. package/server/registry.js +53 -0
  171. package/server/serve.js +163 -0
  172. package/state/admission-queue-store.js +325 -0
  173. package/state/attribution-registries.js +102 -0
  174. package/state/batch-writer.js +162 -0
  175. package/state/db-guard.js +63 -0
  176. package/state/db-health.js +27 -0
  177. package/state/db.js +122 -0
  178. package/state/discuss-store.js +299 -0
  179. package/state/failure-store.js +427 -0
  180. package/state/migrate.js +104 -0
  181. package/state/node-cost.js +67 -0
  182. package/state/node-executions.js +775 -0
  183. package/state/node-health-store.js +124 -0
  184. package/state/ops-merge.js +93 -0
  185. package/state/paths.js +146 -0
  186. package/state/pipeline-dirs.js +99 -0
  187. package/state/pipeline-registry.js +449 -0
  188. package/state/run-store.js +697 -0
  189. package/state/schema.js +438 -0
  190. package/state/schema.ops.js +44 -0
  191. package/state/session-index.js +177 -0
  192. package/state/session-keys.js +39 -0
  193. package/state/state-store.js +311 -0
  194. package/state/subflow-state.js +126 -0
  195. package/state/tool-handoff.js +169 -0
  196. package/telemetry/budget-alert.js +353 -0
  197. package/telemetry/cli.js +331 -0
  198. package/telemetry/grammar.js +119 -0
  199. package/telemetry/http-budget.js +132 -0
  200. package/telemetry/http-cost.js +670 -0
  201. package/telemetry/http-helpers.js +134 -0
  202. package/telemetry/http-tree.js +349 -0
  203. package/telemetry/http.js +44 -0
  204. package/telemetry/index.js +307 -0
  205. package/telemetry/ingest.js +134 -0
  206. package/telemetry/labels.js +115 -0
  207. package/telemetry/pricer.js +77 -0
  208. package/telemetry/resolver.js +314 -0
  209. package/telemetry/store.js +147 -0
  210. package/utils/run-patch.js +43 -0
@@ -0,0 +1,539 @@
1
+ import { createRetryFailure } from "./operator-retry.js";
2
+ import { createDiscussHandler } from "./operator-discuss.js";
3
+ import { createStateStore } from "../state/state-store.js";
4
+ import { completeCancellation } from "../engine/executor-cancel.js";
5
+ import { getNodeExecutionsStore } from "../state/node-executions.js";
6
+ import { resumeFlowFromGate } from "./operator-resume.js";
7
+ import { authorizeBearerRequest, sendUnauthorized } from "./bearer-auth.js";
8
+
9
+ // Elevated handlers run with ambient operator scope, so the stamped harness
10
+ // identity in the body is the only per-call check available: refuse unstamped
11
+ // calls here rather than letting them run unauthenticated.
12
+ function elevIdentityMissing(body, fields) {
13
+ return fields.filter((f) => typeof body?.[f] !== "string" || !body[f].trim());
14
+ }
15
+
16
+ function refuseElevIdentity(res, missing) {
17
+ res.statusCode = 400;
18
+ res.setHeader("Content-Type", "application/json");
19
+ res.end(JSON.stringify({ ok: false, code: "missing_identity", error: `${missing.join(", ")} required` }));
20
+ }
21
+
22
+ export function registerOperatorRoutes({
23
+ api,
24
+ store,
25
+ readStore,
26
+ executor,
27
+ gateResolver,
28
+ failureStore,
29
+ pipelinesRef,
30
+ ensurePipelinesLoaded,
31
+ invokeElevated,
32
+ ownerSessionKey,
33
+ runStore,
34
+ admissionQueueStore,
35
+ resumeParentRef,
36
+ // The discuss store owns the pending proposal (discuss_state.pending_json).
37
+ discussStore,
38
+ getBroadcast,
39
+ // Injected gateway adapter, not core state: session reads go through
40
+ // here so core never imports the gateway's session store.
41
+ sessions,
42
+ }) {
43
+ const { retryFailureBranch } = createRetryFailure({
44
+ store, readStore, failureStore, pipelinesRef, executor, runStore, ownerSessionKey,
45
+ admissionQueueStore,
46
+ logger: api.logger,
47
+ api,
48
+ });
49
+
50
+ const { handleDiscuss } = createDiscussHandler({
51
+ api, discussStore, sessions,
52
+ });
53
+
54
+ // ── POST /manifold/gate/resolve ───────────────────────────────────────────
55
+
56
+ api.registerHttpRoute({
57
+ path: "/manifold/gate/resolve",
58
+ auth: "plugin",
59
+ match: "exact",
60
+ handler: async (req, res) => {
61
+ if (req.method !== "POST") {
62
+ res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true;
63
+ }
64
+ const authz = authorizeBearerRequest(req, { logger: api.logger });
65
+ if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
66
+ api.logger.info("manifold gate/resolve: bearer ok");
67
+ let raw = "";
68
+ for await (const chunk of req) raw += chunk;
69
+ let body;
70
+ try { body = raw ? JSON.parse(raw) : {}; }
71
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
72
+ try {
73
+ // recordItemDecision can trigger executor.resume(), which may
74
+ // dispatch a kind:"agent" node (e.g. SETTLE_GAP_ANSWER) — that
75
+ // needs operator.write scope, which this auth:"plugin" route never
76
+ // has ambient. Loop back through invokeElevated to a sibling
77
+ // auth:"gateway" route that does, same pattern as /manifold/start.
78
+ const elev = await invokeElevated("/manifold/_elev/gate-resolve", {
79
+ flowId: body.flowId,
80
+ itemId: body.itemId,
81
+ decision: body.decision,
82
+ source: body.source,
83
+ editedText: body.editedText,
84
+ reason: body.reason,
85
+ session: body.session,
86
+ agentId: body.agentId,
87
+ });
88
+ res.statusCode = elev.status || 500;
89
+ res.setHeader("Content-Type", "application/json");
90
+ res.end(elev.body || JSON.stringify({ ok: false, code: "elevation_no_body" }));
91
+ } catch (err) {
92
+ api.logger.warn(`manifold gate/resolve handler error: ${err?.message ?? err}`);
93
+ res.statusCode = 500;
94
+ res.setHeader("Content-Type", "application/json");
95
+ res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
96
+ }
97
+ return true;
98
+ },
99
+ });
100
+
101
+ // ── POST /manifold/_elev/gate-resolve ─────────────────────────────────────
102
+
103
+ api.registerHttpRoute({
104
+ path: "/manifold/_elev/gate-resolve",
105
+ auth: "gateway",
106
+ match: "exact",
107
+ handler: async (req, res) => {
108
+ if (req.method !== "POST") {
109
+ res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true;
110
+ }
111
+ let raw = "";
112
+ for await (const chunk of req) raw += chunk;
113
+ let body;
114
+ try { body = raw ? JSON.parse(raw) : {}; }
115
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
116
+ const missingGateIdentity = elevIdentityMissing(body, ["flowId", "session", "agentId"]);
117
+ if (missingGateIdentity.length > 0) { refuseElevIdentity(res, missingGateIdentity); return true; }
118
+ try {
119
+ const result = await gateResolver.recordItemDecision({
120
+ flowId: body.flowId,
121
+ itemId: body.itemId,
122
+ decision: body.decision,
123
+ source: body.source,
124
+ editedText: body.editedText,
125
+ reason: body.reason,
126
+ });
127
+ const status = result.ok ? 200 : (result.code === "flow_not_found" ? 404 : 409);
128
+ res.statusCode = status;
129
+ res.setHeader("Content-Type", "application/json");
130
+ res.end(JSON.stringify(result));
131
+ } catch (err) {
132
+ api.logger.warn(`manifold gate-resolve (elev) handler error: ${err?.message ?? err}`);
133
+ res.statusCode = 500;
134
+ res.setHeader("Content-Type", "application/json");
135
+ res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
136
+ }
137
+ return true;
138
+ },
139
+ });
140
+
141
+ // ── POST /manifold/cancel ─────────────────────────────────────────────────
142
+
143
+ api.registerHttpRoute({
144
+ path: "/manifold/cancel",
145
+ auth: "plugin",
146
+ match: "exact",
147
+ handler: async (req, res) => {
148
+ if (req.method !== "POST") {
149
+ res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true;
150
+ }
151
+ const authz = authorizeBearerRequest(req, { logger: api.logger });
152
+ if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
153
+ let raw = "";
154
+ for await (const chunk of req) raw += chunk;
155
+ let body;
156
+ try { body = raw ? JSON.parse(raw) : {}; }
157
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
158
+ const flowId = typeof body.flowId === "string" ? body.flowId.trim() : "";
159
+ const reason = typeof body.reason === "string" && body.reason.trim() ? body.reason.trim() : null;
160
+ if (!flowId) {
161
+ res.statusCode = 400;
162
+ res.setHeader("Content-Type", "application/json");
163
+ res.end(JSON.stringify({ ok: false, error: "flowId required" }));
164
+ return true;
165
+ }
166
+ try {
167
+ let result = await executor.cancel(flowId, reason);
168
+ if (!result?.cancelled && result?.reason === "Flow not found.") {
169
+ const sqlFlow = await readStore?.get(flowId);
170
+ if (sqlFlow && !["succeeded", "failed", "cancelled", "skipped"].includes(sqlFlow.status)) {
171
+ // Use a store bound to the flow's actual ownerKey. Legacy flows were
172
+ // created under "graph-flow-owner"; the default store is bound to
173
+ // "manifold-owner". Mismatched ownerKey → setRunning returns applied=false.
174
+ //
175
+ // Must carry the same dependency set as the primary `store` and
176
+ // index.js's own createCancelStore factory (nodeExecutionsStore,
177
+ // resumeParentRef, pipelinesRef, failureStore, admissionQueueStore):
178
+ // state-store.js's cancel()/finish()/fail() silently no-op
179
+ // triggerAdmissionRelease without admissionQueueStore and
180
+ // triggerParentResume without nodeExecutionsStore/resumeParentRef —
181
+ // a fallback missing either leaks the flow's start/child-dispatch
182
+ // admission slot permanently and leaves a waiting parent on
183
+ // "children-pending" forever.
184
+ const flowOwnerKey = sqlFlow.ownerKey ?? ownerSessionKey;
185
+ const cancelStore =
186
+ flowOwnerKey === ownerSessionKey
187
+ ? store
188
+ : createStateStore(api, flowOwnerKey, {
189
+ runStore, nodeExecutionsStore: getNodeExecutionsStore(), resumeParentRef,
190
+ pipelinesRef, failureStore, admissionQueueStore, logger: api.logger,
191
+ });
192
+ // Pre-load into the in-process Map so the runtime's cancel() can find it.
193
+ const setResult = await cancelStore.setRunning({
194
+ flowId,
195
+ expectedRevision: sqlFlow.revision,
196
+ lastStep: sqlFlow.lastStep,
197
+ stateJson: sqlFlow.stateJson ?? {},
198
+ });
199
+ if (setResult.applied) {
200
+ // completeCancellation stamps branches + writes stateJson to DB.
201
+ // Pass revision+1 because setRunning just incremented it.
202
+ result = await completeCancellation({
203
+ store: cancelStore,
204
+ flowId,
205
+ stateJson: sqlFlow.stateJson ?? {},
206
+ lastStep: sqlFlow.lastStep,
207
+ revision: sqlFlow.revision + 1,
208
+ cfg: api.config,
209
+ runStore,
210
+ nodeExecutionsStore: getNodeExecutionsStore(),
211
+ reason,
212
+ });
213
+ }
214
+ }
215
+ }
216
+ if (!result?.cancelled) {
217
+ res.statusCode = 409;
218
+ res.setHeader("Content-Type", "application/json");
219
+ res.end(JSON.stringify({ ok: false, code: "cancel_not_applied", reason: result?.reason ?? "unknown" }));
220
+ return true;
221
+ }
222
+ api.logger.info(`manifold cancel: flow=${flowId.slice(0, 8)} via=${authz.via}`);
223
+ res.statusCode = 200;
224
+ res.setHeader("Content-Type", "application/json");
225
+ res.end(JSON.stringify({ ok: true, cancelled: true }));
226
+ } catch (err) {
227
+ api.logger.warn(`manifold cancel handler error: ${err?.message ?? err}`);
228
+ res.statusCode = 500;
229
+ res.setHeader("Content-Type", "application/json");
230
+ res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
231
+ }
232
+ return true;
233
+ },
234
+ });
235
+
236
+ // ── POST /manifold/start ──────────────────────────────────────────────────
237
+
238
+ api.registerHttpRoute({
239
+ path: "/manifold/start",
240
+ auth: "plugin",
241
+ match: "exact",
242
+ handler: async (req, res) => {
243
+ if (req.method !== "POST") {
244
+ res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true;
245
+ }
246
+ const authz = authorizeBearerRequest(req, { logger: api.logger });
247
+ if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
248
+ let raw = "";
249
+ for await (const chunk of req) raw += chunk;
250
+ let body;
251
+ try { body = raw ? JSON.parse(raw) : {}; }
252
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
253
+ const pipelineId = typeof body.pipelineId === "string" ? body.pipelineId.trim() : "";
254
+ if (!pipelineId) {
255
+ res.statusCode = 400;
256
+ res.setHeader("Content-Type", "application/json");
257
+ res.end(JSON.stringify({ ok: false, error: "pipelineId required" }));
258
+ return true;
259
+ }
260
+ try {
261
+ await ensurePipelinesLoaded?.();
262
+ if (!pipelinesRef.value.has(pipelineId)) {
263
+ res.statusCode = 404;
264
+ res.setHeader("Content-Type", "application/json");
265
+ res.end(JSON.stringify({ ok: false, error: `unknown pipeline: ${pipelineId}`, known: [...pipelinesRef.value.keys()] }));
266
+ return true;
267
+ }
268
+ const args = (body.args && typeof body.args === "object") ? body.args : {};
269
+ const elev = await invokeElevated("/manifold/_elev/start", {
270
+ pipelineId, args, via: authz.via,
271
+ session: body.session, agentId: body.agentId,
272
+ });
273
+ res.statusCode = elev.status || 500;
274
+ res.setHeader("Content-Type", "application/json");
275
+ res.end(elev.body || JSON.stringify({ ok: false, code: "elevation_no_body" }));
276
+ } catch (err) {
277
+ api.logger.warn(`manifold start handler error: ${err?.message ?? err}`);
278
+ res.statusCode = 500;
279
+ res.setHeader("Content-Type", "application/json");
280
+ res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
281
+ }
282
+ return true;
283
+ },
284
+ });
285
+
286
+ // ── POST /manifold/_elev/start ────────────────────────────────────────────
287
+
288
+ api.registerHttpRoute({
289
+ path: "/manifold/_elev/start",
290
+ auth: "gateway",
291
+ match: "exact",
292
+ handler: async (req, res) => {
293
+ if (req.method !== "POST") {
294
+ res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true;
295
+ }
296
+ let raw = "";
297
+ for await (const chunk of req) raw += chunk;
298
+ let body;
299
+ try { body = raw ? JSON.parse(raw) : {}; }
300
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
301
+ const pipelineId = typeof body.pipelineId === "string" ? body.pipelineId.trim() : "";
302
+ const args = (body.args && typeof body.args === "object") ? body.args : {};
303
+ const via = typeof body.via === "string" ? body.via : "elev";
304
+ const parentSessionKey = typeof body.parentSessionKey === "string" && body.parentSessionKey.trim()
305
+ ? body.parentSessionKey.trim() : null;
306
+ if (!pipelineId) {
307
+ res.statusCode = 400;
308
+ res.setHeader("Content-Type", "application/json");
309
+ res.end(JSON.stringify({ ok: false, error: "pipelineId required" }));
310
+ return true;
311
+ }
312
+ const missingStartIdentity = elevIdentityMissing(body, ["session", "agentId"]);
313
+ if (missingStartIdentity.length > 0) { refuseElevIdentity(res, missingStartIdentity); return true; }
314
+ try {
315
+ await ensurePipelinesLoaded?.();
316
+ if (!pipelinesRef.value.has(pipelineId)) {
317
+ res.statusCode = 404;
318
+ res.setHeader("Content-Type", "application/json");
319
+ res.end(JSON.stringify({ ok: false, error: `unknown pipeline: ${pipelineId}`, known: [...pipelinesRef.value.keys()] }));
320
+ return true;
321
+ }
322
+ const triggeredBy = {
323
+ kind: via.includes("app") ? "operator_http" : via.includes("cron") ? "cron" : via.includes("cli") ? "cli" : "operator_http",
324
+ label: `via ${via}`,
325
+ ts: Date.now(),
326
+ };
327
+ const record = await executor.start(pipelineId, args, triggeredBy);
328
+ try {
329
+ if (parentSessionKey) {
330
+ await api.runtime?.lineage?.recordTrigger?.({
331
+ flowId: record.flowId,
332
+ runId: record.stateJson?.runId ?? null,
333
+ pipelineId,
334
+ parentSessionKey,
335
+ });
336
+ } else {
337
+ const source = triggeredBy.kind === "cli" ? "cli" : triggeredBy.kind === "operator_http" ? "app" : "operator";
338
+ const synthSession = `agent:operator:${source}:${record.flowId}`;
339
+ await api.runtime?.lineage?.recordTrigger?.({
340
+ flowId: record.flowId,
341
+ runId: record.stateJson?.runId ?? null,
342
+ pipelineId,
343
+ parentSessionKey: synthSession,
344
+ rootKind: "explicit",
345
+ rootLabel: pipelineId,
346
+ });
347
+ }
348
+ } catch (err) {
349
+ api.logger.warn(`manifold start: lineage edge skipped: ${err?.message ?? err}`);
350
+ try {
351
+ const freshForLineage = await store.get(record.flowId);
352
+ if (freshForLineage) {
353
+ await store.setRunning({
354
+ flowId: record.flowId,
355
+ expectedRevision: freshForLineage.revision,
356
+ lastStep: freshForLineage.lastStep,
357
+ stateJson: { ...freshForLineage.stateJson, _lineageError: { ts: Date.now(), message: err?.message ?? String(err) } },
358
+ });
359
+ }
360
+ } catch { /* non-fatal; lineage attribution already lost */ }
361
+ }
362
+ setImmediate(() => {
363
+ executor.runFlow(record.flowId, "fresh").catch((err) => {
364
+ api.logger.warn(`manifold start: runFlow [${record.flowId.slice(0, 8)}] error: ${err?.message ?? err}`);
365
+ });
366
+ });
367
+ if (triggeredBy.kind === "cron") {
368
+ try { getBroadcast?.()?.("cron.job.fired", { flowId: record.flowId, pipelineId, firedAt: Date.now() }); } catch {}
369
+ }
370
+ api.logger.info(`manifold start: pipeline=${pipelineId} flow=${record.flowId.slice(0, 8)} via=${via} (elev)`);
371
+ res.statusCode = 200;
372
+ res.setHeader("Content-Type", "application/json");
373
+ res.end(JSON.stringify({ ok: true, pipelineId, flowId: record.flowId }));
374
+ } catch (err) {
375
+ api.logger.warn(`manifold start (elev) handler error: ${err?.message ?? err}`);
376
+ res.statusCode = 500;
377
+ res.setHeader("Content-Type", "application/json");
378
+ res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
379
+ }
380
+ return true;
381
+ },
382
+ });
383
+
384
+ // ── POST /manifold/resume-from-gate ──────────────────────────────────────
385
+
386
+ api.registerHttpRoute({
387
+ path: "/manifold/resume-from-gate",
388
+ auth: "gateway",
389
+ match: "exact",
390
+ handler: async (req, res) => {
391
+ if (req.method !== "POST") { res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true; }
392
+ let raw = "";
393
+ for await (const chunk of req) raw += chunk;
394
+ let body;
395
+ try { body = raw ? JSON.parse(raw) : {}; }
396
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
397
+ const flowId = typeof body.flowId === "string" ? body.flowId : "";
398
+ if (!flowId) {
399
+ res.statusCode = 400; res.setHeader("Content-Type", "application/json");
400
+ res.end(JSON.stringify({ ok: false, error: "flowId required" })); return true;
401
+ }
402
+ // Core resume lives in routes/operator-resume.js (shared with the
403
+ // budget decide route, which calls it in-process); this stays a thin
404
+ // validating wrapper mapping outcomes to status codes.
405
+ const result = await resumeFlowFromGate({
406
+ store, readStore, admissionQueueStore, runStore, executor,
407
+ logger: api.logger, api, flowId, nodeId: body.nodeId, decision: body.decision,
408
+ });
409
+ if (!result.ok && result.code === "flow_not_found") {
410
+ res.statusCode = 404; res.setHeader("Content-Type", "application/json");
411
+ res.end(JSON.stringify({ ok: false, error: "flow not found" })); return true;
412
+ }
413
+ if (!result.ok) {
414
+ res.statusCode = 409; res.setHeader("Content-Type", "application/json");
415
+ res.end(JSON.stringify({ ok: false, error: result.code })); return true;
416
+ }
417
+ if (!result.queued) {
418
+ setImmediate(() => {
419
+ executor.resume(flowId).catch((err) => {
420
+ api.logger.warn(`manifold resume-from-gate: runFlow [${flowId.slice(0, 8)}] error: ${err?.message ?? err}`);
421
+ });
422
+ });
423
+ }
424
+ api.logger.info(`manifold: resume-from-gate flow=${flowId.slice(0, 8)} node=${body.nodeId ?? "?"} decision=${body.decision ?? "?"}${result.queued ? " (queued for admission)" : ""}`);
425
+ res.statusCode = 200; res.setHeader("Content-Type", "application/json");
426
+ res.end(JSON.stringify({ ok: true, flowId, queued: result.queued })); return true;
427
+ },
428
+ });
429
+
430
+ // ── POST /manifold/_elev/rerun ────────────────────────────────────────────
431
+
432
+ api.registerHttpRoute({
433
+ path: "/manifold/_elev/rerun",
434
+ auth: "gateway",
435
+ match: "exact",
436
+ handler: async (req, res) => {
437
+ if (req.method !== "POST") {
438
+ res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true;
439
+ }
440
+ let raw = "";
441
+ for await (const chunk of req) raw += chunk;
442
+ let body;
443
+ try { body = raw ? JSON.parse(raw) : {}; }
444
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
445
+ const sourceFlowId = typeof body.sourceFlowId === "string" ? body.sourceFlowId.trim() : "";
446
+ if (!sourceFlowId) {
447
+ res.statusCode = 400; res.setHeader("Content-Type", "application/json");
448
+ res.end(JSON.stringify({ ok: false, error: "sourceFlowId required" })); return true;
449
+ }
450
+ const argsOverride = (body && "args" in body) ? body.args : undefined;
451
+ if (argsOverride !== undefined && (argsOverride === null || typeof argsOverride !== "object" || Array.isArray(argsOverride))) {
452
+ res.statusCode = 400; res.setHeader("Content-Type", "application/json");
453
+ res.end(JSON.stringify({ ok: false, error: "args must be a JSON object" })); return true;
454
+ }
455
+ const missingRerunIdentity = elevIdentityMissing(body, ["session", "agentId"]);
456
+ if (missingRerunIdentity.length > 0) { refuseElevIdentity(res, missingRerunIdentity); return true; }
457
+ const sourceFlow = await readStore.get(sourceFlowId);
458
+ if (!sourceFlow) {
459
+ res.statusCode = 404; res.setHeader("Content-Type", "application/json");
460
+ res.end(JSON.stringify({ ok: false, error: `source flow not found: ${sourceFlowId}` })); return true;
461
+ }
462
+ let record;
463
+ try {
464
+ record = executor.rerunFromSource(sourceFlow, argsOverride);
465
+ } catch (err) {
466
+ const msg = err?.message ?? String(err);
467
+ if (msg.includes("active flow")) { res.statusCode = 409; res.setHeader("Content-Type", "application/json"); res.end(JSON.stringify({ ok: false, error: msg })); return true; }
468
+ if (msg.includes("not found")) { res.statusCode = 404; res.setHeader("Content-Type", "application/json"); res.end(JSON.stringify({ ok: false, error: msg })); return true; }
469
+ api.logger.warn(`manifold rerun (elev) handler error: ${msg}`);
470
+ res.statusCode = 500; res.setHeader("Content-Type", "application/json");
471
+ res.end(JSON.stringify({ ok: false, code: "internal", error: msg })); return true;
472
+ }
473
+ setImmediate(() => {
474
+ executor.runFlow(record.flowId, "fresh").catch((err) => {
475
+ api.logger.warn(`manifold rerun (elev): runFlow [${record.flowId.slice(0, 8)}] error: ${err?.message ?? err}`);
476
+ });
477
+ });
478
+ api.logger.info(`manifold rerun (elev): source=${sourceFlow.flowId.slice(0, 8)} → new=${record.flowId.slice(0, 8)} pipeline=${record.stateJson?.pipelineId ?? "?"}`);
479
+ res.statusCode = 200; res.setHeader("Content-Type", "application/json");
480
+ res.end(JSON.stringify({ ok: true, flowId: record.flowId, pipelineId: record.stateJson?.pipelineId ?? null, rerunOf: sourceFlow.flowId }));
481
+ return true;
482
+ },
483
+ });
484
+
485
+ // ── POST /manifold/_elev/retry-failure ────────────────────────────────────
486
+
487
+ api.registerHttpRoute({
488
+ path: "/manifold/_elev/retry-failure",
489
+ auth: "gateway",
490
+ match: "exact",
491
+ handler: async (req, res) => {
492
+ if (req.method !== "POST") {
493
+ res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true;
494
+ }
495
+ let raw = "";
496
+ for await (const chunk of req) raw += chunk;
497
+ let body;
498
+ try { body = raw ? JSON.parse(raw) : {}; }
499
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
500
+ const id = typeof body.id === "string" ? body.id.trim() : "";
501
+ if (!id) {
502
+ res.statusCode = 400; res.setHeader("Content-Type", "application/json");
503
+ res.end(JSON.stringify({ ok: false, error: "id required" })); return true;
504
+ }
505
+ const missingRetryIdentity = elevIdentityMissing(body, ["session", "agentId"]);
506
+ if (missingRetryIdentity.length > 0) { refuseElevIdentity(res, missingRetryIdentity); return true; }
507
+ const via = typeof body.via === "string" ? body.via : "elev";
508
+ const result = await retryFailureBranch({ id, via });
509
+ res.statusCode = result.code === "ok" ? 200 : result.code === "not_found" ? 404 : result.code === "conflict" ? 409 : 500;
510
+ res.setHeader("Content-Type", "application/json");
511
+ res.end(JSON.stringify(result));
512
+ return true;
513
+ },
514
+ });
515
+
516
+ // ── POST /manifold/_elev/discuss ──────────────────────────────────────────
517
+
518
+ api.registerHttpRoute({
519
+ path: "/manifold/_elev/discuss",
520
+ auth: "gateway",
521
+ match: "exact",
522
+ handler: async (req, res) => {
523
+ if (req.method !== "POST") {
524
+ res.statusCode = 405; res.setHeader("Allow", "POST"); res.end("method not allowed"); return true;
525
+ }
526
+ let raw = "";
527
+ for await (const chunk of req) raw += chunk;
528
+ let body;
529
+ try { body = raw ? JSON.parse(raw) : {}; }
530
+ catch { res.statusCode = 400; res.end("invalid json"); return true; }
531
+ const { status, body: responseBody } = await handleDiscuss(body);
532
+ res.statusCode = status;
533
+ res.setHeader("Content-Type", "application/json");
534
+ res.end(JSON.stringify(responseBody));
535
+ return true;
536
+ },
537
+ });
538
+
539
+ }
@@ -0,0 +1,118 @@
1
+ import { authorizeBearerRequest, sendUnauthorized } from "./bearer-auth.js";
2
+
3
+ export function registerPipelineRoutes({ api, pipelinesRef, readStore, ensurePipelinesLoaded }) {
4
+ // GET /manifold/pipelines — catalog of pipeline definitions joined with
5
+ // run-history vitals. Mirrors the dashboard's /api/pipelines but trimmed
6
+ // to what a pipelines list view needs at a glance (lastRunAt,
7
+ // todayCount, succeeded/failed today, success-rate over a recent window).
8
+ // No cost (slow JSONL walking lives in the per-pipeline detail route).
9
+ // Historical pipelines (no def on disk but runs in db) are appended last.
10
+ api.registerHttpRoute({
11
+ path: "/manifold/pipelines",
12
+ auth: "plugin",
13
+ match: "exact",
14
+ handler: async (req, res) => {
15
+ if (req.method !== "GET") {
16
+ res.statusCode = 405;
17
+ res.setHeader("Allow", "GET");
18
+ res.end("method not allowed");
19
+ return true;
20
+ }
21
+ const authz = authorizeBearerRequest(req, { logger: api.logger });
22
+ if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
23
+
24
+ try {
25
+ await ensurePipelinesLoaded?.();
26
+ const nowMs = Date.now();
27
+ // Caller-supplied "today" boundary so clients can bucket by
28
+ // their local-day midnight (the operator thinks in their own TZ).
29
+ // Falls back to UTC midnight to keep behaviour identical for
30
+ // any caller that doesn't send it (parity with the dashboard).
31
+ const url = new URL(req.url, `http://${req.headers.host || "localhost"}`);
32
+ const todayParam = url.searchParams.get("todayStartMs");
33
+ const parsedToday = todayParam ? Number(todayParam) : null;
34
+ const todayStartMs = Number.isFinite(parsedToday) && parsedToday > 0
35
+ ? parsedToday
36
+ : (() => { const d = new Date(nowMs); return new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime(); })();
37
+ const sevenDaysMs = nowMs - 7 * 86_400_000;
38
+
39
+ // SQL GROUP BY + window function replace the JS aggregation loop —
40
+ // two queries instead of iterating 7K rows in JS.
41
+ const agg = await readStore.listAllPipelineStats({ todayStartMs, sevenDaysMs });
42
+
43
+ // _synthetic: true — a subflow node's .flow registered as its own
44
+ // addressable pipeline (`${pipelineId}::${nodeId}`) so childFlows
45
+ // can start a real child flow_run against it (see engine/loader.js).
46
+ // Purely an internal dispatch target, never a pipeline an operator
47
+ // runs or sees on its own — must not appear in the catalog.
48
+ const implemented = [...pipelinesRef.value.values()].filter((p) => !p._synthetic).map((p) => {
49
+ const a = agg.get(p.id) ?? null;
50
+ const nodeCount = Object.keys(p.nodes ?? {}).length;
51
+ return {
52
+ id: p.id,
53
+ goal: p.goal ?? null,
54
+ file: p._filepath ? p._filepath.replace(/^.*\//, "") : null,
55
+ nodeCount,
56
+ historical: false,
57
+ cronJobId: typeof p.cronJobId === "string" ? p.cronJobId : null,
58
+ lastRunAtMs: a?.lastRunAtMs ?? null,
59
+ lastRunStatus: a?.lastRunStatus ?? null,
60
+ todayTotal: a?.todayTotal ?? 0,
61
+ todaySucceeded: a?.todaySucceeded ?? 0,
62
+ todayFailed: a?.todayFailed ?? 0,
63
+ todayActive: a?.todayActive ?? 0,
64
+ last7dTotal: a?.last7dTotal ?? 0,
65
+ last7dSucceeded: a?.last7dSucceeded ?? 0,
66
+ last7dFailed: a?.last7dFailed ?? 0,
67
+ totalRuns: a?.totalRuns ?? 0,
68
+ };
69
+ });
70
+ const implementedIds = new Set(implemented.map((p) => p.id));
71
+ const historical = [];
72
+ for (const [pid, a] of agg.entries()) {
73
+ if (implementedIds.has(pid)) continue;
74
+ historical.push({
75
+ id: pid,
76
+ goal: null,
77
+ file: null,
78
+ nodeCount: 0,
79
+ historical: true,
80
+ cronJobId: null,
81
+ lastRunAtMs: a.lastRunAtMs,
82
+ lastRunStatus: a.lastRunStatus,
83
+ todayTotal: a.todayTotal,
84
+ todaySucceeded: a.todaySucceeded,
85
+ todayFailed: a.todayFailed,
86
+ todayActive: a.todayActive,
87
+ last7dTotal: a.last7dTotal,
88
+ last7dSucceeded: a.last7dSucceeded,
89
+ last7dFailed: a.last7dFailed,
90
+ totalRuns: a.totalRuns,
91
+ });
92
+ }
93
+
94
+ // Sort by recent activity: implemented-with-runs first (by
95
+ // lastRunAt desc), then implemented-no-runs (alpha), then
96
+ // historical last (lastRunAt desc).
97
+ const sortKey = (p) => p.lastRunAtMs ?? 0;
98
+ const live = implemented.filter((p) => p.lastRunAtMs).sort((a, b) => sortKey(b) - sortKey(a));
99
+ const idle = implemented.filter((p) => !p.lastRunAtMs).sort((a, b) => a.id.localeCompare(b.id));
100
+ historical.sort((a, b) => sortKey(b) - sortKey(a));
101
+
102
+ res.statusCode = 200;
103
+ res.setHeader("Content-Type", "application/json");
104
+ res.end(JSON.stringify({
105
+ pipelines: [...live, ...idle, ...historical],
106
+ todayStartMs,
107
+ generatedAtMs: nowMs,
108
+ }));
109
+ } catch (err) {
110
+ api.logger.warn(`manifold pipelines handler error: ${err?.message ?? err}`);
111
+ res.statusCode = 500;
112
+ res.setHeader("Content-Type", "application/json");
113
+ res.end(JSON.stringify({ ok: false, code: "internal", error: err?.message ?? String(err) }));
114
+ }
115
+ return true;
116
+ },
117
+ });
118
+ }