From 3b24a9a53264f96e7caeea0577b994b0d10a8c6f Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Mon, 13 Apr 2026 21:52:56 -0700 Subject: [PATCH 01/16] Refactor plugin loading to async (#17747) Simplifies skills migration. --- .../app-server/src/codex_message_processor.rs | 48 ++++---- codex-rs/app-server/src/config_api.rs | 11 +- codex-rs/chatgpt/src/connectors.rs | 12 +- codex-rs/cli/src/mcp_cmd.rs | 8 +- codex-rs/core/src/agent/role_tests.rs | 2 +- codex-rs/core/src/codex.rs | 40 +++--- codex-rs/core/src/codex_tests.rs | 6 +- codex-rs/core/src/config/config_tests.rs | 10 +- codex-rs/core/src/config/mod.rs | 7 +- codex-rs/core/src/connectors.rs | 12 +- codex-rs/core/src/connectors_tests.rs | 2 +- codex-rs/core/src/mcp.rs | 12 +- codex-rs/core/src/mcp_skill_dependencies.rs | 8 +- codex-rs/core/src/plugins/discoverable.rs | 11 +- .../core/src/plugins/discoverable_tests.rs | 24 +++- codex-rs/core/src/plugins/manager.rs | 114 +++++++++++------- codex-rs/core/src/plugins/manager_tests.rs | 109 +++++++++-------- codex-rs/core/src/skills_watcher.rs | 4 +- codex-rs/core/src/thread_manager.rs | 13 +- codex-rs/tui/src/app.rs | 30 +++++ codex-rs/tui/src/app_event.rs | 9 ++ codex-rs/tui/src/chatwidget.rs | 14 ++- codex-rs/tui/src/history_cell.rs | 11 +- 23 files changed, 308 insertions(+), 209 deletions(-) diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 5c70cdb9d3..4feda2ab06 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -5328,7 +5328,11 @@ impl CodexMessageProcessor { &self, config: &Config, ) -> Result<(), JSONRPCErrorError> { - let configured_servers = self.thread_manager.mcp_manager().configured_servers(config); + let configured_servers = self + .thread_manager + .mcp_manager() + .configured_servers(config) + .await; let mcp_servers = match serde_json::to_value(configured_servers) { Ok(value) => value, Err(err) => { @@ -5388,7 +5392,8 @@ impl CodexMessageProcessor { let configured_servers = self .thread_manager .mcp_manager() - .configured_servers(&config); + .configured_servers(&config) + .await; let Some(server) = configured_servers.get(&name) else { let error = JSONRPCErrorError { code: INVALID_REQUEST_ERROR_CODE, @@ -5490,7 +5495,9 @@ impl CodexMessageProcessor { return; } }; - let mcp_config = config.to_mcp_config(self.thread_manager.plugins_manager().as_ref()); + let mcp_config = config + .to_mcp_config(self.thread_manager.plugins_manager().as_ref()) + .await; let auth = self.auth_manager.auth().await; tokio::spawn(async move { @@ -6315,10 +6322,12 @@ impl CodexMessageProcessor { continue; } }; - let effective_skill_roots = plugins_manager.effective_skill_roots_for_layer_stack( - &config_layer_stack, - config.features.enabled(Feature::Plugins), - ); + let effective_skill_roots = plugins_manager + .effective_skill_roots_for_layer_stack( + &config_layer_stack, + config.features.enabled(Feature::Plugins), + ) + .await; let skills_input = codex_core::skills::SkillsLoadInput::new( cwd_abs, effective_skill_roots, @@ -6544,26 +6553,16 @@ impl CodexMessageProcessor { plugin_name, marketplace_path, }; - let config_for_read = config.clone(); - let outcome = match tokio::task::spawn_blocking(move || { - plugins_manager.read_plugin_for_config(&config_for_read, &request) - }) - .await + let outcome = match plugins_manager + .read_plugin_for_config(&config, &request) + .await { - Ok(Ok(outcome)) => outcome, - Ok(Err(err)) => { + Ok(outcome) => outcome, + Err(err) => { self.send_marketplace_error(request_id, err, "read plugin details") .await; return; } - Err(err) => { - self.send_internal_error( - request_id, - format!("failed to read plugin details: {err}"), - ) - .await; - return; - } }; let app_summaries = plugin_app_helpers::load_plugin_app_summaries(&config, &outcome.plugin.apps).await; @@ -6704,7 +6703,8 @@ impl CodexMessageProcessor { self.clear_plugin_related_caches(); - let plugin_mcp_servers = load_plugin_mcp_servers(result.installed_path.as_path()); + let plugin_mcp_servers = + load_plugin_mcp_servers(result.installed_path.as_path()).await; if !plugin_mcp_servers.is_empty() { if let Err(err) = self.queue_mcp_server_refresh_for_config(&config).await { @@ -6717,7 +6717,7 @@ impl CodexMessageProcessor { .await; } - let plugin_apps = load_plugin_apps(result.installed_path.as_path()); + let plugin_apps = load_plugin_apps(result.installed_path.as_path()).await; let auth = self.auth_manager.auth().await; let apps_needing_auth = if plugin_apps.is_empty() || !config.features.apps_enabled_for_auth( diff --git a/codex-rs/app-server/src/config_api.rs b/codex-rs/app-server/src/config_api.rs index e85f137bc9..7c39f1c44f 100644 --- a/codex-rs/app-server/src/config_api.rs +++ b/codex-rs/app-server/src/config_api.rs @@ -210,7 +210,7 @@ impl ConfigApi { .write_value(params) .await .map_err(map_error)?; - self.emit_plugin_toggle_events(pending_changes); + self.emit_plugin_toggle_events(pending_changes).await; Ok(response) } @@ -230,7 +230,7 @@ impl ConfigApi { .batch_write(params) .await .map_err(map_error)?; - self.emit_plugin_toggle_events(pending_changes); + self.emit_plugin_toggle_events(pending_changes).await; if reload_user_config { self.user_config_reloader.reload_user_config().await; } @@ -299,13 +299,16 @@ impl ConfigApi { Ok(ExperimentalFeatureEnablementSetResponse { enablement }) } - fn emit_plugin_toggle_events(&self, pending_changes: std::collections::BTreeMap) { + async fn emit_plugin_toggle_events( + &self, + pending_changes: std::collections::BTreeMap, + ) { for (plugin_id, enabled) in pending_changes { let Ok(plugin_id) = PluginId::parse(&plugin_id) else { continue; }; let metadata = - installed_plugin_telemetry_metadata(self.codex_home.as_path(), &plugin_id); + installed_plugin_telemetry_metadata(self.codex_home.as_path(), &plugin_id).await; if enabled { self.analytics_events_client.track_plugin_enabled(metadata); } else { diff --git a/codex-rs/chatgpt/src/connectors.rs b/codex-rs/chatgpt/src/connectors.rs index 5927881a0f..f37609be31 100644 --- a/codex-rs/chatgpt/src/connectors.rs +++ b/codex-rs/chatgpt/src/connectors.rs @@ -73,10 +73,9 @@ pub async fn list_cached_all_connectors(config: &Config) -> Option> } let token_data = get_chatgpt_token_data()?; let cache_key = all_connectors_cache_key(config, &token_data); - codex_connectors::cached_all_connectors(&cache_key).map(|connectors| { - let connectors = merge_plugin_apps(connectors, plugin_apps_for_config(config)); - filter_disallowed_connectors(connectors) - }) + let connectors = codex_connectors::cached_all_connectors(&cache_key)?; + let connectors = merge_plugin_apps(connectors, plugin_apps_for_config(config).await); + Some(filter_disallowed_connectors(connectors)) } pub async fn list_all_connectors_with_options( @@ -106,7 +105,7 @@ pub async fn list_all_connectors_with_options( }, ) .await?; - let connectors = merge_plugin_apps(connectors, plugin_apps_for_config(config)); + let connectors = merge_plugin_apps(connectors, plugin_apps_for_config(config).await); Ok(filter_disallowed_connectors(connectors)) } @@ -119,9 +118,10 @@ fn all_connectors_cache_key(config: &Config, token_data: &TokenData) -> AllConne ) } -fn plugin_apps_for_config(config: &Config) -> Vec { +async fn plugin_apps_for_config(config: &Config) -> Vec { PluginsManager::new(config.codex_home.to_path_buf()) .plugins_for_config(config) + .await .effective_apps() } diff --git a/codex-rs/cli/src/mcp_cmd.rs b/codex-rs/cli/src/mcp_cmd.rs index b8e3fc670d..cac6ef2169 100644 --- a/codex-rs/cli/src/mcp_cmd.rs +++ b/codex-rs/cli/src/mcp_cmd.rs @@ -394,7 +394,7 @@ async fn run_login(config_overrides: &CliConfigOverrides, login_args: LoginArgs) let mcp_manager = McpManager::new(Arc::new(PluginsManager::new( config.codex_home.to_path_buf(), ))); - let mcp_servers = mcp_manager.effective_servers(&config, /*auth*/ None); + let mcp_servers = mcp_manager.effective_servers(&config, /*auth*/ None).await; let LoginArgs { name, scopes } = login_args; @@ -447,7 +447,7 @@ async fn run_logout(config_overrides: &CliConfigOverrides, logout_args: LogoutAr let mcp_manager = McpManager::new(Arc::new(PluginsManager::new( config.codex_home.to_path_buf(), ))); - let mcp_servers = mcp_manager.effective_servers(&config, /*auth*/ None); + let mcp_servers = mcp_manager.effective_servers(&config, /*auth*/ None).await; let LogoutArgs { name } = logout_args; @@ -479,7 +479,7 @@ async fn run_list(config_overrides: &CliConfigOverrides, list_args: ListArgs) -> let mcp_manager = McpManager::new(Arc::new(PluginsManager::new( config.codex_home.to_path_buf(), ))); - let mcp_servers = mcp_manager.effective_servers(&config, /*auth*/ None); + let mcp_servers = mcp_manager.effective_servers(&config, /*auth*/ None).await; let mut entries: Vec<_> = mcp_servers.iter().collect(); entries.sort_by(|(a, _), (b, _)| a.cmp(b)); @@ -730,7 +730,7 @@ async fn run_get(config_overrides: &CliConfigOverrides, get_args: GetArgs) -> Re let mcp_manager = McpManager::new(Arc::new(PluginsManager::new( config.codex_home.to_path_buf(), ))); - let mcp_servers = mcp_manager.effective_servers(&config, /*auth*/ None); + let mcp_servers = mcp_manager.effective_servers(&config, /*auth*/ None).await; let Some(server) = mcp_servers.get(&get_args.name) else { bail!("No MCP server named '{name}' found.", name = get_args.name); diff --git a/codex-rs/core/src/agent/role_tests.rs b/codex-rs/core/src/agent/role_tests.rs index e1d4d3f8c9..2bb10744ac 100644 --- a/codex-rs/core/src/agent/role_tests.rs +++ b/codex-rs/core/src/agent/role_tests.rs @@ -655,7 +655,7 @@ enabled = false let plugins_manager = Arc::new(PluginsManager::new(home.path().to_path_buf())); let skills_manager = SkillsManager::new(home.path().abs(), /*bundled_skills_enabled*/ true); - let plugin_outcome = plugins_manager.plugins_for_config(&config); + let plugin_outcome = plugins_manager.plugins_for_config(&config).await; let effective_skill_roots = plugin_outcome.effective_skill_roots(); let skills_input = skills_load_input_from_config(&config, effective_skill_roots); let outcome = skills_manager.skills_for_config(&skills_input); diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index ec3d7c2022..bab91b177c 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -496,7 +496,7 @@ impl Codex { let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); let (tx_event, rx_event) = async_channel::unbounded(); - let plugin_outcome = plugins_manager.plugins_for_config(&config); + let plugin_outcome = plugins_manager.plugins_for_config(&config).await; let effective_skill_roots = plugin_outcome.effective_skill_roots(); let skills_input = skills_load_input_from_config(&config, effective_skill_roots); let loaded_skills = skills_manager.skills_for_config(&skills_input); @@ -1746,7 +1746,9 @@ impl Session { let mcp_manager_for_mcp = Arc::clone(&mcp_manager); let auth_and_mcp_fut = async move { let auth = auth_manager_clone.auth().await; - let mcp_servers = mcp_manager_for_mcp.effective_servers(&config_for_mcp, auth.as_ref()); + let mcp_servers = mcp_manager_for_mcp + .effective_servers(&config_for_mcp, auth.as_ref()) + .await; let auth_statuses = compute_auth_statuses( mcp_servers.iter(), config_for_mcp.mcp_oauth_credentials_store_mode, @@ -2161,7 +2163,7 @@ impl Session { required_mcp_servers.sort(); let enabled_mcp_server_count = mcp_servers.values().filter(|server| server.enabled).count(); let required_mcp_server_count = required_mcp_servers.len(); - let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config.as_ref()); + let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config.as_ref()).await; { let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; cancel_guard.cancel(); @@ -2658,7 +2660,8 @@ impl Session { let plugin_outcome = self .services .plugins_manager - .plugins_for_config(&per_turn_config); + .plugins_for_config(&per_turn_config) + .await; let effective_skill_roots = plugin_outcome.effective_skill_roots(); let skills_input = skills_load_input_from_config(&per_turn_config, effective_skill_roots); let skills_outcome = Arc::new( @@ -3852,7 +3855,8 @@ impl Session { let loaded_plugins = self .services .plugins_manager - .plugins_for_config(&turn_context.config); + .plugins_for_config(&turn_context.config) + .await; if let Some(plugin_section) = render_plugins_section(loaded_plugins.capability_summaries()) { developer_sections.push(plugin_section); @@ -4501,11 +4505,14 @@ impl Session { ) { let auth = self.services.auth_manager.auth().await; let config = self.get_config().await; - let mcp_config = config.to_mcp_config(self.services.plugins_manager.as_ref()); + let mcp_config = config + .to_mcp_config(self.services.plugins_manager.as_ref()) + .await; let tool_plugin_provenance = self .services .mcp_manager - .tool_plugin_provenance(config.as_ref()); + .tool_plugin_provenance(config.as_ref()) + .await; let mcp_servers = with_codex_apps_mcp(mcp_servers, auth.as_ref(), &mcp_config); let auth_statuses = compute_auth_statuses(mcp_servers.iter(), store_mode).await; let sandbox_state = SandboxState { @@ -5360,7 +5367,8 @@ mod handlers { let mcp_servers = sess .services .mcp_manager - .effective_servers(config, auth.as_ref()); + .effective_servers(config, auth.as_ref()) + .await; let snapshot = collect_mcp_snapshot_from_manager( &mcp_connection_manager, compute_auth_statuses(mcp_servers.iter(), config.mcp_oauth_credentials_store_mode) @@ -5434,10 +5442,12 @@ mod handlers { continue; } }; - let effective_skill_roots = plugins_manager.effective_skill_roots_for_layer_stack( - &config_layer_stack, - config.features.enabled(Feature::Plugins), - ); + let effective_skill_roots = plugins_manager + .effective_skill_roots_for_layer_stack( + &config_layer_stack, + config.features.enabled(Feature::Plugins), + ) + .await; let skills_input = crate::SkillsLoadInput::new( cwd_abs, effective_skill_roots, @@ -6144,7 +6154,8 @@ pub(crate) async fn run_turn( let loaded_plugins = sess .services .plugins_manager - .plugins_for_config(&turn_context.config); + .plugins_for_config(&turn_context.config) + .await; // Structured plugin:// mentions are resolved from the current session's // enabled plugins, then converted into turn-scoped guidance below. let mentioned_plugins = @@ -7057,7 +7068,8 @@ pub(crate) async fn built_tools( let loaded_plugins = sess .services .plugins_manager - .plugins_for_config(&turn_context.config); + .plugins_for_config(&turn_context.config) + .await; let mut effective_explicitly_enabled_connectors = explicitly_enabled_connectors.clone(); effective_explicitly_enabled_connectors.extend(sess.get_connector_selection().await); diff --git a/codex-rs/core/src/codex_tests.rs b/codex-rs/core/src/codex_tests.rs index 34d1b4a625..19ee45f411 100644 --- a/codex-rs/core/src/codex_tests.rs +++ b/codex-rs/core/src/codex_tests.rs @@ -2906,7 +2906,8 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) { let plugin_outcome = services .plugins_manager - .plugins_for_config(&per_turn_config); + .plugins_for_config(&per_turn_config) + .await; let effective_skill_roots = plugin_outcome.effective_skill_roots(); let skills_input = crate::skills_load_input_from_config(&per_turn_config, effective_skill_roots); @@ -3751,7 +3752,8 @@ pub(crate) async fn make_session_and_context_with_dynamic_tools_and_rx( let plugin_outcome = services .plugins_manager - .plugins_for_config(&per_turn_config); + .plugins_for_config(&per_turn_config) + .await; let effective_skill_roots = plugin_outcome.effective_skill_roots(); let skills_input = crate::skills_load_input_from_config(&per_turn_config, effective_skill_roots); diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index b10c62ed14..b2931e1055 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -2192,8 +2192,8 @@ approval_mode = "approve" ); } -#[test] -fn to_mcp_config_preserves_apps_feature_from_config() -> std::io::Result<()> { +#[tokio::test] +async fn to_mcp_config_preserves_apps_feature_from_config() -> std::io::Result<()> { let codex_home = TempDir::new()?; let mut config = Config::load_from_base_config_with_overrides( ConfigToml::default(), @@ -2202,15 +2202,15 @@ fn to_mcp_config_preserves_apps_feature_from_config() -> std::io::Result<()> { )?; let plugins_manager = PluginsManager::new(codex_home.path().to_path_buf()); - let mcp_config = config.to_mcp_config(&plugins_manager); + let mcp_config = config.to_mcp_config(&plugins_manager).await; assert!(mcp_config.apps_enabled); let _ = config.features.disable(Feature::Apps); - let mcp_config = config.to_mcp_config(&plugins_manager); + let mcp_config = config.to_mcp_config(&plugins_manager).await; assert!(!mcp_config.apps_enabled); let _ = config.features.enable(Feature::Apps); - let mcp_config = config.to_mcp_config(&plugins_manager); + let mcp_config = config.to_mcp_config(&plugins_manager).await; assert!(mcp_config.apps_enabled); Ok(()) diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index a5ddd3e195..88987e4166 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -747,8 +747,11 @@ impl Config { } } - pub fn to_mcp_config(&self, plugins_manager: &crate::plugins::PluginsManager) -> McpConfig { - let loaded_plugins = plugins_manager.plugins_for_config(self); + pub async fn to_mcp_config( + &self, + plugins_manager: &crate::plugins::PluginsManager, + ) -> McpConfig { + let loaded_plugins = plugins_manager.plugins_for_config(self).await; let mut configured_mcp_servers = self.mcp_servers.get().clone(); for (name, plugin_server) in loaded_plugins.effective_mcp_servers() { configured_mcp_servers.entry(name).or_insert(plugin_server); diff --git a/codex-rs/core/src/connectors.rs b/codex-rs/core/src/connectors.rs index 14154cffa5..eef6d5747b 100644 --- a/codex-rs/core/src/connectors.rs +++ b/codex-rs/core/src/connectors.rs @@ -124,7 +124,7 @@ pub(crate) async fn list_tool_suggest_discoverable_tools_with_auth( ) -> anyhow::Result> { let directory_connectors = list_directory_connectors_for_tool_suggest_with_auth(config, auth).await?; - let connector_ids = tool_suggest_connector_ids(config); + let connector_ids = tool_suggest_connector_ids(config).await; let discoverable_connectors = filter_tool_suggest_discoverable_connectors( directory_connectors, accessible_connectors, @@ -132,7 +132,8 @@ pub(crate) async fn list_tool_suggest_discoverable_tools_with_auth( ) .into_iter() .map(DiscoverableTool::from); - let discoverable_plugins = list_tool_suggest_discoverable_plugins(config)? + let discoverable_plugins = list_tool_suggest_discoverable_plugins(config) + .await? .into_iter() .map(DiscoverableTool::from); Ok(discoverable_connectors @@ -201,7 +202,7 @@ pub async fn list_accessible_connectors_from_mcp_tools_with_options_and_status( let cache_key = accessible_connectors_cache_key(config, auth.as_ref()); let plugins_manager = Arc::new(PluginsManager::new(config.codex_home.to_path_buf())); let mcp_manager = McpManager::new(Arc::clone(&plugins_manager)); - let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config); + let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config).await; if !force_refetch && let Some(cached_connectors) = read_cached_accessible_connectors(&cache_key) { let cached_connectors = filter_disallowed_connectors(cached_connectors); @@ -212,7 +213,7 @@ pub async fn list_accessible_connectors_from_mcp_tools_with_options_and_status( }); } - let mcp_config = config.to_mcp_config(plugins_manager.as_ref()); + let mcp_config = config.to_mcp_config(plugins_manager.as_ref()).await; let mcp_servers = with_codex_apps_mcp(HashMap::new(), auth.as_ref(), &mcp_config); if mcp_servers.is_empty() { return Ok(AccessibleConnectorsStatus { @@ -395,9 +396,10 @@ fn filter_tool_suggest_discoverable_connectors( connectors } -fn tool_suggest_connector_ids(config: &Config) -> HashSet { +async fn tool_suggest_connector_ids(config: &Config) -> HashSet { let mut connector_ids = PluginsManager::new(config.codex_home.to_path_buf()) .plugins_for_config(config) + .await .capability_summaries() .iter() .flat_map(|plugin| plugin.app_connector_ids.iter()) diff --git a/codex-rs/core/src/connectors_tests.rs b/codex-rs/core/src/connectors_tests.rs index 3c6504111a..c9f77d0469 100644 --- a/codex-rs/core/src/connectors_tests.rs +++ b/codex-rs/core/src/connectors_tests.rs @@ -1037,7 +1037,7 @@ discoverables = [ .expect("config should load"); assert_eq!( - tool_suggest_connector_ids(&config), + tool_suggest_connector_ids(&config).await, HashSet::from(["connector_2128aebfecb84f64a069897515042a44".to_string()]) ); } diff --git a/codex-rs/core/src/mcp.rs b/codex-rs/core/src/mcp.rs index 83becdc07e..0d4c26991d 100644 --- a/codex-rs/core/src/mcp.rs +++ b/codex-rs/core/src/mcp.rs @@ -20,22 +20,22 @@ impl McpManager { Self { plugins_manager } } - pub fn configured_servers(&self, config: &Config) -> HashMap { - let mcp_config = config.to_mcp_config(self.plugins_manager.as_ref()); + pub async fn configured_servers(&self, config: &Config) -> HashMap { + let mcp_config = config.to_mcp_config(self.plugins_manager.as_ref()).await; configured_mcp_servers(&mcp_config) } - pub fn effective_servers( + pub async fn effective_servers( &self, config: &Config, auth: Option<&CodexAuth>, ) -> HashMap { - let mcp_config = config.to_mcp_config(self.plugins_manager.as_ref()); + let mcp_config = config.to_mcp_config(self.plugins_manager.as_ref()).await; effective_mcp_servers(&mcp_config, auth) } - pub fn tool_plugin_provenance(&self, config: &Config) -> ToolPluginProvenance { - let mcp_config = config.to_mcp_config(self.plugins_manager.as_ref()); + pub async fn tool_plugin_provenance(&self, config: &Config) -> ToolPluginProvenance { + let mcp_config = config.to_mcp_config(self.plugins_manager.as_ref()).await; collect_tool_plugin_provenance(&mcp_config) } } diff --git a/codex-rs/core/src/mcp_skill_dependencies.rs b/codex-rs/core/src/mcp_skill_dependencies.rs index 536c4eb4b8..6cdd3cf084 100644 --- a/codex-rs/core/src/mcp_skill_dependencies.rs +++ b/codex-rs/core/src/mcp_skill_dependencies.rs @@ -53,7 +53,8 @@ pub(crate) async fn maybe_prompt_and_install_mcp_dependencies( let installed = sess .services .mcp_manager - .configured_servers(config.as_ref()); + .configured_servers(config.as_ref()) + .await; let missing = collect_missing_mcp_dependencies(mentioned_skills, &installed); if missing.is_empty() { return; @@ -86,7 +87,7 @@ pub(crate) async fn maybe_install_mcp_dependencies( } let codex_home = config.codex_home.clone(); - let installed = sess.services.mcp_manager.configured_servers(config); + let installed = sess.services.mcp_manager.configured_servers(config).await; let missing = collect_missing_mcp_dependencies(mentioned_skills, &installed); if missing.is_empty() { return; @@ -197,7 +198,8 @@ pub(crate) async fn maybe_install_mcp_dependencies( let mut refresh_servers = sess .services .mcp_manager - .effective_servers(config, auth.as_ref()); + .effective_servers(config, auth.as_ref()) + .await; for (name, server_config) in &servers { refresh_servers .entry(name.clone()) diff --git a/codex-rs/core/src/plugins/discoverable.rs b/codex-rs/core/src/plugins/discoverable.rs index 91d564becd..e856815a7c 100644 --- a/codex-rs/core/src/plugins/discoverable.rs +++ b/codex-rs/core/src/plugins/discoverable.rs @@ -21,7 +21,7 @@ const TOOL_SUGGEST_DISCOVERABLE_PLUGIN_ALLOWLIST: &[&str] = &[ "figma@openai-curated", ]; -pub(crate) fn list_tool_suggest_discoverable_plugins( +pub(crate) async fn list_tool_suggest_discoverable_plugins( config: &Config, ) -> anyhow::Result> { if !config.features.enabled(Feature::Plugins) { @@ -59,11 +59,10 @@ pub(crate) fn list_tool_suggest_discoverable_plugins( let plugin_id = plugin.id.clone(); - match plugins_manager.read_plugin_detail_for_marketplace_plugin( - config, - &curated_marketplace_name, - plugin, - ) { + match plugins_manager + .read_plugin_detail_for_marketplace_plugin(config, &curated_marketplace_name, plugin) + .await + { Ok(plugin) => { let plugin: PluginCapabilitySummary = plugin.into(); discoverable_plugins.push(DiscoverablePluginInfo { diff --git a/codex-rs/core/src/plugins/discoverable_tests.rs b/codex-rs/core/src/plugins/discoverable_tests.rs index f17c897fe9..b1d0d79e2f 100644 --- a/codex-rs/core/src/plugins/discoverable_tests.rs +++ b/codex-rs/core/src/plugins/discoverable_tests.rs @@ -21,7 +21,9 @@ async fn list_tool_suggest_discoverable_plugins_returns_uninstalled_curated_plug write_plugins_feature_config(codex_home.path()); let config = load_plugins_config(codex_home.path()).await; - let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config).unwrap(); + let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config) + .await + .unwrap(); assert_eq!( discoverable_plugins, @@ -51,7 +53,9 @@ plugins = false ); let config = load_plugins_config(codex_home.path()).await; - let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config).unwrap(); + let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config) + .await + .unwrap(); assert_eq!(discoverable_plugins, Vec::::new()); } @@ -71,7 +75,9 @@ async fn list_tool_suggest_discoverable_plugins_normalizes_description() { ); let config = load_plugins_config(codex_home.path()).await; - let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config).unwrap(); + let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config) + .await + .unwrap(); assert_eq!( discoverable_plugins, @@ -106,7 +112,9 @@ async fn list_tool_suggest_discoverable_plugins_omits_installed_curated_plugins( .expect("plugin should install"); let refreshed_config = load_plugins_config(codex_home.path()).await; - let discoverable_plugins = list_tool_suggest_discoverable_plugins(&refreshed_config).unwrap(); + let discoverable_plugins = list_tool_suggest_discoverable_plugins(&refreshed_config) + .await + .unwrap(); assert_eq!(discoverable_plugins, Vec::::new()); } @@ -127,7 +135,9 @@ discoverables = [{ type = "plugin", id = "sample@openai-curated" }] ); let config = load_plugins_config(codex_home.path()).await; - let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config).unwrap(); + let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config) + .await + .unwrap(); assert_eq!( discoverable_plugins, @@ -182,7 +192,9 @@ async fn list_tool_suggest_discoverable_plugins_does_not_reload_marketplace_per_ .finish(); let _guard = tracing::subscriber::set_default(subscriber); - let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config).unwrap(); + let discoverable_plugins = list_tool_suggest_discoverable_plugins(&config) + .await + .unwrap(); assert_eq!(discoverable_plugins.len(), 1); assert_eq!(discoverable_plugins[0].id, "slack@openai-curated"); diff --git a/codex-rs/core/src/plugins/manager.rs b/codex-rs/core/src/plugins/manager.rs index bfae2edf28..4f6090fd76 100644 --- a/codex-rs/core/src/plugins/manager.rs +++ b/codex-rs/core/src/plugins/manager.rs @@ -374,11 +374,12 @@ impl PluginsManager { } } - pub fn plugins_for_config(&self, config: &Config) -> PluginLoadOutcome { + pub async fn plugins_for_config(&self, config: &Config) -> PluginLoadOutcome { self.plugins_for_config_with_force_reload(config, /*force_reload*/ false) + .await } - pub(crate) fn plugins_for_config_with_force_reload( + pub(crate) async fn plugins_for_config_with_force_reload( &self, config: &Config, force_reload: bool, @@ -395,7 +396,8 @@ impl PluginsManager { &config.config_layer_stack, &self.store, self.restriction_product, - ); + ) + .await; log_plugin_load_errors(&outcome); let mut cache = match self.cached_enabled_outcome.write() { Ok(cache) => cache, @@ -419,7 +421,7 @@ impl PluginsManager { } /// Resolve plugin skill roots for a config layer stack without touching the plugins cache. - pub fn effective_skill_roots_for_layer_stack( + pub async fn effective_skill_roots_for_layer_stack( &self, config_layer_stack: &ConfigLayerStack, plugins_feature_enabled: bool, @@ -428,6 +430,7 @@ impl PluginsManager { return Vec::new(); } load_plugins_from_layer_stack(config_layer_stack, &self.store, self.restriction_product) + .await .effective_skill_roots() } @@ -585,10 +588,10 @@ impl PluginsManager { Err(err) => err.into_inner().clone(), }; if let Some(analytics_events_client) = analytics_events_client { - analytics_events_client.track_plugin_installed(plugin_telemetry_metadata_from_root( - &result.plugin_id, - &result.installed_path, - )); + analytics_events_client.track_plugin_installed( + plugin_telemetry_metadata_from_root(&result.plugin_id, &result.installed_path) + .await, + ); } Ok(PluginInstallOutcome { @@ -622,10 +625,11 @@ impl PluginsManager { } async fn uninstall_plugin_id(&self, plugin_id: PluginId) -> Result<(), PluginUninstallError> { - let plugin_telemetry = self - .store - .active_plugin_root(&plugin_id) - .map(|_| installed_plugin_telemetry_metadata(self.codex_home.as_path(), &plugin_id)); + let plugin_telemetry = if self.store.active_plugin_root(&plugin_id).is_some() { + Some(installed_plugin_telemetry_metadata(self.codex_home.as_path(), &plugin_id).await) + } else { + None + }; let store = self.store.clone(); let plugin_id_for_store = plugin_id.clone(); tokio::task::spawn_blocking(move || store.uninstall(&plugin_id_for_store)) @@ -931,7 +935,7 @@ impl PluginsManager { }) } - pub fn read_plugin_for_config( + pub async fn read_plugin_for_config( &self, config: &Config, request: &PluginReadRequest, @@ -959,19 +963,21 @@ impl PluginsManager { )?; let plugin_key = plugin_id.as_key(); let (installed_plugins, enabled_plugins) = self.configured_plugin_states(config); - let plugin = self.read_plugin_detail_for_marketplace_plugin( - config, - &marketplace.name, - ConfiguredMarketplacePlugin { - id: plugin_key.clone(), - name: plugin.name, - source: plugin.source, - policy: plugin.policy, - interface: plugin.interface, - installed: installed_plugins.contains(&plugin_key), - enabled: enabled_plugins.contains(&plugin_key), - }, - )?; + let plugin = self + .read_plugin_detail_for_marketplace_plugin( + config, + &marketplace.name, + ConfiguredMarketplacePlugin { + id: plugin_key.clone(), + name: plugin.name, + source: plugin.source, + policy: plugin.policy, + interface: plugin.interface, + installed: installed_plugins.contains(&plugin_key), + enabled: enabled_plugins.contains(&plugin_key), + }, + ) + .await?; Ok(PluginReadOutcome { marketplace_name: if marketplace.name == OPENAI_CURATED_MARKETPLACE_NAME { @@ -984,7 +990,7 @@ impl PluginsManager { }) } - pub(crate) fn read_plugin_detail_for_marketplace_plugin( + pub(crate) async fn read_plugin_detail_for_marketplace_plugin( &self, config: &Config, marketplace_name: &str, @@ -1026,12 +1032,17 @@ impl PluginsManager { self.restriction_product, &skill_config_rules, ); - let apps = load_plugin_apps(source_path.as_path()); + let apps = load_apps_from_paths( + source_path.as_path(), + plugin_app_config_paths(source_path.as_path(), manifest_paths), + ) + .await; let mcp_config_paths = plugin_mcp_config_paths(source_path.as_path(), manifest_paths); let mut mcp_server_names = Vec::new(); for mcp_config_path in mcp_config_paths { mcp_server_names.extend( load_mcp_servers_from_file(source_path.as_path(), &mcp_config_path) + .await .mcp_servers .into_keys(), ); @@ -1374,7 +1385,7 @@ struct PluginAppConfig { id: String, } -pub(crate) fn load_plugins_from_layer_stack( +pub(crate) async fn load_plugins_from_layer_stack( config_layer_stack: &ConfigLayerStack, store: &PluginStore, restriction_product: Option, @@ -1394,7 +1405,8 @@ pub(crate) fn load_plugins_from_layer_stack( store, restriction_product, &skill_config_rules, - ); + ) + .await; for name in loaded_plugin.mcp_servers.keys() { if let Some(previous_plugin) = seen_mcp_server_names.insert(name.clone(), configured_name.clone()) @@ -1663,7 +1675,7 @@ fn non_curated_plugin_ids_from_config_keys( configured_non_curated_plugin_ids } -fn load_plugin( +async fn load_plugin( config_name: String, plugin: &PluginConfig, store: &PluginStore, @@ -1745,7 +1757,7 @@ fn load_plugin( loaded_plugin.has_enabled_skills = has_enabled_skills; let mut mcp_servers = HashMap::new(); for mcp_config_path in plugin_mcp_config_paths(plugin_root.as_path(), manifest_paths) { - let plugin_mcp = load_mcp_servers_from_file(plugin_root.as_path(), &mcp_config_path); + let plugin_mcp = load_mcp_servers_from_file(plugin_root.as_path(), &mcp_config_path).await; for (name, config) in plugin_mcp.mcp_servers { if mcp_servers.insert(name.clone(), config).is_some() { warn!( @@ -1758,7 +1770,11 @@ fn load_plugin( } } loaded_plugin.mcp_servers = mcp_servers; - loaded_plugin.apps = load_plugin_apps(plugin_root.as_path()); + loaded_plugin.apps = load_apps_from_paths( + plugin_root.as_path(), + plugin_app_config_paths(plugin_root.as_path(), manifest_paths), + ) + .await; loaded_plugin } @@ -1853,14 +1869,15 @@ fn default_mcp_config_paths(plugin_root: &Path) -> Vec { paths } -pub fn load_plugin_apps(plugin_root: &Path) -> Vec { +pub async fn load_plugin_apps(plugin_root: &Path) -> Vec { if let Some(manifest) = load_plugin_manifest(plugin_root) { return load_apps_from_paths( plugin_root, plugin_app_config_paths(plugin_root, &manifest.paths), - ); + ) + .await; } - load_apps_from_paths(plugin_root, default_app_config_paths(plugin_root)) + load_apps_from_paths(plugin_root, default_app_config_paths(plugin_root)).await } fn plugin_app_config_paths( @@ -1886,13 +1903,13 @@ fn default_app_config_paths(plugin_root: &Path) -> Vec { paths } -fn load_apps_from_paths( +async fn load_apps_from_paths( plugin_root: &Path, app_config_paths: Vec, ) -> Vec { let mut connector_ids = Vec::new(); for app_config_path in app_config_paths { - let Ok(contents) = fs::read_to_string(app_config_path.as_path()) else { + let Ok(contents) = tokio::fs::read_to_string(app_config_path.as_path()).await else { continue; }; let parsed = match serde_json::from_str::(&contents) { @@ -1925,7 +1942,7 @@ fn load_apps_from_paths( connector_ids } -pub fn plugin_telemetry_metadata_from_root( +pub async fn plugin_telemetry_metadata_from_root( plugin_id: &PluginId, plugin_root: &AbsolutePathBuf, ) -> PluginTelemetryMetadata { @@ -1939,6 +1956,7 @@ pub fn plugin_telemetry_metadata_from_root( for path in plugin_mcp_config_paths(plugin_root.as_path(), manifest_paths) { mcp_server_names.extend( load_mcp_servers_from_file(plugin_root.as_path(), &path) + .await .mcp_servers .into_keys(), ); @@ -1954,19 +1972,23 @@ pub fn plugin_telemetry_metadata_from_root( description: None, has_skills, mcp_server_names, - app_connector_ids: load_plugin_apps(plugin_root.as_path()), + app_connector_ids: load_apps_from_paths( + plugin_root.as_path(), + plugin_app_config_paths(plugin_root.as_path(), manifest_paths), + ) + .await, }), } } -pub fn load_plugin_mcp_servers(plugin_root: &Path) -> HashMap { +pub async fn load_plugin_mcp_servers(plugin_root: &Path) -> HashMap { let Some(manifest) = load_plugin_manifest(plugin_root) else { return HashMap::new(); }; let mut mcp_servers = HashMap::new(); for mcp_config_path in plugin_mcp_config_paths(plugin_root, &manifest.paths) { - let plugin_mcp = load_mcp_servers_from_file(plugin_root, &mcp_config_path); + let plugin_mcp = load_mcp_servers_from_file(plugin_root, &mcp_config_path).await; for (name, config) in plugin_mcp.mcp_servers { mcp_servers.entry(name).or_insert(config); } @@ -1975,7 +1997,7 @@ pub fn load_plugin_mcp_servers(plugin_root: &Path) -> HashMap PluginTelemetryMetadata { @@ -1984,14 +2006,14 @@ pub fn installed_plugin_telemetry_metadata( return PluginTelemetryMetadata::from_plugin_id(plugin_id); }; - plugin_telemetry_metadata_from_root(plugin_id, &plugin_root) + plugin_telemetry_metadata_from_root(plugin_id, &plugin_root).await } -fn load_mcp_servers_from_file( +async fn load_mcp_servers_from_file( plugin_root: &Path, mcp_config_path: &AbsolutePathBuf, ) -> PluginMcpDiscovery { - let Ok(contents) = fs::read_to_string(mcp_config_path.as_path()) else { + let Ok(contents) = tokio::fs::read_to_string(mcp_config_path.as_path()).await else { return PluginMcpDiscovery::default(); }; let parsed = match serde_json::from_str::(&contents) { diff --git a/codex-rs/core/src/plugins/manager_tests.rs b/codex-rs/core/src/plugins/manager_tests.rs index 3b06a08a9f..23b202f1a8 100644 --- a/codex-rs/core/src/plugins/manager_tests.rs +++ b/codex-rs/core/src/plugins/manager_tests.rs @@ -82,10 +82,12 @@ fn plugin_config_toml(enabled: bool, plugins_feature_enabled: bool) -> String { toml::to_string(&Value::Table(root)).expect("plugin test config should serialize") } -fn load_plugins_from_config(config_toml: &str, codex_home: &Path) -> PluginLoadOutcome { +async fn load_plugins_from_config(config_toml: &str, codex_home: &Path) -> PluginLoadOutcome { write_file(&codex_home.join(CONFIG_TOML_FILE), config_toml); - let config = load_config_blocking(codex_home, codex_home); - PluginsManager::new(codex_home.to_path_buf()).plugins_for_config(&config) + let config = load_config(codex_home, codex_home).await; + PluginsManager::new(codex_home.to_path_buf()) + .plugins_for_config(&config) + .await } async fn load_config(codex_home: &Path, cwd: &Path) -> crate::config::Config { @@ -97,16 +99,8 @@ async fn load_config(codex_home: &Path, cwd: &Path) -> crate::config::Config { .expect("config should load") } -fn load_config_blocking(codex_home: &Path, cwd: &Path) -> crate::config::Config { - tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .expect("tokio runtime should build") - .block_on(load_config(codex_home, cwd)) -} - -#[test] -fn load_plugins_loads_default_skills_and_mcp_servers() { +#[tokio::test] +async fn load_plugins_loads_default_skills_and_mcp_servers() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -153,7 +147,8 @@ fn load_plugins_loads_default_skills_and_mcp_servers() { let outcome = load_plugins_from_config( &plugin_config_toml(/*enabled*/ true, /*plugins_feature_enabled*/ true), codex_home.path(), - ); + ) + .await; assert_eq!( outcome.plugins(), @@ -216,8 +211,8 @@ fn load_plugins_loads_default_skills_and_mcp_servers() { ); } -#[test] -fn load_plugins_resolves_disabled_skill_names_against_loaded_plugin_skills() { +#[tokio::test] +async fn load_plugins_resolves_disabled_skill_names_against_loaded_plugin_skills() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -244,7 +239,7 @@ enabled = false [plugins."sample@test"] enabled = true "#; - let outcome = load_plugins_from_config(config_toml, codex_home.path()); + let outcome = load_plugins_from_config(config_toml, codex_home.path()).await; let skill_path = dunce::canonicalize(skill_path) .expect("skill path should canonicalize") .abs(); @@ -257,8 +252,8 @@ enabled = true assert!(outcome.capability_summaries().is_empty()); } -#[test] -fn load_plugins_ignores_unknown_disabled_skill_names() { +#[tokio::test] +async fn load_plugins_ignores_unknown_disabled_skill_names() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -284,7 +279,7 @@ enabled = false [plugins."sample@test"] enabled = true "#; - let outcome = load_plugins_from_config(config_toml, codex_home.path()); + let outcome = load_plugins_from_config(config_toml, codex_home.path()).await; assert!(outcome.plugins()[0].disabled_skill_paths.is_empty()); assert!(outcome.plugins()[0].has_enabled_skills); @@ -301,8 +296,8 @@ enabled = true ); } -#[test] -fn plugin_telemetry_metadata_uses_default_mcp_config_path() { +#[tokio::test] +async fn plugin_telemetry_metadata_uses_default_mcp_config_path() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -330,7 +325,8 @@ fn plugin_telemetry_metadata_uses_default_mcp_config_path() { let metadata = plugin_telemetry_metadata_from_root( &PluginId::parse("sample@test").expect("plugin id should parse"), &plugin_root.abs(), - ); + ) + .await; assert_eq!( metadata.capability_summary, @@ -345,8 +341,8 @@ fn plugin_telemetry_metadata_uses_default_mcp_config_path() { ); } -#[test] -fn capability_summary_sanitizes_plugin_descriptions_to_one_line() { +#[tokio::test] +async fn capability_summary_sanitizes_plugin_descriptions_to_one_line() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -368,7 +364,8 @@ fn capability_summary_sanitizes_plugin_descriptions_to_one_line() { let outcome = load_plugins_from_config( &plugin_config_toml(/*enabled*/ true, /*plugins_feature_enabled*/ true), codex_home.path(), - ); + ) + .await; assert_eq!( outcome.plugins()[0].manifest_description.as_deref(), @@ -380,8 +377,8 @@ fn capability_summary_sanitizes_plugin_descriptions_to_one_line() { ); } -#[test] -fn capability_summary_truncates_overlong_plugin_descriptions() { +#[tokio::test] +async fn capability_summary_truncates_overlong_plugin_descriptions() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -406,7 +403,8 @@ fn capability_summary_truncates_overlong_plugin_descriptions() { let outcome = load_plugins_from_config( &plugin_config_toml(/*enabled*/ true, /*plugins_feature_enabled*/ true), codex_home.path(), - ); + ) + .await; assert_eq!( outcome.plugins()[0].manifest_description.as_deref(), @@ -418,8 +416,8 @@ fn capability_summary_truncates_overlong_plugin_descriptions() { ); } -#[test] -fn load_plugins_uses_manifest_configured_component_paths() { +#[tokio::test] +async fn load_plugins_uses_manifest_configured_component_paths() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -489,7 +487,8 @@ fn load_plugins_uses_manifest_configured_component_paths() { let outcome = load_plugins_from_config( &plugin_config_toml(/*enabled*/ true, /*plugins_feature_enabled*/ true), codex_home.path(), - ); + ) + .await; assert_eq!( outcome.plugins()[0].skill_roots, @@ -529,8 +528,8 @@ fn load_plugins_uses_manifest_configured_component_paths() { ); } -#[test] -fn load_plugins_ignores_manifest_component_paths_without_dot_slash() { +#[tokio::test] +async fn load_plugins_ignores_manifest_component_paths_without_dot_slash() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -600,7 +599,8 @@ fn load_plugins_ignores_manifest_component_paths_without_dot_slash() { let outcome = load_plugins_from_config( &plugin_config_toml(/*enabled*/ true, /*plugins_feature_enabled*/ true), codex_home.path(), - ); + ) + .await; assert_eq!( outcome.plugins()[0].skill_roots, @@ -637,8 +637,8 @@ fn load_plugins_ignores_manifest_component_paths_without_dot_slash() { ); } -#[test] -fn load_plugins_preserves_disabled_plugins_without_effective_contributions() { +#[tokio::test] +async fn load_plugins_preserves_disabled_plugins_without_effective_contributions() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -666,7 +666,8 @@ fn load_plugins_preserves_disabled_plugins_without_effective_contributions() { /*enabled*/ false, /*plugins_feature_enabled*/ true, ), codex_home.path(), - ); + ) + .await; assert_eq!( outcome.plugins(), @@ -688,8 +689,8 @@ fn load_plugins_preserves_disabled_plugins_without_effective_contributions() { assert!(outcome.effective_mcp_servers().is_empty()); } -#[test] -fn effective_apps_dedupes_connector_ids_across_plugins() { +#[tokio::test] +async fn effective_apps_dedupes_connector_ids_across_plugins() { let codex_home = TempDir::new().unwrap(); let plugin_a_root = codex_home .path() @@ -751,7 +752,7 @@ fn effective_apps_dedupes_connector_ids_across_plugins() { let config_toml = toml::to_string(&Value::Table(root)).expect("plugin test config should serialize"); - let outcome = load_plugins_from_config(&config_toml, codex_home.path()); + let outcome = load_plugins_from_config(&config_toml, codex_home.path()).await; assert_eq!( outcome.effective_apps(), @@ -858,8 +859,8 @@ fn capability_index_filters_inactive_and_zero_capability_plugins() { ); } -#[test] -fn load_plugins_returns_empty_when_feature_disabled() { +#[tokio::test] +async fn load_plugins_returns_empty_when_feature_disabled() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -881,14 +882,16 @@ fn load_plugins_returns_empty_when_feature_disabled() { ), ); - let config = load_config_blocking(codex_home.path(), codex_home.path()); - let outcome = PluginsManager::new(codex_home.path().to_path_buf()).plugins_for_config(&config); + let config = load_config(codex_home.path(), codex_home.path()).await; + let outcome = PluginsManager::new(codex_home.path().to_path_buf()) + .plugins_for_config(&config) + .await; assert_eq!(outcome, PluginLoadOutcome::default()); } -#[test] -fn load_plugins_rejects_invalid_plugin_keys() { +#[tokio::test] +async fn load_plugins_rejects_invalid_plugin_keys() { let codex_home = TempDir::new().unwrap(); let plugin_root = codex_home .path() @@ -915,7 +918,8 @@ fn load_plugins_rejects_invalid_plugin_keys() { let outcome = load_plugins_from_config( &toml::to_string(&Value::Table(root)).expect("plugin test config should serialize"), codex_home.path(), - ); + ) + .await; assert_eq!(outcome.plugins().len(), 1); assert_eq!( @@ -1346,6 +1350,7 @@ enabled = true marketplace_path, }, ) + .await .unwrap_err(); assert!(matches!(err, MarketplaceError::PluginsDisabled)); @@ -1410,6 +1415,7 @@ enabled = false .unwrap(), }, ) + .await .unwrap(); assert!(outcome.plugin.disabled_skill_paths.is_empty()); @@ -2727,8 +2733,8 @@ enabled = true ); } -#[test] -fn load_plugins_ignores_project_config_files() { +#[tokio::test] +async fn load_plugins_ignores_project_config_files() { let codex_home = TempDir::new().unwrap(); let project_root = codex_home.path().join("project"); let plugin_root = codex_home @@ -2764,7 +2770,8 @@ fn load_plugins_ignores_project_config_files() { &stack, &PluginStore::new(codex_home.path().to_path_buf()), Some(Product::Codex), - ); + ) + .await; assert_eq!(outcome, PluginLoadOutcome::default()); } diff --git a/codex-rs/core/src/skills_watcher.rs b/codex-rs/core/src/skills_watcher.rs index 07f3d1ebf0..dd7d21cfce 100644 --- a/codex-rs/core/src/skills_watcher.rs +++ b/codex-rs/core/src/skills_watcher.rs @@ -54,13 +54,13 @@ impl SkillsWatcher { self.tx.subscribe() } - pub(crate) fn register_config( + pub(crate) async fn register_config( &self, config: &Config, skills_manager: &SkillsManager, plugins_manager: &PluginsManager, ) -> WatchRegistration { - let plugin_outcome = plugins_manager.plugins_for_config(config); + let plugin_outcome = plugins_manager.plugins_for_config(config).await; let effective_skill_roots = plugin_outcome.effective_skill_roots(); let skills_input = skills_load_input_from_config(config, effective_skill_roots); let roots = skills_manager diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index b4658e81d0..06aa1bc7b0 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -903,11 +903,14 @@ impl ThreadManagerState { parent_trace: Option, user_shell_override: Option, ) -> CodexResult { - let watch_registration = self.skills_watcher.register_config( - &config, - self.skills_manager.as_ref(), - self.plugins_manager.as_ref(), - ); + let watch_registration = self + .skills_watcher + .register_config( + &config, + self.skills_manager.as_ref(), + self.plugins_manager.as_ref(), + ) + .await; let CodexSpawnOk { codex, thread_id, .. } = Codex::spawn(CodexSpawnArgs { diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index a1f813e9b6..ba8de0bacd 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -42,6 +42,7 @@ use crate::legacy_core::config::edit::ConfigEdit; use crate::legacy_core::config::edit::ConfigEditsBuilder; use crate::legacy_core::config_loader::ConfigLayerStackOrdering; use crate::legacy_core::lookup_message_history_entry; +use crate::legacy_core::plugins::PluginsManager; #[cfg(target_os = "windows")] use crate::legacy_core::windows_sandbox::WindowsSandboxLevelExt; use crate::model_catalog::ModelCatalog; @@ -2017,6 +2018,26 @@ impl App { }); } + fn refresh_plugin_mentions(&mut self) { + let config = self.config.clone(); + let app_event_tx = self.app_event_tx.clone(); + if !config.features.enabled(Feature::Plugins) { + app_event_tx.send(AppEvent::PluginMentionsLoaded { plugins: None }); + return; + } + + tokio::spawn(async move { + let plugins = PluginsManager::new(config.codex_home.to_path_buf()) + .plugins_for_config(&config) + .await + .capability_summaries() + .to_vec(); + app_event_tx.send(AppEvent::PluginMentionsLoaded { + plugins: Some(plugins), + }); + }); + } + fn submit_feedback( &mut self, app_server: &AppServerSession, @@ -5044,6 +5065,15 @@ impl App { self.fetch_plugins_list(app_server, cwd); } } + AppEvent::RefreshPluginMentions => { + self.refresh_plugin_mentions(); + } + AppEvent::PluginMentionsLoaded { mut plugins } => { + if !self.config.features.enabled(Feature::Plugins) { + plugins = None; + } + self.chat_widget.on_plugin_mentions_loaded(plugins); + } AppEvent::PersistPersonalitySelection { personality } => { let profile = self.active_profile.as_deref(); match ConfigEditsBuilder::new(&self.config.codex_home) diff --git a/codex-rs/tui/src/app_event.rs b/codex-rs/tui/src/app_event.rs index f448a59886..91f00b99b0 100644 --- a/codex-rs/tui/src/app_event.rs +++ b/codex-rs/tui/src/app_event.rs @@ -30,6 +30,7 @@ use crate::bottom_pane::ApprovalRequest; use crate::bottom_pane::StatusLineItem; use crate::bottom_pane::TerminalTitleItem; use crate::history_cell::HistoryCell; +use crate::legacy_core::plugins::PluginCapabilitySummary; use codex_config::types::ApprovalsReviewer; use codex_features::Feature; @@ -270,6 +271,14 @@ pub(crate) enum AppEvent { result: Result, }, + /// Refresh plugin mention bindings from the current config. + RefreshPluginMentions, + + /// Result of refreshing plugin mention bindings. + PluginMentionsLoaded { + plugins: Option>, + }, + /// Advance the post-install plugin app-auth flow. PluginInstallAuthAdvance { refresh_connectors: bool, diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 6c96e1598b..7ae7bbc6c5 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -62,7 +62,6 @@ use crate::legacy_core::config::Constrained; use crate::legacy_core::config::ConstraintResult; use crate::legacy_core::config_loader::ConfigLayerStackOrdering; use crate::legacy_core::find_thread_name_by_id; -use crate::legacy_core::plugins::PluginsManager; use crate::legacy_core::skills::model::SkillMetadata; #[cfg(target_os = "windows")] use crate::legacy_core::windows_sandbox::WindowsSandboxLevelExt; @@ -10369,11 +10368,14 @@ impl ChatWidget { return; } - let plugins = PluginsManager::new(self.config.codex_home.to_path_buf()) - .plugins_for_config(&self.config) - .capability_summaries() - .to_vec(); - self.bottom_pane.set_plugin_mentions(Some(plugins)); + self.app_event_tx.send(AppEvent::RefreshPluginMentions); + } + + pub(crate) fn on_plugin_mentions_loaded( + &mut self, + plugins: Option>, + ) { + self.bottom_pane.set_plugin_mentions(plugins); } pub(crate) fn sync_plugin_mentions_config(&mut self, config: &Config) { diff --git a/codex-rs/tui/src/history_cell.rs b/codex-rs/tui/src/history_cell.rs index 89869a6057..b87dd4ff81 100644 --- a/codex-rs/tui/src/history_cell.rs +++ b/codex-rs/tui/src/history_cell.rs @@ -19,11 +19,7 @@ use crate::exec_cell::output_lines; use crate::exec_cell::spinner; use crate::exec_command::relativize_to_home; use crate::exec_command::strip_bash_lc_and_escape; -#[cfg(test)] -use crate::legacy_core::McpManager; use crate::legacy_core::config::Config; -#[cfg(test)] -use crate::legacy_core::plugins::PluginsManager; use crate::legacy_core::web_search_detail; use crate::live_wrap::take_prefix_by_width; use crate::markdown::append_markdown; @@ -88,8 +84,6 @@ use std::collections::HashMap; use std::io::Cursor; use std::path::Path; use std::path::PathBuf; -#[cfg(test)] -use std::sync::Arc; use std::time::Duration; use std::time::Instant; use tracing::error; @@ -1877,10 +1871,7 @@ pub(crate) fn new_mcp_tools_output( lines.push("".into()); } - let mcp_manager = McpManager::new(Arc::new(PluginsManager::new( - config.codex_home.to_path_buf(), - ))); - let effective_servers = mcp_manager.effective_servers(config, /*auth*/ None); + let effective_servers = config.mcp_servers.get().clone(); let mut servers: Vec<_> = effective_servers.iter().collect(); servers.sort_by(|(a, _), (b, _)| a.cmp(b)); From ad37389c18afbe58374936731956cd9cdf24a5a8 Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Mon, 13 Apr 2026 22:01:58 -0700 Subject: [PATCH 02/16] [codex] Initialize ICU data for code mode V8 (#17709) Link ICU data into code mode, otherwise locale-dependent methods cause a panic and a crash. --- MODULE.bazel.lock | 1 + codex-rs/Cargo.lock | 7 +++ codex-rs/Cargo.toml | 1 + codex-rs/code-mode/Cargo.toml | 1 + codex-rs/code-mode/src/runtime/mod.rs | 19 ++++--- codex-rs/code-mode/src/service.rs | 79 +++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 7 deletions(-) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 2dfb03c3de..c270e6a9fa 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -782,6 +782,7 @@ "debugid_0.8.0": "{\"dependencies\":[{\"name\":\"serde\",\"optional\":true,\"req\":\"^1.0.85\"},{\"kind\":\"dev\",\"name\":\"serde_json\",\"req\":\"^1.0.37\"},{\"name\":\"uuid\",\"req\":\"^1.0.0\"}],\"features\":{}}", "debugserver-types_0.5.0": "{\"dependencies\":[{\"name\":\"schemafy\",\"req\":\"^0.5.0\"},{\"features\":[\"derive\"],\"name\":\"serde\",\"req\":\"^1.0\"},{\"name\":\"serde_json\",\"req\":\"^1.0\"}],\"features\":{}}", "deflate64_0.1.10": "{\"dependencies\":[{\"features\":[\"derive\"],\"kind\":\"dev\",\"name\":\"bytemuck\",\"req\":\"^1.13.1\"},{\"kind\":\"dev\",\"name\":\"proptest\",\"req\":\"^1.2.0\"},{\"kind\":\"dev\",\"name\":\"tempfile\",\"req\":\"^3.7.1\"}],\"features\":{}}", + "deno_core_icudata_0.77.0": "{\"dependencies\":[],\"features\":{}}", "der-parser_10.0.0": "{\"dependencies\":[{\"name\":\"asn1-rs\",\"req\":\"^0.7\"},{\"name\":\"bitvec\",\"optional\":true,\"req\":\"^1.0\"},{\"name\":\"cookie-factory\",\"optional\":true,\"req\":\"^0.3.0\"},{\"default_features\":false,\"name\":\"displaydoc\",\"req\":\"^0.2\"},{\"kind\":\"dev\",\"name\":\"hex-literal\",\"req\":\"^0.4\"},{\"name\":\"nom\",\"req\":\"^7.0\"},{\"name\":\"num-bigint\",\"optional\":true,\"req\":\"^0.4\"},{\"name\":\"num-traits\",\"req\":\"^0.2\"},{\"kind\":\"dev\",\"name\":\"pretty_assertions\",\"req\":\"^1.0\"},{\"name\":\"rusticata-macros\",\"req\":\"^4.0\"},{\"kind\":\"dev\",\"name\":\"test-case\",\"req\":\"^3.0\"}],\"features\":{\"as_bitvec\":[\"bitvec\"],\"bigint\":[\"num-bigint\"],\"default\":[\"std\"],\"serialize\":[\"std\",\"cookie-factory\"],\"std\":[],\"unstable\":[]}}", "der_0.7.10": "{\"dependencies\":[{\"features\":[\"derive\"],\"name\":\"arbitrary\",\"optional\":true,\"req\":\"^1.3\"},{\"default_features\":false,\"name\":\"bytes\",\"optional\":true,\"req\":\"^1\"},{\"name\":\"const-oid\",\"optional\":true,\"req\":\"^0.9.2\"},{\"name\":\"der_derive\",\"optional\":true,\"req\":\"^0.7.2\"},{\"name\":\"flagset\",\"optional\":true,\"req\":\"^0.4.3\"},{\"kind\":\"dev\",\"name\":\"hex-literal\",\"req\":\"^0.4.1\"},{\"features\":[\"alloc\"],\"name\":\"pem-rfc7468\",\"optional\":true,\"req\":\"^0.7\"},{\"kind\":\"dev\",\"name\":\"proptest\",\"req\":\"^1\"},{\"default_features\":false,\"name\":\"time\",\"optional\":true,\"req\":\"^0.3.4\"},{\"default_features\":false,\"name\":\"zeroize\",\"optional\":true,\"req\":\"^1.5\"}],\"features\":{\"alloc\":[\"zeroize?/alloc\"],\"arbitrary\":[\"dep:arbitrary\",\"const-oid?/arbitrary\",\"std\"],\"bytes\":[\"dep:bytes\",\"alloc\"],\"derive\":[\"dep:der_derive\"],\"oid\":[\"dep:const-oid\"],\"pem\":[\"dep:pem-rfc7468\",\"alloc\",\"zeroize\"],\"real\":[],\"std\":[\"alloc\"]}}", "deranged_0.5.5": "{\"dependencies\":[{\"name\":\"deranged-macros\",\"optional\":true,\"req\":\"=0.3.0\"},{\"default_features\":false,\"name\":\"num-traits\",\"optional\":true,\"req\":\"^0.2.15\"},{\"default_features\":false,\"name\":\"powerfmt\",\"optional\":true,\"req\":\"^0.2.0\"},{\"default_features\":false,\"name\":\"quickcheck\",\"optional\":true,\"req\":\"^1.0.3\"},{\"default_features\":false,\"name\":\"rand08\",\"optional\":true,\"package\":\"rand\",\"req\":\"^0.8.4\"},{\"kind\":\"dev\",\"name\":\"rand08\",\"package\":\"rand\",\"req\":\"^0.8.4\"},{\"default_features\":false,\"name\":\"rand09\",\"optional\":true,\"package\":\"rand\",\"req\":\"^0.9.0\"},{\"kind\":\"dev\",\"name\":\"rand09\",\"package\":\"rand\",\"req\":\"^0.9.0\"},{\"default_features\":false,\"name\":\"serde_core\",\"optional\":true,\"req\":\"^1.0.220\"},{\"kind\":\"dev\",\"name\":\"serde_json\",\"req\":\"^1.0.86\"}],\"features\":{\"alloc\":[],\"default\":[],\"macros\":[\"dep:deranged-macros\"],\"num\":[\"dep:num-traits\"],\"powerfmt\":[\"dep:powerfmt\"],\"quickcheck\":[\"dep:quickcheck\",\"alloc\"],\"rand\":[\"rand08\",\"rand09\"],\"rand08\":[\"dep:rand08\"],\"rand09\":[\"dep:rand09\"],\"serde\":[\"dep:serde_core\"]}}", diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 8f36814684..fc5c70e6b6 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1828,6 +1828,7 @@ name = "codex-code-mode" version = "0.0.0" dependencies = [ "async-trait", + "deno_core_icudata", "pretty_assertions", "serde", "serde_json", @@ -3899,6 +3900,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26bf8fc351c5ed29b5c2f0cbbac1b209b74f60ecd62e675a998df72c49af5204" +[[package]] +name = "deno_core_icudata" +version = "0.77.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9efff8990a82c1ae664292507e1a5c6749ddd2312898cdf9cd7cb1fd4bc64c6" + [[package]] name = "der" version = "0.7.10" diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index 007c57e97c..866d343fd9 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -220,6 +220,7 @@ crossbeam-channel = "0.5.15" crossterm = "0.28.1" csv = "1.3.1" ctor = "0.6.3" +deno_core_icudata = "0.77.0" derive_more = "2" diffy = "0.4.2" dirs = "6" diff --git a/codex-rs/code-mode/Cargo.toml b/codex-rs/code-mode/Cargo.toml index e821ca0e4d..fa7ce63a42 100644 --- a/codex-rs/code-mode/Cargo.toml +++ b/codex-rs/code-mode/Cargo.toml @@ -14,6 +14,7 @@ workspace = true [dependencies] async-trait = { workspace = true } +deno_core_icudata = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "sync", "time"] } diff --git a/codex-rs/code-mode/src/runtime/mod.rs b/codex-rs/code-mode/src/runtime/mod.rs index 411f81bddc..febe54216a 100644 --- a/codex-rs/code-mode/src/runtime/mod.rs +++ b/codex-rs/code-mode/src/runtime/mod.rs @@ -104,6 +104,8 @@ pub(crate) fn spawn_runtime( request: ExecuteRequest, event_tx: mpsc::UnboundedSender, ) -> Result<(std_mpsc::Sender, v8::IsolateHandle), String> { + initialize_v8()?; + let (command_tx, command_rx) = std_mpsc::channel(); let runtime_command_tx = command_tx.clone(); let (isolate_handle_tx, isolate_handle_rx) = std_mpsc::sync_channel(1); @@ -164,15 +166,20 @@ pub(super) enum CompletionState { }, } -fn initialize_v8() { - static PLATFORM: OnceLock> = OnceLock::new(); +fn initialize_v8() -> Result<(), String> { + static PLATFORM: OnceLock, String>> = OnceLock::new(); - let _ = PLATFORM.get_or_init(|| { + match PLATFORM.get_or_init(|| { + v8::icu::set_common_data_77(deno_core_icudata::ICU_DATA) + .map_err(|error_code| format!("failed to initialize ICU data: {error_code}"))?; let platform = v8::new_default_platform(0, false).make_shared(); v8::V8::initialize_platform(platform.clone()); v8::V8::initialize(); - platform - }); + Ok(platform) + }) { + Ok(_) => Ok(()), + Err(error_text) => Err(error_text.clone()), + } } fn run_runtime( @@ -182,8 +189,6 @@ fn run_runtime( isolate_handle_tx: std_mpsc::SyncSender, runtime_command_tx: std_mpsc::Sender, ) { - initialize_v8(); - let isolate = &mut v8::Isolate::new(v8::CreateParams::default()); let isolate_handle = isolate.thread_safe_handle(); if isolate_handle_tx.send(isolate_handle).is_err() { diff --git a/codex-rs/code-mode/src/service.rs b/codex-rs/code-mode/src/service.rs index 5b67dd17b8..c14fee1957 100644 --- a/codex-rs/code-mode/src/service.rs +++ b/codex-rs/code-mode/src/service.rs @@ -561,6 +561,85 @@ mod tests { ); } + #[tokio::test] + async fn date_locale_string_formats_with_icu_data() { + let service = CodeModeService::new(); + + let response = service + .execute(ExecuteRequest { + source: r#" +const value = new Date("2025-01-02T03:04:05Z") + .toLocaleString("fr-FR", { + weekday: "long", + month: "long", + day: "numeric", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + hour12: false, + timeZone: "UTC", + }); +text(value); +"# + .to_string(), + yield_time_ms: None, + ..execute_request("") + }) + .await + .unwrap(); + + assert_eq!( + response, + RuntimeResponse::Result { + cell_id: "1".to_string(), + content_items: vec![FunctionCallOutputContentItem::InputText { + text: "jeudi 2 janvier \u{e0} 03:04:05".to_string(), + }], + stored_values: HashMap::new(), + error_text: None, + } + ); + } + + #[tokio::test] + async fn intl_date_time_format_formats_with_icu_data() { + let service = CodeModeService::new(); + + let response = service + .execute(ExecuteRequest { + source: r#" +const formatter = new Intl.DateTimeFormat("fr-FR", { + weekday: "long", + month: "long", + day: "numeric", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + hour12: false, + timeZone: "UTC", +}); +text(formatter.format(new Date("2025-01-02T03:04:05Z"))); +"# + .to_string(), + yield_time_ms: None, + ..execute_request("") + }) + .await + .unwrap(); + + assert_eq!( + response, + RuntimeResponse::Result { + cell_id: "1".to_string(), + content_items: vec![FunctionCallOutputContentItem::InputText { + text: "jeudi 2 janvier \u{e0} 03:04:05".to_string(), + }], + stored_values: HashMap::new(), + error_text: None, + } + ); + } + #[tokio::test] async fn output_helpers_return_undefined() { let service = CodeModeService::new(); From 05c582992359e47afaa298c045c62af42001a463 Mon Sep 17 00:00:00 2001 From: Thibault Sottiaux Date: Mon, 13 Apr 2026 22:09:51 -0700 Subject: [PATCH 03/16] [codex] drain mailbox only at request boundaries (#17749) This changes multi-agent v2 mailbox handling so incoming inter-agent messages no longer preempt an in-flight sampling stream at reasoning or commentary output-item boundaries. --- codex-rs/core/src/codex.rs | 27 ------------- codex-rs/core/tests/suite/pending_input.rs | 39 +++++++++---------- ...ng_input_queued_mail_after_commentary.snap | 6 ++- ...ing_input_queued_mail_after_reasoning.snap | 3 +- 4 files changed, 24 insertions(+), 51 deletions(-) diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index bab91b177c..2fd36376dd 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -338,7 +338,6 @@ use codex_protocol::config_types::ServiceTier; use codex_protocol::config_types::WindowsSandboxLevel; use codex_protocol::models::ContentItem; use codex_protocol::models::DeveloperInstructions; -use codex_protocol::models::MessagePhase; use codex_protocol::models::ResponseInputItem; use codex_protocol::models::ResponseItem; use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig; @@ -7836,25 +7835,6 @@ async fn try_run_sampling_request( cancellation_token: cancellation_token.child_token(), }; - let preempt_for_mailbox_mail = match &item { - ResponseItem::Message { role, phase, .. } => { - role == "assistant" && matches!(phase, Some(MessagePhase::Commentary)) - } - ResponseItem::Reasoning { .. } => true, - ResponseItem::LocalShellCall { .. } - | ResponseItem::FunctionCall { .. } - | ResponseItem::ToolSearchCall { .. } - | ResponseItem::FunctionCallOutput { .. } - | ResponseItem::CustomToolCall { .. } - | ResponseItem::CustomToolCallOutput { .. } - | ResponseItem::ToolSearchOutput { .. } - | ResponseItem::WebSearchCall { .. } - | ResponseItem::ImageGenerationCall { .. } - | ResponseItem::GhostSnapshot { .. } - | ResponseItem::Compaction { .. } - | ResponseItem::Other => false, - }; - let output_result = match handle_output_item_done(&mut ctx, item, previously_active_item) .instrument(handle_responses) @@ -7870,13 +7850,6 @@ async fn try_run_sampling_request( last_agent_message = Some(agent_message); } needs_follow_up |= output_result.needs_follow_up; - // todo: remove before stabilizing multi-agent v2 - if preempt_for_mailbox_mail && sess.mailbox_rx.lock().await.has_pending() { - break Ok(SamplingRequestResult { - needs_follow_up: true, - last_agent_message, - }); - } } ResponseEvent::OutputItemAdded(item) => { if let Some(turn_item) = handle_non_tool_response_item( diff --git a/codex-rs/core/tests/suite/pending_input.rs b/codex-rs/core/tests/suite/pending_input.rs index 406d6a3d57..719cc1f0a5 100644 --- a/codex-rs/core/tests/suite/pending_input.rs +++ b/codex-rs/core/tests/suite/pending_input.rs @@ -313,7 +313,7 @@ async fn injected_user_input_triggers_follow_up_request_with_deltas() { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn queued_inter_agent_mail_triggers_follow_up_after_reasoning_item() { +async fn queued_inter_agent_mail_waits_for_request_boundary_after_reasoning_item() { let (gate_reasoning_done_tx, gate_reasoning_done_rx) = oneshot::channel(); let first_chunks = vec![ @@ -323,14 +323,18 @@ async fn queued_inter_agent_mail_triggers_follow_up_after_reasoning_item() { gate_reasoning_done_rx, vec![ ev_reasoning_item("reason-1", &["thinking"], &[]), - ev_function_call( - "call-stale", - "shell", - r#"{"command":"echo stale tool call"}"#, - ), - ev_message_item_added("msg-stale", ""), - ev_output_text_delta("stale final"), - ev_message_item_done("msg-stale", "stale final"), + ev_message_item_added("msg-preserved", ""), + ev_output_text_delta("preserved commentary"), + json!({ + "type": "response.output_item.done", + "item": { + "type": "message", + "role": "assistant", + "id": "msg-preserved", + "content": [{"type": "output_text", "text": "preserved commentary"}], + "phase": "commentary", + } + }), ev_completed("resp-1"), ], ), @@ -358,7 +362,7 @@ async fn queued_inter_agent_mail_triggers_follow_up_after_reasoning_item() { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn queued_inter_agent_mail_triggers_follow_up_after_commentary_message_item() { +async fn queued_inter_agent_mail_waits_for_request_boundary_after_commentary_message_item() { let (gate_message_done_tx, gate_message_done_rx) = oneshot::channel(); let first_chunks = vec![ @@ -367,25 +371,18 @@ async fn queued_inter_agent_mail_triggers_follow_up_after_commentary_message_ite gated_chunk( gate_message_done_rx, vec![ - ev_output_text_delta("first answer"), + ev_output_text_delta("first commentary"), json!({ "type": "response.output_item.done", "item": { "type": "message", "role": "assistant", "id": "msg-1", - "content": [{"type": "output_text", "text": "first answer"}], + "content": [{"type": "output_text", "text": "first commentary"}], "phase": "commentary", } }), - ev_function_call( - "call-stale", - "shell", - r#"{"command":"echo stale tool call"}"#, - ), - ev_message_item_added("msg-stale", ""), - ev_output_text_delta("stale final"), - ev_message_item_done("msg-stale", "stale final"), + ev_function_call("call-preserved", "test_tool", "{}"), ev_completed("resp-1"), ], ), @@ -411,7 +408,7 @@ async fn queued_inter_agent_mail_triggers_follow_up_after_commentary_message_ite let _ = gate_message_done_tx.send(()); - wait_for_agent_message(&codex, "first answer").await; + wait_for_agent_message(&codex, "first commentary").await; wait_for_turn_complete(&codex).await; diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__pending_input__pending_input_queued_mail_after_commentary.snap b/codex-rs/core/tests/suite/snapshots/all__suite__pending_input__pending_input_queued_mail_after_commentary.snap index e65a5f34f0..d3889fcd32 100644 --- a/codex-rs/core/tests/suite/snapshots/all__suite__pending_input__pending_input_queued_mail_after_commentary.snap +++ b/codex-rs/core/tests/suite/snapshots/all__suite__pending_input__pending_input_queued_mail_after_commentary.snap @@ -13,5 +13,7 @@ Scenario: /responses POST bodies (input only, redacted like other suite snapshot 00:message/developer: 01:message/user:> 02:message/user:first prompt -03:message/assistant:first answer -04:message/assistant:{"author":"/root/worker","recipient":"/root","other_recipients":[],"content":"queued child update","trigger_turn":false} +03:message/assistant:first commentary +04:function_call/test_tool +05:function_call_output:unsupported call: test_tool +06:message/assistant:{"author":"/root/worker","recipient":"/root","other_recipients":[],"content":"queued child update","trigger_turn":false} diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__pending_input__pending_input_queued_mail_after_reasoning.snap b/codex-rs/core/tests/suite/snapshots/all__suite__pending_input__pending_input_queued_mail_after_reasoning.snap index 004196f97a..8e23dfe52d 100644 --- a/codex-rs/core/tests/suite/snapshots/all__suite__pending_input__pending_input_queued_mail_after_reasoning.snap +++ b/codex-rs/core/tests/suite/snapshots/all__suite__pending_input__pending_input_queued_mail_after_reasoning.snap @@ -14,4 +14,5 @@ Scenario: /responses POST bodies (input only, redacted like other suite snapshot 01:message/user:> 02:message/user:first prompt 03:reasoning:summary=thinking:encrypted=true -04:message/assistant:{"author":"/root/worker","recipient":"/root","other_recipients":[],"content":"queued child update","trigger_turn":false} +04:message/assistant:preserved commentary +05:message/assistant:{"author":"/root/worker","recipient":"/root","other_recipients":[],"content":"queued child update","trigger_turn":false} From b704df85b856cc7ae20fb339b25742fdf7150d5c Mon Sep 17 00:00:00 2001 From: rhan-oai Date: Mon, 13 Apr 2026 23:11:49 -0700 Subject: [PATCH 04/16] [codex-analytics] feature plumbing and emittance (#16640) --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/openai/codex/pull/16640). * #16870 * #16706 * #16641 * __->__ #16640 --- .../analytics/src/analytics_client_tests.rs | 1035 ++++++++++++++++- codex-rs/analytics/src/client.rs | 46 + codex-rs/analytics/src/events.rs | 94 +- codex-rs/analytics/src/facts.rs | 139 +++ codex-rs/analytics/src/lib.rs | 20 + codex-rs/analytics/src/reducer.rs | 641 +++++++++- .../app-server/src/bespoke_event_handling.rs | 33 + .../app-server/src/codex_message_processor.rs | 97 +- codex-rs/app-server/src/main.rs | 23 +- codex-rs/app-server/src/message_processor.rs | 10 + codex-rs/app-server/tests/common/config.rs | 28 + codex-rs/app-server/tests/common/lib.rs | 1 + .../app-server/tests/common/mcp_process.rs | 5 + .../app-server/tests/suite/v2/analytics.rs | 53 + .../app-server/tests/suite/v2/thread_fork.rs | 4 +- .../tests/suite/v2/thread_resume.rs | 4 +- .../app-server/tests/suite/v2/thread_start.rs | 20 +- .../tests/suite/v2/turn_interrupt.rs | 21 +- .../app-server/tests/suite/v2/turn_start.rs | 163 +++ .../app-server/tests/suite/v2/turn_steer.rs | 83 +- codex-rs/core/src/codex.rs | 55 + codex-rs/core/src/codex_delegate.rs | 2 +- codex-rs/core/src/codex_tests_guardian.rs | 2 +- codex-rs/core/src/compact.rs | 10 +- codex-rs/core/src/state/session.rs | 12 + codex-rs/core/src/tasks/mod.rs | 8 + .../core/src/thread_rollout_truncation.rs | 12 + codex-rs/protocol/src/protocol.rs | 8 + 28 files changed, 2511 insertions(+), 118 deletions(-) diff --git a/codex-rs/analytics/src/analytics_client_tests.rs b/codex-rs/analytics/src/analytics_client_tests.rs index e5aca39b8e..0c672f6eec 100644 --- a/codex-rs/analytics/src/analytics_client_tests.rs +++ b/codex-rs/analytics/src/analytics_client_tests.rs @@ -7,7 +7,7 @@ use crate::events::CodexCompactionEventRequest; use crate::events::CodexPluginEventRequest; use crate::events::CodexPluginUsedEventRequest; use crate::events::CodexRuntimeMetadata; -use crate::events::ThreadInitializationMode; +use crate::events::CodexTurnEventRequest; use crate::events::ThreadInitializedEvent; use crate::events::ThreadInitializedEventParams; use crate::events::TrackEventRequest; @@ -16,6 +16,7 @@ use crate::events::codex_plugin_metadata; use crate::events::codex_plugin_used_metadata; use crate::events::subagent_thread_started_event_request; use crate::facts::AnalyticsFact; +use crate::facts::AnalyticsJsonRpcError; use crate::facts::AppInvocation; use crate::facts::AppMentionedInput; use crate::facts::AppUsedInput; @@ -27,6 +28,7 @@ use crate::facts::CompactionStatus; use crate::facts::CompactionStrategy; use crate::facts::CompactionTrigger; use crate::facts::CustomAnalyticsFact; +use crate::facts::InputError; use crate::facts::InvocationType; use crate::facts::PluginState; use crate::facts::PluginStateChangedInput; @@ -34,30 +36,55 @@ use crate::facts::PluginUsedInput; use crate::facts::SkillInvocation; use crate::facts::SkillInvokedInput; use crate::facts::SubAgentThreadStartedInput; +use crate::facts::ThreadInitializationMode; use crate::facts::TrackEventsContext; +use crate::facts::TurnResolvedConfigFact; +use crate::facts::TurnStatus; +use crate::facts::TurnSteerRequestError; +use crate::facts::TurnTokenUsageFact; use crate::reducer::AnalyticsReducer; use crate::reducer::normalize_path_for_skill_id; use crate::reducer::skill_id_for_local_skill; use codex_app_server_protocol::ApprovalsReviewer as AppServerApprovalsReviewer; use codex_app_server_protocol::AskForApproval as AppServerAskForApproval; use codex_app_server_protocol::ClientInfo; +use codex_app_server_protocol::ClientRequest; use codex_app_server_protocol::ClientResponse; +use codex_app_server_protocol::CodexErrorInfo; use codex_app_server_protocol::InitializeCapabilities; use codex_app_server_protocol::InitializeParams; +use codex_app_server_protocol::JSONRPCErrorError; +use codex_app_server_protocol::NonSteerableTurnKind; use codex_app_server_protocol::RequestId; use codex_app_server_protocol::SandboxPolicy as AppServerSandboxPolicy; +use codex_app_server_protocol::ServerNotification; use codex_app_server_protocol::SessionSource as AppServerSessionSource; use codex_app_server_protocol::Thread; use codex_app_server_protocol::ThreadResumeResponse; use codex_app_server_protocol::ThreadStartResponse; use codex_app_server_protocol::ThreadStatus as AppServerThreadStatus; +use codex_app_server_protocol::Turn; +use codex_app_server_protocol::TurnCompletedNotification; +use codex_app_server_protocol::TurnError as AppServerTurnError; +use codex_app_server_protocol::TurnStartParams; +use codex_app_server_protocol::TurnStartedNotification; +use codex_app_server_protocol::TurnStatus as AppServerTurnStatus; +use codex_app_server_protocol::TurnSteerParams; +use codex_app_server_protocol::TurnSteerResponse; +use codex_app_server_protocol::UserInput; use codex_login::default_client::DEFAULT_ORIGINATOR; use codex_login::default_client::originator; use codex_plugin::AppConnectorId; use codex_plugin::PluginCapabilitySummary; use codex_plugin::PluginId; use codex_plugin::PluginTelemetryMetadata; +use codex_protocol::config_types::ApprovalsReviewer; +use codex_protocol::config_types::ModeKind; +use codex_protocol::protocol::AskForApproval; +use codex_protocol::protocol::SandboxPolicy; +use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::SubAgentSource; +use codex_protocol::protocol::TokenUsage; use pretty_assertions::assert_eq; use serde_json::json; use std::collections::HashSet; @@ -165,6 +192,339 @@ fn sample_thread_resume_response_with_source( } } +fn sample_turn_start_request(thread_id: &str, request_id: i64) -> ClientRequest { + ClientRequest::TurnStart { + request_id: RequestId::Integer(request_id), + params: TurnStartParams { + thread_id: thread_id.to_string(), + input: vec![ + UserInput::Text { + text: "hello".to_string(), + text_elements: vec![], + }, + UserInput::Image { + url: "https://example.com/a.png".to_string(), + }, + ], + ..Default::default() + }, + } +} + +fn sample_turn_start_response(turn_id: &str, request_id: i64) -> ClientResponse { + ClientResponse::TurnStart { + request_id: RequestId::Integer(request_id), + response: codex_app_server_protocol::TurnStartResponse { + turn: Turn { + id: turn_id.to_string(), + items: vec![], + status: AppServerTurnStatus::InProgress, + error: None, + started_at: None, + completed_at: None, + duration_ms: None, + }, + }, + } +} + +fn sample_turn_started_notification(thread_id: &str, turn_id: &str) -> ServerNotification { + ServerNotification::TurnStarted(TurnStartedNotification { + thread_id: thread_id.to_string(), + turn: Turn { + id: turn_id.to_string(), + items: vec![], + status: AppServerTurnStatus::InProgress, + error: None, + started_at: Some(455), + completed_at: None, + duration_ms: None, + }, + }) +} + +fn sample_turn_token_usage_fact(thread_id: &str, turn_id: &str) -> TurnTokenUsageFact { + TurnTokenUsageFact { + thread_id: thread_id.to_string(), + turn_id: turn_id.to_string(), + token_usage: TokenUsage { + total_tokens: 321, + input_tokens: 123, + cached_input_tokens: 45, + output_tokens: 140, + reasoning_output_tokens: 13, + }, + } +} + +fn sample_turn_completed_notification( + thread_id: &str, + turn_id: &str, + status: AppServerTurnStatus, + codex_error_info: Option, +) -> ServerNotification { + ServerNotification::TurnCompleted(TurnCompletedNotification { + thread_id: thread_id.to_string(), + turn: Turn { + id: turn_id.to_string(), + items: vec![], + status, + error: codex_error_info.map(|codex_error_info| AppServerTurnError { + message: "turn failed".to_string(), + codex_error_info: Some(codex_error_info), + additional_details: None, + }), + started_at: None, + completed_at: Some(456), + duration_ms: Some(1234), + }, + }) +} + +fn sample_turn_resolved_config(turn_id: &str) -> TurnResolvedConfigFact { + TurnResolvedConfigFact { + turn_id: turn_id.to_string(), + thread_id: "thread-2".to_string(), + num_input_images: 1, + submission_type: None, + ephemeral: false, + session_source: SessionSource::Exec, + model: "gpt-5".to_string(), + model_provider: "openai".to_string(), + sandbox_policy: SandboxPolicy::new_read_only_policy(), + reasoning_effort: None, + reasoning_summary: None, + service_tier: None, + approval_policy: AskForApproval::OnRequest, + approvals_reviewer: ApprovalsReviewer::GuardianSubagent, + sandbox_network_access: true, + collaboration_mode: ModeKind::Plan, + personality: None, + is_first_turn: true, + } +} + +fn sample_turn_steer_request( + thread_id: &str, + expected_turn_id: &str, + request_id: i64, +) -> ClientRequest { + ClientRequest::TurnSteer { + request_id: RequestId::Integer(request_id), + params: TurnSteerParams { + thread_id: thread_id.to_string(), + expected_turn_id: expected_turn_id.to_string(), + input: vec![ + UserInput::Text { + text: "more".to_string(), + text_elements: vec![], + }, + UserInput::LocalImage { + path: "/tmp/a.png".into(), + }, + ], + responsesapi_client_metadata: None, + }, + } +} + +fn sample_turn_steer_response(turn_id: &str, request_id: i64) -> ClientResponse { + ClientResponse::TurnSteer { + request_id: RequestId::Integer(request_id), + response: TurnSteerResponse { + turn_id: turn_id.to_string(), + }, + } +} + +fn no_active_turn_steer_error() -> JSONRPCErrorError { + JSONRPCErrorError { + code: -32600, + message: "no active turn to steer".to_string(), + data: None, + } +} + +fn no_active_turn_steer_error_type() -> AnalyticsJsonRpcError { + AnalyticsJsonRpcError::TurnSteer(TurnSteerRequestError::NoActiveTurn) +} + +fn non_steerable_review_error() -> JSONRPCErrorError { + JSONRPCErrorError { + code: -32600, + message: "cannot steer a review turn".to_string(), + data: Some( + serde_json::to_value(AppServerTurnError { + message: "cannot steer a review turn".to_string(), + codex_error_info: Some(CodexErrorInfo::ActiveTurnNotSteerable { + turn_kind: NonSteerableTurnKind::Review, + }), + additional_details: None, + }) + .expect("serialize turn error"), + ), + } +} + +fn non_steerable_review_error_type() -> AnalyticsJsonRpcError { + AnalyticsJsonRpcError::TurnSteer(TurnSteerRequestError::NonSteerableReview) +} + +fn input_too_large_steer_error() -> JSONRPCErrorError { + JSONRPCErrorError { + code: -32602, + message: "Input exceeds the maximum length of 1048576 characters.".to_string(), + data: Some(json!({ + "input_error_code": "input_too_large", + "actual_chars": 1048577, + "max_chars": 1048576, + })), + } +} + +fn input_too_large_error_type() -> AnalyticsJsonRpcError { + AnalyticsJsonRpcError::Input(InputError::TooLarge) +} + +async fn ingest_rejected_turn_steer( + reducer: &mut AnalyticsReducer, + out: &mut Vec, + error: JSONRPCErrorError, + error_type: Option, +) -> serde_json::Value { + ingest_turn_prerequisites( + reducer, out, /*include_initialize*/ true, /*include_resolved_config*/ false, + /*include_started*/ false, /*include_token_usage*/ false, + ) + .await; + reducer + .ingest( + AnalyticsFact::Request { + connection_id: 7, + request_id: RequestId::Integer(4), + request: Box::new(sample_turn_steer_request( + "thread-2", "turn-2", /*request_id*/ 4, + )), + }, + out, + ) + .await; + reducer + .ingest( + AnalyticsFact::ErrorResponse { + connection_id: 7, + request_id: RequestId::Integer(4), + error, + error_type, + }, + out, + ) + .await; + + assert_eq!(out.len(), 1); + serde_json::to_value(&out[0]).expect("serialize turn steer event") +} + +async fn ingest_initialize(reducer: &mut AnalyticsReducer, out: &mut Vec) { + reducer + .ingest( + AnalyticsFact::Initialize { + connection_id: 7, + params: InitializeParams { + client_info: ClientInfo { + name: "codex-tui".to_string(), + title: None, + version: "1.0.0".to_string(), + }, + capabilities: None, + }, + product_client_id: "codex-tui".to_string(), + runtime: sample_runtime_metadata(), + rpc_transport: AppServerRpcTransport::Stdio, + }, + out, + ) + .await; +} + +async fn ingest_turn_prerequisites( + reducer: &mut AnalyticsReducer, + out: &mut Vec, + include_initialize: bool, + include_resolved_config: bool, + include_started: bool, + include_token_usage: bool, +) { + if include_initialize { + ingest_initialize(reducer, out).await; + reducer + .ingest( + AnalyticsFact::Response { + connection_id: 7, + response: Box::new(sample_thread_start_response( + "thread-2", /*ephemeral*/ false, "gpt-5", + )), + }, + out, + ) + .await; + out.clear(); + } + + reducer + .ingest( + AnalyticsFact::Request { + connection_id: 7, + request_id: RequestId::Integer(3), + request: Box::new(sample_turn_start_request("thread-2", /*request_id*/ 3)), + }, + out, + ) + .await; + reducer + .ingest( + AnalyticsFact::Response { + connection_id: 7, + response: Box::new(sample_turn_start_response("turn-2", /*request_id*/ 3)), + }, + out, + ) + .await; + + if include_resolved_config { + reducer + .ingest( + AnalyticsFact::Custom(CustomAnalyticsFact::TurnResolvedConfig(Box::new( + sample_turn_resolved_config("turn-2"), + ))), + out, + ) + .await; + } + + if include_started { + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_started_notification( + "thread-2", "turn-2", + ))), + out, + ) + .await; + } + + if include_token_usage { + reducer + .ingest( + AnalyticsFact::Custom(CustomAnalyticsFact::TurnTokenUsage(Box::new( + sample_turn_token_usage_fact("thread-2", "turn-2"), + ))), + out, + ) + .await; + } +} + fn expected_absolute_path(path: &PathBuf) -> String { std::fs::canonicalize(path) .unwrap_or_else(|_| path.to_path_buf()) @@ -565,10 +925,6 @@ async fn initialize_caches_client_and_thread_lifecycle_publishes_once_initialize payload[0]["event_params"]["runtime"]["runtime_arch"], "x86_64" ); - assert_eq!(payload[0]["event_params"]["initialization_mode"], "resumed"); - assert_eq!(payload[0]["event_params"]["thread_source"], "user"); - assert_eq!(payload[0]["event_params"]["subagent_source"], json!(null)); - assert_eq!(payload[0]["event_params"]["parent_thread_id"], json!(null)); } #[tokio::test] @@ -1089,6 +1445,675 @@ async fn reducer_ingests_plugin_state_changed_fact() { ); } +#[test] +fn turn_event_serializes_expected_shape() { + let event = TrackEventRequest::TurnEvent(Box::new(CodexTurnEventRequest { + event_type: "codex_turn_event", + event_params: crate::events::CodexTurnEventParams { + thread_id: "thread-2".to_string(), + turn_id: "turn-2".to_string(), + app_server_client: sample_app_server_client_metadata(), + runtime: sample_runtime_metadata(), + submission_type: None, + ephemeral: false, + thread_source: Some("user".to_string()), + initialization_mode: ThreadInitializationMode::New, + subagent_source: None, + parent_thread_id: None, + model: Some("gpt-5".to_string()), + model_provider: "openai".to_string(), + sandbox_policy: Some("read_only"), + reasoning_effort: Some("high".to_string()), + reasoning_summary: Some("detailed".to_string()), + service_tier: "flex".to_string(), + approval_policy: "on-request".to_string(), + approvals_reviewer: "guardian_subagent".to_string(), + sandbox_network_access: true, + collaboration_mode: Some("plan"), + personality: Some("pragmatic".to_string()), + num_input_images: 2, + is_first_turn: true, + status: Some(TurnStatus::Completed), + turn_error: None, + steer_count: Some(0), + total_tool_call_count: None, + shell_command_count: None, + file_change_count: None, + mcp_tool_call_count: None, + dynamic_tool_call_count: None, + subagent_tool_call_count: None, + web_search_count: None, + image_generation_count: None, + input_tokens: None, + cached_input_tokens: None, + output_tokens: None, + reasoning_output_tokens: None, + total_tokens: None, + duration_ms: Some(1234), + started_at: Some(455), + completed_at: Some(456), + }, + })); + + let payload = serde_json::to_value(&event).expect("serialize turn event"); + let expected = serde_json::from_str::( + r#"{ + "event_type": "codex_turn_event", + "event_params": { + "thread_id": "thread-2", + "turn_id": "turn-2", + "submission_type": null, + "app_server_client": { + "product_client_id": "codex_cli_rs", + "client_name": "codex-tui", + "client_version": "1.0.0", + "rpc_transport": "stdio", + "experimental_api_enabled": true + }, + "runtime": { + "codex_rs_version": "0.1.0", + "runtime_os": "macos", + "runtime_os_version": "15.3.1", + "runtime_arch": "aarch64" + }, + "ephemeral": false, + "thread_source": "user", + "initialization_mode": "new", + "subagent_source": null, + "parent_thread_id": null, + "model": "gpt-5", + "model_provider": "openai", + "sandbox_policy": "read_only", + "reasoning_effort": "high", + "reasoning_summary": "detailed", + "service_tier": "flex", + "approval_policy": "on-request", + "approvals_reviewer": "guardian_subagent", + "sandbox_network_access": true, + "collaboration_mode": "plan", + "personality": "pragmatic", + "num_input_images": 2, + "is_first_turn": true, + "status": "completed", + "turn_error": null, + "steer_count": 0, + "total_tool_call_count": null, + "shell_command_count": null, + "file_change_count": null, + "mcp_tool_call_count": null, + "dynamic_tool_call_count": null, + "subagent_tool_call_count": null, + "web_search_count": null, + "image_generation_count": null, + "input_tokens": null, + "cached_input_tokens": null, + "output_tokens": null, + "reasoning_output_tokens": null, + "total_tokens": null, + "duration_ms": 1234, + "started_at": 455, + "completed_at": 456 + } + }"#, + ) + .expect("parse expected turn event"); + + assert_eq!(payload, expected); +} + +#[tokio::test] +async fn accepted_turn_steer_emits_expected_event() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_turn_prerequisites( + &mut reducer, + &mut out, + /*include_initialize*/ true, + /*include_resolved_config*/ false, + /*include_started*/ false, + /*include_token_usage*/ false, + ) + .await; + reducer + .ingest( + AnalyticsFact::Request { + connection_id: 7, + request_id: RequestId::Integer(4), + request: Box::new(sample_turn_steer_request( + "thread-2", "turn-2", /*request_id*/ 4, + )), + }, + &mut out, + ) + .await; + reducer + .ingest( + AnalyticsFact::Response { + connection_id: 7, + response: Box::new(sample_turn_steer_response("turn-2", /*request_id*/ 4)), + }, + &mut out, + ) + .await; + + assert_eq!(out.len(), 1); + let payload = serde_json::to_value(&out[0]).expect("serialize turn steer event"); + assert_eq!(payload["event_type"], json!("codex_turn_steer_event")); + assert_eq!(payload["event_params"]["thread_id"], json!("thread-2")); + assert_eq!(payload["event_params"]["expected_turn_id"], json!("turn-2")); + assert_eq!(payload["event_params"]["accepted_turn_id"], json!("turn-2")); + assert_eq!(payload["event_params"]["num_input_images"], json!(1)); + assert_eq!(payload["event_params"]["result"], json!("accepted")); + assert_eq!(payload["event_params"]["rejection_reason"], json!(null)); + assert!( + payload["event_params"]["created_at"] + .as_u64() + .expect("created_at") + > 0 + ); + assert_eq!( + payload["event_params"]["app_server_client"]["product_client_id"], + json!("codex-tui") + ); + assert_eq!( + payload["event_params"]["runtime"]["codex_rs_version"], + json!("0.1.0") + ); + assert_eq!(payload["event_params"]["thread_source"], json!("user")); + assert_eq!(payload["event_params"]["subagent_source"], json!(null)); + assert_eq!(payload["event_params"]["parent_thread_id"], json!(null)); + assert!(payload["event_params"].get("product_client_id").is_none()); +} + +#[tokio::test] +async fn rejected_turn_steer_uses_request_connection_metadata() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + let payload = ingest_rejected_turn_steer( + &mut reducer, + &mut out, + no_active_turn_steer_error(), + Some(no_active_turn_steer_error_type()), + ) + .await; + + assert_eq!(payload["event_type"], json!("codex_turn_steer_event")); + assert_eq!(payload["event_params"]["thread_id"], json!("thread-2")); + assert_eq!(payload["event_params"]["expected_turn_id"], json!("turn-2")); + assert_eq!(payload["event_params"]["accepted_turn_id"], json!(null)); + assert_eq!(payload["event_params"]["num_input_images"], json!(1)); + assert_eq!( + payload["event_params"]["app_server_client"]["product_client_id"], + json!("codex-tui") + ); + assert_eq!( + payload["event_params"]["runtime"]["codex_rs_version"], + json!("0.1.0") + ); + assert_eq!(payload["event_params"]["thread_source"], json!("user")); + assert_eq!(payload["event_params"]["subagent_source"], json!(null)); + assert_eq!(payload["event_params"]["parent_thread_id"], json!(null)); + assert_eq!(payload["event_params"]["result"], json!("rejected")); + assert_eq!( + payload["event_params"]["rejection_reason"], + json!("no_active_turn") + ); + assert!( + payload["event_params"]["created_at"] + .as_u64() + .expect("created_at") + > 0 + ); +} + +#[tokio::test] +async fn rejected_turn_steer_maps_active_turn_not_steerable_error_type() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + let payload = ingest_rejected_turn_steer( + &mut reducer, + &mut out, + non_steerable_review_error(), + Some(non_steerable_review_error_type()), + ) + .await; + + assert_eq!( + payload["event_params"]["rejection_reason"], + json!("non_steerable_review") + ); +} + +#[tokio::test] +async fn rejected_turn_steer_maps_input_too_large_error_type() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + let payload = ingest_rejected_turn_steer( + &mut reducer, + &mut out, + input_too_large_steer_error(), + Some(input_too_large_error_type()), + ) + .await; + + assert_eq!( + payload["event_params"]["rejection_reason"], + json!("input_too_large") + ); +} + +#[tokio::test] +async fn turn_steer_does_not_emit_without_pending_request() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + reducer + .ingest( + AnalyticsFact::ErrorResponse { + connection_id: 7, + request_id: RequestId::Integer(4), + error: no_active_turn_steer_error(), + error_type: Some(no_active_turn_steer_error_type()), + }, + &mut out, + ) + .await; + + assert!(out.is_empty()); +} + +#[tokio::test] +async fn turn_start_error_response_discards_pending_start_request() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_initialize(&mut reducer, &mut out).await; + reducer + .ingest( + AnalyticsFact::Request { + connection_id: 7, + request_id: RequestId::Integer(3), + request: Box::new(sample_turn_start_request("thread-2", /*request_id*/ 3)), + }, + &mut out, + ) + .await; + reducer + .ingest( + AnalyticsFact::ErrorResponse { + connection_id: 7, + request_id: RequestId::Integer(3), + error: no_active_turn_steer_error(), + error_type: None, + }, + &mut out, + ) + .await; + + // A late/synthetic response for the same request id must not resurrect the + // failed turn/start request and attach request-scoped connection metadata. + reducer + .ingest( + AnalyticsFact::Response { + connection_id: 7, + response: Box::new(sample_turn_start_response("turn-2", /*request_id*/ 3)), + }, + &mut out, + ) + .await; + assert!(out.is_empty()); + + reducer + .ingest( + AnalyticsFact::Custom(CustomAnalyticsFact::TurnResolvedConfig(Box::new( + sample_turn_resolved_config("turn-2"), + ))), + &mut out, + ) + .await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_completed_notification( + "thread-2", + "turn-2", + AppServerTurnStatus::Completed, + /*codex_error_info*/ None, + ))), + &mut out, + ) + .await; + + assert!(out.is_empty()); +} + +#[tokio::test] +async fn turn_lifecycle_emits_turn_event() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_turn_prerequisites( + &mut reducer, + &mut out, + /*include_initialize*/ true, + /*include_resolved_config*/ true, + /*include_started*/ true, + /*include_token_usage*/ true, + ) + .await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_completed_notification( + "thread-2", + "turn-2", + AppServerTurnStatus::Completed, + /*codex_error_info*/ None, + ))), + &mut out, + ) + .await; + + assert_eq!(out.len(), 1); + let payload = serde_json::to_value(&out[0]).expect("serialize turn event"); + assert_eq!(payload["event_type"], json!("codex_turn_event")); + assert_eq!(payload["event_params"]["thread_id"], json!("thread-2")); + assert_eq!(payload["event_params"]["turn_id"], json!("turn-2")); + assert_eq!( + payload["event_params"]["app_server_client"], + json!({ + "product_client_id": "codex-tui", + "client_name": "codex-tui", + "client_version": "1.0.0", + "rpc_transport": "stdio", + "experimental_api_enabled": null, + }) + ); + assert_eq!( + payload["event_params"]["runtime"], + json!({ + "codex_rs_version": "0.1.0", + "runtime_os": "macos", + "runtime_os_version": "15.3.1", + "runtime_arch": "aarch64", + }) + ); + assert!(payload["event_params"].get("product_client_id").is_none()); + assert_eq!(payload["event_params"]["ephemeral"], json!(false)); + assert_eq!(payload["event_params"]["num_input_images"], json!(1)); + assert_eq!(payload["event_params"]["status"], json!("completed")); + assert_eq!(payload["event_params"]["steer_count"], json!(0)); + assert_eq!(payload["event_params"]["started_at"], json!(455)); + assert_eq!(payload["event_params"]["completed_at"], json!(456)); + assert_eq!(payload["event_params"]["duration_ms"], json!(1234)); + assert_eq!(payload["event_params"]["input_tokens"], json!(123)); + assert_eq!(payload["event_params"]["cached_input_tokens"], json!(45)); + assert_eq!(payload["event_params"]["output_tokens"], json!(140)); + assert_eq!( + payload["event_params"]["reasoning_output_tokens"], + json!(13) + ); + assert_eq!(payload["event_params"]["total_tokens"], json!(321)); +} + +#[tokio::test] +async fn accepted_steers_increment_turn_steer_count() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_turn_prerequisites( + &mut reducer, + &mut out, + /*include_initialize*/ true, + /*include_resolved_config*/ true, + /*include_started*/ true, + /*include_token_usage*/ false, + ) + .await; + + reducer + .ingest( + AnalyticsFact::Request { + connection_id: 7, + request_id: RequestId::Integer(4), + request: Box::new(sample_turn_steer_request( + "thread-2", "turn-2", /*request_id*/ 4, + )), + }, + &mut out, + ) + .await; + reducer + .ingest( + AnalyticsFact::Response { + connection_id: 7, + response: Box::new(sample_turn_steer_response("turn-2", /*request_id*/ 4)), + }, + &mut out, + ) + .await; + + reducer + .ingest( + AnalyticsFact::Request { + connection_id: 7, + request_id: RequestId::Integer(5), + request: Box::new(sample_turn_steer_request( + "thread-2", "turn-2", /*request_id*/ 5, + )), + }, + &mut out, + ) + .await; + reducer + .ingest( + AnalyticsFact::ErrorResponse { + connection_id: 7, + request_id: RequestId::Integer(5), + error: no_active_turn_steer_error(), + error_type: Some(no_active_turn_steer_error_type()), + }, + &mut out, + ) + .await; + + reducer + .ingest( + AnalyticsFact::Request { + connection_id: 7, + request_id: RequestId::Integer(6), + request: Box::new(sample_turn_steer_request( + "thread-2", "turn-2", /*request_id*/ 6, + )), + }, + &mut out, + ) + .await; + reducer + .ingest( + AnalyticsFact::Response { + connection_id: 7, + response: Box::new(sample_turn_steer_response("turn-2", /*request_id*/ 6)), + }, + &mut out, + ) + .await; + + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_completed_notification( + "thread-2", + "turn-2", + AppServerTurnStatus::Completed, + /*codex_error_info*/ None, + ))), + &mut out, + ) + .await; + + let turn_event = out + .iter() + .find(|event| matches!(event, TrackEventRequest::TurnEvent(_))) + .expect("turn event should be emitted"); + let payload = serde_json::to_value(turn_event).expect("serialize turn event"); + assert_eq!(payload["event_params"]["steer_count"], json!(2)); +} + +#[tokio::test] +async fn turn_does_not_emit_without_required_prerequisites() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_turn_prerequisites( + &mut reducer, + &mut out, + /*include_initialize*/ false, + /*include_resolved_config*/ true, + /*include_started*/ false, + /*include_token_usage*/ false, + ) + .await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_completed_notification( + "thread-2", + "turn-2", + AppServerTurnStatus::Completed, + /*codex_error_info*/ None, + ))), + &mut out, + ) + .await; + assert!(out.is_empty()); + + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_turn_prerequisites( + &mut reducer, + &mut out, + /*include_initialize*/ true, + /*include_resolved_config*/ false, + /*include_started*/ false, + /*include_token_usage*/ false, + ) + .await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_completed_notification( + "thread-2", + "turn-2", + AppServerTurnStatus::Completed, + /*codex_error_info*/ None, + ))), + &mut out, + ) + .await; + assert!(out.is_empty()); +} + +#[tokio::test] +async fn turn_lifecycle_emits_failed_turn_event() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_turn_prerequisites( + &mut reducer, + &mut out, + /*include_initialize*/ true, + /*include_resolved_config*/ true, + /*include_started*/ true, + /*include_token_usage*/ false, + ) + .await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_completed_notification( + "thread-2", + "turn-2", + AppServerTurnStatus::Failed, + Some(codex_app_server_protocol::CodexErrorInfo::BadRequest), + ))), + &mut out, + ) + .await; + + assert_eq!(out.len(), 1); + let payload = serde_json::to_value(&out[0]).expect("serialize turn event"); + assert_eq!(payload["event_params"]["status"], json!("failed")); + assert_eq!(payload["event_params"]["turn_error"], json!("badRequest")); +} + +#[tokio::test] +async fn turn_lifecycle_emits_interrupted_turn_event_without_error() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_turn_prerequisites( + &mut reducer, + &mut out, + /*include_initialize*/ true, + /*include_resolved_config*/ true, + /*include_started*/ true, + /*include_token_usage*/ false, + ) + .await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_completed_notification( + "thread-2", + "turn-2", + AppServerTurnStatus::Interrupted, + /*codex_error_info*/ None, + ))), + &mut out, + ) + .await; + + assert_eq!(out.len(), 1); + let payload = serde_json::to_value(&out[0]).expect("serialize turn event"); + assert_eq!(payload["event_params"]["status"], json!("interrupted")); + assert_eq!(payload["event_params"]["turn_error"], json!(null)); +} + +#[tokio::test] +async fn turn_completed_without_started_notification_emits_null_started_at() { + let mut reducer = AnalyticsReducer::default(); + let mut out = Vec::new(); + + ingest_turn_prerequisites( + &mut reducer, + &mut out, + /*include_initialize*/ true, + /*include_resolved_config*/ true, + /*include_started*/ false, + /*include_token_usage*/ false, + ) + .await; + reducer + .ingest( + AnalyticsFact::Notification(Box::new(sample_turn_completed_notification( + "thread-2", + "turn-2", + AppServerTurnStatus::Completed, + /*codex_error_info*/ None, + ))), + &mut out, + ) + .await; + + let payload = serde_json::to_value(&out[0]).expect("serialize turn event"); + assert_eq!(payload["event_params"]["started_at"], json!(null)); + assert_eq!(payload["event_params"]["duration_ms"], json!(1234)); + assert_eq!(payload["event_params"]["input_tokens"], json!(null)); + assert_eq!(payload["event_params"]["cached_input_tokens"], json!(null)); + assert_eq!(payload["event_params"]["output_tokens"], json!(null)); + assert_eq!( + payload["event_params"]["reasoning_output_tokens"], + json!(null) + ); + assert_eq!(payload["event_params"]["total_tokens"], json!(null)); +} + fn sample_plugin_metadata() -> PluginTelemetryMetadata { PluginTelemetryMetadata { plugin_id: PluginId::parse("sample@test").expect("valid plugin id"), diff --git a/codex-rs/analytics/src/client.rs b/codex-rs/analytics/src/client.rs index 0d96ee6061..5ba60e3ef4 100644 --- a/codex-rs/analytics/src/client.rs +++ b/codex-rs/analytics/src/client.rs @@ -4,6 +4,7 @@ use crate::events::TrackEventRequest; use crate::events::TrackEventsRequest; use crate::events::current_runtime_metadata; use crate::facts::AnalyticsFact; +use crate::facts::AnalyticsJsonRpcError; use crate::facts::AppInvocation; use crate::facts::AppMentionedInput; use crate::facts::AppUsedInput; @@ -14,9 +15,15 @@ use crate::facts::SkillInvocation; use crate::facts::SkillInvokedInput; use crate::facts::SubAgentThreadStartedInput; use crate::facts::TrackEventsContext; +use crate::facts::TurnResolvedConfigFact; +use crate::facts::TurnTokenUsageFact; use crate::reducer::AnalyticsReducer; +use codex_app_server_protocol::ClientRequest; use codex_app_server_protocol::ClientResponse; use codex_app_server_protocol::InitializeParams; +use codex_app_server_protocol::JSONRPCErrorError; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ServerNotification; use codex_login::AuthManager; use codex_login::default_client::create_client; use codex_plugin::PluginTelemetryMetadata; @@ -167,6 +174,14 @@ impl AnalyticsEventsClient { ))); } + pub fn track_request(&self, connection_id: u64, request_id: RequestId, request: ClientRequest) { + self.record_fact(AnalyticsFact::Request { + connection_id, + request_id, + request: Box::new(request), + }); + } + pub fn track_app_used(&self, tracking: TrackEventsContext, app: AppInvocation) { if !self.queue.should_enqueue_app_used(&tracking, &app) { return; @@ -191,6 +206,18 @@ impl AnalyticsEventsClient { ))); } + pub fn track_turn_resolved_config(&self, fact: TurnResolvedConfigFact) { + self.record_fact(AnalyticsFact::Custom( + CustomAnalyticsFact::TurnResolvedConfig(Box::new(fact)), + )); + } + + pub fn track_turn_token_usage(&self, fact: TurnTokenUsageFact) { + self.record_fact(AnalyticsFact::Custom(CustomAnalyticsFact::TurnTokenUsage( + Box::new(fact), + ))); + } + pub fn track_plugin_installed(&self, plugin: PluginTelemetryMetadata) { self.record_fact(AnalyticsFact::Custom( CustomAnalyticsFact::PluginStateChanged(PluginStateChangedInput { @@ -240,6 +267,25 @@ impl AnalyticsEventsClient { response: Box::new(response), }); } + + pub fn track_error_response( + &self, + connection_id: u64, + request_id: RequestId, + error: JSONRPCErrorError, + error_type: Option, + ) { + self.record_fact(AnalyticsFact::ErrorResponse { + connection_id, + request_id, + error, + error_type, + }); + } + + pub fn track_notification(&self, notification: ServerNotification) { + self.record_fact(AnalyticsFact::Notification(Box::new(notification))); + } } async fn send_track_events( diff --git a/codex-rs/analytics/src/events.rs b/codex-rs/analytics/src/events.rs index 618dd8ffeb..87f5a4fbed 100644 --- a/codex-rs/analytics/src/events.rs +++ b/codex-rs/analytics/src/events.rs @@ -3,7 +3,13 @@ use crate::facts::CodexCompactionEvent; use crate::facts::InvocationType; use crate::facts::PluginState; use crate::facts::SubAgentThreadStartedInput; +use crate::facts::ThreadInitializationMode; use crate::facts::TrackEventsContext; +use crate::facts::TurnStatus; +use crate::facts::TurnSteerRejectionReason; +use crate::facts::TurnSteerResult; +use crate::facts::TurnSubmissionType; +use codex_app_server_protocol::CodexErrorInfo; use codex_login::default_client::originator; use codex_plugin::PluginTelemetryMetadata; use codex_protocol::approvals::NetworkApprovalProtocol; @@ -21,14 +27,6 @@ pub enum AppServerRpcTransport { InProcess, } -#[derive(Clone, Copy, Debug, Serialize)] -#[serde(rename_all = "snake_case")] -pub(crate) enum ThreadInitializationMode { - New, - Forked, - Resumed, -} - #[derive(Serialize)] pub(crate) struct TrackEventsRequest { pub(crate) events: Vec, @@ -43,6 +41,8 @@ pub(crate) enum TrackEventRequest { AppMentioned(CodexAppMentionedEventRequest), AppUsed(CodexAppUsedEventRequest), Compaction(Box), + TurnEvent(Box), + TurnSteer(CodexTurnSteerEventRequest), PluginUsed(CodexPluginUsedEventRequest), PluginInstalled(CodexPluginEventRequest), PluginUninstalled(CodexPluginEventRequest), @@ -330,6 +330,84 @@ pub(crate) struct CodexCompactionEventRequest { pub(crate) event_params: CodexCompactionEventParams, } +#[derive(Serialize)] +pub(crate) struct CodexTurnEventParams { + pub(crate) thread_id: String, + pub(crate) turn_id: String, + // TODO(rhan-oai): Populate once queued/default submission type is plumbed from + // the turn/start callsites instead of always being reported as None. + pub(crate) submission_type: Option, + pub(crate) app_server_client: CodexAppServerClientMetadata, + pub(crate) runtime: CodexRuntimeMetadata, + pub(crate) ephemeral: bool, + pub(crate) thread_source: Option, + pub(crate) initialization_mode: ThreadInitializationMode, + pub(crate) subagent_source: Option, + pub(crate) parent_thread_id: Option, + pub(crate) model: Option, + pub(crate) model_provider: String, + pub(crate) sandbox_policy: Option<&'static str>, + pub(crate) reasoning_effort: Option, + pub(crate) reasoning_summary: Option, + pub(crate) service_tier: String, + pub(crate) approval_policy: String, + pub(crate) approvals_reviewer: String, + pub(crate) sandbox_network_access: bool, + pub(crate) collaboration_mode: Option<&'static str>, + pub(crate) personality: Option, + pub(crate) num_input_images: usize, + pub(crate) is_first_turn: bool, + pub(crate) status: Option, + pub(crate) turn_error: Option, + pub(crate) steer_count: Option, + // TODO(rhan-oai): Populate these once tool-call accounting is emitted from + // core; the schema is reserved but these fields are currently always None. + pub(crate) total_tool_call_count: Option, + pub(crate) shell_command_count: Option, + pub(crate) file_change_count: Option, + pub(crate) mcp_tool_call_count: Option, + pub(crate) dynamic_tool_call_count: Option, + pub(crate) subagent_tool_call_count: Option, + pub(crate) web_search_count: Option, + pub(crate) image_generation_count: Option, + pub(crate) input_tokens: Option, + pub(crate) cached_input_tokens: Option, + pub(crate) output_tokens: Option, + pub(crate) reasoning_output_tokens: Option, + pub(crate) total_tokens: Option, + pub(crate) duration_ms: Option, + pub(crate) started_at: Option, + pub(crate) completed_at: Option, +} + +#[derive(Serialize)] +pub(crate) struct CodexTurnEventRequest { + pub(crate) event_type: &'static str, + pub(crate) event_params: CodexTurnEventParams, +} + +#[derive(Serialize)] +pub(crate) struct CodexTurnSteerEventParams { + pub(crate) thread_id: String, + pub(crate) expected_turn_id: Option, + pub(crate) accepted_turn_id: Option, + pub(crate) app_server_client: CodexAppServerClientMetadata, + pub(crate) runtime: CodexRuntimeMetadata, + pub(crate) thread_source: Option, + pub(crate) subagent_source: Option, + pub(crate) parent_thread_id: Option, + pub(crate) num_input_images: usize, + pub(crate) result: TurnSteerResult, + pub(crate) rejection_reason: Option, + pub(crate) created_at: u64, +} + +#[derive(Serialize)] +pub(crate) struct CodexTurnSteerEventRequest { + pub(crate) event_type: &'static str, + pub(crate) event_params: CodexTurnSteerEventParams, +} + #[derive(Serialize)] pub(crate) struct CodexPluginMetadata { pub(crate) plugin_id: Option, diff --git a/codex-rs/analytics/src/facts.rs b/codex-rs/analytics/src/facts.rs index 931ae01013..5590fbfa6d 100644 --- a/codex-rs/analytics/src/facts.rs +++ b/codex-rs/analytics/src/facts.rs @@ -4,11 +4,22 @@ use crate::events::GuardianReviewEventParams; use codex_app_server_protocol::ClientRequest; use codex_app_server_protocol::ClientResponse; use codex_app_server_protocol::InitializeParams; +use codex_app_server_protocol::JSONRPCErrorError; use codex_app_server_protocol::RequestId; use codex_app_server_protocol::ServerNotification; use codex_plugin::PluginTelemetryMetadata; +use codex_protocol::config_types::ApprovalsReviewer; +use codex_protocol::config_types::ModeKind; +use codex_protocol::config_types::Personality; +use codex_protocol::config_types::ReasoningSummary; +use codex_protocol::config_types::ServiceTier; +use codex_protocol::openai_models::ReasoningEffort; +use codex_protocol::protocol::AskForApproval; +use codex_protocol::protocol::SandboxPolicy; +use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::SkillScope; use codex_protocol::protocol::SubAgentSource; +use codex_protocol::protocol::TokenUsage; use serde::Serialize; use std::path::PathBuf; @@ -31,6 +42,126 @@ pub fn build_track_events_context( } } +#[derive(Clone, Copy, Debug, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum TurnSubmissionType { + Default, + Queued, +} + +#[derive(Clone)] +pub struct TurnResolvedConfigFact { + pub turn_id: String, + pub thread_id: String, + pub num_input_images: usize, + pub submission_type: Option, + pub ephemeral: bool, + pub session_source: SessionSource, + pub model: String, + pub model_provider: String, + pub sandbox_policy: SandboxPolicy, + pub reasoning_effort: Option, + pub reasoning_summary: Option, + pub service_tier: Option, + pub approval_policy: AskForApproval, + pub approvals_reviewer: ApprovalsReviewer, + pub sandbox_network_access: bool, + pub collaboration_mode: ModeKind, + pub personality: Option, + pub is_first_turn: bool, +} + +#[derive(Clone, Copy, Debug, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum ThreadInitializationMode { + New, + Forked, + Resumed, +} + +#[derive(Clone)] +pub struct TurnTokenUsageFact { + pub turn_id: String, + pub thread_id: String, + pub token_usage: TokenUsage, +} + +#[derive(Clone, Copy, Debug, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum TurnStatus { + Completed, + Failed, + Interrupted, +} + +#[derive(Clone, Copy, Debug, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum TurnSteerResult { + Accepted, + Rejected, +} + +#[derive(Clone, Copy, Debug, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum TurnSteerRejectionReason { + NoActiveTurn, + ExpectedTurnMismatch, + NonSteerableReview, + NonSteerableCompact, + EmptyInput, + InputTooLarge, +} + +#[derive(Clone)] +pub struct CodexTurnSteerEvent { + pub expected_turn_id: Option, + pub accepted_turn_id: Option, + pub num_input_images: usize, + pub result: TurnSteerResult, + pub rejection_reason: Option, + pub created_at: u64, +} + +#[derive(Clone, Copy, Debug)] +pub enum AnalyticsJsonRpcError { + TurnSteer(TurnSteerRequestError), + Input(InputError), +} + +#[derive(Clone, Copy, Debug)] +pub enum TurnSteerRequestError { + NoActiveTurn, + ExpectedTurnMismatch, + NonSteerableReview, + NonSteerableCompact, +} + +#[derive(Clone, Copy, Debug)] +pub enum InputError { + Empty, + TooLarge, +} + +impl From for TurnSteerRejectionReason { + fn from(error: TurnSteerRequestError) -> Self { + match error { + TurnSteerRequestError::NoActiveTurn => Self::NoActiveTurn, + TurnSteerRequestError::ExpectedTurnMismatch => Self::ExpectedTurnMismatch, + TurnSteerRequestError::NonSteerableReview => Self::NonSteerableReview, + TurnSteerRequestError::NonSteerableCompact => Self::NonSteerableCompact, + } + } +} + +impl From for TurnSteerRejectionReason { + fn from(error: InputError) -> Self { + match error { + InputError::Empty => Self::EmptyInput, + InputError::TooLarge => Self::InputTooLarge, + } + } +} + #[derive(Clone, Debug)] pub struct SkillInvocation { pub skill_name: String, @@ -146,6 +277,12 @@ pub(crate) enum AnalyticsFact { connection_id: u64, response: Box, }, + ErrorResponse { + connection_id: u64, + request_id: RequestId, + error: JSONRPCErrorError, + error_type: Option, + }, Notification(Box), // Facts that do not naturally exist on the app-server protocol surface, or // would require non-trivial protocol reshaping on this branch. @@ -156,6 +293,8 @@ pub(crate) enum CustomAnalyticsFact { SubAgentThreadStarted(SubAgentThreadStartedInput), Compaction(Box), GuardianReview(Box), + TurnResolvedConfig(Box), + TurnTokenUsage(Box), SkillInvoked(SkillInvokedInput), AppMentioned(AppMentionedInput), AppUsed(AppUsedInput), diff --git a/codex-rs/analytics/src/lib.rs b/codex-rs/analytics/src/lib.rs index 9b4cc1e9bc..1a1a123152 100644 --- a/codex-rs/analytics/src/lib.rs +++ b/codex-rs/analytics/src/lib.rs @@ -3,6 +3,9 @@ mod events; mod facts; mod reducer; +use std::time::SystemTime; +use std::time::UNIX_EPOCH; + pub use client::AnalyticsEventsClient; pub use events::AppServerRpcTransport; pub use events::GuardianApprovalRequestSource; @@ -16,19 +19,36 @@ pub use events::GuardianReviewSessionKind; pub use events::GuardianReviewTerminalStatus; pub use events::GuardianReviewUserAuthorization; pub use events::GuardianReviewedAction; +pub use facts::AnalyticsJsonRpcError; pub use facts::AppInvocation; pub use facts::CodexCompactionEvent; +pub use facts::CodexTurnSteerEvent; pub use facts::CompactionImplementation; pub use facts::CompactionPhase; pub use facts::CompactionReason; pub use facts::CompactionStatus; pub use facts::CompactionStrategy; pub use facts::CompactionTrigger; +pub use facts::InputError; pub use facts::InvocationType; pub use facts::SkillInvocation; pub use facts::SubAgentThreadStartedInput; +pub use facts::ThreadInitializationMode; pub use facts::TrackEventsContext; +pub use facts::TurnResolvedConfigFact; +pub use facts::TurnStatus; +pub use facts::TurnSteerRejectionReason; +pub use facts::TurnSteerRequestError; +pub use facts::TurnSteerResult; +pub use facts::TurnTokenUsageFact; pub use facts::build_track_events_context; #[cfg(test)] mod analytics_client_tests; + +pub fn now_unix_seconds() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_secs() +} diff --git a/codex-rs/analytics/src/reducer.rs b/codex-rs/analytics/src/reducer.rs index 0ed2899762..772a091b15 100644 --- a/codex-rs/analytics/src/reducer.rs +++ b/codex-rs/analytics/src/reducer.rs @@ -6,12 +6,15 @@ use crate::events::CodexCompactionEventRequest; use crate::events::CodexPluginEventRequest; use crate::events::CodexPluginUsedEventRequest; use crate::events::CodexRuntimeMetadata; +use crate::events::CodexTurnEventParams; +use crate::events::CodexTurnEventRequest; +use crate::events::CodexTurnSteerEventParams; +use crate::events::CodexTurnSteerEventRequest; use crate::events::GuardianReviewEventParams; use crate::events::GuardianReviewEventPayload; use crate::events::GuardianReviewEventRequest; use crate::events::SkillInvocationEventParams; use crate::events::SkillInvocationEventRequest; -use crate::events::ThreadInitializationMode; use crate::events::ThreadInitializedEvent; use crate::events::ThreadInitializedEventParams; use crate::events::TrackEventRequest; @@ -25,6 +28,7 @@ use crate::events::subagent_source_name; use crate::events::subagent_thread_started_event_request; use crate::events::thread_source_name; use crate::facts::AnalyticsFact; +use crate::facts::AnalyticsJsonRpcError; use crate::facts::AppMentionedInput; use crate::facts::AppUsedInput; use crate::facts::CodexCompactionEvent; @@ -34,19 +38,39 @@ use crate::facts::PluginStateChangedInput; use crate::facts::PluginUsedInput; use crate::facts::SkillInvokedInput; use crate::facts::SubAgentThreadStartedInput; +use crate::facts::ThreadInitializationMode; +use crate::facts::TurnResolvedConfigFact; +use crate::facts::TurnStatus; +use crate::facts::TurnSteerRejectionReason; +use crate::facts::TurnSteerResult; +use crate::facts::TurnTokenUsageFact; +use crate::now_unix_seconds; +use codex_app_server_protocol::ClientRequest; use codex_app_server_protocol::ClientResponse; +use codex_app_server_protocol::CodexErrorInfo; use codex_app_server_protocol::InitializeParams; +use codex_app_server_protocol::RequestId; +use codex_app_server_protocol::ServerNotification; +use codex_app_server_protocol::TurnSteerResponse; +use codex_app_server_protocol::UserInput; use codex_git_utils::collect_git_info; use codex_git_utils::get_git_repo_root; use codex_login::default_client::originator; +use codex_protocol::config_types::ModeKind; +use codex_protocol::config_types::Personality; +use codex_protocol::config_types::ReasoningSummary; +use codex_protocol::protocol::SandboxPolicy; use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::SkillScope; +use codex_protocol::protocol::TokenUsage; use sha1::Digest; use std::collections::HashMap; use std::path::Path; #[derive(Default)] pub(crate) struct AnalyticsReducer { + requests: HashMap<(u64, RequestId), RequestState>, + turns: HashMap, connections: HashMap, thread_connections: HashMap, thread_metadata: HashMap, @@ -60,12 +84,16 @@ struct ConnectionState { #[derive(Clone)] struct ThreadMetadataState { thread_source: Option<&'static str>, + initialization_mode: ThreadInitializationMode, subagent_source: Option, parent_thread_id: Option, } impl ThreadMetadataState { - fn from_session_source(session_source: &SessionSource) -> Self { + fn from_thread_metadata( + session_source: &SessionSource, + initialization_mode: ThreadInitializationMode, + ) -> Self { let (subagent_source, parent_thread_id) = match session_source { SessionSource::SubAgent(subagent_source) => ( Some(subagent_source_name(subagent_source)), @@ -80,12 +108,49 @@ impl ThreadMetadataState { }; Self { thread_source: thread_source_name(session_source), + initialization_mode, subagent_source, parent_thread_id, } } } +enum RequestState { + TurnStart(PendingTurnStartState), + TurnSteer(PendingTurnSteerState), +} + +struct PendingTurnStartState { + thread_id: String, + num_input_images: usize, +} + +struct PendingTurnSteerState { + thread_id: String, + expected_turn_id: String, + num_input_images: usize, + created_at: u64, +} + +#[derive(Clone)] +struct CompletedTurnState { + status: Option, + turn_error: Option, + completed_at: u64, + duration_ms: Option, +} + +struct TurnState { + connection_id: Option, + thread_id: Option, + num_input_images: Option, + resolved_config: Option, + started_at: Option, + token_usage: Option, + completed: Option, + steer_count: usize, +} + impl AnalyticsReducer { pub(crate) async fn ingest(&mut self, input: AnalyticsFact, out: &mut Vec) { match input { @@ -105,17 +170,29 @@ impl AnalyticsReducer { ); } AnalyticsFact::Request { - connection_id: _connection_id, - request_id: _request_id, - request: _request, - } => {} + connection_id, + request_id, + request, + } => { + self.ingest_request(connection_id, request_id, *request); + } AnalyticsFact::Response { connection_id, response, } => { self.ingest_response(connection_id, *response, out); } - AnalyticsFact::Notification(_notification) => {} + AnalyticsFact::ErrorResponse { + connection_id, + request_id, + error: _, + error_type, + } => { + self.ingest_error_response(connection_id, request_id, error_type, out); + } + AnalyticsFact::Notification(notification) => { + self.ingest_notification(*notification, out); + } AnalyticsFact::Custom(input) => match input { CustomAnalyticsFact::SubAgentThreadStarted(input) => { self.ingest_subagent_thread_started(input, out); @@ -126,6 +203,12 @@ impl AnalyticsReducer { CustomAnalyticsFact::GuardianReview(input) => { self.ingest_guardian_review(*input, out); } + CustomAnalyticsFact::TurnResolvedConfig(input) => { + self.ingest_turn_resolved_config(*input, out); + } + CustomAnalyticsFact::TurnTokenUsage(input) => { + self.ingest_turn_token_usage(*input, out); + } CustomAnalyticsFact::SkillInvoked(input) => { self.ingest_skill_invoked(input, out).await; } @@ -216,6 +299,82 @@ impl AnalyticsReducer { ))); } + fn ingest_request( + &mut self, + connection_id: u64, + request_id: RequestId, + request: ClientRequest, + ) { + match request { + ClientRequest::TurnStart { params, .. } => { + self.requests.insert( + (connection_id, request_id), + RequestState::TurnStart(PendingTurnStartState { + thread_id: params.thread_id, + num_input_images: num_input_images(¶ms.input), + }), + ); + } + ClientRequest::TurnSteer { params, .. } => { + self.requests.insert( + (connection_id, request_id), + RequestState::TurnSteer(PendingTurnSteerState { + thread_id: params.thread_id, + expected_turn_id: params.expected_turn_id, + num_input_images: num_input_images(¶ms.input), + created_at: now_unix_seconds(), + }), + ); + } + _ => {} + } + } + + fn ingest_turn_resolved_config( + &mut self, + input: TurnResolvedConfigFact, + out: &mut Vec, + ) { + let turn_id = input.turn_id.clone(); + let thread_id = input.thread_id.clone(); + let num_input_images = input.num_input_images; + let turn_state = self.turns.entry(turn_id.clone()).or_insert(TurnState { + connection_id: None, + thread_id: None, + num_input_images: None, + resolved_config: None, + started_at: None, + token_usage: None, + completed: None, + steer_count: 0, + }); + turn_state.thread_id = Some(thread_id); + turn_state.num_input_images = Some(num_input_images); + turn_state.resolved_config = Some(input); + self.maybe_emit_turn_event(&turn_id, out); + } + + fn ingest_turn_token_usage( + &mut self, + input: TurnTokenUsageFact, + out: &mut Vec, + ) { + let turn_id = input.turn_id.clone(); + let turn_state = self.turns.entry(turn_id.clone()).or_insert(TurnState { + connection_id: None, + thread_id: None, + num_input_images: None, + resolved_config: None, + started_at: None, + token_usage: None, + completed: None, + steer_count: 0, + }); + turn_state.thread_id = Some(input.thread_id); + turn_state.token_usage = Some(input.token_usage); + self.maybe_emit_turn_event(&turn_id, out); + } + async fn ingest_skill_invoked( &mut self, input: SkillInvokedInput, @@ -316,30 +475,193 @@ impl AnalyticsReducer { response: ClientResponse, out: &mut Vec, ) { - let (thread, model, initialization_mode) = match response { - ClientResponse::ThreadStart { response, .. } => ( - response.thread, - response.model, - ThreadInitializationMode::New, - ), - ClientResponse::ThreadResume { response, .. } => ( - response.thread, - response.model, - ThreadInitializationMode::Resumed, - ), - ClientResponse::ThreadFork { response, .. } => ( - response.thread, - response.model, - ThreadInitializationMode::Forked, - ), - _ => return, + match response { + ClientResponse::ThreadStart { response, .. } => { + self.emit_thread_initialized( + connection_id, + response.thread, + response.model, + ThreadInitializationMode::New, + out, + ); + } + ClientResponse::ThreadResume { response, .. } => { + self.emit_thread_initialized( + connection_id, + response.thread, + response.model, + ThreadInitializationMode::Resumed, + out, + ); + } + ClientResponse::ThreadFork { response, .. } => { + self.emit_thread_initialized( + connection_id, + response.thread, + response.model, + ThreadInitializationMode::Forked, + out, + ); + } + ClientResponse::TurnStart { + request_id, + response, + } => { + let turn_id = response.turn.id; + let Some(RequestState::TurnStart(pending_request)) = + self.requests.remove(&(connection_id, request_id)) + else { + return; + }; + let turn_state = self.turns.entry(turn_id.clone()).or_insert(TurnState { + connection_id: None, + thread_id: None, + num_input_images: None, + resolved_config: None, + started_at: None, + token_usage: None, + completed: None, + steer_count: 0, + }); + turn_state.connection_id = Some(connection_id); + turn_state.thread_id = Some(pending_request.thread_id); + turn_state.num_input_images = Some(pending_request.num_input_images); + self.maybe_emit_turn_event(&turn_id, out); + } + ClientResponse::TurnSteer { + request_id, + response, + } => { + self.ingest_turn_steer_response(connection_id, request_id, response, out); + } + _ => {} + } + } + + fn ingest_error_response( + &mut self, + connection_id: u64, + request_id: RequestId, + error_type: Option, + out: &mut Vec, + ) { + let Some(request) = self.requests.remove(&(connection_id, request_id)) else { + return; }; + self.ingest_request_error_response(connection_id, request, error_type, out); + } + + fn ingest_request_error_response( + &mut self, + connection_id: u64, + request: RequestState, + error_type: Option, + out: &mut Vec, + ) { + match request { + RequestState::TurnStart(_) => {} + RequestState::TurnSteer(pending_request) => { + self.ingest_turn_steer_error_response( + connection_id, + pending_request, + error_type, + out, + ); + } + } + } + + fn ingest_turn_steer_error_response( + &mut self, + connection_id: u64, + pending_request: PendingTurnSteerState, + error_type: Option, + out: &mut Vec, + ) { + self.emit_turn_steer_event( + connection_id, + pending_request, + /*accepted_turn_id*/ None, + TurnSteerResult::Rejected, + rejection_reason_from_error_type(error_type), + out, + ); + } + + fn ingest_notification( + &mut self, + notification: ServerNotification, + out: &mut Vec, + ) { + match notification { + ServerNotification::TurnStarted(notification) => { + let turn_state = self.turns.entry(notification.turn.id).or_insert(TurnState { + connection_id: None, + thread_id: None, + num_input_images: None, + resolved_config: None, + started_at: None, + token_usage: None, + completed: None, + steer_count: 0, + }); + turn_state.started_at = notification + .turn + .started_at + .and_then(|started_at| u64::try_from(started_at).ok()); + } + ServerNotification::TurnCompleted(notification) => { + let turn_state = + self.turns + .entry(notification.turn.id.clone()) + .or_insert(TurnState { + connection_id: None, + thread_id: None, + num_input_images: None, + resolved_config: None, + started_at: None, + token_usage: None, + completed: None, + steer_count: 0, + }); + turn_state.completed = Some(CompletedTurnState { + status: analytics_turn_status(notification.turn.status), + turn_error: notification + .turn + .error + .and_then(|error| error.codex_error_info), + completed_at: notification + .turn + .completed_at + .and_then(|completed_at| u64::try_from(completed_at).ok()) + .unwrap_or_default(), + duration_ms: notification + .turn + .duration_ms + .and_then(|duration_ms| u64::try_from(duration_ms).ok()), + }); + let turn_id = notification.turn.id; + self.maybe_emit_turn_event(&turn_id, out); + } + _ => {} + } + } + + fn emit_thread_initialized( + &mut self, + connection_id: u64, + thread: codex_app_server_protocol::Thread, + model: String, + initialization_mode: ThreadInitializationMode, + out: &mut Vec, + ) { let thread_source: SessionSource = thread.source.into(); let thread_id = thread.id; let Some(connection_state) = self.connections.get(&connection_id) else { return; }; - let thread_metadata = ThreadMetadataState::from_session_source(&thread_source); + let thread_metadata = + ThreadMetadataState::from_thread_metadata(&thread_source, initialization_mode); self.thread_connections .insert(thread_id.clone(), connection_id); self.thread_metadata @@ -403,6 +725,275 @@ impl AnalyticsReducer { }, ))); } + + fn ingest_turn_steer_response( + &mut self, + connection_id: u64, + request_id: RequestId, + response: TurnSteerResponse, + out: &mut Vec, + ) { + let Some(RequestState::TurnSteer(pending_request)) = + self.requests.remove(&(connection_id, request_id)) + else { + return; + }; + if let Some(turn_state) = self.turns.get_mut(&response.turn_id) { + turn_state.steer_count += 1; + } + self.emit_turn_steer_event( + connection_id, + pending_request, + Some(response.turn_id), + TurnSteerResult::Accepted, + /*rejection_reason*/ None, + out, + ); + } + + fn emit_turn_steer_event( + &mut self, + connection_id: u64, + pending_request: PendingTurnSteerState, + accepted_turn_id: Option, + result: TurnSteerResult, + rejection_reason: Option, + out: &mut Vec, + ) { + let Some(connection_state) = self.connections.get(&connection_id) else { + return; + }; + let Some(thread_metadata) = self.thread_metadata.get(&pending_request.thread_id) else { + tracing::warn!( + thread_id = %pending_request.thread_id, + "dropping turn steer analytics event: missing thread lifecycle metadata" + ); + return; + }; + out.push(TrackEventRequest::TurnSteer(CodexTurnSteerEventRequest { + event_type: "codex_turn_steer_event", + event_params: CodexTurnSteerEventParams { + thread_id: pending_request.thread_id, + expected_turn_id: Some(pending_request.expected_turn_id), + accepted_turn_id, + app_server_client: connection_state.app_server_client.clone(), + runtime: connection_state.runtime.clone(), + thread_source: thread_metadata.thread_source.map(str::to_string), + subagent_source: thread_metadata.subagent_source.clone(), + parent_thread_id: thread_metadata.parent_thread_id.clone(), + num_input_images: pending_request.num_input_images, + result, + rejection_reason, + created_at: pending_request.created_at, + }, + })); + } + + fn maybe_emit_turn_event(&mut self, turn_id: &str, out: &mut Vec) { + let Some(turn_state) = self.turns.get(turn_id) else { + return; + }; + if turn_state.thread_id.is_none() + || turn_state.num_input_images.is_none() + || turn_state.resolved_config.is_none() + || turn_state.completed.is_none() + { + return; + } + let connection_metadata = turn_state + .connection_id + .and_then(|connection_id| self.connections.get(&connection_id)) + .map(|connection_state| { + ( + connection_state.app_server_client.clone(), + connection_state.runtime.clone(), + ) + }); + let Some((app_server_client, runtime)) = connection_metadata else { + if let Some(connection_id) = turn_state.connection_id { + tracing::warn!( + turn_id, + connection_id, + "dropping turn analytics event: missing connection metadata" + ); + } + return; + }; + let Some(thread_id) = turn_state.thread_id.as_ref() else { + return; + }; + let Some(thread_metadata) = self.thread_metadata.get(thread_id) else { + tracing::warn!( + thread_id, + turn_id, + "dropping turn analytics event: missing thread lifecycle metadata" + ); + return; + }; + out.push(TrackEventRequest::TurnEvent(Box::new( + CodexTurnEventRequest { + event_type: "codex_turn_event", + event_params: codex_turn_event_params( + app_server_client, + runtime, + turn_id.to_string(), + turn_state, + thread_metadata, + ), + }, + ))); + self.turns.remove(turn_id); + } +} + +fn codex_turn_event_params( + app_server_client: CodexAppServerClientMetadata, + runtime: CodexRuntimeMetadata, + turn_id: String, + turn_state: &TurnState, + thread_metadata: &ThreadMetadataState, +) -> CodexTurnEventParams { + let (Some(thread_id), Some(num_input_images), Some(resolved_config), Some(completed)) = ( + turn_state.thread_id.clone(), + turn_state.num_input_images, + turn_state.resolved_config.clone(), + turn_state.completed.clone(), + ) else { + unreachable!("turn event params require a fully populated turn state"); + }; + let started_at = turn_state.started_at; + let TurnResolvedConfigFact { + turn_id: _resolved_turn_id, + thread_id: _resolved_thread_id, + num_input_images: _resolved_num_input_images, + submission_type, + ephemeral, + session_source: _session_source, + model, + model_provider, + sandbox_policy, + reasoning_effort, + reasoning_summary, + service_tier, + approval_policy, + approvals_reviewer, + sandbox_network_access, + collaboration_mode, + personality, + is_first_turn, + } = resolved_config; + let token_usage = turn_state.token_usage.clone(); + CodexTurnEventParams { + thread_id, + turn_id, + app_server_client, + runtime, + submission_type, + ephemeral, + thread_source: thread_metadata.thread_source.map(str::to_string), + initialization_mode: thread_metadata.initialization_mode, + subagent_source: thread_metadata.subagent_source.clone(), + parent_thread_id: thread_metadata.parent_thread_id.clone(), + model: Some(model), + model_provider, + sandbox_policy: Some(sandbox_policy_mode(&sandbox_policy)), + reasoning_effort: reasoning_effort.map(|value| value.to_string()), + reasoning_summary: reasoning_summary_mode(reasoning_summary), + service_tier: service_tier + .map(|value| value.to_string()) + .unwrap_or_else(|| "default".to_string()), + approval_policy: approval_policy.to_string(), + approvals_reviewer: approvals_reviewer.to_string(), + sandbox_network_access, + collaboration_mode: Some(collaboration_mode_mode(collaboration_mode)), + personality: personality_mode(personality), + num_input_images, + is_first_turn, + status: completed.status, + turn_error: completed.turn_error, + steer_count: Some(turn_state.steer_count), + total_tool_call_count: None, + shell_command_count: None, + file_change_count: None, + mcp_tool_call_count: None, + dynamic_tool_call_count: None, + subagent_tool_call_count: None, + web_search_count: None, + image_generation_count: None, + input_tokens: token_usage + .as_ref() + .map(|token_usage| token_usage.input_tokens), + cached_input_tokens: token_usage + .as_ref() + .map(|token_usage| token_usage.cached_input_tokens), + output_tokens: token_usage + .as_ref() + .map(|token_usage| token_usage.output_tokens), + reasoning_output_tokens: token_usage + .as_ref() + .map(|token_usage| token_usage.reasoning_output_tokens), + total_tokens: token_usage + .as_ref() + .map(|token_usage| token_usage.total_tokens), + duration_ms: completed.duration_ms, + started_at, + completed_at: Some(completed.completed_at), + } +} + +fn sandbox_policy_mode(sandbox_policy: &SandboxPolicy) -> &'static str { + match sandbox_policy { + SandboxPolicy::DangerFullAccess => "full_access", + SandboxPolicy::ReadOnly { .. } => "read_only", + SandboxPolicy::WorkspaceWrite { .. } => "workspace_write", + SandboxPolicy::ExternalSandbox { .. } => "external_sandbox", + } +} + +fn collaboration_mode_mode(mode: ModeKind) -> &'static str { + match mode { + ModeKind::Plan => "plan", + ModeKind::Default | ModeKind::PairProgramming | ModeKind::Execute => "default", + } +} + +fn reasoning_summary_mode(summary: Option) -> Option { + match summary { + Some(ReasoningSummary::None) | None => None, + Some(summary) => Some(summary.to_string()), + } +} + +fn personality_mode(personality: Option) -> Option { + match personality { + Some(Personality::None) | None => None, + Some(personality) => Some(personality.to_string()), + } +} + +fn analytics_turn_status(status: codex_app_server_protocol::TurnStatus) -> Option { + match status { + codex_app_server_protocol::TurnStatus::Completed => Some(TurnStatus::Completed), + codex_app_server_protocol::TurnStatus::Failed => Some(TurnStatus::Failed), + codex_app_server_protocol::TurnStatus::Interrupted => Some(TurnStatus::Interrupted), + codex_app_server_protocol::TurnStatus::InProgress => None, + } +} + +fn num_input_images(input: &[UserInput]) -> usize { + input + .iter() + .filter(|item| matches!(item, UserInput::Image { .. } | UserInput::LocalImage { .. })) + .count() +} + +fn rejection_reason_from_error_type( + error_type: Option, +) -> Option { + match error_type? { + AnalyticsJsonRpcError::TurnSteer(error) => Some(error.into()), + AnalyticsJsonRpcError::Input(error) => Some(error.into()), + } } pub(crate) fn skill_id_for_local_skill( diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index 8d0d40ff94..de9f1245e5 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -12,6 +12,7 @@ use crate::thread_state::TurnSummary; use crate::thread_state::resolve_server_request_on_thread_listener; use crate::thread_status::ThreadWatchActiveGuard; use crate::thread_status::ThreadWatchManager; +use codex_analytics::AnalyticsEventsClient; use codex_app_server_protocol::AccountRateLimitsUpdatedNotification; use codex_app_server_protocol::AdditionalPermissionProfile as V2AdditionalPermissionProfile; use codex_app_server_protocol::AgentMessageDeltaNotification; @@ -167,6 +168,7 @@ pub(crate) async fn apply_bespoke_event_handling( conversation_id: ThreadId, conversation: Arc, thread_manager: Arc, + analytics_events_client: Option, outgoing: ThreadScopedOutgoingMessageSender, thread_state: Arc>, thread_watch_manager: ThreadWatchManager, @@ -202,6 +204,10 @@ pub(crate) async fn apply_bespoke_event_handling( thread_id: conversation_id.to_string(), turn, }; + if let Some(analytics_events_client) = analytics_events_client.as_ref() { + analytics_events_client + .track_notification(ServerNotification::TurnStarted(notification.clone())); + } outgoing .send_server_notification(ServerNotification::TurnStarted(notification)) .await; @@ -218,6 +224,7 @@ pub(crate) async fn apply_bespoke_event_handling( conversation_id, event_turn_id, turn_complete_event, + analytics_events_client.as_ref(), &outgoing, &thread_state, ) @@ -1773,6 +1780,7 @@ pub(crate) async fn apply_bespoke_event_handling( conversation_id, event_turn_id, turn_aborted_event, + analytics_events_client.as_ref(), &outgoing, &thread_state, ) @@ -1950,6 +1958,7 @@ async fn emit_turn_completed_with_status( conversation_id: ThreadId, event_turn_id: String, turn_completion_metadata: TurnCompletionMetadata, + analytics_events_client: Option<&AnalyticsEventsClient>, outgoing: &ThreadScopedOutgoingMessageSender, ) { let notification = TurnCompletedNotification { @@ -1964,6 +1973,10 @@ async fn emit_turn_completed_with_status( duration_ms: turn_completion_metadata.duration_ms, }, }; + if let Some(analytics_events_client) = analytics_events_client { + analytics_events_client + .track_notification(ServerNotification::TurnCompleted(notification.clone())); + } outgoing .send_server_notification(ServerNotification::TurnCompleted(notification)) .await; @@ -2156,6 +2169,7 @@ async fn handle_turn_complete( conversation_id: ThreadId, event_turn_id: String, turn_complete_event: TurnCompleteEvent, + analytics_events_client: Option<&AnalyticsEventsClient>, outgoing: &ThreadScopedOutgoingMessageSender, thread_state: &Arc>, ) { @@ -2176,6 +2190,7 @@ async fn handle_turn_complete( completed_at: turn_complete_event.completed_at, duration_ms: turn_complete_event.duration_ms, }, + analytics_events_client, outgoing, ) .await; @@ -2185,6 +2200,7 @@ async fn handle_turn_interrupted( conversation_id: ThreadId, event_turn_id: String, turn_aborted_event: TurnAbortedEvent, + analytics_events_client: Option<&AnalyticsEventsClient>, outgoing: &ThreadScopedOutgoingMessageSender, thread_state: &Arc>, ) { @@ -2200,6 +2216,7 @@ async fn handle_turn_interrupted( completed_at: turn_aborted_event.completed_at, duration_ms: turn_aborted_event.duration_ms, }, + analytics_events_client, outgoing, ) .await; @@ -2940,6 +2957,7 @@ mod tests { use codex_app_server_protocol::GuardianApprovalReviewStatus; use codex_app_server_protocol::JSONRPCErrorError; use codex_app_server_protocol::TurnPlanStepStatus; + use codex_login::AuthManager; use codex_login::CodexAuth; use codex_protocol::items::HookPromptFragment; use codex_protocol::items::build_hook_prompt_message; @@ -3070,6 +3088,7 @@ mod tests { outgoing: ThreadScopedOutgoingMessageSender, thread_state: Arc>, thread_watch_manager: ThreadWatchManager, + analytics_events_client: AnalyticsEventsClient, codex_home: PathBuf, } @@ -3084,6 +3103,7 @@ mod tests { self.conversation_id, self.conversation.clone(), self.thread_manager.clone(), + Some(self.analytics_events_client.clone()), self.outgoing.clone(), self.thread_state.clone(), self.thread_watch_manager.clone(), @@ -3410,6 +3430,13 @@ mod tests { outgoing: outgoing.clone(), thread_state: thread_state.clone(), thread_watch_manager: thread_watch_manager.clone(), + analytics_events_client: AnalyticsEventsClient::new( + AuthManager::from_auth_for_testing( + CodexAuth::create_dummy_chatgpt_auth_for_testing(), + ), + "http://localhost".to_string(), + Some(false), + ), codex_home: codex_home.path().to_path_buf(), }; @@ -3860,6 +3887,7 @@ mod tests { conversation_id, event_turn_id.clone(), turn_complete_event(&event_turn_id), + /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -3908,6 +3936,7 @@ mod tests { conversation_id, event_turn_id.clone(), turn_aborted_event(&event_turn_id), + /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -3955,6 +3984,7 @@ mod tests { conversation_id, event_turn_id.clone(), turn_complete_event(&event_turn_id), + /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -4221,6 +4251,7 @@ mod tests { conversation_a, a_turn1.clone(), turn_complete_event(&a_turn1), + /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -4242,6 +4273,7 @@ mod tests { conversation_b, b_turn1.clone(), turn_complete_event(&b_turn1), + /*analytics_events_client*/ None, &outgoing, &thread_state, ) @@ -4253,6 +4285,7 @@ mod tests { conversation_a, a_turn2.clone(), turn_complete_event(&a_turn2), + /*analytics_events_client*/ None, &outgoing, &thread_state, ) diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 4feda2ab06..9f307eb247 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -22,6 +22,9 @@ use chrono::DateTime; use chrono::SecondsFormat; use chrono::Utc; use codex_analytics::AnalyticsEventsClient; +use codex_analytics::AnalyticsJsonRpcError; +use codex_analytics::InputError; +use codex_analytics::TurnSteerRequestError; use codex_app_server_protocol::Account; use codex_app_server_protocol::AccountLoginCompletedNotification; use codex_app_server_protocol::AccountUpdatedNotification; @@ -36,7 +39,7 @@ use codex_app_server_protocol::CancelLoginAccountResponse; use codex_app_server_protocol::CancelLoginAccountStatus; use codex_app_server_protocol::ClientRequest; use codex_app_server_protocol::ClientResponse; -use codex_app_server_protocol::CodexErrorInfo as AppServerCodexErrorInfo; +use codex_app_server_protocol::CodexErrorInfo; use codex_app_server_protocol::CollaborationModeListParams; use codex_app_server_protocol::CollaborationModeListResponse; use codex_app_server_protocol::CommandExecParams; @@ -642,6 +645,22 @@ impl CodexMessageProcessor { } } + fn track_error_response( + &self, + request_id: &ConnectionRequestId, + error: &JSONRPCErrorError, + error_type: Option, + ) { + if self.config.features.enabled(Feature::GeneralAnalytics) { + self.analytics_events_client.track_error_response( + request_id.connection_id.0, + request_id.request_id.clone(), + error.clone(), + error_type, + ); + } + } + async fn load_thread( &self, thread_id: &str, @@ -6920,12 +6939,18 @@ impl CodexMessageProcessor { app_server_client_version: Option, ) { if let Err(error) = Self::validate_v2_input_limit(¶ms.input) { + self.track_error_response( + &request_id, + &error, + Some(AnalyticsJsonRpcError::Input(InputError::TooLarge)), + ); self.outgoing.send_error(request_id, error).await; return; } let (_, thread) = match self.load_thread(¶ms.thread_id).await { Ok(v) => v, Err(error) => { + self.track_error_response(&request_id, &error, /*error_type*/ None); self.outgoing.send_error(request_id, error).await; return; } @@ -6937,6 +6962,7 @@ impl CodexMessageProcessor { ) .await { + self.track_error_response(&request_id, &error, /*error_type*/ None); self.outgoing.send_error(request_id, error).await; return; } @@ -7020,6 +7046,15 @@ impl CodexMessageProcessor { }; let response = TurnStartResponse { turn }; + if self.config.features.enabled(Feature::GeneralAnalytics) { + self.analytics_events_client.track_response( + request_id.connection_id.0, + ClientResponse::TurnStart { + request_id: request_id.request_id.clone(), + response: response.clone(), + }, + ); + } self.outgoing.send_response(request_id, response).await; } Err(err) => { @@ -7028,6 +7063,7 @@ impl CodexMessageProcessor { message: format!("failed to start turn: {err}"), data: None, }; + self.track_error_response(&request_id, &error, /*error_type*/ None); self.outgoing.send_error(request_id, error).await; } } @@ -7101,6 +7137,7 @@ impl CodexMessageProcessor { let (_, thread) = match self.load_thread(¶ms.thread_id).await { Ok(v) => v, Err(error) => { + self.track_error_response(&request_id, &error, /*error_type*/ None); self.outgoing.send_error(request_id, error).await; return; } @@ -7118,6 +7155,11 @@ impl CodexMessageProcessor { .record_request_turn_id(&request_id, ¶ms.expected_turn_id) .await; if let Err(error) = Self::validate_v2_input_limit(¶ms.input) { + self.track_error_response( + &request_id, + &error, + Some(AnalyticsJsonRpcError::Input(InputError::TooLarge)), + ); self.outgoing.send_error(request_id, error).await; return; } @@ -7138,36 +7180,51 @@ impl CodexMessageProcessor { { Ok(turn_id) => { let response = TurnSteerResponse { turn_id }; + if self.config.features.enabled(Feature::GeneralAnalytics) { + self.analytics_events_client.track_response( + request_id.connection_id.0, + ClientResponse::TurnSteer { + request_id: request_id.request_id.clone(), + response: response.clone(), + }, + ); + } self.outgoing.send_response(request_id, response).await; } Err(err) => { - let (code, message, data) = match err { + let (code, message, data, error_type) = match err { SteerInputError::NoActiveTurn(_) => ( INVALID_REQUEST_ERROR_CODE, "no active turn to steer".to_string(), None, + Some(AnalyticsJsonRpcError::TurnSteer( + TurnSteerRequestError::NoActiveTurn, + )), ), SteerInputError::ExpectedTurnMismatch { expected, actual } => ( INVALID_REQUEST_ERROR_CODE, format!("expected active turn id `{expected}` but found `{actual}`"), None, + Some(AnalyticsJsonRpcError::TurnSteer( + TurnSteerRequestError::ExpectedTurnMismatch, + )), ), SteerInputError::ActiveTurnNotSteerable { turn_kind } => { - let message = match turn_kind { - codex_protocol::protocol::NonSteerableTurnKind::Review => { - "cannot steer a review turn".to_string() - } - codex_protocol::protocol::NonSteerableTurnKind::Compact => { - "cannot steer a compact turn".to_string() - } + let (message, turn_steer_error) = match turn_kind { + codex_protocol::protocol::NonSteerableTurnKind::Review => ( + "cannot steer a review turn".to_string(), + TurnSteerRequestError::NonSteerableReview, + ), + codex_protocol::protocol::NonSteerableTurnKind::Compact => ( + "cannot steer a compact turn".to_string(), + TurnSteerRequestError::NonSteerableCompact, + ), }; let error = TurnError { message: message.clone(), - codex_error_info: Some( - AppServerCodexErrorInfo::ActiveTurnNotSteerable { - turn_kind: turn_kind.into(), - }, - ), + codex_error_info: Some(CodexErrorInfo::ActiveTurnNotSteerable { + turn_kind: turn_kind.into(), + }), additional_details: None, }; let data = match serde_json::to_value(error) { @@ -7180,12 +7237,18 @@ impl CodexMessageProcessor { None } }; - (INVALID_REQUEST_ERROR_CODE, message, data) + ( + INVALID_REQUEST_ERROR_CODE, + message, + data, + Some(AnalyticsJsonRpcError::TurnSteer(turn_steer_error)), + ) } SteerInputError::EmptyInput => ( INVALID_REQUEST_ERROR_CODE, "input must not be empty".to_string(), None, + Some(AnalyticsJsonRpcError::Input(InputError::Empty)), ), }; let error = JSONRPCErrorError { @@ -7193,6 +7256,7 @@ impl CodexMessageProcessor { message, data, }; + self.track_error_response(&request_id, &error, error_type); self.outgoing.send_error(request_id, error).await; } } @@ -7940,6 +8004,9 @@ impl CodexMessageProcessor { conversation_id, conversation.clone(), thread_manager.clone(), + listener_task_context + .general_analytics_enabled + .then(|| listener_task_context.analytics_events_client.clone()), thread_outgoing, thread_state.clone(), thread_watch_manager.clone(), diff --git a/codex-rs/app-server/src/main.rs b/codex-rs/app-server/src/main.rs index 9a23680fb9..d896f2f8ec 100644 --- a/codex-rs/app-server/src/main.rs +++ b/codex-rs/app-server/src/main.rs @@ -12,6 +12,7 @@ use std::path::PathBuf; // Debug-only test hook: lets integration tests point the server at a temporary // managed config file without writing to /etc. const MANAGED_CONFIG_PATH_ENV_VAR: &str = "CODEX_APP_SERVER_MANAGED_CONFIG_PATH"; +const DISABLE_MANAGED_CONFIG_ENV_VAR: &str = "CODEX_APP_SERVER_DISABLE_MANAGED_CONFIG"; #[derive(Debug, Parser)] struct AppServerArgs { @@ -40,10 +41,13 @@ struct AppServerArgs { fn main() -> anyhow::Result<()> { arg0_dispatch_or_else(|arg0_paths: Arg0DispatchPaths| async move { let args = AppServerArgs::parse(); - let managed_config_path = managed_config_path_from_debug_env(); - let loader_overrides = LoaderOverrides { - managed_config_path, - ..Default::default() + let loader_overrides = if disable_managed_config_from_debug_env() { + LoaderOverrides::without_managed_config_for_tests() + } else { + LoaderOverrides { + managed_config_path: managed_config_path_from_debug_env(), + ..Default::default() + } }; let transport = args.listen; let session_source = args.session_source; @@ -63,6 +67,17 @@ fn main() -> anyhow::Result<()> { }) } +fn disable_managed_config_from_debug_env() -> bool { + #[cfg(debug_assertions)] + { + if let Ok(value) = std::env::var(DISABLE_MANAGED_CONFIG_ENV_VAR) { + return matches!(value.as_str(), "1" | "true" | "TRUE" | "yes" | "YES"); + } + } + + false +} + fn managed_config_path_from_debug_env() -> Option { #[cfg(debug_assertions)] { diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index 221bdf872a..7bfc01b2a1 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -665,6 +665,16 @@ impl MessageProcessor { self.outgoing.send_error(connection_request_id, error).await; return; } + if self.config.features.enabled(Feature::GeneralAnalytics) + && let ClientRequest::TurnStart { request_id, .. } + | ClientRequest::TurnSteer { request_id, .. } = &codex_request + { + self.analytics_events_client.track_request( + connection_id.0, + request_id.clone(), + codex_request.clone(), + ); + } match codex_request { ClientRequest::ConfigRead { request_id, params } => { diff --git a/codex-rs/app-server/tests/common/config.rs b/codex-rs/app-server/tests/common/config.rs index deb16c6322..1ac2572fa2 100644 --- a/codex-rs/app-server/tests/common/config.rs +++ b/codex-rs/app-server/tests/common/config.rs @@ -78,3 +78,31 @@ model_provider = "{model_provider_id}" ), ) } + +pub fn write_mock_responses_config_toml_with_chatgpt_base_url( + codex_home: &Path, + server_uri: &str, + chatgpt_base_url: &str, +) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + format!( + r#" +model = "mock-model" +approval_policy = "never" +sandbox_mode = "read-only" +chatgpt_base_url = "{chatgpt_base_url}" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "{server_uri}/v1" +wire_api = "responses" +request_max_retries = 0 +stream_max_retries = 0 +"# + ), + ) +} diff --git a/codex-rs/app-server/tests/common/lib.rs b/codex-rs/app-server/tests/common/lib.rs index 3f89765851..90553760d9 100644 --- a/codex-rs/app-server/tests/common/lib.rs +++ b/codex-rs/app-server/tests/common/lib.rs @@ -14,6 +14,7 @@ pub use auth_fixtures::encode_id_token; pub use auth_fixtures::write_chatgpt_auth; use codex_app_server_protocol::JSONRPCResponse; pub use config::write_mock_responses_config_toml; +pub use config::write_mock_responses_config_toml_with_chatgpt_base_url; pub use core_test_support::format_with_current_shell; pub use core_test_support::format_with_current_shell_display; pub use core_test_support::format_with_current_shell_display_non_login; diff --git a/codex-rs/app-server/tests/common/mcp_process.rs b/codex-rs/app-server/tests/common/mcp_process.rs index afb095aa71..eddab545a5 100644 --- a/codex-rs/app-server/tests/common/mcp_process.rs +++ b/codex-rs/app-server/tests/common/mcp_process.rs @@ -102,12 +102,17 @@ pub struct McpProcess { } pub const DEFAULT_CLIENT_NAME: &str = "codex-app-server-tests"; +const DISABLE_MANAGED_CONFIG_ENV_VAR: &str = "CODEX_APP_SERVER_DISABLE_MANAGED_CONFIG"; impl McpProcess { pub async fn new(codex_home: &Path) -> anyhow::Result { Self::new_with_env_and_args(codex_home, &[], &[]).await } + pub async fn new_without_managed_config(codex_home: &Path) -> anyhow::Result { + Self::new_with_env(codex_home, &[(DISABLE_MANAGED_CONFIG_ENV_VAR, Some("1"))]).await + } + pub async fn new_with_args(codex_home: &Path, args: &[&str]) -> anyhow::Result { Self::new_with_env_and_args(codex_home, &[], args).await } diff --git a/codex-rs/app-server/tests/suite/v2/analytics.rs b/codex-rs/app-server/tests/suite/v2/analytics.rs index a4d7a7f349..a3ecdbc1f4 100644 --- a/codex-rs/app-server/tests/suite/v2/analytics.rs +++ b/codex-rs/app-server/tests/suite/v2/analytics.rs @@ -80,6 +80,24 @@ async fn app_server_default_analytics_enabled_with_flag() -> Result<()> { } pub(crate) async fn enable_analytics_capture(server: &MockServer, codex_home: &Path) -> Result<()> { + let config_path = codex_home.join("config.toml"); + let config_toml = std::fs::read_to_string(&config_path)?; + if !config_toml.contains("[features]") { + std::fs::write( + &config_path, + format!("{config_toml}\n[features]\ngeneral_analytics = true\n"), + )?; + } else if !config_toml.contains("general_analytics") { + std::fs::write( + &config_path, + config_toml.replace("[features]\n", "[features]\ngeneral_analytics = true\n"), + )?; + } + + mount_analytics_capture(server, codex_home).await +} + +pub(crate) async fn mount_analytics_capture(server: &MockServer, codex_home: &Path) -> Result<()> { Mock::given(method("POST")) .and(path("/codex/analytics-events/events")) .respond_with(ResponseTemplate::new(200)) @@ -120,6 +138,41 @@ pub(crate) async fn wait_for_analytics_payload( serde_json::from_slice(&body).map_err(|err| anyhow::anyhow!("invalid analytics payload: {err}")) } +pub(crate) async fn wait_for_analytics_event( + server: &MockServer, + read_timeout: Duration, + event_type: &str, +) -> Result { + timeout(read_timeout, async { + loop { + let Some(requests) = server.received_requests().await else { + tokio::time::sleep(Duration::from_millis(25)).await; + continue; + }; + for request in &requests { + if request.method != "POST" + || request.url.path() != "/codex/analytics-events/events" + { + continue; + } + let payload: Value = serde_json::from_slice(&request.body) + .map_err(|err| anyhow::anyhow!("invalid analytics payload: {err}"))?; + let Some(events) = payload["events"].as_array() else { + continue; + }; + if let Some(event) = events + .iter() + .find(|event| event["event_type"] == event_type) + { + return Ok::(event.clone()); + } + } + tokio::time::sleep(Duration::from_millis(25)).await; + } + }) + .await? +} + pub(crate) fn thread_initialized_event(payload: &Value) -> Result<&Value> { let events = payload["events"] .as_array() diff --git a/codex-rs/app-server/tests/suite/v2/thread_fork.rs b/codex-rs/app-server/tests/suite/v2/thread_fork.rs index 9907fc4b1d..19bf00f64a 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_fork.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_fork.rs @@ -205,7 +205,7 @@ async fn thread_fork_tracks_thread_initialized_analytics() -> Result<()> { /*git_info*/ None, )?; - let mut mcp = McpProcess::new(codex_home.path()).await?; + let mut mcp = McpProcess::new_without_managed_config(codex_home.path()).await?; timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let fork_id = mcp @@ -565,7 +565,7 @@ fn create_config_toml_with_chatgpt_base_url( let general_analytics_toml = if general_analytics_enabled { "\ngeneral_analytics = true".to_string() } else { - String::new() + "\ngeneral_analytics = false".to_string() }; let config_toml = codex_home.join("config.toml"); std::fs::write( diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs index 80ae756888..0a302c2380 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_resume.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs @@ -178,7 +178,7 @@ async fn thread_resume_tracks_thread_initialized_analytics() -> Result<()> { /*git_info*/ None, )?; - let mut mcp = McpProcess::new(codex_home.path()).await?; + let mut mcp = McpProcess::new_without_managed_config(codex_home.path()).await?; timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let resume_id = mcp @@ -1901,7 +1901,7 @@ fn create_config_toml_with_chatgpt_base_url( let general_analytics_toml = if general_analytics_enabled { "\ngeneral_analytics = true".to_string() } else { - String::new() + "\ngeneral_analytics = false".to_string() }; let config_toml = codex_home.join("config.toml"); std::fs::write( diff --git a/codex-rs/app-server/tests/suite/v2/thread_start.rs b/codex-rs/app-server/tests/suite/v2/thread_start.rs index c0df2cab07..978260f8e2 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_start.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_start.rs @@ -40,8 +40,9 @@ use wiremock::matchers::method; use wiremock::matchers::path; use super::analytics::assert_basic_thread_initialized_event; -use super::analytics::enable_analytics_capture; +use super::analytics::mount_analytics_capture; use super::analytics::thread_initialized_event; +use super::analytics::wait_for_analytics_event; use super::analytics::wait_for_analytics_payload; const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); @@ -232,9 +233,9 @@ async fn thread_start_tracks_thread_initialized_analytics() -> Result<()> { &server.uri(), /*general_analytics_enabled*/ true, )?; - enable_analytics_capture(&server, codex_home.path()).await?; + mount_analytics_capture(&server, codex_home.path()).await?; - let mut mcp = McpProcess::new(codex_home.path()).await?; + let mut mcp = McpProcess::new_without_managed_config(codex_home.path()).await?; timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let req_id = mcp @@ -265,9 +266,9 @@ async fn thread_start_does_not_track_thread_initialized_analytics_without_featur &server.uri(), /*general_analytics_enabled*/ false, )?; - enable_analytics_capture(&server, codex_home.path()).await?; + mount_analytics_capture(&server, codex_home.path()).await?; - let mut mcp = McpProcess::new(codex_home.path()).await?; + let mut mcp = McpProcess::new_without_managed_config(codex_home.path()).await?; timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let req_id = mcp @@ -280,7 +281,12 @@ async fn thread_start_does_not_track_thread_initialized_analytics_without_featur .await??; let _ = to_response::(resp)?; - let payload = wait_for_analytics_payload(&server, Duration::from_millis(250)).await; + let payload = wait_for_analytics_event( + &server, + Duration::from_millis(250), + "codex_thread_initialized", + ) + .await; assert!( payload.is_err(), "thread analytics should be gated off when general_analytics is disabled" @@ -888,7 +894,7 @@ fn create_config_toml_with_chatgpt_base_url( let general_analytics_toml = if general_analytics_enabled { "\ngeneral_analytics = true".to_string() } else { - String::new() + "\ngeneral_analytics = false".to_string() }; let config_toml = codex_home.join("config.toml"); std::fs::write( diff --git a/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs b/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs index 2850c7b74f..b553137752 100644 --- a/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs +++ b/codex-rs/app-server/tests/suite/v2/turn_interrupt.rs @@ -3,6 +3,7 @@ use anyhow::Result; use app_test_support::McpProcess; use app_test_support::create_mock_responses_server_sequence; +use app_test_support::create_mock_responses_server_sequence_unchecked; use app_test_support::create_shell_command_sse_response; use app_test_support::to_response; use codex_app_server_protocol::JSONRPCNotification; @@ -43,14 +44,15 @@ async fn turn_interrupt_aborts_running_turn() -> Result<()> { std::fs::create_dir(&working_directory)?; // Mock server: long-running shell command then (after abort) nothing else needed. - let server = create_mock_responses_server_sequence(vec![create_shell_command_sse_response( - shell_command.clone(), - Some(&working_directory), - Some(10_000), - "call_sleep", - )?]) - .await; - create_config_toml(&codex_home, &server.uri(), "never", "danger-full-access")?; + let server = + create_mock_responses_server_sequence_unchecked(vec![create_shell_command_sse_response( + shell_command.clone(), + Some(&working_directory), + Some(10_000), + "call_sleep", + )?]) + .await; + create_config_toml(&codex_home, &server.uri(), "never", "workspace-write")?; let mut mcp = McpProcess::new(&codex_home).await?; timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; @@ -87,6 +89,7 @@ async fn turn_interrupt_aborts_running_turn() -> Result<()> { ) .await??; let TurnStartResponse { turn } = to_response::(turn_resp)?; + let turn_id = turn.id.clone(); // Give the command a brief moment to start. tokio::time::sleep(std::time::Duration::from_secs(1)).await; @@ -96,7 +99,7 @@ async fn turn_interrupt_aborts_running_turn() -> Result<()> { let interrupt_id = mcp .send_turn_interrupt_request(TurnInterruptParams { thread_id: thread_id.clone(), - turn_id: turn.id, + turn_id: turn_id.clone(), }) .await?; let interrupt_resp: JSONRPCResponse = timeout( diff --git a/codex-rs/app-server/tests/suite/v2/turn_start.rs b/codex-rs/app-server/tests/suite/v2/turn_start.rs index d81a5d27a0..694d0a6eef 100644 --- a/codex-rs/app-server/tests/suite/v2/turn_start.rs +++ b/codex-rs/app-server/tests/suite/v2/turn_start.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use app_test_support::DEFAULT_CLIENT_NAME; use app_test_support::McpProcess; use app_test_support::create_apply_patch_sse_response; use app_test_support::create_exec_command_sse_response; @@ -9,6 +10,7 @@ use app_test_support::create_mock_responses_server_sequence_unchecked; use app_test_support::create_shell_command_sse_response; use app_test_support::format_with_current_shell_display; use app_test_support::to_response; +use app_test_support::write_mock_responses_config_toml_with_chatgpt_base_url; use codex_app_server::INPUT_TOO_LARGE_ERROR_CODE; use codex_app_server::INVALID_PARAMS_ERROR_CODE; use codex_app_server_protocol::ByteRange; @@ -64,6 +66,10 @@ use std::path::Path; use tempfile::TempDir; use tokio::time::timeout; +use super::analytics::enable_analytics_capture; +use super::analytics::mount_analytics_capture; +use super::analytics::wait_for_analytics_event; + #[cfg(windows)] const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(25); #[cfg(not(windows))] @@ -328,6 +334,163 @@ async fn thread_start_omits_empty_instruction_overrides_from_model_request() -> Ok(()) } +#[tokio::test] +async fn turn_start_tracks_turn_event_analytics() -> Result<()> { + let responses = vec![create_final_assistant_message_sse_response("Done")?]; + let server = create_mock_responses_server_sequence_unchecked(responses).await; + + let codex_home = TempDir::new()?; + write_mock_responses_config_toml_with_chatgpt_base_url( + codex_home.path(), + &server.uri(), + &server.uri(), + )?; + enable_analytics_capture(&server, codex_home.path()).await?; + + let mut mcp = McpProcess::new_without_managed_config(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let thread_req = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("mock-model".to_string()), + ..Default::default() + }) + .await?; + let thread_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(thread_req)), + ) + .await??; + let ThreadStartResponse { thread, .. } = to_response::(thread_resp)?; + + let turn_req = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Image { + url: "https://example.com/a.png".to_string(), + }], + ..Default::default() + }) + .await?; + let turn_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(turn_req)), + ) + .await??; + let TurnStartResponse { turn } = to_response::(turn_resp)?; + + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("turn/completed"), + ) + .await??; + + let event = wait_for_analytics_event(&server, DEFAULT_READ_TIMEOUT, "codex_turn_event").await?; + assert_eq!(event["event_params"]["thread_id"], thread.id); + assert_eq!(event["event_params"]["turn_id"], turn.id); + assert_eq!( + event["event_params"]["app_server_client"]["product_client_id"], + DEFAULT_CLIENT_NAME + ); + assert_eq!(event["event_params"]["model"], "mock-model"); + assert_eq!(event["event_params"]["model_provider"], "mock_provider"); + assert_eq!(event["event_params"]["sandbox_policy"], "read_only"); + assert_eq!(event["event_params"]["ephemeral"], false); + assert_eq!(event["event_params"]["thread_source"], "user"); + assert_eq!(event["event_params"]["initialization_mode"], "new"); + assert_eq!( + event["event_params"]["subagent_source"], + serde_json::Value::Null + ); + assert_eq!( + event["event_params"]["parent_thread_id"], + serde_json::Value::Null + ); + assert_eq!(event["event_params"]["num_input_images"], 1); + assert_eq!(event["event_params"]["status"], "completed"); + assert!(event["event_params"]["started_at"].as_u64().is_some()); + assert!(event["event_params"]["completed_at"].as_u64().is_some()); + assert!(event["event_params"]["duration_ms"].as_u64().is_some()); + assert_eq!(event["event_params"]["input_tokens"], 0); + assert_eq!(event["event_params"]["cached_input_tokens"], 0); + assert_eq!(event["event_params"]["output_tokens"], 0); + assert_eq!(event["event_params"]["reasoning_output_tokens"], 0); + assert_eq!(event["event_params"]["total_tokens"], 0); + + Ok(()) +} + +#[tokio::test] +async fn turn_start_does_not_track_turn_event_analytics_without_feature() -> Result<()> { + let responses = vec![create_final_assistant_message_sse_response("Done")?]; + let server = create_mock_responses_server_sequence_unchecked(responses).await; + + let codex_home = TempDir::new()?; + write_mock_responses_config_toml_with_chatgpt_base_url( + codex_home.path(), + &server.uri(), + &server.uri(), + )?; + let config_path = codex_home.path().join("config.toml"); + let config_toml = std::fs::read_to_string(&config_path)?; + std::fs::write( + &config_path, + format!("{config_toml}\n[features]\ngeneral_analytics = false\n"), + )?; + mount_analytics_capture(&server, codex_home.path()).await?; + + let mut mcp = McpProcess::new_without_managed_config(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let thread_req = mcp + .send_thread_start_request(ThreadStartParams { + model: Some("mock-model".to_string()), + ..Default::default() + }) + .await?; + let thread_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(thread_req)), + ) + .await??; + let ThreadStartResponse { thread, .. } = to_response::(thread_resp)?; + + let turn_req = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id, + input: vec![V2UserInput::Text { + text: "hello".to_string(), + text_elements: Vec::new(), + }], + ..Default::default() + }) + .await?; + let turn_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(turn_req)), + ) + .await??; + let _ = to_response::(turn_resp)?; + + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("turn/completed"), + ) + .await??; + + let turn_event = wait_for_analytics_event( + &server, + std::time::Duration::from_millis(250), + "codex_turn_event", + ) + .await; + assert!( + turn_event.is_err(), + "turn analytics should be gated off when general_analytics is disabled" + ); + Ok(()) +} + #[tokio::test] async fn turn_start_accepts_text_at_limit_with_mention_item() -> Result<()> { let responses = vec![create_final_assistant_message_sse_response("Done")?]; diff --git a/codex-rs/app-server/tests/suite/v2/turn_steer.rs b/codex-rs/app-server/tests/suite/v2/turn_steer.rs index a93bf6c6ab..16e28d6cc5 100644 --- a/codex-rs/app-server/tests/suite/v2/turn_steer.rs +++ b/codex-rs/app-server/tests/suite/v2/turn_steer.rs @@ -6,6 +6,7 @@ use app_test_support::create_mock_responses_server_sequence; use app_test_support::create_mock_responses_server_sequence_unchecked; use app_test_support::create_shell_command_sse_response; use app_test_support::to_response; +use app_test_support::write_mock_responses_config_toml_with_chatgpt_base_url; use codex_app_server::INPUT_TOO_LARGE_ERROR_CODE; use codex_app_server::INVALID_PARAMS_ERROR_CODE; use codex_app_server_protocol::JSONRPCError; @@ -23,6 +24,9 @@ use codex_protocol::user_input::MAX_USER_INPUT_TEXT_CHARS; use tempfile::TempDir; use tokio::time::timeout; +use super::analytics::enable_analytics_capture; +use super::analytics::wait_for_analytics_event; + const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); #[tokio::test] @@ -32,9 +36,14 @@ async fn turn_steer_requires_active_turn() -> Result<()> { std::fs::create_dir(&codex_home)?; let server = create_mock_responses_server_sequence(vec![]).await; - create_config_toml(&codex_home, &server.uri())?; + write_mock_responses_config_toml_with_chatgpt_base_url( + &codex_home, + &server.uri(), + &server.uri(), + )?; + enable_analytics_capture(&server, &codex_home).await?; - let mut mcp = McpProcess::new(&codex_home).await?; + let mut mcp = McpProcess::new_without_managed_config(&codex_home).await?; timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let thread_req = mcp @@ -52,7 +61,7 @@ async fn turn_steer_requires_active_turn() -> Result<()> { let steer_req = mcp .send_turn_steer_request(TurnSteerParams { - thread_id: thread.id, + thread_id: thread.id.clone(), input: vec![V2UserInput::Text { text: "steer".to_string(), text_elements: Vec::new(), @@ -68,6 +77,21 @@ async fn turn_steer_requires_active_turn() -> Result<()> { .await??; assert_eq!(steer_err.error.code, -32600); + let event = + wait_for_analytics_event(&server, DEFAULT_READ_TIMEOUT, "codex_turn_steer_event").await?; + assert_eq!(event["event_params"]["thread_id"], thread.id); + assert_eq!(event["event_params"]["result"], "rejected"); + assert_eq!(event["event_params"]["num_input_images"], 0); + assert_eq!( + event["event_params"]["expected_turn_id"], + "turn-does-not-exist" + ); + assert_eq!( + event["event_params"]["accepted_turn_id"], + serde_json::Value::Null + ); + assert_eq!(event["event_params"]["rejection_reason"], "no_active_turn"); + Ok(()) } @@ -96,9 +120,14 @@ async fn turn_steer_rejects_oversized_text_input() -> Result<()> { "call_sleep", )?]) .await; - create_config_toml(&codex_home, &server.uri())?; + write_mock_responses_config_toml_with_chatgpt_base_url( + &codex_home, + &server.uri(), + &server.uri(), + )?; + enable_analytics_capture(&server, &codex_home).await?; - let mut mcp = McpProcess::new(&codex_home).await?; + let mut mcp = McpProcess::new_without_managed_config(&codex_home).await?; timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let thread_req = mcp @@ -200,9 +229,14 @@ async fn turn_steer_returns_active_turn_id() -> Result<()> { "call_sleep", )?]) .await; - create_config_toml(&codex_home, &server.uri())?; + write_mock_responses_config_toml_with_chatgpt_base_url( + &codex_home, + &server.uri(), + &server.uri(), + )?; + enable_analytics_capture(&server, &codex_home).await?; - let mut mcp = McpProcess::new(&codex_home).await?; + let mut mcp = McpProcess::new_without_managed_config(&codex_home).await?; timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; let thread_req = mcp @@ -261,31 +295,20 @@ async fn turn_steer_returns_active_turn_id() -> Result<()> { let steer: TurnSteerResponse = to_response::(steer_resp)?; assert_eq!(steer.turn_id, turn.id); + let event = + wait_for_analytics_event(&server, DEFAULT_READ_TIMEOUT, "codex_turn_steer_event").await?; + assert_eq!(event["event_params"]["thread_id"], thread.id); + assert_eq!(event["event_params"]["result"], "accepted"); + assert_eq!(event["event_params"]["num_input_images"], 0); + assert_eq!(event["event_params"]["expected_turn_id"], turn.id); + assert_eq!(event["event_params"]["accepted_turn_id"], turn.id); + assert_eq!( + event["event_params"]["rejection_reason"], + serde_json::Value::Null + ); + mcp.interrupt_turn_and_wait_for_aborted(thread.id, steer.turn_id, DEFAULT_READ_TIMEOUT) .await?; Ok(()) } - -fn create_config_toml(codex_home: &std::path::Path, server_uri: &str) -> std::io::Result<()> { - let config_toml = codex_home.join("config.toml"); - std::fs::write( - config_toml, - format!( - r#" -model = "mock-model" -approval_policy = "never" -sandbox_mode = "danger-full-access" - -model_provider = "mock_provider" - -[model_providers.mock_provider] -name = "Mock provider for test" -base_url = "{server_uri}/v1" -wire_api = "responses" -request_max_retries = 0 -stream_max_retries = 0 -"# - ), - ) -} diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 2fd36376dd..845c5c0859 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -55,6 +55,7 @@ use codex_analytics::CompactionPhase; use codex_analytics::CompactionReason; use codex_analytics::InvocationType; use codex_analytics::SubAgentThreadStartedInput; +use codex_analytics::TurnResolvedConfigFact; use codex_analytics::build_track_events_context; use codex_app_server_protocol::AuthMode; use codex_app_server_protocol::McpServerElicitationRequest; @@ -190,6 +191,7 @@ use crate::config::resolve_web_search_mode_for_turn; use crate::context_manager::ContextManager; use crate::context_manager::TotalTokenUsageBreakdown; use crate::environment_context::EnvironmentContext; +use crate::thread_rollout_truncation::initial_history_has_prior_user_turns; use codex_config::CONFIG_TOML_FILE; use codex_config::types::McpServerConfig; use codex_config::types::ShellEnvironmentPolicy; @@ -2370,6 +2372,11 @@ impl Session { SessionSource::SubAgent(_) ) }; + let has_prior_user_turns = initial_history_has_prior_user_turns(&conversation_history); + { + let mut state = self.state.lock().await; + state.set_next_turn_is_first(!has_prior_user_turns); + } match conversation_history { InitialHistory::New | InitialHistory::Cleared => { // Defer initial context insertion until the first real turn starts so @@ -6328,6 +6335,8 @@ pub(crate) async fn run_turn( .await; } + track_turn_resolved_config_analytics(&sess, &turn_context, &input).await; + let skills_outcome = Some(turn_context.turn_skills.outcome.as_ref()); sess.maybe_start_ghost_snapshot(Arc::clone(&turn_context), cancellation_token.child_token()) .await; @@ -6631,6 +6640,52 @@ pub(crate) async fn run_turn( last_agent_message } +async fn track_turn_resolved_config_analytics( + sess: &Session, + turn_context: &TurnContext, + input: &[UserInput], +) { + if !sess.enabled(Feature::GeneralAnalytics) { + return; + } + + let thread_config = { + let state = sess.state.lock().await; + state.session_configuration.thread_config_snapshot() + }; + let is_first_turn = { + let mut state = sess.state.lock().await; + state.take_next_turn_is_first() + }; + sess.services + .analytics_events_client + .track_turn_resolved_config(TurnResolvedConfigFact { + turn_id: turn_context.sub_id.clone(), + thread_id: sess.conversation_id.to_string(), + num_input_images: input + .iter() + .filter(|item| { + matches!(item, UserInput::Image { .. } | UserInput::LocalImage { .. }) + }) + .count(), + submission_type: None, + ephemeral: thread_config.ephemeral, + session_source: thread_config.session_source, + model: turn_context.model_info.slug.clone(), + model_provider: turn_context.config.model_provider_id.clone(), + sandbox_policy: turn_context.sandbox_policy.get().clone(), + reasoning_effort: turn_context.reasoning_effort, + reasoning_summary: Some(turn_context.reasoning_summary), + service_tier: turn_context.config.service_tier, + approval_policy: turn_context.approval_policy.value(), + approvals_reviewer: turn_context.config.approvals_reviewer, + sandbox_network_access: turn_context.network_sandbox_policy.is_enabled(), + collaboration_mode: turn_context.collaboration_mode.mode, + personality: turn_context.personality, + is_first_turn, + }); +} + async fn run_pre_sampling_compact( sess: &Arc, turn_context: &Arc, diff --git a/codex-rs/core/src/codex_delegate.rs b/codex-rs/core/src/codex_delegate.rs index 55b3619e11..78ab8c163c 100644 --- a/codex-rs/core/src/codex_delegate.rs +++ b/codex-rs/core/src/codex_delegate.rs @@ -78,7 +78,6 @@ pub(crate) async fn run_codex_thread_interactive( let CodexSpawnOk { codex, .. } = Codex::spawn(CodexSpawnArgs { config, auth_manager, - analytics_events_client: Some(parent_session.services.analytics_events_client.clone()), models_manager, environment_manager: Arc::new(EnvironmentManager::from_environment( parent_ctx.environment.as_deref(), @@ -97,6 +96,7 @@ pub(crate) async fn run_codex_thread_interactive( user_shell_override: None, inherited_exec_policy: Some(Arc::clone(&parent_session.services.exec_policy)), parent_trace: None, + analytics_events_client: Some(parent_session.services.analytics_events_client.clone()), }) .await?; if parent_session.enabled(codex_features::Feature::GeneralAnalytics) { diff --git a/codex-rs/core/src/codex_tests_guardian.rs b/codex-rs/core/src/codex_tests_guardian.rs index bf308858ff..cad67dcc8f 100644 --- a/codex-rs/core/src/codex_tests_guardian.rs +++ b/codex-rs/core/src/codex_tests_guardian.rs @@ -433,7 +433,6 @@ async fn guardian_subagent_does_not_inherit_parent_exec_policy_rules() { let CodexSpawnOk { codex, .. } = Codex::spawn(CodexSpawnArgs { config, auth_manager, - analytics_events_client: None, models_manager, environment_manager: Arc::new(EnvironmentManager::new(/*exec_server_url*/ None)), skills_manager, @@ -452,6 +451,7 @@ async fn guardian_subagent_does_not_inherit_parent_exec_policy_rules() { inherited_exec_policy: Some(Arc::new(parent_exec_policy)), user_shell_override: None, parent_trace: None, + analytics_events_client: None, }) .await .expect("spawn guardian subagent"); diff --git a/codex-rs/core/src/compact.rs b/codex-rs/core/src/compact.rs index e132ce0213..59fb45454d 100644 --- a/codex-rs/core/src/compact.rs +++ b/codex-rs/core/src/compact.rs @@ -1,7 +1,5 @@ use std::sync::Arc; use std::time::Instant; -use std::time::SystemTime; -use std::time::UNIX_EPOCH; use crate::Prompt; use crate::client::ModelClientSession; @@ -19,6 +17,7 @@ use codex_analytics::CompactionReason; use codex_analytics::CompactionStatus; use codex_analytics::CompactionStrategy; use codex_analytics::CompactionTrigger; +use codex_analytics::now_unix_seconds; use codex_features::Feature; use codex_model_provider_info::ModelProviderInfo; use codex_protocol::error::CodexErr; @@ -372,13 +371,6 @@ pub(crate) fn compaction_status_from_result(result: &CodexResult) -> Compa } } -fn now_unix_seconds() -> u64 { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|duration| duration.as_secs()) - .unwrap_or_default() -} - pub fn content_items_to_text(content: &[ContentItem]) -> Option { let mut pieces = Vec::new(); for item in content { diff --git a/codex-rs/core/src/state/session.rs b/codex-rs/core/src/state/session.rs index 206f75060c..4360b16de4 100644 --- a/codex-rs/core/src/state/session.rs +++ b/codex-rs/core/src/state/session.rs @@ -33,6 +33,7 @@ pub(crate) struct SessionState { pub(crate) active_connector_selection: HashSet, pub(crate) pending_session_start_source: Option, granted_permissions: Option, + next_turn_is_first: bool, } impl SessionState { @@ -51,6 +52,7 @@ impl SessionState { active_connector_selection: HashSet::new(), pending_session_start_source: None, granted_permissions: None, + next_turn_is_first: true, } } @@ -73,6 +75,16 @@ impl SessionState { self.previous_turn_settings = previous_turn_settings; } + pub(crate) fn set_next_turn_is_first(&mut self, value: bool) { + self.next_turn_is_first = value; + } + + pub(crate) fn take_next_turn_is_first(&mut self) -> bool { + let is_first_turn = self.next_turn_is_first; + self.next_turn_is_first = false; + is_first_turn + } + pub(crate) fn clone_history(&self) -> ContextManager { self.history.clone() } diff --git a/codex-rs/core/src/tasks/mod.rs b/codex-rs/core/src/tasks/mod.rs index b46db8e98f..f17017316e 100644 --- a/codex-rs/core/src/tasks/mod.rs +++ b/codex-rs/core/src/tasks/mod.rs @@ -30,6 +30,7 @@ use crate::hook_runtime::record_pending_input; use crate::state::ActiveTurn; use crate::state::RunningTask; use crate::state::TaskKind; +use codex_analytics::TurnTokenUsageFact; use codex_login::AuthManager; use codex_models_manager::manager::ModelsManager; use codex_otel::SessionTelemetry; @@ -497,6 +498,13 @@ impl Session { - token_usage_at_turn_start.total_tokens) .max(0), }; + self.services + .analytics_events_client + .track_turn_token_usage(TurnTokenUsageFact { + turn_id: turn_context.sub_id.clone(), + thread_id: self.conversation_id.to_string(), + token_usage: turn_token_usage.clone(), + }); self.services.session_telemetry.histogram( TURN_TOKEN_USAGE_METRIC, turn_token_usage.total_tokens, diff --git a/codex-rs/core/src/thread_rollout_truncation.rs b/codex-rs/core/src/thread_rollout_truncation.rs index 97370ce41e..e20ee53d47 100644 --- a/codex-rs/core/src/thread_rollout_truncation.rs +++ b/codex-rs/core/src/thread_rollout_truncation.rs @@ -8,9 +8,21 @@ use crate::event_mapping; use codex_protocol::items::TurnItem; use codex_protocol::models::ResponseItem; use codex_protocol::protocol::EventMsg; +use codex_protocol::protocol::InitialHistory; use codex_protocol::protocol::InterAgentCommunication; use codex_protocol::protocol::RolloutItem; +pub(crate) fn initial_history_has_prior_user_turns(conversation_history: &InitialHistory) -> bool { + conversation_history.scan_rollout_items(rollout_item_is_user_turn_boundary) +} + +fn rollout_item_is_user_turn_boundary(item: &RolloutItem) -> bool { + match item { + RolloutItem::ResponseItem(item) => is_user_turn_boundary(item), + _ => false, + } +} + /// Return the indices of user message boundaries in a rollout. /// /// A user message boundary is a `RolloutItem::ResponseItem(ResponseItem::Message { .. })` diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 1fc707469e..97c34097e8 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -2444,6 +2444,14 @@ pub enum InitialHistory { } impl InitialHistory { + pub fn scan_rollout_items(&self, mut predicate: impl FnMut(&RolloutItem) -> bool) -> bool { + match self { + InitialHistory::New | InitialHistory::Cleared => false, + InitialHistory::Resumed(resumed) => resumed.history.iter().any(&mut predicate), + InitialHistory::Forked(items) => items.iter().any(predicate), + } + } + pub fn forked_from_id(&self) -> Option { match self { InitialHistory::New | InitialHistory::Cleared => None, From a6b03a22cc35b36d46065185c7982cd02bb82c4e Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Mon, 13 Apr 2026 23:33:51 -0700 Subject: [PATCH 05/16] Log realtime call location (#17761) Add a trace-level log for the realtime call Location header when decoding the call id. --- codex-rs/codex-api/src/endpoint/realtime_call.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/codex-rs/codex-api/src/endpoint/realtime_call.rs b/codex-rs/codex-api/src/endpoint/realtime_call.rs index 8a68d088c7..6b02a0a2c7 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_call.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_call.rs @@ -19,6 +19,7 @@ use serde_json::to_string; use serde_json::to_value; use std::sync::Arc; use tracing::instrument; +use tracing::trace; const MULTIPART_BOUNDARY: &str = "codex-realtime-call-boundary"; const MULTIPART_CONTENT_TYPE: &str = "multipart/form-data; boundary=codex-realtime-call-boundary"; @@ -200,6 +201,7 @@ fn decode_call_id_from_location(headers: &HeaderMap) -> Result .ok_or_else(|| ApiError::Stream("realtime call response missing Location".to_string()))? .to_str() .map_err(|err| ApiError::Stream(format!("invalid realtime call Location: {err}")))?; + trace!("realtime call Location: {location}"); location .split('?') From 2f6fc7c137bfa1b607b97bd932370022bea198eb Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Tue, 14 Apr 2026 00:13:13 -0700 Subject: [PATCH 06/16] Add realtime output modality and transcript events (#17701) - Add outputModality to thread/realtime/start and wire text/audio output selection through app-server, core, API, and TUI.\n- Rename the realtime transcript delta notification and add a separate transcript done notification that forwards final text from item done without correlating it with deltas. --- .../schema/json/ClientRequest.json | 7 + .../schema/json/ServerNotification.json | 52 ++++- .../codex_app_server_protocol.schemas.json | 63 +++++- .../codex_app_server_protocol.v2.schemas.json | 63 +++++- ...dRealtimeTranscriptDeltaNotification.json} | 9 +- ...eadRealtimeTranscriptDoneNotification.json | 23 ++ .../typescript/RealtimeOutputModality.ts | 5 + .../schema/typescript/ServerNotification.ts | 5 +- .../schema/typescript/index.ts | 1 + ...eadRealtimeTranscriptDeltaNotification.ts} | 6 +- ...hreadRealtimeTranscriptDoneNotification.ts | 13 ++ .../schema/typescript/v2/index.ts | 3 +- .../src/protocol/common.rs | 19 +- .../app-server-protocol/src/protocol/v2.rs | 21 +- codex-rs/app-server/README.md | 6 +- .../app-server/src/bespoke_event_handling.rs | 39 +++- .../app-server/src/codex_message_processor.rs | 1 + .../tests/suite/v2/experimental_api.rs | 3 + .../tests/suite/v2/realtime_conversation.rs | 205 ++++++++++++++++-- codex-rs/codex-api/src/endpoint/mod.rs | 1 + .../codex-api/src/endpoint/realtime_call.rs | 2 + .../endpoint/realtime_websocket/methods.rs | 61 +++++- .../realtime_websocket/methods_common.rs | 5 +- .../endpoint/realtime_websocket/methods_v2.rs | 12 +- .../src/endpoint/realtime_websocket/mod.rs | 1 + .../endpoint/realtime_websocket/protocol.rs | 3 +- .../realtime_websocket/protocol_common.rs | 12 + .../realtime_websocket/protocol_v2.rs | 37 +++- codex-rs/codex-api/src/lib.rs | 1 + .../codex-api/tests/realtime_websocket_e2e.rs | 7 + codex-rs/core/src/realtime_conversation.rs | 22 +- codex-rs/core/tests/suite/compact_remote.rs | 2 + .../core/tests/suite/realtime_conversation.rs | 35 +++ codex-rs/protocol/src/protocol.rs | 30 ++- codex-rs/tui/src/app/app_server_adapter.rs | 5 +- codex-rs/tui/src/app_server_session.rs | 1 + codex-rs/tui/src/chatwidget.rs | 3 +- codex-rs/tui/src/chatwidget/realtime.rs | 4 + 38 files changed, 711 insertions(+), 77 deletions(-) rename codex-rs/app-server-protocol/schema/json/v2/{ThreadRealtimeTranscriptUpdatedNotification.json => ThreadRealtimeTranscriptDeltaNotification.json} (70%) create mode 100644 codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptDoneNotification.json create mode 100644 codex-rs/app-server-protocol/schema/typescript/RealtimeOutputModality.ts rename codex-rs/app-server-protocol/schema/typescript/v2/{ThreadRealtimeTranscriptUpdatedNotification.ts => ThreadRealtimeTranscriptDeltaNotification.ts} (60%) create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptDoneNotification.ts diff --git a/codex-rs/app-server-protocol/schema/json/ClientRequest.json b/codex-rs/app-server-protocol/schema/json/ClientRequest.json index 5e36c9aa5c..a2659bd234 100644 --- a/codex-rs/app-server-protocol/schema/json/ClientRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ClientRequest.json @@ -1525,6 +1525,13 @@ } ] }, + "RealtimeOutputModality": { + "enum": [ + "text", + "audio" + ], + "type": "string" + }, "RealtimeVoice": { "enum": [ "alloy", diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index c3ab83766a..4edff15748 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -3384,13 +3384,35 @@ ], "type": "object" }, - "ThreadRealtimeTranscriptUpdatedNotification": { + "ThreadRealtimeTranscriptDeltaNotification": { "description": "EXPERIMENTAL - flat transcript delta emitted whenever realtime transcript text changes.", + "properties": { + "delta": { + "description": "Live transcript delta from the realtime event.", + "type": "string" + }, + "role": { + "type": "string" + }, + "threadId": { + "type": "string" + } + }, + "required": [ + "delta", + "role", + "threadId" + ], + "type": "object" + }, + "ThreadRealtimeTranscriptDoneNotification": { + "description": "EXPERIMENTAL - final transcript text emitted when realtime completes a transcript part.", "properties": { "role": { "type": "string" }, "text": { + "description": "Final complete text for the transcript part.", "type": "string" }, "threadId": { @@ -4949,20 +4971,40 @@ "properties": { "method": { "enum": [ - "thread/realtime/transcriptUpdated" + "thread/realtime/transcript/delta" ], - "title": "Thread/realtime/transcriptUpdatedNotificationMethod", + "title": "Thread/realtime/transcript/deltaNotificationMethod", "type": "string" }, "params": { - "$ref": "#/definitions/ThreadRealtimeTranscriptUpdatedNotification" + "$ref": "#/definitions/ThreadRealtimeTranscriptDeltaNotification" } }, "required": [ "method", "params" ], - "title": "Thread/realtime/transcriptUpdatedNotification", + "title": "Thread/realtime/transcript/deltaNotification", + "type": "object" + }, + { + "properties": { + "method": { + "enum": [ + "thread/realtime/transcript/done" + ], + "title": "Thread/realtime/transcript/doneNotificationMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/ThreadRealtimeTranscriptDoneNotification" + } + }, + "required": [ + "method", + "params" + ], + "title": "Thread/realtime/transcript/doneNotification", "type": "object" }, { diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 8ad93be035..5ecdb5a5c0 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -4379,20 +4379,40 @@ "properties": { "method": { "enum": [ - "thread/realtime/transcriptUpdated" + "thread/realtime/transcript/delta" ], - "title": "Thread/realtime/transcriptUpdatedNotificationMethod", + "title": "Thread/realtime/transcript/deltaNotificationMethod", "type": "string" }, "params": { - "$ref": "#/definitions/v2/ThreadRealtimeTranscriptUpdatedNotification" + "$ref": "#/definitions/v2/ThreadRealtimeTranscriptDeltaNotification" } }, "required": [ "method", "params" ], - "title": "Thread/realtime/transcriptUpdatedNotification", + "title": "Thread/realtime/transcript/deltaNotification", + "type": "object" + }, + { + "properties": { + "method": { + "enum": [ + "thread/realtime/transcript/done" + ], + "title": "Thread/realtime/transcript/doneNotificationMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/v2/ThreadRealtimeTranscriptDoneNotification" + } + }, + "required": [ + "method", + "params" + ], + "title": "Thread/realtime/transcript/doneNotification", "type": "object" }, { @@ -10718,6 +10738,13 @@ ], "type": "string" }, + "RealtimeOutputModality": { + "enum": [ + "text", + "audio" + ], + "type": "string" + }, "RealtimeVoice": { "enum": [ "alloy", @@ -14088,14 +14115,38 @@ "title": "ThreadRealtimeStartedNotification", "type": "object" }, - "ThreadRealtimeTranscriptUpdatedNotification": { + "ThreadRealtimeTranscriptDeltaNotification": { "$schema": "http://json-schema.org/draft-07/schema#", "description": "EXPERIMENTAL - flat transcript delta emitted whenever realtime transcript text changes.", + "properties": { + "delta": { + "description": "Live transcript delta from the realtime event.", + "type": "string" + }, + "role": { + "type": "string" + }, + "threadId": { + "type": "string" + } + }, + "required": [ + "delta", + "role", + "threadId" + ], + "title": "ThreadRealtimeTranscriptDeltaNotification", + "type": "object" + }, + "ThreadRealtimeTranscriptDoneNotification": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "EXPERIMENTAL - final transcript text emitted when realtime completes a transcript part.", "properties": { "role": { "type": "string" }, "text": { + "description": "Final complete text for the transcript part.", "type": "string" }, "threadId": { @@ -14107,7 +14158,7 @@ "text", "threadId" ], - "title": "ThreadRealtimeTranscriptUpdatedNotification", + "title": "ThreadRealtimeTranscriptDoneNotification", "type": "object" }, "ThreadResumeParams": { diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index fd06afaee8..a37456c86d 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -7514,6 +7514,13 @@ ], "type": "string" }, + "RealtimeOutputModality": { + "enum": [ + "text", + "audio" + ], + "type": "string" + }, "RealtimeVoice": { "enum": [ "alloy", @@ -9683,20 +9690,40 @@ "properties": { "method": { "enum": [ - "thread/realtime/transcriptUpdated" + "thread/realtime/transcript/delta" ], - "title": "Thread/realtime/transcriptUpdatedNotificationMethod", + "title": "Thread/realtime/transcript/deltaNotificationMethod", "type": "string" }, "params": { - "$ref": "#/definitions/ThreadRealtimeTranscriptUpdatedNotification" + "$ref": "#/definitions/ThreadRealtimeTranscriptDeltaNotification" } }, "required": [ "method", "params" ], - "title": "Thread/realtime/transcriptUpdatedNotification", + "title": "Thread/realtime/transcript/deltaNotification", + "type": "object" + }, + { + "properties": { + "method": { + "enum": [ + "thread/realtime/transcript/done" + ], + "title": "Thread/realtime/transcript/doneNotificationMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/ThreadRealtimeTranscriptDoneNotification" + } + }, + "required": [ + "method", + "params" + ], + "title": "Thread/realtime/transcript/doneNotification", "type": "object" }, { @@ -11936,14 +11963,38 @@ "title": "ThreadRealtimeStartedNotification", "type": "object" }, - "ThreadRealtimeTranscriptUpdatedNotification": { + "ThreadRealtimeTranscriptDeltaNotification": { "$schema": "http://json-schema.org/draft-07/schema#", "description": "EXPERIMENTAL - flat transcript delta emitted whenever realtime transcript text changes.", + "properties": { + "delta": { + "description": "Live transcript delta from the realtime event.", + "type": "string" + }, + "role": { + "type": "string" + }, + "threadId": { + "type": "string" + } + }, + "required": [ + "delta", + "role", + "threadId" + ], + "title": "ThreadRealtimeTranscriptDeltaNotification", + "type": "object" + }, + "ThreadRealtimeTranscriptDoneNotification": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "EXPERIMENTAL - final transcript text emitted when realtime completes a transcript part.", "properties": { "role": { "type": "string" }, "text": { + "description": "Final complete text for the transcript part.", "type": "string" }, "threadId": { @@ -11955,7 +12006,7 @@ "text", "threadId" ], - "title": "ThreadRealtimeTranscriptUpdatedNotification", + "title": "ThreadRealtimeTranscriptDoneNotification", "type": "object" }, "ThreadResumeParams": { diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptUpdatedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptDeltaNotification.json similarity index 70% rename from codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptUpdatedNotification.json rename to codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptDeltaNotification.json index 2c6860fa31..22ad778eb2 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptUpdatedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptDeltaNotification.json @@ -2,10 +2,11 @@ "$schema": "http://json-schema.org/draft-07/schema#", "description": "EXPERIMENTAL - flat transcript delta emitted whenever realtime transcript text changes.", "properties": { - "role": { + "delta": { + "description": "Live transcript delta from the realtime event.", "type": "string" }, - "text": { + "role": { "type": "string" }, "threadId": { @@ -13,10 +14,10 @@ } }, "required": [ + "delta", "role", - "text", "threadId" ], - "title": "ThreadRealtimeTranscriptUpdatedNotification", + "title": "ThreadRealtimeTranscriptDeltaNotification", "type": "object" } \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptDoneNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptDoneNotification.json new file mode 100644 index 0000000000..2f4199fdb9 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadRealtimeTranscriptDoneNotification.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "EXPERIMENTAL - final transcript text emitted when realtime completes a transcript part.", + "properties": { + "role": { + "type": "string" + }, + "text": { + "description": "Final complete text for the transcript part.", + "type": "string" + }, + "threadId": { + "type": "string" + } + }, + "required": [ + "role", + "text", + "threadId" + ], + "title": "ThreadRealtimeTranscriptDoneNotification", + "type": "object" +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/typescript/RealtimeOutputModality.ts b/codex-rs/app-server-protocol/schema/typescript/RealtimeOutputModality.ts new file mode 100644 index 0000000000..78e00e7143 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/RealtimeOutputModality.ts @@ -0,0 +1,5 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type RealtimeOutputModality = "text" | "audio"; diff --git a/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts b/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts index a985914134..1db7027feb 100644 --- a/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts +++ b/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts @@ -43,7 +43,8 @@ import type { ThreadRealtimeItemAddedNotification } from "./v2/ThreadRealtimeIte import type { ThreadRealtimeOutputAudioDeltaNotification } from "./v2/ThreadRealtimeOutputAudioDeltaNotification"; import type { ThreadRealtimeSdpNotification } from "./v2/ThreadRealtimeSdpNotification"; import type { ThreadRealtimeStartedNotification } from "./v2/ThreadRealtimeStartedNotification"; -import type { ThreadRealtimeTranscriptUpdatedNotification } from "./v2/ThreadRealtimeTranscriptUpdatedNotification"; +import type { ThreadRealtimeTranscriptDeltaNotification } from "./v2/ThreadRealtimeTranscriptDeltaNotification"; +import type { ThreadRealtimeTranscriptDoneNotification } from "./v2/ThreadRealtimeTranscriptDoneNotification"; import type { ThreadStartedNotification } from "./v2/ThreadStartedNotification"; import type { ThreadStatusChangedNotification } from "./v2/ThreadStatusChangedNotification"; import type { ThreadTokenUsageUpdatedNotification } from "./v2/ThreadTokenUsageUpdatedNotification"; @@ -58,4 +59,4 @@ import type { WindowsWorldWritableWarningNotification } from "./v2/WindowsWorldW /** * Notification sent from the server to the client. */ -export type ServerNotification = { "method": "error", "params": ErrorNotification } | { "method": "thread/started", "params": ThreadStartedNotification } | { "method": "thread/status/changed", "params": ThreadStatusChangedNotification } | { "method": "thread/archived", "params": ThreadArchivedNotification } | { "method": "thread/unarchived", "params": ThreadUnarchivedNotification } | { "method": "thread/closed", "params": ThreadClosedNotification } | { "method": "skills/changed", "params": SkillsChangedNotification } | { "method": "thread/name/updated", "params": ThreadNameUpdatedNotification } | { "method": "thread/tokenUsage/updated", "params": ThreadTokenUsageUpdatedNotification } | { "method": "turn/started", "params": TurnStartedNotification } | { "method": "hook/started", "params": HookStartedNotification } | { "method": "turn/completed", "params": TurnCompletedNotification } | { "method": "hook/completed", "params": HookCompletedNotification } | { "method": "turn/diff/updated", "params": TurnDiffUpdatedNotification } | { "method": "turn/plan/updated", "params": TurnPlanUpdatedNotification } | { "method": "item/started", "params": ItemStartedNotification } | { "method": "item/autoApprovalReview/started", "params": ItemGuardianApprovalReviewStartedNotification } | { "method": "item/autoApprovalReview/completed", "params": ItemGuardianApprovalReviewCompletedNotification } | { "method": "item/completed", "params": ItemCompletedNotification } | { "method": "rawResponseItem/completed", "params": RawResponseItemCompletedNotification } | { "method": "item/agentMessage/delta", "params": AgentMessageDeltaNotification } | { "method": "item/plan/delta", "params": PlanDeltaNotification } | { "method": "command/exec/outputDelta", "params": CommandExecOutputDeltaNotification } | { "method": "item/commandExecution/outputDelta", "params": CommandExecutionOutputDeltaNotification } | { "method": "item/commandExecution/terminalInteraction", "params": TerminalInteractionNotification } | { "method": "item/fileChange/outputDelta", "params": FileChangeOutputDeltaNotification } | { "method": "serverRequest/resolved", "params": ServerRequestResolvedNotification } | { "method": "item/mcpToolCall/progress", "params": McpToolCallProgressNotification } | { "method": "mcpServer/oauthLogin/completed", "params": McpServerOauthLoginCompletedNotification } | { "method": "mcpServer/startupStatus/updated", "params": McpServerStatusUpdatedNotification } | { "method": "account/updated", "params": AccountUpdatedNotification } | { "method": "account/rateLimits/updated", "params": AccountRateLimitsUpdatedNotification } | { "method": "app/list/updated", "params": AppListUpdatedNotification } | { "method": "fs/changed", "params": FsChangedNotification } | { "method": "item/reasoning/summaryTextDelta", "params": ReasoningSummaryTextDeltaNotification } | { "method": "item/reasoning/summaryPartAdded", "params": ReasoningSummaryPartAddedNotification } | { "method": "item/reasoning/textDelta", "params": ReasoningTextDeltaNotification } | { "method": "thread/compacted", "params": ContextCompactedNotification } | { "method": "model/rerouted", "params": ModelReroutedNotification } | { "method": "deprecationNotice", "params": DeprecationNoticeNotification } | { "method": "configWarning", "params": ConfigWarningNotification } | { "method": "fuzzyFileSearch/sessionUpdated", "params": FuzzyFileSearchSessionUpdatedNotification } | { "method": "fuzzyFileSearch/sessionCompleted", "params": FuzzyFileSearchSessionCompletedNotification } | { "method": "thread/realtime/started", "params": ThreadRealtimeStartedNotification } | { "method": "thread/realtime/itemAdded", "params": ThreadRealtimeItemAddedNotification } | { "method": "thread/realtime/transcriptUpdated", "params": ThreadRealtimeTranscriptUpdatedNotification } | { "method": "thread/realtime/outputAudio/delta", "params": ThreadRealtimeOutputAudioDeltaNotification } | { "method": "thread/realtime/sdp", "params": ThreadRealtimeSdpNotification } | { "method": "thread/realtime/error", "params": ThreadRealtimeErrorNotification } | { "method": "thread/realtime/closed", "params": ThreadRealtimeClosedNotification } | { "method": "windows/worldWritableWarning", "params": WindowsWorldWritableWarningNotification } | { "method": "windowsSandbox/setupCompleted", "params": WindowsSandboxSetupCompletedNotification } | { "method": "account/login/completed", "params": AccountLoginCompletedNotification }; +export type ServerNotification = { "method": "error", "params": ErrorNotification } | { "method": "thread/started", "params": ThreadStartedNotification } | { "method": "thread/status/changed", "params": ThreadStatusChangedNotification } | { "method": "thread/archived", "params": ThreadArchivedNotification } | { "method": "thread/unarchived", "params": ThreadUnarchivedNotification } | { "method": "thread/closed", "params": ThreadClosedNotification } | { "method": "skills/changed", "params": SkillsChangedNotification } | { "method": "thread/name/updated", "params": ThreadNameUpdatedNotification } | { "method": "thread/tokenUsage/updated", "params": ThreadTokenUsageUpdatedNotification } | { "method": "turn/started", "params": TurnStartedNotification } | { "method": "hook/started", "params": HookStartedNotification } | { "method": "turn/completed", "params": TurnCompletedNotification } | { "method": "hook/completed", "params": HookCompletedNotification } | { "method": "turn/diff/updated", "params": TurnDiffUpdatedNotification } | { "method": "turn/plan/updated", "params": TurnPlanUpdatedNotification } | { "method": "item/started", "params": ItemStartedNotification } | { "method": "item/autoApprovalReview/started", "params": ItemGuardianApprovalReviewStartedNotification } | { "method": "item/autoApprovalReview/completed", "params": ItemGuardianApprovalReviewCompletedNotification } | { "method": "item/completed", "params": ItemCompletedNotification } | { "method": "rawResponseItem/completed", "params": RawResponseItemCompletedNotification } | { "method": "item/agentMessage/delta", "params": AgentMessageDeltaNotification } | { "method": "item/plan/delta", "params": PlanDeltaNotification } | { "method": "command/exec/outputDelta", "params": CommandExecOutputDeltaNotification } | { "method": "item/commandExecution/outputDelta", "params": CommandExecutionOutputDeltaNotification } | { "method": "item/commandExecution/terminalInteraction", "params": TerminalInteractionNotification } | { "method": "item/fileChange/outputDelta", "params": FileChangeOutputDeltaNotification } | { "method": "serverRequest/resolved", "params": ServerRequestResolvedNotification } | { "method": "item/mcpToolCall/progress", "params": McpToolCallProgressNotification } | { "method": "mcpServer/oauthLogin/completed", "params": McpServerOauthLoginCompletedNotification } | { "method": "mcpServer/startupStatus/updated", "params": McpServerStatusUpdatedNotification } | { "method": "account/updated", "params": AccountUpdatedNotification } | { "method": "account/rateLimits/updated", "params": AccountRateLimitsUpdatedNotification } | { "method": "app/list/updated", "params": AppListUpdatedNotification } | { "method": "fs/changed", "params": FsChangedNotification } | { "method": "item/reasoning/summaryTextDelta", "params": ReasoningSummaryTextDeltaNotification } | { "method": "item/reasoning/summaryPartAdded", "params": ReasoningSummaryPartAddedNotification } | { "method": "item/reasoning/textDelta", "params": ReasoningTextDeltaNotification } | { "method": "thread/compacted", "params": ContextCompactedNotification } | { "method": "model/rerouted", "params": ModelReroutedNotification } | { "method": "deprecationNotice", "params": DeprecationNoticeNotification } | { "method": "configWarning", "params": ConfigWarningNotification } | { "method": "fuzzyFileSearch/sessionUpdated", "params": FuzzyFileSearchSessionUpdatedNotification } | { "method": "fuzzyFileSearch/sessionCompleted", "params": FuzzyFileSearchSessionCompletedNotification } | { "method": "thread/realtime/started", "params": ThreadRealtimeStartedNotification } | { "method": "thread/realtime/itemAdded", "params": ThreadRealtimeItemAddedNotification } | { "method": "thread/realtime/transcript/delta", "params": ThreadRealtimeTranscriptDeltaNotification } | { "method": "thread/realtime/transcript/done", "params": ThreadRealtimeTranscriptDoneNotification } | { "method": "thread/realtime/outputAudio/delta", "params": ThreadRealtimeOutputAudioDeltaNotification } | { "method": "thread/realtime/sdp", "params": ThreadRealtimeSdpNotification } | { "method": "thread/realtime/error", "params": ThreadRealtimeErrorNotification } | { "method": "thread/realtime/closed", "params": ThreadRealtimeClosedNotification } | { "method": "windows/worldWritableWarning", "params": WindowsWorldWritableWarningNotification } | { "method": "windowsSandbox/setupCompleted", "params": WindowsSandboxSetupCompletedNotification } | { "method": "account/login/completed", "params": AccountLoginCompletedNotification }; diff --git a/codex-rs/app-server-protocol/schema/typescript/index.ts b/codex-rs/app-server-protocol/schema/typescript/index.ts index 3f07f71695..7bbb417fdc 100644 --- a/codex-rs/app-server-protocol/schema/typescript/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/index.ts @@ -49,6 +49,7 @@ export type { ParsedCommand } from "./ParsedCommand"; export type { Personality } from "./Personality"; export type { PlanType } from "./PlanType"; export type { RealtimeConversationVersion } from "./RealtimeConversationVersion"; +export type { RealtimeOutputModality } from "./RealtimeOutputModality"; export type { RealtimeVoice } from "./RealtimeVoice"; export type { RealtimeVoicesList } from "./RealtimeVoicesList"; export type { ReasoningEffort } from "./ReasoningEffort"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptUpdatedNotification.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptDeltaNotification.ts similarity index 60% rename from codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptUpdatedNotification.ts rename to codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptDeltaNotification.ts index d2940029f2..805eeddd76 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptUpdatedNotification.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptDeltaNotification.ts @@ -6,4 +6,8 @@ * EXPERIMENTAL - flat transcript delta emitted whenever realtime * transcript text changes. */ -export type ThreadRealtimeTranscriptUpdatedNotification = { threadId: string, role: string, text: string, }; +export type ThreadRealtimeTranscriptDeltaNotification = { threadId: string, role: string, +/** + * Live transcript delta from the realtime event. + */ +delta: string, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptDoneNotification.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptDoneNotification.ts new file mode 100644 index 0000000000..d4667ad039 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadRealtimeTranscriptDoneNotification.ts @@ -0,0 +1,13 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +/** + * EXPERIMENTAL - final transcript text emitted when realtime completes + * a transcript part. + */ +export type ThreadRealtimeTranscriptDoneNotification = { threadId: string, role: string, +/** + * Final complete text for the transcript part. + */ +text: string, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index 2b8fd187f9..91a51737f8 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -305,7 +305,8 @@ export type { ThreadRealtimeOutputAudioDeltaNotification } from "./ThreadRealtim export type { ThreadRealtimeSdpNotification } from "./ThreadRealtimeSdpNotification"; export type { ThreadRealtimeStartTransport } from "./ThreadRealtimeStartTransport"; export type { ThreadRealtimeStartedNotification } from "./ThreadRealtimeStartedNotification"; -export type { ThreadRealtimeTranscriptUpdatedNotification } from "./ThreadRealtimeTranscriptUpdatedNotification"; +export type { ThreadRealtimeTranscriptDeltaNotification } from "./ThreadRealtimeTranscriptDeltaNotification"; +export type { ThreadRealtimeTranscriptDoneNotification } from "./ThreadRealtimeTranscriptDoneNotification"; export type { ThreadResumeParams } from "./ThreadResumeParams"; export type { ThreadResumeResponse } from "./ThreadResumeResponse"; export type { ThreadRollbackParams } from "./ThreadRollbackParams"; diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index 571832610c..6853703042 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -1026,8 +1026,10 @@ server_notification_definitions! { ThreadRealtimeStarted => "thread/realtime/started" (v2::ThreadRealtimeStartedNotification), #[experimental("thread/realtime/itemAdded")] ThreadRealtimeItemAdded => "thread/realtime/itemAdded" (v2::ThreadRealtimeItemAddedNotification), - #[experimental("thread/realtime/transcriptUpdated")] - ThreadRealtimeTranscriptUpdated => "thread/realtime/transcriptUpdated" (v2::ThreadRealtimeTranscriptUpdatedNotification), + #[experimental("thread/realtime/transcript/delta")] + ThreadRealtimeTranscriptDelta => "thread/realtime/transcript/delta" (v2::ThreadRealtimeTranscriptDeltaNotification), + #[experimental("thread/realtime/transcript/done")] + ThreadRealtimeTranscriptDone => "thread/realtime/transcript/done" (v2::ThreadRealtimeTranscriptDoneNotification), #[experimental("thread/realtime/outputAudio/delta")] ThreadRealtimeOutputAudioDelta => "thread/realtime/outputAudio/delta" (v2::ThreadRealtimeOutputAudioDeltaNotification), #[experimental("thread/realtime/sdp")] @@ -1060,6 +1062,8 @@ mod tests { use codex_protocol::account::PlanType; use codex_protocol::parse_command::ParsedCommand; use codex_protocol::protocol::RealtimeConversationVersion; + use codex_protocol::protocol::RealtimeOutputModality; + use codex_protocol::protocol::RealtimeVoice; use codex_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; use serde_json::json; @@ -1788,10 +1792,11 @@ mod tests { request_id: RequestId::Integer(9), params: v2::ThreadRealtimeStartParams { thread_id: "thr_123".to_string(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("You are on a call".to_string())), session_id: Some("sess_456".to_string()), transport: None, - voice: Some(codex_protocol::protocol::RealtimeVoice::Marin), + voice: Some(RealtimeVoice::Marin), }, }; assert_eq!( @@ -1800,6 +1805,7 @@ mod tests { "id": 9, "params": { "threadId": "thr_123", + "outputModality": "audio", "prompt": "You are on a call", "sessionId": "sess_456", "transport": null, @@ -1817,6 +1823,7 @@ mod tests { request_id: RequestId::Integer(9), params: v2::ThreadRealtimeStartParams { thread_id: "thr_123".to_string(), + output_modality: RealtimeOutputModality::Audio, prompt: None, session_id: None, transport: None, @@ -1829,6 +1836,7 @@ mod tests { "id": 9, "params": { "threadId": "thr_123", + "outputModality": "audio", "sessionId": null, "transport": null, "voice": null @@ -1841,6 +1849,7 @@ mod tests { request_id: RequestId::Integer(9), params: v2::ThreadRealtimeStartParams { thread_id: "thr_123".to_string(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(None), session_id: None, transport: None, @@ -1853,6 +1862,7 @@ mod tests { "id": 9, "params": { "threadId": "thr_123", + "outputModality": "audio", "prompt": null, "sessionId": null, "transport": null, @@ -1867,6 +1877,7 @@ mod tests { "id": 9, "params": { "threadId": "thr_123", + "outputModality": "audio", "sessionId": null, "transport": null, "voice": null @@ -1882,6 +1893,7 @@ mod tests { "id": 9, "params": { "threadId": "thr_123", + "outputModality": "audio", "prompt": null, "sessionId": null, "transport": null, @@ -1966,6 +1978,7 @@ mod tests { request_id: RequestId::Integer(1), params: v2::ThreadRealtimeStartParams { thread_id: "thr_123".to_string(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("You are on a call".to_string())), session_id: None, transport: None, diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 7914a49b41..4f8d9f121c 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -74,6 +74,7 @@ use codex_protocol::protocol::RateLimitWindow as CoreRateLimitWindow; use codex_protocol::protocol::ReadOnlyAccess as CoreReadOnlyAccess; use codex_protocol::protocol::RealtimeAudioFrame as CoreRealtimeAudioFrame; use codex_protocol::protocol::RealtimeConversationVersion; +use codex_protocol::protocol::RealtimeOutputModality; use codex_protocol::protocol::RealtimeVoice; use codex_protocol::protocol::RealtimeVoicesList; use codex_protocol::protocol::ReviewDecision as CoreReviewDecision; @@ -3976,11 +3977,14 @@ impl From for CoreRealtimeAudioFrame { } /// EXPERIMENTAL - start a thread-scoped realtime session. -#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, JsonSchema, TS)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] pub struct ThreadRealtimeStartParams { pub thread_id: String, + /// Selects text or audio output for the realtime session. Transport and voice stay + /// independent so clients can choose how they connect separately from what the model emits. + pub output_modality: RealtimeOutputModality, #[serde( default, deserialize_with = "super::serde_helpers::deserialize_double_option", @@ -4098,9 +4102,22 @@ pub struct ThreadRealtimeItemAddedNotification { #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] -pub struct ThreadRealtimeTranscriptUpdatedNotification { +pub struct ThreadRealtimeTranscriptDeltaNotification { pub thread_id: String, pub role: String, + /// Live transcript delta from the realtime event. + pub delta: String, +} + +/// EXPERIMENTAL - final transcript text emitted when realtime completes +/// a transcript part. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadRealtimeTranscriptDoneNotification { + pub thread_id: String, + pub role: String, + /// Final complete text for the transcript part. pub text: String, } diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 8d463b833d..6337fe1f01 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -154,7 +154,7 @@ Example with notification opt-out: - `thread/inject_items` — append raw Responses API items to a loaded thread’s model-visible history without starting a user turn; returns `{}` on success. - `turn/steer` — add user input to an already in-flight regular turn without starting a new turn; returns the active `turnId` that accepted the input. Review and manual compaction turns reject `turn/steer`. - `turn/interrupt` — request cancellation of an in-flight turn by `(thread_id, turn_id)`; success is an empty `{}` response and the turn finishes with `status: "interrupted"`. -- `thread/realtime/start` — start a thread-scoped realtime session (experimental); returns `{}` and streams `thread/realtime/*` notifications. Omit `transport` for the websocket transport, or pass `{ "type": "webrtc", "sdp": "..." }` to create a WebRTC session from a browser-generated SDP offer; the remote answer SDP is emitted as `thread/realtime/sdp`. +- `thread/realtime/start` — start a thread-scoped realtime session (experimental); pass `outputModality: "text"` or `outputModality: "audio"` to choose model output, returns `{}` and streams `thread/realtime/*` notifications. Omit `transport` for the websocket transport, or pass `{ "type": "webrtc", "sdp": "..." }` to create a WebRTC session from a browser-generated SDP offer; the remote answer SDP is emitted as `thread/realtime/sdp`. - `thread/realtime/appendAudio` — append an input audio chunk to the active realtime session (experimental); returns `{}`. - `thread/realtime/appendText` — append text input to the active realtime session (experimental); returns `{}`. - `thread/realtime/stop` — stop the active realtime session for the thread (experimental); returns `{}`. @@ -628,6 +628,7 @@ Then send `offer.sdp` to app-server. Core uses `experimental_realtime_ws_backend ```json { "method": "thread/realtime/start", "id": 40, "params": { "threadId": "thr_123", + "outputModality": "audio", "prompt": "You are on a call.", "sessionId": null, "transport": { "type": "webrtc", "sdp": "v=0\r\no=..." } @@ -952,7 +953,8 @@ The thread realtime API emits thread-scoped notifications for session lifecycle - `thread/realtime/started` — `{ threadId, sessionId }` once realtime starts for the thread (experimental). - `thread/realtime/itemAdded` — `{ threadId, item }` for raw non-audio realtime items that do not have a dedicated typed app-server notification, including `handoff_request` (experimental). `item` is forwarded as raw JSON while the upstream websocket item schema remains unstable. -- `thread/realtime/transcriptUpdated` — `{ threadId, role, text }` whenever realtime transcript text changes (experimental). This forwards the live transcript delta from that realtime event, not the full accumulated transcript. +- `thread/realtime/transcript/delta` — `{ threadId, role, delta }` for live realtime transcript deltas (experimental). +- `thread/realtime/transcript/done` — `{ threadId, role, text }` when realtime emits the final full text for a transcript part (experimental). - `thread/realtime/outputAudio/delta` — `{ threadId, audio }` for streamed output audio chunks (experimental). `audio` uses camelCase fields (`data`, `sampleRate`, `numChannels`, `samplesPerChannel`). - `thread/realtime/error` — `{ threadId, message }` when realtime encounters a transport or backend error (experimental). - `thread/realtime/closed` — `{ threadId, reason }` when the realtime transport closes (experimental). diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index de9f1245e5..0eec5ad0d4 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -83,7 +83,8 @@ use codex_app_server_protocol::ThreadRealtimeItemAddedNotification; use codex_app_server_protocol::ThreadRealtimeOutputAudioDeltaNotification; use codex_app_server_protocol::ThreadRealtimeSdpNotification; use codex_app_server_protocol::ThreadRealtimeStartedNotification; -use codex_app_server_protocol::ThreadRealtimeTranscriptUpdatedNotification; +use codex_app_server_protocol::ThreadRealtimeTranscriptDeltaNotification; +use codex_app_server_protocol::ThreadRealtimeTranscriptDoneNotification; use codex_app_server_protocol::ThreadRollbackResponse; use codex_app_server_protocol::ThreadTokenUsage; use codex_app_server_protocol::ThreadTokenUsageUpdatedNotification; @@ -408,26 +409,50 @@ pub(crate) async fn apply_bespoke_event_handling( .await; } RealtimeEvent::InputTranscriptDelta(event) => { - let notification = ThreadRealtimeTranscriptUpdatedNotification { + let notification = ThreadRealtimeTranscriptDeltaNotification { thread_id: conversation_id.to_string(), role: "user".to_string(), - text: event.delta, + delta: event.delta, }; outgoing .send_server_notification( - ServerNotification::ThreadRealtimeTranscriptUpdated(notification), + ServerNotification::ThreadRealtimeTranscriptDelta(notification), + ) + .await; + } + RealtimeEvent::InputTranscriptDone(event) => { + let notification = ThreadRealtimeTranscriptDoneNotification { + thread_id: conversation_id.to_string(), + role: "user".to_string(), + text: event.text, + }; + outgoing + .send_server_notification( + ServerNotification::ThreadRealtimeTranscriptDone(notification), ) .await; } RealtimeEvent::OutputTranscriptDelta(event) => { - let notification = ThreadRealtimeTranscriptUpdatedNotification { + let notification = ThreadRealtimeTranscriptDeltaNotification { thread_id: conversation_id.to_string(), role: "assistant".to_string(), - text: event.delta, + delta: event.delta, }; outgoing .send_server_notification( - ServerNotification::ThreadRealtimeTranscriptUpdated(notification), + ServerNotification::ThreadRealtimeTranscriptDelta(notification), + ) + .await; + } + RealtimeEvent::OutputTranscriptDone(event) => { + let notification = ThreadRealtimeTranscriptDoneNotification { + thread_id: conversation_id.to_string(), + role: "assistant".to_string(), + text: event.text, + }; + outgoing + .send_server_notification( + ServerNotification::ThreadRealtimeTranscriptDone(notification), ) .await; } diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 9f307eb247..c027501db0 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -7323,6 +7323,7 @@ impl CodexMessageProcessor { &request_id, thread.as_ref(), Op::RealtimeConversationStart(ConversationStartParams { + output_modality: params.output_modality, prompt: params.prompt, session_id: params.session_id, transport: params.transport.map(|transport| match transport { diff --git a/codex-rs/app-server/tests/suite/v2/experimental_api.rs b/codex-rs/app-server/tests/suite/v2/experimental_api.rs index 25a607390e..2fd457faf2 100644 --- a/codex-rs/app-server/tests/suite/v2/experimental_api.rs +++ b/codex-rs/app-server/tests/suite/v2/experimental_api.rs @@ -17,6 +17,7 @@ use codex_app_server_protocol::ThreadRealtimeStartParams; use codex_app_server_protocol::ThreadRealtimeStartTransport; use codex_app_server_protocol::ThreadStartParams; use codex_app_server_protocol::ThreadStartResponse; +use codex_protocol::protocol::RealtimeOutputModality; use pretty_assertions::assert_eq; use std::path::Path; use std::time::Duration; @@ -76,6 +77,7 @@ async fn realtime_conversation_start_requires_experimental_api_capability() -> R let request_id = mcp .send_thread_realtime_start_request(ThreadRealtimeStartParams { thread_id: "thr_123".to_string(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("hello".to_string())), session_id: None, transport: None, @@ -145,6 +147,7 @@ async fn realtime_webrtc_start_requires_experimental_api_capability() -> Result< let request_id = mcp .send_thread_realtime_start_request(ThreadRealtimeStartParams { thread_id: "thr_123".to_string(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("hello".to_string())), session_id: None, transport: Some(ThreadRealtimeStartTransport::Webrtc { diff --git a/codex-rs/app-server/tests/suite/v2/realtime_conversation.rs b/codex-rs/app-server/tests/suite/v2/realtime_conversation.rs index 59a815c144..ab593381b2 100644 --- a/codex-rs/app-server/tests/suite/v2/realtime_conversation.rs +++ b/codex-rs/app-server/tests/suite/v2/realtime_conversation.rs @@ -31,7 +31,8 @@ use codex_app_server_protocol::ThreadRealtimeStartTransport; use codex_app_server_protocol::ThreadRealtimeStartedNotification; use codex_app_server_protocol::ThreadRealtimeStopParams; use codex_app_server_protocol::ThreadRealtimeStopResponse; -use codex_app_server_protocol::ThreadRealtimeTranscriptUpdatedNotification; +use codex_app_server_protocol::ThreadRealtimeTranscriptDeltaNotification; +use codex_app_server_protocol::ThreadRealtimeTranscriptDoneNotification; use codex_app_server_protocol::ThreadStartParams; use codex_app_server_protocol::ThreadStartResponse; use codex_app_server_protocol::TurnCompletedNotification; @@ -39,6 +40,7 @@ use codex_app_server_protocol::TurnStartedNotification; use codex_features::FEATURES; use codex_features::Feature; use codex_protocol::protocol::RealtimeConversationVersion; +use codex_protocol::protocol::RealtimeOutputModality; use codex_protocol::protocol::RealtimeVoice; use codex_protocol::protocol::RealtimeVoicesList; use core_test_support::responses; @@ -301,6 +303,7 @@ impl RealtimeE2eHarness { .mcp .send_thread_realtime_start_request(ThreadRealtimeStartParams { thread_id: self.thread_id.clone(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: Some(ThreadRealtimeStartTransport::Webrtc { @@ -478,6 +481,15 @@ async fn realtime_conversation_streams_v2_notifications() -> Result<()> { "type": "response.output_text.delta", "delta": "working" }), + json!({ + "type": "conversation.item.done", + "item": { + "id": "item_assistant_1", + "type": "message", + "role": "assistant", + "content": [{ "type": "output_text", "text": "working on it" }] + } + }), json!({ "type": "conversation.item.done", "item": { @@ -523,6 +535,7 @@ async fn realtime_conversation_streams_v2_notifications() -> Result<()> { let start_request_id = mcp .send_thread_realtime_start_request(ThreadRealtimeStartParams { thread_id: thread_start.thread.id.clone(), + output_modality: RealtimeOutputModality::Audio, prompt: None, session_id: None, transport: None, @@ -554,6 +567,10 @@ async fn realtime_conversation_streams_v2_notifications() -> Result<()> { startup_context_request.body_json()["session"]["audio"]["output"]["voice"], "cedar" ); + assert_eq!( + startup_context_request.body_json()["session"]["output_modalities"], + json!(["audio"]) + ); let startup_context_instructions = startup_context_request.body_json()["session"]["instructions"] .as_str() @@ -612,24 +629,32 @@ async fn realtime_conversation_streams_v2_notifications() -> Result<()> { assert_eq!(item_added.thread_id, output_audio.thread_id); assert_eq!(item_added.item["type"], json!("message")); - let first_transcript_update = read_notification::( + let first_transcript_delta = read_notification::( &mut mcp, - "thread/realtime/transcriptUpdated", + "thread/realtime/transcript/delta", ) .await?; - assert_eq!(first_transcript_update.thread_id, output_audio.thread_id); - assert_eq!(first_transcript_update.role, "user"); - assert_eq!(first_transcript_update.text, "delegate now"); + assert_eq!(first_transcript_delta.thread_id, output_audio.thread_id); + assert_eq!(first_transcript_delta.role, "user"); + assert_eq!(first_transcript_delta.delta, "delegate now"); - let second_transcript_update = - read_notification::( - &mut mcp, - "thread/realtime/transcriptUpdated", - ) - .await?; - assert_eq!(second_transcript_update.thread_id, output_audio.thread_id); - assert_eq!(second_transcript_update.role, "assistant"); - assert_eq!(second_transcript_update.text, "working"); + let second_transcript_delta = read_notification::( + &mut mcp, + "thread/realtime/transcript/delta", + ) + .await?; + assert_eq!(second_transcript_delta.thread_id, output_audio.thread_id); + assert_eq!(second_transcript_delta.role, "assistant"); + assert_eq!(second_transcript_delta.delta, "working"); + + let final_transcript_done = read_notification::( + &mut mcp, + "thread/realtime/transcript/done", + ) + .await?; + assert_eq!(final_transcript_done.thread_id, output_audio.thread_id); + assert_eq!(final_transcript_done.role, "assistant"); + assert_eq!(final_transcript_done.text, "working on it"); let handoff_item_added = read_notification::( &mut mcp, @@ -693,6 +718,140 @@ async fn realtime_conversation_streams_v2_notifications() -> Result<()> { Ok(()) } +#[tokio::test] +async fn realtime_text_output_modality_requests_text_output_and_final_transcript() -> Result<()> { + skip_if_no_network!(Ok(())); + + let responses_server = create_mock_responses_server_sequence_unchecked(Vec::new()).await; + let realtime_server = start_websocket_server(vec![vec![vec![ + json!({ + "type": "session.updated", + "session": { "id": "sess_text", "instructions": "backend prompt" } + }), + json!({ + "type": "response.output_text.delta", + "delta": "hello " + }), + json!({ + "type": "response.output_text.delta", + "delta": "world" + }), + json!({ + "type": "response.output_audio_transcript.done", + "transcript": "hello world" + }), + json!({ + "type": "conversation.item.done", + "item": { + "id": "item_output_1", + "type": "message", + "role": "assistant", + "content": [{"type": "output_text", "text": "hello world"}] + } + }), + ]]]) + .await; + + let codex_home = TempDir::new()?; + create_config_toml( + codex_home.path(), + &responses_server.uri(), + realtime_server.uri(), + /*realtime_enabled*/ true, + StartupContextConfig::Generated, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + mcp.initialize().await?; + login_with_api_key(&mut mcp, "sk-test-key").await?; + + let thread_start_request_id = mcp + .send_thread_start_request(ThreadStartParams::default()) + .await?; + let thread_start_response: JSONRPCResponse = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(thread_start_request_id)), + ) + .await??; + let thread_start: ThreadStartResponse = to_response(thread_start_response)?; + + let start_request_id = mcp + .send_thread_realtime_start_request(ThreadRealtimeStartParams { + thread_id: thread_start.thread.id.clone(), + output_modality: RealtimeOutputModality::Text, + prompt: None, + session_id: None, + transport: None, + voice: None, + }) + .await?; + let start_response: JSONRPCResponse = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(start_request_id)), + ) + .await??; + let _: ThreadRealtimeStartResponse = to_response(start_response)?; + + let session_update = realtime_server + .wait_for_request(/*connection_index*/ 0, /*request_index*/ 0) + .await; + assert_eq!( + session_update.body_json()["session"]["output_modalities"], + json!(["text"]) + ); + + let first_delta = read_notification::( + &mut mcp, + "thread/realtime/transcript/delta", + ) + .await?; + let second_delta = read_notification::( + &mut mcp, + "thread/realtime/transcript/delta", + ) + .await?; + let done = read_notification::( + &mut mcp, + "thread/realtime/transcript/done", + ) + .await?; + assert_eq!( + vec![first_delta, second_delta], + vec![ + ThreadRealtimeTranscriptDeltaNotification { + thread_id: thread_start.thread.id.clone(), + role: "assistant".to_string(), + delta: "hello ".to_string(), + }, + ThreadRealtimeTranscriptDeltaNotification { + thread_id: thread_start.thread.id.clone(), + role: "assistant".to_string(), + delta: "world".to_string(), + }, + ] + ); + assert_eq!( + done, + ThreadRealtimeTranscriptDoneNotification { + thread_id: thread_start.thread.id, + role: "assistant".to_string(), + text: "hello world".to_string(), + } + ); + assert!( + timeout( + Duration::from_millis(200), + mcp.read_stream_until_notification_message("thread/realtime/transcript/done"), + ) + .await + .is_err(), + "should not emit duplicate transcript done from audio transcript done" + ); + + realtime_server.shutdown().await; + Ok(()) +} + #[tokio::test] async fn realtime_list_voices_returns_supported_names() -> Result<()> { let codex_home = TempDir::new()?; @@ -793,6 +952,7 @@ async fn realtime_conversation_stop_emits_closed_notification() -> Result<()> { let start_request_id = mcp .send_thread_realtime_start_request(ThreadRealtimeStartParams { thread_id: thread_start.thread.id.clone(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -889,6 +1049,7 @@ async fn realtime_webrtc_start_emits_sdp_notification() -> Result<()> { let start_request_id = mcp .send_thread_realtime_start_request(ThreadRealtimeStartParams { thread_id: thread_id.clone(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: Some(ThreadRealtimeStartTransport::Webrtc { @@ -1163,11 +1324,11 @@ async fn webrtc_v2_forwards_audio_and_text_between_client_and_sideband() -> Resu harness.append_text(thread_id, "hello").await?; let transcript = harness - .read_notification::( - "thread/realtime/transcriptUpdated", + .read_notification::( + "thread/realtime/transcript/delta", ) .await?; - assert_eq!(transcript.text, "transcribed audio"); + assert_eq!(transcript.delta, "transcribed audio"); let output_audio = harness .read_notification::( "thread/realtime/outputAudio/delta", @@ -1252,11 +1413,11 @@ async fn webrtc_v2_text_input_is_append_only_while_response_is_active() -> Resul "first", ); let transcript = harness - .read_notification::( - "thread/realtime/transcriptUpdated", + .read_notification::( + "thread/realtime/transcript/delta", ) .await?; - assert_eq!(transcript.text, "active response started"); + assert_eq!(transcript.delta, "active response started"); // Phase 3: send a second text turn while `resp_active` is still open. The // user message must reach realtime without requesting another response. @@ -1736,6 +1897,7 @@ async fn realtime_webrtc_start_surfaces_backend_error() -> Result<()> { let start_request_id = mcp .send_thread_realtime_start_request(ThreadRealtimeStartParams { thread_id: thread_start.thread.id, + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: Some(ThreadRealtimeStartTransport::Webrtc { @@ -1794,6 +1956,7 @@ async fn realtime_conversation_requires_feature_flag() -> Result<()> { let start_request_id = mcp .send_thread_realtime_start_request(ThreadRealtimeStartParams { thread_id: thread_start.thread.id.clone(), + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, diff --git a/codex-rs/codex-api/src/endpoint/mod.rs b/codex-rs/codex-api/src/endpoint/mod.rs index 4a208317a9..c16687ff28 100644 --- a/codex-rs/codex-api/src/endpoint/mod.rs +++ b/codex-rs/codex-api/src/endpoint/mod.rs @@ -13,6 +13,7 @@ pub use models::ModelsClient; pub use realtime_call::RealtimeCallClient; pub use realtime_call::RealtimeCallResponse; pub use realtime_websocket::RealtimeEventParser; +pub use realtime_websocket::RealtimeOutputModality; pub use realtime_websocket::RealtimeSessionConfig; pub use realtime_websocket::RealtimeSessionMode; pub use realtime_websocket::RealtimeWebsocketClient; diff --git a/codex-rs/codex-api/src/endpoint/realtime_call.rs b/codex-rs/codex-api/src/endpoint/realtime_call.rs index 6b02a0a2c7..5f0ad35269 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_call.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_call.rs @@ -221,6 +221,7 @@ fn decode_call_id_from_location(headers: &HeaderMap) -> Result mod tests { use super::*; use crate::endpoint::realtime_websocket::RealtimeEventParser; + use crate::endpoint::realtime_websocket::RealtimeOutputModality; use crate::endpoint::realtime_websocket::RealtimeSessionMode; use crate::provider::RetryConfig; use async_trait::async_trait; @@ -311,6 +312,7 @@ mod tests { session_id: Some(session_id.to_string()), event_parser: RealtimeEventParser::RealtimeV2, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Marin, } } diff --git a/codex-rs/codex-api/src/endpoint/realtime_websocket/methods.rs b/codex-rs/codex-api/src/endpoint/realtime_websocket/methods.rs index a2681f4969..b1fd1ef75c 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_websocket/methods.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_websocket/methods.rs @@ -7,9 +7,9 @@ use crate::endpoint::realtime_websocket::protocol::RealtimeAudioFrame; use crate::endpoint::realtime_websocket::protocol::RealtimeEvent; use crate::endpoint::realtime_websocket::protocol::RealtimeEventParser; use crate::endpoint::realtime_websocket::protocol::RealtimeOutboundMessage; +use crate::endpoint::realtime_websocket::protocol::RealtimeOutputModality; use crate::endpoint::realtime_websocket::protocol::RealtimeSessionConfig; use crate::endpoint::realtime_websocket::protocol::RealtimeSessionMode; -use crate::endpoint::realtime_websocket::protocol::RealtimeTranscriptDelta; use crate::endpoint::realtime_websocket::protocol::RealtimeTranscriptEntry; use crate::endpoint::realtime_websocket::protocol::RealtimeVoice; use crate::endpoint::realtime_websocket::protocol::parse_realtime_event; @@ -17,6 +17,7 @@ use crate::error::ApiError; use crate::provider::Provider; use codex_client::backoff; use codex_client::maybe_build_rustls_client_config_with_custom_ca; +use codex_protocol::protocol::RealtimeTranscriptDelta; use codex_utils_rustls_provider::ensure_rustls_crypto_provider; use futures::SinkExt; use futures::StreamExt; @@ -307,10 +308,17 @@ impl RealtimeWebsocketWriter { &self, instructions: String, session_mode: RealtimeSessionMode, + output_modality: RealtimeOutputModality, voice: RealtimeVoice, ) -> Result<(), ApiError> { let session_mode = normalized_session_mode(self.event_parser, session_mode); - let session = session_update_session(self.event_parser, instructions, session_mode, voice); + let session = session_update_session( + self.event_parser, + instructions, + session_mode, + output_modality, + voice, + ); self.send_json(&RealtimeOutboundMessage::SessionUpdate { session }) .await } @@ -406,10 +414,10 @@ impl RealtimeWebsocketEvents { let mut active_transcript = self.active_transcript.lock().await; match event { RealtimeEvent::InputAudioSpeechStarted(_) => {} - RealtimeEvent::InputTranscriptDelta(RealtimeTranscriptDelta { delta }) => { + RealtimeEvent::InputTranscriptDelta(RealtimeTranscriptDelta { delta, .. }) => { append_transcript_delta(&mut active_transcript.entries, "user", delta); } - RealtimeEvent::OutputTranscriptDelta(RealtimeTranscriptDelta { delta }) => { + RealtimeEvent::OutputTranscriptDelta(RealtimeTranscriptDelta { delta, .. }) => { append_transcript_delta(&mut active_transcript.entries, "assistant", delta); } RealtimeEvent::HandoffRequested(handoff) => { @@ -418,6 +426,8 @@ impl RealtimeWebsocketEvents { } } RealtimeEvent::SessionUpdated { .. } + | RealtimeEvent::InputTranscriptDone(_) + | RealtimeEvent::OutputTranscriptDone(_) | RealtimeEvent::AudioOut(_) | RealtimeEvent::ResponseCreated(_) | RealtimeEvent::ResponseCancelled(_) @@ -581,7 +591,12 @@ impl RealtimeWebsocketClient { ); connection .writer - .send_session_update(config.instructions, config.session_mode, config.voice) + .send_session_update( + config.instructions, + config.session_mode, + config.output_modality, + config.voice, + ) .await?; Ok(connection) } @@ -721,13 +736,14 @@ fn normalize_realtime_path(url: &mut Url) { #[cfg(test)] mod tests { use super::*; - use crate::endpoint::realtime_websocket::protocol::RealtimeTranscriptDelta; use crate::endpoint::realtime_websocket::protocol::RealtimeTranscriptEntry; use codex_protocol::protocol::RealtimeHandoffRequested; use codex_protocol::protocol::RealtimeInputAudioSpeechStarted; use codex_protocol::protocol::RealtimeResponseCancelled; use codex_protocol::protocol::RealtimeResponseCreated; use codex_protocol::protocol::RealtimeResponseDone; + use codex_protocol::protocol::RealtimeTranscriptDelta; + use codex_protocol::protocol::RealtimeTranscriptDone; use codex_protocol::protocol::RealtimeVoice; use http::HeaderValue; use pretty_assertions::assert_eq; @@ -894,6 +910,8 @@ mod tests { fn parse_realtime_v2_input_audio_transcription_delta_event() { let payload = json!({ "type": "conversation.item.input_audio_transcription.delta", + "item_id": "item_input_1", + "content_index": 0, "delta": "hello" }) .to_string(); @@ -908,6 +926,32 @@ mod tests { ); } + #[test] + fn parse_realtime_v2_item_done_output_text_event() { + let payload = json!({ + "type": "conversation.item.done", + "item": { + "id": "item_output_1", + "type": "message", + "role": "assistant", + "content": [ + {"type": "output_text", "text": "hello"}, + {"type": "output_text", "text": " world"} + ] + } + }) + .to_string(); + + assert_eq!( + parse_realtime_event(payload.as_str(), RealtimeEventParser::RealtimeV2), + Some(RealtimeEvent::OutputTranscriptDone( + RealtimeTranscriptDone { + text: "hello world".to_string(), + } + )) + ); + } + #[test] fn parse_realtime_v2_output_audio_delta_defaults_audio_shape() { let payload = json!({ @@ -1374,6 +1418,7 @@ mod tests { session_id: Some("conv_1".to_string()), event_parser: RealtimeEventParser::V1, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Breeze, }, HeaderMap::new(), @@ -1648,6 +1693,7 @@ mod tests { session_id: Some("conv_1".to_string()), event_parser: RealtimeEventParser::RealtimeV2, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Cedar, }, HeaderMap::new(), @@ -1753,6 +1799,7 @@ mod tests { session_id: Some("conv_1".to_string()), event_parser: RealtimeEventParser::RealtimeV2, session_mode: RealtimeSessionMode::Transcription, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Marin, }, HeaderMap::new(), @@ -1856,6 +1903,7 @@ mod tests { session_id: Some("conv_1".to_string()), event_parser: RealtimeEventParser::V1, session_mode: RealtimeSessionMode::Transcription, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Cove, }, HeaderMap::new(), @@ -1945,6 +1993,7 @@ mod tests { session_id: Some("conv_1".to_string()), event_parser: RealtimeEventParser::V1, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Cove, }, HeaderMap::new(), diff --git a/codex-rs/codex-api/src/endpoint/realtime_websocket/methods_common.rs b/codex-rs/codex-api/src/endpoint/realtime_websocket/methods_common.rs index 8eb079fe83..67345bdc7e 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_websocket/methods_common.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_websocket/methods_common.rs @@ -8,6 +8,7 @@ use crate::endpoint::realtime_websocket::methods_v2::session_update_session as v use crate::endpoint::realtime_websocket::methods_v2::websocket_intent as v2_websocket_intent; use crate::endpoint::realtime_websocket::protocol::RealtimeEventParser; use crate::endpoint::realtime_websocket::protocol::RealtimeOutboundMessage; +use crate::endpoint::realtime_websocket::protocol::RealtimeOutputModality; use crate::endpoint::realtime_websocket::protocol::RealtimeSessionConfig; use crate::endpoint::realtime_websocket::protocol::RealtimeSessionMode; use crate::endpoint::realtime_websocket::protocol::RealtimeVoice; @@ -57,13 +58,14 @@ pub(super) fn session_update_session( event_parser: RealtimeEventParser, instructions: String, session_mode: RealtimeSessionMode, + output_modality: RealtimeOutputModality, voice: RealtimeVoice, ) -> SessionUpdateSession { let session_mode = normalized_session_mode(event_parser, session_mode); match event_parser { RealtimeEventParser::V1 => v1_session_update_session(instructions, voice), RealtimeEventParser::RealtimeV2 => { - v2_session_update_session(instructions, session_mode, voice) + v2_session_update_session(instructions, session_mode, output_modality, voice) } } } @@ -73,6 +75,7 @@ pub fn session_update_session_json(config: RealtimeSessionConfig) -> JsonResult< config.event_parser, config.instructions, config.session_mode, + config.output_modality, config.voice, ); session.id = config.session_id; diff --git a/codex-rs/codex-api/src/endpoint/realtime_websocket/methods_v2.rs b/codex-rs/codex-api/src/endpoint/realtime_websocket/methods_v2.rs index c8881a7f06..f0f81d95eb 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_websocket/methods_v2.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_websocket/methods_v2.rs @@ -9,6 +9,7 @@ use crate::endpoint::realtime_websocket::protocol::ConversationMessageItem; use crate::endpoint::realtime_websocket::protocol::ConversationRole; use crate::endpoint::realtime_websocket::protocol::NoiseReductionType; use crate::endpoint::realtime_websocket::protocol::RealtimeOutboundMessage; +use crate::endpoint::realtime_websocket::protocol::RealtimeOutputModality; use crate::endpoint::realtime_websocket::protocol::RealtimeSessionMode; use crate::endpoint::realtime_websocket::protocol::RealtimeVoice; use crate::endpoint::realtime_websocket::protocol::SessionAudio; @@ -26,6 +27,7 @@ use crate::endpoint::realtime_websocket::protocol::TurnDetectionType; use serde_json::json; const REALTIME_V2_OUTPUT_MODALITY_AUDIO: &str = "audio"; +const REALTIME_V2_OUTPUT_MODALITY_TEXT: &str = "text"; const REALTIME_V2_TOOL_CHOICE: &str = "auto"; const REALTIME_V2_BACKGROUND_AGENT_TOOL_NAME: &str = "background_agent"; const REALTIME_V2_BACKGROUND_AGENT_TOOL_DESCRIPTION: &str = "Send a user request to the background agent. Use this as the default action. Do not rephrase the user's ask or rewrite it in your own words; pass along the user's own words. If the background agent is idle, this starts a new task and returns the final result to the user. If the background agent is already working on a task, this sends the request as guidance to steer that previous task. If the user asks to do something next, later, after this, or once current work finishes, call this tool so the work is actually queued instead of merely promising to do it later."; @@ -59,6 +61,7 @@ pub(super) fn conversation_handoff_append_message( pub(super) fn session_update_session( instructions: String, session_mode: RealtimeSessionMode, + output_modality: RealtimeOutputModality, voice: RealtimeVoice, ) -> SessionUpdateSession { match session_mode { @@ -67,7 +70,7 @@ pub(super) fn session_update_session( r#type: SessionType::Realtime, model: None, instructions: Some(instructions), - output_modalities: Some(vec![REALTIME_V2_OUTPUT_MODALITY_AUDIO.to_string()]), + output_modalities: Some(vec![output_modality_value(output_modality).to_string()]), audio: SessionAudio { input: SessionAudioInput { format: SessionAudioFormat { @@ -132,6 +135,13 @@ pub(super) fn session_update_session( } } +fn output_modality_value(output_modality: RealtimeOutputModality) -> &'static str { + match output_modality { + RealtimeOutputModality::Text => REALTIME_V2_OUTPUT_MODALITY_TEXT, + RealtimeOutputModality::Audio => REALTIME_V2_OUTPUT_MODALITY_AUDIO, + } +} + pub(super) fn websocket_intent() -> Option<&'static str> { None } diff --git a/codex-rs/codex-api/src/endpoint/realtime_websocket/mod.rs b/codex-rs/codex-api/src/endpoint/realtime_websocket/mod.rs index 4031e01286..1fb49b2436 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_websocket/mod.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_websocket/mod.rs @@ -13,5 +13,6 @@ pub use methods::RealtimeWebsocketEvents; pub use methods::RealtimeWebsocketWriter; pub use methods_common::session_update_session_json; pub use protocol::RealtimeEventParser; +pub use protocol::RealtimeOutputModality; pub use protocol::RealtimeSessionConfig; pub use protocol::RealtimeSessionMode; diff --git a/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol.rs b/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol.rs index 0185984c61..0706ea2422 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol.rs @@ -2,7 +2,7 @@ use crate::endpoint::realtime_websocket::protocol_v1::parse_realtime_event_v1; use crate::endpoint::realtime_websocket::protocol_v2::parse_realtime_event_v2; pub use codex_protocol::protocol::RealtimeAudioFrame; pub use codex_protocol::protocol::RealtimeEvent; -pub use codex_protocol::protocol::RealtimeTranscriptDelta; +pub use codex_protocol::protocol::RealtimeOutputModality; pub use codex_protocol::protocol::RealtimeTranscriptEntry; pub use codex_protocol::protocol::RealtimeVoice; use serde::Serialize; @@ -27,6 +27,7 @@ pub struct RealtimeSessionConfig { pub session_id: Option, pub event_parser: RealtimeEventParser, pub session_mode: RealtimeSessionMode, + pub output_modality: RealtimeOutputModality, pub voice: RealtimeVoice, } diff --git a/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol_common.rs b/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol_common.rs index dbd8544d94..c89c5ea4d0 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol_common.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol_common.rs @@ -1,5 +1,6 @@ use codex_protocol::protocol::RealtimeEvent; use codex_protocol::protocol::RealtimeTranscriptDelta; +use codex_protocol::protocol::RealtimeTranscriptDone; use serde_json::Value; use tracing::debug; @@ -53,6 +54,17 @@ pub(super) fn parse_transcript_delta_event( .map(|delta| RealtimeTranscriptDelta { delta }) } +pub(super) fn parse_transcript_done_event( + parsed: &Value, + field: &str, +) -> Option { + parsed + .get(field) + .and_then(Value::as_str) + .map(str::to_string) + .map(|text| RealtimeTranscriptDone { text }) +} + pub(super) fn parse_error_event(parsed: &Value) -> Option { parsed .get("message") diff --git a/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol_v2.rs b/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol_v2.rs index 4c2c909e80..559e83426b 100644 --- a/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol_v2.rs +++ b/codex-rs/codex-api/src/endpoint/realtime_websocket/protocol_v2.rs @@ -2,6 +2,7 @@ use crate::endpoint::realtime_websocket::protocol_common::parse_error_event; use crate::endpoint::realtime_websocket::protocol_common::parse_realtime_payload; use crate::endpoint::realtime_websocket::protocol_common::parse_session_updated_event; use crate::endpoint::realtime_websocket::protocol_common::parse_transcript_delta_event; +use crate::endpoint::realtime_websocket::protocol_common::parse_transcript_done_event; use codex_protocol::protocol::RealtimeAudioFrame; use codex_protocol::protocol::RealtimeEvent; use codex_protocol::protocol::RealtimeHandoffRequested; @@ -9,6 +10,7 @@ use codex_protocol::protocol::RealtimeInputAudioSpeechStarted; use codex_protocol::protocol::RealtimeResponseCancelled; use codex_protocol::protocol::RealtimeResponseCreated; use codex_protocol::protocol::RealtimeResponseDone; +use codex_protocol::protocol::RealtimeTranscriptDone; use serde_json::Map as JsonMap; use serde_json::Value; use tracing::debug; @@ -30,8 +32,8 @@ pub(super) fn parse_realtime_event_v2(payload: &str) -> Option { parse_transcript_delta_event(&parsed, "delta").map(RealtimeEvent::InputTranscriptDelta) } "conversation.item.input_audio_transcription.completed" => { - parse_transcript_delta_event(&parsed, "transcript") - .map(RealtimeEvent::InputTranscriptDelta) + parse_transcript_done_event(&parsed, "transcript") + .map(RealtimeEvent::InputTranscriptDone) } "response.output_text.delta" | "response.output_audio_transcript.delta" => { parse_transcript_delta_event(&parsed, "delta").map(RealtimeEvent::OutputTranscriptDelta) @@ -120,12 +122,43 @@ fn parse_conversation_item_done_event(parsed: &Value) -> Option { return Some(handoff); } + if let Some(transcript_done) = parse_item_done_transcript(item) { + return Some(transcript_done); + } + item.get("id") .and_then(Value::as_str) .map(str::to_string) .map(|item_id| RealtimeEvent::ConversationItemDone { item_id }) } +fn parse_item_done_transcript(item: &JsonMap) -> Option { + let role = item.get("role").and_then(Value::as_str)?; + let text = item + .get("content") + .and_then(Value::as_array)? + .iter() + .filter_map(item_content_text) + .collect::(); + if text.is_empty() { + return None; + } + + let done = RealtimeTranscriptDone { text }; + match role { + "user" => Some(RealtimeEvent::InputTranscriptDone(done)), + "assistant" => Some(RealtimeEvent::OutputTranscriptDone(done)), + _ => None, + } +} + +fn item_content_text(content: &Value) -> Option<&str> { + content + .get("text") + .or_else(|| content.get("transcript")) + .and_then(Value::as_str) +} + fn parse_handoff_requested_event(item: &JsonMap) -> Option { let item_type = item.get("type").and_then(Value::as_str); let item_name = item.get("name").and_then(Value::as_str); diff --git a/codex-rs/codex-api/src/lib.rs b/codex-rs/codex-api/src/lib.rs index ac26d3cdba..f4f90b289c 100644 --- a/codex-rs/codex-api/src/lib.rs +++ b/codex-rs/codex-api/src/lib.rs @@ -41,6 +41,7 @@ pub use crate::endpoint::ModelsClient; pub use crate::endpoint::RealtimeCallClient; pub use crate::endpoint::RealtimeCallResponse; pub use crate::endpoint::RealtimeEventParser; +pub use crate::endpoint::RealtimeOutputModality; pub use crate::endpoint::RealtimeSessionConfig; pub use crate::endpoint::RealtimeSessionMode; pub use crate::endpoint::RealtimeWebsocketClient; diff --git a/codex-rs/codex-api/tests/realtime_websocket_e2e.rs b/codex-rs/codex-api/tests/realtime_websocket_e2e.rs index 9969a96f09..abafaef2ae 100644 --- a/codex-rs/codex-api/tests/realtime_websocket_e2e.rs +++ b/codex-rs/codex-api/tests/realtime_websocket_e2e.rs @@ -6,6 +6,7 @@ use codex_api::Provider; use codex_api::RealtimeAudioFrame; use codex_api::RealtimeEvent; use codex_api::RealtimeEventParser; +use codex_api::RealtimeOutputModality; use codex_api::RealtimeSessionConfig; use codex_api::RealtimeSessionMode; use codex_api::RealtimeWebsocketClient; @@ -145,6 +146,7 @@ async fn realtime_ws_e2e_session_create_and_event_flow() { session_id: Some("conv_123".to_string()), event_parser: RealtimeEventParser::V1, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Cove, }, HeaderMap::new(), @@ -248,6 +250,7 @@ async fn realtime_ws_connect_webrtc_sideband_retries_join_until_server_is_availa session_id: Some("conv_123".to_string()), event_parser: RealtimeEventParser::RealtimeV2, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Marin, }, "rtc_test", @@ -319,6 +322,7 @@ async fn realtime_ws_e2e_send_while_next_event_waits() { session_id: Some("conv_123".to_string()), event_parser: RealtimeEventParser::V1, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Cove, }, HeaderMap::new(), @@ -386,6 +390,7 @@ async fn realtime_ws_e2e_disconnected_emitted_once() { session_id: Some("conv_123".to_string()), event_parser: RealtimeEventParser::V1, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Cove, }, HeaderMap::new(), @@ -449,6 +454,7 @@ async fn realtime_ws_e2e_ignores_unknown_text_events() { session_id: Some("conv_123".to_string()), event_parser: RealtimeEventParser::V1, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Cove, }, HeaderMap::new(), @@ -515,6 +521,7 @@ async fn realtime_ws_e2e_realtime_v2_parser_emits_handoff_requested() { session_id: Some("conv_123".to_string()), event_parser: RealtimeEventParser::RealtimeV2, session_mode: RealtimeSessionMode::Conversational, + output_modality: RealtimeOutputModality::Audio, voice: RealtimeVoice::Marin, }, HeaderMap::new(), diff --git a/codex-rs/core/src/realtime_conversation.rs b/codex-rs/core/src/realtime_conversation.rs index 8200ba4908..e749736710 100644 --- a/codex-rs/core/src/realtime_conversation.rs +++ b/codex-rs/core/src/realtime_conversation.rs @@ -42,6 +42,7 @@ use codex_protocol::protocol::RealtimeConversationRealtimeEvent; use codex_protocol::protocol::RealtimeConversationSdpEvent; use codex_protocol::protocol::RealtimeConversationStartedEvent; use codex_protocol::protocol::RealtimeHandoffRequested; +use codex_protocol::protocol::RealtimeOutputModality; use codex_protocol::protocol::RealtimeVoice; use codex_protocol::protocol::RealtimeVoicesList; use http::HeaderMap; @@ -593,8 +594,14 @@ async fn prepare_realtime_start( api_provider.base_url = realtime_ws_base_url.clone(); } let version = config.realtime.version; - let session_config = - build_realtime_session_config(sess, params.prompt, params.session_id, params.voice).await?; + let session_config = build_realtime_session_config( + sess, + params.prompt, + params.session_id, + params.output_modality, + params.voice, + ) + .await?; let requested_session_id = session_config.session_id.clone(); let extra_headers = match transport { ConversationStartTransport::Websocket => { @@ -622,6 +629,7 @@ pub(crate) async fn build_realtime_session_config( sess: &Arc, prompt: Option>, session_id: Option, + output_modality: RealtimeOutputModality, voice: Option, ) -> CodexResult { let config = sess.get_config().await; @@ -653,6 +661,13 @@ pub(crate) async fn build_realtime_session_config( RealtimeWsVersion::V1 => RealtimeEventParser::V1, RealtimeWsVersion::V2 => RealtimeEventParser::RealtimeV2, }; + if config.realtime.version == RealtimeWsVersion::V1 + && matches!(output_modality, RealtimeOutputModality::Text) + { + return Err(CodexErr::InvalidRequest( + "text realtime output modality requires realtime v2".to_string(), + )); + } let session_mode = match config.realtime.session_type { RealtimeWsMode::Conversational => RealtimeSessionMode::Conversational, RealtimeWsMode::Transcription => RealtimeSessionMode::Transcription, @@ -667,6 +682,7 @@ pub(crate) async fn build_realtime_session_config( session_id: Some(session_id.unwrap_or_else(|| sess.conversation_id.to_string())), event_parser, session_mode, + output_modality, voice, }) } @@ -1216,7 +1232,9 @@ async fn handle_realtime_server_event( RealtimeEvent::Error(_) => true, RealtimeEvent::SessionUpdated { .. } | RealtimeEvent::InputTranscriptDelta(_) + | RealtimeEvent::InputTranscriptDone(_) | RealtimeEvent::OutputTranscriptDelta(_) + | RealtimeEvent::OutputTranscriptDone(_) | RealtimeEvent::ConversationItemAdded(_) | RealtimeEvent::ConversationItemDone { .. } => false, }; diff --git a/codex-rs/core/tests/suite/compact_remote.rs b/codex-rs/core/tests/suite/compact_remote.rs index a2681423f3..8322046d10 100644 --- a/codex-rs/core/tests/suite/compact_remote.rs +++ b/codex-rs/core/tests/suite/compact_remote.rs @@ -17,6 +17,7 @@ use codex_protocol::protocol::ItemStartedEvent; use codex_protocol::protocol::Op; use codex_protocol::protocol::RealtimeConversationRealtimeEvent; use codex_protocol::protocol::RealtimeEvent; +use codex_protocol::protocol::RealtimeOutputModality; use codex_protocol::protocol::RolloutItem; use codex_protocol::protocol::RolloutLine; use codex_protocol::user_input::UserInput; @@ -116,6 +117,7 @@ async fn start_remote_realtime_server() -> responses::WebSocketTestServer { async fn start_realtime_conversation(codex: &codex_core::CodexThread) -> Result<()> { codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, diff --git a/codex-rs/core/tests/suite/realtime_conversation.rs b/codex-rs/core/tests/suite/realtime_conversation.rs index c4e2c758c6..174491f9d6 100644 --- a/codex-rs/core/tests/suite/realtime_conversation.rs +++ b/codex-rs/core/tests/suite/realtime_conversation.rs @@ -21,6 +21,7 @@ use codex_protocol::protocol::RealtimeAudioFrame; use codex_protocol::protocol::RealtimeConversationRealtimeEvent; use codex_protocol::protocol::RealtimeConversationVersion; use codex_protocol::protocol::RealtimeEvent; +use codex_protocol::protocol::RealtimeOutputModality; use codex_protocol::protocol::RealtimeVoice; use codex_protocol::protocol::RolloutItem; use codex_protocol::protocol::SessionSource; @@ -248,6 +249,7 @@ async fn conversation_start_audio_text_close_round_trip() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -381,6 +383,7 @@ async fn conversation_start_defaults_to_v2_and_gpt_realtime_1_5() -> Result<()> test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -464,6 +467,7 @@ async fn conversation_webrtc_start_posts_generated_session() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: Some(ConversationStartTransport::Webrtc { @@ -601,6 +605,7 @@ async fn conversation_start_uses_openai_env_key_fallback_with_chatgpt_auth() -> test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -662,6 +667,7 @@ async fn conversation_transport_close_emits_closed_event() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -747,6 +753,7 @@ async fn conversation_start_preflight_failure_emits_realtime_error_only() -> Res test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -790,6 +797,7 @@ async fn conversation_start_connect_failure_emits_realtime_error_only() -> Resul test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -880,6 +888,7 @@ async fn conversation_second_start_replaces_runtime() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("old".to_string())), session_id: Some("conv_old".to_string()), transport: None, @@ -898,6 +907,7 @@ async fn conversation_second_start_replaces_runtime() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("new".to_string())), session_id: Some("conv_new".to_string()), transport: None, @@ -987,6 +997,7 @@ async fn conversation_uses_experimental_realtime_ws_base_url_override() -> Resul test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1044,6 +1055,7 @@ async fn conversation_uses_default_realtime_backend_prompt() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: None, session_id: None, transport: None, @@ -1109,6 +1121,7 @@ async fn conversation_uses_empty_instructions_for_null_or_empty_prompt() -> Resu ] { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt, session_id: None, transport: None, @@ -1167,6 +1180,7 @@ async fn conversation_uses_explicit_start_voice() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1217,6 +1231,7 @@ async fn conversation_uses_configured_realtime_voice() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1255,6 +1270,7 @@ async fn conversation_rejects_voice_for_wrong_realtime_version() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1298,6 +1314,7 @@ async fn conversation_uses_experimental_realtime_ws_backend_prompt_override() -> test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("prompt from op".to_string())), session_id: None, transport: None, @@ -1363,6 +1380,7 @@ async fn conversation_uses_experimental_realtime_ws_startup_context_override() - test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("prompt from op".to_string())), session_id: None, transport: None, @@ -1426,6 +1444,7 @@ async fn conversation_disables_realtime_startup_context_with_empty_override() -> test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("prompt from op".to_string())), session_id: None, transport: None, @@ -1482,6 +1501,7 @@ async fn conversation_start_injects_startup_context_from_thread_history() -> Res test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1593,6 +1613,7 @@ async fn conversation_startup_context_current_thread_selects_many_turns_by_budge codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1697,6 +1718,7 @@ async fn conversation_startup_context_falls_back_to_workspace_map() -> Result<() test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1751,6 +1773,7 @@ async fn conversation_startup_context_is_truncated_and_sent_once_per_start() -> test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1826,6 +1849,7 @@ async fn conversation_user_text_turn_is_sent_to_realtime_when_active() -> Result test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -1948,6 +1972,7 @@ async fn conversation_user_text_turn_is_capped_when_mirrored_to_realtime() -> Re // active WebSocket session. test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -2075,6 +2100,7 @@ async fn conversation_mirrors_assistant_message_text_to_realtime_handoff() -> Re test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -2204,6 +2230,7 @@ async fn conversation_handoff_persists_across_item_done_until_turn_complete() -> test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -2348,6 +2375,7 @@ async fn inbound_handoff_request_starts_turn() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -2445,6 +2473,7 @@ async fn inbound_handoff_request_uses_active_transcript() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -2540,6 +2569,7 @@ async fn inbound_handoff_request_clears_active_transcript_after_each_handoff() - test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -2642,6 +2672,7 @@ async fn inbound_conversation_item_does_not_start_turn_and_still_forwards_audio( test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -2757,6 +2788,7 @@ async fn delegated_turn_user_role_echo_does_not_redelegate_and_still_forwards_au test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -2902,6 +2934,7 @@ async fn inbound_handoff_request_does_not_block_realtime_event_forwarding() -> R test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -3032,6 +3065,7 @@ async fn inbound_handoff_request_steers_active_turn() -> Result<()> { test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, @@ -3183,6 +3217,7 @@ async fn inbound_handoff_request_starts_turn_and_does_not_block_realtime_audio() test.codex .submit(Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("backend prompt".to_string())), session_id: None, transport: None, diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 97c34097e8..f56e54fd21 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -134,6 +134,8 @@ pub struct McpServerRefreshConfig { #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, TS)] pub struct ConversationStartParams { + /// Selects whether the realtime session should produce text or audio output. + pub output_modality: RealtimeOutputModality, #[serde( default, deserialize_with = "conversation_start_prompt_serde::deserialize", @@ -157,6 +159,13 @@ pub enum ConversationStartTransport { Webrtc { sdp: String }, } +#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +pub enum RealtimeOutputModality { + Text, + Audio, +} + mod conversation_start_prompt_serde { use serde::Deserializer; use serde::Serializer; @@ -290,6 +299,11 @@ pub struct RealtimeTranscriptDelta { pub delta: String, } +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] +pub struct RealtimeTranscriptDone { + pub text: String, +} + #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] pub struct RealtimeTranscriptEntry { pub role: String, @@ -332,7 +346,9 @@ pub enum RealtimeEvent { }, InputAudioSpeechStarted(RealtimeInputAudioSpeechStarted), InputTranscriptDelta(RealtimeTranscriptDelta), + InputTranscriptDone(RealtimeTranscriptDone), OutputTranscriptDelta(RealtimeTranscriptDelta), + OutputTranscriptDone(RealtimeTranscriptDone), AudioOut(RealtimeAudioFrame), ResponseCreated(RealtimeResponseCreated), ResponseCancelled(RealtimeResponseCancelled), @@ -4594,12 +4610,14 @@ mod tests { }, }); let start = Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("be helpful".to_string())), session_id: Some("conv_1".to_string()), transport: None, voice: None, }); let webrtc_start = Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(Some("be helpful".to_string())), session_id: Some("conv_1".to_string()), transport: Some(ConversationStartTransport::Webrtc { @@ -4612,12 +4630,14 @@ mod tests { }); let close = Op::RealtimeConversationClose; let default_prompt_start = Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: None, session_id: None, transport: None, voice: None, }); let null_prompt_start = Op::RealtimeConversationStart(ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: Some(None), session_id: None, transport: None, @@ -4629,6 +4649,7 @@ mod tests { serde_json::to_value(&start).unwrap(), json!({ "type": "realtime_conversation_start", + "output_modality": "audio", "prompt": "be helpful", "session_id": "conv_1" }) @@ -4636,19 +4657,22 @@ mod tests { assert_eq!( serde_json::to_value(&default_prompt_start).unwrap(), json!({ - "type": "realtime_conversation_start" + "type": "realtime_conversation_start", + "output_modality": "audio" }) ); assert_eq!( serde_json::to_value(&null_prompt_start).unwrap(), json!({ "type": "realtime_conversation_start", + "output_modality": "audio", "prompt": null }) ); assert_eq!( serde_json::from_value::(json!({ - "type": "realtime_conversation_start" + "type": "realtime_conversation_start", + "output_modality": "audio" })) .unwrap(), default_prompt_start @@ -4656,6 +4680,7 @@ mod tests { assert_eq!( serde_json::from_value::(json!({ "type": "realtime_conversation_start", + "output_modality": "audio", "prompt": null })) .unwrap(), @@ -4701,6 +4726,7 @@ mod tests { serde_json::to_value(&webrtc_start).unwrap(), json!({ "type": "realtime_conversation_start", + "output_modality": "audio", "prompt": "be helpful", "session_id": "conv_1", "transport": { diff --git a/codex-rs/tui/src/app/app_server_adapter.rs b/codex-rs/tui/src/app/app_server_adapter.rs index 0bd78f353a..b5eccbfc6d 100644 --- a/codex-rs/tui/src/app/app_server_adapter.rs +++ b/codex-rs/tui/src/app/app_server_adapter.rs @@ -385,7 +385,10 @@ fn server_notification_thread_target( ServerNotification::ThreadRealtimeItemAdded(notification) => { Some(notification.thread_id.as_str()) } - ServerNotification::ThreadRealtimeTranscriptUpdated(notification) => { + ServerNotification::ThreadRealtimeTranscriptDelta(notification) => { + Some(notification.thread_id.as_str()) + } + ServerNotification::ThreadRealtimeTranscriptDone(notification) => { Some(notification.thread_id.as_str()) } ServerNotification::ThreadRealtimeOutputAudioDelta(notification) => { diff --git a/codex-rs/tui/src/app_server_session.rs b/codex-rs/tui/src/app_server_session.rs index d53d564da0..d27ad14760 100644 --- a/codex-rs/tui/src/app_server_session.rs +++ b/codex-rs/tui/src/app_server_session.rs @@ -657,6 +657,7 @@ impl AppServerSession { request_id, params: ThreadRealtimeStartParams { thread_id: thread_id.to_string(), + output_modality: params.output_modality, prompt: params.prompt, session_id: params.session_id, voice: params.voice, diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 7ae7bbc6c5..25f9472c8a 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -6272,7 +6272,8 @@ impl ChatWidget { | ServerNotification::FsChanged(_) | ServerNotification::FuzzyFileSearchSessionUpdated(_) | ServerNotification::FuzzyFileSearchSessionCompleted(_) - | ServerNotification::ThreadRealtimeTranscriptUpdated(_) + | ServerNotification::ThreadRealtimeTranscriptDelta(_) + | ServerNotification::ThreadRealtimeTranscriptDone(_) | ServerNotification::WindowsWorldWritableWarning(_) | ServerNotification::WindowsSandboxSetupCompleted(_) | ServerNotification::AccountLoginCompleted(_) => {} diff --git a/codex-rs/tui/src/chatwidget/realtime.rs b/codex-rs/tui/src/chatwidget/realtime.rs index 03a59c224c..6357361f8e 100644 --- a/codex-rs/tui/src/chatwidget/realtime.rs +++ b/codex-rs/tui/src/chatwidget/realtime.rs @@ -7,6 +7,7 @@ use codex_protocol::protocol::RealtimeConversationClosedEvent; use codex_protocol::protocol::RealtimeConversationRealtimeEvent; use codex_protocol::protocol::RealtimeConversationStartedEvent; use codex_protocol::protocol::RealtimeEvent; +use codex_protocol::protocol::RealtimeOutputModality; use codex_realtime_webrtc::RealtimeWebrtcEvent; use codex_realtime_webrtc::RealtimeWebrtcSession; use codex_realtime_webrtc::RealtimeWebrtcSessionHandle; @@ -236,6 +237,7 @@ impl ChatWidget { ) { self.submit_op(AppCommand::realtime_conversation_start( ConversationStartParams { + output_modality: RealtimeOutputModality::Audio, prompt: None, session_id: None, transport, @@ -327,7 +329,9 @@ impl ChatWidget { } RealtimeEvent::InputAudioSpeechStarted(_) => self.interrupt_realtime_audio_playback(), RealtimeEvent::InputTranscriptDelta(_) => {} + RealtimeEvent::InputTranscriptDone(_) => {} RealtimeEvent::OutputTranscriptDelta(_) => {} + RealtimeEvent::OutputTranscriptDone(_) => {} RealtimeEvent::AudioOut(frame) => self.enqueue_realtime_audio_out(&frame), RealtimeEvent::ResponseCreated(_) => {} RealtimeEvent::ResponseCancelled(_) => self.interrupt_realtime_audio_playback(), From 34a9ca083ee1e3ad478e51465e8a7fcfeabb1813 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Tue, 14 Apr 2026 13:44:01 +0100 Subject: [PATCH 07/16] nit: feature flag (#17777) --- codex-rs/core/config.schema.json | 6 ++++++ codex-rs/features/src/lib.rs | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index f47fee612a..7c3f6dd99a 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -485,6 +485,9 @@ "steer": { "type": "boolean" }, + "telepathy": { + "type": "boolean" + }, "tool_call_mcp_elicitation": { "type": "boolean" }, @@ -2338,6 +2341,9 @@ "steer": { "type": "boolean" }, + "telepathy": { + "type": "boolean" + }, "tool_call_mcp_elicitation": { "type": "boolean" }, diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index 10d5fdf884..7e7d3b5dc8 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -130,6 +130,8 @@ pub enum Feature { Sqlite, /// Enable startup memory extraction and file-backed memory consolidation. MemoryTool, + /// Enable the Telepathy sidecar for passive screen-context memories. + Telepathy, /// Append additional AGENTS.md guidance to user instructions. ChildAgentsMd, /// Allow the model to request `detail: "original"` image outputs on supported models. @@ -669,6 +671,12 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::UnderDevelopment, default_enabled: false, }, + FeatureSpec { + id: Feature::Telepathy, + key: "telepathy", + stage: Stage::UnderDevelopment, + default_enabled: false, + }, FeatureSpec { id: Feature::ChildAgentsMd, key: "child_agents_md", From e6947f85f6620390ebca1c48353773ece735c08e Mon Sep 17 00:00:00 2001 From: jif-oai Date: Tue, 14 Apr 2026 14:27:24 +0100 Subject: [PATCH 08/16] feat: add context percent to status line (#17637) Co-authored-by: Codex --- ..._snapshot_uses_runtime_preview_values.snap | 16 +++--- .../tui/src/bottom_pane/status_line_setup.rs | 19 +++++++ ...ning_context_remaining_percent_footer.snap | 9 ++++ .../tui/src/chatwidget/status_surfaces.rs | 2 +- .../src/chatwidget/tests/status_and_layout.rs | 52 +++++++++++++++++++ 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_line_model_with_reasoning_context_remaining_percent_footer.snap diff --git a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__status_line_setup__tests__setup_view_snapshot_uses_runtime_preview_values.snap b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__status_line_setup__tests__setup_view_snapshot_uses_runtime_preview_values.snap index 20aca1e334..679aa77f4a 100644 --- a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__status_line_setup__tests__setup_view_snapshot_uses_runtime_preview_values.snap +++ b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__status_line_setup__tests__setup_view_snapshot_uses_runtime_preview_values.snap @@ -8,14 +8,14 @@ expression: "render_lines(&view, 72)" Type to search > -› [x] model-name Current model name - [x] current-dir Current working directory - [x] git-branch Current Git branch (omitted when unavaila… - [ ] model-with-reasoning Current model name with reasoning level - [ ] project-root Project root directory (omitted when unav… - [ ] context-remaining Percentage of context window remaining (o… - [ ] context-used Percentage of context window used (omitte… - [ ] five-hour-limit Remaining usage on 5-hour usage limit (om… +› [x] model-name Current model name + [x] current-dir Current working directory + [x] git-branch Current Git branch (omitted when unavail… + [ ] model-with-reasoning Current model name with reasoning level + [ ] project-root Project root directory (omitted when una… + [ ] context-remaining Percentage of context window remaining (… + [ ] context-remaining-... Percentage of context window remaining (… + [ ] context-used Percentage of context window used (omitt… gpt-5-codex · ~/codex-rs · jif/statusline-preview Use ↑↓ to navigate, ←→ to move, space to select, enter to confirm, esc diff --git a/codex-rs/tui/src/bottom_pane/status_line_setup.rs b/codex-rs/tui/src/bottom_pane/status_line_setup.rs index 7265d61ad3..b5fe8456ed 100644 --- a/codex-rs/tui/src/bottom_pane/status_line_setup.rs +++ b/codex-rs/tui/src/bottom_pane/status_line_setup.rs @@ -66,6 +66,10 @@ pub(crate) enum StatusLineItem { /// Percentage of context window remaining. ContextRemaining, + /// Percentage of context window remaining. + #[strum(to_string = "context-remaining-percent")] + ContextRemainingPercent, + /// Percentage of context window used. /// /// Also accepts the legacy `context-usage` config value. @@ -115,6 +119,9 @@ impl StatusLineItem { StatusLineItem::ContextRemaining => { "Percentage of context window remaining (omitted when unknown)" } + StatusLineItem::ContextRemainingPercent => { + "Percentage of context window remaining (omitted when unknown)" + } StatusLineItem::ContextUsed => { "Percentage of context window used (omitted when unknown)" } @@ -325,6 +332,18 @@ mod tests { ); } + #[test] + fn context_remaining_percent_is_separate_selectable_id() { + assert_eq!( + StatusLineItem::ContextRemainingPercent.to_string(), + "context-remaining-percent" + ); + assert_eq!( + "context-remaining-percent".parse::(), + Ok(StatusLineItem::ContextRemainingPercent) + ); + } + #[test] fn preview_uses_runtime_values() { let preview_data = StatusLinePreviewData::from_iter([ diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_line_model_with_reasoning_context_remaining_percent_footer.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_line_model_with_reasoning_context_remaining_percent_footer.snap new file mode 100644 index 0000000000..6200fdf108 --- /dev/null +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__status_line_model_with_reasoning_context_remaining_percent_footer.snap @@ -0,0 +1,9 @@ +--- +source: tui/src/chatwidget/tests/status_and_layout.rs +expression: normalized_backend_snapshot(terminal.backend()) +--- +" " +" " +"› Ask Codex to do anything " +" " +" gpt-5.4 xhigh fast · Context 100% left · /tmp/project " diff --git a/codex-rs/tui/src/chatwidget/status_surfaces.rs b/codex-rs/tui/src/chatwidget/status_surfaces.rs index a744711c2c..ba95da93b3 100644 --- a/codex-rs/tui/src/chatwidget/status_surfaces.rs +++ b/codex-rs/tui/src/chatwidget/status_surfaces.rs @@ -450,7 +450,7 @@ impl ChatWidget { Some(format!("{} used", format_tokens_compact(total))) } } - StatusLineItem::ContextRemaining => self + StatusLineItem::ContextRemaining | StatusLineItem::ContextRemainingPercent => self .status_line_context_remaining_percent() .map(|remaining| format!("Context {remaining}% left")), StatusLineItem::ContextUsed => self diff --git a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs index 1c43d2e91d..37255c0f01 100644 --- a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs +++ b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs @@ -914,6 +914,24 @@ async fn status_line_legacy_context_usage_renders_context_used_percent() { ); } +#[tokio::test] +async fn status_line_context_remaining_percent_renders_labeled_percent() { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.thread_id = Some(ThreadId::new()); + chat.config.tui_status_line = Some(vec!["context-remaining-percent".to_string()]); + + chat.refresh_status_line(); + + assert_eq!( + status_line_text(&chat), + Some("Context 100% left".to_string()) + ); + assert!( + drain_insert_history(&mut rx).is_empty(), + "context-remaining-percent should remain a valid status line item" + ); +} + #[tokio::test] async fn status_line_branch_state_resets_when_git_branch_disabled() { let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; @@ -1181,6 +1199,40 @@ async fn status_line_model_with_reasoning_fast_footer_snapshot() { ); } +#[tokio::test] +async fn status_line_model_with_reasoning_context_remaining_percent_footer_snapshot() { + use ratatui::Terminal; + use ratatui::backend::TestBackend; + + let (mut chat, _rx, _op_rx) = make_chatwidget_manual(Some("gpt-5.4")).await; + set_fast_mode_test_catalog(&mut chat); + assert!(get_available_model(&chat, "gpt-5.4").supports_fast_mode()); + chat.show_welcome_banner = false; + chat.config.cwd = test_project_path().abs(); + chat.config.tui_status_line = Some(vec![ + "model-with-reasoning".to_string(), + "context-remaining-percent".to_string(), + "current-dir".to_string(), + ]); + chat.set_reasoning_effort(Some(ReasoningEffortConfig::XHigh)); + chat.set_service_tier(Some(ServiceTier::Fast)); + set_chatgpt_auth(&mut chat); + set_fast_mode_test_catalog(&mut chat); + assert!(get_available_model(&chat, "gpt-5.4").supports_fast_mode()); + chat.refresh_status_line(); + + let width = 80; + let height = chat.desired_height(width); + let mut terminal = Terminal::new(TestBackend::new(width, height)).expect("create terminal"); + terminal + .draw(|f| chat.render(f.area(), f.buffer_mut())) + .expect("draw model-with-reasoning footer"); + assert_chatwidget_snapshot!( + "status_line_model_with_reasoning_context_remaining_percent_footer", + normalized_backend_snapshot(terminal.backend()) + ); +} + #[tokio::test] async fn runtime_metrics_websocket_timing_logs_and_final_separator_sums_totals() { let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; From f030ab62ebee01cae5a4d18a7a167a81e3f836c4 Mon Sep 17 00:00:00 2001 From: Curtis 'Fjord' Hawthorne Date: Tue, 14 Apr 2026 08:15:56 -0700 Subject: [PATCH 09/16] Always enable original image detail on supported models (#17665) ## Summary This PR removes `image_detail_original` as a runtime experiment and makes original image detail available whenever the selected model supports it. Concretely, this change: - drops the `image_detail_original` feature flag from the feature registry and generated config schema - makes tool-emitted image detail depend only on `ModelInfo.supports_image_detail_original` - updates `view_image` and `code_mode`/`js_repl` image emission to use that capability check directly - removes now-redundant experiment-specific tests and instruction coverage - keeps backward compatibility for existing configs by silently ignoring a stale `features.image_detail_original` entry The net effect is that `detail: "original"` is always available on supported models, without requiring an experiment toggle. --- codex-rs/core/config.schema.json | 6 --- codex-rs/core/src/project_doc_tests.rs | 19 ------- .../core/src/tools/handlers/view_image.rs | 3 +- codex-rs/core/src/tools/js_repl/mod.rs | 2 +- codex-rs/core/src/tools/js_repl/mod_tests.rs | 39 +------------- codex-rs/core/tests/suite/code_mode.rs | 1 - codex-rs/core/tests/suite/view_image.rs | 47 +++------------- codex-rs/features/src/lib.rs | 15 ++---- codex-rs/features/src/tests.rs | 31 ++++++----- codex-rs/tools/src/image_detail.rs | 9 ++-- codex-rs/tools/src/image_detail_tests.rs | 29 +++------- codex-rs/tools/src/tool_config.rs | 2 +- .../tools/src/tool_registry_plan_tests.rs | 9 ++-- codex-rs/tools/src/view_image.rs | 4 -- codex-rs/tools/src/view_image_tests.rs | 54 ------------------- 15 files changed, 48 insertions(+), 222 deletions(-) delete mode 100644 codex-rs/tools/src/view_image_tests.rs diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 7c3f6dd99a..58c1eb4d12 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -398,9 +398,6 @@ "guardian_approval": { "type": "boolean" }, - "image_detail_original": { - "type": "boolean" - }, "image_generation": { "type": "boolean" }, @@ -2254,9 +2251,6 @@ "guardian_approval": { "type": "boolean" }, - "image_detail_original": { - "type": "boolean" - }, "image_generation": { "type": "boolean" }, diff --git a/codex-rs/core/src/project_doc_tests.rs b/codex-rs/core/src/project_doc_tests.rs index 47deeff950..705801317e 100644 --- a/codex-rs/core/src/project_doc_tests.rs +++ b/codex-rs/core/src/project_doc_tests.rs @@ -228,25 +228,6 @@ async fn js_repl_tools_only_instructions_are_feature_gated() { assert_eq!(res, expected); } -#[tokio::test] -async fn js_repl_image_detail_original_does_not_change_instructions() { - let tmp = tempfile::tempdir().expect("tempdir"); - let mut cfg = make_config(&tmp, /*limit*/ 4096, /*instructions*/ None).await; - let mut features = cfg.features.get().clone(); - features - .enable(Feature::JsRepl) - .enable(Feature::ImageDetailOriginal); - cfg.features - .set(features) - .expect("test config should allow js_repl image detail settings"); - - let res = get_user_instructions(&cfg) - .await - .expect("js_repl instructions expected"); - let expected = "## JavaScript REPL (Node)\n- Use `js_repl` for Node-backed JavaScript with top-level await in a persistent kernel.\n- `js_repl` is a freeform/custom tool. Direct `js_repl` calls must send raw JavaScript tool input (optionally with first-line `// codex-js-repl: timeout_ms=15000`). Do not wrap code in JSON (for example `{\"code\":\"...\"}`), quotes, or markdown code fences.\n- Helpers: `codex.cwd`, `codex.homeDir`, `codex.tmpDir`, `codex.tool(name, args?)`, and `codex.emitImage(imageLike)`.\n- `codex.tool` executes a normal tool call and resolves to the raw tool output object. Use it for shell and non-shell tools alike. Nested tool outputs stay inside JavaScript unless you emit them explicitly.\n- `codex.emitImage(...)` adds one image to the outer `js_repl` function output each time you call it, so you can call it multiple times to emit multiple images. It accepts a data URL, a single `input_image` item, an object like `{ bytes, mimeType }`, or a raw tool response object with exactly one image and no text. It rejects mixed text-and-image content.\n- `codex.tool(...)` and `codex.emitImage(...)` keep stable helper identities across cells. Saved references and persisted objects can reuse them in later cells, but async callbacks that fire after a cell finishes still fail because no exec is active.\n- Request full-resolution image processing with `detail: \"original\"` only when the `view_image` tool schema includes a `detail` argument. The same availability applies to `codex.emitImage(...)`: if `view_image.detail` is present, you may also pass `detail: \"original\"` there. Use this when high-fidelity image perception or precise localization is needed, especially for CUA agents.\n- Example of sharing an in-memory Playwright screenshot: `await codex.emitImage({ bytes: await page.screenshot({ type: \"jpeg\", quality: 85 }), mimeType: \"image/jpeg\", detail: \"original\" })`.\n- Example of sharing a local image tool result: `await codex.emitImage(codex.tool(\"view_image\", { path: \"/absolute/path\", detail: \"original\" }))`.\n- When encoding an image to send with `codex.emitImage(...)` or `view_image`, prefer JPEG at about 85 quality when lossy compression is acceptable; use PNG when transparency or lossless detail matters. Smaller uploads are faster and less likely to hit size limits.\n- Top-level bindings persist across cells. If a cell throws, prior bindings remain available and bindings that finished initializing before the throw often remain usable in later cells. For code you plan to reuse across cells, prefer declaring or assigning it in direct top-level statements before operations that might throw. If you hit `SyntaxError: Identifier 'x' has already been declared`, first reuse the existing binding, reassign a previously declared `let`, or pick a new descriptive name. Use `{ ... }` only for a short temporary block when you specifically need local scratch names; do not wrap an entire cell in block scope if you want those names reusable later. Reset the kernel with `js_repl_reset` only when you need a clean state.\n- Top-level static import declarations (for example `import x from \"./file.js\"`) are currently unsupported in `js_repl`; use dynamic imports with `await import(\"pkg\")`, `await import(\"./file.js\")`, or `await import(\"/abs/path/file.mjs\")` instead. Imported local files must be ESM `.js`/`.mjs` files and run in the same REPL VM context. Bare package imports always resolve from REPL-global search roots (`CODEX_JS_REPL_NODE_MODULE_DIRS`, then cwd), not relative to the imported file location. Local files may statically import only other local relative/absolute/`file://` `.js`/`.mjs` files; package and builtin imports from local files must stay dynamic. `import.meta.resolve()` returns importable strings such as `file://...`, bare package names, and `node:...` specifiers. Local file modules reload between execs, while top-level bindings persist until `js_repl_reset`.\n- Avoid direct access to `process.stdout` / `process.stderr` / `process.stdin`; it can corrupt the JSON line protocol. Use `console.log`, `codex.tool(...)`, and `codex.emitImage(...)`."; - assert_eq!(res, expected); -} - /// When both system instructions *and* a project doc are present the two /// should be concatenated with the separator. #[tokio::test] diff --git a/codex-rs/core/src/tools/handlers/view_image.rs b/codex-rs/core/src/tools/handlers/view_image.rs index 1c9ddd3153..7f7448f250 100644 --- a/codex-rs/core/src/tools/handlers/view_image.rs +++ b/codex-rs/core/src/tools/handlers/view_image.rs @@ -125,8 +125,7 @@ impl ToolHandler for ViewImageHandler { })?; let event_path = abs_path.to_path_buf(); - let can_request_original_detail = - can_request_original_image_detail(turn.features.get(), &turn.model_info); + let can_request_original_detail = can_request_original_image_detail(&turn.model_info); let use_original_detail = can_request_original_detail && matches!(detail, Some(ViewImageDetail::Original)); let image_mode = if use_original_detail { diff --git a/codex-rs/core/src/tools/js_repl/mod.rs b/codex-rs/core/src/tools/js_repl/mod.rs index 9e2e88a5aa..b728009499 100644 --- a/codex-rs/core/src/tools/js_repl/mod.rs +++ b/codex-rs/core/src/tools/js_repl/mod.rs @@ -1714,7 +1714,7 @@ fn emitted_image_content_item( ) -> FunctionCallOutputContentItem { FunctionCallOutputContentItem::InputImage { image_url, - detail: normalize_output_image_detail(turn.features.get(), &turn.model_info, detail), + detail: normalize_output_image_detail(&turn.model_info, detail), } } diff --git a/codex-rs/core/src/tools/js_repl/mod_tests.rs b/codex-rs/core/src/tools/js_repl/mod_tests.rs index bdf6931330..6eb0552e34 100644 --- a/codex-rs/core/src/tools/js_repl/mod_tests.rs +++ b/codex-rs/core/src/tools/js_repl/mod_tests.rs @@ -2,7 +2,6 @@ use super::*; use crate::codex::make_session_and_context; use crate::codex::make_session_and_context_with_dynamic_tools_and_rx; use crate::turn_diff_tracker::TurnDiffTracker; -use codex_features::Feature; use codex_protocol::dynamic_tools::DynamicToolCallOutputContentItem; use codex_protocol::dynamic_tools::DynamicToolResponse; use codex_protocol::dynamic_tools::DynamicToolSpec; @@ -295,42 +294,8 @@ async fn emitted_image_content_item_drops_unsupported_explicit_detail() { } #[tokio::test] -async fn emitted_image_content_item_does_not_force_original_when_enabled() { +async fn emitted_image_content_item_allows_explicit_original_detail_when_supported() { let (_session, mut turn) = make_session_and_context().await; - Arc::make_mut(&mut turn.config) - .features - .enable(Feature::ImageDetailOriginal) - .expect("test config should allow feature update"); - turn.features - .enable(Feature::ImageDetailOriginal) - .expect("test turn features should allow feature update"); - turn.model_info.supports_image_detail_original = true; - - let content_item = emitted_image_content_item( - &turn, - "data:image/png;base64,AAA".to_string(), - /*detail*/ None, - ); - - assert_eq!( - content_item, - FunctionCallOutputContentItem::InputImage { - image_url: "data:image/png;base64,AAA".to_string(), - detail: None, - } - ); -} - -#[tokio::test] -async fn emitted_image_content_item_allows_explicit_original_detail_when_enabled() { - let (_session, mut turn) = make_session_and_context().await; - Arc::make_mut(&mut turn.config) - .features - .enable(Feature::ImageDetailOriginal) - .expect("test config should allow feature update"); - turn.features - .enable(Feature::ImageDetailOriginal) - .expect("test turn features should allow feature update"); turn.model_info.supports_image_detail_original = true; let content_item = emitted_image_content_item( @@ -349,7 +314,7 @@ async fn emitted_image_content_item_allows_explicit_original_detail_when_enabled } #[tokio::test] -async fn emitted_image_content_item_drops_explicit_original_detail_when_disabled() { +async fn emitted_image_content_item_drops_explicit_original_detail_when_unsupported() { let (_session, turn) = make_session_and_context().await; let content_item = emitted_image_content_item( diff --git a/codex-rs/core/tests/suite/code_mode.rs b/codex-rs/core/tests/suite/code_mode.rs index 0c65158d3c..76a70d5076 100644 --- a/codex-rs/core/tests/suite/code_mode.rs +++ b/codex-rs/core/tests/suite/code_mode.rs @@ -1832,7 +1832,6 @@ async fn code_mode_can_use_view_image_result_with_image_helper() -> Result<()> { .with_model("gpt-5.3-codex") .with_config(move |config| { let _ = config.features.enable(Feature::CodeMode); - let _ = config.features.enable(Feature::ImageDetailOriginal); }); let test = builder.build(&server).await?; diff --git a/codex-rs/core/tests/suite/view_image.rs b/codex-rs/core/tests/suite/view_image.rs index c2dab9d178..c7b9c02466 100644 --- a/codex-rs/core/tests/suite/view_image.rs +++ b/codex-rs/core/tests/suite/view_image.rs @@ -3,7 +3,6 @@ use base64::Engine; use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; use codex_exec_server::CreateDirectoryOptions; -use codex_features::Feature; use codex_login::CodexAuth; use codex_protocol::config_types::ReasoningSummary; use codex_protocol::openai_models::ConfigShellToolType; @@ -361,14 +360,7 @@ async fn view_image_tool_can_preserve_original_resolution_when_requested_on_gpt5 skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex() - .with_model("gpt-5.3-codex") - .with_config(|config| { - config - .features - .enable(Feature::ImageDetailOriginal) - .expect("test config should allow feature update"); - }); + let mut builder = test_codex().with_model("gpt-5.3-codex"); let test = builder.build_remote_aware(&server).await?; let TestCodex { codex, @@ -469,14 +461,7 @@ async fn view_image_tool_errors_clearly_for_unsupported_detail_values() -> anyho skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex() - .with_model("gpt-5.3-codex") - .with_config(|config| { - config - .features - .enable(Feature::ImageDetailOriginal) - .expect("test config should allow feature update"); - }); + let mut builder = test_codex().with_model("gpt-5.3-codex"); let test = builder.build_remote_aware(&server).await?; let TestCodex { codex, @@ -559,14 +544,7 @@ async fn view_image_tool_treats_null_detail_as_omitted() -> anyhow::Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex() - .with_model("gpt-5.3-codex") - .with_config(|config| { - config - .features - .enable(Feature::ImageDetailOriginal) - .expect("test config should allow feature update"); - }); + let mut builder = test_codex().with_model("gpt-5.3-codex"); let test = builder.build_remote_aware(&server).await?; let TestCodex { codex, @@ -661,12 +639,7 @@ async fn view_image_tool_resizes_when_model_lacks_original_detail_support() -> a skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex().with_model("gpt-5.2").with_config(|config| { - config - .features - .enable(Feature::ImageDetailOriginal) - .expect("test config should allow feature update"); - }); + let mut builder = test_codex().with_model("gpt-5.2"); let test = builder.build_remote_aware(&server).await?; let TestCodex { codex, @@ -765,19 +738,12 @@ async fn view_image_tool_resizes_when_model_lacks_original_detail_support() -> a } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn view_image_tool_does_not_force_original_resolution_with_capability_feature_only() +async fn view_image_tool_does_not_force_original_resolution_with_capability_only() -> anyhow::Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; - let mut builder = test_codex() - .with_model("gpt-5.3-codex") - .with_config(|config| { - config - .features - .enable(Feature::ImageDetailOriginal) - .expect("test config should allow feature update"); - }); + let mut builder = test_codex().with_model("gpt-5.3-codex"); let test = builder.build_remote_aware(&server).await?; let TestCodex { codex, @@ -1533,3 +1499,4 @@ async fn replaces_invalid_local_image_after_bad_request() -> anyhow::Result<()> Ok(()) } +use codex_features::Feature; diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index 7e7d3b5dc8..aa32a45718 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -134,8 +134,6 @@ pub enum Feature { Telepathy, /// Append additional AGENTS.md guidance to user instructions. ChildAgentsMd, - /// Allow the model to request `detail: "original"` image outputs on supported models. - ImageDetailOriginal, /// Compress request bodies (zstd) when sending streaming requests to codex-backend. EnableRequestCompression, /// Enable collab tools. @@ -363,6 +361,9 @@ impl Features { "tui_app_server" => { continue; } + "image_detail_original" => { + continue; + } _ => {} } match feature_for_key(k) { @@ -683,16 +684,6 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::UnderDevelopment, default_enabled: false, }, - FeatureSpec { - id: Feature::ImageDetailOriginal, - key: "image_detail_original", - stage: Stage::Experimental { - name: "Original image detail", - menu_description: "Let the model inspect tool-emitted images at full resolution on supported models instead of a resized approximation. This affects tool-emitted images such as those produced by `view_image`, not images attached directly in the UI. It is particularly important for localization and precise UI targeting, for reading small text, and for reasoning about precise layout.", - announcement: "NEW: Original image detail is now available in /experimental. Enable it to let tools request full-resolution image detail on supported models for CUA and localization tasks.", - }, - default_enabled: false, - }, FeatureSpec { id: Feature::ApplyPatchFreeform, key: "apply_patch_freeform", diff --git a/codex-rs/features/src/tests.rs b/codex-rs/features/src/tests.rs index bc357a7fb4..3ecab0997c 100644 --- a/codex-rs/features/src/tests.rs +++ b/codex-rs/features/src/tests.rs @@ -169,18 +169,6 @@ fn use_agent_identity_is_under_development() { assert_eq!(Feature::UseAgentIdentity.default_enabled(), false); } -#[test] -fn image_detail_original_feature_is_experimental_and_user_toggleable() { - let stage = Feature::ImageDetailOriginal.stage(); - - assert!(matches!(stage, Stage::Experimental { .. })); - assert_eq!( - stage.experimental_menu_name(), - Some("Original image detail") - ); - assert_eq!(Feature::ImageDetailOriginal.default_enabled(), false); -} - #[test] fn collab_is_legacy_alias_for_multi_agent() { assert_eq!(feature_for_key("multi_agent"), Some(Feature::Collab)); @@ -263,6 +251,25 @@ fn from_sources_applies_base_profile_and_overrides() { assert_eq!(features.enabled(Feature::WebSearchRequest), false); } +#[test] +fn from_sources_ignores_removed_image_detail_original_feature_key() { + let features_toml = FeaturesToml::from(BTreeMap::from([( + "image_detail_original".to_string(), + true, + )])); + + let features = Features::from_sources( + FeatureConfigSource { + features: Some(&features_toml), + ..Default::default() + }, + FeatureConfigSource::default(), + FeatureOverrides::default(), + ); + + assert_eq!(features, Features::with_defaults()); +} + #[test] fn multi_agent_v2_feature_config_deserializes_boolean_toggle() { let features: FeaturesToml = toml::from_str( diff --git a/codex-rs/tools/src/image_detail.rs b/codex-rs/tools/src/image_detail.rs index 93c11aee82..94639e61f9 100644 --- a/codex-rs/tools/src/image_detail.rs +++ b/codex-rs/tools/src/image_detail.rs @@ -1,19 +1,16 @@ -use codex_features::Feature; -use codex_features::Features; use codex_protocol::models::ImageDetail; use codex_protocol::openai_models::ModelInfo; -pub fn can_request_original_image_detail(features: &Features, model_info: &ModelInfo) -> bool { - model_info.supports_image_detail_original && features.enabled(Feature::ImageDetailOriginal) +pub fn can_request_original_image_detail(model_info: &ModelInfo) -> bool { + model_info.supports_image_detail_original } pub fn normalize_output_image_detail( - features: &Features, model_info: &ModelInfo, detail: Option, ) -> Option { match detail { - Some(ImageDetail::Original) if can_request_original_image_detail(features, model_info) => { + Some(ImageDetail::Original) if can_request_original_image_detail(model_info) => { Some(ImageDetail::Original) } Some(ImageDetail::Original) | Some(_) | None => None, diff --git a/codex-rs/tools/src/image_detail_tests.rs b/codex-rs/tools/src/image_detail_tests.rs index cfaeb7fb26..35d666149f 100644 --- a/codex-rs/tools/src/image_detail_tests.rs +++ b/codex-rs/tools/src/image_detail_tests.rs @@ -1,6 +1,4 @@ use super::*; -use codex_features::Feature; -use codex_features::Features; use codex_protocol::models::ImageDetail; use codex_protocol::openai_models::ModelInfo; use pretty_assertions::assert_eq; @@ -42,37 +40,26 @@ fn model_info() -> ModelInfo { } #[test] -fn image_detail_original_feature_enables_explicit_original_without_force() { +fn explicit_original_is_allowed_when_model_supports_it() { let model_info = model_info(); - let mut features = Features::with_defaults(); - features.enable(Feature::ImageDetailOriginal); - assert!(can_request_original_image_detail(&features, &model_info)); + assert!(can_request_original_image_detail(&model_info)); assert_eq!( - normalize_output_image_detail(&features, &model_info, Some(ImageDetail::Original)), + normalize_output_image_detail(&model_info, Some(ImageDetail::Original)), Some(ImageDetail::Original) ); assert_eq!( - normalize_output_image_detail(&features, &model_info, /*detail*/ None), + normalize_output_image_detail(&model_info, /*detail*/ None), None ); } #[test] -fn explicit_original_is_dropped_without_feature_or_model_support() { +fn explicit_original_is_dropped_without_model_support() { let mut model_info = model_info(); - let features = Features::with_defaults(); - - assert_eq!( - normalize_output_image_detail(&features, &model_info, Some(ImageDetail::Original)), - None - ); - - let mut features = Features::with_defaults(); - features.enable(Feature::ImageDetailOriginal); model_info.supports_image_detail_original = false; assert_eq!( - normalize_output_image_detail(&features, &model_info, Some(ImageDetail::Original)), + normalize_output_image_detail(&model_info, Some(ImageDetail::Original)), None ); } @@ -80,11 +67,9 @@ fn explicit_original_is_dropped_without_feature_or_model_support() { #[test] fn unsupported_non_original_detail_is_dropped() { let model_info = model_info(); - let mut features = Features::with_defaults(); - features.enable(Feature::ImageDetailOriginal); assert_eq!( - normalize_output_image_detail(&features, &model_info, Some(ImageDetail::Low)), + normalize_output_image_detail(&model_info, Some(ImageDetail::Low)), None ); } diff --git a/codex-rs/tools/src/tool_config.rs b/codex-rs/tools/src/tool_config.rs index 912542d9f6..20c812ce96 100644 --- a/codex-rs/tools/src/tool_config.rs +++ b/codex-rs/tools/src/tool_config.rs @@ -153,7 +153,7 @@ impl ToolsConfig { let include_tool_suggest = features.enabled(Feature::ToolSuggest) && features.enabled(Feature::Apps) && features.enabled(Feature::Plugins); - let include_original_image_detail = can_request_original_image_detail(features, model_info); + let include_original_image_detail = can_request_original_image_detail(model_info); // API-key auth bypasses Codex backend entitlement/tool normalization, so // callers must confirm ChatGPT auth before exposing the built-in tool. let include_image_gen_tool = *image_generation_tool_auth_allowed diff --git a/codex-rs/tools/src/tool_registry_plan_tests.rs b/codex-rs/tools/src/tool_registry_plan_tests.rs index 9da58849c1..69b8b55b54 100644 --- a/codex-rs/tools/src/tool_registry_plan_tests.rs +++ b/codex-rs/tools/src/tool_registry_plan_tests.rs @@ -362,9 +362,9 @@ fn test_build_specs_enable_fanout_enables_agent_jobs_and_collab_tools() { } #[test] -fn view_image_tool_omits_detail_without_original_detail_feature() { +fn view_image_tool_omits_detail_without_original_detail_support() { let mut model_info = model_info(); - model_info.supports_image_detail_original = true; + model_info.supports_image_detail_original = false; let features = Features::with_defaults(); let available_models = Vec::new(); let tools_config = ToolsConfig::new(&ToolsConfigParams { @@ -392,11 +392,10 @@ fn view_image_tool_omits_detail_without_original_detail_feature() { } #[test] -fn view_image_tool_includes_detail_with_original_detail_feature() { +fn view_image_tool_includes_detail_with_original_detail_support() { let mut model_info = model_info(); model_info.supports_image_detail_original = true; - let mut features = Features::with_defaults(); - features.enable(Feature::ImageDetailOriginal); + let features = Features::with_defaults(); let available_models = Vec::new(); let tools_config = ToolsConfig::new(&ToolsConfigParams { model_info: &model_info, diff --git a/codex-rs/tools/src/view_image.rs b/codex-rs/tools/src/view_image.rs index 5d591a339f..1d77ceadf3 100644 --- a/codex-rs/tools/src/view_image.rs +++ b/codex-rs/tools/src/view_image.rs @@ -53,7 +53,3 @@ fn view_image_output_schema() -> Value { "additionalProperties": false }) } - -#[cfg(test)] -#[path = "view_image_tests.rs"] -mod tests; diff --git a/codex-rs/tools/src/view_image_tests.rs b/codex-rs/tools/src/view_image_tests.rs deleted file mode 100644 index 3d67829657..0000000000 --- a/codex-rs/tools/src/view_image_tests.rs +++ /dev/null @@ -1,54 +0,0 @@ -use super::*; -use crate::JsonSchema; -use pretty_assertions::assert_eq; -use std::collections::BTreeMap; - -#[test] -fn view_image_tool_omits_detail_without_original_detail_feature() { - assert_eq!( - create_view_image_tool(ViewImageToolOptions { - can_request_original_image_detail: false, - }), - ToolSpec::Function(ResponsesApiTool { - name: "view_image".to_string(), - description: "View a local image from the filesystem (only use if given a full filepath by the user, and the image isn't already attached to the thread context within tags)." - .to_string(), - strict: false, - defer_loading: None, - parameters: JsonSchema::object(BTreeMap::from([( - "path".to_string(), - JsonSchema::string(Some("Local filesystem path to an image file".to_string()),), - )]), Some(vec!["path".to_string()]), Some(false.into())), - output_schema: Some(view_image_output_schema()), - }) - ); -} - -#[test] -fn view_image_tool_includes_detail_with_original_detail_feature() { - assert_eq!( - create_view_image_tool(ViewImageToolOptions { - can_request_original_image_detail: true, - }), - ToolSpec::Function(ResponsesApiTool { - name: "view_image".to_string(), - description: "View a local image from the filesystem (only use if given a full filepath by the user, and the image isn't already attached to the thread context within tags)." - .to_string(), - strict: false, - defer_loading: None, - parameters: JsonSchema::object(BTreeMap::from([ - ( - "detail".to_string(), - JsonSchema::string(Some( - "Optional detail override. The only supported value is `original`; omit this field for default resized behavior. Use `original` to preserve the file's original resolution instead of resizing to fit. This is important when high-fidelity image perception or precise localization is needed, especially for CUA agents.".to_string(), - ),), - ), - ( - "path".to_string(), - JsonSchema::string(Some("Local filesystem path to an image file".to_string()),), - ), - ]), Some(vec!["path".to_string()]), Some(false.into())), - output_schema: Some(view_image_output_schema()), - }) - ); -} From 61fe23159ed275e444e9b7614bb59a3ac4639f2a Mon Sep 17 00:00:00 2001 From: marksteinbrick-oai Date: Tue, 14 Apr 2026 08:55:12 -0700 Subject: [PATCH 10/16] [codex-analytics] add session source to client metadata (#17374) ## Summary Adds `thread_source` field to the existing Codex turn metadata sent to Responses API - Sends `thread_source: "user"` for user-initiated sessions: CLI, VS Code, and Exec - Sends `thread_source: "subagent"` for subagent sessions - Omits `thread_source` for MCP, custom, and unknown session sources - Uses the existing turn metadata transport: - HTTP requests send through the `x-codex-turn-metadata` header - WebSocket `response.create` requests send through `client_metadata["x-codex-turn-metadata"]` ## Testing - `cargo test -p codex-protocol session_source_thread_source_name_classifies_user_and_subagent_sources` - `cargo test -p codex-core turn_metadata_state` - `cargo test -p codex-core --test responses_headers responses_stream_includes_turn_metadata_header_for_git_workspace_e2e -- --nocapture` --- codex-rs/analytics/src/events.rs | 9 ------ codex-rs/analytics/src/reducer.rs | 3 +- codex-rs/core/src/codex.rs | 2 ++ codex-rs/core/src/turn_metadata.rs | 9 ++++++ codex-rs/core/src/turn_metadata_tests.rs | 32 +++++++++++++++++++ codex-rs/core/tests/responses_headers.rs | 18 +++++++++++ .../core/tests/suite/client_websockets.rs | 7 ++-- codex-rs/protocol/src/protocol.rs | 27 ++++++++++++++++ 8 files changed, 94 insertions(+), 13 deletions(-) diff --git a/codex-rs/analytics/src/events.rs b/codex-rs/analytics/src/events.rs index 87f5a4fbed..eb312da140 100644 --- a/codex-rs/analytics/src/events.rs +++ b/codex-rs/analytics/src/events.rs @@ -15,7 +15,6 @@ use codex_plugin::PluginTelemetryMetadata; use codex_protocol::approvals::NetworkApprovalProtocol; use codex_protocol::models::PermissionProfile; use codex_protocol::models::SandboxPermissions; -use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::SubAgentSource; use serde::Serialize; @@ -530,14 +529,6 @@ pub(crate) fn codex_plugin_used_metadata( } } -pub(crate) fn thread_source_name(thread_source: &SessionSource) -> Option<&'static str> { - match thread_source { - SessionSource::Cli | SessionSource::VSCode | SessionSource::Exec => Some("user"), - SessionSource::SubAgent(_) => Some("subagent"), - SessionSource::Mcp | SessionSource::Custom(_) | SessionSource::Unknown => None, - } -} - pub(crate) fn current_runtime_metadata() -> CodexRuntimeMetadata { let os_info = os_info::get(); CodexRuntimeMetadata { diff --git a/codex-rs/analytics/src/reducer.rs b/codex-rs/analytics/src/reducer.rs index 772a091b15..a5e5165bd2 100644 --- a/codex-rs/analytics/src/reducer.rs +++ b/codex-rs/analytics/src/reducer.rs @@ -26,7 +26,6 @@ use crate::events::plugin_state_event_type; use crate::events::subagent_parent_thread_id; use crate::events::subagent_source_name; use crate::events::subagent_thread_started_event_request; -use crate::events::thread_source_name; use crate::facts::AnalyticsFact; use crate::facts::AnalyticsJsonRpcError; use crate::facts::AppMentionedInput; @@ -107,7 +106,7 @@ impl ThreadMetadataState { | SessionSource::Unknown => (None, None), }; Self { - thread_source: thread_source_name(session_source), + thread_source: session_source.thread_source_name(), initialization_mode, subagent_source, parent_thread_id, diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 845c5c0859..21725cd334 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -1570,6 +1570,7 @@ impl Session { let per_turn_config = Arc::new(per_turn_config); let turn_metadata_state = Arc::new(TurnMetadataState::new( conversation_id.to_string(), + &session_source, sub_id.clone(), cwd.to_path_buf(), session_configuration.sandbox_policy.get(), @@ -5983,6 +5984,7 @@ async fn spawn_review_thread( let review_turn_id = sub_id.to_string(); let turn_metadata_state = Arc::new(TurnMetadataState::new( sess.conversation_id.to_string(), + &session_source, review_turn_id.clone(), parent_turn_context.cwd.to_path_buf(), parent_turn_context.sandbox_policy.get(), diff --git a/codex-rs/core/src/turn_metadata.rs b/codex-rs/core/src/turn_metadata.rs index e4385096a1..9bdffa45d2 100644 --- a/codex-rs/core/src/turn_metadata.rs +++ b/codex-rs/core/src/turn_metadata.rs @@ -17,6 +17,7 @@ use codex_git_utils::get_has_changes; use codex_git_utils::get_head_commit_hash; use codex_protocol::config_types::WindowsSandboxLevel; use codex_protocol::protocol::SandboxPolicy; +use codex_protocol::protocol::SessionSource; #[derive(Clone, Debug, Default)] struct WorkspaceGitMetadata { @@ -58,6 +59,8 @@ pub(crate) struct TurnMetadataBag { #[serde(default, skip_serializing_if = "Option::is_none")] session_id: Option, #[serde(default, skip_serializing_if = "Option::is_none")] + thread_source: Option<&'static str>, + #[serde(default, skip_serializing_if = "Option::is_none")] turn_id: Option, #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] workspaces: BTreeMap, @@ -87,6 +90,7 @@ fn merge_responsesapi_client_metadata( fn build_turn_metadata_bag( session_id: Option, + thread_source: Option<&'static str>, turn_id: Option, sandbox: Option, repo_root: Option, @@ -101,6 +105,7 @@ fn build_turn_metadata_bag( TurnMetadataBag { session_id, + thread_source, turn_id, workspaces, sandbox, @@ -126,6 +131,7 @@ pub async fn build_turn_metadata_header(cwd: &Path, sandbox: Option<&str>) -> Op build_turn_metadata_bag( /*session_id*/ None, + /*thread_source*/ None, /*turn_id*/ None, sandbox.map(ToString::to_string), repo_root, @@ -152,6 +158,7 @@ pub(crate) struct TurnMetadataState { impl TurnMetadataState { pub(crate) fn new( session_id: String, + session_source: &SessionSource, turn_id: String, cwd: PathBuf, sandbox_policy: &SandboxPolicy, @@ -161,6 +168,7 @@ impl TurnMetadataState { let sandbox = Some(sandbox_tag(sandbox_policy, windows_sandbox_level).to_string()); let base_metadata = build_turn_metadata_bag( Some(session_id), + session_source.thread_source_name(), Some(turn_id), sandbox, /*repo_root*/ None, @@ -240,6 +248,7 @@ impl TurnMetadataState { let enriched_metadata = build_turn_metadata_bag( state.base_metadata.session_id.clone(), + state.base_metadata.thread_source, state.base_metadata.turn_id.clone(), state.base_metadata.sandbox.clone(), Some(repo_root), diff --git a/codex-rs/core/src/turn_metadata_tests.rs b/codex-rs/core/src/turn_metadata_tests.rs index dfd9322a98..71fd296258 100644 --- a/codex-rs/core/src/turn_metadata_tests.rs +++ b/codex-rs/core/src/turn_metadata_tests.rs @@ -1,5 +1,7 @@ use super::*; +use codex_protocol::protocol::SessionSource; +use codex_protocol::protocol::SubAgentSource; use serde_json::Value; use std::collections::HashMap; use tempfile::TempDir; @@ -69,6 +71,7 @@ fn turn_metadata_state_uses_platform_sandbox_tag() { let state = TurnMetadataState::new( "session-a".to_string(), + &SessionSource::Exec, "turn-a".to_string(), cwd, &sandbox_policy, @@ -79,10 +82,36 @@ fn turn_metadata_state_uses_platform_sandbox_tag() { let json: Value = serde_json::from_str(&header).expect("json"); let sandbox_name = json.get("sandbox").and_then(Value::as_str); let session_id = json.get("session_id").and_then(Value::as_str); + let thread_source = json.get("thread_source").and_then(Value::as_str); let expected_sandbox = sandbox_tag(&sandbox_policy, WindowsSandboxLevel::Disabled); assert_eq!(sandbox_name, Some(expected_sandbox)); assert_eq!(session_id, Some("session-a")); + assert_eq!(thread_source, Some("user")); + assert!(json.get("session_source").is_none()); +} + +#[test] +fn turn_metadata_state_classifies_subagent_thread_source() { + let temp_dir = TempDir::new().expect("temp dir"); + let cwd = temp_dir.path().to_path_buf(); + let sandbox_policy = SandboxPolicy::new_read_only_policy(); + let session_source = SessionSource::SubAgent(SubAgentSource::Review); + + let state = TurnMetadataState::new( + "session-a".to_string(), + &session_source, + "turn-a".to_string(), + cwd, + &sandbox_policy, + WindowsSandboxLevel::Disabled, + ); + + let header = state.current_header_value().expect("header"); + let json: Value = serde_json::from_str(&header).expect("json"); + + assert_eq!(json["thread_source"].as_str(), Some("subagent")); + assert!(json.get("session_source").is_none()); } #[test] @@ -93,6 +122,7 @@ fn turn_metadata_state_merges_client_metadata_without_replacing_reserved_fields( let state = TurnMetadataState::new( "session-a".to_string(), + &SessionSource::Exec, "turn-a".to_string(), cwd, &sandbox_policy, @@ -101,6 +131,7 @@ fn turn_metadata_state_merges_client_metadata_without_replacing_reserved_fields( state.set_responsesapi_client_metadata(HashMap::from([ ("fiber_run_id".to_string(), "fiber-123".to_string()), ("session_id".to_string(), "client-supplied".to_string()), + ("thread_source".to_string(), "client-supplied".to_string()), ])); let header = state.current_header_value().expect("header"); @@ -108,5 +139,6 @@ fn turn_metadata_state_merges_client_metadata_without_replacing_reserved_fields( assert_eq!(json["fiber_run_id"].as_str(), Some("fiber-123")); assert_eq!(json["session_id"].as_str(), Some("session-a")); + assert_eq!(json["thread_source"].as_str(), Some("user")); assert_eq!(json["turn_id"].as_str(), Some("turn-a")); } diff --git a/codex-rs/core/tests/responses_headers.rs b/codex-rs/core/tests/responses_headers.rs index 03870beab2..8498956517 100644 --- a/codex-rs/core/tests/responses_headers.rs +++ b/codex-rs/core/tests/responses_headers.rs @@ -437,6 +437,12 @@ async fn responses_stream_includes_turn_metadata_header_for_git_workspace_e2e() .and_then(serde_json::Value::as_str), Some("none") ); + assert_eq!( + initial_parsed + .get("thread_source") + .and_then(serde_json::Value::as_str), + Some("user") + ); let git_config_global = cwd.join("empty-git-config"); std::fs::write(&git_config_global, "").expect("write empty git config"); @@ -528,6 +534,18 @@ async fn responses_stream_includes_turn_metadata_header_for_git_workspace_e2e() .get("turn_id") .and_then(serde_json::Value::as_str) .expect("second turn_id should be present"); + assert_eq!( + first_parsed + .get("thread_source") + .and_then(serde_json::Value::as_str), + Some("user") + ); + assert_eq!( + second_parsed + .get("thread_source") + .and_then(serde_json::Value::as_str), + Some("user") + ); assert_eq!( first_turn_id, second_turn_id, "requests should share turn_id" diff --git a/codex-rs/core/tests/suite/client_websockets.rs b/codex-rs/core/tests/suite/client_websockets.rs index 424f6764e6..eaa8a9610d 100755 --- a/codex-rs/core/tests/suite/client_websockets.rs +++ b/codex-rs/core/tests/suite/client_websockets.rs @@ -1209,8 +1209,9 @@ async fn responses_websocket_forwards_turn_metadata_on_initial_and_incremental_c let harness = websocket_harness(&server).await; let mut client_session = harness.client.new_session(); - let first_turn_metadata = r#"{"turn_id":"turn-123","sandbox":"workspace-write"}"#; - let enriched_turn_metadata = r#"{"turn_id":"turn-123","sandbox":"workspace-write","workspaces":[{"root_path":"/tmp/repo","latest_git_commit_hash":"abc123","associated_remote_urls":["git@github.com:openai/codex.git"],"has_changes":true}]}"#; + let first_turn_metadata = + r#"{"turn_id":"turn-123","thread_source":"user","sandbox":"workspace-write"}"#; + let enriched_turn_metadata = r#"{"turn_id":"turn-123","thread_source":"user","sandbox":"workspace-write","workspaces":[{"root_path":"/tmp/repo","latest_git_commit_hash":"abc123","associated_remote_urls":["git@github.com:openai/codex.git"],"has_changes":true}]}"#; let prompt_one = prompt_with_input(vec![message_item("hello")]); let prompt_two = prompt_with_input(vec![ message_item("hello"), @@ -1259,6 +1260,8 @@ async fn responses_websocket_forwards_turn_metadata_on_initial_and_incremental_c assert_eq!(first_metadata["turn_id"].as_str(), Some("turn-123")); assert_eq!(second_metadata["turn_id"].as_str(), Some("turn-123")); + assert_eq!(first_metadata["thread_source"].as_str(), Some("user")); + assert_eq!(second_metadata["thread_source"].as_str(), Some("user")); assert_eq!( second_metadata["workspaces"][0]["has_changes"].as_bool(), Some(true) diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index f56e54fd21..2f9d7021a1 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -2633,6 +2633,15 @@ impl SessionSource { }) } + /// Low cardinality thread source label for analytics. + pub fn thread_source_name(&self) -> Option<&'static str> { + match self { + SessionSource::Cli | SessionSource::VSCode | SessionSource::Exec => Some("user"), + SessionSource::SubAgent(_) => Some("subagent"), + SessionSource::Mcp | SessionSource::Custom(_) | SessionSource::Unknown => None, + } + } + pub fn get_nickname(&self) -> Option { match self { SessionSource::SubAgent(SubAgentSource::ThreadSpawn { agent_nickname, .. }) => { @@ -3882,6 +3891,24 @@ mod tests { ); } + #[test] + fn session_source_thread_source_name_classifies_user_and_subagent_sources() { + for (source, expected) in [ + (SessionSource::Cli, Some("user")), + (SessionSource::VSCode, Some("user")), + (SessionSource::Exec, Some("user")), + ( + SessionSource::SubAgent(SubAgentSource::Review), + Some("subagent"), + ), + (SessionSource::Mcp, None), + (SessionSource::Custom("atlas".to_string()), None), + (SessionSource::Unknown, None), + ] { + assert_eq!(source.thread_source_name(), expected); + } + } + #[test] fn session_source_restriction_product_defaults_non_subagent_sources_to_codex() { assert_eq!( From 4f2fc3e3fa8d72e2eafc133fc3c9c4281f4dad81 Mon Sep 17 00:00:00 2001 From: David de Regt Date: Tue, 14 Apr 2026 11:55:34 -0400 Subject: [PATCH 11/16] Moving updated-at timestamps to unique millisecond times (#17489) To allow the ability to have guaranteed-unique cursors, we make two important updates: * Add new updated_at_ms and created_at_ms columns that are in millisecond precision * Guarantee uniqueness -- if multiple items are inserted at the same millisecond, bump the new one by one millisecond until it becomes unique This lets us use single-number cursors for forwards and backwards paging through resultsets and guarantee that the cursor is a fixed point to do (timestamp > cursor) and get new items only. This updated implementation is backwards-compatible since multiple appservers can be running and won't handle the previous method well. --- .../app-server/src/codex_message_processor.rs | 20 +- .../tests/suite/conversation_summary.rs | 3 +- codex-rs/rollout/src/list.rs | 15 +- codex-rs/rollout/src/metadata.rs | 4 +- codex-rs/rollout/src/recorder.rs | 2 +- codex-rs/rollout/src/state_db.rs | 4 +- .../0025_thread_timestamps_millis.sql | 112 ++++++ codex-rs/state/src/model/mod.rs | 2 + codex-rs/state/src/model/thread_metadata.rs | 30 +- codex-rs/state/src/paths.rs | 3 +- codex-rs/state/src/runtime.rs | 10 + codex-rs/state/src/runtime/memories.rs | 77 ++-- codex-rs/state/src/runtime/threads.rs | 355 ++++++++++++------ 13 files changed, 470 insertions(+), 167 deletions(-) create mode 100644 codex-rs/state/migrations/0025_thread_timestamps_millis.sql diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index c027501db0..b381317d70 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -9703,7 +9703,7 @@ async fn read_updated_at(path: &Path, created_at: Option<&str>) -> Option = modified.into(); - updated_at.to_rfc3339_opts(SecondsFormat::Secs, true) + updated_at.to_rfc3339_opts(SecondsFormat::Millis, true) }); updated_at.or_else(|| created_at.map(str::to_string)) } @@ -9954,6 +9954,22 @@ mod tests { Ok(metadata) } + #[test] + fn summary_from_thread_metadata_formats_protocol_timestamps_as_seconds() -> Result<()> { + let mut metadata = + test_thread_metadata(/*model*/ None, /*reasoning_effort*/ None)?; + metadata.created_at = + DateTime::parse_from_rfc3339("2025-09-05T16:53:11.123Z")?.with_timezone(&Utc); + metadata.updated_at = + DateTime::parse_from_rfc3339("2025-09-05T16:53:12.456Z")?.with_timezone(&Utc); + + let summary = summary_from_thread_metadata(&metadata); + + assert_eq!(summary.timestamp, Some("2025-09-05T16:53:11Z".to_string())); + assert_eq!(summary.updated_at, Some("2025-09-05T16:53:12Z".to_string())); + Ok(()) + } + #[test] fn merge_persisted_resume_metadata_prefers_persisted_model_and_reasoning_effort() -> Result<()> { @@ -10213,7 +10229,7 @@ mod tests { let expected = ConversationSummary { conversation_id, timestamp: Some(timestamp.clone()), - updated_at: Some("2025-09-05T16:53:11Z".to_string()), + updated_at: Some(timestamp), path: path.clone(), preview: String::new(), model_provider: "fallback".to_string(), diff --git a/codex-rs/app-server/tests/suite/conversation_summary.rs b/codex-rs/app-server/tests/suite/conversation_summary.rs index 9e292d602f..4690a44ca3 100644 --- a/codex-rs/app-server/tests/suite/conversation_summary.rs +++ b/codex-rs/app-server/tests/suite/conversation_summary.rs @@ -18,6 +18,7 @@ use tokio::time::timeout; const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); const FILENAME_TS: &str = "2025-01-02T12-00-00"; const META_RFC3339: &str = "2025-01-02T12:00:00Z"; +const UPDATED_AT_RFC3339: &str = "2025-01-02T12:00:00.000Z"; const PREVIEW: &str = "Summarize this conversation"; const MODEL_PROVIDER: &str = "openai"; @@ -27,7 +28,7 @@ fn expected_summary(conversation_id: ThreadId, path: PathBuf) -> ConversationSum path, preview: PREVIEW.to_string(), timestamp: Some(META_RFC3339.to_string()), - updated_at: Some(META_RFC3339.to_string()), + updated_at: Some(UPDATED_AT_RFC3339.to_string()), model_provider: MODEL_PROVIDER.to_string(), cwd: PathBuf::from("/"), cli_version: "0.0.0".to_string(), diff --git a/codex-rs/rollout/src/list.rs b/codex-rs/rollout/src/list.rs index e7d3dae5de..afa4103307 100644 --- a/codex-rs/rollout/src/list.rs +++ b/codex-rs/rollout/src/list.rs @@ -71,7 +71,6 @@ pub struct ThreadItem { /// created_at comes from the filename timestamp with second precision. pub created_at: Option, /// RFC3339 timestamp string for the most recent update (from file mtime). - /// updated_at is truncated to second precision to match created_at. pub updated_at: Option, } @@ -292,7 +291,10 @@ impl<'de> serde::Deserialize<'de> for Cursor { impl From for Cursor { fn from(anchor: codex_state::Anchor) -> Self { - let ts = OffsetDateTime::from_unix_timestamp(anchor.ts.timestamp()) + let ts = anchor + .ts + .timestamp_nanos_opt() + .and_then(|nanos| OffsetDateTime::from_unix_timestamp_nanos(nanos as i128).ok()) .unwrap_or(OffsetDateTime::UNIX_EPOCH); Self::new(ts, anchor.id) } @@ -1156,17 +1158,16 @@ async fn file_modified_time(path: &Path) -> io::Result> { return Ok(None); }; let dt = OffsetDateTime::from(modified); - // Truncate to seconds so ordering and cursor comparisons align with the - // cursor timestamp format (which exposes seconds), keeping pagination stable. - Ok(truncate_to_seconds(dt)) + Ok(truncate_to_millis(dt)) } fn format_rfc3339(dt: OffsetDateTime) -> Option { dt.format(&Rfc3339).ok() } -fn truncate_to_seconds(dt: OffsetDateTime) -> Option { - dt.replace_nanosecond(0).ok() +fn truncate_to_millis(dt: OffsetDateTime) -> Option { + let millis_nanos = (dt.nanosecond() / 1_000_000) * 1_000_000; + dt.replace_nanosecond(millis_nanos).ok() } async fn find_thread_path_by_id_str_in_subdir( diff --git a/codex-rs/rollout/src/metadata.rs b/codex-rs/rollout/src/metadata.rs index e8a95839d7..58d55a887d 100644 --- a/codex-rs/rollout/src/metadata.rs +++ b/codex-rs/rollout/src/metadata.rs @@ -371,7 +371,7 @@ fn backfill_watermark_for_path(codex_home: &Path, path: &Path) -> String { async fn file_modified_time_utc(path: &Path) -> Option> { let modified = tokio::fs::metadata(path).await.ok()?.modified().ok()?; let updated_at: DateTime = modified.into(); - updated_at.with_nanosecond(0) + Some(updated_at) } fn parse_timestamp_to_utc(ts: &str) -> Option> { @@ -381,7 +381,7 @@ fn parse_timestamp_to_utc(ts: &str) -> Option> { return dt.with_nanosecond(0); } if let Ok(dt) = DateTime::parse_from_rfc3339(ts) { - return dt.with_timezone(&Utc).with_nanosecond(0); + return Some(dt.with_timezone(&Utc)); } None } diff --git a/codex-rs/rollout/src/recorder.rs b/codex-rs/rollout/src/recorder.rs index 01ea10cb95..4f88ff1a21 100644 --- a/codex-rs/rollout/src/recorder.rs +++ b/codex-rs/rollout/src/recorder.rs @@ -1252,7 +1252,7 @@ impl From for ThreadsPage { model_provider: Some(item.model_provider), cli_version: Some(item.cli_version), created_at: Some(item.created_at.to_rfc3339_opts(SecondsFormat::Secs, true)), - updated_at: Some(item.updated_at.to_rfc3339_opts(SecondsFormat::Secs, true)), + updated_at: Some(item.updated_at.to_rfc3339_opts(SecondsFormat::Millis, true)), }) .collect(); Self { diff --git a/codex-rs/rollout/src/state_db.rs b/codex-rs/rollout/src/state_db.rs index 6367a27ca9..7f609bf267 100644 --- a/codex-rs/rollout/src/state_db.rs +++ b/codex-rs/rollout/src/state_db.rs @@ -5,7 +5,6 @@ use crate::list::ThreadSortKey; use crate::metadata; use chrono::DateTime; use chrono::NaiveDateTime; -use chrono::Timelike; use chrono::Utc; use codex_protocol::ThreadId; use codex_protocol::dynamic_tools::DynamicToolSpec; @@ -131,8 +130,7 @@ fn cursor_to_anchor(cursor: Option<&Cursor>) -> Option { dt.with_timezone(&Utc) } else { return None; - } - .with_nanosecond(0)?; + }; Some(codex_state::Anchor { ts, id }) } diff --git a/codex-rs/state/migrations/0025_thread_timestamps_millis.sql b/codex-rs/state/migrations/0025_thread_timestamps_millis.sql new file mode 100644 index 0000000000..d29847438d --- /dev/null +++ b/codex-rs/state/migrations/0025_thread_timestamps_millis.sql @@ -0,0 +1,112 @@ +ALTER TABLE threads ADD COLUMN created_at_ms INTEGER; +ALTER TABLE threads ADD COLUMN updated_at_ms INTEGER; + +CREATE TEMP TABLE thread_timestamp_migration AS +SELECT + id, + CASE + WHEN created_at < 1577836800000 THEN created_at * 1000 + ELSE created_at + END AS created_at_base_ms, + CASE + WHEN updated_at < 1577836800000 THEN updated_at * 1000 + ELSE updated_at + END AS updated_at_base_ms +FROM threads; + +WITH RECURSIVE +ordered_created AS ( + SELECT + id, + created_at_base_ms, + ROW_NUMBER() OVER (ORDER BY created_at_base_ms, id) AS row_number + FROM thread_timestamp_migration +), +assigned_created(row_number, id, created_at_ms) AS ( + SELECT row_number, id, created_at_base_ms + FROM ordered_created + WHERE row_number = 1 + UNION ALL + SELECT + ordered_created.row_number, + ordered_created.id, + MAX(ordered_created.created_at_base_ms, assigned_created.created_at_ms + 1) + FROM ordered_created + JOIN assigned_created ON ordered_created.row_number = assigned_created.row_number + 1 +) +UPDATE threads +SET created_at_ms = ( + SELECT created_at_ms + FROM assigned_created + WHERE assigned_created.id = threads.id +); + +WITH RECURSIVE +ordered_updated AS ( + SELECT + id, + updated_at_base_ms, + ROW_NUMBER() OVER (ORDER BY updated_at_base_ms, id) AS row_number + FROM thread_timestamp_migration +), +assigned_updated(row_number, id, updated_at_ms) AS ( + SELECT row_number, id, updated_at_base_ms + FROM ordered_updated + WHERE row_number = 1 + UNION ALL + SELECT + ordered_updated.row_number, + ordered_updated.id, + MAX(ordered_updated.updated_at_base_ms, assigned_updated.updated_at_ms + 1) + FROM ordered_updated + JOIN assigned_updated ON ordered_updated.row_number = assigned_updated.row_number + 1 +) +UPDATE threads +SET updated_at_ms = ( + SELECT updated_at_ms + FROM assigned_updated + WHERE assigned_updated.id = threads.id +); + +DROP TABLE thread_timestamp_migration; + +CREATE TRIGGER threads_created_at_ms_after_insert +AFTER INSERT ON threads +WHEN NEW.created_at_ms IS NULL +BEGIN + UPDATE threads + SET created_at_ms = NEW.created_at * 1000 + WHERE id = NEW.id; +END; + +CREATE TRIGGER threads_updated_at_ms_after_insert +AFTER INSERT ON threads +WHEN NEW.updated_at_ms IS NULL +BEGIN + UPDATE threads + SET updated_at_ms = NEW.updated_at * 1000 + WHERE id = NEW.id; +END; + +CREATE TRIGGER threads_created_at_ms_after_update +AFTER UPDATE OF created_at ON threads +WHEN NEW.created_at != OLD.created_at + AND NEW.created_at_ms IS OLD.created_at_ms +BEGIN + UPDATE threads + SET created_at_ms = NEW.created_at * 1000 + WHERE id = NEW.id; +END; + +CREATE TRIGGER threads_updated_at_ms_after_update +AFTER UPDATE OF updated_at ON threads +WHEN NEW.updated_at != OLD.updated_at + AND NEW.updated_at_ms IS OLD.updated_at_ms +BEGIN + UPDATE threads + SET updated_at_ms = NEW.updated_at * 1000 + WHERE id = NEW.id; +END; + +CREATE INDEX idx_threads_created_at_ms ON threads(created_at_ms DESC, id DESC); +CREATE INDEX idx_threads_updated_at_ms ON threads(updated_at_ms DESC, id DESC); diff --git a/codex-rs/state/src/model/mod.rs b/codex-rs/state/src/model/mod.rs index 39f0e800fc..af4b30a8fe 100644 --- a/codex-rs/state/src/model/mod.rs +++ b/codex-rs/state/src/model/mod.rs @@ -39,4 +39,6 @@ pub(crate) use memories::Stage1OutputRow; pub(crate) use memories::stage1_output_ref_from_parts; pub(crate) use thread_metadata::ThreadRow; pub(crate) use thread_metadata::anchor_from_item; +pub(crate) use thread_metadata::datetime_to_epoch_millis; pub(crate) use thread_metadata::datetime_to_epoch_seconds; +pub(crate) use thread_metadata::epoch_millis_to_datetime; diff --git a/codex-rs/state/src/model/thread_metadata.rs b/codex-rs/state/src/model/thread_metadata.rs index d14e3b4798..db4cba27fe 100644 --- a/codex-rs/state/src/model/thread_metadata.rs +++ b/codex-rs/state/src/model/thread_metadata.rs @@ -1,6 +1,5 @@ use anyhow::Result; use chrono::DateTime; -use chrono::Timelike; use chrono::Utc; use codex_protocol::ThreadId; use codex_protocol::openai_models::ReasoningEffort; @@ -300,7 +299,7 @@ impl ThreadMetadata { } fn canonicalize_datetime(dt: DateTime) -> DateTime { - dt.with_nanosecond(0).unwrap_or(dt) + epoch_millis_to_datetime(datetime_to_epoch_millis(dt)).unwrap_or(dt) } #[derive(Debug)] @@ -389,8 +388,8 @@ impl TryFrom for ThreadMetadata { Ok(Self { id: ThreadId::try_from(id)?, rollout_path: PathBuf::from(rollout_path), - created_at: epoch_seconds_to_datetime(created_at)?, - updated_at: epoch_seconds_to_datetime(updated_at)?, + created_at: epoch_millis_to_datetime(created_at)?, + updated_at: epoch_millis_to_datetime(updated_at)?, source, agent_nickname, agent_role, @@ -423,13 +422,30 @@ pub(crate) fn anchor_from_item(item: &ThreadMetadata, sort_key: SortKey) -> Opti Some(Anchor { ts, id }) } +pub(crate) fn datetime_to_epoch_millis(dt: DateTime) -> i64 { + dt.timestamp_millis() +} + pub(crate) fn datetime_to_epoch_seconds(dt: DateTime) -> i64 { dt.timestamp() } -pub(crate) fn epoch_seconds_to_datetime(secs: i64) -> Result> { - DateTime::::from_timestamp(secs, 0) - .ok_or_else(|| anyhow::anyhow!("invalid unix timestamp: {secs}")) +pub(crate) fn epoch_millis_to_datetime(value: i64) -> Result> { + // Values older than 2020 if interpreted as milliseconds are legacy second-precision rows. + // Convert them in memory so old state DBs keep ordering correctly after new writes use ms. + const MIN_EPOCH_MILLIS: i64 = 1_577_836_800_000; + let millis = if value < MIN_EPOCH_MILLIS { + value.saturating_mul(1000) + } else { + value + }; + DateTime::::from_timestamp_millis(millis) + .ok_or_else(|| anyhow::anyhow!("invalid unix timestamp millis: {value}")) +} + +pub(crate) fn epoch_seconds_to_datetime(value: i64) -> Result> { + DateTime::::from_timestamp(value, 0) + .ok_or_else(|| anyhow::anyhow!("invalid unix timestamp seconds: {value}")) } /// Statistics about a backfill operation. diff --git a/codex-rs/state/src/paths.rs b/codex-rs/state/src/paths.rs index 8123743821..65458cb9e9 100644 --- a/codex-rs/state/src/paths.rs +++ b/codex-rs/state/src/paths.rs @@ -1,10 +1,9 @@ use chrono::DateTime; -use chrono::Timelike; use chrono::Utc; use std::path::Path; pub(crate) async fn file_modified_time_utc(path: &Path) -> Option> { let modified = tokio::fs::metadata(path).await.ok()?.modified().ok()?; let updated_at: DateTime = modified.into(); - Some(updated_at.with_nanosecond(0).unwrap_or(updated_at)) + Some(updated_at) } diff --git a/codex-rs/state/src/runtime.rs b/codex-rs/state/src/runtime.rs index f71b6adf05..610e45e68a 100644 --- a/codex-rs/state/src/runtime.rs +++ b/codex-rs/state/src/runtime.rs @@ -22,7 +22,9 @@ use crate::migrations::runtime_state_migrator; use crate::model::AgentJobRow; use crate::model::ThreadRow; use crate::model::anchor_from_item; +use crate::model::datetime_to_epoch_millis; use crate::model::datetime_to_epoch_seconds; +use crate::model::epoch_millis_to_datetime; use crate::paths::file_modified_time_utc; use chrono::DateTime; use chrono::Utc; @@ -47,6 +49,7 @@ use std::collections::BTreeSet; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; +use std::sync::atomic::AtomicI64; use std::time::Duration; use tracing::warn; @@ -76,6 +79,7 @@ pub struct StateRuntime { default_provider: String, pool: Arc, logs_pool: Arc, + thread_updated_at_millis: Arc, } impl StateRuntime { @@ -120,11 +124,17 @@ impl StateRuntime { return Err(err); } }; + let thread_updated_at_millis: Option = + sqlx::query_scalar("SELECT MAX(threads.updated_at_ms) FROM threads") + .fetch_one(pool.as_ref()) + .await?; + let thread_updated_at_millis = thread_updated_at_millis.unwrap_or(0); let runtime = Arc::new(Self { pool, logs_pool, codex_home, default_provider, + thread_updated_at_millis: Arc::new(AtomicI64::new(thread_updated_at_millis)), }); if let Err(err) = runtime.run_logs_startup_maintenance().await { warn!( diff --git a/codex-rs/state/src/runtime/memories.rs b/codex-rs/state/src/runtime/memories.rs index 1c1060d0bc..50f2f0cd8c 100644 --- a/codex-rs/state/src/runtime/memories.rs +++ b/codex-rs/state/src/runtime/memories.rs @@ -126,12 +126,9 @@ WHERE thread_id = ? /// (`push_thread_filters`) /// - excludes threads with `memory_mode != 'enabled'` /// - excludes the current thread id - /// - keeps only threads in the age window: - /// `updated_at >= now - max_age_days` and `updated_at <= now - min_rollout_idle_hours` - /// - keeps only threads whose memory is stale: - /// `COALESCE(stage1_outputs.source_updated_at, -1) < threads.updated_at` and - /// `COALESCE(jobs.last_success_watermark, -1) < threads.updated_at` - /// - orders by `updated_at DESC, id DESC` and applies `scan_limit` + /// - keeps only threads whose millisecond `updated_at` is in the age window + /// - keeps only threads whose memory is stale compared to millisecond `updated_at` + /// - orders by `updated_at_ms DESC, id DESC` and applies `scan_limit` /// /// For each selected thread, this function calls [`Self::try_claim_stage1_job`] /// with `source_updated_at = thread.updated_at.timestamp()` and returns up to @@ -155,34 +152,35 @@ WHERE thread_id = ? let worker_id = current_thread_id; let current_thread_id = worker_id.to_string(); - let max_age_cutoff = (Utc::now() - Duration::days(max_age_days.max(0))).timestamp(); - let idle_cutoff = (Utc::now() - Duration::hours(min_rollout_idle_hours.max(0))).timestamp(); + let max_age_cutoff = (Utc::now() - Duration::days(max_age_days.max(0))).timestamp_millis(); + let idle_cutoff = + (Utc::now() - Duration::hours(min_rollout_idle_hours.max(0))).timestamp_millis(); let mut builder = QueryBuilder::::new( r#" SELECT - id, - rollout_path, - created_at, - updated_at, - source, - agent_path, - agent_nickname, - agent_role, - model_provider, - model, - reasoning_effort, - cwd, - cli_version, - title, - sandbox_policy, - approval_mode, - tokens_used, - first_user_message, - archived_at, - git_sha, - git_branch, - git_origin_url + threads.id, + threads.rollout_path, + threads.created_at_ms AS created_at, + threads.updated_at_ms AS updated_at, + threads.source, + threads.agent_path, + threads.agent_nickname, + threads.agent_role, + threads.model_provider, + threads.model, + threads.reasoning_effort, + threads.cwd, + threads.cli_version, + threads.title, + threads.sandbox_policy, + threads.approval_mode, + threads.tokens_used, + threads.first_user_message, + threads.archived_at, + threads.git_sha, + threads.git_branch, + threads.git_origin_url FROM threads LEFT JOIN stage1_outputs ON stage1_outputs.thread_id = threads.id @@ -207,14 +205,23 @@ LEFT JOIN jobs ); builder.push(" AND threads.memory_mode = 'enabled'"); builder - .push(" AND id != ") + .push(" AND threads.id != ") .push_bind(current_thread_id.as_str()); builder - .push(" AND updated_at >= ") + .push(" AND ") + .push("threads.updated_at_ms") + .push(" >= ") .push_bind(max_age_cutoff); - builder.push(" AND updated_at <= ").push_bind(idle_cutoff); - builder.push(" AND COALESCE(stage1_outputs.source_updated_at, -1) < updated_at"); - builder.push(" AND COALESCE(jobs.last_success_watermark, -1) < updated_at"); + builder + .push(" AND ") + .push("threads.updated_at_ms") + .push(" <= ") + .push_bind(idle_cutoff); + let updated_at = "threads.updated_at_ms"; + builder.push(" AND ((COALESCE(stage1_outputs.source_updated_at, -1) + 1) * 1000) <= "); + builder.push(updated_at); + builder.push(" AND ((COALESCE(jobs.last_success_watermark, -1) + 1) * 1000) <= "); + builder.push(updated_at); push_thread_order_and_limit(&mut builder, SortKey::UpdatedAt, scan_limit); let items = builder diff --git a/codex-rs/state/src/runtime/threads.rs b/codex-rs/state/src/runtime/threads.rs index e56ca3f386..efd5b9c192 100644 --- a/codex-rs/state/src/runtime/threads.rs +++ b/codex-rs/state/src/runtime/threads.rs @@ -1,35 +1,36 @@ use super::*; use codex_protocol::protocol::SessionSource; +use std::sync::atomic::Ordering; impl StateRuntime { pub async fn get_thread(&self, id: ThreadId) -> anyhow::Result> { let row = sqlx::query( r#" SELECT - id, - rollout_path, - created_at, - updated_at, - source, - agent_nickname, - agent_role, - agent_path, - model_provider, - model, - reasoning_effort, - cwd, - cli_version, - title, - sandbox_policy, - approval_mode, - tokens_used, - first_user_message, - archived_at, - git_sha, - git_branch, - git_origin_url + threads.id, + threads.rollout_path, + threads.created_at_ms AS created_at, + threads.updated_at_ms AS updated_at, + threads.source, + threads.agent_nickname, + threads.agent_role, + threads.agent_path, + threads.model_provider, + threads.model, + threads.reasoning_effort, + threads.cwd, + threads.cli_version, + threads.title, + threads.sandbox_policy, + threads.approval_mode, + threads.tokens_used, + threads.first_user_message, + threads.archived_at, + threads.git_sha, + threads.git_branch, + threads.git_origin_url FROM threads -WHERE id = ? +WHERE threads.id = ? "#, ) .bind(id.to_string()) @@ -336,34 +337,9 @@ ON CONFLICT(child_thread_id) DO NOTHING archived_only: bool, cwd: Option<&Path>, ) -> anyhow::Result> { - let mut builder = QueryBuilder::::new( - r#" -SELECT - id, - rollout_path, - created_at, - updated_at, - source, - agent_nickname, - agent_role, - agent_path, - model_provider, - model, - reasoning_effort, - cwd, - cli_version, - title, - sandbox_policy, - approval_mode, - tokens_used, - first_user_message, - archived_at, - git_sha, - git_branch, - git_origin_url -FROM threads - "#, - ); + let mut builder = QueryBuilder::::new(""); + push_thread_select_columns(&mut builder); + builder.push(" FROM threads"); push_thread_filters( &mut builder, archived_only, @@ -373,10 +349,10 @@ FROM threads crate::SortKey::UpdatedAt, /*search_term*/ None, ); - builder.push(" AND title = "); + builder.push(" AND threads.title = "); builder.push_bind(title); if let Some(cwd) = cwd { - builder.push(" AND cwd = "); + builder.push(" AND threads.cwd = "); builder.push_bind(cwd.display().to_string()); } push_thread_order_and_limit(&mut builder, crate::SortKey::UpdatedAt, /*limit*/ 1); @@ -400,34 +376,9 @@ FROM threads ) -> anyhow::Result { let limit = page_size.saturating_add(1); - let mut builder = QueryBuilder::::new( - r#" -SELECT - id, - rollout_path, - created_at, - updated_at, - source, - agent_nickname, - agent_role, - agent_path, - model_provider, - model, - reasoning_effort, - cwd, - cli_version, - title, - sandbox_policy, - approval_mode, - tokens_used, - first_user_message, - archived_at, - git_sha, - git_branch, - git_origin_url -FROM threads - "#, - ); + let mut builder = QueryBuilder::::new(""); + push_thread_select_columns(&mut builder); + builder.push(" FROM threads"); push_thread_filters( &mut builder, archived_only, @@ -470,7 +421,7 @@ FROM threads model_providers: Option<&[String]>, archived_only: bool, ) -> anyhow::Result> { - let mut builder = QueryBuilder::::new("SELECT id FROM threads"); + let mut builder = QueryBuilder::::new("SELECT threads.id FROM threads"); push_thread_filters( &mut builder, archived_only, @@ -501,6 +452,7 @@ FROM threads &self, metadata: &crate::ThreadMetadata, ) -> anyhow::Result { + let updated_at = self.allocate_thread_updated_at(metadata.updated_at)?; let result = sqlx::query( r#" INSERT INTO threads ( @@ -508,6 +460,8 @@ INSERT INTO threads ( rollout_path, created_at, updated_at, + created_at_ms, + updated_at_ms, source, agent_nickname, agent_role, @@ -528,14 +482,16 @@ INSERT INTO threads ( git_branch, git_origin_url, memory_mode -) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) +) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO NOTHING "#, ) .bind(metadata.id.to_string()) .bind(metadata.rollout_path.display().to_string()) .bind(datetime_to_epoch_seconds(metadata.created_at)) - .bind(datetime_to_epoch_seconds(metadata.updated_at)) + .bind(datetime_to_epoch_seconds(updated_at)) + .bind(datetime_to_epoch_millis(metadata.created_at)) + .bind(datetime_to_epoch_millis(updated_at)) .bind(metadata.source.as_str()) .bind(metadata.agent_nickname.as_deref()) .bind(metadata.agent_role.as_deref()) @@ -599,14 +555,63 @@ ON CONFLICT(id) DO NOTHING thread_id: ThreadId, updated_at: DateTime, ) -> anyhow::Result { - let result = sqlx::query("UPDATE threads SET updated_at = ? WHERE id = ?") - .bind(datetime_to_epoch_seconds(updated_at)) - .bind(thread_id.to_string()) - .execute(self.pool.as_ref()) - .await?; + let updated_at = self.allocate_thread_updated_at(updated_at)?; + let result = + sqlx::query("UPDATE threads SET updated_at = ?, updated_at_ms = ? WHERE id = ?") + .bind(datetime_to_epoch_seconds(updated_at)) + .bind(datetime_to_epoch_millis(updated_at)) + .bind(thread_id.to_string()) + .execute(self.pool.as_ref()) + .await?; Ok(result.rows_affected() > 0) } + /// Allocate a persisted `updated_at` value for thread-list cursor ordering. + /// + /// We keep a process-local high-water mark so hot rollout writes can get unique, + /// monotonic millisecond timestamps without querying SQLite on every update. Older + /// backfill/repair timestamps are allowed through unchanged so historical ordering + /// remains tied to the rollout file mtimes. + fn allocate_thread_updated_at( + &self, + updated_at: DateTime, + ) -> anyhow::Result> { + let candidate = datetime_to_epoch_millis(updated_at); + let allocated = loop { + let current = self.thread_updated_at_millis.load(Ordering::Relaxed); + + // New wall-clock time: advance the process-local high-water mark and use it as-is. + if candidate > current { + if self + .thread_updated_at_millis + .compare_exchange(current, candidate, Ordering::Relaxed, Ordering::Relaxed) + .is_ok() + { + break candidate; + } + continue; + } + + // Older timestamps come from backfill/repair paths that preserve rollout mtimes. + // Do not drag historical rows forward just because this process has seen newer writes. + if candidate.saturating_add(1000) <= current { + break candidate; + } + + // Same hot one-second bucket as the current high-water mark. Allocate the next + // millisecond so updated_at remains unique and cursor-orderable inside the process. + let bumped = current.saturating_add(1); + if self + .thread_updated_at_millis + .compare_exchange(current, bumped, Ordering::Relaxed, Ordering::Relaxed) + .is_ok() + { + break bumped; + } + }; + epoch_millis_to_datetime(allocated) + } + pub async fn update_thread_git_info( &self, thread_id: ThreadId, @@ -641,6 +646,7 @@ WHERE id = ? metadata: &crate::ThreadMetadata, creation_memory_mode: Option<&str>, ) -> anyhow::Result<()> { + let updated_at = self.allocate_thread_updated_at(metadata.updated_at)?; sqlx::query( r#" INSERT INTO threads ( @@ -648,6 +654,8 @@ INSERT INTO threads ( rollout_path, created_at, updated_at, + created_at_ms, + updated_at_ms, source, agent_nickname, agent_role, @@ -668,11 +676,13 @@ INSERT INTO threads ( git_branch, git_origin_url, memory_mode -) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) +) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET rollout_path = excluded.rollout_path, created_at = excluded.created_at, updated_at = excluded.updated_at, + created_at_ms = excluded.created_at_ms, + updated_at_ms = excluded.updated_at_ms, source = excluded.source, agent_nickname = excluded.agent_nickname, agent_role = excluded.agent_role, @@ -697,7 +707,9 @@ ON CONFLICT(id) DO UPDATE SET .bind(metadata.id.to_string()) .bind(metadata.rollout_path.display().to_string()) .bind(datetime_to_epoch_seconds(metadata.created_at)) - .bind(datetime_to_epoch_seconds(metadata.updated_at)) + .bind(datetime_to_epoch_seconds(updated_at)) + .bind(datetime_to_epoch_millis(metadata.created_at)) + .bind(datetime_to_epoch_millis(updated_at)) .bind(metadata.source.as_str()) .bind(metadata.agent_nickname.as_deref()) .bind(metadata.agent_role.as_deref()) @@ -909,6 +921,36 @@ fn one_thread_id_from_rows( } } +pub(super) fn push_thread_select_columns(builder: &mut QueryBuilder<'_, Sqlite>) { + builder.push( + r#" +SELECT + threads.id, + threads.rollout_path, + threads.created_at_ms AS created_at, + threads.updated_at_ms AS updated_at, + threads.source, + threads.agent_nickname, + threads.agent_role, + threads.agent_path, + threads.model_provider, + threads.model, + threads.reasoning_effort, + threads.cwd, + threads.cli_version, + threads.title, + threads.sandbox_policy, + threads.approval_mode, + threads.tokens_used, + threads.first_user_message, + threads.archived_at, + threads.git_sha, + threads.git_branch, + threads.git_origin_url +"#, + ); +} + pub(super) fn extract_dynamic_tools(items: &[RolloutItem]) -> Option>> { items.iter().find_map(|item| match item { RolloutItem::SessionMeta(meta_line) => Some(meta_line.meta.dynamic_tools.clone()), @@ -952,13 +994,13 @@ pub(super) fn push_thread_filters<'a>( ) { builder.push(" WHERE 1 = 1"); if archived_only { - builder.push(" AND archived = 1"); + builder.push(" AND threads.archived = 1"); } else { - builder.push(" AND archived = 0"); + builder.push(" AND threads.archived = 0"); } - builder.push(" AND first_user_message <> ''"); + builder.push(" AND threads.first_user_message <> ''"); if !allowed_sources.is_empty() { - builder.push(" AND source IN ("); + builder.push(" AND threads.source IN ("); let mut separated = builder.separated(", "); for source in allowed_sources { separated.push_bind(source); @@ -968,7 +1010,7 @@ pub(super) fn push_thread_filters<'a>( if let Some(model_providers) = model_providers && !model_providers.is_empty() { - builder.push(" AND model_provider IN ("); + builder.push(" AND threads.model_provider IN ("); let mut separated = builder.separated(", "); for provider in model_providers { separated.push_bind(provider); @@ -976,15 +1018,15 @@ pub(super) fn push_thread_filters<'a>( separated.push_unseparated(")"); } if let Some(search_term) = search_term { - builder.push(" AND instr(title, "); + builder.push(" AND instr(threads.title, "); builder.push_bind(search_term); builder.push(") > 0"); } if let Some(anchor) = anchor { - let anchor_ts = datetime_to_epoch_seconds(anchor.ts); + let anchor_ts = datetime_to_epoch_millis(anchor.ts); let column = match sort_key { - SortKey::CreatedAt => "created_at", - SortKey::UpdatedAt => "updated_at", + SortKey::CreatedAt => "threads.created_at_ms", + SortKey::UpdatedAt => "threads.updated_at_ms", }; builder.push(" AND ("); builder.push(column); @@ -1006,8 +1048,8 @@ pub(super) fn push_thread_order_and_limit( limit: usize, ) { let order_column = match sort_key { - SortKey::CreatedAt => "created_at", - SortKey::UpdatedAt => "updated_at", + SortKey::CreatedAt => "threads.created_at_ms", + SortKey::UpdatedAt => "threads.updated_at_ms", }; builder.push(" ORDER BY "); builder.push(order_column); @@ -1207,12 +1249,13 @@ mod tests { .await .expect("initial upsert should succeed"); - let updated_at = datetime_to_epoch_seconds( + let updated_at = datetime_to_epoch_millis( DateTime::::from_timestamp(1_700_000_100, 0).expect("timestamp"), ); sqlx::query( - "UPDATE threads SET updated_at = ?, tokens_used = ?, first_user_message = ? WHERE id = ?", + "UPDATE threads SET updated_at = ?, updated_at_ms = ?, tokens_used = ?, first_user_message = ? WHERE id = ?", ) + .bind(updated_at / 1000) .bind(updated_at) .bind(123_i64) .bind("newer preview") @@ -1242,7 +1285,7 @@ mod tests { persisted.first_user_message.as_deref(), Some("newer preview") ); - assert_eq!(datetime_to_epoch_seconds(persisted.updated_at), updated_at); + assert_eq!(datetime_to_epoch_millis(persisted.updated_at), updated_at); assert_eq!(persisted.git_sha.as_deref(), Some("abc123")); assert_eq!(persisted.git_branch.as_deref(), Some("feature/branch")); assert_eq!( @@ -1291,8 +1334,8 @@ mod tests { Some("newer preview") ); assert_eq!( - datetime_to_epoch_seconds(persisted.updated_at), - datetime_to_epoch_seconds(existing.updated_at) + datetime_to_epoch_millis(persisted.updated_at), + datetime_to_epoch_millis(existing.updated_at) ); } @@ -1367,6 +1410,104 @@ mod tests { ); } + #[tokio::test] + async fn thread_updated_at_uses_unique_epoch_millis_and_reads_legacy_seconds() { + let codex_home = unique_temp_dir(); + let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string()) + .await + .expect("state db should initialize"); + let first_id = + ThreadId::from_string("00000000-0000-0000-0000-000000000901").expect("valid thread id"); + let second_id = + ThreadId::from_string("00000000-0000-0000-0000-000000000902").expect("valid thread id"); + let older_id = + ThreadId::from_string("00000000-0000-0000-0000-000000000903").expect("valid thread id"); + let updated_at = + DateTime::::from_timestamp_millis(1_700_001_111_123).expect("timestamp millis"); + let mut first = test_thread_metadata(&codex_home, first_id, codex_home.clone()); + first.updated_at = updated_at; + let mut second = test_thread_metadata(&codex_home, second_id, codex_home.clone()); + second.updated_at = updated_at; + + runtime + .upsert_thread(&first) + .await + .expect("first upsert should succeed"); + runtime + .upsert_thread(&second) + .await + .expect("second upsert should succeed"); + + let first = runtime + .get_thread(first_id) + .await + .expect("thread should load") + .expect("thread should exist"); + let second = runtime + .get_thread(second_id) + .await + .expect("thread should load") + .expect("thread should exist"); + assert_eq!( + datetime_to_epoch_millis(first.updated_at), + 1_700_001_111_123 + ); + assert_eq!( + datetime_to_epoch_millis(second.updated_at), + 1_700_001_111_124 + ); + let second_row: (i64, i64, Option, Option) = sqlx::query_as( + "SELECT created_at, updated_at, created_at_ms, updated_at_ms FROM threads WHERE id = ?", + ) + .bind(second_id.to_string()) + .fetch_one(runtime.pool.as_ref()) + .await + .expect("thread timestamp row should load"); + assert_eq!( + second_row, + ( + datetime_to_epoch_seconds(second.created_at), + 1_700_001_111, + Some(datetime_to_epoch_millis(second.created_at)), + Some(1_700_001_111_124) + ) + ); + + let older_updated_at = + DateTime::::from_timestamp_millis(1_700_001_100_123).expect("timestamp millis"); + let mut older = test_thread_metadata(&codex_home, older_id, codex_home.clone()); + older.updated_at = older_updated_at; + runtime + .upsert_thread(&older) + .await + .expect("older upsert should succeed"); + let older = runtime + .get_thread(older_id) + .await + .expect("thread should load") + .expect("thread should exist"); + assert_eq!( + datetime_to_epoch_millis(older.updated_at), + 1_700_001_100_123 + ); + + sqlx::query("UPDATE threads SET updated_at = ? WHERE id = ?") + .bind(1_700_001_112_i64) + .bind(first_id.to_string()) + .execute(runtime.pool.as_ref()) + .await + .expect("legacy timestamp write should succeed"); + let legacy = runtime + .get_thread(first_id) + .await + .expect("thread should load") + .expect("thread should exist"); + assert_eq!( + datetime_to_epoch_millis(legacy.updated_at), + 1_700_001_112_000 + ); + } + #[tokio::test] async fn apply_rollout_items_uses_override_updated_at_when_provided() { let codex_home = unique_temp_dir(); From b3ae531b3a6c4f9742dff6a7bd1b14796bde9c53 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Tue, 14 Apr 2026 17:00:18 +0100 Subject: [PATCH 12/16] feat: codex sampler (#17784) Add a pure sampler using the Codex auth and model config. To be used by other binary such as tape recorder --- codex-rs/Cargo.lock | 1 + codex-rs/cli/Cargo.toml | 1 + codex-rs/cli/src/main.rs | 24 ++++ codex-rs/cli/src/responses_cmd.rs | 219 ++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 codex-rs/cli/src/responses_cmd.rs diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index fc5c70e6b6..ad0e627d1b 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1669,6 +1669,7 @@ dependencies = [ "assert_matches", "clap", "clap_complete", + "codex-api", "codex-app-server", "codex-app-server-protocol", "codex-app-server-test-client", diff --git a/codex-rs/cli/Cargo.toml b/codex-rs/cli/Cargo.toml index ab0d86de2c..60caf888f8 100644 --- a/codex-rs/cli/Cargo.toml +++ b/codex-rs/cli/Cargo.toml @@ -24,6 +24,7 @@ codex-app-server = { workspace = true } codex-app-server-protocol = { workspace = true } codex-app-server-test-client = { workspace = true } codex-arg0 = { workspace = true } +codex-api = { workspace = true } codex-chatgpt = { workspace = true } codex-cloud-tasks = { path = "../cloud-tasks" } codex-utils-cli = { workspace = true } diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index baa6684b4c..563790c827 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -40,11 +40,14 @@ mod app_cmd; mod desktop_app; mod marketplace_cmd; mod mcp_cmd; +mod responses_cmd; #[cfg(not(windows))] mod wsl_paths; use crate::marketplace_cmd::MarketplaceCli; use crate::mcp_cmd::McpCli; +use crate::responses_cmd::ResponsesCommand; +use crate::responses_cmd::run_responses_command; use codex_core::config::Config; use codex_core::config::ConfigOverrides; @@ -151,6 +154,10 @@ enum Subcommand { #[clap(hide = true)] ResponsesApiProxy(ResponsesApiProxyArgs), + /// Internal: send one raw Responses API payload through Codex auth. + #[clap(hide = true)] + Responses(ResponsesCommand), + /// Internal: relay stdio to a Unix domain socket. #[clap(hide = true, name = "stdio-to-uds")] StdioToUds(StdioToUdsCommand), @@ -1015,6 +1022,14 @@ async fn cli_main(arg0_paths: Arg0DispatchPaths) -> anyhow::Result<()> { tokio::task::spawn_blocking(move || codex_responses_api_proxy::run_main(args)) .await??; } + Some(Subcommand::Responses(ResponsesCommand {})) => { + reject_remote_mode_for_subcommand( + root_remote.as_deref(), + root_remote_auth_token_env.as_deref(), + "responses", + )?; + run_responses_command(root_config_overrides).await?; + } Some(Subcommand::StdioToUds(cmd)) => { reject_remote_mode_for_subcommand( root_remote.as_deref(), @@ -1666,6 +1681,15 @@ mod tests { ); } + #[test] + fn responses_subcommand_is_hidden_from_help_but_parses() { + let help = MultitoolCli::command().render_help().to_string(); + assert!(!help.contains("responses")); + + let cli = MultitoolCli::try_parse_from(["codex", "responses"]).expect("parse"); + assert!(matches!(cli.subcommand, Some(Subcommand::Responses(_)))); + } + fn sample_exit_info(conversation_id: Option<&str>, thread_name: Option<&str>) -> AppExitInfo { let token_usage = TokenUsage { output_tokens: 2, diff --git a/codex-rs/cli/src/responses_cmd.rs b/codex-rs/cli/src/responses_cmd.rs new file mode 100644 index 0000000000..044c7e0b22 --- /dev/null +++ b/codex-rs/cli/src/responses_cmd.rs @@ -0,0 +1,219 @@ +use clap::Parser; +use codex_core::config::Config; +use codex_utils_cli::CliConfigOverrides; +use serde_json::json; +use tokio::io::AsyncReadExt; + +#[derive(Debug, Parser)] +pub(crate) struct ResponsesCommand {} + +pub(crate) async fn run_responses_command( + root_config_overrides: CliConfigOverrides, +) -> anyhow::Result<()> { + let mut payload_text = String::new(); + tokio::io::stdin().read_to_string(&mut payload_text).await?; + if payload_text.trim().is_empty() { + anyhow::bail!("expected Responses API JSON payload on stdin"); + } + + let payload: serde_json::Value = serde_json::from_str(&payload_text) + .map_err(|err| anyhow::anyhow!("failed to parse Responses API JSON payload: {err}"))?; + if payload.get("stream").and_then(serde_json::Value::as_bool) != Some(true) { + anyhow::bail!("codex responses expects a streaming payload with `\"stream\": true`"); + } + + let cli_overrides = root_config_overrides + .parse_overrides() + .map_err(anyhow::Error::msg)?; + let config = Config::load_with_cli_overrides(cli_overrides).await?; + let base_auth_manager = codex_login::AuthManager::shared_from_config( + &config, /*enable_codex_api_key_env*/ true, + ); + let auth_manager = + codex_login::auth_manager_for_provider(Some(base_auth_manager), &config.model_provider); + let auth = match auth_manager { + Some(auth_manager) => auth_manager.auth().await, + None => None, + }; + let api_provider = config + .model_provider + .to_api_provider(auth.as_ref().map(codex_login::CodexAuth::auth_mode))?; + let api_auth = codex_login::auth_provider_from_auth(auth, &config.model_provider)?; + let client = codex_api::ResponsesClient::new( + codex_api::ReqwestTransport::new(codex_login::default_client::build_reqwest_client()), + api_provider, + api_auth, + ); + + let mut stream = client + .stream( + payload, + Default::default(), + codex_api::Compression::None, + /*turn_state*/ None, + ) + .await?; + while let Some(event) = stream.rx_event.recv().await { + let event = event?; + println!("{}", serde_json::to_string(&response_event_to_json(event))?); + } + + Ok(()) +} + +fn response_event_to_json(event: codex_api::ResponseEvent) -> serde_json::Value { + match event { + codex_api::ResponseEvent::Created => { + json!({ "type": "response.created", "response": {} }) + } + codex_api::ResponseEvent::OutputItemDone(item) => { + json!({ "type": "response.output_item.done", "item": item }) + } + codex_api::ResponseEvent::OutputItemAdded(item) => { + json!({ "type": "response.output_item.added", "item": item }) + } + codex_api::ResponseEvent::ServerModel(model) => { + json!({ "type": "response.server_model", "model": model }) + } + codex_api::ResponseEvent::ServerReasoningIncluded(included) => { + json!({ "type": "response.server_reasoning_included", "included": included }) + } + codex_api::ResponseEvent::Completed { + response_id, + token_usage, + } => { + let response = match token_usage { + Some(token_usage) => json!({ + "id": response_id, + "usage": { + "input_tokens": token_usage.input_tokens, + "input_tokens_details": { + "cached_tokens": token_usage.cached_input_tokens, + }, + "output_tokens": token_usage.output_tokens, + "output_tokens_details": { + "reasoning_tokens": token_usage.reasoning_output_tokens, + }, + "total_tokens": token_usage.total_tokens, + }, + }), + None => json!({ "id": response_id }), + }; + json!({ "type": "response.completed", "response": response }) + } + codex_api::ResponseEvent::OutputTextDelta(delta) => { + json!({ "type": "response.output_text.delta", "delta": delta }) + } + codex_api::ResponseEvent::ReasoningSummaryDelta { + delta, + summary_index, + } => json!({ + "type": "response.reasoning_summary_text.delta", + "delta": delta, + "summary_index": summary_index, + }), + codex_api::ResponseEvent::ReasoningContentDelta { + delta, + content_index, + } => json!({ + "type": "response.reasoning_text.delta", + "delta": delta, + "content_index": content_index, + }), + codex_api::ResponseEvent::ReasoningSummaryPartAdded { summary_index } => { + json!({ + "type": "response.reasoning_summary_part.added", + "summary_index": summary_index, + }) + } + codex_api::ResponseEvent::RateLimits(rate_limits) => { + json!({ "type": "response.rate_limits", "rate_limits": rate_limits }) + } + codex_api::ResponseEvent::ModelsEtag(etag) => { + json!({ "type": "response.models_etag", "etag": etag }) + } + } +} + +#[cfg(test)] +mod tests { + use super::response_event_to_json; + use codex_protocol::protocol::TokenUsage; + use pretty_assertions::assert_eq; + use serde_json::json; + + #[test] + fn response_events_keep_replayable_response_envelopes() { + let created = response_event_to_json(codex_api::ResponseEvent::Created); + assert_eq!(created, json!({"type": "response.created", "response": {}})); + + let completed = response_event_to_json(codex_api::ResponseEvent::Completed { + response_id: "resp-1".to_string(), + token_usage: Some(TokenUsage { + input_tokens: 10, + cached_input_tokens: 4, + output_tokens: 7, + reasoning_output_tokens: 3, + total_tokens: 17, + }), + }); + assert_eq!( + completed, + json!({ + "type": "response.completed", + "response": { + "id": "resp-1", + "usage": { + "input_tokens": 10, + "input_tokens_details": { + "cached_tokens": 4, + }, + "output_tokens": 7, + "output_tokens_details": { + "reasoning_tokens": 3, + }, + "total_tokens": 17, + }, + }, + }) + ); + + let completed_without_usage = response_event_to_json(codex_api::ResponseEvent::Completed { + response_id: "resp-2".to_string(), + token_usage: None, + }); + assert_eq!( + completed_without_usage, + json!({"type": "response.completed", "response": {"id": "resp-2"}}) + ); + } + + #[test] + fn reasoning_deltas_use_responses_event_names() { + let summary = response_event_to_json(codex_api::ResponseEvent::ReasoningSummaryDelta { + delta: "plan".to_string(), + summary_index: 1, + }); + assert_eq!( + summary, + json!({ + "type": "response.reasoning_summary_text.delta", + "delta": "plan", + "summary_index": 1, + }) + ); + + let content = response_event_to_json(codex_api::ResponseEvent::ReasoningContentDelta { + delta: "detail".to_string(), + content_index: 2, + }); + assert_eq!( + content, + json!({ + "type": "response.reasoning_text.delta", + "delta": "detail", + "content_index": 2, + }) + ); + } +} From 81c0bcc9215e61b422f31e2f3443f0ac64fa68bc Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Tue, 14 Apr 2026 09:50:14 -0700 Subject: [PATCH 13/16] fix: Revert danger-full-access denylist-only mode (#17732) ## Summary - Reverts openai/codex#16946 and removes the danger-full-access denylist-only network mode. - Removes the corresponding config requirements, app-server protocol/schema, config API, TUI debug output, and network proxy behavior. - Drops stale tests that depended on the reverted mode while preserving newer managed allowlist-only coverage. ## Verification - `just write-app-server-schema` - `just fmt` - `cargo test -p codex-config network_requirements` - `cargo test -p codex-core network_proxy_spec` - `cargo test -p codex-core managed_network_proxy_decider_survives_full_access_start` - `cargo test -p codex-app-server map_requirements_toml_to_api` - `cargo test -p codex-tui debug_config_output` - `cargo test -p codex-app-server-protocol` - `just fix -p codex-config -p codex-core -p codex-app-server-protocol -p codex-app-server -p codex-tui` - `git diff --cached --check` Not run: full workspace `cargo test` (repo instructions ask for confirmation before that broader run). --- .github/scripts/run-bazel-ci.sh | 2 +- .../codex_app_server_protocol.schemas.json | 6 - .../codex_app_server_protocol.v2.schemas.json | 6 - .../v2/ConfigRequirementsReadResponse.json | 6 - .../typescript/v2/NetworkRequirements.ts | 2 +- .../app-server-protocol/src/protocol/v2.rs | 4 - codex-rs/app-server/README.md | 2 +- codex-rs/app-server/src/config_api.rs | 5 - codex-rs/config/src/config_requirements.rs | 20 -- codex-rs/core/src/codex_tests.rs | 66 ------ .../core/src/config/network_proxy_spec.rs | 82 +++----- .../src/config/network_proxy_spec_tests.rs | 193 ------------------ .../core/src/plugins/discoverable_tests.rs | 7 +- codex-rs/core/tests/suite/apply_patch_cli.rs | 24 ++- codex-rs/core/tests/suite/approvals.rs | 2 +- codex-rs/core/tests/suite/pending_input.rs | 8 + codex-rs/tui/src/debug_config.rs | 9 +- 17 files changed, 60 insertions(+), 384 deletions(-) diff --git a/.github/scripts/run-bazel-ci.sh b/.github/scripts/run-bazel-ci.sh index 9c95fda157..fefca90b3a 100755 --- a/.github/scripts/run-bazel-ci.sh +++ b/.github/scripts/run-bazel-ci.sh @@ -92,7 +92,7 @@ print_bazel_test_log_tails() { for target in "${failed_targets[@]}"; do local rel_path="${target#//}" - rel_path="${rel_path/:/\/}" + rel_path="${rel_path/://}" local test_log="${testlogs_dir}/${rel_path}/test.log" echo "::group::Bazel test log tail for ${target}" diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 5ecdb5a5c0..ec3db74945 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -9829,12 +9829,6 @@ "null" ] }, - "dangerFullAccessDenylistOnly": { - "type": [ - "boolean", - "null" - ] - }, "dangerouslyAllowAllUnixSockets": { "type": [ "boolean", diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index a37456c86d..9294e533f3 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -6605,12 +6605,6 @@ "null" ] }, - "dangerFullAccessDenylistOnly": { - "type": [ - "boolean", - "null" - ] - }, "dangerouslyAllowAllUnixSockets": { "type": [ "boolean", diff --git a/codex-rs/app-server-protocol/schema/json/v2/ConfigRequirementsReadResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ConfigRequirementsReadResponse.json index ae6eb1dc7d..614575a955 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ConfigRequirementsReadResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ConfigRequirementsReadResponse.json @@ -151,12 +151,6 @@ "null" ] }, - "dangerFullAccessDenylistOnly": { - "type": [ - "boolean", - "null" - ] - }, "dangerouslyAllowAllUnixSockets": { "type": [ "boolean", diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/NetworkRequirements.ts b/codex-rs/app-server-protocol/schema/typescript/v2/NetworkRequirements.ts index d1cd1ab298..04e07ef1de 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/NetworkRequirements.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/NetworkRequirements.ts @@ -29,4 +29,4 @@ unixSockets: { [key in string]?: NetworkUnixSocketPermission } | null, /** * Legacy compatibility view derived from `unix_sockets`. */ -allowUnixSockets: Array | null, allowLocalBinding: boolean | null, dangerFullAccessDenylistOnly: boolean | null, }; +allowUnixSockets: Array | null, allowLocalBinding: boolean | null, }; diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 4f8d9f121c..6144ac290f 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -899,7 +899,6 @@ pub struct NetworkRequirements { /// Legacy compatibility view derived from `unix_sockets`. pub allow_unix_sockets: Option>, pub allow_local_binding: Option, - pub danger_full_access_denylist_only: Option, } #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] @@ -8103,7 +8102,6 @@ mod tests { dangerously_allow_all_unix_sockets: None, domains: None, managed_allowed_domains_only: None, - danger_full_access_denylist_only: None, allowed_domains: Some(vec!["api.openai.com".to_string()]), denied_domains: Some(vec!["blocked.example.com".to_string()]), unix_sockets: None, @@ -8130,7 +8128,6 @@ mod tests { ), ])), managed_allowed_domains_only: Some(true), - danger_full_access_denylist_only: Some(true), allowed_domains: Some(vec!["api.openai.com".to_string()]), denied_domains: Some(vec!["blocked.example.com".to_string()]), unix_sockets: Some(BTreeMap::from([ @@ -8161,7 +8158,6 @@ mod tests { "blocked.example.com": "deny" }, "managedAllowedDomainsOnly": true, - "dangerFullAccessDenylistOnly": true, "allowedDomains": ["api.openai.com"], "deniedDomains": ["blocked.example.com"], "unixSockets": { diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 6337fe1f01..9715db92f2 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -200,7 +200,7 @@ Example with notification opt-out: - `externalAgentConfig/import` — apply selected external-agent migration items by passing explicit `migrationItems` with `cwd` (`null` for home). - `config/value/write` — write a single config key/value to the user's config.toml on disk. - `config/batchWrite` — apply multiple config edits atomically to the user's config.toml on disk, with optional `reloadUserConfig: true` to hot-reload loaded threads. -- `configRequirements/read` — fetch loaded requirements constraints from `requirements.toml` and/or MDM (or `null` if none are configured), including allow-lists (`allowedApprovalPolicies`, `allowedSandboxModes`, `allowedWebSearchModes`), pinned feature values (`featureRequirements`), `enforceResidency`, and `network` constraints such as canonical domain/socket permissions plus `managedAllowedDomainsOnly` and `dangerFullAccessDenylistOnly`. +- `configRequirements/read` — fetch loaded requirements constraints from `requirements.toml` and/or MDM (or `null` if none are configured), including allow-lists (`allowedApprovalPolicies`, `allowedSandboxModes`, `allowedWebSearchModes`), pinned feature values (`featureRequirements`), `enforceResidency`, and `network` constraints such as canonical domain/socket permissions plus `managedAllowedDomainsOnly`. ### Example: Start or resume a thread diff --git a/codex-rs/app-server/src/config_api.rs b/codex-rs/app-server/src/config_api.rs index 7c39f1c44f..4d0b450d73 100644 --- a/codex-rs/app-server/src/config_api.rs +++ b/codex-rs/app-server/src/config_api.rs @@ -452,7 +452,6 @@ fn map_network_requirements_to_api( .collect() }), managed_allowed_domains_only: network.managed_allowed_domains_only, - danger_full_access_denylist_only: network.danger_full_access_denylist_only, allowed_domains, denied_domains, unix_sockets: network.unix_sockets.map(|unix_sockets| { @@ -598,7 +597,6 @@ mod tests { ]), }), managed_allowed_domains_only: Some(false), - danger_full_access_denylist_only: Some(true), unix_sockets: Some(CoreNetworkUnixSocketPermissionsToml { entries: std::collections::BTreeMap::from([( "/tmp/proxy.sock".to_string(), @@ -658,7 +656,6 @@ mod tests { ("example.com".to_string(), NetworkDomainPermission::Deny), ])), managed_allowed_domains_only: Some(false), - danger_full_access_denylist_only: Some(true), allowed_domains: Some(vec!["api.openai.com".to_string()]), denied_domains: Some(vec!["example.com".to_string()]), unix_sockets: Some(std::collections::BTreeMap::from([( @@ -693,7 +690,6 @@ mod tests { dangerously_allow_all_unix_sockets: None, domains: None, managed_allowed_domains_only: None, - danger_full_access_denylist_only: None, unix_sockets: Some(CoreNetworkUnixSocketPermissionsToml { entries: std::collections::BTreeMap::from([( "/tmp/ignored.sock".to_string(), @@ -717,7 +713,6 @@ mod tests { dangerously_allow_all_unix_sockets: None, domains: None, managed_allowed_domains_only: None, - danger_full_access_denylist_only: None, allowed_domains: None, denied_domains: None, unix_sockets: Some(std::collections::BTreeMap::from([( diff --git a/codex-rs/config/src/config_requirements.rs b/codex-rs/config/src/config_requirements.rs index 2e6756d81e..7abedc62f1 100644 --- a/codex-rs/config/src/config_requirements.rs +++ b/codex-rs/config/src/config_requirements.rs @@ -237,8 +237,6 @@ pub struct NetworkRequirementsToml { /// When true, only managed `allowed_domains` are respected while managed /// network enforcement is active. User allowlist entries are ignored. pub managed_allowed_domains_only: Option, - /// In danger-full-access mode, allow all network access and enforce managed deny entries. - pub danger_full_access_denylist_only: Option, pub unix_sockets: Option, pub allow_local_binding: Option, } @@ -257,8 +255,6 @@ struct RawNetworkRequirementsToml { /// When true, only managed `allowed_domains` are respected while managed /// network enforcement is active. User allowlist entries are ignored. managed_allowed_domains_only: Option, - /// In danger-full-access mode, allow all network access and enforce managed deny entries. - danger_full_access_denylist_only: Option, #[serde(default)] denied_domains: Option>, unix_sockets: Option, @@ -283,7 +279,6 @@ impl<'de> Deserialize<'de> for NetworkRequirementsToml { domains, allowed_domains, managed_allowed_domains_only, - danger_full_access_denylist_only, denied_domains, unix_sockets, allow_unix_sockets, @@ -312,7 +307,6 @@ impl<'de> Deserialize<'de> for NetworkRequirementsToml { domains: domains .or_else(|| legacy_domain_permissions_from_lists(allowed_domains, denied_domains)), managed_allowed_domains_only, - danger_full_access_denylist_only, unix_sockets: unix_sockets .or_else(|| legacy_unix_socket_permissions_from_list(allow_unix_sockets)), allow_local_binding, @@ -365,8 +359,6 @@ pub struct NetworkConstraints { /// When true, only managed `allowed_domains` are respected while managed /// network enforcement is active. User allowlist entries are ignored. pub managed_allowed_domains_only: Option, - /// In danger-full-access mode, allow all network access and enforce managed deny entries. - pub danger_full_access_denylist_only: Option, pub unix_sockets: Option, pub allow_local_binding: Option, } @@ -392,7 +384,6 @@ impl From for NetworkConstraints { dangerously_allow_all_unix_sockets, domains, managed_allowed_domains_only, - danger_full_access_denylist_only, unix_sockets, allow_local_binding, } = value; @@ -405,7 +396,6 @@ impl From for NetworkConstraints { dangerously_allow_all_unix_sockets, domains, managed_allowed_domains_only, - danger_full_access_denylist_only, unix_sockets, allow_local_binding, } @@ -1811,7 +1801,6 @@ allowed_approvals_reviewers = ["user"] allow_upstream_proxy = false dangerously_allow_all_unix_sockets = true managed_allowed_domains_only = true - danger_full_access_denylist_only = true allow_local_binding = false [experimental_network.domains] @@ -1862,10 +1851,6 @@ allowed_approvals_reviewers = ["user"] sourced_network.value.managed_allowed_domains_only, Some(true) ); - assert_eq!( - sourced_network.value.danger_full_access_denylist_only, - Some(true) - ); assert_eq!( sourced_network.value.unix_sockets.as_ref(), Some(&NetworkUnixSocketPermissionsToml { @@ -1889,7 +1874,6 @@ allowed_approvals_reviewers = ["user"] dangerously_allow_all_unix_sockets = true allowed_domains = ["api.example.com", "*.openai.com"] managed_allowed_domains_only = true - danger_full_access_denylist_only = true denied_domains = ["blocked.example.com"] allow_unix_sockets = ["/tmp/example.sock"] allow_local_binding = false @@ -1934,10 +1918,6 @@ allowed_approvals_reviewers = ["user"] sourced_network.value.managed_allowed_domains_only, Some(true) ); - assert_eq!( - sourced_network.value.danger_full_access_denylist_only, - Some(true) - ); assert_eq!( sourced_network.value.unix_sockets.as_ref(), Some(&NetworkUnixSocketPermissionsToml { diff --git a/codex-rs/core/src/codex_tests.rs b/codex-rs/core/src/codex_tests.rs index 19ee45f411..25a72d3bb7 100644 --- a/codex-rs/core/src/codex_tests.rs +++ b/codex-rs/core/src/codex_tests.rs @@ -588,78 +588,12 @@ async fn start_managed_network_proxy_ignores_invalid_execpolicy_network_rules() Ok(()) } -#[tokio::test] -async fn managed_network_proxy_refreshes_when_sandbox_policy_changes() -> anyhow::Result<()> { - let spec = crate::config::NetworkProxySpec::from_config_and_constraints( - NetworkProxyConfig::default(), - Some(NetworkConstraints { - domains: Some(NetworkDomainPermissionsToml { - entries: std::collections::BTreeMap::from([( - "blocked.example.com".to_string(), - NetworkDomainPermissionToml::Deny, - )]), - }), - danger_full_access_denylist_only: Some(true), - allow_local_binding: Some(false), - ..Default::default() - }), - &SandboxPolicy::new_workspace_write_policy(), - )?; - let exec_policy = Policy::empty(); - - let (started_proxy, _) = Session::start_managed_network_proxy( - &spec, - &exec_policy, - &SandboxPolicy::new_workspace_write_policy(), - /*network_policy_decider*/ None, - /*blocked_request_observer*/ None, - /*managed_network_requirements_enabled*/ false, - crate::config::NetworkProxyAuditMetadata::default(), - ) - .await?; - - assert!(!started_proxy.proxy().allow_local_binding()); - let current_cfg = started_proxy.proxy().current_cfg().await?; - assert_eq!(current_cfg.network.allowed_domains(), None); - assert_eq!( - current_cfg.network.denied_domains(), - Some(vec!["blocked.example.com".to_string()]) - ); - - let spec = spec.recompute_for_sandbox_policy(&SandboxPolicy::DangerFullAccess)?; - spec.apply_to_started_proxy(&started_proxy).await?; - - assert!(started_proxy.proxy().allow_local_binding()); - let current_cfg = started_proxy.proxy().current_cfg().await?; - assert_eq!( - current_cfg.network.allowed_domains(), - Some(vec!["*".to_string()]) - ); - assert_eq!( - current_cfg.network.denied_domains(), - Some(vec!["blocked.example.com".to_string()]) - ); - - let spec = spec.recompute_for_sandbox_policy(&SandboxPolicy::new_workspace_write_policy())?; - spec.apply_to_started_proxy(&started_proxy).await?; - - assert!(!started_proxy.proxy().allow_local_binding()); - let current_cfg = started_proxy.proxy().current_cfg().await?; - assert_eq!(current_cfg.network.allowed_domains(), None); - assert_eq!( - current_cfg.network.denied_domains(), - Some(vec!["blocked.example.com".to_string()]) - ); - Ok(()) -} - #[tokio::test] async fn managed_network_proxy_decider_survives_full_access_start() -> anyhow::Result<()> { let spec = crate::config::NetworkProxySpec::from_config_and_constraints( NetworkProxyConfig::default(), Some(NetworkConstraints { enabled: Some(true), - danger_full_access_denylist_only: Some(true), ..Default::default() }), &SandboxPolicy::DangerFullAccess, diff --git a/codex-rs/core/src/config/network_proxy_spec.rs b/codex-rs/core/src/config/network_proxy_spec.rs index b67c41a442..acabe24f20 100644 --- a/codex-rs/core/src/config/network_proxy_spec.rs +++ b/codex-rs/core/src/config/network_proxy_spec.rs @@ -20,8 +20,6 @@ use codex_protocol::protocol::SandboxPolicy; use std::collections::HashSet; use std::sync::Arc; -const GLOBAL_ALLOWLIST_PATTERN: &str = "*"; - #[derive(Debug, Clone, PartialEq, Eq)] pub struct NetworkProxySpec { base_config: NetworkProxyConfig, @@ -225,8 +223,6 @@ impl NetworkProxySpec { let allowlist_expansion_enabled = Self::allowlist_expansion_enabled(sandbox_policy, hard_deny_allowlist_misses); let denylist_expansion_enabled = Self::denylist_expansion_enabled(sandbox_policy); - let danger_full_access_denylist_only = - Self::danger_full_access_denylist_only_enabled(requirements, sandbox_policy); if let Some(enabled) = requirements.enabled { config.network.enabled = enabled; @@ -257,43 +253,37 @@ impl NetworkProxySpec { constraints.dangerously_allow_all_unix_sockets = Some(dangerously_allow_all_unix_sockets); } - if danger_full_access_denylist_only { - config - .network - .set_allowed_domains(vec![GLOBAL_ALLOWLIST_PATTERN.to_string()]); - } else { - let managed_allowed_domains = if hard_deny_allowlist_misses { - Some( - requirements - .domains - .as_ref() - .and_then(codex_config::NetworkDomainPermissionsToml::allowed_domains) - .unwrap_or_default(), - ) - } else { + let managed_allowed_domains = if hard_deny_allowlist_misses { + Some( requirements .domains .as_ref() .and_then(codex_config::NetworkDomainPermissionsToml::allowed_domains) + .unwrap_or_default(), + ) + } else { + requirements + .domains + .as_ref() + .and_then(codex_config::NetworkDomainPermissionsToml::allowed_domains) + }; + if let Some(managed_allowed_domains) = managed_allowed_domains { + // Managed requirements seed the baseline allowlist. User additions + // can extend that baseline unless managed-only mode pins the + // effective allowlist to the managed set. + let effective_allowed_domains = if allowlist_expansion_enabled { + Self::merge_domain_lists( + managed_allowed_domains.clone(), + config.network.allowed_domains().as_deref().unwrap_or(&[]), + ) + } else { + managed_allowed_domains.clone() }; - if let Some(managed_allowed_domains) = managed_allowed_domains { - // Managed requirements seed the baseline allowlist. User additions - // can extend that baseline unless managed-only mode pins the - // effective allowlist to the managed set. - let effective_allowed_domains = if allowlist_expansion_enabled { - Self::merge_domain_lists( - managed_allowed_domains.clone(), - config.network.allowed_domains().as_deref().unwrap_or(&[]), - ) - } else { - managed_allowed_domains.clone() - }; - config - .network - .set_allowed_domains(effective_allowed_domains); - constraints.allowed_domains = Some(managed_allowed_domains); - constraints.allowlist_expansion_enabled = Some(allowlist_expansion_enabled); - } + config + .network + .set_allowed_domains(effective_allowed_domains); + constraints.allowed_domains = Some(managed_allowed_domains); + constraints.allowlist_expansion_enabled = Some(allowlist_expansion_enabled); } let managed_denied_domains = requirements .domains @@ -312,7 +302,7 @@ impl NetworkProxySpec { constraints.denied_domains = Some(managed_denied_domains); constraints.denylist_expansion_enabled = Some(denylist_expansion_enabled); } - if requirements.unix_sockets.is_some() && !danger_full_access_denylist_only { + if requirements.unix_sockets.is_some() { let allow_unix_sockets = requirements .unix_sockets .as_ref() @@ -327,14 +317,6 @@ impl NetworkProxySpec { config.network.allow_local_binding = allow_local_binding; constraints.allow_local_binding = Some(allow_local_binding); } - if danger_full_access_denylist_only { - config.network.allow_upstream_proxy = true; - constraints.allow_upstream_proxy = Some(true); - config.network.dangerously_allow_all_unix_sockets = true; - constraints.dangerously_allow_all_unix_sockets = Some(true); - config.network.allow_local_binding = true; - constraints.allow_local_binding = Some(true); - } (config, constraints) } @@ -353,16 +335,6 @@ impl NetworkProxySpec { requirements.managed_allowed_domains_only.unwrap_or(false) } - fn danger_full_access_denylist_only_enabled( - requirements: &NetworkConstraints, - sandbox_policy: &SandboxPolicy, - ) -> bool { - matches!(sandbox_policy, SandboxPolicy::DangerFullAccess) - && requirements - .danger_full_access_denylist_only - .unwrap_or(false) - } - fn denylist_expansion_enabled(sandbox_policy: &SandboxPolicy) -> bool { matches!( sandbox_policy, diff --git a/codex-rs/core/src/config/network_proxy_spec_tests.rs b/codex-rs/core/src/config/network_proxy_spec_tests.rs index ff351d1e73..5ba4bd1536 100644 --- a/codex-rs/core/src/config/network_proxy_spec_tests.rs +++ b/codex-rs/core/src/config/network_proxy_spec_tests.rs @@ -1,11 +1,8 @@ use super::*; use crate::config_loader::NetworkDomainPermissionToml; use crate::config_loader::NetworkDomainPermissionsToml; -use crate::config_loader::NetworkUnixSocketPermissionToml; -use crate::config_loader::NetworkUnixSocketPermissionsToml; use codex_network_proxy::NetworkDomainPermission; use pretty_assertions::assert_eq; -use std::collections::BTreeMap; fn domain_permissions( entries: impl IntoIterator, @@ -183,196 +180,6 @@ fn danger_full_access_keeps_managed_allowlist_and_denylist_fixed() { assert_eq!(spec.constraints.denylist_expansion_enabled, Some(false)); } -#[test] -fn danger_full_access_denylist_only_allows_all_domains_and_enforces_managed_denies() { - let mut config = NetworkProxyConfig::default(); - config - .network - .set_allowed_domains(vec!["evil.com".to_string()]); - config - .network - .set_denied_domains(vec!["more-blocked.example.com".to_string()]); - let requirements = NetworkConstraints { - allow_upstream_proxy: Some(false), - dangerously_allow_all_unix_sockets: Some(false), - domains: Some(domain_permissions([ - ("*.example.com", NetworkDomainPermissionToml::Allow), - ("blocked.example.com", NetworkDomainPermissionToml::Deny), - ])), - danger_full_access_denylist_only: Some(true), - unix_sockets: Some(NetworkUnixSocketPermissionsToml { - entries: BTreeMap::from([( - "/tmp/managed.sock".to_string(), - NetworkUnixSocketPermissionToml::Allow, - )]), - }), - allow_local_binding: Some(false), - ..Default::default() - }; - - let spec = NetworkProxySpec::from_config_and_constraints( - config, - Some(requirements), - &SandboxPolicy::DangerFullAccess, - ) - .expect("denylist-only yolo mode should allow all domains except managed denies"); - - assert_eq!( - spec.config.network.allowed_domains(), - Some(vec!["*".to_string()]) - ); - assert_eq!( - spec.config.network.denied_domains(), - Some(vec!["blocked.example.com".to_string()]) - ); - assert!(spec.config.network.allow_upstream_proxy); - assert!(spec.config.network.dangerously_allow_all_unix_sockets); - assert!(spec.config.network.allow_local_binding); - assert_eq!(spec.constraints.allow_upstream_proxy, Some(true)); - assert_eq!( - spec.constraints.dangerously_allow_all_unix_sockets, - Some(true) - ); - assert_eq!(spec.constraints.allow_unix_sockets, None); - assert_eq!(spec.constraints.allow_local_binding, Some(true)); - assert_eq!(spec.constraints.allowed_domains, None); - assert_eq!(spec.constraints.allowlist_expansion_enabled, None); - assert_eq!( - spec.constraints.denied_domains, - Some(vec!["blocked.example.com".to_string()]) - ); - assert_eq!(spec.constraints.denylist_expansion_enabled, Some(false)); -} - -#[test] -fn danger_full_access_denylist_only_does_not_change_workspace_write_behavior() { - let mut config = NetworkProxyConfig::default(); - config - .network - .set_allowed_domains(vec!["api.example.com".to_string()]); - config - .network - .set_denied_domains(vec!["blocked.example.com".to_string()]); - let requirements = NetworkConstraints { - allow_upstream_proxy: Some(false), - dangerously_allow_all_unix_sockets: Some(false), - domains: Some(domain_permissions([ - ("*.example.com", NetworkDomainPermissionToml::Allow), - ( - "managed-blocked.example.com", - NetworkDomainPermissionToml::Deny, - ), - ])), - danger_full_access_denylist_only: Some(true), - unix_sockets: Some(NetworkUnixSocketPermissionsToml { - entries: BTreeMap::from([( - "/tmp/managed.sock".to_string(), - NetworkUnixSocketPermissionToml::Allow, - )]), - }), - allow_local_binding: Some(false), - ..Default::default() - }; - - let spec = NetworkProxySpec::from_config_and_constraints( - config, - Some(requirements), - &SandboxPolicy::new_workspace_write_policy(), - ) - .expect("denylist-only yolo flag should not affect workspace-write mode"); - - assert_eq!( - spec.config.network.allowed_domains(), - Some(vec![ - "*.example.com".to_string(), - "api.example.com".to_string() - ]) - ); - assert_eq!( - spec.config.network.denied_domains(), - Some(vec![ - "managed-blocked.example.com".to_string(), - "blocked.example.com".to_string() - ]) - ); - assert!(!spec.config.network.allow_upstream_proxy); - assert!(!spec.config.network.dangerously_allow_all_unix_sockets); - assert_eq!( - spec.config.network.allow_unix_sockets(), - vec!["/tmp/managed.sock".to_string()] - ); - assert!(!spec.config.network.allow_local_binding); - assert_eq!(spec.constraints.allow_upstream_proxy, Some(false)); - assert_eq!( - spec.constraints.dangerously_allow_all_unix_sockets, - Some(false) - ); - assert_eq!( - spec.constraints.allow_unix_sockets, - Some(vec!["/tmp/managed.sock".to_string()]) - ); - assert_eq!(spec.constraints.allow_local_binding, Some(false)); - assert_eq!( - spec.constraints.allowed_domains, - Some(vec!["*.example.com".to_string()]) - ); - assert_eq!(spec.constraints.allowlist_expansion_enabled, Some(true)); - assert_eq!( - spec.constraints.denied_domains, - Some(vec!["managed-blocked.example.com".to_string()]) - ); - assert_eq!(spec.constraints.denylist_expansion_enabled, Some(true)); -} - -#[test] -fn recompute_for_sandbox_policy_rebuilds_denylist_only_full_access_policy() { - let requirements = NetworkConstraints { - domains: Some(domain_permissions([( - "blocked.example.com", - NetworkDomainPermissionToml::Deny, - )])), - danger_full_access_denylist_only: Some(true), - ..Default::default() - }; - let spec = NetworkProxySpec::from_config_and_constraints( - NetworkProxyConfig::default(), - Some(requirements), - &SandboxPolicy::new_workspace_write_policy(), - ) - .expect("workspace-write policy should load"); - - assert_eq!(spec.config.network.allowed_domains(), None); - assert_eq!( - spec.config.network.denied_domains(), - Some(vec!["blocked.example.com".to_string()]) - ); - - let spec = spec - .recompute_for_sandbox_policy(&SandboxPolicy::DangerFullAccess) - .expect("full-access policy should load"); - - assert_eq!( - spec.config.network.allowed_domains(), - Some(vec!["*".to_string()]) - ); - assert_eq!( - spec.config.network.denied_domains(), - Some(vec!["blocked.example.com".to_string()]) - ); - assert!(spec.config.network.allow_local_binding); - - let spec = spec - .recompute_for_sandbox_policy(&SandboxPolicy::new_workspace_write_policy()) - .expect("workspace-write policy should reload"); - - assert_eq!(spec.config.network.allowed_domains(), None); - assert_eq!( - spec.config.network.denied_domains(), - Some(vec!["blocked.example.com".to_string()]) - ); - assert!(!spec.config.network.allow_local_binding); -} - #[test] fn managed_allowed_domains_only_disables_default_mode_allowlist_expansion() { let mut config = NetworkProxyConfig::default(); diff --git a/codex-rs/core/src/plugins/discoverable_tests.rs b/codex-rs/core/src/plugins/discoverable_tests.rs index b1d0d79e2f..a0fc3fb289 100644 --- a/codex-rs/core/src/plugins/discoverable_tests.rs +++ b/codex-rs/core/src/plugins/discoverable_tests.rs @@ -201,13 +201,16 @@ async fn list_tool_suggest_discoverable_plugins_does_not_reload_marketplace_per_ let logs = String::from_utf8(buffer.lock().expect("buffer lock").clone()).expect("utf8 logs"); assert_eq!(logs.matches("ignoring interface.defaultPrompt").count(), 2); + let normalized_logs = logs.replace('\\', "/"); assert_eq!( - logs.matches("build-ios-apps/.codex-plugin/plugin.json") + normalized_logs + .matches("build-ios-apps/.codex-plugin/plugin.json") .count(), 1 ); assert_eq!( - logs.matches("life-science-research/.codex-plugin/plugin.json") + normalized_logs + .matches("life-science-research/.codex-plugin/plugin.json") .count(), 1 ); diff --git a/codex-rs/core/tests/suite/apply_patch_cli.rs b/codex-rs/core/tests/suite/apply_patch_cli.rs index 4882bcd113..cb264a0b26 100644 --- a/codex-rs/core/tests/suite/apply_patch_cli.rs +++ b/codex-rs/core/tests/suite/apply_patch_cli.rs @@ -10,6 +10,7 @@ use core_test_support::test_codex::ApplyPatchModelOutput; use pretty_assertions::assert_eq; use std::sync::atomic::AtomicI32; use std::sync::atomic::Ordering; +use std::time::Duration; use codex_features::Feature; use codex_protocol::protocol::AskForApproval; @@ -34,6 +35,7 @@ use core_test_support::test_codex::TestCodexBuilder; use core_test_support::test_codex::TestCodexHarness; use core_test_support::test_codex::test_codex; use core_test_support::wait_for_event; +use core_test_support::wait_for_event_with_timeout; use serde_json::json; use test_case::test_case; use wiremock::Mock; @@ -950,7 +952,7 @@ async fn apply_patch_shell_command_heredoc_with_cd_emits_turn_diff() -> Result<( let script = "cd sub && apply_patch <<'EOF'\n*** Begin Patch\n*** Update File: in_sub.txt\n@@\n-before\n+after\n*** End Patch\nEOF\n"; let call_id = "shell-heredoc-cd"; - let args = json!({ "command": script, "timeout_ms": 5_000 }); + let args = json!({ "command": script, "timeout_ms": 30_000 }); let bodies = vec![ sse(vec![ ev_response_created("resp-1"), @@ -1444,14 +1446,18 @@ async fn apply_patch_aggregates_diff_preserves_success_after_failure() -> Result .await?; let mut last_diff: Option = None; - wait_for_event(&codex, |event| match event { - EventMsg::TurnDiff(ev) => { - last_diff = Some(ev.unified_diff.clone()); - false - } - EventMsg::TurnComplete(_) => true, - _ => false, - }) + wait_for_event_with_timeout( + &codex, + |event| match event { + EventMsg::TurnDiff(ev) => { + last_diff = Some(ev.unified_diff.clone()); + false + } + EventMsg::TurnComplete(_) => true, + _ => false, + }, + Duration::from_secs(30), + ) .await; let diff = last_diff.expect("expected TurnDiff after failed patch"); diff --git a/codex-rs/core/tests/suite/approvals.rs b/codex-rs/core/tests/suite/approvals.rs index f7874eaf45..84066ff6e6 100644 --- a/codex-rs/core/tests/suite/approvals.rs +++ b/codex-rs/core/tests/suite/approvals.rs @@ -229,7 +229,7 @@ impl ActionKind { let event = shell_event( call_id, &command, - /*timeout_ms*/ 5_000, + /*timeout_ms*/ 30_000, sandbox_permissions, )?; Ok((event, Some(command))) diff --git a/codex-rs/core/tests/suite/pending_input.rs b/codex-rs/core/tests/suite/pending_input.rs index 719cc1f0a5..2376da3d6c 100644 --- a/codex-rs/core/tests/suite/pending_input.rs +++ b/codex-rs/core/tests/suite/pending_input.rs @@ -157,6 +157,14 @@ async fn submit_queue_only_agent_mail(codex: &CodexThread, text: &str) { }) .await .unwrap_or_else(|err| panic!("submit queue-only agent mail: {err}")); + codex + .submit(Op::ListMcpTools) + .await + .unwrap_or_else(|err| panic!("submit list-mcp-tools barrier: {err}")); + wait_for_event(codex, |event| { + matches!(event, EventMsg::McpListToolsResponse(_)) + }) + .await; } async fn wait_for_reasoning_item_started(codex: &CodexThread) { diff --git a/codex-rs/tui/src/debug_config.rs b/codex-rs/tui/src/debug_config.rs index aa9526c4ed..16af4c24fb 100644 --- a/codex-rs/tui/src/debug_config.rs +++ b/codex-rs/tui/src/debug_config.rs @@ -367,7 +367,6 @@ fn format_network_constraints(network: &NetworkConstraints) -> String { dangerously_allow_all_unix_sockets, domains, managed_allowed_domains_only, - danger_full_access_denylist_only, unix_sockets, allow_local_binding, } = network; @@ -405,11 +404,6 @@ fn format_network_constraints(network: &NetworkConstraints) -> String { "managed_allowed_domains_only={managed_allowed_domains_only}" )); } - if let Some(danger_full_access_denylist_only) = danger_full_access_denylist_only { - parts.push(format!( - "danger_full_access_denylist_only={danger_full_access_denylist_only}" - )); - } if let Some(unix_sockets) = unix_sockets { parts.push(format!( "unix_sockets={}", @@ -605,7 +599,6 @@ mod tests { NetworkDomainPermissionToml::Allow, )]), }), - danger_full_access_denylist_only: Some(true), ..Default::default() }, RequirementSource::CloudRequirements, @@ -676,7 +669,7 @@ mod tests { assert!(rendered.contains("mcp_servers: docs (source: MDM managed_config.toml (legacy))")); assert!(rendered.contains("enforce_residency: us (source: cloud requirements)")); assert!(rendered.contains( - "experimental_network: enabled=true, domains={example.com=allow}, danger_full_access_denylist_only=true (source: cloud requirements)" + "experimental_network: enabled=true, domains={example.com=allow} (source: cloud requirements)" )); assert!(!rendered.contains(" - rules:")); } From d013576f8bd8f03144881a3756332e3e1079283c Mon Sep 17 00:00:00 2001 From: Rasmus Rygaard Date: Tue, 14 Apr 2026 09:53:17 -0700 Subject: [PATCH 14/16] Redirect debug client output to a file (#17234) In the app-server debug client, allow redirecting output to a file in addition to just stdout. Shell redirecting works OK but is a bit weird with the interactive mode of the debug client since a bunch of newlines get dumped into the shell. With async messages from MCPs starting it's also tricky to actually type in a prompt. --- codex-rs/debug-client/README.md | 7 ++-- codex-rs/debug-client/src/client.rs | 4 +-- codex-rs/debug-client/src/main.rs | 19 +++++++++- codex-rs/debug-client/src/output.rs | 55 ++++++++++++++++++++++++++++- codex-rs/debug-client/src/reader.rs | 4 +-- 5 files changed, 81 insertions(+), 8 deletions(-) diff --git a/codex-rs/debug-client/README.md b/codex-rs/debug-client/README.md index bd310d9056..447ed12360 100644 --- a/codex-rs/debug-client/README.md +++ b/codex-rs/debug-client/README.md @@ -12,7 +12,8 @@ Start the app-server client (it will spawn `codex app-server` itself): ``` cargo run -p codex-debug-client -- \ --codex-bin codex \ - --approval-policy on-request + --approval-policy on-request \ + --output-file /tmp/app-server-server-json.jsonl ``` You can resume a specific thread: @@ -29,6 +30,7 @@ cargo run -p codex-debug-client -- --thread-id thr_123 - `--approval-policy `: `untrusted`, `on-failure` (deprecated), `on-request`, `never`. - `--auto-approve`: auto-approve command/file-change approvals (default: decline). - `--final-only`: only show completed assistant messages and tool items. +- `--output-file `: write raw server JSONL to this file instead of stdout. - `--model `: optional model override for thread start/resume. - `--model-provider `: optional provider override. - `--cwd `: optional working directory override. @@ -46,7 +48,8 @@ Type a line to send it as a new turn. Commands are prefixed with `:`: The prompt shows the active thread id. Client messages (help, errors, approvals) print to stderr; raw server JSON prints to stdout so you can pipe/record it -unless `--final-only` is set. +unless `--final-only` is set. Pass `--output-file ` to record raw server +JSONL to a file instead of stdout. ## Notes diff --git a/codex-rs/debug-client/src/client.rs b/codex-rs/debug-client/src/client.rs index 2ada10e377..8962a1f02f 100644 --- a/codex-rs/debug-client/src/client.rs +++ b/codex-rs/debug-client/src/client.rs @@ -299,8 +299,8 @@ impl AppServerClient { } let line = buffer.trim_end_matches(['\n', '\r']); - if !line.is_empty() && !self.filtered_output { - let _ = output.server_line(line); + if !line.is_empty() { + let _ = output.server_json_line(line, self.filtered_output); } let message = match serde_json::from_str::(line) { diff --git a/codex-rs/debug-client/src/main.rs b/codex-rs/debug-client/src/main.rs index 6350331d6d..a09d9d04f5 100644 --- a/codex-rs/debug-client/src/main.rs +++ b/codex-rs/debug-client/src/main.rs @@ -4,8 +4,10 @@ mod output; mod reader; mod state; +use std::fs::File; use std::io; use std::io::BufRead; +use std::path::PathBuf; use std::sync::mpsc; use anyhow::Context; @@ -50,6 +52,10 @@ struct Cli { #[arg(long, default_value_t = false)] final_only: bool, + /// Write raw server JSONL to this file instead of stdout. + #[arg(long, value_name = "PATH")] + output_file: Option, + /// Optional model override when starting/resuming a thread. #[arg(long)] model: Option, @@ -65,7 +71,18 @@ struct Cli { fn main() -> Result<()> { let cli = Cli::parse(); - let output = Output::new(); + let jsonl_file = cli + .output_file + .as_ref() + .map(File::create) + .transpose() + .with_context(|| { + let Some(path) = cli.output_file.as_ref() else { + return "open output file".to_string(); + }; + format!("open output file {}", path.display()) + })?; + let output = Output::new(jsonl_file); let approval_policy = parse_approval_policy(&cli.approval_policy)?; let mut client = AppServerClient::spawn( diff --git a/codex-rs/debug-client/src/output.rs b/codex-rs/debug-client/src/output.rs index ca3ac9d9cb..f164a3ade8 100644 --- a/codex-rs/debug-client/src/output.rs +++ b/codex-rs/debug-client/src/output.rs @@ -1,4 +1,5 @@ #![allow(clippy::expect_used)] +use std::fs::File; use std::io; use std::io::IsTerminal; use std::io::Write; @@ -24,19 +25,41 @@ pub struct Output { lock: Arc>, prompt: Arc>, color: bool, + jsonl_file: Option>>, } impl Output { - pub fn new() -> Self { + pub fn new(jsonl_file: Option) -> Self { let no_color = std::env::var_os("NO_COLOR").is_some(); let color = !no_color && io::stdout().is_terminal() && io::stderr().is_terminal(); Self { lock: Arc::new(Mutex::new(())), prompt: Arc::new(Mutex::new(PromptState::default())), color, + jsonl_file: jsonl_file.map(|file| Arc::new(Mutex::new(file))), } } + pub fn server_json_line(&self, line: &str, filtered_output: bool) -> io::Result<()> { + let _guard = self.lock.lock().expect("output lock poisoned"); + + if let Some(file) = self.jsonl_file.as_ref() { + let mut file = file.lock().expect("jsonl file lock poisoned"); + writeln!(file, "{line}")?; + file.flush()?; + } + + if self.jsonl_file.is_none() && !filtered_output { + self.clear_prompt_line_locked()?; + let mut stdout = io::stdout(); + writeln!(stdout, "{line}")?; + stdout.flush()?; + self.redraw_prompt_locked()?; + } + + Ok(()) + } + pub fn server_line(&self, line: &str) -> io::Result<()> { let _guard = self.lock.lock().expect("output lock poisoned"); self.clear_prompt_line_locked()?; @@ -120,3 +143,33 @@ impl Output { Ok(()) } } + +#[cfg(test)] +mod tests { + use std::fs; + + use super::*; + + #[test] + fn server_json_line_writes_to_configured_file() { + let path = std::env::temp_dir().join(format!( + "codex-debug-client-output-{}.jsonl", + std::process::id() + )); + let file = File::create(&path).expect("create output file"); + let output = Output::new(Some(file)); + + output + .server_json_line(r#"{"id":1}"#, false) + .expect("write unfiltered line"); + output + .server_json_line(r#"{"id":2}"#, true) + .expect("write filtered line"); + + assert_eq!( + fs::read_to_string(&path).expect("read output file"), + "{\"id\":1}\n{\"id\":2}\n" + ); + let _ = fs::remove_file(path); + } +} diff --git a/codex-rs/debug-client/src/reader.rs b/codex-rs/debug-client/src/reader.rs index ed401bb100..cb51641633 100644 --- a/codex-rs/debug-client/src/reader.rs +++ b/codex-rs/debug-client/src/reader.rs @@ -67,8 +67,8 @@ pub fn start_reader( } let line = buffer.trim_end_matches(['\n', '\r']); - if !line.is_empty() && !filtered_output { - let _ = output.server_line(line); + if !line.is_empty() { + let _ = output.server_json_line(line, filtered_output); } let Ok(message) = serde_json::from_str::(line) else { From 769b1c3d7e40195f5a4048f82245e3fc19441c4d Mon Sep 17 00:00:00 2001 From: Curtis 'Fjord' Hawthorne Date: Tue, 14 Apr 2026 11:06:50 -0700 Subject: [PATCH 15/16] Keep image_detail_original as a removed feature flag (#17803) --- codex-rs/cli/src/main.rs | 13 +++++++++++++ codex-rs/core/config.schema.json | 6 ++++++ codex-rs/features/src/lib.rs | 9 +++++++++ codex-rs/features/src/tests.rs | 14 ++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 563790c827..5ad86a80fd 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -2205,6 +2205,19 @@ mod tests { ); } + #[test] + fn feature_toggles_accept_removed_image_detail_original_flag() { + let toggles = FeatureToggles { + enable: vec!["image_detail_original".to_string()], + disable: Vec::new(), + }; + let overrides = toggles.to_overrides().expect("valid features"); + assert_eq!( + overrides, + vec!["features.image_detail_original=true".to_string(),] + ); + } + #[test] fn feature_toggles_unknown_feature_errors() { let toggles = FeatureToggles { diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 58c1eb4d12..7c3f6dd99a 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -398,6 +398,9 @@ "guardian_approval": { "type": "boolean" }, + "image_detail_original": { + "type": "boolean" + }, "image_generation": { "type": "boolean" }, @@ -2251,6 +2254,9 @@ "guardian_approval": { "type": "boolean" }, + "image_detail_original": { + "type": "boolean" + }, "image_generation": { "type": "boolean" }, diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index aa32a45718..727c32ccd4 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -178,6 +178,9 @@ pub enum Feature { RealtimeConversation, /// Connect app-server to the ChatGPT remote control service. RemoteControl, + /// Removed compatibility flag retained as a no-op so old wrappers can + /// still pass `--enable image_detail_original`. + ImageDetailOriginal, /// Removed compatibility flag. The TUI now always uses the app-server implementation. TuiAppServer, /// Prevent idle system sleep while a turn is actively running. @@ -874,6 +877,12 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::UnderDevelopment, default_enabled: false, }, + FeatureSpec { + id: Feature::ImageDetailOriginal, + key: "image_detail_original", + stage: Stage::Removed, + default_enabled: false, + }, FeatureSpec { id: Feature::TuiAppServer, key: "tui_app_server", diff --git a/codex-rs/features/src/tests.rs b/codex-rs/features/src/tests.rs index 3ecab0997c..818ffc8bd7 100644 --- a/codex-rs/features/src/tests.rs +++ b/codex-rs/features/src/tests.rs @@ -53,6 +53,12 @@ fn use_linux_sandbox_bwrap_is_removed_and_disabled_by_default() { assert_eq!(Feature::UseLinuxSandboxBwrap.default_enabled(), false); } +#[test] +fn image_detail_original_is_removed_and_disabled_by_default() { + assert_eq!(Feature::ImageDetailOriginal.stage(), Stage::Removed); + assert_eq!(Feature::ImageDetailOriginal.default_enabled(), false); +} + #[test] fn js_repl_is_experimental_and_user_toggleable() { let spec = Feature::JsRepl.info(); @@ -145,6 +151,14 @@ fn use_linux_sandbox_bwrap_is_a_removed_feature_key() { ); } +#[test] +fn image_detail_original_is_a_removed_feature_key() { + assert_eq!( + feature_for_key("image_detail_original"), + Some(Feature::ImageDetailOriginal) + ); +} + #[test] fn image_generation_is_under_development() { assert_eq!(Feature::ImageGeneration.stage(), Stage::UnderDevelopment); From 23d4098c0f5ef26deca8aaff4851055aadd82e37 Mon Sep 17 00:00:00 2001 From: Ruslan Nigmatullin Date: Tue, 14 Apr 2026 11:24:34 -0700 Subject: [PATCH 16/16] app-server: prepare to run initialized rpcs concurrently (#17372) ## Summary - Refactors `MessageProcessor` and per-connection session state so initialized service RPC handling can be moved into spawned tasks in a follow-up PR. - Shares the processor and initialized session data with `Arc`/`OnceLock` instead of mutable borrowed connection state. - Keeps initialized request handling synchronous in this PR; it does **not** call `tokio::spawn` for service RPCs yet. ## Testing - `just fmt` - `cargo test -p codex-app-server` *(fails on existing hardening gaps covered by #17375, #17376, and #17377; the pipelined config regression passed before the unrelated failures)* - `just fix -p codex-app-server` --- codex-rs/app-server/src/app_server_tracing.rs | 8 +- codex-rs/app-server/src/in_process.rs | 23 +- codex-rs/app-server/src/lib.rs | 37 +- codex-rs/app-server/src/message_processor.rs | 362 +++++++++++------- .../src/message_processor/tracing_tests.rs | 16 +- codex-rs/app-server/src/transport/mod.rs | 4 +- 6 files changed, 282 insertions(+), 168 deletions(-) diff --git a/codex-rs/app-server/src/app_server_tracing.rs b/codex-rs/app-server/src/app_server_tracing.rs index b06a8e52c4..2118e77300 100644 --- a/codex-rs/app-server/src/app_server_tracing.rs +++ b/codex-rs/app-server/src/app_server_tracing.rs @@ -72,10 +72,10 @@ pub(crate) fn typed_request_span( &span, client_info .map(|(client_name, _)| client_name) - .or(session.app_server_client_name.as_deref()), + .or(session.app_server_client_name()), client_info .map(|(_, client_version)| client_version) - .or(session.client_version.as_deref()), + .or(session.client_version()), ); attach_parent_context(&span, &method, request.id(), /*parent_trace*/ None); @@ -147,7 +147,7 @@ fn client_name<'a>( if let Some(params) = initialize_client_info { return Some(params.client_info.name.as_str()); } - session.app_server_client_name.as_deref() + session.app_server_client_name() } fn client_version<'a>( @@ -157,7 +157,7 @@ fn client_version<'a>( if let Some(params) = initialize_client_info { return Some(params.client_info.version.as_str()); } - session.client_version.as_deref() + session.client_version() } fn initialize_client_info(request: &JSONRPCRequest) -> Option { diff --git a/codex-rs/app-server/src/in_process.rs b/codex-rs/app-server/src/in_process.rs index eb76848c57..9ac0e6cf42 100644 --- a/codex-rs/app-server/src/in_process.rs +++ b/codex-rs/app-server/src/in_process.rs @@ -386,7 +386,7 @@ fn start_uninitialized(args: InProcessStartArgs) -> InProcessClientHandle { AuthManager::shared_from_config(args.config.as_ref(), args.enable_codex_api_key_env); let (processor_tx, mut processor_rx) = mpsc::channel::(channel_capacity); let mut processor_handle = tokio::spawn(async move { - let processor = MessageProcessor::new(MessageProcessorArgs { + let processor = Arc::new(MessageProcessor::new(MessageProcessorArgs { outgoing: Arc::clone(&processor_outgoing), arg0_paths: args.arg0_paths, config: args.config, @@ -401,9 +401,9 @@ fn start_uninitialized(args: InProcessStartArgs) -> InProcessClientHandle { auth_manager, rpc_transport: AppServerRpcTransport::InProcess, remote_control_handle: None, - }); + })); let mut thread_created_rx = processor.thread_created_receiver(); - let mut session = ConnectionSessionState::default(); + let session = Arc::new(ConnectionSessionState::default()); let mut listen_for_threads = true; loop { @@ -411,28 +411,33 @@ fn start_uninitialized(args: InProcessStartArgs) -> InProcessClientHandle { command = processor_rx.recv() => { match command { Some(ProcessorCommand::Request(request)) => { - let was_initialized = session.initialized; + let was_initialized = session.initialized(); processor .process_client_request( IN_PROCESS_CONNECTION_ID, *request, - &mut session, + Arc::clone(&session), &outbound_initialized, ) .await; + let opted_out_notification_methods_snapshot = + session.opted_out_notification_methods(); + let experimental_api_enabled = + session.experimental_api_enabled(); + let is_initialized = session.initialized(); if let Ok(mut opted_out_notification_methods) = outbound_opted_out_notification_methods.write() { *opted_out_notification_methods = - session.opted_out_notification_methods.clone(); + opted_out_notification_methods_snapshot; } else { warn!("failed to update outbound opted-out notifications"); } outbound_experimental_api_enabled.store( - session.experimental_api_enabled, + experimental_api_enabled, Ordering::Release, ); - if !was_initialized && session.initialized { + if !was_initialized && is_initialized { processor.send_initialize_notifications().await; } } @@ -447,7 +452,7 @@ fn start_uninitialized(args: InProcessStartArgs) -> InProcessClientHandle { created = thread_created_rx.recv(), if listen_for_threads => { match created { Ok(thread_id) => { - let connection_ids = if session.initialized { + let connection_ids = if session.initialized() { vec![IN_PROCESS_CONNECTION_ID] } else { Vec::::new() diff --git a/codex-rs/app-server/src/lib.rs b/codex-rs/app-server/src/lib.rs index 918108f16b..ca48ad0f41 100644 --- a/codex-rs/app-server/src/lib.rs +++ b/codex-rs/app-server/src/lib.rs @@ -652,7 +652,7 @@ pub async fn run_main_with_transport( AuthManager::shared_from_config(&config, /*enable_codex_api_key_env*/ false); let cli_overrides: Vec<(String, TomlValue)> = cli_kv_overrides.clone(); let loader_overrides = loader_overrides_for_config_api; - let processor = MessageProcessor::new(MessageProcessorArgs { + let processor = Arc::new(MessageProcessor::new(MessageProcessorArgs { outgoing: outgoing_message_sender, arg0_paths, config: Arc::new(config), @@ -667,7 +667,7 @@ pub async fn run_main_with_transport( auth_manager, rpc_transport: analytics_rpc_transport(transport), remote_control_handle: Some(remote_control_handle), - }); + })); let mut thread_created_rx = processor.thread_created_receiver(); let mut running_turn_count_rx = processor.subscribe_running_assistant_turn_count(); let mut connections = HashMap::::new(); @@ -769,23 +769,28 @@ pub async fn run_main_with_transport( warn!("dropping request from unknown connection: {connection_id:?}"); continue; }; - let was_initialized = connection_state.session.initialized; + let was_initialized = + connection_state.session.initialized(); processor .process_request( connection_id, request, transport, - &mut connection_state.session, + Arc::clone(&connection_state.session), ) .await; + let opted_out_notification_methods_snapshot = connection_state + .session + .opted_out_notification_methods(); + let experimental_api_enabled = + connection_state.session.experimental_api_enabled(); + let is_initialized = connection_state.session.initialized(); if let Ok(mut opted_out_notification_methods) = connection_state .outbound_opted_out_notification_methods .write() { - *opted_out_notification_methods = connection_state - .session - .opted_out_notification_methods - .clone(); + *opted_out_notification_methods = + opted_out_notification_methods_snapshot; } else { warn!( "failed to update outbound opted-out notifications" @@ -794,10 +799,10 @@ pub async fn run_main_with_transport( connection_state .outbound_experimental_api_enabled .store( - connection_state.session.experimental_api_enabled, + experimental_api_enabled, std::sync::atomic::Ordering::Release, ); - if !was_initialized && connection_state.session.initialized { + if !was_initialized && is_initialized { processor .send_initialize_notifications_to_connection( connection_id, @@ -837,12 +842,12 @@ pub async fn run_main_with_transport( created = thread_created_rx.recv(), if listen_for_threads => { match created { Ok(thread_id) => { - let initialized_connection_ids: Vec = connections - .iter() - .filter_map(|(connection_id, connection_state)| { - connection_state.session.initialized.then_some(*connection_id) - }) - .collect(); + let mut initialized_connection_ids = Vec::new(); + for (connection_id, connection_state) in &connections { + if connection_state.session.initialized() { + initialized_connection_ids.push(*connection_id); + } + } processor .try_attach_thread_listener( thread_id, diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index 7bfc01b2a1..48072cce6d 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -2,6 +2,7 @@ use std::collections::BTreeMap; use std::collections::HashSet; use std::future::Future; use std::sync::Arc; +use std::sync::OnceLock; use std::sync::RwLock; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; @@ -20,6 +21,7 @@ use crate::outgoing_message::RequestContext; use crate::transport::AppServerTransport; use crate::transport::RemoteControlHandle; use async_trait::async_trait; +use axum::http::HeaderValue; use codex_analytics::AnalyticsEventsClient; use codex_analytics::AppServerRpcTransport; use codex_app_server_protocol::AppListUpdatedNotification; @@ -173,13 +175,52 @@ pub(crate) struct MessageProcessor { remote_control_handle: Option, } -#[derive(Clone, Debug, Default)] +#[derive(Debug, Default)] pub(crate) struct ConnectionSessionState { - pub(crate) initialized: bool, - pub(crate) experimental_api_enabled: bool, - pub(crate) opted_out_notification_methods: HashSet, - pub(crate) app_server_client_name: Option, - pub(crate) client_version: Option, + initialized: OnceLock, +} + +#[derive(Debug)] +struct InitializedConnectionSessionState { + experimental_api_enabled: bool, + opted_out_notification_methods: HashSet, + app_server_client_name: String, + client_version: String, +} + +impl ConnectionSessionState { + pub(crate) fn initialized(&self) -> bool { + self.initialized.get().is_some() + } + + pub(crate) fn experimental_api_enabled(&self) -> bool { + self.initialized + .get() + .is_some_and(|session| session.experimental_api_enabled) + } + + pub(crate) fn opted_out_notification_methods(&self) -> HashSet { + self.initialized + .get() + .map(|session| session.opted_out_notification_methods.clone()) + .unwrap_or_default() + } + + pub(crate) fn app_server_client_name(&self) -> Option<&str> { + self.initialized + .get() + .map(|session| session.app_server_client_name.as_str()) + } + + pub(crate) fn client_version(&self) -> Option<&str> { + self.initialized + .get() + .map(|session| session.client_version.as_str()) + } + + fn initialize(&self, session: InitializedConnectionSessionState) -> Result<(), ()> { + self.initialized.set(session).map_err(|_| ()) + } } pub(crate) struct MessageProcessorArgs { @@ -299,11 +340,11 @@ impl MessageProcessor { } pub(crate) async fn process_request( - &self, + self: &Arc, connection_id: ConnectionId, request: JSONRPCRequest, transport: AppServerTransport, - session: &mut ConnectionSessionState, + session: Arc, ) { let request_method = request.method.as_str(); tracing::trace!( @@ -316,7 +357,7 @@ impl MessageProcessor { request_id: request.id.clone(), }; let request_span = - crate::app_server_tracing::request_span(&request, transport, connection_id, session); + crate::app_server_tracing::request_span(&request, transport, connection_id, &session); let request_trace = request.trace.as_ref().map(|trace| W3cTraceContext { traceparent: trace.traceparent.clone(), tracestate: trace.tracestate.clone(), @@ -358,7 +399,7 @@ impl MessageProcessor { self.handle_client_request( request_id.clone(), codex_request, - session, + Arc::clone(&session), /*outbound_initialized*/ None, request_context.clone(), ) @@ -373,10 +414,10 @@ impl MessageProcessor { /// This bypasses JSON request deserialization but keeps identical request /// semantics by delegating to `handle_client_request`. pub(crate) async fn process_client_request( - &self, + self: &Arc, connection_id: ConnectionId, request: ClientRequest, - session: &mut ConnectionSessionState, + session: Arc, outbound_initialized: &AtomicBool, ) { let request_id = ConnectionRequestId { @@ -384,7 +425,7 @@ impl MessageProcessor { request_id: request.id().clone(), }; let request_span = - crate::app_server_tracing::typed_request_span(&request, connection_id, session); + crate::app_server_tracing::typed_request_span(&request, connection_id, &session); let request_context = RequestContext::new(request_id.clone(), request_span, /*parent_trace*/ None); tracing::trace!( @@ -402,7 +443,7 @@ impl MessageProcessor { self.handle_client_request( request_id.clone(), request, - session, + Arc::clone(&session), Some(outbound_initialized), request_context.clone(), ) @@ -525,10 +566,10 @@ impl MessageProcessor { } async fn handle_client_request( - &self, + self: &Arc, connection_request_id: ConnectionRequestId, codex_request: ClientRequest, - session: &mut ConnectionSessionState, + session: Arc, // `Some(...)` means the caller wants initialize to immediately mark the // connection outbound-ready. Websocket JSON-RPC calls pass `None` so // lib.rs can deliver connection-scoped initialize notifications first. @@ -536,126 +577,166 @@ impl MessageProcessor { request_context: RequestContext, ) { let connection_id = connection_request_id.connection_id; - match codex_request { + if let ClientRequest::Initialize { request_id, params } = codex_request { // Handle Initialize internally so CodexMessageProcessor does not have to concern // itself with the `initialized` bool. - ClientRequest::Initialize { request_id, params } => { - let connection_request_id = ConnectionRequestId { - connection_id, - request_id, + let connection_request_id = ConnectionRequestId { + connection_id, + request_id, + }; + if session.initialized() { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "Already initialized".to_string(), + data: None, }; - if session.initialized { - let error = JSONRPCErrorError { - code: INVALID_REQUEST_ERROR_CODE, - message: "Already initialized".to_string(), - data: None, - }; - self.outgoing.send_error(connection_request_id, error).await; - return; - } - - // TODO(maxj): Revisit capability scoping for `experimental_api_enabled`. - // Current behavior is per-connection. Reviewer feedback notes this can - // create odd cross-client behavior (for example dynamic tool calls on a - // shared thread when another connected client did not opt into - // experimental API). Proposed direction is instance-global first-write-wins - // with initialize-time mismatch rejection. - let analytics_initialize_params = params.clone(); - let (experimental_api_enabled, opt_out_notification_methods) = - match params.capabilities { - Some(capabilities) => ( - capabilities.experimental_api, - capabilities - .opt_out_notification_methods - .unwrap_or_default(), - ), - None => (false, Vec::new()), - }; - session.experimental_api_enabled = experimental_api_enabled; - session.opted_out_notification_methods = - opt_out_notification_methods.into_iter().collect(); - let ClientInfo { - name, - title: _title, - version, - } = params.client_info; - session.app_server_client_name = Some(name.clone()); - session.client_version = Some(version.clone()); - let originator = name.clone(); - if let Err(error) = set_default_originator(originator.clone()) { - match error { - SetOriginatorError::InvalidHeaderValue => { - let error = JSONRPCErrorError { - code: INVALID_REQUEST_ERROR_CODE, - message: format!( - "Invalid clientInfo.name: '{name}'. Must be a valid HTTP header value." - ), - data: None, - }; - self.outgoing - .send_error(connection_request_id.clone(), error) - .await; - return; - } - SetOriginatorError::AlreadyInitialized => { - // No-op. This is expected to happen if the originator is already set via env var. - // TODO(owen): Once we remove support for CODEX_INTERNAL_ORIGINATOR_OVERRIDE, - // this will be an unexpected state and we can return a JSON-RPC error indicating - // internal server error. - } - } - } - if self.config.features.enabled(Feature::GeneralAnalytics) { - self.analytics_events_client.track_initialize( - connection_id.0, - analytics_initialize_params, - originator, - self.rpc_transport, - ); - } - set_default_client_residency_requirement(self.config.enforce_residency.value()); - let user_agent_suffix = format!("{name}; {version}"); - if let Ok(mut suffix) = USER_AGENT_SUFFIX.lock() { - *suffix = Some(user_agent_suffix); - } - - let user_agent = get_codex_user_agent(); - let response = InitializeResponse { - user_agent, - codex_home: self.config.codex_home.clone(), - platform_family: std::env::consts::FAMILY.to_string(), - platform_os: std::env::consts::OS.to_string(), - }; - self.outgoing - .send_response(connection_request_id, response) - .await; - - session.initialized = true; - if let Some(outbound_initialized) = outbound_initialized { - // In-process clients can complete readiness immediately here. The - // websocket path defers this until lib.rs finishes transport-layer - // initialize handling for the specific connection. - outbound_initialized.store(true, Ordering::Release); - self.codex_message_processor - .connection_initialized(connection_id) - .await; - } + self.outgoing.send_error(connection_request_id, error).await; return; } - _ => { - if !session.initialized { - let error = JSONRPCErrorError { - code: INVALID_REQUEST_ERROR_CODE, - message: "Not initialized".to_string(), - data: None, - }; - self.outgoing.send_error(connection_request_id, error).await; - return; + + // TODO(maxj): Revisit capability scoping for `experimental_api_enabled`. + // Current behavior is per-connection. Reviewer feedback notes this can + // create odd cross-client behavior (for example dynamic tool calls on a + // shared thread when another connected client did not opt into + // experimental API). Proposed direction is instance-global first-write-wins + // with initialize-time mismatch rejection. + let analytics_initialize_params = params.clone(); + let (experimental_api_enabled, opt_out_notification_methods) = match params.capabilities + { + Some(capabilities) => ( + capabilities.experimental_api, + capabilities + .opt_out_notification_methods + .unwrap_or_default(), + ), + None => (false, Vec::new()), + }; + let ClientInfo { + name, + title: _title, + version, + } = params.client_info; + // Validate before committing; set_default_originator validates while + // mutating process-global metadata. + if HeaderValue::from_str(&name).is_err() { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!( + "Invalid clientInfo.name: '{name}'. Must be a valid HTTP header value." + ), + data: None, + }; + self.outgoing + .send_error(connection_request_id.clone(), error) + .await; + return; + } + let originator = name.clone(); + let user_agent_suffix = format!("{name}; {version}"); + let codex_home = self.config.codex_home.clone(); + if session + .initialize(InitializedConnectionSessionState { + experimental_api_enabled, + opted_out_notification_methods: opt_out_notification_methods + .into_iter() + .collect(), + app_server_client_name: name.clone(), + client_version: version, + }) + .is_err() + { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "Already initialized".to_string(), + data: None, + }; + self.outgoing.send_error(connection_request_id, error).await; + return; + } + + // Only the request that wins session initialization may mutate + // process-global client metadata. + if let Err(error) = set_default_originator(originator.clone()) { + match error { + SetOriginatorError::InvalidHeaderValue => { + tracing::warn!( + client_info_name = %name, + "validated clientInfo.name was rejected while setting originator" + ); + } + SetOriginatorError::AlreadyInitialized => { + // No-op. This is expected to happen if the originator is already set via env var. + // TODO(owen): Once we remove support for CODEX_INTERNAL_ORIGINATOR_OVERRIDE, + // this will be an unexpected state and we can return a JSON-RPC error indicating + // internal server error. + } } } + if self.config.features.enabled(Feature::GeneralAnalytics) { + self.analytics_events_client.track_initialize( + connection_id.0, + analytics_initialize_params, + originator, + self.rpc_transport, + ); + } + set_default_client_residency_requirement(self.config.enforce_residency.value()); + if let Ok(mut suffix) = USER_AGENT_SUFFIX.lock() { + *suffix = Some(user_agent_suffix); + } + + let user_agent = get_codex_user_agent(); + let response = InitializeResponse { + user_agent, + codex_home, + platform_family: std::env::consts::FAMILY.to_string(), + platform_os: std::env::consts::OS.to_string(), + }; + + self.outgoing + .send_response(connection_request_id, response) + .await; + + if let Some(outbound_initialized) = outbound_initialized { + // In-process clients can complete readiness immediately here. The + // websocket path defers this until lib.rs finishes transport-layer + // initialize handling for the specific connection. + outbound_initialized.store(true, Ordering::Release); + self.codex_message_processor + .connection_initialized(connection_id) + .await; + } + return; } + + self.dispatch_initialized_client_request( + connection_request_id, + codex_request, + session, + request_context, + ) + .await; + } + + async fn dispatch_initialized_client_request( + self: &Arc, + connection_request_id: ConnectionRequestId, + codex_request: ClientRequest, + session: Arc, + request_context: RequestContext, + ) { + if !session.initialized() { + let error = JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: "Not initialized".to_string(), + data: None, + }; + self.outgoing.send_error(connection_request_id, error).await; + return; + } + if let Some(reason) = codex_request.experimental_reason() - && !session.experimental_api_enabled + && !session.experimental_api_enabled() { let error = JSONRPCErrorError { code: INVALID_REQUEST_ERROR_CODE, @@ -676,6 +757,29 @@ impl MessageProcessor { ); } + let app_server_client_name = session.app_server_client_name().map(str::to_string); + let client_version = session.client_version().map(str::to_string); + Arc::clone(self) + .handle_initialized_client_request( + connection_request_id, + codex_request, + request_context, + app_server_client_name, + client_version, + ) + .await; + } + + async fn handle_initialized_client_request( + self: Arc, + connection_request_id: ConnectionRequestId, + codex_request: ClientRequest, + request_context: RequestContext, + app_server_client_name: Option, + client_version: Option, + ) { + let connection_id = connection_request_id.connection_id; + match codex_request { ClientRequest::ConfigRead { request_id, params } => { self.handle_config_read( @@ -847,8 +951,8 @@ impl MessageProcessor { .process_request( connection_id, other, - session.app_server_client_name.clone(), - session.client_version.clone(), + app_server_client_name, + client_version, request_context, ) .boxed() diff --git a/codex-rs/app-server/src/message_processor/tracing_tests.rs b/codex-rs/app-server/src/message_processor/tracing_tests.rs index d0fe22e9b0..c1bb995bab 100644 --- a/codex-rs/app-server/src/message_processor/tracing_tests.rs +++ b/codex-rs/app-server/src/message_processor/tracing_tests.rs @@ -109,9 +109,9 @@ fn tracing_test_guard() -> &'static tokio::sync::Mutex<()> { struct TracingHarness { _server: MockServer, _codex_home: TempDir, - processor: MessageProcessor, + processor: Arc, outgoing_rx: mpsc::Receiver, - session: ConnectionSessionState, + session: Arc, tracing: &'static TestTracing, } @@ -129,7 +129,7 @@ impl TracingHarness { _codex_home: codex_home, processor, outgoing_rx, - session: ConnectionSessionState::default(), + session: Arc::new(ConnectionSessionState::default()), tracing, }; @@ -152,7 +152,7 @@ impl TracingHarness { /*trace*/ None, ) .await; - assert!(harness.session.initialized); + assert!(harness.session.initialized()); Ok(harness) } @@ -182,7 +182,7 @@ impl TracingHarness { TEST_CONNECTION_ID, request, AppServerTransport::Stdio, - &mut self.session, + Arc::clone(&self.session), ) .await; read_response(&mut self.outgoing_rx, request_id).await @@ -230,14 +230,14 @@ async fn build_test_config(codex_home: &Path, server_uri: &str) -> Result, ) -> ( - MessageProcessor, + Arc, mpsc::Receiver, ) { let (outgoing_tx, outgoing_rx) = mpsc::channel(16); let outgoing = Arc::new(OutgoingMessageSender::new(outgoing_tx)); let auth_manager = AuthManager::shared_from_config(config.as_ref(), /*enable_codex_api_key_env*/ false); - let processor = MessageProcessor::new(MessageProcessorArgs { + let processor = Arc::new(MessageProcessor::new(MessageProcessorArgs { outgoing, arg0_paths: Arg0DispatchPaths::default(), config, @@ -252,7 +252,7 @@ fn build_test_processor( auth_manager, rpc_transport: AppServerRpcTransport::Stdio, remote_control_handle: None, - }); + })); (processor, outgoing_rx) } diff --git a/codex-rs/app-server/src/transport/mod.rs b/codex-rs/app-server/src/transport/mod.rs index 92383cb78f..44e2cb0f92 100644 --- a/codex-rs/app-server/src/transport/mod.rs +++ b/codex-rs/app-server/src/transport/mod.rs @@ -121,7 +121,7 @@ pub(crate) struct ConnectionState { pub(crate) outbound_initialized: Arc, pub(crate) outbound_experimental_api_enabled: Arc, pub(crate) outbound_opted_out_notification_methods: Arc>>, - pub(crate) session: ConnectionSessionState, + pub(crate) session: Arc, } impl ConnectionState { @@ -134,7 +134,7 @@ impl ConnectionState { outbound_initialized, outbound_experimental_api_enabled, outbound_opted_out_notification_methods, - session: ConnectionSessionState::default(), + session: Arc::new(ConnectionSessionState::default()), } } }