use cpa_core::exec::{ExecError, FailureScope};
use cpa_core::gostr::GoStr;
use serde_json::Value;
pub fn error_text(e: &ExecError) -> String {
if e.body.is_empty() {
format!("status {}", e.status)
} else {
String::from_utf8_lossy(&e.body).into_owned()
}
}
pub fn rule_text(e: &ExecError) -> String {
let text = error_text(e);
if e.status == 404 && explicit_request_scoped(e) && text == cpa_exec::claude::THREAD_NOT_FOUND {
return format!(r#"{{"type":"error","error":{{"type":"not_found_error","message":"{text}"}}}}"#);
}
text
}
fn explicit_request_scoped(e: &ExecError) -> bool {
e.scope == FailureScope::Request && e.headers.is_empty()
}
pub fn go_status(e: &ExecError) -> u16 {
if e.scope == FailureScope::Transport {
0
} else {
e.status
}
}
pub fn empty_stream() -> ExecError {
ExecError::local(
0,
FailureScope::Credential,
"empty_stream: upstream stream closed before first payload",
)
}
pub fn upstream_attempted(e: &ExecError) -> bool {
!e.headers.is_empty() || e.scope == FailureScope::Transport || e.status == 0
}
pub fn is_unauthorized(e: &ExecError) -> bool {
!explicit_request_scoped(e) && names_unauthorized(e)
}
pub fn names_unauthorized(e: &ExecError) -> bool {
let raw = error_text(e).go_lower();
go_status(e) == 401 || raw.contains("status 401") || raw.contains("401 unauthorized")
}
pub fn response_status(e: &ExecError) -> u16 {
match go_status(e) {
0 => 500,
s => s,
}
}
const REQUEST_FAULT_CODES: [&str; 9] = [
"cyber_policy",
"context_length_exceeded",
"message_too_big",
"string_above_max_length",
"invalid_prompt",
"invalid_value",
"unsupported_value",
"invalid_request_error",
"previous_response_not_found",
];
const REQUEST_FAULT_TYPES: [&str; 4] = [
"invalid_request",
"invalid_request_error",
"bad_request_error",
"invalid_prompt",
];
fn json_body(text: &str) -> Option<Value> {
serde_json::from_str(text.trim()).ok()
}
fn path_str<'a>(v: &'a Value, path: &str) -> Option<&'a str> {
path.split('.').try_fold(v, |v, k| v.get(k))?.as_str()
}
fn any_path(v: &Value, paths: &[&str], pred: impl Fn(&str) -> bool) -> bool {
paths
.iter()
.filter_map(|p| path_str(v, p))
.any(|s| pred(&s.trim().go_lower()))
}
const CODE_PATHS: [&str; 4] = ["error.code", "code", "response.error.code", "body.error.code"];
const TYPE_PATHS: [&str; 4] = ["error.type", "type", "response.error.type", "body.error.type"];
pub fn is_claude_thread_not_found(status: u16, text: &str) -> bool {
let body = text.trim();
if status != 404 || body.is_empty() {
return false;
}
let names_thread = |message: &str| {
let lower = message.go_lower();
lower.contains("thread state") && lower.contains("previous_message_id")
};
match json_body(body) {
Some(value) => {
path_str(&value, "error.type").is_some_and(|t| t.trim().eq_ignore_ascii_case("not_found_error"))
&& names_thread(path_str(&value, "error.message").unwrap_or_default())
}
None => names_thread(body),
}
}
pub fn is_request_fault(status: u16, text: &str) -> bool {
if status == 402 || status == 429 {
return false;
}
let body = json_body(text);
if let Some(body) = &body
&& status == 401
&& any_path(body, &TYPE_PATHS, |t| t == "authentication_error")
{
return false;
}
if is_claude_thread_not_found(status, text) {
return true;
}
if let Some(body) = &body {
if any_path(body, &CODE_PATHS, |c| {
c == "model_not_found" || c == "model_not_found_error"
}) {
return false;
}
if any_path(body, &CODE_PATHS, |c| REQUEST_FAULT_CODES.contains(&c))
|| any_path(body, &TYPE_PATHS, |t| REQUEST_FAULT_TYPES.contains(&t))
{
return true;
}
}
let lower = text.go_lower();
if lower.contains("item with id")
&& lower.contains("not found")
&& lower.contains("items are not persisted when `store` is set to false")
{
return true;
}
matches!(status, 400 | 409 | 413 | 422)
}
fn model_support_message(text: &str) -> bool {
let lower = text.trim().go_lower();
!lower.is_empty()
&& [
"model_not_supported",
"requested model is not supported",
"requested model is unsupported",
"requested model is unavailable",
"model is not supported",
"model not supported",
"unsupported model",
"model unavailable",
"not available for your plan",
"not available for your account",
]
.iter()
.any(|p| lower.contains(p))
}
pub fn is_model_support(status: u16, text: &str) -> bool {
explicit_model_not_found(text, "") || (matches!(status, 400 | 404 | 422) && model_support_message(text))
}
pub fn is_invalid_grant(status: u16, text: &str) -> bool {
text.go_lower().contains("invalid_grant") && matches!(status, 0 | 400 | 401)
}
pub fn is_cloudflare(status: u16, text: &str) -> bool {
let lower = text.trim().go_lower();
status < 500
&& (lower.contains("challenge-platform")
|| lower.contains("cf-mitigated")
|| lower.contains("cloudflare challenge")
|| (lower.contains("just a moment") && lower.contains("cloudflare")))
}
pub fn is_request_invalid(e: &ExecError) -> bool {
if explicit_request_scoped(e) {
return true;
}
let status = go_status(e);
let text = error_text(e);
if is_cloudflare(status, &text) || is_invalid_grant(status, &text) || is_model_support(status, &text) {
return false;
}
is_request_fault(status, &text)
}
pub fn is_compact_fault(e: &ExecError) -> bool {
let status = go_status(e);
let text = error_text(e);
if credential_scoped(e) || is_cloudflare(status, &text) || is_invalid_grant(status, &text) {
return false;
}
is_request_fault(status, &text) || matches!(status, 400 | 404 | 405 | 409 | 413 | 422 | 501)
}
pub fn is_compact_neutral(e: &ExecError) -> bool {
let status = go_status(e);
let text = error_text(e);
!(credential_scoped(e)
|| is_cloudflare(status, &text)
|| is_invalid_grant(status, &text)
|| matches!(status, 401 | 402 | 403 | 429))
}
pub fn is_count_endpoint_missing(e: &ExecError, requested_model: &str) -> bool {
let base = crate::scheduler::canonical_model(requested_model);
go_status(e) == 404 && !explicit_model_not_found(&error_text(e), base)
}
pub fn credential_scoped(e: &ExecError) -> bool {
e.scope == FailureScope::Credential && e.status == 429
}
fn statusless(e: &ExecError) -> Option<(String, String)> {
(e.status == 0 && e.scope == FailureScope::Model).then(|| {
let text = error_text(e);
let lower = text.trim().go_lower();
(text, lower)
})
}
pub fn skips_cooldown(e: &ExecError) -> bool {
if is_request_invalid(e) || is_transport(e) {
return true;
}
let Some((text, lower)) = statusless(e) else {
return false;
};
let (lifecycle, transient) = (lifecycle_message(&lower), transient_message(&lower));
if explicit_model_not_found(&text, "") {
lifecycle || (transient && !lower.contains("tls handshake timeout"))
} else {
lifecycle || transient
}
}
fn lifecycle_message(lower: &str) -> bool {
matches!(
lower,
"context canceled" | "context deadline exceeded" | "eof" | "unexpected eof"
) || [
"websocket: close 1000",
"websocket: close 1001",
"websocket: close 1006",
"unexpected eof",
]
.iter()
.any(|needle| lower.contains(needle))
|| http2_closed(lower)
}
fn transient_message(lower: &str) -> bool {
const NEEDLES: [&str; 16] = [
"tls: tls handshake",
"tls handshake timeout",
"wsarecv",
"wsasend",
"a connection attempt failed",
"connection refused",
"connection reset",
"i/o timeout",
"no such host",
"server misbehaving",
"network is unreachable",
"no route to host",
"broken pipe",
"connection aborted",
"use of closed network connection",
"unexpected eof",
];
NEEDLES.iter().any(|needle| lower.contains(needle)) || http2_closed(lower)
}
fn http2_closed(lower: &str) -> bool {
lower.contains("http2: client connection force closed via clientconn.close")
|| lower.contains("http2: client conn is closed")
}
pub fn is_transport(e: &ExecError) -> bool {
if e.scope == FailureScope::Transport {
return true;
}
let lower = error_text(e).trim().go_lower();
e.headers.is_empty() && lower.starts_with("upstream request failed")
}
pub fn is_retry_round(e: &ExecError) -> bool {
is_transport(e)
|| matches!(e.status, 403 | 408 | 429 | 500 | 502 | 503 | 504)
|| statusless(e).is_some_and(|(_, lower)| transient_message(&lower))
}
pub fn explicit_model_not_found(text: &str, requested: &str) -> bool {
json_body(text).is_some_and(|v| structured_not_found(&v, requested))
}
fn structured_not_found(v: &Value, requested: &str) -> bool {
match v {
Value::Object(map) => {
let mut not_found_type = false;
let mut exact_reference = false;
for (key, item) in map {
if let Value::String(text) = item {
match key.trim().go_lower().as_str() {
"code" if model_not_found_identifier(text) => return true,
"type" => {
if model_not_found_identifier(text) {
return true;
}
not_found_type |= not_found_identifier(text);
}
"error" | "message" | "detail" | "error_description" | "title" => {
if explicit_not_found_message(text, requested) {
return true;
}
exact_reference |= exact_model_reference(text, requested);
}
_ => {}
}
}
if matches!(item, Value::Object(_) | Value::Array(_)) && structured_not_found(item, requested) {
return true;
}
}
not_found_type && exact_reference
}
Value::Array(items) => items.iter().any(|item| {
item.as_str().is_some_and(|t| explicit_not_found_message(t, requested))
|| structured_not_found(item, requested)
}),
_ => false,
}
}
fn normalize_identifier(s: &str) -> String {
s.replace(['-', ' '], "_")
}
fn model_not_found_identifier(value: &str) -> bool {
let mut candidate = value.trim().go_lower();
match candidate.rfind('#') {
Some(i) if i + 1 < candidate.len() => candidate = candidate[i + 1..].to_owned(),
_ => {
if let Some(q) = candidate.find('?') {
candidate.truncate(q);
}
let trimmed = candidate.trim_end_matches('/');
candidate = match trimmed.rfind(['/', ':']) {
Some(i) => trimmed[i + 1..].to_owned(),
None => trimmed.to_owned(),
};
}
}
matches!(
normalize_identifier(&candidate).as_str(),
"model_not_found" | "model_not_found_error" | "unknown_model" | "model_does_not_exist" | "model_not_exist"
)
}
fn not_found_identifier(value: &str) -> bool {
matches!(
normalize_identifier(&value.trim().go_lower()).as_str(),
"not_found" | "not_found_error"
)
}
fn trim_sentence(s: &str) -> String {
s.trim()
.go_lower()
.trim_matches(|c| " .!;\t\r\n".contains(c))
.to_owned()
}
fn strip_prefix<'a>(lower: &'a str, prefix: &str) -> Option<&'a str> {
if lower == prefix {
return Some("");
}
let rest = lower
.strip_prefix(&format!("{prefix} "))
.or_else(|| lower.strip_prefix(&format!("{prefix}:")))?;
let rest = rest.trim();
Some(rest.strip_prefix(':').unwrap_or(rest).trim())
}
fn explicit_not_found_message(message: &str, requested: &str) -> bool {
let lower = trim_sentence(message);
if lower.is_empty() || lower.contains("in request") || lower.contains("in body") || lower.contains("request body") {
return false;
}
let normalized = lower.replace('-', "_");
if normalized.contains("model_not_found") || normalized.contains("unknown_model") {
return true;
}
for prefix in ["no such model", "unknown model"] {
if let Some(rest) = strip_prefix(&lower, prefix) {
if rest.is_empty() {
return true;
}
return trim_model_reference(rest, requested).is_some_and(|s| s.is_empty());
}
}
for prefix in ["the requested model", "requested model", "the model", "model"] {
if let Some(rest) = strip_prefix(&lower, prefix) {
if missing_phrase(rest) {
return true;
}
return trim_model_reference(rest, requested).is_some_and(missing_phrase);
}
}
false
}
fn exact_model_reference(message: &str, requested: &str) -> bool {
let lower = trim_sentence(message);
for prefix in ["the requested model", "requested model", "the model", "model"] {
if let Some(rest) = strip_prefix(&lower, prefix) {
return trim_model_reference(rest, requested).is_some_and(|s| s.is_empty());
}
}
false
}
fn trim_model_reference<'a>(value: &'a str, requested: &str) -> Option<&'a str> {
let model = requested.trim().go_lower();
if model.is_empty() {
return None;
}
for candidate in [
model.clone(),
format!("'{model}'"),
format!("\"{model}\""),
format!("`{model}`"),
] {
if value == candidate {
return Some("");
}
if let Some(rest) = value.strip_prefix(&candidate)
&& (rest.is_empty() || rest.starts_with([' ', ':', ',']))
{
return Some(rest.trim_start_matches([' ', ':', ',']));
}
}
None
}
fn missing_phrase(value: &str) -> bool {
matches!(
value.trim_matches(|c| " .!;\t\r\n".contains(c)),
"not found"
| "was not found"
| "could not be found"
| "does not exist"
| "doesn't exist"
| "not exist"
| "is unknown"
| "does not exist or you do not have access to it"
)
}
#[cfg(test)]
mod tests {
use super::*;
use bytes::Bytes;
#[test]
fn compaction_summary_failures_follow_go() {
for message in [
"extract summary: no summary text found in upstream response",
"seal compaction capsule: generate nonce: x",
] {
let e = ExecError::local(0, FailureScope::Model, message);
assert!(!skips_cooldown(&e), "{message}");
assert!(!is_retry_round(&e), "{message}");
assert!(!is_transport(&e), "{message}");
assert!(is_compact_neutral(&e) && !is_compact_fault(&e), "{message}");
assert!(!is_request_invalid(&e), "{message}");
}
}
#[test]
fn http2_closed_connections_are_transient_like_go() {
for message in [
"http2: client connection force closed via ClientConn.Close",
"http2: client conn is closed",
"Post \"https://example.com\": http2: client conn is closed",
] {
let e = ExecError::local(0, FailureScope::Model, message);
assert!(skips_cooldown(&e), "{message}");
assert!(is_retry_round(&e), "{message}");
}
let other = ExecError::local(0, FailureScope::Model, "http2: server sent GOAWAY");
assert!(!skips_cooldown(&other) && !is_retry_round(&other));
}
fn upstream(status: u16, body: &str) -> ExecError {
let mut e = ExecError::local(status, FailureScope::Request, body.to_owned());
e.headers.insert("content-type", "application/json".parse().unwrap());
e
}
#[test]
fn request_faults_follow_go_status_and_body_rules() {
assert!(is_request_invalid(&upstream(
500,
r#"{"type":"error","error":{"type":"invalid_request_error"}}"#
)));
assert!(!is_request_invalid(&upstream(
429,
r#"{"error":{"type":"invalid_request_error"}}"#
)));
assert!(!is_request_invalid(&upstream(
401,
r#"{"error":{"type":"authentication_error"}}"#
)));
assert!(!is_request_invalid(&upstream(404, "not found")));
const THREAD: &str = "No thread state was found for the requested previous_message_id. Replay the full conversation with thread create to start a new Thread.";
let json = format!(r#"{{"type":"error","error":{{"type":"not_found_error","message":"{THREAD}"}}}}"#);
assert!(is_request_invalid(&upstream(404, &json)));
assert!(is_request_invalid(&upstream(404, THREAD)));
assert!(!is_request_invalid(&upstream(
404,
r#"{"error":{"type":"not_found_error","message":"Not Found"}}"#
)));
assert!(!is_request_fault(500, &json));
assert!(is_request_invalid(&upstream(422, "bad")));
assert!(!is_request_invalid(&upstream(
400,
r#"{"error":{"code":"model_not_found"}}"#
)));
assert!(!is_request_invalid(&upstream(400, "invalid_grant")));
assert!(is_request_invalid(&ExecError::local(
501,
FailureScope::Request,
"not supported"
)));
let transport = ExecError::local(502, FailureScope::Transport, "reset");
assert!(!is_request_invalid(&transport) && is_retry_round(&transport));
assert_eq!(response_status(&transport), 500);
}
#[test]
fn special_classifiers_match_go() {
assert!(is_model_support(404, "The model is not supported for this account"));
assert!(!is_model_support(500, "model not supported"));
assert!(!is_model_support(
500,
r#"{"error":{"type":"not_found_error","message":"model: claude-x"}}"#
));
assert!(explicit_model_not_found(
r#"{"type":"error","error":{"type":"not_found_error","message":"model: claude-x"}}"#,
"claude-x"
));
assert!(explicit_model_not_found(
r#"{"error":{"message":"The model `gpt-9` does not exist"}}"#,
"gpt-9"
));
assert!(!explicit_model_not_found(
r#"{"error":{"message":"model field missing in request body"}}"#,
""
));
assert!(is_invalid_grant(400, r#"{"error":"invalid_grant"}"#));
assert!(!is_invalid_grant(403, "invalid_grant"));
assert!(is_cloudflare(403, "<title>Just a moment...</title> cloudflare"));
assert!(!is_cloudflare(503, "challenge-platform"));
let count_404 = upstream(404, "Not Found");
assert!(is_count_endpoint_missing(&count_404, "claude-x(high)"));
assert!(!is_count_endpoint_missing(
&upstream(404, r#"{"error":{"code":"model_not_found"}}"#),
"claude-x"
));
assert!(is_compact_fault(&upstream(404, "nope")) && is_compact_neutral(&upstream(404, "nope")));
assert!(!is_compact_neutral(&upstream(429, "quota")));
let mut empty = upstream(500, "");
empty.body = Bytes::new();
assert_eq!(error_text(&empty), "status 500");
}
}