mirror of
https://github.com/openai/codex.git
synced 2026-04-24 14:45:27 +00:00
convert to config
This commit is contained in:
@@ -21,7 +21,6 @@ use crate::client_common::ResponseEvent;
|
||||
use crate::client_common::ResponseStream;
|
||||
use crate::error::CodexErr;
|
||||
use crate::error::Result;
|
||||
use crate::flags::OPENAI_REQUEST_MAX_RETRIES;
|
||||
use crate::flags::OPENAI_STREAM_IDLE_TIMEOUT_MS;
|
||||
use crate::models::ContentItem;
|
||||
use crate::models::ResponseItem;
|
||||
@@ -34,6 +33,7 @@ pub(crate) async fn stream_chat_completions(
|
||||
model: &str,
|
||||
client: &reqwest::Client,
|
||||
provider: &ModelProviderInfo,
|
||||
max_retries: u64,
|
||||
) -> Result<ResponseStream> {
|
||||
// Build messages array
|
||||
let mut messages = Vec::<serde_json::Value>::new();
|
||||
@@ -146,7 +146,7 @@ pub(crate) async fn stream_chat_completions(
|
||||
return Err(CodexErr::UnexpectedStatus(status, body));
|
||||
}
|
||||
|
||||
if attempt > *OPENAI_REQUEST_MAX_RETRIES {
|
||||
if attempt > max_retries {
|
||||
return Err(CodexErr::RetryLimit(status));
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ pub(crate) async fn stream_chat_completions(
|
||||
tokio::time::sleep(delay).await;
|
||||
}
|
||||
Err(e) => {
|
||||
if attempt > *OPENAI_REQUEST_MAX_RETRIES {
|
||||
if attempt > max_retries {
|
||||
return Err(e.into());
|
||||
}
|
||||
let delay = backoff(attempt);
|
||||
@@ -569,9 +569,15 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let _ = stream_chat_completions(&prompt, &config.model, &client, &provider)
|
||||
.await
|
||||
.unwrap();
|
||||
let _ = stream_chat_completions(
|
||||
&prompt,
|
||||
&config.model,
|
||||
&client,
|
||||
&provider,
|
||||
config.openai_request_max_retries,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let body = capture.lock().unwrap().take().unwrap();
|
||||
let messages = body.get("messages").unwrap();
|
||||
|
||||
@@ -29,7 +29,6 @@ use crate::config_types::ReasoningSummary as ReasoningSummaryConfig;
|
||||
use crate::error::CodexErr;
|
||||
use crate::error::Result;
|
||||
use crate::flags::CODEX_RS_SSE_FIXTURE;
|
||||
use crate::flags::OPENAI_REQUEST_MAX_RETRIES;
|
||||
use crate::flags::OPENAI_STREAM_IDLE_TIMEOUT_MS;
|
||||
use crate::model_provider_info::ModelProviderInfo;
|
||||
use crate::model_provider_info::WireApi;
|
||||
@@ -77,6 +76,7 @@ impl ModelClient {
|
||||
&self.config.model,
|
||||
&self.client,
|
||||
&self.provider,
|
||||
self.config.openai_request_max_retries,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -135,6 +135,7 @@ impl ModelClient {
|
||||
);
|
||||
|
||||
let mut attempt = 0;
|
||||
let max_retries = self.config.openai_request_max_retries;
|
||||
loop {
|
||||
attempt += 1;
|
||||
|
||||
@@ -171,7 +172,7 @@ impl ModelClient {
|
||||
return Err(CodexErr::UnexpectedStatus(status, body));
|
||||
}
|
||||
|
||||
if attempt > *OPENAI_REQUEST_MAX_RETRIES {
|
||||
if attempt > max_retries {
|
||||
return Err(CodexErr::RetryLimit(status));
|
||||
}
|
||||
|
||||
@@ -188,7 +189,7 @@ impl ModelClient {
|
||||
tokio::time::sleep(delay).await;
|
||||
}
|
||||
Err(e) => {
|
||||
if attempt > *OPENAI_REQUEST_MAX_RETRIES {
|
||||
if attempt > max_retries {
|
||||
return Err(e.into());
|
||||
}
|
||||
let delay = backoff(attempt);
|
||||
@@ -423,7 +424,7 @@ mod tests {
|
||||
|
||||
// ─────────────────────────── Helpers ───────────────────────────
|
||||
|
||||
fn default_config(provider: ModelProviderInfo) -> Arc<Config> {
|
||||
fn default_config(provider: ModelProviderInfo, max_retries: u64) -> Arc<Config> {
|
||||
let codex_home = TempDir::new().unwrap();
|
||||
let mut cfg = Config::load_from_base_config_with_overrides(
|
||||
ConfigToml::default(),
|
||||
@@ -433,10 +434,11 @@ mod tests {
|
||||
.unwrap();
|
||||
cfg.model_provider = provider.clone();
|
||||
cfg.model = "gpt-test".into();
|
||||
cfg.openai_request_max_retries = max_retries;
|
||||
Arc::new(cfg)
|
||||
}
|
||||
|
||||
fn create_test_client(server: &MockServer) -> ModelClient {
|
||||
fn create_test_client(server: &MockServer, max_retries: u64) -> ModelClient {
|
||||
let provider = ModelProviderInfo {
|
||||
name: "openai".into(),
|
||||
base_url: format!("{}/v1", server.uri()),
|
||||
@@ -447,7 +449,7 @@ mod tests {
|
||||
http_headers: None,
|
||||
env_http_headers: None,
|
||||
};
|
||||
let config = default_config(provider.clone());
|
||||
let config = default_config(provider.clone(), max_retries);
|
||||
ModelClient::new(
|
||||
config,
|
||||
provider,
|
||||
@@ -519,9 +521,7 @@ mod tests {
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
unsafe { std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "1") };
|
||||
|
||||
let client = create_test_client(&server);
|
||||
let client = create_test_client(&server, 1);
|
||||
let prompt = Prompt::default();
|
||||
let mut stream = client.stream(&prompt).await.unwrap();
|
||||
while let Some(ev) = stream.next().await {
|
||||
@@ -565,9 +565,7 @@ mod tests {
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
unsafe { std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "1") };
|
||||
|
||||
let client = create_test_client(&server);
|
||||
let client = create_test_client(&server, 1);
|
||||
let prompt = Prompt::default();
|
||||
let mut stream = client.stream(&prompt).await.unwrap();
|
||||
while let Some(ev) = stream.next().await {
|
||||
@@ -614,9 +612,7 @@ mod tests {
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
unsafe { std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "1") };
|
||||
|
||||
let client = create_test_client(&server);
|
||||
let client = create_test_client(&server, 1);
|
||||
let prompt = Prompt::default();
|
||||
let mut stream = client.stream(&prompt).await.unwrap();
|
||||
while let Some(ev) = stream.next().await {
|
||||
@@ -643,9 +639,7 @@ mod tests {
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
unsafe { std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "0") };
|
||||
|
||||
let client = create_test_client(&server);
|
||||
let client = create_test_client(&server, 0);
|
||||
let prompt = Prompt::default();
|
||||
match client.stream(&prompt).await {
|
||||
Ok(_) => panic!("expected error"),
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::config_types::ShellEnvironmentPolicyToml;
|
||||
use crate::config_types::Tui;
|
||||
use crate::config_types::UriBasedFileOpener;
|
||||
use crate::flags::OPENAI_DEFAULT_MODEL;
|
||||
use crate::flags::OPENAI_REQUEST_MAX_RETRIES;
|
||||
use crate::model_provider_info::ModelProviderInfo;
|
||||
use crate::model_provider_info::built_in_model_providers;
|
||||
use crate::openai_model_info::get_model_info;
|
||||
@@ -137,6 +138,9 @@ pub struct Config {
|
||||
|
||||
/// Base URL for requests to ChatGPT (as opposed to the OpenAI API).
|
||||
pub chatgpt_base_url: String,
|
||||
|
||||
/// Max number of retries for a request to the model.
|
||||
pub openai_request_max_retries: u64,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -321,6 +325,9 @@ pub struct ConfigToml {
|
||||
|
||||
/// Base URL for requests to ChatGPT (as opposed to the OpenAI API).
|
||||
pub chatgpt_base_url: Option<String>,
|
||||
|
||||
/// Max number of retries for a request to the model.
|
||||
pub openai_request_max_retries: Option<u64>,
|
||||
}
|
||||
|
||||
impl ConfigToml {
|
||||
@@ -353,6 +360,7 @@ pub struct ConfigOverrides {
|
||||
pub model_provider: Option<String>,
|
||||
pub config_profile: Option<String>,
|
||||
pub codex_linux_sandbox_exe: Option<PathBuf>,
|
||||
pub openai_request_max_retries: Option<u64>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -374,6 +382,7 @@ impl Config {
|
||||
model_provider,
|
||||
config_profile: config_profile_key,
|
||||
codex_linux_sandbox_exe,
|
||||
openai_request_max_retries,
|
||||
} = overrides;
|
||||
|
||||
let config_profile = match config_profile_key.as_ref().or(cfg.profile.as_ref()) {
|
||||
@@ -448,6 +457,12 @@ impl Config {
|
||||
.as_ref()
|
||||
.map(|info| info.max_output_tokens)
|
||||
});
|
||||
|
||||
// Resolve the max-retry setting (CLI override > config.toml > env flag default).
|
||||
let resolved_openai_request_max_retries = openai_request_max_retries
|
||||
.or(cfg.openai_request_max_retries)
|
||||
.unwrap_or_else(|| *OPENAI_REQUEST_MAX_RETRIES);
|
||||
|
||||
let config = Self {
|
||||
model,
|
||||
model_context_window,
|
||||
@@ -494,6 +509,8 @@ impl Config {
|
||||
.chatgpt_base_url
|
||||
.or(cfg.chatgpt_base_url)
|
||||
.unwrap_or("https://chatgpt.com/backend-api/".to_string()),
|
||||
|
||||
openai_request_max_retries: resolved_openai_request_max_retries,
|
||||
};
|
||||
Ok(config)
|
||||
}
|
||||
@@ -559,6 +576,7 @@ pub fn log_dir(cfg: &Config) -> std::io::Result<PathBuf> {
|
||||
mod tests {
|
||||
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
||||
use crate::config_types::HistoryPersistence;
|
||||
use crate::flags::OPENAI_REQUEST_MAX_RETRIES;
|
||||
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
@@ -800,6 +818,7 @@ disable_response_storage = true
|
||||
model_reasoning_summary: ReasoningSummary::Detailed,
|
||||
model_supports_reasoning_summaries: false,
|
||||
chatgpt_base_url: "https://chatgpt.com/backend-api/".to_string(),
|
||||
openai_request_max_retries: *OPENAI_REQUEST_MAX_RETRIES,
|
||||
},
|
||||
o3_profile_config
|
||||
);
|
||||
@@ -846,6 +865,7 @@ disable_response_storage = true
|
||||
model_reasoning_summary: ReasoningSummary::default(),
|
||||
model_supports_reasoning_summaries: false,
|
||||
chatgpt_base_url: "https://chatgpt.com/backend-api/".to_string(),
|
||||
openai_request_max_retries: *OPENAI_REQUEST_MAX_RETRIES,
|
||||
};
|
||||
|
||||
assert_eq!(expected_gpt3_profile_config, gpt3_profile_config);
|
||||
@@ -907,6 +927,7 @@ disable_response_storage = true
|
||||
model_reasoning_summary: ReasoningSummary::default(),
|
||||
model_supports_reasoning_summaries: false,
|
||||
chatgpt_base_url: "https://chatgpt.com/backend-api/".to_string(),
|
||||
openai_request_max_retries: *OPENAI_REQUEST_MAX_RETRIES,
|
||||
};
|
||||
|
||||
assert_eq!(expected_zdr_profile_config, zdr_profile_config);
|
||||
|
||||
@@ -55,12 +55,13 @@ async fn spawn_codex() -> Result<Codex, CodexErr> {
|
||||
// beginning of the test, before we spawn any background tasks that could
|
||||
// observe the environment.
|
||||
unsafe {
|
||||
std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "2");
|
||||
std::env::set_var("OPENAI_STREAM_MAX_RETRIES", "2");
|
||||
}
|
||||
|
||||
let codex_home = TempDir::new().unwrap();
|
||||
let config = load_default_config_for_test(&codex_home);
|
||||
let mut config = load_default_config_for_test(&codex_home);
|
||||
// Live tests keep retries low to avoid slow backoffs on flaky networks.
|
||||
config.openai_request_max_retries = 2;
|
||||
let (agent, _init_id) = Codex::spawn(config, std::sync::Arc::new(Notify::new())).await?;
|
||||
|
||||
Ok(agent)
|
||||
@@ -79,7 +80,7 @@ async fn live_streaming_and_prev_id_reset() {
|
||||
|
||||
let codex = spawn_codex().await.unwrap();
|
||||
|
||||
// ---------- Task 1 ----------
|
||||
// ---------- Task 1 ----------
|
||||
codex
|
||||
.submit(Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
@@ -113,7 +114,7 @@ async fn live_streaming_and_prev_id_reset() {
|
||||
"Agent did not stream any AgentMessage before TaskComplete"
|
||||
);
|
||||
|
||||
// ---------- Task 2 (same session) ----------
|
||||
// ---------- Task 2 (same session) ----------
|
||||
codex
|
||||
.submit(Op::UserInput {
|
||||
items: vec![InputItem::Text {
|
||||
|
||||
@@ -91,8 +91,8 @@ async fn keeps_previous_response_id_between_tasks() {
|
||||
// Environment
|
||||
// Update environment – `set_var` is `unsafe` starting with the 2024
|
||||
// edition so we group the calls into a single `unsafe { … }` block.
|
||||
// NOTE: per-request retry count is now configured directly on the Config.
|
||||
unsafe {
|
||||
std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "0");
|
||||
std::env::set_var("OPENAI_STREAM_MAX_RETRIES", "0");
|
||||
}
|
||||
let model_provider = ModelProviderInfo {
|
||||
@@ -113,6 +113,8 @@ async fn keeps_previous_response_id_between_tasks() {
|
||||
let codex_home = TempDir::new().unwrap();
|
||||
let mut config = load_default_config_for_test(&codex_home);
|
||||
config.model_provider = model_provider;
|
||||
// No per-request retries so each new user input triggers exactly one HTTP request.
|
||||
config.openai_request_max_retries = 0;
|
||||
let ctrl_c = std::sync::Arc::new(tokio::sync::Notify::new());
|
||||
let (codex, _init_id) = Codex::spawn(config, ctrl_c.clone()).await.unwrap();
|
||||
|
||||
|
||||
@@ -74,12 +74,11 @@ async fn retries_on_early_close() {
|
||||
//
|
||||
// As of Rust 2024 `std::env::set_var` has been made `unsafe` because
|
||||
// mutating the process environment is inherently racy when other threads
|
||||
// are running. We therefore have to wrap every call in an explicit
|
||||
// `unsafe` block. These are limited to the test-setup section so the
|
||||
// scope is very small and clearly delineated.
|
||||
// are running. We used to tweak the per-request retry counts via the
|
||||
// `OPENAI_REQUEST_MAX_RETRIES` env var but that caused data races in
|
||||
// multi-threaded tests. Configure the value directly on the Config instead.
|
||||
|
||||
unsafe {
|
||||
std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "0");
|
||||
std::env::set_var("OPENAI_STREAM_MAX_RETRIES", "1");
|
||||
std::env::set_var("OPENAI_STREAM_IDLE_TIMEOUT_MS", "2000");
|
||||
}
|
||||
@@ -102,6 +101,8 @@ async fn retries_on_early_close() {
|
||||
let codex_home = TempDir::new().unwrap();
|
||||
let mut config = load_default_config_for_test(&codex_home);
|
||||
config.model_provider = model_provider;
|
||||
// Disable per-request retries (we want to exercise stream-level retries).
|
||||
config.openai_request_max_retries = 0;
|
||||
let (codex, _init_id) = Codex::spawn(config, ctrl_c).await.unwrap();
|
||||
|
||||
codex
|
||||
|
||||
@@ -104,6 +104,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option<PathBuf>) -> any
|
||||
cwd: cwd.map(|p| p.canonicalize().unwrap_or(p)),
|
||||
model_provider: None,
|
||||
codex_linux_sandbox_exe,
|
||||
openai_request_max_retries: None,
|
||||
};
|
||||
// Parse `-c` overrides.
|
||||
let cli_kv_overrides = match config_overrides.parse_overrides() {
|
||||
|
||||
@@ -142,6 +142,7 @@ impl CodexToolCallParam {
|
||||
sandbox_mode: sandbox.map(Into::into),
|
||||
model_provider: None,
|
||||
codex_linux_sandbox_exe,
|
||||
openai_request_max_retries: None,
|
||||
};
|
||||
|
||||
let cli_overrides = cli_overrides
|
||||
|
||||
@@ -75,6 +75,7 @@ pub fn run_main(cli: Cli, codex_linux_sandbox_exe: Option<PathBuf>) -> std::io::
|
||||
model_provider: None,
|
||||
config_profile: cli.config_profile.clone(),
|
||||
codex_linux_sandbox_exe,
|
||||
openai_request_max_retries: None,
|
||||
};
|
||||
// Parse `-c` overrides from the CLI.
|
||||
let cli_kv_overrides = match cli.config_overrides.parse_overrides() {
|
||||
|
||||
Reference in New Issue
Block a user