Skip to content

Measure pickup waits, and run a session's hooks on the Leaf that can read its pages - #4557

Merged
max-sixty merged 8 commits into
mainfrom
pickup-latency
Oct 28, 2026
Merged

max-sixty merged 8 commits into
mainfrom
pickup-latency

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

A week of real pickups (20–27 Oct, 277 messages on 87 pages) put 94% of the time comments sat at "Queued for Claude" on idle sessions nothing woke until the user typed: 16.1 h, median 187 s, p90 85 min. Almost all of it (15.5 h) was one cause. A page was re-vendored by a newer Leaf than the plugin copy the session had loaded, so the session's hooks and watch couldn't read it, and its blind watch still held the wait lease, so the server never nudged. Only /reload-plugins cleared it. This branch measures pickup waits and removes that cause.

Measure pickup waits. leaf-dev response-times gains opened_* rows that sort each opened pickup by what ended its wait (reply_phases.pickup_wait): a running turn's tool boundary, Leaf's watch, Leaf's nudge (recognised by harness.NUDGE_WORDS, also as a resumed background job's prompt), the user's prompt, or something else, with the turns it missed and a total column. Each watch between turns writes a watch record (started, protocol, how it ended, interaction_log.record_watch), which response-times attaches to the pickup it spanned. Before, a blind watch showed only in Claude Code transcripts.

A blind watch no longer blocks the nudge. The wait lease holds the protocol its watch speaks (leases.take_wait_lease), and wait_is_live counts a lease at an older protocol, or an empty one, as no watch, so admission nudges the session. Every wait-lease taker, the website worker's included, stamps it. No protocol bump.

Hooks and commands follow the session's Leaf. The copy a session loaded hands each hook, its watch and, from an installed copy, its bare leaf commands to one launcher (session_runtime.delivering_launcher): the session's selected checkout while it exists (a user preview, or now also a checkout whose command vendored one of its pages, session_runtime.select_checkout; a hook selects nothing, so its lifecycle snapshot stays put), otherwise the copy Claude Code has installed since (installed_plugins.json), otherwise itself. A plugin update or a worktree's re-vendor then reaches a session at its next hook with no /reload-plugins; only hooks.json and the hooks module still wait for a reload. This reverses the preview rule that an unavailable selection refuses every hook: it now falls back to the installed copy, which refuses only a page it can't read, and the newer-layer refusal no longer tells the agent to run /reload-plugins. A hook never writes the session record, since the hook's own checks compare that whole record and drop their output when it changes. The Codex adapter still follows only a preview's selection. layer-registry.md ("Page protocol") and session-lifetime.md state the rules.

The hand-off reaches only sessions that load a copy containing it, so sessions running when this lands need one more reload.

Also: test_a_prompt_re_vendors_a_page_an_older_leaf_vendored_and_hands_over_its_input no longer times its re-vendor against CONFIRM_WITHIN, which a loaded host stretched past (26 s under four background workers), handing a pointer instead of inline input.

Checked: the model tests for hooks, hook registration, wait, activity, claims, stop guard, session lifecycle and the Codex adapter, the dev journey tests, and the claude-code agent journey (all 11 steps, before the merge of main). The journey runs a checkout directly, so the hand-off from an installed copy has only unit tests. The Codex journeys weren't run.

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

`reply_phases.pickup_wait` reads a Claude Code session's transcript for the
wait from a comment's admission to its opened pickup and names what ended it:
the running turn's tool boundary (`working`), Leaf's watch (`woken`), Leaf's
nudge (`nudged`), the user's own prompt (`prompted`), something else
(`other`), or a session that took the page later (`started`), with the turns
that ended in the wait without taking the comment. `claude_code_records` now
keeps each turn's origin from Claude Code's `turnOrigin`, telling Leaf's wake
from other task notifications by the summary `hooks.json` gives it.

`leaf-dev response-times` attaches the reading to each sample and adds an
`opened_<cause>` row per cause with the seconds the waits add up to, and reads
each session's transcript once a run, which took a week's survey of 87 pages
from over two minutes to about 20 seconds on a loaded host.

Over that week, `prompted` was 31 of 277 inputs and 94% of all waiting: an
idle session nothing of Leaf's woke until the user typed.
…cord each watch's end

A Claude Code session keeps running the plugin it loaded, so once a newer
Leaf re-vendors a page (a plugin update, or a worktree's `bin/leaf`), the
session's between-turns watch can't read it. That watch still held the
session's wait lease, so admission never nudged, and the page said Queued
for Claude until the user typed: 94% of all pickup wait over 20–27 Oct.

A wait lease now holds the page protocol its Leaf speaks
(`leases.take_wait_lease`, the one way a watch takes it), and
`wait_is_live` counts a lease only where that protocol is at least the
reader's. A Leaf at a later protocol, which re-vendors what it reads,
therefore reads an older watch as no watch, and admission nudges the session
as it does when none runs. The lease's content was never read before, so no
older reader changes and the protocol needs no bump; a lease an older Leaf
took names no protocol and counts as no watch, so a session started before
this lands may get one nudge beside its watch's wake until it reloads.

Each watch between turns also records in every page it read when it
started, the protocol it spoke and how it ended (`record_watch`: woke,
superseded, server-down, no-pages, harness-exited, stopped). `leaf-dev
response-times` attaches the record of the watch running at each input's
admission to its pickup reading, so a wait nothing woke says whether a watch
ran and whether it could read the page, without reading the transcript.

Tests that held a wait lease directly take it with `take_wait_lease`.
A session runs its hooks, watch and bare `leaf` from the plugin copy it
loaded, which Claude Code keeps after installing an update until
/reload-plugins. The page-protocol spec now says so, with what happens when a
newer Leaf re-vendors a page the session holds; the vendoring, layer and
launcher comments that said an update reaches a running session, or leaves
every page working, now say under what condition.
…ob's prompt too

A background job whose worker has retired is nudged by resuming it with the
nudge as its prompt, which Claude Code doesn't mark as another session's
message, and another session's own messages are marked like the nudge. The
nudge's words now live in `harness.NUDGE_WORDS`, and `pickup_wait` names a
turn they start `nudged` whatever Claude Code calls its origin; another
session's message is `other`.
…reloads plugins

Claude Code keeps a session on the plugin copy it loaded after installing an
update, and a worktree's `bin/leaf` re-vendors a page the session holds past
that copy; either way the session's hooks and watch refuse the page until the
user runs /reload-plugins and types. That was 94% of all pickup wait over
20–27 Oct.

`session_runtime.delivering_launcher` now names the Leaf that delivers for a
session: the checkout it selected while that checkout exists (a user preview's,
as before, or one that re-vendored a page past the Leaf delivering for the
session), and otherwise, from a copy Claude Code installed, the copy
`installed_plugins.json` records since. Hooks hand themselves to it as they did
to a preview's selection, so the watch follows too, and a bare `leaf` command
from an installed copy does the same (`hand_off_command`). An update reaches a
session at its next hook; hooks.json and the hooks module still need a reload
to change.

This reverses the preview rule that a selection whose checkout is gone refuses
every hook: it now falls back to the installed copy, which refuses only a page
it can't read and says why. A hook never writes a selection, since it already
runs the delivering Leaf and its own readings compare the lifecycle record.
The `newer-layer` refusal no longer tells the agent to /reload-plugins.
… test from the confirm deadline

selected_launcher checks only that the recorded launcher still runs: Leaf writes
it, resolved, so its being absolute needs no check. installed_launcher reads
every installPath inside the guarded read.

test_a_prompt_re_vendors_a_page_an_older_leaf_vendored_and_hands_over_its_input
timed its re-vendor against CONFIRM_WITHIN, which a loaded host stretched past
(the hook took 26 s under four background workers), so it handed a pointer and
failed. The bound has its own tests; this one now protects only the re-vendor and
the inline handover.

@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 checkout handoff misses sessions with no previous launcher selection, leaving the runtime mismatch this PR intends to fix. Details are inline.

Comment thread src/leaf/session_runtime.py Resolved
…nd stamp the website's wait lease

A checkout can't see which copy a session's hooks run: installed_launcher reads
only from an installed copy, so from a checkout delivering_launcher fell back to
the checkout itself and select_checkout never recorded a first selection. An
installed plugin's session whose worktree re-vendored its page kept running the
installed copy's hooks, which refuse the page (review on #4557). A command from a
checkout that vendors one of the session's pages, by `page init` or the renewal a
write command makes, now selects that checkout whenever the session lives. A hook
selects nothing (`renew_layer(command=False)`), so the lifecycle record its own
readings compare against stays put.

The website worker took its wait lease with plain take_lease, so the lease held
no protocol and wait_is_live read the website harness as away; it now takes it
with take_wait_lease.
@max-sixty
max-sixty merged commit 68337d3 into main Oct 28, 2026
5 of 6 checks passed
@max-sixty
max-sixty deleted the pickup-latency branch October 28, 2026 06:48
max-sixty added a commit that referenced this pull request Nov 3, 2026
`leaf-dev journey` timed each comment with a reader of its own
(`recorded_steps`, `pickup_at`) beside `response_times.page_timings`,
which `leaf-dev response-times` and `delivery_eval` already used, so the
page's milestones were computed twice. The journey now reads them with
`page_timings`, and `journey-chart` draws that reader's rows.

## What changes

- **One reader** (`dev/leaf_dev/commands/journey.py`): each end's
`timings()` runs `page_timings` over the page directory (`input_rows`),
and a sample's comment carries that row's `milestones` in place of
`sinceAdmissionMs`. `recorded_steps` and `pickup_at` are gone, and the
Claude Code journey's `fork-work` step reads `first_reply` from the same
rows.
- **Milestones only the journey had move into `page_timings`**
(`dev/leaf_dev/commands/response_times.py`), so `response-times` reports
them too:
- `titled`: when the input's thread got its first title, from the page
server or the agent's own message, for an input admitted while the
thread had none;
- `published`: when the page published the revision the input's answer
stands on, where that came after the input, read from the page
directory's revision files. Nothing records which input a revision
serves, so a revision published while several inputs were open counts
for each answer that stands on it: on recent real pages, 10 of the 62
inputs with `published` stand on a revision an earlier answer stood on.
- **A website target** has no page directory within reach, so the
journey reads the log the page serves (written to a temporary directory
for `page_timings`), which gives no `published`.
- **`journey-chart`** draws `titled`, `opened`, `started`, `progress`
and `answered` from the rows, under the same labels. Samples kept before
this change have no `milestones` and drop out of the chart and the
`--replies` table; with none left it says so rather than failing.
- **A website's reading stops at the served state's own `now`**, so a
container clock ahead of the runner's drops nothing.
- **The browser's reading of when the reply showed stays**
(`verify_site_browser.js`, whose header now says why). The runtime's
`reply_visible` record covers agent words coming into view, but it
records nothing while a thread holds the reply behind its notice: on a
seated thread holding an answer behind "1 new", no record appeared until
the notice was pressed. A website's diagnostics also stay in its
container.

## Evidence

- On the 25 journey page logs kept on this machine (208 comments, 176 of
them in kept samples), the old reader, `page_timings` and the kept
samples agree on every queued, picked-up, started, titled, progress and
answered reading. Progress now follows `page_timings`' rule, an
ephemeral reply that takes its input in hand (`start`), where the old
reader counted any ephemeral reply in the thread; no kept log differs.
- On the pages touched in the last three days, `published` came at or
before the answer for all 62 inputs that have one; `response-times` over
42 recent inputs observes `titled` for 35 and `published` for 34.
- A real `leaf-dev journey claude-code` did not get past `setup` in two
attempts, before any code this changes runs: with about 160 `uv run`
processes on the machine, the payload's first `bin/leaf` call took 185 s
in one and over 300 s in the other, so the session never claimed the
page within the step's 300 s. The release ask's path through
`run_journey`, `HttpEnd.timings` and `page_timings` runs end to end in
`test_the_release_ask_over_http_is_the_shared_check`, against a real
page server and a real published revision, reading `titled`, `published`
and `answered`; the website path runs in
`test_the_page_a_turn_has_just_written_waits_for_its_revision_after_presentation`.

Tests: the `recorded_steps` attribution tests are deleted, their rules
now `page_timings`' and covered in
`dev/tests/model/test_response_times.py`, which gains a titled and
published case. The response-times summary tests now expect `total_s`,
which they had missed since #4557.
`dev/tests/model/test_response_times.py`,
`dev/tests/model/test_journey.py` and the touched tests in
`dev/tests/process/test_website_server.py` pass.

#3840 (draft) also edits `journey.py` and `journey_chart.py`: its
CI-sample test builds samples with `sinceAdmissionMs`, which this
change's chart leaves out, so it needs `milestones` there, and `SIGNS`
loses its clock column.

> _This was written by Claude Code on behalf of max-sixty_
leaf-agent pushed a commit that referenced this pull request Nov 3, 2026
#5229)

A checkout's `page init` made that checkout its session's delivering
Leaf (`session_runtime.select_checkout`) whatever page it initialized. A
plain `leaf-dev preview`, or a scratch page a Claude Code subagent
initialized in its own worktree, then moved the session's hooks, watch
and post-compaction skill reread to that checkout, though the session
never reads that page. The claim commands (`server start`, `server run`,
`page claim`) selected nothing, so a fresh session's first `page init`
and `server start` from a checkout left its hooks on the installed copy,
which can't read the page if the checkout is at a newer page protocol
(#4557's case).

Now a checkout becomes the session's Leaf when a command takes a page
for the session (`server start`, `server run`, `page claim`), or when it
initializes or renews a page the session already holds.
`layer-registry.md` already stated that rule ("the checkout that last
initialized one of its pages"); the code ignored whose page it was.

A claim selects before it prepares delivery. A Codex task's adapter
starts from the selected Leaf, and an adapter running from another Leaf
exits once a different one is selected (`codex_adapter.run_adapter`), so
a selection made after preparing would retire the adapter that preparing
had just joined, leaving the task with none. Likewise, a checkout's init
or renewal of a held page now starts delivery again when it changes the
session's Leaf; on main that left a Codex task with no adapter. Where
the selection doesn't change, as from an installed copy or the checkout
already selected, it leaves delivery alone, so a Codex watcher task
delivering through its own `leaf wait` doesn't get an adapter that takes
its wait lease (inferred from `session.py`, not run).

## What this leaves

Claude Code runs a subagent's shell commands with its parent's session
variables and nothing that names the subagent. In a subagent's Bash at
2.1.317, `CLAUDE_CODE_SESSION_ID`, `CLAUDE_PID`, `CLAUDE_JOB_DIR`,
`CLAUDE_CODE_CHILD_SESSION`, `AI_AGENT` and the shell snapshot all match
the parent's; only hook payloads carry `agent_id`. So a subagent's
`server start` on a scratch page still claims it for the parent: the
parent holds it, Leaf's turn-end clerk offers it to its fork beside the
user's page, and the claim makes the subagent's checkout the parent's
Leaf. `ClaudeCodeHarness`'s docstring now records that fact.

A hook could pass the id on only by rewriting the command (PreToolUse
`updatedInput`, or the hooks module's `tool.call`). When Claude Code
matches a command against permission rules it strips only a fixed list
of variable prefixes (`NODE_ENV`, `LANG` and the like), so a command
rewritten to set a Leaf variable would likely stop matching a rule that
allows `bin/leaf`. That is read from 2.1.317's bundled source, not
tried. This PR doesn't rewrite commands.

Deriving the delivering Leaf from the runtime each held page's service
record names, instead of storing a selection that commands set, would
remove `select_checkout` and its ordering. It isn't in this PR, because
it needs an answer every process computes the same way (the hooks, a
Codex adapter and the command preparing its successor), and a session
holding pages served by two runtimes, such as its own page and a
subagent's scratch page, has no record that says which one the user
reads. It also has to keep a user preview's explicit choice, give a held
page that was never served a fallback, and have a Codex adapter hand
over to its successor rather than exit.

## Evidence

`.tmp/repro.sh` (not committed) runs in a throwaway `XDG_STATE_HOME`
with `CLAUDE_CODE_SESSION_ID` and `CLAUDE_PID` set by hand, as every
Bash call of a Claude Code session and its subagents carries them. The
parent serves its page and selects another Leaf; then, in the same
environment, a "subagent" runs this checkout's commands.

| Step, from the subagent's checkout | main | this branch |
| --- | --- | --- |
| `page init` of a scratch page | moves the parent's Leaf | leaves it |
| plain `leaf-dev preview triage-board` | moves it | leaves it |
| `leaf-dev probe review-a-plan` | leaves it (`arms.py` scrubs identity)
| leaves it |
| `server start` on that scratch page | parent holds the page (its Leaf
had moved at the init) | parent holds the page, and its Leaf moves at
the start |

A fresh session's first `page init` then `server start` from a checkout
selects nothing on main and selects the checkout here.

Tests:

-
`test_a_checkout_leaves_its_session_alone_for_a_page_the_session_doesnt_hold`
fails on main and passes here; it also checks that delivery never
starts.
-
`test_a_checkout_that_takes_over_a_held_page_starts_its_sessions_delivery_again`,
for `page init` and a renewal, checks that delivery starts once, after
the checkout is selected, and not again when nothing changed. It fails
with the restart removed or made unconditional.
-
`test_a_claim_makes_its_checkout_the_sessions_leaf_before_preparing_delivery`,
for `page claim`, `server start` and `server run`, checks the selection
is in place when delivery preparation starts.
- The reviewer's Codex journey test (a task whose adapter runs one Leaf
takes its page from another checkout by `page claim`, `page init` or
`server start`, then expects an adapter to queue the next input; not
committed) passes all three here. By the reviewer's runs, `claim` and
`start` lose the adapter on this PR's first commit, and `init` loses it
on main.

## Tests run

- `uv run pytest tests/model/test_hooks.py
tests/model/test_hook_registration.py tests/model/test_claims.py
tests/model/test_server_lifetime.py
tests/model/test_session_lifecycle.py
tests/model/interact_contract/test_revendoring.py
tests/model/test_codex_adapter.py
dev/tests/model/test_interact_preview.py
tests/process/test_interact_server.py tests/journeys/test_claims.py
tests/journeys/test_server_lifetime.py
tests/journeys/interact_contract/test_revendoring.py
"tests/journeys/test_codex_adapter.py::test_selecting_a_checkout_replaces_the_sessions_delivery_runtime"
-n 4`: 454 passed, 1 skipped, 1 failed. The failure,
`dev/tests/model/test_interact_preview.py::test_preview_file_subscription_follows_atomic_replacements`,
fails the same way on main.
- The reviewer's Codex test, `-n 3`: 3 passed.
- After the last commit: `uv run pytest tests/model/test_hooks.py
tests/model/test_codex_adapter.py tests/model/test_claims.py
tests/model/test_hook_registration.py
tests/model/test_server_lifetime.py
tests/model/interact_contract/test_revendoring.py
tests/process/test_interact_server.py
tests/journeys/interact_contract/test_revendoring.py
"tests/journeys/test_codex_adapter.py::test_selecting_a_checkout_replaces_the_sessions_delivery_runtime"
-n 4`, with the reviewer's Codex test added: 365 passed, 1 skipped.
- `uv run pre-commit run --files` on the changed files.

Not run: the claude-code and codex journeys (`leaf-dev journey`), which
`/developing-leaf` asks for after a change to `ClaudeCodeHarness` or
Codex delivery; the `ClaudeCodeHarness` change is a docstring, and the
Codex ordering is covered by the reviewer's test above.

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
max-sixty added a commit that referenced this pull request Nov 15, 2026
…ultiplying them (#5982)

Every plugin update started another sandbox bridge that never retired. A
bridge was keyed by the install it ran from and served each registered
session for that session's whole lifetime, which for a background job
lasts until the job is deleted. Since #4557 a hook hands itself to the
newest installed copy before it starts the bridge, so each commit on
main that a long-lived session lived through left one more bridge, none
of which that session's sandboxed `leaf` (the loaded copy's client)
could reach. On Max's Mac this left 85 bridges, up to 20 days old and
about 1.85 GB in all; 78 had been started by a copy the hook was handed
to, and 68 of the 70 installs still on disk ship the same `bridge.py`.

Now the bridge is keyed by a hash of `bridge.py` itself, so every copy
that ships the same file shares one bridge, and a request carries the
client's interpreter, which the bridge runs, so each command still runs
as its own copy's Leaf. The hook registers the session before the
hand-off as well as after it, so a loaded copy whose `bridge.py` differs
still gets its bridge. Registrations are named by session and lifetime,
so two processes of one session each keep the bridge, and `serve`
imports the lifetime check at start, since a shared bridge can outlive
the install that started it.

To weigh: copies now share a wire contract, scoped by the file hash. A
request naming its interpreter adds nothing to what the token already
allowed, since the token holder already set each command's environment.
Untested: a bridge outliving its own install; the eager import covers
that case.

Independent review by Codex, two rounds; its first round rejected an
earlier version that started bridges only from the copy Claude Code
loaded. Tests: `tests/process/test_bridge.py`, the hook model and
registration tests, `pnpm run check:browser`; each new test fails with
its part of the fix reverted.

Requested by Max via a dispatched investigation into processes outliving
their sessions.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 20, 2026
… copy installed since

A session's hooks, watch and bare `leaf` commands now run the plugin copy the
session loaded, as Claude Code intends: it puts that copy's `bin/` on the Bash
tool's PATH and names it in each hook. They still hand themselves to a checkout
the session selected. This reverses #4557, which handed every session to the
copy `installed_plugins.json` records, and deletes that hop from the launcher
(`bin/leaf`) and `session_runtime`, with the pages follower's restart on it.

The hop stopped buying what it was added for a day after it landed. #4557 fixed
idle sessions refusing pages a newer Leaf had re-vendored (94% of pickup wait,
20-27 Oct). #4551 then stopped copying the layer into pages, so the protocol
covers only the log and records, and only the session holding a page renews it
(`vendoring.renew_layer`), so a session's pages stay at its own copy's protocol.
Of 36 live sessions on 2026-11-19, 34 ran a replaced copy, and 33 were at the
installed copy's protocol (51) anyway; the protocol moved 15 times in the last
1,318 commits.

The hop also ran code the session's instructions didn't describe: two live
sessions loaded a copy at protocol 44 whose references tell the agent to pass
`--markup` to a thread message, a flag the installed Leaf dropped (#6208). With
it gone, a session's instructions, hooks module and Python are one copy, except
under a selected checkout, and Leaf no longer reads Claude Code's undocumented
install record or cache marks. An update now reaches a running session when it
runs /reload-plugins or starts anew. A copy that has lost its project still
refuses, naming /reload-plugins, rather than leave uv to run the caller's
`python`.
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