aft-base 0.67.1

Shared foundation for Agent Foundational Toolkit
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! URL fetches against local test servers: deadlines for servers that stall
//! in one phase, remembered failures, and reuse of the document cache. Most
//! tests run the fetch under a short call budget, the same way a tool call
//! does, and check the typed timeout and how long it took.

use super::*;
use std::net::TcpListener;
use std::net::TcpStream;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;

/// Starts a loopback server. `handler` gets each connection after the request
/// head has been read, together with the request path.
pub fn serve(handler: impl Fn(&str, TcpStream) + Send + Sync + 'static) -> SocketAddr {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
    let address = listener.local_addr().expect("test server address");
    let handler = Arc::new(handler);
    thread::spawn(move || {
        for stream in listener.incoming().flatten() {
            let handler = Arc::clone(&handler);
            thread::spawn(move || {
                let mut stream = stream;
                let path = read_request_path(&mut stream);
                handler(&path, stream);
            });
        }
    });
    address
}

fn read_request_path(stream: &mut TcpStream) -> String {
    let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
    let mut head = Vec::new();
    let mut buffer = [0u8; 1024];
    while !head.windows(4).any(|window| window == b"\r\n\r\n") {
        match stream.read(&mut buffer) {
            Ok(0) | Err(_) => break,
            Ok(n) => head.extend_from_slice(&buffer[..n]),
        }
    }
    String::from_utf8_lossy(&head)
        .split_whitespace()
        .nth(1)
        .unwrap_or("/")
        .to_string()
}

pub fn url(address: SocketAddr, path: &str) -> String {
    format!("http://127.0.0.1:{}{path}", address.port())
}

fn local() -> UrlFetchOptions {
    UrlFetchOptions {
        allow_private: true,
        ..UrlFetchOptions::default()
    }
}

/// Runs one fetch inside a call budget of `budget`, as a tool call would.
fn fetch_within(
    budget: Duration,
    url: &str,
    storage: &Path,
    options: UrlFetchOptions,
) -> (Result<PathBuf, UrlFetchError>, Duration) {
    let _call = crate::bounded_io::enter(Some(crate::bounded_io::Budget::after(budget)));
    let started = Instant::now();
    let result = fetch_url_to_cache(url, storage, options);
    (result, started.elapsed())
}

/// The call budget most tests run under. The fetch deadline is 2 s of it:
/// the rest is the read-back reserve and the reply margin.
pub const CALL_BUDGET: Duration = Duration::from_secs(5);
/// A fetch under [`CALL_BUDGET`] must have returned by this time: its 2 s
/// deadline plus 1 s for scheduling on a loaded machine.
pub const FETCH_DONE_BY: Duration = Duration::from_secs(3);

/// Long enough that a fetch which ignored its deadline could not finish a
/// test in time, short enough that leftover server threads end soon.
pub const SERVER_STALL: Duration = Duration::from_secs(60);

#[test]
fn fetch_deadline_is_the_call_budget_less_the_read_back_reserve_and_reply_margin_capped_at_the_ceiling(
) {
    let near = |deadline: FetchDeadline, expected: Duration| {
        let allowed = deadline.allowed;
        assert!(
            allowed <= expected && allowed + Duration::from_millis(200) >= expected,
            "allowed {allowed:?}, expected about {expected:?}"
        );
    };
    near(FetchDeadline::for_current_call(), URL_FETCH_CEILING);
    for (budget, expected) in [
        (Duration::from_secs(5), Duration::from_secs(2)),
        (Duration::from_secs(60), URL_FETCH_CEILING),
        (Duration::from_millis(2_500), Duration::ZERO),
    ] {
        let _call = crate::bounded_io::enter(Some(crate::bounded_io::Budget::after(budget)));
        near(FetchDeadline::for_current_call(), expected);
    }
}

#[test]
fn header_stall_times_out_in_the_headers_phase_within_the_call_budget() {
    let storage = tempfile::tempdir().unwrap();
    let address = serve(|_, _stream| thread::sleep(SERVER_STALL));
    let (result, elapsed) = fetch_within(
        CALL_BUDGET,
        &url(address, "/silent.md"),
        storage.path(),
        local(),
    );
    let error = result.expect_err("a server that never answers must fail");
    assert_eq!(error.code(), "url_fetch_timeout", "{error}");
    assert_eq!(error.phase(), Some(FetchPhase::Headers), "{error}");
    assert!(error.to_string().contains("headers phase"), "{error}");
    assert!(
        elapsed < FETCH_DONE_BY,
        "the fetch outlived its deadline: {elapsed:?}"
    );
}

#[test]
fn body_drip_stops_at_the_whole_body_deadline() {
    let storage = tempfile::tempdir().unwrap();
    // Each gap is far below the per-chunk stall limit, so only a deadline for
    // the whole body can stop this response.
    let address = serve(|_, mut stream| {
        let _ = stream.write_all(
            b"HTTP/1.1 200 OK\r\ncontent-type: text/markdown\r\nconnection: close\r\n\r\n",
        );
        let started = Instant::now();
        while started.elapsed() < SERVER_STALL {
            if stream.write_all(b"## drip\n\nline\n\n").is_err() {
                return;
            }
            thread::sleep(Duration::from_millis(200));
        }
    });
    let (result, elapsed) = fetch_within(
        CALL_BUDGET,
        &url(address, "/drip.md"),
        storage.path(),
        local(),
    );
    let error = result.expect_err("an endless body must fail at the deadline");
    assert_eq!(error.code(), "url_fetch_timeout", "{error}");
    assert_eq!(error.phase(), Some(FetchPhase::Body), "{error}");
    assert!(
        elapsed < FETCH_DONE_BY,
        "the body read outlived its deadline: {elapsed:?}"
    );
}

/// A server that accepts and closes every connection without answering, which
/// reqwest reports as a transient failure worth a retry.
fn hang_up_server() -> (SocketAddr, Arc<AtomicUsize>) {
    let accepted = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&accepted);
    let address = serve(move |_, stream| {
        counter.fetch_add(1, Ordering::SeqCst);
        drop(stream);
    });
    (address, accepted)
}

#[test]
fn transient_failures_retry_while_the_backoff_and_an_attempt_fit() {
    let storage = tempfile::tempdir().unwrap();
    let (address, accepted) = hang_up_server();
    let (result, _) = fetch_within(
        Duration::from_secs(10),
        &url(address, "/closed.md"),
        storage.path(),
        local(),
    );
    assert!(result.is_err());
    assert_eq!(
        accepted.load(Ordering::SeqCst),
        1 + TRANSIENT_RETRY_ATTEMPTS
    );
}

#[test]
fn transient_failures_do_not_retry_when_the_backoff_and_an_attempt_do_not_fit() {
    let storage = tempfile::tempdir().unwrap();
    let (address, accepted) = hang_up_server();
    // 3.6 s of budget leaves a 0.6 s fetch deadline: less than the first
    // backoff plus one useful attempt.
    let (result, elapsed) = fetch_within(
        Duration::from_millis(3_600),
        &url(address, "/closed.md"),
        storage.path(),
        local(),
    );
    assert!(result.is_err());
    assert_eq!(accepted.load(Ordering::SeqCst), 1);
    assert!(elapsed < Duration::from_millis(600), "{elapsed:?}");
}

fn stalled_lookup() -> HostLookup {
    Arc::new(|_host: &str| {
        thread::sleep(SERVER_STALL);
        Ok(vec![IpAddr::V4(Ipv4Addr::LOCALHOST)])
    })
}

#[test]
fn stalled_name_lookup_times_out_in_the_dns_phase_before_the_ssrf_check_answers() {
    let storage = tempfile::tempdir().unwrap();
    let options = UrlFetchOptions {
        lookup_override: Some(stalled_lookup()),
        ..UrlFetchOptions::default()
    };
    let (result, elapsed) = fetch_within(
        CALL_BUDGET,
        "http://slow-dns.test/doc.md",
        storage.path(),
        options,
    );
    let error = result.expect_err("a lookup that never answers must fail");
    assert_eq!(error.code(), "url_fetch_timeout", "{error}");
    assert_eq!(error.phase(), Some(FetchPhase::Dns), "{error}");
    assert!(elapsed < FETCH_DONE_BY, "{elapsed:?}");
}

#[test]
fn stalled_name_lookup_inside_the_connection_times_out_in_the_dns_phase() {
    // With private addresses allowed there is no SSRF lookup first, so the
    // connection's own lookup is the one that stalls.
    let storage = tempfile::tempdir().unwrap();
    let options = UrlFetchOptions {
        allow_private: true,
        lookup_override: Some(stalled_lookup()),
        ..UrlFetchOptions::default()
    };
    let (result, elapsed) = fetch_within(
        CALL_BUDGET,
        "http://slow-dns.test/doc.md",
        storage.path(),
        options,
    );
    let error = result.expect_err("a lookup that never answers must fail");
    assert_eq!(error.code(), "url_fetch_timeout", "{error}");
    assert_eq!(error.phase(), Some(FetchPhase::Dns), "{error}");
    assert!(elapsed < FETCH_DONE_BY, "{elapsed:?}");
}

#[test]
fn host_resolving_to_a_private_address_is_refused() {
    let storage = tempfile::tempdir().unwrap();
    let options = UrlFetchOptions {
        lookup_override: Some(Arc::new(|_host: &str| {
            Ok(vec![IpAddr::V4(Ipv4Addr::new(10, 0, 0, 5))])
        })),
        ..UrlFetchOptions::default()
    };
    let (result, _) = fetch_within(
        CALL_BUDGET,
        "http://intranet.test/doc.md",
        storage.path(),
        options,
    );
    let error = result.expect_err("a private answer must be refused");
    assert!(
        error.to_string().contains("Blocked private URL host"),
        "{error}"
    );
}

#[test]
fn connection_uses_the_addresses_the_ssrf_check_approved_without_a_second_lookup() {
    // The first answer is public; any later answer is loopback, as a
    // DNS-rebinding server would reply. The connection must get the first.
    let lookups = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&lookups);
    let approved_ip = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 9));
    let options = UrlFetchOptions {
        lookup_override: Some(Arc::new(move |_host: &str| {
            let first = counter.fetch_add(1, Ordering::SeqCst) == 0;
            Ok(vec![if first {
                approved_ip
            } else {
                IpAddr::V4(Ipv4Addr::LOCALHOST)
            }])
        })),
        ..UrlFetchOptions::default()
    };
    let deadline = FetchDeadline::for_current_call();
    let approved = Arc::new(ApprovedHosts::default());
    let target = Url::parse("http://rebind.test/doc.md").unwrap();
    validate_public_url(&target, &options, &deadline, &approved).expect("public answer");

    let resolver = GuardedResolver {
        approved,
        connect_overrides: Vec::new(),
        lookup_override: options.lookup_override.clone(),
        progress: Arc::new(ConnectProgress::default()),
    };
    let name: reqwest::dns::Name = "rebind.test".parse().unwrap();
    let addresses: Vec<SocketAddr> = tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap()
        .block_on(reqwest::dns::Resolve::resolve(&resolver, name))
        .expect("approved host resolves")
        .collect();
    assert_eq!(addresses, vec![SocketAddr::new(approved_ip, 0)]);
    assert_eq!(lookups.load(Ordering::SeqCst), 1, "no second lookup");
}

/// Counts requests per path and answers each path with the status its name
/// gives (`/503`, `/404`, ...); `/flaky` answers 503 once and 200 after.
pub fn status_server() -> (SocketAddr, Arc<std::sync::Mutex<HashMap<String, usize>>>) {
    let seen = Arc::new(std::sync::Mutex::new(HashMap::<String, usize>::new()));
    let counter = Arc::clone(&seen);
    let address = serve(move |path, mut stream| {
        let count = {
            let mut seen = counter.lock().unwrap();
            let count = seen.entry(path.to_string()).or_default();
            *count += 1;
            *count
        };
        let status = match path.trim_start_matches('/') {
            "flaky" if count > 1 => "200 OK",
            "flaky" | "503" => "503 Service Unavailable",
            "404" => "404 Not Found",
            "408" => "408 Request Timeout",
            "429" => "429 Too Many Requests",
            _ => "200 OK",
        };
        let body = b"# Doc\n\n## Section\n\ntext\n";
        let _ = write!(
            stream,
            "HTTP/1.1 {status}\r\ncontent-type: text/markdown\r\ncontent-length: {}\r\nconnection: close\r\n\r\n",
            body.len()
        );
        let _ = stream.write_all(body);
    });
    (address, seen)
}

#[test]
fn a_server_that_hit_the_headers_limit_fails_from_memory_without_a_new_fetch() {
    let storage = tempfile::tempdir().unwrap();
    let connections = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&connections);
    let address = serve(move |_, _stream| {
        counter.fetch_add(1, Ordering::SeqCst);
        thread::sleep(SERVER_STALL);
    });
    let target = url(address, "/silent.md");
    // A budget large enough that the 15 s headers limit, not the caller's
    // budget, ends the wait: the server is to blame.
    let roomy = Duration::from_secs(30);
    let (first, _) = fetch_within(roomy, &target, storage.path(), local());
    assert_eq!(first.unwrap_err().code(), "url_fetch_timeout");

    let (second, elapsed) = fetch_within(roomy, &target, storage.path(), local());
    let error = second.expect_err("the remembered failure is repeated");
    // A new fetch would wait out the 15 s headers limit again, so anything well
    // under that proves the answer came from memory. The connection count
    // below is the exact check; this bound only has to survive a loaded runner
    // (a Windows CI run took 212 ms).
    assert!(elapsed < Duration::from_secs(5), "{elapsed:?}");
    assert_eq!(error.code(), "url_fetch_timeout", "{error}");
    assert_eq!(error.phase(), Some(FetchPhase::Headers), "{error}");
    assert!(error.to_string().contains("cached failure"), "{error}");
    assert!(error.to_string().contains("fetched again after"), "{error}");
    let retry_after = error.retry_after().expect("a remembered failure says when");
    assert!(retry_after <= FAILURE_MEMORY && !retry_after.is_zero());
    assert_eq!(connections.load(Ordering::SeqCst), 1, "no second fetch");
}

/// Answers every request with a small Markdown document, after stalling
/// the first one; counts requests.
fn answers_after_a_first_stall(stall_first: bool) -> (SocketAddr, Arc<AtomicUsize>) {
    let requests = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&requests);
    let address = serve(move |_, mut stream| {
        if counter.fetch_add(1, Ordering::SeqCst) == 0 && stall_first {
            thread::sleep(SERVER_STALL);
            return;
        }
        let body = b"# Doc\n\n## Section\n\ntext\n";
        let _ = write!(
            stream,
            "HTTP/1.1 200 OK\r\ncontent-type: text/markdown\r\ncontent-length: {}\r\nconnection: close\r\n\r\n",
            body.len()
        );
        let _ = stream.write_all(body);
    });
    (address, requests)
}

#[test]
fn a_fetch_never_started_for_lack_of_budget_is_not_remembered() {
    let storage = tempfile::tempdir().unwrap();
    let (address, requests) = answers_after_a_first_stall(false);
    let target = url(address, "/late-in-a-batch.md");
    // Less budget than the read-back reserve and reply margin: no time is
    // left to fetch at all, as for a late URL in a long zoom batch.
    let (starved, _) = fetch_within(
        Duration::from_millis(2_900),
        &target,
        storage.path(),
        local(),
    );
    assert_eq!(starved.unwrap_err().code(), "url_fetch_timeout");
    assert_eq!(
        requests.load(Ordering::SeqCst),
        0,
        "the fetch never started"
    );

    let (fresh, _) = fetch_within(CALL_BUDGET, &target, storage.path(), local());
    fresh.expect("a call with a fresh budget fetches normally");
    assert_eq!(requests.load(Ordering::SeqCst), 1);
}

#[test]
fn a_fetch_cut_short_by_the_call_budget_is_not_remembered() {
    let storage = tempfile::tempdir().unwrap();
    let (address, requests) = answers_after_a_first_stall(true);
    let target = url(address, "/slow-once.md");
    // The 2 s fetch deadline this budget leaves ends the first request
    // before any limit of the fetcher's own would.
    let (cut, _) = fetch_within(CALL_BUDGET, &target, storage.path(), local());
    let error = cut.expect_err("the stalled first request is cut off");
    assert_eq!(error.phase(), Some(FetchPhase::Headers), "{error}");

    let (fresh, _) = fetch_within(CALL_BUDGET, &target, storage.path(), local());
    fresh.expect("a call with a fresh budget fetches normally");
    assert_eq!(requests.load(Ordering::SeqCst), 2);
}

#[test]
fn a_cached_document_is_served_while_a_failure_is_remembered() {
    let storage = tempfile::tempdir().unwrap();
    let (address, requests) = answers_after_a_first_stall(false);
    let target = url(address, "/cached.md");
    let (first, _) = fetch_within(CALL_BUDGET, &target, storage.path(), local());
    let cached = first.expect("first fetch");
    remember_failure(
        FailureKey::new(storage.path(), &target, true),
        &UrlFetchError::http_status(503, "HTTP 503 Service Unavailable"),
    );

    let (second, _) = fetch_within(CALL_BUDGET, &target, storage.path(), local());
    assert_eq!(second.expect("the cached document is served"), cached);
    assert_eq!(requests.load(Ordering::SeqCst), 1, "no new fetch");
}

#[test]
fn server_errors_and_client_errors_other_than_408_and_429_are_remembered() {
    let storage = tempfile::tempdir().unwrap();
    let (address, seen) = status_server();
    for path in ["/503", "/404", "/408", "/429"] {
        for _ in 0..2 {
            let (result, _) =
                fetch_within(CALL_BUDGET, &url(address, path), storage.path(), local());
            assert!(result.is_err(), "{path}");
        }
    }
    let seen = seen.lock().unwrap();
    assert_eq!(seen.get("/503"), Some(&1), "{seen:?}");
    assert_eq!(seen.get("/404"), Some(&1), "{seen:?}");
    assert_eq!(seen.get("/408"), Some(&2), "{seen:?}");
    assert_eq!(seen.get("/429"), Some(&2), "{seen:?}");
}

#[test]
fn a_remembered_failure_expires_and_the_url_is_fetched_again() {
    let storage = tempfile::tempdir().unwrap();
    let (address, seen) = status_server();
    let target = url(address, "/flaky");
    let (first, _) = fetch_within(CALL_BUDGET, &target, storage.path(), local());
    assert!(first.unwrap_err().to_string().contains("HTTP 503"));

    age_remembered_failure(
        &FailureKey::new(storage.path(), &target, true),
        FAILURE_MEMORY + Duration::from_secs(1),
    );
    let (second, _) = fetch_within(CALL_BUDGET, &target, storage.path(), local());
    second.expect("an expired failure no longer stands in for the fetch");
    assert_eq!(seen.lock().unwrap().get("/flaky"), Some(&2));
}

#[test]
fn a_silent_server_gets_the_headers_limit_and_no_retry() {
    let storage = tempfile::tempdir().unwrap();
    let connections = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&connections);
    let address = serve(move |_, _stream| {
        counter.fetch_add(1, Ordering::SeqCst);
        thread::sleep(SERVER_STALL);
    });
    // The budget leaves the full 20 s ceiling, so the 15 s headers limit is
    // what ends the wait.
    let (result, elapsed) = fetch_within(
        Duration::from_secs(30),
        &url(address, "/silent.md"),
        storage.path(),
        local(),
    );
    let error = result.expect_err("a silent server must fail");
    assert_eq!(error.phase(), Some(FetchPhase::Headers), "{error}");
    assert!(error.to_string().contains("15000ms limit"), "{error}");
    assert!(
        elapsed >= HEADERS_TIMEOUT && elapsed < URL_FETCH_CEILING,
        "{elapsed:?}"
    );
    assert_eq!(connections.load(Ordering::SeqCst), 1);
}

#[test]
fn a_stalled_connect_ends_at_the_connect_limit() {
    let storage = tempfile::tempdir().unwrap();
    let address = serve(|_, _stream| thread::sleep(SERVER_STALL));
    let options = UrlFetchOptions {
        connect_delay: Some(SERVER_STALL),
        ..local()
    };
    // A 14 s budget leaves an 11 s fetch deadline: the 10 s connect limit ends the first attempt,
    // and the second does not fit in what is left.
    let (result, elapsed) = fetch_within(
        Duration::from_secs(14),
        &url(address, "/unreachable.md"),
        storage.path(),
        options,
    );
    let error = result.expect_err("a connect that never finishes must fail");
    assert_eq!(error.phase(), Some(FetchPhase::Connect), "{error}");
    assert!(error.to_string().contains("10000ms limit"), "{error}");
    assert!(
        elapsed >= CONNECT_TIMEOUT && elapsed < Duration::from_secs(11),
        "{elapsed:?}"
    );
}

#[test]
fn a_retry_stopped_by_the_deadline_while_connecting_is_named_the_connect_phase() {
    let storage = tempfile::tempdir().unwrap();
    let address = serve(|_, _stream| thread::sleep(SERVER_STALL));
    let options = UrlFetchOptions {
        connect_delay: Some(SERVER_STALL),
        ..local()
    };
    let (result, elapsed) = fetch_within(
        Duration::from_secs(30),
        &url(address, "/unreachable.md"),
        storage.path(),
        options,
    );
    let error = result.expect_err("a connect that never finishes must fail");
    // The first attempt ends at the 10 s connect limit and is retried; the
    // retry has less than 10 s left, so the fetch deadline ends it while it
    // is still connecting, and it must still be named the connect phase.
    assert_eq!(error.code(), "url_fetch_timeout", "{error}");
    assert_eq!(error.phase(), Some(FetchPhase::Connect), "{error}");
    assert!(error.to_string().contains("fetch deadline"), "{error}");
    assert!(
        elapsed >= URL_FETCH_CEILING - Duration::from_millis(500)
            && elapsed < URL_FETCH_CEILING + Duration::from_secs(2),
        "{elapsed:?}"
    );
}

#[test]
fn a_cached_html_page_is_served_from_the_cache_without_a_new_fetch() {
    let storage = tempfile::tempdir().unwrap();
    let requests = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&requests);
    let address = serve(move |_, mut stream| {
        counter.fetch_add(1, Ordering::SeqCst);
        let body = b"<html><body><h2>Heading 1</h2><p>text</p></body></html>";
        let _ = write!(
            stream,
            "HTTP/1.1 200 OK\r\ncontent-type: text/html\r\ncontent-length: {}\r\nconnection: close\r\n\r\n",
            body.len()
        );
        let _ = stream.write_all(body);
    });
    let target = url(address, "/guide/page.html");
    let (first, _) = fetch_within(Duration::from_secs(5), &target, storage.path(), local());
    let first = first.expect("first fetch");
    let modified = fs::metadata(&first).unwrap().modified().unwrap();
    let (second, _) = fetch_within(Duration::from_secs(5), &target, storage.path(), local());
    let second = second.expect("cached copy");
    assert_eq!(first, second);
    assert_eq!(
        requests.load(Ordering::SeqCst),
        1,
        "the page was fetched again"
    );
    // An unchanged file keeps the parsed symbols of the last call valid.
    assert_eq!(fs::metadata(&second).unwrap().modified().unwrap(), modified);
}

#[test]
fn a_stalled_cache_check_returns_the_remembered_failure_within_the_budget_and_is_not_repeated() {
    let storage = tempfile::tempdir().unwrap();
    // No server: with the failure remembered, nothing may be fetched.
    let target = "http://127.0.0.1:9/never-fetched.md";
    remember_failure(
        FailureKey::new(storage.path(), target, true),
        &UrlFetchError::http_status(503, "HTTP 503 Service Unavailable"),
    );
    let stall = stall_cache_reads(storage.path());
    let budget = Duration::from_secs(2);
    for call in 1..=2 {
        let (result, elapsed) = fetch_within(budget, target, storage.path(), local());
        let error = result.expect_err("the remembered failure answers");
        assert!(error.retry_after().is_some(), "call {call}: {error}");
        assert!(
            error.to_string().contains("HTTP 503"),
            "call {call}: {error}"
        );
        // The check waits until one reply margin before the end of the call.
        assert!(
            elapsed + URL_REPLY_MARGIN / 2 < budget,
            "call {call} answered {elapsed:?} into a {budget:?} call"
        );
    }
    assert_eq!(
        stall.reached.load(Ordering::SeqCst),
        1,
        "the second call joined the stalled check instead of starting another"
    );
    stall.release();
}

/// A read failure the way reqwest's blocking body reader reports its own
/// timeouts: an `io::Error` of kind `Other`, not `TimedOut`.
const REQWEST_STYLE_TIMEOUT: io::ErrorKind = io::ErrorKind::Other;

#[test]
fn a_generic_read_error_after_the_fetch_deadline_is_a_body_timeout() {
    // The deadline has already passed when the failed read is reported, as
    // when reqwest's whole-request timer fires before this side's own wait.
    let deadline = FetchDeadline {
        at: Instant::now(),
        allowed: Duration::from_millis(2_000),
        set_by_call: true,
    };
    let error = body_read_failure(
        &deadline,
        "http://example.test/doc.md",
        REQWEST_STYLE_TIMEOUT,
        "error decoding response body: operation timed out",
        Duration::from_millis(1_990),
    );
    assert_eq!(error.code(), "url_fetch_timeout", "{error}");
    assert_eq!(error.phase(), Some(FetchPhase::Body), "{error}");
}

#[test]
fn a_generic_read_error_after_the_chunk_stall_limit_is_a_body_timeout() {
    let deadline = FetchDeadline {
        at: Instant::now() + Duration::from_secs(5),
        allowed: URL_FETCH_CEILING,
        set_by_call: false,
    };
    let error = body_read_failure(
        &deadline,
        "http://example.test/doc.md",
        REQWEST_STYLE_TIMEOUT,
        "error decoding response body: operation timed out",
        BODY_CHUNK_TIMEOUT,
    );
    assert_eq!(error.code(), "url_fetch_timeout", "{error}");
    assert_eq!(error.phase(), Some(FetchPhase::Body), "{error}");
    assert!(error.to_string().contains("Body read stalled"), "{error}");
}

#[test]
fn a_generic_read_error_before_any_limit_is_a_read_failure() {
    let deadline = FetchDeadline {
        at: Instant::now() + Duration::from_secs(5),
        allowed: URL_FETCH_CEILING,
        set_by_call: false,
    };
    let error = body_read_failure(
        &deadline,
        "http://example.test/doc.md",
        REQWEST_STYLE_TIMEOUT,
        "connection reset by peer",
        Duration::from_millis(10),
    );
    assert_eq!(error.code(), "url_fetch_failed", "{error}");
    assert_eq!(error.phase(), None, "{error}");
}