Skip to content

Read a keyboard reflection again on a focus move only where its rows asked where the user is - #5324

Merged
max-sixty merged 1 commit into
mainfrom
keys-follow-focus
Nov 4, 2026
Merged

max-sixty merged 1 commit into
mainfrom
keys-follow-focus

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Each scoped element's aria-keyshortcuts and each claimed button's aria-disabled is a signals effect over what its rows read. Every one of them also subscribed to focus.js's revision outright, so every move of focus (a Tab, a click, an Escape that hands the user back) read every scoped element and every named button on the page again.

focus.js gains followingFocus(read). Inside it, asking focused subscribes the reading to where the user is. The keyboard reflections run inside it, so a move reads again only those whose rows asked. Counted on the asks page of leaf-dev scaling at 512 sections:

readings main, elements main, buttons this, elements this, buttons
per Tab 4,123 523 9 1
per option click 5,170 527 1,068 7
per letter in the comment field 3 1 3 1

In profiles of 20 Tabs on main at 512 sections, these readings took 61–127 ms of each Tab. Two keyboard costs stay page-sized and are left to separate changes: scopesWithin climbs from every scoped element, and every pass gathers every button.

This serves the rule in root AGENTS.md, "Responsiveness", as adopted for the large-page work: "An input's work depends on what it changes and on what the window shows, not on how much the page holds."

It relies on the rule the module header already states: state a row reads is a signal. A row whose availability rested on a plain variable a focus listener keeps, without asking focused in the same reading, would now go stale on a move. To look for one, a debug check compared every scoped element's aria-keyshortcuts, aria-disabled and disabled after each input with what a fresh full reflection (paintKeys) writes, on every example page (24 pages, about 30 inputs each: Tab, Shift+Tab, walks, the thread walk, the Threads panel, Page Map, Go-to, the comment field, digits and Escape) and on a journey through margin markers and their thread cards on 9 pages. It found no difference. A control run that removed focus tracking altogether reported stale thread card and margin cluster shortcuts within the first few inputs. A Codex review audited every reflection's callers for native focus reads, listener-kept variables, deferred getters and untracked calls, and found none; button availability still follows the focus revision, as on main.

A new runtime test checks that a focus move re-reads only the keys and buttons that ask where the user is (12 reads on main). Runtime tests pass 778/778; of the keyboard browser tests, the same two test_commenting.py tests fail on main.

This was written by Claude Code on behalf of max-sixty

…asked where the user is

Each scoped element's `aria-keyshortcuts` and each claimed button's
`aria-disabled` is a signals effect over what its rows read. Every one of them
also subscribed to focus.js's revision outright. So every move of focus, a
Tab, a click or an Escape that hands the user back, read every scoped element
and every named button on the page again.

focus.js gains `followingFocus(read)`. Inside it, asking `focused` subscribes
the reading to where the user is. The keyboard reflections run inside it, so a
move reads again only those whose rows asked. Counted on the `asks` page at
512 sections:

| readings | main, elements | main, buttons | this, elements | this, buttons |
|---|---|---|---|---|
| per Tab | 4,123 | 523 | 9 | 1 |
| per option click | 5,170 | 527 | 1,068 | 7 |
| per letter in the comment field | 3 | 1 | 3 | 1 |

In profiles of 20 Tabs on main at 512 sections, these readings took 61–127 ms of
each Tab, about an eighth of it. Most of the rest is other work. Two keyboard
parts of it stay page-sized: `scopesWithin` climbs from every scoped element,
and every pass gathers every button. Separate changes take those on.

This relies on the rule the module header already states: state a row reads
is a signal. A row whose availability rests on a plain variable a focus
listener keeps, without asking `focused` in the same reading, now goes stale
on a move until something marks it. Under the old revision, every move re-read
every row and hid such a row. To look for one, a debug check compared every
scoped element's `aria-keyshortcuts`, `aria-disabled` and `disabled` after
each input with what a fresh full reflection (`paintKeys`) writes. It ran
every example page (24 pages, about 30 inputs each: Tab, Shift+Tab, walks, the
thread walk, the Threads panel, Page Map, Go-to, the comment field, digits and
Escape), and a journey through margin markers and their thread cards on 9
pages. It found no difference. When this was first written, a control run on
the earlier base removed focus tracking altogether. The same check then
reported stale thread card and margin cluster shortcuts within the first few
inputs.

One case loses an accidental repair. A button outside a scope that is then
removed kept the scope's availability until the next pass that re-gathered
the page's buttons. On main, every focus move ran such a pass. Now it waits
for any other button change. The incremental button work that would close
this belongs to a later change.
@max-sixty
max-sixty merged commit 22c53d4 into main Nov 4, 2026
4 checks passed
@max-sixty
max-sixty deleted the keys-follow-focus branch November 4, 2026 09:17
max-sixty added a commit that referenced this pull request Nov 12, 2026
…each margin row against only the rows beside it, and name a revealed code block's Copy key (#5866)

Sending a comment on the generated asks page at 1,024 sections now draws
the message in a median 185 ms in Chromium, down from 354 ms, and shows
the server's reading at 298 ms, down from 504 ms. Resolving a thread
there shows the server's reading at 194 ms, down from 470 ms. Both are
still above the 100 ms root `AGENTS.md` ("Responsiveness") asks for;
what remains is listed at the end. In Firefox, the send's main-thread
time at 1,024 sections falls from 639 ms to 487 ms, and Resolve's from
601 ms to 217 ms. A code block that a disclosure or a tab reveals now
names its Copy key, Enter, to assistive technology as soon as it shows;
before, it named none until something repainted every key.

## Why

Profiling the send at 1,024 sections in Chromium found two costs in the
frame after the press that grew with the page.

- **Every key on the page was reflected again.** Opening the thread card
called `paintKeys()` (`previewChanged`, margin-projection.js), which
marks every element that declares keys. A send re-read 7,196 elements'
`aria-keyshortcuts` and button availability, about 110 ms of a profiled
frame. Closing the card by Escape and opening it by its margin row did
the same, through the same call and through `closePreview`, and a
Resolve did it twice, before and after its landing (`landGesture`,
thread/folding.js).
- **Each margin row was seated against every row seated before it.**
`seatRow` copied the whole list of seated footprints into its own arrays
for each pin, and `packRow` filtered and sorted it for each rail row. A
rows-only pass (#5817) still seats every row, so each of the send's two
passes cost 27–30 ms at 1,024 sections, growing with the square of the
rows.

## Changes

**Key reflection follows what changed.** Each element's key reflection
is a tracked effect that reads again when a signal it read moves
(keyboard/scopes.js). The card already re-reads the keys of what it
holds as it shows and as it hides (`paintKeysWithin(preview)`,
thread-preview.js, since #4407), a margin row's reflection that asks
whether the user is on the card's entry reads the card's revision signal
(`onOpener`), and a settlement in flight already shows in the thread's
state, which the reflections read (`threadReading`, thread-card.js). The
four page-wide `paintKeys()` calls date from before the card painted its
own keys (#4407) and before a reflection followed focus only where its
rows asked (#5324). They are gone. The stale-keys sweep, fixed here so
that it can fail, passes on every published example, and an independent
probe of the card and Resolve routes found no stale key (both under
Checked). Counted on the 1,024-section page:

| Gesture | Elements reflected, main | This branch |
| --- | ---: | ---: |
| Send | 7,196 | 18 |
| Escape out of the card | 7,200 | 3 |
| Reopen the card from its margin row | 7,200 | 6 |
| Resolve | 7,204 twice | 17 and 14 |

**Seated footprints are indexed by their tops.** `Footprints`
(margin-placement.js) holds the footprints the shared seating order has
placed so far, sorted by top, and answers the two questions the seating
folds ask: the footprint a rectangle overlaps (`hit`), and the
footprints in order of their tops from the first that can reach below a
line (`from`, for `packRow`'s push down). Each question reads only the
footprints level with the rectangle it names, so a pass costs the rows
times the logarithm of their number. Every row stands where it stood. A
slide moves a seat past the first footprint it meets, and with a gap
between seats, which of two overlapping footprints it meets first
changes where the seat lands, so `hit` answers the overlapping footprint
seated first, as main's scan in seating order did. `pinSpot` is no
longer exported, since only a test used it; that assertion now goes
through `seatRow`. On the 1,024-section page each rows-only pass of a
send now takes 7–10 ms, from 27–30 ms (timed in the page, unprofiled).

**The stale-keys sweep repaints every element's keys again.** Its forced
repaint called `commandsChanged(document.body)`, which since #5369 marks
only the element it names, and `body` declares no commands, so the
repaint read no element's keys and the sweep could not fail. The check
API now exports `paintKeys`, and the sweep calls it.

**A code block a disclosure or a tab reveals names its Copy key.** The
repaired sweep failed on four examples, on this branch and on main
alike: dynamic-widgets, file-editor, motion and feature-gallery. A
block's Copy row was live only once Web Awesome had drawn the copy
control's button (`copyTrigger`), and `projectCommandScope` reads the
row at once, in the task that makes the control, before the control's
first update draws the button. Sampled frame by frame on motion after
opening its disclosure, the row was read once, with no button yet; the
button drew a frame later, and nothing read the row again, so the
block's `aria-keyshortcuts` lacked Enter until something repainted every
key. Blocks shown at load already named Enter once the page settled,
since the sweep's reading on arrival found none stale; a block prepared
later, as a disclosure or tab reveals it, stayed stale. `code.copy`
(code-copy.js) now works as `heading.link.copy` (heading-links.js)
already did for a control that hasn't drawn yet: its row has no
condition, and its run waits for the control's definition and first
update before clicking. The row so never depends on when the control
draws, and nothing needs reading again.

**The bench times the frame that paints.** `leaf-dev bench`'s paint pass
timed a goal at the first task after the frame that showed it. When the
server's answer arrived during a long input, its task ran before that
one, so the send's `painted` included applying the server's reading. A
profiled run at 1,024 sections read `painted` and `shown` both as 382
ms, while the frame showing the message had been laid out at about 245
ms. `painted.js` now times the frame with a size observation made as the
frame renders, which the browser delivers after the frame's animation
callbacks and layout, just before its paint. Both arms of a run use the
same measurement, so main's numbers below are measured this way too. The
review measured the step this puts into the daily chart: with the same
runtime, old against new `painted.js`, at 64 sections over 4 rounds, a
send's `shown` reads a median 101 → 90 ms, a Resolve's 40 → 37.5 ms and
a click's 31 → 30 ms.

## Measurements

Chromium, `uv run leaf-dev bench asks --sizes 256,1024 --input comment
--input resolve --rounds 3 --base 3b9ff64`: both arms interleaved in
one run, this branch against its merge base with main, load average 7.8
at the start and 9.2 at the end. Each cell is the median of 3 rounds,
with the range in brackets. Paint is from the input to the frame that
draws its own result; shown is to the frame presenting the server's
reading of it; CPU is the input's main-thread time.

| Input | Sections | Paint, main → branch (ms) | Shown, main → branch
(ms) | CPU, main → branch (ms) |
| --- | ---: | --- | --- | --- |
| Send | 8 | 57 [54–88] → 55 [54–89] | 87 [86–88] → 89 [88–89] | 75
[75–84] → 85 [80–85] |
| Send | 256 | 112 [111–113] → 81 [74–84] | 155 [154–158] → 124
[122–124] | 152 [146–159] → 118 [115–124] |
| Send | 1,024 | 354 [310–362] → 185 [167–323] | 504 [430–507] → 298
[283–323] | 451 [447–458] → 291 [291–326] |
| Resolve | 8 | 20 [19–25] → 22 [21–22] | 33 [30–45] → 32 [32–34] | 38
[37–46] → 34 [32–35] |
| Resolve | 256 | 124 [119–126] → 57 [56–60] | 124 [119–126] → 57
[56–60] | 134 [124–135] → 67 [65–73] |
| Resolve | 1,024 | 242 [226–249] → 108 [108–110] | 470 [438–481] → 194
[194–198] | 431 [426–470] → 210 [201–214] |

The 8-section rows measure the runtime's fixed work, which this change
leaves alone, and their differences are within the spread of the rounds.

Firefox, the same command with `--browser firefox`, which reads only
main-thread CPU there, run straight after; load average 8.8 at the start
and 16.8 at the end:

| Input | Sections | CPU, main → branch (ms) |
| --- | ---: | --- |
| Send | 8 | 75 [70–94] → 82 [74–90] |
| Send | 256 | 210 [190–245] → 172 [165–268] |
| Send | 1,024 | 639 [591–673] → 487 [433–489] |
| Resolve | 8 | 46 [39–46] → 41 [40–54] |
| Resolve | 256 | 206 [189–221] → 109 [99–119] |
| Resolve | 1,024 | 601 [555–603] → 217 [189–316] |

## What remains at 1,024 sections

In the run above, the bench's long-frame attribution at 1,024 sections
puts 135 ms of script in the press's task and 51 ms in the frame after
it. The costs below are part of that time and grow with the page. Each
time given is from a profiled run of this branch at 1,024 sections, with
the host's load about 17, so read the times as proportions:

- **The annotations are collected and drawn whole, twice per send.** The
presenter's paint of the sent comment and the card's own redraw
(`showPreview` → `renderAnnotations.refresh`) each run
`paintAnnotations`. Its `collectEntries` (annotation-inventory.js) reads
every thread, contribution and workflow on the page. Its `renderNow`
walks every entry, and though it skips drawing a host whose entry is
unchanged, it registers every row and reads every target's place down
the page (`readSpokenPositions`). Each marker's name also counts the
markers ("3 of 1,024"), so a row added or taken away renames every
marker (`nameMarkers`). Together 64 ms profiled in the press's task, and
the same again when the server's reading arrives. Removing it takes an
inventory that rebuilds only the entries whose inputs a publication
changed (an unchanged entry already keeps its identity, `keptEntry`), a
projection that visits only the hosts whose entries changed, and one
paint per send rather than two.
- **The application's publication shares every subtree with the last
reading** (framework/snapshot.js, `share`), 16 ms profiled for the
send's own publication. Folds that produce only what moved would remove
it.
- **The rows-only margin pass still visits every row**, 12–14 ms
profiled: each row's record is carried and its seat written, though only
the new row moved. #5752 describes the change that removes it, a pass
that seats only the rows a change can move.
- **The Tab stop is chosen from every marker** (`syncRoving` →
`visibleRows`, margin-projection.js), clipping each marker's box, 8.5 ms
profiled. Reading only the markers around the window would remove it.
- **Focus moves record the reading place** (`recordPlaces`, version.js,
through `elementsFromPoint`), 17 ms profiled across the press and the
frame.

This change does not close #5752: a change to the page's words or styles
still reads every row.

## Checked

- `test_presses_and_fills_leave_no_element_naming_stale_keys`
(`tests/journeys/test_render_navigation.py`), the stale-keys sweep, on
all 37 published examples: after each of 40 seeded presses and fills,
some followed by Escape, it forces a repaint of every element's keys and
fails on any attribute the repaint changes.
- It can fail now. With every visible `aria-keyshortcuts` overwritten
before the repaint, the old repaint passed on review-a-plan and
`paintKeys()` fails. With focus tracking removed from focus.js, it fails
on review-a-plan at step 14, on the margin projection's keys.
- Repaired, it failed on four examples, identically before the copy fix
on this branch and with main's runtime: dynamic-widgets, file-editor,
motion and feature-gallery, each a code block a disclosure or tab
revealed that lacked Enter. With the copy fix it passes on all 37; after
the copy row took the heading link's design, it passes on those four
again.
- Under parallel load, file-tree's teardown sometimes reports an unmoved
scroll: the vendored file tree (`pierre-trees`) writes a `scrollTop`
that moves nothing, which the write watch flags. It is not a stale key,
and main's runtime shows it too. Across every parallel run that included
file-tree, it showed in 4 of 11 on this branch and 2 of 8 with main's
runtime; in the last four alternating pairs over the same 10 examples,
in 2 on main and none on this branch. Alone, file-tree passed in all 5
runs.
- An independent review forced the real `paintKeys()` after a send (with
its POST held and landed), Escape, reopening the card from its margin
row, Tab into the card, a Resolve (held, landed, and refused with a
409), `z` and a second Resolve, on review-a-plan and the bench's asks
page, and found no stale `aria-keyshortcuts` or `aria-disabled`. It also
reproduced the speedup: send paint 348 → 193 ms and Resolve paint 227 →
124 ms at 1,024 sections in Chromium.
- `the footprints answer what a scan in the order they were seated
answers` (new, `tests/runtime/margin-placement.test.mjs`): 400
footprints of mixed heights, many overlapping and some sharing a top,
against 400 probes, comparing `hit` and `from` with a linear scan. It
fails when `hit` answers the first overlapping footprint by top rather
than the first seated. The other placement tests now seat through
`Footprints`.
- A scratch comparison, not committed, seated rows in 4,000 randomized
trials (up to 120 rows each, rail rows and pins, held, folded and level
pins, and gaps up to 8 px) with main's `seatRow` and `packRow` and with
this branch's: every seat and push was identical. With `hit` answering
by top instead, it failed on trial 5.
- 41 browser tests by name: the full-pass comparison and seat tests in
`margin/test_pins.py`, the reachability and held-marker tests in
`margin/test_rail.py` and `margin/test_clusters.py`, the card's opening,
closing, Escape and travel tests in `margin/test_opening_threads.py`,
the Resolve, Reopen, undo and in-flight tests in
`threads/test_resolving.py`, and the submit-shortcut test in
`navigation/test_commenting.py`.
- 28 code-block copy tests by name: the copy tests in
`test_render_anchors.py` (Enter beside nested links, hidden and deferred
blocks, closed sections, scroll stops) and
`test_render_code_reading.py`, and the copy controls in
`margin/test_scrollers.py`, `drafts/test_draft_editing.py`,
`threads/test_rewrites.py`, `widgets/test_lifecycle_and_controls.py` and
`test_page_head.py`. All 28 pass.
`test_hidden_code_arrivals_do_not_read_skipped_blocks` now also asks
that the block a disclosure reveals names Enter, and fails on main's
copy row.
- `pnpm run test:runtime` (936 pass), `pnpm run check:browser`,
pre-commit on every changed file.
- The reflection counts above, read with a temporary counter in
`reflectKeys` on main and on this branch.

## Not checked

- Firefox's paint and shown times: the bench reads only CPU there.
- WebKit.
- The broad suite and the main-push browser tier.
- The stale-keys sweep presses controls in a seeded random order, so it
reaches the card and settlement routes it happens to press, not every
one.

Part of #5194.
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.

2 participants