@@ -401,7 +401,7 @@ enum Phase {
401401}
402402
403403struct PendingCall {
404- _tracing : Option < codex_otel:: sdk_trace:: ContextLease > ,
404+ _tracing : Option < Arc < codex_otel:: sdk_trace:: ContextLease > > ,
405405 invocation : u64 ,
406406 manual_request : Option < Value > ,
407407 sender : oneshot:: Sender < DynamicToolResponse > ,
@@ -1957,7 +1957,11 @@ impl PythonTools {
19571957 runtime_tool_call_id,
19581958 } => json ! ( { "cell_id" : cell_id, "nested_call_id" : runtime_tool_call_id} ) ,
19591959 } ;
1960- let tracing = codex_otel:: sdk_trace:: ContextLease :: capture ( ) ;
1960+ let tracing = if self . instance . is_some ( ) {
1961+ self . admission_ready ( turn) ?. tracing . clone ( )
1962+ } else {
1963+ codex_otel:: sdk_trace:: ContextLease :: capture ( ) . map ( Arc :: new)
1964+ } ;
19611965 let mut request = json ! ( {
19621966 "id" : call,
19631967 "msg" : {
@@ -2228,9 +2232,14 @@ impl TurnLifecycleContributor for PythonContributor {
22282232 // The default BeforeTaskRegistration phase includes compaction tasks.
22292233 fn on_turn_start < ' a > ( & ' a self , input : TurnStartInput < ' a > ) -> ExtensionFuture < ' a , ( ) > {
22302234 Box :: pin ( async move {
2231- let tracing = input
2232- . turn_store
2233- . get_or_init ( codex_otel:: sdk_trace:: CapturedContext :: capture) ;
2235+ let tracing = input. turn_store . get_or_init ( || {
2236+ input
2237+ . thread_store
2238+ . get :: < crate :: native_interception:: Control > ( )
2239+ . map_or_else ( codex_otel:: sdk_trace:: CapturedContext :: capture, |owner| {
2240+ owner. tracing_context ( )
2241+ } )
2242+ } ) ;
22342243 #[ cfg( test) ]
22352244 if let Some ( hold) = input. thread_store . get :: < admission_order_tests:: StartHold > ( ) {
22362245 hold. entered . add_permits ( 1 ) ;
@@ -2826,7 +2835,12 @@ impl ToolContributor for PythonContributor {
28262835 . read ( )
28272836 . expect ( "Step defaults poisoned" )
28282837 . clone ( ) ;
2829- let tracing = codex_otel:: sdk_trace:: ContextLease :: capture ( ) ;
2838+ let tracing = if tools. instance . is_some ( ) {
2839+ turn. get :: < codex_otel:: sdk_trace:: CapturedContext > ( )
2840+ . and_then ( |captured| captured. context . clone ( ) )
2841+ } else {
2842+ codex_otel:: sdk_trace:: ContextLease :: capture ( ) . map ( Arc :: new)
2843+ } ;
28302844 let mut request = json ! ( {
28312845 "id" : turn. level_id( ) ,
28322846 "msg" : { "type" : "_sdk_select_step" , "turn_id" : turn. level_id( ) ,
@@ -3173,8 +3187,15 @@ pub(crate) mod managed_tests {
31733187 }
31743188
31753189 async fn start_admission ( thread : & ExtensionData , turn_id : & str ) -> ExtensionData {
3190+ start_admission_with_store ( thread, turn_id, ExtensionData :: new ( turn_id) ) . await
3191+ }
3192+
3193+ async fn start_admission_with_store (
3194+ thread : & ExtensionData ,
3195+ turn_id : & str ,
3196+ turn : ExtensionData ,
3197+ ) -> ExtensionData {
31763198 let session = ExtensionData :: new ( "session" ) ;
3177- let turn = ExtensionData :: new ( turn_id) ;
31783199 let mode = serde_json:: from_value ( json ! ( {
31793200 "mode" : "default" , "settings" : {
31803201 "model" : "test" , "reasoning_effort" : null, "developer_instructions" : null
@@ -3214,38 +3235,92 @@ pub(crate) mod managed_tests {
32143235 return ;
32153236 }
32163237 codex_otel:: sdk_trace:: configure_pipeline ( false ) ;
3217- let original: serde_json:: Value = serde_json:: from_str ( & codex_otel:: sdk_trace:: start_trace_span (
3218- r#"{"name":"origin","kind":"workflow","trace_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","disabled":true,"include_sensitive_data":false}"# ,
3219- ) . unwrap ( ) ) . unwrap ( ) ;
3220- let handle = original[ "handle" ] . as_str ( ) . unwrap ( ) ;
3221- let attached = codex_otel:: sdk_trace:: context ( handle) . unwrap ( ) . attach ( ) ;
3222- let thread = admission_thread ( ) ;
3223- let turn = ExtensionData :: new ( "fixture" ) ;
3224- let control = crate :: native_interception:: tests:: bind_owner ( & thread, & turn) ;
3225- let turn = start_admission ( & thread, "original" ) . await ;
3226- let receipt = turn. get :: < Arc < TurnAdmission > > ( ) . unwrap ( ) ;
3227- let creation = control. creation_tracing ( ) . unwrap ( ) ;
3228- let tracing = receipt. tracing . as_ref ( ) . unwrap ( ) . clone ( ) ;
3229- drop ( attached) ;
3230- codex_otel:: sdk_trace:: end_trace_span ( handle, r#"{"status":"ok"}"# ) . unwrap ( ) ;
3231- codex_otel:: sdk_trace:: release_trace_span ( handle) ;
3232- let unrelated: serde_json:: Value = serde_json:: from_str ( & codex_otel:: sdk_trace:: start_trace_span (
3233- r#"{"name":"unrelated","kind":"workflow","trace_id":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","include_sensitive_data":true}"# ,
3234- ) . unwrap ( ) ) . unwrap ( ) ;
3235- let unrelated_handle = unrelated[ "handle" ] . as_str ( ) . unwrap ( ) ;
3236- let _unrelated = codex_otel:: sdk_trace:: context ( unrelated_handle)
3237- . unwrap ( )
3238- . attach ( ) ;
3239- for owner in [ creation, tracing] {
3240- let snapshot: serde_json:: Value = serde_json:: from_str (
3241- & codex_otel:: sdk_trace:: trace_span_context ( owner. token ( ) ) . unwrap ( ) ,
3238+ for ( disabled, sensitive) in [ ( true , true ) , ( false , false ) , ( true , false ) ] {
3239+ let original: serde_json:: Value = serde_json:: from_str (
3240+ & codex_otel:: sdk_trace:: start_trace_span (
3241+ & json ! ( {
3242+ "name" : "origin" , "kind" : "workflow" ,
3243+ "trace_id" : "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
3244+ "disabled" : disabled, "include_sensitive_data" : sensitive,
3245+ } )
3246+ . to_string ( ) ,
3247+ )
3248+ . unwrap ( ) ,
32423249 )
32433250 . unwrap ( ) ;
3244- assert_eq ! ( snapshot[ "trace_id" ] , "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) ;
3245- assert_eq ! ( snapshot[ "disabled" ] , true ) ;
3246- assert_eq ! ( snapshot[ "include_sensitive_data" ] , false ) ;
3251+ let handle = original[ "handle" ] . as_str ( ) . unwrap ( ) ;
3252+ let attached = codex_otel:: sdk_trace:: context ( handle) . unwrap ( ) . attach ( ) ;
3253+ let creation = codex_otel:: sdk_trace:: CapturedContext :: capture ( )
3254+ . context
3255+ . unwrap ( ) ;
3256+ let thread = admission_thread ( ) ;
3257+ crate :: native_interception:: tests:: bind_child_tracing_owner (
3258+ & thread,
3259+ Some ( creation. clone ( ) ) ,
3260+ ) ;
3261+ drop ( attached) ;
3262+ codex_otel:: sdk_trace:: end_trace_span ( handle, r#"{"status":"ok"}"# ) . unwrap ( ) ;
3263+ codex_otel:: sdk_trace:: release_trace_span ( handle) ;
3264+ let unrelated: serde_json:: Value = serde_json:: from_str (
3265+ & codex_otel:: sdk_trace:: start_trace_span (
3266+ r#"{"name":"unrelated","kind":"workflow","trace_id":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","include_sensitive_data":true}"# ,
3267+ ) . unwrap ( ) ,
3268+ ) . unwrap ( ) ;
3269+ let unrelated_handle = unrelated[ "handle" ] . as_str ( ) . unwrap ( ) ;
3270+ let unrelated_context = codex_otel:: sdk_trace:: context ( unrelated_handle)
3271+ . unwrap ( )
3272+ . attach ( ) ;
3273+ for id in [ "original" , "successor" ] {
3274+ let turn = start_admission ( & thread, id) . await ;
3275+ let tracing = turn
3276+ . get :: < codex_otel:: sdk_trace:: CapturedContext > ( )
3277+ . unwrap ( )
3278+ . context
3279+ . clone ( )
3280+ . unwrap ( ) ;
3281+ let receipt = turn. get :: < Arc < TurnAdmission > > ( ) . unwrap ( ) ;
3282+ assert ! ( Arc :: ptr_eq( & tracing, & creation) ) ;
3283+ assert ! ( Arc :: ptr_eq( receipt. tracing. as_ref( ) . unwrap( ) , & creation) ) ;
3284+ let snapshot: serde_json:: Value = serde_json:: from_str (
3285+ & codex_otel:: sdk_trace:: trace_span_context ( tracing. token ( ) ) . unwrap ( ) ,
3286+ )
3287+ . unwrap ( ) ;
3288+ assert_eq ! ( snapshot[ "trace_id" ] , "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) ;
3289+ assert_eq ! ( snapshot[ "disabled" ] , disabled) ;
3290+ assert_eq ! ( snapshot[ "include_sensitive_data" ] , sensitive) ;
3291+ }
3292+ // An existing turn carrier belongs to its own admission and must survive.
3293+ let turn = ExtensionData :: new ( "existing-carrier" ) ;
3294+ let explicit = codex_otel:: sdk_trace:: CapturedContext :: capture ( )
3295+ . context
3296+ . unwrap ( ) ;
3297+ turn. insert ( codex_otel:: sdk_trace:: CapturedContext {
3298+ context : Some ( explicit. clone ( ) ) ,
3299+ } ) ;
3300+ let turn = start_admission_with_store ( & thread, "existing-carrier" , turn) . await ;
3301+ assert ! ( Arc :: ptr_eq(
3302+ turn. get:: <Arc <TurnAdmission >>( )
3303+ . unwrap( )
3304+ . tracing
3305+ . as_ref( )
3306+ . unwrap( ) ,
3307+ & explicit
3308+ ) ) ;
3309+ // Explicitly absent child ownership cannot borrow this unrelated workflow.
3310+ let absent = admission_thread ( ) ;
3311+ crate :: native_interception:: tests:: bind_child_tracing_owner ( & absent, None ) ;
3312+ let turn = start_admission ( & absent, "untraced-child" ) . await ;
3313+ assert ! (
3314+ turn. get:: <codex_otel:: sdk_trace:: CapturedContext >( )
3315+ . unwrap( )
3316+ . context
3317+ . is_none( )
3318+ ) ;
3319+ assert ! ( turn. get:: <Arc <TurnAdmission >>( ) . unwrap( ) . tracing. is_none( ) ) ;
3320+ drop ( unrelated_context) ;
3321+ codex_otel:: sdk_trace:: end_trace_span ( unrelated_handle, r#"{"status":"ok"}"# ) . unwrap ( ) ;
3322+ codex_otel:: sdk_trace:: release_trace_span ( unrelated_handle) ;
32473323 }
3248- codex_otel:: sdk_trace:: release_trace_span ( unrelated_handle) ;
32493324 }
32503325
32513326 // Seed the real registered SDK contributors in their Core registration
0 commit comments