Support fractional rollout budgets and configurable context reminders (#61579)

## What changed

- Add a positive, finite `features.rollout_budget.limit_budget` allowance while retaining `limit_tokens` compatibility. Reject configurations that specify both.
- Preserve fractional balances in reminders and compare thresholds without rounding the balance down.
- Accumulate charges with compensated summation and allow spending exactly the allowance, including floating-point rounding tolerance for cases such as `0.1 + 0.2` against `0.3`.
- Add `reminder_at_context_start`, defaulting to `true`. Set it to `false` to suppress reminders at thread start and after compaction; combine it with an empty threshold list to disable budget reminders entirely.

## Testing

Extend configuration and rollout-budget tests to cover fractional allowances, legacy integer persistence, invalid limits, threshold-only reminders, and enforcement at decimal equality and after overspending.

GitOrigin-RevId: b8e7a2faa8d39e562bcae196364de419698bc67d
This commit is contained in:
tongzhou wang
2026-11-14 23:53:23 +00:00
committed by copyberry
parent f226c9ccfb
commit c8e4cbcd94
17 changed files with 279 additions and 93 deletions
+11
View File
@@ -4815,7 +4815,14 @@
"description": "Stop when the shared budget is exhausted. Defaults to true.",
"type": "boolean"
},
"limit_budget": {
"description": "Positive, finite allowance in budget units. Set either limit_tokens or limit_budget. Spending exactly this amount is allowed.",
"exclusiveMinimum": 0.0,
"format": "double",
"type": "number"
},
"limit_tokens": {
"description": "Positive whole-token allowance. Set either limit_tokens or limit_budget.",
"format": "int64",
"minimum": 1.0,
"type": "integer"
@@ -4825,6 +4832,10 @@
"minimum": 0.0,
"type": "number"
},
"reminder_at_context_start": {
"description": "Restate the balance when a thread starts or its context is compacted. Defaults to true. Set false with an empty threshold list for no budget reminders.",
"type": "boolean"
},
"reminder_at_remaining_tokens": {
"description": "Remaining weighted-token values that trigger reminders when crossed.",
"items": {
+54 -11
View File
@@ -949,7 +949,7 @@ async fn load_config_resolves_rollout_budget() -> std::io::Result<()> {
r#"
[features.rollout_budget]
enabled = true
limit_tokens = 100000
limit_budget = 100000.5
reminder_at_remaining_tokens = [50000, 25000, 10000]
sampling_token_weight = 1.0
prefill_token_weight = 0.1
@@ -969,20 +969,66 @@ prefill_token_weight = 0.1
config.rollout_budget,
Some(RolloutBudgetConfig {
enforce_limit: true,
limit_tokens: 100_000,
limit_budget: 100_000.5,
reminder_at_context_start: true,
reminder_at_remaining_tokens: vec![50_000, 25_000, 10_000],
sampling_token_weight: 1.0,
prefill_token_weight: 0.1,
})
);
// A persisted integer limit and threshold may differ by one beyond f64's exact range.
let legacy: ConfigToml = toml::from_str(
"[features.rollout_budget]\nenabled = true\nlimit_tokens = 9007199254740993\nreminder_at_remaining_tokens = [9007199254740992]\n",
)
.expect("legacy integer budget should deserialize");
let persisted = serde_json::to_value(&legacy).expect("serialize persisted config");
let persisted_budget = &persisted["features"]["rollout_budget"];
assert_eq!(
persisted_budget["limit_tokens"].as_i64(),
Some(9_007_199_254_740_993)
);
assert!(persisted_budget.get("limit_budget").is_none());
let config = Config::load_from_base_config_with_overrides(
legacy,
ConfigOverrides::default(),
codex_home.abs(),
)
.await?;
let resolved = config.rollout_budget.expect("legacy budget should resolve");
assert_eq!(
resolved.reminder_at_remaining_tokens,
vec![9_007_199_254_740_992]
);
Ok(())
}
#[tokio::test]
async fn load_config_rejects_enabled_rollout_budget_without_limit() -> std::io::Result<()> {
for config_toml in [
"[features]\nrollout_budget = true\n",
"[features.rollout_budget]\nenabled = true\n",
async fn load_config_rejects_invalid_rollout_budget_limits() -> std::io::Result<()> {
for (config_toml, expected_error) in [
(
"[features]\nrollout_budget = true\n",
"features.rollout_budget requires limit_tokens or limit_budget when enabled",
),
(
"[features.rollout_budget]\nenabled = true\n",
"features.rollout_budget requires limit_tokens or limit_budget when enabled",
),
(
"[features.rollout_budget]\nenabled = true\nlimit_tokens = 100\nlimit_budget = 100.5\n",
"features.rollout_budget.limit_tokens and limit_budget cannot both be set",
),
(
"[features.rollout_budget]\nenabled = false\nlimit_tokens = 100\nlimit_budget = 100.5\n",
"features.rollout_budget.limit_tokens and limit_budget cannot both be set",
),
(
"[features.rollout_budget]\nenabled = true\nlimit_tokens = 0\n",
"features.rollout_budget allowance must be finite and positive",
),
(
"[features.rollout_budget]\nenabled = true\nlimit_budget = inf\n",
"features.rollout_budget allowance must be finite and positive",
),
] {
let codex_home = tempdir()?;
let config_toml: ConfigToml =
@@ -993,13 +1039,10 @@ async fn load_config_rejects_enabled_rollout_budget_without_limit() -> std::io::
codex_home.abs(),
)
.await
.expect_err("enabled rollout budget without limit_tokens should be rejected");
.expect_err("invalid rollout budget should be rejected");
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
assert_eq!(
err.to_string(),
"features.rollout_budget.limit_tokens is required when rollout_budget is enabled"
);
assert_eq!(err.to_string(), expected_error);
}
Ok(())
}
+43 -18
View File
@@ -1401,12 +1401,21 @@ impl Default for TokenBudgetConfig {
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct RolloutBudgetConfig {
pub enforce_limit: bool,
pub limit_tokens: i64,
/// Positive, finite allowance, including fractional opaque units.
pub limit_budget: f64,
pub reminder_at_context_start: bool,
pub reminder_at_remaining_tokens: Vec<i64>,
pub sampling_token_weight: f64,
pub prefill_token_weight: f64,
}
/// An integer threshold is reached when the actual fractional balance is at
/// or below it. Round the balance up, not the threshold to `f64`: an `i64`
/// threshold above 2^53 is still exact, and every threshold fits in `i128`.
pub(crate) fn rollout_budget_threshold_reached(remaining_units: f64, threshold: i64) -> bool {
remaining_units.ceil() as i128 <= i128::from(threshold)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct CurrentTimeReminderConfig {
pub reminder_interval_seconds: u64,
@@ -3053,31 +3062,42 @@ fn resolve_rollout_budget_config(
config_toml: &ConfigToml,
features: &ManagedFeatures,
) -> std::io::Result<Option<RolloutBudgetConfig>> {
let config = config_toml
.features
.as_ref()
.and_then(|features| features.rollout_budget.as_ref());
if let Some(FeatureToml::Config(config)) = config
&& config.limit_tokens.is_some()
&& config.limit_budget.is_some()
{
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"features.rollout_budget.limit_tokens and limit_budget cannot both be set",
));
}
if !features.enabled(Feature::RolloutBudget) {
return Ok(None);
}
let missing_limit_error = || {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"features.rollout_budget.limit_tokens is required when rollout_budget is enabled",
"features.rollout_budget requires limit_tokens or limit_budget when enabled",
)
};
let Some(FeatureToml::Config(config)) = config_toml
.features
.as_ref()
.and_then(|features| features.rollout_budget.as_ref())
else {
let Some(FeatureToml::Config(config)) = config else {
return Err(missing_limit_error());
};
let Some(limit_tokens) = config.limit_tokens else {
return Err(missing_limit_error());
};
if limit_tokens <= 0 {
let limit_budget = config
.limit_budget
.or_else(|| config.limit_tokens.map(|tokens| tokens as f64))
.ok_or_else(missing_limit_error)?;
if !limit_budget.is_finite() || limit_budget <= 0.0 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"features.rollout_budget.limit_tokens must be positive",
"features.rollout_budget allowance must be finite and positive",
));
}
let reminder_at_context_start = config.reminder_at_context_start.unwrap_or(true);
let reminder_at_remaining_tokens =
config
.reminder_at_remaining_tokens
@@ -3088,13 +3108,17 @@ fn resolve_rollout_budget_config(
"features.rollout_budget.reminder_at_remaining_tokens is required when rollout_budget is enabled",
)
})?;
if reminder_at_remaining_tokens
.iter()
.any(|&tokens| tokens <= 0 || tokens >= limit_tokens)
{
if reminder_at_remaining_tokens.iter().any(|&tokens| {
// Persisted token allowances and thresholds are exact integers, including above 2^53.
tokens <= 0
|| match config.limit_tokens {
Some(limit) => tokens >= limit,
None => rollout_budget_threshold_reached(limit_budget, tokens),
}
}) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"features.rollout_budget.reminder_at_remaining_tokens must contain only positive values below limit_tokens",
"features.rollout_budget.reminder_at_remaining_tokens must contain only positive values below the rollout allowance",
));
}
let sampling_token_weight = config.sampling_token_weight.unwrap_or(1.0);
@@ -3112,7 +3136,8 @@ fn resolve_rollout_budget_config(
}
Ok(Some(RolloutBudgetConfig {
enforce_limit: config.enforce_limit.unwrap_or(true),
limit_tokens,
limit_budget,
reminder_at_context_start,
reminder_at_remaining_tokens,
sampling_token_weight,
prefill_token_weight,
+4 -3
View File
@@ -3,9 +3,9 @@ use codex_protocol::models::ContentItem;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::input_text;
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct RolloutBudgetContext {
pub(crate) remaining_tokens: i64,
pub(crate) remaining_units: f64,
}
impl ContextualUserFragment for RolloutBudgetContext {
@@ -26,9 +26,10 @@ impl ContextualUserFragment for RolloutBudgetContext {
}
fn body(&self) -> ContentItem {
// "Weighted tokens" is the legacy wording. Provider-reported units can be opaque.
input_text(format!(
"You have {} weighted tokens left in the shared session token budget.",
self.remaining_tokens
self.remaining_units
))
}
}
+1 -1
View File
@@ -44,7 +44,7 @@ pub(crate) fn usage_tags(
"token_budget.present": config.token_budget.is_some(),
"token_budget.reminder_threshold_tokens": config.token_budget.as_ref().and_then(|budget| budget.reminder_threshold_tokens),
"token_budget.auto_compact_fallback_buffer_tokens": config.token_budget.as_ref().and_then(|budget| budget.auto_compact_fallback_buffer_tokens),
"rollout_budget.limit_tokens": config.rollout_budget.as_ref().map(|budget| budget.limit_tokens),
"rollout_budget.limit_budget": config.rollout_budget.as_ref().map(|budget| budget.limit_budget),
"rollout_budget.sampling_token_weight": config.rollout_budget.as_ref().map(|budget| budget.sampling_token_weight),
"rollout_budget.prefill_token_weight": config.rollout_budget.as_ref().map(|budget| budget.prefill_token_weight),
});
+1 -1
View File
@@ -252,7 +252,7 @@ pub(crate) async fn finalize(
.await
{
let reminder = ContextualUserFragment::into(crate::context::RolloutBudgetContext {
remaining_tokens: reminder.remaining_tokens,
remaining_units: reminder.remaining_units,
});
existing = existing.saturating_add(
usize::try_from(estimate_item_token_count(&reminder)).unwrap_or(usize::MAX),
+17 -13
View File
@@ -1,4 +1,6 @@
use crate::config::RolloutBudgetConfig;
use crate::config::rollout_budget_threshold_reached;
use codex_protocol::BudgetUnitsSum;
use codex_protocol::ThreadId;
use codex_protocol::error::CodexErr;
use codex_protocol::error::Result as CodexResult;
@@ -12,7 +14,7 @@ use std::sync::atomic::Ordering;
/// Budget reminder returned by a controller and acknowledged after history insertion.
pub struct RolloutBudgetReminder {
pub remaining_tokens: i64,
pub remaining_units: f64,
/// Backend-defined reminder position, returned unchanged on acknowledgement.
pub reminder_index: i64,
}
@@ -27,7 +29,7 @@ pub(crate) struct RolloutBudget {
struct RolloutBudgetState {
config: RolloutBudgetConfig,
weighted_tokens_used: f64,
posted: BudgetUnitsSum,
/// Last reminder delivered to each thread, so every thread observes crossed thresholds.
deliveries: HashMap<ThreadId, ThreadBudgetDelivery>,
}
@@ -42,7 +44,7 @@ impl RolloutBudget {
self.state.get_or_init(|| {
Mutex::new(RolloutBudgetState {
config,
weighted_tokens_used: 0.0,
posted: BudgetUnitsSum::default(),
deliveries: HashMap::new(),
})
});
@@ -66,9 +68,11 @@ impl RolloutBudget {
usage.output_tokens.max(0) as f64 * state.config.sampling_token_weight
+ usage.non_cached_input() as f64 * state.config.prefill_token_weight
};
state.weighted_tokens_used += units;
state.posted.add(units);
// Equality is allowed, including one rounding step (0.1 + 0.2 versus 0.3).
let exhausted = state.config.enforce_limit
&& state.weighted_tokens_used >= state.config.limit_tokens as f64;
&& (state.posted.total().is_infinite()
|| state.posted.total() > state.config.limit_budget.next_up());
if exhausted {
self.stop_new_tools();
}
@@ -89,22 +93,22 @@ impl RolloutBudget {
window_id: &str,
) -> Option<RolloutBudgetReminder> {
let state = self.lock()?;
let remaining_tokens = (state.config.limit_tokens as f64 - state.weighted_tokens_used)
.max(0.0)
.floor() as i64;
let remaining_units = (state.config.limit_budget - state.posted.total()).max(0.0);
let reminder_index = state
.config
.reminder_at_remaining_tokens
.iter()
.filter(|&&threshold| remaining_tokens <= threshold)
.filter(|&&threshold| rollout_budget_threshold_reached(remaining_units, threshold))
.count() as i64;
if state.deliveries.get(&thread_id).is_some_and(|delivery| {
delivery.window_id.as_str() == window_id && delivery.reminder_index >= reminder_index
}) {
let previous = state.deliveries.get(&thread_id);
let crossed_threshold = reminder_index > previous.map_or(0, |d| d.reminder_index);
let new_window = state.config.reminder_at_context_start
&& previous.is_none_or(|d| d.window_id != window_id);
if !crossed_threshold && !new_window {
return None;
}
Some(RolloutBudgetReminder {
remaining_tokens,
remaining_units,
reminder_index,
})
}
+1 -1
View File
@@ -26,7 +26,7 @@ pub(super) async fn maybe_record_reminder(
return;
};
let response_item = ContextualUserFragment::into(crate::context::RolloutBudgetContext {
remaining_tokens: reminder.remaining_tokens,
remaining_units: reminder.remaining_units,
});
sess.record_conversation_items(
turn_context,
@@ -278,7 +278,8 @@ async fn mcp_budget_stop_at_handoff(phase: McpPause) -> anyhow::Result<()> {
.rollout_budget()
.configure(crate::config::RolloutBudgetConfig {
enforce_limit: true,
limit_tokens: 1,
limit_budget: 1.0,
reminder_at_context_start: true,
reminder_at_remaining_tokens: Vec::new(),
sampling_token_weight: 1.0,
prefill_token_weight: 1.0,
+3 -2
View File
@@ -1826,7 +1826,8 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history
let mut config = test_config().await;
config.rollout_budget = Some(RolloutBudgetConfig {
enforce_limit: true,
limit_tokens: 100,
limit_budget: 100.0,
reminder_at_context_start: true,
reminder_at_remaining_tokens: vec![75, 50, 25],
sampling_token_weight: 1.0,
prefill_token_weight: 1.0,
@@ -1989,7 +1990,7 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history
.pending_budget_reminder(parent.thread_id, "window")
.await
.expect("parent budget reminder");
assert_eq!(reminder.remaining_tokens, 75);
assert_eq!(reminder.remaining_units, 75.0);
assert_eq!(reviewer_config.parent_thread_id, Some(parent.thread_id));
assert_eq!(reviewer_config.forked_from_thread_id, None);
assert_eq!(reviewer_config.originator, "codex_work_desktop");
+3 -2
View File
@@ -1037,7 +1037,8 @@ mod tests {
.rollout_budget()
.configure(crate::config::RolloutBudgetConfig {
enforce_limit: true,
limit_tokens: 1,
limit_budget: 1.0,
reminder_at_context_start: true,
reminder_at_remaining_tokens: Vec::new(),
sampling_token_weight: 1.0,
prefill_token_weight: 1.0,
@@ -1128,7 +1129,7 @@ mod tests {
.await
.expect("the tool should be queued behind the held slot");
let usage = codex_protocol::protocol::TokenUsage {
output_tokens: 1,
output_tokens: 2,
..Default::default()
};
session
@@ -51,7 +51,8 @@ async fn first_request_item_types_roles_and_content_annotations() -> Result<()>
});
config.rollout_budget = Some(RolloutBudgetConfig {
enforce_limit: true,
limit_tokens: 100,
limit_budget: 100.0,
reminder_at_context_start: true,
reminder_at_remaining_tokens: Vec::new(),
sampling_token_weight: 1.0,
prefill_token_weight: 1.0,
@@ -280,7 +280,8 @@ async fn review_respects_complete_context_budget(
config.current_time_reminder = Some(CurrentTimeReminderConfig::default());
config.rollout_budget = Some(RolloutBudgetConfig {
enforce_limit: true,
limit_tokens: 1_000_000,
limit_budget: 1_000_000.0,
reminder_at_context_start: true,
reminder_at_remaining_tokens: Vec::new(),
sampling_token_weight: 1.0,
prefill_token_weight: 1.0,
+61 -38
View File
@@ -108,7 +108,8 @@ impl ThreadLifecycleContributor<Config> for HostAccounting {
pub(super) fn rollout_budget() -> RolloutBudgetConfig {
RolloutBudgetConfig {
enforce_limit: true,
limit_tokens: 100,
limit_budget: 100.0,
reminder_at_context_start: true,
reminder_at_remaining_tokens: vec![75, 50, 25],
sampling_token_weight: 1.0,
prefill_token_weight: 1.0,
@@ -123,7 +124,7 @@ fn rollout_budget_texts(request: &ResponsesRequest) -> Vec<String> {
.collect()
}
fn rollout_budget_message(remaining_tokens: i64) -> String {
fn rollout_budget_message(remaining_tokens: impl std::fmt::Display) -> String {
format!(
"<rollout_budget>\nYou have {remaining_tokens} weighted tokens left in the shared session token budget.\n</rollout_budget>"
)
@@ -134,10 +135,11 @@ fn wire_request_contains(request: &wiremock::Request, text: &str) -> bool {
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[test_case(None ; "weighted token usage")]
#[test_case(Some(40.5) ; "provider budget units")]
#[test_case(None, true ; "weighted tokens and initial reminder")]
#[test_case(Some(40.5), false ; "opaque units and threshold reminders only")]
async fn adds_weighted_initial_and_threshold_reminders(
rollout_budget_units: Option<f64>,
initial_reminder: bool,
) -> Result<()> {
skip_if_no_network!(Ok(()));
@@ -167,11 +169,18 @@ async fn adds_weighted_initial_and_threshold_reminders(
)
.await;
let test = test_codex()
.with_config(|config| {
.with_config(move |config| {
let mut budget = rollout_budget();
if !initial_reminder {
// Starting at 100.75 has not crossed the threshold at 100.
budget.limit_budget = 100.75;
budget.reminder_at_remaining_tokens = vec![100];
}
config.rollout_budget = Some(RolloutBudgetConfig {
reminder_at_context_start: initial_reminder,
sampling_token_weight: 2.0,
prefill_token_weight: 0.5,
..rollout_budget()
..budget
});
})
.build(&server)
@@ -181,22 +190,21 @@ async fn adds_weighted_initial_and_threshold_reminders(
test.submit_turn("second turn").await?;
let requests = responses.requests();
assert!(requests[0].has_content_kinds(&["rollout_budget.remaining_tokens"]));
assert_eq!(
rollout_budget_texts(&requests[0]),
vec![rollout_budget_message(/*remaining_tokens*/ 100)]
);
assert_eq!(
rollout_budget_texts(&requests[1]),
vec![
rollout_budget_message(/*remaining_tokens*/ 100),
rollout_budget_message(if rollout_budget_units.is_some() {
59
} else {
60
}),
]
requests[0].has_content_kinds(&["rollout_budget.remaining_tokens"]),
initial_reminder
);
let mut expected: Vec<_> = initial_reminder
.then(|| rollout_budget_message(/*remaining_tokens*/ 100))
.into_iter()
.collect();
assert_eq!(rollout_budget_texts(&requests[0]), expected);
expected.push(rollout_budget_message(if rollout_budget_units.is_some() {
60.25
} else {
60.0
}));
assert_eq!(rollout_budget_texts(&requests[1]), expected);
Ok(())
}
@@ -352,7 +360,7 @@ async fn subagent_usage_draws_from_the_shared_budget(host_accounting: bool) -> R
.expect("test config should allow multi-agent v2");
config.rollout_budget = Some(RolloutBudgetConfig {
// Host success must not also charge the Local budget.
limit_tokens: if host_accounting { 1 } else { 100 },
limit_budget: if host_accounting { 1.0 } else { 100.0 },
..rollout_budget()
});
})
@@ -426,16 +434,31 @@ async fn exhausted_budget_respects_enforcement_on_current_and_later_turns(
skip_if_no_network!(Ok(()));
let server = start_mock_server().await;
let mut sequence = vec![sse(vec![
ev_response_created("exhaust-budget"),
ev_completed_with_tokens("exhaust-budget", /*total_tokens*/ 30),
])];
if owner == AgentRuntimeOwnership::Host || !enforce_limit {
sequence.push(sse(vec![
ev_response_created("already-exhausted"),
ev_completed_with_tokens("already-exhausted", /*total_tokens*/ 1),
]));
}
// Enforced: decimal equality is allowed; the next material charge exceeds it.
// Advisory: a real overspend still permits a later request with a zero-balance reminder.
let (limit_budget, reminder_at_remaining_tokens, charges): (f64, Vec<i64>, &[f64]) =
if enforce_limit {
(0.3, vec![], &[0.1, 0.2, 0.000001, 0.0])
} else {
(30.0, vec![20, 10], &[31.0, 1.0])
};
let crossing_turn = if enforce_limit { 2 } else { 0 };
// The later user turn still runs, but an enforced Core budget refuses its model send.
let model_charges = if enforce_limit && owner == AgentRuntimeOwnership::Controller {
&charges[..=crossing_turn]
} else {
charges
};
let sequence = model_charges
.iter()
.enumerate()
.map(|(turn, units)| {
let id = format!("budget-{turn}");
let mut completed = ev_completed_with_tokens(&id, /*total_tokens*/ 1);
completed["response"]["usage"]["codex_rollout_budget_units"] = json!(units);
sse(vec![ev_response_created(&id), completed])
})
.collect();
let responses = mount_sse_sequence(&server, sequence).await;
let mut extensions = ExtensionRegistryBuilder::new();
if owner == AgentRuntimeOwnership::Host {
@@ -446,8 +469,8 @@ async fn exhausted_budget_respects_enforcement_on_current_and_later_turns(
.with_config(move |config| {
config.rollout_budget = Some(RolloutBudgetConfig {
enforce_limit,
limit_tokens: 30,
reminder_at_remaining_tokens: vec![20, 10],
limit_budget,
reminder_at_remaining_tokens,
..rollout_budget()
});
})
@@ -461,10 +484,10 @@ async fn exhausted_budget_respects_enforcement_on_current_and_later_turns(
data.insert(late.clone() as Arc<dyn AgentUsageAccounting>);
}
for prompt in ["exhaust the budget", "try another turn"] {
for turn in 0..charges.len() {
test.codex
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
text: prompt.to_string(),
text: format!("Budget turn {turn}"),
text_elements: Vec::new(),
}]))
.await?;
@@ -473,7 +496,7 @@ async fn exhausted_budget_respects_enforcement_on_current_and_later_turns(
matches!(event, EventMsg::Error(_) | EventMsg::TurnComplete(_))
})
.await;
if owner == AgentRuntimeOwnership::Host || !enforce_limit {
if turn < crossing_turn || owner == AgentRuntimeOwnership::Host || !enforce_limit {
assert!(matches!(event, EventMsg::TurnComplete(done) if done.error.is_none()));
continue;
}
@@ -490,7 +513,7 @@ async fn exhausted_budget_respects_enforcement_on_current_and_later_turns(
if owner == AgentRuntimeOwnership::Host {
let requests = responses.requests();
assert_eq!(requests.len(), 2);
assert_eq!(requests.len(), charges.len());
for request in &requests {
assert!(rollout_budget_texts(request).is_empty());
}
@@ -557,7 +580,7 @@ async fn compaction_accounting_errors_fail_without_retry(
.with_config(move |config| {
config.model_provider.stream_max_retries = Some(2);
config.rollout_budget = Some(RolloutBudgetConfig {
limit_tokens: 10,
limit_budget: 9.0,
reminder_at_remaining_tokens: vec![5],
..rollout_budget()
});
+19
View File
@@ -455,9 +455,20 @@ pub struct RolloutBudgetConfigToml {
pub enforce_limit: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
// TODO: Remove limit_tokens once clients write limit_budget and persisted thread configs have migrated.
/// Positive whole-token allowance. Set either limit_tokens or limit_budget.
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(range(min = 1))]
pub limit_tokens: Option<i64>,
/// Positive, finite allowance in budget units. Set either limit_tokens or limit_budget.
/// Spending exactly this amount is allowed.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(schema_with = "positive_budget_limit_schema")]
pub limit_budget: Option<f64>,
/// Restate the balance when a thread starts or its context is compacted.
/// Defaults to true. Set false with an empty threshold list for no budget reminders.
#[serde(skip_serializing_if = "Option::is_none")]
pub reminder_at_context_start: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Remaining weighted-token values that trigger reminders when crossed.
pub reminder_at_remaining_tokens: Option<Vec<i64>>,
@@ -469,6 +480,14 @@ pub struct RolloutBudgetConfigToml {
pub prefill_token_weight: Option<f64>,
}
fn positive_budget_limit_schema(
generator: &mut schemars::r#gen::SchemaGenerator,
) -> schemars::schema::Schema {
let mut schema = f64::json_schema(generator).into_object();
schema.number().exclusive_minimum = Some(0.0);
schema.into()
}
impl FeatureConfig for RolloutBudgetConfigToml {
fn enabled(&self) -> Option<bool> {
self.enabled
+53
View File
@@ -0,0 +1,53 @@
//! Numerical accounting for nonnegative rollout-budget units.
/// Accumulate validated, nonnegative `f64` charges without dropping every small
/// contribution after a large one. An unrepresentable total stays infinite.
#[derive(Debug, Default)]
pub struct BudgetUnitsSum {
total: f64,
remainder: f64,
}
impl BudgetUnitsSum {
pub fn add(&mut self, units: f64) {
debug_assert!(units >= 0.0);
if units == 0.0 || self.total.is_infinite() {
return;
}
if units.is_infinite() {
self.total = f64::INFINITY;
self.remainder = 0.0;
return;
}
// Near f64::MAX the first addition can overflow before a negative
// remainder brings it back into range. Halving both inputs avoids that.
let scale = if (self.total + units).is_finite() {
1.0
} else {
0.5
};
let previous = self.total * scale;
let units = units * scale;
let sum = previous + units;
// Recover the small part lost to rounding before adding the prior remainder.
let lost = if previous >= units {
(previous - sum) + units
} else {
(units - sum) + previous
};
let remainder = self.remainder * scale + lost;
// A nonnegative bill cannot lower posted spending. Keep any unused
// correction for later receipts rather than reopening the budget.
let next = (sum + remainder).max(previous);
self.total = next / scale;
self.remainder = if self.total.is_finite() {
((sum - next) + remainder) / scale
} else {
0.0
};
}
pub fn total(&self) -> f64 {
self.total
}
}
+2
View File
@@ -1,6 +1,7 @@
pub mod account;
mod agent_path;
pub mod auth;
mod budget_units;
mod context_window_id;
mod item_id_generator;
mod request_country;
@@ -13,6 +14,7 @@ mod session_id;
mod thread_id;
mod tool_name;
pub use agent_path::AgentPath;
pub use budget_units::BudgetUnitsSum;
pub use context_window_id::ContextWindowId;
pub use item_id_generator::ItemIdGenerator;
#[cfg(test)]