|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { |
| 5 | + QueueStore, |
| 6 | + type QueuedTask, |
| 7 | + type TaskHandoff, |
| 8 | +} from '@lib/programs/orchestrator/queue'; |
| 9 | +import { drainQueue, type RunTask } from '@lib/programs/orchestrator/executor'; |
| 10 | + |
| 11 | +jest.mock('@utils/analytics', () => ({ |
| 12 | + analytics: { captureException: jest.fn(), wizardCapture: jest.fn() }, |
| 13 | +})); |
| 14 | +import { analytics } from '@utils/analytics'; |
| 15 | + |
| 16 | +const HANDOFF: TaskHandoff = { goals: 'g', did: 'd', forNextAgent: 'n' }; |
| 17 | + |
| 18 | +function tmpDir(): string { |
| 19 | + return fs.mkdtempSync(path.join(os.tmpdir(), 'executor-test-')); |
| 20 | +} |
| 21 | + |
| 22 | +describe('drainQueue', () => { |
| 23 | + let dir: string; |
| 24 | + let q: QueueStore; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + dir = tmpDir(); |
| 28 | + q = new QueueStore(dir, 'run-1'); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => fs.rmSync(dir, { recursive: true, force: true })); |
| 32 | + |
| 33 | + const completing: RunTask = (task) => { |
| 34 | + q.complete(task.id, HANDOFF); |
| 35 | + return Promise.resolve(); |
| 36 | + }; |
| 37 | + |
| 38 | + it('runs a single task to done and drains', async () => { |
| 39 | + const a = q.enqueue({ type: 'install' }); |
| 40 | + await drainQueue(q, completing, { maxStarts: 50 }); |
| 41 | + expect(q.get(a.id)?.status).toBe('done'); |
| 42 | + expect(q.isDrained()).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('runs a dependent task only after its dependency completes', async () => { |
| 46 | + const order: string[] = []; |
| 47 | + const a = q.enqueue({ type: 'install' }); |
| 48 | + const b = q.enqueue({ type: 'init', dependsOn: [a.id] }); |
| 49 | + const runner: RunTask = (task) => { |
| 50 | + order.push(task.type); |
| 51 | + q.complete(task.id, HANDOFF); |
| 52 | + return Promise.resolve(); |
| 53 | + }; |
| 54 | + await drainQueue(q, runner, { maxStarts: 50 }); |
| 55 | + expect(order).toEqual(['install', 'init']); |
| 56 | + expect(q.get(b.id)?.status).toBe('done'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('runs independent branches concurrently; the graph is the only schedule', async () => { |
| 60 | + let active = 0; |
| 61 | + let maxActive = 0; |
| 62 | + const runner: RunTask = async (task) => { |
| 63 | + active += 1; |
| 64 | + maxActive = Math.max(maxActive, active); |
| 65 | + await new Promise((r) => setTimeout(r, 5)); |
| 66 | + q.complete(task.id, HANDOFF); |
| 67 | + active -= 1; |
| 68 | + }; |
| 69 | + const a = q.enqueue({ type: 'install' }); |
| 70 | + const b = q.enqueue({ type: 'init' }); |
| 71 | + q.enqueue({ type: 'capture', dependsOn: [a.id, b.id] }); |
| 72 | + await drainQueue(q, runner, { maxStarts: 50 }); |
| 73 | + // install and init overlap; capture waits for both. |
| 74 | + expect(maxActive).toBe(2); |
| 75 | + expect(q.summary().done).toBe(3); |
| 76 | + }); |
| 77 | + |
| 78 | + it('starts a dependent the moment its dependency finishes, not in waves', async () => { |
| 79 | + const startedAt: Record<string, number> = {}; |
| 80 | + let clock = 0; |
| 81 | + const runner: RunTask = async (task) => { |
| 82 | + startedAt[task.type] = clock++; |
| 83 | + // slow holds the wave open; fast finishes early and unblocks after-fast. |
| 84 | + const delay = task.type === 'slow' ? 30 : 5; |
| 85 | + await new Promise((r) => setTimeout(r, delay)); |
| 86 | + q.complete(task.id, HANDOFF); |
| 87 | + }; |
| 88 | + q.enqueue({ type: 'slow' }); |
| 89 | + const fast = q.enqueue({ type: 'fast' }); |
| 90 | + q.enqueue({ type: 'after-fast', dependsOn: [fast.id] }); |
| 91 | + await drainQueue(q, runner, { maxStarts: 50 }); |
| 92 | + // after-fast started while slow was still running. |
| 93 | + expect(startedAt['after-fast']).toBeDefined(); |
| 94 | + expect(q.summary().done).toBe(3); |
| 95 | + }); |
| 96 | + |
| 97 | + it('retries a task that ends without reporting, then fails it', async () => { |
| 98 | + const a = q.enqueue({ type: 'install', maxAttempts: 2 }); |
| 99 | + const noReport: RunTask = async () => { |
| 100 | + /* agent never calls complete_task */ |
| 101 | + }; |
| 102 | + await drainQueue(q, noReport, { maxStarts: 50 }); |
| 103 | + expect(q.get(a.id)?.status).toBe('failed'); |
| 104 | + expect(q.get(a.id)?.attempts).toBe(2); |
| 105 | + }); |
| 106 | + |
| 107 | + it('succeeds on a retry within the attempt budget', async () => { |
| 108 | + let calls = 0; |
| 109 | + const a = q.enqueue({ type: 'install', maxAttempts: 3 }); |
| 110 | + const flaky: RunTask = (task: QueuedTask) => { |
| 111 | + calls += 1; |
| 112 | + if (calls >= 2) q.complete(task.id, HANDOFF); |
| 113 | + return Promise.resolve(); |
| 114 | + }; |
| 115 | + await drainQueue(q, flaky, { maxStarts: 50 }); |
| 116 | + expect(q.get(a.id)?.status).toBe('done'); |
| 117 | + expect(calls).toBe(2); |
| 118 | + }); |
| 119 | + |
| 120 | + it('captures and fails a task whose runner throws', async () => { |
| 121 | + const a = q.enqueue({ type: 'install', maxAttempts: 1 }); |
| 122 | + const throwing: RunTask = () => Promise.reject(new Error('agent exploded')); |
| 123 | + await drainQueue(q, throwing, { maxStarts: 50 }); |
| 124 | + expect(q.get(a.id)?.status).toBe('failed'); |
| 125 | + expect(analytics.captureException).toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('does not run a task whose dependency failed', async () => { |
| 129 | + const a = q.enqueue({ type: 'install', maxAttempts: 1 }); |
| 130 | + const b = q.enqueue({ type: 'init', dependsOn: [a.id] }); |
| 131 | + const runner: RunTask = (task) => { |
| 132 | + if (task.type === 'init') q.complete(task.id, HANDOFF); |
| 133 | + // install never reports, so it fails after its single attempt. |
| 134 | + return Promise.resolve(); |
| 135 | + }; |
| 136 | + await drainQueue(q, runner, { maxStarts: 50 }); |
| 137 | + expect(q.get(a.id)?.status).toBe('failed'); |
| 138 | + expect(q.get(b.id)?.status).toBe('pending'); |
| 139 | + expect(q.isDrained()).toBe(true); |
| 140 | + }); |
| 141 | + |
| 142 | + it('terminates via the start backstop instead of looping forever', async () => { |
| 143 | + const a = q.enqueue({ type: 'install', maxAttempts: 999 }); |
| 144 | + const neverReports: RunTask = async () => { |
| 145 | + /* would retry forever without the backstop */ |
| 146 | + }; |
| 147 | + await drainQueue(q, neverReports, { maxStarts: 3 }); |
| 148 | + expect(q.get(a.id)?.attempts).toBeLessThanOrEqual(3); |
| 149 | + }); |
| 150 | +}); |
0 commit comments