1. Core Architecture - Worktree Management System
Purpose: Create isolated Git worktrees for parallel SPEC development.
Key Components:
- Worktree Registry - Central registry tracking all worktrees
- Manager Layer - Core worktree operations including create, switch, remove, and sync
- CLI Interface - User-friendly command interface
- Models - Data structures for worktree metadata
- Integration Layer - MoAI-ADK workflow integration
Registry Structure:
The registry file stores worktree metadata in JSON format. Each worktree entry contains an identifier, file path, branch name, creation timestamp, last sync time, status (active or merged), and base branch reference. The config section defines the worktree root directory, auto-sync preference, and cleanup behavior for merged branches.
File Structure:
The worktree system creates a dedicated directory structure inside the project's .moai directory. At the worktree root ({repo}/.moai/worktrees/{ProjectName}/), you will find the central registry JSON file and individual directories for each SPEC. Each SPEC directory contains a .git file for worktree metadata and a complete copy of all project files.
Detailed Reference: Refer to Worktree Management Module at modules/worktree-management.md
---
2. CLI Commands - Complete Command Interface
Purpose: Provide intuitive CLI commands for worktree management.
Core Commands:
To create a new worktree for a SPEC, use the new command followed by the SPEC ID and description. To list all worktrees, use the list command. To switch to a specific worktree, use the switch command with the SPEC ID. To get the worktree path for shell integration, use the go command with eval. To sync a worktree with its base branch, use the sync command. To remove a worktree, use the remove command. To clean up merged worktrees, use the clean command. To show worktree status, use the status command. For configuration management, use the config command with get or set subcommands.
Command Categories:
- Creation: The new command creates an isolated worktree
- Navigation: The list, switch, and go commands enable browsing and navigating
- Management: The sync, remove, and clean commands maintain worktrees
- Status: The status command checks worktree state
- Configuration: The config command manages settings
Shell Integration:
For switching to a worktree directory, two approaches work well. The switch command directly changes to the worktree directory. The go command outputs a cd command that can be evaluated by the shell, which is the recommended pattern for shell scripts and automation.
Detailed Reference: Refer to Worktree Commands Module at modules/worktree-commands.md
---
3. Parallel Development Workflow - Isolated SPEC Development
Purpose: Enable true parallel development without context switching.
Workflow Integration:
During the Plan Phase using /moai:1-plan, the SPEC is created and the worktree new command sets up automatic worktree isolation.
During the Development Phase, the isolated worktree environment provides independent Git state with zero context switching overhead.
During the Sync Phase using /moai:3-sync, the worktree sync command ensures clean integration with conflict resolution support.
During the Cleanup Phase, the worktree clean command provides automatic cleanup with registry maintenance.
Parallel Development Benefits:
- Context Isolation: Each SPEC has its own Git state, files, and environment
- Zero Switching Cost: Instant switching between worktrees
- Independent Development: Work on multiple SPECs simultaneously
- Safe Experimentation: Isolated environment for experimental features
- Clean Integration: Automatic sync and conflict resolution
Example Workflow:
First, create a worktree for SPEC-001 with a description like "User Authentication" and switch to that directory. Then run /moai:2-run SPEC-001 to develop in isolation. Next, navigate back to the main repository and create another worktree for SPEC-002 with description "Payment Integration". Switch to that worktree and run /moai:2-run SPEC-002 for parallel development. When needed, switch between worktrees and continue development. Finally, sync both worktrees when ready for integration.
Detailed Reference: Refer to Parallel Development Module at modules/parallel-development.md
---
4. Integration Patterns - MoAI-ADK Workflow Integration
Purpose: Seamless integration with MoAI-ADK Plan-Run-Sync workflow.
Integration Points:
During Plan Phase Integration with /moai:1-plan, after SPEC creation, create the worktree using the new command with the SPEC ID. The output provides guidance for switching to the worktree using either the switch command or the shell eval pattern with the go command.
During Development Phase with /moai:2-run, worktree isolation provides a clean development environment with independent Git state preventing conflicts and automatic registry tracking.
During Sync Phase with /moai:3-sync, before PR creation run the sync command for the SPEC. After PR merge, run the clean command with the merged-only flag to remove completed worktrees.
Auto-Detection Patterns:
The system detects worktree environments by checking for the registry file in the parent directory. When detected, the SPEC ID is extracted from the current directory name. The status command with sync-check option automatically identifies worktrees that need synchronization.
Configuration Integration:
The MoAI configuration supports worktree settings including auto_create for automatic worktree creation, auto_sync for automatic synchronization, cleanup_merged for automatic cleanup of merged branches, and worktree_root for specifying the worktree directory location with project name substitution.
Detailed Reference: Refer to Integration Patterns Module at modules/integration-patterns.md
---