|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { InvocationResult } from '../agents/contract.ts'; |
| 3 | + |
| 4 | +// When profile creation and its cleanup both fail, the adapter keeps retrying that one cleanup inside the bounded |
| 5 | +// window. It must not add a second network removal with a fresh deadline on every retry (#51 item 1). |
| 6 | +const state = vi.hoisted(() => ({ budgets: [] as { at: number; budget: number }[], networkRemovals: 0 })); |
| 7 | +vi.mock('../agents/network/network.ts', async importOriginal => ({ |
| 8 | + ...await importOriginal<typeof import('../agents/network/network.ts')>(), |
| 9 | + createVendorNetwork: () => ({ name: 'codeboost-egress-codex-x', proxyContainer: 'codeboost-proxy-codex-x', |
| 10 | + proxyUrl: 'http://10.254.0.2:3128', vendor: 'codex' }), |
| 11 | + removeVendorNetwork: () => { state.networkRemovals += 1; }, |
| 12 | +})); |
| 13 | +// The placeholder image is not built here; image trust is not what this test is about. |
| 14 | +vi.mock('../agents/container/image.ts', async importOriginal => ({ |
| 15 | + ...await importOriginal<typeof import('../agents/container/image.ts')>(), assertBuiltAgentImage: () => {}, |
| 16 | +})); |
| 17 | +vi.mock('../agents/container/profile.ts', async importOriginal => { |
| 18 | + const actual = await importOriginal<typeof import('../agents/container/profile.ts')>(); |
| 19 | + return { ...actual, createContainerProfile: () => { |
| 20 | + throw new actual.ProfileCreationCleanupError(new Error('schema input is not readable'), |
| 21 | + new Error('daemon unreachable'), (budgetMs?: number) => { |
| 22 | + state.budgets.push({ at: performance.now(), budget: budgetMs ?? 30_000 }); |
| 23 | + throw new Error('daemon unreachable'); |
| 24 | + }, () => [{ kind: 'network', name: 'codeboost-egress-codex-x' }]); |
| 25 | + } }; |
| 26 | +}); |
| 27 | +const { startCodexInvocation } = await import('../agents/adapters/codex.ts'); |
| 28 | +const { CLEANUP_RETRY_WINDOW_MS } = await import('../agents/adapters/supervisor.ts'); |
| 29 | +const { captureInvocation } = await import('../agents/contract.ts'); |
| 30 | + |
| 31 | +describe('adapter profile-creation cleanup', () => { |
| 32 | + beforeEach(() => { |
| 33 | + vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] }); |
| 34 | + state.budgets = []; state.networkRemovals = 0; |
| 35 | + }); |
| 36 | + afterEach(() => { vi.useRealTimers(); }); |
| 37 | + |
| 38 | + it('retries only the profile cleanup, within the window, and never removes the network separately', async () => { |
| 39 | + const invocation = captureInvocation({ |
| 40 | + clone: { id: 'clone', taskId: 'task', directory: '/tmp/task', head: 'a'.repeat(40) }, |
| 41 | + phase: 'planning', vendor: 'codex', approvedArgv: [], deadline: Date.now() + 10 * 60_000, |
| 42 | + attemptId: 'adapter-profile-cleanup', |
| 43 | + context: { snapshotId: 's', planId: 'p', planRevision: 1, assignmentId: 'a', referencedCodeHash: 'c', stateVersion: 1 }, |
| 44 | + }); |
| 45 | + const started = performance.now(); |
| 46 | + const handle = startCodexInvocation({ invocation, filesystems: {} as never, inputDirectory: '/unused', |
| 47 | + imageId: `sha256:${'a'.repeat(64)}`, prompt: 'unused' }, '/unused/auth.json'); |
| 48 | + const box: { result?: InvocationResult } = {}; |
| 49 | + void handle.settled.then(result => { box.result = result; }); |
| 50 | + await vi.advanceTimersByTimeAsync(CLEANUP_RETRY_WINDOW_MS + 5_000); |
| 51 | + expect(box.result?.unreleased).toEqual([{ kind: 'network', name: 'codeboost-egress-codex-x' }]); |
| 52 | + expect(state.networkRemovals).toBe(0); |
| 53 | + expect(state.budgets.length).toBeGreaterThan(1); |
| 54 | + for (const call of state.budgets) expect(call.at + call.budget).toBeLessThanOrEqual(started + CLEANUP_RETRY_WINDOW_MS); |
| 55 | + }); |
| 56 | +}); |
0 commit comments