Copilot agent session worktree creation silently fails when repository is in detached HEAD state
@lszomoru is already working on this.
Since Jun 30, 2026.
Assessment
This issue has not been assessed yet.
Description
Bug Report
VS Code version: 1.123.0
Copilot Chat version: 0.51.0
OS: Windows
Describe the bug
When the workspace repository is in detached HEAD state, starting a new Copilot CLI agent session fails to create an isolated worktree. The session silently falls back to isolationEnabled: false with no visible error to the user.
Steps to reproduce
- Have a git repository where the main working directory is in detached HEAD (e.g.
git checkout <commit-sha>) - Start a new Copilot agent session that triggers worktree creation
Expected behavior
The worktree is created successfully (the underlying git worktree add command succeeds), and the session runs with isolation enabled.
Actual behavior
The Copilot Chat extension log shows:
[ChatSessionWorktreeService][_createWorktree] Failed to create worktree for isolation.
…and the session starts with isolationEnabled: false, worktreePath: undefined.
Notably, the git worktree add command itself succeeds — the worktree directory is created on disk — but the result is discarded.
Root cause
In ChatSessionWorktreeService._createWorktree, after gitService.createWorktree() succeeds, the result is only returned if activeRepository.headBranchName is truthy:
if (worktreePath && activeRepository.headCommitHash && activeRepository.headBranchName) {
// build and return properties
}
// falls through → logs error, returns undefined
In detached HEAD, headBranchName is undefined, so the condition fails even though the worktree was created successfully. However, headBranchName is only actually used as a fallback when baseBranch is not provided:
const baseBranchName = baseBranch ?? activeRepository.headBranchName;
The guard is overly strict — it should not require headBranchName when baseBranch is already supplied by the caller, or when the worktree was successfully created regardless.
Suggested fix
if (worktreePath && activeRepository.headCommitHash && (activeRepository.headBranchName || baseBranch)) {
Or more robustly, separate the guard from the fallback so that a missing headBranchName only affects baseBranchName computation rather than discarding the entire result.
- Dominant language
- TypeScript
- Stars
- 193k
- Forks
- 42.9k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/vscode
-
testplan-item
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
new release
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
testplan-item
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
testplan-item
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
All issues in microsoft/vscode
Similar issues
-
clawsweeper:fix-shape-clear clawsweeper:queueable-fix clawsweeper:source-repro impact:ux-friction issue-rating: 🦞 diamond lobster no-stale P3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulty 1/5 Under an hour Newbie friendliness 76/100
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
vercel/react-tweet#225 ·