From f4dbaae0e3a1c77ace8b9976587118b8f1de6e01 Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 12:12:32 +0100 Subject: [PATCH] fix: Guard worktree creation against branch === baseBranch Throws if branch and baseBranch are identical, preventing git branch -f from force-resetting shared branches (like the initiative branch) when accidentally passed as both. --- apps/server/git/manager.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/server/git/manager.ts b/apps/server/git/manager.ts index f7d3c1b..a28dd2a 100644 --- a/apps/server/git/manager.ts +++ b/apps/server/git/manager.ts @@ -61,10 +61,18 @@ export class SimpleGitWorktreeManager implements WorktreeManager { const worktreePath = path.join(this.worktreesDir, id); log.info({ id, branch, baseBranch }, 'creating worktree'); + // Safety: never force-reset a branch to its own base — this would nuke + // shared branches like the initiative branch if passed as both branch and baseBranch. + if (branch === baseBranch) { + throw new Error(`Worktree branch and baseBranch are the same (${branch}). Use a unique branch name.`); + } + // Create worktree — reuse existing branch or create new one const branchExists = await this.branchExists(branch); if (branchExists) { - // Branch exists from a previous run — reset it to baseBranch and check it out + // Branch exists from a previous run — reset it to baseBranch and check it out. + // Only safe because branch !== baseBranch (checked above), so we're resetting + // an agent-scoped branch, not a shared branch like main or the initiative branch. await this.git.raw(['branch', '-f', branch, baseBranch]); await this.git.raw(['worktree', 'add', worktreePath, branch]); } else {