Skip to content

Answer Claude Code's empty tool boundaries from the page log - #5051

Merged
max-sixty merged 3 commits into
mainfrom
grind/issue-4549-boundary
Nov 2, 2026
Merged

max-sixty merged 3 commits into
mainfrom
grind/issue-4549-boundary

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

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, spent at every tool batch.

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 (python -X importtime).

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 reaches the turn wherever a hook still folds the pages:

  • a boundary with new input or a clerk line beside it;
  • a boundary whose status check is due, every STATUS_RENEWAL (two minutes) and at a session's first boundary;
  • Stop under plain --print, where no watch follows the turn;
  • otherwise the prompt of the turn the watch wakes once this one ends.

Codex's tool hook already leaves it to Stop or its adapter. Likewise, a page at an older page protocol offers no input until it is renewed, and an empty boundary no longer renews it; the next prompt or Stop hook does, the two vendoring.renew_layer already names.

Stop and UserPromptSubmit still fold every page, and composing the page's layer is most of their cost too: in-process, read_plans takes 355 ms with the full composition and 44 ms when the layer composes only its registry and widget names. Composing only what a reader needs is a separate change.

Evidence

Each registered command run through bin/leaf against a page claimed in a throwaway state home (.tmp/bench/bench.py in the worktree), base and candidate interleaved, three rounds of 9 runs per arm. Median of the round medians, with the range of round medians:

Hook Base This change Load
PostToolBatch, nothing pending 742 ms (657–795) 161 ms (113–227) 12–25
PostToolBatch, nothing pending 720 ms (620–842) 173 ms (115–189) 37–56
Stop 747 / 674 ms 758 / 817 ms both runs
UserPromptSubmit 684 / 660 ms 679 / 646 ms both runs

At load 3, before the change, the same PostToolBatch took 502 ms (n=15). In-process on the base, read_plans took 350–390 ms of it and the imports up to hook_transport about 60 ms. After it, the empty boundary's own work is new_input at about 2 ms on a short log; what remains is the gate's interpreter, uv, the second interpreter, Click and Leaf's base modules.

In a real Claude Code session (uv run leaf-dev journey claude-code, Claude Code 2.1.316), Leaf's hook durations from Claude Code's own Hooks: debug lines:

Hook Before (main, load ~5) After (load ~20–45)
PostToolBatch 565 ms median, n=26 212 ms median, n=26
Stop 573 ms, n=13 767 ms, n=13
UserPromptSubmit 920 ms, n=18 1256 ms, n=18

Stop and UserPromptSubmit run unchanged code here, so their rise measures the busier host during the second run; PostToolBatch fell despite it. Every journey step passed on both runs, including mid-turn, ending and boundary, whose comments a tool boundary hands over.

An independent review drove each boundary case through cmd_hook on base and head and found no delivery the change misses beyond the deferral above.

test_a_tool_boundary_with_nothing_new_answers_from_the_log fails on the base (the empty boundary folded the page) and passes here; 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. test_the_hook_transport_imports_no_page_model (formerly test_a_claimed_hook_imports_no_css_script_or_watch_parser) now forbids jsonschema, turbohtml and markdown_it too, so hoisting a page-model import back into hook_transport fails it.

Checks run

  • uv run pytest tests/model/test_hooks.py tests/model/test_clerk.py tests/model/test_forks.py tests/model/test_hook_registration.py tests/process/test_hook_gate.py tests/model/test_session_lifecycle.py tests/model/test_thread_reading.py tests/model/test_interact_anchors.py (210 passed)
  • uv run pytest tests/model/test_codex_adapter.py tests/model/test_codex_app_server.py (194 passed)
  • uv run leaf-dev journey claude-code before (on main) and after (this change with Run Claude Code's start hook only after a compaction #5052 and current main): every step passed

Not verified

  • Linux, where the issue's numbers came from. The saving there should be larger, since that host compiled stdlib bytecode on every start, but I measured only this Mac.

Part of #4549

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

max-sixty and others added 2 commits November 1, 2026 16:18
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]>
@max-sixty
max-sixty marked this pull request as ready for review November 2, 2026 05:19
@max-sixty
max-sixty merged commit 5bf7592 into main Nov 2, 2026
5 checks passed
@max-sixty
max-sixty deleted the grind/issue-4549-boundary branch November 2, 2026 05:38
max-sixty added a commit that referenced this pull request Nov 2, 2026
…files (#5059)

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.LayerVocabulary` is 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_vocabulary` composes
it and holds it for the process apart from `layer.selected_layer`, which
builds the full composition over the same vocabulary object, so a server
shares one. `registry.storage` reads every vocabulary
(`layer_vocabulary`, `layer_widgets`, `revision_vocabulary`,
`read_page_registry`) through it, `page.page_instructions` reads the
instructions from it, and `LayerComposition.registry` becomes
`LayerComposition.vocabulary.registry`. The layer's fingerprint keeps
its value.

`leaf page state` also 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`), and `state.schema.json`
moves `layer` from `Reading` to the answer. The `/api/state` answer the
browser reads is unchanged, `layer` key 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.md` says so.

## Where a package that stops composing is refused

`page init` still 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; }` in `defaults.css`, or a widget
module importing `/runtime/semantic-state.js` (both cases read the
same):

| | Before | After |
| --- | --- | --- |
| `page check` | refused, `layer-unavailable` | refused,
`layer-unavailable`, naming the file and `leaf page init --package` |
| `page state`, hooks | refused on every read | read normally |
| `page vocabulary` | refused | reads normally |
| `page instructions` | refused | reads normally |
| `page inspect` | refused | refused (it reports the layer's CSS) |
| `server start` | printed a URL and exited 0, leaving an enabled
service.json for a server that had died in its row publisher, with the
reason sent to /dev/null | exits 1 with the same `layer-unavailable`
message, and writes no service record |
| `page export` | refused | refused |

`page check` composes the layer after its protocol check
(`validation/command.py`), and `http.page_endpoint`, the gate every
one-page server passes before it binds, composes it beside its protocol
check.

## Evidence

Each hook as `hooks.json` registers it, through `bin/leaf`, and each
read command, on a stamped page claimed for one session in a throwaway
state home. Both arms are `git archive` snapshots, interleaved with
alternating order; for the hooks and `page state`, 3 rounds of 9 per
arm, median of round medians and range of round medians.

| | Base | This change | Load |
| --- | --- | --- | --- |
| UserPromptSubmit | 692 ms (683–701) | 307 ms (274–350) | 9–12 |
| Stop | 713 ms (694–757) | 297 ms (296–331) | 9–12 |
| `page state` | 702 ms (676–710) | 261 ms (257–317) | 9–12 |
| `page instructions` | 613 ms (597–642) | 202 ms (196–212) | 9 |
| `page vocabulary` | 620 ms (583–628) | 203 ms (185–233) | 9 |

Base 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 and `page state` 705 →
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_plans` took 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 start` moves 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's `server start` waits about 0.4 s longer, once per
server.

## Tests

- `test_reading_a_page_composes_only_its_layers_vocabulary` runs a
prompt hook (the page's candidate), `page state` (which activates it), a
Stop hook (its revision), `page vocabulary` and `page instructions` with
`layer._composition` recording each call, and asserts none. It fails
when storage's readers compose the full layer, when `read_served_page`
computes the layer identity again, and when `page instructions` reads
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 after `page init` and asserts
`page state` reads, `page check` refuses with `layer-unavailable` naming
the file and the repair, and `http.page_endpoint` refuses. Each half
fails when its gate is removed.
- After merging current main: `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) and `pnpm
run check`.
- Before it: the same layer, hook and served-state modules with
`test_todos`, `test_file_bindings`, `test_module_url_delivery`,
`process/website_server` and `process/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.
- Review ran `leaf-dev journey claude-code` on this branch: all 13 steps
passed.
- Three `dev/tests/model` tests
(`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

- Linux.
- The broad `uv run pytest tests` selection and the browser tiers, which
landing runs.
- Pages that name a layer file in an attribute, such as `<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 stamp` and automatic activation read only the vocabulary, so
they no longer refuse a page whose package copy stopped composing; `page
check`, `server start` and export do.
- `server start` composes 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

> _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