Surface misalignment policy violations as typed errors (#38682)

## What changed

- Recognize `misalignment_policy_violation` errors from response streams and HTTP 400 or 403 responses.
- Preserve the upstream message, use a fallback for blank messages, and treat the error as non-retryable.
- Expose `misalignmentPolicyViolation` through the app-server protocol and generated schemas so turns fail with a typed terminal error.

## Testing

- Cover streamed and HTTP policy violations, fallback messages, retry behavior, and app-server turn completion.

GitOrigin-RevId: fd3485bf0be7bfe3d51c078bbc36a081692fd57f
This commit is contained in:
Francis Chalissery
2026-08-15 01:34:33 +00:00
committed by copyberry
parent 4e9a1a9073
commit eb147c0db3
32 changed files with 299 additions and 2 deletions
@@ -146,6 +146,39 @@ fn map_api_error_uses_cyber_policy_fallback_for_missing_message() {
);
}
#[test]
fn map_api_error_maps_misalignment_policy_violation_from_400_body() {
assert_misalignment_policy_violation_from_http_body(http::StatusCode::BAD_REQUEST);
}
#[test]
fn map_api_error_maps_misalignment_policy_violation_from_403_body() {
assert_misalignment_policy_violation_from_http_body(http::StatusCode::FORBIDDEN);
}
fn assert_misalignment_policy_violation_from_http_body(status: http::StatusCode) {
let body = serde_json::json!({
"error": {
"message": "This request violated the misalignment policy.",
"type": "invalid_request_error",
"code": "misalignment_policy_violation"
}
})
.to_string();
let err = map_api_error(ApiError::Transport(TransportError::Http {
status,
url: Some("http://example.com/v1/responses".to_string()),
headers: None,
body: Some(body),
}));
let CodexErrorDetails::MisalignmentPolicyViolation { message } = err.details() else {
panic!("expected CodexErrorDetails::MisalignmentPolicyViolation, got {err:?}");
};
assert_eq!(message, "This request violated the misalignment policy.");
assert!(!err.is_retryable());
}
#[test]
fn map_api_error_keeps_unknown_400_errors_generic() {
let body = serde_json::json!({