Skip to content

Read each revision and each logged fragment once per process - #1321

Merged
max-sixty merged 5 commits into
mainfrom
held-revision-readings
Sep 27, 2026
Merged

max-sixty merged 5 commits into
mainfrom
held-revision-readings

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 27, 2026 •

Copy link
Copy Markdown
Owner

A state read of the corpus example spent most of its time walking a revision that had not changed: page_reading took spoken(document, registry) on every read and read_document walked the same document again for its retired view. Seven stamp-keyed caches in revision_artifact plus structure._revisions each held one piece of the same immutable revision, and nothing held the readings derived from it. Logged markup had the same shape: frozen_thread_reading re-parsed every fragment right after thread_structure had parsed them, and about ten readers of the log parsed all markup again.

What changed

One held reading per revision. revision_artifact.read_revision(page_dir, n) returns a RevisionReading keyed by the marker's stamp. It owns the manifest, digest, captured registry, parsed document, and, through its base class passages.SourceReading, the authored passage reading, spoken, and containment, each read on first use. parse_revision, read_registry, read_manifest, their stamp caches, and structure._revisions are gone; every caller takes the reading. A candidate document (a check, a snapshot, a render probe) gets a plain SourceReading for as long as its check. page_reading now takes a SourceReading, so the log fold is the only per-read work. RevisionReading.under(registry) returns the held reading when a caller reads a revision under its own captured vocabulary; the active registry is that same object, so leaf thread open, work subjects, and the append door share it (PageView.reading, AdmissionReadings.page(revision)).

Bounded by source size. Held readings are charged their source size against an 8 MB budget, least recently read evicted first, under a lock (endpoints read from a thread pool). A document's parse dominates an entry and scales with its source: the corpus example's 323 KB parses to about 9 MB, and its passage and word readings add about 3 MB. So resident parses stay at a few hundred MB at most, and a normal history fits whole (about 180 revisions of the largest shipped example page, 700 of the median). A history past the budget re-parses what a whole-history scan reaches after the budget is spent; the old structure._revisions instead held every parse unbounded. The reading doesn't keep source bytes beside the parse; its document decodes the file's bytes, so document.data is exactly what was captured, CRLF included. Stored revisions in a page snapshot, export, and sample creation now take the held reading instead of re-parsing the bundle's HTML; a snapshot reads its readings' documents and registries at capture, so it serves what it froze.

The retired view is the same walk. SourceReading.decided_passages(outcomes) is page_passages with those outcomes. The parser consults outcomes only by the id of an element it opens, so outcomes naming no element of the document leave every field as authored and the held reading answers. Otherwise the walk runs once and is held until the outcomes change. I rejected deriving it by masking the held reading: retirement moves block separators, fences, and gone, so a mask would restate the walk's rules in a second place.

Logged markup is parsed once. thread_context.logged_fragment(event) holds each fragment's parse by its markup text, since a logged event never changes. thread_structure, frozen_thread_reading, agent state, data contracts, and the render gate's verbatim reading use it. Markup a writer hands in is still parsed from scratch at check_markup.

Caches left separate, as different facts: read_artifact's bounded bundle materialization (a bundle is ~11 MB and snapshots ask for every revision's), _read_stamped (capture reads of mutable page files), _capture_artifact_stamped (a candidate's capture), _css_dependencies (a pure function of CSS text), _shared_registry (one parse per distinct registry across revisions), and revisioning._held (the activation answer for the mutable source). Activation and version check now compare the manifest digest rather than materializing the active bundle to read it. validation.source_history.RevisionReading became PredecessorReading, holding the predecessor's SourceReading instead of three parallel fields. browser_state, project_browser_state, and PageSnapshot take one readings map in place of documents plus registry plus registries.

Measured

Paired, alternating base (origin/main at 64186bc) and head, in-process PageStateService.page_state() warm reads, median of 30 reads per run, 6 runs per arm. The machine was running three other agents' suites (load 50–85), so absolute numbers are inflated several times over; the ratio is what counts. At an idle moment earlier, base corpus measured ~45 ms per read, with two page_passages walks taking ~40 ms of it.

page base (median of runs) head
corpus 462 ms 11 ms
ship-review 4.6 ms 3.3 ms

A warm corpus read now makes zero page_passages calls, down from two. one_transition.py corpus status 6 origin/main (paired browser runs, painted ms): base median 280, head median 206.

Tests

uv run pytest tests/test_interact_{contract,document,server,layer,anchors,product,mcp,session,preview}.py -n 4: all pass. test_a_preview_holds_one_contract_until_it_closes failed once under load; its 5 s join takes 2–5.8 s on both base and head, so the failure is load, not this change. New tests: a second state read walks nothing (bug-back: without the held reading it walks); decided_passages equals page_passages under the same outcomes, including outcomes the document does not carry (bug-back: always returning the authored reading fails); held readings stay within the source budget, least recently read first (bug-back: without eviction it fails); a CRLF source re-checked unchanged is the active revision (bug-back: reading the revision as text fails). Behavior that moved in tests: the materialization test now expects the active revision to open its document, registry, and manifest (3 files) rather than its whole bundle.

Not done

The projection helpers that take spk (state_projection, protected_ids, retirable_ids) still derive containment with enclosing_of, because the frozen thread passes them a union of fragment readings that no single SourceReading holds. The frozen thread's per-fragment spoken is still walked on every read. Fragments are no longer re-parsed, and the walk costs ~0.1 ms on ship-review, so holding it per registry wasn't worth the extra machinery.

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

🤖 Generated with Claude Code

A state read of the corpus example spent most of its time walking a revision
that had not changed: `page_reading` took `spoken(document, registry)` on every
read, and `read_document` walked the same document again for its retired view.
Seven stamp-keyed caches in `revision_artifact` plus `structure._revisions`
each held one piece of the same immutable revision, and nothing held the
derived readings at all.

A stored revision now has one held reading, `revision_artifact.RevisionReading`
from `read_revision`, keyed by its marker's stamp. It owns the manifest,
digest, captured registry, parsed document, and, through its base class
`passages.SourceReading`, the authored passage reading, `spoken`, and
containment. `parse_revision`, `read_registry`, `read_manifest`, their stamp
caches, and `structure._revisions` are gone; every caller takes the reading.
A candidate document (a check, a snapshot, a render probe) gets a plain
`SourceReading` for as long as its check. `page_reading` takes a
`SourceReading`, so the page fold is the only per-read work, and
`RevisionReading.under(registry)` hands back the held reading when a caller
reads a revision under its own captured vocabulary (the active registry is
that same object).

The retired view is the same `page_passages` walk with the standing outcomes,
now `SourceReading.decided_passages`. The walk consults outcomes only by the id
of an element it opens, so outcomes naming no element of the document leave
every field as authored and the held reading answers; otherwise the walk runs
once and is held until the outcomes change. No second rule: masking the held
reading was rejected because retirement moves block separators, fences, and
`gone`, which would restate the walk.

What stays separate, as different facts: `read_artifact`'s bounded bundle
materialization (a bundle is ~11 MB and snapshots ask for every revision's),
`_read_stamped` (capture reads of mutable page files), `_capture_artifact_stamped`
(a candidate's capture), `_css_dependencies` (a pure function of CSS text),
`_shared_registry` (one parse per distinct registry across revisions), and
`revisioning._held` (the activation answer for the mutable source). Activation
and `version check` now compare the manifest digest instead of materializing the
active bundle to read it.

Logged markup is parsed once too: `thread_context.logged_fragment` holds each
fragment's parse by its text, since a logged event never changes.
`thread_structure`, `frozen_thread_reading` (which re-parsed every fragment
after `thread_structure` had), agent state, data contracts, and the render
gate's verbatim reading take it. Markup a writer hands in is still parsed afresh
at `check_markup`.

`PageView.reading` and `AdmissionReadings.page(revision)` give the append door
the held reading, so admitting a gesture no longer walks the page either.
`validation.source_history.RevisionReading` became `PredecessorReading`, holding
the predecessor's `SourceReading` instead of three parallel fields.

Behavior moved in tests: the materialization test now asserts the active
revision opens its document, registry, and manifest (3 files) rather than its
whole bundle; `browser_state` and `page_reading` callers pass readings. New
tests pin that a second state read walks nothing and that `decided_passages`
equals the walk under the same outcomes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
leaf-agent previously approved these changes Sep 27, 2026
`SourceReading.decided_passages` read its held (outcomes, reading) pair twice,
so a thread reading other outcomes between the check and the return handed
back another request's retired view. It now binds the pair once.

`read_revision` mutated its map unlocked from a thread-pool endpoint, and two
threads evicting past the bound could `del` the same key. The map and its
total are now under a lock. The bound is a budget of 8 MB of authored source
rather than an entry count: an entry's weight is its document's parse, which
scales with the source (the corpus example's 323 KB parses to about 9 MB, and
its passage and word readings add about 3 MB), so each entry is charged its
source size. A normal history fits whole (about 180 revisions of the largest
shipped example page, 700 of the median one); a history past that re-parses
what a whole-history scan reaches after the budget is spent, where the earlier
unbounded `structure._revisions` held every parse a server ever made. The
reading no longer keeps the source bytes beside the parse, since the document
holds its own.

Cut-over leftovers: `work_subject` and the transcript fold threads on the held
reading's `within` rather than re-deriving it from `spoken`; the projection
helpers that take `spk` keep `enclosing_of`, because the frozen thread passes
a union of fragment readings that no single `SourceReading` holds. A page
snapshot takes the held reading of every stored revision and builds one only
for a candidate it captured. `cmd_export` refuses a specimen page from the
revision's parsed document, and `export_document` scans that document and the
logged fragments for widgets instead of re-parsing the bundle's HTML.
`Specimens.create` takes the parent's parsed document from its caller.
`candidate_vocabulary_gaps` reads each revision once per call again.

New test: held readings stay within the source budget, least recently read
first.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	skills/leaf/scripts/leaf/presence.py
leaf-agent
leaf-agent previously approved these changes Sep 27, 2026
`RevisionReading.document` read the marker as text, whose universal newlines
turned CRLF into LF, so `document.data` stopped equalling the captured bytes.
The unchanged-source check compares those bytes with the candidate's, so a CRLF
page checked again unchanged read as a new candidate: its skipped gates ran and
continuity was judged against the active revision instead of its predecessor.
The document now decodes the file's bytes. New test: a CRLF source re-checked
unchanged is the active revision.

A page snapshot now reads each held revision's document and registry inside
its transaction, so a preview server serves what it captured even if the page
directory later goes away.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	skills/leaf/scripts/leaf/exporting.py

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

An unchanged revision with CRLF line endings is treated as a new transition during source validation.

@max-sixty
max-sixty merged commit f7dacff into main Sep 27, 2026
6 checks passed
@max-sixty
max-sixty deleted the held-revision-readings branch September 27, 2026 20:02
max-sixty added a commit that referenced this pull request Sep 27, 2026
)

Activating a revision took every authored attribute, class token and
inline declaration off `html` and `body` and put the arriving revision's
back on, including the ones both revisions write. A class taken off
`body` and put back restyles the whole document, so every corpus
revision paid a full restyle for attributes that hadn't changed. This
came out of re-profiling revisions after the recent performance PRs
(#1321, #1324, #1334).

`replaceAuthoredAttributes` now takes off only what the outgoing
revision wrote and the arriving one doesn't, and writes only what
differs, through the layer's identical-write rule (`keeps`).
Declarations are compared by name, so an empty custom property (`--x:
;`, which reads back as `""`) stays, and a newly declared empty one is
written as the single space that parses to it, since `setProperty` with
`""` removes; the old code dropped both. Runtime-owned attributes and
inline properties survive, as before; the existing live-revision test
covers adding, retiring and the runtime-owned `--lf-thread-panel-width`.

Measured on the corpus with `scripts/profile_page.py corpus revision` (3
runs each, current main, load ~20):

| | main | this branch |
|---|---|---|
| style per revision | 149.5 ms | 54.6 ms |
| install task | 313–357 ms | 160–188 ms |

Most of what's left in that task is `reachScrollers`, which #1340 takes
down separately; the two together bring style per revision to about 15
ms.

New test: a revision that changes only the title makes no authored
attribute write on either root. It fails on the old code.

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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[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