Subchapter 54.61
worktree-reference.mdMarkdown7 KBView on GitHub
Squad and all spawned agents may be running inside a git worktree rather than the main checkout. All .squad/ paths (charters, history, decisions, logs) MUST be resolved relative to a known team root, never assumed from CWD.
Two strategies for resolving the team root:
| Strategy | Team root | State scope | When to use |
|---|---|---|---|
| worktree-local | Current worktree root | Branch-local — each worktree has its own .squad/ state | Feature branches that need isolated decisions and history |
| main-checkout | Main working tree root | Shared — all worktrees read/write the main checkout’s .squad/ | Single source of truth for memories, decisions, and logs across all branches |
How the Coordinator resolves the team root (on every session start):
.squad/config.json in the current directory (or at the git root):
teamRoot is set → Team root = that path. STOP — do not walk further.stateLocation is "external" → Resolve external AppData path. Team root = external path. STOP..squad/ exist in the current working directory?
.squad/ lives in a subfolder.git rev-parse --show-toplevel to get the current worktree root..squad/ exists at that root (fall back to .ai-team/ for repos that haven’t migrated yet).
git worktree list --porcelainworktree line is the main working tree. Team root = that path.Passing the team root to agents:
TEAM_ROOT: {resolved_path} in every spawn prompt..squad/ paths from the provided team root — charter, history, decisions inbox, logs.Cross-worktree considerations (worktree-local strategy — recommended for concurrent work):
.squad/ files are branch-local. Each worktree works independently — no locking, no shared-state races..squad/ state merges with them. The append-only pattern ensures both sides only added content, making merges clean.merge=union driver in .gitattributes (see Init Mode) auto-resolves append-only files by keeping all lines from both sides — no manual conflict resolution needed..squad/ changes to the worktree’s branch. State flows to other branches through normal git merge / PR workflow.Cross-worktree considerations (main-checkout strategy):
.squad/ state on disk via the main checkout — changes are immediately visible without merging.decisions.md and git index. Use only when a single session is active at a time.When worktree mode is enabled, the coordinator creates dedicated worktrees for issue-based work. This gives each issue its own isolated branch checkout without disrupting the main repo.
Worktree mode activation:
worktrees: true in project config (squad.config.ts or package.json squad section)SQUAD_WORKTREES=1 set in environment variablesfalse (backward compatibility — agents work in the main repo)Creating worktrees:
{repo-parent}/{repo-name}-{issue-number}
C:\src\squad → worktree at C:\src\squad-42squad/{issue-number}-{kebab-case-slug} (created from base branch, typically main)Dependency management:
node_modules from the main repo to avoid reinstallingcmd /c "mklink /J {worktree}\node_modules {main-repo}\node_modules"ln -s {main-repo}/node_modules {worktree}/node_modulesnpm install in the worktreeReusing worktrees:
git worktree list shows all active worktreesgit pull to sync)git stash / git clean from one agent can delete another agent’s not-yet-committed files — prefer per-issue worktrees for parallel background workCleanup:
git worktree remove {path} + git branch -d {branch}When spawning an agent for issue-based work (user request references an issue number, or agent is working on a GitHub issue):
1. Check worktree mode:
SQUAD_WORKTREES=1 set in the environment?worktrees: true?2. If worktrees enabled:
a. Determine the worktree path:
#42, issue 42, GitHub issue assignment){repo-parent}/{repo-name}-{issue-number}C:\src\squad, issue #42 → C:\src\squad-42b. Check if worktree already exists:
git worktree list to see all active worktreessquad/{issue-number}-*)cd to the worktree pathgit pull to sync latest changesc. Create the worktree:
squad/{issue-number}-{kebab-case-slug} (derive slug from issue title if available)main, check default branch if needed)git worktree add {path} -b {branch} {baseBranch}git worktree add C:\src\squad-42 -b squad/42-fix-login maind. Set up dependencies:
node_modules from main repo to avoid reinstalling:
cmd /c "mklink /J {worktree}\node_modules {main-repo}\node_modules"ln -s {main-repo}/node_modules {worktree}/node_modulescd {worktree} && npm installe. Include worktree context in spawn:
WORKTREE_PATH to the resolved worktree pathWORKTREE_MODE to true3. If worktrees disabled:
WORKTREE_PATH to "n/a"WORKTREE_MODE to falsegit checkout -b flow (no changes to current behavior)