Skip to content

fix(run): phase agents never get SEQUANT_BASE_BRANCH — skills fall back to main on master-default repos #1460

Closed

Description

@admarble

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

Problem

Phase agents get no base-branch information. The CLI resolves the base correctly: #343 added detectDefaultBranch() (worktree-manager.ts:180) and settings.run.defaultBase, and phase-executor.ts records the base per branch (branch.<b>.sequantBaseRef, read back by resolveBaseRef, :906). But the env built for the phase agent (phase-executor.ts:1683–1710) sets SEQUANT_WORKTREE, SEQUANT_ISSUE, SEQUANT_ORCHESTRATOR, SEQUANT_PHASE and SEQUANT_ISSUE_TYPE, and no base branch. The skills that do read one (BASE="${SEQUANT_BASE_BRANCH:-main}" in exec, qa, loop and testgen) therefore always fall back to main. grep -rn SEQUANT_BASE_BRANCH src finds nothing.

On a repo whose default branch is master, every origin/main in a phase's git commands fails. Measured in varius (default master): git diff origin/main...HEAD --name-only prints fatal: ambiguous argument 'origin/main...HEAD' and exits 128. The skill-side half of the fix is #1461.

Acceptance Criteria

  • AC-1: The phase env sets SEQUANT_BASE_BRANCH to the base branch name (no origin/ prefix) taken from resolveBaseRef(cwd), for every phase, including spec, which runs in the main checkout with no recorded base. Evidence: phase-executor unit tests for a worktree phase with a recorded sequantBaseRef of origin/master and for a no-worktree spec phase on a repo whose origin/HEAD is master, both asserting env.SEQUANT_BASE_BRANCH === "master", mutation-verified by deleting the assignment
  • AC-2: resolveBaseRef(cwd)'s literal "origin/main" fallback (used when HEAD is detached, nothing is recorded, or git fails) is replaced by origin/<default branch of cwd>, using detectDefaultBranch with an optional cwd argument (default process.cwd(), so existing callers are unchanged), so no hard-coded main is left on the resolver path. Evidence: a resolveBaseRef unit test in a temp repo whose default branch is master, with no recorded base, passing the temp repo as cwd with no process.cwd patch, returning origin/master, mutation-verified by restoring the literal
  • AC-3: docs/reference/run-command.md lists SEQUANT_BASE_BRANCH with the other phase env variables. Evidence: grep -n SEQUANT_BASE_BRANCH docs/reference/run-command.md returns the new row

Why AC-2 is in scope: resolveBaseRef (phase-executor.ts:906–937) returns the literal origin/main in four places whenever there is no recorded base. Worktree phases always have a recorded base (#1234), but spec runs in the main checkout on the default branch with nothing recorded. Without AC-2, spec on a master repo would get SEQUANT_BASE_BRANCH=main, which is the bug this issue fixes. AC-2 adds no new producer. It routes the fallback to the existing detectDefaultBranch(). Callers that benefit too: the exec guard (phase-executor.ts:1000) and hasCommitsAheadOfBase (worktree-manager.ts:421, not edited).

Graph note (graph-2026-10-freeze-b, 2026-10-16): detectDefaultBranch() runs its git commands in process.cwd(), not in a passed cwd (worktree-manager.ts:180). Called from resolveBaseRef(cwd) as-is, it would read the wrong repo whenever the two differ, and AC-2's temp-repo test could only pass by patching process.cwd, which this repo's forks pool leaks across test files. So AC-2 adds an optional cwd parameter to detectDefaultBranch. That edits worktree-manager.ts, so this issue is blocked by #1406 (same file as #1405 and #1406) and runs in wave 3. When origin/HEAD is unset, detectDefaultBranch falls back to git remote set-head origin --auto, a network call; that path is reached only when no base is recorded (the spec phase), at most once per phase.

Import note: worktree-manager.ts already imports from phase-executor.ts, so importing detectDefaultBranch back is an ESM cycle. That's safe here, because both are function declarations called at run time, not at module load, and the repo has no cycle lint. Moving detectDefaultBranch out of worktree-manager.ts to break the cycle is allowed but not required; either way the edit lands after #1406.

May touch

src/lib/workflow/phase-executor.ts (env block, resolveBaseRef fallback, and their tests), src/lib/workflow/worktree-manager.ts (detectDefaultBranch signature only: an optional cwd), docs/reference/run-command.md, CHANGELOG.md.

Prior fixes / producers

This is the base-ref design cluster from the 2026-09-30 re-patch retrospective (#1236: "base ref / two worktree producers", 5 fixes then, more since). Prior fixes: #748, #878 (diff stats against origin/<base>, which added resolveDiffBase), #890 (remaining bare main...HEAD consumers via resolveDiffBase), #1069, #1234 (worktrees record their exact base ref; the exec guard and pre-PR rebase read it), #343 (CLI default-branch detection), #1386 (--base PR target).

Root cause, from the trace: every TypeScript consumer now goes through one resolver (resolveDiffBase / resolveBaseRef), but shell and skill consumers have no channel to it, so each one hard-codes a guess. #890 itself changed quality-checks.sh from main...HEAD to a literal origin/main...HEAD, which fixed staleness but left the default branch hard-coded. This issue is that channel: it exports the resolver's answer and adds no third producer. #1136 added SEQUANT_BASE_BRANCH as a skill input for in-place mode, where the user sets it. Nothing in sequant run sets it.

Not a regression: no commit broke a working path, so nothing needs reverting.

Non-Goals

Activity

  1. added
    bug
    graph-2026-10-freeze-b
    on Oct 16, 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 3)

    #1406 merged as 91fb052 and changed computeWorktreeBaseRef and its fetch in worktree-manager.ts, so build on main as it is now. 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, 3 directories)

    AC Quality Check

    • All three ACs are single-line, carry an Evidence: clause, and the issue has ## Non-Goals.
    • Not vacuous on base. grep -n SEQUANT_BASE_BRANCH docs/reference/run-command.md returns nothing on main (91fb052). The AC-1/AC-2 tests don't exist yet. The existing resolveBaseRef falls back to origin/main test (phase-executor.integration.test.ts:183) still passes with the literal restored, so it can't serve as AC-2's mutation target. Only the new master test can.
    • ⚠️ AC-3 wording. run-command.md has no list of "the other phase env variables" today. Its only env table (~:636) covers input settings (SEQUANT_QUALITY_LOOP, …). Exec should add a short "Phase agent environment" table (SEQUANT_WORKTREE, SEQUANT_ISSUE, SEQUANT_ORCHESTRATOR, SEQUANT_PHASE, SEQUANT_ISSUE_TYPE, SEQUANT_BASE_BRANCH), not one orphan row.

    Scope Assessment

    Non-Goals: present (no skill edits, #1461; no other worktree-manager.ts change; nothing under plugin/, .claude/skills/, templates/skills/).

    Metric Value Status
    Feature count 1 ✅
    AC items 3 ✅
    Directory spread 3 (src/lib/workflow, docs/reference, root CHANGELOG.md) ✅

    Verdict: ✅ SCOPE_OK


    Acceptance Criteria

    AC-1: phase env sets SEQUANT_BASE_BRANCH from resolveBaseRef(cwd), for every phase

    Verification: Unit Test (driver boundary mocked, as in full-qa.test.ts)
    Scenario: (a) Given a temp worktree with branch.<b>.sequantBaseRef=origin/master, passed as worktreePath → When executePhaseWithRetry(N, "exec", config) runs → Then agentConfig.env.SEQUANT_BASE_BRANCH === "master". (b) Given no worktree and a repo whose origin/HEAD is master → When the spec phase runs → Then the same holds.
    Assumptions: Strip only a leading origin/. A local chain-mode ref (feature/x, base-arm) passes through unchanged, which matches how the skills use it ([[ "$CURRENT" == "$BASE" ]], a bare branch name). Case (b) has a test-harness problem, see OQ-1.

    AC-2: resolveBaseRef fallback becomes origin/<detectDefaultBranch(cwd)>

    Verification: Integration Test (real temp git repo)
    Scenario: Given a temp repo + bare origin whose HEAD is master, and no sequantBaseRef or sequantBase → When resolveBaseRef(tmp) → Then "origin/master". No process.cwd patch.
    Assumptions: All three spawnSync calls in detectDefaultBranch take { cwd }. The default process.cwd() keeps the 6 existing call sites (worktree-manager.ts:994,1032,1105,1190,1390,1437,2496) unchanged. The existing no-origin tests (:183, :307) still resolve origin/main, because their bare origins are --initial-branch=main and set-head --auto against a local path works without network access.

    AC-3: run-command.md documents SEQUANT_BASE_BRANCH

    Verification: Manual Test (grep, plus reading the row)
    Scenario: Given the docs → When grep runs → Then a row says it's a bare branch name, set on every phase, taken from the worktree's recorded base or else the repo's default branch.


    Implementation Plan

    Phase 1: Resolver (worktree-manager.ts, signature only; phase-executor.ts)

    1. detectDefaultBranch(verbose = false, cwd: string = process.cwd()): thread cwd into its three spawnSync calls. Add it as a second parameter so existing detectDefaultBranch(verbose) callers don't change.
    2. resolveBaseRef(cwd): replace const fallback = "origin/main" with a lazy const fallback = () => \origin/${detectDefaultBranch(false, cwd)}`. Lazy so the recorded-base path (every worktree phase) never spawns the extra git calls or the set-head --autonetwork call. Update the doc comment's step 3. ImportdetectDefaultBranchfrom./worktree-manager.js`. The cycle is safe because it only runs at call time (issue's import note). Don't move the function.

    Phase 2: Env channel (phase-executor.ts ~:1697)

    1. After env.SEQUANT_PHASE = phase;, add env.SEQUANT_BASE_BRANCH = resolveBaseRef(cwd).replace(/^origin\//, "");. cwd is the existing local at :1636 (worktree or process.cwd()). Set it unconditionally, so it covers spec too.

    Phase 3: Tests + docs

    1. New src/lib/workflow/phase-executor.base-branch.integration.test.ts (the .integration. suffix avoids the 5s cap on real git). It holds AC-2's resolveBaseRef test plus AC-1 (a) and (b).
    2. docs/reference/run-command.md: add the phase-env table (AC-3). Add a CHANGELOG.md Unreleased → Fixed entry.
    3. Run only npx vitest run on the new file + phase-executor.integration.test.ts + worktree-manager.test.ts + full-qa.test.ts (runner note: no full npm test). Mutation-verify AC-1 (delete the assignment) and AC-2 (restore the literal). Record both as SEQUANT_MUTATION markers with no braces in the text, and failedTest starting with the file path.

    Design Review

    1. Where does it belong? In the phase-executor env block, the single place that builds a phase agent's env. The fallback belongs in resolveBaseRef, the single TS resolver.
    2. Simplest approach? Export the resolver's existing answer. Rejected alternatives: (a) thread RunOptions.base / BatchExecutionConfig.baseBranch into ExecutionConfig. That is a second producer racing the recorded ref, which is the tracker(graph): pre-release fixes before the next release after 2.18.0 (4 nodes) #1236 failure shape. (b) Detect the default branch inside each skill. That's fix(skills): origin/main is hard-coded in exec/qa/spec commands, and quality-checks.sh turns the git failure into zeros on master-default repos #1461's job, and it's frozen.
    3. Pattern / re-patch count: this is the base-ref cluster: fix(chain): --chain successors branch from main, not predecessor's committed work (up-front worktree provisioning) #748, fix(observability): phase diff stats compare against the local base ref while worktrees branch from origin/<base> — phantom filesModified + base tip logged as the phase commit #878, fix(observability): remaining bare main...HEAD consumers after #878 — qa-cache diff hash, worktree-manager summaries, aider file-context, non-qa skill gates #890, bug(qa): the qa phase rebases an already-pushed branch onto main, forcing a force-push to land it #1069, 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, docs(release): bring docs and README up to date for 2.20 #1343/fix(run): --base creates the worktree from the branch but opens the PR against the repo default branch #1386, fix(worktree): a non-default bare --base is cut from local <base> but counted against origin/<base> (#1380 follow-up, #1234/#1386 class) #1406, so ≥2, stop. This plan fixes the class for shell consumers: it gives them a channel to the one resolver instead of another guess, and removes the last hard-coded main on the TS resolver path. No new producer, so not a divergence.
    4. Senior reviewer challenge: "Why not use --base for spec?" Spec doesn't read the variable (only exec/qa/loop/testgen do), and worktree phases already get --base through the recorded ref. "Why the extra git spawns per phase?" With the lazy fallback, a recorded base costs 2–3 local git calls. Only the spec phase can reach set-head --auto.
    5. Producers/consumers (whole-repo grep): Producers: the new env assignment, and the user in in-place mode (docs/reference/checkout-modes.md:30, outside sequant run). Consumers: {.claude,plugin,templates}/skills/{exec,qa,loop,testgen}/SKILL.md (BASE="${SEQUANT_BASE_BRANCH:-main}"). resolveBaseRef consumers that get the new fallback: exec guard phase-executor.ts:1000, hasCommitsAheadOfBase worktree-manager.ts:421. That's one producer inside sequant run, so no parity test needed.

    Feature Quality Planning

    All standard checks pass. Notable items:


    Open Questions

    1. How does AC-1(b) pin the spec phase's cwd? For spec, cwd = process.cwd() (the sequant checkout, origin/HEAD = main). process.cwd spies leak across the forks pool. Recommendation: in the new test file, vi.mock("child_process", …importActual…) and override execFileSync/spawnSync only for the rev-parse --abbrev-ref HEAD, config --get branch.*, and symbolic-ref refs/remotes/origin/HEAD argv. Everything else passes through. Impact if wrong: a process.chdir version flakes other files in the same fork.
    2. Should an existing SEQUANT_BASE_BRANCH in process.env win? Recommendation: no, the resolver wins (AC-1 literal). Impact: a user exporting it before sequant run is silently overridden. That's documented, and it's the correct value anyway.
    3. Spec under --base feature/x gets the default branch. That's accepted, because no spec consumer exists. Recommendation: note it in the docs row and don't fix it here.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: bug, 3 ACs, one module plus docs. Skip testgen: the tests are specific git fixtures exec writes alongside the code. Not security-sensitive (execFileSync argv is unchanged).

    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:

    • Gate-test ACs AC-1 and AC-2 lack recorded mutation markers (QA ran the mutations and both were caught; record them)
    • Stale doc comment after the resolveBaseRef fallback change
    • Cross-file sibling hard-codes origin/main outside the resolver path (out of scope for fix(run): phase agents never get SEQUANT_BASE_BRANCH — skills fall back to main on master-default repos #1460)
    • The doc comment at phase-executor.ts:993 still says the base ref "defaults to origin/main". It is now the default branch of cwd.
    • src/lib/merge-check/combined-branch-test.ts:57 has const BASE_REF = "origin/main". This is the same hard-coded-default defect, but it is not on the resolver path, so it is outside this issue's scope.
  7. added 2 commits that reference 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