@melite/estate-queue 0.0.0-stage → 0.1.5-pre-alpha

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 (131) hide show
  1. package/LICENSE +557 -0
  2. package/README.md +54 -2
  3. package/lib/bakeParams.d.ts +17 -0
  4. package/lib/bakeParams.js +182 -0
  5. package/lib/bench.d.ts +32 -0
  6. package/lib/bench.js +111 -0
  7. package/lib/cardsArmError.d.ts +61 -0
  8. package/lib/cardsArmError.js +57 -0
  9. package/lib/cardsCompose.d.ts +53 -0
  10. package/lib/cardsCompose.js +295 -0
  11. package/lib/cardsDangling.d.ts +16 -0
  12. package/lib/cardsDangling.js +213 -0
  13. package/lib/cardsFacts.d.ts +87 -0
  14. package/lib/cardsFacts.js +78 -0
  15. package/lib/cardsLanes.d.ts +43 -0
  16. package/lib/cardsLanes.js +178 -0
  17. package/lib/cardsRegistry.d.ts +404 -0
  18. package/lib/cardsRegistry.js +1543 -0
  19. package/lib/cardsVariant.d.ts +37 -0
  20. package/lib/cardsVariant.js +243 -0
  21. package/lib/discloseRegistry.d.ts +95 -0
  22. package/lib/discloseRegistry.js +156 -0
  23. package/lib/filmLead.d.ts +4 -0
  24. package/lib/filmLead.js +17 -0
  25. package/lib/filmMemoryWarn.d.ts +27 -0
  26. package/lib/filmMemoryWarn.js +99 -0
  27. package/lib/flowsData.d.ts +234 -0
  28. package/lib/flowsData.js +673 -0
  29. package/lib/flowsFamilyCompose.d.ts +45 -0
  30. package/lib/flowsFamilyCompose.js +191 -0
  31. package/lib/flowsParamsOverlay.d.ts +55 -0
  32. package/lib/flowsParamsOverlay.js +197 -0
  33. package/lib/flowsRegistryRows.d.ts +12 -0
  34. package/lib/flowsRegistryRows.js +520 -0
  35. package/lib/index.d.ts +47 -0
  36. package/lib/index.js +75 -0
  37. package/lib/ledgerTail.d.ts +20 -0
  38. package/lib/ledgerTail.js +123 -0
  39. package/lib/lifecycleLatch.d.ts +16 -0
  40. package/lib/lifecycleLatch.js +42 -0
  41. package/lib/loadPlane.d.ts +94 -0
  42. package/lib/loadPlane.js +570 -0
  43. package/lib/loadRegistry.d.ts +22 -0
  44. package/lib/loadRegistry.js +55 -0
  45. package/lib/manageOps.d.ts +27 -0
  46. package/lib/manageOps.js +162 -0
  47. package/lib/managePlane.d.ts +151 -0
  48. package/lib/managePlane.js +1790 -0
  49. package/lib/manageProbe.d.ts +48 -0
  50. package/lib/manageProbe.js +165 -0
  51. package/lib/manageRegistry.d.ts +66 -0
  52. package/lib/manageRegistry.js +113 -0
  53. package/lib/managerRail.d.ts +33 -0
  54. package/lib/managerRail.js +91 -0
  55. package/lib/mediaKinds.d.ts +3 -0
  56. package/lib/mediaKinds.js +2 -0
  57. package/lib/queue.d.ts +9 -0
  58. package/lib/queue.js +80 -0
  59. package/lib/queueCore.d.ts +72 -0
  60. package/lib/queueCore.js +128 -0
  61. package/lib/queueLifecycle.d.ts +2 -0
  62. package/lib/queueLifecycle.js +11 -0
  63. package/lib/queueLock.d.ts +60 -0
  64. package/lib/queueLock.js +198 -0
  65. package/lib/queueResurrect.d.ts +59 -0
  66. package/lib/queueResurrect.js +249 -0
  67. package/lib/queueRetry.d.ts +36 -0
  68. package/lib/queueRetry.js +24 -0
  69. package/lib/queueSidecar.d.ts +149 -0
  70. package/lib/queueSidecar.js +183 -0
  71. package/lib/queueSubmit.d.ts +82 -0
  72. package/lib/queueSubmit.js +191 -0
  73. package/lib/queueSweep.d.ts +30 -0
  74. package/lib/queueSweep.js +132 -0
  75. package/lib/resurrect.d.ts +6 -0
  76. package/lib/resurrect.js +18 -0
  77. package/lib/rootsManifest.d.ts +11 -0
  78. package/lib/rootsManifest.js +11 -0
  79. package/lib/runsChange.d.ts +61 -0
  80. package/lib/runsChange.js +79 -0
  81. package/lib/runsErrorCodes.d.ts +2 -0
  82. package/lib/runsErrorCodes.js +48 -0
  83. package/lib/runsExecute.d.ts +52 -0
  84. package/lib/runsExecute.js +494 -0
  85. package/lib/runsGates.d.ts +161 -0
  86. package/lib/runsGates.js +307 -0
  87. package/lib/runsList.d.ts +78 -0
  88. package/lib/runsList.js +574 -0
  89. package/lib/runsRefusalVoices.d.ts +9 -0
  90. package/lib/runsRefusalVoices.js +96 -0
  91. package/lib/runsRegistry.d.ts +351 -0
  92. package/lib/runsRegistry.js +2394 -0
  93. package/lib/runsSourceEcho.d.ts +81 -0
  94. package/lib/runsSourceEcho.js +341 -0
  95. package/lib/runsStart.d.ts +163 -0
  96. package/lib/runsStart.js +667 -0
  97. package/lib/runsTypes.d.ts +218 -0
  98. package/lib/runsTypes.js +289 -0
  99. package/lib/runsValidate.d.ts +5 -0
  100. package/lib/runsValidate.js +31 -0
  101. package/lib/runsValidateArms.d.ts +7 -0
  102. package/lib/runsValidateArms.js +339 -0
  103. package/lib/runsValidateInline.d.ts +6 -0
  104. package/lib/runsValidateInline.js +82 -0
  105. package/lib/runsValidateRefusals.d.ts +7 -0
  106. package/lib/runsValidateRefusals.js +18 -0
  107. package/lib/runsValidateServe.d.ts +15 -0
  108. package/lib/runsValidateServe.js +276 -0
  109. package/lib/runsValidateShared.d.ts +84 -0
  110. package/lib/runsValidateShared.js +88 -0
  111. package/lib/runsValidateStages.d.ts +32 -0
  112. package/lib/runsValidateStages.js +511 -0
  113. package/lib/runsValidateSweeps.d.ts +6 -0
  114. package/lib/runsValidateSweeps.js +143 -0
  115. package/lib/service.d.ts +63 -0
  116. package/lib/service.js +89 -0
  117. package/lib/skillFrontmatter.d.ts +11 -0
  118. package/lib/skillFrontmatter.js +46 -0
  119. package/lib/skillFs.d.ts +24 -0
  120. package/lib/skillFs.js +78 -0
  121. package/lib/skillPlane.d.ts +99 -0
  122. package/lib/skillPlane.js +269 -0
  123. package/lib/skillRegistry.d.ts +23 -0
  124. package/lib/skillRegistry.js +55 -0
  125. package/lib/skillRows.d.ts +127 -0
  126. package/lib/skillRows.js +468 -0
  127. package/lib/toolProjection.d.ts +30 -0
  128. package/lib/toolProjection.js +142 -0
  129. package/lib/vocab.d.ts +6 -0
  130. package/lib/vocab.js +18 -0
  131. package/package.json +48 -4
@@ -0,0 +1,511 @@
1
+ // runsValidateStages.ts — stage-row + flow-construction validators split from
2
+ // runsValidate.ts (pure move, zero logic changes): strict-body admission,
3
+ // stage rows, wires, the card gate, kinds, engines, flow_from_body.
4
+ import { coerceWireArrays, coerceWireNumbers, unwrapItemArrays } from '@melite/comfyui-card-kit';
5
+ import { engineIdOk } from "@melite/engine-contract";
6
+ import { checkStrictBody } from "./runsGates.js";
7
+ import { ENGINE_TABLE, FLOW_ID_RE, INLINE_TOP_SORTED, STAGE_KEYS_SORTED, FlowError, needCardRegistry, } from "./runsValidateShared.js";
8
+ import { edgeRef, inlineEdge, inlineKind, inlineSkip } from "./runsValidateInline.js";
9
+ import { coerceRecord, minLengthRefusal } from "./runsValidateRefusals.js";
10
+ // ── strict body ──────
11
+ // Key split owned by the admission gate; the FlowError prose stays here.
12
+ export function strictBody(body) {
13
+ const offense = checkStrictBody(body);
14
+ if (offense.bad.length > 0) {
15
+ throw new FlowError("request body carries retired runner knob(s) " +
16
+ JSON.stringify(offense.bad) +
17
+ " — the runner never reads these (silent-drop class); " +
18
+ "remove them");
19
+ }
20
+ if (offense.carry.length > 0) {
21
+ throw new FlowError("request body carries retired pipe-carry key(s) " +
22
+ JSON.stringify(offense.carry) +
23
+ " — the 2026-08-10 stowaway wire: no gateway ever " +
24
+ "stashed it (flow_doc was never emitted). The revive " +
25
+ "rail is the draft (draft_id) + the run registry; card " +
26
+ "bodies carry params only");
27
+ }
28
+ }
29
+ // Build one stage row from a JSON stage: the extras gate runs
30
+ // OUTSIDE the per-stage try (a non-object stage with extras
31
+ // refuses on the keys walk — never a silent coerce), then the
32
+ // object gate, then the per-name pulls inside the try whose
33
+ // FlowErrors pass through bare and every other exception wraps as
34
+ // "stage[i] refuses:".
35
+ export function buildStageRow(i, st) {
36
+ const where = "stage[" + i + "]";
37
+ const isRow = typeof st === "object" && st !== null && !Array.isArray(st);
38
+ // unknown keys = the stage object's own keys minus the legal set.
39
+ // (a non-object row has no keys to walk — the object gate below
40
+ // owns its refusal voice; Object.keys(null) would throw a
41
+
42
+ const rawExtras = isRow
43
+ ? [...new Set(Object.keys(st))].filter((k) => typeof k !== "string" || !STAGE_KEYS_SORTED.includes(k))
44
+ : [];
45
+ if (rawExtras.length > 0) {
46
+ // (a non-object row cannot reach here: isRow gated the keys walk,
47
+ // so rawExtras is empty for it and the object gate below owns its
48
+
49
+ const rawId = st["id"] ?? "<no id>";
50
+ throw new FlowError(where +
51
+ " " +
52
+ JSON.stringify(rawId) +
53
+ ": body carries unknown field(s) " +
54
+ JSON.stringify(rawExtras) +
55
+ " — the schema declares " +
56
+ JSON.stringify([...STAGE_KEYS_SORTED]) +
57
+ " (extra=forbid at the door)");
58
+ }
59
+ if (typeof st !== "object" || st === null || Array.isArray(st)) {
60
+ throw new FlowError(where + ": " + JSON.stringify(st) + " is not an object");
61
+ }
62
+ try {
63
+ const rec = st;
64
+ // (st.get("inputs") or {}) — falsy rides {}, truthy must be a
65
+ // dict (.items() AttributeError text by container type).
66
+ const rawInputs = rec["inputs"] || {};
67
+ if (typeof rawInputs !== "object" || rawInputs === null || Array.isArray(rawInputs)) {
68
+ throw new Error(where + " inputs must be an object");
69
+ }
70
+ const inputs = Object.entries(rawInputs).map(([name, edge]) => ({
71
+ name,
72
+ edge: inlineEdge(where + " input " + JSON.stringify(name), edge),
73
+ }));
74
+ const rawOutputs = rec["outputs"] || {};
75
+ if (typeof rawOutputs !== "object" || rawOutputs === null || Array.isArray(rawOutputs)) {
76
+ throw new Error(where + " outputs must be an object");
77
+ }
78
+ const outputs = [];
79
+ for (const [name, kind] of Object.entries(rawOutputs)) {
80
+ outputs.push({ name, kind: inlineKind(where + " output " + JSON.stringify(name), kind) });
81
+ }
82
+ const rawExpected = rec["expected"] || {};
83
+ if (typeof rawExpected !== "object" || rawExpected === null || Array.isArray(rawExpected)) {
84
+ throw new Error(where + " expected must be an object");
85
+ }
86
+ const expected = [];
87
+ for (const [name, kind] of Object.entries(rawExpected)) {
88
+ expected.push({ name, kind: inlineKind(where + " expected " + JSON.stringify(name), kind) });
89
+ }
90
+ const rawCardIo = rec["card_io"] || {};
91
+ if (typeof rawCardIo !== "object" || rawCardIo === null || Array.isArray(rawCardIo)) {
92
+ throw new Error(where + " card_io must be an object");
93
+ }
94
+ const cardIo = [];
95
+ for (const [name, kind] of Object.entries(rawCardIo)) {
96
+ cardIo.push({ name, kind: inlineKind(where + " card_io " + JSON.stringify(name), kind) });
97
+ }
98
+ const skipWhen = inlineSkip(where, rec["skip_when"] ?? null);
99
+ // params defaults to {} — strict record, never a throw.
100
+ const params = coerceRecord(rec["params"] || {});
101
+ // Stage fields in DEFINITION order: id/engine
102
+ // id/engine refuse empty first, then the id-legal check over
103
+ // the full record.
104
+ // missing rides "", present rides String().
105
+ const id = "id" in rec ? String(rec["id"] ?? "") : "";
106
+ if (id === "")
107
+ throw new FlowError(where + " refuses: " + minLengthRefusal("Stage", "id"));
108
+ const engine = "engine" in rec ? String(rec["engine"] ?? "") : "";
109
+ if (engine === "") {
110
+ throw new FlowError(where + " refuses: " + minLengthRefusal("Stage", "engine"));
111
+ }
112
+ const description = "description" in rec ? String(rec["description"] ?? "") : "";
113
+ const card = String(rec["card"] || "");
114
+ // (THE CARD FOLD NOTE, attempt-035's dead-route receipt
115
+
116
+ // the registry — the feature stands. The dead route was the
117
+ // TOOL seat DROPPING card in foldConnects: validate saw a
118
+ // cardless row, start's flow_inline card gate never fired, and
119
+ // the engine answered `node carries no inline workflow`. The
120
+ // fold now keeps card — start refuses loud, never cryptic.)
121
+ if (!FLOW_ID_RE.test(id)) {
122
+ throw new FlowError(where +
123
+ " refuses: " +
124
+ "stage id " + JSON.stringify(id) + " is not a legal identifier");
125
+ }
126
+ // FromInitial(key) min_length=1 — the empty-key fake (raised by
127
+ // _inline_edge's own FromInitial(...) construction, before any
128
+ // Stage check — unreachable here, kept for shape faithfulness).
129
+ for (const inp of inputs) {
130
+ if (inp.edge.kind === "initial" && inp.edge.key === "") {
131
+ throw new FlowError(where + " refuses: " + minLengthRefusal("FromInitial", "key"));
132
+ }
133
+ }
134
+ return { id, engine, description, params, inputs, outputs, expected, card, cardIo, skipWhen };
135
+ }
136
+ catch (e) {
137
+ if (e instanceof FlowError)
138
+ throw e;
139
+ throw new FlowError(where + " refuses: " + e.message);
140
+ }
141
+ }
142
+ // ── Flow construction validators (pure subset) ─────
143
+ // _wires_resolve: illegal flow id, duplicate ids
144
+ // (own loop FIRST), then earlier-stage edges. Plain FlowErrors
145
+ // flow_from_body digs them out of the ValidationError wrapper, so
146
+ // the door prints them bare.
147
+ export function wiresResolve(flowId, stages) {
148
+ if (!FLOW_ID_RE.test(flowId)) {
149
+ throw new FlowError("flow id " + JSON.stringify(flowId) + " is not a legal identifier");
150
+ }
151
+ const seen = new Set();
152
+ for (const st of stages) {
153
+ if (seen.has(st.id)) {
154
+ throw new FlowError("duplicate stage id " + JSON.stringify(st.id));
155
+ }
156
+ seen.add(st.id);
157
+ }
158
+ const done = [];
159
+ for (const st of stages) {
160
+ for (const inp of st.inputs) {
161
+ const edge = inp.edge;
162
+ if (edge.kind !== "stage")
163
+ continue;
164
+ if (!done.includes(edge.stage)) {
165
+ throw new FlowError("stage " +
166
+ JSON.stringify(st.id) +
167
+ " input " +
168
+ JSON.stringify(inp.name) +
169
+ " wires to " +
170
+ JSON.stringify(edgeRef(edge)) +
171
+ " — not an EARLIER stage (known earlier: " +
172
+ (done.length > 0 ? JSON.stringify([...done].sort()) : "none") +
173
+ ")");
174
+ }
175
+ }
176
+ done.push(st.id);
177
+ }
178
+ }
179
+ // cardDefs — the live card registry for the card-stage lattice,
180
+ // cardGate — the CARD-STAGE LATTICE body gate (
181
+ // _card_params_gate -> stage_params.validate_stage_params, PORTED
182
+
183
+ // wall pins it); (2) the schema pass rides the TS single
184
+ // declaration — the registry's Schemastery schema parse, the same
185
+ // validator compose runs, never a second truth.
186
+ // (Stroke 2a: the defs map rides the service — the old module cache
187
+ // resolved ONCE at plugin apply is gone; the loader gates this
188
+ // door on the card-registry provider, so defsSync() is primed here.
189
+ // The registry arg is optional only for pre-service callers —
190
+ // card stages without it throw LOUD.)
191
+ export function cardGate(flowId, st, cards) {
192
+ const defs = needCardRegistry(cards, "cardGate").defsSync();
193
+ const def = defs.get(st.card);
194
+ // requiresModel: false (pure-tool cards, boarded: []) skips the
195
+ // {model:} demand — the row.requires_model. Unknown
196
+ // cards still hit the model gate first (order preserved: the wall
197
+ // pins the model-gate text verbatim).
198
+ if (def?.requiresModel !== false) {
199
+ const model = st.params["model"];
200
+ if (!model || !String(model).trim()) {
201
+ throw new FlowError("flow " +
202
+ JSON.stringify(flowId) +
203
+ " stage " +
204
+ JSON.stringify(st.id) +
205
+ ": card " +
206
+ JSON.stringify(st.card) +
207
+ " requires params.model (the model row id the card composes " +
208
+ "against — see GET /v1/models) — the flow-stage equivalent " +
209
+ "of the {model:} door");
210
+ }
211
+ }
212
+ if (def === undefined) {
213
+ throw new FlowError("flow " +
214
+ JSON.stringify(flowId) +
215
+ " stage " +
216
+ JSON.stringify(st.id) +
217
+ ": card " +
218
+ JSON.stringify(st.card) +
219
+ " is not a registered card (the card set boots from " +
220
+ "src/cards — see GET /v1/tools for the served surface)");
221
+ }
222
+ try {
223
+ // THE WIRE-COERCION FAMILY AT THE LATTICE SEAT (attempt-035's
224
+
225
+ // got 12` and `$.ref_images expected array but got [object
226
+ // Object]` — the same corruption the compose door already
227
+ // guards. The lattice runs the SAME validator compose runs;
228
+ // it now reads through the SAME coercion (shape unwrap →
229
+ // schema-derived array fill → schema-derived numeric fill).
230
+ const carrier = (def.schemaCarrier ?? def.schema);
231
+ def.schema(coerceWireNumbers(carrier, coerceWireArrays(carrier, unwrapItemArrays(st.params))));
232
+ }
233
+ catch (exc) {
234
+ const msg = exc instanceof Error ? exc.message : String(exc);
235
+ throw new FlowError("flow " +
236
+ JSON.stringify(flowId) +
237
+ " stage " +
238
+ JSON.stringify(st.id) +
239
+ ": card " +
240
+ JSON.stringify(st.card) +
241
+ " schema refused body — " +
242
+ msg);
243
+ }
244
+ }
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+ export function coerceSnapshotBody(def, body) {
259
+ if (def === undefined)
260
+ return body;
261
+ const carrier = (def.schemaCarrier ?? def.schema);
262
+ return coerceWireNumbers(carrier, coerceWireArrays(carrier, unwrapItemArrays(body)));
263
+ }
264
+ // kindsOk with engineDefaults ({} at construction = deferred,
265
+ // the ENGINE_TABLE at validate_engines = full).
266
+ export function kindsOk(flowId, stages, engineDefaults, cards) {
267
+ // The lattice gate is its own pass BEFORE kinds (
268
+ // docstring: "Runs BEFORE _validate_kinds").
269
+ for (const st of stages) {
270
+ if (st.card)
271
+ cardGate(flowId, st, cards);
272
+ }
273
+ // THE DERIVATION LAW (roadmap §12): a card stage's input kind
274
+ // DERIVES from the card's own flowKinds.inputs when the port is
275
+ // declared — the registry's hand-written expected is the override
276
+ // for initials the card cannot know, never a mirror to hand-copy
277
+ // and rot. A hand-written kind the card CONTRADICTS refuses LOUD
278
+ // (never compiles a lie).
279
+ let cardPorts = null;
280
+ for (const st of stages) {
281
+ if (!st.card)
282
+ continue;
283
+ const defs = needCardRegistry(cards, "kindsOk").defsSync();
284
+ const def = defs.get(st.card);
285
+ const ports = def?.flowKinds?.inputs;
286
+ // THE NAMING LAW's lattice half: a legacy alias derives its
287
+ // kind exactly like a declared port (old graphs keep
288
+ // validating) — but only `inputs` is ever a FACE; the alias
289
+ // never renders, never picks, never serves a tool param.
290
+ const aliases = def?.flowKinds?.legacyAliases;
291
+ const merged = aliases === undefined ? ports : { ...ports, ...aliases };
292
+ if (merged === undefined || Object.keys(merged).length === 0)
293
+ continue;
294
+ if (cardPorts === null)
295
+ cardPorts = new Map();
296
+ cardPorts.set(st.id, new Map(Object.entries(merged)));
297
+ }
298
+ const outputsByStage = new Map();
299
+ for (const st of stages) {
300
+ const effectiveExpected = new Map(st.expected.map((e) => [e.name, e.kind]));
301
+ const portKinds = cardPorts?.get(st.id);
302
+ const inputNames = new Set(st.inputs.map((i) => i.name));
303
+ // the contradiction sweep FIRST (a loud, named refusal beats
304
+ // the generic mismatch text for the same row)
305
+ if (portKinds !== undefined) {
306
+ for (const [name, kind] of effectiveExpected) {
307
+ const cardKind = portKinds.get(name);
308
+ if (cardKind !== undefined && cardKind !== kind) {
309
+ throw new FlowError("flow " +
310
+ JSON.stringify(flowId) +
311
+ " stage " +
312
+ JSON.stringify(st.id) +
313
+ " input " +
314
+ JSON.stringify(name) +
315
+ ": expected kind " +
316
+ JSON.stringify(kind) +
317
+ " contradicts card " +
318
+ JSON.stringify(st.card) +
319
+ " (the card declares " +
320
+ JSON.stringify(cardKind) +
321
+ ") — the card's own port declaration is the truth");
322
+ }
323
+ }
324
+ for (const name of inputNames) {
325
+ const cardKind = portKinds.get(name);
326
+ if (cardKind !== undefined && !effectiveExpected.has(name)) {
327
+ effectiveExpected.set(name, cardKind);
328
+ }
329
+ }
330
+ }
331
+ const expectedNames = new Set(effectiveExpected.keys());
332
+ const onlyInput = [...inputNames].filter((k) => !expectedNames.has(k)).sort();
333
+ const onlyExpected = [...expectedNames].filter((k) => !inputNames.has(k)).sort();
334
+ if (onlyInput.length > 0 || onlyExpected.length > 0) {
335
+ const bits = [];
336
+ if (onlyInput.length > 0)
337
+ bits.push("wired but untyped: " + JSON.stringify(onlyInput));
338
+ if (onlyExpected.length > 0)
339
+ bits.push("typed but unwired: " + JSON.stringify(onlyExpected));
340
+ throw new FlowError("flow " +
341
+ JSON.stringify(flowId) +
342
+ " stage " +
343
+ JSON.stringify(st.id) +
344
+ ": inputs/expected mismatch (" +
345
+ bits.join("; ") +
346
+ ") — every input needs an expected Kind, every expected " +
347
+ "Kind needs an input");
348
+ }
349
+ const resolved = new Map();
350
+ for (const o of st.outputs)
351
+ resolved.set(o.name, o.kind);
352
+ for (const o of st.cardIo)
353
+ resolved.set(o.name, o.kind);
354
+ if (resolved.size === 0 && !st.card) {
355
+ if (st.engine in engineDefaults) {
356
+
357
+
358
+
359
+ const def = engineDefaults[st.engine] ?? null;
360
+ if (def === null) {
361
+ throw new FlowError("flow " +
362
+ JSON.stringify(flowId) +
363
+ " stage " +
364
+ JSON.stringify(st.id) +
365
+ ": engine " +
366
+ JSON.stringify(st.engine) +
367
+ " declares no default_output and stage declares no " +
368
+ "outputs — add outputs={'default': <Kind>} on the stage, " +
369
+ "or set DEFAULT_OUTPUT on the engine");
370
+ }
371
+ /* v8 ignore stop */
372
+ resolved.set("default", def);
373
+ }
374
+ // else: defer; the validate_engines() pass fills it in.
375
+ }
376
+ if (resolved.size > 0)
377
+ outputsByStage.set(st.id, resolved);
378
+ // else: defer; the validate_engines() pass fills it in (a stage
379
+ // with no entry reads as upstream-unknown downstream — skipped,
380
+ // never refused — until the full pass).
381
+ for (const inp of st.inputs) {
382
+ if (inp.edge.kind !== "stage")
383
+ continue;
384
+ const upstream = outputsByStage.get(inp.edge.stage);
385
+ if (upstream === undefined)
386
+ continue; // later stage or deferred
387
+ if (!upstream.has(inp.edge.output)) {
388
+ throw new FlowError("flow " +
389
+ JSON.stringify(flowId) +
390
+ " stage " +
391
+ JSON.stringify(st.id) +
392
+ " input " +
393
+ JSON.stringify(inp.name) +
394
+ ": stage " +
395
+ JSON.stringify(inp.edge.stage) +
396
+ " has no output named " +
397
+ JSON.stringify(inp.edge.output) +
398
+ " (declared outputs: " +
399
+ JSON.stringify([...upstream.keys()].sort()) +
400
+ ")");
401
+ }
402
+ const upstreamKind = upstream.get(inp.edge.output); // the has() above proves presence
403
+ const receivingKind = effectiveExpected.get(inp.name); // the match above proves presence
404
+
405
+ // `_list` seat ACCUMULATES producers of its singular kind —
406
+ // an image producer feeds an image_list port, a video producer
407
+ // a video_list, an audio producer an audio_list (the picker's
408
+ // own edge law, stated at the lattice too; before this, the
409
+ // strict equality refused exactly the wire the picker offers).
410
+ if (upstreamKind !== receivingKind && upstreamKind + "_list" !== receivingKind) {
411
+ throw new FlowError("flow " +
412
+ JSON.stringify(flowId) +
413
+ " stage " +
414
+ JSON.stringify(st.id) +
415
+ " input " +
416
+ JSON.stringify(inp.name) +
417
+ ": expected " +
418
+ receivingKind +
419
+ ", got " +
420
+ upstreamKind +
421
+ " from stage " +
422
+ JSON.stringify(inp.edge.stage) +
423
+ " output " +
424
+ JSON.stringify(inp.edge.output) +
425
+ " — must be " +
426
+ receivingKind);
427
+ }
428
+ }
429
+ }
430
+ }
431
+ // validateEngines: every stage's engine through the
432
+ // bus — here the wall-pinned ENGINE_TABLE (existence + DEFAULT_OUTPUT);
433
+ // then the FULL kind pass. Unknown-engine text mirrors
434
+ // plugins.load_engine verbatim.
435
+ export function validateEngines(flow, cards,
436
+
437
+
438
+
439
+
440
+
441
+
442
+ knownEngines) {
443
+ const engineDefaults = {};
444
+ for (const st of flow.stages) {
445
+ if (knownEngines !== undefined && !knownEngines.includes(st.engine)) {
446
+ throw new FlowError("flow " +
447
+ JSON.stringify(flow.id) +
448
+ " stage " +
449
+ JSON.stringify(st.id) +
450
+ " names engine " +
451
+ JSON.stringify(st.engine) +
452
+ " — this gateway serves registered engines only (" +
453
+ (knownEngines.length === 0
454
+ ? "none registered"
455
+ : knownEngines.slice().sort().join(", ")) +
456
+ ")");
457
+ }
458
+ const row = st.engine.startsWith("mcp:") && engineIdOk(st.engine)
459
+ ? { plugin: "mcp-client", def: "image" }
460
+ : ENGINE_TABLE[st.engine];
461
+ if (row === undefined) {
462
+ throw new FlowError("flow " +
463
+ JSON.stringify(flow.id) +
464
+ " stage " +
465
+ JSON.stringify(st.id) +
466
+ ": engine " +
467
+ JSON.stringify(st.engine) +
468
+ " — no engine plugin for type " +
469
+ JSON.stringify(st.engine) +
470
+ " (no engine plugin declares it)");
471
+ }
472
+ engineDefaults[st.engine] = row.def;
473
+ }
474
+ kindsOk(flow.id, flow.stages, engineDefaults, cards);
475
+ return flow;
476
+ }
477
+ // flowFromBody: JSON body -> validated Flow.
478
+ export function flowFromBody(body, cards, knownEngines) {
479
+ if (typeof body !== "object" || body === null || Array.isArray(body)) {
480
+ throw new FlowError("flow body must be a JSON object");
481
+ }
482
+ const rec = body;
483
+ const dead = Object.keys(rec).filter((k) => !INLINE_TOP_SORTED.includes(k));
484
+ if (dead.length > 0) {
485
+ throw new FlowError("flow body carries unknown keys " +
486
+ JSON.stringify(dead) +
487
+ " — allowed: " +
488
+ JSON.stringify([...INLINE_TOP_SORTED]));
489
+ }
490
+ if (!("id" in rec) || !("stages" in rec)) {
491
+ throw new FlowError("flow body needs at least id and stages — got keys " + JSON.stringify(Object.keys(rec)));
492
+ }
493
+ const stagesIn = rec["stages"];
494
+ if (!Array.isArray(stagesIn) || stagesIn.length === 0) {
495
+ throw new FlowError("flow stages must be a non-empty list");
496
+ }
497
+ const rows = [];
498
+ for (let i = 0; i < stagesIn.length; i++) {
499
+ rows.push(buildStageRow(i, stagesIn[i]));
500
+ }
501
+ const flowId = String(rec["id"] ?? "");
502
+ // Flow construction: wires + deferred kinds.
503
+ if (flowId === "") {
504
+ throw new FlowError(minLengthRefusal("Flow", "id"));
505
+ }
506
+ wiresResolve(flowId, rows);
507
+ kindsOk(flowId, rows, {}, cards);
508
+ const flow = { id: flowId, description: String(rec["description"] ?? ""), stages: rows };
509
+ return validateEngines(flow, cards, knownEngines);
510
+ }
511
+ //# sourceMappingURL=runsValidateStages.js.map
@@ -0,0 +1,6 @@
1
+ import type { CardRegistryLike } from "./runsValidateShared.js";
2
+ export declare function batchRows(body: unknown, firstError: string, cards?: CardRegistryLike | null): Array<Record<string, unknown>>;
3
+ export declare function servedNodeClasses(cards?: CardRegistryLike | null): Set<string> | null;
4
+ export declare function validateWorkflow(params: unknown, cards?: CardRegistryLike | null): string[];
5
+ export declare function sweepWorkflows(body: unknown, cards?: CardRegistryLike | null): Array<Record<string, unknown>>;
6
+ //# sourceMappingURL=runsValidateSweeps.d.ts.map
@@ -0,0 +1,143 @@
1
+ // runsValidateSweeps.ts — batch-row + workflow-pin sweep validators split from
2
+ // runsValidate.ts (pure move, zero logic changes).
3
+ import { FlowError } from "./runsValidateShared.js";
4
+ import { flowFromBody } from "./runsValidateStages.js";
5
+ // ── batch rows ────────
6
+ export function batchRows(body, firstError, cards) {
7
+ const rows = [
8
+ { scope: "flow", error: firstError, code: "flow_compile", loc: "flow" },
9
+ ];
10
+ const stages = typeof body === "object" && body !== null && !Array.isArray(body)
11
+ ? body["stages"]
12
+ : undefined;
13
+ if (!Array.isArray(stages))
14
+ return rows;
15
+ for (let i = 0; i < stages.length; i++) {
16
+ const st = stages[i];
17
+ // a present null labels "null", not the default.
18
+ const label = typeof st === "object" && st !== null && !Array.isArray(st)
19
+ ? JSON.stringify("id" in st ? st["id"] : "stage[" + i + "]")
20
+ : "stage[" + i + "]";
21
+ if (typeof st === "object" && st !== null && !Array.isArray(st)) {
22
+ const stripped = {};
23
+ for (const [k, v] of Object.entries(st)) {
24
+ if (k !== "inputs" && k !== "expected")
25
+ stripped[k] = v;
26
+ }
27
+ try {
28
+ flowFromBody({ id: "probe", stages: [stripped] }, cards);
29
+ }
30
+ catch (e) {
31
+ if (e instanceof FlowError) {
32
+ rows.push({ stage: label, error: e.message, code: "flow_compile" });
33
+ }
34
+ else {
35
+ throw e;
36
+ }
37
+ }
38
+ }
39
+ }
40
+ for (const wrow of sweepWorkflows(body, cards))
41
+ rows.push(wrow);
42
+ return rows;
43
+ }
44
+ // ── pin sweep (validateWorkflow + servedNodeClasses, same file) ──
45
+ // STROKE 4b (the stroke-2a followup — same defect class): the
46
+ // module-level pinCache singleton (read-once, never refreshed) is
47
+ // DEAD. The pin now reads through ctx.cardRegistry.objectInfo()
48
+ // (the service owns the snapshot, mtime-keyed — a re-pin is never
49
+ // stale, strictly fresher than the old read-once cache).
50
+ //
51
+ // THE PIN'S THREE ANSWERS. An engine that answered is judged
52
+ // against what it said — including an answer of zero classes, which
53
+ // is an answer and stays fail-closed. Nobody able to ask (no cards
54
+ // seat for a non-plugin caller, or an owner whose objectInfo()
55
+ // stayed null because the engine never answered) yields null, and
56
+ // the sweep judges shape only: a node with no class_type is still a
57
+ // refusal, because that judgment needs no engine.
58
+ export function servedNodeClasses(cards) {
59
+ // NULL, NOT EMPTY, when no pin can judge a class: no cards seat (a
60
+ // non-plugin caller), or the owner is mounted but the engine never
61
+ // answered (objectInfo() null — the snapshot is primed by a live
62
+ // read). An empty set and an absent pin are different facts: the
63
+ // first is the engine serving no classes, the second is nobody
64
+ // able to ask. Collapsing them made a down engine refuse every
65
+ // valid workflow with "class_type X is not in the engine's served
66
+ // node set (0 known classes) — fix the class name or install the
67
+ // node" — a false accusation carrying a wrong instruction. The
68
+ // caller skips the membership judgment on null (the schema-only
69
+ // verdict the compose walk's sibling leg already takes when the
70
+ // engine is unreachable); the start door, which composes before
71
+ // queuing, still refuses a genuinely unserved class.
72
+ if (cards === null || cards === undefined)
73
+ return null;
74
+ const oi = cards.objectInfo();
75
+ if (oi === null)
76
+ return null;
77
+ return new Set(Object.keys(oi));
78
+ }
79
+ export function validateWorkflow(params, cards) {
80
+ // The sweep proves params is a non-null, non-array object (its guard
81
+ // continues otherwise) — the entry cast is honest.
82
+ const wf = params["workflow"];
83
+ if (typeof wf !== "object" || wf === null || Array.isArray(wf))
84
+ return [];
85
+ const wfRec = wf;
86
+ const raw = typeof wfRec["workflow"] === "object" && wfRec["workflow"] !== null && !Array.isArray(wfRec["workflow"])
87
+ ? wfRec["workflow"]
88
+ : wf;
89
+ const known = servedNodeClasses(cards);
90
+ const rows = [];
91
+ for (const [nodeId, node] of Object.entries(raw)) {
92
+ const cls = typeof node === "object" && node !== null && !Array.isArray(node)
93
+ ? node["class_type"]
94
+ : undefined;
95
+ if (cls === undefined || cls === null) {
96
+ rows.push("workflow node " + JSON.stringify(nodeId) + ": no class_type");
97
+ }
98
+ else if (known !== null && !known.has(typeof cls === "string" ? cls : String(cls))) {
99
+ rows.push("workflow node " +
100
+ JSON.stringify(nodeId) +
101
+ ": class_type " +
102
+ JSON.stringify(cls) +
103
+ " is not in the engine's served node set (" +
104
+ known.size +
105
+ " known classes) — fix the class name or install the node");
106
+ }
107
+ }
108
+ return rows;
109
+ }
110
+ export function sweepWorkflows(body, cards) {
111
+ const stages = typeof body === "object" && body !== null && !Array.isArray(body)
112
+ ? body["stages"]
113
+ :
114
+
115
+
116
+ undefined;
117
+
118
+ if (!Array.isArray(stages))
119
+ return [];
120
+ const rows = [];
121
+ for (let i = 0; i < stages.length; i++) {
122
+ const st = stages[i];
123
+ if (typeof st !== "object" || st === null || Array.isArray(st))
124
+ continue;
125
+ const rec = st;
126
+ const engine = rec["engine"];
127
+ const label = JSON.stringify("id" in rec ? rec["id"] : "stage[" + i + "]");
128
+ if (typeof engine !== "string")
129
+ continue;
130
+ // Only engines exporting validate_workflow are swept (comfyui today — validateWorkflow above owns the judgment).
131
+ // The bus try/except-continue collapses to this since the table is the bus here.
132
+ if (engine !== "comfyui")
133
+ continue;
134
+ if (typeof rec["params"] !== "object" || rec["params"] === null || Array.isArray(rec["params"])) {
135
+ continue;
136
+ }
137
+ for (const wrow of validateWorkflow(rec["params"], cards)) {
138
+ rows.push({ stage: label, error: wrow, code: "workflow_pin" });
139
+ }
140
+ }
141
+ return rows;
142
+ }
143
+ //# sourceMappingURL=runsValidateSweeps.js.map