@@ -84,7 +84,8 @@ export class WorkspaceService {
8484 async createWorkspace (
8585 options : CreateWorkspaceOptions ,
8686 ) : Promise < WorkspaceInfo > {
87- const { taskId, mainRepoPath, folderId, folderPath, mode } = options ;
87+ const { taskId, mainRepoPath, folderId, folderPath, mode, branch } =
88+ options ;
8889 log . info (
8990 `Creating workspace for task ${ taskId } in ${ mainRepoPath } (mode: ${ mode } )` ,
9091 ) ;
@@ -117,6 +118,25 @@ export class WorkspaceService {
117118
118119 // Root mode: skip worktree creation entirely
119120 if ( mode === "root" ) {
121+ // try to create the branch, if it already exists just switch to it
122+ if ( branch ) {
123+ try {
124+ log . info ( `Creating/switching to branch ${ branch } for task ${ taskId } ` ) ;
125+ try {
126+ await execAsync ( `git checkout -b "${ branch } "` , { cwd : folderPath } ) ;
127+ log . info ( `Created and switched to new branch ${ branch } ` ) ;
128+ } catch ( _error ) {
129+ await execAsync ( `git checkout "${ branch } "` , { cwd : folderPath } ) ;
130+ log . info ( `Switched to existing branch ${ branch } ` ) ;
131+ }
132+ } catch ( error ) {
133+ log . error ( `Failed to create/switch to branch ${ branch } :` , error ) ;
134+ throw new Error (
135+ `Failed to create/switch to branch ${ branch } : ${ String ( error ) } ` ,
136+ ) ;
137+ }
138+ }
139+
120140 // Save task association without worktree
121141 const associations = getTaskAssociations ( ) ;
122142 const existingIndex = associations . findIndex ( ( a ) => a . taskId === taskId ) ;
@@ -219,7 +239,9 @@ export class WorkspaceService {
219239 let worktree : WorktreeInfo ;
220240
221241 try {
222- worktree = await worktreeManager . createWorktree ( ) ;
242+ worktree = await worktreeManager . createWorktree ( {
243+ baseBranch : branch ?? undefined ,
244+ } ) ;
223245 log . info (
224246 `Created worktree: ${ worktree . worktreeName } at ${ worktree . worktreePath } ` ,
225247 ) ;
0 commit comments