Skip to content

Commit d9539fb

Browse files
mchwangclaude
andcommitted
Skip cleanup for unauthenticated profiles and stop on late close
Commit pending work left in the D4 worktree: - Add isContainerProfileAuthentic and skip disposal when container validation fails for a profile whose identity was already removed. - Route a close observed after the deadline through stop('timeout') so a running decoder is aborted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 10c8d52 commit d9539fb

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

‎agents/adapters/supervisor.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ 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, type ContainerProfile } from '../container/profile.ts';
5+
import { assertContainerProfileAuthenticity, isContainerProfileAuthentic,
6+
type ContainerProfile } from '../container/profile.ts';
67
import { removeVendorNetwork, type VendorNetwork } from '../network/network.ts';
78

89
export const OUTPUT_LIMITS = Object.freeze({
@@ -256,6 +257,7 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
256257
try {
257258
createValidatedContainer(profile, remaining(), options.secrets ?? {});
258259
} catch (error) {
260+
if (!isContainerProfileAuthentic(profile)) throw error;
259261
try { disposeValidatedContainer(profile); }
260262
catch (cleanupError) {
261263
const detail = `Container validation failed and cleanup remains unsettled: ${String(error)}; ${String(cleanupError)}`;
@@ -510,8 +512,8 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
510512
child.once('close', async (code, signal) => {
511513
for (const timer of timers) clearTimeout(timer);
512514
timers.clear();
513-
if (!stopReason && performance.now() >= deadline) stopReason = 'timeout';
514515
closed = true;
516+
if (!stopReason && performance.now() >= deadline) stop('timeout');
515517
if (protocolBuffer.length) {
516518
if (!protocolLine(protocolBuffer)) capture('stderr', protocolBuffer, true);
517519
protocolBuffer = Buffer.alloc(0);

‎agents/container/profile.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export function assertContainerProfileAuthenticity(profile: ContainerProfile): v
132132
if (!identities.has(profile)) throw new Error('Container profile was not created by the trusted profile builder.');
133133
}
134134

135+
export function isContainerProfileAuthentic(profile: ContainerProfile): boolean {
136+
return identities.has(profile);
137+
}
138+
135139
/** Internal authenticity and host-file revalidation used at every launch boundary. */
136140
export function assertContainerProfile(profile: ContainerProfile, timeoutMs = 30_000): void {
137141
const expected = identities.get(profile);
@@ -149,10 +153,6 @@ export function assertContainerProfile(profile: ContainerProfile, timeoutMs = 30
149153
}
150154
}
151155

152-
export function isContainerProfileAuthentic(profile: ContainerProfile): boolean {
153-
return identities.has(profile);
154-
}
155-
156156
/** Clamp a Docker budget to the captured invocation deadline, which no launch may outlive. */
157157
export function profileTimeout(profile: ContainerProfile, timeoutMs: number, now = Date.now()): number {
158158
const expected = identities.get(profile);

‎test/agent-container.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { join } from 'node:path';
66
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest';
77
import { captureInvocation, type InvocationInput, type Phase } from '../agents/contract.ts';
88
import { AGENT_IMAGE, assertBuiltAgentImage, buildAgentImage } from '../agents/container/image.ts';
9-
import { assertContainerProfile, createContainerProfile, disposeContainerProfile } from '../agents/container/profile.ts';
9+
import { assertContainerProfile, createContainerProfile, disposeContainerProfile,
10+
isContainerProfileAuthentic } from '../agents/container/profile.ts';
1011
import { createValidatedContainer, prepareTaskFilesystems, removeTaskFilesystems, runContainer, startValidatedContainer,
1112
hasExactOptions, validateContainer } from '../agents/container/run.ts';
1213
import { createTaskClone } from '../git/clone.ts';
@@ -272,6 +273,7 @@ describe('real Docker agent isolation', () => {
272273
docker(...first.args); containers.add(first.name);
273274
expect(() => createValidatedContainer(duplicate)).toThrow('Container creation failed and cleanup did not settle.');
274275
expect(existsSync(duplicate.codexAuthFile!)).toBe(true);
276+
expect(isContainerProfileAuthentic(duplicate)).toBe(true);
275277
const state = JSON.parse(docker('container', 'inspect', first.name))[0] as { State: { Status: string } };
276278
expect(state.State.Status).toBe('created');
277279
docker('rm', '--force', first.name); containers.delete(first.name);

0 commit comments

Comments
 (0)