[codex] Add optional IDs to response items (#28812)

## Why

`ResponseItem` variants do not have a consistent internal ID shape: some
variants carry required IDs, some carry optional IDs, and some cannot
represent an ID at all. The existing fields also use inconsistent serde,
TypeScript, and JSON-schema annotations. A single enum-level access path
is needed before history recording can assign and retain IDs.

This PR establishes that internal model only. It intentionally does not
generate or serialize IDs; allocation and wire persistence are isolated
in the stacked follow-up.

## What changed

- Give every concrete `ResponseItem` variant an `Option<String>` ID
field.
- Apply the same internal-only annotations to every ID field:
`#[serde(default, skip_serializing)]`, `#[ts(skip)]`, and
`#[schemars(skip)]`.
- Add `ResponseItem::id()` and `ResponseItem::set_id()` as the shared
accessors.
- Preserve IDs when history items are rewritten for truncation.
- Adapt consumers that previously assumed reasoning and image-generation
IDs were required.
- Regenerate app-server schemas so the hidden fields are represented
consistently.

The serde catch-all `ResponseItem::Other` remains ID-less because it
must remain a unit variant.

## Test plan

- `cargo check --tests -p codex-core -p codex-api -p codex-rollout-trace
-p codex-image-generation-extension`
- `just test -p codex-protocol`
- `just test -p codex-app-server-protocol`
- `just test -p codex-api -p codex-rollout-trace -p
codex-image-generation-extension`
- `just test -p codex-core event_mapping`
This commit is contained in:
pakrym-oai
2026-06-17 18:27:43 -07:00
committed by GitHub
Unverified
parent c274a83f8b
commit dbd2857f4b
36 changed files with 278 additions and 258 deletions
@@ -339,20 +339,24 @@ impl ContextManager {
let policy_with_serialization_budget = policy * 1.2;
match item {
ResponseItem::FunctionCallOutput {
id,
call_id,
output,
metadata,
} => ResponseItem::FunctionCallOutput {
id: id.clone(),
call_id: call_id.clone(),
output: truncate_function_output_payload(output, policy_with_serialization_budget),
metadata: metadata.clone(),
},
ResponseItem::CustomToolCallOutput {
id,
call_id,
name,
output,
metadata,
} => ResponseItem::CustomToolCallOutput {
id: id.clone(),
call_id: call_id.clone(),
name: name.clone(),
output: truncate_function_output_payload(output, policy_with_serialization_budget),
@@ -154,6 +154,7 @@ fn reference_context_item() -> TurnContextItem {
fn custom_tool_call_output(call_id: &str, output: &str) -> ResponseItem {
ResponseItem::CustomToolCallOutput {
id: None,
call_id: call_id.to_string(),
name: None,
output: FunctionCallOutputPayload::from_text(output.to_string()),
@@ -163,7 +164,7 @@ fn custom_tool_call_output(call_id: &str, output: &str) -> ResponseItem {
fn reasoning_msg(text: &str) -> ResponseItem {
ResponseItem::Reasoning {
id: String::new(),
id: None,
summary: vec![ReasoningItemReasoningSummary::SummaryText {
text: "summary".to_string(),
}],
@@ -177,7 +178,7 @@ fn reasoning_msg(text: &str) -> ResponseItem {
fn reasoning_with_encrypted_content(len: usize) -> ResponseItem {
ResponseItem::Reasoning {
id: String::new(),
id: None,
summary: vec![ReasoningItemReasoningSummary::SummaryText {
text: "summary".to_string(),
}],
@@ -222,7 +223,7 @@ fn filters_non_api_messages() {
items,
vec![
ResponseItem::Reasoning {
id: String::new(),
id: None,
summary: vec![ReasoningItemReasoningSummary::SummaryText {
text: "summary".to_string(),
}],
@@ -409,6 +410,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::InputText {
@@ -430,6 +432,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() {
metadata: None,
},
ResponseItem::CustomToolCallOutput {
id: None,
call_id: "tool-1".to_string(),
name: None,
output: FunctionCallOutputPayload::from_content_items(vec![
@@ -476,6 +479,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::InputText {
@@ -497,6 +501,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() {
metadata: None,
},
ResponseItem::CustomToolCallOutput {
id: None,
call_id: "tool-1".to_string(),
name: None,
output: FunctionCallOutputPayload::from_content_items(vec![
@@ -544,7 +549,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() {
fn for_prompt_preserves_image_generation_calls_when_images_are_supported() {
let history = create_history_with_items(vec![
ResponseItem::ImageGenerationCall {
id: "ig_123".to_string(),
id: Some("ig_123".to_string()),
status: "generating".to_string(),
revised_prompt: Some("lobster".to_string()),
result: "Zm9v".to_string(),
@@ -565,7 +570,7 @@ fn for_prompt_preserves_image_generation_calls_when_images_are_supported() {
history.for_prompt(&default_input_modalities()),
vec![
ResponseItem::ImageGenerationCall {
id: "ig_123".to_string(),
id: Some("ig_123".to_string()),
status: "generating".to_string(),
revised_prompt: Some("lobster".to_string()),
result: "Zm9v".to_string(),
@@ -597,7 +602,7 @@ fn for_prompt_clears_image_generation_result_when_images_are_unsupported() {
metadata: None,
},
ResponseItem::ImageGenerationCall {
id: "ig_123".to_string(),
id: Some("ig_123".to_string()),
status: "completed".to_string(),
revised_prompt: Some("lobster".to_string()),
result: "Zm9v".to_string(),
@@ -618,7 +623,7 @@ fn for_prompt_clears_image_generation_result_when_images_are_unsupported() {
metadata: None,
},
ResponseItem::ImageGenerationCall {
id: "ig_123".to_string(),
id: Some("ig_123".to_string()),
status: "completed".to_string(),
revised_prompt: Some("lobster".to_string()),
result: String::new(),
@@ -662,6 +667,7 @@ fn remove_first_item_removes_matching_output_for_function_call() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload::from_text("ok".to_string()),
metadata: None,
@@ -676,6 +682,7 @@ fn remove_first_item_removes_matching_output_for_function_call() {
fn remove_first_item_removes_matching_call_for_output() {
let items = vec![
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-2".to_string(),
output: FunctionCallOutputPayload::from_text("ok".to_string()),
metadata: None,
@@ -699,6 +706,7 @@ fn replace_last_turn_images_replaces_tool_output_images() {
let items = vec![
user_input_text_msg("hi"),
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload {
body: FunctionCallOutputBody::ContentItems(vec![
@@ -721,6 +729,7 @@ fn replace_last_turn_images_replaces_tool_output_images() {
vec![
user_input_text_msg("hi"),
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload {
body: FunctionCallOutputBody::ContentItems(vec![
@@ -771,6 +780,7 @@ fn remove_first_item_handles_local_shell_pair() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-3".to_string(),
output: FunctionCallOutputPayload::from_text("ok".to_string()),
metadata: None,
@@ -998,6 +1008,7 @@ fn remove_first_item_handles_custom_tool_pair() {
metadata: None,
},
ResponseItem::CustomToolCallOutput {
id: None,
call_id: "tool-1".to_string(),
name: None,
output: FunctionCallOutputPayload::from_text("ok".to_string()),
@@ -1026,6 +1037,7 @@ fn normalization_retains_local_shell_outputs() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "shell-1".to_string(),
output: FunctionCallOutputPayload::from_text("Total output lines: 1\n\nok".to_string()),
metadata: None,
@@ -1047,6 +1059,7 @@ fn record_items_truncates_function_call_output_content() {
let long_line = "a very long line to trigger truncation\n";
let long_output = long_line.repeat(2_500);
let item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-100".to_string(),
output: FunctionCallOutputPayload {
body: FunctionCallOutputBody::Text(long_output.clone()),
@@ -1086,6 +1099,7 @@ fn record_items_truncates_custom_tool_call_output_content() {
let line = "custom output that is very long\n";
let long_output = line.repeat(2_500);
let item = ResponseItem::CustomToolCallOutput {
id: None,
call_id: "tool-200".to_string(),
name: None,
output: FunctionCallOutputPayload::from_text(long_output.clone()),
@@ -1118,6 +1132,7 @@ fn record_items_respects_custom_token_limit() {
let policy = TruncationPolicy::Tokens(10);
let long_output = "tokenized content repeated many times ".repeat(200);
let item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-custom-limit".to_string(),
output: FunctionCallOutputPayload {
body: FunctionCallOutputBody::Text(long_output),
@@ -1261,6 +1276,7 @@ fn normalize_adds_missing_output_for_function_call() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-x".to_string(),
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
metadata: None,
@@ -1296,6 +1312,7 @@ fn normalize_adds_missing_output_for_custom_tool_call() {
metadata: None,
},
ResponseItem::CustomToolCallOutput {
id: None,
call_id: "tool-x".to_string(),
name: None,
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
@@ -1342,6 +1359,7 @@ fn normalize_adds_missing_output_for_local_shell_call_with_id() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "shell-1".to_string(),
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
metadata: None,
@@ -1354,6 +1372,7 @@ fn normalize_adds_missing_output_for_local_shell_call_with_id() {
#[test]
fn normalize_removes_orphan_function_call_output() {
let items = vec![ResponseItem::FunctionCallOutput {
id: None,
call_id: "orphan-1".to_string(),
output: FunctionCallOutputPayload::from_text("ok".to_string()),
metadata: None,
@@ -1369,6 +1388,7 @@ fn normalize_removes_orphan_function_call_output() {
#[test]
fn normalize_removes_orphan_custom_tool_call_output() {
let items = vec![ResponseItem::CustomToolCallOutput {
id: None,
call_id: "orphan-2".to_string(),
name: None,
output: FunctionCallOutputPayload::from_text("ok".to_string()),
@@ -1396,6 +1416,7 @@ fn normalize_mixed_inserts_and_removals() {
},
// Orphan output that should be removed
ResponseItem::FunctionCallOutput {
id: None,
call_id: "c2".to_string(),
output: FunctionCallOutputPayload::from_text("ok".to_string()),
metadata: None,
@@ -1440,6 +1461,7 @@ fn normalize_mixed_inserts_and_removals() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "c1".to_string(),
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
metadata: None,
@@ -1453,6 +1475,7 @@ fn normalize_mixed_inserts_and_removals() {
metadata: None,
},
ResponseItem::CustomToolCallOutput {
id: None,
call_id: "t1".to_string(),
name: None,
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
@@ -1472,6 +1495,7 @@ fn normalize_mixed_inserts_and_removals() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "s1".to_string(),
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
metadata: None,
@@ -1504,6 +1528,7 @@ fn normalize_adds_missing_output_for_function_call_inserts_output() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-x".to_string(),
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
metadata: None,
@@ -1538,6 +1563,7 @@ fn normalize_adds_missing_output_for_tool_search_call() {
metadata: None,
},
ResponseItem::ToolSearchOutput {
id: None,
call_id: Some("search-call-x".to_string()),
status: "completed".to_string(),
execution: "client".to_string(),
@@ -1590,6 +1616,7 @@ fn normalize_adds_missing_output_for_local_shell_call_with_id_panics_in_debug()
#[should_panic]
fn normalize_removes_orphan_function_call_output_panics_in_debug() {
let items = vec![ResponseItem::FunctionCallOutput {
id: None,
call_id: "orphan-1".to_string(),
output: FunctionCallOutputPayload::from_text("ok".to_string()),
metadata: None,
@@ -1603,6 +1630,7 @@ fn normalize_removes_orphan_function_call_output_panics_in_debug() {
#[should_panic]
fn normalize_removes_orphan_custom_tool_call_output_panics_in_debug() {
let items = vec![ResponseItem::CustomToolCallOutput {
id: None,
call_id: "orphan-2".to_string(),
name: None,
output: FunctionCallOutputPayload::from_text("ok".to_string()),
@@ -1616,6 +1644,7 @@ fn normalize_removes_orphan_custom_tool_call_output_panics_in_debug() {
#[test]
fn normalize_removes_orphan_client_tool_search_output() {
let items = vec![ResponseItem::ToolSearchOutput {
id: None,
call_id: Some("orphan-search".to_string()),
status: "completed".to_string(),
execution: "client".to_string(),
@@ -1634,6 +1663,7 @@ fn normalize_removes_orphan_client_tool_search_output() {
#[should_panic]
fn normalize_removes_orphan_client_tool_search_output_panics_in_debug() {
let items = vec![ResponseItem::ToolSearchOutput {
id: None,
call_id: Some("orphan-search".to_string()),
status: "completed".to_string(),
execution: "client".to_string(),
@@ -1647,6 +1677,7 @@ fn normalize_removes_orphan_client_tool_search_output_panics_in_debug() {
#[test]
fn normalize_keeps_server_tool_search_output_without_matching_call() {
let items = vec![ResponseItem::ToolSearchOutput {
id: None,
call_id: Some("server-search".to_string()),
status: "completed".to_string(),
execution: "server".to_string(),
@@ -1660,6 +1691,7 @@ fn normalize_keeps_server_tool_search_output_without_matching_call() {
assert_eq!(
h.raw_items(),
vec![ResponseItem::ToolSearchOutput {
id: None,
call_id: Some("server-search".to_string()),
status: "completed".to_string(),
execution: "server".to_string(),
@@ -1683,6 +1715,7 @@ fn normalize_mixed_inserts_and_removals_panics_in_debug() {
metadata: None,
},
ResponseItem::FunctionCallOutput {
id: None,
call_id: "c2".to_string(),
output: FunctionCallOutputPayload::from_text("ok".to_string()),
metadata: None,
@@ -1757,6 +1790,7 @@ fn image_data_url_payload_does_not_dominate_function_call_output_estimate() {
let payload = "B".repeat(50_000);
let image_url = format!("data:image/png;base64,{payload}");
let item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-abc".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::InputText {
@@ -1783,6 +1817,7 @@ fn image_data_url_payload_does_not_dominate_custom_tool_call_output_estimate() {
let payload = "C".repeat(50_000);
let image_url = format!("data:image/png;base64,{payload}");
let item = ResponseItem::CustomToolCallOutput {
id: None,
call_id: "call-js-repl".to_string(),
name: None,
output: FunctionCallOutputPayload::from_content_items(vec![
@@ -1818,6 +1853,7 @@ fn non_base64_image_urls_are_unchanged() {
metadata: None,
};
let function_output_item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::InputImage {
@@ -1842,6 +1878,7 @@ fn non_base64_image_urls_are_unchanged() {
fn encrypted_function_output_uses_plaintext_byte_estimate() {
let encrypted_content = "A".repeat(1_868);
let item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-encrypted".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::EncryptedContent {
@@ -1883,6 +1920,7 @@ fn non_image_base64_data_url_is_unchanged() {
let payload = "C".repeat(4_096);
let image_url = format!("data:application/octet-stream;base64,{payload}");
let item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-octet".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::InputImage {
@@ -1971,6 +2009,7 @@ fn original_detail_images_scale_with_dimensions() {
let payload = BASE64_STANDARD.encode(bytes.get_ref());
let image_url = format!("data:image/png;base64,{payload}");
let item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-original".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::InputImage {
@@ -2002,6 +2041,7 @@ fn original_detail_images_are_capped_at_max_patch_count() {
let payload = BASE64_STANDARD.encode(bytes.get_ref());
let image_url = format!("data:image/png;base64,{payload}");
let item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-original-capped".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::InputImage {
@@ -2036,6 +2076,7 @@ fn original_detail_webp_images_scale_with_dimensions() {
let payload = BASE64_STANDARD.encode(bytes.get_ref());
let image_url = format!("data:image/webp;base64,{payload}");
let item = ResponseItem::FunctionCallOutput {
id: None,
call_id: "call-original-webp".to_string(),
output: FunctionCallOutputPayload::from_content_items(vec![
FunctionCallOutputContentItem::InputImage {
@@ -47,6 +47,7 @@ pub(crate) fn ensure_call_outputs_present(items: &mut Vec<ResponseItem>) {
missing_outputs_to_insert.push((
idx,
ResponseItem::FunctionCallOutput {
id: None,
call_id: call_id.clone(),
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
metadata: None,
@@ -61,6 +62,7 @@ pub(crate) fn ensure_call_outputs_present(items: &mut Vec<ResponseItem>) {
missing_outputs_to_insert.push((
idx,
ResponseItem::ToolSearchOutput {
id: None,
call_id: Some(call_id.clone()),
status: "completed".to_string(),
execution: "client".to_string(),
@@ -78,6 +80,7 @@ pub(crate) fn ensure_call_outputs_present(items: &mut Vec<ResponseItem>) {
missing_outputs_to_insert.push((
idx,
ResponseItem::CustomToolCallOutput {
id: None,
call_id: call_id.clone(),
name: None,
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
@@ -96,6 +99,7 @@ pub(crate) fn ensure_call_outputs_present(items: &mut Vec<ResponseItem>) {
missing_outputs_to_insert.push((
idx,
ResponseItem::FunctionCallOutput {
id: None,
call_id: call_id.clone(),
output: FunctionCallOutputPayload::from_text("aborted".to_string()),
metadata: None,