@fbp/evaluator 2.1.0 → 2.1.2

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
@@ -30,23 +30,27 @@ export async function evaluate(graph, options) {
30
30
  * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
31
31
  * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
32
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.
33
34
  */
34
- export function refEdges(nodes, scopePath = [], wiring) {
35
+ export function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
35
36
  const names = new Set(nodes.map(n => n.name));
36
37
  const scope = joinPath(scopePath);
37
38
  const edges = [];
38
- const find = (segments) => {
39
- if (segments.length <= scopePath.length || scopePath.some((part, i) => segments[i] !== part))
40
- return undefined;
41
- let level = nodes;
42
- let found;
43
- for (const part of segments.slice(scopePath.length)) {
44
- found = level.find(n => n.name === part);
45
- if (!found)
46
- return undefined;
47
- level = found.nodes ?? [];
39
+ // The node a reference names, as evaluation finds it: in a scope enclosing the holder, matched by joined
40
+ // path so a name that contains a slash is one name. Scopes above `nodes` are out of reach.
41
+ const find = (from, owner, name) => {
42
+ const path = joinPath(owner);
43
+ for (let depth = from.length; depth >= scopePath.length; depth--) {
44
+ const enclosing = from.slice(0, depth);
45
+ if (joinPath(enclosing) !== path)
46
+ continue;
47
+ let level = nodes;
48
+ for (const part of enclosing.slice(scopePath.length))
49
+ level = level?.find(n => n.name === part)?.nodes;
50
+ const node = level?.find(n => n.name === name);
51
+ return node && { node, scope: enclosing };
48
52
  }
49
- return found;
53
+ return undefined;
50
54
  };
51
55
  // The siblings a reference reads. A `.prop` reference reads whatever that prop refers to, wired or not.
52
56
  const follow = (value, from, seen) => {
@@ -68,16 +72,22 @@ export function refEdges(nodes, scopePath = [], wiring) {
68
72
  const key = `${target.path}.${target.prop}`;
69
73
  if (seen.includes(key))
70
74
  return [];
71
- return follow(find(segments)?.props?.find(p => p.name === target.prop)?.value, owner, [...seen, key]);
75
+ const held = find(from, owner, segments[segments.length - 1]);
76
+ if (!held || isBoundaryDefault(held.node, target.prop))
77
+ return [];
78
+ return follow(held.node.props?.find(p => p.name === target.prop)?.value, held.scope, [...seen, key]);
72
79
  };
73
80
  const collect = (holder, holderScope, dependent, wires, context) => {
74
81
  for (const prop of holder.props ?? []) {
75
- if (!isRef(prop.value))
82
+ if (!isRef(prop.value) || isBoundaryDefault(holder, prop.name))
76
83
  continue;
77
84
  // A typed value on an input that is wired is never read, unless the type also declares a prop of that name.
78
- if (wiring && holder.type !== SUBNET &&
85
+ // A subnet declares the props its graphProps name.
86
+ if (wiring &&
79
87
  wires.some(wire => wire.dst.node === holder.name && wire.dst.port === prop.name) &&
80
- !wiring.declaresProp(holder.context || context, holder.type, prop.name))
88
+ !(holder.type === SUBNET
89
+ ? holder.nodes?.some(n => n.type === GRAPH_PROP && boundaryName(n) === prop.name)
90
+ : wiring.declaresProp(holder.context || context, holder.type, prop.name)))
81
91
  continue;
82
92
  for (const sibling of follow(prop.value, holderScope, [])) {
83
93
  edges.push({ src: { node: sibling, port: 'value' }, dst: { node: dependent, port: prop.name } });
@@ -85,14 +95,59 @@ export function refEdges(nodes, scopePath = [], wiring) {
85
95
  }
86
96
  if (holder.type !== SUBNET)
87
97
  return;
98
+ const inner = [...holderScope, holder.name];
99
+ const innerContext = holder.context || context;
100
+ const innerWiring = wiring && { ...wiring, edges: holder.edges ?? [], context: innerContext };
101
+ // Only the nodes its outputs pull are read: a reference on any other inner node is never resolved.
102
+ // Keyed by where the subnet sits: the same node object placed twice reads by its own scope and context.
103
+ const at = JSON.stringify(inner);
104
+ let read = reads.get(at);
105
+ if (!read) {
106
+ read = pulled(holder.nodes ?? [], [...(holder.edges ?? []), ...refEdges(holder.nodes ?? [], inner, innerWiring, reads)]);
107
+ reads.set(at, read);
108
+ }
88
109
  for (const child of holder.nodes ?? []) {
89
- collect(child, [...holderScope, holder.name], dependent, holder.edges ?? [], holder.context || context);
110
+ if (read.has(child.name))
111
+ collect(child, inner, dependent, holder.edges ?? [], innerContext);
90
112
  }
91
113
  };
92
114
  for (const node of nodes)
93
115
  collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
94
116
  return edges;
95
117
  }
118
+ /** A subnet input's or parameter's `default` is plain JSON however it is read: a `{ ref }` inside it is never followed, so it is no dependency. */
119
+ const isBoundaryDefault = (node, prop) => prop === 'default' && (node.type === GRAPH_INPUT || node.type === GRAPH_PROP);
120
+ /** The nodes a subnet evaluates to produce its outputs: its graphOutputs and everything upstream of them. */
121
+ function pulled(nodes, edges) {
122
+ const upstream = new Map();
123
+ for (const edge of edges) {
124
+ const sources = upstream.get(edge.dst.node);
125
+ if (sources)
126
+ sources.push(edge.src.node);
127
+ else
128
+ upstream.set(edge.dst.node, [edge.src.node]);
129
+ }
130
+ const read = new Set();
131
+ const pending = nodes.filter(n => n.type === GRAPH_OUTPUT).map(n => n.name);
132
+ while (pending.length) {
133
+ const name = pending.pop();
134
+ if (read.has(name))
135
+ continue;
136
+ read.add(name);
137
+ pending.push(...(upstream.get(name) ?? []));
138
+ }
139
+ return read;
140
+ }
141
+ /**
142
+ * The nodes and edges a graph's cycle check covers. Inside a subnet or digital asset only the nodes
143
+ * its outputs pull are ever evaluated, so the rest cannot loop; in the root graph any node can be asked for.
144
+ */
145
+ export function cycleScope(nodes, edges, nested) {
146
+ if (!nested)
147
+ return [nodes.map(n => n.name), edges];
148
+ const read = pulled(nodes, edges);
149
+ return [nodes.filter(n => read.has(n.name)).map(n => n.name), edges];
150
+ }
96
151
  /** `Wiring` for a scope, answered from its definitions. */
97
152
  export function wiringOf(edges, context, definitions) {
98
153
  return {
@@ -118,7 +173,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
118
173
  for (const def of definitions) {
119
174
  defMap.set(definitionKey(def), def);
120
175
  }
121
- const cycle = findCycle(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath, wiringOf(graph.edges, graph.context || '', defMap))]);
176
+ const cycle = findCycle(...cycleScope(graph.nodes, [...graph.edges, ...refEdges(graph.nodes, scopePath, wiringOf(graph.edges, graph.context || '', defMap))], scopePath.length > 0 || assetPath.length > 0));
122
177
  if (cycle) {
123
178
  throw new Error(`Graph "${graph.name}" contains a cycle: ${formatCycle(cycle)}`);
124
179
  }
@@ -172,6 +227,8 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
172
227
  throw new Error(`Reference "${value.ref}" on node "${holder}" refers back to itself`);
173
228
  }
174
229
  const prop = found.getNode(name).props?.find(p => p.name === target.prop);
230
+ if (isBoundaryDefault(found.getNode(name), target.prop))
231
+ return plainValue(prop?.value);
175
232
  return found.resolve(prop?.value, name, [...chain, key]);
176
233
  }
177
234
  const outputs = await found.evaluateNode(name);
package/esm/validate.js CHANGED
@@ -1,6 +1,6 @@
1
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, wiringOf } from './evaluate';
3
+ import { cycleScope, 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();
@@ -83,10 +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), [
86
+ const cycle = findCycle(...cycleScope(g.nodes ?? [], [
87
87
  ...(g.edges ?? []),
88
88
  ...refEdges(g.nodes ?? [], scope, wiringOf(g.edges ?? [], graphContext, defMap))
89
- ]);
89
+ ], g !== graph));
90
90
  if (cycle) {
91
91
  const path = cycle.map(name => qualify(pathPrefix, name));
92
92
  errors.push({
package/evaluate.d.ts CHANGED
@@ -19,8 +19,14 @@ export declare function evaluate(graph: Graph, options: EvaluateOptions): Promis
19
19
  * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
20
20
  * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
21
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.
22
23
  */
23
- export declare function refEdges(nodes: Node[], scopePath?: string[], wiring?: Wiring): Edge[];
24
+ export declare function refEdges(nodes: Node[], scopePath?: string[], wiring?: Wiring, reads?: Map<string, Set<string>>): Edge[];
25
+ /**
26
+ * The nodes and edges a graph's cycle check covers. Inside a subnet or digital asset only the nodes
27
+ * its outputs pull are ever evaluated, so the rest cannot loop; in the root graph any node can be asked for.
28
+ */
29
+ export declare function cycleScope(nodes: Node[], edges: Edge[], nested: boolean): [string[], Edge[]];
24
30
  /** What `refEdges` needs to tell a typed value that a wire overrides from one that is read. */
25
31
  export interface Wiring {
26
32
  /** The edges and context of the scope the nodes live in. */
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.cycleScope = cycleScope;
5
6
  exports.wiringOf = wiringOf;
6
7
  const spec_1 = require("@fbp/spec");
7
8
  const cycles_1 = require("./cycles");
@@ -35,23 +36,27 @@ async function evaluate(graph, options) {
35
36
  * A reference held anywhere inside an inline subnet is a dependency of that subnet: to produce
36
37
  * its outputs the subnet reads what its inner nodes refer to. So an inner node that refers to
37
38
  * the subnet enclosing it, or to a sibling fed by that subnet, is a cycle like any other.
39
+ * Inner nodes that no output of the subnet pulls are never evaluated, so theirs do not count.
38
40
  */
39
- function refEdges(nodes, scopePath = [], wiring) {
41
+ function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
40
42
  const names = new Set(nodes.map(n => n.name));
41
43
  const scope = (0, spec_1.joinPath)(scopePath);
42
44
  const edges = [];
43
- const find = (segments) => {
44
- if (segments.length <= scopePath.length || scopePath.some((part, i) => segments[i] !== part))
45
- return undefined;
46
- let level = nodes;
47
- let found;
48
- for (const part of segments.slice(scopePath.length)) {
49
- found = level.find(n => n.name === part);
50
- if (!found)
51
- return undefined;
52
- level = found.nodes ?? [];
45
+ // The node a reference names, as evaluation finds it: in a scope enclosing the holder, matched by joined
46
+ // path so a name that contains a slash is one name. Scopes above `nodes` are out of reach.
47
+ const find = (from, owner, name) => {
48
+ const path = (0, spec_1.joinPath)(owner);
49
+ for (let depth = from.length; depth >= scopePath.length; depth--) {
50
+ const enclosing = from.slice(0, depth);
51
+ if ((0, spec_1.joinPath)(enclosing) !== path)
52
+ continue;
53
+ let level = nodes;
54
+ for (const part of enclosing.slice(scopePath.length))
55
+ level = level?.find(n => n.name === part)?.nodes;
56
+ const node = level?.find(n => n.name === name);
57
+ return node && { node, scope: enclosing };
53
58
  }
54
- return found;
59
+ return undefined;
55
60
  };
56
61
  // The siblings a reference reads. A `.prop` reference reads whatever that prop refers to, wired or not.
57
62
  const follow = (value, from, seen) => {
@@ -73,16 +78,22 @@ function refEdges(nodes, scopePath = [], wiring) {
73
78
  const key = `${target.path}.${target.prop}`;
74
79
  if (seen.includes(key))
75
80
  return [];
76
- return follow(find(segments)?.props?.find(p => p.name === target.prop)?.value, owner, [...seen, key]);
81
+ const held = find(from, owner, segments[segments.length - 1]);
82
+ if (!held || isBoundaryDefault(held.node, target.prop))
83
+ return [];
84
+ return follow(held.node.props?.find(p => p.name === target.prop)?.value, held.scope, [...seen, key]);
77
85
  };
78
86
  const collect = (holder, holderScope, dependent, wires, context) => {
79
87
  for (const prop of holder.props ?? []) {
80
- if (!(0, spec_1.isRef)(prop.value))
88
+ if (!(0, spec_1.isRef)(prop.value) || isBoundaryDefault(holder, prop.name))
81
89
  continue;
82
90
  // A typed value on an input that is wired is never read, unless the type also declares a prop of that name.
83
- if (wiring && holder.type !== spec_1.SUBNET &&
91
+ // A subnet declares the props its graphProps name.
92
+ if (wiring &&
84
93
  wires.some(wire => wire.dst.node === holder.name && wire.dst.port === prop.name) &&
85
- !wiring.declaresProp(holder.context || context, holder.type, prop.name))
94
+ !(holder.type === spec_1.SUBNET
95
+ ? holder.nodes?.some(n => n.type === spec_1.GRAPH_PROP && (0, spec_1.boundaryName)(n) === prop.name)
96
+ : wiring.declaresProp(holder.context || context, holder.type, prop.name)))
86
97
  continue;
87
98
  for (const sibling of follow(prop.value, holderScope, [])) {
88
99
  edges.push({ src: { node: sibling, port: 'value' }, dst: { node: dependent, port: prop.name } });
@@ -90,14 +101,59 @@ function refEdges(nodes, scopePath = [], wiring) {
90
101
  }
91
102
  if (holder.type !== spec_1.SUBNET)
92
103
  return;
104
+ const inner = [...holderScope, holder.name];
105
+ const innerContext = holder.context || context;
106
+ const innerWiring = wiring && { ...wiring, edges: holder.edges ?? [], context: innerContext };
107
+ // Only the nodes its outputs pull are read: a reference on any other inner node is never resolved.
108
+ // Keyed by where the subnet sits: the same node object placed twice reads by its own scope and context.
109
+ const at = JSON.stringify(inner);
110
+ let read = reads.get(at);
111
+ if (!read) {
112
+ read = pulled(holder.nodes ?? [], [...(holder.edges ?? []), ...refEdges(holder.nodes ?? [], inner, innerWiring, reads)]);
113
+ reads.set(at, read);
114
+ }
93
115
  for (const child of holder.nodes ?? []) {
94
- collect(child, [...holderScope, holder.name], dependent, holder.edges ?? [], holder.context || context);
116
+ if (read.has(child.name))
117
+ collect(child, inner, dependent, holder.edges ?? [], innerContext);
95
118
  }
96
119
  };
97
120
  for (const node of nodes)
98
121
  collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
99
122
  return edges;
100
123
  }
124
+ /** A subnet input's or parameter's `default` is plain JSON however it is read: a `{ ref }` inside it is never followed, so it is no dependency. */
125
+ const isBoundaryDefault = (node, prop) => prop === 'default' && (node.type === spec_1.GRAPH_INPUT || node.type === spec_1.GRAPH_PROP);
126
+ /** The nodes a subnet evaluates to produce its outputs: its graphOutputs and everything upstream of them. */
127
+ function pulled(nodes, edges) {
128
+ const upstream = new Map();
129
+ for (const edge of edges) {
130
+ const sources = upstream.get(edge.dst.node);
131
+ if (sources)
132
+ sources.push(edge.src.node);
133
+ else
134
+ upstream.set(edge.dst.node, [edge.src.node]);
135
+ }
136
+ const read = new Set();
137
+ const pending = nodes.filter(n => n.type === spec_1.GRAPH_OUTPUT).map(n => n.name);
138
+ while (pending.length) {
139
+ const name = pending.pop();
140
+ if (read.has(name))
141
+ continue;
142
+ read.add(name);
143
+ pending.push(...(upstream.get(name) ?? []));
144
+ }
145
+ return read;
146
+ }
147
+ /**
148
+ * The nodes and edges a graph's cycle check covers. Inside a subnet or digital asset only the nodes
149
+ * its outputs pull are ever evaluated, so the rest cannot loop; in the root graph any node can be asked for.
150
+ */
151
+ function cycleScope(nodes, edges, nested) {
152
+ if (!nested)
153
+ return [nodes.map(n => n.name), edges];
154
+ const read = pulled(nodes, edges);
155
+ return [nodes.filter(n => read.has(n.name)).map(n => n.name), edges];
156
+ }
101
157
  /** `Wiring` for a scope, answered from its definitions. */
102
158
  function wiringOf(edges, context, definitions) {
103
159
  return {
@@ -123,7 +179,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
123
179
  for (const def of definitions) {
124
180
  defMap.set((0, spec_1.definitionKey)(def), def);
125
181
  }
126
- const cycle = (0, cycles_1.findCycle)(graph.nodes.map(n => n.name), [...graph.edges, ...refEdges(graph.nodes, scopePath, wiringOf(graph.edges, graph.context || '', defMap))]);
182
+ const cycle = (0, cycles_1.findCycle)(...cycleScope(graph.nodes, [...graph.edges, ...refEdges(graph.nodes, scopePath, wiringOf(graph.edges, graph.context || '', defMap))], scopePath.length > 0 || assetPath.length > 0));
127
183
  if (cycle) {
128
184
  throw new Error(`Graph "${graph.name}" contains a cycle: ${(0, cycles_1.formatCycle)(cycle)}`);
129
185
  }
@@ -177,6 +233,8 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
177
233
  throw new Error(`Reference "${value.ref}" on node "${holder}" refers back to itself`);
178
234
  }
179
235
  const prop = found.getNode(name).props?.find(p => p.name === target.prop);
236
+ if (isBoundaryDefault(found.getNode(name), target.prop))
237
+ return (0, spec_1.plainValue)(prop?.value);
180
238
  return found.resolve(prop?.value, name, [...chain, key]);
181
239
  }
182
240
  const outputs = await found.evaluateNode(name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fbp/evaluator",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
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.1.0"
15
+ "@fbp/spec": "2.1.1"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/jest": "^30.0.0",
package/validate.js CHANGED
@@ -86,10 +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), [
89
+ const cycle = (0, cycles_1.findCycle)(...(0, evaluate_1.cycleScope)(g.nodes ?? [], [
90
90
  ...(g.edges ?? []),
91
91
  ...(0, evaluate_1.refEdges)(g.nodes ?? [], scope, (0, evaluate_1.wiringOf)(g.edges ?? [], graphContext, defMap))
92
- ]);
92
+ ], g !== graph));
93
93
  if (cycle) {
94
94
  const path = cycle.map(name => qualify(pathPrefix, name));
95
95
  errors.push({