Skip to content

fix(queue): charge the review budget at Codex generation start - #1738

Merged
steipete merged 3 commits into
mainfrom
fix/charge-budget-at-generation-start
Oct 4, 2026
Merged

steipete merged 3 commits into
mainfrom
fix/charge-budget-at-generation-start

Conversation

@steipete

@steipete steipete commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Scheduled backfill is still starved after #1736. The public budget telemetry from #1737 shows why:

  • scheduled_feed.token_balance sits around −3.
  • Scheduled active is 1 of 32.
  • The normal-backfill lane sits full at 16 of 16 because it never gets to spend.
  • Every shed is scheduled_rate (89,253 since reset).

Organic runs claim about 220 leases/hour, and roughly a third of them exit before Codex starts: live-item admission skips, or the lease is superseded during setup. Charging at claim spends the whole 220/hour allowance on claims, and many of those claims never run a review.

This PR moves the charge to the moment Codex generation actually starts. That is the existing startup ownership check in the review step, which the workflow already performs immediately before generation.

Change

  • .github/workflows/sweep.yml: verify_startup_authority adds generation_start: true to the lease heartbeat it already sends before generation. Both review paths (ordinary and oversized) call it. Plain periodic heartbeats are unchanged.
  • dashboard/exact-review-queue.ts:
    • /claim no longer debits. It records reviewBudgetChargeGeneration (the claim generation that owes one token).
    • /heartbeat validates generation_start (true with phase review, else 400 invalid_generation_start). After the lease-tuple and status-supersession checks pass, it charges once when the marker matches the live claim generation, then deletes the marker in the same synchronous write.
    • Publication and acknowledgement-only finalizers still never charge, and a scheduled prepayment is consumed at generation start instead of debiting. clearExactReviewLease clears the marker.
  • Docs: docs/limits.md, docs/scheduler.md, and the dashboard/wrangler.toml rationale comment now describe generation-start charging. This is a comment-only change; no configured value changes. The perf(queue): charge organic executions and let scheduled backfill use its review budget #1710 and fix(queue): charge the review budget when a review lease is claimed #1736 proofs are marked historical. A changelog fragment is included.

Behavior proof

The proof is committed in docs/proof/scheduled-budget-generation-start/ (README and result.json). It uses the extended scripts/proof-scheduled-budget.mjs and runs the real ExactReviewQueue SQLite Durable Object in workerd with production wrangler.toml values. Both variants receive the same generation-start heartbeats.

  • Base: 6566c6974a (origin/main at proof time).
  • Head: 830d2c1f8b, clean runtime tree. After rebasing onto f7c8c55f33, dashboard/exact-review-queue.ts, dashboard/wrangler.toml, .github/workflows/sweep.yml and the harness are byte-identical to the proven files (same SHA-256 as the receipt).
  • Receipt SHA-256: 26cd16811d5b578edcf08adcd392931a515321f8a63b8c52f9d9f1d5b3ae8dda.
node scripts/proof-scheduled-budget.mjs 6566c6974a29b61193690f4fcc9a3181ee34c233 <tools-dir> <fresh-output-dir>

The production scenario models about 220 organic claims/hour, with a third exiting before generation. Totals are over 3 simulated hours:

Base (claim charging) Head (generation-start charging)
Scheduled admissions 34 223
Started reviews 480 669 (about 220/hour, the budget's target)
Claims that exited before generation 208, each charged 208, none charged
Global balance minimum, hours 2–3 −21 / −24 (floor) −4 / −1
Busiest rolling hour of started reviews 180 241 (allowance 244)
Organic admitted 100% 100%

Other properties the proof holds:

  • No regressions in the other ten scenarios. When every claim starts generation, the variants match within three. The one exception rises by design: 21 leases that were superseded during setup are no longer charged.
  • Throttle pause is identical. On both variants, 0 scheduled items are admitted during the 15-minute pause.
  • Reconciliation stays exactly-once. Two executions cost two tokens on both variants. The base charges at claim and successor_claim; the head charges at generation_start and successor_generation_start. Retries and replays cost 0.
  • Upgrade and rollback are safe on the same SQLite object (base → head → base, at 60/6 and 220/24). Leases the base charged at claim are not charged again at generation start, organic start plus retry costs 1, and a scheduled start consumes its prepayment. Rows and balance survive both reopenings.

Limits: the arrival mix, the exit-before-generation share, and latencies are synthetic but seeded. The proof uses no live inference, production state, or GitHub writes. The workflow shell is covered by the test below rather than by this harness.

Validation

  • pnpm run build:all, pnpm run lint and pnpm run check:static pass.
  • node --test test/dashboard-*.test.ts test/exact-review-*.test.ts test/sweep-workflow.test.ts: 1,323 tests, 0 failed.
  • The four new contract tests in test/dashboard-worker-queue-policy.test.ts fail on the base and pass on the head.
  • A new test/sweep-workflow.test.ts case runs the real verify_startup_authority against a stubbed control plane. It covers the owned and superseded responses, asserts the exact payload, and checks that both generation paths call it.
  • scripts/e2e/exact-review-start-authority.mjs was updated after the first CI run failed on it. It runs the real review-step shell against a loopback queue, but only on Linux, so the macOS run had skipped it. It now asserts that every startup-check attempt carries generation_start: true, including retries until one is accepted, and that every periodic heartbeat after an accepted start does not. All 12 scenarios pass in a Linux container (node:24-bookworm, Node 24.21.0) on head 69699c1cfd. For example, current sends 3 requests with 1 start, retry-current sends 503 then 200 with 2 starts, and revoked-after-start sends 2 requests with 1 start. test/exact-review-start-authority.test.ts additionally requires exactly one generation-start request on the current path.

Rollout and rollback

Charges cannot be doubled. Leases claimed and charged before the Worker deploy carry no marker, so their generation start is free (proven above). Two one-time undercharge windows remain:

  • Mixed versions at deploy. A run dispatched before the merge uses the old workflow and never sends generation_start. It goes uncharged only if its claim lands after the Worker deploy. Claims normally land about a minute after dispatch, and the Worker deploys a few minutes after the merge, so this mostly affects old-workflow runs whose runner pickup is delayed. The ceiling is the review runs in flight at deploy: 80 slots, with about 14 active as of 2026-10-04 22:27 UTC.
  • Rollback. After a revert, leases claimed by the new Worker that have not yet reached the startup check (the 1–2 minute setup window) are never charged. At about 250 claims/hour, that is roughly 5–10 leases.

An uncharged start costs one scheduled admission token, so the worst case is a single burst of extra scheduled reviews. It is bounded by the in-flight leases, and realistically single digits. A charge is never lost permanently, and nothing is charged twice.

Maintainer decision (Peter, 2026-10-04): accept the bounded transient and land once CI and the current ClawSweeper review are green. Dispatch is not paused during the deploy.

After the merge, the rollout is:

  1. Land at a quiet moment.
  2. Audit Actions for old-workflow runs that claimed after the Worker deploy; these are the only uncharged starts.
  3. Monitor the live budget fields below.

After deploy, watch:

  • scheduled_feed.token_balance should rise off the floor.
  • scheduled_feed.active and the lane balances should start spending.
  • lanes.review.shed_reasons_since_reset.scheduled_rate should grow more slowly.

OpenClaw Bay

Not affected. No public status field, value, or lifecycle stage changes. reviewBudgetChargeGeneration is internal item state.

Risks / rollback

Started Codex reviews rise toward the configured 220/hour, which is the budget's intended meter, instead of about 160/hour. Runs that exit early still cost a few GitHub requests (claim and live-item check) that the budget no longer meters. Those requests are small next to the ~30 a review makes, and the 220/hour rationale keeps more than half of the installation bucket free. Rollback is a revert; the base ignores the new field and marker.

Review

Codex /review (autoreview, branch mode against origin/main, P0–P1 threshold): clean on rebased head 69699c1cfd, which includes the e2e update; no accepted or actionable findings. It was also clean earlier on 02b72f8a83.

The ClawSweeper review of 02b72f8a83 reported proof sufficient and no findings. Its remaining items are the rollout decision above.

@steipete
steipete requested a review from a team as a code owner October 1, 2026 16:52
@clawsweeper clawsweeper Bot added P2 Normal priority bug or improvement with limited blast radius. merge-risk: 🚨 automation 🚨 Merging this PR could break CI, automerge, proof capture, label sync, or automation. proof: sufficient Contributor real behavior proof is sufficient. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 1, 2026
@clawsweeper

clawsweeper Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed October 4, 2026, 7:01 PM ET / 23:01 UTC (Revision 4).

ClawSweeper review

What this changes

Move review-budget charging to the workflow’s startup heartbeat so claimed runs that exit during setup leave capacity available for scheduled reviews.

Merge readiness

✅ Ready for maintainer review

This PR remains necessary: current main still charges at claim. No actionable patch defect remains, and Peter’s recorded acceptance resolves the prior rollout blocker.

Priority: P2
Reviewed head: 69699c1cfdb9b2dad3faa08c91e67975a9a1d81b

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused accounting repair with strong production-owner proof, verified source continuity, and no remaining actionable blocker.
Proof confidence 🦞 diamond lobster (5/6) ✨ media proof bonus Sufficient (linked_artifact): The real ExactReviewQueue runs in workerd/SQLite through claim, generation-start heartbeat, reconciliation, and native loopback dispatch; the receipt shows recovered scheduled capacity and exactly-once charging. Runtime and harness hashes match this head, with current Linux workflow-shell validation reported in the body. Same-object upgrade and rollback preserve records at both rate profiles; synthetic workload and transport fixtures limit claims about live quota or inference.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (linked_artifact): The real ExactReviewQueue runs in workerd/SQLite through claim, generation-start heartbeat, reconciliation, and native loopback dispatch; the receipt shows recovered scheduled capacity and exactly-once charging. Runtime and harness hashes match this head, with current Linux workflow-shell validation reported in the body. Same-object upgrade and rollback preserve records at both rate profiles; synthetic workload and transport fixtures limit claims about live quota or inference.
Evidence reviewed 10 items Pinned introduction and merge result: Reviewed the merge-base-to-head delta. The raw test-merge commit records the pinned main parent followed by the exact PR head; its result changes the same 15 introduced files and preserves main-only work.
Current main still needs the fix: Current main calls chargeClaimedReviewExecutionSync when granting a new claim. The merged accounting changes at #1736 do not implement generation-start charging; #1737 supplies telemetry rather than this repair.
Ownership checks precede accounting: The heartbeat validates phase and generation_start, rejects inactive or mismatched lease tuples and status-superseded owners, then consumes the pending-charge marker. Scheduled prepayment, publication exclusions, and ordinary heartbeat behavior are preserved. The change moves accounting without granting new execution authority.
Findings None None.
Security None None.

How this fits together

ClawSweeper’s exact-review queue receives organic events and scheduled review requests, then leases work to GitHub Actions. Its shared budget determines how much scheduled work can run alongside organic reviews.

flowchart TD
  A[Organic events] --> C[Exact review queue]
  B[Scheduled requests] --> C
  C --> D[Workflow claims lease]
  D --> E[Startup ownership check]
  E --> F[Charge once or consume prepayment]
  F --> G[Review generation]
  F --> H[Remaining scheduled capacity]
Loading

Before merge

None.

Agent review details

Security

None.

Review metrics

Metric Value Why it matters
Code growth production +23, tests +179, proof harnesses +153 net lines The production growth implements deferred accounting; most supporting growth exercises retries, early exits, and persistence.
Persisted compatibility 2 profiles, each base → head → base Both deployed-rate profiles preserve existing records and verify charging across upgrade and rollback.

Technical review

Best possible solution:

Meter review starts once per claim generation while preserving scheduled prepayment, with the accepted rollout audit and budget monitoring.

Do we have a high-confidence way to reproduce the issue?

Yes: main charges every new claim before generation, and the committed real-queue comparison demonstrates starvation when claims exit during setup. This read-only review inspected that evidence without rerunning it.

Is this the best way to solve the issue?

Yes: using the existing startup heartbeat is a narrow accounting repair, and the runtime receipt verifies retry idempotency, prepayment, and persisted compatibility.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 6e783d80e517.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: This is a bounded queue-accounting repair that restores scheduled capacity without changing configured rate limits.
  • merge-risk: 🚨 automation: Mixed Worker/workflow versions and rollback can temporarily undercharge reviews; Peter explicitly accepted this bounded tradeoff for the current scope.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (linked_artifact): The real ExactReviewQueue runs in workerd/SQLite through claim, generation-start heartbeat, reconciliation, and native loopback dispatch; the receipt shows recovered scheduled capacity and exactly-once charging. Runtime and harness hashes match this head, with current Linux workflow-shell validation reported in the body. Same-object upgrade and rollback preserve records at both rate profiles; synthetic workload and transport fixtures limit claims about live quota or inference.
  • proof: sufficient: Contributor real behavior proof is sufficient. The real ExactReviewQueue runs in workerd/SQLite through claim, generation-start heartbeat, reconciliation, and native loopback dispatch; the receipt shows recovered scheduled capacity and exactly-once charging. Runtime and harness hashes match this head, with current Linux workflow-shell validation reported in the body. Same-object upgrade and rollback preserve records at both rate profiles; synthetic workload and transport fixtures limit claims about live quota or inference.

Evidence

What I checked:

  • Pinned introduction and merge result: Reviewed the merge-base-to-head delta. The raw test-merge commit records the pinned main parent followed by the exact PR head; its result changes the same 15 introduced files and preserves main-only work. (e20249a44cd7)
  • Current main still needs the fix: Current main calls chargeClaimedReviewExecutionSync when granting a new claim. The merged accounting changes at fix(queue): charge the review budget when a review lease is claimed #1736 do not implement generation-start charging; feat(status): expose the scheduled review budget and shed reasons publicly #1737 supplies telemetry rather than this repair. (dashboard/exact-review-queue.ts:2948, 6e783d80e517)
  • Ownership checks precede accounting: The heartbeat validates phase and generation_start, rejects inactive or mismatched lease tuples and status-superseded owners, then consumes the pending-charge marker. Scheduled prepayment, publication exclusions, and ordinary heartbeat behavior are preserved. The change moves accounting without granting new execution authority. (dashboard/exact-review-queue.ts:3107, 69699c1cfdb9)
  • Workflow entrypoint: Both review paths invoke the existing startup ownership check with generation_start: true. Periodic heartbeats retain the original payload. The updated Linux shell harness checks startup retries and post-start heartbeats; the body reports all 12 scenarios passing on the pinned head. (.github/workflows/sweep.yml:1455, 69699c1cfdb9)
  • Production-owner runtime proof: Inspected the harness and parsed the committed receipt. Real workerd/SQLite queue routes with a native loopback transport record scheduled admissions rising from 34 to 223, with 208 early-exit claims uncharged and 669 started reviews. Reconciliation costs two tokens for two starts; retries cost zero; scheduled admission pauses during throttle. Arrival mix, clock, credentials, and GitHub responses are synthetic; no live inference or production writes are claimed. (docs/proof/scheduled-budget-generation-start/result.json:1, 69699c1cfdb9)
  • Proof identity remains current: Current queue, configuration, workflow, and budget-harness SHA-256 values match the receipt. Its complete file hash is 26cd16811d5b578edcf08adcd392931a515321f8a63b8c52f9d9f1d5b3ae8dda. This establishes source continuity with the exercised runtime, alongside the inspected scenarios. (docs/proof/scheduled-budget-generation-start/result.json:1, 69699c1cfdb9)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (3 earlier review cycles)
  • reviewed 2026-10-01T16:56:31.897Z sha 02b72f8 :: blocked before merge. :: none
  • reviewed 2026-10-04T10:51:12.186Z sha 02b72f8 :: blocked before merge. :: none
  • reviewed 2026-10-04T22:48:17.102Z sha 69699c1 :: blocked before merge. :: none

Organic runs claim about 220 leases/hour, and roughly a third exit before
Codex starts: live-item admission skips, or the lease is superseded during
setup. Charging the review budget at claim spent the whole 220/hour
allowance on organic claims, so scheduled hot intake and normal backfill
stayed shed with reason scheduled_rate.

A claim now records which claim generation still owes a token. The
workflow's existing startup ownership check (verify_startup_authority,
right before generation) sends generation_start: true on the lease
heartbeat. The queue charges that generation once there. A scheduled
prepayment is consumed at that point instead.

Retried starts, stale leases, plain heartbeats and status heartbeats never
charge. Leases claimed (and charged) under the previous Worker carry no
marker, so they are not charged twice during the rollout.
@steipete
steipete force-pushed the fix/charge-budget-at-generation-start branch from 02b72f8 to 69699c1 Compare October 4, 2026 22:43
@steipete
steipete merged commit c46e375 into main Oct 4, 2026
22 checks passed
@steipete
steipete deleted the fix/charge-budget-at-generation-start branch October 4, 2026 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 automation 🚨 Merging this PR could break CI, automerge, proof capture, label sync, or automation. P2 Normal priority bug or improvement with limited blast radius. proof: sufficient Contributor real behavior proof is sufficient. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant