Skip to content

Commit 78b7643

Browse files
fix(condition): async execution isolated vm error (#2446)
* fix(condition): async execution isolated vm error * fix tests
1 parent 7ef1150 commit 78b7643

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

apps/sim/executor/handlers/condition/condition-handler.test.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,32 @@ vi.mock('@/lib/core/utils/request', () => ({
1717
generateRequestId: vi.fn(() => 'test-request-id'),
1818
}))
1919

20-
vi.mock('@/lib/execution/isolated-vm', () => ({
21-
executeInIsolatedVM: vi.fn(),
20+
vi.mock('@/tools', () => ({
21+
executeTool: vi.fn(),
2222
}))
2323

24-
import { executeInIsolatedVM } from '@/lib/execution/isolated-vm'
24+
import { executeTool } from '@/tools'
2525

26-
const mockExecuteInIsolatedVM = executeInIsolatedVM as ReturnType<typeof vi.fn>
26+
const mockExecuteTool = executeTool as ReturnType<typeof vi.fn>
2727

28-
function simulateIsolatedVMExecution(
29-
code: string,
30-
contextVariables: Record<string, unknown>
31-
): { result: unknown; stdout: string; error?: { message: string; name: string } } {
28+
/**
29+
* Simulates what the function_execute tool does when evaluating condition code
30+
*/
31+
function simulateConditionExecution(code: string): {
32+
success: boolean
33+
output?: { result: unknown }
34+
error?: string
35+
} {
3236
try {
33-
const fn = new Function(...Object.keys(contextVariables), code)
34-
const result = fn(...Object.values(contextVariables))
35-
return { result, stdout: '' }
37+
// The code is in format: "const context = {...};\nreturn Boolean(...)"
38+
// We need to execute it and return the result
39+
const fn = new Function(code)
40+
const result = fn()
41+
return { success: true, output: { result } }
3642
} catch (error: any) {
3743
return {
38-
result: null,
39-
stdout: '',
40-
error: { message: error.message, name: error.name || 'Error' },
44+
success: false,
45+
error: error.message,
4146
}
4247
}
4348
}
@@ -143,8 +148,8 @@ describe('ConditionBlockHandler', () => {
143148

144149
vi.clearAllMocks()
145150

146-
mockExecuteInIsolatedVM.mockImplementation(async ({ code, contextVariables }) => {
147-
return simulateIsolatedVMExecution(code, contextVariables)
151+
mockExecuteTool.mockImplementation(async (_toolId: string, params: { code: string }) => {
152+
return simulateConditionExecution(params.code)
148153
})
149154
})
150155

apps/sim/executor/handlers/condition/condition-handler.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { generateRequestId } from '@/lib/core/utils/request'
2-
import { executeInIsolatedVM } from '@/lib/execution/isolated-vm'
31
import { createLogger } from '@/lib/logs/console/logger'
42
import type { BlockOutput } from '@/blocks/types'
53
import { BlockType, CONDITION, DEFAULTS, EDGE } from '@/executor/constants'
64
import type { BlockHandler, ExecutionContext } from '@/executor/types'
75
import type { SerializedBlock } from '@/serializer/types'
6+
import { executeTool } from '@/tools'
87

98
const logger = createLogger('ConditionBlockHandler')
109

@@ -39,32 +38,38 @@ export async function evaluateConditionExpression(
3938
}
4039

4140
try {
42-
const requestId = generateRequestId()
43-
44-
const code = `return Boolean(${resolvedConditionValue})`
45-
46-
const result = await executeInIsolatedVM({
47-
code,
48-
params: {},
49-
envVars: {},
50-
contextVariables: { context: evalContext },
51-
timeoutMs: CONDITION_TIMEOUT_MS,
52-
requestId,
53-
})
41+
const contextSetup = `const context = ${JSON.stringify(evalContext)};`
42+
const code = `${contextSetup}\nreturn Boolean(${resolvedConditionValue})`
43+
44+
const result = await executeTool(
45+
'function_execute',
46+
{
47+
code,
48+
timeout: CONDITION_TIMEOUT_MS,
49+
envVars: {},
50+
_context: {
51+
workflowId: ctx.workflowId,
52+
workspaceId: ctx.workspaceId,
53+
},
54+
},
55+
false,
56+
false,
57+
ctx
58+
)
5459

55-
if (result.error) {
56-
logger.error(`Failed to evaluate condition: ${result.error.message}`, {
60+
if (!result.success) {
61+
logger.error(`Failed to evaluate condition: ${result.error}`, {
5762
originalCondition: conditionExpression,
5863
resolvedCondition: resolvedConditionValue,
5964
evalContext,
6065
error: result.error,
6166
})
6267
throw new Error(
63-
`Evaluation error in condition: ${result.error.message}. (Resolved: ${resolvedConditionValue})`
68+
`Evaluation error in condition: ${result.error}. (Resolved: ${resolvedConditionValue})`
6469
)
6570
}
6671

67-
return Boolean(result.result)
72+
return Boolean(result.output?.result)
6873
} catch (evalError: any) {
6974
logger.error(`Failed to evaluate condition: ${evalError.message}`, {
7075
originalCondition: conditionExpression,

0 commit comments

Comments
 (0)