402
return workingDirectory;
403
}
405
>
// Idempotent: if a worktree was already created for this session in this
406
>
// process (e.g. the caller re-enters materialization after a thread
407
>
// restart or a post-creation failure) reuse it rather than creating a
408
>
// second branch + worktree.
409
>
const already = this._createdWorktrees.get(sessionId);
410
>
if (already) {
411
return already.worktree;
412
}
414
>
const repositoryRoot = await this._gitService.getRepositoryRoot(workingDirectory);
415
>
if (!repositoryRoot) {
416
return workingDirectory;
417
}
419
>
const worktreesRoot = getWorktreesRoot(repositoryRoot);
420
>
// Prefix (e.g. the user's `git.branchPrefix`) the client forwards for
421
>
// worktree-isolated sessions. Prepended ahead of the built-in `agents/`
422
>
// prefix when naming the branch and stripped from the worktree dir name.
423
>
const worktreeBranchPrefix = typeof config[SessionConfigKey.WorktreeBranchPrefix] === 'string'
424
? config[SessionConfigKey.WorktreeBranchPrefix] as string
426
const selectedBranch = config[SessionConfigKey.Branch] as string;
427
const { branchName, worktree, baseBranch } = await this._worktreeCreationSequencer.queue(repositoryRoot.toString(), async () => {
429
>
sessionId,
430
>
message: prompt,
431
>
githubToken,
432
>
branchPrefix: worktreeBranchPrefix,
433
>
branchNameCollides: async candidate => {
434
if (await this._gitService.branchExists(repositoryRoot, candidate).catch(() => true)) {
435
return true;