opencrabs 0.3.66

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Regression tests for proxy-error surfacing and retry classification.
//!
//! Locks in the fix for the 2026-04-23 incident where opencode.ai/zen/go
//! returned HTTP 400 with `{"error":{"message":"Provider returned error",
//! "metadata":{"raw":"{\"error\":{\"message\":\"thinking is enabled but
//! reasoning_content is missing in assistant tool call message at index
//! 39\",\"type\":\"invalid_request_error\"}},"provider_name":"Moonshot AI"}}}`
//! — the real Moonshot error was hidden inside `metadata.raw` and we were
//! treating every 400 as non-retryable regardless of content.

use crate::brain::provider::custom_openai_compatible::{
    OpenAIErrorResponse, needs_reasoning_content_for, unwrap_proxy_error,
};
use crate::brain::provider::error::{
    ProviderError, is_html_error_body, is_temporary_unavailable_signal, is_transient_proxy_400,
};

// ─── unwrap_proxy_error ─────────────────────────────────────────────

#[test]
fn unwrap_proxy_error_pulls_inner_message_from_opencode_envelope() {
    let body = r#"{
      "error": {
        "message": "Provider returned error",
        "code": 400,
        "metadata": {
          "raw": "{\"error\":{\"message\":\"thinking is enabled but reasoning_content is missing in assistant tool call message at index 39\",\"type\":\"invalid_request_error\"}}",
          "provider_name": "Moonshot AI",
          "is_byok": true
        }
      },
      "user_id": "user_x"
    }"#;
    let parsed: OpenAIErrorResponse = serde_json::from_str(body).expect("parse");
    let (msg, ty) = unwrap_proxy_error(&parsed.error);
    assert_eq!(
        msg,
        "[Moonshot AI] thinking is enabled but reasoning_content is missing in assistant tool call message at index 39"
    );
    assert_eq!(ty.as_deref(), Some("invalid_request_error"));
}

#[test]
fn unwrap_proxy_error_falls_back_when_no_metadata() {
    let body = r#"{"error":{"message":"Missing API key","type":"authentication_error"}}"#;
    let parsed: OpenAIErrorResponse = serde_json::from_str(body).expect("parse");
    let (msg, ty) = unwrap_proxy_error(&parsed.error);
    assert_eq!(msg, "Missing API key");
    assert_eq!(ty.as_deref(), Some("authentication_error"));
}

#[test]
fn unwrap_proxy_error_handles_non_json_raw() {
    let body = r#"{
      "error": {
        "message": "Provider returned error",
        "metadata": {
          "raw": "backend timed out",
          "provider_name": "Alibaba"
        }
      }
    }"#;
    let parsed: OpenAIErrorResponse = serde_json::from_str(body).expect("parse");
    let (msg, _) = unwrap_proxy_error(&parsed.error);
    assert!(msg.contains("[Alibaba]"), "should prefix backend name");
    assert!(
        msg.contains("backend timed out"),
        "should include raw text when it isn't JSON: got {msg:?}"
    );
}

#[test]
fn unwrap_proxy_error_metadata_present_but_no_raw_field() {
    let body = r#"{
      "error": {
        "message": "rate limited",
        "type": "rate_limit_exceeded",
        "metadata": { "provider_name": "Moonshot" }
      }
    }"#;
    let parsed: OpenAIErrorResponse = serde_json::from_str(body).expect("parse");
    let (msg, ty) = unwrap_proxy_error(&parsed.error);
    // No `raw` → return outer as-is (no prefix added).
    assert_eq!(msg, "rate limited");
    assert_eq!(ty.as_deref(), Some("rate_limit_exceeded"));
}

// ─── ProviderError::Display and is_retryable ────────────────────────

#[test]
fn api_error_display_hides_empty_error_type_brackets() {
    let err = ProviderError::ApiError {
        status: 400,
        message: "boom".to_string(),
        error_type: Some(String::new()),
    };
    let rendered = err.to_string();
    assert_eq!(rendered, "API error (400): boom");
    assert!(
        !rendered.contains("[]"),
        "Display must not print '[]' when error_type is Some(\"\")"
    );
}

#[test]
fn api_error_display_shows_non_empty_error_type() {
    let err = ProviderError::ApiError {
        status: 400,
        message: "bad".to_string(),
        error_type: Some("invalid_request_error".to_string()),
    };
    assert_eq!(
        err.to_string(),
        "API error (400) [invalid_request_error]: bad"
    );
}

#[test]
fn transient_proxy_400_retryable_on_generic_passthrough() {
    let err = ProviderError::ApiError {
        status: 400,
        message: "Provider returned error".to_string(),
        error_type: None,
    };
    assert!(
        err.is_retryable(),
        "proxy passthrough 400s must get the retry budget"
    );
}

#[test]
fn transient_proxy_400_retryable_on_empty_type_and_empty_message() {
    let err = ProviderError::ApiError {
        status: 400,
        message: String::new(),
        error_type: Some(String::new()),
    };
    assert!(err.is_retryable());
}

#[test]
fn transient_proxy_400_not_retryable_when_real_error_type_present() {
    let err = ProviderError::ApiError {
        status: 400,
        message:
            "thinking is enabled but reasoning_content is missing in assistant tool call message at index 39"
                .to_string(),
        error_type: Some("invalid_request_error".to_string()),
    };
    assert!(
        !err.is_retryable(),
        "real invalid_request_error must not be retried"
    );
}

#[test]
fn transient_proxy_400_not_retryable_on_specific_client_messages() {
    let err = ProviderError::ApiError {
        status: 400,
        message: "invalid model 'x'".to_string(),
        error_type: None,
    };
    assert!(
        !err.is_retryable(),
        "specific client-side 400 messages stay non-retryable"
    );
}

#[test]
fn is_transient_proxy_400_recognizes_known_phrases() {
    assert!(is_transient_proxy_400("Provider returned error", None));
    assert!(is_transient_proxy_400("Upstream error", Some("")));
    assert!(is_transient_proxy_400("Internal error", None));
    assert!(is_transient_proxy_400("Bad Gateway", Some("")));
    assert!(is_transient_proxy_400("Please try again", None));
    assert!(is_transient_proxy_400("", None));
}

#[test]
fn is_transient_proxy_400_rejects_actionable_messages() {
    assert!(!is_transient_proxy_400(
        "invalid api key format",
        Some("authentication_error")
    ));
    assert!(!is_transient_proxy_400(
        "model 'foo' not found",
        Some("model_not_found")
    ));
    assert!(!is_transient_proxy_400("some random reason", None));
}

// ─── temporary unavailability (overload/capacity) on 4xx JSON (#505) ─
// Some providers report an overloaded/at-capacity model as a 4xx JSON API
// error instead of 429/5xx. Those must be retried in place and (through
// is_retryable) fall to the next provider, WITHOUT reclassifying permanent
// model/auth errors.

#[test]
fn overload_400_with_real_error_type_is_retryable() {
    // A 400 that carries a real error_type would fail is_transient_proxy_400
    // (non-empty type), but the overload wording makes it temporary.
    let err = ProviderError::ApiError {
        status: 400,
        message: "The model is currently overloaded, please try again later".to_string(),
        error_type: Some("invalid_request_error".to_string()),
    };
    assert!(err.is_temporarily_unavailable());
    assert!(
        err.is_retryable(),
        "an overloaded 400 must get the retry budget, not surface"
    );
}

#[test]
fn capacity_409_is_temporary_and_retryable() {
    // A non-400 4xx overload (409) is invisible to is_transient_proxy_400
    // and to should_try_next's 400 arm; the classifier catches it.
    let err = ProviderError::ApiError {
        status: 409,
        message: "Model at capacity".to_string(),
        error_type: Some("capacity".to_string()),
    };
    assert!(err.is_temporarily_unavailable());
    assert!(err.is_retryable());
}

#[test]
fn temporarily_unavailable_503_style_type_on_4xx() {
    let err = ProviderError::ApiError {
        status: 400,
        message: "backend temporarily unavailable".to_string(),
        error_type: Some("server_error".to_string()),
    };
    assert!(err.is_retryable());
}

#[test]
fn permanent_model_not_found_is_not_temporary() {
    // Must NOT be reclassified: routes to the model-mismatch path, not retry.
    let err = ProviderError::ApiError {
        status: 404,
        message: "The model `foo` does not exist or you do not have access".to_string(),
        error_type: Some("model_not_found".to_string()),
    };
    assert!(
        !err.is_temporarily_unavailable(),
        "a permanent model error must never read as temporary"
    );
}

#[test]
fn auth_error_is_not_temporary_even_with_retry_wording() {
    // Belt-and-suspenders: an auth body must stay permanent even if it says
    // "try again".
    let err = ProviderError::ApiError {
        status: 401,
        message: "Invalid API key, please try again with a valid key".to_string(),
        error_type: Some("authentication_error".to_string()),
    };
    assert!(!err.is_temporarily_unavailable());
    // 401 still falls back (dead credential), but via the auth arm, not as a
    // retryable transient error.
    assert!(!err.is_retryable());
}

#[test]
fn is_temporary_unavailable_signal_positive_and_negative() {
    // Overload/capacity/try-again vocabulary → transient.
    assert!(is_temporary_unavailable_signal("Model overloaded", None));
    assert!(is_temporary_unavailable_signal(
        "service unavailable",
        Some("")
    ));
    assert!(is_temporary_unavailable_signal(
        "we are experiencing high demand",
        None
    ));
    assert!(is_temporary_unavailable_signal("please try again", None));
    assert!(is_temporary_unavailable_signal(
        "busy",
        Some("overloaded_error")
    ));
    // Permanent / actionable → not transient.
    assert!(!is_temporary_unavailable_signal(
        "model not found",
        Some("model_not_found")
    ));
    assert!(!is_temporary_unavailable_signal(
        "invalid api key",
        Some("authentication_error")
    ));
    assert!(!is_temporary_unavailable_signal("some unrelated 400", None));
}

// ─── needs_reasoning_content_for ────────────────────────────────────

#[test]
fn reasoning_needed_for_opencode_kimi() {
    assert!(needs_reasoning_content_for(
        "https://opencode.ai/zen/go/v1/chat/completions",
        "kimi-k2.6"
    ));
    assert!(needs_reasoning_content_for(
        "https://opencode.ai/zen/go/v1/chat/completions",
        "Kimi-K2.6"
    ));
}

#[test]
fn reasoning_needed_for_direct_moonshot() {
    assert!(needs_reasoning_content_for(
        "https://api.moonshot.ai/v1/chat/completions",
        "moonshot-v1"
    ));
}

#[test]
fn reasoning_not_needed_for_opencode_qwen() {
    assert!(!needs_reasoning_content_for(
        "https://opencode.ai/zen/go/v1/chat/completions",
        "qwen3.6-plus"
    ));
}

#[test]
fn reasoning_not_needed_for_unrelated_providers() {
    assert!(!needs_reasoning_content_for(
        "https://api.z.ai/api/coding/paas/v4/chat/completions",
        "glm-5.1"
    ));
    assert!(!needs_reasoning_content_for(
        "https://api.minimax.io/v1/chat/completions",
        "MiniMax-M2.7"
    ));
    assert!(!needs_reasoning_content_for(
        "https://api.openai.com/v1/chat/completions",
        "gpt-5"
    ));
}

// ─── HTML error pages on 4xx are transient infra errors (retryable) ──
// Regression (2026-06-07): modelscope intermittently returned HTTP 405
// with a Chinese HTML error page for a valid POST. 405 was non-retryable,
// so the request bounced straight to the fallback chain with ZERO retries
// — the user saw "no resilience, instant fallback" and a manual swap-back
// worked because the 405 was a transient infra blip.

#[test]
fn html_body_detected_as_infra_error() {
    for body in [
        "<!doctypehtml><html lang=\"zh-cn\"><meta charset=\"utf-8\">...",
        "<!DOCTYPE html>\n<html><head><title>405</title></head>",
        "  \n  <html><body>Method Not Allowed</body></html>",
        "<head><title>502 Bad Gateway</title></head>",
    ] {
        assert!(is_html_error_body(body), "should be HTML: {body:?}");
    }
}

#[test]
fn json_body_not_detected_as_infra_error() {
    for body in [
        r#"{"error":{"message":"invalid model","type":"invalid_request_error"}}"#,
        r#"{"error":"unauthorized"}"#,
        "Method Not Allowed",
        "rate limit exceeded, retry in 5s",
    ] {
        assert!(!is_html_error_body(body), "should NOT be HTML: {body:?}");
    }
}

#[test]
fn http_405_with_html_body_is_retryable() {
    // The exact modelscope case: 405 + HTML page → retry, don't instant-fail.
    let err = ProviderError::ApiError {
        status: 405,
        message: "<!doctypehtml><html lang=\"zh-cn\">Method Not Allowed</html>".to_string(),
        error_type: None,
    };
    assert!(
        err.is_retryable(),
        "a 405 with an HTML infra error page must be retryable, not bounced to fallback"
    );
}

#[test]
fn http_405_with_json_body_is_not_retryable() {
    // A genuine API 405 (JSON) is a real client error — do not retry.
    let err = ProviderError::ApiError {
        status: 405,
        message: r#"{"error":{"message":"method not allowed","type":"invalid_request_error"}}"#
            .to_string(),
        error_type: Some("invalid_request_error".to_string()),
    };
    assert!(
        !err.is_retryable(),
        "a JSON 405 client error must stay non-retryable"
    );
}

#[test]
fn http_404_html_retryable_but_json_not() {
    let html = ProviderError::ApiError {
        status: 404,
        message: "<html><head><title>404 Not Found</title></head></html>".to_string(),
        error_type: None,
    };
    assert!(html.is_retryable(), "404 HTML infra page → retryable");

    let json = ProviderError::ApiError {
        status: 404,
        message: r#"{"error":"model not found"}"#.to_string(),
        error_type: None,
    };
    assert!(
        !json.is_retryable(),
        "404 JSON client error → not retryable"
    );
}