-
Notifications
You must be signed in to change notification settings - Fork 0
fix(runner): bound the per-agent PTY output buffer #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /** | ||
| * The per-agent PTY buffer used to grow without limit for the whole life of an | ||
| * agent step, so a chatty agent's entire transcript stayed resident in the | ||
| * orchestrator's heap. A 34-step DAG with 9 agents OOM-killed its orchestrator | ||
| * in an 8 GB sandbox partway through (AgentWorkforce/cloud#1967, dev run | ||
| * 71cc4995 — `bun exited with signal SIGKILL` while the parent node process | ||
| * survived, which is the cgroup OOM-killer signature). | ||
| * | ||
| * Retaining a tail is safe: consumers clip to the last few thousand characters, | ||
| * and the complete transcript is still written to the PTY log file on disk. | ||
| */ | ||
| import { readFileSync } from 'node:fs'; | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { WorkflowRunner } from '../runner.js'; | ||
|
|
||
| type BoundedAppend = ( | ||
| agentName: string, | ||
| buffer: string[] | undefined, | ||
| chunk: string, | ||
| ) => void; | ||
|
|
||
| /** Reach the private helper without widening the public surface. */ | ||
| function boundedAppend(runner: WorkflowRunner): BoundedAppend { | ||
| const fn = (runner as unknown as { appendBoundedPtyChunk: BoundedAppend }) | ||
| .appendBoundedPtyChunk; | ||
| return fn.bind(runner) as BoundedAppend; | ||
| } | ||
|
|
||
| function maxChars(): number { | ||
| return (WorkflowRunner as unknown as { MAX_PTY_BUFFER_CHARS: number }) | ||
| .MAX_PTY_BUFFER_CHARS; | ||
| } | ||
|
|
||
| function newRunner(): WorkflowRunner { | ||
| return Object.create(WorkflowRunner.prototype) as WorkflowRunner; | ||
| } | ||
|
|
||
| function withSizes(runner: WorkflowRunner): WorkflowRunner { | ||
| (runner as unknown as { ptyOutputBufferSizes: Map<string, number> }) | ||
| .ptyOutputBufferSizes = new Map(); | ||
| return runner; | ||
| } | ||
|
|
||
| describe('PTY output buffer is bounded', () => { | ||
| it('keeps a chatty agent from growing the buffer without limit', () => { | ||
| const runner = withSizes(newRunner()); | ||
| const append = boundedAppend(runner); | ||
| const buffer: string[] = []; | ||
|
|
||
| // 40 MB of output through a 1 MB cap. | ||
| const chunk = 'x'.repeat(100_000); | ||
| for (let i = 0; i < 400; i += 1) append('scout', buffer, chunk); | ||
|
|
||
| const retained = buffer.join('').length; | ||
| expect(retained).toBeLessThanOrEqual(maxChars()); | ||
| // Sanity: the old behaviour would have retained everything. | ||
| expect(retained).toBeLessThan(40_000_000); | ||
| }); | ||
|
|
||
| it('retains the most recent output, not the oldest', () => { | ||
| const runner = withSizes(newRunner()); | ||
| const append = boundedAppend(runner); | ||
| const buffer: string[] = []; | ||
|
|
||
| append('scout', buffer, 'FIRST'); | ||
| for (let i = 0; i < 30; i += 1) append('scout', buffer, 'y'.repeat(100_000)); | ||
| append('scout', buffer, 'LAST'); | ||
|
|
||
| const retained = buffer.join(''); | ||
| // The tail is what every consumer reads, so it must survive. | ||
| expect(retained.endsWith('LAST')).toBe(true); | ||
| expect(retained).not.toContain('FIRST'); | ||
| }); | ||
|
|
||
| it('leaves small transcripts completely untouched', () => { | ||
| const runner = withSizes(newRunner()); | ||
| const append = boundedAppend(runner); | ||
| const buffer: string[] = []; | ||
|
|
||
| append('scout', buffer, 'hello '); | ||
| append('scout', buffer, 'world'); | ||
|
|
||
| expect(buffer.join('')).toBe('hello world'); | ||
| }); | ||
|
|
||
| it('keeps a single oversized write rather than dropping it entirely', () => { | ||
| const runner = withSizes(newRunner()); | ||
| const append = boundedAppend(runner); | ||
| const buffer: string[] = []; | ||
|
|
||
| const huge = 'z'.repeat(maxChars() * 2); | ||
| append('scout', buffer, huge); | ||
|
|
||
| // One chunk bigger than the cap is still visible — silently discarding it | ||
| // would lose the very output someone is debugging. | ||
| expect(buffer).toHaveLength(1); | ||
| expect(buffer[0]).toBe(huge); | ||
| }); | ||
|
|
||
| it('tracks size per agent independently', () => { | ||
| const runner = withSizes(newRunner()); | ||
| const append = boundedAppend(runner); | ||
| const scout = ['seed-scout']; | ||
| const lead = ['seed-lead']; | ||
|
|
||
| append('scout', scout, 'a'.repeat(10)); | ||
| append('lead', lead, 'b'.repeat(20)); | ||
|
|
||
| const sizes = (runner as unknown as { ptyOutputBufferSizes: Map<string, number> }) | ||
| .ptyOutputBufferSizes; | ||
| expect(sizes.get('scout')).toBe(10); | ||
| expect(sizes.get('lead')).toBe(20); | ||
| }); | ||
|
|
||
| it('is a no-op when the buffer is missing', () => { | ||
| const runner = withSizes(newRunner()); | ||
| const append = boundedAppend(runner); | ||
| expect(() => append('gone', undefined, 'anything')).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Regression cover for the rekey path (cubic P1 on relayflows#49). | ||
| * | ||
| * When the broker assigns an agent a different name than requested, | ||
| * `rekeyPtyStreams` installs a replacement listener under BOTH names and never | ||
| * swaps it out — so it handles every remaining chunk for the rest of that | ||
| * agent's life. The first version of this fix bounded the original listener but | ||
| * left the rekeyed one doing a bare `buffer.push(...)`, which meant any rekeyed | ||
| * agent stayed unbounded: the exact OOM the cap exists to prevent. | ||
| */ | ||
| describe('rekeyed PTY listener stays bounded', () => { | ||
| it('routes rekeyed chunks through the bounded append', () => { | ||
| const source = readFileSync( | ||
| new URL('../runner.ts', import.meta.url), | ||
| 'utf8', | ||
| ); | ||
| const rekeyBody = source.slice( | ||
| source.indexOf('const rekeyedListener = (chunk: string) => {'), | ||
| ); | ||
| const listener = rekeyBody.slice(0, rekeyBody.indexOf('};')); | ||
|
|
||
| // The bare push is what made rekeyed agents unbounded. | ||
| expect(listener).not.toMatch(/buffer\.push\(stripped\)/); | ||
| expect(listener).toContain('appendBoundedPtyChunk'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: This regression test greps runner.ts source text instead of exercising the rekey path, so it only proves the current string literals still exist. It gives false confidence: if the variable
strippedis ever renamed (e.g. toclean), a reintroduced barebuffer.push(clean)would bypass thenot.toMatch(/buffer\.push\(stripped\)/)assertion and the test would still pass while rekeyed agents go unbounded again. It is also brittle — harmless refactors (reformatting, splitting the listener, or a};appearing earlier in the body) will make the string-marker slicing silently produce an empty or wrong slice and fail with a confusing message unrelated to the real bug. Prefer a behavioral test that invokes the rekey path (or extracts the listener's chunk handler into a testable method) and asserts the retained buffer stays under MAX_PTY_BUFFER_CHARS.Prompt for AI agents