Files
codex/codex-rs/core/src/model_request_tests.rs
T
jif 1396b2e0e7 Correlate Guardian failure reports with review and response IDs (#65276)
## Why

A turn can contain multiple Guardian reviews, and denial reconsideration
can produce multiple responses for one review. Thread and turn IDs alone
cannot identify which review and provider response produced the recorded
decision.

## What changed

Add `review_id`, `reviewer_request_id`, and `reviewer_response_id` to opt-in
Guardian failure reports when available. Keep the provider IDs with the
selected decision, including the original denial when reconsideration
fails and falls back to it.

## Testing

Extend tests to check the recorded IDs for initial and
reconsidered denials and verify that an invalid retry preserves the
original denial's IDs.

GitOrigin-RevId: 4bd906bfb9a80808cb3c2a8ac71c5354d4caad1f
2026-12-02 15:17:02 +00:00

109 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 {
upstream_request_id: None,
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()),
]))
);
}