From 848d7ffbe3c03097225eb9782d6e707dea6cd870 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 1 Dec 2026 18:38:03 +0000 Subject: [PATCH] Trace per-step preparation for each tool contributor (#65067) Add an info-level `tool_contributor.prepare_for_step` span so traces show how long each contributor spends preparing before tools are built. Label the span with the contributor's `trace_name()`, which defaults to its concrete Rust type name, and skip recording the preparation inputs. GitOrigin-RevId: cc644deb2d7c1a00e8eea68698516ad1c2e102da --- codex-rs/core/src/session/mod.rs | 29 +++++++++++++++---- .../ext/extension-api/src/contributors.rs | 5 ++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index 0d4be50ea8..03b5159317 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -1472,6 +1472,23 @@ async fn thread_title_from_thread_store( (!title.is_empty() && thread.preview.trim() != title).then(|| title.to_string()) } +#[instrument( + name = "tool_contributor.prepare_for_step", + level = "info", + skip_all, + fields(contributor = contributor.trace_name()) +)] +async fn prepare_tool_contributor_for_step( + contributor: &dyn codex_extension_api::ToolContributor, + input: codex_extension_api::StepPreparationInput<'_>, + cancellation_token: &CancellationToken, +) -> Result<(), codex_async_utils::CancelErr> { + contributor + .prepare_for_step(input) + .or_cancel(cancellation_token) + .await +} + impl Session { pub(crate) async fn app_server_client_metadata(&self) -> AppServerClientMetadata { let state = self.state.lock().await; @@ -4908,8 +4925,9 @@ impl Session { }) .collect::>(); for contributor in self.services.extensions.tool_contributors() { - contributor - .prepare_for_step(codex_extension_api::StepPreparationInput { + prepare_tool_contributor_for_step( + contributor.as_ref(), + codex_extension_api::StepPreparationInput { turn_id: &turn_context.sub_id, ready_selected_capability_roots: &accessed_roots, executor_capability_discovery: skill_discovery, @@ -4919,9 +4937,10 @@ impl Session { thread_store: &self.services.thread_extension_data, turn_store: turn_context.extension_data.as_ref(), step_store: &extension_data, - }) - .or_cancel(cancellation_token) - .await?; + }, + cancellation_token, + ) + .await?; } drop(catalogs); extension_data.insert(selected_plugins); diff --git a/codex-rs/ext/extension-api/src/contributors.rs b/codex-rs/ext/extension-api/src/contributors.rs index 371e65eaec..5cca333ab8 100644 --- a/codex-rs/ext/extension-api/src/contributors.rs +++ b/codex-rs/ext/extension-api/src/contributors.rs @@ -416,6 +416,11 @@ pub struct StepPreparationInput<'a> { /// Extension contribution that exposes native tools owned by a feature. pub trait ToolContributor: Send + Sync { + /// Identifies the concrete contributor in traces, without any per-instance data. + fn trace_name(&self) -> &'static str { + std::any::type_name::() + } + /// Prepares step state once, before tools are built and without rendering. /// Never publish partial state; discovery failures may report warnings. fn prepare_for_step<'a>(&'a self, _input: StepPreparationInput<'a>) -> ExtensionFuture<'a, ()> {