Add usage accounting for host-owned agent runtimes (#58975)

## Why

Host-owned runtimes skip controller rollout-budget accounting. Give hosts an optional callback to account for inference spending and signal budget exhaustion.

## What changed

- Add `AgentUsageAccounting`, `AgentUsageReport`, and `UsageAccountingDecision`, captured at startup and supported by host child spawn reservations.
- Await per-response usage reports with thread and turn attribution, including provider budget units and foreground and background compaction usage.
- Map host exhaustion to `SessionBudgetExceeded` and accounting failures to fatal errors without retrying inference. Preserve the original error for incomplete responses.
- Keep controller accounting unchanged and leave accounting disabled for host-owned runtimes without a provider.

## Testing

Add coverage for usage attribution, child accounting, budget exhaustion, nonretryable accounting failures, incomplete responses, and compaction reporting, including background usage charged once to its originating turn.

GitOrigin-RevId: 887703f50fe882497e4e8cb33259072935ec82e0
This commit is contained in:
rhan-oai
2026-11-02 20:37:55 +00:00
committed by copyberry
parent 636cdcb7ec
commit c725e472e5
18 changed files with 611 additions and 55 deletions
+1
View File
@@ -358,6 +358,7 @@ async fn run_compact_task_inner_impl(
e.details(),
CodexErrorDetails::SessionBudgetExceeded
| CodexErrorDetails::ResponseIncomplete(_)
| CodexErrorDetails::Fatal(_)
) =>
{
return Err(e);