Skip to content

Commit df83ab2

Browse files
author
Peter Steinberger
committed
fix: preserve truthful guarded review publication and queue fairness
1 parent d3c22b4 commit df83ab2

10 files changed

Lines changed: 774 additions & 85 deletions

‎src/clawsweeper-apply-close-guards.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export function requiresLockedReviewCommentMutation(
103103
isCloseProposal: options.isCloseProposal,
104104
commentSyncMinAgeDays: options.commentSyncMinAgeDays,
105105
reviewCommentSyncedAt: frontMatterValue(options.markdown, "review_comment_synced_at"),
106+
reviewCommentVerifiedAt: frontMatterValue(options.markdown, "review_comment_checked_at"),
106107
reviewedAt: frontMatterValue(options.markdown, "reviewed_at"),
107108
lastFullReviewAt: frontMatterValue(options.markdown, "last_full_review_at"),
108109
guardedReviewedAt: guarded ? frontMatterValue(options.markdown, "apply_checked_at") : undefined,

‎src/clawsweeper-apply-decision-workflow.ts‎

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,16 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
11831183
state === "open" && item.kind === "pull_request"
11841184
? stalePullRequestReviewHead(markdown, currentItemContext())
11851185
: null;
1186+
const guardedReviewAction = isGuardedApplyReviewAction(action, isLiveRecheckGuardClose);
1187+
const markReviewedSourceDrift = () =>
1188+
markChangedSinceReview({
1189+
...applyReviewedSourceDriftEvidence(dependencies, {
1190+
currentItemContext,
1191+
item,
1192+
storedUpdatedAt,
1193+
}),
1194+
preserveAction: guardedReviewAction ? action : undefined,
1195+
});
11861196
let currentPrStatusKind: PrStatusLabelKind | null = null;
11871197
let lockedMetadataOnly = false;
11881198
if (state === "open") {
@@ -1240,17 +1250,12 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
12401250
continue;
12411251
}
12421252
}
1243-
if (lockedIssueReason && isCloseProposal && !reviewedSourceFresh()) {
1244-
if (
1245-
markChangedSinceReview(
1246-
applyReviewedSourceDriftEvidence(dependencies, {
1247-
currentItemContext,
1248-
item,
1249-
storedUpdatedAt,
1250-
}),
1251-
)
1252-
)
1253-
break;
1253+
if (
1254+
lockedIssueReason &&
1255+
(isCloseProposal || guardedReviewAction) &&
1256+
!reviewedSourceFresh()
1257+
) {
1258+
if (markReviewedSourceDrift()) break;
12541259
continue;
12551260
}
12561261
if (needsLockedCommentMutation) {
@@ -1263,7 +1268,8 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
12631268
currentItemContext,
12641269
dryRun,
12651270
item,
1266-
labelSyncFreshEnough,
1271+
labelSyncFreshEnough: () =>
1272+
labelSyncFreshEnough() && (!guardedReviewAction || reviewedSourceFresh()),
12671273
markdown,
12681274
number,
12691275
onMutation: recordMutation,
@@ -1278,6 +1284,53 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
12781284
if (clawSweeperLabelsChanged && !dryRun) {
12791285
rememberSelfMutationUpdatedAt();
12801286
}
1287+
if (
1288+
state === "open" &&
1289+
isCloseProposal &&
1290+
applyBlockingProtectedLabels(item.labels, closeReason).length > 0
1291+
) {
1292+
if (
1293+
markApplySkipped(
1294+
"skipped_protected_label",
1295+
applyProtectedLabelReason(item.labels, closeReason),
1296+
true,
1297+
)
1298+
)
1299+
break;
1300+
continue;
1301+
}
1302+
const currentAuthorAssociation = normalizeAuthorAssociation(item.authorAssociation);
1303+
const reviewedAuthorAssociation = normalizeAuthorAssociation(storedAuthorAssociation);
1304+
if (
1305+
isCloseProposal &&
1306+
!isVerifiedFixedCloseReason(closeReason) &&
1307+
(isMaintainerAuthorAssociation(currentAuthorAssociation) ||
1308+
isMaintainerAuthorAssociation(reviewedAuthorAssociation))
1309+
) {
1310+
const authorAssociation = isMaintainerAuthorAssociation(currentAuthorAssociation)
1311+
? currentAuthorAssociation
1312+
: reviewedAuthorAssociation;
1313+
markdown = replaceFrontMatterValue(markdown, "author_association", authorAssociation);
1314+
markdown = replaceFrontMatterValue(markdown, "action_taken", "skipped_maintainer_authored");
1315+
if (
1316+
recordApplySkipped(
1317+
"skipped_maintainer_authored",
1318+
`author association is ${authorAssociation}`,
1319+
true,
1320+
)
1321+
)
1322+
break;
1323+
continue;
1324+
}
1325+
if (
1326+
state === "open" &&
1327+
(isCloseProposal || guardedReviewAction) &&
1328+
!stalePrReviewHead &&
1329+
!reviewedSourceFresh()
1330+
) {
1331+
if (markReviewedSourceDrift()) break;
1332+
continue;
1333+
}
12811334
const renderOptions: ReviewCommentRenderOptions = {
12821335
prStatusKind: currentPrStatusKind,
12831336
previousLabels,
@@ -1304,36 +1357,6 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
13041357
reviewComment = renderCurrentReviewComment();
13051358
}
13061359
let markedReviewComment = markedReviewCommentForApply(reviewComment);
1307-
const protectedApplyReason = applyProtectedLabelReason(item.labels, closeReason);
1308-
if (applyBlockingProtectedLabels(item.labels, closeReason).length > 0) {
1309-
if (isCloseProposal) {
1310-
if (markApplySkipped("skipped_protected_label", protectedApplyReason, true)) break;
1311-
}
1312-
if (isCloseProposal) continue;
1313-
}
1314-
const currentAuthorAssociation = normalizeAuthorAssociation(item.authorAssociation);
1315-
const reviewedAuthorAssociation = normalizeAuthorAssociation(storedAuthorAssociation);
1316-
if (
1317-
isCloseProposal &&
1318-
!isVerifiedFixedCloseReason(closeReason) &&
1319-
(isMaintainerAuthorAssociation(currentAuthorAssociation) ||
1320-
isMaintainerAuthorAssociation(reviewedAuthorAssociation))
1321-
) {
1322-
const authorAssociation = isMaintainerAuthorAssociation(currentAuthorAssociation)
1323-
? currentAuthorAssociation
1324-
: reviewedAuthorAssociation;
1325-
markdown = replaceFrontMatterValue(markdown, "author_association", authorAssociation);
1326-
markdown = replaceFrontMatterValue(markdown, "action_taken", "skipped_maintainer_authored");
1327-
if (
1328-
recordApplySkipped(
1329-
"skipped_maintainer_authored",
1330-
`author association is ${authorAssociation}`,
1331-
true,
1332-
)
1333-
)
1334-
break;
1335-
continue;
1336-
}
13371360
const { postProofCoveringPrFreshnessBlock, postProofFreshnessBlock } =
13381361
createApplyProofFreshnessGuards({
13391362
...dependencies,
@@ -1407,19 +1430,6 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
14071430
break;
14081431
continue;
14091432
}
1410-
if (isCloseProposal && !reviewedSourceFresh()) {
1411-
if (
1412-
markChangedSinceReview(
1413-
applyReviewedSourceDriftEvidence(dependencies, {
1414-
currentItemContext,
1415-
item,
1416-
storedUpdatedAt,
1417-
}),
1418-
)
1419-
)
1420-
break;
1421-
continue;
1422-
}
14231433
const labelsCanSync = !lockedMetadataOnly && !stalePrReviewHead && labelSyncFreshEnough();
14241434
const complete = frontMatterValue(markdown, "review_status") === "complete" && labelsCanSync;
14251435
const reportLabelSync = syncApplyReportLabels(dependencies, {
@@ -1483,14 +1493,15 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
14831493
/^(?:none|unknown)?$/.test(frontMatterValue(markdown, "review_comment_id") ?? "") ||
14841494
/^(?:none|unknown)?$/.test(frontMatterValue(markdown, "review_comment_url") ?? "");
14851495
const guarded =
1486-
isGuardedApplyReviewAction(action, isLiveRecheckGuardClose) &&
1496+
guardedReviewAction &&
14871497
reviewedSourceFresh() &&
14881498
!stalePrReviewHead;
14891499
let needsReviewCommentSync = shouldSyncReviewComment({
14901500
syncCommentsOnly,
14911501
isCloseProposal,
14921502
commentSyncMinAgeDays,
14931503
reviewCommentSyncedAt: frontMatterValue(markdown, "review_comment_synced_at"),
1504+
reviewCommentVerifiedAt: frontMatterValue(markdown, "review_comment_checked_at"),
14941505
reviewedAt: frontMatterValue(markdown, "reviewed_at"),
14951506
lastFullReviewAt: frontMatterValue(markdown, "last_full_review_at"),
14961507
guardedReviewedAt: guarded ? frontMatterValue(markdown, "apply_checked_at") : undefined,

‎src/clawsweeper-apply-dependencies.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ export interface CreateApplyDecisionWorkflowDependencies {
426426
isCloseProposal: boolean;
427427
commentSyncMinAgeDays: number;
428428
reviewCommentSyncedAt: string | undefined;
429+
reviewCommentVerifiedAt?: string | undefined;
429430
reviewedAt?: string | undefined;
430431
lastFullReviewAt?: string | undefined;
431432
guardedReviewedAt?: string | undefined;

‎src/clawsweeper-apply-source-freshness.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,23 @@ export function createApplyChangedSinceReviewMarker(
6666
reason,
6767
currentUpdatedAt,
6868
currentSnapshotHash,
69+
currentLabels,
70+
preserveAction,
6971
}: {
7072
reason: string;
7173
currentUpdatedAt?: string | undefined;
7274
currentSnapshotHash?: string | undefined;
75+
currentLabels?: string[] | undefined;
76+
preserveAction?: string | undefined;
7377
}): boolean => {
7478
let markdown = replaceFrontMatterValue(
7579
options.getMarkdown(),
7680
"action_taken",
77-
"skipped_changed_since_review",
81+
preserveAction ?? "skipped_changed_since_review",
7882
);
83+
if (currentLabels) {
84+
markdown = replaceFrontMatterValue(markdown, "labels", JSON.stringify(currentLabels));
85+
}
7986
if (currentUpdatedAt) {
8087
markdown = replaceFrontMatterValue(markdown, "current_item_updated_at", currentUpdatedAt);
8188
}
@@ -109,12 +116,22 @@ export function applyReviewedSourceDriftEvidence(
109116
item: Item;
110117
storedUpdatedAt: string | undefined;
111118
},
112-
): { reason: string; currentUpdatedAt?: string; currentSnapshotHash?: string } {
119+
): {
120+
reason: string;
121+
currentUpdatedAt?: string;
122+
currentSnapshotHash?: string;
123+
currentLabels: string[];
124+
} {
113125
return options.storedUpdatedAt
114-
? { reason: "updated_at changed", currentUpdatedAt: options.item.updatedAt }
126+
? {
127+
reason: "updated_at changed",
128+
currentUpdatedAt: options.item.updatedAt,
129+
currentLabels: options.item.labels,
130+
}
115131
: {
116132
reason: "snapshot changed",
117133
currentSnapshotHash: itemSnapshotHash(options.item, options.currentItemContext()),
134+
currentLabels: options.item.labels,
118135
};
119136
}
120137

‎src/clawsweeper-record-metadata.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export function createRecordMetadata({
188188
isCloseProposal: boolean;
189189
commentSyncMinAgeDays: number;
190190
reviewCommentSyncedAt: string | undefined;
191+
reviewCommentVerifiedAt?: string | undefined;
191192
reviewedAt?: string | undefined;
192193
lastFullReviewAt?: string | undefined;
193194
guardedReviewedAt?: string | undefined;
@@ -199,6 +200,11 @@ export function createRecordMetadata({
199200
now?: number;
200201
}): boolean {
201202
const syncedAt = Date.parse(options.reviewCommentSyncedAt ?? "");
203+
const verifiedAt = Date.parse(options.reviewCommentVerifiedAt ?? "");
204+
const confirmedAt = Math.max(
205+
Number.isFinite(syncedAt) ? syncedAt : 0,
206+
Number.isFinite(verifiedAt) ? verifiedAt : 0,
207+
);
202208
const reviewedAt = Math.max(
203209
Date.parse(options.reviewedAt ?? "") || 0,
204210
Date.parse(options.lastFullReviewAt ?? "") || 0,
@@ -208,8 +214,8 @@ export function createRecordMetadata({
208214
options.syncCommentsOnly &&
209215
options.hasExistingReviewComment &&
210216
(!Number.isFinite(syncedAt) ||
211-
reviewedAt > syncedAt ||
212-
(options.now ?? Date.now()) - syncedAt >= 7 * 24 * 60 * 60 * 1_000);
217+
reviewedAt > confirmedAt ||
218+
(options.now ?? Date.now()) - confirmedAt >= 7 * 24 * 60 * 60 * 1_000);
213219
if (
214220
!options.needsReviewCommentBodySync &&
215221
!options.needsReviewCommentHashSync &&

‎src/clawsweeper-review-comments-workflow.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,13 @@ export function createReviewCommentWorkflow({
11211121
const url = commentUrl(comment);
11221122
if (id !== null) next = replaceFrontMatterValue(next, "review_comment_id", String(id));
11231123
if (url) next = replaceFrontMatterValue(next, "review_comment_url", url);
1124-
next = replaceFrontMatterValue(next, "review_comment_synced_at", new Date().toISOString());
1124+
const checkedAt = new Date().toISOString();
1125+
next = replaceFrontMatterValue(
1126+
next,
1127+
"review_comment_synced_at",
1128+
commentUpdatedAt(comment) ?? checkedAt,
1129+
);
1130+
next = replaceFrontMatterValue(next, "review_comment_checked_at", checkedAt);
11251131
return next;
11261132
}
11271133

‎src/repair/workflow-utils.ts‎

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,7 +2245,9 @@ function commentSyncCandidates(
22452245
if (repoFor(markdown, name) !== targetRepo) return [];
22462246
const type = frontMatterValue(markdown, "type");
22472247
if (applyKind !== "all" && type !== applyKind) return [];
2248-
if (frontMatterValue(markdown, "review_status") !== "complete") return [];
2248+
const reviewStatus = frontMatterValue(markdown, "review_status");
2249+
const failedReview = reviewStatus === "failed";
2250+
if (reviewStatus !== "complete" && !failedReview) return [];
22492251
if (!frontMatterValue(markdown, "item_snapshot_hash")) return [];
22502252
const actionTaken = frontMatterValue(markdown, "action_taken");
22512253
if (actionTaken === "skipped_invalid_decision") {
@@ -2286,18 +2288,46 @@ function commentSyncCandidates(
22862288
const invalidReviewCommentHash =
22872289
!reviewCommentHash || !/^[a-f\d]{64}$/i.test(reviewCommentHash);
22882290
const syncedAt = Date.parse(frontMatterValue(markdown, "review_comment_synced_at") ?? "");
2291+
const hasSyncedTimestamp = Number.isFinite(syncedAt);
2292+
const verifiedAt = Date.parse(frontMatterValue(markdown, "review_comment_checked_at") ?? "");
2293+
const commentConfirmedAt = Math.max(
2294+
hasSyncedTimestamp ? syncedAt : 0,
2295+
Number.isFinite(verifiedAt) ? verifiedAt : 0,
2296+
);
2297+
const guardedSourceDrift =
2298+
guardedReview &&
2299+
Boolean(
2300+
frontMatterValue(markdown, "current_item_updated_at") ||
2301+
frontMatterValue(markdown, "current_item_snapshot_hash"),
2302+
);
22892303
const reviewedAt = Math.max(
22902304
Date.parse(frontMatterValue(markdown, "last_full_review_at") || "") || 0,
22912305
Date.parse(frontMatterValue(markdown, "reviewed_at") || "") || 0,
2292-
guardedReview ? Date.parse(frontMatterValue(markdown, "apply_checked_at") || "") || 0 : 0,
2306+
guardedReview && !guardedSourceDrift
2307+
? Date.parse(frontMatterValue(markdown, "apply_checked_at") || "") || 0
2308+
: 0,
22932309
);
2294-
const freshlyReviewedSinceSync = Number.isFinite(syncedAt) && reviewedAt > syncedAt;
2310+
const freshlyReviewedSinceSync = commentConfirmedAt > 0 && reviewedAt > commentConfirmedAt;
2311+
if (
2312+
failedReview &&
2313+
hasStoredReviewComment &&
2314+
!requiresDurableCommentRepair &&
2315+
hasSyncedTimestamp &&
2316+
commentConfirmedAt > 0 &&
2317+
!freshlyReviewedSinceSync &&
2318+
Date.now() - commentConfirmedAt < 7 * 24 * 60 * 60 * 1000
2319+
) {
2320+
return [];
2321+
}
22952322
if (
22962323
guardedReview &&
2324+
type === "issue" &&
22972325
hasStoredReviewComment &&
22982326
!invalidReviewCommentHash &&
2299-
Number.isFinite(syncedAt) &&
2300-
!freshlyReviewedSinceSync
2327+
hasSyncedTimestamp &&
2328+
commentConfirmedAt > 0 &&
2329+
!freshlyReviewedSinceSync &&
2330+
(!failedReview || Date.now() - commentConfirmedAt < 7 * 24 * 60 * 60 * 1000)
23012331
) {
23022332
return [];
23032333
}
@@ -2306,12 +2336,13 @@ function commentSyncCandidates(
23062336
automaticAllItemCursor &&
23072337
type === "issue" &&
23082338
hasStoredReviewComment &&
2309-
!requiresDurableCommentRepair
2339+
!requiresDurableCommentRepair &&
2340+
hasSyncedTimestamp
23102341
) {
23112342
if (
2312-
Number.isFinite(syncedAt) &&
2313-
(!Number.isFinite(reviewedAt) || reviewedAt <= syncedAt) &&
2314-
Date.now() - syncedAt < 7 * 24 * 60 * 60 * 1000
2343+
commentConfirmedAt > 0 &&
2344+
(!Number.isFinite(reviewedAt) || reviewedAt <= commentConfirmedAt) &&
2345+
Date.now() - commentConfirmedAt < 7 * 24 * 60 * 60 * 1000
23152346
) {
23162347
return [];
23172348
}
@@ -2333,7 +2364,8 @@ function commentSyncCandidates(
23332364
verifiedLocalCheckout &&
23342365
(!hasStoredReviewComment ||
23352366
invalidReviewCommentHash ||
2336-
(guardedReview && !Number.isFinite(syncedAt)) ||
2367+
(!hasSyncedTimestamp && Number.isFinite(verifiedAt)) ||
2368+
(guardedReview && commentConfirmedAt <= 0) ||
23372369
((actionTaken === "kept_open" || actionTaken === "proposed_close" || guardedReview) &&
23382370
freshlyReviewedSinceSync))
23392371
) {

0 commit comments

Comments
 (0)