From 35c76ad47d0f6f134923026c9c80d1f2e9bbd83f Mon Sep 17 00:00:00 2001 From: Gabriel Peal Date: Wed, 1 Oct 2025 22:41:56 -0700 Subject: [PATCH 01/16] fix: update the gpt-5-codex prompt to be more explicit that it should always used fenced code blocks info tags (#4569) We get spurrious reports that the model writes fenced code blocks without an info tag which then causes auto-language detection in the extension to incorrectly highlight the code and show the wrong language. The model should really always include a tag when it can. --- codex-rs/core/gpt_5_codex_prompt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codex-rs/core/gpt_5_codex_prompt.md b/codex-rs/core/gpt_5_codex_prompt.md index 9a298f460f..33ab98807d 100644 --- a/codex-rs/core/gpt_5_codex_prompt.md +++ b/codex-rs/core/gpt_5_codex_prompt.md @@ -89,7 +89,7 @@ You are producing plain text that will later be styled by the CLI. Follow these - Headers: optional; short Title Case (1-3 words) wrapped in **…**; no blank line before the first bullet; add only if they truly help. - Bullets: use - ; merge related points; keep to one line when possible; 4–6 per list ordered by importance; keep phrasing consistent. - Monospace: backticks for commands/paths/env vars/code ids and inline examples; use for literal keyword bullets; never combine with **. -- Code samples or multi-line snippets should be wrapped in fenced code blocks; add a language hint whenever obvious. +- Code samples or multi-line snippets should be wrapped in fenced code blocks; include an info string as often as possible. - Structure: group related bullets; order sections general → specific → supporting; for subsections, start with a bolded keyword bullet, then items; match complexity to the task. - Tone: collaborative, concise, factual; present tense, active voice; self‑contained; no "above/below"; parallel wording. - Don'ts: no nested bullets/hierarchies; no ANSI codes; don't cram unrelated keywords; keep keyword lists short—wrap/reformat if long; avoid naming formatting styles in answers. From 2f6fb37d72d552f2c7bed66be8be8502ace6d71b Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 2 Oct 2025 09:59:45 -0700 Subject: [PATCH 02/16] Support CODEX_API_KEY for codex exec (#4615) Allows to set API key per invocation of `codex exec` --- codex-rs/app-server/src/message_processor.rs | 2 +- codex-rs/cloud-tasks/src/lib.rs | 2 +- codex-rs/cloud-tasks/src/util.rs | 2 +- codex-rs/core/src/auth.rs | 44 +++++++++--- codex-rs/core/tests/common/test_codex_exec.rs | 3 +- codex-rs/exec/src/lib.rs | 10 +-- codex-rs/exec/tests/suite/auth_env.rs | 34 +++++++++ codex-rs/exec/tests/suite/mod.rs | 1 + codex-rs/exec/tests/suite/resume.rs | 72 +++++++------------ codex-rs/login/src/lib.rs | 1 + codex-rs/mcp-server/src/message_processor.rs | 2 +- codex-rs/tui/src/lib.rs | 2 +- sdk/typescript/src/exec.ts | 2 +- 13 files changed, 109 insertions(+), 68 deletions(-) create mode 100644 codex-rs/exec/tests/suite/auth_env.rs diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index 1b9fcffb71..9d8db2f921 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -34,7 +34,7 @@ impl MessageProcessor { config: Arc, ) -> Self { let outgoing = Arc::new(outgoing); - let auth_manager = AuthManager::shared(config.codex_home.clone()); + let auth_manager = AuthManager::shared(config.codex_home.clone(), false); let conversation_manager = Arc::new(ConversationManager::new(auth_manager.clone())); let codex_message_processor = CodexMessageProcessor::new( auth_manager, diff --git a/codex-rs/cloud-tasks/src/lib.rs b/codex-rs/cloud-tasks/src/lib.rs index cea19b9741..da2d4eb908 100644 --- a/codex-rs/cloud-tasks/src/lib.rs +++ b/codex-rs/cloud-tasks/src/lib.rs @@ -190,7 +190,7 @@ pub async fn run_main(_cli: Cli, _codex_linux_sandbox_exe: Option) -> a // Require ChatGPT login (SWIC). Exit with a clear message if missing. let _token = match codex_core::config::find_codex_home() .ok() - .map(codex_login::AuthManager::new) + .map(|home| codex_login::AuthManager::new(home, false)) .and_then(|am| am.auth()) { Some(auth) => { diff --git a/codex-rs/cloud-tasks/src/util.rs b/codex-rs/cloud-tasks/src/util.rs index 9a610da244..8003a02f1e 100644 --- a/codex-rs/cloud-tasks/src/util.rs +++ b/codex-rs/cloud-tasks/src/util.rs @@ -70,7 +70,7 @@ pub async fn build_chatgpt_headers() -> HeaderMap { HeaderValue::from_str(&ua).unwrap_or(HeaderValue::from_static("codex-cli")), ); if let Ok(home) = codex_core::config::find_codex_home() { - let am = codex_login::AuthManager::new(home); + let am = codex_login::AuthManager::new(home, false); if let Some(auth) = am.auth() && let Ok(tok) = auth.get_token().await && !tok.is_empty() diff --git a/codex-rs/core/src/auth.rs b/codex-rs/core/src/auth.rs index b68540615a..4eea313e66 100644 --- a/codex-rs/core/src/auth.rs +++ b/codex-rs/core/src/auth.rs @@ -73,7 +73,7 @@ impl CodexAuth { /// Loads the available auth information from the auth.json. pub fn from_codex_home(codex_home: &Path) -> std::io::Result> { - load_auth(codex_home) + load_auth(codex_home, false) } pub async fn get_token_data(&self) -> Result { @@ -188,6 +188,7 @@ impl CodexAuth { } pub const OPENAI_API_KEY_ENV_VAR: &str = "OPENAI_API_KEY"; +pub const CODEX_API_KEY_ENV_VAR: &str = "CODEX_API_KEY"; pub fn read_openai_api_key_from_env() -> Option { env::var(OPENAI_API_KEY_ENV_VAR) @@ -196,6 +197,13 @@ pub fn read_openai_api_key_from_env() -> Option { .filter(|value| !value.is_empty()) } +pub fn read_codex_api_key_from_env() -> Option { + env::var(CODEX_API_KEY_ENV_VAR) + .ok() + .map(|value| value.trim().to_string()) + .filter(|value| !value.is_empty()) +} + pub fn get_auth_file(codex_home: &Path) -> PathBuf { codex_home.join("auth.json") } @@ -221,7 +229,18 @@ pub fn login_with_api_key(codex_home: &Path, api_key: &str) -> std::io::Result<( write_auth_json(&get_auth_file(codex_home), &auth_dot_json) } -fn load_auth(codex_home: &Path) -> std::io::Result> { +fn load_auth( + codex_home: &Path, + enable_codex_api_key_env: bool, +) -> std::io::Result> { + if enable_codex_api_key_env && let Some(api_key) = read_codex_api_key_from_env() { + let client = crate::default_client::create_client(); + return Ok(Some(CodexAuth::from_api_key_with_client( + api_key.as_str(), + client, + ))); + } + let auth_file = get_auth_file(codex_home); let client = crate::default_client::create_client(); let auth_dot_json = match try_read_auth_json(&auth_file) { @@ -455,7 +474,7 @@ mod tests { auth_dot_json, auth_file: _, .. - } = super::load_auth(codex_home.path()).unwrap().unwrap(); + } = super::load_auth(codex_home.path(), false).unwrap().unwrap(); assert_eq!(None, api_key); assert_eq!(AuthMode::ChatGPT, mode); @@ -494,7 +513,7 @@ mod tests { ) .unwrap(); - let auth = super::load_auth(dir.path()).unwrap().unwrap(); + let auth = super::load_auth(dir.path(), false).unwrap().unwrap(); assert_eq!(auth.mode, AuthMode::ApiKey); assert_eq!(auth.api_key, Some("sk-test-key".to_string())); @@ -577,6 +596,7 @@ mod tests { pub struct AuthManager { codex_home: PathBuf, inner: RwLock, + enable_codex_api_key_env: bool, } impl AuthManager { @@ -584,11 +604,14 @@ impl AuthManager { /// preferred auth method. Errors loading auth are swallowed; `auth()` will /// simply return `None` in that case so callers can treat it as an /// unauthenticated state. - pub fn new(codex_home: PathBuf) -> Self { - let auth = CodexAuth::from_codex_home(&codex_home).ok().flatten(); + pub fn new(codex_home: PathBuf, enable_codex_api_key_env: bool) -> Self { + let auth = load_auth(&codex_home, enable_codex_api_key_env) + .ok() + .flatten(); Self { codex_home, inner: RwLock::new(CachedAuth { auth }), + enable_codex_api_key_env, } } @@ -598,6 +621,7 @@ impl AuthManager { Arc::new(Self { codex_home: PathBuf::new(), inner: RwLock::new(cached), + enable_codex_api_key_env: false, }) } @@ -609,7 +633,9 @@ impl AuthManager { /// Force a reload of the auth information from auth.json. Returns /// whether the auth value changed. pub fn reload(&self) -> bool { - let new_auth = CodexAuth::from_codex_home(&self.codex_home).ok().flatten(); + let new_auth = load_auth(&self.codex_home, self.enable_codex_api_key_env) + .ok() + .flatten(); if let Ok(mut guard) = self.inner.write() { let changed = !AuthManager::auths_equal(&guard.auth, &new_auth); guard.auth = new_auth; @@ -628,8 +654,8 @@ impl AuthManager { } /// Convenience constructor returning an `Arc` wrapper. - pub fn shared(codex_home: PathBuf) -> Arc { - Arc::new(Self::new(codex_home)) + pub fn shared(codex_home: PathBuf, enable_codex_api_key_env: bool) -> Arc { + Arc::new(Self::new(codex_home, enable_codex_api_key_env)) } /// Attempt to refresh the current auth token (if any). On success, reload diff --git a/codex-rs/core/tests/common/test_codex_exec.rs b/codex-rs/core/tests/common/test_codex_exec.rs index 6a4ce32b0f..478d0c57f4 100644 --- a/codex-rs/core/tests/common/test_codex_exec.rs +++ b/codex-rs/core/tests/common/test_codex_exec.rs @@ -1,4 +1,5 @@ #![allow(clippy::expect_used)] +use codex_core::auth::CODEX_API_KEY_ENV_VAR; use std::path::Path; use tempfile::TempDir; use wiremock::MockServer; @@ -14,7 +15,7 @@ impl TestCodexExecBuilder { .expect("should find binary for codex-exec"); cmd.current_dir(self.cwd.path()) .env("CODEX_HOME", self.home.path()) - .env("OPENAI_API_KEY", "dummy"); + .env(CODEX_API_KEY_ENV_VAR, "dummy"); cmd } pub fn cmd_with_server(&self, server: &MockServer) -> assert_cmd::Command { diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index d77f968593..0ec905de15 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -236,8 +236,8 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any std::process::exit(1); } - let conversation_manager = - ConversationManager::new(AuthManager::shared(config.codex_home.clone())); + let auth_manager = AuthManager::shared(config.codex_home.clone(), true); + let conversation_manager = ConversationManager::new(auth_manager.clone()); // Handle resume subcommand by resolving a rollout path and using explicit resume API. let NewConversation { @@ -249,11 +249,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any if let Some(path) = resume_path { conversation_manager - .resume_conversation_from_rollout( - config.clone(), - path, - AuthManager::shared(config.codex_home.clone()), - ) + .resume_conversation_from_rollout(config.clone(), path, auth_manager.clone()) .await? } else { conversation_manager diff --git a/codex-rs/exec/tests/suite/auth_env.rs b/codex-rs/exec/tests/suite/auth_env.rs new file mode 100644 index 0000000000..d59f46cde8 --- /dev/null +++ b/codex-rs/exec/tests/suite/auth_env.rs @@ -0,0 +1,34 @@ +#![allow(clippy::unwrap_used, clippy::expect_used)] +use core_test_support::responses::ev_completed; +use core_test_support::responses::sse; +use core_test_support::responses::sse_response; +use core_test_support::responses::start_mock_server; +use core_test_support::test_codex_exec::test_codex_exec; +use wiremock::Mock; +use wiremock::matchers::header; +use wiremock::matchers::method; +use wiremock::matchers::path; + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn exec_uses_codex_api_key_env_var() -> anyhow::Result<()> { + let test = test_codex_exec(); + let server = start_mock_server().await; + + Mock::given(method("POST")) + .and(path("/v1/responses")) + .and(header("Authorization", "Bearer dummy")) + .respond_with(sse_response(sse(vec![ev_completed("request_0")]))) + .expect(1) + .mount(&server) + .await; + + test.cmd_with_server(&server) + .arg("--skip-git-repo-check") + .arg("-C") + .arg(env!("CARGO_MANIFEST_DIR")) + .arg("echo testing codex api key") + .assert() + .success(); + + Ok(()) +} diff --git a/codex-rs/exec/tests/suite/mod.rs b/codex-rs/exec/tests/suite/mod.rs index 52f5bca34c..d04ecd2c93 100644 --- a/codex-rs/exec/tests/suite/mod.rs +++ b/codex-rs/exec/tests/suite/mod.rs @@ -1,5 +1,6 @@ // Aggregates all former standalone integration tests as modules. mod apply_patch; +mod auth_env; mod output_schema; mod resume; mod sandbox; diff --git a/codex-rs/exec/tests/suite/resume.rs b/codex-rs/exec/tests/suite/resume.rs index 4c4d343ee3..16be995e4d 100644 --- a/codex-rs/exec/tests/suite/resume.rs +++ b/codex-rs/exec/tests/suite/resume.rs @@ -1,10 +1,9 @@ #![allow(clippy::unwrap_used, clippy::expect_used)] use anyhow::Context; -use assert_cmd::prelude::*; +use core_test_support::test_codex_exec::test_codex_exec; use serde_json::Value; -use std::process::Command; +use std::path::Path; use std::string::ToString; -use tempfile::TempDir; use uuid::Uuid; use walkdir::WalkDir; @@ -72,18 +71,15 @@ fn extract_conversation_id(path: &std::path::Path) -> String { #[test] fn exec_resume_last_appends_to_existing_file() -> anyhow::Result<()> { - let home = TempDir::new()?; - let fixture = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("tests/fixtures/cli_responses_fixture.sse"); + let test = test_codex_exec(); + let fixture = + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/cli_responses_fixture.sse"); // 1) First run: create a session with a unique marker in the content. let marker = format!("resume-last-{}", Uuid::new_v4()); let prompt = format!("echo {marker}"); - Command::cargo_bin("codex-exec") - .context("should find binary for codex-exec")? - .env("CODEX_HOME", home.path()) - .env("OPENAI_API_KEY", "dummy") + test.cmd() .env("CODEX_RS_SSE_FIXTURE", &fixture) .env("OPENAI_BASE_URL", "http://unused.local") .arg("--skip-git-repo-check") @@ -94,7 +90,7 @@ fn exec_resume_last_appends_to_existing_file() -> anyhow::Result<()> { .success(); // Find the created session file containing the marker. - let sessions_dir = home.path().join("sessions"); + let sessions_dir = test.home_path().join("sessions"); let path = find_session_file_containing_marker(&sessions_dir, &marker) .expect("no session file found after first run"); @@ -102,11 +98,7 @@ fn exec_resume_last_appends_to_existing_file() -> anyhow::Result<()> { let marker2 = format!("resume-last-2-{}", Uuid::new_v4()); let prompt2 = format!("echo {marker2}"); - let mut binding = assert_cmd::Command::cargo_bin("codex-exec") - .context("should find binary for codex-exec")?; - let cmd = binding - .env("CODEX_HOME", home.path()) - .env("OPENAI_API_KEY", "dummy") + test.cmd() .env("CODEX_RS_SSE_FIXTURE", &fixture) .env("OPENAI_BASE_URL", "http://unused.local") .arg("--skip-git-repo-check") @@ -114,8 +106,9 @@ fn exec_resume_last_appends_to_existing_file() -> anyhow::Result<()> { .arg(env!("CARGO_MANIFEST_DIR")) .arg(&prompt2) .arg("resume") - .arg("--last"); - cmd.assert().success(); + .arg("--last") + .assert() + .success(); // Ensure the same file was updated and contains both markers. let resumed_path = find_session_file_containing_marker(&sessions_dir, &marker2) @@ -132,18 +125,15 @@ fn exec_resume_last_appends_to_existing_file() -> anyhow::Result<()> { #[test] fn exec_resume_by_id_appends_to_existing_file() -> anyhow::Result<()> { - let home = TempDir::new()?; - let fixture = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("tests/fixtures/cli_responses_fixture.sse"); + let test = test_codex_exec(); + let fixture = + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/cli_responses_fixture.sse"); // 1) First run: create a session let marker = format!("resume-by-id-{}", Uuid::new_v4()); let prompt = format!("echo {marker}"); - Command::cargo_bin("codex-exec") - .context("should find binary for codex-exec")? - .env("CODEX_HOME", home.path()) - .env("OPENAI_API_KEY", "dummy") + test.cmd() .env("CODEX_RS_SSE_FIXTURE", &fixture) .env("OPENAI_BASE_URL", "http://unused.local") .arg("--skip-git-repo-check") @@ -153,7 +143,7 @@ fn exec_resume_by_id_appends_to_existing_file() -> anyhow::Result<()> { .assert() .success(); - let sessions_dir = home.path().join("sessions"); + let sessions_dir = test.home_path().join("sessions"); let path = find_session_file_containing_marker(&sessions_dir, &marker) .expect("no session file found after first run"); let session_id = extract_conversation_id(&path); @@ -166,11 +156,7 @@ fn exec_resume_by_id_appends_to_existing_file() -> anyhow::Result<()> { let marker2 = format!("resume-by-id-2-{}", Uuid::new_v4()); let prompt2 = format!("echo {marker2}"); - let mut binding = assert_cmd::Command::cargo_bin("codex-exec") - .context("should find binary for codex-exec")?; - let cmd = binding - .env("CODEX_HOME", home.path()) - .env("OPENAI_API_KEY", "dummy") + test.cmd() .env("CODEX_RS_SSE_FIXTURE", &fixture) .env("OPENAI_BASE_URL", "http://unused.local") .arg("--skip-git-repo-check") @@ -178,8 +164,9 @@ fn exec_resume_by_id_appends_to_existing_file() -> anyhow::Result<()> { .arg(env!("CARGO_MANIFEST_DIR")) .arg(&prompt2) .arg("resume") - .arg(&session_id); - cmd.assert().success(); + .arg(&session_id) + .assert() + .success(); let resumed_path = find_session_file_containing_marker(&sessions_dir, &marker2) .expect("no resumed session file containing marker2"); @@ -195,17 +182,14 @@ fn exec_resume_by_id_appends_to_existing_file() -> anyhow::Result<()> { #[test] fn exec_resume_preserves_cli_configuration_overrides() -> anyhow::Result<()> { - let home = TempDir::new()?; - let fixture = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("tests/fixtures/cli_responses_fixture.sse"); + let test = test_codex_exec(); + let fixture = + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/cli_responses_fixture.sse"); let marker = format!("resume-config-{}", Uuid::new_v4()); let prompt = format!("echo {marker}"); - Command::cargo_bin("codex-exec") - .context("should find binary for codex-exec")? - .env("CODEX_HOME", home.path()) - .env("OPENAI_API_KEY", "dummy") + test.cmd() .env("CODEX_RS_SSE_FIXTURE", &fixture) .env("OPENAI_BASE_URL", "http://unused.local") .arg("--skip-git-repo-check") @@ -219,17 +203,15 @@ fn exec_resume_preserves_cli_configuration_overrides() -> anyhow::Result<()> { .assert() .success(); - let sessions_dir = home.path().join("sessions"); + let sessions_dir = test.home_path().join("sessions"); let path = find_session_file_containing_marker(&sessions_dir, &marker) .expect("no session file found after first run"); let marker2 = format!("resume-config-2-{}", Uuid::new_v4()); let prompt2 = format!("echo {marker2}"); - let output = Command::cargo_bin("codex-exec") - .context("should find binary for codex-exec")? - .env("CODEX_HOME", home.path()) - .env("OPENAI_API_KEY", "dummy") + let output = test + .cmd() .env("CODEX_RS_SSE_FIXTURE", &fixture) .env("OPENAI_BASE_URL", "http://unused.local") .arg("--skip-git-repo-check") diff --git a/codex-rs/login/src/lib.rs b/codex-rs/login/src/lib.rs index df4fc1e76a..0a64c61e4e 100644 --- a/codex-rs/login/src/lib.rs +++ b/codex-rs/login/src/lib.rs @@ -14,6 +14,7 @@ pub use codex_core::AuthManager; pub use codex_core::CodexAuth; pub use codex_core::auth::AuthDotJson; pub use codex_core::auth::CLIENT_ID; +pub use codex_core::auth::CODEX_API_KEY_ENV_VAR; pub use codex_core::auth::OPENAI_API_KEY_ENV_VAR; pub use codex_core::auth::get_auth_file; pub use codex_core::auth::login_with_api_key; diff --git a/codex-rs/mcp-server/src/message_processor.rs b/codex-rs/mcp-server/src/message_processor.rs index 039375b05d..418c2a5162 100644 --- a/codex-rs/mcp-server/src/message_processor.rs +++ b/codex-rs/mcp-server/src/message_processor.rs @@ -52,7 +52,7 @@ impl MessageProcessor { config: Arc, ) -> Self { let outgoing = Arc::new(outgoing); - let auth_manager = AuthManager::shared(config.codex_home.clone()); + let auth_manager = AuthManager::shared(config.codex_home.clone(), false); let conversation_manager = Arc::new(ConversationManager::new(auth_manager)); Self { outgoing, diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index 8130d64796..d8dde74f06 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -361,7 +361,7 @@ async fn run_ratatui_app( // Initialize high-fidelity session event logging if enabled. session_log::maybe_init(&config); - let auth_manager = AuthManager::shared(config.codex_home.clone()); + let auth_manager = AuthManager::shared(config.codex_home.clone(), false); let login_status = get_login_status(&config); let should_show_onboarding = should_show_onboarding(login_status, &config, should_show_trust_screen); diff --git a/sdk/typescript/src/exec.ts b/sdk/typescript/src/exec.ts index 91ce763909..a20d1c4db1 100644 --- a/sdk/typescript/src/exec.ts +++ b/sdk/typescript/src/exec.ts @@ -58,7 +58,7 @@ export class CodexExec { env.OPENAI_BASE_URL = args.baseUrl; } if (args.apiKey) { - env.OPENAI_API_KEY = args.apiKey; + env.CODEX_API_KEY = args.apiKey; } const child = spawn(this.executablePath, commandArgs, { From b727d3f98aba0eecccfdd4af866bad83c2c4b9c0 Mon Sep 17 00:00:00 2001 From: Marcus Griep Date: Thu, 2 Oct 2025 11:05:51 -0600 Subject: [PATCH 03/16] fix: handle JSON Schema in additionalProperties for MCP tools (#4454) Fixes #4176 Some common tools provide a schema (even if just an empty object schema) as the value for `additionalProperties`. The parsing as it currently stands fails when it encounters this. This PR updates the schema to accept a schema object in addition to a boolean value, per the JSON Schema spec. --- .../core/src/exec_command/responses_api.rs | 4 +- codex-rs/core/src/openai_tools.rs | 153 +++++++++++++++++- codex-rs/core/src/plan_tool.rs | 4 +- codex-rs/core/src/tool_apply_patch.rs | 2 +- 4 files changed, 153 insertions(+), 10 deletions(-) diff --git a/codex-rs/core/src/exec_command/responses_api.rs b/codex-rs/core/src/exec_command/responses_api.rs index 70b90dd425..10629f4301 100644 --- a/codex-rs/core/src/exec_command/responses_api.rs +++ b/codex-rs/core/src/exec_command/responses_api.rs @@ -49,7 +49,7 @@ pub fn create_exec_command_tool_for_responses_api() -> ResponsesApiTool { parameters: JsonSchema::Object { properties, required: Some(vec!["cmd".to_string()]), - additional_properties: Some(false), + additional_properties: Some(false.into()), }, } } @@ -92,7 +92,7 @@ Can write control characters (\u0003 for Ctrl-C), or an empty string to just pol parameters: JsonSchema::Object { properties, required: Some(vec!["session_id".to_string(), "chars".to_string()]), - additional_properties: Some(false), + additional_properties: Some(false.into()), }, } } diff --git a/codex-rs/core/src/openai_tools.rs b/codex-rs/core/src/openai_tools.rs index 48dca79671..9e9b9388c7 100644 --- a/codex-rs/core/src/openai_tools.rs +++ b/codex-rs/core/src/openai_tools.rs @@ -122,6 +122,26 @@ impl ToolsConfig { } } +/// Whether additional properties are allowed, and if so, any required schema +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(untagged)] +pub(crate) enum AdditionalProperties { + Boolean(bool), + Schema(Box), +} + +impl From for AdditionalProperties { + fn from(b: bool) -> Self { + Self::Boolean(b) + } +} + +impl From for AdditionalProperties { + fn from(s: JsonSchema) -> Self { + Self::Schema(Box::new(s)) + } +} + /// Generic JSON‑Schema subset needed for our tool definitions #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[serde(tag = "type", rename_all = "lowercase")] @@ -154,7 +174,7 @@ pub(crate) enum JsonSchema { rename = "additionalProperties", skip_serializing_if = "Option::is_none" )] - additional_properties: Option, + additional_properties: Option, }, } @@ -200,7 +220,7 @@ fn create_unified_exec_tool() -> OpenAiTool { parameters: JsonSchema::Object { properties, required: Some(vec!["input".to_string()]), - additional_properties: Some(false), + additional_properties: Some(false.into()), }, }) } @@ -247,7 +267,7 @@ fn create_shell_tool() -> OpenAiTool { parameters: JsonSchema::Object { properties, required: Some(vec!["command".to_string()]), - additional_properties: Some(false), + additional_properties: Some(false.into()), }, }) } @@ -271,7 +291,7 @@ fn create_view_image_tool() -> OpenAiTool { parameters: JsonSchema::Object { properties, required: Some(vec!["path".to_string()]), - additional_properties: Some(false), + additional_properties: Some(false.into()), }, }) } @@ -708,7 +728,130 @@ mod tests { "string_property".to_string(), "number_property".to_string(), ]), - additional_properties: Some(false), + additional_properties: Some(false.into()), + }, + ), + ]), + required: None, + additional_properties: None, + }, + description: "Do something cool".to_string(), + strict: false, + }) + ); + } + + #[test] + fn test_get_openai_tools_mcp_tools_with_additional_properties_schema() { + let model_family = find_family_for_model("o3").expect("o3 should be a valid model family"); + let config = ToolsConfig::new(&ToolsConfigParams { + model_family: &model_family, + include_plan_tool: false, + include_apply_patch_tool: false, + include_web_search_request: true, + use_streamable_shell_tool: false, + include_view_image_tool: true, + experimental_unified_exec_tool: true, + }); + let tools = get_openai_tools( + &config, + Some(HashMap::from([( + "test_server/do_something_cool".to_string(), + mcp_types::Tool { + name: "do_something_cool".to_string(), + input_schema: ToolInputSchema { + properties: Some(serde_json::json!({ + "string_argument": { + "type": "string", + }, + "number_argument": { + "type": "number", + }, + "object_argument": { + "type": "object", + "properties": { + "string_property": { "type": "string" }, + "number_property": { "type": "number" }, + }, + "required": [ + "string_property", + "number_property", + ], + "additionalProperties": { + "type": "object", + "properties": { + "addtl_prop": { "type": "string" }, + }, + "required": [ + "addtl_prop", + ], + "additionalProperties": false, + }, + }, + })), + required: None, + r#type: "object".to_string(), + }, + output_schema: None, + title: None, + annotations: None, + description: Some("Do something cool".to_string()), + }, + )])), + ); + + assert_eq_tool_names( + &tools, + &[ + "unified_exec", + "web_search", + "view_image", + "test_server/do_something_cool", + ], + ); + + assert_eq!( + tools[3], + OpenAiTool::Function(ResponsesApiTool { + name: "test_server/do_something_cool".to_string(), + parameters: JsonSchema::Object { + properties: BTreeMap::from([ + ( + "string_argument".to_string(), + JsonSchema::String { description: None } + ), + ( + "number_argument".to_string(), + JsonSchema::Number { description: None } + ), + ( + "object_argument".to_string(), + JsonSchema::Object { + properties: BTreeMap::from([ + ( + "string_property".to_string(), + JsonSchema::String { description: None } + ), + ( + "number_property".to_string(), + JsonSchema::Number { description: None } + ), + ]), + required: Some(vec![ + "string_property".to_string(), + "number_property".to_string(), + ]), + additional_properties: Some( + JsonSchema::Object { + properties: BTreeMap::from([( + "addtl_prop".to_string(), + JsonSchema::String { description: None } + ),]), + required: Some(vec!["addtl_prop".to_string(),]), + additional_properties: Some(false.into()), + } + .into() + ), }, ), ]), diff --git a/codex-rs/core/src/plan_tool.rs b/codex-rs/core/src/plan_tool.rs index c81675698f..e0fdb56529 100644 --- a/codex-rs/core/src/plan_tool.rs +++ b/codex-rs/core/src/plan_tool.rs @@ -32,7 +32,7 @@ pub(crate) static PLAN_TOOL: LazyLock = LazyLock::new(|| { items: Box::new(JsonSchema::Object { properties: plan_item_props, required: Some(vec!["step".to_string(), "status".to_string()]), - additional_properties: Some(false), + additional_properties: Some(false.into()), }), }; @@ -54,7 +54,7 @@ At most one step can be in_progress at a time. parameters: JsonSchema::Object { properties, required: Some(vec!["plan".to_string()]), - additional_properties: Some(false), + additional_properties: Some(false.into()), }, }) }); diff --git a/codex-rs/core/src/tool_apply_patch.rs b/codex-rs/core/src/tool_apply_patch.rs index 164a1e127f..5f34b0d229 100644 --- a/codex-rs/core/src/tool_apply_patch.rs +++ b/codex-rs/core/src/tool_apply_patch.rs @@ -116,7 +116,7 @@ It is important to remember: parameters: JsonSchema::Object { properties, required: Some(vec!["input".to_string()]), - additional_properties: Some(false), + additional_properties: Some(false.into()), }, }) } From b07aafa5f55b74eadaaff404d871f4dc7694a389 Mon Sep 17 00:00:00 2001 From: dedrisian-oai Date: Thu, 2 Oct 2025 10:27:10 -0700 Subject: [PATCH 04/16] Fix status usage ratio (#4584) 1. Removes "Token usage" line for chatgpt sub users 2. Adds the word "used" to the context window line --- codex-rs/tui/src/status/card.rs | 7 +++++-- ...tus__tests__status_snapshot_includes_monthly_limit.snap | 2 +- ..._tests__status_snapshot_includes_reasoning_details.snap | 2 +- ..._tests__status_snapshot_shows_empty_limits_message.snap | 2 +- ...ests__status_snapshot_shows_missing_limits_message.snap | 2 +- ...ests__status_snapshot_truncates_in_narrow_terminal.snap | 2 +- codex-rs/tui/src/status/tests.rs | 2 +- 7 files changed, 11 insertions(+), 8 deletions(-) diff --git a/codex-rs/tui/src/status/card.rs b/codex-rs/tui/src/status/card.rs index 2674fc3e07..4832713bf9 100644 --- a/codex-rs/tui/src/status/card.rs +++ b/codex-rs/tui/src/status/card.rs @@ -152,7 +152,7 @@ impl StatusHistoryCell { Span::from(format!("{percent}% left")), Span::from(" (").dim(), Span::from(used_fmt).dim(), - Span::from(" / ").dim(), + Span::from(" used / ").dim(), Span::from(window_fmt).dim(), Span::from(")").dim(), ]) @@ -302,7 +302,10 @@ impl HistoryCell for StatusHistoryCell { } lines.push(Line::from(Vec::>::new())); - lines.push(formatter.line("Token usage", self.token_usage_spans())); + // Hide token usage only for ChatGPT subscribers + if !matches!(self.account, Some(StatusAccountDisplay::ChatGpt { .. })) { + lines.push(formatter.line("Token usage", self.token_usage_spans())); + } if let Some(spans) = self.context_window_spans() { lines.push(formatter.line("Context window", spans)); 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 73767d986b..cf3f492eef 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 @@ -14,6 +14,6 @@ expression: sanitized │ Agents.md: │ │ │ │ Token usage: 1.2K total (800 input + 400 output) │ -│ Context window: 100% left (1.2K / 272K) │ +│ Context window: 100% left (1.2K used / 272K) │ │ Monthly limit: [██░░░░░░░░░░░░░░░░░░] 12% used (resets 07:08 on 7 May) │ ╰────────────────────────────────────────────────────────────────────────────╯ 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 20904760cf..505375eae5 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 @@ -14,7 +14,7 @@ expression: sanitized │ Agents.md: │ │ │ │ Token usage: 1.9K total (1K input + 900 output) │ -│ Context window: 100% left (2.1K / 272K) │ +│ Context window: 100% left (2.1K used / 272K) │ │ 5h limit: [███████████████░░░░░] 72% used (resets 03:14) │ │ Weekly limit: [█████████░░░░░░░░░░░] 45% used (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 0ac26c5d54..472df543d6 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 @@ -14,6 +14,6 @@ expression: sanitized │ Agents.md: │ │ │ │ Token usage: 750 total (500 input + 250 output) │ -│ Context window: 100% left (750 / 272K) │ +│ 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 d59140a355..66bcfa3e21 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 @@ -14,6 +14,6 @@ expression: sanitized │ Agents.md: │ │ │ │ Token usage: 750 total (500 input + 250 output) │ -│ Context window: 100% left (750 / 272K) │ +│ Context window: 100% left (750 used / 272K) │ │ Limits: send a message to load usage data │ ╰─────────────────────────────────────────────────────────────────╯ 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 fd429dd940..b7c595340d 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 @@ -14,7 +14,7 @@ expression: sanitized │ Agents.md: │ │ │ │ Token usage: 1.9K total (1K input + │ -│ Context window: 100% left (2.1K / 272K) │ +│ Context window: 100% left (2.1K used / │ │ 5h limit: [███████████████░░░░░] │ │ (resets 03:14) │ ╰────────────────────────────────────────────╯ diff --git a/codex-rs/tui/src/status/tests.rs b/codex-rs/tui/src/status/tests.rs index eff8d8f47f..395d952f41 100644 --- a/codex-rs/tui/src/status/tests.rs +++ b/codex-rs/tui/src/status/tests.rs @@ -314,7 +314,7 @@ fn status_context_window_uses_last_usage() { .expect("context line"); assert!( - context_line.contains("13.7K / 272K"), + context_line.contains("13.7K used / 272K"), "expected context line to reflect last usage tokens, got: {context_line}" ); assert!( From ec98445abf602e54c0399b7dd61da14de286b8a6 Mon Sep 17 00:00:00 2001 From: Jeremy Rose <172423086+nornagon-openai@users.noreply.github.com> Date: Thu, 2 Oct 2025 11:34:47 -0700 Subject: [PATCH 05/16] normalize key hints (#4586) render key hints the same everywhere. | Before | After | |--------|-------| | Screenshot 2025-10-01 at 5 15 42
PM | Screenshot 2025-10-01 at 5 13 56
PM | | Screenshot 2025-10-01 at 5 17 01
PM | Screenshot 2025-10-01 at 5 14 24
PM | | Screenshot 2025-10-01 at 5 17 29
PM | Screenshot 2025-10-01 at 5 14 37
PM | | Screenshot 2025-10-01 at 5 17 40
PM | Screenshot 2025-10-01 at 5 14 56
PM | --- .../tui/src/bottom_pane/approval_overlay.rs | 9 +- .../tui/src/bottom_pane/custom_prompt_view.rs | 4 +- codex-rs/tui/src/bottom_pane/footer.rs | 153 ++++++++---------- .../src/bottom_pane/list_selection_view.rs | 12 +- codex-rs/tui/src/bottom_pane/popup_consts.rs | 15 +- ...__tests__footer_mode_shortcut_overlay.snap | 1 - ...tests__footer_shortcuts_shift_and_esc.snap | 1 - ..._list_selection_spacing_with_subtitle.snap | 2 +- ...st_selection_spacing_without_subtitle.snap | 2 +- codex-rs/tui/src/chatwidget.rs | 14 +- ...hatwidget__tests__approval_modal_exec.snap | 2 +- ..._tests__approval_modal_exec_no_reason.snap | 2 +- ...atwidget__tests__approval_modal_patch.snap | 2 +- ...twidget__tests__chat_small_running_h2.snap | 2 +- ...exec_and_status_layout_vt100_snapshot.snap | 2 +- ...dget__tests__exec_approval_modal_exec.snap | 4 +- ...atwidget__tests__status_widget_active.snap | 2 +- ...sts__status_widget_and_approval_modal.snap | 2 +- codex-rs/tui/src/key_hint.rs | 95 +++++++++-- codex-rs/tui/src/pager_overlay.rs | 115 +++++-------- codex-rs/tui/src/resume_picker.rs | 23 +-- ..._tests__static_overlay_snapshot_basic.snap | 4 +- ...ript_overlay_apply_patch_scroll_vt100.snap | 4 +- ...ts__transcript_overlay_snapshot_basic.snap | 4 +- ...ator_widget__tests__renders_truncated.snap | 2 +- ...__tests__renders_with_queued_messages.snap | 4 +- ...t__tests__renders_with_working_header.snap | 2 +- codex-rs/tui/src/status_indicator_widget.rs | 13 +- 28 files changed, 270 insertions(+), 227 deletions(-) diff --git a/codex-rs/tui/src/bottom_pane/approval_overlay.rs b/codex-rs/tui/src/bottom_pane/approval_overlay.rs index 511039df4a..d85e5db57f 100644 --- a/codex-rs/tui/src/bottom_pane/approval_overlay.rs +++ b/codex-rs/tui/src/bottom_pane/approval_overlay.rs @@ -11,6 +11,7 @@ use crate::bottom_pane::list_selection_view::SelectionViewParams; use crate::diff_render::DiffSummary; use crate::exec_command::strip_bash_lc_and_escape; use crate::history_cell; +use crate::key_hint; use crate::render::highlight::highlight_bash_to_lines; use crate::render::renderable::ColumnRenderable; use crate::render::renderable::Renderable; @@ -116,7 +117,13 @@ impl ApprovalOverlay { .collect(); let params = SelectionViewParams { - footer_hint: Some("Press Enter to confirm or Esc to cancel".to_string()), + footer_hint: Some(Line::from(vec![ + "Press ".into(), + key_hint::plain(KeyCode::Enter).into(), + " to confirm or ".into(), + key_hint::plain(KeyCode::Esc).into(), + " to cancel".into(), + ])), items, header, ..Default::default() diff --git a/codex-rs/tui/src/bottom_pane/custom_prompt_view.rs b/codex-rs/tui/src/bottom_pane/custom_prompt_view.rs index 07bd920384..54f474fb1f 100644 --- a/codex-rs/tui/src/bottom_pane/custom_prompt_view.rs +++ b/codex-rs/tui/src/bottom_pane/custom_prompt_view.rs @@ -14,7 +14,7 @@ use std::cell::RefCell; use crate::render::renderable::Renderable; -use super::popup_consts::STANDARD_POPUP_HINT_LINE; +use super::popup_consts::standard_popup_hint_line; use super::CancellationEvent; use super::bottom_pane_view::BottomPaneView; @@ -221,7 +221,7 @@ impl Renderable for CustomPromptView { let hint_y = hint_blank_y.saturating_add(1); if hint_y < area.y.saturating_add(area.height) { - Paragraph::new(STANDARD_POPUP_HINT_LINE).render( + Paragraph::new(standard_popup_hint_line()).render( Rect { x: area.x, y: hint_y, diff --git a/codex-rs/tui/src/bottom_pane/footer.rs b/codex-rs/tui/src/bottom_pane/footer.rs index 133c0b34bf..3fc9b2e1f4 100644 --- a/codex-rs/tui/src/bottom_pane/footer.rs +++ b/codex-rs/tui/src/bottom_pane/footer.rs @@ -1,13 +1,15 @@ +use crate::key_hint; +use crate::key_hint::KeyBinding; +use crate::render::line_utils::prefix_lines; use crate::ui_consts::FOOTER_INDENT_COLS; use crossterm::event::KeyCode; -use crossterm::event::KeyModifiers; use ratatui::buffer::Buffer; use ratatui::layout::Rect; use ratatui::style::Stylize; use ratatui::text::Line; use ratatui::text::Span; -use ratatui::widgets::WidgetRef; -use std::iter; +use ratatui::widgets::Paragraph; +use ratatui::widgets::Widget; #[derive(Clone, Copy, Debug)] pub(crate) struct FooterProps { @@ -61,15 +63,12 @@ pub(crate) fn footer_height(props: FooterProps) -> u16 { } pub(crate) fn render_footer(area: Rect, buf: &mut Buffer, props: FooterProps) { - let lines = footer_lines(props); - for (idx, line) in lines.into_iter().enumerate() { - let y = area.y + idx as u16; - if y >= area.y + area.height { - break; - } - let row = Rect::new(area.x, y, area.width, 1); - line.render_ref(row, buf); - } + Paragraph::new(prefix_lines( + footer_lines(props), + " ".repeat(FOOTER_INDENT_COLS).into(), + " ".repeat(FOOTER_INDENT_COLS).into(), + )) + .render(area, buf); } fn footer_lines(props: FooterProps) -> Vec> { @@ -81,7 +80,10 @@ fn footer_lines(props: FooterProps) -> Vec> { if props.is_task_running { vec![context_window_line(props.context_window_percent)] } else { - vec![dim_line(indent_text("? for shortcuts"))] + vec![Line::from(vec![ + key_hint::plain(KeyCode::Char('?')).into(), + " for shortcuts".dim(), + ])] } } FooterMode::ShortcutOverlay => shortcut_overlay_lines(ShortcutsState { @@ -110,27 +112,36 @@ fn ctrl_c_reminder_line(state: CtrlCReminderState) -> Line<'static> { } else { "quit" }; - let text = format!("ctrl + c again to {action}"); - dim_line(indent_text(&text)) + Line::from(vec![ + key_hint::ctrl(KeyCode::Char('c')).into(), + format!(" again to {action}").into(), + ]) + .dim() } fn esc_hint_line(esc_backtrack_hint: bool) -> Line<'static> { - let text = if esc_backtrack_hint { - "esc again to edit previous message" + let esc = key_hint::plain(KeyCode::Esc); + if esc_backtrack_hint { + Line::from(vec![esc.into(), " again to edit previous message".into()]).dim() } else { - "esc esc to edit previous message" - }; - dim_line(indent_text(text)) + Line::from(vec![ + esc.into(), + " ".into(), + esc.into(), + " to edit previous message".into(), + ]) + .dim() + } } fn shortcut_overlay_lines(state: ShortcutsState) -> Vec> { - let mut commands = String::new(); - let mut newline = String::new(); - let mut file_paths = String::new(); - let mut paste_image = String::new(); - let mut edit_previous = String::new(); - let mut quit = String::new(); - let mut show_transcript = String::new(); + let mut commands = Line::from(""); + let mut newline = Line::from(""); + let mut file_paths = Line::from(""); + let mut paste_image = Line::from(""); + let mut edit_previous = Line::from(""); + let mut quit = Line::from(""); + let mut show_transcript = Line::from(""); for descriptor in SHORTCUTS { if let Some(text) = descriptor.overlay_entry(state) { @@ -153,14 +164,14 @@ fn shortcut_overlay_lines(state: ShortcutsState) -> Vec> { paste_image, edit_previous, quit, - String::new(), + Line::from(""), show_transcript, ]; build_columns(ordered) } -fn build_columns(entries: Vec) -> Vec> { +fn build_columns(entries: Vec>) -> Vec> { if entries.is_empty() { return Vec::new(); } @@ -174,7 +185,7 @@ fn build_columns(entries: Vec) -> Vec> { let mut entries = entries; if entries.len() < target_len { entries.extend(std::iter::repeat_n( - String::new(), + Line::from(""), target_len - entries.len(), )); } @@ -183,7 +194,7 @@ fn build_columns(entries: Vec) -> Vec> { for (idx, entry) in entries.iter().enumerate() { let column = idx % COLUMNS; - column_widths[column] = column_widths[column].max(entry.len()); + column_widths[column] = column_widths[column].max(entry.width()); } for (idx, width) in column_widths.iter_mut().enumerate() { @@ -193,42 +204,30 @@ fn build_columns(entries: Vec) -> Vec> { entries .chunks(COLUMNS) .map(|chunk| { - let mut line = String::new(); + let mut line = Line::from(""); for (col, entry) in chunk.iter().enumerate() { - line.push_str(entry); + line.extend(entry.spans.clone()); if col < COLUMNS - 1 { let target_width = column_widths[col]; - let padding = target_width.saturating_sub(entry.len()) + COLUMN_GAP; - line.push_str(&" ".repeat(padding)); + let padding = target_width.saturating_sub(entry.width()) + COLUMN_GAP; + line.push_span(Span::from(" ".repeat(padding))); } } - let indented = indent_text(&line); - dim_line(indented) + line.dim() }) .collect() } -fn indent_text(text: &str) -> String { - let mut indented = String::with_capacity(FOOTER_INDENT_COLS + text.len()); - indented.extend(iter::repeat_n(' ', FOOTER_INDENT_COLS)); - indented.push_str(text); - indented -} - -fn dim_line(text: String) -> Line<'static> { - Line::from(text).dim() -} - fn context_window_line(percent: Option) -> Line<'static> { let mut spans: Vec> = Vec::new(); - spans.push(indent_text("").into()); match percent { Some(percent) => { spans.push(format!("{percent}%").bold()); spans.push(" context left".dim()); } None => { - spans.push("? for shortcuts".dim()); + spans.push(key_hint::plain(KeyCode::Char('?')).into()); + spans.push(" for shortcuts".dim()); } } Line::from(spans) @@ -247,9 +246,7 @@ enum ShortcutId { #[derive(Clone, Copy, Debug, Eq, PartialEq)] struct ShortcutBinding { - code: KeyCode, - modifiers: KeyModifiers, - overlay_text: &'static str, + key: KeyBinding, condition: DisplayCondition, } @@ -288,20 +285,24 @@ impl ShortcutDescriptor { self.bindings.iter().find(|binding| binding.matches(state)) } - fn overlay_entry(&self, state: ShortcutsState) -> Option { + fn overlay_entry(&self, state: ShortcutsState) -> Option> { let binding = self.binding_for(state)?; - let label = match self.id { + let mut line = Line::from(vec![self.prefix.into(), binding.key.into()]); + match self.id { ShortcutId::EditPrevious => { if state.esc_backtrack_hint { - " again to edit previous message" + line.push_span(" again to edit previous message"); } else { - " esc to edit previous message" + line.extend(vec![ + " ".into(), + key_hint::plain(KeyCode::Esc).into(), + " to edit previous message".into(), + ]); } } - _ => self.label, + _ => line.push_span(self.label), }; - let text = format!("{}{}{}", self.prefix, binding.overlay_text, label); - Some(text) + Some(line) } } @@ -309,9 +310,7 @@ const SHORTCUTS: &[ShortcutDescriptor] = &[ ShortcutDescriptor { id: ShortcutId::Commands, bindings: &[ShortcutBinding { - code: KeyCode::Char('/'), - modifiers: KeyModifiers::NONE, - overlay_text: "/", + key: key_hint::plain(KeyCode::Char('/')), condition: DisplayCondition::Always, }], prefix: "", @@ -321,15 +320,11 @@ const SHORTCUTS: &[ShortcutDescriptor] = &[ id: ShortcutId::InsertNewline, bindings: &[ ShortcutBinding { - code: KeyCode::Enter, - modifiers: KeyModifiers::SHIFT, - overlay_text: "shift + enter", + key: key_hint::shift(KeyCode::Enter), condition: DisplayCondition::WhenShiftEnterHint, }, ShortcutBinding { - code: KeyCode::Char('j'), - modifiers: KeyModifiers::CONTROL, - overlay_text: "ctrl + j", + key: key_hint::ctrl(KeyCode::Char('j')), condition: DisplayCondition::WhenNotShiftEnterHint, }, ], @@ -339,9 +334,7 @@ const SHORTCUTS: &[ShortcutDescriptor] = &[ ShortcutDescriptor { id: ShortcutId::FilePaths, bindings: &[ShortcutBinding { - code: KeyCode::Char('@'), - modifiers: KeyModifiers::NONE, - overlay_text: "@", + key: key_hint::plain(KeyCode::Char('@')), condition: DisplayCondition::Always, }], prefix: "", @@ -350,9 +343,7 @@ const SHORTCUTS: &[ShortcutDescriptor] = &[ ShortcutDescriptor { id: ShortcutId::PasteImage, bindings: &[ShortcutBinding { - code: KeyCode::Char('v'), - modifiers: KeyModifiers::CONTROL, - overlay_text: "ctrl + v", + key: key_hint::ctrl(KeyCode::Char('v')), condition: DisplayCondition::Always, }], prefix: "", @@ -361,9 +352,7 @@ const SHORTCUTS: &[ShortcutDescriptor] = &[ ShortcutDescriptor { id: ShortcutId::EditPrevious, bindings: &[ShortcutBinding { - code: KeyCode::Esc, - modifiers: KeyModifiers::NONE, - overlay_text: "esc", + key: key_hint::plain(KeyCode::Esc), condition: DisplayCondition::Always, }], prefix: "", @@ -372,9 +361,7 @@ const SHORTCUTS: &[ShortcutDescriptor] = &[ ShortcutDescriptor { id: ShortcutId::Quit, bindings: &[ShortcutBinding { - code: KeyCode::Char('c'), - modifiers: KeyModifiers::CONTROL, - overlay_text: "ctrl + c", + key: key_hint::ctrl(KeyCode::Char('c')), condition: DisplayCondition::Always, }], prefix: "", @@ -383,9 +370,7 @@ const SHORTCUTS: &[ShortcutDescriptor] = &[ ShortcutDescriptor { id: ShortcutId::ShowTranscript, bindings: &[ShortcutBinding { - code: KeyCode::Char('t'), - modifiers: KeyModifiers::CONTROL, - overlay_text: "ctrl + t", + key: key_hint::ctrl(KeyCode::Char('t')), condition: DisplayCondition::Always, }], prefix: "", diff --git a/codex-rs/tui/src/bottom_pane/list_selection_view.rs b/codex-rs/tui/src/bottom_pane/list_selection_view.rs index e2614885a0..cb3de2122c 100644 --- a/codex-rs/tui/src/bottom_pane/list_selection_view.rs +++ b/codex-rs/tui/src/bottom_pane/list_selection_view.rs @@ -43,7 +43,7 @@ pub(crate) struct SelectionItem { pub(crate) struct SelectionViewParams { pub title: Option, pub subtitle: Option, - pub footer_hint: Option, + pub footer_hint: Option>, pub items: Vec, pub is_searchable: bool, pub search_placeholder: Option, @@ -65,7 +65,7 @@ impl Default for SelectionViewParams { } pub(crate) struct ListSelectionView { - footer_hint: Option, + footer_hint: Option>, items: Vec, state: ScrollState, complete: bool, @@ -416,7 +416,7 @@ impl Renderable for ListSelectionView { width: footer_area.width.saturating_sub(2), height: footer_area.height, }; - Line::from(hint.clone().dim()).render(hint_area, buf); + hint.clone().dim().render(hint_area, buf); } } } @@ -425,7 +425,7 @@ impl Renderable for ListSelectionView { mod tests { use super::*; use crate::app_event::AppEvent; - use crate::bottom_pane::popup_consts::STANDARD_POPUP_HINT_LINE; + use crate::bottom_pane::popup_consts::standard_popup_hint_line; use insta::assert_snapshot; use ratatui::layout::Rect; use tokio::sync::mpsc::unbounded_channel; @@ -455,7 +455,7 @@ mod tests { SelectionViewParams { title: Some("Select Approval Mode".to_string()), subtitle: subtitle.map(str::to_string), - footer_hint: Some(STANDARD_POPUP_HINT_LINE.to_string()), + footer_hint: Some(standard_popup_hint_line()), items, ..Default::default() }, @@ -517,7 +517,7 @@ mod tests { let mut view = ListSelectionView::new( SelectionViewParams { title: Some("Select Approval Mode".to_string()), - footer_hint: Some(STANDARD_POPUP_HINT_LINE.to_string()), + footer_hint: Some(standard_popup_hint_line()), items, is_searchable: true, search_placeholder: Some("Type to search branches".to_string()), diff --git a/codex-rs/tui/src/bottom_pane/popup_consts.rs b/codex-rs/tui/src/bottom_pane/popup_consts.rs index 5147b2ee5f..2cabe389b1 100644 --- a/codex-rs/tui/src/bottom_pane/popup_consts.rs +++ b/codex-rs/tui/src/bottom_pane/popup_consts.rs @@ -1,8 +1,21 @@ //! Shared popup-related constants for bottom pane widgets. +use crossterm::event::KeyCode; +use ratatui::text::Line; + +use crate::key_hint; + /// Maximum number of rows any popup should attempt to display. /// Keep this consistent across all popups for a uniform feel. pub(crate) const MAX_POPUP_ROWS: usize = 8; /// Standard footer hint text used by popups. -pub(crate) const STANDARD_POPUP_HINT_LINE: &str = "Press Enter to confirm or Esc to go back"; +pub(crate) fn standard_popup_hint_line() -> Line<'static> { + Line::from(vec![ + "Press ".into(), + key_hint::plain(KeyCode::Enter).into(), + " to confirm or ".into(), + key_hint::plain(KeyCode::Esc).into(), + " to go back".into(), + ]) +} diff --git a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__chat_composer__tests__footer_mode_shortcut_overlay.snap b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__chat_composer__tests__footer_mode_shortcut_overlay.snap index 170dedc01e..3b6782d06d 100644 --- a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__chat_composer__tests__footer_mode_shortcut_overlay.snap +++ b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__chat_composer__tests__footer_mode_shortcut_overlay.snap @@ -1,6 +1,5 @@ --- source: tui/src/bottom_pane/chat_composer.rs -assertion_line: 1497 expression: terminal.backend() --- " " diff --git a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__footer__tests__footer_shortcuts_shift_and_esc.snap b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__footer__tests__footer_shortcuts_shift_and_esc.snap index ffa4c5b048..264515a6c2 100644 --- a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__footer__tests__footer_shortcuts_shift_and_esc.snap +++ b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__footer__tests__footer_shortcuts_shift_and_esc.snap @@ -1,6 +1,5 @@ --- source: tui/src/bottom_pane/footer.rs -assertion_line: 389 expression: terminal.backend() --- " / for commands shift + enter for newline " diff --git a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__list_selection_view__tests__list_selection_spacing_with_subtitle.snap b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__list_selection_view__tests__list_selection_spacing_with_subtitle.snap index 9cbfe88f78..512f6bbca6 100644 --- a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__list_selection_view__tests__list_selection_spacing_with_subtitle.snap +++ b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__list_selection_view__tests__list_selection_spacing_with_subtitle.snap @@ -9,4 +9,4 @@ expression: render_lines(&view) › 1. Read Only (current) Codex can read files 2. Full Access Codex can edit files - Press Enter to confirm or Esc to go back + Press enter to confirm or esc to go back diff --git a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__list_selection_view__tests__list_selection_spacing_without_subtitle.snap b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__list_selection_view__tests__list_selection_spacing_without_subtitle.snap index 5e3cf2c6e6..ddd0f90cd8 100644 --- a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__list_selection_view__tests__list_selection_spacing_without_subtitle.snap +++ b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__list_selection_view__tests__list_selection_spacing_without_subtitle.snap @@ -8,4 +8,4 @@ expression: render_lines(&view) › 1. Read Only (current) Codex can read files 2. Full Access Codex can edit files - Press Enter to confirm or Esc to go back + Press enter to confirm or esc to go back diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 9940c8f7d7..32566b3ecd 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -68,7 +68,7 @@ use crate::bottom_pane::SelectionAction; use crate::bottom_pane::SelectionItem; use crate::bottom_pane::SelectionViewParams; use crate::bottom_pane::custom_prompt_view::CustomPromptView; -use crate::bottom_pane::popup_consts::STANDARD_POPUP_HINT_LINE; +use crate::bottom_pane::popup_consts::standard_popup_hint_line; use crate::clipboard_paste::paste_image_to_temp_png; use crate::diff_render::display_path_for; use crate::exec_cell::CommandOutput; @@ -1625,7 +1625,7 @@ impl ChatWidget { subtitle: Some( "Switch between OpenAI models for this and future Codex CLI session".to_string(), ), - footer_hint: Some(STANDARD_POPUP_HINT_LINE.to_string()), + footer_hint: Some(standard_popup_hint_line()), items, ..Default::default() }); @@ -1668,7 +1668,7 @@ impl ChatWidget { self.bottom_pane.show_selection_view(SelectionViewParams { title: Some("Select Approval Mode".to_string()), - footer_hint: Some(STANDARD_POPUP_HINT_LINE.to_string()), + footer_hint: Some(standard_popup_hint_line()), items, ..Default::default() }); @@ -1843,7 +1843,7 @@ impl ChatWidget { self.bottom_pane.show_selection_view(SelectionViewParams { title: Some("Select a review preset".into()), - footer_hint: Some(STANDARD_POPUP_HINT_LINE.to_string()), + footer_hint: Some(standard_popup_hint_line()), items, ..Default::default() }); @@ -1879,7 +1879,7 @@ impl ChatWidget { self.bottom_pane.show_selection_view(SelectionViewParams { title: Some("Select a base branch".to_string()), - footer_hint: Some(STANDARD_POPUP_HINT_LINE.to_string()), + footer_hint: Some(standard_popup_hint_line()), items, is_searchable: true, search_placeholder: Some("Type to search branches".to_string()), @@ -1920,7 +1920,7 @@ impl ChatWidget { self.bottom_pane.show_selection_view(SelectionViewParams { title: Some("Select a commit to review".to_string()), - footer_hint: Some(STANDARD_POPUP_HINT_LINE.to_string()), + footer_hint: Some(standard_popup_hint_line()), items, is_searchable: true, search_placeholder: Some("Type to search commits".to_string()), @@ -2145,7 +2145,7 @@ pub(crate) fn show_review_commit_picker_with_entries( chat.bottom_pane.show_selection_view(SelectionViewParams { title: Some("Select a commit to review".to_string()), - footer_hint: Some(STANDARD_POPUP_HINT_LINE.to_string()), + footer_hint: Some(standard_popup_hint_line()), items, is_searchable: true, search_placeholder: Some("Type to search commits".to_string()), diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec.snap index 10ded3d33f..558728bd31 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec.snap @@ -13,4 +13,4 @@ expression: terminal.backend().vt100().screen().contents() rest of the session 3. Cancel Do not run the command - Press Enter to confirm or Esc to cancel + Press enter to confirm or esc to cancel diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec_no_reason.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec_no_reason.snap index c3e04bd158..53348af155 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec_no_reason.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec_no_reason.snap @@ -13,5 +13,5 @@ expression: terminal.backend() " rest of the session " " 3. Cancel Do not run the command " " " -" Press Enter to confirm or Esc to cancel " +" Press enter to confirm or esc to cancel " " " diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_patch.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_patch.snap index fd1860caf5..64fbada3be 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_patch.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_patch.snap @@ -15,5 +15,5 @@ expression: terminal.backend() "› 1. Approve Apply the proposed changes " " 2. Cancel Do not apply the changes " " " -" Press Enter to confirm or Esc to cancel " +" Press enter to confirm or esc to cancel " " " diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__chat_small_running_h2.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__chat_small_running_h2.snap index 5164cd455d..3b236fff13 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__chat_small_running_h2.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__chat_small_running_h2.snap @@ -2,5 +2,5 @@ source: tui/src/chatwidget/tests.rs expression: terminal.backend() --- -" Thinking (0s • Esc to interrupt) " +" Thinking (0s • esc to interrupt) " "› Ask Codex to do anything " diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__chatwidget_exec_and_status_layout_vt100_snapshot.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__chatwidget_exec_and_status_layout_vt100_snapshot.snap index e3f7f532a7..a05ef27991 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__chatwidget_exec_and_status_layout_vt100_snapshot.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__chatwidget_exec_and_status_layout_vt100_snapshot.snap @@ -9,7 +9,7 @@ expression: term.backend().vt100().screen().contents() └ Search Change Approved Read diff_render.rs - Investigating rendering code (0s • Esc to interrupt) + Investigating rendering code (0s • esc to interrupt) › Summarize recent commits diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__exec_approval_modal_exec.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__exec_approval_modal_exec.snap index 5beb5323a2..8b7c5838cc 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__exec_approval_modal_exec.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__exec_approval_modal_exec.snap @@ -18,7 +18,7 @@ Buffer { " rest of the session ", " 3. Cancel Do not run the command ", " ", - " Press Enter to confirm or Esc to cancel ", + " Press enter to confirm or esc to cancel ", " ", ], styles: [ @@ -37,6 +37,6 @@ Buffer { x: 34, y: 11, fg: Reset, bg: Reset, underline: Reset, modifier: DIM, x: 56, y: 11, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, x: 2, y: 13, fg: Reset, bg: Reset, underline: Reset, modifier: DIM, - x: 41, y: 13, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, + x: 0, y: 14, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, ] } diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_active.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_active.snap index e4ae90ba90..465774602c 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_active.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_active.snap @@ -3,7 +3,7 @@ source: tui/src/chatwidget/tests.rs expression: terminal.backend() --- " " -" Analyzing (0s • Esc to interrupt) " +" Analyzing (0s • esc to interrupt) " " " " " "› Ask Codex to do anything " diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_and_approval_modal.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_and_approval_modal.snap index 86d89b80f5..96b51f7a36 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_and_approval_modal.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_and_approval_modal.snap @@ -15,5 +15,5 @@ expression: terminal.backend() " rest of the session " " 3. Cancel Do not run the command " " " -" Press Enter to confirm or Esc to cancel " +" Press enter to confirm or esc to cancel " " " diff --git a/codex-rs/tui/src/key_hint.rs b/codex-rs/tui/src/key_hint.rs index f68a493e9c..b79c2a2703 100644 --- a/codex-rs/tui/src/key_hint.rs +++ b/codex-rs/tui/src/key_hint.rs @@ -1,23 +1,86 @@ +use crossterm::event::KeyCode; +use crossterm::event::KeyEvent; +use crossterm::event::KeyEventKind; +use crossterm::event::KeyModifiers; use ratatui::style::Style; use ratatui::style::Stylize; use ratatui::text::Span; -use std::fmt::Display; -#[cfg(test)] -const ALT_PREFIX: &str = "⌥"; -#[cfg(all(not(test), target_os = "macos"))] -const ALT_PREFIX: &str = "⌥"; -#[cfg(all(not(test), not(target_os = "macos")))] -const ALT_PREFIX: &str = "Alt+"; +const ALT_PREFIX: &str = "alt + "; +const CTRL_PREFIX: &str = "ctrl + "; +const SHIFT_PREFIX: &str = "shift + "; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(crate) struct KeyBinding { + key: KeyCode, + modifiers: KeyModifiers, +} + +impl KeyBinding { + pub(crate) const fn new(key: KeyCode, modifiers: KeyModifiers) -> Self { + Self { key, modifiers } + } + + pub fn is_press(&self, event: KeyEvent) -> bool { + self.key == event.code + && self.modifiers == event.modifiers + && (event.kind == KeyEventKind::Press || event.kind == KeyEventKind::Repeat) + } +} + +pub(crate) const fn plain(key: KeyCode) -> KeyBinding { + KeyBinding::new(key, KeyModifiers::NONE) +} + +pub(crate) const fn alt(key: KeyCode) -> KeyBinding { + KeyBinding::new(key, KeyModifiers::ALT) +} + +pub(crate) const fn shift(key: KeyCode) -> KeyBinding { + KeyBinding::new(key, KeyModifiers::SHIFT) +} + +pub(crate) const fn ctrl(key: KeyCode) -> KeyBinding { + KeyBinding::new(key, KeyModifiers::CONTROL) +} + +fn modifiers_to_string(modifiers: KeyModifiers) -> String { + let mut result = String::new(); + if modifiers.contains(KeyModifiers::CONTROL) { + result.push_str(CTRL_PREFIX); + } + if modifiers.contains(KeyModifiers::SHIFT) { + result.push_str(SHIFT_PREFIX); + } + if modifiers.contains(KeyModifiers::ALT) { + result.push_str(ALT_PREFIX); + } + result +} + +impl From for Span<'static> { + fn from(binding: KeyBinding) -> Self { + (&binding).into() + } +} +impl From<&KeyBinding> for Span<'static> { + fn from(binding: &KeyBinding) -> Self { + let KeyBinding { key, modifiers } = binding; + let modifiers = modifiers_to_string(*modifiers); + let key = match key { + KeyCode::Enter => "enter".to_string(), + KeyCode::Up => "↑".to_string(), + KeyCode::Down => "↓".to_string(), + KeyCode::Left => "←".to_string(), + KeyCode::Right => "→".to_string(), + KeyCode::PageUp => "pgup".to_string(), + KeyCode::PageDown => "pgdn".to_string(), + _ => format!("{key}").to_ascii_lowercase(), + }; + Span::styled(format!("{modifiers}{key}"), key_hint_style()) + } +} fn key_hint_style() -> Style { - Style::default().bold() -} - -fn modifier_span(prefix: &str, key: impl Display) -> Span<'static> { - Span::styled(format!("{prefix}{key}"), key_hint_style()) -} - -pub(crate) fn alt(key: impl Display) -> Span<'static> { - modifier_span(ALT_PREFIX, key) + Style::default().dim() } diff --git a/codex-rs/tui/src/pager_overlay.rs b/codex-rs/tui/src/pager_overlay.rs index 6a0562fe82..7997625aae 100644 --- a/codex-rs/tui/src/pager_overlay.rs +++ b/codex-rs/tui/src/pager_overlay.rs @@ -3,18 +3,16 @@ use std::sync::Arc; use std::time::Duration; use crate::history_cell::HistoryCell; +use crate::key_hint; +use crate::key_hint::KeyBinding; use crate::render::renderable::Renderable; use crate::tui; use crate::tui::TuiEvent; use crossterm::event::KeyCode; use crossterm::event::KeyEvent; -use crossterm::event::KeyEventKind; use ratatui::buffer::Buffer; use ratatui::buffer::Cell; use ratatui::layout::Rect; -use ratatui::style::Color; -use ratatui::style::Style; -use ratatui::style::Styled; use ratatui::style::Stylize; use ratatui::text::Line; use ratatui::text::Span; @@ -61,23 +59,40 @@ impl Overlay { } } +const KEY_UP: KeyBinding = key_hint::plain(KeyCode::Up); +const KEY_DOWN: KeyBinding = key_hint::plain(KeyCode::Down); +const KEY_PAGE_UP: KeyBinding = key_hint::plain(KeyCode::PageUp); +const KEY_PAGE_DOWN: KeyBinding = key_hint::plain(KeyCode::PageDown); +const KEY_SPACE: KeyBinding = key_hint::plain(KeyCode::Char(' ')); +const KEY_HOME: KeyBinding = key_hint::plain(KeyCode::Home); +const KEY_END: KeyBinding = key_hint::plain(KeyCode::End); +const KEY_Q: KeyBinding = key_hint::plain(KeyCode::Char('q')); +const KEY_ESC: KeyBinding = key_hint::plain(KeyCode::Esc); +const KEY_ENTER: KeyBinding = key_hint::plain(KeyCode::Enter); +const KEY_CTRL_T: KeyBinding = key_hint::ctrl(KeyCode::Char('t')); +const KEY_CTRL_C: KeyBinding = key_hint::ctrl(KeyCode::Char('c')); + // Common pager navigation hints rendered on the first line -const PAGER_KEY_HINTS: &[(&str, &str)] = &[ - ("↑/↓", "scroll"), - ("PgUp/PgDn", "page"), - ("Home/End", "jump"), +const PAGER_KEY_HINTS: &[(&[KeyBinding], &str)] = &[ + (&[KEY_UP, KEY_DOWN], "to scroll"), + (&[KEY_PAGE_UP, KEY_PAGE_DOWN], "to page"), + (&[KEY_HOME, KEY_END], "to jump"), ]; -// Render a single line of key hints from (key, description) pairs. -fn render_key_hints(area: Rect, buf: &mut Buffer, pairs: &[(&str, &str)]) { - let key_hint_style = Style::default().fg(Color::Cyan); +// Render a single line of key hints from (key(s), description) pairs. +fn render_key_hints(area: Rect, buf: &mut Buffer, pairs: &[(&[KeyBinding], &str)]) { let mut spans: Vec> = vec![" ".into()]; let mut first = true; - for (key, desc) in pairs { + for (keys, desc) in pairs { if !first { spans.push(" ".into()); } - spans.push(Span::from(key.to_string()).set_style(key_hint_style)); + for (i, key) in keys.iter().enumerate() { + if i > 0 { + spans.push("/".into()); + } + spans.push(Span::from(key)); + } spans.push(" ".into()); spans.push(Span::from(desc.to_string())); first = false; @@ -214,48 +229,24 @@ impl PagerView { fn handle_key_event(&mut self, tui: &mut tui::Tui, key_event: KeyEvent) -> Result<()> { match key_event { - KeyEvent { - code: KeyCode::Up, - kind: KeyEventKind::Press | KeyEventKind::Repeat, - .. - } => { + e if KEY_UP.is_press(e) => { self.scroll_offset = self.scroll_offset.saturating_sub(1); } - KeyEvent { - code: KeyCode::Down, - kind: KeyEventKind::Press | KeyEventKind::Repeat, - .. - } => { + e if KEY_DOWN.is_press(e) => { self.scroll_offset = self.scroll_offset.saturating_add(1); } - KeyEvent { - code: KeyCode::PageUp, - kind: KeyEventKind::Press | KeyEventKind::Repeat, - .. - } => { + e if KEY_PAGE_UP.is_press(e) => { let area = self.content_area(tui.terminal.viewport_area); self.scroll_offset = self.scroll_offset.saturating_sub(area.height as usize); } - KeyEvent { - code: KeyCode::PageDown | KeyCode::Char(' '), - kind: KeyEventKind::Press | KeyEventKind::Repeat, - .. - } => { + e if KEY_PAGE_DOWN.is_press(e) || KEY_SPACE.is_press(e) => { let area = self.content_area(tui.terminal.viewport_area); self.scroll_offset = self.scroll_offset.saturating_add(area.height as usize); } - KeyEvent { - code: KeyCode::Home, - kind: KeyEventKind::Press | KeyEventKind::Repeat, - .. - } => { + e if KEY_HOME.is_press(e) => { self.scroll_offset = 0; } - KeyEvent { - code: KeyCode::End, - kind: KeyEventKind::Press | KeyEventKind::Repeat, - .. - } => { + e if KEY_END.is_press(e) => { self.scroll_offset = usize::MAX; } _ => { @@ -434,9 +425,11 @@ impl TranscriptOverlay { let line1 = Rect::new(area.x, area.y, area.width, 1); let line2 = Rect::new(area.x, area.y.saturating_add(1), area.width, 1); render_key_hints(line1, buf, PAGER_KEY_HINTS); - let mut pairs: Vec<(&str, &str)> = vec![("q", "quit"), ("Esc", "edit prev")]; + + let mut pairs: Vec<(&[KeyBinding], &str)> = + vec![(&[KEY_Q], "to quit"), (&[KEY_ESC], "to edit prev")]; if self.highlight_cell.is_some() { - pairs.push(("⏎", "edit message")); + pairs.push((&[KEY_ENTER], "to edit message")); } render_key_hints(line2, buf, &pairs); } @@ -454,23 +447,7 @@ impl TranscriptOverlay { pub(crate) fn handle_event(&mut self, tui: &mut tui::Tui, event: TuiEvent) -> Result<()> { match event { TuiEvent::Key(key_event) => match key_event { - KeyEvent { - code: KeyCode::Char('q'), - kind: KeyEventKind::Press, - .. - } - | KeyEvent { - code: KeyCode::Char('t'), - modifiers: crossterm::event::KeyModifiers::CONTROL, - kind: KeyEventKind::Press, - .. - } - | KeyEvent { - code: KeyCode::Char('c'), - modifiers: crossterm::event::KeyModifiers::CONTROL, - kind: KeyEventKind::Press, - .. - } => { + e if KEY_Q.is_press(e) || KEY_CTRL_C.is_press(e) || KEY_CTRL_T.is_press(e) => { self.is_done = true; Ok(()) } @@ -516,7 +493,7 @@ impl StaticOverlay { let line1 = Rect::new(area.x, area.y, area.width, 1); let line2 = Rect::new(area.x, area.y.saturating_add(1), area.width, 1); render_key_hints(line1, buf, PAGER_KEY_HINTS); - let pairs = [("q", "quit")]; + let pairs: Vec<(&[KeyBinding], &str)> = vec![(&[KEY_Q], "to quit")]; render_key_hints(line2, buf, &pairs); } @@ -533,17 +510,7 @@ impl StaticOverlay { pub(crate) fn handle_event(&mut self, tui: &mut tui::Tui, event: TuiEvent) -> Result<()> { match event { TuiEvent::Key(key_event) => match key_event { - KeyEvent { - code: KeyCode::Char('q'), - kind: KeyEventKind::Press, - .. - } - | KeyEvent { - code: KeyCode::Char('c'), - modifiers: crossterm::event::KeyModifiers::CONTROL, - kind: KeyEventKind::Press, - .. - } => { + e if KEY_Q.is_press(e) || KEY_CTRL_C.is_press(e) => { self.is_done = true; Ok(()) } diff --git a/codex-rs/tui/src/resume_picker.rs b/codex-rs/tui/src/resume_picker.rs index 9a8e8f408a..ded7d41afc 100644 --- a/codex-rs/tui/src/resume_picker.rs +++ b/codex-rs/tui/src/resume_picker.rs @@ -24,6 +24,7 @@ use tokio_stream::StreamExt; use tokio_stream::wrappers::UnboundedReceiverStream; use unicode_width::UnicodeWidthStr; +use crate::key_hint; use crate::text_formatting::truncate_text; use crate::tui::FrameRequester; use crate::tui::Tui; @@ -678,16 +679,18 @@ fn draw_picker(tui: &mut Tui, state: &PickerState) -> std::io::Result<()> { // Hint line let hint_line: Line = vec![ - "Enter".bold(), - " to resume ".into(), - "• ".dim(), - "Esc".bold(), - " to start new ".into(), - "• ".dim(), - "Ctrl+C".into(), - " to quit ".into(), - "• ".dim(), - "↑/↓".into(), + key_hint::plain(KeyCode::Enter).into(), + " to resume ".dim(), + " ".dim(), + key_hint::plain(KeyCode::Esc).into(), + " to start new ".dim(), + " ".dim(), + key_hint::ctrl(KeyCode::Char('c')).into(), + " to quit ".dim(), + " ".dim(), + key_hint::plain(KeyCode::Up).into(), + "/".dim(), + key_hint::plain(KeyCode::Down).into(), " to browse".dim(), ] .into(); diff --git a/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__static_overlay_snapshot_basic.snap b/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__static_overlay_snapshot_basic.snap index ee65b04d73..bc7818aff9 100644 --- a/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__static_overlay_snapshot_basic.snap +++ b/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__static_overlay_snapshot_basic.snap @@ -9,6 +9,6 @@ expression: term.backend() "~ " "~ " "───────────────────────────────── 100% ─" -" ↑/↓ scroll PgUp/PgDn page Home/End " -" q quit " +" ↑/↓ to scroll pgup/pgdn to page hom" +" q to quit " " " diff --git a/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__transcript_overlay_apply_patch_scroll_vt100.snap b/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__transcript_overlay_apply_patch_scroll_vt100.snap index df5f17adc7..7beca4a4c2 100644 --- a/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__transcript_overlay_apply_patch_scroll_vt100.snap +++ b/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__transcript_overlay_apply_patch_scroll_vt100.snap @@ -11,5 +11,5 @@ expression: snapshot 1 +hello 2 +world ─────────────────────────────────────────────────────────────────────────── 0% ─ - ↑/↓ scroll PgUp/PgDn page Home/End jump - q quit Esc edit prev + ↑/↓ to scroll pgup/pgdn to page home/end to jump + q to quit esc to edit prev diff --git a/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__transcript_overlay_snapshot_basic.snap b/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__transcript_overlay_snapshot_basic.snap index 0c03edef47..61fcf7d2ea 100644 --- a/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__transcript_overlay_snapshot_basic.snap +++ b/codex-rs/tui/src/snapshots/codex_tui__pager_overlay__tests__transcript_overlay_snapshot_basic.snap @@ -9,6 +9,6 @@ expression: term.backend() " " "gamma " "───────────────────────────────── 100% ─" -" ↑/↓ scroll PgUp/PgDn page Home/End " -" q quit Esc edit prev " +" ↑/↓ to scroll pgup/pgdn to page hom" +" q to quit esc to edit prev " " " diff --git a/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_truncated.snap b/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_truncated.snap index 6aa3401770..95dbc33bb2 100644 --- a/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_truncated.snap +++ b/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_truncated.snap @@ -2,5 +2,5 @@ source: tui/src/status_indicator_widget.rs expression: terminal.backend() --- -" Working (0s • Esc " +" Working (0s • esc " " " diff --git a/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_with_queued_messages.snap b/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_with_queued_messages.snap index 061f3a1393..7df7e5de75 100644 --- a/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_with_queued_messages.snap +++ b/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_with_queued_messages.snap @@ -2,11 +2,11 @@ source: tui/src/status_indicator_widget.rs expression: terminal.backend() --- -" Working (0s • Esc to interrupt) " +" Working (0s • esc to interrupt) " " " " ↳ first " " ↳ second " -" ⌥↑ edit " +" alt + ↑ edit " " " " " " " diff --git a/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_with_working_header.snap b/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_with_working_header.snap index debf282118..c02e83263d 100644 --- a/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_with_working_header.snap +++ b/codex-rs/tui/src/snapshots/codex_tui__status_indicator_widget__tests__renders_with_working_header.snap @@ -2,5 +2,5 @@ source: tui/src/status_indicator_widget.rs expression: terminal.backend() --- -" Working (0s • Esc to interrupt) " +" Working (0s • esc to interrupt) " " " diff --git a/codex-rs/tui/src/status_indicator_widget.rs b/codex-rs/tui/src/status_indicator_widget.rs index afb7952069..caf15b2882 100644 --- a/codex-rs/tui/src/status_indicator_widget.rs +++ b/codex-rs/tui/src/status_indicator_widget.rs @@ -5,6 +5,7 @@ use std::time::Duration; use std::time::Instant; use codex_core::protocol::Op; +use crossterm::event::KeyCode; use ratatui::buffer::Buffer; use ratatui::layout::Rect; use ratatui::style::Stylize; @@ -164,7 +165,7 @@ impl WidgetRef for StatusIndicatorWidget { spans.extend(vec![ " ".into(), format!("({pretty_elapsed} • ").dim(), - "Esc".dim().bold(), + key_hint::plain(KeyCode::Esc).into(), " to interrupt)".dim(), ]); @@ -188,8 +189,14 @@ impl WidgetRef for StatusIndicatorWidget { } } if !self.queued_messages.is_empty() { - let shortcut = key_hint::alt("↑"); - lines.push(Line::from(vec![" ".into(), shortcut, " edit".into()]).dim()); + lines.push( + Line::from(vec![ + " ".into(), + key_hint::alt(KeyCode::Up).into(), + " edit".into(), + ]) + .dim(), + ); } let paragraph = Paragraph::new(lines); From 45936f8fbd761dbeaf75c8357d29dc89d53b9dc6 Mon Sep 17 00:00:00 2001 From: Jeremy Rose <172423086+nornagon-openai@users.noreply.github.com> Date: Thu, 2 Oct 2025 11:36:03 -0700 Subject: [PATCH 06/16] show "Viewed Image" when the model views an image (#4475) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Screenshot 2025-09-29 at 4 22 00 PM --- codex-rs/core/src/codex.rs | 11 +++++++++- codex-rs/core/src/rollout/policy.rs | 1 + .../src/event_processor_with_human_output.rs | 8 ++++++++ codex-rs/mcp-server/src/codex_tool_runner.rs | 1 + codex-rs/protocol/src/protocol.rs | 11 ++++++++++ codex-rs/tui/src/chatwidget.rs | 11 ++++++++++ ...cal_image_attachment_history_snapshot.snap | 6 ++++++ codex-rs/tui/src/chatwidget/tests.rs | 20 +++++++++++++++++++ codex-rs/tui/src/diff_render.rs | 4 +++- codex-rs/tui/src/history_cell.rs | 12 +++++++++++ 10 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__local_image_attachment_history_snapshot.snap diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 9f6b02a556..3114fc30f8 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -109,6 +109,7 @@ use crate::protocol::Submission; use crate::protocol::TokenCountEvent; use crate::protocol::TokenUsage; use crate::protocol::TurnDiffEvent; +use crate::protocol::ViewImageToolCallEvent; use crate::protocol::WebSearchBeginEvent; use crate::rollout::RolloutRecorder; use crate::rollout::RolloutRecorderParams; @@ -2469,13 +2470,21 @@ async fn handle_function_call( )) })?; let abs = turn_context.resolve_path(Some(args.path)); - sess.inject_input(vec![InputItem::LocalImage { path: abs }]) + sess.inject_input(vec![InputItem::LocalImage { path: abs.clone() }]) .await .map_err(|_| { FunctionCallError::RespondToModel( "unable to attach image (no active task)".to_string(), ) })?; + sess.send_event(Event { + id: sub_id.clone(), + msg: EventMsg::ViewImageToolCall(ViewImageToolCallEvent { + call_id: call_id.clone(), + path: abs, + }), + }) + .await; Ok("attached local image path".to_string()) } diff --git a/codex-rs/core/src/rollout/policy.rs b/codex-rs/core/src/rollout/policy.rs index 2fd0efb0dc..8fc39e79f5 100644 --- a/codex-rs/core/src/rollout/policy.rs +++ b/codex-rs/core/src/rollout/policy.rs @@ -70,6 +70,7 @@ pub(crate) fn should_persist_event_msg(ev: &EventMsg) -> bool { | EventMsg::ListCustomPromptsResponse(_) | EventMsg::PlanUpdate(_) | EventMsg::ShutdownComplete + | EventMsg::ViewImageToolCall(_) | EventMsg::ConversationPath(_) => false, } } diff --git a/codex-rs/exec/src/event_processor_with_human_output.rs b/codex-rs/exec/src/event_processor_with_human_output.rs index 4f231bab84..72c31c2979 100644 --- a/codex-rs/exec/src/event_processor_with_human_output.rs +++ b/codex-rs/exec/src/event_processor_with_human_output.rs @@ -580,6 +580,14 @@ impl EventProcessor for EventProcessorWithHumanOutput { EventMsg::ListCustomPromptsResponse(_) => { // Currently ignored in exec output. } + EventMsg::ViewImageToolCall(view) => { + ts_println!( + self, + "{} {}", + "viewed image".style(self.magenta), + view.path.display() + ); + } EventMsg::TurnAborted(abort_reason) => match abort_reason.reason { TurnAbortReason::Interrupted => { ts_println!(self, "task interrupted"); diff --git a/codex-rs/mcp-server/src/codex_tool_runner.rs b/codex-rs/mcp-server/src/codex_tool_runner.rs index bcbfdf4471..f09dc98cbc 100644 --- a/codex-rs/mcp-server/src/codex_tool_runner.rs +++ b/codex-rs/mcp-server/src/codex_tool_runner.rs @@ -280,6 +280,7 @@ async fn run_codex_tool_session_inner( | EventMsg::ConversationPath(_) | EventMsg::UserMessage(_) | EventMsg::ShutdownComplete + | EventMsg::ViewImageToolCall(_) | EventMsg::EnteredReviewMode(_) | EventMsg::ExitedReviewMode(_) => { // For now, we do not do anything extra for these diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 98e218b962..17a4a844b8 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -477,6 +477,9 @@ pub enum EventMsg { ExecCommandEnd(ExecCommandEndEvent), + /// Notification that the agent attached a local image via the view_image tool. + ViewImageToolCall(ViewImageToolCallEvent), + ExecApprovalRequest(ExecApprovalRequestEvent), ApplyPatchApprovalRequest(ApplyPatchApprovalRequestEvent), @@ -1074,6 +1077,14 @@ pub struct ExecCommandEndEvent { pub formatted_output: String, } +#[derive(Debug, Clone, Deserialize, Serialize, TS)] +pub struct ViewImageToolCallEvent { + /// Identifier for the originating tool call. + pub call_id: String, + /// Local filesystem path provided to the tool. + pub path: PathBuf, +} + #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, TS)] #[serde(rename_all = "snake_case")] pub enum ExecOutputStream { diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 32566b3ecd..48382354f6 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -39,6 +39,7 @@ use codex_core::protocol::TokenUsageInfo; use codex_core::protocol::TurnAbortReason; use codex_core::protocol::TurnDiffEvent; use codex_core::protocol::UserMessageEvent; +use codex_core::protocol::ViewImageToolCallEvent; use codex_core::protocol::WebSearchBeginEvent; use codex_core::protocol::WebSearchEndEvent; use codex_protocol::ConversationId; @@ -538,6 +539,15 @@ impl ChatWidget { )); } + fn on_view_image_tool_call(&mut self, event: ViewImageToolCallEvent) { + self.flush_answer_stream_with_separator(); + self.add_to_history(history_cell::new_view_image_tool_call( + event.path, + &self.config.cwd, + )); + self.request_redraw(); + } + fn on_patch_apply_end(&mut self, event: codex_core::protocol::PatchApplyEndEvent) { let ev2 = event.clone(); self.defer_or_handle( @@ -1398,6 +1408,7 @@ impl ChatWidget { EventMsg::PatchApplyBegin(ev) => self.on_patch_apply_begin(ev), EventMsg::PatchApplyEnd(ev) => self.on_patch_apply_end(ev), EventMsg::ExecCommandEnd(ev) => self.on_exec_command_end(ev), + EventMsg::ViewImageToolCall(ev) => self.on_view_image_tool_call(ev), EventMsg::McpToolCallBegin(ev) => self.on_mcp_tool_call_begin(ev), EventMsg::McpToolCallEnd(ev) => self.on_mcp_tool_call_end(ev), EventMsg::WebSearchBegin(ev) => self.on_web_search_begin(ev), diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__local_image_attachment_history_snapshot.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__local_image_attachment_history_snapshot.snap new file mode 100644 index 0000000000..cf4c6943fd --- /dev/null +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__local_image_attachment_history_snapshot.snap @@ -0,0 +1,6 @@ +--- +source: tui/src/chatwidget/tests.rs +expression: combined +--- +• Viewed Image + └ example.png diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index 77cde195c5..e175e180b9 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -35,6 +35,7 @@ use codex_core::protocol::ReviewRequest; use codex_core::protocol::StreamErrorEvent; use codex_core::protocol::TaskCompleteEvent; use codex_core::protocol::TaskStartedEvent; +use codex_core::protocol::ViewImageToolCallEvent; use codex_protocol::ConversationId; use crossterm::event::KeyCode; use crossterm::event::KeyEvent; @@ -794,6 +795,25 @@ fn custom_prompt_enter_empty_does_not_send() { assert!(rx.try_recv().is_err(), "no app event should be sent"); } +#[test] +fn view_image_tool_call_adds_history_cell() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(); + let image_path = chat.config.cwd.join("example.png"); + + chat.handle_codex_event(Event { + id: "sub-image".into(), + msg: EventMsg::ViewImageToolCall(ViewImageToolCallEvent { + call_id: "call-image".into(), + path: image_path, + }), + }); + + let cells = drain_insert_history(&mut rx); + assert_eq!(cells.len(), 1, "expected a single history cell"); + let combined = lines_to_single_string(&cells[0]); + assert_snapshot!("local_image_attachment_history_snapshot", combined); +} + // Snapshot test: interrupting a running exec finalizes the active cell with a red ✗ // marker (replacing the spinner) and flushes it into history. #[test] diff --git a/codex-rs/tui/src/diff_render.rs b/codex-rs/tui/src/diff_render.rs index 033fd09240..6f133feeb5 100644 --- a/codex-rs/tui/src/diff_render.rs +++ b/codex-rs/tui/src/diff_render.rs @@ -268,7 +268,9 @@ pub(crate) fn display_path_for(path: &Path, cwd: &Path) -> String { let chosen = if path_in_same_repo { pathdiff::diff_paths(path, cwd).unwrap_or_else(|| path.to_path_buf()) } else { - relativize_to_home(path).unwrap_or_else(|| path.to_path_buf()) + relativize_to_home(path) + .map(|p| PathBuf::from_iter([Path::new("~"), p.as_path()])) + .unwrap_or_else(|| path.to_path_buf()) }; chosen.display().to_string() } diff --git a/codex-rs/tui/src/history_cell.rs b/codex-rs/tui/src/history_cell.rs index 4121a98397..4bab8afb70 100644 --- a/codex-rs/tui/src/history_cell.rs +++ b/codex-rs/tui/src/history_cell.rs @@ -1,4 +1,5 @@ use crate::diff_render::create_diff_summary; +use crate::diff_render::display_path_for; use crate::exec_cell::CommandOutput; use crate::exec_cell::OutputLinesParams; use crate::exec_cell::TOOL_CALL_MAX_LINES; @@ -1037,6 +1038,17 @@ pub(crate) fn new_patch_apply_failure(stderr: String) -> PlainHistoryCell { PlainHistoryCell { lines } } +pub(crate) fn new_view_image_tool_call(path: PathBuf, cwd: &Path) -> PlainHistoryCell { + let display_path = display_path_for(&path, cwd); + + let lines: Vec> = vec![ + vec!["• ".dim(), "Viewed Image".bold()].into(), + vec![" └ ".dim(), display_path.dim()].into(), + ]; + + PlainHistoryCell { lines } +} + pub(crate) fn new_reasoning_block( full_reasoning_buffer: String, config: &Config, From 06e34d4607ce4b2c03b21539f1e9577bdc5cc13a Mon Sep 17 00:00:00 2001 From: easong-openai Date: Thu, 2 Oct 2025 12:38:24 -0700 Subject: [PATCH 07/16] Make model switcher two-stage (#4178) https://github.com/user-attachments/assets/16d5c67c-e580-4a29-983c-a315f95424ee --- codex-rs/common/src/model_presets.rs | 14 +- codex-rs/tui/src/app.rs | 16 +- codex-rs/tui/src/app_event.rs | 7 + codex-rs/tui/src/chatwidget.rs | 194 ++++++++++++++---- ...ests__model_reasoning_selection_popup.snap | 13 ++ ...twidget__tests__model_selection_popup.snap | 12 ++ codex-rs/tui/src/chatwidget/tests.rs | 59 ++++++ 7 files changed, 265 insertions(+), 50 deletions(-) create mode 100644 codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__model_reasoning_selection_popup.snap create mode 100644 codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__model_selection_popup.snap diff --git a/codex-rs/common/src/model_presets.rs b/codex-rs/common/src/model_presets.rs index 68acb5ce90..9954ad1890 100644 --- a/codex-rs/common/src/model_presets.rs +++ b/codex-rs/common/src/model_presets.rs @@ -20,49 +20,49 @@ const PRESETS: &[ModelPreset] = &[ ModelPreset { id: "gpt-5-codex-low", label: "gpt-5-codex low", - description: "", + description: "Fastest responses with limited reasoning", model: "gpt-5-codex", effort: Some(ReasoningEffort::Low), }, ModelPreset { id: "gpt-5-codex-medium", label: "gpt-5-codex medium", - description: "", + description: "Dynamically adjusts reasoning based on the task", model: "gpt-5-codex", effort: Some(ReasoningEffort::Medium), }, ModelPreset { id: "gpt-5-codex-high", label: "gpt-5-codex high", - description: "", + description: "Maximizes reasoning depth for complex or ambiguous problems", model: "gpt-5-codex", effort: Some(ReasoningEffort::High), }, ModelPreset { id: "gpt-5-minimal", label: "gpt-5 minimal", - description: "— fastest responses with limited reasoning; ideal for coding, instructions, or lightweight tasks", + description: "Fastest responses with little reasoning", model: "gpt-5", effort: Some(ReasoningEffort::Minimal), }, ModelPreset { id: "gpt-5-low", label: "gpt-5 low", - description: "— balances speed with some reasoning; useful for straightforward queries and short explanations", + description: "Balances speed with some reasoning; useful for straightforward queries and short explanations", model: "gpt-5", effort: Some(ReasoningEffort::Low), }, ModelPreset { id: "gpt-5-medium", label: "gpt-5 medium", - description: "— default setting; provides a solid balance of reasoning depth and latency for general-purpose tasks", + description: "Provides a solid balance of reasoning depth and latency for general-purpose tasks", model: "gpt-5", effort: Some(ReasoningEffort::Medium), }, ModelPreset { id: "gpt-5-high", label: "gpt-5 high", - description: "— maximizes reasoning depth for complex or ambiguous problems", + description: "Maximizes reasoning depth for complex or ambiguous problems", model: "gpt-5", effort: Some(ReasoningEffort::High), }, diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 383e5cae54..ab43ceb0a3 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -320,24 +320,28 @@ impl App { self.config.model_family = family; } } + AppEvent::OpenReasoningPopup { model, presets } => { + self.chat_widget.open_reasoning_popup(model, presets); + } AppEvent::PersistModelSelection { model, effort } => { let profile = self.active_profile.as_deref(); match persist_model_selection(&self.config.codex_home, profile, &model, effort) .await { Ok(()) => { + let effort_label = effort + .map(|eff| format!(" with {eff} reasoning")) + .unwrap_or_else(|| " with default reasoning".to_string()); if let Some(profile) = profile { self.chat_widget.add_info_message( - format!("Model changed to {model}{reasoning_effort} for {profile} profile", reasoning_effort = effort.map(|e| format!(" {e}")).unwrap_or_default()), + format!( + "Model changed to {model}{effort_label} for {profile} profile" + ), None, ); } else { self.chat_widget.add_info_message( - format!( - "Model changed to {model}{reasoning_effort}", - reasoning_effort = - effort.map(|e| format!(" {e}")).unwrap_or_default() - ), + format!("Model changed to {model}{effort_label}"), None, ); } diff --git a/codex-rs/tui/src/app_event.rs b/codex-rs/tui/src/app_event.rs index 9c8a71de2e..9d79c8ae13 100644 --- a/codex-rs/tui/src/app_event.rs +++ b/codex-rs/tui/src/app_event.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; +use codex_common::model_presets::ModelPreset; use codex_core::protocol::ConversationPathResponseEvent; use codex_core::protocol::Event; use codex_file_search::FileMatch; @@ -60,6 +61,12 @@ pub(crate) enum AppEvent { effort: Option, }, + /// Open the reasoning selection popup after picking a model. + OpenReasoningPopup { + model: String, + presets: Vec, + }, + /// Update the current approval policy in the running app and widget. UpdateAskForApprovalPolicy(AskForApproval), diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 48382354f6..c03caca85c 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -1,3 +1,4 @@ +use std::collections::BTreeMap; use std::collections::HashMap; use std::collections::VecDeque; use std::path::PathBuf; @@ -111,6 +112,7 @@ use codex_git_tooling::GhostCommit; use codex_git_tooling::GitToolingError; use codex_git_tooling::create_ghost_commit; use codex_git_tooling::restore_ghost_commit; +use strum::IntoEnumIterator; const MAX_TRACKED_GHOST_COMMITS: usize = 20; @@ -283,6 +285,16 @@ fn create_initial_user_message(text: String, image_paths: Vec) -> Optio } impl ChatWidget { + fn model_description_for(slug: &str) -> Option<&'static str> { + if slug.starts_with("gpt-5-codex") { + Some("Optimized for coding tasks with many tools.") + } else if slug.starts_with("gpt-5") { + Some("Broad world knowledge with strong general reasoning.") + } else { + None + } + } + fn flush_answer_stream_with_separator(&mut self) { if let Some(mut controller) = self.stream_controller.take() && let Some(cell) = controller.finalize() @@ -1579,47 +1591,38 @@ impl ChatWidget { )); } - /// Open a popup to choose the model preset (model + reasoning effort). + /// Open a popup to choose the model (stage 1). After selecting a model, + /// a second popup is shown to choose the reasoning effort. pub(crate) fn open_model_popup(&mut self) { let current_model = self.config.model.clone(); - let current_effort = self.config.model_reasoning_effort; let auth_mode = self.auth_manager.auth().map(|auth| auth.mode); let presets: Vec = builtin_model_presets(auth_mode); + let mut grouped: BTreeMap<&str, Vec> = BTreeMap::new(); + for preset in presets.into_iter() { + grouped.entry(preset.model).or_default().push(preset); + } + let mut items: Vec = Vec::new(); - for preset in presets.iter() { - let name = preset.label.to_string(); - let description = Some(preset.description.to_string()); - let is_current = preset.model == current_model && preset.effort == current_effort; - let model_slug = preset.model.to_string(); - let effort = preset.effort; - let current_model = current_model.clone(); + for (model_slug, entries) in grouped.into_iter() { + let name = model_slug.to_string(); + let description = Self::model_description_for(model_slug) + .map(std::string::ToString::to_string) + .or_else(|| { + entries + .iter() + .find(|preset| !preset.description.is_empty()) + .map(|preset| preset.description.to_string()) + }) + .or_else(|| entries.first().map(|preset| preset.description.to_string())); + let is_current = model_slug == current_model; + let model_slug_string = model_slug.to_string(); + let presets_for_model = entries.clone(); let actions: Vec = vec![Box::new(move |tx| { - tx.send(AppEvent::CodexOp(Op::OverrideTurnContext { - cwd: None, - approval_policy: None, - sandbox_policy: None, - model: Some(model_slug.clone()), - effort: Some(effort), - summary: None, - })); - tx.send(AppEvent::UpdateModel(model_slug.clone())); - tx.send(AppEvent::UpdateReasoningEffort(effort)); - tx.send(AppEvent::PersistModelSelection { - model: model_slug.clone(), - effort, + tx.send(AppEvent::OpenReasoningPopup { + model: model_slug_string.clone(), + presets: presets_for_model.clone(), }); - tracing::info!( - "New model: {}, New effort: {}, Current model: {}, Current effort: {}", - model_slug.clone(), - effort - .map(|effort| effort.to_string()) - .unwrap_or_else(|| "none".to_string()), - current_model, - current_effort - .map(|effort| effort.to_string()) - .unwrap_or_else(|| "none".to_string()) - ); })]; items.push(SelectionItem { name, @@ -1632,10 +1635,127 @@ impl ChatWidget { } self.bottom_pane.show_selection_view(SelectionViewParams { - title: Some("Select model and reasoning level".to_string()), - subtitle: Some( - "Switch between OpenAI models for this and future Codex CLI session".to_string(), - ), + title: Some("Select Model".to_string()), + subtitle: Some("Switch the model for this and future Codex CLI sessions".to_string()), + footer_hint: Some(standard_popup_hint_line()), + items, + ..Default::default() + }); + } + + /// Open a popup to choose the reasoning effort (stage 2) for the given model. + pub(crate) fn open_reasoning_popup(&mut self, model_slug: String, presets: Vec) { + let default_effort = ReasoningEffortConfig::default(); + + let has_none_choice = presets.iter().any(|preset| preset.effort.is_none()); + struct EffortChoice { + stored: Option, + display: ReasoningEffortConfig, + } + let mut choices: Vec = Vec::new(); + for effort in ReasoningEffortConfig::iter() { + if presets.iter().any(|preset| preset.effort == Some(effort)) { + choices.push(EffortChoice { + stored: Some(effort), + display: effort, + }); + } + if has_none_choice && default_effort == effort { + choices.push(EffortChoice { + stored: None, + display: effort, + }); + } + } + if choices.is_empty() { + choices.push(EffortChoice { + stored: Some(default_effort), + display: default_effort, + }); + } + + let default_choice: Option = if has_none_choice { + None + } else if choices + .iter() + .any(|choice| choice.stored == Some(default_effort)) + { + Some(default_effort) + } else { + choices + .iter() + .find_map(|choice| choice.stored) + .or(Some(default_effort)) + }; + + let is_current_model = self.config.model == model_slug; + let highlight_choice = if is_current_model { + self.config.model_reasoning_effort + } else { + default_choice + }; + + let mut items: Vec = Vec::new(); + for choice in choices.iter() { + let effort = choice.display; + let mut effort_label = effort.to_string(); + if let Some(first) = effort_label.get_mut(0..1) { + first.make_ascii_uppercase(); + } + if choice.stored == default_choice { + effort_label.push_str(" (default)"); + } + + let description = presets + .iter() + .find(|preset| preset.effort == choice.stored && !preset.description.is_empty()) + .map(|preset| preset.description.to_string()) + .or_else(|| { + presets + .iter() + .find(|preset| preset.effort == choice.stored) + .map(|preset| preset.description.to_string()) + }); + + let model_for_action = model_slug.clone(); + let effort_for_action = choice.stored; + let actions: Vec = vec![Box::new(move |tx| { + tx.send(AppEvent::CodexOp(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: Some(model_for_action.clone()), + effort: Some(effort_for_action), + summary: None, + })); + tx.send(AppEvent::UpdateModel(model_for_action.clone())); + tx.send(AppEvent::UpdateReasoningEffort(effort_for_action)); + tx.send(AppEvent::PersistModelSelection { + model: model_for_action.clone(), + effort: effort_for_action, + }); + tracing::info!( + "Selected model: {}, Selected effort: {}", + model_for_action, + effort_for_action + .map(|e| e.to_string()) + .unwrap_or_else(|| "default".to_string()) + ); + })]; + + items.push(SelectionItem { + name: effort_label, + description, + is_current: is_current_model && choice.stored == highlight_choice, + actions, + dismiss_on_select: true, + search_value: None, + }); + } + + self.bottom_pane.show_selection_view(SelectionViewParams { + title: Some("Select Reasoning Level".to_string()), + subtitle: Some(format!("Reasoning for model {model_slug}")), footer_hint: Some(standard_popup_hint_line()), items, ..Default::default() diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__model_reasoning_selection_popup.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__model_reasoning_selection_popup.snap new file mode 100644 index 0000000000..664ec0f50f --- /dev/null +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__model_reasoning_selection_popup.snap @@ -0,0 +1,13 @@ +--- +source: tui/src/chatwidget/tests.rs +expression: popup +--- + Select Reasoning Level + Reasoning for model gpt-5-codex + + 1. Low Fastest responses with limited reasoning + 2. Medium (default) Dynamically adjusts reasoning based on the task +› 3. High (current) Maximizes reasoning depth for complex or ambiguous + problems + + Press enter to confirm or esc to go back diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__model_selection_popup.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__model_selection_popup.snap new file mode 100644 index 0000000000..cfe99bd0a6 --- /dev/null +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__model_selection_popup.snap @@ -0,0 +1,12 @@ +--- +source: tui/src/chatwidget/tests.rs +expression: popup +--- + Select Model + Switch the model for this and future Codex CLI sessions + + 1. gpt-5 Broad world knowledge with strong general + reasoning. +› 2. gpt-5-codex (current) Optimized for coding tasks with many tools. + + Press enter to confirm or esc to go back diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index e175e180b9..64b4c72b34 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -936,6 +936,65 @@ fn render_bottom_first_row(chat: &ChatWidget, width: u16) -> String { String::new() } +fn render_bottom_popup(chat: &ChatWidget, width: u16) -> String { + let height = chat.desired_height(width); + let area = Rect::new(0, 0, width, height); + let mut buf = Buffer::empty(area); + (chat).render_ref(area, &mut buf); + + let mut lines: Vec = (0..area.height) + .map(|row| { + let mut line = String::new(); + for col in 0..area.width { + let symbol = buf[(area.x + col, area.y + row)].symbol(); + if symbol.is_empty() { + line.push(' '); + } else { + line.push_str(symbol); + } + } + line.trim_end().to_string() + }) + .collect(); + + while lines.first().is_some_and(|line| line.trim().is_empty()) { + lines.remove(0); + } + while lines.last().is_some_and(|line| line.trim().is_empty()) { + lines.pop(); + } + + lines.join("\n") +} + +#[test] +fn model_selection_popup_snapshot() { + let (mut chat, _rx, _op_rx) = make_chatwidget_manual(); + + chat.config.model = "gpt-5-codex".to_string(); + chat.open_model_popup(); + + let popup = render_bottom_popup(&chat, 80); + assert_snapshot!("model_selection_popup", popup); +} + +#[test] +fn model_reasoning_selection_popup_snapshot() { + let (mut chat, _rx, _op_rx) = make_chatwidget_manual(); + + chat.config.model = "gpt-5-codex".to_string(); + chat.config.model_reasoning_effort = Some(ReasoningEffortConfig::High); + + let presets = builtin_model_presets(None) + .into_iter() + .filter(|preset| preset.model == "gpt-5-codex") + .collect::>(); + chat.open_reasoning_popup("gpt-5-codex".to_string(), presets); + + let popup = render_bottom_popup(&chat, 80); + assert_snapshot!("model_reasoning_selection_popup", popup); +} + #[test] fn exec_history_extends_previous_when_consecutive() { let (mut chat, _rx, _op_rx) = make_chatwidget_manual(); From 4c566d484afc60bb9e33895810dafaa6acfe790b Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 2 Oct 2025 13:06:21 -0700 Subject: [PATCH 08/16] Separate interactive and non-interactive sessions (#4612) Do not show exec session in VSCode/TUI selector. --- .../app-server/src/codex_message_processor.rs | 2 + codex-rs/app-server/src/message_processor.rs | 6 +- codex-rs/core/src/codex.rs | 10 +- codex-rs/core/src/conversation_manager.rs | 22 +- codex-rs/core/src/lib.rs | 1 + codex-rs/core/src/rollout/list.rs | 15 +- codex-rs/core/src/rollout/mod.rs | 4 + codex-rs/core/src/rollout/recorder.rs | 14 +- codex-rs/core/src/rollout/tests.rs | 265 ++++++++++++++---- codex-rs/core/tests/suite/cli_stream.rs | 2 +- codex-rs/core/tests/suite/client.rs | 3 +- codex-rs/exec/src/lib.rs | 7 +- codex-rs/mcp-server/src/message_processor.rs | 4 +- codex-rs/protocol/src/protocol.rs | 31 +- codex-rs/tui/src/app.rs | 6 +- codex-rs/tui/src/lib.rs | 10 +- codex-rs/tui/src/resume_picker.rs | 10 +- 17 files changed, 346 insertions(+), 66 deletions(-) diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 1e5eb82ef0..c158621d2e 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -53,6 +53,7 @@ use codex_core::AuthManager; use codex_core::CodexConversation; use codex_core::ConversationManager; use codex_core::Cursor as RolloutCursor; +use codex_core::INTERACTIVE_SESSION_SOURCES; use codex_core::NewConversation; use codex_core::RolloutRecorder; use codex_core::SessionMeta; @@ -708,6 +709,7 @@ impl CodexMessageProcessor { &self.config.codex_home, page_size, cursor_ref, + INTERACTIVE_SESSION_SOURCES, ) .await { diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index 9d8db2f921..15086c19e1 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -17,6 +17,7 @@ use codex_core::ConversationManager; use codex_core::config::Config; use codex_core::default_client::USER_AGENT_SUFFIX; use codex_core::default_client::get_codex_user_agent; +use codex_protocol::protocol::SessionSource; use std::sync::Arc; pub(crate) struct MessageProcessor { @@ -35,7 +36,10 @@ impl MessageProcessor { ) -> Self { let outgoing = Arc::new(outgoing); let auth_manager = AuthManager::shared(config.codex_home.clone(), false); - let conversation_manager = Arc::new(ConversationManager::new(auth_manager.clone())); + let conversation_manager = Arc::new(ConversationManager::new( + auth_manager.clone(), + SessionSource::VSCode, + )); let codex_message_processor = CodexMessageProcessor::new( auth_manager, conversation_manager, diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 3114fc30f8..2bba2b42b7 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -22,6 +22,7 @@ use codex_protocol::protocol::ConversationPathResponseEvent; use codex_protocol::protocol::ExitedReviewModeEvent; use codex_protocol::protocol::ReviewRequest; use codex_protocol::protocol::RolloutItem; +use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::TaskStartedEvent; use codex_protocol::protocol::TurnAbortReason; use codex_protocol::protocol::TurnContextItem; @@ -172,6 +173,7 @@ impl Codex { config: Config, auth_manager: Arc, conversation_history: InitialHistory, + session_source: SessionSource, ) -> CodexResult { let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); let (tx_event, rx_event) = async_channel::unbounded(); @@ -200,6 +202,7 @@ impl Codex { auth_manager.clone(), tx_event.clone(), conversation_history, + session_source, ) .await .map_err(|e| { @@ -334,6 +337,7 @@ impl Session { auth_manager: Arc, tx_event: Sender, initial_history: InitialHistory, + session_source: SessionSource, ) -> anyhow::Result<(Arc, TurnContext)> { let ConfigureSession { provider, @@ -357,7 +361,11 @@ impl Session { let conversation_id = ConversationId::default(); ( conversation_id, - RolloutRecorderParams::new(conversation_id, user_instructions.clone()), + RolloutRecorderParams::new( + conversation_id, + user_instructions.clone(), + session_source, + ), ) } InitialHistory::Resumed(resumed_history) => ( diff --git a/codex-rs/core/src/conversation_manager.rs b/codex-rs/core/src/conversation_manager.rs index 73d9b658ce..150794fc8d 100644 --- a/codex-rs/core/src/conversation_manager.rs +++ b/codex-rs/core/src/conversation_manager.rs @@ -17,6 +17,7 @@ use codex_protocol::ConversationId; use codex_protocol::models::ResponseItem; use codex_protocol::protocol::InitialHistory; use codex_protocol::protocol::RolloutItem; +use codex_protocol::protocol::SessionSource; use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; @@ -35,20 +36,25 @@ pub struct NewConversation { pub struct ConversationManager { conversations: Arc>>>, auth_manager: Arc, + session_source: SessionSource, } impl ConversationManager { - pub fn new(auth_manager: Arc) -> Self { + pub fn new(auth_manager: Arc, session_source: SessionSource) -> Self { Self { conversations: Arc::new(RwLock::new(HashMap::new())), auth_manager, + session_source, } } /// Construct with a dummy AuthManager containing the provided CodexAuth. /// Used for integration tests: should not be used by ordinary business logic. pub fn with_auth(auth: CodexAuth) -> Self { - Self::new(crate::AuthManager::from_auth_for_testing(auth)) + Self::new( + crate::AuthManager::from_auth_for_testing(auth), + SessionSource::Exec, + ) } pub async fn new_conversation(&self, config: Config) -> CodexResult { @@ -64,7 +70,13 @@ impl ConversationManager { let CodexSpawnOk { codex, conversation_id, - } = Codex::spawn(config, auth_manager, InitialHistory::New).await?; + } = Codex::spawn( + config, + auth_manager, + InitialHistory::New, + self.session_source, + ) + .await?; self.finalize_spawn(codex, conversation_id).await } @@ -121,7 +133,7 @@ impl ConversationManager { let CodexSpawnOk { codex, conversation_id, - } = Codex::spawn(config, auth_manager, initial_history).await?; + } = Codex::spawn(config, auth_manager, initial_history, self.session_source).await?; self.finalize_spawn(codex, conversation_id).await } @@ -155,7 +167,7 @@ impl ConversationManager { let CodexSpawnOk { codex, conversation_id, - } = Codex::spawn(config, auth_manager, history).await?; + } = Codex::spawn(config, auth_manager, history, self.session_source).await?; self.finalize_spawn(codex, conversation_id).await } diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index 4c7dfdcb3c..94350a447e 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -68,6 +68,7 @@ pub mod terminal; mod tool_apply_patch; pub mod turn_diff_tracker; pub use rollout::ARCHIVED_SESSIONS_SUBDIR; +pub use rollout::INTERACTIVE_SESSION_SOURCES; pub use rollout::RolloutRecorder; pub use rollout::SESSIONS_SUBDIR; pub use rollout::SessionMeta; diff --git a/codex-rs/core/src/rollout/list.rs b/codex-rs/core/src/rollout/list.rs index 755e63df86..b27b3382e3 100644 --- a/codex-rs/core/src/rollout/list.rs +++ b/codex-rs/core/src/rollout/list.rs @@ -17,6 +17,7 @@ use super::SESSIONS_SUBDIR; use crate::protocol::EventMsg; use codex_protocol::protocol::RolloutItem; use codex_protocol::protocol::RolloutLine; +use codex_protocol::protocol::SessionSource; /// Returned page of conversation summaries. #[derive(Debug, Default, PartialEq)] @@ -52,6 +53,7 @@ struct HeadTailSummary { tail: Vec, saw_session_meta: bool, saw_user_event: bool, + source: Option, created_at: Option, updated_at: Option, } @@ -106,6 +108,7 @@ pub(crate) async fn get_conversations( codex_home: &Path, page_size: usize, cursor: Option<&Cursor>, + allowed_sources: &[SessionSource], ) -> io::Result { let mut root = codex_home.to_path_buf(); root.push(SESSIONS_SUBDIR); @@ -121,7 +124,8 @@ pub(crate) async fn get_conversations( let anchor = cursor.cloned(); - let result = traverse_directories_for_paths(root.clone(), page_size, anchor).await?; + let result = + traverse_directories_for_paths(root.clone(), page_size, anchor, allowed_sources).await?; Ok(result) } @@ -140,6 +144,7 @@ async fn traverse_directories_for_paths( root: PathBuf, page_size: usize, anchor: Option, + allowed_sources: &[SessionSource], ) -> io::Result { let mut items: Vec = Vec::with_capacity(page_size); let mut scanned_files = 0usize; @@ -196,6 +201,13 @@ async fn traverse_directories_for_paths( let summary = read_head_and_tail(&path, HEAD_RECORD_LIMIT, TAIL_RECORD_LIMIT) .await .unwrap_or_default(); + if !allowed_sources.is_empty() + && !summary + .source + .is_some_and(|source| allowed_sources.iter().any(|s| s == &source)) + { + continue; + } // Apply filters: must have session meta and at least one user message event if summary.saw_session_meta && summary.saw_user_event { let HeadTailSummary { @@ -341,6 +353,7 @@ async fn read_head_and_tail( match rollout_line.item { RolloutItem::SessionMeta(session_meta_line) => { + summary.source = Some(session_meta_line.meta.source); summary.created_at = summary .created_at .clone() diff --git a/codex-rs/core/src/rollout/mod.rs b/codex-rs/core/src/rollout/mod.rs index 3c4cb10535..23410bd4d0 100644 --- a/codex-rs/core/src/rollout/mod.rs +++ b/codex-rs/core/src/rollout/mod.rs @@ -1,7 +1,11 @@ //! Rollout module: persistence and discovery of session rollout files. +use codex_protocol::protocol::SessionSource; + pub const SESSIONS_SUBDIR: &str = "sessions"; pub const ARCHIVED_SESSIONS_SUBDIR: &str = "archived_sessions"; +pub const INTERACTIVE_SESSION_SOURCES: &[SessionSource] = + &[SessionSource::Cli, SessionSource::VSCode]; pub mod list; pub(crate) mod policy; diff --git a/codex-rs/core/src/rollout/recorder.rs b/codex-rs/core/src/rollout/recorder.rs index f67c076c04..95f5d479b8 100644 --- a/codex-rs/core/src/rollout/recorder.rs +++ b/codex-rs/core/src/rollout/recorder.rs @@ -32,6 +32,7 @@ use codex_protocol::protocol::RolloutItem; use codex_protocol::protocol::RolloutLine; use codex_protocol::protocol::SessionMeta; use codex_protocol::protocol::SessionMetaLine; +use codex_protocol::protocol::SessionSource; /// Records all [`ResponseItem`]s for a session and flushes them to disk after /// every update. @@ -53,6 +54,7 @@ pub enum RolloutRecorderParams { Create { conversation_id: ConversationId, instructions: Option, + source: SessionSource, }, Resume { path: PathBuf, @@ -71,10 +73,15 @@ enum RolloutCmd { } impl RolloutRecorderParams { - pub fn new(conversation_id: ConversationId, instructions: Option) -> Self { + pub fn new( + conversation_id: ConversationId, + instructions: Option, + source: SessionSource, + ) -> Self { Self::Create { conversation_id, instructions, + source, } } @@ -89,8 +96,9 @@ impl RolloutRecorder { codex_home: &Path, page_size: usize, cursor: Option<&Cursor>, + allowed_sources: &[SessionSource], ) -> std::io::Result { - get_conversations(codex_home, page_size, cursor).await + get_conversations(codex_home, page_size, cursor, allowed_sources).await } /// Attempt to create a new [`RolloutRecorder`]. If the sessions directory @@ -101,6 +109,7 @@ impl RolloutRecorder { RolloutRecorderParams::Create { conversation_id, instructions, + source, } => { let LogFileInfo { file, @@ -127,6 +136,7 @@ impl RolloutRecorder { originator: originator().value.clone(), cli_version: env!("CARGO_PKG_VERSION").to_string(), instructions, + source, }), ) } diff --git a/codex-rs/core/src/rollout/tests.rs b/codex-rs/core/src/rollout/tests.rs index 7664ced0d7..b1f34e3a9c 100644 --- a/codex-rs/core/src/rollout/tests.rs +++ b/codex-rs/core/src/rollout/tests.rs @@ -12,6 +12,7 @@ use time::format_description::FormatItem; use time::macros::format_description; use uuid::Uuid; +use crate::rollout::INTERACTIVE_SESSION_SOURCES; use crate::rollout::list::ConversationItem; use crate::rollout::list::ConversationsPage; use crate::rollout::list::Cursor; @@ -28,13 +29,17 @@ use codex_protocol::protocol::RolloutItem; use codex_protocol::protocol::RolloutLine; use codex_protocol::protocol::SessionMeta; use codex_protocol::protocol::SessionMetaLine; +use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::UserMessageEvent; +const NO_SOURCE_FILTER: &[SessionSource] = &[]; + fn write_session_file( root: &Path, ts_str: &str, uuid: Uuid, num_records: usize, + source: Option, ) -> std::io::Result<(OffsetDateTime, Uuid)> { let format: &[FormatItem] = format_description!("[year]-[month]-[day]T[hour]-[minute]-[second]"); @@ -52,17 +57,23 @@ fn write_session_file( let file_path = dir.join(filename); let mut file = File::create(file_path)?; + let mut payload = serde_json::json!({ + "id": uuid, + "timestamp": ts_str, + "instructions": null, + "cwd": ".", + "originator": "test_originator", + "cli_version": "test_version", + }); + + if let Some(source) = source { + payload["source"] = serde_json::to_value(source).unwrap(); + } + let meta = serde_json::json!({ "timestamp": ts_str, "type": "session_meta", - "payload": { - "id": uuid, - "timestamp": ts_str, - "instructions": null, - "cwd": ".", - "originator": "test_originator", - "cli_version": "test_version" - } + "payload": payload, }); writeln!(file, "{meta}")?; @@ -99,11 +110,34 @@ async fn test_list_conversations_latest_first() { let u3 = Uuid::from_u128(3); // Create three sessions across three days - write_session_file(home, "2025-01-01T12-00-00", u1, 3).unwrap(); - write_session_file(home, "2025-01-02T12-00-00", u2, 3).unwrap(); - write_session_file(home, "2025-01-03T12-00-00", u3, 3).unwrap(); + write_session_file( + home, + "2025-01-01T12-00-00", + u1, + 3, + Some(SessionSource::VSCode), + ) + .unwrap(); + write_session_file( + home, + "2025-01-02T12-00-00", + u2, + 3, + Some(SessionSource::VSCode), + ) + .unwrap(); + write_session_file( + home, + "2025-01-03T12-00-00", + u3, + 3, + Some(SessionSource::VSCode), + ) + .unwrap(); - let page = get_conversations(home, 10, None).await.unwrap(); + let page = get_conversations(home, 10, None, INTERACTIVE_SESSION_SOURCES) + .await + .unwrap(); // Build expected objects let p1 = home @@ -131,7 +165,8 @@ async fn test_list_conversations_latest_first() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let head_2 = vec![serde_json::json!({ "id": u2, @@ -139,7 +174,8 @@ async fn test_list_conversations_latest_first() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let head_1 = vec![serde_json::json!({ "id": u1, @@ -147,7 +183,8 @@ async fn test_list_conversations_latest_first() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let expected_cursor: Cursor = @@ -198,13 +235,50 @@ async fn test_pagination_cursor() { let u5 = Uuid::from_u128(55); // Oldest to newest - write_session_file(home, "2025-03-01T09-00-00", u1, 1).unwrap(); - write_session_file(home, "2025-03-02T09-00-00", u2, 1).unwrap(); - write_session_file(home, "2025-03-03T09-00-00", u3, 1).unwrap(); - write_session_file(home, "2025-03-04T09-00-00", u4, 1).unwrap(); - write_session_file(home, "2025-03-05T09-00-00", u5, 1).unwrap(); + write_session_file( + home, + "2025-03-01T09-00-00", + u1, + 1, + Some(SessionSource::VSCode), + ) + .unwrap(); + write_session_file( + home, + "2025-03-02T09-00-00", + u2, + 1, + Some(SessionSource::VSCode), + ) + .unwrap(); + write_session_file( + home, + "2025-03-03T09-00-00", + u3, + 1, + Some(SessionSource::VSCode), + ) + .unwrap(); + write_session_file( + home, + "2025-03-04T09-00-00", + u4, + 1, + Some(SessionSource::VSCode), + ) + .unwrap(); + write_session_file( + home, + "2025-03-05T09-00-00", + u5, + 1, + Some(SessionSource::VSCode), + ) + .unwrap(); - let page1 = get_conversations(home, 2, None).await.unwrap(); + let page1 = get_conversations(home, 2, None, INTERACTIVE_SESSION_SOURCES) + .await + .unwrap(); let p5 = home .join("sessions") .join("2025") @@ -223,7 +297,8 @@ async fn test_pagination_cursor() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let head_4 = vec![serde_json::json!({ "id": u4, @@ -231,7 +306,8 @@ async fn test_pagination_cursor() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let expected_cursor1: Cursor = serde_json::from_str(&format!("\"2025-03-04T09-00-00|{u4}\"")).unwrap(); @@ -258,9 +334,14 @@ async fn test_pagination_cursor() { }; assert_eq!(page1, expected_page1); - let page2 = get_conversations(home, 2, page1.next_cursor.as_ref()) - .await - .unwrap(); + let page2 = get_conversations( + home, + 2, + page1.next_cursor.as_ref(), + INTERACTIVE_SESSION_SOURCES, + ) + .await + .unwrap(); let p3 = home .join("sessions") .join("2025") @@ -279,7 +360,8 @@ async fn test_pagination_cursor() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let head_2 = vec![serde_json::json!({ "id": u2, @@ -287,7 +369,8 @@ async fn test_pagination_cursor() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let expected_cursor2: Cursor = serde_json::from_str(&format!("\"2025-03-02T09-00-00|{u2}\"")).unwrap(); @@ -314,9 +397,14 @@ async fn test_pagination_cursor() { }; assert_eq!(page2, expected_page2); - let page3 = get_conversations(home, 2, page2.next_cursor.as_ref()) - .await - .unwrap(); + let page3 = get_conversations( + home, + 2, + page2.next_cursor.as_ref(), + INTERACTIVE_SESSION_SOURCES, + ) + .await + .unwrap(); let p1 = home .join("sessions") .join("2025") @@ -329,7 +417,8 @@ async fn test_pagination_cursor() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let expected_cursor3: Cursor = serde_json::from_str(&format!("\"2025-03-01T09-00-00|{u1}\"")).unwrap(); @@ -355,9 +444,11 @@ async fn test_get_conversation_contents() { let uuid = Uuid::new_v4(); let ts = "2025-04-01T10-30-00"; - write_session_file(home, ts, uuid, 2).unwrap(); + write_session_file(home, ts, uuid, 2, Some(SessionSource::VSCode)).unwrap(); - let page = get_conversations(home, 1, None).await.unwrap(); + let page = get_conversations(home, 1, None, INTERACTIVE_SESSION_SOURCES) + .await + .unwrap(); let path = &page.items[0].path; let content = get_conversation(path).await.unwrap(); @@ -375,7 +466,8 @@ async fn test_get_conversation_contents() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })]; let expected_cursor: Cursor = serde_json::from_str(&format!("\"{ts}|{uuid}\"")).unwrap(); let expected_page = ConversationsPage { @@ -393,7 +485,19 @@ async fn test_get_conversation_contents() { assert_eq!(page, expected_page); // Entire file contents equality - let meta = serde_json::json!({"timestamp": ts, "type": "session_meta", "payload": {"id": uuid, "timestamp": ts, "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version"}}); + let meta = serde_json::json!({ + "timestamp": ts, + "type": "session_meta", + "payload": { + "id": uuid, + "timestamp": ts, + "instructions": null, + "cwd": ".", + "originator": "test_originator", + "cli_version": "test_version", + "source": "vscode", + } + }); let user_event = serde_json::json!({ "timestamp": ts, "type": "event_msg", @@ -428,6 +532,7 @@ async fn test_tail_includes_last_response_items() -> Result<()> { cwd: ".".into(), originator: "test_originator".into(), cli_version: "test_version".into(), + source: SessionSource::VSCode, }, git: None, }), @@ -460,7 +565,7 @@ async fn test_tail_includes_last_response_items() -> Result<()> { } drop(file); - let page = get_conversations(home, 1, None).await?; + let page = get_conversations(home, 1, None, INTERACTIVE_SESSION_SOURCES).await?; let item = page.items.first().expect("conversation item"); let tail_len = item.tail.len(); assert_eq!(tail_len, 10usize.min(total_messages)); @@ -511,6 +616,7 @@ async fn test_tail_handles_short_sessions() -> Result<()> { cwd: ".".into(), originator: "test_originator".into(), cli_version: "test_version".into(), + source: SessionSource::VSCode, }, git: None, }), @@ -542,7 +648,7 @@ async fn test_tail_handles_short_sessions() -> Result<()> { } drop(file); - let page = get_conversations(home, 1, None).await?; + let page = get_conversations(home, 1, None, INTERACTIVE_SESSION_SOURCES).await?; let tail = &page.items.first().expect("conversation item").tail; assert_eq!(tail.len(), 3); @@ -595,6 +701,7 @@ async fn test_tail_skips_trailing_non_responses() -> Result<()> { cwd: ".".into(), originator: "test_originator".into(), cli_version: "test_version".into(), + source: SessionSource::VSCode, }, git: None, }), @@ -640,7 +747,7 @@ async fn test_tail_skips_trailing_non_responses() -> Result<()> { writeln!(file, "{}", serde_json::to_string(&shutdown_event)?)?; drop(file); - let page = get_conversations(home, 1, None).await?; + let page = get_conversations(home, 1, None, INTERACTIVE_SESSION_SOURCES).await?; let tail = &page.items.first().expect("conversation item").tail; let expected: Vec = (0..4) @@ -678,11 +785,13 @@ async fn test_stable_ordering_same_second_pagination() { let u2 = Uuid::from_u128(2); let u3 = Uuid::from_u128(3); - write_session_file(home, ts, u1, 0).unwrap(); - write_session_file(home, ts, u2, 0).unwrap(); - write_session_file(home, ts, u3, 0).unwrap(); + write_session_file(home, ts, u1, 0, Some(SessionSource::VSCode)).unwrap(); + write_session_file(home, ts, u2, 0, Some(SessionSource::VSCode)).unwrap(); + write_session_file(home, ts, u3, 0, Some(SessionSource::VSCode)).unwrap(); - let page1 = get_conversations(home, 2, None).await.unwrap(); + let page1 = get_conversations(home, 2, None, INTERACTIVE_SESSION_SOURCES) + .await + .unwrap(); let p3 = home .join("sessions") @@ -703,7 +812,8 @@ async fn test_stable_ordering_same_second_pagination() { "instructions": null, "cwd": ".", "originator": "test_originator", - "cli_version": "test_version" + "cli_version": "test_version", + "source": "vscode", })] }; let expected_cursor1: Cursor = serde_json::from_str(&format!("\"{ts}|{u2}\"")).unwrap(); @@ -730,9 +840,14 @@ async fn test_stable_ordering_same_second_pagination() { }; assert_eq!(page1, expected_page1); - let page2 = get_conversations(home, 2, page1.next_cursor.as_ref()) - .await - .unwrap(); + let page2 = get_conversations( + home, + 2, + page1.next_cursor.as_ref(), + INTERACTIVE_SESSION_SOURCES, + ) + .await + .unwrap(); let p1 = home .join("sessions") .join("2025") @@ -754,3 +869,59 @@ async fn test_stable_ordering_same_second_pagination() { }; assert_eq!(page2, expected_page2); } + +#[tokio::test] +async fn test_source_filter_excludes_non_matching_sessions() { + let temp = TempDir::new().unwrap(); + let home = temp.path(); + + let interactive_id = Uuid::from_u128(42); + let non_interactive_id = Uuid::from_u128(77); + + write_session_file( + home, + "2025-08-02T10-00-00", + interactive_id, + 2, + Some(SessionSource::Cli), + ) + .unwrap(); + write_session_file( + home, + "2025-08-01T10-00-00", + non_interactive_id, + 2, + Some(SessionSource::Exec), + ) + .unwrap(); + + let interactive_only = get_conversations(home, 10, None, INTERACTIVE_SESSION_SOURCES) + .await + .unwrap(); + let paths: Vec<_> = interactive_only + .items + .iter() + .map(|item| item.path.as_path()) + .collect(); + + assert_eq!(paths.len(), 1); + assert!(paths.iter().all(|path| { + path.ends_with("rollout-2025-08-02T10-00-00-00000000-0000-0000-0000-00000000002a.jsonl") + })); + + let all_sessions = get_conversations(home, 10, None, NO_SOURCE_FILTER) + .await + .unwrap(); + let all_paths: Vec<_> = all_sessions + .items + .into_iter() + .map(|item| item.path) + .collect(); + assert_eq!(all_paths.len(), 2); + assert!(all_paths.iter().any(|path| { + path.ends_with("rollout-2025-08-02T10-00-00-00000000-0000-0000-0000-00000000002a.jsonl") + })); + assert!(all_paths.iter().any(|path| { + path.ends_with("rollout-2025-08-01T10-00-00-00000000-0000-0000-0000-00000000004d.jsonl") + })); +} diff --git a/codex-rs/core/tests/suite/cli_stream.rs b/codex-rs/core/tests/suite/cli_stream.rs index 5b698934c0..8fc36772ad 100644 --- a/codex-rs/core/tests/suite/cli_stream.rs +++ b/codex-rs/core/tests/suite/cli_stream.rs @@ -76,7 +76,7 @@ async fn chat_mode_stream_cli() { server.verify().await; // Verify a new session rollout was created and is discoverable via list_conversations - let page = RolloutRecorder::list_conversations(home.path(), 10, None) + let page = RolloutRecorder::list_conversations(home.path(), 10, None, &[]) .await .expect("list conversations"); assert!( diff --git a/codex-rs/core/tests/suite/client.rs b/codex-rs/core/tests/suite/client.rs index d4ff9da2d6..7157a105fd 100644 --- a/codex-rs/core/tests/suite/client.rs +++ b/codex-rs/core/tests/suite/client.rs @@ -17,6 +17,7 @@ use codex_core::built_in_model_providers; use codex_core::protocol::EventMsg; use codex_core::protocol::InputItem; use codex_core::protocol::Op; +use codex_core::protocol::SessionSource; use codex_otel::otel_event_manager::OtelEventManager; use codex_protocol::ConversationId; use codex_protocol::models::ReasoningItemReasoningSummary; @@ -538,7 +539,7 @@ async fn prefers_apikey_when_config_prefers_apikey_even_with_chatgpt_tokens() { Ok(None) => panic!("No CodexAuth found in codex_home"), Err(e) => panic!("Failed to load CodexAuth: {e}"), }; - let conversation_manager = ConversationManager::new(auth_manager); + let conversation_manager = ConversationManager::new(auth_manager, SessionSource::Exec); let NewConversation { conversation: codex, .. diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index 0ec905de15..98f3d22fdc 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -17,6 +17,7 @@ use codex_core::protocol::Event; use codex_core::protocol::EventMsg; use codex_core::protocol::InputItem; use codex_core::protocol::Op; +use codex_core::protocol::SessionSource; use codex_core::protocol::TaskCompleteEvent; use codex_ollama::DEFAULT_OSS_MODEL; use codex_protocol::config_types::SandboxMode; @@ -237,7 +238,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any } let auth_manager = AuthManager::shared(config.codex_home.clone(), true); - let conversation_manager = ConversationManager::new(auth_manager.clone()); + let conversation_manager = ConversationManager::new(auth_manager.clone(), SessionSource::Exec); // Handle resume subcommand by resolving a rollout path and using explicit resume API. let NewConversation { @@ -375,7 +376,9 @@ async fn resolve_resume_path( args: &crate::cli::ResumeArgs, ) -> anyhow::Result> { if args.last { - match codex_core::RolloutRecorder::list_conversations(&config.codex_home, 1, None).await { + match codex_core::RolloutRecorder::list_conversations(&config.codex_home, 1, None, &[]) + .await + { Ok(page) => Ok(page.items.first().map(|it| it.path.clone())), Err(e) => { error!("Error listing conversations: {e}"); diff --git a/codex-rs/mcp-server/src/message_processor.rs b/codex-rs/mcp-server/src/message_processor.rs index 418c2a5162..bb24f5f0dc 100644 --- a/codex-rs/mcp-server/src/message_processor.rs +++ b/codex-rs/mcp-server/src/message_processor.rs @@ -8,6 +8,7 @@ use crate::codex_tool_config::create_tool_for_codex_tool_call_reply_param; use crate::error_code::INVALID_REQUEST_ERROR_CODE; use crate::outgoing_message::OutgoingMessageSender; use codex_protocol::ConversationId; +use codex_protocol::protocol::SessionSource; use codex_core::AuthManager; use codex_core::ConversationManager; @@ -53,7 +54,8 @@ impl MessageProcessor { ) -> Self { let outgoing = Arc::new(outgoing); let auth_manager = AuthManager::shared(config.codex_home.clone(), false); - let conversation_manager = Arc::new(ConversationManager::new(auth_manager)); + let conversation_manager = + Arc::new(ConversationManager::new(auth_manager, SessionSource::Mcp)); Self { outgoing, initialized: false, diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 17a4a844b8..2c6d3b33f0 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -919,7 +919,20 @@ impl InitialHistory { } } -#[derive(Serialize, Deserialize, Clone, Default, Debug, TS)] +#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Eq, TS, Default)] +#[serde(rename_all = "lowercase")] +#[ts(rename_all = "lowercase")] +pub enum SessionSource { + Cli, + #[default] + VSCode, + Exec, + Mcp, + #[serde(other)] + Unknown, +} + +#[derive(Serialize, Deserialize, Clone, Debug, TS)] pub struct SessionMeta { pub id: ConversationId, pub timestamp: String, @@ -927,6 +940,22 @@ pub struct SessionMeta { pub originator: String, pub cli_version: String, pub instructions: Option, + #[serde(default)] + pub source: SessionSource, +} + +impl Default for SessionMeta { + fn default() -> Self { + SessionMeta { + id: ConversationId::default(), + timestamp: String::new(), + cwd: PathBuf::new(), + originator: String::new(), + cli_version: String::new(), + instructions: None, + source: SessionSource::default(), + } + } } #[derive(Serialize, Deserialize, Debug, Clone, TS)] diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index ab43ceb0a3..cb3dea5e60 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -18,6 +18,7 @@ use codex_core::ConversationManager; use codex_core::config::Config; use codex_core::config::persist_model_selection; use codex_core::model_family::find_family_for_model; +use codex_core::protocol::SessionSource; use codex_core::protocol::TokenUsage; use codex_core::protocol_config_types::ReasoningEffort as ReasoningEffortConfig; use codex_protocol::ConversationId; @@ -86,7 +87,10 @@ impl App { let (app_event_tx, mut app_event_rx) = unbounded_channel(); let app_event_tx = AppEventSender::new(app_event_tx); - let conversation_manager = Arc::new(ConversationManager::new(auth_manager.clone())); + let conversation_manager = Arc::new(ConversationManager::new( + auth_manager.clone(), + SessionSource::Cli, + )); let enhanced_keys_supported = tui.enhanced_keys_supported(); diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index d8dde74f06..7807bf0a18 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -9,6 +9,7 @@ use codex_app_server_protocol::AuthMode; use codex_core::AuthManager; use codex_core::BUILT_IN_OSS_MODEL_PROVIDER_ID; use codex_core::CodexAuth; +use codex_core::INTERACTIVE_SESSION_SOURCES; use codex_core::RolloutRecorder; use codex_core::config::Config; use codex_core::config::ConfigOverrides; @@ -393,7 +394,14 @@ async fn run_ratatui_app( } } } else if cli.resume_last { - match RolloutRecorder::list_conversations(&config.codex_home, 1, None).await { + match RolloutRecorder::list_conversations( + &config.codex_home, + 1, + None, + INTERACTIVE_SESSION_SOURCES, + ) + .await + { Ok(page) => page .items .first() diff --git a/codex-rs/tui/src/resume_picker.rs b/codex-rs/tui/src/resume_picker.rs index ded7d41afc..eb2a9cd4ea 100644 --- a/codex-rs/tui/src/resume_picker.rs +++ b/codex-rs/tui/src/resume_picker.rs @@ -8,6 +8,7 @@ use chrono::Utc; use codex_core::ConversationItem; use codex_core::ConversationsPage; use codex_core::Cursor; +use codex_core::INTERACTIVE_SESSION_SOURCES; use codex_core::RolloutRecorder; use color_eyre::eyre::Result; use crossterm::event::KeyCode; @@ -77,6 +78,7 @@ pub async fn run_resume_picker(tui: &mut Tui, codex_home: &Path) -> Result Result<()> { - let page = RolloutRecorder::list_conversations(&self.codex_home, PAGE_SIZE, None).await?; + let page = RolloutRecorder::list_conversations( + &self.codex_home, + PAGE_SIZE, + None, + INTERACTIVE_SESSION_SOURCES, + ) + .await?; self.reset_pagination(); self.all_rows.clear(); self.filtered_rows.clear(); From b93cc0f4312586ea49682bacfea14a979f14e09f Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 2 Oct 2025 13:33:08 -0700 Subject: [PATCH 09/16] Add a separate exec doc (#4583) More/better docs. --- README.md | 1 + docs/advanced.md | 46 --------------------- docs/exec.md | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 46 deletions(-) create mode 100644 docs/exec.md diff --git a/README.md b/README.md index ae04239ae6..0a57a608eb 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Codex CLI supports a rich set of configuration options, with preferences stored - [**Authentication**](./docs/authentication.md) - [Auth methods](./docs/authentication.md#forcing-a-specific-auth-method-advanced) - [Login on a "Headless" machine](./docs/authentication.md#connecting-on-a-headless-machine) +- [**Non-interactive mode**](./docs/exec.md) - [**Advanced**](./docs/advanced.md) - [Non-interactive / CI mode](./docs/advanced.md#non-interactive--ci-mode) - [Tracing / verbose logging](./docs/advanced.md#tracing--verbose-logging) diff --git a/docs/advanced.md b/docs/advanced.md index 869a5fb533..d6a1e2ba8e 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -1,51 +1,5 @@ ## Advanced -## Non-interactive / CI mode - -Run Codex head-less in pipelines. Example GitHub Action step: - -```yaml -- name: Update changelog via Codex - run: | - npm install -g @openai/codex - codex login --api-key "${{ secrets.OPENAI_KEY }}" - codex exec --full-auto "update CHANGELOG for next release" -``` - -### Resuming non-interactive sessions - -You can resume a previous headless run to continue the same conversation context and append to the same rollout file. - -Interactive TUI equivalent: - -```shell -codex resume # picker -codex resume --last # most recent -codex resume -``` - -Compatibility: - -- Latest source builds include `codex exec resume` (examples below). -- Current released CLI may not include this yet. If `codex exec --help` shows no `resume`, use the workaround in the next subsection. - -```shell -# Resume the most recent recorded session and run with a new prompt (source builds) -codex exec "ship a release draft changelog" resume --last - -# Alternatively, pass the prompt via stdin (source builds) -# Note: omit the trailing '-' to avoid it being parsed as a SESSION_ID -echo "ship a release draft changelog" | codex exec resume --last - -# Or resume a specific session by id (UUID) (source builds) -codex exec resume 7f9f9a2e-1b3c-4c7a-9b0e-123456789abc "continue the task" -``` - -Notes: - -- When using `--last`, Codex picks the newest recorded session; if none exist, it behaves like starting fresh. -- Resuming appends new events to the existing session file and maintains the same conversation id. - ## Tracing / verbose logging Because Codex is written in Rust, it honors the `RUST_LOG` environment variable to configure its logging behavior. diff --git a/docs/exec.md b/docs/exec.md new file mode 100644 index 0000000000..5c3588eb5a --- /dev/null +++ b/docs/exec.md @@ -0,0 +1,105 @@ +## Non-interactive mode + +Use Codex in non-interactive mode to automate common workflows. + +```shell +codex exec "count the total number of lines of code in this project" +``` + +In non-interactive mode, Codex does not ask for command or edit approvals. By default it runs in `read-only` mode, so it cannot edit files or run commands that require network access. + +Use `codex exec --full-auto` to allow file edits. Use `codex exec --sandbox danger-full-access` to allow edits and networked commands. + + +### JSON output mode + +`codex exec` supports a `--json` mode that streams events to stdout as JSON Lines (JSONL) while the agent runs. + +Supported event types: +- `thread.started` - when a thread is started or resumed. +- `turn.started` - when a turn starts. A turn encompasses all events between the user message and the assistant response. +- `turn.completed` - when a turn completes; includes token usage. +- `turn.failed` - when a turn fails; includes error details. +- `item.started`/`item.updated`/`item.completed` - when a thread item is added/updated/completed. + +Supported item types: +- `assistant_message` - assistant message. +- `reasoning` - a summary of the assistant's thinking. +- `command_execution` - assistant executing a command. +- `file_change` - assistant making file changes. +- `mcp_tool_call` - assistant calling an MCP tool. +- `web_search` - assistant performing a web search. + +Typically, an `assistant_message` is added at the end of the turn. + +Sample output: +```jsonl +{"type":"thread.started","thread_id":"0199a213-81c0-7800-8aa1-bbab2a035a53"} +{"type":"turn.started"} +{"type":"item.completed","item":{"id":"item_0","item_type":"reasoning","text":"**Searching for README files**"}} +{"type":"item.started","item":{"id":"item_1","item_type":"command_execution","command":"bash -lc ls","aggregated_output":"","status":"in_progress"}} +{"type":"item.completed","item":{"id":"item_1","item_type":"command_execution","command":"bash -lc ls","aggregated_output":"2025-09-11\nAGENTS.md\nCHANGELOG.md\ncliff.toml\ncodex-cli\ncodex-rs\ndocs\nexamples\nflake.lock\nflake.nix\nLICENSE\nnode_modules\nNOTICE\npackage.json\npnpm-lock.yaml\npnpm-workspace.yaml\nPNPM.md\nREADME.md\nscripts\nsdk\ntmp\n","exit_code":0,"status":"completed"}} +{"type":"item.completed","item":{"id":"item_2","item_type":"reasoning","text":"**Checking repository root for README**"}} +{"type":"item.completed","item":{"id":"item_3","item_type":"assistant_message","text":"Yep — there’s a `README.md` in the repository root."}} +{"type":"turn.completed","usage":{"input_tokens":24763,"cached_input_tokens":24448,"output_tokens":122}} +``` + +### Structured output + +By default, the agent responds with natural language. Use `--output-schema` to provide a JSON Schema that defines the expected JSON output. + +The JSON Schema must follow the [strict schema rules](https://platform.openai.com/docs/guides/structured-outputs). + +Sample schema: + +```json +{ + "type": "object", + "properties": { + "project_name": { "type": "string" }, + "programming_languages": { "type": "array", "items": { "type": "string" } } + }, + "required": ["project_name", "programming_languages"], + "additionalProperties": false +} +``` + +```shell +codex exec "Extract details of the project" --output-schema ~/schema.json +... + +{"project_name":"Codex CLI","programming_languages":["Rust","TypeScript","Shell"]} +``` + +Combine `--output-schema` with `-o` to only print the final JSON output. You can also pass a file path to `-o` to save the JSON output to a file. + +### Git repository requirement + +Codex requires a Git repository to avoid destructive changes. To disable this check, use `codex exec --skip-git-repo-check`. + + +### Resuming non-interactive sessions + +Resume a previous non-interactive session with `codex exec resume ` or `codex exec resume --last`. This preserves conversation context so you can ask follow-up questions or give new tasks to the agent. + +```shell +codex exec "Review the change, look for use-after-free issues" +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" +``` + +## Authentication + +By default, `codex exec` will use the same authentication method as Codex CLI and VSCode extension. You can override the api key by setting the `CODEX_API_KEY` environment variable. + +```shell +CODEX_API_KEY=your-api-key-here codex exec "Fix merge conflict" +``` + +NOTE: `CODEX_API_KEY` is only supported in `codex exec`. From c43a5619165c5df41ba5a4f951ebf92d48486db7 Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 2 Oct 2025 13:39:45 -0700 Subject: [PATCH 10/16] Add issue labeler workflow (#4621) Auto label issues using codex cli --- .github/prompts/issue-labeler.txt | 26 ++++++++++ .github/workflows/issue-labeler.yml | 76 +++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 .github/prompts/issue-labeler.txt create mode 100644 .github/workflows/issue-labeler.yml diff --git a/.github/prompts/issue-labeler.txt b/.github/prompts/issue-labeler.txt new file mode 100644 index 0000000000..7e880d307f --- /dev/null +++ b/.github/prompts/issue-labeler.txt @@ -0,0 +1,26 @@ +You are an assistant that reviews GitHub issues for the repository. + +Your job is to choose the most appropriate existing labels for the issue described later in this prompt. +Follow these rules: +- Only pick labels out of the list below. +- Prefer a small set of precise labels over many broad ones. +- If none of the labels fit, respond with an empty JSON array: [] +- Output must be a JSON array of label names (strings) with no additional commentary. + +Labels to apply: +1. bug — Reproducible defects in Codex products (CLI, VS Code extension, web, auth). +2. enhancement — Feature requests or usability improvements that ask for new capabilities, better ergonomics, or quality-of-life tweaks. +3. extension — VS Code (or other IDE) extension-specific issues. +4. windows-os — Bugs or friction specific to Windows environments (PowerShell behavior, path handling, copy/paste, OS-specific auth or tooling failures). +5. mcp — Topics involving Model Context Protocol servers/clients. +6. codex-web — Issues targeting the Codex web UI/Cloud experience. +8. azure — Problems or requests tied to Azure OpenAI deployments. +9. documentation — Updates or corrections needed in docs/README/config references (broken links, missing examples, outdated keys, clarification requests). +10. model-behavior — Undesirable LLM behavior: forgetting goals, refusing work, hallucinating environment details, quota misreports, or other reasoning/performance anomalies. + +Issue information is available in environment variables: + +ISSUE_NUMBER +ISSUE_TITLE +ISSUE_BODY +REPO_FULL_NAME diff --git a/.github/workflows/issue-labeler.yml b/.github/workflows/issue-labeler.yml new file mode 100644 index 0000000000..dfbd17c27f --- /dev/null +++ b/.github/workflows/issue-labeler.yml @@ -0,0 +1,76 @@ +name: Issue Labeler + +on: + issues: + types: +# - opened - disabled while testing + - labeled + +jobs: + gather-labels: + name: Generate label suggestions + if: ${{ github.event.action == 'opened' || (github.event.action == 'labeled' && github.event.label.name == 'codex-label') }} + runs-on: ubuntu-latest + permissions: + contents: read + env: + ISSUE_NUMBER: ${{ github.event.issue.number }} + ISSUE_TITLE: ${{ github.event.issue.title }} + ISSUE_BODY: ${{ github.event.issue.body }} + REPO_FULL_NAME: ${{ github.repository }} + outputs: + codex_output: ${{ steps.codex.outputs.final_message }} + steps: + - uses: actions/checkout@v4 + + - id: codex + uses: openai/codex-action@main + with: + openai_api_key: ${{ secrets.CODEX_OPENAI_API_KEY }} + prompt_file: .github/prompts/issue-labeler.txt + + apply-labels: + name: Apply labels from Codex output + needs: gather-labels + if: ${{ needs.gather-labels.result != 'skipped' }} + runs-on: ubuntu-latest + permissions: + contents: read + issues: write + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + CODEX_OUTPUT: ${{ needs.gather-labels.outputs.codex_output }} + steps: + - name: Apply labels + run: | + json=${CODEX_OUTPUT//$'\r'/} + if [ -z "$json" ]; then + echo "Codex produced no output. Skipping label application." + exit 0 + fi + + if ! printf '%s' "$json" | jq -e 'type == "array"' >/dev/null 2>&1; then + echo "Codex output was not a JSON array. Raw output: $json" + exit 0 + fi + + labels=$(printf '%s' "$json" | jq -r '.[] | tostring') + if [ -z "$labels" ]; then + echo "Codex returned an empty array. Nothing to do." + exit 0 + fi + + cmd=(gh issue edit "$ISSUE_NUMBER") + while IFS= read -r label; do + cmd+=(--add-label "$label") + done <<< "$labels" + + "${cmd[@]}" || true + + - name: Remove codex-label trigger + if: ${{ always() && github.event.action == 'labeled' && github.event.label.name == 'codex-label' }} + run: | + gh issue edit "$ISSUE_NUMBER" --remove-label codex-label || true + echo "Attempted to remove label: codex-label" From ed5d656fa8fbb3136cc8fcb7a870929143c2467a Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Thu, 2 Oct 2025 14:09:21 -0700 Subject: [PATCH 11/16] Revert "chore: sanbox extraction" (#4626) Reverts openai/codex#4286 --- .../app-server/tests/common/mcp_process.rs | 40 +- .../suite/codex_message_processor_flow.rs | 233 --------- codex-rs/core/src/apply_patch.rs | 26 - codex-rs/core/src/codex.rs | 495 ++++++++++++++---- codex-rs/core/src/executor/backends.rs | 101 ---- codex-rs/core/src/executor/cache.rs | 51 -- codex-rs/core/src/executor/mod.rs | 64 --- codex-rs/core/src/executor/runner.rs | 387 -------------- codex-rs/core/src/executor/sandbox.rs | 405 -------------- codex-rs/core/src/lib.rs | 1 - codex-rs/core/src/safety.rs | 5 +- codex-rs/core/src/state/service.rs | 4 +- codex-rs/core/src/state/session.rs | 12 + codex-rs/core/tests/suite/seatbelt.rs | 6 - 14 files changed, 414 insertions(+), 1416 deletions(-) delete mode 100644 codex-rs/core/src/executor/backends.rs delete mode 100644 codex-rs/core/src/executor/cache.rs delete mode 100644 codex-rs/core/src/executor/mod.rs delete mode 100644 codex-rs/core/src/executor/runner.rs delete mode 100644 codex-rs/core/src/executor/sandbox.rs diff --git a/codex-rs/app-server/tests/common/mcp_process.rs b/codex-rs/app-server/tests/common/mcp_process.rs index 29768df81c..bdc96cad0a 100644 --- a/codex-rs/app-server/tests/common/mcp_process.rs +++ b/codex-rs/app-server/tests/common/mcp_process.rs @@ -1,4 +1,3 @@ -use std::collections::VecDeque; use std::path::Path; use std::process::Stdio; use std::sync::atomic::AtomicI64; @@ -48,7 +47,6 @@ pub struct McpProcess { process: Child, stdin: ChildStdin, stdout: BufReader, - pending_user_messages: VecDeque, } impl McpProcess { @@ -119,7 +117,6 @@ impl McpProcess { process, stdin, stdout, - pending_user_messages: VecDeque::new(), }) } @@ -378,9 +375,8 @@ impl McpProcess { let message = self.read_jsonrpc_message().await?; match message { - JSONRPCMessage::Notification(notification) => { - eprintln!("notification: {notification:?}"); - self.enqueue_user_message(notification); + JSONRPCMessage::Notification(_) => { + eprintln!("notification: {message:?}"); } JSONRPCMessage::Request(jsonrpc_request) => { return jsonrpc_request.try_into().with_context( @@ -406,9 +402,8 @@ impl McpProcess { loop { let message = self.read_jsonrpc_message().await?; match message { - JSONRPCMessage::Notification(notification) => { - eprintln!("notification: {notification:?}"); - self.enqueue_user_message(notification); + JSONRPCMessage::Notification(_) => { + eprintln!("notification: {message:?}"); } JSONRPCMessage::Request(_) => { anyhow::bail!("unexpected JSONRPCMessage::Request: {message:?}"); @@ -432,9 +427,8 @@ impl McpProcess { loop { let message = self.read_jsonrpc_message().await?; match message { - JSONRPCMessage::Notification(notification) => { - eprintln!("notification: {notification:?}"); - self.enqueue_user_message(notification); + JSONRPCMessage::Notification(_) => { + eprintln!("notification: {message:?}"); } JSONRPCMessage::Request(_) => { anyhow::bail!("unexpected JSONRPCMessage::Request: {message:?}"); @@ -457,10 +451,6 @@ impl McpProcess { ) -> anyhow::Result { eprintln!("in read_stream_until_notification_message({method})"); - if let Some(notification) = self.take_pending_notification_by_method(method) { - return Ok(notification); - } - loop { let message = self.read_jsonrpc_message().await?; match message { @@ -468,7 +458,6 @@ impl McpProcess { if notification.method == method { return Ok(notification); } - self.enqueue_user_message(notification); } JSONRPCMessage::Request(_) => { anyhow::bail!("unexpected JSONRPCMessage::Request: {message:?}"); @@ -482,21 +471,4 @@ impl McpProcess { } } } - - fn take_pending_notification_by_method(&mut self, method: &str) -> Option { - if let Some(pos) = self - .pending_user_messages - .iter() - .position(|notification| notification.method == method) - { - return self.pending_user_messages.remove(pos); - } - None - } - - fn enqueue_user_message(&mut self, notification: JSONRPCNotification) { - if notification.method == "codex/event/user_message" { - self.pending_user_messages.push_back(notification); - } - } } diff --git a/codex-rs/app-server/tests/suite/codex_message_processor_flow.rs b/codex-rs/app-server/tests/suite/codex_message_processor_flow.rs index 4dff2a1575..f1f34f952f 100644 --- a/codex-rs/app-server/tests/suite/codex_message_processor_flow.rs +++ b/codex-rs/app-server/tests/suite/codex_message_processor_flow.rs @@ -8,7 +8,6 @@ use app_test_support::to_response; use codex_app_server_protocol::AddConversationListenerParams; use codex_app_server_protocol::AddConversationSubscriptionResponse; use codex_app_server_protocol::ExecCommandApprovalParams; -use codex_app_server_protocol::InputItem; use codex_app_server_protocol::JSONRPCNotification; use codex_app_server_protocol::JSONRPCResponse; use codex_app_server_protocol::NewConversationParams; @@ -26,10 +25,6 @@ use codex_core::protocol::SandboxPolicy; use codex_core::protocol_config_types::ReasoningEffort; use codex_core::protocol_config_types::ReasoningSummary; use codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR; -use codex_protocol::config_types::SandboxMode; -use codex_protocol::protocol::Event; -use codex_protocol::protocol::EventMsg; -use codex_protocol::protocol::InputMessageKind; use pretty_assertions::assert_eq; use std::env; use tempfile::TempDir; @@ -372,234 +367,6 @@ async fn test_send_user_turn_changes_approval_policy_behavior() { } // Helper: minimal config.toml pointing at mock provider. - -#[tokio::test(flavor = "multi_thread", worker_threads = 4)] -async fn test_send_user_turn_updates_sandbox_and_cwd_between_turns() { - if env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() { - println!( - "Skipping test because it cannot execute when network is disabled in a Codex sandbox." - ); - return; - } - - let tmp = TempDir::new().expect("tmp dir"); - let codex_home = tmp.path().join("codex_home"); - std::fs::create_dir(&codex_home).expect("create codex home dir"); - let workspace_root = tmp.path().join("workspace"); - std::fs::create_dir(&workspace_root).expect("create workspace root"); - let first_cwd = workspace_root.join("turn1"); - let second_cwd = workspace_root.join("turn2"); - std::fs::create_dir(&first_cwd).expect("create first cwd"); - std::fs::create_dir(&second_cwd).expect("create second cwd"); - - let responses = vec![ - create_shell_sse_response( - vec![ - "bash".to_string(), - "-lc".to_string(), - "echo first turn".to_string(), - ], - None, - Some(5000), - "call-first", - ) - .expect("create first shell response"), - create_final_assistant_message_sse_response("done first") - .expect("create first final assistant message"), - create_shell_sse_response( - vec![ - "bash".to_string(), - "-lc".to_string(), - "echo second turn".to_string(), - ], - None, - Some(5000), - "call-second", - ) - .expect("create second shell response"), - create_final_assistant_message_sse_response("done second") - .expect("create second final assistant message"), - ]; - let server = create_mock_chat_completions_server(responses).await; - create_config_toml(&codex_home, &server.uri()).expect("write config"); - - let mut mcp = McpProcess::new(&codex_home) - .await - .expect("spawn mcp process"); - timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()) - .await - .expect("init timeout") - .expect("init failed"); - - let new_conv_id = mcp - .send_new_conversation_request(NewConversationParams { - cwd: Some(first_cwd.to_string_lossy().into_owned()), - approval_policy: Some(AskForApproval::Never), - sandbox: Some(SandboxMode::WorkspaceWrite), - ..Default::default() - }) - .await - .expect("send newConversation"); - let new_conv_resp: JSONRPCResponse = timeout( - DEFAULT_READ_TIMEOUT, - mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), - ) - .await - .expect("newConversation timeout") - .expect("newConversation resp"); - let NewConversationResponse { - conversation_id, - model, - .. - } = to_response::(new_conv_resp) - .expect("deserialize newConversation response"); - - let add_listener_id = mcp - .send_add_conversation_listener_request(AddConversationListenerParams { conversation_id }) - .await - .expect("send addConversationListener"); - timeout( - DEFAULT_READ_TIMEOUT, - mcp.read_stream_until_response_message(RequestId::Integer(add_listener_id)), - ) - .await - .expect("addConversationListener timeout") - .expect("addConversationListener resp"); - - let first_turn_id = mcp - .send_send_user_turn_request(SendUserTurnParams { - conversation_id, - items: vec![InputItem::Text { - text: "first turn".to_string(), - }], - cwd: first_cwd.clone(), - approval_policy: AskForApproval::Never, - sandbox_policy: SandboxPolicy::WorkspaceWrite { - writable_roots: vec![first_cwd.clone()], - network_access: false, - exclude_tmpdir_env_var: false, - exclude_slash_tmp: false, - }, - model: model.clone(), - effort: Some(ReasoningEffort::Medium), - summary: ReasoningSummary::Auto, - }) - .await - .expect("send first sendUserTurn"); - timeout( - DEFAULT_READ_TIMEOUT, - mcp.read_stream_until_response_message(RequestId::Integer(first_turn_id)), - ) - .await - .expect("sendUserTurn 1 timeout") - .expect("sendUserTurn 1 resp"); - timeout( - DEFAULT_READ_TIMEOUT, - mcp.read_stream_until_notification_message("codex/event/task_complete"), - ) - .await - .expect("task_complete 1 timeout") - .expect("task_complete 1 notification"); - - let second_turn_id = mcp - .send_send_user_turn_request(SendUserTurnParams { - conversation_id, - items: vec![InputItem::Text { - text: "second turn".to_string(), - }], - cwd: second_cwd.clone(), - approval_policy: AskForApproval::Never, - sandbox_policy: SandboxPolicy::DangerFullAccess, - model: model.clone(), - effort: Some(ReasoningEffort::Medium), - summary: ReasoningSummary::Auto, - }) - .await - .expect("send second sendUserTurn"); - timeout( - DEFAULT_READ_TIMEOUT, - mcp.read_stream_until_response_message(RequestId::Integer(second_turn_id)), - ) - .await - .expect("sendUserTurn 2 timeout") - .expect("sendUserTurn 2 resp"); - - let mut env_message: Option = None; - let second_cwd_str = second_cwd.to_string_lossy().into_owned(); - for _ in 0..10 { - let notification = timeout( - DEFAULT_READ_TIMEOUT, - mcp.read_stream_until_notification_message("codex/event/user_message"), - ) - .await - .expect("user_message timeout") - .expect("user_message notification"); - let params = notification - .params - .clone() - .expect("user_message should include params"); - let event: Event = serde_json::from_value(params).expect("deserialize user_message event"); - if let EventMsg::UserMessage(user) = event.msg - && matches!(user.kind, Some(InputMessageKind::EnvironmentContext)) - && user.message.contains(&second_cwd_str) - { - env_message = Some(user.message); - break; - } - } - let env_message = env_message.expect("expected environment context update"); - assert!( - env_message.contains("danger-full-access"), - "env context should reflect new sandbox mode: {env_message}" - ); - assert!( - env_message.contains("enabled"), - "env context should enable network access for danger-full-access policy: {env_message}" - ); - assert!( - env_message.contains(&second_cwd_str), - "env context should include updated cwd: {env_message}" - ); - - let exec_begin_notification = timeout( - DEFAULT_READ_TIMEOUT, - mcp.read_stream_until_notification_message("codex/event/exec_command_begin"), - ) - .await - .expect("exec_command_begin timeout") - .expect("exec_command_begin notification"); - let params = exec_begin_notification - .params - .clone() - .expect("exec_command_begin params"); - let event: Event = serde_json::from_value(params).expect("deserialize exec begin event"); - let exec_begin = match event.msg { - EventMsg::ExecCommandBegin(exec_begin) => exec_begin, - other => panic!("expected ExecCommandBegin event, got {other:?}"), - }; - assert_eq!( - exec_begin.cwd, second_cwd, - "exec turn should run from updated cwd" - ); - assert_eq!( - exec_begin.command, - vec![ - "bash".to_string(), - "-lc".to_string(), - "echo second turn".to_string() - ], - "exec turn should run expected command" - ); - - timeout( - DEFAULT_READ_TIMEOUT, - mcp.read_stream_until_notification_message("codex/event/task_complete"), - ) - .await - .expect("task_complete 2 timeout") - .expect("task_complete 2 notification"); -} - fn create_config_toml(codex_home: &Path, server_uri: &str) -> std::io::Result<()> { let config_toml = codex_home.join("config.toml"); std::fs::write( diff --git a/codex-rs/core/src/apply_patch.rs b/codex-rs/core/src/apply_patch.rs index 5b6728ad0f..836b859633 100644 --- a/codex-rs/core/src/apply_patch.rs +++ b/codex-rs/core/src/apply_patch.rs @@ -27,7 +27,6 @@ pub(crate) enum InternalApplyPatchInvocation { DelegateToExec(ApplyPatchExec), } -#[derive(Debug)] pub(crate) struct ApplyPatchExec { pub(crate) action: ApplyPatchAction, pub(crate) user_explicitly_approved_this_action: bool, @@ -110,28 +109,3 @@ pub(crate) fn convert_apply_patch_to_protocol( } result } - -#[cfg(test)] -mod tests { - use super::*; - use pretty_assertions::assert_eq; - - use tempfile::tempdir; - - #[test] - fn convert_apply_patch_maps_add_variant() { - let tmp = tempdir().expect("tmp"); - let p = tmp.path().join("a.txt"); - // Create an action with a single Add change - let action = ApplyPatchAction::new_add_for_test(&p, "hello".to_string()); - - let got = convert_apply_patch_to_protocol(&action); - - assert_eq!( - got.get(&p), - Some(&FileChange::Add { - content: "hello".to_string() - }) - ); - } -} diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 2bba2b42b7..4baa63ec3e 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -1,9 +1,11 @@ use std::borrow::Cow; use std::collections::HashMap; use std::fmt::Debug; +use std::path::Path; use std::path::PathBuf; use std::sync::Arc; use std::sync::atomic::AtomicU64; +use std::time::Duration; use crate::AuthManager; use crate::client_common::REVIEW_PROMPT; @@ -43,6 +45,7 @@ use tracing::warn; use crate::ModelProviderInfo; use crate::apply_patch; use crate::apply_patch::ApplyPatchExec; +use crate::apply_patch::CODEX_APPLY_PATCH_ARG1; use crate::apply_patch::InternalApplyPatchInvocation; use crate::apply_patch::convert_apply_patch_to_protocol; use crate::client::ModelClient; @@ -55,21 +58,19 @@ use crate::environment_context::EnvironmentContext; use crate::error::CodexErr; use crate::error::Result as CodexResult; use crate::error::SandboxErr; +use crate::error::get_error_message_ui; use crate::exec::ExecParams; use crate::exec::ExecToolCallOutput; +use crate::exec::SandboxType; use crate::exec::StdoutStream; -#[cfg(test)] use crate::exec::StreamOutput; +use crate::exec::process_exec_tool_call; use crate::exec_command::EXEC_COMMAND_TOOL_NAME; use crate::exec_command::ExecCommandParams; use crate::exec_command::ExecSessionManager; use crate::exec_command::WRITE_STDIN_TOOL_NAME; use crate::exec_command::WriteStdinParams; use crate::exec_env::create_env; -use crate::executor::ExecutionMode; -use crate::executor::Executor; -use crate::executor::ExecutorConfig; -use crate::executor::normalize_exec_result; use crate::mcp_connection_manager::McpConnectionManager; use crate::mcp_tool_call::handle_mcp_tool_call; use crate::model_family::find_family_for_model; @@ -114,6 +115,9 @@ use crate::protocol::ViewImageToolCallEvent; use crate::protocol::WebSearchBeginEvent; use crate::rollout::RolloutRecorder; use crate::rollout::RolloutRecorderParams; +use crate::safety::SafetyCheck; +use crate::safety::assess_command_safety; +use crate::safety::assess_safety_for_untrusted_command; use crate::shell; use crate::state::ActiveTurn; use crate::state::SessionServices; @@ -126,6 +130,7 @@ use crate::user_instructions::UserInstructions; use crate::user_notification::UserNotification; use crate::util::backoff; use codex_otel::otel_event_manager::OtelEventManager; +use codex_otel::otel_event_manager::ToolDecisionSource; use codex_protocol::config_types::ReasoningEffort as ReasoningEffortConfig; use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig; use codex_protocol::custom_prompts::CustomPrompt; @@ -490,13 +495,9 @@ impl Session { unified_exec_manager: UnifiedExecSessionManager::default(), notifier: notify, rollout: Mutex::new(Some(rollout_recorder)), + codex_linux_sandbox_exe: config.codex_linux_sandbox_exe.clone(), user_shell: default_shell, show_raw_agent_reasoning: config.show_raw_agent_reasoning, - executor: Executor::new(ExecutorConfig::new( - turn_context.sandbox_policy.clone(), - turn_context.cwd.clone(), - config.codex_linux_sandbox_exe.clone(), - )), }; let sess = Arc::new(Session { @@ -581,11 +582,6 @@ impl Session { } } - /// Emit an exec approval request event and await the user's decision. - /// - /// The request is keyed by `sub_id`/`call_id` so matching responses are delivered - /// to the correct in-flight turn. If the task is aborted, this returns the - /// default `ReviewDecision` (`Denied`). pub async fn request_command_approval( &self, sub_id: String, @@ -683,6 +679,11 @@ impl Session { } } + pub async fn add_approved_command(&self, cmd: Vec) { + let mut state = self.state.lock().await; + state.add_approved_command(cmd); + } + /// Records input items: always append to conversation history and /// persist these response items to rollout. async fn record_conversation_items(&self, items: &[ResponseItem]) { @@ -840,7 +841,6 @@ impl Session { command_for_display, cwd, apply_patch, - .. } = exec_command_context; let msg = match apply_patch { Some(ApplyPatchCommandContext { @@ -937,29 +937,45 @@ impl Session { /// command even on error. /// /// Returns the output of the exec tool call. - async fn run_exec_with_events( + async fn run_exec_with_events<'a>( &self, turn_diff_tracker: &mut TurnDiffTracker, - prepared: PreparedExec, - approval_policy: AskForApproval, - ) -> Result { - let PreparedExec { context, request } = prepared; - let is_apply_patch = context.apply_patch.is_some(); - let sub_id = context.sub_id.clone(); - let call_id = context.call_id.clone(); + begin_ctx: ExecCommandContext, + exec_args: ExecInvokeArgs<'a>, + ) -> crate::error::Result { + let is_apply_patch = begin_ctx.apply_patch.is_some(); + let sub_id = begin_ctx.sub_id.clone(); + let call_id = begin_ctx.call_id.clone(); - self.on_exec_command_begin(turn_diff_tracker, context.clone()) + self.on_exec_command_begin(turn_diff_tracker, begin_ctx.clone()) .await; - let result = self - .services - .executor - .run(request, self, approval_policy, &context) - .await; - - let normalized = normalize_exec_result(&result); - let borrowed = normalized.event_output(); + let result = process_exec_tool_call( + exec_args.params, + exec_args.sandbox_type, + exec_args.sandbox_policy, + exec_args.sandbox_cwd, + exec_args.codex_linux_sandbox_exe, + exec_args.stdout_stream, + ) + .await; + let output_stderr; + let borrowed: &ExecToolCallOutput = match &result { + Ok(output) => output, + Err(CodexErr::Sandbox(SandboxErr::Timeout { output })) => output, + Err(e) => { + output_stderr = ExecToolCallOutput { + exit_code: -1, + stdout: StreamOutput::new(String::new()), + stderr: StreamOutput::new(get_error_message_ui(e)), + aggregated_output: StreamOutput::new(get_error_message_ui(e)), + duration: Duration::default(), + timed_out: false, + }; + &output_stderr + } + }; self.on_exec_command_end( turn_diff_tracker, &sub_id, @@ -969,15 +985,13 @@ impl Session { ) .await; - drop(normalized); - result } /// Helper that emits a BackgroundEvent with the given message. This keeps /// the call‑sites terse so adding more diagnostics does not clutter the /// core agent logic. - pub(crate) async fn notify_background_event(&self, sub_id: &str, message: impl Into) { + async fn notify_background_event(&self, sub_id: &str, message: impl Into) { let event = Event { id: sub_id.to_string(), msg: EventMsg::BackgroundEvent(BackgroundEventEvent { @@ -1065,7 +1079,7 @@ impl Session { &self.services.notifier } - pub(crate) fn user_shell(&self) -> &shell::Shell { + fn user_shell(&self) -> &shell::Shell { &self.services.user_shell } @@ -1087,8 +1101,6 @@ pub(crate) struct ExecCommandContext { pub(crate) command_for_display: Vec, pub(crate) cwd: PathBuf, pub(crate) apply_patch: Option, - pub(crate) tool_name: String, - pub(crate) otel_event_manager: OtelEventManager, } #[derive(Clone, Debug)] @@ -1295,19 +1307,8 @@ async fn submission_loop( let previous_env_context = EnvironmentContext::from(turn_context.as_ref()); let new_env_context = EnvironmentContext::from(&fresh_turn_context); if !new_env_context.equals_except_shell(&previous_env_context) { - let env_response_item = ResponseItem::from(new_env_context); - sess.record_conversation_items(std::slice::from_ref(&env_response_item)) + sess.record_conversation_items(&[ResponseItem::from(new_env_context)]) .await; - for msg in map_response_item_to_event_messages( - &env_response_item, - sess.show_raw_agent_reasoning(), - ) { - let event = Event { - id: sub.id.clone(), - msg, - }; - sess.send_event(event).await; - } } // Install the new persistent context for subsequent tasks/turns. @@ -2626,6 +2627,33 @@ fn parse_container_exec_arguments( }) } +pub struct ExecInvokeArgs<'a> { + pub params: ExecParams, + pub sandbox_type: SandboxType, + pub sandbox_policy: &'a SandboxPolicy, + pub sandbox_cwd: &'a Path, + pub codex_linux_sandbox_exe: &'a Option, + pub stdout_stream: Option, +} + +fn maybe_translate_shell_command( + params: ExecParams, + sess: &Session, + turn_context: &TurnContext, +) -> ExecParams { + let should_translate = matches!(sess.user_shell(), crate::shell::Shell::PowerShell(_)) + || turn_context.shell_environment_policy.use_profile; + + if should_translate + && let Some(command) = sess + .user_shell() + .format_default_shell_invocation(params.command.clone()) + { + return ExecParams { command, ..params }; + } + params +} + async fn handle_container_exec_with_params( tool_name: &str, params: ExecParams, @@ -2671,10 +2699,152 @@ async fn handle_container_exec_with_params( MaybeApplyPatchVerified::NotApplyPatch => None, }; - let command_for_display = if let Some(exec) = apply_patch_exec.as_ref() { - vec!["apply_patch".to_string(), exec.action.patch.clone()] - } else { - params.command.clone() + let (params, safety, command_for_display) = match &apply_patch_exec { + Some(ApplyPatchExec { + action: ApplyPatchAction { patch, cwd, .. }, + user_explicitly_approved_this_action, + }) => { + let path_to_codex = std::env::current_exe() + .ok() + .map(|p| p.to_string_lossy().to_string()); + let Some(path_to_codex) = path_to_codex else { + return Err(FunctionCallError::RespondToModel( + "failed to determine path to codex executable".to_string(), + )); + }; + + let params = ExecParams { + command: vec![ + path_to_codex, + CODEX_APPLY_PATCH_ARG1.to_string(), + patch.clone(), + ], + cwd: cwd.clone(), + timeout_ms: params.timeout_ms, + env: HashMap::new(), + with_escalated_permissions: params.with_escalated_permissions, + justification: params.justification.clone(), + }; + let safety = if *user_explicitly_approved_this_action { + SafetyCheck::AutoApprove { + sandbox_type: SandboxType::None, + user_explicitly_approved: true, + } + } else { + assess_safety_for_untrusted_command( + turn_context.approval_policy, + &turn_context.sandbox_policy, + params.with_escalated_permissions.unwrap_or(false), + ) + }; + ( + params, + safety, + vec!["apply_patch".to_string(), patch.clone()], + ) + } + None => { + let safety = { + let state = sess.state.lock().await; + assess_command_safety( + ¶ms.command, + turn_context.approval_policy, + &turn_context.sandbox_policy, + state.approved_commands_ref(), + params.with_escalated_permissions.unwrap_or(false), + ) + }; + let command_for_display = params.command.clone(); + (params, safety, command_for_display) + } + }; + + let sandbox_type = match safety { + SafetyCheck::AutoApprove { + sandbox_type, + user_explicitly_approved, + } => { + otel_event_manager.tool_decision( + tool_name, + call_id.as_str(), + ReviewDecision::Approved, + if user_explicitly_approved { + ToolDecisionSource::User + } else { + ToolDecisionSource::Config + }, + ); + + sandbox_type + } + SafetyCheck::AskUser => { + let decision = sess + .request_command_approval( + sub_id.clone(), + call_id.clone(), + params.command.clone(), + params.cwd.clone(), + params.justification.clone(), + ) + .await; + match decision { + ReviewDecision::Approved => { + otel_event_manager.tool_decision( + tool_name, + call_id.as_str(), + ReviewDecision::Approved, + ToolDecisionSource::User, + ); + } + ReviewDecision::ApprovedForSession => { + otel_event_manager.tool_decision( + tool_name, + call_id.as_str(), + ReviewDecision::ApprovedForSession, + ToolDecisionSource::User, + ); + sess.add_approved_command(params.command.clone()).await; + } + ReviewDecision::Denied => { + otel_event_manager.tool_decision( + tool_name, + call_id.as_str(), + ReviewDecision::Denied, + ToolDecisionSource::User, + ); + return Err(FunctionCallError::RespondToModel( + "exec command rejected by user".to_string(), + )); + } + ReviewDecision::Abort => { + otel_event_manager.tool_decision( + tool_name, + call_id.as_str(), + ReviewDecision::Abort, + ToolDecisionSource::User, + ); + return Err(FunctionCallError::RespondToModel( + "exec command aborted by user".to_string(), + )); + } + } + // No sandboxing is applied because the user has given + // explicit approval. Often, we end up in this case because + // the command cannot be run in a sandbox, such as + // installing a new dependency that requires network access. + SandboxType::None + } + SafetyCheck::Reject { reason } => { + otel_event_manager.tool_decision( + tool_name, + call_id.as_str(), + ReviewDecision::Denied, + ToolDecisionSource::Config, + ); + return Err(FunctionCallError::RespondToModel(format!( + "exec command rejected: {reason:?}" + ))); + } }; let exec_command_context = ExecCommandContext { @@ -2682,47 +2852,38 @@ async fn handle_container_exec_with_params( call_id: call_id.clone(), command_for_display: command_for_display.clone(), cwd: params.cwd.clone(), - apply_patch: apply_patch_exec.as_ref().map( + apply_patch: apply_patch_exec.map( |ApplyPatchExec { action, user_explicitly_approved_this_action, }| ApplyPatchCommandContext { - user_explicitly_approved_this_action: *user_explicitly_approved_this_action, - changes: convert_apply_patch_to_protocol(action), + user_explicitly_approved_this_action, + changes: convert_apply_patch_to_protocol(&action), }, ), - tool_name: tool_name.to_string(), - otel_event_manager, }; - let mode = match apply_patch_exec { - Some(exec) => ExecutionMode::ApplyPatch(exec), - None => ExecutionMode::Shell, - }; - - sess.services.executor.update_environment( - turn_context.sandbox_policy.clone(), - turn_context.cwd.clone(), - ); - - let prepared_exec = PreparedExec::new( - exec_command_context, - params, - command_for_display, - mode, - Some(StdoutStream { - sub_id: sub_id.clone(), - call_id: call_id.clone(), - tx_event: sess.tx_event.clone(), - }), - turn_context.shell_environment_policy.use_profile, - ); - + let params = maybe_translate_shell_command(params, sess, turn_context); let output_result = sess .run_exec_with_events( turn_diff_tracker, - prepared_exec, - turn_context.approval_policy, + exec_command_context.clone(), + ExecInvokeArgs { + params: params.clone(), + sandbox_type, + sandbox_policy: &turn_context.sandbox_policy, + sandbox_cwd: &turn_context.cwd, + codex_linux_sandbox_exe: &sess.services.codex_linux_sandbox_exe, + stdout_stream: if exec_command_context.apply_patch.is_some() { + None + } else { + Some(StdoutStream { + sub_id: sub_id.clone(), + call_id: call_id.clone(), + tx_event: sess.tx_event.clone(), + }) + }, + }, ) .await; @@ -2736,16 +2897,154 @@ async fn handle_container_exec_with_params( Err(FunctionCallError::RespondToModel(content)) } } - Err(ExecError::Function(err)) => Err(err), - Err(ExecError::Codex(CodexErr::Sandbox(SandboxErr::Timeout { output }))) => Err( - FunctionCallError::RespondToModel(format_exec_output(&output)), - ), - Err(ExecError::Codex(err)) => Err(FunctionCallError::RespondToModel(format!( - "execution error: {err:?}" + Err(CodexErr::Sandbox(error)) => { + handle_sandbox_error( + tool_name, + turn_diff_tracker, + params, + exec_command_context, + error, + sandbox_type, + sess, + turn_context, + &otel_event_manager, + ) + .await + } + Err(e) => Err(FunctionCallError::RespondToModel(format!( + "execution error: {e:?}" ))), } } +#[allow(clippy::too_many_arguments)] +async fn handle_sandbox_error( + tool_name: &str, + turn_diff_tracker: &mut TurnDiffTracker, + params: ExecParams, + exec_command_context: ExecCommandContext, + error: SandboxErr, + sandbox_type: SandboxType, + sess: &Session, + turn_context: &TurnContext, + otel_event_manager: &OtelEventManager, +) -> Result { + let call_id = exec_command_context.call_id.clone(); + let sub_id = exec_command_context.sub_id.clone(); + let cwd = exec_command_context.cwd.clone(); + + if let SandboxErr::Timeout { output } = &error { + let content = format_exec_output(output); + return Err(FunctionCallError::RespondToModel(content)); + } + + // Early out if either the user never wants to be asked for approval, or + // we're letting the model manage escalation requests. Otherwise, continue + match turn_context.approval_policy { + AskForApproval::Never | AskForApproval::OnRequest => { + return Err(FunctionCallError::RespondToModel(format!( + "failed in sandbox {sandbox_type:?} with execution error: {error:?}" + ))); + } + AskForApproval::UnlessTrusted | AskForApproval::OnFailure => (), + } + + // Note that when `error` is `SandboxErr::Denied`, it could be a false + // positive. That is, it may have exited with a non-zero exit code, not + // because the sandbox denied it, but because that is its expected behavior, + // i.e., a grep command that did not match anything. Ideally we would + // include additional metadata on the command to indicate whether non-zero + // exit codes merit a retry. + + // For now, we categorically ask the user to retry without sandbox and + // emit the raw error as a background event. + sess.notify_background_event(&sub_id, format!("Execution failed: {error}")) + .await; + + let decision = sess + .request_command_approval( + sub_id.clone(), + call_id.clone(), + params.command.clone(), + cwd.clone(), + Some("command failed; retry without sandbox?".to_string()), + ) + .await; + + match decision { + ReviewDecision::Approved | ReviewDecision::ApprovedForSession => { + // Persist this command as pre‑approved for the + // remainder of the session so future + // executions skip the sandbox directly. + // TODO(ragona): Isn't this a bug? It always saves the command in an | fork? + sess.add_approved_command(params.command.clone()).await; + // Inform UI we are retrying without sandbox. + sess.notify_background_event(&sub_id, "retrying command without sandbox") + .await; + + otel_event_manager.tool_decision( + tool_name, + call_id.as_str(), + decision, + ToolDecisionSource::User, + ); + + // This is an escalated retry; the policy will not be + // examined and the sandbox has been set to `None`. + let retry_output_result = sess + .run_exec_with_events( + turn_diff_tracker, + exec_command_context.clone(), + ExecInvokeArgs { + params, + sandbox_type: SandboxType::None, + sandbox_policy: &turn_context.sandbox_policy, + sandbox_cwd: &turn_context.cwd, + codex_linux_sandbox_exe: &sess.services.codex_linux_sandbox_exe, + stdout_stream: if exec_command_context.apply_patch.is_some() { + None + } else { + Some(StdoutStream { + sub_id: sub_id.clone(), + call_id: call_id.clone(), + tx_event: sess.tx_event.clone(), + }) + }, + }, + ) + .await; + + match retry_output_result { + Ok(retry_output) => { + let ExecToolCallOutput { exit_code, .. } = &retry_output; + let content = format_exec_output(&retry_output); + if *exit_code == 0 { + Ok(content) + } else { + Err(FunctionCallError::RespondToModel(content)) + } + } + Err(e) => Err(FunctionCallError::RespondToModel(format!( + "retry failed: {e}" + ))), + } + } + decision @ (ReviewDecision::Denied | ReviewDecision::Abort) => { + otel_event_manager.tool_decision( + tool_name, + call_id.as_str(), + decision, + ToolDecisionSource::User, + ); + + // Fall through to original failure handling. + Err(FunctionCallError::RespondToModel( + "exec command rejected by user".to_string(), + )) + } + } +} + fn format_exec_output_str(exec_output: &ExecToolCallOutput) -> String { let ExecToolCallOutput { aggregated_output, .. @@ -3004,8 +3303,6 @@ pub(crate) async fn exit_review_mode( .await; } -use crate::executor::errors::ExecError; -use crate::executor::linkers::PreparedExec; #[cfg(test)] pub(crate) use tests::make_session_and_context; @@ -3319,13 +3616,9 @@ mod tests { unified_exec_manager: UnifiedExecSessionManager::default(), notifier: UserNotifier::default(), rollout: Mutex::new(None), + codex_linux_sandbox_exe: None, user_shell: shell::Shell::Unknown, show_raw_agent_reasoning: config.show_raw_agent_reasoning, - executor: Executor::new(ExecutorConfig::new( - turn_context.sandbox_policy.clone(), - turn_context.cwd.clone(), - None, - )), }; let session = Session { conversation_id, @@ -3392,13 +3685,9 @@ mod tests { unified_exec_manager: UnifiedExecSessionManager::default(), notifier: UserNotifier::default(), rollout: Mutex::new(None), + codex_linux_sandbox_exe: None, user_shell: shell::Shell::Unknown, show_raw_agent_reasoning: config.show_raw_agent_reasoning, - executor: Executor::new(ExecutorConfig::new( - config.sandbox_policy.clone(), - config.cwd.clone(), - None, - )), }; let session = Arc::new(Session { conversation_id, diff --git a/codex-rs/core/src/executor/backends.rs b/codex-rs/core/src/executor/backends.rs deleted file mode 100644 index 95cdb3cacb..0000000000 --- a/codex-rs/core/src/executor/backends.rs +++ /dev/null @@ -1,101 +0,0 @@ -use std::collections::HashMap; -use std::env; - -use async_trait::async_trait; - -use crate::CODEX_APPLY_PATCH_ARG1; -use crate::apply_patch::ApplyPatchExec; -use crate::exec::ExecParams; -use crate::function_tool::FunctionCallError; - -pub(crate) enum ExecutionMode { - Shell, - ApplyPatch(ApplyPatchExec), -} - -#[async_trait] -/// Backend-specific hooks that prepare and post-process execution requests for a -/// given [`ExecutionMode`]. -pub(crate) trait ExecutionBackend: Send + Sync { - fn prepare( - &self, - params: ExecParams, - // Required for downcasting the apply_patch. - mode: &ExecutionMode, - ) -> Result; - - fn stream_stdout(&self, _mode: &ExecutionMode) -> bool { - true - } -} - -static SHELL_BACKEND: ShellBackend = ShellBackend; -static APPLY_PATCH_BACKEND: ApplyPatchBackend = ApplyPatchBackend; - -pub(crate) fn backend_for_mode(mode: &ExecutionMode) -> &'static dyn ExecutionBackend { - match mode { - ExecutionMode::Shell => &SHELL_BACKEND, - ExecutionMode::ApplyPatch(_) => &APPLY_PATCH_BACKEND, - } -} - -struct ShellBackend; - -#[async_trait] -impl ExecutionBackend for ShellBackend { - fn prepare( - &self, - params: ExecParams, - mode: &ExecutionMode, - ) -> Result { - match mode { - ExecutionMode::Shell => Ok(params), - _ => Err(FunctionCallError::RespondToModel( - "shell backend invoked with non-shell mode".to_string(), - )), - } - } -} - -struct ApplyPatchBackend; - -#[async_trait] -impl ExecutionBackend for ApplyPatchBackend { - fn prepare( - &self, - params: ExecParams, - mode: &ExecutionMode, - ) -> Result { - match mode { - ExecutionMode::ApplyPatch(exec) => { - let path_to_codex = env::current_exe() - .ok() - .map(|p| p.to_string_lossy().to_string()) - .ok_or_else(|| { - FunctionCallError::RespondToModel( - "failed to determine path to codex executable".to_string(), - ) - })?; - - let patch = exec.action.patch.clone(); - Ok(ExecParams { - command: vec![path_to_codex, CODEX_APPLY_PATCH_ARG1.to_string(), patch], - cwd: exec.action.cwd.clone(), - timeout_ms: params.timeout_ms, - // Run apply_patch with a minimal environment for determinism and to - // avoid leaking host environment variables into the patch process. - env: HashMap::new(), - with_escalated_permissions: params.with_escalated_permissions, - justification: params.justification, - }) - } - ExecutionMode::Shell => Err(FunctionCallError::RespondToModel( - "apply_patch backend invoked without patch context".to_string(), - )), - } - } - - fn stream_stdout(&self, _mode: &ExecutionMode) -> bool { - false - } -} diff --git a/codex-rs/core/src/executor/cache.rs b/codex-rs/core/src/executor/cache.rs deleted file mode 100644 index 737ecb927c..0000000000 --- a/codex-rs/core/src/executor/cache.rs +++ /dev/null @@ -1,51 +0,0 @@ -use std::collections::HashSet; -use std::sync::Arc; -use std::sync::Mutex; - -#[derive(Clone, Debug, Default)] -/// Thread-safe store of user approvals so repeated commands can reuse -/// previously granted trust. -pub(crate) struct ApprovalCache { - inner: Arc>>>, -} - -impl ApprovalCache { - pub(crate) fn insert(&self, command: Vec) { - if command.is_empty() { - return; - } - if let Ok(mut guard) = self.inner.lock() { - guard.insert(command); - } - } - - pub(crate) fn snapshot(&self) -> HashSet> { - self.inner.lock().map(|g| g.clone()).unwrap_or_default() - } -} - -#[cfg(test)] -mod tests { - use super::*; - use pretty_assertions::assert_eq; - - #[test] - fn insert_ignores_empty_and_dedupes() { - let cache = ApprovalCache::default(); - - // Empty should be ignored - cache.insert(vec![]); - assert!(cache.snapshot().is_empty()); - - // Insert a command and verify snapshot contains it - let cmd = vec!["foo".to_string(), "bar".to_string()]; - cache.insert(cmd.clone()); - let snap1 = cache.snapshot(); - assert!(snap1.contains(&cmd)); - - // Reinserting should not create duplicates - cache.insert(cmd); - let snap2 = cache.snapshot(); - assert_eq!(snap1, snap2); - } -} diff --git a/codex-rs/core/src/executor/mod.rs b/codex-rs/core/src/executor/mod.rs deleted file mode 100644 index a5a305c604..0000000000 --- a/codex-rs/core/src/executor/mod.rs +++ /dev/null @@ -1,64 +0,0 @@ -mod backends; -mod cache; -mod runner; -mod sandbox; - -pub(crate) use backends::ExecutionMode; -pub(crate) use runner::ExecutionRequest; -pub(crate) use runner::Executor; -pub(crate) use runner::ExecutorConfig; -pub(crate) use runner::normalize_exec_result; - -pub(crate) mod linkers { - use crate::codex::ExecCommandContext; - use crate::exec::ExecParams; - use crate::exec::StdoutStream; - use crate::executor::backends::ExecutionMode; - use crate::executor::runner::ExecutionRequest; - - pub struct PreparedExec { - pub(crate) context: ExecCommandContext, - pub(crate) request: ExecutionRequest, - } - - impl PreparedExec { - pub fn new( - context: ExecCommandContext, - params: ExecParams, - approval_command: Vec, - mode: ExecutionMode, - stdout_stream: Option, - use_shell_profile: bool, - ) -> Self { - let request = ExecutionRequest { - params, - approval_command, - mode, - stdout_stream, - use_shell_profile, - }; - - Self { context, request } - } - } -} - -pub mod errors { - use crate::error::CodexErr; - use crate::function_tool::FunctionCallError; - use thiserror::Error; - - #[derive(Debug, Error)] - pub enum ExecError { - #[error(transparent)] - Function(#[from] FunctionCallError), - #[error(transparent)] - Codex(#[from] CodexErr), - } - - impl ExecError { - pub(crate) fn rejection(msg: impl Into) -> Self { - FunctionCallError::RespondToModel(msg.into()).into() - } - } -} diff --git a/codex-rs/core/src/executor/runner.rs b/codex-rs/core/src/executor/runner.rs deleted file mode 100644 index befa83360d..0000000000 --- a/codex-rs/core/src/executor/runner.rs +++ /dev/null @@ -1,387 +0,0 @@ -use std::path::PathBuf; -use std::sync::Arc; -use std::sync::RwLock; -use std::time::Duration; - -use super::backends::ExecutionMode; -use super::backends::backend_for_mode; -use super::cache::ApprovalCache; -use crate::codex::ExecCommandContext; -use crate::codex::Session; -use crate::error::CodexErr; -use crate::error::SandboxErr; -use crate::error::get_error_message_ui; -use crate::exec::ExecParams; -use crate::exec::ExecToolCallOutput; -use crate::exec::SandboxType; -use crate::exec::StdoutStream; -use crate::exec::StreamOutput; -use crate::exec::process_exec_tool_call; -use crate::executor::errors::ExecError; -use crate::executor::sandbox::select_sandbox; -use crate::function_tool::FunctionCallError; -use crate::protocol::AskForApproval; -use crate::protocol::ReviewDecision; -use crate::protocol::SandboxPolicy; -use crate::shell; -use codex_otel::otel_event_manager::ToolDecisionSource; - -#[derive(Clone, Debug)] -pub(crate) struct ExecutorConfig { - pub(crate) sandbox_policy: SandboxPolicy, - pub(crate) sandbox_cwd: PathBuf, - codex_linux_sandbox_exe: Option, -} - -impl ExecutorConfig { - pub(crate) fn new( - sandbox_policy: SandboxPolicy, - sandbox_cwd: PathBuf, - codex_linux_sandbox_exe: Option, - ) -> Self { - Self { - sandbox_policy, - sandbox_cwd, - codex_linux_sandbox_exe, - } - } -} - -/// Coordinates sandbox selection, backend-specific preparation, and command -/// execution for tool calls requested by the model. -pub(crate) struct Executor { - approval_cache: ApprovalCache, - config: Arc>, -} - -impl Executor { - pub(crate) fn new(config: ExecutorConfig) -> Self { - Self { - approval_cache: ApprovalCache::default(), - config: Arc::new(RwLock::new(config)), - } - } - - /// Updates the sandbox policy and working directory used for future - /// executions without recreating the executor. - pub(crate) fn update_environment(&self, sandbox_policy: SandboxPolicy, sandbox_cwd: PathBuf) { - if let Ok(mut cfg) = self.config.write() { - cfg.sandbox_policy = sandbox_policy; - cfg.sandbox_cwd = sandbox_cwd; - } - } - - /// Runs a prepared execution request end-to-end: prepares parameters, decides on - /// sandbox placement (prompting the user when necessary), launches the command, - /// and lets the backend post-process the final output. - pub(crate) async fn run( - &self, - mut request: ExecutionRequest, - session: &Session, - approval_policy: AskForApproval, - context: &ExecCommandContext, - ) -> Result { - if matches!(request.mode, ExecutionMode::Shell) { - request.params = - maybe_translate_shell_command(request.params, session, request.use_shell_profile); - } - - // Step 1: Normalise parameters via the selected backend. - let backend = backend_for_mode(&request.mode); - let stdout_stream = if backend.stream_stdout(&request.mode) { - request.stdout_stream.clone() - } else { - None - }; - request.params = backend - .prepare(request.params, &request.mode) - .map_err(ExecError::from)?; - - // Step 2: Snapshot sandbox configuration so it stays stable for this run. - let config = self - .config - .read() - .map_err(|_| ExecError::rejection("executor config poisoned"))? - .clone(); - - // Step 3: Decide sandbox placement, prompting for approval when needed. - let sandbox_decision = select_sandbox( - &request, - approval_policy, - self.approval_cache.snapshot(), - &config, - session, - &context.sub_id, - &context.call_id, - &context.otel_event_manager, - ) - .await?; - if sandbox_decision.record_session_approval { - self.approval_cache.insert(request.approval_command.clone()); - } - - // Step 4: Launch the command within the chosen sandbox. - let first_attempt = self - .spawn( - request.params.clone(), - sandbox_decision.initial_sandbox, - &config, - stdout_stream.clone(), - ) - .await; - - // Step 5: Handle sandbox outcomes, optionally escalating to an unsandboxed retry. - match first_attempt { - Ok(output) => Ok(output), - Err(CodexErr::Sandbox(SandboxErr::Timeout { output })) => { - Err(CodexErr::Sandbox(SandboxErr::Timeout { output }).into()) - } - Err(CodexErr::Sandbox(error)) => { - if sandbox_decision.escalate_on_failure { - self.retry_without_sandbox( - &request, - &config, - session, - context, - stdout_stream, - error, - ) - .await - } else { - Err(ExecError::rejection(format!( - "failed in sandbox {:?} with execution error: {error:?}", - sandbox_decision.initial_sandbox - ))) - } - } - Err(err) => Err(err.into()), - } - } - - /// Fallback path invoked when a sandboxed run is denied so the user can - /// approve rerunning without isolation. - async fn retry_without_sandbox( - &self, - request: &ExecutionRequest, - config: &ExecutorConfig, - session: &Session, - context: &ExecCommandContext, - stdout_stream: Option, - sandbox_error: SandboxErr, - ) -> Result { - session - .notify_background_event( - &context.sub_id, - format!("Execution failed: {sandbox_error}"), - ) - .await; - let decision = session - .request_command_approval( - context.sub_id.to_string(), - context.call_id.to_string(), - request.approval_command.clone(), - request.params.cwd.clone(), - Some("command failed; retry without sandbox?".to_string()), - ) - .await; - - context.otel_event_manager.tool_decision( - &context.tool_name, - &context.call_id, - decision, - ToolDecisionSource::User, - ); - match decision { - ReviewDecision::Approved | ReviewDecision::ApprovedForSession => { - if matches!(decision, ReviewDecision::ApprovedForSession) { - self.approval_cache.insert(request.approval_command.clone()); - } - session - .notify_background_event(&context.sub_id, "retrying command without sandbox") - .await; - - let retry_output = self - .spawn( - request.params.clone(), - SandboxType::None, - config, - stdout_stream, - ) - .await?; - - Ok(retry_output) - } - ReviewDecision::Denied | ReviewDecision::Abort => { - Err(ExecError::rejection("exec command rejected by user")) - } - } - } - - async fn spawn( - &self, - params: ExecParams, - sandbox: SandboxType, - config: &ExecutorConfig, - stdout_stream: Option, - ) -> Result { - process_exec_tool_call( - params, - sandbox, - &config.sandbox_policy, - &config.sandbox_cwd, - &config.codex_linux_sandbox_exe, - stdout_stream, - ) - .await - } -} - -fn maybe_translate_shell_command( - params: ExecParams, - session: &Session, - use_shell_profile: bool, -) -> ExecParams { - let should_translate = - matches!(session.user_shell(), shell::Shell::PowerShell(_)) || use_shell_profile; - - if should_translate - && let Some(command) = session - .user_shell() - .format_default_shell_invocation(params.command.clone()) - { - return ExecParams { command, ..params }; - } - - params -} - -pub(crate) struct ExecutionRequest { - pub params: ExecParams, - pub approval_command: Vec, - pub mode: ExecutionMode, - pub stdout_stream: Option, - pub use_shell_profile: bool, -} - -pub(crate) struct NormalizedExecOutput<'a> { - borrowed: Option<&'a ExecToolCallOutput>, - synthetic: Option, -} - -impl<'a> NormalizedExecOutput<'a> { - pub(crate) fn event_output(&'a self) -> &'a ExecToolCallOutput { - match (self.borrowed, self.synthetic.as_ref()) { - (Some(output), _) => output, - (None, Some(output)) => output, - (None, None) => unreachable!("normalized exec output missing data"), - } - } -} - -/// Converts a raw execution result into a uniform view that always exposes an -/// [`ExecToolCallOutput`], synthesizing error output when the command fails -/// before producing a response. -pub(crate) fn normalize_exec_result( - result: &Result, -) -> NormalizedExecOutput<'_> { - match result { - Ok(output) => NormalizedExecOutput { - borrowed: Some(output), - synthetic: None, - }, - Err(ExecError::Codex(CodexErr::Sandbox(SandboxErr::Timeout { output }))) => { - NormalizedExecOutput { - borrowed: Some(output.as_ref()), - synthetic: None, - } - } - Err(err) => { - let message = match err { - ExecError::Function(FunctionCallError::RespondToModel(msg)) => msg.clone(), - ExecError::Codex(e) => get_error_message_ui(e), - }; - let synthetic = ExecToolCallOutput { - exit_code: -1, - stdout: StreamOutput::new(String::new()), - stderr: StreamOutput::new(message.clone()), - aggregated_output: StreamOutput::new(message), - duration: Duration::default(), - timed_out: false, - }; - NormalizedExecOutput { - borrowed: None, - synthetic: Some(synthetic), - } - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::error::CodexErr; - use crate::error::EnvVarError; - use crate::error::SandboxErr; - use crate::exec::StreamOutput; - use pretty_assertions::assert_eq; - - fn make_output(text: &str) -> ExecToolCallOutput { - ExecToolCallOutput { - exit_code: 1, - stdout: StreamOutput::new(String::new()), - stderr: StreamOutput::new(String::new()), - aggregated_output: StreamOutput::new(text.to_string()), - duration: Duration::from_millis(123), - timed_out: false, - } - } - - #[test] - fn normalize_success_borrows() { - let out = make_output("ok"); - let result: Result = Ok(out); - let normalized = normalize_exec_result(&result); - assert_eq!(normalized.event_output().aggregated_output.text, "ok"); - } - - #[test] - fn normalize_timeout_borrows_embedded_output() { - let out = make_output("timed out payload"); - let err = CodexErr::Sandbox(SandboxErr::Timeout { - output: Box::new(out), - }); - let result: Result = Err(ExecError::Codex(err)); - let normalized = normalize_exec_result(&result); - assert_eq!( - normalized.event_output().aggregated_output.text, - "timed out payload" - ); - } - - #[test] - fn normalize_function_error_synthesizes_payload() { - let err = FunctionCallError::RespondToModel("boom".to_string()); - let result: Result = Err(ExecError::Function(err)); - let normalized = normalize_exec_result(&result); - assert_eq!(normalized.event_output().aggregated_output.text, "boom"); - } - - #[test] - fn normalize_codex_error_synthesizes_user_message() { - // Use a simple EnvVar error which formats to a clear message - let e = CodexErr::EnvVar(EnvVarError { - var: "FOO".to_string(), - instructions: Some("set it".to_string()), - }); - let result: Result = Err(ExecError::Codex(e)); - let normalized = normalize_exec_result(&result); - assert!( - normalized - .event_output() - .aggregated_output - .text - .contains("Missing environment variable: `FOO`"), - "expected synthesized user-friendly message" - ); - } -} diff --git a/codex-rs/core/src/executor/sandbox.rs b/codex-rs/core/src/executor/sandbox.rs deleted file mode 100644 index 5c01ff69b4..0000000000 --- a/codex-rs/core/src/executor/sandbox.rs +++ /dev/null @@ -1,405 +0,0 @@ -use crate::apply_patch::ApplyPatchExec; -use crate::codex::Session; -use crate::exec::SandboxType; -use crate::executor::ExecutionMode; -use crate::executor::ExecutionRequest; -use crate::executor::ExecutorConfig; -use crate::executor::errors::ExecError; -use crate::safety::SafetyCheck; -use crate::safety::assess_command_safety; -use crate::safety::assess_patch_safety; -use codex_otel::otel_event_manager::OtelEventManager; -use codex_otel::otel_event_manager::ToolDecisionSource; -use codex_protocol::protocol::AskForApproval; -use codex_protocol::protocol::ReviewDecision; -use std::collections::HashSet; - -/// Sandbox placement options selected for an execution run, including whether -/// to escalate after failures and whether approvals should persist. -pub(crate) struct SandboxDecision { - pub(crate) initial_sandbox: SandboxType, - pub(crate) escalate_on_failure: bool, - pub(crate) record_session_approval: bool, -} - -impl SandboxDecision { - fn auto(sandbox: SandboxType, escalate_on_failure: bool) -> Self { - Self { - initial_sandbox: sandbox, - escalate_on_failure, - record_session_approval: false, - } - } - - fn user_override(record_session_approval: bool) -> Self { - Self { - initial_sandbox: SandboxType::None, - escalate_on_failure: false, - record_session_approval, - } - } -} - -fn should_escalate_on_failure(approval: AskForApproval, sandbox: SandboxType) -> bool { - matches!( - (approval, sandbox), - ( - AskForApproval::UnlessTrusted | AskForApproval::OnFailure, - SandboxType::MacosSeatbelt | SandboxType::LinuxSeccomp - ) - ) -} - -/// Determines how a command should be sandboxed, prompting the user when -/// policy requires explicit approval. -#[allow(clippy::too_many_arguments)] -pub async fn select_sandbox( - request: &ExecutionRequest, - approval_policy: AskForApproval, - approval_cache: HashSet>, - config: &ExecutorConfig, - session: &Session, - sub_id: &str, - call_id: &str, - otel_event_manager: &OtelEventManager, -) -> Result { - match &request.mode { - ExecutionMode::Shell => { - select_shell_sandbox( - request, - approval_policy, - approval_cache, - config, - session, - sub_id, - call_id, - otel_event_manager, - ) - .await - } - ExecutionMode::ApplyPatch(exec) => { - select_apply_patch_sandbox(exec, approval_policy, config) - } - } -} - -#[allow(clippy::too_many_arguments)] -async fn select_shell_sandbox( - request: &ExecutionRequest, - approval_policy: AskForApproval, - approved_snapshot: HashSet>, - config: &ExecutorConfig, - session: &Session, - sub_id: &str, - call_id: &str, - otel_event_manager: &OtelEventManager, -) -> Result { - let command_for_safety = if request.approval_command.is_empty() { - request.params.command.clone() - } else { - request.approval_command.clone() - }; - - let safety = assess_command_safety( - &command_for_safety, - approval_policy, - &config.sandbox_policy, - &approved_snapshot, - request.params.with_escalated_permissions.unwrap_or(false), - ); - - match safety { - SafetyCheck::AutoApprove { - sandbox_type, - user_explicitly_approved, - } => { - let mut decision = SandboxDecision::auto( - sandbox_type, - should_escalate_on_failure(approval_policy, sandbox_type), - ); - if user_explicitly_approved { - decision.record_session_approval = true; - } - let (decision_for_event, source) = if user_explicitly_approved { - (ReviewDecision::ApprovedForSession, ToolDecisionSource::User) - } else { - (ReviewDecision::Approved, ToolDecisionSource::Config) - }; - otel_event_manager.tool_decision("local_shell", call_id, decision_for_event, source); - Ok(decision) - } - SafetyCheck::AskUser => { - let decision = session - .request_command_approval( - sub_id.to_string(), - call_id.to_string(), - request.approval_command.clone(), - request.params.cwd.clone(), - request.params.justification.clone(), - ) - .await; - - otel_event_manager.tool_decision( - "local_shell", - call_id, - decision, - ToolDecisionSource::User, - ); - match decision { - ReviewDecision::Approved => Ok(SandboxDecision::user_override(false)), - ReviewDecision::ApprovedForSession => Ok(SandboxDecision::user_override(true)), - ReviewDecision::Denied | ReviewDecision::Abort => { - Err(ExecError::rejection("exec command rejected by user")) - } - } - } - SafetyCheck::Reject { reason } => Err(ExecError::rejection(format!( - "exec command rejected: {reason}" - ))), - } -} - -fn select_apply_patch_sandbox( - exec: &ApplyPatchExec, - approval_policy: AskForApproval, - config: &ExecutorConfig, -) -> Result { - if exec.user_explicitly_approved_this_action { - return Ok(SandboxDecision::user_override(false)); - } - - match assess_patch_safety( - &exec.action, - approval_policy, - &config.sandbox_policy, - &config.sandbox_cwd, - ) { - SafetyCheck::AutoApprove { sandbox_type, .. } => Ok(SandboxDecision::auto( - sandbox_type, - should_escalate_on_failure(approval_policy, sandbox_type), - )), - SafetyCheck::AskUser => Err(ExecError::rejection( - "patch requires approval but none was recorded", - )), - SafetyCheck::Reject { reason } => { - Err(ExecError::rejection(format!("patch rejected: {reason}"))) - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::codex::make_session_and_context; - use crate::exec::ExecParams; - use crate::function_tool::FunctionCallError; - use crate::protocol::SandboxPolicy; - use codex_apply_patch::ApplyPatchAction; - use pretty_assertions::assert_eq; - - #[tokio::test] - async fn select_apply_patch_user_override_when_explicit() { - let (session, ctx) = make_session_and_context(); - let tmp = tempfile::tempdir().expect("tmp"); - let p = tmp.path().join("a.txt"); - let action = ApplyPatchAction::new_add_for_test(&p, "hello".to_string()); - let exec = ApplyPatchExec { - action, - user_explicitly_approved_this_action: true, - }; - let cfg = ExecutorConfig::new(SandboxPolicy::ReadOnly, std::env::temp_dir(), None); - let request = ExecutionRequest { - params: ExecParams { - command: vec!["apply_patch".into()], - cwd: std::env::temp_dir(), - timeout_ms: None, - env: std::collections::HashMap::new(), - with_escalated_permissions: None, - justification: None, - }, - approval_command: vec!["apply_patch".into()], - mode: ExecutionMode::ApplyPatch(exec), - stdout_stream: None, - use_shell_profile: false, - }; - let otel_event_manager = ctx.client.get_otel_event_manager(); - let decision = select_sandbox( - &request, - AskForApproval::OnRequest, - Default::default(), - &cfg, - &session, - "sub", - "call", - &otel_event_manager, - ) - .await - .expect("ok"); - // Explicit user override runs without sandbox - assert_eq!(decision.initial_sandbox, SandboxType::None); - assert_eq!(decision.escalate_on_failure, false); - } - - #[tokio::test] - async fn select_apply_patch_autoapprove_in_danger() { - let (session, ctx) = make_session_and_context(); - let tmp = tempfile::tempdir().expect("tmp"); - let p = tmp.path().join("a.txt"); - let action = ApplyPatchAction::new_add_for_test(&p, "hello".to_string()); - let exec = ApplyPatchExec { - action, - user_explicitly_approved_this_action: false, - }; - let cfg = ExecutorConfig::new(SandboxPolicy::DangerFullAccess, std::env::temp_dir(), None); - let request = ExecutionRequest { - params: ExecParams { - command: vec!["apply_patch".into()], - cwd: std::env::temp_dir(), - timeout_ms: None, - env: std::collections::HashMap::new(), - with_escalated_permissions: None, - justification: None, - }, - approval_command: vec!["apply_patch".into()], - mode: ExecutionMode::ApplyPatch(exec), - stdout_stream: None, - use_shell_profile: false, - }; - let otel_event_manager = ctx.client.get_otel_event_manager(); - let decision = select_sandbox( - &request, - AskForApproval::OnRequest, - Default::default(), - &cfg, - &session, - "sub", - "call", - &otel_event_manager, - ) - .await - .expect("ok"); - // On platforms with a sandbox, DangerFullAccess still prefers it - let expected = crate::safety::get_platform_sandbox().unwrap_or(SandboxType::None); - assert_eq!(decision.initial_sandbox, expected); - assert_eq!(decision.escalate_on_failure, false); - } - - #[tokio::test] - async fn select_apply_patch_requires_approval_on_unless_trusted() { - let (session, ctx) = make_session_and_context(); - let tempdir = tempfile::tempdir().expect("tmpdir"); - let p = tempdir.path().join("a.txt"); - let action = ApplyPatchAction::new_add_for_test(&p, "hello".to_string()); - let exec = ApplyPatchExec { - action, - user_explicitly_approved_this_action: false, - }; - let cfg = ExecutorConfig::new(SandboxPolicy::ReadOnly, std::env::temp_dir(), None); - let request = ExecutionRequest { - params: ExecParams { - command: vec!["apply_patch".into()], - cwd: std::env::temp_dir(), - timeout_ms: None, - env: std::collections::HashMap::new(), - with_escalated_permissions: None, - justification: None, - }, - approval_command: vec!["apply_patch".into()], - mode: ExecutionMode::ApplyPatch(exec), - stdout_stream: None, - use_shell_profile: false, - }; - let otel_event_manager = ctx.client.get_otel_event_manager(); - let result = select_sandbox( - &request, - AskForApproval::UnlessTrusted, - Default::default(), - &cfg, - &session, - "sub", - "call", - &otel_event_manager, - ) - .await; - match result { - Ok(_) => panic!("expected error"), - Err(ExecError::Function(FunctionCallError::RespondToModel(msg))) => { - assert!(msg.contains("requires approval")) - } - Err(other) => panic!("unexpected error: {other:?}"), - } - } - - #[tokio::test] - async fn select_shell_autoapprove_in_danger_mode() { - let (session, ctx) = make_session_and_context(); - let cfg = ExecutorConfig::new(SandboxPolicy::DangerFullAccess, std::env::temp_dir(), None); - let request = ExecutionRequest { - params: ExecParams { - command: vec!["some-unknown".into()], - cwd: std::env::temp_dir(), - timeout_ms: None, - env: std::collections::HashMap::new(), - with_escalated_permissions: None, - justification: None, - }, - approval_command: vec!["some-unknown".into()], - mode: ExecutionMode::Shell, - stdout_stream: None, - use_shell_profile: false, - }; - let otel_event_manager = ctx.client.get_otel_event_manager(); - let decision = select_sandbox( - &request, - AskForApproval::OnRequest, - Default::default(), - &cfg, - &session, - "sub", - "call", - &otel_event_manager, - ) - .await - .expect("ok"); - assert_eq!(decision.initial_sandbox, SandboxType::None); - assert_eq!(decision.escalate_on_failure, false); - } - - #[cfg(any(target_os = "macos", target_os = "linux"))] - #[tokio::test] - async fn select_shell_escalates_on_failure_with_platform_sandbox() { - let (session, ctx) = make_session_and_context(); - let cfg = ExecutorConfig::new(SandboxPolicy::ReadOnly, std::env::temp_dir(), None); - let request = ExecutionRequest { - params: ExecParams { - // Unknown command => untrusted but not flagged dangerous - command: vec!["some-unknown".into()], - cwd: std::env::temp_dir(), - timeout_ms: None, - env: std::collections::HashMap::new(), - with_escalated_permissions: None, - justification: None, - }, - approval_command: vec!["some-unknown".into()], - mode: ExecutionMode::Shell, - stdout_stream: None, - use_shell_profile: false, - }; - let otel_event_manager = ctx.client.get_otel_event_manager(); - let decision = select_sandbox( - &request, - AskForApproval::OnFailure, - Default::default(), - &cfg, - &session, - "sub", - "call", - &otel_event_manager, - ) - .await - .expect("ok"); - // On macOS/Linux we should have a platform sandbox and escalate on failure - assert_ne!(decision.initial_sandbox, SandboxType::None); - assert_eq!(decision.escalate_on_failure, true); - } -} diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index 94350a447e..c3f61642b0 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -27,7 +27,6 @@ pub mod error; pub mod exec; mod exec_command; pub mod exec_env; -pub mod executor; mod flags; pub mod git_info; pub mod landlock; diff --git a/codex-rs/core/src/safety.rs b/codex-rs/core/src/safety.rs index 0ed0f929ff..b976ae4a4c 100644 --- a/codex-rs/core/src/safety.rs +++ b/codex-rs/core/src/safety.rs @@ -125,10 +125,9 @@ pub fn assess_command_safety( // the session _because_ they know it needs to run outside a sandbox. if is_known_safe_command(command) || approved.contains(command) { - let user_explicitly_approved = approved.contains(command); return SafetyCheck::AutoApprove { sandbox_type: SandboxType::None, - user_explicitly_approved, + user_explicitly_approved: false, }; } @@ -381,7 +380,7 @@ mod tests { safety_check, SafetyCheck::AutoApprove { sandbox_type: SandboxType::None, - user_explicitly_approved: true, + user_explicitly_approved: false, } ); } diff --git a/codex-rs/core/src/state/service.rs b/codex-rs/core/src/state/service.rs index 994352eddf..a67b9dda93 100644 --- a/codex-rs/core/src/state/service.rs +++ b/codex-rs/core/src/state/service.rs @@ -1,9 +1,9 @@ use crate::RolloutRecorder; use crate::exec_command::ExecSessionManager; -use crate::executor::Executor; use crate::mcp_connection_manager::McpConnectionManager; use crate::unified_exec::UnifiedExecSessionManager; use crate::user_notification::UserNotifier; +use std::path::PathBuf; use tokio::sync::Mutex; pub(crate) struct SessionServices { @@ -12,7 +12,7 @@ pub(crate) struct SessionServices { pub(crate) unified_exec_manager: UnifiedExecSessionManager, pub(crate) notifier: UserNotifier, pub(crate) rollout: Mutex>, + pub(crate) codex_linux_sandbox_exe: Option, pub(crate) user_shell: crate::shell::Shell, pub(crate) show_raw_agent_reasoning: bool, - pub(crate) executor: Executor, } diff --git a/codex-rs/core/src/state/session.rs b/codex-rs/core/src/state/session.rs index f170a10c18..ee0c5fc976 100644 --- a/codex-rs/core/src/state/session.rs +++ b/codex-rs/core/src/state/session.rs @@ -1,5 +1,7 @@ //! Session-wide mutable state. +use std::collections::HashSet; + use codex_protocol::models::ResponseItem; use crate::conversation_history::ConversationHistory; @@ -10,6 +12,7 @@ use crate::protocol::TokenUsageInfo; /// Persistent, session-scoped state previously stored directly on `Session`. #[derive(Default)] pub(crate) struct SessionState { + pub(crate) approved_commands: HashSet>, pub(crate) history: ConversationHistory, pub(crate) token_info: Option, pub(crate) latest_rate_limits: Option, @@ -41,6 +44,15 @@ impl SessionState { self.history.replace(items); } + // Approved command helpers + pub(crate) fn add_approved_command(&mut self, cmd: Vec) { + self.approved_commands.insert(cmd); + } + + pub(crate) fn approved_commands_ref(&self) -> &HashSet> { + &self.approved_commands + } + // Token/rate limit helpers pub(crate) fn update_token_info_from_usage( &mut self, diff --git a/codex-rs/core/tests/suite/seatbelt.rs b/codex-rs/core/tests/suite/seatbelt.rs index a879d3e952..78f599d42e 100644 --- a/codex-rs/core/tests/suite/seatbelt.rs +++ b/codex-rs/core/tests/suite/seatbelt.rs @@ -169,12 +169,6 @@ async fn python_getpwuid_works_under_seatbelt() { return; } - // For local dev. - if which::which("python3").is_err() { - eprintln!("python3 not found in PATH, skipping test."); - return; - } - // ReadOnly is sufficient here since we are only exercising user lookup. let policy = SandboxPolicy::ReadOnly; let command_cwd = std::env::current_dir().expect("getcwd"); From f895d4cbb314bf476de35f5547fe032ed7fd0457 Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 2 Oct 2025 14:17:42 -0700 Subject: [PATCH 12/16] Minor cleanup of codex exec output (#4585) image image --- codex-rs/Cargo.lock | 1 - codex-rs/exec/Cargo.toml | 5 +- codex-rs/exec/src/event_processor.rs | 2 + .../src/event_processor_with_human_output.rs | 202 ++++++------------ codex-rs/exec/src/lib.rs | 1 + 5 files changed, 65 insertions(+), 146 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 53451bb2b1..dfa8b8650c 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -913,7 +913,6 @@ version = "0.0.0" dependencies = [ "anyhow", "assert_cmd", - "chrono", "clap", "codex-arg0", "codex-common", diff --git a/codex-rs/exec/Cargo.toml b/codex-rs/exec/Cargo.toml index 27ae067c92..a5a547508a 100644 --- a/codex-rs/exec/Cargo.toml +++ b/codex-rs/exec/Cargo.toml @@ -16,7 +16,6 @@ workspace = true [dependencies] anyhow = { workspace = true } -chrono = { workspace = true } clap = { workspace = true, features = ["derive"] } codex-arg0 = { workspace = true } codex-common = { workspace = true, features = [ @@ -27,6 +26,7 @@ codex-common = { workspace = true, features = [ codex-core = { workspace = true } codex-ollama = { workspace = true } codex-protocol = { workspace = true } +opentelemetry-appender-tracing = { workspace = true } owo-colors = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } @@ -40,7 +40,6 @@ tokio = { workspace = true, features = [ ] } tracing = { workspace = true, features = ["log"] } tracing-subscriber = { workspace = true, features = ["env-filter"] } -opentelemetry-appender-tracing = { workspace = true } ts-rs = { workspace = true, features = [ "uuid-impl", "serde-json-impl", @@ -52,10 +51,10 @@ ts-rs = { workspace = true, features = [ assert_cmd = { workspace = true } core_test_support = { workspace = true } libc = { workspace = true } +mcp-types = { workspace = true } predicates = { workspace = true } pretty_assertions = { workspace = true } tempfile = { workspace = true } uuid = { workspace = true } walkdir = { workspace = true } wiremock = { workspace = true } -mcp-types = { workspace = true } diff --git a/codex-rs/exec/src/event_processor.rs b/codex-rs/exec/src/event_processor.rs index 9211429028..2a0b1eb833 100644 --- a/codex-rs/exec/src/event_processor.rs +++ b/codex-rs/exec/src/event_processor.rs @@ -21,6 +21,8 @@ pub(crate) trait EventProcessor { /// Handle a single event emitted by the agent. fn process_event(&mut self, event: Event) -> CodexStatus; + + fn print_final_output(&mut self) {} } pub(crate) fn handle_last_message(last_agent_message: Option<&str>, output_file: &Path) { diff --git a/codex-rs/exec/src/event_processor_with_human_output.rs b/codex-rs/exec/src/event_processor_with_human_output.rs index 72c31c2979..a78139eeed 100644 --- a/codex-rs/exec/src/event_processor_with_human_output.rs +++ b/codex-rs/exec/src/event_processor_with_human_output.rs @@ -2,10 +2,7 @@ use codex_common::elapsed::format_duration; use codex_common::elapsed::format_elapsed; use codex_core::config::Config; use codex_core::plan_tool::UpdatePlanArgs; -use codex_core::protocol::AgentMessageDeltaEvent; use codex_core::protocol::AgentMessageEvent; -use codex_core::protocol::AgentReasoningDeltaEvent; -use codex_core::protocol::AgentReasoningRawContentDeltaEvent; use codex_core::protocol::AgentReasoningRawContentEvent; use codex_core::protocol::BackgroundEventEvent; use codex_core::protocol::ErrorEvent; @@ -31,7 +28,6 @@ use owo_colors::OwoColorize; use owo_colors::Style; use shlex::try_join; use std::collections::HashMap; -use std::io::Write; use std::path::PathBuf; use std::time::Instant; @@ -44,7 +40,6 @@ use codex_common::create_config_summary_entries; /// a limit so they can see the full transcript. const MAX_OUTPUT_LINES_FOR_EXEC_TOOL_CALL: usize = 20; pub(crate) struct EventProcessorWithHumanOutput { - call_id_to_command: HashMap, call_id_to_patch: HashMap, // To ensure that --color=never is respected, ANSI escapes _must_ be added @@ -62,10 +57,8 @@ pub(crate) struct EventProcessorWithHumanOutput { /// Whether to include `AgentReasoning` events in the output. show_agent_reasoning: bool, show_raw_agent_reasoning: bool, - answer_started: bool, - reasoning_started: bool, - raw_reasoning_started: bool, last_message_path: Option, + last_total_token_usage: Option, } impl EventProcessorWithHumanOutput { @@ -74,12 +67,10 @@ impl EventProcessorWithHumanOutput { config: &Config, last_message_path: Option, ) -> Self { - let call_id_to_command = HashMap::new(); let call_id_to_patch = HashMap::new(); if with_ansi { Self { - call_id_to_command, call_id_to_patch, bold: Style::new().bold(), italic: Style::new().italic(), @@ -90,14 +81,11 @@ impl EventProcessorWithHumanOutput { cyan: Style::new().cyan(), show_agent_reasoning: !config.hide_agent_reasoning, show_raw_agent_reasoning: config.show_raw_agent_reasoning, - answer_started: false, - reasoning_started: false, - raw_reasoning_started: false, last_message_path, + last_total_token_usage: None, } } else { Self { - call_id_to_command, call_id_to_patch, bold: Style::new(), italic: Style::new(), @@ -108,19 +96,13 @@ impl EventProcessorWithHumanOutput { cyan: Style::new(), show_agent_reasoning: !config.hide_agent_reasoning, show_raw_agent_reasoning: config.show_raw_agent_reasoning, - answer_started: false, - reasoning_started: false, - raw_reasoning_started: false, last_message_path, + last_total_token_usage: None, } } } } -struct ExecCommandBegin { - command: Vec, -} - struct PatchApplyBegin { start_time: Instant, auto_approved: bool, @@ -130,9 +112,6 @@ struct PatchApplyBegin { #[macro_export] macro_rules! ts_println { ($self:ident, $($arg:tt)*) => {{ - let now = chrono::Utc::now(); - let formatted = now.format("[%Y-%m-%dT%H:%M:%S]"); - print!("{} ", formatted.style($self.dimmed)); println!($($arg)*); }}; } @@ -141,7 +120,12 @@ impl EventProcessor for EventProcessorWithHumanOutput { /// Print a concise summary of the effective configuration that will be used /// for the session. This mirrors the information shown in the TUI welcome /// screen. - fn print_config_summary(&mut self, config: &Config, prompt: &str, _: &SessionConfiguredEvent) { + fn print_config_summary( + &mut self, + config: &Config, + prompt: &str, + session_configured_event: &SessionConfiguredEvent, + ) { const VERSION: &str = env!("CARGO_PKG_VERSION"); ts_println!( self, @@ -149,7 +133,11 @@ impl EventProcessor for EventProcessorWithHumanOutput { VERSION ); - let entries = create_config_summary_entries(config); + let mut entries = create_config_summary_entries(config); + entries.push(( + "session id", + session_configured_event.session_id.to_string(), + )); for (key, value) in entries { println!("{} {}", format!("{key}:").style(self.bold), value); @@ -160,12 +148,7 @@ impl EventProcessor for EventProcessorWithHumanOutput { // Echo the prompt that will be sent to the agent so it is visible in the // transcript/logs before any events come in. Note the prompt may have been // read from stdin, so it may not be visible in the terminal otherwise. - ts_println!( - self, - "{}\n{}", - "User instructions:".style(self.bold).style(self.cyan), - prompt - ); + ts_println!(self, "{}\n{}", "user".style(self.cyan), prompt); } fn process_event(&mut self, event: Event) -> CodexStatus { @@ -191,126 +174,49 @@ impl EventProcessor for EventProcessorWithHumanOutput { return CodexStatus::InitiateShutdown; } EventMsg::TokenCount(ev) => { - if let Some(usage_info) = ev.info { - ts_println!( - self, - "tokens used: {}", - format_with_separators(usage_info.total_token_usage.blended_total()) - ); - } - } - EventMsg::AgentMessageDelta(AgentMessageDeltaEvent { delta }) => { - if !self.answer_started { - ts_println!(self, "{}\n", "codex".style(self.italic).style(self.magenta)); - self.answer_started = true; - } - print!("{delta}"); - #[expect(clippy::expect_used)] - std::io::stdout().flush().expect("could not flush stdout"); - } - EventMsg::AgentReasoningDelta(AgentReasoningDeltaEvent { delta }) => { - if !self.show_agent_reasoning { - return CodexStatus::Running; - } - if !self.reasoning_started { - ts_println!( - self, - "{}\n", - "thinking".style(self.italic).style(self.magenta), - ); - self.reasoning_started = true; - } - print!("{delta}"); - #[expect(clippy::expect_used)] - std::io::stdout().flush().expect("could not flush stdout"); + self.last_total_token_usage = ev.info; } + EventMsg::AgentReasoningSectionBreak(_) => { if !self.show_agent_reasoning { return CodexStatus::Running; } println!(); - #[expect(clippy::expect_used)] - std::io::stdout().flush().expect("could not flush stdout"); } EventMsg::AgentReasoningRawContent(AgentReasoningRawContentEvent { text }) => { - if !self.show_raw_agent_reasoning { - return CodexStatus::Running; - } - if !self.raw_reasoning_started { - print!("{text}"); - #[expect(clippy::expect_used)] - std::io::stdout().flush().expect("could not flush stdout"); - } else { - println!(); - self.raw_reasoning_started = false; - } - } - EventMsg::AgentReasoningRawContentDelta(AgentReasoningRawContentDeltaEvent { - delta, - }) => { - if !self.show_raw_agent_reasoning { - return CodexStatus::Running; - } - if !self.raw_reasoning_started { - self.raw_reasoning_started = true; - } - print!("{delta}"); - #[expect(clippy::expect_used)] - std::io::stdout().flush().expect("could not flush stdout"); - } - EventMsg::AgentMessage(AgentMessageEvent { message }) => { - // if answer_started is false, this means we haven't received any - // delta. Thus, we need to print the message as a new answer. - if !self.answer_started { + if self.show_raw_agent_reasoning { ts_println!( self, "{}\n{}", - "codex".style(self.italic).style(self.magenta), - message, + "thinking".style(self.italic).style(self.magenta), + text, ); - } else { - println!(); - self.answer_started = false; } } - EventMsg::ExecCommandBegin(ExecCommandBeginEvent { - call_id, - command, - cwd, - parsed_cmd: _, - }) => { - self.call_id_to_command.insert( - call_id, - ExecCommandBegin { - command: command.clone(), - }, - ); + EventMsg::AgentMessage(AgentMessageEvent { message }) => { ts_println!( self, - "{} {} in {}", - "exec".style(self.magenta), + "{}\n{}", + "codex".style(self.italic).style(self.magenta), + message, + ); + } + EventMsg::ExecCommandBegin(ExecCommandBeginEvent { command, cwd, .. }) => { + print!( + "{}\n{} in {}", + "exec".style(self.italic).style(self.magenta), escape_command(&command).style(self.bold), cwd.to_string_lossy(), ); } EventMsg::ExecCommandOutputDelta(_) => {} EventMsg::ExecCommandEnd(ExecCommandEndEvent { - call_id, aggregated_output, duration, exit_code, .. }) => { - let exec_command = self.call_id_to_command.remove(&call_id); - let (duration, call) = if let Some(ExecCommandBegin { command, .. }) = exec_command - { - ( - format!(" in {}", format_duration(duration)), - format!("{}", escape_command(&command).style(self.bold)), - ) - } else { - ("".to_string(), format!("exec('{call_id}')")) - }; + let duration = format!(" in {}", format_duration(duration)); let truncated_output = aggregated_output .lines() @@ -319,11 +225,11 @@ impl EventProcessor for EventProcessorWithHumanOutput { .join("\n"); match exit_code { 0 => { - let title = format!("{call} succeeded{duration}:"); + let title = format!(" succeeded{duration}:"); ts_println!(self, "{}", title.style(self.green)); } _ => { - let title = format!("{call} exited {exit_code}{duration}:"); + let title = format!(" exited {exit_code}{duration}:"); ts_println!(self, "{}", title.style(self.red)); } } @@ -391,9 +297,8 @@ impl EventProcessor for EventProcessorWithHumanOutput { ts_println!( self, - "{} auto_approved={}:", - "apply_patch".style(self.magenta), - auto_approved, + "{}", + "file update".style(self.magenta).style(self.italic), ); // Pretty-print the patch summary with colored diff markers so @@ -492,7 +397,11 @@ impl EventProcessor for EventProcessorWithHumanOutput { } } EventMsg::TurnDiff(TurnDiffEvent { unified_diff }) => { - ts_println!(self, "{}", "turn diff:".style(self.magenta)); + ts_println!( + self, + "{}", + "file update:".style(self.magenta).style(self.italic) + ); println!("{unified_diff}"); } EventMsg::ExecApprovalRequest(_) => { @@ -503,17 +412,12 @@ impl EventProcessor for EventProcessorWithHumanOutput { } EventMsg::AgentReasoning(agent_reasoning_event) => { if self.show_agent_reasoning { - if !self.reasoning_started { - ts_println!( - self, - "{}\n{}", - "codex".style(self.italic).style(self.magenta), - agent_reasoning_event.text, - ); - } else { - println!(); - self.reasoning_started = false; - } + ts_println!( + self, + "{}\n{}", + "thinking".style(self.italic).style(self.magenta), + agent_reasoning_event.text, + ); } } EventMsg::SessionConfigured(session_configured_event) => { @@ -604,9 +508,23 @@ impl EventProcessor for EventProcessorWithHumanOutput { EventMsg::UserMessage(_) => {} EventMsg::EnteredReviewMode(_) => {} EventMsg::ExitedReviewMode(_) => {} + EventMsg::AgentMessageDelta(_) => {} + EventMsg::AgentReasoningDelta(_) => {} + EventMsg::AgentReasoningRawContentDelta(_) => {} } CodexStatus::Running } + + fn print_final_output(&mut self) { + if let Some(usage_info) = &self.last_total_token_usage { + ts_println!( + self, + "{}\n{}", + "tokens used".style(self.magenta).style(self.italic), + format_with_separators(usage_info.total_token_usage.blended_total()) + ); + } + } } fn escape_command(command: &[String]) -> String { diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index 98f3d22fdc..6888baff48 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -364,6 +364,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any } } } + event_processor.print_final_output(); if error_seen { std::process::exit(1); } From 62cc8a4b8d17741143b859af8cd159745646c2dc Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 2 Oct 2025 14:36:33 -0700 Subject: [PATCH 13/16] Add issue deduplicator workflow (#4628) It's a bit hand-holdy in that it pre-downloads issue list but that keeps codex running in read-only no-network mode. --- .github/prompts/issue-deduplicator.txt | 19 +++++ .github/workflows/issue-deduplicator.yml | 88 ++++++++++++++++++++++++ .github/workflows/issue-labeler.yml | 1 + 3 files changed, 108 insertions(+) create mode 100644 .github/prompts/issue-deduplicator.txt create mode 100644 .github/workflows/issue-deduplicator.yml diff --git a/.github/prompts/issue-deduplicator.txt b/.github/prompts/issue-deduplicator.txt new file mode 100644 index 0000000000..854e3c4487 --- /dev/null +++ b/.github/prompts/issue-deduplicator.txt @@ -0,0 +1,19 @@ +You are an assistant that triages new GitHub issues by identifying potential duplicates. + +You will receive the following JSON files located in the current working directory: +- `codex-current-issue.json`: JSON object describing the newly created issue (fields: number, title, body). +- `codex-existing-issues.json`: JSON array of recent issues (each element includes number, title, body, createdAt). + +Instructions: +- Load both files as JSON and review their contents carefully. +- Compare the current issue against the existing issues to find up to five that appear to describe the same underlying problem or request. +- Only consider an issue a potential duplicate if there is a clear overlap in symptoms, feature requests, reproduction steps, or error messages. +- Prioritize newer issues when similarity is comparable. +- Ignore pull requests and issues whose similarity is tenuous. +- When unsure, prefer returning fewer matches. + +Output requirements: +- Respond with a JSON array of issue numbers (integers), ordered from most likely duplicate to least. +- Include at most five numbers. +- If you find no plausible duplicates, respond with `[]`. +- Do not emit any additional commentary, text, or keys beyond the JSON array. diff --git a/.github/workflows/issue-deduplicator.yml b/.github/workflows/issue-deduplicator.yml new file mode 100644 index 0000000000..05453450e4 --- /dev/null +++ b/.github/workflows/issue-deduplicator.yml @@ -0,0 +1,88 @@ +name: Issue Deduplicator + +on: + issues: + types: +# - opened - disabled while testing + - labeled + +jobs: + gather-duplicates: + name: Identify potential duplicates + if: ${{ github.event.action == 'opened' || (github.event.action == 'labeled' && github.event.label.name == 'codex-deduplicate') }} + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + codex_output: ${{ steps.codex.outputs.final_message }} + steps: + - uses: actions/checkout@v4 + + - name: Prepare Codex inputs + env: + GH_TOKEN: ${{ github.token }} + run: | + set -eo pipefail + + CURRENT_ISSUE_FILE=codex-current-issue.json + EXISTING_ISSUES_FILE=codex-existing-issues.json + + gh issue list --repo "${{ github.repository }}" \ + --json number,title,body,createdAt \ + --limit 1000 \ + --state all \ + --search "sort:created-desc" \ + | jq '.' \ + > "$EXISTING_ISSUES_FILE" + + printf '%s' '${{ toJson(github.event.issue) }}' \ + | jq '{number, title, body}' \ + > "$CURRENT_ISSUE_FILE" + + - id: codex + uses: openai/codex-action@main + with: + openai_api_key: ${{ secrets.CODEX_OPENAI_API_KEY }} + prompt_file: .github/prompts/issue-deduplicator.txt + require_repo_write: false + + comment-on-issue: + name: Comment with potential duplicates + needs: gather-duplicates + if: ${{ needs.gather-duplicates.result != 'skipped' }} + runs-on: ubuntu-latest + permissions: + contents: read + issues: write + steps: + - name: Comment on issue + uses: actions/github-script@v7 + env: + CODEX_OUTPUT: ${{ needs.gather-duplicates.outputs.codex_output }} + with: + github-token: ${{ github.token }} + script: | + let numbers; + try { + numbers = JSON.parse(process.env.CODEX_OUTPUT); + } catch (error) { + core.info(`Codex output was not valid JSON. Raw output: ${raw}`); + return; + } + + const lines = ['Potential duplicates detected:', ...numbers.map((value) => `- #${value}`)]; + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.issue.number, + body: lines.join("\n"), + }); + + - name: Remove codex-deduplicate label + if: ${{ always() && github.event.action == 'labeled' && github.event.label.name == 'codex-deduplicate' }} + env: + GH_TOKEN: ${{ github.token }} + run: | + gh issue edit "${{ github.event.issue.number }}" --remove-label codex-deduplicate || true + echo "Attempted to remove label: codex-deduplicate" diff --git a/.github/workflows/issue-labeler.yml b/.github/workflows/issue-labeler.yml index dfbd17c27f..24b9276db0 100644 --- a/.github/workflows/issue-labeler.yml +++ b/.github/workflows/issue-labeler.yml @@ -28,6 +28,7 @@ jobs: with: openai_api_key: ${{ secrets.CODEX_OPENAI_API_KEY }} prompt_file: .github/prompts/issue-labeler.txt + require_repo_write: false apply-labels: name: Apply labels from Codex output From 25a2e15ec5cc58becdec269651386663fc7a4694 Mon Sep 17 00:00:00 2001 From: Jeremy Rose <172423086+nornagon-openai@users.noreply.github.com> Date: Thu, 2 Oct 2025 14:41:29 -0700 Subject: [PATCH 14/16] tui: tweaks to dialog display (#4622) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - prefix command approval reasons with "Reason:" - show keyboard shortcuts for some ListSelectionItems - remove "description" lines for approval options, and make the labels more verbose - add a spacer line in diff display after the path and some other minor refactors that go along with the above. Screenshot 2025-10-02 at 1 24 50 PM --- .../tui/src/bottom_pane/approval_overlay.rs | 86 ++++++++++--------- codex-rs/tui/src/bottom_pane/command_popup.rs | 1 + .../tui/src/bottom_pane/file_search_popup.rs | 1 + .../src/bottom_pane/list_selection_view.rs | 38 ++++---- .../src/bottom_pane/selection_popup_common.rs | 9 ++ codex-rs/tui/src/chatwidget.rs | 30 +++---- ...hatwidget__tests__approval_modal_exec.snap | 12 +-- ..._tests__approval_modal_exec_no_reason.snap | 24 +++--- ...atwidget__tests__approval_modal_patch.snap | 30 +++---- ...dget__tests__exec_approval_modal_exec.snap | 33 ++++--- ...sts__status_widget_and_approval_modal.snap | 12 +-- codex-rs/tui/src/chatwidget/tests.rs | 32 +++++-- codex-rs/tui/src/diff_render.rs | 1 + 13 files changed, 159 insertions(+), 150 deletions(-) diff --git a/codex-rs/tui/src/bottom_pane/approval_overlay.rs b/codex-rs/tui/src/bottom_pane/approval_overlay.rs index d85e5db57f..7f52b11bfa 100644 --- a/codex-rs/tui/src/bottom_pane/approval_overlay.rs +++ b/codex-rs/tui/src/bottom_pane/approval_overlay.rs @@ -12,6 +12,7 @@ use crate::diff_render::DiffSummary; use crate::exec_command::strip_bash_lc_and_escape; use crate::history_cell; use crate::key_hint; +use crate::key_hint::KeyBinding; use crate::render::highlight::highlight_bash_to_lines; use crate::render::renderable::ColumnRenderable; use crate::render::renderable::Renderable; @@ -94,8 +95,14 @@ impl ApprovalOverlay { header: Box, ) -> (Vec, SelectionViewParams) { let (options, title) = match &variant { - ApprovalVariant::Exec { .. } => (exec_options(), "Allow command?".to_string()), - ApprovalVariant::ApplyPatch { .. } => (patch_options(), "Apply changes?".to_string()), + ApprovalVariant::Exec { .. } => ( + exec_options(), + "Would you like to run the following command?".to_string(), + ), + ApprovalVariant::ApplyPatch { .. } => ( + patch_options(), + "Would you like to make the following edits?".to_string(), + ), }; let header = Box::new(ColumnRenderable::new([ @@ -108,11 +115,9 @@ impl ApprovalOverlay { .iter() .map(|opt| SelectionItem { name: opt.label.clone(), - description: Some(opt.description.clone()), - is_current: false, - actions: Vec::new(), + display_shortcut: opt.display_shortcut, dismiss_on_select: false, - search_value: None, + ..Default::default() }) .collect(); @@ -197,28 +202,18 @@ impl ApprovalOverlay { false } } - KeyEvent { - kind: KeyEventKind::Press, - code: KeyCode::Char(c), - modifiers, - .. - } if !modifiers.contains(KeyModifiers::CONTROL) - && !modifiers.contains(KeyModifiers::ALT) => - { - let lower = c.to_ascii_lowercase(); - match self + e => { + if let Some(idx) = self .options .iter() - .position(|opt| opt.shortcut.map(|s| s == lower).unwrap_or(false)) + .position(|opt| opt.shortcuts().any(|s| s.is_press(*e))) { - Some(idx) => { - self.apply_selection(idx); - true - } - None => false, + self.apply_selection(idx); + true + } else { + false } } - _ => false, } } } @@ -299,7 +294,7 @@ impl From for ApprovalRequestState { if let Some(reason) = reason && !reason.is_empty() { - header.push(reason.italic().into()); + header.push(Line::from(vec!["Reason: ".into(), reason.italic()])); header.push(Line::from("")); } let full_cmd = strip_bash_lc_and_escape(&command); @@ -347,31 +342,38 @@ enum ApprovalVariant { #[derive(Clone)] struct ApprovalOption { label: String, - description: String, decision: ReviewDecision, - shortcut: Option, + display_shortcut: Option, + additional_shortcuts: Vec, +} + +impl ApprovalOption { + fn shortcuts(&self) -> impl Iterator + '_ { + self.display_shortcut + .into_iter() + .chain(self.additional_shortcuts.iter().copied()) + } } fn exec_options() -> Vec { vec![ ApprovalOption { - label: "Approve and run now".to_string(), - description: "Run this command one time".to_string(), + label: "Yes, proceed".to_string(), decision: ReviewDecision::Approved, - shortcut: Some('y'), + display_shortcut: None, + additional_shortcuts: vec![key_hint::plain(KeyCode::Char('y'))], }, ApprovalOption { - label: "Always approve this session".to_string(), - description: "Automatically approve this command for the rest of the session" - .to_string(), + label: "Yes, and don't ask again for this command".to_string(), decision: ReviewDecision::ApprovedForSession, - shortcut: Some('a'), + display_shortcut: None, + additional_shortcuts: vec![key_hint::plain(KeyCode::Char('a'))], }, ApprovalOption { - label: "Cancel".to_string(), - description: "Do not run the command".to_string(), + label: "No, and tell Codex what to do differently".to_string(), decision: ReviewDecision::Abort, - shortcut: Some('n'), + display_shortcut: Some(key_hint::plain(KeyCode::Esc)), + additional_shortcuts: vec![key_hint::plain(KeyCode::Char('n'))], }, ] } @@ -379,16 +381,16 @@ fn exec_options() -> Vec { fn patch_options() -> Vec { vec![ ApprovalOption { - label: "Approve".to_string(), - description: "Apply the proposed changes".to_string(), + label: "Yes, proceed".to_string(), decision: ReviewDecision::Approved, - shortcut: Some('y'), + display_shortcut: None, + additional_shortcuts: vec![key_hint::plain(KeyCode::Char('y'))], }, ApprovalOption { - label: "Cancel".to_string(), - description: "Do not apply the changes".to_string(), + label: "No, and tell Codex what to do differently".to_string(), decision: ReviewDecision::Abort, - shortcut: Some('n'), + display_shortcut: Some(key_hint::plain(KeyCode::Esc)), + additional_shortcuts: vec![key_hint::plain(KeyCode::Char('n'))], }, ] } diff --git a/codex-rs/tui/src/bottom_pane/command_popup.rs b/codex-rs/tui/src/bottom_pane/command_popup.rs index b597f9ec08..1842a999e3 100644 --- a/codex-rs/tui/src/bottom_pane/command_popup.rs +++ b/codex-rs/tui/src/bottom_pane/command_popup.rs @@ -173,6 +173,7 @@ impl CommandPopup { name, match_indices: indices.map(|v| v.into_iter().map(|i| i + 1).collect()), is_current: false, + display_shortcut: None, description: Some(description), } }) diff --git a/codex-rs/tui/src/bottom_pane/file_search_popup.rs b/codex-rs/tui/src/bottom_pane/file_search_popup.rs index 6ba8599dd1..708b004748 100644 --- a/codex-rs/tui/src/bottom_pane/file_search_popup.rs +++ b/codex-rs/tui/src/bottom_pane/file_search_popup.rs @@ -130,6 +130,7 @@ impl WidgetRef for &FileSearchPopup { .as_ref() .map(|v| v.iter().map(|&i| i as usize).collect()), is_current: false, + display_shortcut: None, description: None, }) .collect() diff --git a/codex-rs/tui/src/bottom_pane/list_selection_view.rs b/codex-rs/tui/src/bottom_pane/list_selection_view.rs index cb3de2122c..9f0ce3df24 100644 --- a/codex-rs/tui/src/bottom_pane/list_selection_view.rs +++ b/codex-rs/tui/src/bottom_pane/list_selection_view.rs @@ -1,6 +1,7 @@ use crossterm::event::KeyCode; use crossterm::event::KeyEvent; use crossterm::event::KeyModifiers; +use itertools::Itertools as _; use ratatui::buffer::Buffer; use ratatui::layout::Constraint; use ratatui::layout::Layout; @@ -13,6 +14,7 @@ use ratatui::widgets::Paragraph; use ratatui::widgets::Widget; use crate::app_event_sender::AppEventSender; +use crate::key_hint::KeyBinding; use crate::render::Insets; use crate::render::RectExt as _; use crate::render::renderable::ColumnRenderable; @@ -31,8 +33,10 @@ use super::selection_popup_common::render_rows; /// One selectable item in the generic selection list. pub(crate) type SelectionAction = Box; +#[derive(Default)] pub(crate) struct SelectionItem { pub name: String, + pub display_shortcut: Option, pub description: Option, pub is_current: bool, pub actions: Vec, @@ -135,18 +139,10 @@ impl ListSelectionView { self.filtered_indices = self .items .iter() - .enumerate() - .filter_map(|(idx, item)| { - let matches = if let Some(search_value) = &item.search_value { - search_value.to_lowercase().contains(&query_lower) - } else { - let mut matches = item.name.to_lowercase().contains(&query_lower); - if !matches && let Some(desc) = &item.description { - matches = desc.to_lowercase().contains(&query_lower); - } - matches - }; - matches.then_some(idx) + .positions(|item| { + item.search_value + .as_ref() + .is_some_and(|v| v.to_lowercase().contains(&query_lower)) }) .collect(); } else { @@ -200,6 +196,7 @@ impl ListSelectionView { }; GenericDisplayRow { name: display_name, + display_shortcut: item.display_shortcut, match_indices: None, is_current: item.is_current, description: item.description.clone(), @@ -329,7 +326,8 @@ impl Renderable for ListSelectionView { let rows_height = measure_rows_height(&rows, &self.state, MAX_POPUP_ROWS, width); - let mut height = self.header.desired_height(width); + // Subtract 4 for the padding on the left and right of the header. + let mut height = self.header.desired_height(width.saturating_sub(4)); height = height.saturating_add(rows_height + 3); if self.is_searchable { height = height.saturating_add(1); @@ -355,7 +353,10 @@ impl Renderable for ListSelectionView { .style(user_message_style(terminal_palette::default_bg())) .render(content_area, buf); - let header_height = self.header.desired_height(content_area.width); + let header_height = self + .header + // Subtract 4 for the padding on the left and right of the header. + .desired_height(content_area.width.saturating_sub(4)); let rows = self.build_rows(); let rows_height = measure_rows_height(&rows, &self.state, MAX_POPUP_ROWS, content_area.width); @@ -438,17 +439,15 @@ mod tests { name: "Read Only".to_string(), description: Some("Codex can read files".to_string()), is_current: true, - actions: vec![], dismiss_on_select: true, - search_value: None, + ..Default::default() }, SelectionItem { name: "Full Access".to_string(), description: Some("Codex can edit files".to_string()), is_current: false, - actions: vec![], dismiss_on_select: true, - search_value: None, + ..Default::default() }, ]; ListSelectionView::new( @@ -510,9 +509,8 @@ mod tests { name: "Read Only".to_string(), description: Some("Codex can read files".to_string()), is_current: false, - actions: vec![], dismiss_on_select: true, - search_value: None, + ..Default::default() }]; let mut view = ListSelectionView::new( SelectionViewParams { diff --git a/codex-rs/tui/src/bottom_pane/selection_popup_common.rs b/codex-rs/tui/src/bottom_pane/selection_popup_common.rs index fd523532a2..4246adf4b5 100644 --- a/codex-rs/tui/src/bottom_pane/selection_popup_common.rs +++ b/codex-rs/tui/src/bottom_pane/selection_popup_common.rs @@ -9,11 +9,14 @@ use ratatui::text::Span; use ratatui::widgets::Widget; use unicode_width::UnicodeWidthChar; +use crate::key_hint::KeyBinding; + use super::scroll_state::ScrollState; /// A generic representation of a display row for selection popups. pub(crate) struct GenericDisplayRow { pub name: String, + pub display_shortcut: Option, pub match_indices: Option>, // indices to bold (char positions) pub is_current: bool, pub description: Option, // optional grey text after the name @@ -92,6 +95,10 @@ fn build_full_line(row: &GenericDisplayRow, desc_col: usize) -> Line<'static> { let this_name_width = Line::from(name_spans.clone()).width(); let mut full_spans: Vec = name_spans; + if let Some(display_shortcut) = row.display_shortcut { + full_spans.push(" ".into()); + full_spans.push(display_shortcut.into()); + } if let Some(desc) = row.description.as_ref() { let gap = desc_col.saturating_sub(this_name_width); if gap > 0 { @@ -155,6 +162,7 @@ pub(crate) fn render_rows( let GenericDisplayRow { name, match_indices, + display_shortcut, is_current: _is_current, description, } = row; @@ -163,6 +171,7 @@ pub(crate) fn render_rows( &GenericDisplayRow { name: name.clone(), match_indices: match_indices.clone(), + display_shortcut: *display_shortcut, is_current: *_is_current, description: description.clone(), }, diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index c03caca85c..1292aafd34 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -1630,7 +1630,7 @@ impl ChatWidget { is_current, actions, dismiss_on_select: true, - search_value: None, + ..Default::default() }); } @@ -1749,7 +1749,7 @@ impl ChatWidget { is_current: is_current_model && choice.stored == highlight_choice, actions, dismiss_on_select: true, - search_value: None, + ..Default::default() }); } @@ -1793,7 +1793,7 @@ impl ChatWidget { is_current, actions, dismiss_on_select: true, - search_value: None, + ..Default::default() }); } @@ -1917,7 +1917,6 @@ impl ChatWidget { items.push(SelectionItem { name: "Review against a base branch".to_string(), description: Some("(PR Style)".into()), - is_current: false, actions: vec![Box::new({ let cwd = self.config.cwd.clone(); move |tx| { @@ -1925,13 +1924,11 @@ impl ChatWidget { } })], dismiss_on_select: false, - search_value: None, + ..Default::default() }); items.push(SelectionItem { name: "Review uncommitted changes".to_string(), - description: None, - is_current: false, actions: vec![Box::new( move |tx: &AppEventSender| { tx.send(AppEvent::CodexOp(Op::Review { @@ -1943,14 +1940,12 @@ impl ChatWidget { }, )], dismiss_on_select: true, - search_value: None, + ..Default::default() }); // New: Review a specific commit (opens commit picker) items.push(SelectionItem { name: "Review a commit".to_string(), - description: None, - is_current: false, actions: vec![Box::new({ let cwd = self.config.cwd.clone(); move |tx| { @@ -1958,18 +1953,16 @@ impl ChatWidget { } })], dismiss_on_select: false, - search_value: None, + ..Default::default() }); items.push(SelectionItem { name: "Custom review instructions".to_string(), - description: None, - is_current: false, actions: vec![Box::new(move |tx| { tx.send(AppEvent::OpenReviewCustomPrompt); })], dismiss_on_select: false, - search_value: None, + ..Default::default() }); self.bottom_pane.show_selection_view(SelectionViewParams { @@ -1991,8 +1984,6 @@ impl ChatWidget { let branch = option.clone(); items.push(SelectionItem { name: format!("{current_branch} -> {branch}"), - description: None, - is_current: false, actions: vec![Box::new(move |tx3: &AppEventSender| { tx3.send(AppEvent::CodexOp(Op::Review { review_request: ReviewRequest { @@ -2005,6 +1996,7 @@ impl ChatWidget { })], dismiss_on_select: true, search_value: Some(option), + ..Default::default() }); } @@ -2030,8 +2022,6 @@ impl ChatWidget { items.push(SelectionItem { name: subject.clone(), - description: None, - is_current: false, actions: vec![Box::new(move |tx3: &AppEventSender| { let hint = format!("commit {short}"); let prompt = format!( @@ -2046,6 +2036,7 @@ impl ChatWidget { })], dismiss_on_select: true, search_value: Some(search_val), + ..Default::default() }); } @@ -2255,8 +2246,6 @@ pub(crate) fn show_review_commit_picker_with_entries( items.push(SelectionItem { name: subject.clone(), - description: None, - is_current: false, actions: vec![Box::new(move |tx3: &AppEventSender| { let hint = format!("commit {short}"); let prompt = format!( @@ -2271,6 +2260,7 @@ pub(crate) fn show_review_commit_picker_with_entries( })], dismiss_on_select: true, search_value: Some(search_val), + ..Default::default() }); } diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec.snap index 558728bd31..d0990fa93a 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec.snap @@ -2,15 +2,15 @@ source: tui/src/chatwidget/tests.rs expression: terminal.backend().vt100().screen().contents() --- - Allow command? + Would you like to run the following command? - this is a test reason such as one that would be produced by the model + Reason: this is a test reason such as one that would be produced by the + model $ echo hello world -› 1. Approve and run now Run this command one time - 2. Always approve this session Automatically approve this command for the - rest of the session - 3. Cancel Do not run the command +› 1. Yes, proceed + 2. Yes, and don't ask again for this command + 3. No, and tell Codex what to do differently esc Press enter to confirm or esc to cancel diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec_no_reason.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec_no_reason.snap index 53348af155..3a557bf6c9 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec_no_reason.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_exec_no_reason.snap @@ -1,17 +1,13 @@ --- source: tui/src/chatwidget/tests.rs -expression: terminal.backend() +expression: terminal.backend().vt100().screen().contents() --- -" " -" " -" Allow command? " -" " -" $ echo hello world " -" " -"› 1. Approve and run now Run this command one time " -" 2. Always approve this session Automatically approve this command for the " -" rest of the session " -" 3. Cancel Do not run the command " -" " -" Press enter to confirm or esc to cancel " -" " + Would you like to run the following command? + + $ echo hello world + +› 1. Yes, proceed + 2. Yes, and don't ask again for this command + 3. No, and tell Codex what to do differently esc + + Press enter to confirm or esc to cancel diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_patch.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_patch.snap index 64fbada3be..ab88ffaf73 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_patch.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__approval_modal_patch.snap @@ -1,19 +1,17 @@ --- source: tui/src/chatwidget/tests.rs -expression: terminal.backend() +expression: terminal.backend().vt100().screen().contents() --- -" " -" " -" Apply changes? " -" " -" README.md (+2 -0) " -" 1 +hello " -" 2 +world " -" " -" The model wants to apply changes " -" " -"› 1. Approve Apply the proposed changes " -" 2. Cancel Do not apply the changes " -" " -" Press enter to confirm or esc to cancel " -" " + Would you like to make the following edits? + + README.md (+2 -0) + + 1 +hello + 2 +world + + The model wants to apply changes + +› 1. Yes, proceed + 2. No, and tell Codex what to do differently esc + + Press enter to confirm or esc to cancel diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__exec_approval_modal_exec.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__exec_approval_modal_exec.snap index 8b7c5838cc..f52a0f38a2 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__exec_approval_modal_exec.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__exec_approval_modal_exec.snap @@ -7,16 +7,16 @@ Buffer { content: [ " ", " ", - " Allow command? ", + " Would you like to run the following command? ", " ", - " this is a test reason such as one that would be produced by the model ", + " Reason: this is a test reason such as one that would be produced by the ", + " model ", " ", " $ echo hello world ", " ", - "› 1. Approve and run now Run this command one time ", - " 2. Always approve this session Automatically approve this command for the ", - " rest of the session ", - " 3. Cancel Do not run the command ", + "› 1. Yes, proceed ", + " 2. Yes, and don't ask again for this command ", + " 3. No, and tell Codex what to do differently esc ", " ", " Press enter to confirm or esc to cancel ", " ", @@ -24,18 +24,15 @@ Buffer { styles: [ x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, x: 2, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: BOLD, - x: 16, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, - x: 2, y: 4, fg: Reset, bg: Reset, underline: Reset, modifier: ITALIC, - x: 71, y: 4, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, - x: 0, y: 8, fg: Cyan, bg: Reset, underline: Reset, modifier: BOLD, - x: 34, y: 8, fg: Cyan, bg: Reset, underline: Reset, modifier: BOLD | DIM, - x: 59, y: 8, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, - x: 34, y: 9, fg: Reset, bg: Reset, underline: Reset, modifier: DIM, - x: 76, y: 9, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, - x: 34, y: 10, fg: Reset, bg: Reset, underline: Reset, modifier: DIM, - x: 53, y: 10, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, - x: 34, y: 11, fg: Reset, bg: Reset, underline: Reset, modifier: DIM, - x: 56, y: 11, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, + x: 46, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, + x: 10, y: 4, fg: Reset, bg: Reset, underline: Reset, modifier: ITALIC, + x: 73, y: 4, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, + x: 2, y: 5, fg: Reset, bg: Reset, underline: Reset, modifier: ITALIC, + x: 7, y: 5, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, + x: 0, y: 9, fg: Cyan, bg: Reset, underline: Reset, modifier: BOLD, + x: 17, y: 9, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, + x: 47, y: 11, fg: Reset, bg: Reset, underline: Reset, modifier: DIM, + x: 50, y: 11, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, x: 2, y: 13, fg: Reset, bg: Reset, underline: Reset, modifier: DIM, x: 0, y: 14, fg: Reset, bg: Reset, underline: Reset, modifier: NONE, ] diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_and_approval_modal.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_and_approval_modal.snap index 96b51f7a36..d1951cd082 100644 --- a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_and_approval_modal.snap +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_widget_and_approval_modal.snap @@ -4,16 +4,16 @@ expression: terminal.backend() --- " " " " -" Allow command? " +" Would you like to run the following command? " " " -" this is a test reason such as one that would be produced by the model " +" Reason: this is a test reason such as one that would be produced by the " +" model " " " " $ echo 'hello world' " " " -"› 1. Approve and run now Run this command one time " -" 2. Always approve this session Automatically approve this command for the " -" rest of the session " -" 3. Cancel Do not run the command " +"› 1. Yes, proceed " +" 2. Yes, and don't ask again for this command " +" 3. No, and tell Codex what to do differently esc " " " " Press enter to confirm or esc to cancel " " " diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index 64b4c72b34..b61d3d2e4b 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -1236,6 +1236,14 @@ fn approval_modal_exec_snapshot() { terminal .draw(|f| f.render_widget_ref(&chat, f.area())) .expect("draw approval modal"); + assert!( + terminal + .backend() + .vt100() + .screen() + .contents() + .contains("echo hello world") + ); assert_snapshot!( "approval_modal_exec", terminal.backend().vt100().screen().contents() @@ -1261,12 +1269,16 @@ fn approval_modal_exec_without_reason_snapshot() { }); let height = chat.desired_height(80); - let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, height)) - .expect("create terminal"); + let mut terminal = + ratatui::Terminal::new(VT100Backend::new(80, height)).expect("create terminal"); + terminal.set_viewport_area(Rect::new(0, 0, 80, height)); terminal .draw(|f| f.render_widget_ref(&chat, f.area())) .expect("draw approval modal (no reason)"); - assert_snapshot!("approval_modal_exec_no_reason", terminal.backend()); + assert_snapshot!( + "approval_modal_exec_no_reason", + terminal.backend().vt100().screen().contents() + ); } // Snapshot test: patch approval modal @@ -1296,12 +1308,16 @@ fn approval_modal_patch_snapshot() { // Render at the widget's desired height and snapshot. let height = chat.desired_height(80); - let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, height)) - .expect("create terminal"); + let mut terminal = + ratatui::Terminal::new(VT100Backend::new(80, height)).expect("create terminal"); + terminal.set_viewport_area(Rect::new(0, 0, 80, height)); terminal .draw(|f| f.render_widget_ref(&chat, f.area())) .expect("draw patch approval modal"); - assert_snapshot!("approval_modal_patch", terminal.backend()); + assert_snapshot!( + "approval_modal_patch", + terminal.backend().vt100().screen().contents() + ); } #[test] @@ -1831,14 +1847,14 @@ fn apply_patch_untrusted_shows_approval_modal() { for x in 0..area.width { row.push(buf[(x, y)].symbol().chars().next().unwrap_or(' ')); } - if row.contains("Apply changes?") { + if row.contains("Would you like to make the following edits?") { contains_title = true; break; } } assert!( contains_title, - "expected approval modal to be visible with title 'Apply changes?'" + "expected approval modal to be visible with title 'Would you like to make the following edits?'" ); } diff --git a/codex-rs/tui/src/diff_render.rs b/codex-rs/tui/src/diff_render.rs index 6f133feeb5..b7fa5be541 100644 --- a/codex-rs/tui/src/diff_render.rs +++ b/codex-rs/tui/src/diff_render.rs @@ -64,6 +64,7 @@ impl From for Box { path.push_span(" "); path.extend(render_line_count_summary(row.added, row.removed)); rows.push(Box::new(path)); + rows.push(Box::new(RtLine::from(""))); rows.push(Box::new(row.change)); } From 138be0fd73d8aea87308dbe20cd5e07bc1ada473 Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 2 Oct 2025 14:43:40 -0700 Subject: [PATCH 15/16] Use GH cli to fetch current issue (#4630) Attempting to format the env var caused escaping issues --- .github/workflows/issue-deduplicator.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/issue-deduplicator.yml b/.github/workflows/issue-deduplicator.yml index 05453450e4..e729415c4e 100644 --- a/.github/workflows/issue-deduplicator.yml +++ b/.github/workflows/issue-deduplicator.yml @@ -35,8 +35,10 @@ jobs: | jq '.' \ > "$EXISTING_ISSUES_FILE" - printf '%s' '${{ toJson(github.event.issue) }}' \ - | jq '{number, title, body}' \ + gh issue view "${{ github.event.issue.number }}" \ + --repo "${{ github.repository }}" \ + --json number,title,body \ + | jq '.' \ > "$CURRENT_ISSUE_FILE" - id: codex From c405d8c06c877faeb55621258bd0c5bcce6591ef Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 2 Oct 2025 15:07:14 -0700 Subject: [PATCH 16/16] Rename assistant message to agent message and fix item type field naming (#4610) Naming cleanup --- .../src/event_processor_with_jsonl_output.rs | 4 ++-- codex-rs/exec/src/exec_events.rs | 6 ++--- .../tests/event_processor_with_json_output.rs | 6 ++--- sdk/typescript/samples/basic_streaming.ts | 6 ++--- sdk/typescript/src/index.ts | 2 +- sdk/typescript/src/items.ts | 22 +++++++++---------- sdk/typescript/src/thread.ts | 2 +- sdk/typescript/tests/run.test.ts | 2 +- sdk/typescript/tests/runStreamed.test.ts | 2 +- 9 files changed, 26 insertions(+), 26 deletions(-) diff --git a/codex-rs/exec/src/event_processor_with_jsonl_output.rs b/codex-rs/exec/src/event_processor_with_jsonl_output.rs index b3c82fc943..51ecd71a9b 100644 --- a/codex-rs/exec/src/event_processor_with_jsonl_output.rs +++ b/codex-rs/exec/src/event_processor_with_jsonl_output.rs @@ -5,7 +5,7 @@ use std::sync::atomic::AtomicU64; use crate::event_processor::CodexStatus; use crate::event_processor::EventProcessor; use crate::event_processor::handle_last_message; -use crate::exec_events::AssistantMessageItem; +use crate::exec_events::AgentMessageItem; use crate::exec_events::CommandExecutionItem; use crate::exec_events::CommandExecutionStatus; use crate::exec_events::FileChangeItem; @@ -162,7 +162,7 @@ impl EventProcessorWithJsonOutput { let item = ThreadItem { id: self.get_next_item_id(), - details: ThreadItemDetails::AssistantMessage(AssistantMessageItem { + details: ThreadItemDetails::AgentMessage(AgentMessageItem { text: payload.message.clone(), }), }; diff --git a/codex-rs/exec/src/exec_events.rs b/codex-rs/exec/src/exec_events.rs index 4ea57e19b5..606cf9187a 100644 --- a/codex-rs/exec/src/exec_events.rs +++ b/codex-rs/exec/src/exec_events.rs @@ -95,11 +95,11 @@ pub struct ThreadItem { /// Typed payloads for each supported thread item type. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)] -#[serde(tag = "item_type", rename_all = "snake_case")] +#[serde(tag = "type", rename_all = "snake_case")] pub enum ThreadItemDetails { /// Response from the agent. /// Either a natural-language response or a JSON string when structured output is requested. - AssistantMessage(AssistantMessageItem), + AgentMessage(AgentMessageItem), /// Agent's reasoning summary. Reasoning(ReasoningItem), /// Tracks a command executed by the agent. The item starts when the command is @@ -124,7 +124,7 @@ pub enum ThreadItemDetails { /// Response from the agent. /// Either a natural-language response or a JSON string when structured output is requested. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)] -pub struct AssistantMessageItem { +pub struct AgentMessageItem { pub text: String, } diff --git a/codex-rs/exec/tests/event_processor_with_json_output.rs b/codex-rs/exec/tests/event_processor_with_json_output.rs index 9cbff57943..b6475ad111 100644 --- a/codex-rs/exec/tests/event_processor_with_json_output.rs +++ b/codex-rs/exec/tests/event_processor_with_json_output.rs @@ -14,7 +14,7 @@ use codex_core::protocol::PatchApplyEndEvent; use codex_core::protocol::SessionConfiguredEvent; use codex_core::protocol::WebSearchEndEvent; use codex_exec::event_processor_with_jsonl_output::EventProcessorWithJsonOutput; -use codex_exec::exec_events::AssistantMessageItem; +use codex_exec::exec_events::AgentMessageItem; use codex_exec::exec_events::CommandExecutionItem; use codex_exec::exec_events::CommandExecutionStatus; use codex_exec::exec_events::ItemCompletedEvent; @@ -410,7 +410,7 @@ fn agent_reasoning_produces_item_completed_reasoning() { } #[test] -fn agent_message_produces_item_completed_assistant_message() { +fn agent_message_produces_item_completed_agent_message() { let mut ep = EventProcessorWithJsonOutput::new(None); let ev = event( "e1", @@ -424,7 +424,7 @@ fn agent_message_produces_item_completed_assistant_message() { vec![ThreadEvent::ItemCompleted(ItemCompletedEvent { item: ThreadItem { id: "item_0".to_string(), - details: ThreadItemDetails::AssistantMessage(AssistantMessageItem { + details: ThreadItemDetails::AgentMessage(AgentMessageItem { text: "hello".to_string(), }), }, diff --git a/sdk/typescript/samples/basic_streaming.ts b/sdk/typescript/samples/basic_streaming.ts index 97098d8a69..e78a0d41ac 100755 --- a/sdk/typescript/samples/basic_streaming.ts +++ b/sdk/typescript/samples/basic_streaming.ts @@ -16,8 +16,8 @@ const thread = codex.startThread(); const rl = createInterface({ input, output }); const handleItemCompleted = (item: ThreadItem): void => { - switch (item.item_type) { - case "assistant_message": + switch (item.type) { + case "agent_message": console.log(`Assistant: ${item.text}`); break; case "reasoning": @@ -38,7 +38,7 @@ const handleItemCompleted = (item: ThreadItem): void => { }; const handleItemUpdated = (item: ThreadItem): void => { - switch (item.item_type) { + switch (item.type) { case "todo_list": { console.log(`Todo:`); for (const todo of item.items) { diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.ts index b759b3c68f..fc8383f363 100644 --- a/sdk/typescript/src/index.ts +++ b/sdk/typescript/src/index.ts @@ -12,7 +12,7 @@ export type { } from "./events"; export type { ThreadItem, - AssistantMessageItem, + AgentMessageItem, ReasoningItem, CommandExecutionItem, FileChangeItem, diff --git a/sdk/typescript/src/items.ts b/sdk/typescript/src/items.ts index 85f54d78ca..37563f9af9 100644 --- a/sdk/typescript/src/items.ts +++ b/sdk/typescript/src/items.ts @@ -6,7 +6,7 @@ export type CommandExecutionStatus = "in_progress" | "completed" | "failed"; /** A command executed by the agent. */ export type CommandExecutionItem = { id: string; - item_type: "command_execution"; + type: "command_execution"; /** The command line executed by the agent. */ command: string; /** Aggregated stdout and stderr captured while the command was running. */ @@ -32,7 +32,7 @@ export type PatchApplyStatus = "completed" | "failed"; /** A set of file changes by the agent. Emitted once the patch succeeds or fails. */ export type FileChangeItem = { id: string; - item_type: "file_change"; + type: "file_change"; /** Individual file changes that comprise the patch. */ changes: FileUpdateChange[]; /** Whether the patch ultimately succeeded or failed. */ @@ -48,7 +48,7 @@ export type McpToolCallStatus = "in_progress" | "completed" | "failed"; */ export type McpToolCallItem = { id: string; - item_type: "mcp_tool_call"; + type: "mcp_tool_call"; /** Name of the MCP server handling the request. */ server: string; /** The tool invoked on the MCP server. */ @@ -58,9 +58,9 @@ export type McpToolCallItem = { }; /** Response from the agent. Either natural-language text or JSON when structured output is requested. */ -export type AssistantMessageItem = { +export type AgentMessageItem = { id: string; - item_type: "assistant_message"; + type: "agent_message"; /** Either natural-language text or JSON when structured output is requested. */ text: string; }; @@ -68,21 +68,21 @@ export type AssistantMessageItem = { /** Agent's reasoning summary. */ export type ReasoningItem = { id: string; - item_type: "reasoning"; + type: "reasoning"; text: string; }; /** Captures a web search request. Completes when results are returned to the agent. */ export type WebSearchItem = { id: string; - item_type: "web_search"; + type: "web_search"; query: string; }; /** Describes a non-fatal error surfaced as an item. */ export type ErrorItem = { id: string; - item_type: "error"; + type: "error"; message: string; }; @@ -98,19 +98,19 @@ export type TodoItem = { */ export type TodoListItem = { id: string; - item_type: "todo_list"; + type: "todo_list"; items: TodoItem[]; }; export type SessionItem = { id: string; - item_type: "session"; + type: "session"; session_id: string; }; /** Canonical union of thread items and their type-specific payloads. */ export type ThreadItem = - | AssistantMessageItem + | AgentMessageItem | ReasoningItem | CommandExecutionItem | FileChangeItem diff --git a/sdk/typescript/src/thread.ts b/sdk/typescript/src/thread.ts index fe32a27136..b710fec399 100644 --- a/sdk/typescript/src/thread.ts +++ b/sdk/typescript/src/thread.ts @@ -87,7 +87,7 @@ export class Thread { let finalResponse: string = ""; for await (const event of generator) { if (event.type === "item.completed") { - if (event.item.item_type === "assistant_message") { + if (event.item.type === "agent_message") { finalResponse = event.item.text; } items.push(event.item); diff --git a/sdk/typescript/tests/run.test.ts b/sdk/typescript/tests/run.test.ts index 357d8cd0db..17cdae09e6 100644 --- a/sdk/typescript/tests/run.test.ts +++ b/sdk/typescript/tests/run.test.ts @@ -33,7 +33,7 @@ describe("Codex", () => { const expectedItems = [ { id: expect.any(String), - item_type: "assistant_message", + type: "agent_message", text: "Hi!", }, ]; diff --git a/sdk/typescript/tests/runStreamed.test.ts b/sdk/typescript/tests/runStreamed.test.ts index 1db273b98a..f95203ae3b 100644 --- a/sdk/typescript/tests/runStreamed.test.ts +++ b/sdk/typescript/tests/runStreamed.test.ts @@ -45,7 +45,7 @@ describe("Codex", () => { type: "item.completed", item: { id: "item_0", - item_type: "assistant_message", + type: "agent_message", text: "Hi!", }, },