Skip to content

Commit 2a61adc

Browse files
authored
fix(queue): preserve review budget for temporary deferrals (#1028)
Preserve exact-review attempt counters for typed coordination and throttle deferrals while retaining normal failure accounting. Validated by focused queue/workflow tests, exact-head CI, and fresh AWS Crabbox proof.
1 parent 3d4b45a commit 2a61adc

4 files changed

Lines changed: 171 additions & 12 deletions

File tree

‎.github/workflows/sweep.yml‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ jobs:
791791
echo "::notice::GitHub throttled the live-item check for $TARGET_REPO#$ITEM_NUMBER; releasing the claim for retry at $retry_at without spending failure budget."
792792
{
793793
echo "admission_retry=true"
794+
echo "retry_kind=throttle"
794795
echo "retry_at=$retry_at"
795796
echo "proceed=false"
796797
echo "terminal_noop=false"
@@ -1008,7 +1009,7 @@ jobs:
10081009
if grep -Eqi 'rate limit exceeded|secondary rate limit|HTTP 429' "$reservation_error"; then
10091010
retry_at="$(date -u -d "@$(( $(date -u +%s) + 1200 ))" +%Y-%m-%dT%H:%M:%SZ)"
10101011
echo "::notice::GitHub throttled the exact-review reservation; deferring until $retry_at so the durable queue retries without spending failure budget."
1011-
reservation="{\"status\":\"held\",\"retryAt\":\"$retry_at\"}"
1012+
reservation="{\"status\":\"held\",\"retryAt\":\"$retry_at\",\"retryKind\":\"throttle\"}"
10121013
break
10131014
fi
10141015
if ! grep -Eqi '(exact-review queue authority changed|review revision changed|could not (confirm|identify).+review lease).+retry required' "$reservation_error"; then
@@ -1043,7 +1044,10 @@ jobs:
10431044
if (reservation.status === "held") {
10441045
const retryAt = String(reservation.retryAt || "");
10451046
if (!Number.isFinite(Date.parse(retryAt))) process.exit(1);
1047+
const retryKind =
1048+
reservation.retryKind === "throttle" ? "throttle" : "coordination";
10461049
append("status", "held");
1050+
append("retry_kind", retryKind);
10471051
append("retry_at", new Date(retryAt).toISOString());
10481052
process.exit(0);
10491053
}
@@ -1258,6 +1262,7 @@ jobs:
12581262
if (!value || !Number.isFinite(timestamp)) process.exit(1);
12591263
process.stdout.write(new Date(timestamp).toISOString());
12601264
' "$coordination_held_path")"
1265+
echo "retry_kind=coordination" >> "$GITHUB_OUTPUT"
12611266
echo "retry_at=$retry_at" >> "$GITHUB_OUTPUT"
12621267
echo "::notice::Review coordination is held until $retry_at."
12631268
fi
@@ -1661,6 +1666,7 @@ jobs:
16611666
if: ${{ steps.claim-exact-review-queue.outputs.claimed == 'true' && always() }}
16621667
env:
16631668
ADMISSION_RETRY: ${{ steps.live-item.outputs.admission_retry }}
1669+
RETRY_KIND: ${{ steps.live-item.outputs.retry_kind || steps.reserve-exact-review-lease.outputs.retry_kind || steps.review-exact-event-item.outputs.retry_kind }}
16641670
TARGET_ENABLED: ${{ steps.target.outputs.target_enabled }}
16651671
LIVE_OUTCOME: ${{ steps.live-item.outcome }}
16661672
REVIEW_OUTCOME: ${{ steps.review-exact-event-item.outcome }}
@@ -1674,7 +1680,7 @@ jobs:
16741680
run: |
16751681
outcome=failure
16761682
requeue_latest=false
1677-
if [ "$ADMISSION_RETRY" = "true" ]; then
1683+
if [ "$ADMISSION_RETRY" = "true" ] && [ -z "$RETRY_KIND" ]; then
16781684
outcome=success
16791685
requeue_latest=true
16801686
elif [ "$TARGET_ENABLED" = "false" ]; then
@@ -1706,6 +1712,7 @@ jobs:
17061712
QUEUE_LEASE_REVISION: ${{ steps.claim-exact-review-queue.outputs.lease_revision }}
17071713
QUEUE_URL: ${{ vars.CLAWSWEEPER_EXACT_REVIEW_QUEUE_URL || 'https://clawsweeper.openclaw.ai' }}
17081714
REQUEUE_LATEST: ${{ steps.exact-review-generation-result.outputs.requeue_latest }}
1715+
RETRY_KIND: ${{ steps.live-item.outputs.retry_kind || steps.reserve-exact-review-lease.outputs.retry_kind || steps.review-exact-event-item.outputs.retry_kind }}
17091716
RETRY_AT: ${{ steps.live-item.outputs.retry_at || steps.reserve-exact-review-lease.outputs.retry_at || steps.review-exact-event-item.outputs.retry_at }}
17101717
DIRECT_PUBLICATION_ACCEPTED: ${{ steps.direct-exact-review-publication.outputs.accepted }}
17111718
DIRECT_PUBLICATION_SUPERSEDED: ${{ steps.direct-exact-review-publication.outputs.superseded }}
@@ -1736,7 +1743,10 @@ jobs:
17361743
? primaryOutcome
17371744
: "failure";
17381745
const requeueLatest = process.env.REQUEUE_LATEST === "true";
1746+
const retryKind = String(process.env.RETRY_KIND || "").trim();
17391747
const retryAt = String(process.env.RETRY_AT || "").trim();
1748+
if (retryKind && !["coordination", "throttle"].includes(retryKind)) process.exit(1);
1749+
if (retryKind && !retryAt) process.exit(1);
17401750
const directPublicationCompleted =
17411751
process.env.DIRECT_PUBLICATION_ACCEPTED === "true" &&
17421752
process.env.DIRECT_LIFECYCLE_OUTCOME === "success";
@@ -1758,6 +1768,7 @@ jobs:
17581768
run_attempt: runAttempt,
17591769
outcome,
17601770
...(requeueLatest ? { requeue_latest: true } : {}),
1771+
...(retryKind ? { retry_kind: retryKind } : {}),
17611772
...(directPublicationCompleted
17621773
? directPublicationSuperseded
17631774
? { completion_kind: "superseded", reason_code: "remote_newer_tuple" }

‎dashboard/exact-review-queue.ts‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ type ExactReviewBackoffReason =
147147
| "dispatch_debounce"
148148
| "dispatcher_backoff"
149149
| "admission_retry"
150+
| "coordination_retry"
151+
| "throttle_retry"
150152
| "review_retry"
151153
| "publication_retry";
152154
type ExactReviewParkedReason =
@@ -212,6 +214,7 @@ export type ExactReviewQueueItem = {
212214
terminalFinalization?: ExactReviewTerminalFinalization;
213215
};
214216
export type ExactReviewCompletionOutcome = "success" | "failure" | "cancelled";
217+
type ExactReviewRetryKind = "coordination" | "throttle";
215218
type ExactReviewPublicationFailureKind = "github_rate_limit" | "github_transient";
216219
type ExactReviewDispatchFailureClass =
217220
| "permanent_rejection"
@@ -1702,6 +1705,17 @@ export class ExactReviewQueue {
17021705
if (body.retry_at !== undefined && requestedRetryAt === null) {
17031706
return json({ error: "invalid_retry_at" }, 400);
17041707
}
1708+
const retryKind =
1709+
body.retry_kind === undefined ? undefined : exactReviewRetryKind(body.retry_kind);
1710+
if (body.retry_kind !== undefined && !retryKind) {
1711+
return json({ error: "invalid_retry_kind" }, 400);
1712+
}
1713+
if (retryKind && outcome !== "failure") {
1714+
return json({ error: "retry_kind_without_failure" }, 400);
1715+
}
1716+
if (retryKind && requestedRetryAt === null) {
1717+
return json({ error: "retry_kind_without_retry_at" }, 400);
1718+
}
17051719
const state = this.readStateSync();
17061720
const item = tupleCompletion ? state.items[itemKey] : exactReviewItemForLease(state, leaseId);
17071721
if (
@@ -1753,6 +1767,9 @@ export class ExactReviewQueue {
17531767
if (failureKind && !publicationItem) {
17541768
return json({ error: "failure_kind_outside_publication" }, 400);
17551769
}
1770+
if (retryKind && publicationItem) {
1771+
return json({ error: "retry_kind_outside_regular_review" }, 400);
1772+
}
17561773
const leasedDirectLifecycle = item.leaseDecision?.publication?.directLifecycle;
17571774
const directLifecycleLeasePublication =
17581775
item.leaseDecision?.sourceAction === EXACT_REVIEW_ARTIFACT_PUBLISH_SOURCE_ACTION &&
@@ -1854,6 +1871,7 @@ export class ExactReviewQueue {
18541871
outcome,
18551872
requestedRetryAt ?? undefined,
18561873
requeueLatest,
1874+
retryKind,
18571875
),
18581876
retried: outcome !== "success",
18591877
refreshed: false,
@@ -10717,22 +10735,35 @@ function finishExactReviewQueueItem(
1071710735
outcome: ExactReviewCompletionOutcome,
1071810736
requestedRetryAt = 0,
1071910737
requeueLatest = false,
10738+
retryKind?: ExactReviewRetryKind,
1072010739
) {
1072110740
const retryingFailure = outcome !== "success";
1072210741
const hasNewerRevision = item.revision > Number(item.leaseRevision || 0);
10742+
const typedDeferral = retryKind !== undefined && !hasNewerRevision && !requeueLatest;
1072310743
// A regular queue item may back off and retry after a failed lease. Failed
1072410744
// sweep shards already consumed their one recovery attempt before reaching
1072510745
// the queue, so only a newer source revision may supersede that recovery.
1072610746
const oneShotRecovery =
1072710747
item.leaseDecision?.sourceAction === FAILED_REVIEW_SHARD_RECOVERY_SOURCE_ACTION;
10728-
const requeued = (!oneShotRecovery && retryingFailure) || hasNewerRevision || requeueLatest;
10748+
const requeued =
10749+
typedDeferral || (!oneShotRecovery && retryingFailure) || hasNewerRevision || requeueLatest;
1072910750
if (!requeued) {
1073010751
delete state.items[item.key];
1073110752
return { requeued: false, parked: false };
1073210753
}
1073310754
clearExactReviewLease(item);
1073410755
item.state = "pending";
10735-
if (retryingFailure && !hasNewerRevision && !requeueLatest) {
10756+
if (typedDeferral) {
10757+
const dispatcherAttemptAt = exactReviewQueueEnqueueAttemptAt(state, now);
10758+
item.nextAttemptAt = Math.max(dispatcherAttemptAt, requestedRetryAt);
10759+
item.backoffReason =
10760+
dispatcherAttemptAt >= item.nextAttemptAt
10761+
? "dispatcher_backoff"
10762+
: retryKind === "coordination"
10763+
? "coordination_retry"
10764+
: "throttle_retry";
10765+
item.parkedReason = undefined;
10766+
} else if (retryingFailure && !hasNewerRevision && !requeueLatest) {
1073610767
item.attempts += 1;
1073710768
if (!exactReviewQueueIsPublication(item)) {
1073810769
const failureAttempts = Number(item.reviewFailureAttempts || 0) + 1;
@@ -10773,6 +10804,11 @@ function exactReviewCompletionRetryAt(value, now: number): number | null {
1077310804
return Math.max(now, retryAt);
1077410805
}
1077510806

10807+
function exactReviewRetryKind(value): ExactReviewRetryKind | null {
10808+
const normalized = String(value || "");
10809+
return normalized === "coordination" || normalized === "throttle" ? normalized : null;
10810+
}
10811+
1077610812
function clearExactReviewLease(item: ExactReviewQueueItem) {
1077710813
item.leaseId = undefined;
1077810814
item.leaseRevision = undefined;

‎test/dashboard-worker.test.ts‎

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19381,7 +19381,52 @@ test("failed shard recovery replaces an expired recovery lease", async () => {
1938119381
assert.equal(replacement.leaseId, undefined);
1938219382
});
1938319383

19384-
test("exact-review queue defers a coordination-held failure until the lease expires", async () => {
19384+
for (const retryKind of ["coordination", "throttle"] as const) {
19385+
test(`exact-review queue defers ${retryKind} without spending review attempts`, async () => {
19386+
const storage = new MemoryDurableStorage();
19387+
const retryAt = Date.now() + 45 * 60_000;
19388+
const item = leasedExactReviewQueueItem(711, "7110");
19389+
item.attempts = 3;
19390+
item.reviewFailureAttempts = 2;
19391+
await storage.put("exact-review-queue", {
19392+
deliveries: {},
19393+
items: {
19394+
"openclaw/openclaw#711": item,
19395+
},
19396+
});
19397+
const queue = new ExactReviewQueue({ storage }, {});
19398+
19399+
const response = await queue.fetch(
19400+
new Request("https://clawsweeper-exact-review-queue/complete", {
19401+
method: "POST",
19402+
body: JSON.stringify({
19403+
lease_id: "lease-711",
19404+
item_key: "openclaw/openclaw#711",
19405+
lease_revision: 1,
19406+
claim_generation: 1,
19407+
run_id: "7110",
19408+
run_attempt: 1,
19409+
outcome: "failure",
19410+
retry_kind: retryKind,
19411+
retry_at: new Date(retryAt).toISOString(),
19412+
}),
19413+
}),
19414+
);
19415+
19416+
assert.equal(response.status, 200);
19417+
assert.deepEqual(await response.json(), { ok: true, requeued: true });
19418+
const state = (await storage.get("exact-review-queue")) as {
19419+
items: Record<string, Record<string, unknown>>;
19420+
};
19421+
const deferred = state.items["openclaw/openclaw#711"];
19422+
assert.ok(Number(deferred.nextAttemptAt) >= retryAt);
19423+
assert.equal(deferred.attempts, 3);
19424+
assert.equal(deferred.reviewFailureAttempts, 2);
19425+
assert.equal(deferred.backoffReason, `${retryKind}_retry`);
19426+
});
19427+
}
19428+
19429+
test("exact-review queue spends review attempts for an untyped retry deadline", async () => {
1938519430
const storage = new MemoryDurableStorage();
1938619431
const retryAt = Date.now() + 45 * 60_000;
1938719432
await storage.put("exact-review-queue", {
@@ -19409,18 +19454,19 @@ test("exact-review queue defers a coordination-held failure until the lease expi
1940919454
);
1941019455

1941119456
assert.equal(response.status, 200);
19412-
assert.deepEqual(await response.json(), { ok: true, requeued: true });
1941319457
const state = (await storage.get("exact-review-queue")) as {
1941419458
items: Record<string, Record<string, unknown>>;
1941519459
};
19416-
assert.ok(Number(state.items["openclaw/openclaw#711"].nextAttemptAt) >= retryAt);
1941719460
assert.equal(state.items["openclaw/openclaw#711"].attempts, 1);
19461+
assert.equal(state.items["openclaw/openclaw#711"].reviewFailureAttempts, 1);
1941819462
});
1941919463

1942019464
test("exact-review queue does not carry an old coordination deadline to a newer revision", async () => {
1942119465
const storage = new MemoryDurableStorage();
1942219466
const retryAt = Date.now() + 45 * 60_000;
1942319467
const item = leasedExactReviewQueueItem(712, "7120");
19468+
item.attempts = 3;
19469+
item.reviewFailureAttempts = 2;
1942419470
item.revision = Number(item.leaseRevision) + 1;
1942519471
await storage.put("exact-review-queue", {
1942619472
deliveries: {},
@@ -19439,6 +19485,7 @@ test("exact-review queue does not carry an old coordination deadline to a newer
1943919485
run_id: "7120",
1944019486
run_attempt: 1,
1944119487
outcome: "failure",
19488+
retry_kind: "coordination",
1944219489
retry_at: new Date(retryAt).toISOString(),
1944319490
}),
1944419491
}),
@@ -19449,11 +19496,35 @@ test("exact-review queue does not carry an old coordination deadline to a newer
1944919496
items: Record<string, Record<string, unknown>>;
1945019497
};
1945119498
assert.ok(Number(state.items["openclaw/openclaw#712"].nextAttemptAt) < retryAt);
19499+
assert.equal(state.items["openclaw/openclaw#712"].attempts, 0);
19500+
assert.equal(state.items["openclaw/openclaw#712"].reviewFailureAttempts, 0);
1945219501
});
1945319502

19454-
test("exact-review queue rejects invalid coordination retry deadlines", async () => {
19503+
test("exact-review queue rejects invalid retry deferral contracts", async () => {
1945519504
const queue = new ExactReviewQueue({ storage: new MemoryDurableStorage() }, {});
19456-
for (const retryAt of ["not-a-timestamp", new Date(Date.now() + 3 * 60 * 60_000).toISOString()]) {
19505+
for (const [payload, error] of [
19506+
[
19507+
{ retry_kind: "capacity", retry_at: new Date(Date.now() + 60_000).toISOString() },
19508+
"invalid_retry_kind",
19509+
],
19510+
[{ retry_kind: "coordination" }, "retry_kind_without_retry_at"],
19511+
[
19512+
{
19513+
outcome: "success",
19514+
retry_kind: "coordination",
19515+
retry_at: new Date(Date.now() + 60_000).toISOString(),
19516+
},
19517+
"retry_kind_without_failure",
19518+
],
19519+
[{ retry_kind: "coordination", retry_at: "not-a-timestamp" }, "invalid_retry_at"],
19520+
[
19521+
{
19522+
retry_kind: "throttle",
19523+
retry_at: new Date(Date.now() + 3 * 60 * 60_000).toISOString(),
19524+
},
19525+
"invalid_retry_at",
19526+
],
19527+
] as const) {
1945719528
const response = await queue.fetch(
1945819529
new Request("https://clawsweeper-exact-review-queue/complete", {
1945919530
method: "POST",
@@ -19465,15 +19536,45 @@ test("exact-review queue rejects invalid coordination retry deadlines", async ()
1946519536
run_id: "7120",
1946619537
run_attempt: 1,
1946719538
outcome: "failure",
19468-
retry_at: retryAt,
19539+
...payload,
1946919540
}),
1947019541
}),
1947119542
);
1947219543
assert.equal(response.status, 400);
19473-
assert.deepEqual(await response.json(), { error: "invalid_retry_at" });
19544+
assert.deepEqual(await response.json(), { error });
1947419545
}
1947519546
});
1947619547

19548+
test("exact-review queue rejects regular-review retry kinds for publication work", async () => {
19549+
const storage = new MemoryDurableStorage();
19550+
const item = leasedExactReviewPublicationItem(712, "7120");
19551+
await storage.put("exact-review-queue", {
19552+
deliveries: {},
19553+
items: { [item.key]: item },
19554+
});
19555+
const queue = new ExactReviewQueue({ storage }, {});
19556+
19557+
const response = await queue.fetch(
19558+
new Request("https://clawsweeper-exact-review-queue/complete", {
19559+
method: "POST",
19560+
body: JSON.stringify({
19561+
lease_id: item.leaseId,
19562+
item_key: item.key,
19563+
lease_revision: item.leaseRevision,
19564+
claim_generation: item.claimGeneration,
19565+
run_id: item.claimedRunId,
19566+
run_attempt: item.claimedRunAttempt,
19567+
outcome: "failure",
19568+
retry_kind: "throttle",
19569+
retry_at: new Date(Date.now() + 60_000).toISOString(),
19570+
}),
19571+
}),
19572+
);
19573+
19574+
assert.equal(response.status, 400);
19575+
assert.deepEqual(await response.json(), { error: "retry_kind_outside_regular_review" });
19576+
});
19577+
1947719578
test("exact-review queue requeues a verified source drift exactly once without failure backoff", async () => {
1947819579
const storage = new MemoryDurableStorage();
1947919580
await storage.put("exact-review-queue", {

0 commit comments

Comments
 (0)