|
| 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 | +const HANDOFF: TaskHandoff = { goals: 'g', did: 'd', forNextAgent: 'n' }; |
| 12 | + |
| 13 | +function tmpDir(): string { |
| 14 | + return fs.mkdtempSync(path.join(os.tmpdir(), 'executor-test-')); |
| 15 | +} |
| 16 | + |
| 17 | +describe('drainQueue', () => { |
| 18 | + let dir: string; |
| 19 | + let q: QueueStore; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + dir = tmpDir(); |
| 23 | + q = new QueueStore(dir, 'run-1'); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => fs.rmSync(dir, { recursive: true, force: true })); |
| 27 | + |
| 28 | + const completing: RunTask = async (task) => { |
| 29 | + await q.complete(task.id, HANDOFF); |
| 30 | + }; |
| 31 | + |
| 32 | + it('runs a single task to done and drains', async () => { |
| 33 | + const a = await q.enqueue({ type: 'install' }); |
| 34 | + await drainQueue(q, completing, { cap: 1, maxIterations: 50 }); |
| 35 | + expect(q.get(a.id)?.status).toBe('done'); |
| 36 | + expect(q.isDrained()).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it('runs a dependent task only after its dependency completes', async () => { |
| 40 | + const order: string[] = []; |
| 41 | + const a = await q.enqueue({ type: 'install' }); |
| 42 | + const b = await q.enqueue({ type: 'init', dependsOn: [a.id] }); |
| 43 | + const runner: RunTask = async (task) => { |
| 44 | + order.push(task.type); |
| 45 | + await q.complete(task.id, HANDOFF); |
| 46 | + }; |
| 47 | + await drainQueue(q, runner, { cap: 1, maxIterations: 50 }); |
| 48 | + expect(order).toEqual(['install', 'init']); |
| 49 | + expect(q.get(b.id)?.status).toBe('done'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('retries a task that ends without reporting, then fails it', async () => { |
| 53 | + const a = await q.enqueue({ type: 'install', maxAttempts: 2 }); |
| 54 | + const noReport: RunTask = async () => { |
| 55 | + /* agent never calls complete_task */ |
| 56 | + }; |
| 57 | + await drainQueue(q, noReport, { cap: 1, maxIterations: 50 }); |
| 58 | + expect(q.get(a.id)?.status).toBe('failed'); |
| 59 | + expect(q.get(a.id)?.attempts).toBe(2); |
| 60 | + }); |
| 61 | + |
| 62 | + it('succeeds on a retry within the attempt budget', async () => { |
| 63 | + let calls = 0; |
| 64 | + const a = await q.enqueue({ type: 'install', maxAttempts: 3 }); |
| 65 | + const flaky: RunTask = async (task: QueuedTask) => { |
| 66 | + calls += 1; |
| 67 | + if (calls >= 2) await q.complete(task.id, HANDOFF); |
| 68 | + }; |
| 69 | + await drainQueue(q, flaky, { cap: 1, maxIterations: 50 }); |
| 70 | + expect(q.get(a.id)?.status).toBe('done'); |
| 71 | + expect(calls).toBe(2); |
| 72 | + }); |
| 73 | + |
| 74 | + it('does not run a task whose dependency failed', async () => { |
| 75 | + const a = await q.enqueue({ type: 'install', maxAttempts: 1 }); |
| 76 | + const b = await q.enqueue({ type: 'init', dependsOn: [a.id] }); |
| 77 | + const runner: RunTask = async (task) => { |
| 78 | + if (task.type === 'init') await q.complete(task.id, HANDOFF); |
| 79 | + // install never reports, so it fails after its single attempt. |
| 80 | + }; |
| 81 | + await drainQueue(q, runner, { cap: 1, maxIterations: 50 }); |
| 82 | + expect(q.get(a.id)?.status).toBe('failed'); |
| 83 | + expect(q.get(b.id)?.status).toBe('pending'); |
| 84 | + expect(q.isDrained()).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it('respects the concurrency cap', async () => { |
| 88 | + let active = 0; |
| 89 | + let maxActive = 0; |
| 90 | + const runner: RunTask = async (task) => { |
| 91 | + active += 1; |
| 92 | + maxActive = Math.max(maxActive, active); |
| 93 | + await new Promise((r) => setTimeout(r, 5)); |
| 94 | + await q.complete(task.id, HANDOFF); |
| 95 | + active -= 1; |
| 96 | + }; |
| 97 | + await q.enqueue({ type: 'install' }); |
| 98 | + await q.enqueue({ type: 'init' }); |
| 99 | + await drainQueue(q, runner, { cap: 2, maxIterations: 50 }); |
| 100 | + expect(maxActive).toBe(2); |
| 101 | + }); |
| 102 | + |
| 103 | + it('runs independent tasks one at a time at cap 1', async () => { |
| 104 | + let active = 0; |
| 105 | + let maxActive = 0; |
| 106 | + const runner: RunTask = async (task) => { |
| 107 | + active += 1; |
| 108 | + maxActive = Math.max(maxActive, active); |
| 109 | + await new Promise((r) => setTimeout(r, 5)); |
| 110 | + await q.complete(task.id, HANDOFF); |
| 111 | + active -= 1; |
| 112 | + }; |
| 113 | + await q.enqueue({ type: 'install' }); |
| 114 | + await q.enqueue({ type: 'init' }); |
| 115 | + await drainQueue(q, runner, { cap: 1, maxIterations: 50 }); |
| 116 | + expect(maxActive).toBe(1); |
| 117 | + }); |
| 118 | + |
| 119 | + it('terminates via the iteration backstop instead of looping forever', async () => { |
| 120 | + const a = await q.enqueue({ type: 'install', maxAttempts: 999 }); |
| 121 | + const neverReports: RunTask = async () => { |
| 122 | + /* would retry forever without the backstop */ |
| 123 | + }; |
| 124 | + await drainQueue(q, neverReports, { cap: 1, maxIterations: 3 }); |
| 125 | + expect(q.get(a.id)?.attempts).toBeLessThanOrEqual(3); |
| 126 | + }); |
| 127 | +}); |
0 commit comments