diff --git a/codex-rs/app-server-protocol/schema/json/ClientRequest.json b/codex-rs/app-server-protocol/schema/json/ClientRequest.json index 6d9be4ec02..a6d05dfccc 100644 --- a/codex-rs/app-server-protocol/schema/json/ClientRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ClientRequest.json @@ -2112,8 +2112,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "phase": { "anyOf": [ @@ -2155,6 +2154,12 @@ }, "type": "array" }, + "id": { + "type": [ + "string", + "null" + ] + }, "recipient": { "type": "string" }, @@ -2193,6 +2198,9 @@ "null" ] }, + "id": { + "type": "string" + }, "summary": { "items": { "$ref": "#/definitions/ReasoningItemReasoningSummary" @@ -2231,8 +2239,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "$ref": "#/definitions/LocalShellStatus" @@ -2265,8 +2272,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "name": { "type": "string" @@ -2310,8 +2316,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -2340,6 +2345,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "output": { "$ref": "#/definitions/FunctionCallOutputBody" }, @@ -2368,8 +2379,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "input": { "type": "string" @@ -2405,6 +2415,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "name": { "type": [ "string", @@ -2441,6 +2457,12 @@ "execution": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "status": { "type": "string" }, @@ -2481,8 +2503,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -2530,7 +2551,6 @@ } }, "required": [ - "id", "result", "status", "type" @@ -2543,6 +2563,12 @@ "encrypted_content": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "type": { "enum": [ "compaction" diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 1873ed317e..6b050fe1b3 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -14182,8 +14182,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "phase": { "anyOf": [ @@ -14225,6 +14224,12 @@ }, "type": "array" }, + "id": { + "type": [ + "string", + "null" + ] + }, "recipient": { "type": "string" }, @@ -14263,6 +14268,9 @@ "null" ] }, + "id": { + "type": "string" + }, "summary": { "items": { "$ref": "#/definitions/v2/ReasoningItemReasoningSummary" @@ -14301,8 +14309,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "$ref": "#/definitions/v2/LocalShellStatus" @@ -14335,8 +14342,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "name": { "type": "string" @@ -14380,8 +14386,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -14410,6 +14415,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "output": { "$ref": "#/definitions/v2/FunctionCallOutputBody" }, @@ -14438,8 +14449,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "input": { "type": "string" @@ -14475,6 +14485,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "name": { "type": [ "string", @@ -14511,6 +14527,12 @@ "execution": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "status": { "type": "string" }, @@ -14551,8 +14573,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -14600,7 +14621,6 @@ } }, "required": [ - "id", "result", "status", "type" @@ -14613,6 +14633,12 @@ "encrypted_content": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "type": { "enum": [ "compaction" diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index 38c74b1859..c36662f4a8 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -10684,8 +10684,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "phase": { "anyOf": [ @@ -10727,6 +10726,12 @@ }, "type": "array" }, + "id": { + "type": [ + "string", + "null" + ] + }, "recipient": { "type": "string" }, @@ -10765,6 +10770,9 @@ "null" ] }, + "id": { + "type": "string" + }, "summary": { "items": { "$ref": "#/definitions/ReasoningItemReasoningSummary" @@ -10803,8 +10811,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "$ref": "#/definitions/LocalShellStatus" @@ -10837,8 +10844,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "name": { "type": "string" @@ -10882,8 +10888,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -10912,6 +10917,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "output": { "$ref": "#/definitions/FunctionCallOutputBody" }, @@ -10940,8 +10951,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "input": { "type": "string" @@ -10977,6 +10987,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "name": { "type": [ "string", @@ -11013,6 +11029,12 @@ "execution": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "status": { "type": "string" }, @@ -11053,8 +11075,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -11102,7 +11123,6 @@ } }, "required": [ - "id", "result", "status", "type" @@ -11115,6 +11135,12 @@ "encrypted_content": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "type": { "enum": [ "compaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/RawResponseItemCompletedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/RawResponseItemCompletedNotification.json index c18f9aeb15..3431590934 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/RawResponseItemCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/RawResponseItemCompletedNotification.json @@ -361,8 +361,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "phase": { "anyOf": [ @@ -404,6 +403,12 @@ }, "type": "array" }, + "id": { + "type": [ + "string", + "null" + ] + }, "recipient": { "type": "string" }, @@ -442,6 +447,9 @@ "null" ] }, + "id": { + "type": "string" + }, "summary": { "items": { "$ref": "#/definitions/ReasoningItemReasoningSummary" @@ -480,8 +488,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "$ref": "#/definitions/LocalShellStatus" @@ -514,8 +521,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "name": { "type": "string" @@ -559,8 +565,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -589,6 +594,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "output": { "$ref": "#/definitions/FunctionCallOutputBody" }, @@ -617,8 +628,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "input": { "type": "string" @@ -654,6 +664,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "name": { "type": [ "string", @@ -690,6 +706,12 @@ "execution": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "status": { "type": "string" }, @@ -730,8 +752,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -779,7 +800,6 @@ } }, "required": [ - "id", "result", "status", "type" @@ -792,6 +812,12 @@ "encrypted_content": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "type": { "enum": [ "compaction" diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeParams.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeParams.json index dc5ef5cdaa..72cf3ae8d6 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeParams.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeParams.json @@ -432,8 +432,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "phase": { "anyOf": [ @@ -475,6 +474,12 @@ }, "type": "array" }, + "id": { + "type": [ + "string", + "null" + ] + }, "recipient": { "type": "string" }, @@ -513,6 +518,9 @@ "null" ] }, + "id": { + "type": "string" + }, "summary": { "items": { "$ref": "#/definitions/ReasoningItemReasoningSummary" @@ -551,8 +559,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "$ref": "#/definitions/LocalShellStatus" @@ -585,8 +592,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "name": { "type": "string" @@ -630,8 +636,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -660,6 +665,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "output": { "$ref": "#/definitions/FunctionCallOutputBody" }, @@ -688,8 +699,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "input": { "type": "string" @@ -725,6 +735,12 @@ "call_id": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "name": { "type": [ "string", @@ -761,6 +777,12 @@ "execution": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "status": { "type": "string" }, @@ -801,8 +823,7 @@ "type": [ "string", "null" - ], - "writeOnly": true + ] }, "status": { "type": [ @@ -850,7 +871,6 @@ } }, "required": [ - "id", "result", "status", "type" @@ -863,6 +883,12 @@ "encrypted_content": { "type": "string" }, + "id": { + "type": [ + "string", + "null" + ] + }, "type": { "enum": [ "compaction" diff --git a/codex-rs/app-server-protocol/schema/typescript/ResponseItem.ts b/codex-rs/app-server-protocol/schema/typescript/ResponseItem.ts index b90963db80..4f36f5971b 100644 --- a/codex-rs/app-server-protocol/schema/typescript/ResponseItem.ts +++ b/codex-rs/app-server-protocol/schema/typescript/ResponseItem.ts @@ -11,8 +11,12 @@ import type { ReasoningItemContent } from "./ReasoningItemContent"; import type { ReasoningItemReasoningSummary } from "./ReasoningItemReasoningSummary"; import type { WebSearchAction } from "./WebSearchAction"; -export type ResponseItem = { "type": "message", role: string, content: Array, phase?: MessagePhase, } | { "type": "agent_message", author: string, recipient: string, content: Array, } | { "type": "reasoning", summary: Array, content?: Array, encrypted_content: string | null, } | { "type": "local_shell_call", +export type ResponseItem = { "type": "message", id?: string, role: string, content: Array, phase?: MessagePhase, } | { "type": "agent_message", id?: string, author: string, recipient: string, content: Array, } | { "type": "reasoning", id?: string, summary: Array, content?: Array, encrypted_content: string | null, } | { "type": "local_shell_call", +/** + * Legacy id field retained for compatibility with older payloads. + */ +id?: string, /** * Set when using the Responses API. */ -call_id: string | null, status: LocalShellStatus, action: LocalShellAction, } | { "type": "function_call", name: string, namespace?: string, arguments: string, call_id: string, } | { "type": "tool_search_call", call_id: string | null, status?: string, execution: string, arguments: unknown, } | { "type": "function_call_output", call_id: string, output: FunctionCallOutputBody, } | { "type": "custom_tool_call", status?: string, call_id: string, name: string, input: string, } | { "type": "custom_tool_call_output", call_id: string, name?: string, output: FunctionCallOutputBody, } | { "type": "tool_search_output", call_id: string | null, status: string, execution: string, tools: unknown[], } | { "type": "web_search_call", status?: string, action?: WebSearchAction, } | { "type": "image_generation_call", id: string, status: string, revised_prompt?: string, result: string, } | { "type": "compaction", encrypted_content: string, } | { "type": "compaction_trigger" } | { "type": "context_compaction", encrypted_content?: string, } | { "type": "other" }; +call_id: string | null, status: LocalShellStatus, action: LocalShellAction, } | { "type": "function_call", id?: string, name: string, namespace?: string, arguments: string, call_id: string, } | { "type": "tool_search_call", id?: string, call_id: string | null, status?: string, execution: string, arguments: unknown, } | { "type": "function_call_output", id?: string, call_id: string, output: FunctionCallOutputBody, } | { "type": "custom_tool_call", id?: string, status?: string, call_id: string, name: string, input: string, } | { "type": "custom_tool_call_output", id?: string, call_id: string, name?: string, output: FunctionCallOutputBody, } | { "type": "tool_search_output", id?: string, call_id: string | null, status: string, execution: string, tools: unknown[], } | { "type": "web_search_call", id?: string, status?: string, action?: WebSearchAction, } | { "type": "image_generation_call", id?: string, status: string, revised_prompt?: string, result: string, } | { "type": "compaction", id?: string, encrypted_content: string, } | { "type": "compaction_trigger" } | { "type": "context_compaction", encrypted_content?: string, } | { "type": "other" }; diff --git a/codex-rs/codex-api/src/endpoint/compact.rs b/codex-rs/codex-api/src/endpoint/compact.rs index ebcf1a5722..f08b9650de 100644 --- a/codex-rs/codex-api/src/endpoint/compact.rs +++ b/codex-rs/codex-api/src/endpoint/compact.rs @@ -3,7 +3,8 @@ use crate::common::CompactionInput; use crate::endpoint::session::EndpointSession; use crate::error::ApiError; use crate::provider::Provider; -use crate::requests::attach_stateless_response_item_ids; +use crate::requests::ResponseItemIdPolicy; +use crate::requests::apply_response_item_id_policy; use codex_client::HttpTransport; use codex_client::RequestTelemetry; use codex_protocol::models::ResponseItem; @@ -67,9 +68,12 @@ impl CompactClient { ) -> Result, ApiError> { let mut body = to_value(input) .map_err(|e| ApiError::Stream(format!("failed to encode compaction input: {e}")))?; - if include_item_ids_for_stateless_mode { - attach_stateless_response_item_ids(&mut body, input.input); - } + let item_id_policy = if include_item_ids_for_stateless_mode { + ResponseItemIdPolicy::KeepAll + } else { + ResponseItemIdPolicy::OmitAll + }; + apply_response_item_id_policy(&mut body, input.input, item_id_policy); self.compact(body, extra_headers, request_timeout).await } } diff --git a/codex-rs/codex-api/src/endpoint/responses.rs b/codex-rs/codex-api/src/endpoint/responses.rs index 7b5f3f6137..2575d1ebb1 100644 --- a/codex-rs/codex-api/src/endpoint/responses.rs +++ b/codex-rs/codex-api/src/endpoint/responses.rs @@ -5,8 +5,8 @@ use crate::endpoint::session::EndpointSession; use crate::error::ApiError; use crate::provider::Provider; use crate::requests::Compression; -use crate::requests::attach_stateful_response_item_ids; -use crate::requests::attach_stateless_response_item_ids; +use crate::requests::ResponseItemIdPolicy; +use crate::requests::apply_response_item_id_policy; use crate::requests::headers::build_session_headers; use crate::requests::headers::insert_header; use crate::requests::headers::subagent_header; @@ -87,11 +87,14 @@ impl ResponsesClient { let mut body = serde_json::to_value(&request) .map_err(|e| ApiError::Stream(format!("failed to encode responses request: {e}")))?; let is_azure_responses_endpoint = self.session.provider().is_azure_responses_endpoint(); - if is_azure_responses_endpoint && request.store { - attach_stateful_response_item_ids(&mut body, &request.input); + let item_id_policy = if is_azure_responses_endpoint && request.store { + ResponseItemIdPolicy::KeepStatefulStoredOnly } else if !is_azure_responses_endpoint && include_item_ids_for_stateless_mode { - attach_stateless_response_item_ids(&mut body, &request.input); - } + ResponseItemIdPolicy::KeepAll + } else { + ResponseItemIdPolicy::OmitAll + }; + apply_response_item_id_policy(&mut body, &request.input, item_id_policy); let mut headers = extra_headers; if let Some(ref thread_id) = thread_id { diff --git a/codex-rs/codex-api/src/endpoint/responses_websocket.rs b/codex-rs/codex-api/src/endpoint/responses_websocket.rs index a9339b9678..07591ae889 100644 --- a/codex-rs/codex-api/src/endpoint/responses_websocket.rs +++ b/codex-rs/codex-api/src/endpoint/responses_websocket.rs @@ -5,7 +5,8 @@ use crate::common::ResponsesWsRequest; use crate::error::ApiError; use crate::provider::Provider; use crate::rate_limits::parse_rate_limit_event; -use crate::requests::attach_stateless_response_item_ids; +use crate::requests::ResponseItemIdPolicy; +use crate::requests::apply_response_item_id_policy; use crate::sse::ResponsesStreamEvent; use crate::sse::process_responses_event; use crate::telemetry::WebsocketTelemetry; @@ -229,10 +230,13 @@ impl ResponsesWebsocketConnection { let mut request_body = serde_json::to_value(&request).map_err(|err| { ApiError::Stream(format!("failed to encode websocket request: {err}")) })?; - if include_item_ids_for_stateless_mode { - let ResponsesWsRequest::ResponseCreate(request) = &request; - attach_stateless_response_item_ids(&mut request_body, &request.input); - } + let ResponsesWsRequest::ResponseCreate(request) = &request; + let item_id_policy = if include_item_ids_for_stateless_mode { + ResponseItemIdPolicy::KeepAll + } else { + ResponseItemIdPolicy::OmitAll + }; + apply_response_item_id_policy(&mut request_body, &request.input, item_id_policy); let current_span = Span::current(); tokio::spawn( diff --git a/codex-rs/codex-api/src/endpoint/search.rs b/codex-rs/codex-api/src/endpoint/search.rs index 143048c954..8f5b5aa40a 100644 --- a/codex-rs/codex-api/src/endpoint/search.rs +++ b/codex-rs/codex-api/src/endpoint/search.rs @@ -2,6 +2,9 @@ use crate::auth::SharedAuthProvider; use crate::endpoint::session::EndpointSession; use crate::error::ApiError; use crate::provider::Provider; +use crate::requests::ResponseItemIdPolicy; +use crate::requests::apply_response_item_id_policy; +use crate::search::SearchInput; use crate::search::SearchRequest; use crate::search::SearchResponse; use codex_client::HttpTransport; @@ -37,8 +40,11 @@ impl SearchClient { request: &SearchRequest, extra_headers: HeaderMap, ) -> Result { - let body = to_value(request) + let mut body = to_value(request) .map_err(|e| ApiError::Stream(format!("failed to encode search request: {e}")))?; + if let Some(SearchInput::Items(input)) = &request.input { + apply_response_item_id_policy(&mut body, input, ResponseItemIdPolicy::OmitAll); + } let resp = self .session .execute(Method::POST, Self::path(), extra_headers, Some(body)) diff --git a/codex-rs/codex-api/src/requests/mod.rs b/codex-rs/codex-api/src/requests/mod.rs index 8bd0d915f8..fdbbce1cad 100644 --- a/codex-rs/codex-api/src/requests/mod.rs +++ b/codex-rs/codex-api/src/requests/mod.rs @@ -2,5 +2,5 @@ pub(crate) mod headers; pub(crate) mod responses; pub use responses::Compression; -pub(crate) use responses::attach_stateful_response_item_ids; -pub(crate) use responses::attach_stateless_response_item_ids; +pub(crate) use responses::ResponseItemIdPolicy; +pub(crate) use responses::apply_response_item_id_policy; diff --git a/codex-rs/codex-api/src/requests/responses.rs b/codex-rs/codex-api/src/requests/responses.rs index 1a888b3add..527aa5958d 100644 --- a/codex-rs/codex-api/src/requests/responses.rs +++ b/codex-rs/codex-api/src/requests/responses.rs @@ -8,59 +8,57 @@ pub enum Compression { Zstd, } -/// Reattaches the item IDs historically sent with stored/stateful Responses requests. -/// -/// `ResponseItem` IDs are skipped by serde so request modes can choose their attachment policy -/// after serializing the common request body. Stateful requests use the legacy narrower policy: -/// attach IDs for prompt items the stateful Responses path may treat as existing stored items, -/// but do not opt every Codex-owned output item into the newer stateless stable-ID behavior. -pub(crate) fn attach_stateful_response_item_ids( - payload_json: &mut Value, - original_items: &[ResponseItem], -) { - let Some(input_value) = payload_json.get_mut("input") else { - return; - }; - let Value::Array(items) = input_value else { - return; - }; - - for (value, item) in items.iter_mut().zip(original_items.iter()) { - if let ResponseItem::Reasoning { id, .. } - | ResponseItem::Message { id: Some(id), .. } - | ResponseItem::WebSearchCall { id: Some(id), .. } - | ResponseItem::FunctionCall { id: Some(id), .. } - | ResponseItem::ToolSearchCall { id: Some(id), .. } - | ResponseItem::LocalShellCall { id: Some(id), .. } - | ResponseItem::CustomToolCall { id: Some(id), .. } = item - { - if id.is_empty() { - continue; - } - - if let Some(obj) = value.as_object_mut() { - obj.insert("id".to_string(), Value::String(id.clone())); - } - } - } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum ResponseItemIdPolicy { + KeepAll, + KeepStatefulStoredOnly, + OmitAll, } -/// Reattaches every non-empty stable item ID before sending a stateless Responses request. +/// Applies the request-specific Responses API item ID policy after common serialization. /// -/// Stateless requests resend the full prompt history by value, so the server only sees stable -/// item identity when Codex explicitly puts each known ID back into the serialized `input` array. -/// This includes Codex-generated IDs for new local prompt items and IDs preserved from server -/// output, compaction, and rollout replay. -pub(crate) fn attach_stateless_response_item_ids( +/// `ResponseItem` normally serializes every known ID so rollout, app-server, and other internal +/// JSON surfaces preserve item identity. For now, Responses API request modes are the exception +/// to preserve historical behavior: stateless OpenAI requests keep every item ID, whereas +/// stateful Responses API usage keep only the narrower legacy set (only item IDs returned by the +/// server). We can simplify this in the future and always send all IDs once we validate that +/// doing so is safe for stateful usage of Responses API (i.e. Azure). +pub(crate) fn apply_response_item_id_policy( payload_json: &mut Value, original_items: &[ResponseItem], + policy: ResponseItemIdPolicy, ) { let Some(Value::Array(items)) = payload_json.get_mut("input") else { return; }; for (value, item) in items.iter_mut().zip(original_items.iter()) { - item.attach_id_to_json(value); + if !should_keep_response_item_id(item, policy) + && let Some(obj) = value.as_object_mut() + { + obj.remove("id"); + } + } +} + +fn should_keep_response_item_id(item: &ResponseItem, policy: ResponseItemIdPolicy) -> bool { + if !item.id().is_some_and(|id| !id.is_empty()) { + return false; + } + + match policy { + ResponseItemIdPolicy::KeepAll => true, + ResponseItemIdPolicy::KeepStatefulStoredOnly => matches!( + item, + ResponseItem::Reasoning { .. } + | ResponseItem::Message { .. } + | ResponseItem::WebSearchCall { .. } + | ResponseItem::FunctionCall { .. } + | ResponseItem::ToolSearchCall { .. } + | ResponseItem::LocalShellCall { .. } + | ResponseItem::CustomToolCall { .. } + ), + ResponseItemIdPolicy::OmitAll => false, } } @@ -70,7 +68,7 @@ mod tests { use pretty_assertions::assert_eq; #[test] - fn attaches_stateless_response_item_ids_to_input_json() { + fn keeps_all_response_item_ids_in_input_json() { let items = serde_json::from_str::>( r#"[ {"type":"message","id":"msg_1","role":"user","content":[]}, @@ -93,7 +91,7 @@ mod tests { "input": serde_json::to_value(&items).expect("serialize input"), }); - attach_stateless_response_item_ids(&mut payload, &items); + apply_response_item_id_policy(&mut payload, &items, ResponseItemIdPolicy::KeepAll); let ids = payload["input"] .as_array() @@ -111,9 +109,9 @@ mod tests { } #[test] - fn attach_stateless_response_item_ids_leaves_missing_ids_unchanged() { + fn omits_response_item_ids_without_request_opt_in() { let items = vec![ResponseItem::Message { - id: None, + id: Some("msg_1".to_string()), role: "user".to_string(), content: Vec::new(), phase: None, @@ -122,7 +120,7 @@ mod tests { "input": serde_json::to_value(&items).expect("serialize input"), }); - attach_stateless_response_item_ids(&mut payload, &items); + apply_response_item_id_policy(&mut payload, &items, ResponseItemIdPolicy::OmitAll); assert_eq!(payload["input"][0].get("id"), None); } diff --git a/codex-rs/core/tests/suite/client.rs b/codex-rs/core/tests/suite/client.rs index fb242c4dae..4ab8a17b6b 100644 --- a/codex-rs/core/tests/suite/client.rs +++ b/codex-rs/core/tests/suite/client.rs @@ -297,8 +297,7 @@ async fn resume_includes_initial_messages_and_sends_prior_items() { }], phase: None, }; - let mut prior_user_json = serde_json::to_value(&prior_user).unwrap(); - prior_user.attach_id_to_json(&mut prior_user_json); + let prior_user_json = serde_json::to_value(&prior_user).unwrap(); writeln!( f, "{}", @@ -319,8 +318,7 @@ async fn resume_includes_initial_messages_and_sends_prior_items() { }], phase: None, }; - let mut prior_system_json = serde_json::to_value(&prior_system).unwrap(); - prior_system.attach_id_to_json(&mut prior_system_json); + let prior_system_json = serde_json::to_value(&prior_system).unwrap(); writeln!( f, "{}", @@ -341,8 +339,7 @@ async fn resume_includes_initial_messages_and_sends_prior_items() { }], phase: Some(MessagePhase::Commentary), }; - let mut prior_item_json = serde_json::to_value(&prior_item).unwrap(); - prior_item.attach_id_to_json(&mut prior_item_json); + let prior_item_json = serde_json::to_value(&prior_item).unwrap(); writeln!( f, "{}", diff --git a/codex-rs/memories/write/src/phase1.rs b/codex-rs/memories/write/src/phase1.rs index 4a157718e9..e3668466b6 100644 --- a/codex-rs/memories/write/src/phase1.rs +++ b/codex-rs/memories/write/src/phase1.rs @@ -420,13 +420,14 @@ mod job { fn sanitize_response_item_for_memories(item: &ResponseItem) -> Option { let ResponseItem::Message { - id, + id: _, role, content, phase, } = item else { - return should_persist_response_item_for_memories(item).then(|| item.clone()); + return should_persist_response_item_for_memories(item) + .then(|| item.clone().without_id()); }; if role == "developer" { @@ -434,7 +435,7 @@ mod job { } if role != "user" { - return Some(item.clone()); + return Some(item.clone().without_id()); } let content = content @@ -447,7 +448,7 @@ mod job { } Some(ResponseItem::Message { - id: id.clone(), + id: None, role: role.clone(), content, phase: phase.clone(), diff --git a/codex-rs/protocol/src/models.rs b/codex-rs/protocol/src/models.rs index 8659a62a6c..84177c98b9 100644 --- a/codex-rs/protocol/src/models.rs +++ b/codex-rs/protocol/src/models.rs @@ -754,8 +754,8 @@ pub enum MessagePhase { #[serde(tag = "type", rename_all = "snake_case")] pub enum ResponseItem { Message { - #[serde(default, skip_serializing)] - #[ts(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, role: String, content: Vec, @@ -767,18 +767,15 @@ pub enum ResponseItem { phase: Option, }, AgentMessage { - #[serde(default, skip_serializing)] - #[ts(skip)] - #[schemars(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, author: String, recipient: String, content: Vec, }, Reasoning { - #[serde(default, skip_serializing)] - #[ts(skip)] - #[schemars(skip)] + #[serde(default, skip_serializing_if = "String::is_empty")] id: String, summary: Vec, #[serde(default, skip_serializing_if = "should_serialize_reasoning_content")] @@ -788,8 +785,8 @@ pub enum ResponseItem { }, LocalShellCall { /// Legacy id field retained for compatibility with older payloads. - #[serde(default, skip_serializing)] - #[ts(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, /// Set when using the Responses API. call_id: Option, @@ -797,8 +794,8 @@ pub enum ResponseItem { action: LocalShellAction, }, FunctionCall { - #[serde(default, skip_serializing)] - #[ts(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, name: String, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -811,8 +808,8 @@ pub enum ResponseItem { call_id: String, }, ToolSearchCall { - #[serde(default, skip_serializing)] - #[ts(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, call_id: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -828,9 +825,8 @@ pub enum ResponseItem { // - an array of structured content items (`content_items`) // We keep this behavior centralized in `FunctionCallOutputPayload`. FunctionCallOutput { - #[serde(default, skip_serializing)] - #[ts(skip)] - #[schemars(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, call_id: String, #[ts(as = "FunctionCallOutputBody")] @@ -838,8 +834,8 @@ pub enum ResponseItem { output: FunctionCallOutputPayload, }, CustomToolCall { - #[serde(default, skip_serializing)] - #[ts(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] @@ -853,9 +849,8 @@ pub enum ResponseItem { // `function_call_output.output` so freeform tools can return either plain // text or structured content items. CustomToolCallOutput { - #[serde(default, skip_serializing)] - #[ts(skip)] - #[schemars(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, call_id: String, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -866,9 +861,8 @@ pub enum ResponseItem { output: FunctionCallOutputPayload, }, ToolSearchOutput { - #[serde(default, skip_serializing)] - #[ts(skip)] - #[schemars(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, call_id: Option, status: String, @@ -885,8 +879,8 @@ pub enum ResponseItem { // "action": {"type":"search","query":"weather: San Francisco, CA"} // } WebSearchCall { - #[serde(default, skip_serializing)] - #[ts(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] @@ -905,6 +899,7 @@ pub enum ResponseItem { // "result":"..." // } ImageGenerationCall { + #[serde(default, skip_serializing_if = "String::is_empty")] id: String, status: String, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -914,9 +909,8 @@ pub enum ResponseItem { }, #[serde(alias = "compaction_summary")] Compaction { - #[serde(default, skip_serializing)] - #[ts(skip)] - #[schemars(skip)] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] id: Option, encrypted_content: String, }, @@ -1039,14 +1033,23 @@ impl ResponseItem { } } - pub fn attach_id_to_json(&self, value: &mut Value) { - let Some(id) = self.id().filter(|id| !id.is_empty()) else { - return; - }; - let Some(obj) = value.as_object_mut() else { - return; - }; - obj.insert("id".to_string(), Value::String(id.to_string())); + pub fn without_id(mut self) -> Self { + match &mut self { + Self::Message { id, .. } + | Self::AgentMessage { id, .. } + | Self::LocalShellCall { id, .. } + | Self::FunctionCall { id, .. } + | Self::ToolSearchCall { id, .. } + | Self::FunctionCallOutput { id, .. } + | Self::CustomToolCall { id, .. } + | Self::CustomToolCallOutput { id, .. } + | Self::ToolSearchOutput { id, .. } + | Self::WebSearchCall { id, .. } + | Self::Compaction { id, .. } => *id = None, + Self::Reasoning { id, .. } | Self::ImageGenerationCall { id, .. } => id.clear(), + Self::CompactionTrigger | Self::ContextCompaction { .. } | Self::Other => {} + } + self } /// Returns whether this item is an ordinary user-role message. @@ -1055,12 +1058,6 @@ impl ResponseItem { } } -pub(crate) fn attach_all_response_item_ids(values: &mut [Value], items: &[ResponseItem]) { - for (value, item) in values.iter_mut().zip(items) { - item.attach_id_to_json(value); - } -} - pub const BASE_INSTRUCTIONS_DEFAULT: &str = include_str!("prompts/base_instructions/default.md"); /// Base instructions for the model in a thread. Corresponds to the `instructions` field in the ResponsesAPI. @@ -2732,7 +2729,6 @@ mod tests { queries: Some(vec!["weather seattle".into(), "seattle weather now".into()]), }), Some("completed".into()), - true, ), ( r#"{ @@ -2748,7 +2744,6 @@ mod tests { url: Some("https://example.com".into()), }), Some("open".into()), - true, ), ( r#"{ @@ -2766,7 +2761,6 @@ mod tests { pattern: Some("installation".into()), }), Some("in_progress".into()), - true, ), ( r#"{ @@ -2777,12 +2771,10 @@ mod tests { Some("ws_partial".into()), None, Some("in_progress".into()), - false, ), ]; - for (json_literal, expected_id, expected_action, expected_status, expect_roundtrip) in cases - { + for (json_literal, expected_id, expected_action, expected_status) in cases { let parsed: ResponseItem = serde_json::from_str(json_literal)?; let expected = ResponseItem::WebSearchCall { id: expected_id.clone(), @@ -2792,10 +2784,7 @@ mod tests { assert_eq!(parsed, expected); let serialized = serde_json::to_value(&parsed)?; - let mut expected_serialized: serde_json::Value = serde_json::from_str(json_literal)?; - if !expect_roundtrip && let Some(obj) = expected_serialized.as_object_mut() { - obj.remove("id"); - } + let expected_serialized: serde_json::Value = serde_json::from_str(json_literal)?; assert_eq!(serialized, expected_serialized); } diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index 8230b07fee..a92fe7cb94 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -43,7 +43,6 @@ use crate::models::ResponseInputItem; use crate::models::ResponseItem; use crate::models::SandboxEnforcement; use crate::models::WebSearchAction; -use crate::models::attach_all_response_item_ids; use crate::num_format::format_with_separators; use crate::openai_models::ReasoningEffort as ReasoningEffortConfig; use crate::parse_command::ParsedCommand; @@ -2865,34 +2864,6 @@ pub enum RolloutItem { EventMsg(EventMsg), } -impl RolloutItem { - pub fn attach_response_item_ids_to_json(&self, value: &mut Value) { - let Some(payload) = value.get_mut("payload") else { - return; - }; - - match self { - Self::ResponseItem(item) => item.attach_id_to_json(payload), - Self::Compacted(CompactedItem { - replacement_history: Some(items), - .. - }) => { - let Some(Value::Array(values)) = payload.get_mut("replacement_history") else { - return; - }; - attach_all_response_item_ids(values, items); - } - Self::SessionMeta(_) - | Self::Compacted(CompactedItem { - replacement_history: None, - .. - }) - | Self::TurnContext(_) - | Self::EventMsg(_) => {} - } - } -} - #[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, TS)] pub struct CompactedItem { pub message: String, diff --git a/codex-rs/rollout/src/recorder.rs b/codex-rs/rollout/src/recorder.rs index 08dbbce63d..bb8a04bdf6 100644 --- a/codex-rs/rollout/src/recorder.rs +++ b/codex-rs/rollout/src/recorder.rs @@ -1682,9 +1682,7 @@ impl JsonlWriter { timestamp, item: rollout_item, }; - let mut value = serde_json::to_value(line)?; - rollout_item.attach_response_item_ids_to_json(&mut value); - self.write_line(&value).await + self.write_line(&line).await } async fn write_line(&mut self, item: &impl serde::Serialize) -> std::io::Result<()> { let mut json = serde_json::to_string(item)?;