From 99f47d6e9a3546c14c43af99c7a58fa6bd130548 Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Thu, 15 Jan 2026 18:33:11 -0800 Subject: [PATCH 01/32] fix(mcp): include threadId in both content and structuredContent in CallToolResult (#9338) --- codex-rs/docs/codex_mcp_interface.md | 18 +++++++ codex-rs/mcp-server/src/codex_tool_config.rs | 47 +++++++++++++++++-- codex-rs/mcp-server/src/codex_tool_runner.rs | 25 ++++++---- codex-rs/mcp-server/src/message_processor.rs | 16 ++----- codex-rs/mcp-server/tests/suite/codex_tool.rs | 3 ++ 5 files changed, 86 insertions(+), 23 deletions(-) diff --git a/codex-rs/docs/codex_mcp_interface.md b/codex-rs/docs/codex_mcp_interface.md index edd5ac1b2d..10f81e59b9 100644 --- a/codex-rs/docs/codex_mcp_interface.md +++ b/codex-rs/docs/codex_mcp_interface.md @@ -105,6 +105,24 @@ While a conversation runs, the server sends notifications: Clients should render events and, when present, surface approval requests (see next section). +## Tool responses + +The `codex` and `codex-reply` tools return standard MCP `CallToolResult` payloads. For +compatibility with MCP clients that prefer `structuredContent`, Codex mirrors the +content blocks inside `structuredContent` alongside the `threadId`. + +Example: + +```json +{ + "content": [{ "type": "text", "text": "Hello from Codex" }], + "structuredContent": { + "threadId": "019bbed6-1e9e-7f31-984c-a05b65045719", + "content": "Hello from Codex" + } +} +``` + ## Approvals (server → client) When Codex needs approval to apply changes or run commands, the server issues JSON‑RPC requests to the client: diff --git a/codex-rs/mcp-server/src/codex_tool_config.rs b/codex-rs/mcp-server/src/codex_tool_config.rs index f5237bfc06..8131d7da52 100644 --- a/codex-rs/mcp-server/src/codex_tool_config.rs +++ b/codex-rs/mcp-server/src/codex_tool_config.rs @@ -8,6 +8,7 @@ use codex_protocol::config_types::SandboxMode; use codex_utils_json_to_toml::json_to_toml; use mcp_types::Tool; use mcp_types::ToolInputSchema; +use mcp_types::ToolOutputSchema; use schemars::JsonSchema; use schemars::r#gen::SchemaSettings; use serde::Deserialize; @@ -127,8 +128,7 @@ pub(crate) fn create_tool_for_codex_tool_call_param() -> Tool { name: "codex".to_string(), title: Some("Codex".to_string()), input_schema: tool_input_schema, - // TODO(mbolin): This should be defined. - output_schema: None, + output_schema: Some(codex_tool_output_schema()), description: Some( "Run a Codex session. Accepts configuration parameters matching the Codex Config struct.".to_string(), ), @@ -136,6 +136,17 @@ pub(crate) fn create_tool_for_codex_tool_call_param() -> Tool { } } +fn codex_tool_output_schema() -> ToolOutputSchema { + ToolOutputSchema { + properties: Some(serde_json::json!({ + "threadId": { "type": "string" }, + "content": { "type": "string" } + })), + required: Some(vec!["threadId".to_string(), "content".to_string()]), + r#type: "object".to_string(), + } +} + impl CodexToolCallParam { /// Returns the initial user prompt to start the Codex conversation and the /// effective Config object generated from the supplied parameters. @@ -239,7 +250,7 @@ pub(crate) fn create_tool_for_codex_tool_call_reply_param() -> Tool { name: "codex-reply".to_string(), title: Some("Codex Reply".to_string()), input_schema: tool_input_schema, - output_schema: None, + output_schema: Some(codex_tool_output_schema()), description: Some( "Continue a Codex conversation by providing the thread id and prompt.".to_string(), ), @@ -330,6 +341,21 @@ mod tests { "type": "object" }, "name": "codex", + "outputSchema": { + "properties": { + "content": { + "type": "string" + }, + "threadId": { + "type": "string" + } + }, + "required": [ + "threadId", + "content" + ], + "type": "object" + }, "title": "Codex" }); assert_eq!(expected_tool_json, tool_json); @@ -362,6 +388,21 @@ mod tests { "type": "object", }, "name": "codex-reply", + "outputSchema": { + "properties": { + "content": { + "type": "string" + }, + "threadId": { + "type": "string" + } + }, + "required": [ + "threadId", + "content" + ], + "type": "object" + }, "title": "Codex Reply", }); assert_eq!(expected_tool_json, tool_json); diff --git a/codex-rs/mcp-server/src/codex_tool_runner.rs b/codex-rs/mcp-server/src/codex_tool_runner.rs index 6aafdf6de9..531bf90d23 100644 --- a/codex-rs/mcp-server/src/codex_tool_runner.rs +++ b/codex-rs/mcp-server/src/codex_tool_runner.rs @@ -34,21 +34,27 @@ pub(crate) const INVALID_PARAMS_ERROR_CODE: i64 = -32602; /// To adhere to MCP `tools/call` response format, include the Codex /// `threadId` in the `structured_content` field of the response. -fn create_call_tool_result_with_thread_id( +/// Some MCP clients ignore `content` when `structuredContent` is present, so +/// mirror the text there as well. +pub(crate) fn create_call_tool_result_with_thread_id( thread_id: ThreadId, text: String, is_error: Option, ) -> CallToolResult { + let content_text = text; + let content = vec![ContentBlock::TextContent(TextContent { + r#type: "text".to_string(), + text: content_text.clone(), + annotations: None, + })]; + let structured_content = json!({ + "threadId": thread_id, + "content": content_text, + }); CallToolResult { - content: vec![ContentBlock::TextContent(TextContent { - r#type: "text".to_string(), - text, - annotations: None, - })], + content, is_error, - structured_content: Some(json!({ - "threadId": thread_id, - })), + structured_content: Some(structured_content), } } @@ -398,6 +404,7 @@ mod tests { result.structured_content, Some(json!({ "threadId": thread_id, + "content": "done", })) ); } diff --git a/codex-rs/mcp-server/src/message_processor.rs b/codex-rs/mcp-server/src/message_processor.rs index 33bad85d75..9d947cda3f 100644 --- a/codex-rs/mcp-server/src/message_processor.rs +++ b/codex-rs/mcp-server/src/message_processor.rs @@ -498,17 +498,11 @@ impl MessageProcessor { Ok(c) => c, Err(_) => { tracing::warn!("Session not found for thread_id: {thread_id}"); - let result = CallToolResult { - content: vec![ContentBlock::TextContent(TextContent { - r#type: "text".to_owned(), - text: format!("Session not found for thread_id: {thread_id}"), - annotations: None, - })], - is_error: Some(true), - structured_content: Some(json!({ - "threadId": thread_id, - })), - }; + let result = crate::codex_tool_runner::create_call_tool_result_with_thread_id( + thread_id, + format!("Session not found for thread_id: {thread_id}"), + Some(true), + ); outgoing.send_response(request_id, result).await; return; } diff --git a/codex-rs/mcp-server/tests/suite/codex_tool.rs b/codex-rs/mcp-server/tests/suite/codex_tool.rs index dfe3651205..31c451d24f 100644 --- a/codex-rs/mcp-server/tests/suite/codex_tool.rs +++ b/codex-rs/mcp-server/tests/suite/codex_tool.rs @@ -162,6 +162,7 @@ async fn shell_command_approval_triggers_elicitation() -> anyhow::Result<()> { ], "structuredContent": { "threadId": params.thread_id, + "content": "File created!" } }), }, @@ -323,6 +324,7 @@ async fn patch_approval_triggers_elicitation() -> anyhow::Result<()> { ], "structuredContent": { "threadId": params.thread_id, + "content": "Patch has been applied successfully!" } }), }, @@ -394,6 +396,7 @@ async fn codex_tool_passes_base_instructions() -> anyhow::Result<()> { .and_then(|v| v.get("threadId")) .and_then(serde_json::Value::as_str) .expect("codex tool response should include structuredContent.threadId"), + "content": "Enjoy!" } }) ); From 1fc72c647fd52e3e73d4309c3b568d4d5fe012b5 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Thu, 15 Jan 2026 19:48:11 -0800 Subject: [PATCH 02/32] Fix token estimate during compaction (#9337) This addresses #9287 --- codex-rs/core/src/context_manager/history.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs index 4ee2e252c4..2279beef20 100644 --- a/codex-rs/core/src/context_manager/history.rs +++ b/codex-rs/core/src/context_manager/history.rs @@ -97,7 +97,11 @@ impl ContextManager { } | ResponseItem::Compaction { encrypted_content: content, - } => estimate_reasoning_length(content.len()) as i64, + } => { + let reasoning_bytes = estimate_reasoning_length(content.len()); + i64::try_from(approx_tokens_from_byte_count(reasoning_bytes)) + .unwrap_or(i64::MAX) + } item => { let serialized = serde_json::to_string(item).unwrap_or_default(); i64::try_from(approx_token_count(&serialized)).unwrap_or(i64::MAX) From 0cce6ebd833da7be1d3e432d4231fe5eae10c740 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Fri, 16 Jan 2026 01:06:24 -0800 Subject: [PATCH 03/32] rename model turn to sampling request (#9336) We have two type of turns now: model and user turns. It's always confusing to refer to either. Model turn is basically a sampling request. --- codex-rs/core/src/codex.rs | 46 ++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 06000a345c..61fb48f697 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -2528,17 +2528,17 @@ fn errors_to_info(errors: &[SkillError]) -> Vec { .collect() } -/// Takes a user message as input and runs a loop where, at each turn, the model +/// Takes a user message as input and runs a loop where, at each sampling request, the model /// replies with either: /// /// - requested function calls /// - an assistant message /// /// While it is possible for the model to return multiple of these items in a -/// single turn, in practice, we generally one item per turn: +/// single sampling request, in practice, we generally one item per sampling request: /// /// - If the model requests a function call, we execute it and send the output -/// back to the model in the next turn. +/// back to the model in the next sampling request. /// - If the model sends only an assistant message, we record it in the /// conversation history and consider the turn complete. /// @@ -2611,13 +2611,13 @@ pub(crate) async fn run_turn( .collect::>(); // Construct the input that we will send to the model. - let turn_input: Vec = { + let sampling_request_input: Vec = { sess.record_conversation_items(&turn_context, &pending_input) .await; sess.clone_history().await.for_prompt() }; - let turn_input_messages = turn_input + let sampling_request_input_messages = sampling_request_input .iter() .filter_map(|item| match parse_turn_item(item) { Some(TurnItem::UserMessage(user_message)) => Some(user_message), @@ -2625,21 +2625,21 @@ pub(crate) async fn run_turn( }) .map(|user_message| user_message.message()) .collect::>(); - match run_model_turn( + match run_sampling_request( Arc::clone(&sess), Arc::clone(&turn_context), Arc::clone(&turn_diff_tracker), &mut client_session, - turn_input, + sampling_request_input, cancellation_token.child_token(), ) .await { - Ok(turn_output) => { - let TurnRunResult { + Ok(sampling_request_output) => { + let SamplingRequestResult { needs_follow_up, - last_agent_message: turn_last_agent_message, - } = turn_output; + last_agent_message: sampling_request_last_agent_message, + } = sampling_request_output; let total_usage_tokens = sess.get_total_token_usage().await; let token_limit_reached = total_usage_tokens >= auto_compact_limit; @@ -2650,13 +2650,13 @@ pub(crate) async fn run_turn( } if !needs_follow_up { - last_agent_message = turn_last_agent_message; + last_agent_message = sampling_request_last_agent_message; sess.notifier() .notify(&UserNotification::AgentTurnComplete { thread_id: sess.conversation_id.to_string(), turn_id: turn_context.sub_id.clone(), cwd: turn_context.cwd.display().to_string(), - input_messages: turn_input_messages, + input_messages: sampling_request_input_messages, last_assistant_message: last_agent_message.clone(), }); break; @@ -2712,14 +2712,14 @@ async fn run_auto_compact(sess: &Arc, turn_context: &Arc) cwd = %turn_context.cwd.display() ) )] -async fn run_model_turn( +async fn run_sampling_request( sess: Arc, turn_context: Arc, turn_diff_tracker: SharedTurnDiffTracker, client_session: &mut ModelClientSession, input: Vec, cancellation_token: CancellationToken, -) -> CodexResult { +) -> CodexResult { let mcp_tools = sess .services .mcp_connection_manager @@ -2753,7 +2753,7 @@ async fn run_model_turn( let mut retries = 0; loop { - let err = match try_run_turn( + let err = match try_run_sampling_request( Arc::clone(&router), Arc::clone(&sess), Arc::clone(&turn_context), @@ -2793,7 +2793,9 @@ async fn run_model_turn( } _ => backoff(retries), }; - warn!("stream disconnected - retrying turn ({retries}/{max_retries} in {delay:?})...",); + warn!( + "stream disconnected - retrying sampling request ({retries}/{max_retries} in {delay:?})...", + ); // Surface retry information to any UI/front‑end so the // user understands what is happening instead of staring @@ -2813,7 +2815,7 @@ async fn run_model_turn( } #[derive(Debug)] -struct TurnRunResult { +struct SamplingRequestResult { needs_follow_up: bool, last_agent_message: Option, } @@ -2845,7 +2847,7 @@ async fn drain_in_flight( model = %turn_context.client.get_model() ) )] -async fn try_run_turn( +async fn try_run_sampling_request( router: Arc, sess: Arc, turn_context: Arc, @@ -2853,7 +2855,7 @@ async fn try_run_turn( turn_diff_tracker: SharedTurnDiffTracker, prompt: &Prompt, cancellation_token: CancellationToken, -) -> CodexResult { +) -> CodexResult { let rollout_item = RolloutItem::TurnContext(TurnContextItem { cwd: turn_context.cwd.clone(), approval_policy: turn_context.approval_policy, @@ -2897,7 +2899,7 @@ async fn try_run_turn( let mut active_item: Option = None; let mut should_emit_turn_diff = false; let receiving_span = trace_span!("receiving_stream"); - let outcome: CodexResult = loop { + let outcome: CodexResult = loop { let handle_responses = trace_span!( parent: &receiving_span, "handle_responses", @@ -2983,7 +2985,7 @@ async fn try_run_turn( needs_follow_up |= sess.has_pending_input().await; - break Ok(TurnRunResult { + break Ok(SamplingRequestResult { needs_follow_up, last_agent_message, }); From f5b3e738fbaa742a38071e367c18b1626837c25e Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 16 Jan 2026 11:23:01 +0100 Subject: [PATCH 04/32] feat: propagate approval request of unsubscribed threads (#9232) A thread can now be spawned by another thread. In order to process the approval requests of such sub-threads, we need to detect those event and show them in the TUI. This is a temporary solution while the UX is being figured out. This PR should be reverted once done --- codex-rs/tui/src/app.rs | 189 +++++++++++++++++++++++++++++++--- codex-rs/tui/src/app_event.rs | 5 + 2 files changed, 180 insertions(+), 14 deletions(-) diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index f1d6ca5da4..87d76909de 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -15,6 +15,8 @@ use crate::external_editor; use crate::file_search::FileSearchManager; use crate::history_cell; use crate::history_cell::HistoryCell; +#[cfg(not(debug_assertions))] +use crate::history_cell::UpdateAvailableHistoryCell; use crate::model_migration::ModelMigrationOutcome; use crate::model_migration::migration_copy_for_models; use crate::model_migration::run_model_migration_prompt; @@ -37,6 +39,7 @@ use codex_core::models_manager::manager::RefreshStrategy; use codex_core::models_manager::model_presets::HIDE_GPT_5_1_CODEX_MAX_MIGRATION_PROMPT_CONFIG; use codex_core::models_manager::model_presets::HIDE_GPT5_1_MIGRATION_PROMPT_CONFIG; use codex_core::protocol::DeprecationNoticeEvent; +use codex_core::protocol::Event; use codex_core::protocol::EventMsg; use codex_core::protocol::FinalOutput; use codex_core::protocol::ListSkillsResponseEvent; @@ -58,6 +61,8 @@ use ratatui::text::Line; use ratatui::widgets::Paragraph; use ratatui::widgets::Wrap; use std::collections::BTreeMap; +use std::collections::HashMap; +use std::collections::VecDeque; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; @@ -66,11 +71,9 @@ use std::sync::atomic::Ordering; use std::thread; use std::time::Duration; use tokio::select; +use tokio::sync::broadcast; use tokio::sync::mpsc::unbounded_channel; -#[cfg(not(debug_assertions))] -use crate::history_cell::UpdateAvailableHistoryCell; - const EXTERNAL_EDITOR_HINT: &str = "Save and close external editor to continue."; #[derive(Debug, Clone)] @@ -347,6 +350,13 @@ pub(crate) struct App { // One-shot suppression of the next world-writable scan after user confirmation. skip_world_writable_scan_once: bool, + + // TODO(jif) drop once new UX is here. + // Track external agent approvals spawned via AgentControl. + /// Map routed approval IDs to their originating external threads and original IDs. + external_approval_routes: HashMap, + /// Buffered Codex events while external approvals are pending. + paused_codex_events: VecDeque, } impl App { @@ -496,6 +506,8 @@ impl App { pending_update_action: None, suppress_shutdown_complete: false, skip_world_writable_scan_once: false, + external_approval_routes: HashMap::new(), + paused_codex_events: VecDeque::new(), }; // On startup, if Agent mode (workspace-write) or ReadOnly is active, warn about world-writable dirs on Windows. @@ -548,6 +560,9 @@ impl App { tui.frame_requester().schedule_frame(); + let mut thread_created_rx = thread_manager.subscribe_thread_created(); + let mut listen_for_threads = true; + let exit_reason = loop { let control = select! { Some(event) = app_event_rx.recv() => { @@ -556,6 +571,21 @@ impl App { Some(event) = tui_events.next() => { app.handle_tui_event(tui, event).await? } + // Listen on new thread creation due to collab tools. + created = thread_created_rx.recv(), if listen_for_threads => { + match created { + Ok(thread_id) => { + app.handle_thread_created(thread_id).await?; + } + Err(broadcast::error::RecvError::Lagged(_)) => { + tracing::warn!("thread_created receiver lagged; skipping resync"); + } + Err(broadcast::error::RecvError::Closed) => { + listen_for_threads = false; + } + } + AppRunControl::Continue + } }; match control { AppRunControl::Continue => {} @@ -846,18 +876,15 @@ impl App { self.chat_widget.on_commit_tick(); } AppEvent::CodexEvent(event) => { - if self.suppress_shutdown_complete - && matches!(event.msg, EventMsg::ShutdownComplete) - { - self.suppress_shutdown_complete = false; + if !self.external_approval_routes.is_empty() { + // Store the events while the approval is pending. + self.paused_codex_events.push_back(event); return Ok(AppRunControl::Continue); } - if let EventMsg::ListSkillsResponse(response) = &event.msg { - let cwd = self.chat_widget.config_ref().cwd.clone(); - let errors = errors_for_cwd(&cwd, response); - emit_skill_load_warnings(&self.app_event_tx, &errors); - } - self.chat_widget.handle_codex_event(event); + self.handle_codex_event_now(event); + } + AppEvent::ExternalApprovalRequest { thread_id, event } => { + self.handle_external_approval_request(thread_id, event); } AppEvent::Exit(mode) => match mode { ExitMode::ShutdownFirst => self.chat_widget.submit_op(Op::Shutdown), @@ -868,7 +895,54 @@ impl App { AppEvent::FatalExitRequest(message) => { return Ok(AppRunControl::Exit(ExitReason::Fatal(message))); } - AppEvent::CodexOp(op) => self.chat_widget.submit_op(op), + AppEvent::CodexOp(op) => match op { + // Catch potential approvals coming from an external thread and treat them + // directly. This support both command and patch approval. In such case + // the approval get transferred to the corresponding thread and the external + // approval map (`external_approval_routes`) is updated. + Op::ExecApproval { id, decision } => { + if let Some((thread_id, original_id)) = + self.external_approval_routes.remove(&id) + { + // Approval of a sub-agent. + self.forward_external_op( + thread_id, + Op::ExecApproval { + id: original_id, + decision, + }, + ) + .await; + self.finish_external_approval(); + } else { + // This is an approval but not external. + self.chat_widget + .submit_op(Op::ExecApproval { id, decision }); + } + } + Op::PatchApproval { id, decision } => { + if let Some((thread_id, original_id)) = + self.external_approval_routes.remove(&id) + { + // Approval of a sub-agent. + self.forward_external_op( + thread_id, + Op::PatchApproval { + id: original_id, + decision, + }, + ) + .await; + self.finish_external_approval(); + } else { + // This is an approval but not external. + self.chat_widget + .submit_op(Op::PatchApproval { id, decision }); + } + } + // Standard path where this is not an external approval response. + _ => self.chat_widget.submit_op(op), + }, AppEvent::DiffResult(text) => { // Clear the in-progress state in the bottom pane self.chat_widget.on_diff_complete(); @@ -1343,6 +1417,89 @@ impl App { Ok(AppRunControl::Continue) } + fn handle_codex_event_now(&mut self, event: Event) { + if self.suppress_shutdown_complete && matches!(event.msg, EventMsg::ShutdownComplete) { + self.suppress_shutdown_complete = false; + return; + } + if let EventMsg::ListSkillsResponse(response) = &event.msg { + let cwd = self.chat_widget.config_ref().cwd.clone(); + let errors = errors_for_cwd(&cwd, response); + emit_skill_load_warnings(&self.app_event_tx, &errors); + } + self.chat_widget.handle_codex_event(event); + } + + /// Routes external approval request events through the chat widget by + /// rewriting the event id to include the originating thread. + /// + /// `thread_id` is the external thread that issued the approval request. + /// `event` is the approval request event whose id is rewritten so replies + /// can be routed back to the correct thread. + fn handle_external_approval_request(&mut self, thread_id: ThreadId, mut event: Event) { + match &mut event.msg { + EventMsg::ExecApprovalRequest(_) | EventMsg::ApplyPatchApprovalRequest(_) => { + let original_id = event.id.clone(); + let routing_id = format!("{thread_id}:{original_id}"); + self.external_approval_routes + .insert(routing_id.clone(), (thread_id, original_id)); + event.id = routing_id; + } + _ => return, + } + self.chat_widget.handle_codex_event(event); + } + + async fn forward_external_op(&self, thread_id: ThreadId, op: Op) { + let thread = match self.server.get_thread(thread_id).await { + Ok(thread) => thread, + Err(err) => { + tracing::warn!("failed to find thread {thread_id} for approval response: {err}"); + return; + } + }; + if let Err(err) = thread.submit(op).await { + tracing::warn!("failed to submit approval response to thread {thread_id}: {err}"); + } + } + + fn finish_external_approval(&mut self) { + if self.external_approval_routes.is_empty() { + while let Some(event) = self.paused_codex_events.pop_front() { + self.handle_codex_event_now(event); + } + } + } + + async fn handle_thread_created(&mut self, thread_id: ThreadId) -> Result<()> { + let thread = match self.server.get_thread(thread_id).await { + Ok(thread) => thread, + Err(err) => { + tracing::warn!("failed to attach listener for thread {thread_id}: {err}"); + return Ok(()); + } + }; + let app_event_tx = self.app_event_tx.clone(); + tokio::spawn(async move { + loop { + let event = match thread.next_event().await { + Ok(event) => event, + Err(err) => { + tracing::debug!("external thread {thread_id} listener stopped: {err}"); + break; + } + }; + match event.msg { + EventMsg::ExecApprovalRequest(_) | EventMsg::ApplyPatchApprovalRequest(_) => { + app_event_tx.send(AppEvent::ExternalApprovalRequest { thread_id, event }); + } + _ => {} + } + } + }); + Ok(()) + } + fn reasoning_label(reasoning_effort: Option) -> &'static str { match reasoning_effort { Some(ReasoningEffortConfig::Minimal) => "minimal", @@ -1598,6 +1755,8 @@ mod tests { pending_update_action: None, suppress_shutdown_complete: false, skip_world_writable_scan_once: false, + external_approval_routes: HashMap::new(), + paused_codex_events: VecDeque::new(), } } @@ -1638,6 +1797,8 @@ mod tests { pending_update_action: None, suppress_shutdown_complete: false, skip_world_writable_scan_once: false, + external_approval_routes: HashMap::new(), + paused_codex_events: VecDeque::new(), }, rx, op_rx, diff --git a/codex-rs/tui/src/app_event.rs b/codex-rs/tui/src/app_event.rs index 6ce5d2cae5..785f3ae2ce 100644 --- a/codex-rs/tui/src/app_event.rs +++ b/codex-rs/tui/src/app_event.rs @@ -14,6 +14,7 @@ use codex_common::approval_presets::ApprovalPreset; use codex_core::protocol::Event; use codex_core::protocol::RateLimitSnapshot; use codex_file_search::FileMatch; +use codex_protocol::ThreadId; use codex_protocol::openai_models::ModelPreset; use crate::bottom_pane::ApprovalRequest; @@ -41,6 +42,10 @@ pub(crate) enum WindowsSandboxFallbackReason { #[derive(Debug)] pub(crate) enum AppEvent { CodexEvent(Event), + ExternalApprovalRequest { + thread_id: ThreadId, + event: Event, + }, /// Start a new session. NewSession, From c1ac5223e125b042205466a70cbd509166ebec57 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 16 Jan 2026 11:49:28 +0100 Subject: [PATCH 05/32] feat: run user commands under user snapshot (#9357) The initial goal is for user snapshots to have access to aliases etc --- codex-rs/core/src/tasks/user_shell.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/codex-rs/core/src/tasks/user_shell.rs b/codex-rs/core/src/tasks/user_shell.rs index 2b7ae53d0c..508b16d484 100644 --- a/codex-rs/core/src/tasks/user_shell.rs +++ b/codex-rs/core/src/tasks/user_shell.rs @@ -27,6 +27,7 @@ use crate::sandboxing::ExecEnv; use crate::sandboxing::SandboxPermissions; use crate::state::TaskKind; use crate::tools::format_exec_output_str; +use crate::tools::runtimes::maybe_wrap_shell_lc_with_snapshot; use crate::user_shell_command::user_shell_command_record_item; use super::SessionTask; @@ -74,9 +75,9 @@ impl SessionTask for UserShellCommandTask { // allows commands that use shell features (pipes, &&, redirects, etc.). // We do not source rc files or otherwise reformat the script. let use_login_shell = true; - let command = session - .user_shell() - .derive_exec_args(&self.command, use_login_shell); + let session_shell = session.user_shell(); + let command = session_shell.derive_exec_args(&self.command, use_login_shell); + let command = maybe_wrap_shell_lc_with_snapshot(&command, session_shell.as_ref()); let call_id = Uuid::new_v4().to_string(); let raw_command = self.command.clone(); From c576756c81ee84036093e7abcb0f0660dd3c2914 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 16 Jan 2026 12:05:04 +0100 Subject: [PATCH 06/32] feat: collab wait multiple IDs (#9294) --- .../app-server-protocol/src/protocol/v2.rs | 8 +- .../app-server/src/bespoke_event_handling.rs | 92 ++++-- codex-rs/core/src/tools/handlers/collab.rs | 291 +++++++++++++----- codex-rs/core/src/tools/spec.rs | 13 +- codex-rs/protocol/src/protocol.rs | 10 +- codex-rs/tui/src/collab.rs | 10 +- codex-rs/tui2/src/collab.rs | 10 +- 7 files changed, 302 insertions(+), 132 deletions(-) diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index ac930e1f42..8655d89c41 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -1771,12 +1771,12 @@ pub enum ThreadItem { /// Thread ID of the agent issuing the collab request. sender_thread_id: String, /// Thread ID of the receiving agent, when applicable. In case of spawn operation, - /// this correspond to the newly spawned agent. - receiver_thread_id: Option, + /// this corresponds to the newly spawned agent. + receiver_thread_ids: Vec, /// Prompt text sent as part of the collab tool call, when available. prompt: Option, - /// Last known status of the target agent, when available. - agent_state: Option, + /// Last known status of the target agents, when available. + agents_states: HashMap, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index f14dd2ddca..0b2520098d 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -287,9 +287,9 @@ pub(crate) async fn apply_bespoke_event_handling( tool: CollabAgentTool::SpawnAgent, status: V2CollabToolCallStatus::InProgress, sender_thread_id: begin_event.sender_thread_id.to_string(), - receiver_thread_id: None, + receiver_thread_ids: Vec::new(), prompt: Some(begin_event.prompt), - agent_state: None, + agents_states: HashMap::new(), }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -301,19 +301,32 @@ pub(crate) async fn apply_bespoke_event_handling( .await; } EventMsg::CollabAgentSpawnEnd(end_event) => { - let status = if end_event.new_thread_id.is_some() { - V2CollabToolCallStatus::Completed - } else { - V2CollabToolCallStatus::Failed + let has_receiver = end_event.new_thread_id.is_some(); + let status = match &end_event.status { + codex_protocol::protocol::AgentStatus::Errored(_) + | codex_protocol::protocol::AgentStatus::NotFound => V2CollabToolCallStatus::Failed, + _ if has_receiver => V2CollabToolCallStatus::Completed, + _ => V2CollabToolCallStatus::Failed, + }; + let (receiver_thread_ids, agents_states) = match end_event.new_thread_id { + Some(id) => { + let receiver_id = id.to_string(); + let received_status = V2CollabAgentStatus::from(end_event.status.clone()); + ( + vec![receiver_id.clone()], + [(receiver_id, received_status)].into_iter().collect(), + ) + } + None => (Vec::new(), HashMap::new()), }; let item = ThreadItem::CollabAgentToolCall { id: end_event.call_id, tool: CollabAgentTool::SpawnAgent, status, sender_thread_id: end_event.sender_thread_id.to_string(), - receiver_thread_id: end_event.new_thread_id.map(|id| id.to_string()), + receiver_thread_ids, prompt: Some(end_event.prompt), - agent_state: Some(V2CollabAgentStatus::from(end_event.status)), + agents_states, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -325,14 +338,15 @@ pub(crate) async fn apply_bespoke_event_handling( .await; } EventMsg::CollabAgentInteractionBegin(begin_event) => { + let receiver_thread_ids = vec![begin_event.receiver_thread_id.to_string()]; let item = ThreadItem::CollabAgentToolCall { id: begin_event.call_id, tool: CollabAgentTool::SendInput, status: V2CollabToolCallStatus::InProgress, sender_thread_id: begin_event.sender_thread_id.to_string(), - receiver_thread_id: Some(begin_event.receiver_thread_id.to_string()), + receiver_thread_ids, prompt: Some(begin_event.prompt), - agent_state: None, + agents_states: HashMap::new(), }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -344,19 +358,21 @@ pub(crate) async fn apply_bespoke_event_handling( .await; } EventMsg::CollabAgentInteractionEnd(end_event) => { - let status = match end_event.status { + let status = match &end_event.status { codex_protocol::protocol::AgentStatus::Errored(_) | codex_protocol::protocol::AgentStatus::NotFound => V2CollabToolCallStatus::Failed, _ => V2CollabToolCallStatus::Completed, }; + let receiver_id = end_event.receiver_thread_id.to_string(); + let received_status = V2CollabAgentStatus::from(end_event.status); let item = ThreadItem::CollabAgentToolCall { id: end_event.call_id, tool: CollabAgentTool::SendInput, status, sender_thread_id: end_event.sender_thread_id.to_string(), - receiver_thread_id: Some(end_event.receiver_thread_id.to_string()), + receiver_thread_ids: vec![receiver_id.clone()], prompt: Some(end_event.prompt), - agent_state: Some(V2CollabAgentStatus::from(end_event.status)), + agents_states: [(receiver_id, received_status)].into_iter().collect(), }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -368,14 +384,19 @@ pub(crate) async fn apply_bespoke_event_handling( .await; } EventMsg::CollabWaitingBegin(begin_event) => { + let receiver_thread_ids = begin_event + .receiver_thread_ids + .iter() + .map(ToString::to_string) + .collect(); let item = ThreadItem::CollabAgentToolCall { id: begin_event.call_id, tool: CollabAgentTool::Wait, status: V2CollabToolCallStatus::InProgress, sender_thread_id: begin_event.sender_thread_id.to_string(), - receiver_thread_id: Some(begin_event.receiver_thread_id.to_string()), + receiver_thread_ids, prompt: None, - agent_state: None, + agents_states: HashMap::new(), }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -387,19 +408,31 @@ pub(crate) async fn apply_bespoke_event_handling( .await; } EventMsg::CollabWaitingEnd(end_event) => { - let status = match end_event.status { - codex_protocol::protocol::AgentStatus::Errored(_) - | codex_protocol::protocol::AgentStatus::NotFound => V2CollabToolCallStatus::Failed, - _ => V2CollabToolCallStatus::Completed, + let status = if end_event.statuses.values().any(|status| { + matches!( + status, + codex_protocol::protocol::AgentStatus::Errored(_) + | codex_protocol::protocol::AgentStatus::NotFound + ) + }) { + V2CollabToolCallStatus::Failed + } else { + V2CollabToolCallStatus::Completed }; + let receiver_thread_ids = end_event.statuses.keys().map(ToString::to_string).collect(); + let agents_states = end_event + .statuses + .iter() + .map(|(id, status)| (id.to_string(), V2CollabAgentStatus::from(status.clone()))) + .collect(); let item = ThreadItem::CollabAgentToolCall { id: end_event.call_id, tool: CollabAgentTool::Wait, status, sender_thread_id: end_event.sender_thread_id.to_string(), - receiver_thread_id: Some(end_event.receiver_thread_id.to_string()), + receiver_thread_ids, prompt: None, - agent_state: Some(V2CollabAgentStatus::from(end_event.status)), + agents_states, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -416,9 +449,9 @@ pub(crate) async fn apply_bespoke_event_handling( tool: CollabAgentTool::CloseAgent, status: V2CollabToolCallStatus::InProgress, sender_thread_id: begin_event.sender_thread_id.to_string(), - receiver_thread_id: Some(begin_event.receiver_thread_id.to_string()), + receiver_thread_ids: vec![begin_event.receiver_thread_id.to_string()], prompt: None, - agent_state: None, + agents_states: HashMap::new(), }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -430,19 +463,26 @@ pub(crate) async fn apply_bespoke_event_handling( .await; } EventMsg::CollabCloseEnd(end_event) => { - let status = match end_event.status { + let status = match &end_event.status { codex_protocol::protocol::AgentStatus::Errored(_) | codex_protocol::protocol::AgentStatus::NotFound => V2CollabToolCallStatus::Failed, _ => V2CollabToolCallStatus::Completed, }; + let receiver_id = end_event.receiver_thread_id.to_string(); + let agents_states = [( + receiver_id.clone(), + V2CollabAgentStatus::from(end_event.status), + )] + .into_iter() + .collect(); let item = ThreadItem::CollabAgentToolCall { id: end_event.call_id, tool: CollabAgentTool::CloseAgent, status, sender_thread_id: end_event.sender_thread_id.to_string(), - receiver_thread_id: Some(end_event.receiver_thread_id.to_string()), + receiver_thread_ids: vec![receiver_id], prompt: None, - agent_state: Some(V2CollabAgentStatus::from(end_event.status)), + agents_states, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), diff --git a/codex-rs/core/src/tools/handlers/collab.rs b/codex-rs/core/src/tools/handlers/collab.rs index df3f2901da..6117211757 100644 --- a/codex-rs/core/src/tools/handlers/collab.rs +++ b/codex-rs/core/src/tools/handlers/collab.rs @@ -254,20 +254,26 @@ mod send_input { mod wait { use super::*; use crate::agent::status::is_final; + use futures::FutureExt; + use futures::StreamExt; + use futures::stream::FuturesUnordered; + use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; + use tokio::sync::watch::Receiver; use tokio::time::Instant; + use tokio::time::timeout_at; #[derive(Debug, Deserialize)] struct WaitArgs { - id: String, + ids: Vec, timeout_ms: Option, } #[derive(Debug, Serialize)] struct WaitResult { - status: AgentStatus, + status: HashMap, timed_out: bool, } @@ -278,7 +284,16 @@ mod wait { arguments: String, ) -> Result { let args: WaitArgs = parse_arguments(&arguments)?; - let receiver_thread_id = agent_id(&args.id)?; + if args.ids.is_empty() { + return Err(FunctionCallError::RespondToModel( + "ids must be non-empty".to_owned(), + )); + } + let receiver_thread_ids = args + .ids + .iter() + .map(|id| agent_id(id)) + .collect::, _>>()?; // Validate timeout. let timeout_ms = args.timeout_ms.unwrap_or(DEFAULT_WAIT_TIMEOUT_MS); @@ -296,105 +311,131 @@ mod wait { &turn, CollabWaitingBeginEvent { sender_thread_id: session.conversation_id, - receiver_thread_id, + receiver_thread_ids: receiver_thread_ids.clone(), call_id: call_id.clone(), } .into(), ) .await; - let status_rx = match session - .services - .agent_control - .subscribe_status(receiver_thread_id) - .await - { - Ok(status_rx) => status_rx, - Err(err) => { - let status = session - .services - .agent_control - .get_status(receiver_thread_id) - .await; - session - .send_event( - &turn, - CollabWaitingEndEvent { - sender_thread_id: session.conversation_id, - receiver_thread_id, - call_id: call_id.clone(), - status, - } - .into(), - ) - .await; - return Err(collab_agent_error(receiver_thread_id, err)); + + let mut status_rxs = Vec::with_capacity(receiver_thread_ids.len()); + let mut initial_final_statuses = Vec::new(); + for id in &receiver_thread_ids { + match session.services.agent_control.subscribe_status(*id).await { + Ok(rx) => { + let status = rx.borrow().clone(); + if is_final(&status) { + initial_final_statuses.push((*id, status)); + } + status_rxs.push((*id, rx)); + } + Err(CodexErr::ThreadNotFound(_)) => { + initial_final_statuses.push((*id, AgentStatus::NotFound)); + } + Err(err) => { + let mut statuses = HashMap::with_capacity(1); + statuses.insert(*id, session.services.agent_control.get_status(*id).await); + session + .send_event( + &turn, + CollabWaitingEndEvent { + sender_thread_id: session.conversation_id, + call_id: call_id.clone(), + statuses, + } + .into(), + ) + .await; + return Err(collab_agent_error(*id, err)); + } } + } + + let statuses = if !initial_final_statuses.is_empty() { + initial_final_statuses + } else { + // Wait for the first agent to reach a final status. + let mut futures = FuturesUnordered::new(); + for (id, rx) in status_rxs.into_iter() { + let session = session.clone(); + futures.push(wait_for_final_status(session, id, rx)); + } + let mut results = Vec::new(); + let deadline = Instant::now() + Duration::from_millis(timeout_ms as u64); + loop { + match timeout_at(deadline, futures.next()).await { + Ok(Some(Some(result))) => { + results.push(result); + break; + } + Ok(Some(None)) => continue, + Ok(None) | Err(_) => break, + } + } + if !results.is_empty() { + // Drain the unlikely last elements to prevent race. + loop { + match futures.next().now_or_never() { + Some(Some(Some(result))) => results.push(result), + Some(Some(None)) => continue, + Some(None) | None => break, + } + } + } + results }; - let result = - wait_for_status(session.as_ref(), receiver_thread_id, timeout_ms, status_rx).await; + + // Convert payload. + let statuses_map = statuses.clone().into_iter().collect::>(); + let result = WaitResult { + status: statuses_map.clone(), + timed_out: statuses.is_empty(), + }; + + // Final event emission. session .send_event( &turn, CollabWaitingEndEvent { sender_thread_id: session.conversation_id, - receiver_thread_id, call_id, - status: result.status.clone(), + statuses: statuses_map, } .into(), ) .await; - if matches!(result.status, AgentStatus::NotFound) { - return Err(FunctionCallError::RespondToModel(format!( - "agent with id {receiver_thread_id} not found" - ))); - } - let content = serde_json::to_string(&result).map_err(|err| { FunctionCallError::Fatal(format!("failed to serialize wait result: {err}")) })?; - let success = !result.timed_out && !matches!(result.status, AgentStatus::Errored(_)); - Ok(ToolOutput::Function { content, - success: Some(success), + success: None, content_items: None, }) } - async fn wait_for_status( - session: &Session, - agent_id: ThreadId, - timeout_ms: i64, - mut status_rx: tokio::sync::watch::Receiver, - ) -> WaitResult { - // Get last known status. - let mut status = status_rx.borrow_and_update().clone(); - let deadline = Instant::now() + Duration::from_millis(timeout_ms as u64); + async fn wait_for_final_status( + session: Arc, + thread_id: ThreadId, + mut status_rx: Receiver, + ) -> Option<(ThreadId, AgentStatus)> { + let mut status = status_rx.borrow().clone(); + if is_final(&status) { + return Some((thread_id, status)); + } - let timed_out = loop { + loop { + if status_rx.changed().await.is_err() { + let latest = session.services.agent_control.get_status(thread_id).await; + return is_final(&latest).then_some((thread_id, latest)); + } + status = status_rx.borrow().clone(); if is_final(&status) { - break false; + return Some((thread_id, status)); } - - match timeout_at(deadline, status_rx.changed()).await { - Ok(Ok(())) => status = status_rx.borrow().clone(), - Ok(Err(_)) => { - let last_status = session.services.agent_control.get_status(agent_id).await; - if last_status != AgentStatus::NotFound { - // On-purpose we keep the last known status if the agent gets dropped. This - // event is not supposed to happen. - status = last_status; - } - break false; - } - Err(_) => break true, - } - }; - - WaitResult { status, timed_out } + } } } @@ -560,7 +601,9 @@ mod tests { use crate::turn_diff_tracker::TurnDiffTracker; use codex_protocol::ThreadId; use pretty_assertions::assert_eq; + use serde::Deserialize; use serde_json::json; + use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; @@ -772,6 +815,12 @@ mod tests { .expect("shutdown should submit"); } + #[derive(Debug, Deserialize, PartialEq, Eq)] + struct WaitResult { + status: HashMap, + timed_out: bool, + } + #[tokio::test] async fn wait_rejects_non_positive_timeout() { let (session, turn) = make_session_and_context().await; @@ -779,7 +828,10 @@ mod tests { Arc::new(session), Arc::new(turn), "wait", - function_payload(json!({"id": ThreadId::new().to_string(), "timeout_ms": 0})), + function_payload(json!({ + "ids": [ThreadId::new().to_string()], + "timeout_ms": 0 + })), ); let Err(err) = CollabHandler.handle(invocation).await else { panic!("non-positive timeout should be rejected"); @@ -797,7 +849,7 @@ mod tests { Arc::new(session), Arc::new(turn), "wait", - function_payload(json!({"id": "invalid"})), + function_payload(json!({"ids": ["invalid"]})), ); let Err(err) = CollabHandler.handle(invocation).await else { panic!("invalid id should be rejected"); @@ -808,6 +860,65 @@ mod tests { assert!(msg.starts_with("invalid agent id invalid:")); } + #[tokio::test] + async fn wait_rejects_empty_ids() { + let (session, turn) = make_session_and_context().await; + let invocation = invocation( + Arc::new(session), + Arc::new(turn), + "wait", + function_payload(json!({"ids": []})), + ); + let Err(err) = CollabHandler.handle(invocation).await else { + panic!("empty ids should be rejected"); + }; + assert_eq!( + err, + FunctionCallError::RespondToModel("ids must be non-empty".to_string()) + ); + } + + #[tokio::test] + async fn wait_returns_not_found_for_missing_agents() { + let (mut session, turn) = make_session_and_context().await; + let manager = thread_manager(); + session.services.agent_control = manager.agent_control(); + let id_a = ThreadId::new(); + let id_b = ThreadId::new(); + let invocation = invocation( + Arc::new(session), + Arc::new(turn), + "wait", + function_payload(json!({ + "ids": [id_a.to_string(), id_b.to_string()], + "timeout_ms": 1000 + })), + ); + let output = CollabHandler + .handle(invocation) + .await + .expect("wait should succeed"); + let ToolOutput::Function { + content, success, .. + } = output + else { + panic!("expected function output"); + }; + let result: WaitResult = + serde_json::from_str(&content).expect("wait result should be json"); + assert_eq!( + result, + WaitResult { + status: HashMap::from([ + (id_a, AgentStatus::NotFound), + (id_b, AgentStatus::NotFound), + ]), + timed_out: false + } + ); + assert_eq!(success, None); + } + #[tokio::test] async fn wait_times_out_when_status_is_not_final() { let (mut session, turn) = make_session_and_context().await; @@ -820,7 +931,10 @@ mod tests { Arc::new(session), Arc::new(turn), "wait", - function_payload(json!({"id": agent_id.to_string(), "timeout_ms": 10})), + function_payload(json!({ + "ids": [agent_id.to_string()], + "timeout_ms": 10 + })), ); let output = CollabHandler .handle(invocation) @@ -832,8 +946,16 @@ mod tests { else { panic!("expected function output"); }; - assert_eq!(content, r#"{"status":"pending_init","timed_out":true}"#); - assert_eq!(success, Some(false)); + let result: WaitResult = + serde_json::from_str(&content).expect("wait result should be json"); + assert_eq!( + result, + WaitResult { + status: HashMap::new(), + timed_out: true + } + ); + assert_eq!(success, None); let _ = thread .thread @@ -869,7 +991,10 @@ mod tests { Arc::new(session), Arc::new(turn), "wait", - function_payload(json!({"id": agent_id.to_string(), "timeout_ms": 1000})), + function_payload(json!({ + "ids": [agent_id.to_string()], + "timeout_ms": 1000 + })), ); let output = CollabHandler .handle(invocation) @@ -881,8 +1006,16 @@ mod tests { else { panic!("expected function output"); }; - assert_eq!(content, r#"{"status":"shutdown","timed_out":false}"#); - assert_eq!(success, Some(true)); + let result: WaitResult = + serde_json::from_str(&content).expect("wait result should be json"); + assert_eq!( + result, + WaitResult { + status: HashMap::from([(agent_id, AgentStatus::Shutdown)]), + timed_out: false + } + ); + assert_eq!(success, None); } #[tokio::test] diff --git a/codex-rs/core/src/tools/spec.rs b/codex-rs/core/src/tools/spec.rs index 5a35f2b648..9d272a5e6f 100644 --- a/codex-rs/core/src/tools/spec.rs +++ b/codex-rs/core/src/tools/spec.rs @@ -503,9 +503,10 @@ fn create_send_input_tool() -> ToolSpec { fn create_wait_tool() -> ToolSpec { let mut properties = BTreeMap::new(); properties.insert( - "id".to_string(), - JsonSchema::String { - description: Some("Identifier of the agent to wait on.".to_string()), + "ids".to_string(), + JsonSchema::Array { + items: Box::new(JsonSchema::String { description: None }), + description: Some("Identifiers of the agents to wait on.".to_string()), }, ); properties.insert( @@ -519,11 +520,13 @@ fn create_wait_tool() -> ToolSpec { ToolSpec::Function(ResponsesApiTool { name: "wait".to_string(), - description: "Wait for an agent and return its status.".to_string(), + description: + "Wait for agents and return their statuses. If no agent is done, no status get returned." + .to_string(), strict: false, parameters: JsonSchema::Object { properties, - required: Some(vec!["id".to_string()]), + required: Some(vec!["ids".to_string()]), additional_properties: Some(false.into()), }, }) diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index df1b523cbc..7ab9099ae8 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -2177,8 +2177,8 @@ pub struct CollabAgentInteractionEndEvent { pub struct CollabWaitingBeginEvent { /// Thread ID of the sender. pub sender_thread_id: ThreadId, - /// Thread ID of the receiver. - pub receiver_thread_id: ThreadId, + /// Thread ID of the receivers. + pub receiver_thread_ids: Vec, /// ID of the waiting call. pub call_id: String, } @@ -2187,12 +2187,10 @@ pub struct CollabWaitingBeginEvent { pub struct CollabWaitingEndEvent { /// Thread ID of the sender. pub sender_thread_id: ThreadId, - /// Thread ID of the receiver. - pub receiver_thread_id: ThreadId, /// ID of the waiting call. pub call_id: String, - /// Last known status of the receiver agent reported to the sender agent. - pub status: AgentStatus, + /// Last known status of the receiver agents reported to the sender agent. + pub statuses: HashMap, } #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, TS)] diff --git a/codex-rs/tui/src/collab.rs b/codex-rs/tui/src/collab.rs index 6b1042810c..5a1a18f63c 100644 --- a/codex-rs/tui/src/collab.rs +++ b/codex-rs/tui/src/collab.rs @@ -59,12 +59,12 @@ pub(crate) fn waiting_begin(ev: CollabWaitingBeginEvent) -> PlainHistoryCell { let CollabWaitingBeginEvent { call_id, sender_thread_id, - receiver_thread_id, + receiver_thread_ids, } = ev; let details = vec![ detail_line("call", call_id), detail_line("sender", sender_thread_id), - detail_line("receiver", receiver_thread_id), + detail_line("receiver", format!("{receiver_thread_ids:?}")), ]; collab_event("Collab wait begin", details) } @@ -73,14 +73,12 @@ pub(crate) fn waiting_end(ev: CollabWaitingEndEvent) -> PlainHistoryCell { let CollabWaitingEndEvent { call_id, sender_thread_id, - receiver_thread_id, - status, + statuses, } = ev; let details = vec![ detail_line("call", call_id), detail_line("sender", sender_thread_id), - detail_line("receiver", receiver_thread_id), - status_line(&status), + detail_line("statuses", format!("{statuses:#?}")), ]; collab_event("Collab wait end", details) } diff --git a/codex-rs/tui2/src/collab.rs b/codex-rs/tui2/src/collab.rs index 6b1042810c..5a1a18f63c 100644 --- a/codex-rs/tui2/src/collab.rs +++ b/codex-rs/tui2/src/collab.rs @@ -59,12 +59,12 @@ pub(crate) fn waiting_begin(ev: CollabWaitingBeginEvent) -> PlainHistoryCell { let CollabWaitingBeginEvent { call_id, sender_thread_id, - receiver_thread_id, + receiver_thread_ids, } = ev; let details = vec![ detail_line("call", call_id), detail_line("sender", sender_thread_id), - detail_line("receiver", receiver_thread_id), + detail_line("receiver", format!("{receiver_thread_ids:?}")), ]; collab_event("Collab wait begin", details) } @@ -73,14 +73,12 @@ pub(crate) fn waiting_end(ev: CollabWaitingEndEvent) -> PlainHistoryCell { let CollabWaitingEndEvent { call_id, sender_thread_id, - receiver_thread_id, - status, + statuses, } = ev; let details = vec![ detail_line("call", call_id), detail_line("sender", sender_thread_id), - detail_line("receiver", receiver_thread_id), - status_line(&status), + detail_line("statuses", format!("{statuses:#?}")), ]; collab_event("Collab wait end", details) } From 7fc49697dd1ac2fc13e24102fc376a854f9357b8 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 16 Jan 2026 14:52:16 +0100 Subject: [PATCH 07/32] feat: CODEX_CI (#9366) --- codex-rs/core/src/unified_exec/process_manager.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/codex-rs/core/src/unified_exec/process_manager.rs b/codex-rs/core/src/unified_exec/process_manager.rs index 851b92d24f..e51dc5e266 100644 --- a/codex-rs/core/src/unified_exec/process_manager.rs +++ b/codex-rs/core/src/unified_exec/process_manager.rs @@ -47,7 +47,7 @@ use crate::unified_exec::process::OutputHandles; use crate::unified_exec::process::UnifiedExecProcess; use crate::unified_exec::resolve_max_tokens; -const UNIFIED_EXEC_ENV: [(&str, &str); 9] = [ +const UNIFIED_EXEC_ENV: [(&str, &str); 10] = [ ("NO_COLOR", "1"), ("TERM", "dumb"), ("LANG", "C.UTF-8"), @@ -57,6 +57,7 @@ const UNIFIED_EXEC_ENV: [(&str, &str); 9] = [ ("PAGER", "cat"), ("GIT_PAGER", "cat"), ("GH_PAGER", "cat"), + ("CODEX_CI", "1"), ]; fn apply_unified_exec_env(mut env: HashMap) -> HashMap { @@ -688,6 +689,7 @@ mod tests { ("PAGER".to_string(), "cat".to_string()), ("GIT_PAGER".to_string(), "cat".to_string()), ("GH_PAGER".to_string(), "cat".to_string()), + ("CODEX_CI".to_string(), "1".to_string()), ]); assert_eq!(env, expected); From 7905e99d03027f17dff5df50cef0fb601fc10f93 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 16 Jan 2026 15:12:41 +0100 Subject: [PATCH 08/32] prompt collab (#9367) --- .../core/templates/agents/orchestrator.md | 79 +++++++++++++------ 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/codex-rs/core/templates/agents/orchestrator.md b/codex-rs/core/templates/agents/orchestrator.md index 89d98c9334..19be2a1afe 100644 --- a/codex-rs/core/templates/agents/orchestrator.md +++ b/codex-rs/core/templates/agents/orchestrator.md @@ -1,38 +1,71 @@ You are Codex Orchestrator, based on GPT-5. You are running as an orchestration agent in the Codex CLI on a user's computer. ## Role -- The interface between the user and the workers. Your role is to understand a problem and then delegate/coordinate workers to solve the task. -- Monitor progress, resolve conflicts, and integrate results into a single, coherent outcome. -- You can perform basic actions such as code exploration or running basic commands if needed to understand the problem, but you must delegate the hard work to workers. -- If a task can be split in well scoped sub-tasks, use multiple workers to solve it, and you take care of the global orchestration. -- Your job is not finished before the entire task is completed. While this is not the case, keep monitoring and coordinating your workers. -- Do not rush the workers. If they are working, let them work and don't ask them to "finalize now" unless requested by the user. + +* You are the interface between the user and the workers. +* Your job is to understand the task, decompose it, and delegate well-scoped work to workers. +* You coordinate execution, monitor progress, resolve conflicts, and integrate results into a single coherent outcome. +* You may perform lightweight actions (e.g. reading files, basic commands) to understand the task, but all substantive work must be delegated to workers. +* **Your job is not finished until the entire task is fully completed and verified.** +* While the task is incomplete, you must keep monitoring and coordinating workers. You must not return early. + +## Core invariants + +* **Never stop monitoring workers.** +* **Do not rush workers. Be patient.** +* The orchestrator must not return unless the task is fully accomplished. + +## Worker execution semantics + +* While a worker is running, you cannot observe intermediate state. +* Messages sent with `send_input` are queued and processed only after the worker finishes, unless interrupted. +* Therefore: + * Do not send messages to “check status” or “ask for progress” unless being asked. + * Monitoring happens exclusively via `wait`. + * Sending a message is a commitment for the *next* phase of work. + +## Interrupt semantics + +* If a worker is taking longer than expected but is still working, do nothing and keep waiting unless being asked. +* Only intervene if you must change, stop, or redirect the *current* work. +* To stop a worker’s current task, you **must** use `send_input(interrupt=true)`. +* Use `interrupt=true` sparingly and deliberately. ## Multi-agent workflow -1. Understand the request and identify the optimal set of workers needed. +1. Understand the request and determine the optimal set of workers. If the task can be divided into sub-tasks, spawn one worker per sub-task and make them work together. 2. Spawn worker(s) with precise goals, constraints, and expected deliverables. -3. Monitor workers with `wait`, route questions via `send_input`, and keep scope boundaries clear. -4. When all workers report done, verify their work to make sure the task was correctly solved. -5. If you spot issues, assign fixes to the relevant worker(s) and repeat steps 3–5 until the task is correctly completed. -6. Close all agents when you don't need them anymore (i.e. when the task if fully finished). +3. Monitor workers using `wait`. +4. When a worker finishes: + * verify correctness, + * check integration with other work, + * assess whether the global task is closer to completion. +5. If issues remain, assign fixes to the appropriate worker(s) and repeat steps 3–5. +6. Close agents only when no further work is required from them. +7. Return to the user only when the task is fully completed and verified. ## Collaboration rules -- Tell every worker they are not alone in the environment and must not revert or overwrite others' work. -- Default: workers must not spawn sub-agents unless you explicitly allow it. -- When multiple workers are running, you can provide multiple ids to `wait` in order to wait for the first worker to finish. This will make your workflow event-based as the tool will return when the first agent is done (i.e. when you need to react on it). +* Workers operate in a shared environment. You must tell it to them. +* Workers must not revert, overwrite, or conflict with others’ work. +* By default, workers must not spawn sub-agents unless explicitly allowed. +* When multiple workers are active, you may pass multiple IDs to `wait` to react to the first completion and keep the workflow event-driven and use a long timeout (e.g. 5 minutes). ## Collab tools -- `spawn_agent`: create a worker with an initial prompt (set `agent_type`). -- `send_input`: send follow-ups, clarifications, or fix requests (`interrupt` can stop the current task first). -- `wait`: poll the completion status of a list of workers. Return once at least one worker in the list is done. -- `close_agent`: close the agent when done. +* `spawn_agent`: create a worker with an initial prompt (`agent_type` required). +* `send_input`: send follow-ups or fixes (queued unless interrupted). +* `send_input(interrupt=true)`: stop current work and redirect immediately. +* `wait`: wait for one or more workers; returns when at least one finishes. +* `close_agent`: close a worker when fully done. -## Presenting your work and final message +## Final response -- Keep responses concise, factual, and in plain text. -- Summarize: what was delegated, key outcomes, tests/verification status, and any remaining risks. -- If verification failed, state the issues clearly and what you asked workers to change. -- Do not dump large files; reference paths with backticks. +* Keep responses concise, factual, and in plain text. +* Summarize: + * what was delegated, + * key outcomes, + * verification performed, + * and any remaining risks. +* If verification failed, state issues clearly and describe what was reassigned. +* Do not dump large files inline; reference paths using backticks. From 1668ca726ff8cc5649710a20f297e16b1ec6420c Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 16 Jan 2026 15:54:32 +0100 Subject: [PATCH 09/32] chore: close pipe on non-pty processes (#9369) Closing the STDIN of piped process when starting them to avoid commands like `rg` to wait for content on STDIN and hangs for ever --- .../core/src/tools/handlers/unified_exec.rs | 2 +- codex-rs/core/src/unified_exec/errors.rs | 4 ++ codex-rs/core/src/unified_exec/mod.rs | 1 + .../core/src/unified_exec/process_manager.rs | 11 +++- codex-rs/core/tests/suite/unified_exec.rs | 5 ++ codex-rs/utils/pty/README.md | 2 + codex-rs/utils/pty/src/lib.rs | 2 + codex-rs/utils/pty/src/pipe.rs | 62 +++++++++++++++---- 8 files changed, 74 insertions(+), 15 deletions(-) diff --git a/codex-rs/core/src/tools/handlers/unified_exec.rs b/codex-rs/core/src/tools/handlers/unified_exec.rs index d90ed3b644..daa39bc358 100644 --- a/codex-rs/core/src/tools/handlers/unified_exec.rs +++ b/codex-rs/core/src/tools/handlers/unified_exec.rs @@ -202,7 +202,7 @@ impl ToolHandler for UnifiedExecHandler { }) .await .map_err(|err| { - FunctionCallError::RespondToModel(format!("write_stdin failed: {err:?}")) + FunctionCallError::RespondToModel(format!("write_stdin failed: {err}")) })?; let interaction = TerminalInteractionEvent { diff --git a/codex-rs/core/src/unified_exec/errors.rs b/codex-rs/core/src/unified_exec/errors.rs index d8df389252..284c7bca6d 100644 --- a/codex-rs/core/src/unified_exec/errors.rs +++ b/codex-rs/core/src/unified_exec/errors.rs @@ -10,6 +10,10 @@ pub(crate) enum UnifiedExecError { UnknownProcessId { process_id: String }, #[error("failed to write to stdin")] WriteToStdin, + #[error( + "stdin is closed for this session; rerun exec_command with tty=true to keep stdin open" + )] + StdinClosed, #[error("missing command line for unified exec request")] MissingCommandLine, #[error("Command denied by sandbox: {message}")] diff --git a/codex-rs/core/src/unified_exec/mod.rs b/codex-rs/core/src/unified_exec/mod.rs index 1db38b9905..42a08b571f 100644 --- a/codex-rs/core/src/unified_exec/mod.rs +++ b/codex-rs/core/src/unified_exec/mod.rs @@ -136,6 +136,7 @@ struct ProcessEntry { call_id: String, process_id: String, command: Vec, + tty: bool, last_used: tokio::time::Instant, } diff --git a/codex-rs/core/src/unified_exec/process_manager.rs b/codex-rs/core/src/unified_exec/process_manager.rs index e51dc5e266..3230e75b15 100644 --- a/codex-rs/core/src/unified_exec/process_manager.rs +++ b/codex-rs/core/src/unified_exec/process_manager.rs @@ -74,6 +74,7 @@ struct PreparedProcessHandles { cancellation_token: CancellationToken, command: Vec, process_id: String, + tty: bool, } impl UnifiedExecProcessManager { @@ -218,6 +219,7 @@ impl UnifiedExecProcessManager { cwd.clone(), start, process_id, + request.tty, Arc::clone(&transcript), ) .await; @@ -256,10 +258,14 @@ impl UnifiedExecProcessManager { cancellation_token, command: session_command, process_id, + tty, .. } = self.prepare_process_handles(process_id.as_str()).await?; if !request.input.is_empty() { + if !tty { + return Err(UnifiedExecError::StdinClosed); + } Self::send_input(&writer_tx, request.input.as_bytes()).await?; // Give the remote process a brief window to react so that we are // more likely to capture its output in the poll below. @@ -380,6 +386,7 @@ impl UnifiedExecProcessManager { cancellation_token, command: entry.command.clone(), process_id: entry.process_id.clone(), + tty: entry.tty, }) } @@ -402,6 +409,7 @@ impl UnifiedExecProcessManager { cwd: PathBuf, started_at: Instant, process_id: String, + tty: bool, transcript: Arc>, ) { let entry = ProcessEntry { @@ -409,6 +417,7 @@ impl UnifiedExecProcessManager { call_id: context.call_id.clone(), process_id: process_id.clone(), command: command.to_vec(), + tty, last_used: started_at, }; let number_processes = { @@ -461,7 +470,7 @@ impl UnifiedExecProcessManager { ) .await } else { - codex_utils_pty::pipe::spawn_process( + codex_utils_pty::pipe::spawn_process_no_stdin( program, args, env.cwd.as_path(), diff --git a/codex-rs/core/tests/suite/unified_exec.rs b/codex-rs/core/tests/suite/unified_exec.rs index 3a6c728df0..df72d249b3 100644 --- a/codex-rs/core/tests/suite/unified_exec.rs +++ b/codex-rs/core/tests/suite/unified_exec.rs @@ -805,6 +805,7 @@ async fn unified_exec_emits_terminal_interaction_for_write_stdin() -> Result<()> let open_args = json!({ "cmd": "/bin/bash -i", "yield_time_ms": 200, + "tty": true, }); let stdin_call_id = "uexec-stdin-delta"; @@ -905,6 +906,7 @@ async fn unified_exec_terminal_interaction_captures_delayed_output() -> Result<( let open_args = json!({ "cmd": "sleep 3 && echo MARKER1 && sleep 3 && echo MARKER2", "yield_time_ms": 10, + "tty": true, }); // Poll stdin three times: first for no output, second after the first marker, @@ -1966,6 +1968,7 @@ async fn unified_exec_reuses_session_via_stdin() -> Result<()> { let first_args = serde_json::json!({ "cmd": "/bin/cat", "yield_time_ms": 200, + "tty": true, }); let second_call_id = "uexec-stdin"; @@ -2678,6 +2681,7 @@ async fn unified_exec_prunes_exited_sessions_first() -> Result<()> { let keep_args = serde_json::json!({ "cmd": "/bin/cat", "yield_time_ms": 250, + "tty": true, }); let prune_call_id = "uexec-prune-target"; @@ -2685,6 +2689,7 @@ async fn unified_exec_prunes_exited_sessions_first() -> Result<()> { let prune_args = serde_json::json!({ "cmd": "sleep 1", "yield_time_ms": 1_250, + "tty": true, }); let mut events = vec![ev_response_created("resp-prune-1")]; diff --git a/codex-rs/utils/pty/README.md b/codex-rs/utils/pty/README.md index 22f2e3a89b..d0f77268a2 100644 --- a/codex-rs/utils/pty/README.md +++ b/codex-rs/utils/pty/README.md @@ -6,6 +6,7 @@ Lightweight helpers for spawning interactive processes either under a PTY (pseud - `spawn_pty_process(program, args, cwd, env, arg0)` → `SpawnedProcess` - `spawn_pipe_process(program, args, cwd, env, arg0)` → `SpawnedProcess` +- `spawn_pipe_process_no_stdin(program, args, cwd, env, arg0)` → `SpawnedProcess` - `conpty_supported()` → `bool` (Windows only; always true elsewhere) - `ProcessHandle` exposes: - `writer_sender()` → `mpsc::Sender>` (stdin) @@ -46,6 +47,7 @@ let exit_code = spawned.exit_rx.await.unwrap_or(-1); ``` Swap in `spawn_pipe_process` for a non-TTY subprocess; the rest of the API stays the same. +Use `spawn_pipe_process_no_stdin` to force stdin closed (commands that read stdin will see EOF immediately). ## Tests diff --git a/codex-rs/utils/pty/src/lib.rs b/codex-rs/utils/pty/src/lib.rs index 037b0d761c..590770e2ae 100644 --- a/codex-rs/utils/pty/src/lib.rs +++ b/codex-rs/utils/pty/src/lib.rs @@ -9,6 +9,8 @@ mod win; /// Spawn a non-interactive process using regular pipes for stdin/stdout/stderr. pub use pipe::spawn_process as spawn_pipe_process; +/// Spawn a non-interactive process using regular pipes, but close stdin immediately. +pub use pipe::spawn_process_no_stdin as spawn_pipe_process_no_stdin; /// Handle for interacting with a spawned process (PTY or pipe). pub use process::ProcessHandle; /// Bundle of process handles plus output and exit receivers returned by spawn helpers. diff --git a/codex-rs/utils/pty/src/pipe.rs b/codex-rs/utils/pty/src/pipe.rs index c3dcd4ddcb..5d9eb0232d 100644 --- a/codex-rs/utils/pty/src/pipe.rs +++ b/codex-rs/utils/pty/src/pipe.rs @@ -90,13 +90,19 @@ where } } -/// Spawn a process using regular pipes (no PTY), returning handles for stdin, output, and exit. -pub async fn spawn_process( +#[derive(Clone, Copy)] +enum PipeStdinMode { + Piped, + Null, +} + +async fn spawn_process_with_stdin_mode( program: &str, args: &[String], cwd: &Path, env: &HashMap, arg0: &Option, + stdin_mode: PipeStdinMode, ) -> Result { if program.is_empty() { anyhow::bail!("missing program for pipe spawn"); @@ -128,7 +134,14 @@ pub async fn spawn_process( for arg in args { command.arg(arg); } - command.stdin(Stdio::piped()); + match stdin_mode { + PipeStdinMode::Piped => { + command.stdin(Stdio::piped()); + } + PipeStdinMode::Null => { + command.stdin(Stdio::null()); + } + } command.stdout(Stdio::piped()); command.stderr(Stdio::piped()); @@ -147,18 +160,19 @@ pub async fn spawn_process( let (output_tx, _) = broadcast::channel::>(256); let initial_output_rx = output_tx.subscribe(); - let writer_handle = tokio::spawn({ - let writer = stdin.map(|w| Arc::new(tokio::sync::Mutex::new(w))); - async move { + let writer_handle = if let Some(stdin) = stdin { + let writer = Arc::new(tokio::sync::Mutex::new(stdin)); + tokio::spawn(async move { while let Some(bytes) = writer_rx.recv().await { - if let Some(writer) = &writer { - let mut guard = writer.lock().await; - let _ = guard.write_all(&bytes).await; - let _ = guard.flush().await; - } + let mut guard = writer.lock().await; + let _ = guard.write_all(&bytes).await; + let _ = guard.flush().await; } - } - }); + }) + } else { + drop(writer_rx); + tokio::spawn(async {}) + }; let stdout_handle = stdout.map(|stdout| { let output_tx = output_tx.clone(); @@ -230,3 +244,25 @@ pub async fn spawn_process( exit_rx, }) } + +/// Spawn a process using regular pipes (no PTY), returning handles for stdin, output, and exit. +pub async fn spawn_process( + program: &str, + args: &[String], + cwd: &Path, + env: &HashMap, + arg0: &Option, +) -> Result { + spawn_process_with_stdin_mode(program, args, cwd, env, arg0, PipeStdinMode::Piped).await +} + +/// Spawn a process using regular pipes, but close stdin immediately. +pub async fn spawn_process_no_stdin( + program: &str, + args: &[String], + cwd: &Path, + env: &HashMap, + arg0: &Option, +) -> Result { + spawn_process_with_stdin_mode(program, args, cwd, env, arg0, PipeStdinMode::Null).await +} From 2691e1ce218d483deae43ce04fbd10039b2278c0 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 16 Jan 2026 17:24:41 +0100 Subject: [PATCH 10/32] fix: flaky tests (#9373) --- codex-rs/utils/pty/src/tests.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/codex-rs/utils/pty/src/tests.rs b/codex-rs/utils/pty/src/tests.rs index cdf9f0824a..8c41d1dbde 100644 --- a/codex-rs/utils/pty/src/tests.rs +++ b/codex-rs/utils/pty/src/tests.rs @@ -163,9 +163,11 @@ async fn pipe_and_pty_share_interface() -> anyhow::Result<()> { spawn_pipe_process(&pipe_program, &pipe_args, Path::new("."), &env_map, &None).await?; let pty = spawn_pty_process(&pty_program, &pty_args, Path::new("."), &env_map, &None).await?; + let timeout_ms = if cfg!(windows) { 10_000 } else { 3_000 }; let (pipe_out, pipe_code) = - collect_output_until_exit(pipe.output_rx, pipe.exit_rx, 3_000).await; - let (pty_out, pty_code) = collect_output_until_exit(pty.output_rx, pty.exit_rx, 3_000).await; + collect_output_until_exit(pipe.output_rx, pipe.exit_rx, timeout_ms).await; + let (pty_out, pty_code) = + collect_output_until_exit(pty.output_rx, pty.exit_rx, timeout_ms).await; assert_eq!(pipe_code, 0); assert_eq!(pty_code, 0); From 131590066e171c78dfe493a7c492c7eef1bf0069 Mon Sep 17 00:00:00 2001 From: Matthew Zeng Date: Fri, 16 Jan 2026 08:35:03 -0800 Subject: [PATCH 11/32] [device-auth] Add device code auth as a standalone option when headless environment is detected. (#9333) --- codex-rs/cli/src/main.rs | 9 - codex-rs/tui/src/onboarding/auth.rs | 212 +++++++++++++----- .../tui/src/onboarding/onboarding_screen.rs | 6 +- codex-rs/tui2/src/onboarding/auth.rs | 212 +++++++++++++----- .../tui2/src/onboarding/onboarding_screen.rs | 6 +- 5 files changed, 308 insertions(+), 137 deletions(-) diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 1c82eae004..337be81e5d 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -14,11 +14,9 @@ use codex_cli::login::run_login_status; use codex_cli::login::run_login_with_api_key; use codex_cli::login::run_login_with_chatgpt; use codex_cli::login::run_login_with_device_code; -use codex_cli::login::run_login_with_device_code_fallback_to_browser; use codex_cli::login::run_logout; use codex_cloud_tasks::Cli as CloudTasksCli; use codex_common::CliConfigOverrides; -use codex_core::env::is_headless_environment; use codex_exec::Cli as ExecCli; use codex_exec::Command as ExecCommand; use codex_exec::ReviewArgs; @@ -600,13 +598,6 @@ async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<() } else if login_cli.with_api_key { let api_key = read_api_key_from_stdin(); run_login_with_api_key(login_cli.config_overrides, api_key).await; - } else if is_headless_environment() { - run_login_with_device_code_fallback_to_browser( - login_cli.config_overrides, - login_cli.issuer_base_url, - login_cli.client_id, - ) - .await; } else { run_login_with_chatgpt(login_cli.config_overrides).await; } diff --git a/codex-rs/tui/src/onboarding/auth.rs b/codex-rs/tui/src/onboarding/auth.rs index 430ef5f195..551fa9180e 100644 --- a/codex-rs/tui/src/onboarding/auth.rs +++ b/codex-rs/tui/src/onboarding/auth.rs @@ -5,7 +5,6 @@ use codex_core::auth::AuthCredentialsStoreMode; use codex_core::auth::CLIENT_ID; use codex_core::auth::login_with_api_key; use codex_core::auth::read_openai_api_key_from_env; -use codex_core::env::is_headless_environment; use codex_login::DeviceCode; use codex_login::ServerOptions; use codex_login::ShutdownHandle; @@ -59,6 +58,13 @@ pub(crate) enum SignInState { ApiKeyConfigured, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum SignInOption { + ChatGpt, + DeviceCode, + ApiKey, +} + const API_KEY_DISABLED_MESSAGE: &str = "API key login is disabled."; #[derive(Clone, Default)] @@ -96,42 +102,26 @@ impl KeyboardHandler for AuthModeWidget { match key_event.code { KeyCode::Up | KeyCode::Char('k') => { - if self.is_chatgpt_login_allowed() { - self.highlighted_mode = AuthMode::ChatGPT; - } + self.move_highlight(-1); } KeyCode::Down | KeyCode::Char('j') => { - if self.is_api_login_allowed() { - self.highlighted_mode = AuthMode::ApiKey; - } + self.move_highlight(1); } KeyCode::Char('1') => { - if self.is_chatgpt_login_allowed() { - self.start_chatgpt_login(); - } + self.select_option_by_index(0); } KeyCode::Char('2') => { - if self.is_api_login_allowed() { - self.start_api_key_entry(); - } else { - self.disallow_api_login(); - } + self.select_option_by_index(1); + } + KeyCode::Char('3') => { + self.select_option_by_index(2); } KeyCode::Enter => { let sign_in_state = { (*self.sign_in_state.read().unwrap()).clone() }; match sign_in_state { - SignInState::PickMode => match self.highlighted_mode { - AuthMode::ChatGPT if self.is_chatgpt_login_allowed() => { - self.start_chatgpt_login(); - } - AuthMode::ApiKey if self.is_api_login_allowed() => { - self.start_api_key_entry(); - } - AuthMode::ChatGPT => {} - AuthMode::ApiKey => { - self.disallow_api_login(); - } - }, + SignInState::PickMode => { + self.handle_sign_in_option(self.highlighted_mode); + } SignInState::ChatGptSuccessMessage => { *self.sign_in_state.write().unwrap() = SignInState::ChatGptSuccess; } @@ -170,7 +160,7 @@ impl KeyboardHandler for AuthModeWidget { #[derive(Clone)] pub(crate) struct AuthModeWidget { pub request_frame: FrameRequester, - pub highlighted_mode: AuthMode, + pub highlighted_mode: SignInOption, pub error: Option, pub sign_in_state: Arc>, pub codex_home: PathBuf, @@ -191,8 +181,75 @@ impl AuthModeWidget { !matches!(self.forced_login_method, Some(ForcedLoginMethod::Api)) } + fn displayed_sign_in_options(&self) -> Vec { + let mut options = vec![SignInOption::ChatGpt]; + if self.is_chatgpt_login_allowed() { + options.push(SignInOption::DeviceCode); + } + if self.is_api_login_allowed() { + options.push(SignInOption::ApiKey); + } + options + } + + fn selectable_sign_in_options(&self) -> Vec { + let mut options = Vec::new(); + if self.is_chatgpt_login_allowed() { + options.push(SignInOption::ChatGpt); + options.push(SignInOption::DeviceCode); + } + if self.is_api_login_allowed() { + options.push(SignInOption::ApiKey); + } + options + } + + fn move_highlight(&mut self, delta: isize) { + let options = self.selectable_sign_in_options(); + if options.is_empty() { + return; + } + + let current_index = options + .iter() + .position(|option| *option == self.highlighted_mode) + .unwrap_or(0); + let next_index = + (current_index as isize + delta).rem_euclid(options.len() as isize) as usize; + self.highlighted_mode = options[next_index]; + } + + fn select_option_by_index(&mut self, index: usize) { + let options = self.displayed_sign_in_options(); + if let Some(option) = options.get(index).copied() { + self.handle_sign_in_option(option); + } + } + + fn handle_sign_in_option(&mut self, option: SignInOption) { + match option { + SignInOption::ChatGpt => { + if self.is_chatgpt_login_allowed() { + self.start_chatgpt_login(); + } + } + SignInOption::DeviceCode => { + if self.is_chatgpt_login_allowed() { + self.start_device_code_login(); + } + } + SignInOption::ApiKey => { + if self.is_api_login_allowed() { + self.start_api_key_entry(); + } else { + self.disallow_api_login(); + } + } + } + } + fn disallow_api_login(&mut self) { - self.highlighted_mode = AuthMode::ChatGPT; + self.highlighted_mode = SignInOption::ChatGpt; self.error = Some(API_KEY_DISABLED_MESSAGE.to_string()); *self.sign_in_state.write().unwrap() = SignInState::PickMode; self.request_frame.schedule_frame(); @@ -212,7 +269,7 @@ impl AuthModeWidget { ]; let create_mode_item = |idx: usize, - selected_mode: AuthMode, + selected_mode: SignInOption, text: &str, description: &str| -> Vec> { @@ -221,11 +278,11 @@ impl AuthModeWidget { let line1 = if is_selected { Line::from(vec![ - format!("{} {}. ", caret, idx + 1).cyan().dim(), + format!("{caret} {index}. ", index = idx + 1).cyan().dim(), text.to_string().cyan(), ]) } else { - format!(" {}. {text}", idx + 1).into() + format!(" {index}. {text}", index = idx + 1).into() }; let line2 = if is_selected { @@ -242,27 +299,42 @@ impl AuthModeWidget { let chatgpt_description = if !self.is_chatgpt_login_allowed() { "ChatGPT login is disabled" - } else if is_headless_environment() { - "Uses device code login (headless environment detected)" } else { "Usage included with Plus, Pro, Team, and Enterprise plans" }; - lines.extend(create_mode_item( - 0, - AuthMode::ChatGPT, - "Sign in with ChatGPT", - chatgpt_description, - )); - lines.push("".into()); - if self.is_api_login_allowed() { - lines.extend(create_mode_item( - 1, - AuthMode::ApiKey, - "Provide your own API key", - "Pay for what you use", - )); + let device_code_description = "Sign in from another device with a one-time code"; + + for (idx, option) in self.displayed_sign_in_options().into_iter().enumerate() { + match option { + SignInOption::ChatGpt => { + lines.extend(create_mode_item( + idx, + option, + "Sign in with ChatGPT", + chatgpt_description, + )); + } + SignInOption::DeviceCode => { + lines.extend(create_mode_item( + idx, + option, + "Sign in with Device Code", + device_code_description, + )); + } + SignInOption::ApiKey => { + lines.extend(create_mode_item( + idx, + option, + "Provide your own API key", + "Pay for what you use", + )); + } + } lines.push("".into()); - } else { + } + + if !self.is_api_login_allowed() { lines.push( " API key login is disabled by this workspace. Sign in with ChatGPT to continue." .dim() @@ -309,9 +381,9 @@ impl AuthModeWidget { ])); lines.push("".into()); lines.push(Line::from(vec![ - " On a remote or headless machine? Use ".into(), - "codex login --device-auth".cyan(), - " instead".into(), + " On a remote or headless machine? Press Esc and choose ".into(), + "Sign in with Device Code".cyan(), + ".".into(), ])); lines.push("".into()); } @@ -588,13 +660,21 @@ impl AuthModeWidget { self.request_frame.schedule_frame(); } + fn handle_existing_chatgpt_login(&mut self) -> bool { + if matches!(self.login_status, LoginStatus::AuthMode(AuthMode::ChatGPT)) { + *self.sign_in_state.write().unwrap() = SignInState::ChatGptSuccess; + self.request_frame.schedule_frame(); + true + } else { + false + } + } + /// Kicks off the ChatGPT auth flow and keeps the UI state consistent with the attempt. fn start_chatgpt_login(&mut self) { // If we're already authenticated with ChatGPT, don't start a new login – // just proceed to the success message flow. - if matches!(self.login_status, LoginStatus::AuthMode(AuthMode::ChatGPT)) { - *self.sign_in_state.write().unwrap() = SignInState::ChatGptSuccess; - self.request_frame.schedule_frame(); + if self.handle_existing_chatgpt_login() { return; } @@ -606,11 +686,6 @@ impl AuthModeWidget { self.cli_auth_credentials_store_mode, ); - if is_headless_environment() { - headless_chatgpt_login::start_headless_chatgpt_login(self, opts); - return; - } - match run_login_server(opts) { Ok(child) => { let sign_in_state = self.sign_in_state.clone(); @@ -650,6 +725,21 @@ impl AuthModeWidget { } } } + + fn start_device_code_login(&mut self) { + if self.handle_existing_chatgpt_login() { + return; + } + + self.error = None; + let opts = ServerOptions::new( + self.codex_home.clone(), + CLIENT_ID.to_string(), + self.forced_chatgpt_workspace_id.clone(), + self.cli_auth_credentials_store_mode, + ); + headless_chatgpt_login::start_headless_chatgpt_login(self, opts); + } } impl StepStateProvider for AuthModeWidget { @@ -708,7 +798,7 @@ mod tests { let codex_home_path = codex_home.path().to_path_buf(); let widget = AuthModeWidget { request_frame: FrameRequester::test_dummy(), - highlighted_mode: AuthMode::ChatGPT, + highlighted_mode: SignInOption::ChatGpt, error: None, sign_in_state: Arc::new(RwLock::new(SignInState::PickMode)), codex_home: codex_home_path.clone(), diff --git a/codex-rs/tui/src/onboarding/onboarding_screen.rs b/codex-rs/tui/src/onboarding/onboarding_screen.rs index 14999b2229..7e076ae160 100644 --- a/codex-rs/tui/src/onboarding/onboarding_screen.rs +++ b/codex-rs/tui/src/onboarding/onboarding_screen.rs @@ -11,11 +11,11 @@ use ratatui::style::Color; use ratatui::widgets::Clear; use ratatui::widgets::WidgetRef; -use codex_app_server_protocol::AuthMode; use codex_protocol::config_types::ForcedLoginMethod; use crate::LoginStatus; use crate::onboarding::auth::AuthModeWidget; +use crate::onboarding::auth::SignInOption; use crate::onboarding::auth::SignInState; use crate::onboarding::trust_directory::TrustDirectorySelection; use crate::onboarding::trust_directory::TrustDirectoryWidget; @@ -92,8 +92,8 @@ impl OnboardingScreen { ))); if show_login_screen { let highlighted_mode = match forced_login_method { - Some(ForcedLoginMethod::Api) => AuthMode::ApiKey, - _ => AuthMode::ChatGPT, + Some(ForcedLoginMethod::Api) => SignInOption::ApiKey, + _ => SignInOption::ChatGpt, }; steps.push(Step::Auth(AuthModeWidget { request_frame: tui.frame_requester(), diff --git a/codex-rs/tui2/src/onboarding/auth.rs b/codex-rs/tui2/src/onboarding/auth.rs index 640fca14da..a1fb3dee12 100644 --- a/codex-rs/tui2/src/onboarding/auth.rs +++ b/codex-rs/tui2/src/onboarding/auth.rs @@ -5,7 +5,6 @@ use codex_core::auth::AuthCredentialsStoreMode; use codex_core::auth::CLIENT_ID; use codex_core::auth::login_with_api_key; use codex_core::auth::read_openai_api_key_from_env; -use codex_core::env::is_headless_environment; use codex_login::DeviceCode; use codex_login::ServerOptions; use codex_login::ShutdownHandle; @@ -59,6 +58,13 @@ pub(crate) enum SignInState { ApiKeyConfigured, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum SignInOption { + ChatGpt, + DeviceCode, + ApiKey, +} + const API_KEY_DISABLED_MESSAGE: &str = "API key login is disabled."; #[derive(Clone, Default)] @@ -96,42 +102,26 @@ impl KeyboardHandler for AuthModeWidget { match key_event.code { KeyCode::Up | KeyCode::Char('k') => { - if self.is_chatgpt_login_allowed() { - self.highlighted_mode = AuthMode::ChatGPT; - } + self.move_highlight(-1); } KeyCode::Down | KeyCode::Char('j') => { - if self.is_api_login_allowed() { - self.highlighted_mode = AuthMode::ApiKey; - } + self.move_highlight(1); } KeyCode::Char('1') => { - if self.is_chatgpt_login_allowed() { - self.start_chatgpt_login(); - } + self.select_option_by_index(0); } KeyCode::Char('2') => { - if self.is_api_login_allowed() { - self.start_api_key_entry(); - } else { - self.disallow_api_login(); - } + self.select_option_by_index(1); + } + KeyCode::Char('3') => { + self.select_option_by_index(2); } KeyCode::Enter => { let sign_in_state = { (*self.sign_in_state.read().unwrap()).clone() }; match sign_in_state { - SignInState::PickMode => match self.highlighted_mode { - AuthMode::ChatGPT if self.is_chatgpt_login_allowed() => { - self.start_chatgpt_login(); - } - AuthMode::ApiKey if self.is_api_login_allowed() => { - self.start_api_key_entry(); - } - AuthMode::ChatGPT => {} - AuthMode::ApiKey => { - self.disallow_api_login(); - } - }, + SignInState::PickMode => { + self.handle_sign_in_option(self.highlighted_mode); + } SignInState::ChatGptSuccessMessage => { *self.sign_in_state.write().unwrap() = SignInState::ChatGptSuccess; } @@ -170,7 +160,7 @@ impl KeyboardHandler for AuthModeWidget { #[derive(Clone)] pub(crate) struct AuthModeWidget { pub request_frame: FrameRequester, - pub highlighted_mode: AuthMode, + pub highlighted_mode: SignInOption, pub error: Option, pub sign_in_state: Arc>, pub codex_home: PathBuf, @@ -191,8 +181,75 @@ impl AuthModeWidget { !matches!(self.forced_login_method, Some(ForcedLoginMethod::Api)) } + fn displayed_sign_in_options(&self) -> Vec { + let mut options = vec![SignInOption::ChatGpt]; + if self.is_chatgpt_login_allowed() { + options.push(SignInOption::DeviceCode); + } + if self.is_api_login_allowed() { + options.push(SignInOption::ApiKey); + } + options + } + + fn selectable_sign_in_options(&self) -> Vec { + let mut options = Vec::new(); + if self.is_chatgpt_login_allowed() { + options.push(SignInOption::ChatGpt); + options.push(SignInOption::DeviceCode); + } + if self.is_api_login_allowed() { + options.push(SignInOption::ApiKey); + } + options + } + + fn move_highlight(&mut self, delta: isize) { + let options = self.selectable_sign_in_options(); + if options.is_empty() { + return; + } + + let current_index = options + .iter() + .position(|option| *option == self.highlighted_mode) + .unwrap_or(0); + let next_index = + (current_index as isize + delta).rem_euclid(options.len() as isize) as usize; + self.highlighted_mode = options[next_index]; + } + + fn select_option_by_index(&mut self, index: usize) { + let options = self.displayed_sign_in_options(); + if let Some(option) = options.get(index).copied() { + self.handle_sign_in_option(option); + } + } + + fn handle_sign_in_option(&mut self, option: SignInOption) { + match option { + SignInOption::ChatGpt => { + if self.is_chatgpt_login_allowed() { + self.start_chatgpt_login(); + } + } + SignInOption::DeviceCode => { + if self.is_chatgpt_login_allowed() { + self.start_device_code_login(); + } + } + SignInOption::ApiKey => { + if self.is_api_login_allowed() { + self.start_api_key_entry(); + } else { + self.disallow_api_login(); + } + } + } + } + fn disallow_api_login(&mut self) { - self.highlighted_mode = AuthMode::ChatGPT; + self.highlighted_mode = SignInOption::ChatGpt; self.error = Some(API_KEY_DISABLED_MESSAGE.to_string()); *self.sign_in_state.write().unwrap() = SignInState::PickMode; self.request_frame.schedule_frame(); @@ -212,7 +269,7 @@ impl AuthModeWidget { ]; let create_mode_item = |idx: usize, - selected_mode: AuthMode, + selected_mode: SignInOption, text: &str, description: &str| -> Vec> { @@ -221,11 +278,11 @@ impl AuthModeWidget { let line1 = if is_selected { Line::from(vec![ - format!("{} {}. ", caret, idx + 1).cyan().dim(), + format!("{caret} {index}. ", index = idx + 1).cyan().dim(), text.to_string().cyan(), ]) } else { - format!(" {}. {text}", idx + 1).into() + format!(" {index}. {text}", index = idx + 1).into() }; let line2 = if is_selected { @@ -242,27 +299,42 @@ impl AuthModeWidget { let chatgpt_description = if !self.is_chatgpt_login_allowed() { "ChatGPT login is disabled" - } else if is_headless_environment() { - "Uses device code login (headless environment detected)" } else { "Usage included with Plus, Pro, Team, and Enterprise plans" }; - lines.extend(create_mode_item( - 0, - AuthMode::ChatGPT, - "Sign in with ChatGPT", - chatgpt_description, - )); - lines.push("".into()); - if self.is_api_login_allowed() { - lines.extend(create_mode_item( - 1, - AuthMode::ApiKey, - "Provide your own API key", - "Pay for what you use", - )); + let device_code_description = "Sign in from another device with a one-time code"; + + for (idx, option) in self.displayed_sign_in_options().into_iter().enumerate() { + match option { + SignInOption::ChatGpt => { + lines.extend(create_mode_item( + idx, + option, + "Sign in with ChatGPT", + chatgpt_description, + )); + } + SignInOption::DeviceCode => { + lines.extend(create_mode_item( + idx, + option, + "Sign in with Device Code", + device_code_description, + )); + } + SignInOption::ApiKey => { + lines.extend(create_mode_item( + idx, + option, + "Provide your own API key", + "Pay for what you use", + )); + } + } lines.push("".into()); - } else { + } + + if !self.is_api_login_allowed() { lines.push( " API key login is disabled by this workspace. Sign in with ChatGPT to continue." .dim() @@ -309,9 +381,9 @@ impl AuthModeWidget { ])); lines.push("".into()); lines.push(Line::from(vec![ - " On a remote or headless machine? Use ".into(), - "codex login --device-auth".cyan(), - " instead".into(), + " On a remote or headless machine? Press Esc and choose ".into(), + "Sign in with Device Code".cyan(), + ".".into(), ])); lines.push("".into()); } @@ -588,12 +660,20 @@ impl AuthModeWidget { self.request_frame.schedule_frame(); } - fn start_chatgpt_login(&mut self) { - // If we're already authenticated with ChatGPT, don't start a new login – - // just proceed to the success message flow. + fn handle_existing_chatgpt_login(&mut self) -> bool { if matches!(self.login_status, LoginStatus::AuthMode(AuthMode::ChatGPT)) { *self.sign_in_state.write().unwrap() = SignInState::ChatGptSuccess; self.request_frame.schedule_frame(); + true + } else { + false + } + } + + fn start_chatgpt_login(&mut self) { + // If we're already authenticated with ChatGPT, don't start a new login – + // just proceed to the success message flow. + if self.handle_existing_chatgpt_login() { return; } @@ -605,11 +685,6 @@ impl AuthModeWidget { self.cli_auth_credentials_store_mode, ); - if is_headless_environment() { - headless_chatgpt_login::start_headless_chatgpt_login(self, opts); - return; - } - match run_login_server(opts) { Ok(child) => { let sign_in_state = self.sign_in_state.clone(); @@ -649,6 +724,21 @@ impl AuthModeWidget { } } } + + fn start_device_code_login(&mut self) { + if self.handle_existing_chatgpt_login() { + return; + } + + self.error = None; + let opts = ServerOptions::new( + self.codex_home.clone(), + CLIENT_ID.to_string(), + self.forced_chatgpt_workspace_id.clone(), + self.cli_auth_credentials_store_mode, + ); + headless_chatgpt_login::start_headless_chatgpt_login(self, opts); + } } impl StepStateProvider for AuthModeWidget { @@ -707,7 +797,7 @@ mod tests { let codex_home_path = codex_home.path().to_path_buf(); let widget = AuthModeWidget { request_frame: FrameRequester::test_dummy(), - highlighted_mode: AuthMode::ChatGPT, + highlighted_mode: SignInOption::ChatGpt, error: None, sign_in_state: Arc::new(RwLock::new(SignInState::PickMode)), codex_home: codex_home_path.clone(), diff --git a/codex-rs/tui2/src/onboarding/onboarding_screen.rs b/codex-rs/tui2/src/onboarding/onboarding_screen.rs index 3ba2619a87..b16a980625 100644 --- a/codex-rs/tui2/src/onboarding/onboarding_screen.rs +++ b/codex-rs/tui2/src/onboarding/onboarding_screen.rs @@ -11,11 +11,11 @@ use ratatui::style::Color; use ratatui::widgets::Clear; use ratatui::widgets::WidgetRef; -use codex_app_server_protocol::AuthMode; use codex_protocol::config_types::ForcedLoginMethod; use crate::LoginStatus; use crate::onboarding::auth::AuthModeWidget; +use crate::onboarding::auth::SignInOption; use crate::onboarding::auth::SignInState; use crate::onboarding::trust_directory::TrustDirectorySelection; use crate::onboarding::trust_directory::TrustDirectoryWidget; @@ -92,8 +92,8 @@ impl OnboardingScreen { ))); if show_login_screen { let highlighted_mode = match forced_login_method { - Some(ForcedLoginMethod::Api) => AuthMode::ApiKey, - _ => AuthMode::ChatGPT, + Some(ForcedLoginMethod::Api) => SignInOption::ApiKey, + _ => SignInOption::ChatGpt, }; steps.push(Step::Auth(AuthModeWidget { request_frame: tui.frame_requester(), From 9147df0e60ac4600941bc11ccc08123ece9ea889 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Fri, 16 Jan 2026 08:53:47 -0800 Subject: [PATCH 12/32] Made `codex exec resume --last` consistent with `codex resume --last` (#9352) PR #9245 made `codex resume --last` honor cwd, but I forgot to make the same change for `codex exec resume --last`. This PR fixes the inconsistency. This addresses #8700 --- codex-rs/exec/src/cli.rs | 4 ++ codex-rs/exec/src/lib.rs | 12 ++-- codex-rs/exec/tests/suite/resume.rs | 85 +++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 4 deletions(-) diff --git a/codex-rs/exec/src/cli.rs b/codex-rs/exec/src/cli.rs index e12e869394..3d2001ae61 100644 --- a/codex-rs/exec/src/cli.rs +++ b/codex-rs/exec/src/cli.rs @@ -119,6 +119,10 @@ pub struct ResumeArgs { #[arg(long = "last", default_value_t = false)] pub last: bool, + /// Show all sessions (disables cwd filtering). + #[arg(long = "all", default_value_t = false)] + pub all: bool, + /// Optional image(s) to attach to the prompt sent after resuming. #[arg( long = "image", diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index 966dc65346..cdd8bf7daf 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -507,17 +507,21 @@ async fn resolve_resume_path( ) -> anyhow::Result> { if args.last { let default_provider_filter = vec![config.model_provider_id.clone()]; - match codex_core::RolloutRecorder::list_threads( + let filter_cwd = if args.all { + None + } else { + Some(config.cwd.as_path()) + }; + match codex_core::RolloutRecorder::find_latest_thread_path( &config.codex_home, - 1, - None, &[], Some(default_provider_filter.as_slice()), &config.model_provider_id, + filter_cwd, ) .await { - Ok(page) => Ok(page.items.first().map(|it| it.path.clone())), + Ok(path) => Ok(path), Err(e) => { error!("Error listing threads: {e}"); Ok(None) diff --git a/codex-rs/exec/tests/suite/resume.rs b/codex-rs/exec/tests/suite/resume.rs index 32913983e6..ec7fec0831 100644 --- a/codex-rs/exec/tests/suite/resume.rs +++ b/codex-rs/exec/tests/suite/resume.rs @@ -5,6 +5,7 @@ use core_test_support::test_codex_exec::test_codex_exec; use pretty_assertions::assert_eq; use serde_json::Value; use std::string::ToString; +use tempfile::TempDir; use uuid::Uuid; use walkdir::WalkDir; @@ -219,6 +220,90 @@ fn exec_resume_last_accepts_prompt_after_flag_in_json_mode() -> anyhow::Result<( Ok(()) } +#[test] +fn exec_resume_last_respects_cwd_filter_and_all_flag() -> anyhow::Result<()> { + let test = test_codex_exec(); + let fixture = exec_fixture()?; + + let dir_a = TempDir::new()?; + let dir_b = TempDir::new()?; + + let marker_a = format!("resume-cwd-a-{}", Uuid::new_v4()); + let prompt_a = format!("echo {marker_a}"); + test.cmd() + .env("CODEX_RS_SSE_FIXTURE", &fixture) + .env("OPENAI_BASE_URL", "http://unused.local") + .arg("--skip-git-repo-check") + .arg("-C") + .arg(dir_a.path()) + .arg(&prompt_a) + .assert() + .success(); + + let marker_b = format!("resume-cwd-b-{}", Uuid::new_v4()); + let prompt_b = format!("echo {marker_b}"); + test.cmd() + .env("CODEX_RS_SSE_FIXTURE", &fixture) + .env("OPENAI_BASE_URL", "http://unused.local") + .arg("--skip-git-repo-check") + .arg("-C") + .arg(dir_b.path()) + .arg(&prompt_b) + .assert() + .success(); + + let sessions_dir = test.home_path().join("sessions"); + let path_a = find_session_file_containing_marker(&sessions_dir, &marker_a) + .expect("no session file found for marker_a"); + let path_b = find_session_file_containing_marker(&sessions_dir, &marker_b) + .expect("no session file found for marker_b"); + + let marker_b2 = format!("resume-cwd-b-2-{}", Uuid::new_v4()); + let prompt_b2 = format!("echo {marker_b2}"); + test.cmd() + .env("CODEX_RS_SSE_FIXTURE", &fixture) + .env("OPENAI_BASE_URL", "http://unused.local") + .arg("--skip-git-repo-check") + .arg("-C") + .arg(dir_a.path()) + .arg("resume") + .arg("--last") + .arg("--all") + .arg(&prompt_b2) + .assert() + .success(); + + let resumed_path_all = find_session_file_containing_marker(&sessions_dir, &marker_b2) + .expect("no resumed session file containing marker_b2"); + assert_eq!( + resumed_path_all, path_b, + "resume --last --all should pick newest session" + ); + + let marker_a2 = format!("resume-cwd-a-2-{}", Uuid::new_v4()); + let prompt_a2 = format!("echo {marker_a2}"); + test.cmd() + .env("CODEX_RS_SSE_FIXTURE", &fixture) + .env("OPENAI_BASE_URL", "http://unused.local") + .arg("--skip-git-repo-check") + .arg("-C") + .arg(dir_a.path()) + .arg("resume") + .arg("--last") + .arg(&prompt_a2) + .assert() + .success(); + + let resumed_path_cwd = find_session_file_containing_marker(&sessions_dir, &marker_a2) + .expect("no resumed session file containing marker_a2"); + assert_eq!( + resumed_path_cwd, path_a, + "resume --last should prefer sessions from the same cwd" + ); + + Ok(()) +} + #[test] fn exec_resume_accepts_global_flags_after_subcommand() -> anyhow::Result<()> { let test = test_codex_exec(); From 4125c825f9ac26a2db42f37bdf6a20cfff262503 Mon Sep 17 00:00:00 2001 From: Jeremy Rose <172423086+nornagon-openai@users.noreply.github.com> Date: Fri, 16 Jan 2026 08:56:38 -0800 Subject: [PATCH 13/32] add codex cloud list (#9324) for listing cloud tasks. --- codex-rs/backend-client/src/client.rs | 6 + codex-rs/cloud-tasks-client/src/api.rs | 13 ++- codex-rs/cloud-tasks-client/src/http.rs | 32 +++++- codex-rs/cloud-tasks-client/src/lib.rs | 1 + codex-rs/cloud-tasks-client/src/mock.rs | 14 ++- codex-rs/cloud-tasks/src/app.rs | 29 ++++- codex-rs/cloud-tasks/src/cli.rs | 32 ++++++ codex-rs/cloud-tasks/src/lib.rs | 140 ++++++++++++++++++++++- codex-rs/cloud-tasks/tests/env_filter.rs | 15 ++- 9 files changed, 259 insertions(+), 23 deletions(-) diff --git a/codex-rs/backend-client/src/client.rs b/codex-rs/backend-client/src/client.rs index fdd4504bb6..ea3585956b 100644 --- a/codex-rs/backend-client/src/client.rs +++ b/codex-rs/backend-client/src/client.rs @@ -174,6 +174,7 @@ impl Client { limit: Option, task_filter: Option<&str>, environment_id: Option<&str>, + cursor: Option<&str>, ) -> Result { let url = match self.path_style { PathStyle::CodexApi => format!("{}/api/codex/tasks/list", self.base_url), @@ -190,6 +191,11 @@ impl Client { } else { req }; + let req = if let Some(c) = cursor { + req.query(&[("cursor", c)]) + } else { + req + }; let req = if let Some(id) = environment_id { req.query(&[("environment_id", id)]) } else { diff --git a/codex-rs/cloud-tasks-client/src/api.rs b/codex-rs/cloud-tasks-client/src/api.rs index cd8228bc28..7059bdb39f 100644 --- a/codex-rs/cloud-tasks-client/src/api.rs +++ b/codex-rs/cloud-tasks-client/src/api.rs @@ -94,6 +94,12 @@ pub struct CreatedTask { pub id: TaskId, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct TaskListPage { + pub tasks: Vec, + pub cursor: Option, +} + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] pub struct DiffSummary { pub files_changed: usize, @@ -126,7 +132,12 @@ impl Default for TaskText { #[async_trait::async_trait] pub trait CloudBackend: Send + Sync { - async fn list_tasks(&self, env: Option<&str>) -> Result>; + async fn list_tasks( + &self, + env: Option<&str>, + limit: Option, + cursor: Option<&str>, + ) -> Result; async fn get_task_summary(&self, id: TaskId) -> Result; async fn get_task_diff(&self, id: TaskId) -> Result>; /// Return assistant output messages (no diff) when available. diff --git a/codex-rs/cloud-tasks-client/src/http.rs b/codex-rs/cloud-tasks-client/src/http.rs index f55d0fe797..e6990b7ebd 100644 --- a/codex-rs/cloud-tasks-client/src/http.rs +++ b/codex-rs/cloud-tasks-client/src/http.rs @@ -6,6 +6,7 @@ use crate::CloudTaskError; use crate::DiffSummary; use crate::Result; use crate::TaskId; +use crate::TaskListPage; use crate::TaskStatus; use crate::TaskSummary; use crate::TurnAttempt; @@ -59,8 +60,13 @@ impl HttpClient { #[async_trait::async_trait] impl CloudBackend for HttpClient { - async fn list_tasks(&self, env: Option<&str>) -> Result> { - self.tasks_api().list(env).await + async fn list_tasks( + &self, + env: Option<&str>, + limit: Option, + cursor: Option<&str>, + ) -> Result { + self.tasks_api().list(env, limit, cursor).await } async fn get_task_summary(&self, id: TaskId) -> Result { @@ -132,10 +138,16 @@ mod api { } } - pub(crate) async fn list(&self, env: Option<&str>) -> Result> { + pub(crate) async fn list( + &self, + env: Option<&str>, + limit: Option, + cursor: Option<&str>, + ) -> Result { + let limit_i32 = limit.and_then(|lim| i32::try_from(lim).ok()); let resp = self .backend - .list_tasks(Some(20), Some("current"), env) + .list_tasks(limit_i32, Some("current"), env, cursor) .await .map_err(|e| CloudTaskError::Http(format!("list_tasks failed: {e}")))?; @@ -146,11 +158,19 @@ mod api { .collect(); append_error_log(&format!( - "http.list_tasks: env={} items={}", + "http.list_tasks: env={} limit={} cursor_in={} cursor_out={} items={}", env.unwrap_or(""), + limit_i32 + .map(|v| v.to_string()) + .unwrap_or_else(|| "".to_string()), + cursor.unwrap_or(""), + resp.cursor.as_deref().unwrap_or(""), tasks.len() )); - Ok(tasks) + Ok(TaskListPage { + tasks, + cursor: resp.cursor, + }) } pub(crate) async fn summary(&self, id: TaskId) -> Result { diff --git a/codex-rs/cloud-tasks-client/src/lib.rs b/codex-rs/cloud-tasks-client/src/lib.rs index a723512f8a..b28b356f2a 100644 --- a/codex-rs/cloud-tasks-client/src/lib.rs +++ b/codex-rs/cloud-tasks-client/src/lib.rs @@ -9,6 +9,7 @@ pub use api::CreatedTask; pub use api::DiffSummary; pub use api::Result; pub use api::TaskId; +pub use api::TaskListPage; pub use api::TaskStatus; pub use api::TaskSummary; pub use api::TaskText; diff --git a/codex-rs/cloud-tasks-client/src/mock.rs b/codex-rs/cloud-tasks-client/src/mock.rs index 2d03cea029..f6e14e61a2 100644 --- a/codex-rs/cloud-tasks-client/src/mock.rs +++ b/codex-rs/cloud-tasks-client/src/mock.rs @@ -16,7 +16,12 @@ pub struct MockClient; #[async_trait::async_trait] impl CloudBackend for MockClient { - async fn list_tasks(&self, _env: Option<&str>) -> Result> { + async fn list_tasks( + &self, + _env: Option<&str>, + _limit: Option, + _cursor: Option<&str>, + ) -> Result { // Slightly vary content by env to aid tests that rely on the mock let rows = match _env { Some("env-A") => vec![("T-2000", "A: First", TaskStatus::Ready)], @@ -58,11 +63,14 @@ impl CloudBackend for MockClient { attempt_total: Some(if id_str == "T-1000" { 2 } else { 1 }), }); } - Ok(out) + Ok(crate::TaskListPage { + tasks: out, + cursor: None, + }) } async fn get_task_summary(&self, id: TaskId) -> Result { - let tasks = self.list_tasks(None).await?; + let tasks = self.list_tasks(None, None, None).await?.tasks; tasks .into_iter() .find(|t| t.id == id) diff --git a/codex-rs/cloud-tasks/src/app.rs b/codex-rs/cloud-tasks/src/app.rs index ce12128a3e..ac3dd9e8df 100644 --- a/codex-rs/cloud-tasks/src/app.rs +++ b/codex-rs/cloud-tasks/src/app.rs @@ -123,9 +123,13 @@ pub async fn load_tasks( env: Option<&str>, ) -> anyhow::Result> { // In later milestones, add a small debounce, spinner, and error display. - let tasks = tokio::time::timeout(Duration::from_secs(5), backend.list_tasks(env)).await??; + let tasks = tokio::time::timeout( + Duration::from_secs(5), + backend.list_tasks(env, Some(20), None), + ) + .await??; // Hide review-only tasks from the main list. - let filtered: Vec = tasks.into_iter().filter(|t| !t.is_review).collect(); + let filtered: Vec = tasks.tasks.into_iter().filter(|t| !t.is_review).collect(); Ok(filtered) } @@ -362,7 +366,9 @@ mod tests { async fn list_tasks( &self, env: Option<&str>, - ) -> codex_cloud_tasks_client::Result> { + limit: Option, + cursor: Option<&str>, + ) -> codex_cloud_tasks_client::Result { let key = env.map(str::to_string); let titles = self .by_env @@ -383,15 +389,28 @@ mod tests { attempt_total: Some(1), }); } - Ok(out) + let max = limit.unwrap_or(i64::MAX); + let max = max.min(20); + let mut limited = Vec::new(); + for task in out { + if (limited.len() as i64) >= max { + break; + } + limited.push(task); + } + Ok(codex_cloud_tasks_client::TaskListPage { + tasks: limited, + cursor: cursor.map(str::to_string), + }) } async fn get_task_summary( &self, id: TaskId, ) -> codex_cloud_tasks_client::Result { - self.list_tasks(None) + self.list_tasks(None, None, None) .await? + .tasks .into_iter() .find(|t| t.id == id) .ok_or_else(|| CloudTaskError::Msg(format!("Task {} not found", id.0))) diff --git a/codex-rs/cloud-tasks/src/cli.rs b/codex-rs/cloud-tasks/src/cli.rs index 6b36509639..595c649b38 100644 --- a/codex-rs/cloud-tasks/src/cli.rs +++ b/codex-rs/cloud-tasks/src/cli.rs @@ -18,6 +18,8 @@ pub enum Command { Exec(ExecCommand), /// Show the status of a Codex Cloud task. Status(StatusCommand), + /// List Codex Cloud tasks. + List(ListCommand), /// Apply the diff for a Codex Cloud task locally. Apply(ApplyCommand), /// Show the unified diff for a Codex Cloud task. @@ -58,6 +60,17 @@ fn parse_attempts(input: &str) -> Result { } } +fn parse_limit(input: &str) -> Result { + let value: i64 = input + .parse() + .map_err(|_| "limit must be an integer between 1 and 20".to_string())?; + if (1..=20).contains(&value) { + Ok(value) + } else { + Err("limit must be between 1 and 20".to_string()) + } +} + #[derive(Debug, Args)] pub struct StatusCommand { /// Codex Cloud task identifier to inspect. @@ -65,6 +78,25 @@ pub struct StatusCommand { pub task_id: String, } +#[derive(Debug, Args)] +pub struct ListCommand { + /// Filter tasks by environment identifier. + #[arg(long = "env", value_name = "ENV_ID")] + pub environment: Option, + + /// Maximum number of tasks to return (1-20). + #[arg(long = "limit", default_value_t = 20, value_parser = parse_limit, value_name = "N")] + pub limit: i64, + + /// Pagination cursor returned by a previous call. + #[arg(long = "cursor", value_name = "CURSOR")] + pub cursor: Option, + + /// Emit JSON instead of plain text. + #[arg(long = "json", default_value_t = false)] + pub json: bool, +} + #[derive(Debug, Args)] pub struct ApplyCommand { /// Codex Cloud task identifier to apply. diff --git a/codex-rs/cloud-tasks/src/lib.rs b/codex-rs/cloud-tasks/src/lib.rs index e1bedbc1ce..b1d42fb86f 100644 --- a/codex-rs/cloud-tasks/src/lib.rs +++ b/codex-rs/cloud-tasks/src/lib.rs @@ -393,11 +393,10 @@ fn summary_line(summary: &codex_cloud_tasks_client::DiffSummary, colorize: bool) let bullet = "•" .if_supports_color(Stream::Stdout, |t| t.dimmed()) .to_string(); - let file_label = "file" + let file_label = format!("file{}", if files == 1 { "" } else { "s" }) .if_supports_color(Stream::Stdout, |t| t.dimmed()) .to_string(); - let plural = if files == 1 { "" } else { "s" }; - format!("{adds_str}/{dels_str} {bullet} {files} {file_label}{plural}") + format!("{adds_str}/{dels_str} {bullet} {files} {file_label}") } else { format!( "+{adds}/-{dels} • {files} file{}", @@ -473,6 +472,25 @@ fn format_task_status_lines( lines } +fn format_task_list_lines( + tasks: &[codex_cloud_tasks_client::TaskSummary], + base_url: &str, + now: chrono::DateTime, + colorize: bool, +) -> Vec { + let mut lines = Vec::new(); + for (idx, task) in tasks.iter().enumerate() { + lines.push(util::task_url(base_url, &task.id.0)); + for line in format_task_status_lines(task, now, colorize) { + lines.push(format!(" {line}")); + } + if idx + 1 < tasks.len() { + lines.push(String::new()); + } + } + lines +} + async fn run_status_command(args: crate::cli::StatusCommand) -> anyhow::Result<()> { let ctx = init_backend("codex_cloud_tasks_status").await?; let task_id = parse_task_id(&args.task_id)?; @@ -489,6 +507,73 @@ async fn run_status_command(args: crate::cli::StatusCommand) -> anyhow::Result<( Ok(()) } +async fn run_list_command(args: crate::cli::ListCommand) -> anyhow::Result<()> { + let ctx = init_backend("codex_cloud_tasks_list").await?; + let env_filter = if let Some(env) = args.environment { + Some(resolve_environment_id(&ctx, &env).await?) + } else { + None + }; + let page = codex_cloud_tasks_client::CloudBackend::list_tasks( + &*ctx.backend, + env_filter.as_deref(), + Some(args.limit), + args.cursor.as_deref(), + ) + .await?; + if args.json { + let tasks: Vec<_> = page + .tasks + .iter() + .map(|task| { + serde_json::json!({ + "id": task.id.0, + "url": util::task_url(&ctx.base_url, &task.id.0), + "title": task.title, + "status": task.status, + "updated_at": task.updated_at, + "environment_id": task.environment_id, + "environment_label": task.environment_label, + "summary": { + "files_changed": task.summary.files_changed, + "lines_added": task.summary.lines_added, + "lines_removed": task.summary.lines_removed, + }, + "is_review": task.is_review, + "attempt_total": task.attempt_total, + }) + }) + .collect(); + let payload = serde_json::json!({ + "tasks": tasks, + "cursor": page.cursor, + }); + println!("{}", serde_json::to_string_pretty(&payload)?); + return Ok(()); + } + if page.tasks.is_empty() { + println!("No tasks found."); + return Ok(()); + } + let now = Utc::now(); + let colorize = supports_color::on(SupportStream::Stdout).is_some(); + for line in format_task_list_lines(&page.tasks, &ctx.base_url, now, colorize) { + println!("{line}"); + } + if let Some(cursor) = page.cursor { + let command = format!("codex cloud list --cursor='{cursor}'"); + if colorize { + println!( + "\nTo fetch the next page, run {}", + command.if_supports_color(Stream::Stdout, |text| text.cyan()) + ); + } else { + println!("\nTo fetch the next page, run {command}"); + } + } + Ok(()) +} + async fn run_diff_command(args: crate::cli::DiffCommand) -> anyhow::Result<()> { let ctx = init_backend("codex_cloud_tasks_diff").await?; let task_id = parse_task_id(&args.task_id)?; @@ -649,6 +734,7 @@ pub async fn run_main(cli: Cli, _codex_linux_sandbox_exe: Option) -> an return match command { crate::cli::Command::Exec(args) => run_exec_command(args).await, crate::cli::Command::Status(args) => run_status_command(args).await, + crate::cli::Command::List(args) => run_list_command(args).await, crate::cli::Command::Apply(args) => run_apply_command(args).await, crate::cli::Command::Diff(args) => run_diff_command(args).await, }; @@ -2181,6 +2267,54 @@ mod tests { ); } + #[test] + fn format_task_list_lines_formats_urls() { + let now = Utc::now(); + let tasks = vec![ + TaskSummary { + id: TaskId("task_1".to_string()), + title: "Example task".to_string(), + status: TaskStatus::Ready, + updated_at: now, + environment_id: Some("env-1".to_string()), + environment_label: Some("Env".to_string()), + summary: DiffSummary { + files_changed: 3, + lines_added: 5, + lines_removed: 2, + }, + is_review: false, + attempt_total: None, + }, + TaskSummary { + id: TaskId("task_2".to_string()), + title: "No diff task".to_string(), + status: TaskStatus::Pending, + updated_at: now, + environment_id: Some("env-2".to_string()), + environment_label: None, + summary: DiffSummary::default(), + is_review: false, + attempt_total: Some(1), + }, + ]; + let lines = format_task_list_lines(&tasks, "https://chatgpt.com/backend-api", now, false); + assert_eq!( + lines, + vec![ + "https://chatgpt.com/codex/tasks/task_1".to_string(), + " [READY] Example task".to_string(), + " Env • 0s ago".to_string(), + " +5/-2 • 3 files".to_string(), + String::new(), + "https://chatgpt.com/codex/tasks/task_2".to_string(), + " [PENDING] No diff task".to_string(), + " env-2 • 0s ago".to_string(), + " no diff".to_string(), + ] + ); + } + #[tokio::test] async fn collect_attempt_diffs_includes_sibling_attempts() { let backend = MockClient; diff --git a/codex-rs/cloud-tasks/tests/env_filter.rs b/codex-rs/cloud-tasks/tests/env_filter.rs index 8c737c6c28..688ccd29bd 100644 --- a/codex-rs/cloud-tasks/tests/env_filter.rs +++ b/codex-rs/cloud-tasks/tests/env_filter.rs @@ -5,18 +5,23 @@ use codex_cloud_tasks_client::MockClient; async fn mock_backend_varies_by_env() { let client = MockClient; - let root = CloudBackend::list_tasks(&client, None).await.unwrap(); + let root = CloudBackend::list_tasks(&client, None, None, None) + .await + .unwrap() + .tasks; assert!(root.iter().any(|t| t.title.contains("Update README"))); - let a = CloudBackend::list_tasks(&client, Some("env-A")) + let a = CloudBackend::list_tasks(&client, Some("env-A"), None, None) .await - .unwrap(); + .unwrap() + .tasks; assert_eq!(a.len(), 1); assert_eq!(a[0].title, "A: First"); - let b = CloudBackend::list_tasks(&client, Some("env-B")) + let b = CloudBackend::list_tasks(&client, Some("env-B"), None, None) .await - .unwrap(); + .unwrap() + .tasks; assert_eq!(b.len(), 2); assert!(b[0].title.starts_with("B: ")); } From ebdd8795e924a8149b616e46ca2ed7848c207a4b Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Fri, 16 Jan 2026 09:30:11 -0800 Subject: [PATCH 14/32] Turn-state sticky routing per turn (#9332) - capture the header from SSE/WS handshakes, store it per ModelClientSession using `Oncelock`, echo it on turn-scoped requests, and add SSE+WS integration tests for within-turn persistence + cross-turn reset. - keep `x-codex-turn-state` sticky within a user turn to maintain routing continuity for retries/tool follow-ups. --- codex-rs/codex-api/src/endpoint/chat.rs | 1 + codex-rs/codex-api/src/endpoint/responses.rs | 17 ++- .../src/endpoint/responses_websocket.rs | 21 ++- codex-rs/codex-api/src/endpoint/streaming.rs | 12 +- codex-rs/codex-api/src/sse/chat.rs | 5 +- codex-rs/codex-api/src/sse/responses.rs | 10 ++ codex-rs/codex-api/tests/clients.rs | 6 +- codex-rs/codex-api/tests/sse_end_to_end.rs | 2 + codex-rs/core/src/client.rs | 31 +++- codex-rs/core/tests/common/responses.rs | 140 ++++++++++++++++-- codex-rs/core/tests/suite/turn_state.rs | 122 +++++++++++++++ 11 files changed, 343 insertions(+), 24 deletions(-) create mode 100644 codex-rs/core/tests/suite/turn_state.rs diff --git a/codex-rs/codex-api/src/endpoint/chat.rs b/codex-rs/codex-api/src/endpoint/chat.rs index f747c57411..cd830a09fc 100644 --- a/codex-rs/codex-api/src/endpoint/chat.rs +++ b/codex-rs/codex-api/src/endpoint/chat.rs @@ -87,6 +87,7 @@ impl ChatClient { extra_headers, RequestCompression::None, spawn_chat_stream, + None, ) .await } diff --git a/codex-rs/codex-api/src/endpoint/responses.rs b/codex-rs/codex-api/src/endpoint/responses.rs index 57c7b0da03..4aded33911 100644 --- a/codex-rs/codex-api/src/endpoint/responses.rs +++ b/codex-rs/codex-api/src/endpoint/responses.rs @@ -19,6 +19,7 @@ use codex_protocol::protocol::SessionSource; use http::HeaderMap; use serde_json::Value; use std::sync::Arc; +use std::sync::OnceLock; use tracing::instrument; pub struct ResponsesClient { @@ -36,6 +37,7 @@ pub struct ResponsesOptions { pub session_source: Option, pub extra_headers: HeaderMap, pub compression: Compression, + pub turn_state: Option>>, } impl ResponsesClient { @@ -58,9 +60,15 @@ impl ResponsesClient { pub async fn stream_request( &self, request: ResponsesRequest, + turn_state: Option>>, ) -> Result { - self.stream(request.body, request.headers, request.compression) - .await + self.stream( + request.body, + request.headers, + request.compression, + turn_state, + ) + .await } #[instrument(level = "trace", skip_all, err)] @@ -80,6 +88,7 @@ impl ResponsesClient { session_source, extra_headers, compression, + turn_state, } = options; let request = ResponsesRequestBuilder::new(model, &prompt.instructions, &prompt.input) @@ -96,7 +105,7 @@ impl ResponsesClient { .compression(compression) .build(self.streaming.provider())?; - self.stream_request(request).await + self.stream_request(request, turn_state).await } fn path(&self) -> &'static str { @@ -111,6 +120,7 @@ impl ResponsesClient { body: Value, extra_headers: HeaderMap, compression: Compression, + turn_state: Option>>, ) -> Result { let compression = match compression { Compression::None => RequestCompression::None, @@ -124,6 +134,7 @@ impl ResponsesClient { extra_headers, compression, spawn_response_stream, + turn_state, ) .await } diff --git a/codex-rs/codex-api/src/endpoint/responses_websocket.rs b/codex-rs/codex-api/src/endpoint/responses_websocket.rs index af3335ce76..3c6cab74e5 100644 --- a/codex-rs/codex-api/src/endpoint/responses_websocket.rs +++ b/codex-rs/codex-api/src/endpoint/responses_websocket.rs @@ -13,6 +13,7 @@ use http::HeaderMap; use http::HeaderValue; use serde_json::Value; use std::sync::Arc; +use std::sync::OnceLock; use std::time::Duration; use tokio::net::TcpStream; use tokio::sync::Mutex; @@ -27,6 +28,7 @@ use tracing::trace; use url::Url; type WsStream = WebSocketStream>; +const X_CODEX_TURN_STATE_HEADER: &str = "x-codex-turn-state"; pub struct ResponsesWebsocketConnection { stream: Arc>>, @@ -100,6 +102,7 @@ impl ResponsesWebsocketClient { pub async fn connect( &self, extra_headers: HeaderMap, + turn_state: Option>>, ) -> Result { let ws_url = Url::parse(&self.provider.url_for_path("responses")) .map_err(|err| ApiError::Stream(format!("failed to build websocket URL: {err}")))?; @@ -108,7 +111,7 @@ impl ResponsesWebsocketClient { headers.extend(extra_headers); apply_auth_headers(&mut headers, &self.auth); - let stream = connect_websocket(ws_url, headers).await?; + let stream = connect_websocket(ws_url, headers, turn_state).await?; Ok(ResponsesWebsocketConnection::new( stream, self.provider.stream_idle_timeout, @@ -130,16 +133,28 @@ fn apply_auth_headers(headers: &mut HeaderMap, auth: &impl AuthProvider) { } } -async fn connect_websocket(url: Url, headers: HeaderMap) -> Result { +async fn connect_websocket( + url: Url, + headers: HeaderMap, + turn_state: Option>>, +) -> Result { let mut request = url .clone() .into_client_request() .map_err(|err| ApiError::Stream(format!("failed to build websocket request: {err}")))?; request.headers_mut().extend(headers); - let (stream, _) = tokio_tungstenite::connect_async(request) + let (stream, response) = tokio_tungstenite::connect_async(request) .await .map_err(|err| map_ws_error(err, &url))?; + if let Some(turn_state) = turn_state + && let Some(header_value) = response + .headers() + .get(X_CODEX_TURN_STATE_HEADER) + .and_then(|value| value.to_str().ok()) + { + let _ = turn_state.set(header_value.to_string()); + } Ok(stream) } diff --git a/codex-rs/codex-api/src/endpoint/streaming.rs b/codex-rs/codex-api/src/endpoint/streaming.rs index de180845e8..15d4c077a0 100644 --- a/codex-rs/codex-api/src/endpoint/streaming.rs +++ b/codex-rs/codex-api/src/endpoint/streaming.rs @@ -13,6 +13,7 @@ use http::HeaderMap; use http::Method; use serde_json::Value; use std::sync::Arc; +use std::sync::OnceLock; use std::time::Duration; pub(crate) struct StreamingClient { @@ -23,6 +24,13 @@ pub(crate) struct StreamingClient { sse_telemetry: Option>, } +type StreamSpawner = fn( + StreamResponse, + Duration, + Option>, + Option>>, +) -> ResponseStream; + impl StreamingClient { pub(crate) fn new(transport: T, provider: Provider, auth: A) -> Self { Self { @@ -54,7 +62,8 @@ impl StreamingClient { body: Value, extra_headers: HeaderMap, compression: RequestCompression, - spawner: fn(StreamResponse, Duration, Option>) -> ResponseStream, + spawner: StreamSpawner, + turn_state: Option>>, ) -> Result { let builder = || { let mut req = self.provider.build_request(Method::POST, path); @@ -80,6 +89,7 @@ impl StreamingClient { stream_response, self.provider.stream_idle_timeout, self.sse_telemetry.clone(), + turn_state, )) } } diff --git a/codex-rs/codex-api/src/sse/chat.rs b/codex-rs/codex-api/src/sse/chat.rs index dec35890b7..afd7c8852b 100644 --- a/codex-rs/codex-api/src/sse/chat.rs +++ b/codex-rs/codex-api/src/sse/chat.rs @@ -11,6 +11,8 @@ use futures::Stream; use futures::StreamExt; use std::collections::HashMap; use std::collections::HashSet; +use std::sync::Arc; +use std::sync::OnceLock; use std::time::Duration; use tokio::sync::mpsc; use tokio::time::Instant; @@ -21,7 +23,8 @@ use tracing::trace; pub(crate) fn spawn_chat_stream( stream_response: StreamResponse, idle_timeout: Duration, - telemetry: Option>, + telemetry: Option>, + _turn_state: Option>>, ) -> ResponseStream { let (tx_event, rx_event) = mpsc::channel::>(1600); tokio::spawn(async move { diff --git a/codex-rs/codex-api/src/sse/responses.rs b/codex-rs/codex-api/src/sse/responses.rs index 06f74db0ab..5299310e06 100644 --- a/codex-rs/codex-api/src/sse/responses.rs +++ b/codex-rs/codex-api/src/sse/responses.rs @@ -16,6 +16,7 @@ use serde_json::Value; use std::io::BufRead; use std::path::Path; use std::sync::Arc; +use std::sync::OnceLock; use std::time::Duration; use tokio::sync::mpsc; use tokio::time::Instant; @@ -49,6 +50,7 @@ pub fn spawn_response_stream( stream_response: StreamResponse, idle_timeout: Duration, telemetry: Option>, + turn_state: Option>>, ) -> ResponseStream { let rate_limits = parse_rate_limit(&stream_response.headers); let models_etag = stream_response @@ -56,6 +58,14 @@ pub fn spawn_response_stream( .get("X-Models-Etag") .and_then(|v| v.to_str().ok()) .map(ToString::to_string); + if let Some(turn_state) = turn_state.as_ref() + && let Some(header_value) = stream_response + .headers + .get("x-codex-turn-state") + .and_then(|v| v.to_str().ok()) + { + let _ = turn_state.set(header_value.to_string()); + } let (tx_event, rx_event) = mpsc::channel::>(1600); tokio::spawn(async move { if let Some(snapshot) = rate_limits { diff --git a/codex-rs/codex-api/tests/clients.rs b/codex-rs/codex-api/tests/clients.rs index 70af9fe829..1a513bdca4 100644 --- a/codex-rs/codex-api/tests/clients.rs +++ b/codex-rs/codex-api/tests/clients.rs @@ -231,7 +231,7 @@ async fn responses_client_uses_responses_path_for_responses_wire() -> Result<()> let body = serde_json::json!({ "echo": true }); let _stream = client - .stream(body, HeaderMap::new(), Compression::None) + .stream(body, HeaderMap::new(), Compression::None, None) .await?; let requests = state.take_stream_requests(); @@ -247,7 +247,7 @@ async fn responses_client_uses_chat_path_for_chat_wire() -> Result<()> { let body = serde_json::json!({ "echo": true }); let _stream = client - .stream(body, HeaderMap::new(), Compression::None) + .stream(body, HeaderMap::new(), Compression::None, None) .await?; let requests = state.take_stream_requests(); @@ -264,7 +264,7 @@ async fn streaming_client_adds_auth_headers() -> Result<()> { let body = serde_json::json!({ "model": "gpt-test" }); let _stream = client - .stream(body, HeaderMap::new(), Compression::None) + .stream(body, HeaderMap::new(), Compression::None, None) .await?; let requests = state.take_stream_requests(); diff --git a/codex-rs/codex-api/tests/sse_end_to_end.rs b/codex-rs/codex-api/tests/sse_end_to_end.rs index f324cc7480..eb9d7993f8 100644 --- a/codex-rs/codex-api/tests/sse_end_to_end.rs +++ b/codex-rs/codex-api/tests/sse_end_to_end.rs @@ -129,6 +129,7 @@ async fn responses_stream_parses_items_and_completed_end_to_end() -> Result<()> serde_json::json!({"echo": true}), HeaderMap::new(), Compression::None, + None, ) .await?; @@ -198,6 +199,7 @@ async fn responses_stream_aggregates_output_text_deltas() -> Result<()> { serde_json::json!({"echo": true}), HeaderMap::new(), Compression::None, + None, ) .await?; diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 326d81e0e9..03e74b54b9 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -1,4 +1,5 @@ use std::sync::Arc; +use std::sync::OnceLock; use crate::api_bridge::CoreAuthProvider; use crate::api_bridge::auth_provider_from_auth; @@ -66,6 +67,7 @@ use crate::tools::spec::create_tools_json_for_chat_completions_api; use crate::tools::spec::create_tools_json_for_responses_api; pub const WEB_SEARCH_ELIGIBLE_HEADER: &str = "x-oai-web-search-eligible"; +pub const X_CODEX_TURN_STATE_HEADER: &str = "x-codex-turn-state"; #[derive(Debug)] struct ModelClientState { @@ -89,6 +91,17 @@ pub struct ModelClientSession { state: Arc, connection: Option, websocket_last_items: Vec, + /// Turn state for sticky routing. + /// + /// This is an `OnceLock` that stores the turn state value received from the server + /// on turn start via the `x-codex-turn-state` response header. Once set, this value + /// should be sent back to the server in the `x-codex-turn-state` request header for + /// all subsequent requests within the same turn to maintain sticky routing. + /// + /// This is a contract between the client and server: we receive it at turn start, + /// keep sending it unchanged between turn requests (e.g., for retries, incremental + /// appends, or continuation requests), and must not send it between different turns. + turn_state: Arc>, } #[allow(clippy::too_many_arguments)] @@ -124,6 +137,7 @@ impl ModelClient { state: Arc::clone(&self.state), connection: None, websocket_last_items: Vec::new(), + turn_state: Arc::new(OnceLock::new()), } } } @@ -226,7 +240,6 @@ impl ModelClient { extra_headers.insert("x-openai-subagent", val); } } - client .compact_input(&payload, extra_headers) .await @@ -322,8 +335,9 @@ impl ModelClientSession { store_override: None, conversation_id: Some(conversation_id), session_source: Some(self.state.session_source.clone()), - extra_headers: build_responses_headers(&self.state.config), + extra_headers: build_responses_headers(&self.state.config, Some(&self.turn_state)), compression, + turn_state: Some(Arc::clone(&self.turn_state)), } } @@ -397,7 +411,7 @@ impl ModelClientSession { headers.extend(build_conversation_headers(options.conversation_id.clone())); let new_conn: ApiWebSocketConnection = ApiWebSocketResponsesClient::new(api_provider, api_auth) - .connect(headers) + .connect(headers, options.turn_state.clone()) .await?; self.connection = Some(new_conn); } @@ -638,7 +652,10 @@ fn beta_feature_headers(config: &Config) -> ApiHeaderMap { headers } -fn build_responses_headers(config: &Config) -> ApiHeaderMap { +fn build_responses_headers( + config: &Config, + turn_state: Option<&Arc>>, +) -> ApiHeaderMap { let mut headers = beta_feature_headers(config); headers.insert( WEB_SEARCH_ELIGIBLE_HEADER, @@ -650,6 +667,12 @@ fn build_responses_headers(config: &Config) -> ApiHeaderMap { }, ), ); + if let Some(turn_state) = turn_state + && let Some(state) = turn_state.get() + && let Ok(header_value) = HeaderValue::from_str(state) + { + headers.insert(X_CODEX_TURN_STATE_HEADER, header_value); + } headers } diff --git a/codex-rs/core/tests/common/responses.rs b/codex-rs/core/tests/common/responses.rs index f8fbcfff5c..3896911cc2 100644 --- a/codex-rs/core/tests/common/responses.rs +++ b/codex-rs/core/tests/common/responses.rs @@ -12,6 +12,8 @@ use serde_json::Value; use tokio::net::TcpListener; use tokio::sync::oneshot; use tokio_tungstenite::tungstenite::Message; +use tokio_tungstenite::tungstenite::handshake::server::Request; +use tokio_tungstenite::tungstenite::handshake::server::Response; use wiremock::BodyPrintLimit; use wiremock::Match; use wiremock::Mock; @@ -19,6 +21,8 @@ use wiremock::MockBuilder; use wiremock::MockServer; use wiremock::Respond; use wiremock::ResponseTemplate; +use wiremock::http::HeaderName; +use wiremock::http::HeaderValue; use wiremock::matchers::method; use wiremock::matchers::path_regex; @@ -216,9 +220,30 @@ impl WebSocketRequest { } } +#[derive(Debug, Clone)] +pub struct WebSocketHandshake { + headers: Vec<(String, String)>, +} + +impl WebSocketHandshake { + pub fn header(&self, name: &str) -> Option { + self.headers + .iter() + .find(|(header, _)| header.eq_ignore_ascii_case(name)) + .map(|(_, value)| value.clone()) + } +} + +#[derive(Debug, Clone)] +pub struct WebSocketConnectionConfig { + pub requests: Vec>, + pub response_headers: Vec<(String, String)>, +} + pub struct WebSocketTestServer { uri: String, connections: Arc>>>, + handshakes: Arc>>, shutdown: oneshot::Sender<()>, task: tokio::task::JoinHandle<()>, } @@ -240,6 +265,18 @@ impl WebSocketTestServer { connections.first().cloned().unwrap_or_default() } + pub fn handshakes(&self) -> Vec { + self.handshakes.lock().unwrap().clone() + } + + pub fn single_handshake(&self) -> WebSocketHandshake { + let handshakes = self.handshakes.lock().unwrap(); + if handshakes.len() != 1 { + panic!("expected 1 handshake, got {}", handshakes.len()); + } + handshakes.first().cloned().unwrap() + } + pub async fn shutdown(self) { let _ = self.shutdown.send(()); let _ = self.task.await; @@ -786,13 +823,28 @@ pub async fn start_mock_server() -> MockServer { /// request message, the server records the payload and streams the matching /// events as WebSocket text frames before moving to the next request. pub async fn start_websocket_server(connections: Vec>>) -> WebSocketTestServer { + let connections = connections + .into_iter() + .map(|requests| WebSocketConnectionConfig { + requests, + response_headers: Vec::new(), + }) + .collect(); + start_websocket_server_with_headers(connections).await +} + +pub async fn start_websocket_server_with_headers( + connections: Vec, +) -> WebSocketTestServer { let listener = TcpListener::bind("127.0.0.1:0") .await .expect("bind websocket server"); let addr = listener.local_addr().expect("websocket server address"); let uri = format!("ws://{addr}"); let connections_log = Arc::new(Mutex::new(Vec::new())); + let handshakes_log = Arc::new(Mutex::new(Vec::new())); let requests = Arc::clone(&connections_log); + let handshakes = Arc::clone(&handshakes_log); let connections = Arc::new(Mutex::new(VecDeque::from(connections))); let (shutdown_tx, mut shutdown_rx) = oneshot::channel(); @@ -806,27 +858,57 @@ pub async fn start_websocket_server(connections: Vec>>) -> WebSoc Ok(value) => value, Err(_) => return, }; - let mut ws_stream = match tokio_tungstenite::accept_async(stream).await { - Ok(ws) => ws, - Err(_) => continue, - }; - - let connection_requests = { + let connection = { let mut pending = connections.lock().unwrap(); pending.pop_front() }; - let Some(connection_requests) = connection_requests else { - let _ = ws_stream.close(None).await; + let Some(connection) = connection else { continue; }; + let response_headers = connection.response_headers.clone(); + let handshake_log = Arc::clone(&handshakes); + let callback = move |req: &Request, mut response: Response| { + let headers = req + .headers() + .iter() + .filter_map(|(name, value)| { + value + .to_str() + .ok() + .map(|value| (name.as_str().to_string(), value.to_string())) + }) + .collect(); + handshake_log + .lock() + .unwrap() + .push(WebSocketHandshake { headers }); + + let headers_mut = response.headers_mut(); + for (name, value) in &response_headers { + if let (Ok(name), Ok(value)) = ( + HeaderName::from_bytes(name.as_bytes()), + HeaderValue::from_str(value), + ) { + headers_mut.insert(name, value); + } + } + + Ok(response) + }; + + let mut ws_stream = match tokio_tungstenite::accept_hdr_async(stream, callback).await { + Ok(ws) => ws, + Err(_) => continue, + }; + let connection_index = { let mut log = requests.lock().unwrap(); log.push(Vec::new()); log.len() - 1 }; - for request_events in connection_requests { + for request_events in connection.requests { let Some(Ok(message)) = ws_stream.next().await else { break; }; @@ -858,6 +940,7 @@ pub async fn start_websocket_server(connections: Vec>>) -> WebSoc WebSocketTestServer { uri, connections: connections_log, + handshakes: handshakes_log, shutdown: shutdown_tx, task, } @@ -942,6 +1025,45 @@ pub async fn mount_sse_sequence(server: &MockServer, bodies: Vec) -> Res response_mock } +/// Mounts a sequence of responses for each POST to `/v1/responses`. +/// Panics if more requests are received than responses provided. +pub async fn mount_response_sequence( + server: &MockServer, + responses: Vec, +) -> ResponseMock { + use std::sync::atomic::AtomicUsize; + use std::sync::atomic::Ordering; + + struct SeqResponder { + num_calls: AtomicUsize, + responses: Vec, + } + + impl Respond for SeqResponder { + fn respond(&self, _: &wiremock::Request) -> ResponseTemplate { + let call_num = self.num_calls.fetch_add(1, Ordering::SeqCst); + self.responses + .get(call_num) + .unwrap_or_else(|| panic!("no response for {call_num}")) + .clone() + } + } + + let num_calls = responses.len(); + let responder = SeqResponder { + num_calls: AtomicUsize::new(0), + responses, + }; + + let (mock, response_mock) = base_mock(); + mock.respond_with(responder) + .up_to_n_times(num_calls as u64) + .expect(num_calls as u64) + .mount(server) + .await; + response_mock +} + /// Validate invariants on the request body sent to `/v1/responses`. /// /// - No `function_call_output`/`custom_tool_call_output` with missing/empty `call_id`. diff --git a/codex-rs/core/tests/suite/turn_state.rs b/codex-rs/core/tests/suite/turn_state.rs new file mode 100644 index 0000000000..3198f07cd6 --- /dev/null +++ b/codex-rs/core/tests/suite/turn_state.rs @@ -0,0 +1,122 @@ +#![allow(clippy::expect_used, clippy::unwrap_used)] + +use anyhow::Result; +use core_test_support::responses::WebSocketConnectionConfig; +use core_test_support::responses::ev_assistant_message; +use core_test_support::responses::ev_completed; +use core_test_support::responses::ev_done; +use core_test_support::responses::ev_reasoning_item; +use core_test_support::responses::ev_response_created; +use core_test_support::responses::ev_shell_command_call; +use core_test_support::responses::mount_response_sequence; +use core_test_support::responses::sse; +use core_test_support::responses::sse_response; +use core_test_support::responses::start_mock_server; +use core_test_support::responses::start_websocket_server_with_headers; +use core_test_support::skip_if_no_network; +use core_test_support::test_codex::test_codex; +use pretty_assertions::assert_eq; + +const TURN_STATE_HEADER: &str = "x-codex-turn-state"; + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn responses_turn_state_persists_within_turn_and_resets_after() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let call_id = "shell-turn-state"; + + let first_response = sse(vec![ + ev_response_created("resp-1"), + ev_reasoning_item("rsn-1", &["thinking"], &[]), + ev_shell_command_call(call_id, "echo turn-state"), + ev_completed("resp-1"), + ]); + let second_response = sse(vec![ + ev_response_created("resp-2"), + ev_assistant_message("msg-1", "done"), + ev_completed("resp-2"), + ]); + let third_response = sse(vec![ + ev_response_created("resp-3"), + ev_assistant_message("msg-2", "done"), + ev_completed("resp-3"), + ]); + + // First response sets turn_state; follow-up request in the same turn should echo it. + let responses = vec![ + sse_response(first_response).insert_header(TURN_STATE_HEADER, "ts-1"), + sse_response(second_response), + sse_response(third_response), + ]; + let request_log = mount_response_sequence(&server, responses).await; + + let test = test_codex().build(&server).await?; + test.submit_turn("run a shell command").await?; + test.submit_turn("second turn").await?; + + let requests = request_log.requests(); + assert_eq!(requests.len(), 3); + // Initial turn request has no header; follow-up has it; next turn clears it. + assert_eq!(requests[0].header(TURN_STATE_HEADER), None); + assert_eq!( + requests[1].header(TURN_STATE_HEADER), + Some("ts-1".to_string()) + ); + assert_eq!(requests[2].header(TURN_STATE_HEADER), None); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn websocket_turn_state_persists_within_turn_and_resets_after() -> Result<()> { + skip_if_no_network!(Ok(())); + + let call_id = "ws-shell-turn-state"; + // First connection delivers turn_state; second (same turn) must send it; third (new turn) must not. + let server = start_websocket_server_with_headers(vec![ + WebSocketConnectionConfig { + requests: vec![vec![ + ev_response_created("resp-1"), + ev_reasoning_item("rsn-1", &["thinking"], &[]), + ev_shell_command_call(call_id, "echo websocket"), + ev_done(), + ]], + response_headers: vec![(TURN_STATE_HEADER.to_string(), "ts-1".to_string())], + }, + WebSocketConnectionConfig { + requests: vec![vec![ + ev_response_created("resp-2"), + ev_assistant_message("msg-1", "done"), + ev_completed("resp-2"), + ]], + response_headers: Vec::new(), + }, + WebSocketConnectionConfig { + requests: vec![vec![ + ev_response_created("resp-3"), + ev_assistant_message("msg-2", "done"), + ev_completed("resp-3"), + ]], + response_headers: Vec::new(), + }, + ]) + .await; + + let mut builder = test_codex(); + let test = builder.build_with_websocket_server(&server).await?; + test.submit_turn("run the echo command").await?; + test.submit_turn("second turn").await?; + + let handshakes = server.handshakes(); + assert_eq!(handshakes.len(), 3); + assert_eq!(handshakes[0].header(TURN_STATE_HEADER), None); + assert_eq!( + handshakes[1].header(TURN_STATE_HEADER), + Some("ts-1".to_string()) + ); + assert_eq!(handshakes[2].header(TURN_STATE_HEADER), None); + + server.shutdown().await; + Ok(()) +} From e650d4b02c690751123b02f42330a848644c8484 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Fri, 16 Jan 2026 18:33:14 +0100 Subject: [PATCH 15/32] feat: tool call duration metric (#9364) --- codex-rs/otel/src/traces/otel_manager.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/codex-rs/otel/src/traces/otel_manager.rs b/codex-rs/otel/src/traces/otel_manager.rs index 2b490bbb37..a1aaf0b513 100644 --- a/codex-rs/otel/src/traces/otel_manager.rs +++ b/codex-rs/otel/src/traces/otel_manager.rs @@ -446,6 +446,11 @@ impl OtelManager { 1, &[("tool", tool_name), ("success", success_str)], ); + self.record_duration( + "codex.tool.call.duration_ms", + duration, + &[("tool", tool_name), ("success", success_str)], + ); tracing::event!( tracing::Level::INFO, event.name = "codex.tool_result", From f89a40a849f18e897192baa522dcb59c23771a06 Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Fri, 16 Jan 2026 11:12:52 -0800 Subject: [PATCH 16/32] chore: upgrade to Rust 1.92.0 (#8860) **Summary** - Upgrade Rust toolchain used by CI to 1.92.0. - Address new clippy `derivable_impls` warnings by deriving `Default` for enums across protocol, core, backend openapi models, and windows-sandbox setup. - Tidy up related test/config behavior (originator header handling, env override cleanup) and remove a now-unused assignment in TUI/TUI2 render layout. **Testing** - `just fmt` - `just fix -p codex-tui` - `just fix -p codex-tui2` - `just fix -p codex-windows-sandbox` - `cargo test -p codex-tui` - `cargo test -p codex-tui2` - `cargo test -p codex-windows-sandbox` - `cargo test -p codex-core --test all` - `cargo test -p codex-app-server --test all` - `cargo test -p codex-mcp-server --test all` - `cargo test --all-features` --- .github/workflows/rust-ci.yml | 8 ++++---- .github/workflows/rust-release.yml | 2 +- .github/workflows/sdk.yml | 2 +- .github/workflows/shell-tool-mcp.yml | 2 +- .../src/models/rate_limit_status_payload.rs | 11 ++++------- codex-rs/core/src/config/types.rs | 9 ++------- codex-rs/core/src/shell.rs | 13 +++++++------ codex-rs/core/src/tools/handlers/read_file.rs | 9 ++------- codex-rs/protocol/src/protocol.rs | 11 ++++------- codex-rs/rust-toolchain.toml | 2 +- codex-rs/scripts/setup-windows.ps1 | 2 +- codex-rs/tui/src/render/renderable.rs | 1 - codex-rs/tui2/src/render/renderable.rs | 1 - codex-rs/windows-sandbox-rs/src/setup_main_win.rs | 9 ++------- 14 files changed, 30 insertions(+), 52 deletions(-) diff --git a/.github/workflows/rust-ci.yml b/.github/workflows/rust-ci.yml index 49c2e6c771..22a1a8320e 100644 --- a/.github/workflows/rust-ci.yml +++ b/.github/workflows/rust-ci.yml @@ -59,7 +59,7 @@ jobs: working-directory: codex-rs steps: - uses: actions/checkout@v6 - - uses: dtolnay/rust-toolchain@1.90 + - uses: dtolnay/rust-toolchain@1.92 with: components: rustfmt - name: cargo fmt @@ -77,7 +77,7 @@ jobs: working-directory: codex-rs steps: - uses: actions/checkout@v6 - - uses: dtolnay/rust-toolchain@1.90 + - uses: dtolnay/rust-toolchain@1.92 - uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2 with: tool: cargo-shear @@ -177,7 +177,7 @@ jobs: steps: - uses: actions/checkout@v6 - - uses: dtolnay/rust-toolchain@1.90 + - uses: dtolnay/rust-toolchain@1.92 with: targets: ${{ matrix.target }} components: clippy @@ -416,7 +416,7 @@ jobs: - name: Install DotSlash uses: facebook/install-dotslash@v2 - - uses: dtolnay/rust-toolchain@1.90 + - uses: dtolnay/rust-toolchain@1.92 with: targets: ${{ matrix.target }} diff --git a/.github/workflows/rust-release.yml b/.github/workflows/rust-release.yml index 76f49eeabb..acc8b6bafe 100644 --- a/.github/workflows/rust-release.yml +++ b/.github/workflows/rust-release.yml @@ -80,7 +80,7 @@ jobs: steps: - uses: actions/checkout@v6 - - uses: dtolnay/rust-toolchain@1.90 + - uses: dtolnay/rust-toolchain@1.92 with: targets: ${{ matrix.target }} diff --git a/.github/workflows/sdk.yml b/.github/workflows/sdk.yml index 3e5a249d40..f506dce6ba 100644 --- a/.github/workflows/sdk.yml +++ b/.github/workflows/sdk.yml @@ -24,7 +24,7 @@ jobs: node-version: 22 cache: pnpm - - uses: dtolnay/rust-toolchain@1.90 + - uses: dtolnay/rust-toolchain@1.92 - name: build codex run: cargo build --bin codex diff --git a/.github/workflows/shell-tool-mcp.yml b/.github/workflows/shell-tool-mcp.yml index b27b4bdbd4..bc49a90fa6 100644 --- a/.github/workflows/shell-tool-mcp.yml +++ b/.github/workflows/shell-tool-mcp.yml @@ -93,7 +93,7 @@ jobs: - name: Checkout repository uses: actions/checkout@v6 - - uses: dtolnay/rust-toolchain@1.90 + - uses: dtolnay/rust-toolchain@1.92 with: targets: ${{ matrix.target }} diff --git a/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs index 0f5caf52ff..daf8e6af39 100644 --- a/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs +++ b/codex-rs/codex-backend-openapi-models/src/models/rate_limit_status_payload.rs @@ -42,9 +42,12 @@ impl RateLimitStatusPayload { } } -#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +#[derive( + Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Default, +)] pub enum PlanType { #[serde(rename = "guest")] + #[default] Guest, #[serde(rename = "free")] Free, @@ -71,9 +74,3 @@ pub enum PlanType { #[serde(rename = "edu")] Edu, } - -impl Default for PlanType { - fn default() -> PlanType { - Self::Guest - } -} diff --git a/codex-rs/core/src/config/types.rs b/codex-rs/core/src/config/types.rs index 981f9b541a..9d75302cbd 100644 --- a/codex-rs/core/src/config/types.rs +++ b/codex-rs/core/src/config/types.rs @@ -423,10 +423,11 @@ impl Default for Notifications { /// Terminals generally encode both mouse wheels and trackpads as the same "scroll up/down" mouse /// button events, without a magnitude. This setting controls whether Codex uses a heuristic to /// infer wheel vs trackpad per stream, or forces a specific behavior. -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, Default)] #[serde(rename_all = "snake_case")] pub enum ScrollInputMode { /// Infer wheel vs trackpad behavior per scroll stream. + #[default] Auto, /// Always treat scroll events as mouse-wheel input (fixed lines per tick). Wheel, @@ -434,12 +435,6 @@ pub enum ScrollInputMode { Trackpad, } -impl Default for ScrollInputMode { - fn default() -> Self { - Self::Auto - } -} - /// Collection of settings that are specific to the TUI. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[schemars(deny_unknown_fields)] diff --git a/codex-rs/core/src/shell.rs b/codex-rs/core/src/shell.rs index d22b6543a9..b6adf861d8 100644 --- a/codex-rs/core/src/shell.rs +++ b/codex-rs/core/src/shell.rs @@ -340,6 +340,7 @@ mod detect_shell_type_tests { #[cfg(unix)] mod tests { use super::*; + use std::path::Path; use std::path::PathBuf; use std::process::Command; @@ -350,7 +351,7 @@ mod tests { let shell_path = zsh_shell.shell_path; - assert_eq!(shell_path, PathBuf::from("/bin/zsh")); + assert_eq!(shell_path, Path::new("/bin/zsh")); } #[test] @@ -360,7 +361,7 @@ mod tests { let shell_path = zsh_shell.shell_path; - assert_eq!(shell_path, PathBuf::from("/bin/zsh")); + assert_eq!(shell_path, Path::new("/bin/zsh")); } #[test] @@ -369,9 +370,9 @@ mod tests { let shell_path = bash_shell.shell_path; assert!( - shell_path == PathBuf::from("/bin/bash") - || shell_path == PathBuf::from("/usr/bin/bash") - || shell_path == PathBuf::from("/usr/local/bin/bash"), + shell_path == Path::new("/bin/bash") + || shell_path == Path::new("/usr/bin/bash") + || shell_path == Path::new("/usr/local/bin/bash"), "shell path: {shell_path:?}", ); } @@ -381,7 +382,7 @@ mod tests { let sh_shell = get_shell(ShellType::Sh, None).unwrap(); let shell_path = sh_shell.shell_path; assert!( - shell_path == PathBuf::from("/bin/sh") || shell_path == PathBuf::from("/usr/bin/sh"), + shell_path == Path::new("/bin/sh") || shell_path == Path::new("/usr/bin/sh"), "shell path: {shell_path:?}", ); } diff --git a/codex-rs/core/src/tools/handlers/read_file.rs b/codex-rs/core/src/tools/handlers/read_file.rs index 4f187540a6..eb5ce647ce 100644 --- a/codex-rs/core/src/tools/handlers/read_file.rs +++ b/codex-rs/core/src/tools/handlers/read_file.rs @@ -40,9 +40,10 @@ struct ReadFileArgs { indentation: Option, } -#[derive(Deserialize)] +#[derive(Deserialize, Default)] #[serde(rename_all = "snake_case")] enum ReadMode { + #[default] Slice, Indentation, } @@ -461,12 +462,6 @@ mod defaults { } } - impl Default for ReadMode { - fn default() -> Self { - Self::Slice - } - } - pub fn offset() -> usize { 1 } diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 7ab9099ae8..a08e30e226 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -1656,21 +1656,18 @@ pub struct ReviewLineRange { pub end: u32, } -#[derive(Debug, Clone, Copy, Display, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS)] +#[derive( + Debug, Clone, Copy, Display, Deserialize, Serialize, PartialEq, Eq, JsonSchema, TS, Default, +)] #[serde(rename_all = "snake_case")] pub enum ExecCommandSource { + #[default] Agent, UserShell, UnifiedExecStartup, UnifiedExecInteraction, } -impl Default for ExecCommandSource { - fn default() -> Self { - Self::Agent - } -} - #[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, TS)] pub struct ExecCommandBeginEvent { /// Identifier so this can be paired with the ExecCommandEnd event. diff --git a/codex-rs/rust-toolchain.toml b/codex-rs/rust-toolchain.toml index 7187a33189..05eeaac960 100644 --- a/codex-rs/rust-toolchain.toml +++ b/codex-rs/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.90.0" +channel = "1.92.0" components = ["clippy", "rustfmt", "rust-src"] diff --git a/codex-rs/scripts/setup-windows.ps1 b/codex-rs/scripts/setup-windows.ps1 index 554dc63352..33b6b0c352 100644 --- a/codex-rs/scripts/setup-windows.ps1 +++ b/codex-rs/scripts/setup-windows.ps1 @@ -179,7 +179,7 @@ if (-not (Ensure-Command 'cargo')) { Write-Host "==> Configuring Rust toolchain per rust-toolchain.toml" -ForegroundColor Cyan # Pin to the workspace toolchain and install components -$toolchain = '1.90.0' +$toolchain = '1.92.0' & rustup toolchain install $toolchain --profile minimal | Out-Host & rustup default $toolchain | Out-Host & rustup component add clippy rustfmt rust-src --toolchain $toolchain | Out-Host diff --git a/codex-rs/tui/src/render/renderable.rs b/codex-rs/tui/src/render/renderable.rs index 8581468c0a..eb514b3b0f 100644 --- a/codex-rs/tui/src/render/renderable.rs +++ b/codex-rs/tui/src/render/renderable.rs @@ -275,7 +275,6 @@ impl<'a> FlexRenderable<'a> { }; let child_size = child.desired_height(area.width).min(max_child_extent); child_sizes[i] = child_size; - allocated_size += child_size; allocated_flex_space += child_size; } } diff --git a/codex-rs/tui2/src/render/renderable.rs b/codex-rs/tui2/src/render/renderable.rs index 8581468c0a..eb514b3b0f 100644 --- a/codex-rs/tui2/src/render/renderable.rs +++ b/codex-rs/tui2/src/render/renderable.rs @@ -275,7 +275,6 @@ impl<'a> FlexRenderable<'a> { }; let child_size = child.desired_height(area.width).min(max_child_extent); child_sizes[i] = child_size; - allocated_size += child_size; allocated_flex_space += child_size; } } diff --git a/codex-rs/windows-sandbox-rs/src/setup_main_win.rs b/codex-rs/windows-sandbox-rs/src/setup_main_win.rs index e1b914859e..52e60a1da7 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_main_win.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_main_win.rs @@ -76,19 +76,14 @@ struct Payload { refresh_only: bool, } -#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)] #[serde(rename_all = "kebab-case")] enum SetupMode { + #[default] Full, ReadAclsOnly, } -impl Default for SetupMode { - fn default() -> Self { - Self::Full - } -} - fn log_line(log: &mut File, msg: &str) -> Result<()> { let ts = chrono::Utc::now().to_rfc3339(); writeln!(log, "[{ts}] {msg}")?; From e893e83eb9b5209ab4ac21206d6b9b86bf8ec5d2 Mon Sep 17 00:00:00 2001 From: Anton Panasenko Date: Fri, 16 Jan 2026 11:28:52 -0800 Subject: [PATCH 17/32] feat: /fork the current session instead of opening session picker (#9385) Implemented /fork to fork the current session directly (no picker), handling it via a new ForkCurrentSession app event in both tui and tui2. Updated slash command descriptions/tooltips and adjusted the fork tests accordingly. Removed the unused in-session fork picker event. --- codex-rs/tui/src/app.rs | 110 ++++++++++++-------------- codex-rs/tui/src/app_event.rs | 4 +- codex-rs/tui/src/chatwidget.rs | 2 +- codex-rs/tui/src/chatwidget/tests.rs | 4 +- codex-rs/tui/src/slash_command.rs | 2 +- codex-rs/tui/tooltips.txt | 2 +- codex-rs/tui2/src/app.rs | 110 ++++++++++++-------------- codex-rs/tui2/src/app_event.rs | 4 +- codex-rs/tui2/src/chatwidget.rs | 2 +- codex-rs/tui2/src/chatwidget/tests.rs | 4 +- codex-rs/tui2/src/slash_command.rs | 2 +- codex-rs/tui2/tooltips.txt | 2 +- 12 files changed, 113 insertions(+), 135 deletions(-) diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 87d76909de..adc48c1d12 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -758,73 +758,61 @@ impl App { // Leaving alt-screen may blank the inline viewport; force a redraw either way. tui.frame_requester().schedule_frame(); } - AppEvent::OpenForkPicker => { - match crate::resume_picker::run_fork_picker( - tui, - &self.config.codex_home, - &self.config.model_provider_id, - false, - ) - .await? - { - SessionSelection::Fork(path) => { - let summary = session_summary( - self.chat_widget.token_usage(), - self.chat_widget.thread_id(), - ); - match self - .server - .fork_thread(usize::MAX, self.config.clone(), path.clone()) - .await - { - Ok(forked) => { - self.shutdown_current_thread().await; - let init = crate::chatwidget::ChatWidgetInit { - config: self.config.clone(), - frame_requester: tui.frame_requester(), - app_event_tx: self.app_event_tx.clone(), - initial_prompt: None, - initial_images: Vec::new(), - enhanced_keys_supported: self.enhanced_keys_supported, - auth_manager: self.auth_manager.clone(), - models_manager: self.server.get_models_manager(), - feedback: self.feedback.clone(), - is_first_run: false, - model: Some(self.current_model.clone()), - }; - self.chat_widget = ChatWidget::new_from_existing( - init, - forked.thread, - forked.session_configured, - ); - self.current_model = model_info.slug.clone(); - if let Some(summary) = summary { - let mut lines: Vec> = - vec![summary.usage_line.clone().into()]; - if let Some(command) = summary.resume_command { - let spans = vec![ - "To continue this session, run ".into(), - command.cyan(), - ]; - lines.push(spans.into()); - } - self.chat_widget.add_plain_history_lines(lines); + AppEvent::ForkCurrentSession => { + let summary = + session_summary(self.chat_widget.token_usage(), self.chat_widget.thread_id()); + if let Some(path) = self.chat_widget.rollout_path() { + match self + .server + .fork_thread(usize::MAX, self.config.clone(), path.clone()) + .await + { + Ok(forked) => { + self.shutdown_current_thread().await; + let init = crate::chatwidget::ChatWidgetInit { + config: self.config.clone(), + frame_requester: tui.frame_requester(), + app_event_tx: self.app_event_tx.clone(), + initial_prompt: None, + initial_images: Vec::new(), + enhanced_keys_supported: self.enhanced_keys_supported, + auth_manager: self.auth_manager.clone(), + models_manager: self.server.get_models_manager(), + feedback: self.feedback.clone(), + is_first_run: false, + model: Some(self.current_model.clone()), + }; + self.chat_widget = ChatWidget::new_from_existing( + init, + forked.thread, + forked.session_configured, + ); + self.current_model = model_info.slug.clone(); + if let Some(summary) = summary { + let mut lines: Vec> = + vec![summary.usage_line.clone().into()]; + if let Some(command) = summary.resume_command { + let spans = vec![ + "To continue this session, run ".into(), + command.cyan(), + ]; + lines.push(spans.into()); } - } - Err(err) => { - let path_display = path.display(); - self.chat_widget.add_error_message(format!( - "Failed to fork session from {path_display}: {err}" - )); + self.chat_widget.add_plain_history_lines(lines); } } + Err(err) => { + let path_display = path.display(); + self.chat_widget.add_error_message(format!( + "Failed to fork current session from {path_display}: {err}" + )); + } } - SessionSelection::Exit - | SessionSelection::StartFresh - | SessionSelection::Resume(_) => {} + } else { + self.chat_widget + .add_error_message("Current session is not ready to fork yet.".to_string()); } - // Leaving alt-screen may blank the inline viewport; force a redraw either way. tui.frame_requester().schedule_frame(); } AppEvent::InsertHistoryCell(cell) => { diff --git a/codex-rs/tui/src/app_event.rs b/codex-rs/tui/src/app_event.rs index 785f3ae2ce..f34e1d7d5a 100644 --- a/codex-rs/tui/src/app_event.rs +++ b/codex-rs/tui/src/app_event.rs @@ -53,8 +53,8 @@ pub(crate) enum AppEvent { /// Open the resume picker inside the running TUI session. OpenResumePicker, - /// Open the fork picker inside the running TUI session. - OpenForkPicker, + /// Fork the current session into a new thread. + ForkCurrentSession, /// Request to exit the application. /// diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 3d566356e2..d301243ca7 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -1994,7 +1994,7 @@ impl ChatWidget { self.app_event_tx.send(AppEvent::OpenResumePicker); } SlashCommand::Fork => { - self.app_event_tx.send(AppEvent::OpenForkPicker); + self.app_event_tx.send(AppEvent::ForkCurrentSession); } SlashCommand::Init => { let init_target = self.config.cwd.join(DEFAULT_PROJECT_DOC_FILENAME); diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index 40d30c8956..6b216e6d76 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -1532,12 +1532,12 @@ async fn slash_resume_opens_picker() { } #[tokio::test] -async fn slash_fork_opens_picker() { +async fn slash_fork_requests_current_fork() { let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(None).await; chat.dispatch_command(SlashCommand::Fork); - assert_matches!(rx.try_recv(), Ok(AppEvent::OpenForkPicker)); + assert_matches!(rx.try_recv(), Ok(AppEvent::ForkCurrentSession)); } #[tokio::test] diff --git a/codex-rs/tui/src/slash_command.rs b/codex-rs/tui/src/slash_command.rs index 0d274f8eb5..d7cbd7fc1d 100644 --- a/codex-rs/tui/src/slash_command.rs +++ b/codex-rs/tui/src/slash_command.rs @@ -48,7 +48,7 @@ impl SlashCommand { SlashCommand::Compact => "summarize conversation to prevent hitting the context limit", SlashCommand::Review => "review my current changes and find issues", SlashCommand::Resume => "resume a saved chat", - SlashCommand::Fork => "fork a saved chat", + SlashCommand::Fork => "fork the current chat", // SlashCommand::Undo => "ask Codex to undo a turn", SlashCommand::Quit | SlashCommand::Exit => "exit Codex", SlashCommand::Diff => "show git diff (including untracked files)", diff --git a/codex-rs/tui/tooltips.txt b/codex-rs/tui/tooltips.txt index 3ba7931ff1..302a2b1ea5 100644 --- a/codex-rs/tui/tooltips.txt +++ b/codex-rs/tui/tooltips.txt @@ -6,7 +6,7 @@ Use /approvals to control when Codex asks for confirmation. Run /review to get a code review of your current changes. Use /skills to list available skills or ask Codex to use one. Use /status to see the current model, approvals, and token usage. -Use /fork to branch a saved chat into a new thread. +Use /fork to branch the current chat into a new thread. Use /init to create an AGENTS.md with project-specific guidance. Use /mcp to list configured MCP tools. You can run any shell command from Codex using `!` (e.g. `!ls`) diff --git a/codex-rs/tui2/src/app.rs b/codex-rs/tui2/src/app.rs index cd174fbbbd..52bf416419 100644 --- a/codex-rs/tui2/src/app.rs +++ b/codex-rs/tui2/src/app.rs @@ -1531,72 +1531,62 @@ impl App { // Leaving alt-screen may blank the inline viewport; force a redraw either way. tui.frame_requester().schedule_frame(); } - AppEvent::OpenForkPicker => { - match crate::resume_picker::run_fork_picker( - tui, - &self.config.codex_home, - &self.config.model_provider_id, - false, - ) - .await? - { - SessionSelection::Fork(path) => { - let summary = session_summary( - self.chat_widget.token_usage(), - self.chat_widget.conversation_id(), - ); - match self - .server - .fork_thread(usize::MAX, self.config.clone(), path.clone()) - .await - { - Ok(forked) => { - self.shutdown_current_conversation().await; - let init = crate::chatwidget::ChatWidgetInit { - config: self.config.clone(), - frame_requester: tui.frame_requester(), - app_event_tx: self.app_event_tx.clone(), - initial_prompt: None, - initial_images: Vec::new(), - enhanced_keys_supported: self.enhanced_keys_supported, - auth_manager: self.auth_manager.clone(), - models_manager: self.server.get_models_manager(), - feedback: self.feedback.clone(), - is_first_run: false, - model: Some(self.current_model.clone()), - }; - self.chat_widget = ChatWidget::new_from_existing( - init, - forked.thread, - forked.session_configured, - ); - if let Some(summary) = summary { - let mut lines: Vec> = - vec![summary.usage_line.clone().into()]; - if let Some(command) = summary.resume_command { - let spans = vec![ - "To continue this session, run ".into(), - command.cyan(), - ]; - lines.push(spans.into()); - } - self.chat_widget.add_plain_history_lines(lines); + AppEvent::ForkCurrentSession => { + let summary = session_summary( + self.chat_widget.token_usage(), + self.chat_widget.conversation_id(), + ); + if let Some(path) = self.chat_widget.rollout_path() { + match self + .server + .fork_thread(usize::MAX, self.config.clone(), path.clone()) + .await + { + Ok(forked) => { + self.shutdown_current_conversation().await; + let init = crate::chatwidget::ChatWidgetInit { + config: self.config.clone(), + frame_requester: tui.frame_requester(), + app_event_tx: self.app_event_tx.clone(), + initial_prompt: None, + initial_images: Vec::new(), + enhanced_keys_supported: self.enhanced_keys_supported, + auth_manager: self.auth_manager.clone(), + models_manager: self.server.get_models_manager(), + feedback: self.feedback.clone(), + is_first_run: false, + model: Some(self.current_model.clone()), + }; + self.chat_widget = ChatWidget::new_from_existing( + init, + forked.thread, + forked.session_configured, + ); + if let Some(summary) = summary { + let mut lines: Vec> = + vec![summary.usage_line.clone().into()]; + if let Some(command) = summary.resume_command { + let spans = vec![ + "To continue this session, run ".into(), + command.cyan(), + ]; + lines.push(spans.into()); } - } - Err(err) => { - let path_display = path.display(); - self.chat_widget.add_error_message(format!( - "Failed to fork session from {path_display}: {err}" - )); + self.chat_widget.add_plain_history_lines(lines); } } + Err(err) => { + let path_display = path.display(); + self.chat_widget.add_error_message(format!( + "Failed to fork current session from {path_display}: {err}" + )); + } } - SessionSelection::Exit - | SessionSelection::StartFresh - | SessionSelection::Resume(_) => {} + } else { + self.chat_widget + .add_error_message("Current session is not ready to fork yet.".to_string()); } - // Leaving alt-screen may blank the inline viewport; force a redraw either way. tui.frame_requester().schedule_frame(); } AppEvent::InsertHistoryCell(cell) => { diff --git a/codex-rs/tui2/src/app_event.rs b/codex-rs/tui2/src/app_event.rs index f24ca1e07c..b2a70cae19 100644 --- a/codex-rs/tui2/src/app_event.rs +++ b/codex-rs/tui2/src/app_event.rs @@ -47,8 +47,8 @@ pub(crate) enum AppEvent { /// Open the resume picker inside the running TUI session. OpenResumePicker, - /// Open the fork picker inside the running TUI session. - OpenForkPicker, + /// Fork the current session into a new thread. + ForkCurrentSession, /// Request to exit the application. /// diff --git a/codex-rs/tui2/src/chatwidget.rs b/codex-rs/tui2/src/chatwidget.rs index 5facd756f7..ab40b19c7b 100644 --- a/codex-rs/tui2/src/chatwidget.rs +++ b/codex-rs/tui2/src/chatwidget.rs @@ -1769,7 +1769,7 @@ impl ChatWidget { self.app_event_tx.send(AppEvent::OpenResumePicker); } SlashCommand::Fork => { - self.app_event_tx.send(AppEvent::OpenForkPicker); + self.app_event_tx.send(AppEvent::ForkCurrentSession); } SlashCommand::Init => { let init_target = self.config.cwd.join(DEFAULT_PROJECT_DOC_FILENAME); diff --git a/codex-rs/tui2/src/chatwidget/tests.rs b/codex-rs/tui2/src/chatwidget/tests.rs index 22871c39d7..fa555e597e 100644 --- a/codex-rs/tui2/src/chatwidget/tests.rs +++ b/codex-rs/tui2/src/chatwidget/tests.rs @@ -1338,12 +1338,12 @@ async fn slash_resume_opens_picker() { } #[tokio::test] -async fn slash_fork_opens_picker() { +async fn slash_fork_requests_current_fork() { let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(None).await; chat.dispatch_command(SlashCommand::Fork); - assert_matches!(rx.try_recv(), Ok(AppEvent::OpenForkPicker)); + assert_matches!(rx.try_recv(), Ok(AppEvent::ForkCurrentSession)); } #[tokio::test] diff --git a/codex-rs/tui2/src/slash_command.rs b/codex-rs/tui2/src/slash_command.rs index e2d7761221..caac37b85d 100644 --- a/codex-rs/tui2/src/slash_command.rs +++ b/codex-rs/tui2/src/slash_command.rs @@ -46,7 +46,7 @@ impl SlashCommand { SlashCommand::Compact => "summarize conversation to prevent hitting the context limit", SlashCommand::Review => "review my current changes and find issues", SlashCommand::Resume => "resume a saved chat", - SlashCommand::Fork => "fork a saved chat", + SlashCommand::Fork => "fork the current chat", // SlashCommand::Undo => "ask Codex to undo a turn", SlashCommand::Quit | SlashCommand::Exit => "exit Codex", SlashCommand::Diff => "show git diff (including untracked files)", diff --git a/codex-rs/tui2/tooltips.txt b/codex-rs/tui2/tooltips.txt index 3ba7931ff1..302a2b1ea5 100644 --- a/codex-rs/tui2/tooltips.txt +++ b/codex-rs/tui2/tooltips.txt @@ -6,7 +6,7 @@ Use /approvals to control when Codex asks for confirmation. Run /review to get a code review of your current changes. Use /skills to list available skills or ask Codex to use one. Use /status to see the current model, approvals, and token usage. -Use /fork to branch a saved chat into a new thread. +Use /fork to branch the current chat into a new thread. Use /init to create an AGENTS.md with project-specific guidance. Use /mcp to list configured MCP tools. You can run any shell command from Codex using `!` (e.g. `!ls`) From f1653dd4d369d411f35442b48293816e9a92fb0e Mon Sep 17 00:00:00 2001 From: Owen Lin Date: Fri, 16 Jan 2026 12:58:55 -0800 Subject: [PATCH 18/32] feat(app-server, core): return threads by created_at or updated_at (#9247) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for returning threads by either `created_at` OR `updated_at` descending. Previously core always returned threads ordered by `created_at`. This PR: - updates core to be able to list threads by `updated_at` OR `created_at` descending based on what the caller wants - also update `thread/list` in app-server to expose this (default to `created_at` if not specified) All existing codepaths (app-server, TUI) still default to `created_at`, so no behavior change is expected with this PR. **Implementation** To sort by `updated_at` is a bit nontrivial (whereas `created_at` is easy due to the way we structure the folders and filenames on disk, which are all based on `created_at`). The most naive way to do this without introducing a cache file or sqlite DB (which we have to implement/maintain) is to scan files in reverse `created_at` order on disk, and look at the file's mtime (last modified timestamp according to the filesystem) until we reach `MAX_SCAN_FILES` (currently set to 10,000). Then, we can return the most recent N threads. Based on some quick and dirty benchmarking on my machine with ~1000 rollout files, calling `thread/list` with limit 50, the `updated_at` path is slower as expected due to all the I/O: - updated-at: average 103.10 ms - created-at: average 41.10 ms Those absolute numbers aren't a big deal IMO, but we can certainly optimize this in a followup if needed by introducing more state stored on disk. **Caveat** There's also a limitation in that any files older than `MAX_SCAN_FILES` will be excluded, which means if a user continues a REALLY old thread, it's possible to not be included. In practice that should not be too big of an issue. If a user makes... - 1000 rollouts/day → threads older than 10 days won't show up - 100 rollouts/day → ~100 days If this becomes a problem for some reason, even more motivation to implement an updated_at cache. --- .../app-server-protocol/src/protocol/v1.rs | 1 + .../app-server-protocol/src/protocol/v2.rs | 13 + codex-rs/app-server/README.md | 8 +- .../app-server/src/codex_message_processor.rs | 84 ++- codex-rs/app-server/tests/common/lib.rs | 1 + codex-rs/app-server/tests/common/rollout.rs | 34 +- .../app-server/tests/suite/v2/thread_list.rs | 382 +++++++++++++ codex-rs/core/src/lib.rs | 1 + codex-rs/core/src/rollout/list.rs | 504 ++++++++++++++---- codex-rs/core/src/rollout/recorder.rs | 12 +- codex-rs/core/src/rollout/tests.rs | 88 ++- codex-rs/core/tests/suite/cli_stream.rs | 1 + codex-rs/debug-client/src/client.rs | 1 + codex-rs/exec/src/lib.rs | 3 + codex-rs/tui/src/lib.rs | 5 + codex-rs/tui/src/resume_picker.rs | 3 + codex-rs/tui2/src/lib.rs | 5 + codex-rs/tui2/src/resume_picker.rs | 3 + 18 files changed, 1022 insertions(+), 127 deletions(-) diff --git a/codex-rs/app-server-protocol/src/protocol/v1.rs b/codex-rs/app-server-protocol/src/protocol/v1.rs index a5bc69be5d..39090c022f 100644 --- a/codex-rs/app-server-protocol/src/protocol/v1.rs +++ b/codex-rs/app-server-protocol/src/protocol/v1.rs @@ -128,6 +128,7 @@ pub struct ConversationSummary { pub path: PathBuf, pub preview: String, pub timestamp: Option, + pub updated_at: Option, pub model_provider: String, pub cwd: PathBuf, pub cli_version: String, diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 8655d89c41..b44e1575ad 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -1182,11 +1182,21 @@ pub struct ThreadListParams { pub cursor: Option, /// Optional page size; defaults to a reasonable server-side value. pub limit: Option, + /// Optional sort key; defaults to created_at. + pub sort_key: Option, /// Optional provider filter; when set, only sessions recorded under these /// providers are returned. When present but empty, includes all providers. pub model_providers: Option>, } +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(export_to = "v2/")] +pub enum ThreadSortKey { + CreatedAt, + UpdatedAt, +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] @@ -1359,6 +1369,9 @@ pub struct Thread { /// Unix timestamp (in seconds) when the thread was created. #[ts(type = "number")] pub created_at: i64, + /// Unix timestamp (in seconds) when the thread was last updated. + #[ts(type = "number")] + pub updated_at: i64, /// [UNSTABLE] Path to the thread on disk. pub path: PathBuf, /// Working directory captured for the thread. diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 63f4dea965..75e1853d70 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -138,10 +138,11 @@ To branch from a stored session, call `thread/fork` with the `thread.id`. This c ### Example: List threads (with pagination & filters) -`thread/list` lets you render a history UI. Pass any combination of: +`thread/list` lets you render a history UI. Results default to `createdAt` (newest first) descending. Pass any combination of: - `cursor` — opaque string from a prior response; omit for the first page. - `limit` — server defaults to a reasonable page size if unset. +- `sortKey` — `created_at` (default) or `updated_at`. - `modelProviders` — restrict results to specific providers; unset, null, or an empty array will include all providers. Example: @@ -150,11 +151,12 @@ Example: { "method": "thread/list", "id": 20, "params": { "cursor": null, "limit": 25, + "sortKey": "created_at" } } { "id": 20, "result": { "data": [ - { "id": "thr_a", "preview": "Create a TUI", "modelProvider": "openai", "createdAt": 1730831111 }, - { "id": "thr_b", "preview": "Fix tests", "modelProvider": "openai", "createdAt": 1730750000 } + { "id": "thr_a", "preview": "Create a TUI", "modelProvider": "openai", "createdAt": 1730831111, "updatedAt": 1730831111 }, + { "id": "thr_b", "preview": "Fix tests", "modelProvider": "openai", "createdAt": 1730750000, "updatedAt": 1730750000 } ], "nextCursor": "opaque-token-or-null" } } diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index fc7115447d..153028512e 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -6,6 +6,7 @@ use crate::models::supported_models; use crate::outgoing_message::OutgoingMessageSender; use crate::outgoing_message::OutgoingNotification; use chrono::DateTime; +use chrono::SecondsFormat; use chrono::Utc; use codex_app_server_protocol::Account; use codex_app_server_protocol::AccountLoginCompletedNotification; @@ -99,6 +100,7 @@ use codex_app_server_protocol::ThreadLoadedListResponse; use codex_app_server_protocol::ThreadResumeParams; use codex_app_server_protocol::ThreadResumeResponse; use codex_app_server_protocol::ThreadRollbackParams; +use codex_app_server_protocol::ThreadSortKey; use codex_app_server_protocol::ThreadStartParams; use codex_app_server_protocol::ThreadStartResponse; use codex_app_server_protocol::ThreadStartedNotification; @@ -123,6 +125,7 @@ use codex_core::NewThread; use codex_core::RolloutRecorder; use codex_core::SessionMeta; use codex_core::ThreadManager; +use codex_core::ThreadSortKey as CoreThreadSortKey; use codex_core::auth::CLIENT_ID; use codex_core::auth::login_with_api_key; use codex_core::config::Config; @@ -1598,6 +1601,7 @@ impl CodexMessageProcessor { let ThreadListParams { cursor, limit, + sort_key, model_providers, } = params; @@ -1605,8 +1609,12 @@ impl CodexMessageProcessor { .map(|value| value as usize) .unwrap_or(THREAD_LIST_DEFAULT_LIMIT) .clamp(1, THREAD_LIST_MAX_LIMIT); + let core_sort_key = match sort_key.unwrap_or(ThreadSortKey::CreatedAt) { + ThreadSortKey::CreatedAt => CoreThreadSortKey::CreatedAt, + ThreadSortKey::UpdatedAt => CoreThreadSortKey::UpdatedAt, + }; let (summaries, next_cursor) = match self - .list_threads_common(requested_page_size, cursor, model_providers) + .list_threads_common(requested_page_size, cursor, model_providers, core_sort_key) .await { Ok(r) => r, @@ -2171,7 +2179,12 @@ impl CodexMessageProcessor { .clamp(1, THREAD_LIST_MAX_LIMIT); match self - .list_threads_common(requested_page_size, cursor, model_providers) + .list_threads_common( + requested_page_size, + cursor, + model_providers, + CoreThreadSortKey::UpdatedAt, + ) .await { Ok((items, next_cursor)) => { @@ -2189,8 +2202,18 @@ impl CodexMessageProcessor { requested_page_size: usize, cursor: Option, model_providers: Option>, + sort_key: CoreThreadSortKey, ) -> Result<(Vec, Option), JSONRPCErrorError> { - let mut cursor_obj: Option = cursor.as_ref().and_then(|s| parse_cursor(s)); + let mut cursor_obj: Option = match cursor.as_ref() { + Some(cursor_str) => { + Some(parse_cursor(cursor_str).ok_or_else(|| JSONRPCErrorError { + code: INVALID_REQUEST_ERROR_CODE, + message: format!("invalid cursor: {cursor_str}"), + data: None, + })?) + } + None => None, + }; let mut last_cursor = cursor_obj.clone(); let mut remaining = requested_page_size; let mut items = Vec::with_capacity(requested_page_size); @@ -2214,6 +2237,7 @@ impl CodexMessageProcessor { &self.config.codex_home, page_size, cursor_obj.as_ref(), + sort_key, INTERACTIVE_SESSION_SOURCES, model_provider_filter.as_deref(), fallback_provider.as_str(), @@ -2229,6 +2253,7 @@ impl CodexMessageProcessor { .items .into_iter() .filter_map(|it| { + let updated_at = it.updated_at.clone(); let session_meta_line = it.head.first().and_then(|first| { serde_json::from_value::(first.clone()).ok() })?; @@ -2238,6 +2263,7 @@ impl CodexMessageProcessor { &session_meta_line.meta, session_meta_line.git.as_ref(), fallback_provider.as_str(), + updated_at, ) }) .collect::>(); @@ -3982,12 +4008,19 @@ pub(crate) async fn read_summary_from_rollout( git, } = session_meta_line; + let created_at = if session_meta.timestamp.is_empty() { + None + } else { + Some(session_meta.timestamp.as_str()) + }; + let updated_at = read_updated_at(path, created_at).await; if let Some(summary) = extract_conversation_summary( path.to_path_buf(), &head, &session_meta, git.as_ref(), fallback_provider, + updated_at.clone(), ) { return Ok(summary); } @@ -4002,10 +4035,12 @@ pub(crate) async fn read_summary_from_rollout( .clone() .unwrap_or_else(|| fallback_provider.to_string()); let git_info = git.as_ref().map(map_git_info); + let updated_at = updated_at.or_else(|| timestamp.clone()); Ok(ConversationSummary { conversation_id: session_meta.id, timestamp, + updated_at, path: path.to_path_buf(), preview: String::new(), model_provider, @@ -4040,6 +4075,7 @@ fn extract_conversation_summary( session_meta: &SessionMeta, git: Option<&CoreGitInfo>, fallback_provider: &str, + updated_at: Option, ) -> Option { let preview = head .iter() @@ -4065,10 +4101,12 @@ fn extract_conversation_summary( .clone() .unwrap_or_else(|| fallback_provider.to_string()); let git_info = git.map(map_git_info); + let updated_at = updated_at.or_else(|| timestamp.clone()); Some(ConversationSummary { conversation_id, timestamp, + updated_at, path, preview: preview.to_string(), model_provider, @@ -4095,12 +4133,25 @@ fn parse_datetime(timestamp: Option<&str>) -> Option> { }) } +async fn read_updated_at(path: &Path, created_at: Option<&str>) -> Option { + let updated_at = tokio::fs::metadata(path) + .await + .ok() + .and_then(|meta| meta.modified().ok()) + .map(|modified| { + let updated_at: DateTime = modified.into(); + updated_at.to_rfc3339_opts(SecondsFormat::Secs, true) + }); + updated_at.or_else(|| created_at.map(str::to_string)) +} + pub(crate) fn summary_to_thread(summary: ConversationSummary) -> Thread { let ConversationSummary { conversation_id, path, preview, timestamp, + updated_at, model_provider, cwd, cli_version, @@ -4109,6 +4160,7 @@ pub(crate) fn summary_to_thread(summary: ConversationSummary) -> Thread { } = summary; let created_at = parse_datetime(timestamp.as_deref()); + let updated_at = parse_datetime(updated_at.as_deref()).or(created_at); let git_info = git_info.map(|info| ApiGitInfo { sha: info.sha, branch: info.branch, @@ -4120,6 +4172,7 @@ pub(crate) fn summary_to_thread(summary: ConversationSummary) -> Thread { preview, model_provider, created_at: created_at.map(|dt| dt.timestamp()).unwrap_or(0), + updated_at: updated_at.map(|dt| dt.timestamp()).unwrap_or(0), path, cwd, cli_version, @@ -4174,13 +4227,20 @@ mod tests { let session_meta = serde_json::from_value::(head[0].clone())?; - let summary = - extract_conversation_summary(path.clone(), &head, &session_meta, None, "test-provider") - .expect("summary"); + let summary = extract_conversation_summary( + path.clone(), + &head, + &session_meta, + None, + "test-provider", + timestamp.clone(), + ) + .expect("summary"); let expected = ConversationSummary { conversation_id, - timestamp, + timestamp: timestamp.clone(), + updated_at: timestamp, path, preview: "Count to 5".to_string(), model_provider: "test-provider".to_string(), @@ -4200,6 +4260,7 @@ mod tests { use codex_protocol::protocol::RolloutLine; use codex_protocol::protocol::SessionMetaLine; use std::fs; + use std::fs::FileTimes; let temp_dir = TempDir::new()?; let path = temp_dir.path().join("rollout.jsonl"); @@ -4223,12 +4284,19 @@ mod tests { }; fs::write(&path, format!("{}\n", serde_json::to_string(&line)?))?; + let parsed = chrono::DateTime::parse_from_rfc3339(×tamp)?.with_timezone(&Utc); + let times = FileTimes::new().set_modified(parsed.into()); + std::fs::OpenOptions::new() + .append(true) + .open(&path)? + .set_times(times)?; let summary = read_summary_from_rollout(path.as_path(), "fallback").await?; let expected = ConversationSummary { conversation_id, - timestamp: Some(timestamp), + timestamp: Some(timestamp.clone()), + updated_at: Some("2025-09-05T16:53:11Z".to_string()), path: path.clone(), preview: String::new(), model_provider: "fallback".to_string(), diff --git a/codex-rs/app-server/tests/common/lib.rs b/codex-rs/app-server/tests/common/lib.rs index 48577db110..d350a5df82 100644 --- a/codex-rs/app-server/tests/common/lib.rs +++ b/codex-rs/app-server/tests/common/lib.rs @@ -30,6 +30,7 @@ pub use responses::create_final_assistant_message_sse_response; pub use responses::create_shell_command_sse_response; pub use rollout::create_fake_rollout; pub use rollout::create_fake_rollout_with_text_elements; +pub use rollout::rollout_path; use serde::de::DeserializeOwned; pub fn to_response(response: JSONRPCResponse) -> anyhow::Result { diff --git a/codex-rs/app-server/tests/common/rollout.rs b/codex-rs/app-server/tests/common/rollout.rs index 784b79b98d..31d4356638 100644 --- a/codex-rs/app-server/tests/common/rollout.rs +++ b/codex-rs/app-server/tests/common/rollout.rs @@ -6,10 +6,23 @@ use codex_protocol::protocol::SessionMetaLine; use codex_protocol::protocol::SessionSource; use serde_json::json; use std::fs; +use std::fs::FileTimes; use std::path::Path; use std::path::PathBuf; use uuid::Uuid; +pub fn rollout_path(codex_home: &Path, filename_ts: &str, thread_id: &str) -> PathBuf { + let year = &filename_ts[0..4]; + let month = &filename_ts[5..7]; + let day = &filename_ts[8..10]; + codex_home + .join("sessions") + .join(year) + .join(month) + .join(day) + .join(format!("rollout-{filename_ts}-{thread_id}.jsonl")) +} + /// Create a minimal rollout file under `CODEX_HOME/sessions/YYYY/MM/DD/`. /// /// - `filename_ts` is the filename timestamp component in `YYYY-MM-DDThh-mm-ss` format. @@ -30,14 +43,11 @@ pub fn create_fake_rollout( let uuid_str = uuid.to_string(); let conversation_id = ThreadId::from_string(&uuid_str)?; - // sessions/YYYY/MM/DD derived from filename_ts (YYYY-MM-DDThh-mm-ss) - let year = &filename_ts[0..4]; - let month = &filename_ts[5..7]; - let day = &filename_ts[8..10]; - let dir = codex_home.join("sessions").join(year).join(month).join(day); - fs::create_dir_all(&dir)?; - - let file_path = dir.join(format!("rollout-{filename_ts}-{uuid}.jsonl")); + let file_path = rollout_path(codex_home, filename_ts, &uuid_str); + let dir = file_path + .parent() + .ok_or_else(|| anyhow::anyhow!("missing rollout parent directory"))?; + fs::create_dir_all(dir)?; // Build JSONL lines let meta = SessionMeta { @@ -84,7 +94,13 @@ pub fn create_fake_rollout( .to_string(), ]; - fs::write(file_path, lines.join("\n") + "\n")?; + fs::write(&file_path, lines.join("\n") + "\n")?; + let parsed = chrono::DateTime::parse_from_rfc3339(meta_rfc3339)?.with_timezone(&chrono::Utc); + let times = FileTimes::new().set_modified(parsed.into()); + std::fs::OpenOptions::new() + .append(true) + .open(&file_path)? + .set_times(times)?; Ok(uuid_str) } diff --git a/codex-rs/app-server/tests/suite/v2/thread_list.rs b/codex-rs/app-server/tests/suite/v2/thread_list.rs index 0132651df8..7c3568c637 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_list.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_list.rs @@ -1,17 +1,27 @@ use anyhow::Result; use app_test_support::McpProcess; use app_test_support::create_fake_rollout; +use app_test_support::rollout_path; use app_test_support::to_response; +use chrono::DateTime; +use chrono::Utc; use codex_app_server_protocol::GitInfo as ApiGitInfo; +use codex_app_server_protocol::JSONRPCError; use codex_app_server_protocol::JSONRPCResponse; use codex_app_server_protocol::RequestId; use codex_app_server_protocol::SessionSource; use codex_app_server_protocol::ThreadListResponse; +use codex_app_server_protocol::ThreadSortKey; use codex_protocol::protocol::GitInfo as CoreGitInfo; +use pretty_assertions::assert_eq; +use std::cmp::Reverse; +use std::fs::FileTimes; +use std::fs::OpenOptions; use std::path::Path; use std::path::PathBuf; use tempfile::TempDir; use tokio::time::timeout; +use uuid::Uuid; const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); @@ -26,11 +36,22 @@ async fn list_threads( cursor: Option, limit: Option, providers: Option>, +) -> Result { + list_threads_with_sort(mcp, cursor, limit, providers, None).await +} + +async fn list_threads_with_sort( + mcp: &mut McpProcess, + cursor: Option, + limit: Option, + providers: Option>, + sort_key: Option, ) -> Result { let request_id = mcp .send_thread_list_request(codex_app_server_protocol::ThreadListParams { cursor, limit, + sort_key, model_providers: providers, }) .await?; @@ -82,6 +103,16 @@ fn timestamp_at( ) } +fn set_rollout_mtime(path: &Path, updated_at_rfc3339: &str) -> Result<()> { + let parsed = DateTime::parse_from_rfc3339(updated_at_rfc3339)?.with_timezone(&Utc); + let times = FileTimes::new().set_modified(parsed.into()); + OpenOptions::new() + .append(true) + .open(path)? + .set_times(times)?; + Ok(()) +} + #[tokio::test] async fn thread_list_basic_empty() -> Result<()> { let codex_home = TempDir::new()?; @@ -163,6 +194,7 @@ async fn thread_list_pagination_next_cursor_none_on_last_page() -> Result<()> { assert_eq!(thread.preview, "Hello"); assert_eq!(thread.model_provider, "mock_provider"); assert!(thread.created_at > 0); + assert_eq!(thread.updated_at, thread.created_at); assert_eq!(thread.cwd, PathBuf::from("/")); assert_eq!(thread.cli_version, "0.0.0"); assert_eq!(thread.source, SessionSource::Cli); @@ -186,6 +218,7 @@ async fn thread_list_pagination_next_cursor_none_on_last_page() -> Result<()> { assert_eq!(thread.preview, "Hello"); assert_eq!(thread.model_provider, "mock_provider"); assert!(thread.created_at > 0); + assert_eq!(thread.updated_at, thread.created_at); assert_eq!(thread.cwd, PathBuf::from("/")); assert_eq!(thread.cli_version, "0.0.0"); assert_eq!(thread.source, SessionSource::Cli); @@ -236,6 +269,7 @@ async fn thread_list_respects_provider_filter() -> Result<()> { assert_eq!(thread.model_provider, "other_provider"); let expected_ts = chrono::DateTime::parse_from_rfc3339("2025-01-02T11:00:00Z")?.timestamp(); assert_eq!(thread.created_at, expected_ts); + assert_eq!(thread.updated_at, expected_ts); assert_eq!(thread.cwd, PathBuf::from("/")); assert_eq!(thread.cli_version, "0.0.0"); assert_eq!(thread.source, SessionSource::Cli); @@ -429,3 +463,351 @@ async fn thread_list_includes_git_info() -> Result<()> { Ok(()) } + +#[tokio::test] +async fn thread_list_default_sorts_by_created_at() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + let id_a = create_fake_rollout( + codex_home.path(), + "2025-01-02T12-00-00", + "2025-01-02T12:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + let id_b = create_fake_rollout( + codex_home.path(), + "2025-01-01T13-00-00", + "2025-01-01T13:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + let id_c = create_fake_rollout( + codex_home.path(), + "2025-01-01T12-00-00", + "2025-01-01T12:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + + let mut mcp = init_mcp(codex_home.path()).await?; + + let ThreadListResponse { data, .. } = list_threads_with_sort( + &mut mcp, + None, + Some(10), + Some(vec!["mock_provider".to_string()]), + None, + ) + .await?; + + let ids: Vec<_> = data.iter().map(|thread| thread.id.as_str()).collect(); + assert_eq!(ids, vec![id_a.as_str(), id_b.as_str(), id_c.as_str()]); + + Ok(()) +} + +#[tokio::test] +async fn thread_list_sort_updated_at_orders_by_mtime() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + let id_old = create_fake_rollout( + codex_home.path(), + "2025-01-01T10-00-00", + "2025-01-01T10:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + let id_mid = create_fake_rollout( + codex_home.path(), + "2025-01-01T11-00-00", + "2025-01-01T11:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + let id_new = create_fake_rollout( + codex_home.path(), + "2025-01-01T12-00-00", + "2025-01-01T12:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-01-01T10-00-00", &id_old).as_path(), + "2025-01-03T00:00:00Z", + )?; + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-01-01T11-00-00", &id_mid).as_path(), + "2025-01-02T00:00:00Z", + )?; + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-01-01T12-00-00", &id_new).as_path(), + "2025-01-01T00:00:00Z", + )?; + + let mut mcp = init_mcp(codex_home.path()).await?; + + let ThreadListResponse { data, .. } = list_threads_with_sort( + &mut mcp, + None, + Some(10), + Some(vec!["mock_provider".to_string()]), + Some(ThreadSortKey::UpdatedAt), + ) + .await?; + + let ids: Vec<_> = data.iter().map(|thread| thread.id.as_str()).collect(); + assert_eq!(ids, vec![id_old.as_str(), id_mid.as_str(), id_new.as_str()]); + + Ok(()) +} + +#[tokio::test] +async fn thread_list_updated_at_paginates_with_cursor() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + let id_a = create_fake_rollout( + codex_home.path(), + "2025-02-01T10-00-00", + "2025-02-01T10:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + let id_b = create_fake_rollout( + codex_home.path(), + "2025-02-01T11-00-00", + "2025-02-01T11:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + let id_c = create_fake_rollout( + codex_home.path(), + "2025-02-01T12-00-00", + "2025-02-01T12:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-02-01T10-00-00", &id_a).as_path(), + "2025-02-03T00:00:00Z", + )?; + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-02-01T11-00-00", &id_b).as_path(), + "2025-02-02T00:00:00Z", + )?; + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-02-01T12-00-00", &id_c).as_path(), + "2025-02-01T00:00:00Z", + )?; + + let mut mcp = init_mcp(codex_home.path()).await?; + + let ThreadListResponse { + data: page1, + next_cursor: cursor1, + } = list_threads_with_sort( + &mut mcp, + None, + Some(2), + Some(vec!["mock_provider".to_string()]), + Some(ThreadSortKey::UpdatedAt), + ) + .await?; + let ids_page1: Vec<_> = page1.iter().map(|thread| thread.id.as_str()).collect(); + assert_eq!(ids_page1, vec![id_a.as_str(), id_b.as_str()]); + let cursor1 = cursor1.expect("expected nextCursor on first page"); + + let ThreadListResponse { + data: page2, + next_cursor: cursor2, + } = list_threads_with_sort( + &mut mcp, + Some(cursor1), + Some(2), + Some(vec!["mock_provider".to_string()]), + Some(ThreadSortKey::UpdatedAt), + ) + .await?; + let ids_page2: Vec<_> = page2.iter().map(|thread| thread.id.as_str()).collect(); + assert_eq!(ids_page2, vec![id_c.as_str()]); + assert_eq!(cursor2, None); + + Ok(()) +} + +#[tokio::test] +async fn thread_list_created_at_tie_breaks_by_uuid() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + let id_a = create_fake_rollout( + codex_home.path(), + "2025-02-01T10-00-00", + "2025-02-01T10:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + let id_b = create_fake_rollout( + codex_home.path(), + "2025-02-01T10-00-00", + "2025-02-01T10:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + + let mut mcp = init_mcp(codex_home.path()).await?; + + let ThreadListResponse { data, .. } = list_threads( + &mut mcp, + None, + Some(10), + Some(vec!["mock_provider".to_string()]), + ) + .await?; + + let ids: Vec<_> = data.iter().map(|thread| thread.id.as_str()).collect(); + let mut expected = [id_a, id_b]; + expected.sort_by_key(|id| Reverse(Uuid::parse_str(id).expect("uuid should parse"))); + let expected: Vec<_> = expected.iter().map(String::as_str).collect(); + assert_eq!(ids, expected); + + Ok(()) +} + +#[tokio::test] +async fn thread_list_updated_at_tie_breaks_by_uuid() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + let id_a = create_fake_rollout( + codex_home.path(), + "2025-02-01T10-00-00", + "2025-02-01T10:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + let id_b = create_fake_rollout( + codex_home.path(), + "2025-02-01T11-00-00", + "2025-02-01T11:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + + let updated_at = "2025-02-03T00:00:00Z"; + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-02-01T10-00-00", &id_a).as_path(), + updated_at, + )?; + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-02-01T11-00-00", &id_b).as_path(), + updated_at, + )?; + + let mut mcp = init_mcp(codex_home.path()).await?; + + let ThreadListResponse { data, .. } = list_threads_with_sort( + &mut mcp, + None, + Some(10), + Some(vec!["mock_provider".to_string()]), + Some(ThreadSortKey::UpdatedAt), + ) + .await?; + + let ids: Vec<_> = data.iter().map(|thread| thread.id.as_str()).collect(); + let mut expected = [id_a, id_b]; + expected.sort_by_key(|id| Reverse(Uuid::parse_str(id).expect("uuid should parse"))); + let expected: Vec<_> = expected.iter().map(String::as_str).collect(); + assert_eq!(ids, expected); + + Ok(()) +} + +#[tokio::test] +async fn thread_list_updated_at_uses_mtime() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + let thread_id = create_fake_rollout( + codex_home.path(), + "2025-02-01T10-00-00", + "2025-02-01T10:00:00Z", + "Hello", + Some("mock_provider"), + None, + )?; + + set_rollout_mtime( + rollout_path(codex_home.path(), "2025-02-01T10-00-00", &thread_id).as_path(), + "2025-02-05T00:00:00Z", + )?; + + let mut mcp = init_mcp(codex_home.path()).await?; + + let ThreadListResponse { data, .. } = list_threads_with_sort( + &mut mcp, + None, + Some(10), + Some(vec!["mock_provider".to_string()]), + Some(ThreadSortKey::UpdatedAt), + ) + .await?; + + let thread = data + .iter() + .find(|item| item.id == thread_id) + .expect("expected thread for created rollout"); + let expected_created = + chrono::DateTime::parse_from_rfc3339("2025-02-01T10:00:00Z")?.timestamp(); + let expected_updated = + chrono::DateTime::parse_from_rfc3339("2025-02-05T00:00:00Z")?.timestamp(); + assert_eq!(thread.created_at, expected_created); + assert_eq!(thread.updated_at, expected_updated); + + Ok(()) +} + +#[tokio::test] +async fn thread_list_invalid_cursor_returns_error() -> Result<()> { + let codex_home = TempDir::new()?; + create_minimal_config(codex_home.path())?; + + let mut mcp = init_mcp(codex_home.path()).await?; + + let request_id = mcp + .send_thread_list_request(codex_app_server_protocol::ThreadListParams { + cursor: Some("not-a-cursor".to_string()), + limit: Some(2), + sort_key: None, + model_providers: Some(vec!["mock_provider".to_string()]), + }) + .await?; + let error: JSONRPCError = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_error_message(RequestId::Integer(request_id)), + ) + .await??; + assert_eq!(error.error.code, -32600); + assert_eq!(error.error.message, "invalid cursor: not-a-cursor"); + + Ok(()) +} diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index 498c45748f..e5a21b0517 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -100,6 +100,7 @@ pub use rollout::find_conversation_path_by_id_str; pub use rollout::find_thread_path_by_id_str; pub use rollout::list::Cursor; pub use rollout::list::ThreadItem; +pub use rollout::list::ThreadSortKey; pub use rollout::list::ThreadsPage; pub use rollout::list::parse_cursor; pub use rollout::list::read_head_for_summary; diff --git a/codex-rs/core/src/rollout/list.rs b/codex-rs/core/src/rollout/list.rs index 487304ddc8..3e53882fcf 100644 --- a/codex-rs/core/src/rollout/list.rs +++ b/codex-rs/core/src/rollout/list.rs @@ -1,11 +1,13 @@ use std::cmp::Reverse; use std::io::{self}; use std::num::NonZero; +use std::ops::ControlFlow; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; use std::sync::atomic::AtomicBool; +use async_trait::async_trait; use time::OffsetDateTime; use time::PrimitiveDateTime; use time::format_description::FormatItem; @@ -41,8 +43,10 @@ pub struct ThreadItem { /// First up to `HEAD_RECORD_LIMIT` JSONL records parsed as JSON (includes meta line). pub head: Vec, /// RFC3339 timestamp string for when the session was created, if available. + /// 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, } @@ -68,6 +72,12 @@ struct HeadTailSummary { const MAX_SCAN_FILES: usize = 10000; const HEAD_RECORD_LIMIT: usize = 10; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ThreadSortKey { + CreatedAt, + UpdatedAt, +} + /// Pagination cursor identifying a file by timestamp and UUID. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Cursor { @@ -81,6 +91,135 @@ impl Cursor { } } +/// Keeps track of where a paginated listing left off. As the file scan goes newest -> oldest, +/// it ignores everything until it reaches the last seen item from the previous page, then +/// starts returning results after that. This makes paging stable even if new files show up during +/// pagination. +struct AnchorState { + ts: OffsetDateTime, + id: Uuid, + passed: bool, +} + +impl AnchorState { + fn new(anchor: Option) -> Self { + match anchor { + Some(cursor) => Self { + ts: cursor.ts, + id: cursor.id, + passed: false, + }, + None => Self { + ts: OffsetDateTime::UNIX_EPOCH, + id: Uuid::nil(), + passed: true, + }, + } + } + + fn should_skip(&mut self, ts: OffsetDateTime, id: Uuid) -> bool { + if self.passed { + return false; + } + if ts < self.ts || (ts == self.ts && id < self.id) { + self.passed = true; + false + } else { + true + } + } +} + +/// Visitor interface to customize behavior when visiting each rollout file +/// in `walk_rollout_files`. +/// +/// We need to apply different logic if we're ultimately going to be returning +/// threads ordered by created_at or updated_at. +#[async_trait] +trait RolloutFileVisitor { + async fn visit( + &mut self, + ts: OffsetDateTime, + id: Uuid, + path: PathBuf, + scanned: usize, + ) -> ControlFlow<()>; +} + +/// Collects thread items during directory traversal in created_at order, +/// applying pagination and filters inline. +struct FilesByCreatedAtVisitor<'a> { + items: &'a mut Vec, + page_size: usize, + anchor_state: AnchorState, + more_matches_available: bool, + allowed_sources: &'a [SessionSource], + provider_matcher: Option<&'a ProviderMatcher<'a>>, +} + +#[async_trait] +impl<'a> RolloutFileVisitor for FilesByCreatedAtVisitor<'a> { + async fn visit( + &mut self, + ts: OffsetDateTime, + id: Uuid, + path: PathBuf, + scanned: usize, + ) -> ControlFlow<()> { + if scanned >= MAX_SCAN_FILES && self.items.len() >= self.page_size { + self.more_matches_available = true; + return ControlFlow::Break(()); + } + if self.anchor_state.should_skip(ts, id) { + return ControlFlow::Continue(()); + } + if self.items.len() == self.page_size { + self.more_matches_available = true; + return ControlFlow::Break(()); + } + let updated_at = file_modified_time(&path) + .await + .unwrap_or(None) + .and_then(format_rfc3339); + if let Some(item) = build_thread_item( + path, + self.allowed_sources, + self.provider_matcher, + updated_at, + ) + .await + { + self.items.push(item); + } + ControlFlow::Continue(()) + } +} + +/// Collects lightweight file candidates (path + id + mtime). +/// Sorting after mtime happens after all files are collected. +struct FilesByUpdatedAtVisitor<'a> { + candidates: &'a mut Vec, +} + +#[async_trait] +impl<'a> RolloutFileVisitor for FilesByUpdatedAtVisitor<'a> { + async fn visit( + &mut self, + _ts: OffsetDateTime, + id: Uuid, + path: PathBuf, + _scanned: usize, + ) -> ControlFlow<()> { + let updated_at = file_modified_time(&path).await.unwrap_or(None); + self.candidates.push(ThreadCandidate { + path, + id, + updated_at, + }); + ControlFlow::Continue(()) + } +} + impl serde::Serialize for Cursor { fn serialize(&self, serializer: S) -> Result where @@ -108,11 +247,13 @@ impl<'de> serde::Deserialize<'de> for Cursor { /// Retrieve recorded thread file paths with token pagination. The returned `next_cursor` /// can be supplied on the next call to resume after the last returned item, resilient to -/// concurrent new sessions being appended. Ordering is stable by timestamp desc, then UUID desc. +/// concurrent new sessions being appended. Ordering is stable by the requested sort key +/// (timestamp desc, then UUID desc). pub(crate) async fn get_threads( codex_home: &Path, page_size: usize, cursor: Option<&Cursor>, + sort_key: ThreadSortKey, allowed_sources: &[SessionSource], model_providers: Option<&[String]>, default_provider: &str, @@ -138,6 +279,7 @@ pub(crate) async fn get_threads( root.clone(), page_size, anchor, + sort_key, allowed_sources, provider_matcher.as_ref(), ) @@ -148,8 +290,46 @@ pub(crate) async fn get_threads( /// Load thread file paths from disk using directory traversal. /// /// Directory layout: `~/.codex/sessions/YYYY/MM/DD/rollout-YYYY-MM-DDThh-mm-ss-.jsonl` -/// Returned newest (latest) first. +/// Returned newest (based on sort key) first. async fn traverse_directories_for_paths( + root: PathBuf, + page_size: usize, + anchor: Option, + sort_key: ThreadSortKey, + allowed_sources: &[SessionSource], + provider_matcher: Option<&ProviderMatcher<'_>>, +) -> io::Result { + match sort_key { + ThreadSortKey::CreatedAt => { + traverse_directories_for_paths_created( + root, + page_size, + anchor, + allowed_sources, + provider_matcher, + ) + .await + } + ThreadSortKey::UpdatedAt => { + traverse_directories_for_paths_updated( + root, + page_size, + anchor, + allowed_sources, + provider_matcher, + ) + .await + } + } +} + +/// Walk the rollout directory tree in reverse chronological order and +/// collect items until the page fills or the scan cap is hit. +/// +/// Ordering comes from directory/filename sorting, so created_at is derived +/// from the filename timestamp. Pagination is handled by the anchor cursor +/// so we resume strictly after the last returned `(ts, id)` pair. +async fn traverse_directories_for_paths_created( root: PathBuf, page_size: usize, anchor: Option, @@ -158,98 +338,17 @@ async fn traverse_directories_for_paths( ) -> io::Result { let mut items: Vec = Vec::with_capacity(page_size); let mut scanned_files = 0usize; - let mut anchor_passed = anchor.is_none(); - let (anchor_ts, anchor_id) = match anchor { - Some(c) => (c.ts, c.id), - None => (OffsetDateTime::UNIX_EPOCH, Uuid::nil()), - }; let mut more_matches_available = false; - - let year_dirs = collect_dirs_desc(&root, |s| s.parse::().ok()).await?; - - 'outer: for (_year, year_path) in year_dirs.iter() { - if scanned_files >= MAX_SCAN_FILES { - break; - } - let month_dirs = collect_dirs_desc(year_path, |s| s.parse::().ok()).await?; - for (_month, month_path) in month_dirs.iter() { - if scanned_files >= MAX_SCAN_FILES { - break 'outer; - } - let day_dirs = collect_dirs_desc(month_path, |s| s.parse::().ok()).await?; - for (_day, day_path) in day_dirs.iter() { - if scanned_files >= MAX_SCAN_FILES { - break 'outer; - } - let mut day_files = collect_files(day_path, |name_str, path| { - if !name_str.starts_with("rollout-") || !name_str.ends_with(".jsonl") { - return None; - } - - parse_timestamp_uuid_from_filename(name_str) - .map(|(ts, id)| (ts, id, name_str.to_string(), path.to_path_buf())) - }) - .await?; - // Stable ordering within the same second: (timestamp desc, uuid desc) - day_files.sort_by_key(|(ts, sid, _name_str, _path)| (Reverse(*ts), Reverse(*sid))); - for (ts, sid, _name_str, path) in day_files.into_iter() { - scanned_files += 1; - if scanned_files >= MAX_SCAN_FILES && items.len() >= page_size { - more_matches_available = true; - break 'outer; - } - if !anchor_passed { - if ts < anchor_ts || (ts == anchor_ts && sid < anchor_id) { - anchor_passed = true; - } else { - continue; - } - } - if items.len() == page_size { - more_matches_available = true; - break 'outer; - } - // Read head and detect message events; stop once meta + user are found. - let summary = read_head_summary(&path, HEAD_RECORD_LIMIT) - .await - .unwrap_or_default(); - if !allowed_sources.is_empty() - && !summary - .source - .is_some_and(|source| allowed_sources.iter().any(|s| s == &source)) - { - continue; - } - if let Some(matcher) = provider_matcher - && !matcher.matches(summary.model_provider.as_deref()) - { - continue; - } - // Apply filters: must have session meta and at least one user message event - if summary.saw_session_meta && summary.saw_user_event { - let HeadTailSummary { - head, - created_at, - mut updated_at, - .. - } = summary; - if updated_at.is_none() { - updated_at = file_modified_rfc3339(&path) - .await - .unwrap_or(None) - .or_else(|| created_at.clone()); - } - items.push(ThreadItem { - path, - head, - created_at, - updated_at, - }); - } - } - } - } - } + let mut visitor = FilesByCreatedAtVisitor { + items: &mut items, + page_size, + anchor_state: AnchorState::new(anchor), + more_matches_available, + allowed_sources, + provider_matcher, + }; + walk_rollout_files(&root, &mut scanned_files, &mut visitor).await?; + more_matches_available = visitor.more_matches_available; let reached_scan_cap = scanned_files >= MAX_SCAN_FILES; if reached_scan_cap && !items.is_empty() { @@ -257,7 +356,7 @@ async fn traverse_directories_for_paths( } let next = if more_matches_available { - build_next_cursor(&items) + build_next_cursor(&items, ThreadSortKey::CreatedAt) } else { None }; @@ -269,9 +368,77 @@ async fn traverse_directories_for_paths( }) } -/// Pagination cursor token format: "|" where `file_ts` matches the -/// filename timestamp portion (YYYY-MM-DDThh-mm-ss) used in rollout filenames. -/// The cursor orders files by timestamp desc, then UUID desc. +/// Walk the rollout directory tree to collect files by updated_at, then sort by +/// file mtime (updated_at) and apply pagination/filtering in that order. +/// +/// Because updated_at is not encoded in filenames, this path must scan all +/// files up to the scan cap, then sort and filter by the anchor cursor. +/// +/// NOTE: This can be optimized in the future if we store additional state on disk +/// to cache updated_at timestamps. +async fn traverse_directories_for_paths_updated( + root: PathBuf, + page_size: usize, + anchor: Option, + allowed_sources: &[SessionSource], + provider_matcher: Option<&ProviderMatcher<'_>>, +) -> io::Result { + let mut items: Vec = Vec::with_capacity(page_size); + let mut scanned_files = 0usize; + let mut anchor_state = AnchorState::new(anchor); + let mut more_matches_available = false; + + let candidates = collect_files_by_updated_at(&root, &mut scanned_files).await?; + let mut candidates = candidates; + candidates.sort_by_key(|candidate| { + let ts = candidate.updated_at.unwrap_or(OffsetDateTime::UNIX_EPOCH); + (Reverse(ts), Reverse(candidate.id)) + }); + + for candidate in candidates.into_iter() { + let ts = candidate.updated_at.unwrap_or(OffsetDateTime::UNIX_EPOCH); + if anchor_state.should_skip(ts, candidate.id) { + continue; + } + if items.len() == page_size { + more_matches_available = true; + break; + } + + let updated_at_fallback = candidate.updated_at.and_then(format_rfc3339); + if let Some(item) = build_thread_item( + candidate.path, + allowed_sources, + provider_matcher, + updated_at_fallback, + ) + .await + { + items.push(item); + } + } + + let reached_scan_cap = scanned_files >= MAX_SCAN_FILES; + if reached_scan_cap && !items.is_empty() { + more_matches_available = true; + } + + let next = if more_matches_available { + build_next_cursor(&items, ThreadSortKey::UpdatedAt) + } else { + None + }; + Ok(ThreadsPage { + items, + next_cursor: next, + num_scanned_files: scanned_files, + reached_scan_cap, + }) +} + +/// Pagination cursor token format: "|" where `ts` uses +/// YYYY-MM-DDThh-mm-ss (UTC, second precision). +/// The cursor orders files by the requested sort key (timestamp desc, then UUID desc). pub fn parse_cursor(token: &str) -> Option { let (file_ts, uuid_str) = token.split_once('|')?; @@ -286,13 +453,63 @@ pub fn parse_cursor(token: &str) -> Option { Some(Cursor::new(ts, uuid)) } -fn build_next_cursor(items: &[ThreadItem]) -> Option { +fn build_next_cursor(items: &[ThreadItem], sort_key: ThreadSortKey) -> Option { let last = items.last()?; let file_name = last.path.file_name()?.to_string_lossy(); - let (ts, id) = parse_timestamp_uuid_from_filename(&file_name)?; + let (created_ts, id) = parse_timestamp_uuid_from_filename(&file_name)?; + let ts = match sort_key { + ThreadSortKey::CreatedAt => created_ts, + ThreadSortKey::UpdatedAt => { + let updated_at = last.updated_at.as_deref()?; + OffsetDateTime::parse(updated_at, &Rfc3339).ok()? + } + }; Some(Cursor::new(ts, id)) } +async fn build_thread_item( + path: PathBuf, + allowed_sources: &[SessionSource], + provider_matcher: Option<&ProviderMatcher<'_>>, + updated_at: Option, +) -> Option { + // Read head and detect message events; stop once meta + user are found. + let summary = read_head_summary(&path, HEAD_RECORD_LIMIT) + .await + .unwrap_or_default(); + if !allowed_sources.is_empty() + && !summary + .source + .is_some_and(|source| allowed_sources.contains(&source)) + { + return None; + } + if let Some(matcher) = provider_matcher + && !matcher.matches(summary.model_provider.as_deref()) + { + return None; + } + // Apply filters: must have session meta and at least one user message event + if summary.saw_session_meta && summary.saw_user_event { + let HeadTailSummary { + head, + created_at, + updated_at: mut summary_updated_at, + .. + } = summary; + if summary_updated_at.is_none() { + summary_updated_at = updated_at.or_else(|| created_at.clone()); + } + return Some(ThreadItem { + path, + head, + created_at, + updated_at: summary_updated_at, + }); + } + None +} + /// Collects immediate subdirectories of `parent`, parses their (string) names with `parse`, /// and returns them sorted descending by the parsed key. async fn collect_dirs_desc(parent: &Path, parse: F) -> io::Result> @@ -340,6 +557,22 @@ where Ok(collected) } +async fn collect_rollout_day_files( + day_path: &Path, +) -> io::Result> { + let mut day_files = collect_files(day_path, |name_str, path| { + if !name_str.starts_with("rollout-") || !name_str.ends_with(".jsonl") { + return None; + } + + parse_timestamp_uuid_from_filename(name_str).map(|(ts, id)| (ts, id, path.to_path_buf())) + }) + .await?; + // Stable ordering within the same second: (timestamp desc, uuid desc) + day_files.sort_by_key(|(ts, sid, _path)| (Reverse(*ts), Reverse(*sid))); + Ok(day_files) +} + fn parse_timestamp_uuid_from_filename(name: &str) -> Option<(OffsetDateTime, Uuid)> { // Expected: rollout-YYYY-MM-DDThh-mm-ss-.jsonl let core = name.strip_prefix("rollout-")?.strip_suffix(".jsonl")?; @@ -357,6 +590,65 @@ fn parse_timestamp_uuid_from_filename(name: &str) -> Option<(OffsetDateTime, Uui Some((ts, uuid)) } +struct ThreadCandidate { + path: PathBuf, + id: Uuid, + updated_at: Option, +} + +async fn collect_files_by_updated_at( + root: &Path, + scanned_files: &mut usize, +) -> io::Result> { + let mut candidates = Vec::new(); + let mut visitor = FilesByUpdatedAtVisitor { + candidates: &mut candidates, + }; + walk_rollout_files(root, scanned_files, &mut visitor).await?; + + Ok(candidates) +} + +async fn walk_rollout_files( + root: &Path, + scanned_files: &mut usize, + visitor: &mut impl RolloutFileVisitor, +) -> io::Result<()> { + let year_dirs = collect_dirs_desc(root, |s| s.parse::().ok()).await?; + + 'outer: for (_year, year_path) in year_dirs.iter() { + if *scanned_files >= MAX_SCAN_FILES { + break; + } + let month_dirs = collect_dirs_desc(year_path, |s| s.parse::().ok()).await?; + for (_month, month_path) in month_dirs.iter() { + if *scanned_files >= MAX_SCAN_FILES { + break 'outer; + } + let day_dirs = collect_dirs_desc(month_path, |s| s.parse::().ok()).await?; + for (_day, day_path) in day_dirs.iter() { + if *scanned_files >= MAX_SCAN_FILES { + break 'outer; + } + let day_files = collect_rollout_day_files(day_path).await?; + for (ts, id, path) in day_files.into_iter() { + *scanned_files += 1; + if *scanned_files > MAX_SCAN_FILES { + break 'outer; + } + if let ControlFlow::Break(()) = + visitor.visit(ts, id, path, *scanned_files).await + { + break 'outer; + } + } + } + } + } + + Ok(()) +} + struct ProviderMatcher<'a> { filters: &'a [String], matches_default_provider: bool, @@ -452,14 +744,24 @@ pub async fn read_head_for_summary(path: &Path) -> io::Result io::Result> { +async fn file_modified_time(path: &Path) -> io::Result> { let meta = tokio::fs::metadata(path).await?; let modified = meta.modified().ok(); let Some(modified) = modified else { return Ok(None); }; let dt = OffsetDateTime::from(modified); - Ok(dt.format(&Rfc3339).ok()) + // 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)) +} + +fn format_rfc3339(dt: OffsetDateTime) -> Option { + dt.format(&Rfc3339).ok() +} + +fn truncate_to_seconds(dt: OffsetDateTime) -> Option { + dt.replace_nanosecond(0).ok() } /// Locate a recorded thread rollout file by its UUID string using the existing diff --git a/codex-rs/core/src/rollout/recorder.rs b/codex-rs/core/src/rollout/recorder.rs index 7151f95b95..3bde40a734 100644 --- a/codex-rs/core/src/rollout/recorder.rs +++ b/codex-rs/core/src/rollout/recorder.rs @@ -22,6 +22,7 @@ use tracing::warn; use super::SESSIONS_SUBDIR; use super::list::Cursor; +use super::list::ThreadSortKey; use super::list::ThreadsPage; use super::list::get_threads; use super::policy::is_persisted_response_item; @@ -99,6 +100,7 @@ impl RolloutRecorder { codex_home: &Path, page_size: usize, cursor: Option<&Cursor>, + sort_key: ThreadSortKey, allowed_sources: &[SessionSource], model_providers: Option<&[String]>, default_provider: &str, @@ -107,6 +109,7 @@ impl RolloutRecorder { codex_home, page_size, cursor, + sort_key, allowed_sources, model_providers, default_provider, @@ -115,19 +118,24 @@ impl RolloutRecorder { } /// Find the newest recorded thread path, optionally filtering to a matching cwd. + #[allow(clippy::too_many_arguments)] pub async fn find_latest_thread_path( codex_home: &Path, + page_size: usize, + cursor: Option<&Cursor>, + sort_key: ThreadSortKey, allowed_sources: &[SessionSource], model_providers: Option<&[String]>, default_provider: &str, filter_cwd: Option<&Path>, ) -> std::io::Result> { - let mut cursor: Option = None; + let mut cursor = cursor.cloned(); loop { let page = Self::list_threads( codex_home, - 25, + page_size, cursor.as_ref(), + sort_key, allowed_sources, model_providers, default_provider, diff --git a/codex-rs/core/src/rollout/tests.rs b/codex-rs/core/src/rollout/tests.rs index c16ec65cf8..d0675e97ed 100644 --- a/codex-rs/core/src/rollout/tests.rs +++ b/codex-rs/core/src/rollout/tests.rs @@ -1,11 +1,13 @@ #![allow(clippy::unwrap_used, clippy::expect_used)] use std::fs::File; +use std::fs::FileTimes; use std::fs::{self}; use std::io::Write; use std::path::Path; use tempfile::TempDir; +use time::Duration; use time::OffsetDateTime; use time::PrimitiveDateTime; use time::format_description::FormatItem; @@ -15,6 +17,7 @@ use uuid::Uuid; use crate::rollout::INTERACTIVE_SESSION_SOURCES; use crate::rollout::list::Cursor; use crate::rollout::list::ThreadItem; +use crate::rollout::list::ThreadSortKey; use crate::rollout::list::ThreadsPage; use crate::rollout::list::get_threads; use anyhow::Result; @@ -122,6 +125,8 @@ fn write_session_file_with_provider( }); writeln!(file, "{rec}")?; } + let times = FileTimes::new().set_modified(dt.into()); + file.set_times(times)?; Ok((dt, uuid)) } @@ -166,6 +171,7 @@ async fn test_list_conversations_latest_first() { home, 10, None, + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -315,6 +321,7 @@ async fn test_pagination_cursor() { home, 2, None, + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -382,6 +389,7 @@ async fn test_pagination_cursor() { home, 2, page1.next_cursor.as_ref(), + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -449,6 +457,7 @@ async fn test_pagination_cursor() { home, 2, page2.next_cursor.as_ref(), + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -501,6 +510,7 @@ async fn test_get_thread_contents() { home, 1, None, + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -567,6 +577,52 @@ async fn test_get_thread_contents() { assert_eq!(content, expected_content); } +#[tokio::test] +async fn test_created_at_sort_uses_file_mtime_for_updated_at() -> Result<()> { + let temp = TempDir::new().unwrap(); + let home = temp.path(); + + let ts = "2025-06-01T08-00-00"; + let uuid = Uuid::from_u128(43); + write_session_file(home, ts, uuid, 0, Some(SessionSource::VSCode)).unwrap(); + + let created = PrimitiveDateTime::parse( + ts, + format_description!("[year]-[month]-[day]T[hour]-[minute]-[second]"), + )? + .assume_utc(); + let updated = created + Duration::hours(2); + let expected_updated = updated.format(&time::format_description::well_known::Rfc3339)?; + + let file_path = home + .join("sessions") + .join("2025") + .join("06") + .join("01") + .join(format!("rollout-{ts}-{uuid}.jsonl")); + let file = std::fs::OpenOptions::new().write(true).open(&file_path)?; + let times = FileTimes::new().set_modified(updated.into()); + file.set_times(times)?; + + let provider_filter = provider_vec(&[TEST_PROVIDER]); + let page = get_threads( + home, + 1, + None, + ThreadSortKey::CreatedAt, + INTERACTIVE_SESSION_SOURCES, + Some(provider_filter.as_slice()), + TEST_PROVIDER, + ) + .await?; + + let item = page.items.first().expect("conversation item"); + assert_eq!(item.created_at.as_deref(), Some(ts)); + assert_eq!(item.updated_at.as_deref(), Some(expected_updated.as_str())); + + Ok(()) +} + #[tokio::test] async fn test_updated_at_uses_file_mtime() -> Result<()> { let temp = TempDir::new().unwrap(); @@ -630,6 +686,7 @@ async fn test_updated_at_uses_file_mtime() -> Result<()> { home, 1, None, + ThreadSortKey::UpdatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -669,6 +726,7 @@ async fn test_stable_ordering_same_second_pagination() { home, 2, None, + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -728,6 +786,7 @@ async fn test_stable_ordering_same_second_pagination() { home, 2, page1.next_cursor.as_ref(), + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -786,6 +845,7 @@ async fn test_source_filter_excludes_non_matching_sessions() { home, 10, None, + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), TEST_PROVIDER, @@ -803,9 +863,17 @@ async fn test_source_filter_excludes_non_matching_sessions() { path.ends_with("rollout-2025-08-02T10-00-00-00000000-0000-0000-0000-00000000002a.jsonl") })); - let all_sessions = get_threads(home, 10, None, NO_SOURCE_FILTER, None, TEST_PROVIDER) - .await - .unwrap(); + let all_sessions = get_threads( + home, + 10, + None, + ThreadSortKey::CreatedAt, + NO_SOURCE_FILTER, + None, + TEST_PROVIDER, + ) + .await + .unwrap(); let all_paths: Vec<_> = all_sessions .items .into_iter() @@ -861,6 +929,7 @@ async fn test_model_provider_filter_selects_only_matching_sessions() -> Result<( home, 10, None, + ThreadSortKey::CreatedAt, NO_SOURCE_FILTER, Some(openai_filter.as_slice()), "openai", @@ -886,6 +955,7 @@ async fn test_model_provider_filter_selects_only_matching_sessions() -> Result<( home, 10, None, + ThreadSortKey::CreatedAt, NO_SOURCE_FILTER, Some(beta_filter.as_slice()), "openai", @@ -906,6 +976,7 @@ async fn test_model_provider_filter_selects_only_matching_sessions() -> Result<( home, 10, None, + ThreadSortKey::CreatedAt, NO_SOURCE_FILTER, Some(unknown_filter.as_slice()), "openai", @@ -913,7 +984,16 @@ async fn test_model_provider_filter_selects_only_matching_sessions() -> Result<( .await?; assert!(unknown_sessions.items.is_empty()); - let all_sessions = get_threads(home, 10, None, NO_SOURCE_FILTER, None, "openai").await?; + let all_sessions = get_threads( + home, + 10, + None, + ThreadSortKey::CreatedAt, + NO_SOURCE_FILTER, + None, + "openai", + ) + .await?; assert_eq!(all_sessions.items.len(), 3); Ok(()) diff --git a/codex-rs/core/tests/suite/cli_stream.rs b/codex-rs/core/tests/suite/cli_stream.rs index 633d55a608..9108f263f9 100644 --- a/codex-rs/core/tests/suite/cli_stream.rs +++ b/codex-rs/core/tests/suite/cli_stream.rs @@ -88,6 +88,7 @@ async fn chat_mode_stream_cli() { home.path(), 10, None, + codex_core::ThreadSortKey::UpdatedAt, &[], Some(provider_filter.as_slice()), "mock", diff --git a/codex-rs/debug-client/src/client.rs b/codex-rs/debug-client/src/client.rs index 5267e48156..377dc02a8d 100644 --- a/codex-rs/debug-client/src/client.rs +++ b/codex-rs/debug-client/src/client.rs @@ -171,6 +171,7 @@ impl AppServerClient { params: ThreadListParams { cursor, limit: None, + sort_key: None, model_providers: None, }, }; diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index cdd8bf7daf..ccab33048b 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -514,6 +514,9 @@ async fn resolve_resume_path( }; match codex_core::RolloutRecorder::find_latest_thread_path( &config.codex_home, + 1, + None, + codex_core::ThreadSortKey::UpdatedAt, &[], Some(default_provider_filter.as_slice()), &config.model_provider_id, diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index 29bf2b1ce0..880ff19322 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -15,6 +15,7 @@ use codex_core::AuthManager; use codex_core::CodexAuth; use codex_core::INTERACTIVE_SESSION_SOURCES; use codex_core::RolloutRecorder; +use codex_core::ThreadSortKey; use codex_core::auth::enforce_login_restrictions; use codex_core::config::Config; use codex_core::config::ConfigOverrides; @@ -478,6 +479,7 @@ async fn run_ratatui_app( &config.codex_home, 1, None, + ThreadSortKey::UpdatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), &config.model_provider_id, @@ -529,6 +531,9 @@ async fn run_ratatui_app( }; match RolloutRecorder::find_latest_thread_path( &config.codex_home, + 1, + None, + ThreadSortKey::UpdatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), &config.model_provider_id, diff --git a/codex-rs/tui/src/resume_picker.rs b/codex-rs/tui/src/resume_picker.rs index 5e96c8f2bb..71d6da453b 100644 --- a/codex-rs/tui/src/resume_picker.rs +++ b/codex-rs/tui/src/resume_picker.rs @@ -9,6 +9,7 @@ use codex_core::Cursor; use codex_core::INTERACTIVE_SESSION_SOURCES; use codex_core::RolloutRecorder; use codex_core::ThreadItem; +use codex_core::ThreadSortKey; use codex_core::ThreadsPage; use codex_core::path_utils; use codex_protocol::items::TurnItem; @@ -156,6 +157,7 @@ async fn run_session_picker( &request.codex_home, PAGE_SIZE, request.cursor.as_ref(), + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), request.default_provider.as_str(), @@ -1422,6 +1424,7 @@ mod tests { &state.codex_home, PAGE_SIZE, None, + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(&[String::from("openai")]), "openai", diff --git a/codex-rs/tui2/src/lib.rs b/codex-rs/tui2/src/lib.rs index 55e640c58f..11a0f9f6be 100644 --- a/codex-rs/tui2/src/lib.rs +++ b/codex-rs/tui2/src/lib.rs @@ -15,6 +15,7 @@ use codex_core::AuthManager; use codex_core::CodexAuth; use codex_core::INTERACTIVE_SESSION_SOURCES; use codex_core::RolloutRecorder; +use codex_core::ThreadSortKey; use codex_core::auth::enforce_login_restrictions; use codex_core::config::Config; use codex_core::config::ConfigOverrides; @@ -500,6 +501,7 @@ async fn run_ratatui_app( &config.codex_home, 1, None, + ThreadSortKey::UpdatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), &config.model_provider_id, @@ -552,6 +554,9 @@ async fn run_ratatui_app( }; match RolloutRecorder::find_latest_thread_path( &config.codex_home, + 1, + None, + ThreadSortKey::UpdatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), &config.model_provider_id, diff --git a/codex-rs/tui2/src/resume_picker.rs b/codex-rs/tui2/src/resume_picker.rs index 5e96c8f2bb..71d6da453b 100644 --- a/codex-rs/tui2/src/resume_picker.rs +++ b/codex-rs/tui2/src/resume_picker.rs @@ -9,6 +9,7 @@ use codex_core::Cursor; use codex_core::INTERACTIVE_SESSION_SOURCES; use codex_core::RolloutRecorder; use codex_core::ThreadItem; +use codex_core::ThreadSortKey; use codex_core::ThreadsPage; use codex_core::path_utils; use codex_protocol::items::TurnItem; @@ -156,6 +157,7 @@ async fn run_session_picker( &request.codex_home, PAGE_SIZE, request.cursor.as_ref(), + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(provider_filter.as_slice()), request.default_provider.as_str(), @@ -1422,6 +1424,7 @@ mod tests { &state.codex_home, PAGE_SIZE, None, + ThreadSortKey::CreatedAt, INTERACTIVE_SESSION_SOURCES, Some(&[String::from("openai")]), "openai", From c26fe645393534c47b8ae5e03b6037c1dfc0d504 Mon Sep 17 00:00:00 2001 From: Anton Panasenko Date: Fri, 16 Jan 2026 13:41:46 -0800 Subject: [PATCH 19/32] feat: show forked from session id in /status (#9330) Summary: - Add forked_from to SessionMeta/SessionConfiguredEvent and persist it for forked sessions. - Surface forked_from in /status for tui + tui2 and add snapshots. --- codex-rs/app-server/tests/common/rollout.rs | 2 + codex-rs/core/src/codex.rs | 4 ++ codex-rs/core/src/rollout/recorder.rs | 5 ++ codex-rs/core/src/rollout/tests.rs | 1 + .../tests/event_processor_with_json_output.rs | 1 + codex-rs/mcp-server/src/outgoing_message.rs | 3 + codex-rs/protocol/src/protocol.rs | 22 ++++++ codex-rs/tui/src/app.rs | 3 + codex-rs/tui/src/chatwidget.rs | 5 ++ codex-rs/tui/src/chatwidget/tests.rs | 2 + codex-rs/tui/src/status/card.rs | 14 ++++ ..._status_snapshot_includes_forked_from.snap | 24 +++++++ codex-rs/tui/src/status/tests.rs | 66 ++++++++++++++++++ codex-rs/tui2/src/app.rs | 3 + codex-rs/tui2/src/chatwidget.rs | 5 ++ codex-rs/tui2/src/chatwidget/tests.rs | 2 + codex-rs/tui2/src/status/card.rs | 14 ++++ ..._status_snapshot_includes_forked_from.snap | 24 +++++++ codex-rs/tui2/src/status/tests.rs | 67 +++++++++++++++++++ 19 files changed, 267 insertions(+) create mode 100644 codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_forked_from.snap create mode 100644 codex-rs/tui2/src/status/snapshots/codex_tui2__status__tests__status_snapshot_includes_forked_from.snap diff --git a/codex-rs/app-server/tests/common/rollout.rs b/codex-rs/app-server/tests/common/rollout.rs index 31d4356638..72bc07753d 100644 --- a/codex-rs/app-server/tests/common/rollout.rs +++ b/codex-rs/app-server/tests/common/rollout.rs @@ -52,6 +52,7 @@ pub fn create_fake_rollout( // Build JSONL lines let meta = SessionMeta { id: conversation_id, + forked_from_id: None, timestamp: meta_rfc3339.to_string(), cwd: PathBuf::from("/"), originator: "codex".to_string(), @@ -129,6 +130,7 @@ pub fn create_fake_rollout_with_text_elements( // Build JSONL lines let meta = SessionMeta { id: conversation_id, + forked_from_id: None, timestamp: meta_rfc3339.to_string(), cwd: PathBuf::from("/"), originator: "codex".to_string(), diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 61fb48f697..00aa70aa69 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -588,6 +588,8 @@ impl Session { )); } + let forked_from_id = initial_history.forked_from_id(); + let (conversation_id, rollout_params) = match &initial_history { InitialHistory::New | InitialHistory::Forked(_) => { let conversation_id = ThreadId::default(); @@ -595,6 +597,7 @@ impl Session { conversation_id, RolloutRecorderParams::new( conversation_id, + forked_from_id, session_configuration.user_instructions.clone(), session_source, ), @@ -733,6 +736,7 @@ impl Session { id: INITIAL_SUBMIT_ID.to_owned(), msg: EventMsg::SessionConfigured(SessionConfiguredEvent { session_id: conversation_id, + forked_from_id, model: session_configuration.model.clone(), model_provider_id: config.model_provider_id.clone(), approval_policy: session_configuration.approval_policy.value(), diff --git a/codex-rs/core/src/rollout/recorder.rs b/codex-rs/core/src/rollout/recorder.rs index 3bde40a734..13eace9f3c 100644 --- a/codex-rs/core/src/rollout/recorder.rs +++ b/codex-rs/core/src/rollout/recorder.rs @@ -57,6 +57,7 @@ pub struct RolloutRecorder { pub enum RolloutRecorderParams { Create { conversation_id: ThreadId, + forked_from_id: Option, instructions: Option, source: SessionSource, }, @@ -79,11 +80,13 @@ enum RolloutCmd { impl RolloutRecorderParams { pub fn new( conversation_id: ThreadId, + forked_from_id: Option, instructions: Option, source: SessionSource, ) -> Self { Self::Create { conversation_id, + forked_from_id, instructions, source, } @@ -158,6 +161,7 @@ impl RolloutRecorder { let (file, rollout_path, meta) = match params { RolloutRecorderParams::Create { conversation_id, + forked_from_id, instructions, source, } => { @@ -181,6 +185,7 @@ impl RolloutRecorder { path, Some(SessionMeta { id: session_id, + forked_from_id, timestamp, cwd: config.cwd.clone(), originator: originator().value, diff --git a/codex-rs/core/src/rollout/tests.rs b/codex-rs/core/src/rollout/tests.rs index d0675e97ed..660e83e56f 100644 --- a/codex-rs/core/src/rollout/tests.rs +++ b/codex-rs/core/src/rollout/tests.rs @@ -641,6 +641,7 @@ async fn test_updated_at_uses_file_mtime() -> Result<()> { item: RolloutItem::SessionMeta(SessionMetaLine { meta: SessionMeta { id: conversation_id, + forked_from_id: None, timestamp: ts.to_string(), instructions: None, cwd: ".".into(), diff --git a/codex-rs/exec/tests/event_processor_with_json_output.rs b/codex-rs/exec/tests/event_processor_with_json_output.rs index e60fcba8e8..e0b2509667 100644 --- a/codex-rs/exec/tests/event_processor_with_json_output.rs +++ b/codex-rs/exec/tests/event_processor_with_json_output.rs @@ -75,6 +75,7 @@ fn session_configured_produces_thread_started_event() { "e1", EventMsg::SessionConfigured(SessionConfiguredEvent { session_id, + forked_from_id: None, model: "codex-mini-latest".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, diff --git a/codex-rs/mcp-server/src/outgoing_message.rs b/codex-rs/mcp-server/src/outgoing_message.rs index 0b05bc3609..b7c66592ff 100644 --- a/codex-rs/mcp-server/src/outgoing_message.rs +++ b/codex-rs/mcp-server/src/outgoing_message.rs @@ -257,6 +257,7 @@ mod tests { id: "1".to_string(), msg: EventMsg::SessionConfigured(SessionConfiguredEvent { session_id: thread_id, + forked_from_id: None, model: "gpt-4o".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, @@ -296,6 +297,7 @@ mod tests { let rollout_file = NamedTempFile::new()?; let session_configured_event = SessionConfiguredEvent { session_id: conversation_id, + forked_from_id: None, model: "gpt-4o".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, @@ -359,6 +361,7 @@ mod tests { let rollout_file = NamedTempFile::new()?; let session_configured_event = SessionConfiguredEvent { session_id: thread_id, + forked_from_id: None, model: "gpt-4o".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index a08e30e226..6ddc0aeb5a 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -1376,6 +1376,22 @@ pub enum InitialHistory { } impl InitialHistory { + pub fn forked_from_id(&self) -> Option { + match self { + InitialHistory::New => None, + InitialHistory::Resumed(resumed) => { + resumed.history.iter().find_map(|item| match item { + RolloutItem::SessionMeta(meta_line) => meta_line.meta.forked_from_id, + _ => None, + }) + } + InitialHistory::Forked(items) => items.iter().find_map(|item| match item { + RolloutItem::SessionMeta(meta_line) => Some(meta_line.meta.id), + _ => None, + }), + } + } + pub fn get_rollout_items(&self) -> Vec { match self { InitialHistory::New => Vec::new(), @@ -1459,6 +1475,8 @@ impl fmt::Display for SubAgentSource { #[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, TS)] pub struct SessionMeta { pub id: ThreadId, + #[serde(skip_serializing_if = "Option::is_none")] + pub forked_from_id: Option, pub timestamp: String, pub cwd: PathBuf, pub originator: String, @@ -1473,6 +1491,7 @@ impl Default for SessionMeta { fn default() -> Self { SessionMeta { id: ThreadId::default(), + forked_from_id: None, timestamp: String::new(), cwd: PathBuf::new(), originator: String::new(), @@ -2003,6 +2022,8 @@ pub struct SkillsListEntry { pub struct SessionConfiguredEvent { /// Name left as session_id instead of thread_id for backwards compatibility. pub session_id: ThreadId, + #[serde(skip_serializing_if = "Option::is_none")] + pub forked_from_id: Option, /// Tell the client what model is being queried. pub model: String, @@ -2376,6 +2397,7 @@ mod tests { id: "1234".to_string(), msg: EventMsg::SessionConfigured(SessionConfiguredEvent { session_id: conversation_id, + forked_from_id: None, model: "codex-mini-latest".to_string(), model_provider_id: "openai".to_string(), approval_policy: AskForApproval::Never, diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index adc48c1d12..0178b31b2e 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -1992,6 +1992,7 @@ mod tests { let make_header = |is_first| { let event = SessionConfiguredEvent { session_id: ThreadId::new(), + forked_from_id: None, model: "gpt-test".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, @@ -2033,6 +2034,7 @@ mod tests { id: String::new(), msg: EventMsg::SessionConfigured(SessionConfiguredEvent { session_id: base_id, + forked_from_id: None, model: "gpt-test".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, @@ -2075,6 +2077,7 @@ mod tests { let thread_id = ThreadId::new(); let event = SessionConfiguredEvent { session_id: thread_id, + forked_from_id: None, model: "gpt-test".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index d301243ca7..e1054d0a3e 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -440,6 +440,7 @@ pub(crate) struct ChatWidget { // Previous status header to restore after a transient stream retry. retry_status_header: Option, thread_id: Option, + forked_from: Option, frame_requester: FrameRequester, // Whether to include the initial welcome banner on session configured show_welcome_banner: bool, @@ -600,6 +601,7 @@ impl ChatWidget { .set_history_metadata(event.history_log_id, event.history_entry_count); self.set_skills(None); self.thread_id = Some(event.session_id); + self.forked_from = event.forked_from_id; self.current_rollout_path = Some(event.rollout_path.clone()); let initial_messages = event.initial_messages.clone(); let model_for_header = event.model.clone(); @@ -1699,6 +1701,7 @@ impl ChatWidget { current_status_header: String::from("Working"), retry_status_header: None, thread_id: None, + forked_from: None, queued_user_messages: VecDeque::new(), show_welcome_banner: is_first_run, suppress_session_configured_redraw: false, @@ -1797,6 +1800,7 @@ impl ChatWidget { current_status_header: String::from("Working"), retry_status_header: None, thread_id: None, + forked_from: None, queued_user_messages: VecDeque::new(), show_welcome_banner: false, suppress_session_configured_redraw: true, @@ -2635,6 +2639,7 @@ impl ChatWidget { token_info, total_usage, &self.thread_id, + self.forked_from, self.rate_limit_snapshot.as_ref(), self.plan_type, Local::now(), diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index 6b216e6d76..19d10ad51d 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -131,6 +131,7 @@ async fn resumed_initial_messages_render_history() { let rollout_file = NamedTempFile::new().unwrap(); let configured = codex_core::protocol::SessionConfiguredEvent { session_id: conversation_id, + forked_from_id: None, model: "test-model".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, @@ -428,6 +429,7 @@ async fn make_chatwidget_manual( current_status_header: String::from("Working"), retry_status_header: None, thread_id: None, + forked_from: None, frame_requester: FrameRequester::test_dummy(), show_welcome_banner: true, queued_user_messages: VecDeque::new(), diff --git a/codex-rs/tui/src/status/card.rs b/codex-rs/tui/src/status/card.rs index 7174cf41c9..b860b03941 100644 --- a/codex-rs/tui/src/status/card.rs +++ b/codex-rs/tui/src/status/card.rs @@ -66,6 +66,7 @@ struct StatusHistoryCell { model_provider: Option, account: Option, session_id: Option, + forked_from: Option, token_usage: StatusTokenUsageData, rate_limits: StatusRateLimitData, } @@ -77,6 +78,7 @@ pub(crate) fn new_status_output( token_info: Option<&TokenUsageInfo>, total_usage: &TokenUsage, session_id: &Option, + forked_from: Option, rate_limits: Option<&RateLimitSnapshotDisplay>, plan_type: Option, now: DateTime, @@ -89,6 +91,7 @@ pub(crate) fn new_status_output( token_info, total_usage, session_id, + forked_from, rate_limits, plan_type, now, @@ -106,6 +109,7 @@ impl StatusHistoryCell { token_info: Option<&TokenUsageInfo>, total_usage: &TokenUsage, session_id: &Option, + forked_from: Option, rate_limits: Option<&RateLimitSnapshotDisplay>, plan_type: Option, now: DateTime, @@ -134,6 +138,7 @@ impl StatusHistoryCell { let model_provider = format_model_provider(config); let account = compose_account_display(auth_manager, plan_type); let session_id = session_id.as_ref().map(std::string::ToString::to_string); + let forked_from = forked_from.map(|id| id.to_string()); let default_usage = TokenUsage::default(); let (context_usage, context_window) = match token_info { Some(info) => (&info.last_token_usage, info.model_context_window), @@ -163,6 +168,7 @@ impl StatusHistoryCell { model_provider, account, session_id, + forked_from, token_usage, rate_limits, } @@ -351,6 +357,9 @@ impl HistoryCell for StatusHistoryCell { if self.session_id.is_some() { push_label(&mut labels, &mut seen, "Session"); } + if self.session_id.is_some() && self.forked_from.is_some() { + push_label(&mut labels, &mut seen, "Forked from"); + } push_label(&mut labels, &mut seen, "Token usage"); if self.token_usage.context_window.is_some() { push_label(&mut labels, &mut seen, "Context window"); @@ -403,6 +412,11 @@ impl HistoryCell for StatusHistoryCell { if let Some(session) = self.session_id.as_ref() { lines.push(formatter.line("Session", vec![Span::from(session.clone())])); } + if self.session_id.is_some() + && let Some(forked_from) = self.forked_from.as_ref() + { + lines.push(formatter.line("Forked from", vec![Span::from(forked_from.clone())])); + } lines.push(Line::from(Vec::>::new())); // Hide token usage only for ChatGPT subscribers diff --git a/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_forked_from.snap b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_forked_from.snap new file mode 100644 index 0000000000..18772b7d76 --- /dev/null +++ b/codex-rs/tui/src/status/snapshots/codex_tui__status__tests__status_snapshot_includes_forked_from.snap @@ -0,0 +1,24 @@ +--- +source: tui/src/status/tests.rs +expression: sanitized +--- +/status + +╭───────────────────────────────────────────────────────────────────────╮ +│ >_ OpenAI Codex (v0.0.0) │ +│ │ +│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ +│ information on rate limits and credits │ +│ │ +│ Model: gpt-5.1-codex-max (reasoning none, summaries auto) │ +│ Directory: [[workspace]] │ +│ Approval: on-request │ +│ Sandbox: read-only │ +│ Agents.md: │ +│ Session: 0f0f3c13-6cf9-4aa4-8b80-7d49c2f1be2e │ +│ Forked from: e9f18a88-8081-4e51-9d4e-8af5cde2d8dd │ +│ │ +│ Token usage: 1.2K total (800 input + 400 output) │ +│ Context window: 100% left (1.2K used / 272K) │ +│ Limits: data not available yet │ +╰───────────────────────────────────────────────────────────────────────╯ diff --git a/codex-rs/tui/src/status/tests.rs b/codex-rs/tui/src/status/tests.rs index bb16f13328..b01a139522 100644 --- a/codex-rs/tui/src/status/tests.rs +++ b/codex-rs/tui/src/status/tests.rs @@ -14,6 +14,7 @@ use codex_core::protocol::RateLimitWindow; use codex_core::protocol::SandboxPolicy; use codex_core::protocol::TokenUsage; use codex_core::protocol::TokenUsageInfo; +use codex_protocol::ThreadId; use codex_protocol::config_types::ReasoningSummary; use codex_protocol::openai_models::ReasoningEffort; use insta::assert_snapshot; @@ -146,6 +147,7 @@ async fn status_snapshot_includes_reasoning_details() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -161,6 +163,57 @@ async fn status_snapshot_includes_reasoning_details() { assert_snapshot!(sanitized); } +#[tokio::test] +async fn status_snapshot_includes_forked_from() { + let temp_home = TempDir::new().expect("temp home"); + let mut config = test_config(&temp_home).await; + config.model = Some("gpt-5.1-codex-max".to_string()); + config.model_provider_id = "openai".to_string(); + config.cwd = PathBuf::from("/workspace/tests"); + + let auth_manager = test_auth_manager(&config); + let usage = TokenUsage { + input_tokens: 800, + cached_input_tokens: 0, + output_tokens: 400, + reasoning_output_tokens: 0, + total_tokens: 1_200, + }; + + let captured_at = chrono::Local + .with_ymd_and_hms(2024, 8, 9, 10, 11, 12) + .single() + .expect("valid time"); + + let model_slug = ModelsManager::get_model_offline(config.model.as_deref()); + let token_info = token_info_for(&model_slug, &config, &usage); + let session_id = + ThreadId::from_string("0f0f3c13-6cf9-4aa4-8b80-7d49c2f1be2e").expect("session id"); + let forked_from = + ThreadId::from_string("e9f18a88-8081-4e51-9d4e-8af5cde2d8dd").expect("forked id"); + + let composite = new_status_output( + &config, + &auth_manager, + Some(&token_info), + &usage, + &Some(session_id), + Some(forked_from), + None, + None, + captured_at, + &model_slug, + ); + let mut rendered_lines = render_lines(&composite.display_lines(80)); + if cfg!(windows) { + for line in &mut rendered_lines { + *line = line.replace('\\', "/"); + } + } + let sanitized = sanitize_directory(rendered_lines).join("\n"); + assert_snapshot!(sanitized); +} + #[tokio::test] async fn status_snapshot_includes_monthly_limit() { let temp_home = TempDir::new().expect("temp home"); @@ -202,6 +255,7 @@ async fn status_snapshot_includes_monthly_limit() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -246,6 +300,7 @@ async fn status_snapshot_shows_unlimited_credits() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -289,6 +344,7 @@ async fn status_snapshot_shows_positive_credits() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -332,6 +388,7 @@ async fn status_snapshot_hides_zero_credits() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -373,6 +430,7 @@ async fn status_snapshot_hides_when_has_no_credits_flag() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -416,6 +474,7 @@ async fn status_card_token_usage_excludes_cached_tokens() { &None, None, None, + None, now, &model_slug, ); @@ -470,6 +529,7 @@ async fn status_snapshot_truncates_in_narrow_terminal() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -517,6 +577,7 @@ async fn status_snapshot_shows_missing_limits_message() { &None, None, None, + None, now, &model_slug, ); @@ -578,6 +639,7 @@ async fn status_snapshot_includes_credits_and_limits() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -629,6 +691,7 @@ async fn status_snapshot_shows_empty_limits_message() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -689,6 +752,7 @@ async fn status_snapshot_shows_stale_limits_message() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, now, @@ -753,6 +817,7 @@ async fn status_snapshot_cached_limits_hide_credits_without_flag() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, now, @@ -809,6 +874,7 @@ async fn status_context_window_uses_last_usage() { &None, None, None, + None, now, &model_slug, ); diff --git a/codex-rs/tui2/src/app.rs b/codex-rs/tui2/src/app.rs index 52bf416419..5b66869056 100644 --- a/codex-rs/tui2/src/app.rs +++ b/codex-rs/tui2/src/app.rs @@ -2627,6 +2627,7 @@ mod tests { let make_header = |is_first| { let event = SessionConfiguredEvent { session_id: ThreadId::new(), + forked_from_id: None, model: "gpt-test".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, @@ -2668,6 +2669,7 @@ mod tests { id: String::new(), msg: EventMsg::SessionConfigured(SessionConfiguredEvent { session_id: base_id, + forked_from_id: None, model: "gpt-test".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, @@ -2950,6 +2952,7 @@ mod tests { let conversation_id = ThreadId::new(); let event = SessionConfiguredEvent { session_id: conversation_id, + forked_from_id: None, model: "gpt-test".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, diff --git a/codex-rs/tui2/src/chatwidget.rs b/codex-rs/tui2/src/chatwidget.rs index ab40b19c7b..c0944fa089 100644 --- a/codex-rs/tui2/src/chatwidget.rs +++ b/codex-rs/tui2/src/chatwidget.rs @@ -383,6 +383,7 @@ pub(crate) struct ChatWidget { // Previous status header to restore after a transient stream retry. retry_status_header: Option, conversation_id: Option, + forked_from: Option, frame_requester: FrameRequester, // Whether to include the initial welcome banner on session configured show_welcome_banner: bool, @@ -522,6 +523,7 @@ impl ChatWidget { .set_history_metadata(event.history_log_id, event.history_entry_count); self.set_skills(None); self.conversation_id = Some(event.session_id); + self.forked_from = event.forked_from_id; self.current_rollout_path = Some(event.rollout_path.clone()); let initial_messages = event.initial_messages.clone(); let model_for_header = event.model.clone(); @@ -1502,6 +1504,7 @@ impl ChatWidget { current_status_header: String::from("Working"), retry_status_header: None, conversation_id: None, + forked_from: None, queued_user_messages: VecDeque::new(), show_welcome_banner: is_first_run, suppress_session_configured_redraw: false, @@ -1598,6 +1601,7 @@ impl ChatWidget { current_status_header: String::from("Working"), retry_status_header: None, conversation_id: None, + forked_from: None, queued_user_messages: VecDeque::new(), show_welcome_banner: false, suppress_session_configured_redraw: true, @@ -2400,6 +2404,7 @@ impl ChatWidget { token_info, total_usage, &self.conversation_id, + self.forked_from, self.rate_limit_snapshot.as_ref(), self.plan_type, Local::now(), diff --git a/codex-rs/tui2/src/chatwidget/tests.rs b/codex-rs/tui2/src/chatwidget/tests.rs index fa555e597e..b3ad2114c7 100644 --- a/codex-rs/tui2/src/chatwidget/tests.rs +++ b/codex-rs/tui2/src/chatwidget/tests.rs @@ -120,6 +120,7 @@ async fn resumed_initial_messages_render_history() { let rollout_file = NamedTempFile::new().unwrap(); let configured = codex_core::protocol::SessionConfiguredEvent { session_id: conversation_id, + forked_from_id: None, model: "test-model".to_string(), model_provider_id: "test-provider".to_string(), approval_policy: AskForApproval::Never, @@ -414,6 +415,7 @@ async fn make_chatwidget_manual( current_status_header: String::from("Working"), retry_status_header: None, conversation_id: None, + forked_from: None, frame_requester: FrameRequester::test_dummy(), show_welcome_banner: true, queued_user_messages: VecDeque::new(), diff --git a/codex-rs/tui2/src/status/card.rs b/codex-rs/tui2/src/status/card.rs index 55896365e2..3e9e07d538 100644 --- a/codex-rs/tui2/src/status/card.rs +++ b/codex-rs/tui2/src/status/card.rs @@ -66,6 +66,7 @@ struct StatusHistoryCell { model_provider: Option, account: Option, session_id: Option, + forked_from: Option, token_usage: StatusTokenUsageData, rate_limits: StatusRateLimitData, } @@ -77,6 +78,7 @@ pub(crate) fn new_status_output( token_info: Option<&TokenUsageInfo>, total_usage: &TokenUsage, session_id: &Option, + forked_from: Option, rate_limits: Option<&RateLimitSnapshotDisplay>, plan_type: Option, now: DateTime, @@ -89,6 +91,7 @@ pub(crate) fn new_status_output( token_info, total_usage, session_id, + forked_from, rate_limits, plan_type, now, @@ -106,6 +109,7 @@ impl StatusHistoryCell { token_info: Option<&TokenUsageInfo>, total_usage: &TokenUsage, session_id: &Option, + forked_from: Option, rate_limits: Option<&RateLimitSnapshotDisplay>, plan_type: Option, now: DateTime, @@ -134,6 +138,7 @@ impl StatusHistoryCell { let model_provider = format_model_provider(config); let account = compose_account_display(auth_manager, plan_type); let session_id = session_id.as_ref().map(std::string::ToString::to_string); + let forked_from = forked_from.map(|id| id.to_string()); let default_usage = TokenUsage::default(); let (context_usage, context_window) = match token_info { Some(info) => (&info.last_token_usage, info.model_context_window), @@ -163,6 +168,7 @@ impl StatusHistoryCell { model_provider, account, session_id, + forked_from, token_usage, rate_limits, } @@ -351,6 +357,9 @@ impl HistoryCell for StatusHistoryCell { if self.session_id.is_some() { push_label(&mut labels, &mut seen, "Session"); } + if self.session_id.is_some() && self.forked_from.is_some() { + push_label(&mut labels, &mut seen, "Forked from"); + } push_label(&mut labels, &mut seen, "Token usage"); if self.token_usage.context_window.is_some() { push_label(&mut labels, &mut seen, "Context window"); @@ -402,6 +411,11 @@ impl HistoryCell for StatusHistoryCell { if let Some(session) = self.session_id.as_ref() { lines.push(formatter.line("Session", vec![Span::from(session.clone())])); } + if self.session_id.is_some() + && let Some(forked_from) = self.forked_from.as_ref() + { + lines.push(formatter.line("Forked from", vec![Span::from(forked_from.clone())])); + } lines.push(Line::from(Vec::>::new())); // Hide token usage only for ChatGPT subscribers diff --git a/codex-rs/tui2/src/status/snapshots/codex_tui2__status__tests__status_snapshot_includes_forked_from.snap b/codex-rs/tui2/src/status/snapshots/codex_tui2__status__tests__status_snapshot_includes_forked_from.snap new file mode 100644 index 0000000000..f238fd0eeb --- /dev/null +++ b/codex-rs/tui2/src/status/snapshots/codex_tui2__status__tests__status_snapshot_includes_forked_from.snap @@ -0,0 +1,24 @@ +--- +source: tui2/src/status/tests.rs +expression: sanitized +--- +/status + +╭───────────────────────────────────────────────────────────────────────╮ +│ >_ OpenAI Codex (v0.0.0) │ +│ │ +│ Visit https://chatgpt.com/codex/settings/usage for up-to-date │ +│ information on rate limits and credits │ +│ │ +│ Model: gpt-5.1-codex-max (reasoning none, summaries auto) │ +│ Directory: [[workspace]] │ +│ Approval: on-request │ +│ Sandbox: read-only │ +│ Agents.md: │ +│ Session: 0f0f3c13-6cf9-4aa4-8b80-7d49c2f1be2e │ +│ Forked from: e9f18a88-8081-4e51-9d4e-8af5cde2d8dd │ +│ │ +│ Token usage: 1.2K total (800 input + 400 output) │ +│ Context window: 100% left (1.2K used / 272K) │ +│ Limits: data not available yet │ +╰───────────────────────────────────────────────────────────────────────╯ diff --git a/codex-rs/tui2/src/status/tests.rs b/codex-rs/tui2/src/status/tests.rs index b16940a4de..e79688f44e 100644 --- a/codex-rs/tui2/src/status/tests.rs +++ b/codex-rs/tui2/src/status/tests.rs @@ -14,6 +14,7 @@ use codex_core::protocol::RateLimitWindow; use codex_core::protocol::SandboxPolicy; use codex_core::protocol::TokenUsage; use codex_core::protocol::TokenUsageInfo; +use codex_protocol::ThreadId; use codex_protocol::config_types::ReasoningSummary; use codex_protocol::openai_models::ReasoningEffort; use insta::assert_snapshot; @@ -146,6 +147,7 @@ async fn status_snapshot_includes_reasoning_details() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -160,6 +162,58 @@ async fn status_snapshot_includes_reasoning_details() { let sanitized = sanitize_directory(rendered_lines).join("\n"); assert_snapshot!(sanitized); } + +#[tokio::test] +async fn status_snapshot_includes_forked_from() { + let temp_home = TempDir::new().expect("temp home"); + let mut config = test_config(&temp_home).await; + config.model = Some("gpt-5.1-codex-max".to_string()); + config.model_provider_id = "openai".to_string(); + config.cwd = PathBuf::from("/workspace/tests"); + + let auth_manager = test_auth_manager(&config); + let usage = TokenUsage { + input_tokens: 800, + cached_input_tokens: 0, + output_tokens: 400, + reasoning_output_tokens: 0, + total_tokens: 1_200, + }; + + let captured_at = chrono::Local + .with_ymd_and_hms(2024, 8, 9, 10, 11, 12) + .single() + .expect("valid time"); + + let model_slug = ModelsManager::get_model_offline(config.model.as_deref()); + let token_info = token_info_for(&model_slug, &config, &usage); + let session_id = + ThreadId::from_string("0f0f3c13-6cf9-4aa4-8b80-7d49c2f1be2e").expect("session id"); + let forked_from = + ThreadId::from_string("e9f18a88-8081-4e51-9d4e-8af5cde2d8dd").expect("forked id"); + + let composite = new_status_output( + &config, + &auth_manager, + Some(&token_info), + &usage, + &Some(session_id), + Some(forked_from), + None, + None, + captured_at, + &model_slug, + ); + let mut rendered_lines = render_lines(&composite.display_lines(80)); + if cfg!(windows) { + for line in &mut rendered_lines { + *line = line.replace('\\', "/"); + } + } + let sanitized = sanitize_directory(rendered_lines).join("\n"); + assert_snapshot!(sanitized); +} + #[tokio::test] async fn status_snapshot_includes_monthly_limit() { let temp_home = TempDir::new().expect("temp home"); @@ -201,6 +255,7 @@ async fn status_snapshot_includes_monthly_limit() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -244,6 +299,7 @@ async fn status_snapshot_shows_unlimited_credits() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -287,6 +343,7 @@ async fn status_snapshot_shows_positive_credits() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -330,6 +387,7 @@ async fn status_snapshot_hides_zero_credits() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -371,6 +429,7 @@ async fn status_snapshot_hides_when_has_no_credits_flag() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -414,6 +473,7 @@ async fn status_card_token_usage_excludes_cached_tokens() { &None, None, None, + None, now, &model_slug, ); @@ -468,6 +528,7 @@ async fn status_snapshot_truncates_in_narrow_terminal() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -515,6 +576,7 @@ async fn status_snapshot_shows_missing_limits_message() { &None, None, None, + None, now, &model_slug, ); @@ -575,6 +637,7 @@ async fn status_snapshot_includes_credits_and_limits() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -626,6 +689,7 @@ async fn status_snapshot_shows_empty_limits_message() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, captured_at, @@ -686,6 +750,7 @@ async fn status_snapshot_shows_stale_limits_message() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, now, @@ -750,6 +815,7 @@ async fn status_snapshot_cached_limits_hide_credits_without_flag() { Some(&token_info), &usage, &None, + None, Some(&rate_display), None, now, @@ -806,6 +872,7 @@ async fn status_context_window_uses_last_usage() { &None, None, None, + None, now, &model_slug, ); From 246f50655111fda3d4fa4f2032285d03464fe249 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Fri, 16 Jan 2026 16:28:22 -0800 Subject: [PATCH 20/32] Introduce collaboration modes (#9340) - Merge `model` and `reasoning_effort` under collaboration modes. - Add additional instructions for custom collaboration mode - Default to Custom to not change behavior --- codex-rs/core/src/codex.rs | 135 +++++++++++++++++--------- codex-rs/protocol/src/config_types.rs | 69 +++++++++++++ 2 files changed, 156 insertions(+), 48 deletions(-) diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 00aa70aa69..b554450d11 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -160,12 +160,13 @@ use crate::user_notification::UserNotification; use crate::util::backoff; use codex_async_utils::OrCancelExt; use codex_otel::OtelManager; +use codex_protocol::config_types::CollaborationMode; use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig; +use codex_protocol::config_types::Settings; use codex_protocol::models::ContentItem; use codex_protocol::models::DeveloperInstructions; use codex_protocol::models::ResponseInputItem; use codex_protocol::models::ResponseItem; -use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig; use codex_protocol::protocol::CodexErrorInfo; use codex_protocol::protocol::InitialHistory; use codex_protocol::user_input::UserInput; @@ -270,10 +271,16 @@ impl Codex { crate::models_manager::manager::RefreshStrategy::OnlineIfUncached, ) .await; + // TODO (aibrahim): Consolidate config.model and config.model_reasoning_effort into config.collaboration_mode + // to avoid extracting these fields separately and constructing CollaborationMode here. + let collaboration_mode = CollaborationMode::Custom(Settings { + model: model.clone(), + reasoning_effort: config.model_reasoning_effort, + developer_instructions: None, + }); let session_configuration = SessionConfiguration { provider: config.model_provider.clone(), - model: model.clone(), - model_reasoning_effort: config.model_reasoning_effort, + collaboration_mode, model_reasoning_summary: config.model_reasoning_summary, developer_instructions: config.developer_instructions.clone(), user_instructions, @@ -422,10 +429,7 @@ pub(crate) struct SessionConfiguration { /// Provider identifier ("openai", "openrouter", ...). provider: ModelProviderInfo, - /// If not specified, server will use its default model. - model: String, - - model_reasoning_effort: Option, + collaboration_mode: CollaborationMode, model_reasoning_summary: ReasoningSummaryConfig, /// Developer instructions that supplement the base instructions. @@ -463,11 +467,8 @@ pub(crate) struct SessionConfiguration { impl SessionConfiguration { pub(crate) fn apply(&self, updates: &SessionSettingsUpdate) -> ConstraintResult { let mut next_configuration = self.clone(); - if let Some(model) = updates.model.clone() { - next_configuration.model = model; - } - if let Some(effort) = updates.reasoning_effort { - next_configuration.model_reasoning_effort = effort; + if let Some(collaboration_mode) = updates.collaboration_mode.clone() { + next_configuration.collaboration_mode = collaboration_mode; } if let Some(summary) = updates.reasoning_summary { next_configuration.model_reasoning_summary = summary; @@ -490,8 +491,7 @@ pub(crate) struct SessionSettingsUpdate { pub(crate) cwd: Option, pub(crate) approval_policy: Option, pub(crate) sandbox_policy: Option, - pub(crate) model: Option, - pub(crate) reasoning_effort: Option>, + pub(crate) collaboration_mode: Option, pub(crate) reasoning_summary: Option, pub(crate) final_output_json_schema: Option>, } @@ -502,7 +502,8 @@ impl Session { // todo(aibrahim): store this state somewhere else so we don't need to mut config let config = session_configuration.original_config_do_not_use.clone(); let mut per_turn_config = (*config).clone(); - per_turn_config.model_reasoning_effort = session_configuration.model_reasoning_effort; + per_turn_config.model_reasoning_effort = + session_configuration.collaboration_mode.reasoning_effort(); per_turn_config.model_reasoning_summary = session_configuration.model_reasoning_summary; per_turn_config.features = config.features.clone(); per_turn_config @@ -520,7 +521,7 @@ impl Session { sub_id: String, ) -> TurnContext { let otel_manager = otel_manager.clone().with_model( - session_configuration.model.as_str(), + session_configuration.collaboration_mode.model(), model_info.slug.as_str(), ); @@ -531,7 +532,7 @@ impl Session { model_info.clone(), otel_manager, provider, - session_configuration.model_reasoning_effort, + session_configuration.collaboration_mode.reasoning_effort(), session_configuration.model_reasoning_summary, conversation_id, session_configuration.session_source.clone(), @@ -579,7 +580,8 @@ impl Session { ) -> anyhow::Result> { debug!( "Configuring session: model={}; provider={:?}", - session_configuration.model, session_configuration.provider + session_configuration.collaboration_mode.model(), + session_configuration.provider ); if !session_configuration.cwd.is_absolute() { return Err(anyhow::anyhow!( @@ -655,8 +657,8 @@ impl Session { let auth = auth.as_ref(); let otel_manager = OtelManager::new( conversation_id, - session_configuration.model.as_str(), - session_configuration.model.as_str(), + session_configuration.collaboration_mode.model(), + session_configuration.collaboration_mode.model(), auth.and_then(CodexAuth::get_account_id), auth.and_then(CodexAuth::get_account_email), auth.map(|a| a.mode), @@ -680,7 +682,7 @@ impl Session { otel_manager.conversation_starts( config.model_provider.name.as_str(), - config.model_reasoning_effort, + session_configuration.collaboration_mode.reasoning_effort(), config.model_reasoning_summary, config.model_context_window, config.model_auto_compact_token_limit, @@ -737,12 +739,12 @@ impl Session { msg: EventMsg::SessionConfigured(SessionConfiguredEvent { session_id: conversation_id, forked_from_id, - model: session_configuration.model.clone(), + model: session_configuration.collaboration_mode.model().to_string(), model_provider_id: config.model_provider_id.clone(), approval_policy: session_configuration.approval_policy.value(), sandbox_policy: session_configuration.sandbox_policy.get().clone(), cwd: session_configuration.cwd.clone(), - reasoning_effort: session_configuration.model_reasoning_effort, + reasoning_effort: session_configuration.collaboration_mode.reasoning_effort(), history_log_id, history_entry_count, initial_messages, @@ -978,7 +980,10 @@ impl Session { let model_info = self .services .models_manager - .get_model_info(session_configuration.model.as_str(), &per_turn_config) + .get_model_info( + session_configuration.collaboration_mode.model(), + &per_turn_config, + ) .await; let mut turn_context: TurnContext = Self::make_turn_context( Some(Arc::clone(&self.services.auth_manager)), @@ -1837,6 +1842,13 @@ async fn submission_loop(sess: Arc, config: Arc, rx_sub: Receiv effort, summary, } => { + let collaboration_mode = { + let state = sess.state.lock().await; + state + .session_configuration + .collaboration_mode + .with_updates(model, effort, None) + }; handlers::override_turn_context( &sess, sub.id.clone(), @@ -1844,8 +1856,7 @@ async fn submission_loop(sess: Arc, config: Arc, rx_sub: Receiv cwd, approval_policy, sandbox_policy, - model, - reasoning_effort: effort, + collaboration_mode: Some(collaboration_mode), reasoning_summary: summary, ..Default::default() }, @@ -1953,6 +1964,8 @@ mod handlers { use codex_protocol::protocol::WarningEvent; use crate::context_manager::is_user_turn_boundary; + use codex_protocol::config_types::CollaborationMode; + use codex_protocol::config_types::Settings; use codex_protocol::user_input::UserInput; use codex_rmcp_client::ElicitationAction; use codex_rmcp_client::ElicitationResponse; @@ -1999,18 +2012,24 @@ mod handlers { summary, final_output_json_schema, items, - } => ( - items, - SessionSettingsUpdate { - cwd: Some(cwd), - approval_policy: Some(approval_policy), - sandbox_policy: Some(sandbox_policy), - model: Some(model), - reasoning_effort: Some(effort), - reasoning_summary: Some(summary), - final_output_json_schema: Some(final_output_json_schema), - }, - ), + } => { + let collaboration_mode = Some(CollaborationMode::Custom(Settings { + model, + reasoning_effort: effort, + developer_instructions: None, + })); + ( + items, + SessionSettingsUpdate { + cwd: Some(cwd), + approval_policy: Some(approval_policy), + sandbox_policy: Some(sandbox_policy), + collaboration_mode, + reasoning_summary: Some(summary), + final_output_json_schema: Some(final_output_json_schema), + }, + ) + } Op::UserInput { items, final_output_json_schema, @@ -3402,10 +3421,15 @@ mod tests { let config = build_test_config(codex_home.path()).await; let config = Arc::new(config); let model = ModelsManager::get_model_offline(config.model.as_deref()); + let reasoning_effort = config.model_reasoning_effort; + let collaboration_mode = CollaborationMode::Custom(Settings { + model, + reasoning_effort, + developer_instructions: None, + }); let session_configuration = SessionConfiguration { provider: config.model_provider.clone(), - model, - model_reasoning_effort: config.model_reasoning_effort, + collaboration_mode, model_reasoning_summary: config.model_reasoning_summary, developer_instructions: config.developer_instructions.clone(), user_instructions: config.user_instructions.clone(), @@ -3468,10 +3492,15 @@ mod tests { let config = build_test_config(codex_home.path()).await; let config = Arc::new(config); let model = ModelsManager::get_model_offline(config.model.as_deref()); + let reasoning_effort = config.model_reasoning_effort; + let collaboration_mode = CollaborationMode::Custom(Settings { + model, + reasoning_effort, + developer_instructions: None, + }); let session_configuration = SessionConfiguration { provider: config.model_provider.clone(), - model, - model_reasoning_effort: config.model_reasoning_effort, + collaboration_mode, model_reasoning_summary: config.model_reasoning_summary, developer_instructions: config.developer_instructions.clone(), user_instructions: config.user_instructions.clone(), @@ -3718,10 +3747,15 @@ mod tests { let exec_policy = ExecPolicyManager::default(); let (agent_status_tx, _agent_status_rx) = watch::channel(AgentStatus::PendingInit); let model = ModelsManager::get_model_offline(config.model.as_deref()); + let reasoning_effort = config.model_reasoning_effort; + let collaboration_mode = CollaborationMode::Custom(Settings { + model, + reasoning_effort, + developer_instructions: None, + }); let session_configuration = SessionConfiguration { provider: config.model_provider.clone(), - model, - model_reasoning_effort: config.model_reasoning_effort, + collaboration_mode, model_reasoning_summary: config.model_reasoning_summary, developer_instructions: config.developer_instructions.clone(), user_instructions: config.user_instructions.clone(), @@ -3735,7 +3769,7 @@ mod tests { }; let per_turn_config = Session::build_per_turn_config(&session_configuration); let model_info = ModelsManager::construct_model_info_offline( - session_configuration.model.as_str(), + session_configuration.collaboration_mode.model(), &per_turn_config, ); let otel_manager = otel_manager( @@ -3813,10 +3847,15 @@ mod tests { let exec_policy = ExecPolicyManager::default(); let (agent_status_tx, _agent_status_rx) = watch::channel(AgentStatus::PendingInit); let model = ModelsManager::get_model_offline(config.model.as_deref()); + let reasoning_effort = config.model_reasoning_effort; + let collaboration_mode = CollaborationMode::Custom(Settings { + model, + reasoning_effort, + developer_instructions: None, + }); let session_configuration = SessionConfiguration { provider: config.model_provider.clone(), - model, - model_reasoning_effort: config.model_reasoning_effort, + collaboration_mode, model_reasoning_summary: config.model_reasoning_summary, developer_instructions: config.developer_instructions.clone(), user_instructions: config.user_instructions.clone(), @@ -3830,7 +3869,7 @@ mod tests { }; let per_turn_config = Session::build_per_turn_config(&session_configuration); let model_info = ModelsManager::construct_model_info_offline( - session_configuration.model.as_str(), + session_configuration.collaboration_mode.model(), &per_turn_config, ); let otel_manager = otel_manager( diff --git a/codex-rs/protocol/src/config_types.rs b/codex-rs/protocol/src/config_types.rs index 3979bc3810..0febd2fdd5 100644 --- a/codex-rs/protocol/src/config_types.rs +++ b/codex-rs/protocol/src/config_types.rs @@ -4,6 +4,8 @@ use serde::Serialize; use strum_macros::Display; use ts_rs::TS; +use crate::openai_models::ReasoningEffort; + /// A summary of the reasoning performed by the model. This can be useful for /// debugging and understanding the model's reasoning process. /// See https://platform.openai.com/docs/guides/reasoning?api-mode=responses#reasoning-summaries @@ -127,3 +129,70 @@ pub enum AltScreenMode { /// Never use alternate screen (inline mode only). Never, } + +/// Collaboration mode for a Codex session. +#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, JsonSchema, TS)] +#[serde(tag = "mode", rename_all = "lowercase")] +pub enum CollaborationMode { + Plan(Settings), + Collaborate(Settings), + Execute(Settings), + Custom(Settings), +} + +impl CollaborationMode { + /// Returns a reference to the settings, regardless of variant. + fn settings(&self) -> &Settings { + match self { + CollaborationMode::Plan(settings) + | CollaborationMode::Collaborate(settings) + | CollaborationMode::Execute(settings) + | CollaborationMode::Custom(settings) => settings, + } + } + + pub fn model(&self) -> &str { + self.settings().model.as_str() + } + + pub fn reasoning_effort(&self) -> Option { + self.settings().reasoning_effort + } + + /// Updates the collaboration mode with new model and/or effort values. + /// + /// - `model`: `Some(s)` to update the model, `None` to keep the current model + /// - `effort`: `Some(Some(e))` to set effort to `e`, `Some(None)` to clear effort, `None` to keep current effort + /// - `developer_instructions`: `Some(s)` to update developer instructions, `None` to keep current + /// + /// Returns a new `CollaborationMode` with updated values, preserving the variant. + pub fn with_updates( + &self, + model: Option, + effort: Option>, + developer_instructions: Option, + ) -> Self { + let settings = self.settings(); + let updated_settings = Settings { + model: model.unwrap_or_else(|| settings.model.clone()), + reasoning_effort: effort.unwrap_or(settings.reasoning_effort), + developer_instructions: developer_instructions + .or_else(|| settings.developer_instructions.clone()), + }; + + match self { + CollaborationMode::Plan(_) => CollaborationMode::Plan(updated_settings), + CollaborationMode::Collaborate(_) => CollaborationMode::Collaborate(updated_settings), + CollaborationMode::Execute(_) => CollaborationMode::Execute(updated_settings), + CollaborationMode::Custom(_) => CollaborationMode::Custom(updated_settings), + } + } +} + +/// Settings for a collaboration mode. +#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, JsonSchema, TS)] +pub struct Settings { + pub model: String, + pub reasoning_effort: Option, + pub developer_instructions: Option, +} From ad8bf59cbfb6edbc1dd54626b23fa35c2a94a688 Mon Sep 17 00:00:00 2001 From: xl-openai Date: Fri, 16 Jan 2026 20:22:05 -0800 Subject: [PATCH 21/32] Support enable/disable skill via config/api. (#9328) In config.toml: ``` [[skills.config]] path = "/Users/xl/.codex/skills/my_skill/SKILL.md" enabled = false ``` API: skills/list, skills/config/write --- .../src/protocol/common.rs | 4 + .../app-server-protocol/src/protocol/v2.rs | 17 + codex-rs/app-server/README.md | 24 +- .../app-server/src/codex_message_processor.rs | 76 +++- codex-rs/core/config.schema.json | 48 ++- codex-rs/core/src/codex.rs | 6 +- codex-rs/core/src/config/edit.rs | 168 +++++++++ codex-rs/core/src/config/mod.rs | 4 + codex-rs/core/src/config/types.rs | 14 + codex-rs/core/src/skills/injection.rs | 12 +- codex-rs/core/src/skills/loader.rs | 335 +++++++----------- codex-rs/core/src/skills/manager.rs | 54 ++- codex-rs/core/src/skills/model.rs | 22 ++ codex-rs/tui/src/chatwidget.rs | 2 +- codex-rs/tui2/src/chatwidget.rs | 2 +- 15 files changed, 550 insertions(+), 238 deletions(-) diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index dd54eb25da..bce2e48c97 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -133,6 +133,10 @@ client_request_definitions! { params: v2::SkillsListParams, response: v2::SkillsListResponse, }, + SkillsConfigWrite => "skills/config/write" { + params: v2::SkillsConfigWriteParams, + response: v2::SkillsConfigWriteResponse, + }, TurnStart => "turn/start" { params: v2::TurnStartParams, response: v2::TurnStartResponse, diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index b44e1575ad..2855b6f578 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -1274,6 +1274,7 @@ pub struct SkillMetadata { pub interface: Option, pub path: PathBuf, pub scope: SkillScope, + pub enabled: bool, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] @@ -1311,6 +1312,21 @@ pub struct SkillsListEntry { pub errors: Vec, } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct SkillsConfigWriteParams { + pub path: PathBuf, + pub enabled: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct SkillsConfigWriteResponse { + pub effective_enabled: bool, +} + impl From for SkillMetadata { fn from(value: CoreSkillMetadata) -> Self { Self { @@ -1320,6 +1336,7 @@ impl From for SkillMetadata { interface: value.interface.map(SkillInterface::from), path: value.path, scope: value.scope.into(), + enabled: true, } } } diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 75e1853d70..891632796b 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -87,6 +87,7 @@ Example (from OpenAI's official VSCode extension): - `command/exec` — run a single command under the server sandbox without starting a thread/turn (handy for utilities and validation). - `model/list` — list available models (with reasoning effort options). - `skills/list` — list skills for one or more `cwd` values (optional `forceReload`). +- `skills/config/write` — write user-level skill config by path. - `mcpServer/oauth/login` — start an OAuth login for a configured MCP server; returns an `authorization_url` and later emits `mcpServer/oauthLogin/completed` once the browser flow finishes. - `config/mcpServer/reload` — reload MCP server config from disk and queue a refresh for loaded threads (applied on each thread's next active turn); returns `{}`. Use this after editing `config.toml` without restarting the server. - `mcpServerStatus/list` — enumerate configured MCP servers with their tools, resources, resource templates, and auth status; supports cursor+limit pagination. @@ -483,17 +484,30 @@ Example: $skill-creator Add a new skill for triaging flaky CI and include step-by-step usage. ``` -Use `skills/list` to fetch the available skills (optionally scoped by `cwd` and/or with `forceReload`). +Use `skills/list` to fetch the available skills (optionally scoped by `cwds`, with `forceReload`). ```json { "method": "skills/list", "id": 25, "params": { - "cwd": "/Users/me/project", + "cwds": ["/Users/me/project"], "forceReload": false } } { "id": 25, "result": { - "skills": [ - { "name": "skill-creator", "description": "Create or update a Codex skill" } - ] + "data": [{ + "cwd": "/Users/me/project", + "skills": [ + { "name": "skill-creator", "description": "Create or update a Codex skill", "enabled": true } + ], + "errors": [] + }] +} } +``` + +To enable or disable a skill by path: + +```json +{ "method": "skills/config/write", "id": 26, "params": { + "path": "/Users/me/.codex/skills/skill-creator/SKILL.md", + "enabled": false } } ``` diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 153028512e..fb6699a34f 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -85,6 +85,8 @@ use codex_app_server_protocol::ServerNotification; use codex_app_server_protocol::SessionConfiguredNotification; use codex_app_server_protocol::SetDefaultModelParams; use codex_app_server_protocol::SetDefaultModelResponse; +use codex_app_server_protocol::SkillsConfigWriteParams; +use codex_app_server_protocol::SkillsConfigWriteResponse; use codex_app_server_protocol::SkillsListParams; use codex_app_server_protocol::SkillsListResponse; use codex_app_server_protocol::Thread; @@ -131,6 +133,7 @@ use codex_core::auth::login_with_api_key; use codex_core::config::Config; use codex_core::config::ConfigOverrides; use codex_core::config::ConfigService; +use codex_core::config::edit::ConfigEdit; use codex_core::config::edit::ConfigEditsBuilder; use codex_core::config::types::McpServerTransportConfig; use codex_core::default_client::get_codex_user_agent; @@ -400,6 +403,9 @@ impl CodexMessageProcessor { ClientRequest::SkillsList { request_id, params } => { self.skills_list(request_id, params).await; } + ClientRequest::SkillsConfigWrite { request_id, params } => { + self.skills_config_write(request_id, params).await; + } ClientRequest::TurnStart { request_id, params } => { self.turn_start(request_id, params).await; } @@ -3246,7 +3252,7 @@ impl CodexMessageProcessor { for cwd in cwds { let outcome = skills_manager.skills_for_cwd(&cwd, force_reload).await; let errors = errors_to_info(&outcome.errors); - let skills = skills_to_info(&outcome.skills); + let skills = skills_to_info(&outcome.skills, &outcome.disabled_paths); data.push(codex_app_server_protocol::SkillsListEntry { cwd, skills, @@ -3258,6 +3264,37 @@ impl CodexMessageProcessor { .await; } + async fn skills_config_write(&self, request_id: RequestId, params: SkillsConfigWriteParams) { + let SkillsConfigWriteParams { path, enabled } = params; + let edits = vec![ConfigEdit::SetSkillConfig { path, enabled }]; + let result = ConfigEditsBuilder::new(&self.config.codex_home) + .with_edits(edits) + .apply() + .await; + + match result { + Ok(()) => { + self.thread_manager.skills_manager().clear_cache(); + self.outgoing + .send_response( + request_id, + SkillsConfigWriteResponse { + effective_enabled: enabled, + }, + ) + .await; + } + Err(err) => { + let error = JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: format!("failed to update skill settings: {err}"), + data: None, + }; + self.outgoing.send_error(request_id, error).await; + } + } + } + async fn interrupt_conversation( &mut self, request_id: RequestId, @@ -3918,25 +3955,30 @@ impl CodexMessageProcessor { fn skills_to_info( skills: &[codex_core::skills::SkillMetadata], + disabled_paths: &std::collections::HashSet, ) -> Vec { skills .iter() - .map(|skill| codex_app_server_protocol::SkillMetadata { - name: skill.name.clone(), - description: skill.description.clone(), - short_description: skill.short_description.clone(), - interface: skill.interface.clone().map(|interface| { - codex_app_server_protocol::SkillInterface { - display_name: interface.display_name, - short_description: interface.short_description, - icon_small: interface.icon_small, - icon_large: interface.icon_large, - brand_color: interface.brand_color, - default_prompt: interface.default_prompt, - } - }), - path: skill.path.clone(), - scope: skill.scope.into(), + .map(|skill| { + let enabled = !disabled_paths.contains(&skill.path); + codex_app_server_protocol::SkillMetadata { + name: skill.name.clone(), + description: skill.description.clone(), + short_description: skill.short_description.clone(), + interface: skill.interface.clone().map(|interface| { + codex_app_server_protocol::SkillInterface { + display_name: interface.display_name, + short_description: interface.short_description, + icon_small: interface.icon_small, + icon_large: interface.icon_large, + brand_color: interface.brand_color, + default_prompt: interface.default_prompt, + } + }), + path: skill.path.clone(), + scope: skill.scope.into(), + enabled, + } }) .collect() } diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 37a0cce703..e954d88c1a 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -75,6 +75,9 @@ "apply_patch_freeform": { "type": "boolean" }, + "child_agents_md": { + "type": "boolean" + }, "collab": { "type": "boolean" }, @@ -99,9 +102,6 @@ "experimental_windows_sandbox": { "type": "boolean" }, - "child_agents_md": { - "type": "boolean" - }, "include_apply_patch_tool": { "type": "boolean" }, @@ -373,6 +373,14 @@ "description": "When set to `true`, `AgentReasoningRawContentEvent` events will be shown in the UI/output. Defaults to `false`.", "type": "boolean" }, + "skills": { + "description": "User-level skill config entries keyed by SKILL.md path.", + "allOf": [ + { + "$ref": "#/definitions/SkillsConfig" + } + ] + }, "tool_output_token_limit": { "description": "Token budget applied when storing tool/function outputs in the context manager.", "type": "integer", @@ -543,6 +551,9 @@ "apply_patch_freeform": { "type": "boolean" }, + "child_agents_md": { + "type": "boolean" + }, "collab": { "type": "boolean" }, @@ -567,9 +578,6 @@ "experimental_windows_sandbox": { "type": "boolean" }, - "child_agents_md": { - "type": "boolean" - }, "include_apply_patch_tool": { "type": "boolean" }, @@ -1288,6 +1296,34 @@ }, "additionalProperties": false }, + "SkillConfig": { + "type": "object", + "required": [ + "enabled", + "path" + ], + "properties": { + "enabled": { + "type": "boolean" + }, + "path": { + "$ref": "#/definitions/AbsolutePathBuf" + } + }, + "additionalProperties": false + }, + "SkillsConfig": { + "type": "object", + "properties": { + "config": { + "type": "array", + "items": { + "$ref": "#/definitions/SkillConfig" + } + } + }, + "additionalProperties": false + }, "ToolsToml": { "type": "object", "properties": { diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index b554450d11..b59856f26f 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -251,7 +251,8 @@ impl Codex { ); } - let user_instructions = get_user_instructions(&config, Some(&loaded_skills.skills)).await; + let enabled_skills = loaded_skills.enabled_skills(); + let user_instructions = get_user_instructions(&config, Some(&enabled_skills)).await; let exec_policy = ExecPolicyManager::load(&config.features, &config.config_layer_stack) .await @@ -2264,7 +2265,8 @@ mod handlers { for cwd in cwds { let outcome = skills_manager.skills_for_cwd(&cwd, force_reload).await; let errors = super::errors_to_info(&outcome.errors); - let skills_metadata = super::skills_to_info(&outcome.skills); + let enabled_skills = outcome.enabled_skills(); + let skills_metadata = super::skills_to_info(&enabled_skills); skills.push(SkillsListEntry { cwd, skills: skills_metadata, diff --git a/codex-rs/core/src/config/edit.rs b/codex-rs/core/src/config/edit.rs index fb6829bb96..f97903b31a 100644 --- a/codex-rs/core/src/config/edit.rs +++ b/codex-rs/core/src/config/edit.rs @@ -9,6 +9,7 @@ use std::path::Path; use std::path::PathBuf; use tempfile::NamedTempFile; use tokio::task; +use toml_edit::ArrayOfTables; use toml_edit::DocumentMut; use toml_edit::Item as TomlItem; use toml_edit::Table as TomlTable; @@ -36,6 +37,8 @@ pub enum ConfigEdit { RecordModelMigrationSeen { from: String, to: String }, /// Replace the entire `[mcp_servers]` table. ReplaceMcpServers(BTreeMap), + /// Set or clear a skill config entry under `[[skills.config]]`. + SetSkillConfig { path: PathBuf, enabled: bool }, /// Set trust_level under `[projects.""]`, /// migrating inline tables to explicit tables. SetProjectTrustLevel { path: PathBuf, level: TrustLevel }, @@ -298,6 +301,9 @@ impl ConfigDocument { value(*acknowledged), )), ConfigEdit::ReplaceMcpServers(servers) => Ok(self.replace_mcp_servers(servers)), + ConfigEdit::SetSkillConfig { path, enabled } => { + Ok(self.set_skill_config(path.as_path(), *enabled)) + } ConfigEdit::SetPath { segments, value } => Ok(self.insert(segments, value.clone())), ConfigEdit::ClearPath { segments } => Ok(self.clear_owned(segments)), ConfigEdit::SetProjectTrustLevel { path, level } => { @@ -387,6 +393,113 @@ impl ConfigDocument { true } + fn set_skill_config(&mut self, path: &Path, enabled: bool) -> bool { + let normalized_path = normalize_skill_config_path(path); + let mut remove_skills_table = false; + let mut mutated = false; + + { + let root = self.doc.as_table_mut(); + let skills_item = match root.get_mut("skills") { + Some(item) => item, + None => { + if enabled { + return false; + } + root.insert( + "skills", + TomlItem::Table(document_helpers::new_implicit_table()), + ); + let Some(item) = root.get_mut("skills") else { + return false; + }; + item + } + }; + + if document_helpers::ensure_table_for_write(skills_item).is_none() { + if enabled { + return false; + } + *skills_item = TomlItem::Table(document_helpers::new_implicit_table()); + } + let Some(skills_table) = skills_item.as_table_mut() else { + return false; + }; + + let config_item = match skills_table.get_mut("config") { + Some(item) => item, + None => { + if enabled { + return false; + } + skills_table.insert("config", TomlItem::ArrayOfTables(ArrayOfTables::new())); + let Some(item) = skills_table.get_mut("config") else { + return false; + }; + item + } + }; + + if !matches!(config_item, TomlItem::ArrayOfTables(_)) { + if enabled { + return false; + } + *config_item = TomlItem::ArrayOfTables(ArrayOfTables::new()); + } + + let TomlItem::ArrayOfTables(overrides) = config_item else { + return false; + }; + + let existing_index = overrides.iter().enumerate().find_map(|(idx, table)| { + table + .get("path") + .and_then(|item| item.as_str()) + .map(Path::new) + .map(normalize_skill_config_path) + .filter(|value| *value == normalized_path) + .map(|_| idx) + }); + + if enabled { + if let Some(index) = existing_index { + overrides.remove(index); + mutated = true; + if overrides.is_empty() { + skills_table.remove("config"); + if skills_table.is_empty() { + remove_skills_table = true; + } + } + } + } else if let Some(index) = existing_index { + for (idx, table) in overrides.iter_mut().enumerate() { + if idx == index { + table["path"] = value(normalized_path); + table["enabled"] = value(false); + mutated = true; + break; + } + } + } else { + let mut entry = TomlTable::new(); + entry.set_implicit(false); + entry["path"] = value(normalized_path); + entry["enabled"] = value(false); + overrides.push(entry); + mutated = true; + } + } + + if remove_skills_table { + let root = self.doc.as_table_mut(); + root.remove("skills"); + } + + mutated + } + fn scoped_segments(&self, scope: Scope, segments: &[&str]) -> Vec { let resolved: Vec = segments .iter() @@ -494,6 +607,13 @@ impl ConfigDocument { } } +fn normalize_skill_config_path(path: &Path) -> String { + dunce::canonicalize(path) + .unwrap_or_else(|_| path.to_path_buf()) + .to_string_lossy() + .to_string() +} + /// Persist edits using a blocking strategy. pub fn apply_blocking( codex_home: &Path, @@ -737,6 +857,54 @@ model_reasoning_effort = "high" assert_eq!(contents, "enabled = true\n"); } + #[test] + fn set_skill_config_writes_disabled_entry() { + let tmp = tempdir().expect("tmpdir"); + let codex_home = tmp.path(); + + ConfigEditsBuilder::new(codex_home) + .with_edits([ConfigEdit::SetSkillConfig { + path: PathBuf::from("/tmp/skills/demo/SKILL.md"), + enabled: false, + }]) + .apply_blocking() + .expect("persist"); + + let contents = + std::fs::read_to_string(codex_home.join(CONFIG_TOML_FILE)).expect("read config"); + let expected = r#"[[skills.config]] +path = "/tmp/skills/demo/SKILL.md" +enabled = false +"#; + assert_eq!(contents, expected); + } + + #[test] + fn set_skill_config_removes_entry_when_enabled() { + let tmp = tempdir().expect("tmpdir"); + let codex_home = tmp.path(); + std::fs::write( + codex_home.join(CONFIG_TOML_FILE), + r#"[[skills.config]] +path = "/tmp/skills/demo/SKILL.md" +enabled = false +"#, + ) + .expect("seed config"); + + ConfigEditsBuilder::new(codex_home) + .with_edits([ConfigEdit::SetSkillConfig { + path: PathBuf::from("/tmp/skills/demo/SKILL.md"), + enabled: true, + }]) + .apply_blocking() + .expect("persist"); + + let contents = + std::fs::read_to_string(codex_home.join(CONFIG_TOML_FILE)).expect("read config"); + assert_eq!(contents, ""); + } + #[test] fn blocking_set_model_preserves_inline_table_contents() { let tmp = tempdir().expect("tmpdir"); diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 6228bc3ac3..956df12e9c 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -13,6 +13,7 @@ use crate::config::types::SandboxWorkspaceWrite; use crate::config::types::ScrollInputMode; use crate::config::types::ShellEnvironmentPolicy; use crate::config::types::ShellEnvironmentPolicyToml; +use crate::config::types::SkillsConfig; use crate::config::types::Tui; use crate::config::types::UriBasedFileOpener; use crate::config_loader::ConfigLayerStack; @@ -911,6 +912,9 @@ pub struct ConfigToml { /// Nested tools section for feature toggles pub tools: Option, + /// User-level skill config entries keyed by SKILL.md path. + pub skills: Option, + /// Centralized feature flags (new). Prefer this over individual toggles. #[serde(default)] // Injects known feature keys into the schema and forbids unknown keys. diff --git a/codex-rs/core/src/config/types.rs b/codex-rs/core/src/config/types.rs index 9d75302cbd..46c06d9a22 100644 --- a/codex-rs/core/src/config/types.rs +++ b/codex-rs/core/src/config/types.rs @@ -600,6 +600,20 @@ impl Notice { pub(crate) const TABLE_KEY: &'static str = "notice"; } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema)] +#[schemars(deny_unknown_fields)] +pub struct SkillConfig { + pub path: AbsolutePathBuf, + pub enabled: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)] +#[schemars(deny_unknown_fields)] +pub struct SkillsConfig { + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub config: Vec, +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[schemars(deny_unknown_fields)] pub struct SandboxWorkspaceWrite { diff --git a/codex-rs/core/src/skills/injection.rs b/codex-rs/core/src/skills/injection.rs index a143fce1f2..dfd7e2af59 100644 --- a/codex-rs/core/src/skills/injection.rs +++ b/codex-rs/core/src/skills/injection.rs @@ -1,4 +1,5 @@ use std::collections::HashSet; +use std::path::PathBuf; use crate::skills::SkillLoadOutcome; use crate::skills::SkillMetadata; @@ -25,7 +26,8 @@ pub(crate) async fn build_skill_injections( return SkillInjections::default(); }; - let mentioned_skills = collect_explicit_skill_mentions(inputs, &outcome.skills); + let mentioned_skills = + collect_explicit_skill_mentions(inputs, &outcome.skills, &outcome.disabled_paths); if mentioned_skills.is_empty() { return SkillInjections::default(); } @@ -46,9 +48,9 @@ pub(crate) async fn build_skill_injections( } Err(err) => { let message = format!( - "Failed to load skill {} at {}: {err:#}", - skill.name, - skill.path.display() + "Failed to load skill {name} at {path}: {err:#}", + name = skill.name, + path = skill.path.display() ); result.warnings.push(message); } @@ -61,6 +63,7 @@ pub(crate) async fn build_skill_injections( fn collect_explicit_skill_mentions( inputs: &[UserInput], skills: &[SkillMetadata], + disabled_paths: &HashSet, ) -> Vec { let mut selected: Vec = Vec::new(); let mut seen: HashSet = HashSet::new(); @@ -69,6 +72,7 @@ fn collect_explicit_skill_mentions( if let UserInput::Skill { name, path } = input && seen.insert(name.clone()) && let Some(skill) = skills.iter().find(|s| s.name == *name && s.path == *path) + && !disabled_paths.contains(&skill.path) { selected.push(skill.clone()); } diff --git a/codex-rs/core/src/skills/loader.rs b/codex-rs/core/src/skills/loader.rs index da51fea41e..f6dd3dae92 100644 --- a/codex-rs/core/src/skills/loader.rs +++ b/codex-rs/core/src/skills/loader.rs @@ -105,13 +105,13 @@ where discover_skills_under_root(&root.path, root.scope, &mut outcome); } - let mut seen: HashSet = HashSet::new(); + let mut seen: HashSet = HashSet::new(); outcome .skills - .retain(|skill| seen.insert(skill.name.clone())); + .retain(|skill| seen.insert(skill.path.clone())); fn scope_rank(scope: SkillScope) -> u8 { - // Higher-priority scopes first (matches dedupe priority order). + // Higher-priority scopes first (matches root scan order for dedupe). match scope { SkillScope::Repo => 0, SkillScope::User => 1, @@ -445,10 +445,23 @@ fn resolve_asset_path( return None; } - let mut components = path.components().peekable(); - while matches!(components.peek(), Some(Component::CurDir)) { - components.next(); + let mut normalized = PathBuf::new(); + for component in path.components() { + match component { + Component::CurDir => {} + Component::Normal(component) => normalized.push(component), + Component::ParentDir => { + tracing::warn!("ignoring {field}: icon path must not contain '..'"); + return None; + } + _ => { + tracing::warn!("ignoring {field}: icon path must be under assets/"); + return None; + } + } } + + let mut components = normalized.components(); match components.next() { Some(Component::Normal(component)) if component == "assets" => {} _ => { @@ -456,12 +469,8 @@ fn resolve_asset_path( return None; } } - if components.any(|component| matches!(component, Component::ParentDir)) { - tracing::warn!("ignoring {field}: icon path must not contain '..'"); - return None; - } - Some(skill_dir.join(path)) + Some(skill_dir.join(normalized)) } fn sanitize_single_line(raw: &str) -> String { @@ -707,8 +716,8 @@ default_prompt = " default prompt " interface: Some(SkillInterface { display_name: Some("UI Skill".to_string()), short_description: Some("short desc".to_string()), - icon_small: Some(normalized_skill_dir.join("./assets/small-400px.png")), - icon_large: Some(normalized_skill_dir.join("./assets/large-logo.svg")), + icon_small: Some(normalized_skill_dir.join("assets/small-400px.png")), + icon_large: Some(normalized_skill_dir.join("assets/large-logo.svg")), brand_color: Some("#3B82F6".to_string()), default_prompt: Some("default prompt".to_string()), }), @@ -753,7 +762,7 @@ icon_large = "./assets/logo.svg" display_name: Some("UI Skill".to_string()), short_description: None, icon_small: Some(normalized_skill_dir.join("assets/icon.png")), - icon_large: Some(normalized_skill_dir.join("./assets/logo.svg")), + icon_large: Some(normalized_skill_dir.join("assets/logo.svg")), brand_color: None, default_prompt: None, }), @@ -835,7 +844,7 @@ default_prompt = "{too_long}" interface: Some(SkillInterface { display_name: Some("UI Skill".to_string()), short_description: None, - icon_small: Some(normalized_skill_dir.join("./assets/small-400px.png")), + icon_small: Some(normalized_skill_dir.join("assets/small-400px.png")), icon_large: None, brand_color: None, default_prompt: None, @@ -1376,12 +1385,47 @@ icon_large = "./assets/../logo.svg" } #[tokio::test] - async fn deduplicates_by_name_preferring_repo_over_user() { + async fn deduplicates_by_path_preferring_first_root() { + let root = tempfile::tempdir().expect("tempdir"); + + let skill_path = write_skill_at(root.path(), "dupe", "dupe-skill", "from repo"); + + let outcome = load_skills_from_roots([ + SkillRoot { + path: root.path().to_path_buf(), + scope: SkillScope::Repo, + }, + SkillRoot { + path: root.path().to_path_buf(), + scope: SkillScope::User, + }, + ]); + + assert!( + outcome.errors.is_empty(), + "unexpected errors: {:?}", + outcome.errors + ); + assert_eq!( + outcome.skills, + vec![SkillMetadata { + name: "dupe-skill".to_string(), + description: "from repo".to_string(), + short_description: None, + interface: None, + path: normalized(&skill_path), + scope: SkillScope::Repo, + }] + ); + } + + #[tokio::test] + async fn keeps_duplicate_names_from_repo_and_user() { let codex_home = tempfile::tempdir().expect("tempdir"); let repo_dir = tempfile::tempdir().expect("tempdir"); mark_as_git_repo(repo_dir.path()); - let _user_skill_path = write_skill(&codex_home, "user", "dupe-skill", "from user"); + let user_skill_path = write_skill(&codex_home, "user", "dupe-skill", "from user"); let repo_skill_path = write_skill_at( &repo_dir .path() @@ -1402,42 +1446,94 @@ icon_large = "./assets/../logo.svg" ); assert_eq!( outcome.skills, - vec![SkillMetadata { - name: "dupe-skill".to_string(), - description: "from repo".to_string(), - short_description: None, - interface: None, - path: normalized(&repo_skill_path), - scope: SkillScope::Repo, - }] + vec![ + SkillMetadata { + name: "dupe-skill".to_string(), + description: "from repo".to_string(), + short_description: None, + interface: None, + path: normalized(&repo_skill_path), + scope: SkillScope::Repo, + }, + SkillMetadata { + name: "dupe-skill".to_string(), + description: "from user".to_string(), + short_description: None, + interface: None, + path: normalized(&user_skill_path), + scope: SkillScope::User, + }, + ] ); } #[tokio::test] - async fn loads_system_skills_when_present() { + async fn keeps_duplicate_names_from_nested_codex_dirs() { let codex_home = tempfile::tempdir().expect("tempdir"); + let repo_dir = tempfile::tempdir().expect("tempdir"); + mark_as_git_repo(repo_dir.path()); - let _system_skill_path = - write_system_skill(&codex_home, "system", "dupe-skill", "from system"); - let user_skill_path = write_skill(&codex_home, "user", "dupe-skill", "from user"); + let nested_dir = repo_dir.path().join("nested/inner"); + fs::create_dir_all(&nested_dir).unwrap(); - let cfg = make_config(&codex_home).await; + let root_skill_path = write_skill_at( + &repo_dir + .path() + .join(REPO_ROOT_CONFIG_DIR_NAME) + .join(SKILLS_DIR_NAME), + "root", + "dupe-skill", + "from root", + ); + let nested_skill_path = write_skill_at( + &repo_dir + .path() + .join("nested") + .join(REPO_ROOT_CONFIG_DIR_NAME) + .join(SKILLS_DIR_NAME), + "nested", + "dupe-skill", + "from nested", + ); + + let cfg = make_config_for_cwd(&codex_home, nested_dir).await; let outcome = load_skills(&cfg); + assert!( outcome.errors.is_empty(), "unexpected errors: {:?}", outcome.errors ); + let root_path = + canonicalize_path(&root_skill_path).unwrap_or_else(|_| root_skill_path.clone()); + let nested_path = + canonicalize_path(&nested_skill_path).unwrap_or_else(|_| nested_skill_path.clone()); + let (first_path, second_path, first_description, second_description) = + if root_path <= nested_path { + (root_path, nested_path, "from root", "from nested") + } else { + (nested_path, root_path, "from nested", "from root") + }; assert_eq!( outcome.skills, - vec![SkillMetadata { - name: "dupe-skill".to_string(), - description: "from user".to_string(), - short_description: None, - interface: None, - path: normalized(&user_skill_path), - scope: SkillScope::User, - }] + vec![ + SkillMetadata { + name: "dupe-skill".to_string(), + description: first_description.to_string(), + short_description: None, + interface: None, + path: first_path, + scope: SkillScope::Repo, + }, + SkillMetadata { + name: "dupe-skill".to_string(), + description: second_description.to_string(), + short_description: None, + interface: None, + path: second_path, + scope: SkillScope::Repo, + }, + ] ); } @@ -1448,7 +1544,7 @@ icon_large = "./assets/../logo.svg" let repo_dir = outer_dir.path().join("repo"); fs::create_dir_all(&repo_dir).unwrap(); - write_skill_at( + let _skill_path = write_skill_at( &outer_dir .path() .join(REPO_ROOT_CONFIG_DIR_NAME) @@ -1457,7 +1553,6 @@ icon_large = "./assets/../logo.svg" "outer-skill", "from outer", ); - mark_as_git_repo(&repo_dir); let cfg = make_config_for_cwd(&codex_home, repo_dir).await; @@ -1581,164 +1676,4 @@ icon_large = "./assets/../logo.svg" } assert_eq!(scopes, expected); } - - #[tokio::test] - async fn deduplicates_by_name_preferring_system_over_admin() { - let system_dir = tempfile::tempdir().expect("tempdir"); - let admin_dir = tempfile::tempdir().expect("tempdir"); - - let system_skill_path = - write_skill_at(system_dir.path(), "system", "dupe-skill", "from system"); - let _admin_skill_path = - write_skill_at(admin_dir.path(), "admin", "dupe-skill", "from admin"); - - let outcome = load_skills_from_roots([ - SkillRoot { - path: system_dir.path().to_path_buf(), - scope: SkillScope::System, - }, - SkillRoot { - path: admin_dir.path().to_path_buf(), - scope: SkillScope::Admin, - }, - ]); - - assert!( - outcome.errors.is_empty(), - "unexpected errors: {:?}", - outcome.errors - ); - assert_eq!( - outcome.skills, - vec![SkillMetadata { - name: "dupe-skill".to_string(), - description: "from system".to_string(), - short_description: None, - interface: None, - path: normalized(&system_skill_path), - scope: SkillScope::System, - }] - ); - } - - #[tokio::test] - async fn deduplicates_by_name_preferring_user_over_system() { - let codex_home = tempfile::tempdir().expect("tempdir"); - let work_dir = tempfile::tempdir().expect("tempdir"); - - let user_skill_path = write_skill(&codex_home, "user", "dupe-skill", "from user"); - let _system_skill_path = - write_system_skill(&codex_home, "system", "dupe-skill", "from system"); - - let cfg = make_config_for_cwd(&codex_home, work_dir.path().to_path_buf()).await; - - let outcome = load_skills(&cfg); - assert!( - outcome.errors.is_empty(), - "unexpected errors: {:?}", - outcome.errors - ); - assert_eq!( - outcome.skills, - vec![SkillMetadata { - name: "dupe-skill".to_string(), - description: "from user".to_string(), - short_description: None, - interface: None, - path: normalized(&user_skill_path), - scope: SkillScope::User, - }] - ); - } - - #[tokio::test] - async fn deduplicates_by_name_preferring_repo_over_system() { - let codex_home = tempfile::tempdir().expect("tempdir"); - let repo_dir = tempfile::tempdir().expect("tempdir"); - mark_as_git_repo(repo_dir.path()); - - let repo_skill_path = write_skill_at( - &repo_dir - .path() - .join(REPO_ROOT_CONFIG_DIR_NAME) - .join(SKILLS_DIR_NAME), - "repo", - "dupe-skill", - "from repo", - ); - let _system_skill_path = - write_system_skill(&codex_home, "system", "dupe-skill", "from system"); - - let cfg = make_config_for_cwd(&codex_home, repo_dir.path().to_path_buf()).await; - - let outcome = load_skills(&cfg); - assert!( - outcome.errors.is_empty(), - "unexpected errors: {:?}", - outcome.errors - ); - assert_eq!( - outcome.skills, - vec![SkillMetadata { - name: "dupe-skill".to_string(), - description: "from repo".to_string(), - short_description: None, - interface: None, - path: normalized(&repo_skill_path), - scope: SkillScope::Repo, - }] - ); - } - - #[tokio::test] - async fn deduplicates_by_name_preferring_nearest_project_codex_dir() { - let codex_home = tempfile::tempdir().expect("tempdir"); - let repo_dir = tempfile::tempdir().expect("tempdir"); - mark_as_git_repo(repo_dir.path()); - - let nested_dir = repo_dir.path().join("nested/inner"); - fs::create_dir_all(&nested_dir).unwrap(); - - let _root_skill_path = write_skill_at( - &repo_dir - .path() - .join(REPO_ROOT_CONFIG_DIR_NAME) - .join(SKILLS_DIR_NAME), - "root", - "dupe-skill", - "from root", - ); - let nested_skill_path = write_skill_at( - &repo_dir - .path() - .join("nested") - .join(REPO_ROOT_CONFIG_DIR_NAME) - .join(SKILLS_DIR_NAME), - "nested", - "dupe-skill", - "from nested", - ); - - let cfg = make_config_for_cwd(&codex_home, nested_dir).await; - let outcome = load_skills(&cfg); - - assert!( - outcome.errors.is_empty(), - "unexpected errors: {:?}", - outcome.errors - ); - let expected_path = - canonicalize_path(&nested_skill_path).unwrap_or_else(|_| nested_skill_path.clone()); - assert_eq!( - vec![SkillMetadata { - name: "dupe-skill".to_string(), - description: "from nested".to_string(), - short_description: None, - interface: None, - path: expected_path, - scope: SkillScope::Repo, - }], - outcome.skills - ); - } } diff --git a/codex-rs/core/src/skills/manager.rs b/codex-rs/core/src/skills/manager.rs index bff928a565..8aa6986882 100644 --- a/codex-rs/core/src/skills/manager.rs +++ b/codex-rs/core/src/skills/manager.rs @@ -1,12 +1,15 @@ use std::collections::HashMap; +use std::collections::HashSet; use std::path::Path; use std::path::PathBuf; use std::sync::RwLock; use codex_utils_absolute_path::AbsolutePathBuf; use toml::Value as TomlValue; +use tracing::warn; use crate::config::Config; +use crate::config::types::SkillsConfig; use crate::config_loader::LoaderOverrides; use crate::config_loader::load_config_layers_state; use crate::skills::SkillLoadOutcome; @@ -44,7 +47,8 @@ impl SkillsManager { } let roots = skill_roots_from_layer_stack(&config.config_layer_stack); - let outcome = load_skills_from_roots(roots); + let mut outcome = load_skills_from_roots(roots); + outcome.disabled_paths = disabled_paths_from_stack(&config.config_layer_stack); match self.cache_by_cwd.write() { Ok(mut cache) => { cache.insert(cwd.to_path_buf(), outcome.clone()); @@ -100,7 +104,8 @@ impl SkillsManager { }; let roots = skill_roots_from_layer_stack(&config_layer_stack); - let outcome = load_skills_from_roots(roots); + let mut outcome = load_skills_from_roots(roots); + outcome.disabled_paths = disabled_paths_from_stack(&config_layer_stack); match self.cache_by_cwd.write() { Ok(mut cache) => { cache.insert(cwd.to_path_buf(), outcome.clone()); @@ -111,6 +116,51 @@ impl SkillsManager { } outcome } + + pub fn clear_cache(&self) { + match self.cache_by_cwd.write() { + Ok(mut cache) => cache.clear(), + Err(err) => err.into_inner().clear(), + } + } +} + +fn disabled_paths_from_stack( + config_layer_stack: &crate::config_loader::ConfigLayerStack, +) -> HashSet { + let mut disabled = HashSet::new(); + let mut configs = HashMap::new(); + // Skills config is user-layer only for now; higher-precedence layers are ignored. + let Some(user_layer) = config_layer_stack.get_user_layer() else { + return disabled; + }; + let Some(skills_value) = user_layer.config.get("skills") else { + return disabled; + }; + let skills: SkillsConfig = match skills_value.clone().try_into() { + Ok(skills) => skills, + Err(err) => { + warn!("invalid skills config: {err}"); + return disabled; + } + }; + + for entry in skills.config { + let path = normalize_override_path(entry.path.as_path()); + configs.insert(path, entry.enabled); + } + + for (path, enabled) in configs { + if !enabled { + disabled.insert(path); + } + } + + disabled +} + +fn normalize_override_path(path: &Path) -> PathBuf { + dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) } #[cfg(test)] diff --git a/codex-rs/core/src/skills/model.rs b/codex-rs/core/src/skills/model.rs index fba00d2711..fe3357f9d9 100644 --- a/codex-rs/core/src/skills/model.rs +++ b/codex-rs/core/src/skills/model.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; use std::path::PathBuf; use codex_protocol::protocol::SkillScope; @@ -32,4 +33,25 @@ pub struct SkillError { pub struct SkillLoadOutcome { pub skills: Vec, pub errors: Vec, + pub disabled_paths: HashSet, +} + +impl SkillLoadOutcome { + pub fn is_skill_enabled(&self, skill: &SkillMetadata) -> bool { + !self.disabled_paths.contains(&skill.path) + } + + pub fn enabled_skills(&self) -> Vec { + self.skills + .iter() + .filter(|skill| self.is_skill_enabled(skill)) + .cloned() + .collect() + } + + pub fn skills_with_enabled(&self) -> impl Iterator { + self.skills + .iter() + .map(|skill| (skill, self.is_skill_enabled(skill))) + } } diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index e1054d0a3e..3593e1f16b 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -622,7 +622,7 @@ impl ChatWidget { self.submit_op(Op::ListCustomPrompts); self.submit_op(Op::ListSkills { cwds: Vec::new(), - force_reload: false, + force_reload: true, }); if let Some(user_message) = self.initial_user_message.take() { self.submit_user_message(user_message); diff --git a/codex-rs/tui2/src/chatwidget.rs b/codex-rs/tui2/src/chatwidget.rs index c0944fa089..5b3343d5b4 100644 --- a/codex-rs/tui2/src/chatwidget.rs +++ b/codex-rs/tui2/src/chatwidget.rs @@ -544,7 +544,7 @@ impl ChatWidget { self.submit_op(Op::ListCustomPrompts); self.submit_op(Op::ListSkills { cwds: Vec::new(), - force_reload: false, + force_reload: true, }); if let Some(user_message) = self.initial_user_message.take() { self.submit_user_message(user_message); From 146d54cede3c4ea38fbcacc89ab15a7e30d73b20 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Fri, 16 Jan 2026 21:51:25 -0800 Subject: [PATCH 22/32] Add collaboration_mode override to turns (#9408) --- .../app-server-protocol/src/protocol/v2.rs | 5 ++ .../app-server/src/codex_message_processor.rs | 5 +- .../app-server/tests/suite/v2/turn_start.rs | 76 +++++++++++++++++++ codex-rs/core/src/codex.rs | 27 ++++--- codex-rs/core/tests/common/test_codex.rs | 1 + codex-rs/core/tests/suite/apply_patch_cli.rs | 7 ++ codex-rs/core/tests/suite/approvals.rs | 1 + codex-rs/core/tests/suite/client.rs | 57 ++++++++++++++ codex-rs/core/tests/suite/compact.rs | 1 + codex-rs/core/tests/suite/exec_policy.rs | 1 + codex-rs/core/tests/suite/image_rollout.rs | 2 + codex-rs/core/tests/suite/json_result.rs | 1 + codex-rs/core/tests/suite/model_overrides.rs | 2 + codex-rs/core/tests/suite/models_cache_ttl.rs | 1 + .../core/tests/suite/models_etag_responses.rs | 1 + .../core/tests/suite/permissions_messages.rs | 3 + codex-rs/core/tests/suite/prompt_caching.rs | 27 ++++++- codex-rs/core/tests/suite/remote_models.rs | 4 + codex-rs/core/tests/suite/review.rs | 1 + codex-rs/core/tests/suite/rmcp_client.rs | 6 ++ codex-rs/core/tests/suite/shell_snapshot.rs | 3 + codex-rs/core/tests/suite/skills.rs | 1 + codex-rs/core/tests/suite/tool_harness.rs | 5 ++ codex-rs/core/tests/suite/tool_parallelism.rs | 2 + codex-rs/core/tests/suite/truncation.rs | 1 + codex-rs/core/tests/suite/unified_exec.rs | 25 ++++++ codex-rs/core/tests/suite/view_image.rs | 6 ++ codex-rs/exec/src/lib.rs | 1 + codex-rs/protocol/src/protocol.rs | 11 +++ codex-rs/tui/src/app.rs | 1 + codex-rs/tui/src/chatwidget.rs | 4 + codex-rs/tui2/src/app.rs | 1 + codex-rs/tui2/src/chatwidget.rs | 4 + 33 files changed, 281 insertions(+), 13 deletions(-) diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 2855b6f578..c0caa7ea32 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; use crate::protocol::common::AuthMode; use codex_protocol::account::PlanType; use codex_protocol::approvals::ExecPolicyAmendment as CoreExecPolicyAmendment; +use codex_protocol::config_types::CollaborationMode; use codex_protocol::config_types::ForcedLoginMethod; use codex_protocol::config_types::ReasoningSummary; use codex_protocol::config_types::SandboxMode as CoreSandboxMode; @@ -1538,6 +1539,10 @@ pub struct TurnStartParams { pub summary: Option, /// Optional JSON Schema used to constrain the final assistant message for this turn. pub output_schema: Option, + + /// EXPERIMENTAL - set a pre-set collaboration mode. + /// Takes precedence over model, reasoning_effort, and developer instructions if set. + pub collaboration_mode: Option, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index fb6699a34f..551015f94a 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -3231,6 +3231,7 @@ impl CodexMessageProcessor { effort, summary, final_output_json_schema: output_schema, + collaboration_mode: None, }) .await; @@ -3344,7 +3345,8 @@ impl CodexMessageProcessor { || params.sandbox_policy.is_some() || params.model.is_some() || params.effort.is_some() - || params.summary.is_some(); + || params.summary.is_some() + || params.collaboration_mode.is_some(); // If any overrides are provided, update the session turn context first. if has_any_overrides { @@ -3356,6 +3358,7 @@ impl CodexMessageProcessor { model: params.model, effort: params.effort.map(Some), summary: params.summary, + collaboration_mode: params.collaboration_mode, }) .await; } 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 e3b758a4c6..4c8df56c3d 100644 --- a/codex-rs/app-server/tests/suite/v2/turn_start.rs +++ b/codex-rs/app-server/tests/suite/v2/turn_start.rs @@ -35,7 +35,10 @@ use codex_app_server_protocol::TurnStartedNotification; use codex_app_server_protocol::TurnStatus; use codex_app_server_protocol::UserInput as V2UserInput; use codex_core::protocol_config_types::ReasoningSummary; +use codex_protocol::config_types::CollaborationMode; +use codex_protocol::config_types::Settings; use codex_protocol::openai_models::ReasoningEffort; +use core_test_support::responses; use core_test_support::skip_if_no_network; use pretty_assertions::assert_eq; use std::path::Path; @@ -305,6 +308,77 @@ async fn turn_start_emits_notifications_and_accepts_model_override() -> Result<( Ok(()) } +#[tokio::test] +async fn turn_start_accepts_collaboration_mode_override_v2() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = responses::start_mock_server().await; + let body = responses::sse(vec![ + responses::ev_response_created("resp-1"), + responses::ev_assistant_message("msg-1", "Done"), + responses::ev_completed("resp-1"), + ]); + let response_mock = responses::mount_sse_once(&server, body).await; + + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri(), "never")?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let thread_req = mcp + .send_thread_start_request(ThreadStartParams { + ..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 collaboration_mode = CollaborationMode::Custom(Settings { + model: "mock-model-collab".to_string(), + reasoning_effort: Some(ReasoningEffort::High), + developer_instructions: None, + }); + + let turn_req = mcp + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![V2UserInput::Text { + text: "Hello".to_string(), + text_elements: Vec::new(), + }], + model: Some("mock-model-override".to_string()), + effort: Some(ReasoningEffort::Low), + summary: Some(ReasoningSummary::Auto), + output_schema: None, + collaboration_mode: Some(collaboration_mode), + ..Default::default() + }) + .await?; + let turn_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(turn_req)), + ) + .await??; + let _turn: TurnStartResponse = to_response::(turn_resp)?; + + timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("turn/completed"), + ) + .await??; + + let request = response_mock.single_request(); + let payload = request.body_json(); + assert_eq!(payload["model"].as_str(), Some("mock-model-collab")); + + Ok(()) +} + #[tokio::test] async fn turn_start_accepts_local_image_input() -> Result<()> { // Two Codex turns hit the mock model (session start + turn/start). @@ -703,6 +777,7 @@ async fn turn_start_updates_sandbox_and_cwd_between_turns_v2() -> Result<()> { effort: Some(ReasoningEffort::Medium), summary: Some(ReasoningSummary::Auto), output_schema: None, + collaboration_mode: None, }) .await?; timeout( @@ -732,6 +807,7 @@ async fn turn_start_updates_sandbox_and_cwd_between_turns_v2() -> Result<()> { effort: Some(ReasoningEffort::Medium), summary: Some(ReasoningSummary::Auto), output_schema: None, + collaboration_mode: None, }) .await?; timeout( diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index b59856f26f..a9dc1004ab 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -1842,13 +1842,17 @@ async fn submission_loop(sess: Arc, config: Arc, rx_sub: Receiv model, effort, summary, + collaboration_mode, } => { - let collaboration_mode = { + let collaboration_mode = if let Some(collab_mode) = collaboration_mode { + collab_mode + } else { let state = sess.state.lock().await; - state - .session_configuration - .collaboration_mode - .with_updates(model, effort, None) + state.session_configuration.collaboration_mode.with_updates( + model.clone(), + effort, + None, + ) }; handlers::override_turn_context( &sess, @@ -2013,12 +2017,15 @@ mod handlers { summary, final_output_json_schema, items, + collaboration_mode, } => { - let collaboration_mode = Some(CollaborationMode::Custom(Settings { - model, - reasoning_effort: effort, - developer_instructions: None, - })); + let collaboration_mode = collaboration_mode.or_else(|| { + Some(CollaborationMode::Custom(Settings { + model: model.clone(), + reasoning_effort: effort, + developer_instructions: None, + })) + }); ( items, SessionSettingsUpdate { diff --git a/codex-rs/core/tests/common/test_codex.rs b/codex-rs/core/tests/common/test_codex.rs index 7bf0819488..9a902b6d12 100644 --- a/codex-rs/core/tests/common/test_codex.rs +++ b/codex-rs/core/tests/common/test_codex.rs @@ -279,6 +279,7 @@ impl TestCodex { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/apply_patch_cli.rs b/codex-rs/core/tests/suite/apply_patch_cli.rs index 5f764d227a..69147843c4 100644 --- a/codex-rs/core/tests/suite/apply_patch_cli.rs +++ b/codex-rs/core/tests/suite/apply_patch_cli.rs @@ -311,6 +311,7 @@ async fn apply_patch_cli_move_without_content_change_has_no_turn_diff( model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -898,6 +899,7 @@ async fn apply_patch_shell_command_heredoc_with_cd_emits_turn_diff() -> Result<( model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -976,6 +978,7 @@ async fn apply_patch_shell_command_failure_propagates_error_and_skips_diff() -> model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1124,6 +1127,7 @@ async fn apply_patch_emits_turn_diff_event_with_unified_diff( model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1185,6 +1189,7 @@ async fn apply_patch_turn_diff_for_rename_with_content_change( model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1254,6 +1259,7 @@ async fn apply_patch_aggregates_diff_across_multiple_tool_calls() -> Result<()> model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1323,6 +1329,7 @@ async fn apply_patch_aggregates_diff_preserves_success_after_failure() -> Result model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/approvals.rs b/codex-rs/core/tests/suite/approvals.rs index 357fdd124a..18abb2f189 100644 --- a/codex-rs/core/tests/suite/approvals.rs +++ b/codex-rs/core/tests/suite/approvals.rs @@ -501,6 +501,7 @@ async fn submit_turn( model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/client.rs b/codex-rs/core/tests/suite/client.rs index 6f659e1b64..cfae930d95 100644 --- a/codex-rs/core/tests/suite/client.rs +++ b/codex-rs/core/tests/suite/client.rs @@ -22,7 +22,9 @@ use codex_core::protocol::Op; use codex_core::protocol::SessionSource; use codex_otel::OtelManager; use codex_protocol::ThreadId; +use codex_protocol::config_types::CollaborationMode; use codex_protocol::config_types::ReasoningSummary; +use codex_protocol::config_types::Settings; use codex_protocol::config_types::Verbosity; use codex_protocol::models::FunctionCallOutputPayload; use codex_protocol::models::ReasoningItemContent; @@ -44,6 +46,7 @@ use core_test_support::test_codex::test_codex; use core_test_support::wait_for_event; use dunce::canonicalize as normalize_path; use futures::StreamExt; +use pretty_assertions::assert_eq; use serde_json::json; use std::io::Write; use std::sync::Arc; @@ -865,6 +868,60 @@ async fn includes_default_reasoning_effort_in_request_when_defined_by_model_info Ok(()) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn user_turn_collaboration_mode_overrides_model_and_effort() -> anyhow::Result<()> { + skip_if_no_network!(Ok(())); + let server = MockServer::start().await; + + let resp_mock = mount_sse_once(&server, sse_completed("resp1")).await; + let TestCodex { + codex, + config, + session_configured, + .. + } = test_codex() + .with_model("gpt-5.1-codex") + .build(&server) + .await?; + + let collaboration_mode = CollaborationMode::Custom(Settings { + model: "gpt-5.1".to_string(), + reasoning_effort: Some(ReasoningEffort::High), + developer_instructions: None, + }); + + codex + .submit(Op::UserTurn { + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + cwd: config.cwd.clone(), + approval_policy: config.approval_policy.value(), + sandbox_policy: config.sandbox_policy.get().clone(), + model: session_configured.model.clone(), + effort: Some(ReasoningEffort::Low), + summary: config.model_reasoning_summary, + collaboration_mode: Some(collaboration_mode), + final_output_json_schema: None, + }) + .await?; + + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let request_body = resp_mock.single_request().body_json(); + assert_eq!(request_body["model"].as_str(), Some("gpt-5.1")); + assert_eq!( + request_body + .get("reasoning") + .and_then(|t| t.get("effort")) + .and_then(|v| v.as_str()), + Some("high") + ); + + Ok(()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn configured_reasoning_summary_is_sent() -> anyhow::Result<()> { skip_if_no_network!(Ok(())); diff --git a/codex-rs/core/tests/suite/compact.rs b/codex-rs/core/tests/suite/compact.rs index 79bac55a8b..d033c66644 100644 --- a/codex-rs/core/tests/suite/compact.rs +++ b/codex-rs/core/tests/suite/compact.rs @@ -1288,6 +1288,7 @@ async fn auto_compact_runs_after_resume_when_token_usage_is_over_limit() { model: resumed.session_configured.model.clone(), effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await .unwrap(); diff --git a/codex-rs/core/tests/suite/exec_policy.rs b/codex-rs/core/tests/suite/exec_policy.rs index c7ae447bd0..70a3153344 100644 --- a/codex-rs/core/tests/suite/exec_policy.rs +++ b/codex-rs/core/tests/suite/exec_policy.rs @@ -81,6 +81,7 @@ async fn execpolicy_blocks_shell_invocation() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/image_rollout.rs b/codex-rs/core/tests/suite/image_rollout.rs index 0df7c7e731..3c3f92ce45 100644 --- a/codex-rs/core/tests/suite/image_rollout.rs +++ b/codex-rs/core/tests/suite/image_rollout.rs @@ -127,6 +127,7 @@ async fn copy_paste_local_image_persists_rollout_request_shape() -> anyhow::Resu model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -205,6 +206,7 @@ async fn drag_drop_image_persists_rollout_request_shape() -> anyhow::Result<()> model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/json_result.rs b/codex-rs/core/tests/suite/json_result.rs index d90cb217c8..06d42e8a99 100644 --- a/codex-rs/core/tests/suite/json_result.rs +++ b/codex-rs/core/tests/suite/json_result.rs @@ -85,6 +85,7 @@ async fn codex_returns_json_result(model: String) -> anyhow::Result<()> { model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/model_overrides.rs b/codex-rs/core/tests/suite/model_overrides.rs index a418e35a39..9501bc06da 100644 --- a/codex-rs/core/tests/suite/model_overrides.rs +++ b/codex-rs/core/tests/suite/model_overrides.rs @@ -40,6 +40,7 @@ async fn override_turn_context_does_not_persist_when_config_exists() { model: Some("o3".to_string()), effort: Some(Some(ReasoningEffort::High)), summary: None, + collaboration_mode: None, }) .await .expect("submit override"); @@ -82,6 +83,7 @@ async fn override_turn_context_does_not_create_config_file() { model: Some("o3".to_string()), effort: Some(Some(ReasoningEffort::Medium)), summary: None, + collaboration_mode: None, }) .await .expect("submit override"); diff --git a/codex-rs/core/tests/suite/models_cache_ttl.rs b/codex-rs/core/tests/suite/models_cache_ttl.rs index 706a3dea55..5bcb0ccc0b 100644 --- a/codex-rs/core/tests/suite/models_cache_ttl.rs +++ b/codex-rs/core/tests/suite/models_cache_ttl.rs @@ -98,6 +98,7 @@ async fn renews_cache_ttl_on_matching_models_etag() -> Result<()> { model: test.session_configured.model.clone(), effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/models_etag_responses.rs b/codex-rs/core/tests/suite/models_etag_responses.rs index 4976b2881c..c264b5da97 100644 --- a/codex-rs/core/tests/suite/models_etag_responses.rs +++ b/codex-rs/core/tests/suite/models_etag_responses.rs @@ -109,6 +109,7 @@ async fn refresh_models_on_models_etag_mismatch_and_avoid_duplicate_models_fetch model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/permissions_messages.rs b/codex-rs/core/tests/suite/permissions_messages.rs index 6f676c1da7..3e54fa29a0 100644 --- a/codex-rs/core/tests/suite/permissions_messages.rs +++ b/codex-rs/core/tests/suite/permissions_messages.rs @@ -109,6 +109,7 @@ async fn permissions_message_added_on_override_change() -> Result<()> { model: None, effort: None, summary: None, + collaboration_mode: None, }) .await?; @@ -224,6 +225,7 @@ async fn resume_replays_permissions_messages() -> Result<()> { model: None, effort: None, summary: None, + collaboration_mode: None, }) .await?; @@ -300,6 +302,7 @@ async fn resume_and_fork_append_permissions_messages() -> Result<()> { model: None, effort: None, summary: None, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/prompt_caching.rs b/codex-rs/core/tests/suite/prompt_caching.rs index 6ea5619bfe..093932512f 100644 --- a/codex-rs/core/tests/suite/prompt_caching.rs +++ b/codex-rs/core/tests/suite/prompt_caching.rs @@ -11,6 +11,8 @@ use codex_core::protocol::SandboxPolicy; use codex_core::protocol_config_types::ReasoningSummary; use codex_core::shell::Shell; use codex_core::shell::default_user_shell; +use codex_protocol::config_types::CollaborationMode; +use codex_protocol::config_types::Settings; use codex_protocol::config_types::WebSearchMode; use codex_protocol::openai_models::ReasoningEffort; use codex_protocol::user_input::UserInput; @@ -22,6 +24,7 @@ use core_test_support::skip_if_no_network; use core_test_support::test_codex::TestCodex; use core_test_support::test_codex::test_codex; use core_test_support::wait_for_event; +use pretty_assertions::assert_eq; use tempfile::TempDir; fn text_user_input(text: String) -> serde_json::Value { @@ -344,6 +347,7 @@ async fn overrides_turn_context_but_keeps_cached_prefix_and_key_constant() -> an model: Some("o3".to_string()), effort: Some(Some(ReasoningEffort::High)), summary: Some(ReasoningSummary::Detailed), + collaboration_mode: None, }) .await?; @@ -399,14 +403,21 @@ async fn override_before_first_turn_emits_environment_context() -> anyhow::Resul let TestCodex { codex, .. } = test_codex().build(&server).await?; + let collaboration_mode = CollaborationMode::Custom(Settings { + model: "gpt-5.1".to_string(), + reasoning_effort: Some(ReasoningEffort::High), + developer_instructions: None, + }); + codex .submit(Op::OverrideTurnContext { cwd: None, approval_policy: Some(AskForApproval::Never), sandbox_policy: None, - model: None, - effort: None, + model: Some("gpt-5.1-codex".to_string()), + effort: Some(Some(ReasoningEffort::Low)), summary: None, + collaboration_mode: Some(collaboration_mode), }) .await?; @@ -423,6 +434,13 @@ async fn override_before_first_turn_emits_environment_context() -> anyhow::Resul wait_for_event(&codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; let body = req.single_request().body_json(); + assert_eq!(body["model"].as_str(), Some("gpt-5.1")); + assert_eq!( + body.get("reasoning") + .and_then(|reasoning| reasoning.get("effort")) + .and_then(|value| value.as_str()), + Some("high") + ); let input = body["input"] .as_array() .expect("input array must be present"); @@ -554,6 +572,7 @@ async fn per_turn_overrides_keep_cached_prefix_and_key_constant() -> anyhow::Res model: "o3".to_string(), effort: Some(ReasoningEffort::High), summary: ReasoningSummary::Detailed, + collaboration_mode: None, final_output_json_schema: None, }) .await?; @@ -646,6 +665,7 @@ async fn send_user_turn_with_no_changes_does_not_send_environment_context() -> a model: default_model.clone(), effort: default_effort, summary: default_summary, + collaboration_mode: None, final_output_json_schema: None, }) .await?; @@ -663,6 +683,7 @@ async fn send_user_turn_with_no_changes_does_not_send_environment_context() -> a model: default_model.clone(), effort: default_effort, summary: default_summary, + collaboration_mode: None, final_output_json_schema: None, }) .await?; @@ -741,6 +762,7 @@ async fn send_user_turn_with_changes_sends_environment_context() -> anyhow::Resu model: default_model, effort: default_effort, summary: default_summary, + collaboration_mode: None, final_output_json_schema: None, }) .await?; @@ -758,6 +780,7 @@ async fn send_user_turn_with_changes_sends_environment_context() -> anyhow::Resu model: "o3".to_string(), effort: Some(ReasoningEffort::High), summary: ReasoningSummary::Detailed, + collaboration_mode: None, final_output_json_schema: None, }) .await?; diff --git a/codex-rs/core/tests/suite/remote_models.rs b/codex-rs/core/tests/suite/remote_models.rs index 75c22df554..e7123c4305 100644 --- a/codex-rs/core/tests/suite/remote_models.rs +++ b/codex-rs/core/tests/suite/remote_models.rs @@ -139,6 +139,7 @@ async fn remote_models_remote_model_uses_unified_exec() -> Result<()> { model: Some(REMOTE_MODEL_SLUG.to_string()), effort: None, summary: None, + collaboration_mode: None, }) .await?; @@ -174,6 +175,7 @@ async fn remote_models_remote_model_uses_unified_exec() -> Result<()> { model: REMOTE_MODEL_SLUG.to_string(), effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -363,6 +365,7 @@ async fn remote_models_apply_remote_base_instructions() -> Result<()> { model: Some(model.to_string()), effort: None, summary: None, + collaboration_mode: None, }) .await?; @@ -379,6 +382,7 @@ async fn remote_models_apply_remote_base_instructions() -> Result<()> { model: model.to_string(), effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/review.rs b/codex-rs/core/tests/suite/review.rs index d182af8255..0de18438ce 100644 --- a/codex-rs/core/tests/suite/review.rs +++ b/codex-rs/core/tests/suite/review.rs @@ -820,6 +820,7 @@ async fn review_uses_overridden_cwd_for_base_branch_merge_base() { model: None, effort: None, summary: None, + collaboration_mode: None, }) .await .unwrap(); diff --git a/codex-rs/core/tests/suite/rmcp_client.rs b/codex-rs/core/tests/suite/rmcp_client.rs index 5abc392644..cba1ed6225 100644 --- a/codex-rs/core/tests/suite/rmcp_client.rs +++ b/codex-rs/core/tests/suite/rmcp_client.rs @@ -118,6 +118,7 @@ async fn stdio_server_round_trip() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -256,6 +257,7 @@ async fn stdio_image_responses_round_trip() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -452,6 +454,7 @@ async fn stdio_image_completions_round_trip() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -596,6 +599,7 @@ async fn stdio_server_propagates_whitelisted_env_vars() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -751,6 +755,7 @@ async fn streamable_http_tool_call_round_trip() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -938,6 +943,7 @@ async fn streamable_http_with_oauth_round_trip() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/shell_snapshot.rs b/codex-rs/core/tests/suite/shell_snapshot.rs index 74ff56e84e..443a41abab 100644 --- a/codex-rs/core/tests/suite/shell_snapshot.rs +++ b/codex-rs/core/tests/suite/shell_snapshot.rs @@ -80,6 +80,7 @@ async fn run_snapshot_command(command: &str) -> Result { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -157,6 +158,7 @@ async fn run_shell_command_snapshot(command: &str) -> Result { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -295,6 +297,7 @@ async fn shell_command_snapshot_still_intercepts_apply_patch() -> Result<()> { model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/skills.rs b/codex-rs/core/tests/suite/skills.rs index eb6b4288d0..4c1c338bca 100644 --- a/codex-rs/core/tests/suite/skills.rs +++ b/codex-rs/core/tests/suite/skills.rs @@ -78,6 +78,7 @@ async fn user_turn_includes_skill_instructions() -> Result<()> { model: session_model, effort: None, summary: codex_protocol::config_types::ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/tool_harness.rs b/codex-rs/core/tests/suite/tool_harness.rs index 75d707af53..8f5646fc30 100644 --- a/codex-rs/core/tests/suite/tool_harness.rs +++ b/codex-rs/core/tests/suite/tool_harness.rs @@ -91,6 +91,7 @@ async fn shell_tool_executes_command_and_streams_output() -> anyhow::Result<()> model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -158,6 +159,7 @@ async fn update_plan_tool_emits_plan_update_event() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -235,6 +237,7 @@ async fn update_plan_tool_rejects_malformed_payload() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -324,6 +327,7 @@ async fn apply_patch_tool_executes_and_emits_patch_events() -> anyhow::Result<() model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -421,6 +425,7 @@ async fn apply_patch_reports_parse_diagnostics() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/tool_parallelism.rs b/codex-rs/core/tests/suite/tool_parallelism.rs index 4abf85d7dc..05c02d4228 100644 --- a/codex-rs/core/tests/suite/tool_parallelism.rs +++ b/codex-rs/core/tests/suite/tool_parallelism.rs @@ -47,6 +47,7 @@ async fn run_turn(test: &TestCodex, prompt: &str) -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -363,6 +364,7 @@ async fn shell_tools_start_before_response_completed_when_stream_delayed() -> an model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/truncation.rs b/codex-rs/core/tests/suite/truncation.rs index 4ae095517c..bd4a9a3061 100644 --- a/codex-rs/core/tests/suite/truncation.rs +++ b/codex-rs/core/tests/suite/truncation.rs @@ -547,6 +547,7 @@ async fn mcp_image_output_preserves_image_and_no_text_summary() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/unified_exec.rs b/codex-rs/core/tests/suite/unified_exec.rs index df72d249b3..c3816aefd3 100644 --- a/codex-rs/core/tests/suite/unified_exec.rs +++ b/codex-rs/core/tests/suite/unified_exec.rs @@ -209,6 +209,7 @@ async fn unified_exec_intercepts_apply_patch_exec_command() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -336,6 +337,7 @@ async fn unified_exec_emits_exec_command_begin_event() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -412,6 +414,7 @@ async fn unified_exec_resolves_relative_workdir() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -491,6 +494,7 @@ async fn unified_exec_respects_workdir_override() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -582,6 +586,7 @@ async fn unified_exec_emits_exec_command_end_event() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -655,6 +660,7 @@ async fn unified_exec_emits_output_delta_for_exec_command() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -729,6 +735,7 @@ async fn unified_exec_full_lifecycle_with_background_end_event() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -857,6 +864,7 @@ async fn unified_exec_emits_terminal_interaction_for_write_stdin() -> Result<()> model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -992,6 +1000,7 @@ async fn unified_exec_terminal_interaction_captures_delayed_output() -> Result<( model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1150,6 +1159,7 @@ async fn unified_exec_emits_one_begin_and_one_end_event() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1246,6 +1256,7 @@ async fn exec_command_reports_chunk_and_exit_metadata() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1362,6 +1373,7 @@ async fn unified_exec_defaults_to_pipe() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1450,6 +1462,7 @@ async fn unified_exec_can_enable_tty() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1529,6 +1542,7 @@ async fn unified_exec_respects_early_exit_notifications() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1658,6 +1672,7 @@ async fn write_stdin_returns_exit_metadata_and_clears_session() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1824,6 +1839,7 @@ async fn unified_exec_emits_end_event_when_session_dies_via_stdin() -> Result<() model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -1899,6 +1915,7 @@ async fn unified_exec_closes_long_running_session_at_turn_end() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -2019,6 +2036,7 @@ async fn unified_exec_reuses_session_via_stdin() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -2152,6 +2170,7 @@ PY model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; // This is a worst case scenario for the truncate logic. @@ -2264,6 +2283,7 @@ async fn unified_exec_timeout_and_followup_poll() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -2358,6 +2378,7 @@ PY model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -2438,6 +2459,7 @@ async fn unified_exec_runs_under_sandbox() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -2540,6 +2562,7 @@ async fn unified_exec_python_prompt_under_seatbelt() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -2633,6 +2656,7 @@ async fn unified_exec_runs_on_all_platforms() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -2766,6 +2790,7 @@ async fn unified_exec_prunes_exited_sessions_first() -> Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/core/tests/suite/view_image.rs b/codex-rs/core/tests/suite/view_image.rs index ebbbfc478e..c6869323ff 100644 --- a/codex-rs/core/tests/suite/view_image.rs +++ b/codex-rs/core/tests/suite/view_image.rs @@ -88,6 +88,7 @@ async fn user_turn_with_local_image_attaches_image() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -180,6 +181,7 @@ async fn view_image_tool_attaches_local_image() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -311,6 +313,7 @@ async fn view_image_tool_errors_when_path_is_directory() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -384,6 +387,7 @@ async fn view_image_tool_placeholder_for_non_image_files() -> anyhow::Result<()> model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -476,6 +480,7 @@ async fn view_image_tool_errors_when_file_missing() -> anyhow::Result<()> { model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; @@ -557,6 +562,7 @@ async fn replaces_invalid_local_image_after_bad_request() -> anyhow::Result<()> model: session_model, effort: None, summary: ReasoningSummary::Auto, + collaboration_mode: None, }) .await?; diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index ccab33048b..a76510ed95 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -452,6 +452,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any effort: default_effort, summary: default_summary, final_output_json_schema: output_schema, + collaboration_mode: None, }) .await?; info!("Sent prompt with event ID: {task_id}"); diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 6ddc0aeb5a..29eabe2bfa 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -13,6 +13,7 @@ use std::time::Duration; use crate::ThreadId; use crate::approvals::ElicitationRequestEvent; +use crate::config_types::CollaborationMode; use crate::config_types::ReasoningSummary as ReasoningSummaryConfig; use crate::custom_prompts::CustomPrompt; use crate::items::TurnItem; @@ -115,6 +116,11 @@ pub enum Op { summary: ReasoningSummaryConfig, // The JSON schema to use for the final assistant message final_output_json_schema: Option, + + /// EXPERIMENTAL - set a pre-set collaboration mode. + /// Takes precedence over model, effort, and developer instructions if set. + #[serde(skip_serializing_if = "Option::is_none")] + collaboration_mode: Option, }, /// Override parts of the persistent turn context for subsequent turns. @@ -150,6 +156,11 @@ pub enum Op { /// Updated reasoning summary preference (honored only for reasoning-capable models). #[serde(skip_serializing_if = "Option::is_none")] summary: Option, + + /// EXPERIMENTAL - set a pre-set collaboration mode. + /// Takes precedence over model, effort, and developer instructions if set. + #[serde(skip_serializing_if = "Option::is_none")] + collaboration_mode: Option, }, /// Approve a command execution diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 0178b31b2e..eace453899 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -1110,6 +1110,7 @@ impl App { model: None, effort: None, summary: None, + collaboration_mode: None, }, )); self.app_event_tx diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 3593e1f16b..bab27efd0a 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -2737,6 +2737,7 @@ impl ChatWidget { model: Some(switch_model.clone()), effort: Some(Some(default_effort)), summary: None, + collaboration_mode: None, })); tx.send(AppEvent::UpdateModel(switch_model.clone())); tx.send(AppEvent::UpdateReasoningEffort(Some(default_effort))); @@ -3011,6 +3012,7 @@ impl ChatWidget { model: Some(model_for_action.clone()), effort: Some(effort_for_action), summary: None, + collaboration_mode: None, })); tx.send(AppEvent::UpdateModel(model_for_action.clone())); tx.send(AppEvent::UpdateReasoningEffort(effort_for_action)); @@ -3182,6 +3184,7 @@ impl ChatWidget { model: Some(model.clone()), effort: Some(effort), summary: None, + collaboration_mode: None, })); self.app_event_tx.send(AppEvent::UpdateModel(model.clone())); self.app_event_tx @@ -3350,6 +3353,7 @@ impl ChatWidget { model: None, effort: None, summary: None, + collaboration_mode: None, })); tx.send(AppEvent::UpdateAskForApprovalPolicy(approval)); tx.send(AppEvent::UpdateSandboxPolicy(sandbox_clone)); diff --git a/codex-rs/tui2/src/app.rs b/codex-rs/tui2/src/app.rs index 5b66869056..1a406264ec 100644 --- a/codex-rs/tui2/src/app.rs +++ b/codex-rs/tui2/src/app.rs @@ -1820,6 +1820,7 @@ impl App { model: None, effort: None, summary: None, + collaboration_mode: None, }, )); self.app_event_tx diff --git a/codex-rs/tui2/src/chatwidget.rs b/codex-rs/tui2/src/chatwidget.rs index 5b3343d5b4..117d320a06 100644 --- a/codex-rs/tui2/src/chatwidget.rs +++ b/codex-rs/tui2/src/chatwidget.rs @@ -2492,6 +2492,7 @@ impl ChatWidget { model: Some(switch_model.clone()), effort: Some(Some(default_effort)), summary: None, + collaboration_mode: None, })); tx.send(AppEvent::UpdateModel(switch_model.clone())); tx.send(AppEvent::UpdateReasoningEffort(Some(default_effort))); @@ -2724,6 +2725,7 @@ impl ChatWidget { model: Some(model_for_action.clone()), effort: Some(effort_for_action), summary: None, + collaboration_mode: None, })); tx.send(AppEvent::UpdateModel(model_for_action.clone())); tx.send(AppEvent::UpdateReasoningEffort(effort_for_action)); @@ -2895,6 +2897,7 @@ impl ChatWidget { model: Some(model.clone()), effort: Some(effort), summary: None, + collaboration_mode: None, })); self.app_event_tx.send(AppEvent::UpdateModel(model.clone())); self.app_event_tx @@ -3040,6 +3043,7 @@ impl ChatWidget { model: None, effort: None, summary: None, + collaboration_mode: None, })); tx.send(AppEvent::UpdateAskForApprovalPolicy(approval)); tx.send(AppEvent::UpdateSandboxPolicy(sandbox_clone)); From 93a5e0fe1c1cb103bea37b44367023e3095e27ef Mon Sep 17 00:00:00 2001 From: Fouad Matin <169186268+fouad-openai@users.noreply.github.com> Date: Fri, 16 Jan 2026 22:22:08 -0800 Subject: [PATCH 23/32] fix(codex-api): treat invalid_prompt as non-retryable (#9400) **Goal**: Prevent response.failed events with `invalid_prompt` from being treated as retryable errors so the UI shows the actual error message instead of continually retrying. **Before**: Codex would continue to retry despite the prompt being marked as disallowed **After**: Codex will stop retrying once prompt is marked disallowed --- codex-rs/codex-api/src/error.rs | 2 ++ codex-rs/codex-api/src/sse/responses.rs | 30 +++++++++++++++++++++++++ codex-rs/core/src/api_bridge.rs | 1 + 3 files changed, 33 insertions(+) diff --git a/codex-rs/codex-api/src/error.rs b/codex-rs/codex-api/src/error.rs index 60118e8723..e7fbd14543 100644 --- a/codex-rs/codex-api/src/error.rs +++ b/codex-rs/codex-api/src/error.rs @@ -25,6 +25,8 @@ pub enum ApiError { }, #[error("rate limit: {0}")] RateLimit(String), + #[error("invalid request: {message}")] + InvalidRequest { message: String }, } impl From for ApiError { diff --git a/codex-rs/codex-api/src/sse/responses.rs b/codex-rs/codex-api/src/sse/responses.rs index 5299310e06..a70111d988 100644 --- a/codex-rs/codex-api/src/sse/responses.rs +++ b/codex-rs/codex-api/src/sse/responses.rs @@ -217,6 +217,11 @@ pub fn process_responses_event( response_error = ApiError::QuotaExceeded; } else if is_usage_not_included(&error) { response_error = ApiError::UsageNotIncluded; + } else if is_invalid_prompt_error(&error) { + let message = error + .message + .unwrap_or_else(|| "Invalid request.".to_string()); + response_error = ApiError::InvalidRequest { message }; } else { let delay = try_parse_retry_after(&error); let message = error.message.unwrap_or_default(); @@ -396,6 +401,10 @@ fn is_usage_not_included(error: &Error) -> bool { error.code.as_deref() == Some("usage_not_included") } +fn is_invalid_prompt_error(error: &Error) -> bool { + error.code.as_deref() == Some("invalid_prompt") +} + fn rate_limit_regex() -> &'static regex_lite::Regex { static RE: std::sync::OnceLock = std::sync::OnceLock::new(); #[expect(clippy::unwrap_used)] @@ -711,6 +720,27 @@ mod tests { assert_matches!(events[0], Err(ApiError::QuotaExceeded)); } + #[tokio::test] + async fn invalid_prompt_without_type_is_invalid_request() { + let raw_error = r#"{"type":"response.failed","sequence_number":3,"response":{"id":"resp_invalid_prompt_no_type","object":"response","created_at":1759771628,"status":"failed","background":false,"error":{"code":"invalid_prompt","message":"Invalid prompt: we've limited access to this content for safety reasons."},"incomplete_details":null}}"#; + + let sse1 = format!("event: response.failed\ndata: {raw_error}\n\n"); + + let events = collect_events(&[sse1.as_bytes()]).await; + + assert_eq!(events.len(), 1); + + match &events[0] { + Err(ApiError::InvalidRequest { message }) => { + assert_eq!( + message, + "Invalid prompt: we've limited access to this content for safety reasons." + ); + } + other => panic!("unexpected event: {other:?}"), + } + } + #[tokio::test] async fn table_driven_event_kinds() { struct TestCase { diff --git a/codex-rs/core/src/api_bridge.rs b/codex-rs/core/src/api_bridge.rs index 19bd8d5ecb..79ca839816 100644 --- a/codex-rs/core/src/api_bridge.rs +++ b/codex-rs/core/src/api_bridge.rs @@ -28,6 +28,7 @@ pub(crate) fn map_api_error(err: ApiError) -> CodexErr { url: None, request_id: None, }), + ApiError::InvalidRequest { message } => CodexErr::InvalidRequest(message), ApiError::Transport(transport) => match transport { TransportError::Http { status, From 764f3c7d03b25a477659afd956a288e322396dfe Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Fri, 16 Jan 2026 22:29:41 -0800 Subject: [PATCH 24/32] fix(tui) Defer backtrack trim until rollback confirms (#9401) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Document the backtrack/rollback state machine and invariants between the transcript overlay, in-flight “live tail”, and core thread state (tui + tui2). Also adjust behavior for correctness: - Track a single pending rollback and block additional rollbacks until core responds. - Defer trimming transcript cells until ThreadRolledBack for the active session. - Clear the guard on ThreadRollbackFailed so the user can retry. - After a confirmed trim, schedule a one-shot scrollback refresh on the next draw. - Clear stale pending rollback state when switching sessions. --------- Co-authored-by: Josh McKinney --- codex-rs/tui/src/app.rs | 18 +++++ codex-rs/tui/src/app_backtrack.rs | 110 +++++++++++++++++++++++++--- codex-rs/tui2/src/app.rs | 18 +++++ codex-rs/tui2/src/app_backtrack.rs | 114 ++++++++++++++++++++++++++--- 4 files changed, 237 insertions(+), 23 deletions(-) diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index eace453899..b256ccd200 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -340,6 +340,11 @@ pub(crate) struct App { // Esc-backtracking state grouped pub(crate) backtrack: crate::app_backtrack::BacktrackState, + /// When set, the next draw re-renders the transcript into terminal scrollback once. + /// + /// This is used after a confirmed thread rollback to ensure scrollback reflects the trimmed + /// transcript cells. + pub(crate) backtrack_render_pending: bool, pub(crate) feedback: codex_feedback::CodexFeedback, /// Set when the user confirms an update; propagated on exit. pub(crate) pending_update_action: Option, @@ -362,6 +367,8 @@ pub(crate) struct App { impl App { async fn shutdown_current_thread(&mut self) { if let Some(thread_id) = self.chat_widget.thread_id() { + // Clear any in-flight rollback guard when switching threads. + self.backtrack.pending_rollback = None; self.suppress_shutdown_complete = true; self.chat_widget.submit_op(Op::Shutdown); self.server.remove_thread(&thread_id).await; @@ -502,6 +509,7 @@ impl App { has_emitted_history_lines: false, commit_anim_running: Arc::new(AtomicBool::new(false)), backtrack: BacktrackState::default(), + backtrack_render_pending: false, feedback: feedback.clone(), pending_update_action: None, suppress_shutdown_complete: false, @@ -622,6 +630,10 @@ impl App { self.chat_widget.handle_paste(pasted); } TuiEvent::Draw => { + if self.backtrack_render_pending { + self.backtrack_render_pending = false; + self.render_transcript_once(tui); + } self.chat_widget.maybe_post_pending_notification(tui); if self .chat_widget @@ -870,6 +882,9 @@ impl App { return Ok(AppRunControl::Continue); } self.handle_codex_event_now(event); + if self.backtrack_render_pending { + tui.frame_requester().schedule_frame(); + } } AppEvent::ExternalApprovalRequest { thread_id, event } => { self.handle_external_approval_request(thread_id, event); @@ -1416,6 +1431,7 @@ impl App { let errors = errors_for_cwd(&cwd, response); emit_skill_load_warnings(&self.app_event_tx, &errors); } + self.handle_backtrack_event(&event.msg); self.chat_widget.handle_codex_event(event); } @@ -1740,6 +1756,7 @@ mod tests { enhanced_keys_supported: false, commit_anim_running: Arc::new(AtomicBool::new(false)), backtrack: BacktrackState::default(), + backtrack_render_pending: false, feedback: codex_feedback::CodexFeedback::new(), pending_update_action: None, suppress_shutdown_complete: false, @@ -1782,6 +1799,7 @@ mod tests { enhanced_keys_supported: false, commit_anim_running: Arc::new(AtomicBool::new(false)), backtrack: BacktrackState::default(), + backtrack_render_pending: false, feedback: codex_feedback::CodexFeedback::new(), pending_update_action: None, suppress_shutdown_complete: false, diff --git a/codex-rs/tui/src/app_backtrack.rs b/codex-rs/tui/src/app_backtrack.rs index 43dacb573a..73d238f9a4 100644 --- a/codex-rs/tui/src/app_backtrack.rs +++ b/codex-rs/tui/src/app_backtrack.rs @@ -3,6 +3,18 @@ //! This file owns backtrack mode (Esc/Enter navigation in the transcript overlay) and also //! mediates a key rendering boundary for the transcript overlay. //! +//! Overall goal: keep the main chat view and the transcript overlay in sync while allowing +//! users to "rewind" to an earlier user message. We stage a rollback request, wait for core to +//! confirm it, then trim the local transcript to the matching history boundary. This avoids UI +//! state diverging from the agent if a rollback fails or targets a different thread. +//! +//! Backtrack operates as a small state machine: +//! - The first `Esc` in the main view "primes" the feature and captures a base thread id. +//! - A subsequent `Esc` opens the transcript overlay (`Ctrl+T`) and highlights a user message. +//! - `Enter` requests a rollback from core and records a `pending_rollback` guard. +//! - Only after receiving `EventMsg::ThreadRolledBack` do we trim local transcript state and +//! schedule a one-time scrollback refresh. +//! //! The transcript overlay (`Ctrl+T`) renders committed transcript cells plus a render-only live //! tail derived from the current in-flight `ChatWidget.active_cell`. //! @@ -20,6 +32,9 @@ use crate::history_cell::UserHistoryCell; use crate::pager_overlay::Overlay; use crate::tui; use crate::tui::TuiEvent; +use codex_core::protocol::CodexErrorInfo; +use codex_core::protocol::ErrorEvent; +use codex_core::protocol::EventMsg; use codex_core::protocol::Op; use codex_protocol::ThreadId; use color_eyre::eyre::Result; @@ -33,24 +48,53 @@ pub(crate) struct BacktrackState { /// True when Esc has primed backtrack mode in the main view. pub(crate) primed: bool, /// Session id of the base thread to rollback. + /// + /// If the current thread changes, backtrack selections become invalid and must be ignored. pub(crate) base_id: Option, - /// Index in the transcript of the last user message. + /// Index of the currently highlighted user message. + /// + /// This is an index into the filtered "user messages since the last session start" view, + /// not an index into `transcript_cells`. `usize::MAX` indicates "no selection". pub(crate) nth_user_message: usize, /// True when the transcript overlay is showing a backtrack preview. pub(crate) overlay_preview_active: bool, + /// Pending rollback request awaiting confirmation from core. + /// + /// This acts as a guardrail: once we request a rollback, we block additional backtrack + /// submissions until core responds with either a success or failure event. + pub(crate) pending_rollback: Option, } +/// A user-visible backtrack choice that can be confirmed into a rollback request. #[derive(Debug, Clone)] pub(crate) struct BacktrackSelection { + /// The selected user message, counted from the most recent session start. + /// + /// This value is used both to compute the rollback depth and to trim the local transcript + /// after core confirms the rollback. pub(crate) nth_user_message: usize, + /// Composer prefill derived from the selected user message. + /// + /// This is applied immediately on selection confirmation; if the rollback fails, the prefill + /// remains as a convenience so the user can retry or edit. pub(crate) prefill: String, } +/// An in-flight rollback requested from core. +/// +/// We keep enough information to apply the corresponding local trim only if the response targets +/// the same active thread we issued the request for. +#[derive(Debug, Clone)] +pub(crate) struct PendingBacktrackRollback { + pub(crate) selection: BacktrackSelection, + pub(crate) thread_id: Option, +} + impl App { - /// Route overlay events when transcript overlay is active. - /// - If backtrack preview is active: Esc steps selection; Enter confirms. - /// - Otherwise: Esc begins preview; all other events forward to overlay. - /// interactions (Esc to step target, Enter to confirm) and overlay lifecycle. + /// Route overlay events while the transcript overlay is active. + /// + /// If backtrack preview is active, Esc steps the selection and Enter confirms it. + /// Otherwise, Esc begins preview mode and all other events are forwarded to the overlay. pub(crate) async fn handle_backtrack_overlay_event( &mut self, tui: &mut tui::Tui, @@ -112,22 +156,38 @@ impl App { } /// Stage a backtrack and request thread history from the agent. + /// + /// We send the rollback request immediately, but we only mutate the transcript after core + /// confirms success so the UI cannot get ahead of the actual thread state. + /// + /// The composer prefill is applied immediately as a UX convenience; it does not imply that + /// core has accepted the rollback. pub(crate) fn apply_backtrack_rollback(&mut self, selection: BacktrackSelection) { let user_total = user_count(&self.transcript_cells); if user_total == 0 { return; } + if self.backtrack.pending_rollback.is_some() { + self.chat_widget + .add_error_message("Backtrack rollback already in progress.".to_string()); + return; + } + let num_turns = user_total.saturating_sub(selection.nth_user_message); let num_turns = u32::try_from(num_turns).unwrap_or(u32::MAX); if num_turns == 0 { return; } + let prefill = selection.prefill.clone(); + self.backtrack.pending_rollback = Some(PendingBacktrackRollback { + selection, + thread_id: self.chat_widget.thread_id(), + }); self.chat_widget.submit_op(Op::ThreadRollback { num_turns }); - self.trim_transcript_for_backtrack(selection.nth_user_message); - if !selection.prefill.is_empty() { - self.chat_widget.set_composer_text(selection.prefill); + if !prefill.is_empty() { + self.chat_widget.set_composer_text(prefill); } } @@ -290,7 +350,6 @@ impl App { self.close_transcript_overlay(tui); if let Some(selection) = selection { self.apply_backtrack_rollback(selection); - self.render_transcript_once(tui); tui.frame_requester().schedule_frame(); } } @@ -328,10 +387,39 @@ impl App { selection: BacktrackSelection, ) { self.apply_backtrack_rollback(selection); - self.render_transcript_once(tui); tui.frame_requester().schedule_frame(); } + pub(crate) fn handle_backtrack_event(&mut self, event: &EventMsg) { + match event { + EventMsg::ThreadRolledBack(_) => self.finish_pending_backtrack(), + EventMsg::Error(ErrorEvent { + codex_error_info: Some(CodexErrorInfo::ThreadRollbackFailed), + .. + }) => { + // Core rejected the rollback; clear the guard so the user can retry. + self.backtrack.pending_rollback = None; + } + _ => {} + } + } + + /// Finish a pending rollback by applying the local trim and scheduling a scrollback refresh. + /// + /// We ignore events that do not correspond to the currently active thread to avoid applying + /// stale updates after a session switch. + fn finish_pending_backtrack(&mut self) { + let Some(pending) = self.backtrack.pending_rollback.take() else { + return; + }; + if pending.thread_id != self.chat_widget.thread_id() { + // Ignore rollbacks targeting a prior thread. + return; + } + self.trim_transcript_for_backtrack(pending.selection.nth_user_message); + self.backtrack_render_pending = true; + } + fn backtrack_selection(&self, nth_user_message: usize) -> Option { let base_id = self.backtrack.base_id?; if self.chat_widget.thread_id() != Some(base_id) { @@ -350,7 +438,7 @@ impl App { }) } - /// Trim transcript_cells to preserve only content up to the selected user message. + /// Trim `transcript_cells` to preserve only content before the selected user message. fn trim_transcript_for_backtrack(&mut self, nth_user_message: usize) { trim_transcript_cells_to_nth_user(&mut self.transcript_cells, nth_user_message); } diff --git a/codex-rs/tui2/src/app.rs b/codex-rs/tui2/src/app.rs index 1a406264ec..6cc764c4a2 100644 --- a/codex-rs/tui2/src/app.rs +++ b/codex-rs/tui2/src/app.rs @@ -406,6 +406,11 @@ pub(crate) struct App { // Esc-backtracking state grouped pub(crate) backtrack: crate::app_backtrack::BacktrackState, + /// When set, the next draw re-renders the transcript into terminal scrollback once. + /// + /// This is used after a confirmed conversation rollback to ensure scrollback reflects the + /// trimmed transcript cells. + pub(crate) backtrack_render_pending: bool, pub(crate) feedback: codex_feedback::CodexFeedback, /// Set when the user confirms an update; propagated on exit. pub(crate) pending_update_action: Option, @@ -420,6 +425,8 @@ pub(crate) struct App { impl App { async fn shutdown_current_conversation(&mut self) { if let Some(conversation_id) = self.chat_widget.conversation_id() { + // Clear any in-flight rollback guard when switching conversations. + self.backtrack.pending_rollback = None; self.suppress_shutdown_complete = true; self.chat_widget.submit_op(Op::Shutdown); self.server.remove_thread(&conversation_id).await; @@ -587,6 +594,7 @@ impl App { scroll_config, scroll_state: MouseScrollState::default(), backtrack: BacktrackState::default(), + backtrack_render_pending: false, feedback: feedback.clone(), pending_update_action: None, suppress_shutdown_complete: false, @@ -711,6 +719,10 @@ impl App { self.chat_widget.handle_paste(pasted); } TuiEvent::Draw => { + if self.backtrack_render_pending { + self.backtrack_render_pending = false; + self.render_transcript_once(tui); + } self.chat_widget.maybe_post_pending_notification(tui); if self .chat_widget @@ -1634,7 +1646,11 @@ impl App { let errors = errors_for_cwd(&cwd, response); emit_skill_load_warnings(&self.app_event_tx, &errors); } + self.handle_backtrack_event(&event.msg); self.chat_widget.handle_codex_event(event); + if self.backtrack_render_pending { + tui.frame_requester().schedule_frame(); + } } AppEvent::Exit(mode) => match mode { ExitMode::ShutdownFirst => self.chat_widget.submit_op(Op::Shutdown), @@ -2363,6 +2379,7 @@ mod tests { scroll_config: ScrollConfig::default(), scroll_state: MouseScrollState::default(), backtrack: BacktrackState::default(), + backtrack_render_pending: false, feedback: codex_feedback::CodexFeedback::new(), pending_update_action: None, suppress_shutdown_complete: false, @@ -2416,6 +2433,7 @@ mod tests { scroll_config: ScrollConfig::default(), scroll_state: MouseScrollState::default(), backtrack: BacktrackState::default(), + backtrack_render_pending: false, feedback: codex_feedback::CodexFeedback::new(), pending_update_action: None, suppress_shutdown_complete: false, diff --git a/codex-rs/tui2/src/app_backtrack.rs b/codex-rs/tui2/src/app_backtrack.rs index 1f5ec3e45e..195dc79b3e 100644 --- a/codex-rs/tui2/src/app_backtrack.rs +++ b/codex-rs/tui2/src/app_backtrack.rs @@ -3,6 +3,18 @@ //! This file owns backtrack mode (Esc/Enter navigation in the transcript overlay) and also //! mediates a key rendering boundary for the transcript overlay. //! +//! Overall goal: keep the main chat view and the transcript overlay in sync while allowing +//! users to "rewind" to an earlier user message. We stage a rollback request, wait for core to +//! confirm it, then trim the local transcript to the matching history boundary. This avoids UI +//! state diverging from the agent if a rollback fails or targets a different thread. +//! +//! Backtrack operates as a small state machine: +//! - The first `Esc` in the main view "primes" the feature and captures a base conversation id. +//! - A subsequent `Esc` opens the transcript overlay (`Ctrl+T`) and highlights a user message. +//! - `Enter` requests a rollback from core and records a `pending_rollback` guard. +//! - Only after receiving `EventMsg::ThreadRolledBack` do we trim local transcript state and +//! schedule a one-time scrollback refresh. +//! //! The transcript overlay (`Ctrl+T`) renders committed transcript cells plus a render-only live //! tail derived from the current in-flight `ChatWidget.active_cell`. //! @@ -20,6 +32,9 @@ use crate::history_cell::UserHistoryCell; use crate::pager_overlay::Overlay; use crate::tui; use crate::tui::TuiEvent; +use codex_core::protocol::CodexErrorInfo; +use codex_core::protocol::ErrorEvent; +use codex_core::protocol::EventMsg; use codex_core::protocol::Op; use codex_protocol::ThreadId; use color_eyre::eyre::Result; @@ -32,25 +47,55 @@ use crossterm::event::KeyEventKind; pub(crate) struct BacktrackState { /// True when Esc has primed backtrack mode in the main view. pub(crate) primed: bool, - /// Session id of the base thread to rollback. + /// Session id of the base conversation to rollback. + /// + /// If the current conversation changes, backtrack selections become invalid and must be + /// ignored. pub(crate) base_id: Option, - /// Index in the transcript of the last user message. + /// Index of the currently highlighted user message. + /// + /// This is an index into the filtered "user messages since the last session start" view, + /// not an index into `transcript_cells`. `usize::MAX` indicates "no selection". pub(crate) nth_user_message: usize, /// True when the transcript overlay is showing a backtrack preview. pub(crate) overlay_preview_active: bool, + /// Pending rollback request awaiting confirmation from core. + /// + /// This acts as a guardrail: once we request a rollback, we block additional backtrack + /// submissions until core responds with either a success or failure event. + pub(crate) pending_rollback: Option, } +/// A user-visible backtrack choice that can be confirmed into a rollback request. #[derive(Debug, Clone)] pub(crate) struct BacktrackSelection { + /// The selected user message, counted from the most recent session start. + /// + /// This value is used both to compute the rollback depth and to trim the local transcript + /// after core confirms the rollback. pub(crate) nth_user_message: usize, + /// Composer prefill derived from the selected user message. + /// + /// This is applied immediately on selection confirmation; if the rollback fails, the prefill + /// remains as a convenience so the user can retry or edit. pub(crate) prefill: String, } +/// An in-flight rollback requested from core. +/// +/// We keep enough information to apply the corresponding local trim only if the response targets +/// the same active conversation we issued the request for. +#[derive(Debug, Clone)] +pub(crate) struct PendingBacktrackRollback { + pub(crate) selection: BacktrackSelection, + pub(crate) thread_id: Option, +} + impl App { - /// Route overlay events when transcript overlay is active. - /// - If backtrack preview is active: Esc steps selection; Enter confirms. - /// - Otherwise: Esc begins preview; all other events forward to overlay. - /// interactions (Esc to step target, Enter to confirm) and overlay lifecycle. + /// Route overlay events while the transcript overlay is active. + /// + /// If backtrack preview is active, Esc steps the selection and Enter confirms it. + /// Otherwise, Esc begins preview mode and all other events are forwarded to the overlay. pub(crate) async fn handle_backtrack_overlay_event( &mut self, tui: &mut tui::Tui, @@ -111,22 +156,39 @@ impl App { } } + /// Stage a backtrack and request thread history from the agent. + /// + /// We send the rollback request immediately, but we only mutate the transcript after core + /// confirms success so the UI cannot get ahead of the actual thread state. + /// + /// The composer prefill is applied immediately as a UX convenience; it does not imply that + /// core has accepted the rollback. pub(crate) fn apply_backtrack_rollback(&mut self, selection: BacktrackSelection) { let user_total = user_count(&self.transcript_cells); if user_total == 0 { return; } + if self.backtrack.pending_rollback.is_some() { + self.chat_widget + .add_error_message("Backtrack rollback already in progress.".to_string()); + return; + } + let num_turns = user_total.saturating_sub(selection.nth_user_message); let num_turns = u32::try_from(num_turns).unwrap_or(u32::MAX); if num_turns == 0 { return; } + let prefill = selection.prefill.clone(); + self.backtrack.pending_rollback = Some(PendingBacktrackRollback { + selection, + thread_id: self.chat_widget.conversation_id(), + }); self.chat_widget.submit_op(Op::ThreadRollback { num_turns }); - self.trim_transcript_for_backtrack(selection.nth_user_message); - if !selection.prefill.is_empty() { - self.chat_widget.set_composer_text(selection.prefill); + if !prefill.is_empty() { + self.chat_widget.set_composer_text(prefill); } } @@ -311,7 +373,6 @@ impl App { self.close_transcript_overlay(tui); if let Some(selection) = selection { self.apply_backtrack_rollback(selection); - self.render_transcript_once(tui); tui.frame_requester().schedule_frame(); } } @@ -349,9 +410,38 @@ impl App { selection: BacktrackSelection, ) { self.apply_backtrack_rollback(selection); - self.render_transcript_once(tui); tui.frame_requester().schedule_frame(); } + + pub(crate) fn handle_backtrack_event(&mut self, event: &EventMsg) { + match event { + EventMsg::ThreadRolledBack(_) => self.finish_pending_backtrack(), + EventMsg::Error(ErrorEvent { + codex_error_info: Some(CodexErrorInfo::ThreadRollbackFailed), + .. + }) => { + // Core rejected the rollback; clear the guard so the user can retry. + self.backtrack.pending_rollback = None; + } + _ => {} + } + } + + /// Finish a pending rollback by applying the local trim and scheduling a scrollback refresh. + /// + /// We ignore events that do not correspond to the currently active conversation to avoid + /// applying stale updates after a session switch. + fn finish_pending_backtrack(&mut self) { + let Some(pending) = self.backtrack.pending_rollback.take() else { + return; + }; + if pending.thread_id != self.chat_widget.conversation_id() { + // Ignore rollbacks targeting a prior thread. + return; + } + self.trim_transcript_for_backtrack(pending.selection.nth_user_message); + self.backtrack_render_pending = true; + } fn backtrack_selection(&self, nth_user_message: usize) -> Option { let base_id = self.backtrack.base_id?; if self.chat_widget.conversation_id() != Some(base_id) { @@ -370,7 +460,7 @@ impl App { }) } - /// Trim transcript_cells to preserve only content up to the selected user message. + /// Trim `transcript_cells` to preserve only content before the selected user message. fn trim_transcript_for_backtrack(&mut self, nth_user_message: usize) { trim_transcript_cells_to_nth_user(&mut self.transcript_cells, nth_user_message); } From b877a2041e08d38768aa9764b6aa216e60581f0c Mon Sep 17 00:00:00 2001 From: Alex Hornby <14246801+ahornby@users.noreply.github.com> Date: Sat, 17 Jan 2026 17:05:53 +0000 Subject: [PATCH 25/32] fix unified_exec::tests::unified_exec_timeouts to use a more unique match value (#9414) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix unified_exec_timeouts to use a unique variable value rather than "codex" which was causing false positives when running tests locally (presumably from my bash prompts). Discovered while running tests to validate another change. Fixes https://github.com/openai/codex/issues/9413 Test Plan: Ran test locally on my fedora 43 x86_64 machine with: ``` cd codex/cargo-rs cargo nextest run --all-features --no-fail-fast unified_exec::tests::unified_exec_timeouts ``` Before, unified_exec_timeouts fails: ``` Finished `test` profile [unoptimized + debuginfo] target(s) in 0.38s ──────────── Nextest run ID fa2b4949-a66c-408c-8002-32c52c70ec4f with nextest profile: default Starting 1 test across 107 binaries (3211 tests skipped) FAIL [ 5.667s] codex-core unified_exec::tests::unified_exec_timeouts stdout ─── running 1 test test unified_exec::tests::unified_exec_timeouts ... FAILED failures: failures: unified_exec::tests::unified_exec_timeouts test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 774 filtered out; finished in 5.66s stderr ─── thread 'unified_exec::tests::unified_exec_timeouts' (459601) panicked at core/src/unified_exec/mod.rs:381:9: timeout too short should yield incomplete output note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ──────────── Summary [ 5.677s] 1 test run: 0 passed, 1 failed, 3211 skipped FAIL [ 5.667s] codex-core unified_exec::tests::unified_exec_timeouts error: test run failed ``` After, works: ``` Finished `test` profile [unoptimized + debuginfo] target(s) in 0.34s ──────────── Nextest run ID f49e9004-e30b-4049-b0ff-283b543a1cd7 with nextest profile: default Starting 1 test across 107 binaries (3211 tests skipped) SLOW [> 15.000s] codex-core unified_exec::tests::unified_exec_timeouts PASS [ 17.666s] codex-core unified_exec::tests::unified_exec_timeouts ──────────── Summary [ 17.676s] 1 test run: 1 passed (1 slow), 3211 skipped ``` --- codex-rs/core/src/unified_exec/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/codex-rs/core/src/unified_exec/mod.rs b/codex-rs/core/src/unified_exec/mod.rs index 42a08b571f..3f359c3866 100644 --- a/codex-rs/core/src/unified_exec/mod.rs +++ b/codex-rs/core/src/unified_exec/mod.rs @@ -354,6 +354,8 @@ mod tests { async fn unified_exec_timeouts() -> anyhow::Result<()> { skip_if_sandbox!(Ok(())); + const TEST_VAR_VALUE: &str = "unified_exec_var_123"; + let (session, turn) = test_session_and_turn().await; let open_shell = exec_command(&session, &turn, "bash -i", 2_500).await?; @@ -366,7 +368,7 @@ mod tests { write_stdin( &session, process_id, - "export CODEX_INTERACTIVE_SHELL_VAR=codex\n", + format!("export CODEX_INTERACTIVE_SHELL_VAR={TEST_VAR_VALUE}\n").as_str(), 2_500, ) .await?; @@ -379,7 +381,7 @@ mod tests { ) .await?; assert!( - !out_2.output.contains("codex"), + !out_2.output.contains(TEST_VAR_VALUE), "timeout too short should yield incomplete output" ); @@ -388,7 +390,7 @@ mod tests { let out_3 = write_stdin(&session, process_id, "", 100).await?; assert!( - out_3.output.contains("codex"), + out_3.output.contains(TEST_VAR_VALUE), "subsequent poll should retrieve output" ); From 8f0e0300d2ebdb8e743342d73b30a7ba615e9747 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Sat, 17 Jan 2026 12:32:50 -0800 Subject: [PATCH 26/32] Expose collaboration presets (#9421) Expose collaboration presets for clients --------- Co-authored-by: Josh McKinney --- .../src/protocol/common.rs | 22 +++++ .../app-server-protocol/src/protocol/v2.rs | 14 +++ codex-rs/app-server/README.md | 1 + .../app-server/src/codex_message_processor.rs | 23 +++++ .../app-server/tests/common/mcp_process.rs | 10 +++ .../tests/suite/v2/collaboration_mode_list.rs | 85 +++++++++++++++++++ codex-rs/app-server/tests/suite/v2/mod.rs | 1 + .../collaboration_mode_presets.rs | 31 +++++++ codex-rs/core/src/models_manager/manager.rs | 9 ++ codex-rs/core/src/models_manager/mod.rs | 1 + codex-rs/core/src/thread_manager.rs | 5 ++ codex-rs/docs/codex_mcp_interface.md | 7 ++ 12 files changed, 209 insertions(+) create mode 100644 codex-rs/app-server/tests/suite/v2/collaboration_mode_list.rs create mode 100644 codex-rs/core/src/models_manager/collaboration_mode_presets.rs diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index bce2e48c97..4011328062 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -154,6 +154,11 @@ client_request_definitions! { params: v2::ModelListParams, response: v2::ModelListResponse, }, + /// EXPERIMENTAL - list collaboration mode presets. + CollaborationModeList => "collaborationMode/list" { + params: v2::CollaborationModeListParams, + response: v2::CollaborationModeListResponse, + }, McpServerOauthLogin => "mcpServer/oauth/login" { params: v2::McpServerOauthLoginParams, @@ -878,4 +883,21 @@ mod tests { ); Ok(()) } + + #[test] + fn serialize_list_collaboration_modes() -> Result<()> { + let request = ClientRequest::CollaborationModeList { + request_id: RequestId::Integer(7), + params: v2::CollaborationModeListParams::default(), + }; + assert_eq!( + json!({ + "method": "collaborationMode/list", + "id": 7, + "params": {} + }), + serde_json::to_value(&request)?, + ); + Ok(()) + } } diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index c0caa7ea32..db51c278f0 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -917,6 +917,20 @@ pub struct ModelListResponse { pub next_cursor: Option, } +/// EXPERIMENTAL - list collaboration mode presets. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct CollaborationModeListParams {} + +/// EXPERIMENTAL - collaboration mode presets response. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct CollaborationModeListResponse { + pub data: Vec, +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 891632796b..4c376ce6a4 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -86,6 +86,7 @@ Example (from OpenAI's official VSCode extension): - `review/start` — kick off Codex’s automated reviewer for a thread; responds like `turn/start` and emits `item/started`/`item/completed` notifications with `enteredReviewMode` and `exitedReviewMode` items, plus a final assistant `agentMessage` containing the review. - `command/exec` — run a single command under the server sandbox without starting a thread/turn (handy for utilities and validation). - `model/list` — list available models (with reasoning effort options). +- `collaborationMode/list` — list available collaboration mode presets (experimental, no pagination). - `skills/list` — list skills for one or more `cwd` values (optional `forceReload`). - `skills/config/write` — write user-level skill config by path. - `mcpServer/oauth/login` — start an OAuth login for a configured MCP server; returns an `authorization_url` and later emits `mcpServer/oauthLogin/completed` once the browser flow finishes. diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 551015f94a..1aeb623987 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -23,6 +23,8 @@ use codex_app_server_protocol::CancelLoginAccountResponse; use codex_app_server_protocol::CancelLoginAccountStatus; use codex_app_server_protocol::CancelLoginChatGptResponse; use codex_app_server_protocol::ClientRequest; +use codex_app_server_protocol::CollaborationModeListParams; +use codex_app_server_protocol::CollaborationModeListResponse; use codex_app_server_protocol::CommandExecParams; use codex_app_server_protocol::ConversationGitInfo; use codex_app_server_protocol::ConversationSummary; @@ -436,6 +438,15 @@ impl CodexMessageProcessor { Self::list_models(outgoing, thread_manager, config, request_id, params).await; }); } + ClientRequest::CollaborationModeList { request_id, params } => { + let outgoing = self.outgoing.clone(); + let thread_manager = self.thread_manager.clone(); + + tokio::spawn(async move { + Self::list_collaboration_modes(outgoing, thread_manager, request_id, params) + .await; + }); + } ClientRequest::McpServerOauthLogin { request_id, params } => { self.mcp_server_oauth_login(request_id, params).await; } @@ -2371,6 +2382,18 @@ impl CodexMessageProcessor { outgoing.send_response(request_id, response).await; } + async fn list_collaboration_modes( + outgoing: Arc, + thread_manager: Arc, + request_id: RequestId, + params: CollaborationModeListParams, + ) { + let CollaborationModeListParams {} = params; + let items = thread_manager.list_collaboration_modes(); + let response = CollaborationModeListResponse { data: items }; + outgoing.send_response(request_id, response).await; + } + async fn mcp_server_refresh(&self, request_id: RequestId, _params: Option<()>) { let config = match self.load_latest_config().await { Ok(config) => config, diff --git a/codex-rs/app-server/tests/common/mcp_process.rs b/codex-rs/app-server/tests/common/mcp_process.rs index cde6ff3913..fe236a52b2 100644 --- a/codex-rs/app-server/tests/common/mcp_process.rs +++ b/codex-rs/app-server/tests/common/mcp_process.rs @@ -17,6 +17,7 @@ use codex_app_server_protocol::CancelLoginAccountParams; use codex_app_server_protocol::CancelLoginChatGptParams; use codex_app_server_protocol::ClientInfo; use codex_app_server_protocol::ClientNotification; +use codex_app_server_protocol::CollaborationModeListParams; use codex_app_server_protocol::ConfigBatchWriteParams; use codex_app_server_protocol::ConfigReadParams; use codex_app_server_protocol::ConfigValueWriteParams; @@ -396,6 +397,15 @@ impl McpProcess { self.send_request("model/list", params).await } + /// Send a `collaborationMode/list` JSON-RPC request. + pub async fn send_list_collaboration_modes_request( + &mut self, + params: CollaborationModeListParams, + ) -> anyhow::Result { + let params = Some(serde_json::to_value(params)?); + self.send_request("collaborationMode/list", params).await + } + /// Send a `resumeConversation` JSON-RPC request. pub async fn send_resume_conversation_request( &mut self, diff --git a/codex-rs/app-server/tests/suite/v2/collaboration_mode_list.rs b/codex-rs/app-server/tests/suite/v2/collaboration_mode_list.rs new file mode 100644 index 0000000000..b6ff3f888e --- /dev/null +++ b/codex-rs/app-server/tests/suite/v2/collaboration_mode_list.rs @@ -0,0 +1,85 @@ +//! Validates that the collaboration mode list endpoint returns the expected default presets. +//! +//! The test drives the app server through the MCP harness and asserts that the list response +//! includes the plan, collaborate, and execute modes with their default model and reasoning +//! effort settings, which keeps the API contract visible in one place. + +use std::time::Duration; + +use anyhow::Result; +use app_test_support::McpProcess; +use app_test_support::to_response; +use codex_app_server_protocol::CollaborationModeListParams; +use codex_app_server_protocol::CollaborationModeListResponse; +use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::RequestId; +use codex_protocol::config_types::CollaborationMode; +use codex_protocol::config_types::Settings; +use codex_protocol::openai_models::ReasoningEffort; +use pretty_assertions::assert_eq; +use tempfile::TempDir; +use tokio::time::timeout; + +const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10); + +/// Confirms the server returns the default collaboration mode presets in a stable order. +#[tokio::test] +async fn list_collaboration_modes_returns_presets() -> Result<()> { + let codex_home = TempDir::new()?; + let mut mcp = McpProcess::new(codex_home.path()).await?; + + timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??; + + let request_id = mcp + .send_list_collaboration_modes_request(CollaborationModeListParams {}) + .await?; + + let response: JSONRPCResponse = timeout( + DEFAULT_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(request_id)), + ) + .await??; + + let CollaborationModeListResponse { data: items } = + to_response::(response)?; + + let expected = vec![plan_preset(), collaborate_preset(), execute_preset()]; + assert_eq!(expected, items); + Ok(()) +} + +/// Builds the plan preset that the list response is expected to return. +/// +/// If the defaults change in the app server, this helper should be updated alongside the +/// contract, or the test will fail in ways that imply a regression in the API. +fn plan_preset() -> CollaborationMode { + CollaborationMode::Plan(Settings { + model: "gpt-5.2-codex".to_string(), + reasoning_effort: Some(ReasoningEffort::Medium), + developer_instructions: None, + }) +} + +/// Builds the collaborate preset that the list response is expected to return. +/// +/// The helper keeps the expected model and reasoning defaults co-located with the test +/// so that mismatches point directly at the API contract being exercised. +fn collaborate_preset() -> CollaborationMode { + CollaborationMode::Collaborate(Settings { + model: "gpt-5.2-codex".to_string(), + reasoning_effort: Some(ReasoningEffort::Medium), + developer_instructions: None, + }) +} + +/// Builds the execute preset that the list response is expected to return. +/// +/// The execute preset uses a different reasoning effort to capture the higher-effort +/// execution contract the server currently exposes. +fn execute_preset() -> CollaborationMode { + CollaborationMode::Execute(Settings { + model: "gpt-5.2-codex".to_string(), + reasoning_effort: Some(ReasoningEffort::XHigh), + developer_instructions: None, + }) +} diff --git a/codex-rs/app-server/tests/suite/v2/mod.rs b/codex-rs/app-server/tests/suite/v2/mod.rs index b2159ab9ce..82939908ee 100644 --- a/codex-rs/app-server/tests/suite/v2/mod.rs +++ b/codex-rs/app-server/tests/suite/v2/mod.rs @@ -1,5 +1,6 @@ mod account; mod analytics; +mod collaboration_mode_list; mod config_rpc; mod initialize; mod model_list; diff --git a/codex-rs/core/src/models_manager/collaboration_mode_presets.rs b/codex-rs/core/src/models_manager/collaboration_mode_presets.rs new file mode 100644 index 0000000000..3d6eb9ca3c --- /dev/null +++ b/codex-rs/core/src/models_manager/collaboration_mode_presets.rs @@ -0,0 +1,31 @@ +use codex_protocol::config_types::CollaborationMode; +use codex_protocol::config_types::Settings; +use codex_protocol::openai_models::ReasoningEffort; + +pub(super) fn builtin_collaboration_mode_presets() -> Vec { + vec![plan_preset(), collaborate_preset(), execute_preset()] +} + +fn plan_preset() -> CollaborationMode { + CollaborationMode::Plan(Settings { + model: "gpt-5.2-codex".to_string(), + reasoning_effort: Some(ReasoningEffort::Medium), + developer_instructions: None, + }) +} + +fn collaborate_preset() -> CollaborationMode { + CollaborationMode::Collaborate(Settings { + model: "gpt-5.2-codex".to_string(), + reasoning_effort: Some(ReasoningEffort::Medium), + developer_instructions: None, + }) +} + +fn execute_preset() -> CollaborationMode { + CollaborationMode::Execute(Settings { + model: "gpt-5.2-codex".to_string(), + reasoning_effort: Some(ReasoningEffort::XHigh), + developer_instructions: None, + }) +} diff --git a/codex-rs/core/src/models_manager/manager.rs b/codex-rs/core/src/models_manager/manager.rs index 206b4cab27..82fea6e71f 100644 --- a/codex-rs/core/src/models_manager/manager.rs +++ b/codex-rs/core/src/models_manager/manager.rs @@ -1,6 +1,7 @@ use codex_api::ModelsClient; use codex_api::ReqwestTransport; use codex_app_server_protocol::AuthMode; +use codex_protocol::config_types::CollaborationMode; use codex_protocol::openai_models::ModelInfo; use codex_protocol::openai_models::ModelPreset; use codex_protocol::openai_models::ModelsResponse; @@ -23,6 +24,7 @@ use crate::error::CodexErr; use crate::error::Result as CoreResult; use crate::features::Feature; use crate::model_provider_info::ModelProviderInfo; +use crate::models_manager::collaboration_mode_presets::builtin_collaboration_mode_presets; use crate::models_manager::model_info; use crate::models_manager::model_presets::builtin_model_presets; @@ -90,6 +92,13 @@ impl ModelsManager { self.build_available_models(remote_models) } + /// List collaboration mode presets. + /// + /// Returns a static set of presets seeded with the configured model. + pub fn list_collaboration_modes(&self) -> Vec { + builtin_collaboration_mode_presets() + } + /// Attempt to list models without blocking, using the current cached state. /// /// Returns an error if the internal lock cannot be acquired. diff --git a/codex-rs/core/src/models_manager/mod.rs b/codex-rs/core/src/models_manager/mod.rs index d0e3c8214a..1b3d95dc40 100644 --- a/codex-rs/core/src/models_manager/mod.rs +++ b/codex-rs/core/src/models_manager/mod.rs @@ -1,4 +1,5 @@ pub mod cache; +pub mod collaboration_mode_presets; pub mod manager; pub mod model_info; pub mod model_presets; diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index 6f2187f527..8d02828d59 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -19,6 +19,7 @@ use crate::rollout::RolloutRecorder; use crate::rollout::truncation; use crate::skills::SkillsManager; use codex_protocol::ThreadId; +use codex_protocol::config_types::CollaborationMode; use codex_protocol::openai_models::ModelPreset; use codex_protocol::protocol::InitialHistory; use codex_protocol::protocol::McpServerRefreshConfig; @@ -157,6 +158,10 @@ impl ThreadManager { .await } + pub fn list_collaboration_modes(&self) -> Vec { + self.state.models_manager.list_collaboration_modes() + } + pub async fn list_thread_ids(&self) -> Vec { self.state.threads.read().await.keys().copied().collect() } diff --git a/codex-rs/docs/codex_mcp_interface.md b/codex-rs/docs/codex_mcp_interface.md index 10f81e59b9..883c7321bc 100644 --- a/codex-rs/docs/codex_mcp_interface.md +++ b/codex-rs/docs/codex_mcp_interface.md @@ -20,6 +20,7 @@ At a glance: - Configuration and info - `getUserSavedConfig`, `setDefaultModel`, `getUserAgent`, `userInfo` - `model/list` → enumerate available models and reasoning options + - `collaborationMode/list` → enumerate collaboration mode presets (experimental) - Auth - `account/read`, `account/login/start`, `account/login/cancel`, `account/logout`, `account/rateLimits/read` - notifications: `account/login/completed`, `account/updated`, `account/rateLimits/updated` @@ -96,6 +97,12 @@ Each response yields: - `isDefault` – whether the model is recommended for most users - `nextCursor` – pass into the next request to continue paging (optional) +## Collaboration modes (experimental) + +Fetch the built-in collaboration mode presets with `collaborationMode/list`. This endpoint does not accept pagination and returns the full list in one response: + +- `data` – ordered list of collaboration mode presets + ## Event stream While a conversation runs, the server sends notifications: From bffe9b33e9729fa981c14c0aa4dfae32f2746aa2 Mon Sep 17 00:00:00 2001 From: Dylan Hurd Date: Sat, 17 Jan 2026 16:01:26 -0800 Subject: [PATCH 27/32] chore(core) Create instructions module (#9422) ## Summary We have a variety of things we refer to as instructions in the code base: our current canonical terms are: - base instructions (raw string) - developer instructions (has a type in protocol) - user instructions We also have `instructions` floating around in various places. We should standardize on the above, and start using types to prevent them from ending up in the wrong place. There will be additional PRs, but I'm going to keep these small so we can easily follow them! ## Testing - [x] Tests pass, this is purely a file move --- codex-rs/core/src/codex.rs | 2 +- codex-rs/core/src/context_manager/history.rs | 4 ++-- codex-rs/core/src/event_mapping.rs | 4 ++-- codex-rs/core/src/instructions/mod.rs | 6 ++++++ codex-rs/core/src/{ => instructions}/user_instructions.rs | 0 codex-rs/core/src/lib.rs | 2 +- codex-rs/core/src/skills/injection.rs | 2 +- 7 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 codex-rs/core/src/instructions/mod.rs rename codex-rs/core/src/{ => instructions}/user_instructions.rs (100%) diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index a9dc1004ab..48c689e644 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -98,6 +98,7 @@ use crate::error::Result as CodexResult; use crate::exec::StreamOutput; use crate::exec_policy::ExecPolicyUpdateError; use crate::feedback_tags; +use crate::instructions::UserInstructions; use crate::mcp::auth::compute_auth_statuses; use crate::mcp_connection_manager::McpConnectionManager; use crate::model_provider_info::CHAT_WIRE_API_DEPRECATION_SUMMARY; @@ -155,7 +156,6 @@ use crate::tools::spec::ToolsConfig; use crate::tools::spec::ToolsConfigParams; use crate::turn_diff_tracker::TurnDiffTracker; use crate::unified_exec::UnifiedExecProcessManager; -use crate::user_instructions::UserInstructions; use crate::user_notification::UserNotification; use crate::util::backoff; use codex_async_utils::OrCancelExt; diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs index 2279beef20..0c133bdc28 100644 --- a/codex-rs/core/src/context_manager/history.rs +++ b/codex-rs/core/src/context_manager/history.rs @@ -1,12 +1,12 @@ use crate::codex::TurnContext; use crate::context_manager::normalize; +use crate::instructions::SkillInstructions; +use crate::instructions::UserInstructions; use crate::truncate::TruncationPolicy; use crate::truncate::approx_token_count; use crate::truncate::approx_tokens_from_byte_count; use crate::truncate::truncate_function_output_items_with_policy; use crate::truncate::truncate_text; -use crate::user_instructions::SkillInstructions; -use crate::user_instructions::UserInstructions; use crate::user_shell_command::is_user_shell_command_text; use codex_protocol::models::ContentItem; use codex_protocol::models::FunctionCallOutputContentItem; diff --git a/codex-rs/core/src/event_mapping.rs b/codex-rs/core/src/event_mapping.rs index 6c19c726df..eaa477b085 100644 --- a/codex-rs/core/src/event_mapping.rs +++ b/codex-rs/core/src/event_mapping.rs @@ -17,8 +17,8 @@ use codex_protocol::user_input::UserInput; use tracing::warn; use uuid::Uuid; -use crate::user_instructions::SkillInstructions; -use crate::user_instructions::UserInstructions; +use crate::instructions::SkillInstructions; +use crate::instructions::UserInstructions; use crate::user_shell_command::is_user_shell_command_text; fn is_session_prefix(text: &str) -> bool { diff --git a/codex-rs/core/src/instructions/mod.rs b/codex-rs/core/src/instructions/mod.rs new file mode 100644 index 0000000000..9da925ac74 --- /dev/null +++ b/codex-rs/core/src/instructions/mod.rs @@ -0,0 +1,6 @@ +mod user_instructions; + +pub(crate) use user_instructions::SkillInstructions; +pub use user_instructions::USER_INSTRUCTIONS_OPEN_TAG_LEGACY; +pub use user_instructions::USER_INSTRUCTIONS_PREFIX; +pub(crate) use user_instructions::UserInstructions; diff --git a/codex-rs/core/src/user_instructions.rs b/codex-rs/core/src/instructions/user_instructions.rs similarity index 100% rename from codex-rs/core/src/user_instructions.rs rename to codex-rs/core/src/instructions/user_instructions.rs diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index e5a21b0517..a23ae55aa8 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -31,6 +31,7 @@ mod exec_policy; pub mod features; mod flags; pub mod git_info; +pub mod instructions; pub mod landlock; pub mod mcp; mod mcp_connection_manager; @@ -50,7 +51,6 @@ mod text_encoding; pub mod token_data; mod truncate; mod unified_exec; -mod user_instructions; pub mod windows_sandbox; pub use model_provider_info::CHAT_WIRE_API_DEPRECATION_SUMMARY; pub use model_provider_info::DEFAULT_LMSTUDIO_PORT; diff --git a/codex-rs/core/src/skills/injection.rs b/codex-rs/core/src/skills/injection.rs index dfd7e2af59..ffe879bcd9 100644 --- a/codex-rs/core/src/skills/injection.rs +++ b/codex-rs/core/src/skills/injection.rs @@ -1,9 +1,9 @@ use std::collections::HashSet; use std::path::PathBuf; +use crate::instructions::SkillInstructions; use crate::skills::SkillLoadOutcome; use crate::skills::SkillMetadata; -use crate::user_instructions::SkillInstructions; use codex_protocol::models::ResponseItem; use codex_protocol::user_input::UserInput; use tokio::fs; From 80d7a5d7fe523f9771b3d005aa8f550e75743f2f Mon Sep 17 00:00:00 2001 From: Dylan Hurd Date: Sat, 17 Jan 2026 16:02:28 -0800 Subject: [PATCH 28/32] chore(instructions) Remove unread SessionMeta.instructions field (#9423) ### Description - Remove the now-unused `instructions` field from the session metadata to simplify SessionMeta and stop propagating transient instruction text through the rollout recorder API. This was only saving user_instructions, and was never being read. - Stop passing user instructions into the rollout writer at session creation so the rollout header only contains canonical session metadata. ### Testing - Ran `just fmt` which completed successfully. - Ran `just fix -p codex-protocol`, `just fix -p codex-core`, `just fix -p codex-app-server`, `just fix -p codex-tui`, and `just fix -p codex-tui2` which completed (Clippy fixes applied) as part of verification. - Ran `cargo test -p codex-protocol` which passed (28 tests). - Ran `cargo test -p codex-core` which showed failures in a small set of tests (not caused by the protocol type change directly): `default_client::tests::test_create_client_sets_default_headers`, several `models_manager::manager::tests::refresh_available_models_*`, and `shell_snapshot::tests::linux_sh_snapshot_includes_sections` (these tests failed in this CI run). - Ran `cargo test -p codex-app-server` which reported several failing integration tests (including `suite::codex_message_processor_flow::test_codex_jsonrpc_conversation_flow`, `suite::output_schema::send_user_turn_*`, and `suite::user_agent::get_user_agent_returns_current_codex_user_agent`). - `cargo test -p codex-tui` and `cargo test -p codex-tui2` were attempted but aborted due to disk space exhaustion (`No space left on device`). ------ [Codex Task](https://chatgpt.com/codex/tasks/task_i_696bd8ce632483228d298cf07c7eb41c) --- codex-rs/app-server/src/codex_message_processor.rs | 1 - codex-rs/app-server/tests/common/rollout.rs | 2 -- codex-rs/core/src/codex.rs | 7 +------ codex-rs/core/src/rollout/recorder.rs | 5 ----- codex-rs/core/src/rollout/tests.rs | 13 ------------- codex-rs/core/tests/suite/review.rs | 1 - codex-rs/core/tests/suite/rollout_list_find.rs | 1 - codex-rs/protocol/src/protocol.rs | 2 -- codex-rs/tui/src/resume_picker.rs | 1 - codex-rs/tui2/src/resume_picker.rs | 1 - 10 files changed, 1 insertion(+), 33 deletions(-) diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 1aeb623987..a83607a621 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -4272,7 +4272,6 @@ mod tests { "cwd": "/", "originator": "codex", "cli_version": "0.0.0", - "instructions": null, "model_provider": "test-provider" }), json!({ diff --git a/codex-rs/app-server/tests/common/rollout.rs b/codex-rs/app-server/tests/common/rollout.rs index 72bc07753d..994cdf5ff8 100644 --- a/codex-rs/app-server/tests/common/rollout.rs +++ b/codex-rs/app-server/tests/common/rollout.rs @@ -57,7 +57,6 @@ pub fn create_fake_rollout( cwd: PathBuf::from("/"), originator: "codex".to_string(), cli_version: "0.0.0".to_string(), - instructions: None, source: SessionSource::Cli, model_provider: model_provider.map(str::to_string), }; @@ -135,7 +134,6 @@ pub fn create_fake_rollout_with_text_elements( cwd: PathBuf::from("/"), originator: "codex".to_string(), cli_version: "0.0.0".to_string(), - instructions: None, source: SessionSource::Cli, model_provider: model_provider.map(str::to_string), }; diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 48c689e644..e3369f8228 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -598,12 +598,7 @@ impl Session { let conversation_id = ThreadId::default(); ( conversation_id, - RolloutRecorderParams::new( - conversation_id, - forked_from_id, - session_configuration.user_instructions.clone(), - session_source, - ), + RolloutRecorderParams::new(conversation_id, forked_from_id, session_source), ) } InitialHistory::Resumed(resumed_history) => ( diff --git a/codex-rs/core/src/rollout/recorder.rs b/codex-rs/core/src/rollout/recorder.rs index 13eace9f3c..1c6503f318 100644 --- a/codex-rs/core/src/rollout/recorder.rs +++ b/codex-rs/core/src/rollout/recorder.rs @@ -58,7 +58,6 @@ pub enum RolloutRecorderParams { Create { conversation_id: ThreadId, forked_from_id: Option, - instructions: Option, source: SessionSource, }, Resume { @@ -81,13 +80,11 @@ impl RolloutRecorderParams { pub fn new( conversation_id: ThreadId, forked_from_id: Option, - instructions: Option, source: SessionSource, ) -> Self { Self::Create { conversation_id, forked_from_id, - instructions, source, } } @@ -162,7 +159,6 @@ impl RolloutRecorder { RolloutRecorderParams::Create { conversation_id, forked_from_id, - instructions, source, } => { let LogFileInfo { @@ -190,7 +186,6 @@ impl RolloutRecorder { cwd: config.cwd.clone(), originator: originator().value, cli_version: env!("CARGO_PKG_VERSION").to_string(), - instructions, source, model_provider: Some(config.model_provider_id.clone()), }), diff --git a/codex-rs/core/src/rollout/tests.rs b/codex-rs/core/src/rollout/tests.rs index 660e83e56f..c519c0c70c 100644 --- a/codex-rs/core/src/rollout/tests.rs +++ b/codex-rs/core/src/rollout/tests.rs @@ -86,7 +86,6 @@ fn write_session_file_with_provider( let mut payload = serde_json::json!({ "id": uuid, "timestamp": ts_str, - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -202,7 +201,6 @@ async fn test_list_conversations_latest_first() { let head_3 = vec![serde_json::json!({ "id": u3, "timestamp": "2025-01-03T12-00-00", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -212,7 +210,6 @@ async fn test_list_conversations_latest_first() { let head_2 = vec![serde_json::json!({ "id": u2, "timestamp": "2025-01-02T12-00-00", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -222,7 +219,6 @@ async fn test_list_conversations_latest_first() { let head_1 = vec![serde_json::json!({ "id": u1, "timestamp": "2025-01-01T12-00-00", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -343,7 +339,6 @@ async fn test_pagination_cursor() { let head_5 = vec![serde_json::json!({ "id": u5, "timestamp": "2025-03-05T09-00-00", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -353,7 +348,6 @@ async fn test_pagination_cursor() { let head_4 = vec![serde_json::json!({ "id": u4, "timestamp": "2025-03-04T09-00-00", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -411,7 +405,6 @@ async fn test_pagination_cursor() { let head_3 = vec![serde_json::json!({ "id": u3, "timestamp": "2025-03-03T09-00-00", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -421,7 +414,6 @@ async fn test_pagination_cursor() { let head_2 = vec![serde_json::json!({ "id": u2, "timestamp": "2025-03-02T09-00-00", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -473,7 +465,6 @@ async fn test_pagination_cursor() { let head_1 = vec![serde_json::json!({ "id": u1, "timestamp": "2025-03-01T09-00-00", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -531,7 +522,6 @@ async fn test_get_thread_contents() { let expected_head = vec![serde_json::json!({ "id": uuid, "timestamp": ts, - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -558,7 +548,6 @@ async fn test_get_thread_contents() { "payload": { "id": uuid, "timestamp": ts, - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", @@ -643,7 +632,6 @@ async fn test_updated_at_uses_file_mtime() -> Result<()> { id: conversation_id, forked_from_id: None, timestamp: ts.to_string(), - instructions: None, cwd: ".".into(), originator: "test_originator".into(), cli_version: "test_version".into(), @@ -751,7 +739,6 @@ async fn test_stable_ordering_same_second_pagination() { vec![serde_json::json!({ "id": u, "timestamp": ts, - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", diff --git a/codex-rs/core/tests/suite/review.rs b/codex-rs/core/tests/suite/review.rs index 0de18438ce..e5c980f681 100644 --- a/codex-rs/core/tests/suite/review.rs +++ b/codex-rs/core/tests/suite/review.rs @@ -515,7 +515,6 @@ async fn review_input_isolated_from_parent_history() { "payload": { "id": convo_id, "timestamp": "2024-01-01T00:00:00Z", - "instructions": null, "cwd": ".", "originator": "test_originator", "cli_version": "test_version", diff --git a/codex-rs/core/tests/suite/rollout_list_find.rs b/codex-rs/core/tests/suite/rollout_list_find.rs index 518f26c562..a4213729bc 100644 --- a/codex-rs/core/tests/suite/rollout_list_find.rs +++ b/codex-rs/core/tests/suite/rollout_list_find.rs @@ -25,7 +25,6 @@ fn write_minimal_rollout_with_id(codex_home: &Path, id: Uuid) -> PathBuf { "payload": { "id": id, "timestamp": "2024-01-01T00:00:00Z", - "instructions": null, "cwd": ".", "originator": "test", "cli_version": "test", diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 29eabe2bfa..d5ead442db 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -1492,7 +1492,6 @@ pub struct SessionMeta { pub cwd: PathBuf, pub originator: String, pub cli_version: String, - pub instructions: Option, #[serde(default)] pub source: SessionSource, pub model_provider: Option, @@ -1507,7 +1506,6 @@ impl Default for SessionMeta { cwd: PathBuf::new(), originator: String::new(), cli_version: String::new(), - instructions: None, source: SessionSource::default(), model_provider: None, } diff --git a/codex-rs/tui/src/resume_picker.rs b/codex-rs/tui/src/resume_picker.rs index 71d6da453b..4ce17721cd 100644 --- a/codex-rs/tui/src/resume_picker.rs +++ b/codex-rs/tui/src/resume_picker.rs @@ -1362,7 +1362,6 @@ mod tests { "cwd": cwd, "originator": "user", "cli_version": "0.0.0", - "instructions": null, "source": "Cli", "model_provider": "openai", } diff --git a/codex-rs/tui2/src/resume_picker.rs b/codex-rs/tui2/src/resume_picker.rs index 71d6da453b..4ce17721cd 100644 --- a/codex-rs/tui2/src/resume_picker.rs +++ b/codex-rs/tui2/src/resume_picker.rs @@ -1362,7 +1362,6 @@ mod tests { "cwd": cwd, "originator": "user", "cli_version": "0.0.0", - "instructions": null, "source": "Cli", "model_provider": "openai", } From 1478a88eb0e655ede6efe86764a5bf9255ca7a13 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Sat, 17 Jan 2026 17:31:14 -0800 Subject: [PATCH 29/32] Add collaboration developer instructions (#9424) - Add additional instructions when they are available - Make sure to update them on change either UserInput or UserTurn --- codex-rs/core/src/codex.rs | 158 ++++-- codex-rs/core/src/compact.rs | 2 +- codex-rs/core/src/rollout/truncation.rs | 2 +- codex-rs/core/src/thread_manager.rs | 2 +- .../tests/suite/collaboration_instructions.rs | 500 ++++++++++++++++++ codex-rs/core/tests/suite/mod.rs | 1 + codex-rs/core/tests/suite/override_updates.rs | 216 ++++++++ .../core/tests/suite/permissions_messages.rs | 6 +- codex-rs/core/tests/suite/prompt_caching.rs | 7 +- codex-rs/protocol/src/models.rs | 22 + codex-rs/protocol/src/protocol.rs | 2 + 11 files changed, 875 insertions(+), 43 deletions(-) create mode 100644 codex-rs/core/tests/suite/collaboration_instructions.rs create mode 100644 codex-rs/core/tests/suite/override_updates.rs diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index e3369f8228..c6e11b1376 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -815,7 +815,7 @@ impl Session { match conversation_history { InitialHistory::New => { // Build and record initial items (user instructions + environment context) - let items = self.build_initial_context(&turn_context); + let items = self.build_initial_context(&turn_context).await; self.record_conversation_items(&turn_context, &items).await; // Ensure initial items are visible to immediate readers (e.g., tests, forks). self.flush_rollout().await; @@ -853,8 +853,9 @@ impl Session { } // Always add response items to conversation history - let reconstructed_history = - self.reconstruct_history_from_rollout(&turn_context, &rollout_items); + let reconstructed_history = self + .reconstruct_history_from_rollout(&turn_context, &rollout_items) + .await; if !reconstructed_history.is_empty() { self.record_into_history(&reconstructed_history, &turn_context) .await; @@ -873,7 +874,7 @@ impl Session { } // Append the current session's initial context after the reconstructed history. - let initial_context = self.build_initial_context(&turn_context); + let initial_context = self.build_initial_context(&turn_context).await; self.record_conversation_items(&turn_context, &initial_context) .await; // Flush after seeding history and any persisted rollout copy. @@ -1061,6 +1062,50 @@ impl Session { ) } + fn build_collaboration_mode_update_item( + &self, + previous_collaboration_mode: &CollaborationMode, + next_collaboration_mode: Option<&CollaborationMode>, + ) -> Option { + if let Some(next_mode) = next_collaboration_mode { + if previous_collaboration_mode == next_mode { + return None; + } + // If the next mode has empty developer instructions, this returns None and we emit no + // update, so prior collaboration instructions remain in the prompt history. + Some(DeveloperInstructions::from_collaboration_mode(next_mode)?.into()) + } else { + None + } + } + + fn build_settings_update_items( + &self, + previous_context: Option<&Arc>, + current_context: &TurnContext, + previous_collaboration_mode: &CollaborationMode, + next_collaboration_mode: Option<&CollaborationMode>, + ) -> Vec { + let mut update_items = Vec::new(); + if let Some(env_item) = + self.build_environment_update_item(previous_context, current_context) + { + update_items.push(env_item); + } + if let Some(permissions_item) = + self.build_permissions_update_item(previous_context, current_context) + { + update_items.push(permissions_item); + } + if let Some(collaboration_mode_item) = self.build_collaboration_mode_update_item( + previous_collaboration_mode, + next_collaboration_mode, + ) { + update_items.push(collaboration_mode_item); + } + update_items + } + /// Persist the event to rollout and send it to clients. pub(crate) async fn send_event(&self, turn_context: &TurnContext, msg: EventMsg) { let legacy_source = msg.clone(); @@ -1299,7 +1344,7 @@ impl Session { self.send_raw_response_items(turn_context, items).await; } - fn reconstruct_history_from_rollout( + async fn reconstruct_history_from_rollout( &self, turn_context: &TurnContext, rollout_items: &[RolloutItem], @@ -1319,7 +1364,7 @@ impl Session { } else { let user_messages = collect_user_messages(history.raw_items()); let rebuilt = compact::build_compacted_history( - self.build_initial_context(turn_context), + self.build_initial_context(turn_context).await, &user_messages, &compacted.message, ); @@ -1389,7 +1434,10 @@ impl Session { } } - pub(crate) fn build_initial_context(&self, turn_context: &TurnContext) -> Vec { + pub(crate) async fn build_initial_context( + &self, + turn_context: &TurnContext, + ) -> Vec { let mut items = Vec::::with_capacity(4); let shell = self.user_shell(); items.push( @@ -1403,6 +1451,16 @@ impl Session { if let Some(developer_instructions) = turn_context.developer_instructions.as_deref() { items.push(DeveloperInstructions::new(developer_instructions.to_string()).into()); } + // Add developer instructions from collaboration_mode if they exist and are non-empty + let collaboration_mode = { + let state = self.state.lock().await; + state.session_configuration.collaboration_mode.clone() + }; + if let Some(collab_instructions) = + DeveloperInstructions::from_collaboration_mode(&collaboration_mode) + { + items.push(collab_instructions.into()); + } if let Some(user_instructions) = turn_context.user_instructions.as_deref() { items.push( UserInstructions { @@ -1984,6 +2042,18 @@ mod handlers { sub_id: String, updates: SessionSettingsUpdate, ) { + let previous_context = sess + .new_default_turn_with_sub_id(sess.next_internal_sub_id()) + .await; + let previous_collaboration_mode = sess + .state + .lock() + .await + .session_configuration + .collaboration_mode + .clone(); + let next_collaboration_mode = updates.collaboration_mode.clone(); + if let Err(err) = sess.update_settings(updates).await { sess.send_event_raw(Event { id: sub_id, @@ -1993,6 +2063,19 @@ mod handlers { }), }) .await; + return; + } + + let current_context = sess.new_default_turn_with_sub_id(sub_id).await; + let update_items = sess.build_settings_update_items( + Some(&previous_context), + ¤t_context, + &previous_collaboration_mode, + next_collaboration_mode.as_ref(), + ); + if !update_items.is_empty() { + sess.record_conversation_items(¤t_context, &update_items) + .await; } } @@ -2046,6 +2129,14 @@ mod handlers { _ => unreachable!(), }; + let previous_collaboration_mode = sess + .state + .lock() + .await + .session_configuration + .collaboration_mode + .clone(); + let next_collaboration_mode = updates.collaboration_mode.clone(); let Ok(current_context) = sess.new_turn_with_sub_id(sub_id, updates).await else { // new_turn_with_sub_id already emits the error event. return; @@ -2057,17 +2148,12 @@ mod handlers { // Attempt to inject input into current task if let Err(items) = sess.inject_input(items).await { - let mut update_items = Vec::new(); - if let Some(env_item) = - sess.build_environment_update_item(previous_context.as_ref(), ¤t_context) - { - update_items.push(env_item); - } - if let Some(permissions_item) = - sess.build_permissions_update_item(previous_context.as_ref(), ¤t_context) - { - update_items.push(permissions_item); - } + let update_items = sess.build_settings_update_items( + previous_context.as_ref(), + ¤t_context, + &previous_collaboration_mode, + next_collaboration_mode.as_ref(), + ); if !update_items.is_empty() { sess.record_conversation_items(¤t_context, &update_items) .await; @@ -3178,9 +3264,11 @@ mod tests { #[tokio::test] async fn reconstruct_history_matches_live_compactions() { let (session, turn_context) = make_session_and_context().await; - let (rollout_items, expected) = sample_rollout(&session, &turn_context); + let (rollout_items, expected) = sample_rollout(&session, &turn_context).await; - let reconstructed = session.reconstruct_history_from_rollout(&turn_context, &rollout_items); + let reconstructed = session + .reconstruct_history_from_rollout(&turn_context, &rollout_items) + .await; assert_eq!(expected, reconstructed); } @@ -3188,7 +3276,7 @@ mod tests { #[tokio::test] async fn record_initial_history_reconstructs_resumed_transcript() { let (session, turn_context) = make_session_and_context().await; - let (rollout_items, mut expected) = sample_rollout(&session, &turn_context); + let (rollout_items, mut expected) = sample_rollout(&session, &turn_context).await; session .record_initial_history(InitialHistory::Resumed(ResumedHistory { @@ -3198,7 +3286,7 @@ mod tests { })) .await; - expected.extend(session.build_initial_context(&turn_context)); + expected.extend(session.build_initial_context(&turn_context).await); let history = session.state.lock().await.clone_history(); assert_eq!(expected, history.raw_items()); } @@ -3206,7 +3294,7 @@ mod tests { #[tokio::test] async fn record_initial_history_seeds_token_info_from_rollout() { let (session, turn_context) = make_session_and_context().await; - let (mut rollout_items, _expected) = sample_rollout(&session, &turn_context); + let (mut rollout_items, _expected) = sample_rollout(&session, &turn_context).await; let info1 = TokenUsageInfo { total_token_usage: TokenUsage { @@ -3283,13 +3371,13 @@ mod tests { #[tokio::test] async fn record_initial_history_reconstructs_forked_transcript() { let (session, turn_context) = make_session_and_context().await; - let (rollout_items, mut expected) = sample_rollout(&session, &turn_context); + let (rollout_items, mut expected) = sample_rollout(&session, &turn_context).await; session .record_initial_history(InitialHistory::Forked(rollout_items)) .await; - expected.extend(session.build_initial_context(&turn_context)); + expected.extend(session.build_initial_context(&turn_context).await); let history = session.state.lock().await.clone_history(); assert_eq!(expected, history.raw_items()); } @@ -3298,7 +3386,7 @@ mod tests { async fn thread_rollback_drops_last_turn_from_history() { let (sess, tc, rx) = make_session_and_context_with_rx().await; - let initial_context = sess.build_initial_context(tc.as_ref()); + let initial_context = sess.build_initial_context(tc.as_ref()).await; sess.record_into_history(&initial_context, tc.as_ref()) .await; @@ -3355,7 +3443,7 @@ mod tests { async fn thread_rollback_clears_history_when_num_turns_exceeds_existing_turns() { let (sess, tc, rx) = make_session_and_context_with_rx().await; - let initial_context = sess.build_initial_context(tc.as_ref()); + let initial_context = sess.build_initial_context(tc.as_ref()).await; sess.record_into_history(&initial_context, tc.as_ref()) .await; @@ -3381,7 +3469,7 @@ mod tests { async fn thread_rollback_fails_when_turn_in_progress() { let (sess, tc, rx) = make_session_and_context_with_rx().await; - let initial_context = sess.build_initial_context(tc.as_ref()); + let initial_context = sess.build_initial_context(tc.as_ref()).await; sess.record_into_history(&initial_context, tc.as_ref()) .await; @@ -3402,7 +3490,7 @@ mod tests { async fn thread_rollback_fails_when_num_turns_is_zero() { let (sess, tc, rx) = make_session_and_context_with_rx().await; - let initial_context = sess.build_initial_context(tc.as_ref()); + let initial_context = sess.build_initial_context(tc.as_ref()).await; sess.record_into_history(&initial_context, tc.as_ref()) .await; @@ -4188,14 +4276,14 @@ mod tests { } } - fn sample_rollout( + async fn sample_rollout( session: &Session, turn_context: &TurnContext, ) -> (Vec, Vec) { let mut rollout_items = Vec::new(); let mut live_history = ContextManager::new(); - let initial_context = session.build_initial_context(turn_context); + let initial_context = session.build_initial_context(turn_context).await; for item in &initial_context { rollout_items.push(RolloutItem::ResponseItem(item.clone())); } @@ -4225,7 +4313,7 @@ mod tests { let snapshot1 = live_history.clone().for_prompt(); let user_messages1 = collect_user_messages(&snapshot1); let rebuilt1 = compact::build_compacted_history( - session.build_initial_context(turn_context), + session.build_initial_context(turn_context).await, &user_messages1, summary1, ); @@ -4259,7 +4347,7 @@ mod tests { let snapshot2 = live_history.clone().for_prompt(); let user_messages2 = collect_user_messages(&snapshot2); let rebuilt2 = compact::build_compacted_history( - session.build_initial_context(turn_context), + session.build_initial_context(turn_context).await, &user_messages2, summary2, ); @@ -4277,7 +4365,7 @@ mod tests { }], }; live_history.record_items(std::iter::once(&user3), turn_context.truncation_policy); - rollout_items.push(RolloutItem::ResponseItem(user3.clone())); + rollout_items.push(RolloutItem::ResponseItem(user3)); let assistant3 = ResponseItem::Message { id: None, @@ -4287,7 +4375,7 @@ mod tests { }], }; live_history.record_items(std::iter::once(&assistant3), turn_context.truncation_policy); - rollout_items.push(RolloutItem::ResponseItem(assistant3.clone())); + rollout_items.push(RolloutItem::ResponseItem(assistant3)); (rollout_items, live_history.for_prompt()) } diff --git a/codex-rs/core/src/compact.rs b/codex-rs/core/src/compact.rs index 4dc56f10d2..250b91415d 100644 --- a/codex-rs/core/src/compact.rs +++ b/codex-rs/core/src/compact.rs @@ -167,7 +167,7 @@ async fn run_compact_task_inner( let summary_text = format!("{SUMMARY_PREFIX}\n{summary_suffix}"); let user_messages = collect_user_messages(history_items); - let initial_context = sess.build_initial_context(turn_context.as_ref()); + let initial_context = sess.build_initial_context(turn_context.as_ref()).await; let mut new_history = build_compacted_history(initial_context, &user_messages, &summary_text); let ghost_snapshots: Vec = history_items .iter() diff --git a/codex-rs/core/src/rollout/truncation.rs b/codex-rs/core/src/rollout/truncation.rs index 1f70be46f3..0f72cfc50e 100644 --- a/codex-rs/core/src/rollout/truncation.rs +++ b/codex-rs/core/src/rollout/truncation.rs @@ -189,7 +189,7 @@ mod tests { #[tokio::test] async fn ignores_session_prefix_messages_when_truncating_rollout_from_start() { let (session, turn_context) = make_session_and_context().await; - let mut items = session.build_initial_context(&turn_context); + let mut items = session.build_initial_context(&turn_context).await; items.push(user_msg("feature request")); items.push(assistant_msg("ack")); items.push(user_msg("second question")); diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index 8d02828d59..f533e608f7 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -467,7 +467,7 @@ mod tests { #[tokio::test] async fn ignores_session_prefix_messages_when_truncating() { let (session, turn_context) = make_session_and_context().await; - let mut items = session.build_initial_context(&turn_context); + let mut items = session.build_initial_context(&turn_context).await; items.push(user_msg("feature request")); items.push(assistant_msg("ack")); items.push(user_msg("second question")); diff --git a/codex-rs/core/tests/suite/collaboration_instructions.rs b/codex-rs/core/tests/suite/collaboration_instructions.rs new file mode 100644 index 0000000000..c0df7e5ec5 --- /dev/null +++ b/codex-rs/core/tests/suite/collaboration_instructions.rs @@ -0,0 +1,500 @@ +use anyhow::Result; +use codex_core::protocol::COLLABORATION_MODE_CLOSE_TAG; +use codex_core::protocol::COLLABORATION_MODE_OPEN_TAG; +use codex_core::protocol::EventMsg; +use codex_core::protocol::Op; +use codex_protocol::config_types::CollaborationMode; +use codex_protocol::config_types::Settings; +use codex_protocol::user_input::UserInput; +use core_test_support::responses::ev_completed; +use core_test_support::responses::ev_response_created; +use core_test_support::responses::mount_sse_once; +use core_test_support::responses::sse; +use core_test_support::responses::start_mock_server; +use core_test_support::skip_if_no_network; +use core_test_support::test_codex::test_codex; +use core_test_support::wait_for_event; +use pretty_assertions::assert_eq; +use serde_json::Value; + +fn sse_completed(id: &str) -> String { + sse(vec![ev_response_created(id), ev_completed(id)]) +} + +fn collab_mode_with_instructions(instructions: Option<&str>) -> CollaborationMode { + CollaborationMode::Custom(Settings { + model: "gpt-5.1".to_string(), + reasoning_effort: None, + developer_instructions: instructions.map(str::to_string), + }) +} + +fn developer_texts(input: &[Value]) -> Vec { + input + .iter() + .filter_map(|item| { + let role = item.get("role")?.as_str()?; + if role != "developer" { + return None; + } + let text = item + .get("content")? + .as_array()? + .first()? + .get("text")? + .as_str()?; + Some(text.to_string()) + }) + .collect() +} + +fn collab_xml(text: &str) -> String { + format!("{COLLABORATION_MODE_OPEN_TAG}{text}{COLLABORATION_MODE_CLOSE_TAG}") +} + +fn count_exact(texts: &[String], target: &str) -> usize { + texts.iter().filter(|text| text.as_str() == target).count() +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn no_collaboration_instructions_by_default() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let req = mount_sse_once(&server, sse_completed("resp-1")).await; + + let test = test_codex().build(&server).await?; + + test.codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req.single_request().input(); + let dev_texts = developer_texts(&input); + assert_eq!(dev_texts.len(), 1); + assert!(dev_texts[0].contains("`approval_policy`")); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn user_input_includes_collaboration_instructions_after_override() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let req = mount_sse_once(&server, sse_completed("resp-1")).await; + + let test = test_codex().build(&server).await?; + + let collab_text = "collab instructions"; + let collaboration_mode = collab_mode_with_instructions(Some(collab_text)); + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collaboration_mode), + }) + .await?; + + test.codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req.single_request().input(); + let dev_texts = developer_texts(&input); + let collab_text = collab_xml(collab_text); + assert_eq!(count_exact(&dev_texts, &collab_text), 1); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn collaboration_instructions_added_on_user_turn() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let req = mount_sse_once(&server, sse_completed("resp-1")).await; + + let test = test_codex().build(&server).await?; + let collab_text = "turn instructions"; + let collaboration_mode = collab_mode_with_instructions(Some(collab_text)); + + test.codex + .submit(Op::UserTurn { + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + cwd: test.config.cwd.clone(), + approval_policy: test.config.approval_policy.value(), + sandbox_policy: test.config.sandbox_policy.get().clone(), + model: test.session_configured.model.clone(), + effort: None, + summary: test.config.model_reasoning_summary, + collaboration_mode: Some(collaboration_mode), + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req.single_request().input(); + let dev_texts = developer_texts(&input); + let collab_text = collab_xml(collab_text); + assert_eq!(count_exact(&dev_texts, &collab_text), 1); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn override_then_user_turn_uses_updated_collaboration_instructions() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let req = mount_sse_once(&server, sse_completed("resp-1")).await; + + let test = test_codex().build(&server).await?; + let collab_text = "override instructions"; + let collaboration_mode = collab_mode_with_instructions(Some(collab_text)); + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collaboration_mode), + }) + .await?; + + test.codex + .submit(Op::UserTurn { + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + cwd: test.config.cwd.clone(), + approval_policy: test.config.approval_policy.value(), + sandbox_policy: test.config.sandbox_policy.get().clone(), + model: test.session_configured.model.clone(), + effort: None, + summary: test.config.model_reasoning_summary, + collaboration_mode: None, + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req.single_request().input(); + let dev_texts = developer_texts(&input); + let collab_text = collab_xml(collab_text); + assert_eq!(count_exact(&dev_texts, &collab_text), 1); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn user_turn_overrides_collaboration_instructions_after_override() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let req = mount_sse_once(&server, sse_completed("resp-1")).await; + + let test = test_codex().build(&server).await?; + let base_text = "base instructions"; + let base_mode = collab_mode_with_instructions(Some(base_text)); + let turn_text = "turn override"; + let turn_mode = collab_mode_with_instructions(Some(turn_text)); + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(base_mode), + }) + .await?; + + test.codex + .submit(Op::UserTurn { + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + cwd: test.config.cwd.clone(), + approval_policy: test.config.approval_policy.value(), + sandbox_policy: test.config.sandbox_policy.get().clone(), + model: test.session_configured.model.clone(), + effort: None, + summary: test.config.model_reasoning_summary, + collaboration_mode: Some(turn_mode), + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req.single_request().input(); + let dev_texts = developer_texts(&input); + let base_text = collab_xml(base_text); + let turn_text = collab_xml(turn_text); + assert_eq!(count_exact(&dev_texts, &base_text), 1); + assert_eq!(count_exact(&dev_texts, &turn_text), 1); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn collaboration_mode_update_emits_new_instruction_message() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let _req1 = mount_sse_once(&server, sse_completed("resp-1")).await; + let req2 = mount_sse_once(&server, sse_completed("resp-2")).await; + + let test = test_codex().build(&server).await?; + let first_text = "first instructions"; + let second_text = "second instructions"; + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collab_mode_with_instructions(Some(first_text))), + }) + .await?; + + test.codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello 1".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collab_mode_with_instructions(Some(second_text))), + }) + .await?; + + test.codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello 2".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req2.single_request().input(); + let dev_texts = developer_texts(&input); + let first_text = collab_xml(first_text); + let second_text = collab_xml(second_text); + assert_eq!(count_exact(&dev_texts, &first_text), 1); + assert_eq!(count_exact(&dev_texts, &second_text), 1); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn collaboration_mode_update_noop_does_not_append() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let _req1 = mount_sse_once(&server, sse_completed("resp-1")).await; + let req2 = mount_sse_once(&server, sse_completed("resp-2")).await; + + let test = test_codex().build(&server).await?; + let collab_text = "same instructions"; + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collab_mode_with_instructions(Some(collab_text))), + }) + .await?; + + test.codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello 1".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collab_mode_with_instructions(Some(collab_text))), + }) + .await?; + + test.codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello 2".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req2.single_request().input(); + let dev_texts = developer_texts(&input); + let collab_text = collab_xml(collab_text); + assert_eq!(count_exact(&dev_texts, &collab_text), 1); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn resume_replays_collaboration_instructions() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let _req1 = mount_sse_once(&server, sse_completed("resp-1")).await; + let req2 = mount_sse_once(&server, sse_completed("resp-2")).await; + + let mut builder = test_codex(); + let initial = builder.build(&server).await?; + let rollout_path = initial.session_configured.rollout_path.clone(); + let home = initial.home.clone(); + + let collab_text = "resume instructions"; + initial + .codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collab_mode_with_instructions(Some(collab_text))), + }) + .await?; + + initial + .codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&initial.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let resumed = builder.resume(&server, home, rollout_path).await?; + resumed + .codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "after resume".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&resumed.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req2.single_request().input(); + let dev_texts = developer_texts(&input); + let collab_text = collab_xml(collab_text); + assert_eq!(count_exact(&dev_texts, &collab_text), 1); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn empty_collaboration_instructions_are_ignored() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let req = mount_sse_once(&server, sse_completed("resp-1")).await; + + let test = test_codex().build(&server).await?; + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collab_mode_with_instructions(Some(""))), + }) + .await?; + + test.codex + .submit(Op::UserInput { + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + }) + .await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let input = req.single_request().input(); + let dev_texts = developer_texts(&input); + assert_eq!(dev_texts.len(), 1); + let collab_text = collab_xml(""); + assert_eq!(count_exact(&dev_texts, &collab_text), 0); + + Ok(()) +} diff --git a/codex-rs/core/tests/suite/mod.rs b/codex-rs/core/tests/suite/mod.rs index 0d22e8c393..b8066db387 100644 --- a/codex-rs/core/tests/suite/mod.rs +++ b/codex-rs/core/tests/suite/mod.rs @@ -24,6 +24,7 @@ mod cli_stream; mod client; mod client_websockets; mod codex_delegate; +mod collaboration_instructions; mod compact; mod compact_remote; mod compact_resume_fork; diff --git a/codex-rs/core/tests/suite/override_updates.rs b/codex-rs/core/tests/suite/override_updates.rs new file mode 100644 index 0000000000..ddacd97abb --- /dev/null +++ b/codex-rs/core/tests/suite/override_updates.rs @@ -0,0 +1,216 @@ +use anyhow::Result; +use codex_core::config::Constrained; +use codex_core::protocol::AskForApproval; +use codex_core::protocol::COLLABORATION_MODE_CLOSE_TAG; +use codex_core::protocol::COLLABORATION_MODE_OPEN_TAG; +use codex_core::protocol::EventMsg; +use codex_core::protocol::Op; +use codex_core::protocol::RolloutItem; +use codex_core::protocol::RolloutLine; +use codex_core::protocol::ENVIRONMENT_CONTEXT_OPEN_TAG; +use codex_protocol::config_types::CollaborationMode; +use codex_protocol::config_types::Settings; +use codex_protocol::models::ContentItem; +use codex_protocol::models::ResponseItem; +use core_test_support::responses::start_mock_server; +use core_test_support::skip_if_no_network; +use core_test_support::test_codex::test_codex; +use core_test_support::wait_for_event; +use pretty_assertions::assert_eq; +use std::collections::HashSet; +use std::path::Path; +use std::time::Duration; +use tempfile::TempDir; + +fn collab_mode_with_instructions(instructions: Option<&str>) -> CollaborationMode { + CollaborationMode::Custom(Settings { + model: "gpt-5.1".to_string(), + reasoning_effort: None, + developer_instructions: instructions.map(str::to_string), + }) +} + +fn collab_xml(text: &str) -> String { + format!("{COLLABORATION_MODE_OPEN_TAG}{text}{COLLABORATION_MODE_CLOSE_TAG}") +} + +async fn read_rollout_text(path: &Path) -> anyhow::Result { + for _ in 0..50 { + if path.exists() + && let Ok(text) = std::fs::read_to_string(path) + && !text.trim().is_empty() + { + return Ok(text); + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + Ok(std::fs::read_to_string(path)?) +} + +fn rollout_developer_texts(text: &str) -> Vec { + let mut texts = Vec::new(); + for line in text.lines() { + let trimmed = line.trim(); + if trimmed.is_empty() { + continue; + } + let rollout: RolloutLine = match serde_json::from_str(trimmed) { + Ok(rollout) => rollout, + Err(_) => continue, + }; + if let RolloutItem::ResponseItem(ResponseItem::Message { role, content, .. }) = + rollout.item + && role == "developer" + { + for item in content { + if let ContentItem::InputText { text } = item { + texts.push(text); + } + } + } + } + texts +} + +fn rollout_environment_texts(text: &str) -> Vec { + let mut texts = Vec::new(); + for line in text.lines() { + let trimmed = line.trim(); + if trimmed.is_empty() { + continue; + } + let rollout: RolloutLine = match serde_json::from_str(trimmed) { + Ok(rollout) => rollout, + Err(_) => continue, + }; + if let RolloutItem::ResponseItem(ResponseItem::Message { role, content, .. }) = + rollout.item + && role == "user" + { + for item in content { + if let ContentItem::InputText { text } = item + && text.starts_with(ENVIRONMENT_CONTEXT_OPEN_TAG) + { + texts.push(text); + } + } + } + } + texts +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn override_turn_context_records_permissions_update() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let mut builder = test_codex().with_config(|config| { + config.approval_policy = Constrained::allow_any(AskForApproval::OnRequest); + }); + let test = builder.build(&server).await?; + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: Some(AskForApproval::Never), + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: None, + }) + .await?; + + test.codex.submit(Op::Shutdown).await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::ShutdownComplete)).await; + + let rollout_path = test.codex.rollout_path(); + let rollout_text = read_rollout_text(&rollout_path).await?; + let developer_texts = rollout_developer_texts(&rollout_text); + let approval_texts: Vec<&String> = developer_texts + .iter() + .filter(|text| text.contains("`approval_policy`")) + .collect(); + assert!( + approval_texts + .iter() + .any(|text| text.contains("`approval_policy` is `never`")), + "expected updated approval policy instructions in rollout" + ); + let unique: HashSet<&String> = approval_texts.iter().copied().collect(); + assert_eq!(unique.len(), 2); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn override_turn_context_records_environment_update() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let test = test_codex().build(&server).await?; + let new_cwd = TempDir::new()?; + + test.codex + .submit(Op::OverrideTurnContext { + cwd: Some(new_cwd.path().to_path_buf()), + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: None, + }) + .await?; + + test.codex.submit(Op::Shutdown).await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::ShutdownComplete)).await; + + let rollout_path = test.codex.rollout_path(); + let rollout_text = read_rollout_text(&rollout_path).await?; + let env_texts = rollout_environment_texts(&rollout_text); + let new_cwd_text = new_cwd.path().display().to_string(); + assert!( + env_texts.iter().any(|text| text.contains(&new_cwd_text)), + "expected environment update with new cwd in rollout" + ); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn override_turn_context_records_collaboration_update() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let test = test_codex().build(&server).await?; + let collab_text = "override collaboration instructions"; + let collaboration_mode = collab_mode_with_instructions(Some(collab_text)); + + test.codex + .submit(Op::OverrideTurnContext { + cwd: None, + approval_policy: None, + sandbox_policy: None, + model: None, + effort: None, + summary: None, + collaboration_mode: Some(collaboration_mode), + }) + .await?; + + test.codex.submit(Op::Shutdown).await?; + wait_for_event(&test.codex, |ev| matches!(ev, EventMsg::ShutdownComplete)).await; + + let rollout_path = test.codex.rollout_path(); + let rollout_text = read_rollout_text(&rollout_path).await?; + let developer_texts = rollout_developer_texts(&rollout_text); + let collab_text = collab_xml(collab_text); + let collab_count = developer_texts + .iter() + .filter(|text| text.as_str() == collab_text.as_str()) + .count(); + assert_eq!(collab_count, 1); + + Ok(()) +} diff --git a/codex-rs/core/tests/suite/permissions_messages.rs b/codex-rs/core/tests/suite/permissions_messages.rs index 3e54fa29a0..af61e54a5c 100644 --- a/codex-rs/core/tests/suite/permissions_messages.rs +++ b/codex-rs/core/tests/suite/permissions_messages.rs @@ -132,7 +132,7 @@ async fn permissions_message_added_on_override_change() -> Result<()> { let permissions_2 = permissions_texts(input2); assert_eq!(permissions_1.len(), 1); - assert_eq!(permissions_2.len(), 2); + assert_eq!(permissions_2.len(), 3); let unique = permissions_2.into_iter().collect::>(); assert_eq!(unique.len(), 2); @@ -257,7 +257,7 @@ async fn resume_replays_permissions_messages() -> Result<()> { let body3 = req3.single_request().body_json(); let input = body3["input"].as_array().expect("input array"); let permissions = permissions_texts(input); - assert_eq!(permissions.len(), 3); + assert_eq!(permissions.len(), 4); let unique = permissions.into_iter().collect::>(); assert_eq!(unique.len(), 2); @@ -321,7 +321,7 @@ async fn resume_and_fork_append_permissions_messages() -> Result<()> { let body2 = req2.single_request().body_json(); let input2 = body2["input"].as_array().expect("input array"); let permissions_base = permissions_texts(input2); - assert_eq!(permissions_base.len(), 2); + assert_eq!(permissions_base.len(), 3); builder = builder.with_config(|config| { config.approval_policy = Constrained::allow_any(AskForApproval::UnlessTrusted); diff --git a/codex-rs/core/tests/suite/prompt_caching.rs b/codex-rs/core/tests/suite/prompt_caching.rs index 093932512f..4e87a43640 100644 --- a/codex-rs/core/tests/suite/prompt_caching.rs +++ b/codex-rs/core/tests/suite/prompt_caching.rs @@ -379,15 +379,18 @@ async fn overrides_turn_context_but_keeps_cached_prefix_and_key_constant() -> an "content": [ { "type": "input_text", "text": "hello 2" } ] }); let expected_permissions_msg = body1["input"][0].clone(); - // After overriding the turn context, emit a new permissions message. let body1_input = body1["input"].as_array().expect("input array"); + // After overriding the turn context, emit two updated permissions messages. let expected_permissions_msg_2 = body2["input"][body1_input.len()].clone(); + let expected_permissions_msg_3 = body2["input"][body1_input.len() + 1].clone(); assert_ne!( expected_permissions_msg_2, expected_permissions_msg, "expected updated permissions message after override" ); - let mut expected_body2 = body1["input"].as_array().expect("input array").to_vec(); + assert_eq!(expected_permissions_msg_2, expected_permissions_msg_3); + let mut expected_body2 = body1_input.to_vec(); expected_body2.push(expected_permissions_msg_2); + expected_body2.push(expected_permissions_msg_3); expected_body2.push(expected_user_message_2); assert_eq!(body2["input"], serde_json::Value::Array(expected_body2)); diff --git a/codex-rs/protocol/src/models.rs b/codex-rs/protocol/src/models.rs index d2c9ddd564..4d54ed08f0 100644 --- a/codex-rs/protocol/src/models.rs +++ b/codex-rs/protocol/src/models.rs @@ -10,8 +10,11 @@ use serde::Serialize; use serde::ser::Serializer; use ts_rs::TS; +use crate::config_types::CollaborationMode; use crate::config_types::SandboxMode; use crate::protocol::AskForApproval; +use crate::protocol::COLLABORATION_MODE_CLOSE_TAG; +use crate::protocol::COLLABORATION_MODE_OPEN_TAG; use crate::protocol::NetworkAccess; use crate::protocol::SandboxPolicy; use crate::protocol::WritableRoot; @@ -230,6 +233,25 @@ impl DeveloperInstructions { ) } + /// Returns developer instructions from a collaboration mode if they exist and are non-empty. + pub fn from_collaboration_mode(collaboration_mode: &CollaborationMode) -> Option { + let settings = match collaboration_mode { + CollaborationMode::Plan(settings) + | CollaborationMode::Collaborate(settings) + | CollaborationMode::Execute(settings) + | CollaborationMode::Custom(settings) => settings, + }; + settings + .developer_instructions + .as_ref() + .filter(|instructions| !instructions.is_empty()) + .map(|instructions| { + DeveloperInstructions::new(format!( + "{COLLABORATION_MODE_OPEN_TAG}{instructions}{COLLABORATION_MODE_CLOSE_TAG}" + )) + }) + } + fn from_permissions_with_network( sandbox_mode: SandboxMode, network_access: NetworkAccess, diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index d5ead442db..1142f182c0 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -51,6 +51,8 @@ pub const USER_INSTRUCTIONS_OPEN_TAG: &str = ""; pub const USER_INSTRUCTIONS_CLOSE_TAG: &str = ""; pub const ENVIRONMENT_CONTEXT_OPEN_TAG: &str = ""; pub const ENVIRONMENT_CONTEXT_CLOSE_TAG: &str = ""; +pub const COLLABORATION_MODE_OPEN_TAG: &str = ""; +pub const COLLABORATION_MODE_CLOSE_TAG: &str = ""; pub const USER_MESSAGE_BEGIN: &str = "## My request for Codex:"; /// Submission Queue Entry - requests from user From aeaff26451ec9b230550a4f1855d731e719ca608 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Sat, 17 Jan 2026 19:46:07 -0800 Subject: [PATCH 30/32] Preserve slash command order in search (#9425) Keep slash popup search results in presentation order for built-ins and prompts. --- codex-rs/tui/src/bottom_pane/command_popup.rs | 44 ++++++++++++------- .../tui2/src/bottom_pane/command_popup.rs | 34 +++++++------- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/codex-rs/tui/src/bottom_pane/command_popup.rs b/codex-rs/tui/src/bottom_pane/command_popup.rs index ec4e86af03..10a469c683 100644 --- a/codex-rs/tui/src/bottom_pane/command_popup.rs +++ b/codex-rs/tui/src/bottom_pane/command_popup.rs @@ -113,8 +113,8 @@ impl CommandPopup { } /// Compute fuzzy-filtered matches over built-in commands and user prompts, - /// paired with optional highlight indices and score. Sorted by ascending - /// score, then by name for stability. + /// paired with optional highlight indices and score. Preserves the original + /// presentation order for built-ins and prompts. fn filtered(&self) -> Vec<(CommandItem, Option>, i32)> { let filter = self.command_filter.trim(); let mut out: Vec<(CommandItem, Option>, i32)> = Vec::new(); @@ -144,20 +144,6 @@ impl CommandPopup { out.push((CommandItem::UserPrompt(idx), Some(indices), score)); } } - // When filtering, sort by ascending score and then by name for stability. - out.sort_by(|a, b| { - a.2.cmp(&b.2).then_with(|| { - let an = match a.0 { - CommandItem::Builtin(c) => c.command(), - CommandItem::UserPrompt(i) => &self.prompts[i].name, - }; - let bn = match b.0 { - CommandItem::Builtin(c) => c.command(), - CommandItem::UserPrompt(i) => &self.prompts[i].name, - }; - an.cmp(bn) - }) - }); out } @@ -292,6 +278,32 @@ mod tests { } } + #[test] + fn filtered_commands_keep_presentation_order() { + let mut popup = CommandPopup::new(Vec::new(), false); + popup.on_composer_text_change("/m".to_string()); + + let cmds: Vec<&str> = popup + .filtered_items() + .into_iter() + .filter_map(|item| match item { + CommandItem::Builtin(cmd) => Some(cmd.command()), + CommandItem::UserPrompt(_) => None, + }) + .collect(); + assert_eq!( + cmds, + vec![ + "model", + "experimental", + "resume", + "compact", + "mention", + "mcp" + ] + ); + } + #[test] fn prompt_discovery_lists_custom_prompts() { let prompts = vec![ diff --git a/codex-rs/tui2/src/bottom_pane/command_popup.rs b/codex-rs/tui2/src/bottom_pane/command_popup.rs index 2fee23f1b6..ba49ab12c0 100644 --- a/codex-rs/tui2/src/bottom_pane/command_popup.rs +++ b/codex-rs/tui2/src/bottom_pane/command_popup.rs @@ -113,8 +113,8 @@ impl CommandPopup { } /// Compute fuzzy-filtered matches over built-in commands and user prompts, - /// paired with optional highlight indices and score. Sorted by ascending - /// score, then by name for stability. + /// paired with optional highlight indices and score. Preserves the original + /// presentation order for built-ins and prompts. fn filtered(&self) -> Vec<(CommandItem, Option>, i32)> { let filter = self.command_filter.trim(); let mut out: Vec<(CommandItem, Option>, i32)> = Vec::new(); @@ -144,20 +144,6 @@ impl CommandPopup { out.push((CommandItem::UserPrompt(idx), Some(indices), score)); } } - // When filtering, sort by ascending score and then by name for stability. - out.sort_by(|a, b| { - a.2.cmp(&b.2).then_with(|| { - let an = match a.0 { - CommandItem::Builtin(c) => c.command(), - CommandItem::UserPrompt(i) => &self.prompts[i].name, - }; - let bn = match b.0 { - CommandItem::Builtin(c) => c.command(), - CommandItem::UserPrompt(i) => &self.prompts[i].name, - }; - an.cmp(bn) - }) - }); out } @@ -291,6 +277,22 @@ mod tests { } } + #[test] + fn filtered_commands_keep_presentation_order() { + let mut popup = CommandPopup::new(Vec::new(), false); + popup.on_composer_text_change("/m".to_string()); + + let cmds: Vec<&str> = popup + .filtered_items() + .into_iter() + .filter_map(|item| match item { + CommandItem::Builtin(cmd) => Some(cmd.command()), + CommandItem::UserPrompt(_) => None, + }) + .collect(); + assert_eq!(cmds, vec!["model", "resume", "compact", "mention", "mcp"]); + } + #[test] fn prompt_discovery_lists_custom_prompts() { let prompts = vec![ From 0a568a47fdd9dc9290e50d8050f4685e2511b74b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SlKz=E1=B5=8D=E1=B5=90?= <105301169+slkzgm@users.noreply.github.com> Date: Sun, 18 Jan 2026 05:10:24 +0100 Subject: [PATCH 31/32] tui: allow forward navigation in backtrack preview (#9059) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #9058 ## Summary When the transcript backtrack preview is armed (press `Esc`), allow navigating to newer user messages with the `→` arrow, in addition to navigating backwards with `Esc`/`←`, before confirming with `Enter`. ## Changes - Backtrack preview navigation: `Esc`/`←` steps to older user messages, `→` steps to newer ones, `Enter` edits the selected message (clamped at bounds, no wrap-around). - Transcript overlay footer hints updated to advertise `esc/←`, `→`, and `enter` when a message is highlighted. ## Related - WSL shortcut-overlay snapshot determinism: #9359 ## Testing - `just fmt` - `just fix -p codex-tui` - `just fix -p codex-tui2` - `cargo test -p codex-tui app_backtrack::` - `cargo test -p codex-tui pager_overlay::` - `cargo test -p codex-tui2 app_backtrack::` - `cargo test -p codex-tui2 pager_overlay::` --------- Co-authored-by: Josh McKinney --- codex-rs/tui/src/app_backtrack.rs | 56 ++++++++++++++++++++++++++++-- codex-rs/tui/src/pager_overlay.rs | 41 +++++++++++++++------- codex-rs/tui2/src/app_backtrack.rs | 56 ++++++++++++++++++++++++++++-- codex-rs/tui2/src/pager_overlay.rs | 41 +++++++++++++++------- 4 files changed, 166 insertions(+), 28 deletions(-) diff --git a/codex-rs/tui/src/app_backtrack.rs b/codex-rs/tui/src/app_backtrack.rs index 73d238f9a4..80fc65ca77 100644 --- a/codex-rs/tui/src/app_backtrack.rs +++ b/codex-rs/tui/src/app_backtrack.rs @@ -93,8 +93,9 @@ pub(crate) struct PendingBacktrackRollback { impl App { /// Route overlay events while the transcript overlay is active. /// - /// If backtrack preview is active, Esc steps the selection and Enter confirms it. - /// Otherwise, Esc begins preview mode and all other events are forwarded to the overlay. + /// If backtrack preview is active, Esc / Left steps selection, Right steps forward, Enter + /// confirms. Otherwise, Esc begins preview mode and all other events are forwarded to the + /// overlay. pub(crate) async fn handle_backtrack_overlay_event( &mut self, tui: &mut tui::Tui, @@ -110,6 +111,22 @@ impl App { self.overlay_step_backtrack(tui, event)?; Ok(true) } + TuiEvent::Key(KeyEvent { + code: KeyCode::Left, + kind: KeyEventKind::Press | KeyEventKind::Repeat, + .. + }) => { + self.overlay_step_backtrack(tui, event)?; + Ok(true) + } + TuiEvent::Key(KeyEvent { + code: KeyCode::Right, + kind: KeyEventKind::Press | KeyEventKind::Repeat, + .. + }) => { + self.overlay_step_backtrack_forward(tui, event)?; + Ok(true) + } TuiEvent::Key(KeyEvent { code: KeyCode::Enter, kind: KeyEventKind::Press, @@ -277,6 +294,27 @@ impl App { tui.frame_requester().schedule_frame(); } + /// Step selection to the next newer user message and update overlay. + fn step_forward_backtrack_and_highlight(&mut self, tui: &mut tui::Tui) { + let count = user_count(&self.transcript_cells); + if count == 0 { + return; + } + + let last_index = count.saturating_sub(1); + let next_selection = if self.backtrack.nth_user_message == usize::MAX { + last_index + } else { + self.backtrack + .nth_user_message + .saturating_add(1) + .min(last_index) + }; + + self.apply_backtrack_selection_internal(next_selection); + tui.frame_requester().schedule_frame(); + } + /// Apply a computed backtrack selection to the overlay and internal counter. fn apply_backtrack_selection_internal(&mut self, nth_user_message: usize) { if let Some(cell_idx) = nth_user_position(&self.transcript_cells, nth_user_message) { @@ -364,6 +402,20 @@ impl App { Ok(()) } + /// Handle Right in overlay backtrack preview: step selection forward if armed, else forward. + fn overlay_step_backtrack_forward( + &mut self, + tui: &mut tui::Tui, + event: TuiEvent, + ) -> Result<()> { + if self.backtrack.base_id.is_some() { + self.step_forward_backtrack_and_highlight(tui); + } else { + self.overlay_forward_event(tui, event)?; + } + Ok(()) + } + /// Confirm a primed backtrack from the main view (no overlay visible). /// Computes the prefill from the selected user message for rollback. pub(crate) fn confirm_backtrack_from_main(&mut self) -> Option { diff --git a/codex-rs/tui/src/pager_overlay.rs b/codex-rs/tui/src/pager_overlay.rs index ae71a58c6d..5f3f79e320 100644 --- a/codex-rs/tui/src/pager_overlay.rs +++ b/codex-rs/tui/src/pager_overlay.rs @@ -92,6 +92,8 @@ const KEY_SPACE: KeyBinding = key_hint::plain(KeyCode::Char(' ')); const KEY_SHIFT_SPACE: KeyBinding = key_hint::shift(KeyCode::Char(' ')); const KEY_HOME: KeyBinding = key_hint::plain(KeyCode::Home); const KEY_END: KeyBinding = key_hint::plain(KeyCode::End); +const KEY_LEFT: KeyBinding = key_hint::plain(KeyCode::Left); +const KEY_RIGHT: KeyBinding = key_hint::plain(KeyCode::Right); const KEY_CTRL_F: KeyBinding = key_hint::ctrl(KeyCode::Char('f')); const KEY_CTRL_D: KeyBinding = key_hint::ctrl(KeyCode::Char('d')); const KEY_CTRL_B: KeyBinding = key_hint::ctrl(KeyCode::Char('b')); @@ -637,10 +639,13 @@ impl TranscriptOverlay { let line2 = Rect::new(area.x, area.y.saturating_add(1), area.width, 1); render_key_hints(line1, buf, PAGER_KEY_HINTS); - let mut pairs: Vec<(&[KeyBinding], &str)> = - vec![(&[KEY_Q], "to quit"), (&[KEY_ESC], "to edit prev")]; + let mut pairs: Vec<(&[KeyBinding], &str)> = vec![(&[KEY_Q], "to quit")]; if self.highlight_cell.is_some() { + pairs.push((&[KEY_ESC, KEY_LEFT], "to edit prev")); + pairs.push((&[KEY_RIGHT], "to edit next")); pairs.push((&[KEY_ENTER], "to edit message")); + } else { + pairs.push((&[KEY_ESC], "to edit prev")); } render_key_hints(line2, buf, &pairs); } @@ -816,25 +821,37 @@ mod tests { lines: vec![Line::from("hello")], })]); - // Render into a small buffer and assert the backtrack hint is present - let area = Rect::new(0, 0, 40, 10); + // Render into a wide buffer so the footer hints aren't truncated. + let area = Rect::new(0, 0, 120, 10); let mut buf = Buffer::empty(area); overlay.render(area, &mut buf); - // Flatten buffer to a string and check for the hint text - let mut s = String::new(); - for y in area.y..area.bottom() { - for x in area.x..area.right() { - s.push(buf[(x, y)].symbol().chars().next().unwrap_or(' ')); - } - s.push('\n'); - } + let s = buffer_to_text(&buf, area); assert!( s.contains("edit prev"), "expected 'edit prev' hint in overlay footer, got: {s:?}" ); } + #[test] + fn edit_next_hint_is_visible_when_highlighted() { + let mut overlay = TranscriptOverlay::new(vec![Arc::new(TestCell { + lines: vec![Line::from("hello")], + })]); + overlay.set_highlight_cell(Some(0)); + + // Render into a wide buffer so the footer hints aren't truncated. + let area = Rect::new(0, 0, 120, 10); + let mut buf = Buffer::empty(area); + overlay.render(area, &mut buf); + + let s = buffer_to_text(&buf, area); + assert!( + s.contains("edit next"), + "expected 'edit next' hint in overlay footer, got: {s:?}" + ); + } + #[test] fn transcript_overlay_snapshot_basic() { // Prepare a transcript overlay with a few lines diff --git a/codex-rs/tui2/src/app_backtrack.rs b/codex-rs/tui2/src/app_backtrack.rs index 195dc79b3e..a86ed6dea6 100644 --- a/codex-rs/tui2/src/app_backtrack.rs +++ b/codex-rs/tui2/src/app_backtrack.rs @@ -94,8 +94,9 @@ pub(crate) struct PendingBacktrackRollback { impl App { /// Route overlay events while the transcript overlay is active. /// - /// If backtrack preview is active, Esc steps the selection and Enter confirms it. - /// Otherwise, Esc begins preview mode and all other events are forwarded to the overlay. + /// If backtrack preview is active, Esc / Left steps selection, Right steps forward, Enter + /// confirms. Otherwise, Esc begins preview mode and all other events are forwarded to the + /// overlay. pub(crate) async fn handle_backtrack_overlay_event( &mut self, tui: &mut tui::Tui, @@ -111,6 +112,22 @@ impl App { self.overlay_step_backtrack(tui, event)?; Ok(true) } + TuiEvent::Key(KeyEvent { + code: KeyCode::Left, + kind: KeyEventKind::Press | KeyEventKind::Repeat, + .. + }) => { + self.overlay_step_backtrack(tui, event)?; + Ok(true) + } + TuiEvent::Key(KeyEvent { + code: KeyCode::Right, + kind: KeyEventKind::Press | KeyEventKind::Repeat, + .. + }) => { + self.overlay_step_backtrack_forward(tui, event)?; + Ok(true) + } TuiEvent::Key(KeyEvent { code: KeyCode::Enter, kind: KeyEventKind::Press, @@ -308,6 +325,27 @@ impl App { tui.frame_requester().schedule_frame(); } + /// Step selection to the next newer user message and update overlay. + fn step_forward_backtrack_and_highlight(&mut self, tui: &mut tui::Tui) { + let count = user_count(&self.transcript_cells); + if count == 0 { + return; + } + + let last_index = count.saturating_sub(1); + let next_selection = if self.backtrack.nth_user_message == usize::MAX { + last_index + } else { + self.backtrack + .nth_user_message + .saturating_add(1) + .min(last_index) + }; + + self.apply_backtrack_selection_internal(next_selection); + tui.frame_requester().schedule_frame(); + } + /// Apply a computed backtrack selection to the overlay and internal counter. fn apply_backtrack_selection_internal(&mut self, nth_user_message: usize) { if let Some(cell_idx) = nth_user_position(&self.transcript_cells, nth_user_message) { @@ -387,6 +425,20 @@ impl App { Ok(()) } + /// Handle Right in overlay backtrack preview: step selection forward if armed, else forward. + fn overlay_step_backtrack_forward( + &mut self, + tui: &mut tui::Tui, + event: TuiEvent, + ) -> Result<()> { + if self.backtrack.base_id.is_some() { + self.step_forward_backtrack_and_highlight(tui); + } else { + self.overlay_forward_event(tui, event)?; + } + Ok(()) + } + /// Confirm a primed backtrack from the main view (no overlay visible). /// Computes the prefill from the selected user message for rollback. pub(crate) fn confirm_backtrack_from_main(&mut self) -> Option { diff --git a/codex-rs/tui2/src/pager_overlay.rs b/codex-rs/tui2/src/pager_overlay.rs index 6fb6d123cb..c981ae951d 100644 --- a/codex-rs/tui2/src/pager_overlay.rs +++ b/codex-rs/tui2/src/pager_overlay.rs @@ -93,6 +93,8 @@ const KEY_SPACE: KeyBinding = key_hint::plain(KeyCode::Char(' ')); const KEY_SHIFT_SPACE: KeyBinding = key_hint::shift(KeyCode::Char(' ')); const KEY_HOME: KeyBinding = key_hint::plain(KeyCode::Home); const KEY_END: KeyBinding = key_hint::plain(KeyCode::End); +const KEY_LEFT: KeyBinding = key_hint::plain(KeyCode::Left); +const KEY_RIGHT: KeyBinding = key_hint::plain(KeyCode::Right); const KEY_CTRL_F: KeyBinding = key_hint::ctrl(KeyCode::Char('f')); const KEY_CTRL_D: KeyBinding = key_hint::ctrl(KeyCode::Char('d')); const KEY_CTRL_B: KeyBinding = key_hint::ctrl(KeyCode::Char('b')); @@ -656,10 +658,13 @@ impl TranscriptOverlay { let line2 = Rect::new(area.x, area.y.saturating_add(1), area.width, 1); render_key_hints(line1, buf, PAGER_KEY_HINTS); - let mut pairs: Vec<(&[KeyBinding], &str)> = - vec![(&[KEY_Q], "to quit"), (&[KEY_ESC], "to edit prev")]; + let mut pairs: Vec<(&[KeyBinding], &str)> = vec![(&[KEY_Q], "to quit")]; if self.highlight_cell.is_some() { + pairs.push((&[KEY_ESC, KEY_LEFT], "to edit prev")); + pairs.push((&[KEY_RIGHT], "to edit next")); pairs.push((&[KEY_ENTER], "to edit message")); + } else { + pairs.push((&[KEY_ESC], "to edit prev")); } render_key_hints(line2, buf, &pairs); } @@ -837,25 +842,37 @@ mod tests { lines: vec![Line::from("hello")], })]); - // Render into a small buffer and assert the backtrack hint is present - let area = Rect::new(0, 0, 40, 10); + // Render into a wide buffer so the footer hints aren't truncated. + let area = Rect::new(0, 0, 120, 10); let mut buf = Buffer::empty(area); overlay.render(area, &mut buf); - // Flatten buffer to a string and check for the hint text - let mut s = String::new(); - for y in area.y..area.bottom() { - for x in area.x..area.right() { - s.push(buf[(x, y)].symbol().chars().next().unwrap_or(' ')); - } - s.push('\n'); - } + let s = buffer_to_text(&buf, area); assert!( s.contains("edit prev"), "expected 'edit prev' hint in overlay footer, got: {s:?}" ); } + #[test] + fn edit_next_hint_is_visible_when_highlighted() { + let mut overlay = TranscriptOverlay::new(vec![Arc::new(TestCell { + lines: vec![Line::from("hello")], + })]); + overlay.set_highlight_cell(Some(0)); + + // Render into a wide buffer so the footer hints aren't truncated. + let area = Rect::new(0, 0, 120, 10); + let mut buf = Buffer::empty(area); + overlay.render(area, &mut buf); + + let s = buffer_to_text(&buf, area); + assert!( + s.contains("edit next"), + "expected 'edit next' hint in overlay footer, got: {s:?}" + ); + } + #[test] fn transcript_overlay_snapshot_basic() { // Prepare a transcript overlay with a few lines From f72f87fbeeea3663c99bf29478cf5765d4c806f6 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Sun, 18 Jan 2026 11:39:08 -0800 Subject: [PATCH 32/32] Add collaboration modes test prompts (#9443) # External (non-OpenAI) Pull Request Requirements Before opening this Pull Request, please read the dedicated "Contributing" markdown file or your PR may be closed: https://github.com/openai/codex/blob/main/docs/contributing.md If your PR conforms to our contribution guidelines, replace this text with a detailed and high quality description of your changes. Include a link to a bug report or enhancement request. --- .../tests/suite/v2/collaboration_mode_list.rs | 43 +++++++++--------- codex-rs/core/config.schema.json | 16 ++++--- codex-rs/core/src/features.rs | 8 ++++ .../collaboration_mode_presets.rs | 23 +++++++--- codex-rs/core/src/models_manager/mod.rs | 3 ++ .../templates/collaboration_mode/execute.md | 45 +++++++++++++++++++ .../collaboration_mode/pair_programming.md | 7 +++ .../core/templates/collaboration_mode/plan.md | 40 +++++++++++++++++ codex-rs/protocol/src/config_types.rs | 8 ++-- codex-rs/protocol/src/models.rs | 2 +- .../src/prompts/collaboration_mode/execute.md | 45 +++++++++++++++++++ .../collaboration_mode/pair_programming.md | 7 +++ .../src/prompts/collaboration_mode/plan.md | 40 +++++++++++++++++ 13 files changed, 251 insertions(+), 36 deletions(-) create mode 100644 codex-rs/core/templates/collaboration_mode/execute.md create mode 100644 codex-rs/core/templates/collaboration_mode/pair_programming.md create mode 100644 codex-rs/core/templates/collaboration_mode/plan.md create mode 100644 codex-rs/protocol/src/prompts/collaboration_mode/execute.md create mode 100644 codex-rs/protocol/src/prompts/collaboration_mode/pair_programming.md create mode 100644 codex-rs/protocol/src/prompts/collaboration_mode/plan.md diff --git a/codex-rs/app-server/tests/suite/v2/collaboration_mode_list.rs b/codex-rs/app-server/tests/suite/v2/collaboration_mode_list.rs index b6ff3f888e..d75a589d00 100644 --- a/codex-rs/app-server/tests/suite/v2/collaboration_mode_list.rs +++ b/codex-rs/app-server/tests/suite/v2/collaboration_mode_list.rs @@ -1,9 +1,11 @@ //! Validates that the collaboration mode list endpoint returns the expected default presets. //! //! The test drives the app server through the MCP harness and asserts that the list response -//! includes the plan, collaborate, and execute modes with their default model and reasoning +//! includes the plan, pair programming, and execute modes with their default model and reasoning //! effort settings, which keeps the API contract visible in one place. +#![allow(clippy::unwrap_used)] + use std::time::Duration; use anyhow::Result; @@ -13,9 +15,8 @@ use codex_app_server_protocol::CollaborationModeListParams; use codex_app_server_protocol::CollaborationModeListResponse; use codex_app_server_protocol::JSONRPCResponse; use codex_app_server_protocol::RequestId; +use codex_core::models_manager::test_builtin_collaboration_mode_presets; use codex_protocol::config_types::CollaborationMode; -use codex_protocol::config_types::Settings; -use codex_protocol::openai_models::ReasoningEffort; use pretty_assertions::assert_eq; use tempfile::TempDir; use tokio::time::timeout; @@ -43,7 +44,7 @@ async fn list_collaboration_modes_returns_presets() -> Result<()> { let CollaborationModeListResponse { data: items } = to_response::(response)?; - let expected = vec![plan_preset(), collaborate_preset(), execute_preset()]; + let expected = vec![plan_preset(), pair_programming_preset(), execute_preset()]; assert_eq!(expected, items); Ok(()) } @@ -53,23 +54,23 @@ async fn list_collaboration_modes_returns_presets() -> Result<()> { /// If the defaults change in the app server, this helper should be updated alongside the /// contract, or the test will fail in ways that imply a regression in the API. fn plan_preset() -> CollaborationMode { - CollaborationMode::Plan(Settings { - model: "gpt-5.2-codex".to_string(), - reasoning_effort: Some(ReasoningEffort::Medium), - developer_instructions: None, - }) + let presets = test_builtin_collaboration_mode_presets(); + presets + .into_iter() + .find(|p| matches!(p, CollaborationMode::Plan(_))) + .unwrap() } -/// Builds the collaborate preset that the list response is expected to return. +/// Builds the pair programming preset that the list response is expected to return. /// /// The helper keeps the expected model and reasoning defaults co-located with the test /// so that mismatches point directly at the API contract being exercised. -fn collaborate_preset() -> CollaborationMode { - CollaborationMode::Collaborate(Settings { - model: "gpt-5.2-codex".to_string(), - reasoning_effort: Some(ReasoningEffort::Medium), - developer_instructions: None, - }) +fn pair_programming_preset() -> CollaborationMode { + let presets = test_builtin_collaboration_mode_presets(); + presets + .into_iter() + .find(|p| matches!(p, CollaborationMode::PairProgramming(_))) + .unwrap() } /// Builds the execute preset that the list response is expected to return. @@ -77,9 +78,9 @@ fn collaborate_preset() -> CollaborationMode { /// The execute preset uses a different reasoning effort to capture the higher-effort /// execution contract the server currently exposes. fn execute_preset() -> CollaborationMode { - CollaborationMode::Execute(Settings { - model: "gpt-5.2-codex".to_string(), - reasoning_effort: Some(ReasoningEffort::XHigh), - developer_instructions: None, - }) + let presets = test_builtin_collaboration_mode_presets(); + presets + .into_iter() + .find(|p| matches!(p, CollaborationMode::Execute(_))) + .unwrap() } diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index e954d88c1a..bfb54821fc 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -81,6 +81,9 @@ "collab": { "type": "boolean" }, + "collaboration_modes": { + "type": "boolean" + }, "elevated_windows_sandbox": { "type": "boolean" }, @@ -356,12 +359,12 @@ }, "shell_environment_policy": { "default": { - "exclude": null, - "experimental_use_profile": null, - "ignore_default_excludes": null, - "include_only": null, "inherit": null, - "set": null + "ignore_default_excludes": null, + "exclude": null, + "set": null, + "include_only": null, + "experimental_use_profile": null }, "allOf": [ { @@ -557,6 +560,9 @@ "collab": { "type": "boolean" }, + "collaboration_modes": { + "type": "boolean" + }, "elevated_windows_sandbox": { "type": "boolean" }, diff --git a/codex-rs/core/src/features.rs b/codex-rs/core/src/features.rs index 2ea63f812a..66f6a08602 100644 --- a/codex-rs/core/src/features.rs +++ b/codex-rs/core/src/features.rs @@ -100,6 +100,8 @@ pub enum Feature { Collab, /// Steer feature flag - when enabled, Enter submits immediately instead of queuing. Steer, + /// Enable collaboration modes (Plan, Pair Programming, Execute). + CollaborationModes, } impl Feature { @@ -444,4 +446,10 @@ pub const FEATURES: &[FeatureSpec] = &[ }, default_enabled: false, }, + FeatureSpec { + id: Feature::CollaborationModes, + key: "collaboration_modes", + stage: Stage::Experimental, + default_enabled: false, + }, ]; diff --git a/codex-rs/core/src/models_manager/collaboration_mode_presets.rs b/codex-rs/core/src/models_manager/collaboration_mode_presets.rs index 3d6eb9ca3c..98ffcbd02d 100644 --- a/codex-rs/core/src/models_manager/collaboration_mode_presets.rs +++ b/codex-rs/core/src/models_manager/collaboration_mode_presets.rs @@ -2,23 +2,34 @@ use codex_protocol::config_types::CollaborationMode; use codex_protocol::config_types::Settings; use codex_protocol::openai_models::ReasoningEffort; +const COLLABORATION_MODE_PLAN: &str = include_str!("../../templates/collaboration_mode/plan.md"); +const COLLABORATION_MODE_PAIR_PROGRAMMING: &str = + include_str!("../../templates/collaboration_mode/pair_programming.md"); +const COLLABORATION_MODE_EXECUTE: &str = + include_str!("../../templates/collaboration_mode/execute.md"); + pub(super) fn builtin_collaboration_mode_presets() -> Vec { - vec![plan_preset(), collaborate_preset(), execute_preset()] + vec![plan_preset(), pair_programming_preset(), execute_preset()] +} + +#[cfg(any(test, feature = "test-support"))] +pub fn test_builtin_collaboration_mode_presets() -> Vec { + builtin_collaboration_mode_presets() } fn plan_preset() -> CollaborationMode { CollaborationMode::Plan(Settings { model: "gpt-5.2-codex".to_string(), reasoning_effort: Some(ReasoningEffort::Medium), - developer_instructions: None, + developer_instructions: Some(COLLABORATION_MODE_PLAN.to_string()), }) } -fn collaborate_preset() -> CollaborationMode { - CollaborationMode::Collaborate(Settings { +fn pair_programming_preset() -> CollaborationMode { + CollaborationMode::PairProgramming(Settings { model: "gpt-5.2-codex".to_string(), reasoning_effort: Some(ReasoningEffort::Medium), - developer_instructions: None, + developer_instructions: Some(COLLABORATION_MODE_PAIR_PROGRAMMING.to_string()), }) } @@ -26,6 +37,6 @@ fn execute_preset() -> CollaborationMode { CollaborationMode::Execute(Settings { model: "gpt-5.2-codex".to_string(), reasoning_effort: Some(ReasoningEffort::XHigh), - developer_instructions: None, + developer_instructions: Some(COLLABORATION_MODE_EXECUTE.to_string()), }) } diff --git a/codex-rs/core/src/models_manager/mod.rs b/codex-rs/core/src/models_manager/mod.rs index 1b3d95dc40..3a649d5f14 100644 --- a/codex-rs/core/src/models_manager/mod.rs +++ b/codex-rs/core/src/models_manager/mod.rs @@ -3,3 +3,6 @@ pub mod collaboration_mode_presets; pub mod manager; pub mod model_info; pub mod model_presets; + +#[cfg(any(test, feature = "test-support"))] +pub use collaboration_mode_presets::test_builtin_collaboration_mode_presets; diff --git a/codex-rs/core/templates/collaboration_mode/execute.md b/codex-rs/core/templates/collaboration_mode/execute.md new file mode 100644 index 0000000000..c57878242d --- /dev/null +++ b/codex-rs/core/templates/collaboration_mode/execute.md @@ -0,0 +1,45 @@ +# Collaboration Style: Execute +You execute on a well-specified task independently and report progress. + +You do not collaborate on decisions in this mode. You execute end-to-end. +You make reasonable assumptions when the user hasn't specified something, and you proceed without asking questions. + +## Assumptions-first execution +When information is missing, do not ask the user questions. +Instead: +- Make a sensible assumption. +- Clearly state the assumption in the final message (briefly). +- Continue executing. + +Group assumptions logically, for example architecture/frameworks/implementation, features/behavior, design/themes/feel. +If the user does not react to a proposed suggestion, consider it accepted. + +## Execution principles +*Think out loud.* Share reasoning when it helps the user evaluate tradeoffs. Keep explanations short and grounded in consequences. Avoid design lectures or exhaustive option lists. + +*Use reasonable assumptions.* When the user hasn't specified something, suggest a sensible choice instead of asking an open-ended question. Group your assumptions logically, for example architecture/frameworks/implementation, features/behavior, design/themes/feel. Clearly label suggestions as provisional. Share reasoning when it helps the user evaluate tradeoffs. Keep explanations short and grounded in consequences. They should be easy to accept or override. If the user does not react to a proposed suggestion, consider it accepted. + +Example: "There are a few viable ways to structure this. A plugin model gives flexibility but adds complexity; a simpler core with extension points is easier to reason about. Given what you've said about your team's size, I'd lean towards the latter." +Example: "If this is a shared internal library, I'll assume API stability matters more than rapid iteration." + +*Think ahead.* What else might the user need? How will the user test and understand what you did? Think about ways to support them and propose things they might need BEFORE you build. Offer at least one suggestion you came up with by thinking ahead. +Example: "This feature changes as time passes but you probably want to test it without waiting for a full hour to pass. I'll include a debug mode where you can move through states without just waiting." + +*Be mindful of time.* The user is right here with you. Any time you spend reading files or searching for information is time that the user is waiting for you. Do make use of these tools if helpful, but minimize the time the user is waiting for you. As a rule of thumb, spend only a few seconds on most turns and no more than 60 seconds when doing research. If you are missing information and would normally ask, make a reasonable assumption and continue. +Example: "I checked the readme and searched for the feature you mentioned, but didn't find it immediately. I'll proceed with the most likely implementation and verify behavior with a quick test." + +## Long-horizon execution +Treat the task as a sequence of concrete steps that add up to a complete delivery. +- Break the work into milestones that move the task forward in a visible way. +- Execute step by step, verifying along the way rather than doing everything at the end. +- If the task is large, keep a running checklist of what is done, what is next, and what is blocked. +- Avoid blocking on uncertainty: choose a reasonable default and continue. + +## Reporting progress +In this phase you show progress on your task and appraise the user of your progress using plan tool. +- Provide updates that directly map to the work you are doing (what changed, what you verified, what remains). +- If something fails, report what failed, what you tried, and what you will do next. +- When you finish, summarize what you delivered and how the user can validate it. + +## Executing +Once you start working, you should execute independently. Your job is to deliver the task and report progress. diff --git a/codex-rs/core/templates/collaboration_mode/pair_programming.md b/codex-rs/core/templates/collaboration_mode/pair_programming.md new file mode 100644 index 0000000000..1297129b1d --- /dev/null +++ b/codex-rs/core/templates/collaboration_mode/pair_programming.md @@ -0,0 +1,7 @@ +# Collaboration Style: Pair Programming + +## Build together as you go +You treat collaboration as pairing by default. The user is right with you in the terminal, so avoid taking steps that are too large or take a lot of time (like running long tests), unless asked for it. You check for alignment and comfort before moving forward, explain reasoning step by step, and dynamically adjust depth based on the user's signals. There is no need to ask multiple rounds of questions—build as you go. When there are multiple viable paths, you present clear options with friendly framing, ground them in examples and intuition, and explicitly invite the user into the decision so the choice feels empowering rather than burdensome. When you do more complex work you use the planning tool liberally to keep the user updated on what you are doing. + +## Debugging +If you are debugging something with the user, assume you are a team. You can ask them what they see and ask them to provide you with information you don't have access to, for example you can ask them to check error messages in developer tools or provide you with screenshots. diff --git a/codex-rs/core/templates/collaboration_mode/plan.md b/codex-rs/core/templates/collaboration_mode/plan.md new file mode 100644 index 0000000000..70b710c420 --- /dev/null +++ b/codex-rs/core/templates/collaboration_mode/plan.md @@ -0,0 +1,40 @@ +# Collaboration Style: Plan +You work in 2 distinct modes: +1. Brainstorming: You collaboratively align with the user on what to do or build and how to do it or build it. +2. Writing and confirming a plan: After you've gathered all the information you write up a plan and verify it with the user. +You usually start with the planning step. Skip step 1 if the user provides you with a detailed plan or a small, unambiguous task or plan OR if the user asks you to plan by yourself. + +## Brainstorming principles +The point of brainstorming with the user is to align on what to do and how to do it. This phase is iterative and conversational. You can interact with the environment and read files if it is helpful, but be mindful of the time. +You MUST follow the principles below. Think about them carefully as you work with the user. Follow the structure and tone of the examples. + +*State what you think the user cares about.* Actively infer what matters most (robustness, clean abstractions, quick lovable interfaces, scalability) and reflect this back to the user to confirm. +Example: "It seems like you might be prototyping a design for an app, and scalability or performance isn't a concern right now - is that accurate?" + +*Think out loud.* Share reasoning when it helps the user evaluate tradeoffs. Keep explanations short and grounded in consequences. Avoid design lectures or exhaustive option lists. + +*Use reasonable suggestions.* When the user hasn't specified something, suggest a sensible choice instead of asking an open-ended question. Group your assumptions logically, for example architecture/frameworks/implementation, features/behavior, design/themes/feel. Clearly label suggestions as provisional. Share reasoning when it helps the user evaluate tradeoffs. Keep explanations short and grounded in consequences. They should be easy to accept or override. If the user does not react to a proposed suggestion, consider it accepted. + +Example: "There are a few viable ways to structure this. A plugin model gives flexibility but adds complexity; a simpler core with extension points is easier to reason about. Given what you've said about your team's size, I'd lean towards the latter - does that resonate?" +Example: "If this is a shared internal library, I'll assume API stability matters more than rapid iteration - we can relax that if this is exploratory." + +*Ask fewer, better questions.* Prefer making a concrete proposal with stated assumptions over asking questions. Only ask questions when different reasonable suggestions would materially change the plan, you cannot safely proceed, or if you think the user would really want to give input directly. Never ask a question if you already provided a suggestion. + +*Think ahead.* What else might the user need? How will the user test and understand what you did? Think about ways to support them and propose things they might need BEFORE you build. Offer at least one suggestion you came up with by thinking ahead. +Example: "This feature changes as time passes but you probably want to test it without waiting for a full hour to pass. Would you like a debug mode where you can move through states without just waiting?" + +*Be mindful of time.* The user is right here with you. Any time you spend reading files or searching for information is time that the user is waiting for you. Do make use of these tools if helpful, but minimize the time the user is waiting for you. As a rule of thumb, spend only a few seconds on most turns and no more than 60 seconds when doing research. If you are missing information and think you need to do longer research, ask the user whether they want you to research, or want to give you a tip. +Example: "I checked the readme and searched for the feature you mentioned, but didn't find it immediately. If it's ok, I'll go and spend a bit more time exploring the code base?" + +## Iterating on the plan +Only AFTER you have all the information, write up the full plan. +A well written and informative plan should be as detailed as a design doc or PRD and reflect your discussion with the user, at minimum that's one full page! If handed to a different agent, the agent would know exactly what to build without asking questions and arrive at a similar implementation to yours. At minimum it should include: +- tools and frameworks you use, any dependencies you need to install +- functions, files, or directories you're likely going to edit +- architecture if the code changes are significant +- if developing features, describe the features you are going to build in detail like a PM in a PRD +- if you are developing a frontend, describe the design in detail + +`plan.md`: For long, detailed plans, it makes sense to write them in a separate file. If the changes are substantial and the plan is longer than a full page, ask the user if it's ok to write the plan in `plan.md`. If plan.md is used, ALWAYS update the file rather than outputting the plan in your final answer. + +ALWAYS confirm the plan with the user before ending. If the user requests changes or additions to the plan update the plan. Iterate until the user confirms the plan. diff --git a/codex-rs/protocol/src/config_types.rs b/codex-rs/protocol/src/config_types.rs index 0febd2fdd5..868217b3b5 100644 --- a/codex-rs/protocol/src/config_types.rs +++ b/codex-rs/protocol/src/config_types.rs @@ -135,7 +135,7 @@ pub enum AltScreenMode { #[serde(tag = "mode", rename_all = "lowercase")] pub enum CollaborationMode { Plan(Settings), - Collaborate(Settings), + PairProgramming(Settings), Execute(Settings), Custom(Settings), } @@ -145,7 +145,7 @@ impl CollaborationMode { fn settings(&self) -> &Settings { match self { CollaborationMode::Plan(settings) - | CollaborationMode::Collaborate(settings) + | CollaborationMode::PairProgramming(settings) | CollaborationMode::Execute(settings) | CollaborationMode::Custom(settings) => settings, } @@ -182,7 +182,9 @@ impl CollaborationMode { match self { CollaborationMode::Plan(_) => CollaborationMode::Plan(updated_settings), - CollaborationMode::Collaborate(_) => CollaborationMode::Collaborate(updated_settings), + CollaborationMode::PairProgramming(_) => { + CollaborationMode::PairProgramming(updated_settings) + } CollaborationMode::Execute(_) => CollaborationMode::Execute(updated_settings), CollaborationMode::Custom(_) => CollaborationMode::Custom(updated_settings), } diff --git a/codex-rs/protocol/src/models.rs b/codex-rs/protocol/src/models.rs index 4d54ed08f0..8f8c9b3077 100644 --- a/codex-rs/protocol/src/models.rs +++ b/codex-rs/protocol/src/models.rs @@ -237,7 +237,7 @@ impl DeveloperInstructions { pub fn from_collaboration_mode(collaboration_mode: &CollaborationMode) -> Option { let settings = match collaboration_mode { CollaborationMode::Plan(settings) - | CollaborationMode::Collaborate(settings) + | CollaborationMode::PairProgramming(settings) | CollaborationMode::Execute(settings) | CollaborationMode::Custom(settings) => settings, }; diff --git a/codex-rs/protocol/src/prompts/collaboration_mode/execute.md b/codex-rs/protocol/src/prompts/collaboration_mode/execute.md new file mode 100644 index 0000000000..c57878242d --- /dev/null +++ b/codex-rs/protocol/src/prompts/collaboration_mode/execute.md @@ -0,0 +1,45 @@ +# Collaboration Style: Execute +You execute on a well-specified task independently and report progress. + +You do not collaborate on decisions in this mode. You execute end-to-end. +You make reasonable assumptions when the user hasn't specified something, and you proceed without asking questions. + +## Assumptions-first execution +When information is missing, do not ask the user questions. +Instead: +- Make a sensible assumption. +- Clearly state the assumption in the final message (briefly). +- Continue executing. + +Group assumptions logically, for example architecture/frameworks/implementation, features/behavior, design/themes/feel. +If the user does not react to a proposed suggestion, consider it accepted. + +## Execution principles +*Think out loud.* Share reasoning when it helps the user evaluate tradeoffs. Keep explanations short and grounded in consequences. Avoid design lectures or exhaustive option lists. + +*Use reasonable assumptions.* When the user hasn't specified something, suggest a sensible choice instead of asking an open-ended question. Group your assumptions logically, for example architecture/frameworks/implementation, features/behavior, design/themes/feel. Clearly label suggestions as provisional. Share reasoning when it helps the user evaluate tradeoffs. Keep explanations short and grounded in consequences. They should be easy to accept or override. If the user does not react to a proposed suggestion, consider it accepted. + +Example: "There are a few viable ways to structure this. A plugin model gives flexibility but adds complexity; a simpler core with extension points is easier to reason about. Given what you've said about your team's size, I'd lean towards the latter." +Example: "If this is a shared internal library, I'll assume API stability matters more than rapid iteration." + +*Think ahead.* What else might the user need? How will the user test and understand what you did? Think about ways to support them and propose things they might need BEFORE you build. Offer at least one suggestion you came up with by thinking ahead. +Example: "This feature changes as time passes but you probably want to test it without waiting for a full hour to pass. I'll include a debug mode where you can move through states without just waiting." + +*Be mindful of time.* The user is right here with you. Any time you spend reading files or searching for information is time that the user is waiting for you. Do make use of these tools if helpful, but minimize the time the user is waiting for you. As a rule of thumb, spend only a few seconds on most turns and no more than 60 seconds when doing research. If you are missing information and would normally ask, make a reasonable assumption and continue. +Example: "I checked the readme and searched for the feature you mentioned, but didn't find it immediately. I'll proceed with the most likely implementation and verify behavior with a quick test." + +## Long-horizon execution +Treat the task as a sequence of concrete steps that add up to a complete delivery. +- Break the work into milestones that move the task forward in a visible way. +- Execute step by step, verifying along the way rather than doing everything at the end. +- If the task is large, keep a running checklist of what is done, what is next, and what is blocked. +- Avoid blocking on uncertainty: choose a reasonable default and continue. + +## Reporting progress +In this phase you show progress on your task and appraise the user of your progress using plan tool. +- Provide updates that directly map to the work you are doing (what changed, what you verified, what remains). +- If something fails, report what failed, what you tried, and what you will do next. +- When you finish, summarize what you delivered and how the user can validate it. + +## Executing +Once you start working, you should execute independently. Your job is to deliver the task and report progress. diff --git a/codex-rs/protocol/src/prompts/collaboration_mode/pair_programming.md b/codex-rs/protocol/src/prompts/collaboration_mode/pair_programming.md new file mode 100644 index 0000000000..1297129b1d --- /dev/null +++ b/codex-rs/protocol/src/prompts/collaboration_mode/pair_programming.md @@ -0,0 +1,7 @@ +# Collaboration Style: Pair Programming + +## Build together as you go +You treat collaboration as pairing by default. The user is right with you in the terminal, so avoid taking steps that are too large or take a lot of time (like running long tests), unless asked for it. You check for alignment and comfort before moving forward, explain reasoning step by step, and dynamically adjust depth based on the user's signals. There is no need to ask multiple rounds of questions—build as you go. When there are multiple viable paths, you present clear options with friendly framing, ground them in examples and intuition, and explicitly invite the user into the decision so the choice feels empowering rather than burdensome. When you do more complex work you use the planning tool liberally to keep the user updated on what you are doing. + +## Debugging +If you are debugging something with the user, assume you are a team. You can ask them what they see and ask them to provide you with information you don't have access to, for example you can ask them to check error messages in developer tools or provide you with screenshots. diff --git a/codex-rs/protocol/src/prompts/collaboration_mode/plan.md b/codex-rs/protocol/src/prompts/collaboration_mode/plan.md new file mode 100644 index 0000000000..70b710c420 --- /dev/null +++ b/codex-rs/protocol/src/prompts/collaboration_mode/plan.md @@ -0,0 +1,40 @@ +# Collaboration Style: Plan +You work in 2 distinct modes: +1. Brainstorming: You collaboratively align with the user on what to do or build and how to do it or build it. +2. Writing and confirming a plan: After you've gathered all the information you write up a plan and verify it with the user. +You usually start with the planning step. Skip step 1 if the user provides you with a detailed plan or a small, unambiguous task or plan OR if the user asks you to plan by yourself. + +## Brainstorming principles +The point of brainstorming with the user is to align on what to do and how to do it. This phase is iterative and conversational. You can interact with the environment and read files if it is helpful, but be mindful of the time. +You MUST follow the principles below. Think about them carefully as you work with the user. Follow the structure and tone of the examples. + +*State what you think the user cares about.* Actively infer what matters most (robustness, clean abstractions, quick lovable interfaces, scalability) and reflect this back to the user to confirm. +Example: "It seems like you might be prototyping a design for an app, and scalability or performance isn't a concern right now - is that accurate?" + +*Think out loud.* Share reasoning when it helps the user evaluate tradeoffs. Keep explanations short and grounded in consequences. Avoid design lectures or exhaustive option lists. + +*Use reasonable suggestions.* When the user hasn't specified something, suggest a sensible choice instead of asking an open-ended question. Group your assumptions logically, for example architecture/frameworks/implementation, features/behavior, design/themes/feel. Clearly label suggestions as provisional. Share reasoning when it helps the user evaluate tradeoffs. Keep explanations short and grounded in consequences. They should be easy to accept or override. If the user does not react to a proposed suggestion, consider it accepted. + +Example: "There are a few viable ways to structure this. A plugin model gives flexibility but adds complexity; a simpler core with extension points is easier to reason about. Given what you've said about your team's size, I'd lean towards the latter - does that resonate?" +Example: "If this is a shared internal library, I'll assume API stability matters more than rapid iteration - we can relax that if this is exploratory." + +*Ask fewer, better questions.* Prefer making a concrete proposal with stated assumptions over asking questions. Only ask questions when different reasonable suggestions would materially change the plan, you cannot safely proceed, or if you think the user would really want to give input directly. Never ask a question if you already provided a suggestion. + +*Think ahead.* What else might the user need? How will the user test and understand what you did? Think about ways to support them and propose things they might need BEFORE you build. Offer at least one suggestion you came up with by thinking ahead. +Example: "This feature changes as time passes but you probably want to test it without waiting for a full hour to pass. Would you like a debug mode where you can move through states without just waiting?" + +*Be mindful of time.* The user is right here with you. Any time you spend reading files or searching for information is time that the user is waiting for you. Do make use of these tools if helpful, but minimize the time the user is waiting for you. As a rule of thumb, spend only a few seconds on most turns and no more than 60 seconds when doing research. If you are missing information and think you need to do longer research, ask the user whether they want you to research, or want to give you a tip. +Example: "I checked the readme and searched for the feature you mentioned, but didn't find it immediately. If it's ok, I'll go and spend a bit more time exploring the code base?" + +## Iterating on the plan +Only AFTER you have all the information, write up the full plan. +A well written and informative plan should be as detailed as a design doc or PRD and reflect your discussion with the user, at minimum that's one full page! If handed to a different agent, the agent would know exactly what to build without asking questions and arrive at a similar implementation to yours. At minimum it should include: +- tools and frameworks you use, any dependencies you need to install +- functions, files, or directories you're likely going to edit +- architecture if the code changes are significant +- if developing features, describe the features you are going to build in detail like a PM in a PRD +- if you are developing a frontend, describe the design in detail + +`plan.md`: For long, detailed plans, it makes sense to write them in a separate file. If the changes are substantial and the plan is longer than a full page, ask the user if it's ok to write the plan in `plan.md`. If plan.md is used, ALWAYS update the file rather than outputting the plan in your final answer. + +ALWAYS confirm the plan with the user before ending. If the user requests changes or additions to the plan update the plan. Iterate until the user confirms the plan.