Copilot agent session worktree creation silently fails when repository is in detached HEAD state

Open
#323,751 1 comment 0 reactions 1 assignee View on GitHub

@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
  1. Have a git repository where the main working directory is in detached HEAD (e.g. git checkout <commit-sha>)
  2. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/vscode

All issues in microsoft/vscode

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.