Skip to content

feat: Add git worktree isolation for concurrent issue handling#3

Open
roger704 wants to merge 1 commit intocoleam00:mainfrom
roger704:feature/worktree-isolation
Open

feat: Add git worktree isolation for concurrent issue handling#3
roger704 wants to merge 1 commit intocoleam00:mainfrom
roger704:feature/worktree-isolation

Conversation

@roger704
Copy link

Summary

This PR adds git worktree isolation to allow concurrent handling of multiple GitHub issues without workspace conflicts.

Problem

When handling multiple GitHub issues concurrently, they all share the same workspace directory (/workspace/REPO/). This causes conflicts:

  1. Issue #29 creates a branch and makes code changes
  2. Issue #26 webhook arrives, triggers git cleanup/reset
  3. Issue #29's uncommitted work is destroyed

This is especially problematic for agents that take time to complete tasks.

Solution

Each issue/PR now gets its own isolated git worktree:

/workspace/REPO/           # Base repo (shared git history)
/workspace/REPO-issue-29/  # Worktree for issue #29
/workspace/REPO-issue-26/  # Worktree for issue #26

Features Added

Feature Description
Worktree per issue getOrCreateWorktree() creates isolated worktrees
Retry logic 3 attempts with exponential backoff for transient failures
Base repo check Detects missing repo after container restart and clones first
Always update cwd Session always uses correct worktree path, not just on first request
Auto cleanup Worktrees cleaned when PR is merged or issue is closed
Stale cleanup Worktrees older than 7 days are automatically removed

Benefits

  • Concurrent work - Multiple issues can be worked on simultaneously
  • Disk efficient - Worktrees share git history with base repo
  • Container restarts - Handles gracefully by detecting missing repos
  • Self-cleaning - Prevents disk exhaustion from accumulated worktrees

Changes

  • Modified getOrCreateCodebaseForRepo() to accept optional issueNumber parameter
  • Added getOrCreateWorktree() with retry logic
  • Added cleanupWorktree() and cleanupStaleWorktrees()
  • Handle closed events to trigger cleanup
  • Always update conversation cwd to current worktree path

Testing

Tested in production with concurrent issue handling:

  • Multiple issues processed simultaneously without conflicts
  • Worktrees created and cleaned up correctly
  • Container restarts handled gracefully

Problem:
When handling multiple GitHub issues concurrently, they all share the same
workspace directory. This causes conflicts when:
- Issue coleam00#1 creates a branch and makes changes
- Issue coleam00#2 arrives and triggers git cleanup
- Issue coleam00#1's uncommitted work is destroyed

Solution:
Each issue/PR now gets its own isolated git worktree:
- /workspace/REPO/           # Base repo (shared git history)
- /workspace/REPO-issue-29/  # Worktree for issue #29
- /workspace/REPO-issue-26/  # Worktree for issue #26

Features added:
1. **Worktree per issue** - getOrCreateWorktree() creates isolated worktrees
2. **Retry logic** - 3 attempts with exponential backoff for transient failures
3. **Base repo check** - Detects missing repo after container restart
4. **Always update cwd** - Session always uses correct worktree path
5. **Auto cleanup** - Worktrees cleaned on PR merge/issue close
6. **Stale cleanup** - Worktrees older than 7 days are automatically removed

Benefits:
- Concurrent work on multiple issues without interference
- Disk-efficient (worktrees share git history)
- Handles container restarts gracefully
- Self-cleaning to prevent disk exhaustion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant