Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Phone web app WebSocket over the session: the close deadline covers a close the computer starts and a failed connection
Bugfixer 259fdb6bc2576
diff --git a/src/lib/e2e/websocket.test.ts b/src/lib/e2e/websocket.test.tsindex 4633923e17..43cfb0346a 100644--- a/src/lib/e2e/websocket.test.ts+++ b/src/lib/e2e/websocket.test.ts@@ -276,6 +276,47 @@ describe("StreamWebSocket frames", () => {expect(fake.state.reset).toBe(true);});+ /** Writes from now on wait until the stream is reset, as a session's do when the computer stops reading. */+ function holdWrites(fake: Fake, held: (bytes: Uint8Array) => boolean) {+ const stream = fake.open();+ const { write, reset } = stream;+ const waiting: ((error: Error) => void)[] = [];+ stream.write = (bytes) => (held(bytes) ? new Promise<void>((_, reject) => void waiting.push(reject)) : write(bytes));+ stream.reset = () => {+ reset();+ for (const reject of waiting.splice(0)) reject(new Error("stream reset"));+ };+ return waiting;+ }++ it("resets the stream and reports 1006 when its answer to the computer's close is not handed over in time", async () => {+ const fake = fakeComputer();+ const { page } = await switched(fake, { closeTimeoutMs: 50 });+ const waiting = holdWrites(fake, () => true);+ fake.push(serverFrame(8, Uint8Array.of(0x03, 0xe8)));+ await vi.waitFor(() => expect(waiting).toHaveLength(1));+ await vi.waitFor(() => expect(page.closes).toEqual([{ code: 1006, reason: "", wasClean: false }]));+ expect(fake.state.reset).toBe(true);+ });++ it("resets at once on a protocol error while waiting for the computer's close", async () => {+ const fake = fakeComputer();+ const { socket, page } = await switched(fake);+ socket.close(1000);+ fake.push(serverFrame(1, "x", { rsv: 4 }));+ await vi.waitFor(() => expect(page.closes).toEqual([{ code: 1006, reason: "", wasClean: false }]));+ expect(fake.state).toMatchObject({ reset: true, ended: false });+ });++ it("resets the stream when the close frame for a protocol error is not handed over in time", async () => {+ const fake = fakeComputer();+ const { page } = await switched(fake, { closeTimeoutMs: 50 });+ holdWrites(fake, () => true);+ fake.push(serverFrame(1, "x", { rsv: 4 }));+ await vi.waitFor(() => expect(page.closes).toEqual([{ code: 1006, reason: "", wasClean: false }]));+ await vi.waitFor(() => expect(fake.state.reset).toBe(true));+ });+it("puts a message of 300,000 one-byte fragments back together, empty fragments between them, and an all-empty one", async () => {const fake = fakeComputer();const { page } = await switched(fake);diff --git a/src/lib/e2e/websocket.ts b/src/lib/e2e/websocket.tsindex 72fd0f3d66..2cd953eefc 100644--- a/src/lib/e2e/websocket.ts+++ b/src/lib/e2e/websocket.ts@@ -120,7 +120,12 @@ export class StreamWebSocket {this.#state = "closing";this.#control(OP.close, code === undefined ? EMPTY : closePayload(code, why));// A computer that never answers would hold the stream: reset it, and the read loop reports 1006.- this.#closeTimer = setTimeout(() => this.#stream.reset(), this.#options.closeTimeoutMs ?? CLOSE_TIMEOUT_MS);+ this.#closeTimer ??= this.#deadline();+ }++ /** Resets the stream after the close deadline; a reset rejects any write still waiting, so nothing waits past it. */+ #deadline(): ReturnType<typeof setTimeout> {+ return setTimeout(() => this.#stream.reset(), this.#options.closeTimeoutMs ?? CLOSE_TIMEOUT_MS);}/** Frames go out in call order; a failed write leaves the chain rejected, so later sends fail too. */@@ -144,8 +149,13 @@ export class StreamWebSocket {return this.#finish({ code: 1006, reason: "", wasClean: false });}// Fail the connection: tell the computer why, then end the stream. The page sees 1006, as a browser reports it.- if (this.#state === "open") this.#control(OP.close, closePayload(error.code, utf8(error.message.slice(0, 123))));- this.#end();+ // Once this side's close is out there is nothing left to say or wait for: reset at once.+ if (this.#state === "open") {+ this.#control(OP.close, closePayload(error.code, utf8(error.message.slice(0, 123))));+ this.#end();+ } else {+ this.#stream.reset();+ }this.#finish({ code: 1006, reason: "", wasClean: false });}// Read on to the end of the stream so it is not reset under the computer.@@ -156,12 +166,15 @@ export class StreamWebSocket {}}- /** FIN once every queued frame is handed to the session, so the closing frame goes before it. */+ /** FIN once every queued frame is handed to the session, so the closing frame goes before it; reset at the deadline. */#end(): void {- void this.#sending.then(- () => this.#stream.end(),- () => this.#stream.reset(),- );+ const deadline = this.#deadline();+ void this.#sending+ .then(+ () => this.#stream.end(),+ () => this.#stream.reset(),+ )+ .finally(() => clearTimeout(deadline));}/** Reads frames until the closing handshake; throws a Failure for a protocol error, anything else when the stream ends. */@@ -241,6 +254,8 @@ export class StreamWebSocket {// Answer the computer's close with its code, unless this side already sent one.if (this.#state === "open") this.#control(OP.close, payload.length ? closePayload(code, EMPTY) : EMPTY);this.#state = "closing";+ // A computer that stops reading would hold the answer forever: the close deadline covers this side too.+ this.#closeTimer ??= this.#deadline();// Clean only once every frame, the answer included, is handed to the session; a reset meanwhile is a 1006.try {await this.#sending;