@fbp/evaluator 2.1.1 → 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
@@ -36,18 +36,21 @@ export function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
36
36
  const names = new Set(nodes.map(n => n.name));
37
37
  const scope = joinPath(scopePath);
38
38
  const edges = [];
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 ?? [];
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 };
49
52
  }
50
- return found;
53
+ return undefined;
51
54
  };
52
55
  // The siblings a reference reads. A `.prop` reference reads whatever that prop refers to, wired or not.
53
56
  const follow = (value, from, seen) => {
@@ -69,16 +72,22 @@ export function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
69
72
  const key = `${target.path}.${target.prop}`;
70
73
  if (seen.includes(key))
71
74
  return [];
72
- 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]);
73
79
  };
74
80
  const collect = (holder, holderScope, dependent, wires, context) => {
75
81
  for (const prop of holder.props ?? []) {
76
- if (!isRef(prop.value))
82
+ if (!isRef(prop.value) || isBoundaryDefault(holder, prop.name))
77
83
  continue;
78
84
  // 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 &&
85
+ // A subnet declares the props its graphProps name.
86
+ if (wiring &&
80
87
  wires.some(wire => wire.dst.node === holder.name && wire.dst.port === prop.name) &&
81
- !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)))
82
91
  continue;
83
92
  for (const sibling of follow(prop.value, holderScope, [])) {
84
93
  edges.push({ src: { node: sibling, port: 'value' }, dst: { node: dependent, port: prop.name } });
@@ -106,6 +115,8 @@ export function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
106
115
  collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
107
116
  return edges;
108
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);
109
120
  /** The nodes a subnet evaluates to produce its outputs: its graphOutputs and everything upstream of them. */
110
121
  function pulled(nodes, edges) {
111
122
  const upstream = new Map();
@@ -127,6 +138,16 @@ function pulled(nodes, edges) {
127
138
  }
128
139
  return read;
129
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
+ }
130
151
  /** `Wiring` for a scope, answered from its definitions. */
131
152
  export function wiringOf(edges, context, definitions) {
132
153
  return {
@@ -152,7 +173,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
152
173
  for (const def of definitions) {
153
174
  defMap.set(definitionKey(def), def);
154
175
  }
155
- 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));
156
177
  if (cycle) {
157
178
  throw new Error(`Graph "${graph.name}" contains a cycle: ${formatCycle(cycle)}`);
158
179
  }
@@ -206,6 +227,8 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
206
227
  throw new Error(`Reference "${value.ref}" on node "${holder}" refers back to itself`);
207
228
  }
208
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);
209
232
  return found.resolve(prop?.value, name, [...chain, key]);
210
233
  }
211
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
@@ -22,6 +22,11 @@ export declare function evaluate(graph: Graph, options: EvaluateOptions): Promis
22
22
  * Inner nodes that no output of the subnet pulls are never evaluated, so theirs do not count.
23
23
  */
24
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[]];
25
30
  /** What `refEdges` needs to tell a typed value that a wire overrides from one that is read. */
26
31
  export interface Wiring {
27
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");
@@ -41,18 +42,21 @@ function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
41
42
  const names = new Set(nodes.map(n => n.name));
42
43
  const scope = (0, spec_1.joinPath)(scopePath);
43
44
  const edges = [];
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 ?? [];
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 };
54
58
  }
55
- return found;
59
+ return undefined;
56
60
  };
57
61
  // The siblings a reference reads. A `.prop` reference reads whatever that prop refers to, wired or not.
58
62
  const follow = (value, from, seen) => {
@@ -74,16 +78,22 @@ function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
74
78
  const key = `${target.path}.${target.prop}`;
75
79
  if (seen.includes(key))
76
80
  return [];
77
- 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]);
78
85
  };
79
86
  const collect = (holder, holderScope, dependent, wires, context) => {
80
87
  for (const prop of holder.props ?? []) {
81
- if (!(0, spec_1.isRef)(prop.value))
88
+ if (!(0, spec_1.isRef)(prop.value) || isBoundaryDefault(holder, prop.name))
82
89
  continue;
83
90
  // 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 &&
91
+ // A subnet declares the props its graphProps name.
92
+ if (wiring &&
85
93
  wires.some(wire => wire.dst.node === holder.name && wire.dst.port === prop.name) &&
86
- !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)))
87
97
  continue;
88
98
  for (const sibling of follow(prop.value, holderScope, [])) {
89
99
  edges.push({ src: { node: sibling, port: 'value' }, dst: { node: dependent, port: prop.name } });
@@ -111,6 +121,8 @@ function refEdges(nodes, scopePath = [], wiring, reads = new Map()) {
111
121
  collect(node, scopePath, node.name, wiring?.edges ?? [], wiring?.context ?? '');
112
122
  return edges;
113
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);
114
126
  /** The nodes a subnet evaluates to produce its outputs: its graphOutputs and everything upstream of them. */
115
127
  function pulled(nodes, edges) {
116
128
  const upstream = new Map();
@@ -132,6 +144,16 @@ function pulled(nodes, edges) {
132
144
  }
133
145
  return read;
134
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
+ }
135
157
  /** `Wiring` for a scope, answered from its definitions. */
136
158
  function wiringOf(edges, context, definitions) {
137
159
  return {
@@ -157,7 +179,7 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
157
179
  for (const def of definitions) {
158
180
  defMap.set((0, spec_1.definitionKey)(def), def);
159
181
  }
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))]);
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));
161
183
  if (cycle) {
162
184
  throw new Error(`Graph "${graph.name}" contains a cycle: ${(0, cycles_1.formatCycle)(cycle)}`);
163
185
  }
@@ -211,6 +233,8 @@ function openGraph(graph, options, assetPath, scopePath = [], parentScope) {
211
233
  throw new Error(`Reference "${value.ref}" on node "${holder}" refers back to itself`);
212
234
  }
213
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);
214
238
  return found.resolve(prop?.value, name, [...chain, key]);
215
239
  }
216
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.1",
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",
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({