Skip to content

Commit 1ca7e95

Browse files
mchwangclaude
andcommitted
Retry the profile cleanup once per attempt in the adapters
Review fix (Copilot, #69): when profile creation and its cleanup both failed, each adapter retry ran the profile's cleanup (which already removes the vendor network) and then removed that network again with a fresh full budget, so a retained retry could run past the shrinking cleanup window. The adapters now retain exactly the profile's cleanup with the window's budget, as the network-creation path already did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4b1c0f7 commit 1ca7e95

3 files changed

Lines changed: 66 additions & 24 deletions

File tree

‎agents/adapters/claude.ts‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,11 @@ export function startClaudeInvocation(request: AgentAdapterRequest,
3535
invocationBudget: remaining,
3636
decode: (_profile, raw) => parseClaudeOutput(raw) });
3737
} catch (error) {
38-
if (error instanceof ProfileCreationCleanupError) {
39-
const retryCleanup = (networkTimeoutMs = 30_000) => {
40-
const failures: unknown[] = [];
41-
try { error.retryCleanup(networkTimeoutMs); } catch (cleanupError) { failures.push(cleanupError); }
42-
try { removeVendorNetwork(network, networkTimeoutMs); } catch (cleanupError) { failures.push(cleanupError); }
43-
if (failures.length) throw new AggregateError(failures, 'Adapter setup cleanup did not settle.');
44-
};
45-
try { retryCleanup(Math.min(30_000, remaining())); }
46-
catch (cleanupError) { return retainSetupCleanup(request.invocation, budget => retryCleanup(budget),
47-
error.startupError, cleanupError, 'profile and network cleanup', () => error.resources); }
48-
throw error.startupError;
49-
}
38+
// Profile creation already tried its cleanup, which removes this network too, and failed. Retry that one cleanup
39+
// with the window's budget; removing the network again here would start a second full deadline per retry.
40+
if (error instanceof ProfileCreationCleanupError)
41+
return retainSetupCleanup(request.invocation, budget => error.retryCleanup(budget), error.startupError, error,
42+
'profile and network cleanup', () => error.resources);
5043
try { removeVendorNetwork(network, Math.min(30_000, remaining())); }
5144
catch (cleanupError) { return retainNetworkCleanup(request.invocation, network, error, cleanupError); }
5245
throw error;

‎agents/adapters/codex.ts‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,11 @@ export function startCodexInvocation(request: AgentAdapterRequest,
4545
decode: (current, _raw, maximum, timeoutMs, signal) =>
4646
readCodexOutput(codexContainer(current), maximum, timeoutMs, signal) });
4747
} catch (error) {
48-
if (error instanceof ProfileCreationCleanupError) {
49-
const retryCleanup = (networkTimeoutMs = 30_000) => {
50-
const failures: unknown[] = [];
51-
try { error.retryCleanup(networkTimeoutMs); } catch (cleanupError) { failures.push(cleanupError); }
52-
try { removeVendorNetwork(network, networkTimeoutMs); } catch (cleanupError) { failures.push(cleanupError); }
53-
if (failures.length) throw new AggregateError(failures, 'Adapter setup cleanup did not settle.');
54-
};
55-
try { retryCleanup(Math.min(30_000, remaining())); }
56-
catch (cleanupError) { return retainSetupCleanup(request.invocation, budget => retryCleanup(budget),
57-
error.startupError, cleanupError, 'profile and network cleanup', () => error.resources); }
58-
throw error.startupError;
59-
}
48+
// Profile creation already tried its cleanup, which removes this network too, and failed. Retry that one cleanup
49+
// with the window's budget; removing the network again here would start a second full deadline per retry.
50+
if (error instanceof ProfileCreationCleanupError)
51+
return retainSetupCleanup(request.invocation, budget => error.retryCleanup(budget), error.startupError, error,
52+
'profile and network cleanup', () => error.resources);
6053
try { removeVendorNetwork(network, Math.min(30_000, remaining())); }
6154
catch (cleanupError) { return retainNetworkCleanup(request.invocation, network, error, cleanupError); }
6255
throw error;

‎test/agent-adapter-cleanup.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)