Skip to content

Commit e709b11

Browse files
committed
fix(publication): align superseded departure eligibility
1 parent 2760c12 commit e709b11

2 files changed

Lines changed: 127 additions & 16 deletions

File tree

‎dashboard/exact-review-queue.ts‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,6 +3847,7 @@ export class ExactReviewQueue {
38473847
new Set(snapshotBatchOwnership.itemKeys),
38483848
snapshotBatchOwnership.activeBatches,
38493849
this.freshPublicationItemKeysSync(snapshot, startedAt),
3850+
this.supersededPublicationItemKeysSync(snapshot),
38503851
);
38513852
let snapshotBatchCandidates = snapshotBatchDeparture?.due
38523853
? this.publicationBatchCandidates(
@@ -3912,6 +3913,7 @@ export class ExactReviewQueue {
39123913
new Set(snapshotBatchOwnership.itemKeys),
39133914
snapshotBatchOwnership.activeBatches,
39143915
this.freshPublicationItemKeysSync(snapshot, recheckedAt),
3916+
this.supersededPublicationItemKeysSync(snapshot),
39153917
);
39163918
snapshotBatchCandidates = snapshotBatchDeparture?.due
39173919
? this.publicationBatchCandidates(
@@ -5929,7 +5931,10 @@ export class ExactReviewQueue {
59295931
// shared helper counts candidate rows, so widen only its scan window by
59305932
// requestedSize; passing +1 here would silently collapse every batch to one.
59315933
const activePublishers = exactReviewQueueActivePublicationCount(state);
5932-
const excludedItemKeys = new Set<string>(batchOwnership.itemKeys);
5934+
const excludedItemKeys = new Set<string>([
5935+
...batchOwnership.itemKeys,
5936+
...this.supersededPublicationItemKeysSync(state),
5937+
]);
59335938
for (const item of Object.values(state.items)) {
59345939
// Direct receipts and terminal acknowledgement drivers must remain on
59355940
// the normal publisher. Exclude them before admission so they cannot
@@ -5940,13 +5945,6 @@ export class ExactReviewQueue {
59405945
if (exactReviewGithubCircuitBlocksItem(state, item, now)) {
59415946
excludedItemKeys.add(item.key);
59425947
}
5943-
const revision = exactReviewPublicationRevision(item.decision);
5944-
if (
5945-
revision &&
5946-
revision.sourceRevision < this.publicationHeadRevisionSync(revision.targetKey)
5947-
) {
5948-
excludedItemKeys.add(item.key);
5949-
}
59505948
}
59515949
const freshItemKeys = this.freshPublicationItemKeysSync(state, now);
59525950
const readyCandidates =
@@ -5968,14 +5966,7 @@ export class ExactReviewQueue {
59685966
// immutable lifecycle plan must return through the fenced legacy
59695967
// publisher, which is the only path that replays that plan.
59705968
.filter(exactReviewQueueIsBatchablePublication)
5971-
.filter((item) => !item.terminalFinalization)
5972-
.filter((item) => {
5973-
const revision = exactReviewPublicationRevision(item.decision);
5974-
return (
5975-
!revision ||
5976-
revision.sourceRevision >= this.publicationHeadRevisionSync(revision.targetKey)
5977-
);
5978-
});
5969+
.filter((item) => !item.terminalFinalization);
59795970
const selection = exactReviewPublicationBatchSelection(
59805971
readyCandidates,
59815972
freshItemKeys,
@@ -7858,6 +7849,20 @@ export class ExactReviewQueue {
78587849
return Number(row?.source_revision || 0);
78597850
}
78607851

7852+
private supersededPublicationItemKeysSync(state: ExactReviewQueueState) {
7853+
// Active batch ownership can preserve an older row after a newer head lands.
7854+
// Departure and claim must exclude that same row when ownership expires.
7855+
return new Set(
7856+
Object.values(state.items).flatMap((item) => {
7857+
const revision = exactReviewPublicationRevision(item.decision);
7858+
return revision &&
7859+
revision.sourceRevision < this.publicationHeadRevisionSync(revision.targetKey)
7860+
? [item.key]
7861+
: [];
7862+
}),
7863+
);
7864+
}
7865+
78617866
private nextExactReviewItemRevisionSync(itemKey: string, minimumRevision = 1): number {
78627867
return Math.max(minimumRevision, this.publicationHeadRevisionSync(itemKey.toLowerCase()) + 1);
78637868
}
@@ -10859,6 +10864,7 @@ export class ExactReviewQueue {
1085910864
new Set(batchOwnership.itemKeys),
1086010865
batchOwnership.activeBatches,
1086110866
this.freshPublicationItemKeysSync(state, now),
10867+
this.supersededPublicationItemKeysSync(state),
1086210868
);
1086310869
const sourceAuthorityNext = await this.nextSourceAuthorityVerificationAt();
1086410870
const commandIntakeNext = this.commandIntakeStore.nextAttemptAt();
@@ -12964,6 +12970,7 @@ function exactReviewPublicationBatchDeparture(
1296412970
ownedItemKeys: ReadonlySet<string>,
1296512971
activeBatchCount: number,
1296612972
freshItemKeys: ReadonlySet<string> = new Set(),
12973+
supersededItemKeys: ReadonlySet<string> = new Set(),
1296712974
) {
1296812975
if (
1296912976
!exactReviewPublicationBatchingEnabled(env) ||
@@ -12979,6 +12986,7 @@ function exactReviewPublicationBatchDeparture(
1297912986
item.state === "pending" &&
1298012987
!item.terminalFinalization &&
1298112988
!ownedItemKeys.has(item.key) &&
12989+
!supersededItemKeys.has(item.key) &&
1298212990
!exactReviewGithubCircuitBlocksItem(state, item, now),
1298312991
)
1298412992
.sort((left, right) => left.createdAt - right.createdAt || left.key.localeCompare(right.key)),

‎test/exact-review-publication-batches.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,109 @@ test("batch ask widens owner scanning without exceeding the configured lease siz
28292829
}
28302830
});
28312831

2832+
test("aged superseded publication does not dispatch a fresh owner before its deadline", async () => {
2833+
const originalFetch = globalThis.fetch;
2834+
const originalNow = Date.now;
2835+
let now = 1_850_000;
2836+
const agedEnqueuedAt = now;
2837+
Date.now = () => now;
2838+
const { privateKey } = generateKeyPairSync("rsa", {
2839+
modulusLength: 2048,
2840+
privateKeyEncoding: { type: "pkcs8", format: "pem" },
2841+
publicKeyEncoding: { type: "spki", format: "pem" },
2842+
});
2843+
const checkedTargets: string[] = [];
2844+
const dispatches: Array<{ inputs: Record<string, string> }> = [];
2845+
globalThis.fetch = async (input, init) => {
2846+
const url = new URL(String(input));
2847+
if (url.pathname === "/repos/openclaw/clawsweeper/installation") {
2848+
return new Response(JSON.stringify({ id: 999 }), {
2849+
headers: { "content-type": "application/json" },
2850+
});
2851+
}
2852+
if (url.pathname === "/repos/fresh/repo/installation") {
2853+
return new Response(JSON.stringify({ id: 1001 }), {
2854+
headers: { "content-type": "application/json" },
2855+
});
2856+
}
2857+
if (url.pathname === "/app/installations/999/access_tokens") {
2858+
return new Response(JSON.stringify({ token: "dispatch-token" }), {
2859+
headers: { "content-type": "application/json" },
2860+
});
2861+
}
2862+
if (url.pathname === "/app/installations/1001/access_tokens") {
2863+
return new Response(JSON.stringify({ token: "fresh-token" }), {
2864+
headers: { "content-type": "application/json" },
2865+
});
2866+
}
2867+
if (url.pathname === "/repos/fresh/repo/issues/161") {
2868+
checkedTargets.push("fresh/repo#161");
2869+
return new Response(JSON.stringify({ state: "open" }), {
2870+
headers: { "content-type": "application/json" },
2871+
});
2872+
}
2873+
if (
2874+
url.pathname ===
2875+
"/repos/openclaw/clawsweeper/actions/workflows/exact-review-batch-publish.yml/dispatches"
2876+
) {
2877+
dispatches.push(JSON.parse(String(init?.body)));
2878+
return new Response(null, { status: 204 });
2879+
}
2880+
throw new Error(`unexpected fetch ${url}`);
2881+
};
2882+
try {
2883+
const storage = new TestStorage();
2884+
const queue = new ExactReviewQueue(
2885+
{ storage },
2886+
{
2887+
CLAWSWEEPER_APP_CLIENT_ID: "Iv23test",
2888+
CLAWSWEEPER_APP_PRIVATE_KEY: privateKey,
2889+
EXACT_REVIEW_PUBLICATION_BATCHING_ENABLED: "1",
2890+
EXACT_REVIEW_PUBLICATION_BATCH_SIZE: "2",
2891+
EXACT_REVIEW_PUBLICATION_BATCH_LEASE_MS: "60000",
2892+
EXACT_REVIEW_PUBLICATION_BATCH_WAIT_MS: "300000",
2893+
EXACT_REVIEW_PUBLICATION_FRESH_LANE_ENABLED: "1",
2894+
EXACT_REVIEW_PUBLICATION_FRESH_LANE_MAX_ITEMS: "1",
2895+
EXACT_REVIEW_PUBLICATION_FRESH_LANE_MAX_AGE_MS: "60000",
2896+
},
2897+
);
2898+
await queue.fetch(publicationRequest("superseded-aged-1", 160, "1060", "stale/repo", 1));
2899+
const owned = await (
2900+
await queue.fetch(
2901+
batchRequest("/publication-batches/claim", {
2902+
claim_id: "claim-superseded-aged",
2903+
lease_owner: "worker-1",
2904+
max_items: 1,
2905+
}),
2906+
)
2907+
).json();
2908+
assert.equal(owned.claimed, true, JSON.stringify(owned));
2909+
2910+
now += 1;
2911+
await queue.fetch(publicationRequest("superseded-aged-2", 160, "1061", "stale/repo", 2));
2912+
const removedNewer = await (
2913+
await queue.fetch(
2914+
batchRequest("/publications/supersede", {
2915+
items: [{ item_key: "stale/repo#160@publish:1061:1", revision: 1 }],
2916+
}),
2917+
)
2918+
).json();
2919+
assert.equal(removedNewer.superseded, 1);
2920+
2921+
now = agedEnqueuedAt + 300_001;
2922+
const freshEnqueuedAt = now;
2923+
await queue.fetch(publicationRequest("valid-fresh-owner", 161, "1062", "fresh/repo"));
2924+
await queue.alarm();
2925+
2926+
assert.deepEqual(checkedTargets, []);
2927+
assert.equal(dispatches.length, 0);
2928+
assert.equal(storage.scheduledAlarm(), freshEnqueuedAt + 300_000);
2929+
} finally {
2930+
globalThis.fetch = originalFetch;
2931+
Date.now = originalNow;
2932+
}
2933+
});
2934+
28322935
test("oldest publication aging deadline drives the alarm and then its owner claim", async () => {
28332936
const originalFetch = globalThis.fetch;
28342937
const originalNow = Date.now;

0 commit comments

Comments
 (0)