Skip to content

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

Closed

Description

@admarble

Epic: graph-2026-10-freeze-c Tier: mechanical Execution: sequant_run

Follow-up from #1405 (PR #1468). sequant run now starts a first-time worktree from origin/<branch>, through resolveBranchStartRef in src/lib/workflow/worktree-manager.ts. scripts/new-feature.sh still does not. This is the seventh divergence between the script and the TS worktree path (see Prior fixes).

Problem

On a machine with no local feature/<N>-<slug> branch, templates/scripts/new-feature.sh:

  • checks only refs/heads/<branch> (git show-ref --verify --quiet "refs/heads/${BRANCH_NAME}");
  • then runs git worktree add "$WORKTREE_DIR" -b "$BRANCH_NAME" "origin/${BASE_BRANCH}".

When the issue already has a pushed PR branch on origin, the new worktree starts from the base without the PR's commits. Pushing from it then diverges from the PR, or a force push discards the PR's work. The skills send standalone /exec, /fullsolve and /testgen here, and docs/guides/git-workflows.md documents it.

Reproduced 2026-10-16 (main @ 0857484, git 2.47.1). The setup:

  • a bare origin with main and a pushed feature/77-add-widget carrying one commit;
  • a fresh clone;
  • gh stubbed to return the issue title.

bash scripts/new-feature.sh 77 printed Creating new worktree from main.... The worktree HEAD was origin/main's commit, not origin/feature/77-add-widget's. The PR's file was absent.

Acceptance Criteria

  • AC-1: On a fresh clone where origin/<branch> exists and is not merged into the base, scripts/new-feature.sh <N> creates the worktree from origin/<branch>: its HEAD equals origin/<branch> and the local branch tracks it. Evidence: npx vitest run __tests__/new-feature-start-ref.integration.test.ts -t "AC-1" 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 making the script's start-ref function always return the base ref.
  • AC-2: For each fixture, the script's start ref equals resolveBranchStartRef(repo, branch, baseRef) on the same repo. The fixtures are: an unmerged remote branch, a squash-merged remote branch, a merge-commit-merged remote branch, a deleted remote branch with a stale tracking ref, and that deleted case under LC_ALL=de_DE.UTF-8. Evidence: npx vitest run __tests__/new-feature-start-ref.integration.test.ts -t "parity" reports ≥ 5 passed, 0 failed. They compare the script's gh-free --print-start-ref <branch> <base-ref> output with the TS function. Mutation-verified by removing the script's squash-merge check (the squash-merged case fails).
  • AC-3: When origin/<branch> does not exist, the script behaves as today: the worktree starts from origin/<base> and branch.<name>.sequantBase records the base. Evidence: npx vitest run __tests__/new-feature-start-ref.integration.test.ts -t "AC-3" 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 report 0 failed.
  • AC-4: docs/guides/git-workflows.md says the helper starts from origin/<branch> when the remote already has the issue's branch, and from origin/<base> otherwise. Evidence: grep -n "origin/<branch>" docs/guides/git-workflows.md prints a line under ### Script Usage.

Prior fixes / producers

May touch

templates/scripts/new-feature.sh (scripts/new-feature.sh is a symlink to it), __tests__/new-feature-start-ref.integration.test.ts (new), docs/guides/git-workflows.md, CHANGELOG.md.

Non-Goals

Activity

  1. added
    bug
    graph-2026-10-freeze-c
    on Oct 17, 2026
  2. admarble commented on Oct 17, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (4 ACs, 3 directories)

    AC Quality Check

    All four ACs are one line, with Evidence: clauses naming a command. Run against main:

    • AC-1..AC-3: __tests__/new-feature-start-ref.integration.test.ts does not exist, so every vitest Evidence fails today. Not vacuous.
    • AC-4: grep -n "origin/<branch>" docs/guides/git-workflows.md prints nothing today. Not vacuous. It does not check that the line sits under ### Script Usage; /qa should confirm that by reading the section (lines 246-258).
    • AC-2 locale fixture: de_DE.UTF-8 is installed here, but CI may lack it. A missing locale makes git fall back to English, so the fixture passes without the LC_ALL=C the script needs. See Open Question 1.

    Scope Assessment

    Non-Goals: present in the issue (TS side, "branch exists, no worktree" path, skill prose and plugin/, release).

    Metric Value Status
    Feature count 1 ✅
    AC items 4 ✅
    Directory spread 3 (templates/scripts, __tests__, docs/guides) + CHANGELOG ✅

    Verdict: ✅ SCOPE_OK

    Acceptance Criteria

    AC-1: Fresh clone, unmerged origin/<branch> → worktree starts there

    Verification: Integration Test
    Scenario: Given a bare origin with main and a pushed feature/N-slug and a fresh clone, with gh stubbed → When new-feature.sh N runs → Then worktree HEAD == origin/<branch> and branch.<b>.remote/merge track it.
    Assumptions: git worktree add -b <b> <path> origin/<b> sets upstream by default (branch.autoSetupMerge). The test should assert tracking, not assume it.

    AC-2: Script start ref equals resolveBranchStartRef on 5 fixtures

    Verification: Integration Test (parity)
    Scenario: Given each fixture (unmerged, squash-merged, merge-commit-merged, deleted-with-stale-tracking-ref, deleted under LC_ALL=de_DE.UTF-8) → When new-feature.sh --print-start-ref <branch> <base-ref> runs in the repo and the TS function runs on the same repo → Then the two refs are equal.
    Assumptions: Fixtures reset the repo state between the two calls. Both fetch, and the fetch is idempotent.

    AC-3: No origin/<branch> → unchanged behaviour

    Verification: Integration Test
    Scenario: Given no remote branch → When the script runs → Then HEAD == origin/<base> and branch.<b>.sequantBase == base. The existing worktree-root and checkout-guard suites stay green.

    AC-4: Docs

    Verification: Manual Test + grep
    Scenario: Given ### Script Usage in docs/guides/git-workflows.md → Then it says the helper starts from origin/<branch> when the remote has the branch, else origin/<base>.

    Implementation Plan

    Phase 1: Script (templates/scripts/new-feature.sh)

    1. Add resolve_branch_start_ref <branch> <base-ref>, a bash port of resolveBranchStartRef (worktree-manager.ts:882):
      • LC_ALL=C git fetch origin <branch> with stderr captured. "couldn't find remote ref" returns the base ref. Any other fetch failure (offline) falls through to the tracking ref. set -e must not abort here (use if/|| true).
      • rev-parse --verify --quiet refs/remotes/origin/<branch>; absent → base ref.
      • merge-base --is-ancestor origin/<b> <base> true → base ref.
      • Squash check: merge-tree --write-tree <base> origin/<b> first line == <base>^{tree}, else fall through (needs git ≥2.38, same as TS) → base ref.
      • Else origin/<branch>.
        Put it next to branch_name_for, with a "SOURCE OF TRUTH" comment like the other ported helpers.
    2. Add the --print-start-ref <branch> <base-ref> early exit beside --print-branch (before the gh checks, operating on the cwd repo).
    3. In the main flow, after git fetch origin "$BASE_BRANCH", call the helper with origin/${BASE_BRANCH}. If the result is origin/<branch>, run git worktree add "$WORKTREE_DIR" -B "$BRANCH_NAME" "origin/${BRANCH_NAME}" (matches TS -B at worktree-manager.ts:1245). Otherwise keep today's -b … origin/<base>. Keep the sequantBase config write for both paths.
    4. Fix the "Creating new worktree from ${BASE_BRANCH}..." message so it names the real start ref.

    Phase 2: Tests (__tests__/new-feature-start-ref.integration.test.ts, new)

    Model it on new-feature-worktree-root.integration.test.ts (bare origin, gh stub, throwaway repo). .integration. in the name gets the long timeout.

    • AC-1 test: drives the real script.
    • parity describe with 5 cases: import resolveBranchStartRef and compare with --print-start-ref.
    • AC-3 test.
    • Record the mutation results in the PR body using the Mutation-verified: AC-N — …; <test file path> > … failed; restored. marker. The failedTest must start with the file path.

    Phase 3: Docs and changelog

    1. Edit the ### Script Usage paragraph (the one saying it branches off origin/<base>).
    2. Add a CHANGELOG.md [Unreleased] → Fixed entry (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).

    Design Review

    1. Where does this logic belong? In new-feature.sh as a bash port. The script runs in non-Node repos and as an installed copy with no node_modules/sequant, as the issue says. The TS side stays untouched.
    2. Simplest correct approach? A port plus a parity test. Rejected: calling sequant or tsx from the script, which adds a runtime dependency. Rejected: a new sequant worktree subcommand, which is a larger surface than a bug fix.
    3. Existing pattern and re-patch count? The hand-copied-logic-plus-drift-test class: 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 and feat(worktree): configurable worktree root #1199 (six prior fixes, which is 2 or more). Per the stop rule, the issue handles this by naming the class and making the lever the parity test, which compares behaviour instead of strings. That is a legitimate class-level fix for the start ref. The remaining drift points (the "local branch exists, no worktree" path) are explicitly out of scope and unfiled. Recommend filing one issue for them so the class stays tracked. Not a divergence, so no halt.
    4. What would a senior reviewer challenge? "Why not one shared implementation?" Answered in point 1. "Why -B?" It matches the TS path and tolerates a stale local ref. Safe here because the earlier refs/heads check already exits when the branch exists.
    5. Producers and consumers of the start ref: two producers, resolveBranchStartRef (first-create at worktree-manager.ts:1228, stale-recreate at :1033) and new-feature.sh. The parity test on all five fixtures is the parity guard. Consumers: scripts/new-feature.sh is a symlink to the template, and scripts/dev/new-feature.sh installs are copies (sync), which is why no release is claimed.

    No ADR needed: the design choice is the one the issue already states.

    Feature Quality Planning

    All standard checks pass. Notable items:

    • Offline or fetch failure must not abort under set -e. Add a test only if cheap.
    • The first git fetch origin "$BASE_BRANCH" can fail for a repo without that remote branch (today's behaviour). Leave it.
    • Sibling scan: the "branch exists, no worktree" path (lines ~125-140) is a known out-of-scope sibling. I did not widen scope.

    Open Questions

    1. AC-2 locale fixture can pass vacuously. If de_DE.UTF-8 is not installed, git emits English and the fixture passes without LC_ALL=C. Recommendation: the test asserts the locale is installed (locale -a, or git actually printing German) and skipIfs loudly otherwise. Mutation-verify it by removing LC_ALL=C from the script's fetch. Impact if ignored: the locale regression is ungated on CI.
    2. Should the origin/<branch> path also record sequantBase? Recommendation: yes, same as the base path, so the phase-executor zero-diff guard (fix(orchestrator): respect custom base branch in exec zero-diff guard (#534 follow-up) #537) resolves the right comparison ref. AC-3 only constrains the absent-branch case.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: One-script bug fix with a new integration test. Tests are written alongside the fix, so testgen adds little. No UI, no security surface. The plugin freeze applies, and this touches only templates/scripts/, tests and docs.

    Label Review

    Current: bug, graph-2026-10-freeze-c
    Recommended: + planned
    Quality Loop: Won't auto-enable.

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

    @admarble
    ContributorAuthor

    QA Verdict: NEEDS_VERIFICATION

    AC coverage: 1/4 met

    Gaps:

    • The bash fetch can hang where the TS one times out after 30 seconds

    Suggestions:

    • The TS fetch has a 30-second timeout; the bash fetch has none. Offline with a hanging remote, the script could block where TS gives up. This is minor and does not cause any AC to fail.
    • The bash $(merge-tree … | head -n 1) ignores merge-tree's exit code, while TS requires status 0. If the merge conflicts, the output is a tree with conflicts that will not equal the base tree. So the result is the same, but the reason is different from TS.

    Pending ACs:

    • AC-1
    • AC-2
    • AC-3

    Needs human review:

    • AC-1..AC-3 Evidence commands, CI status and the PR-body mutation markers could not be checked because the disk is full
    • The 'local branch exists, no worktree' divergence is neither filed nor dropped
  5. admarble commented on Oct 17, 2026

    @admarble
    ContributorAuthor

    Runner verification of QA's NEEDS_VERIFICATION, on d84eeac. QA could not run the Evidence commands because the disk was full. The runner ran them exactly as written:

    • AC-1: 1 passed.
    • AC-2 (parity): 5 passed, including the German-locale case.
    • AC-3: 1 passed, plus 7 passed in the two regression files.
    • AC-4: the grep matches under ### Script Usage.

    Both mutations named in the ACs failed the expected tests. The records are in PR #1495. The 'local branch exists, no worktree' divergence is filed as #1491.

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

    buggraph-2026-10-freeze-cplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions