Skip to content

Commit b03f556

Browse files
Expose typed reasons for native output failures (#5520)
* Expose typed reasons for owned model output failures * Handle the expected parser disconnect in the reason fixture * Preserve received response history on current v1 * fix: preserve trace context on repaired history base * Fix CI assumptions about child snapshots and temporary directories * Preserve defined trace flags and manual action budgets * Sequence trace regression tool replies before final model output * feat: expose passive request metadata and response links (#5519) Observe passive request metadata on repaired history Co-authored-by: markstuart-oai <[email protected]> * Keep integrity reason tests within the native enum contract * Validate native trace prefix widths and fresh-root controls * Align manual continuation trace and observation arguments * Fix worker deadline fixtures and Windows storage type checks * Verify the winning reply when manual responses race * Check complete trace handles and both MCP callback parents * Observe child turns before testing unconfirmed cancellation * Respect supported platforms in SQLite trace fixtures * Distinguish caller exports from disabled child work * Preserve native child callback tracing ownership * Distinguish child trace exports and surface callback failures * Keep remote trace ancestry separate from private SDK policy * Preserve owned child tracing policy across native admission * Avoid callback scheduling assumptions in trace policy test * Separate session setup from concurrent trace policy admissions * Account for split trace processor batches in loop-loss test * Wait for a pending observation read before cancellation * Wait for asynchronous release command before checking child retirement --------- Co-authored-by: markstuart-oai <[email protected]>
1 parent 6d31986 commit b03f556

14 files changed

Lines changed: 1524 additions & 32 deletions

‎packages/codex/patches/0045-native-response-integrity-reasons.patch‎

Lines changed: 827 additions & 0 deletions
Large diffs are not rendered by default.

‎packages/codex/patches/series.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@
211211
{
212212
"path": "0051-w3c-defined-trace-flags.patch",
213213
"sha256": "b138e7672a1f3d2e20255598bab467d8b7d08f4b8f2415091f41474b8778d21c"
214+
},
215+
{
216+
"path": "0045-native-response-integrity-reasons.patch",
217+
"sha256": "2f149307ac7f3245cbf8b7e417c64c248acb198070ba7a46c5225eb0e93b487c"
214218
}
215219
],
216-
"result_tree": "88f56076d10d0b266a3ce93f6915e3629d0a07ac"
220+
"result_tree": "ff16323d830250635006360c65d7fb2821620eb3"
217221
}

‎packages/codex/rust/engine.rs‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,7 @@ fn native_event(
15531553
source: Option<(ThreadId, u64)>,
15541554
mut event: Event,
15551555
session_reader: bool,
1556+
project_integrity_reason: bool,
15561557
) -> anyhow::Result<serde_json::Value> {
15571558
// Session observations and stored items use Core's same retention limit.
15581559
// Keep the full native event for callers of the low-level raw reader.
@@ -1564,6 +1565,26 @@ fn native_event(
15641565
event.msg = retained;
15651566
}
15661567
let mut value = serde_json::to_value(&event)?;
1568+
if project_integrity_reason {
1569+
match &event.msg {
1570+
EventMsg::Error(error) => {
1571+
if let Some(reason) = error.response_integrity_reason {
1572+
value["msg"]["_sdk_response_integrity_reason"] = serde_json::json!(reason);
1573+
}
1574+
}
1575+
EventMsg::TurnComplete(done) => {
1576+
if let Some(reason) = done
1577+
.error
1578+
.as_ref()
1579+
.and_then(|error| error.response_integrity_reason)
1580+
{
1581+
value["msg"]["error"]["_sdk_response_integrity_reason"] =
1582+
serde_json::json!(reason);
1583+
}
1584+
}
1585+
_ => {}
1586+
}
1587+
}
15671588
if let Some((id, instance)) = source {
15681589
value["subagent_id"] = serde_json::json!(id);
15691590
value["subagent_instance_id"] = serde_json::json!(instance);
@@ -1704,7 +1725,21 @@ impl ThreadState {
17041725
} else {
17051726
None
17061727
};
1707-
let mut event = native_event(Some((source.0, source.1)), event, session_reader)?;
1728+
let project_reason = thread
1729+
.as_ref()
1730+
.and_then(|thread| {
1731+
thread
1732+
.thread_extension_data()
1733+
.get::<crate::python_tools::PythonTools>()
1734+
})
1735+
.and_then(|tools| tools.observed_admission(&event.id))
1736+
.is_some();
1737+
let mut event = native_event(
1738+
Some((source.0, source.1)),
1739+
event,
1740+
session_reader,
1741+
project_reason,
1742+
)?;
17081743
if let Some(grant) = question {
17091744
let tracing = grant.tracing_context();
17101745
if let Some(tracing) = &tracing {
@@ -1766,7 +1801,7 @@ impl ThreadState {
17661801
let mut events = self.events.lock().await;
17671802
ensure!(!*closing.borrow(), "Codex thread is closed");
17681803
if let Some(event) = events.initial.take() {
1769-
return native_event(None, event, admission_ready);
1804+
return native_event(None, event, admission_ready, false);
17701805
}
17711806
loop {
17721807
if let Some(metadata) = self.request_metadata.try_next() {
@@ -1786,7 +1821,12 @@ impl ThreadState {
17861821
{
17871822
settlement.delivered(&event.msg);
17881823
}
1789-
let mut value = native_event(None, event, admission_ready)?;
1824+
let mut value = native_event(
1825+
None,
1826+
event,
1827+
admission_ready,
1828+
receipt.permits_integrity_reason(),
1829+
)?;
17901830
receipt.attach_provider_error(&mut value);
17911831
if !value["msg"]["error"].is_null() {
17921832
receipt.attach_policy_failure(&mut value);
@@ -1835,7 +1875,9 @@ impl ThreadState {
18351875
.get::<crate::turn_settlement::Settlement>() {
18361876
settlement.delivered(&event.msg);
18371877
}
1838-
return native_event(None, event, admission_ready);
1878+
let project_reason = python_tools.as_ref()
1879+
.and_then(|tools| tools.observed_admission(&event.id)).is_some();
1880+
return native_event(None, event, admission_ready, project_reason);
18391881
},
18401882
(source, event) = read_child(children) => return self.child_event(source, event, admission_ready).await,
18411883
Some(event) = graph.recv() => {
@@ -1853,7 +1895,7 @@ impl ThreadState {
18531895
event = extensions.recv() => {
18541896
let (target, event) = event?;
18551897
if target.is_empty() || target == self.id.to_string() {
1856-
return native_event(None, event, admission_ready);
1898+
return native_event(None, event, admission_ready, false);
18571899
}
18581900
if let Ok(id) = ThreadId::from_string(&target) {
18591901
if !events.children.iter().any(|(child_id, _)| *child_id == id) {

‎packages/codex/rust/python_tools.rs‎

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ enum QueuedRequest {
142142
receipt: Weak<TurnAdmission>,
143143
thread: String,
144144
policy_message: Option<&'static str>,
145+
response_integrity_reason: Option<codex_protocol::error::ResponseIntegrityReason>,
145146
},
146147
}
147148

@@ -227,6 +228,8 @@ pub(crate) struct TurnAdmission {
227228
steering_tail: AtomicBool,
228229
exact_steering: OnceLock<bool>,
229230
provider_error: Mutex<Option<(Value, Value)>>,
231+
// An unseeded event cannot distinguish a queued old same-ID lifecycle.
232+
same_id_recovery: bool,
230233
policy_failure: OnceLock<&'static str>,
231234
}
232235

@@ -237,6 +240,10 @@ tokio::task_local! {
237240
}
238241

239242
impl TurnAdmission {
243+
pub(crate) fn permits_integrity_reason(&self) -> bool {
244+
!self.same_id_recovery
245+
}
246+
240247
pub(crate) fn is_current(self: &Arc<Self>) -> bool {
241248
self.owner
242249
.upgrade()
@@ -278,7 +285,11 @@ impl TurnAdmission {
278285
}
279286
}
280287

281-
pub(crate) fn reject_model(self: &Arc<Self>, thread: &str) {
288+
pub(crate) fn reject_model(
289+
self: &Arc<Self>,
290+
thread: &str,
291+
reason: codex_protocol::error::ResponseIntegrityReason,
292+
) {
282293
if let Some(owner) = self.owner.upgrade() {
283294
// Core drains tool futures before reporting the model error. Wake
284295
// the SDK's existing failure settlement so it can interrupt the
@@ -288,6 +299,7 @@ impl TurnAdmission {
288299
receipt: Arc::downgrade(self),
289300
thread: thread.to_owned(),
290301
policy_message: None,
302+
response_integrity_reason: Some(reason),
291303
});
292304
}
293305
}
@@ -302,6 +314,7 @@ impl TurnAdmission {
302314
receipt: Arc::downgrade(self),
303315
thread: thread.to_owned(),
304316
policy_message: Some(message),
317+
response_integrity_reason: None,
305318
});
306319
}
307320
}
@@ -933,6 +946,15 @@ impl PythonTools {
933946
Ok(())
934947
}
935948

949+
pub(crate) fn observed_admission(&self, turn_id: &str) -> Option<Arc<TurnAdmission>> {
950+
self.admission
951+
.lock()
952+
.expect("Python admission receipt poisoned")
953+
.as_ref()
954+
.filter(|receipt| receipt.turn_id == turn_id && receipt.permits_integrity_reason())
955+
.cloned()
956+
}
957+
936958
pub(crate) fn admission_ready(&self, turn_id: &str) -> Result<Arc<TurnAdmission>> {
937959
self.admission
938960
.lock()
@@ -1840,6 +1862,7 @@ impl PythonTools {
18401862
receipt,
18411863
thread,
18421864
policy_message,
1865+
response_integrity_reason,
18431866
}) => {
18441867
if let (Some(owner), Some(receipt)) = (owner.upgrade(), receipt.upgrade())
18451868
&& Arc::ptr_eq(&self.family, &owner.family)
@@ -1860,6 +1883,7 @@ impl PythonTools {
18601883
"turn_id": receipt.turn_id,
18611884
"message": policy_message.unwrap_or(crate::response_integrity::UNSUPPORTED_OUTPUT),
18621885
"policy_stop": policy_message.is_some(),
1886+
"response_integrity_reason": response_integrity_reason,
18631887
}
18641888
});
18651889
if let Some(instance) = &owner.instance {
@@ -2248,6 +2272,13 @@ impl TurnLifecycleContributor for PythonContributor {
22482272
false
22492273
};
22502274
let graph = tools.family.graph.lock().expect("Python graph poisoned");
2275+
let mut admission = tools
2276+
.admission
2277+
.lock()
2278+
.expect("Python admission receipt poisoned");
2279+
let same_id_recovery = admission
2280+
.as_ref()
2281+
.is_some_and(|prior| prior.turn_id == input.turn_id);
22512282
let receipt = Arc::new(TurnAdmission {
22522283
tracing: tracing.context.clone(),
22532284
turn_id: input.turn_id.to_owned(),
@@ -2268,16 +2299,12 @@ impl TurnLifecycleContributor for PythonContributor {
22682299
steering_tail: AtomicBool::new(false),
22692300
exact_steering: OnceLock::new(),
22702301
provider_error: Mutex::default(),
2302+
same_id_recovery,
22712303
policy_failure: OnceLock::new(),
22722304
});
22732305
crate::native_interception::attach_sdk_admission(input.turn_store, &receipt);
22742306
input.turn_store.insert(receipt.clone());
2275-
if let Some(previous) = tools
2276-
.admission
2277-
.lock()
2278-
.expect("Python admission receipt poisoned")
2279-
.replace(receipt)
2280-
{
2307+
if let Some(previous) = admission.replace(receipt) {
22812308
previous.graceful_requested.store(false, Ordering::Release);
22822309
previous.state.send_if_modified(|state| {
22832310
if *state == AdmissionState::Pending {
@@ -2288,6 +2315,7 @@ impl TurnLifecycleContributor for PythonContributor {
22882315
}
22892316
});
22902317
}
2318+
drop(admission);
22912319
tools.family.graph_changed.send_replace(());
22922320
}
22932321
})
@@ -3679,6 +3707,54 @@ pub(crate) mod managed_tests {
36793707
assert!(receipt.wait().await.is_err());
36803708
}
36813709

3710+
#[tokio::test]
3711+
async fn integrity_reason_projection_refuses_a_same_id_recovery_receipt() {
3712+
let thread = admission_thread();
3713+
let tools = thread.get::<PythonTools>().unwrap();
3714+
let _first = start_admission(&thread, "same-id").await;
3715+
let receipt = tools.observed_admission("same-id").unwrap();
3716+
assert!(receipt.permits_integrity_reason());
3717+
let _second = start_admission(&thread, "same-id").await;
3718+
assert!(tools.observed_admission("same-id").is_none());
3719+
assert!(
3720+
!tools
3721+
.admission_ready("same-id")
3722+
.unwrap()
3723+
.permits_integrity_reason()
3724+
);
3725+
}
3726+
3727+
#[tokio::test]
3728+
async fn model_failure_reason_cannot_follow_same_id_receipt_replacement() {
3729+
use codex_protocol::error::ResponseIntegrityReason;
3730+
let thread = admission_thread();
3731+
let tools = thread.get::<PythonTools>().unwrap();
3732+
tools
3733+
.family
3734+
.members
3735+
.write()
3736+
.unwrap()
3737+
.insert("thread".into(), Arc::downgrade(&tools));
3738+
let _first_store = start_admission(&thread, "same-id").await;
3739+
let first = tools.admission_ready("same-id").unwrap();
3740+
first.reject_model("thread", ResponseIntegrityReason::UndecodableOutputItem);
3741+
let _second_store = start_admission(&thread, "same-id").await;
3742+
let second = tools.admission_ready("same-id").unwrap();
3743+
assert!(!Arc::ptr_eq(&first, &second));
3744+
let mut read = Box::pin(tools.next_request());
3745+
assert!(futures_util::poll!(&mut read).is_pending());
3746+
second.reject_model("thread", ResponseIntegrityReason::UnfinishedAssistantItem);
3747+
let notice = read.await;
3748+
assert_eq!(
3749+
notice["msg"]["response_integrity_reason"],
3750+
"unfinished_assistant_item"
3751+
);
3752+
assert_eq!(
3753+
notice["msg"]["message"],
3754+
crate::response_integrity::UNSUPPORTED_OUTPUT
3755+
);
3756+
}
3757+
36823758
#[tokio::test]
36833759
async fn graceful_request_and_failure_cannot_follow_same_id_replacement() {
36843760
let thread = admission_thread();

‎packages/codex/rust/response_integrity.rs‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use codex_extension_api::{
88
ModelRequestContributor, ModelRequestInput, ModelRequestKind, ModelResponseError,
99
ModelResponseInterceptor, ModelResponseStream, ResponseEvent,
1010
};
11+
use codex_protocol::error::ResponseIntegrityReason;
1112
use codex_protocol::models::{ContentItem, ResponseItem};
1213
use futures_util::Stream;
1314

@@ -87,11 +88,12 @@ impl Stream for Generation {
8788
// provider. Unpaired external context and server search
8889
// outputs belong to Core and still pass through.
8990
this.rejected = true;
91+
let reason = ResponseIntegrityReason::ProviderOwnedToolReceipt;
9092
if let Some(receipt) = &this.owner.admission {
91-
receipt.reject_model(&this.owner.thread);
93+
receipt.reject_model(&this.owner.thread, reason);
9294
}
93-
return Poll::Ready(Some(Err(ModelResponseError::InvalidRequest {
94-
message: UNSUPPORTED_OUTPUT.into(),
95+
return Poll::Ready(Some(Err(ModelResponseError::ResponseIntegrity {
96+
reason,
9597
})));
9698
}
9799
ResponseEvent::OutputItemAdded(ResponseItem::Message { role, .. })
@@ -150,11 +152,16 @@ impl Stream for Generation {
150152
// output. Never claim the response was empty success. Do
151153
// not include model text, IDs, or provider details in errors.
152154
this.rejected = true;
155+
let reason = if this.pending_assistant {
156+
ResponseIntegrityReason::UnfinishedAssistantItem
157+
} else {
158+
ResponseIntegrityReason::MissingSupportedTerminalOutput
159+
};
153160
if let Some(receipt) = &this.owner.admission {
154-
receipt.reject_model(&this.owner.thread);
161+
receipt.reject_model(&this.owner.thread, reason);
155162
}
156-
return Poll::Ready(Some(Err(ModelResponseError::InvalidRequest {
157-
message: UNSUPPORTED_OUTPUT.into(),
163+
return Poll::Ready(Some(Err(ModelResponseError::ResponseIntegrity {
164+
reason,
158165
})));
159166
}
160167
_ => {}

‎src/agents/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
InteractionExpiredError as InteractionExpiredError,
8989
InteractionRequired as InteractionRequired,
9090
NativeError as NativeError,
91+
ResponseIntegrityReason as ResponseIntegrityReason,
9192
Session as Session,
9293
SessionBusyError as SessionBusyError,
9394
StaleTurnError as StaleTurnError,
@@ -201,6 +202,7 @@
201202
"TurnStoppedError": ".session",
202203
"InteractionRequired": ".session",
203204
"NativeError": ".session",
205+
"ResponseIntegrityReason": ".session",
204206
"Session": ".session",
205207
"SessionBusyError": ".session",
206208
"Turn": ".turn",

0 commit comments

Comments
 (0)