Skip to content

fix(dream): settle actual spend per user - #21416

Merged
Git-on-my-level merged 3 commits into
mainfrom
lane/dream-fullctx-4
Oct 13, 2026
Merged

Git-on-my-level merged 3 commits into
mainfrom
lane/dream-fullctx-4

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

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 with aggregate tokens * admission accounting rate in 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:

  • All 30 Dream unit-suite files passed through 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.
  • All 14 affected integration tests passed through backend/test.sh against 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.
  • Firestore query-shape unit tests passed through backend/test.sh: 460 passed. The finish driver now seeds the complete authoritative lease metadata.
  • Required make preflight passed all 31 selected checks; all four PR metadata checks passed.
  • Backend type checking: zero errors; warnings remain. The missing worktree environment was restored with canonical make setup-backend before final verification.
  • No production customer reads/writes, cloud credentials, live model inference or deployment. The emulator was stopped after the test run.

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 .github edits; reusable behavioral prevention is in backend/tests/unit/test_dream_budget_settlement.py and the real local emulator contention test.

View guided diff Turn on auto-fix

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T16:33:58.785019Z 19e0f78 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Git-on-my-level
Git-on-my-level merged commit 11f317f into main Oct 13, 2026
63 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/dream-fullctx-4 branch October 13, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant