| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame^] | 1 | # Git Concurrency Solution: Per-Agent Repository Clones |
| 2 | |
| 3 | ## Problem Statement |
| 4 | |
| 5 | Git is not thread-safe, which creates critical race conditions when multiple AI agents try to perform Git operations concurrently: |
| 6 | |
| 7 | - **Repository Corruption**: Multiple agents modifying the same `.git` folder simultaneously |
| 8 | - **Branch Conflicts**: Agents creating branches with the same names or overwriting each other's work |
| 9 | - **Push Failures**: Concurrent pushes causing merge conflicts and failed operations |
| 10 | - **Index Lock Errors**: Git index.lock conflicts when multiple processes access the repository |
| 11 | |
| 12 | ## Solution: Per-Agent Git Clones |
| 13 | |
| 14 | Instead of using mutexes (which would serialize all Git operations and hurt performance), we give each agent its own Git repository clone: |
| 15 | |
| 16 | ``` |
| 17 | workspace/ |
| 18 | ├── agent-backend-engineer/ # Backend engineer's clone |
| 19 | │ ├── .git/ |
| 20 | │ ├── tasks/ |
| 21 | │ └── ... |
| 22 | ├── agent-frontend-engineer/ # Frontend engineer's clone |
| 23 | │ ├── .git/ |
| 24 | │ ├── tasks/ |
| 25 | │ └── ... |
| 26 | └── agent-qa-engineer/ # QA engineer's clone |
| 27 | ├── .git/ |
| 28 | ├── tasks/ |
| 29 | └── ... |
| 30 | ``` |
| 31 | |
| 32 | ## Key Benefits |
| 33 | |
| 34 | ### 🚀 **True Concurrency** |
| 35 | - Multiple agents can work simultaneously without blocking each other |
| 36 | - No waiting for Git lock releases |
| 37 | - Scales to hundreds of concurrent agents |
| 38 | |
| 39 | ### 🛡️ **Complete Isolation** |
| 40 | - Each agent has its own `.git` directory and working tree |
| 41 | - No shared state or race conditions |
| 42 | - Agent failures don't affect other agents |
| 43 | |
| 44 | ### 🔄 **Automatic Synchronization** |
| 45 | - Each clone automatically pulls latest changes before creating branches |
| 46 | - All branches push to the same remote repository |
| 47 | - PRs are created against the main repository |
| 48 | |
| 49 | ### 🧹 **Easy Cleanup** |
| 50 | - `staff cleanup-clones` removes all agent workspaces |
| 51 | - Clones are recreated on-demand when agents start working |
| 52 | - No manual Git state management required |
| 53 | |
| 54 | ## Implementation Details |
| 55 | |
| 56 | ### CloneManager (`git/clone_manager.go`) |
| 57 | |
| 58 | ```go |
| 59 | type CloneManager struct { |
| 60 | baseRepoURL string // Source repository URL |
| 61 | workspacePath string // Base workspace directory |
| 62 | agentClones map[string]string // agent name -> clone path |
| 63 | mu sync.RWMutex // Thread-safe map access |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | **Key Methods:** |
| 68 | - `GetAgentClonePath(agentName)` - Get/create agent's clone directory |
| 69 | - `RefreshAgentClone(agentName)` - Pull latest changes for an agent |
| 70 | - `CleanupAgentClone(agentName)` - Remove specific agent's clone |
| 71 | - `CleanupAllClones()` - Remove all agent clones |
| 72 | |
| 73 | ### Agent Integration |
| 74 | |
| 75 | Each agent's Git operations are automatically routed to its dedicated clone: |
| 76 | |
| 77 | ```go |
| 78 | // Get agent's dedicated Git clone |
| 79 | clonePath, err := am.cloneManager.GetAgentClonePath(agent.Name) |
| 80 | if err != nil { |
| 81 | return fmt.Errorf("failed to get agent clone: %w", err) |
| 82 | } |
| 83 | |
| 84 | // All Git operations use the agent's clone directory |
| 85 | gitCmd := func(args ...string) *exec.Cmd { |
| 86 | return exec.CommandContext(ctx, "git", append([]string{"-C", clonePath}, args...)...) |
| 87 | } |
| 88 | ``` |
| 89 | |
| 90 | ## Workflow Example |
| 91 | |
| 92 | 1. **Agent Starts Task**: |
| 93 | ```bash |
| 94 | Agent backend-engineer gets task: "Add user authentication" |
| 95 | Creating clone: workspace/agent-backend-engineer/ |
| 96 | ``` |
| 97 | |
| 98 | 2. **Concurrent Operations**: |
| 99 | ```bash |
| 100 | # These happen simultaneously without conflicts: |
| 101 | Agent backend-engineer: git clone -> workspace/agent-backend-engineer/ |
| 102 | Agent frontend-engineer: git clone -> workspace/agent-frontend-engineer/ |
| 103 | Agent qa-engineer: git clone -> workspace/agent-qa-engineer/ |
| 104 | ``` |
| 105 | |
| 106 | 3. **Branch Creation**: |
| 107 | ```bash |
| 108 | # Each agent creates branches in their own clone: |
| 109 | backend-engineer: git checkout -b task-123-auth-backend |
| 110 | frontend-engineer: git checkout -b task-124-auth-ui |
| 111 | qa-engineer: git checkout -b task-125-auth-tests |
| 112 | ``` |
| 113 | |
| 114 | 4. **Concurrent Pushes**: |
| 115 | ```bash |
| 116 | # All agents push to origin simultaneously: |
| 117 | git push -u origin task-123-auth-backend # ✅ Success |
| 118 | git push -u origin task-124-auth-ui # ✅ Success |
| 119 | git push -u origin task-125-auth-tests # ✅ Success |
| 120 | ``` |
| 121 | |
| 122 | ## Management Commands |
| 123 | |
| 124 | ### List Agent Clones |
| 125 | ```bash |
| 126 | staff list-agents # Shows which agents are running and their clone status |
| 127 | ``` |
| 128 | |
| 129 | ### Cleanup All Clones |
| 130 | ```bash |
| 131 | staff cleanup-clones # Removes all agent workspace directories |
| 132 | ``` |
| 133 | |
| 134 | ### Monitor Disk Usage |
| 135 | ```bash |
| 136 | du -sh workspace/ # Check total workspace disk usage |
| 137 | ``` |
| 138 | |
| 139 | ## Resource Considerations |
| 140 | |
| 141 | ### Disk Space |
| 142 | - Each clone uses ~repository size (typically 10-100MB per agent) |
| 143 | - For 10 agents with 50MB repo = ~500MB total |
| 144 | - Use `staff cleanup-clones` to free space when needed |
| 145 | |
| 146 | ### Network Usage |
| 147 | - Initial clone downloads full repository |
| 148 | - Subsequent `git pull` operations are incremental |
| 149 | - All agents share the same remote repository |
| 150 | |
| 151 | ### Performance |
| 152 | - Clone creation: ~2-5 seconds per agent (one-time cost) |
| 153 | - Git operations: Full speed, no waiting for locks |
| 154 | - Parallel processing: Linear scalability with agent count |
| 155 | |
| 156 | ## Comparison to Alternatives |
| 157 | |
| 158 | | Solution | Concurrency | Complexity | Performance | Risk | |
| 159 | |----------|-------------|------------|-------------|------| |
| 160 | | **Per-Agent Clones** | ✅ Full | 🟡 Medium | ✅ High | 🟢 Low | |
| 161 | | Global Git Mutex | ❌ None | 🟢 Low | ❌ Poor | 🟡 Medium | |
| 162 | | File Locking | 🟡 Limited | 🔴 High | 🟡 Medium | 🔴 High | |
| 163 | | Separate Repositories | ✅ Full | 🔴 Very High | ✅ High | 🔴 High | |
| 164 | |
| 165 | ## Future Enhancements |
| 166 | |
| 167 | - **Lazy Cleanup**: Auto-remove unused clones after N days |
| 168 | - **Clone Sharing**: Share clones between agents with similar tasks |
| 169 | - **Compressed Clones**: Use `git clone --depth=1` for space efficiency |
| 170 | - **Remote Workspaces**: Support for distributed agent execution |
| 171 | |
| 172 | The per-agent clone solution provides the optimal balance of performance, safety, and maintainability for concurrent AI agent operations. |