adds reset budget honor flow - #6002
Merged
Merged
Conversation
Contributor
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis change adds explicit virtual-key budget usage reset support. It resets persisted and in-memory usage, preserves ChangesBudget usage reset
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant VirtualKeyUpdate
participant GovernanceManager
participant BifrostHTTPServer
participant GovernanceStore
participant BudgetDatabase
VirtualKeyUpdate->>GovernanceManager: process reset_budget_usage
GovernanceManager->>BudgetDatabase: persist zero usage
GovernanceManager->>BifrostHTTPServer: reset recorded budgets
BifrostHTTPServer->>GovernanceStore: clear in-memory usage
GovernanceStore->>GovernanceStore: increment reset generation
GovernanceStore-->>BifrostHTTPServer: return reset result
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment |
This was referenced Aug 9, 2026
Merged
akshaydeo
marked this pull request as ready for review
August 9, 2026 22:16
Contributor
Author
This was referenced Aug 9, 2026
coderabbitai
Bot
requested review from
Pratham-Mishra04,
danpiths and
roroghost17
August 9, 2026 22:17
Contributor
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@plugins/governance/store.go`:
- Around line 2047-2053: Synchronize ResetBudgetUsageInMemory with DumpBudgets
so a dump cannot persist stale CurrentUsage after the in-memory reset updates
LastDBUsagesBudgets. Use the existing budget/reset synchronization or add a
generation check that invalidates snapshots taken before the reset, while
preserving the reset’s database behavior; add a test covering the interleaving
where DumpBudgets snapshots before ResetBudgetUsageInMemory completes.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 06eabe75-3c2e-4298-bdd0-94a4f0c77ea9
📒 Files selected for processing (5)
plugins/governance/budgetcycle_test.goplugins/governance/store.gotransports/bifrost-http/handlers/governance.gotransports/bifrost-http/handlers/pricing_override_test.gotransports/bifrost-http/server/server.go
This was referenced Aug 9, 2026
akshaydeo
force-pushed
the
quarterly-budget-docs
branch
from
August 10, 2026 04:37
40ad078 to
3b145d7
Compare
akshaydeo
force-pushed
the
honour-reset-budget-usage
branch
from
August 10, 2026 04:37
c148a5f to
c2e580a
Compare
danpiths
previously approved these changes
Aug 10, 2026
akshaydeo
force-pushed
the
quarterly-budget-docs
branch
from
August 10, 2026 06:57
3b145d7 to
b1da7d1
Compare
akshaydeo
force-pushed
the
honour-reset-budget-usage
branch
from
August 10, 2026 06:57
c2e580a to
aff9859
Compare
17 tasks
akshaydeo
force-pushed
the
honour-reset-budget-usage
branch
from
August 10, 2026 21:38
aff9859 to
1ef8496
Compare
Contributor
Author
Merge activity
|
akshaydeo
changed the base branch from
quarterly-budget-docs
to
graphite-base/6002
August 10, 2026 21:48
akshaydeo
dismissed stale reviews from danpiths and coderabbitai[bot]
August 10, 2026 21:51
The base branch was changed.
akshaydeo
force-pushed
the
honour-reset-budget-usage
branch
from
August 10, 2026 21:56
1ef8496 to
26a50f0
Compare
atharvamhaske
pushed a commit
to atharvamhaske/bifrost
that referenced
this pull request
Aug 13, 2026
## Summary Adds an operator-triggered budget usage reset that clears a virtual key's spend without advancing its reset boundary. Previously, there was no way to zero a budget's `CurrentUsage` outside of a natural window expiry — the config write path deliberately preserves live accounting values, and `ReloadVirtualKey` carries cached usage forward. This PR threads a `ResetBudgetUsage` flag from the update request through DB persistence, in-memory governance store clearing, and (in enterprise) cluster-wide propagation. ## Changes - **`ResetBudgetUsageInMemory` on `GovernanceStore`**: New method on `LocalGovernanceStore` that calls `RebaseBudget` with zero usage and simultaneously zeroes the `LastDBUsages` baseline. The baseline must be cleared alongside usage — the dump path writes the delta between in-memory usage and that baseline, so a stale baseline would immediately re-add the spend that was just cleared. - **`budgetUsageReset` struct**: Carries the operator's reset intent through budget reconciliation (`reconcileModelConfigBudgets`, `reconcileVKModelConfig`, `syncVKGovernanceToModelConfigs`) and collects the IDs of budgets that were actually reset, so the in-memory store can be cleared after the transaction commits. A nil pointer means no reset was requested, keeping all existing call sites unaffected. - **`UpdateVirtualKey` handler**: Reads `req.ResetBudgetUsage`, constructs a `budgetUsageReset`, passes it through reconciliation, and after `ReloadVirtualKey` completes calls `ResetBudgetUsageInMemory` on the governance manager. The ordering is deliberate — the reload must happen first because it carries cached usage forward, which would otherwise undo the reset. - **`BifrostHTTPServer.ResetBudgetUsageInMemory`**: Implements the `ServerCallbacks` and `GovernanceManager` interfaces, iterating the collected budget IDs and calling the store method for each. Missing budgets are logged and skipped rather than treated as errors, since a budget can be deleted in the same request that requested the reset. - **`LastReset` is intentionally left untouched**: Every persistence path guards `LastReset` forward-only, and the operator reset targets a window that has not yet closed. Moving the boundary is explicitly ruled out. ## Type of change - [ ] Bug fix - [x] Feature - [ ] Refactor - [ ] Documentation - [ ] Chore/CI ## Affected areas - [x] Core (Go) - [x] Transports (HTTP) - [ ] Providers/Integrations - [x] Plugins - [ ] UI (React) - [ ] Docs ## How to test ```sh go test ./plugins/governance/... -run TestResetBudgetUsageInMemory go test ./plugins/governance/... -run TestResetBudgetUsageInMemoryMissingBudget go test ./transports/bifrost-http/... go test ./... ``` 1. Create a virtual key with a monthly budget and generate enough traffic to accumulate spend. 2. Call the update virtual key endpoint with `"reset_budget_usage": true`. 3. Verify `CurrentUsage` is zero in the database and that subsequent requests are not blocked by the previous spend. 4. Verify `LastReset` has not changed. 5. Verify that a subsequent config dump does not restore the old usage value. ## Breaking changes - [ ] Yes - [x] No ## Related issues ## Security considerations The reset is operator-gated through the existing virtual key update endpoint. No new authentication surface is introduced. The reset does not expose usage history or allow usage to be set to an arbitrary value — only zeroed. ## Checklist - [x] I read `docs/contributing/README.md` and followed the guidelines - [x] I added/updated tests where appropriate - [ ] I updated documentation where needed - [x] I verified builds succeed (Go and UI) - [ ] I verified the CI pipeline passes locally if applicable
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.

Summary
Adds an operator-triggered budget usage reset that clears a virtual key's spend without advancing its reset boundary. Previously, there was no way to zero a budget's
CurrentUsageoutside of a natural window expiry — the config write path deliberately preserves live accounting values, andReloadVirtualKeycarries cached usage forward. This PR threads aResetBudgetUsageflag from the update request through DB persistence, in-memory governance store clearing, and (in enterprise) cluster-wide propagation.Changes
ResetBudgetUsageInMemoryonGovernanceStore: New method onLocalGovernanceStorethat callsRebaseBudgetwith zero usage and simultaneously zeroes theLastDBUsagesbaseline. The baseline must be cleared alongside usage — the dump path writes the delta between in-memory usage and that baseline, so a stale baseline would immediately re-add the spend that was just cleared.budgetUsageResetstruct: Carries the operator's reset intent through budget reconciliation (reconcileModelConfigBudgets,reconcileVKModelConfig,syncVKGovernanceToModelConfigs) and collects the IDs of budgets that were actually reset, so the in-memory store can be cleared after the transaction commits. A nil pointer means no reset was requested, keeping all existing call sites unaffected.UpdateVirtualKeyhandler: Readsreq.ResetBudgetUsage, constructs abudgetUsageReset, passes it through reconciliation, and afterReloadVirtualKeycompletes callsResetBudgetUsageInMemoryon the governance manager. The ordering is deliberate — the reload must happen first because it carries cached usage forward, which would otherwise undo the reset.BifrostHTTPServer.ResetBudgetUsageInMemory: Implements theServerCallbacksandGovernanceManagerinterfaces, iterating the collected budget IDs and calling the store method for each. Missing budgets are logged and skipped rather than treated as errors, since a budget can be deleted in the same request that requested the reset.LastResetis intentionally left untouched: Every persistence path guardsLastResetforward-only, and the operator reset targets a window that has not yet closed. Moving the boundary is explicitly ruled out.Type of change
Affected areas
How to test
"reset_budget_usage": true.CurrentUsageis zero in the database and that subsequent requests are not blocked by the previous spend.LastResethas not changed.Breaking changes
Related issues
Security considerations
The reset is operator-gated through the existing virtual key update endpoint. No new authentication surface is introduced. The reset does not expose usage history or allow usage to be set to an arbitrary value — only zeroed.
Checklist
docs/contributing/README.mdand followed the guidelines