Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: retry unsuccessful browser viewport changes

Afloat3de4bee18e64
diff --git a/server/browser-page-size-retry.test.ts b/server/browser-page-size-retry.test.ts
new file mode 100644
index 0000000000..d9e9dd9613
--- /dev/null
+++ b/server/browser-page-size-retry.test.ts
@@ -0,0 +1,42 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { BROWSER_VIEWPORT, PageSizes } from "./browser-page-size.ts";
+
+describe("page sizing after an unsuccessful attempt", () => {
+ it.each(["rejects", "returns false"])("retries before the next bot call when the viewport change %s", async (failure) => {
+ const apply = vi.fn().mockResolvedValue(undefined);
+ if (failure === "rejects") apply.mockRejectedValueOnce(new Error("browser restarting"));
+ else apply.mockResolvedValueOnce(false);
+ const sizes = new PageSizes({ apply, lane: (_session, work) => work(), watched: () => true });
+ await sizes.resize("s", { width: 1440, height: 900 });
+ expect(apply).toHaveBeenCalledTimes(1);
+ expect(await sizes.beforeCall("s")).toBe(true);
+ expect(apply).toHaveBeenLastCalledWith("s", { width: 1440, height: 900 });
+ expect(apply).toHaveBeenCalledTimes(2);
+ expect(await sizes.beforeCall("s")).toBe(false);
+ });
+
+ it.each(["rejects", "returns false"])("keeps a pre-call change pending when it %s", async (failure) => {
+ const apply = vi.fn().mockResolvedValue(undefined);
+ if (failure === "rejects") apply.mockRejectedValueOnce(new Error("browser restarting"));
+ else apply.mockResolvedValueOnce(false);
+ const sizes = new PageSizes({ apply, lane: (_session, work) => work(), watched: () => false });
+ expect(await sizes.beforeCall("s")).toBe(true);
+ expect(await sizes.beforeCall("s")).toBe(true);
+ expect(apply).toHaveBeenCalledTimes(2);
+ expect(apply).toHaveBeenLastCalledWith("s", BROWSER_VIEWPORT);
+ expect(await sizes.beforeCall("s")).toBe(false);
+ });
+
+ it("stops when the lane wait expires and leaves the change for the next call", async () => {
+ const apply = vi.fn().mockResolvedValue(undefined);
+ const lane = vi.fn<(session: string, work: () => Promise<void>) => Promise<void | null>>()
+ .mockResolvedValueOnce(null).mockImplementation((_session, work) => work());
+ const sizes = new PageSizes({ apply, lane, watched: () => true });
+ await sizes.resize("s", { width: 1440, height: 900 });
+ expect(lane).toHaveBeenCalledTimes(1);
+ expect(apply).not.toHaveBeenCalled();
+ expect(await sizes.beforeCall("s")).toBe(true);
+ expect(apply).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/server/browser-page-size.ts b/server/browser-page-size.ts
index 0a3fcd93a0..8745553887 100644
--- a/server/browser-page-size.ts
+++ b/server/browser-page-size.ts
@@ -30,7 +30,7 @@ export function streamedPage(panel: PageSize): PageSize {
const key = (size: PageSize) => `${size.width}x${size.height}`;
export interface PageSizeHost {
- /** Sizes the session's running page; resolves once the engine answered or gave up. */
+ /** Sizes the session's running page; false means the engine gave up. */
apply(session: string, size: PageSize): Promise<unknown>;
/** Runs `work` in the session's browser lane, after the bot's call running now; null when that call outlasted the wait. */
lane(session: string, work: () => Promise<void>): Promise<void | null>;
@@ -89,12 +89,15 @@ export class PageSizes {
async #follow(session: string, entry: { wanted: PageSize; applied: string | null; applying: Promise<void> | null }): Promise<void> {
try {
while (this.#behind(session, entry)) {
- await this.#host.lane(session, async () => {
+ let applied = false;
+ const ran = await this.#host.lane(session, async () => {
const wanted = entry.wanted;
if (!this.#behind(session, entry)) return;
- await this.#host.apply(session, wanted).catch(() => undefined);
+ if (await this.#host.apply(session, wanted).catch(() => false) === false) return;
entry.applied = key(wanted);
+ applied = true;
});
+ if (ran === null || !applied) return;
}
} finally {
entry.applying = null;
@@ -106,8 +109,7 @@ export class PageSizes {
const entry = this.#entry(session);
const wanted = entry.wanted;
if (entry.applied === key(wanted)) return false;
- await this.#host.apply(session, wanted).catch(() => undefined);
- entry.applied = key(wanted);
+ if (await this.#host.apply(session, wanted).catch(() => false) !== false) entry.applied = key(wanted);
return true;
}