Worktreeサブエージェント分離パターン
ワークツリー中級
2026年の機能で各Agentツール呼び出しに一時的なworktreeを自動作成します。変更なしなら自動削除、変更ありならブランチとパスが返されマージ可能です。
worktreeサブエージェント分離並列git
パターンコード
# Worktree Subagent Isolation Pattern
## Basic Usage — isolation: "worktree"
```typescript
// In CLAUDE.md or custom subagent definition
// Each subagent gets its own git worktree automatically
// Parent agent launches isolated subagents
await Promise.all([
agent({
prompt: "Implement user authentication module",
isolation: "worktree", // <-- auto-creates .claude/worktrees/<random>
}),
agent({
prompt: "Implement payment processing module",
isolation: "worktree", // <-- separate worktree, no conflicts
}),
agent({
prompt: "Write integration tests for both modules",
isolation: "worktree",
}),
]);
```
## How It Works
```
Main worktree (your working directory)
├── .claude/worktrees/
│ ├── auth-abc123/ ← subagent 1's isolated copy
│ │ └── (full repo clone on separate branch)
│ ├── payment-def456/ ← subagent 2's isolated copy
│ │ └── (full repo clone on separate branch)
│ └── tests-ghi789/ ← subagent 3's isolated copy
│ └── (full repo clone on separate branch)
```
## Lifecycle
1. **Creation**: `git worktree add .claude/worktrees/<name> -b <branch>`
2. **Execution**: Subagent runs in isolated directory
3. **Completion**:
- If NO changes → worktree auto-removed (clean)
- If changes made → worktree + branch preserved
- Parent receives: { worktreePath, branch, hasChanges }
## Merge Strategy
```bash
# After subagents complete, merge results
git merge worktree/auth-abc123 --no-ff -m "feat: add auth module"
git merge worktree/payment-def456 --no-ff -m "feat: add payment module"
# Clean up merged worktrees
git worktree remove .claude/worktrees/auth-abc123
git worktree remove .claude/worktrees/payment-def456
```
## Benefits Over Manual Worktrees
- No setup scripts needed — just add `isolation: "worktree"`
- Auto-cleanup on no-change subagents (no stale worktrees)
- Branch naming handled automatically
- Works with existing git hooks and CI
- Compatible with Agent Teams for team-level isolation
このパターンをプロジェクト設定にコピーして適用してください。
実行プレビュー
Worktreeサブエージェント分離パターン
Worktreeサブエージェント分離パターンについて
Claude Codeパターンは、複雑な開発シナリオに対応するための実証済みアーキテクチャ設計とワークフロー構造です。Worktreeサブエージェント分離パターンは中級レベルのワークツリーパターンで、プロジェクトに合わせて応用できるテスト済みの再現可能なアプローチを提供し、より効率的で一貫した成果を実現します。