Skip to content

Find a selection's words from its two ends, so selecting on a large page takes seconds, not minutes - #5441

Merged
max-sixty merged 4 commits into
mainfrom
grind/selection-scan
Nov 5, 2026
Merged

max-sixty merged 4 commits into
mainfrom
grind/selection-scan

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

segmentsIn maps a range to the page reading's segments for every selection capture.
It asked each segment of the reading whether the range covered it, and each question
compared two ancestries. On a served page of 30 lf-text-documents holding 1.5 million
lines (2,033,035 segments), a V8 profile of one triple click in Chromium puts 206 of its
228 sampled seconds in segmentsIn, reached through selectionAnchor from the response
field's placement (anchorGeometry), the key hints (hasPageSelectionTarget), the
capture (remember) and updateFab.

A range covers one contiguous run of the reading's segments, so two binary searches with
beforePoint now find the first and last segment of that run. fencedAt, which stops a
datum's quote context at the datum's edges, finds the element's run the same way and then
trims words a slot inside it renders from its host, using the under test it filtered
with before. segmentAt's binary search moves onto the same helper, firstPassing.

The search needs the segments sorted under the comparison it asks, so boundary points are
now compared in the reading's own order instead of the rendered order. The two differ
inside a host whose undeclared shadow root slots its children out of light order, which
page-behavior.md tells page authors to do with quotable words: walk and a native
Range both take those children in light order. readingParent is the step up the tree
walk comes down, a slot only in a declared root and otherwise the composed parent.
alongClimb gives any climb its order and containment from one definition, and
renderedOrder, renderedUnder and under now come from it too. A host's shadow root
now comes before its other children, where browsers answered by memory address. So a
range with both ends in an undeclared root covers none of the host's children.

That drops a capture Firefox users had on main. Firefox keeps both ends of a selection
inside such a root when it starts and ends on words only the root shows. A shift-click,
or a drag from the root's own heading to its footer across slotted words, quoted the
slotted words on main, and c opened a comment on that passage. Now it quotes nothing,
and c comments on the page. Chromium captured nothing for these gestures on main
either, and none of the 634 local pages attaches a shadow root of its own. Keeping the
capture would mean mapping such a point to the host's children through the root's
slots, about 20 lines; this is a call Max may reverse.

Evidence

Time from a triple click on one line of that page until the page answers a
requestAnimationFrame followed by setTimeout(0), with Playwright's mouse in headless
browsers, the page served from each tree, on the same Mac at a load average of 20 to 60:

Engine main (fe91eb9) this change
Chromium 214.6 s 1.2 s
Firefox 439.9 s 3.3 s

With the change, a repeated selectionAnchor on the same selection costs 0.1 to 0.5 ms
in Chromium and 0 to 2 ms in Firefox, so the callers asking again on each selection change
add under 50 ms and get no memo. In the Chromium profile, most of what remains of the
click is the first capture's search of the page's whole raw text for other copies of the
quote (about 750 ms for the probed line; four other lines took 50 to 120 ms in either
engine), and the user-view report's render checks (scrollbarClearance, openRoots).

The change also corrects what main stores on a page-script element that slots Title before
Body and holds Body first. The stored quote against the browser's selection, for a double
and a triple click on each of three children and three drags across them:

Engine main this change
Chromium 5 of 9 9 of 9
Firefox 7 of 9 9 of 9

A differential run in the page compared the search against a linear scan using the same
comparison, over about 7,900 ranges on release-notes with fixtures for declared and
undeclared slots in both engines, and about 2,400 on worktree-diffs in Chromium. It
found no difference, and no pair of adjacent segments out of order. Against main's
rendered comparison, about 830 ranges in each engine differ. Each has an end inside an
undeclared host, except 7 in Firefox whose end lies in a child of a declared host that no
slot shows, a place no one can select. fencedAt matches main on every element it tried.

Part of #5194.

Checks run

  • pnpm run test:runtime, run with six test processes: 814 pass. The new tests:
    • a 4,096-row page counts the text nodes asked where they render while segmentsIn and
      fencedAt read one row: 8,287 on main, 59 now.
    • a page holding each way the walk reaches a word checks a range from every word to every
      later one, which fails on rendered order.
    • an undeclared host that reorders its children checks each child's words and that a
      range inside its root covers none of them, which also fails with rendered containment.
    • in page-walk-order.test.mjs, a host's shadow root stands before a child no slot
      takes, with compareDocumentPosition stubbed on both the child and the root to give
      each memory-address answer. It fails with either of the two root lines in alongClimb
      removed.
  • The browser type check (tsc --project browser/types/tsconfig.json): clean.
  • 34 browser tests that read a selection's segments, snap one, fence a datum's context or
    walk across a shadow tree: 32 pass. Both variants of
    test_agent_progress_stays_on_the_thread_control fail at the Escape that should close a
    thread card, at the same assertion on this branch's base (fe91eb9).
  • pre-commit run --files on the changed files.

Not verified

The triple-click times are one run per engine on a shared machine whose load moved during
the runs, so they show the scale of the change rather than a precise figure.

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

max-sixty and others added 2 commits November 5, 2026 00:19
…age takes a second

`segmentsIn` maps a range to the page reading's segments for every selection capture.
It asked each segment of the reading whether the range covered it, and each question
compared two rendered ancestries. On a served page of 30 `lf-text-document`s holding
1.5 million lines (2,033,035 segments), a V8 profile of one triple click in Chromium
puts 206 of its 228 sampled seconds in `segmentsIn`, reached through `selectionAnchor`
from the response field's placement (`anchorGeometry`), the key hints
(`hasPageSelectionTarget`), the capture (`remember`) and `updateFab`.

The reading holds its segments in rendered order, and a range covers one contiguous
run of them. Two binary searches with the existing `beforePoint` tests now find the
first and last segment of that run. `fencedAt`, which stops a datum's quote context at
the datum's edges, finds the element's run the same way and then trims words a slot
inside it renders from its host, using the `under` test it filtered with before.
`segmentAt`'s binary search moves onto the same helper, `firstPassing`.

A triple click on one line of that page, measured with the brief's probe
(`probe.py URL ENGINE --triple`, which times the click until the next frame answers),
on the same machine at a load average of 30 to 60:

| Engine   | main (fe91eb9) | this change  |
| -------- | ---------------- | ------------ |
| Chromium | 214.6 s          | 1.2 s, 1.1 s |
| Firefox  | 439.9 s          | 3.9 s, 3.7 s |

With the change, a repeated `selectionAnchor` on the same selection costs 0.1 to
0.4 ms in Chromium and 0 to 2 ms in Firefox, so the callers asking again on each
selection change add under 50 ms and get no memo. In the Chromium profile, most of what
remains of the click is the first capture's search of the page's whole raw text for
other copies of the quote (770 ms for the probed line; four other lines took 50 to
120 ms in either engine), and the user-view report's render checks
(`scrollbarClearance`, `openRoots`).

The covered segments are the same wherever the reading's order is the rendered order,
which is the order the walk builds it in through declared shadow roots and slots. An
undeclared open shadow root reads by its host's light children in light order, while
rendered order follows the slots they are assigned to. Where such a root slots them
out of light order, a range with an end inside that host can now get a different, and
contiguous, run than the old per-segment test gave.

`passage-boundaries.test.mjs` gains a 4,096-row page that counts the text nodes asked
where they render while `segmentsIn` and `fencedAt` read one row: 8,287 on main and 59
now. The nested-slot selection test also pins ends at the far edge of one text node
and the near edge of another, and select-all over `<html>`; both pass on main.

Co-authored-by: Claude Opus 5.5 <[email protected]>
…s their words

The search in `segmentsIn` and `fencedAt` needs the reading's segments sorted under the
comparison it asks, and they weren't inside a host whose undeclared shadow root slots
its children out of light order. `walk` and a native Range both take those children in
light order, while `renderedOrder` follows the slots. So a double click on such a child
stored no quote, or a detached quote holding its neighbour's words. `page-behavior.md`
tells page authors to slot quotable words this way.

`readingParent` is the step up the tree `walk` comes down: a slot only in a declared
root, the one kind of root `walk` crosses into, and otherwise the composed parent.
`alongClimb` gives any climb its order and its containment from one definition, and
`renderedOrder`, `renderedUnder` and `under` now come from it too, replacing three
copies of the loop. `renderedOrder` now climbs through roots as `renderedUnder` did.

A host's shadow root now comes before its other children. Those two share a parent and
no tree, and browsers order them by where each sits in memory, which could put a host's
children on both sides of its root and break the search. Firefox keeps both ends of a
selection inside an undeclared root when it selects words only that root shows, and
such a range now covers none of the host's children. The review's patch left
`renderedUnder` in `beforePoint`, and its differential run still found 2 ranges where
the search and a scan disagreed. Without reading containment, a range around a slot in
such a root stores its host's unrelated words, which the new test pins.

Quote stored against the browser's selection, for a double and a triple click on each
of three children and three drags across them, on a page-script element that slots
Title before Body and holds Body first:

| Engine   | main   | previous commit | this commit |
| -------- | ------ | --------------- | ----------- |
| Chromium | 5 of 9 | 4 of 9          | 9 of 9      |
| Firefox  | 7 of 9 | 3 of 9          | 9 of 9      |

The search against a linear scan with the same comparison, over about 7,900 ranges on
`release-notes` with slot fixtures in both engines and about 2,400 on `worktree-diffs`
in Chromium, finds no difference, and no pair of adjacent segments out of order. Against
main's rendered comparison, about 830 ranges in each engine differ, each with an end
inside an undeclared host.

A triple click on the 1.5-million-line page holds at 1.2 s in Chromium and 3.3 s in
Firefox.

Co-authored-by: Claude Opus 5.5 <[email protected]>
@max-sixty max-sixty changed the title Find a selection's words from its two ends, so selecting on a large page takes a second Find a selection's words from its two ends, so selecting on a large page takes seconds, not minutes Nov 5, 2026
…s other children

The test stubbed `compareDocumentPosition` only on the loose child. With
`if (isShadowRoot(first)) return -1;` removed, `alongClimb` asked the unstubbed shadow
root instead, which happy-dom happens to answer correctly, so no test failed. The root
now gives each memory-address answer too, mirroring the child's, and the test fails
with either of the two root lines removed on its own.

Co-authored-by: Claude Opus 5.5 <[email protected]>
@max-sixty
max-sixty marked this pull request as ready for review November 5, 2026 10:09
leaf-agent
leaf-agent previously approved these changes Nov 5, 2026
…ndex reads them

`test_each_owner_in_the_question_index_is_exported_by_its_module` reads each module's
exports with `tests/js_facts.mjs`, which takes a declared export's name only from a plain
identifier, so the destructured `export const { ... } = alongClimb(...)` hid all three
owners from it. No other module exports by destructuring, so the exports are now plain
names bound to the climb's functions rather than a second pattern the extractor would
have to learn.

Co-authored-by: Claude Opus 5.5 <[email protected]>
@max-sixty
max-sixty merged commit 642d981 into main Nov 5, 2026
4 checks passed
@max-sixty
max-sixty deleted the grind/selection-scan branch November 5, 2026 10:37
max-sixty added a commit that referenced this pull request Nov 10, 2026
Tend's PR review now checks each change against the open backlog and
gets the description to close what the change settles. Where the change
leaves nothing of a `backlog` issue to do, by finishing what remains,
removing its premise or recording a decision against what it asks, the
description says `Closes #N`. Where it does part, the description says
`Part of #N` and what is left, so the review of the last part can tell
that part finishes the issue. On Tend's own pull request the review
edits the description. On someone else's it asks in the review, and the
ask goes in an approval rather than holding one back. The rule is a new
section of `running-tend`, **Issues a change finishes**, after **Review
threshold**.

## Why

Leaf's backlog triage of 2026-11-04 found seven merged pull requests
that did an open backlog issue's work without closing it. Four said
`Part of #N`: #3879, #3902, #3856 and #3868. Three named no issue:
#4487, #4124 and #5039. Nothing asked those three to look, since
`/developing-leaf` asks for `Closes #N` only of a session that starts
from a backlog item. Tend's nightly sweep reads a seventh of the open
issues each night, long after the diff has left the reader's context,
and, by the triage's count, it commented on none of them in ten nights.
The review is the step every change passes with its diff in hand.

The replay below sorts the seven more finely than the triage did. #4011,
#4036 and #4013 were settled by #3879, #4124 and #5039. #3902 did half
of #4023, whose five composition defects stayed open. #3856, #3868 and
#4487 each did one item of #4027, which is still open. For those four
the right description is `Part of #N` with what is left. A `Closes #N`
would have been wrong.

## Replay

Each case gets the rule as this branch ships it, together with
`running-tend`'s **Review threshold** section, which it competes with.
It also gets the PR's title, author, description and diff, capped at 80k
characters with vendored files left out, and every `backlog` issue open
when the PR merged, as it read then. That list is rebuilt from the
issues' creation and close times, label events and body edit history,
plus their comments up to the merge. Each case runs once through a
headless `claude -p` on `claude-opus-5-5` (the review's model), with no
tools, in a scratch home. The JSON verdict is graded against an expected
answer fixed before the run.

| PR | What it did | Expected | Result |
| --- | --- | --- | --- |
| #3879 | the play-through #4011 asked for | close #4011, or part with
its open decision left | pass: part, the decision and the home-page test
left |
| #3902 | #4023's touch-target half | part of #4023 | pass (also part of
#4027) |
| #3856 | #4027's Android back item | part of #4027 | pass |
| #3868 | #4027's Draw mode item | part of #4027 | pass |
| #4487 | #4027's lf-draft back item | part of #4027 | pass |
| #4124 | recorded the call that settled #4036 | close #4036 | **miss**:
read #4036 and judged the call unrelated |
| #5039 | one held-news count, #4013 | close #4013 | pass (also part of
#4012) |
| #5331 | removed #4060's premise | close #4060 | pass |
| #4643, #4718, #4847, #5020, #5252 | nothing in the backlog | nothing |
5 pass |
| #5441, #5446, #5454 | rightly a part of an open issue | that part | 3
pass |
| #4212 | rightly said `Closes #4037` | close #4037 | pass |

That is 6 of the 7 triage cases, the premise case, and 9 of 9 controls.
No run closed or named an issue it shouldn't have. #5020 is a hard
negative: it changes held-news notices, which #4013 is about, and the
run left #4013 alone. #4124's miss held in all three wordings tried. The
triage itself only inferred that the 2026-10-25 call in
`reading-place.js` covers #4036's case.

The clause for a removed premise or a recorded decision goes beyond the
two cases Max named. It is there because the triage closed several
issues as obsolete after a landed change removed their premise. Without
the clause, the #5331 run read #4060 and declined: "It only makes one
sentence of the issue's background out of date". With it, the run closed
#4060.

Two grades were corrected after the first run, each on evidence from the
record. #5252 says `Part of #5194`, but #5194 was labelled `backlog`
only on 2026-11-05, after #5252 merged. #5039 left a count that gains a
digit outgrowing its room, as its own `thread-card.js` comment says, and
#5288 fixed that on 2026-11-04, so `Part of #4013` naming that is also
right at merge time. The final wording closed #4013 anyway.

```sh
uv run python .tmp/replay/replay.py --jobs 4
uv run python .tmp/replay/replay.py --only 5331,4124 --rule-file .tmp/replay/arm-finishes-only.md --runs runs-finishes-only --results results-finishes-only.json
```

The final wording's 17 runs cost $6.75. All runs together cost $19.64,
across three wordings and the two-run ablation. The script, cases,
prompts, answers and results are in `.tmp/replay/` of the worktree that
wrote this PR, not committed. The last run used this text before one
sentence was split in two.

## Decisions Max may reverse

- **Where the rule lives.** It lives in `running-tend`, which only Tend
loads. `/developing-leaf`'s `Closes #N` under **Take a backlog item**
stays as it is. That rule claims an item early: the closing keyword is
what makes `TODO.md`'s `-linked:pr` query skip it. The review then
corrects a claim the change only partly fulfils to `Part of #N`.
Repeating the search in the author's skill would make every Leaf session
pay for a check the review already makes on every PR.
- **How it acts on others' pull requests.** The review asks and doesn't
edit, since the author owns the description and Leaf sessions rewrite it
whole from a file. The ask doesn't withhold approval. `tend-review` runs
on `opened`, `synchronize`, `ready_for_review` and `reopened`, not on
`edited`, so a held approval would wait for the next push. The review
approves with the ask as its body.
- **Scope.** Only issues labelled `backlog`. CI failure reports and
cause issues close by their own routes.

## Not verified

- **Use inside a full review.** The replay gives the section on its own,
with the backlog inline. It shows that the rule, once applied, picks the
right issue. It doesn't show whether a whole Tend review, with the
bundled review skill in context, remembers to apply it. Tend's next
reviews will show that.
- **What-is-left lists.** The replay leaves out the earlier PRs that
name each issue, which the rule tells the review to read. So its lists
repeat items that earlier parts had done. #5454's ask lists #4027 items
that #3856 and #3868 had finished.
- **Repeat runs.** Each case ran once.

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5.5 <[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