@@ -77,6 +77,14 @@ export class ReviewManager extends Disposable {
7777 */
7878 private _cachedMaxPRNumbers : Map < string , number > | undefined ;
7979 private _cachedBranchName : string | undefined ;
80+ /**
81+ * Tracks branches for which we've already performed the one-shot GitHub
82+ * re-check after detecting existing local PR metadata. This allows stale
83+ * `branch.<name>.github-pr-owner-number` entries (e.g. pointing at a
84+ * closed PR) to self-heal once, without bypassing the
85+ * branch-change/new-PR cache on every subsequent `validateState` call.
86+ */
87+ private readonly _staleMetadataCheckedBranches = new Set < string > ( ) ;
8088 private _pollHandle : NodeJS . Timeout | undefined ;
8189 /**
8290 * Flag set when the "Checkout" action is used and cleared on the next git
@@ -588,15 +596,19 @@ export class ReviewManager extends Disposable {
588596 Logger . appendLine ( `No matching pull request metadata found locally for current branch ${ branch . name } ` , this . id ) ;
589597 }
590598
591- // Always check GitHub for a matching open PR (subject to the new-PRs/branch-change cache),
592- // even when local metadata already exists. If GitHub returns a result, it overwrites the
593- // local metadata via associateBranchWithPullRequest. This allows branches whose local
594- // metadata points to a stale closed PR to recover automatically once an open PR exists.
595- if ( this . _cachedBranchName !== branch . name || await this . hasNewPullRequests ( ) || ! matchingPullRequestMetadata ) {
599+ // One-shot self-heal: when local metadata exists for this branch, re-check GitHub once
600+ // (per branch) in case the local metadata points to a stale closed PR. If GitHub returns
601+ // a result, it overwrites the local metadata via associateBranchWithPullRequest. Subsequent
602+ // checks for the same branch fall back to the branch-change/new-PR cache.
603+ const needsStaleMetadataCheck = ! ! matchingPullRequestMetadata && ! ! branch . name && ! this . _staleMetadataCheckedBranches . has ( branch . name ) ;
604+ if ( this . _cachedBranchName !== branch . name || await this . hasNewPullRequests ( ) || needsStaleMetadataCheck ) {
596605 const metadataFromGithub = await this . checkGitHubForPrBranch ( branch ) ;
597606 if ( metadataFromGithub ) {
598607 matchingPullRequestMetadata = metadataFromGithub ;
599608 }
609+ if ( needsStaleMetadataCheck && branch . name ) {
610+ this . _staleMetadataCheckedBranches . add ( branch . name ) ;
611+ }
600612 } else {
601613 Logger . appendLine ( `Skipping GitHub check for branch ${ branch . name } : no new PRs since last check` , this . id ) ;
602614 }
0 commit comments