Repository navigation
Bound Nested Claude Sessions' Tool-Call Scopes as One Tree and Reap Their Orphans - #2599
Conversation
Every tool-call scope now joins a slice named for the root session of its chain of nested claude sessions, carrying a total ceiling of 32768 tasks and 50% of RAM, while each scope keeps its own 8192 and 25%. The root id travels to nested sessions in AGENT_CONTAINMENT_ROOT. Each scope records the pid of the agent that ran it. The SessionEnd sweep stops scopes in its tree whose agent is gone, naming the nested session, and the root ends its empty tree and reverts the ceiling. `stray-process-sweep.py --orphans` reports, read-only, every scope on the machine whose agent is gone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sweep judged agent liveness once, so a scope whose agent ended only when a shallower orphan was stopped stayed running unreported. It now re-reads the process table after each stop, up to four rounds, inside a 20 second share of the hook's timeout, and names each stopped scope from the table read before its stop. Only a session handed no root ends its tree, and the root reports another session's live scope that keeps the tree in place. README sentences are shortened and the stop's bound is stated consistently. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The root's report listed every scope left in its tree as belonging to a running session, including ones it had just judged ended and those recording no agent. It now names a scope as live only where its recorded agent runs, lists scopes recording no agent apart, and claims neither when the process table could not be read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configuration
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2599 +/- ##
========================================
Coverage 58.85% 58.85%
========================================
Files 16 16
Lines 8181 8181
========================================
Hits 4815 4815
Misses 3366 3366
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved cleanup deadline, PID reuse, failed-read handling, and missing-tree issues remain.
3 open findings
What changed in this PR
Extends Claude tool containment across nested session trees with shared ceilings and orphan cleanup.
Changes:
- Adds root propagation and tree-wide task/memory limits.
- Reaps nested orphan scopes and adds
--orphansreporting. - Expands self-tests and operational documentation.
Outstanding findings affect deadline enforcement, PID reuse, incomplete property reads, and missing-tree handling in stray-process-sweep.py.
| File | Summary |
|---|---|
host-setup/agent-safety/claude/tool-containment.py |
Implements tree slices, aggregate limits, root propagation, and agent tracking. |
host-setup/agent-safety/claude/test_install.py |
Runs both hook self-tests on POSIX. |
host-setup/agent-safety/claude/stray-process-sweep.py |
Reaps and reports orphaned scopes; has unresolved review findings. |
host-setup/agent-safety/claude/README.md |
Documents tree containment and orphan recovery. |
🧠 Review effort: Lite
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
A tool-call scope `systemctl show` did not answer for has no known slice, and reading it as outside the tree could let the root end a tree still holding it, so an incomplete answer is a failure to list. Every systemctl call the tree phase makes now runs inside its share of the hook's time, and one cut short stops nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
A recorded local strict-review pass covers head |
…es (#2601) ## Summary Promotes develop to main. - #2600: count a finding a Copilot overview names only in its `Review findings:` prose list, in `pr_review.py`. - #2599: bound nested Claude sessions' tool-call scopes as one tree and reap their orphans. Fixes #2283 Fixes #1903 🤖 Generated with [Claude Code](https://claude.com/claude-code)


Summary
Closes on promotion: #1903
A
claudesession started from a tool call ran its own tool calls in fresh scopes, so a chain of agents calling agents had no total ceiling. Stopping the parent's scopes killed the nestedclaudebefore its own SessionEnd sweep ran, which left its scopes running unreported. This bounds the chain as one tree and reaps what it orphans, without the rejected design from #1900 of running nested tool calls inside the parent's scope.Tree ceiling (
tool-containment.py).claude-tree-<root session>.slice. The root's id reaches nested sessions asAGENT_CONTAINMENT_ROOT, which the prefix puts in every contained command's environment, so a nestedclaudeand its own tool calls inherit it. Each scope keeps its own name and its own ceiling.systemctl --user set-property --runtimebefore the first scope joins. Later calls see the slice's cgroup and skip that step, so the steady-state cost is onestat. If setting the ceiling fails, the command still runs under its own scope's ceiling, stays out of the uncapped slice, and prints why.claudeancestor of the prefix. That record is the usable test the issue comment asked for.Ceiling values. 32768 tasks and 50% of RAM for the tree, against 8192 and 25% per scope, overridable as
AGENT_CONTAINMENT_TREE_TASKS_MAXandAGENT_CONTAINMENT_TREE_MEMORY_MAX.claude. It takes at least two scopes at full use together to reach it, and then the largest process is most likely a build.pid_max. The incident in A Probe's Self-Recursive Command Shim Ran Unbounded and Unreaped, Taking Down the Host #1890 reached about 568k processes.claude -pworkers included, so a run that fans out heavy builds shares it. The override is there if that proves tight.Orphan reaping (
stray-process-sweep.py).Sliceproperty.systemctl show -p ConsistsOfon a slice does not list its scopes, which an earlier iteration of this branch hit on this host when its slice stop ended a nested scope unreported.stray-process-sweep.py --orphansis a read-only, machine-wide report for a session that ended with no sweep at all. It lists each scope whose agent is gone, grouped by tree, with the session id and a stop line, and lists unrecorded scopes apart as unknown. Exit 0 means clean, 1 means a finding, and 2 means it could not check. The kit README now points the maintainer at it instead of at "stop only the ones whose session has ended".Tests. Both hooks' self-tests gain the tree matrix.
test_install.pynow runs both self-tests (POSIX only), so CI covers them, since before this change only the guard's self-test ran there.Follow-Up
host-setup/agent-safety/README.mdlies outside this change's file claim. It does not yet state the tree bound, or the stop judged by a scope's recorded agent. That issue also carries one unverified question about a nested session's hook environment.Local Strict Review
Four passes ran. The first found four things. A chain two levels deep left its deepest scopes unreported, now fixed by the round loop. The tree-ending guard was too loose, and is now
if inherited. The hook's time budget was at risk, now bounded by a deadline. And requirement 9 had drifted, now #2598. The second pass found the root's report calling a just-orphaned scope live, now fixed. The third found a docstring nit, now fixed, and the fourth found nothing.Verification (WSL2, Debian 13, systemd 257)
Simulated chain: a fake root agent (a
claude-named symlink topython3) runs a tool call through this branch's prefix. That call starts a fake nested agent, which sets its own session id and runs asetsid sleeptool call. All ids are constructed.Afterwards, no
claude-tree-f1903*slice, scope, or drop-in remained, andpgrepfound nothing.The crash case killed the nested agent with SIGKILL and ran no sweep.
--orphansthen reported the nested scope undertree claude-tree-f1903aaa...slice, with its session id,agent pid ... gone, and itssleep. It exited 1 and handed oversystemctl --user stop <scope>, and that line, plusstopandreverton the slice, cleared everything./procand the slicestat.os.getuidandos.posix_spawnremoved to stand in for Windows.test_install.pypasses, 157 tests. Mutation checks on the new cases were all caught, the round loop, the root guard, the deadline, and the live-scope report included.comments), markdownlint, cspell, editorconfig, and the eol check are clean.🤖 Generated with Claude Code