Skip to content

Commit 8bc00da

Browse files
mchwangclaude
andcommitted
Keep killed creates unsettled across cleanups and publish strict UTF-8
- Record, per profile, when a killed docker create stops counting as in flight. Later cleanup (for example the supervisor's dispose after an early create-path cleanup failure) no longer treats absence inside that window as proof and releases the profile; it reports "did not settle" so ownership is retained and retried. Also round the create-path wait to whole milliseconds, which the deadline helper requires. - Decode final stdout and stderr with a fatal UTF-8 policy. Invalid output becomes a capture-failure with that stream withheld, so replacement characters cannot grow the result past its byte ceilings; an incomplete trailing character cut at a limit is dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dd0af27 commit 8bc00da

5 files changed

Lines changed: 59 additions & 9 deletions

File tree

‎agents/adapters/supervisor.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,18 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
583583
});
584584
}
585585
}
586+
// Publish only strictly valid UTF-8: replacement characters would grow the result past the byte ceilings.
587+
// A multi-byte character cut at a capture limit is an incomplete tail, which the streaming decode drops.
588+
const strictText = (value: Buffer) => {
589+
try { return new TextDecoder('utf-8', { fatal: true }).decode(value, { stream: true }); }
590+
catch { return undefined; }
591+
};
592+
const stdoutText = strictText(finalStdout), stderrText = strictText(finalStderr);
593+
if (stdoutText === undefined || stderrText === undefined) {
594+
stopReason ??= 'capture-failure';
595+
failureDetail ??= 'Captured output is not valid UTF-8.';
596+
}
597+
finalStdout = Buffer.from(stdoutText ?? ''); finalStderr = Buffer.from(stderrText ?? '');
586598
if (stopReason) finalStderr = withDiagnostic(finalStderr, finalStdout.length, stopReason, limits, failureDetail);
587599
const result = Object.freeze({ attemptId: invocation.attemptId, context: invocation.context,
588600
exitCode, signal: finalSignal, ...(stopReason ? { stopReason } : {}),

‎agents/container/run.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ const canonicalDockerBindSource = (source: string) => {
4949
/** How long a killed `docker create` may still materialize its container in the daemon. */
5050
const CREATE_SETTLE_MS = 10_000;
5151
const sleep = (ms: number) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
52-
const removeContainerOrThrow = (profile: ContainerProfile, createUnsettled = false) => {
52+
// When a killed `docker create` for a profile stops counting as possibly in flight (performance.now() timestamp).
53+
const unsettledCreates = new WeakMap<ContainerProfile, number>();
54+
const removeContainerOrThrow = (profile: ContainerProfile, waitForSettle = false) => {
5355
// Destructive cleanup acts only for the builder-registered profile; a copy's name and label are not a capability.
5456
if (!isContainerProfileAuthentic(profile))
5557
throw new Error('Container profile was not created by the trusted profile builder.');
56-
const remaining = createDeadline(30_000 + (createUnsettled ? CREATE_SETTLE_MS : 0));
57-
const settleBy = performance.now() + (createUnsettled ? CREATE_SETTLE_MS : 0);
58+
const settleUntil = unsettledCreates.get(profile) ?? 0;
59+
const remaining = createDeadline(30_000 + (waitForSettle ? Math.max(0, Math.ceil(settleUntil - performance.now())) : 0));
5860
let before: ReturnType<typeof spawnSync>;
5961
for (;;) {
6062
before = spawnSync('docker', ['container', 'inspect', profile.name], {
@@ -63,12 +65,15 @@ const removeContainerOrThrow = (profile: ContainerProfile, createUnsettled = fal
6365
if (before.status === 0) break;
6466
const missing = !before.error && /No such (?:object|container)/i.test(`${before.stdout ?? ''}\n${before.stderr ?? ''}`);
6567
if (!missing) throw new Error('Failed to establish ownership of the agent container; staged credentials were retained.');
66-
if (!createUnsettled) {
68+
// A killed create may still land in the daemon; absence only counts once its settle window has passed. Only the
69+
// create path waits here; later cleanup (such as a supervisor recovery) reports "not settled" and retries later.
70+
const settled = performance.now() >= settleUntil;
71+
if (settled && !(waitForSettle && settleUntil)) {
72+
unsettledCreates.delete(profile);
6773
disposeContainerProfile(profile);
6874
return;
6975
}
70-
// A killed create may still land in the daemon; absence is not proof until the settle window passes.
71-
if (performance.now() >= settleBy)
76+
if (settled || !waitForSettle)
7277
throw new Error('Agent container creation did not settle; staged credentials were retained.');
7378
sleep(250);
7479
}
@@ -86,6 +91,7 @@ const removeContainerOrThrow = (profile: ContainerProfile, createUnsettled = fal
8691
&& /No such (?:object|container)/i.test(`${inspect.stdout ?? ''}\n${inspect.stderr ?? ''}`);
8792
if (!absent) throw new Error('Failed to confirm removal of the agent container; staged credentials were retained.');
8893
}
94+
unsettledCreates.delete(profile);
8995
disposeContainerProfile(profile);
9096
};
9197

@@ -299,6 +305,7 @@ export function createValidatedContainer(profile: ContainerProfile, timeoutMs =
299305
remaining();
300306
return profile.name;
301307
} catch (error) {
308+
if (createUnsettled) unsettledCreates.set(profile, performance.now() + CREATE_SETTLE_MS);
302309
try { removeContainerOrThrow(profile, createUnsettled); }
303310
catch (cleanupError) { throw new AggregateError([error, cleanupError], 'Container creation failed and cleanup did not settle.'); }
304311
throw error;

‎agents/policy.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function createCodexCommand(policy: PhasePolicy, prompt: string): AgentCo
9191
export type IsolationProbe = 'noop' | 'phase-worktree' | 'read-only-isolation' | 'persist-write'
9292
| 'persist-read' | 'capacity' | 'metadata' | 'must-not-run' | 'input-marker' | 'finite-output'
9393
| 'infinite-stdout' | 'infinite-stderr' | 'infinite-mixed' | 'ignore-term' | 'symlink-output'
94-
| 'oversized-output' | 'fifo-output' | 'invalid-utf8-output' | 'replace-output-directory'
94+
| 'oversized-output' | 'fifo-output' | 'invalid-utf8-output' | 'invalid-utf8-stderr' | 'replace-output-directory'
9595
| 'nonzero-output' | 'duplicate-protocol' | 'newline-free-deferred-output';
9696

9797
/** Fixed startup probes validate the sandbox itself without granting an agent a process tool. */
@@ -116,6 +116,7 @@ export function createIsolationProbeCommand(policy: PhasePolicy, probe: Isolatio
116116
'input-marker': 'set -eu; grep -q codeboost-schema-marker /run/codeboost-input/schema.json; '
117117
+ 'test ! -e /run/codeboost-input/extra.json',
118118
'finite-output': 'printf stdout-marker; printf stderr-marker >&2',
119+
'invalid-utf8-stderr': "printf 'bad-\\377\\377-stderr' >&2",
119120
'infinite-stdout': "while :; do head -c 4096 /dev/zero | tr '\\0' x; done",
120121
'infinite-stderr': "while :; do head -c 4096 /dev/zero | tr '\\0' x >&2; done",
121122
'infinite-mixed': "while :; do head -c 4096 /dev/zero | tr '\\0' x; head -c 4096 /dev/zero | tr '\\0' y >&2; done",

‎test/agent-container.test.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { captureInvocation, type InvocationInput, type Phase } from '../agents/c
88
import { AGENT_IMAGE, assertBuiltAgentImage, buildAgentImage } from '../agents/container/image.ts';
99
import { assertContainerProfile, createContainerProfile, disposeContainerProfile,
1010
isContainerProfileAuthentic } from '../agents/container/profile.ts';
11-
import { createValidatedContainer, prepareTaskFilesystems, removeTaskFilesystems, runContainer, startValidatedContainer,
12-
hasExactOptions, validateContainer } from '../agents/container/run.ts';
11+
import { createValidatedContainer, disposeValidatedContainer, prepareTaskFilesystems, removeTaskFilesystems, runContainer,
12+
startValidatedContainer, hasExactOptions, validateContainer } from '../agents/container/run.ts';
1313
import { createTaskClone } from '../git/clone.ts';
1414
import { createVendorNetwork, removeVendorNetwork, type VendorNetwork } from '../agents/network/network.ts';
1515
import { createClaudeCommand, createCodexCommand, createIsolationProbeCommand, createPhasePolicy,
@@ -293,6 +293,29 @@ describe('real Docker agent isolation', () => {
293293
expect(existsSync(unsettled.codexAuthFile!)).toBe(true);
294294
}, 60_000);
295295

296+
it('keeps a killed create unsettled for later cleanup until its settle window passes', () => {
297+
const data = fixture(), unsettled = profile(data, 'planning', 'noop');
298+
const shim = join(data.root, 'docker-shim'); mkdirSync(shim);
299+
const created = join(shim, 'created'), failed = join(shim, 'failed');
300+
const realDocker = execFileSync('sh', ['-c', 'command -v docker'], { encoding: 'utf8' }).trim();
301+
// The create client hangs until killed, and the first inspect after it fails for an unrelated reason, so the
302+
// create path gives up early, inside the settle window.
303+
writeFileSync(join(shim, 'docker'), ['#!/bin/sh',
304+
`if [ "$1" = create ]; then touch '${created}'; exec sleep 30; fi`,
305+
`if [ "$1" = container ] && [ "$2" = inspect ] && [ -e '${created}' ] && [ ! -e '${failed}' ]; then`,
306+
` touch '${failed}'; echo 'daemon unavailable' >&2; exit 1`, 'fi',
307+
`exec '${realDocker}' "$@"`].join('\n'), { mode: 0o755 });
308+
const path = process.env.PATH;
309+
process.env.PATH = `${shim}:${path}`;
310+
try {
311+
expect(() => createValidatedContainer(unsettled, 3_000)).toThrow('cleanup did not settle');
312+
// A follow-up cleanup inside the window must not treat absence as proof and release the profile.
313+
expect(() => disposeValidatedContainer(unsettled)).toThrow('did not settle');
314+
} finally { process.env.PATH = path; }
315+
expect(isContainerProfileAuthentic(unsettled)).toBe(true);
316+
expect(existsSync(unsettled.codexAuthFile!)).toBe(true);
317+
}, 60_000);
318+
296319
it('refuses to seed a clone whose staging directory was replaced after creation', () => {
297320
const data = fixture();
298321
const clone = createTaskClone({ source: data.source, parent: join(data.root, 'staging'), taskId: 'task-2',

‎test/agent-supervisor.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ describe('container invocation supervisor', () => {
154154
expect(result.stopReason).toBe('timeout');
155155
}, 15_000);
156156

157+
it('fails capture instead of publishing replacement characters for invalid UTF-8 stderr', async () => {
158+
const result = await startProfileInvocation(profile(fixture(), 'invalid-utf8-stderr'), { timeoutMs: 30_000 }).settled;
159+
expect(result.stopReason).toBe('capture-failure');
160+
expect(result.stderr).not.toContain('\uFFFD');
161+
expect(result.stderr).not.toContain('bad-');
162+
}, 60_000);
163+
157164
it('blocks a duplicate attempt while the original container remains active', async () => {
158165
const data = fixture(), duplicate = invocation(data, 'duplicate');
159166
const first = startProfileInvocation(profile(data, 'ignore-term', duplicate), { timeoutMs: 30_000 });

0 commit comments

Comments
 (0)