[Bug] Sandbox profile E2BIG with many git worktrees due to unbounded ancestor rule expansion
Bug Description
Title: Sandbox Seatbelt profile grows ~7–15 KB per git worktree → E2BIG on all sandboxed commands (regression in v2.1.196)
Summary: On macOS, sandbox-exec profile is passed inline in argv. Since v2.1.196 the profile generator enumerates .git/worktrees/* and injects 3 write-deny paths per registered worktree (config.worktree, config.worktree.lock, commondir). The pre-existing ancestor-expander (msa) then emits (deny file-write-unlink/-create …) rules for each path and every ancestor directory, ×2 kinds, ×3 tagged lines, with no dedup across entries. So the profile scales ~linearly with worktree count and crosses the ~1.04 MB single-exec argv limit, after which every sandboxed Bash command fails with E2BIG: argument list too long, posix_spawn '/bin/zsh' — even trivial ones.
Impact: Repos with many Claude Code worktrees become completely unusable in sandbox mode. Cliff is ~50 worktrees. Confusingly, the session that hit it stays broken after cleanup because the profile is cached at session start.
Regression window (verified by diffing on-disk binaries):
- 2.1.92, 2.1.114 — no .git/worktrees reference in profile; immune at any worktree count
- 2.1.196 (2026-06-30) — per-worktree enumeration added ← regression
- 2.1.197, 2.1.198 — same
Repro:
- In a git repo, create ~60+ worktrees (git worktree add).
- Run any sandboxed Bash command.
- → E2BIG … posix_spawn '/bin/zsh'.
Suggested fix: Dedup the ancestor (deny … (literal \<dir\>)) rules across entries, or replace per-worktree enumeration with a single (subpath "<repo>/.git/worktrees") deny, or bound/omit the (with message …) tag on ancestor rules. Any one collapses the size dramatically.
Env: macOS (Darwin 25.2.0), arm64, Claude Code 2.1.198, getconf ARG_MAX = 1048576; measured effective single-exec argv limit ≈ 1.044 MB.
Environment Info
- Platform: darwin
- Terminal: ghostty
- Version: 2.1.198
- Feedback ID: f695d7e6-f10c-4f4f-8ca9-44ce1578e4f3
Errors
[]This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗