Repository navigation
Description
Epic: graph-2026-10-freeze Tier: judgment Execution: sequant_run
Summary
When a PR's worktree is more than 5 commits behind base and has no uncommitted or unpushed work, ensureWorktree "recreates fresh". In doing so it deletes the local branch and rebuilds it from base, ignoring origin/<branch>. A qa re-run then reviews base with none of the PR's commits.
Observed
admarble/thenewwayai #330 (PR #355), 2026-10-06, sequant 2.19.0 over MCP. On a qa re-run the log printed Worktree is 12 commits behind master — recreating fresh. QA then recorded "commit": null and filesModified: []. The worktree had none of the PR's commits; git log origin/master..HEAD was empty. The QA agent happened to notice and ran git show <head>:<file> itself, but nothing in sequant guards against this.
Mechanism (main @ 68446b02, src/lib/workflow/worktree-manager.ts)
ensureWorktree(~L842/901): behind by more than the threshold, no uncommitted changes,!hasUnpushedCommits, so it takes the "Safe to recreate" branch. Commits already pushed to the PR count as safe.removeStaleWorktree(~L355/378) runsgit branch -D <branch>.- Creation checks the branch with
show-ref --verify refs/heads/<branch>(~L992), which is local only. The branch was just deleted, so it falls through togit worktree add <path> -b <branch> <baseRef>(~L1055): a new branch from base.origin/<branch>is never consulted.
Impact
- QA reviews the wrong tree, and its verdict refers to code that isn't the PR.
- Any later commit on the recreated branch diverges from
origin/<branch>. Pushing it is non-fast-forward, or a force-push overwrites the PR. - It's latent in any repo where a PR's branch falls more than 5 commits behind base before a qa-only re-run, which is common on a busy default branch.
Suggested fix
When origin/<branch> exists, recreate from it (worktree add -B <branch> <path> origin/<branch>) and then merge or rebase base in. Never -b <branch> <base> while a remote branch exists. A cheap guard: after any recreate, assert that git rev-list --count <base>..HEAD is greater than 0 when the issue has an open PR, and refuse or warn otherwise.
Reported from a graph-run session; mechanism traced by a sequant-repo session and re-checked against main.
Acceptance Criteria
- AC-1: When
ensureWorktreerecreates a worktree andorigin/<branch>exists, the recreated branch starts fromorigin/<branch>, so HEAD contains every commitorigin/<branch>has. Evidence: a test with a temp repo whose PR branch is pushed and more than 5 commits behind base, assertinggit rev-list --count HEAD..origin/<branch>is 0 after recreate, mutation-verified by restoring the-b <branch> <baseRef>path. - AC-2: When
origin/<branch>does not exist, recreate behaves as today: a local-only branch whose HEAD is on base is recreated from the base, and a local-only branch with commits of its own is kept by the existing unpushed-commits check, not destroyed. (Clarified 2026-10-10: the spec's Open Question 1 thought ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380's guard changed the second case; a test againstmainshowsmainalready keeps it.) Evidence: a test with an unpushed local-only branch whose HEAD is on base, and a test that a local-only branch with its own commits is kept. - AC-3: A recreate that would leave HEAD with none of an existing
origin/<branch>'s commits does not proceed silently: it refuses, or prints a warning in the same style as the existing "commits behind" line (worktree-manager.ts:339) and keeps the old worktree. Evidence: a test that forces the case and asserts the warning or refusal text. - AC-4: The base the recreate path uses is the run's resolved base (
ResolvedRun.baseBranch), or the spec records why it differs (graph doc OQ-2). Evidence: the spec comment's answer to OQ-2, and a test if the value is threaded through.
Prior fixes / producers
- The recreate path dates from the refactor(run): split 3,459-line monolith into focused modules #318 split (2026-03-11,
14b81192); no later fix touched it. - Branch producers in this flow:
removeStaleWorktree(deletes the local branch), theshow-ref refs/heads/<branch>check (local only),git worktree add -b. The remote branch is never consulted. Deliberately local toensureWorktree; the base-resolution class is tracked in fix(run): --base creates the worktree from the branch but opens the PR against the repo default branch #1386.
May touch
src/lib/workflow/worktree-manager.ts (ensureWorktree, removeStaleWorktree, worktree creation), its tests. Serialize with #1386.
Non-Goals
- Changing the 5-commits-behind stale threshold.
- Rebasing or merging base into the recreated branch automatically (the suggested fix mentions it; out of scope unless the spec shows a test needs it).
- Any edit under
plugin/,.claude/skills/ortemplates/skills/(plugin freeze, I-1 of the graph doc).
Activity
docs(#1390): graph-2026-10-freeze founding doc, issue graph and as-fi…
admarble commentedon Oct 10, 2026
Runner context for the phase agents (graph-2026-10-freeze, wave 2). This applies to spec, exec and qa by name.
#1386 has merged (
9c017df6, PR #1394), the other change toworktree-manager.ts(graph doc D-5: serialized, disjoint functions).createPRnow takes the run's resolved base throughresolvePrBase. Line numbers in this issue's body predate that merge: findensureWorktree(now about L866),removeStaleWorktree(about L355) and theworktree add … -b <branch> <baseRef>call (about L1079) by name.Spec must answer OQ-2 before exec (graph doc
docs/internal/graph-2026-10-freeze/PROJECT-DISCUSSION.md, verbatim):Answer it from the code as it is on
mainnow: traceResolvedRun.baseBranch(run-orchestrator.ts) toensureWorktreesandensureWorktreesChainand intocomputeWorktreeBaseRef. Put the answer in the spec comment; AC-4 cites it. If the values can differ, say where. Thread the resolved base through rather than adding a second resolver (D-2).npm testin exec or qa. Up to three phases run in parallel in this wave, and a full-suite run (10–13 min) under that load crosses the 30-minute phase wall and kills the phase. Run the test files you touch or add plus the stack's affected-tests command, in the foreground, withtimeout, after a WIP commit. CI's requiredtestcheck runs the full suite on the PR, and the runner waits for it before any merge.plugin/,.claude/skills/ortemplates/skills/.CHANGELOG.md[Unreleased]: add your line. Conflicts with sibling PRs are expected, and the runner resolves them.failedTeststarts with the test file path.admarble commentedon Oct 10, 2026
Complexity: Standard (4 ACs, 2 directories)
AC Quality Check
Each AC is one line and has an
Evidence:clause. None of them are vacuous onmain(afc37bd0), because the tests they name don't exist yet. Two tensions:HEAD..origin/<branch>at 0, so the AC-1 test passes under the mutation. Fix: the AC-1 test must also assert that a recreate happened:existed: falseand the "recreating fresh" line. Compute the start ref once and use that same value in the guard and inworktree add(see the plan). Then the mutation fails the AC-1 test and nothing else.Scope Assessment
Non-Goals: present. They cover the threshold, the auto-merge of base, and the plugin freeze.
src/lib/workflow/,docs/adr/) + CHANGELOGVerdict: ✅ SCOPE_OK
OQ-2 answer (graph doc), from
main@afc37bd0The value is the same. The ref derived from it can differ.
ResolvedRun.baseBranch(run-orchestrator.ts:721) is never empty. It resolves asinit.baseBranch ?? --base ?? settings.run.defaultBase ?? detectDefaultBranch().ensureWorktrees(…, baseBranch, …)(run-orchestrator.ts:1309) passes it unchanged intoensureWorktree(worktree-manager.ts:1214). The recreate path then uses it in two different ways:detectedBase = baseBranch(:890).checkWorktreeFreshnessfetches it and counts commits behindorigin/<base>(countCommitsBehind,:244).baseRef = computeWorktreeBaseRef(baseBranch, detectDefaultBranch())(:1034). This returnsorigin/<default>for the default branch andorigin/xfororigin/x. For any other bare name (--base develop,run.defaultBase: "develop") it returns the local branchdevelop.origin/develop, but the recreate cuts from localdevelop, which can lag. Also, when the base isorigin/x,checkWorktreeFreshnessrunsgit fetch origin origin/x. That fetch fails silently, so the count uses an un-refreshed ref.ensureWorktreesChain(run-orchestrator.ts:1301) passeschainResume?.resumeBase ?? baseBranchfor the first link, then each link's predecessor branch. This is deliberately notResolvedRun.baseBranch. A predecessor that hasn't been pushed has noorigin/<pred>, socountCommitsBehindreturns null and links 2 and later are never recreated.Consequence for #1380 (AC-4): no new resolver is needed (D-2). The recreate path already gets
ResolvedRun.baseBranchunchanged. After the fix, whenorigin/<branch>exists the recreate starts from it, so the base only feeds two things: the stale count and the recordedbranch.<b>.sequantBaseRef. The recorded ref must staycomputeWorktreeBaseRef(baseBranch, …), notorigin/<branch>. Two readers depend on it: the exec guard (phase-executor.ts:884/922) andrebaseBeforePR. Note thatgit branch -DinremoveStaleWorktreedeletes that config section, and the record at:1092rewrites it. The local-vs-origin divergence for non-default bases belongs to the #1234/#1386 class and is not fixed here. See Open Question 3.Acceptance Criteria
AC-1: Recreate starts from
origin/<branch>when it existsVerification: Integration Test (temp bare remote + clone, same harness as
#1234inworktree-manager.test.ts:782)Scenario: Given feature branch F is pushed with
-u, its worktree exists, and the remote base is ≥6 commits ahead and fetched → WhenensureWorktreeruns → Then the output contains "recreating fresh",existed: false,git rev-list --count HEAD..origin/F= 0, and F's file is present.Assumptions: The branch was pushed with
push -u(createPR,:2340), so@{u}=origin/FandhasUnpushedCommitsis false. This is the observed path.AC-2: No
origin/<branch>means recreate from base, as todayVerification: Integration Test
Scenario: Given a local-only branch with no upstream and no commits of its own, ≥6 behind base → When
ensureWorktreeruns → Then it recreates, and HEAD =origin/main.Assumptions: See Open Question 1 for a local-only branch that has commits.
AC-3: A recreate that would drop
origin/<branch>'s commits does not proceedVerification: Integration Test
Scenario: Given F is pushed and its worktree is clean and ≥6 behind. Then
refs/remotes/origin/Fis deleted andorigin's URL is pointed at a missing path, so the branch fetch fails (an offline blip: sequant can't see the remote branch) → WhenensureWorktreeruns → Then it prints a yellow! Worktree is N commits behind <base> but … — keeping existing worktreeline, returnsexisted: true, and HEAD is unchanged.Assumptions:
countCommitsBehinduses the already-fetched localorigin/mainref, so the worktree still reads as stale.@{u}errors, sohasUnpushedCommitsis false and the code reaches the recreate path.AC-4: The base is
ResolvedRun.baseBranch; the recorded base ref is unchangedVerification: Integration Test, plus the OQ-2 answer above
Scenario: Given the AC-1 setup → When the recreate runs → Then
git config branch.F.sequantBaseRef=origin/main(the base), notorigin/F.Implementation Plan
Phase 1: Start ref + guard (
src/lib/workflow/worktree-manager.ts,ensureWorktreerecreate branch ~:922)removeStaleWorktree, rungit fetch origin <branch>(stdio pipe, 30 s timeout, failure tolerated). Then resolvestartRefonce:origin/<branch>ifrev-parse --verify --quiet refs/remotes/origin/<branch>succeeds, elsecomputeWorktreeBaseRef(baseBranch, detectDefaultBranch()). A small exported helper is fine (e.g.resolveRecreateStartRef,@internal).git merge-base --is-ancestor <existing HEAD> <startRef>fails, print the yellow warning (same style as:902/:914) plus anℹ️ Keeping existing worktreeline, and do not remove the worktree. This guard is the AC-3 check.startRefto the creation step (a local variable, e.g.recreateFrom).Phase 2: Creation (~
:1062-1082)recreateFromisorigin/<branch>, create the worktree withgit worktree add -B <branch> <path> origin/<branch>. Otherwise keep the existing-b <branch> <baseRef>andbranchExistspaths unchanged. Never use-b <branch> <baseRef>whilerecreateFromnames a remote branch.recordWorktreeBaseRef(worktreePath, branch, baseRef)recordingbaseRef(AC-4). Do not auto-merge base in (Non-Goal). The pre-PRrebaseBeforePRalready merges base into pushed branches (bug(qa): the qa phase rebases an already-pushed branch onto main, forcing a force-push to land it #1069).Phase 3: Tests + docs
describe("#1380 …")block inworktree-manager.test.tsthat reuses the 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 temp-repo/tsxscript harness. Cover AC-1, AC-2, AC-3 and AC-4. Advance base with a second clone that pushes 6 commits, then fetch. Run only this file plus affected tests, foreground, withtimeout(runner context: no fullnpm test).docs/adr/0011-…(see Design Review), a CHANGELOG[Unreleased]line, and mutation records for AC-1 (restore-b <branch> <baseRef>in the recreate path), AC-3 (delete the guard) and AC-4 (recordstartRefas the base). EachfailedTeststarts withsrc/lib/workflow/worktree-manager.test.ts.Design Review
ensureWorktree's recreate branch and its creation step.removeStaleWorktreestays a dumb remover.run-compat.tsre-exports it, so don't change its signature.startRefonce fromorigin/<branch>or the base, guard on containment, then create with-B … origin/<branch>. Rejected alternatives: (a) don't delete the local branch when it's pushed: this reuses a local ref that can lagorigin/<branch>(e.g. a runner's merge-main pushed from elsewhere). (b) skip the recreate wheneverorigin/<branch>exists: simpler, but it fails AC-1 as written and misses remote-only commits. Because the plan picks between designs, the exec PR must include an ADR (docs/adr/0011-*.md, shape perdocs/adr/README.md).518e6939,46580a05), split out in refactor(run): split 3,459-line monolith into focused modules #318 (14b81192).git log -S removeStaleWorktree/-S '@{u}..HEAD'show no fix since then: zero prior fix issues, so this is under the stop threshold. The base-ref class (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) is adjacent but separate. fix(run): --base creates the worktree from the branch but opens the PR against the repo default branch #1386 confirmed the base flow, and the issue records ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380 asdeliberately local, with the class tracked in fix(run): --base creates the worktree from the branch but opens the PR against the repo default branch #1386.origin/<branch>that the local branch lacks, andnode_modulesreinstall. Base gets merged in before the PR anyway.checkWorktreeFreshnessalready fetches base. One more fetch with a 30 s cap is consistent with that.removeStaleWorktree(branch -D),worktree add -b(:1079), the new-Bpath,createPRpush -u(:2340), andrebaseBeforePRmerge/rebase.recordWorktreeBaseRefat:916(reuse) and:1092(create). Both usecomputeWorktreeBaseRef, so there is one resolver.readRecordedBaseRef(exec guardphase-executor.ts:922,rebaseBeforePR),ensureWorktrees/ensureWorktreesChain(run-orchestrator.ts:1301/1309),run-compat.tsre-exports, and the MCP qa re-run, which is the observed path.Feature Quality Planning
All standard checks pass. Notable items:
git log @{u}..HEADexit 128, sohasUnpushedCommits= false (:351). A local-only branch with unpushed commits (local--baseor a chain link, exec failed before the PR) is then destroyed by today's recreate. The AC-3 containment guard covers this too. See Open Question 1.-Bfromorigin/<branch>is only reachable when the predecessor is pushed, since links 2 and later are otherwise never stale. Confirm that the existing chain rebase path (needsRebase) is not taken for a recreate fromorigin, or that it is harmless.uv pip install -q— useuv sync --frozenwith uv.lock, else requirements or skip #1217, fix(run): a watchdog-killed run writes no run log and half-deletes a clean worktree — worktree removal outruns the 5 s SIGKILL grace #1222, fix(pr): sequant run PRs double the conventional prefix in the title and ship a placeholder body #1223 and fix(run): sequant run has two PR producers — exec opens the PR itself and createPR returns it untouched, dropping the QA note, ready-gate report and summary #1247 touchworktree-manager.ts, but all of those issues are CLOSED. Those are stale worktrees, not a conflict. fix(run): --base creates the worktree from the branch but opens the PR against the repo default branch #1386 has merged.Open Questions
-bpath: no local branch, butorigin/<branch>exists. — Recommendation: out of scope. File a follow-up. Changing first-creation semantics affects abandoned remote branches.origin/xbut cut from localx(OQ-2 above). — Recommendation: follow-up in the 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 class. Not fixed here.Recommended Workflow
Phases: exec → qa
Quality Loop: disabled
Reasoning: This is a focused fix in one function. The tests are bespoke temp-repo integration scenarios in an existing harness, so testgen stubs add little. The graph runner drives exec and qa.
Label Review
Current: graph-2026-10-freeze
Recommended: +
bug,plannedReason: a destructive-path correctness bug.
Quality Loop: won't auto-enable (SCOPE_OK).
admarble commentedon Oct 10, 2026
QA Verdict: AC_MET_BUT_NOT_A_PLUS
AC coverage: 4/4 met
Gaps:
test(#1380): the recreated branch still tracks origin/<branch>
admarble commentedon Oct 10, 2026
Runner note (2026-10-10): AC-2 clarified, no behavior change. The spec's Open Question 1, repeated in QA's
pause_for_humanfinding, said #1380's containment guard newly keeps a local-only branch that has commits of its own. It doesn't. That branch is already kept onmainby the existinghasUnpushedCommitscheck ("Keeping existing worktree with WIP commits"). A new test (1380 AC-2 keeps a local-only branch that has commits of its own) passes againstmain'sworktree-manager.tsand with the guard disabled. AC-2's text now says so; the owner's earlier amendment rested on the same premise and is superseded by this clarification.test(#1380): a local-only branch with its own commits is kept (as on …
fix(#1380): recreate a stale worktree from origin/<branch>, never dro…
fix(#1405): a fresh machine starts a PR branch worktree from origin/<…
fix(#1406): a non-default bare --base is cut from local <base> but co…