Lightweight coding agent that runs in your terminal
Rust
Preserve late result metadata for truncated code-mode calls (#48222)
ningyi-oai63eb71c9da95
diff --git a/codex-rs/core/src/tools/executed_tool_calls.rs b/codex-rs/core/src/tools/executed_tool_calls.rsindex 99ca2028d2..ee05b483bd 100644--- a/codex-rs/core/src/tools/executed_tool_calls.rs+++ b/codex-rs/core/src/tools/executed_tool_calls.rs@@ -102,6 +102,8 @@ struct ExecutedToolCallRecorderState {retained_calls: HashMap<(std::mem::Discriminant<ResponseItem>, String), RetainedToolCalls>,pending_nested_calls: usize,seen_ids: SeenIds,+ // Keep nested IDs separate so this conservative filter cannot change existing completeness.+ seen_nested_ids: SeenIds,can_prove_wait_completion: bool,pending_wrapper_origins: HashSet<String>,}@@ -115,9 +117,24 @@ struct RetainedToolCalls {runtime_cell_id: Option<CellId>,// Invocation IDs stay local and are retained only with their original output's calls.call_index_by_id: HashMap<String, usize>,+ // Track newly backfilled metadata separately from pre-existing observations.+ truncated_call_index_by_id: HashMap<String, usize>,+ late_truncated_indices: HashSet<usize>,result_metadata_updated: bool,}+impl RetainedToolCalls {+ fn clear_late_truncated_metadata(&mut self) {+ for index in self.late_truncated_indices.drain() {+ if let Some(call) = self.calls.get_mut(index) {+ call.set_tool_result_metadata(ToolResultMetadata::default());+ self.result_metadata_updated = true;+ }+ }+ self.truncated_call_index_by_id.clear();+ }+}+#[derive(Default, PartialEq, Eq)]enum CellCompletion {#[default]@@ -133,6 +150,9 @@ struct RecordedCell {pending_full_argument_bytes: usize,completion: CellCompletion,originating_call_id: Option<String>,+ truncated_metadata_binding_valid: bool,+ observed_truncated_call: bool,+ dispatch_closed: bool,}impl ExecutedToolCallRecorderState {@@ -141,11 +161,13 @@ impl ExecutedToolCallRecorderState {for cell in self.cells.values_mut() {if cell.originating_call_id.as_deref() == Some(origin) {cell.completion = CellCompletion::Incomplete;+ cell.truncated_metadata_binding_valid = false;}}for ((_, output_id), retained) in &mut self.retained_calls {if output_id == origin || retained.cell_id.as_deref() == Some(origin) {retained.complete = false;+ retained.clear_late_truncated_metadata();}}}@@ -163,10 +185,12 @@ impl ExecutedToolCallRecorderState {fn invalidate_cell(&mut self, cell_id: &CellId) {if let Some(cell) = self.cells.get_mut(cell_id) {cell.completion = CellCompletion::Incomplete;+ cell.truncated_metadata_binding_valid = false;}for retained in self.retained_calls.values_mut() {if retained.runtime_cell_id.as_ref() == Some(cell_id) {retained.complete = false;+ retained.clear_late_truncated_metadata();}}}@@ -498,7 +522,19 @@ impl ExecutedToolCalls {let Some(state) = state.as_mut() else {return;};- if !state.cells.contains_key(&cell_id) {+ let unique_nested_id = state.seen_nested_ids.observe_call_id(&call_id);+ if !unique_nested_id {+ for retained in state.retained_calls.values_mut() {+ if retained.truncated_call_index_by_id.contains_key(&call_id) {+ retained.clear_late_truncated_metadata();+ }+ }+ }+ if state+ .cells+ .get(&cell_id)+ .is_none_or(|cell| cell.dispatch_closed)+ {state.invalidate_cell(&cell_id);}if state.pending_nested_calls > MAX_PENDING_EXECUTED_TOOL_CALLS@@ -513,6 +549,9 @@ impl ExecutedToolCalls {let at_pending_call_limit = state.pending_nested_calls == MAX_PENDING_EXECUTED_TOOL_CALLS;let cell = state.cells.entry(cell_id).or_default();let duplicate_call_id = cell.pending_calls.contains_key(&call_id);+ if !unique_nested_id || duplicate_call_id {+ cell.truncated_metadata_binding_valid = false;+ }let max_bytes = MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES.min(MAX_EXECUTED_TOOL_CALL_FULL_ARGUMENT_BYTES_PER_OUTPUT.saturating_sub(cell.pending_full_argument_bytes),@@ -527,18 +566,21 @@ impl ExecutedToolCalls {} else {ExecutedToolCall::truncated(call.name, original_bytes, max_bytes)};- cell.completion = if cell.completion == CellCompletion::Recording- && !duplicate_call_id- && !matches!(- call.arguments(),- ExecutedToolCallArguments::Truncated { .. }- ) {- CellCompletion::Recording- } else {- CellCompletion::Incomplete- };+ let truncated = matches!(+ call.arguments(),+ ExecutedToolCallArguments::Truncated { .. }+ );+ cell.observed_truncated_call |= truncated;+ cell.completion =+ if cell.completion == CellCompletion::Recording && !duplicate_call_id && !truncated {+ CellCompletion::Recording+ } else {+ CellCompletion::Incomplete+ };cell.pending_calls.insert(call_id, call);- state.pending_nested_calls += 1;+ if !duplicate_call_id {+ state.pending_nested_calls += 1;+ }}fn record_tool_result_metadata(@@ -564,16 +606,33 @@ impl ExecutedToolCalls {call.set_tool_result_metadata(metadata);return has_metadata;}- let Some((retained, index)) = state.retained_calls.values_mut().find_map(|retained| {+ let retained_call = state.retained_calls.values_mut().find_map(|retained| {if retained.runtime_cell_id.as_ref()?.as_str() != cell_id.as_str() {return None;}let index = *retained.call_index_by_id.get(call_id)?;Some((retained, index))- }) else {+ });+ if let Some((retained, index)) = retained_call {+ // Older retry copies must not overwrite this output's accepted result metadata.+ retained.result_metadata_updated = true;+ retained.calls[index].set_tool_result_metadata(metadata);+ return has_metadata;+ }+ let mut candidates = state.retained_calls.values_mut().filter_map(|retained| {+ if retained.runtime_cell_id.as_ref()?.as_str() != cell_id.as_str() {+ return None;+ }+ let index = *retained.truncated_call_index_by_id.get(call_id)?;+ Some((retained, index))+ });+ let Some((retained, index)) = candidates.next() else {return false;};- // Older retry copies must not overwrite this output's accepted result metadata.+ if candidates.next().is_some() {+ return false;+ }+ retained.late_truncated_indices.insert(index);retained.result_metadata_updated = true;retained.calls[index].set_tool_result_metadata(metadata);has_metadata@@ -599,11 +658,22 @@ impl ExecutedToolCalls {let unique_origin = state.observe_cell_origin(output_call_id);if !unique_cell {state.invalidate_cell(cell_id);+ // A reused runtime ID cannot distinguish the old pending calls or+ // output mappings from this new execution. Do not attach them to it.+ if let Some(previous) = state.cells.remove(cell_id) {+ state.pending_nested_calls = state+ .pending_nested_calls+ .saturating_sub(previous.pending_calls.len());+ }+ state.output_cells.retain(|_, mapped| mapped != cell_id);}let history_ids_indexed = state.seen_ids.history_ids_indexed();state.register_cell(cell_id, output_call_id);if let Some(cell) = state.cells.get_mut(cell_id) {// Failed indexing must not make a known historical ID look fresh.+ cell.dispatch_closed = false;+ cell.truncated_metadata_binding_valid =+ unique_cell && unique_origin && history_ids_indexed;cell.completion = if unique_cell && unique_origin && history_ids_indexed {CellCompletion::Recording} else {@@ -617,7 +687,14 @@ impl ExecutedToolCalls {let Some(state) = state.as_mut() else {return;};+ // A closed cell may still be referenced by the final wait output. Keep its+ // original binding while late truncated results remain eligible for backfill.+ let has_retained_truncated_calls = state.retained_calls.values().any(|retained| {+ retained.runtime_cell_id.as_ref() == Some(cell_id)+ && !retained.truncated_call_index_by_id.is_empty()+ });if let Some(cell) = state.cells.get_mut(cell_id) {+ cell.dispatch_closed = true;match cell.completion {CellCompletion::Recording => {// The closed dispatch gate makes this lossless inventory final, even if empty.@@ -625,7 +702,7 @@ impl ExecutedToolCalls {}CellCompletion::Unobserved | CellCompletion::Incomplete => {// Drop unverified cells only after emitting any partial records.- if cell.pending_calls.is_empty() {+ if cell.pending_calls.is_empty() && !has_retained_truncated_calls {state.cells.remove(cell_id);}}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.rsindex 053c4a490a..762eccee89 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@@ -86,6 +86,96 @@ impl ExecutedToolCalls {}}+ let has_truncated_bindings = state+ .retained_calls+ .values()+ .any(|retained| !retained.truncated_call_index_by_id.is_empty())+ || state.cells.values().any(|cell| {+ cell.truncated_metadata_binding_valid+ && cell.pending_calls.values().any(|call| {+ matches!(+ call.arguments(),+ ExecutedToolCallArguments::Truncated { .. }+ ) && !call.has_tool_result_metadata()+ })+ });+ if has_truncated_bindings {+ // A conflicting sibling output invalidates the new late backfill too.+ // Check every known output before cloning any retained calls into this request.+ let mut untrusted_cells = HashSet::new();+ for (index, item) in items.iter().enumerate() {+ let Some(call_id) = output_call_id(item) else {+ continue;+ };+ let key = (std::mem::discriminant(item), call_id.to_string());+ let retained = state.retained_calls.get(&key);+ let binding = retained+ .and_then(|retained| {+ Some((+ retained.runtime_cell_id.as_ref()?,+ retained.cell_id.as_deref()?,+ true,+ ))+ })+ .or_else(|| {+ let cell_id = state.output_cells.get(call_id)?;+ let origin = state.cells.get(cell_id)?.originating_call_id.as_deref()?;+ Some((cell_id, origin, false))+ });+ let Some((cell_id, origin, previously_retained)) = binding else {+ continue;+ };+ let empty_inventory = item.executed_tool_call_metadata().is_none_or(|metadata| {+ metadata.executed_tool_calls.is_none() && metadata.cell_id.is_none()+ });+ let matches_input = match input_indices.get(call_id) {+ None => previously_retained,+ Some(input_index) => input_index.is_some_and(|input_index| {+ input_index < index+ && code_mode_input_matches_output(+ &items[input_index],+ item,+ origin,+ cell_id,+ )+ }),+ };+ let matches_retry = retained.is_none_or(|retained| {+ retry_cache+ .get(&key)+ .filter(|_| !retained.result_metadata_updated)+ .is_none_or(|cached| {+ cached.len() == retained.calls.len()+ && cached.iter().zip(&retained.calls).all(|(cached, current)| {+ cached.name == current.name+ && cached.arguments() == current.arguments()+ })+ })+ });+ if output_counts.get(call_id) != Some(&1)+ || !matches_input+ || !empty_inventory+ || !matches_retry+ {+ untrusted_cells.insert(cell_id.clone());+ }+ }+ for cell_id in &untrusted_cells {+ if let Some(cell) = state.cells.get_mut(cell_id) {+ cell.truncated_metadata_binding_valid = false;+ }+ }+ for retained in state.retained_calls.values_mut() {+ if retained+ .runtime_cell_id+ .as_ref()+ .is_some_and(|id| untrusted_cells.contains(id))+ {+ retained.clear_late_truncated_metadata();+ }+ }+ }+// Updated records supersede older retry snapshots.retry_cache.retain(|key, _| {!state@@ -125,6 +215,7 @@ impl ExecutedToolCalls {let mut runtime_cell_id = None;let mut calls = Vec::new();let mut call_index_by_id = HashMap::new();+ let mut truncated_call_index_by_id = HashMap::new();if let Some(output_cell_id) = state.output_cells.remove(call_id)&& let Some(cell) = state.cells.get_mut(&output_cell_id){@@ -148,13 +239,19 @@ impl ExecutedToolCalls {)})});- if output_counts.get(call_id) != Some(&1) || !matches_input {+ let valid_output = output_counts.get(call_id) == Some(&1) && matches_input;+ if !valid_output {cell.completion = CellCompletion::Incomplete;+ cell.truncated_metadata_binding_valid = false;}let pending_calls = cell.pending_calls.len();for (call_id, call) in cell.pending_calls.drain(..) {if matches!(call.arguments(), ExecutedToolCallArguments::Raw(_)) {call_index_by_id.insert(call_id, calls.len());+ } else if cell.truncated_metadata_binding_valid+ && !call.has_tool_result_metadata()+ {+ truncated_call_index_by_id.insert(call_id, calls.len());}calls.push(call);}@@ -162,10 +259,23 @@ impl ExecutedToolCalls {complete = cell.completion == CellCompletion::Complete&& (state.can_prove_wait_completion|| matches!(item, ResponseItem::CustomToolCallOutput { .. }));+ let has_retained_truncated_calls =+ state.retained_calls.values().any(|retained| {+ retained.runtime_cell_id.as_ref() == Some(&output_cell_id)+ && !retained.truncated_call_index_by_id.is_empty()+ });+ let keep_truncated_binding = cell.truncated_metadata_binding_valid+ && cell.observed_truncated_call+ && (!cell.dispatch_closed+ || !truncated_call_index_by_id.is_empty()+ || has_retained_truncated_calls);if matches!(cell.completion,CellCompletion::Complete | CellCompletion::Incomplete- ) {+ ) && !keep_truncated_binding+ {+ // Retain the original binding while a live cell can dispatch or+ // a closed cell still has results eligible for late backfill.state.cells.remove(&output_cell_id);}state.pending_nested_calls =@@ -186,6 +296,8 @@ impl ExecutedToolCalls {cell_id: cell_id.clone(),runtime_cell_id,call_index_by_id,+ truncated_call_index_by_id,+ late_truncated_indices: HashSet::new(),result_metadata_updated: false,},);@@ -255,6 +367,7 @@ impl ExecutedToolCalls {for cell_id in &invalid_cells {if let Some(cell) = state.cells.get_mut(cell_id) {cell.completion = CellCompletion::Incomplete;+ cell.truncated_metadata_binding_valid = false;}}if !invalid_cells.is_empty() {@@ -265,6 +378,7 @@ impl ExecutedToolCalls {.is_some_and(|cell_id| invalid_cells.contains(cell_id)){retained.complete = false;+ retained.clear_late_truncated_metadata();}}}@@ -345,6 +459,7 @@ impl ExecutedToolCalls {let unique_output = bounded_outputs.insert(key.clone());if !unique_output && let Some(retained) = state.retained_calls.get_mut(&key) {retained.call_index_by_id.clear();+ retained.clear_late_truncated_metadata();}if let Some(runtime_cell_id) = &previous.runtime_cell_id&& metadata@@ -361,12 +476,12 @@ impl ExecutedToolCalls {|| metadata.tool_calls_complete.is_some()){// Metadata-only shedding preserves slots; changed calls or duplicate outputs do not.- let call_index_by_id =- if unique_output && metadata.has_same_tool_calls(&previous.calls) {- previous.call_index_by_id.clone()- } else {- HashMap::new()- };+ let same_calls = unique_output && metadata.has_same_tool_calls(&previous.calls);+ let call_index_by_id = if same_calls {+ previous.call_index_by_id.clone()+ } else {+ HashMap::new()+ };let retained = state.retained_calls.entry(key).or_default();retained.runtime_cell_id = previous.runtime_cell_id.clone();retained.calls = metadata.executed_tool_calls.clone().unwrap_or_default();@@ -375,6 +490,20 @@ impl ExecutedToolCalls {}retained.complete |= metadata.tool_calls_complete == Some(true);retained.call_index_by_id = call_index_by_id;+ // Budgeting preserves call order; changed inventories lose their indices.+ if same_calls {+ retained.truncated_call_index_by_id =+ previous.truncated_call_index_by_id.clone();+ retained.late_truncated_indices = previous.late_truncated_indices.clone();+ } else {+ // The budgeter never reorders calls. Remove only the new late+ // evidence before losing the indices that identify it.+ for index in &previous.late_truncated_indices {+ if let Some(call) = retained.calls.get_mut(*index) {+ call.set_tool_result_metadata(ToolResultMetadata::default());+ }+ }+ }retained.result_metadata_updated = previous.result_metadata_updated;}}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.rsindex 274ff6b7b7..032eaab27c 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@@ -80,6 +80,25 @@ fn record_nested_call(call}+fn record_truncated_call(+ recorder: &ExecutedToolCalls,+ cell: &CellId,+ call_id: &str,+) -> ExecutedToolCall {+ let call = ExecutedToolCall::truncated(+ call_id.to_string(),+ /*original_bytes*/ 9_000,+ /*max_bytes*/ 8_192,+ );+ recorder.record_nested_tool_call(+ cell.clone(),+ call_id.to_string(),+ call.clone(),+ /*original_bytes*/ 9_000,+ );+ call+}+fn tool_calls_complete(item: &ResponseItem) -> Option<bool> {item.executed_tool_call_metadata().and_then(|metadata| metadata.tool_calls_complete)@@ -607,7 +626,11 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {let state = state.as_ref().unwrap();assert!(state.pending_wrapper_origins.is_empty());assert_eq!(state.pending_nested_calls, 0);- assert!(!state.cells.contains_key(&cell_id));+ let retained_cell = state.cells.get(&cell_id).expect("late result binding");+ assert!(retained_cell.dispatch_closed);+ assert!(retained_cell.truncated_metadata_binding_valid);+ assert!(retained_cell.pending_calls.is_empty());+ assert!(state.cells.len() <= MAX_PENDING_EXECUTED_TOOL_CALLS);assert_eq!(retry_cache.len(), 1);}@@ -623,9 +646,11 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {let mut compacted_retry_cache = HashMap::new();assert!(!recorder.attach_pending_to_prompt(&mut [], &mut compacted_retry_cache));+ recorder.finish_cell_recording(&cell_id);let state = recorder.lock_state();let state = state.as_ref().unwrap();assert!(state.retained_calls.is_empty());+ assert!(!state.cells.contains_key(&cell_id));}#[test]@@ -1342,6 +1367,392 @@ fn result_metadata_updates_the_exact_retained_call_and_preserves_large_retries()assert_eq!(serde_json::to_value(&retry).unwrap(), expected);}+#[test]+fn late_result_metadata_survives_truncated_arguments() {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("late-truncated-cell".to_string());+ recorder.start_cell(&cell, "exec");+ let first = ExecutedToolCall::truncated(+ "first".to_string(),+ /*original_bytes*/ 9_000,+ /*max_bytes*/ 8_192,+ );+ let second = ExecutedToolCall::truncated(+ "second".to_string(),+ /*original_bytes*/ 9_001,+ /*max_bytes*/ 8_192,+ );+ for (id, call, bytes) in [("first", &first, 9_000), ("second", &second, 9_001)] {+ recorder.record_nested_tool_call(cell.clone(), id.to_string(), call.clone(), bytes);+ }+ let history = [exec_input("exec"), exec_output("exec")];+ let mut retry_cache = HashMap::new();+ let mut initial = history.clone();+ assert!(recorder.attach_pending_to_prompt(&mut initial, &mut retry_cache));++ let source = ToolCallSource::CodeMode {+ cell_id: cell.as_str().to_string(),+ runtime_tool_call_id: "runtime".to_string(),+ };+ let first_metadata = json!({"openai/resource_access": {"resources": ["first"]}, "other": true});+ let second_metadata = json!({"openai/resource_access": {"resources": ["second"]}});+ assert!(recorder.record_tool_result_metadata(&source, "second", &second_metadata));+ assert!(recorder.record_tool_result_metadata(&source, "first", &first_metadata));++ let mut first = first;+ first.set_tool_result_metadata(ToolResultMetadata::new(&first_metadata));+ let mut second = second;+ second.set_tool_result_metadata(ToolResultMetadata::new(&second_metadata));+ let mut expected = history.clone();+ expected[1].append_executed_tool_calls(vec![first, second]);+ expected[1].set_tool_call_cell_id("exec");+ for _ in 0..2 {+ let mut retry = history.clone();+ assert!(recorder.attach_pending_to_prompt(&mut retry, &mut retry_cache));+ assert_eq!(retry, expected);+ assert_eq!(tool_calls_complete(&retry[1]), None);+ }+ let mut compact = history;+ recorder.attach_to_compaction_prompt(&mut compact);+ assert_eq!(compact, expected);+}++#[test]+fn late_truncated_metadata_survives_subsequent_waits() {+ for (result_before_wait, call_before_wait, close_before_wait) in [+ (false, false, false),+ (false, false, true),+ (false, true, false),+ (false, true, true),+ (true, false, false),+ (true, false, true),+ (true, true, false),+ (true, true, true),+ ] {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("live-cell".to_string());+ recorder.start_cell(&cell, "exec");+ let mut call = record_truncated_call(&recorder, &cell, "nested");+ let mut history = vec![exec_input("exec"), exec_output("exec")];+ let mut retry_cache = HashMap::new();+ recorder.attach_pending_to_prompt(&mut history.clone(), &mut retry_cache);++ let source = ToolCallSource::CodeMode {+ cell_id: cell.as_str().to_string(),+ runtime_tool_call_id: "nested".to_string(),+ };+ let metadata = json!({"openai/resource_access": {"resources": ["result"]}});+ if result_before_wait {+ assert!(recorder.record_tool_result_metadata(&source, "nested", &metadata));+ }+ let later_call = if call_before_wait {+ let call = record_nested_call(&recorder, &cell, "later");+ recorder.register_cell(&cell, "wait-1");+ call+ } else {+ recorder.register_cell(&cell, "wait-1");+ record_nested_call(&recorder, &cell, "later")+ };+ if !result_before_wait {+ assert!(recorder.record_tool_result_metadata(&source, "nested", &metadata));+ }+ history.extend([wait_input("wait-1", &cell), output("wait-1")]);+ let mut expected = history.clone();+ call.set_tool_result_metadata(ToolResultMetadata::new(&metadata));+ expected[1].append_executed_tool_calls(vec![call]);+ expected[1].set_tool_call_cell_id("exec");+ expected[3].append_executed_tool_calls(vec![later_call]);+ expected[3].set_tool_call_cell_id("exec");++ let mut request = history.clone();+ recorder.attach_pending_to_prompt(&mut request, &mut retry_cache);+ assert_eq!(request, expected);++ // The dispatch can close before or after the final wait is registered.+ if close_before_wait {+ recorder.finish_cell_recording(&cell);+ }+ recorder.register_cell(&cell, "wait-2");+ recorder.finish_cell_recording(&cell);+ 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")]);+ recorder.attach_pending_to_prompt(&mut history, &mut retry_cache);+ assert_eq!(history, expected);+ }+}++#[test]+fn late_truncated_metadata_survives_close_before_first_attachment() {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("closed-cell".to_string());+ recorder.start_cell(&cell, "exec");+ let mut call = record_truncated_call(&recorder, &cell, "nested");+ recorder.finish_cell_recording(&cell);+ let mut history = vec![exec_input("exec"), exec_output("exec")];+ let mut retry_cache = HashMap::new();+ recorder.attach_pending_to_prompt(&mut history.clone(), &mut retry_cache);+ let source = ToolCallSource::CodeMode {+ cell_id: cell.as_str().to_string(),+ runtime_tool_call_id: "nested".to_string(),+ };+ let metadata = json!({"provider": "late"});+ assert!(recorder.record_tool_result_metadata(&source, "nested", &metadata));+ call.set_tool_result_metadata(ToolResultMetadata::new(&metadata));+ let mut expected = history.clone();+ expected[1].append_executed_tool_calls(vec![call]);+ expected[1].set_tool_call_cell_id("exec");+ 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 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);+ }+}++#[test]+fn closed_truncated_cell_is_reclaimed_safely_under_pressure() {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("old-cell".to_string());+ recorder.start_cell(&cell, "old-exec");+ let call = record_truncated_call(&recorder, &cell, "nested");+ recorder.finish_cell_recording(&cell);+ let history = vec![exec_input("old-exec"), exec_output("old-exec")];+ let mut retry_cache = HashMap::new();+ recorder.attach_pending_to_prompt(&mut history.clone(), &mut retry_cache);+ let source = ToolCallSource::CodeMode {+ cell_id: cell.as_str().to_string(),+ runtime_tool_call_id: "nested".to_string(),+ };+ let metadata = json!({"provider": "late"});+ assert!(recorder.record_tool_result_metadata(&source, "nested", &metadata));++ // Occupy every other cell slot with a live mapped cell. The old, closed+ // cell is the only reclaimable entry, so pressure must discard its binding.+ for index in 0..MAX_PENDING_EXECUTED_TOOL_CALLS {+ recorder.start_cell(+ &CellId::new(format!("live-cell-{index}")),+ &format!("live-exec-{index}"),+ );+ }+ assert!(+ !recorder+ .lock_state()+ .as_ref()+ .unwrap()+ .cells+ .contains_key(&cell)+ );+ assert!(!recorder.record_tool_result_metadata(&source, "nested", &metadata));+ let mut expected = history.clone();+ expected[1].append_executed_tool_calls(vec![call]);+ expected[1].set_tool_call_cell_id("old-exec");+ let mut request = history;+ recorder.attach_pending_to_prompt(&mut request, &mut retry_cache);+ assert_eq!(request, expected);+}++#[test]+fn duplicate_pending_ids_do_not_consume_extra_capacity() {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("duplicate-cell".to_string());+ recorder.start_cell(&cell, "old-exec");+ for _ in 0..MAX_PENDING_EXECUTED_TOOL_CALLS {+ record_nested_call(&recorder, &cell, "duplicate");+ }+ {+ let state = recorder.lock_state();+ let state = state.as_ref().unwrap();+ assert_eq!(state.pending_nested_calls, 1);+ assert_eq!(state.cells[&cell].pending_calls.len(), 1);+ assert!(!state.cells[&cell].truncated_metadata_binding_valid);+ }+ recorder.start_cell(&cell, "new-exec");+ assert_eq!(+ recorder.lock_state().as_ref().unwrap().pending_nested_calls,+ 0+ );+ record_nested_call(&recorder, &cell, "new");+ assert_eq!(+ recorder.lock_state().as_ref().unwrap().pending_nested_calls,+ 1+ );+}++#[test]+fn reused_cell_does_not_attach_old_pending_calls_to_new_exec() {+ for previously_attached in [false, true] {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("reused-cell".to_string());+ recorder.start_cell(&cell, "old-exec");+ let old_call = record_truncated_call(&recorder, &cell, "old");+ let mut history = vec![exec_input("old-exec"), exec_output("old-exec")];+ let mut retry_cache = HashMap::new();+ if previously_attached {+ recorder.attach_pending_to_prompt(&mut history.clone(), &mut retry_cache);+ }++ recorder.start_cell(&cell, "new-exec");+ let new_call = record_nested_call(&recorder, &cell, "new");+ history.extend([exec_input("new-exec"), exec_output("new-exec")]);+ let mut expected = history.clone();+ if previously_attached {+ expected[1].append_executed_tool_calls(vec![old_call]);+ expected[1].set_tool_call_cell_id("old-exec");+ }+ expected[3].append_executed_tool_calls(vec![new_call]);+ expected[3].set_tool_call_cell_id("new-exec");+ recorder.attach_pending_to_prompt(&mut history, &mut retry_cache);+ assert_eq!(history, expected);+ }+}++#[test]+fn late_truncated_metadata_is_not_attached_to_ambiguous_outputs() {+ for scenario in [+ "duplicate_output",+ "wrong_input",+ "wrong_sibling_input",+ "duplicate_id",+ "reused_cell",+ "dispatch_after_close",+ ] {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("late-truncated-cell".to_string());+ recorder.start_cell(&cell, "exec");+ let call = record_truncated_call(&recorder, &cell, "nested");+ let history = [exec_input("exec"), exec_output("exec")];+ let mut retry_cache = HashMap::new();+ let mut initial = history.clone();+ recorder.attach_pending_to_prompt(&mut initial, &mut retry_cache);+ let source = ToolCallSource::CodeMode {+ cell_id: cell.as_str().to_string(),+ runtime_tool_call_id: "runtime".to_string(),+ };+ let metadata = json!({"openai/resource_access": {"resources": ["result"]}});+ assert!(recorder.record_tool_result_metadata(&source, "nested", &metadata));+ let mut retry = history.to_vec();+ match scenario {+ "duplicate_output" => retry.push(exec_output("exec")),+ "wrong_input" => retry[0] = wait_input("exec", &cell),+ "wrong_sibling_input" => {+ recorder.register_cell(&cell, "wait");+ retry.extend([+ wait_input("wait", &CellId::new("different-cell".to_string())),+ output("wait"),+ ]);+ }+ "duplicate_id" => {+ recorder.record_nested_tool_call(+ cell.clone(),+ "nested".to_string(),+ call,+ /*original_bytes*/ 9_000,+ );+ }+ "reused_cell" => recorder.start_cell(&cell, "another-exec"),+ "dispatch_after_close" => {+ recorder.finish_cell_recording(&cell);+ record_nested_call(&recorder, &cell, "after-close");+ }+ _ => unreachable!(),+ }+ recorder.attach_pending_to_prompt(&mut retry, &mut retry_cache);+ for item in retry.iter().skip(1) {+ assert!(!item.has_tool_result_metadata(), "{scenario}");+ }+ }+}++#[test]+fn late_truncated_metadata_obeys_the_existing_request_budget() {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("late-truncated-cell".to_string());+ recorder.start_cell(&cell, "exec");+ record_truncated_call(&recorder, &cell, "nested");+ let history = [exec_input("exec"), exec_output("exec")];+ let mut retry_cache = HashMap::new();+ let mut initial = history.clone();+ recorder.attach_pending_to_prompt(&mut initial, &mut retry_cache);+ let source = ToolCallSource::CodeMode {+ cell_id: cell.as_str().to_string(),+ runtime_tool_call_id: "runtime".to_string(),+ };+ let metadata = json!({+ "openai/resource_access": {"resources": ["result"]},+ "optional": "x".repeat(2 * 1024 * 1024),+ });+ assert!(recorder.record_tool_result_metadata(&source, "nested", &metadata));+ for _ in 0..2 {+ let mut retry = history.clone();+ recorder.attach_pending_to_prompt(&mut retry, &mut retry_cache);+ let actual = serde_json::to_value(&retry[1]).unwrap();+ assert_eq!(+ actual["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]["tool_result_metadata"],+ json!({"openai/resource_access": {"resources": ["result"]}}),+ );+ }+}++#[test]+fn late_truncated_metadata_is_not_reused_after_budget_changes_calls() {+ let recorder = new_recorder(InitialHistory::New);+ let cell = CellId::new("late-truncated-cell".to_string());+ recorder.start_cell(&cell, "exec");+ record_truncated_call(&recorder, &cell, "nested");+ // The reserved key requires a wrapper in recorded arguments. The wrapper can+ // put an otherwise accepted function argument over the later prompt limit.+ let arguments = json!({+ "_codex_executed_tool_call_truncated": true,+ "padding": "x".repeat(8_120),+ })+ .to_string();+ let (wrapped_call, original_bytes) = recorded_call(&ToolCall {+ tool_name: codex_tools::ToolName::plain("other"),+ call_id: "other".to_string(),+ payload: ToolPayload::Function { arguments },+ encrypted_function_args: None,+ });+ assert!(original_bytes <= MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);+ assert!(+ serialized_json_bytes(wrapped_call.arguments()).unwrap()+ > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES+ );+ recorder.record_nested_tool_call(+ cell.clone(),+ "other".to_string(),+ wrapped_call,+ original_bytes,+ );+ let history = [exec_input("exec"), exec_output("exec")];+ let mut retry_cache = HashMap::new();+ let mut initial = history.clone();+ recorder.attach_pending_to_prompt(&mut initial, &mut retry_cache);+ let source = ToolCallSource::CodeMode {+ cell_id: cell.as_str().to_string(),+ runtime_tool_call_id: "runtime".to_string(),+ };+ assert!(recorder.record_tool_result_metadata(+ &source,+ "nested",+ &json!({"openai/resource_access": {"resources": ["result"]}, "other": "x".repeat(40 * 1024)}),+ ));+ let mut bounded = history.clone();+ recorder.attach_pending_to_prompt(&mut bounded, &mut retry_cache);+ assert!(bounded[1].has_tool_result_metadata());+ let mut ambiguous = vec![history[0].clone(), history[1].clone(), history[1].clone()];+ recorder.attach_pending_to_prompt(&mut ambiguous, &mut retry_cache);+ assert!(+ ambiguous+ .iter()+ .all(|item| !item.has_tool_result_metadata())+ );+}+#[test]fn untracked_history_budget_loss_keeps_later_wait_incomplete() {let recorder = new_recorder(InitialHistory::New);diff --git a/codex-rs/core/tests/suite/code_mode.rs b/codex-rs/core/tests/suite/code_mode.rsindex 1c589fbfde..fd44103451 100644--- a/codex-rs/core/tests/suite/code_mode.rs+++ b/codex-rs/core/tests/suite/code_mode.rs@@ -1,5 +1,6 @@#![allow(clippy::unwrap_used)]+use anyhow::Context;use anyhow::Result;use base64::Engine;use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;@@ -2965,6 +2966,168 @@ async fn code_mode_result_metadata_keeps_prepared_call_binding_across_runtime_reOk(())}+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn code_mode_late_truncated_result_metadata_survives_waits() -> Result<()> {+ skip_if_no_network!(Ok(()));+ let server = responses::start_mock_server().await;+ let metadata = serde_json::json!({+ "openai/resource_access": {"resource_coverage": "incomplete"},+ "provider": {"id": "accepted"},+ });+ let apps_server =+ mount_result_metadata_app(&server, Some(metadata.clone()), /*is_error*/ false).await?;+ let (reached_tx, reached_rx) = oneshot::channel();+ let (release_tx, release_rx) = oneshot::channel();+ let control = Arc::new(ResultMetadataTestControl {+ server: Mutex::new(McpServerContribution::HostedApps {+ config: Box::new(codex_apps_mcp_server_config(+ &apps_server.chatgpt_base_url,+ /*apps_mcp_product_sku*/ None,+ /*originator*/ None,+ )),+ protocol_mode: None,+ }),+ gate: Mutex::new(Some((reached_tx, release_rx))),+ });+ let mut extensions = ExtensionRegistryBuilder::<Config>::new();+ extensions.tool_lifecycle_contributor(control);+ let builder = result_metadata_apps_builder(apps_server.chatgpt_base_url, "[email protected]")+ .with_extensions(Arc::new(extensions.build()))+ .with_code_mode_host_program(codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?)+ .with_config(|config| {+ config.features.enable(Feature::CodeModeHost).unwrap();+ config.code_mode.disable_in_process_fallback = true;+ });+ let arguments = serde_json::json!({ "query": "x".repeat(9_000) });+ let later_arguments = serde_json::json!({ "query": "later" });+ let code = format!(+ "const tool = ALL_TOOLS.find(({{ name }}) => name.endsWith(\"{RESULT_METADATA_TOOL}\")); \+ const pending = tools[tool.name]({arguments}); yield_control(); await pending; \+ await tools[tool.name]({later_arguments}); text(\"accepted\"); \+ yield_control(); await new Promise(() => {{}});"+ );+ let (test, first_response) =+ run_code_mode_turn_with_builder(&server, "Search a connected app", &code, builder).await?;+ let first_items = custom_tool_output_items(&first_response.single_request(), "call-1");+ assert!(+ text_item(&first_items, /*index*/ 0).starts_with("Script running with cell ID "),+ "expected a running cell: {first_items:?}"+ );+ let cell_id = extract_running_cell_id(text_item(&first_items, /*index*/ 0));+ tokio::time::timeout(Duration::from_secs(10), reached_rx)+ .await+ .context("first call did not reach the result gate")??;++ // The call may be dispatched after the first yield. Wait while its result is held so+ // the request has already recorded the truncated call before accepting its metadata.+ let held = responses::mount_function_call_agent_response(+ &server,+ "call-2",+ &serde_json::json!({"cell_id": cell_id, "yield_time_ms": 1}).to_string(),+ "wait",+ )+ .await;+ test.submit_turn("Read the pending call inventory").await?;+ let held_request = held.completion.single_request();+ let held_input = held_request.input();+ let held_calls = result_metadata_fixture_calls(&held_input).collect::<Vec<_>>();+ assert_eq!(held_calls.len(), 1);+ let original_output = held_calls[0];+ let original_id = original_output["call_id"].as_str().unwrap().to_string();+ let original_type = original_output["type"].as_str().unwrap().to_string();+ let truncated =+ &original_output["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0];+ assert!(+ truncated["name"]+ .as_str()+ .unwrap()+ .ends_with(RESULT_METADATA_TOOL)+ );+ assert!(truncated["arguments"]["_codex_executed_tool_call_truncated"].is_object());+ assert!(truncated.get("tool_result_metadata").is_none());++ release_tx.send(()).unwrap();+ let resumed = responses::mount_function_call_agent_response(+ &server,+ "call-3",+ &serde_json::json!({"cell_id": cell_id, "yield_time_ms": 10_000}).to_string(),+ "wait",+ )+ .await;+ test.submit_turn("Wait for the accepted results").await?;+ let resumed_request = resumed.completion.single_request();+ assert!(+ function_tool_output_items(&resumed_request, "call-3")+ .iter()+ .any(|item| item["text"] == "accepted")+ );+ let calls = recorded_apps_tool_calls(&server).await;+ assert_eq!(calls.len(), 2);+ assert_eq!(calls[0]["params"]["arguments"], arguments);+ assert_eq!(calls[1]["params"]["arguments"], later_arguments);+ let resumed_input = resumed_request.input();+ assert_eq!(result_metadata_fixture_calls(&resumed_input).count(), 2);+ assert_result_metadata_call(+ &resumed_request.function_call_output("call-3"),+ &later_arguments,+ /*expected_metadata*/ None,+ );++ // The custom inference endpoint strips raw result metadata. Inspect both the actual+ // request inventory and the recorder's unfiltered capture, including after another wait.+ for phase in 0..2 {+ let captured = codex_core::test_support::history_with_tool_call_metadata(&test.codex).await;+ let captured = serde_json::to_value(captured)?;+ let captured_output = captured+ .as_array()+ .unwrap()+ .iter()+ .find(|item| item["type"] == original_type && item["call_id"] == original_id)+ .expect("captured original output");+ let captured_calls =+ &captured_output["internal_chat_message_metadata_passthrough"]["executed_tool_calls"];+ assert_eq!(captured_calls.as_array().unwrap().len(), 1);+ assert_eq!(captured_calls[0]["arguments"], truncated["arguments"]);+ assert_eq!(captured_calls[0]["tool_result_metadata"], metadata);+ assert_ne!(+ captured_output["internal_chat_message_metadata_passthrough"]["tool_calls_complete"],+ true+ );+ let later_output = captured+ .as_array()+ .unwrap()+ .iter()+ .find(|item| item["type"] == "function_call_output" && item["call_id"] == "call-3")+ .expect("captured later wait output");+ assert_result_metadata_call(later_output, &later_arguments, Some(metadata.clone()));+ if phase == 0 {+ let terminal = responses::mount_function_call_agent_response(+ &server,+ "call-4",+ &serde_json::json!({"cell_id": cell_id, "terminate": true}).to_string(),+ "wait",+ )+ .await;+ test.submit_turn("Finish the cell").await?;+ let terminal_request = terminal.completion.single_request();+ let terminal_input = terminal_request.input();+ assert_eq!(result_metadata_fixture_calls(&terminal_input).count(), 2);+ let original = terminal_request.call_output(&original_id, &original_type);+ assert_eq!(+ original["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]["arguments"],+ truncated["arguments"]+ );+ assert!(+ original["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]+ .get("tool_result_metadata")+ .is_none()+ );+ }+ }+ test.codex.shutdown_and_wait().await?;+ Ok(())+}+#[cfg_attr(windows, ignore = "no exec_command on Windows")]#[tokio::test(flavor = "multi_thread", worker_threads = 2)]async fn code_mode_resumed_wait_does_not_certify_a_reused_runtime_cell() -> Result<()> {diff --git a/codex-rs/protocol/src/models/executed_tool_calls.rs b/codex-rs/protocol/src/models/executed_tool_calls.rsindex 88e7775a30..676c0919d8 100644--- a/codex-rs/protocol/src/models/executed_tool_calls.rs+++ b/codex-rs/protocol/src/models/executed_tool_calls.rs@@ -581,6 +581,7 @@ impl ToolResultMetadata {}}+ /// Whether there is no captured result metadata.fn is_none(&self) -> bool {self.0.is_none()}@@ -675,6 +676,11 @@ impl ExecutedToolCall {&self.arguments}+ /// Whether this call has result metadata or an omission marker.+ pub fn has_tool_result_metadata(&self) -> bool {+ self.tool_result_metadata.is_some()+ }+/// Replaces this invocation's capture outcome, including clearing omitted evidence.pub fn set_tool_result_sources(&mut self, sources: ToolResultSources) -> bool {self.tool_result_sources = sources.0;