miyabi-worktree
Git worktree management for parallel agent execution in Miyabi.
Features
- Automated Worktree Management: Create, manage, and cleanup Git worktrees for isolated parallel task execution
- Concurrency Control: Built-in Semaphore-based parallel execution limiting
- WorktreePool: High-level abstraction for batch parallel task execution
- Status Tracking: Monitor worktree lifecycle (Active, Idle, Completed, Failed)
- Timeout Support: Configurable task execution timeouts
- Auto-cleanup: Automatic worktree removal after task completion
- Telemetry & Observability: Built-in lifecycle event tracking with structured logging and metrics
Architecture
WorktreeManager
Low-level worktree operations with automatic concurrency control via tokio::sync::Semaphore.
use WorktreeManager;
// Automatic repository discovery from current directory
let manager = new_with_discovery?;
// Create worktree for issue #123
let worktree = manager.create_worktree.await?;
println!;
// Push changes
manager.push_worktree.await?;
// Merge to main
manager.merge_worktree.await?;
// Cleanup
manager.remove_worktree.await?;
WorktreePool
High-level parallel task execution with lifecycle management.
use ;
let config = PoolConfig ;
let pool = new?;
// Execute tasks in parallel
let tasks = vec!;
let result = pool.execute_parallel.await;
println!;
println!;
Simplified API
For simple use cases, execute_simple() provides automatic lifecycle management:
use ;
let config = default;
let pool = new?;
let issue_numbers = vec!;
let result = pool.execute_simple.await;
if result.all_successful
Concurrency Control
The WorktreeManager internally uses tokio::sync::Semaphore to limit concurrent worktree operations:
// Semaphore is automatically managed
let manager = new_with_discovery?;
// These calls will be limited to 3 concurrent executions
for i in 0..10 // Semaphore permit released
Statistics & Monitoring
// Get worktree statistics
let stats = manager.stats.await;
println!;
println!;
// Get pool statistics
let pool_stats = pool.stats.await;
println!;
Telemetry & Observability
The WorktreeManager includes built-in telemetry for lifecycle event tracking:
Event Recording
Automatically records the following events:
- CreateStart / CreateComplete: Worktree creation lifecycle
- CleanupStart / CleanupComplete: Worktree cleanup lifecycle
- ExecuteStart / ExecuteComplete: Agent execution (via hooks)
- Error: Error events with context
Human-Readable Reports
// Generate telemetry report
let report = manager.telemetry_report.await;
println!;
// Output:
// 📊 Worktree実行レポート
// - 作成: 5回
// - 実行: 5回(成功: 4, 失敗: 1)
// - クリーンアップ: 5回
// - エラー: 1回
// - 平均実行時間: 12.34秒
// - 成功率: 80.0%
Structured Statistics
use TelemetryStats;
let stats: TelemetryStats = manager.telemetry_stats.await;
println!;
println!;
println!;
println!;
Integration with Structured Logging
All telemetry events are also logged with tracing:
use tracing_subscriber;
// Initialize structured logging
init;
// Telemetry events will be logged:
// INFO Worktree作成開始 worktree_id="abc123" branch="feature/issue-100"
// INFO Worktree作成完了 worktree_id="abc123" duration_ms=1234
Task Results
let result = pool.execute_parallel.await;
for task_result in result.results
// Aggregate metrics
println!;
println!;
Error Handling
All operations return Result<T, MiyabiError>:
use Result;
async
Configuration Options
PoolConfig
Testing
Run unit tests:
Run integration tests (requires git repository):
Integration with CoordinatorAgent
The miyabi-worktree crate is designed to integrate with Miyabi's CoordinatorAgent for parallel issue processing:
use ;
// CoordinatorAgent creates tasks from GitHub Issues
let tasks: = issues
.into_iter
.map
.collect;
// Execute in parallel with concurrency limit
let pool = new?;
let result = pool.execute_parallel.await;
Performance Considerations
- Semaphore Overhead: Minimal overhead for concurrency control
- Git Operations: Worktree creation/removal involves git CLI calls
- Filesystem I/O: Each worktree requires disk space and I/O operations
- Recommended Concurrency: 3-5 for optimal performance on typical hardware
Safety & Cleanup
- Automatic cleanup with
auto_cleanup: true - Manual cleanup with
manager.cleanup_all().await - Git worktree prune on cleanup
- Proper error handling for failed operations
License
This crate is part of the Miyabi project. See LICENSE for details.