Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(web): preserve composer focus during preview presses #9530

Closed0bkevin(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromrefs/pull/9530/headopened View on GitHub

Problem

preview_press temporarily focuses the preview guest WebContents so Electron can deliver native keyboard input. Restoring the outer renderer afterward did not restore its active DOM element, interrupting users typing in either the current thread or another thread.

Fix

Capture the renderer's meaningful active element around preview_press and restore it after success or failure while that element remains connected.

Restoration is best-effort, avoids scrolling, skips detached elements, and remains scoped to the press path that transfers native focus.

Testing

  • ./node_modules/.bin/vp test run apps/web/src/lib/previewAutomationFocus.test.ts — 4 tests passed
  • ./node_modules/.bin/vp lint --report-unused-disable-directives apps/web/src/lib/previewAutomationFocus.ts apps/web/src/lib/previewAutomationFocus.test.ts
  • ./node_modules/.bin/vp fmt apps/web/src/lib/previewAutomationFocus.ts apps/web/src/lib/previewAutomationFocus.test.ts --check
  • ./node_modules/.bin/vp run -F @t3tools/web typecheck
  • git diff --check
  • Integrated Electron pass: reproduced the unfixed branch moving focus from composer-editor to a host button and dropping the next keystroke; verified the fixed branch keeps composer-editor focused and accepts the next keystroke
  • Captured direct, unedited 1440×900/30fps before and after recordings

Fixes #5792

Implemented with GPT-5.6 Luna and reviewed with GPT-5.6 Sol in T3 Code through the Codex harness.


Note

Medium Risk Changes native focus/keyboard cleanup and IPC contract for press, with nuanced restore vs preserve behavior; mistakes could leave stuck keys or steal composer focus, but scope is limited to preview automation press.

Overview Preview automation press now reports whether native focus was restored or preserved, and surfaces human takeover as an explicit interrupted outcome instead of a silent void.

Contracts add DesktopPreviewAutomationPressResult (completed with restored / preserved, or interrupted). Desktop PreviewManager.automationPress returns that disposition: cleanup runs uninterruptibly (key-up and focus emulation off even on fiber interrupt), native restore is skipped when the app is unfocused or focus moved elsewhere, and human control during press still fails with PreviewAutomationControlInterruptedError without restoring renderer focus. IPC maps that error to { status: "interrupted" } via automationPressResult.

On web, preview_press runs inside new withPreviewAutomationFocus, which serializes focus transactions, tracks trusted user focus intent, and restores the prior DOM element only when the desktop result says focusDisposition === "restored". Interrupted presses throw PreviewAutomationControlInterruptedHostError and serialize to PreviewAutomationControlInterruptedError for automation clients.

Breaking: DesktopPreviewBridge.automation.press and the preview IPC handler return DesktopPreviewAutomationPressResult instead of void.

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

Note

Preserve composer focus during preview automation presses

  • Desktop performAutomationPress now inspects the focused WebContents and BrowserWindow during cleanup and restores the prior renderer only when automation still owns focus or the prior renderer already has it; human-control takeover completes cleanup before re-emitting the interruption.
  • Adds withPreviewAutomationFocus in previewAutomationFocus.ts to serialize concurrent focus transactions, track trusted user focus vs synthetic input via trackFocusIntent, and restore the prior host element only when the document stays focused and no newer transaction supersedes it.
  • Changes the desktop press IPC contract from Promise<void> to Promise<DesktopPreviewAutomationPressResult> in ipc.ts, returning either a completed result with focusDisposition or an interrupted status; the web host converts interrupted results into the new PreviewAutomationControlInterruptedHostError.
  • Behavioral Change: DesktopPreviewBridge.automation.press and PreviewManager.automationPress now return a typed DesktopPreviewAutomationPressFocusDisposition or DesktopPreviewAutomationPressResult instead of void.
Macroscope summarized d293589.
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    2. 0bkevin(GitHub profile, opens in a new tab)commented

      Alternatives considered

      I reviewed the two earlier open approaches and then verified the behavior in the real Electron desktop app:

      • #4118 has the correct core renderer-side focus preservation behavior. I did not take the PR wholesale because it also generalizes the helper and refactors the existing element-picker path, which is outside #5792, and its old branch no longer applies cleanly to current main. This PR keeps that useful try/finally restoration behavior but scopes it only to preview_press and adds current focused regression coverage.
      • #5898 saves a renderer DOM node from the Electron main process through executeJavaScript and a temporary random window property. That adds an unnecessary process boundary and global lifecycle state, and it has no regression tests for the focus behavior.

      The integrated Electron pass also caught and corrected an earlier guard in this PR: native Tab dispatch can move focus to another connected host button, so restoration must not be conditional on document.activeElement becoming body/null. The final implementation always restores the previously focused element when it remains connected, on both success and failure. On the unfixed branch, the next typed character was dropped; on this branch, focus stayed on composer-editor and the character was accepted.

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This changes preview press behavior across the web renderer, desktop native focus handling, and the IPC contract, adding nontrivial focus tracking, concurrency coordination, cancellation cleanup, and interruption reporting. The cross-layer runtime behavior is substantially more involved than a small isolated fix and merits human review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. Live T3 Connect validation

      I also exercised this through the hosted web client against a real connected environment:

      • Opened app.t3.codes and confirmed under Settings → Connections that vm-t3code-dev was Connected via T3 Connect with the relay online.
      • Created a separate live thread and asked the agent to use browser preview automation against https://httpbin.org/forms/post.
      • The remote run opened the page, typed into Customer name and Telephone, clicked Medium and Bacon, scrolled, filled Comments, and verified the final values from a snapshot.
      • While that run executed preview_press(Tab), I kept FOCUS-PROBE-A in the hosted composer. The composer remained the active element and accepted a following -B, ending at FOCUS-PROBE-A-B.

      Scope note: the hosted web right panel explicitly reports that the Browser surface is only available in the desktop app. This validates the T3 Connect/relay path and hosted-composer behavior, but it does not execute the Electron-specific renderer focus wrapper changed by this PR; that path is covered by the separate integrated Electron before/after pass.

      The live browser run also exposed a separate preview-driver quirk: Tab did not advance to the intended form field, so the agent corrected the two fields explicitly before the final snapshot. That is distinct from this PR's host-composer focus restoration and is not hidden by the successful final values.

    5. Cursor Bugbot has reviewed your changes using default 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 6da350829258ae036e0c06512ae95aa502b3455a. Configure [here](https://www.cursor.com/dashboard/bugbot).
    6. This PR is superseded by the two focused draft PRs:

      • #9848 — native desktop focus handling.
      • #9849 — renderer DOM focus restoration.

      Both branches are independently based on main, keep the existing Promise<void> IPC contract, and are pending live Electron evidence. Closing #9530 keeps the original combined cross-layer change out of review while the two focused PRs proceed separately.

Sign in to join this conversation.