Skip to content

Keep the page's reading by part, so a load or a gesture reads only the documents it needs - #5570

Merged
max-sixty merged 9 commits into
mainfrom
grind/reading-datum
Nov 12, 2026
Merged

max-sixty merged 9 commits into
mainfrom
grind/reading-datum

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

On #4781's page, 30 tabs each holding a 4 MB text document with one comment on Doc 23's text, and on the same page with a paragraph of prose under its title, no load and no gesture reads more than the frame (the page's words outside its documents, under 2 ms) and the one document it touches: Doc 23 for the comment at load, Doc 0 for the first gesture over it or with it in view, 58–80 ms in Chromium and 71–105 ms in Firefox. The load's longest frame is 0.81–0.87 s in Chromium and 1.04–1.16 s in Firefox, against 5.3–5.7 s and 8.5–9.0 s on main. The first scroll pause and click cost within 30 ms of main, which read the whole page at load instead. A small scroll that brings the place to rest on the prose line, and a double click on the title, take 0.12–0.29 s, where main takes 2.2–6.4 s. A triple click holds it 0.59 s in Chromium and 1.0–1.3 s in Firefox, against 27–28 s on main in Firefox; most of that is the selection's capture.

This combines #5570, which finds a comment on a datum by reading that datum alone, with the reading Max chose for it: the page's reading is kept by part, so a change re-reads only the part it touches, and each reader reads only the parts it needs. Since the reviews, prose is read in the frame alone, so nothing puts the documents' words together with it any more; the quote index is kept for each reading a search asks; each change drops exactly the pieces it moves, found from what it changed; and a selection finds the parts it touches by binary search (below, "What the reviews changed").

The reading kept by part

passages.js keeps the page's reading in pieces (Piece). Each outermost datum is a part, read alone. The frame is the rest of the page, read with each part left out where it stands and a fence there. A heading, header or hgroup is never a part, since a heading in it heads what stands outside it, and neither is a datum that runs inline with the words around it.

pageText() is the frame's reading and pageText(part) a part's. Nothing reads the two together, because a quote in prose never has a copy in a part: every datum the projection draws carries data-lf-gen, which makes it a passage cell of its own, fenced at both edges, so a copy across its edge is fenced and found by no search, and a selection whose words all lie in one datum is captured on that datum. I checked the premise in Chromium on every example that draws datums (pr-walkthrough, 32 datums; github-discoveries, 8; review-a-fix, 5; command-hub, 5; live-progress, 1) and on the reviewer's two-document fixture: every datum is fenced at both edges, and no search matches across one. One capture broke it on main: a selection starting in the newline before a document and ending in its first line was captured as a prose anchor whose words stand inside the document. A selection's whitespace-only ends are no part of its passage now, so that selection is captured on the document; a selection whose words cross a part's edge is captured detached, which it already was in effect, since its copy is fenced.

The owner of each change drops exactly the pieces it moves:

  • The watcher's records go through touch. Words edited inside a part drop that part. A node put into or taken out of the frame drops the frame and any kept part inside it. A change to the markers the walk reads on an element (chrome, a datum's own marks) drops the piece it stands in and every part under it, and the frame where the element is a part's root. Anything that changes which slot shows a host's child drops everything under the host: the host's children, a slot put into or taken out of its tree, a node's slot, a slot's name. A batch of records finds the kept parts it moved from the nodes it changed down, through the declared trees under them; a node that holds a slot of its declared tree stands over the host's children the slot shows, so the parts go from that host down, and on up while the host holds a slot in turn (forgetPartsBeneath). So hearing a change costs what it changed, however many parts are kept.
  • The two doors for changes no record reports, fencePassageParts and watchPassageRoot, go through changedAt, which drops what a marker change at that node would.
  • A new vocabulary or settlement drops every piece, as before.

What an element inside a part answers (elementReading, keptAnswer) is kept under that part's token, so it stands while other parts change.

Each reader reads only the pieces it needs:

  • A quote in prose is captured and found in the frame; a quote in a datum in its part.
  • The quote index is kept for each reading a search asks: the frame's, and each datum's own, whether the datum is a part, stands in one, runs inline or is a heading. A reading's copies of every quote the threads name that it lacks are found by one pass over it when a search first asks it for one, and carried from one reading of the same words to the next, so a rewritten line re-places only the threads quoting it.
  • The outline is the frame's, with the headings of each part that holds one spliced in. A part with no heading in it, in a declared tree under it, or among what its slots show, needs no walk for the outline. What stands over an element in a part joins the part's own record to the frame's at the part's root, so a part is walked for its outline only when someone asks about an element in it.
  • The reading place reads the frame and the parts that may show in the window (mayShow). It asks a part the page skips, as in a closed tab, nothing beyond drawn: asking such a part for its box or style had Chromium style everything in it, 240 ms for the 29 closed documents here. A box holding a part holds text wherever the part stands.
  • A selection is captured in the reading of the piece it stands in, and each end snaps in its own piece's reading. Where a range stands among the parts is a binary search over the frame's marks, which are in reading order, so it compares a few parts however many the page holds; a range across a part's edge takes its segments from the frame and each part it touches.
  • A search (pageFind) finds matches in the frame and in each part, in reading order, since a match stands in one passage cell. The first search reads every part.
  • A drawing reads the parts that may show; a match's or a control's context reads the piece it stands in (readingHolding).

Against main the change adds 806 production lines and removes 308 (509 and 195 outside comments), 693 and 230 of them in passages.js: the pieces and their invalidation, the outline put together from them, the per-piece quote index, the part-aware readers above, and #5570's datum reading. It adds no option. The first review round added 399 lines and removed 303 (252 and 191 outside comments), and the second 122 and 105 (63 and 56). By my count of the diff, reading prose in the frame alone added about as much as it deleted: it deleted the splice, the per-part record of datum quotes and anchor placement's prose rule, and added the frame's fences, the search across pieces, the segments of a range across an edge and the capture fix. The fixes for B, D, E and F added the rest.

What the reviews changed

The first review:

  • A. Many comments on one large document. A change to a part marked every quote found in it changed, and each re-placement then scanned the whole part again, once per quote. The quote index is now kept per piece: one pass over a piece finds the copies of every quote it lacks, when a search first asks it for one, and a change carries them through what it changed, so a rewritten line re-places only the threads quoting that line. This deletes the per-part record of datum quotes (foundUnder, changedUnder) and anchor placement's rule naming which anchors are prose, which restated resolveAnchor's branch order. Test: a line rewritten in a part changes only the quotes standing on it. On the reviewer's repro (a 2.5 MB document with 50 comments, one line rewritten, four runs each), the longest task is 397–402 ms in Chromium and 426–451 ms in Firefox, against 289–466 ms and 495–526 ms on main and 2,053 ms and 3,036 ms in the review.
  • B. Hearing a change grew with changed nodes times parts. A batch of records first forgot the parts under the nodes it changed once, climbing from each kept part; the second review's 3 replaced that with finding them from the changed nodes down. On the reviewer's repro, hearing 500, 1,000 and 2,000 replaced rows took 25, 88 and 345 ms in Chromium and 34, 127 and 471 ms in Firefox in the first review; measured below.
  • C. Prose near the top brought the whole read back. Prose is now read in the frame alone (above), so the reading place on a prose line, a double click on the title and a prose comment read the frame. The splice that put the whole reading together is gone. Tests: the pieces test holds the frame to a fresh walk's words outside the parts; a selection from the space before a datum into its line is captured on the datum and a selection across a datum's edge is captured detached hold the one capture that broke the premise. Measured below on the page with prose under its title.
  • D. A chart beside a document out of view lost its place. A box holding a part now holds text wherever the part stands. Test: test_a_drawing_beside_a_document_out_of_view_stays_the_place (browser); without the rule, restoring moves the chart 132 px.
  • E1, E2. A slot put into or taken out of one part moved a child out of or into another part's slot. Anything that changes which slot shows a host's child now drops everything under the host. Test: test_a_slot_moving_a_child_between_parts_changes_both, both cases (browser, since happy-dom assigns a child to every slot that would take it); without the rule, the kept reading holds the child's words twice, or not at all.
  • E3. The outline missed a heading a part's slot shows. Whether a part holds a heading counts the headings its slots show. Test: the pieces test's first case, whose page has a slotted heading in a part.
  • E4. A datum that came to stand inside a newly marked datum stayed a part. A change to an element's markers now forgets every part under it, whether or not the element is itself a part. Test: the change a part comes to stand inside a new part, then words edited in the part that stands inside it, which checks every element's kept words against a fresh reading.
  • F. mayShow asks drawn, which skipper now exports from shadow-tree.js, rather than repeating it; pageRuns and pageBlocks always take their part filter, and the tests that called pageBlocks() pass one.

The second review:

  • 1. A selection asked every part whether it covered it. segmentsIn ran on every selection change and tested each of the frame's marks against the range, 7.9 ms in Chromium and 14 ms in Firefox with 5,000 datum rows below the prose. The marks are in reading order, so the parts a range covers now run from the first mark not before its start to the first not before its end, found by binary search, with the part its start stands in just before them; a range across a part's edge gathers the frame's segments and each touched part's, each clipped by its own binary search, rather than every run of the page. Test: a selection compares a few parts' places however many the page holds, which counts the steps up the reading's tree: 170 for a selection in prose beside 100 parts and 230 beside 1,000; scanning the marks fails it. a selection across parts reads the frame's words and theirs in order holds the gather.
  • 2. Quotes in a datum that is no part were never reported changed. Anchor placement finds a datum's quote in that datum's own words, but only a part's reading kept an index. Every reading of an element now registers with that element, and the index brings it to the element's reading now. Tests: a quote in a datum inside a part / standing in a line / that is a heading changes when its words move, the reviewer's three cases.
  • 3. Hearing a change in the frame climbed from every kept part. A batch now finds the kept parts it moved from the nodes it changed down: each node's datums and those in the declared trees under it, and for a node holding a slot of its declared tree, from the slot's host down, and on up while that host holds a slot in turn. Tests: hearing a paragraph put in the frame costs the same however many parts are kept (3 steps beside 100 kept parts and beside 1,000; climbing from every kept part fails it), which replaces the first review's B test; the pieces test's new changes, a slot that shows a part made a part and words edited in the part it shows; and a change over a slot reaches the parts another slot passes on to it.
  • 4. partOf and readingOf are now readingPart and readingHolding.

Measurements

Each arm served a fresh copy of #4781's commented page directory with leaf server run --lifetime temporary and a throwaway state home: main at b14fa1e, and this branch at f90bcc6, before the second review's fixes. Those fixes change no cost these gestures pay on this page (with 30 parts, the selection's scan of the parts was already a few comparisons; the triple click stays in one document; the redraw changes one document), which I read off the code and did not re-measure. The prose variant adds one paragraph under the page's title, as the reviewer's perf-page3 does at a tenth the size. Each runtime logged the page readings it took. Three rounds alternated the arms on one shared host (load average 3–5), one browser per round, engine and page, warmed 15 s, each load in a new context. Each gesture had its own load, made 5 s after the comment's mark stood on its passage and every document had drawn:

  • the first scroll: a 400 px wheel over Doc 0's listing;
  • the first small scroll over the prose: a 40 px wheel over the paragraph, which brings the place to rest on its line;
  • the first click: on the listing's fifth line;
  • the first double click on the title;
  • the first triple click: on the listing's fifth line;
  • a redraw: after a wheel to warm the reading place, leaf data set rewrites a line of Doc 5's source, a closed tab, and 2 s after Doc 5 redraws another wheel reads the place.

Each gesture's window opens before the pointer moves onto its target, since resting there starts a preview. The longest frame is the longest gap between animation-frame callbacks; the longest task is the longest gap in a chain of zero timeouts, which only main-thread work holds back. The page's view checks (user-view.js), which walk every element every 10 s, were stubbed in every arm, so a window holds only its gesture's work ("Found along the way"). Times are in milliseconds; each cell is the range over three loads (twelve for the load rows). The triple click and redraw rows come from three more rounds after the last change to the quote index. Chromium 156 headless and Firefox 157, through Playwright.

#4781's page:

Chromium, main Chromium, this branch Firefox, main Firefox, this branch
Comment on its passage, from navigation 7,594–7,923 2,876–3,064 16,030–16,567 8,356–8,691
Load: longest frame 5,346–5,661 813–844 8,513–8,832 1,039–1,091
First scroll pause: longest task 109–113 127–129 288–305 292–314
First click: longest task 150–163 169–177 177–187 184–209
First triple click: longest frame / task 5,831–6,025 / under 50 437–459 / 585–589 5,576–5,812 / 27,335–28,148 679–789 / 1,001–1,263
Redraw of Doc 5: longest task 4,031–4,305 809–837 5,596–6,241 1,104–1,490
Scroll after the redraw: longest task 58–71 under 50 104–159 under 50

The page with prose under its title:

Chromium, main Chromium, this branch Firefox, main Firefox, this branch
Comment on its passage, from navigation 7,539–7,836 2,905–2,970 16,005–16,878 8,651–8,914
Load: longest frame 5,320–5,539 823–873 8,514–9,012 1,043–1,162
First small scroll over the prose: longest task 2,402–2,511 121–146 3,563–3,616 279–292
First double click on the title: longest frame / task 2,220–2,286 / under 50 143–149 / 180–186 2,989–3,021 / 6,202–6,412 149–154 / 155–226
First scroll pause: longest task 110–118 126–137 295–307 313–327
First click: longest task 142–157 158–173 173–198 191–208

What each arm read: main read the whole page at load (2.6–2.8 s in Chromium, 4.3–4.5 s in Firefox) and again on the redraw (2.9–3.0 s and 3.5–3.6 s). This branch read the frame (0–2 ms) and Doc 23 (61–66 ms in Chromium, 71–87 ms in Firefox) at load, and Doc 0 (58–80 ms and 82–105 ms) on the first gesture over it or with it in view; the double click on the title, the redraw and the scroll after it read nothing.

The first scroll and click cost Doc 0's reading on top of main, which read the whole page at load instead; that is the 10–30 ms these rows show. A triple click's remaining cost is mostly the selection's capture, not the reading: in a Chromium profile, its 0.59 s task held the selection's capture (0.28 s, of which scanning Doc 0's 4 M characters for copies of the line took 0.19 s and comparing their context 0.09 s) and the reading place (0.11 s, Doc 0's reading among it). Searching one document for a quote without scanning all of it is the next lever. On main, a triple click held Firefox's main thread 27–28 s, and in Chromium drew no frame for 5.8–6.0 s with no task over 50 ms, which I didn't trace. Main's small scroll over the prose, profiled in Chromium, spent 2.0 s in the place's capture scanning the whole page for the line's copies.

The second review's cases, on the reviewer's fixture with 5,000 datum rows added below its prose, through leaf-dev probe --base: main (4b0bdd7), the reviewed head (f90bcc6) and this head, three rounds interleaved per engine. Times in milliseconds; each one-shot cell is the median of seven runs, and each cell the range over rounds.

Chromium, main Chromium, reviewed Chromium, now Firefox, main Firefox, reviewed Firefox, now
One segmentsIn in prose (1) 0 7.8–7.9 0–0.1 0 14–15 0
One anchorForRange in prose (1) 0.1 7.8–7.9 0.1 0 14 0
A drag over the prose: longest task (1) 86–91 175–177 89–103 47–51 221–224 47–53
A drag over the prose: time in tasks over 16 ms (1) 91–112 232–310 89–103 66–70 583–607 64–72
A paragraph put in the frame, every part kept: hearing it (3) 0–0.1 2–2.1 0–0.1 0 4 0–1

Firefox reports whole milliseconds. The drag is the reviewer's drag-many.py as a probe journey: 30 pointer moves across the first paragraph, timed by a chain of zero timeouts; the reviewed head held 2–3 tasks over 16 ms in Chromium and 10 in Firefox, main and this head 1–2 and 2.

The first review's cases, main against this branch through leaf-dev probe --base:

  • A, a 2.5 MB document with 50 comments and one line rewritten (redraw-e2e.js, four runs): longest task 289–466 ms on main and 397–402 ms here in Chromium; 495–526 ms and 426–451 ms in Firefox.
  • B, 500, 1,000 and 2,000 datum rows replaced at once (redraw-parts.js), two rounds: hearing them takes 2, 2–3 and 5 ms on main, 2, 3 and 5–6 ms at the reviewed head and 2–3, 3 and 6–7 ms here in Chromium; 10, 19 and 48–49 ms on main, 9–10, 19–20 and 48–49 ms at the reviewed head and 10–11, 20–21 and 50–51 ms here in Firefox. Finding a batch's parts from its 4,000 changed nodes down costs 1 to 2 ms more at 2,000 rows than climbing from the kept parts did.
  • D (drawn-restore.js): the chart stays where it was after the restore in both engines, on main and here.
  • E3, E4 (slot-probe.js): the outline holds the slotted heading and the inner datum's words follow its edit in both engines, on main and here. The probe's E1 and E2 cases compare pageText(), which is now the frame and holds no part's words, so the browser test above checks those in the parts.

#5371's queue stays

The queue draws closed tabs' documents after the page presents and reports their draws together. With the reading kept by part, a report reads only the frame. Taken out, so each document reported its draw as it landed, the 29 reports re-walked only the frame (32 walks, about 0 ms in all) and Doc 23 once, but each report's document pass still held the main thread about 420 ms in Chromium and 580–680 ms in Firefox, in work other than the reading that grows with the whole page. Every document then drew by 5.7–6.5 s in Chromium and 11.9–12.2 s in Firefox, against 3.0–3.1 s and 8.7 s batched (measured against the previous head, four loads per arm in Chromium and two in Firefox). The batching stays, and its comment in lf-text-document.js still gives the reason.

Found along the way

The view checks (user-view.js, reading src/leaf/render-checks/view.js) walk every element of the page every 10 s, and when the window, scheme or revision changes, calling checkVisibility on each (scrollbarClearance). On this page each run held the main thread 490–610 ms in Chromium and about 770 ms in Firefox, the same in every arm. It is probably the 750 ms frame #5570's earlier measurement saw on main about 0.5 s after a gesture. It is a whole-page pass the reading doesn't own, so this change leaves it alone; the measurements stub it in every arm.

Checks

  • tests/runtime/reading-pieces.test.mjs, at pageText, documentOutline, elementReading and the quote index. After each kind of change it holds the frame and each kept part to a fresh walk of the whole page (its words outside the parts, a fence where each stands, each part's words under it), every element's kept words to a fresh reading, and the outline to one read afresh. The changes: words edited inside a part, a break or a heading put in one, a part replaced, added and removed, prose edited, a part's own mark taken off, prose made a part and then edited, a part coming to stand inside a new part and then edited, the outer mark taken off, words a part's slot shows edited, a child given to the host whose part's slot shows it, a child moved to a slot in another part, a part moved under chrome, chrome put over parts and taken off, a fence put around parts, and a slot outside the declared tree's parts that shows a part made a part and then edited. It also holds every datum's own reading to the fresh walk. Further cases: the outline put together from parts against one walk of main, a slotted heading and a part's heading with none before it included; a change inside one part asking no element of another anything; a rewritten line changing only its own quote; a quote in a datum inside a part, inside a line or that is a heading changing with its words; a change over a slot reaching the parts another slot passes on to it; a selection in prose and one across two parts taking about as many steps beside 1,000 parts as beside 100 (170 and 230 in prose), and one across parts reading the frame's and their words in order; and a paragraph put in the frame costing the same 3 steps beside 1,000 kept parts as beside 100.
  • datum-quotes.test.mjs: capturing a selection inside a datum reads no words outside it; a selection from the newline before a datum into its line is captured on the datum; a selection across a datum's edge is captured detached.
  • tests/browser/test_reading_pieces.py (new): a slot put into or taken out of one part, moving a child between parts, both in a browser; a search finding each match in prose and in every part, in reading order. test_reading_position.py adds the chart beside a document out of view.
  • Breaking each rule in turn turns a test red, and restoring it turns it green: the six forgetting rules (characterData, a host's children, nodes put in or taken out, a node's slot, a marker on a part's root, parts under a marker change), parts under a new part (E4), a slot put in or taken out (E1, E2, browser), forgetting the batch's parts at all, forgetting per node rather than per batch (5,750 against 21,500 climbs), the frame's fence where a part stands, the outline's join, its part headings and slotted headings (E3), the piece index for a quote, a datum reading's index and pieceReading for any element (2), the binary search over the marks (1), the datums under a changed node, the declared trees under it, the climb over a slot and its second step, and finding parts from the changed nodes rather than climbing from every kept part (3), the selection's whitespace trim, the detached capture across an edge, and a box holding a part holding text (D, browser).
  • pnpm run test:runtime (935 pass), pnpm run check:browser, and the pre-commit hooks on the changed files.
  • The 76 browser tests whose bodies touch a quote on projected data or a text document, with the new ones (107 cases), by node id: 105 pass. Two fail identically on main's tree (b14fa1e): test_call_diff_source_paths_do_not_alias_durable_rename_coordinates and test_visual_review_keeps_its_inline_comment_editor_in_view_after_phone_resize[760].

Calls Max may reverse

  • Prose is read in the frame alone. A prose anchor no longer counts copies inside documents. Where main would let a prose comment move onto a copy inside a document once its own words changed, it now detaches, as Python's reading of the page's files, which holds no datum, already reads it.
  • A selection's whitespace-only ends are no part of its passage, so a selection from the newline before a document into its first line is captured on the document. A selection whose words cross a part's edge is captured detached at once, with no context, where main reached a detached anchor with context through its search.
  • Each end of a selection snaps in its own piece, so a selection inside one document no longer snaps past its edges, and one across an edge gets no sentence snap, as on main.
  • What a part is. Only the outermost datum the walk meets is a part. Nested datums are read with their part; a datum that is a heading, header or hgroup reads with the frame, and so does one that runs inline (a span). Data drawn without datum marks stays in the frame.
  • A search reads every part the first time it runs, since it searches the whole page.
  • A change at a node holding a slot forgets every part under the slot's host, not only those the slot shows, so the rule needs no assigned-node lists and covers slots that pass on what they show.
  • Memory. Each part read keeps two records per text node (the walk's and the indexed reading's), so a page whose every part is read, as by a search, keeps about twice what main kept for its whole reading (read off the code, not measured). On Export: an exported page parses every data source before its first draw #4781's page only Doc 23 and Doc 0 are read.

Not verified

  • WebKit.
  • A prose comment on the large page, end to end; by construction its search covers the frame, and the prose variant's place capture shows that cost.
  • A search's first run on the large page, which reads all 30 documents; not measured.
  • A real drag across a document's edge in a browser; the capture and snap across an edge are tested in the runtime's DOM.
  • The browser tests beyond those that touch a quote on projected data or a text document.

Part of #4781

Part of #5753: a change inside a datum now re-reads only that part. A change in prose still drops and re-walks the whole frame, and placement still compares the frame's old and new readings; that is what remains of it.

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

https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs

max-sixty and others added 2 commits November 5, 2026 14:55
On #4781's page, 30 tabs each holding a 4 MB text document with one comment
on Doc 23's text, the comment stands on its passage and every document has
drawn at 3.4-4.0 s in Chromium instead of 6.0-6.5 s, and at 7.8-11.7 s in
Firefox instead of 11.7-13.0 s. The load's longest frame drops from
2.9-3.3 s to 0.60-0.67 s in Chromium and from 4.4-4.9 s to 0.9-1.75 s in
Firefox, because the page no longer reads itself whole to find the comment.

A quote anchored in a widget's data was searched for in the page's whole
reading, `findQuote(pageText(), ..., datum)`, which walks every element:
2.3-2.6 s in Chromium and 3.6-4.2 s in Firefox on this page. `pageText(el)`
now gives the part of that reading under one element, read alone with the
element's edges as its ends, by the page reading's rules, and kept as long
as the whole reading. Capture and resolution both read the datum's part
(60-130 ms here), and `fencedAt` goes. A quote in prose still searches the
whole reading.

Reading the datum alone also lets a line at a datum's top or foot, held
twice in it, keep its place: its empty side was compared against the page
around the datum, so the comment detached.

#5371's queue stays: each closed document reporting its draw as it lands
drew every document by 7.6-7.9 s in Chromium and 12 s in Firefox, with a
520-690 ms frame per document after Doc 23's, since a document pass still
does work that grows with the whole page.

Part of #4781

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ts report together

`pageText(within)` holds the page reading's rules only for an element the
page reading reaches, as a datum is; under a shadow root the page doesn't
read, a part would hold words the whole skips, whose changes no watcher
reports. The queue's comment in lf-text-document.js keeps its reason, that
each draw report starts a document pass whose cost grows with the whole
page, and leaves the run's numbers to the first commit's message.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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.

Moving the whole-page reading off load puts its 2.4–3 s onto the user's first scroll pause or click. By the PR's own table, Firefox's first scroll pause goes from 83 ms to about 3 s, and a triple click goes from 0.9 s to 2.9 s in Chromium. AGENTS.md's Responsiveness ranks those inputs above load: a wheel step is meant to fit in 8 ms whatever the page's size. A frame that freezes on the first gesture costs the user more than one that freezes while the page is still arriving.

The PR describes this under "What moved" but leaves it out of "Calls Max may reverse", and it is the call most worth putting in front of Max. The cause underneath is that recordPlaces → capturePlace → pageBlocks needs the whole reading on a scroll pause or focus. Until that path reads only the window or the part a gesture changed, this branch swaps a load stall for a gesture stall. It would help to either name that follow-up (an issue under #4781) as the condition for landing, or say why the gesture stall is the better trade.

# Conflicts:
#	browser/runtime/passages.js
…nts it needs

The page's reading is now put together from pieces kept apart (`Piece` in
passages.js): each outermost datum is a part, read alone, and the frame is the rest
of the page, read with each part left out where it stands. A change forgets only the
piece it lands in. The watcher's records go through `touch`, and the two doors that
report changes no record does (`fencePassageParts`, `watchPassageRoot`) through
`changedAt`; each drops the part a node stands in, and for a change to the markers
the walk reads at an element, every part under it too.

Each reader now reads only the pieces it needs:

- The whole reading (`pageText()`) is spliced from the kept pieces, carrying a line
  break across a part's edges as one walk does, and its fences are worked out over
  the spliced segments.
- The outline is the frame's, with the headings of each part that holds one spliced
  in. What stands over an element in a part joins the part's own record to the
  frame's at the part's root, so a part without headings is read for the outline
  only when someone asks about an element in it.
- A quote in a datum is found in its part alone and is no longer kept in the
  whole-page quote index; it counts as changed once its part's reading is replaced.
  So a page whose threads all quote data never reads its words whole.
- The reading place reads the frame and the parts that may show in the window, and
  never asks a part the page skips, as in a closed tab, for its box or style, which
  had the browser style everything in it (240 ms in Chromium on #4781's page).
- A selection inside one part is captured and snapped in that part's reading.

Behavior that changes: a selection's snap stops at the edges of the datum it lies in,
where it used to be able to run on into the words beside the datum.

On #4781's page (30 tabs of 4 MB text documents, a comment on Doc 23's text) no load,
first scroll, click or triple click, and no redraw of a closed document, reads the
whole page. The pull request has the three-arm measurements.

New tests: `tests/runtime/reading-pieces.test.mjs` holds the reading and the outline
put together from kept pieces to one fresh walk after each kind of change, and a
change inside one part to leaving the others unread; `datum-quotes.test.mjs` adds
that capturing a selection inside a datum reads no words outside it.

Claude-Session: https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs
@max-sixty max-sixty changed the title Find a comment on a document's text by reading that document alone Keep the page's reading by part, so a load or a gesture reads only the documents it needs Nov 11, 2026

@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 what the earlier review raised: the first scroll pause and click now read only the document they touch. One cost the new invalidation adds is noted inline.

Comment thread browser/runtime/passages.js Resolved
… each piece a change moves

`pageText()` is now the frame's reading: the page's words outside its parts, with a
fence where each part stands. Nothing puts the parts' words together with it any more,
so the splice (`wholeReading`) and the breaks it carried across a part's edges are gone.
Every part is a datum, which the projection stamps `data-lf-gen` and so a passage cell
of its own, and on every example that draws datums each one is fenced at both edges. So
a quote in prose has no copy inside a part, and prose is searched, captured and snapped
in the frame alone. A prose line at the top of the page, which brought the whole read
back to the first scroll, now reads the frame.

The one capture that broke that rule now doesn't: a selection starting in the newline
before a datum and ending in its first line was captured as a prose anchor whose words
stand inside the datum. A selection's whitespace-only ends are no part of its passage
now, so that selection is captured on the datum, and a selection whose words cross a
part's edge is captured detached, as it already resolved to nothing.

Readers that do want a part's words read it: a search (`pageFind`) finds matches in the
frame and in each part, in reading order; a range crossing a part's edge takes its
segments from each piece it touches (`segmentsIn`); a selection snaps each end in the
reading it stands in; a drawing reads the parts that may show; a match's or a
control's context reads the piece it stands in (`readingOf`).

The quote index is kept per piece. Each piece's copies of every quote the threads name
are found by one pass over it and carried from one reading of it to the next, so a line
rewritten in a document re-places the threads quoting that line alone. This replaces
the per-part record of datum quotes (`foundUnder`, `changedUnder`), which marked every
quote in a changed part changed and had each re-placement scan the part again, and
anchor placement tracks every quote again rather than restating `resolveAnchor`'s
branch order.

Pieces a change moves:

- A batch of records forgets the parts under the nodes it changed once, climbing from
  each kept part (`forgetPartsBeneath`), so hearing rows replaced at once costs the kept
  parts rather than rows times parts.
- A slot put into or taken out of a declared tree forgets everything under its host,
  since it changes which slot shows each of the host's children.
- A change to an element's markers forgets every part under it, so a datum that comes
  to stand inside a newly marked one is no longer kept as a part.
- Whether a part holds a heading counts the headings its slots show.

The reading place takes a part to hold text wherever it stands, shown or not, so a
chart beside a document out of view stays the place; `mayShow` uses `drawn` from
shadow-tree.js; `pageRuns` and `pageBlocks` take their part filter always.

Tests: `reading-pieces.test.mjs` holds the frame and each kept part to a fresh walk of
the whole page after each kind of change, every element's kept words to a fresh reading,
a rewritten line to changing only its quotes, and hearing replaced rows to growing with
the rows; `datum-quotes.test.mjs` adds the selection from the newline before a datum
and the selection across its edge; `tests/browser/test_reading_pieces.py` holds a slot
moving a child between parts and a search across parts in a browser;
`test_reading_position.py` holds the chart beside a document out of view.

Claude-Session: https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs

@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 projection/data.js header, outside the diff, still says "Selections crossing datum boundaries remain ordinary quote anchors because they name a passage, not one fact." Since this commit, anchorForPassage captures a selection whose words reach into a part as {section, quote, detached: true}. That covers a selection across two sibling datums, and one across two datums inside the same outer datum. The description argues these were detached in effect already, because each datum is fenced. That holds, but the contract still promises the opposite, and it is the one place a widget author reads what a cross-datum selection gives. AGENTS.md's Stage asks for contract prose to change together with the code. Rewriting that sentence to state the new rule, that such a selection is kept as a detached quote, would keep the two in step.

…anged, and keep each datum reading's quotes

Fixes from the second review of the reading kept by part.

Where a range stands among the parts is now a binary search. The frame's marks are in
reading order, so the parts a range covers run from the first mark not before its start
to the first not before its end, with the part its start stands in just before them.
`segmentsIn` asked every part whether the range covered it on each call, which with
5,000 datum rows took 7.9 ms in Chromium and 14 ms in Firefox per selection change;
it now takes what main takes. A range across a part's edge gathers the frame's words
and each touched part's, each clipped by its own binary search, rather than every run of
the page. `rangeReading` and `covers` are gone into it.

A batch of records finds the kept parts it moved from the nodes it changed down: each
node's datums, and those in the declared trees under it. A node that holds a slot of the
declared tree it stands in stands over what the slot shows, the host's children, so the
parts go from that host down instead, and from that host's host where the host holds a
slot in turn, as slots pass on what they show. Hearing one paragraph put in the frame
beside 5,000 kept parts climbed from every one of them (2 ms in Chromium, 4 ms in
Firefox); it now costs what it changed.

The quote index keeps the copies in every datum reading a search asks, not only a
part's: anchor placement finds a datum's quote in that datum's own words, and a datum
inside a part, inside a line or that is a heading is no part, so its quotes were never
reported changed. `pageText(within)` registers its reading with the element it reads,
and `pieceReading` answers any element the page reads.

`partOf` and `readingOf` are now `readingPart` and `readingHolding`, since css-anchors.js
has a `partOf` and preview.js and keyboard/scopes.js a `readingOf`.

Tests (tests/runtime/reading-pieces.test.mjs):

- `a selection compares a few parts' places however many the page holds` counts the
  steps up the reading's tree a selection in prose and one across two parts take beside
  100 and 1,000 parts (170 and 230 in prose; a linear scan fails it).
- `a selection across parts reads the frame's words and theirs in order`.
- `hearing a paragraph put in the frame costs the same however many parts are kept`
  replaces `hearing rows replaced at once costs the kept parts, not rows times parts`:
  the cost it guarded now follows the nodes changed, which this one pins exactly (3
  steps beside 100 parts and beside 1,000; climbing from every kept part fails it).
- `a quote in a datum inside a part / standing in a line / that is a heading changes when
  its words move`.
- `a change over a slot reaches the parts another slot passes on to it`.
- The pieces test's page gains a slot outside the declared tree's parts that shows a
  part, with the changes `a slot that shows a part made a part` and `words edited in the
  part it shows`, and it now holds every datum's own reading to a fresh walk, not only
  each part's.

Breaking each new rule turns a test red: the binary search (a linear scan), the datums
under a changed node, the declared trees under it, the climb over a slot, the climb's
second step, the reading registered for an element, and `pieceReading` for any element.

Claude-Session: https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs
@max-sixty
max-sixty marked this pull request as ready for review November 12, 2026 02:04

@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 earlier review's point still holds at this head. The browser/runtime/projection/data.js header still says "Selections crossing datum boundaries remain ordinary quote anchors because they name a passage, not one fact." anchorForPassage now captures such a selection as {section, quote, detached: true}. That sentence is where a widget author reads what a cross-datum selection gives, and AGENTS.md's Stage asks for contract prose to change with the code. The fix is one sentence, saying such a selection is kept as a detached quote. I can push it if you'd like.

The PR also does part of backlog issue #5753 ("The page's reading follows a change to its words without rereading the whole page"). A change inside a datum now re-reads only that part. A change in prose, like that issue's counter among 1,600 sections, still drops and re-walks the whole frame, and placement still compares the frame's old and new readings. A Part of #5753 line in the description, saying that the frame is what remains, would let the review of the last part close it.

@max-sixty
max-sixty merged commit d9a8a08 into main Nov 12, 2026
5 checks passed
@max-sixty
max-sixty deleted the grind/reading-datum branch November 12, 2026 02:28
leaf-agent added a commit that referenced this pull request Nov 12, 2026
…-read (#5867)

Since #5845, Go-to reads every visible target and its filter narrows
that reading through `kept`. The rows that depend on the map (Tab,
Shift+Tab, Enter, and the filter's "No visible …" status) read the
narrowed members, but `paint()` still decided whether those rows needed
repainting by whether the *whole* reading was empty. So with a filter
active, a scroll that moved every matching target out of view while
other targets stayed left Tab and Enter offered and the "No visible …"
status unshown until some unrelated repaint, and scrolling matches back
in left the rows missing the same way. `paint()` now compares the
filtered members on both sides
([`hints.js`](https://github.com/max-sixty/leaf/blob/3ff38d97df81324e667aeb959f6e71140b6e65eb/browser/runtime/keyboard/hints.js#L829)).

The new case in `tests/runtime/hint-session.test.mjs` arms a filtered
map, re-reads it empty and then refilled, and checks that each re-read
asks for a repaint. It fails on `main` (`no repaint after the map read
one`) and passes here. `hint-session`, `hint-seating` and `go-to-keys`
pass, as does the browser type check.

The commit also fixes two comments: `passages.js` pointed at `readPage`,
which #5570 split into `collect` and `indexed`, and
`createHintSession`'s header gave a reason for members carrying their
codes that no caller has any more.

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Nov 12, 2026
…ts its draw as it lands (#5872)

After a closed tab's document draws, the document pass that follows
takes 5–10 ms in Chromium and, at the median, 6–12 ms in Firefox,
whether the page holds 3 text documents of 4 MB or 30. On main it took
340–410 ms in Chromium and 380–960 ms in Firefox, most of it re-reading
documents that hadn't changed. With the pass that cheap, #5371's
batching of closed tabs' draw reports goes: each document reports its
draw as it lands. On #4781's page the longest task while Doc 5 is
rewritten drops from 847–968 to 494–576 ms in Chromium and from
1,364–2,044 to 815–1,021 ms in Firefox, and the longest frame while the
documents draw from 882–985 to 624–738 ms in Chromium. Load times move
less: in Chromium the comment on Doc 23 stood on its words 0.2–0.9 s
sooner round for round, and in Firefox the closed tabs' times overlap
between arms, so no Firefox load-time win is claimed.

## What re-read the page after each draw

A document's draw report (`drawn`, which stamps its data's provenance)
starts a document pass. On #4781's page (30 tabs, each a 4 MB text
document, one comment quoting a line of Doc 23), three parts of that
pass read far more than the document that drew:

- **The annotation inventory** keyed the comment's margin row by reading
all of Doc 23 again (`passageRows`): 198 ms in Chromium. The key depends
only on the target's words and the passage, so it is now kept under the
target's piece of the page's reading (`keptAnswer`, from #5570), by
where the passage starts and what it says. A collection reads a target
again only when its words change or a passage in it is new.
- **Two labels** in the same collection, the outline subject and the
row's title, each collapsed and split all of Doc 23's words to cut a
60-character excerpt: 23 ms each. `excerptWords` now reads only as much
of a value as holds more than its cap once collapsed, doubling the read
until it does. On 200,000 random values (whitespace runs, surrogate
pairs, caps 1–80) it cuts the same as the whole-value rule.
- **The visual action plan** queried every element of the page for
pictures (`svg, img, figure` and declared visuals): 57 ms on 30
documents. It now asks `readingElements(visualTags())`, new in
`passages.js`, for the elements with those tags that stand where the
page reading reads its words, in reading order. Each piece's are kept
until that piece changes. The frame's are among the elements its walk
enters, which its outline already records (`over`); a part's come from
native queries of its trees and of what their slots show, keeping those
the walk reaches: 2 ms for one document. It takes tag names rather than
a selector because the watcher drops changes to `id` and `class`, so a
selector on them would go stale. On all 24 shipped examples it returns
the same candidates in the same order as the old query.

The Layout's header was found by `:scope > header` on the layout box,
which walks every element under it, 6 ms a layout read here. It is now
read off the box's children, as `passages.js` already does for `main`.

## The report batching goes

#5371 made closed tabs' documents report their draws together once the
last had drawn, because each report's pass grew with the page; its
comment called the batching a stopgap until a pass costs what the change
it follows does. With the pass at 5–12 ms, the batching only holds a
comment on a closed document back until every other document has drawn.
Each document now marks its text and dispatches `drawn` in the turn it
draws. The queue that draws closed tabs after presentation, one a task,
stays: it keeps presentation from waiting on them.

A third arm kept the batching on top of this branch's passes ("batched"
below). It placed the comment later than this branch in two of three
Chromium rounds and drew every document no sooner. Without the batching,
Firefox drew the last document 0.2–0.5 s later than main in each of
these rounds, about the 29 passes of 8–12 ms that now run as documents
draw rather than none until the last; an independent review's own
Firefox loads put the closed tabs' medians at 9.2 s and 9.8 s,
overlapping, and saw no extra long frame per document.

## Measurements

These numbers are from the first commit. The second changes only which
elements `readingElements` keeps and their order; timed again on the
30-document page, two loads per engine, its pass after a closed
document's report reads 6.3–6.7 ms median in Chromium and 8.0–10.5 ms in
Firefox, and Doc 5's rewrite 5.5–5.9 ms and 7–11 ms.

Each arm served a fresh copy of #4781's page directory from its own tree
(`leaf server run --lifetime temporary`, throwaway state home): main at
ce24dda, this branch, and this branch with main's
`lf-text-document.js` ("batched"), each on the 30-document page, and
main and this branch on a copy holding only Docs 0, 5 and 23. Rounds
alternated the arms in an order that rotated each round, one warmed
browser per round and engine (Chromium 156 headless, Firefox 157,
through Playwright), each load in a new context, load average 8–26 from
other sessions. After every document drew and the comment stood on its
words, a wheel over Doc 0 warmed the reading place, then `leaf data set`
rewrote one line of Doc 5, a closed tab. Times are milliseconds on the
page's clock; each cell is the range over three loads.

The document pass, timed by wrapping each region's paint in the arms'
runtimes (`semantic-state.js`) and summing paints that run back to back.
Main reports every closed document in one task, so it has no pass per
document; its passes over the load follow that one task.

| Chromium | main, 30 | this branch, 30 | main, 3 | this branch, 3 |
|---|---|---|---|---|
| Pass after a closed document's report, but Doc 23's: median | – | 6.4
| – | 6.5–7.9 |
| Same, longest | – | 7.5–10.3 | – | 6.5–7.9 |
| Pass after Doc 23's report, which places the comment | – | 599–610 | –
| 592–641 |
| Passes over the load after presentation, summed | 796–860 | 782–794 |
662–726 | 599–649 |
| Doc 5 rewritten: passes | 373–414 | 5.7–6.7 | 338–371 | 4.9–6.1 |

| Firefox | main, 30 | this branch, 30 | main, 3 | this branch, 3 |
|---|---|---|---|---|
| Pass after a closed document's report, but Doc 23's: median | – | 8–12
| – | 6–11 |
| Same, longest | – | 9–35 | – | 6–11 |
| Pass after Doc 23's report, which places the comment | – | 190–364 | –
| 178–1,237 |
| Passes over the load after presentation, summed | 984–1,321 |
825–1,723 | 855–1,430 | 617–3,524 |
| Doc 5 rewritten: passes | 563–961 | 6–182 | 383–610 | 6–36 |

Firefox's ranges follow the host's load average: the 182 ms rewrite and
the 1,237 ms placement are single loads at 24–26, whose arms' other
loads read 6–12 ms and 178–230 ms.

Load and rewrite as the page shows them. The longest task is the longest
gap in a chain of zero timeouts, which only main-thread work holds back.

| Chromium | main, 30 | this branch, 30 | batched, 30 | main, 3 | this
branch, 3 |
|---|---|---|---|---|---|
| Every document drawn | 3,341–4,349 | 3,179–4,203 | 3,126–4,541 |
1,251–1,634 | 1,207–1,612 |
| Comment on its words | 3,341–4,349 | 2,753–3,725 | 3,127–4,541 |
1,251–1,634 | 1,208–1,612 |
| Load: longest frame | 882–985 | 624–738 | 815–872 | 720–754 | 645–673
|
| Doc 5 rewritten: longest task | 847–968 | 494–576 | 474–522 | 667–738
| 339–379 |

| Firefox | main, 30 | this branch, 30 | batched, 30 | main, 3 | this
branch, 3 |
|---|---|---|---|---|---|
| Every document drawn | 9,367–12,465 | 9,840–13,005 | 10,402–16,421 |
2,560–4,097 | 2,567–5,266 |
| Comment on its words | 9,367–12,465 | 8,240–11,085 | 10,402–16,421 |
2,560–4,097 | 2,567–5,266 |
| Load: longest frame | 1,099–1,588 | 997–1,397 | 1,187–2,912 |
987–1,379 | 864–1,658 |
| Doc 5 rewritten: longest task | 1,364–2,044 | 815–1,021 | 812–1,744 |
1,113–1,866 | 587–1,034 |

In Firefox the time until every document has drawn is mostly the
documents' own draws: 29 frames over 150 ms, 7.4–9.9 s together, in
every arm.

Where Chromium's main thread went in the rewrite of Doc 5, from a CPU
profile of each arm, two loads each, alternated:

| ms | main, 30 | this branch, 30 | main, 3 | this branch, 3 |
|---|---|---|---|---|
| The document pass | 314–320 | 6–7 | 275–280 | 6 |
| Doc 5's own draw | 283–284 | 293–294 | 293 | 283–309 |
| Decoding the state answer | 101–105 | 103 | 10–11 | 11 |
| Garbage collection | 147–150 | 46–47 | 153–174 | 19–33 |
| Native, unattributed | 132–162 | 131–137 | 51–59 | 56–63 |
| Total | 1,084–1,128 | 680–689 | 839–862 | 422–472 |

The rewrite still costs 210–270 ms more on 30 documents than on 3, none
of it in the pass. The state answer that follows any data change carries
every source's value, since `Leaf-Data-Version` names the data as a
whole (`http.py`): 124 MB here against 12 MB. Decoding it took 108 ms
against 10 in Chromium and 143 against 24 in Firefox, besides the copy,
its comparison with the held data (`share`) and its garbage. That is the
data delivery, which this change leaves as it was.

## Checks

- `tests/runtime/reading-pieces.test.mjs`: after each kind of change it
already applies, `readingElements` returns every element of the page
with the asked tags outside chrome, each once, compared by identity,
slotted and staged ones included; its order matches the outline's; a
part's elements are those the walk reaches, in its order, leaving out a
light child no slot shows; and a change inside one part asks nothing of
another part's elements. The `elements` helper now reaches a declared
tree inside another. Keeping a part's or the frame's matches past a
change, or removing the reach filter, the reading-order sort or the
frame's read filter, turns a test red.
- `tests/runtime/annotation-inventory.test.mjs`, new test: a collection
after a change elsewhere reads none of the quoted target's elements and
keeps its key; a change to the target's words reads it again and
renumbers the key. Removing the keeping turns it red.
- `tests/runtime/words.test.mjs`, new: an excerpt of a long value cuts
as one of all of it would, past a run of whitespace and a surrogate pair
split where the first read ends.
- `tests/browser/widgets/test_code_and_documents.py`: the drawing-order
test now expects each closed document to announce as it draws, and the
test of a tab opened while documents draw becomes one that a document
reports its draw while later ones still wait. Both fail with main's
`lf-text-document.js`.
- `pnpm run test:runtime` (950 pass), `pnpm run check:browser`,
pre-commit on the changed files.
- Browser tests at this head, by `-k`: the text-document tests in
`test_code_and_documents.py` (25), the visual-target tests in
`test_render_reactions.py`, `test_render_semantic_selection.py` and
`test_render_pages.py` with
`startup/test_readiness.py::test_visual_comments_arrive_with_the_initialized_document`
(26), and the quoted-passage rows, revisions and sticky headers in
`margin/`, `test_render_anchors.py` and `test_workspaces_and_bounds.py`
(13). Three fail, each the same way on main's tree here (c5739b8):
`test_render_pages.py::test_a_marked_scrolling_visual_keeps_its_keyboard_focus_ring`
(the diagram has no `tabindex`; it also fails at ce24dda),
`test_render_semantic_selection.py::test_dense_selection_hints_stay_short_and_reach_an_atomic_visual`,
and
`test_code_and_documents.py::test_a_text_document_update_keeps_a_selection_it_carries_into_another_run[browser]`,
which passed here before the rebase onto c5739b8.

## Calls Max may reverse

- **The batching goes.** A page of many closed documents now runs a pass
per document as each draws, 5–12 ms each here, where it ran one at the
end; in Firefox the last document drew 0.2–0.5 s later than on main.
- **The plan's pictures are the page reading's.** `readingElements`
keeps the elements the walk reaches where the page reads its words, so a
picture among a declared widget's light children that no slot shows,
which nobody sees, or inside silenced words, no longer gets a "Respond
to" proxy; candidates come in reading order rather than the light tree's
followed by each shadow tree's. On the shipped examples nothing differs.

## Not verified

- WebKit.
- A comment quoting prose whose block is in the frame and holds parts,
such as words directly in a `section` of documents: its target is read
again after every change, as on main.

Part of #5194. All four costs that issue names stay open after this:
per-input work growing with the square of the widgets, images refetched
on every load, `/api/state` resending the whole state on every change
(here 124 MB after a one-line data change on the 30-document page), and
forced layout while scrolling past comment threads. This PR cuts a
neighbouring cost, the passes after a closed tab's document draws.
max-sixty added a commit that referenced this pull request Nov 12, 2026
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 #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.
max-sixty pushed a commit that referenced this pull request Nov 12, 2026
… as capture writes them (#5884)

Since #5570, an anchor that carries a quote but no `datum` is searched
only in the frame, the page's reading with each datum part left out.
Diff lines and a text document's words are datums, so the two tests in
#5882 lost their mark. Neither producer writes that anchor any more.
Browser capture gives words in one datum a `datum` anchor, and captures
a non-datum quote over part words as `detached`
([`anchor-resolution.js`](https://github.com/max-sixty/leaf/blob/8d50eb00e9abce46f13f23656128f5178759e97c/browser/runtime/anchor-resolution.js#L415-L427)).
`leaf thread create --quote` refuses words in a data widget's body ("§
doc holds no quotable text…", and "the page doesn't say …" for the diff
line). Only the tests, which write the log directly, still seeded the
old shape.

So the tests now seed the anchor capture writes: `{section: "how-patch",
datum: '["gateway/limits.py","new",2]', quote}` and `{section: "doc",
datum: "document", identity: "document", quote}`. I read these keys off
the rendered widgets. The behavior each test protects is unchanged. A
passage a widget draws into its shadow tree is marked inside that tree.
Its thread sorts where the widget stands. Hiding annotations gives the
words their syntax colour back.

Verification on Linux:

- `test_a_thread_on_words_a_widget_renders_stands_where_the_widget_does`
fails on main ("the fixture's passage is not marked inside a shadow
tree") and passes here.
- `test_o_gives_marked_words_their_own_colours_back[browser]` times out
on main at the `lf-mark` wait and passes here. Its Firefox and WebKit
variants pass here too.

The fix keeps #5570's design, in which a quote in a datum is found in
that datum alone. It doesn't add a frame-then-parts fallback for
datum-less quotes, which would bring back the whole-page reads #5570
removed, for an anchor shape nothing writes.

Closes #5882 — automated triage

Co-authored-by: leaf-agent <[email protected]>
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