diff --git a/packages/core/src/execution-engine/partial-execution-utils/__tests__/rewire-graph.test.ts b/packages/core/src/execution-engine/partial-execution-utils/__tests__/rewire-graph.test.ts index 839650f6c0..77a939b212 100644 --- a/packages/core/src/execution-engine/partial-execution-utils/__tests__/rewire-graph.test.ts +++ b/packages/core/src/execution-engine/partial-execution-utils/__tests__/rewire-graph.test.ts @@ -93,14 +93,15 @@ describe('rewireGraph()', () => { expect(tool.rewireOutputLogTo).toBe(NodeConnectionTypes.AiTool); }); - it('fails when the tool has no incoming connections', () => { + it('should not rewire when the tool has no root', () => { const tool = createNodeData({ name: 'tool', type: 'n8n-nodes-base.ai-tool' }); const root = createNodeData({ name: 'root' }); const graph = new DirectedGraph(); graph.addNodes(root, tool); + const result = rewireGraph(tool, graph); - expect(() => rewireGraph(tool, graph)).toThrow(); + expect(result).toStrictEqual(graph); }); it('removes the root node from the graph', () => { diff --git a/packages/core/src/execution-engine/partial-execution-utils/rewire-graph.ts b/packages/core/src/execution-engine/partial-execution-utils/rewire-graph.ts index 51decae9a0..bda9d3830c 100644 --- a/packages/core/src/execution-engine/partial-execution-utils/rewire-graph.ts +++ b/packages/core/src/execution-engine/partial-execution-utils/rewire-graph.ts @@ -4,26 +4,28 @@ import { type INode, NodeConnectionTypes } from 'n8n-workflow'; import { type DirectedGraph } from './directed-graph'; export function rewireGraph(tool: INode, graph: DirectedGraph): DirectedGraph { - graph = graph.clone(); - const children = graph.getChildren(tool); + const modifiedGraph = graph.clone(); + const children = modifiedGraph.getChildren(tool); - a.ok(children.size > 0, 'Tool must be connected to a root node'); + if (children.size === 0) { + return graph; + } const rootNode = [...children][0]; a.ok(rootNode); - const allIncomingConnection = graph + const allIncomingConnection = modifiedGraph .getDirectParentConnections(rootNode) .filter((cn) => cn.type === NodeConnectionTypes.Main); tool.rewireOutputLogTo = NodeConnectionTypes.AiTool; for (const cn of allIncomingConnection) { - graph.addConnection({ from: cn.from, to: tool }); + modifiedGraph.addConnection({ from: cn.from, to: tool }); } - graph.removeNode(rootNode); + modifiedGraph.removeNode(rootNode); - return graph; + return modifiedGraph; }