Skip to content

fix(worktree): a non-default bare --base is cut from local <base> but counted against origin/<base> (#1380 follow-up, #1234/#1386 class) #1406

Closed

Description

@admarble

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

Follow-up from #1380 (PR #1403; spec Open Question 3, the answer to graph-2026-10-freeze OQ-2). Same class as #1234 and #1386: one base value, consumers that disagree.

Problem

For a non-default bare base (--base release or settings.run.defaultBase: "release", where the repo default is main):

  • Worktree creation cuts from local release: computeWorktreeBaseRef (worktree-manager.ts:826) returns the bare name unchanged when it isn't the detected default.
  • Staleness (countCommitsBehind, :244) and the recorded base compare against origin/release.

Local release can lag origin/release or not exist at all. In the first case a new worktree starts from an old commit while freshness reads as current. In the second, worktree creation fails or picks up an unrelated local ref.

Acceptance Criteria

  • AC-1: For a non-default bare base, worktree creation, staleness and the recorded sequantBaseRef all use the same ref (origin/<base> after a fetch, unless the base is explicitly local-only). Evidence: a temp-repo test where local release lags origin/release, asserting the new worktree's HEAD equals origin/release, mutation-verified by restoring the bare return in computeWorktreeBaseRef.
  • AC-2: A base that exists only locally (no origin/<base>) still works, as today. Evidence: a test with a local-only base branch.
  • AC-3: --stacked predecessor refs (local branches by design) are unaffected. Evidence: the existing feat(cli): --stacked flag — set chain-mode PR base to predecessor branch #605 tests pass.

Prior fixes / producers

May touch

src/lib/workflow/worktree-manager.ts (computeWorktreeBaseRef, countCommitsBehind), its tests, CHANGELOG.md.

Non-Goals

Activity

  1. added
    bug
    graph-2026-10-freeze-b
    on Oct 10, 2026
  2. added 2 commits that reference this issue on Oct 16, 2026
  3. admarble commented on Oct 16, 2026

    @admarble
    ContributorAuthor

    Runner context (graph-2026-10-freeze-b, wave 2)

    Wave 1 merged on 2026-10-16. main is at 9f1fb3f and includes #1440, #1396, #1305, #1325, #1405 and #1409. These notes are for both the exec phase and the qa phase:

  4. admarble commented on Oct 16, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (3 ACs, 1 directory)

    AC Quality Check

    • AC-1: Evidence is concrete, but "unless the base is explicitly local-only" is undefined. This plan reads it as: no origin/<base> after a fetch. A literal reading would need a new flag.
    • AC-2: Evidence ("a test with a local-only base branch") names no assertion. Tighten it: the worktree HEAD equals local <base> and sequantBaseRef is <base>.
    • AC-3: Evidence is the existing feat(cli): --stacked flag — set chain-mode PR base to predecessor branch #605 tests. They pass on unchanged code, so they gate nothing for the new branch. This is acceptable only as a regression guard. Add one test where the chain predecessor also exists on origin and the worktree still cuts from the local predecessor.
    • AC-1's mutation (restore the bare return) is valid. It must fail exactly the lag test, so use origin/release ahead of local release.

    Scope Assessment

    Non-Goals: present (base resolution, ensureWorktree's branch-existence check, plugin/skill dirs).

    Metric Value Status
    Feature count 1 ✅
    AC items 3 ✅
    Directory spread 1 ✅

    Verdict: ✅ SCOPE_OK

    Acceptance Criteria

    AC-1: One ref for creation, staleness and the record

    Verification: Integration Test (temp repos)
    Scenario: Given a bare origin with main and release, and a clone whose local release lags origin/release → When ensureWorktree(..., baseBranch="release", chainMode=false) → Then the worktree HEAD equals origin/release, sequantBaseRef is origin/release, and countCommitsBehind(wt, "release") is 0.
    Assumptions: The fetch of origin release has already run before the ref is chosen. It does at worktree-manager.ts:1169, but only when !isLocalBranch, so that gate must change too.

    AC-2: A local-only base still works

    Verification: Integration Test
    Scenario: Given a base branch that exists locally with no origin/<base> → When the worktree is created → Then it is cut from local <base> and the record is <base>. The failed fetch must not error.

    AC-3: --stacked is unaffected

    Verification: Integration Test (existing #605/#1234 tests plus one new case)
    Scenario: Given chain mode and a predecessor that also exists on origin → Then the worktree is cut from the local predecessor.

    Implementation Plan

    Phase 1: One resolver (worktree-manager.ts)

    1. computeWorktreeBaseRef (:826) is pure and cannot tell a stacked predecessor from a non-default bare base. Both arrive as a bare name. Add the distinguishing input rather than guessing. chainMode is already known in ensureWorktree (:947), and ensureWorktrees passes false (:1366). The new signature takes { chainMode, hasRemote }, where hasRemote is a local git rev-parse --verify --quiet refs/remotes/origin/<base> check, injected so the function stays unit-testable.
    2. Resolution order: no base → origin/<default>; origin/… → as-is; chain mode → bare local; non-default bare with a remote ref → origin/<base>; otherwise bare (local-only).
    3. In ensureWorktree (:1157-1180) drop isLocalBranch and fetch origin <base> whenever not in chain mode. A failed fetch is already tolerated, which covers AC-2. Do the fetch before resolving the ref.
    4. The three other callers (:1005, :1074, and the reuse and recreate paths) go through the same helper so creation, recreate and the record agree. countCommitsBehind already compares origin/<base>, so it needs no change once the others converge.

    Phase 2: Tests and changelog

    1. Add temp-repo tests for AC-1, AC-2 and the AC-3 addition. Run the AC-1 mutation (restore the bare return) and record Mutation-verified: AC-1 — … in the PR body. Keep braces out of the marker text (2.19.0 parser).
    2. Add a CHANGELOG.md entry. Run npx vitest run src/lib/workflow/worktree-manager.test.ts and the run-base-parity integration test. Do not run the full suite.

    Design Review

    1. Where does this logic belong? In computeWorktreeBaseRef alone. Every consumer already routes through it.
    2. Simplest correct approach? Make the chain/non-chain distinction explicit and check for origin/<base>. Rejected: probing for origin/<base> inside the function with no chain flag. A stacked predecessor that was pushed would then silently switch to the remote ref and break feat(cli): --stacked flag — set chain-mode PR base to predecessor branch #605.
    3. Pattern and re-patch count? Pattern: the base-ref helper from bug(exec): sequant run worktrees never record sequantBase, so the zero-diff/uncommitted guard compares against origin/main and a --base ahead of main disables it #1234. Prior fixes: feat(cli): --stacked flag — set chain-mode PR base to predecessor branch #605, bug(exec): sequant run worktrees never record sequantBase, so the zero-diff/uncommitted guard compares against origin/main and a --base ahead of main disables it #1234, fix(run): --base creates the worktree from the branch but opens the PR against the repo default branch #1386, ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380. That is four, so this is not precedent. The fix is class-level: one helper that every consumer uses, with parity tests on creation and record. fix(run): phase agents never get SEQUANT_BASE_BRANCH — skills fall back to main on master-default repos #1460 follows in this file, so keep the helper signature stable.
    4. Senior challenge: "Why not always use origin/<base>?" Because local-only bases (AC-2) and stacked predecessors must stay local.
    5. Producers and consumers: Producer is ResolvedRun.baseBranch (run-orchestrator.ts:721). Consumers are computeWorktreeBaseRef ×4 call sites, countCommitsBehind (worktree-manager.ts:312, run-orchestrator.ts:1351) and recordWorktreeBaseRef/readRecordedBaseRef (phase-executor.ts:821). One producer, so no divergence.

    No SPEC_DIVERGENCE: the plan agrees with the issue's prescription.

    Feature Quality Planning

    All standard checks pass. Notable items:

    Open Questions

    1. What does "explicitly local-only" mean? Recommendation: no origin/<base> after the fetch, with no new flag. Impact if wrong: a user who wants a local ref over an existing remote one has no option, but that case is not in the ACs.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: A single-file bug fix. AC-1 declares evidence (a temp-repo test) but this is a bug label, so testgen is skipped. No UI.

    Label Review

    Current: bug, graph-2026-10-freeze-b
    Recommended: add planned
    Quality Loop: won't auto-enable (SCOPE_OK)

  5. added
    planned
    on Oct 16, 2026
  6. admarble commented on Oct 16, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 3/3 met

    Gaps:

    • AC-1 is a gate-test AC with no recorded mutation marker (QA reproduced the mutation: deleting the hasRemote origin/ return fails only the 1406 lagging-base test)
  7. added a commit that references this issue on Oct 16, 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

    buggraph-2026-10-freeze-bplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions