fix: Increment runIndex in WorkflowToolV2 tool executions to avoid reusing out of date inputs (#13008)

This commit is contained in:
Charlie Kolb 2025-02-04 13:02:50 +01:00 committed by GitHub
parent fdcff9082b
commit cc907fbca9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 3 deletions

View file

@ -11,9 +11,14 @@ import type {
import { WorkflowToolService } from './utils/WorkflowToolService';
type ISupplyDataFunctionsWithRunIndex = ISupplyDataFunctions & { runIndex: number };
// Mock ISupplyDataFunctions interface
function createMockContext(overrides?: Partial<ISupplyDataFunctions>): ISupplyDataFunctions {
function createMockContext(
overrides?: Partial<ISupplyDataFunctions>,
): ISupplyDataFunctionsWithRunIndex {
return {
runIndex: 0,
getNodeParameter: jest.fn(),
getWorkflowDataProxy: jest.fn(),
getNode: jest.fn(),
@ -35,11 +40,11 @@ function createMockContext(overrides?: Partial<ISupplyDataFunctions>): ISupplyDa
warn: jest.fn(),
},
...overrides,
} as ISupplyDataFunctions;
} as ISupplyDataFunctionsWithRunIndex;
}
describe('WorkflowTool::WorkflowToolService', () => {
let context: ISupplyDataFunctions;
let context: ISupplyDataFunctionsWithRunIndex;
let service: WorkflowToolService;
beforeEach(() => {
@ -93,6 +98,7 @@ describe('WorkflowTool::WorkflowToolService', () => {
expect(result).toBe(JSON.stringify(TEST_RESPONSE, null, 2));
expect(context.addOutputData).toHaveBeenCalled();
expect(context.runIndex).toBe(1);
});
it('should handle errors during tool execution', async () => {

View file

@ -90,6 +90,9 @@ export class WorkflowToolService {
const errorResponse = `There was an error: "${executionError.message}"`;
void this.context.addOutputData(NodeConnectionType.AiTool, index, executionError);
return errorResponse;
} finally {
// @ts-expect-error this accesses a private member on the actual implementation to fix https://linear.app/n8n/issue/ADO-3186/bug-workflowtool-v2-always-uses-first-row-of-input-data
this.context.runIndex++;
}
};