use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex as StdMutex, OnceLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use eyre::{Result, WrapErr};
use rig_core::ProviderError;
use rig_core::completion::CompletionRequest;
use rig_core::providers::openai::responses_api::wire::Responses;
use rig_core::providers::openai::wire::CallerIdentity;
use rig_core::providers::{chatgpt, openai};
use serde_json::Value;
use sha2::{Digest, Sha256};
use tokio::sync::Mutex;
use crate::llm::{
Completion, CompletionResponse, LLMClient, LLMClientDyn, WithRetryExt,
compact_provider_error_body, provider_http_status,
};
const CLIENT_ID: &str = "app_EMoamEEZ73f0CkXaXp7hrann";
const TOKEN_URL: &str = "https://auth.openai.com/oauth/token";
const ORIGINATOR: &str = "nitpicker";
const EXPIRY_SKEW: Duration = Duration::from_secs(60);
const REFRESH_TIMEOUT: Duration = Duration::from_secs(30);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(30);
const READ_TIMEOUT: Duration = Duration::from_secs(120);
fn build_http_client() -> Result<reqwest::Client> {
reqwest::Client::builder()
.connect_timeout(CONNECT_TIMEOUT)
.read_timeout(READ_TIMEOUT)
.build()
.wrap_err("building Codex HTTP client")
}
fn user_agent() -> String {
format!(
"nitpicker/{} ({}; {})",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
std::env::consts::ARCH
)
}
fn decode_jwt_claims(token: &str) -> Option<Value> {
let payload = token.split('.').nth(1)?;
let bytes = URL_SAFE_NO_PAD.decode(payload).ok()?;
serde_json::from_slice(&bytes).ok()
}
fn account_id_from_claims(claims: &Value) -> Option<String> {
let direct = claims.get("chatgpt_account_id").and_then(Value::as_str);
let nested = claims
.get("https://api.openai.com/auth")
.and_then(|a| a.get("chatgpt_account_id"))
.and_then(Value::as_str);
let org = claims
.get("organizations")
.and_then(Value::as_array)
.and_then(|orgs| orgs.first())
.and_then(|o| o.get("id"))
.and_then(Value::as_str);
direct.or(nested).or(org).map(str::to_string)
}
fn account_id_from_tokens(id_token: Option<&str>, access_token: &str) -> Option<String> {
id_token
.and_then(decode_jwt_claims)
.as_ref()
.and_then(account_id_from_claims)
.or_else(|| {
decode_jwt_claims(access_token)
.as_ref()
.and_then(account_id_from_claims)
})
}
fn expiry_from_access_token(access_token: &str) -> SystemTime {
decode_jwt_claims(access_token)
.and_then(|c| c.get("exp").and_then(Value::as_u64))
.and_then(|exp| UNIX_EPOCH.checked_add(Duration::from_secs(exp)))
.unwrap_or(UNIX_EPOCH)
}
#[derive(Clone)]
struct CodexTokens {
access_token: String,
refresh_token: String,
account_id: Option<String>,
expires_at: SystemTime,
}
impl CodexTokens {
fn is_expired(&self) -> bool {
self.expires_at <= SystemTime::now() + EXPIRY_SKEW
}
}
fn auth_path() -> Result<PathBuf> {
resolve_auth_path(
std::env::var("CODEX_HOME").ok().as_deref(),
dirs::home_dir(),
)
}
fn resolve_auth_path(codex_home: Option<&str>, home_dir: Option<PathBuf>) -> Result<PathBuf> {
if let Some(home) = codex_home {
let trimmed = home.trim();
if !trimmed.is_empty() {
let path = Path::new(trimmed);
if path.is_absolute() {
return Ok(path.join("auth.json"));
}
eyre::bail!("CODEX_HOME must be an absolute path, got \"{home}\"");
}
}
let home =
home_dir.ok_or_else(|| eyre::eyre!("cannot resolve home directory (set CODEX_HOME)"))?;
Ok(home.join(".codex").join("auth.json"))
}
fn load_tokens(path: &Path) -> Result<CodexTokens> {
let raw = std::fs::read_to_string(path).wrap_err_with(|| {
format!(
"cannot read {} — run `codex login` with a ChatGPT subscription first",
path.display()
)
})?;
parse_tokens(&raw, &path.display().to_string())
}
pub fn auth_available() -> bool {
auth_path().and_then(|p| load_tokens(&p)).is_ok()
}
fn parse_tokens(raw: &str, source: &str) -> Result<CodexTokens> {
let v: Value =
serde_json::from_str(raw).wrap_err_with(|| format!("invalid JSON in {source}"))?;
let tokens = v.get("tokens").ok_or_else(|| {
eyre::eyre!(
"no ChatGPT subscription tokens in {source} — run `codex login` (API-key mode is not supported here)"
)
})?;
let access_token = tokens
.get("access_token")
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
.ok_or_else(|| eyre::eyre!("no access_token in {source}"))?
.to_string();
let refresh_token = tokens
.get("refresh_token")
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
.ok_or_else(|| eyre::eyre!("no refresh_token in {source}"))?
.to_string();
let account_id = tokens
.get("account_id")
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
.map(str::to_string)
.or_else(|| {
account_id_from_tokens(
tokens.get("id_token").and_then(Value::as_str),
&access_token,
)
});
let expires_at = expiry_from_access_token(&access_token);
Ok(CodexTokens {
access_token,
refresh_token,
account_id,
expires_at,
})
}
#[derive(serde::Deserialize)]
struct RefreshResponse {
access_token: String,
refresh_token: Option<String>,
id_token: Option<String>,
expires_in: Option<u64>,
}
async fn refresh_tokens(
http: &reqwest::Client,
refresh_token: &str,
prev_account_id: Option<&str>,
) -> Result<CodexTokens> {
let resp = http
.post(TOKEN_URL)
.timeout(REFRESH_TIMEOUT)
.form(&[
("grant_type", "refresh_token"),
("refresh_token", refresh_token),
("client_id", CLIENT_ID),
])
.send()
.await
.wrap_err("Codex token refresh request failed")?;
let status = resp.status();
let body = resp.text().await.unwrap_or_default();
if !status.is_success() {
return Err(eyre::Report::new(ProviderError::from_http_response(
status,
compact_provider_error_body(&body, 1000),
)))
.wrap_err("Codex token refresh failed");
}
let parsed: RefreshResponse =
serde_json::from_str(&body).wrap_err("parsing Codex token refresh response")?;
let expires_at = match parsed.expires_in {
Some(secs) => SystemTime::now()
.checked_add(Duration::from_secs(secs))
.unwrap_or_else(|| expiry_from_access_token(&parsed.access_token)),
None => expiry_from_access_token(&parsed.access_token),
};
let account_id = prev_account_id
.map(str::to_string)
.or_else(|| account_id_from_tokens(parsed.id_token.as_deref(), &parsed.access_token));
Ok(CodexTokens {
access_token: parsed.access_token,
refresh_token: parsed
.refresh_token
.unwrap_or_else(|| refresh_token.to_string()),
account_id,
expires_at,
})
}
pub struct CodexClient {
http: reqwest::Client,
auth_path: PathBuf,
tokens: Mutex<CodexTokens>,
}
impl CodexClient {
pub fn new() -> Result<Self> {
let path = auth_path()?;
let tokens = load_tokens(&path)?;
Ok(Self {
http: build_http_client()?,
auth_path: path,
tokens: Mutex::new(tokens),
})
}
async fn refresh_locked(&self, tokens: &mut CodexTokens) -> Result<()> {
match refresh_tokens(
&self.http,
&tokens.refresh_token,
tokens.account_id.as_deref(),
)
.await
{
Ok(fresh) => {
*tokens = fresh;
Ok(())
}
Err(err) if is_rotation_4xx(&err) => {
let disk = load_tokens(&self.auth_path)
.wrap_err("Codex refresh token rejected and reloading auth.json failed")?;
let fresh = refresh_tokens(
&self.http,
&disk.refresh_token,
disk.account_id.as_deref(),
)
.await
.wrap_err(
"Codex refresh failed even after reloading auth.json (run `codex login`)",
)?;
*tokens = fresh;
Ok(())
}
Err(err) => Err(err),
}
}
async fn current_access(&self) -> Result<(String, Option<String>)> {
let mut guard = self.tokens.lock().await;
if guard.is_expired() {
self.refresh_locked(&mut guard).await?;
}
Ok((guard.access_token.clone(), guard.account_id.clone()))
}
async fn force_refresh(&self, used_access: &str) -> Result<(String, Option<String>)> {
let mut guard = self.tokens.lock().await;
if guard.access_token != used_access {
return Ok((guard.access_token.clone(), guard.account_id.clone()));
}
self.refresh_locked(&mut guard).await?;
Ok((guard.access_token.clone(), guard.account_id.clone()))
}
async fn send(
&self,
completion: &Completion,
access: &str,
account_id: Option<&str>,
) -> Result<CompletionResponse> {
let wire = codex_wire(&completion.model, access, account_id);
let model = rig_core::Model::new(wire, rig_reqwest::ReqwestClient::from(self.http.clone()));
let response = model
.call(codex_request(completion)?)
.await
.wrap_err("Codex completion failed")?;
Ok(CompletionResponse::from_rig(
response,
completion.model.clone(),
))
}
}
pub fn shared_client() -> Result<Arc<dyn LLMClientDyn>> {
static CELL: OnceLock<StdMutex<Option<Arc<dyn LLMClientDyn>>>> = OnceLock::new();
let cell = CELL.get_or_init(|| StdMutex::new(None));
let mut guard = cell.lock().expect("codex shared-client mutex poisoned");
if let Some(existing) = guard.as_ref() {
return Ok(Arc::clone(existing));
}
let client = CodexClient::new()?.with_retry().into_arc();
*guard = Some(Arc::clone(&client));
Ok(client)
}
impl LLMClient for CodexClient {
async fn completion(&self, completion: Completion) -> Result<CompletionResponse> {
let (access, account_id) = self.current_access().await?;
match self.send(&completion, &access, account_id.as_deref()).await {
Ok(response) => Ok(response),
Err(err) if provider_http_status(&err) == Some(401) => {
let (access, account_id) = self.force_refresh(&access).await?;
self.send(&completion, &access, account_id.as_deref()).await
}
Err(err) => Err(err),
}
}
}
fn codex_wire(model: &str, access: &str, account_id: Option<&str>) -> Responses {
let mut config = openai::OpenAIConfig::with_key(&chatgpt::DIALECT, access);
config.instructions = None;
config.identity = Some(CallerIdentity {
originator: ORIGINATOR.into(),
user_agent: user_agent(),
});
config.account_id = account_id.map(str::to_owned);
Responses::new(config, model)
}
fn codex_request(completion: &Completion) -> Result<CompletionRequest> {
eyre::ensure!(
completion
.preamble
.as_deref()
.is_some_and(|s| !s.trim().is_empty()),
"Codex auth requires a system prompt: this completion had no `instructions`"
);
let mut request = CompletionRequest::from(completion.clone());
request.max_tokens = None;
request.options.cache_key = Some(codex_prompt_cache_key(completion)?);
Ok(request)
}
fn codex_prompt_cache_key(completion: &Completion) -> Result<String> {
fn hash_part(hasher: &mut Sha256, bytes: &[u8]) {
hasher.update((bytes.len() as u64).to_be_bytes());
hasher.update(bytes);
}
let mut hasher = Sha256::new();
hash_part(&mut hasher, completion.model.as_bytes());
hash_part(
&mut hasher,
completion
.preamble
.as_deref()
.unwrap_or_default()
.as_bytes(),
);
let tools = serde_json::to_vec(&completion.tools)
.wrap_err("serializing Codex tools for prompt cache routing")?;
hash_part(&mut hasher, &tools);
let digest = hasher.finalize();
let suffix = digest[..16]
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<String>();
Ok(format!("nitpicker-{suffix}"))
}
fn is_rotation_4xx(err: &eyre::Report) -> bool {
provider_http_status(err).is_some_and(|status| (400..500).contains(&status) && status != 429)
}
#[cfg(test)]
mod tests {
use super::*;
use rig_core::completion::message::{
AssistantContent, AssistantMessage, CallId, Message, ToolCall, ToolFunction, ToolName,
ToolResultContent, UserContent,
};
use serde_json::json;
fn jwt(claims: Value) -> String {
let header = URL_SAFE_NO_PAD.encode(br#"{"alg":"none"}"#);
let payload = URL_SAFE_NO_PAD.encode(serde_json::to_vec(&claims).unwrap());
format!("{header}.{payload}.sig")
}
#[test]
fn account_id_resolution_order() {
assert_eq!(
account_id_from_claims(&json!({ "chatgpt_account_id": "root" })).as_deref(),
Some("root")
);
assert_eq!(
account_id_from_claims(
&json!({ "https://api.openai.com/auth": { "chatgpt_account_id": "nested" } })
)
.as_deref(),
Some("nested")
);
assert_eq!(
account_id_from_claims(&json!({
"chatgpt_account_id": "root",
"https://api.openai.com/auth": { "chatgpt_account_id": "nested" }
}))
.as_deref(),
Some("root")
);
assert_eq!(
account_id_from_claims(
&json!({ "organizations": [{ "id": "org-1" }, { "id": "org-2" }] })
)
.as_deref(),
Some("org-1")
);
assert_eq!(account_id_from_claims(&json!({ "email": "x@y.z" })), None);
}
#[test]
fn account_id_prefers_id_token_then_access_token() {
let id_token = jwt(json!({ "chatgpt_account_id": "from-id" }));
let access = jwt(json!({ "chatgpt_account_id": "from-access" }));
assert_eq!(
account_id_from_tokens(Some(&id_token), &access).as_deref(),
Some("from-id")
);
let id_token_no_acct = jwt(json!({ "email": "x@y.z" }));
assert_eq!(
account_id_from_tokens(Some(&id_token_no_acct), &access).as_deref(),
Some("from-access")
);
}
#[test]
fn expiry_from_exp_claim_and_missing() {
let with_exp = jwt(json!({ "exp": 4_000_000_000u64 }));
assert_eq!(
expiry_from_access_token(&with_exp),
UNIX_EPOCH + Duration::from_secs(4_000_000_000)
);
assert_eq!(expiry_from_access_token(&jwt(json!({}))), UNIX_EPOCH);
assert_eq!(expiry_from_access_token("not-a-jwt"), UNIX_EPOCH);
}
#[test]
fn parse_tokens_full_and_derived_account_id() {
let access = jwt(json!({ "exp": 4_000_000_000u64 }));
let raw = json!({
"tokens": { "access_token": access, "refresh_token": "rt", "account_id": "acct-explicit" }
})
.to_string();
let t = parse_tokens(&raw, "test").unwrap();
assert_eq!(t.account_id.as_deref(), Some("acct-explicit"));
assert!(!t.is_expired());
let id_token = jwt(json!({ "chatgpt_account_id": "acct-derived" }));
let raw = json!({
"tokens": { "access_token": access, "refresh_token": "rt", "id_token": id_token }
})
.to_string();
let t = parse_tokens(&raw, "test").unwrap();
assert_eq!(t.account_id.as_deref(), Some("acct-derived"));
}
#[test]
fn parse_tokens_rejects_api_key_mode_and_missing_fields() {
let raw = json!({ "auth_mode": "apikey", "OPENAI_API_KEY": "sk-x" }).to_string();
assert!(parse_tokens(&raw, "test").is_err());
let raw = json!({ "tokens": { "refresh_token": "rt" } }).to_string();
assert!(parse_tokens(&raw, "test").is_err());
}
fn build_body_value(completion: &Completion) -> Result<Value> {
use rig_core::wire::{Body, Mode, Wire};
let wire = codex_wire(&completion.model, "unused", None);
let encoded = wire.encode(codex_request(completion)?, Mode::Unary)?;
let Body::Bytes(bytes) = encoded.request.into_body() else {
panic!("expected a JSON body")
};
Ok(serde_json::from_slice(&bytes)?)
}
#[test]
fn build_body_applies_codex_quirks() {
let completion = Completion {
model: "gpt-5.4".to_string(),
prompt: Message::user("hello"),
preamble: Some("You are a reviewer.".to_string()),
history: vec![],
tools: vec![],
tool_choice: None,
max_tokens: Some(4096),
additional_params: None,
};
let json = build_body_value(&completion).unwrap();
assert_eq!(json["instructions"], "You are a reviewer.");
assert_eq!(json["stream"], true);
assert!(json.get("max_output_tokens").is_none());
assert_eq!(json.get("store"), Some(&Value::Bool(false)));
assert!(
json.get("prompt_cache_key")
.and_then(Value::as_str)
.is_some_and(|key| key.starts_with("nitpicker-")),
"Codex requests must carry a stable prompt cache routing key"
);
assert_eq!(
json.get("include").and_then(Value::as_array),
Some(&vec![Value::String(
"reasoning.encrypted_content".to_string()
)]),
"must request reasoning.encrypted_content to avoid bare rs_ id 404s"
);
let has_system_input = json
.get("input")
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.any(|i| i.get("role").and_then(Value::as_str) == Some("system"))
})
.unwrap_or(false);
assert!(
!has_system_input,
"system must be in `instructions`, not `input`"
);
}
#[test]
fn build_body_requires_a_system_prompt() {
let completion = Completion {
model: "gpt-5.4".to_string(),
prompt: Message::user("hello"),
preamble: None,
history: vec![],
tools: vec![],
tool_choice: None,
max_tokens: None,
additional_params: None,
};
assert!(build_body_value(&completion).is_err());
}
#[test]
fn prompt_cache_key_tracks_the_stable_prefix_not_the_conversation_tail() {
let first = Completion {
model: "gpt-5.4".to_string(),
prompt: Message::user("initial request"),
preamble: Some("stable reviewer and project context".to_string()),
history: vec![],
tools: vec![rig_core::completion::ToolDefinition {
name: ToolName::new("read_file").unwrap(),
annotations: Default::default(),
description: "Read a file".to_string(),
parameters: json!({
"type": "object",
"properties": { "path": { "type": "string" } }
}),
}],
tool_choice: None,
max_tokens: None,
additional_params: None,
};
let mut later_turn = first.clone();
later_turn.history = vec![
Message::user("initial request"),
Message::assistant("intermediate answer"),
];
later_turn.prompt = Message::user("continue");
let first_key = codex_prompt_cache_key(&first).unwrap();
let later_key = codex_prompt_cache_key(&later_turn).unwrap();
assert_eq!(first_key, later_key);
let mut different_prefix = later_turn;
different_prefix.preamble = Some("different reviewer or project context".to_string());
let different_key = codex_prompt_cache_key(&different_prefix).unwrap();
assert_ne!(first_key, different_key);
}
#[test]
fn build_body_preserves_existing_additional_params() {
let completion = Completion {
model: "gpt-5.4".to_string(),
prompt: Message::user("hello"),
preamble: Some("sys".to_string()),
history: vec![],
tools: vec![],
tool_choice: None,
max_tokens: None,
additional_params: Some(json!({ "reasoning": { "effort": "low" } })),
};
let json = build_body_value(&completion).unwrap();
assert_eq!(json.get("store"), Some(&Value::Bool(false)));
assert!(json.get("reasoning").is_some());
}
#[test]
fn body_value_preserves_assistant_text_history() {
let completion = Completion {
model: "gpt-5.5".to_string(),
prompt: Message::user("continue"),
preamble: Some("sys".to_string()),
history: vec![Message::assistant("previous answer")],
tools: vec![],
tool_choice: None,
max_tokens: None,
additional_params: None,
};
let body = build_body_value(&completion).unwrap();
let assistant_message = body["input"]
.as_array()
.unwrap()
.iter()
.find(|item| item.get("role").and_then(Value::as_str) == Some("assistant"))
.unwrap();
assert_eq!(assistant_message["content"][0]["text"], "previous answer");
}
#[test]
fn body_value_omits_foreign_function_call_item_id() {
let tool_id = "tool_5Xt5WpWtZ6Whah4Z2uZcZO34";
let call = ToolCall::new(
CallId::from_wire(tool_id),
ToolFunction::new(
ToolName::new("read_file").unwrap(),
json!({"path":"src/main.rs"}),
),
);
let result = call.result(vec![ToolResultContent::text("ok")]);
let completion = Completion {
model: "gpt-5.5".to_string(),
prompt: Message::user("continue"),
preamble: Some("sys".to_string()),
history: vec![
Message::Assistant(AssistantMessage::new(vec![AssistantContent::ToolCall(
call,
)])),
Message::User {
content: vec![UserContent::ToolResult(result)],
},
],
tools: vec![],
tool_choice: None,
max_tokens: None,
additional_params: None,
};
let body = build_body_value(&completion).unwrap();
let function_call = body["input"]
.as_array()
.unwrap()
.iter()
.find(|item| item.get("type").and_then(Value::as_str) == Some("function_call"))
.unwrap();
assert!(function_call.get("id").is_none());
assert_eq!(function_call["call_id"], tool_id);
let function_call_output = body["input"]
.as_array()
.unwrap()
.iter()
.find(|item| item.get("type").and_then(Value::as_str) == Some("function_call_output"))
.unwrap();
assert_eq!(function_call_output["call_id"], tool_id);
}
#[test]
fn resolve_auth_path_modes() {
let p = resolve_auth_path(Some("/custom/codex"), Some(PathBuf::from("/home/u"))).unwrap();
assert_eq!(p, PathBuf::from("/custom/codex/auth.json"));
assert!(resolve_auth_path(Some("relative/dir"), Some(PathBuf::from("/home/u"))).is_err());
let p = resolve_auth_path(Some(" "), Some(PathBuf::from("/home/u"))).unwrap();
assert_eq!(p, PathBuf::from("/home/u/.codex/auth.json"));
let p = resolve_auth_path(None, Some(PathBuf::from("/home/u"))).unwrap();
assert_eq!(p, PathBuf::from("/home/u/.codex/auth.json"));
assert!(resolve_auth_path(None, None).is_err());
}
async fn codex_reply(sse: String) -> Result<CompletionResponse> {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let base_url = format!("http://{}", listener.local_addr()?);
let server = tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
let mut request = Vec::new();
loop {
assert_ne!(socket.read_buf(&mut request).await.unwrap(), 0);
if let Some(end) = request.windows(4).position(|part| part == b"\r\n\r\n") {
let headers = String::from_utf8_lossy(&request[..end]).to_ascii_lowercase();
let length: usize = headers
.lines()
.find_map(|line| line.strip_prefix("content-length: "))
.unwrap()
.parse()
.unwrap();
if request.len() >= end + 4 + length {
break;
}
}
}
let response = format!(
"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{sse}",
sse.len()
);
socket.write_all(response.as_bytes()).await.unwrap();
});
let mut wire = codex_wire("gpt-5.4", "unused", None);
wire.provider = wire.provider.with_base_url(base_url);
let model =
rig_core::Model::new(wire, rig_reqwest::ReqwestClient::from(build_http_client()?));
let response = model
.call(CompletionRequest::new("hello").preamble("sys"))
.await;
server.await?;
Ok(CompletionResponse::from_rig(response?, "gpt-5.4".into()))
}
#[tokio::test]
async fn parse_sse_accumulates_output_items_with_empty_completed() {
let item = json!({
"type": "message",
"id": "msg_1",
"role": "assistant",
"status": "completed",
"content": [{ "type": "output_text", "text": "pong" }]
});
let completed = json!({
"id": "resp_1",
"object": "response",
"created_at": 1,
"status": "completed",
"model": "gpt-5.4",
"output": [],
"usage": {
"input_tokens": 5000,
"input_tokens_details": { "cached_tokens": 4800 },
"output_tokens": 1,
"output_tokens_details": { "reasoning_tokens": 0 },
"total_tokens": 5001
}
});
let sse = format!(
"data: {}\n\ndata: {}\n\ndata: [DONE]\n",
json!({ "type": "response.output_item.done", "item": item }),
json!({ "type": "response.completed", "response": completed })
);
let resp = codex_reply(sse).await.unwrap();
assert_eq!(resp.text(), "pong");
assert_eq!(resp.finish_reason, crate::llm::FinishReason::Stop);
assert_eq!(resp.usage.input_tokens, 5000);
assert_eq!(resp.usage.cached_input_tokens, 4800);
assert_eq!(resp.usage.total_tokens, 5001);
}
#[tokio::test]
async fn parse_sse_surfaces_error_events() {
let sse = format!(
"data: {}\n\n",
json!({ "type": "response.failed", "error": { "message": "boom" } })
);
assert!(codex_reply(sse).await.is_err());
}
#[tokio::test]
async fn parse_sse_preserves_codes_from_large_error_events() {
let sse = format!(
"data: {}\n\n",
json!({
"type": "response.failed",
"error": {
"type": "usage_limit_reached",
"message": "quota exhausted",
"diagnostic": "x".repeat(4_000),
}
})
);
let err = codex_reply(sse).await.unwrap_err();
assert!(crate::llm::provider_error_has_code(
&err,
&["usage_limit_reached"]
));
}
fn usage_json() -> Value {
json!({
"input_tokens": 5,
"input_tokens_details": { "cached_tokens": 0 },
"output_tokens": 1,
"output_tokens_details": { "reasoning_tokens": 0 },
"total_tokens": 6
})
}
#[tokio::test]
async fn parse_sse_handles_incomplete_as_max_tokens() {
let item = json!({
"type": "message", "id": "m", "role": "assistant", "status": "incomplete",
"content": [{ "type": "output_text", "text": "partial" }]
});
let response = json!({
"id": "r", "object": "response", "created_at": 1, "status": "incomplete",
"model": "gpt-5.4", "output": [],
"incomplete_details": { "reason": "max_output_tokens" },
"usage": usage_json()
});
let sse = format!(
"data: {}\n\ndata: {}\n\n",
json!({ "type": "response.output_item.done", "item": item }),
json!({ "type": "response.incomplete", "response": response })
);
let resp = codex_reply(sse).await.unwrap();
assert_eq!(resp.text(), "partial");
assert_eq!(resp.finish_reason, crate::llm::FinishReason::MaxTokens);
}
#[tokio::test]
async fn parse_sse_rejoins_multiline_data_fields() {
let completed = json!({
"type": "response.completed",
"response": {
"id": "r", "object": "response", "created_at": 1, "status": "completed",
"model": "gpt-5.4",
"output": [{
"type": "message", "id": "m", "role": "assistant", "status": "completed",
"content": [{ "type": "output_text", "text": "hi" }]
}],
"usage": usage_json()
}
})
.to_string();
let comma = completed.find(',').unwrap();
let (head, tail) = completed.split_at(comma);
let sse = format!("data: {head}\ndata: {tail}\n\n");
let resp = codex_reply(sse).await.unwrap();
assert_eq!(resp.text(), "hi");
}
#[test]
fn is_rotation_4xx_excludes_429_and_5xx() {
let mk = |status| {
eyre::Report::new(ProviderError::from_http_response(
reqwest::StatusCode::from_u16(status).unwrap(),
r#"{"error":"test"}"#,
))
};
assert!(is_rotation_4xx(&mk(400)));
assert!(is_rotation_4xx(&mk(401)));
assert!(!is_rotation_4xx(&mk(429)));
assert!(!is_rotation_4xx(&mk(500)));
}
}