Skip to content

Commit 5601df8

Browse files
committed
🤖 fix: validate trunkBranch exists before sub-agent workspace creation
When forkWorkspace() fails during task creation, the fallback logic used parentMeta.name directly as the trunkBranch for git worktree creation. This fails when the parent's branch doesn't exist locally (e.g., SSH workspaces, or branches that were never fetched). The fix validates that trunkBranch exists as a local branch before using it, falling back to the repository's default trunk branch (main/master/etc) if not found. For non-git projects (LocalRuntime), git commands are wrapped in try-catch to fall back to "main". Fixes the cosmetic error: fatal: 'git-debounce-5w4e' is not a commit and a branch 'agent_explore_7eeffc7df6' cannot be created from it
1 parent d52e599 commit 5601df8

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/node/services/taskService.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { HistoryService } from "@/node/services/historyService";
1010
import type { PartialService } from "@/node/services/partialService";
1111
import type { InitStateManager } from "@/node/services/initStateManager";
1212
import { log } from "@/node/services/log";
13+
import { detectDefaultTrunkBranch, listLocalBranches } from "@/node/git";
1314
import { createRuntime } from "@/node/runtime/runtimeFactory";
1415
import type { InitLogger, WorkspaceCreationResult } from "@/node/runtime/Runtime";
1516
import { validateWorkspaceName } from "@/common/utils/validation/workspaceValidation";
@@ -497,9 +498,23 @@ export class TaskService {
497498
initLogger,
498499
});
499500

500-
const trunkBranch = forkResult.success
501-
? (forkResult.sourceBranch ?? parentMeta.name)
502-
: parentMeta.name;
501+
let trunkBranch: string;
502+
if (forkResult.success && forkResult.sourceBranch) {
503+
trunkBranch = forkResult.sourceBranch;
504+
} else {
505+
// Fork failed - validate parentMeta.name is a valid local branch.
506+
// For non-git projects (LocalRuntime), git commands fail - fall back to "main".
507+
try {
508+
const localBranches = await listLocalBranches(parentMeta.projectPath);
509+
if (localBranches.includes(parentMeta.name)) {
510+
trunkBranch = parentMeta.name;
511+
} else {
512+
trunkBranch = await detectDefaultTrunkBranch(parentMeta.projectPath, localBranches);
513+
}
514+
} catch {
515+
trunkBranch = "main";
516+
}
517+
}
503518
const createResult: WorkspaceCreationResult = forkResult.success
504519
? { success: true as const, workspacePath: forkResult.workspacePath }
505520
: await runtime.createWorkspace({

0 commit comments

Comments
 (0)