@devries-cameron20 / t3code Mirror · read-only / commits / fec3b3a227
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 18:30 UTC

fix(preview): bound stalled screenshot captures (#25862)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Marcos Caballero committed Nov 13, 2026 at 01:25 UTC fec3b3a227f7b5da7b86f743f1aa9d8645f4a9df
2 files changed +65 -1
apps/server/src/preview/ServerBrowserPage.test.ts
+45
@@ -19,9 +19,54 @@ import {
19 } from "vite-plus/test";
20
21 import * as ServerBrowserPage from "./ServerBrowserPage.ts";
22 +import { SessionControl } from "./SessionControl.ts";
23
24 const decodeEvaluateInput = Schema.decodeUnknownSync(PreviewAutomationEvaluateInput);
25
26 +describe("server browser capture deadlines", () => {
27 + it("releases control after an unanswered capture and ignores its late reply", async () => {
28 + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
29 + try {
30 + const captured = Promise.withResolvers<{ data: string }>();
31 + const send = vi.fn(async (method: string) =>
32 + method === "Page.captureScreenshot"
33 + ? captured.promise
34 + : { result: { value: "still responsive" } },
35 + );
36 + const cdp = { send } as unknown as CDPSession;
37 + const control = new SessionControl("agent");
38 + let outcome: unknown;
39 + const capture = control
40 + .agent("agent", () => ServerBrowserPage.captureViewport(cdp, { format: "png" }))
41 + .catch((cause: unknown) => {
42 + outcome = cause;
43 + });
44 + const evaluation = control.agent("agent", () =>
45 + ServerBrowserPage.evaluate(cdp, { expression: "document.title" }, 1_000),
46 + );
47 +
48 + await vi.advanceTimersByTimeAsync(3_000);
49 + expect(outcome).toMatchObject({
50 + tag: "PreviewAutomationExecutionError",
51 + message: expect.stringContaining("not producing screenshot frames"),
52 + });
53 + await capture;
54 + expect(await evaluation).toBe("still responsive");
55 + expect(vi.getTimerCount()).toBe(0);
56 +
57 + captured.resolve({ data: "late-frame" });
58 + await captured.promise;
59 + expect(outcome).toBeInstanceOf(ServerBrowserPage.ServerBrowserOperationError);
60 + expect(send.mock.calls.map(([method]) => method)).toEqual([
61 + "Page.captureScreenshot",
62 + "Runtime.evaluate",
63 + ]);
64 + } finally {
65 + vi.useRealTimers();
66 + }
67 + });
68 +});
69 +
70 describe("server browser element refs", () => {
71 let browser: Browser;
72 let context: BrowserContext;
apps/server/src/preview/ServerBrowserPage.ts
+20 -1
@@ -29,6 +29,8 @@ const isBrowserControlInterrupted = Schema.is(BrowserControlInterrupted);
29 const MAX_EVALUATION_BYTES = 64_000;
30 const MAX_VISIBLE_TEXT_LENGTH = 20_000;
31 const MAX_SCREENSHOT_WIDTH = 1280;
32 +// Two attempts must fit in a snapshot's 10-second deadline.
33 +const SCREENSHOT_TIMEOUT_MS = 3_000;
34 const WAIT_POLL_MS = 100;
35 export const DIAGNOSTIC_BUFFER_LIMIT = 200;
36
@@ -468,10 +470,27 @@ export const captureViewport = async (
470 cdp: CDPSession,
471 options: { readonly format: "png" | "jpeg"; readonly quality?: number },
472 ) => {
471 - const { data } = await cdp.send("Page.captureScreenshot", {
473 + const capture = cdp.send("Page.captureScreenshot", {
474 format: options.format,
475 ...(options.quality === undefined ? {} : { quality: options.quality }),
476 });
477 + let timer: ReturnType<typeof setTimeout> | undefined;
478 + const expired = new Promise<never>((_, reject) => {
479 + timer = setTimeout(
480 + () =>
481 + reject(
482 + new ServerBrowserOperationError({
483 + tag: "PreviewAutomationExecutionError",
484 + message:
485 + "The browser is not producing screenshot frames. For a desktop tab, show or restore the T3 Code window and try again.",
486 + }),
487 + ),
488 + SCREENSHOT_TIMEOUT_MS,
489 + );
490 + });
491 + // Hidden native guests can leave CDP captures pending forever. This deadline also
492 + // releases a viewer's still-capture lock, which an agent snapshot may be waiting for.
493 + const { data } = await Promise.race([capture, expired]).finally(() => clearTimeout(timer));
494 return data;
495 };
496