Worktree Plugin
The worktree plugin provides workspace isolation using git worktrees, enabling multiple parallel agent sessions to work on different branches without duplicating the entire repository.Overview
Git worktrees allow you to check out multiple branches from the same repository simultaneously. This plugin creates isolated workspaces for each agent session by leveraging worktrees, which share the git object database but have separate working directories.Worktrees are ideal when you want fast workspace creation with minimal disk usage, as they share the git object database with the main repository.
Configuration
Configure the worktree plugin in youragent-orchestrator.yaml:
Configuration Options
Base directory where worktrees will be created. Path supports
~ expansion for home directory.Worktrees are organized as: {worktreeDir}/{projectId}/{sessionId}/How It Works
- Create Workspace: Creates a new git worktree from the specified branch
- Fetch Latest: Fetches the latest changes from
originbefore creating the worktree - Branch Creation: Creates a new feature branch based on the project’s default branch
- Symlink Support: Can symlink shared resources (e.g.,
node_modules,.env) from the main repository - Post-Create Hooks: Runs custom setup commands after workspace creation
Usage Example
Requirements
Path Segment Safety
Project IDs and session IDs must contain only safe characters:a-z, A-Z, 0-9, _, -
This prevents directory traversal attacks and ensures filesystem compatibility.
Features
Workspace Lifecycle
Create- Fetches latest from remote (fails silently if offline)
- Creates worktree with new branch from
origin/{defaultBranch} - Handles branch name conflicts gracefully
- Creates symlinks for shared resources
- Runs post-create hooks
- Removes worktree using
git worktree remove --force - Falls back to manual directory deletion if git command fails
- Does NOT delete branches (prevents accidental deletion of pre-existing branches)
- Lists all worktrees for a project using
git worktree list --porcelain - Returns workspace info including path, branch, session ID
- Checks if workspace path exists and is a valid git working tree
- Uses
git rev-parse --is-inside-work-tree
- Recreates a workspace after it was destroyed or corrupted
- Prunes stale worktree entries
- Fetches latest from remote
- Creates worktree on existing or new branch
Symlink Support
Symlinks allow sharing large directories like
node_modules or vendor across worktrees, saving disk space and installation time... segments for security.
Post-Create Hooks
Run custom commands after workspace creation:Troubleshooting
Error: Branch already exists
Error: Branch already exists
The plugin handles this automatically by creating the worktree and checking out the existing branch.If checkout fails, it removes the orphaned worktree and throws an error.
Error: Invalid projectId or sessionId
Error: Invalid projectId or sessionId
Project IDs and session IDs must match the pattern:
/^[a-zA-Z0-9_-]+$/Use only letters, numbers, hyphens, and underscores.Symlink targets are missing
Symlink targets are missing
Symlinks are created from
{mainRepoPath}/{symlinkPath} to {worktreePath}/{symlinkPath}.If the source doesn’t exist, the symlink is skipped silently.Worktree cleanup failed
Worktree cleanup failed
If
git worktree remove fails, the plugin falls back to rmSync to forcefully delete the directory.Stale worktree entries can be cleaned up with:Fetch fails (offline mode)
Fetch fails (offline mode)
The plugin catches fetch errors and continues. Worktrees can be created offline using cached refs.If you need the latest changes, ensure network connectivity before creating workspaces.
Advanced Configuration
Custom Worktree Directory Structure
Worktrees are organized by project and session:Comparison with Clone Plugin
| Feature | Worktree | Clone |
|---|---|---|
| Disk usage | Low (shared objects) | High (full copy) |
| Creation speed | Fast | Slower |
| Independence | Shared git database | Fully independent |
| Use case | Parallel branches | Isolated experiments |
Security Considerations
Source Code
Source:packages/plugins/workspace-worktree/src/index.ts