Skip to content

Read state once per change: news names the state read's reading, and the page asks only for a reading it lacks - #5453

Merged
max-sixty merged 10 commits into
mainfrom
agent-afab409f4e934ffea
Nov 5, 2026
Merged

max-sixty merged 10 commits into
mainfrom
agent-afab409f4e934ffea

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

An agent revises a page by saving index.html and then running leaf page stamp. On main, each revision cost the open tab 3.4 state reads on the corpus page, when two carry everything: one for the revision and one for the stamp. Each extra read re-sent a reading the tab already had, and held the page's log lock while the server built it. With this change a revision costs two reads.

Measured

leaf-dev bench-latency --page corpus, main (3adac4668) against this branch at 6d3f8141d. The runs were traced in the browser, and every hold of the page's log lock was recorded on the server. 20 revisions per arm, in four batches on 2026-11-05 between 00:08 and 00:32 PT, at a one-minute load of 14–42. The commits after that change only what the clock does after a failed read, which these runs never hit, and one test, and merge main.

per revision main this branch
state reads 3.4 (3–4) 2.0 (2 in every run)
state bytes, median 184 KB 87 KB
log-lock time in state reads and activation, median 510 ms 426 ms
  • Lock time: main made 28 extra reads, each holding the log lock 17–303 ms (median 50), about 105 ms per revision. The source check itself, about 400 ms, now runs in the news look that first sees the save, instead of in the first state read.
  • Gestures: over 20 comments and 20 card moves per arm, main read again after 4 comments. This branch read again after 1 move. The trace's timing suggests the look heard the gesture's news just as the gesture's own answer arrived.
  • Startup (leaf-dev probe corpus --base, 3adac4668 against 288f7b7d3, three rounds): by the time the page first shows the server's state, both arms have made 527 requests (528 in one round of main's). This branch loads 2,627 more bytes, all of it the larger state-feed.js and state-application.js source modules.

The server: news takes its token the way a state read does

/api/news stamped the page's files before activating a saved source. /api/state activated it first, which writes the revision, and then stamped. So after every save, the look named one reading and the state read answered with another, and the next look asked again for the reading the tab had just been handed.

Both now take their token through LivePage._activated: activate, then stamp.

  • State read: passes the transaction it holds.
  • News look: stamps the page once under the log's lease (reading.page_readings, which also gives the source_readings of those stamps). If the held activation covers them (revisioning.held_activation), it returns them, since activating would change nothing they name. Otherwise it takes the transaction, activates and stamps again.

A look at an unchanged page costs what it did on main: 1.35 ms against 1.40 ms on a built corpus page.

The browser: compare news with the reading in hand

The look and the 2 s clock compared the news token with runtime.reading, which moves only once an answer is adopted. A revision's answer takes seconds to prepare on the corpus, so a look or tick in that window asked again for a reading the page already had.

  • The reading in hand: state-application.js keeps every answer handed to receiveState, through any door, until its application settles. stateInHand() returns the newest of those and the adopted state, by when the server took it. The look asks only when the news names a reading other than that one.

  • The read slot's queue: it records which news a trailing read is for. When the slot is given back, the trailing read is skipped if the answer just handed in already names that news. A read the clock started before a save can wait out the save's activation and answer with its revision.

  • The clock: it asks only with no read in flight, and only for one of three reasons:

    • a failed read;
    • an activity transition the state in hand says has come due;
    • the last look heard news naming another reading than the one the page still holds.

    It still needs that last check: a dropped answer leaves the page behind the news with no change in the news.

Calls you may reverse

  • state_read drops with_token and always takes the token. A comparison view (/api/view) now stamps the page's files once more than before, and test_comparison_revision_reads_stay_inside_the_page_transaction no longer asserts that it doesn't.
  • A browser test Send a click only the records it changed, and read again only what changed #5307 broke is fixed here. test_a_presence_read_crossing_freshness_returns_to_the_current_lease failed on main with KeyError: 'listening', because its route read listening from what is now a patch. It now asks for a whole answer, and passes. Its page is unclaimed, so the lease it takes moves the files part of the token, and the look repairs the crossing without the clock.
  • One rare extra read is left. News shares one presence observation for 2 s, while a state read observes presence afresh. On a claimed page, a state read that catches a short-lived lease names a reading the news never names, and the clock reads again on each tick until the news observation expires, about 2 s. Letting the state read write its observation into the news cache would remove it, but would delete nothing, since dropped answers still need the clock.

Not in this change

On the corpus page, a revision read straight after a reload was a whole 186 KB answer rather than a patch, in revision-only runs at ecf4dc10d, even though the request carried Leaf-Held-Basis. The likely cause, from a count and the code rather than a direct test: the page's 21 live samples read state between the reload and the revision, and served_state/tail.py remembers readings for only 8 pages (PAGES), so the page's own reading is gone by then.

Checks

On the branch merged with main at 7607555fc:

  • pnpm run check passes, including the 811 runtime tests and 88 Worker tests.
  • 105 Python tests on activation, readings, news and state answers pass, by -k selection.
  • 34 browser tests that read the news route or count state reads pass, by node id.

Each new runtime and Python assertion fails on main's code. receiveState's in-hand bookkeeping has no unit test of its own: the runtime test's feed stub models it, and the gesture counts above check it end to end.

test_status_changes_coalesce_behind_one_state_read expected a trailing read after an answer that already named the latest news, which is the read this change removes. It now fulfils the first read with an answer taken before the later writes, so the burst still queues one trailing read.

Three browser tests fail here and fail the same way with this branch's runtime and server files set back to 3adac4668, so this change doesn't cause them. A search found no open issue or pull request for them:

  • test_required_page_failure_retains_composer_seat_focus_and_caret
  • test_a_reload_mid_flight_never_wedges_round_trip
  • test_an_accepted_event_is_not_retried_when_its_state_cannot_render

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

…ge asks only for a reading it lacks

A page revision written the way an agent writes it, saving `index.html`
and then running `leaf page stamp`, cost the open tab 3.5 state reads on
the corpus where two carry everything, one for the revision and one for
the stamp. Each extra read re-sent a reading the tab already had and held
the page's log lock while the server built it. Main against this branch,
`leaf-dev bench-latency --page corpus`, 20 revisions each, load 23–64:

| per revision | main | branch |
|---|---|---|
| state reads | 3.5 (3–5) | 2.0 (2–2) |
| state bytes, median | 327 KB | 141 KB |
| log-lock ms in reads and activation, median | 554 | 436 |

The 29 duplicate reads on main each held the lock 11–407 ms (median 44),
105 ms per revision. On 20 gestures each (10 comments, 10 card moves),
main read again after 3 and this branch after none.

## The server: news takes its token the way a state read does

`/api/news` stamped the page's files before activating a saved source,
while `/api/state` activated it first, which writes the revision, and
then stamped. After every save the look named one reading and the state
read answered with another, so the next look asked again for the reading
the tab had just been handed.

Both now take their token through `LivePage._activated`: activate, then
stamp. A state read passes the transaction it holds. News stamps the page
once under the log's lease (`reading.page_readings`, which also gives the
`source_readings` of the same stamps) and returns those stamps when the
held activation covers them (`revisioning.held_activation`), since
activating would change nothing they name. Otherwise it takes the
transaction, activates and stamps again. A look at an unchanged page
costs what it did on main (1.35 ms against 1.40 on a built corpus page);
the look after a save now runs the source check that main's first state
read ran.

`state_read` now always takes the token, so `with_token` is gone. A
comparison view (`/api/view`) stamps the page's files once more than
before, and `test_comparison_revision_reads_stay_inside_the_page_transaction`
no longer asserts that it doesn't.

## The browser: compare news with the reading in hand

The look and the 2 s clock compared the news token with
`runtime.reading`, which moves only once an answer is adopted. A
revision's answer takes seconds to prepare on the corpus, and a
gesture's POST answer some milliseconds, so a look or tick in that window
asked again for a reading the page already had.

`state-application.js` now keeps every answer handed to `receiveState`,
through any door, until its application settles, and `readingInHand`
names the newest by `taken` of those and the adopted state. The look asks
when the news names a reading other than that one. The clock's crossing
repair asks only when the last look heard the news name another reading
than the one in hand and the page still holds that same reading, with no
read in flight. The repair stays: a dropped answer leaves the page behind
the news with no news change, and on a claimed page a state read that
catches a short-lived lease names a reading news never names, since news
shares one presence observation for 2 s and a state read observes it
afresh.

The bookkeeping in `receiveState` has no unit test: the runtime test's
feed stub models it, and the bench's gesture counts above check it end
to end.

`test_a_presence_read_crossing_freshness_returns_to_the_current_lease`
has failed on main since #5307 with `KeyError: 'listening'`: its route
read `listening` from the answer, which is now a patch from the tab's
reading. It now asks for a whole answer, and passes. Its page is
unclaimed, so the lease it takes moves the files half of the token and
the look repairs the crossing without the clock.
…the news

On the merged tree the A/B still read three times on 2 of 20 corpus revisions. In both, the 2 s clock asked for state while the look that had heard the save was still activating it. The clock's read waited for the activation's lock and answered with the revision. Meanwhile the look's news, arriving while that read was out, queued a trailing read for the same reading.

The read slot's queue now records what the trailing read is for: the news a look heard, or nothing in particular for the clock or a failed look. When the slot is given back, a read queued for news the answer just handed in already names is skipped.

The fold test double in `tests/model_folds.py`, new on main since this branch began, still took `with_token`; it now yields its token on every read, like the sources it stands in for.
… flight

The clock asked for state whenever the adopted state's activity transition had come due, including while a newer answer that settles it was being prepared, and queued a trailing read behind a read in flight. With a due transition and an answer in hand, state reads went 1, 2, 3 over two ticks.

`state-application.js` now exposes `stateInHand`, the newest state the page has adopted or has in hand, in place of `readingInHand`; the feed derives the reading from it. The clock asks only with no read in flight, and judges the transition on the state in hand.
After a failed read, the clock queued another read behind the one already in flight, so a slow answer was followed by a repeat of the same read. The clock now asks for none of its reasons, a failed read, a due activity transition or a crossed reading, while a read is in flight; if that read also fails, the next tick asks.
…e news

`test_status_changes_coalesce_behind_one_state_read` expected a trailing read after an answer that already named the latest news, which is the read this branch removes. It now fulfils the first read with an answer taken before the later writes: the burst still queues one trailing read, that read takes the latest state, and the page asks for nothing more.
@max-sixty
max-sixty merged commit 05498e0 into main Nov 5, 2026
4 checks passed
@max-sixty
max-sixty deleted the agent-afab409f4e934ffea branch November 5, 2026 09:28
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