Skip to content

Session deletion fails with "not a git repository" for sessions tied to removed background-agent worktrees #2893

Description

@chboeye

Description

Deleting a session fails with the following error:

Delete failed
Failed to delete session: git ["--no-optional-locks", "status", "--porcelain=v1", "-b", "-M", "-uall"] failed: fatal: not a git repository (or any of the parent directories): .git

Root cause (confirmed via local investigation)

Each session record in session-store.db stores a cwd path. For sessions created from background-agent tasks, this cwd points to a disposable per-task Git worktree under:

~/.copilot/repos/copilot-worktrees/<repo-name>/<random-worktree-name>/

Once the background task finishes, the worktree folder is often removed (either by the user, by app cleanup, or by the worktree lifecycle itself), but the corresponding row in sessions is not always cleaned up in sync. When the user later tries to delete that now-orphaned session, the app runs git status against the recorded cwd to build session context before deleting it. Since the directory either:

  1. no longer exists at all, or
  2. still exists but its .git was removed,

git status fails with fatal: not a git repository, and this failure is surfaced as a hard error that blocks session deletion entirely, instead of being treated as a non-fatal condition.

Evidence

Inspecting a read-only copy of session-store.db, out of 1181 total sessions:

  • 194 sessions reference a cwd directory that no longer exists on disk.
  • 47 sessions reference a cwd directory that still exists, but its .git is missing.

All of the above 241 sessions are tied to ephemeral worktrees created for background-agent tasks (e.g. ~/.copilot/repos/copilot-worktrees/<repo>/chboeye-microsoft-<random-name>/), confirming these are orphaned worktree references, not a corrupted main repository. The main project repo and all currently-existing worktrees have valid .git directories and git status runs fine on them.

Impact

Any session whose worktree has since been deleted becomes permanently un-deletable through the UI, since the delete action itself depends on a successful git status call.

Expected behavior

Session deletion should not depend on the target directory still being a valid Git repository. If git status fails because the directory is missing or is no longer a Git repo, the app should:

  • Skip/ignore the Git status check for a deletion action, and/or
  • Fall back gracefully (e.g., treat as "no git context") instead of throwing a fatal error, and
  • Still allow the session record to be deleted from the database.

Environment

  • OS: Windows
  • Affected: session deletion for sessions created via background-agent worktrees

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions