Skip to content

fix(worktree): an existing branch for issue N is reused whatever the title — a gh blip or a retitle cuts feature/N-issue-n from main and QA reviews main #1487

Closed

Description

@admarble

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

Problem

sequant run finds an issue's existing worktree by a branch name rebuilt from the issue title. If the title differs from the one the branch was cut with, it misses the branch and creates a second one from the base:

  • ensureWorktree builds feature/${issueNumber}-${slugify(title)} (src/lib/workflow/worktree-manager.ts:987-988).
  • It looks that exact name up (findExistingWorktree(branch), :148, a substring match on git worktree list --porcelain).

Two triggers:

  1. A failed gh issue view. getIssueInfo (src/lib/workflow/batch-executor.ts:318-351) returns the title Issue #N for every gh failure except "Could not resolve to an issue" (offline, auth, rate limit, 5xx, unparseable JSON). This is the silent default fix(doctor/run): doctor and run --dry-run report success when nothing would work #1257 kept on purpose. The orchestrator passes it straight to ensureWorktree (run-orchestrator.ts:1161-1163, :1294).
  2. A retitled issue. Any title edit after the branch exists changes the slug.

Then:

  • The phase runs in a fresh worktree at the base.
  • batch-executor.ts:1164-1170 overwrites state.json's branch/worktree record with the new branch.
  • sequant worktree resolve N now reports WORKTREE_AMBIGUOUS.

Seen: 2026-10-16, a qa-only re-run of #1305 (MCP sequant_run, phases qa) whose gh issue view failed. It created feature/1305-issue-1305 from main next to the PR branch feature/1305-fix-qa-a-sequant-mutation-marker-whose-text-contai, and QA recorded commit: null.

Reproduced on main (0857484), in a temp clone using the #1380 harness shape:

  1. ensureWorktree(1305, "fix(qa): a SEQUANT_MUTATION marker whose text contains }"), then a commit pushed with -u.
  2. ensureWorktree(1305, "Issue #1305") returned { branch: "feature/1305-issue-1305", existed: false }, at the init commit. The PR worktree still held pr commit.

Everything else in src already treats the issue number as a branch's identity:

Only the code that creates worktrees keys on the title.

Acceptance Criteria

  • AC-1: When exactly one branch for issue N exists (a worktree of this repo, a local refs/heads/feature/N-*, or refs/remotes/origin/feature/N-*), ensureWorktree reuses that branch whatever title it is given, including the fallback Issue #N and a changed title, and creates no new branch. Evidence: npx vitest run src/lib/workflow/worktree-manager.test.ts -t "reuses the issue's branch" reports ≥ 4 passed, 0 failed. The temp-repo tests cover (a) an existing pushed worktree called again with title Issue #N, (b) the same with a different real title, and (c) a fresh clone where only origin/feature/N-<slug> exists, called with title Issue #N, and (d) issue 1 beside an existing feature/12-other branch, which must not be taken as issue 1's branch (the prefix match includes the dash). Each asserts the returned branch equals the original and git branch --list 'feature/N-issue-n' is empty. Mutation-verified by restoring the title-only lookup (findExistingWorktree(\feature/${issueNumber}-${slug}`)`).
  • AC-2: When two or more branches for issue N exist, ensureWorktree creates no new branch. It uses the one whose name equals the title-derived name if there is one. Otherwise it returns no worktree and prints every candidate branch, and the issue fails rather than running in another checkout. Evidence: npx vitest run src/lib/workflow/worktree-manager.test.ts -t "several branches for one issue" reports ≥ 1 passed, 0 failed: a temp-repo test with feature/N-a and feature/N-b and title Issue #N asserting no third branch, a null result and both names in the output; mutation-verified by making the multi-match case pick the first candidate.
  • AC-3: A non-dry run that could not read an issue's title (a gh failure other than not-found) prints a warning naming the issue and the gh error before worktree setup. A run that read the title prints no such warning. Evidence: npx vitest run src/lib/workflow/get-issue-info.test.ts -t "title unreadable warning" reports ≥ 2 passed, 0 failed: a test that stubs spawnSync to fail with HTTP 502 and asserts the warning names #N, plus a passing-gh case that asserts no warning; mutation-verified by removing the warning.
  • AC-4: With no branch for issue N anywhere, first creation is unchanged: feature/N-<slugify(title)> from the run's base, or from origin/<branch> on a fresh machine (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). Evidence: npx vitest run src/lib/workflow/worktree-manager.test.ts -t "1380|1405" reports 0 failed, with no test edited.

Prior fixes / producers

May touch

src/lib/workflow/worktree-manager.ts (ensureWorktree's existing-branch lookup, findExistingWorktree), src/lib/workflow/batch-executor.ts (getIssueInfo may report the gh error), src/lib/workflow/run-orchestrator.ts (the warning at the issue-info loop, :1158-1164), their tests (worktree-manager.test.ts, get-issue-info.test.ts), CHANGELOG.md.

Non-Goals

Activity

  1. added
    bug
    graph-2026-10-freeze-c
    on Oct 17, 2026
  2. admarble commented on Oct 17, 2026

    @admarble
    ContributorAuthor

    QA Review for Issue #1487

    AC Coverage

    AC Description Status Notes
    AC-1 One branch for issue N is reused whatever the title MET findIssueBranches + selectIssueBranch (worktree-manager.ts). Evidence command: 4 passed, 0 failed. Covers (a)–(d), including feature/12-other not taken as issue 1's branch.
    AC-2 Several branches: no new branch; use the title match, else no worktree, list candidates, and the issue fails rather than running in another checkout PARTIALLY_MET No-new-branch, title-match and candidate list work (evidence: 2 passed). But the null result does not fail the issue. ensureWorktrees skips a null entry, runIssueWithLogging gets worktree: undefined, and phase-executor.ts:1642-1643 sets cwd = process.cwd(). So in non-chain mode the issue's phases run in the main checkout, which is the failure #1487 describes (QA reviews main). Chain mode does stop ("Chain broken"). No test covers the "issue fails" half.
    AC-3 A non-dry run warns, naming #N and the gh error; a run that read the title does not MET issueTitleWarning is wired at run-orchestrator.ts:1164-1170. Evidence command: 4 passed. run-orchestrator.test.ts covers both the real run and the dry run.
    AC-4 First creation unchanged; no #1380/#1405 test edited MET -t "1380|1405": 12 passed, 0 failed. No removed lines in worktree-manager.test.ts.

    Coverage: 3/4 met

    CI Status (PR #1497)

    test is IN_PROGRESS. Gitleaks, canary, node-floor-guard, validate-skills, typecheck, validate-plugin and tautology-advisory pass.

    Quality Checks

    Check Status Notes
    Type safety / deleted tests ✅ Pass 0 any, 0 deleted tests
    Scope / size ✅ Pass 7 files, +357/−10, about 120 of them source. run-orchestrator.test.ts is the only file outside "May touch", and it is the sibling test.
    Security ✅ Pass The new spawnSync passes an args array with no shell

    Local run: the 3 changed test files ran 99/99 passed. Build: typecheck passes in CI.

    Code Review

    Strengths:

    • The issue number is now the branch's identity, which matches resolveIssueWorktree, merge-check and pr-status.
    • findExistingWorktree changed from substring to exact match, which also closes a latent bug where feature/1- matched feature/12-.
    • The warning goes through bracketedConsoleLog, so it also appears in parallel and MCP runs.

    Issues:

    • AC-2: an ambiguous issue is not failed. Non-chain runs execute it in the main checkout (see above). Fix: in run-orchestrator.ts, after provisioning with isolation on, give each issue that has no worktreeMap entry a failed IssueResult (with an abort reason) instead of running it. Add a test for that.

    Suggestions:

    • A stale origin/feature/N-* ref from a merged and deleted PR counts as a candidate until fetch --prune. A reopened issue would reuse it or become ambiguous. This is by design per the ACs, but the CHANGELOG could say so.

    Mutation Verification

    AC Gate Test? Marker Status
    AC-1 Yes — Missing
    AC-2 Yes — Missing
    AC-3 Yes — Missing
    AC-4 No — N/A

    Status: Missing. The PR body is the auto-generated stub and has no SEQUANT_MUTATION markers.

    Declared-Evidence Execution

    AC Evidence Executed? Result
    AC-1 -t "reuses the issue's branch" Yes ✅ 4 passed
    AC-2 -t "several branches for one issue" Yes ✅ 2 passed (does not test the "issue fails" half)
    AC-3 -t "title unreadable warning" Yes ✅ 4 passed
    AC-4 -t "1380|1405" Yes ✅ 12 passed

    Status: Complete

    Trust-Boundary Check

    Finding: Clean. The diff implements only product requirements, and no agent-directed instruction from external text was acted on.
    Status: Clean

    Behavior-Rule Survival Check

    Status: N/A. No AC triggers the behavior-rule heuristic, and the title-only lookup feature/${issueNumber}-${slug} survives only as the title-match fallback the ACs intend.

    CHANGELOG Verification

    Result: Met. An [Unreleased] → Fixed entry with (#1487) is present.

    Risk Assessment

    Adversarial Re-Read

    Findings: I traced the AC-2 null result end to end and found the main-checkout fallback described above. I ran the issue's repro shape: AC-1(a) is the "Issue #1305" fallback case, and it passes. Process state is clean: 0 commits behind main and no uncommitted work. Non-Goals are respected: no plugin or skill edits, no new-feature.sh edits, no retry of gh.
    Status: Severe Gap. AC-2's "issue fails" clause is marked unmet. It had no test, and I verified it from the code path.

    Follow-up Ledger

    Status: Resolved

    Verdict: AC_NOT_MET

    AC-2 is PARTIALLY_MET. An ambiguous issue returns no worktree, but the run does not fail it. In non-chain mode its phases run in the main checkout. The mutation markers for AC-1 to AC-3 are also missing, and CI test is still running.

    Next Steps

    1. In run-orchestrator.ts, when worktree isolation is on, fail each issue that has no worktreeMap entry instead of running it in process.cwd(). Add a test that two branches plus the title Issue #N give a failed issue and no phase run.
    2. Add SEQUANT_MUTATION markers for AC-1, AC-2 and AC-3 to exec's summary or the PR body.
    3. Re-run /qa 1487 after CI test reports.
  3. added
    needs-work
    on Oct 17, 2026
  4. admarble commented on Oct 17, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_NOT_MET

    AC coverage: 3/4 met

  5. added a commit that references this issue on Oct 17, 2026
  6. admarble commented on Oct 17, 2026

    @admarble
    ContributorAuthor

    QA Review for Issue #1487 (re-run at 0eec3b4)

    The previous QA, at 4a2a275, returned AC_NOT_MET for two reasons: the AC-2 "issue fails" clause was not met, and the mutation markers were missing. This re-run checks the merged main (#1469 / #1493) and commit 0eec3b4.

    AC Coverage

    AC Description Status Notes
    AC-1 One branch for issue N is reused whatever the title MET findIssueBranches + selectIssueBranch. Evidence -t "reuses the issue's branch": 4 passed, 0 failed. Covers (a)–(d).
    AC-2 Several branches: no new branch; the title match wins, otherwise null + candidates listed + the issue fails MET Evidence -t "several branches for one issue": 2 passed. Commit 0eec3b4 writes the candidate list into provisioning.failures, and the test asserts it. batch-executor.ts:1189 (worktreeRequired && !worktree, from #1469) fails the issue before any phase runs. run-orchestrator.worktree-failure.test.ts (3 passed) covers that half. Both orchestrator call sites (run-orchestrator.ts:1322,1331, chain and non-chain) pass { failures: worktreeFailures }.
    AC-3 A non-dry run warns, naming #N and the gh error; no warning on success MET Evidence -t "title unreadable warning": 4 passed. The dry-run and real-run wiring is covered in run-orchestrator.test.ts.
    AC-4 First creation unchanged; no #1380/#1405 test edited MET -t "1380|1405": 12 passed. The diff of worktree-manager.test.ts against main removes 0 lines.

    Coverage: 4/4

    CI Status (PR #1497)

    Check State
    test ⏳ IN_PROGRESS
    canary, typecheck, node-floor-guard, Gitleaks, validate-skills, validate-plugin, tautology-advisory ✅ SUCCESS

    test is a required check and has not finished, so the verdict below is gated on it.

    Quality Metrics

    Metric Value
    any / security patterns in added lines 0 / 0
    Deleted tests 0
    Files changed 7 (+373 / −10)
    tsc --noEmit ✅ exit 0
    Local tests (4 affected files) ✅ 102/102 passed
    Freeze (plugin/, .claude/skills/, templates/skills/) ✅ untouched

    The quality checks ran inline in this pass. No sub-agents were used.

    Code Review

    Strengths:

    • Branch identity is now the issue number, the same key resolveIssueWorktree, merge-check and pr-status already use.
    • findExistingWorktree matches exactly instead of by substring. This closes the latent bug where feature/1- matched feature/12-.
    • An ambiguous issue now fails with a reason that names the candidates, not a generic "no worktree".

    Suggestions (non-blocking):

    • An origin/feature/N-* ref left by a merged and deleted PR stays a candidate until fetch --prune. A reopened issue would reuse it. This matches the behavior before this change when the title was unchanged, so it is not a regression.
    • If findIssueBranches hits a git error it returns [] and falls back to the title-derived name. That is the old behavior, but it does so silently.

    Mutation Verification

    AC Marker Status
    AC-1 src/lib/workflow/worktree-manager.test.ts > … > reuses the issue's branch for the fallback title Issue #N Verified
    AC-2 src/lib/workflow/worktree-manager.test.ts > … > several branches for one issue… Verified
    AC-3 src/lib/workflow/get-issue-info.test.ts > title unreadable warning (#1487) > …HTTP 502 Verified
    AC-4 — (not a gate test) N/A

    Status: Verified. All three files named in the markers are in the diff.

    Declared-Evidence Execution

    All four Evidence commands ran in this pass and were green (4 / 2 / 4 / 12 passed). Status: Complete

    Trust-Boundary Check

    Finding: Clean. The diff implements only the product requirements. Status: Clean

    Behavior-Rule Survival Check

    Status: N/A. The title-derived name survives only as the tie-break and first-creation name, which the ACs intend.

    CHANGELOG Verification

    Result: Met. An [Unreleased] → Fixed entry with (#1487) is present.

    Risk Assessment

    • Likely failure mode: A repo where a feature/N-* branch exists that is not the issue's working branch, for example a hand-made side branch. That issue becomes ambiguous and fails loudly. This is the intended trade, and the message names the branches.
    • Not tested: No single test drives two branches through a full sequant run to a failed IssueResult. That outcome is covered by composing the failures-map assertion with fix(run): a failed git worktree add must fail the issue, not run its phases in the main checkout (two CLIs race on .git/config) #1469's run-level test.
    • Sibling sites considered: Class searched: code that creates branch names from the title. templates/scripts/new-feature.sh (node C, a Non-Goal) and the frozen /exec in-place prose are still title-keyed. All consumers already key on the number.
    • Sibling-line audit: ensureWorktreesChain and ensureWorktrees both pass the failures map, and the getGitRoot null path already records its failure.

    Adversarial Re-Read

    Findings: I ran the issue's repro shape (an existing pushed branch, then Issue #N), which is AC-1(a), and it passes. I traced the AC-2 null result through batch-executor.ts:1189 to the failed issue. Process state: 0 commits behind main, clean tree, and the merge of main brings in #1469. The getIssueInfo error paths cover non-zero exit (stderr), spawn error (ENOENT) and a JSON parse throw (catch). Non-Goals are respected: no retry or caching of gh, no rename of stray branches, no new-feature.sh change, no start-ref change. No new gaps. Status: Clean

    Follow-up Ledger

    • Stale origin refs after a merged PR are dropped: the ACs define origin refs as candidates, and the behavior is unchanged for an unchanged title.
    • The new-feature.sh title lookup is dropped: it is node C of graph freeze-c (Non-Goal).

    Status: Resolved

    Verdict: NEEDS_VERIFICATION

    All four ACs are met. Every Evidence command passed locally, the mutation records are verified, and both prior blockers are fixed. The required CI check test is still IN_PROGRESS, so this cannot be READY_FOR_MERGE yet. If test goes green, the code review supports READY_FOR_MERGE.

    Next Steps

    1. Wait for the test check on PR fix(#1487): an existing branch for issue N is reused whatever the title — a gh blip or a retitle cuts feature/N-issue-n from main and QA reviews main #1497, then re-run /qa 1487, or merge on green.
    2. Optional: log a warning when findIssueBranches' git call fails.
  7. added
    needs-verification
    and removed
    needs-work
    on Oct 17, 2026
  8. admarble commented on Oct 17, 2026

    @admarble
    ContributorAuthor

    QA Verdict: NEEDS_VERIFICATION

    AC coverage: 4/4 met

  9. admarble commented on Oct 17, 2026

    @admarble
    ContributorAuthor

    Runner verification of QA's NEEDS_VERIFICATION, on 081f898 (0eec3b4 merged with main). QA marked 4/4 met and named nothing pending. The runner ran every Evidence command exactly as written:

    The AC-1 and AC-2 tests run real git in temp repos, with no mocks. The three mutations (AC-1 to AC-3) each failed the test their AC names; the records are in PR #1497. AC-2's 'the issue fails' clause comes from #1469's guard (merged as 73d0078). This branch also records the candidate branches as the failure reason.

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-cneeds-verification

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions