Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(desktop): keep preview automation when leaving a thread #7200

Closedmaslinedwin(GitHub profile, opens in a new tab)wants to merge 6 commits intomain fromrefs/pull/7200/headopened View on GitHub

Problem

Leaving a thread with an embedded browser preview can drop that tab's CDP control session. After returning, the old tab still exists, but preview_snapshot fails with UnknownVizError or hangs for ~76–320s. Reusing the tab does not recover; a fresh tab does. Background-agent browser work is poisoned even though the backend stays healthy.

Fix

Do not treat a hidden thread as a closed tab.

  • Drop a control session only after Chromium has already detached it. The next automation call (snapshot / click / evaluate) restores the session through the existing restoreControlSession path.
  • Retry one snapshot after reattach when capture fails with UnknownVizError.
  • Bound capturePage at 8s so a poisoned compositor fails fast instead of hanging for minutes.
  • Disable backgroundThrottling on preview guests so offscreen/background-thread tabs keep a usable compositor.

Tests

apps/desktop/src/preview/Manager.test.ts

  • Re-registering a live webview (thread visible again) does not detach the control session, and snapshot still runs.
  • A hidden-tab debugger detach restores on the next snapshot.
  • UnknownVizError retries after reattach.
  • A hung capturePage fails quickly with a recoverable UnknownVizError instead of waiting minutes.

Residual risk

If Electron destroys the guest WebContents entirely when the panel is hidden, the next snapshot fails immediately with PreviewWebContentsNotFoundError until the webview remounts and re-registers. This change does not persist tabs across app quit.

Fixes #6355

Made with Grok.


Note

Medium Risk Touches CDP session lifecycle, snapshot retries, and Electron webPreferences for all preview guests; regressions could affect automation reliability or guest resource usage, but behavior is heavily tested.

Overview Keeps preview browser automation alive when a thread (and its embedded preview) is hidden, instead of leaving preview_snapshot stuck on UnknownVizError or multi-minute hangs.

The CDP control session now listens for Chromium debugger detach, forgets only that session (with an expected guard so a stale finalizer cannot tear down a replacement), and reattaches when automation runs again. ensureControlSession treats a map entry without an attached debugger as stale and recreates the session. detachControlSession closes the scope under the session lock so a new attach cannot race the old debugger teardown.

Snapshots wrap capturePage in an 8s timeout, surface failures as PreviewCaptureUnavailableError, retry once after detaching the control session, and drop retryable capture misses from the action timeline instead of recording a failed snapshot row. Automation actions call restoreControlSession before acquiring the session. requireWebContents also rejects destroyed guests.

Preview webviews set backgroundThrottling: false in DesktopWindow (and docs in WebviewPreferences) so offscreen guests keep a usable compositor. Manager.test.ts adds broad coverage for re-register, detach/reattach, viz retry, timeout, and session races.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit d4fc245707953fb2d9da70beb8093ae70a4fd3cb. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Fix preview automation control sessions to persist when leaving a thread

  • Stale debugger detach events no longer tear down a replacement control session; detachControlSession now operates atomically and only removes the session it was given.
  • ensureControlSession re-establishes the CDP session automatically when Chromium detaches the debugger, avoiding a state where automation silently stops working.
  • automationSnapshot retries once after a compositor capture miss by detaching the stale session and re-snapshotting against the current webview.
  • Snapshot capture is now bounded by an 8s timeout (AUTOMATION_CAPTURE_TIMEOUT_MS); failures surface as the new PreviewCaptureUnavailableError and are dropped from the action timeline rather than recorded as failures.
  • Preview webviews now run with backgroundThrottling=false to keep the compositor and CDP responsive when the webview is hidden.
Macroscope summarized d4fc245.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 200d78ec-f392-4dbf-964a-dc32ab309f1c

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. Effect service conventions review of the preview manager changes. One clear violation of the error-modeling conventions (fabricated cause used as a control-flow signal), plus the retry predicate that depends on it.

      Posted via Macroscope — Effect Service Conventions
    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR modifies Chrome DevTools Protocol session lifecycle management with retry logic, timeout handling, and detach recovery. The changes affect how automation sessions survive tab hiding and recover from failures - non-trivial runtime behavior that warrants human review.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot). Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 1a250543a47543071bb610360c55aa094ba886fa. Configure [here](https://www.cursor.com/dashboard/bugbot).
    6. Waiting for this to be merged, facing this issue a lot.

    7. Note

      🤖 Claude Fable 5 in Claude Code responding on behalf of Kiri110K

      Thanks for digging into this — the reattach flow, the typed PreviewCaptureUnavailableError, and the fixes from the earlier review round (retry re-entering withControlSession, re-reading tabsRef, the controller reset) are clear improvements. I reviewed the current head (d4fc2457) against latest main and found two remaining blockers plus a few smaller races.

      1. The 8s timeout doesn't cancel the native capture, and the retry starts a second one. Effect.timeout interrupts the fiber, but Electron's capturePage() takes no abort signal, so the native capture keeps running after the timeout — in our logs a capture kept going for ~120s after the broker had already given up. The retry in captureAutomationPage then starts a second capture on the same WebContents, so two native captures can be live at once — the same corrupted-compositor state this PR is trying to escape. Note this also limits the mutex fix from c98d4b0c: the retry correctly acquires the replacement session's semaphore, but the timed-out capture keeps running outside any permit.

      2. Permanent backgroundThrottling: false reverts #7445. #7445 (merged after this branch's base) deliberately keeps hidden previews throttled and only lifts throttling during recording/PiP, because a single unthrottled WebContents keeps the whole window compositing (their measurement: 240 frames over 4s vs 0). Setting backgroundThrottling: false for every preview guest in DesktopWindow.ts semantically undoes that fix. Rebasing on current main and reusing its reference-counted throttling would resolve this.

      Smaller notes:

      • Two 8s attempts don't fit inside the broker's 15s budget, so the agent still gets a generic timeout before PreviewCaptureUnavailableError can surface. #8272 takes the same "inner budget below the broker deadline" approach for the web hosts; the desktop capture path needs the equivalent.
      • restoreControlSession bails out when it sees the guest was replaced, but withControlSession then still calls ensureControlSession on the stale WebContents.

      Happy to help with any of this if useful.

    8. Note

      🤖 GPT-5.6 Sol responding on behalf of Theo

      We're closing this PR as we clean up the T3 Code backlog. Thank you for taking the time to put this together.

      This disables background throttling for every attached hidden preview guest, which can keep hidden pages running and increase CPU use after a thread change. Preview automation needs an explicit active lease before this approach is safe.

      If you believe we closed this in error, please reopen the PR and leave a comment explaining what we missed.

Sign in to join this conversation.