Skip to content

Commit 6f40d1d

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 7010f8d commit 6f40d1d

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
@@ -9,6 +9,7 @@ import type { HistoryService } from "@/node/services/historyService";
99
import type { PartialService } from "@/node/services/partialService";
1010
import type { InitStateManager } from "@/node/services/initStateManager";
1111
import { log } from "@/node/services/log";
12+
import { detectDefaultTrunkBranch, listLocalBranches } from "@/node/git";
1213
import { createRuntime } from "@/node/runtime/runtimeFactory";
1314
import type { InitLogger, WorkspaceCreationResult } from "@/node/runtime/Runtime";
1415
import { validateWorkspaceName } from "@/common/utils/validation/workspaceValidation";
@@ -483,9 +484,23 @@ export class TaskService {
483484
initLogger,
484485
});
485486

486-
const trunkBranch = forkResult.success
487-
? (forkResult.sourceBranch ?? parentMeta.name)
488-
: parentMeta.name;
487+
let trunkBranch: string;
488+
if (forkResult.success && forkResult.sourceBranch) {
489+
trunkBranch = forkResult.sourceBranch;
490+
} else {
491+
// Fork failed - validate parentMeta.name is a valid local branch.
492+
// For non-git projects (LocalRuntime), git commands fail - fall back to "main".
493+
try {
494+
const localBranches = await listLocalBranches(parentMeta.projectPath);
495+
if (localBranches.includes(parentMeta.name)) {
496+
trunkBranch = parentMeta.name;
497+
} else {
498+
trunkBranch = await detectDefaultTrunkBranch(parentMeta.projectPath, localBranches);
499+
}
500+
} catch {
501+
trunkBranch = "main";
502+
}
503+
}
489504
const createResult: WorkspaceCreationResult = forkResult.success
490505
? { success: true as const, workspacePath: forkResult.workspacePath }
491506
: await runtime.createWorkspace({

0 commit comments

Comments
 (0)