Skip to content

Enhancement: Add PR state validation and auto-recovery for cross-repository forks #1216

@konard

Description

@konard

Background

Based on the case study in #1214, we identified that cross-repository fork PRs can be unexpectedly closed by GitHub during push operations. This is a GitHub platform issue, but we can implement mitigations in hive-mind.

Related

Proposed Enhancements

1. PR State Validation After Push

Add validation after every push to detect unexpected PR closures:

async function validatePRState(owner, repo, prNumber) {
  const state = await gh(`api repos/${owner}/${repo}/pulls/${prNumber} --jq .state`);
  if (state !== 'open') {
    throw new Error(`PR #${prNumber} is ${state}, expected 'open'`);
  }
}

// After push
await sleep(5000); // Wait for GitHub to sync
await validatePRState(owner, repo, prNumber);

2. Auto-Reopen Mechanism

Implement automatic PR reopening when an unexpected close is detected:

async function ensurePROpen(owner, repo, prNumber) {
  const pr = await getPRDetails(owner, repo, prNumber);
  if (pr.state === 'closed' && !pr.merged) {
    console.log(`PR #${prNumber} was unexpectedly closed, attempting to reopen...`);
    await gh(`pr reopen ${prNumber} --repo ${owner}/${repo}`);
    await notifyOwner(owner, repo, prNumber, 'PR was automatically reopened after unexpected closure');
  }
}

3. Periodic State Monitoring

During long-running AI sessions, periodically check PR state (every 2-3 minutes):

const stateCheckInterval = setInterval(async () => {
  await validatePRState(owner, repo, prNumber);
}, 120000); // Every 2 minutes

4. Cross-Repository Awareness

Add special handling for isCrossRepository: true PRs:

  • Extra delay after push operations (5-10 seconds) for GitHub sync
  • More frequent state validation
  • Automatic reopen attempts with backoff

Acceptance Criteria

  • After every push, validate PR state is still 'open'
  • If PR is unexpectedly closed, attempt automatic reopen
  • If reopen fails, notify via PR comment and raise clear error
  • Add logging for PR state changes
  • Add telemetry to track frequency of unexpected closures

Priority

Medium - The issue is rare but disruptive when it occurs.


Generated from case study analysis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions