Skip to content

Commit d309dd7

Browse files
mchwangclaude
andcommitted
Release only the profile when rejecting an invocation before creation
Rejections before container creation (invalid limits, an expired deadline, a duplicate attempt) own no container, but they cleaned up with container-level disposal. If the deterministic name was held by another invocation or Docker inspect failed, that cleanup could never settle, and the recovery retried it forever while the profile's staging and network leaked. These paths now dispose only the profile, and recovery retries that same profile-only cleanup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a7a6eb9 commit d309dd7

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

‎agents/adapters/supervisor.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export class ByteCollector {
8989
get blockCount(): number { return this.blocks.length; }
9090
}
9191

92-
const retainCleanupOwnership = (profile: ContainerProfile, detail: string, register = true): InvocationHandle => {
92+
// `cleanup` is what recovery retries: container-level disposal by default, or profile-only disposal for a
93+
// rejection that happened before this profile created any container.
94+
const retainCleanupOwnership = (profile: ContainerProfile, detail: string, register = true,
95+
cleanup: (profile: ContainerProfile) => void = disposeValidatedContainer): InvocationHandle => {
9396
const invocation = assertPhasePolicy(profile.policy);
9497
let resolveSettled!: (result: InvocationResult) => void, cleaning = false, complete = false;
9598
let cancelReason: StopReason | undefined;
@@ -104,7 +107,7 @@ const retainCleanupOwnership = (profile: ContainerProfile, detail: string, regis
104107
if (cleaning || complete) return;
105108
cleaning = true;
106109
try {
107-
disposeValidatedContainer(profile);
110+
cleanup(profile);
108111
if (timer) clearTimeout(timer);
109112
timer = undefined;
110113
complete = true;
@@ -246,11 +249,14 @@ export function isInvocationActive(attemptId: string): boolean {
246249
export function startProfileInvocation(profile: ContainerProfile, options: SupervisorOptions = {}): InvocationHandle {
247250
assertContainerProfileAuthenticity(profile);
248251
const invocation = assertPhasePolicy(profile.policy);
252+
// Rejections before container creation own no container, so they release (and retry) only the profile's own
253+
// staging and network; a name held by another invocation or a failing inspect cannot block that.
249254
const rejectWithCleanup = (error: unknown, register = true): InvocationHandle => {
250-
try { disposeValidatedContainer(profile); }
255+
try { disposeContainerProfile(profile); }
251256
catch (cleanupError) {
252257
return retainCleanupOwnership(profile,
253-
`Invocation was rejected and cleanup remains unsettled: ${String(error)}; ${String(cleanupError)}`, register);
258+
`Invocation was rejected and cleanup remains unsettled: ${String(error)}; ${String(cleanupError)}`, register,
259+
disposeContainerProfile);
254260
}
255261
throw error;
256262
};
@@ -259,13 +265,7 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
259265
throw new Error('This container profile already owns the active invocation.');
260266
// This profile never created a container; the name belongs to the active invocation, so only
261267
// release this profile's own staging and network.
262-
const error = new Error('An invocation with this attempt ID is still active.');
263-
try { disposeContainerProfile(profile); }
264-
catch (cleanupError) {
265-
return retainCleanupOwnership(profile,
266-
`Invocation was rejected and cleanup remains unsettled: ${String(error)}; ${String(cleanupError)}`, false);
267-
}
268-
throw error;
268+
return rejectWithCleanup(new Error('An invocation with this attempt ID is still active.'), false);
269269
}
270270
let limits: CaptureLimits, configuredTimeout: number, carriedBudget: number;
271271
try {

‎test/agent-supervisor.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { isInvocationActive, readBoundedContainerFile, retainSetupCleanup,
99
startProfileInvocation } from '../agents/adapters/supervisor.ts';
1010
import { captureInvocation, type InvocationInput } from '../agents/contract.ts';
1111
import { buildAgentImage } from '../agents/container/image.ts';
12-
import { createContainerProfile, disposeContainerProfile, type ContainerProfile } from '../agents/container/profile.ts';
12+
import { createContainerProfile, disposeContainerProfile, isContainerProfileAuthentic,
13+
type ContainerProfile } from '../agents/container/profile.ts';
1314
import { disposeValidatedContainer, prepareTaskFilesystems, removeTaskFilesystems } from '../agents/container/run.ts';
1415
import { createVendorNetwork } from '../agents/network/network.ts';
1516
import { createIsolationProbeCommand, createPhasePolicy, type IsolationProbe } from '../agents/policy.ts';
@@ -167,6 +168,18 @@ describe('container invocation supervisor', () => {
167168
expect(result.stderr).not.toContain('cut-');
168169
}, 60_000);
169170

171+
it('releases only the profile when rejecting before creation, even if its name is held elsewhere', () => {
172+
const current = profile(fixture(), 'noop');
173+
// A foreign container occupies the deterministic name, so container-level cleanup could never settle.
174+
execFileSync('docker', ['create', '--name', current.name, '--label', 'io.codeboost.invocation=someone-else',
175+
'--entrypoint', 'true', imageId], { stdio: 'ignore' });
176+
try {
177+
expect(() => startProfileInvocation(current, { timeoutMs: 10 * 60_000 + 1 })).toThrow('ceiling');
178+
expect(isContainerProfileAuthentic(current)).toBe(false);
179+
expect(spawnSync('docker', ['container', 'inspect', current.name], { stdio: 'ignore' }).status).toBe(0);
180+
} finally { spawnSync('docker', ['rm', '--force', current.name], { stdio: 'ignore' }); }
181+
}, 60_000);
182+
170183
it('blocks a duplicate attempt while the original container remains active', async () => {
171184
const data = fixture(), duplicate = invocation(data, 'duplicate');
172185
const first = startProfileInvocation(profile(data, 'ignore-term', duplicate), { timeoutMs: 30_000 });

0 commit comments

Comments
 (0)