Repository navigation
Description
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/mainRoot 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 fallback2. 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"; // hardcodedProposed Fix
Add auto-detection with the standard override chain:
CLI --base → settings.run.defaultBase → auto-detect from remote → "main"
Implementation
-
New utility:
detectDefaultBranch()Use
git symbolic-ref refs/remotes/origin/HEADto detect the remote's default branch. This is set automatically bygit cloneand is the same pattern used bygh(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 }
-
Wire into resolution chain (
src/commands/run.ts~line 2401)const resolvedBaseBranch = options.base ?? settings.run.defaultBase ?? detectDefaultBranch();
-
Pass resolved base to
rebaseBeforePR()— add abaseBranchparameter and use it instead of hardcoded"origin/main". -
Update
ensureWorktree()— the"origin/main"fallback on line 636 becomes unreachable once the caller always passes a resolved value, but update it defensively to usedetectDefaultBranch().
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 runworks out-of-the-box on repos withmaster(or any) default branch — no--baseflag required -
detectDefaultBranch()utility readsorigin/HEADsymbolic ref without a network call when available - Falls back to
git remote set-head origin --autowhenorigin/HEADis missing - Falls back to
"main"as final hardcoded default -
--baseflag andsettings.run.defaultBasestill 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.tsor similar fordetectDefaultBranch()
Activity
admarble commentedon Mar 7, 2026
Spec Plan — #343
AC Checklist
sequant runworks on repos withmaster(or any) default branch — no--baserequireddetectDefaultBranch()readsorigin/HEADsymbolic ref without network callgit remote set-head origin --autowhenorigin/HEADmissing"main"as final hardcoded default--baseandsettings.run.defaultBasestill override auto-detectionrebaseBeforePR()uses resolved base branch, not hardcoded"origin/main"ensureWorktree()fallback updated to use detected branch-v) logs which branch was detected/usedmainImplementation Plan
Phase 1: Create
detectDefaultBranch()insrc/lib/workflow/worktree-manager.tsgit symbolic-ref refs/remotes/origin/HEAD→ fallbackgit remote set-head origin --auto→ fallback"main"Phase 2: Wire into resolution chain
src/commands/run.ts:193—?? undefined→?? detectDefaultBranch()Phase 3: Fix
rebaseBeforePR()— addbaseBranchparameter, replace hardcoded refsPhase 4: Fix
checkWorktreeFreshness()— addbaseBranchparameterPhase 5: Fix remaining hardcoded
"main"ingit-diff-utils.ts,pr-status.tsPhase 6: Unit tests for
detectDefaultBranch(), updaterebaseBeforePRtestsAffected Files
src/lib/workflow/worktree-manager.ts— main changessrc/commands/run.ts— resolution chainsrc/lib/workflow/batch-executor.ts— pass baseBranch throughsrc/lib/workflow/git-diff-utils.ts— default paramsrc/lib/workflow/pr-status.ts— hardcoded refsfix(#343): auto-detect default branch instead of hardcoding origin/main
fix(#343): use detected base branch in stale worktree messages
admarble commentedon Mar 8, 2026
/fullsolve Complete
Issue: #343
Status: READY_FOR_MERGE
AC Coverage
masteror any default branchdetectDefaultBranch()readsorigin/HEADwithout networkgit remote set-head origin --auto"main"as final default--baseandsettings.run.defaultBaseoverride auto-detectrebaseBeforePR()uses resolved base branchensureWorktree()fallback uses detected branchmain-based reposQuality Metrics
Iterations
PR: #345
fix(#343): auto-detect default branch instead of hardcoding origin/ma…