Distinguish Guardian parent-compaction failure reasons (#53393)

## Why

The generic `parent_compaction_error` classification metric does not distinguish serialization failures, oversized compactions, and unusable compactions.

## What changed

Record separate `failure_reason` values for each case: `parent_compaction_serialization`, `parent_compaction_oversized`, and `parent_compaction_unusable`.

## Testing

Extend parent-compaction reuse tests to cover unusable checkpoints in parent-context mode and assert specific failure reasons. Verify rejected compactions receive the maximum risk score, provide no cached approval, and do not reuse stale context.

GitOrigin-RevId: 103c4173b2ebbdc33eb71bce7f8ecb56f73055f6
This commit is contained in:
ioanbudea-OAI
2026-10-12 14:16:27 +00:00
committed by copyberry
parent 27e6192e84
commit cab328f275
2 changed files with 98 additions and 65 deletions
@@ -3389,67 +3389,91 @@ async fn assert_parent_compaction_reuse(parent_context_for_review: bool) -> Resu
encrypted_content: "a".repeat(TruncationPolicy::Tokens(/*limit*/ 256).byte_budget()),
internal_chat_message_metadata_passthrough: None,
};
registry.tool_lifecycle_contributors()[0]
.on_tool_start(ToolStartInput {
permissions: Box::pin(async { Some(Default::default()) }),
session_store: &session_store,
thread_store,
turn_store: &turn_store,
turn_id: "turn-1",
root_turn_id: None,
call_id: "call-2",
originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &tool_payload,
conversation_history: Arc::new(TestRetainedHistory {
current: TestConversationHistory(vec![latest_compaction, oversized_compaction]),
retained: Vec::new(),
retained_context: parent_context_for_review
.then(codex_history::RetainedContext::default),
compaction_model_hash: thread_store
.get::<ModelInfo>()
.and_then(|model| model.comp_hash.clone()),
review_context_revision,
}),
source: ToolCallSource::Direct,
})
.await;
let mut rejected_compactions = vec![(oversized_compaction, "parent_compaction_oversized")];
// Legacy context can omit unusable checkpoints and still review its raw history.
if parent_context_for_review {
rejected_compactions.push((
ResponseItem::ContextCompaction {
id: Some(ResponseItemId::from_server("cmp_unusable".to_owned())),
encrypted_content: None,
internal_chat_message_metadata_passthrough: None,
},
"parent_compaction_unusable",
));
}
for (index, (rejected_compaction, failure_reason)) in
rejected_compactions.into_iter().enumerate()
{
let call_id = format!("call-{}", index + 2);
registry.tool_lifecycle_contributors()[0]
.on_tool_start(ToolStartInput {
permissions: Box::pin(async { Some(Default::default()) }),
session_store: &session_store,
thread_store,
turn_store: &turn_store,
turn_id: "turn-1",
root_turn_id: None,
call_id: &call_id,
originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &tool_payload,
conversation_history: Arc::new(TestRetainedHistory {
current: TestConversationHistory(vec![
latest_compaction.clone(),
rejected_compaction,
]),
retained: Vec::new(),
retained_context: parent_context_for_review
.then(codex_history::RetainedContext::default),
compaction_model_hash: thread_store
.get::<ModelInfo>()
.and_then(|model| model.comp_hash.clone()),
review_context_revision,
}),
source: ToolCallSource::Direct,
})
.await;
let fail_closed_score = cached_score(thread_store)
.expect("an oversized compaction should immediately receive the maximum risk score");
assert_eq!(
&fail_closed_score,
&SecurityRiskScore {
scores: BTreeMap::from([("action_risk".to_owned(), 1.0)]),
call_id: None,
action: None,
sampled_at: fail_closed_score.sampled_at,
}
);
assert_eq!(
cached_approval(
&registry,
thread_store,
"review action",
/*metrics*/ None,
)
.await,
None
);
assert_eq!(
server.connections().iter().map(Vec::len).sum::<usize>(),
1,
"an oversized latest compaction must bypass Luna rather than reuse stale context"
);
assert!(metrics.0.lock().unwrap().iter().any(|sample| {
matches!(
sample,
RecordedMetric::Counter(name, 1, tags)
if name == CLASSIFICATION_METRIC
&& tags.contains(&("outcome".to_owned(), "failure".to_owned()))
)
}));
let fail_closed_score = cached_score(thread_store)
.expect("a rejected compaction should immediately receive the maximum risk score");
assert_eq!(
&fail_closed_score,
&SecurityRiskScore {
scores: BTreeMap::from([("action_risk".to_owned(), 1.0)]),
call_id: None,
action: None,
sampled_at: fail_closed_score.sampled_at,
}
);
assert_eq!(
cached_approval(
&registry,
thread_store,
"review action",
/*metrics*/ None,
)
.await,
None
);
assert_eq!(
server.connections().iter().map(Vec::len).sum::<usize>(),
1,
"a rejected latest compaction must bypass Luna rather than reuse stale context"
);
assert!(metrics.0.lock().unwrap().iter().any(|sample| {
matches!(
sample,
RecordedMetric::Counter(name, 1, tags)
if name == CLASSIFICATION_METRIC
&& tags.contains(&("outcome".to_owned(), "failure".to_owned()))
&& tags.contains(&(
"failure_reason".to_owned(),
failure_reason.to_owned(),
))
)
}));
}
Ok(())
}
@@ -270,11 +270,20 @@ impl GuardianV2Extension {
) {
Ok(compaction) => compaction,
Err(error) => {
let (outcome, failure_reason) = if error == ParentCompactionError::RequiresSync {
score_progress.invalidate(tool_call_index);
("skipped", None)
} else {
("failure", Some("parent_compaction_error"))
let (outcome, failure_reason) = match error {
ParentCompactionError::RequiresSync => {
score_progress.invalidate(tool_call_index);
("skipped", None)
}
ParentCompactionError::Serialization => {
("failure", Some("parent_compaction_serialization"))
}
ParentCompactionError::Oversized => {
("failure", Some("parent_compaction_oversized"))
}
ParentCompactionError::Unusable => {
("failure", Some("parent_compaction_unusable"))
}
};
score_progress.fail_closed(sampled_at);
record_classification(