Skip to content

Conversation

@jdalton
Copy link
Contributor

@jdalton jdalton commented Nov 18, 2025

Fixes bug where PRs fail to be created on subsequent runs when branch exists from previous failed attempts.

Changes:

  • Check PR existence instead of branch existence
  • Clean up stale branches without open PRs
  • Add detailed error handling for PR creation failures

Note

Checks for existing open PRs, cleans up stale branches, adds structured PR creation errors, and integrates new branch cleanup utilities with tests; updates reach help snapshots.

  • Fix PR creation flow (src/commands/fix/coana-fix.mts):
    • Check for existing open PRs before proceeding; skip if found.
    • If remote branch exists without an open PR, delete as stale via cleanupStaleBranch.
    • Validate GitHub token earlier; skip safely if missing.
    • Use structured openSocketFixPr result for detailed failure handling (already exists, validation, permission, network, unknown) and clean up branches accordingly.
    • After successful PR, delete only the local branch via cleanupSuccessfulPrLocalBranch.
    • On errors, detect remote branch existence and clean up via cleanupErrorBranches; reset back to base branch.
  • Branch cleanup utilities (src/commands/fix/branch-cleanup.mts):
    • Add cleanupStaleBranch, cleanupFailedPrBranches, cleanupSuccessfulPrLocalBranch, cleanupErrorBranches with unit and integration tests.
  • PR API (src/commands/fix/pull-request.mts):
    • openSocketFixPr now returns a discriminated OpenPrResult with explicit error reasons and logging.
  • Tests/Help snapshots:
    • Add new integration/unit tests for branch cleanup.
    • Update reachability help text/tests to include --reach-concurrency and --reach-disable-analysis-splitting and ordering tweaks.
  • Misc:
    • Minor spawnCoanaDlx local-path execution formatting/refactor in src/utils/dlx.mts.

Written by Cursor Bugbot for commit e1fbda4. Configure here.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment @cursor review or bugbot run to trigger another review on this PR

Replaces undefined return with OpenPrResult discriminated union type:
- Distinguishes failure reasons: already_exists, validation_error, permission_denied, network_error, unknown
- Enables caller to handle specific error cases appropriately
- Provides detailed error information including validation details

This foundational change enables better error handling in subsequent commits.
Previously, the socket fix command would only create a PR once and then
never again because it checked for branch existence instead of PR
existence. Since branches persist after PR creation, subsequent runs
would skip PR creation entirely.

Changes:
- Check for open PR existence instead of branch existence
- Add stale branch cleanup before creating new branches
- Move GitHub token check before git operations for early validation
- Integrate cleanup functions for proper branch lifecycle management
- Add comprehensive error handling for all PR creation failure types
- Remove unused gitDeleteRemoteBranch import

This ensures PRs can be recreated after being closed/merged and prevents
branch accumulation from failed PR attempts.
Extract branch cleanup operations into a separate module to improve
code organization and testability. Each cleanup function has a specific
purpose based on the branch lifecycle stage.

Added functions:
- cleanupStaleBranch: Delete both remote and local for branches without PRs
- cleanupFailedPrBranches: Clean up after PR creation failure
- cleanupSuccessfulPrLocalBranch: Remove only local branch after PR success
- cleanupErrorBranches: Handle cleanup after unexpected errors

Tests:
- 8 unit tests with mocked git functions
- 5 integration tests with real git repositories in tmpdir
@jdalton jdalton force-pushed the jdalton/fix-pr-creation-logic branch from db211c9 to e1fbda4 Compare November 18, 2025 20:25
@jdalton
Copy link
Contributor Author

jdalton commented Nov 18, 2025

@cursor review

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no bugs!


Comment @cursor review or bugbot run to trigger another review on this PR

@jdalton jdalton merged commit b31f372 into v1.x Nov 18, 2025
8 checks passed
@jdalton jdalton deleted the jdalton/fix-pr-creation-logic branch November 18, 2025 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants