Repository navigation
Read a page in its layer's vocabulary, without composing the layer's files - #5059
Merged
Merged
Conversation
Claude Code waits for Leaf's PostToolBatch hook before every model call. In a session holding a page, that hook folded every page it held before deciding it had nothing to hand over, and nearly every boundary has nothing. The fold reads the page's vocabulary, which composes the page's whole layer, parsing every package stylesheet and scanning every widget module's imports. On this Mac that was about 350 ms of the hook's 500, for nothing. A tool boundary carries only input past a page's cursor, a status line due for its two-minute check, or a line the clerk queued. `carry_turn` now checks those first: the clerk queue and the status mark are files, and `new_input` reads each page's log under its transaction and asks `delivery.deliverable(received=False)`, the question Codex's tool hook already asks. Only when one of them holds does the hook fold its pages, exactly as before. `hook_transport` imports the page model (`activity`, `served_state.context`, `event_contracts`) in the functions that fold a page, so an empty boundary loads no jsonschema, turbohtml, tinycss2 or tree-sitter: 176 modules where it loaded 389. One delivery moves later. An update the cursor has passed that the session holds no usable answer reference for, as after the page changed hands (`delivery.unreferenced`), reached the next boundary; it now rides with new input or waits for the next prompt, which the watch wakes once the turn ends. Codex's tool hook already treats it this way. Measured with `.tmp/bench/bench.py` (each registered command run through `bin/leaf`, a page claimed in a throwaway state home), medians of 9 runs per arm, base and candidate interleaved over three rounds at load 12-25: | Hook | Base | This change | | --- | --- | --- | | PostToolBatch, nothing pending | 657-795 ms | 113-227 ms | | Stop | 615-805 ms | unchanged within noise | | UserPromptSubmit | 653-706 ms | unchanged within noise | At load 3, before the change, PostToolBatch took 502 ms (n=15). `test_a_tool_boundary_with_nothing_new_answers_from_the_log` fails on the base (the empty boundary folded the page) and passes here, and the comment that arrives next still reaches the following boundary. The read_page seam of `test_hook_snapshot_serializes_receipt_and_reply` moves to `served_state.context`, where `read_plans` now looks it up. Part of #4549 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`read_plans` renews a page at an older page protocol before it reads it, so the tool-boundary hook used to renew one at every boundary. An empty boundary no longer calls `read_plans`, and such a page offers no input (`delivery.deliverable`), so its renewal waits for the next prompt or Stop hook, the two `vendoring.renew_layer` already names. The module header now says so. Part of #4549 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ports Review of the empty-boundary change found its header incomplete about where an update the cursor has passed, with no usable answer reference, reaches the turn. Besides a boundary with new input or a clerk line, a boundary folds its pages whenever its status check is due, every `STATUS_RENEWAL` and at a session's first boundary; Stop hands it over under plain `--print`; and otherwise the prompt of the turn the watch wakes carries it. The header now lists all four. It also drops the measurement from the header, since how the rule was found belongs here: on macOS on 1 November 2026, reading the pages took about 350 ms of an empty boundary hook's 500. `test_a_claimed_hook_imports_no_css_script_or_watch_parser` said the claimed hook needs the schema validators, which no longer holds. As `test_the_hook_transport_imports_no_page_model` it forbids jsonschema, turbohtml and markdown_it beside tinycss2, tree-sitter and watchfiles, so hoisting a page model import back into `hook_transport` fails the suite (checked: moving `from .activity import unanswered` to module level fails it with jsonschema and turbohtml loaded). Part of #4549 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…agent-a3a71007653a64ef5
…files Every prompt and Stop hook, and every command that reads a page, read it in its layer's vocabulary, and getting the vocabulary composed the whole layer: every package stylesheet parsed with tinycss2 and every widget module's imports scanned with tree-sitter. That was most of each hook's time. `layer.LayerVocabulary` is now the part of a layer a page's documents are read in: the merged registry and the paths of its widget modules. `layer.selected_vocabulary` composes and holds it apart from `layer.selected_layer`, which builds the full composition over the same vocabulary. `registry.storage` reads every vocabulary (`layer_vocabulary`, `layer_widgets`, `revision_vocabulary`, `read_page_registry`) through it. `leaf page state` read the served state's layer identity too, whose fingerprint digests every file the layer serves. That identity is a fact of serving, so the serving route adds it (`served_state.page.served_layer_identity`), and the schema moves `layer` from `Reading` to the answer. The `/api/state` answer is unchanged. A package copy that stops composing after `page init` (edited in place, or a sheet a stricter Leaf refuses) is now refused where the layer's files are composed, naming the repair: `page check` composes the layer before checking the source, and `http.page_endpoint` composes it before a server binds, so `server start` refuses rather than announcing a URL. Before, every read refused it, and `server start` printed a URL while the server died in its row publisher with the reason on /dev/null. Measured through `bin/leaf` on a stamped, claimed page in a throwaway state home, base and candidate snapshots interleaved, 3 rounds of 9 (median of round medians): UserPromptSubmit 731 -> 281 ms, Stop 732 -> 287 ms, `page state` 705 -> 259 ms at load 13-14, and 747 -> 317, 761 -> 301, 714 -> 274 ms at load 18-20. `server start` now composes before it announces: 411 -> 901 ms, with the first document request afterwards unchanged (111 / 121 ms), since the old server composed the layer in its row publisher's thread right after announcing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`leaf page instructions`, which agents run while authoring, composed the full layer to read the packages' instruction files, and refused a page whose package copy no longer composed. The instructions are now part of `LayerVocabulary` (`composed_instructions` over the same inputs), so the command reads the vocabulary like every other reader, and the full composition takes them from it for its fingerprint, which keeps its value. The contract prose said every hook and command that reads a page parses none of the layer's sheets or modules. `page inspect` and the capture of a revision whose markup names a layer file still compose the layer's files, so `layer-registry.md` now says where Leaf parses them, and the function names move to `layer.py`'s header. Co-Authored-By: Claude Opus 5.5 <[email protected]>
max-sixty
marked this pull request as ready for review
November 2, 2026 06:46
leaf-agent
approved these changes
Nov 2, 2026
This was referenced Nov 3, 2026
max-sixty
added a commit
that referenced
this pull request
Nov 4, 2026
…ema only to validate (#5272) Every hook in a session holding a page, every fork step and every command that reads a page composed the page's layer vocabulary, and composing it validated the merged registry: a JSON Schema meta-check of each widget schema and a parse of every catalog example, 25–50 ms of each cold process, twice for a page whose revisions select different packages. That validation is why jsonschema, referencing, attrs, rpds, regress and jmespath sat on the hook path at all; moving their imports alone would have saved nothing. ### Where the registry is validated `layer.compose_vocabulary`, which every reader of a page uses, now merges the registries and stamps the result (`registry.layer.merged_registry`) and validates nothing. `layer._composition`, the full composition behind `page init`, `page check`, `server start`, export and `package check`, validates the registry as `page init` vendors it (`validation.composition.incoming_registry`). A page's package copy edited in place, or an installed package a stricter Leaf refuses, is then refused by `page check` and `server start`, naming the repair, while hooks and reads of the page go on, as #5059 made them for a broken sheet or module; `layer-registry.md` and `validation.md` say so. A candidate never activated, or whose own declarations differ from the active revision's, is still validated when it is read (`registry.storage.read_page_registry`). Behavior that changes: a hook or read of a page whose registry stopped validating after `page init` reads on where it used to refuse with `layer-unavailable`. A registry broken in a way the stamping trips on fails the read with that error instead of the refusal. The full composition validates the registry before it composes the vocabulary, since stamping and the widget-module check read each declaration as valid: a package declaration missing a verb's `unit` or a widget's `x-upgrade` is refused by name at `page init`, not with a `KeyError`. `registry/layer.py` keeps the composition, merging and stamping; the checks of a vocabulary's layer facts and declarations move to `registry/validation.py`, beside the orchestration that calls them. ### When jsonschema loads `registry/schema.py` builds its format checkers, and points jsonschema's regular expressions at regress, on first use (`_checkers`), so a module importing it for `aware_instant` or `json_value` pays nothing. `registry.contract.same_state_definition` compares two recorded definitions itself, as the browser's `sameStateDefinition` does, rather than through `jsonschema._utils.equal`, a private helper that loaded the whole library for every page read (identical results on a product of JSON values, bools, ints, floats, lists and objects). jmespath (in `data_contracts`), `registry.validation` (in `registry/page.py`), `validation.source` (in `vendoring`) and `http.client` (in the bridge's client) load in the functions that use them, and the theme catalogue is read through `json_validator` rather than a module-level validator. A Stop hook on a claimed page now loads 365 modules where it loaded 454. ### Evidence Each scenario as a whole `python -m leaf` process (no launcher; `bin/leaf` adds the same overhead to both arms) on a copy of a real 71-event page claimed in a throwaway state home, base (`e2369b804`) and candidate interleaved in random order, each run from the same restored snapshot, 9 runs per arm. Medians of wall time; CPU time (user+sys) moved the same way. | | Base | This change | Base | This change | | --- | --- | --- | --- | --- | | Load | 18–33 | | 50–75 | | | UserPromptSubmit | 202 ms | 139 ms | 282 ms | 170 ms | | PostToolBatch, status check due | 212 ms | 145 ms | 280 ms | 163 ms | | PostToolBatch, nothing pending | 59 ms | 56 ms | 76 ms | 67 ms | | Stop | 216 ms | 150 ms | 294 ms | 194 ms | | Clerk's fork prompt (`hook --fork`) | 259 ms | 167 ms | 383 ms | 214 ms | | Held thread's fork prompt, without the hold check | 200 ms | 127 ms | 260 ms | 151 ms | | `page state` | 276 ms | 206 ms | 369 ms | 266 ms | | `thread reply --ephemeral` | 205 ms | 167 ms | 266 ms | 213 ms | | PreToolUse | 80 ms | 71 ms | 96 ms | 85 ms | | `--root` | 43 ms | 42 ms | 54 ms | 52 ms | Every scenario printed the same output in both arms and left the same page log and state files, apart from a duration field and the bridge records keyed by install path. ### Tests - `test_a_package_that_stops_composing_is_refused_where_the_page_is_served` gains a registry case and stamps the page first, so its `page state`, run as a process of its own, reads an active revision; on the base the registry case fails at that read. - `test_a_hook_reading_a_page_it_holds_loads_no_schema_validator` runs a Stop hook on a stamped, claimed page in a fresh interpreter and finds none of jsonschema, referencing, regress or jmespath loaded; it fails on the base. - `test_init_refuses_a_declaration_the_vocabulary_would_read_unchecked` holds that ordering; it fails when the full composition validates after composing the vocabulary. - Tests of the moved checks name `registry.validation`. > _This was written by Claude Code on behalf of max-sixty_
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every prompt and Stop hook, and every command that reads a page's documents, state or instructions, read the page in its layer's vocabulary, and getting the vocabulary composed the whole layer: every package stylesheet parsed with tinycss2 and every widget module's imports scanned with tree-sitter. That was most of each hook's time.
layer.LayerVocabularyis now the part of a layer a reader of a page needs: the merged registry, the paths of its widget modules and the packages' instructions by audience.layer.selected_vocabularycomposes it and holds it for the process apart fromlayer.selected_layer, which builds the full composition over the same vocabulary object, so a server shares one.registry.storagereads every vocabulary (layer_vocabulary,layer_widgets,revision_vocabulary,read_page_registry) through it,page.page_instructionsreads the instructions from it, andLayerComposition.registrybecomesLayerComposition.vocabulary.registry. The layer's fingerprint keeps its value.leaf page statealso read the served state's layer identity, whose fingerprint digests every file the layer serves. That identity is a fact of serving, so the serving route adds it (served_state.page.served_layer_identity), andstate.schema.jsonmoveslayerfromReadingto the answer. The/api/stateanswer the browser reads is unchanged,layerkey first included.Leaf still parses the layer's sheets and modules to serve, export, check or inspect a page, and to capture a revision whose markup names a file the layer serves;
layer-registry.mdsays so.Where a package that stops composing is refused
page initstill refuses a selection that doesn't compose. A page's copy of a package can stop composing later, edited in place or read by a stricter Leaf. Run on current main and on this branch, on a page whose copied package then got.x { broken; }indefaults.css, or a widget module importing/runtime/semantic-state.js(both cases read the same):page checklayer-unavailablelayer-unavailable, naming the file andleaf page init --packagepage state, hookspage vocabularypage instructionspage inspectserver startlayer-unavailablemessage, and writes no service recordpage exportpage checkcomposes the layer after its protocol check (validation/command.py), andhttp.page_endpoint, the gate every one-page server passes before it binds, composes it beside its protocol check.Evidence
Each hook as
hooks.jsonregisters it, throughbin/leaf, and each read command, on a stamped page claimed for one session in a throwaway state home. Both arms aregit archivesnapshots, interleaved with alternating order; for the hooks andpage state, 3 rounds of 9 per arm, median of round medians and range of round medians.page statepage instructionspage vocabularyBase is current main (
5a16ff03f); the two commands are the median of 8 runs. An earlier run against main as of #5051 gave the same picture: UserPromptSubmit 731 → 281 ms, Stop 732 → 287 ms andpage state705 → 259 ms at load 13–14, and 747 → 317, 761 → 301 and 714 → 274 ms at load 18–20. The review's own re-measurement read 686 → 307, 688 → 295 and 690 → 258 ms.In-process and cold,
read_planstook 608 ms on the base and 158 ms here (one run each), and loads neither tinycss2 nor tree-sitter. What remains is mostly the merged registry's validation (incoming_registry, about 35 ms) and the page model's imports.server startmoves the composition before it announces the URL: 322 → 688 ms (median of 6, load 6). The base server composed the layer anyway, in its row publisher's thread right after announcing, so the work moves rather than grows. A document requested right after start took 535 → 113 ms (the review measured 545 → 99 ms), and one requested 3 s later 103 → 99 ms. The agent'sserver startwaits about 0.4 s longer, once per server.Tests
test_reading_a_page_composes_only_its_layers_vocabularyruns a prompt hook (the page's candidate),page state(which activates it), a Stop hook (its revision),page vocabularyandpage instructionswithlayer._compositionrecording each call, and asserts none. It fails when storage's readers compose the full layer, whenread_served_pagecomputes the layer identity again, and whenpage instructionsreads the full composition.test_a_package_that_stops_composing_is_refused_where_the_page_is_served[sheet|module]breaks a copied package's sheet or module afterpage initand assertspage statereads,page checkrefuses withlayer-unavailablenaming the file and the repair, andhttp.page_endpointrefuses. Each half fails when its gate is removed.uv run pytest tests/model/test_interact_layer.py tests/model/test_themes.py tests/model/test_distribution.py tests/model/test_claims.py tests/model/test_stop_guard.py tests/model/test_hooks.py tests/model/test_hook_registration.py tests/model/test_activity.py tests/model/test_seen.py tests/model/interact_contract tests/process/test_interact_server.py tests/process/test_hook_gate.py(895 passed, 1 skipped),pnpm run build:browser(no change) andpnpm run check.test_todos,test_file_bindings,test_module_url_delivery,process/website_serverandprocess/test_http_connections(1169 passed, 1 skipped); the served-state and delivery model tests (test_clerk,test_delivery,test_forks,test_interact_anchors,test_interact_document,test_interact_product,test_native_watch,test_obligations,test_render_export,test_revision_delivery,test_thread_counts,test_thread_reading,test_update_obligations,test_workflows,test_codex_app_server,test_codex_adapter,test_session_lifecycle,process/website_server/test_saved_records): 768 passed;tests/browser/widgets/test_pressed_controls.py tests/browser/test_widget_quality.py tests/smoke/test_plain_page.py tests/browser/test_page_inspection.py: 31 passed.journeys/test_interact_layer.py::test_package_is_the_unit_that_init_creates_checks_and_vendors, which reads the instructions from the vocabulary now: passed.leaf-dev journey claude-codeon this branch: all 13 steps passed.dev/tests/modeltests (test_data_structures_page_lists_every_declared_schema,test_a_composed_part_refuses_an_authored_copy,test_preview_file_subscription_follows_atomic_replacements) fail here and on the base alike.Not verified
uv run pytest testsselection and the browser tiers, which landing runs.<link rel="icon" href="/icon.svg">(Leaf's own site pages do), still compose the full layer when their source is checked, since the capture asks whether that path is one the layer serves (revision_artifact._layer_files). Agent pages rarely do.Calls Max may reverse
page stampand automatic activation read only the vocabulary, so they no longer refuse a page whose package copy stopped composing;page check,server startand export do.server startcomposes before it announces, which costs about 0.4 s per start and buys a refusal the agent sees. The alternative is composing in the server after it announces, as the base did by accident, which keeps start fast but leaves a broken package to a 500 in the user's browser.Part of #4549