-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(executor): harden JSON parsing and error visibility in block handlers #4788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rpatricksmith
wants to merge
2
commits into
simstudioai:main
Choose a base branch
from
rpatricksmith:fix/executor-json-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
apps/sim/executor/handlers/human-in-the-loop/human-in-the-loop-handler.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import '@sim/testing/mocks/executor' | ||
|
|
||
| import { urlsMock, urlsMockFns } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest' | ||
| import { BlockType } from '@/executor/constants' | ||
| import { HumanInTheLoopBlockHandler } from '@/executor/handlers/human-in-the-loop/human-in-the-loop-handler' | ||
| import type { ExecutionContext } from '@/executor/types' | ||
| import type { SerializedBlock } from '@/serializer/types' | ||
| import { executeTool } from '@/tools' | ||
|
|
||
| vi.mock('@/lib/core/utils/urls', () => urlsMock) | ||
|
|
||
| const { mockGeneratePauseContextId, mockMapNodeMetadataToPauseScopes } = vi.hoisted(() => ({ | ||
| mockGeneratePauseContextId: vi.fn(() => 'test-pause-context-id'), | ||
| mockMapNodeMetadataToPauseScopes: vi.fn(() => ({ | ||
| parallelScope: undefined, | ||
| loopScope: undefined, | ||
| })), | ||
| })) | ||
|
|
||
| vi.mock('@/executor/human-in-the-loop/utils', () => ({ | ||
| generatePauseContextId: mockGeneratePauseContextId, | ||
| mapNodeMetadataToPauseScopes: mockMapNodeMetadataToPauseScopes, | ||
| })) | ||
|
|
||
| vi.mock('@/executor/utils/builder-data', () => ({ | ||
| convertBuilderDataToJson: vi.fn(() => ({ key: 'value' })), | ||
| convertPropertyValue: vi.fn((prop: any) => prop.value), | ||
| })) | ||
|
|
||
| vi.mock('@/executor/utils/block-data', () => ({ | ||
| collectBlockData: vi.fn(() => ({ | ||
| blockData: {}, | ||
| blockNameMapping: {}, | ||
| })), | ||
| })) | ||
|
|
||
| const mockExecuteTool = executeTool as Mock | ||
|
|
||
| describe('HumanInTheLoopBlockHandler', () => { | ||
| let handler: HumanInTheLoopBlockHandler | ||
| let mockBlock: SerializedBlock | ||
| let mockContext: ExecutionContext | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
|
|
||
| handler = new HumanInTheLoopBlockHandler() | ||
|
|
||
| mockBlock = { | ||
| id: 'hitl-block-1', | ||
| metadata: { id: BlockType.HUMAN_IN_THE_LOOP, name: 'Test HITL Block' }, | ||
| position: { x: 0, y: 0 }, | ||
| config: { tool: BlockType.HUMAN_IN_THE_LOOP, params: {} }, | ||
| inputs: {}, | ||
| outputs: {}, | ||
| enabled: true, | ||
| } | ||
|
|
||
| mockContext = { | ||
| workflowId: 'test-workflow-id', | ||
| blockStates: new Map(), | ||
| blockLogs: [], | ||
| metadata: { duration: 0 }, | ||
| environmentVariables: {}, | ||
| decisions: { router: new Map(), condition: new Map() }, | ||
| loopExecutions: new Map(), | ||
| executedBlocks: new Set(), | ||
| activeExecutionPath: new Set(), | ||
| completedLoops: new Set(), | ||
| } | ||
|
|
||
| urlsMockFns.mockGetBaseUrl.mockReturnValue('http://localhost:3000') | ||
| mockExecuteTool.mockResolvedValue({ success: true, output: {} }) | ||
| mockGeneratePauseContextId.mockReturnValue('test-pause-context-id') | ||
| mockMapNodeMetadataToPauseScopes.mockReturnValue({ | ||
| parallelScope: undefined, | ||
| loopScope: undefined, | ||
| }) | ||
| }) | ||
|
|
||
| it('should return true for human-in-the-loop blocks', () => { | ||
| expect(handler.canHandle(mockBlock)).toBe(true) | ||
| }) | ||
|
|
||
| it('should return false for non-hitl blocks', () => { | ||
| const nonHitlBlock: SerializedBlock = { | ||
| ...mockBlock, | ||
| metadata: { id: 'other-block' }, | ||
| } | ||
| expect(handler.canHandle(nonHitlBlock)).toBe(false) | ||
| }) | ||
|
|
||
| it('should execute with human operation and return correct response shape', async () => { | ||
| const inputs = { | ||
| operation: 'human', | ||
| inputFormat: [{ id: 'field-1', name: 'username', label: 'Username', type: 'string' }], | ||
| builderData: [{ id: '1', name: 'result', type: 'string', value: 'test' }], | ||
| } | ||
|
|
||
| const result = await handler.execute(mockContext, mockBlock, inputs) | ||
|
|
||
| expect(result.response).toBeDefined() | ||
| expect(result.response.status).toBe(200) | ||
| expect(result.response.headers).toHaveProperty('Content-Type') | ||
| expect(result.response.data).toHaveProperty('operation', 'human') | ||
| expect(result.response.data).toHaveProperty('responseStructure') | ||
| expect(result.response.data).toHaveProperty('inputFormat') | ||
| expect(result.response.data).toHaveProperty('submission', null) | ||
| expect(result._pauseMetadata).toBeDefined() | ||
| expect(result._pauseMetadata.pauseKind).toBe('human') | ||
| }) | ||
|
|
||
| it('should handle malformed JSON data in api operation mode', async () => { | ||
| const inputs = { | ||
| operation: 'api', | ||
| dataMode: 'json', | ||
| data: '{invalid json}', | ||
| } | ||
|
|
||
| const result = await handler.execute(mockContext, mockBlock, inputs) | ||
|
|
||
| expect(result).toBeDefined() | ||
| expect(result.response).toBeDefined() | ||
| expect(result.response.data).toBe('{invalid json}') | ||
| }) | ||
|
|
||
| it('should handle valid JSON data in api operation mode', async () => { | ||
| const inputs = { | ||
| operation: 'api', | ||
| dataMode: 'json', | ||
| data: '{"message":"hello"}', | ||
| } | ||
|
|
||
| const result = await handler.execute(mockContext, mockBlock, inputs) | ||
|
|
||
| expect(result.response.data).toMatchObject({ message: 'hello' }) | ||
| }) | ||
|
|
||
| it('should return error response on execution failure', async () => { | ||
| const inputs = { | ||
| operation: 'human', | ||
| inputFormat: 'not-an-array', | ||
| builderData: 'not-an-array', | ||
| } | ||
|
|
||
| mockMapNodeMetadataToPauseScopes.mockImplementation(() => { | ||
| throw new Error('Metadata mapping failed') | ||
| }) | ||
|
|
||
| const result = await handler.execute(mockContext, mockBlock, inputs) | ||
|
|
||
| expect(result.response).toBeDefined() | ||
| expect(result.response.status).toBe(500) | ||
| expect(result.response.data).toHaveProperty('error') | ||
| expect(result.response.data.message).toBe('Metadata mapping failed') | ||
| }) | ||
|
|
||
| it('should include resume links when executionId and workflowId exist', async () => { | ||
| const contextWithExecution: ExecutionContext = { | ||
| ...mockContext, | ||
| executionId: 'exec-123', | ||
| } | ||
|
|
||
| const inputs = { | ||
| operation: 'human', | ||
| inputFormat: [], | ||
| } | ||
|
|
||
| const result = await handler.execute(contextWithExecution, mockBlock, inputs) | ||
|
|
||
| expect(result.response.data._resume).toBeDefined() | ||
| expect(result.url).toBeDefined() | ||
| expect(result.resumeEndpoint).toBeDefined() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger.warn(\Failed to parse ${inputType} field "${key}":`, { error })on parse failure. Replacing it withparseJSON(value, value)— which silently returns the fallback — drops that diagnostic. The same pattern applies inresponse-handler.tsandhuman-in-the-loop-handler.ts, both of which also hadlogger.warn('Failed to parse JSON data, returning as string:', error)` before. With these warnings gone, malformed-JSON inputs in production are now invisible in logs until a downstream symptom appears.