@@ -388,7 +388,7 @@ enum Phase {
388388}
389389
390390struct PendingCall {
391- _tracing : Option < codex_otel:: sdk_trace:: ContextLease > ,
391+ _tracing : Option < Arc < codex_otel:: sdk_trace:: ContextLease > > ,
392392 invocation : u64 ,
393393 manual_request : Option < Value > ,
394394 sender : oneshot:: Sender < DynamicToolResponse > ,
@@ -1933,7 +1933,11 @@ impl PythonTools {
19331933 runtime_tool_call_id,
19341934 } => json ! ( { "cell_id" : cell_id, "nested_call_id" : runtime_tool_call_id} ) ,
19351935 } ;
1936- let tracing = codex_otel:: sdk_trace:: ContextLease :: capture ( ) ;
1936+ let tracing = if self . instance . is_some ( ) {
1937+ self . admission_ready ( turn) ?. tracing . clone ( )
1938+ } else {
1939+ codex_otel:: sdk_trace:: ContextLease :: capture ( ) . map ( Arc :: new)
1940+ } ;
19371941 let mut request = json ! ( {
19381942 "id" : call,
19391943 "msg" : {
@@ -2204,9 +2208,14 @@ impl TurnLifecycleContributor for PythonContributor {
22042208 // The default BeforeTaskRegistration phase includes compaction tasks.
22052209 fn on_turn_start < ' a > ( & ' a self , input : TurnStartInput < ' a > ) -> ExtensionFuture < ' a , ( ) > {
22062210 Box :: pin ( async move {
2207- let tracing = input
2208- . turn_store
2209- . get_or_init ( codex_otel:: sdk_trace:: CapturedContext :: capture) ;
2211+ let tracing = input. turn_store . get_or_init ( || {
2212+ input
2213+ . thread_store
2214+ . get :: < crate :: native_interception:: Control > ( )
2215+ . map_or_else ( codex_otel:: sdk_trace:: CapturedContext :: capture, |owner| {
2216+ owner. tracing_context ( )
2217+ } )
2218+ } ) ;
22102219 #[ cfg( test) ]
22112220 if let Some ( hold) = input. thread_store . get :: < admission_order_tests:: StartHold > ( ) {
22122221 hold. entered . add_permits ( 1 ) ;
@@ -2798,7 +2807,12 @@ impl ToolContributor for PythonContributor {
27982807 . read ( )
27992808 . expect ( "Step defaults poisoned" )
28002809 . clone ( ) ;
2801- let tracing = codex_otel:: sdk_trace:: ContextLease :: capture ( ) ;
2810+ let tracing = if tools. instance . is_some ( ) {
2811+ turn. get :: < codex_otel:: sdk_trace:: CapturedContext > ( )
2812+ . and_then ( |captured| captured. context . clone ( ) )
2813+ } else {
2814+ codex_otel:: sdk_trace:: ContextLease :: capture ( ) . map ( Arc :: new)
2815+ } ;
28022816 let mut request = json ! ( {
28032817 "id" : turn. level_id( ) ,
28042818 "msg" : { "type" : "_sdk_select_step" , "turn_id" : turn. level_id( ) ,
@@ -3145,8 +3159,15 @@ pub(crate) mod managed_tests {
31453159 }
31463160
31473161 async fn start_admission ( thread : & ExtensionData , turn_id : & str ) -> ExtensionData {
3162+ start_admission_with_store ( thread, turn_id, ExtensionData :: new ( turn_id) ) . await
3163+ }
3164+
3165+ async fn start_admission_with_store (
3166+ thread : & ExtensionData ,
3167+ turn_id : & str ,
3168+ turn : ExtensionData ,
3169+ ) -> ExtensionData {
31483170 let session = ExtensionData :: new ( "session" ) ;
3149- let turn = ExtensionData :: new ( turn_id) ;
31503171 let mode = serde_json:: from_value ( json ! ( {
31513172 "mode" : "default" , "settings" : {
31523173 "model" : "test" , "reasoning_effort" : null, "developer_instructions" : null
@@ -3186,38 +3207,92 @@ pub(crate) mod managed_tests {
31863207 return ;
31873208 }
31883209 codex_otel:: sdk_trace:: configure_pipeline ( false ) ;
3189- let original: serde_json:: Value = serde_json:: from_str ( & codex_otel:: sdk_trace:: start_trace_span (
3190- r#"{"name":"origin","kind":"workflow","trace_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","disabled":true,"include_sensitive_data":false}"# ,
3191- ) . unwrap ( ) ) . unwrap ( ) ;
3192- let handle = original[ "handle" ] . as_str ( ) . unwrap ( ) ;
3193- let attached = codex_otel:: sdk_trace:: context ( handle) . unwrap ( ) . attach ( ) ;
3194- let thread = admission_thread ( ) ;
3195- let turn = ExtensionData :: new ( "fixture" ) ;
3196- let control = crate :: native_interception:: tests:: bind_owner ( & thread, & turn) ;
3197- let turn = start_admission ( & thread, "original" ) . await ;
3198- let receipt = turn. get :: < Arc < TurnAdmission > > ( ) . unwrap ( ) ;
3199- let creation = control. creation_tracing ( ) . unwrap ( ) ;
3200- let tracing = receipt. tracing . as_ref ( ) . unwrap ( ) . clone ( ) ;
3201- drop ( attached) ;
3202- codex_otel:: sdk_trace:: end_trace_span ( handle, r#"{"status":"ok"}"# ) . unwrap ( ) ;
3203- codex_otel:: sdk_trace:: release_trace_span ( handle) ;
3204- let unrelated: serde_json:: Value = serde_json:: from_str ( & codex_otel:: sdk_trace:: start_trace_span (
3205- r#"{"name":"unrelated","kind":"workflow","trace_id":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","include_sensitive_data":true}"# ,
3206- ) . unwrap ( ) ) . unwrap ( ) ;
3207- let unrelated_handle = unrelated[ "handle" ] . as_str ( ) . unwrap ( ) ;
3208- let _unrelated = codex_otel:: sdk_trace:: context ( unrelated_handle)
3209- . unwrap ( )
3210- . attach ( ) ;
3211- for owner in [ creation, tracing] {
3212- let snapshot: serde_json:: Value = serde_json:: from_str (
3213- & codex_otel:: sdk_trace:: trace_span_context ( owner. token ( ) ) . unwrap ( ) ,
3210+ for ( disabled, sensitive) in [ ( true , true ) , ( false , false ) , ( true , false ) ] {
3211+ let original: serde_json:: Value = serde_json:: from_str (
3212+ & codex_otel:: sdk_trace:: start_trace_span (
3213+ & json ! ( {
3214+ "name" : "origin" , "kind" : "workflow" ,
3215+ "trace_id" : "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
3216+ "disabled" : disabled, "include_sensitive_data" : sensitive,
3217+ } )
3218+ . to_string ( ) ,
3219+ )
3220+ . unwrap ( ) ,
32143221 )
32153222 . unwrap ( ) ;
3216- assert_eq ! ( snapshot[ "trace_id" ] , "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) ;
3217- assert_eq ! ( snapshot[ "disabled" ] , true ) ;
3218- assert_eq ! ( snapshot[ "include_sensitive_data" ] , false ) ;
3223+ let handle = original[ "handle" ] . as_str ( ) . unwrap ( ) ;
3224+ let attached = codex_otel:: sdk_trace:: context ( handle) . unwrap ( ) . attach ( ) ;
3225+ let creation = codex_otel:: sdk_trace:: CapturedContext :: capture ( )
3226+ . context
3227+ . unwrap ( ) ;
3228+ let thread = admission_thread ( ) ;
3229+ crate :: native_interception:: tests:: bind_child_tracing_owner (
3230+ & thread,
3231+ Some ( creation. clone ( ) ) ,
3232+ ) ;
3233+ drop ( attached) ;
3234+ codex_otel:: sdk_trace:: end_trace_span ( handle, r#"{"status":"ok"}"# ) . unwrap ( ) ;
3235+ codex_otel:: sdk_trace:: release_trace_span ( handle) ;
3236+ let unrelated: serde_json:: Value = serde_json:: from_str (
3237+ & codex_otel:: sdk_trace:: start_trace_span (
3238+ r#"{"name":"unrelated","kind":"workflow","trace_id":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","include_sensitive_data":true}"# ,
3239+ ) . unwrap ( ) ,
3240+ ) . unwrap ( ) ;
3241+ let unrelated_handle = unrelated[ "handle" ] . as_str ( ) . unwrap ( ) ;
3242+ let unrelated_context = codex_otel:: sdk_trace:: context ( unrelated_handle)
3243+ . unwrap ( )
3244+ . attach ( ) ;
3245+ for id in [ "original" , "successor" ] {
3246+ let turn = start_admission ( & thread, id) . await ;
3247+ let tracing = turn
3248+ . get :: < codex_otel:: sdk_trace:: CapturedContext > ( )
3249+ . unwrap ( )
3250+ . context
3251+ . clone ( )
3252+ . unwrap ( ) ;
3253+ let receipt = turn. get :: < Arc < TurnAdmission > > ( ) . unwrap ( ) ;
3254+ assert ! ( Arc :: ptr_eq( & tracing, & creation) ) ;
3255+ assert ! ( Arc :: ptr_eq( receipt. tracing. as_ref( ) . unwrap( ) , & creation) ) ;
3256+ let snapshot: serde_json:: Value = serde_json:: from_str (
3257+ & codex_otel:: sdk_trace:: trace_span_context ( tracing. token ( ) ) . unwrap ( ) ,
3258+ )
3259+ . unwrap ( ) ;
3260+ assert_eq ! ( snapshot[ "trace_id" ] , "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ) ;
3261+ assert_eq ! ( snapshot[ "disabled" ] , disabled) ;
3262+ assert_eq ! ( snapshot[ "include_sensitive_data" ] , sensitive) ;
3263+ }
3264+ // An existing turn carrier belongs to its own admission and must survive.
3265+ let turn = ExtensionData :: new ( "existing-carrier" ) ;
3266+ let explicit = codex_otel:: sdk_trace:: CapturedContext :: capture ( )
3267+ . context
3268+ . unwrap ( ) ;
3269+ turn. insert ( codex_otel:: sdk_trace:: CapturedContext {
3270+ context : Some ( explicit. clone ( ) ) ,
3271+ } ) ;
3272+ let turn = start_admission_with_store ( & thread, "existing-carrier" , turn) . await ;
3273+ assert ! ( Arc :: ptr_eq(
3274+ turn. get:: <Arc <TurnAdmission >>( )
3275+ . unwrap( )
3276+ . tracing
3277+ . as_ref( )
3278+ . unwrap( ) ,
3279+ & explicit
3280+ ) ) ;
3281+ // Explicitly absent child ownership cannot borrow this unrelated workflow.
3282+ let absent = admission_thread ( ) ;
3283+ crate :: native_interception:: tests:: bind_child_tracing_owner ( & absent, None ) ;
3284+ let turn = start_admission ( & absent, "untraced-child" ) . await ;
3285+ assert ! (
3286+ turn. get:: <codex_otel:: sdk_trace:: CapturedContext >( )
3287+ . unwrap( )
3288+ . context
3289+ . is_none( )
3290+ ) ;
3291+ assert ! ( turn. get:: <Arc <TurnAdmission >>( ) . unwrap( ) . tracing. is_none( ) ) ;
3292+ drop ( unrelated_context) ;
3293+ codex_otel:: sdk_trace:: end_trace_span ( unrelated_handle, r#"{"status":"ok"}"# ) . unwrap ( ) ;
3294+ codex_otel:: sdk_trace:: release_trace_span ( unrelated_handle) ;
32193295 }
3220- codex_otel:: sdk_trace:: release_trace_span ( unrelated_handle) ;
32213296 }
32223297
32233298 // Seed the real registered SDK contributors in their Core registration
0 commit comments