Files
codex/codex-rs/core/src/model_request_tests.rs
T
tongzhou wang 92c1a4dc2e Add response processing after completion and usage accounting (#61577)
## Why

Streaming can dispatch tools before a response finishes and its usage is accounted for. Allow callers to wait for completion and synchronous post-hooks so budget enforcement can block tools from the response that exhausts the budget.

## What changed

- Add `responses.process_mode = "after_complete_response"` to buffer response items and release them in order after completion, synchronous post-hooks, and usage accounting. Keep `streaming` as the default and deliver safety-buffering notifications immediately.
- Track whether response usage has already been charged to avoid duplicate accounting, including during compaction.
- Stop new tools across the agent tree when the rollout budget is exhausted. Coordinate steering with accounting and prevent buffered tool calls from dispatching after steering or cancellation.
- Bound buffered responses to 65,535 events and reject overflow.

## Testing

Add configuration coverage and extend integration tests for buffered output ordering, synchronous hooks, interrupted responses and resume, steering, invalid usage, and budget exhaustion preventing tool dispatch.

GitOrigin-RevId: bd06811cfd50cb701babe6353465c57dfed2a37a
2026-11-14 23:50:12 +00:00

108 lines
3.5 KiB
Rust

//! Interceptors can transform incremental events and retain upstream cancellation.
use super::*;
use crate::client_common::SamplingStream;
use codex_extension_api::ResponseEvent;
use futures::StreamExt;
use pretty_assertions::assert_eq;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
struct Append(&'static str);
impl ModelResponseInterceptor for Append {
fn intercept(self: Box<Self>, stream: ModelResponseStream) -> ModelResponseStream {
Box::pin(stream.map(move |event| {
event.map(|event| match event {
ResponseEvent::OutputTextDelta(text) => {
ResponseEvent::OutputTextDelta(format!("{text}{}", self.0))
}
other => other,
})
}))
}
}
#[tokio::test]
async fn interceptors_forward_before_completion_in_order_and_cancel_upstream() {
let (tx_event, rx_event) = mpsc::channel(1);
let consumer_dropped = CancellationToken::new();
let upstream_cancelled = consumer_dropped.clone();
let mut stream = intercept_stream(
Box::pin(
SamplingStream {
routing_affinity: None,
rx_event,
interrupt: None,
consumer_dropped,
settlement_started: Default::default(),
}
.map(|event| {
event.event.map_err(|error| {
codex_extension_api::ModelResponseError::Stream(error.to_string())
})
}),
),
vec![Box::new(Append("a")), Box::new(Append("b"))],
);
tx_event
.send(Ok(ResponseEvent::OutputTextDelta("text".into())).into())
.await
.unwrap();
// Upstream remains open and has not sent Completed.
let event = tokio::time::timeout(Duration::from_secs(1), stream.next())
.await
.unwrap()
.unwrap()
.unwrap();
let ResponseEvent::OutputTextDelta(text) = event else {
panic!("expected text delta");
};
assert_eq!(text, "textab");
drop(stream);
tokio::time::timeout(Duration::from_secs(1), upstream_cancelled.cancelled())
.await
.unwrap();
}
#[derive(Debug)]
struct MetadataContributor;
impl ModelRequestContributor for MetadataContributor {
fn request(&self, input: ModelRequestInput<'_>) -> Option<Box<dyn ModelResponseInterceptor>> {
*input.client_metadata = Some(HashMap::from([
("thread_id".into(), "replaced".into()),
("guardian_credits_requested".into(), "true".into()),
("parent_response_id".into(), "forged".into()),
("existing".into(), "replaced".into()),
("x-request-id".into(), "request".into()),
]));
None
}
}
#[test]
fn contributor_metadata_preserves_core_fields_and_existing_values() {
let mut metadata = Some(HashMap::from([
("thread_id".into(), "thread".into()),
("existing".into(), "original".into()),
]));
let contributors: Vec<Arc<dyn ModelRequestContributor>> = vec![Arc::new(MetadataContributor)];
assert!(
prepare(
&contributors,
"thread",
"model",
ModelRequestKind::Generation,
&mut metadata
)
.is_empty()
);
assert_eq!(
metadata,
Some(HashMap::from([
("thread_id".into(), "thread".into()),
("existing".into(), "original".into()),
("x-request-id".into(), "request".into()),
]))
);
}