Skip to content

Commit a5ca515

Browse files
Merge child callback tracing and shared fixture repairs
2 parents 4e6789b + 834da45 commit a5ca515

5 files changed

Lines changed: 209 additions & 49 deletions

File tree

‎packages/codex/rust/native_interception.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,17 @@ impl Control {
540540
self.creation_tracing.get().cloned().flatten()
541541
}
542542

543+
pub(crate) fn tracing_context(&self) -> codex_otel::sdk_trace::CapturedContext {
544+
if self.native_ancestor.is_some() {
545+
// A child keeps its creation owner, including an absent tracing scope.
546+
codex_otel::sdk_trace::CapturedContext {
547+
context: self.creation_tracing(),
548+
}
549+
} else {
550+
codex_otel::sdk_trace::CapturedContext::capture()
551+
}
552+
}
553+
543554
// Lifecycle callbacks retain the unpublished receiver if its opening future
544555
// is cancelled. Cleanup always joins this exact native session.
545556
async fn close_pending_code_import(&self, turn: Option<&Arc<DirectTurn>>) -> Result<()> {
@@ -4033,6 +4044,16 @@ pub(crate) mod tests {
40334044
control
40344045
}
40354046

4047+
pub(crate) fn bind_child_tracing_owner(
4048+
thread: &ExtensionData,
4049+
tracing: Option<Arc<codex_otel::sdk_trace::ContextLease>>,
4050+
) {
4051+
let parent = Control::new("tracing-parent".into(), true);
4052+
let control = Control::descendant(thread.level_id().to_owned(), &parent);
4053+
let _ = control.creation_tracing.set(tracing);
4054+
thread.insert(control);
4055+
}
4056+
40364057
#[test]
40374058
fn pinned_executor_schema_requires_fields_and_rejects_wrong_types() {
40384059
let admission = PinnedExecutorAdmission {

‎packages/codex/rust/python_tools.rs‎

Lines changed: 111 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ enum Phase {
388388
}
389389

390390
struct 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

‎tests/test_native_child_cancel_join.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ async def _cancel_child_turn(self, handle: Any) -> bool:
452452
pending = None
453453
try:
454454
await (await session.submit("Start both children")).get_result()
455+
# Observe the child turn before releasing its callback.
456+
await handles(session, 2)
455457
peer.release["alpha"].set()
456458
await entered.wait()
457459
alpha_id = peer.child_metadata["alpha"][0]["thread_id"]

‎tests/test_native_code_manual.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@
3434
from tests.test_native_workflow_persistence import model_restore_agent
3535

3636

37+
@pytest.mark.parametrize(
38+
"replies",
39+
[("accepted-a", "duplicate"), ("duplicate", "accepted-a")],
40+
ids=["primary-first", "duplicate-first"],
41+
)
3742
async def test_live_manual_code_can_replace_automatic_and_cannot_export_its_stack(
3843
responses: ResponsesFixture,
44+
replies: tuple[str, str],
3945
) -> None:
4046
automatic: list[str] = []
4147

@@ -92,17 +98,19 @@ def lookup(key: str) -> str:
9298
with pytest.raises(ValueError, match="validation"):
9399
await by_key["a"].respond(7)
94100
competing = await asyncio.gather(
95-
by_key["a"].respond("accepted-a"),
96-
by_key["a"].respond("duplicate"),
101+
*(by_key["a"].respond(reply) for reply in replies),
97102
return_exceptions=True,
98103
)
99104
assert sum(result is None for result in competing) == 1
100105
assert sum(isinstance(result, InteractionExpiredError) for result in competing) == 1
101106
assert await by_key["b"].respond("accepted-b") is None
102107
assert (await (original).get_result()).final_output == "both accepted"
103108
rendered = code_output(responses.requests[1], "same-outer")
104-
assert "accepted-a" in rendered and "accepted-b" in rendered
105-
assert "duplicate" not in rendered and automatic == []
109+
winning_reply = next(
110+
reply for reply, result in zip(replies, competing, strict=True) if result is None
111+
)
112+
assert json.loads(rendered.split("RESULT:", 1)[1]) == [winning_reply, "accepted-b"]
113+
assert automatic == []
106114
assert (await session.save_state()).format_version == 6
107115

108116
responses.respond(code_call("text(await tools.lookup({key:'fresh'}));", "same-outer"))

‎tests/test_tracing_native.py‎

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,10 @@ async def transform(context: Any, value: dict[str, Any]) -> dict[str, Any]:
486486
children = [event for event in ended if event["name"] == "MCP policy child"]
487487
assert len(callbacks) == len(children) == 1
488488
assert callbacks[0]["trace_id"] == "a" * 32
489-
assert children[0]["parent_span_id"] == callbacks[0]["span_id"]
489+
active_callbacks = [event for event in ended if event["span_id"] == seen[0][0].span_id]
490+
assert len(active_callbacks) == 1 and active_callbacks[0]["name"] == "callback"
491+
assert active_callbacks[0]["parent_span_id"] == callbacks[0]["span_id"]
492+
assert children[0]["parent_span_id"] == active_callbacks[0]["span_id"]
490493
assert not any(event["trace_id"] == "b" * 32 for event in recorder.events)
491494

492495

@@ -575,7 +578,9 @@ def check_restored_context(self) -> Arguments:
575578

576579
# Native history can retain historical trace IDs. It must not retain a
577580
# live scope lease or the process's exporter credentials/routing context.
578-
assert source_scope.context._handle not in serialized
581+
# Registry handles are short tokens, so exclude the complete JSON value
582+
# rather than coincidental substrings of retained revision/content hashes.
583+
assert json.dumps(source_scope.context._handle) not in serialized
579584
assert "tracing_context" not in serialized
580585
assert "credential_ref" not in serialized
581586
assert "default-key" not in serialized
@@ -1231,20 +1236,48 @@ def response(original=original, fresh=fresh, steer=steer):
12311236
disabled=mode == "origin_disabled",
12321237
include_sensitive_data=True,
12331238
),
1234-
):
1239+
) as creation_scope:
12351240
initial = await session.submit("Create child")
12361241
assert (await initial.get_result()).final_output == "root complete"
1237-
await model.entered["alpha"].wait()
12381242
[first] = await handles(session, 1)
1243+
initial_wait = asyncio.create_task(first.get_result())
1244+
entered_wait = asyncio.create_task(model.entered["alpha"].wait())
1245+
ready, _ = await asyncio.wait(
1246+
(initial_wait, entered_wait), return_when=asyncio.FIRST_COMPLETED
1247+
)
1248+
if initial_wait in ready:
1249+
entered_wait.cancel()
1250+
await asyncio.gather(entered_wait, return_exceptions=True)
1251+
try:
1252+
await initial_wait
1253+
except Exception as error:
1254+
raise AssertionError(
1255+
(
1256+
mode,
1257+
len(model.requests),
1258+
seen,
1259+
"child failed before its response hold",
1260+
)
1261+
) from error
1262+
raise AssertionError(
1263+
(
1264+
mode,
1265+
len(model.requests),
1266+
seen,
1267+
"child completed before its response hold",
1268+
)
1269+
)
12391270
with telemetry.trace(
12401271
"unrelated child caller",
12411272
options=telemetry.TraceOptions(
12421273
trace_id="f" * 32, include_sensitive_data=True
12431274
),
12441275
):
1245-
await first.steer(steer)
1246-
model.release["alpha"].set()
1247-
assert (await first.get_result()).final_output == "alpha complete"
1276+
try:
1277+
await first.steer(steer)
1278+
finally:
1279+
model.release["alpha"].set()
1280+
assert (await initial_wait).final_output == "alpha complete"
12481281
[child] = [child async for child in session.subagents()]
12491282
later = await child.start(fresh)
12501283
assert later.turn_id != first.turn_id
@@ -1254,10 +1287,31 @@ def response(original=original, fresh=fresh, steer=steer):
12541287
row[1:] == (origin_id, disabled, mode != "metadata") for row in seen
12551288
), seen
12561289
assert (await telemetry.force_flush(timeout_ms=5000)).status == "success"
1290+
origin_events = [
1291+
event for event in recorder.events if event["trace_id"] == origin_id
1292+
]
1293+
caller_events = [
1294+
event
1295+
for event in origin_events
1296+
if event["span_id"] == creation_scope.context.span_id
1297+
]
1298+
# The independent caller workflow may record even when the
1299+
# session/child is disabled. It does not grant child export.
1300+
assert bool(caller_events) == (mode != "origin_disabled")
1301+
if caller_events:
1302+
assert {event["lifecycle"] for event in caller_events} == {"start", "end"}
1303+
assert all(
1304+
event["name"] == "child creation"
1305+
and event["parent_span_id"] == creation_scope.context.parent_span_id
1306+
for event in caller_events
1307+
)
12571308
if disabled:
1258-
assert not any(event["trace_id"] == origin_id for event in recorder.events)
1309+
assert origin_events == caller_events
12591310
else:
1260-
assert any(event["trace_id"] == origin_id for event in recorder.events)
1311+
assert any(
1312+
event["span_id"] != creation_scope.context.span_id
1313+
for event in origin_events
1314+
)
12611315
assert fresh not in json.dumps(peer.records)
12621316
assert steer not in json.dumps(peer.records)
12631317

0 commit comments

Comments
 (0)