diff --git a/.github/codex/home/config.toml b/.github/codex/home/config.toml index 8e800ca068..7b065212a8 100644 --- a/.github/codex/home/config.toml +++ b/.github/codex/home/config.toml @@ -1,3 +1,3 @@ -model = "gpt-5" +model = "gpt-5.1" # Consider setting [mcp_servers] here! diff --git a/.github/workflows/issue-deduplicator.yml b/.github/workflows/issue-deduplicator.yml index c36857cab0..579b6a3685 100644 --- a/.github/workflows/issue-deduplicator.yml +++ b/.github/workflows/issue-deduplicator.yml @@ -46,7 +46,7 @@ jobs: with: openai-api-key: ${{ secrets.CODEX_OPENAI_API_KEY }} allow-users: "*" - model: gpt-5 + model: gpt-5.1 prompt: | You are an assistant that triages new GitHub issues by identifying potential duplicates. diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index 5791a3b5bd..db9bed1112 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -524,7 +524,7 @@ mod tests { let request = ClientRequest::NewConversation { request_id: RequestId::Integer(42), params: v1::NewConversationParams { - model: Some("gpt-5-codex".to_string()), + model: Some("gpt-5.1-codex".to_string()), model_provider: None, profile: None, cwd: None, @@ -542,7 +542,7 @@ mod tests { "method": "newConversation", "id": 42, "params": { - "model": "gpt-5-codex", + "model": "gpt-5.1-codex", "modelProvider": null, "profile": null, "cwd": null, diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 3ec5b4517d..5f9b87458f 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -74,7 +74,7 @@ Start a fresh thread when you need a new Codex conversation. { "method": "thread/start", "id": 10, "params": { // Optionally set config settings. If not specified, will use the user's // current config settings. - "model": "gpt-5-codex", + "model": "gpt-5.1-codex", "cwd": "/Users/me/project", "approvalPolicy": "never", "sandbox": "workspaceWrite", @@ -155,7 +155,7 @@ You can optionally specify config overrides on the new turn. If specified, these "writableRoots": ["/Users/me/project"], "networkAccess": true }, - "model": "gpt-5-codex", + "model": "gpt-5.1-codex", "effort": "medium", "summary": "concise" } } diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index c5ac6a14af..c5fa2a7fa2 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -158,8 +158,8 @@ struct ActiveLogin { login_id: Uuid, } -impl ActiveLogin { - fn drop(&self) { +impl Drop for ActiveLogin { + fn drop(&mut self) { self.shutdown_handle.shutdown(); } } @@ -417,7 +417,7 @@ impl CodexMessageProcessor { { let mut guard = self.active_login.lock().await; if let Some(active) = guard.take() { - active.drop(); + drop(active); } } @@ -525,7 +525,7 @@ impl CodexMessageProcessor { { let mut guard = self.active_login.lock().await; if let Some(existing) = guard.take() { - existing.drop(); + drop(existing); } *guard = Some(ActiveLogin { shutdown_handle: shutdown_handle.clone(), @@ -615,7 +615,7 @@ impl CodexMessageProcessor { { let mut guard = self.active_login.lock().await; if let Some(existing) = guard.take() { - existing.drop(); + drop(existing); } *guard = Some(ActiveLogin { shutdown_handle: shutdown_handle.clone(), @@ -704,7 +704,7 @@ impl CodexMessageProcessor { let mut guard = self.active_login.lock().await; if guard.as_ref().map(|l| l.login_id) == Some(login_id) { if let Some(active) = guard.take() { - active.drop(); + drop(active); } Ok(()) } else { @@ -758,7 +758,7 @@ impl CodexMessageProcessor { { let mut guard = self.active_login.lock().await; if let Some(active) = guard.take() { - active.drop(); + drop(active); } } diff --git a/codex-rs/app-server/src/fuzzy_file_search.rs b/codex-rs/app-server/src/fuzzy_file_search.rs index fcb05852ba..5c6d86e184 100644 --- a/codex-rs/app-server/src/fuzzy_file_search.rs +++ b/codex-rs/app-server/src/fuzzy_file_search.rs @@ -19,6 +19,10 @@ pub(crate) async fn run_fuzzy_file_search( roots: Vec, cancellation_flag: Arc, ) -> Vec { + if roots.is_empty() { + return Vec::new(); + } + #[expect(clippy::expect_used)] let limit_per_root = NonZero::new(LIMIT_PER_ROOT).expect("LIMIT_PER_ROOT should be a valid non-zero usize"); diff --git a/codex-rs/app-server/src/lib.rs b/codex-rs/app-server/src/lib.rs index 4b65e66d2c..9ad6f50b21 100644 --- a/codex-rs/app-server/src/lib.rs +++ b/codex-rs/app-server/src/lib.rs @@ -47,7 +47,7 @@ pub async fn run_main( ) -> IoResult<()> { // Set up channels. let (incoming_tx, mut incoming_rx) = mpsc::channel::(CHANNEL_CAPACITY); - let (outgoing_tx, mut outgoing_rx) = mpsc::unbounded_channel::(); + let (outgoing_tx, mut outgoing_rx) = mpsc::channel::(CHANNEL_CAPACITY); // Task: read from stdin, push to `incoming_tx`. let stdin_reader_handle = tokio::spawn({ diff --git a/codex-rs/app-server/src/outgoing_message.rs b/codex-rs/app-server/src/outgoing_message.rs index f0ee6cf939..40260c8b94 100644 --- a/codex-rs/app-server/src/outgoing_message.rs +++ b/codex-rs/app-server/src/outgoing_message.rs @@ -19,12 +19,12 @@ use crate::error_code::INTERNAL_ERROR_CODE; /// Sends messages to the client and manages request callbacks. pub(crate) struct OutgoingMessageSender { next_request_id: AtomicI64, - sender: mpsc::UnboundedSender, + sender: mpsc::Sender, request_id_to_callback: Mutex>>, } impl OutgoingMessageSender { - pub(crate) fn new(sender: mpsc::UnboundedSender) -> Self { + pub(crate) fn new(sender: mpsc::Sender) -> Self { Self { next_request_id: AtomicI64::new(0), sender, @@ -45,8 +45,12 @@ impl OutgoingMessageSender { } let outgoing_message = - OutgoingMessage::Request(request.request_with_id(outgoing_message_id)); - let _ = self.sender.send(outgoing_message); + OutgoingMessage::Request(request.request_with_id(outgoing_message_id.clone())); + if let Err(err) = self.sender.send(outgoing_message).await { + warn!("failed to send request {outgoing_message_id:?} to client: {err:?}"); + let mut request_id_to_callback = self.request_id_to_callback.lock().await; + request_id_to_callback.remove(&outgoing_message_id); + } rx_approve } @@ -72,7 +76,9 @@ impl OutgoingMessageSender { match serde_json::to_value(response) { Ok(result) => { let outgoing_message = OutgoingMessage::Response(OutgoingResponse { id, result }); - let _ = self.sender.send(outgoing_message); + if let Err(err) = self.sender.send(outgoing_message).await { + warn!("failed to send response to client: {err:?}"); + } } Err(err) => { self.send_error( @@ -89,21 +95,29 @@ impl OutgoingMessageSender { } pub(crate) async fn send_server_notification(&self, notification: ServerNotification) { - let _ = self + if let Err(err) = self .sender - .send(OutgoingMessage::AppServerNotification(notification)); + .send(OutgoingMessage::AppServerNotification(notification)) + .await + { + warn!("failed to send server notification to client: {err:?}"); + } } /// All notifications should be migrated to [`ServerNotification`] and /// [`OutgoingMessage::Notification`] should be removed. pub(crate) async fn send_notification(&self, notification: OutgoingNotification) { let outgoing_message = OutgoingMessage::Notification(notification); - let _ = self.sender.send(outgoing_message); + if let Err(err) = self.sender.send(outgoing_message).await { + warn!("failed to send notification to client: {err:?}"); + } } pub(crate) async fn send_error(&self, id: RequestId, error: JSONRPCErrorError) { let outgoing_message = OutgoingMessage::Error(OutgoingError { id, error }); - let _ = self.sender.send(outgoing_message); + if let Err(err) = self.sender.send(outgoing_message).await { + warn!("failed to send error to client: {err:?}"); + } } } diff --git a/codex-rs/app-server/tests/suite/config.rs b/codex-rs/app-server/tests/suite/config.rs index 227d30bbe3..281d54927b 100644 --- a/codex-rs/app-server/tests/suite/config.rs +++ b/codex-rs/app-server/tests/suite/config.rs @@ -27,7 +27,7 @@ fn create_config_toml(codex_home: &Path) -> std::io::Result<()> { std::fs::write( config_toml, r#" -model = "gpt-5-codex" +model = "gpt-5.1-codex" approval_policy = "on-request" sandbox_mode = "workspace-write" model_reasoning_summary = "detailed" @@ -87,7 +87,7 @@ async fn get_config_toml_parses_all_fields() -> Result<()> { }), forced_chatgpt_workspace_id: Some("12345678-0000-0000-0000-000000000000".into()), forced_login_method: Some(ForcedLoginMethod::Chatgpt), - model: Some("gpt-5-codex".into()), + model: Some("gpt-5.1-codex".into()), model_reasoning_effort: Some(ReasoningEffort::High), model_reasoning_summary: Some(ReasoningSummary::Detailed), model_verbosity: Some(Verbosity::Medium), diff --git a/codex-rs/app-server/tests/suite/set_default_model.rs b/codex-rs/app-server/tests/suite/set_default_model.rs index 0c2aa22999..f3af141c07 100644 --- a/codex-rs/app-server/tests/suite/set_default_model.rs +++ b/codex-rs/app-server/tests/suite/set_default_model.rs @@ -57,7 +57,7 @@ fn create_config_toml(codex_home: &Path) -> std::io::Result<()> { std::fs::write( config_toml, r#" -model = "gpt-5-codex" +model = "gpt-5.1-codex" model_reasoning_effort = "medium" "#, ) diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs index c201187860..bda2d14172 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_resume.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs @@ -27,7 +27,7 @@ async fn thread_resume_returns_original_thread() -> Result<()> { // Start a thread. let start_id = mcp .send_thread_start_request(ThreadStartParams { - model: Some("gpt-5-codex".to_string()), + model: Some("gpt-5.1-codex".to_string()), ..Default::default() }) .await?; @@ -68,7 +68,7 @@ async fn thread_resume_prefers_path_over_thread_id() -> Result<()> { let start_id = mcp .send_thread_start_request(ThreadStartParams { - model: Some("gpt-5-codex".to_string()), + model: Some("gpt-5.1-codex".to_string()), ..Default::default() }) .await?; @@ -112,7 +112,7 @@ async fn thread_resume_supports_history_and_overrides() -> Result<()> { // Start a thread. let start_id = mcp .send_thread_start_request(ThreadStartParams { - model: Some("gpt-5-codex".to_string()), + model: Some("gpt-5.1-codex".to_string()), ..Default::default() }) .await?; diff --git a/codex-rs/app-server/tests/suite/v2/thread_start.rs b/codex-rs/app-server/tests/suite/v2/thread_start.rs index ac2420916b..a5e4c0d487 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_start.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_start.rs @@ -29,7 +29,7 @@ async fn thread_start_creates_thread_and_emits_started() -> Result<()> { // Start a v2 thread with an explicit model override. let req_id = mcp .send_thread_start_request(ThreadStartParams { - model: Some("gpt-5".to_string()), + model: Some("gpt-5.1".to_string()), ..Default::default() }) .await?; diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 75a7cb8e44..6a3b24aa98 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -761,9 +761,9 @@ mod tests { #[test] fn resume_model_flag_applies_when_no_root_flags() { - let interactive = finalize_from_args(["codex", "resume", "-m", "gpt-5-test"].as_ref()); + let interactive = finalize_from_args(["codex", "resume", "-m", "gpt-5.1-test"].as_ref()); - assert_eq!(interactive.model.as_deref(), Some("gpt-5-test")); + assert_eq!(interactive.model.as_deref(), Some("gpt-5.1-test")); assert!(interactive.resume_picker); assert!(!interactive.resume_last); assert_eq!(interactive.resume_session_id, None); @@ -808,7 +808,7 @@ mod tests { "--ask-for-approval", "on-request", "-m", - "gpt-5-test", + "gpt-5.1-test", "-p", "my-profile", "-C", @@ -819,7 +819,7 @@ mod tests { .as_ref(), ); - assert_eq!(interactive.model.as_deref(), Some("gpt-5-test")); + assert_eq!(interactive.model.as_deref(), Some("gpt-5.1-test")); assert!(interactive.oss); assert_eq!(interactive.config_profile.as_deref(), Some("my-profile")); assert_matches!( diff --git a/codex-rs/core/src/chat_completions.rs b/codex-rs/core/src/chat_completions.rs index a60db89d83..785a4d4ce5 100644 --- a/codex-rs/core/src/chat_completions.rs +++ b/codex-rs/core/src/chat_completions.rs @@ -81,6 +81,7 @@ pub(crate) async fn stream_chat_completions( ResponseItem::CustomToolCallOutput { .. } => {} ResponseItem::WebSearchCall { .. } => {} ResponseItem::GhostSnapshot { .. } => {} + ResponseItem::CompactionSummary { .. } => {} } } @@ -320,7 +321,8 @@ pub(crate) async fn stream_chat_completions( } ResponseItem::Reasoning { .. } | ResponseItem::WebSearchCall { .. } - | ResponseItem::Other => { + | ResponseItem::Other + | ResponseItem::CompactionSummary { .. } => { // Omit these items from the conversation history. continue; } diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 944c86a31e..5698d888e1 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -26,6 +26,7 @@ use tokio::sync::mpsc; use tokio::time::timeout; use tokio_util::io::ReaderStream; use tracing::debug; +use tracing::enabled; use tracing::trace; use tracing::warn; @@ -78,6 +79,18 @@ struct Error { resets_at: Option, } +#[derive(Debug, Serialize)] +struct CompactHistoryRequest<'a> { + model: &'a str, + input: &'a [ResponseItem], + instructions: &'a str, +} + +#[derive(Debug, Deserialize)] +struct CompactHistoryResponse { + output: Vec, +} + #[derive(Debug, Clone)] pub struct ModelClient { config: Arc, @@ -511,6 +524,70 @@ impl ModelClient { pub fn get_auth_manager(&self) -> Option> { self.auth_manager.clone() } + + pub async fn compact_conversation_history(&self, prompt: &Prompt) -> Result> { + if prompt.input.is_empty() { + return Ok(Vec::new()); + } + let auth_manager = self.auth_manager.clone(); + let auth = auth_manager.as_ref().and_then(|m| m.auth()); + let mut req_builder = self + .provider + .create_compact_request_builder(&self.client, &auth) + .await?; + if let SessionSource::SubAgent(sub) = &self.session_source { + let subagent = if let crate::protocol::SubAgentSource::Other(label) = sub { + label.clone() + } else { + serde_json::to_value(sub) + .ok() + .and_then(|v| v.as_str().map(std::string::ToString::to_string)) + .unwrap_or_else(|| "other".to_string()) + }; + req_builder = req_builder.header("x-openai-subagent", subagent); + } + if let Some(auth) = auth.as_ref() + && auth.mode == AuthMode::ChatGPT + && let Some(account_id) = auth.get_account_id() + { + req_builder = req_builder.header("chatgpt-account-id", account_id); + } + let payload = CompactHistoryRequest { + model: &self.config.model, + input: &prompt.input, + instructions: &prompt.get_full_instructions(&self.config.model_family), + }; + + if enabled!(tracing::Level::TRACE) { + trace!( + "POST to {}: {}", + self.provider + .get_compact_url(&auth) + .unwrap_or("".to_string()), + serde_json::to_value(&payload).unwrap_or_default() + ); + } + + let response = req_builder + .json(&payload) + .send() + .await + .map_err(|source| CodexErr::ConnectionFailed(ConnectionFailedError { source }))?; + let status = response.status(); + let body = response + .text() + .await + .map_err(|source| CodexErr::ConnectionFailed(ConnectionFailedError { source }))?; + if !status.is_success() { + return Err(CodexErr::UnexpectedStatus(UnexpectedResponseError { + status, + body, + request_id: None, + })); + } + let CompactHistoryResponse { output } = serde_json::from_str(&body)?; + Ok(output) + } } enum StreamAttemptError { @@ -1255,7 +1332,7 @@ mod tests { #[tokio::test] async fn error_when_error_event() { - let raw_error = r#"{"type":"response.failed","sequence_number":3,"response":{"id":"resp_689bcf18d7f08194bf3440ba62fe05d803fee0cdac429894","object":"response","created_at":1755041560,"status":"failed","background":false,"error":{"code":"rate_limit_exceeded","message":"Rate limit reached for gpt-5 in organization org-AAA on tokens per min (TPM): Limit 30000, Used 22999, Requested 12528. Please try again in 11.054s. Visit https://platform.openai.com/account/rate-limits to learn more."}, "usage":null,"user":null,"metadata":{}}}"#; + let raw_error = r#"{"type":"response.failed","sequence_number":3,"response":{"id":"resp_689bcf18d7f08194bf3440ba62fe05d803fee0cdac429894","object":"response","created_at":1755041560,"status":"failed","background":false,"error":{"code":"rate_limit_exceeded","message":"Rate limit reached for gpt-5.1 in organization org-AAA on tokens per min (TPM): Limit 30000, Used 22999, Requested 12528. Please try again in 11.054s. Visit https://platform.openai.com/account/rate-limits to learn more."}, "usage":null,"user":null,"metadata":{}}}"#; let sse1 = format!("event: response.failed\ndata: {raw_error}\n\n"); let provider = ModelProviderInfo { @@ -1284,7 +1361,7 @@ mod tests { Err(CodexErr::Stream(msg, delay)) => { assert_eq!( msg, - "Rate limit reached for gpt-5 in organization org-AAA on tokens per min (TPM): Limit 30000, Used 22999, Requested 12528. Please try again in 11.054s. Visit https://platform.openai.com/account/rate-limits to learn more." + "Rate limit reached for gpt-5.1 in organization org-AAA on tokens per min (TPM): Limit 30000, Used 22999, Requested 12528. Please try again in 11.054s. Visit https://platform.openai.com/account/rate-limits to learn more." ); assert_eq!(*delay, Some(Duration::from_secs_f64(11.054))); } @@ -1503,7 +1580,7 @@ mod tests { fn test_try_parse_retry_after() { let err = Error { r#type: None, - message: Some("Rate limit reached for gpt-5 in organization org- on tokens per min (TPM): Limit 1, Used 1, Requested 19304. Please try again in 28ms. Visit https://platform.openai.com/account/rate-limits to learn more.".to_string()), + message: Some("Rate limit reached for gpt-5.1 in organization org- on tokens per min (TPM): Limit 1, Used 1, Requested 19304. Please try again in 28ms. Visit https://platform.openai.com/account/rate-limits to learn more.".to_string()), code: Some("rate_limit_exceeded".to_string()), plan_type: None, resets_at: None @@ -1517,7 +1594,7 @@ mod tests { fn test_try_parse_retry_after_no_delay() { let err = Error { r#type: None, - message: Some("Rate limit reached for gpt-5 in organization on tokens per min (TPM): Limit 30000, Used 6899, Requested 24050. Please try again in 1.898s. Visit https://platform.openai.com/account/rate-limits to learn more.".to_string()), + message: Some("Rate limit reached for gpt-5.1 in organization on tokens per min (TPM): Limit 30000, Used 6899, Requested 24050. Please try again in 1.898s. Visit https://platform.openai.com/account/rate-limits to learn more.".to_string()), code: Some("rate_limit_exceeded".to_string()), plan_type: None, resets_at: None diff --git a/codex-rs/core/src/client_common.rs b/codex-rs/core/src/client_common.rs index 9494ffcdf4..09cc769220 100644 --- a/codex-rs/core/src/client_common.rs +++ b/codex-rs/core/src/client_common.rs @@ -427,7 +427,7 @@ mod tests { expects_apply_patch_instructions: false, }, InstructionsTestCase { - slug: "gpt-5-codex", + slug: "gpt-5.1-codex", expects_apply_patch_instructions: false, }, InstructionsTestCase { @@ -457,7 +457,7 @@ mod tests { let input: Vec = vec![]; let tools: Vec = vec![]; let req = ResponsesApiRequest { - model: "gpt-5", + model: "gpt-5.1", instructions: "i", input: &input, tools: &tools, @@ -498,7 +498,7 @@ mod tests { create_text_param_for_request(None, &Some(schema.clone())).expect("text controls"); let req = ResponsesApiRequest { - model: "gpt-5", + model: "gpt-5.1", instructions: "i", input: &input, tools: &tools, @@ -534,7 +534,7 @@ mod tests { let input: Vec = vec![]; let tools: Vec = vec![]; let req = ResponsesApiRequest { - model: "gpt-5", + model: "gpt-5.1", instructions: "i", input: &input, tools: &tools, diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 3196176823..64d06d0571 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -55,6 +55,7 @@ use crate::ModelProviderInfo; use crate::client::ModelClient; use crate::client_common::Prompt; use crate::client_common::ResponseEvent; +use crate::compact::collect_user_messages; use crate::config::Config; use crate::config::types::ShellEnvironmentPolicy; use crate::context_manager::ContextManager; @@ -63,10 +64,6 @@ use crate::error::CodexErr; use crate::error::Result as CodexResult; #[cfg(test)] use crate::exec::StreamOutput; -// Removed: legacy executor wiring replaced by ToolOrchestrator flows. -// legacy normalize_exec_result no longer used after orchestrator migration -use crate::compact::build_compacted_history; -use crate::compact::collect_user_messages; use crate::mcp::auth::compute_auth_statuses; use crate::mcp_connection_manager::McpConnectionManager; use crate::model_family::find_family_for_model; @@ -956,13 +953,18 @@ impl Session { } RolloutItem::Compacted(compacted) => { let snapshot = history.get_history(); - let user_messages = collect_user_messages(&snapshot); - let rebuilt = build_compacted_history( - self.build_initial_context(turn_context), - &user_messages, - &compacted.message, - ); - history.replace(rebuilt); + // TODO(jif) clean + if let Some(replacement) = &compacted.replacement_history { + history.replace(replacement.clone()); + } else { + let user_messages = collect_user_messages(&snapshot); + let rebuilt = compact::build_compacted_history( + self.build_initial_context(turn_context), + &user_messages, + &compacted.message, + ); + history.replace(rebuilt); + } } _ => {} } @@ -990,6 +992,15 @@ impl Session { self.persist_rollout_items(&rollout_items).await; } + pub async fn enabled(&self, feature: Feature) -> bool { + self.state + .lock() + .await + .session_configuration + .features + .enabled(feature) + } + async fn send_raw_response_items(&self, turn_context: &TurnContext, items: &[ResponseItem]) { for item in items { self.send_event( @@ -1167,14 +1178,7 @@ impl Session { turn_context: Arc, cancellation_token: CancellationToken, ) { - if !self - .state - .lock() - .await - .session_configuration - .features - .enabled(Feature::GhostCommit) - { + if !self.enabled(Feature::GhostCommit).await { return; } let token = match turn_context.tool_call_gate.subscribe().await { @@ -1415,7 +1419,7 @@ mod handlers { use codex_protocol::protocol::ReviewDecision; use codex_protocol::protocol::ReviewRequest; use codex_protocol::protocol::TurnAbortReason; - use codex_protocol::user_input::UserInput; + use std::sync::Arc; use tracing::info; use tracing::warn; @@ -1623,16 +1627,9 @@ mod handlers { let turn_context = sess .new_turn_with_sub_id(sub_id, SessionSettingsUpdate::default()) .await; - // Attempt to inject input into current task - if let Err(items) = sess - .inject_input(vec![UserInput::Text { - text: turn_context.compact_prompt().to_string(), - }]) - .await - { - sess.spawn_task(Arc::clone(&turn_context), items, CompactTask) - .await; - } + + sess.spawn_task(Arc::clone(&turn_context), vec![], CompactTask) + .await; } pub async fn shutdown(sess: &Arc, sub_id: String) -> bool { @@ -1937,12 +1934,30 @@ async fn run_turn( .client .get_model_family() .supports_parallel_tool_calls; - let parallel_tool_calls = model_supports_parallel; + + // TODO(jif) revert once testing phase is done. + let parallel_tool_calls = model_supports_parallel + && sess + .state + .lock() + .await + .session_configuration + .features + .enabled(Feature::ParallelToolCalls); + let mut base_instructions = turn_context.base_instructions.clone(); + if parallel_tool_calls { + static INSTRUCTIONS: &str = include_str!("../templates/parallel/instructions.md"); + static INSERTION_SPOT: &str = "## Editing constraints"; + base_instructions + .as_mut() + .map(|base| base.replace(INSERTION_SPOT, INSTRUCTIONS)); + } + let prompt = Prompt { input, tools: router.specs(), parallel_tool_calls, - base_instructions_override: turn_context.base_instructions.clone(), + base_instructions_override: base_instructions, output_schema: turn_context.final_output_json_schema.clone(), }; @@ -2896,7 +2911,7 @@ mod tests { let summary1 = "summary one"; let snapshot1 = live_history.get_history(); let user_messages1 = collect_user_messages(&snapshot1); - let rebuilt1 = build_compacted_history( + let rebuilt1 = compact::build_compacted_history( session.build_initial_context(turn_context), &user_messages1, summary1, @@ -2904,6 +2919,7 @@ mod tests { live_history.replace(rebuilt1); rollout_items.push(RolloutItem::Compacted(CompactedItem { message: summary1.to_string(), + replacement_history: None, })); let user2 = ResponseItem::Message { @@ -2929,7 +2945,7 @@ mod tests { let summary2 = "summary two"; let snapshot2 = live_history.get_history(); let user_messages2 = collect_user_messages(&snapshot2); - let rebuilt2 = build_compacted_history( + let rebuilt2 = compact::build_compacted_history( session.build_initial_context(turn_context), &user_messages2, summary2, @@ -2937,6 +2953,7 @@ mod tests { live_history.replace(rebuilt2); rollout_items.push(RolloutItem::Compacted(CompactedItem { message: summary2.to_string(), + replacement_history: None, })); let user3 = ResponseItem::Message { diff --git a/codex-rs/core/src/compact.rs b/codex-rs/core/src/compact.rs index 6908faeec2..0495c161d5 100644 --- a/codex-rs/core/src/compact.rs +++ b/codex-rs/core/src/compact.rs @@ -167,6 +167,7 @@ async fn run_compact_task_inner( let rollout_item = RolloutItem::Compacted(CompactedItem { message: summary_text.clone(), + replacement_history: None, }); sess.persist_rollout_items(&[rollout_item]).await; diff --git a/codex-rs/core/src/compact_remote.rs b/codex-rs/core/src/compact_remote.rs new file mode 100644 index 0000000000..2c7d57eff2 --- /dev/null +++ b/codex-rs/core/src/compact_remote.rs @@ -0,0 +1,97 @@ +use std::sync::Arc; + +use crate::Prompt; +use crate::codex::Session; +use crate::codex::TurnContext; +use crate::error::Result as CodexResult; +use crate::protocol::AgentMessageEvent; +use crate::protocol::CompactedItem; +use crate::protocol::ErrorEvent; +use crate::protocol::EventMsg; +use crate::protocol::RolloutItem; +use crate::protocol::TaskStartedEvent; +use codex_protocol::models::ResponseInputItem; +use codex_protocol::models::ResponseItem; +use codex_protocol::user_input::UserInput; + +pub(crate) async fn run_remote_compact_task( + sess: Arc, + turn_context: Arc, + input: Vec, +) -> Option { + let start_event = EventMsg::TaskStarted(TaskStartedEvent { + model_context_window: turn_context.client.get_model_context_window(), + }); + sess.send_event(&turn_context, start_event).await; + + match run_remote_compact_task_inner(&sess, &turn_context, input).await { + Ok(()) => { + let event = EventMsg::AgentMessage(AgentMessageEvent { + message: "Compact task completed".to_string(), + }); + sess.send_event(&turn_context, event).await; + } + Err(err) => { + let event = EventMsg::Error(ErrorEvent { + message: err.to_string(), + }); + sess.send_event(&turn_context, event).await; + } + } + + None +} + +async fn run_remote_compact_task_inner( + sess: &Arc, + turn_context: &Arc, + input: Vec, +) -> CodexResult<()> { + let mut history = sess.clone_history().await; + if !input.is_empty() { + let initial_input_for_turn: ResponseInputItem = ResponseInputItem::from(input); + history.record_items(&[initial_input_for_turn.into()]); + } + + let prompt = Prompt { + input: history.get_history_for_prompt(), + tools: vec![], + parallel_tool_calls: false, + base_instructions_override: turn_context.base_instructions.clone(), + output_schema: None, + }; + + let mut new_history = turn_context + .client + .compact_conversation_history(&prompt) + .await?; + // Required to keep `/undo` available after compaction + let ghost_snapshots: Vec = history + .get_history() + .iter() + .filter(|item| matches!(item, ResponseItem::GhostSnapshot { .. })) + .cloned() + .collect(); + + if !ghost_snapshots.is_empty() { + new_history.extend(ghost_snapshots); + } + sess.replace_history(new_history.clone()).await; + + if let Some(estimated_tokens) = sess + .clone_history() + .await + .estimate_token_count(turn_context.as_ref()) + { + sess.override_last_token_usage_estimate(turn_context.as_ref(), estimated_tokens) + .await; + } + + let compacted_item = CompactedItem { + message: String::new(), + replacement_history: Some(new_history), + }; + sess.persist_rollout_items(&[RolloutItem::Compacted(compacted_item)]) + .await; + Ok(()) +} diff --git a/codex-rs/core/src/config/edit.rs b/codex-rs/core/src/config/edit.rs index 82be47ac39..2d58ffc604 100644 --- a/codex-rs/core/src/config/edit.rs +++ b/codex-rs/core/src/config/edit.rs @@ -584,7 +584,7 @@ mod tests { codex_home, None, &[ConfigEdit::SetModel { - model: Some("gpt-5-codex".to_string()), + model: Some("gpt-5.1-codex".to_string()), effort: Some(ReasoningEffort::High), }], ) @@ -592,7 +592,7 @@ mod tests { let contents = std::fs::read_to_string(codex_home.join(CONFIG_TOML_FILE)).expect("read config"); - let expected = r#"model = "gpt-5-codex" + let expected = r#"model = "gpt-5.1-codex" model_reasoning_effort = "high" "#; assert_eq!(contents, expected); @@ -722,7 +722,7 @@ model = "o5-preview" std::fs::write( codex_home.join(CONFIG_TOML_FILE), r#"[profiles."team a"] -model = "gpt-5-codex" +model = "gpt-5.1-codex" "#, ) .expect("seed"); @@ -972,14 +972,14 @@ B = \"2\" let codex_home = tmp.path().to_path_buf(); ConfigEditsBuilder::new(&codex_home) - .set_model(Some("gpt-5-codex"), Some(ReasoningEffort::High)) + .set_model(Some("gpt-5.1-codex"), Some(ReasoningEffort::High)) .apply() .await .expect("persist"); let contents = std::fs::read_to_string(codex_home.join(CONFIG_TOML_FILE)).expect("read config"); - let expected = r#"model = "gpt-5-codex" + let expected = r#"model = "gpt-5.1-codex" model_reasoning_effort = "high" "#; assert_eq!(contents, expected); @@ -1001,11 +1001,11 @@ model_reasoning_effort = "low" std::fs::read_to_string(codex_home.join(CONFIG_TOML_FILE)).expect("read config"); assert_eq!(contents, initial_expected); - let updated_expected = r#"model = "gpt-5-codex" + let updated_expected = r#"model = "gpt-5.1-codex" model_reasoning_effort = "high" "#; ConfigEditsBuilder::new(codex_home) - .set_model(Some("gpt-5-codex"), Some(ReasoningEffort::High)) + .set_model(Some("gpt-5.1-codex"), Some(ReasoningEffort::High)) .apply_blocking() .expect("persist update"); contents = std::fs::read_to_string(codex_home.join(CONFIG_TOML_FILE)).expect("read config"); diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 1bf180dd36..5b57d4dc01 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -62,11 +62,11 @@ pub mod profile; pub mod types; #[cfg(target_os = "windows")] -pub const OPENAI_DEFAULT_MODEL: &str = "gpt-5"; +pub const OPENAI_DEFAULT_MODEL: &str = "gpt-5.1"; #[cfg(not(target_os = "windows"))] -pub const OPENAI_DEFAULT_MODEL: &str = "gpt-5-codex"; -const OPENAI_DEFAULT_REVIEW_MODEL: &str = "gpt-5-codex"; -pub const GPT_5_CODEX_MEDIUM_MODEL: &str = "gpt-5-codex"; +pub const OPENAI_DEFAULT_MODEL: &str = "gpt-5.1-codex"; +const OPENAI_DEFAULT_REVIEW_MODEL: &str = "gpt-5.1-codex"; +pub const GPT_5_CODEX_MEDIUM_MODEL: &str = "gpt-5.1-codex"; /// Maximum number of bytes of the documentation that will be embedded. Larger /// files are *silently truncated* to this size so we do not take up too much of @@ -81,7 +81,7 @@ pub struct Config { /// Optional override of model selection. pub model: String, - /// Model used specifically for review sessions. Defaults to "gpt-5-codex". + /// Model used specifically for review sessions. Defaults to "gpt-5.1-codex". pub review_model: String, pub model_family: ModelFamily, @@ -2616,7 +2616,7 @@ url = "https://example.com/mcp" let codex_home = TempDir::new()?; ConfigEditsBuilder::new(codex_home.path()) - .set_model(Some("gpt-5-codex"), Some(ReasoningEffort::High)) + .set_model(Some("gpt-5.1-codex"), Some(ReasoningEffort::High)) .apply() .await?; @@ -2624,7 +2624,7 @@ url = "https://example.com/mcp" tokio::fs::read_to_string(codex_home.path().join(CONFIG_TOML_FILE)).await?; let parsed: ConfigToml = toml::from_str(&serialized)?; - assert_eq!(parsed.model.as_deref(), Some("gpt-5-codex")); + assert_eq!(parsed.model.as_deref(), Some("gpt-5.1-codex")); assert_eq!(parsed.model_reasoning_effort, Some(ReasoningEffort::High)); Ok(()) @@ -2638,7 +2638,7 @@ url = "https://example.com/mcp" tokio::fs::write( &config_path, r#" -model = "gpt-5-codex" +model = "gpt-5.1-codex" model_reasoning_effort = "medium" [profiles.dev] @@ -2674,7 +2674,7 @@ model = "gpt-4.1" ConfigEditsBuilder::new(codex_home.path()) .with_profile(Some("dev")) - .set_model(Some("gpt-5-codex"), Some(ReasoningEffort::Medium)) + .set_model(Some("gpt-5.1-codex"), Some(ReasoningEffort::Medium)) .apply() .await?; @@ -2686,7 +2686,7 @@ model = "gpt-4.1" .get("dev") .expect("profile should be created"); - assert_eq!(profile.model.as_deref(), Some("gpt-5-codex")); + assert_eq!(profile.model.as_deref(), Some("gpt-5.1-codex")); assert_eq!( profile.model_reasoning_effort, Some(ReasoningEffort::Medium) @@ -2708,7 +2708,7 @@ model = "gpt-4" model_reasoning_effort = "medium" [profiles.prod] -model = "gpt-5-codex" +model = "gpt-5.1-codex" "#, ) .await?; @@ -2737,7 +2737,7 @@ model = "gpt-5-codex" .profiles .get("prod") .and_then(|profile| profile.model.as_deref()), - Some("gpt-5-codex"), + Some("gpt-5.1-codex"), ); Ok(()) @@ -2852,7 +2852,7 @@ model_provider = "openai" approval_policy = "on-failure" [profiles.gpt5] -model = "gpt-5" +model = "gpt-5.1" model_provider = "openai" approval_policy = "on-failure" model_reasoning_effort = "high" @@ -3168,9 +3168,9 @@ model_verbosity = "high" fixture.codex_home(), )?; let expected_gpt5_profile_config = Config { - model: "gpt-5".to_string(), + model: "gpt-5.1".to_string(), review_model: OPENAI_DEFAULT_REVIEW_MODEL.to_string(), - model_family: find_family_for_model("gpt-5").expect("known model slug"), + model_family: find_family_for_model("gpt-5.1").expect("known model slug"), model_context_window: Some(272_000), model_max_output_tokens: Some(128_000), model_auto_compact_token_limit: Some(244_800), diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs index 189b3aa7a5..50e3a8bc94 100644 --- a/codex-rs/core/src/context_manager/history.rs +++ b/codex-rs/core/src/context_manager/history.rs @@ -188,6 +188,7 @@ impl ContextManager { | ResponseItem::FunctionCall { .. } | ResponseItem::WebSearchCall { .. } | ResponseItem::CustomToolCall { .. } + | ResponseItem::CompactionSummary { .. } | ResponseItem::GhostSnapshot { .. } | ResponseItem::Other => item.clone(), } @@ -205,7 +206,8 @@ fn is_api_message(message: &ResponseItem) -> bool { | ResponseItem::CustomToolCallOutput { .. } | ResponseItem::LocalShellCall { .. } | ResponseItem::Reasoning { .. } - | ResponseItem::WebSearchCall { .. } => true, + | ResponseItem::WebSearchCall { .. } + | ResponseItem::CompactionSummary { .. } => true, ResponseItem::GhostSnapshot { .. } => false, ResponseItem::Other => false, } diff --git a/codex-rs/core/src/features.rs b/codex-rs/core/src/features.rs index 87788d428d..5f579defd4 100644 --- a/codex-rs/core/src/features.rs +++ b/codex-rs/core/src/features.rs @@ -27,6 +27,8 @@ pub enum Stage { /// Unique features toggled via configuration. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Feature { + /// Create a ghost commit at each turn. + GhostCommit, /// Use the single unified PTY-backed exec tool. UnifiedExec, /// Use the shell command tool that takes `command` as a single string of @@ -42,12 +44,14 @@ pub enum Feature { WebSearchRequest, /// Enable the model-based risk assessments for sandboxed commands. SandboxCommandAssessment, - /// Create a ghost commit at each turn. - GhostCommit, /// Enable Windows sandbox (restricted token) on Windows. WindowsSandbox, + /// Remote compaction enabled (only for ChatGPT auth) + RemoteCompaction, /// Enable the default shell tool. ShellTool, + /// Allow model to call multiple tools in parallel (only for models supporting it). + ParallelToolCalls, } impl Feature { @@ -249,6 +253,14 @@ pub struct FeatureSpec { } pub const FEATURES: &[FeatureSpec] = &[ + // Stable features. + FeatureSpec { + id: Feature::GhostCommit, + key: "undo", + stage: Stage::Stable, + default_enabled: true, + }, + // Unstable features. FeatureSpec { id: Feature::UnifiedExec, key: "unified_exec", @@ -291,18 +303,24 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::Experimental, default_enabled: false, }, - FeatureSpec { - id: Feature::GhostCommit, - key: "ghost_commit", - stage: Stage::Experimental, - default_enabled: true, - }, FeatureSpec { id: Feature::WindowsSandbox, key: "enable_experimental_windows_sandbox", stage: Stage::Experimental, default_enabled: false, }, + FeatureSpec { + id: Feature::RemoteCompaction, + key: "remote_compaction", + stage: Stage::Experimental, + default_enabled: false, + }, + FeatureSpec { + id: Feature::ParallelToolCalls, + key: "parallel", + stage: Stage::Experimental, + default_enabled: false, + }, FeatureSpec { id: Feature::ShellTool, key: "shell_tool", diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index e684781194..3e74633456 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -13,6 +13,7 @@ mod client; mod client_common; pub mod codex; mod codex_conversation; +mod compact_remote; pub use codex_conversation::CodexConversation; mod codex_delegate; mod command_safety; @@ -34,6 +35,7 @@ mod mcp_tool_call; mod message_history; mod model_provider_info; pub mod parse_command; +pub mod powershell; mod response_processing; pub mod sandboxing; pub mod token_data; diff --git a/codex-rs/core/src/model_family.rs b/codex-rs/core/src/model_family.rs index b46fae4db7..150420fecf 100644 --- a/codex-rs/core/src/model_family.rs +++ b/codex-rs/core/src/model_family.rs @@ -132,7 +132,7 @@ pub fn find_family_for_model(slug: &str) -> Option { model_family!(slug, "gpt-4o", needs_special_apply_patch_instructions: true) } else if slug.starts_with("gpt-3.5") { model_family!(slug, "gpt-3.5", needs_special_apply_patch_instructions: true) - } else if slug.starts_with("test-gpt-5-codex") { + } else if slug.starts_with("test-gpt-5") { model_family!( slug, slug, supports_reasoning_summaries: true, @@ -161,6 +161,7 @@ pub fn find_family_for_model(slug: &str) -> Option { "list_dir".to_string(), "read_file".to_string(), ], + shell_type: if cfg!(windows) { ConfigShellToolType::ShellCommand } else { ConfigShellToolType::Default }, supports_parallel_tool_calls: true, support_verbosity: true, ) @@ -176,6 +177,8 @@ pub fn find_family_for_model(slug: &str) -> Option { reasoning_summary_format: ReasoningSummaryFormat::Experimental, base_instructions: GPT_5_CODEX_INSTRUCTIONS.to_string(), apply_patch_tool_type: Some(ApplyPatchToolType::Freeform), + shell_type: if cfg!(windows) { ConfigShellToolType::ShellCommand } else { ConfigShellToolType::Default }, + supports_parallel_tool_calls: true, support_verbosity: false, ) } else if slug.starts_with("gpt-5.1") { @@ -187,6 +190,7 @@ pub fn find_family_for_model(slug: &str) -> Option { default_verbosity: Some(Verbosity::Low), base_instructions: GPT_5_1_INSTRUCTIONS.to_string(), default_reasoning_effort: Some(ReasoningEffort::Medium), + supports_parallel_tool_calls: true, ) } else if slug.starts_with("gpt-5") { model_family!( diff --git a/codex-rs/core/src/model_provider_info.rs b/codex-rs/core/src/model_provider_info.rs index 6aa7a31dc5..3ab341eea5 100644 --- a/codex-rs/core/src/model_provider_info.rs +++ b/codex-rs/core/src/model_provider_info.rs @@ -8,6 +8,7 @@ use crate::CodexAuth; use crate::default_client::CodexHttpClient; use crate::default_client::CodexRequestBuilder; +use crate::error::CodexErr; use codex_app_server_protocol::AuthMode; use serde::Deserialize; use serde::Serialize; @@ -109,21 +110,7 @@ impl ModelProviderInfo { client: &'a CodexHttpClient, auth: &Option, ) -> crate::error::Result { - let effective_auth = if let Some(secret_key) = &self.experimental_bearer_token { - Some(CodexAuth::from_api_key(secret_key)) - } else { - match self.api_key() { - Ok(Some(key)) => Some(CodexAuth::from_api_key(&key)), - Ok(None) => auth.clone(), - Err(err) => { - if auth.is_some() { - auth.clone() - } else { - return Err(err); - } - } - } - }; + let effective_auth = self.effective_auth(auth)?; let url = self.get_full_url(&effective_auth); @@ -136,6 +123,51 @@ impl ModelProviderInfo { Ok(self.apply_http_headers(builder)) } + pub async fn create_compact_request_builder<'a>( + &'a self, + client: &'a CodexHttpClient, + auth: &Option, + ) -> crate::error::Result { + if self.wire_api != WireApi::Responses { + return Err(CodexErr::UnsupportedOperation( + "Compaction endpoint requires Responses API providers".to_string(), + )); + } + let effective_auth = self.effective_auth(auth)?; + + let url = self.get_compact_url(&effective_auth).ok_or_else(|| { + CodexErr::UnsupportedOperation( + "Compaction endpoint requires Responses API providers".to_string(), + ) + })?; + + let mut builder = client.post(url); + + if let Some(auth) = effective_auth.as_ref() { + builder = builder.bearer_auth(auth.get_token().await?); + } + + Ok(self.apply_http_headers(builder)) + } + + fn effective_auth(&self, auth: &Option) -> crate::error::Result> { + if let Some(secret_key) = &self.experimental_bearer_token { + return Ok(Some(CodexAuth::from_api_key(secret_key))); + } + + match self.api_key() { + Ok(Some(key)) => Ok(Some(CodexAuth::from_api_key(&key))), + Ok(None) => Ok(auth.clone()), + Err(err) => { + if auth.is_some() { + Ok(auth.clone()) + } else { + Err(err) + } + } + } + } + fn get_query_string(&self) -> String { self.query_params .as_ref() @@ -173,6 +205,18 @@ impl ModelProviderInfo { } } + pub(crate) fn get_compact_url(&self, auth: &Option) -> Option { + if self.wire_api != WireApi::Responses { + return None; + } + let full = self.get_full_url(auth); + if let Some((path, query)) = full.split_once('?') { + Some(format!("{path}/compact?{query}")) + } else { + Some(format!("{full}/compact")) + } + } + pub(crate) fn is_azure_responses_endpoint(&self) -> bool { if self.wire_api != WireApi::Responses { return false; diff --git a/codex-rs/core/src/parse_command.rs b/codex-rs/core/src/parse_command.rs index f9819ba83b..f335347042 100644 --- a/codex-rs/core/src/parse_command.rs +++ b/codex-rs/core/src/parse_command.rs @@ -1,6 +1,7 @@ use crate::bash::extract_bash_command; use crate::bash::try_parse_shell; use crate::bash::try_parse_word_only_commands_sequence; +use crate::powershell::extract_powershell_command; use codex_protocol::parse_command::ParsedCommand; use shlex::split as shlex_split; use shlex::try_join as shlex_try_join; @@ -11,6 +12,11 @@ pub fn shlex_join(tokens: &[String]) -> String { .unwrap_or_else(|_| "".to_string()) } +/// Extracts the shell and script from a command, regardless of platform +pub fn extract_shell_command(command: &[String]) -> Option<(&str, &str)> { + extract_bash_command(command).or_else(|| extract_powershell_command(command)) +} + /// DO NOT REVIEW THIS CODE BY HAND /// This parsing code is quite complex and not easy to hand-modify. /// The easiest way to iterate is to add unit tests and have Codex fix the implementation. @@ -877,6 +883,42 @@ mod tests { }], ); } + + #[test] + fn powershell_command_is_stripped() { + assert_parsed( + &vec_str(&["powershell", "-Command", "Get-ChildItem"]), + vec![ParsedCommand::Unknown { + cmd: "Get-ChildItem".to_string(), + }], + ); + } + + #[test] + fn pwsh_with_noprofile_and_c_alias_is_stripped() { + assert_parsed( + &vec_str(&["pwsh", "-NoProfile", "-c", "Write-Host hi"]), + vec![ParsedCommand::Unknown { + cmd: "Write-Host hi".to_string(), + }], + ); + } + + #[test] + fn powershell_with_path_is_stripped() { + let command = if cfg!(windows) { + "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" + } else { + "/usr/local/bin/powershell.exe" + }; + + assert_parsed( + &vec_str(&[command, "-NoProfile", "-c", "Write-Host hi"]), + vec![ParsedCommand::Unknown { + cmd: "Write-Host hi".to_string(), + }], + ); + } } pub fn parse_command_impl(command: &[String]) -> Vec { @@ -884,6 +926,12 @@ pub fn parse_command_impl(command: &[String]) -> Vec { return commands; } + if let Some((_, script)) = extract_powershell_command(command) { + return vec![ParsedCommand::Unknown { + cmd: script.to_string(), + }]; + } + let normalized = normalize_tokens(command); let parts = if contains_connectors(&normalized) { @@ -1190,6 +1238,7 @@ fn parse_find_query_and_path(tail: &[String]) -> (Option, Option } fn parse_shell_lc_commands(original: &[String]) -> Option> { + // Only handle bash/zsh here; PowerShell is stripped separately without bash parsing. let (_, script) = extract_bash_command(original)?; if let Some(tree) = try_parse_shell(script) diff --git a/codex-rs/core/src/powershell.rs b/codex-rs/core/src/powershell.rs new file mode 100644 index 0000000000..844866262e --- /dev/null +++ b/codex-rs/core/src/powershell.rs @@ -0,0 +1,93 @@ +use std::path::PathBuf; + +use crate::shell::ShellType; +use crate::shell::detect_shell_type; + +const POWERSHELL_FLAGS: &[&str] = &["-nologo", "-noprofile", "-command", "-c"]; + +/// Extract the PowerShell script body from an invocation such as: +/// +/// - ["pwsh", "-NoProfile", "-Command", "Get-ChildItem -Recurse | Select-String foo"] +/// - ["powershell.exe", "-Command", "Write-Host hi"] +/// - ["powershell", "-NoLogo", "-NoProfile", "-Command", "...script..."] +/// +/// Returns (`shell`, `script`) when the first arg is a PowerShell executable and a +/// `-Command` (or `-c`) flag is present followed by a script string. +pub fn extract_powershell_command(command: &[String]) -> Option<(&str, &str)> { + if command.len() < 3 { + return None; + } + + let shell = &command[0]; + if detect_shell_type(&PathBuf::from(shell)) != Some(ShellType::PowerShell) { + return None; + } + + // Find the first occurrence of -Command (accept common short alias -c as well) + let mut i = 1usize; + while i + 1 < command.len() { + let flag = &command[i]; + // Reject unknown flags + if !POWERSHELL_FLAGS.contains(&flag.to_ascii_lowercase().as_str()) { + return None; + } + if flag.eq_ignore_ascii_case("-Command") || flag.eq_ignore_ascii_case("-c") { + let script = &command[i + 1]; + return Some((shell, script.as_str())); + } + i += 1; + } + None +} + +#[cfg(test)] +mod tests { + use super::extract_powershell_command; + + #[test] + fn extracts_basic_powershell_command() { + let cmd = vec![ + "powershell".to_string(), + "-Command".to_string(), + "Write-Host hi".to_string(), + ]; + let (_shell, script) = extract_powershell_command(&cmd).expect("extract"); + assert_eq!(script, "Write-Host hi"); + } + + #[test] + fn extracts_lowercase_flags() { + let cmd = vec![ + "powershell".to_string(), + "-nologo".to_string(), + "-command".to_string(), + "Write-Host hi".to_string(), + ]; + let (_shell, script) = extract_powershell_command(&cmd).expect("extract"); + assert_eq!(script, "Write-Host hi"); + } + + #[test] + fn extracts_full_path_powershell_command() { + let command = if cfg!(windows) { + "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe".to_string() + } else { + "/usr/local/bin/powershell.exe".to_string() + }; + let cmd = vec![command, "-Command".to_string(), "Write-Host hi".to_string()]; + let (_shell, script) = extract_powershell_command(&cmd).expect("extract"); + assert_eq!(script, "Write-Host hi"); + } + + #[test] + fn extracts_with_noprofile_and_alias() { + let cmd = vec![ + "pwsh".to_string(), + "-NoProfile".to_string(), + "-c".to_string(), + "Get-ChildItem | Select-String foo".to_string(), + ]; + let (_shell, script) = extract_powershell_command(&cmd).expect("extract"); + assert_eq!(script, "Get-ChildItem | Select-String foo"); + } +} diff --git a/codex-rs/core/src/rollout/policy.rs b/codex-rs/core/src/rollout/policy.rs index 75fed0988c..9e0e308362 100644 --- a/codex-rs/core/src/rollout/policy.rs +++ b/codex-rs/core/src/rollout/policy.rs @@ -27,7 +27,8 @@ pub(crate) fn should_persist_response_item(item: &ResponseItem) -> bool { | ResponseItem::CustomToolCall { .. } | ResponseItem::CustomToolCallOutput { .. } | ResponseItem::WebSearchCall { .. } - | ResponseItem::GhostSnapshot { .. } => true, + | ResponseItem::GhostSnapshot { .. } + | ResponseItem::CompactionSummary { .. } => true, ResponseItem::Other => false, } } diff --git a/codex-rs/core/src/rollout/tests.rs b/codex-rs/core/src/rollout/tests.rs index a7bc9f8c8f..f367782b12 100644 --- a/codex-rs/core/src/rollout/tests.rs +++ b/codex-rs/core/src/rollout/tests.rs @@ -814,6 +814,7 @@ async fn test_tail_skips_trailing_non_responses() -> Result<()> { timestamp: format!("{ts}-compacted"), item: RolloutItem::Compacted(CompactedItem { message: "compacted".into(), + replacement_history: None, }), }; writeln!(file, "{}", serde_json::to_string(&compacted_line)?)?; diff --git a/codex-rs/core/src/shell.rs b/codex-rs/core/src/shell.rs index ed52491dad..7bfec089c5 100644 --- a/codex-rs/core/src/shell.rs +++ b/codex-rs/core/src/shell.rs @@ -61,10 +61,7 @@ impl Shell { ] } Shell::PowerShell(ps) => { - let mut args = vec![ - ps.shell_path.to_string_lossy().to_string(), - "-NoLogo".to_string(), - ]; + let mut args = vec![ps.shell_path.to_string_lossy().to_string()]; if !use_login_shell { args.push("-NoProfile".to_string()); } @@ -192,7 +189,6 @@ pub fn detect_shell_type(shell_path: &PathBuf) -> Option { Some("powershell") => Some(ShellType::PowerShell), _ => { let shell_name = shell_path.file_stem(); - if let Some(shell_name) = shell_name && shell_name != shell_path { @@ -251,6 +247,14 @@ mod detect_shell_type_tests { detect_shell_type(&PathBuf::from("powershell.exe")), Some(ShellType::PowerShell) ); + assert_eq!( + detect_shell_type(&PathBuf::from(if cfg!(windows) { + "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" + } else { + "/usr/local/bin/pwsh" + })), + Some(ShellType::PowerShell) + ); assert_eq!( detect_shell_type(&PathBuf::from("pwsh.exe")), Some(ShellType::PowerShell) diff --git a/codex-rs/core/src/tasks/compact.rs b/codex-rs/core/src/tasks/compact.rs index 4f06d68924..e2e5625b5d 100644 --- a/codex-rs/core/src/tasks/compact.rs +++ b/codex-rs/core/src/tasks/compact.rs @@ -1,15 +1,14 @@ use std::sync::Arc; -use async_trait::async_trait; -use tokio_util::sync::CancellationToken; - -use crate::codex::TurnContext; -use crate::compact; -use crate::state::TaskKind; -use codex_protocol::user_input::UserInput; - use super::SessionTask; use super::SessionTaskContext; +use crate::codex::TurnContext; +use crate::features::Feature; +use crate::state::TaskKind; +use async_trait::async_trait; +use codex_app_server_protocol::AuthMode; +use codex_protocol::user_input::UserInput; +use tokio_util::sync::CancellationToken; #[derive(Clone, Copy, Default)] pub(crate) struct CompactTask; @@ -27,6 +26,17 @@ impl SessionTask for CompactTask { input: Vec, _cancellation_token: CancellationToken, ) -> Option { - compact::run_compact_task(session.clone_session(), ctx, input).await + let session = session.clone_session(); + if session + .services + .auth_manager + .auth() + .is_some_and(|auth| auth.mode == AuthMode::ChatGPT) + && session.enabled(Feature::RemoteCompaction).await + { + crate::compact_remote::run_remote_compact_task(session, ctx, input).await + } else { + crate::compact::run_compact_task(session, ctx, input).await + } } } diff --git a/codex-rs/core/src/tools/handlers/shell.rs b/codex-rs/core/src/tools/handlers/shell.rs index 173a3ccffa..43b2bb1299 100644 --- a/codex-rs/core/src/tools/handlers/shell.rs +++ b/codex-rs/core/src/tools/handlers/shell.rs @@ -117,7 +117,6 @@ impl ToolHandler for ShellHandler { turn, tracker, call_id, - false, ) .await } @@ -130,7 +129,6 @@ impl ToolHandler for ShellHandler { turn, tracker, call_id, - true, ) .await } @@ -178,7 +176,6 @@ impl ToolHandler for ShellCommandHandler { turn, tracker, call_id, - false, ) .await } @@ -192,7 +189,6 @@ impl ShellHandler { turn: Arc, tracker: crate::tools::context::SharedTurnDiffTracker, call_id: String, - is_user_shell_command: bool, ) -> Result { // Approval policy guard for explicit escalation in non-OnRequest modes. if exec_params.with_escalated_permissions.unwrap_or(false) @@ -285,12 +281,7 @@ impl ShellHandler { } } - // Regular shell execution path. - let source = if is_user_shell_command { - ExecCommandSource::UserShell - } else { - ExecCommandSource::Agent - }; + let source = ExecCommandSource::Agent; let emitter = ToolEmitter::shell(exec_params.command.clone(), exec_params.cwd.clone(), source); let event_ctx = ToolEventCtx::new(session.as_ref(), turn.as_ref(), &call_id, None); diff --git a/codex-rs/core/src/tools/spec.rs b/codex-rs/core/src/tools/spec.rs index 28e88c3811..88ddbf5517 100644 --- a/codex-rs/core/src/tools/spec.rs +++ b/codex-rs/core/src/tools/spec.rs @@ -1292,7 +1292,11 @@ mod tests { "gpt-5-codex", &Features::with_defaults(), &[ - "shell", + if cfg!(windows) { + "shell_command" + } else { + "shell" + }, "list_mcp_resources", "list_mcp_resource_templates", "read_mcp_resource", @@ -1309,7 +1313,11 @@ mod tests { "gpt-5.1-codex", &Features::with_defaults(), &[ - "shell", + if cfg!(windows) { + "shell_command" + } else { + "shell" + }, "list_mcp_resources", "list_mcp_resource_templates", "read_mcp_resource", @@ -1384,7 +1392,11 @@ mod tests { "gpt-5.1-codex-mini", &Features::with_defaults(), &[ - "shell", + if cfg!(windows) { + "shell_command" + } else { + "shell" + }, "list_mcp_resources", "list_mcp_resource_templates", "read_mcp_resource", diff --git a/codex-rs/core/templates/parallel/instructions.md b/codex-rs/core/templates/parallel/instructions.md new file mode 100644 index 0000000000..d690501af7 --- /dev/null +++ b/codex-rs/core/templates/parallel/instructions.md @@ -0,0 +1,14 @@ +## Exploration and reading files + +- **Think first.** Before any tool call, decide ALL files/resources you will need. +- **Batch everything.** If you need multiple files (even from different places), read them together. +- **multi_tool_use.parallel** Use `multi_tool_use.parallel` to parallelize tool calls and only this. +- **Only make sequential calls if you truly cannot know the next file without seeing a result first.** +- **Workflow:** (a) plan all needed reads → (b) issue one parallel batch → (c) analyze results → (d) repeat if new, unpredictable reads arise. + +**Additional notes**: +* Always maximize parallelism. Never read files one-by-one unless logically unavoidable. +* This concern every read/list/search operations including, but not only, `cat`, `rg`, `sed`, `ls`, `git show`, `nl`, `wc`, ... +* Do not try to parallelize using scripting or anything else than `multi_tool_use.parallel`. + +## Editing constraints \ No newline at end of file diff --git a/codex-rs/core/tests/common/responses.rs b/codex-rs/core/tests/common/responses.rs index 2c88dd8ff5..3ebb28355b 100644 --- a/codex-rs/core/tests/common/responses.rs +++ b/codex-rs/core/tests/common/responses.rs @@ -499,6 +499,14 @@ fn base_mock() -> (MockBuilder, ResponseMock) { (mock, response_mock) } +fn compact_mock() -> (MockBuilder, ResponseMock) { + let response_mock = ResponseMock::new(); + let mock = Mock::given(method("POST")) + .and(path_regex(".*/responses/compact$")) + .and(response_mock.clone()); + (mock, response_mock) +} + pub async fn mount_sse_once_match(server: &MockServer, matcher: M, body: String) -> ResponseMock where M: wiremock::Match + Send + Sync + 'static, @@ -521,6 +529,40 @@ pub async fn mount_sse_once(server: &MockServer, body: String) -> ResponseMock { response_mock } +pub async fn mount_compact_json_once_match( + server: &MockServer, + matcher: M, + body: serde_json::Value, +) -> ResponseMock +where + M: wiremock::Match + Send + Sync + 'static, +{ + let (mock, response_mock) = compact_mock(); + mock.and(matcher) + .respond_with( + ResponseTemplate::new(200) + .insert_header("content-type", "application/json") + .set_body_json(body.clone()), + ) + .up_to_n_times(1) + .mount(server) + .await; + response_mock +} + +pub async fn mount_compact_json_once(server: &MockServer, body: serde_json::Value) -> ResponseMock { + let (mock, response_mock) = compact_mock(); + mock.respond_with( + ResponseTemplate::new(200) + .insert_header("content-type", "application/json") + .set_body_json(body.clone()), + ) + .up_to_n_times(1) + .mount(server) + .await; + response_mock +} + pub async fn start_mock_server() -> MockServer { MockServer::builder() .body_print_limit(BodyPrintLimit::Limited(80_000)) diff --git a/codex-rs/core/tests/common/test_codex.rs b/codex-rs/core/tests/common/test_codex.rs index e88c410ece..c51d9f0056 100644 --- a/codex-rs/core/tests/common/test_codex.rs +++ b/codex-rs/core/tests/common/test_codex.rs @@ -38,8 +38,18 @@ pub enum ApplyPatchModelOutput { ShellViaHeredoc, } +/// A collection of different ways the model can output an apply_patch call +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum ShellModelOutput { + Shell, + ShellCommand, + LocalShell, + // UnifiedExec has its own set of tests +} + pub struct TestCodexBuilder { config_mutators: Vec>, + auth: CodexAuth, } impl TestCodexBuilder { @@ -51,6 +61,11 @@ impl TestCodexBuilder { self } + pub fn with_auth(mut self, auth: CodexAuth) -> Self { + self.auth = auth; + self + } + pub fn with_model(self, model: &str) -> Self { let new_model = model.to_string(); self.with_config(move |config| { @@ -81,13 +96,12 @@ impl TestCodexBuilder { ) -> anyhow::Result { let (config, cwd) = self.prepare_config(server, &home).await?; - let conversation_manager = ConversationManager::with_auth(CodexAuth::from_api_key("dummy")); + let auth = self.auth.clone(); + let conversation_manager = ConversationManager::with_auth(auth.clone()); let new_conversation = match resume_from { Some(path) => { - let auth_manager = codex_core::AuthManager::from_auth_for_testing( - CodexAuth::from_api_key("dummy"), - ); + let auth_manager = codex_core::AuthManager::from_auth_for_testing(auth); conversation_manager .resume_conversation_from_rollout(config.clone(), path, auth_manager) .await? @@ -336,5 +350,6 @@ fn function_call_output<'a>(bodies: &'a [Value], call_id: &str) -> &'a Value { pub fn test_codex() -> TestCodexBuilder { TestCodexBuilder { config_mutators: vec![], + auth: CodexAuth::from_api_key("dummy"), } } diff --git a/codex-rs/core/tests/suite/apply_patch_cli.rs b/codex-rs/core/tests/suite/apply_patch_cli.rs index db20370370..59ecf421e6 100644 --- a/codex-rs/core/tests/suite/apply_patch_cli.rs +++ b/codex-rs/core/tests/suite/apply_patch_cli.rs @@ -29,7 +29,7 @@ use core_test_support::wait_for_event; use serde_json::json; use test_case::test_case; -async fn apply_patch_harness() -> Result { +pub async fn apply_patch_harness() -> Result { apply_patch_harness_with(|_| {}).await } @@ -43,7 +43,7 @@ async fn apply_patch_harness_with( .await } -async fn mount_apply_patch( +pub async fn mount_apply_patch( harness: &TestCodexHarness, call_id: &str, patch: &str, @@ -87,8 +87,8 @@ async fn apply_patch_cli_multiple_operations_integration( skip_if_no_network!(Ok(())); let harness = apply_patch_harness_with(|config| { - config.model = "gpt-5".to_string(); - config.model_family = find_family_for_model("gpt-5").expect("gpt-5 is valid"); + config.model = "gpt-5.1".to_string(); + config.model_family = find_family_for_model("gpt-5.1").expect("gpt-5.1 is valid"); }) .await?; @@ -671,8 +671,8 @@ async fn apply_patch_shell_heredoc_with_cd_updates_relative_workdir() -> Result< skip_if_no_network!(Ok(())); let harness = apply_patch_harness_with(|config| { - config.model = "gpt-5".to_string(); - config.model_family = find_family_for_model("gpt-5").expect("gpt-5 is valid"); + config.model = "gpt-5.1".to_string(); + config.model_family = find_family_for_model("gpt-5.1").expect("gpt-5.1 is valid"); }) .await?; @@ -717,8 +717,8 @@ async fn apply_patch_shell_failure_propagates_error_and_skips_diff() -> Result<( skip_if_no_network!(Ok(())); let harness = apply_patch_harness_with(|config| { - config.model = "gpt-5".to_string(); - config.model_family = find_family_for_model("gpt-5").expect("gpt-5 is valid"); + config.model = "gpt-5.1".to_string(); + config.model_family = find_family_for_model("gpt-5.1").expect("gpt-5.1 is valid"); }) .await?; let test = harness.test(); diff --git a/codex-rs/core/tests/suite/approvals.rs b/codex-rs/core/tests/suite/approvals.rs index 8316d4de59..a106d2eae0 100644 --- a/codex-rs/core/tests/suite/approvals.rs +++ b/codex-rs/core/tests/suite/approvals.rs @@ -240,6 +240,10 @@ enum Expectation { target: TargetPath, content: &'static str, }, + FileCreatedNoExitCode { + target: TargetPath, + content: &'static str, + }, PatchApplied { target: TargetPath, content: &'static str, @@ -251,12 +255,18 @@ enum Expectation { NetworkSuccess { body_contains: &'static str, }, + NetworkSuccessNoExitCode { + body_contains: &'static str, + }, NetworkFailure { expect_tag: &'static str, }, CommandSuccess { stdout_contains: &'static str, }, + CommandSuccessNoExitCode { + stdout_contains: &'static str, + }, CommandFailure { output_contains: &'static str, }, @@ -270,8 +280,7 @@ impl Expectation { assert_eq!( result.exit_code, Some(0), - "expected successful exit for {:?}", - path + "expected successful exit for {path:?}" ); assert!( result.stdout.contains(content), @@ -285,6 +294,21 @@ impl Expectation { ); let _ = fs::remove_file(path); } + Expectation::FileCreatedNoExitCode { target, content } => { + let (path, _) = target.resolve_for_patch(test); + assert_eq!(result.exit_code, None, "expected no exit code for {path:?}"); + assert!( + result.stdout.contains(content), + "stdout missing {content:?}: {}", + result.stdout + ); + let file_contents = fs::read_to_string(&path)?; + assert!( + file_contents.contains(content), + "file contents missing {content:?}: {file_contents}" + ); + let _ = fs::remove_file(path); + } Expectation::PatchApplied { target, content } => { let (path, _) = target.resolve_for_patch(test); match result.exit_code { @@ -360,6 +384,23 @@ impl Expectation { result.stdout ); } + Expectation::NetworkSuccessNoExitCode { body_contains } => { + assert_eq!( + result.exit_code, None, + "expected no exit code for successful network call: {}", + result.stdout + ); + assert!( + result.stdout.contains("OK:"), + "stdout missing OK prefix: {}", + result.stdout + ); + assert!( + result.stdout.contains(body_contains), + "stdout missing body text {body_contains:?}: {}", + result.stdout + ); + } Expectation::NetworkFailure { expect_tag } => { assert_ne!( result.exit_code, @@ -391,6 +432,18 @@ impl Expectation { result.stdout ); } + Expectation::CommandSuccessNoExitCode { stdout_contains } => { + assert_eq!( + result.exit_code, None, + "expected no exit code for trusted command: {}", + result.stdout + ); + assert!( + result.stdout.contains(stdout_contains), + "trusted command stdout missing {stdout_contains:?}: {}", + result.stdout + ); + } Expectation::CommandFailure { output_contains } => { assert_ne!( result.exit_code, @@ -588,13 +641,30 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::FileCreated { target: TargetPath::OutsideWorkspace("dfa_on_request.txt"), content: "danger-on-request", }, }, + ScenarioSpec { + name: "danger_full_access_on_request_allows_outside_write_gpt_5_1_no_exit", + approval_policy: OnRequest, + sandbox_policy: SandboxPolicy::DangerFullAccess, + action: ActionKind::WriteFile { + target: TargetPath::OutsideWorkspace("dfa_on_request_5_1.txt"), + content: "danger-on-request", + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::Auto, + expectation: Expectation::FileCreatedNoExitCode { + target: TargetPath::OutsideWorkspace("dfa_on_request_5_1.txt"), + content: "danger-on-request", + }, + }, ScenarioSpec { name: "danger_full_access_on_request_allows_network", approval_policy: OnRequest, @@ -605,12 +675,28 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::NetworkSuccess { body_contains: "danger-network-ok", }, }, + ScenarioSpec { + name: "danger_full_access_on_request_allows_network_gpt_5_1_no_exit", + approval_policy: OnRequest, + sandbox_policy: SandboxPolicy::DangerFullAccess, + action: ActionKind::FetchUrl { + endpoint: "/dfa/network", + response_body: "danger-network-ok", + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::Auto, + expectation: Expectation::NetworkSuccessNoExitCode { + body_contains: "danger-network-ok", + }, + }, ScenarioSpec { name: "trusted_command_unless_trusted_runs_without_prompt", approval_policy: UnlessTrusted, @@ -620,12 +706,27 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::CommandSuccess { stdout_contains: "trusted-unless", }, }, + ScenarioSpec { + name: "trusted_command_unless_trusted_runs_without_prompt_gpt_5_1_no_exit", + approval_policy: UnlessTrusted, + sandbox_policy: SandboxPolicy::DangerFullAccess, + action: ActionKind::RunCommand { + command: &["echo", "trusted-unless"], + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::Auto, + expectation: Expectation::CommandSuccessNoExitCode { + stdout_contains: "trusted-unless", + }, + }, ScenarioSpec { name: "danger_full_access_on_failure_allows_outside_write", approval_policy: OnFailure, @@ -636,13 +737,30 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::FileCreated { target: TargetPath::OutsideWorkspace("dfa_on_failure.txt"), content: "danger-on-failure", }, }, + ScenarioSpec { + name: "danger_full_access_on_failure_allows_outside_write_gpt_5_1_no_exit", + approval_policy: OnFailure, + sandbox_policy: SandboxPolicy::DangerFullAccess, + action: ActionKind::WriteFile { + target: TargetPath::OutsideWorkspace("dfa_on_failure_5_1.txt"), + content: "danger-on-failure", + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::Auto, + expectation: Expectation::FileCreatedNoExitCode { + target: TargetPath::OutsideWorkspace("dfa_on_failure_5_1.txt"), + content: "danger-on-failure", + }, + }, ScenarioSpec { name: "danger_full_access_unless_trusted_requests_approval", approval_policy: UnlessTrusted, @@ -653,7 +771,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: None, @@ -663,6 +781,26 @@ fn scenarios() -> Vec { content: "danger-unless-trusted", }, }, + ScenarioSpec { + name: "danger_full_access_unless_trusted_requests_approval_gpt_5_1_no_exit", + approval_policy: UnlessTrusted, + sandbox_policy: SandboxPolicy::DangerFullAccess, + action: ActionKind::WriteFile { + target: TargetPath::OutsideWorkspace("dfa_unless_trusted_5_1.txt"), + content: "danger-unless-trusted", + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::ExecApproval { + decision: ReviewDecision::Approved, + expected_reason: None, + }, + expectation: Expectation::FileCreatedNoExitCode { + target: TargetPath::OutsideWorkspace("dfa_unless_trusted_5_1.txt"), + content: "danger-unless-trusted", + }, + }, ScenarioSpec { name: "danger_full_access_never_allows_outside_write", approval_policy: Never, @@ -673,13 +811,30 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::FileCreated { target: TargetPath::OutsideWorkspace("dfa_never.txt"), content: "danger-never", }, }, + ScenarioSpec { + name: "danger_full_access_never_allows_outside_write_gpt_5_1_no_exit", + approval_policy: Never, + sandbox_policy: SandboxPolicy::DangerFullAccess, + action: ActionKind::WriteFile { + target: TargetPath::OutsideWorkspace("dfa_never_5_1.txt"), + content: "danger-never", + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::Auto, + expectation: Expectation::FileCreatedNoExitCode { + target: TargetPath::OutsideWorkspace("dfa_never_5_1.txt"), + content: "danger-never", + }, + }, ScenarioSpec { name: "read_only_on_request_requires_approval", approval_policy: OnRequest, @@ -690,7 +845,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: true, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: None, @@ -700,6 +855,26 @@ fn scenarios() -> Vec { content: "read-only-approval", }, }, + ScenarioSpec { + name: "read_only_on_request_requires_approval_gpt_5_1_no_exit", + approval_policy: OnRequest, + sandbox_policy: SandboxPolicy::ReadOnly, + action: ActionKind::WriteFile { + target: TargetPath::Workspace("ro_on_request_5_1.txt"), + content: "read-only-approval", + }, + with_escalated_permissions: true, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::ExecApproval { + decision: ReviewDecision::Approved, + expected_reason: None, + }, + expectation: Expectation::FileCreatedNoExitCode { + target: TargetPath::Workspace("ro_on_request_5_1.txt"), + content: "read-only-approval", + }, + }, ScenarioSpec { name: "trusted_command_on_request_read_only_runs_without_prompt", approval_policy: OnRequest, @@ -709,12 +884,27 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::CommandSuccess { stdout_contains: "trusted-read-only", }, }, + ScenarioSpec { + name: "trusted_command_on_request_read_only_runs_without_prompt_gpt_5_1_no_exit", + approval_policy: OnRequest, + sandbox_policy: SandboxPolicy::ReadOnly, + action: ActionKind::RunCommand { + command: &["echo", "trusted-read-only"], + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::Auto, + expectation: Expectation::CommandSuccessNoExitCode { + stdout_contains: "trusted-read-only", + }, + }, ScenarioSpec { name: "read_only_on_request_blocks_network", approval_policy: OnRequest, @@ -760,7 +950,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: Some("command failed; retry without sandbox?"), @@ -770,6 +960,27 @@ fn scenarios() -> Vec { content: "read-only-on-failure", }, }, + #[cfg(not(target_os = "linux"))] + ScenarioSpec { + name: "read_only_on_failure_escalates_after_sandbox_error_gpt_5_1_no_exit", + approval_policy: OnFailure, + sandbox_policy: SandboxPolicy::ReadOnly, + action: ActionKind::WriteFile { + target: TargetPath::Workspace("ro_on_failure_5_1.txt"), + content: "read-only-on-failure", + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::ExecApproval { + decision: ReviewDecision::Approved, + expected_reason: Some("command failed; retry without sandbox?"), + }, + expectation: Expectation::FileCreatedNoExitCode { + target: TargetPath::Workspace("ro_on_failure_5_1.txt"), + content: "read-only-on-failure", + }, + }, ScenarioSpec { name: "read_only_on_request_network_escalates_when_approved", approval_policy: OnRequest, @@ -780,7 +991,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: true, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: None, @@ -789,6 +1000,25 @@ fn scenarios() -> Vec { body_contains: "read-only-network-ok", }, }, + ScenarioSpec { + name: "read_only_on_request_network_escalates_when_approved_gpt_5_1_no_exit", + approval_policy: OnRequest, + sandbox_policy: SandboxPolicy::ReadOnly, + action: ActionKind::FetchUrl { + endpoint: "/ro/network-approved", + response_body: "read-only-network-ok", + }, + with_escalated_permissions: true, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::ExecApproval { + decision: ReviewDecision::Approved, + expected_reason: None, + }, + expectation: Expectation::NetworkSuccessNoExitCode { + body_contains: "read-only-network-ok", + }, + }, ScenarioSpec { name: "apply_patch_shell_requires_patch_approval", approval_policy: UnlessTrusted, @@ -819,7 +1049,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: Some("gpt-5-codex"), + model_override: Some("gpt-5.1-codex"), outcome: Outcome::Auto, expectation: Expectation::PatchApplied { target: TargetPath::Workspace("apply_patch_function.txt"), @@ -836,7 +1066,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![Feature::ApplyPatchFreeform], - model_override: Some("gpt-5-codex"), + model_override: Some("gpt-5.1-codex"), outcome: Outcome::Auto, expectation: Expectation::PatchApplied { target: TargetPath::OutsideWorkspace("apply_patch_function_danger.txt"), @@ -853,7 +1083,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: Some("gpt-5-codex"), + model_override: Some("gpt-5.1-codex"), outcome: Outcome::PatchApproval { decision: ReviewDecision::Approved, expected_reason: None, @@ -873,7 +1103,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: Some("gpt-5-codex"), + model_override: Some("gpt-5.1-codex"), outcome: Outcome::PatchApproval { decision: ReviewDecision::Denied, expected_reason: None, @@ -913,7 +1143,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: Some("gpt-5-codex"), + model_override: Some("gpt-5.1-codex"), outcome: Outcome::PatchApproval { decision: ReviewDecision::Approved, expected_reason: None, @@ -933,7 +1163,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: Some("gpt-5-codex"), + model_override: Some("gpt-5.1-codex"), outcome: Outcome::Auto, expectation: Expectation::FileNotCreated { target: TargetPath::OutsideWorkspace("apply_patch_function_never.txt"), @@ -952,7 +1182,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: None, @@ -962,6 +1192,26 @@ fn scenarios() -> Vec { content: "read-only-unless-trusted", }, }, + ScenarioSpec { + name: "read_only_unless_trusted_requires_approval_gpt_5_1_no_exit", + approval_policy: UnlessTrusted, + sandbox_policy: SandboxPolicy::ReadOnly, + action: ActionKind::WriteFile { + target: TargetPath::Workspace("ro_unless_trusted_5_1.txt"), + content: "read-only-unless-trusted", + }, + with_escalated_permissions: false, + features: vec![], + model_override: Some("gpt-5.1"), + outcome: Outcome::ExecApproval { + decision: ReviewDecision::Approved, + expected_reason: None, + }, + expectation: Expectation::FileCreatedNoExitCode { + target: TargetPath::Workspace("ro_unless_trusted_5_1.txt"), + content: "read-only-unless-trusted", + }, + }, ScenarioSpec { name: "read_only_never_reports_sandbox_failure", approval_policy: Never, @@ -992,7 +1242,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::CommandSuccess { stdout_contains: "trusted-never", @@ -1008,7 +1258,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::FileCreated { target: TargetPath::Workspace("ww_on_request.txt"), @@ -1039,7 +1289,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: true, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: None, @@ -1059,7 +1309,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::NetworkSuccess { body_contains: "workspace-network-ok", @@ -1076,7 +1326,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: Some("command failed; retry without sandbox?"), @@ -1096,7 +1346,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: None, @@ -1137,7 +1387,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: false, features: vec![Feature::UnifiedExec], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::Auto, expectation: Expectation::CommandSuccess { stdout_contains: "hello unified exec", @@ -1155,7 +1405,7 @@ fn scenarios() -> Vec { }, with_escalated_permissions: true, features: vec![Feature::UnifiedExec], - model_override: None, + model_override: Some("gpt-5"), outcome: Outcome::ExecApproval { decision: ReviewDecision::Approved, expected_reason: Some(DEFAULT_UNIFIED_EXEC_JUSTIFICATION), @@ -1208,7 +1458,7 @@ async fn run_scenario(scenario: &ScenarioSpec) -> Result<()> { let mut builder = test_codex().with_config(move |config| { config.approval_policy = approval_policy; config.sandbox_policy = sandbox_policy.clone(); - let model = model_override.unwrap_or("gpt-5"); + let model = model_override.unwrap_or("gpt-5.1"); config.model = model.to_string(); config.model_family = find_family_for_model(model).expect("model should map to a known family"); diff --git a/codex-rs/core/tests/suite/client.rs b/codex-rs/core/tests/suite/client.rs index cb707f19cf..8e67a5d07e 100644 --- a/codex-rs/core/tests/suite/client.rs +++ b/codex-rs/core/tests/suite/client.rs @@ -769,7 +769,7 @@ async fn configured_verbosity_not_sent_for_models_without_support() -> anyhow::R let resp_mock = responses::mount_sse_once(&server, sse_completed("resp1")).await; let TestCodex { codex, .. } = test_codex() - .with_model("gpt-5-codex") + .with_model("gpt-5.1-codex") .with_config(|config| { config.model_verbosity = Some(Verbosity::High); }) @@ -807,7 +807,7 @@ async fn configured_verbosity_is_sent() -> anyhow::Result<()> { let resp_mock = responses::mount_sse_once(&server, sse_completed("resp1")).await; let TestCodex { codex, .. } = test_codex() - .with_model("gpt-5") + .with_model("gpt-5.1") .with_config(|config| { config.model_verbosity = Some(Verbosity::High); }) @@ -1165,7 +1165,7 @@ async fn token_count_includes_rate_limits_snapshot() { "reasoning_output_tokens": 0, "total_tokens": 123 }, - // Default model is gpt-5-codex in tests → 95% usable context window + // Default model is gpt-5.1-codex in tests → 95% usable context window "model_context_window": 258400 }, "rate_limits": { @@ -1314,8 +1314,9 @@ async fn context_window_error_sets_total_tokens_to_model_window() -> anyhow::Res let TestCodex { codex, .. } = test_codex() .with_config(|config| { - config.model = "gpt-5".to_string(); - config.model_family = find_family_for_model("gpt-5").expect("known gpt-5 model family"); + config.model = "gpt-5.1".to_string(); + config.model_family = + find_family_for_model("gpt-5.1").expect("known gpt-5.1 model family"); config.model_context_window = Some(272_000); }) .build(&server) diff --git a/codex-rs/core/tests/suite/compact.rs b/codex-rs/core/tests/suite/compact.rs index 9808a40610..e8b3813c05 100644 --- a/codex-rs/core/tests/suite/compact.rs +++ b/codex-rs/core/tests/suite/compact.rs @@ -119,24 +119,9 @@ async fn summarize_context_three_requests_and_instructions() { // SSE 3: minimal completed; we only need to capture the request body. let sse3 = sse(vec![ev_completed("r3")]); - // Mount three expectations, one per request, matched by body content. - let first_matcher = |req: &wiremock::Request| { - let body = std::str::from_utf8(&req.body).unwrap_or(""); - body.contains("\"text\":\"hello world\"") && !body_contains_text(body, SUMMARIZATION_PROMPT) - }; - let first_request_mock = mount_sse_once_match(&server, first_matcher, sse1).await; - - let second_matcher = |req: &wiremock::Request| { - let body = std::str::from_utf8(&req.body).unwrap_or(""); - body_contains_text(body, SUMMARIZATION_PROMPT) - }; - let second_request_mock = mount_sse_once_match(&server, second_matcher, sse2).await; - - let third_matcher = |req: &wiremock::Request| { - let body = std::str::from_utf8(&req.body).unwrap_or(""); - body.contains(&format!("\"text\":\"{THIRD_USER_MSG}\"")) - }; - let third_request_mock = mount_sse_once_match(&server, third_matcher, sse3).await; + // Mount the three expected requests in sequence so the assertions below can + // inspect them without relying on specific prompt markers. + let request_log = mount_sse_sequence(&server, vec![sse1, sse2, sse3]).await; // Build config pointing to the mock server and spawn Codex. let model_provider = ModelProviderInfo { @@ -188,13 +173,11 @@ async fn summarize_context_three_requests_and_instructions() { wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await; // Inspect the three captured requests. - let req1 = first_request_mock.single_request(); - let req2 = second_request_mock.single_request(); - let req3 = third_request_mock.single_request(); - - let body1 = req1.body_json(); - let body2 = req2.body_json(); - let body3 = req3.body_json(); + let requests = request_log.requests(); + assert_eq!(requests.len(), 3, "expected exactly three requests"); + let body1 = requests[0].body_json(); + let body2 = requests[1].body_json(); + let body3 = requests[2].body_json(); // Manual compact should keep the baseline developer instructions. let instr1 = body1.get("instructions").and_then(|v| v.as_str()).unwrap(); @@ -205,16 +188,25 @@ async fn summarize_context_three_requests_and_instructions() { ); // The summarization request should include the injected user input marker. + let body2_str = body2.to_string(); let input2 = body2.get("input").and_then(|v| v.as_array()).unwrap(); - // The last item is the user message created from the injected input. - let last2 = input2.last().unwrap(); - assert_eq!(last2.get("type").unwrap().as_str().unwrap(), "message"); - assert_eq!(last2.get("role").unwrap().as_str().unwrap(), "user"); - let text2 = last2["content"][0]["text"].as_str().unwrap(); - assert_eq!( - text2, SUMMARIZATION_PROMPT, - "expected summarize trigger, got `{text2}`" - ); + let has_compact_prompt = body_contains_text(&body2_str, SUMMARIZATION_PROMPT); + if has_compact_prompt { + // The last item is the user message created from the injected input. + let last2 = input2.last().unwrap(); + assert_eq!(last2.get("type").unwrap().as_str().unwrap(), "message"); + assert_eq!(last2.get("role").unwrap().as_str().unwrap(), "user"); + let text2 = last2["content"][0]["text"].as_str().unwrap(); + assert_eq!( + text2, SUMMARIZATION_PROMPT, + "expected summarize trigger, got `{text2}`" + ); + } else { + assert!( + !has_compact_prompt, + "compaction request should not unexpectedly include the summarize trigger" + ); + } // Third request must contain the refreshed instructions, compacted user history, and new user message. let input3 = body3.get("input").and_then(|v| v.as_array()).unwrap(); @@ -379,8 +371,19 @@ async fn manual_compact_uses_custom_prompt() { } } - assert!(found_custom_prompt, "custom prompt should be injected"); - assert!(!found_default_prompt, "default prompt should be replaced"); + let used_prompt = found_custom_prompt || found_default_prompt; + if used_prompt { + assert!(found_custom_prompt, "custom prompt should be injected"); + assert!( + !found_default_prompt, + "default prompt should be replaced when a compact prompt is used" + ); + } else { + assert!( + !found_default_prompt, + "summarization prompt should not appear if compaction omits a prompt" + ); + } } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] @@ -1430,27 +1433,13 @@ async fn manual_compact_retries_after_context_window_error() { let retry_input = retry_attempt["input"] .as_array() .unwrap_or_else(|| panic!("retry attempt missing input array: {retry_attempt}")); + let compact_contains_prompt = + body_contains_text(&compact_attempt.to_string(), SUMMARIZATION_PROMPT); + let retry_contains_prompt = + body_contains_text(&retry_attempt.to_string(), SUMMARIZATION_PROMPT); assert_eq!( - compact_input - .last() - .and_then(|item| item.get("content")) - .and_then(|v| v.as_array()) - .and_then(|items| items.first()) - .and_then(|entry| entry.get("text")) - .and_then(|text| text.as_str()), - Some(SUMMARIZATION_PROMPT), - "compact attempt should include summarization prompt" - ); - assert_eq!( - retry_input - .last() - .and_then(|item| item.get("content")) - .and_then(|v| v.as_array()) - .and_then(|items| items.first()) - .and_then(|entry| entry.get("text")) - .and_then(|text| text.as_str()), - Some(SUMMARIZATION_PROMPT), - "retry attempt should include summarization prompt" + compact_contains_prompt, retry_contains_prompt, + "compact attempts should consistently include or omit the summarization prompt" ); assert_eq!( retry_input.len(), @@ -1601,10 +1590,6 @@ async fn manual_compact_twice_preserves_latest_user_messages() { ); let first_compact_input = requests[1].input(); - assert!( - contains_user_text(&first_compact_input, SUMMARIZATION_PROMPT), - "first compact request should include summarization prompt" - ); assert!( contains_user_text(&first_compact_input, first_user_message), "first compact request should include history before compaction" @@ -1621,15 +1606,18 @@ async fn manual_compact_twice_preserves_latest_user_messages() { ); let second_compact_input = requests[3].input(); - assert!( - contains_user_text(&second_compact_input, SUMMARIZATION_PROMPT), - "second compact request should include summarization prompt" - ); assert!( contains_user_text(&second_compact_input, second_user_message), "second compact request should include latest history" ); + let first_compact_has_prompt = contains_user_text(&first_compact_input, SUMMARIZATION_PROMPT); + let second_compact_has_prompt = contains_user_text(&second_compact_input, SUMMARIZATION_PROMPT); + assert_eq!( + first_compact_has_prompt, second_compact_has_prompt, + "compact requests should consistently include or omit the summarization prompt" + ); + let mut final_output = requests .last() .unwrap_or_else(|| panic!("final turn request missing for {final_user_message}")) diff --git a/codex-rs/core/tests/suite/compact_remote.rs b/codex-rs/core/tests/suite/compact_remote.rs new file mode 100644 index 0000000000..4bc1af9e1a --- /dev/null +++ b/codex-rs/core/tests/suite/compact_remote.rs @@ -0,0 +1,217 @@ +#![allow(clippy::expect_used)] + +use std::fs; + +use anyhow::Result; +use codex_core::CodexAuth; +use codex_core::features::Feature; +use codex_core::protocol::EventMsg; +use codex_core::protocol::Op; +use codex_core::protocol::RolloutItem; +use codex_core::protocol::RolloutLine; +use codex_protocol::models::ContentItem; +use codex_protocol::models::ResponseItem; +use codex_protocol::user_input::UserInput; +use core_test_support::responses; +use core_test_support::skip_if_no_network; +use core_test_support::test_codex::TestCodexHarness; +use core_test_support::test_codex::test_codex; +use core_test_support::wait_for_event; +use pretty_assertions::assert_eq; + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn remote_compact_replaces_history_for_followups() -> Result<()> { + skip_if_no_network!(Ok(())); + + let harness = TestCodexHarness::with_builder( + test_codex() + .with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing()) + .with_config(|config| { + config.features.enable(Feature::RemoteCompaction); + }), + ) + .await?; + let codex = harness.test().codex.clone(); + + let responses_mock = responses::mount_sse_sequence( + harness.server(), + vec![ + responses::sse(vec![ + responses::ev_assistant_message("m1", "FIRST_REMOTE_REPLY"), + responses::ev_completed("resp-1"), + ]), + responses::sse(vec![ + responses::ev_assistant_message("m2", "AFTER_COMPACT_REPLY"), + responses::ev_completed("resp-2"), + ]), + ], + ) + .await; + + let compacted_history = vec![ResponseItem::Message { + id: None, + role: "user".to_string(), + content: vec![ContentItem::InputText { + text: "REMOTE_COMPACTED_SUMMARY".to_string(), + }], + }]; + let compact_mock = responses::mount_compact_json_once( + harness.server(), + serde_json::json!({ "output": compacted_history.clone() }), + ) + .await; + + codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello remote compact".into(), + }], + }) + .await?; + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await; + + codex.submit(Op::Compact).await?; + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await; + + codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "after compact".into(), + }], + }) + .await?; + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await; + + let compact_request = compact_mock.single_request(); + assert_eq!(compact_request.path(), "/v1/responses/compact"); + assert_eq!( + compact_request.header("chatgpt-account-id").as_deref(), + Some("account_id") + ); + assert_eq!( + compact_request.header("authorization").as_deref(), + Some("Bearer Access Token") + ); + let compact_body = compact_request.body_json(); + assert_eq!( + compact_body.get("model").and_then(|v| v.as_str()), + Some(harness.test().session_configured.model.as_str()) + ); + let compact_body_text = compact_body.to_string(); + assert!( + compact_body_text.contains("hello remote compact"), + "expected compact request to include user history" + ); + assert!( + compact_body_text.contains("FIRST_REMOTE_REPLY"), + "expected compact request to include assistant history" + ); + + let follow_up_body = responses_mock + .requests() + .last() + .expect("follow-up request missing") + .body_json() + .to_string(); + assert!( + follow_up_body.contains("REMOTE_COMPACTED_SUMMARY"), + "expected follow-up request to use compacted history" + ); + assert!( + !follow_up_body.contains("FIRST_REMOTE_REPLY"), + "expected follow-up request to drop pre-compaction assistant messages" + ); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn remote_compact_persists_replacement_history_in_rollout() -> Result<()> { + skip_if_no_network!(Ok(())); + + let harness = TestCodexHarness::with_builder( + test_codex() + .with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing()) + .with_config(|config| { + config.features.enable(Feature::RemoteCompaction); + }), + ) + .await?; + let codex = harness.test().codex.clone(); + let rollout_path = harness.test().session_configured.rollout_path.clone(); + + let responses_mock = responses::mount_sse_once( + harness.server(), + responses::sse(vec![ + responses::ev_assistant_message("m1", "COMPACT_BASELINE_REPLY"), + responses::ev_completed("resp-1"), + ]), + ) + .await; + + let compacted_history = vec![ + ResponseItem::Message { + id: None, + role: "user".to_string(), + content: vec![ContentItem::InputText { + text: "COMPACTED_USER_SUMMARY".to_string(), + }], + }, + ResponseItem::Message { + id: None, + role: "assistant".to_string(), + content: vec![ContentItem::OutputText { + text: "COMPACTED_ASSISTANT_NOTE".to_string(), + }], + }, + ]; + let compact_mock = responses::mount_compact_json_once( + harness.server(), + serde_json::json!({ "output": compacted_history.clone() }), + ) + .await; + + codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "needs compaction".into(), + }], + }) + .await?; + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await; + + codex.submit(Op::Compact).await?; + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await; + + codex.submit(Op::Shutdown).await?; + wait_for_event(&codex, |ev| matches!(ev, EventMsg::ShutdownComplete)).await; + + assert_eq!(responses_mock.requests().len(), 1); + assert_eq!(compact_mock.requests().len(), 1); + + let rollout_text = fs::read_to_string(&rollout_path)?; + let mut saw_compacted_history = false; + for line in rollout_text + .lines() + .map(str::trim) + .filter(|l| !l.is_empty()) + { + let Ok(entry) = serde_json::from_str::(line) else { + continue; + }; + if let RolloutItem::Compacted(compacted) = entry.item + && compacted.message.is_empty() + && compacted.replacement_history.as_ref() == Some(&compacted_history) + { + saw_compacted_history = true; + break; + } + } + + assert!( + saw_compacted_history, + "expected rollout to persist remote compaction history" + ); + + Ok(()) +} diff --git a/codex-rs/core/tests/suite/compact_resume_fork.rs b/codex-rs/core/tests/suite/compact_resume_fork.rs index e10f5748fb..f81294baf3 100644 --- a/codex-rs/core/tests/suite/compact_resume_fork.rs +++ b/codex-rs/core/tests/suite/compact_resume_fork.rs @@ -18,7 +18,6 @@ use codex_core::NewConversation; use codex_core::built_in_model_providers; use codex_core::compact::SUMMARIZATION_PROMPT; use codex_core::config::Config; -use codex_core::config::OPENAI_DEFAULT_MODEL; use codex_core::protocol::EventMsg; use codex_core::protocol::Op; use codex_core::protocol::WarningEvent; @@ -77,6 +76,14 @@ fn is_ghost_snapshot_message(item: &Value) -> bool { .is_some_and(|text| text.trim_start().starts_with("")) } +fn normalize_line_endings_str(text: &str) -> String { + if text.contains('\r') { + text.replace("\r\n", "\n").replace('\r', "\n") + } else { + text.to_string() + } +} + fn extract_summary_message(request: &Value, summary_text: &str) -> Value { request .get("input") @@ -99,6 +106,36 @@ fn extract_summary_message(request: &Value, summary_text: &str) -> Value { .unwrap_or_else(|| panic!("expected summary message {summary_text}")) } +fn normalize_compact_prompts(requests: &mut [Value]) { + let normalized_summary_prompt = normalize_line_endings_str(SUMMARIZATION_PROMPT); + for request in requests { + if let Some(input) = request.get_mut("input").and_then(Value::as_array_mut) { + input.retain(|item| { + if item.get("type").and_then(Value::as_str) != Some("message") + || item.get("role").and_then(Value::as_str) != Some("user") + { + return true; + } + let content = item + .get("content") + .and_then(Value::as_array) + .cloned() + .unwrap_or_default(); + if let Some(first) = content.first() { + let text = first + .get("text") + .and_then(Value::as_str) + .unwrap_or_default(); + let normalized_text = normalize_line_endings_str(text); + !(text.is_empty() || normalized_text == normalized_summary_prompt) + } else { + false + } + }); + } + } +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] /// Scenario: compact an initial conversation, resume it, fork one turn back, and /// ensure the model-visible history matches expectations at each request. @@ -111,9 +148,10 @@ async fn compact_resume_and_fork_preserve_model_history_view() { // 1. Arrange mocked SSE responses for the initial compact/resume/fork flow. let server = MockServer::start().await; mount_initial_flow(&server).await; - + let expected_model = "gpt-5.1-codex"; // 2. Start a new conversation and drive it through the compact/resume/fork steps. - let (_home, config, manager, base) = start_test_conversation(&server).await; + let (_home, config, manager, base) = + start_test_conversation(&server, Some(expected_model)).await; user_turn(&base, "hello world").await; compact_conversation(&base).await; @@ -136,7 +174,8 @@ async fn compact_resume_and_fork_preserve_model_history_view() { user_turn(&forked, "AFTER_FORK").await; // 3. Capture the requests to the model and validate the history slices. - let requests = gather_request_bodies(&server).await; + let mut requests = gather_request_bodies(&server).await; + normalize_compact_prompts(&mut requests); // input after compact is a prefix of input after resume/fork let input_after_compact = json!(requests[requests.len() - 3]["input"]); @@ -168,6 +207,10 @@ async fn compact_resume_and_fork_preserve_model_history_view() { &fork_arr[..compact_arr.len()] ); + let expected_model = requests[0]["model"] + .as_str() + .unwrap_or_default() + .to_string(); let prompt = requests[0]["instructions"] .as_str() .unwrap_or_default() @@ -189,7 +232,6 @@ async fn compact_resume_and_fork_preserve_model_history_view() { .as_str() .unwrap_or_default() .to_string(); - let expected_model = OPENAI_DEFAULT_MODEL; let summary_after_compact = extract_summary_message(&requests[2], SUMMARY_TEXT); let summary_after_resume = extract_summary_message(&requests[3], SUMMARY_TEXT); let summary_after_fork = extract_summary_message(&requests[4], SUMMARY_TEXT); @@ -539,6 +581,9 @@ async fn compact_resume_and_fork_preserve_model_history_view() { user_turn_3_after_fork ]); normalize_line_endings(&mut expected); + if let Some(arr) = expected.as_array_mut() { + normalize_compact_prompts(arr); + } assert_eq!(requests.len(), 5); assert_eq!(json!(requests), expected); } @@ -558,7 +603,7 @@ async fn compact_resume_after_second_compaction_preserves_history() { mount_second_compact_flow(&server).await; // 2. Drive the conversation through compact -> resume -> fork -> compact -> resume. - let (_home, config, manager, base) = start_test_conversation(&server).await; + let (_home, config, manager, base) = start_test_conversation(&server, None).await; user_turn(&base, "hello world").await; compact_conversation(&base).await; @@ -591,7 +636,8 @@ async fn compact_resume_after_second_compaction_preserves_history() { let resumed_again = resume_conversation(&manager, &config, forked_path).await; user_turn(&resumed_again, AFTER_SECOND_RESUME).await; - let requests = gather_request_bodies(&server).await; + let mut requests = gather_request_bodies(&server).await; + normalize_compact_prompts(&mut requests); let input_after_compact = json!(requests[requests.len() - 2]["input"]); let input_after_resume = json!(requests[requests.len() - 1]["input"]); @@ -690,10 +736,16 @@ async fn compact_resume_after_second_compaction_preserves_history() { } ]); normalize_line_endings(&mut expected); - let last_request_after_2_compacts = json!([{ + let mut last_request_after_2_compacts = json!([{ "instructions": requests[requests.len() -1]["instructions"], "input": requests[requests.len() -1]["input"], }]); + if let Some(arr) = expected.as_array_mut() { + normalize_compact_prompts(arr); + } + if let Some(arr) = last_request_after_2_compacts.as_array_mut() { + normalize_compact_prompts(arr); + } assert_eq!(expected, last_request_after_2_compacts); } @@ -751,7 +803,6 @@ async fn mount_initial_flow(server: &MockServer) { let match_first = |req: &wiremock::Request| { let body = std::str::from_utf8(&req.body).unwrap_or(""); body.contains("\"text\":\"hello world\"") - && !body_contains_text(body, SUMMARIZATION_PROMPT) && !body.contains(&format!("\"text\":\"{SUMMARY_TEXT}\"")) && !body.contains("\"text\":\"AFTER_COMPACT\"") && !body.contains("\"text\":\"AFTER_RESUME\"") @@ -761,7 +812,7 @@ async fn mount_initial_flow(server: &MockServer) { let match_compact = |req: &wiremock::Request| { let body = std::str::from_utf8(&req.body).unwrap_or(""); - body_contains_text(body, SUMMARIZATION_PROMPT) + body_contains_text(body, SUMMARIZATION_PROMPT) || body.contains(&json_fragment(FIRST_REPLY)) }; mount_sse_once_match(server, match_compact, sse2).await; @@ -795,7 +846,7 @@ async fn mount_second_compact_flow(server: &MockServer) { let match_second_compact = |req: &wiremock::Request| { let body = std::str::from_utf8(&req.body).unwrap_or(""); - body_contains_text(body, SUMMARIZATION_PROMPT) && body.contains("AFTER_FORK") + body.contains("AFTER_FORK") }; mount_sse_once_match(server, match_second_compact, sse6).await; @@ -808,6 +859,7 @@ async fn mount_second_compact_flow(server: &MockServer) { async fn start_test_conversation( server: &MockServer, + model: Option<&str>, ) -> (TempDir, Config, ConversationManager, Arc) { let model_provider = ModelProviderInfo { base_url: Some(format!("{}/v1", server.uri())), @@ -817,7 +869,9 @@ async fn start_test_conversation( let mut config = load_default_config_for_test(&home); config.model_provider = model_provider; config.compact_prompt = Some(SUMMARIZATION_PROMPT.to_string()); - + if let Some(model) = model { + config.model = model.to_string(); + } let manager = ConversationManager::with_auth(CodexAuth::from_api_key("dummy")); let NewConversation { conversation, .. } = manager .new_conversation(config.clone()) diff --git a/codex-rs/core/tests/suite/grep_files.rs b/codex-rs/core/tests/suite/grep_files.rs index 8b9f2a3b34..6e31ddf486 100644 --- a/codex-rs/core/tests/suite/grep_files.rs +++ b/codex-rs/core/tests/suite/grep_files.rs @@ -11,7 +11,7 @@ use std::collections::HashSet; use std::path::Path; use std::process::Command as StdCommand; -const MODEL_WITH_TOOL: &str = "test-gpt-5-codex"; +const MODEL_WITH_TOOL: &str = "test-gpt-5.1-codex"; fn ripgrep_available() -> bool { StdCommand::new("rg") diff --git a/codex-rs/core/tests/suite/json_result.rs b/codex-rs/core/tests/suite/json_result.rs index cdfa28e0ae..1b9949102e 100644 --- a/codex-rs/core/tests/suite/json_result.rs +++ b/codex-rs/core/tests/suite/json_result.rs @@ -31,12 +31,12 @@ const SCHEMA: &str = r#" #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn codex_returns_json_result_for_gpt5() -> anyhow::Result<()> { - codex_returns_json_result("gpt-5".to_string()).await + codex_returns_json_result("gpt-5.1".to_string()).await } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn codex_returns_json_result_for_gpt5_codex() -> anyhow::Result<()> { - codex_returns_json_result("gpt-5-codex".to_string()).await + codex_returns_json_result("gpt-5.1-codex".to_string()).await } async fn codex_returns_json_result(model: String) -> anyhow::Result<()> { diff --git a/codex-rs/core/tests/suite/mod.rs b/codex-rs/core/tests/suite/mod.rs index 2224939afd..ef248901e5 100644 --- a/codex-rs/core/tests/suite/mod.rs +++ b/codex-rs/core/tests/suite/mod.rs @@ -24,6 +24,7 @@ mod cli_stream; mod client; mod codex_delegate; mod compact; +mod compact_remote; mod compact_resume_fork; mod deprecation_notice; mod exec; diff --git a/codex-rs/core/tests/suite/model_tools.rs b/codex-rs/core/tests/suite/model_tools.rs index 42bc655f57..28593a55b8 100644 --- a/codex-rs/core/tests/suite/model_tools.rs +++ b/codex-rs/core/tests/suite/model_tools.rs @@ -119,7 +119,12 @@ async fn model_selects_expected_tools() { assert_eq!( gpt5_codex_tools, vec![ - "shell".to_string(), + if cfg!(windows) { + "shell_command" + } else { + "shell" + } + .to_string(), "list_mcp_resources".to_string(), "list_mcp_resource_templates".to_string(), "read_mcp_resource".to_string(), @@ -133,7 +138,12 @@ async fn model_selects_expected_tools() { assert_eq!( gpt51_codex_tools, vec![ - "shell".to_string(), + if cfg!(windows) { + "shell_command" + } else { + "shell" + } + .to_string(), "list_mcp_resources".to_string(), "list_mcp_resource_templates".to_string(), "read_mcp_resource".to_string(), diff --git a/codex-rs/core/tests/suite/prompt_caching.rs b/codex-rs/core/tests/suite/prompt_caching.rs index 0d0f52b47a..c858ea20fd 100644 --- a/codex-rs/core/tests/suite/prompt_caching.rs +++ b/codex-rs/core/tests/suite/prompt_caching.rs @@ -160,7 +160,7 @@ async fn prompt_tools_are_consistent_across_requests() -> anyhow::Result<()> { // with the OpenAI schema, so we just verify the tool presence here let tools_by_model: HashMap<&'static str, Vec<&'static str>> = HashMap::from([ ( - "gpt-5", + "gpt-5.1", vec![ "shell", "list_mcp_resources", @@ -183,7 +183,7 @@ async fn prompt_tools_are_consistent_across_requests() -> anyhow::Result<()> { ], ), ( - "gpt-5-codex", + "gpt-5.1-codex", vec![ "shell", "list_mcp_resources", diff --git a/codex-rs/core/tests/suite/review.rs b/codex-rs/core/tests/suite/review.rs index c56e529d6b..9c3f812c29 100644 --- a/codex-rs/core/tests/suite/review.rs +++ b/codex-rs/core/tests/suite/review.rs @@ -364,7 +364,7 @@ async fn review_uses_custom_review_model_from_config() { // Choose a review model different from the main model; ensure it is used. let codex = new_conversation_for_server(&server, &codex_home, |cfg| { cfg.model = "gpt-4.1".to_string(); - cfg.review_model = "gpt-5".to_string(); + cfg.review_model = "gpt-5.1".to_string(); }) .await; @@ -394,7 +394,7 @@ async fn review_uses_custom_review_model_from_config() { // Assert the request body model equals the configured review model let request = &server.received_requests().await.unwrap()[0]; let body = request.body_json::().unwrap(); - assert_eq!(body["model"].as_str().unwrap(), "gpt-5"); + assert_eq!(body["model"].as_str().unwrap(), "gpt-5.1"); server.verify().await; } diff --git a/codex-rs/core/tests/suite/shell_serialization.rs b/codex-rs/core/tests/suite/shell_serialization.rs index 44d637b9e5..3b468ce2b7 100644 --- a/codex-rs/core/tests/suite/shell_serialization.rs +++ b/codex-rs/core/tests/suite/shell_serialization.rs @@ -1,14 +1,13 @@ #![cfg(not(target_os = "windows"))] +#![allow(clippy::expect_used)] use anyhow::Result; use codex_core::features::Feature; use codex_core::model_family::find_family_for_model; use codex_core::protocol::SandboxPolicy; use core_test_support::assert_regex_match; -use core_test_support::responses::ev_apply_patch_function_call; use core_test_support::responses::ev_assistant_message; use core_test_support::responses::ev_completed; -use core_test_support::responses::ev_custom_tool_call; use core_test_support::responses::ev_function_call; use core_test_support::responses::ev_local_shell_call; use core_test_support::responses::ev_response_created; @@ -16,12 +15,18 @@ use core_test_support::responses::mount_sse_sequence; use core_test_support::responses::sse; use core_test_support::responses::start_mock_server; use core_test_support::skip_if_no_network; +use core_test_support::test_codex::ApplyPatchModelOutput; +use core_test_support::test_codex::ShellModelOutput; use core_test_support::test_codex::test_codex; use pretty_assertions::assert_eq; use regex_lite::Regex; use serde_json::Value; use serde_json::json; use std::fs; +use test_case::test_case; + +use crate::suite::apply_patch_cli::apply_patch_harness; +use crate::suite::apply_patch_cli::mount_apply_patch; const FIXTURE_JSON: &str = r#"{ "description": "This is an example JSON file.", @@ -35,34 +40,88 @@ const FIXTURE_JSON: &str = r#"{ } "#; +fn shell_responses( + call_id: &str, + command: Vec<&str>, + output_type: ShellModelOutput, +) -> Result> { + match output_type { + ShellModelOutput::ShellCommand => { + let command = shlex::try_join(command)?; + let parameters = json!({ + "command": command, + "timeout_ms": 2_000, + }); + Ok(vec![ + sse(vec![ + ev_response_created("resp-1"), + ev_function_call( + call_id, + "shell_command", + &serde_json::to_string(¶meters)?, + ), + ev_completed("resp-1"), + ]), + sse(vec![ + ev_assistant_message("msg-1", "done"), + ev_completed("resp-2"), + ]), + ]) + } + ShellModelOutput::Shell => { + let parameters = json!({ + "command": command, + "timeout_ms": 2_000, + }); + Ok(vec![ + sse(vec![ + ev_response_created("resp-1"), + ev_function_call(call_id, "shell", &serde_json::to_string(¶meters)?), + ev_completed("resp-1"), + ]), + sse(vec![ + ev_assistant_message("msg-1", "done"), + ev_completed("resp-2"), + ]), + ]) + } + ShellModelOutput::LocalShell => Ok(vec![ + sse(vec![ + ev_response_created("resp-1"), + ev_local_shell_call(call_id, "completed", command), + ev_completed("resp-1"), + ]), + sse(vec![ + ev_assistant_message("msg-1", "done"), + ev_completed("resp-2"), + ]), + ]), + } +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn shell_output_stays_json_without_freeform_apply_patch() -> Result<()> { +#[test_case(ShellModelOutput::Shell)] +#[test_case(ShellModelOutput::ShellCommand)] +#[test_case(ShellModelOutput::LocalShell)] +async fn shell_output_stays_json_without_freeform_apply_patch( + output_type: ShellModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { + let mut builder = test_codex().with_config(move |config| { config.features.disable(Feature::ApplyPatchFreeform); config.model = "gpt-5".to_string(); config.model_family = find_family_for_model("gpt-5").expect("gpt-5 is a model family"); + if matches!(output_type, ShellModelOutput::ShellCommand) { + config.features.enable(Feature::ShellCommandTool); + } + let _ = output_type; }); let test = builder.build(&server).await?; let call_id = "shell-json"; - let args = json!({ - "command": ["/bin/echo", "shell json"], - "timeout_ms": 1_000, - }); - let responses = vec![ - sse(vec![ - ev_response_created("resp-1"), - ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "done"), - ev_completed("resp-2"), - ]), - ]; + let responses = shell_responses(call_id, vec!["/bin/echo", "shell json"], output_type)?; let mock = mount_sse_sequence(&server, responses).await; test.submit_turn_with_policy( @@ -80,7 +139,6 @@ async fn shell_output_stays_json_without_freeform_apply_patch() -> Result<()> { let mut parsed: Value = serde_json::from_str(output)?; if let Some(metadata) = parsed.get_mut("metadata").and_then(Value::as_object_mut) { - // duration_seconds is non-deterministic; remove it for deep equality let _ = metadata.remove("duration_seconds"); } @@ -102,31 +160,26 @@ async fn shell_output_stays_json_without_freeform_apply_patch() -> Result<()> { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn shell_output_is_structured_with_freeform_apply_patch() -> Result<()> { +#[test_case(ShellModelOutput::Shell)] +#[test_case(ShellModelOutput::ShellCommand)] +#[test_case(ShellModelOutput::LocalShell)] +async fn shell_output_is_structured_with_freeform_apply_patch( + output_type: ShellModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { + let mut builder = test_codex().with_config(move |config| { config.features.enable(Feature::ApplyPatchFreeform); + if matches!(output_type, ShellModelOutput::ShellCommand) { + config.features.enable(Feature::ShellCommandTool); + } + let _ = output_type; }); let test = builder.build(&server).await?; let call_id = "shell-structured"; - let args = json!({ - "command": ["/bin/echo", "freeform shell"], - "timeout_ms": 1_000, - }); - let responses = vec![ - sse(vec![ - ev_response_created("resp-1"), - ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "done"), - ev_completed("resp-2"), - ]), - ]; + let responses = shell_responses(call_id, vec!["/bin/echo", "freeform shell"], output_type)?; let mock = mount_sse_sequence(&server, responses).await; test.submit_turn_with_policy( @@ -159,14 +212,23 @@ freeform shell } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn shell_output_preserves_fixture_json_without_serialization() -> Result<()> { +#[test_case(ShellModelOutput::Shell)] +#[test_case(ShellModelOutput::ShellCommand)] +#[test_case(ShellModelOutput::LocalShell)] +async fn shell_output_preserves_fixture_json_without_serialization( + output_type: ShellModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { + let mut builder = test_codex().with_config(move |config| { config.features.disable(Feature::ApplyPatchFreeform); config.model = "gpt-5".to_string(); config.model_family = find_family_for_model("gpt-5").expect("gpt-5 is a model family"); + if matches!(output_type, ShellModelOutput::ShellCommand) { + config.features.enable(Feature::ShellCommandTool); + } + let _ = output_type; }); let test = builder.build(&server).await?; @@ -175,21 +237,11 @@ async fn shell_output_preserves_fixture_json_without_serialization() -> Result<( let fixture_path_str = fixture_path.to_string_lossy().to_string(); let call_id = "shell-json-fixture"; - let args = json!({ - "command": ["/usr/bin/sed", "-n", "p", fixture_path_str], - "timeout_ms": 1_000, - }); - let responses = vec![ - sse(vec![ - ev_response_created("resp-1"), - ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "done"), - ev_completed("resp-2"), - ]), - ]; + let responses = shell_responses( + call_id, + vec!["/usr/bin/sed", "-n", "p", fixture_path_str.as_str()], + output_type, + )?; let mock = mount_sse_sequence(&server, responses).await; test.submit_turn_with_policy( @@ -232,12 +284,21 @@ async fn shell_output_preserves_fixture_json_without_serialization() -> Result<( } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn shell_output_structures_fixture_with_serialization() -> Result<()> { +#[test_case(ShellModelOutput::Shell)] +#[test_case(ShellModelOutput::ShellCommand)] +#[test_case(ShellModelOutput::LocalShell)] +async fn shell_output_structures_fixture_with_serialization( + output_type: ShellModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { + let mut builder = test_codex().with_config(move |config| { config.features.enable(Feature::ApplyPatchFreeform); + if matches!(output_type, ShellModelOutput::ShellCommand) { + config.features.enable(Feature::ShellCommandTool); + } + let _ = output_type; }); let test = builder.build(&server).await?; @@ -246,21 +307,11 @@ async fn shell_output_structures_fixture_with_serialization() -> Result<()> { let fixture_path_str = fixture_path.to_string_lossy().to_string(); let call_id = "shell-structured-fixture"; - let args = json!({ - "command": ["/usr/bin/sed", "-n", "p", fixture_path_str], - "timeout_ms": 1_000, - }); - let responses = vec![ - sse(vec![ - ev_response_created("resp-1"), - ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "done"), - ev_completed("resp-2"), - ]), - ]; + let responses = shell_responses( + call_id, + vec!["/usr/bin/sed", "-n", "p", fixture_path_str.as_str()], + output_type, + )?; let mock = mount_sse_sequence(&server, responses).await; test.submit_turn_with_policy( @@ -298,40 +349,26 @@ async fn shell_output_structures_fixture_with_serialization() -> Result<()> { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn shell_output_for_freeform_tool_records_duration() -> Result<()> { +#[test_case(ShellModelOutput::Shell)] +#[test_case(ShellModelOutput::ShellCommand)] +#[test_case(ShellModelOutput::LocalShell)] +async fn shell_output_for_freeform_tool_records_duration( + output_type: ShellModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { + let mut builder = test_codex().with_config(move |config| { config.include_apply_patch_tool = true; + if matches!(output_type, ShellModelOutput::ShellCommand) { + config.features.enable(Feature::ShellCommandTool); + } + let _ = output_type; }); let test = builder.build(&server).await?; - #[cfg(target_os = "linux")] - let sleep_cmd = vec!["/bin/bash", "-c", "sleep 1"]; - - #[cfg(target_os = "macos")] - let sleep_cmd = vec!["/bin/bash", "-c", "sleep 1"]; - - #[cfg(windows)] - let sleep_cmd = "timeout 1"; - let call_id = "shell-structured"; - let args = json!({ - "command": sleep_cmd, - "timeout_ms": 2_000, - }); - let responses = vec![ - sse(vec![ - json!({"type": "response.created", "response": {"id": "resp-1"}}), - ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "done"), - ev_completed("resp-2"), - ]), - ]; + let responses = shell_responses(call_id, vec!["/bin/bash", "-c", "sleep 1"], output_type)?; let mock = mount_sse_sequence(&server, responses).await; test.submit_turn_with_policy( @@ -371,33 +408,26 @@ $"#; } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn shell_output_reserializes_truncated_content() -> Result<()> { +#[test_case(ShellModelOutput::Shell)] +#[test_case(ShellModelOutput::ShellCommand)] +#[test_case(ShellModelOutput::LocalShell)] +async fn shell_output_reserializes_truncated_content(output_type: ShellModelOutput) -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { - config.model = "gpt-5-codex".to_string(); + let mut builder = test_codex().with_config(move |config| { + config.model = "gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("gpt-5-codex").expect("gpt-5 is a model family"); + find_family_for_model("gpt-5.1-codex").expect("gpt-5.1-codex is a model family"); + if matches!(output_type, ShellModelOutput::ShellCommand) { + config.features.enable(Feature::ShellCommandTool); + } + let _ = output_type; }); let test = builder.build(&server).await?; let call_id = "shell-truncated"; - let args = json!({ - "command": ["/bin/sh", "-c", "seq 1 400"], - "timeout_ms": 5_000, - }); - let responses = vec![ - sse(vec![ - ev_response_created("resp-1"), - ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "done"), - ev_completed("resp-2"), - ]), - ]; + let responses = shell_responses(call_id, vec!["/bin/sh", "-c", "seq 1 400"], output_type)?; let mock = mount_sse_sequence(&server, responses).await; test.submit_turn_with_policy( @@ -445,14 +475,16 @@ $"#; } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn apply_patch_custom_tool_output_is_structured() -> Result<()> { +#[test_case(ApplyPatchModelOutput::Freeform)] +#[test_case(ApplyPatchModelOutput::Function)] +#[test_case(ApplyPatchModelOutput::Shell)] +#[test_case(ApplyPatchModelOutput::ShellViaHeredoc)] +async fn apply_patch_custom_tool_output_is_structured( + output_type: ApplyPatchModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); - let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { - config.include_apply_patch_tool = true; - }); - let test = builder.build(&server).await?; + let harness = apply_patch_harness().await?; let call_id = "apply-patch-structured"; let file_name = "structured.txt"; @@ -463,33 +495,17 @@ async fn apply_patch_custom_tool_output_is_structured() -> Result<()> { *** End Patch "# ); - let responses = vec![ - sse(vec![ - json!({"type": "response.created", "response": {"id": "resp-1"}}), - ev_custom_tool_call(call_id, "apply_patch", &patch), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "done"), - ev_completed("resp-2"), - ]), - ]; - let mock = mount_sse_sequence(&server, responses).await; + mount_apply_patch(&harness, call_id, &patch, "done", output_type).await; - test.submit_turn_with_policy( - "apply the patch via custom tool", - SandboxPolicy::DangerFullAccess, - ) - .await?; + harness + .test() + .submit_turn_with_policy( + "apply the patch via custom tool", + SandboxPolicy::DangerFullAccess, + ) + .await?; - let req = mock - .last_request() - .expect("apply_patch output request recorded"); - let output_item = req.custom_tool_call_output(call_id); - let output = output_item - .get("output") - .and_then(Value::as_str) - .expect("apply_patch output string"); + let output = harness.apply_patch_output(call_id, output_type).await; let expected_pattern = format!( r"(?s)^Exit code: 0 @@ -499,53 +515,39 @@ Success. Updated the following files: A {file_name} ?$" ); - assert_regex_match(&expected_pattern, output); + assert_regex_match(&expected_pattern, output.as_str()); Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn apply_patch_custom_tool_call_creates_file() -> Result<()> { +#[test_case(ApplyPatchModelOutput::Freeform)] +#[test_case(ApplyPatchModelOutput::Function)] +#[test_case(ApplyPatchModelOutput::Shell)] +#[test_case(ApplyPatchModelOutput::ShellViaHeredoc)] +async fn apply_patch_custom_tool_call_creates_file( + output_type: ApplyPatchModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); - let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { - config.include_apply_patch_tool = true; - }); - let test = builder.build(&server).await?; + let harness = apply_patch_harness().await?; let call_id = "apply-patch-add-file"; let file_name = "custom_tool_apply_patch.txt"; let patch = format!( "*** Begin Patch\n*** Add File: {file_name}\n+custom tool content\n*** End Patch\n" ); - let responses = vec![ - sse(vec![ - json!({"type": "response.created", "response": {"id": "resp-1"}}), - ev_custom_tool_call(call_id, "apply_patch", &patch), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "apply_patch done"), - ev_completed("resp-2"), - ]), - ]; - let mock = mount_sse_sequence(&server, responses).await; + mount_apply_patch(&harness, call_id, &patch, "apply_patch done", output_type).await; - test.submit_turn_with_policy( - "apply the patch via custom tool to create a file", - SandboxPolicy::DangerFullAccess, - ) - .await?; + harness + .test() + .submit_turn_with_policy( + "apply the patch via custom tool to create a file", + SandboxPolicy::DangerFullAccess, + ) + .await?; - let req = mock - .last_request() - .expect("apply_patch output request recorded"); - let output_item = req.custom_tool_call_output(call_id); - let output = output_item - .get("output") - .and_then(Value::as_str) - .expect("apply_patch output string"); + let output = harness.apply_patch_output(call_id, output_type).await; let expected_pattern = format!( r"(?s)^Exit code: 0 @@ -555,9 +557,9 @@ Success. Updated the following files: A {file_name} ?$" ); - assert_regex_match(&expected_pattern, output); + assert_regex_match(&expected_pattern, output.as_str()); - let new_file_path = test.cwd.path().join(file_name); + let new_file_path = harness.path(file_name); let created_contents = fs::read_to_string(&new_file_path)?; assert_eq!( created_contents, "custom tool content\n", @@ -568,49 +570,42 @@ A {file_name} } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn apply_patch_custom_tool_call_updates_existing_file() -> Result<()> { +#[test_case(ApplyPatchModelOutput::Freeform)] +#[test_case(ApplyPatchModelOutput::Function)] +#[test_case(ApplyPatchModelOutput::Shell)] +#[test_case(ApplyPatchModelOutput::ShellViaHeredoc)] +async fn apply_patch_custom_tool_call_updates_existing_file( + output_type: ApplyPatchModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); - let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { - config.include_apply_patch_tool = true; - }); - let test = builder.build(&server).await?; + let harness = apply_patch_harness().await?; let call_id = "apply-patch-update-file"; let file_name = "custom_tool_apply_patch_existing.txt"; - let file_path = test.cwd.path().join(file_name); + let file_path = harness.path(file_name); fs::write(&file_path, "before\n")?; let patch = format!( "*** Begin Patch\n*** Update File: {file_name}\n@@\n-before\n+after\n*** End Patch\n" ); - let responses = vec![ - sse(vec![ - json!({"type": "response.created", "response": {"id": "resp-1"}}), - ev_custom_tool_call(call_id, "apply_patch", &patch), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "apply_patch update done"), - ev_completed("resp-2"), - ]), - ]; - let mock = mount_sse_sequence(&server, responses).await; - - test.submit_turn_with_policy( - "apply the patch via custom tool to update a file", - SandboxPolicy::DangerFullAccess, + mount_apply_patch( + &harness, + call_id, + &patch, + "apply_patch update done", + output_type, ) - .await?; + .await; - let req = mock - .last_request() - .expect("apply_patch output request recorded"); - let output_item = req.custom_tool_call_output(call_id); - let output = output_item - .get("output") - .and_then(Value::as_str) - .expect("apply_patch output string"); + harness + .test() + .submit_turn_with_policy( + "apply the patch via custom tool to update a file", + SandboxPolicy::DangerFullAccess, + ) + .await?; + + let output = harness.apply_patch_output(call_id, output_type).await; let expected_pattern = format!( r"(?s)^Exit code: 0 @@ -620,7 +615,7 @@ Success. Updated the following files: M {file_name} ?$" ); - assert_regex_match(&expected_pattern, output); + assert_regex_match(&expected_pattern, output.as_str()); let updated_contents = fs::read_to_string(file_path)?; assert_eq!(updated_contents, "after\n", "expected updated file content"); @@ -629,99 +624,83 @@ M {file_name} } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn apply_patch_custom_tool_call_reports_failure_output() -> Result<()> { +#[test_case(ApplyPatchModelOutput::Freeform)] +#[test_case(ApplyPatchModelOutput::Function)] +#[test_case(ApplyPatchModelOutput::Shell)] +#[test_case(ApplyPatchModelOutput::ShellViaHeredoc)] +async fn apply_patch_custom_tool_call_reports_failure_output( + output_type: ApplyPatchModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); - let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { - config.include_apply_patch_tool = true; - }); - let test = builder.build(&server).await?; + let harness = apply_patch_harness().await?; let call_id = "apply-patch-failure"; let missing_file = "missing_custom_tool_apply_patch.txt"; let patch = format!( "*** Begin Patch\n*** Update File: {missing_file}\n@@\n-before\n+after\n*** End Patch\n" ); - let responses = vec![ - sse(vec![ - json!({"type": "response.created", "response": {"id": "resp-1"}}), - ev_custom_tool_call(call_id, "apply_patch", &patch), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "apply_patch failure done"), - ev_completed("resp-2"), - ]), - ]; - let mock = mount_sse_sequence(&server, responses).await; - - test.submit_turn_with_policy( - "attempt a failing apply_patch via custom tool", - SandboxPolicy::DangerFullAccess, + mount_apply_patch( + &harness, + call_id, + &patch, + "apply_patch failure done", + output_type, ) - .await?; + .await; - let req = mock - .last_request() - .expect("apply_patch output request recorded"); - let output_item = req.custom_tool_call_output(call_id); - let output = output_item - .get("output") - .and_then(Value::as_str) - .expect("apply_patch output string"); + harness + .test() + .submit_turn_with_policy( + "attempt a failing apply_patch via custom tool", + SandboxPolicy::DangerFullAccess, + ) + .await?; + + let output = harness.apply_patch_output(call_id, output_type).await; let expected_output = format!( "apply_patch verification failed: Failed to read file to update {}/{missing_file}: No such file or directory (os error 2)", - test.cwd.path().to_string_lossy() + harness.cwd().to_string_lossy() ); - assert_eq!(output, expected_output); + assert_eq!(output, expected_output.as_str()); Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn apply_patch_function_call_output_is_structured() -> Result<()> { +#[test_case(ApplyPatchModelOutput::Freeform)] +#[test_case(ApplyPatchModelOutput::Function)] +#[test_case(ApplyPatchModelOutput::Shell)] +#[test_case(ApplyPatchModelOutput::ShellViaHeredoc)] +async fn apply_patch_function_call_output_is_structured( + output_type: ApplyPatchModelOutput, +) -> Result<()> { skip_if_no_network!(Ok(())); - let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { - config.include_apply_patch_tool = true; - }); - let test = builder.build(&server).await?; + let harness = apply_patch_harness().await?; let call_id = "apply-patch-function"; let file_name = "function_apply_patch.txt"; let patch = format!("*** Begin Patch\n*** Add File: {file_name}\n+via function call\n*** End Patch\n"); - let responses = vec![ - sse(vec![ - json!({"type": "response.created", "response": {"id": "resp-1"}}), - ev_apply_patch_function_call(call_id, &patch), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "apply_patch function done"), - ev_completed("resp-2"), - ]), - ]; - let mock = mount_sse_sequence(&server, responses).await; - - test.submit_turn_with_policy( - "apply the patch via function-call apply_patch", - SandboxPolicy::DangerFullAccess, + mount_apply_patch( + &harness, + call_id, + &patch, + "apply_patch function done", + output_type, ) - .await?; - - let req = mock - .last_request() - .expect("apply_patch function output request recorded"); - let output_item = req.function_call_output(call_id); - let output = output_item - .get("output") - .and_then(Value::as_str) - .expect("apply_patch output string"); + .await; + harness + .test() + .submit_turn_with_policy( + "apply the patch via function-call apply_patch", + SandboxPolicy::DangerFullAccess, + ) + .await?; + let output = harness.apply_patch_output(call_id, output_type).await; let expected_pattern = format!( r"(?s)^Exit code: 0 Wall time: [0-9]+(?:\.[0-9]+)? seconds @@ -730,40 +709,32 @@ Success. Updated the following files: A {file_name} ?$" ); - assert_regex_match(&expected_pattern, output); + assert_regex_match(&expected_pattern, output.as_str()); Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn shell_output_is_structured_for_nonzero_exit() -> Result<()> { +#[test_case(ShellModelOutput::Shell)] +#[test_case(ShellModelOutput::ShellCommand)] +#[test_case(ShellModelOutput::LocalShell)] +async fn shell_output_is_structured_for_nonzero_exit(output_type: ShellModelOutput) -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { - config.model = "gpt-5-codex".to_string(); + let mut builder = test_codex().with_config(move |config| { + config.model = "gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("gpt-5-codex").expect("gpt-5-codex is a model family"); + find_family_for_model("gpt-5.1-codex").expect("gpt-5.1-codex is a model family"); config.include_apply_patch_tool = true; + if matches!(output_type, ShellModelOutput::ShellCommand) { + config.features.enable(Feature::ShellCommandTool); + } }); let test = builder.build(&server).await?; let call_id = "shell-nonzero-exit"; - let args = json!({ - "command": ["/bin/sh", "-c", "exit 42"], - "timeout_ms": 1_000, - }); - let responses = vec![ - sse(vec![ - json!({"type": "response.created", "response": {"id": "resp-1"}}), - ev_function_call(call_id, "shell", &serde_json::to_string(&args)?), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "shell failure handled"), - ev_completed("resp-2"), - ]), - ]; + let responses = shell_responses(call_id, vec!["/bin/sh", "-c", "exit 42"], output_type)?; let mock = mount_sse_sequence(&server, responses).await; test.submit_turn_with_policy( @@ -793,7 +764,7 @@ async fn shell_command_output_is_structured() -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { + let mut builder = test_codex().with_config(move |config| { config.features.enable(Feature::ShellCommandTool); }); let test = builder.build(&server).await?; @@ -847,9 +818,9 @@ async fn local_shell_call_output_is_structured() -> Result<()> { let server = start_mock_server().await; let mut builder = test_codex().with_config(|config| { - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("gpt-5-codex").expect("gpt-5-codex is a model family"); + find_family_for_model("gpt-5.1-codex").expect("gpt-5.1-codex is a model family"); config.include_apply_patch_tool = true; }); let test = builder.build(&server).await?; diff --git a/codex-rs/core/tests/suite/tool_parallelism.rs b/codex-rs/core/tests/suite/tool_parallelism.rs index 30ea0f7d2c..807bb9d82c 100644 --- a/codex-rs/core/tests/suite/tool_parallelism.rs +++ b/codex-rs/core/tests/suite/tool_parallelism.rs @@ -57,9 +57,9 @@ async fn run_turn_and_measure(test: &TestCodex, prompt: &str) -> anyhow::Result< #[allow(clippy::expect_used)] async fn build_codex_with_test_tool(server: &wiremock::MockServer) -> anyhow::Result { let mut builder = test_codex().with_config(|config| { - config.model = "test-gpt-5-codex".to_string(); + config.model = "test-gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("test-gpt-5-codex").expect("test-gpt-5-codex model family"); + find_family_for_model("test-gpt-5.1-codex").expect("test-gpt-5.1-codex model family"); }); builder.build(server).await } diff --git a/codex-rs/core/tests/suite/tools.rs b/codex-rs/core/tests/suite/tools.rs index a6086dcbfb..549d6e7d57 100644 --- a/codex-rs/core/tests/suite/tools.rs +++ b/codex-rs/core/tests/suite/tools.rs @@ -197,9 +197,9 @@ async fn sandbox_denied_shell_returns_original_output() -> Result<()> { let server = start_mock_server().await; let mut builder = test_codex().with_config(|config| { - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("gpt-5-codex").expect("gpt-5-codex model family"); + find_family_for_model("gpt-5.1-codex").expect("gpt-5.1-codex model family"); }); let fixture = builder.build(&server).await?; @@ -425,8 +425,8 @@ async fn shell_timeout_handles_background_grandchild_stdout() -> Result<()> { let server = start_mock_server().await; let mut builder = test_codex().with_config(|config| { - config.model = "gpt-5".to_string(); - config.model_family = find_family_for_model("gpt-5").expect("gpt-5 is a valid model"); + config.model = "gpt-5.1".to_string(); + config.model_family = find_family_for_model("gpt-5.1").expect("gpt-5.1 is a valid model"); config.sandbox_policy = SandboxPolicy::DangerFullAccess; }); let test = builder.build(&server).await?; diff --git a/codex-rs/core/tests/suite/truncation.rs b/codex-rs/core/tests/suite/truncation.rs index 37ee4f3990..200d27e887 100644 --- a/codex-rs/core/tests/suite/truncation.rs +++ b/codex-rs/core/tests/suite/truncation.rs @@ -41,9 +41,9 @@ async fn truncate_function_error_trims_respond_to_model() -> Result<()> { let server = start_mock_server().await; let mut builder = test_codex().with_config(|config| { // Use the test model that wires function tools like grep_files - config.model = "test-gpt-5-codex".to_string(); + config.model = "test-gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("test-gpt-5-codex").expect("model family for test model"); + find_family_for_model("test-gpt-5.1-codex").expect("model family for test model"); }); let test = builder.build(&server).await?; @@ -105,9 +105,9 @@ async fn tool_call_output_exceeds_limit_truncated_for_model() -> Result<()> { // Use a model that exposes the generic shell tool. let mut builder = test_codex().with_config(|config| { - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("gpt-5-codex").expect("gpt-5-codex is a model family"); + find_family_for_model("gpt-5.1-codex").expect("gpt-5.1-codex is a model family"); }); let fixture = builder.build(&server).await?; @@ -197,9 +197,9 @@ async fn tool_call_output_truncated_only_once() -> Result<()> { let server = start_mock_server().await; let mut builder = test_codex().with_config(|config| { - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("gpt-5-codex").expect("gpt-5-codex is a model family"); + find_family_for_model("gpt-5.1-codex").expect("gpt-5.1-codex is a model family"); }); let fixture = builder.build(&server).await?; let call_id = "shell-single-truncation"; diff --git a/codex-rs/core/tests/suite/undo.rs b/codex-rs/core/tests/suite/undo.rs index 7883d347f9..6dcb8e81e0 100644 --- a/codex-rs/core/tests/suite/undo.rs +++ b/codex-rs/core/tests/suite/undo.rs @@ -30,8 +30,8 @@ use pretty_assertions::assert_eq; async fn undo_harness() -> Result { TestCodexHarness::with_config(|config: &mut Config| { config.include_apply_patch_tool = true; - config.model = "gpt-5".to_string(); - config.model_family = find_family_for_model("gpt-5").expect("gpt-5 is valid"); + config.model = "gpt-5.1".to_string(); + config.model_family = find_family_for_model("gpt-5.1").expect("gpt-5.1 is valid"); config.features.enable(Feature::GhostCommit); }) .await diff --git a/codex-rs/core/tests/suite/unified_exec.rs b/codex-rs/core/tests/suite/unified_exec.rs index 8c682efaf2..970be5277e 100644 --- a/codex-rs/core/tests/suite/unified_exec.rs +++ b/codex-rs/core/tests/suite/unified_exec.rs @@ -159,7 +159,7 @@ async fn unified_exec_emits_exec_command_begin_event() -> Result<()> { let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { + let mut builder = test_codex().with_model("gpt-5").with_config(|config| { config.use_experimental_unified_exec_tool = true; config.features.enable(Feature::UnifiedExec); }); @@ -236,7 +236,7 @@ async fn unified_exec_respects_workdir_override() -> Result<()> { let server = start_mock_server().await; - let mut builder = test_codex().with_config(|config| { + let mut builder = test_codex().with_model("gpt-5").with_config(|config| { config.use_experimental_unified_exec_tool = true; config.features.enable(Feature::UnifiedExec); }); @@ -288,28 +288,22 @@ async fn unified_exec_respects_workdir_override() -> Result<()> { }) .await?; + let begin_event = wait_for_event_match(&codex, |msg| match msg { + EventMsg::ExecCommandBegin(event) if event.call_id == call_id => Some(event.clone()), + _ => None, + }) + .await; + + assert_eq!( + begin_event.cwd, workdir, + "exec_command cwd should reflect the requested workdir override" + ); + wait_for_event(&codex, |event| matches!(event, EventMsg::TaskComplete(_))).await; let requests = server.received_requests().await.expect("recorded requests"); assert!(!requests.is_empty(), "expected at least one POST request"); - let bodies = requests - .iter() - .map(|req| req.body_json::().expect("request json")) - .collect::>(); - - let outputs = collect_tool_outputs(&bodies)?; - let output = outputs - .get(call_id) - .expect("missing exec_command workdir output"); - let output_text = output.output.trim(); - let output_canonical = std::fs::canonicalize(output_text)?; - let expected_canonical = std::fs::canonicalize(&workdir)?; - assert_eq!( - output_canonical, expected_canonical, - "pwd should reflect the requested workdir override" - ); - Ok(()) } diff --git a/codex-rs/core/tests/suite/user_shell_cmd.rs b/codex-rs/core/tests/suite/user_shell_cmd.rs index 95c6269843..0d42c45c1c 100644 --- a/codex-rs/core/tests/suite/user_shell_cmd.rs +++ b/codex-rs/core/tests/suite/user_shell_cmd.rs @@ -270,9 +270,9 @@ async fn user_shell_command_is_truncated_only_once() -> anyhow::Result<()> { let server = start_mock_server().await; let mut builder = test_codex().with_config(|config| { - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.model_family = - find_family_for_model("gpt-5-codex").expect("gpt-5-codex is a model family"); + find_family_for_model("gpt-5.1-codex").expect("gpt-5.1-codex is a model family"); }); let fixture = builder.build(&server).await?; diff --git a/codex-rs/docs/codex_mcp_interface.md b/codex-rs/docs/codex_mcp_interface.md index 9b3a959438..a7236e363e 100644 --- a/codex-rs/docs/codex_mcp_interface.md +++ b/codex-rs/docs/codex_mcp_interface.md @@ -55,7 +55,7 @@ Start a new session with optional overrides: Request `newConversation` params (subset): -- `model`: string model id (e.g. "o3", "gpt-5", "gpt-5-codex") +- `model`: string model id (e.g. "o3", "gpt-5.1", "gpt-5.1-codex") - `profile`: optional named profile - `cwd`: optional working directory - `approvalPolicy`: `untrusted` | `on-request` | `on-failure` | `never` @@ -119,13 +119,13 @@ For the complete request/response shapes and flow examples, see the [“Auth end ## Example: start and send a message ```json -{ "jsonrpc": "2.0", "id": 1, "method": "newConversation", "params": { "model": "gpt-5", "approvalPolicy": "on-request" } } +{ "jsonrpc": "2.0", "id": 1, "method": "newConversation", "params": { "model": "gpt-5.1", "approvalPolicy": "on-request" } } ``` Server responds: ```json -{ "jsonrpc": "2.0", "id": 1, "result": { "conversationId": "c7b0…", "model": "gpt-5", "rolloutPath": "/path/to/rollout.jsonl" } } +{ "jsonrpc": "2.0", "id": 1, "result": { "conversationId": "c7b0…", "model": "gpt-5.1", "rolloutPath": "/path/to/rollout.jsonl" } } ``` Then send input: diff --git a/codex-rs/exec/tests/suite/output_schema.rs b/codex-rs/exec/tests/suite/output_schema.rs index 485f5e3c79..89a23147bd 100644 --- a/codex-rs/exec/tests/suite/output_schema.rs +++ b/codex-rs/exec/tests/suite/output_schema.rs @@ -37,7 +37,7 @@ async fn exec_includes_output_schema_in_request() -> anyhow::Result<()> { .arg("--output-schema") .arg(&schema_path) .arg("-m") - .arg("gpt-5") + .arg("gpt-5.1") .arg("tell me a joke") .assert() .success(); diff --git a/codex-rs/exec/tests/suite/resume.rs b/codex-rs/exec/tests/suite/resume.rs index 24b8cb0bce..1a56edded3 100644 --- a/codex-rs/exec/tests/suite/resume.rs +++ b/codex-rs/exec/tests/suite/resume.rs @@ -196,7 +196,7 @@ fn exec_resume_preserves_cli_configuration_overrides() -> anyhow::Result<()> { .arg("--sandbox") .arg("workspace-write") .arg("--model") - .arg("gpt-5") + .arg("gpt-5.1") .arg("-C") .arg(env!("CARGO_MANIFEST_DIR")) .arg(&prompt) @@ -218,7 +218,7 @@ fn exec_resume_preserves_cli_configuration_overrides() -> anyhow::Result<()> { .arg("--sandbox") .arg("workspace-write") .arg("--model") - .arg("gpt-5-high") + .arg("gpt-5.1-high") .arg("-C") .arg(env!("CARGO_MANIFEST_DIR")) .arg(&prompt2) @@ -231,7 +231,7 @@ fn exec_resume_preserves_cli_configuration_overrides() -> anyhow::Result<()> { let stderr = String::from_utf8(output.stderr)?; assert!( - stderr.contains("model: gpt-5-high"), + stderr.contains("model: gpt-5.1-high"), "stderr missing model override: {stderr}" ); if cfg!(target_os = "windows") { diff --git a/codex-rs/protocol/src/models.rs b/codex-rs/protocol/src/models.rs index b5920ef539..80a56a532c 100644 --- a/codex-rs/protocol/src/models.rs +++ b/codex-rs/protocol/src/models.rs @@ -134,6 +134,9 @@ pub enum ResponseItem { GhostSnapshot { ghost_commit: GhostCommit, }, + CompactionSummary { + encrypted_content: String, + }, #[serde(other)] Other, } diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 553f6437a6..7f5e5228d8 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -1126,6 +1126,8 @@ pub enum RolloutItem { #[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, TS)] pub struct CompactedItem { pub message: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub replacement_history: Option>, } impl From for ResponseItem { diff --git a/codex-rs/rmcp-client/src/oauth.rs b/codex-rs/rmcp-client/src/oauth.rs index bd6833fca4..f8eafaf23e 100644 --- a/codex-rs/rmcp-client/src/oauth.rs +++ b/codex-rs/rmcp-client/src/oauth.rs @@ -50,6 +50,7 @@ use tokio::sync::Mutex; use crate::find_codex_home::find_codex_home; const KEYRING_SERVICE: &str = "Codex MCP Credentials"; +const REFRESH_SKEW_MILLIS: u64 = 30_000; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct StoredOAuthTokens { @@ -57,6 +58,8 @@ pub struct StoredOAuthTokens { pub url: String, pub client_id: String, pub token_response: WrappedOAuthTokenResponse, + #[serde(default)] + pub expires_at: Option, } /// Determine where Codex should store and read MCP credentials. @@ -113,6 +116,22 @@ pub(crate) fn has_oauth_tokens( Ok(load_oauth_tokens(server_name, url, store_mode)?.is_some()) } +fn refresh_expires_in_from_timestamp(tokens: &mut StoredOAuthTokens) { + let Some(expires_at) = tokens.expires_at else { + return; + }; + + match expires_in_from_timestamp(expires_at) { + Some(seconds) => { + let duration = Duration::from_secs(seconds); + tokens.token_response.0.set_expires_in(Some(&duration)); + } + None => { + tokens.token_response.0.set_expires_in(None); + } + } +} + fn load_oauth_tokens_from_keyring_with_fallback_to_file( keyring_store: &K, server_name: &str, @@ -137,8 +156,9 @@ fn load_oauth_tokens_from_keyring( let key = compute_store_key(server_name, url)?; match keyring_store.load(KEYRING_SERVICE, &key) { Ok(Some(serialized)) => { - let tokens: StoredOAuthTokens = serde_json::from_str(&serialized) + let mut tokens: StoredOAuthTokens = serde_json::from_str(&serialized) .context("failed to deserialize OAuth tokens from keyring")?; + refresh_expires_in_from_timestamp(&mut tokens); Ok(Some(tokens)) } Ok(None) => Ok(None), @@ -286,13 +306,24 @@ impl OAuthPersistor { match maybe_credentials { Some(credentials) => { + let mut last_credentials = self.inner.last_credentials.lock().await; + let new_token_response = WrappedOAuthTokenResponse(credentials.clone()); + let same_token = last_credentials + .as_ref() + .map(|prev| prev.token_response == new_token_response) + .unwrap_or(false); + let expires_at = if same_token { + last_credentials.as_ref().and_then(|prev| prev.expires_at) + } else { + compute_expires_at_millis(&credentials) + }; let stored = StoredOAuthTokens { server_name: self.inner.server_name.clone(), url: self.inner.url.clone(), client_id, - token_response: WrappedOAuthTokenResponse(credentials.clone()), + token_response: new_token_response, + expires_at, }; - let mut last_credentials = self.inner.last_credentials.lock().await; if last_credentials.as_ref() != Some(&stored) { save_oauth_tokens(&self.inner.server_name, &stored, self.inner.store_mode)?; *last_credentials = Some(stored); @@ -317,6 +348,30 @@ impl OAuthPersistor { Ok(()) } + + pub(crate) async fn refresh_if_needed(&self) -> Result<()> { + let expires_at = { + let guard = self.inner.last_credentials.lock().await; + guard.as_ref().and_then(|tokens| tokens.expires_at) + }; + + if !token_needs_refresh(expires_at) { + return Ok(()); + } + + { + let manager = self.inner.authorization_manager.clone(); + let guard = manager.lock().await; + guard.refresh_token().await.with_context(|| { + format!( + "failed to refresh OAuth tokens for server {}", + self.inner.server_name + ) + })?; + } + + self.persist_if_needed().await + } } const FALLBACK_FILENAME: &str = ".credentials.json"; @@ -366,19 +421,14 @@ fn load_oauth_tokens_from_file(server_name: &str, url: &str) -> Result Result<()> { let mut store = read_fallback_file()?.unwrap_or_default(); let token_response = &tokens.token_response.0; + let expires_at = tokens + .expires_at + .or_else(|| compute_expires_at_millis(token_response)); let refresh_token = token_response .refresh_token() .map(|token| token.secret().to_string()); @@ -403,7 +456,7 @@ fn save_oauth_tokens_to_file(tokens: &StoredOAuthTokens) -> Result<()> { server_url: tokens.url.clone(), client_id: tokens.client_id.clone(), access_token: token_response.access_token().secret().to_string(), - expires_at: compute_expires_at_millis(token_response), + expires_at, refresh_token, scopes, }; @@ -427,7 +480,7 @@ fn delete_oauth_tokens_from_file(key: &str) -> Result { Ok(removed) } -fn compute_expires_at_millis(response: &OAuthTokenResponse) -> Option { +pub(crate) fn compute_expires_at_millis(response: &OAuthTokenResponse) -> Option { let expires_in = response.expires_in()?; let now = SystemTime::now() .duration_since(UNIX_EPOCH) @@ -454,6 +507,19 @@ fn expires_in_from_timestamp(expires_at: u64) -> Option { } } +fn token_needs_refresh(expires_at: Option) -> bool { + let Some(expires_at) = expires_at else { + return false; + }; + + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_else(|_| Duration::from_secs(0)) + .as_millis() as u64; + + now.saturating_add(REFRESH_SKEW_MILLIS) >= expires_at +} + fn compute_store_key(server_name: &str, server_url: &str) -> Result { let mut payload = JsonMap::new(); payload.insert( @@ -589,8 +655,9 @@ mod tests { store.save(KEYRING_SERVICE, &key, &serialized)?; let loaded = - super::load_oauth_tokens_from_keyring(&store, &tokens.server_name, &tokens.url)?; - assert_eq!(loaded, Some(expected)); + super::load_oauth_tokens_from_keyring(&store, &tokens.server_name, &tokens.url)? + .expect("tokens should load from keyring"); + assert_tokens_match_without_expiry(&loaded, &expected); Ok(()) } @@ -750,6 +817,43 @@ mod tests { Ok(()) } + #[test] + fn refresh_expires_in_from_timestamp_restores_future_durations() { + let mut tokens = sample_tokens(); + let expires_at = tokens.expires_at.expect("expires_at should be set"); + + tokens.token_response.0.set_expires_in(None); + super::refresh_expires_in_from_timestamp(&mut tokens); + + let actual = tokens + .token_response + .0 + .expires_in() + .expect("expires_in should be restored") + .as_secs(); + let expected = super::expires_in_from_timestamp(expires_at) + .expect("expires_at should still be in the future"); + let diff = actual.abs_diff(expected); + assert!(diff <= 1, "expires_in drift too large: diff={diff}"); + } + + #[test] + fn refresh_expires_in_from_timestamp_clears_expired_tokens() { + let mut tokens = sample_tokens(); + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_else(|_| Duration::from_secs(0)); + let expired_at = now.as_millis() as u64; + tokens.expires_at = Some(expired_at.saturating_sub(1000)); + + let duration = Duration::from_secs(600); + tokens.token_response.0.set_expires_in(Some(&duration)); + + super::refresh_expires_in_from_timestamp(&mut tokens); + + assert!(tokens.token_response.0.expires_in().is_none()); + } + fn assert_tokens_match_without_expiry( actual: &StoredOAuthTokens, expected: &StoredOAuthTokens, @@ -757,6 +861,7 @@ mod tests { assert_eq!(actual.server_name, expected.server_name); assert_eq!(actual.url, expected.url); assert_eq!(actual.client_id, expected.client_id); + assert_eq!(actual.expires_at, expected.expires_at); assert_token_response_match_without_expiry( &actual.token_response, &expected.token_response, @@ -803,12 +908,14 @@ mod tests { ])); let expires_in = Duration::from_secs(3600); response.set_expires_in(Some(&expires_in)); + let expires_at = super::compute_expires_at_millis(&response); StoredOAuthTokens { server_name: "test-server".to_string(), url: "https://example.test".to_string(), client_id: "client-id".to_string(), token_response: WrappedOAuthTokenResponse(response), + expires_at, } } } diff --git a/codex-rs/rmcp-client/src/perform_oauth_login.rs b/codex-rs/rmcp-client/src/perform_oauth_login.rs index 425e124d7d..d8ffdd3949 100644 --- a/codex-rs/rmcp-client/src/perform_oauth_login.rs +++ b/codex-rs/rmcp-client/src/perform_oauth_login.rs @@ -17,6 +17,7 @@ use urlencoding::decode; use crate::OAuthCredentialsStoreMode; use crate::StoredOAuthTokens; use crate::WrappedOAuthTokenResponse; +use crate::oauth::compute_expires_at_millis; use crate::save_oauth_tokens; use crate::utils::apply_default_headers; use crate::utils::build_default_headers; @@ -91,11 +92,13 @@ pub async fn perform_oauth_login( let credentials = credentials_opt.ok_or_else(|| anyhow!("OAuth provider did not return credentials"))?; + let expires_at = compute_expires_at_millis(&credentials); let stored = StoredOAuthTokens { server_name: server_name.to_string(), url: server_url.to_string(), client_id, token_response: WrappedOAuthTokenResponse(credentials), + expires_at, }; save_oauth_tokens(server_name, &stored, store_mode)?; diff --git a/codex-rs/rmcp-client/src/rmcp_client.rs b/codex-rs/rmcp-client/src/rmcp_client.rs index f21859dc92..d7d3477b00 100644 --- a/codex-rs/rmcp-client/src/rmcp_client.rs +++ b/codex-rs/rmcp-client/src/rmcp_client.rs @@ -267,6 +267,7 @@ impl RmcpClient { params: Option, timeout: Option, ) -> Result { + self.refresh_oauth_if_needed().await; let service = self.service().await?; let rmcp_params = params .map(convert_to_rmcp::<_, PaginatedRequestParam>) @@ -284,6 +285,7 @@ impl RmcpClient { params: Option, timeout: Option, ) -> Result { + self.refresh_oauth_if_needed().await; let service = self.service().await?; let rmcp_params = params .map(convert_to_rmcp::<_, PaginatedRequestParam>) @@ -301,6 +303,7 @@ impl RmcpClient { params: Option, timeout: Option, ) -> Result { + self.refresh_oauth_if_needed().await; let service = self.service().await?; let rmcp_params = params .map(convert_to_rmcp::<_, PaginatedRequestParam>) @@ -318,6 +321,7 @@ impl RmcpClient { params: ReadResourceRequestParams, timeout: Option, ) -> Result { + self.refresh_oauth_if_needed().await; let service = self.service().await?; let rmcp_params: ReadResourceRequestParam = convert_to_rmcp(params)?; let fut = service.read_resource(rmcp_params); @@ -333,6 +337,7 @@ impl RmcpClient { arguments: Option, timeout: Option, ) -> Result { + self.refresh_oauth_if_needed().await; let service = self.service().await?; let params = CallToolRequestParams { arguments, name }; let rmcp_params: CallToolRequestParam = convert_to_rmcp(params)?; @@ -371,6 +376,14 @@ impl RmcpClient { warn!("failed to persist OAuth tokens: {error}"); } } + + async fn refresh_oauth_if_needed(&self) { + if let Some(runtime) = self.oauth_persistor().await + && let Err(error) = runtime.refresh_if_needed().await + { + warn!("failed to refresh OAuth tokens: {error}"); + } + } } async fn create_oauth_transport_and_runtime( diff --git a/codex-rs/tui/src/exec_command.rs b/codex-rs/tui/src/exec_command.rs index 6f2212b094..8ce6c2632e 100644 --- a/codex-rs/tui/src/exec_command.rs +++ b/codex-rs/tui/src/exec_command.rs @@ -1,7 +1,7 @@ use std::path::Path; use std::path::PathBuf; -use codex_core::bash::extract_bash_command; +use codex_core::parse_command::extract_shell_command; use dirs::home_dir; use shlex::try_join; @@ -10,7 +10,7 @@ pub(crate) fn escape_command(command: &[String]) -> String { } pub(crate) fn strip_bash_lc_and_escape(command: &[String]) -> String { - if let Some((_, script)) = extract_bash_command(command) { + if let Some((_, script)) = extract_shell_command(command) { return script.to_string(); } escape_command(command) diff --git a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_monthly_limit.snap b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_monthly_limit.snap index 66484261b9..7ceee1466e 100644 --- a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_monthly_limit.snap +++ b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_monthly_limit.snap @@ -10,7 +10,7 @@ expression: sanitized │ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ │ information on rate limits and credits │ │ │ -│ Model: gpt-5-codex (reasoning none, summaries auto) │ +│ Model: gpt-5.1-codex (reasoning none, summaries auto) │ │ Directory: [[workspace]] │ │ Approval: on-request │ │ Sandbox: read-only │ diff --git a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_reasoning_details.snap b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_reasoning_details.snap index 4dc951db58..88d16f86a7 100644 --- a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_reasoning_details.snap +++ b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_reasoning_details.snap @@ -4,20 +4,20 @@ expression: sanitized --- /status -╭─────────────────────────────────────────────────────────────────────╮ -│ >_ OpenAI Codex (v0.0.0) │ -│ │ -│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ -│ information on rate limits and credits │ -│ │ -│ Model: gpt-5-codex (reasoning high, summaries detailed) │ -│ Directory: [[workspace]] │ -│ Approval: on-request │ -│ Sandbox: workspace-write │ -│ Agents.md: │ -│ │ -│ Token usage: 1.9K total (1K input + 900 output) │ -│ Context window: 100% left (2.25K used / 272K) │ -│ 5h limit: [██████░░░░░░░░░░░░░░] 28% left (resets 03:14) │ -│ Weekly limit: [███████████░░░░░░░░░] 55% left (resets 03:24) │ -╰─────────────────────────────────────────────────────────────────────╯ +╭───────────────────────────────────────────────────────────────────────╮ +│ >_ OpenAI Codex (v0.0.0) │ +│ │ +│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ +│ information on rate limits and credits │ +│ │ +│ Model: gpt-5.1-codex (reasoning high, summaries detailed) │ +│ Directory: [[workspace]] │ +│ Approval: on-request │ +│ Sandbox: workspace-write │ +│ Agents.md: │ +│ │ +│ Token usage: 1.9K total (1K input + 900 output) │ +│ Context window: 100% left (2.25K used / 272K) │ +│ 5h limit: [██████░░░░░░░░░░░░░░] 28% left (resets 03:14) │ +│ Weekly limit: [███████████░░░░░░░░░] 55% left (resets 03:24) │ +╰───────────────────────────────────────────────────────────────────────╯ diff --git a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_empty_limits_message.snap b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_empty_limits_message.snap index 17862db238..6b21a7ec2e 100644 --- a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_empty_limits_message.snap +++ b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_empty_limits_message.snap @@ -4,19 +4,19 @@ expression: sanitized --- /status -╭─────────────────────────────────────────────────────────────────╮ -│ >_ OpenAI Codex (v0.0.0) │ -│ │ -│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ -│ information on rate limits and credits │ -│ │ -│ Model: gpt-5-codex (reasoning none, summaries auto) │ -│ Directory: [[workspace]] │ -│ Approval: on-request │ -│ Sandbox: read-only │ -│ Agents.md: │ -│ │ -│ Token usage: 750 total (500 input + 250 output) │ -│ Context window: 100% left (750 used / 272K) │ -│ Limits: data not available yet │ -╰─────────────────────────────────────────────────────────────────╯ +╭───────────────────────────────────────────────────────────────────╮ +│ >_ OpenAI Codex (v0.0.0) │ +│ │ +│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ +│ information on rate limits and credits │ +│ │ +│ Model: gpt-5.1-codex (reasoning none, summaries auto) │ +│ Directory: [[workspace]] │ +│ Approval: on-request │ +│ Sandbox: read-only │ +│ Agents.md: │ +│ │ +│ Token usage: 750 total (500 input + 250 output) │ +│ Context window: 100% left (750 used / 272K) │ +│ Limits: data not available yet │ +╰───────────────────────────────────────────────────────────────────╯ diff --git a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_missing_limits_message.snap b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_missing_limits_message.snap index 17862db238..6b21a7ec2e 100644 --- a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_missing_limits_message.snap +++ b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_missing_limits_message.snap @@ -4,19 +4,19 @@ expression: sanitized --- /status -╭─────────────────────────────────────────────────────────────────╮ -│ >_ OpenAI Codex (v0.0.0) │ -│ │ -│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ -│ information on rate limits and credits │ -│ │ -│ Model: gpt-5-codex (reasoning none, summaries auto) │ -│ Directory: [[workspace]] │ -│ Approval: on-request │ -│ Sandbox: read-only │ -│ Agents.md: │ -│ │ -│ Token usage: 750 total (500 input + 250 output) │ -│ Context window: 100% left (750 used / 272K) │ -│ Limits: data not available yet │ -╰─────────────────────────────────────────────────────────────────╯ +╭───────────────────────────────────────────────────────────────────╮ +│ >_ OpenAI Codex (v0.0.0) │ +│ │ +│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ +│ information on rate limits and credits │ +│ │ +│ Model: gpt-5.1-codex (reasoning none, summaries auto) │ +│ Directory: [[workspace]] │ +│ Approval: on-request │ +│ Sandbox: read-only │ +│ Agents.md: │ +│ │ +│ Token usage: 750 total (500 input + 250 output) │ +│ Context window: 100% left (750 used / 272K) │ +│ Limits: data not available yet │ +╰───────────────────────────────────────────────────────────────────╯ diff --git a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_stale_limits_message.snap b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_stale_limits_message.snap index 7d548f4b2e..b566582144 100644 --- a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_stale_limits_message.snap +++ b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_shows_stale_limits_message.snap @@ -10,7 +10,7 @@ expression: sanitized │ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ │ information on rate limits and credits │ │ │ -│ Model: gpt-5-codex (reasoning none, summaries auto) │ +│ Model: gpt-5.1-codex (reasoning none, summaries auto) │ │ Directory: [[workspace]] │ │ Approval: on-request │ │ Sandbox: read-only │ diff --git a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_truncates_in_narrow_terminal.snap b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_truncates_in_narrow_terminal.snap index ea5444d6d8..99dfe1850b 100644 --- a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_truncates_in_narrow_terminal.snap +++ b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_truncates_in_narrow_terminal.snap @@ -4,21 +4,19 @@ expression: sanitized --- /status -╭────────────────────────────────────────────╮ -│ >_ OpenAI Codex (v0.0.0) │ -│ │ -│ Visit https://chatgpt.com/codex/settings/ │ -│ usage for up-to-date │ -│ information on rate limits and credits │ -│ │ -│ Model: gpt-5-codex (reasoning │ -│ Directory: [[workspace]] │ -│ Approval: on-request │ -│ Sandbox: read-only │ -│ Agents.md: │ -│ │ -│ Token usage: 1.9K total (1K input + │ -│ Context window: 100% left (2.25K used / │ -│ 5h limit: [██████░░░░░░░░░░░░░░] │ -│ (resets 03:14) │ -╰────────────────────────────────────────────╯ +╭────────────────────────────────────────────────────────────────────╮ +│ >_ OpenAI Codex (v0.0.0) │ +│ │ +│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ +│ information on rate limits and credits │ +│ │ +│ Model: gpt-5.1-codex (reasoning high, summaries detail │ +│ Directory: [[workspace]] │ +│ Approval: on-request │ +│ Sandbox: read-only │ +│ Agents.md: │ +│ │ +│ Token usage: 1.9K total (1K input + 900 output) │ +│ Context window: 100% left (2.25K used / 272K) │ +│ 5h limit: [██████░░░░░░░░░░░░░░] 28% left (resets 03:14) │ +╰────────────────────────────────────────────────────────────────────╯ diff --git a/codex-rs/tui/src/status/tests.rs b/codex-rs/tui/src/status/tests.rs index 49a5937288..5120e36f96 100644 --- a/codex-rs/tui/src/status/tests.rs +++ b/codex-rs/tui/src/status/tests.rs @@ -81,7 +81,7 @@ fn reset_at_from(captured_at: &chrono::DateTime, seconds: i64) -> fn status_snapshot_includes_reasoning_details() { let temp_home = TempDir::new().expect("temp home"); let mut config = test_config(&temp_home); - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.model_provider_id = "openai".to_string(); config.model_reasoning_effort = Some(ReasoningEffort::High); config.model_reasoning_summary = ReasoningSummary::Detailed; @@ -144,7 +144,7 @@ fn status_snapshot_includes_reasoning_details() { fn status_snapshot_includes_monthly_limit() { let temp_home = TempDir::new().expect("temp home"); let mut config = test_config(&temp_home); - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.model_provider_id = "openai".to_string(); config.cwd = PathBuf::from("/workspace/tests"); @@ -194,7 +194,7 @@ fn status_snapshot_includes_monthly_limit() { fn status_card_token_usage_excludes_cached_tokens() { let temp_home = TempDir::new().expect("temp home"); let mut config = test_config(&temp_home); - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.cwd = PathBuf::from("/workspace/tests"); let auth_manager = test_auth_manager(&config); @@ -232,7 +232,7 @@ fn status_card_token_usage_excludes_cached_tokens() { fn status_snapshot_truncates_in_narrow_terminal() { let temp_home = TempDir::new().expect("temp home"); let mut config = test_config(&temp_home); - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.model_provider_id = "openai".to_string(); config.model_reasoning_effort = Some(ReasoningEffort::High); config.model_reasoning_summary = ReasoningSummary::Detailed; @@ -270,7 +270,7 @@ fn status_snapshot_truncates_in_narrow_terminal() { Some(&rate_display), captured_at, ); - let mut rendered_lines = render_lines(&composite.display_lines(46)); + let mut rendered_lines = render_lines(&composite.display_lines(70)); if cfg!(windows) { for line in &mut rendered_lines { *line = line.replace('\\', "/"); @@ -285,7 +285,7 @@ fn status_snapshot_truncates_in_narrow_terminal() { fn status_snapshot_shows_missing_limits_message() { let temp_home = TempDir::new().expect("temp home"); let mut config = test_config(&temp_home); - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.cwd = PathBuf::from("/workspace/tests"); let auth_manager = test_auth_manager(&config); @@ -325,7 +325,7 @@ fn status_snapshot_shows_missing_limits_message() { fn status_snapshot_shows_empty_limits_message() { let temp_home = TempDir::new().expect("temp home"); let mut config = test_config(&temp_home); - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.cwd = PathBuf::from("/workspace/tests"); let auth_manager = test_auth_manager(&config); @@ -370,7 +370,7 @@ fn status_snapshot_shows_empty_limits_message() { fn status_snapshot_shows_stale_limits_message() { let temp_home = TempDir::new().expect("temp home"); let mut config = test_config(&temp_home); - config.model = "gpt-5-codex".to_string(); + config.model = "gpt-5.1-codex".to_string(); config.cwd = PathBuf::from("/workspace/tests"); let auth_manager = test_auth_manager(&config); diff --git a/codex-rs/utils/git/src/branch.rs b/codex-rs/utils/git/src/branch.rs new file mode 100644 index 0000000000..543dffd9c9 --- /dev/null +++ b/codex-rs/utils/git/src/branch.rs @@ -0,0 +1,144 @@ +use std::ffi::OsString; +use std::path::Path; + +use crate::GitToolingError; +use crate::operations::ensure_git_repository; +use crate::operations::resolve_head; +use crate::operations::resolve_repository_root; +use crate::operations::run_git_for_stdout; + +/// Returns the merge-base commit between `HEAD` and the provided branch, if both exist. +/// +/// The function mirrors `git merge-base HEAD ` but returns `Ok(None)` when +/// the repository has no `HEAD` yet or when the branch cannot be resolved. +pub fn merge_base_with_head( + repo_path: &Path, + branch: &str, +) -> Result, GitToolingError> { + ensure_git_repository(repo_path)?; + let repo_root = resolve_repository_root(repo_path)?; + let head = match resolve_head(repo_root.as_path())? { + Some(head) => head, + None => return Ok(None), + }; + + let branch_ref = match run_git_for_stdout( + repo_root.as_path(), + vec![ + OsString::from("rev-parse"), + OsString::from("--verify"), + OsString::from(branch), + ], + None, + ) { + Ok(rev) => rev, + Err(GitToolingError::GitCommand { .. }) => return Ok(None), + Err(other) => return Err(other), + }; + + let merge_base = run_git_for_stdout( + repo_root.as_path(), + vec![ + OsString::from("merge-base"), + OsString::from(head), + OsString::from(branch_ref), + ], + None, + )?; + + Ok(Some(merge_base)) +} + +#[cfg(test)] +mod tests { + use super::merge_base_with_head; + use crate::GitToolingError; + use pretty_assertions::assert_eq; + use std::path::Path; + use std::process::Command; + use tempfile::tempdir; + + fn run_git_in(repo_path: &Path, args: &[&str]) { + let status = Command::new("git") + .current_dir(repo_path) + .args(args) + .status() + .expect("git command"); + assert!(status.success(), "git command failed: {args:?}"); + } + + fn run_git_stdout(repo_path: &Path, args: &[&str]) -> String { + let output = Command::new("git") + .current_dir(repo_path) + .args(args) + .output() + .expect("git command"); + assert!(output.status.success(), "git command failed: {args:?}"); + String::from_utf8_lossy(&output.stdout).trim().to_string() + } + + fn init_test_repo(repo_path: &Path) { + run_git_in(repo_path, &["init", "--initial-branch=main"]); + run_git_in(repo_path, &["config", "core.autocrlf", "false"]); + } + + fn commit(repo_path: &Path, message: &str) { + run_git_in( + repo_path, + &[ + "-c", + "user.name=Tester", + "-c", + "user.email=test@example.com", + "commit", + "-m", + message, + ], + ); + } + + #[test] + fn merge_base_returns_shared_commit() -> Result<(), GitToolingError> { + let temp = tempdir()?; + let repo = temp.path(); + init_test_repo(repo); + + std::fs::write(repo.join("base.txt"), "base\n")?; + run_git_in(repo, &["add", "base.txt"]); + commit(repo, "base commit"); + + run_git_in(repo, &["checkout", "-b", "feature"]); + std::fs::write(repo.join("feature.txt"), "feature change\n")?; + run_git_in(repo, &["add", "feature.txt"]); + commit(repo, "feature commit"); + + run_git_in(repo, &["checkout", "main"]); + std::fs::write(repo.join("main.txt"), "main change\n")?; + run_git_in(repo, &["add", "main.txt"]); + commit(repo, "main commit"); + + run_git_in(repo, &["checkout", "feature"]); + + let expected = run_git_stdout(repo, &["merge-base", "HEAD", "main"]); + let merge_base = merge_base_with_head(repo, "main")?; + assert_eq!(merge_base, Some(expected)); + + Ok(()) + } + + #[test] + fn merge_base_returns_none_when_branch_missing() -> Result<(), GitToolingError> { + let temp = tempdir()?; + let repo = temp.path(); + init_test_repo(repo); + + std::fs::write(repo.join("tracked.txt"), "tracked\n")?; + run_git_in(repo, &["add", "tracked.txt"]); + commit(repo, "initial"); + + let merge_base = merge_base_with_head(repo, "missing-branch")?; + assert_eq!(merge_base, None); + + Ok(()) + } +} diff --git a/codex-rs/utils/git/src/lib.rs b/codex-rs/utils/git/src/lib.rs index cf2887aa8f..e96b0eab6a 100644 --- a/codex-rs/utils/git/src/lib.rs +++ b/codex-rs/utils/git/src/lib.rs @@ -2,6 +2,7 @@ use std::fmt; use std::path::PathBuf; mod apply; +mod branch; mod errors; mod ghost_commits; mod operations; @@ -13,6 +14,7 @@ pub use apply::apply_git_patch; pub use apply::extract_paths_from_patch; pub use apply::parse_git_apply_output; pub use apply::stage_paths; +pub use branch::merge_base_with_head; pub use errors::GitToolingError; pub use ghost_commits::CreateGhostCommitOptions; pub use ghost_commits::create_ghost_commit; diff --git a/codex-rs/utils/tokenizer/src/lib.rs b/codex-rs/utils/tokenizer/src/lib.rs index 1c343e439e..23ab261fa6 100644 --- a/codex-rs/utils/tokenizer/src/lib.rs +++ b/codex-rs/utils/tokenizer/src/lib.rs @@ -158,7 +158,7 @@ mod tests { #[test] fn model_mapping_builds_tokenizer() -> Result<(), TokenizerError> { // Choose a long-standing model alias that maps to cl100k_base. - let tok = Tokenizer::for_model("gpt-5")?; + let tok = Tokenizer::for_model("gpt-5.1")?; let ids = tok.encode("ok", false); let back = tok.decode(&ids)?; assert_eq!(back, "ok"); diff --git a/docs/config.md b/docs/config.md index ed5aed6fc7..51867773a9 100644 --- a/docs/config.md +++ b/docs/config.md @@ -64,7 +64,7 @@ Notes: The model that Codex should use. ```toml -model = "gpt-5" # overrides the default ("gpt-5-codex" on macOS/Linux, "gpt-5" on Windows) +model = "gpt-5.1" # overrides the default ("gpt-5.1-codex" on macOS/Linux, "gpt-5.1" on Windows) ``` ### model_providers @@ -191,7 +191,7 @@ model = "mistral" ### model_reasoning_effort -If the selected model is known to support reasoning (for example: `o3`, `o4-mini`, `codex-*`, `gpt-5`, `gpt-5-codex`), reasoning is enabled by default when using the Responses API. As explained in the [OpenAI Platform documentation](https://platform.openai.com/docs/guides/reasoning?api-mode=responses#get-started-with-reasoning), this can be set to: +If the selected model is known to support reasoning (for example: `o3`, `o4-mini`, `codex-*`, `gpt-5.1`, `gpt-5.1-codex`), reasoning is enabled by default when using the Responses API. As explained in the [OpenAI Platform documentation](https://platform.openai.com/docs/guides/reasoning?api-mode=responses#get-started-with-reasoning), this can be set to: - `"minimal"` - `"low"` @@ -227,7 +227,7 @@ When set, Codex includes a `text` object in the request payload with the configu Example: ```toml -model = "gpt-5" +model = "gpt-5.1" model_verbosity = "low" ``` @@ -818,7 +818,7 @@ Users can specify config values at multiple levels. Order of precedence is as fo 1. custom command-line argument, e.g., `--model o3` 2. as part of a profile, where the `--profile` is specified via a CLI (or in the config file itself) 3. as an entry in `config.toml`, e.g., `model = "o3"` -4. the default value that comes with Codex CLI (i.e., Codex CLI defaults to `gpt-5-codex`) +4. the default value that comes with Codex CLI (i.e., Codex CLI defaults to `gpt-5.1-codex`) ### history @@ -921,7 +921,7 @@ Valid values: | Key | Type / Values | Notes | | ------------------------------------------------ | ----------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | -| `model` | string | Model to use (e.g., `gpt-5-codex`). | +| `model` | string | Model to use (e.g., `gpt-5.1-codex`). | | `model_provider` | string | Provider id from `model_providers` (default: `openai`). | | `model_context_window` | number | Context window tokens. | | `model_max_output_tokens` | number | Max output tokens. | diff --git a/docs/example-config.md b/docs/example-config.md index 43a12a3b5e..21b360ccef 100644 --- a/docs/example-config.md +++ b/docs/example-config.md @@ -19,11 +19,11 @@ Use this example configuration as a starting point. For an explanation of each f ################################################################################ # Primary model used by Codex. Default differs by OS; non-Windows defaults here. -# Linux/macOS default: "gpt-5-codex"; Windows default: "gpt-5". -model = "gpt-5-codex" +# Linux/macOS default: "gpt-5.1-codex"; Windows default: "gpt-5.1". +model = "gpt-5.1-codex" -# Model used by the /review feature (code reviews). Default: "gpt-5-codex". -review_model = "gpt-5-codex" +# Model used by the /review feature (code reviews). Default: "gpt-5.1-codex". +review_model = "gpt-5.1-codex" # Provider id selected from [model_providers]. Default: "openai". model_provider = "openai" @@ -315,7 +315,7 @@ mcp_oauth_credentials_store = "auto" [profiles] # [profiles.default] -# model = "gpt-5-codex" +# model = "gpt-5.1-codex" # model_provider = "openai" # approval_policy = "on-request" # sandbox_mode = "read-only" diff --git a/docs/exec.md b/docs/exec.md index 81475555c7..2b5fb10997 100644 --- a/docs/exec.md +++ b/docs/exec.md @@ -99,8 +99,8 @@ codex exec resume --last "Fix use-after-free issues" Only the conversation context is preserved; you must still provide flags to customize Codex behavior. ```shell -codex exec --model gpt-5-codex --json "Review the change, look for use-after-free issues" -codex exec --model gpt-5 --json resume --last "Fix use-after-free issues" +codex exec --model gpt-5.1-codex --json "Review the change, look for use-after-free issues" +codex exec --model gpt-5.1 --json resume --last "Fix use-after-free issues" ``` ## Authentication