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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/README.md +2 -1583
  2. package/package.json +4 -49
  3. package/.migrations/ops/0000_futuristic_karma.sql +0 -111
  4. package/.migrations/ops/0001_low_maximus.sql +0 -7
  5. package/.migrations/ops/meta/0000_snapshot.json +0 -763
  6. package/.migrations/ops/meta/0001_snapshot.json +0 -173
  7. package/.migrations/ops/meta/_journal.json +0 -20
  8. package/.migrations/runtime/0000_real_boomer.sql +0 -180
  9. package/.migrations/runtime/0001_hot_dakota_north.sql +0 -2
  10. package/.migrations/runtime/0002_breezy_vertigo.sql +0 -8
  11. package/.migrations/runtime/0003_dashing_jack_flag.sql +0 -5
  12. package/.migrations/runtime/0004_panoramic_ego.sql +0 -1
  13. package/.migrations/runtime/0005_tidy_ben_grimm.sql +0 -10
  14. package/.migrations/runtime/0006_wet_nightmare.sql +0 -57
  15. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +0 -8
  16. package/.migrations/runtime/0008_wealthy_thundra.sql +0 -301
  17. package/.migrations/runtime/0009_lying_shocker.sql +0 -96
  18. package/.migrations/runtime/0010_solid_phantom_reporter.sql +0 -160
  19. package/.migrations/runtime/0011_windy_demogoblin.sql +0 -175
  20. package/.migrations/runtime/0012_restore_harness_fks.sql +0 -103
  21. package/.migrations/runtime/meta/0000_snapshot.json +0 -1181
  22. package/.migrations/runtime/meta/0001_snapshot.json +0 -1209
  23. package/.migrations/runtime/meta/0002_snapshot.json +0 -1261
  24. package/.migrations/runtime/meta/0003_snapshot.json +0 -1292
  25. package/.migrations/runtime/meta/0004_snapshot.json +0 -1240
  26. package/.migrations/runtime/meta/0006_snapshot.json +0 -1310
  27. package/.migrations/runtime/meta/0007_snapshot.json +0 -1362
  28. package/.migrations/runtime/meta/0008_snapshot.json +0 -1742
  29. package/.migrations/runtime/meta/0009_snapshot.json +0 -2413
  30. package/.migrations/runtime/meta/0010_snapshot.json +0 -2562
  31. package/.migrations/runtime/meta/0011_snapshot.json +0 -2812
  32. package/.migrations/runtime/meta/0012_snapshot.json +0 -2812
  33. package/.migrations/runtime/meta/_journal.json +0 -97
  34. package/RELEASING.md +0 -89
  35. package/agent/fleet.js +0 -100
  36. package/bin/cli.js +0 -854
  37. package/bin/manifold.js +0 -4
  38. package/boot/flow-revival.js +0 -536
  39. package/budget/index.js +0 -495
  40. package/budget/lib.js +0 -59
  41. package/dashboard/cost.js +0 -528
  42. package/dashboard/dist/assets/index-CUfp63sj.css +0 -1
  43. package/dashboard/dist/assets/index-Qy9JMpma.js +0 -371
  44. package/dashboard/dist/index.html +0 -13
  45. package/dashboard/event-log.js +0 -267
  46. package/dashboard/index.html +0 -12
  47. package/dashboard/package-lock.json +0 -2877
  48. package/dashboard/package.json +0 -37
  49. package/dashboard/pipeline-graph.js +0 -131
  50. package/dashboard/postcss.config.js +0 -6
  51. package/dashboard/search.js +0 -183
  52. package/dashboard/server.js +0 -1015
  53. package/dashboard/src/App.tsx +0 -42
  54. package/dashboard/src/api/client.ts +0 -118
  55. package/dashboard/src/api/types.ts +0 -510
  56. package/dashboard/src/api/ws.tsx +0 -112
  57. package/dashboard/src/components/CancelButton.tsx +0 -63
  58. package/dashboard/src/components/ClickToCopy.tsx +0 -68
  59. package/dashboard/src/components/CopyButton.tsx +0 -61
  60. package/dashboard/src/components/DailyCostTrend.tsx +0 -85
  61. package/dashboard/src/components/DailyTimeTrend.tsx +0 -94
  62. package/dashboard/src/components/Funnel.tsx +0 -149
  63. package/dashboard/src/components/GateForm.tsx +0 -158
  64. package/dashboard/src/components/GraphView.tsx +0 -302
  65. package/dashboard/src/components/GroupPathsSummary.tsx +0 -47
  66. package/dashboard/src/components/Histogram.tsx +0 -292
  67. package/dashboard/src/components/Layout.tsx +0 -28
  68. package/dashboard/src/components/NeedsAttention.tsx +0 -98
  69. package/dashboard/src/components/NodeContextCard.tsx +0 -132
  70. package/dashboard/src/components/PathTrail.tsx +0 -35
  71. package/dashboard/src/components/PipelineCostBreakdown.tsx +0 -277
  72. package/dashboard/src/components/RerunButton.tsx +0 -46
  73. package/dashboard/src/components/RunComparison.tsx +0 -53
  74. package/dashboard/src/components/Sparkline.tsx +0 -94
  75. package/dashboard/src/components/StatStrip.tsx +0 -147
  76. package/dashboard/src/components/StateTree.tsx +0 -84
  77. package/dashboard/src/components/StatusBadge.tsx +0 -18
  78. package/dashboard/src/components/SubflowPathPills.tsx +0 -38
  79. package/dashboard/src/components/SubflowSummary.tsx +0 -24
  80. package/dashboard/src/components/SubflowsCard.tsx +0 -327
  81. package/dashboard/src/components/TodayStrip.tsx +0 -86
  82. package/dashboard/src/components/TranscriptPane.tsx +0 -99
  83. package/dashboard/src/components/TranscriptRow.tsx +0 -69
  84. package/dashboard/src/components/ui/badge.tsx +0 -38
  85. package/dashboard/src/components/ui/button.tsx +0 -28
  86. package/dashboard/src/components/ui/card.tsx +0 -42
  87. package/dashboard/src/components/ui/skeleton.tsx +0 -9
  88. package/dashboard/src/components/ui/table.tsx +0 -62
  89. package/dashboard/src/index.css +0 -26
  90. package/dashboard/src/lib/anomaly.ts +0 -42
  91. package/dashboard/src/lib/format.ts +0 -30
  92. package/dashboard/src/lib/step-history.ts +0 -13
  93. package/dashboard/src/lib/subflow.ts +0 -154
  94. package/dashboard/src/lib/use-click-outside.ts +0 -28
  95. package/dashboard/src/lib/use-ignored-clusters.ts +0 -67
  96. package/dashboard/src/lib/utils.ts +0 -6
  97. package/dashboard/src/main.tsx +0 -10
  98. package/dashboard/src/types/shims.d.ts +0 -4
  99. package/dashboard/src/views/Home.tsx +0 -478
  100. package/dashboard/src/views/PipelineDetail.tsx +0 -1185
  101. package/dashboard/src/views/PipelinesList.tsx +0 -111
  102. package/dashboard/src/views/RunDetail.tsx +0 -248
  103. package/dashboard/src/views/Search.tsx +0 -196
  104. package/dashboard/stats.js +0 -668
  105. package/dashboard/tailwind.config.ts +0 -43
  106. package/dashboard/tree-stats.js +0 -12
  107. package/dashboard/tsconfig.json +0 -25
  108. package/dashboard/views.js +0 -231
  109. package/dashboard/vite.config.ts +0 -34
  110. package/drizzle.config.js +0 -12
  111. package/drizzle.config.ops.js +0 -12
  112. package/elevation.js +0 -116
  113. package/engine/admission-promotion-resume.js +0 -127
  114. package/engine/agent-dispatch.js +0 -64
  115. package/engine/child-resume-trigger.js +0 -315
  116. package/engine/executor-budget.js +0 -87
  117. package/engine/executor-cancel.js +0 -83
  118. package/engine/executor-errors.js +0 -122
  119. package/engine/executor-helpers.js +0 -254
  120. package/engine/executor-nodes.js +0 -292
  121. package/engine/executor-persist.js +0 -212
  122. package/engine/executor-subflow-children.js +0 -275
  123. package/engine/executor.js +0 -1013
  124. package/engine/global-concurrency.js +0 -33
  125. package/engine/loader.js +0 -528
  126. package/engine/needs-resolver.js +0 -229
  127. package/engine/pipelines-watcher.js +0 -133
  128. package/engine/result-replay.js +0 -34
  129. package/engine/revival-messages.js +0 -11
  130. package/engine/spawn-child.js +0 -55
  131. package/engine/wait-for-flow-settlement.js +0 -109
  132. package/events/catalog.js +0 -26
  133. package/events/emit.js +0 -44
  134. package/events/kinds.js +0 -118
  135. package/events/serve-ws.js +0 -74
  136. package/events/stream.js +0 -35
  137. package/events/ws-dialog.js +0 -257
  138. package/events/ws-frame.js +0 -194
  139. package/events/ws-run-events.js +0 -70
  140. package/events.js +0 -32
  141. package/gates/gate-resolver.js +0 -576
  142. package/gates/paused-flows.js +0 -55
  143. package/graphql/columns.js +0 -67
  144. package/graphql/schema.js +0 -333
  145. package/index.js +0 -49
  146. package/mcp/http.js +0 -159
  147. package/mcp/identity.js +0 -94
  148. package/mcp/stdio.js +0 -80
  149. package/mcp/tools.js +0 -245
  150. package/mcp/wire-ops.js +0 -178
  151. package/routes/bearer-auth.js +0 -57
  152. package/routes/discuss-audio.js +0 -87
  153. package/routes/discuss-component.js +0 -39
  154. package/routes/discuss-draft.js +0 -159
  155. package/routes/discuss-helpers.js +0 -20
  156. package/routes/discuss.js +0 -848
  157. package/routes/failures.js +0 -104
  158. package/routes/graphql.js +0 -61
  159. package/routes/health.js +0 -54
  160. package/routes/operator-discuss.js +0 -193
  161. package/routes/operator-resume.js +0 -32
  162. package/routes/operator-retry.js +0 -301
  163. package/routes/operator.js +0 -539
  164. package/routes/pipelines.js +0 -118
  165. package/routes/runs.js +0 -1129
  166. package/scripts/ci/import-check.mjs +0 -50
  167. package/scripts/link-core-self.mjs +0 -31
  168. package/scripts/with-guard.sh +0 -24
  169. package/server/app.js +0 -503
  170. package/server/registry.js +0 -53
  171. package/server/serve.js +0 -163
  172. package/state/admission-queue-store.js +0 -325
  173. package/state/attribution-registries.js +0 -102
  174. package/state/batch-writer.js +0 -162
  175. package/state/db-guard.js +0 -63
  176. package/state/db-health.js +0 -27
  177. package/state/db.js +0 -122
  178. package/state/discuss-store.js +0 -299
  179. package/state/failure-store.js +0 -427
  180. package/state/migrate.js +0 -104
  181. package/state/node-cost.js +0 -67
  182. package/state/node-executions.js +0 -775
  183. package/state/node-health-store.js +0 -124
  184. package/state/ops-merge.js +0 -93
  185. package/state/paths.js +0 -146
  186. package/state/pipeline-dirs.js +0 -99
  187. package/state/pipeline-registry.js +0 -449
  188. package/state/run-store.js +0 -697
  189. package/state/schema.js +0 -438
  190. package/state/schema.ops.js +0 -44
  191. package/state/session-index.js +0 -177
  192. package/state/session-keys.js +0 -39
  193. package/state/state-store.js +0 -311
  194. package/state/subflow-state.js +0 -126
  195. package/state/tool-handoff.js +0 -169
  196. package/telemetry/budget-alert.js +0 -353
  197. package/telemetry/cli.js +0 -331
  198. package/telemetry/grammar.js +0 -119
  199. package/telemetry/http-budget.js +0 -132
  200. package/telemetry/http-cost.js +0 -670
  201. package/telemetry/http-helpers.js +0 -134
  202. package/telemetry/http-tree.js +0 -349
  203. package/telemetry/http.js +0 -44
  204. package/telemetry/index.js +0 -307
  205. package/telemetry/ingest.js +0 -134
  206. package/telemetry/labels.js +0 -115
  207. package/telemetry/pricer.js +0 -77
  208. package/telemetry/resolver.js +0 -314
  209. package/telemetry/store.js +0 -147
  210. package/utils/run-patch.js +0 -43
@@ -1,33 +0,0 @@
1
- // global-concurrency.js — the single shared concurrency ceiling
2
- // state/admission-queue-store.js reads as its getLimit() — the pool of
3
- // running flow runs: kind:"child-dispatch" (a subflow child) and
4
- // kind:"flow-start" (a whole flow run, requested by executor.js's start()
5
- // and re-requested on every resume from blocked — see engine/child-resume-
6
- // trigger.js's admitAndSetRunning/flipToRunningAndResume). A run executes
7
- // one node at a time, so this also bounds concurrent agent dispatches.
8
- //
9
- // The only per-pipeline escape hatch is `allowConcurrent: false`, and it is
10
- // coarse: a singleton gate in executor.js's start() rejects a run outright if
11
- // another non-terminal run of the same pipelineId exists. There is no way to
12
- // raise or lower the ceiling for one pipeline — a pipeline needing tighter
13
- // serialization than this shared number has to be handled by the
14
- // pipeline author (e.g. via allowConcurrent or node-level guards).
15
-
16
- const DEFAULT_MAX_CONCURRENT = 8;
17
-
18
- let maxConcurrent = DEFAULT_MAX_CONCURRENT;
19
-
20
- // Called once at plugin registration from pluginConfig, if set. A no-op
21
- // (keeps the current value) for anything not a positive integer.
22
- export function configureGlobalConcurrency({ maxConcurrent: max } = {}) {
23
- if (Number.isInteger(max) && max > 0) maxConcurrent = max;
24
- }
25
-
26
- export function getGlobalConcurrencyLimit() {
27
- return maxConcurrent;
28
- }
29
-
30
- // Test-only reset (module state is a singleton; tests need a clean slate).
31
- export function _resetForTests(max = DEFAULT_MAX_CONCURRENT) {
32
- maxConcurrent = max;
33
- }
package/engine/loader.js DELETED
@@ -1,528 +0,0 @@
1
- // loader.js — discovery of pipeline definition directories.
2
-
3
- import { readdir, stat, readFile } from "node:fs/promises";
4
- import path from "node:path";
5
- import { pathToFileURL } from "node:url";
6
- import { loadFleet } from "../agent/fleet.js";
7
- import { ENTRY_FIELDS } from "./needs-resolver.js";
8
-
9
- // Phase 2: for every subflow node under `nodes`, register its .flow as an
10
- // addressable pipeline (`${pipelineId}::${nodeId}`) so a real child
11
- // flow_run can be started against it via the normal
12
- // executor.start()/pipelinesRef path. Must happen HERE, inside
13
- // loadPipelines() itself — registering it as a one-off
14
- // pipelinesRef.value.set(...) elsewhere would be silently dropped on the
15
- // next pipelines-watcher reload tick, since that merge only preserves
16
- // stale entries backed by a real on-disk pipeline directory. Own
17
- // try/catch per node so one bad subflow doesn't block the parent pipeline
18
- // (already registered by the caller) or any other subflow node at the
19
- // same nesting level. Renamed from kind:"loop".
20
- //
21
- // Recursive (first real nested-subflow site — freelance_workana-reply's
22
- // REPLY > ENSURE_INFRA): a synthesized child pipeline's own `.nodes` can
23
- // itself contain a `kind:"subflow"` sub-node (e.g.
24
- // `freelance_workana-reply::REPLY::ENSURE_INFRA`), whose OWN children won't
25
- // dispatch correctly unless THAT compound id is registered too —
26
- // engine/executor-subflow-children.js computes a spawned child's
27
- // pipelineId as `${state.pipelineId}::${nodeId}`, so a grandchild's
28
- // pipelineId is exactly this function's own recursive `pipelineId`
29
- // parameter one level down. No depth limit; genuinely unbounded nesting
30
- // works the same way arbitrarily deep, though in practice one level is
31
- // already rare.
32
- function registerSubflowChildren(pipelineId, nodes, { result, indexPath, pipelineDir, entName, logger }, ancestorNodeMaps = [], proposalMeta = undefined) {
33
- for (const [nodeId, node] of Object.entries(nodes ?? {})) {
34
- if (node?.kind !== "subflow") continue;
35
- const syntheticId = `${pipelineId}::${nodeId}`;
36
- try {
37
- // .flow.kind is copied through if declared (e.g. "cascade") onto the
38
- // synthesized child's own `kind`, which that child's runFlow does
39
- // read — executor.js's `pipeline.kind === "cascade"` throw path is
40
- // what gives a cascade-shaped subflow its throw-then-retry-from-start
41
- // semantics. A plain subflow has no .kind and this is simply
42
- // undefined, so its dispatch is ordinary.
43
- const synthetic = { id: syntheticId, kind: node.flow.kind, state: {}, nodes: node.flow.nodes, edges: node.flow.edges };
44
- // Option B "needs:" — this synthetic child's own hop-1 ancestor is
45
- // `nodes`, the map that contains the subflow node (nodeId) which
46
- // spawns it; prepend it onto whatever ancestor chain THIS level was
47
- // itself given, so a needs:{hops:N} declared anywhere inside the
48
- // synthetic tree can be checked against a real node map, statically,
49
- // at load time.
50
- validatePipeline(synthetic, `${entName}/index.js (${nodeId}.flow)`, [nodes, ...ancestorNodeMaps]);
51
- // proposalMeta (and any future root-level config a tool looks up by
52
- // pipelineId) is declared once on the ROOT pipeline's own index.js,
53
- // never on a subflow's .flow — but a per-item agent node dispatched
54
- // from inside that subflow resolves its OWN pipelineId as this
55
- // synthetic child's id (e.g. "freelance_freelancer-proposal::PROPOSE"),
56
- // not the root's. Without carrying it through here, a tool that keys
57
- // off pipelinesRef.value.get(pipelineId) would silently miss the
58
- // root's config for every such dispatch. Threaded recursively so it
59
- // survives arbitrarily deep nesting too.
60
- result.set(syntheticId, {
61
- ...synthetic,
62
- _filepath: indexPath, _dir: pipelineDir, _fleet: null, _synthetic: true,
63
- _parentPipelineId: pipelineId, _parentNodeId: nodeId,
64
- ...(proposalMeta ? { proposalMeta } : {}),
65
- });
66
- logger?.info?.(`manifold loader: registered child-flow pipeline "${syntheticId}"`);
67
- registerSubflowChildren(syntheticId, node.flow.nodes, { result, indexPath, pipelineDir, entName, logger }, [nodes, ...ancestorNodeMaps], proposalMeta);
68
- } catch (err) {
69
- logger?.warn?.(`manifold loader: ${entName}/ node "${nodeId}" subflow branch failed to register: ${err.message}`);
70
- }
71
- }
72
- }
73
-
74
- /**
75
- * Scan a directory for pipeline definitions.
76
- *
77
- * Layout: `<pipelinesDir>/<team>_<name>/index.js` (folder per pipeline).
78
- * Each pipeline folder may also contain `design.md`, `runs.jsonl`, and a
79
- * `runs/<runId>/` subdirectory for per-run artifacts.
80
- *
81
- * Each `index.js` must `export default { id, state, nodes, edges, goal? }`.
82
- *
83
- * Returns a Map<pipelineId, pipelineDef> with `_filepath` (the index.js)
84
- * and `_dir` (the pipeline folder) attached for downstream use.
85
- */
86
- export async function loadPipelines(dir, logger) {
87
- const result = new Map();
88
- let entries;
89
- try {
90
- entries = await readdir(dir, { withFileTypes: true });
91
- } catch (err) {
92
- if (err.code === "ENOENT") {
93
- logger?.info?.(`manifold loader: pipelines dir not found at ${dir} (no pipelines loaded)`);
94
- return result;
95
- }
96
- logger?.warn?.(`manifold loader: cannot read ${dir}: ${err.message}`);
97
- return result;
98
- }
99
-
100
- for (const ent of entries) {
101
- if (ent.name.startsWith(".")) continue;
102
- if (!ent.isDirectory()) {
103
- if (ent.name.endsWith(".js")) {
104
- logger?.warn?.(`manifold loader: skipping legacy flat-file pipeline ${ent.name}; convert to a folder with index.js`);
105
- }
106
- continue;
107
- }
108
- const pipelineDir = path.join(dir, ent.name);
109
- const indexPath = path.join(pipelineDir, "index.js");
110
- try { await stat(indexPath); } catch {
111
- logger?.warn?.(`manifold loader: ${ent.name}/ has no index.js; skipping`);
112
- continue;
113
- }
114
- try {
115
- // Cache-bust strategy: read the file ourselves and import it as a
116
- // `data:` URL. The original `?t=Date.now()` query-string approach LOOKS
117
- // like a unique URL each call, but in Node 25's long-running ESM loader
118
- // the dynamic-import cache still keys on the file:// path and returns
119
- // the original module on every subsequent import — verified by
120
- // debug-logging actual pipeline contents across watcher reloads. data:
121
- // URLs are uncacheable per-path (each has a unique base64 payload) so
122
- // editing the source file always yields a fresh module.
123
- //
124
- // Relative imports (`from "./lib"`) are rewritten to absolute file://
125
- // URLs before encoding, so pipeline modules can use normal sibling
126
- // imports without knowing their install path.
127
- const source = await readFile(indexPath, "utf8");
128
- const processed = resolveRelativeImports(source, pipelineDir);
129
- const url = `data:text/javascript;base64,${Buffer.from(processed).toString("base64")}`;
130
- const mod = await import(url);
131
- const pipeline = mod.default ?? mod.pipeline ?? mod;
132
- validatePipeline(pipeline, `${ent.name}/index.js`);
133
- if (result.has(pipeline.id)) {
134
- logger?.warn?.(`manifold loader: duplicate pipeline id "${pipeline.id}" in ${ent.name}/; keeping first`);
135
- continue;
136
- }
137
- let _fleet = null;
138
- try { _fleet = await loadFleet(pipelineDir); } catch {}
139
- result.set(pipeline.id, { ...pipeline, _filepath: indexPath, _dir: pipelineDir, _fleet });
140
- logger?.info?.(`manifold loader: loaded pipeline "${pipeline.id}" from ${ent.name}/${_fleet ? " (fleet.yaml found)" : ""}`);
141
-
142
- // Phase 2: for every subflow node, register its .flow as an
143
- // addressable pipeline (`${pipeline.id}::${nodeId}`) so a real child
144
- // flow_run can be started against it via the normal
145
- // executor.start()/pipelinesRef path. Must happen HERE, inside
146
- // loadPipelines() itself — registering it as a one-off
147
- // pipelinesRef.value.set(...) elsewhere would be silently dropped on
148
- // the next pipelines-watcher reload tick, since that merge only
149
- // preserves stale entries backed by a real on-disk pipeline
150
- // directory. Own try/catch per node so one bad subflow doesn't block
151
- // the parent pipeline (already registered above) or any other
152
- // subflow node in the same pipeline. Renamed from kind:"loop".
153
- registerSubflowChildren(pipeline.id, pipeline.nodes, { result, indexPath, pipelineDir, entName: ent.name, logger }, [], pipeline.proposalMeta);
154
- } catch (err) {
155
- logger?.warn?.(`manifold loader: ${ent.name}/ failed to load: ${err.message}`);
156
- }
157
- }
158
- return result;
159
- }
160
-
161
- /**
162
- * Build a memoized `ensurePipelinesLoaded()` for a given `pipelinesRef` box.
163
- * All callers (the loader service, flow-revival, executor's per-flow
164
- * fallback) share the SAME in-flight/resolved promise instead of each doing
165
- * its own `pipelinesRef.value.size > 0` check-then-act — that heuristic let
166
- * a caller race ahead of another's still-in-flight load, intermittently
167
- * finding `pipelinesRef.value` empty and starting a redundant load whose
168
- * completion the original caller never awaited (observed 2026-08-04: orphan
169
- * flow revival at boot raced the loader service and failed with
170
- * "pipeline not loaded: <id>" even though pipelines finished loading ~150ms
171
- * later in the same boot).
172
- *
173
- * `load` is injectable for tests; defaults to the real `loadPipelines`.
174
- */
175
- export function createPipelinesLoader(dir, logger, pipelinesRef, { load = loadPipelines } = {}) {
176
- let pipelinesLoadedPromise = null;
177
- return function ensurePipelinesLoaded() {
178
- if (!pipelinesLoadedPromise) {
179
- pipelinesLoadedPromise = load(dir, logger).then((loaded) => {
180
- pipelinesRef.value = loaded;
181
- return loaded;
182
- });
183
- }
184
- return pipelinesLoadedPromise;
185
- };
186
- }
187
-
188
- function validatePipeline(p, filename, ancestorNodeMaps = []) {
189
- if (!p || typeof p !== "object") throw new Error(`pipeline must export an object`);
190
- if (!p.id || typeof p.id !== "string") throw new Error(`pipeline ${filename} missing .id`);
191
- if (!p.state || typeof p.state !== "object") throw new Error(`pipeline ${p.id}: missing .state`);
192
- if (!p.nodes || typeof p.nodes !== "object") throw new Error(`pipeline ${p.id}: missing .nodes`);
193
- if (!p.edges || typeof p.edges !== "object") throw new Error(`pipeline ${p.id}: missing .edges`);
194
- if (!p.edges.__start__) throw new Error(`pipeline ${p.id}: edges must define __start__`);
195
-
196
- // Track groupIds claimed by subflow nodes so we can catch collisions.
197
- // Subflow-mode subflows that omit groupId use their nodeId as the implicit key.
198
- const usedGroupIds = new Map(); // effectiveGroupId → nodeId
199
-
200
- for (const [nodeId, node] of Object.entries(p.nodes)) {
201
- if (!node || typeof node !== "object") throw new Error(`pipeline ${p.id}: node "${nodeId}" not an object`);
202
- if (!node.kind) throw new Error(`pipeline ${p.id}: node "${nodeId}" missing .kind`);
203
- if (!["code", "agent", "subflow", "human-gate"].includes(node.kind)) {
204
- throw new Error(`pipeline ${p.id}: node "${nodeId}" has unknown kind "${node.kind}"`);
205
- }
206
- validateNeeds(p, `node "${nodeId}"`, node, ancestorNodeMaps);
207
- if (node.kind === "code") {
208
- validateCodeKind(p, `node "${nodeId}"`, node);
209
- }
210
-
211
- if (node.kind === "agent") {
212
- validateAgentKind(p, `node "${nodeId}"`, node, { allowSkillShorthand: true });
213
- }
214
-
215
- if (node.kind === "human-gate") {
216
- if (typeof node.pendingSelector !== "function") {
217
- throw new Error(`pipeline ${p.id}: node "${nodeId}" (human-gate) missing .pendingSelector function`);
218
- }
219
- if (typeof node.approvalRequestBuilder !== "function") {
220
- throw new Error(`pipeline ${p.id}: node "${nodeId}" (human-gate) missing .approvalRequestBuilder function`);
221
- }
222
- // Optional: fired once per decided item, immediately after that
223
- // item's decision commits (gates/gate-resolver.js's
224
- // applyResolutionsAndResume) — the root-level-gate counterpart to
225
- // a subflow's WRITE sub-node, without needing subflow machinery.
226
- if (node.apply !== undefined && typeof node.apply !== "function") {
227
- throw new Error(`pipeline ${p.id}: node "${nodeId}" (human-gate) .apply must be a function`);
228
- }
229
- }
230
-
231
- if (node.kind === "subflow") {
232
- // groupId is always the node's own id — a custom groupId would make
233
- // state/node-executions.js's listInvocationIdsForNode (keyed by the
234
- // subflow's own node_id, plus branch_key IS NOT NULL to exclude
235
- // ordinary sub-node timing rows) never find this subflow's children,
236
- // permanently deadlocking the parent's resume-trigger. Top-level-only
237
- // check (uniqueness across a nested subflow's own id and some other
238
- // node's id is not a real collision risk — they live under
239
- // different flowIds — so this isn't extended into
240
- // validateSubflowNode's recursive nested-node validation below).
241
- if (node.groupId !== undefined) {
242
- throw new Error(`pipeline ${p.id}: node "${nodeId}" (subflow) must not set .groupId — it is always the node's own id`);
243
- }
244
- if (usedGroupIds.has(nodeId)) {
245
- throw new Error(`pipeline ${p.id}: node "${nodeId}" implicit groupId collides with another subflow node using groupId "${nodeId}"`);
246
- }
247
- usedGroupIds.set(nodeId, nodeId);
248
-
249
- // Option B "needs:" — node.flow.nodes' own hop-1 ancestor, once
250
- // spawned, is this pipeline itself (p.nodes) — prepend it here so
251
- // nested needs: entries resolve against a real ancestor chain.
252
- validateSubflowNode(p, `node "${nodeId}"`, node, [p.nodes, ...ancestorNodeMaps]);
253
- }
254
- }
255
-
256
- // Optional `groups` field — logical groupings of nodes the dashboard
257
- // renders as compound parents. Each group's `members` array must list
258
- // existing node ids; a single node cannot belong to more than one group.
259
- //
260
- // Members may be either:
261
- // - top-level node ids (e.g. "SCAN_CODE"), or
262
- // - qualified branch-internal ids (e.g. "PROPOSE.SUBMIT_CODE") where
263
- // "PROPOSE" is a subflow node and "SUBMIT_CODE" lives in
264
- // that node's flow.nodes. The dashboard renders these as a
265
- // compound nested inside the subflow node's own compound.
266
- //
267
- // Mixing scopes inside a single group is forbidden — every member of a
268
- // group must live in the same scope (all top-level, or all inside the
269
- // same subflow). Cytoscape compound nodes can only have
270
- // one parent, so a group's compound has to live entirely in one scope.
271
- if (p.groups !== undefined) {
272
- if (typeof p.groups !== "object" || Array.isArray(p.groups)) {
273
- throw new Error(`pipeline ${p.id}: .groups must be an object`);
274
- }
275
- const membership = new Map();
276
- for (const [groupId, group] of Object.entries(p.groups)) {
277
- if (!group || typeof group !== "object") {
278
- throw new Error(`pipeline ${p.id}: group "${groupId}" must be an object`);
279
- }
280
- if (!Array.isArray(group.members) || group.members.length === 0) {
281
- throw new Error(`pipeline ${p.id}: group "${groupId}" must have a non-empty .members array`);
282
- }
283
- let scopeParent = null; // null = top-level; otherwise the subflow node id
284
- for (const m of group.members) {
285
- if (typeof m !== "string") {
286
- throw new Error(`pipeline ${p.id}: group "${groupId}" member must be a string, got ${typeof m}`);
287
- }
288
- const dot = m.indexOf(".");
289
- let memberScope = null;
290
- if (dot === -1) {
291
- if (!(m in p.nodes)) {
292
- throw new Error(`pipeline ${p.id}: group "${groupId}" member "${m}" not found in .nodes`);
293
- }
294
- } else {
295
- const parent = m.slice(0, dot);
296
- const child = m.slice(dot + 1);
297
- const parentNode = p.nodes[parent];
298
- if (!parentNode) {
299
- throw new Error(`pipeline ${p.id}: group "${groupId}" member "${m}" references unknown parent "${parent}"`);
300
- }
301
- if (parentNode.kind !== "subflow") {
302
- throw new Error(`pipeline ${p.id}: group "${groupId}" member "${m}" — parent "${parent}" is "${parentNode.kind}", expected "subflow"`);
303
- }
304
- if (!parentNode.flow?.nodes || !(child in parentNode.flow.nodes)) {
305
- throw new Error(`pipeline ${p.id}: group "${groupId}" member "${m}" — child "${child}" not in flow.nodes of "${parent}"`);
306
- }
307
- memberScope = parent;
308
- }
309
- if (scopeParent === null && memberScope !== null) scopeParent = memberScope;
310
- else if (memberScope !== scopeParent) {
311
- throw new Error(`pipeline ${p.id}: group "${groupId}" mixes scopes — members must all be top-level or all inside the same subflow`);
312
- }
313
- if (membership.has(m)) {
314
- throw new Error(`pipeline ${p.id}: node "${m}" is in multiple groups ("${membership.get(m)}" and "${groupId}")`);
315
- }
316
- membership.set(m, groupId);
317
- }
318
- }
319
- }
320
- }
321
-
322
- // Option B "needs:" — any code/agent/subflow node, top-level or nested,
323
- // may declare `.needs: { varName: {hops, nodeId}, ... }` — keyed by the
324
- // LOCAL variable name it'll be resolved as via ctx.needs(varName), not by
325
- // nodeId, so it reads like an ORM relation declaration rather than a bare
326
- // fetch list.
327
- // Validated here (not at runtime, per this codebase's own load-time-
328
- // validation convention — see the analogous groups/.members check below)
329
- // using `ancestorNodeMaps`, threaded down from validatePipeline/register
330
- // SubflowChildren: since a subflow's synthetic childPipelineId is always
331
- // `${parentPipelineId}::${nodeId}`, chained at every nesting level, the
332
- // FULL ancestor chain of real node maps is statically knowable at load
333
- // time — a needs: entry naming a hop that doesn't exist, or a nodeId that
334
- // doesn't exist at that ancestor level, throws here rather than resolving
335
- // to a silent runtime "not-found" indistinguishable from a race.
336
- //
337
- // A spec names EXACTLY ONE of `nodeId` (an ancestor's own node output) or
338
- // `entry` (an ancestor flow's own entry/bookkeeping field — runId, args,
339
- // etc., set at that flow's start(), never via any node's resultJson).
340
- // `entry` is checked against ENTRY_FIELDS (needs-resolver.js) — `gates` is
341
- // deliberately not on that list even though it's a real stateJson key; it
342
- // has its own dedicated accessor (gate_records) precisely because reading
343
- // it through the generic needs: path reopens a real bug that mechanism
344
- // was built to close. An `entry`-type need still needs the same hop-depth
345
- // check as a `nodeId`-type one (does the pipeline nest that deep at all)
346
- // but skips the node-existence check, since it isn't naming a node.
347
- //
348
- // human-gate is deliberately excluded — ctx.needs() isn't wired into
349
- // pendingSelector/approvalRequestBuilder yet (no real use case surfaced),
350
- // so a human-gate node declaring .needs would validate fine and then have
351
- // no way to ever call it — throwing here is louder and more honest than
352
- // silently accepting something inert.
353
- function validateNeeds(p, label, node, ancestorNodeMaps) {
354
- if (node.needs === undefined) return;
355
- if (node.kind === "human-gate") {
356
- throw new Error(`pipeline ${p.id}: ${label} (human-gate) .needs is not yet supported — ctx.needs() is not wired into pendingSelector/approvalRequestBuilder`);
357
- }
358
- if (typeof node.needs !== "object" || node.needs === null || Array.isArray(node.needs)) {
359
- throw new Error(`pipeline ${p.id}: ${label} .needs must be an object keyed by variable name: { varName: {hops, nodeId} }`);
360
- }
361
- for (const [varName, need] of Object.entries(node.needs)) {
362
- if (!need || typeof need !== "object") {
363
- throw new Error(`pipeline ${p.id}: ${label} .needs.${varName} must be an object {hops, nodeId}, {hops, entry}, or {hops, config}`);
364
- }
365
- const { hops, nodeId, entry, config } = need;
366
- // hops:0 = same-flow (the declaring node's own flow — always exists,
367
- // no nesting-depth check needed). hops>=1 = walk that many parent
368
- // flows up, same as before.
369
- if (!Number.isInteger(hops) || hops < 0) {
370
- throw new Error(`pipeline ${p.id}: ${label} .needs.${varName}.hops must be a non-negative integer, got ${JSON.stringify(hops)}`);
371
- }
372
- const declared = [nodeId !== undefined, entry !== undefined, config !== undefined].filter(Boolean).length;
373
- if (declared !== 1) {
374
- throw new Error(`pipeline ${p.id}: ${label} .needs.${varName} must declare exactly one of .nodeId, .entry, or .config`);
375
- }
376
- if (nodeId !== undefined && (typeof nodeId !== "string" || !nodeId)) {
377
- throw new Error(`pipeline ${p.id}: ${label} .needs.${varName}.nodeId must be a non-empty string`);
378
- }
379
- if (entry !== undefined && !ENTRY_FIELDS.has(entry)) {
380
- throw new Error(`pipeline ${p.id}: ${label} .needs.${varName}.entry "${entry}" is not a recognized entry/bookkeeping field — see needs-resolver.js's ENTRY_FIELDS`);
381
- }
382
- // config isn't checkable against the target pipeline's own `state:`
383
- // keys at load time the way entry/nodeId are — pipelines can load in
384
- // any order and `state:` declares no schema. Just require a non-empty
385
- // string; an unknown key resolves to `undefined` at runtime, same as
386
- // reading a missing object property anywhere else in this codebase.
387
- if (config !== undefined && (typeof config !== "string" || !config)) {
388
- throw new Error(`pipeline ${p.id}: ${label} .needs.${varName}.config must be a non-empty string`);
389
- }
390
- if (need.fields !== undefined && (!Array.isArray(need.fields) || need.fields.length === 0 || !need.fields.every((f) => typeof f === "string" && f))) {
391
- throw new Error(`pipeline ${p.id}: ${label} .needs.${varName}.fields must be a non-empty array of non-empty strings — always an array, even for a single field`);
392
- }
393
- if (hops === 0) {
394
- if (nodeId !== undefined && !(nodeId in p.nodes)) {
395
- throw new Error(`pipeline ${p.id}: ${label} needs.${varName} {hops:0, nodeId:"${nodeId}"} — no node "${nodeId}" in this pipeline`);
396
- }
397
- continue;
398
- }
399
- const ancestorMap = ancestorNodeMaps[hops - 1];
400
- if (!ancestorMap) {
401
- throw new Error(`pipeline ${p.id}: ${label} needs.${varName} {hops:${hops}} but this node's pipeline nesting doesn't go that deep`);
402
- }
403
- if (nodeId !== undefined && !(nodeId in ancestorMap)) {
404
- throw new Error(`pipeline ${p.id}: ${label} needs.${varName} {hops:${hops}, nodeId:"${nodeId}"} — no node "${nodeId}" at that ancestor level`);
405
- }
406
- }
407
- }
408
-
409
- function validateCodeKind(p, label, obj) {
410
- if (typeof obj.run !== "function") {
411
- throw new Error(`pipeline ${p.id}: ${label} (code) missing .run function`);
412
- }
413
- }
414
-
415
- // Shared shape validation for a kind:"subflow" node, called both for a
416
- // top-level node and recursively for a nested one (a `flow.nodes` entry
417
- // that is itself kind:"subflow" — first real site: freelance_workana-
418
- // reply's REPLY > ENSURE_INFRA). `label` is already the caller's fully
419
- // qualified prefix (e.g. `node "REPLY"` or `node "REPLY".flow.nodes.
420
- // "ENSURE_INFRA"`), so nesting composes into a readable dotted path
421
- // automatically. groupId-uniqueness is NOT checked here — see the
422
- // top-level call site's own comment for why that stays top-level-only.
423
- function validateSubflowNode(p, label, node, ancestorNodeMaps) {
424
- if (typeof node.selector !== "function") {
425
- throw new Error(`pipeline ${p.id}: ${label} (subflow) missing .selector function`);
426
- }
427
- // Required, not defaulted — a subflow node's children going terminal
428
- // resumes the parent regardless of success/failure unless mode says
429
- // otherwise (see child-resume-trigger.js's maybeResumeParent), so every
430
- // author has to pick a propagation policy explicitly rather than
431
- // silently inheriting "never fails the parent."
432
- if (node.mode !== "any" && node.mode !== "all") {
433
- throw new Error(`pipeline ${p.id}: ${label} (subflow) requires .mode to be "any" or "all"`);
434
- }
435
- // Every subflow node's items become real flow_runs children — requires a
436
- // .flow definition (.flow.nodes/.edges); there is no other subflow shape,
437
- // the old branch-worker engine having been deleted outright. Renamed from
438
- // kind:"loop" — a selector returning a single-item array is the
439
- // degenerate "spawn exactly one child" case, not a different kind.
440
- if (!node.flow) {
441
- throw new Error(`pipeline ${p.id}: ${label} (subflow) requires .flow (.flow.nodes/.edges)`);
442
- }
443
- if (!node.flow.nodes || typeof node.flow.nodes !== "object") {
444
- throw new Error(`pipeline ${p.id}: ${label} .flow missing .nodes`);
445
- }
446
- if (!node.flow.edges || typeof node.flow.edges !== "object") {
447
- throw new Error(`pipeline ${p.id}: ${label} .flow missing .edges`);
448
- }
449
- if (!node.flow.edges.__start__) {
450
- throw new Error(`pipeline ${p.id}: ${label} .flow.edges must define __start__`);
451
- }
452
- validateFlowSubNodes(p, label, node.flow.nodes, ancestorNodeMaps);
453
- }
454
-
455
- // The `for (const [subId, sub] of ...)` loop over one subflow's
456
- // flow.nodes, shared between the top-level subflow branch and
457
- // validateSubflowNode's own recursive nested case. `sub.kind ===
458
- // "subflow"` recurses back into validateSubflowNode — arbitrarily deep
459
- // nesting validates the same way at every level.
460
- function validateFlowSubNodes(p, label, flowNodes, ancestorNodeMaps = []) {
461
- for (const [subId, sub] of Object.entries(flowNodes)) {
462
- const subLabel = `${label}.flow.nodes."${subId}"`;
463
- if (!sub || typeof sub !== "object") {
464
- throw new Error(`pipeline ${p.id}: ${subLabel} not an object`);
465
- }
466
- if (!sub.kind) {
467
- throw new Error(`pipeline ${p.id}: ${subLabel} missing .kind`);
468
- }
469
- if (!["code", "agent", "subflow", "human-gate"].includes(sub.kind)) {
470
- throw new Error(`pipeline ${p.id}: ${subLabel} has unknown kind "${sub.kind}"`);
471
- }
472
- validateNeeds(p, subLabel, sub, ancestorNodeMaps);
473
- if (sub.kind === "agent") {
474
- validateAgentKind(p, subLabel, sub, { allowSkillShorthand: false });
475
- }
476
- if (sub.kind === "subflow") {
477
- // Same pattern as the top-level loop: sub.flow.nodes' own hop-1
478
- // ancestor, once spawned, is `flowNodes` (the map sub itself lives
479
- // in) — prepend it before recursing.
480
- validateSubflowNode(p, subLabel, sub, [flowNodes, ...ancestorNodeMaps]);
481
- }
482
- if (sub.kind === "human-gate") {
483
- // A child is an ordinary flow — its gate sub-nodes need the
484
- // same shape a root-level human-gate node does.
485
- if (typeof sub.pendingSelector !== "function") {
486
- throw new Error(`pipeline ${p.id}: ${subLabel} (human-gate) missing .pendingSelector function`);
487
- }
488
- if (typeof sub.approvalRequestBuilder !== "function") {
489
- throw new Error(`pipeline ${p.id}: ${subLabel} (human-gate) missing .approvalRequestBuilder function`);
490
- }
491
- // Optional, same as the root-level human-gate check above.
492
- if (sub.apply !== undefined && typeof sub.apply !== "function") {
493
- throw new Error(`pipeline ${p.id}: ${subLabel} (human-gate) .apply must be a function`);
494
- }
495
- }
496
- }
497
- }
498
-
499
- function validateAgentKind(p, label, obj, { allowSkillShorthand }) {
500
- // Top-level agent nodes accept a `skill` shorthand string in place of
501
- // `.promptBuilder`; subflow sub-nodes don't.
502
- if (allowSkillShorthand) {
503
- if (typeof obj.promptBuilder !== "function" && typeof obj.skill !== "string") {
504
- throw new Error(`pipeline ${p.id}: ${label} (agent) missing .promptBuilder function`);
505
- }
506
- } else if (typeof obj.promptBuilder !== "function") {
507
- throw new Error(`pipeline ${p.id}: ${label} (agent) missing .promptBuilder function`);
508
- }
509
- if (typeof obj.apply !== "function" && typeof obj.applyResult !== "function") {
510
- throw new Error(`pipeline ${p.id}: ${label} (agent) missing .apply function`);
511
- }
512
- }
513
-
514
- // Replace relative import specifiers (`from "./lib"`, `from "../shared"`,
515
- // and dynamic `import("./file")`) with absolute file:// URLs so pipeline
516
- // modules loaded via data: URLs can import sibling files normally.
517
- function resolveRelativeImports(source, dir) {
518
- const dirUrl = pathToFileURL(dir + "/").href;
519
- return source
520
- .replace(
521
- /(\bfrom\s+["'])(\.\.?\/[^"']+)(["'])/g,
522
- (_, prefix, rel, suffix) => `${prefix}${new URL(rel, dirUrl).href}${suffix}`,
523
- )
524
- .replace(
525
- /(\bimport\s*\(\s*["'])(\.\.?\/[^"']+)(["']\s*\))/g,
526
- (_, prefix, rel, suffix) => `${prefix}${new URL(rel, dirUrl).href}${suffix}`,
527
- );
528
- }