Skip to content

bug(run): worktree creation and rebase hardcode 'origin/main' — fails on repos with non-main default branch #343

Closed

Description

@admarble

Summary

sequant run fails with fatal: invalid reference: origin/main on repos whose default branch is master (or any non-main name). The --base flag and settings.run.defaultBase exist but are only partially wired — worktree creation respects them, but rebaseBeforePR() hardcodes origin/main. Additionally, there's no auto-detection of the remote's default branch, so users must manually configure every non-main repo.

Reproduction

# In a repo where the default branch is 'master'
git branch -a
# * master
#   remotes/origin/master

npx sequant run 1 2 3 -q
# ❌ Failed to create worktree: fatal: invalid reference: origin/main

Root Cause

Three related issues:

1. No auto-detection of default branch

When no --base flag or settings.run.defaultBase is provided, the resolution chain falls through to a hardcoded "main":

// src/commands/run.ts ~line 2401
const resolvedBaseBranch =
  options.base ?? settings.run.defaultBase ?? undefined;

When undefined, ensureWorktree() defaults to "origin/main" at line 636:

const baseRef = baseBranch
  ? /* ... resolved logic ... */
  : "origin/main"; // hardcoded fallback

2. rebaseBeforePR() ignores --base entirely

Even when --base master is passed correctly and worktrees are created successfully, the pre-PR rebase always targets origin/main:

// src/commands/run.ts ~line 1129
["git", "-C", worktreePath, "fetch", "origin", "main"]
// ~line 1148
["git", "-C", worktreePath, "rebase", "origin/main"]

The resolved base branch is never passed to rebaseBeforePR().

3. Fetch fallback also hardcodes "main"

// src/commands/run.ts ~line 639
const branchToFetch = baseBranch
  ? baseBranch.replace(/^origin\//, "")
  : "main"; // hardcoded

Proposed Fix

Add auto-detection with the standard override chain:

CLI --base  →  settings.run.defaultBase  →  auto-detect from remote  →  "main"

Implementation

  1. New utility: detectDefaultBranch()

    Use git symbolic-ref refs/remotes/origin/HEAD to detect the remote's default branch. This is set automatically by git clone and is the same pattern used by gh (GitHub CLI).

    function detectDefaultBranch(): string {
      // Try reading the symbolic ref (no network call)
      const result = spawnSync("git", ["symbolic-ref", "refs/remotes/origin/HEAD"], { stdio: "pipe" });
      if (result.status === 0) {
        // "refs/remotes/origin/master" → "master"
        return result.stdout.toString().trim().replace("refs/remotes/origin/", "");
      }
      // If not set, try refreshing it (requires network, but sequant already fetches)
      const autoResult = spawnSync("git", ["remote", "set-head", "origin", "--auto"], { stdio: "pipe" });
      if (autoResult.status === 0) {
        const retry = spawnSync("git", ["symbolic-ref", "refs/remotes/origin/HEAD"], { stdio: "pipe" });
        if (retry.status === 0) {
          return retry.stdout.toString().trim().replace("refs/remotes/origin/", "");
        }
      }
      return "main"; // final fallback
    }
  2. Wire into resolution chain (src/commands/run.ts ~line 2401)

    const resolvedBaseBranch =
      options.base ?? settings.run.defaultBase ?? detectDefaultBranch();
  3. Pass resolved base to rebaseBeforePR() — add a baseBranch parameter and use it instead of hardcoded "origin/main".

  4. Update ensureWorktree() — the "origin/main" fallback on line 636 becomes unreachable once the caller always passes a resolved value, but update it defensively to use detectDefaultBranch().

Edge Cases

Scenario Behavior
origin/HEAD not set (repo created via git init + remote add) Falls back to git remote set-head origin --auto (network call), then "main"
Stale origin/HEAD (remote renamed branch) User can fix with git remote set-head origin --auto or use --base
No remote at all Falls through to "main" fallback (unlikely — sequant creates PRs)
Shallow clone symbolic-ref still works; only an issue if ref wasn't fetched
--base explicitly provided Auto-detect is skipped entirely (highest priority)

Acceptance Criteria

  • sequant run works out-of-the-box on repos with master (or any) default branch — no --base flag required
  • detectDefaultBranch() utility reads origin/HEAD symbolic ref without a network call when available
  • Falls back to git remote set-head origin --auto when origin/HEAD is missing
  • Falls back to "main" as final hardcoded default
  • --base flag and settings.run.defaultBase still override auto-detection
  • rebaseBeforePR() uses the resolved base branch, not hardcoded "origin/main"
  • ensureWorktree() fallback updated to use detected branch
  • Verbose mode (-v) logs which branch was detected/used
  • No behavior change for repos where default branch is already main

Affected Files

  • src/commands/run.ts — ensureWorktree(), rebaseBeforePR(), resolution logic
  • Possibly new src/lib/git-utils.ts or similar for detectDefaultBranch()

Activity

  1. added
    bug
    cli
    auto-ready
    ai-in-progress
    on Mar 1, 2026
  2. admarble commented on Mar 7, 2026

    @admarble
    ContributorAuthor

    Spec Plan — #343

    AC Checklist

    • AC-1: sequant run works on repos with master (or any) default branch — no --base required
    • AC-2: detectDefaultBranch() reads origin/HEAD symbolic ref without network call
    • AC-3: Falls back to git remote set-head origin --auto when origin/HEAD missing
    • AC-4: Falls back to "main" as final hardcoded default
    • AC-5: --base and settings.run.defaultBase still override auto-detection
    • AC-6: rebaseBeforePR() uses resolved base branch, not hardcoded "origin/main"
    • AC-7: ensureWorktree() fallback updated to use detected branch
    • AC-8: Verbose mode (-v) logs which branch was detected/used
    • AC-9: No behavior change for repos where default branch is already main

    Implementation Plan

    Phase 1: Create detectDefaultBranch() in src/lib/workflow/worktree-manager.ts

    • git symbolic-ref refs/remotes/origin/HEAD → fallback git remote set-head origin --auto → fallback "main"

    Phase 2: Wire into resolution chain

    • src/commands/run.ts:193 — ?? undefined → ?? detectDefaultBranch()
    • Add verbose logging

    Phase 3: Fix rebaseBeforePR() — add baseBranch parameter, replace hardcoded refs

    Phase 4: Fix checkWorktreeFreshness() — add baseBranch parameter

    Phase 5: Fix remaining hardcoded "main" in git-diff-utils.ts, pr-status.ts

    Phase 6: Unit tests for detectDefaultBranch(), update rebaseBeforePR tests

    Affected Files

    • src/lib/workflow/worktree-manager.ts — main changes
    • src/commands/run.ts — resolution chain
    • src/lib/workflow/batch-executor.ts — pass baseBranch through
    • src/lib/workflow/git-diff-utils.ts — default param
    • src/lib/workflow/pr-status.ts — hardcoded refs
  3. added
    ai-in-progress
    and removed
    ai-in-progress
    on Mar 7, 2026
  4. added 2 commits that reference this issue on Mar 8, 2026
  5. admarble commented on Mar 8, 2026

    @admarble
    ContributorAuthor

    /fullsolve Complete

    Issue: #343
    Status: READY_FOR_MERGE

    AC Coverage

    AC Description Status
    AC-1 Works on repos with master or any default branch ✅ MET
    AC-2 detectDefaultBranch() reads origin/HEAD without network ✅ MET
    AC-3 Falls back to git remote set-head origin --auto ✅ MET
    AC-4 Falls back to "main" as final default ✅ MET
    AC-5 --base and settings.run.defaultBase override auto-detect ✅ MET
    AC-6 rebaseBeforePR() uses resolved base branch ✅ MET
    AC-7 ensureWorktree() fallback uses detected branch ✅ MET
    AC-8 Verbose mode logs detected branch ✅ MET
    AC-9 No behavior change for main-based repos ✅ MET

    Quality Metrics

    • Tests: 100/100 passed (4 new)
    • Build: ✅ Clean
    • Type Safety: ✅ No errors

    Iterations

    Phase Iterations Status
    Spec 1/1 Complete (prior session)
    Exec 1/1 Complete
    QA 2/2 Complete (fixed stale worktree messages)

    PR: #345

  6. added
    ai-in-progress
    and removed
    ai-in-progress
    on Mar 10, 2026
  7. added a commit that references this issue on Mar 11, 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

    auto-readybugcli

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions