Skip to content

Commit b704d7c

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. 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 b704d7c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/node/services/taskService.ts

Lines changed: 14 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,19 @@ 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+
const localBranches = await listLocalBranches(parentMeta.projectPath);
493+
if (localBranches.includes(parentMeta.name)) {
494+
trunkBranch = parentMeta.name;
495+
} else {
496+
// Parent branch doesn't exist locally, fall back to default trunk
497+
trunkBranch = await detectDefaultTrunkBranch(parentMeta.projectPath, localBranches);
498+
}
499+
}
489500
const createResult: WorkspaceCreationResult = forkResult.success
490501
? { success: true as const, workspacePath: forkResult.workspacePath }
491502
: await runtime.createWorkspace({

0 commit comments

Comments
 (0)