The computer's screen has no GPU and no window manager, so the tab in front is always on show and Chrome draws its animations in software at full rate whether or not anyone watches: one animated site kept a production computer at 200% CPU. - The agent parks the browser after COBBLE_BROWSER_PARK_AFTER_SECONDS (default 180, 0 turns it off) without a /mcp call or a screen view: a blank tab goes in front of every site on show, as a person switching tabs would, so pages keep their connections and see nothing unusual. The next /mcp call or screen view brings them back before it goes through. Done over the agent's own small CDP client (the image's Node has no WebSocket). - Chrome composites pages in software (--disable-gpu-compositing): about half the CPU while a bot works, WebGL unchanged. - The managed policy sets TabDiscardingExceptions so a parked tab is never discarded and reloaded. - The screen's websocket bridge now closes when a viewer goes away without a close frame; it used to stay half open for good. - Computer image bumped to 1.1.0. Co-Authored-By: Claude Opus 5.5 <[email protected]>
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
export type WebsocketFrame = {
|
|
fin: boolean;
|
|
opcode: number;
|
|
payload: Buffer;
|
|
};
|
|
|
|
type Params = {
|
|
onFrame: (frame: WebsocketFrame) => void;
|
|
/**
|
|
* A frame claiming more than this ends the connection.
|
|
*/
|
|
max_payload: number;
|
|
/**
|
|
* Who sends the frames: a client (a browser, the default) always
|
|
* masks them, a server (Chrome's DevTools, to the agent's own client)
|
|
* never does.
|
|
*/
|
|
from?: "client" | "server";
|
|
};
|
|
|
|
/**
|
|
* Reads websocket frames (RFC 6455) from a byte stream fed in any chunks:
|
|
* returns the function to feed them to, which calls `onFrame` with each
|
|
* whole frame, unmasked. Throws on a frame masked the wrong way for its
|
|
* sender, or an oversized one.
|
|
*/
|
|
export default function readWebsocketFrames({ onFrame, max_payload, from = "client" }: Params) {
|
|
let buffer: Buffer = Buffer.alloc(0);
|
|
|
|
return (chunk: Buffer) => {
|
|
buffer = buffer.length ? Buffer.concat([buffer, chunk]) : chunk;
|
|
|
|
while (buffer.length >= 2) {
|
|
const first = buffer[0]!;
|
|
const second = buffer[1]!;
|
|
const masked = Boolean(second & 0x80);
|
|
let length = second & 0x7f;
|
|
let offset = 2;
|
|
|
|
if (from == "client" && !masked) throw new Error("A client frame came unmasked.");
|
|
if (from == "server" && masked) throw new Error("A server frame came masked.");
|
|
|
|
if (length == 126) {
|
|
if (buffer.length < 4) return;
|
|
length = buffer.readUInt16BE(2);
|
|
offset = 4;
|
|
} else if (length == 127) {
|
|
if (buffer.length < 10) return;
|
|
const long = buffer.readBigUInt64BE(2);
|
|
if (long > BigInt(max_payload)) throw new Error("A frame was too large.");
|
|
length = Number(long);
|
|
offset = 10;
|
|
}
|
|
|
|
if (length > max_payload) throw new Error("A frame was too large.");
|
|
|
|
const mask_length = masked ? 4 : 0;
|
|
|
|
if (buffer.length < offset + mask_length + length) return;
|
|
|
|
const payload = Buffer.from(buffer.subarray(offset + mask_length, offset + mask_length + length));
|
|
|
|
if (masked) {
|
|
const mask = buffer.subarray(offset, offset + 4);
|
|
|
|
for (let index = 0; index < payload.length; index++) payload[index]! ^= mask[index & 3]!;
|
|
}
|
|
|
|
buffer = buffer.subarray(offset + mask_length + length);
|
|
onFrame({ fin: Boolean(first & 0x80), opcode: first & 0x0f, payload });
|
|
}
|
|
};
|
|
}
|