From 79d8f99a7f3df8ce34c03820be8969f9343c71b2 Mon Sep 17 00:00:00 2001 From: Roy Han Date: Wed, 11 Mar 2026 12:07:33 -0700 Subject: [PATCH] initial surface projection for sandbox --- .../src/protocol/thread_history.rs | 91 ++++++++++++-- .../app-server-protocol/src/protocol/v2.rs | 119 ++++++++++++++++-- .../app-server/src/bespoke_event_handling.rs | 29 +++++ .../app-server/src/codex_message_processor.rs | 1 + .../app-server/tests/suite/v2/compaction.rs | 21 +++- .../tests/suite/v2/dynamic_tools.rs | 2 + .../app-server/tests/suite/v2/plan_item.rs | 1 + codex-rs/app-server/tests/suite/v2/review.rs | 4 +- .../tests/suite/v2/thread_resume.rs | 1 + .../app-server/tests/suite/v2/turn_start.rs | 2 + codex-rs/core/src/codex.rs | 3 + codex-rs/core/src/event_mapping.rs | 10 +- codex-rs/protocol/src/items.rs | 40 ++++++ 13 files changed, 297 insertions(+), 27 deletions(-) diff --git a/codex-rs/app-server-protocol/src/protocol/thread_history.rs b/codex-rs/app-server-protocol/src/protocol/thread_history.rs index 38a8c64968..a28215bbd9 100644 --- a/codex-rs/app-server-protocol/src/protocol/thread_history.rs +++ b/codex-rs/app-server-protocol/src/protocol/thread_history.rs @@ -204,7 +204,11 @@ impl ThreadHistoryBuilder { .unwrap_or_else(|| self.new_turn(None)); let id = self.next_item_id(); let content = self.build_user_inputs(payload); - turn.items.push(ThreadItem::UserMessage { id, content }); + turn.items.push(ThreadItem::UserMessage { + id, + content, + metadata: None, + }); self.current_turn = Some(turn); } @@ -214,9 +218,12 @@ impl ThreadHistoryBuilder { } let id = self.next_item_id(); - self.ensure_turn() - .items - .push(ThreadItem::AgentMessage { id, text, phase }); + self.ensure_turn().items.push(ThreadItem::AgentMessage { + id, + text, + phase, + metadata: None, + }); } fn handle_agent_reasoning(&mut self, payload: &AgentReasoningEvent) { @@ -236,6 +243,7 @@ impl ThreadHistoryBuilder { id, summary: vec![payload.text.clone()], content: Vec::new(), + metadata: None, }); } @@ -256,6 +264,7 @@ impl ThreadHistoryBuilder { id, summary: Vec::new(), content: vec![payload.text.clone()], + metadata: None, }); } @@ -304,6 +313,7 @@ impl ThreadHistoryBuilder { id: payload.call_id.clone(), query: String::new(), action: None, + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -313,6 +323,7 @@ impl ThreadHistoryBuilder { id: payload.call_id.clone(), query: payload.query.clone(), action: Some(WebSearchAction::from(payload.action.clone())), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -336,6 +347,7 @@ impl ThreadHistoryBuilder { aggregated_output: None, exit_code: None, duration_ms: None, + metadata: None, }; self.upsert_item_in_turn_id(&payload.turn_id, item); } @@ -366,6 +378,7 @@ impl ThreadHistoryBuilder { aggregated_output, exit_code: Some(payload.exit_code), duration_ms: Some(duration_ms), + metadata: None, }; // Command completions can arrive out of order. Unified exec may return // while a PTY is still running, then emit ExecCommandEnd later from a @@ -380,6 +393,7 @@ impl ThreadHistoryBuilder { id: payload.call_id.clone(), changes: convert_patch_changes(&payload.changes), status: PatchApplyStatus::InProgress, + metadata: None, }; if payload.turn_id.is_empty() { self.upsert_item_in_current_turn(item); @@ -393,6 +407,7 @@ impl ThreadHistoryBuilder { id: payload.call_id.clone(), changes: convert_patch_changes(&payload.changes), status: PatchApplyStatus::InProgress, + metadata: None, }; if payload.turn_id.is_empty() { self.upsert_item_in_current_turn(item); @@ -407,6 +422,7 @@ impl ThreadHistoryBuilder { id: payload.call_id.clone(), changes: convert_patch_changes(&payload.changes), status, + metadata: None, }; if payload.turn_id.is_empty() { self.upsert_item_in_current_turn(item); @@ -427,6 +443,7 @@ impl ThreadHistoryBuilder { content_items: None, success: None, duration_ms: None, + metadata: None, }; if payload.turn_id.is_empty() { self.upsert_item_in_current_turn(item); @@ -450,6 +467,7 @@ impl ThreadHistoryBuilder { content_items: Some(convert_dynamic_tool_content_items(&payload.content_items)), success: Some(payload.success), duration_ms, + metadata: None, }; if payload.turn_id.is_empty() { self.upsert_item_in_current_turn(item); @@ -472,6 +490,7 @@ impl ThreadHistoryBuilder { result: None, error: None, duration_ms: None, + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -511,6 +530,7 @@ impl ThreadHistoryBuilder { result, error, duration_ms, + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -519,6 +539,7 @@ impl ThreadHistoryBuilder { let item = ThreadItem::ImageView { id: payload.call_id.clone(), path: payload.path.to_string_lossy().into_owned(), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -529,6 +550,7 @@ impl ThreadHistoryBuilder { status: String::new(), revised_prompt: None, result: String::new(), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -539,6 +561,7 @@ impl ThreadHistoryBuilder { status: payload.status.clone(), revised_prompt: payload.revised_prompt.clone(), result: payload.result.clone(), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -555,6 +578,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids: Vec::new(), prompt: Some(payload.prompt.clone()), agents_states: HashMap::new(), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -588,6 +612,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids, prompt: Some(payload.prompt.clone()), agents_states, + metadata: None, }); } @@ -603,6 +628,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids: vec![payload.receiver_thread_id.to_string()], prompt: Some(payload.prompt.clone()), agents_states: HashMap::new(), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -625,6 +651,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids: vec![receiver_id.clone()], prompt: Some(payload.prompt.clone()), agents_states: [(receiver_id, received_status)].into_iter().collect(), + metadata: None, }); } @@ -644,6 +671,7 @@ impl ThreadHistoryBuilder { .collect(), prompt: None, agents_states: HashMap::new(), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -677,6 +705,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids, prompt: None, agents_states, + metadata: None, }); } @@ -692,6 +721,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids: vec![payload.receiver_thread_id.to_string()], prompt: None, agents_states: HashMap::new(), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -716,6 +746,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids: vec![receiver_id], prompt: None, agents_states, + metadata: None, }); } @@ -731,6 +762,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids: vec![payload.receiver_thread_id.to_string()], prompt: None, agents_states: HashMap::new(), + metadata: None, }; self.upsert_item_in_current_turn(item); } @@ -758,6 +790,7 @@ impl ThreadHistoryBuilder { receiver_thread_ids: vec![receiver_id], prompt: None, agents_states, + metadata: None, }); } @@ -765,7 +798,7 @@ impl ThreadHistoryBuilder { let id = self.next_item_id(); self.ensure_turn() .items - .push(ThreadItem::ContextCompaction { id }); + .push(ThreadItem::ContextCompaction { id, metadata: None }); } fn handle_entered_review_mode(&mut self, payload: &codex_protocol::protocol::ReviewRequest) { @@ -776,7 +809,11 @@ impl ThreadHistoryBuilder { let id = self.next_item_id(); self.ensure_turn() .items - .push(ThreadItem::EnteredReviewMode { id, review }); + .push(ThreadItem::EnteredReviewMode { + id, + review, + metadata: None, + }); } fn handle_exited_review_mode( @@ -789,9 +826,11 @@ impl ThreadHistoryBuilder { .map(render_review_output_text) .unwrap_or_else(|| REVIEW_FALLBACK_MESSAGE.to_string()); let id = self.next_item_id(); - self.ensure_turn() - .items - .push(ThreadItem::ExitedReviewMode { id, review }); + self.ensure_turn().items.push(ThreadItem::ExitedReviewMode { + id, + review, + metadata: None, + }); } fn handle_error(&mut self, payload: &ErrorEvent) { @@ -1201,6 +1240,7 @@ mod tests { url: "https://example.com/one.png".into(), } ], + metadata: None, } ); assert_eq!( @@ -1209,6 +1249,7 @@ mod tests { id: "item-2".into(), text: "Hi there".into(), phase: None, + metadata: None, } ); assert_eq!( @@ -1217,6 +1258,7 @@ mod tests { id: "item-3".into(), summary: vec!["thinking".into()], content: vec!["full reasoning".into()], + metadata: None, } ); @@ -1232,6 +1274,7 @@ mod tests { text: "Second turn".into(), text_elements: Vec::new(), }], + metadata: None, } ); assert_eq!( @@ -1240,6 +1283,7 @@ mod tests { id: "item-5".into(), text: "Reply two".into(), phase: None, + metadata: None, } ); } @@ -1266,6 +1310,7 @@ mod tests { item: CoreTurnItem::UserMessage(CoreUserMessageItem { id: "user-item-id".to_string(), content: Vec::new(), + metadata: None, }), }), EventMsg::TurnComplete(TurnCompleteEvent { @@ -1289,6 +1334,7 @@ mod tests { text: "hello".into(), text_elements: Vec::new(), }], + metadata: None, } ); } @@ -1312,6 +1358,7 @@ mod tests { id: "item-1".into(), text: "Final reply".into(), phase: Some(MessagePhase::FinalAnswer), + metadata: None, } ); } @@ -1355,6 +1402,7 @@ mod tests { id: "item-2".into(), summary: vec!["first summary".into()], content: vec!["first content".into()], + metadata: None, } ); assert_eq!( @@ -1363,6 +1411,7 @@ mod tests { id: "item-4".into(), summary: vec!["second summary".into()], content: Vec::new(), + metadata: None, } ); } @@ -1414,6 +1463,7 @@ mod tests { text: "Please do the thing".into(), text_elements: Vec::new(), }], + metadata: None, } ); assert_eq!( @@ -1422,6 +1472,7 @@ mod tests { id: "item-2".into(), text: "Working...".into(), phase: None, + metadata: None, } ); @@ -1436,6 +1487,7 @@ mod tests { text: "Let's try again".into(), text_elements: Vec::new(), }], + metadata: None, } ); assert_eq!( @@ -1444,6 +1496,7 @@ mod tests { id: "item-4".into(), text: "Second attempt complete.".into(), phase: None, + metadata: None, } ); } @@ -1504,11 +1557,13 @@ mod tests { text: "First".into(), text_elements: Vec::new(), }], + metadata: None, }, ThreadItem::AgentMessage { id: "item-2".into(), text: "A1".into(), phase: None, + metadata: None, }, ] ); @@ -1521,11 +1576,13 @@ mod tests { text: "Third".into(), text_elements: Vec::new(), }], + metadata: None, }, ThreadItem::AgentMessage { id: "item-4".into(), text: "A3".into(), phase: None, + metadata: None, }, ] ); @@ -1607,6 +1664,7 @@ mod tests { text: "Start".into(), text_elements: Vec::new(), }], + metadata: None, }, ThreadItem::UserMessage { id: "item-2".into(), @@ -1614,6 +1672,7 @@ mod tests { text: "Steer".into(), text_elements: Vec::new(), }], + metadata: None, }, ] ); @@ -1688,6 +1747,7 @@ mod tests { query: Some("codex".into()), queries: None, }), + metadata: None, } ); assert_eq!( @@ -1704,6 +1764,7 @@ mod tests { aggregated_output: Some("hello world\n".into()), exit_code: Some(0), duration_ms: Some(12), + metadata: None, } ); assert_eq!( @@ -1719,6 +1780,7 @@ mod tests { message: "boom".into(), }), duration_ms: Some(8), + metadata: None, } ); } @@ -1778,6 +1840,7 @@ mod tests { }]), success: Some(true), duration_ms: Some(42), + metadata: None, } ); } @@ -1852,6 +1915,7 @@ mod tests { aggregated_output: Some("exec command rejected by user".into()), exit_code: Some(-1), duration_ms: Some(0), + metadata: None, } ); assert_eq!( @@ -1864,6 +1928,7 @@ mod tests { diff: "hello\n".into(), }], status: PatchApplyStatus::Declined, + metadata: None, } ); } @@ -1946,6 +2011,7 @@ mod tests { aggregated_output: Some("done\n".into()), exit_code: Some(0), duration_ms: Some(5), + metadata: None, } ); } @@ -2022,6 +2088,7 @@ mod tests { text: "second".into(), text_elements: Vec::new(), }], + metadata: None, } ); } @@ -2075,6 +2142,7 @@ mod tests { text: "apply patch".into(), text_elements: Vec::new(), }], + metadata: None, }, ThreadItem::FileChange { id: "patch-call".into(), @@ -2084,6 +2152,7 @@ mod tests { diff: "hello\n".into(), }], status: PatchApplyStatus::InProgress, + metadata: None, }, ] ); @@ -2139,6 +2208,7 @@ mod tests { text: "apply patch".into(), text_elements: Vec::new(), }], + metadata: None, }, ThreadItem::FileChange { id: "patch-call".into(), @@ -2148,6 +2218,7 @@ mod tests { diff: "hello\n".into(), }], status: PatchApplyStatus::InProgress, + metadata: None, }, ] ); @@ -2334,6 +2405,7 @@ mod tests { )] .into_iter() .collect(), + metadata: None, } ); } @@ -2409,6 +2481,7 @@ mod tests { text: "hello".into(), text_elements: Vec::new(), }], + metadata: None, }], } ); diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index aaf0484a58..2d12f07547 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -25,7 +25,9 @@ use codex_protocol::config_types::Verbosity; use codex_protocol::config_types::WebSearchMode; use codex_protocol::config_types::WebSearchToolConfig; use codex_protocol::items::AgentMessageContent as CoreAgentMessageContent; +use codex_protocol::items::ItemSandboxPolicy as CoreItemSandboxPolicy; use codex_protocol::items::TurnItem as CoreTurnItem; +use codex_protocol::items::TurnItemMetadata as CoreTurnItemMetadata; use codex_protocol::mcp::Resource as McpResource; use codex_protocol::mcp::ResourceTemplate as McpResourceTemplate; use codex_protocol::mcp::Tool as McpTool; @@ -3794,6 +3796,41 @@ pub enum UserInput { }, } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case", export_to = "v2/")] +pub enum ItemSandboxPolicy { + ReadOnly, + Sandbox, + FullAccess, +} + +impl From for ItemSandboxPolicy { + fn from(value: CoreItemSandboxPolicy) -> Self { + match value { + CoreItemSandboxPolicy::ReadOnly => ItemSandboxPolicy::ReadOnly, + CoreItemSandboxPolicy::Sandbox => ItemSandboxPolicy::Sandbox, + CoreItemSandboxPolicy::FullAccess => ItemSandboxPolicy::FullAccess, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[ts(export_to = "v2/")] +pub struct ItemMetadata { + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub sandbox_policy: Option, +} + +impl From for ItemMetadata { + fn from(value: CoreTurnItemMetadata) -> Self { + Self { + sandbox_policy: value.sandbox_policy.map(Into::into), + } + } +} + impl UserInput { pub fn into_core(self) -> CoreUserInput { match self { @@ -3850,7 +3887,12 @@ impl UserInput { pub enum ThreadItem { #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] - UserMessage { id: String, content: Vec }, + UserMessage { + id: String, + content: Vec, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, + }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] AgentMessage { @@ -3858,12 +3900,19 @@ pub enum ThreadItem { text: String, #[serde(default)] phase: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] /// EXPERIMENTAL - proposed plan item content. The completed plan item is /// authoritative and may not match the concatenation of `PlanDelta` text. - Plan { id: String, text: String }, + Plan { + id: String, + text: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, + }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] Reasoning { @@ -3872,6 +3921,8 @@ pub enum ThreadItem { summary: Vec, #[serde(default)] content: Vec, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] @@ -3895,6 +3946,8 @@ pub enum ThreadItem { /// The duration of the command execution in milliseconds. #[ts(type = "number | null")] duration_ms: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] @@ -3902,6 +3955,8 @@ pub enum ThreadItem { id: String, changes: Vec, status: PatchApplyStatus, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] @@ -3916,6 +3971,8 @@ pub enum ThreadItem { /// The duration of the MCP tool call in milliseconds. #[ts(type = "number | null")] duration_ms: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] @@ -3929,6 +3986,8 @@ pub enum ThreadItem { /// The duration of the dynamic tool call in milliseconds. #[ts(type = "number | null")] duration_ms: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] @@ -3948,6 +4007,8 @@ pub enum ThreadItem { prompt: Option, /// Last known status of the target agents, when available. agents_states: HashMap, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] @@ -3955,10 +4016,17 @@ pub enum ThreadItem { id: String, query: String, action: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] - ImageView { id: String, path: String }, + ImageView { + id: String, + path: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, + }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] ImageGeneration { @@ -3966,16 +4034,32 @@ pub enum ThreadItem { status: String, revised_prompt: Option, result: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] - EnteredReviewMode { id: String, review: String }, + EnteredReviewMode { + id: String, + review: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, + }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] - ExitedReviewMode { id: String, review: String }, + ExitedReviewMode { + id: String, + review: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, + }, #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase")] - ContextCompaction { id: String }, + ContextCompaction { + id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + metadata: Option, + }, } impl ThreadItem { @@ -4043,6 +4127,7 @@ impl From for ThreadItem { CoreTurnItem::UserMessage(user) => ThreadItem::UserMessage { id: user.id, content: user.content.into_iter().map(UserInput::from).collect(), + metadata: user.metadata.map(Into::into), }, CoreTurnItem::AgentMessage(agent) => { let text = agent @@ -4056,31 +4141,37 @@ impl From for ThreadItem { id: agent.id, text, phase: agent.phase, + metadata: agent.metadata.map(Into::into), } } CoreTurnItem::Plan(plan) => ThreadItem::Plan { id: plan.id, text: plan.text, + metadata: plan.metadata.map(Into::into), }, CoreTurnItem::Reasoning(reasoning) => ThreadItem::Reasoning { id: reasoning.id, summary: reasoning.summary_text, content: reasoning.raw_content, + metadata: reasoning.metadata.map(Into::into), }, CoreTurnItem::WebSearch(search) => ThreadItem::WebSearch { id: search.id, query: search.query, action: Some(WebSearchAction::from(search.action)), + metadata: search.metadata.map(Into::into), }, CoreTurnItem::ImageGeneration(image) => ThreadItem::ImageGeneration { id: image.id, status: image.status, revised_prompt: image.revised_prompt, result: image.result, + metadata: image.metadata.map(Into::into), + }, + CoreTurnItem::ContextCompaction(compaction) => ThreadItem::ContextCompaction { + id: compaction.id, + metadata: compaction.metadata.map(Into::into), }, - CoreTurnItem::ContextCompaction(compaction) => { - ThreadItem::ContextCompaction { id: compaction.id } - } } } } @@ -6807,6 +6898,7 @@ mod tests { path: "app://demo-app".to_string(), }, ], + metadata: None, }); assert_eq!( @@ -6833,6 +6925,7 @@ mod tests { path: "app://demo-app".to_string(), }, ], + metadata: None, } ); @@ -6847,6 +6940,7 @@ mod tests { }, ], phase: None, + metadata: None, }); assert_eq!( @@ -6855,6 +6949,7 @@ mod tests { id: "agent-1".to_string(), text: "Hello world".to_string(), phase: None, + metadata: None, } ); @@ -6864,6 +6959,7 @@ mod tests { text: "final".to_string(), }], phase: Some(MessagePhase::FinalAnswer), + metadata: None, }); assert_eq!( @@ -6872,6 +6968,7 @@ mod tests { id: "agent-2".to_string(), text: "final".to_string(), phase: Some(MessagePhase::FinalAnswer), + metadata: None, } ); @@ -6879,6 +6976,7 @@ mod tests { id: "reasoning-1".to_string(), summary_text: vec!["line one".to_string(), "line two".to_string()], raw_content: vec![], + metadata: None, }); assert_eq!( @@ -6887,6 +6985,7 @@ mod tests { id: "reasoning-1".to_string(), summary: vec!["line one".to_string(), "line two".to_string()], content: vec![], + metadata: None, } ); @@ -6897,6 +6996,7 @@ mod tests { query: Some("docs".to_string()), queries: None, }, + metadata: None, }); assert_eq!( @@ -6908,6 +7008,7 @@ mod tests { query: Some("docs".to_string()), queries: None, }), + metadata: None, } ); } diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index 620f397af9..0ee4683e37 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -389,6 +389,7 @@ pub(crate) async fn apply_bespoke_event_handling( id: item_id.clone(), changes: patch_changes.clone(), status: PatchApplyStatus::InProgress, + metadata: None, }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -750,6 +751,7 @@ pub(crate) async fn apply_bespoke_event_handling( content_items: None, success: None, duration_ms: None, + metadata: None, }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -820,6 +822,7 @@ pub(crate) async fn apply_bespoke_event_handling( ), success: Some(response.success), duration_ms, + metadata: None, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -863,6 +866,7 @@ pub(crate) async fn apply_bespoke_event_handling( receiver_thread_ids: Vec::new(), prompt: Some(begin_event.prompt), agents_states: HashMap::new(), + metadata: None, }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -900,6 +904,7 @@ pub(crate) async fn apply_bespoke_event_handling( receiver_thread_ids, prompt: Some(end_event.prompt), agents_states, + metadata: None, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -920,6 +925,7 @@ pub(crate) async fn apply_bespoke_event_handling( receiver_thread_ids, prompt: Some(begin_event.prompt), agents_states: HashMap::new(), + metadata: None, }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -946,6 +952,7 @@ pub(crate) async fn apply_bespoke_event_handling( receiver_thread_ids: vec![receiver_id.clone()], prompt: Some(end_event.prompt), agents_states: [(receiver_id, received_status)].into_iter().collect(), + metadata: None, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -970,6 +977,7 @@ pub(crate) async fn apply_bespoke_event_handling( receiver_thread_ids, prompt: None, agents_states: HashMap::new(), + metadata: None, }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -1006,6 +1014,7 @@ pub(crate) async fn apply_bespoke_event_handling( receiver_thread_ids, prompt: None, agents_states, + metadata: None, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -1025,6 +1034,7 @@ pub(crate) async fn apply_bespoke_event_handling( receiver_thread_ids: vec![begin_event.receiver_thread_id.to_string()], prompt: None, agents_states: HashMap::new(), + metadata: None, }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -1065,6 +1075,7 @@ pub(crate) async fn apply_bespoke_event_handling( receiver_thread_ids: vec![receiver_id], prompt: None, agents_states, + metadata: None, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -1247,6 +1258,7 @@ pub(crate) async fn apply_bespoke_event_handling( let item = ThreadItem::ImageView { id: view_image_event.call_id.clone(), path: view_image_event.path.to_string_lossy().into_owned(), + metadata: None, }; let started = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -1272,6 +1284,7 @@ pub(crate) async fn apply_bespoke_event_handling( let item = ThreadItem::EnteredReviewMode { id: event_turn_id.clone(), review, + metadata: None, }; let started = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -1344,6 +1357,7 @@ pub(crate) async fn apply_bespoke_event_handling( let item = ThreadItem::ExitedReviewMode { id: event_turn_id.clone(), review, + metadata: None, }; let started = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -1390,6 +1404,7 @@ pub(crate) async fn apply_bespoke_event_handling( id: item_id.clone(), changes, status: PatchApplyStatus::InProgress, + metadata: None, }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -1448,6 +1463,7 @@ pub(crate) async fn apply_bespoke_event_handling( aggregated_output: None, exit_code: None, duration_ms: None, + metadata: None, }; let notification = ItemStartedNotification { thread_id: conversation_id.to_string(), @@ -1557,6 +1573,7 @@ pub(crate) async fn apply_bespoke_event_handling( aggregated_output, exit_code: Some(exit_code), duration_ms: Some(duration_ms), + metadata: None, }; let notification = ItemCompletedNotification { @@ -1796,6 +1813,7 @@ async fn complete_file_change_item( id: item_id, changes, status, + metadata: None, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -1829,6 +1847,7 @@ async fn complete_command_execution_item( aggregated_output: None, exit_code: None, duration_ms: None, + metadata: None, }; let notification = ItemCompletedNotification { thread_id: conversation_id.to_string(), @@ -2516,6 +2535,7 @@ fn collab_resume_begin_item( receiver_thread_ids: vec![begin_event.receiver_thread_id.to_string()], prompt: None, agents_states: HashMap::new(), + metadata: None, } } @@ -2540,6 +2560,7 @@ fn collab_resume_end_item(end_event: codex_protocol::protocol::CollabResumeEndEv receiver_thread_ids: vec![receiver_id], prompt: None, agents_states, + metadata: None, } } @@ -2558,6 +2579,7 @@ async fn construct_mcp_tool_call_notification( result: None, error: None, duration_ms: None, + metadata: None, }; ItemStartedNotification { thread_id, @@ -2604,6 +2626,7 @@ async fn construct_mcp_tool_call_end_notification( result, error, duration_ms, + metadata: None, }; ItemCompletedNotification { thread_id, @@ -2912,6 +2935,7 @@ mod tests { receiver_thread_ids: vec![event.receiver_thread_id.to_string()], prompt: None, agents_states: HashMap::new(), + metadata: None, }; assert_eq!(item, expected); } @@ -2942,6 +2966,7 @@ mod tests { )] .into_iter() .collect(), + metadata: None, }; assert_eq!(item, expected); } @@ -3305,6 +3330,7 @@ mod tests { result: None, error: None, duration_ms: None, + metadata: None, }, }; @@ -3441,6 +3467,7 @@ mod tests { result: None, error: None, duration_ms: None, + metadata: None, }, }; @@ -3495,6 +3522,7 @@ mod tests { }), error: None, duration_ms: Some(0), + metadata: None, }, }; @@ -3537,6 +3565,7 @@ mod tests { message: "boom".to_string(), }), duration_ms: Some(1), + metadata: None, }, }; diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index a4909c7745..f988b827e6 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -6109,6 +6109,7 @@ impl CodexMessageProcessor { // Review prompt display text is synthesized; no UI element ranges to preserve. text_elements: Vec::new(), }], + metadata: None, }] }; diff --git a/codex-rs/app-server/tests/suite/v2/compaction.rs b/codex-rs/app-server/tests/suite/v2/compaction.rs index 5b5faa02d6..09528c6a32 100644 --- a/codex-rs/app-server/tests/suite/v2/compaction.rs +++ b/codex-rs/app-server/tests/suite/v2/compaction.rs @@ -88,10 +88,13 @@ async fn auto_compaction_local_emits_started_and_completed_items() -> Result<()> let started = wait_for_context_compaction_started(&mut mcp).await?; let completed = wait_for_context_compaction_completed(&mut mcp).await?; - let ThreadItem::ContextCompaction { id: started_id } = started.item else { + let ThreadItem::ContextCompaction { id: started_id, .. } = started.item else { unreachable!("started item should be context compaction"); }; - let ThreadItem::ContextCompaction { id: completed_id } = completed.item else { + let ThreadItem::ContextCompaction { + id: completed_id, .. + } = completed.item + else { unreachable!("completed item should be context compaction"); }; @@ -177,10 +180,13 @@ async fn auto_compaction_remote_emits_started_and_completed_items() -> Result<() let started = wait_for_context_compaction_started(&mut mcp).await?; let completed = wait_for_context_compaction_completed(&mut mcp).await?; - let ThreadItem::ContextCompaction { id: started_id } = started.item else { + let ThreadItem::ContextCompaction { id: started_id, .. } = started.item else { unreachable!("started item should be context compaction"); }; - let ThreadItem::ContextCompaction { id: completed_id } = completed.item else { + let ThreadItem::ContextCompaction { + id: completed_id, .. + } = completed.item + else { unreachable!("completed item should be context compaction"); }; @@ -240,10 +246,13 @@ async fn thread_compact_start_triggers_compaction_and_returns_empty_response() - let started = wait_for_context_compaction_started(&mut mcp).await?; let completed = wait_for_context_compaction_completed(&mut mcp).await?; - let ThreadItem::ContextCompaction { id: started_id } = started.item else { + let ThreadItem::ContextCompaction { id: started_id, .. } = started.item else { unreachable!("started item should be context compaction"); }; - let ThreadItem::ContextCompaction { id: completed_id } = completed.item else { + let ThreadItem::ContextCompaction { + id: completed_id, .. + } = completed.item + else { unreachable!("completed item should be context compaction"); }; diff --git a/codex-rs/app-server/tests/suite/v2/dynamic_tools.rs b/codex-rs/app-server/tests/suite/v2/dynamic_tools.rs index 338593c465..a759ad815d 100644 --- a/codex-rs/app-server/tests/suite/v2/dynamic_tools.rs +++ b/codex-rs/app-server/tests/suite/v2/dynamic_tools.rs @@ -200,6 +200,7 @@ async fn dynamic_tool_call_round_trip_sends_text_content_items_to_model() -> Res content_items, success, duration_ms, + .. } = started.item else { panic!("expected dynamic tool call item"); @@ -253,6 +254,7 @@ async fn dynamic_tool_call_round_trip_sends_text_content_items_to_model() -> Res content_items, success, duration_ms, + .. } = completed.item else { panic!("expected dynamic tool call item"); diff --git a/codex-rs/app-server/tests/suite/v2/plan_item.rs b/codex-rs/app-server/tests/suite/v2/plan_item.rs index 58471f434f..181a22d33b 100644 --- a/codex-rs/app-server/tests/suite/v2/plan_item.rs +++ b/codex-rs/app-server/tests/suite/v2/plan_item.rs @@ -67,6 +67,7 @@ async fn plan_mode_uses_proposed_plan_block_for_plan_item() -> Result<()> { let expected_plan = ThreadItem::Plan { id: format!("{}-plan", turn.id), text: "# Final plan\n- first\n- second\n".to_string(), + metadata: None, }; let expected_plan_id = format!("{}-plan", turn.id); let streamed_plan = plan_deltas diff --git a/codex-rs/app-server/tests/suite/v2/review.rs b/codex-rs/app-server/tests/suite/v2/review.rs index 6febbaf52a..4c2ee889c4 100644 --- a/codex-rs/app-server/tests/suite/v2/review.rs +++ b/codex-rs/app-server/tests/suite/v2/review.rs @@ -97,7 +97,7 @@ async fn review_start_runs_review_turn_and_emits_code_review_item() -> Result<() let started: ItemStartedNotification = serde_json::from_value(item_started.params.expect("params must be present"))?; match started.item { - ThreadItem::EnteredReviewMode { id, review } => { + ThreadItem::EnteredReviewMode { id, review, .. } => { assert_eq!(id, turn_id); assert_eq!(review, "commit 1234567: Tidy UI colors"); saw_entered_review_mode = true; @@ -123,7 +123,7 @@ async fn review_start_runs_review_turn_and_emits_code_review_item() -> Result<() let completed: ItemCompletedNotification = serde_json::from_value(review_notif.params.expect("params must be present"))?; match completed.item { - ThreadItem::ExitedReviewMode { id, review } => { + ThreadItem::ExitedReviewMode { id, review, .. } => { assert_eq!(id, turn_id); review_body = Some(review); break; diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs index 12a74ed6e6..64e857fe43 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_resume.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs @@ -1238,6 +1238,7 @@ async fn thread_resume_replays_pending_file_change_request_approval() -> Result< diff: "new line\n".to_string(), }], status: PatchApplyStatus::InProgress, + metadata: None, }; assert_eq!(original_started, expected_file_change); 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 15b309ddf4..3cccc197d2 100644 --- a/codex-rs/app-server/tests/suite/v2/turn_start.rs +++ b/codex-rs/app-server/tests/suite/v2/turn_start.rs @@ -1548,6 +1548,7 @@ async fn turn_start_file_change_approval_v2() -> Result<()> { ref id, status, ref changes, + .. } = started_file_change else { unreachable!("loop ensures we break on file change items"); @@ -1928,6 +1929,7 @@ async fn turn_start_file_change_approval_decline_v2() -> Result<()> { ref id, status, ref changes, + .. } = started_file_change else { unreachable!("loop ensures we break on file change items"); diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index b2bbae4a78..64fb0d0ad4 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -6419,6 +6419,7 @@ impl ProposedPlanItemState { let item = TurnItem::Plan(PlanItem { id: self.item_id.clone(), text: String::new(), + metadata: None, }); sess.emit_turn_item_started(turn_context, &item).await; } @@ -6453,6 +6454,7 @@ impl ProposedPlanItemState { let item = TurnItem::Plan(PlanItem { id: self.item_id.clone(), text, + metadata: None, }); sess.emit_turn_item_completed(turn_context, item).await; } @@ -6765,6 +6767,7 @@ async fn emit_agent_message_in_plan_mode( id: agent_message_id.clone(), content: Vec::new(), phase: None, + metadata: None, }) }); sess.emit_turn_item_started(turn_context, &start_item).await; diff --git a/codex-rs/core/src/event_mapping.rs b/codex-rs/core/src/event_mapping.rs index 09f1235718..ab1f88f2cd 100644 --- a/codex-rs/core/src/event_mapping.rs +++ b/codex-rs/core/src/event_mapping.rs @@ -83,7 +83,12 @@ fn parse_agent_message( } } let id = id.cloned().unwrap_or_else(|| Uuid::new_v4().to_string()); - AgentMessageItem { id, content, phase } + AgentMessageItem { + id, + content, + phase, + metadata: None, + } } pub fn parse_turn_item(item: &ResponseItem) -> Option { @@ -129,6 +134,7 @@ pub fn parse_turn_item(item: &ResponseItem) -> Option { id: id.clone(), summary_text, raw_content, + metadata: None, })) } ResponseItem::WebSearchCall { id, action, .. } => { @@ -140,6 +146,7 @@ pub fn parse_turn_item(item: &ResponseItem) -> Option { id: id.clone().unwrap_or_default(), query, action, + metadata: None, })) } ResponseItem::ImageGenerationCall { @@ -154,6 +161,7 @@ pub fn parse_turn_item(item: &ResponseItem) -> Option { revised_prompt: revised_prompt.clone(), result: result.clone(), saved_path: None, + metadata: None, }, )), _ => None, diff --git a/codex-rs/protocol/src/items.rs b/codex-rs/protocol/src/items.rs index f200fe6f75..d72b5bcf25 100644 --- a/codex-rs/protocol/src/items.rs +++ b/codex-rs/protocol/src/items.rs @@ -29,10 +29,29 @@ pub enum TurnItem { ContextCompaction(ContextCompactionItem), } +#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case")] +pub enum ItemSandboxPolicy { + ReadOnly, + Sandbox, + FullAccess, +} + +#[derive(Debug, Clone, Default, Deserialize, Serialize, TS, JsonSchema, PartialEq, Eq)] +pub struct TurnItemMetadata { + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub sandbox_policy: Option, +} + #[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema)] pub struct UserMessageItem { pub id: String, pub content: Vec, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub metadata: Option, } #[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema)] @@ -58,12 +77,18 @@ pub struct AgentMessageItem { #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] pub phase: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub metadata: Option, } #[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema)] pub struct PlanItem { pub id: String, pub text: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub metadata: Option, } #[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema)] @@ -72,6 +97,9 @@ pub struct ReasoningItem { pub summary_text: Vec, #[serde(default)] pub raw_content: Vec, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub metadata: Option, } #[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)] @@ -79,6 +107,9 @@ pub struct WebSearchItem { pub id: String, pub query: String, pub action: WebSearchAction, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub metadata: Option, } #[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)] @@ -92,17 +123,24 @@ pub struct ImageGenerationItem { #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] pub saved_path: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub metadata: Option, } #[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema)] pub struct ContextCompactionItem { pub id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub metadata: Option, } impl ContextCompactionItem { pub fn new() -> Self { Self { id: uuid::Uuid::new_v4().to_string(), + metadata: None, } } @@ -122,6 +160,7 @@ impl UserMessageItem { Self { id: uuid::Uuid::new_v4().to_string(), content: content.to_vec(), + metadata: None, } } @@ -201,6 +240,7 @@ impl AgentMessageItem { id: uuid::Uuid::new_v4().to_string(), content: content.to_vec(), phase: None, + metadata: None, } }