Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Add a bounded Decisions transport for Guardian comparison classification (#50066)

swastik-oaid91294c39edb
diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/decisions.rs b/codex-rs/ext/guardian-v2/src/async_scorer/decisions.rs
new file mode 100644
index 0000000000..14f1516426
--- /dev/null
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/decisions.rs
@@ -0,0 +1,263 @@
+//! Bounded Decisions transport for the optional Guardian comparison classifier.
+//! Admission retains the newest requests and cancels the oldest unfinished request at capacity.
+//! Unsupported evidence is rejected intact; errors never contain credentials or wire bodies.
+//! The caller records measurements after baseline publication; dropping its task aborts Decisions work.
+
+use super::sampler::LunaSamplingRequest;
+use codex_context_fragments::RenderedFragment;
+use codex_http_client::HttpClient;
+use codex_protocol::models::ContentItem;
+use codex_protocol::models::ImageReference;
+use codex_protocol::models::ResponseItem;
+use serde_json::Value;
+use serde_json::json;
+use std::collections::VecDeque;
+use std::sync::Arc;
+use std::sync::Mutex;
+use std::sync::Weak;
+use std::time::Duration;
+use std::time::Instant;
+use thiserror::Error;
+use tokio::sync::Semaphore;
+use tokio::task::AbortHandle;
+use tokio::task::JoinHandle;
+
+const MAX_CONCURRENT_REQUESTS: usize = 16;
+const MODEL: &str = "gpt-6-luna";
+// Provisional local rollout budgets, not Decisions API limits or model context limits.
+// Guardian owns the text/context budget; bound HTTP lifetime, image bytes and response buffering here.
+// Oversized images skip the complete request; transport failures never affect approvals.
+const DEADLINE: Duration = Duration::from_secs(6);
+const MAX_IMAGE_BYTES: usize = 8 * 1024 * 1024;
+const MAX_RESPONSE_BYTES: usize = 16 * 1024;
+
+#[derive(Clone, Copy, Debug, Error, PartialEq)]
+pub(super) enum DecisionsError {
+ #[error("Decisions credentials are missing or empty")]
+ Credentials,
+ #[error("Decisions cannot represent the complete classifier evidence")]
+ UnsupportedEvidence,
+ #[error("Decisions input exceeds the request limit")]
+ InputTooLarge,
+ #[error("Decisions request timed out")]
+ Timeout,
+ #[error("Decisions transport failed")]
+ Transport,
+ #[error("Decisions returned HTTP {0}")]
+ Http(u16),
+ #[error("Decisions response exceeds the response limit")]
+ ResponseTooLarge,
+ #[error("Decisions returned an invalid classification")]
+ InvalidResponse,
+}
+
+pub(super) struct DecisionsSampler {
+ client: HttpClient,
+ // No Debug implementation: credentials must not reach diagnostics.
+ api_key: String,
+ url: String,
+ slots: Semaphore,
+ active_requests: Mutex<VecDeque<AbortHandle>>,
+}
+
+/// Owns the measurement until it is recorded after baseline publication.
+/// Dropping the classification, including during the join, aborts unfinished work.
+pub(super) struct DecisionsTask {
+ handle: JoinHandle<(Result<&'static str, DecisionsError>, Duration)>,
+ sampler: Weak<DecisionsSampler>,
+}
+
+impl Drop for DecisionsTask {
+ fn drop(&mut self) {
+ self.handle.abort();
+ if let Some(sampler) = self.sampler.upgrade() {
+ sampler
+ .active_requests
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .retain(|task| task.id() != self.handle.id());
+ }
+ }
+}
+
+impl DecisionsTask {
+ pub(super) async fn finish(
+ mut self,
+ ) -> Result<(Result<&'static str, DecisionsError>, Duration), tokio::task::JoinError> {
+ (&mut self.handle).await
+ }
+}
+
+impl DecisionsSampler {
+ pub(super) fn new(
+ client: HttpClient,
+ api_key: String,
+ url: String,
+ ) -> Result<Self, DecisionsError> {
+ if api_key.trim().is_empty() {
+ return Err(DecisionsError::Credentials);
+ }
+ Ok(Self {
+ client,
+ api_key,
+ url,
+ // Keep HTTP concurrency bounded while evicted tasks release their permits.
+ slots: Semaphore::new(MAX_CONCURRENT_REQUESTS),
+ active_requests: Mutex::new(VecDeque::new()),
+ })
+ }
+
+ /// Retain the newest requests; the caller joins after baseline publication.
+ pub(super) fn start(self: &Arc<Self>, request: &LunaSamplingRequest) -> DecisionsTask {
+ let body = match request_body(
+ &request.instructions,
+ &request.input,
+ request.parent_compaction.as_ref(),
+ ) {
+ Ok(body) => body,
+ Err(error) => {
+ return DecisionsTask {
+ handle: tokio::spawn(async move { (Err(error), Duration::ZERO) }),
+ sampler: Weak::new(),
+ };
+ }
+ };
+ let mut active = self
+ .active_requests
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ active.retain(|task| !task.is_finished());
+ if active.len() == MAX_CONCURRENT_REQUESTS
+ && let Some(oldest) = active.pop_front()
+ {
+ oldest.abort();
+ }
+ let sampler = Arc::clone(self);
+ let handle = tokio::spawn(async move {
+ let Ok(_permit) = sampler.slots.acquire().await else {
+ return (Err(DecisionsError::Transport), Duration::ZERO);
+ };
+ let started = Instant::now();
+ let result = tokio::time::timeout(DEADLINE, sampler.request(body))
+ .await
+ .unwrap_or(Err(DecisionsError::Timeout));
+ (result, started.elapsed())
+ });
+ active.push_back(handle.abort_handle());
+ DecisionsTask {
+ handle,
+ sampler: Arc::downgrade(self),
+ }
+ }
+
+ async fn request(&self, body: Value) -> Result<&'static str, DecisionsError> {
+ let request = self
+ .client
+ .post(&self.url)
+ .bearer_auth(&self.api_key)
+ .json(&body);
+ drop(body);
+ let mut response = request
+ .send()
+ .await
+ .map_err(|_| DecisionsError::Transport)?;
+ if !response.status().is_success() {
+ return Err(DecisionsError::Http(response.status().as_u16()));
+ }
+ let mut bytes = Vec::new();
+ while let Some(chunk) = response
+ .chunk()
+ .await
+ .map_err(|_| DecisionsError::Transport)?
+ {
+ if bytes.len().saturating_add(chunk.len()) > MAX_RESPONSE_BYTES {
+ return Err(DecisionsError::ResponseTooLarge);
+ }
+ bytes.extend_from_slice(&chunk);
+ }
+ parse_answer(&serde_json::from_slice(&bytes).map_err(|_| DecisionsError::InvalidResponse)?)
+ }
+}
+
+fn request_body(
+ instructions: &RenderedFragment,
+ evidence: &[ResponseItem],
+ parent_compaction: Option<&ResponseItem>,
+) -> Result<Value, DecisionsError> {
+ if parent_compaction.is_some() {
+ return Err(DecisionsError::UnsupportedEvidence);
+ }
+ let ContentItem::InputText { text: rubric } = instructions.annotated_content().content() else {
+ return Err(DecisionsError::UnsupportedEvidence);
+ };
+ let mut image_bytes = 0usize;
+ let mut messages = Vec::new();
+ for item in evidence {
+ let ResponseItem::Message { role, content, .. } = item else {
+ return Err(DecisionsError::UnsupportedEvidence);
+ };
+ if role != "user" {
+ return Err(DecisionsError::UnsupportedEvidence);
+ }
+ let mut parts = Vec::new();
+ for part in content {
+ match part {
+ ContentItem::InputText { text } => {
+ parts.push(json!({"type": "input_text", "text": text}));
+ }
+ ContentItem::InputImage {
+ image: ImageReference::Inline { image_url },
+ ..
+ } if image_url.starts_with("data:") => {
+ image_bytes = image_bytes.saturating_add(image_url.len());
+ if image_bytes > MAX_IMAGE_BYTES {
+ return Err(DecisionsError::InputTooLarge);
+ }
+ parts.push(
+ // Luna also clears image detail before sending evidence.
+ json!({"type": "input_image", "image_url": image_url, "detail": null}),
+ );
+ }
+ ContentItem::InputImage { .. }
+ | ContentItem::InputAudio { .. }
+ | ContentItem::OutputText { .. } => {
+ return Err(DecisionsError::UnsupportedEvidence);
+ }
+ }
+ }
+ // Harness annotations/IDs are not part of the Decisions contract. Preserve every
+ // model-visible content part and message boundary; never demote trusted developer text.
+ let mut message = json!({"role": "user"});
+ message["content"] = Value::Array(parts);
+ messages.push(message);
+ }
+ let mut body = json!({
+ "model": MODEL,
+ "questions": [{"type": "choice", "name": "guardian_risk", "instructions": rubric,
+ "choices": [{"value": "low"}, {"value": "high"}]}]
+ });
+ body["input"] = Value::Array(messages);
+ Ok(body)
+}
+
+fn parse_answer(body: &Value) -> Result<&'static str, DecisionsError> {
+ let invalid = DecisionsError::InvalidResponse;
+ let answers = body["answers"].as_array().ok_or(invalid)?;
+ if answers.len() != 1 {
+ return Err(invalid);
+ }
+ let answer = &answers[0];
+ if answer["type"] != "choice" || answer["name"] != "guardian_risk" {
+ return Err(invalid);
+ }
+ let choice = match answer["choice"].as_str() {
+ Some("low") => "low",
+ Some("high") => "high",
+ _ => return Err(invalid),
+ };
+ Ok(choice)
+}
+
+#[cfg(test)]
+#[path = "decisions_tests.rs"]
+mod tests;
diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/decisions_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/decisions_tests.rs
new file mode 100644
index 0000000000..b5d32b3225
--- /dev/null
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/decisions_tests.rs
@@ -0,0 +1,216 @@
+use super::*;
+use codex_context_fragments::AnnotatedContent;
+use codex_http_client::HttpClientBuilder;
+use codex_protocol::models::ContentItemKind;
+use pretty_assertions::assert_eq;
+use wiremock::Mock;
+use wiremock::MockServer;
+use wiremock::ResponseTemplate;
+use wiremock::matchers::body_json;
+use wiremock::matchers::header;
+use wiremock::matchers::method;
+use wiremock::matchers::path;
+
+#[test]
+fn adapter_rejects_unsupported_evidence() {
+ let rubric = RenderedFragment::new(
+ "developer",
+ AnnotatedContent::input_text("Classify risk.", ContentItemKind("guardian.test".into())),
+ );
+ let user: ResponseItem = serde_json::from_value(json!({"type":"message", "role":"user", "content":[
+ {"type":"input_text", "text":"Authorized task"}, {"type":"input_text", "text":"Planned action"}
+ ]})).unwrap();
+ let mut trusted = user.clone();
+ if let ResponseItem::Message { role, .. } = &mut trusted {
+ *role = "developer".into();
+ }
+ assert_eq!(
+ request_body(
+ &rubric,
+ &[user.clone(), trusted],
+ /*parent_compaction*/ None
+ ),
+ Err(DecisionsError::UnsupportedEvidence)
+ );
+ assert_eq!(
+ request_body(&rubric, std::slice::from_ref(&user), Some(&user)),
+ Err(DecisionsError::UnsupportedEvidence)
+ );
+ let image: ResponseItem =
+ serde_json::from_value(json!({"type":"message", "role":"user", "content":[
+ {"type":"input_image", "image_url":"https://example.com/image.png"}
+ ]}))
+ .unwrap();
+ assert_eq!(
+ request_body(&rubric, &[image], /*parent_compaction*/ None),
+ Err(DecisionsError::UnsupportedEvidence)
+ );
+}
+
+#[tokio::test]
+async fn http_contract_and_untrusted_response_validation() {
+ core_test_support::skip_if_no_network!();
+ let server = MockServer::start().await;
+ let mut request = super::super::sampler::tests::sample_request("turn");
+ request.instructions = RenderedFragment::new(
+ "developer",
+ AnnotatedContent::input_text("Classify risk", ContentItemKind("guardian.test".into())),
+ );
+ let body = json!({"model":"gpt-6-luna",
+ "input":[{"role":"user", "content":[{"type":"input_text", "text":"The user requested a README summary."}]}],
+ "questions":[{"type":"choice",
+ "name":"guardian_risk", "instructions":"Classify risk", "choices":[{"value":"low"},{"value":"high"}]}]});
+ let response = json!({"answers":[{"type":"choice","name":"guardian_risk", "choice":"low"}]});
+ Mock::given(method("POST"))
+ .and(path("/v1/decisions"))
+ .and(header("authorization", "Bearer synthetic-key"))
+ .and(body_json(body.clone()))
+ .respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(response.clone()))
+ .expect(/*r*/ 1)
+ .mount(&server)
+ .await;
+ let sampler = DecisionsSampler::new(
+ HttpClientBuilder::new().build_direct().unwrap(),
+ "synthetic-key".into(),
+ format!("{}/v1/decisions", server.uri()),
+ )
+ .unwrap();
+ let mut sampler = Arc::new(sampler);
+ assert_eq!(sampler.start(&request).finish().await.unwrap().0, Ok("low"));
+ let mut invalid = response;
+ invalid["answers"][0]["choice"] = json!("untrusted server text");
+ assert_eq!(parse_answer(&invalid), Err(DecisionsError::InvalidResponse));
+ Mock::given(path("/denied"))
+ .respond_with(ResponseTemplate::new(/*s*/ 403).set_body_string("private error details"))
+ .mount(&server)
+ .await;
+ Arc::get_mut(&mut sampler).unwrap().url = format!("{}/denied", server.uri());
+ assert_eq!(sampler.request(body).await, Err(DecisionsError::Http(403)));
+}
+
+#[tokio::test]
+async fn decisions_admits_newest_request_by_cancelling_oldest() {
+ core_test_support::skip_if_no_network!();
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .respond_with(ResponseTemplate::new(/*s*/ 403))
+ .expect(MAX_CONCURRENT_REQUESTS as u64)
+ .mount(&server)
+ .await;
+ let sampler = Arc::new(
+ DecisionsSampler::new(
+ HttpClientBuilder::new().build_direct().unwrap(),
+ "synthetic-key".into(),
+ server.uri(),
+ )
+ .unwrap(),
+ );
+ let request = super::super::sampler::tests::sample_request("turn");
+ // Hold transport permits so all admitted tasks remain pending until eviction is checked.
+ let permits = sampler
+ .slots
+ .acquire_many(MAX_CONCURRENT_REQUESTS as u32)
+ .await
+ .unwrap();
+ let oldest = sampler.start(&request);
+ let mut remaining = (1..MAX_CONCURRENT_REQUESTS)
+ .map(|_| sampler.start(&request))
+ .collect::<Vec<_>>();
+ remaining.push(sampler.start(&request));
+ assert!(oldest.finish().await.unwrap_err().is_cancelled());
+ assert!(server.received_requests().await.unwrap().is_empty());
+ drop(permits);
+ for task in remaining {
+ assert_eq!(
+ task.finish().await.unwrap().0,
+ Err(DecisionsError::Http(403))
+ );
+ }
+}
+
+#[test]
+fn adapter_preserves_inline_images_and_bounds_image_bytes() {
+ let mut request = super::super::sampler::tests::sample_request("turn");
+ let image: ResponseItem =
+ serde_json::from_value(json!({"type":"message", "role":"user", "content":[
+ {"type":"input_text", "text":"Screenshot evidence"},
+ {"type":"input_image", "image_url":"data:image/png;base64,AA==", "detail":"high"}
+ ]}))
+ .unwrap();
+ request.input = vec![image];
+ let body = request_body(
+ &request.instructions,
+ &request.input,
+ /*parent_compaction*/ None,
+ )
+ .unwrap();
+ assert_eq!(
+ body["input"],
+ json!([{"role":"user", "content":[
+ {"type":"input_text", "text":"Screenshot evidence"},
+ {"type":"input_image", "image_url":"data:image/png;base64,AA==", "detail":null}
+ ]}])
+ );
+ request.input = vec![
+ serde_json::from_value(json!({"type":"message", "role":"user", "content":[
+ {"type":"input_image", "image_url":format!("data:image/png;base64,{}", "A".repeat(MAX_IMAGE_BYTES))}
+ ]}))
+ .unwrap(),
+ ];
+ assert_eq!(
+ request_body(
+ &request.instructions,
+ &request.input,
+ /*parent_compaction*/ None
+ ),
+ Err(DecisionsError::InputTooLarge)
+ );
+}
+
+#[tokio::test]
+async fn cancelling_a_decisions_request_releases_thread_capacity() {
+ core_test_support::skip_if_no_network!();
+ use std::sync::Arc;
+ use std::time::Duration;
+ let server = MockServer::start().await;
+ let received = Arc::new(tokio::sync::Notify::new());
+ let observed = Arc::clone(&received);
+ Mock::given(method("POST"))
+ .respond_with(move |_: &wiremock::Request| {
+ observed.notify_one();
+ ResponseTemplate::new(/*s*/ 200).set_delay(Duration::from_secs(60))
+ })
+ .expect(/*r*/ 1)
+ .mount(&server)
+ .await;
+ let sampler = Arc::new(
+ DecisionsSampler::new(
+ HttpClientBuilder::new().build_direct().unwrap(),
+ "synthetic-key".into(),
+ server.uri(),
+ )
+ .unwrap(),
+ );
+ let task = sampler.start(&super::super::sampler::tests::sample_request("turn"));
+ tokio::time::timeout(Duration::from_secs(5), received.notified())
+ .await
+ .unwrap();
+ {
+ let recording = task.finish();
+ tokio::pin!(recording);
+ std::future::poll_fn(|cx| {
+ assert!(std::future::Future::poll(recording.as_mut(), cx).is_pending());
+ std::task::Poll::Ready(())
+ })
+ .await;
+ // Dropping the caller while it awaits the join must abort the HTTP task.
+ }
+ let permit = tokio::time::timeout(
+ Duration::from_secs(5),
+ sampler.slots.acquire_many(MAX_CONCURRENT_REQUESTS as u32),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ drop(permit);
+}
diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs b/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs
index d5207f7c10..e71c6cc57b 100644
--- a/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs
@@ -5,6 +5,9 @@ mod classification;
mod config;
mod conversation;
mod coverage;
+// Compile the transport in tests until the next stack stage wires its runtime caller.
+#[cfg(test)]
+mod decisions;
mod extension;
mod metrics;
mod observation;