Skip to content

Run the installed Leaf from a replaced plugin copy's launcher, before any Python - #6575

Closed
max-sixty wants to merge 10 commits into
mainfrom
route-installed-copy-in-launcher
Closed

max-sixty wants to merge 10 commits into
mainfrom
route-installed-copy-in-launcher

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

A session keeps the plugin copy it loaded while Claude Code installs a new copy for each commit (212 in the cache today, about 30 a day). Until now, every hook and bare leaf in an older session started the old copy's uv and Python only to find the installed copy (installed_launcher) and exec it. That cost about 100 ms a call, broke once Claude Code deleted the old copy's files (which #6567 patched with a shell copy of the rule), and was decided by where the running package was loaded from, which is how a misidentified copy looped until uv's recursion limit.

bin/leaf now decides before anything else. Claude Code's cache sweep marks every copy installed_plugins.json no longer names with .orphaned_at (211 of the 212 here). A cache copy that is marked or has lost its project runs the first absolute, executable launcher the record names, compared by path, unless that is its own; a launcher whose project is gone with no other copy to run keeps #6567's refusal. That is #6567's block applied to every replaced copy, so installed_launcher goes and delivering_launcher is just the selected checkout or this Leaf. A replaced copy never starts uv or builds an environment, the hop costs 10–25 ms, and the installed copy's own launches cost what they did on main. The pages follower ends to be restarted once its copy turns replaced after it has seen it otherwise. A copy replaced as it starts is one the launcher ran anyway, as where the record still names a marked copy, so the follower restarts at most once whatever the launcher decides, and only the launcher reads the record.

Why keep a hop at all: a session that runs only the copy it loaded strands its pages once a selected checkout that renewed them to a newer protocol is removed (202 of the 447 sessions Leaf records as unended deliver through a checkout, and 28 of the 40 latest selections name a removed worktree), and whenever another session's page init moves a page it holds. The installed copy is what such a session can still read.

Worth weighing: .orphaned_at and installed_plugins.json are Claude Code's undocumented files; Leaf already read the second. If Claude Code marks a copy in the fraction of a second between the launcher's check and the follower's first look, that follower stays on the old copy until the module next starts it, and that session's line forks wait out their grace meanwhile; at 30 updates a day that is roughly one follower start in 10,000.

Asked by max-sixty, who hasn't read the diff. The launcher test fails on main's launcher, on one that skips its own copy rather than stopping there, and on one that drops either check; the follower test fails on one that ignores the state its copy started in or never lets it clear. The hook registration, hook gate, bridge, follower, forks and hook model tests pass. The full claude-code agent journey passed all 16 steps on the first commit; the later ones change only what a replaced copy and its follower do, which a journey's checkout never is. Two real copies of this tree in a fake cache: the replaced copy runs the installed one, builds no environment, and keeps reaching it once its project is deleted.

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

… any Python

A session keeps the plugin copy it loaded, and Claude Code installs a new copy
for each commit the marketplace sweep picks up: 212 copies sat in the cache on
2026-11-19, about 30 a day. Every hook and bare command in an older session
started the old copy's uv and Python only to find, in `installed_launcher`, that
another copy delivers, and exec it. That hop cost about 100 ms per call
(`leaf --version`: 210-230 ms through an old copy, 110-130 ms direct), needed the
old copy's files to still be there (#6567 added a shell copy of the rule for a
copy that had lost them), and decided by where the running package was loaded
from, which is how a launcher importing another tree's `leaf` looped until uv's
recursion limit.

Claude Code marks each copy it replaces with `.orphaned_at` (211 of the 212; the
installed copy alone has none), so `bin/leaf` now checks for that marker first
and execs the launcher `installed_plugins.json` records, compared by path, or
refuses where none other is recorded. That is #6567's block applied to every
replaced copy rather than only to one that lost `pyproject.toml`. A replaced
copy never starts uv, so it no longer builds an environment, and the hop now
costs about 20 ms (`leaf --version`: 102-113 ms through the replaced copy, 76-104
ms direct, in a fake cache of two copies of this tree). Version managers that redirect (rustup, Volta, mise) make
the same choice: decide by path before the program starts.

`installed_launcher` goes, and `delivering_launcher` is the selected checkout
or this Leaf. The follower moves on once its own copy is replaced
(`session_runtime.replaced`), which the module answers by starting it from its
launcher, which runs the installed copy or refuses, so a follower can't
restart in a loop. The stubs #6567 gave tests that copy the launcher alone go.

The replaced-copy test fails on main's launcher, where a whole replaced copy
runs its own project.
… only gating the read

Claude Code's cache sweep (2.1.329) reads `installed_plugins.json`, removes
`.orphaned_at` from every copy it records and marks every copy it doesn't, so
the marker is a lagging copy of the record. A marked copy the record still
names, as a version folder installed again until the next sweep, refused every
call with advice a reload couldn't follow. Now the record decides: a marked copy
runs the first absolute, executable launcher it names unless that is its own,
and otherwise runs itself, so the refusal goes. `session_runtime.replaced`
follows the same rule for the pages follower, and the launcher test holds the
two to the same answer in each case, since a follower that moved on where the
launcher runs its own copy would be started again and again. That test fails on
main's launcher, on one that skips its own copy rather than stopping there, and
on a `replaced` that reads the marker alone.

The bin/leaf comment no longer says the session keeps working once the old
copy's files go: the hooks module, the Stop watch and `hooks.json` still load
from it.
`replaced` read Claude Code's record by the launcher's rule, a second reader of
`installed_plugins.json` that had to agree with the shell's on every input: where
they differed, as with no jq on PATH, the follower ended and the module started
it again in the same copy, over and over. Now `replaced` is the marker alone, and
the follower moves on only when its copy is marked after it started. One marked
before was run by its launcher anyway, as where the record still names it, so
the follower stays, and whatever the launcher decides, a follower restarts at
most once. The launcher alone reads the record.

The new follower test fails on a `_moved_on` that ignores when the copy was
marked.
…eared

Claude Code's sweep never rewrites a mark that is already there, so a follower
that started in a copy marked while still recorded never saw a later genuine
replacement. The follower now forgets the mark it started with once a pass finds
the copy unmarked, which the sweep does where the record names the copy, and a
mark after that moves it on. The new follower test runs the whole follower
through that sequence and fails on one that ignores the mark at its start or
never lets it clear.

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

This drops #6567's other trigger: the hop now depends only on .orphaned_at. #6562 saw a copy the cleanup had partly deleted, with pyproject.toml and most of the tree gone and bin/leaf still there, but nobody checked whether .orphaned_at survived. Node's recursive removal goes through a directory in readdir order, and a root dotfile sorts before bin/ and pyproject.toml, so a removal that stops partway can delete the marker and keep the launcher. A copy in that state now skips the hop and reaches uv run --project with no project. That runs the caller's python, which is #6562 again: either No module named leaf, or the Leaf of the checkout the caller is in. The new launcher test deletes nothing and keeps the marker, so it doesn't cover this state.

Taking either sign as the trigger keeps everything this PR gains and costs one test. Once the project is gone, a copy that finds no launcher to hop to should still refuse with #6567's message rather than reach uv.

Comment thread bin/leaf Resolved
…se where none is recorded

A removal that stops partway can take a copy's root `.orphaned_at`, which sorts
first, and keep its launcher, so the mark alone missed the state #6562 reported:
the project gone, and uv left to run the caller's `python`. A copy in Claude
Code's cache now reads the record when it is marked or has lost its project, and
#6567's refusal comes back for a launcher whose project is gone with no other
copy to run, so uv never runs without a project. `session_runtime.replaced`
takes the same two checks, so the follower moves on when the launcher would
look. Reading the record by parameter expansion rather than `dirname` costs
about 4 ms a launch rather than 21; the two checks keep it off the installed
copy's launches, which measured the same as main's (63-66 ms for
`leaf --version`).

The launcher test now fails on main's launcher, on one that skips its own copy,
and on one that drops either check; the project stubs the hook tests need for
the refusal come back.
leaf-agent
leaf-agent previously approved these changes 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`.
@max-sixty max-sixty changed the title Run the installed Leaf from a replaced plugin copy's launcher, before any Python Run each Claude Code session's own Leaf copy, and drop the hop to the copy installed since Nov 20, 2026

@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 description leaves out the biggest cost here. Besides deleting the hop for a replaced copy, this also deletes #6567's launcher fallback. On main, a copy that has lost its project still runs the copy installed_plugins.json records. With this change it refuses. #6562 saw Claude Code delete the files of a copy a long-lived session was still running. In that state every hook in the session, the Stop hook and pages follower included, and every bare leaf command now exits with has lost its files. That holds until the user runs /reload-plugins. So the session stops delivering the user's comments entirely. It isn't just held at an older Leaf. "Worth weighing" names only the second effect.

Please add this to the description so the maintainer can weigh it. The other choice is to keep main's lost-project block in bin/leaf, about 14 lines of shell. It hands over only when the loaded copy can't run at all. That way the only remaining dependence on Claude Code's undocumented install record is the one case where refusing is the sole alternative. Either choice is defensible. The trade between them is the maintainer's to make.

…p to the copy installed since"

This reverts commit f75e61a.
@max-sixty max-sixty changed the title Run each Claude Code session's own Leaf copy, and drop the hop to the copy installed since Run the installed Leaf from a replaced plugin copy's launcher, before any Python Nov 20, 2026
leaf-agent
leaf-agent previously approved these changes Nov 20, 2026
# Conflicts:
#	tests/model/test_forks.py
@max-sixty

Copy link
Copy Markdown
Owner Author

Closing unmerged: the maintainer chose to keep a session's hooks on the copy it loaded rather than hand them to the installed copy, which #6758 builds.

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

@max-sixty max-sixty closed this Nov 22, 2026
max-sixty added a commit that referenced this pull request Nov 22, 2026
…ded (#6758)

A running Claude Code session's Leaf hooks, pages follower and bare
`leaf` commands now run the plugin copy the session loaded, until
`/reload-plugins` or a new process (a background job's wake, a resume)
loads the copy Claude Code installed since. They used to hand themselves
to the copy `installed_plugins.json` names, which left the hooks module
and skills on the loaded copy while the Python ran another: 828 of 1,082
active minutes across 36 job processes over a day and a half.

Checkout selection is unchanged; whether to keep it is a separate
decision, postponed for now. A selection whose checkout is gone now
falls back to the session's own copy rather than the installed one. A
loaded copy that lost its files refuses at `bin/leaf`, naming
`/reload-plugins`, instead of running another copy. The `newer-layer`
refusal now names `/reload-plugins` for Claude Code.

Worth weighing: a Leaf fix reaches a background job at its next wake and
a terminal session at its next restart or reload, rather than at the
next hook. When the newer Leaf behind a `newer-layer` refusal was a
checkout ahead of the installed copy, the reload advice won't help, and
the finding can't tell the two cases apart.

The maintainer chose this on the Leaf page about keeping a session on
one version of Leaf (#8, first part). It supersedes #6575, which kept
the hand-off and moved it into the shell launcher. Tests:
`tests/model/test_hook_registration.py` (two tests rewritten for the new
behaviour), the follower, module-restart, bridge, selection and
`newer-layer` tests, and `test_product_page.py`, all passing. A fresh
reviewer read the diff.

> _This was written by Claude Code on behalf of max-sixty_
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