fix(preview): bound automation waits and screenshot captures #4685
Mergedjuliusmarmingemerged 6 commits into
main fromrefs/pull/4685/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- apps/web/src/components/preview/previewAutomationHostBudget.ts lines 6 to 15Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,21 @@1+/**2+ * Reserve for delivering the host's response back to the broker once a wait3+ * gives up. Without it a host wait that runs the full request budget always4+ * loses the race to the broker's own deadline, so the agent sees an opaque5+ * "timed out" instead of the specific failure the host was about to report.6+ */7+export const PREVIEW_HOST_RESPONSE_MARGIN_MS = 1_500;8+9+/** Smallest useful wait; below this a slow first poll would fail instantly. */10+const MIN_HOST_WAIT_MS = 250;11+12+/**13+ * Budget a host-side wait so it expires before the broker's deadline for the14+ * same request.15+ */16+export function resolveHostWaitBudgetMs(requestTimeoutMs: number): number {17+ if (!Number.isFinite(requestTimeoutMs) || requestTimeoutMs <= 0) {18+ return MIN_HOST_WAIT_MS;19+ }20+ return Math.max(MIN_HOST_WAIT_MS, requestTimeoutMs - PREVIEW_HOST_RESPONSE_MARGIN_MS);21+}
- apps/web/src/components/preview/previewAutomationHostBudget.ts line 20OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,20 @@1+/**2+ * Reserve for delivering the host's response back to the broker once a wait3+ * gives up. Without it a host wait that runs the full request budget always4+ * loses the race to the broker's own deadline, so the agent sees an opaque5+ * "timed out" instead of the specific failure the host was about to report.6+ */7+export const PREVIEW_HOST_RESPONSE_MARGIN_MS = 1_500;8+9+/** Smallest useful wait; below this a slow first poll would fail instantly. */10+const MIN_HOST_WAIT_MS = 250;11+12+/**13+ * Budget a host-side wait so it expires before the broker's deadline for the14+ * same request.15+ */16+export function resolveHostWaitBudgetMs(requestTimeoutMs: number): number {17+ if (!Number.isFinite(requestTimeoutMs) || requestTimeoutMs <= 0) {18+ return MIN_HOST_WAIT_MS;19+ }20+ return Math.max(MIN_HOST_WAIT_MS, requestTimeoutMs - PREVIEW_HOST_RESPONSE_MARGIN_MS);apps/desktop/src/preview/Manager.ts line 684Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -510,3 +519,14 @@510519 try: evaluate,511520 catch: (cause) => new PreviewOperationError({ ...errorContext, cause }),512521 });522+ /**523+ * capturePage for one-shot captures (snapshots, screenshots), retrying the524+ * transient compositor warm-up failure instead of surfacing it to callers.525+ */526+ const capturePageWithRetry = (errorContext: PreviewOperationContext, wc: Electron.WebContents) =>527+ attemptPromise(errorContext, () => wc.capturePage()).pipe(528+ Effect.retry({529+ times: CAPTURE_PAGE_RETRY_ATTEMPTS - 1,530+ schedule: Schedule.spaced(CAPTURE_PAGE_RETRY_DELAY_MS),531+ }),532+ );
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge changed the title
- apps/web/src/components/preview/previewAutomationHostBudget.ts line 35Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +16,21 @@16+17+/** Both readiness probes and polling delays share the request's host deadline. */18+export async function waitForHostReadiness(19+ deadlineMs: number,20+ isReady: () => Promise<boolean>,21+): Promise<boolean> {22+ while (Date.now() < deadlineMs) {23+ let timeout: ReturnType<typeof setTimeout> | undefined;24+ let ready: boolean | null;25+ try {26+ ready = await Promise.race([27+ isReady(),28+ new Promise<null>((resolve) => {29+ timeout = setTimeout(() => resolve(null), Math.max(0, deadlineMs - Date.now()));30+ }),31+ ]);32+ } finally {33+ clearTimeout(timeout);34+ }35+ if (ready === null || Date.now() >= deadlineMs) return false;36+ if (ready) return true;
juliusmarminge changed the title
- apps/desktop/src/preview/Manager.ts lines 670 to 672OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -641,0 +652,21 @@652+ tabId: string,653+ wc: Electron.WebContents,654+ ) {655+ const requireCurrentGuest = Effect.gen(function* () {656+ const tabs = yield* SynchronizedRef.get(tabsRef);657+ if (wc.isDestroyed() || tabs.get(tabId)?.webContentsId !== wc.id) {658+ return yield* new PreviewWebContentsNotFoundError({ tabId, webContentsId: wc.id });659+ }660+ });661+ const capture = Effect.gen(function* () {662+ // Check after the retry delay, and again before accepting its result.663+ yield* requireCurrentGuest;664+ const image = yield* Effect.tryPromise({665+ // An abort-signal parameter makes a stalled promise interruptible.666+ try: (_signal) => wc.capturePage(),667+ catch: (cause) => new PreviewOperationError({ ...errorContext, cause }),668+ }).pipe(669+ Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS),670+ Effect.catchTag("TimeoutError", (cause) =>671+ Effect.fail(new PreviewOperationError({ ...errorContext, cause })),672+ ),
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge merged thisde1b798
juliusmarminge closed this
Sign in to join this conversation.



Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
589584f0-2aba-438e-80ea-e7f1c98be710You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.