Skip to content

Commit a7a6eb9

Browse files
mchwangclaude
andcommitted
Fail closed on a trailing incomplete UTF-8 character unless truncated
The final output decode used streaming mode on every stream, so output ending in a lone lead byte was silently trimmed and published as a success. Only output cut at a capture limit may now end in an incomplete character; otherwise the decode flushes and fails closed as a capture-failure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6c757e1 commit a7a6eb9

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎agents/adapters/supervisor.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,11 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
584584
}
585585
}
586586
// 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.
587+
// Only output cut at a capture limit may end in an incomplete character, which the streaming decode then drops;
588+
// otherwise the decode flushes, so a trailing lone lead byte fails closed.
589+
const truncated = stopReason === 'output-limit';
588590
const strictText = (value: Buffer) => {
589-
try { return new TextDecoder('utf-8', { fatal: true }).decode(value, { stream: true }); }
591+
try { return new TextDecoder('utf-8', { fatal: true }).decode(value, truncated ? { stream: true } : undefined); }
590592
catch { return undefined; }
591593
};
592594
const stdoutText = strictText(finalStdout), stderrText = strictText(finalStderr);

‎agents/policy.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ 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' | 'invalid-utf8-stderr' | 'replace-output-directory'
94+
| 'oversized-output' | 'fifo-output' | 'invalid-utf8-output' | 'invalid-utf8-stderr' | 'truncated-utf8-stderr'
95+
| 'replace-output-directory'
9596
| 'nonzero-output' | 'duplicate-protocol' | 'newline-free-deferred-output';
9697

9798
/** Fixed startup probes validate the sandbox itself without granting an agent a process tool. */
@@ -117,6 +118,7 @@ export function createIsolationProbeCommand(policy: PhasePolicy, probe: Isolatio
117118
+ 'test ! -e /run/codeboost-input/extra.json',
118119
'finite-output': 'printf stdout-marker; printf stderr-marker >&2',
119120
'invalid-utf8-stderr': "printf 'bad-\\377\\377-stderr' >&2",
121+
'truncated-utf8-stderr': "printf 'cut-\\342' >&2",
120122
'infinite-stdout': "while :; do head -c 4096 /dev/zero | tr '\\0' x; done",
121123
'infinite-stderr': "while :; do head -c 4096 /dev/zero | tr '\\0' x >&2; done",
122124
'infinite-mixed': "while :; do head -c 4096 /dev/zero | tr '\\0' x; head -c 4096 /dev/zero | tr '\\0' y >&2; done",

‎test/agent-supervisor.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ describe('container invocation supervisor', () => {
161161
expect(result.stderr).not.toContain('bad-');
162162
}, 60_000);
163163

164+
it('fails capture when output ends in an incomplete character without reaching a limit', async () => {
165+
const result = await startProfileInvocation(profile(fixture(), 'truncated-utf8-stderr'), { timeoutMs: 30_000 }).settled;
166+
expect(result.stopReason).toBe('capture-failure');
167+
expect(result.stderr).not.toContain('cut-');
168+
}, 60_000);
169+
164170
it('blocks a duplicate attempt while the original container remains active', async () => {
165171
const data = fixture(), duplicate = invocation(data, 'duplicate');
166172
const first = startProfileInvocation(profile(data, 'ignore-term', duplicate), { timeoutMs: 30_000 });

0 commit comments

Comments
 (0)