Repository navigation
Description
Epic: graph-2026-10-freeze-b Tier: judgment Execution: sequant_run
Follow-up from #1380 (PR #1403; spec Open Question 2 and a QA document finding).
Problem
#1380 makes a stale-worktree recreate start from origin/<branch>. Two neighbouring paths still lose or repeat work:
- Fresh machine, first creation. With no local branch but an existing
origin/<branch>(a PR opened from another machine, or a cloud session),ensureWorktreestill checks the branch with the local-onlyshow-ref refs/heads/<branch>(worktree-manager.ts:1078). It falls through togit worktree add <path> -b <branch> <baseRef>, a new branch cut from the base. A qa re-run there reviews the base, and a later push diverges from the PR. That's ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380's bug, on the first-creation path. - A recreated worktree stays stale. Recreating from
origin/<branch>keeps the PR's commits, but the branch is still more than 5 commits behind the base. So every laterensureWorktreecall recreates it again (delete, re-add, dependency reinstall) until the pre-PR rebase or a merge of the base lands.
Design constraint (graph doc §6, OQ-2)
AC-3's "abandoned branch" check must not add a network call to every ensureWorktree. #1322's root cause was real git worktree list + gh calls per worktree, which made run time scale with load. The spec answers where the check runs (first-creation and recreate paths only) and whether a local git check is enough (for example, origin/<branch> already contained in origin/<base>).
Acceptance Criteria
- AC-1: With no local
<branch>and an existingorigin/<branch>,ensureWorktreecreates the worktree fromorigin/<branch>with upstream tracking, so HEAD contains every commit oforigin/<branch>. Evidence: a temp-repo test inworktree-manager.test.ts(the ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380 harness), mutation-verified by restoring the-b <branch> <baseRef>path. - AC-2: A worktree recreated from
origin/<branch>is not recreated again by the nextensureWorktreecall when nothing changed onorigin/<branch>. Either the base is merged in at recreate time, or the staleness check skips a branch whose HEAD equalsorigin/<branch>. Evidence: a test callingensureWorktreetwice, asserting the second call reuses the worktree. - AC-3: A remote branch already merged into the base is not resurrected as the new work's base, and the check adds no
ghcall on the reuse path. Evidence: a test covering a branch already merged intoorigin/<base>, plus a test asserting the reuse path makes noghcall.
Prior fixes / producers
git log -S ensureWorktree -- src/lib/workflow/worktree-manager.ts: fix(worktree): frozen installs so provisioning cannot dirty the lockfile #816 (frozen installs), chore(types): reuseWorktrees is a declared-but-unconsumed RunOptions field #810 (dead options), refactor(run): split 3,459-line monolith into focused modules #318 (split).computeWorktreeBaseRef: ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380 (PR feat(#1380): ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1403), 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 (PR fix(#1234): sequant run worktrees record their exact base ref; the exec guard and pre-PR rebase read it #1242). Two fixes on the recreate/base path in the last three weeks.- Producers of a worktree's starting commit:
ensureWorktree's first-creation path and its recreate path (ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380). The ACs state the outcome for both paths ("HEAD contains every commit oforigin/<branch>"), not one call site.
May touch
src/lib/workflow/worktree-manager.ts (ensureWorktree, branch-existence check), its tests, CHANGELOG.md. Serialize before #1406 (graph doc D-5).
Non-Goals
- The stale threshold value.
- A remote branch whose PR was closed unmerged. Detecting it needs
gh, which the design constraint keeps off the hot path; if it matters, file it separately. computeWorktreeBaseRefandcountCommitsBehind(fix(worktree): a non-default bare --base is cut from local <base> but counted against origin/<base> (#1380 follow-up, #1234/#1386 class) #1406).- Any edit under
plugin/,.claude/skills/ortemplates/skills/(plugin freeze).
Related: #1380, ADR-0013.
Activity
admarble commentedon Oct 16, 2026
Runner context (graph-2026-10-freeze-b, tracker #1452), for the exec and qa phases:
npm test/npx vitest runwith no file filter). Six phase agents run at once and the MCP phase wall is 30 minutes (graph doc I-5). Full-suite evidence comes from CI. Run only the targeted test files, in the foreground, undertimeout 600, after a WIP commit. Do not userun_in_backgroundor Monitor.plugin/,.claude/skills/ortemplates/skills/(plugin freeze, I-1).docs/internal/graph-2026-10-freeze-b/PROJECT-DISCUSSION.md§6 (fix(worktree): first creation on a fresh machine ignores origin/<branch>, and a recreated PR worktree is recreated again on every run (#1380 follow-up) #1405 row) and §9 OQ-2 (where AC-3's abandoned-branch check runs, and whether it needsgh). Spec should answer OQ-2 explicitly. The test: state.test.ts idempotency and state-utils discoverUntrackedWorktrees cases fail under full-suite load, pass alone #1322 lesson is that per-worktreeghcalls on the hot path made run time scale with load.worktree-manager.ts, D-5/D-9). Keep this change toensureWorktree's paths.admarble commentedon Oct 16, 2026
Complexity: Standard (3 ACs, 2 directories:
src/lib/workflow/,docs/adr/)AC Quality Check
All 3 ACs parse with declared
Evidence:. Lint is clean. Checked against base:maintoday. First creation cuts-b <branch> origin/main. A secondensureWorktreeafter a ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380 recreate recreates again. Neither evidence passes vacuously.main. The reuse path makes noghcall today. Keep it as a regression guard, and mutation-verify it by injecting aghspawn on the reuse path.origin/<branch>an ancestor oforigin/<base>. A test that uses onlygit mergewould pass a check that misses every real merge on this repo. Recommend amending the AC-3 line to "merged (merge commit or squash) into the base", and testing both.Scope Assessment
Non-Goals: present (threshold value; PRs closed unmerged;
computeWorktreeBaseRef/countCommitsBehind→ #1406; plugin freeze).Verdict: ✅ SCOPE_OK
In-flight: no open PR touches
worktree-manager.ts. The 5 local worktrees with diffs in it (#1199, #1217, #1222, #1223, #1247) are all for closed issues. Afeature/1405-…worktree already exists at8de858a13with no commits; exec may reuse it, but it is behindmain. #1406 waits for this to merge (D-5).Acceptance Criteria
AC-1: First creation starts from
origin/<branch>Verification: Integration Test (temp-repo, #1380 harness)
Scenario: Given clone A pushed
feature/20-…with a commit, and clone B has no local branch → When B callsensureWorktree(20)→ ThenHEAD..origin/<branch>is empty, the PR commit is in the log, andbranch.<b>.remote/mergetrackorigin.Assumptions:
sequantBaseRefstill records the run's base, notorigin/<branch>(same as #1380 AC-4).AC-2: A recreated PR worktree is not recreated again
Verification: Integration Test
Scenario: Given a pushed branch and main advanced by 6 → When
ensureruns twice → Then the 1st call printsrecreating fresh. The 2nd call returnsexisted: true, does not printrecreating fresh, and has the same path and HEAD.Assumptions: "Nothing changed on
origin/<branch>" means HEAD ==origin/<branch>after the fetch.AC-3: A merged remote branch is not resurrected; no
ghcall on reuseVerification: Integration Test
Scenario: (a) Given
origin/<branch>was squash-merged intoorigin/main, with no local branch → Whenensureruns → Then HEAD ==origin/main. (b) The same with a merge-commit merge. (c) Given a stale localrefs/remotes/origin/<branch>whose remote branch was deleted after the merge → Then the start isorigin/main. (d) Given a fakeghfirst onPATHthat writes a marker file → When the reuse path runs → Then no marker file exists.Assumptions: Answer to OQ-2 below: the check is local
gitonly, so noghcall is needed.Implementation Plan
Phase 1: One start-ref resolver (the class fix)
Generalize
resolveRecreateStartRef(worktree-manager.ts:844) intoresolveBranchStartRef(cwd, branch, baseRef). It returnsorigin/<branch>only when all three hold:git fetch origin <branch>did not reportcouldn't find remote ref. A branch that was deleted after its merge leaves a stale tracking ref, so ignore that ref. Any other fetch failure (offline) still falls back to the ref that is present, as in ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380.baseRef. Merged meansmerge-base --is-ancestor origin/<branch> <baseRef>, orgit merge-tree --write-tree <baseRef> origin/<branch>gives a tree equal to<baseRef>^{tree}. The second test catches squash merges (git ≥ 2.38; if merge-tree errors, use only the ancestor result).Otherwise it returns
baseRef.Recreate path (
:959): call the new resolver. The containment guard is unchanged.Phase 2: First-creation path (AC-1, AC-3)
:1076, whenrefs/heads/<branch>is absent and!chainMode, callresolveBranchStartRef(gitRoot, branch, baseRef). If it returnsorigin/<branch>, use the existingrecreateFrombranch (worktree add -B <branch> <path> origin/<branch>,:1130).-Bfrom a remote-tracking ref sets upstream throughbranch.autoSetupMerge, and the ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380 AC-1 test already asserts that. Chain mode keeps cutting from the predecessor.Phase 3: Stop the repeat recreate (AC-2)
:955), after the start ref resolves: ifstartRef === origin/<branch>andrev-parse HEAD==rev-parse startRef, keep the worktree with a gray verbose note ("already at origin/; recreate would not change it"). The pre-PRrebaseBeforePR(bug(qa): the qa phase rebases an already-pushed branch onto main, forcing a force-push to land it #1069) still brings in the base.Phase 4: Tests, ADR, CHANGELOG
#1405describe block next to the ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380 one. Reuseensure/advanceMain, and add a second clone and aPATHghshim. Run only this file, undertimeout 600, after a WIP commit (runner comment).Fixedentry.Design Review
ensureWorktreeplus the resolver it already owns. No new module.origin/<branch>skip over merging the base at recreate time. A merge at provisioning time writes an unasked-for commit that must be pushed, adds conflict handling to provisioning, and duplicatesrebaseBeforePR. Rejected: removingbranch -D(ADR-0013 Option B).gh pr view --json state?" test: state.test.ts idempotency and state-utils discoverUntrackedWorktrees cases fail under full-suite load, pass alone #1322: per-worktreeghcalls on the hot path. The local check covers merged branches. Branches closed unmerged are a Non-Goal.:1147) and the recreate path (:1130), which collapse onto one resolver.countCommitsBehindhas consumers inrun-orchestrator.ts:1351andcheckWorktreeFreshness(run-compat.ts); fix(worktree): a non-default bare --base is cut from local <base> but counted against origin/<base> (#1380 follow-up, #1234/#1386 class) #1406 owns them and they are not touched here.recordWorktreeBaseRefis unchanged.OQ-2 answer: the check runs only on the first-creation and recreate paths, never on reuse. It is local
git: onefetch origin <branch>, which the recreate path already does and which is new on first creation, plus a localmerge-tree. It needs nogh.Feature Quality Planning
fetch origin <branch>, with the same 30 s cap as ensureWorktree "recreating fresh" rebuilds a PR branch from base and drops its commits; qa re-run reviews the wrong tree #1380. For a brand-new issue it fails fast withcouldn't find remote ref.resolveBranchStartRef), so no parity test is needed.Open Questions
origin/<branch>win over the chain predecessor? Recommendation: no; the predecessor keeps winning. Impact if wrong: a chained fresh-machine run cuts from the predecessor, as it does today.Recommended Workflow
Phases: exec → qa
Quality Loop: disabled
Reasoning: This is a bug fix in one file with integration-style temp-repo tests, so testgen is skipped. The tests need the #1380 harness, not stubs.
Label Review
Current: bug, graph-2026-10-freeze-b. Recommended: no change, plus
planned.admarble commentedon Oct 16, 2026
QA Verdict: AC_MET_BUT_NOT_A_PLUS
AC coverage: 3/3 met
Gaps:
"couldn't find remote ref"). On a git with a non-English locale, a stale tracking ref for a branch that was deleted unmerged would be used again. SettingLC_ALL=Con that onespawnSyncwould close this.advanceBranchRemotelyfirst. This is correct, because the old shape (HEAD == origin) now takes the skip. It does mean those tests no longer cover the case where the remote is exactly at HEAD; the fix(worktree): first creation on a fresh machine ignores origin/<branch>, and a recreated PR worktree is recreated again on every run (#1380 follow-up) #1405 AC-2 test covers it instead.origin/<branch>now runsgit fetchplusmerge-treeon every run before the skip. This is accepted by OQ-2, which allows the check on the recreate path, and it makes noghcall.fix(#1405): detect a deleted remote branch under any locale
docs(#1409): renumber the install-scope ADR to 0015
fix(#1405): a fresh machine starts a PR branch worktree from origin/<…
fix(#1409): record the install scope so a skills-only install stops d…