Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(browser): clear prompts when their renderer detaches
Afloat41204110ddbe
diff --git a/server/browser/cdp/auth.ts b/server/browser/cdp/auth.tsindex 2234a0cede..7861422114 100644--- a/server/browser/cdp/auth.ts+++ b/server/browser/cdp/auth.ts@@ -30,14 +30,22 @@ export class AuthChallenges {}watch(session: CdpSession): () => void {+ const clear = () => {+ if (this.#pending?.session !== session) return;+ clearTimeout(this.#pending.timer);+ this.#pending = null;+ this.#emit(null);+ };const offs = [session.on("Fetch.requestPaused", (params: Params) => {void session.send("Fetch.continueRequest", { requestId: params.requestId }).catch(() => {});}),session.on("Fetch.authRequired", (params: Params) => this.#required(session, params)),+ session.onDetach(clear),];return () => {for (const off of offs) off();+ clear();};}diff --git a/server/browser/cdp/dialogs.ts b/server/browser/cdp/dialogs.tsindex 9154f93e63..c76b2a78d5 100644--- a/server/browser/cdp/dialogs.ts+++ b/server/browser/cdp/dialogs.ts@@ -27,14 +27,17 @@ export class Dialogs {/** Frames in other renderers raise their dialogs on their own session. */watch(session: CdpSession): () => void {+ const clear = () => {+ if (this.#pending?.session === session) this.#closed();+ };const offs = [session.on("Page.javascriptDialogOpening", (params) => this.#opened(session, params)),- session.on("Page.javascriptDialogClosed", () => {- if (this.#pending?.session === session) this.#closed();- }),+ session.on("Page.javascriptDialogClosed", clear),+ session.onDetach(clear),];return () => {for (const off of offs) off();+ clear();};}diff --git a/server/browser/cdp/driver-additions.test.ts b/server/browser/cdp/driver-additions.test.tsindex e7506125c5..6ab3a47ced 100644--- a/server/browser/cdp/driver-additions.test.ts+++ b/server/browser/cdp/driver-additions.test.ts@@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";import { AUTH_WAIT_MS, AuthChallenges } from "./auth.ts";import { CdpConnection, type CdpSession, type Params } from "./connection.ts";import { DriverError, isUnanswered } from "./errors.ts";+import { Dialogs } from "./dialogs.ts";import { FrameTree } from "./frames.ts";import { methodRefusal } from "./policy.ts";import type { CdpTransport } from "./transport.ts";@@ -79,6 +80,67 @@ describe("HTTP authentication challenges", () => {});});+describe("prompts whose renderer session went away", () => {+ it("closes a detached renderer's dialog and reports its closure once", () => {+ const { session, emit, detach } = eventSession();+ const dialogs = new Dialogs();+ const unwatch = dialogs.watch(session);+ const closed: string[] = [];+ dialogs.onClose((dialog) => closed.push(dialog.message));+ emit("Page.javascriptDialogOpening", { type: "confirm", message: "Old renderer" });+ detach();+ expect(dialogs.pending).toBeNull();+ unwatch();+ expect(closed).toEqual(["Old renderer"]);+ });++ it("does not clear another watched renderer's dialog", () => {+ const first = eventSession();+ const second = eventSession();+ const dialogs = new Dialogs();+ dialogs.watch(first.session);+ const unwatch = dialogs.watch(second.session);+ second.emit("Page.javascriptDialogOpening", { type: "prompt", message: "Current renderer" });+ first.detach();+ expect(dialogs.pending?.message).toBe("Current renderer");+ unwatch();+ expect(dialogs.pending).toBeNull();+ });++ it("clears detached authentication state and its cancellation timer", async () => {+ vi.useFakeTimers();+ const { session, sent, emit, detach } = eventSession();+ const auth = new AuthChallenges();+ const unwatch = auth.watch(session);+ const changes: Array<string | null> = [];+ auth.onChange((challenge) => changes.push(challenge?.realm ?? null));+ emit("Fetch.authRequired", { requestId: "a1", authChallenge: { source: "Server", realm: "Old renderer" } });+ expect(vi.getTimerCount()).toBe(1);+ detach();+ expect(auth.pending).toBeNull();+ expect(vi.getTimerCount()).toBe(0);+ unwatch();+ await vi.advanceTimersByTimeAsync(AUTH_WAIT_MS);+ expect(sent).toEqual([]);+ expect(changes).toEqual(["Old renderer", null]);+ });++ it("keeps another renderer's authentication challenge until its watch ends", () => {+ vi.useFakeTimers();+ const first = eventSession();+ const second = eventSession();+ const auth = new AuthChallenges();+ auth.watch(first.session);+ const unwatch = auth.watch(second.session);+ second.emit("Fetch.authRequired", { requestId: "a2", authChallenge: { source: "Server", realm: "Current renderer" } });+ first.detach();+ expect(auth.pending?.realm).toBe("Current renderer");+ unwatch();+ expect(auth.pending).toBeNull();+ expect(vi.getTimerCount()).toBe(0);+ });+});+describe("a tab whose session went away", () => {it("has no main frame left, rather than a stale one", () => {const { session, detach } = eventSession();