mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
Handle rollout budget decisions in each accounting path (#62621)
Pass `SamplingUsage` into numeric usage accounting and let the host and controller paths apply their budget decisions directly. Host-wide exhaustion stops new work, while turn-only exhaustion returns `SessionBudgetExceeded`. The controller still stops new work on budget exhaustion when there is no native rollout budget. GitOrigin-RevId: 544b9a49a23e2becc68d9cecae347e4d00bbdea0
This commit is contained in:
@@ -1521,21 +1521,23 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test_case::test_case(false, false, false; "foreground bills a complete response even without a summary")]
|
||||
#[test_case::test_case(true, false, false; "background bills a complete response even without a summary")]
|
||||
#[test_case::test_case(false, true, false; "turn interrupt discards a queued complete response")]
|
||||
#[test_case::test_case(true, true, false; "session shutdown bills a queued complete response even without a summary")]
|
||||
#[test_case::test_case(false, false, true; "a malformed sibling is still reported when valid spending exhausts the account")]
|
||||
#[test_case::test_case(false, false, false, None; "foreground bills a complete response even without a summary")]
|
||||
#[test_case::test_case(true, false, false, None; "background bills a complete response even without a summary")]
|
||||
#[test_case::test_case(false, true, false, None; "turn interrupt discards a queued complete response")]
|
||||
#[test_case::test_case(true, true, false, None; "session shutdown bills a queued complete response even without a summary")]
|
||||
#[test_case::test_case(false, false, true, None; "a malformed sibling is still reported when valid spending exhausts the account")]
|
||||
#[tokio::test]
|
||||
async fn collect_queued_complete_response(
|
||||
background: bool,
|
||||
interrupted: bool,
|
||||
bad_sibling: bool,
|
||||
host_total: Option<f64>,
|
||||
) {
|
||||
let (mut sess, turn, _) = crate::session::tests::make_session_and_context_with_rx().await;
|
||||
let (mut sess, turn, _events) =
|
||||
crate::session::tests::make_session_and_context_with_rx().await;
|
||||
let accounting = Arc::new(UsageRecorder {
|
||||
reports: Mutex::new(Vec::new()),
|
||||
decision: if bad_sibling {
|
||||
decision: if bad_sibling || host_total.is_some() {
|
||||
UsageAccountingDecision::Exhausted
|
||||
} else {
|
||||
UsageAccountingDecision::Continue
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::context::ContextualUserFragment;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::CodexErrorDetails;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
|
||||
pub(super) async fn maybe_record_reminder(
|
||||
sess: &Session,
|
||||
@@ -120,14 +119,7 @@ impl Session {
|
||||
usage: &SamplingUsage,
|
||||
) -> CodexResult<()> {
|
||||
let result = match usage.charge.posting {
|
||||
ChargePosting::Unposted => {
|
||||
self.record_numeric_usage(
|
||||
turn_id,
|
||||
usage.provider.as_ref(),
|
||||
usage.charge.hooks.charge,
|
||||
)
|
||||
.await
|
||||
}
|
||||
ChargePosting::Unposted => self.record_numeric_usage(turn_id, usage).await,
|
||||
ChargePosting::Posted => Ok(()),
|
||||
ChargePosting::PostedTurnExhausted => Err(CodexErr::SessionBudgetExceeded),
|
||||
};
|
||||
@@ -145,13 +137,9 @@ impl Session {
|
||||
result
|
||||
}
|
||||
|
||||
async fn record_numeric_usage(
|
||||
&self,
|
||||
turn_id: &str,
|
||||
usage: Option<&TokenUsage>,
|
||||
hook_charge: codex_hooks::HookCharge,
|
||||
) -> CodexResult<()> {
|
||||
let hook_units = match hook_charge {
|
||||
async fn record_numeric_usage(&self, turn_id: &str, report: &SamplingUsage) -> CodexResult<()> {
|
||||
let usage = report.provider.as_ref();
|
||||
let hook_units = match report.charge.hooks.charge {
|
||||
codex_hooks::HookCharge::Units(units) => units,
|
||||
codex_hooks::HookCharge::Overflow => {
|
||||
if let Some(account) = self.native_rollout_budget()
|
||||
@@ -167,52 +155,57 @@ impl Session {
|
||||
if usage.is_none() && hook_units == 0.0 {
|
||||
return Ok(());
|
||||
}
|
||||
let stop_new_work = match &self.agent_runtime {
|
||||
match &self.agent_runtime {
|
||||
AgentRuntimeServices::Controller => match self
|
||||
.services
|
||||
.agent_control
|
||||
.record_usage(usage.cloned(), hook_units)
|
||||
.await
|
||||
{
|
||||
Ok(()) => false,
|
||||
Ok(()) => Ok(()),
|
||||
// A legacy controller can report a posted bill and a stop through this error.
|
||||
// A native account has already chosen its own deadline action.
|
||||
Err(error)
|
||||
if matches!(error.details(), CodexErrorDetails::SessionBudgetExceeded) =>
|
||||
{
|
||||
self.native_rollout_budget().is_none()
|
||||
if self.native_rollout_budget().is_none() {
|
||||
self.work_budget().stop_new_work();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => return Err(CodexErr::UsageAccountingFailed(error.to_string())),
|
||||
Err(error) => Err(CodexErr::UsageAccountingFailed(error.to_string())),
|
||||
},
|
||||
AgentRuntimeServices::Host(services) => {
|
||||
let Some(accounting) = &services.usage_accounting else {
|
||||
return Ok(());
|
||||
};
|
||||
let usage_report = AgentUsageReport {
|
||||
thread_id: self.thread_id(),
|
||||
turn_id: turn_id.to_owned(),
|
||||
usage: usage.cloned(),
|
||||
additional_budget_units: hook_units,
|
||||
};
|
||||
let host_error = |error| {
|
||||
CodexErr::UsageAccountingFailed(format!(
|
||||
"Host usage accounting failed: {error}"
|
||||
))
|
||||
};
|
||||
let decision = accounting
|
||||
.report_usage(AgentUsageReport {
|
||||
thread_id: self.thread_id(),
|
||||
turn_id: turn_id.to_owned(),
|
||||
usage: usage.cloned(),
|
||||
additional_budget_units: hook_units,
|
||||
})
|
||||
.report_usage(usage_report)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
CodexErr::UsageAccountingFailed(format!(
|
||||
"Host usage accounting failed: {error}"
|
||||
))
|
||||
})?;
|
||||
.map_err(host_error)?;
|
||||
if decision == UsageAccountingDecision::Exhausted {
|
||||
self.work_budget().stop_new_work();
|
||||
}
|
||||
match decision {
|
||||
UsageAccountingDecision::Continue => false,
|
||||
UsageAccountingDecision::Exhausted => true,
|
||||
UsageAccountingDecision::Continue | UsageAccountingDecision::Exhausted => {
|
||||
Ok(())
|
||||
}
|
||||
UsageAccountingDecision::ExhaustedForTurn => {
|
||||
return Err(CodexErr::SessionBudgetExceeded);
|
||||
Err(CodexErr::SessionBudgetExceeded)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
if stop_new_work {
|
||||
self.work_budget().stop_new_work();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user