Skip to content

Share one sandbox bridge across Leaf copies, so plugin updates stop multiplying them - #5982

Merged
max-sixty merged 3 commits into
mainfrom
leaf-process-leaks
Nov 15, 2026
Merged

max-sixty merged 3 commits into
mainfrom
leaf-process-leaks

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

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

…ultiplying them

A bridge was keyed by the install it ran from, and each served every
session registered with it for that session's whole lifetime: for a
background job, until the job is deleted. Since #4557, `hooks.main` hands
each hook to the session's delivering Leaf (the copy Claude Code installed
since, or a selected checkout) before `cmd_hook` started the bridge, so
every plugin update, one per commit on main, started another bridge, keyed
by the newer copy. None of those could serve the session: a sandboxed bare
`leaf` runs the client of the copy Claude Code loaded, which looks only
for its own install's bridge.

On a machine with many background jobs this left 85 bridges running, some
20 days old, about 1.85 GB in all. 79 stayed up for a job's registration.
Each bridge's environment names the CLAUDE_PLUGIN_ROOT of the hook that
started it: 78 were started by a copy the hook had been handed to. One job
was registered with 19 bridges in under six days. Of the 70 installs still
on disk, 68 ship the same `bridge.py`.

Now:

- `bridge.KEY` hashes `bridge.py` itself, so every copy that ships the same
  file, a newer plugin copy, a checkout run by path or a host launcher's,
  reaches one bridge. A copy that changes the file, and so perhaps its
  frames, starts its own.
- A request carries the client's interpreter and the bridge runs it, so a
  command runs as the Leaf of the copy that sent it, whichever copy started
  the bridge. The token already let its holder choose a command's
  environment, so naming the interpreter adds no reach.
- `hooks.main` opens the sandbox's road out before the hand-off, so the copy
  Claude Code loaded registers the session even where its `bridge.py`
  differs from the delivering Leaf's; the delivering Leaf registers too.
- A registration is named by its session and lifetime, so two processes of
  one session, each living as its own pid, keep the bridge until both end;
  copies now share registrations, which made the overwrite more reachable.
- `serve` loads the sweep's lifetime rule as it starts, since a shared
  bridge may outlive the install that started it, and a sweep that failed
  to import would leave it serving for good.

A background job's next sitting on a worker that loaded a newer copy now
reaches the bridge its earlier sitting kept, wherever `bridge.py` is
unchanged, which the job lifetime promised and per-install keys did not
deliver. Where `bridge.py` changed, a script run before that sitting's
first hook still finds no bridge, as before.
leaf-agent
leaf-agent previously approved these changes Nov 14, 2026
# Conflicts:
#	src/leaf/hooks.py
@max-sixty
max-sixty merged commit 546ac5b into main Nov 15, 2026
5 checks passed
@max-sixty
max-sixty deleted the leaf-process-leaks branch November 15, 2026 01:55
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