mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
fix(core): align rooted turn trace ids
This commit is contained in:
@@ -5892,7 +5892,7 @@ async fn root_task_turn_span_does_not_inherit_current_trace_context() {
|
||||
.instrument(request_span)
|
||||
.await;
|
||||
|
||||
let evt = tokio::time::timeout(StdDuration::from_secs(2), rx.recv())
|
||||
let evt = tokio::time::timeout(StdDuration::from_secs(/*secs*/ 2), rx.recv())
|
||||
.await
|
||||
.expect("timeout waiting for turn completion")
|
||||
.expect("event");
|
||||
@@ -5913,6 +5913,52 @@ async fn root_task_turn_span_does_not_inherit_current_trace_context() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rooted_regular_turn_does_not_emit_parent_trace_id() {
|
||||
let _trace_test_context = install_test_tracing("codex-core-tests");
|
||||
let request_parent = W3cTraceContext {
|
||||
traceparent: Some("00-00000000000000000000000000000011-0000000000000022-01".into()),
|
||||
tracestate: Some("vendor=value".into()),
|
||||
};
|
||||
let request_span = tracing::info_span!("completed_turn");
|
||||
assert!(set_parent_from_w3c_trace_context(
|
||||
&request_span,
|
||||
&request_parent
|
||||
));
|
||||
|
||||
let (sess, tc, rx) = make_session_and_context_with_rx()
|
||||
.instrument(request_span.clone())
|
||||
.await;
|
||||
assert_eq!(
|
||||
tc.trace_id.as_deref(),
|
||||
Some("00000000000000000000000000000011")
|
||||
);
|
||||
|
||||
async {
|
||||
sess.start_task(
|
||||
Arc::clone(&tc),
|
||||
Vec::new(),
|
||||
crate::tasks::RegularTask::new(),
|
||||
TurnSpanParent::Root,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
.instrument(request_span)
|
||||
.await;
|
||||
|
||||
let first = tokio::time::timeout(StdDuration::from_secs(/*secs*/ 2), rx.recv())
|
||||
.await
|
||||
.expect("timeout waiting for turn start")
|
||||
.expect("event");
|
||||
let EventMsg::TurnStarted(turn_started) = first.msg else {
|
||||
panic!("expected turn started event");
|
||||
};
|
||||
assert!(turn_started.trace_id.is_some());
|
||||
assert_ne!(turn_started.trace_id, tc.trace_id);
|
||||
|
||||
sess.abort_all_tasks(TurnAbortReason::Interrupted).await;
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[tokio::test]
|
||||
async fn shutdown_complete_does_not_append_to_thread_store_after_shutdown() {
|
||||
|
||||
@@ -174,13 +174,19 @@ fn bool_tag(value: bool) -> &'static str {
|
||||
pub(crate) struct SessionTaskContext {
|
||||
session: Arc<Session>,
|
||||
turn_extension_data: Arc<ExtensionData>,
|
||||
turn_trace_id_override: Option<String>,
|
||||
}
|
||||
|
||||
impl SessionTaskContext {
|
||||
pub(crate) fn new(session: Arc<Session>, turn_extension_data: Arc<ExtensionData>) -> Self {
|
||||
pub(crate) fn new(
|
||||
session: Arc<Session>,
|
||||
turn_extension_data: Arc<ExtensionData>,
|
||||
turn_trace_id_override: Option<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
session,
|
||||
turn_extension_data,
|
||||
turn_trace_id_override,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,10 +391,6 @@ impl Session {
|
||||
let turn = active.get_or_insert_with(ActiveTurn::default);
|
||||
debug_assert!(turn.task.is_none());
|
||||
let done_clone = Arc::clone(&done);
|
||||
let session_ctx = Arc::new(SessionTaskContext::new(
|
||||
Arc::clone(self),
|
||||
Arc::clone(&turn_extension_data),
|
||||
));
|
||||
let ctx = Arc::clone(&turn_context);
|
||||
let task_for_run = Arc::clone(&task);
|
||||
let task_input = input;
|
||||
@@ -415,6 +417,17 @@ impl Session {
|
||||
codex.turn.token_usage.reasoning_output_tokens = field::Empty,
|
||||
codex.turn.token_usage.total_tokens = field::Empty,
|
||||
);
|
||||
// A rooted automatic turn is created beneath the finishing turn, so its
|
||||
// captured context trace must not override the task span's fresh trace.
|
||||
let turn_trace_id_override = match span_parent {
|
||||
TurnSpanParent::Current => None,
|
||||
TurnSpanParent::Root => task_span.in_scope(codex_otel::current_span_trace_id),
|
||||
};
|
||||
let session_ctx = Arc::new(SessionTaskContext::new(
|
||||
Arc::clone(self),
|
||||
Arc::clone(&turn_extension_data),
|
||||
turn_trace_id_override,
|
||||
));
|
||||
let handle = tokio::spawn(
|
||||
async move {
|
||||
let ctx_for_finish = Arc::clone(&ctx);
|
||||
@@ -873,6 +886,7 @@ impl Session {
|
||||
let session_ctx = Arc::new(SessionTaskContext::new(
|
||||
Arc::clone(self),
|
||||
Arc::clone(&task.turn_extension_data),
|
||||
/*turn_trace_id_override*/ None,
|
||||
));
|
||||
session_task
|
||||
.abort(session_ctx, Arc::clone(&task.turn_context))
|
||||
|
||||
@@ -47,7 +47,10 @@ impl SessionTask for RegularTask {
|
||||
// not wait on startup prewarm resolution.
|
||||
let event = EventMsg::TurnStarted(TurnStartedEvent {
|
||||
turn_id: ctx.sub_id.clone(),
|
||||
trace_id: ctx.trace_id.clone(),
|
||||
trace_id: session
|
||||
.turn_trace_id_override
|
||||
.clone()
|
||||
.or_else(|| ctx.trace_id.clone()),
|
||||
started_at: ctx.turn_timing_state.started_at_unix_secs().await,
|
||||
model_context_window: ctx.model_context_window(),
|
||||
collaboration_mode_kind: ctx.collaboration_mode.mode,
|
||||
|
||||
Reference in New Issue
Block a user