Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(desktop): correctly await executeJavaScript for element focus restoration #5894

Closedmohamedmastouri-hue(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/5894/headopened View on GitHub

Fixes #5792b


Note

Low Risk Localized to desktop preview automation cleanup; no auth or data paths, only focus bookkeeping around agent key presses.

Overview Preview key automation (performAutomationPress) now saves and restores the focused DOM element in the previously focused renderer, not only that WebContents’s OS focus.

Before dispatching keys to the guest preview, it runs executeJavaScript on the prior focus target to stash document.activeElement on window.__t3_restoreFocus when it isn’t document.body. In cleanup (after key-up and focus emulation), it refocuses that WebContents and runs JS to call .focus() on the stashed element, then deletes the stash.

Both save and restore use attemptPromise so the Effect pipeline awaits executeJavaScript instead of treating it like a synchronous call—addressing focus restoration races (fixes #5792).

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

Note

Restore focused DOM element after automation key dispatch in PreviewManager

When automation temporarily shifts focus to another WebContents, the previously focused WebContents loses its active DOM element. This fix captures the activeElement into a page-global marker before focus shifts away, then re-focuses it and clears the marker after the key dispatch completes. Both steps use executeJavaScript with attemptPromise and Effect.ignore to avoid surfacing errors if the element is absent.

📊 Macroscope summarized 46bfa4e. 1 file reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted

🗂️ Filtered Issues

No issues evaluated.

    1. 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: fa081f69-fd56-4740-9571-b158e5fd6c23

      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. @@ -3032,3 +3043,3 @@
      30323043 // changing which thread is mounted in the UI. Restore the previous renderer
      30333044 // after dispatch so automation never leaves the app's input focus behind.
      30343045 yield* Effect.gen(function* () {
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High preview/Manager.ts:3045

      The focus-save/restore logic uses the hardcoded global window.__t3_restoreFocus to stash the previously focused element. If the page already defines that property, the save script unconditionally overwrites it and the cleanup script permanently deletes it, destroying the page's own value. Consider using a Symbol key or saving and restoring any pre-existing value so automation never clobbers a page-owned property.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/desktop/src/preview/Manager.ts around line 3045:
      
      The focus-save/restore logic uses the hardcoded global `window.__t3_restoreFocus` to stash the previously focused element. If the page already defines that property, the save script unconditionally overwrites it and the cleanup script permanently deletes it, destroying the page's own value. Consider using a `Symbol` key or saving and restoring any pre-existing value so automation never clobbers a page-owned property.
    4. 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 46bfa4e52e7b7e451a52266cccf92dab422ed95e. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -3032,3 +3043,15 @@
      30323043 // changing which thread is mounted in the UI. Restore the previous renderer
      30333044 // after dispatch so automation never leaves the app's input focus behind.
      30343045 yield* Effect.gen(function* () {
      3046+ if (previouslyFocused && previouslyFocused.id !== wc.id && !previouslyFocused.isDestroyed()) {
      3047+ yield* attemptPromise(
      3048+ {
      3049+ operation: "automationPress.saveActiveElement",
      3050+ tabId,
      3051+ webContentsId: previouslyFocused.id,
      3052+ },
      3053+ () =>
      3054+ previouslyFocused.executeJavaScript(
      3055+ `if (document.activeElement && document.activeElement !== document.body) { window.__t3_restoreFocus = document.activeElement; } else { delete window.__t3_restoreFocus; }`,
      3056+ ),
      3057+ ).pipe(Effect.ignore);

      Shared focus global races

      Medium Severity

      window.__t3_restoreFocus is a single shared slot on the previously focused renderer, while control-session locking is only per preview WebContents. Concurrent automationPress calls on different tabs can both capture the same UI renderer, so one press can overwrite or delete another’s saved element before restore runs, leaving composer focus unrestored.

      Additional Locations (1)
      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 46bfa4e52e7b7e451a52266cccf92dab422ed95e. Configure [here](https://www.cursor.com/dashboard/bugbot).
    5. Approvability

      Verdict: Needs human review

      1 blocking correctness issue found. This PR adds new runtime behavior for element focus management using a hardcoded global property. An unresolved High-severity finding identifies that the global window.__t3_restoreFocus could clobber page-owned properties, and a separate concern raises potential race conditions with concurrent calls. Human review is needed to assess these design concerns.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    6. 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 focus restoration variant waits for the page scripts but still uses one shared window slot. #5898 also uses a unique per-operation key, so this branch has no separate repair to keep.

      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.