Skip to content

Move a session's hooks to a checkout only for a page the session holds - #5229

Merged
leaf-agent merged 3 commits into
mainfrom
grind/subagent-claims
Nov 3, 2026
Merged

leaf-agent merged 3 commits into
mainfrom
grind/subagent-claims

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

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

max-sixty and others added 3 commits November 3, 2026 10:38
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.

A checkout now selects itself as a command claims a page for the session
(`server start`, `server run`, `page claim`), or as it initializes or renews a
page the session holds. `layer-registry.md` already stated that rule ("the
checkout that last initialized one of its pages"); the code ignored whose page
it was. Serving a page from a checkout still hands the session's hooks to it,
now at the claim rather than the init before it.

Claude Code runs a subagent's shell commands with its parent's session
variables and nothing naming the subagent (2.1.317), so a subagent's
`server start` on a scratch page still claims it for the parent and moves the
parent's Leaf. `ClaudeCodeHarness` records that fact.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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`). The previous commit selected after the claim
commands prepared delivery, so preparation joined the adapter about to leave
and nothing replaced it. `server start`, `server run` and `page claim` now
prepare the claim, select, then prepare delivery, the order a user preview
already follows. A checkout's init or renewal of a page the session holds now
readies delivery after it selects; on main that left a Codex task with no
adapter.

`test_a_claim_makes_its_checkout_the_sessions_leaf_before_preparing_delivery`
checks, for all three claim commands, that the selection is in place when
delivery preparation starts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…Leaf

A checkout's init or renewal of a page its session holds started the
session's delivery again every time, even where the selection stayed: from an
installed copy, which selects nothing, or from the checkout already selected.
A Codex watcher task, which delivers through its own `leaf wait`, would then
get an adapter that takes the wait lease. `select_launcher` and
`select_checkout` now say whether they changed the selection, and the init
starts delivery again only then, which is when a Codex adapter running from
the former Leaf leaves.

The held-page rule moves from `select_checkout`'s docstring to
`vendoring._deliver_from_here`, which applies it; the claim commands need no
such check, since the page they claim becomes the session's.

`test_a_checkout_that_takes_over_a_held_page_starts_its_sessions_delivery_again`
checks, for `page init` and a renewal, that delivery starts once, after the
checkout is selected, and not again when nothing changed; the unheld-page test
checks that delivery never starts. Each fails with the restart removed or made
unconditional.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@max-sixty
max-sixty marked this pull request as ready for review November 3, 2026 20:49
@leaf-agent
leaf-agent merged commit f591034 into main Nov 3, 2026
5 checks passed
@leaf-agent
leaf-agent deleted the grind/subagent-claims branch November 3, 2026 21:00
max-sixty added a commit that referenced this pull request Nov 10, 2026
…5558)

Adds one sentence to "Preview a page" in `/developing-leaf`: serve a
scratch or repro page only with `leaf-dev probe`, `stills` or a plain
`leaf-dev preview --source`, never `bin/leaf server start` or `page
claim`.

Those commands bind the page to the session running them, which for a
subagent is its parent. Seven asks from a subagent's repro deck landed
on a parent's turn-end checks and On me list this week. Since #5229 the
dev tools neither claim a page nor move the session's Leaf, so they are
the isolated test path; binding to a live session stays explicit.

This changes agent instructions, so Max lands it.

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

Co-authored-by: Claude Sonnet 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