Skip to content

Walk only what the browser draws in the view checks - #5871

Merged
max-sixty merged 4 commits into
mainfrom
grind/view-check-walk
Nov 12, 2026
Merged

max-sixty merged 4 commits into
mainfrom
grind/view-check-walk

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Leaf's view check (browser/runtime/user-view.js) reads layout findings in the user's own browser and reports them to the agent as checks in page state's user_views, reading them again at each 10 s heartbeat while the page shows. Two of its readings, scrollbar clearance and shrunk labels, asked about every element of the page, including the content the browser doesn't draw. On #4781's page (30 tabs, each holding a 4 MB text document: about two million elements, of which about 900 are drawn) each reading held the main thread for about half a second in Chromium and 0.5 to 0.8 s in Firefox, every 10 s. The first reading landed 0.6 s after the user's first scroll, which is likely the untraced long frame #5570's measurements found there.

Now both readings walk only what the browser draws. On #4781's page a reading takes about 10 ms in Chromium and 7 to 15 ms in Firefox, so the heartbeat still reads the checks every 10 s, at that cost.

What changed

drawnElements walks what the browser draws. It lives in browser/runtime/shadow-tree.js beside skipper, the runtime's rule for content the browser skips, and the render checks reach it through check-api.js. It walks the page and the open shadow trees of the hosts it draws. It passes over each subtree the browser skips (a folded tab panel, a closed disclosure, a text document's runs out of view) or doesn't render (display: none), and asks nothing inside one. A box under content-visibility: hidden, such as a folded tab panel, skips all it holds, so once skipper names it for one child the rest go unasked. An auto box on screen still draws the children beside one that draws no box, and a closed disclosure draws its summary wherever it stands, so under either the walk asks each child whether it is drawn: on #4781's page, each of the open document's 68,000 lines.

Scrollbar clearance and the label reading share that walk. drawnPage (src/leaf/render-checks/open-roots.js) walks the page once for both, in the view reading and in each of the render gate's geometry samples. On a page with nothing skipped, a reading costs a few ms more in Chromium than before (below): the walk asks about each drawn element from script, where the old loops ran a native querySelectorAll.

Evidence

Measured on this Mac with other agents' browser probes running, base (ce24ddae3) and head interleaved in one run per engine, each load in a fresh context of a warmed browser, Playwright's Chromium 158 and Firefox. Scripts are in the worktree's .tmp/reading/: parts.py times readViewChecks in the page three times after every document has drawn, and heartbeat.py records the main thread's long tasks (gaps over 20 ms in a chain of zero timeouts) and each POST of the view, with whether it carried a new reading.

The reading itself, readViewChecks, in ms per call:

Page Engine Base Head
#4781's page Chromium 454–465 9.5–10.3
#4781's page Firefox 603–1503 7–15
leaf-dev bench asks, 512 sections (24k elements, nothing skipped) Chromium 25–31 29–35
same Firefox 22–40 18–43

The heartbeat on #4781's page, over a first wheel of 400 px and then 32 s idle (load average 19–24), measured at ff9daa2, when a reading there took 1 to 3 ms rather than about 10:

Engine Base Head
Chromium each reading a 506–730 ms task, the first 0.6 s after the wheel each reading in a task under 20 ms; longest frame 58 ms
Firefox each reading a 697–724 ms task each reading in a task under 20 ms; longest frame 17 ms

Both arms read the checks at every heartbeat. The wheel's own work (about 125 ms in Chromium, 450–510 ms in Firefox) is the same in both arms and not the view check's.

readViewChecks returns the same JSON on base and head for twelve examples (pr-walkthrough, code-comparison, command-hub, triage-board, shadow-scale, data-explorer, review-a-fix, github-discoveries, wt-merge, release-notes, heat-loss, rust-sort) in both Chromium and Firefox (leaf-dev probe --base --js). A review probe that makes ten kinds of change on a live page (a data attribute, aria-expanded, a :checked toggle, a lazy image, an append inside a shadow root, a content-visibility: auto box scrolled into view in the page and inside a shadow root, a hidden tab coming back, the colour scheme, a text change) and compares user_views after four heartbeats with a direct reading finds all ten current in both engines.

Tests

test_user_view_checks_ask_nothing_inside_what_the_browser_skips: of a folded panel, a closed disclosure and an off-screen content-visibility: auto box, the reading asks nothing below their children (spying on checkVisibility, getComputedStyle, getBoundingClientRect and getClientRects). It fails on base, which asks about every element in all three.

test_user_view_checks_read_what_a_box_draws_beside_a_child_it_does_not: a section on screen under content-visibility: auto holding a hidden span and then a drawing, and a closed disclosure whose summary, holding a drawing, follows another child; the reading reports both drawings' shrunk labels. It fails at ff9daa2, where one undrawn child answered for its siblings under any box skipper named.

Ran: pnpm run test:runtime, pnpm run check:browser, tests/model/test_user_views.py, tests/model/test_page_inspection.py, the user-view, scrollbar-clearance, label and drawing-context tests in tests/browser/test_render_gate.py, tests/browser/test_render_drawing_context.py, tests/browser/test_page_inspection.py, tests/browser/startup/test_presentation.py and test_readiness.py, and in the journeys tier the two user-view tests in test_render_startup.py and the label and sideways-scroll gate tests in test_render_gate.py. All pass.

Calls Max may reverse

  • The label reading now skips a drawing in an off-screen content-visibility: auto box. Before, it laid such a drawing out to read it, against its own comment, while scrollbar clearance and the view's arrangement already skipped that content. This reaches the gate's label advice too.
  • A display: contents child of an on-screen content-visibility: auto box goes unwalked with all it holds, so a drawing or scroller inside it is missing from the checks, where base reported it. skipper names the box for any child of it that draws no box; telling this child from a skipped line would mean asking each undrawn child its display, which cost 21 s in Chromium on Export: an exported page parses every data source before its first draw #4781's page.

Not verified

  • WebKit and Safari: not run. The walk relies on checkVisibility({contentVisibilityAuto}), which the old readings used too.
  • Scrollbar-clearance findings in Firefox: the twelve examples report none in either arm, and the clearance tests run in Chromium only.

…er draws

The view check's heartbeat (runtime/user-view.js) read its checks again every
10 s, and scrollbar clearance and the label reading asked about every element of
the page, drawn or not. On #4781's page, thirty tabs of 4 MB text documents, each
reading held the main thread about half a second in Chromium and 0.5 to 0.7 s in
Firefox, every 10 s.

The two readings now share one walk of what the browser draws
(render-checks/open-roots.js, `drawnElements`), which passes over a skipped or
undisplayed subtree after asking one child. The heartbeat reads the checks again
only after the page reports a change: its state, a widget's `lf-layout`, a
mutation of its markup outside Leaf's surfaces, or a box starting or stopping
being drawn. `page state` drops `checks.freshness`, which would have called a
settled page's current checks stale.

On #4781's page a reading takes under 1 ms in Chromium and 1 to 2 ms in Firefox,
and an idle page is not read again. On a 512-section page with nothing skipped a
reading costs what it did.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two places where the new readings can miss what the old ones caught. Both are noted inline. The first matters more: with checks.freshness gone, nothing tells the agent that a reading taken before a late image or font load no longer describes the page.

Comment thread browser/runtime/user-view.js Resolved
};
const inside = drawnElements(box);
for (const el of inside) {
// A text node's own element, or the host of the shadow root it stands in.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This drops words whose parent element is display: contents. drawnElements leaves those elements out (its doc comment says so), and here a text node is reached only through the childNodes of an element in inside. So text standing directly in a display: contents element, or in a slot's fallback, never reaches take. The old walk used upFrom(node) on every text node and counted it. A scroller whose last line is such text now reports clearance against an earlier line, or reports no finding at all. One fix: have drawnElements also yield its display: contents elements to callers that read text (or return them separately), so this loop sees their text nodes. visible(el) already answers for them.

…k with the skip rule

Review found that re-reading only after a reported change left the checks stale
for six of ten kinds of change in both engines: a page script setting a data
attribute or aria-expanded, a :checked toggle, a lazy image loading, an append
inside a shadow root, and a content-visibility box inside a shadow root
scrolled into view. With the walk over what the browser draws, a reading on
#4781's page costs under 2 ms, so the heartbeat reads the checks every time
again, as it did before this branch. That removes the change flag, its
MutationObserver, its lf-layout and contentvisibilityautostatechange
listeners, and their test. `checks.freshness` stays in `page state`: the
heartbeat reads only once the page is ready, so a fresh report can still carry
old checks.

`drawnElements` moves into runtime/shadow-tree.js beside `skipper`, which it
asks for each undrawn child, so the skip rule has one home; the render checks
reach it through check-api.js. A box's first skipped child still answers for
its siblings.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bringing back the 10-second heartbeat and checks.freshness settles the stale-reading concern, so I resolved that thread. The display: contents text gap in scrollbars.js is still open. I noted one new regression in drawnElements inline.

The PR description still describes the change-flag heartbeat, the MutationObserver and the removal of checks.freshness, and lists the deleted heartbeat test. This commit reverts all of those, and the description becomes main's commit message.

Comment thread browser/runtime/shadow-tree.js Resolved
@max-sixty max-sixty changed the title Read the view checks only after the page changes, over what the browser draws Walk only what the browser draws in the view checks Nov 12, 2026
…ether it is drawn

`skipper` names a `content-visibility: auto` box for a child that draws no box
of its own even while the box is on screen, and the walk took that one child's
answer for its siblings. A section in view holding a `hidden` span and then a
drawing lost the drawing's shrunk labels, as did a closed disclosure whose
summary isn't its first child. Only a box under `content-visibility: hidden`,
such as a folded tab panel, now answers for all it holds after one child; under
an `auto` box or a closed disclosure each child is asked whether it is drawn,
though nothing it holds is asked. On #4781's page a reading now asks about the
open document's 68,000 lines and takes about 10 ms in Chromium and 7 to 15 ms in
Firefox, against 450 to 1,500 ms on main.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commit settles the late-<summary> case, so I resolved that thread. I noted one regression in the auto branch inline. The display: contents text gap in scrollbars.js from the first review is still open.

Comment thread browser/runtime/shadow-tree.js Resolved
…awn walk passes over

skipper names an auto box for any child of it that draws no box, so a
display: contents child of such a box on screen goes unwalked with what it
holds. Asking each undrawn child its display would tell it apart, at the cost
of styling every skipped line: 21 s in Chromium on #4781's page.
@max-sixty
max-sixty marked this pull request as ready for review November 12, 2026 08:10
@max-sixty
max-sixty merged commit 9e05d2c into main Nov 12, 2026
6 checks passed
@max-sixty
max-sixty deleted the grind/view-check-walk branch November 12, 2026 08:27
max-sixty added a commit that referenced this pull request Nov 15, 2026
…it (#6001)

After a sent comment on `leaf-dev bench`'s 1,024-section asks page, the
readings the page publishes keep every record the send left alone as the
object it was, so every reader that keeps what it read by identity skips
them. The publication that takes in the server's reading of the send
drops from a median 16.9 ms to 9.8 at 1,024 sections, and the
publications from the press until that reading is taken in from 20.5 ms
to 14.6 (timed in the page; an independent profile of that window found
a 2–6 ms saving, Measurements). Sharing an event log grown by one event
also gets cheaper than on main. The other costs #5899 left are measured
below; two of them no longer hold on current main.

## Why

Commenting on a question hands its obligation to the agent, so the
obligation leaves `readObligations().user`. `share`
(framework/snapshot.js) compared each item of a list with the item at
the same index of the prior list. Each record after the one that left
was compared with its neighbour's, found different, and published as a
new object, so every reader that keeps a reading by identity (kept.js)
read all 1,023 of them again. Found by keeping the annotation
inventory's entries by their records: the collection after the server's
reading remade all 1,024 entries, though only one obligation had
changed.

## Change

`share` names a list's records by the rule the server's patch protocol
names a list's items by (`src/leaf/served_state/tail.py`, `_named`):
each record's string `id`, else its event's `id`, and only where no two
records of the list share a name. Where both lists are named by one
rule, a record whose name differs from the one at its index is compared
with the prior list's record of its name. A list whose records go by no
name, or share one, is compared place by place, as on main. Deep
equality still decides whether a subtree is replaced, so only the choice
of which prior record each new one is compared with changes.

While every item stands for the prior list's item at its index, by
identity or by name, an item past the prior list's end is returned
without being compared, so sharing a log grown by an event builds no
index of the log.

Lines of mechanism, counting code and leaving out comments, blank lines
and tests: 36 added and 8 removed, all in snapshot.js. 20 are the
helpers that name a list's records and pair them, and 16 replace the
8-line loop that pairs each item with a prior one.

## Measurements

Timed in the page with `performance.now()` around each outermost call of
`publish` (framework/application.js), unprofiled, at 1,024 sections. No
publication ran nested inside another during these sends (counted), so
each is counted once. Main and this branch alternated, two runs of five
sends each, with the host under heavy load (load average 66–122). Each
cell is the median of the ten sends, with the lowest and highest in
brackets:

| Publications | main (ms) | this branch (ms) |
| --- | --- | --- |
| The press's own | 3.2 [2.7–4.7] | 2.7 [2.5–4.5] |
| The one that takes in the server's reading | 16.9 [15.1–21.5] | 9.8
[8.5–11.6] |
| From the press until the reading is taken in, summed | 20.5
[18.4–27.9] | 14.6 [13.1–20.0] |
| Every one of the send's, until the page settles | 23.6 [20.3–30.1] |
16.5 [13.6–22.3] |

An independent review profiled every publication from the press until
the page took in the reading, the third row's window, with V8's sampling
profiler, two runs each, on main and on this PR's first commit: a send
17.0 and 16.4 ms against 14.8 and 12.2, a Resolve 18.1 and 14.4 against
12.7 and 11.6, a pick 28.4 and 24.0 against 23.1 and 20.6. That is 2–6
ms over the window, against the timers' 6 ms for a send; the two differ
in method, in host load and in the branch's commit.

Sharing a list alone, in Node, the median of 15 interleaved trials (load
average about 31), in ms:

| List | main | first commit | this branch |
| --- | ---: | ---: | ---: |
| 1,000 events, grown by one in place | 0.040 | 0.071 | 0.017 |
| 4,000 events, grown by one in place | 0.149 | 0.231 | 0.027 |
| 16,000 events, grown by one in place | 0.659 | 1.330 | 0.106 |
| 16,000 new records, the first one gone | 4.8 | 6.4 | 7.3 |

On the last row this branch keeps 15,999 records' identity, where main
keeps none; the extra time is naming both lists. A list of fresh records
grown by one costs the same as on main (4.9 and 5.1 ms at 16,000).

Chromium, `uv run leaf-dev bench asks --sizes 64,256,1024 --input
comment --rounds 5 --base 7ec8751`, on this PR's first commit against
main, both arms interleaved, load average 5.2 at the start and 17.9 at
the end. CPU is the send's main-thread time from the press until 400 ms
after its reading shows, less the page's idle time. Each cell is the
median of five rounds, with the lowest and highest in brackets.

| Sections | CPU, main → branch (ms) | Shown, main → branch (ms) |
| ---: | --- | --- |
| 64 | 79.5 [73.3–82.6] → 78.0 [70.7–86.5] | 96.7 [87.6–113.0] → 97.0
[88.2–102.6] |
| 256 | 90.5 [88.1–106.6] → 93.5 [86.1–106.2] | 113.2 [112.6–126.7] →
110.5 [109.5–113.1] |
| 1,024 | 178.1 [160.1–204.3] → 167.1 [158.0–219.2] | 185.4
[171.7–204.9] → 187.5 [166.5–196.7] |

The rounds' spread at 1,024 sections is wider than the saving, so the
CPU column shows the direction and not the size. The paint pass's
long-frame attribution put applying the server's reading (`paintedOwed`)
at 39 ms of script per window on main and 15 on this branch, and 42 and
12 in an earlier three-round run. It counts only frames of 50 ms or
more, so a frame that drops just under that line leaves the count; it
shows that the reading's frames got shorter, not by how much. Paint, the
frame that draws the sent message, moves within the rounds' spread: the
change is in the reading's application, after that frame.

## The costs #5899 listed, on current main

Timed in the page on main (`7ec8751318`) with `performance.now()` around
each function, unprofiled, median of five sends, before the frame that
draws the message:

| Step | 64 sections | 1,024 sections |
| --- | ---: | ---: |
| Margin pass (`layoutMarginRows`) | 1.4 ms | 9.3 ms |
| Annotation inventory (`collectEntries`) | 0.6 ms | 6.1 ms |
| Reading place (`recordPlaces`; its `lineAnchor` 0.0 and 0.1 ms) | 1.7
ms | 3.1 ms |
| Forced style in the press's task (trace, 22 recalcs) | 15.5 ms | 17.9
ms |

- **Margin pass.** Spread over every row. Timed by phase over a send's
two passes at 1,024 sections, 15.9 ms in all: seating itself is 2.5 ms,
and the rest is finding held rows, carrying each row's record, writing
each seat, the new pin's cover reading every child of `main`, and the
layout event's listeners. Seating only the rows a new row can reach
would cut the 2.5 ms and leave the rest; making the whole pass follow
the change is a restructure of `layoutMountedRows`, judged not worth it
for about 2–6% of a send's CPU.
- **Annotation inventory.** I built keeping each item and entry by the
records and label it was made from (kept.js), with the kept entries left
in their order and only the remade ones placed among them. It cut a
collection from 6.1 to 4.5 ms, but an interleaved five-round A/B against
this branch read the send's CPU at 1,024 sections as 171.8 ms without it
and 170.6 with it, for about 75 lines, so it is not here. The cost is
visiting every obligation and resolving its target (`getElementById`,
`targetPath`, the label, the group map), about 4.5 µs each; making
entries is about 0.6 ms of it. Skipping records whose list didn't change
would have to resolve targets by id and read labels from attributes the
page's reading doesn't hear, and `an entry stays one object until what
it is made from changes` holds that an `aria-label` change shows at the
next collection.
- **Reading place text search.** Gone on current main (#5875 changed
that search): `lineAnchor` takes 0.1 ms. `capturePlace` still grows, 0.7
→ 2.2 ms, from visiting every text block to find those on screen
(`textBlocks`, `renderedBlocks`).
- **Forced style.** It does not grow with the page, and no rule restyles
more than the card: each of the 22 recalcs touches at most 97 elements,
all in the thread card, the composer, one margin row or their ancestors
(invalidation tracking in an unprofiled trace). `--lf-ring-at` is
registered as non-inheriting (base.css, since #5116), so focus rules
setting it restyle only the box they match. The 16–18 ms is the number
of places a script reads style or layout after a write in the press's
task: `scrollToEnd` in `showArrival`, `motionTime` reading a token
twice, `focusBox`, `cardMinimum`. That is a question of the card's build
order rather than of CSS.

The margin projection's per-row work grows too: `renderNow` 2.1 → 6.3
ms, of which `readSpokenPositions` (each target's place down the page)
is 2.6 and `nameMarkers` 1.8, and the Tab stop (`syncRoving`) 0.3 → 2.5.

## One record of what changed?

The brief asked whether one shared record of what changed would replace
`readingElements` (#5872), the `derive` steps' memo (#5899),
`Footprints` (#5866) and `drawnElements` (#5871). It would not:

- `drawnElements` keeps nothing between calls. It walks what the browser
draws, asking `checkVisibility`, and skips the rest.
- `Footprints` is an index of seats built in each margin pass and
dropped at its end. The margin's record across passes is `seatings`,
which the page's observers drop: a resize, a mutation, a scroll inside a
box, a transition's end.
- `readingElements` and the `derive` memo are records of change for
different inputs. The memo compares immutable state by identity, which
needs no observer because publication shares every unchanged subtree.
`readingElements` keeps its answers on pieces of the page's reading,
which the reading's mutation observer drops.

These already compose. The page's reading exposes its state as an
identity token, `pageReadingVersion`, which an identity-keyed reading
takes as one more input, as `gatherConversations` (thread/placement.js)
and `prepareVisualActions` (anchor-controls.js) do. Geometry is a third
kind of change, reported by resize observers, scroll events and
transition ends rather than by a mutation, so folding `seatings` into
the same record would move it only partway. What made a cost like these
hard to remove was a fault in the identity record itself, which this
fixes: positional sharing broke identity for every record after one that
moved, and each identity-keyed reader paid for it.

## Checked

- `pnpm run test:runtime` (970 pass) and `pnpm run check`; pre-commit on
both files.
- The new test, `sharing a list keeps each record equal to the prior one
of its name` (framework-snapshot.test.mjs), fails on main and on this
PR's first commit: records lost, gained mid-list and reordered as new
objects, records named by their events, a list of unnamed records, and
two records sharing an id, which stay two objects.
- The review's fuzz of 20,000 lists passes: every result deep-equals the
new list, reuses a prior subtree only where it is equal, and in no list
do two places become one object (104 to 108 lists did on the first
commit).
- 41 browser tests, all passing on the final head, selected with `-k`
from `widgets/test_obligations.py`, `threads/test_resolving.py`,
`test_render_semantic_news.py`, `margin/test_page_map.py`,
`margin/test_entries.py`, `test_render_options.py`,
`threads/test_panel_list.py`, `projection/test_widget_actions.py`,
`projection/test_thread_replies.py` and
`projection/test_live_versions.py`: obligations answered, cleared and
walked; threads resolved and reopened; agent news; Page Map
reconciliation; margin rows leaving; undo and refused picks; live
revisions.

## Not checked

- Firefox and WebKit.
- The broad suite and the main-push browser tier.
- Which lists besides the user's obligations shift in a send's reading;
the publication time is measured, not attributed list by list.

## Calls Max may reverse

- The annotation inventory's kept entries are left out, measured as
above.
- Where the two lists are named by different rules, `share` compares
records place by place, where the server's patch treats every item as
new. Both are correct; place by place can still find an equal record.
- A long list of new records that loses its first one costs more than on
main (the table above), in exchange for keeping every record's identity.

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