Skip to content

fix(worktree): new-feature.sh exits 1 when the issue's local branch exists without a worktree, while sequant run reuses the branch #1491

Closed

Description

@admarble

Found during graph-2026-10-freeze-c planning (Phase 0 of node C); not a graph node.

It lands after freeze-c node C, which edits the same script's branch-start logic and adds a parity test beside it.

Problem

When the issue's local branch feature/<N>-<slug> exists but no worktree has it checked out, the two worktree producers disagree:

  • templates/scripts/new-feature.sh:159-177 (scripts/new-feature.sh is a symlink to it) prints advice and exits 1. The advice is either git worktree add <path> <branch> or git branch -D <branch>. The second one deletes the branch's commits.
  • ensureWorktree (src/lib/workflow/worktree-manager.ts:1249-1253) runs git worktree add <path> <branch> and reuses the branch.

This state is common. sequant run removes a clean worktree at shutdown and keeps the branch (docs/reference/abort-command.md:45). git worktree remove does the same. The next standalone /exec, /fullsolve or /testgen on that issue goes through the script and stops.

Reproduced 2026-10-17 (main @ de03af8, git 2.47.1, bash). The setup:

  • a bare origin with main, and a clone;
  • a local feature/77-add-widget with one commit (7684edd feat: widget), its worktree removed;
  • gh stubbed to return the title add widget.

PATH=<stub>:$PATH bash templates/scripts/new-feature.sh 77 printed:

⚠️  Branch feature/77-add-widget already exists
❌ Branch exists but no worktree found
   To create worktree from existing branch:
   git worktree add ../worktrees/feature/77-add-widget feature/77-add-widget
   Or delete the branch first:
   git branch -D feature/77-add-widget

It exited 1. On the same repo, ensureWorktree(77, "add widget", true) created ../worktrees/feature/77-add-widget at 7684edd [feature/77-add-widget] and returned existed: false.

Acceptance Criteria

  • AC-1: When the issue's local branch exists and no worktree has it checked out, scripts/new-feature.sh <N> creates the worktree on that branch and exits 0. The worktree HEAD equals the branch tip from before the run, and the branch is not created, reset or deleted. Evidence: npx vitest run __tests__/new-feature-existing-branch.integration.test.ts -t "local branch without a worktree" reports ≥ 1 passed, 0 failed. It drives the real script with gh stubbed, as new-feature-worktree-root.integration.test.ts does. Mutation-verified by restoring the exit 1 advice branch.
  • AC-2: The script's result matches ensureWorktree's existing-branch rule (worktree-manager.ts:1249-1253: check out the local branch as is) on three fixtures: a local-only branch, a local branch equal to origin/<branch>, and a local branch behind origin/<branch>. In each, the worktree HEAD is the local branch tip. Evidence: npx vitest run __tests__/new-feature-existing-branch.integration.test.ts -t "existing-branch parity" reports ≥ 3 passed, 0 failed, mutation-verified by making the script start from origin/<branch> when it exists (the behind fixture fails).
  • AC-3: The other two paths keep today's result. A branch already checked out in a worktree prints its path and exits 0. No local branch takes the creation path. Evidence: npx vitest run __tests__/new-feature-existing-branch.integration.test.ts -t "worktree already checked out" reports ≥ 1 passed, 0 failed, and npx vitest run __tests__/new-feature-worktree-root.integration.test.ts __tests__/new-feature-checkout-guard.integration.test.ts reports 0 failed.

Prior fixes / producers

May touch

templates/scripts/new-feature.sh, __tests__/new-feature-existing-branch.integration.test.ts (new), docs/guides/git-workflows.md (### Script Usage, if the behaviour is described there), CHANGELOG.md.

Non-Goals

  • src/lib/workflow/worktree-manager.ts. The TS side is the source of truth and doesn't change.
  • Freshness of a reused branch (rebase or recreate when it is behind the base). ensureWorktree does that only for an existing worktree (worktree-manager.ts:997-1093).
  • Branch-name derivation (bug(worktree): new-feature.sh and sequant run derive different branch names for the same issue #1145) and the start ref for a remote-only branch (freeze-c node C).
  • The skill prose that calls the script, and any edit under plugin/, .claude/skills/ or templates/skills/ (plugin freeze).
  • A release. Installed scripts/dev/new-feature.sh copies get the fix with the first post-freeze release.

Activity

  1. added
    bug
    on Oct 17, 2026
  2. admarble commented on Oct 18, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (3 ACs, 3 directories)

    AC Quality Check

    All three ACs are single-line with Evidence: clauses. Notes:

    • Each Evidence command names __tests__/new-feature-existing-branch.integration.test.ts, which does not exist yet, so each fails today (not vacuous). AC-3's second command (new-feature-worktree-root and new-feature-checkout-guard tests) passes today by design (regression guard).
    • The "≥ N passed" counts depend on the exact -t describe names. Exec must use local branch without a worktree, existing-branch parity (3 cases) and worktree already checked out.
    • Line cites in the issue are stale. fix(worktree): new-feature.sh cuts a new branch from the base when origin/<branch> already exists — #1405's fix never reached the script #1486 has landed: the show-ref block is now at templates/scripts/new-feature.sh ~L159, and the ensureWorktree branch-reuse is at worktree-manager.ts ~L1380-1385 (git worktree add <path> <branch>). Behavior is unchanged.

    Scope Assessment

    Non-Goals: present in the issue (TS side, freshness, branch naming, skills/plugin, release).

    Metric Value Status
    Feature count 1 ✅
    AC items 3 ✅
    Directory spread 3 (templates/scripts, tests, docs) ✅

    Verdict: ✅ SCOPE_OK. Blocker #1486 is CLOSED. No in-flight worktree touches new-feature.sh.

    Acceptance Criteria

    AC-1: Existing local branch, no worktree → worktree created, exit 0

    Verification: Integration Test (real script, gh stubbed)
    Scenario: Given local feature/77-add-widget with one commit and no worktree → When new-feature.sh 77 → Then exit 0, worktree HEAD == prior branch tip, branch not recreated or reset.
    Assumptions: The fixture pattern from new-feature-start-ref.integration.test.ts (bare origin, clone, gh stub) is reusable.

    AC-2: Parity with ensureWorktree on 3 fixtures

    Verification: Integration Test
    Scenario: Given local-only / local == origin / local behind origin → When the script runs → Then worktree HEAD is the local tip in each (never origin/<branch>).
    Assumptions: Behind fixture must be a true fast-forward-behind. This is the case where #1486's -B origin/<branch> logic would wrongly win, so the existing-branch check has to run before resolve_branch_start_ref.

    AC-3: Other two paths unchanged

    Verification: Integration Test
    Scenario: Given branch checked out in a worktree → exit 0 and print the path. Given no local branch → creation path as before.

    Implementation Plan

    Phase 1: Script fix

    1. In templates/scripts/new-feature.sh replace the else advice branch (Branch exists but no worktree found + git branch -D hint) with git worktree add "$WORKTREE_DIR" "$BRANCH_NAME". Do not fetch, do not use -b/-B, and do not call resolve_branch_start_ref (matches TS: the local branch is checked out as is).
    2. Fall through to the shared post-creation code (the sequantBase config, env copy, install). Use no early exit, so the worktree gets the same setup as a fresh one. Keep branch.<b>.sequantBase: set it only if unset, so a prior recorded base is not overwritten.
    3. Harden the worktree lookup, which is currently git worktree list | grep "${BRANCH_NAME}". It is a substring match, so feature/7-x would match feature/7-xy. Prefer git worktree list --porcelain with an exact branch refs/heads/<b> match. This is the same class of fix, so keep it in the same file and cover it with a test, or record it as a follow-up if exec judges it out of scope.

    Phase 2: Tests and docs

    1. Add __tests__/new-feature-existing-branch.integration.test.ts with the three describe blocks named above. The file name has .integration., so it gets the integration timeout.
    2. Mutation-verify per CLAUDE.md. For AC-1, restore the exit 1 advice and confirm the local branch without a worktree test fails. For AC-2, start from origin/<branch> and confirm the behind fixture fails. Record Mutation-verified: markers in the PR body using the test file path.
    3. Update docs/guides/git-workflows.md ### Script Usage if it describes this case, and add a CHANGELOG.md entry.

    Design Review

    1. Where does this logic belong? In new-feature.sh's existing-branch block. The TS side (ensureWorktree) is the source of truth and does not change.
    2. Simplest correct approach? Run git worktree add <dir> <branch> instead of printing advice. Rejected: auto-rebasing or freshening the branch (non-goal), and sharing code with TS (a bash script that also runs in non-Node repos).
    3. Pattern / re-patch count: This is the 8th drift fix between script and TS (fix(worktree): new-feature.sh still uses npm install, so provisioning dirties the lockfile (#816 gap) #826, new-feature.sh hardcodes npm ci while the TS path is package-manager aware — pnpm/yarn/bun provisioning fails misleadingly #847, PM_CONFIG.yarn.ciInstall uses Yarn 2+ --immutable, which Yarn 1 rejects — no berry detection exists #871, fix(hooks): new-feature.sh bypasses the checkout guard — the script the block message recommends mutates the main checkout #910, bug(worktree): new-feature.sh and sequant run derive different branch names for the same issue #1145, feat(worktree): configurable worktree root #1199, fix(worktree): new-feature.sh cuts a new branch from the base when origin/<branch> already exists — #1405's fix never reached the script #1486). That is ≥2, so it is a class. The issue records the class and closes each fix with a parity test. AC-2 is that parity test. The class-level fix (a single producer) is out of scope for this issue and is already acknowledged in the issue's "Prior fixes" section. Not a divergence: the prescribed lever (the script) matches the review.
    4. Reviewer challenge: "Why not freshen a behind branch?" Answer: the Non-Goals and TS parity. ensureWorktree doesn't either for a new worktree.
    5. Producers/consumers: Producers of an issue worktree from an existing local branch: ensureWorktree and new-feature.sh (2). Consumers: /exec, /fullsolve, /testgen standalone. No other reference to the removed advice text; exec should grep the repo for Branch exists but no worktree found before finishing, including docs and tests.

    Feature Quality Planning

    All standard checks pass. Notable items:

    • The existing-worktree substring grep (Plan step 3) is a sibling bug in the same block.
    • set -e behavior: the git worktree add failure path (e.g. the target dir already exists) should print a clear error rather than die silently.
    • No plugin/, .claude/skills/ or templates/skills/ edits (plugin freeze).

    Open Questions

    1. Fix the substring worktree match in this PR? Recommendation: yes if it's a few lines with a test, otherwise file a follow-up. Impact if wrong: a rare false "already exists" for an issue number that is a prefix of another.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: Small bug fix to a single script. The ACs declare Evidence (integration tests) but the new test file is part of the fix itself, so testgen adds little for a bug-only issue.

    Label Review

    Current: bug
    Recommended: bug, planned
    Quality Loop: Won't auto-enable (SCOPE_OK, no complexity labels).

  3. added
    planned
    on Oct 18, 2026
  4. admarble commented on Oct 18, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 3/3 met

    Gaps:

    • Gate-test ACs AC-1 and AC-2 lack recorded mutation markers (QA ran both mutations; tests fail as expected)
    • Script change has no /verify evidence (section 11 Not Verified)

    Suggestions:

    • Pre-existing, not a regression. If a worktree directory was deleted without git worktree prune, the porcelain listing still shows its branch. The script then exits 0 and points at a directory that does not exist. The old grep behaved the same way. ensureWorktree only prunes on a retry. Worth a follow-up if it shows up in real use.
  5. added a commit that references this issue on Oct 19, 2026
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

    bugplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions