Skip to content

Commit b01772a

Browse files
committed
fix: prevent stale review comment sync overwrites
1 parent f2fc64e commit b01772a

2 files changed

Lines changed: 622 additions & 0 deletions

File tree

‎src/clawsweeper.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ type ActionTaken =
245245
| "skipped_comment_auth"
246246
| "skipped_locked_conversation"
247247
| "skipped_changed_since_review"
248+
| "skipped_stale_review_comment_sync"
248249
| "skipped_open_closing_pr"
249250
| "skipped_same_author_pair"
250251
| "skipped_already_closed"
@@ -15415,6 +15416,50 @@ function commentBody(comment: Record<string, unknown> | undefined): string | und
1541515416
return typeof body === "string" ? body : undefined;
1541615417
}
1541715418

15419+
function newestReviewMarkerReviewedAt(
15420+
comment: Record<string, unknown> | undefined,
15421+
number: number,
15422+
): string | undefined {
15423+
if (!canPatchReviewComment(comment)) return undefined;
15424+
const body = commentBody(comment);
15425+
if (!body) return undefined;
15426+
const reviewMarker = reviewCommentMarker(number);
15427+
const reviewMarkerIndex = body.lastIndexOf(reviewMarker);
15428+
if (reviewMarkerIndex < 0) return undefined;
15429+
if (body.slice(reviewMarkerIndex + reviewMarker.length).trim() !== "") return undefined;
15430+
const markerBlock = body
15431+
.slice(0, reviewMarkerIndex)
15432+
.trimEnd()
15433+
.match(/(?:<!--[\s\S]*?-->\s*)+$/)?.[0];
15434+
if (!markerBlock) return undefined;
15435+
const markerPattern = /<!--\s+clawsweeper-verdict:[^\s>]+\b([^>]*)-->/g;
15436+
let lastReviewedAt: string | undefined;
15437+
for (const match of markerBlock.matchAll(markerPattern)) {
15438+
const attributes = match[1] ?? "";
15439+
if (!new RegExp(`\\bitem=${number}\\b`).test(attributes)) continue;
15440+
const reviewedAt = attributes.match(/\breviewed_at=([^\s>]+)/)?.[1];
15441+
if (!reviewedAt || reviewedAt === "unknown") continue;
15442+
lastReviewedAt = reviewedAt;
15443+
}
15444+
return lastReviewedAt;
15445+
}
15446+
15447+
function staleReviewCommentSyncReason(
15448+
markdown: string,
15449+
existingReviewComment: Record<string, unknown> | undefined,
15450+
number: number,
15451+
): string | null {
15452+
if (frontMatterValue(markdown, "type") !== "pull_request") return null;
15453+
// Comment updated_at can move for command/status edits; only review markers prove verdict freshness.
15454+
const liveReviewedAt = newestReviewMarkerReviewedAt(existingReviewComment, number);
15455+
const reportReviewedAt = frontMatterValue(markdown, "reviewed_at");
15456+
const liveReviewedAtMs = timestampMs(liveReviewedAt);
15457+
const reportReviewedAtMs = timestampMs(reportReviewedAt);
15458+
if (liveReviewedAtMs === null || reportReviewedAtMs === null) return null;
15459+
if (liveReviewedAtMs <= reportReviewedAtMs) return null;
15460+
return `live durable review comment is newer than the local report: comment reviewed_at=${liveReviewedAt}, report reviewed_at=${reportReviewedAt}`;
15461+
}
15462+
1541815463
const APPLY_SYNC_EQUIVALENT_CLOSE_MARKER_ACTIONS = new Set([
1541915464
"proposed_close",
1542015465
"kept_open",
@@ -18002,6 +18047,22 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1800218047
? `synced advisory issue labels #${number}`
1800318048
: `synced ClawSweeper labels #${number}`;
1800418049
if (needsReviewCommentSync) {
18050+
const staleSyncReason = needsReviewCommentBodySync
18051+
? staleReviewCommentSyncReason(markdown, existingReviewComment, number)
18052+
: null;
18053+
if (staleSyncReason) {
18054+
markdown = replaceFrontMatterValue(markdown, "apply_checked_at", new Date().toISOString());
18055+
if (!dryRun) writeFileSync(path, markdown, "utf8");
18056+
results.push({
18057+
number,
18058+
action: "skipped_stale_review_comment_sync",
18059+
reason: staleSyncReason,
18060+
});
18061+
processedCount += 1;
18062+
maybeLogProgress(`skipped stale review comment sync #${number}`);
18063+
if (processedCount >= processedLimit) break;
18064+
continue;
18065+
}
1800518066
const lockedReason = needsReviewCommentBodySync ? lockedConversationApplyReason(item) : null;
1800618067
if (lockedReason) {
1800718068
if (markApplySkipped("skipped_locked_conversation", lockedReason)) break;

0 commit comments

Comments
 (0)