Skip to main content

agent_core/core/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum RuntimeError {
5    #[error("API error: {0}")]
6    Api(#[from] reqwest::Error),
7    #[error("{0}")]
8    ApiStatus(String),
9    #[error("Auth error: {0}")]
10    Auth(String),
11    #[error("Config error: {0}")]
12    Config(String),
13    #[error("Session error: {0}")]
14    Session(String),
15    #[error("Tool execution failed: {0}")]
16    Tool(String),
17    #[error("Request timed out")]
18    Timeout,
19    #[error("Operation canceled")]
20    Canceled,
21}
22
23/// Translate an Anthropic API error response into a human-actionable message.
24///
25/// SECURITY (spec §5.1): the response `body` — including any nested
26/// `error.message` / `error.type` — is UNTRUSTED. A hostile or misconfigured
27/// provider can echo the entire request (prompts, system text, tool schemas,
28/// credentials) inside it, so no body-derived text is ever reproduced in the
29/// returned message. The body is used only for *classification* (matching
30/// against fixed, vetted patterns); output is built exclusively from static
31/// guidance plus the numeric status.
32pub fn humanize_api_error(status: u16, body: &str) -> String {
33    humanize_api_error_with_reset(status, body, None)
34}
35
36/// Anthropic wire error types we recognise. Matching one lets the message
37/// name the class via OUR static string — never the provider's bytes.
38const VETTED_ERROR_TYPES: &[&str] = &[
39    "invalid_request_error",
40    "authentication_error",
41    "permission_error",
42    "not_found_error",
43    "request_too_large",
44    "rate_limit_error",
45    "api_error",
46    "overloaded_error",
47    "billing_error",
48    "timeout_error",
49];
50
51/// Extract `error.type` from an Anthropic error envelope and map it onto a
52/// vetted static label. Returns `None` for anything unrecognised — the
53/// untrusted value itself is never surfaced.
54fn vetted_error_type(body: &str) -> Option<&'static str> {
55    let v: serde_json::Value = serde_json::from_str(body).ok()?;
56    let ty = v.get("error")?.get("type")?.as_str()?;
57    VETTED_ERROR_TYPES.iter().find(|t| **t == ty).copied()
58}
59
60/// Classification-only peek at the untrusted error body: true when it
61/// contains the given fixed pattern. The body text itself is never emitted.
62fn body_mentions(body: &str, pattern: &str) -> bool {
63    body.contains(pattern)
64}
65
66/// Map an untrusted provider error-type string onto a vetted static label.
67///
68/// Used by streaming/sync callers that already parsed the envelope: the
69/// returned `&'static str` is OUR constant, safe to log/display; the input
70/// itself must never be reproduced.
71pub fn sanitize_error_type(ty: &str) -> Option<&'static str> {
72    VETTED_ERROR_TYPES.iter().find(|t| **t == ty).copied()
73}
74
75/// Like [`humanize_api_error`] but surfaces a known rate-limit reset time in
76/// the 429 message so the failure is honest rather than cryptic.
77/// `reset_hint` is a human-readable duration string, e.g. `"47s"`.
78pub fn humanize_api_error_with_reset(status: u16, body: &str, reset_hint: Option<&str>) -> String {
79    // Vetted static class label, e.g. " [api_error]" — safe because it is
80    // one of OUR constants, selected (not copied) via the untrusted body.
81    let kind = vetted_error_type(body)
82        .map(|t| format!(" [{t}]"))
83        .unwrap_or_default();
84
85    match status {
86        529 => "Anthropic is overloaded right now. Retries exhausted — wait a minute and try again.".to_string(),
87        429 => {
88            if let Some(reset) = reset_hint {
89                format!(
90                    "Rate limit exhausted — retries used up while waiting for reset (next window in {}). \
91                     Try again shortly, or switch models with /model.",
92                    reset
93                )
94            } else {
95                format!("Rate limited by Anthropic (HTTP 429{kind}). Wait for the limit to reset, or switch models with /model.")
96            }
97        }
98        401 => "Authentication rejected. Run `synaps login` to re-authenticate.".to_string(),
99        403 => format!("Access denied (HTTP 403{kind}). Your account may not have access to this model."),
100        404 => format!("Model or endpoint not found (HTTP 404{kind}). Check the model name with /model."),
101        413 => "Request too large. Run /compact to shrink the conversation, or reduce tool output sizes.".to_string(),
102        400 if body_mentions(body, "extended-cache-ttl") =>
103            "Bad request (HTTP 400) — your account may not support 1h cache TTL; set cache_ttl = 5m in config.".to_string(),
104        400 if body_mentions(body, "prompt is too long") || body_mentions(body, "max_tokens") || body_mentions(body, "context") =>
105            "Context window exceeded (HTTP 400). Run /compact to shrink the conversation.".to_string(),
106        400 => format!("Bad request (HTTP 400{kind}). Provider error details withheld — they can echo request content."),
107        500 | 502 | 503 => format!("Anthropic server error (HTTP {status}{kind}). Retries exhausted — usually transient, try again shortly."),
108        _ => format!("API error (HTTP {status}{kind}). Provider error details withheld — they can echo request content."),
109    }
110}
111
112/// Translate a reqwest transport error into a human-actionable message.
113pub fn humanize_network_error(e: &reqwest::Error) -> String {
114    if e.is_timeout() {
115        "Request to api.anthropic.com timed out. Check your connection and try again.".to_string()
116    } else if e.is_connect() {
117        "Could not reach api.anthropic.com (connection failed). Check your network, DNS, or proxy settings.".to_string()
118    } else if e.is_body() || e.is_decode() {
119        "Connection lost mid-response. Partial reply kept — send again to continue.".to_string()
120    } else {
121        format!("Network error: {}", e)
122    }
123}
124
125/// Render an error's full cause chain (`Display` of every level, `: `-joined).
126///
127/// `format!("{e}")` on a `reqwest::Error` prints only the top level — e.g.
128/// `error sending request for url (…)` — and silently drops the source that
129/// says *why* (`operation timed out`, `dns error`, `connection refused`, …).
130/// That cost a real postmortem hours; always surface the chain.
131pub fn error_chain_string(e: &(dyn std::error::Error + 'static)) -> String {
132    let mut msg = e.to_string();
133    let mut source = e.source();
134    while let Some(cause) = source {
135        let cause_str = cause.to_string();
136        // Some wrappers already embed their source's Display; skip duplicates.
137        if !msg.contains(&cause_str) {
138            msg.push_str(": ");
139            msg.push_str(&cause_str);
140        }
141        source = cause.source();
142    }
143    msg
144}
145
146/// Host-aware variant of [`humanize_network_error`] for non-Anthropic
147/// providers (OpenAI Codex, Groq, local OpenAI-compat endpoints, …).
148///
149/// Unlike the Anthropic-specific helper this derives the host from the
150/// failing request's URL and preserves the underlying cause chain, so
151/// `chatgpt.com` failures are never misattributed and the *reason*
152/// (timeout vs connect vs mid-stream drop) survives into the UI.
153pub fn humanize_provider_network_error(e: &reqwest::Error) -> String {
154    let host = e
155        .url()
156        .and_then(|u| u.host_str())
157        .map(String::from)
158        .unwrap_or_else(|| "the provider endpoint".to_string());
159    let chain = error_chain_string(e);
160    if e.is_timeout() {
161        format!(
162            "Request to {host} timed out — usually transient; check your connection and try again. [{chain}]"
163        )
164    } else if e.is_connect() {
165        format!(
166            "Could not reach {host} (connection failed). Check your network, DNS, or proxy settings. [{chain}]"
167        )
168    } else if e.is_body() || e.is_decode() {
169        format!("Connection to {host} lost mid-response — usually transient; try again. [{chain}]")
170    } else {
171        format!("Network error talking to {host}: {chain}")
172    }
173}
174
175pub type Result<T> = std::result::Result<T, RuntimeError>;
176
177#[cfg(test)]
178mod tests {
179    use super::*;
180
181    #[test]
182    fn test_humanize_529_overloaded() {
183        let msg = humanize_api_error(
184            529,
185            r#"{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}"#,
186        );
187        assert!(msg.contains("overloaded"), "got: {msg}");
188        assert!(!msg.contains('{'), "raw JSON leaked: {msg}");
189    }
190
191    #[test]
192    fn test_humanize_401_points_to_login() {
193        let msg = humanize_api_error(401, r#"{"error":{"message":"invalid x-api-key"}}"#);
194        assert!(msg.contains("synaps login"), "got: {msg}");
195    }
196
197    #[test]
198    fn test_humanize_400_context_suggests_compact() {
199        let msg = humanize_api_error(
200            400,
201            r#"{"error":{"message":"prompt is too long: 250000 tokens"}}"#,
202        );
203        assert!(msg.contains("/compact"), "got: {msg}");
204    }
205
206    #[test]
207    fn test_humanize_400_cache_ttl_names_config_key() {
208        let msg = humanize_api_error(
209            400,
210            r#"{"error":{"message":"The extended-cache-ttl-2025-04-11 beta is not enabled for this account"}}"#,
211        );
212        assert!(msg.contains("cache_ttl = 5m"), "got: {msg}");
213    }
214
215    #[test]
216    fn test_humanize_unknown_status_names_status_but_withholds_detail() {
217        // Provider `error.message` is untrusted (can echo the request) —
218        // only the status and vetted guidance may appear.
219        let msg = humanize_api_error(418, r#"{"error":{"message":"teapot"}}"#);
220        assert!(msg.contains("418"), "got: {msg}");
221        assert!(!msg.contains("teapot"), "untrusted detail leaked: {msg}");
222    }
223
224    #[test]
225    fn test_humanize_non_json_body_truncated() {
226        let long_body = "x".repeat(500);
227        let msg = humanize_api_error(418, &long_body);
228        assert!(msg.len() < 300, "not truncated: {} chars", msg.len());
229    }
230
231    /// BUG-1 regression: byte-slicing at offset 200 panics when a multibyte
232    /// char (e.g. the 4-byte emoji 🔥) straddles that boundary.
233    /// Craft a body where the emoji starts at byte 198 (i.e. bytes 198-201),
234    /// so `&trimmed[..200]` would land in the middle of a char → panic.
235    /// The fixed code must NOT panic and must truncate cleanly at a char boundary.
236    #[test]
237    fn test_humanize_multibyte_boundary_no_panic() {
238        // 198 ASCII bytes + 🔥 (4 bytes, positions 198-201) + filler to exceed 200 total
239        let body = format!("{}{}{}", "a".repeat(198), "🔥", "b".repeat(100));
240        assert!(body.len() > 200, "precondition: body must exceed 200 bytes");
241        // This must NOT panic — the bug causes a panic on unpatched code
242        let msg = humanize_api_error(418, &body);
243        // The result must be valid UTF-8 (it's a &str / String, always is if no panic)
244        // and must contain the truncated prefix (not the raw emoji bytes mid-char)
245        assert!(msg.contains("418"), "status should appear: {msg}");
246        // The body portion forwarded must be ≤ 200 bytes (emoji trimmed at boundary)
247        // After truncation at 198 bytes (the last safe boundary before 200), we get
248        // 198 'a's — confirm no garbled bytes leaked through
249        assert!(!msg.contains('\u{FFFD}'), "replacement char leaked: {msg}");
250    }
251
252    // ── error_chain_string: full cause chain, not just the top Display ──────
253
254    #[derive(Debug)]
255    struct Outer(Inner);
256    #[derive(Debug)]
257    struct Inner;
258
259    impl std::fmt::Display for Outer {
260        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
261            write!(f, "error sending request for url (https://example.com/x)")
262        }
263    }
264    impl std::fmt::Display for Inner {
265        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
266            write!(f, "operation timed out")
267        }
268    }
269    impl std::error::Error for Outer {
270        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
271            Some(&self.0)
272        }
273    }
274    impl std::error::Error for Inner {}
275
276    #[test]
277    fn error_chain_string_joins_all_sources() {
278        let msg = error_chain_string(&Outer(Inner));
279        assert!(
280            msg.contains("error sending request") && msg.contains("operation timed out"),
281            "chain must include top-level AND source: {msg}"
282        );
283    }
284
285    #[test]
286    fn error_chain_string_single_level_has_no_separator_suffix() {
287        let msg = error_chain_string(&Inner);
288        assert_eq!(msg, "operation timed out");
289    }
290
291    // ── humanize_provider_network_error: host-aware transport messaging ─────
292
293    /// Listener that accepts connections but never responds → client-side
294    /// timeout while awaiting response headers (the incident failure mode).
295    #[tokio::test]
296    async fn humanize_provider_timeout_names_host_and_says_transient() {
297        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
298        let addr = listener.local_addr().unwrap();
299        // Keep the listener alive but never accept/respond.
300        let client = reqwest::Client::builder()
301            .timeout(std::time::Duration::from_millis(100))
302            .build()
303            .unwrap();
304        let err = client
305            .post(format!("http://{addr}/codex/responses"))
306            .send()
307            .await
308            .expect_err("must time out");
309        drop(listener);
310        assert!(err.is_timeout(), "precondition: {err:?}");
311        let msg = humanize_provider_network_error(&err);
312        assert!(msg.contains("127.0.0.1"), "must name the host: {msg}");
313        assert!(msg.contains("timed out"), "must say timed out: {msg}");
314        assert!(msg.contains("transient"), "must flag transience: {msg}");
315        assert!(
316            !msg.contains("api.anthropic.com"),
317            "must not claim the Anthropic host: {msg}"
318        );
319    }
320
321    #[tokio::test]
322    async fn humanize_provider_connect_error_names_host() {
323        // Bind then drop → guaranteed-refused port.
324        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
325        let addr = listener.local_addr().unwrap();
326        drop(listener);
327        let client = reqwest::Client::new();
328        let err = client
329            .post(format!("http://{addr}/codex/responses"))
330            .send()
331            .await
332            .expect_err("must fail to connect");
333        assert!(err.is_connect(), "precondition: {err:?}");
334        let msg = humanize_provider_network_error(&err);
335        assert!(msg.contains("127.0.0.1"), "must name the host: {msg}");
336        assert!(
337            msg.contains("Could not reach"),
338            "must describe connection failure: {msg}"
339        );
340    }
341
342    // ── Phase 1 holdout regression: hostile provider echoes the request ─────
343    //
344    // A hostile/misconfigured provider can put the ENTIRE request body
345    // (user messages, system prompts, tool schemas, credentials) into
346    // `error.message`. spec §5.1 / Task 1: no raw-content sentinel may
347    // appear in logs or user-visible errors at any level. The humanizer
348    // must therefore never echo any body-derived text.
349
350    const HOSTILE_SENTINEL: &str = "HOLDOUT-SENTINEL-http500-77aa";
351
352    /// Anthropic-shaped error envelope whose message echoes a request body.
353    fn hostile_json_body() -> String {
354        format!(
355            r#"{{"type":"error","error":{{"type":"api_error","message":"ECHOED:{{\"messages\":[{{\"role\":\"user\",\"content\":\"{} tell me things\"}}],\"system\":[{{\"text\":\"You are a secret system prompt\"}}],\"tools\":[{{\"name\":\"bash\",\"input_schema\":{{\"properties\":{{}}}}}}]}}"}}}}"#,
356            HOSTILE_SENTINEL
357        )
358    }
359
360    /// Request-shaped markers that must never surface in a humanized error.
361    fn assert_no_request_content(msg: &str, ctx: &str) {
362        assert!(
363            !msg.contains(HOSTILE_SENTINEL),
364            "{ctx}: sentinel leaked: {msg}"
365        );
366        assert!(!msg.contains("ECHOED"), "{ctx}: echoed body leaked: {msg}");
367        assert!(
368            !msg.contains("input_schema") && !msg.contains("secret system prompt"),
369            "{ctx}: request-shaped content leaked: {msg}"
370        );
371    }
372
373    #[test]
374    fn hostile_error_message_never_leaks_for_any_status() {
375        let body = hostile_json_body();
376        for status in [400u16, 403, 404, 429, 500, 418] {
377            let msg = humanize_api_error(status, &body);
378            assert_no_request_content(&msg, &format!("status {status}"));
379            // Still actionable: names the status class.
380            assert!(
381                msg.contains(&status.to_string())
382                    || msg.contains("Rate limited")
383                    || msg.contains("Access denied")
384                    || msg.contains("not found"),
385                "status {status}: message not actionable: {msg}"
386            );
387        }
388    }
389
390    #[test]
391    fn hostile_429_with_reset_hint_keeps_timing_but_not_body() {
392        let msg = humanize_api_error_with_reset(429, &hostile_json_body(), Some("47s"));
393        assert_no_request_content(&msg, "429+reset");
394        assert!(msg.contains("47s"), "reset timing must survive: {msg}");
395    }
396
397    #[test]
398    fn hostile_non_json_body_never_leaks() {
399        // Non-JSON hostile content (e.g. text/plain echo of the request).
400        let body = format!(
401            "raw echo: {} {{\"messages\":[...],\"system\":[...]}} {}",
402            HOSTILE_SENTINEL,
403            "x".repeat(300)
404        );
405        for status in [400u16, 403, 404, 429, 500, 418] {
406            let msg = humanize_api_error(status, &body);
407            assert_no_request_content(&msg, &format!("non-json status {status}"));
408        }
409    }
410
411    #[test]
412    fn hostile_error_type_field_is_not_echoed_verbatim() {
413        // `error.type` is attacker-controlled too — only vetted static
414        // labels may be reproduced.
415        let body = format!(
416            r#"{{"error":{{"type":"{} injected-type","message":"m"}}}}"#,
417            HOSTILE_SENTINEL
418        );
419        for status in [400u16, 403, 404, 429, 500, 418] {
420            let msg = humanize_api_error(status, &body);
421            assert_no_request_content(&msg, &format!("type-field status {status}"));
422        }
423    }
424
425    #[test]
426    fn test_runtime_error_display() {
427        assert_eq!(
428            format!("{}", RuntimeError::Auth("bad token".into())),
429            "Auth error: bad token"
430        );
431
432        assert_eq!(
433            format!("{}", RuntimeError::Config("missing".into())),
434            "Config error: missing"
435        );
436
437        assert_eq!(
438            format!("{}", RuntimeError::Tool("failed".into())),
439            "Tool execution failed: failed"
440        );
441
442        assert_eq!(
443            format!("{}", RuntimeError::Session("not found".into())),
444            "Session error: not found"
445        );
446
447        assert_eq!(format!("{}", RuntimeError::Timeout), "Request timed out");
448
449        assert_eq!(format!("{}", RuntimeError::Canceled), "Operation canceled");
450    }
451
452    #[test]
453    fn test_runtime_error_to_string() {
454        assert_eq!(
455            RuntimeError::Auth("bad token".into()).to_string(),
456            "Auth error: bad token"
457        );
458
459        assert_eq!(
460            RuntimeError::Config("missing".into()).to_string(),
461            "Config error: missing"
462        );
463
464        assert_eq!(
465            RuntimeError::Tool("failed".into()).to_string(),
466            "Tool execution failed: failed"
467        );
468
469        assert_eq!(
470            RuntimeError::Session("not found".into()).to_string(),
471            "Session error: not found"
472        );
473
474        assert_eq!(RuntimeError::Timeout.to_string(), "Request timed out");
475
476        assert_eq!(RuntimeError::Canceled.to_string(), "Operation canceled");
477    }
478}