Lightweight coding agent that runs in your terminal
Rust
Recover executed tool call metadata under recorder capacity pressure (#46712)
ningyi-oaied0ee704b440
diff --git a/codex-rs/core/src/tools/executed_tool_calls.rs b/codex-rs/core/src/tools/executed_tool_calls.rsindex 0e905272f0..43e8aa534e 100644--- a/codex-rs/core/src/tools/executed_tool_calls.rs+++ b/codex-rs/core/src/tools/executed_tool_calls.rs@@ -157,21 +157,34 @@ impl ExecutedToolCallRecorderState {}fn register_cell(&mut self, cell_id: &CellId, output_call_id: &str) {- if self.cells.len() >= MAX_PENDING_EXECUTED_TOOL_CALLS && !self.cells.contains_key(cell_id)+ if self.output_cells.len() >= MAX_PENDING_EXECUTED_TOOL_CALLS {+ // Ended empty cells can leave mappings without any records to attach.+ // Reclaim those only under pressure, preserving late partial records otherwise.+ self.output_cells+ .retain(|_, cell_id| self.cells.contains_key(cell_id));+ }+ while (self.cells.len() >= MAX_PENDING_EXECUTED_TOOL_CALLS+ && !self.cells.contains_key(cell_id))+ || self.pending_nested_calls >= MAX_PENDING_EXECUTED_TOOL_CALLS{let output_cells = self.output_cells.values().collect::<HashSet<_>>();let finished_cell = self.cells.iter().find_map(|(id, cell)| {- // A finished cell can still have missing or truncated tool call records.- (matches!(- cell.completion,- CellCompletion::Complete | CellCompletion::Incomplete- ) && cell.pending_calls.is_empty()+ // Preserve late records until pressure, and never discard the cell+ // whose output is about to make those records attachable again.+ (id != cell_id+ && matches!(+ cell.completion,+ CellCompletion::Complete | CellCompletion::Incomplete+ )&& !output_cells.contains(id))- .then(|| id.clone())+ .then(|| (id.clone(), cell.pending_calls.len()))});- if let Some(id) = finished_cell {- self.cells.remove(&id);- }+ let Some((id, pending_calls)) = finished_cell else {+ break;+ };+ self.invalidate_cell(&id);+ self.cells.remove(&id);+ self.pending_nested_calls = self.pending_nested_calls.saturating_sub(pending_calls);}if (self.cells.len() >= MAX_PENDING_EXECUTED_TOOL_CALLS&& !self.cells.contains_key(cell_id))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 7acec71289..bb617a1b7d 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@@ -1333,6 +1333,70 @@ fn result_metadata_shedding_preserves_completion_after_compaction() {);}+#[test]+fn mapping_pressure_preserves_existing_cells_and_late_partial_records() {+ let recorder = new_recorder(InitialHistory::New);+ let late = CellId::new("late".to_string());+ recorder.register_cell(&late, "late-output");+ recorder.finish_cell_recording(&late);+ let late_call = record_nested_call(&recorder, &late, "late-call");++ let active = CellId::new("active".to_string());+ recorder.start_cell(&active, "active-output");+ let active_call = record_nested_call(&recorder, &active, "active-call");+ let finished = CellId::new("finished".to_string());+ recorder.start_cell(&finished, "finished-output");+ let finished_call = record_nested_call(&recorder, &finished, "finished-call");+ recorder.finish_cell_recording(&finished);+ let orphan = CellId::new("orphan".to_string());+ for index in 3..MAX_PENDING_EXECUTED_TOOL_CALLS {+ recorder.register_cell(&orphan, &format!("orphan-output-{index}"));+ }+ recorder.finish_cell_recording(&orphan);+ recorder.register_cell(&active, "active-pressure-output");++ // Callbacks can arrive after their orphan output mappings were reclaimed.+ for index in 0..MAX_PENDING_EXECUTED_TOOL_CALLS {+ record_nested_call(&recorder, &orphan, &format!("orphan-late-{index}"));+ }+ let fresh = CellId::new("fresh".to_string());+ recorder.start_cell(&fresh, "fresh-output");+ let fresh_call = record_nested_call(&recorder, &fresh, "fresh-call");+ recorder.finish_cell_recording(&fresh);++ let mut items = [+ exec_input("late-output"),+ exec_output("late-output"),+ exec_input("active-output"),+ exec_output("active-output"),+ exec_input("finished-output"),+ exec_output("finished-output"),+ exec_input("fresh-output"),+ exec_output("fresh-output"),+ ];+ let mut expected = items.clone();+ for (index, origin, call, complete) in [+ (1, None, late_call, false),+ (3, Some("active-output"), active_call, false),+ (5, Some("finished-output"), finished_call, true),+ (7, Some("fresh-output"), fresh_call, true),+ ] {+ expected[index].append_executed_tool_calls(vec![call]);+ if let Some(origin) = origin {+ expected[index].set_tool_call_cell_id(origin);+ }+ if complete {+ expected[index].mark_tool_calls_complete();+ }+ }+ recorder.attach_to_prompt(&mut items, &mut HashMap::new());+ assert_eq!(items, expected);+ assert_eq!(+ recorder.lock_state().as_ref().unwrap().pending_nested_calls,+ 0+ );+}+#[test]fn finished_cells_without_more_waits_do_not_block_new_calls() {let recorder = new_recorder(InitialHistory::Forked(Vec::new()));diff --git a/codex-rs/core/tests/suite/code_mode.rs b/codex-rs/core/tests/suite/code_mode.rsindex ea162cbda7..d443287687 100644--- a/codex-rs/core/tests/suite/code_mode.rs+++ b/codex-rs/core/tests/suite/code_mode.rs@@ -87,6 +87,8 @@ use core_test_support::skip_if_no_network;use core_test_support::skip_if_sandbox;use core_test_support::skip_if_wine_exec;use core_test_support::stdio_server_bin;+use core_test_support::streaming_sse::StreamingSseChunk;+use core_test_support::streaming_sse::start_streaming_sse_server;use core_test_support::test_codex::TestCodex;use core_test_support::test_codex::TestCodexBuilder;use core_test_support::test_codex::test_codex;@@ -2550,6 +2552,124 @@ async fn code_mode_resumed_wait_does_not_certify_a_reused_runtime_cell() -> ResuOk(())}+struct MappingPressureObserver {+ finished: AtomicUsize,+ release: Mutex<Option<oneshot::Sender<()>>>,+}++impl ToolLifecycleContributor for MappingPressureObserver {+ fn on_tool_finish<'a>(&'a self, input: ToolFinishInput<'a>) -> ToolLifecycleFuture<'a> {+ Box::pin(async move {+ if input.tool_name.name == "exec" && input.call_id.starts_with("seed-") {+ assert_eq!(input.outcome, ToolCallOutcome::Completed { success: true });+ if self.finished.fetch_add(1, Ordering::SeqCst) == 511 {+ self.release+ .lock()+ .unwrap()+ .take()+ .unwrap()+ .send(())+ .unwrap();+ }+ }+ })+ }+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn code_mode_recovers_complete_inventory_after_orphaned_mapping_pressure() -> Result<()> {+ skip_if_no_network!(Ok(()));++ let empty_calls = (0..256)+ .map(|index| ev_custom_tool_call(&format!("seed-{index}"), "exec", "void 0;"))+ .collect::<Vec<_>>();+ let mut seed = empty_calls.clone();+ seed.push(ev_completed("resp-seed"));+ let (release, pressure_reached) = oneshot::channel();+ let (server, _) = start_streaming_sse_server(vec![+ vec![StreamingSseChunk {+ gate: None,+ body: sse(seed),+ }],+ vec![+ StreamingSseChunk {+ gate: None,+ body: sse(empty_calls),+ },+ StreamingSseChunk {+ // Reused IDs leave 256 orphan mappings once both batches finish.+ // Keep this response open so attachment cannot drain them first.+ gate: Some(pressure_reached),+ body: sse(vec![+ ev_custom_tool_call(+ "call-fresh",+ "exec",+ r#"await tools.test_sync_tool({}); text("recovered");"#,+ ),+ ev_completed("resp-pressure"),+ ]),+ },+ ],+ vec![StreamingSseChunk {+ gate: None,+ body: sse(vec![+ ev_assistant_message("msg-done", "done"),+ ev_completed("resp-done"),+ ]),+ }],+ ])+ .await;+ let mut extensions = ExtensionRegistryBuilder::<Config>::new();+ extensions.tool_lifecycle_contributor(Arc::new(MappingPressureObserver {+ finished: AtomicUsize::new(0),+ release: Mutex::new(Some(release)),+ }));+ let base_url = format!("{}/v1", server.uri());+ let config_server = responses::start_mock_server().await;+ let test = test_codex()+ .with_model("test-gpt-5.1-codex")+ .with_extensions(Arc::new(extensions.build()))+ .with_config(move |config| {+ config.model_provider.base_url = Some(base_url);+ let _ = config.features.enable(Feature::CodeMode);+ let _ = config.features.enable(Feature::ExecutedToolCallMetadata);+ let _ = config.features.disable(Feature::RemoteCompactionV2);+ })+ .build_with_auto_env(&config_server)+ .await?;+ test.submit_turn("Record a fresh call after orphaned mapping pressure")+ .await?;+ test.codex.shutdown_and_wait().await?;++ let requests = server.requests().await;+ assert_eq!(requests.len(), 3);+ let request: Value = serde_json::from_slice(&requests[2])?;+ let output = request["input"]+ .as_array()+ .unwrap()+ .iter()+ .find(|item| item["type"] == "custom_tool_call_output" && item["call_id"] == "call-fresh")+ .unwrap();+ assert_eq!(+ output["output"]+ .as_array()+ .unwrap()+ .iter()+ .filter_map(|item| item["text"].as_str())+ .rfind(|text| !text.trim().is_empty()),+ Some("recovered"),+ );+ let metadata = &output["internal_chat_message_metadata_passthrough"];+ assert_eq!(+ metadata["executed_tool_calls"],+ serde_json::json!([{"name": "test_sync_tool", "arguments": {}}]),+ );+ assert_eq!(metadata["cell_id"], "call-fresh");+ assert_eq!(metadata["tool_calls_complete"], true);+ server.shutdown().await;+ Ok(())+}+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]async fn code_mode_complete_call_survives_unrelated_truncation() -> Result<()> {skip_if_no_network!(Ok(()));