claude-capsule-kit 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +281 -0
- package/agents/agent-developer.md +206 -0
- package/agents/architecture-explorer.md +90 -0
- package/agents/brainstorm-coordinator.md +120 -0
- package/agents/code-reviewer.md +135 -0
- package/agents/context-librarian.md +227 -0
- package/agents/context-manager.md +151 -0
- package/agents/database-architect.md +107 -0
- package/agents/database-navigator.md +136 -0
- package/agents/debugger.md +121 -0
- package/agents/devops-sre.md +102 -0
- package/agents/error-detective.md +128 -0
- package/agents/git-workflow-manager.md +212 -0
- package/agents/github-issue-tracker.md +252 -0
- package/agents/product-dx-specialist.md +99 -0
- package/agents/refactoring-specialist.md +159 -0
- package/agents/security-engineer.md +102 -0
- package/agents/session-summarizer.md +126 -0
- package/agents/system-architect.md +103 -0
- package/bin/cck.js +1624 -0
- package/commands/crew-setup.md +75 -0
- package/commands/load-session.md +68 -0
- package/commands/sessions.md +55 -0
- package/commands/statusline.md +51 -0
- package/commands/sync-disable.md +35 -0
- package/commands/sync-enable.md +32 -0
- package/commands/sync.md +31 -0
- package/crew/lib/activity-monitor.js +128 -0
- package/crew/lib/crew-config-reader.js +255 -0
- package/crew/lib/health-monitor.js +171 -0
- package/crew/lib/merge-pilot.js +340 -0
- package/crew/lib/prompt-generator.js +268 -0
- package/crew/lib/role-presets.js +63 -0
- package/crew/lib/task-decomposer.js +382 -0
- package/crew/lib/team-spawner.sh +557 -0
- package/crew/lib/team-state-manager.js +155 -0
- package/crew/lib/worktree-gc.js +357 -0
- package/crew/lib/worktree-manager.sh +700 -0
- package/docs/AGENT_ROUTING_GUIDE.md +655 -0
- package/docs/AGENT_TEAMS_WORKTREE_MODE.md +681 -0
- package/docs/BEST_PRACTICES.md +194 -0
- package/docs/CAPSULE_DEGRADATION_RCA.md +577 -0
- package/docs/SKILLS_ORCHESTRATION_ARCHITECTURE.md +455 -0
- package/docs/SUPER_CLAUDE_SYSTEM_ARCHITECTURE.md +1647 -0
- package/docs/TOOL_ENFORCEMENT_REFERENCE.md +418 -0
- package/hooks/check-refresh-needed.sh +77 -0
- package/hooks/detect-changes.sh +90 -0
- package/hooks/keyword-triggers.sh +66 -0
- package/hooks/lib/crew-detect.js +241 -0
- package/hooks/lib/handoff-generator.js +158 -0
- package/hooks/load-from-journal.sh +41 -0
- package/hooks/post-tool-use.js +212 -0
- package/hooks/pre-compact.js +77 -0
- package/hooks/pre-edit-analysis.sh +68 -0
- package/hooks/pre-tool-use.sh +212 -0
- package/hooks/prompt-submit-memory.sh +87 -0
- package/hooks/quality-check.sh +48 -0
- package/hooks/session-end.js +133 -0
- package/hooks/session-start.js +439 -0
- package/hooks/stop.sh +66 -0
- package/hooks/suggest-discoveries.sh +84 -0
- package/hooks/summarize-session.sh +122 -0
- package/hooks/sync-to-journal.sh +77 -0
- package/hooks/sync-todowrite.sh +37 -0
- package/hooks/tool-auto-suggest.sh +77 -0
- package/hooks/user-prompt-submit.sh +71 -0
- package/lib/audit-logger.sh +120 -0
- package/lib/sandbox-validator.sh +194 -0
- package/lib/tool-runner.sh +274 -0
- package/package.json +67 -0
- package/scripts/postinstall.js +4 -0
- package/scripts/show-capsule-visual.sh +103 -0
- package/scripts/show-capsule.sh +113 -0
- package/scripts/show-deps-tree.sh +66 -0
- package/scripts/show-stats-dashboard.sh +52 -0
- package/scripts/show-stats.sh +79 -0
- package/skills/code-review/SKILL.md +520 -0
- package/skills/crew/SKILL.md +395 -0
- package/skills/debug/SKILL.md +473 -0
- package/skills/deep-context/SKILL.md +446 -0
- package/skills/task-router/SKILL.md +390 -0
- package/skills/workflow/SKILL.md +370 -0
- package/templates/CLAUDE.md +124 -0
- package/templates/crew-config.json +21 -0
- package/templates/settings-hooks.json +74 -0
- package/templates/statusline-command.sh +208 -0
- package/tools/context-query/context-query.js +312 -0
- package/tools/context-query/context-query.sh +5 -0
- package/tools/context-query/tool.json +42 -0
- package/tools/dependency-scanner/dependency-scanner.sh +53 -0
- package/tools/dependency-scanner/tool.json +8 -0
- package/tools/find-circular/find-circular.sh +41 -0
- package/tools/find-circular/tool.json +36 -0
- package/tools/find-dead-code/find-dead-code.sh +41 -0
- package/tools/find-dead-code/tool.json +36 -0
- package/tools/impact-analysis/impact-analysis.sh +99 -0
- package/tools/impact-analysis/tool.json +38 -0
- package/tools/progressive-reader/progressive-reader.sh +14 -0
- package/tools/progressive-reader/tool.json +69 -0
- package/tools/query-deps/query-deps.sh +69 -0
- package/tools/query-deps/tool.json +34 -0
- package/tools/stats/stats.js +299 -0
- package/tools/stats/stats.sh +5 -0
- package/tools/stats/tool.json +34 -0
- package/tools/token-counter/README.md +73 -0
- package/tools/token-counter/token-counter.py +202 -0
- package/tools/token-counter/tool.json +40 -0
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
# Agent Teams Worktree Mode - Architecture & Implementation Plan
|
|
2
|
+
|
|
3
|
+
**Version:** 1.0 (Draft)
|
|
4
|
+
**Date:** February 2026
|
|
5
|
+
**Status:** Architecture Planning
|
|
6
|
+
**Note:** Subject to changes post-review
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Executive Summary
|
|
11
|
+
|
|
12
|
+
Enable Claude Code Agent Teams to work across different git branches using git worktrees. Each teammate operates in their own worktree (different branch) while maintaining Agent Teams coordination (messaging, task lists, shared discoveries).
|
|
13
|
+
|
|
14
|
+
**Key Innovation:** PreToolUse hooks with `updatedInput` to automatically rewrite file paths and prefix git commands, redirecting all teammate operations to their assigned worktree.
|
|
15
|
+
|
|
16
|
+
**Implementation:** ~150 lines of hook code + setup scripts, 3-5 days to production-ready.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Problem Statement
|
|
21
|
+
|
|
22
|
+
**Current Limitation:**
|
|
23
|
+
- Agent Teams teammates spawn in Lead's working directory
|
|
24
|
+
- Task tool has no `working_directory` parameter
|
|
25
|
+
- All teammates share same git branch
|
|
26
|
+
- Result: Can't do parallel development on different branches
|
|
27
|
+
|
|
28
|
+
**Current Workaround (Option 5):**
|
|
29
|
+
- All teammates work on same branch
|
|
30
|
+
- Lead creates feature branches AFTER completion
|
|
31
|
+
- Works but not true parallel branch development
|
|
32
|
+
|
|
33
|
+
**Desired State:**
|
|
34
|
+
- Lead: main branch in `/project`
|
|
35
|
+
- Teammate-1: feature-1 branch in `/project-feature-1` (worktree)
|
|
36
|
+
- Teammate-2: feature-2 branch in `/project-feature-2` (worktree)
|
|
37
|
+
- Full Agent Teams coordination maintained
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## System Architecture
|
|
42
|
+
|
|
43
|
+
### High-Level Design
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
┌─────────────────────────────────────────────┐
|
|
47
|
+
│ Lead Agent │
|
|
48
|
+
│ Directory: /project │
|
|
49
|
+
│ Branch: main │
|
|
50
|
+
│ ├─ Creates worktrees │
|
|
51
|
+
│ ├─ Spawns teammates (Task tool) │
|
|
52
|
+
│ └─ Coordinates via task list + messaging │
|
|
53
|
+
└─────────────────┬───────────────────────────┘
|
|
54
|
+
│
|
|
55
|
+
┌─────────┴─────────┐
|
|
56
|
+
│ │
|
|
57
|
+
┌───────▼──────┐ ┌───────▼──────┐
|
|
58
|
+
│ Teammate-1 │ │ Teammate-2 │
|
|
59
|
+
│ Spawned in: │ │ Spawned in: │
|
|
60
|
+
│ /project │ │ /project │
|
|
61
|
+
│ ↓ │ │ ↓ │
|
|
62
|
+
│ [PreToolUse │ │ [PreToolUse │
|
|
63
|
+
│ Hook] │ │ Hook] │
|
|
64
|
+
│ ↓ │ │ ↓ │
|
|
65
|
+
│ Redirected: │ │ Redirected: │
|
|
66
|
+
│ /project- │ │ /project- │
|
|
67
|
+
│ feature-1 │ │ feature-2 │
|
|
68
|
+
│ Branch: │ │ Branch: │
|
|
69
|
+
│ feature-1 │ │ feature-2 │
|
|
70
|
+
└──────────────┘ └──────────────┘
|
|
71
|
+
│ │
|
|
72
|
+
└─────────┬─────────┘
|
|
73
|
+
│
|
|
74
|
+
┌─────────▼─────────┐
|
|
75
|
+
│ Shared State │
|
|
76
|
+
├────────────────────┤
|
|
77
|
+
│ .claude/memory/ │
|
|
78
|
+
│ (discoveries, │
|
|
79
|
+
│ sessions) │
|
|
80
|
+
│ │
|
|
81
|
+
│ Task list │
|
|
82
|
+
│ Messaging │
|
|
83
|
+
└────────────────────┘
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Core Mechanism: Path Rewriting
|
|
87
|
+
|
|
88
|
+
**PreToolUse Hook intercepts all tool calls:**
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
Teammate-1 calls: Read("/project/src/auth.ts")
|
|
92
|
+
↓
|
|
93
|
+
PreToolUse hook:
|
|
94
|
+
- Detects teammate: teammate-1
|
|
95
|
+
- Loads config: worktree_path = /project-feature-1
|
|
96
|
+
- Rewrites path: /project/src/auth.ts → /project-feature-1/src/auth.ts
|
|
97
|
+
- Returns: updatedInput: { file_path: "/project-feature-1/src/auth.ts" }
|
|
98
|
+
↓
|
|
99
|
+
Read tool executes with: /project-feature-1/src/auth.ts ✓
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Same for Bash commands:**
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
Teammate-1 calls: Bash("git status")
|
|
106
|
+
↓
|
|
107
|
+
PreToolUse hook:
|
|
108
|
+
- Rewrites: "git status" → "cd /project-feature-1 && git status"
|
|
109
|
+
- Returns: updatedInput: { command: "cd /project-feature-1 && git status" }
|
|
110
|
+
↓
|
|
111
|
+
Bash executes in worktree, sees feature-1 branch ✓
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## Component Design
|
|
117
|
+
|
|
118
|
+
### 1. Worktree Setup Script
|
|
119
|
+
|
|
120
|
+
**File:** `scripts/setup-agent-worktree.sh`
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
#!/bin/bash
|
|
124
|
+
# Setup worktree for Agent Teams teammate
|
|
125
|
+
|
|
126
|
+
set -euo pipefail
|
|
127
|
+
|
|
128
|
+
PROJECT_ROOT="$1" # /project
|
|
129
|
+
BRANCH_NAME="$2" # feature-auth
|
|
130
|
+
TEAMMATE_NAME="$3" # teammate-1
|
|
131
|
+
|
|
132
|
+
WORKTREE_DIR="${PROJECT_ROOT}-${BRANCH_NAME}"
|
|
133
|
+
CONFIG_DIR="${PROJECT_ROOT}/.claude/worktree-config"
|
|
134
|
+
|
|
135
|
+
# Create worktree
|
|
136
|
+
cd "$PROJECT_ROOT"
|
|
137
|
+
git worktree add "$WORKTREE_DIR" -b "$BRANCH_NAME" 2>/dev/null || \
|
|
138
|
+
git worktree add "$WORKTREE_DIR" "$BRANCH_NAME"
|
|
139
|
+
|
|
140
|
+
# Create teammate config
|
|
141
|
+
mkdir -p "$CONFIG_DIR"
|
|
142
|
+
cat > "${CONFIG_DIR}/${TEAMMATE_NAME}.json" <<EOF
|
|
143
|
+
{
|
|
144
|
+
"teammate_name": "$TEAMMATE_NAME",
|
|
145
|
+
"worktree_path": "$WORKTREE_DIR",
|
|
146
|
+
"branch": "$BRANCH_NAME",
|
|
147
|
+
"project_root": "$PROJECT_ROOT",
|
|
148
|
+
"created_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
149
|
+
}
|
|
150
|
+
EOF
|
|
151
|
+
|
|
152
|
+
# Symlink .claude directory (shared memory)
|
|
153
|
+
ln -sf "$PROJECT_ROOT/.claude" "$WORKTREE_DIR/.claude"
|
|
154
|
+
|
|
155
|
+
echo "✓ Worktree ready: $WORKTREE_DIR (branch: $BRANCH_NAME)"
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
### 2. PreToolUse Hook (Core Redirection)
|
|
161
|
+
|
|
162
|
+
**File:** `hooks/worktree-redirect.sh`
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
#!/bin/bash
|
|
166
|
+
# PreToolUse hook: Redirect file operations to worktree
|
|
167
|
+
|
|
168
|
+
set -euo pipefail
|
|
169
|
+
|
|
170
|
+
INPUT_JSON=$(cat)
|
|
171
|
+
|
|
172
|
+
# Get worktree path from environment (set by SessionStart)
|
|
173
|
+
WORKTREE_PATH="${WORKTREE_PATH:-}"
|
|
174
|
+
PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-}"
|
|
175
|
+
|
|
176
|
+
if [ -z "$WORKTREE_PATH" ] || [ -z "$PROJECT_ROOT" ]; then
|
|
177
|
+
exit 0 # No redirection configured
|
|
178
|
+
fi
|
|
179
|
+
|
|
180
|
+
TOOL_NAME=$(echo "$INPUT_JSON" | jq -r '.tool_name // empty')
|
|
181
|
+
TOOL_INPUT=$(echo "$INPUT_JSON" | jq -c '.tool_input // {}')
|
|
182
|
+
|
|
183
|
+
# Path rewriting function
|
|
184
|
+
rewrite_path() {
|
|
185
|
+
local path="$1"
|
|
186
|
+
# Replace project root with worktree path
|
|
187
|
+
if [[ "$path" == "${PROJECT_ROOT}"* ]]; then
|
|
188
|
+
echo "${WORKTREE_PATH}${path#${PROJECT_ROOT}}"
|
|
189
|
+
else
|
|
190
|
+
echo "$path"
|
|
191
|
+
fi
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
case "$TOOL_NAME" in
|
|
195
|
+
Read|Write|Edit)
|
|
196
|
+
FILE_PATH=$(echo "$TOOL_INPUT" | jq -r '.file_path // empty')
|
|
197
|
+
if [ -n "$FILE_PATH" ]; then
|
|
198
|
+
NEW_PATH=$(rewrite_path "$FILE_PATH")
|
|
199
|
+
if [ "$NEW_PATH" != "$FILE_PATH" ]; then
|
|
200
|
+
jq -n --argjson input "$TOOL_INPUT" --arg newpath "$NEW_PATH" '{
|
|
201
|
+
hookSpecificOutput: {
|
|
202
|
+
hookEventName: "PreToolUse",
|
|
203
|
+
permissionDecision: "allow",
|
|
204
|
+
updatedInput: ($input | .file_path = $newpath)
|
|
205
|
+
}
|
|
206
|
+
}'
|
|
207
|
+
exit 0
|
|
208
|
+
fi
|
|
209
|
+
fi
|
|
210
|
+
;;
|
|
211
|
+
|
|
212
|
+
Glob|Grep)
|
|
213
|
+
SEARCH_PATH=$(echo "$TOOL_INPUT" | jq -r '.path // empty')
|
|
214
|
+
NEW_PATH=$(rewrite_path "${SEARCH_PATH:-$PROJECT_ROOT}")
|
|
215
|
+
jq -n --argjson input "$TOOL_INPUT" --arg newpath "$NEW_PATH" '{
|
|
216
|
+
hookSpecificOutput: {
|
|
217
|
+
hookEventName: "PreToolUse",
|
|
218
|
+
permissionDecision: "allow",
|
|
219
|
+
updatedInput: ($input | .path = $newpath)
|
|
220
|
+
}
|
|
221
|
+
}'
|
|
222
|
+
exit 0
|
|
223
|
+
;;
|
|
224
|
+
|
|
225
|
+
Bash)
|
|
226
|
+
COMMAND=$(echo "$TOOL_INPUT" | jq -r '.command // empty')
|
|
227
|
+
if [ -n "$COMMAND" ] && [[ "$COMMAND" != "cd $WORKTREE_PATH"* ]]; then
|
|
228
|
+
jq -n --argjson input "$TOOL_INPUT" --arg wt "$WORKTREE_PATH" --arg cmd "$COMMAND" '{
|
|
229
|
+
hookSpecificOutput: {
|
|
230
|
+
hookEventName: "PreToolUse",
|
|
231
|
+
permissionDecision: "allow",
|
|
232
|
+
updatedInput: ($input | .command = ("cd " + $wt + " && " + $cmd))
|
|
233
|
+
}
|
|
234
|
+
}'
|
|
235
|
+
exit 0
|
|
236
|
+
fi
|
|
237
|
+
;;
|
|
238
|
+
esac
|
|
239
|
+
|
|
240
|
+
exit 0
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
### 3. SessionStart Hook (Environment Setup)
|
|
246
|
+
|
|
247
|
+
**File:** `hooks/worktree-session-start.sh`
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
#!/bin/bash
|
|
251
|
+
# SessionStart: Set worktree path for teammates
|
|
252
|
+
|
|
253
|
+
set -euo pipefail
|
|
254
|
+
|
|
255
|
+
TEAMMATE_NAME="${TEAMMATE_NAME:-}"
|
|
256
|
+
|
|
257
|
+
if [ -n "$TEAMMATE_NAME" ]; then
|
|
258
|
+
CONFIG_FILE=".claude/worktree-config/${TEAMMATE_NAME}.json"
|
|
259
|
+
if [ -f "$CONFIG_FILE" ]; then
|
|
260
|
+
WORKTREE_PATH=$(jq -r '.worktree_path' "$CONFIG_FILE")
|
|
261
|
+
|
|
262
|
+
if [ -n "${CLAUDE_ENV_FILE:-}" ]; then
|
|
263
|
+
echo "export WORKTREE_PATH=\"${WORKTREE_PATH}\"" >> "$CLAUDE_ENV_FILE"
|
|
264
|
+
echo "export TEAMMATE_NAME=\"${TEAMMATE_NAME}\"" >> "$CLAUDE_ENV_FILE"
|
|
265
|
+
fi
|
|
266
|
+
fi
|
|
267
|
+
fi
|
|
268
|
+
|
|
269
|
+
exit 0
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
### 4. Worktree Cleanup Script
|
|
275
|
+
|
|
276
|
+
**File:** `scripts/cleanup-agent-worktree.sh`
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
#!/bin/bash
|
|
280
|
+
# Cleanup worktree after team work completes
|
|
281
|
+
|
|
282
|
+
set -euo pipefail
|
|
283
|
+
|
|
284
|
+
TEAMMATE_NAME="$1"
|
|
285
|
+
CONFIG_DIR=".claude/worktree-config"
|
|
286
|
+
CONFIG_FILE="${CONFIG_DIR}/${TEAMMATE_NAME}.json"
|
|
287
|
+
|
|
288
|
+
if [ ! -f "$CONFIG_FILE" ]; then
|
|
289
|
+
echo "No config for $TEAMMATE_NAME"
|
|
290
|
+
exit 1
|
|
291
|
+
fi
|
|
292
|
+
|
|
293
|
+
WORKTREE_PATH=$(jq -r '.worktree_path' "$CONFIG_FILE")
|
|
294
|
+
|
|
295
|
+
# Remove worktree
|
|
296
|
+
git worktree remove "$WORKTREE_PATH" --force
|
|
297
|
+
|
|
298
|
+
# Remove config
|
|
299
|
+
rm "$CONFIG_FILE"
|
|
300
|
+
|
|
301
|
+
echo "✓ Cleaned up worktree: $WORKTREE_PATH"
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
---
|
|
305
|
+
|
|
306
|
+
## Implementation Plan
|
|
307
|
+
|
|
308
|
+
### Phase 0: Validation (2 hours)
|
|
309
|
+
|
|
310
|
+
**Goal:** Prove PreToolUse `updatedInput` works for all tool types
|
|
311
|
+
|
|
312
|
+
**Tasks:**
|
|
313
|
+
1. Write minimal PreToolUse hook
|
|
314
|
+
2. Hardcode `WORKTREE_PATH=/tmp/test-worktree`
|
|
315
|
+
3. Test Read tool → verify path rewritten
|
|
316
|
+
4. Test Write tool → verify path rewritten
|
|
317
|
+
5. Test Bash tool → **CRITICAL:** verify command prefixed with cd
|
|
318
|
+
6. Test Glob/Grep → verify path rewritten
|
|
319
|
+
|
|
320
|
+
**Success Criteria:**
|
|
321
|
+
- [ ] All 5 tool types successfully redirected
|
|
322
|
+
- [ ] Bash `updatedInput` for `command` field works
|
|
323
|
+
- [ ] No data corruption or errors
|
|
324
|
+
|
|
325
|
+
**If validation fails:** Fallback to Option E+ (instruction-based) only
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
### Phase 1: MVP (1-2 days)
|
|
330
|
+
|
|
331
|
+
**Goal:** Working worktree mode for 1 teammate
|
|
332
|
+
|
|
333
|
+
**Tasks:**
|
|
334
|
+
1. Build `setup-agent-worktree.sh` script
|
|
335
|
+
2. Build `worktree-redirect.sh` PreToolUse hook
|
|
336
|
+
3. Build `worktree-session-start.sh` SessionStart hook
|
|
337
|
+
4. Test with single teammate on feature branch
|
|
338
|
+
5. Verify git operations work correctly
|
|
339
|
+
6. Verify file operations write to correct worktree
|
|
340
|
+
|
|
341
|
+
**Success Criteria:**
|
|
342
|
+
- [ ] Lead creates worktree successfully
|
|
343
|
+
- [ ] Teammate operates in correct worktree
|
|
344
|
+
- [ ] Git commands show correct branch
|
|
345
|
+
- [ ] File edits go to correct worktree
|
|
346
|
+
- [ ] No data loss or conflicts
|
|
347
|
+
|
|
348
|
+
**Deliverables:**
|
|
349
|
+
- 3 scripts (~200 lines total)
|
|
350
|
+
- Test documentation
|
|
351
|
+
- Known issues list
|
|
352
|
+
|
|
353
|
+
---
|
|
354
|
+
|
|
355
|
+
### Phase 2: Multi-Teammate Support (1-2 days)
|
|
356
|
+
|
|
357
|
+
**Goal:** 3+ teammates in different worktrees simultaneously
|
|
358
|
+
|
|
359
|
+
**Tasks:**
|
|
360
|
+
1. Build `cleanup-agent-worktree.sh` script
|
|
361
|
+
2. Add teammate name discovery mechanism
|
|
362
|
+
3. Test 3 teammates in parallel (different branches)
|
|
363
|
+
4. Verify shared memory works (symlinked .claude/)
|
|
364
|
+
5. Verify no file conflicts across worktrees
|
|
365
|
+
6. Add health checks (TeammateIdle hook)
|
|
366
|
+
|
|
367
|
+
**Success Criteria:**
|
|
368
|
+
- [ ] 3 teammates operate independently
|
|
369
|
+
- [ ] Each on correct branch
|
|
370
|
+
- [ ] Shared discoveries work
|
|
371
|
+
- [ ] Task list coordination works
|
|
372
|
+
- [ ] Messaging works across worktrees
|
|
373
|
+
|
|
374
|
+
**Deliverables:**
|
|
375
|
+
- Cleanup scripts
|
|
376
|
+
- Multi-teammate tests
|
|
377
|
+
- Health check monitoring
|
|
378
|
+
|
|
379
|
+
---
|
|
380
|
+
|
|
381
|
+
### Phase 3: Production Hardening (2-3 days)
|
|
382
|
+
|
|
383
|
+
**Goal:** Robust, error-handled, documented feature
|
|
384
|
+
|
|
385
|
+
**Tasks:**
|
|
386
|
+
1. Add edge case handling (absolute paths, paths outside project)
|
|
387
|
+
2. Add failure recovery (worktree deleted mid-work)
|
|
388
|
+
3. Add file conflict detection (optional)
|
|
389
|
+
4. Write comprehensive documentation
|
|
390
|
+
5. Add integration tests
|
|
391
|
+
6. Performance validation (<10ms overhead)
|
|
392
|
+
|
|
393
|
+
**Success Criteria:**
|
|
394
|
+
- [ ] All edge cases handled
|
|
395
|
+
- [ ] Graceful degradation on errors
|
|
396
|
+
- [ ] Documentation complete
|
|
397
|
+
- [ ] Tests passing
|
|
398
|
+
- [ ] Performance targets met
|
|
399
|
+
|
|
400
|
+
**Deliverables:**
|
|
401
|
+
- Production-ready scripts
|
|
402
|
+
- User documentation
|
|
403
|
+
- Architecture doc (this document)
|
|
404
|
+
- Test suite
|
|
405
|
+
|
|
406
|
+
---
|
|
407
|
+
|
|
408
|
+
## Technical Specification
|
|
409
|
+
|
|
410
|
+
### Hook Integration
|
|
411
|
+
|
|
412
|
+
**Register in `.claude/settings.local.json` (or via install script):**
|
|
413
|
+
|
|
414
|
+
```json
|
|
415
|
+
{
|
|
416
|
+
"hooks": {
|
|
417
|
+
"PreToolUse": [
|
|
418
|
+
{
|
|
419
|
+
"matcher": "Read|Write|Edit|Glob|Grep|Bash",
|
|
420
|
+
"hooks": [
|
|
421
|
+
{
|
|
422
|
+
"type": "command",
|
|
423
|
+
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/worktree-redirect.sh"
|
|
424
|
+
}
|
|
425
|
+
]
|
|
426
|
+
}
|
|
427
|
+
],
|
|
428
|
+
"SessionStart": [
|
|
429
|
+
{
|
|
430
|
+
"matcher": "*",
|
|
431
|
+
"hooks": [
|
|
432
|
+
{
|
|
433
|
+
"type": "command",
|
|
434
|
+
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/worktree-session-start.sh"
|
|
435
|
+
}
|
|
436
|
+
]
|
|
437
|
+
}
|
|
438
|
+
]
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
---
|
|
444
|
+
|
|
445
|
+
### Worktree Structure
|
|
446
|
+
|
|
447
|
+
```
|
|
448
|
+
Project Layout:
|
|
449
|
+
├── /project/ (main branch - Lead works here)
|
|
450
|
+
│ ├── .claude/ (shared memory, tools, hooks)
|
|
451
|
+
│ │ ├── memory/ (Capsule storage - shared!)
|
|
452
|
+
│ │ ├── worktree-config/ (teammate → worktree mappings)
|
|
453
|
+
│ │ │ ├── teammate-1.json
|
|
454
|
+
│ │ │ └── teammate-2.json
|
|
455
|
+
│ │ └── hooks/
|
|
456
|
+
│ │ └── worktree-redirect.sh
|
|
457
|
+
│ └── src/
|
|
458
|
+
│
|
|
459
|
+
├── /project-feature-1/ (feature-1 branch - Teammate-1)
|
|
460
|
+
│ ├── .claude/ → /project/.claude/ (symlink!)
|
|
461
|
+
│ └── src/ (isolated files)
|
|
462
|
+
│
|
|
463
|
+
└── /project-feature-2/ (feature-2 branch - Teammate-2)
|
|
464
|
+
├── .claude/ → /project/.claude/ (symlink!)
|
|
465
|
+
└── src/ (isolated files)
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
**Key insight:** `.claude/` is symlinked, so all teammates share:
|
|
469
|
+
- Memory/discoveries (Capsule storage)
|
|
470
|
+
- Tools and hooks
|
|
471
|
+
- Task list (Agent Teams native)
|
|
472
|
+
- Messaging (Agent Teams native)
|
|
473
|
+
|
|
474
|
+
But each teammate edits files in their own worktree!
|
|
475
|
+
|
|
476
|
+
---
|
|
477
|
+
|
|
478
|
+
### Teammate Config Schema
|
|
479
|
+
|
|
480
|
+
**File:** `.claude/worktree-config/{teammate-name}.json`
|
|
481
|
+
|
|
482
|
+
```json
|
|
483
|
+
{
|
|
484
|
+
"teammate_name": "teammate-1",
|
|
485
|
+
"worktree_path": "/Users/arpit/Desktop/project-feature-1",
|
|
486
|
+
"branch": "feature-1",
|
|
487
|
+
"project_root": "/Users/arpit/Desktop/project",
|
|
488
|
+
"created_at": "2026-02-11T10:00:00Z",
|
|
489
|
+
"owned_paths": [
|
|
490
|
+
"src/auth/",
|
|
491
|
+
"src/middleware/"
|
|
492
|
+
]
|
|
493
|
+
}
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
---
|
|
497
|
+
|
|
498
|
+
## Workflow
|
|
499
|
+
|
|
500
|
+
### Setup (Lead Agent)
|
|
501
|
+
|
|
502
|
+
```bash
|
|
503
|
+
# 1. Create worktrees for each feature
|
|
504
|
+
bash scripts/setup-agent-worktree.sh /project feature-auth teammate-1
|
|
505
|
+
bash scripts/setup-agent-worktree.sh /project feature-api teammate-2
|
|
506
|
+
|
|
507
|
+
# 2. Create tasks
|
|
508
|
+
TaskCreate("Implement auth module")
|
|
509
|
+
TaskCreate("Build API endpoints")
|
|
510
|
+
|
|
511
|
+
# 3. Spawn teammates with names
|
|
512
|
+
Task(
|
|
513
|
+
name="teammate-1",
|
|
514
|
+
team_name="dev-team",
|
|
515
|
+
prompt="You are teammate-1. Work on auth module. Check task list for assignments."
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
Task(
|
|
519
|
+
name="teammate-2",
|
|
520
|
+
team_name="dev-team",
|
|
521
|
+
prompt="You are teammate-2. Work on API endpoints. Check task list for assignments."
|
|
522
|
+
)
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
### Execution (Teammates)
|
|
526
|
+
|
|
527
|
+
```
|
|
528
|
+
Teammate-1 (auto-redirected to /project-feature-1):
|
|
529
|
+
1. Checks task list → Claims "Implement auth module"
|
|
530
|
+
2. Reads files → PreToolUse rewrites paths to /project-feature-1/
|
|
531
|
+
3. Edits code → Changes go to feature-1 branch
|
|
532
|
+
4. Commits → git commit runs in worktree (feature-1 branch)
|
|
533
|
+
5. Completes task → Messages lead with results
|
|
534
|
+
|
|
535
|
+
Teammate-2 (auto-redirected to /project-feature-2):
|
|
536
|
+
1. Claims "Build API endpoints"
|
|
537
|
+
2. Works in /project-feature-2/ (feature-2 branch)
|
|
538
|
+
3. No file conflicts with Teammate-1 (different worktrees!)
|
|
539
|
+
4. Completes and messages lead
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
### Merge (Lead Agent)
|
|
543
|
+
|
|
544
|
+
```bash
|
|
545
|
+
# After teammates complete:
|
|
546
|
+
|
|
547
|
+
# 1. Review Teammate-1's work
|
|
548
|
+
cd /project
|
|
549
|
+
git diff main..feature-1
|
|
550
|
+
|
|
551
|
+
# 2. Merge if approved
|
|
552
|
+
git merge feature-1
|
|
553
|
+
|
|
554
|
+
# 3. Review Teammate-2's work
|
|
555
|
+
git diff main..feature-2
|
|
556
|
+
|
|
557
|
+
# 4. Merge if approved
|
|
558
|
+
git merge feature-2
|
|
559
|
+
|
|
560
|
+
# 5. Cleanup worktrees
|
|
561
|
+
bash scripts/cleanup-agent-worktree.sh teammate-1
|
|
562
|
+
bash scripts/cleanup-agent-worktree.sh teammate-2
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
---
|
|
566
|
+
|
|
567
|
+
## Known Risks & Mitigations
|
|
568
|
+
|
|
569
|
+
| Risk | Severity | Mitigation |
|
|
570
|
+
|------|----------|------------|
|
|
571
|
+
| **PreToolUse `updatedInput` doesn't work for Bash** | HIGH | Phase 0 validation REQUIRED before building |
|
|
572
|
+
| **Teammate name not discoverable** | HIGH | Use SubagentStart hook to inject teammate name |
|
|
573
|
+
| **Worktree deleted mid-work** | MEDIUM | Health check in TeammateIdle hook |
|
|
574
|
+
| **Path rewriting edge cases** | MEDIUM | Only rewrite paths under project root |
|
|
575
|
+
| **Agent Teams API changes** | MEDIUM | Feature is experimental, may change |
|
|
576
|
+
| **Memory race conditions** | LOW | Symlinked .claude/, minor contention acceptable |
|
|
577
|
+
|
|
578
|
+
**CRITICAL:** Phase 0 validation must confirm `updatedInput` works for Bash `command` field. Documentation doesn't explicitly state this!
|
|
579
|
+
|
|
580
|
+
---
|
|
581
|
+
|
|
582
|
+
## Performance Impact
|
|
583
|
+
|
|
584
|
+
| Operation | Overhead | Impact |
|
|
585
|
+
|-----------|----------|--------|
|
|
586
|
+
| PreToolUse hook execution | ~5-10ms | Negligible vs 2-30s LLM inference |
|
|
587
|
+
| Worktree setup | ~2-5s | One-time per teammate |
|
|
588
|
+
| Git operations | 0ms | Native worktree performance |
|
|
589
|
+
| Memory access | 0ms | Symlink, no overhead |
|
|
590
|
+
|
|
591
|
+
**Total impact:** <1% slowdown, imperceptible to users
|
|
592
|
+
|
|
593
|
+
---
|
|
594
|
+
|
|
595
|
+
## Success Metrics
|
|
596
|
+
|
|
597
|
+
### Technical Validation
|
|
598
|
+
- [ ] All tool types (Read, Write, Edit, Glob, Grep, Bash) redirect correctly
|
|
599
|
+
- [ ] Git operations show correct branch
|
|
600
|
+
- [ ] No file corruption across worktrees
|
|
601
|
+
- [ ] Performance overhead <10ms per tool call
|
|
602
|
+
|
|
603
|
+
### Functional Validation
|
|
604
|
+
- [ ] 3 teammates work on different branches simultaneously
|
|
605
|
+
- [ ] Shared discoveries work (symlinked memory)
|
|
606
|
+
- [ ] Task list coordination works
|
|
607
|
+
- [ ] Messaging works across worktrees
|
|
608
|
+
- [ ] Lead can review and merge branches
|
|
609
|
+
|
|
610
|
+
### User Experience
|
|
611
|
+
- [ ] Zero manual path adjustments by teammates
|
|
612
|
+
- [ ] Clear worktree context in prompts
|
|
613
|
+
- [ ] Easy setup (single script)
|
|
614
|
+
- [ ] Easy cleanup (single script)
|
|
615
|
+
|
|
616
|
+
---
|
|
617
|
+
|
|
618
|
+
## Alternative Approaches
|
|
619
|
+
|
|
620
|
+
### Option E+ (Instruction-Based) - No Implementation Needed
|
|
621
|
+
|
|
622
|
+
**Lead instructs teammates:**
|
|
623
|
+
> "You are working in /project-feature-1/ worktree on feature-1 branch. Use absolute paths for all file operations: Read('/project-feature-1/src/...'), Bash('cd /project-feature-1 && git ...')"
|
|
624
|
+
|
|
625
|
+
**Success rate:** ~90% (LLM compliance is good but not perfect)
|
|
626
|
+
**Effort:** 0 days (just instructions)
|
|
627
|
+
**Recommended:** Use this TODAY while building hooks-based approach
|
|
628
|
+
|
|
629
|
+
### Option F (Hooks-Based) - This Document
|
|
630
|
+
|
|
631
|
+
**Automatic redirection via PreToolUse hooks**
|
|
632
|
+
|
|
633
|
+
**Success rate:** ~99% (hooks intercept all operations)
|
|
634
|
+
**Effort:** 3-5 days
|
|
635
|
+
**Recommended:** Build after Phase 0 validation succeeds
|
|
636
|
+
|
|
637
|
+
---
|
|
638
|
+
|
|
639
|
+
## Open Questions (Post-Review)
|
|
640
|
+
|
|
641
|
+
1. **Teammate name discovery:** Best mechanism? (env, config, marker file)
|
|
642
|
+
2. **File ownership:** Enforce or just warn?
|
|
643
|
+
3. **Capsule integration:** How does capsule namespace isolation work with worktrees?
|
|
644
|
+
4. **Error handling:** Fallback strategy if worktree unavailable?
|
|
645
|
+
5. **Multi-project:** Does this work across different projects?
|
|
646
|
+
|
|
647
|
+
---
|
|
648
|
+
|
|
649
|
+
## Next Steps
|
|
650
|
+
|
|
651
|
+
**Immediate (This Week):**
|
|
652
|
+
1. **Phase 0 validation** (2 hours) - Test PreToolUse `updatedInput`
|
|
653
|
+
2. **Go/no-go decision** based on validation results
|
|
654
|
+
|
|
655
|
+
**If validation succeeds:**
|
|
656
|
+
1. **Phase 1 MVP** (1-2 days) - Single teammate worktree support
|
|
657
|
+
2. **Phase 2 multi-teammate** (1-2 days) - 3+ teammates
|
|
658
|
+
3. **Phase 3 production** (2-3 days) - Hardening, docs, tests
|
|
659
|
+
|
|
660
|
+
**Timeline:** ~1 week from validation to production-ready
|
|
661
|
+
|
|
662
|
+
---
|
|
663
|
+
|
|
664
|
+
## Notes
|
|
665
|
+
|
|
666
|
+
- This design assumes Agent Teams API remains stable (experimental risk)
|
|
667
|
+
- PreToolUse `updatedInput` for Bash commands is unvalidated (MUST TEST)
|
|
668
|
+
- Symlinked .claude/ approach is novel (not documented elsewhere)
|
|
669
|
+
- This enables true parallel branch development with Agent Teams coordination
|
|
670
|
+
- Falls back gracefully to Option E+ if hooks don't work
|
|
671
|
+
|
|
672
|
+
---
|
|
673
|
+
|
|
674
|
+
**Status:** Architecture defined, ready for validation. Post-review changes expected.
|
|
675
|
+
|
|
676
|
+
**Owner:** Arpit
|
|
677
|
+
**Reviewers:** Fresh Claude instance (for unbiased evaluation)
|
|
678
|
+
|
|
679
|
+
---
|
|
680
|
+
|
|
681
|
+
**End of Architecture Document**
|