Skip to content

fix(run): pre-flight guard misses in_progress issues already merged on GitHub (gap in #305/#423) #592

Closed

Description

@admarble

Problem

sequant run <N> does not detect when <N> is already merged on GitHub if the local state.json still records it as in_progress. The exec skill spawns, runs for ~3 minutes, produces no changes, and the orchestrator marks the phase failed and triggers the generic MCP fallback retry — burning another spawn.

Evidence (live incident, 2026-05-06)

Command: npx sequant run 583 580 568

Source #568 state
GitHub CLOSED at 2026-05-07T00:23:45Z, PR #590 merged (commit 9aa2b4a)
.sequant/state.json status: "in_progress", currentPhase: "exec" (residue from a partial run on 2026-05-04)
Result phases.exec.error = "exec produced no changes (no commits, no uncommitted work)" after 197s, then ! Phase failed with MCP enabled, retrying without MCP...

The user-facing surface ("retrying without MCP") is misleading — MCP wasn't the cause. The cause was that the work was already on main.

Why existing guards missed it

Two guards exist from prior work; both rely on local-state status fields:

  1. Pre-flight guard (src/lib/workflow/run-orchestrator.ts:472-498, from fix: pre-flight state guard and worktree lifecycle management #305) skips only status === "ready_for_merge" | "merged". in_progress passes through.
  2. reconcileStateAtStartup (src/lib/workflow/state-cleanup.ts:295-299, from fix: pre-flight state guard and worktree lifecycle management #305) only advances ready_for_merge → merged. It never escalates in_progress → merged, even though isIssueMergedIntoMain / checkPRMergeStatus already exist in pr-status.ts and would answer correctly.

#423 introduced reconcile-on-read for sequant status and explicitly listed this drift class ("Issues closed on GitHub still show as in_progress"), but the fix did not extend to the run command path.

State drifts to this configuration whenever a PR lands via a path that doesn't write back to state.json: a separate sequant session, gh pr merge, the web UI, or a --force push.

Acceptance Criteria

  • sequant run <N> skips <N> when GitHub reports the issue closed AND the corresponding branch/PR is merged into the base branch — even if local state.json says in_progress.
  • Skip message clearly identifies the cause (issue merged on GitHub) and mentions --force as the bypass, matching the existing pre-flight guard's voice at run-orchestrator.ts:484.
  • Local state.json is updated to status: "merged" for the skipped issue (so subsequent runs hit the cheaper local-status check).
  • --force continues to bypass the new check, same as the existing guard.
  • One GitHub round-trip per in_progress issue at run start — not per phase. Cached for the duration of the run.
  • Failure to reach GitHub falls through to current behavior (do not block offline runs).
  • Unit test using a mocked PlatformProvider proves an in_progress entry whose remote shows merged is skipped.

Out of scope

  • Reconciling all other state drift cases (abandoned, renamed, etc.) — covered by separate hygiene work.
  • Changing the MCP-fallback message itself. The fallback is correct; the upstream cause is what this issue removes.

Suggested implementation

Prefer extending reconcileStateAtStartup (option (a) from the diagnosis) over adding a second check in the pre-flight loop:

  • Loop over in_progress entries in addition to ready_for_merge.
  • Reuse isIssueMergedIntoMain(issueNum) and (if PR number is recorded) checkPRMergeStatus.
  • On match, advance status → "merged" and stamp resolvedAt.

The existing pre-flight guard at run-orchestrator.ts:472 then handles the skip with its current message, no new code path needed.

Related

Activity

  1. added
    bug
    planned
    on May 7, 2026
  2. admarble commented on May 8, 2026

    @admarble
    ContributorAuthor

    /spec — Plan

    Acceptance Criteria

    • AC-1: sequant run <N> skips <N> when GitHub reports merged, even if local state.json says in_progress (Verification: unit test + manual smoke)
    • AC-2: Skip message reuses existing pre-flight voice (already merged — skipping (use --force to re-run)) at run-orchestrator.ts:484 (Verification: code review)
    • AC-3: state.json advances in_progress → merged, resolvedAt stamped (Verification: unit test)
    • AC-4: --force continues to bypass (no change required, existing flag) (Verification: code review)
    • AC-5: One GitHub round-trip per in_progress issue at run start, cached for run (Verification: code review — reconcile runs once pre-loop)
    • AC-6: GitHub-unreachable falls through to current behavior (Verification: unit test)
    • AC-7: Unit test with mocked GitHubProvider proves the escalation (Verification: new test file)

    Implementation

    File: `src/lib/workflow/state-cleanup.ts`
    Change: Relax the loop guard at line 297 in `reconcileStateAtStartup` to include `in_progress` alongside `ready_for_merge`. Existing logic (PR check → git fallback → state mutation → `resolvedAt` stamp) requires no changes.

    New test: `src/lib/workflow/state-cleanup.test.ts` (mirrors mock pattern from `reconcile.test.ts`).

    Why this approach

    Issue body recommends extending `reconcileStateAtStartup` over adding a second pre-flight check. This is the smallest correct change:

    • One logical condition relaxed (one OR clause added)
    • No new code paths, no new helpers
    • Existing pre-flight guard at `run-orchestrator.ts:472-498` then handles the skip with its current message — no producer-side changes there

    Out of Scope (per issue body)

    Conflict Check

    Only in-flight worktree is #568 (skill namespace fix) — no file overlap.

    Recommended Workflow

    `spec → exec → qa` (no `/test` — backend only)

  3. admarble commented on May 8, 2026

    @admarble
    ContributorAuthor

    /exec — Implementation complete

    Branch: feature/592-fix-run-preflight-merged-inprogress
    Commit: aacbf13

    Diff summary

    File Change
    src/lib/workflow/state-cleanup.ts Relax loop guard in reconcileStateAtStartup to include in_progress; updated JSDoc
    src/lib/workflow/state-cleanup.test.ts NEW — 5 tests covering AC-1/AC-3/AC-6/AC-7 + regression for ready_for_merge
    src/lib/workflow/state-utils.test.ts Updated 1 pre-existing test (should not affect issues with other statuses) — old assertion documented pre-#592 behavior; added spawnSync mock to avoid colliding with real (#44) merge commit in this repo

    Verification

    • New tests: 5/5 passing (state-cleanup.test.ts)
    • Adjacent suites: state-utils.test.ts (31), reconcile.test.ts (32), state-manager.test.ts (65) — all green
    • Full workflow suite: 810/810 passing
    • npm run build — clean
    • npm run lint — clean
  4. admarble commented on May 8, 2026

    @admarble
    ContributorAuthor

    /fullsolve Complete

    Issue: #592
    Status: READY_FOR_MERGE
    PR: #602

    Summary

    Metric Value
    AC 7/7 met
    Tests (workflow suite) 810/810 passing
    New tests 5 (state-cleanup.test.ts)
    Build ✅ clean
    Lint ✅ clean
    Production diff 16 lines (1 file)
    Test diff 200 lines (2 files)
    CHANGELOG ✅ entry added

    Iterations

    • Spec loop: 1
    • Exec loop: 1
    • Test loop: skipped (backend, no UI labels)
    • QA loop: 1

    Implementation

    Single guard relaxation in src/lib/workflow/state-cleanup.ts:reconcileStateAtStartup:

    -      if (issueState.status !== "ready_for_merge") {
    +      if (
    +        issueState.status !== "ready_for_merge" &&
    +        issueState.status !== "in_progress"
    +      ) {
             continue;
           }

    Existing PR-status check + git fallback + resolvedAt stamp + state persistence all reused unchanged. The pre-flight skip guard at run-orchestrator.ts:472-498 (and its message at :484) is unchanged — it sees merged after reconcile and skips with the existing voice. --force continues to bypass.

    Risk Assessment

    • Likely failure mode: False-positive on the git-fallback path — if an in_progress issue's number happens to appear as (#N) in an unrelated commit on main, isIssueMergedIntoMain would return true. Pre-existing risk (same code path is used for ready_for_merge); explicitly out of scope per the issue body.
    • Not tested: The git-fallback isIssueMergedIntoMain path for an in_progress issue without a PR number — covered indirectly by existing pr-status.ts integration tests; mocking spawnSync in unit tests would be brittle. Manual smoke against the originating state.json post-merge is the appropriate complement.

    Ready for human review and merge.

  5. added a commit that references this issue on May 8, 2026
  6. admarble commented on May 8, 2026

    @admarble
    ContributorAuthor

    Adversarial second-look follow-ups

    After the user asked "any gaps?", an adversarial walk-through surfaced 3 minor gaps + 1 noted (non-actionable) latency point. All 3 actionable items addressed:

    Gap Action Commit
    Git-fallback escalation path was not directly tested (only the false-return case was covered) Added vi.mock("child_process") + new test 6 in state-cleanup.test.ts exercising in_progress + no pr.number + mocked merge commit → escalation 0292123
    docs/internal/what-weve-built.md:855 didn't reference the in_progress extension Forward-reference added under the #305 entry 0292123
    waiting_for_qa_gate has the same drift potential Filed follow-up issue #606 with the symmetric one-line fix and AC mirrored from this issue n/a

    Latency note (non-actionable): every sequant run now incurs N gh pr view calls where N = count of in_progress (and post-#606, also waiting_for_qa_gate) entries in state.json. AC-5 of #592 explicitly accepts this behavior. Failure mode is graceful (gh-unreachable falls through, AC-6).

    Updated test count: 6/6 in state-cleanup.test.ts (was 5). Full workflow suite still green.

  7. added a commit that references this issue on May 8, 2026
  8. admarble commented on May 8, 2026

    @admarble
    ContributorAuthor

    /docs — No separate doc needed

    This fix is an internal reconciliation behavior change with no new user-facing surface:

    • bin/cli.ts — unchanged (no new flags)
    • src/commands/, src/mcp/, app/admin/ — unchanged (no new commands, MCP tools, or UI)
    • The only user-visible effect is that the existing skip message at `run-orchestrator.ts:484` (`already merged — skipping (use --force to re-run)`) now fires in one additional scenario (`in_progress` entries reconciled to `merged`).

    Documentation already in place at the correct level:

    Surface Location
    User-facing release note `CHANGELOG.md` → `## [Unreleased]` → `### Fixed`
    Internal architecture log `docs/internal/what-weve-built.md:855` (cross-reference under #305)
    Squashed commit `c6f673d`
    PR #602
    Issue thread spec / exec / qa / summary / adversarial follow-up comments

    A standalone `docs/features/*.md` page would have empty Prerequisites / Setup / Reference sections — not appropriate for an internal fix with no new commands or workflows.

  9. added 3 commits that reference this issue on May 9, 2026
  10. admarble commented on May 11, 2026

    @admarble
    ContributorAuthor

    Cross-ref: #625 (cross-session issue lock) covers a related but distinct staleness mode — local concurrent sessions targeting the same issue, rather than GitHub-side PR merges.

    Both implementations need a "is this state still fresh?" check. If #592 and #625 land close together, worth extracting a shared helper (e.g., a StalenessProbe that takes a state record and returns fresh | stale | unknown) so the two failure modes share a vocabulary.

  11. added a commit that references this issue on Jul 15, 2026
  12. added 2 commits that reference this issue on Jul 28, 2026
  13. added 4 commits that reference this issue on Sep 9, 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