Add turn, tool, and skill attribution to traces (#56524)

## What changed

- Emit `codex.turn_profile` events with finalized sampling request and retry counts when turns complete or abort, and preserve the caller's tracing span for detached extension interrupts.
- Add conversation and turn identifiers to turn and tool dispatch spans, identify tool sources, and include dispatch-to-result-ready duration on tool completion events.
- Emit `codex.skill_read.source_selected` before cache or provider access, recording the selected cloud or executor source, skill and plugin identity, resource kind, and continuation status.

## Testing

Add regression coverage for trace correlation, tool completion timing, turn profile counters on completion and interruption, detached interrupt span propagation, and skill source attribution including cached cloud reads.

GitOrigin-RevId: db78aa270bbcb8dc9e4558d202c109ad54141811
This commit is contained in:
bkotsopoulos
2026-10-23 16:55:32 +00:00
committed by copyberry
parent 3985001067
commit b82d542aef
13 changed files with 363 additions and 15 deletions
@@ -13,6 +13,7 @@ use codex_protocol::protocol::TurnAbortReason;
use std::sync::Arc;
use tokio::sync::oneshot;
use tokio::sync::oneshot::error::TryRecvError;
use tracing::Instrument;
#[cfg(test)]
#[path = "extension_interruption_tests.rs"]
@@ -108,16 +109,21 @@ impl Session {
let runtime = self.services.runtime_handle.clone();
let session = Arc::clone(self);
let turn_id = turn_id.to_owned();
drop(runtime.spawn(async move {
if session
.abort_turn_if_active(&turn_id, TurnAbortReason::Interrupted, error)
.await
{
// Extension aborts bypass normal task completion; user interrupts do not.
session
.emit_thread_idle_lifecycle_if_idle(ThreadIdleCause::Interrupted)
.await;
}
}));
drop(
runtime.spawn(
async move {
if session
.abort_turn_if_active(&turn_id, TurnAbortReason::Interrupted, error)
.await
{
// Extension aborts bypass normal task completion; user interrupts do not.
session
.emit_thread_idle_lifecycle_if_idle(ThreadIdleCause::Interrupted)
.await;
}
}
.in_current_span(),
),
);
}
}
@@ -1,4 +1,5 @@
//! Regression coverage for cancellation while submitting a conditional interrupt.
//! Regression coverage for conditional interruption and detached abort tracing.
#![cfg(test)]
use super::*;
use crate::session::SessionIo;
@@ -12,10 +13,104 @@ use codex_protocol::protocol::AgentStatus;
use codex_protocol::protocol::AskForApproval;
use codex_protocol::protocol::SessionConfiguredEvent;
use codex_protocol::protocol::SessionSource;
use opentelemetry::trace::TracerProvider;
use opentelemetry_sdk::trace::InMemorySpanExporter;
use opentelemetry_sdk::trace::SdkTracerProvider;
use pretty_assertions::assert_eq;
use std::time::Duration;
use tokio::sync::Notify;
use tokio::sync::watch;
use tracing::Instrument;
use tracing_subscriber::prelude::*;
#[tokio::test]
async fn interrupt_with_warning_exports_turn_profile_under_caller_span() -> anyhow::Result<()> {
let exported = InMemorySpanExporter::default();
let provider = SdkTracerProvider::builder()
.with_simple_exporter(exported.clone())
.build();
let subscriber = tracing_subscriber::registry().with(
tracing_opentelemetry::layer()
.with_tracer(provider.tracer("guardian-interrupt-test"))
.with_filter(tracing_subscriber::filter::filter_fn(
codex_otel::OtelProvider::trace_export_filter,
)),
);
let _interest_cache_guard =
tracing::Dispatch::new(tracing::subscriber::NoSubscriber::default());
let _subscriber_guard = tracing::subscriber::set_default(subscriber);
let (session, turn, events) = make_session_and_context_with_rx().await;
session
.spawn_task(
Arc::clone(&turn),
Vec::new(),
HeldStepTask {
kind: TaskKind::Regular,
finish: Arc::new(Notify::new()),
},
)
.await;
turn.turn_timing_state
.mark_turn_started(std::time::Instant::now())
.await;
drop(turn.turn_timing_state.begin_sampling());
turn.turn_timing_state.record_sampling_retry();
// Guardian uses this detached abort path rather than submitting Op::Interrupt.
session
.interrupt_turn_with_warning(
&turn.sub_id,
EventMsg::Warning(codex_protocol::protocol::WarningEvent {
message: "Guardian denial limit reached".into(),
}),
/*error*/ None,
)
.instrument(tracing::info_span!("guardian_review"))
.await;
tokio::time::timeout(Duration::from_secs(/*secs*/ 5), async {
while !matches!(events.recv().await?.msg, EventMsg::TurnAborted(_)) {}
anyhow::Ok(())
})
.await??;
let profile = tokio::time::timeout(Duration::from_secs(/*secs*/ 5), async {
loop {
let spans = exported.get_finished_spans()?;
for span in spans {
if span.name == "guardian_review"
&& let Some(event) = span
.events
.iter()
.find(|event| event.name == "codex.turn_profile")
{
return anyhow::Ok(event.clone());
}
}
tokio::task::yield_now().await;
}
})
.await??;
let attributes: std::collections::BTreeMap<_, _> = profile
.attributes
.iter()
.filter(|attribute| {
matches!(
attribute.key.as_str(),
"conversation.id" | "turn_id" | "sampling_request_count" | "sampling_retry_count"
)
})
.map(|attribute| (attribute.key.as_str(), attribute.value.to_string()))
.collect();
assert_eq!(
attributes,
std::collections::BTreeMap::from([
("conversation.id", session.thread_id.to_string()),
("turn_id", turn.sub_id.clone()),
("sampling_request_count", "1".into()),
("sampling_retry_count", "1".into()),
])
);
Ok(())
}
#[tokio::test]
async fn interrupt_if_no_pending_input_handles_cancelled_submission() {