Per-worktree access isolation for sub-agents and parallel sessions
Problem
When running multiple Claude Code sessions in parallel across git worktrees (e.g., one session per PR), there is no mechanism to prevent a session working in worktree A from reading or writing files in worktree B.
Current options and their limitations:
| Approach | Behavior |
|---|---|
| additionalDirectories with parent path (e.g., .worktrees/) | Grants access to all sibling worktrees |
| additionalDirectories with specific worktree path | Safe but requires manual config per worktree |
| isolation: "worktree" on Agent tool | Creates a new temporary worktree (isolated copy), but doesn't restrict access to other configured paths |
| EnterWorktree / ExitWorktree | Switches working directory but doesn't restrict access to other paths |
| Launch separate claude process per worktree | Effectively isolated, but no programmatic way to enforce it |
Desired Behavior
When a sub-agent or session is scoped to a specific worktree, it should only be able to access files within that worktree — not sibling worktrees or other directories outside its scope. This would enable safe, truly parallel workstreams without risk of cross-contamination.
Proposed Plan
- Scoped
additionalDirectoriesper agent/session — Allowisolation: "worktree"(or a new parameter) to restrict the sub-agent's file access to only the assigned worktree directory, overriding any broaderadditionalDirectoriesfrom the parent session.
- Worktree-aware permission boundaries — When a session enters a worktree (via
EnterWorktreeorisolation), automatically restrictRead,Edit,Write, andBashfile operations to paths within that worktree (plus the shared.gitmetadata).
- Explicit opt-in for cross-worktree access — If cross-worktree access is genuinely needed, require an explicit flag (e.g.,
isolation: "worktree-shared") rather than making it the default.
Use Case
Running parallel Claude Code sessions on different PRs/branches, each in its own worktree. Each session should be sandboxed so it cannot accidentally modify or read code from another in-progress PR.
🤖 Generated with Claude Code
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗