Skip to content

Send a tab only the data source a write changed - #5992

Merged
max-sixty merged 2 commits into
mainfrom
grind/changed-sources
Nov 15, 2026
Merged

max-sixty merged 2 commits into
mainfrom
grind/changed-sources

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

After leaf data set on one source, the server sent every source's value to
every tab. On the page from #4781 (30 lf-text-document tabs, each bound to its
own source of about 4 MB), a one-line change to one document sent 119 MB per
tab.

The cause was that served_state.tail left data out of its patch and sent
data whole beside every tail, while the one saving, Leaf-Data-Version, was
all or nothing: a tab that named the version it held got no sources back, and
any change to any source sent them all.

Data now joins the patch like the rest of the reading. Tail compares each
source's value by its revision, the digest data.read_data already takes of
it, so it never encodes a value to compare it; a source whose revision moved
travels whole at data.sources.<id>.value, a rerun of the same value sends only
its new run and updated, and a cleared or new source is a removal or an
addition. The browser applies those changes to the reading it holds
(completeState), so every unchanged source keeps its identity and its readers
skip it.

Leaf-Data-Version still saves the sources on a whole answer, which a tab gets
from a server that never served or has forgotten its reading, and from
leaf.page's edge for stored answers. http.sent now cuts them after the tail
has remembered the reading whole; cut earlier, as page_state did, the
remembered reading would hold no sources to compare the next one against.

The served-state contract changes with it: a Tail no longer carries data.
state.schema.json, the generated contract.d.ts, the tail and http
docstrings and layer-client.js say so. No protocol bump: the event log and
records kept outside a page are unchanged. A source's revision and the data
version each have one home, data.value_revision and data.data_version,
which read_data and the model fixture served_data both call.

Measured on this Mac with a Playwright script that builds the #4781 page under
a private state home, changes line 1 of the shown document's source with
leaf data set, and reads the state answers the tab receives until it draws
the new revision (base is the merge base, 7ec8751). Chromium ran three times
per arm, Firefox once:

30 documents, one source changed base head
bytes the tab receives 119,172,109 3,939,521
JSON.parse, Chromium 37, 37, 60 ms 1.0, 1.2, 1.6 ms
JSON.parse, Firefox 150 ms 4 ms
main thread, Chromium 407, 489, 681 ms 261, 282, 295 ms
write to drawn, Chromium 0.66, 0.76, 1.71 s 0.31, 0.31, 0.43 s
write to drawn, Firefox 1.24 s 0.59 s

On a 3-document page the base sends 11.8 MB for the same change and the head
3.9 MB, and the head's main thread spends 261 ms, about what it spends on 30
documents. So the time that remains follows the changed document rather than
the page: a CPU profile of the 282 ms run puts 207 ms in lf-text-document's
reconcileRuns, reconciling all 50,000 rows of the rewritten source.

Every run, base and head, also checked that the shown document holds the
change, that it still does after a reload, and that a change to a source in a
closed tab shows when that tab opens, with every other document at its own
revision.

Tests: a model test that a write to one of 2 or 30 sources patches only that
source and a rerun sends no value; a served tail case that rewrites, clears and
sets sources, which the runtime test completes to the whole answer with the
unchanged source kept by identity; and a server test that a tab holding a
reading whose sources were cut by Leaf-Data-Version gets only the rewritten
source. test_new_data_in_a_stale_event_response_is_still_accepted rewrites a
state answer's data, so it now asks for a whole answer (fetch_whole_state),
as render_harness says such a test should.

Checks run: uv run pytest tests/model/test_model_folds.py, a -k selection of
tests/process/test_interact_server.py, tests/process/website_server/ and
tests/model/test_obligations.py for data, held readings and answers, the
schema-checking model tests (test_seen, test_render_export,
interact_contract/test_admission), ten browser tests in
startup/test_projected_data.py, startup/test_freshness.py and
drafts/test_revisions.py that write data on an open page or read a tail,
pnpm run test:runtime and pnpm run check. The new model test fails against the
base tail.py, and the new server test against the base http.py.

Not run: the website's delivery checks (leaf-dev verify-site website-worker),
which this pull request's test job runs. The Worker is unchanged: its stored
answers are whole, and it still cuts their sources by Leaf-Data-Version.

Known cost, left as it is: under a page-root prefix, as on leaf.page and in
live samples, http.scope_page_urls walks every data value in the reading on
each state answer, about 27 ms per 50,000-row structured source (inferred end
to end, not measured in isolation). A text source's value is a string, which
the walk passes over.

Call to review: Leaf-Data-Version stays, for whole answers only. Dropping it
would leave one mechanism, but a restarted server's first answer and every
leaf.page version switch would then resend all of a page's data.

Part of #5194

After `leaf data set` on one source, the server sent every source's value to
every tab. On the page from #4781 (30 `lf-text-document` tabs, each bound to its
own source of about 4 MB), a one-line change to one document sent 119 MB per
tab.

The cause was that `served_state.tail` left data out of its patch and sent
`data` whole beside every tail, while the one saving, `Leaf-Data-Version`, was
all or nothing: a tab that named the version it held got no sources back, and
any change to any source sent them all.

Data now joins the patch like the rest of the reading. Tail compares each
source's value by its `revision`, the digest `data.read_data` already takes of
it, so it never encodes a value to compare it; a source whose revision moved
travels whole at `data.sources.<id>.value`, a rerun of the same value sends only
its new `run` and `updated`, and a cleared or new source is a removal or an
addition. The browser applies those changes to the reading it holds
(`completeState`), so every unchanged source keeps its identity and its readers
skip it.

`Leaf-Data-Version` still saves the sources on a whole answer, which a tab gets
from a server that never served or has forgotten its reading, and from
leaf.page's edge for stored answers. `http.sent` now cuts them after the tail
has remembered the reading whole; cut earlier, as `page_state` did, the
remembered reading would hold no sources to compare the next one against.

The served-state contract changes with it: a `Tail` no longer carries `data`.
`state.schema.json`, the generated `contract.d.ts`, the tail and http
docstrings and `layer-client.js` say so. No protocol bump: the event log and
records kept outside a page are unchanged.

Measured on this Mac with a Playwright script that builds the #4781 page under
a private state home, changes line 1 of the shown document's source with
`leaf data set`, and reads the state answers the tab receives until it draws
the new revision (base is the merge base, 7ec8751). Chromium ran three times
per arm, Firefox once:

| 30 documents, one source changed | base                | head             |
|----------------------------------|---------------------|------------------|
| bytes the tab receives           | 119,172,109         | 3,939,521        |
| JSON.parse, Chromium             | 37, 37, 60 ms       | 1.0, 1.2, 1.6 ms |
| JSON.parse, Firefox              | 150 ms              | 4 ms             |
| main thread, Chromium            | 407, 489, 681 ms    | 261, 282, 295 ms |
| write to drawn, Chromium         | 0.66, 0.76, 1.71 s  | 0.31, 0.31, 0.43 s |
| write to drawn, Firefox          | 1.24 s              | 0.59 s           |

On a 3-document page the base sends 11.8 MB for the same change and the head
3.9 MB, and the head's main thread spends 261 ms, about what it spends on 30
documents. So the time that remains follows the changed document rather than
the page: a CPU profile of the 282 ms run puts 207 ms in `lf-text-document`'s
`reconcileRuns`, reconciling all 50,000 rows of the rewritten source.

Every run, base and head, also checked that the shown document holds the
change, that it still does after a reload, and that a change to a source in a
closed tab shows when that tab opens, with every other document at its own
revision.

Tests: a model test that a write to one of 2 or 30 sources patches only that
source and a rerun sends no value; a served tail case that rewrites, clears and
sets sources, which the runtime test completes to the whole answer with the
unchanged source kept by identity; and a server test that a tab holding a
reading whose sources were cut by `Leaf-Data-Version` gets only the rewritten
source. `test_new_data_in_a_stale_event_response_is_still_accepted` rewrites a
state answer's `data`, so it now asks for a whole answer (`fetch_whole_state`),
as `render_harness` says such a test should.
`data.py` computed a value's revision inside `_publication` and the data
version inside `read_data`, so the model fixture `served_data` copied the
revision digest exactly and the version digest only roughly, leaving out the
contract, error and validity the real one covers. Both now live in
`data.value_revision` and `data.data_version`, which `read_data` and the fixture
call, so a fixture reading carries the version the server would compute.

`http.sent` now cuts a whole answer's sources by testing `"since" not in
answer`, the field a `Tail` always carries, rather than `"data" in answer`,
which told a patch from a whole answer only because a `Tail` happens to carry
no data.
@max-sixty
max-sixty marked this pull request as ready for review November 15, 2026 00:21

@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 description says Part of #5194 but not what of #5194 is left after this change. This PR covers part of its section 3, the data sources a state answer resends. Sections 1, 2 and 4, and whatever else section 3 still needs, are still open. Could the description say what remains, so that the review of the last part can tell it closes the issue?

@max-sixty
max-sixty merged commit 41e9c73 into main Nov 15, 2026
6 checks passed
@max-sixty
max-sixty deleted the grind/changed-sources branch November 15, 2026 00:35
max-sixty added a commit that referenced this pull request Nov 15, 2026
… costs (#6033)

`leaf-dev bench` now generates #4781's page, a side list of tabs each
holding a large text document bound to its own source, and measures the
inputs that found this week's costs on it by hand. The daily `bench` job
runs it, so Tend's review sees these costs grow with the page.

## The page

`documents` holds the shown document, one more in a closed tab for each
eight of `n`, and one more again: 3 documents on the BASE page, so
`reveal` has two closed tabs to open there, then 10, 18 and 34 at the
daily sizes 64, 128 and 256, about #4781's 30 tabs at the largest. Each
source is 2,048 lines of Python (16 of a listing's 128-row runs), the
same at every size. So a cost that grows with the documents a page holds
grows with `n` and shows in the exponent. A cost that grows with the
length of the one document opened stays level and shows only beside
another arm (`--base`).

The largest page holds 69,000 rows where #4781's held 2.9 million. That
is enough for a walk of every panel on each tab switch (#5996) to fit an
exponent in Chromium, and it keeps the daily run short (below). The text
is Python rather than #4781's Markdown, because Python's syntax colour
paints most words, and the colour a closed tab keeps is what `browse`
reads.

The sources are set through the fixture's data companion
(`page.data.json`, which `prepare_page` sets with `leaf data set`), so
each arm builds the page with its own Leaf. `Collection` gains `data`
for it, which `server_paths.build` also sets in process.

## The inputs

| Input | What it does | Fix it watches |
|---|---|---|
| `data` | the agent's `leaf data set` changes the first line of the
shown document's source; `painted` is the frame that draws the new line
| #5992 |
| `reveal` | a click opens the second tab, then the third, neither
opened before | #5996 |
| `search` | the first `/`, which opens page search | #5999 |
| `matches` | type `value`, which every line holds, into page search,
until the count is final | none yet |
| `browse` | put away page search, open every closed tab, then turn the
wheel a run's height down and up the last one's listing, 16 steps |
closed tabs keep their colour ranges (still on main; #6035) |

They run in that order, so `search` and `matches` read the page before
`browse` has opened every tab, whose kept colour every later input would
meet. They need the page's own ids (`#docs #d0[source]`) and find the
other tabs and documents by them. Each finds what it acts on through
`page.evaluate`, never a locator, since a Playwright locator walks every
element of the page for each query. `holds`, which decides whether a
page has what an input needs, now asks the document with `querySelector`
for the same reason. `painted.js` gains three facts: `shows` (an element
is drawn and holds some words), `searching` (page search's field holds
the caret) and `searched` (its count is final). `BenchPage.written`
takes the file a command writes, here the source's file rather than the
log.

## What the marks hold

`held` now covers the requests and bytes any input causes, the size of
the answer to each of its POSTs (`<input> event KB`) included, besides
the CPU of an input the browser answers alone: root `AGENTS.md`,
"Responsiveness", holds an input's work to what it changes. Without it,
#5992's cost, every source sent to the tab after a write to one, fits
exponent 1.08 with no mark, since an agent's write may grow with the
page. An input whose result is the whole page is exempt, through a new
`Input.whole`: `revision` and `stamp`, which carry the new document, and
`matches`. A held count's noise floor is a quarter of its own reading on
the BASE page and at least one, where every count's was a fixed 10 KB or
one request.

Re-judged under main's rules and this branch's, the nine daily reports
of 11-06 to 11-14 differ in one reading: `asks` `comment event KB`, 4.9,
8.1, 11.9 and 19.6 KB across the sizes in the six reports of 11-06 to
11-11, now marked at 1.08. Those reports showed it with no mark; it
reads level from 11-12 on. On this page's runs the only differences are
`matches`, which `whole` lets grow, and `data KB` with #5992 reverted.

## Evidence

All on this Mac (M5 Max, 18 cores, load average 3–21 from other
sessions), `uv run leaf-dev bench documents --sizes 64,128,256 …`.

Current main, Chromium, the daily job's command (`--rounds 2 --input
data --input reveal --input search --input matches --input browse`), 266
s:

```
                            8         64        128        256   exponent
elements               6435.0    20988.0    37620.0    70884.0         - 
load cpu ms             200.0      242.8      299.5      510.5      1.43 
data cpu ms              23.1       21.3       25.5       30.7         - 
reveal cpu ms            46.0       42.0       43.9       47.1         - 
search cpu ms            18.3       17.3       18.4       19.7         - 
matches cpu ms          290.5      718.7     1271.1     2328.5      1.13 
browse cpu ms            15.4       19.5       25.4       28.0      0.81!
data painted ms         133.5       99.9       68.6       99.5         - 
data requests             1.0        1.0        1.0        1.0         - 
data KB                  82.6       82.6       82.6       82.6         - 
reveal painted ms        37.2       33.5       34.8       36.1         - 
reveal requests           0.0        0.0        0.0        0.0         - 
reveal KB                 0.0        0.0        0.0        0.0         - 
```

Current main, Firefox, the first commit's page and input order with
`--browser firefox`:

```
                         8         64        128        256   exponent
elements            4351.0    18904.0    35536.0    68800.0         - 
load cpu ms          344.8      521.9      557.5      917.8      0.85 
data cpu ms           40.6       56.2       43.4       52.0         - 
reveal cpu ms         73.5       78.3       65.0       71.7         - 
browse cpu ms         43.9       51.4       54.6       68.1         - 
search cpu ms         37.5       61.3       40.8       48.5     -0.56 
matches cpu ms        98.3      115.0      201.3      287.1      0.87 
```

What main shows, with a V8 profile of each growing input from
`--profile`, which lists the functions whose time grew past the input's
limit from the smallest page to the largest:

- **`browse`, 0.81 `!` in Chromium**, and 0.96 and 1.04 in two runs of
`browse` alone. A closed tab keeps the colour ranges of the runs it last
showed (896 a tab here), and each run a step colours rebuilds highlights
holding every opened tab's ranges. The profile names `unpaint` and
`follow` (`lf-text-document.js`), 3 to 40–45 ms. This is the cost
#5996's body left unmeasured, which #6035 removes: with its change,
review read `browse` level. Firefox grows too (44 to 68 ms), but under
the noise floor in one of two runs.
- **`load cpu`**, 1.43 here and between 1.02 and 1.71 across seven runs
on main, above 1.5 in two. The profile names an anonymous function at
`addressable-names.js:187`, `headingsIn`'s query of every heading under
an element, growing from 1 to 126 ms (exponent 1.54). Read from the
code, not confirmed: each document's first draw may start a new page
reading, whose heading queries walk the whole page again.
- `matches` grows at 1.13 in Chromium and 0.87 in Firefox, within its
limit as a `whole` input. Chromium's 2.3 s at 34 documents against
Firefox's 0.29 s is #5999's note that a match's visibility read styles
each run it lies in.
- `search`, `data` and `reveal` stay level in both engines. In the first
commit `search` ran after `browse` and grew with the tabs `browse` left
coloured (0.79 `!`); run before it, it reads 17 to 20 ms.

Each fix reverted in the working tree (`git revert --no-commit`), with
`--base HEAD` for main beside it. The #5992 and #5999 rows are from the
first commit's page, which held one document fewer:

- **#5992**, Chromium: `data KB` goes 164, 736, 1,398, 2,722 KB with the
fix reverted, exponent 1.08 `!`; main reads 82.6 KB at every size.
- **#5996**, Chromium, two rounds, this head's page and `reveal`:
`reveal cpu` goes 48, 50, 63, 84 ms with the fix reverted, exponent 1.25
`!`; main reads 48, 43, 51, 49 ms. On the first commit's page, whose
`reveal` reopened the first tab for its second sample, the revert read
1.77 `!`. In Firefox the revert raises `reveal cpu` to 210–290 ms at
every size from 60–90 ms, level rather than growing, since the Firefox
half of #5996 (the fold rule) scaled with the opened document's length,
which this page holds fixed.
- **#5999**, Firefox, two rounds: `search cpu` goes 45, 83, 92, 165 ms
with the fix reverted, exponent 0.82 `!`; main reads 26, 30, 28, 24 ms.

## CI time

The daily job measures `documents` in a step of its own after the
default pages, with only these five inputs, since the `document` page's
text already grows under every other input. The step runs after a failed
one, and the readings to chart are collected once both have run, so one
page's failure keeps the other's chart. Its table goes to
`documents.txt` in the `bench-report` artifact, which Tend's benchmark
review (`.claude/skills/running-tend/SKILL.md`) now names beside the
browser and server tables. The command took 266–284 s here. To scale it
to the runner: the `rows` page with the daily job's arguments took 397 s
here and 7.5 minutes on the runner on 2026-11-14, a ratio of 1.13, while
the runner's load CPU is 2.2 times this Mac's. So the job should grow by
between about 5 and 10.5 minutes, nearer the first, since most of a run
is waiting rather than CPU.

## Not verified

- The runner's time for the new step, which the first daily run's step
timings will show.
- Firefox on CI. The Firefox clock (`ContentClock`) reads macOS's thread
times, so the daily job reads Chromium only, and a Firefox-only cost
such as #5999's shows only in a local run.
- WebKit.

No `dev/tests` smoke test: the bench has none, and each input ran here
in both engines at four sizes.

## Calls Max may reverse

- `held` covering requests and bytes, with `Input.whole` to exempt a
revision and a search.
- The CI step's input list, rather than every input on this page.
- Python content, 2,048 lines a document and `2 + n/8` documents.
- `browse`'s 16 steps, which over two runs fitted 0.96 and 1.04 where
three runs of 8 fitted 0.49 to 0.75.

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