From 57ae612f0e7ddd0466e621ad1f75bd368fea6823 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 28 Jul 2026 00:06:30 +0200 Subject: [PATCH] Clamp host wait below every broker budget and pin capture retries to the guest - Reserve a proportional margin for short request budgets so the host wait never exceeds the broker deadline it is meant to beat - Stop capture retries once the tab points at a different web contents Co-Authored-By: Claude Fable 5 --- apps/desktop/src/preview/Manager.test.ts | 30 ++++++++++++++ apps/desktop/src/preview/Manager.ts | 21 +++++++++- .../previewAutomationHostBudget.test.ts | 39 ++++++++++++------- .../preview/previewAutomationHostBudget.ts | 20 +++++++--- 4 files changed, 87 insertions(+), 23 deletions(-) diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 0c8fd50e80..c106d230c3 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -786,6 +786,36 @@ describe("PreviewManager", () => { ), ); + effectIt.effect("stops capture retries when the tab swaps to another guest", () => + withManager((manager) => + Effect.gen(function* () { + const capturePage = vi.fn(async () => ({ + toPNG: () => Buffer.from("png"), + toJPEG: () => Buffer.from("jpeg"), + getSize: () => ({ width: 100, height: 80 }), + })); + fromId.mockReturnValue(makeTestPreviewWebContents(capturePage, 42)); + yield* manager.createTab("tab_1"); + yield* manager.registerWebview("tab_1", 42); + + // Fail persistently, then swap the tab onto a different web contents: + // retrying against the detached guest would capture the wrong page. + capturePage.mockClear(); + capturePage.mockRejectedValue(new Error("UnknownVizError")); + const fiber = yield* Effect.exit(manager.captureScreenshot("tab_1")).pipe( + Effect.forkChild({ startImmediately: true }), + ); + fromId.mockReturnValue(makeTestPreviewWebContents(capturePage, 43)); + yield* manager.registerWebview("tab_1", 43); + yield* TestClock.adjust(1_000); + const exit = yield* Fiber.join(fiber); + + expect(Exit.isFailure(exit)).toBe(true); + expect(capturePage).toHaveBeenCalledTimes(1); + }), + ), + ); + effectIt.effect("captures hidden preview recordings independently for concurrent tabs", () => withManager((manager) => Effect.gen(function* () { diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index 9cc3d4fb2a..b61df08a45 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -522,14 +522,29 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function /** * capturePage for one-shot captures (snapshots, screenshots), retrying the * transient compositor warm-up failure instead of surfacing it to callers. + * + * Retries stop as soon as the tab no longer points at this guest, so a + * webview swap during the retry window surfaces the original failure instead + * of capturing from a detached web contents. */ - const capturePageWithRetry = (errorContext: PreviewOperationContext, wc: Electron.WebContents) => - attemptPromise(errorContext, () => wc.capturePage()).pipe( + const capturePageWithRetry = ( + errorContext: PreviewOperationContext, + tabId: string, + wc: Electron.WebContents, + ) => { + const guestIsCurrent = Effect.gen(function* () { + if (wc.isDestroyed()) return false; + const tabs = yield* SynchronizedRef.get(tabsRef); + return tabs.get(tabId)?.webContentsId === wc.id; + }); + return attemptPromise(errorContext, () => wc.capturePage()).pipe( Effect.retry({ times: CAPTURE_PAGE_RETRY_ATTEMPTS - 1, schedule: Schedule.spaced(CAPTURE_PAGE_RETRY_DELAY_MS), + while: () => guestIsCurrent, }), ); + }; const currentIso = DateTime.now.pipe(Effect.map(DateTime.formatIso)); const currentMillis = Clock.currentTimeMillis; const encodeJson = (errorContext: PreviewOperationContext, value: unknown) => @@ -1973,6 +1988,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function tabId, webContentsId: wc.id, }, + tabId, wc, ), ]); @@ -2647,6 +2663,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function tabId, webContentsId: wc.id, }, + tabId, wc, ), Ref.get(diagnosticsRef), diff --git a/apps/web/src/components/preview/previewAutomationHostBudget.test.ts b/apps/web/src/components/preview/previewAutomationHostBudget.test.ts index bd68954157..aca68676a8 100644 --- a/apps/web/src/components/preview/previewAutomationHostBudget.test.ts +++ b/apps/web/src/components/preview/previewAutomationHostBudget.test.ts @@ -6,24 +6,33 @@ import { } from "./previewAutomationHostBudget"; describe("resolveHostWaitBudgetMs", () => { - it("expires before the broker deadline so the host failure wins the race", () => { - const requestTimeoutMs = 15_000; - const budget = resolveHostWaitBudgetMs(requestTimeoutMs); - - expect(budget).toBeLessThan(requestTimeoutMs); - expect(requestTimeoutMs - budget).toBe(PREVIEW_HOST_RESPONSE_MARGIN_MS); + it("always expires before the broker deadline, at every request budget", () => { + // The whole point of the helper: the host must lose no race, at any scale. + for (const requestTimeoutMs of [1, 2, 10, 100, 249, 250, 999, 1_500, 3_000, 15_000, 60_000]) { + expect(resolveHostWaitBudgetMs(requestTimeoutMs)).toBeLessThan(requestTimeoutMs); + } }); - it("keeps a usable wait for short request budgets", () => { - expect(resolveHostWaitBudgetMs(1_000)).toBeGreaterThan(0); - expect(resolveHostWaitBudgetMs(1_000)).toBeLessThan(1_000); - expect(resolveHostWaitBudgetMs(10)).toBeGreaterThan(0); - expect(resolveHostWaitBudgetMs(10)).toBeLessThan(PREVIEW_HOST_RESPONSE_MARGIN_MS); + it("reserves the full response margin once the request budget allows it", () => { + expect(resolveHostWaitBudgetMs(15_000)).toBe(15_000 - PREVIEW_HOST_RESPONSE_MARGIN_MS); + expect(resolveHostWaitBudgetMs(60_000)).toBe(60_000 - PREVIEW_HOST_RESPONSE_MARGIN_MS); }); - it("falls back to a positive budget for invalid input", () => { - expect(resolveHostWaitBudgetMs(0)).toBeGreaterThan(0); - expect(resolveHostWaitBudgetMs(-5)).toBeGreaterThan(0); - expect(resolveHostWaitBudgetMs(Number.NaN)).toBeGreaterThan(0); + it("keeps most of a short request budget instead of collapsing it", () => { + expect(resolveHostWaitBudgetMs(1_000)).toBe(800); + expect(resolveHostWaitBudgetMs(100)).toBe(80); + }); + + it("grows monotonically with the request budget", () => { + const budgets = [1, 10, 100, 1_000, 3_000, 15_000].map(resolveHostWaitBudgetMs); + for (let index = 1; index < budgets.length; index += 1) { + expect(budgets[index]!).toBeGreaterThanOrEqual(budgets[index - 1]!); + } + }); + + it("returns a non-negative budget for invalid input", () => { + for (const invalid of [0, -5, Number.NaN, Number.POSITIVE_INFINITY]) { + expect(resolveHostWaitBudgetMs(invalid)).toBeGreaterThanOrEqual(0); + } }); }); diff --git a/apps/web/src/components/preview/previewAutomationHostBudget.ts b/apps/web/src/components/preview/previewAutomationHostBudget.ts index d5e8e301d2..4266221d55 100644 --- a/apps/web/src/components/preview/previewAutomationHostBudget.ts +++ b/apps/web/src/components/preview/previewAutomationHostBudget.ts @@ -6,16 +6,24 @@ */ export const PREVIEW_HOST_RESPONSE_MARGIN_MS = 1_500; -/** Smallest useful wait; below this a slow first poll would fail instantly. */ -const MIN_HOST_WAIT_MS = 250; +/** + * Share of the request budget reserved when the request is too short for the + * full margin, so the host still expires first rather than being clamped past + * the broker's deadline. + */ +const HOST_RESPONSE_MARGIN_FRACTION = 0.2; /** - * Budget a host-side wait so it expires before the broker's deadline for the - * same request. + * Budget a host-side wait so it always expires before the broker's deadline + * for the same request. */ export function resolveHostWaitBudgetMs(requestTimeoutMs: number): number { if (!Number.isFinite(requestTimeoutMs) || requestTimeoutMs <= 0) { - return MIN_HOST_WAIT_MS; + return 0; } - return Math.max(MIN_HOST_WAIT_MS, requestTimeoutMs - PREVIEW_HOST_RESPONSE_MARGIN_MS); + const reservedMs = Math.min( + PREVIEW_HOST_RESPONSE_MARGIN_MS, + Math.ceil(requestTimeoutMs * HOST_RESPONSE_MARGIN_FRACTION), + ); + return Math.max(0, requestTimeoutMs - reservedMs); }