Skip to content

Commit 2556577

Browse files
committed
fix(publication): terminalize verified stale artifacts
1 parent afe9762 commit 2556577

4 files changed

Lines changed: 127 additions & 22 deletions

File tree

‎src/repair/event-apply-proof.ts‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const GUARDED_OPEN_ACTIONS = new Set([
3232
]);
3333

3434
const LEGACY_TUPLELESS_REVIEW_LEASE_REASON = "local report has no durable lease identity";
35+
const NEWER_DURABLE_REVIEW_TUPLE_REASON =
36+
/^live durable review tuple is newer than the local report: comment lease=([1-9][0-9]*), report lease=([1-9][0-9]*)$/;
3537
// The durable queue rejects retry deadlines beyond two hours. Leave a small
3638
// margin for worker/queue clock skew; a still-active long lease is sampled
3739
// again at this bounded deadline instead of rejecting the completion.
@@ -91,6 +93,7 @@ export function exactEventRoutingDeferred({
9193

9294
export type ExactEventApplyDisposition =
9395
| "applied"
96+
| "superseded"
9497
| "terminal_policy_noop"
9598
| "source_drift"
9699
| "close_coverage_deferred"
@@ -138,6 +141,9 @@ export function exactEventApplyProof(
138141
(entry) => entry.action === "skipped_changed_since_review",
139142
);
140143
const hasSourceDrift = sourceDriftActions.length > 0;
144+
const hasStaleReviewTuple = exactActions.some(
145+
(entry) => entry.action === "skipped_stale_review_comment_sync",
146+
);
141147
const sourceDrift =
142148
hasSourceDrift &&
143149
sourceDriftActions.every((entry) => entry.sourceDriftVerified) &&
@@ -160,6 +166,10 @@ export function exactEventApplyProof(
160166
soleExactAction === "kept_open" && soleExactResult?.activeReviewLeaseVerified === true
161167
? normalizedReviewLeaseRetryAt(soleExactResult.activeReviewLeaseExpiresAt)
162168
: null;
169+
const supersededByNewerDurableReviewTuple =
170+
soleExactAction === "skipped_stale_review_comment_sync" &&
171+
soleExactResult !== null &&
172+
hasStrictlyNewerDurableReviewTuple(soleExactResult.reason);
163173
return {
164174
exactActions,
165175
syncedCount,
@@ -182,16 +192,32 @@ export function exactEventApplyProof(
182192
? sourceDrift
183193
? "source_drift"
184194
: "unproven"
185-
: closeCoverageDeferred
186-
? "close_coverage_deferred"
187-
: terminalPolicyNoop
188-
? "terminal_policy_noop"
189-
: syncedCount + terminalCount > 0
190-
? "applied"
191-
: "unproven",
195+
: hasStaleReviewTuple
196+
? supersededByNewerDurableReviewTuple
197+
? "superseded"
198+
: "unproven"
199+
: closeCoverageDeferred
200+
? "close_coverage_deferred"
201+
: terminalPolicyNoop
202+
? "terminal_policy_noop"
203+
: syncedCount + terminalCount > 0
204+
? "applied"
205+
: "unproven",
192206
};
193207
}
194208

209+
function hasStrictlyNewerDurableReviewTuple(reason: string): boolean {
210+
const match = NEWER_DURABLE_REVIEW_TUPLE_REASON.exec(reason);
211+
if (!match) return false;
212+
const liveLeaseCommentId = Number(match[1]);
213+
const reportLeaseCommentId = Number(match[2]);
214+
return (
215+
Number.isSafeInteger(liveLeaseCommentId) &&
216+
Number.isSafeInteger(reportLeaseCommentId) &&
217+
liveLeaseCommentId > reportLeaseCommentId
218+
);
219+
}
220+
195221
function normalizedReviewLeaseRetryAt(value: string): string | null {
196222
const timestamp = Date.parse(value);
197223
return Number.isFinite(timestamp)

‎src/repair/publish-event-result.ts‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ async function publishEventResult(options: EventOptions): Promise<void> {
229229
exactEventPublication: options.exactEventPublication,
230230
legacyTuplelessReviewLease,
231231
});
232+
const summary = () =>
233+
writeSummary({
234+
targetRepo: options.targetRepo,
235+
itemNumber: options.itemNumber,
236+
syncedCount,
237+
closedCount,
238+
missingCount,
239+
closeReasons: options.closeReasons,
240+
});
232241
const rateLimitYield = exactActions.find(
233242
(action) =>
234243
action.action === "skipped_runtime_budget" &&
@@ -242,6 +251,17 @@ async function publishEventResult(options: EventOptions): Promise<void> {
242251
`Requeueing ${options.targetRepo}#${options.itemNumber}: legacy exact artifact lacks its durable review lease tuple`,
243252
);
244253
}
254+
if (applyDisposition === "superseded") {
255+
console.log(
256+
`Completing ${options.targetRepo}#${options.itemNumber} as superseded: a newer durable review tuple is already live`,
257+
);
258+
if (options.batchMutationOutput) {
259+
writeBatchMutationResult(options.batchMutationOutput, { kind: "superseded" });
260+
}
261+
writePublicationCompletionOutputs("superseded", "remote_newer_tuple");
262+
summary();
263+
return;
264+
}
245265
const deferredCloseCoverageExpected = applyDisposition === "close_coverage_deferred";
246266
if (activeReviewLeaseRetryAt !== null) {
247267
console.log(
@@ -266,10 +286,11 @@ async function publishEventResult(options: EventOptions): Promise<void> {
266286
}
267287
}
268288
if (
269-
syncedCount + closedCount + missingCount === 0 &&
270-
guardedOpenAction === null &&
271-
!requeueLatestExpected &&
272-
!deferredCloseCoverageExpected
289+
(applyDisposition === "unproven" && guardedOpenAction === null && !requeueLatestExpected) ||
290+
(syncedCount + closedCount + missingCount === 0 &&
291+
guardedOpenAction === null &&
292+
!requeueLatestExpected &&
293+
!deferredCloseCoverageExpected)
273294
) {
274295
const observed =
275296
exactActions
@@ -280,15 +301,6 @@ async function publishEventResult(options: EventOptions): Promise<void> {
280301
`Event review for ${options.targetRepo}#${options.itemNumber} was not applied; actions: ${observed}`,
281302
);
282303
}
283-
const summary = () =>
284-
writeSummary({
285-
targetRepo: options.targetRepo,
286-
itemNumber: options.itemNumber,
287-
syncedCount,
288-
closedCount,
289-
missingCount,
290-
closeReasons: options.closeReasons,
291-
});
292304
const routableSyncExpected =
293305
syncedCount > 0 &&
294306
closedCount === 0 &&

‎test/repair/event-apply-proof.test.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ test("exact event proof completes live-shaped deterministic guarded-open results
385385
);
386386

387387
assert.equal(proof.guardedOpenAction, action);
388+
assert.equal(proof.disposition, "unproven");
388389
assert.equal(proof.latestRevisionRequeueRequired, false);
389390
}
390391
});
@@ -402,7 +403,7 @@ test("exact event proof keeps changed-since-review on the latest-revision requeu
402403
assert.equal(proof.latestRevisionRequeueRequired, true);
403404
});
404405

405-
test("exact event proof requeues only the guarded legacy tuple-less artifact path", () => {
406+
test("exact event proof supersedes only a verified strictly newer durable tuple", () => {
406407
const legacy = exactEventApplyProof(
407408
[
408409
eventApplyAction({
@@ -419,14 +420,16 @@ test("exact event proof requeues only the guarded legacy tuple-less artifact pat
419420
eventApplyAction({
420421
number: 42,
421422
action: "skipped_stale_review_comment_sync",
422-
reason: "live durable review comment is newer than the local report",
423+
reason:
424+
"live durable review tuple is newer than the local report: comment lease=9200, report lease=9100",
423425
}),
424426
],
425427
42,
426428
);
427429

428430
assert.equal(legacy.legacyTuplelessReviewLease, true);
429431
assert.equal(newerVerdict.legacyTuplelessReviewLease, false);
432+
assert.equal(newerVerdict.disposition, "superseded");
430433
assert.equal(
431434
eventApplyRequeueLatestExpected({
432435
disposition: legacy.disposition,
@@ -445,6 +448,47 @@ test("exact event proof requeues only the guarded legacy tuple-less artifact pat
445448
);
446449
});
447450

451+
test("exact event proof fails closed for ambiguous newer-tuple claims", () => {
452+
const reasons = [
453+
"live durable review tuple is newer than the local report: comment lease=9100, report lease=9100",
454+
"live durable review tuple is newer than the local report: comment lease=9000, report lease=9100",
455+
"live durable review tuple is newer than the local report: comment lease=invalid, report lease=9100",
456+
"live durable review tuple is newer than the local report: comment lease=9007199254740992, report lease=9100",
457+
"live durable review comment is newer than the local report",
458+
];
459+
for (const reason of reasons) {
460+
const proof = exactEventApplyProof(
461+
[
462+
eventApplyAction({
463+
number: 42,
464+
action: "skipped_stale_review_comment_sync",
465+
reason,
466+
}),
467+
],
468+
42,
469+
);
470+
assert.equal(proof.disposition, "unproven", reason);
471+
}
472+
473+
const mixed = exactEventApplyProof(
474+
[
475+
eventApplyAction({
476+
number: 42,
477+
action: "skipped_stale_review_comment_sync",
478+
reason:
479+
"live durable review tuple is newer than the local report: comment lease=9200, report lease=9100",
480+
}),
481+
eventApplyAction({
482+
number: 42,
483+
action: "review_comment_synced",
484+
durableReviewSynced: true,
485+
}),
486+
],
487+
42,
488+
);
489+
assert.equal(mixed.disposition, "unproven");
490+
});
491+
448492
test("guarded-open proof rejects mismatches, extra results, and transient skips", () => {
449493
const snapshotAction = "skipped_same_author_pair";
450494
const transientActions = [

‎test/repair/stale-event-disposition.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,26 @@ test("publish-event-result exits terminally on a stale preflight instead of thro
5656
);
5757
assert.ok(!preflightBlock.includes("throw new Error"));
5858
});
59+
60+
test("publish-event-result terminalizes a verified newer live durable tuple", () => {
61+
const source = readFileSync("src/repair/publish-event-result.ts", "utf8");
62+
const start = source.indexOf('if (applyDisposition === "superseded")');
63+
const end = source.indexOf(
64+
'const deferredCloseCoverageExpected = applyDisposition === "close_coverage_deferred"',
65+
start,
66+
);
67+
assert.ok(start >= 0 && end > start);
68+
const block = source.slice(start, end);
69+
assert.match(
70+
block,
71+
/writeBatchMutationResult\(options\.batchMutationOutput, \{\s*kind: "superseded"/,
72+
);
73+
assert.match(block, /writePublicationCompletionOutputs\("superseded", "remote_newer_tuple"\)/);
74+
assert.match(block, /summary\(\)/);
75+
assert.match(block, /return/);
76+
assert.doesNotMatch(block, /publishSnapshot|prepareBatchMutation|runApplyDecisions/);
77+
assert.match(
78+
source,
79+
/applyDisposition === "unproven" &&\s*guardedOpenAction === null &&\s*!requeueLatestExpected/,
80+
);
81+
});

0 commit comments

Comments
 (0)