@fbp/evaluator 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/evaluate.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { derivePorts, derivePropDefinitions, isRef, joinPath, parsePath, resolveRef } from '@fbp/spec';
1
2
  import { findCycle, formatCycle } from './cycles';
2
3
  /**
3
4
  * Build a definition map key from context, category, and name.
@@ -29,18 +30,48 @@ function parseTaskIdentifier(type) {
29
30
  * @returns Promise resolving to the value at the specified output port
30
31
  */
31
32
  export async function evaluate(graph, options) {
32
- return evaluateGraph(graph, options, []);
33
+ const outputs = await openGraph(graph, options, [])(options.outputNode);
34
+ return outputs[options.outputPort];
35
+ }
36
+ /**
37
+ * A `{ ref }` to a sibling node's value is a dependency, like an edge.
38
+ * `scopePath` is where `nodes` live, so absolute references are recognised too.
39
+ */
40
+ export function refEdges(nodes, scopePath = []) {
41
+ const names = new Set(nodes.map(n => n.name));
42
+ const scope = joinPath(scopePath);
43
+ const edges = [];
44
+ for (const node of nodes) {
45
+ for (const prop of node.props ?? []) {
46
+ if (!isRef(prop.value))
47
+ continue;
48
+ let target;
49
+ try {
50
+ target = resolveRef(scope, prop.value.ref);
51
+ }
52
+ catch {
53
+ continue;
54
+ }
55
+ const segments = parsePath(target.path);
56
+ const sibling = segments[segments.length - 1];
57
+ if (target.prop === undefined && sibling !== undefined && names.has(sibling) &&
58
+ joinPath(segments.slice(0, -1)) === scope) {
59
+ edges.push({ src: { node: sibling, port: 'value' }, dst: { node: node.name, port: prop.name } });
60
+ }
61
+ }
62
+ }
63
+ return edges;
33
64
  }
34
65
  /**
35
66
  * @param assetPath - definition keys of the digital assets being evaluated,
36
67
  * outermost first, so an asset that contains itself throws instead of
37
68
  * recursing forever.
38
69
  */
39
- async function evaluateGraph(graph, options, assetPath) {
40
- const { definitions, outputNode, outputPort, inputs = {}, props = {} } = options;
70
+ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
71
+ const { definitions, inputs = {}, props = {}, absent } = options;
41
72
  // Reject cycles up front, as the validator and the SQL gate do: pulling
42
73
  // through a loop would otherwise recurse until the stack overflows.
43
- const cycle = findCycle(graph.nodes.map(n => n.name), graph.edges);
74
+ const cycle = findCycle(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath)]);
44
75
  if (cycle) {
45
76
  throw new Error(`Graph "${graph.name}" contains a cycle: ${formatCycle(cycle)}`);
46
77
  }
@@ -71,6 +102,42 @@ async function evaluateGraph(graph, options, assetPath) {
71
102
  }
72
103
  // Cache for evaluated node outputs to avoid re-evaluation
73
104
  const cache = new Map();
105
+ /**
106
+ * Resolve a prop value: a `{ ref }` takes the value of the node it points at
107
+ * (its `value` output, or the named prop for a '.prop' reference). The target
108
+ * is in this scope or one that encloses it.
109
+ */
110
+ async function resolve(value, holder, chain = []) {
111
+ if (!isRef(value))
112
+ return value;
113
+ const target = resolveRef(joinPath(scopePath), value.ref);
114
+ const segments = parsePath(target.path);
115
+ const name = segments[segments.length - 1];
116
+ const owner = segments.slice(0, -1).join('/');
117
+ let found = scope;
118
+ while (found && found.path.join('/') !== owner)
119
+ found = found.parent;
120
+ if (!found || name === undefined || !found.getNode(name)) {
121
+ throw new Error(`Reference "${value.ref}" on node "${holder}" does not point at a node in its scope or an enclosing one`);
122
+ }
123
+ if (target.prop !== undefined) {
124
+ const key = `${target.path}.${target.prop}`;
125
+ if (chain.includes(key)) {
126
+ throw new Error(`Reference "${value.ref}" on node "${holder}" refers back to itself`);
127
+ }
128
+ const prop = found.getNode(name).props?.find(p => p.name === target.prop);
129
+ return found.resolve(prop?.value, name, [...chain, key]);
130
+ }
131
+ const outputs = await found.evaluateNode(name);
132
+ return 'value' in outputs ? outputs.value : Object.values(outputs)[0];
133
+ }
134
+ const scope = {
135
+ path: scopePath,
136
+ parent: parentScope,
137
+ getNode: name => nodeMap.get(name),
138
+ evaluateNode: name => evaluateNode(name),
139
+ resolve
140
+ };
74
141
  /**
75
142
  * Recursively evaluate a node and return all its outputs.
76
143
  * Fully async to support async node implementations.
@@ -90,6 +157,11 @@ async function evaluateGraph(graph, options, assetPath) {
90
157
  // Get the port name from the portName property
91
158
  const portNameProp = node.props?.find(p => p.name === 'portName');
92
159
  const inputName = portNameProp?.value || nodeName;
160
+ // A wired input whose arm was not taken stays dark, default or not, as it would in the flat graph.
161
+ if (absent?.has(inputName)) {
162
+ cache.set(nodeName, {});
163
+ return {};
164
+ }
93
165
  // Check external inputs first, then fall back to node's default prop
94
166
  let value = inputs[inputName];
95
167
  if (value === undefined) {
@@ -104,7 +176,10 @@ async function evaluateGraph(graph, options, assetPath) {
104
176
  // Get the prop name from the propName property
105
177
  const propNameProp = node.props?.find(p => p.name === 'propName');
106
178
  const propName = propNameProp?.value || nodeName;
107
- const value = props[propName];
179
+ let value = props[propName];
180
+ if (value === undefined) {
181
+ value = node.props?.find(p => p.name === 'default')?.value;
182
+ }
108
183
  const result = { value };
109
184
  cache.set(nodeName, result);
110
185
  return result;
@@ -117,6 +192,10 @@ async function evaluateGraph(graph, options, assetPath) {
117
192
  if (edges.length > 0) {
118
193
  const edge = edges[0];
119
194
  const upstreamOutputs = await evaluateNode(edge.src.node);
195
+ if (!(edge.src.port in upstreamOutputs)) {
196
+ cache.set(nodeName, {});
197
+ return {};
198
+ }
120
199
  value = upstreamOutputs[edge.src.port];
121
200
  }
122
201
  const result = { value };
@@ -124,48 +203,49 @@ async function evaluateGraph(graph, options, assetPath) {
124
203
  return result;
125
204
  }
126
205
  // Handle subnet nodes (type: 'subnet' with inline nodes/edges)
127
- if (node.type === 'subnet' && node.nodes && node.edges) {
206
+ if (node.type === 'subnet' && node.nodes) {
207
+ // The interface is derived from the boundary nodes inside the subnet.
208
+ const derived = derivePorts(node);
209
+ const inputPorts = node.inputs ?? derived.inputs;
210
+ const outputPorts = node.outputs ?? derived.outputs;
128
211
  // Collect inputs for the subnet by evaluating upstream nodes
129
212
  const subnetInputs = {};
213
+ const absentInputs = new Set();
130
214
  const subnetPortEdges = edgesByDst.get(nodeName);
131
- if (node.inputs && subnetPortEdges) {
132
- for (const inputPort of node.inputs) {
215
+ if (subnetPortEdges) {
216
+ for (const inputPort of inputPorts) {
133
217
  const edges = subnetPortEdges.get(inputPort.name) ?? [];
134
218
  if (edges.length > 0) {
135
219
  const edge = edges[0];
136
220
  const upstreamOutputs = await evaluateNode(edge.src.node);
137
- subnetInputs[inputPort.name] = upstreamOutputs[edge.src.port];
221
+ if (edge.src.port in upstreamOutputs) {
222
+ subnetInputs[inputPort.name] = upstreamOutputs[edge.src.port];
223
+ }
224
+ else {
225
+ absentInputs.add(inputPort.name);
226
+ }
138
227
  }
139
228
  }
140
229
  }
141
- // Find the output boundary nodes in the subnet
230
+ // Promoted parameters: the value set on the subnet node, else the graphProp's default.
231
+ const subnetProps = {};
232
+ for (const propDef of derivePropDefinitions(node)) {
233
+ const instanceProp = node.props?.find(p => p.name === propDef.name);
234
+ if (instanceProp !== undefined && instanceProp.value !== undefined) {
235
+ subnetProps[propDef.name] = await resolve(instanceProp.value, nodeName);
236
+ }
237
+ }
238
+ // One evaluation of the inner graph serves every output port.
239
+ const pull = openGraph({ name: `${nodeName}_subnet`, context: graphContext || undefined, nodes: node.nodes, edges: node.edges ?? [] }, { definitions, inputs: subnetInputs, props: subnetProps, absent: absentInputs }, assetPath, [...scopePath, nodeName], scope);
142
240
  const subnetOutputs = {};
143
- if (node.outputs) {
144
- for (const outputPort of node.outputs) {
145
- // Find the graphOutput boundary node by matching portName property
146
- const outputBoundaryNode = node.nodes.find(n => {
147
- if (n.type !== 'graphOutput')
148
- return false;
149
- const portNameProp = n.props?.find(p => p.name === 'portName');
150
- return portNameProp?.value === outputPort.name;
151
- });
152
- if (!outputBoundaryNode) {
153
- throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
154
- }
155
- // Recursively evaluate the subnet's internal graph
156
- const subnetResult = await evaluateGraph({
157
- name: `${nodeName}_subnet`,
158
- context: graphContext || undefined,
159
- nodes: node.nodes,
160
- edges: node.edges
161
- }, {
162
- definitions,
163
- outputNode: outputBoundaryNode.name,
164
- outputPort: 'value',
165
- inputs: subnetInputs
166
- }, assetPath);
167
- subnetOutputs[outputPort.name] = subnetResult;
241
+ for (const outputPort of outputPorts) {
242
+ const outputBoundaryNode = node.nodes.find(n => n.type === 'graphOutput' && n.props?.find(p => p.name === 'portName')?.value === outputPort.name);
243
+ if (!outputBoundaryNode) {
244
+ throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
168
245
  }
246
+ const result = await pull(outputBoundaryNode.name);
247
+ if ('value' in result)
248
+ subnetOutputs[outputPort.name] = result.value;
169
249
  }
170
250
  cache.set(nodeName, subnetOutputs);
171
251
  return subnetOutputs;
@@ -249,7 +329,7 @@ async function evaluateGraph(graph, options, assetPath) {
249
329
  for (const propDef of definition.props) {
250
330
  const instanceProp = node.props?.find(p => p.name === propDef.name);
251
331
  if (instanceProp !== undefined && instanceProp.value !== undefined) {
252
- nodeProps[propDef.name] = instanceProp.value;
332
+ nodeProps[propDef.name] = await resolve(instanceProp.value, nodeName);
253
333
  }
254
334
  else if (propDef.default !== undefined) {
255
335
  nodeProps[propDef.name] = propDef.default;
@@ -262,14 +342,14 @@ async function evaluateGraph(graph, options, assetPath) {
262
342
  throw new Error(`Digital asset "${nodeContext}:${node.type}" contains itself: ${formatCycle([...assetPath, key])}`);
263
343
  }
264
344
  const assetGraph = definition.graph;
265
- // Find all output boundary nodes in the asset's graph
345
+ // One evaluation of the asset's graph serves every output port.
346
+ const pull = openGraph({ name: `${nodeName}_asset`, context: assetGraph.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, inputs: nodeInputs, props: nodeProps }, [...assetPath, key]);
266
347
  const assetOutputs = {};
267
- const outputBoundaryNodes = assetGraph.nodes.filter(n => n.type === 'graphOutput');
268
- for (const outputBoundaryNode of outputBoundaryNodes) {
348
+ for (const outputBoundaryNode of assetGraph.nodes.filter(n => n.type === 'graphOutput')) {
269
349
  const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
270
- const portName = portNameProp?.value || outputBoundaryNode.name;
271
- const result = await evaluateGraph({ name: `${nodeName}_asset`, context: assetGraph.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, outputNode: outputBoundaryNode.name, outputPort: 'value', inputs: nodeInputs, props: nodeProps }, [...assetPath, key]);
272
- assetOutputs[portName] = result;
350
+ const result = await pull(outputBoundaryNode.name);
351
+ if ('value' in result)
352
+ assetOutputs[portNameProp?.value || outputBoundaryNode.name] = result.value;
273
353
  }
274
354
  cache.set(nodeName, assetOutputs);
275
355
  return assetOutputs;
@@ -282,7 +362,5 @@ async function evaluateGraph(graph, options, assetPath) {
282
362
  cache.set(nodeName, outputs);
283
363
  return outputs;
284
364
  }
285
- // Evaluate starting from the output node
286
- const outputs = await evaluateNode(outputNode);
287
- return outputs[outputPort];
365
+ return evaluateNode;
288
366
  }
package/esm/validate.js CHANGED
@@ -1,4 +1,6 @@
1
+ import { derivePorts, derivePropDefinitions } from '@fbp/spec';
1
2
  import { findCycle, formatCycle } from './cycles';
3
+ import { refEdges } from './evaluate';
2
4
  /**
3
5
  * Boundary node types. These are satisfied by the runtime (graphInput/graphProp
4
6
  * receive values from the execution payload; graphOutput is a pass-through), so
@@ -85,12 +87,12 @@ export function validateGraph(graph, options = {}) {
85
87
  // Digital assets currently being visited, outermost first, so an asset that
86
88
  // contains itself is reported instead of recursed into forever.
87
89
  const assetStack = [];
88
- const visit = (g, pathPrefix) => {
90
+ const visit = (g, pathPrefix, scope = []) => {
89
91
  // Definitions can be embedded per-graph (digital assets carry their own).
90
92
  registerDefs(g.definitions);
91
93
  const graphContext = g.context || '';
92
94
  const connected = connectedPorts(g.edges ?? []);
93
- const cycle = findCycle((g.nodes ?? []).map(n => n.name), g.edges ?? []);
95
+ const cycle = findCycle((g.nodes ?? []).map(n => n.name), [...(g.edges ?? []), ...refEdges(g.nodes ?? [], scope)]);
94
96
  if (cycle) {
95
97
  const path = cycle.map(name => qualify(pathPrefix, name));
96
98
  errors.push({
@@ -107,8 +109,11 @@ export function validateGraph(graph, options = {}) {
107
109
  const nodeConnected = connected.get(node.name) ?? new Set();
108
110
  // Inline subnet: validate against its declared interface, then recurse.
109
111
  if (node.type === 'subnet' && node.nodes) {
110
- for (const port of node.inputs ?? []) {
111
- if (port.optional === true || port.multi === true)
112
+ const defaulted = new Set(node.nodes
113
+ .filter(n => n.type === 'graphInput' && nodeHasProp(n, 'default'))
114
+ .map(n => n.props.find(p => p.name === 'portName')?.value ?? n.name));
115
+ for (const port of node.inputs ?? derivePorts(node).inputs) {
116
+ if (port.optional === true || port.multi === true || defaulted.has(port.name))
112
117
  continue;
113
118
  if (!nodeConnected.has(port.name)) {
114
119
  errors.push({
@@ -119,7 +124,19 @@ export function validateGraph(graph, options = {}) {
119
124
  });
120
125
  }
121
126
  }
122
- visit({ name: `${node.name}_subnet`, context: graphContext || undefined, nodes: node.nodes, edges: node.edges ?? [] }, qualifiedName);
127
+ for (const prop of derivePropDefinitions(node)) {
128
+ if (prop.required !== true || prop.default !== undefined)
129
+ continue;
130
+ if (!nodeHasProp(node, prop.name)) {
131
+ errors.push({
132
+ code: 'MISSING_REQUIRED_PROP',
133
+ node: qualifiedName,
134
+ prop: prop.name,
135
+ message: `Required prop "${prop.name}" of subnet "${qualifiedName}" is not set`,
136
+ });
137
+ }
138
+ }
139
+ visit({ name: `${node.name}_subnet`, context: graphContext || undefined, nodes: node.nodes, edges: node.edges ?? [] }, qualifiedName, [...scope, node.name]);
123
140
  continue;
124
141
  }
125
142
  const nodeContext = node.context || graphContext;
package/evaluate.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Graph } from '@fbp/types';
1
+ import type { Edge, Graph, Node } from '@fbp/types';
2
2
  import type { EvaluateOptions } from './types';
3
3
  /**
4
4
  * Evaluate a graph starting from the specified output node/port.
@@ -10,3 +10,8 @@ import type { EvaluateOptions } from './types';
10
10
  * @returns Promise resolving to the value at the specified output port
11
11
  */
12
12
  export declare function evaluate(graph: Graph, options: EvaluateOptions): Promise<any>;
13
+ /**
14
+ * A `{ ref }` to a sibling node's value is a dependency, like an edge.
15
+ * `scopePath` is where `nodes` live, so absolute references are recognised too.
16
+ */
17
+ export declare function refEdges(nodes: Node[], scopePath?: string[]): Edge[];
package/evaluate.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.evaluate = evaluate;
4
+ exports.refEdges = refEdges;
5
+ const spec_1 = require("@fbp/spec");
4
6
  const cycles_1 = require("./cycles");
5
7
  /**
6
8
  * Build a definition map key from context, category, and name.
@@ -32,18 +34,48 @@ function parseTaskIdentifier(type) {
32
34
  * @returns Promise resolving to the value at the specified output port
33
35
  */
34
36
  async function evaluate(graph, options) {
35
- return evaluateGraph(graph, options, []);
37
+ const outputs = await openGraph(graph, options, [])(options.outputNode);
38
+ return outputs[options.outputPort];
39
+ }
40
+ /**
41
+ * A `{ ref }` to a sibling node's value is a dependency, like an edge.
42
+ * `scopePath` is where `nodes` live, so absolute references are recognised too.
43
+ */
44
+ function refEdges(nodes, scopePath = []) {
45
+ const names = new Set(nodes.map(n => n.name));
46
+ const scope = (0, spec_1.joinPath)(scopePath);
47
+ const edges = [];
48
+ for (const node of nodes) {
49
+ for (const prop of node.props ?? []) {
50
+ if (!(0, spec_1.isRef)(prop.value))
51
+ continue;
52
+ let target;
53
+ try {
54
+ target = (0, spec_1.resolveRef)(scope, prop.value.ref);
55
+ }
56
+ catch {
57
+ continue;
58
+ }
59
+ const segments = (0, spec_1.parsePath)(target.path);
60
+ const sibling = segments[segments.length - 1];
61
+ if (target.prop === undefined && sibling !== undefined && names.has(sibling) &&
62
+ (0, spec_1.joinPath)(segments.slice(0, -1)) === scope) {
63
+ edges.push({ src: { node: sibling, port: 'value' }, dst: { node: node.name, port: prop.name } });
64
+ }
65
+ }
66
+ }
67
+ return edges;
36
68
  }
37
69
  /**
38
70
  * @param assetPath - definition keys of the digital assets being evaluated,
39
71
  * outermost first, so an asset that contains itself throws instead of
40
72
  * recursing forever.
41
73
  */
42
- async function evaluateGraph(graph, options, assetPath) {
43
- const { definitions, outputNode, outputPort, inputs = {}, props = {} } = options;
74
+ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
75
+ const { definitions, inputs = {}, props = {}, absent } = options;
44
76
  // Reject cycles up front, as the validator and the SQL gate do: pulling
45
77
  // through a loop would otherwise recurse until the stack overflows.
46
- const cycle = (0, cycles_1.findCycle)(graph.nodes.map(n => n.name), graph.edges);
78
+ const cycle = (0, cycles_1.findCycle)(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath)]);
47
79
  if (cycle) {
48
80
  throw new Error(`Graph "${graph.name}" contains a cycle: ${(0, cycles_1.formatCycle)(cycle)}`);
49
81
  }
@@ -74,6 +106,42 @@ async function evaluateGraph(graph, options, assetPath) {
74
106
  }
75
107
  // Cache for evaluated node outputs to avoid re-evaluation
76
108
  const cache = new Map();
109
+ /**
110
+ * Resolve a prop value: a `{ ref }` takes the value of the node it points at
111
+ * (its `value` output, or the named prop for a '.prop' reference). The target
112
+ * is in this scope or one that encloses it.
113
+ */
114
+ async function resolve(value, holder, chain = []) {
115
+ if (!(0, spec_1.isRef)(value))
116
+ return value;
117
+ const target = (0, spec_1.resolveRef)((0, spec_1.joinPath)(scopePath), value.ref);
118
+ const segments = (0, spec_1.parsePath)(target.path);
119
+ const name = segments[segments.length - 1];
120
+ const owner = segments.slice(0, -1).join('/');
121
+ let found = scope;
122
+ while (found && found.path.join('/') !== owner)
123
+ found = found.parent;
124
+ if (!found || name === undefined || !found.getNode(name)) {
125
+ throw new Error(`Reference "${value.ref}" on node "${holder}" does not point at a node in its scope or an enclosing one`);
126
+ }
127
+ if (target.prop !== undefined) {
128
+ const key = `${target.path}.${target.prop}`;
129
+ if (chain.includes(key)) {
130
+ throw new Error(`Reference "${value.ref}" on node "${holder}" refers back to itself`);
131
+ }
132
+ const prop = found.getNode(name).props?.find(p => p.name === target.prop);
133
+ return found.resolve(prop?.value, name, [...chain, key]);
134
+ }
135
+ const outputs = await found.evaluateNode(name);
136
+ return 'value' in outputs ? outputs.value : Object.values(outputs)[0];
137
+ }
138
+ const scope = {
139
+ path: scopePath,
140
+ parent: parentScope,
141
+ getNode: name => nodeMap.get(name),
142
+ evaluateNode: name => evaluateNode(name),
143
+ resolve
144
+ };
77
145
  /**
78
146
  * Recursively evaluate a node and return all its outputs.
79
147
  * Fully async to support async node implementations.
@@ -93,6 +161,11 @@ async function evaluateGraph(graph, options, assetPath) {
93
161
  // Get the port name from the portName property
94
162
  const portNameProp = node.props?.find(p => p.name === 'portName');
95
163
  const inputName = portNameProp?.value || nodeName;
164
+ // A wired input whose arm was not taken stays dark, default or not, as it would in the flat graph.
165
+ if (absent?.has(inputName)) {
166
+ cache.set(nodeName, {});
167
+ return {};
168
+ }
96
169
  // Check external inputs first, then fall back to node's default prop
97
170
  let value = inputs[inputName];
98
171
  if (value === undefined) {
@@ -107,7 +180,10 @@ async function evaluateGraph(graph, options, assetPath) {
107
180
  // Get the prop name from the propName property
108
181
  const propNameProp = node.props?.find(p => p.name === 'propName');
109
182
  const propName = propNameProp?.value || nodeName;
110
- const value = props[propName];
183
+ let value = props[propName];
184
+ if (value === undefined) {
185
+ value = node.props?.find(p => p.name === 'default')?.value;
186
+ }
111
187
  const result = { value };
112
188
  cache.set(nodeName, result);
113
189
  return result;
@@ -120,6 +196,10 @@ async function evaluateGraph(graph, options, assetPath) {
120
196
  if (edges.length > 0) {
121
197
  const edge = edges[0];
122
198
  const upstreamOutputs = await evaluateNode(edge.src.node);
199
+ if (!(edge.src.port in upstreamOutputs)) {
200
+ cache.set(nodeName, {});
201
+ return {};
202
+ }
123
203
  value = upstreamOutputs[edge.src.port];
124
204
  }
125
205
  const result = { value };
@@ -127,48 +207,49 @@ async function evaluateGraph(graph, options, assetPath) {
127
207
  return result;
128
208
  }
129
209
  // Handle subnet nodes (type: 'subnet' with inline nodes/edges)
130
- if (node.type === 'subnet' && node.nodes && node.edges) {
210
+ if (node.type === 'subnet' && node.nodes) {
211
+ // The interface is derived from the boundary nodes inside the subnet.
212
+ const derived = (0, spec_1.derivePorts)(node);
213
+ const inputPorts = node.inputs ?? derived.inputs;
214
+ const outputPorts = node.outputs ?? derived.outputs;
131
215
  // Collect inputs for the subnet by evaluating upstream nodes
132
216
  const subnetInputs = {};
217
+ const absentInputs = new Set();
133
218
  const subnetPortEdges = edgesByDst.get(nodeName);
134
- if (node.inputs && subnetPortEdges) {
135
- for (const inputPort of node.inputs) {
219
+ if (subnetPortEdges) {
220
+ for (const inputPort of inputPorts) {
136
221
  const edges = subnetPortEdges.get(inputPort.name) ?? [];
137
222
  if (edges.length > 0) {
138
223
  const edge = edges[0];
139
224
  const upstreamOutputs = await evaluateNode(edge.src.node);
140
- subnetInputs[inputPort.name] = upstreamOutputs[edge.src.port];
225
+ if (edge.src.port in upstreamOutputs) {
226
+ subnetInputs[inputPort.name] = upstreamOutputs[edge.src.port];
227
+ }
228
+ else {
229
+ absentInputs.add(inputPort.name);
230
+ }
141
231
  }
142
232
  }
143
233
  }
144
- // Find the output boundary nodes in the subnet
234
+ // Promoted parameters: the value set on the subnet node, else the graphProp's default.
235
+ const subnetProps = {};
236
+ for (const propDef of (0, spec_1.derivePropDefinitions)(node)) {
237
+ const instanceProp = node.props?.find(p => p.name === propDef.name);
238
+ if (instanceProp !== undefined && instanceProp.value !== undefined) {
239
+ subnetProps[propDef.name] = await resolve(instanceProp.value, nodeName);
240
+ }
241
+ }
242
+ // One evaluation of the inner graph serves every output port.
243
+ const pull = openGraph({ name: `${nodeName}_subnet`, context: graphContext || undefined, nodes: node.nodes, edges: node.edges ?? [] }, { definitions, inputs: subnetInputs, props: subnetProps, absent: absentInputs }, assetPath, [...scopePath, nodeName], scope);
145
244
  const subnetOutputs = {};
146
- if (node.outputs) {
147
- for (const outputPort of node.outputs) {
148
- // Find the graphOutput boundary node by matching portName property
149
- const outputBoundaryNode = node.nodes.find(n => {
150
- if (n.type !== 'graphOutput')
151
- return false;
152
- const portNameProp = n.props?.find(p => p.name === 'portName');
153
- return portNameProp?.value === outputPort.name;
154
- });
155
- if (!outputBoundaryNode) {
156
- throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
157
- }
158
- // Recursively evaluate the subnet's internal graph
159
- const subnetResult = await evaluateGraph({
160
- name: `${nodeName}_subnet`,
161
- context: graphContext || undefined,
162
- nodes: node.nodes,
163
- edges: node.edges
164
- }, {
165
- definitions,
166
- outputNode: outputBoundaryNode.name,
167
- outputPort: 'value',
168
- inputs: subnetInputs
169
- }, assetPath);
170
- subnetOutputs[outputPort.name] = subnetResult;
245
+ for (const outputPort of outputPorts) {
246
+ const outputBoundaryNode = node.nodes.find(n => n.type === 'graphOutput' && n.props?.find(p => p.name === 'portName')?.value === outputPort.name);
247
+ if (!outputBoundaryNode) {
248
+ throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
171
249
  }
250
+ const result = await pull(outputBoundaryNode.name);
251
+ if ('value' in result)
252
+ subnetOutputs[outputPort.name] = result.value;
172
253
  }
173
254
  cache.set(nodeName, subnetOutputs);
174
255
  return subnetOutputs;
@@ -252,7 +333,7 @@ async function evaluateGraph(graph, options, assetPath) {
252
333
  for (const propDef of definition.props) {
253
334
  const instanceProp = node.props?.find(p => p.name === propDef.name);
254
335
  if (instanceProp !== undefined && instanceProp.value !== undefined) {
255
- nodeProps[propDef.name] = instanceProp.value;
336
+ nodeProps[propDef.name] = await resolve(instanceProp.value, nodeName);
256
337
  }
257
338
  else if (propDef.default !== undefined) {
258
339
  nodeProps[propDef.name] = propDef.default;
@@ -265,14 +346,14 @@ async function evaluateGraph(graph, options, assetPath) {
265
346
  throw new Error(`Digital asset "${nodeContext}:${node.type}" contains itself: ${(0, cycles_1.formatCycle)([...assetPath, key])}`);
266
347
  }
267
348
  const assetGraph = definition.graph;
268
- // Find all output boundary nodes in the asset's graph
349
+ // One evaluation of the asset's graph serves every output port.
350
+ const pull = openGraph({ name: `${nodeName}_asset`, context: assetGraph.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, inputs: nodeInputs, props: nodeProps }, [...assetPath, key]);
269
351
  const assetOutputs = {};
270
- const outputBoundaryNodes = assetGraph.nodes.filter(n => n.type === 'graphOutput');
271
- for (const outputBoundaryNode of outputBoundaryNodes) {
352
+ for (const outputBoundaryNode of assetGraph.nodes.filter(n => n.type === 'graphOutput')) {
272
353
  const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
273
- const portName = portNameProp?.value || outputBoundaryNode.name;
274
- const result = await evaluateGraph({ name: `${nodeName}_asset`, context: assetGraph.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, outputNode: outputBoundaryNode.name, outputPort: 'value', inputs: nodeInputs, props: nodeProps }, [...assetPath, key]);
275
- assetOutputs[portName] = result;
354
+ const result = await pull(outputBoundaryNode.name);
355
+ if ('value' in result)
356
+ assetOutputs[portNameProp?.value || outputBoundaryNode.name] = result.value;
276
357
  }
277
358
  cache.set(nodeName, assetOutputs);
278
359
  return assetOutputs;
@@ -285,7 +366,5 @@ async function evaluateGraph(graph, options, assetPath) {
285
366
  cache.set(nodeName, outputs);
286
367
  return outputs;
287
368
  }
288
- // Evaluate starting from the output node
289
- const outputs = await evaluateNode(outputNode);
290
- return outputs[outputPort];
369
+ return evaluateNode;
291
370
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fbp/evaluator",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "FBP graph evaluator — node definitions, graph evaluation and validation",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "license": "MIT",
@@ -12,7 +12,8 @@
12
12
  "directory": "dist"
13
13
  },
14
14
  "dependencies": {
15
- "@fbp/types": "1.4.1"
15
+ "@fbp/spec": "1.4.0",
16
+ "@fbp/types": "1.5.0"
16
17
  },
17
18
  "devDependencies": {
18
19
  "@types/jest": "^30.0.0",
package/validate.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateGraph = validateGraph;
4
+ const spec_1 = require("@fbp/spec");
4
5
  const cycles_1 = require("./cycles");
6
+ const evaluate_1 = require("./evaluate");
5
7
  /**
6
8
  * Boundary node types. These are satisfied by the runtime (graphInput/graphProp
7
9
  * receive values from the execution payload; graphOutput is a pass-through), so
@@ -88,12 +90,12 @@ function validateGraph(graph, options = {}) {
88
90
  // Digital assets currently being visited, outermost first, so an asset that
89
91
  // contains itself is reported instead of recursed into forever.
90
92
  const assetStack = [];
91
- const visit = (g, pathPrefix) => {
93
+ const visit = (g, pathPrefix, scope = []) => {
92
94
  // Definitions can be embedded per-graph (digital assets carry their own).
93
95
  registerDefs(g.definitions);
94
96
  const graphContext = g.context || '';
95
97
  const connected = connectedPorts(g.edges ?? []);
96
- const cycle = (0, cycles_1.findCycle)((g.nodes ?? []).map(n => n.name), g.edges ?? []);
98
+ const cycle = (0, cycles_1.findCycle)((g.nodes ?? []).map(n => n.name), [...(g.edges ?? []), ...(0, evaluate_1.refEdges)(g.nodes ?? [], scope)]);
97
99
  if (cycle) {
98
100
  const path = cycle.map(name => qualify(pathPrefix, name));
99
101
  errors.push({
@@ -110,8 +112,11 @@ function validateGraph(graph, options = {}) {
110
112
  const nodeConnected = connected.get(node.name) ?? new Set();
111
113
  // Inline subnet: validate against its declared interface, then recurse.
112
114
  if (node.type === 'subnet' && node.nodes) {
113
- for (const port of node.inputs ?? []) {
114
- if (port.optional === true || port.multi === true)
115
+ const defaulted = new Set(node.nodes
116
+ .filter(n => n.type === 'graphInput' && nodeHasProp(n, 'default'))
117
+ .map(n => n.props.find(p => p.name === 'portName')?.value ?? n.name));
118
+ for (const port of node.inputs ?? (0, spec_1.derivePorts)(node).inputs) {
119
+ if (port.optional === true || port.multi === true || defaulted.has(port.name))
115
120
  continue;
116
121
  if (!nodeConnected.has(port.name)) {
117
122
  errors.push({
@@ -122,7 +127,19 @@ function validateGraph(graph, options = {}) {
122
127
  });
123
128
  }
124
129
  }
125
- visit({ name: `${node.name}_subnet`, context: graphContext || undefined, nodes: node.nodes, edges: node.edges ?? [] }, qualifiedName);
130
+ for (const prop of (0, spec_1.derivePropDefinitions)(node)) {
131
+ if (prop.required !== true || prop.default !== undefined)
132
+ continue;
133
+ if (!nodeHasProp(node, prop.name)) {
134
+ errors.push({
135
+ code: 'MISSING_REQUIRED_PROP',
136
+ node: qualifiedName,
137
+ prop: prop.name,
138
+ message: `Required prop "${prop.name}" of subnet "${qualifiedName}" is not set`,
139
+ });
140
+ }
141
+ }
142
+ visit({ name: `${node.name}_subnet`, context: graphContext || undefined, nodes: node.nodes, edges: node.edges ?? [] }, qualifiedName, [...scope, node.name]);
126
143
  continue;
127
144
  }
128
145
  const nodeContext = node.context || graphContext;