Skip to content

fix(agent-staged): focus moves into the review card, and the test stops waiting on jsdom frames - #162

Open
aayushbtw wants to merge 1 commit into
ui-sdkfrom
fix/staged-edits-focus
Open

aayushbtw wants to merge 1 commit into
ui-sdkfrom
fix/staged-edits-focus

Conversation

@aayushbtw

Copy link
Copy Markdown
Owner

Why

The goal is to fix every ui-sdk branch bug we can fix on our side, so the branch is ready to land on main with no known issues. This PR closes two tracker follow-ups on /agent-staged:

  • When the review card opened, focus dropped to body (keyboard) or to the Region trigger (pointer). A keyboard user had to Tab back to Discard or Keep changes.
  • agent-staged.test.tsx sometimes timed out when the full suite ran under load.

What

  • When the card mounts, Suggestion focuses Discard. ui-sdk's popup focus rules (docs/popups.md, Focus) send an alert dialog's initial focus to its first Close part. Discard is the card's safe action (approval.tsx: "a slip lands on the button that does nothing"). Focus still returns to Suggest after Discard or Keep. It now also returns to Suggest when nothing could be applied (unapplied), because Suggest was disabled while it had focus.
  • The test file stubs requestAnimationFrame with a 0ms timer. ui-sdk core's agent.act waits two frames per step (@ui-sdk/core/dist/page/agent/agent.js frames()). One staging runs 16 frames. jsdom's frames are 60Hz wall-clock timers, so each staging spent about 165ms just waiting. Under CPU load, one staging took 1060ms, past React Testing Library's (RTL) 1000ms findBy limit.
  • New assertions: focus lands on Discard when the card opens. A new test refuses all three steps and checks that focus returns to Suggest.

Verification

  • vpr check passed on the head commit. vpr build passed.

  • vpr test passed in all 5 full-suite runs (545 passed, 1 expected fail).

  • Per-test times in the full suite, in ms:

    Suggest Discard Keep Agent keep Confirm Refuse all
    Before (1 run) 467 302 191 194 263 n/a
    After (5 runs) 147-200 69-87 42-50 48-59 63-68 26-30
  • Time from pressing Suggest to the card appearing, measured with a temporary probe:

    Condition Before After
    Idle machine 163-209ms 28-74ms
    36 extra CPU burners 231-1060ms (one over 1000ms) 137-746ms
  • File alone with --repeats=19: eight runs passed. Back to back on the same load, 20 repeats took 24.2s and 30.7s before, and 8.4s and 13.5s after.

  • Real browser: Playwright on a static flagged build (VITE_AGENT_BRIDGE=1), with the _dev guard and prerender patched for that build only. Those patches are not in this PR. Both themes passed:

    • Tab to Suggest and press Enter. Focus lands on Discard with a visible ring. Tab moves to Keep changes.
    • Enter on Discard or on Keep changes returns focus to Suggest.
    • Pointer: focus lands on Discard in 10 of 10 clicks and returns to Suggest each time.
    • No console errors.
    • Screenshots: shots/E-staged/keyboard-review-{light,dark}.png, keyboard-discarded-*, keyboard-kept-*, pointer-review-*, plus report.json.

Tradeoffs

  • The rAF stub applies to this test file only. Other tests keep jsdom's frames. Under heavy CPU starvation the remaining roughly 30ms of real work still slows down, but staging no longer adds a fixed wait for each frame.
  • The first assertion uses waitFor. Its focus comes from a passive effect, and in about 1 of 100 repeats findByRole resolved before that effect ran.
  • ui-sdk's agent opens the real Select popup to set Region, which moves focus into the popup and back to its trigger. That focus movement is upstream behavior. Focusing Discard after staging finishes covers it here.

Tracker

  • "Focus leaves the staged-edits card": fixed. The card focuses Discard on open. Focus returns to Suggest after Discard, Keep, or when nothing was applied.
  • "agent-staged.test.tsx times out": fixed. Cause: ui-sdk's agent waits two frames per step on jsdom's 60Hz wall-clock frames. The test stubs rAF with a 0ms timer, and staging dropped from about 165ms to about 30ms.
  • Possible upstream note: an agent step on a Select moves the user's focus, through the popup's initial focus and then focus return.

…st, and the test no longer waits on jsdom frames

Opening the card focuses Discard, its safe action, so a keyboard user lands
on its answers. Focus also returns to Suggest when nothing could be applied.
The test stubs requestAnimationFrame to a 0ms timer. ui-sdk's agent waits two
frames per step, and jsdom's 60Hz frames stretched a staging past the 1000ms
findBy limit under load.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant