@codemation/core 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/{EngineRuntimeRegistration.types-Bjeo7Sfq.d.ts → EngineRuntimeRegistration.types-BtTZolK0.d.ts} +2 -2
  3. package/dist/{EngineWorkflowRunnerService-Dd4yD31l.d.cts → EngineWorkflowRunnerService-Ddl0fekp.d.cts} +2 -2
  4. package/dist/{InMemoryRunDataFactory-OUzDmAHt.d.cts → InMemoryRunDataFactory-i-u2yngD.d.cts} +11 -3
  5. package/dist/{RunIntentService-Bkg4oYrM.d.cts → RunIntentService-Cjx-glgz.d.cts} +232 -237
  6. package/dist/{RunIntentService-BAKikN8h.d.ts → RunIntentService-Dkr4YwN8.d.ts} +313 -259
  7. package/dist/bootstrap/index.cjs +2 -2
  8. package/dist/bootstrap/index.d.cts +19 -7
  9. package/dist/bootstrap/index.d.ts +3 -3
  10. package/dist/bootstrap/index.js +2 -2
  11. package/dist/{bootstrap-DwS5S7s9.cjs → bootstrap-DHH2uo-W.cjs} +4 -2
  12. package/dist/bootstrap-DHH2uo-W.cjs.map +1 -0
  13. package/dist/{bootstrap-BD6CobHl.js → bootstrap-DbUlOl11.js} +4 -2
  14. package/dist/bootstrap-DbUlOl11.js.map +1 -0
  15. package/dist/{index-uCm9l0nw.d.ts → index-B2v4wtys.d.ts} +62 -34
  16. package/dist/index.cjs +22 -15
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +108 -42
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +13 -16
  21. package/dist/index.js.map +1 -1
  22. package/dist/{runtime-Cy-3FTI_.js → runtime-BdH94eBR.js} +502 -123
  23. package/dist/runtime-BdH94eBR.js.map +1 -0
  24. package/dist/{runtime-ZJUpWmPH.cjs → runtime-feFn8OmG.cjs} +561 -122
  25. package/dist/runtime-feFn8OmG.cjs.map +1 -0
  26. package/dist/testing.cjs +40 -36
  27. package/dist/testing.cjs.map +1 -1
  28. package/dist/testing.d.cts +17 -26
  29. package/dist/testing.d.ts +17 -26
  30. package/dist/testing.js +40 -36
  31. package/dist/testing.js.map +1 -1
  32. package/dist/{workflowActivationPolicy-BzyzXLa_.cjs → workflowActivationPolicy-6V3OJD3N.cjs} +65 -19
  33. package/dist/workflowActivationPolicy-6V3OJD3N.cjs.map +1 -0
  34. package/dist/{workflowActivationPolicy-B8HzTk3o.js → workflowActivationPolicy-Td9HTOuD.js} +65 -19
  35. package/dist/workflowActivationPolicy-Td9HTOuD.js.map +1 -0
  36. package/package.json +2 -1
  37. package/src/ai/AgentConfigInspectorFactory.ts +4 -0
  38. package/src/ai/AgentMessageConfigNormalizerFactory.ts +7 -0
  39. package/src/ai/AgentToolFactory.ts +2 -2
  40. package/src/ai/AiHost.ts +11 -10
  41. package/src/ai/NodeBackedToolConfig.ts +1 -1
  42. package/src/authoring/defineNode.types.ts +48 -72
  43. package/src/authoring/index.ts +1 -1
  44. package/src/bootstrap/runtime/EngineRuntimeRegistrar.ts +8 -0
  45. package/src/contracts/credentialTypes.ts +9 -0
  46. package/src/contracts/emitPorts.ts +27 -0
  47. package/src/contracts/index.ts +3 -0
  48. package/src/contracts/itemMeta.ts +11 -0
  49. package/src/contracts/itemValue.ts +147 -0
  50. package/src/contracts/runtimeTypes.ts +39 -22
  51. package/src/contracts/workflowTypes.ts +26 -56
  52. package/src/execution/FanInMergeByOriginMerger.ts +67 -0
  53. package/src/execution/ItemValueResolver.ts +27 -0
  54. package/src/execution/NodeActivationRequestComposer.ts +25 -0
  55. package/src/execution/NodeActivationRequestInputPreparer.ts +57 -25
  56. package/src/execution/NodeExecutor.ts +199 -30
  57. package/src/execution/NodeOutputNormalizer.ts +90 -0
  58. package/src/execution/index.ts +2 -0
  59. package/src/index.ts +2 -0
  60. package/src/orchestration/NodeExecutionRequestHandlerService.ts +39 -18
  61. package/src/orchestration/RunContinuationService.ts +11 -17
  62. package/src/planning/CurrentStateFrontierPlanner.ts +20 -20
  63. package/src/planning/RunQueuePlanner.ts +56 -19
  64. package/src/planning/WorkflowTopologyPlanner.ts +57 -33
  65. package/src/testing/ItemHarnessNode.ts +4 -10
  66. package/src/testing/ItemHarnessNodeConfig.ts +7 -16
  67. package/src/testing/RegistrarEngineTestKitFactory.ts +2 -0
  68. package/src/testing/SubWorkflowRunnerTestNode.ts +28 -43
  69. package/src/testing/SwitchHarnessNode.ts +54 -0
  70. package/src/types/index.ts +3 -0
  71. package/src/workflow/dsl/ChainCursorResolver.ts +68 -23
  72. package/src/workflow/dsl/WorkflowBuilder.ts +3 -5
  73. package/src/workflow/dsl/workflowBuilderTypes.ts +5 -8
  74. package/src/workflowSnapshots/MissingRuntimeNode.ts +4 -4
  75. package/src/workflowSnapshots/MissingRuntimeNodeConfig.ts +2 -2
  76. package/src/workflowSnapshots/WorkflowSnapshotCodec.ts +16 -7
  77. package/dist/bootstrap-BD6CobHl.js.map +0 -1
  78. package/dist/bootstrap-DwS5S7s9.cjs.map +0 -1
  79. package/dist/runtime-Cy-3FTI_.js.map +0 -1
  80. package/dist/runtime-ZJUpWmPH.cjs.map +0 -1
  81. package/dist/workflowActivationPolicy-B8HzTk3o.js.map +0 -1
  82. package/dist/workflowActivationPolicy-BzyzXLa_.cjs.map +0 -1
@@ -1,80 +1,249 @@
1
+ import { z } from "zod";
2
+ import { isPortsEmission, isUnbrandedPortsEmissionShape } from "../contracts/emitPorts";
3
+
1
4
  import type {
2
5
  Item,
6
+ LineageCarryPolicy,
3
7
  MultiInputNode,
4
- Node,
5
8
  NodeActivationRequest,
9
+ NodeConfigBase,
10
+ NodeExecutionContext,
6
11
  NodeOutputs,
12
+ RunnableNode,
13
+ RunnableNodeConfig,
14
+ RunnableNodeExecuteArgs,
15
+ TriggerNode,
7
16
  WorkflowNodeInstanceFactory,
8
17
  } from "../types";
9
18
 
19
+ import { FanInMergeByOriginMerger } from "./FanInMergeByOriginMerger";
20
+ import { ItemValueResolver } from "./ItemValueResolver";
10
21
  import { InProcessRetryRunner } from "./InProcessRetryRunner";
22
+ import { NodeOutputNormalizer } from "./NodeOutputNormalizer";
11
23
 
12
24
  export class NodeExecutor {
25
+ private readonly fanInMerger = new FanInMergeByOriginMerger();
26
+ private readonly outputNormalizer = new NodeOutputNormalizer();
27
+ private readonly itemValueResolver: ItemValueResolver;
28
+
13
29
  constructor(
14
30
  private readonly nodeInstanceFactory: WorkflowNodeInstanceFactory,
15
31
  private readonly retryRunner: InProcessRetryRunner,
16
- ) {}
32
+ itemValueResolver?: ItemValueResolver,
33
+ ) {
34
+ this.itemValueResolver = itemValueResolver ?? new ItemValueResolver();
35
+ }
17
36
 
18
37
  async execute(request: NodeActivationRequest): Promise<NodeOutputs> {
19
38
  const policy = request.ctx.config.retryPolicy;
20
39
  return await this.retryRunner.run(policy, async () => {
21
40
  const nodeInstance = this.nodeInstanceFactory.createByType(request.ctx.config.type);
22
41
  if (request.kind === "multi") {
23
- return await this.executeMultiInputNode(request, nodeInstance);
42
+ return await this.executeMultiInputActivation(request, nodeInstance);
24
43
  }
25
44
  return await this.executeSingleInputNode(request, nodeInstance);
26
45
  });
27
46
  }
28
47
 
29
- private async executeMultiInputNode(
48
+ private async executeMultiInputActivation(
30
49
  request: Extract<NodeActivationRequest, { kind: "multi" }>,
31
50
  node: unknown,
32
51
  ): Promise<NodeOutputs> {
33
52
  const multiInputNode = node as MultiInputNode;
34
- if (typeof (multiInputNode as { executeMulti?: unknown }).executeMulti !== "function") {
35
- throw new Error(`Node ${request.nodeId} does not support executeMulti but received multi-input activation`);
53
+ if (typeof (multiInputNode as { executeMulti?: unknown }).executeMulti === "function") {
54
+ const raw = await multiInputNode.executeMulti(request.inputsByPort, request.ctx as never);
55
+ this.assertNoPortEnvelopeBypass(request.nodeId, raw, "executeMulti()");
56
+ return raw;
36
57
  }
37
- return await multiInputNode.executeMulti(request.inputsByPort, request.ctx as any);
58
+ if (this.isRunnableNode(node)) {
59
+ const merged = this.fanInMerger.merge(request.inputsByPort);
60
+ const single: Extract<NodeActivationRequest, { kind: "single" }> = {
61
+ ...request,
62
+ kind: "single",
63
+ input: merged,
64
+ };
65
+ return await this.executeRunnableActivation(single, node);
66
+ }
67
+ throw new Error(
68
+ `Node ${request.nodeId} does not support executeMulti or RunnableNode.execute but received multi-input activation`,
69
+ );
38
70
  }
39
71
 
40
72
  private async executeSingleInputNode(
41
73
  request: Extract<NodeActivationRequest, { kind: "single" }>,
42
74
  node: unknown,
43
75
  ): Promise<NodeOutputs> {
44
- if (this.hasExecuteOne(node)) {
45
- return await this.executeItemNode(request, node);
76
+ if (this.isTriggerNode(node)) {
77
+ const raw = await (node as TriggerNode).execute(request.input, request.ctx as never);
78
+ this.assertNoPortEnvelopeBypass(request.nodeId, raw, "trigger execute()");
79
+ return raw;
46
80
  }
47
- const singleInputNode = node as Node;
48
- if (typeof (singleInputNode as { execute?: unknown }).execute !== "function") {
49
- throw new Error(`Node ${request.nodeId} does not support execute but received single-input activation`);
81
+ if (this.isRunnableNode(node)) {
82
+ return await this.executeRunnableActivation(request, node);
50
83
  }
51
- return await singleInputNode.execute(request.input, request.ctx as any);
84
+ if (this.hasExecuteMulti(node)) {
85
+ return await this.executeMultiInputActivation(this.asMultiFromSingleActivation(request), node);
86
+ }
87
+ throw new Error(`Node ${request.nodeId} does not support trigger or RunnableNode execution`);
88
+ }
89
+
90
+ private isTriggerNode(node: unknown): node is TriggerNode {
91
+ return typeof node === "object" && node !== null && (node as { kind?: string }).kind === "trigger";
52
92
  }
53
93
 
54
- private hasExecuteOne(node: unknown): node is { executeOne: (args: unknown) => unknown | Promise<unknown> } {
94
+ private isRunnableNode(node: unknown): node is RunnableNode {
55
95
  return (
56
- typeof node === "object" && node !== null && typeof (node as { executeOne?: unknown }).executeOne === "function"
96
+ typeof node === "object" &&
97
+ node !== null &&
98
+ (node as { kind?: string }).kind === "node" &&
99
+ typeof (node as { execute?: unknown }).execute === "function"
57
100
  );
58
101
  }
59
102
 
60
- private async executeItemNode(
103
+ private hasExecuteMulti(node: unknown): boolean {
104
+ return typeof (node as { executeMulti?: unknown })?.executeMulti === "function";
105
+ }
106
+
107
+ private asMultiFromSingleActivation(
61
108
  request: Extract<NodeActivationRequest, { kind: "single" }>,
62
- node: { executeOne: (args: unknown) => unknown | Promise<unknown> },
109
+ ): Extract<NodeActivationRequest, { kind: "multi" }> {
110
+ return {
111
+ kind: "multi",
112
+ runId: request.runId,
113
+ activationId: request.activationId,
114
+ workflowId: request.workflowId,
115
+ nodeId: request.nodeId,
116
+ parent: request.parent,
117
+ executionOptions: request.executionOptions,
118
+ batchId: request.batchId,
119
+ ctx: request.ctx,
120
+ inputsByPort: { in: request.input ?? [] },
121
+ };
122
+ }
123
+
124
+ private async executeRunnableActivation(
125
+ request: Extract<NodeActivationRequest, { kind: "single" }>,
126
+ node: RunnableNode,
63
127
  ): Promise<NodeOutputs> {
64
- const out: Item[] = [];
65
- for (let i = 0; i < request.input.length; i++) {
66
- const item = request.input[i] as Item;
67
- const outputJson = await Promise.resolve(
68
- node.executeOne({
69
- input: item.json,
70
- item,
71
- itemIndex: i,
72
- items: request.input,
73
- ctx: request.ctx,
74
- }),
128
+ const runnableConfig = request.ctx.config as RunnableNodeConfig;
129
+ const carry = this.resolveLineageCarry(node, runnableConfig);
130
+ const inputSchema = this.resolveInputSchema(node, runnableConfig);
131
+ const inputBatch = request.input ?? [];
132
+ if (inputBatch.length === 0 && runnableConfig.emptyBatchExecution === "runOnce") {
133
+ const syntheticItem: Item = { json: {} };
134
+ const parsed = inputSchema.parse(syntheticItem.json);
135
+ const runnableCtx = request.ctx as NodeExecutionContext<RunnableNodeConfig>;
136
+ const resolvedCtx = await this.itemValueResolver.resolveConfigForItem(runnableCtx, syntheticItem, 0, inputBatch);
137
+ const ctx = this.pickExecutionContext(runnableCtx, resolvedCtx);
138
+ const args: RunnableNodeExecuteArgs = {
139
+ input: parsed,
140
+ item: syntheticItem,
141
+ itemIndex: 0,
142
+ items: inputBatch,
143
+ ctx,
144
+ };
145
+ const raw = await Promise.resolve(node.execute(args));
146
+ return this.outputNormalizer.normalizeExecuteResult({
147
+ baseItem: syntheticItem,
148
+ raw,
149
+ carry,
150
+ }) as NodeOutputs;
151
+ }
152
+ const byPort: Partial<Record<string, Item[]>> = {};
153
+ for (let i = 0; i < inputBatch.length; i++) {
154
+ const item = inputBatch[i] as Item;
155
+ this.assertItemJsonNotTopLevelArray(request.nodeId, item);
156
+ const parsed = inputSchema.parse(item.json);
157
+ const runnableCtx = request.ctx as NodeExecutionContext<RunnableNodeConfig>;
158
+ const resolvedCtx = await this.itemValueResolver.resolveConfigForItem(runnableCtx, item, i, inputBatch);
159
+ const ctx = this.pickExecutionContext(runnableCtx, resolvedCtx);
160
+ const args: RunnableNodeExecuteArgs = {
161
+ input: parsed,
162
+ item,
163
+ itemIndex: i,
164
+ items: inputBatch,
165
+ ctx,
166
+ };
167
+ const raw = await Promise.resolve(node.execute(args));
168
+ const normalized = this.outputNormalizer.normalizeExecuteResult({
169
+ baseItem: item,
170
+ raw,
171
+ carry,
172
+ });
173
+ for (const [port, batch] of Object.entries(normalized)) {
174
+ if (!batch || batch.length === 0) {
175
+ continue;
176
+ }
177
+ const list = byPort[port] ?? [];
178
+ list.push(...batch);
179
+ byPort[port] = list;
180
+ }
181
+ }
182
+ return byPort as NodeOutputs;
183
+ }
184
+
185
+ /** Use resolver ctx only when {@link NodeExecutionContext.config} is non-nullish. */
186
+ private pickExecutionContext<TConfig extends RunnableNodeConfig<any, any>>(
187
+ runnableCtx: NodeExecutionContext<TConfig>,
188
+ resolvedCtx: NodeExecutionContext<TConfig> | null | undefined,
189
+ ): NodeExecutionContext<TConfig> {
190
+ if (resolvedCtx != null && resolvedCtx.config != null) {
191
+ return resolvedCtx;
192
+ }
193
+ return runnableCtx;
194
+ }
195
+
196
+ private resolveInputSchema(
197
+ nodeInstance: RunnableNode,
198
+ config: RunnableNodeConfig,
199
+ ): {
200
+ parse: (data: unknown) => unknown;
201
+ } {
202
+ const fromInstance = nodeInstance.inputSchema;
203
+ if (fromInstance && typeof fromInstance.parse === "function") {
204
+ return fromInstance as { parse: (data: unknown) => unknown };
205
+ }
206
+ const fromConfig = config.inputSchema;
207
+ if (fromConfig && typeof fromConfig.parse === "function") {
208
+ return fromConfig as { parse: (data: unknown) => unknown };
209
+ }
210
+ return z.unknown();
211
+ }
212
+
213
+ private assertItemJsonNotTopLevelArray(nodeId: string, item: Item): void {
214
+ if (Array.isArray(item.json)) {
215
+ throw new Error(`Node ${nodeId}: item.json must not be a top-level JSON array`);
216
+ }
217
+ }
218
+
219
+ private assertNoPortEnvelopeBypass(nodeId: string, value: unknown, methodName: string): void {
220
+ if (isPortsEmission(value)) {
221
+ throw new Error(`Node ${nodeId}: ${methodName} must return NodeOutputs, not emitPorts(...).`);
222
+ }
223
+ if (isUnbrandedPortsEmissionShape(value)) {
224
+ throw new Error(
225
+ `Node ${nodeId}: ${methodName} returned an unbranded \`{ ports: ... }\` object. Return NodeOutputs instead.`,
75
226
  );
76
- out.push({ ...item, json: outputJson });
77
227
  }
78
- return { main: out };
228
+ }
229
+
230
+ private resolveLineageCarry(node: unknown, config: RunnableNodeConfig): LineageCarryPolicy {
231
+ if (config.lineageCarry) {
232
+ return config.lineageCarry;
233
+ }
234
+
235
+ const base = config as NodeConfigBase;
236
+ const declared = base.declaredOutputPorts;
237
+ const ports =
238
+ declared && declared.length > 0
239
+ ? [...new Set([...(declared as readonly string[]), ...(base.nodeErrorHandler ? ["error"] : [])])]
240
+ : base.nodeErrorHandler
241
+ ? (["main", "error"] as const)
242
+ : (["main"] as const);
243
+
244
+ if (ports.length > 1) {
245
+ return "carryThrough";
246
+ }
247
+ return "emitOnly";
79
248
  }
80
249
  }
@@ -0,0 +1,90 @@
1
+ import { isPortsEmission, isUnbrandedPortsEmissionShape } from "../contracts/emitPorts";
2
+ import type { PortsEmission } from "../contracts/emitPorts";
3
+ import type { Item, JsonNonArray, LineageCarryPolicy, NodeOutputs, OutputPortKey, Items } from "../types";
4
+
5
+ export class NodeOutputNormalizer {
6
+ normalizeExecuteResult(
7
+ args: Readonly<{
8
+ baseItem: Item;
9
+ raw: unknown;
10
+ carry: LineageCarryPolicy;
11
+ }>,
12
+ ): NodeOutputs {
13
+ const { baseItem, raw, carry } = args;
14
+ if (isPortsEmission(raw)) {
15
+ return this.emitPortsToOutputs(baseItem, raw, carry);
16
+ }
17
+ if (isUnbrandedPortsEmissionShape(raw)) {
18
+ throw new Error(
19
+ "execute() returned an unbranded `{ ports: ... }` object. Use emitPorts(...) for multi-port runnable outputs.",
20
+ );
21
+ }
22
+ if (Array.isArray(raw)) {
23
+ return this.arrayFanOutToMain(baseItem, raw, carry);
24
+ }
25
+ if (this.isItemLike(raw)) {
26
+ return { main: [this.applyLineage(baseItem, raw, carry)] };
27
+ }
28
+ return {
29
+ main: [this.applyLineage(baseItem, { json: raw as JsonNonArray }, carry)],
30
+ };
31
+ }
32
+
33
+ private arrayFanOutToMain(baseItem: Item, raw: readonly unknown[], carry: LineageCarryPolicy): NodeOutputs {
34
+ for (const el of raw) {
35
+ if (Array.isArray(el)) {
36
+ throw new Error(
37
+ "execute() fan-out arrays must contain only non-array JSON elements (nested arrays belong inside objects).",
38
+ );
39
+ }
40
+ }
41
+ const main: Item[] = raw.map((json) => this.applyLineage(baseItem, { json: json as JsonNonArray }, carry));
42
+ return { main };
43
+ }
44
+
45
+ private emitPortsToOutputs(baseItem: Item, emission: PortsEmission, carry: LineageCarryPolicy): NodeOutputs {
46
+ const out: NodeOutputs = {};
47
+ for (const [port, payload] of Object.entries(emission.ports)) {
48
+ if (payload === undefined) {
49
+ continue;
50
+ }
51
+ out[port as OutputPortKey] = this.normalizePortPayload(baseItem, payload, carry);
52
+ }
53
+ return out;
54
+ }
55
+
56
+ private normalizePortPayload(
57
+ baseItem: Item,
58
+ payload: Items | ReadonlyArray<JsonNonArray>,
59
+ carry: LineageCarryPolicy,
60
+ ): Items {
61
+ if (payload.length === 0) {
62
+ return [];
63
+ }
64
+ const el0 = payload[0] as unknown;
65
+ if (this.isItemLike(el0)) {
66
+ return (payload as Items).map((it) => this.applyLineage(baseItem, it, carry));
67
+ }
68
+ return (payload as readonly JsonNonArray[]).map((json) => this.applyLineage(baseItem, { json }, carry));
69
+ }
70
+
71
+ private isItemLike(value: unknown): value is Item {
72
+ return typeof value === "object" && value !== null && "json" in value;
73
+ }
74
+
75
+ private applyLineage(baseItem: Item, next: Item, carry: LineageCarryPolicy): Item {
76
+ if (carry === "carryThrough") {
77
+ return {
78
+ ...baseItem,
79
+ ...next,
80
+ json: next.json,
81
+ };
82
+ }
83
+ return {
84
+ json: next.json,
85
+ ...(next.binary ? { binary: next.binary } : {}),
86
+ ...(next.meta ? { meta: next.meta } : {}),
87
+ ...(next.paired ? { paired: next.paired } : {}),
88
+ };
89
+ }
90
+ }
@@ -5,6 +5,8 @@ export { CredentialResolverFactory } from "./CredentialResolverFactory";
5
5
  export { DefaultAsyncSleeper } from "./DefaultAsyncSleeper";
6
6
  export { DefaultExecutionContextFactory } from "./DefaultExecutionContextFactory";
7
7
  export { InProcessRetryRunner } from "./InProcessRetryRunner";
8
+ export { ItemValueResolver } from "./ItemValueResolver";
9
+ export { NodeOutputNormalizer } from "./NodeOutputNormalizer";
8
10
  export { InProcessRetryRunnerFactory } from "./InProcessRetryRunnerFactory";
9
11
  export { NodeActivationRequestComposer } from "./NodeActivationRequestComposer";
10
12
  export { NodeExecutionSnapshotFactory } from "./NodeExecutionSnapshotFactory";
package/src/index.ts CHANGED
@@ -18,6 +18,8 @@ export {
18
18
  DefaultAsyncSleeper,
19
19
  DefaultExecutionContextFactory,
20
20
  InProcessRetryRunner,
21
+ ItemValueResolver,
22
+ NodeOutputNormalizer,
21
23
  } from "./execution";
22
24
  export { EngineExecutionLimitsPolicy, type EngineExecutionLimitsPolicyConfig } from "./policies";
23
25
  export { InMemoryBinaryStorage, InMemoryRunDataFactory } from "./runStorage";
@@ -66,7 +66,6 @@ export class NodeExecutionRequestHandlerService implements NodeExecutionRequestH
66
66
  const resolvedParent = request.parent ?? state.parent;
67
67
  const data = this.runDataFactory.create(state.outputsByNode);
68
68
  const limits = this.resolveEngineLimitsFromState(state);
69
- const persistedInput = pendingExecution.inputsByPort.in ?? request.input;
70
69
  const base = this.runExecutionContextFactory.create({
71
70
  runId: state.runId,
72
71
  workflowId: state.workflowId,
@@ -78,21 +77,43 @@ export class NodeExecutionRequestHandlerService implements NodeExecutionRequestH
78
77
  data,
79
78
  nodeState: this.nodeStatePublisherFactory.create(state.runId, state.workflowId, resolvedParent),
80
79
  });
81
- const activationRequest = this.nodeActivationRequestComposer.createSingleFromDefinitionWithActivation({
82
- activationId: request.activationId,
83
- runId: request.runId,
84
- workflowId: request.workflowId,
85
- parent: resolvedParent,
86
- executionOptions: request.executionOptions ?? state.executionOptions,
87
- base,
88
- data,
89
- definition: {
90
- id: definition.id,
91
- config: definition.config,
92
- },
93
- batchId: pendingExecution.batchId ?? "batch_1",
94
- input: persistedInput,
95
- });
80
+
81
+ const inputsByPort = pendingExecution.inputsByPort;
82
+ const portKeys = Object.keys(inputsByPort);
83
+ const kind = portKeys.length === 1 && portKeys[0] === "in" ? ("single" as const) : ("multi" as const);
84
+ const batchId = pendingExecution.batchId ?? "batch_1";
85
+ const activationRequest =
86
+ kind === "multi"
87
+ ? this.nodeActivationRequestComposer.createMultiFromDefinitionWithActivation({
88
+ activationId: request.activationId,
89
+ runId: request.runId,
90
+ workflowId: request.workflowId,
91
+ parent: resolvedParent,
92
+ executionOptions: request.executionOptions ?? state.executionOptions,
93
+ base,
94
+ data,
95
+ definition: {
96
+ id: definition.id,
97
+ config: definition.config,
98
+ },
99
+ batchId,
100
+ inputsByPort,
101
+ })
102
+ : this.nodeActivationRequestComposer.createSingleFromDefinitionWithActivation({
103
+ activationId: request.activationId,
104
+ runId: request.runId,
105
+ workflowId: request.workflowId,
106
+ parent: resolvedParent,
107
+ executionOptions: request.executionOptions ?? state.executionOptions,
108
+ base,
109
+ data,
110
+ definition: {
111
+ id: definition.id,
112
+ config: definition.config,
113
+ },
114
+ batchId,
115
+ input: inputsByPort.in ?? request.input ?? [],
116
+ });
96
117
 
97
118
  await this.continuation.markNodeRunning({
98
119
  runId: activationRequest.runId,
@@ -131,7 +152,7 @@ export class NodeExecutionRequestHandlerService implements NodeExecutionRequestH
131
152
  }
132
153
 
133
154
  private async resumeAfterExecutionResult(
134
- request: ReturnType<NodeActivationRequestComposer["createSingleFromDefinitionWithActivation"]>,
155
+ request: Readonly<{ runId: string; activationId: string; nodeId: string }>,
135
156
  outputs: unknown,
136
157
  ): Promise<void> {
137
158
  try {
@@ -147,7 +168,7 @@ export class NodeExecutionRequestHandlerService implements NodeExecutionRequestH
147
168
  }
148
169
 
149
170
  private async resumeAfterExecutionError(
150
- request: ReturnType<NodeActivationRequestComposer["createSingleFromDefinitionWithActivation"]>,
171
+ request: Readonly<{ runId: string; activationId: string; nodeId: string }>,
151
172
  error: Error,
152
173
  ): Promise<void> {
153
174
  try {
@@ -335,7 +335,7 @@ export class RunContinuationService {
335
335
  nextNodeId: next.nodeId,
336
336
  request,
337
337
  completedSnapshot,
338
- nextNodeSnapshotsByNodeId,
338
+ nextNodeSnapshotsByNodeId: nextNodeSnapshotsByNodeId,
339
339
  outputsByNode: data.dump(),
340
340
  engineCounters,
341
341
  error,
@@ -653,6 +653,12 @@ export class RunContinuationService {
653
653
  planner.applyOutputs(queue, { fromNodeId: args.args.nodeId, outputs: triggerOutputs as any, batchId });
654
654
  const next = planner.nextActivation(queue);
655
655
 
656
+ const finishedAt = completedSnapshot.finishedAt ?? completedSnapshot.updatedAt;
657
+ const mergedSnapshots = {
658
+ ...(args.state.nodeSnapshotsByNodeId ?? {}),
659
+ [args.args.nodeId]: completedSnapshot,
660
+ };
661
+
656
662
  if (!next) {
657
663
  const lastNodeId = WorkflowExecutableNodeClassifierFactory.create(
658
664
  args.workflow,
@@ -664,11 +670,8 @@ export class RunContinuationService {
664
670
  status: "completed",
665
671
  queue: [],
666
672
  outputsByNode: data.dump(),
667
- nodeSnapshotsByNodeId: {
668
- ...(args.state.nodeSnapshotsByNodeId ?? {}),
669
- [args.args.nodeId]: completedSnapshot,
670
- },
671
- finishedAtIso: completedSnapshot.finishedAt ?? completedSnapshot.updatedAt,
673
+ nodeSnapshotsByNodeId: mergedSnapshots,
674
+ finishedAtIso: finishedAt,
672
675
  });
673
676
  await this.workflowExecutionRepository.save(completedState);
674
677
  await this.nodeEventPublisher.publish("nodeCompleted", completedSnapshot);
@@ -705,11 +708,8 @@ export class RunContinuationService {
705
708
  status: "failed",
706
709
  queue: queue.map((q) => ({ ...q })),
707
710
  outputsByNode: data.dump(),
708
- nodeSnapshotsByNodeId: {
709
- ...(args.state.nodeSnapshotsByNodeId ?? {}),
710
- [args.args.nodeId]: completedSnapshot,
711
- },
712
- finishedAtIso: completedSnapshot.finishedAt ?? completedSnapshot.updatedAt,
711
+ nodeSnapshotsByNodeId: mergedSnapshots,
712
+ finishedAtIso: finishedAt,
713
713
  });
714
714
  await this.workflowExecutionRepository.save(failedState);
715
715
  await this.nodeEventPublisher.publish("nodeCompleted", completedSnapshot);
@@ -765,11 +765,6 @@ export class RunContinuationService {
765
765
  nodeDefinition: nextDefinition,
766
766
  });
767
767
 
768
- const mergedSnapshots = {
769
- ...(args.state.nodeSnapshotsByNodeId ?? {}),
770
- [args.args.nodeId]: completedSnapshot,
771
- };
772
-
773
768
  try {
774
769
  const { queuedSnapshot, result } = await this.activationEnqueueService.enqueueActivationWithSnapshot({
775
770
  runId: args.state.runId,
@@ -800,7 +795,6 @@ export class RunContinuationService {
800
795
  return result;
801
796
  } catch (cause) {
802
797
  const error = cause instanceof Error ? cause : new Error(String(cause));
803
- const finishedAt = completedSnapshot.finishedAt ?? completedSnapshot.updatedAt;
804
798
  const result = await this.terminateRunAfterActivationEnqueueRejected({
805
799
  wf: args.workflow,
806
800
  state: args.state,
@@ -200,7 +200,7 @@ export class CurrentStateFrontierPlanner {
200
200
  continue;
201
201
  }
202
202
  const incomingEdges = this.topology.incomingByNode.get(nodeId) ?? [];
203
- const isFrontier = incomingEdges.every((edge) => this.isEdgeSatisfied(currentState, nodeId, edge.input));
203
+ const isFrontier = incomingEdges.every((edge) => this.isEdgeSatisfied(currentState, nodeId, edge.collectKey));
204
204
  if (isFrontier) {
205
205
  frontierNodeIds.push(nodeId);
206
206
  }
@@ -235,7 +235,7 @@ export class CurrentStateFrontierPlanner {
235
235
  requiredNodeIds.add(nodeId);
236
236
  for (const edge of this.topology.incomingByNode.get(nodeId) ?? []) {
237
237
  if (
238
- !this.isEdgeSatisfied(currentState, nodeId, edge.input) ||
238
+ !this.isEdgeSatisfied(currentState, nodeId, edge.collectKey) ||
239
239
  this.isNodeSatisfiedByOutputsOnly(currentState, edge.from.nodeId)
240
240
  ) {
241
241
  this.collectRequiredNode(requiredNodeIds, currentState, edge.from.nodeId);
@@ -249,7 +249,7 @@ export class CurrentStateFrontierPlanner {
249
249
  return [];
250
250
  }
251
251
  const expectedInputs = this.topology.expectedInputsByNode.get(nodeId) ?? [];
252
- const usesCollect = expectedInputs.length !== 1 || expectedInputs[0] !== "in";
252
+ const usesCollect = this.usesCollect(nodeId);
253
253
  if (usesCollect) {
254
254
  const received: Record<InputPortKey, Items> = {};
255
255
  for (const input of expectedInputs) {
@@ -268,7 +268,7 @@ export class CurrentStateFrontierPlanner {
268
268
  ];
269
269
  }
270
270
  const input = expectedInputs[0] ?? "in";
271
- const incomingEdge = incomingEdges.find((edge) => edge.input === input);
271
+ const incomingEdge = incomingEdges.find((edge) => edge.collectKey === input);
272
272
  return [
273
273
  {
274
274
  nodeId,
@@ -298,26 +298,31 @@ export class CurrentStateFrontierPlanner {
298
298
  return this.hasOutputs(currentState, nodeId) && !this.hasCompletedSnapshot(currentState, nodeId);
299
299
  }
300
300
 
301
- private isEdgeSatisfied(currentState: RunCurrentState, nodeId: NodeId, input: InputPortKey): boolean {
302
- const incomingEdge = (this.topology.incomingByNode.get(nodeId) ?? []).find((edge) => edge.input === input);
301
+ private isEdgeSatisfied(currentState: RunCurrentState, nodeId: NodeId, collectKey: InputPortKey): boolean {
302
+ const incomingEdge = (this.topology.incomingByNode.get(nodeId) ?? []).find(
303
+ (edge) => edge.collectKey === collectKey,
304
+ );
303
305
  if (!incomingEdge) {
304
306
  return false;
305
307
  }
306
- if (!this.hasOutputPort(currentState, incomingEdge.from.nodeId, incomingEdge.from.output)) {
308
+ const fromNodeId = incomingEdge.from.nodeId;
309
+ if (!this.isNodeSatisfied(currentState, fromNodeId)) {
307
310
  return false;
308
311
  }
309
312
  if (this.usesCollect(nodeId)) {
310
313
  return true;
311
314
  }
312
- const items = this.resolveOutputItems(currentState, incomingEdge.from.nodeId, incomingEdge.from.output);
315
+ const items = this.resolveOutputItems(currentState, fromNodeId, incomingEdge.from.output);
313
316
  if (items.length > 0) {
314
317
  return true;
315
318
  }
316
- return this.shouldContinueAfterEmptyOutputFromSource(incomingEdge.from.nodeId);
319
+ return this.shouldContinueAfterEmptyOutputFromSource(fromNodeId);
317
320
  }
318
321
 
319
- private resolveInput(currentState: RunCurrentState, nodeId: NodeId, input: InputPortKey): Items {
320
- const incomingEdge = (this.topology.incomingByNode.get(nodeId) ?? []).find((edge) => edge.input === input);
322
+ private resolveInput(currentState: RunCurrentState, nodeId: NodeId, collectKey: InputPortKey): Items {
323
+ const incomingEdge = (this.topology.incomingByNode.get(nodeId) ?? []).find(
324
+ (edge) => edge.collectKey === collectKey,
325
+ );
321
326
  if (!incomingEdge) {
322
327
  return [];
323
328
  }
@@ -333,21 +338,16 @@ export class CurrentStateFrontierPlanner {
333
338
  return snapshot?.status === "completed" || snapshot?.status === "skipped";
334
339
  }
335
340
 
336
- private hasOutputPort(currentState: RunCurrentState, nodeId: NodeId, output: OutputPortKey): boolean {
337
- const outputs = currentState.outputsByNode[nodeId];
338
- if (!outputs) {
339
- return false;
340
- }
341
- return Object.prototype.hasOwnProperty.call(outputs, output);
342
- }
343
-
344
341
  private resolveOutputItems(currentState: RunCurrentState, nodeId: NodeId, output: OutputPortKey): Items {
345
342
  return currentState.outputsByNode[nodeId]?.[output] ?? [];
346
343
  }
347
344
 
348
345
  private usesCollect(nodeId: NodeId): boolean {
349
346
  const expectedInputs = this.topology.expectedInputsByNode.get(nodeId) ?? [];
350
- return expectedInputs.length !== 1 || expectedInputs[0] !== "in";
347
+ if (expectedInputs.length !== 1 || expectedInputs[0] !== "in") {
348
+ return true;
349
+ }
350
+ return (this.topology.incomingByNode.get(nodeId) ?? []).length > 1;
351
351
  }
352
352
 
353
353
  private shouldContinueAfterEmptyOutputFromSource(nodeId: NodeId): boolean {