Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(browser): discard transport input after closure

Seth Wilsond97919889376
diff --git a/src/capabilities/browser/pipe.test.ts b/src/capabilities/browser/pipe.test.ts
index efb4400c..9874053c 100644
--- a/src/capabilities/browser/pipe.test.ts
+++ b/src/capabilities/browser/pipe.test.ts
@@ -51,6 +51,23 @@ describe('private CDP pipe bounds and failure handling', () => {
expect(failure).toHaveBeenCalledOnce();
});
+ it('discards partial and late browser output once the transport is closed', async () => {
+ const { pipe, output } = connection();
+ const event = vi.fn();
+ pipe.onEvent = event;
+ const pending = expect(pipe.send('Page.enable')).rejects.toThrow('closed');
+ output.write('{"id":1,"res');
+ pipe.fail();
+ await pending;
+ const buffered = () => (pipe as unknown as { buffer: string }).buffer;
+ expect(buffered()).toBe('');
+ output.write('{"method":"Fetch.requestPaused","params":{}}\0');
+ output.write('x'.repeat(8_000_001));
+ output.write('y'.repeat(8_000_001));
+ expect(event).not.toHaveBeenCalled();
+ expect(buffered()).toBe('');
+ });
+
it('rejects oversized or malformed messages and process exit while a command is pending', async () => {
for (const fail of [(c: ReturnType<typeof connection>) => c.output.write('x'.repeat(8_000_001)),
(c: ReturnType<typeof connection>) => c.output.write('invalid-json\0'),
diff --git a/src/capabilities/browser/pipe.ts b/src/capabilities/browser/pipe.ts
index 142c6203..691eb53b 100644
--- a/src/capabilities/browser/pipe.ts
+++ b/src/capabilities/browser/pipe.ts
@@ -19,6 +19,7 @@ export class BrowserPipe {
const read = child.stdio[4] as Readable;
read.setEncoding('utf8');
read.on('data', (chunk: string) => {
+ if (this.closed) return;
this.buffer += chunk;
if (this.buffer.length > 8_000_000) { this.fail(); return; }
let at: number;
@@ -88,6 +89,7 @@ export class BrowserPipe {
fail(): void {
if (this.closed) return;
this.closed = true;
+ this.buffer = '';
this.writes = [];
this.queuedBytes = 0;
for (const entry of this.pending.values()) { clearTimeout(entry.timer); entry.reject(new Error('Browser closed unexpectedly.')); }