@fbp/evaluator 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/evaluate.js CHANGED
@@ -1,4 +1,4 @@
1
- import { assertTypes, definitionKey, derivePorts, derivePropDefinitions, GRAPH_INPUT, GRAPH_OUTPUT, GRAPH_PROP, hoistDefinitions, isRef, joinPath, parsePath, resolveRef, SUBNET, typeKey } from '@fbp/spec';
1
+ import { assertTypes, definitionKey, derivePorts, derivePropDefinitions, GRAPH_INPUT, GRAPH_OUTPUT, GRAPH_PROP, boundaryName, hoistDefinitions, isLiteral, isRef, joinPath, parsePath, plainValue, resolveRef, SUBNET, typeKey, unwiredInput } from '@fbp/spec';
2
2
  import { findCycle, formatCycle } from './cycles';
3
3
  /**
4
4
  * Evaluate a graph starting from the specified output node/port.
@@ -26,32 +26,118 @@ export async function evaluate(graph, options) {
26
26
  /**
27
27
  * A `{ ref }` to a sibling node's value is a dependency, like an edge.
28
28
  * `scopePath` is where `nodes` live, so absolute references are recognised too.
29
+ *
30
+ * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
31
+ * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
32
+ * the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
33
+ * Inner nodes that no output of the subnet pulls are never evaluated, so theirs do not count.
29
34
  */
30
- export function refEdges(nodes, scopePath = []) {
35
+ export function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
31
36
  const names = new Set(nodes.map(n => n.name));
32
37
  const scope = joinPath(scopePath);
33
38
  const edges = [];
34
- for (const node of nodes) {
35
- for (const prop of node.props ?? []) {
39
+ const find = (segments) => {
40
+ if (segments.length <= scopePath.length || scopePath.some((part, i) => segments[i] !== part))
41
+ return undefined;
42
+ let level = nodes;
43
+ let found;
44
+ for (const part of segments.slice(scopePath.length)) {
45
+ found = level.find(n => n.name === part);
46
+ if (!found)
47
+ return undefined;
48
+ level = found.nodes ?? [];
49
+ }
50
+ return found;
51
+ };
52
+ // The siblings a reference reads. A `.prop` reference reads whatever that prop refers to, wired or not.
53
+ const follow = (value, from, seen) => {
54
+ if (!isRef(value))
55
+ return [];
56
+ let target;
57
+ try {
58
+ target = resolveRef(joinPath(from), value.ref);
59
+ }
60
+ catch {
61
+ return [];
62
+ }
63
+ const segments = parsePath(target.path);
64
+ const owner = segments.slice(0, -1);
65
+ if (target.prop === undefined) {
66
+ const sibling = segments[segments.length - 1];
67
+ return sibling !== undefined && names.has(sibling) && joinPath(owner) === scope ? [sibling] : [];
68
+ }
69
+ const key = `${target.path}.${target.prop}`;
70
+ if (seen.includes(key))
71
+ return [];
72
+ return follow(find(segments)?.props?.find(p => p.name === target.prop)?.value, owner, [...seen, key]);
73
+ };
74
+ const collect = (holder, holderScope, dependent, wires, context) => {
75
+ for (const prop of holder.props ?? []) {
36
76
  if (!isRef(prop.value))
37
77
  continue;
38
- let target;
39
- try {
40
- target = resolveRef(scope, prop.value.ref);
41
- }
42
- catch {
78
+ // A typed value on an input that is wired is never read, unless the type also declares a prop of that name.
79
+ if (wiring && holder.type !== SUBNET &&
80
+ wires.some(wire => wire.dst.node === holder.name && wire.dst.port === prop.name) &&
81
+ !wiring.declaresProp(holder.context || context, holder.type, prop.name))
43
82
  continue;
44
- }
45
- const segments = parsePath(target.path);
46
- const sibling = segments[segments.length - 1];
47
- if (target.prop === undefined && sibling !== undefined && names.has(sibling) &&
48
- joinPath(segments.slice(0, -1)) === scope) {
49
- edges.push({ src: { node: sibling, port: 'value' }, dst: { node: node.name, port: prop.name } });
83
+ for (const sibling of follow(prop.value, holderScope, [])) {
84
+ edges.push({ src: { node: sibling, port: 'value' }, dst: { node: dependent, port: prop.name } });
50
85
  }
51
86
  }
52
- }
87
+ if (holder.type !== SUBNET)
88
+ return;
89
+ const inner = [...holderScope, holder.name];
90
+ const innerContext = holder.context || context;
91
+ const innerWiring = wiring && { ...wiring, edges: holder.edges ?? [], context: innerContext };
92
+ // Only the nodes its outputs pull are read: a reference on any other inner node is never resolved.
93
+ // Keyed by where the subnet sits: the same node object placed twice reads by its own scope and context.
94
+ const at = JSON.stringify(inner);
95
+ let read = reads.get(at);
96
+ if (!read) {
97
+ read = pulled(holder.nodes ?? [], [...(holder.edges ?? []), ...refEdges(holder.nodes ?? [], inner, innerWiring, reads)]);
98
+ reads.set(at, read);
99
+ }
100
+ for (const child of holder.nodes ?? []) {
101
+ if (read.has(child.name))
102
+ collect(child, inner, dependent, holder.edges ?? [], innerContext);
103
+ }
104
+ };
105
+ for (const node of nodes)
106
+ collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
53
107
  return edges;
54
108
  }
109
+ /** The nodes a subnet evaluates to produce its outputs: its graphOutputs and everything upstream of them. */
110
+ function pulled(nodes, edges) {
111
+ const upstream = new Map();
112
+ for (const edge of edges) {
113
+ const sources = upstream.get(edge.dst.node);
114
+ if (sources)
115
+ sources.push(edge.src.node);
116
+ else
117
+ upstream.set(edge.dst.node, [edge.src.node]);
118
+ }
119
+ const read = new Set();
120
+ const pending = nodes.filter(n => n.type === GRAPH_OUTPUT).map(n => n.name);
121
+ while (pending.length) {
122
+ const name = pending.pop();
123
+ if (read.has(name))
124
+ continue;
125
+ read.add(name);
126
+ pending.push(...(upstream.get(name) ?? []));
127
+ }
128
+ return read;
129
+ }
130
+ /** `Wiring` for a scope, answered from its definitions. */
131
+ export function wiringOf(edges, context, definitions) {
132
+ return {
133
+ edges,
134
+ context,
135
+ declaresProp: (ctx, type, prop) => {
136
+ const definition = definitions.get(typeKey(ctx, type));
137
+ return !definition || (definition.props ?? []).some(def => def.name === prop);
138
+ }
139
+ };
140
+ }
55
141
  /**
56
142
  * @param assetPath - definition keys of the digital assets being evaluated,
57
143
  * outermost first, so an asset that contains itself throws instead of
@@ -62,7 +148,11 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
62
148
  const { definitions } = options;
63
149
  // Reject cycles up front, as the validator and the SQL gate do: pulling
64
150
  // through a loop would otherwise recurse until the stack overflows.
65
- const cycle = findCycle(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath)]);
151
+ const defMap = new Map();
152
+ for (const def of definitions) {
153
+ defMap.set(definitionKey(def), def);
154
+ }
155
+ const cycle = findCycle(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath, wiringOf(graph.edges, graph.context || '', defMap))]);
66
156
  if (cycle) {
67
157
  throw new Error(`Graph "${graph.name}" contains a cycle: ${formatCycle(cycle)}`);
68
158
  }
@@ -71,10 +161,6 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
71
161
  for (const node of graph.nodes) {
72
162
  nodeMap.set(node.name, node);
73
163
  }
74
- const defMap = new Map();
75
- for (const def of definitions) {
76
- defMap.set(definitionKey(def), def);
77
- }
78
164
  // Graph-level context used as default for node lookups
79
165
  const graphContext = graph.context || '';
80
166
  // Build edge lookup: destination node -> destination port -> edges (in array order)
@@ -96,9 +182,12 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
96
182
  /**
97
183
  * Resolve a prop value: a `{ ref }` takes the value of the node it points at
98
184
  * (its `value` output, or the named prop for a '.prop' reference). The target
99
- * is in this scope or one that encloses it.
185
+ * is in this scope or one that encloses it. A `{ literal }` is its content,
186
+ * verbatim.
100
187
  */
101
188
  async function resolve(value, holder, chain = []) {
189
+ if (isLiteral(value))
190
+ return value.literal;
102
191
  if (!isRef(value))
103
192
  return value;
104
193
  const target = resolveRef(joinPath(scopePath), value.ref);
@@ -145,9 +234,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
145
234
  // Handle system nodes (graphInput, graphOutput, graphProp)
146
235
  // System nodes are identified by their type name — reserved, no definition lookup needed
147
236
  if (node.type === GRAPH_INPUT) {
148
- // Get the port name from the portName property
149
- const portNameProp = node.props?.find(p => p.name === 'portName');
150
- const inputName = portNameProp?.value || nodeName;
237
+ const inputName = boundaryName(node);
151
238
  // A wired input whose arm was not taken stays dark, default or not, as it would in the flat graph.
152
239
  if (absent?.has(inputName)) {
153
240
  cache.set(nodeName, {});
@@ -157,19 +244,17 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
157
244
  let value = inputs[inputName];
158
245
  if (value === undefined) {
159
246
  const defaultProp = node.props?.find(p => p.name === 'default');
160
- value = defaultProp?.value;
247
+ value = plainValue(defaultProp?.value);
161
248
  }
162
249
  const result = { value };
163
250
  cache.set(nodeName, result);
164
251
  return result;
165
252
  }
166
253
  if (node.type === GRAPH_PROP) {
167
- // Get the prop name from the propName property
168
- const propNameProp = node.props?.find(p => p.name === 'propName');
169
- const propName = propNameProp?.value || nodeName;
254
+ const propName = boundaryName(node);
170
255
  let value = props[propName];
171
256
  if (value === undefined) {
172
- value = node.props?.find(p => p.name === 'default')?.value;
257
+ value = plainValue(node.props?.find(p => p.name === 'default')?.value);
173
258
  }
174
259
  const result = { value };
175
260
  cache.set(nodeName, result);
@@ -217,6 +302,14 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
217
302
  }
218
303
  }
219
304
  }
305
+ // An unwired input takes the subnet's prop of the same name; the graphInput's default comes after.
306
+ for (const inputPort of inputPorts) {
307
+ if (inputPort.multi || (subnetPortEdges?.get(inputPort.name) ?? []).length > 0)
308
+ continue;
309
+ const stored = unwiredInput(node, inputPort.name);
310
+ if (stored !== undefined)
311
+ subnetInputs[inputPort.name] = await resolve(stored, nodeName);
312
+ }
220
313
  // Promoted parameters: the value set on the subnet node, else the graphProp's default.
221
314
  const subnetProps = {};
222
315
  for (const propDef of derivePropDefinitions(node)) {
@@ -229,7 +322,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
229
322
  const pull = openGraph({ name: `${nodeName}_subnet`, context: node.context || graphContext || undefined, nodes: children, edges: node.edges ?? [] }, { definitions, inputs: subnetInputs, props: subnetProps, absent: absentInputs }, assetPath, [...scopePath, nodeName], scope);
230
323
  const subnetOutputs = {};
231
324
  for (const outputPort of outputPorts) {
232
- const outputBoundaryNode = children.find(n => n.type === GRAPH_OUTPUT && n.props?.find(p => p.name === 'portName')?.value === outputPort.name);
325
+ const outputBoundaryNode = children.find(n => n.type === GRAPH_OUTPUT && boundaryName(n) === outputPort.name);
233
326
  if (!outputBoundaryNode) {
234
327
  throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
235
328
  }
@@ -325,6 +418,19 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
325
418
  }
326
419
  }
327
420
  }
421
+ // Unwired: the node's prop of the same name, else the definition's default for it. Read once
422
+ // the wires are known to have arrived, so a node on an arm not taken resolves nothing.
423
+ const ports = [...(definition.inputs ?? []), ...(definition.graph ? derivePorts(definition.graph).inputs : [])];
424
+ const multi = new Set(ports.filter(port => port.multi).map(port => port.name));
425
+ for (const port of new Set(ports.map(port => port.name))) {
426
+ if (multi.has(port))
427
+ continue;
428
+ if ((portEdges?.get(port) ?? []).length > 0)
429
+ continue;
430
+ const stored = unwiredInput(node, port, definition.props);
431
+ if (stored !== undefined)
432
+ nodeInputs[port] = await resolve(stored, nodeName);
433
+ }
328
434
  // Get props from node instance (override definition defaults)
329
435
  const nodeProps = {};
330
436
  if (definition.props) {
@@ -348,10 +454,9 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
348
454
  const pull = openGraph({ name: `${nodeName}_asset`, context: assetGraph.context || definition.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, inputs: nodeInputs, props: nodeProps, absent: absentInputs }, [...assetPath, key]);
349
455
  const assetOutputs = {};
350
456
  for (const outputBoundaryNode of assetGraph.nodes.filter(n => n.type === GRAPH_OUTPUT)) {
351
- const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
352
457
  const result = await pull(outputBoundaryNode.name);
353
458
  if ('value' in result)
354
- assetOutputs[portNameProp?.value || outputBoundaryNode.name] = result.value;
459
+ assetOutputs[boundaryName(outputBoundaryNode)] = result.value;
355
460
  }
356
461
  cache.set(nodeName, assetOutputs);
357
462
  return assetOutputs;
package/esm/validate.js CHANGED
@@ -1,6 +1,6 @@
1
- import { checkTypes, definitionKey, derivePorts, derivePropDefinitions, GRAPH_INPUT, hoistDefinitions, isBoundaryType, isNodeType, SUBNET, typeKey } from '@fbp/spec';
1
+ import { boundaryName, checkTypes, definitionKey, derivePorts, derivePropDefinitions, GRAPH_INPUT, hoistDefinitions, isBoundaryType, isNodeType, plainValue, SUBNET, typeKey, unwiredInput } from '@fbp/spec';
2
2
  import { findCycle, formatCycle } from './cycles';
3
- import { refEdges } from './evaluate';
3
+ import { refEdges, wiringOf } from './evaluate';
4
4
  /** Build "dst node -> dst port -> has at least one incoming edge". */
5
5
  function connectedPorts(edges) {
6
6
  const map = new Map();
@@ -15,8 +15,8 @@ function connectedPorts(edges) {
15
15
  return map;
16
16
  }
17
17
  function nodeHasProp(node, propName) {
18
- const p = node.props?.find(pr => pr.name === propName);
19
- return p !== undefined && p.value !== undefined && p.value !== null;
18
+ const value = plainValue(node.props?.find(pr => pr.name === propName)?.value);
19
+ return value !== undefined && value !== null;
20
20
  }
21
21
  /**
22
22
  * Statically validate a flow graph before publish/execution.
@@ -83,7 +83,10 @@ export function validateGraph(graph, options = {}) {
83
83
  const visit = (g, pathPrefix, scope = []) => {
84
84
  const graphContext = g.context || '';
85
85
  const connected = connectedPorts(g.edges ?? []);
86
- const cycle = findCycle((g.nodes ?? []).map(n => n.name), [...(g.edges ?? []), ...refEdges(g.nodes ?? [], scope)]);
86
+ const cycle = findCycle((g.nodes ?? []).map(n => n.name), [
87
+ ...(g.edges ?? []),
88
+ ...refEdges(g.nodes ?? [], scope, wiringOf(g.edges ?? [], graphContext, defMap))
89
+ ]);
87
90
  if (cycle) {
88
91
  const path = cycle.map(name => qualify(pathPrefix, name));
89
92
  errors.push({
@@ -103,16 +106,16 @@ export function validateGraph(graph, options = {}) {
103
106
  const children = node.nodes ?? [];
104
107
  const defaulted = new Set(children
105
108
  .filter(n => n.type === GRAPH_INPUT && nodeHasProp(n, 'default'))
106
- .map(n => n.props.find(p => p.name === 'portName')?.value ?? n.name));
109
+ .map(boundaryName));
107
110
  for (const port of derivePorts(node).inputs) {
108
111
  if (port.optional === true || port.multi === true || defaulted.has(port.name))
109
112
  continue;
110
- if (!nodeConnected.has(port.name)) {
113
+ if (!nodeConnected.has(port.name) && plainValue(unwiredInput(node, port.name)) == null) {
111
114
  errors.push({
112
115
  code: 'MISSING_REQUIRED_INPUT',
113
116
  node: qualifiedName,
114
117
  port: port.name,
115
- message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected`,
118
+ message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected or set`,
116
119
  });
117
120
  }
118
121
  }
@@ -142,12 +145,12 @@ export function validateGraph(graph, options = {}) {
142
145
  for (const port of definition.inputs ?? []) {
143
146
  if (port.optional === true || port.multi === true)
144
147
  continue;
145
- if (!nodeConnected.has(port.name)) {
148
+ if (!nodeConnected.has(port.name) && plainValue(unwiredInput(node, port.name, definition.props)) == null) {
146
149
  errors.push({
147
150
  code: 'MISSING_REQUIRED_INPUT',
148
151
  node: qualifiedName,
149
152
  port: port.name,
150
- message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected`,
153
+ message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected or set`,
151
154
  });
152
155
  }
153
156
  }
package/evaluate.d.ts CHANGED
@@ -15,5 +15,24 @@ export declare function evaluate(graph: Graph, options: EvaluateOptions): Promis
15
15
  /**
16
16
  * A `{ ref }` to a sibling node's value is a dependency, like an edge.
17
17
  * `scopePath` is where `nodes` live, so absolute references are recognised too.
18
+ *
19
+ * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
20
+ * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
21
+ * the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
22
+ * Inner nodes that no output of the subnet pulls are never evaluated, so theirs do not count.
18
23
  */
19
- export declare function refEdges(nodes: Node[], scopePath?: string[]): Edge[];
24
+ export declare function refEdges(nodes: Node[], scopePath?: string[], wiring?: Wiring, reads?: Map<string, Set<string>>): Edge[];
25
+ /** What `refEdges` needs to tell a typed value that a wire overrides from one that is read. */
26
+ export interface Wiring {
27
+ /** The edges and context of the scope the nodes live in. */
28
+ edges: Edge[];
29
+ context: string;
30
+ /** Whether the type declares a prop of this name; unknown types should answer `true`. */
31
+ declaresProp(context: string, type: string, prop: string): boolean;
32
+ }
33
+ /** `Wiring` for a scope, answered from its definitions. */
34
+ export declare function wiringOf(edges: Edge[], context: string, definitions: Map<string, {
35
+ props?: {
36
+ name: string;
37
+ }[];
38
+ }>): Wiring;
package/evaluate.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.evaluate = evaluate;
4
4
  exports.refEdges = refEdges;
5
+ exports.wiringOf = wiringOf;
5
6
  const spec_1 = require("@fbp/spec");
6
7
  const cycles_1 = require("./cycles");
7
8
  /**
@@ -30,32 +31,118 @@ async function evaluate(graph, options) {
30
31
  /**
31
32
  * A `{ ref }` to a sibling node's value is a dependency, like an edge.
32
33
  * `scopePath` is where `nodes` live, so absolute references are recognised too.
34
+ *
35
+ * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
36
+ * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
37
+ * the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
38
+ * Inner nodes that no output of the subnet pulls are never evaluated, so theirs do not count.
33
39
  */
34
- function refEdges(nodes, scopePath = []) {
40
+ function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
35
41
  const names = new Set(nodes.map(n => n.name));
36
42
  const scope = (0, spec_1.joinPath)(scopePath);
37
43
  const edges = [];
38
- for (const node of nodes) {
39
- for (const prop of node.props ?? []) {
44
+ const find = (segments) => {
45
+ if (segments.length <= scopePath.length || scopePath.some((part, i) => segments[i] !== part))
46
+ return undefined;
47
+ let level = nodes;
48
+ let found;
49
+ for (const part of segments.slice(scopePath.length)) {
50
+ found = level.find(n => n.name === part);
51
+ if (!found)
52
+ return undefined;
53
+ level = found.nodes ?? [];
54
+ }
55
+ return found;
56
+ };
57
+ // The siblings a reference reads. A `.prop` reference reads whatever that prop refers to, wired or not.
58
+ const follow = (value, from, seen) => {
59
+ if (!(0, spec_1.isRef)(value))
60
+ return [];
61
+ let target;
62
+ try {
63
+ target = (0, spec_1.resolveRef)((0, spec_1.joinPath)(from), value.ref);
64
+ }
65
+ catch {
66
+ return [];
67
+ }
68
+ const segments = (0, spec_1.parsePath)(target.path);
69
+ const owner = segments.slice(0, -1);
70
+ if (target.prop === undefined) {
71
+ const sibling = segments[segments.length - 1];
72
+ return sibling !== undefined && names.has(sibling) && (0, spec_1.joinPath)(owner) === scope ? [sibling] : [];
73
+ }
74
+ const key = `${target.path}.${target.prop}`;
75
+ if (seen.includes(key))
76
+ return [];
77
+ return follow(find(segments)?.props?.find(p => p.name === target.prop)?.value, owner, [...seen, key]);
78
+ };
79
+ const collect = (holder, holderScope, dependent, wires, context) => {
80
+ for (const prop of holder.props ?? []) {
40
81
  if (!(0, spec_1.isRef)(prop.value))
41
82
  continue;
42
- let target;
43
- try {
44
- target = (0, spec_1.resolveRef)(scope, prop.value.ref);
45
- }
46
- catch {
83
+ // A typed value on an input that is wired is never read, unless the type also declares a prop of that name.
84
+ if (wiring && holder.type !== spec_1.SUBNET &&
85
+ wires.some(wire => wire.dst.node === holder.name && wire.dst.port === prop.name) &&
86
+ !wiring.declaresProp(holder.context || context, holder.type, prop.name))
47
87
  continue;
48
- }
49
- const segments = (0, spec_1.parsePath)(target.path);
50
- const sibling = segments[segments.length - 1];
51
- if (target.prop === undefined && sibling !== undefined && names.has(sibling) &&
52
- (0, spec_1.joinPath)(segments.slice(0, -1)) === scope) {
53
- edges.push({ src: { node: sibling, port: 'value' }, dst: { node: node.name, port: prop.name } });
88
+ for (const sibling of follow(prop.value, holderScope, [])) {
89
+ edges.push({ src: { node: sibling, port: 'value' }, dst: { node: dependent, port: prop.name } });
54
90
  }
55
91
  }
56
- }
92
+ if (holder.type !== spec_1.SUBNET)
93
+ return;
94
+ const inner = [...holderScope, holder.name];
95
+ const innerContext = holder.context || context;
96
+ const innerWiring = wiring && { ...wiring, edges: holder.edges ?? [], context: innerContext };
97
+ // Only the nodes its outputs pull are read: a reference on any other inner node is never resolved.
98
+ // Keyed by where the subnet sits: the same node object placed twice reads by its own scope and context.
99
+ const at = JSON.stringify(inner);
100
+ let read = reads.get(at);
101
+ if (!read) {
102
+ read = pulled(holder.nodes ?? [], [...(holder.edges ?? []), ...refEdges(holder.nodes ?? [], inner, innerWiring, reads)]);
103
+ reads.set(at, read);
104
+ }
105
+ for (const child of holder.nodes ?? []) {
106
+ if (read.has(child.name))
107
+ collect(child, inner, dependent, holder.edges ?? [], innerContext);
108
+ }
109
+ };
110
+ for (const node of nodes)
111
+ collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
57
112
  return edges;
58
113
  }
114
+ /** The nodes a subnet evaluates to produce its outputs: its graphOutputs and everything upstream of them. */
115
+ function pulled(nodes, edges) {
116
+ const upstream = new Map();
117
+ for (const edge of edges) {
118
+ const sources = upstream.get(edge.dst.node);
119
+ if (sources)
120
+ sources.push(edge.src.node);
121
+ else
122
+ upstream.set(edge.dst.node, [edge.src.node]);
123
+ }
124
+ const read = new Set();
125
+ const pending = nodes.filter(n => n.type === spec_1.GRAPH_OUTPUT).map(n => n.name);
126
+ while (pending.length) {
127
+ const name = pending.pop();
128
+ if (read.has(name))
129
+ continue;
130
+ read.add(name);
131
+ pending.push(...(upstream.get(name) ?? []));
132
+ }
133
+ return read;
134
+ }
135
+ /** `Wiring` for a scope, answered from its definitions. */
136
+ function wiringOf(edges, context, definitions) {
137
+ return {
138
+ edges,
139
+ context,
140
+ declaresProp: (ctx, type, prop) => {
141
+ const definition = definitions.get((0, spec_1.typeKey)(ctx, type));
142
+ return !definition || (definition.props ?? []).some(def => def.name === prop);
143
+ }
144
+ };
145
+ }
59
146
  /**
60
147
  * @param assetPath - definition keys of the digital assets being evaluated,
61
148
  * outermost first, so an asset that contains itself throws instead of
@@ -66,7 +153,11 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
66
153
  const { definitions } = options;
67
154
  // Reject cycles up front, as the validator and the SQL gate do: pulling
68
155
  // through a loop would otherwise recurse until the stack overflows.
69
- const cycle = (0, cycles_1.findCycle)(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath)]);
156
+ const defMap = new Map();
157
+ for (const def of definitions) {
158
+ defMap.set((0, spec_1.definitionKey)(def), def);
159
+ }
160
+ const cycle = (0, cycles_1.findCycle)(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath, wiringOf(graph.edges, graph.context || '', defMap))]);
70
161
  if (cycle) {
71
162
  throw new Error(`Graph "${graph.name}" contains a cycle: ${(0, cycles_1.formatCycle)(cycle)}`);
72
163
  }
@@ -75,10 +166,6 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
75
166
  for (const node of graph.nodes) {
76
167
  nodeMap.set(node.name, node);
77
168
  }
78
- const defMap = new Map();
79
- for (const def of definitions) {
80
- defMap.set((0, spec_1.definitionKey)(def), def);
81
- }
82
169
  // Graph-level context used as default for node lookups
83
170
  const graphContext = graph.context || '';
84
171
  // Build edge lookup: destination node -> destination port -> edges (in array order)
@@ -100,9 +187,12 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
100
187
  /**
101
188
  * Resolve a prop value: a `{ ref }` takes the value of the node it points at
102
189
  * (its `value` output, or the named prop for a '.prop' reference). The target
103
- * is in this scope or one that encloses it.
190
+ * is in this scope or one that encloses it. A `{ literal }` is its content,
191
+ * verbatim.
104
192
  */
105
193
  async function resolve(value, holder, chain = []) {
194
+ if ((0, spec_1.isLiteral)(value))
195
+ return value.literal;
106
196
  if (!(0, spec_1.isRef)(value))
107
197
  return value;
108
198
  const target = (0, spec_1.resolveRef)((0, spec_1.joinPath)(scopePath), value.ref);
@@ -149,9 +239,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
149
239
  // Handle system nodes (graphInput, graphOutput, graphProp)
150
240
  // System nodes are identified by their type name — reserved, no definition lookup needed
151
241
  if (node.type === spec_1.GRAPH_INPUT) {
152
- // Get the port name from the portName property
153
- const portNameProp = node.props?.find(p => p.name === 'portName');
154
- const inputName = portNameProp?.value || nodeName;
242
+ const inputName = (0, spec_1.boundaryName)(node);
155
243
  // A wired input whose arm was not taken stays dark, default or not, as it would in the flat graph.
156
244
  if (absent?.has(inputName)) {
157
245
  cache.set(nodeName, {});
@@ -161,19 +249,17 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
161
249
  let value = inputs[inputName];
162
250
  if (value === undefined) {
163
251
  const defaultProp = node.props?.find(p => p.name === 'default');
164
- value = defaultProp?.value;
252
+ value = (0, spec_1.plainValue)(defaultProp?.value);
165
253
  }
166
254
  const result = { value };
167
255
  cache.set(nodeName, result);
168
256
  return result;
169
257
  }
170
258
  if (node.type === spec_1.GRAPH_PROP) {
171
- // Get the prop name from the propName property
172
- const propNameProp = node.props?.find(p => p.name === 'propName');
173
- const propName = propNameProp?.value || nodeName;
259
+ const propName = (0, spec_1.boundaryName)(node);
174
260
  let value = props[propName];
175
261
  if (value === undefined) {
176
- value = node.props?.find(p => p.name === 'default')?.value;
262
+ value = (0, spec_1.plainValue)(node.props?.find(p => p.name === 'default')?.value);
177
263
  }
178
264
  const result = { value };
179
265
  cache.set(nodeName, result);
@@ -221,6 +307,14 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
221
307
  }
222
308
  }
223
309
  }
310
+ // An unwired input takes the subnet's prop of the same name; the graphInput's default comes after.
311
+ for (const inputPort of inputPorts) {
312
+ if (inputPort.multi || (subnetPortEdges?.get(inputPort.name) ?? []).length > 0)
313
+ continue;
314
+ const stored = (0, spec_1.unwiredInput)(node, inputPort.name);
315
+ if (stored !== undefined)
316
+ subnetInputs[inputPort.name] = await resolve(stored, nodeName);
317
+ }
224
318
  // Promoted parameters: the value set on the subnet node, else the graphProp's default.
225
319
  const subnetProps = {};
226
320
  for (const propDef of (0, spec_1.derivePropDefinitions)(node)) {
@@ -233,7 +327,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
233
327
  const pull = openGraph({ name: `${nodeName}_subnet`, context: node.context || graphContext || undefined, nodes: children, edges: node.edges ?? [] }, { definitions, inputs: subnetInputs, props: subnetProps, absent: absentInputs }, assetPath, [...scopePath, nodeName], scope);
234
328
  const subnetOutputs = {};
235
329
  for (const outputPort of outputPorts) {
236
- const outputBoundaryNode = children.find(n => n.type === spec_1.GRAPH_OUTPUT && n.props?.find(p => p.name === 'portName')?.value === outputPort.name);
330
+ const outputBoundaryNode = children.find(n => n.type === spec_1.GRAPH_OUTPUT && (0, spec_1.boundaryName)(n) === outputPort.name);
237
331
  if (!outputBoundaryNode) {
238
332
  throw new Error(`Output boundary node not found for port: ${outputPort.name}`);
239
333
  }
@@ -329,6 +423,19 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
329
423
  }
330
424
  }
331
425
  }
426
+ // Unwired: the node's prop of the same name, else the definition's default for it. Read once
427
+ // the wires are known to have arrived, so a node on an arm not taken resolves nothing.
428
+ const ports = [...(definition.inputs ?? []), ...(definition.graph ? (0, spec_1.derivePorts)(definition.graph).inputs : [])];
429
+ const multi = new Set(ports.filter(port => port.multi).map(port => port.name));
430
+ for (const port of new Set(ports.map(port => port.name))) {
431
+ if (multi.has(port))
432
+ continue;
433
+ if ((portEdges?.get(port) ?? []).length > 0)
434
+ continue;
435
+ const stored = (0, spec_1.unwiredInput)(node, port, definition.props);
436
+ if (stored !== undefined)
437
+ nodeInputs[port] = await resolve(stored, nodeName);
438
+ }
332
439
  // Get props from node instance (override definition defaults)
333
440
  const nodeProps = {};
334
441
  if (definition.props) {
@@ -352,10 +459,9 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
352
459
  const pull = openGraph({ name: `${nodeName}_asset`, context: assetGraph.context || definition.context, nodes: assetGraph.nodes, edges: assetGraph.edges }, { definitions, inputs: nodeInputs, props: nodeProps, absent: absentInputs }, [...assetPath, key]);
353
460
  const assetOutputs = {};
354
461
  for (const outputBoundaryNode of assetGraph.nodes.filter(n => n.type === spec_1.GRAPH_OUTPUT)) {
355
- const portNameProp = outputBoundaryNode.props?.find(p => p.name === 'portName');
356
462
  const result = await pull(outputBoundaryNode.name);
357
463
  if ('value' in result)
358
- assetOutputs[portNameProp?.value || outputBoundaryNode.name] = result.value;
464
+ assetOutputs[(0, spec_1.boundaryName)(outputBoundaryNode)] = result.value;
359
465
  }
360
466
  cache.set(nodeName, assetOutputs);
361
467
  return assetOutputs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fbp/evaluator",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
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,7 @@
12
12
  "directory": "dist"
13
13
  },
14
14
  "dependencies": {
15
- "@fbp/spec": "2.0.0"
15
+ "@fbp/spec": "2.1.1"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/jest": "^30.0.0",
package/validate.js CHANGED
@@ -18,8 +18,8 @@ function connectedPorts(edges) {
18
18
  return map;
19
19
  }
20
20
  function nodeHasProp(node, propName) {
21
- const p = node.props?.find(pr => pr.name === propName);
22
- return p !== undefined && p.value !== undefined && p.value !== null;
21
+ const value = (0, spec_1.plainValue)(node.props?.find(pr => pr.name === propName)?.value);
22
+ return value !== undefined && value !== null;
23
23
  }
24
24
  /**
25
25
  * Statically validate a flow graph before publish/execution.
@@ -86,7 +86,10 @@ function validateGraph(graph, options = {}) {
86
86
  const visit = (g, pathPrefix, scope = []) => {
87
87
  const graphContext = g.context || '';
88
88
  const connected = connectedPorts(g.edges ?? []);
89
- const cycle = (0, cycles_1.findCycle)((g.nodes ?? []).map(n => n.name), [...(g.edges ?? []), ...(0, evaluate_1.refEdges)(g.nodes ?? [], scope)]);
89
+ const cycle = (0, cycles_1.findCycle)((g.nodes ?? []).map(n => n.name), [
90
+ ...(g.edges ?? []),
91
+ ...(0, evaluate_1.refEdges)(g.nodes ?? [], scope, (0, evaluate_1.wiringOf)(g.edges ?? [], graphContext, defMap))
92
+ ]);
90
93
  if (cycle) {
91
94
  const path = cycle.map(name => qualify(pathPrefix, name));
92
95
  errors.push({
@@ -106,16 +109,16 @@ function validateGraph(graph, options = {}) {
106
109
  const children = node.nodes ?? [];
107
110
  const defaulted = new Set(children
108
111
  .filter(n => n.type === spec_1.GRAPH_INPUT && nodeHasProp(n, 'default'))
109
- .map(n => n.props.find(p => p.name === 'portName')?.value ?? n.name));
112
+ .map(spec_1.boundaryName));
110
113
  for (const port of (0, spec_1.derivePorts)(node).inputs) {
111
114
  if (port.optional === true || port.multi === true || defaulted.has(port.name))
112
115
  continue;
113
- if (!nodeConnected.has(port.name)) {
116
+ if (!nodeConnected.has(port.name) && (0, spec_1.plainValue)((0, spec_1.unwiredInput)(node, port.name)) == null) {
114
117
  errors.push({
115
118
  code: 'MISSING_REQUIRED_INPUT',
116
119
  node: qualifiedName,
117
120
  port: port.name,
118
- message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected`,
121
+ message: `Required input "${port.name}" of subnet "${qualifiedName}" is not connected or set`,
119
122
  });
120
123
  }
121
124
  }
@@ -145,12 +148,12 @@ function validateGraph(graph, options = {}) {
145
148
  for (const port of definition.inputs ?? []) {
146
149
  if (port.optional === true || port.multi === true)
147
150
  continue;
148
- if (!nodeConnected.has(port.name)) {
151
+ if (!nodeConnected.has(port.name) && (0, spec_1.plainValue)((0, spec_1.unwiredInput)(node, port.name, definition.props)) == null) {
149
152
  errors.push({
150
153
  code: 'MISSING_REQUIRED_INPUT',
151
154
  node: qualifiedName,
152
155
  port: port.name,
153
- message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected`,
156
+ message: `Required input "${port.name}" of node "${qualifiedName}" (${node.type}) is not connected or set`,
154
157
  });
155
158
  }
156
159
  }