feat(kernel): add token remaining/usage-ratio methods to BudgetStatus#1652
Open
SH20RAJ wants to merge 1 commit into
Open
feat(kernel): add token remaining/usage-ratio methods to BudgetStatus#1652SH20RAJ wants to merge 1 commit into
SH20RAJ wants to merge 1 commit into
Conversation
Fixes mofa-org#1475 BudgetStatus had convenience helpers for cost tracking (remaining_session_cost, remaining_daily_cost, session_cost_usage_ratio) but lacked the equivalent methods for token tracking, forcing callers to write manual calculations and creating an inconsistent API surface. Add four new methods that mirror the cost conveniences: - remaining_session_tokens() -> Option<u64> - remaining_daily_tokens() -> Option<u64> - session_token_usage_ratio() -> Option<f64> - daily_cost_usage_ratio() -> Option<f64> (completes cost side too) All return None when the corresponding limit is not configured. remaining_session_tokens / remaining_daily_tokens use saturating_sub to clamp to 0 on overrun; the ratio methods return a value >= 1.0 to signal overrun without panicking. Tests added: - test_budget_status_token_remaining_methods - test_budget_status_token_usage_ratio - test_budget_status_token_remaining_clamps_to_zero - test_budget_status_no_token_limits_returns_none
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
Fixes #1475.
BudgetStatusincrates/mofa-kernel/src/budget.rsexposes threeconvenience methods for cost tracking but nothing equivalent for
token tracking, forcing callers to write manual calculations:
New API
This PR adds four methods that complete the symmetry between the cost
and token dimensions:
All methods return
Nonewhen the corresponding limit is not configured,matching the existing cost-method contract exactly.
Design Notes
remaining_*_tokensusessaturating_subso it always returns0rather than panicking on overrun (u64 arithmetic).
session_token_usage_ratioreturns a value > 1.0 on overrun, whichlets callers distinguish "near limit" from "already exceeded" without a
separate
is_exceeded()call.Tests added
test_budget_status_token_remaining_methodstest_budget_status_token_usage_ratiotest_budget_status_token_remaining_clamps_to_zerotest_budget_status_no_token_limits_returns_nonecargo test -p mofa-kernel budget::testsChecklist