Skip to content

Commit dd0af27

Browse files
mchwangclaude
andcommitted
Retain captured output in fixed-size blocks
Each output event was stored as its own Buffer slice, so a container could emit the byte ceiling as millions of one-byte writes and exhaust supervisor memory before the byte limit applied. Output is now copied into 64 KiB blocks, so retained objects scale with bytes rather than with write events, and slices no longer pin their original chunks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 691df93 commit dd0af27

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎.github/workflows/agent-isolation.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ jobs:
2929
- run: npm ci --ignore-scripts
3030
- run: npm run typecheck
3131
# The Docker suites share one image tag and daemon, so run test files one at a time.
32-
- run: npx vitest run --no-file-parallelism test/agent-contract.test.ts test/agent-clone.test.ts test/agent-container.test.ts test/agent-network.test.ts test/agent-policy.test.ts test/agent-proxy.test.ts test/agent-adapter.test.ts test/agent-supervisor.test.ts
32+
- run: npx vitest run --no-file-parallelism test/agent-contract.test.ts test/agent-clone.test.ts test/agent-container.test.ts test/agent-network.test.ts test/agent-policy.test.ts test/agent-proxy.test.ts test/agent-adapter.test.ts test/agent-supervisor.test.ts test/agent-output.test.ts

‎agents/adapters/supervisor.ts‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,35 @@ const captureLimits = (override: Partial<CaptureLimits> | undefined): CaptureLim
6060
};
6161
const diagnosticFor = (reason: StopReason, detail?: string) => Buffer.from(
6262
`[codeboost: ${reason}${detail ? `: ${detail.replace(/[\r\n]+/g, ' ').slice(0, 512)}` : ''}]\n`);
63+
/**
64+
* Retains captured output in fixed-size blocks, so memory scales with retained bytes rather than with the number
65+
* of write events a container emits.
66+
*/
67+
export class ByteCollector {
68+
static readonly BLOCK_BYTES = 64 * 1024;
69+
private readonly blocks: Buffer[] = [];
70+
private used = 0;
71+
push(data: Buffer): void {
72+
for (let offset = 0; offset < data.length;) {
73+
let block = this.blocks.at(-1);
74+
if (!block || this.used === block.length) {
75+
block = Buffer.allocUnsafe(ByteCollector.BLOCK_BYTES);
76+
this.blocks.push(block);
77+
this.used = 0;
78+
}
79+
const count = Math.min(data.length - offset, block.length - this.used);
80+
data.copy(block, this.used, offset, offset + count);
81+
this.used += count;
82+
offset += count;
83+
}
84+
}
85+
toBuffer(): Buffer {
86+
if (!this.blocks.length) return Buffer.alloc(0);
87+
return Buffer.concat([...this.blocks.slice(0, -1), this.blocks.at(-1)!.subarray(0, this.used)]);
88+
}
89+
get blockCount(): number { return this.blocks.length; }
90+
}
91+
6392
const retainCleanupOwnership = (profile: ContainerProfile, detail: string, register = true): InvocationHandle => {
6493
const invocation = assertPhasePolicy(profile.policy);
6594
let resolveSettled!: (result: InvocationResult) => void, cleaning = false, complete = false;
@@ -274,7 +303,7 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
274303
throw error;
275304
}
276305

277-
const stdoutChunks: Buffer[] = [], stderrChunks: Buffer[] = [];
306+
const stdoutChunks = new ByteCollector(), stderrChunks = new ByteCollector();
278307
let stdoutBytes = 0, stderrBytes = 0, combinedBytes = 0;
279308
let stopReason: StopReason | undefined, failureDetail: string | undefined;
280309
let closed = false, terminating = false, settlementComplete = false;
@@ -388,7 +417,7 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
388417
? new CaptureDeadlineError('Adapter output capture exceeded the invocation deadline.')
389418
: new Error('Adapter output capture was cancelled.')), { once: true });
390419
});
391-
const raw = Buffer.concat(stdoutChunks, stdoutBytes);
420+
const raw = stdoutChunks.toBuffer();
392421
const operation = Promise.resolve(options.decode!(profile, raw,
393422
Math.max(1, Math.min(limits.stdoutBytes - stdoutBytes, limits.combinedBytes - combinedBytes)),
394423
budget, controller.signal));
@@ -526,7 +555,7 @@ export function startProfileInvocation(profile: ContainerProfile, options: Super
526555
if (!protocolLine(protocolBuffer)) capture('stderr', protocolBuffer, true);
527556
protocolBuffer = Buffer.alloc(0);
528557
}
529-
let finalStdout = Buffer.concat(stdoutChunks, stdoutBytes), finalStderr = Buffer.concat(stderrChunks);
558+
let finalStdout = stdoutChunks.toBuffer(), finalStderr = stderrChunks.toBuffer();
530559
let exitCode = code, finalSignal = signal;
531560
if (!stopReason && options.decode && !profile.deferredOutput) await decodeOutput();
532561
if (decodePromise) await decodePromise;

‎test/agent-output.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { ByteCollector } from '../agents/adapters/supervisor.ts';
3+
4+
describe('captured output storage', () => {
5+
it('keeps storage proportional to bytes, not to the number of writes', () => {
6+
const collector = new ByteCollector();
7+
for (let i = 0; i < 100_000; i++) collector.push(Buffer.from([i % 256]));
8+
expect(collector.blockCount).toBeLessThanOrEqual(2);
9+
const output = collector.toBuffer();
10+
expect(output.length).toBe(100_000);
11+
expect(output.every((byte, index) => byte === index % 256)).toBe(true);
12+
});
13+
14+
it('splits a large write across blocks without losing or reordering bytes', () => {
15+
const collector = new ByteCollector(), large = Buffer.alloc(3 * ByteCollector.BLOCK_BYTES + 17, 7);
16+
collector.push(Buffer.from('head'));
17+
collector.push(large);
18+
expect(collector.blockCount).toBe(4);
19+
expect(collector.toBuffer()).toEqual(Buffer.concat([Buffer.from('head'), large]));
20+
expect(new ByteCollector().toBuffer().length).toBe(0);
21+
});
22+
});

0 commit comments

Comments
 (0)