Scale AI work across codebases with worktrees and parallel agents
Pick any project. Compare it against a reference implementation using your team's .claude/rules/. Claude produces a gap analysis. Then spin up a team of agents — each in its own git worktree — to fix the gaps in parallel. Each agent creates an independent PR with gh. No merge conflicts.
One agent, one context window — it fills up on large codebases. Agent teams: each agent gets its own context window, its own worktree, its own branch. Worktree isolation = git-level parallelism. Each agent has a full copy of the repo on a separate branch. Gap analysis is a natural fit: analysis is read-only (safe), fixes are independent (parallelizable).
claude --teammate-mode=in-process starts agents as in-process subagents (no tmux needed). Each agent gets isolation: worktree — a temporary git worktree at .claude/worktrees/
Ensure you have a target project and a reference implementation available
cd <your-target-project>
ls ../<reference-project>/ # Reference implementation
ls .claude/rules/ # Team standards
Target project, reference project, and .claude/rules/ files all accessible
Phase 1 (15 min): Ask Claude to compare your target project against the reference using .claude/rules/. Claude produces a full report document with: executive summary, standards framework, gap areas (K8s, observability, resilience, quality, testing, CI/CD, security), compliance checklist, and remediation roadmap. Review the report — this is your working document, not a commit artifact. The report structure follows the pattern from gap-analysis.md: each finding cites STANDARD, CURRENT, REFERENCE, IMPACT, SEVERITY. Phase 2 (20 min): Start claude --teammate-mode=in-process. Paste or reference the gap analysis report as input. Tell Claude: "Read this gap analysis. Create a team of agents to fix the MUST items. Each agent works in its own worktree on non-overlapping files." Claude reads the report, creates agents, each agent fixes its assigned gaps and creates a PR with gh pr create. Verify PRs are independent and conflict-free.
Gap Analysis Report Format
===========================
For each finding, cite three layers:
STANDARD: <Industry standard> + <Team rule from .claude/rules/>
CURRENT: What the target project does today
REFERENCE: What the reference implementation does
IMPACT: What breaks or degrades because of this gap
SEVERITY: MUST / SHOULD / NICE
Example:
STANDARD: CNCF: Startup probes prevent liveness killing slow-starting JVM apps
.claude/rules/spring-boot.md: Configure health probes
CURRENT: Liveness + readiness only. No startup probe.
REFERENCE: startupProbe with 100s budget (10 checks x 10s)
IMPACT: JVM + DB migration takes 60-90s. Liveness fires at 30s = restart loops
SEVERITY: MUST
Agent Team Workflow
===================
1. START
claude --teammate-mode=in-process
2. ANALYZE (you + Claude, single context)
"ultrathink Compare this project against <reference> using .claude/rules/"
-> Gap report with MUST/SHOULD/NICE ratings
3. SPLIT WORK (assign non-overlapping file sets)
Agent 1: chart/ files -> branch: fix/infra
Agent 2: src/**/*.java -> branch: fix/code
Agent 3: build config files -> branch: fix/quality
4. PARALLEL FIX (each agent in its own worktree)
+-- agent-infra ---- chart/ ----------- fix/infra ---+
|-- agent-code ----- src/**/*.java ---- fix/code ----|
+-- agent-quality -- build.gradle ----- fix/quality -+
5. CREATE PRs (from each worktree branch)
gh pr create --title "Fix: ..." --body "..."
6. VERIFY
gh pr list -> 3 PRs, independent files, no conflicts
gh pr view <n> --json files -> confirm no overlap
Git Worktrees for Agent Teams
==============================
What is a worktree?
A second (or third, fourth...) working directory for the same repo.
Each worktree has its own branch, its own files, but shares .git history.
repo/ <- main branch (your working copy)
.claude/worktrees/agent-1/ <- fix/infra branch (agent 1's copy)
.claude/worktrees/agent-2/ <- fix/code branch (agent 2's copy)
Why no conflicts?
Each agent edits different files on different branches.
PRs are independent — reviewable and mergeable separately.
How Claude Code uses them:
isolation: worktree <- in agent definitions
claude -w <- start Claude in a worktree
--worktree <- same as -w
Creating PRs from worktrees:
cd .claude/worktrees/agent-1/
git add -A && git commit -m "Fix: ..."
gh pr create --title "..." --body "..."
Key rule: assign non-overlapping file sets to each agent.
If two agents edit the same file -> merge conflict.
Split by directory or by concern.
Subagents vs Agent Teams
========================
Feature Subagents Agent Teams
----------- ---------- -----------
Spawning Within a session Separate processes
Context Shares parent's window Own context window each
Worktree Same working directory Own worktree + branch
Lifetime Short-lived (single task) Long-lived (parallel work)
Use case Quick delegated subtask Multi-area codebase changes
Isolation None (same branch) Full git-level isolation
PR creation From parent's branch Each agent creates own PR
When to use subagents:
- Small, focused tasks within a larger session
- Tasks that need the parent's context (files already read, decisions made)
- Quick lookups or code generation that feed back into the main task
When to use agent teams:
- Multiple independent changes across a codebase
- Work that benefits from git-level isolation (no merge conflicts)
- Tasks large enough to fill their own context window
- Gap analysis remediation, multi-module refactoring, parallel feature work
Agent Team Cleanup
==================
After agent team sessions, clean up stale worktrees and branches.
1. List worktrees:
git worktree list
# Shows all worktrees including .claude/worktrees/agent-*
2. Remove finished worktrees:
git worktree remove .claude/worktrees/agent-infra
git worktree remove .claude/worktrees/agent-code
3. Prune stale worktree references:
git worktree prune
4. Clean up merged branches:
git branch --merged main | grep 'fix/' | xargs git branch -d
5. Verify PRs are merged before cleanup:
gh pr list --state merged --author @me
Best practices:
- Clean up worktrees after PRs are merged
- Don't delete worktrees with unmerged changes
- Use git worktree prune to remove stale refs
- Tasks expire after 3 days if not completed