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.
This commit is contained in:
Lukas May
2026-03-06 12:12:32 +01:00
parent b853b28751
commit f4dbaae0e3

View File

@@ -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 {