Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(desktop): preserve active element during automation press #5874

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

Fixes #5792


Note

Low Risk Narrow change to preview automation press teardown; uses short injected JS on the main renderer with no auth or data-path changes.

Overview Automation key presses no longer leave the main app UI with the wrong focused control after preview agent input runs.

performAutomationPress already restored the previously focused WebContents; it now also snapshots document.activeElement in that renderer (via window.__t3_restoreFocus) before temporarily focusing the preview guest, and calls .focus() on that element during cleanup alongside previouslyFocused.focus().

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

Note

Preserve active element in renderer during automation key dispatch

During automation key dispatch in Manager.ts, focus temporarily moves to a target WebContents, which previously caused the previously focused renderer to lose its active element. The fix saves the active element into window.__t3_restoreFocus before switching focus and refocuses it afterward via executeJavaScript. These steps are guarded to only run when the previously focused WebContents exists, differs from the current one, and is not destroyed.

📊 Macroscope summarized 025197e. 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: 1a096b32-9986-49d5-8cd2-ade7bc93489a

      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,5 @@
      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* attempt(
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium preview/Manager.ts:3047

      The two new executeJavaScript calls are wrapped with attempt instead of attemptPromise, so their returned Promises are never awaited. The save at automationPress.saveActiveElement can resolve after wc.focus() has already moved focus, and the restore at automationPress.restoreActiveElement can finish after the teardown effect returns — with any rejection escaping Effect.ignore because the unawaited Promise rejects outside the effect's error handling. Use attemptPromise for both calls so they are awaited within the effect.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/desktop/src/preview/Manager.ts around line 3047:
      
      The two new `executeJavaScript` calls are wrapped with `attempt` instead of `attemptPromise`, so their returned Promises are never awaited. The save at `automationPress.saveActiveElement` can resolve after `wc.focus()` has already moved focus, and the restore at `automationPress.restoreActiveElement` can finish after the teardown effect returns — with any rejection escaping `Effect.ignore` because the unawaited Promise rejects outside the effect's error handling. Use `attemptPromise` for both calls so they are awaited within the effect.
    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 025197effed6418c66973fbab27230248eb1b8b6. 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* attempt(
      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);

      Unawaited focus save and restore

      High Severity

      executeJavaScript returns a Promise, but both the active-element save and restore wrap it in attempt (Effect.try) instead of attemptPromise. The Effect finishes as soon as the Promise is created, so wc.focus() can run before __t3_restoreFocus is set, and cleanup can finish before the element is refocused. That races away the caret preservation this change is meant to provide, and rejected scripts can surface as unhandled Promise rejections.

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

      Verdict: Needs human review

      1 blocking correctness issue found. Unresolved review comments (Macroscope Medium, Cursor High) identify a bug where executeJavaScript Promises aren't awaited due to using attempt instead of attemptPromise, causing race conditions that defeat the focus preservation this PR intends to add.

      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 stores one element in a shared window slot and does not wait for the page scripts to finish. #5898 covers the same repair, waits for those scripts, and uses a unique key for concurrent presses.

      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.