Skip to content

Commit f7e8b13

Browse files
authored
[codex] Prevent stale review comment sync overwrites (#389)
Summary: - The PR adds a pull-request-only live-review freshness guard to apply/comment-sync and regression tests for stale overwrite and forged marker cases. - Reproducibility: yes. from source inspection: current main can reach upsertReviewComment from an older repor ... ffers from a newer durable PR review comment. I did not run the new tests because this review is read-only. Automerge notes: - No ClawSweeper repair was needed after automerge opt-in. Validation: - ClawSweeper review passed for head b01772a. - Required merge gates passed before the squash merge. Prepared head SHA: b01772a Review: #389 (comment) Co-authored-by: masonxhuang <[email protected]> Approved-by: hxy91819
1 parent b1e238d commit f7e8b13

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"
@@ -15485,6 +15486,50 @@ function commentBody(comment: Record<string, unknown> | undefined): string | und
1548515486
return typeof body === "string" ? body : undefined;
1548615487
}
1548715488

15489+
function newestReviewMarkerReviewedAt(
15490+
comment: Record<string, unknown> | undefined,
15491+
number: number,
15492+
): string | undefined {
15493+
if (!canPatchReviewComment(comment)) return undefined;
15494+
const body = commentBody(comment);
15495+
if (!body) return undefined;
15496+
const reviewMarker = reviewCommentMarker(number);
15497+
const reviewMarkerIndex = body.lastIndexOf(reviewMarker);
15498+
if (reviewMarkerIndex < 0) return undefined;
15499+
if (body.slice(reviewMarkerIndex + reviewMarker.length).trim() !== "") return undefined;
15500+
const markerBlock = body
15501+
.slice(0, reviewMarkerIndex)
15502+
.trimEnd()
15503+
.match(/(?:<!--[\s\S]*?-->\s*)+$/)?.[0];
15504+
if (!markerBlock) return undefined;
15505+
const markerPattern = /<!--\s+clawsweeper-verdict:[^\s>]+\b([^>]*)-->/g;
15506+
let lastReviewedAt: string | undefined;
15507+
for (const match of markerBlock.matchAll(markerPattern)) {
15508+
const attributes = match[1] ?? "";
15509+
if (!new RegExp(`\\bitem=${number}\\b`).test(attributes)) continue;
15510+
const reviewedAt = attributes.match(/\breviewed_at=([^\s>]+)/)?.[1];
15511+
if (!reviewedAt || reviewedAt === "unknown") continue;
15512+
lastReviewedAt = reviewedAt;
15513+
}
15514+
return lastReviewedAt;
15515+
}
15516+
15517+
function staleReviewCommentSyncReason(
15518+
markdown: string,
15519+
existingReviewComment: Record<string, unknown> | undefined,
15520+
number: number,
15521+
): string | null {
15522+
if (frontMatterValue(markdown, "type") !== "pull_request") return null;
15523+
// Comment updated_at can move for command/status edits; only review markers prove verdict freshness.
15524+
const liveReviewedAt = newestReviewMarkerReviewedAt(existingReviewComment, number);
15525+
const reportReviewedAt = frontMatterValue(markdown, "reviewed_at");
15526+
const liveReviewedAtMs = timestampMs(liveReviewedAt);
15527+
const reportReviewedAtMs = timestampMs(reportReviewedAt);
15528+
if (liveReviewedAtMs === null || reportReviewedAtMs === null) return null;
15529+
if (liveReviewedAtMs <= reportReviewedAtMs) return null;
15530+
return `live durable review comment is newer than the local report: comment reviewed_at=${liveReviewedAt}, report reviewed_at=${reportReviewedAt}`;
15531+
}
15532+
1548815533
const APPLY_SYNC_EQUIVALENT_CLOSE_MARKER_ACTIONS = new Set([
1548915534
"proposed_close",
1549015535
"kept_open",
@@ -18103,6 +18148,22 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1810318148
? `synced advisory issue labels #${number}`
1810418149
: `synced ClawSweeper labels #${number}`;
1810518150
if (needsReviewCommentSync) {
18151+
const staleSyncReason = needsReviewCommentBodySync
18152+
? staleReviewCommentSyncReason(markdown, existingReviewComment, number)
18153+
: null;
18154+
if (staleSyncReason) {
18155+
markdown = replaceFrontMatterValue(markdown, "apply_checked_at", new Date().toISOString());
18156+
if (!dryRun) writeFileSync(path, markdown, "utf8");
18157+
results.push({
18158+
number,
18159+
action: "skipped_stale_review_comment_sync",
18160+
reason: staleSyncReason,
18161+
});
18162+
processedCount += 1;
18163+
maybeLogProgress(`skipped stale review comment sync #${number}`);
18164+
if (processedCount >= processedLimit) break;
18165+
continue;
18166+
}
1810618167
const lockedReason = needsReviewCommentBodySync ? lockedConversationApplyReason(item) : null;
1810718168
if (lockedReason) {
1810818169
if (markApplySkipped("skipped_locked_conversation", lockedReason)) break;

0 commit comments

Comments
 (0)