Skip to content

fix(repos): investigate detached HEAD after a successful worktrees add #6354

Done

Description

@devops-cli-service

Split from #6340.

Problem: On 2026-11-18 a run of devops repos worktrees add on the clone of dan-petty/homelab left the clone in detached HEAD. The body records no code path in src/devops_cli/git/worktree_add.py or the reclaim that detaches HEAD. The cause is unknown. Why deferred: The main change stops a failed origin read before any worktree removal and asserts that HEAD is unchanged on that path. A detachment on a successful run has no evidence, so this change cannot state a test or a fix. Acceptance criteria: A test runs devops repos worktrees add on a clone of a private repository and shows the clone stays on its branch after the command exits 0. The test fails on current main if the defect is present, or the issue records the evidence that the detached HEAD came from outside the command. A fix keeps the clone on its branch after a successful worktrees add run.

Proposed design

Problem statement

Add a postcondition guard that restores the original branch and an offline regression test for devops repos worktrees add. A successful run may leave the clone in detached HEAD, and the cause is unknown. The add paths in worktree_add.py do not change the main worktree HEAD. The regression test covers the create, take-over and reclaim paths. The private-repository live check is deferred because it needs credentials.

Acceptance criteria

Requirement: Create path keeps the clone branch

The devops repos worktrees add command SHALL leave the clone on its original branch after it creates a worktree.

Scenario: Successful create keeps HEAD

  • WHEN A test runs devops repos worktrees add on a temporary clone with a local bare origin and a stubbed claim, and the command exits 0.
  • THEN The test asserts that git -C <clone> symbolic-ref --short -q HEAD returns the original branch.

Requirement: Take-over path keeps the clone branch

The devops repos worktrees add command SHALL leave the clone on its original branch after it takes over a pushed branch.

Scenario: Successful take-over keeps HEAD

  • WHEN A test runs devops repos worktrees add on a temporary clone with a local bare origin and an App-authored branch, and the command exits 0.
  • THEN The test asserts that git -C <clone> symbolic-ref --short -q HEAD returns the original branch.

Requirement: Reclaim keeps the clone branch

The reclaim of merged worktrees SHALL leave the clone on its original branch.

Scenario: Reclaim keeps HEAD

  • WHEN A test calls reclaim_merged_worktrees with a test double for prune_worktrees that detaches HEAD and returns an empty report.
  • THEN The test asserts that git -C <clone> symbolic-ref --short -q HEAD returns the original branch.

Requirement: Restore failure is a typed error

The command MUST raise GitOperationError when it cannot restore the original branch.

Scenario: Failed restore reports a typed error

  • WHEN A test calls add_worktree with a test double that detaches HEAD and a restore helper that fails.
  • THEN The test asserts that GitOperationError is raised and its message names the original branch.

Existing Tool Considered

No alternatives check has run. The Git CLI covers the need: git symbolic-ref --short -q HEAD reads the current branch and git checkout <branch> restores it. Git lacks an automated regression test, so the change adds a pytest test around the existing command. No new dependency is needed.

Key questions

  • Which code path in src/devops_cli/git/worktree_add.py changes the HEAD of the main worktree?: The create and take-over paths do not change it. _create runs git worktree add --no-track -b <branch> <directory> <start>, and _take_over runs git worktree add --track -b <branch> <directory> origin/<branch>. Both create a separate worktree and run no git checkout or git switch in the clone. [sources: src/devops_cli/git/worktree_add.py]
  • How should the regression test provide an origin without a private repository?: Use a temporary clone with a local bare repository as origin. The OriginReads protocol and _CloneOrigin read origin with Git commands, so a local bare origin exercises the same code. Keep the private-repository run as a separate live check. (best practice) [sources: src/devops_cli/git/worktree_add.py]
    • Chosen: local bare origin. It runs offline with no secret and exercises the same Git reads.
    • Rejected: private repository clone. It needs network, credentials and a private repository, so it cannot run in CI.
  • What should the command do when a successful add leaves HEAD detached?: Read the original branch before the command changes any worktree, and restore it before the command returns. If the restore fails, raise GitOperationError that names the branch. A test double that detaches HEAD proves the guard. (best practice) [sources: src/devops_cli/git/worktree_add.py]
    • Chosen: postcondition guard. It keeps the clone on its branch and gives a testable failure path.
    • Rejected: test only. It records the behavior but does not keep the clone on its branch when the defect occurs.
  • Where should the postcondition guard run?: In a context manager in src/devops_cli/git/worktree_add.py that wraps reclaim_merged_worktrees and add_worktree. Both public operations then keep HEAD, and the dry-run path of add_worktree returns inside the guard without a change. (best practice) [sources: src/devops_cli/git/worktree_add.py]
    • Chosen: context manager around both public functions. It covers the reclaim and the add, and every caller uses the same guard.
    • Rejected: inside add_worktree only. The reclaim runs before add_worktree, so a detach there would be missed.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    ContributorAuthor
  2. added
    type/bug
    source/agent
    status/new
    value/medium
    effort/medium
    scope/repos
    priority/p2-medium
    on Nov 19, 2026
  3. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    ContributorAuthor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  4. added
    status/in-progress
    and removed
    status/new
    on Nov 24, 2026
  5. added
    status/in-review
    and removed
    status/in-progress
    on Nov 24, 2026
  6. removed
    status/in-review
    on Nov 24, 2026
  7. devops-cli-service commented on Nov 24, 2026

    @devops-cli-service
    ContributorAuthor

    Live since 2026-11-24 20:09 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/repossource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions