Skip to content

Commit 13f8ffd

Browse files
authored
fix(queue): preserve GitHub quota headroom (#1086)
* fix(queue): preserve GitHub quota headroom * fix(queue): pause background recovery on throttle * docs(scheduler): align quota admission guidance * docs: align exact review quota guidance
1 parent 95f9dab commit 13f8ffd

8 files changed

Lines changed: 197 additions & 41 deletions

File tree

‎.github/workflows/clawsweeper-dispatch.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ jobs:
152152
env:
153153
GH_TOKEN: ${{ steps.token.outputs.token }}
154154
TARGET_REPO: ${{ github.repository }}
155+
TARGET_BRANCH: ${{ github.event.repository.default_branch }}
155156
ITEM_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
156157
ITEM_KIND: ${{ github.event_name == 'pull_request_target' && 'pull_request' || 'issue' }}
157158
SOURCE_EVENT: ${{ github.event_name }}
@@ -199,13 +200,14 @@ jobs:
199200
)"
200201
payload="$(jq -nc \
201202
--arg target_repo "$TARGET_REPO" \
203+
--arg target_branch "$TARGET_BRANCH" \
202204
--argjson item_number "$ITEM_NUMBER" \
203205
--arg item_kind "$ITEM_KIND" \
204206
--arg source_event "$SOURCE_EVENT" \
205207
--arg source_action "$SOURCE_ACTION" \
206208
--arg ingress_fingerprint "$ingress_fingerprint" \
207209
--argjson supersedes_in_progress "$SUPERSEDES_IN_PROGRESS" \
208-
'{event_type:"clawsweeper_item",client_payload:({target_repo:$target_repo,item_number:$item_number,item_kind:$item_kind,source_event:$source_event,source_action:$source_action,supersedes_in_progress:$supersedes_in_progress} + (if $ingress_fingerprint != "" then {ingress_route:"target_dispatcher",ingress_fingerprint:$ingress_fingerprint} else {} end))}')"
210+
'{event_type:"clawsweeper_item",client_payload:({target_repo:$target_repo,target_branch:$target_branch,item_number:$item_number,item_kind:$item_kind,source_event:$source_event,source_action:$source_action,supersedes_in_progress:$supersedes_in_progress} + (if $ingress_fingerprint != "" then {ingress_route:"target_dispatcher",ingress_fingerprint:$ingress_fingerprint} else {} end))}')"
209211
gh api repos/openclaw/clawsweeper/dispatches \
210212
--method POST \
211213
--input - <<< "$payload"

‎.github/workflows/sweep.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3840,7 +3840,7 @@ jobs:
38403840
# The Durable Object turns every selected item into an independent exact-review
38413841
# workflow. Direct schedules use current free queue capacity; target fanout may
38423842
# request a smaller backlog-weighted share. The Worker remains the final owner of
3843-
# the fleet-wide 600/hour admission target and queue backpressure.
3843+
# the fleet-wide 300/hour admission target and queue backpressure.
38443844
requested_batch_size="${{ github.event.client_payload.batch_size || '' }}"
38453845
if ! [[ "$requested_batch_size" =~ ^[0-9]+$ ]] || [ "$requested_batch_size" -lt 1 ]; then
38463846
requested_batch_size="$queue_candidate_capacity"

‎dashboard/exact-review-queue.ts‎

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ const DEFAULT_EXACT_REVIEW_DISPATCH_DEBOUNCE_MAX_MS = 3 * 60_000;
455455
const DEFAULT_EXACT_REVIEW_PENDING_SOFT_LIMIT = 300;
456456
const DEFAULT_EXACT_REVIEW_TARGET_RATE_PER_HOUR = 200;
457457
const DEFAULT_EXACT_REVIEW_TARGET_BURST = 50;
458+
const EXACT_REVIEW_GITHUB_THROTTLE_ADMISSION_COOLDOWN_MS = 15 * 60 * 1000;
458459
const EXACT_REVIEW_SCHEDULED_FEED_KEY_PREFIX = "exact-review-scheduled-feed:v1";
459460
type ExactReviewScheduledLane = "hot_intake" | "normal_backfill";
460461
type ExactReviewScheduledBucket = ExactReviewScheduledLane | "global";
@@ -1288,15 +1289,12 @@ export class ExactReviewQueue {
12881289
);
12891290
return { shed: true as const, reason: "backpressure" as const };
12901291
}
1291-
if (
1292-
exactReviewScheduledLane(decision) &&
1293-
!this.takeScheduledReviewTokenSync(decision, now)
1294-
) {
1292+
if (!this.takeScheduledReviewTokenSync(decision, now)) {
12951293
state.shedSinceReset = exactReviewShedSinceReset(state) + 1;
12961294
this.writeStateSync(state);
12971295
this.incrementQueueMetricsSync({ reviewShed: 1, reviewShedScheduledRate: 1 });
12981296
console.warn(
1299-
`exact-review admission shed: reason=scheduled_rate item=${key} lane=${exactReviewScheduledLane(decision)}`,
1297+
`exact-review admission shed: reason=scheduled_rate item=${key} lane=${exactReviewScheduledLane(decision) || "background"}`,
13001298
);
13011299
return { shed: true as const, reason: "scheduled_rate" as const };
13021300
}
@@ -1955,6 +1953,9 @@ export class ExactReviewQueue {
19551953
item.updatedAt = now;
19561954
}
19571955
const { requeued } = completionResult;
1956+
if (!publicationItem && retryKind === "throttle") {
1957+
this.deferScheduledReviewAdmissionForThrottleSync(now, requestedRetryAt ?? 0);
1958+
}
19581959
// A successful workflow can still request requeue_latest after source
19591960
// drift. That work did not leave its lane, so it must not improve the
19601961
// operator-facing net speed until a later revision actually completes.
@@ -8012,18 +8013,30 @@ export class ExactReviewQueue {
80128013

80138014
private takeScheduledReviewTokenSync(decision: ExactReviewDecision, now: number) {
80148015
const lane = exactReviewScheduledLane(decision);
8015-
if (!lane) return true;
8016+
const lowPriority = isLowPriorityExactReviewDecision(decision);
8017+
if (!lane && !lowPriority) return true;
80168018
const global = this.scheduledReviewBucketSync("global", now);
8017-
const bucket = this.scheduledReviewBucketSync(lane, now);
8018-
const admitted = global.tokens >= 1 && bucket.tokens >= 1;
8019+
const bucket = lane ? this.scheduledReviewBucketSync(lane, now) : null;
8020+
const admitted =
8021+
Number(global.throttleUntil || 0) <= now &&
8022+
(lowPriority || (global.tokens >= 1 && Number(bucket?.tokens || 0) >= 1));
80198023
this.storage.kv.put(exactReviewScheduledFeedKey("global"), {
8020-
tokens: admitted ? global.tokens - 1 : global.tokens,
8021-
updatedAt: now,
8022-
});
8023-
this.storage.kv.put(exactReviewScheduledFeedKey(lane), {
8024-
tokens: admitted ? bucket.tokens - 1 : bucket.tokens,
8024+
tokens: admitted && lane ? global.tokens - 1 : global.tokens,
80258025
updatedAt: now,
8026+
...(global.throttleObservedAt
8027+
? {
8028+
throttleObservedAt: global.throttleObservedAt,
8029+
throttleUntil: global.throttleUntil,
8030+
throttleSource: global.throttleSource,
8031+
}
8032+
: {}),
80268033
});
8034+
if (lane && bucket) {
8035+
this.storage.kv.put(exactReviewScheduledFeedKey(lane), {
8036+
tokens: admitted ? bucket.tokens - 1 : bucket.tokens,
8037+
updatedAt: now,
8038+
});
8039+
}
80278040
return admitted;
80288041
}
80298042

@@ -8032,6 +8045,29 @@ export class ExactReviewQueue {
80328045
this.storage.kv.put(exactReviewScheduledFeedKey("global"), {
80338046
tokens: Math.max(-global.burst, global.tokens - 1),
80348047
updatedAt: now,
8048+
...(global.throttleObservedAt
8049+
? {
8050+
throttleObservedAt: global.throttleObservedAt,
8051+
throttleUntil: global.throttleUntil,
8052+
throttleSource: global.throttleSource,
8053+
}
8054+
: {}),
8055+
});
8056+
}
8057+
8058+
private deferScheduledReviewAdmissionForThrottleSync(now: number, requestedRetryAt: number) {
8059+
const global = this.scheduledReviewBucketSync("global", now);
8060+
const throttleUntil = Math.max(
8061+
now + EXACT_REVIEW_GITHUB_THROTTLE_ADMISSION_COOLDOWN_MS,
8062+
requestedRetryAt,
8063+
Number(global.throttleUntil || 0),
8064+
);
8065+
this.storage.kv.put(exactReviewScheduledFeedKey("global"), {
8066+
tokens: global.tokens,
8067+
updatedAt: now,
8068+
throttleObservedAt: now,
8069+
throttleUntil,
8070+
throttleSource: "review_completion",
80358071
});
80368072
}
80378073

@@ -8041,6 +8077,8 @@ export class ExactReviewQueue {
80418077
const stored = objectValue(this.storage.kv.get(exactReviewScheduledFeedKey(lane)));
80428078
const updatedAt = Number(stored.updatedAt);
80438079
const storedTokens = Number(stored.tokens);
8080+
const throttleObservedAt = Number(stored.throttleObservedAt);
8081+
const throttleUntil = Number(stored.throttleUntil);
80448082
if (!Number.isFinite(updatedAt) || !Number.isFinite(storedTokens)) {
80458083
return { tokens: burst, updatedAt: now, ratePerHour, burst };
80468084
}
@@ -8050,6 +8088,16 @@ export class ExactReviewQueue {
80508088
updatedAt: now,
80518089
ratePerHour,
80528090
burst,
8091+
...(Number.isFinite(throttleObservedAt) &&
8092+
throttleObservedAt > 0 &&
8093+
Number.isFinite(throttleUntil) &&
8094+
throttleUntil > now
8095+
? {
8096+
throttleObservedAt,
8097+
throttleUntil,
8098+
throttleSource: String(stored.throttleSource || "unknown"),
8099+
}
8100+
: {}),
80538101
};
80548102
}
80558103

@@ -8061,6 +8109,13 @@ export class ExactReviewQueue {
80618109
target_rate_per_hour: global.ratePerHour,
80628110
burst: global.burst,
80638111
token_balance: Math.floor(global.tokens),
8112+
...(global.throttleObservedAt
8113+
? {
8114+
throttle_source: global.throttleSource,
8115+
throttle_observed_at: new Date(global.throttleObservedAt).toISOString(),
8116+
throttle_recovery_at: new Date(global.throttleUntil || 0).toISOString(),
8117+
}
8118+
: {}),
80648119
lanes: {
80658120
hot_intake: {
80668121
target_rate_per_hour: hot.ratePerHour,

‎dashboard/wrangler.toml‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,14 @@ EXACT_REVIEW_WORKFLOW_PAUSED_RETRY_MS = "60000"
6565
EXACT_REVIEW_DISPATCH_DEBOUNCE_MS = "90000"
6666
EXACT_REVIEW_DISPATCH_DEBOUNCE_MAX_MS = "180000"
6767
EXACT_REVIEW_PENDING_SOFT_LIMIT = "600"
68-
# 200/h only sustained ~14 concurrent reviews against a 128-slot pool; with the
69-
# review/publication lane split (#953) the queue drains to zero pending while
70-
# 3,306 items have never been reviewed at all (42.9% weekly coverage). Model
71-
# spend scales with this dial. 600/h saturated the GitHub App installation
72-
# token budget once the apply and comment-sync lanes resumed alongside reviews
73-
# (installation 122230863 rate-limit 403s, 2026-07-30 16:56Z): at ~30 API calls
74-
# per review, 450/h leaves the write lanes headroom inside the shared allowance
75-
# while still clearing the first-pass backlog in about a day.
76-
EXACT_REVIEW_TARGET_RATE_PER_HOUR = "450"
77-
EXACT_REVIEW_TARGET_BURST = "120"
68+
# A full review consumes about 30 GitHub requests. Keep scheduled review demand
69+
# at or below 9,000 requests/hour so the 15,000-request installation bucket has
70+
# explicit capacity for exact ingress, comment routing, apply proof, publication,
71+
# and support lanes. A 30-item burst bounds cold-start demand to roughly 900
72+
# requests instead of allowing a 3,600-request spike before the hourly limiter
73+
# takes effect. Interactive exact-event reviews keep their independent lane.
74+
EXACT_REVIEW_TARGET_RATE_PER_HOUR = "300"
75+
EXACT_REVIEW_TARGET_BURST = "30"
7876
# Keep each mutation lease at 8 while allowing four isolated workflows to prepare
7977
# concurrently. Final commits still cross the single state-writer coordinator.
8078
# A full-window publish moves ~2k paths and observed ~40 min; the drain lease

‎docs/limits.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ unset, empty, or invalid values use the defaults.
149149
| `CLAWSWEEPER_QUEUE_PRESSURE_HARD_AGE_MS` | 7200000 |
150150

151151
Only manual normal review and manual hot intake use this pressure
152-
multiplier. Scheduled review uses the queue's 600-item soft limit and 600/hour
152+
multiplier. Scheduled review uses the queue's 600-item soft limit and 300/hour
153153
admission target. Repair, assist, issue implementation, cluster repair, and
154154
exact-item review keep their existing priority budgets.
155155

@@ -208,8 +208,8 @@ backlog cannot consume review admission capacity. Existing items, webhook
208208
events, commands, and publications remain admitted. The queue reports shed
209209
counts under `lanes.review.shed_reasons_since_reset` and the rolling flow by
210210
`backpressure` versus `scheduled_rate`; pre-migration totals remain
211-
`unattributed`. All newly queued review work debits a durable 600-review/hour
212-
budget with a 120-item burst. Organic work is always admitted and consumes the
211+
`unattributed`. All newly queued review work debits a durable 300-review/hour
212+
budget with a 30-item burst. Organic work is always admitted and consumes the
213213
budget first; scheduled work fills the remainder and is split 35% hot intake and
214214
65% normal backfill so hot churn cannot starve oldest-first coverage. Re-offering an item
215215
that is already pending, dispatching, or leased is a semantic dedupe: it does not

‎docs/scheduler.md‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ Current defaults:
307307
- scheduled hot intake and normal backfill: select up to 50 due items per target
308308
cycle, then enqueue each item through the durable exact-review queue; every
309309
admitted item receives its own parallel workflow
310-
- total review admission target: 600 items/hour across the fleet; organic work
310+
- total review admission target: 300 items/hour across the fleet; organic work
311311
consumes the budget first and scheduled backfill fills the remainder, split
312-
35% hot intake and 65% normal backfill, with a 120-item burst
312+
35% hot intake and 65% normal backfill, with a 30-item burst
313313
- review admission and pressure are computed independently from publication;
314314
top-level queue health describes reviews while `lanes.publication` retains
315315
publication backlog, retry, DLQ, and health telemetry
@@ -354,7 +354,7 @@ serializes per target repository, selects globally before sharding, and offers
354354
never-reviewed candidates before the oldest due tracked candidates to the
355355
durable queue. Queue admission is fleet-wide,
356356
so overlapping core and fanout cycles fill only the residual of the configured
357-
600/hour model-spend target after organic review demand.
357+
300/hour model-spend target after organic review demand.
358358

359359
The manual quiet-system ceiling is not a promise that every operator run dispatches
360360
that many shards. The `mode` step checks active repair workers, exact-item sweep
@@ -384,7 +384,9 @@ run-summary funnel for selected, attempted, enqueued, deduped, shed, and deferre
384384
items. The queue exposes the configured rate, burst, and currently available
385385
token balance under `scheduled_feed` in `GET /api/exact-review-queue`. It also
386386
exposes backpressure and scheduled-rate shed counts separately so an operator
387-
can distinguish a full review queue from intentional 600/hour pacing.
387+
can distinguish a full review queue from intentional 300/hour pacing. The
388+
30-item burst bounds a cold-start cohort to roughly 900 GitHub requests at the
389+
observed planning average of 30 requests per completed review.
388390
The producer probes that field before its first enqueue and fails closed while
389391
an older Worker is still deployed, preventing a workflow-first rollout from
390392
bypassing the rate limiter.
@@ -393,11 +395,14 @@ Each hourly normal-fanout step can offer
393395
`50 items/target * 12 targets = 600 items/hour`. The direct five-minute hot and
394396
normal schedules can each offer `50 * 12 = 600 items/hour`, so either lane has
395397
enough candidates to keep its token bucket fed despite dedupe or uneven fleet
396-
distribution. At a 4.1-minute mean service time, 600/hour needs about
397-
`600 * 4.1 / 60 = 41` concurrent review workers, below the 120 per-target and
398-
128 global claim limits. The target rate, burst, and pending limit are explicit
399-
spend and GitHub API dials; lower them together when sustained traffic warrants
400-
more headroom.
398+
distribution. The queue admits at most 300 scheduled reviews/hour, which needs
399+
about `300 * 4.1 / 60 = 21` concurrent review workers at a 4.1-minute mean
400+
service time and budgets roughly 9,000 GitHub requests/hour. That leaves about
401+
6,000 requests in the shared 15,000-request installation allowance for exact
402+
ingress, routing, apply proof, publication, and support lanes. The target rate
403+
and burst are GitHub spend dials. The pending soft limit is a separate queue
404+
backpressure bound and should change only when queue-memory or latency evidence
405+
requires it, not automatically with the request budget.
401406

402407
On saturated queues, normal planning reads the complete bounded open-item scan
403408
before selecting candidates. For the current largest repository this is about

‎docs/target-dispatcher.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ jobs:
197197
env:
198198
GH_TOKEN: ${{ steps.token.outputs.token }}
199199
TARGET_REPO: ${{ github.repository }}
200+
TARGET_BRANCH: ${{ github.event.repository.default_branch }}
200201
ITEM_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
201202
ITEM_KIND: ${{ github.event_name == 'pull_request_target' && 'pull_request' || 'issue' }}
202203
SOURCE_EVENT: ${{ github.event_name }}
@@ -244,13 +245,14 @@ jobs:
244245
)"
245246
payload="$(jq -nc \
246247
--arg target_repo "$TARGET_REPO" \
248+
--arg target_branch "$TARGET_BRANCH" \
247249
--argjson item_number "$ITEM_NUMBER" \
248250
--arg item_kind "$ITEM_KIND" \
249251
--arg source_event "$SOURCE_EVENT" \
250252
--arg source_action "$SOURCE_ACTION" \
251253
--arg ingress_fingerprint "$ingress_fingerprint" \
252254
--argjson supersedes_in_progress "$SUPERSEDES_IN_PROGRESS" \
253-
'{event_type:"clawsweeper_item",client_payload:({target_repo:$target_repo,item_number:$item_number,item_kind:$item_kind,source_event:$source_event,source_action:$source_action,supersedes_in_progress:$supersedes_in_progress} + (if $ingress_fingerprint != "" then {ingress_route:"target_dispatcher",ingress_fingerprint:$ingress_fingerprint} else {} end))}')"
255+
'{event_type:"clawsweeper_item",client_payload:({target_repo:$target_repo,target_branch:$target_branch,item_number:$item_number,item_kind:$item_kind,source_event:$source_event,source_action:$source_action,supersedes_in_progress:$supersedes_in_progress} + (if $ingress_fingerprint != "" then {ingress_route:"target_dispatcher",ingress_fingerprint:$ingress_fingerprint} else {} end))}')"
254256
gh api repos/openclaw/clawsweeper/dispatches \
255257
--method POST \
256258
--input - <<< "$payload"

0 commit comments

Comments
 (0)