Skip to content

fix(state): reconcile marks an open issue merged when any squash commit title mentions (#N) — isIssueMergedIntoMain's commit-mention fallback confuses PR and issue numbers #1044

Closed

Description

@admarble

Tier: mechanical Found: 2026-09-11 during the wave-2 graph run (#1030, PR #1042 open)

What happened

sequant run 1030 --phases qa printed State reconciled: #1030 → merged and sequant_status then reported status: "merged" with nextAction: "sequant status --cleanup" — while PR #1042 was open and the branch feature/1030-… was not an ancestor of main. The run only continued because force was set; a cleanup at that point would have removed a live worktree.

Root cause

isIssueMergedIntoMain (src/lib/workflow/pr-status.ts) — Method 2 "merge commits mentioning the issue" runs

git log main --oneline -20 --grep 'Merge #1030' --grep 'Merge.*#1030' --grep '(#1030)'

The third pattern was meant for squash-merge subjects, where the parenthesised number is the PR number, not the issue. Applied to an issue number it matches any of the last 20 commits whose title mentions the issue — here the merged docs PR #1031, docs(graph): wave-2 plan — node 09 (#1030), …. Reproduced: git log main --oneline -E --grep='\(#1030\)' → 2 hits, both other PRs; git merge-base --is-ancestor feature/1030-… main → no.

Method 1 (feature branch merged) was correct; the fallback overrode it. The state guard (#592) and status --cleanup both trust this answer.

Proposed fix

  • Drop the (#N) pattern from Method 2 (keep Merge #N / Merge pull request #<pr> forms only), or better: when the issue has a linked PR, trust checkPRMergeStatus alone; when it has none, require Method 1 (branch ancestry) and never infer "merged" from a commit mentioning the number.
  • Regression test: a repo whose main has a squash commit titled docs: … (#N) for an unrelated PR and an open, unmerged feature/N-… branch → isIssueMergedIntoMain(N) is false.
  • Never emit nextAction: status --cleanup for an issue whose branch is not an ancestor of the base.

Acceptance criteria

  • AC-1: isIssueMergedIntoMain(N) returns false when main contains a commit whose subject mentions (#N) but no branch feature/N-* is merged and no linked PR is merged — Verify: npx vitest run src/lib/workflow/pr-status.test.ts -t "mention" → passes on a temp repo built for the case. Mutation: restore the (#N) grep → fails; restore.
  • AC-2: the existing true-positive cases (Merge #N merge commit; merged feature branch; merged linked PR) still return true — Verify: npx vitest run src/lib/workflow/pr-status.test.ts → green.
  • AC-3: reconcileStateAtStartup leaves an in_progress issue with an open PR untouched in the scenario above — Verify: npx vitest run src/lib/workflow/state-cleanup.test.ts -t "open PR".
  • AC-4: suite green — Verify: npm run build && npm test.

Related: #616 (a different reconcile false positive — empty orphan branches), #592 (the pre-flight guard that consumes this answer).

Activity

  1. added
    bug
    on Sep 11, 2026
  2. admarble commented on Sep 11, 2026

    @admarble
    ContributorAuthor

    Runner context — launch (2026-09-11). Base: main at ac9afc3 (wave 1 + wave 2 merged). Stop-at-PR: open the PR, do not merge. Gate tests need Mutation-verified: records in the PR body with failedTest as <test file path> > <test name>. Foreground only: run every check in the foreground with a timeout (timeout 600 npm test 2>&1 | tail -80), once — the pre-tool hook blocks Monitor and run_in_background under the orchestrator and nothing will wake you; the phase ends when your turn ends. Commit a WIP within 5 minutes and every ~10 minutes; every phase has a hard 30-minute wall. CHANGELOG.md [Unreleased]: one bullet under the matching existing ### heading.

  3. admarble commented on Sep 11, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (4 ACs, 2 directories)

    AC Quality Check

    All 4 ACs pass lint (lintAcceptanceCriteria) — each has a concrete verify command and, for AC-1, an explicit mutation-verification instruction. No vague/unmeasurable/incomplete/open-ended flags.


    Scope Assessment

    Non-Goals: Not present in the issue body — heuristic warns "Non-Goals section not found."

    Metric Value Status
    Feature count 2 (keyword clusters: "ui", "test") ⚠️
    AC items 4 ✅
    Directory spread 2 (src, test files) ✅

    Verdict: ⚠️ SCOPE_WARNING (raw heuristic) → overriding to SCOPE_OK. The "ui"/"test" 2-feature split is a keyword-clustering false positive — every AC is one root-cause fix (isIssueMergedIntoMain's Method 2 grep) plus its regression coverage, not two features. Not auto-enabling the quality loop on this basis.


    Acceptance Criteria

    AC-1: isIssueMergedIntoMain(N) returns false on a commit that merely mentions (#N)

    Verification: Integration Test (real git, temp repo)
    Scenario: Given a temp repo where main has a squash commit titled docs: ... (#1030) (an unrelated PR that mentions issue 1030 in prose) and an unmerged feature/1030-* branch → When isIssueMergedIntoMain(1030) is called → Then it returns false.
    Assumptions: Real git temp repo required, not mocked spawnSync — the bug is in what real git log --grep matches (per runner's launch comment).

    AC-2: Existing true-positive detection paths still return true

    Verification: Integration Test (real git, temp repo)
    Scenario: Three sub-cases, each in its own temp-repo fixture: (a) a merge commit titled Merge #N ... → true; (b) an actually-merged feature/N-* branch (tip is a non-first parent of a merge commit on main) → true via isBranchMergedIntoMain; (c) a linked PR reported MERGED by checkPRMergeStatus (mocked GitHubProvider) → true.
    Assumptions: (c) exercises checkPRMergeStatus, not isIssueMergedIntoMain — both belong in the new pr-status.test.ts.

    AC-3: reconcileStateAtStartup leaves an in_progress issue with an open PR untouched

    Verification: Integration Test
    Scenario: Given state {status: "in_progress", pr: {number: 1042}}, GitHubProvider.getPRMergeStatusSync mocked to return "OPEN", and a real temp git repo reproducing the AC-1 false-positive commit shape for issue 1030 → When reconcileStateAtStartup() runs → Then issue stays in stillPending, status remains in_progress, resolvedAt stays unset.
    Assumptions: state-cleanup.test.ts mocks spawnSync globally in beforeEach for isolation. This test needs to opt out and delegate to real spawnSync scoped to itself, on a real temp repo it seeds — see Open Questions.

    AC-4: Full suite green

    Verification: Integration Test / CI gate
    Scenario: Given the AC-1–3 changes → When npm run build && npm test runs → Then it exits 0.
    Assumptions: None beyond the new tests themselves passing.


    Implementation Plan

    Phase 1: Fix isIssueMergedIntoMain (src/lib/workflow/pr-status.ts:140-168)

    1. Remove the `(#${issueNumber})` --grep pattern from Method 2's git log call (pr-status.ts:155). Keep `Merge #${issueNumber}` and `Merge.*#${issueNumber}` — both anchor on the word "Merge", so they don't match a squash-commit title that merely references the issue number in prose.
    2. Update the function's JSDoc (pr-status.ts:102-112) to state Method 2 only matches actual merge-commit subjects, not any commit mentioning #N.

    Phase 2: New pr-status.test.ts (AC-1, AC-2)

    1. Create src/lib/workflow/pr-status.test.ts (does not exist today — this file currently has zero test coverage).
    2. Use a real temp git repo per test (fs.mkdtempSync + git init + git -C <dir> commit --allow-empty -m "..." via execSync/spawnSync, chdir into it for the duration of the call since both functions default baseBranch: "main" and use spawnSync with the process cwd).
    3. AC-1 case: build the exact repro shape (squash commit docs: ... (#1030) unrelated to issue 1030 as an ordinary commit — no real merge needed since it just needs to appear in git log --grep results; unmerged feature/1030-x branch created but never merged).
    4. AC-2 cases: (a) real Merge #N commit subject, (b) real merge commit recording an actually-merged branch tip as a non-first parent (matches isBranchMergedIntoMain's docstring definition), (c) mock GitHubProvider per the existing state-cleanup.test.ts pattern (lines 39-47) to test checkPRMergeStatus returns true-equivalent ("MERGED").

    Phase 3: state-cleanup.test.ts — add AC-3 case

    1. Add one test literally named to satisfy -t "open PR" (e.g. "AC-3 (#1044): leaves in_progress untouched when linked PR is open and main has an unrelated squash commit mentioning the issue number").
    2. Within that test only, restore real spawnSync (see AC-3 Assumptions) and seed the temp repo with the AC-1 repro shape; set mockPRStatus = "OPEN".
    3. Assert result.stillPending includes the issue, result.advanced does not, and persisted status/resolvedAt are unchanged.

    Phase 4: Docs + changelog

    1. CHANGELOG.md [Unreleased] → one bullet under the existing ### Fixed heading (line 9) describing the fix and citing fix(state): reconcile marks an open issue merged when any squash commit title mentions (#N) — isIssueMergedIntoMain's commit-mention fallback confuses PR and issue numbers #1044.

    Design Review

    1. Where does this logic belong? pr-status.ts — it already owns both merge-detection strategies; the fix is localized to Method 2's grep patterns, no new module needed.
    2. What's the simplest correct approach? Drop the one over-broad grep pattern. The issue's own "better" alternative (always trust checkPRMergeStatus alone when a linked PR exists, never fall back to Method 2) is a larger behavioral change to reconcileStateAtStartup's fallback semantics — not required by any AC, and risks regressing the #606/#837/#972 escalation paths that intentionally use Method 2 as a fallback when checkPRMergeStatus is unreachable (null). Recommend the minimal fix; see Open Questions.
    3. What existing pattern does this follow? Same shape as the two surviving Method 2 patterns and Method 1 — anchor matches to actual merge semantics (Merge #N, a real non-first-parent merge relationship), never to "any commit that mentions N."
    4. What would a senior reviewer challenge? "Why not require Method 1 or a merged linked PR, full stop, and drop Method 2 entirely?" — because Method 2's two remaining patterns still catch real merge commits made outside this repo's own branch-naming convention (e.g. a manually-authored Merge #1030 commit with no feature/1030-* branch ever pushed). Dropping only the provably-wrong pattern is the narrower, AC-justified change.

    Feature Quality Planning

    • pr-status.ts has zero existing test coverage — this PR introduces pr-status.test.ts, so AC-1/AC-2 tests double as first regression guard for isBranchMergedIntoMain/checkPRMergeStatus; worth thoroughness over minimum repro.
    • Real-git vs mocked-spawnSync split. Keep the real-git override local to the one AC-3 test (scoped mockImplementation, restored after) so it doesn't weaken isolation for the file's other ~15 tests.

    Open Questions

    1. Should reconcileStateAtStartup also stop falling back to Method 2 whenever checkPRMergeStatus gives a definitive non-merged answer, not just when it's null? Recommendation: no — none of the 4 ACs require it, and today's fallback-on-anything-but-"MERGED" is intentional for the gh-unreachable case (fix(run): pre-flight guard misses in_progress issues already merged on GitHub (gap in #305/#423) #592/fix(run): pre-flight guard misses waiting_for_qa_gate issues already merged on GitHub (sibling of #592) #606/fix(chain): --chain --ready-gate resume re-executes already-gated links (COMPLETED_STATUSES misses waiting_for_human_merge/blocked) #837, state-cleanup.ts:329-348). Impact if wrong: a future naming convention could reintroduce a similar false positive via the two surviving patterns — a new bug report, not a regression of this fix.
    2. Shared real-git test helper? Recommendation: write inline in pr-status.test.ts first; extract only if state-cleanup.test.ts's AC-3 version turns out identical enough to dedupe.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: Single-root-cause bug fix, bug label only, SCOPE_OK after override, no UI surface (testgen skipped per bug-only label rule).


    Label Review

    Current: bug
    Recommended: bug, planned
    Reason: Standard bug-fix flow, no complexity/UI/security signals warranting additional labels.
    Quality Loop: Won't auto-enable — SCOPE_WARNING overridden to OK (keyword-clustering false positive, see Scope Assessment).

    → gh issue edit 1044 --add-label planned

  4. added
    planned
    on Sep 11, 2026
  5. admarble commented on Sep 11, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 0/4 met

    Gaps:

    • The retained Merge.*#N pattern still confuses PR numbers with issue numbers via numeric-prefix matching, so an open issue whose number is a prefix of a merge commit's PR number is still reported merged and can still be advanced to merged by state-cleanup.ts:343-347 despite an explicitly OPEN PR.
    • No regression test covers the numeric-prefix collision in the surviving Merge.*#N pattern.
    • checkPRMergeStatus OPEN and null branches lack direct coverage in the new test file.
    • Three tests fail locally on darwin at both HEAD and the base commit while passing in CI — pre-existing and unrelated to this diff, but AC-4's literal 'suite green' does not hold in the local environment.
    • The new real-git tests run under the unit project's 5s timeout with file parallelism enabled, a documented flake profile for subprocess-heavy tests.
    • The surviving Merge.*#${issueNumber} pattern is unanchored and still produces the same class of false positive, reproducible on today's main. pr-status.ts:157-159 keeps --grep "Merge.*#${issueNumber}" with no trailing word boundary, so an issue number that is a numeric prefix of a #N in a merge-commit subject matches. Verified against the real repository:

    Suggestions:

    • Follow-up (or a maintainer's call to fold in — it is a one-line change to the same three lines): switch Method 2 to -E with an end-anchored number, e.g. --grep "Merge.*#${issueNumber}([^0-9]|$)", and add a regression case for Merge pull request #1043 vs isIssueMergedIntoMain(104).
    • pr-status.test.ts AC-2c covers only checkPRMergeStatus → "MERGED"; the "OPEN" and null (gh-unreachable) branches — the ones that actually gate the Method 2 fallback — remain untested in that file.
  6. added 2 commits that reference this issue on Sep 11, 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

    bugplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions