Skip to content

Commit 54e6e47

Browse files
committed
fix(review): fence hot intake exact snapshots
1 parent 5d8cf42 commit 54e6e47

3 files changed

Lines changed: 171 additions & 10 deletions

File tree

‎src/clawsweeper.ts‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,6 +4463,7 @@ type HotIntakeExactReviewSnapshot = {
44634463
sourceRevision: string;
44644464
pullStateDigest: string;
44654465
reviewActivityCursor: string;
4466+
itemUpdatedAt: string;
44664467
};
44674468

44684469
function hotIntakeExactReviewSnapshotFromReport(
@@ -4482,26 +4483,35 @@ function hotIntakeExactReviewSnapshotFromReport(
44824483
const sourceRevision = review.itemSourceRevision?.trim();
44834484
const pullStateDigest = frontMatterValue(review.markdown, "reviewed_pull_state_digest");
44844485
const reviewActivityCursor = frontMatterValue(review.markdown, "review_activity_cursor");
4486+
const itemUpdatedAt = review.itemUpdatedAt?.trim();
44854487
if (
44864488
!headSha ||
44874489
!sourceRevision ||
44884490
sourceRevision === "unknown" ||
44894491
!pullStateDigest ||
44904492
pullStateDigest === "unknown" ||
44914493
pullStateDigest === "none" ||
4492-
!isReviewedPrActivityCursor(reviewActivityCursor)
4494+
!isReviewedPrActivityCursor(reviewActivityCursor) ||
4495+
!itemUpdatedAt
44934496
) {
44944497
return null;
44954498
}
4496-
return { headSha, sourceRevision, pullStateDigest, reviewActivityCursor };
4499+
return { headSha, sourceRevision, pullStateDigest, reviewActivityCursor, itemUpdatedAt };
44974500
}
44984501

44994502
function currentHotIntakePullReviewSnapshot(item: Item): HotIntakeExactReviewSnapshot | null {
45004503
try {
4504+
const reviewActivityCursor = readStableReviewedPrActivityCursor(() =>
4505+
fetchReviewedPrActivityCursor(item.number),
4506+
);
4507+
if (!reviewActivityCursor) return null;
4508+
const comments = ghPaged<unknown>(`repos/${item.repo}/issues/${item.number}/comments`);
45014509
const pull = ghJson<unknown>(["api", `repos/${item.repo}/pulls/${item.number}`]);
45024510
const source = asRecord(pull);
45034511
const headSha = stringOrUndefined(asRecord(source.head).sha)?.trim().toLowerCase();
45044512
if (!headSha) return null;
4513+
const itemUpdatedAt = stringOrUndefined(source.updated_at)?.trim();
4514+
if (!itemUpdatedAt) return null;
45054515
const baseSha = stringOrUndefined(asRecord(source.base).sha)?.trim().toLowerCase();
45064516
const draft = source.draft;
45074517
const mergeable = source.mergeable;
@@ -4534,16 +4544,16 @@ function currentHotIntakePullReviewSnapshot(item: Item): HotIntakeExactReviewSna
45344544
commitCount,
45354545
});
45364546
if (!pullStateDigest) return null;
4537-
const reviewActivityCursor = readStableReviewedPrActivityCursor(() =>
4547+
const revalidatedReviewActivityCursor = readStableReviewedPrActivityCursor(() =>
45384548
fetchReviewedPrActivityCursor(item.number),
45394549
);
4540-
if (!reviewActivityCursor) return null;
4541-
const comments = ghPaged<unknown>(`repos/${item.repo}/issues/${item.number}/comments`);
4550+
if (revalidatedReviewActivityCursor !== reviewActivityCursor) return null;
45424551
return {
45434552
headSha,
45444553
sourceRevision: itemSourceRevisionSha256(pull, comments),
45454554
pullStateDigest,
45464555
reviewActivityCursor,
4556+
itemUpdatedAt,
45474557
};
45484558
} catch (error) {
45494559
console.error(
@@ -4584,6 +4594,11 @@ function shouldSkipScheduledHotIntakeExactReview(
45844594
const current = currentHotIntakePullReviewSnapshot(item);
45854595
return (
45864596
current !== null &&
4597+
(current.itemUpdatedAt === reviewed.itemUpdatedAt ||
4598+
!hasUncapturedActivitySinceExactReview(
4599+
{ ...item, updatedAt: current.itemUpdatedAt },
4600+
review,
4601+
)) &&
45874602
current.headSha === reviewed.headSha &&
45884603
current.sourceRevision === reviewed.sourceRevision &&
45894604
current.pullStateDigest === reviewed.pullStateDigest &&
@@ -4602,6 +4617,7 @@ export function shouldSkipScheduledHotIntakeExactReviewForTest(options: {
46024617
currentSourceRevision?: string;
46034618
currentPullStateDigest?: string;
46044619
currentReviewActivityCursor?: string;
4620+
currentItemUpdatedAt?: string;
46054621
itemUpdatedAt?: string;
46064622
reviewItemUpdatedAt?: string;
46074623
reviewCommentSyncedAt?: string;
@@ -4632,11 +4648,17 @@ export function shouldSkipScheduledHotIntakeExactReviewForTest(options: {
46324648
!options.currentHeadSha ||
46334649
!options.currentSourceRevision ||
46344650
!options.currentPullStateDigest ||
4635-
!options.currentReviewActivityCursor
4651+
!options.currentReviewActivityCursor ||
4652+
!options.currentItemUpdatedAt
46364653
) {
46374654
return false;
46384655
}
46394656
return (
4657+
(options.currentItemUpdatedAt === reviewed.itemUpdatedAt ||
4658+
!hasUncapturedActivitySinceExactReview(
4659+
{ kind: "pull_request", updatedAt: options.currentItemUpdatedAt } as Item,
4660+
review,
4661+
)) &&
46404662
reviewed.headSha === options.currentHeadSha.trim().toLowerCase() &&
46414663
reviewed.sourceRevision === options.currentSourceRevision.trim() &&
46424664
reviewed.pullStateDigest === options.currentPullStateDigest.trim() &&

‎test/command.test.ts‎

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test("local exact reviews default to item-specific artifacts", () => {
100100
assert.equal(defaultReviewArtifactDirForTest(false, 357, undefined), "artifacts/reviews");
101101
});
102102

103-
test("CSW-088 scheduled hot planning suppresses #117063 while explicit re-review remains eligible", () => {
103+
test("CSW-088 scheduled hot planning suppresses #117063, observes an in-flight update, and leaves explicit re-review eligible", () => {
104104
const root = mkdtempSync(join(tmpdir(), "cmd-csw-088-"));
105105
const binDir = join(root, "bin");
106106
const itemsDir = join(root, "items");
@@ -123,6 +123,12 @@ test("CSW-088 scheduled hot planning suppresses #117063 while explicit re-review
123123
deletions: 2,
124124
changed_files: 2,
125125
commits: 1,
126+
updated_at: reviewedAt,
127+
};
128+
const updatedPull = { ...pull, body: "The PR body changed during the snapshot read." };
129+
const commentUpdatedPull = {
130+
...pull,
131+
updated_at: new Date(Date.parse(reviewedAt) + 1).toISOString(),
126132
};
127133
const issue = {
128134
number,
@@ -177,6 +183,7 @@ test("CSW-088 scheduled hot planning suppresses #117063 while explicit re-review
177183
title: pull.title,
178184
reviewed_at: reviewedAt,
179185
review_policy: reviewPolicyHashForTest(),
186+
item_updated_at: reviewedAt,
180187
item_source_revision: sourceRevision,
181188
pull_head_sha: headSha,
182189
reviewed_pull_state_digest: pullStateDigest,
@@ -198,6 +205,15 @@ const path = args[1] || "";
198205
const listItem = ${JSON.stringify(listItem)};
199206
const issue = ${JSON.stringify(issue)};
200207
const pull = ${JSON.stringify(pull)};
208+
const updatedPull = ${JSON.stringify(updatedPull)};
209+
const commentUpdatedPull = ${JSON.stringify(commentUpdatedPull)};
210+
const raceMarker = process.env.CSW_RACE_MARKER;
211+
const race = process.env.CSW_RACE === "1";
212+
const activityRaceMarker = process.env.CSW_ACTIVITY_RACE_MARKER;
213+
const activityRace = process.env.CSW_ACTIVITY_RACE === "1";
214+
const commentRaceMarker = process.env.CSW_COMMENT_RACE_MARKER;
215+
const commentRace = process.env.CSW_COMMENT_RACE === "1";
216+
const updatedReview = { id: 1, user: { login: "reviewer" }, state: "COMMENTED", body: "new review activity", submitted_at: "2026-07-31T00:00:00Z", commit_id: pull.head.sha };
201217
if (args[0] === "api" && /issues\\?state=open/.test(path)) {
202218
console.log(JSON.stringify(listItem));
203219
process.exit(0);
@@ -207,17 +223,24 @@ if (args[0] === "api" && path === "repos/openclaw/openclaw/issues/${number}") {
207223
process.exit(0);
208224
}
209225
if (args[0] === "api" && path === "repos/openclaw/openclaw/pulls/${number}") {
210-
console.log(JSON.stringify(pull));
226+
if (activityRace && activityRaceMarker) require("node:fs").writeFileSync(activityRaceMarker, "seen");
227+
console.log(JSON.stringify(race && raceMarker && require("node:fs").existsSync(raceMarker) ? updatedPull : commentRace && commentRaceMarker && require("node:fs").existsSync(commentRaceMarker) ? commentUpdatedPull : pull));
211228
process.exit(0);
212229
}
213-
if (args[0] === "api" && /(issues|pulls)\\/${number}\\/comments/.test(path)) {
230+
if (args[0] === "api" && path.startsWith("repos/openclaw/openclaw/issues/${number}/comments")) {
231+
if (commentRace && commentRaceMarker) require("node:fs").writeFileSync(commentRaceMarker, "seen");
214232
console.log(JSON.stringify([[]]));
215233
process.exit(0);
216234
}
217-
if (args[0] === "api" && /pulls\\/${number}\\/reviews/.test(path)) {
235+
if (args[0] === "api" && path.startsWith("repos/openclaw/openclaw/pulls/${number}/comments")) {
236+
if (race && raceMarker) require("node:fs").writeFileSync(raceMarker, "seen");
218237
console.log(JSON.stringify([[]]));
219238
process.exit(0);
220239
}
240+
if (args[0] === "api" && /pulls\\/${number}\\/reviews/.test(path)) {
241+
console.log(JSON.stringify(activityRace && activityRaceMarker && require("node:fs").existsSync(activityRaceMarker) ? [[updatedReview]] : [[]]));
242+
process.exit(0);
243+
}
221244
console.error("unexpected gh args " + JSON.stringify(args));
222245
process.exit(1);
223246
`,
@@ -248,6 +271,105 @@ process.exit(1);
248271
assert.equal(scheduled.status, 0, scheduled.stderr);
249272
assert.deepEqual(JSON.parse(scheduled.stdout).candidates, []);
250273

274+
const raced = spawnSync(
275+
process.execPath,
276+
[
277+
CLI,
278+
"plan",
279+
"--target-repo",
280+
"openclaw/openclaw",
281+
"--items-dir",
282+
itemsDir,
283+
"--coverage-tracked-items-manifest",
284+
coverageManifest,
285+
"--hot-intake",
286+
"--max-pages",
287+
"1",
288+
"--batch-size",
289+
"1",
290+
"--shard-count",
291+
"1",
292+
],
293+
{ encoding: "utf8", env: { ...env, CSW_RACE: "1", CSW_RACE_MARKER: join(root, "race") } },
294+
);
295+
assert.equal(raced.status, 0, raced.stderr);
296+
assert.deepEqual(
297+
JSON.parse(raced.stdout).candidates.map((candidate: { number: number }) => candidate.number),
298+
[number],
299+
);
300+
301+
const commentRaced = spawnSync(
302+
process.execPath,
303+
[
304+
CLI,
305+
"plan",
306+
"--target-repo",
307+
"openclaw/openclaw",
308+
"--items-dir",
309+
itemsDir,
310+
"--coverage-tracked-items-manifest",
311+
coverageManifest,
312+
"--hot-intake",
313+
"--max-pages",
314+
"1",
315+
"--batch-size",
316+
"1",
317+
"--shard-count",
318+
"1",
319+
],
320+
{
321+
encoding: "utf8",
322+
env: {
323+
...env,
324+
CSW_COMMENT_RACE: "1",
325+
CSW_COMMENT_RACE_MARKER: join(root, "comment-race"),
326+
},
327+
},
328+
);
329+
assert.equal(commentRaced.status, 0, commentRaced.stderr);
330+
assert.deepEqual(
331+
JSON.parse(commentRaced.stdout).candidates.map(
332+
(candidate: { number: number }) => candidate.number,
333+
),
334+
[number],
335+
);
336+
337+
const activityRaced = spawnSync(
338+
process.execPath,
339+
[
340+
CLI,
341+
"plan",
342+
"--target-repo",
343+
"openclaw/openclaw",
344+
"--items-dir",
345+
itemsDir,
346+
"--coverage-tracked-items-manifest",
347+
coverageManifest,
348+
"--hot-intake",
349+
"--max-pages",
350+
"1",
351+
"--batch-size",
352+
"1",
353+
"--shard-count",
354+
"1",
355+
],
356+
{
357+
encoding: "utf8",
358+
env: {
359+
...env,
360+
CSW_ACTIVITY_RACE: "1",
361+
CSW_ACTIVITY_RACE_MARKER: join(root, "activity-race"),
362+
},
363+
},
364+
);
365+
assert.equal(activityRaced.status, 0, activityRaced.stderr);
366+
assert.deepEqual(
367+
JSON.parse(activityRaced.stdout).candidates.map(
368+
(candidate: { number: number }) => candidate.number,
369+
),
370+
[number],
371+
);
372+
251373
const explicit = spawnSync(
252374
process.execPath,
253375
[

‎test/scheduler-policy.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ test("CSW-088 suppresses only the immediate same-head and same-body hot-intake r
737737
currentPullStateDigest: pullStateDigest,
738738
currentReviewActivityCursor: reviewActivityCursor,
739739
itemUpdatedAt: reviewedAt,
740+
reviewItemUpdatedAt: reviewedAt,
741+
currentItemUpdatedAt: reviewedAt,
740742
now,
741743
};
742744

@@ -769,6 +771,21 @@ test("CSW-088 suppresses only the immediate same-head and same-body hot-intake r
769771
}),
770772
false,
771773
);
774+
assert.equal(
775+
shouldSkipScheduledHotIntakeExactReviewForTest({
776+
...sameSnapshot,
777+
currentItemUpdatedAt: new Date(Date.parse(reviewedAt) + 1).toISOString(),
778+
}),
779+
false,
780+
);
781+
assert.equal(
782+
shouldSkipScheduledHotIntakeExactReviewForTest({
783+
...sameSnapshot,
784+
currentItemUpdatedAt: new Date(Date.parse(reviewedAt) + 1).toISOString(),
785+
reviewCommentSyncedAt: new Date(Date.parse(reviewedAt) + 1).toISOString(),
786+
}),
787+
true,
788+
);
772789
assert.equal(
773790
shouldSkipScheduledHotIntakeExactReviewForTest({
774791
...sameSnapshot,

0 commit comments

Comments
 (0)