@foresthubai/workflow-core 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +14 -14
  3. package/README.md +63 -63
  4. package/dist/api/deployment.d.ts +67 -0
  5. package/dist/api/deployment.d.ts.map +1 -0
  6. package/dist/api/deployment.js +6 -0
  7. package/dist/api/deployment.js.map +1 -0
  8. package/dist/api/engine.d.ts +744 -0
  9. package/dist/api/engine.d.ts.map +1 -0
  10. package/dist/api/engine.js +6 -0
  11. package/dist/api/engine.js.map +1 -0
  12. package/dist/api/index.d.ts +4 -0
  13. package/dist/api/index.d.ts.map +1 -1
  14. package/dist/api/workflow.d.ts +17 -1
  15. package/dist/api/workflow.d.ts.map +1 -1
  16. package/dist/channel/Channel.d.ts +1 -1
  17. package/dist/channel/Channel.d.ts.map +1 -1
  18. package/dist/channel/Channel.js +1 -1
  19. package/dist/channel/Channel.js.map +1 -1
  20. package/dist/channel/ChannelDefinition.d.ts.map +1 -1
  21. package/dist/channel/ChannelDefinition.js +22 -0
  22. package/dist/channel/ChannelDefinition.js.map +1 -1
  23. package/dist/channel/serialization.d.ts.map +1 -1
  24. package/dist/channel/serialization.js +6 -0
  25. package/dist/channel/serialization.js.map +1 -1
  26. package/dist/deploy/index.d.ts +5 -1
  27. package/dist/deploy/index.d.ts.map +1 -1
  28. package/dist/deploy/index.js +2 -1
  29. package/dist/deploy/index.js.map +1 -1
  30. package/dist/deploy/inputs.d.ts +26 -0
  31. package/dist/deploy/inputs.d.ts.map +1 -0
  32. package/dist/deploy/inputs.js +12 -0
  33. package/dist/deploy/inputs.js.map +1 -0
  34. package/dist/deploy/requirements.d.ts +24 -0
  35. package/dist/deploy/requirements.d.ts.map +1 -1
  36. package/dist/deploy/requirements.js +61 -0
  37. package/dist/deploy/requirements.js.map +1 -1
  38. package/dist/deploy/spec.d.ts +32 -0
  39. package/dist/deploy/spec.d.ts.map +1 -0
  40. package/dist/deploy/spec.js +343 -0
  41. package/dist/deploy/spec.js.map +1 -0
  42. package/dist/node/OutputNode.js +2 -2
  43. package/dist/node/OutputNode.js.map +1 -1
  44. package/package.json +2 -2
  45. package/src/api/deployment.ts +67 -0
  46. package/src/api/engine.ts +744 -0
  47. package/src/api/index.ts +26 -11
  48. package/src/api/workflow.ts +17 -1
  49. package/src/channel/Channel.ts +11 -11
  50. package/src/channel/ChannelDefinition.ts +98 -76
  51. package/src/channel/index.ts +6 -6
  52. package/src/channel/serialization.ts +74 -68
  53. package/src/deploy/index.ts +14 -1
  54. package/src/deploy/inputs.ts +41 -0
  55. package/src/deploy/requirements.test.ts +61 -61
  56. package/src/deploy/requirements.ts +156 -41
  57. package/src/deploy/spec.test.ts +226 -0
  58. package/src/deploy/spec.ts +379 -0
  59. package/src/diagnostics/__fixtures__/diagnosticFixtures.ts +158 -158
  60. package/src/diagnostics/diagnostics.test.ts +878 -878
  61. package/src/diagnostics/diagnostics.ts +936 -936
  62. package/src/diagnostics/index.ts +11 -11
  63. package/src/edge/Edge.ts +23 -23
  64. package/src/edge/EdgeDefinition.ts +45 -45
  65. package/src/edge/EdgeType.ts +19 -19
  66. package/src/edge/index.ts +8 -8
  67. package/src/edge/serialization.ts +83 -83
  68. package/src/expression/index.ts +4 -4
  69. package/src/expression/parser.ts +362 -362
  70. package/src/expression/types.ts +30 -30
  71. package/src/function/FunctionDeclaration.ts +54 -54
  72. package/src/function/index.ts +3 -3
  73. package/src/function/serialization.ts +40 -40
  74. package/src/globals.d.ts +9 -9
  75. package/src/id/index.ts +8 -8
  76. package/src/index.ts +22 -22
  77. package/src/memory/Memory.ts +15 -15
  78. package/src/memory/MemoryDefinition.ts +16 -16
  79. package/src/memory/MemoryFileDefinition.ts +37 -37
  80. package/src/memory/MemoryRegistry.ts +35 -35
  81. package/src/memory/VectorDatabaseDefinition.ts +21 -21
  82. package/src/memory/index.ts +8 -8
  83. package/src/memory/serialization.ts +47 -47
  84. package/src/migration/index.ts +4 -4
  85. package/src/migration/migrate.test.ts +44 -44
  86. package/src/migration/migrate.ts +58 -58
  87. package/src/migration/migrations.ts +24 -24
  88. package/src/migration/version.ts +9 -9
  89. package/src/model/LLMModelDefinition.ts +12 -12
  90. package/src/model/Model.ts +39 -39
  91. package/src/model/ModelDefinition.ts +15 -15
  92. package/src/model/ModelRegistry.ts +33 -33
  93. package/src/model/index.ts +7 -7
  94. package/src/model/serialization.ts +30 -30
  95. package/src/node/AgentNode.ts +82 -82
  96. package/src/node/DataNode.ts +41 -41
  97. package/src/node/FunctionNode.ts +76 -76
  98. package/src/node/InputNode.ts +185 -185
  99. package/src/node/LogicNode.ts +33 -33
  100. package/src/node/MqttNode.ts +127 -127
  101. package/src/node/Node.ts +61 -61
  102. package/src/node/NodeDefinition.ts +37 -37
  103. package/src/node/NodeRegistry.ts +85 -85
  104. package/src/node/OutputNode.ts +87 -87
  105. package/src/node/ToolNode.ts +32 -32
  106. package/src/node/TriggerNode.ts +272 -272
  107. package/src/node/constants.ts +16 -16
  108. package/src/node/index.ts +26 -26
  109. package/src/node/methods.ts +278 -278
  110. package/src/node/serialization.ts +544 -544
  111. package/src/parameter/OutputParameter.ts +68 -68
  112. package/src/parameter/Parameter.ts +243 -243
  113. package/src/parameter/index.ts +33 -33
  114. package/src/variable/Variable.ts +10 -10
  115. package/src/variable/index.ts +16 -16
  116. package/src/variable/operations.ts +106 -106
  117. package/src/workflow/Workflow.ts +41 -41
  118. package/src/workflow/index.ts +3 -3
  119. package/src/workflow/serialization.test.ts +240 -240
  120. package/src/workflow/serialization.ts +242 -242
@@ -1,240 +1,240 @@
1
- import { describe, it, expect } from "vitest";
2
- import { serialize, deserialize, computeVariablesFromNodes, buildCanvasVariables } from "./serialization";
3
- import { MAIN_CANVAS_ID, type Workflow, type Canvas } from "./Workflow";
4
- import type { Schemas } from "../api";
5
- import type { NodeData } from "../node";
6
-
7
- // ============================================================================
8
- // Reverse roundtrip: api JSON → deserialize → serialize → deep-equal JSON
9
- //
10
- // This direction is the strongest invariant — deserialize is the function
11
- // that reconstructs derivable state (variable records); if it gets the rest
12
- // right, re-serialize must produce the same JSON back. Tests every code path
13
- // in deserialize + serialize without separately constructing WorkflowState.
14
- // ============================================================================
15
-
16
- const empty: Schemas["Workflow"] = {
17
- schemaVersion: 1,
18
- nodes: [],
19
- edges: [],
20
- functions: [],
21
- declaredVariables: [],
22
- channels: [],
23
- memory: [],
24
- models: [],
25
- };
26
-
27
- const mainOnly: Schemas["Workflow"] = {
28
- schemaVersion: 1,
29
- nodes: [
30
- {
31
- id: "n1",
32
- type: "Agent",
33
- position: { x: 100, y: 200 },
34
- label: "My Agent",
35
- arguments: {
36
- name: "agent-1",
37
- model: "claude-opus-4-7",
38
- instructions: "be helpful",
39
- outputDeclarations: [],
40
- memoryRefs: [],
41
- answer: { active: true, mode: "emit", name: "answer" },
42
- },
43
- },
44
- ],
45
- edges: [],
46
- functions: [],
47
- declaredVariables: [
48
- { uid: "d1", name: "counter", dataType: "int", initialValue: 0 },
49
- ],
50
- channels: [],
51
- memory: [],
52
- models: [],
53
- };
54
-
55
- const allEdgeTypes: Schemas["Workflow"] = {
56
- schemaVersion: 1,
57
- nodes: [],
58
- edges: [
59
- { id: "edge-control", type: "control", from: { nodeId: "a", port: "out" }, to: { nodeId: "b", port: "in" } },
60
- { id: "edge-tool", type: "tool", from: { nodeId: "a", port: "tool-out" }, to: { nodeId: "b", port: "tool-in" } },
61
- {
62
- id: "edge-task",
63
- type: "agentTask",
64
- from: { nodeId: "agent", port: "tool" },
65
- to: { nodeId: "task", port: "trigger" },
66
- prompt: { expression: '"summarize"', references: [], dataType: "string" },
67
- },
68
- {
69
- id: "edge-choice",
70
- type: "agentChoice",
71
- from: { nodeId: "agent", port: "choice" },
72
- to: { nodeId: "branchA", port: "in" },
73
- description: "when the user asks about weather",
74
- },
75
- {
76
- id: "edge-delegate",
77
- type: "agentDelegate",
78
- from: { nodeId: "agent", port: "delegate" },
79
- to: { nodeId: "sub", port: "in" },
80
- prompt: { expression: '"continue"', references: [], dataType: "string" },
81
- description: "delegate everything else",
82
- },
83
- ],
84
- functions: [],
85
- declaredVariables: [],
86
- channels: [],
87
- memory: [],
88
- models: [],
89
- };
90
-
91
- const withFunctionCanvas: Schemas["Workflow"] = {
92
- schemaVersion: 1,
93
- nodes: [
94
- {
95
- id: "fcall",
96
- type: "FunctionCall",
97
- position: { x: 0, y: 0 },
98
- // The wire stores only the reference; the signature is resolved from
99
- // `functions[]` and the snapshot rebuilt on deserialize.
100
- functionId: "fn-uuid",
101
- arguments: {
102
- inputBindings: {
103
- "arg-x": { expression: "1", references: [], dataType: "int" },
104
- "arg-y": { expression: "2", references: [], dataType: "int" },
105
- },
106
- outputBindings: {
107
- "ret-sum": { active: true, mode: "emit", name: "result" },
108
- },
109
- },
110
- },
111
- ],
112
- edges: [],
113
- functions: [
114
- {
115
- functionInfo: {
116
- id: "fn-uuid",
117
- version: 1,
118
- name: "add",
119
- arguments: [
120
- { uid: "arg-x", name: "x", dataType: "int" },
121
- { uid: "arg-y", name: "y", dataType: "int" },
122
- ],
123
- returns: [{ uid: "ret-sum", name: "sum", dataType: "int" }],
124
- },
125
- outputAssignments: {
126
- "ret-sum": { expression: "x + y", references: [
127
- { srcId: "fnarg", varId: "arg-x" },
128
- { srcId: "fnarg", varId: "arg-y" },
129
- ], dataType: "int" },
130
- },
131
- nodes: [],
132
- edges: [],
133
- declaredVariables: [],
134
- },
135
- ],
136
- declaredVariables: [],
137
- channels: [],
138
- memory: [],
139
- models: [],
140
- };
141
-
142
- describe("workflowSerialization — reverse roundtrip (JSON → deserialize → serialize)", () => {
143
- it("empty workflow", () => {
144
- expect(serialize(deserialize(empty))).toEqual(empty);
145
- });
146
-
147
- it("main canvas with declared variables and one node", () => {
148
- expect(serialize(deserialize(mainOnly))).toEqual(mainOnly);
149
- });
150
-
151
- it("preserves all edge type variants verbatim, including ids", () => {
152
- const out = serialize(deserialize(allEdgeTypes));
153
- expect(out).toEqual(allEdgeTypes);
154
- // Specific guardrail: each edge id is preserved (the bug we fixed).
155
- const ids = out.edges.map((e) => e.id).sort();
156
- expect(ids).toEqual(["edge-choice", "edge-control", "edge-delegate", "edge-task", "edge-tool"]);
157
- });
158
-
159
- it("function canvas with fnargs, output assignments, and a FunctionCall on main", () => {
160
- const out = serialize(deserialize(withFunctionCanvas));
161
- expect(out).toEqual(withFunctionCanvas);
162
- });
163
- });
164
-
165
- // ============================================================================
166
- // Forward roundtrip: WorkflowState → serialize → deserialize → equality
167
- //
168
- // Variables are reconstructed from nodes + functionInfo + declared on every
169
- // deserialize. To get exact equality, build fixtures whose `variables` field
170
- // matches what reconstruction would produce (i.e., let buildCanvasVariables
171
- // produce the expected value for us).
172
- // ============================================================================
173
-
174
- function makeMainCanvas(nodes: Canvas["nodes"] = [], edges: Canvas["edges"] = [], declared: Schemas["Variable"][] = []): Canvas {
175
- return {
176
- nodes,
177
- edges,
178
- variables: buildCanvasVariables(nodes, [], declared),
179
- };
180
- }
181
-
182
- describe("workflowSerialization — forward roundtrip (state → serialize → deserialize)", () => {
183
- it("empty state roundtrips to a state with an empty main canvas", () => {
184
- const state: Workflow = { canvases: { [MAIN_CANVAS_ID]: makeMainCanvas() }, functions: {}, channels: {}, memory: {}, models: {} };
185
- expect(deserialize(serialize(state))).toEqual(state);
186
- });
187
-
188
- it("preserves edge ids across serialize/deserialize", () => {
189
- const edges: Canvas["edges"] = [
190
- { id: "stable-id-A", type: "control", source: "n1", sourceHandle: "out", target: "n2", targetHandle: "in" },
191
- { id: "stable-id-B", type: "tool", source: "n1", sourceHandle: "tool", target: "n3", targetHandle: "in" },
192
- ];
193
- const state: Workflow = { canvases: { [MAIN_CANVAS_ID]: makeMainCanvas([], edges) }, functions: {}, channels: {}, memory: {}, models: {} };
194
- const roundTripped = deserialize(serialize(state));
195
- expect(roundTripped.canvases[MAIN_CANVAS_ID]!.edges.map((e) => e.id)).toEqual(["stable-id-A", "stable-id-B"]);
196
- });
197
- });
198
-
199
- // ============================================================================
200
- // Variable reconstruction guardrails
201
- // ============================================================================
202
-
203
- describe("buildCanvasVariables", () => {
204
- it("reconstructs node-output variables from nodes via getNodeOutput", () => {
205
- const nodes: NodeData[] = [
206
- {
207
- id: "agent1",
208
- type: "Agent",
209
- arguments: {
210
- name: "a",
211
- model: "claude-opus-4-7",
212
- instructions: "",
213
- maxTurns: undefined,
214
- outputDeclarations: [],
215
- memoryRefs: [],
216
- answer: { active: true, mode: "emit", name: "answer" },
217
- },
218
- } as NodeData,
219
- ];
220
- const vars = computeVariablesFromNodes(nodes);
221
- // Agent emits at least one output (the answer); key is "<nodeId>:<outputId>".
222
- const keys = Object.keys(vars);
223
- expect(keys.length).toBeGreaterThan(0);
224
- expect(keys.every((k) => k.startsWith("agent1:"))).toBe(true);
225
- });
226
-
227
- it("merges declared + fnarg + node-output into disjoint key namespaces", () => {
228
- const declared: Schemas["Variable"][] = [{ uid: "d1", name: "x", dataType: "int" }];
229
- const fnInfo = {
230
- id: "fn",
231
- version: 1,
232
- name: "f",
233
- arguments: [{ uid: "a1", name: "arg1", dataType: "int" as const }],
234
- returns: [],
235
- };
236
- const merged = buildCanvasVariables([], fnInfo.arguments, declared);
237
- expect(merged["declared:d1"]).toMatchObject({ kind: "declared", uid: "d1" });
238
- expect(merged["fnarg:a1"]).toMatchObject({ kind: "fnarg", uid: "a1" });
239
- });
240
- });
1
+ import { describe, it, expect } from "vitest";
2
+ import { serialize, deserialize, computeVariablesFromNodes, buildCanvasVariables } from "./serialization";
3
+ import { MAIN_CANVAS_ID, type Workflow, type Canvas } from "./Workflow";
4
+ import type { Schemas } from "../api";
5
+ import type { NodeData } from "../node";
6
+
7
+ // ============================================================================
8
+ // Reverse roundtrip: api JSON → deserialize → serialize → deep-equal JSON
9
+ //
10
+ // This direction is the strongest invariant — deserialize is the function
11
+ // that reconstructs derivable state (variable records); if it gets the rest
12
+ // right, re-serialize must produce the same JSON back. Tests every code path
13
+ // in deserialize + serialize without separately constructing WorkflowState.
14
+ // ============================================================================
15
+
16
+ const empty: Schemas["Workflow"] = {
17
+ schemaVersion: 1,
18
+ nodes: [],
19
+ edges: [],
20
+ functions: [],
21
+ declaredVariables: [],
22
+ channels: [],
23
+ memory: [],
24
+ models: [],
25
+ };
26
+
27
+ const mainOnly: Schemas["Workflow"] = {
28
+ schemaVersion: 1,
29
+ nodes: [
30
+ {
31
+ id: "n1",
32
+ type: "Agent",
33
+ position: { x: 100, y: 200 },
34
+ label: "My Agent",
35
+ arguments: {
36
+ name: "agent-1",
37
+ model: "claude-opus-4-7",
38
+ instructions: "be helpful",
39
+ outputDeclarations: [],
40
+ memoryRefs: [],
41
+ answer: { active: true, mode: "emit", name: "answer" },
42
+ },
43
+ },
44
+ ],
45
+ edges: [],
46
+ functions: [],
47
+ declaredVariables: [
48
+ { uid: "d1", name: "counter", dataType: "int", initialValue: 0 },
49
+ ],
50
+ channels: [],
51
+ memory: [],
52
+ models: [],
53
+ };
54
+
55
+ const allEdgeTypes: Schemas["Workflow"] = {
56
+ schemaVersion: 1,
57
+ nodes: [],
58
+ edges: [
59
+ { id: "edge-control", type: "control", from: { nodeId: "a", port: "out" }, to: { nodeId: "b", port: "in" } },
60
+ { id: "edge-tool", type: "tool", from: { nodeId: "a", port: "tool-out" }, to: { nodeId: "b", port: "tool-in" } },
61
+ {
62
+ id: "edge-task",
63
+ type: "agentTask",
64
+ from: { nodeId: "agent", port: "tool" },
65
+ to: { nodeId: "task", port: "trigger" },
66
+ prompt: { expression: '"summarize"', references: [], dataType: "string" },
67
+ },
68
+ {
69
+ id: "edge-choice",
70
+ type: "agentChoice",
71
+ from: { nodeId: "agent", port: "choice" },
72
+ to: { nodeId: "branchA", port: "in" },
73
+ description: "when the user asks about weather",
74
+ },
75
+ {
76
+ id: "edge-delegate",
77
+ type: "agentDelegate",
78
+ from: { nodeId: "agent", port: "delegate" },
79
+ to: { nodeId: "sub", port: "in" },
80
+ prompt: { expression: '"continue"', references: [], dataType: "string" },
81
+ description: "delegate everything else",
82
+ },
83
+ ],
84
+ functions: [],
85
+ declaredVariables: [],
86
+ channels: [],
87
+ memory: [],
88
+ models: [],
89
+ };
90
+
91
+ const withFunctionCanvas: Schemas["Workflow"] = {
92
+ schemaVersion: 1,
93
+ nodes: [
94
+ {
95
+ id: "fcall",
96
+ type: "FunctionCall",
97
+ position: { x: 0, y: 0 },
98
+ // The wire stores only the reference; the signature is resolved from
99
+ // `functions[]` and the snapshot rebuilt on deserialize.
100
+ functionId: "fn-uuid",
101
+ arguments: {
102
+ inputBindings: {
103
+ "arg-x": { expression: "1", references: [], dataType: "int" },
104
+ "arg-y": { expression: "2", references: [], dataType: "int" },
105
+ },
106
+ outputBindings: {
107
+ "ret-sum": { active: true, mode: "emit", name: "result" },
108
+ },
109
+ },
110
+ },
111
+ ],
112
+ edges: [],
113
+ functions: [
114
+ {
115
+ functionInfo: {
116
+ id: "fn-uuid",
117
+ version: 1,
118
+ name: "add",
119
+ arguments: [
120
+ { uid: "arg-x", name: "x", dataType: "int" },
121
+ { uid: "arg-y", name: "y", dataType: "int" },
122
+ ],
123
+ returns: [{ uid: "ret-sum", name: "sum", dataType: "int" }],
124
+ },
125
+ outputAssignments: {
126
+ "ret-sum": { expression: "x + y", references: [
127
+ { srcId: "fnarg", varId: "arg-x" },
128
+ { srcId: "fnarg", varId: "arg-y" },
129
+ ], dataType: "int" },
130
+ },
131
+ nodes: [],
132
+ edges: [],
133
+ declaredVariables: [],
134
+ },
135
+ ],
136
+ declaredVariables: [],
137
+ channels: [],
138
+ memory: [],
139
+ models: [],
140
+ };
141
+
142
+ describe("workflowSerialization — reverse roundtrip (JSON → deserialize → serialize)", () => {
143
+ it("empty workflow", () => {
144
+ expect(serialize(deserialize(empty))).toEqual(empty);
145
+ });
146
+
147
+ it("main canvas with declared variables and one node", () => {
148
+ expect(serialize(deserialize(mainOnly))).toEqual(mainOnly);
149
+ });
150
+
151
+ it("preserves all edge type variants verbatim, including ids", () => {
152
+ const out = serialize(deserialize(allEdgeTypes));
153
+ expect(out).toEqual(allEdgeTypes);
154
+ // Specific guardrail: each edge id is preserved (the bug we fixed).
155
+ const ids = out.edges.map((e) => e.id).sort();
156
+ expect(ids).toEqual(["edge-choice", "edge-control", "edge-delegate", "edge-task", "edge-tool"]);
157
+ });
158
+
159
+ it("function canvas with fnargs, output assignments, and a FunctionCall on main", () => {
160
+ const out = serialize(deserialize(withFunctionCanvas));
161
+ expect(out).toEqual(withFunctionCanvas);
162
+ });
163
+ });
164
+
165
+ // ============================================================================
166
+ // Forward roundtrip: WorkflowState → serialize → deserialize → equality
167
+ //
168
+ // Variables are reconstructed from nodes + functionInfo + declared on every
169
+ // deserialize. To get exact equality, build fixtures whose `variables` field
170
+ // matches what reconstruction would produce (i.e., let buildCanvasVariables
171
+ // produce the expected value for us).
172
+ // ============================================================================
173
+
174
+ function makeMainCanvas(nodes: Canvas["nodes"] = [], edges: Canvas["edges"] = [], declared: Schemas["Variable"][] = []): Canvas {
175
+ return {
176
+ nodes,
177
+ edges,
178
+ variables: buildCanvasVariables(nodes, [], declared),
179
+ };
180
+ }
181
+
182
+ describe("workflowSerialization — forward roundtrip (state → serialize → deserialize)", () => {
183
+ it("empty state roundtrips to a state with an empty main canvas", () => {
184
+ const state: Workflow = { canvases: { [MAIN_CANVAS_ID]: makeMainCanvas() }, functions: {}, channels: {}, memory: {}, models: {} };
185
+ expect(deserialize(serialize(state))).toEqual(state);
186
+ });
187
+
188
+ it("preserves edge ids across serialize/deserialize", () => {
189
+ const edges: Canvas["edges"] = [
190
+ { id: "stable-id-A", type: "control", source: "n1", sourceHandle: "out", target: "n2", targetHandle: "in" },
191
+ { id: "stable-id-B", type: "tool", source: "n1", sourceHandle: "tool", target: "n3", targetHandle: "in" },
192
+ ];
193
+ const state: Workflow = { canvases: { [MAIN_CANVAS_ID]: makeMainCanvas([], edges) }, functions: {}, channels: {}, memory: {}, models: {} };
194
+ const roundTripped = deserialize(serialize(state));
195
+ expect(roundTripped.canvases[MAIN_CANVAS_ID]!.edges.map((e) => e.id)).toEqual(["stable-id-A", "stable-id-B"]);
196
+ });
197
+ });
198
+
199
+ // ============================================================================
200
+ // Variable reconstruction guardrails
201
+ // ============================================================================
202
+
203
+ describe("buildCanvasVariables", () => {
204
+ it("reconstructs node-output variables from nodes via getNodeOutput", () => {
205
+ const nodes: NodeData[] = [
206
+ {
207
+ id: "agent1",
208
+ type: "Agent",
209
+ arguments: {
210
+ name: "a",
211
+ model: "claude-opus-4-7",
212
+ instructions: "",
213
+ maxTurns: undefined,
214
+ outputDeclarations: [],
215
+ memoryRefs: [],
216
+ answer: { active: true, mode: "emit", name: "answer" },
217
+ },
218
+ } as NodeData,
219
+ ];
220
+ const vars = computeVariablesFromNodes(nodes);
221
+ // Agent emits at least one output (the answer); key is "<nodeId>:<outputId>".
222
+ const keys = Object.keys(vars);
223
+ expect(keys.length).toBeGreaterThan(0);
224
+ expect(keys.every((k) => k.startsWith("agent1:"))).toBe(true);
225
+ });
226
+
227
+ it("merges declared + fnarg + node-output into disjoint key namespaces", () => {
228
+ const declared: Schemas["Variable"][] = [{ uid: "d1", name: "x", dataType: "int" }];
229
+ const fnInfo = {
230
+ id: "fn",
231
+ version: 1,
232
+ name: "f",
233
+ arguments: [{ uid: "a1", name: "arg1", dataType: "int" as const }],
234
+ returns: [],
235
+ };
236
+ const merged = buildCanvasVariables([], fnInfo.arguments, declared);
237
+ expect(merged["declared:d1"]).toMatchObject({ kind: "declared", uid: "d1" });
238
+ expect(merged["fnarg:a1"]).toMatchObject({ kind: "fnarg", uid: "a1" });
239
+ });
240
+ });