|
| 1 | +import type { SessionPrincipal } from '@sim/auth/principal' |
| 2 | +import { |
| 3 | + createSerializedBlock, |
| 4 | + createSerializedWorkflow, |
| 5 | +} from '@sim/testing/factories/serialized-block.factory' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { BlockType, DEFAULTS } from '@/executor/constants' |
| 8 | +import { DAGExecutor } from '@/executor/execution/executor' |
| 9 | +import type { ExecutionResult } from '@/executor/types' |
| 10 | +import { hasExecutionResult } from '@/executor/utils/errors' |
| 11 | +import type { SerializedLoop, SerializedWorkflow } from '@/serializer/types' |
| 12 | + |
| 13 | +const { bodyRuns, conditionResult, runaway } = vi.hoisted(() => ({ |
| 14 | + bodyRuns: { count: 0 }, |
| 15 | + conditionResult: { stopAfter: Number.POSITIVE_INFINITY }, |
| 16 | + /** Cancels a loop the ceiling failed to stop, so a regression fails instead of hanging. */ |
| 17 | + runaway: { controller: new AbortController(), limit: 30_000 }, |
| 18 | +})) |
| 19 | + |
| 20 | +/** Count conditions resolve to `return Boolean(<index> < <count>)`; user conditions stay opaque. */ |
| 21 | +vi.mock('@/lib/execution/isolated-vm', () => ({ |
| 22 | + executeInIsolatedVM: vi.fn(async ({ code }: { code: string }) => { |
| 23 | + const indexCheck = /^return Boolean\((\d+) < (\d+)\)$/.exec(code) |
| 24 | + if (indexCheck) return { result: Number(indexCheck[1]) < Number(indexCheck[2]) } |
| 25 | + return { result: bodyRuns.count < conditionResult.stopAfter } |
| 26 | + }), |
| 27 | +})) |
| 28 | + |
| 29 | +vi.mock('@/executor/handlers/registry', () => ({ |
| 30 | + createBlockHandlers: () => [ |
| 31 | + { |
| 32 | + canHandle: () => true, |
| 33 | + execute: async (_ctx: unknown, block: { id: string }) => { |
| 34 | + if (block.id === 'tick') bodyRuns.count++ |
| 35 | + if (bodyRuns.count >= runaway.limit) runaway.controller.abort() |
| 36 | + return { ok: true } |
| 37 | + }, |
| 38 | + }, |
| 39 | + ], |
| 40 | +})) |
| 41 | + |
| 42 | +const PRINCIPAL: SessionPrincipal = { kind: 'session', userId: 'user-1', sessionId: 'session-1' } |
| 43 | +const CEILING = DEFAULTS.MAX_CONDITION_LOOP_ITERATIONS |
| 44 | + |
| 45 | +function loopWorkflow(loop: Partial<Omit<SerializedLoop, 'id' | 'nodes'>>): SerializedWorkflow { |
| 46 | + const blocks = [ |
| 47 | + createSerializedBlock({ id: 'start', name: 'start', type: BlockType.STARTER }), |
| 48 | + createSerializedBlock({ id: 'loop', name: 'loop', type: BlockType.LOOP }), |
| 49 | + createSerializedBlock({ id: 'tick', name: 'tick', type: BlockType.FUNCTION }), |
| 50 | + ] |
| 51 | + return { |
| 52 | + ...createSerializedWorkflow(blocks, [ |
| 53 | + { source: 'start', target: 'loop' }, |
| 54 | + { source: 'loop', target: 'tick', sourceHandle: 'loop-start-source' }, |
| 55 | + ]), |
| 56 | + loops: { loop: { id: 'loop', nodes: ['tick'], iterations: 0, ...loop } }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** Runs the workflow and returns its result, including the one a failed run attaches to its throw. */ |
| 61 | +async function run(workflow: SerializedWorkflow): Promise<ExecutionResult> { |
| 62 | + try { |
| 63 | + return await new DAGExecutor({ |
| 64 | + workflow, |
| 65 | + contextExtensions: { |
| 66 | + workspaceId: 'ws', |
| 67 | + executionId: 'exec-1', |
| 68 | + principal: PRINCIPAL, |
| 69 | + abortSignal: runaway.controller.signal, |
| 70 | + }, |
| 71 | + }).execute('wf') |
| 72 | + } catch (error) { |
| 73 | + if (hasExecutionResult(error)) return error.executionResult |
| 74 | + throw error |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +describe('condition-driven loop iteration ceiling', () => { |
| 79 | + beforeEach(() => { |
| 80 | + bodyRuns.count = 0 |
| 81 | + conditionResult.stopAfter = Number.POSITIVE_INFINITY |
| 82 | + runaway.controller = new AbortController() |
| 83 | + }) |
| 84 | + |
| 85 | + it.each([ |
| 86 | + ['While', { loopType: 'while', whileCondition: '<variable.i> < 3' }], |
| 87 | + ['Do While', { loopType: 'doWhile', doWhileCondition: '<variable.i> < 3', iterations: 5 }], |
| 88 | + ] as const)( |
| 89 | + 'fails a %s loop whose condition never turns false at the ceiling', |
| 90 | + async (label, loop) => { |
| 91 | + const result = await run(loopWorkflow(loop)) |
| 92 | + |
| 93 | + expect(bodyRuns.count).toBe(CEILING) |
| 94 | + expect(result.success).toBe(false) |
| 95 | + expect(result.error).toContain(`${label} loop stopped after 10,000 iterations`) |
| 96 | + expect(result.logs?.find((log) => log.blockId === 'loop')).toMatchObject({ |
| 97 | + success: false, |
| 98 | + error: expect.stringContaining('condition still true'), |
| 99 | + }) |
| 100 | + }, |
| 101 | + 60_000 |
| 102 | + ) |
| 103 | + |
| 104 | + it('ends a While loop normally when its condition turns false on the last allowed iteration', async () => { |
| 105 | + conditionResult.stopAfter = CEILING |
| 106 | + |
| 107 | + const result = await run( |
| 108 | + loopWorkflow({ loopType: 'while', whileCondition: '<variable.i> < 3' }) |
| 109 | + ) |
| 110 | + |
| 111 | + expect(bodyRuns.count).toBe(CEILING) |
| 112 | + expect(result.success).toBe(true) |
| 113 | + }, 60_000) |
| 114 | + |
| 115 | + it.each([ |
| 116 | + ['For', { loopType: 'for', iterations: CEILING + 5 }], |
| 117 | + ['count-driven Do While', { loopType: 'doWhile', iterations: CEILING + 5 }], |
| 118 | + ] as const)( |
| 119 | + 'leaves a %s loop free to run its configured count past the ceiling', |
| 120 | + async (_label, loop) => { |
| 121 | + const result = await run(loopWorkflow(loop)) |
| 122 | + |
| 123 | + expect(result.success).toBe(true) |
| 124 | + expect(bodyRuns.count).toBe(CEILING + 5) |
| 125 | + }, |
| 126 | + 60_000 |
| 127 | + ) |
| 128 | +}) |
0 commit comments