Git is not thread-safe, which creates critical race conditions when multiple AI agents try to perform Git operations concurrently:
.git folder simultaneouslyInstead of using mutexes (which would serialize all Git operations and hurt performance), we give each agent its own Git repository clone:
workspace/
├── agent-backend-engineer/ # Backend engineer's clone
│ ├── .git/
│ ├── tasks/
│ └── ...
├── agent-frontend-engineer/ # Frontend engineer's clone
│ ├── .git/
│ ├── tasks/
│ └── ...
└── agent-qa-engineer/ # QA engineer's clone
├── .git/
├── tasks/
└── ...
.git directory and working treestaff cleanup-clones removes all agent workspacesgit/clone_manager.go)type CloneManager struct { baseRepoURL string // Source repository URL workspacePath string // Base workspace directory agentClones map[string]string // agent name -> clone path mu sync.RWMutex // Thread-safe map access }
Key Methods:
GetAgentClonePath(agentName) - Get/create agent's clone directoryRefreshAgentClone(agentName) - Pull latest changes for an agentCleanupAgentClone(agentName) - Remove specific agent's cloneCleanupAllClones() - Remove all agent clonesEach agent's Git operations are automatically routed to its dedicated clone:
// Get agent's dedicated Git clone clonePath, err := am.cloneManager.GetAgentClonePath(agent.Name) if err != nil { return fmt.Errorf("failed to get agent clone: %w", err) } // All Git operations use the agent's clone directory gitCmd := func(args ...string) *exec.Cmd { return exec.CommandContext(ctx, "git", append([]string{"-C", clonePath}, args...)...) }
Agent Starts Task:
Agent backend-engineer gets task: "Add user authentication" Creating clone: workspace/agent-backend-engineer/
Concurrent Operations:
# These happen simultaneously without conflicts: Agent backend-engineer: git clone -> workspace/agent-backend-engineer/ Agent frontend-engineer: git clone -> workspace/agent-frontend-engineer/ Agent qa-engineer: git clone -> workspace/agent-qa-engineer/
Branch Creation:
# Each agent creates branches in their own clone: backend-engineer: git checkout -b task-123-auth-backend frontend-engineer: git checkout -b task-124-auth-ui qa-engineer: git checkout -b task-125-auth-tests
Concurrent Pushes:
# All agents push to origin simultaneously: git push -u origin task-123-auth-backend # ✅ Success git push -u origin task-124-auth-ui # ✅ Success git push -u origin task-125-auth-tests # ✅ Success
staff list-agents # Shows which agents are running and their clone status
staff cleanup-clones # Removes all agent workspace directories
du -sh workspace/ # Check total workspace disk usage
staff cleanup-clones to free space when neededgit pull operations are incremental| Solution | Concurrency | Complexity | Performance | Risk |
|---|---|---|---|---|
| Per-Agent Clones | ✅ Full | 🟡 Medium | ✅ High | 🟢 Low |
| Global Git Mutex | ❌ None | 🟢 Low | ❌ Poor | 🟡 Medium |
| File Locking | 🟡 Limited | 🔴 High | 🟡 Medium | 🔴 High |
| Separate Repositories | ✅ Full | 🔴 Very High | ✅ High | 🔴 High |
git clone --depth=1 for space efficiencyThe per-agent clone solution provides the optimal balance of performance, safety, and maintainability for concurrent AI agent operations.