|
| 1 | +import { loggerMock } from '@sim/testing' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | +import { DEFAULTS } from '@/executor/constants' |
| 4 | +import type { LoopScope } from '@/executor/execution/state' |
| 5 | +import type { ExecutionContext, NormalizedBlockOutput } from '@/executor/types' |
| 6 | + |
| 7 | +vi.mock('@sim/logger', () => loggerMock) |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests for memory bounds in loop execution (issue #2525). |
| 11 | + * |
| 12 | + * When loops run with many iterations (especially with agent blocks making |
| 13 | + * tool calls), allIterationOutputs and blockLogs can grow unbounded, |
| 14 | + * causing OOM on systems with limited memory. |
| 15 | + */ |
| 16 | + |
| 17 | +function createMinimalContext(overrides: Partial<ExecutionContext> = {}): ExecutionContext { |
| 18 | + return { |
| 19 | + workflowId: 'test-workflow', |
| 20 | + blockStates: new Map(), |
| 21 | + executedBlocks: new Set(), |
| 22 | + blockLogs: [], |
| 23 | + metadata: { duration: 0 }, |
| 24 | + environmentVariables: {}, |
| 25 | + decisions: { router: new Map(), condition: new Map() }, |
| 26 | + completedLoops: new Set(), |
| 27 | + activeExecutionPath: new Set(), |
| 28 | + ...overrides, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +describe('Loop memory bounds', () => { |
| 33 | + describe('allIterationOutputs sliding window', () => { |
| 34 | + it('should keep at most MAX_LOOP_ITERATION_HISTORY entries', () => { |
| 35 | + const scope: LoopScope = { |
| 36 | + iteration: 0, |
| 37 | + currentIterationOutputs: new Map(), |
| 38 | + allIterationOutputs: [], |
| 39 | + } |
| 40 | + |
| 41 | + const limit = DEFAULTS.MAX_LOOP_ITERATION_HISTORY |
| 42 | + |
| 43 | + // Simulate more iterations than the limit |
| 44 | + for (let i = 0; i < limit + 50; i++) { |
| 45 | + const output: NormalizedBlockOutput = { content: `iteration-${i}` } |
| 46 | + const iterationResults = [output] |
| 47 | + scope.allIterationOutputs.push(iterationResults) |
| 48 | + |
| 49 | + // Apply the same sliding window logic as loop.ts |
| 50 | + if (scope.allIterationOutputs.length > limit) { |
| 51 | + const excess = scope.allIterationOutputs.length - limit |
| 52 | + scope.allIterationOutputs.splice(0, excess) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + expect(scope.allIterationOutputs.length).toBe(limit) |
| 57 | + // The oldest retained entry should be from iteration 50 |
| 58 | + expect(scope.allIterationOutputs[0][0].content).toBe('iteration-50') |
| 59 | + // The newest entry should be the last one pushed |
| 60 | + expect(scope.allIterationOutputs[limit - 1][0].content).toBe( |
| 61 | + `iteration-${limit + 49}` |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should not prune when under the limit', () => { |
| 66 | + const scope: LoopScope = { |
| 67 | + iteration: 0, |
| 68 | + currentIterationOutputs: new Map(), |
| 69 | + allIterationOutputs: [], |
| 70 | + } |
| 71 | + |
| 72 | + for (let i = 0; i < 10; i++) { |
| 73 | + scope.allIterationOutputs.push([{ content: `iter-${i}` }]) |
| 74 | + } |
| 75 | + |
| 76 | + expect(scope.allIterationOutputs.length).toBe(10) |
| 77 | + expect(scope.allIterationOutputs[0][0].content).toBe('iter-0') |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + describe('blockLogs pruning', () => { |
| 82 | + it('should keep at most MAX_BLOCK_LOGS entries', () => { |
| 83 | + const ctx = createMinimalContext() |
| 84 | + const limit = DEFAULTS.MAX_BLOCK_LOGS |
| 85 | + |
| 86 | + // Simulate pushing more logs than the limit |
| 87 | + for (let i = 0; i < limit + 100; i++) { |
| 88 | + ctx.blockLogs.push({ |
| 89 | + blockId: `block-${i}`, |
| 90 | + blockType: 'function', |
| 91 | + startedAt: new Date().toISOString(), |
| 92 | + endedAt: new Date().toISOString(), |
| 93 | + durationMs: 1, |
| 94 | + success: true, |
| 95 | + executionOrder: i + 1, |
| 96 | + }) |
| 97 | + |
| 98 | + // Apply the same pruning logic as block-executor.ts |
| 99 | + if (ctx.blockLogs.length > limit) { |
| 100 | + const excess = ctx.blockLogs.length - limit |
| 101 | + ctx.blockLogs.splice(0, excess) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + expect(ctx.blockLogs.length).toBe(limit) |
| 106 | + // The oldest retained log should be from index 100 |
| 107 | + expect(ctx.blockLogs[0].blockId).toBe('block-100') |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + describe('DEFAULTS constants', () => { |
| 112 | + it('should define MAX_LOOP_ITERATION_HISTORY', () => { |
| 113 | + expect(DEFAULTS.MAX_LOOP_ITERATION_HISTORY).toBeGreaterThan(0) |
| 114 | + expect(typeof DEFAULTS.MAX_LOOP_ITERATION_HISTORY).toBe('number') |
| 115 | + }) |
| 116 | + |
| 117 | + it('should define MAX_BLOCK_LOGS', () => { |
| 118 | + expect(DEFAULTS.MAX_BLOCK_LOGS).toBeGreaterThan(0) |
| 119 | + expect(typeof DEFAULTS.MAX_BLOCK_LOGS).toBe('number') |
| 120 | + }) |
| 121 | + }) |
| 122 | +}) |
0 commit comments