The best way to control Claude Code, Codex and more from any computer or phone
fix(desktop): Browser Screenshot saves fresh pixels while T3 Code is hidden (#36671)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Luís Miguel committed
Nov 14, 2026 at 11:44 UTC
8a45c51d8b2796ebda1621e51becef1645e41f70
2 files changed
+273
-2
apps/desktop/src/preview/Manager.test.ts
+176
@@ -349,6 +349,47 @@ const makeTestPreviewWebContents = (
349
} as unknown as TestPreviewWebContents;
350
};
351
352
+const makeStillImage = (pixels = "fresh-png") => ({
353
+ toPNG: () => Buffer.from(pixels),
354
+ toJPEG: () => Buffer.from(pixels),
355
+ getSize: () => ({ width: 100, height: 80 }),
356
+});
357
+
358
+const makeTestStillImageWebContents = (...args: Parameters<typeof makeTestPreviewWebContents>) => {
359
+ const wc = makeTestPreviewWebContents(...args);
360
+ Object.assign(wc, { isDevToolsOpened: () => false });
361
+ holdStillImagePaint(wc, Promise.resolve());
362
+ return wc;
363
+};
364
+
365
+const holdStillImagePaint = (
366
+ wc: TestPreviewWebContents,
367
+ paint: Promise<void> | Array<Promise<void>>,
368
+) => {
369
+ vi.mocked(wc.debugger.sendCommand).mockImplementation(async (method) => {
370
+ if (method === "Page.getFrameTree") return { frameTree: { frame: { id: "main" } } };
371
+ if (method === "Page.createIsolatedWorld") return { executionContextId: 7 };
372
+ if (method === "Runtime.evaluate") {
373
+ await (Array.isArray(paint) ? paint.shift() : paint);
374
+ return { result: { value: true } };
375
+ }
376
+ return undefined;
377
+ });
378
+};
379
+
380
+const setupStill = (manager: PreviewManager.PreviewManager["Service"], paint = Promise.resolve()) =>
381
+ Effect.gen(function* () {
382
+ const image = makeStillImage();
383
+ const capturePage = vi.fn<() => Promise<typeof image>>().mockResolvedValue(image);
384
+ const wc = makeTestStillImageWebContents(capturePage);
385
+ holdStillImagePaint(wc, paint);
386
+ fromId.mockReturnValue(wc);
387
+ yield* manager.createTab("tab_still");
388
+ yield* manager.registerWebview("tab_still", wc.id);
389
+ yield* manager.setColorScheme("tab_still", "system");
390
+ return { image, capturePage, wc, capture: manager.captureScreenshot("tab_still") };
391
+ });
392
+
393
const setupRecordingRaceTabs = (manager: PreviewManager.PreviewManager["Service"]) =>
394
Effect.gen(function* () {
395
const capturePage = vi.fn(async () => ({
@@ -1824,6 +1865,30 @@ describe("PreviewManager", () => {
1865
),
1866
);
1867
1868
+ effectIt.effect("captures final pixels after warmup fails and paint readiness settles", () =>
1869
+ withManager((manager) =>
1870
+ Effect.gen(function* () {
1871
+ const paint = Promise.withResolvers<void>();
1872
+ const { capturePage, wc, capture } = yield* setupStill(manager, paint.promise);
1873
+ capturePage
1874
+ .mockRejectedValueOnce(new Error("UnknownVizError"))
1875
+ .mockRejectedValueOnce(new Error("UnknownVizError"))
1876
+ .mockRejectedValueOnce(new Error("UnknownVizError"))
1877
+ .mockResolvedValue(makeStillImage("final-png"));
1878
+ const fiber = yield* capture.pipe(Effect.forkChild({ startImmediately: true }));
1879
+ yield* TestClock.adjust(300);
1880
+ expect(capturePage).toHaveBeenCalledTimes(3);
1881
+ expect(writeFile).not.toHaveBeenCalled();
1882
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false]]);
1883
+ paint.resolve();
1884
+ const artifact = yield* Fiber.join(fiber);
1885
+ expect(capturePage).toHaveBeenCalledTimes(4);
1886
+ expect(writeFile.mock.calls).toEqual([[artifact.path, Buffer.from("final-png")]]);
1887
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
1888
+ }),
1889
+ ),
1890
+ );
1891
+
1892
effectIt.effect("preserves both screenshots of the same site in the same millisecond", () =>
1893
withManager((manager) =>
1894
Effect.gen(function* () {
@@ -1837,6 +1902,8 @@ describe("PreviewManager", () => {
1902
const capturePage = vi
1903
.fn<() => Promise<ReturnType<typeof pngImage>>>()
1904
.mockResolvedValueOnce(pngImage(firstPng))
1905
+ .mockResolvedValueOnce(pngImage(firstPng))
1906
+ .mockResolvedValueOnce(pngImage(secondPng))
1907
.mockResolvedValueOnce(pngImage(secondPng));
1908
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
1909
yield* manager.createTab("tab_1");
@@ -1857,6 +1924,115 @@ describe("PreviewManager", () => {
1924
),
1925
);
1926
1927
+ effectIt.effect(
1928
+ "waits for paint before capturing and restores rendering after overlapping requests",
1929
+ () =>
1930
+ withManager((manager) =>
1931
+ Effect.gen(function* () {
1932
+ const paints = [Promise.withResolvers<void>(), Promise.withResolvers<void>()];
1933
+ const pendingPaints = paints.map((paint) => paint.promise);
1934
+ const { image, capturePage, wc, capture } = yield* setupStill(manager);
1935
+ holdStillImagePaint(wc, pendingPaints);
1936
+ const first = Promise.withResolvers<typeof image>();
1937
+ const second = Promise.withResolvers<typeof image>();
1938
+ capturePage
1939
+ .mockResolvedValueOnce(makeStillImage("discarded-warmup"))
1940
+ .mockResolvedValueOnce(makeStillImage("discarded-warmup"))
1941
+ .mockReturnValueOnce(first.promise)
1942
+ .mockReturnValueOnce(second.promise);
1943
+ const hostThrottling = vi.fn();
1944
+ yield* manager.setMainWindow({
1945
+ isDestroyed: () => false,
1946
+ once: vi.fn(),
1947
+ webContents: { setBackgroundThrottling: hostThrottling },
1948
+ } as never);
1949
+ const one = yield* capture.pipe(Effect.forkChild({ startImmediately: true }));
1950
+ const two = yield* capture.pipe(Effect.forkChild({ startImmediately: true }));
1951
+ yield* TestClock.adjust(0);
1952
+ expect(capturePage).toHaveBeenCalledTimes(2);
1953
+ expect(pendingPaints).toHaveLength(0);
1954
+ expect(writeFile).not.toHaveBeenCalled();
1955
+ expect(hostThrottling.mock.calls).toEqual([[false]]);
1956
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false]]);
1957
+ paints.forEach((paint) => paint.resolve());
1958
+ yield* TestClock.adjust(0);
1959
+ expect(capturePage).toHaveBeenCalledTimes(4);
1960
+ first.resolve(image);
1961
+ yield* Fiber.join(one);
1962
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false]]);
1963
+ second.resolve(image);
1964
+ yield* Fiber.join(two);
1965
+ const saved = writeFile.mock.calls.map(([, bytes]) => bytes);
1966
+ expect(saved).toEqual([image.toPNG(), image.toPNG()]);
1967
+ expect(hostThrottling.mock.calls).toEqual([[false], [true]]);
1968
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
1969
+ }),
1970
+ ),
1971
+ );
1972
+
1973
+ effectIt.effect("keeps picture-in-picture rendering after a still image completes", () =>
1974
+ withManager((manager) =>
1975
+ Effect.gen(function* () {
1976
+ const { image, capturePage, wc, capture } = yield* setupStill(manager);
1977
+ const pending = Promise.withResolvers<typeof image>();
1978
+ capturePage
1979
+ .mockResolvedValueOnce(image)
1980
+ .mockResolvedValueOnce(makeStillImage("discarded-warmup"))
1981
+ .mockReturnValueOnce(pending.promise);
1982
+ const { pictureInPictureWindow } = makeTestPictureInPictureWindow();
1983
+ browserWindowConstructor.mockImplementation(function () {
1984
+ return pictureInPictureWindow;
1985
+ });
1986
+ yield* manager.openPictureInPicture("tab_still");
1987
+ const still = yield* capture.pipe(Effect.forkChild({ startImmediately: true }));
1988
+ yield* TestClock.adjust(0);
1989
+ expect(capturePage).toHaveBeenCalledTimes(3);
1990
+ expect(writeFile).not.toHaveBeenCalled();
1991
+ pending.resolve(image);
1992
+ yield* Fiber.join(still);
1993
+ expect(writeFile.mock.calls.map((call) => call[1])).toEqual([Buffer.from("fresh-png")]);
1994
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false]]);
1995
+ yield* manager.closePictureInPicture("tab_still");
1996
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
1997
+ }),
1998
+ ),
1999
+ );
2000
+
2001
+ effectIt.effect("keeps a pending still image rendering when recording ends", () =>
2002
+ withManager((manager) =>
2003
+ Effect.gen(function* () {
2004
+ const { image, capturePage, wc, capture } = yield* setupStill(manager);
2005
+ const pending = Promise.withResolvers<typeof image>();
2006
+ capturePage.mockResolvedValueOnce(image).mockReturnValue(pending.promise);
2007
+ yield* manager.startRecording("tab_still");
2008
+ const fiber = yield* capture.pipe(Effect.forkChild({ startImmediately: true }));
2009
+ yield* TestClock.adjust(0);
2010
+ yield* manager.stopRecording("tab_still");
2011
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false]]);
2012
+ pending.resolve(image);
2013
+ yield* Fiber.join(fiber);
2014
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
2015
+ }),
2016
+ ),
2017
+ );
2018
+
2019
+ effectIt.effect("saves available pixels when paint readiness never settles", () =>
2020
+ withManager((manager) =>
2021
+ Effect.gen(function* () {
2022
+ const paint = Promise.withResolvers<void>();
2023
+ const { capturePage, wc, capture } = yield* setupStill(manager, paint.promise);
2024
+ const first = yield* Effect.exit(capture).pipe(
2025
+ Effect.forkChild({ startImmediately: true }),
2026
+ );
2027
+ yield* TestClock.adjust(1_001);
2028
+ expect(Exit.isSuccess(yield* Fiber.join(first))).toBe(true);
2029
+ expect(capturePage).toHaveBeenCalledTimes(2);
2030
+ expect(writeFile.mock.calls.map((call) => call[1])).toEqual([Buffer.from("fresh-png")]);
2031
+ expect(wc.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
2032
+ }),
2033
+ ),
2034
+ );
2035
+
2036
effectIt.effect("keeps every recorded guest unthrottled until its frame capture stops", () =>
2037
withManager((manager) =>
2038
Effect.gen(function* () {
apps/desktop/src/preview/Manager.ts
+97
-2
@@ -387,7 +387,8 @@ interface ManagedListeners {
387
readonly webContents: Electron.WebContents;
388
}
389
390
-type FrameCaptureConsumer = "picture-in-picture" | "recording";
390
+// Each still-image request owns a separate, short-lived rendering lease.
391
+type FrameCaptureConsumer = "picture-in-picture" | "recording" | symbol;
392
393
interface FrameCaptureSession {
394
readonly recordingInputOptions?: RecordingInputOptions;
@@ -2651,7 +2652,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
2652
const [createdAt, millis, image] = yield* Effect.all([
2653
currentIso,
2654
currentMillis,
2654
- capturePageWithRetry(
2655
+ captureStillImage(
2656
{
2657
operation: "captureScreenshot.capturePage",
2658
tabId,
@@ -2699,6 +2700,100 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
2700
};
2701
});
2702
2703
+ const captureStillImage = Effect.fn("PreviewManager.captureStillImage")(function* (
2704
+ errorContext: PreviewOperationContext,
2705
+ tabId: string,
2706
+ wc: Electron.WebContents,
2707
+ ) {
2708
+ const consumer = Symbol("still-image");
2709
+ return yield* Effect.acquireUseRelease(
2710
+ startFrameCapture(tabId, consumer),
2711
+ () =>
2712
+ Effect.gen(function* () {
2713
+ // A DOM mutation can precede its compositor frame even in a shown
2714
+ // window. Keep the original guest rendering through the next paint;
2715
+ // a hidden guest otherwise returns old pixels or never completes.
2716
+ const probe = Effect.fn("PreviewManager.probeStillImagePaint")(function* (
2717
+ send: (
2718
+ method: string,
2719
+ params?: Record<string, unknown>,
2720
+ ) => Effect.Effect<unknown, PreviewOperationError>,
2721
+ ) {
2722
+ // CDP executes against the current document without Electron's
2723
+ // did-stop-loading gate. Isolate scheduling globals from page code.
2724
+ const tree = (yield* send("Page.getFrameTree")) as
2725
+ | { frameTree?: { frame?: { id?: string } } }
2726
+ | undefined;
2727
+ const frameId = tree?.frameTree?.frame?.id;
2728
+ if (typeof frameId !== "string")
2729
+ return yield* new PreviewOperationError({
2730
+ ...errorContext,
2731
+ cause: new Error("The preview paint frame is unavailable."),
2732
+ });
2733
+ const world = (yield* send("Page.createIsolatedWorld", {
2734
+ frameId,
2735
+ worldName: "t3-preview-still-image",
2736
+ })) as { executionContextId?: number } | undefined;
2737
+ if (typeof world?.executionContextId !== "number")
2738
+ return yield* new PreviewOperationError({
2739
+ ...errorContext,
2740
+ cause: new Error("The preview paint context is unavailable."),
2741
+ });
2742
+ const response = (yield* send("Runtime.evaluate", {
2743
+ // Settle and release scheduled work when only timers keep running.
2744
+ // The outer deadline still bounds a frozen renderer.
2745
+ expression: `new Promise(resolve => { let frame; const timer = setTimeout(() => { cancelAnimationFrame(frame); resolve(false); }, ${CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS}); frame = requestAnimationFrame(() => { frame = requestAnimationFrame(() => { clearTimeout(timer); resolve(true); }); }); })`,
2746
+ contextId: world.executionContextId,
2747
+ awaitPromise: true,
2748
+ returnByValue: true,
2749
+ })) as { result?: { value?: unknown }; exceptionDetails?: unknown } | undefined;
2750
+ if (response?.exceptionDetails || response?.result?.value !== true)
2751
+ return yield* new PreviewOperationError({
2752
+ ...errorContext,
2753
+ cause: new Error("The preview paint boundary was not confirmed."),
2754
+ });
2755
+ });
2756
+ const paintReady = Effect.gen(function* () {
2757
+ // Only use an existing owned session; never attach or displace
2758
+ // another debugger to obtain a screenshot.
2759
+ const control = (yield* SynchronizedRef.get(controlSessionsRef)).get(wc.id);
2760
+ if (!control)
2761
+ return yield* new PreviewOperationError({
2762
+ ...errorContext,
2763
+ cause: new Error("The preview paint session is unavailable."),
2764
+ });
2765
+ return yield* probe((method, params) =>
2766
+ attemptPromise(errorContext, () => control.debugger.sendCommand(method, params)),
2767
+ );
2768
+ }).pipe(
2769
+ Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS),
2770
+ Effect.catch((error) =>
2771
+ Effect.logWarning(
2772
+ "Preview paint readiness was not confirmed; capturing available pixels with mutation freshness unconfirmed.",
2773
+ { tabId, webContentsId: wc.id, error },
2774
+ ),
2775
+ ),
2776
+ );
2777
+ // Minimized guests can suspend frames even with throttling disabled.
2778
+ // A native capture can resume them; discard its pixels and read again
2779
+ // after the bounded paint probe.
2780
+ const warmup = capturePageWithRetry(errorContext, tabId, wc).pipe(
2781
+ Effect.catchTags({
2782
+ PreviewOperationError: (error) =>
2783
+ Effect.logWarning("Preview warmup capture failed; continuing with final capture.", {
2784
+ tabId,
2785
+ webContentsId: wc.id,
2786
+ error,
2787
+ }),
2788
+ }),
2789
+ );
2790
+ yield* Effect.all([paintReady, warmup], { concurrency: 2, discard: true });
2791
+ return yield* capturePageWithRetry(errorContext, tabId, wc);
2792
+ }),
2793
+ () => stopFrameCapture(tabId, consumer),
2794
+ );
2795
+ });
2796
+
2797
const capturePreviewFrame = Effect.fn("PreviewManager.capturePreviewFrame")(function* (
2798
tabId: string,
2799
) {