diff --git a/codex-rs/core/src/tools/executed_tool_calls.rs b/codex-rs/core/src/tools/executed_tool_calls.rs index 636d3cbd92..6ca54d44a3 100644 --- a/codex-rs/core/src/tools/executed_tool_calls.rs +++ b/codex-rs/core/src/tools/executed_tool_calls.rs @@ -294,7 +294,7 @@ impl ExecutedToolCalls { } /// A later wait cannot claim a complete inventory if an earlier wire copy - /// lost recorded calls or arguments from the same Code Mode cell. + /// lost recorded calls or tool names from the same Code Mode cell. pub(crate) fn invalidate_wire_inventory_loss( &self, original: &[ResponseItem], @@ -321,7 +321,7 @@ impl ExecutedToolCalls { }; if bounded .executed_tool_call_metadata() - .is_none_or(|bounded| !bounded.has_same_tool_calls(calls)) + .is_none_or(|bounded| !bounded.has_same_tool_call_inventory(calls)) { state.invalidate_origin(origin); } @@ -415,7 +415,7 @@ impl ExecutedToolCalls { if let Some(id) = item.id() { state.direct_calls.insert(id.clone(), call.clone()); } - let complete = matches!(call.arguments(), ExecutedToolCallArguments::Raw(_)); + let complete = call.has_complete_inventory(); item.append_executed_tool_calls(vec![call]); if complete { item.mark_tool_calls_complete(); @@ -580,12 +580,14 @@ impl ExecutedToolCalls { ExecutedToolCallArguments::Truncated { .. } ); cell.observed_truncated_call |= truncated; - cell.completion = - if cell.completion == CellCompletion::Recording && !duplicate_call_id && !truncated { - CellCompletion::Recording - } else { - CellCompletion::Incomplete - }; + cell.completion = if cell.completion == CellCompletion::Recording + && !duplicate_call_id + && call.has_complete_inventory() + { + CellCompletion::Recording + } else { + CellCompletion::Incomplete + }; cell.pending_calls.insert(call_id, call); if !duplicate_call_id { state.pending_nested_calls += 1; diff --git a/codex-rs/core/src/tools/executed_tool_calls/request_metadata.rs b/codex-rs/core/src/tools/executed_tool_calls/request_metadata.rs index 1ae9eefb16..346bc01ff7 100644 --- a/codex-rs/core/src/tools/executed_tool_calls/request_metadata.rs +++ b/codex-rs/core/src/tools/executed_tool_calls/request_metadata.rs @@ -27,7 +27,7 @@ impl ExecutedToolCalls { retry_cache: &mut ExecutedToolCallCache, request_kind: RequestKind, ) { - let recording = { + { let mut state = self.lock_state(); let Some(state) = state.as_mut() else { // Disabling capture also stops replaying Direct records from history. @@ -38,7 +38,7 @@ impl ExecutedToolCalls { if let Some(call) = item.id().and_then(|id| state.direct_calls.get(id)) { item.clear_executed_tool_calls(); item.append_executed_tool_calls(vec![call.clone()]); - if matches!(call.arguments(), ExecutedToolCallArguments::Raw(_)) { + if call.has_complete_inventory() { item.mark_tool_calls_complete(); } } @@ -53,23 +53,11 @@ impl ExecutedToolCalls { state.direct_calls.retain(|id, _| output_ids.contains(id)); } Self::attach_pending_to_prompt_with_state(state, items, retry_cache, request_kind); - Arc::downgrade(&state.recording) - }; - - // Existing history can contain arguments recorded by an older client. Keep the - // per-call limit, and prevent newly truncated cells from claiming completeness - // on a later wait. Do the serialization outside the recorder lock. - let truncated_origins = normalize_executed_tool_call_arguments(items); - if !truncated_origins.is_empty() { - let mut state = self.lock_state(); - if let Some(state) = state.as_mut() - && recording.ptr_eq(&Arc::downgrade(&state.recording)) - { - for origin in truncated_origins { - state.invalidate_origin(&origin); - } - } } + + // Argument truncation preserves inventory completeness; omitted calls or + // names still invalidate completion across the cell's outputs. + normalize_executed_tool_call_arguments(items); } fn attach_pending_to_prompt_with_state( diff --git a/codex-rs/core/src/tools/executed_tool_calls/request_metadata_tests.rs b/codex-rs/core/src/tools/executed_tool_calls/request_metadata_tests.rs index b4ffbc8276..e168c9a660 100644 --- a/codex-rs/core/src/tools/executed_tool_calls/request_metadata_tests.rs +++ b/codex-rs/core/src/tools/executed_tool_calls/request_metadata_tests.rs @@ -928,10 +928,10 @@ fn tool_call_completeness_survives_waits_without_changing_deltas() { } else if index == 3 { recorder.finish_cell_recording(&cell_id); } - if index < 2 || index == 3 && !truncated { + if index < 2 || index == 3 { expected_output.set_tool_call_cell_id("exec"); } - if index == 3 && !truncated { + if index == 3 { expected_output.mark_tool_calls_complete(); } expected.push(expected_output); @@ -1156,7 +1156,7 @@ fn completeness_rejects_direct_id_collision_and_late_calls() { } #[test] -fn newly_normalized_arguments_keep_later_wait_incomplete() { +fn newly_normalized_arguments_preserve_later_wait_completeness() { let recorder = new_recorder(InitialHistory::New); let cell = CellId::new("active-cell".to_string()); recorder.start_cell(&cell, "exec"); @@ -1195,7 +1195,7 @@ fn newly_normalized_arguments_keep_later_wait_incomplete() { recorder.finish_cell_recording(&cell); let mut wait = [wait_input("wait", &cell), output("wait")]; recorder.attach_to_prompt(&mut wait, &mut HashMap::new()); - assert_eq!(tool_calls_complete(&wait[1]), None); + assert_eq!(tool_calls_complete(&wait[1]), Some(true)); } #[test] @@ -1360,6 +1360,8 @@ fn late_truncated_metadata_survives_subsequent_waits() { recorder.finish_cell_recording(&cell); history.extend([wait_input("wait-2", &cell), output("wait-2")]); expected.extend([wait_input("wait-2", &cell), output("wait-2")]); + expected[5].set_tool_call_cell_id("exec"); + expected[5].mark_tool_calls_complete(); recorder.attach_pending_to_prompt(&mut history, &mut retry_cache); assert_eq!(history, expected); } @@ -1385,15 +1387,19 @@ fn late_truncated_metadata_survives_close_before_first_attachment() { let mut expected = history.clone(); expected[1].append_executed_tool_calls(vec![call]); expected[1].set_tool_call_cell_id("exec"); + expected[1].mark_tool_calls_complete(); for wait in ["wait-1", "wait-2"] { recorder.finish_cell_recording(&cell); recorder.register_cell(&cell, wait); history.extend([wait_input(wait, &cell), output(wait)]); - expected.extend([wait_input(wait, &cell), output(wait)]); + let mut expected_output = output(wait); + expected_output.set_tool_call_cell_id("exec"); + expected_output.mark_tool_calls_complete(); + expected.extend([wait_input(wait, &cell), expected_output]); let mut request = history.clone(); recorder.attach_pending_to_prompt(&mut request, &mut retry_cache); assert_eq!(request, expected); - assert_eq!(tool_calls_complete(&request[1]), None); + assert_eq!(tool_calls_complete(&request[1]), Some(true)); } } @@ -1765,7 +1771,7 @@ fn finished_cells_without_more_waits_do_not_block_new_calls() { fn wire_inventory_loss_keeps_later_wait_incomplete() { for (scenario, expect_complete) in [ ("metadata_only", true), - ("arguments", false), + ("arguments", true), ("name", false), ("removed", false), ] { diff --git a/codex-rs/core/src/tools/executed_tool_calls_direct_tests.rs b/codex-rs/core/src/tools/executed_tool_calls_direct_tests.rs index 23f2033d25..bda3cca798 100644 --- a/codex-rs/core/src/tools/executed_tool_calls_direct_tests.rs +++ b/codex-rs/core/src/tools/executed_tool_calls_direct_tests.rs @@ -278,5 +278,5 @@ async fn direct_budget_counts_the_encoded_argument_before_it_enters_history() { arguments["_codex_executed_tool_call_truncated"]["original_bytes"], wire_bytes, ); - assert!(metadata.get("tool_calls_complete").is_none()); + assert_eq!(metadata["tool_calls_complete"], true); } diff --git a/codex-rs/core/tests/suite/code_mode.rs b/codex-rs/core/tests/suite/code_mode.rs index 9b0a4a89a5..fc4b9a9b60 100644 --- a/codex-rs/core/tests/suite/code_mode.rs +++ b/codex-rs/core/tests/suite/code_mode.rs @@ -1653,11 +1653,10 @@ await new Promise(() => {}); .iter() .all(|item| item["call_id"] != "call-1") ); - assert!( - final_request.function_call_output("call-2") - ["internal_chat_message_metadata_passthrough"] - .get("tool_calls_complete") - .is_none() + assert_eq!( + final_request.function_call_output("call-2")["internal_chat_message_metadata_passthrough"] + ["tool_calls_complete"], + true ); } } @@ -3597,7 +3596,7 @@ async fn code_mode_complete_call_survives_unrelated_truncation() -> Result<()> { .get("_codex_executed_tool_call_truncated") .is_some())) ); - assert!(overflow.get("tool_calls_complete").is_none()); + assert_eq!(overflow["tool_calls_complete"], true); let complete = &request.custom_tool_call_output("call-2")["internal_chat_message_metadata_passthrough"]; @@ -9164,7 +9163,7 @@ text(JSON.stringify({ } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn code_mode_oversized_websocket_yield_keeps_later_wait_incomplete() -> Result<()> { +async fn code_mode_argument_truncation_preserves_later_wait_completeness() -> Result<()> { skip_if_no_network!(Ok(())); const LIMIT: usize = 15 * 1024 * 1024; const PROMPT: &str = "Record a call, yield, then stop"; @@ -9309,7 +9308,7 @@ await new Promise(() => {}); let terminal = find_output(&terminal_request, "wait-a"); assert_eq!( terminal["internal_chat_message_metadata_passthrough"].get("tool_calls_complete"), - None + Some(&Value::Bool(true)) ); // Compare ordinary outputs against the live session history. No actual diff --git a/codex-rs/core/tests/suite/direct_tool_metadata.rs b/codex-rs/core/tests/suite/direct_tool_metadata.rs index b9274bf86d..67285f14ac 100644 --- a/codex-rs/core/tests/suite/direct_tool_metadata.rs +++ b/codex-rs/core/tests/suite/direct_tool_metadata.rs @@ -57,14 +57,14 @@ pub(super) fn tool_call_metadata(mut item: Value) -> Value { #[test_case(false; "http")] #[test_case(true; "websocket")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn message_budget_sheds_inventory_without_changing_tool_results_or_history( +async fn message_budget_sheds_arguments_without_losing_inventory_completeness( websocket: bool, ) -> Result<()> { skip_if_no_network!(Ok(())); let message_limit = 15 * 1024 * 1024; let call_count = 16; // Each argument fits 8 KiB and all observations fit Direct's 1 MiB budget. - // Only the final message budget should shed this inventory. HTTP includes + // Only the final message budget should shed recorded arguments. HTTP includes // the original call arguments; a WebSocket delta already has those upstream. let arguments = json!({"plan": [{"step": "x".repeat(7 * 1024), "status": "in_progress"}]}); assert!(arguments.to_string().len() < 8 * 1024); @@ -284,6 +284,7 @@ async fn message_budget_sheds_inventory_without_changing_tool_results_or_history "max_bytes": max_bytes, }}, }], + "tool_calls_complete": true, }) ); truncated_arguments += 1; @@ -629,7 +630,7 @@ async fn direct_function_and_tool_search_mark_complete_attempts( .get("_codex_executed_tool_call_truncated") .is_some() ); - assert!(metadata.get("tool_calls_complete").is_none()); + assert_eq!(metadata["tool_calls_complete"], true); } for index in 0..budget_calls { let output = request.function_call_output(&format!("plan-budget-{index}")); diff --git a/codex-rs/core/tests/suite/tools.rs b/codex-rs/core/tests/suite/tools.rs index 62ab267fa3..cf663b0be4 100644 --- a/codex-rs/core/tests/suite/tools.rs +++ b/codex-rs/core/tests/suite/tools.rs @@ -536,7 +536,7 @@ async fn namespaced_custom_tool_call_preserves_namespace_through_dispatch_and_re }]); assert_eq!( tool_call_metadata(escaped_request.custom_tool_call_output(escaped_call_id)), - json!({"executed_tool_calls": expected_escaped_calls}), + json!({"executed_tool_calls": expected_escaped_calls, "tool_calls_complete": true}), ); let direct_exec_call_id = "custom-direct-exec"; diff --git a/codex-rs/protocol/src/models.rs b/codex-rs/protocol/src/models.rs index af3e53aa8f..0d3305379f 100644 --- a/codex-rs/protocol/src/models.rs +++ b/codex-rs/protocol/src/models.rs @@ -986,7 +986,8 @@ pub struct InternalChatMessageMetadataPassthrough { #[schemars(skip)] #[ts(skip)] pub executed_tool_calls: Option>, - /// Whether the host recorded the complete call inventory without losing calls or arguments. + /// Whether the host recorded the complete ordered call inventory without losing calls or names. + /// Recorded arguments may be truncated independently of this claim. /// For a direct tool output this covers its single invocation; with `cell_id`, it covers /// the Code Mode cell across its outputs. Neither case describes tool success. #[serde(default, skip_deserializing, skip_serializing_if = "Option::is_none")] diff --git a/codex-rs/protocol/src/models/executed_tool_calls.rs b/codex-rs/protocol/src/models/executed_tool_calls.rs index 5bd758d75c..17429ff684 100644 --- a/codex-rs/protocol/src/models/executed_tool_calls.rs +++ b/codex-rs/protocol/src/models/executed_tool_calls.rs @@ -71,9 +71,21 @@ impl InternalChatMessageMetadataPassthrough { }) }) } + + /// Compares the complete ordered call inventory, independently of recorded arguments. + pub fn has_same_tool_call_inventory(&self, calls: &[ExecutedToolCall]) -> bool { + self.executed_tool_calls.as_ref().is_some_and(|recorded| { + recorded.len() == calls.len() + && recorded.iter().zip(calls).all(|(recorded, call)| { + recorded.name == call.name + && recorded.has_complete_inventory() + && call.has_complete_inventory() + }) + }) + } } -/// Bounds recorded arguments and clears completion when any call in a cell is truncated. +/// Bounds recorded arguments, preserving completion unless calls or tool names were lost. /// Returns cells whose arguments were newly truncated. Ordinary tool-call arguments and /// outputs are not changed. pub fn normalize_executed_tool_call_arguments(items: &mut [ResponseItem]) -> HashSet { @@ -86,7 +98,7 @@ pub fn normalize_executed_tool_call_arguments(items: &mut [ResponseItem]) -> Has else { continue; }; - let mut truncated = false; + let mut inventory_lost = false; let mut newly_truncated = false; for call in metadata.executed_tool_calls.iter_mut().flatten() { let argument_bytes = serde_json::to_vec(&call.arguments) @@ -101,9 +113,9 @@ pub fn normalize_executed_tool_call_arguments(items: &mut [ResponseItem]) -> Has ); newly_truncated = true; } - truncated |= call.truncation().is_some(); + inventory_lost |= !call.has_complete_inventory(); } - if truncated { + if inventory_lost { metadata.tool_calls_complete = None; damaged_cells.extend(metadata.cell_id.clone()); } @@ -134,12 +146,7 @@ pub fn bound_executed_tool_calls_for_message( if metadata_bytes(items) > max_metadata_bytes { let overage_bytes = shed_result_sources(items, max_metadata_bytes); if overage_bytes > 0 { - truncate_call_arguments_to_fit( - items, - max_metadata_bytes, - overage_bytes, - &mut damaged_cells, - ); + truncate_call_arguments_to_fit(items, max_metadata_bytes, overage_bytes); } remaining_bytes = metadata_bytes(items); } @@ -310,7 +317,6 @@ fn truncate_call_arguments_to_fit( items: &mut [ResponseItem], max_metadata_bytes: usize, mut overage_bytes: usize, - damaged_cells: &mut HashSet, ) { // A large outgoing message should not lose the other tool names in a // cell when replacing one call's arguments is enough to make it fit. @@ -344,17 +350,12 @@ fn truncate_call_arguments_to_fit( if truncated_bytes < original_bytes { call.arguments = truncated; arguments_changed = true; - metadata.tool_calls_complete = None; - damaged_cells.extend(metadata.cell_id.clone()); break; } } if !arguments_changed { break; } - // Losing one call's arguments invalidates every output in that - // cell. Count those removed fields before trimming another call. - clear_damaged_cell_completeness(items, damaged_cells); overage_bytes = metadata_bytes(items).saturating_sub(max_metadata_bytes); } } @@ -378,12 +379,19 @@ fn distribute_remaining_budget( let item_budget = remaining_bytes / remaining_items; if item_bytes > item_budget { // Remember the cell before a too-small share removes its metadata entirely. - damaged_cells.extend( - item.executed_tool_call_metadata() - .and_then(|metadata| metadata.cell_id.clone()), - ); - item.clear_tool_calls_complete(); + let cell_id = item + .executed_tool_call_metadata() + .and_then(|metadata| metadata.cell_id.clone()); item.bound_executed_tool_calls_with_budget(item_budget); + if item.executed_tool_call_metadata().is_none_or(|metadata| { + metadata + .executed_tool_calls + .as_ref() + .is_none_or(|calls| !calls.iter().all(ExecutedToolCall::has_complete_inventory)) + }) { + item.clear_tool_calls_complete(); + damaged_cells.extend(cell_id); + } } remaining_bytes = remaining_bytes.saturating_sub(executed_tool_call_metadata_bytes(item)); remaining_items -= 1; @@ -618,6 +626,14 @@ impl ExecutedToolCall { } } + /// Whether this entry retains its invocation and full tool name, regardless of arguments. + pub fn has_complete_inventory(&self) -> bool { + self.truncation().is_none_or(|truncation| { + truncation.omitted_calls.unwrap_or_default() == 0 + && truncation.original_name_bytes.is_none() + }) + } + fn set_truncation( &mut self, original_bytes: usize, @@ -686,7 +702,7 @@ impl ResponseItem { } } - /// Discards a completion claim when the host cannot retain its full evidence. + /// Discards a completion claim when the host cannot retain the full call inventory. pub fn clear_tool_calls_complete(&mut self) { if let Some(metadata) = self .internal_chat_message_metadata_passthrough_mut() diff --git a/codex-rs/protocol/src/models/executed_tool_calls_tests.rs b/codex-rs/protocol/src/models/executed_tool_calls_tests.rs index 422db7f82e..311d6c2a3c 100644 --- a/codex-rs/protocol/src/models/executed_tool_calls_tests.rs +++ b/codex-rs/protocol/src/models/executed_tool_calls_tests.rs @@ -225,7 +225,7 @@ fn tool_call_completeness_is_host_only_and_fail_closed() -> Result<()> { .executed_tool_call_metadata() .unwrap() .tool_calls_complete), - [None, (!same_cell).then_some(true)], + [Some(true), Some(true)], ); } } @@ -529,7 +529,7 @@ fn message_budget_removes_generic_fields_before_resources_in_the_same_output() { } #[test] -fn message_budget_preserves_names_and_turn_metadata_and_invalidates_shared_cell_completion() { +fn message_budget_preserves_call_inventory_when_arguments_are_truncated() { let arguments = serde_json::json!({"payload": "é".repeat(256)}); let argument_bytes = serde_json::to_vec(&arguments).unwrap().len(); let mut exec = output("exec"); @@ -555,11 +555,11 @@ fn message_budget_preserves_names_and_turn_metadata_and_invalidates_shared_cell_ .iter() .map(executed_tool_call_metadata_bytes) .sum::(); - // The middle case needs the sibling wait's completeness bytes to fit after - // truncating only the first call; the second call's arguments must survive. + // The first two cases fit after truncating only the first call's arguments; + // the second call's arguments and both completion claims must survive. for budget in [ original_metadata_bytes - 64, - original_metadata_bytes - (argument_bytes - 32), + original_metadata_bytes - (argument_bytes - 128), 0, ] { let mut items = original.clone(); @@ -576,9 +576,6 @@ fn message_budget_preserves_names_and_turn_metadata_and_invalidates_shared_cell_ argument_bytes - (original_metadata_bytes - budget), /*omitted_calls*/ None, ); - for item in &mut expected { - item.clear_tool_calls_complete(); - } } bound_executed_tool_calls_for_message(&mut items, budget); assert_eq!(items, expected); @@ -595,6 +592,29 @@ fn message_budget_preserves_names_and_turn_metadata_and_invalidates_shared_cell_ } } +#[test] +fn inventory_loss_still_clears_completeness() { + for (omitted_calls, original_name_bytes) in [(Some(1), None), (None, Some(20))] { + let mut call = ExecutedToolCall::new("test_tool".to_string(), serde_json::json!({})); + call.set_truncation_with_name( + /*original_bytes*/ 9_000, + /*max_bytes*/ 0, + omitted_calls, + original_name_bytes, + ); + let mut item = output("call"); + item.append_executed_tool_calls(vec![call]); + item.mark_tool_calls_complete(); + normalize_executed_tool_call_arguments(std::slice::from_mut(&mut item)); + assert_eq!( + item.executed_tool_call_metadata() + .unwrap() + .tool_calls_complete, + None + ); + } +} + #[test] fn result_metadata_shedding_keeps_smaller_results_within_one_output() { let budget = 2 * 1024 * 1024;