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:29:46 +00:00
committed by copyberry
parent 4e9a1a9073
commit eb147c0db3
32 changed files with 299 additions and 2 deletions

View File

@@ -48,6 +48,9 @@ pub fn map_api_error(err: ApiError) -> CodexErr {
ApiError::CyberPolicy { message } => {
CodexErr::new(CodexErrorDetails::CyberPolicy { message })
}
ApiError::MisalignmentPolicyViolation { message } => {
CodexErr::new(CodexErrorDetails::MisalignmentPolicyViolation { message })
}
ApiError::Transport(transport) => match transport {
TransportError::Http {
status,
@@ -70,6 +73,26 @@ pub fn map_api_error(err: ApiError) -> CodexErr {
return CodexErr::ServerOverloaded;
}
if (status == http::StatusCode::BAD_REQUEST
|| status == http::StatusCode::FORBIDDEN)
&& let Ok(parsed) = serde_json::from_str::<Value>(&body_text)
&& let Some(error) = parsed.get("error")
&& error.get("code").and_then(Value::as_str)
== Some(MISALIGNMENT_POLICY_VIOLATION_ERROR_CODE)
{
let message = error
.get("message")
.and_then(Value::as_str)
.filter(|message| !message.trim().is_empty())
.map(str::to_string)
.unwrap_or_else(|| {
MISALIGNMENT_POLICY_VIOLATION_FALLBACK_MESSAGE.to_string()
});
return CodexErr::new(CodexErrorDetails::MisalignmentPolicyViolation {
message,
});
}
if status == http::StatusCode::BAD_REQUEST {
if let Ok(parsed) = serde_json::from_str::<Value>(&body_text)
&& let Some(error) = parsed.get("error")
@@ -167,6 +190,9 @@ const X_ERROR_JSON_HEADER: &str = "x-error-json";
const CYBER_POLICY_ERROR_CODE: &str = "cyber_policy";
const CYBER_POLICY_FALLBACK_MESSAGE: &str =
"This request has been flagged for possible cybersecurity risk.";
const MISALIGNMENT_POLICY_VIOLATION_ERROR_CODE: &str = "misalignment_policy_violation";
const MISALIGNMENT_POLICY_VIOLATION_FALLBACK_MESSAGE: &str =
"This request was blocked due to a misalignment policy violation.";
const CLOUDFLARE_BLOCKED_MESSAGE: &str =
"Access blocked by Cloudflare. This usually happens when connecting from a restricted region";

View File

@@ -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!({

View File

@@ -29,6 +29,8 @@ pub enum ApiError {
InvalidRequest { message: String },
#[error("cyber policy: {message}")]
CyberPolicy { message: String },
#[error("misalignment policy violation: {message}")]
MisalignmentPolicyViolation { message: String },
#[error("server overloaded")]
ServerOverloaded,
}

View File

@@ -420,6 +420,15 @@ pub fn process_responses_event(
} else if is_cyber_policy_error(&error) {
let message = cyber_policy_message(error.message);
response_error = ApiError::CyberPolicy { message };
} else if error.code.as_deref() == Some("misalignment_policy_violation") {
let message = error
.message
.filter(|message| !message.trim().is_empty())
.unwrap_or_else(|| {
"This request was blocked due to a misalignment policy violation."
.to_string()
});
response_error = ApiError::MisalignmentPolicyViolation { message };
} else if matches!(error.code.as_deref(), Some("invalid_prompt" | "bio_policy"))
{
let message = error
@@ -1148,6 +1157,51 @@ mod tests {
}
}
#[tokio::test]
async fn misalignment_policy_violation_error_is_fatal() {
let raw_error = r#"{"type":"response.failed","sequence_number":3,"response":{"id":"resp_fatal_misalignment","object":"response","status":"failed","error":{"type":"invalid_request_error","code":"misalignment_policy_violation","message":"This request violated the misalignment policy."}}}"#;
let sse = format!("event: response.failed\ndata: {raw_error}\n\n");
let events = collect_events(&[sse.as_bytes()]).await;
assert_eq!(events.len(), 1);
match &events[0] {
Err(ApiError::MisalignmentPolicyViolation { message }) => {
assert_eq!(message, "This request violated the misalignment policy.");
}
other => panic!("unexpected event: {other:?}"),
}
}
#[tokio::test]
async fn misalignment_policy_violation_uses_fallback_for_blank_message() {
for message in ["", " "] {
let raw_error = serde_json::json!({
"type": "response.failed",
"response": {
"id": "resp_fatal_misalignment",
"status": "failed",
"error": {
"type": "invalid_request_error",
"code": "misalignment_policy_violation",
"message": message,
},
},
});
let sse = format!("event: response.failed\ndata: {raw_error}\n\n");
let events = collect_events(&[sse.as_bytes()]).await;
assert_eq!(events.len(), 1);
match &events[0] {
Err(ApiError::MisalignmentPolicyViolation { message }) => assert_eq!(
message,
"This request was blocked due to a misalignment policy violation."
),
other => panic!("unexpected event: {other:?}"),
}
}
}
#[tokio::test]
async fn content_policy_errors_without_type_are_invalid_requests() {
for (code, expected_message) in [