Repository navigation
fix(dream): settle actual spend per user - #21416
Merged
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dream admission previously accumulated every pass's worst-case reservation in
dream_spend/<UTC date>. That document was shared across all users, so one owner's full-context passes or the synthetic canary could exhaust another owner's allowance; successful passes never settled to observed usage. With K=3, the $250 cap therefore admitted only about nine worst-case reservations.New admissions use
dream_users/<uid>/spend/<UTC date>, shared only by that user's scheduled/manual passes and service hosts. Each lease captures its accounting rate and current budget charge. Finishing the lease atomically replaces that charge withaggregate tokens * admission accounting ratein the same Firestore transaction that writes the report and finishes the lease. Aggregate transport usage already includes all samples and JSON retries; the local manual endpoint tests verify that it becomes the ledger charge. Known billable failures settle too. Unknown or missing usage keeps the full reservation, even if the surviving samples produce a successful plan.Settlement remains on the admission UTC day across midnight. Retained leases store the current charge so repeated recovery cannot deduct the same reservation twice; zero-usage refunds also refund the admission counter only once. Released leases reject duplicate finish attempts. The report and budget record use the same computed actual cost, rather than trusting a separately supplied cost value. Actual usage is not clamped to an underestimated reservation.
At the requested accounting rate, 24 full passes of 820,000 tokens settle to $196.80. The 24th admission temporarily requires $214.56608 ($188.60 already settled plus $25.96608 reserved), below the existing $250 backend-sync cap. User and canary ledgers are independent; scheduled/manual run-count caps still apply. No rollout configuration, provider route, or capacity policy changes.
Migration boundary: new user ledgers start without importing the unattributable historical global total. Historical global records are left intact. A pre-change in-flight lease continues to use its original global record and retains the reservation when its admission rate is unavailable; explicit known-zero refunds still release it. There is no production backfill or lease stealing.
Validation:
backend/test.sh: 628 tests, including independent user admission, 24-pass cadence, aggregate sample/retry costs, known failures, unknown usage, UTC rollover, frozen rates, duplicate finish, retained-lease recovery and pre-write report failure.backend/test.shagainst an isolated loopback Firestore emulator with a synthetic demo project and anonymous credentials. The real SDK contention test commits exactly one concurrent finish, leaves the other user's reservation unchanged, and preserves unknown usage.backend/test.sh: 460 passed. The finish driver now seeds the complete authoritative lease metadata.make preflightpassed all 31 selected checks; all four PR metadata checks passed.make setup-backendbefore final verification.Product invariants affected
none
Failure-Class: none
No existing registered semantic class describes reservation-to-actual settlement and per-user spend ownership. The brief prohibits
.githubedits; reusable behavioral prevention is inbackend/tests/unit/test_dream_budget_settlement.pyand the real local emulator contention test.