Skip to content

Commit cd1fc31

Browse files
mchwangclaude
andcommitted
Reject duplicate attempts without touching the active container
A duplicate attempt's profile never created a container, and its deterministic container name belongs to the active invocation, so rejection now releases only the duplicate profile's staging and network. Previously it tried to remove the container by name, which D2 now refuses for another invocation's container, leaving a cleanup handle that never settled. The regression reuses one captured invocation, since an attempt can only be captured once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d9539fb commit cd1fc31

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎agents/adapters/supervisor.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { execFile, spawn, type ChildProcess } from 'node:child_process';
22
import type { InvocationHandle, InvocationInput, InvocationResult, StopReason } from '../contract.ts';
33
import { assertPhasePolicy } from '../policy.ts';
44
import { createValidatedContainer, disposeValidatedContainer, validateContainer } from '../container/run.ts';
5-
import { assertContainerProfileAuthenticity, isContainerProfileAuthentic,
5+
import { assertContainerProfileAuthenticity, disposeContainerProfile, isContainerProfileAuthentic,
66
type ContainerProfile } from '../container/profile.ts';
77
import { removeVendorNetwork, type VendorNetwork } from '../network/network.ts';
88

@@ -228,7 +228,15 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
228228
if (ownsAttempt(invocation.attemptId)) {
229229
if (activeProfiles.has(profile))
230230
throw new Error('This container profile already owns the active invocation.');
231-
return rejectWithCleanup(new Error('An invocation with this attempt ID is still active.'), false);
231+
// This profile never created a container; the name belongs to the active invocation, so only
232+
// release this profile's own staging and network.
233+
const error = new Error('An invocation with this attempt ID is still active.');
234+
try { disposeContainerProfile(profile); }
235+
catch (cleanupError) {
236+
return retainCleanupOwnership(profile,
237+
`Invocation was rejected and cleanup remains unsettled: ${String(error)}; ${String(cleanupError)}`, false);
238+
}
239+
throw error;
232240
}
233241
let limits: CaptureLimits, configuredTimeout: number, carriedBudget: number;
234242
try {

‎test/agent-supervisor.test.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ function invocation(data: ReturnType<typeof fixture>, attemptId: string, deadlin
4242
context: { snapshotId: 'snapshot', planId: 'plan', planRevision: 1, assignmentId: 'assignment',
4343
referencedCodeHash: 'code', stateVersion: 1 } });
4444
}
45-
function profile(data: ReturnType<typeof fixture>, probe: IsolationProbe, attemptId = `attempt-${Math.random()}`,
46-
deadlineMs = 2 * 60_000, deferredOutput = false) {
47-
const captured = invocation(data, attemptId, deadlineMs), policy = createPhasePolicy(captured);
45+
function profile(data: ReturnType<typeof fixture>, probe: IsolationProbe,
46+
attempt: string | InvocationInput = `attempt-${Math.random()}`, deadlineMs = 2 * 60_000, deferredOutput = false) {
47+
// An attempt can be captured once, so a duplicate-attempt profile reuses the captured invocation.
48+
const captured = typeof attempt === 'string' ? invocation(data, attempt, deadlineMs) : attempt;
49+
const policy = createPhasePolicy(captured);
4850
const network = createVendorNetwork(captured, imageId);
4951
const value = createContainerProfile({ invocation: captured, policy, network, filesystems: data.filesystems,
5052
inputDirectory: data.input, command: createIsolationProbeCommand(policy, probe), imageId, codexAuthFile: data.auth,
@@ -153,8 +155,9 @@ describe('container invocation supervisor', () => {
153155
}, 15_000);
154156

155157
it('blocks a duplicate attempt while the original container remains active', async () => {
156-
const data = fixture(), first = startProfileInvocation(profile(data, 'ignore-term', 'duplicate'), { timeoutMs: 30_000 });
157-
expect(() => startProfileInvocation(profile(data, 'finite-output', 'duplicate'))).toThrow('still active');
158+
const data = fixture(), duplicate = invocation(data, 'duplicate');
159+
const first = startProfileInvocation(profile(data, 'ignore-term', duplicate), { timeoutMs: 30_000 });
160+
expect(() => startProfileInvocation(profile(data, 'finite-output', duplicate))).toThrow('still active');
158161
expect(isInvocationActive('duplicate')).toBe(true);
159162
first.cancel('shutdown');
160163
expect((await first.settled).stopReason).toBe('shutdown');

0 commit comments

Comments
 (0)