Skip to content

Commit 98dcca7

Browse files
committed
fix: prevent stale PR review labels from persisting
1 parent f2fc64e commit 98dcca7

2 files changed

Lines changed: 369 additions & 90 deletions

File tree

‎src/clawsweeper.ts‎

Lines changed: 191 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15168,6 +15168,76 @@ function pullHeadShaFromReport(markdown: string): string | null {
1516815168
return value && value !== "unknown" ? value : null;
1516915169
}
1517015170

15171+
type StalePullRequestReviewHead = {
15172+
reportHeadSha: string;
15173+
liveHeadSha: string;
15174+
reason: string;
15175+
};
15176+
15177+
function stalePullRequestReviewHead(
15178+
markdown: string,
15179+
context: ItemContext,
15180+
): StalePullRequestReviewHead | null {
15181+
if (frontMatterValue(markdown, "type") !== "pull_request") return null;
15182+
const reportHeadSha = pullHeadShaFromReport(markdown);
15183+
const liveHeadSha = pullHeadShaFromContext(context);
15184+
if (!reportHeadSha || !liveHeadSha || reportHeadSha === liveHeadSha) return null;
15185+
return {
15186+
reportHeadSha,
15187+
liveHeadSha,
15188+
reason: `live PR head ${liveHeadSha} differs from reviewed head ${reportHeadSha}`,
15189+
};
15190+
}
15191+
15192+
function isStalePullRequestReviewLabel(label: string): boolean {
15193+
return (
15194+
isClawSweeperOwnedLabel(label) &&
15195+
!PRIORITY_LABEL_NAMES.has(label) &&
15196+
!IMPACT_LABEL_NAMES.has(label) &&
15197+
!MATURITY_LABEL_NAMES.has(label) &&
15198+
!isIssueAdvisoryLabel(label)
15199+
);
15200+
}
15201+
15202+
function syncStalePullRequestReviewLabels(options: {
15203+
number: number;
15204+
labels: readonly string[];
15205+
dryRun: boolean;
15206+
}): { labels: string[]; changed: boolean } {
15207+
const labelsToRemove = options.labels.filter(isStalePullRequestReviewLabel);
15208+
if (labelsToRemove.length === 0) return { labels: [...options.labels], changed: false };
15209+
const nextLabels = options.labels.filter((label) => !labelsToRemove.includes(label));
15210+
if (!options.dryRun) {
15211+
for (const label of labelsToRemove) {
15212+
ghWithRetry(["issue", "edit", String(options.number), "--remove-label", label]);
15213+
}
15214+
}
15215+
return { labels: nextLabels, changed: true };
15216+
}
15217+
15218+
function stalePullRequestReviewComment(options: {
15219+
number: number;
15220+
stale: StalePullRequestReviewHead;
15221+
}): string {
15222+
const attrs = [
15223+
`item=${markerAttributeValue(String(options.number))}`,
15224+
`reviewed_sha=${markerAttributeValue(options.stale.reportHeadSha)}`,
15225+
`current_sha=${markerAttributeValue(options.stale.liveHeadSha)}`,
15226+
"reason=stale_head",
15227+
].join(" ");
15228+
return [
15229+
"Codex review: stale review; fresh review needed.",
15230+
"",
15231+
"**Summary**",
15232+
`The latest durable ClawSweeper review was for head \`${options.stale.reportHeadSha}\`, but the PR head is now \`${options.stale.liveHeadSha}\`. Its old verdict and PR readiness labels are no longer current.`,
15233+
"",
15234+
"**Next step**",
15235+
"Run or wait for a fresh ClawSweeper review on the current PR head.",
15236+
"",
15237+
`<!-- clawsweeper-review-status:stale ${attrs} -->`,
15238+
].join("\n");
15239+
}
15240+
1517115241
function markerAttributeValue(value: string): string {
1517215242
return value.trim().replace(/[^\w./:@-]/g, "_") || "unknown";
1517315243
}
@@ -17431,68 +17501,110 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1743117501
continue;
1743217502
}
1743317503
}
17504+
const existingReviewComment = issueReviewComment(number, [
17505+
renderReviewCommentFromReport(markdown, closeReason ?? "none", { previousLabels }),
17506+
reviewSectionValue(markdown, "closeComment"),
17507+
]);
17508+
const existingReviewCommentUpdatedAt = commentUpdatedAt(existingReviewComment);
17509+
if (existingReviewCommentUpdatedAt) {
17510+
allowedSelfMutationUpdatedAts.add(existingReviewCommentUpdatedAt);
17511+
}
17512+
const updatedSinceReview = Boolean(storedUpdatedAt && item.updatedAt !== storedUpdatedAt);
17513+
const reviewCommentOnlyUpdate = item.updatedAt === commentUpdatedAt(existingReviewComment);
17514+
const labelSyncFreshEnough = (): boolean => {
17515+
if (!storedUpdatedAt) return false;
17516+
if (!updatedSinceReview || reviewCommentOnlyUpdate) return true;
17517+
const existingReviewCommentUpdatedAtMs = timestampMs(commentUpdatedAt(existingReviewComment));
17518+
const itemUpdatedAtMs = timestampMs(item.updatedAt);
17519+
if (existingReviewCommentUpdatedAtMs === null || itemUpdatedAtMs === null) return false;
17520+
if (Math.abs(itemUpdatedAtMs - existingReviewCommentUpdatedAtMs) > 5 * 60 * 1000) {
17521+
return false;
17522+
}
17523+
const storedUpdatedAtMs = timestampMs(storedUpdatedAt);
17524+
if (storedUpdatedAtMs === null) return false;
17525+
return !contextHasNonAutomationActivityAfter(currentItemContext(), storedUpdatedAtMs);
17526+
};
17527+
const stalePrReviewHead =
17528+
state === "open" && item.kind === "pull_request"
17529+
? stalePullRequestReviewHead(markdown, currentItemContext())
17530+
: null;
1743417531
let currentPrStatusKind: PrStatusLabelKind | null = null;
1743517532
if (state === "open" && item.kind === "pull_request") {
17436-
const realBehaviorProof = reportRealBehaviorProof(markdown);
17437-
const proofSufficientSyncResult = syncRealBehaviorProofSufficientLabel({
17438-
number,
17439-
labels: item.labels,
17440-
proof: realBehaviorProof,
17441-
dryRun,
17442-
});
17443-
item.labels = proofSufficientSyncResult.labels;
17444-
clawSweeperLabelsChanged ||= proofSufficientSyncResult.changed;
17445-
const proofMediaSyncResult = syncRealBehaviorProofMediaLabels({
17446-
number,
17447-
labels: item.labels,
17448-
proof: realBehaviorProof,
17449-
dryRun,
17450-
});
17451-
item.labels = proofMediaSyncResult.labels;
17452-
clawSweeperLabelsChanged ||= proofMediaSyncResult.changed;
17453-
const prRatingSyncResult = syncPrRatingLabel({
17454-
number,
17455-
labels: item.labels,
17456-
rating: reportPrRating(markdown),
17457-
reviewFailed: frontMatterValue(markdown, "review_status") === "failed",
17458-
dryRun,
17459-
});
17460-
item.labels = prRatingSyncResult.labels;
17461-
clawSweeperLabelsChanged ||= prRatingSyncResult.changed;
17462-
const featureShowcaseSyncResult = syncFeatureShowcaseLabel({
17463-
number,
17464-
labels: item.labels,
17465-
isPullRequest: true,
17466-
itemCategory: frontMatterValue(markdown, "item_category"),
17467-
requiresNewFeature: frontMatterValue(markdown, "requires_new_feature") === "true",
17468-
showcase: reportFeatureShowcase(markdown),
17469-
securityReview: reportSecurityReview(markdown),
17470-
overallCorrectness: reportOverallCorrectness(markdown),
17471-
dryRun,
17472-
});
17473-
item.labels = featureShowcaseSyncResult.labels;
17474-
clawSweeperLabelsChanged ||= featureShowcaseSyncResult.changed;
17475-
currentPrStatusKind = prStatusLabelKindFromReport(
17476-
markdown,
17477-
currentItemContext(),
17478-
item.labels,
17479-
);
17480-
const prStatusSyncResult = syncPrStatusLabel({
17481-
number,
17482-
labels: item.labels,
17483-
statusKind: currentPrStatusKind,
17484-
dryRun,
17485-
});
17486-
item.labels = prStatusSyncResult.labels;
17487-
clawSweeperLabelsChanged ||= prStatusSyncResult.changed;
17488-
const telegramVisibleProofSyncResult = syncTelegramVisibleProofLabel({
17489-
number,
17490-
labels: item.labels,
17491-
proof: reportTelegramVisibleProof(markdown),
17492-
dryRun,
17493-
});
17494-
item.labels = telegramVisibleProofSyncResult.labels;
17495-
clawSweeperLabelsChanged ||= telegramVisibleProofSyncResult.changed;
17533+
if (stalePrReviewHead) {
17534+
const staleLabelSyncResult = syncStalePullRequestReviewLabels({
17535+
number,
17536+
labels: item.labels,
17537+
dryRun,
17538+
});
17539+
item.labels = staleLabelSyncResult.labels;
17540+
clawSweeperLabelsChanged ||= staleLabelSyncResult.changed;
17541+
markdown = replaceFrontMatterValue(
17542+
markdown,
17543+
"current_pull_head_sha",
17544+
stalePrReviewHead.liveHeadSha,
17545+
);
17546+
} else if (labelSyncFreshEnough()) {
17547+
const realBehaviorProof = reportRealBehaviorProof(markdown);
17548+
const proofSufficientSyncResult = syncRealBehaviorProofSufficientLabel({
17549+
number,
17550+
labels: item.labels,
17551+
proof: realBehaviorProof,
17552+
dryRun,
17553+
});
17554+
item.labels = proofSufficientSyncResult.labels;
17555+
clawSweeperLabelsChanged ||= proofSufficientSyncResult.changed;
17556+
const proofMediaSyncResult = syncRealBehaviorProofMediaLabels({
17557+
number,
17558+
labels: item.labels,
17559+
proof: realBehaviorProof,
17560+
dryRun,
17561+
});
17562+
item.labels = proofMediaSyncResult.labels;
17563+
clawSweeperLabelsChanged ||= proofMediaSyncResult.changed;
17564+
const prRatingSyncResult = syncPrRatingLabel({
17565+
number,
17566+
labels: item.labels,
17567+
rating: reportPrRating(markdown),
17568+
reviewFailed: frontMatterValue(markdown, "review_status") === "failed",
17569+
dryRun,
17570+
});
17571+
item.labels = prRatingSyncResult.labels;
17572+
clawSweeperLabelsChanged ||= prRatingSyncResult.changed;
17573+
const featureShowcaseSyncResult = syncFeatureShowcaseLabel({
17574+
number,
17575+
labels: item.labels,
17576+
isPullRequest: true,
17577+
itemCategory: frontMatterValue(markdown, "item_category"),
17578+
requiresNewFeature: frontMatterValue(markdown, "requires_new_feature") === "true",
17579+
showcase: reportFeatureShowcase(markdown),
17580+
securityReview: reportSecurityReview(markdown),
17581+
overallCorrectness: reportOverallCorrectness(markdown),
17582+
dryRun,
17583+
});
17584+
item.labels = featureShowcaseSyncResult.labels;
17585+
clawSweeperLabelsChanged ||= featureShowcaseSyncResult.changed;
17586+
currentPrStatusKind = prStatusLabelKindFromReport(
17587+
markdown,
17588+
currentItemContext(),
17589+
item.labels,
17590+
);
17591+
const prStatusSyncResult = syncPrStatusLabel({
17592+
number,
17593+
labels: item.labels,
17594+
statusKind: currentPrStatusKind,
17595+
dryRun,
17596+
});
17597+
item.labels = prStatusSyncResult.labels;
17598+
clawSweeperLabelsChanged ||= prStatusSyncResult.changed;
17599+
const telegramVisibleProofSyncResult = syncTelegramVisibleProofLabel({
17600+
number,
17601+
labels: item.labels,
17602+
proof: reportTelegramVisibleProof(markdown),
17603+
dryRun,
17604+
});
17605+
item.labels = telegramVisibleProofSyncResult.labels;
17606+
clawSweeperLabelsChanged ||= telegramVisibleProofSyncResult.changed;
17607+
}
1749617608
}
1749717609
markdown = replaceFrontMatterValue(markdown, "labels", JSON.stringify(item.labels));
1749817610
if (clawSweeperLabelsChanged && !dryRun) {
@@ -17506,19 +17618,9 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1750617618
renderOptions.hasOpenLinkedPullRequest =
1750717619
openClosingPullRequestApplyReason(currentClosingPullRequests) !== null;
1750817620
}
17509-
let reviewComment = renderReviewCommentFromReport(
17510-
markdown,
17511-
closeReason ?? "none",
17512-
renderOptions,
17513-
);
17514-
const existingReviewComment = issueReviewComment(number, [
17515-
reviewComment,
17516-
reviewSectionValue(markdown, "closeComment"),
17517-
]);
17518-
const existingReviewCommentUpdatedAt = commentUpdatedAt(existingReviewComment);
17519-
if (existingReviewCommentUpdatedAt) {
17520-
allowedSelfMutationUpdatedAts.add(existingReviewCommentUpdatedAt);
17521-
}
17621+
let reviewComment = stalePrReviewHead
17622+
? stalePullRequestReviewComment({ number, stale: stalePrReviewHead })
17623+
: renderReviewCommentFromReport(markdown, closeReason ?? "none", renderOptions);
1752217624
let markedReviewComment = markedReviewCommentBody(number, reviewComment);
1752317625
let proofBlockedForCommentSync: PrCloseCoverageProofGateBlock | null = null;
1752417626
const protectedApplyReason = applyProtectedLabelReason(item.labels, closeReason);
@@ -17550,21 +17652,6 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1755017652
break;
1755117653
continue;
1755217654
}
17553-
const updatedSinceReview = Boolean(storedUpdatedAt && item.updatedAt !== storedUpdatedAt);
17554-
const reviewCommentOnlyUpdate = item.updatedAt === commentUpdatedAt(existingReviewComment);
17555-
const labelSyncFreshEnough = (): boolean => {
17556-
if (!storedUpdatedAt) return false;
17557-
if (!updatedSinceReview || reviewCommentOnlyUpdate) return true;
17558-
const existingReviewCommentUpdatedAtMs = timestampMs(commentUpdatedAt(existingReviewComment));
17559-
const itemUpdatedAtMs = timestampMs(item.updatedAt);
17560-
if (existingReviewCommentUpdatedAtMs === null || itemUpdatedAtMs === null) return false;
17561-
if (Math.abs(itemUpdatedAtMs - existingReviewCommentUpdatedAtMs) > 5 * 60 * 1000) {
17562-
return false;
17563-
}
17564-
const storedUpdatedAtMs = timestampMs(storedUpdatedAt);
17565-
if (storedUpdatedAtMs === null) return false;
17566-
return !contextHasNonAutomationActivityAfter(currentItemContext(), storedUpdatedAtMs);
17567-
};
1756817655
const markChangedSinceReview = (options: {
1756917656
reason: string;
1757017657
currentUpdatedAt?: string | undefined;
@@ -17724,6 +17811,16 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1772417811
if (processedCount >= processedLimit) break;
1772517812
continue;
1772617813
}
17814+
if (isCloseProposal && stalePrReviewHead) {
17815+
if (
17816+
markChangedSinceReview({
17817+
reason: stalePrReviewHead.reason,
17818+
currentUpdatedAt: item.updatedAt,
17819+
})
17820+
)
17821+
break;
17822+
continue;
17823+
}
1772717824
if (isCloseProposal && updatedSinceReview && !reviewCommentOnlyUpdate) {
1772817825
markdown = replaceFrontMatterValue(markdown, "action_taken", "skipped_changed_since_review");
1772917826
markdown = replaceFrontMatterValue(markdown, "current_item_updated_at", item.updatedAt);
@@ -17762,7 +17859,9 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1776217859
}
1776317860
}
1776417861
const isCurrentCompleteReport =
17765-
frontMatterValue(markdown, "review_status") === "complete" && labelSyncFreshEnough();
17862+
frontMatterValue(markdown, "review_status") === "complete" &&
17863+
!stalePrReviewHead &&
17864+
labelSyncFreshEnough();
1776617865
if (state === "open" && isCurrentCompleteReport) {
1776717866
try {
1776817867
const syncResult = syncPriorityLabel({
@@ -17844,7 +17943,9 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1784417943
continue;
1784517944
}
1784617945
}
17847-
reviewComment = renderReviewCommentFromReport(markdown, closeReason ?? "none", renderOptions);
17946+
reviewComment = stalePrReviewHead
17947+
? stalePullRequestReviewComment({ number, stale: stalePrReviewHead })
17948+
: renderReviewCommentFromReport(markdown, closeReason ?? "none", renderOptions);
1784817949
markedReviewComment = markedReviewCommentBody(number, reviewComment);
1784917950
if (isCloseProposal && item.kind === "issue") {
1785017951
currentClosingPullRequests ??= closingPullRequestsForIssue(number);

0 commit comments

Comments
 (0)