@@ -262,13 +262,19 @@ jobs:
262262
263263 // A queue-superseded shard leaves no other trace in the logs.
264264 core.info(`Concurrency group: ${process.env.CONCURRENCY_GROUP}`);
265+ // Turn labels reconcile exclusively among themselves: exactly one
266+ // can be true. has-conflicts is deliberately NOT in this group
267+ // (issue #2089) — whether the branch has conflicts is an
268+ // independent fact that can coexist with any turn. It is managed
269+ // only by reconcileConflictFlag below: set on a known dirty read,
270+ // removed on a known read that is not dirty, untouched while
271+ // mergeability is unknown.
265272 const stateLabels = [
266273 'awaiting-author',
267274 'awaiting-coderabbit',
268275 'awaiting-ready',
269276 'awaiting-maintainer',
270277 'awaiting-review', // Legacy label removed during reconciliation.
271- 'has-conflicts',
272278 ];
273279 const labelDefinitions = [
274280 {
@@ -364,9 +370,13 @@ jobs:
364370 }
365371 }
366372
367- // Strips stateLabels from a PR, optionally keeping one desired label.
368- // Callers skip this entirely while mergeability is unknown (see the
369- // hoisted check below), so no keep-list is needed.
373+ // Strips stateLabels (turn labels) from a PR, optionally keeping
374+ // one desired label. Callers skip this entirely while mergeability
375+ // is unknown (see the hoisted check below), so no keep-list is
376+ // needed. has-conflicts lives outside this group: it is the
377+ // independent conflict flag managed by reconcileConflictFlag, so
378+ // turn-label reconciliation — including the outer failure cleanup
379+ // — never strips or sets it.
370380 // Stale-clock labels are owned by the stale step below and are never
371381 // touched here: stripping one on a transient CI or mergeability flap
372382 // would erase a live warning and restart its close grace (issue #2076).
@@ -415,6 +425,50 @@ jobs:
415425 }
416426 }
417427
428+ // Reconciles the independent has-conflicts flag (issue #2089).
429+ // `present` is a mergeability fact, so callers pass a known read
430+ // only: true for a known dirty read, false for a known read that
431+ // is not dirty. While mergeability is unknown no caller runs, so
432+ // the flag freezes together with the turn labels. A conflicted PR
433+ // must never sit in the maintainer queue, so every conflict
434+ // branch pairs this call with reconcileLabels(pr, 'awaiting-author').
435+ async function reconcileConflictFlag(pr, present) {
436+ if (isReadOnlyRun && isForkPR(pr)) {
437+ core.info(`PR #${pr.number}: fork PR on a read-only run — skipping label mutation`);
438+ return;
439+ }
440+ const hasLabel = pr.labels.some(label => label.name === 'has-conflicts');
441+ if (present === hasLabel) return;
442+ try {
443+ if (present) {
444+ await github.rest.issues.addLabels({
445+ owner, repo, issue_number: pr.number, labels: ['has-conflicts'],
446+ });
447+ if (!pr.labels.some(label => label.name === 'has-conflicts')) {
448+ pr.labels.push({ name: 'has-conflicts' });
449+ }
450+ } else {
451+ await github.rest.issues.removeLabel({
452+ owner, repo, issue_number: pr.number, name: 'has-conflicts',
453+ });
454+ pr.labels = pr.labels.filter(label => label.name !== 'has-conflicts');
455+ }
456+ } catch (err) {
457+ if (err.status === 404 && !present) {
458+ pr.labels = pr.labels.filter(label => label.name !== 'has-conflicts');
459+ return;
460+ }
461+ // The flag already decided this run's label state, so a
462+ // failed flag write must fail closed: mark the wrapper
463+ // error so the outer cleanup keeps the current state
464+ // labels exactly like the unknown-mergeability freeze
465+ // instead of stripping them.
466+ const error = new Error(`Could not reconcile has-conflicts label: ${err.message}`);
467+ error.preserveStateLabels = true;
468+ throw error;
469+ }
470+ }
471+
418472 async function findReviewGuide(pr) {
419473 const comments = await github.paginate(github.rest.issues.listComments, {
420474 owner, repo, issue_number: pr.number, per_page: 100,
@@ -772,10 +826,18 @@ jobs:
772826 }
773827
774828 if (prDetail.mergeable === false && prDetail.mergeable_state === 'dirty') {
775- core.info(`PR #${pr.number}: has merge conflicts — labeling has-conflicts`);
829+ core.info(
830+ `PR #${pr.number}: has merge conflicts — ` +
831+ 'labeling has-conflicts and awaiting-author'
832+ );
776833 await updateReviewGate(pr, 'conflict', false);
777834 await setCodeRabbitReviewActive(pr, false);
778- await reconcileLabels(pr, 'has-conflicts');
835+ // Conflicts are an independent flag, and a conflicted PR
836+ // always owes the author action (issue #2089): set the
837+ // flag and force the awaiting-author turn label — never
838+ // has-conflicts alone, and never a maintainer-queue label.
839+ await reconcileConflictFlag(pr, true);
840+ await reconcileLabels(pr, 'awaiting-author');
779841 await reconcileCommunityApproval(pr, 'remove');
780842 await updateReviewGuide(pr, 'conflict', existingGuide);
781843 continue;
@@ -787,15 +849,17 @@ jobs:
787849 // `mergeable_state: "unknown"`, GitHub has not finished computing
788850 // the answer, so it says nothing about conflicts and nothing about
789851 // whose turn it is. This run then changes NO state label for the
790- // PR: every state-label reconcile below is skipped. Acting on
852+ // PR: every state-label reconcile below is skipped, and the
853+ // has-conflicts flag freezes with the turn labels. Acting on
791854 // unknown previously let the normal reconcile swap labels (for
792855 // example has-conflicts to awaiting-author and back on the next
793856 // run's `dirty` read), which flapped labels on unchanged PRs.
794857 // Known-dirty never reaches here (the conflict check above
795- // handles it first); known-clean reconciles normally. The
796- // awaiting-maintainer branch below still refetches mergeability
797- // before the maintainer handoff, because a push between this
798- // fetch and labeling can change the answer; a known read there
858+ // handles it first); a known read that is not dirty removes
859+ // the flag in the branches below. The awaiting-maintainer
860+ // branch below still refetches mergeability before the
861+ // maintainer handoff, because a push between this fetch and
862+ // labeling can change the answer; a known read there
799863 // re-enables the final reconcile.
800864 let mergeabilityUnknown =
801865 prDetail.mergeable === null || prDetail.mergeable_state === 'unknown';
@@ -907,8 +971,11 @@ jobs:
907971 // mean a conflicting PR got no merge-ref CI run — not that its
908972 // conflicts went away. Unknown mergeability changes no state
909973 // label in this run, so this branch keeps every existing state
910- // label (has-conflicts included) instead of stripping them; a
911- // later run with a known read reconciles them.
974+ // label (the has-conflicts flag included) instead of stripping
975+ // them; a later run with a known read reconciles them. On a
976+ // known read the branch got here only because the PR is not
977+ // dirty (the conflict check handles dirty first), so the
978+ // conflict flag is removed along with the turn labels.
912979 if (ciPending || ciFailed) {
913980 core.info(
914981 `PR #${pr.number}: CI ${ciPending ? 'pending' : 'failed'} — ` +
@@ -919,6 +986,7 @@ jobs:
919986 await updateReviewGate(pr, ciPending ? 'ci-pending' : 'ci-failed', false);
920987 await setCodeRabbitReviewActive(pr, false);
921988 if (!mergeabilityUnknown) {
989+ await reconcileConflictFlag(pr, false);
922990 await reconcileLabels(pr, null);
923991 }
924992 await reconcileCommunityApproval(pr, 'remove');
@@ -1147,10 +1215,18 @@ jobs:
11471215 const headChanged = Boolean(latestPrDetail.head?.sha) &&
11481216 latestPrDetail.head.sha !== pr.head.sha;
11491217 if (latestPrDetail.mergeable === false && latestPrDetail.mergeable_state === 'dirty') {
1150- core.info(`PR #${pr.number}: has merge conflicts — labeling has-conflicts`);
1218+ core.info(
1219+ `PR #${pr.number}: has merge conflicts — ` +
1220+ 'labeling has-conflicts and awaiting-author'
1221+ );
11511222 await updateReviewGate(pr, 'conflict', false);
11521223 await setCodeRabbitReviewActive(pr, false);
1153- await reconcileLabels(pr, 'has-conflicts');
1224+ // Same invariant as the first conflict branch (issue
1225+ // #2089): the flag plus the awaiting-author turn label,
1226+ // so a conflicted PR can never land in the maintainer
1227+ // queue this run was about to grant.
1228+ await reconcileConflictFlag(pr, true);
1229+ await reconcileLabels(pr, 'awaiting-author');
11541230 await reconcileCommunityApproval(pr, 'remove');
11551231 await updateReviewGuide(pr, 'conflict', existingGuide);
11561232 continue;
@@ -1212,6 +1288,10 @@ jobs:
12121288 await updateReviewGuide(pr, phase, existingGuide);
12131289 }
12141290 if (!mergeabilityUnknown) {
1291+ // The read that re-enabled this reconcile is known and not
1292+ // dirty (the conflict branches handle dirty first), so the
1293+ // conflict flag comes off whenever the turn labels reconcile.
1294+ await reconcileConflictFlag(pr, false);
12151295 await reconcileLabels(pr, desiredLabel);
12161296 }
12171297 await reconcileCommunityApproval(pr, communityDecision);
0 commit comments