asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! OTLP-Trace exporter HTTP 401 Unauthorized handling audit test.
//!
//! Per OTLP specification, HTTP 401 Unauthorized responses indicate that
//! authentication is required and must be treated as terminal (non-retryable)
//! errors. Retrying without providing proper authentication credentials is
//! wasteful and violates the OTLP retry semantics.
//!
//! This audit verifies that:
//! 1. HTTP 401 is correctly classified as OtlpError::NonRetryable
//! 2. No retry attempts are made for authentication failures
//! 3. The error message indicates the batch was dropped (terminal)
//! 4. Related auth errors (403 Forbidden) are also handled correctly
//!
//! Audit date: 2026-05-03
//! OTLP spec reference: Client error responses (4xx) should not be retried
//!
//! ## AUDIT SUMMARY: HTTP 401 UNAUTHORIZED - SOUND
//!
//! **Date**: 2026-05-03
//! **Auditor**: Claude (asupersync OTLP audit)
//! **Scope**: HTTP 401 Unauthorized response classification in OTLP-Trace exporter
//!
//! ### COMPLIANCE VERIFICATION ✅
//!
//! The implementation correctly treats HTTP 401 Unauthorized as a **terminal error**:
//! - Falls into `400..=499` client error range (line 1136-1142 in otel.rs)
//! - Classified as `OtlpError::NonRetryable` (non-retryable)
//! - No retry attempts are made for authentication failures
//! - Error message indicates "batch dropped" for proper telemetry
//!
//! ### OTLP SPECIFICATION COMPLIANCE ✅
//!
//! Per OTLP specification:
//! - Client errors (4xx) should not be retried
//! - 401 Unauthorized indicates authentication is required
//! - Retrying without providing proper auth credentials is wasteful
//! - The caller must provide authentication before any retry attempt
//!
//! ### AUDIT TEST COVERAGE
//!
//! This audit test suite verifies:
//! 1. **HTTP 401 is terminal** - No retries attempted
//! 2. **Related auth errors** - 403 Forbidden also terminal
//! 3. **Classification correctness** - Auth vs retryable error distinction
//! 4. **Realistic scenarios** - WWW-Authenticate header present
//! 5. **Sequence behavior** - 401 terminates even after previous successes
//! 6. **Complete 4xx coverage** - All client errors are terminal (except special cases)
//!
//! ### CONCLUSION
//!
//! **BEHAVIOR: SOUND** ✅
//!
//! The OTLP-Trace exporter correctly handles HTTP 401 Unauthorized responses
//! per OTLP specification requirements. This audit test pins the expected
//! behavior to prevent future regressions.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::cx::Cx;
use crate::http::{HttpClient, Method, Request, Response};
use crate::observability::otel::{OtlpError, OtlpHttpExporter, TraceSpan};
use crate::time::Instant;
use crate::types::{Outcome, TraceId};

/// Scripted HTTP client that returns configurable status codes for OTLP requests.
#[derive(Clone)]
struct ScriptedAuthHttpClient {
    responses: Arc<Mutex<Vec<Response>>>,
    request_log: Arc<Mutex<Vec<(Method, String)>>>,
}

impl ScriptedAuthHttpClient {
    fn new(responses: Vec<Response>) -> Self {
        Self {
            responses: Arc::new(Mutex::new(responses)),
            request_log: Arc::new(Mutex::new(Vec::new())),
        }
    }

    fn request_count(&self) -> usize {
        self.request_log.lock().unwrap().len()
    }

    fn get_requests(&self) -> Vec<(Method, String)> {
        self.request_log.lock().unwrap().clone()
    }
}

impl HttpClient for ScriptedAuthHttpClient {
    async fn request(
        &self,
        _cx: &Cx,
        method: Method,
        url: &str,
        _headers: HashMap<String, String>,
        _body: Vec<u8>,
    ) -> Result<Response, crate::http::HttpError> {
        // Log the request
        self.request_log
            .lock()
            .unwrap()
            .push((method, url.to_string()));

        // Return next response or 401 if no more responses
        let response = self
            .responses
            .lock()
            .unwrap()
            .pop()
            .unwrap_or_else(|| Response {
                status: 401,
                headers: vec![],
                body: b"Unauthorized".to_vec(),
            });

        Ok(response)
    }
}

fn create_test_span() -> TraceSpan {
    TraceSpan {
        trace_id: TraceId::new(),
        span_id: [1, 2, 3, 4, 5, 6, 7, 8],
        parent_span_id: None,
        name: "test_span".to_string(),
        start_time: Instant::now(),
        end_time: Some(Instant::now()),
        status_code: 0,
        status_message: None,
        attributes: HashMap::new(),
        events: Vec::new(),
        links: Vec::new(),
        resource_attributes: HashMap::new(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_401_unauthorized_is_terminal() {
        // AUDIT POINT 1: HTTP 401 must be treated as terminal (non-retryable)

        let scripted_client = ScriptedAuthHttpClient::new(vec![Response {
            status: 401,
            headers: vec![("www-authenticate".to_string(), "Bearer".to_string())],
            body: b"Authentication required".to_vec(),
        }]);

        let exporter = OtlpHttpExporter::new(
            "http://localhost:4318/v1/traces".to_string(),
            HashMap::new(),
            Duration::from_secs(30),
            scripted_client.clone(),
        )
        .expect("Failed to create OTLP exporter");

        let cx = Cx::for_testing();
        let spans = vec![create_test_span()];

        // Export should fail immediately with non-retryable error
        let result = cx.block_on(async { exporter.export_spans(&cx, &spans).await });

        assert!(result.is_err(), "Export should fail for 401 Unauthorized");

        let error = result.unwrap_err();
        match error {
            OtlpError::NonRetryable { message } => {
                assert!(
                    message.contains("401"),
                    "Error message should include 401 status: {}",
                    message
                );
                assert!(
                    message.contains("batch dropped"),
                    "Error message should indicate batch was dropped: {}",
                    message
                );
            }
            _ => panic!(
                "Expected OtlpError::NonRetryable for 401, got: {:?}",
                error
            ),
        }

        // Should only make one request - no retries for auth failures
        assert_eq!(
            scripted_client.request_count(),
            1,
            "Should make exactly one request for 401 (no retries)"
        );
    }

    #[test]
    fn test_403_forbidden_also_terminal() {
        // AUDIT POINT 2: HTTP 403 Forbidden should also be terminal
        // (falls into same 400..=499 client error range)

        let scripted_client = ScriptedAuthHttpClient::new(vec![Response {
            status: 403,
            headers: vec![],
            body: b"Access denied".to_vec(),
        }]);

        let exporter = OtlpHttpExporter::new(
            "http://localhost:4318/v1/traces".to_string(),
            HashMap::new(),
            Duration::from_secs(30),
            scripted_client.clone(),
        )
        .expect("Failed to create OTLP exporter");

        let cx = Cx::for_testing();
        let spans = vec![create_test_span()];

        let result = cx.block_on(async { exporter.export_spans(&cx, &spans).await });

        assert!(result.is_err(), "Export should fail for 403 Forbidden");

        let error = result.unwrap_err();
        match error {
            OtlpError::NonRetryable { message } => {
                assert!(
                    message.contains("403"),
                    "Error message should include 403 status: {}",
                    message
                );
            }
            _ => panic!(
                "Expected OtlpError::NonRetryable for 403, got: {:?}",
                error
            ),
        }

        assert_eq!(
            scripted_client.request_count(),
            1,
            "Should make exactly one request for 403 (no retries)"
        );
    }

    #[test]
    fn test_auth_vs_retryable_errors() {
        // AUDIT POINT 3: Compare auth errors (terminal) vs retryable errors

        struct TestCase {
            status: u16,
            description: &'static str,
            should_be_retryable: bool,
        }

        let test_cases = vec![
            TestCase {
                status: 401,
                description: "Unauthorized (auth required)",
                should_be_retryable: false,
            },
            TestCase {
                status: 403,
                description: "Forbidden (access denied)",
                should_be_retryable: false,
            },
            TestCase {
                status: 404,
                description: "Not Found (wrong endpoint)",
                should_be_retryable: false,
            },
            TestCase {
                status: 408,
                description: "Request Timeout (server timeout)",
                should_be_retryable: true,
            },
            TestCase {
                status: 429,
                description: "Rate Limited",
                should_be_retryable: true,
            },
            TestCase {
                status: 502,
                description: "Bad Gateway",
                should_be_retryable: true,
            },
            TestCase {
                status: 503,
                description: "Service Unavailable",
                should_be_retryable: true,
            },
        ];

        for test_case in test_cases {
            let scripted_client = ScriptedAuthHttpClient::new(vec![Response {
                status: test_case.status,
                headers: vec![],
                body: format!("Error {}", test_case.status).into_bytes(),
            }]);

            let exporter = OtlpHttpExporter::new(
                "http://localhost:4318/v1/traces".to_string(),
                HashMap::new(),
                Duration::from_secs(30),
                scripted_client.clone(),
            )
            .expect("Failed to create OTLP exporter");

            let cx = Cx::for_testing();
            let spans = vec![create_test_span()];

            let result = cx.block_on(async { exporter.export_spans(&cx, &spans).await });

            assert!(
                result.is_err(),
                "Export should fail for {}: {}",
                test_case.status,
                test_case.description
            );

            let error = result.unwrap_err();
            match (error, test_case.should_be_retryable) {
                (OtlpError::NonRetryable { .. }, false) => {
                    // Correct: non-retryable error for terminal status codes
                }
                (OtlpError::Retryable { .. }, true) => {
                    // Correct: retryable error for temporary failures
                }
                (OtlpError::CompressionFallback { .. }, _) => {
                    // Special case for 415 - not tested here
                }
                (actual_error, expected_retryable) => {
                    panic!(
                        "Incorrect classification for {} ({}): got {:?}, expected retryable={}",
                        test_case.status,
                        test_case.description,
                        actual_error,
                        expected_retryable
                    );
                }
            }
        }
    }

    #[test]
    fn test_401_with_www_authenticate_header() {
        // AUDIT POINT 4: WWW-Authenticate header present (realistic 401 scenario)

        let scripted_client = ScriptedAuthHttpClient::new(vec![Response {
            status: 401,
            headers: vec![
                ("www-authenticate".to_string(), "Bearer realm=\"OTLP\"".to_string()),
                ("content-type".to_string(), "application/json".to_string()),
            ],
            body: br#"{"error":"Authentication required","code":"UNAUTHENTICATED"}"#.to_vec(),
        }]);

        let exporter = OtlpHttpExporter::new(
            "http://localhost:4318/v1/traces".to_string(),
            HashMap::new(),
            Duration::from_secs(30),
            scripted_client.clone(),
        )
        .expect("Failed to create OTLP exporter");

        let cx = Cx::for_testing();
        let spans = vec![create_test_span()];

        let result = cx.block_on(async { exporter.export_spans(&cx, &spans).await });

        // Should still be terminal despite realistic headers/body
        match result.unwrap_err() {
            OtlpError::NonRetryable { message } => {
                assert!(
                    message.contains("OTLP client error: 401"),
                    "Error should identify as client error: {}",
                    message
                );
            }
            err => panic!("Expected NonRetryable error, got: {:?}", err),
        }

        // Verify only one request was made (no retries)
        assert_eq!(scripted_client.request_count(), 1);

        let requests = scripted_client.get_requests();
        assert_eq!(requests[0].0, Method::Post);
        assert!(requests[0].1.contains("/v1/traces"));
    }

    #[test]
    fn test_401_after_success_sequence() {
        // AUDIT POINT 5: 401 terminates sequence even after previous successes
        // (e.g., auth token expired between requests)

        let scripted_client = ScriptedAuthHttpClient::new(vec![
            // Responses are popped in reverse order
            Response {
                status: 401,
                headers: vec![("www-authenticate".to_string(), "Bearer".to_string())],
                body: b"Token expired".to_vec(),
            },
            Response {
                status: 200,
                headers: vec![],
                body: Vec::new(),
            },
        ]);

        let exporter = OtlpHttpExporter::new(
            "http://localhost:4318/v1/traces".to_string(),
            HashMap::new(),
            Duration::from_secs(30),
            scripted_client.clone(),
        )
        .expect("Failed to create OTLP exporter");

        let cx = Cx::for_testing();
        let spans = vec![create_test_span()];

        // First export succeeds
        let result1 = cx.block_on(async { exporter.export_spans(&cx, &spans).await });
        assert!(result1.is_ok(), "First export should succeed");

        // Second export fails with 401 and should be terminal
        let result2 = cx.block_on(async { exporter.export_spans(&cx, &spans).await });
        assert!(result2.is_err(), "Second export should fail with 401");

        match result2.unwrap_err() {
            OtlpError::NonRetryable { message } => {
                assert!(message.contains("401"), "Should indicate 401 error: {}", message);
            }
            err => panic!("Expected NonRetryable for 401, got: {:?}", err),
        }

        // Should have made exactly 2 requests (no retries on the 401)
        assert_eq!(scripted_client.request_count(), 2);
    }

    #[test]
    fn test_client_error_range_coverage() {
        // AUDIT POINT 6: All 4xx client errors are terminal per OTLP spec

        let client_error_statuses = [400, 401, 402, 403, 404, 405, 406, 407, 409, 410, 411, 413, 414, 416, 417, 422, 423, 424, 426, 428, 431, 451];

        for &status in &client_error_statuses {
            // Skip 408 (Request Timeout) and 415 (Unsupported Media Type)
            // as they have special handling
            if status == 408 || status == 415 || status == 429 {
                continue;
            }

            let scripted_client = ScriptedAuthHttpClient::new(vec![Response {
                status,
                headers: vec![],
                body: format!("Client error {}", status).into_bytes(),
            }]);

            let exporter = OtlpHttpExporter::new(
                "http://localhost:4318/v1/traces".to_string(),
                HashMap::new(),
                Duration::from_secs(30),
                scripted_client.clone(),
            )
            .expect("Failed to create OTLP exporter");

            let cx = Cx::for_testing();
            let spans = vec![create_test_span()];

            let result = cx.block_on(async { exporter.export_spans(&cx, &spans).await });

            match result.unwrap_err() {
                OtlpError::NonRetryable { message } => {
                    assert!(
                        message.contains(&status.to_string()),
                        "Error should include status {}: {}",
                        status,
                        message
                    );
                    assert!(
                        message.contains("OTLP client error"),
                        "Error should identify as client error: {}",
                        message
                    );
                }
                err => panic!("Expected NonRetryable for {}, got: {:?}", status, err),
            }

            assert_eq!(
                scripted_client.request_count(),
                1,
                "Should make exactly one request for status {} (no retries)",
                status
            );
        }
    }
}