Add trusted request-country metadata to Responses requests (#61535)

## What changed

- Add `RequestCountry` for host-supplied country codes. Normalize two-letter ASCII codes to uppercase, omit the field by default, and serialize explicit unknown (`XX`) as `null` to prevent fallback to request provenance.
- Carry `request_country` through thread defaults and captured step settings into Codex backend HTTP and WebSocket requests. Allow live country updates without enabling `step_model_switching`, and preserve WebSocket continuation reuse when the country changes.
- Propagate the captured country to subagents, reviews, and compaction requests.

## Testing

Add coverage for country validation and serialization, thread and live-turn update lifetimes, prewarm settings capture, agent inheritance and reloads, compaction and reviews, WebSocket country changes, and HTTP fallback.

GitOrigin-RevId: 787cd1fa7ba5d465a4eb64a92f13c79e93d9bca7
This commit is contained in:
goodman-oai
2026-11-14 15:44:38 +00:00
committed by copyberry
parent b1573f1024
commit 65a5410f72
53 changed files with 854 additions and 64 deletions
+3
View File
@@ -3,6 +3,9 @@ mod agent_path;
pub mod auth;
mod context_window_id;
mod item_id_generator;
mod request_country;
pub use request_country::InvalidRequestCountry;
pub use request_country::RequestCountry;
mod response_item_id;
mod response_usage;
mod sanitized_git_url;
+4
View File
@@ -560,6 +560,8 @@ pub struct ConversationSpeechParams {
/// task kind. Child sessions and consumers of frozen initial settings are unchanged.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TurnSettingsUpdate {
/// Trusted host country. `None` preserves the current setting. Published for the named live turn's subsequent steps.
pub request_country: Option<crate::RequestCountry>,
/// Changes the reviewer for subsequent approval requests, not pending reviews.
pub approvals_reviewer: Option<ApprovalsReviewer>,
/// Replaces the selection for subsequent steps, without changing future turns.
@@ -601,6 +603,8 @@ pub enum TurnSettingsUpdateOutcome {
}),
)]
pub struct ThreadSettingsOverrides {
/// Trusted host country. `None` preserves the current setting. Inherited by future turns on acceptance.
pub request_country: Option<crate::RequestCountry>,
/// Updated fallback `cwd` and environments supplied together as a complete pair.
pub environments: Option<TurnEnvironmentRequests>,
+59
View File
@@ -0,0 +1,59 @@
//! Trusted runtime country for the current Responses request.
use serde::Serialize;
use serde::Serializer;
/// Country selected by a trusted host. The default leaves provenance to the server.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RequestCountry(Country);
#[derive(Clone, Debug, Default, PartialEq, Eq)]
enum Country {
#[default]
Omitted,
Unknown,
Known(String),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[error("request country must contain exactly two ASCII letters")]
pub struct InvalidRequestCountry;
impl RequestCountry {
/// Explicitly unknown country. The server must not fall back to request provenance.
pub const UNKNOWN: Self = Self(Country::Unknown);
/// Normalizes a two-letter country code. The special code `XX` is explicit unknown.
pub fn from_iso_code(code: &str) -> Result<Self, InvalidRequestCountry> {
let code = code.trim();
if code.len() != 2 || !code.bytes().all(|byte| byte.is_ascii_alphabetic()) {
return Err(InvalidRequestCountry);
}
let code = code.to_ascii_uppercase();
if code == "XX" {
return Ok(Self::UNKNOWN);
}
Ok(Self(Country::Known(code)))
}
pub fn known_country(&self) -> Option<&str> {
match &self.0 {
Country::Known(code) => Some(code),
Country::Omitted | Country::Unknown => None,
}
}
pub fn is_omitted(&self) -> bool {
matches!(self.0, Country::Omitted)
}
}
impl Serialize for RequestCountry {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.known_country().serialize(serializer)
}
}
#[cfg(test)]
#[path = "request_country_tests.rs"]
mod tests;
@@ -0,0 +1,13 @@
#![cfg(test)]
use super::*;
#[test]
fn request_country_rejects_malformed_values() {
for malformed in ["", "D", "USA", "D1", "T1", "DÉ", "D E", "DE\nX"] {
assert_eq!(
RequestCountry::from_iso_code(malformed),
Err(InvalidRequestCountry),
);
}
}