Skip to content

Improve state cleanup to detect merged PRs and auto-remove #137

Closed

Description

@admarble

Summary

The current sequant status --cleanup behavior requires two steps to fully remove orphaned entries and doesn't distinguish between merged vs abandoned work.

Current Behavior

  1. sequant status --cleanup marks orphaned entries (missing worktree) as abandoned
  2. sequant status --cleanup --max-age 0 is required to actually remove them
  3. All orphaned entries get abandoned status, even if the PR was successfully merged

Proposed Behavior

  1. --cleanup checks GitHub to detect if the associated PR was merged
  2. Sets status to merged (not abandoned) for successfully merged PRs
  3. Auto-removes orphaned entries with merged status immediately
  4. Reserves abandoned status for truly abandoned work (issue closed without merge, PR closed without merge)

Acceptance Criteria

  • --cleanup checks gh pr list --state merged --search "<issue-number>" for each orphaned entry
  • Orphaned entries with merged PRs get status merged and are removed automatically
  • Orphaned entries without merged PRs get status abandoned (kept for review)
  • --max-age still works for removing old abandoned entries
  • Add --cleanup --all or similar to remove both merged and abandoned in one step

Technical Notes

  • cleanupStaleEntries() in src/lib/workflow/state-utils.ts
  • May need to batch GitHub API calls to avoid rate limiting for many issues
  • Consider caching merge status to avoid repeated API calls

Related

Activity

  1. added
    enhancement
    cli
    on Jan 20, 2026
  2. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    📋 Implementation Plan

    Acceptance Criteria Checklist

    • AC-1: --cleanup checks gh pr view <N> --json state for each orphaned entry with PR info
    • AC-2: Orphaned entries with merged PRs get status merged and are removed automatically
    • AC-3: Orphaned entries without merged PRs get status abandoned (kept for review)
    • AC-4: --max-age still works for removing old abandoned entries
    • AC-5: Add --all flag to remove both merged and abandoned orphaned entries in one step

    Verification Methods

    AC Method Test Scenario
    AC-1 Unit Test Mock gh pr view returning "MERGED" state
    AC-2 Unit Test Orphaned entry with merged PR → removed from state
    AC-3 Unit Test Orphaned entry with closed/no PR → status="abandoned"
    AC-4 Unit Test Existing test coverage at state-utils.test.ts:222-258
    AC-5 Unit + Manual --cleanup --all removes all orphaned entries

    Implementation Phases

    Phase 1: PR merge detection (state-utils.ts)

    • Add checkPRMergeStatus(prNumber) helper using gh pr view
    • Returns "MERGED" | "CLOSED" | "OPEN" | null

    Phase 2: Enhance cleanup logic (state-utils.ts:254-281)

    • Before marking orphaned as "abandoned", check PR status
    • Merged PR → set status="merged" and remove immediately
    • Closed/no PR → set status="abandoned" (current behavior)
    • Add removeAll option to remove abandoned entries too

    Phase 3: CLI interface (state.ts, status.ts, cli.ts)

    • Add --all flag to sequant state clean
    • Add --all flag to sequant status --cleanup

    Phase 4: Tests (state-utils.test.ts)

    • Mock-based unit tests for PR status detection
    • Test all status transition scenarios

    Assumptions to Validate

    • PR info stored in state when created (state.issues[N].pr)
    • Current behavior removes merged+orphaned already (confirmed at line 270-274)
    • gh auth status should be checked before API calls

    Open Questions

    1. Rate limiting: Recommend batch query via gh pr list --state merged first
    2. gh unavailable: Fallback to current behavior (mark as abandoned)
    3. --all scope: Only affects orphaned entries, not active worktrees

    Effort Breakdown

    • Phase 1: Add PR status helper function
    • Phase 2: Modify cleanupStaleEntries logic
    • Phase 3: Add CLI flags
    • Phase 4: Unit tests

    📝 Generated by /spec workflow

  3. added
    planned
    on Jan 20, 2026
  4. added a commit that references this issue on Jan 20, 2026
  5. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    Implementation Complete ✅

    AC Coverage Summary

    AC Status Implementation
    AC-1 ✅ checkPRMergeStatus() helper checks gh pr view <N> --json state for each orphaned entry with PR info
    AC-2 ✅ Orphaned entries with merged PRs get status merged and are removed automatically
    AC-3 ✅ Orphaned entries without merged PRs get status abandoned (kept for review)
    AC-4 ✅ --max-age still works for removing old abandoned entries
    AC-5 ✅ Added --all flag to remove both merged and abandoned orphaned entries in one step

    Key Files Changed

    • src/lib/workflow/state-utils.ts - Added checkPRMergeStatus() helper and enhanced cleanupStaleEntries()
    • src/commands/status.ts - Added --all flag support to handleCleanup()
    • src/commands/state.ts - Added --all flag support to stateCleanCommand()
    • bin/cli.ts - Added --all option to status command
    • src/lib/workflow/state-utils.test.ts - Added comprehensive unit tests

    Test Results

    • All 456 tests pass
    • Build succeeds with no TypeScript errors
    • New tests cover PR merge detection and cleanup behavior

    PR Status


    🤖 Generated by /exec workflow

  6. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    QA Review Complete ✅

    AC Coverage: 5/5 MET

    AC Description Status
    AC-1 --cleanup checks gh pr view <N> --json state for each orphaned entry with PR info ✅ MET
    AC-2 Orphaned entries with merged PRs get status merged and are removed automatically ✅ MET
    AC-3 Orphaned entries without merged PRs get status abandoned (kept for review) ✅ MET
    AC-4 --max-age still works for removing old abandoned entries ✅ MET
    AC-5 Add --all flag to remove both merged and abandoned orphaned entries in one step ✅ MET

    Quality Metrics

    • Type issues: 0 ✓
    • Deleted tests: 0 ✓
    • Files changed: 6 ✓
    • Tests: 456 passed ✓
    • Build: Pass ✓

    Code Review Summary

    Strengths:

    • Well-structured checkPRMergeStatus() helper with proper typing
    • Safe shell execution (array args, 10s timeout, graceful fallback)
    • Comprehensive unit tests (7 new test cases covering all scenarios)
    • Clean integration with existing cleanup logic

    Verdict: READY_FOR_MERGE 🎉

    All AC items fully met with high code quality. Only suggestion is to add a CHANGELOG entry for the new --all flag and PR merge detection behavior.


    🤖 Generated by /qa workflow

  7. added
    ready-for-review
    on Jan 20, 2026
  8. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    QA Review Complete

    AC Coverage: 5/5 MET ✅

    AC Description Status
    AC-1 --cleanup checks gh pr view <N> --json state for each orphaned entry with PR info ✅ MET
    AC-2 Orphaned entries with merged PRs get status merged and are removed automatically ✅ MET
    AC-3 Orphaned entries without merged PRs get status abandoned (kept for review) ✅ MET
    AC-4 --max-age still works for removing old abandoned entries ✅ MET
    AC-5 Add --all flag to remove both merged and abandoned orphaned entries in one step ✅ MET

    Quality Metrics

    • Type issues: 0 ✓
    • Deleted tests: 0 ✓
    • Files changed: 6 ✓
    • Tests: 456/456 passed ✓
    • Build: Pass ✓
    • Lint: FAIL ⚠️ (unused imports)

    Verdict: AC_MET_BUT_NOT_A_PLUS

    All acceptance criteria fully implemented with comprehensive tests. However, CI is failing due to lint warnings (unused imports):

    • state-utils.ts:21 - WorkflowState unused
    • state-utils.ts:28 - RunLog unused
    • state.ts:17 - DiscoverResult unused

    Next Steps

    1. Remove unused imports to fix lint
    2. Add CHANGELOG entry for --all flag and PR merge detection
    3. Re-run QA after fixes

    🤖 Generated by /qa workflow

  9. removed
    ready-for-review
    on Jan 20, 2026
  10. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    QA Review: READY_FOR_MERGE ✅

    AC Coverage: 5/5 MET

    AC Description Status
    AC-1 --cleanup checks gh pr view <N> --json state ✅ MET
    AC-2 Merged PRs auto-removed ✅ MET
    AC-3 Non-merged PRs marked abandoned ✅ MET
    AC-4 --max-age still works ✅ MET
    AC-5 --all flag implemented ✅ MET

    Quality Metrics

    • Type issues: 0 ✓
    • Deleted tests: 0 ✓
    • Files changed: 7 ✓
    • Tests: 456/456 passed ✓
    • Build: Pass ✓
    • Lint: Pass ✓
    • CI: All green ✓

    Verdict: READY_FOR_MERGE 🎉

    All acceptance criteria fully met with high code quality. Lint issues fixed, CI passing.

    Suggested: Add CHANGELOG entry for --all flag before merging.


    🤖 Generated by /qa workflow

  11. added
    ready-for-review
    on Jan 20, 2026
  12. added 2 commits that reference this issue on Jan 20, 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

    clienhancementplannedready-for-review

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions