spider-cloud-agent 0.8.0

An agent other agents call: the Spider Cloud API with local routing, status-aware escalation, and minimal machine-readable responses.
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
//! One builder per endpoint.
//!
//! Every builder is the same shape: name what you want, adjust as little as you
//! need to, then `send`. `send` hands back the one page that worked. `send_all`
//! hands back everything, failures included, which is what a crawl and a batch
//! are actually for.
//!
//! Every operation ends. The budget's wall, fifteen minutes unless the caller
//! says otherwise, is one deadline over every send and every sleep in the
//! walk, and an empty 204 is an empty `Pages` from `send_all` rather than a
//! decode failure. A router, recorder or policy the caller supplied runs on
//! the caller's thread between calls and is not under the deadline.
//!
//! The adjustable surface is short on purpose. Mode, proxy pool, country, wait
//! condition, profile, timeout, session and budget are the settings worth
//! choosing by hand; the service decides better than a client can about the
//! rest, and every setting promoted here is a promise to keep across releases.
//! Anything finer lives behind `params_mut`, which hands over the whole
//! documented parameter set and, with it, ownership of the result.

pub mod crawl;
pub mod data;
pub mod fetch;
pub mod links;
pub mod scrape;
pub mod screenshot;
pub mod search;
pub mod transform;

use std::future::Future;
use std::time::{Duration, Instant};

use serde::Serialize;
use url::Url;

use crate::auth::router::StoredRouter;
use crate::client::{IntoUrl, Spider};
use crate::credits::Credits;
use crate::error::{BudgetKind, Error};
use crate::params::{RequestParams, ReturnFormat, ReturnFormatHandling};
use crate::policy::engine::Reached;
use crate::policy::{AttemptState, Budget, Next, Observed, Policy, Step, StopReason};
use crate::response::{Attempt, Body, FailedPage, Hint, Outcome, Page, PageResult, Pages};
use crate::routing;
use crate::status::{ApiStatus, PageStatus};
use crate::thrift::plan::Endpoint;
use crate::thrift::tokens::{approx_tokens, TokenBudget};
use crate::thrift::trim::Trimmer;
use crate::thrift::{Need, Plan, ThriftReport};
use crate::transport::{route, Route};
use crate::Result;
use spider_route::featurize;
use tokio::time::{sleep, timeout_at, Instant as DeadlineInstant};

/// The service's own address, used as the subject of a result that did not come
/// from fetching a page.
const SERVICE_URL: &str = "https://spider.cloud";

/// One absolute clock shared by sends, sleeps and synchronous checkpoints.
#[derive(Clone, Copy)]
pub(crate) struct Deadline(Option<DeadlineInstant>);

impl Deadline {
    pub(crate) fn new(wall: Option<Duration>) -> Result<Self> {
        let end = wall
            .map(|wall| {
                DeadlineInstant::now()
                    .checked_add(wall)
                    .ok_or_else(|| Error::Config("wall is too large for the clock".into()))
            })
            .transpose()?;
        Ok(Self(end))
    }

    pub(crate) fn expired(self) -> bool {
        self.0.is_some_and(|end| DeadlineInstant::now() >= end)
    }

    /// Observer work is outside the pure policy's elapsed accounting. Recheck
    /// its chosen wait against the actual clock before sleeping, without
    /// shortening a server-directed wait to fit the remaining wall.
    fn allows_wait(self, after: Duration) -> bool {
        self.0
            .is_none_or(|end| after < end.saturating_duration_since(DeadlineInstant::now()))
    }
}

/// Run one call under a wall, or under none.
///
/// `None` comes back when the wall ran out before the call finished. Dropping
/// the call is what ends it: nothing in the transport holds a resource past
/// its future, so a request cut off here leaves nothing behind.
pub(crate) async fn within<T, F>(deadline: Deadline, call: F) -> Option<Result<T>>
where
    F: Future<Output = Result<T>>,
{
    if deadline.expired() {
        return None;
    }
    match deadline.0 {
        Some(end) => timeout_at(end, call).await.ok(),
        None => Some(call.await),
    }
}

/// The state every builder carries, and the one place a call is made.
pub(crate) struct Call<'a> {
    pub(crate) spider: &'a Spider,
    pub(crate) params: crate::params::RequestParams,
    pub(crate) budget: Budget,
    pub(crate) need: Option<Need>,
    pub(crate) max_tokens: Option<usize>,
    url: Option<Url>,
    url_error: Option<String>,
}

impl std::fmt::Debug for Call<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Call")
            .field("spider", &self.spider)
            .field("params", &self.params)
            .field("budget", &self.budget)
            .field("need", &self.need.as_ref().map(|_| "<redacted>"))
            .field("max_tokens", &self.max_tokens)
            .field("url", &self.url.as_ref().map(|_| "<redacted>"))
            .field("url_error", &self.url_error.as_ref().map(|_| "<redacted>"))
            .finish_non_exhaustive()
    }
}

impl<'a> Call<'a> {
    /// Recheck admission immediately before every send. The run reservation is the
    /// concurrency gate; a snapshot alone would let two tasks spend the same credit.
    fn admit(
        &mut self,
        attempts: &[Attempt],
        estimate: Credits,
    ) -> Result<Option<crate::client::Reservation>> {
        let spent: Credits = attempts
            .iter()
            .map(|a| Credits(a.cost.get().max(a.reserved.get())))
            .sum();
        self.budget
            .preflight(attempts.len().min(255) as u8, spent, estimate)
            .map_err(|kind| {
                let error = Error::BudgetExceeded {
                    kind,
                    attempts: attempts.to_vec(),
                };
                match self.spider.run_budget() {
                    Some(run) => error.accounted(attempts.to_vec(), Some(run.snapshot())),
                    None => error,
                }
            })?;
        match self.spider.run_budget() {
            None => Ok(None),
            Some(run) => run
                .reserve(estimate)
                .map(|reservation| {
                    let cap = spent + reservation.allowance;
                    self.budget.credits = Some(
                        self.budget
                            .credits
                            .map_or(cap, |old| Credits(old.get().min(cap.get()))),
                    );
                    Some(reservation)
                })
                .ok_or_else(|| {
                    Error::BudgetExceeded {
                        kind: BudgetKind::Credits,
                        attempts: attempts.to_vec(),
                    }
                    .accounted(attempts.to_vec(), Some(run.snapshot()))
                }),
        }
    }

    fn cap_for_run(&mut self) {
        if let Some(run) = self.spider.run_budget() {
            let left = run.remaining();
            self.budget.credits = Some(
                self.budget
                    .credits
                    .map_or(left, |cap| Credits(cap.get().min(left.get()))),
            );
        }
    }

    fn call_failure(&self, error: Error, attempts: Vec<Attempt>) -> Error {
        error.accounted(
            attempts,
            self.spider.run_budget().map(crate::RunBudget::snapshot),
        )
    }

    /// A call against one address. A bad address is held until `send`.
    pub(crate) fn new(spider: &'a Spider, url: impl IntoUrl) -> Call<'a> {
        let mut call = Call::bare(spider);
        match url.into_url() {
            Ok(url) => {
                call.params.url = Some(url.to_string());
                call.url = Some(url);
            }
            Err(e) => call.url_error = Some(format!("that is not an address: {e}")),
        }
        call
    }

    /// A call with no address, for the endpoints that do not read a page.
    pub(crate) fn bare(spider: &'a Spider) -> Call<'a> {
        Call {
            spider,
            params: crate::params::RequestParams::default(),
            budget: spider.budget(),
            need: None,
            max_tokens: None,
            url: None,
            url_error: None,
        }
    }

    /// The address, once it is needed.
    fn target(&self) -> Result<Url> {
        if let Some(message) = &self.url_error {
            return Err(Error::Config(message.clone()));
        }
        match &self.url {
            Some(url) => Ok(url.clone()),
            // The endpoints that read no page still produce results that need an
            // address on them. The service's own is the honest one to use: the
            // result came from there and from nowhere else.
            None => Url::parse(SERVICE_URL).map_err(|e| {
                Error::Config(format!(
                    "the built in address {SERVICE_URL} does not parse: {e}"
                ))
            }),
        }
    }

    /// The format the request asked for, when it asked for exactly one.
    ///
    /// The wire returns a field called `raw` whatever was requested, so this is
    /// what tells markdown from markup on the way back.
    fn format(&self) -> Option<ReturnFormat> {
        match self.params.return_format.as_ref()? {
            ReturnFormatHandling::Single(format) => Some(*format),
            ReturnFormatHandling::Multi(formats) if formats.len() == 1 => formats.first().copied(),
            ReturnFormatHandling::Multi(_) => None,
        }
    }

    /// Make the calls, escalating under the policy until it says to stop.
    ///
    /// The loop is short because the deciding is not here: [`Policy::decide`]
    /// answers with the next move, this waits for it, patches the request with
    /// the step it named, and calls again. Nothing in this function knows why a
    /// 403 escalates, which is what keeps the rule table testable without a
    /// network.
    ///
    /// `make_body` builds the request from the current parameters, because a
    /// step patches parameters and the endpoints wrap them differently.
    pub(crate) async fn run<B, F>(&mut self, route: Route, make_body: F) -> Result<Outcome<Pages>>
    where
        B: Serialize,
        F: Fn(&crate::params::RequestParams) -> B,
    {
        self.run_at(route, &[], make_body).await
    }

    /// The same, for a route that carries its target in the path.
    ///
    /// `/fetch` names the domain and the path in the address instead of in a
    /// `url` field, so the send loop has to be able to fill a route's
    /// placeholders. Everything else about the walk is the same, which is the
    /// reason this is one function and not a second loop.
    pub(crate) async fn run_at<B, F>(
        &mut self,
        route: Route,
        args: &[&str],
        make_body: F,
    ) -> Result<Outcome<Pages>>
    where
        B: Serialize,
        F: Fn(&crate::params::RequestParams) -> B,
    {
        self.run_at_inner(route, args, make_body)
            .await
            .map_err(|error| match self.spider.run_budget() {
                Some(run) => error.with_run(run.snapshot()),
                None => error,
            })
    }

    async fn run_at_inner<B, F>(
        &mut self,
        route: Route,
        args: &[&str],
        make_body: F,
    ) -> Result<Outcome<Pages>>
    where
        B: Serialize,
        F: Fn(&crate::params::RequestParams) -> B,
    {
        // The clock starts before routing and is never reset by an attempt.
        self.cap_for_run();
        let deadline = Deadline::new(self.budget.wall)?;
        let target = self.target()?;
        let policy = match self.spider.policy() {
            Some(policy) => policy.clone(),
            None => {
                let mut policy = Policy::for_target(target.as_str());
                policy.backoff = crate::policy::Backoff::seeded(self.spider.jitter_seed);
                policy
            }
        };
        // The plan is settled before anything is sent, because the format it
        // chooses is what tells markdown from markup on the way back. The
        // snapshot is the request as the caller left it, and it is what keeps
        // an explicit setting of theirs from being argued with.
        let plan = self.plan();
        let caller = self.params.clone();

        // The router answers before anything is sent, which is the whole point
        // of it: the cheapest settings likely to work cost nothing to choose
        // and a wrong first attempt costs a call. What it answers fills in what
        // the caller left alone and never overrides what they set.
        let remembered = self.spider.site_memory().get(&target);
        let input = routing::input(
            &target,
            routing::declared_need(self.need.as_ref()),
            routing::pins_from(&caller),
            remembered.as_ref(),
        );
        let picked = self.spider.router().route(&input);
        // A caller who fixed a setting is not explored. The pin would win over
        // the drawn arm anyway, and the outcome and the recorded row would then
        // name an action that was never sent.
        let decision = if input.pins.any() {
            picked
        } else {
            self.spider
                .explorer()
                .choose(&target, &picked, &self.budget)
                .unwrap_or(picked)
        };

        routing::apply_decision(&decision, &mut self.params, &caller);
        plan.apply_over(&mut self.params, &caller);
        apply_stored_router(self.spider.provider_router(), &mut self.params, &caller);
        // The optimizer reads the request as it is about to go out, and like the
        // explorer it is never asked about a request the caller pinned. Only the
        // first attempt is edited: every rung after it writes over the mode, the
        // pool and the wait, and leaves a blacklist edit where it was.
        #[cfg(feature = "optimize")]
        let optimized = crate::optimize::decide(self, &input, &caller, &decision);
        let format = self.format();

        let mut state = AttemptState::new(self.budget);
        // A decision that says the cheap steps are hopeless here starts the
        // walk past them rather than paying for attempts it expects to fail.
        state.step = usize::from(decision.start_rung);

        let features = self.spider.recorder().map(|_| featurize(&input));
        let mut routed_attempt = true;
        let mut attempts: Vec<Attempt> = Vec::new();
        let mut pages = Pages::default();
        // Whether any attempt got a page back, and the last failed page it got.
        // Both outlive `pages`, which describes the last attempt only, because
        // a call that failed after a page came back is still a walk that
        // reached the site, and the error has to say so.
        let mut pages_came_back = false;
        let mut last_failed: Option<FailedPage> = None;
        let mut call_error: Option<Error>;
        let current = endpoint_for(&plan, route);
        let mut wire_bytes = 0usize;
        let mut estimate = Budget::floor(Credits::ZERO);
        let mut run_overrun = None;
        let mut page_overrun = None;
        loop {
            let mut attempt_bytes = 0u32;

            // A call is held to whatever is left of the wall. Without this a
            // service that accepted the request and never answered held the
            // caller for as long as the socket stayed open, and the budget only
            // ever looked at the clock between calls.
            if deadline.expired() {
                return Err(out_of_time(attempts));
            }
            if attempts.len() >= usize::from(policy.max_attempts) {
                return Err(Error::BudgetExceeded {
                    kind: BudgetKind::Attempts,
                    attempts,
                });
            }
            let reservation = self.admit(&attempts, estimate)?;
            state.budget = self.budget;
            let mut wire_budget = self.budget;
            wire_budget.credits = self.budget.remaining_credits(state.spent);
            wire_budget.apply(&mut self.params);
            let body = make_body(&self.params);
            let before = Instant::now();
            let sent = within(
                deadline,
                self.spider
                    .raw()
                    .post_with_limit(current, args, &body, self.spider.response_limit),
            )
            .await;
            let mut wall_ran_out = false;
            let mut charge_unknown = false;

            let observed = match sent {
                None => {
                    charge_unknown = true;
                    call_error = None;
                    pages = Pages::default();
                    wall_ran_out = true;
                    Observed::timed_out().taking(before.elapsed())
                }
                Some(Ok(reply)) => {
                    wire_bytes = reply.body.len();
                    attempt_bytes = wire_bytes.min(u32::MAX as usize) as u32;
                    let elapsed = reply.elapsed;
                    let status = reply.status;
                    let wait = reply.retry_after.or(reply.rate_limit.reset);

                    // A body status of 400 or above is copied onto the call's
                    // status, so a page the site refused arrives looking like a
                    // failed call. Sorting it back onto the target plane is what
                    // makes a refusal something to escalate rather than something
                    // to give up on.
                    if reply.is_success() || reply.is_mirrored_page_status() {
                        charge_unknown = !reply.body.is_empty()
                            && serde_json::from_slice::<serde_json::Value>(&reply.body)
                                .ok()
                                .as_ref()
                                .is_none_or(|body| !has_charge(body));
                        call_error = None;
                        pages = match reply.read_pages(&target, format, current == route::TRANSFORM)
                        {
                            Ok(pages) => pages,
                            Err(error) => {
                                let body =
                                    serde_json::from_slice::<serde_json::Value>(&reply.body).ok();
                                let cost = body.as_ref().map(charged).unwrap_or(Credits::ZERO);
                                let mut attempt = Attempt::new(elapsed, status, None, cost);
                                attempt.charge_unknown =
                                    body.as_ref().is_none_or(|body| !has_charge(body));
                                if let Some(reservation) = reservation {
                                    let _ = reservation.settle(cost, attempt.charge_unknown);
                                }
                                attempts.push(attempt);
                                return Err(self.call_failure(error, attempts));
                            }
                        };
                        pages.retain_requested(&self.params);
                        if !pages.is_empty() {
                            pages_came_back = true;
                        }
                        if let Some(cap) = self.budget.per_page_credits {
                            if let Some(page) = pages.0.iter().find(|page| page.cost() > cap) {
                                page_overrun = Some(crate::response::BudgetOverrun {
                                    scope: crate::response::BudgetScope::Page,
                                    cap,
                                    spent: page.cost(),
                                });
                            }
                        }
                        if let Some(failed) = pages.failed().next() {
                            last_failed = Some(failed.clone());
                        }
                        // A mirrored envelope supplies no independent API code.
                        // Zero means unknown, never a success minted from page data.
                        let api = reply.operation_status();
                        let page = representative(&pages);
                        let mut observed = Observed::seen(0, None);
                        observed.api = Reached::Api(api);
                        observed.page = if current == route::TRANSFORM {
                            page.filter(|s| !s.is_unknown())
                        } else {
                            page
                        };
                        let mut observed = observed.costing(pages.total_cost()).taking(elapsed);
                        // An empty body is only a failure when the caller wanted
                        // a body. Need::Metadata and Need::Fields deliberately ask
                        // for return_format=empty, so the page arrives with nothing
                        // in it and that is the request working exactly as asked.
                        // Treating it as a blank page sent the walk up the whole
                        // ladder against a request that had already succeeded, and
                        // charged for every step.
                        if nothing_came_back(&pages) && !body_was_declined(&self.params) {
                            observed = observed.blank();
                        }
                        match wait {
                            Some(after) => observed.retry_after(after),
                            None => observed,
                        }
                    } else {
                        call_error = reply.as_error();
                        pages = Pages::default();
                        let cost = serde_json::from_slice::<serde_json::Value>(&reply.body)
                            .ok()
                            .as_ref()
                            .map(charged)
                            .unwrap_or(Credits::ZERO);
                        let observed = Observed::seen(status.code(), None)
                            .taking(elapsed)
                            .costing(cost);
                        match wait {
                            Some(after) => observed.retry_after(after),
                            None => observed,
                        }
                    }
                }
                Some(Err(Error::Transport(e))) if e.is_timeout() => {
                    charge_unknown = true;
                    call_error = Some(Error::Transport(e));
                    Observed::timed_out().taking(before.elapsed())
                }
                Some(Err(Error::Transport(e))) if e.is_connect() => {
                    call_error = Some(Error::Transport(e));
                    Observed::connect_failed().taking(before.elapsed())
                }
                // The answer ran past the size the caller asked the client to
                // read. The call is recorded before the walk stops, so what
                // was spent on the attempts before it stays on the error, and
                // the walk stops rather than climbs, because a heavier request
                // buys a bigger answer.
                Some(Err(Error::ResponseTooLarge { limit, status })) => {
                    let mut attempt = Attempt::new(before.elapsed(), status, None, Credits::ZERO);
                    attempt.charge_unknown = true;
                    attempts.push(attempt);
                    return Err(Error::Exhausted {
                        attempts,
                        last: last_failed.map(Box::new),
                        reason: StopReason::Unhandled,
                        source: Some(Box::new(Error::ResponseTooLarge { limit, status })),
                    });
                }
                // Anything else went wrong before a call could be judged, so
                // there is nothing for the policy to read.
                Some(Err(other)) => {
                    let mut attempt =
                        Attempt::new(before.elapsed(), ApiStatus::new(0), None, Credits::ZERO);
                    attempt.charge_unknown =
                        matches!(&other, Error::Transport(e) if !e.is_connect() && !e.is_builder());
                    if let Some(reservation) = reservation {
                        let _ = reservation.settle(attempt.cost, attempt.charge_unknown);
                    }
                    attempts.push(attempt);
                    return Err(self.call_failure(other, attempts));
                }
            }
            // Read off the request whether it carried anything a session could
            // keep, which is what decides whether a login wall gets a second
            // call.
            .for_request(&self.params);

            state.record(&observed);
            if let Some(reservation) = reservation {
                run_overrun = reservation
                    .settle(observed.cost, charge_unknown)
                    .or(run_overrun);
            }
            attempts.push(Attempt::new(
                observed.elapsed,
                api_status_of(&observed),
                observed.page,
                observed.cost,
            ));
            if let Some(attempt) = attempts.last_mut() {
                attempt.charge_unknown = charge_unknown;
                if charge_unknown {
                    attempt.reserved = Credits(estimate.get().max(attempt.cost.get()));
                    state.spent += Credits((estimate.get() - attempt.cost.get()).max(0.0));
                }
            }

            // The wall ended the call, so the budget has already decided. The
            // policy is not asked, because whatever it answered would be a call
            // there is no time left for.
            let next = if wall_ran_out || deadline.expired() {
                Next::Stop(StopReason::Budget(BudgetKind::Time))
            } else {
                policy.decide(&observed, &state)
            };

            // What the attempt says about the site, folded in before the next
            // move is acted on. The policy has already judged whether this
            // attempt got what the caller asked for, and asking a second time
            // here would be a second answer to drift apart from the first.
            let result = routing::outcome_of(&observed, next == Next::Accept, attempt_bytes);
            if routing::describes_the_site(&observed) {
                self.spider.site_memory().observe(&target, &result);
            }
            self.spider.router().observe(&input, &result);
            // One comparison row per operation, written when the walk settles.
            #[cfg(feature = "optimize")]
            crate::optimize::compare(self, optimized.as_ref(), &next, &result, &attempts, &pages);

            // Only the first attempt was routed. Every one after it was chosen
            // by the ladder, so a row for it would describe a decision the
            // router did not make.
            if routed_attempt {
                if let (Some(recorder), Some(features)) = (self.spider.recorder(), &features) {
                    recorder.observe(features, &decision, &result);
                }
                routed_attempt = false;
            }

            state.follow(&next);

            if deadline.expired() {
                return Err(out_of_time(attempts));
            }

            match next {
                Next::Accept => {
                    let report = self.settle(&mut pages, wire_bytes);
                    if deadline.expired() {
                        return Err(out_of_time(attempts));
                    }
                    let mut outcome = Outcome::new(pages, attempts)
                        .with_cap(self.budget.credits)
                        .reporting(report)
                        .routed(decision);
                    outcome.overrun = run_overrun.or(outcome.overrun).or(page_overrun);
                    return Ok(outcome);
                }
                Next::Retry { after } => {
                    if !deadline.allows_wait(after) {
                        return Err(out_of_time(attempts));
                    }
                    estimate = Budget::floor(if observed.was_billed() {
                        state.last_cost
                    } else {
                        Credits::ZERO
                    });
                    if within(deadline, async {
                        sleep(after).await;
                        Ok(())
                    })
                    .await
                    .is_none()
                    {
                        return Err(out_of_time(attempts));
                    }
                }
                Next::Escalate { step, after, .. } => {
                    if !deadline.allows_wait(after) {
                        return Err(out_of_time(attempts));
                    }
                    estimate = step.estimate(if observed.was_billed() {
                        state.last_cost
                    } else {
                        Credits::ZERO
                    });
                    if within(deadline, async {
                        sleep(after).await;
                        Ok(())
                    })
                    .await
                    .is_none()
                    {
                        return Err(out_of_time(attempts));
                    }
                    escalate(&step, &mut self.params, &plan, &caller);
                }
                Next::Stop(reason) => {
                    return Err(stopped(
                        reason,
                        attempts,
                        pages_came_back,
                        last_failed,
                        call_error,
                    ));
                }
            }
        }
    }

    /// The parameters the stated need asks for.
    pub(crate) fn plan(&self) -> Plan {
        match &self.need {
            Some(need) => Plan::for_need(need),
            // No need was stated, so nothing is switched off on the caller's
            // behalf and the service answers as it would.
            None => Plan::none(),
        }
    }

    /// Trim what came back, and say what that saved.
    ///
    /// Boilerplate is learned across the whole set first, because a block that
    /// repeats is only visible from more than one page. The token ceiling is
    /// shared out afterwards in proportion to what each page costs.
    fn settle(&self, pages: &mut Pages, wire_bytes: usize) -> ThriftReport {
        let bodies: Vec<String> = pages
            .0
            .iter()
            .map(|result| match result {
                PageResult::Ok(page) => page.text().unwrap_or_default().to_string(),
                PageResult::Failed(_) => String::new(),
            })
            .collect();
        let tokens_in: usize = bodies.iter().map(|body| approx_tokens(body)).sum();

        if self.need.is_none() && self.max_tokens.is_none() {
            return ThriftReport::untrimmed(wire_bytes, tokens_in);
        }

        let mut trimmer = match &self.need {
            Some(need) => Trimmer::for_need(need),
            None => Trimmer::default(),
        };
        trimmer.learn(&bodies);

        let budget = match self.max_tokens {
            Some(total) => TokenBudget::total(total),
            None => TokenBudget::unlimited(),
        };
        let first_pass: Vec<String> = bodies
            .iter()
            .map(|body| trimmer.trim(body, None).text)
            .collect();
        let costs: Vec<usize> = first_pass.iter().map(|text| approx_tokens(text)).collect();
        let shares = budget.split(&costs);

        let mut reasons = Vec::new();
        let mut returned_bytes = 0usize;
        let mut tokens_out = 0usize;
        for ((result, before), ceiling) in pages.0.iter_mut().zip(&first_pass).zip(shares) {
            let PageResult::Ok(page) = result else {
                continue;
            };
            let trimmed = trimmer.trim(before, budget.total.map(|_| ceiling));
            returned_bytes += trimmed.text.len();
            tokens_out += approx_tokens(&trimmed.text);
            reasons.extend(trimmed.reasons);
            replace_text(&mut page.body, trimmed.text);
            // Extractions and metadata are payload too. Counting only the text
            // body reported a hundred per cent saving on a request that did hand
            // the caller its answer, which is a true number and a dishonest one.
            let aside = payload_beside_the_text(page);
            returned_bytes += aside.0;
            tokens_out += aside.1;
        }

        ThriftReport {
            wire_bytes,
            returned_bytes,
            approx_tokens_in: tokens_in,
            approx_tokens_out: tokens_out,
            trimmed: reasons,
        }
    }

    /// Make one call and read the body as a value rather than as pages.
    ///
    /// Used by the endpoints whose answer is not a page: a result list, a stored
    /// table, a balance. Nothing here escalates, because none of these failures
    /// is about a site refusing a fetch.
    pub(crate) async fn run_json<B, T, F>(
        &mut self,
        route: Route,
        make_body: F,
    ) -> Result<Outcome<T>>
    where
        B: Serialize,
        T: serde::de::DeserializeOwned,
        F: Fn(&crate::params::RequestParams) -> B,
    {
        self.run_json_inner(route, make_body)
            .await
            .map_err(|error| match self.spider.run_budget() {
                Some(run) => error.with_run(run.snapshot()),
                None => error,
            })
    }

    async fn run_json_inner<B, T, F>(&mut self, route: Route, make_body: F) -> Result<Outcome<T>>
    where
        B: Serialize,
        T: serde::de::DeserializeOwned,
        F: Fn(&crate::params::RequestParams) -> B,
    {
        self.cap_for_run();
        let deadline = Deadline::new(self.budget.wall)?;
        // One call, held to the wall the same way the send loop holds its
        // calls. A search against a service that went quiet hung here too.
        if deadline.expired() {
            return Err(out_of_time(Vec::new()));
        }
        let reservation = self.admit(&[], Budget::floor(Credits::ZERO))?;
        self.budget.apply(&mut self.params);
        let body = make_body(&self.params);
        let before = Instant::now();
        let reply = match within(
            deadline,
            self.spider
                .raw()
                .post_with_limit(route, &[], &body, self.spider.response_limit),
        )
        .await
        .unwrap_or_else(|| {
            let mut attempt =
                Attempt::new(before.elapsed(), ApiStatus::new(0), None, Credits::ZERO);
            attempt.charge_unknown = true;
            Err(out_of_time(vec![attempt]))
        }) {
            Ok(reply) => reply,
            // Recorded the way the send loop records it, so the one call this
            // made is on the error rather than lost with it.
            Err(Error::ResponseTooLarge { limit, status }) => {
                return Err(Error::Exhausted {
                    attempts: vec![Attempt {
                        charge_unknown: true,
                        ..Attempt::new(before.elapsed(), status, None, Credits::ZERO)
                    }],
                    last: None,
                    reason: StopReason::Unhandled,
                    source: Some(Box::new(Error::ResponseTooLarge { limit, status })),
                });
            }
            Err(other) => {
                if matches!(other, Error::BudgetExceeded { .. }) {
                    return Err(other);
                }
                let mut attempt =
                    Attempt::new(before.elapsed(), ApiStatus::new(0), None, Credits::ZERO);
                attempt.charge_unknown =
                    matches!(&other, Error::Transport(e) if !e.is_connect() && !e.is_builder());
                if let Some(reservation) = reservation {
                    let _ = reservation.settle(attempt.cost, attempt.charge_unknown);
                }
                return Err(self.call_failure(other, vec![attempt]));
            }
        };
        let body = reply.json::<serde_json::Value>();
        let cost = body.as_ref().map(charged).unwrap_or(Credits::ZERO);
        let mut attempt = Attempt::new(reply.elapsed, reply.status, None, cost);
        attempt.charge_unknown =
            reply.is_success() && body.as_ref().map_or(true, |body| !has_charge(body));
        let run_overrun =
            reservation.and_then(|reservation| reservation.settle(cost, attempt.charge_unknown));
        if let Some(error) = reply.as_error() {
            return Err(self.call_failure(error, vec![attempt]));
        }
        let body = body.map_err(|error| self.call_failure(error, vec![attempt.clone()]))?;
        let value: T = serde_json::from_value(body)
            .map_err(|error| self.call_failure(Error::Decode(error), vec![attempt.clone()]))?;
        if deadline.expired() {
            return Err(out_of_time(vec![attempt]));
        }
        let mut outcome = Outcome::new(value, vec![attempt]).with_cap(self.budget.credits);
        outcome.overrun = run_overrun.or(outcome.overrun);
        Ok(outcome)
    }
}

/// Whether every item supplied a readable total, including an explicit zero.
fn has_charge(body: &serde_json::Value) -> bool {
    match body {
        serde_json::Value::Array(items) => !items.is_empty() && items.iter().all(has_charge),
        other => reported_charge(other).is_some(),
    }
}

/// Recover the total independently of page fields and cost breakdown fields.
/// A malformed optional field must not erase a readable bill beside it.
fn reported_charge(body: &serde_json::Value) -> Option<Credits> {
    let total = body.get("costs")?.get("total_cost")?;
    serde_json::from_value::<crate::credits::Usd>(total.clone())
        .ok()
        .map(Credits::from)
}

/// What the service said an answer cost, even if its page fields cannot be decoded.
///
/// A page carries its own cost block and the send loop reads it. The answers
/// that are not pages were recorded as zero whatever the body said, so a search
/// spent nothing as far as the report and the budget were concerned, and an
/// operation a budget cannot see is the failure this crate exists to prevent.
///
/// The cost of a search is still short by the query itself: measured on
/// 2026-09-15, a one result search moved the balance by 10 credits and the
/// service sent no cost block at all with the result list. What arrives is
/// reported, and nothing is invented for what does not.
fn charged(body: &serde_json::Value) -> Credits {
    match body {
        serde_json::Value::Array(items) => items.iter().map(charged).sum(),
        other => reported_charge(other).unwrap_or(Credits::ZERO),
    }
}

/// Take one step up the ladder.
///
/// The step patches parameters, and the plan is written over what it left
/// behind. Nothing on the ladder asks for a fatter return format today, and
/// this pairing is what keeps that true when a rung is added later. It is one
/// function rather than two lines in the send loop so that it can be checked
/// without a socket.
fn escalate(step: &Step, params: &mut RequestParams, plan: &Plan, caller: &RequestParams) {
    step.apply(params);
    // A mode the caller named is theirs, here as much as it is when the router
    // answers. Every rung on the ladder renders the page, so without this a
    // caller who asked for plain HTTP to keep the bill down was billed for a
    // browser they had refused, and nothing in the result said so.
    if let Some(mode) = caller.request {
        params.request = Some(mode);
    }
    plan.apply_over(params, caller);
}

/// Fill in the stored provider fallback where the caller left it alone.
///
/// Read against the caller snapshot, like the routing decision, so a `router`
/// the caller set goes out as they set it, `mode: off` included. The two fields
/// are decided apart. The ladder touches neither, and under `mode: fallback`
/// the service tries the provider only after its own fetch fails.
pub(crate) fn apply_stored_router(
    store: Option<&StoredRouter>,
    params: &mut RequestParams,
    caller: &RequestParams,
) {
    let Some(store) = store else { return };
    if caller.router.is_none() {
        params.router = Some(store.router.clone());
    }
    if caller.provider_options.is_none() && store.provider_options.is_some() {
        params.provider_options.clone_from(&store.provider_options);
    }
}

/// Where a plan sends the call.
///
/// Only a page request is moved. A crawl that asked for links keeps crawling
/// and asks for the links of every page it visits, which is not the same
/// operation as reading the links off one page.
fn endpoint_for(plan: &Plan, requested: Route) -> Route {
    if requested != route::SCRAPE {
        return requested;
    }
    match plan.endpoint {
        Some(Endpoint::Links) => route::LINKS,
        Some(Endpoint::Screenshot) => route::SCREENSHOT,
        Some(Endpoint::Scrape) | None => requested,
    }
}

/// Put trimmed text back in the body it came out of, keeping the form it had.
///
/// A body that is not text is left alone. There is nothing useful to cut out of
/// an image, and turning one into a string is not this layer's decision.
fn replace_text(body: &mut Body, text: String) {
    match body {
        Body::WithFields { content, .. } => replace_text(content, text),
        Body::Text(held) | Body::Markdown(held) | Body::Html(held) | Body::Xml(held) => {
            *held = text
        }
        _ => {}
    }
}

/// The status to record for an attempt, including the ones that never got an
/// answer.
fn api_status_of(observed: &Observed) -> ApiStatus {
    match observed.api {
        Reached::Api(status) => status,
        // Nothing answered, so there is no status to record. Zero is the one
        // value no service can send, which is what makes it readable as "never
        // got there" rather than as a code.
        Reached::TimedOut | Reached::ConnectFailed => ApiStatus::new(0),
    }
}

/// The status that stands for a whole response.
///
/// A served page wins over a refused one, so a crawl with some failures in it
/// reads as a success and is not sent again at a dearer setting.
fn representative(pages: &Pages) -> Option<PageStatus> {
    pages
        .first_ok()
        .map(|page| page.status)
        .or_else(|| pages.0.first().map(|result| result.status()))
}

/// The bytes and tokens a caller receives that are not the page text.
///
/// `Need::Fields` and `Need::Metadata` deliberately return no body, so the
/// answer arrives in the extractions or the metadata block, and a request that
/// asked for links gets its answer in the link list. A report that measured
/// only the text called all of those a total saving, which flattered the
/// number by ignoring the thing the caller asked for.
fn payload_beside_the_text(page: &Page) -> (usize, usize) {
    let mut bytes = 0usize;

    if let Some(fields) = page.body.fields() {
        if let Ok(encoded) = serde_json::to_string(fields) {
            bytes += encoded.len();
        }
    }

    if let Some(meta) = &page.metadata {
        if let Ok(encoded) = serde_json::to_string(meta) {
            bytes += encoded.len();
        }
    }

    for value in [
        &page.json_data,
        &page.request_map,
        &page.response_map,
        &page.trace,
    ]
    .into_iter()
    .flatten()
    {
        if let Ok(encoded) = serde_json::to_string(value) {
            bytes += encoded.len();
        }
    }
    for value in [&page.headers, &page.cookies].into_iter().flatten() {
        if let Ok(encoded) = serde_json::to_string(value) {
            bytes += encoded.len();
        }
    }

    // Links are the payload on a links request and half the payload on a page
    // request that asked for both. Leaving them out reported a request that
    // handed back ninety addresses as having returned nothing.
    if let Some(links) = &page.links {
        bytes += links.iter().map(|link| link.as_str().len()).sum::<usize>();
    }

    // Structured payload is denser than prose, so the prose ratio would
    // understate it. Counting it by bytes over four keeps one rule for both.
    (bytes, bytes.div_ceil(4))
}

/// Whether the request asked the service not to send a body.
///
/// `return_format=empty` is how a caller says it wants the extractions or the
/// metadata and none of the page. A response with no body is then the correct
/// answer, not a silent failure.
fn body_was_declined(params: &RequestParams) -> bool {
    match params.return_format.as_ref() {
        Some(ReturnFormatHandling::Single(format)) => *format == ReturnFormat::Empty,
        Some(ReturnFormatHandling::Multi(formats)) => {
            !formats.is_empty() && formats.iter().all(|f| *f == ReturnFormat::Empty)
        }
        None => false,
    }
}

/// Whether the response is the failure that looks like a success.
///
/// No pages at all, or every served page blank. A refusal is not this: it has a
/// status that says what happened.
fn nothing_came_back(pages: &Pages) -> bool {
    if pages.is_empty() {
        return true;
    }
    let mut served = pages.ok().peekable();
    served.peek().is_some() && pages.ok().all(Page::is_blank)
}

/// The error for a call the wall ended before the service answered.
pub(crate) fn out_of_time(attempts: Vec<Attempt>) -> Error {
    Error::BudgetExceeded {
        kind: BudgetKind::Time,
        attempts,
    }
}

/// Turn the policy's reason for stopping into the error the caller sees.
///
/// When no page ever came back, [`Error::Accounted`] keeps the call error, an
/// [`Error::Api`], [`Error::Auth`] or [`Error::Transport`], because what
/// stopped the walk is a fact about the call rather than about a page. Only
/// when pages came back and the walk then stopped is it [`Error::Exhausted`],
/// carrying the policy's reason and, when a call was what stopped it, that
/// call's error as the source. A rate limit that runs out after a page came
/// back is therefore an exhausted walk and not a bare rate limit.
fn stopped(
    reason: StopReason,
    attempts: Vec<Attempt>,
    pages_came_back: bool,
    last_failed: Option<FailedPage>,
    call_error: Option<Error>,
) -> Error {
    match reason {
        StopReason::OutOfCredits => Error::InsufficientCredits.accounted(attempts, None),
        StopReason::Budget(kind) => Error::BudgetExceeded { kind, attempts },
        _ => match call_error {
            Some(error) if !pages_came_back => error.accounted(attempts, None),
            source => Error::Exhausted {
                attempts,
                last: last_failed.map(Box::new),
                reason,
                source: source.map(Box::new),
            },
        },
    }
}

/// Take the one page that worked, or say what stopped it.
///
/// The walk has already ended in [`Error::Exhausted`] with the policy's own
/// reason when the site refused every attempt, so the only way to get here
/// with no served page is a policy that accepted a failed page as the answer.
/// That is the site's answer settling, and the reason says so, with the hint
/// off the page it settled on. An empty 204 is the other way in: the service
/// accepted the call and had no page to send, so there is no single page and
/// no last failure, and the error carries neither.
pub(crate) fn first_page(outcome: Outcome<Pages>) -> Result<Outcome<Page>> {
    let Outcome {
        overrun,
        value,
        attempts,
        cost,
        thrift,
        route,
    } = outcome;
    let last = value.failed().next().cloned();
    match value.into_ok().into_iter().next() {
        Some(page) => Ok(Outcome {
            overrun,
            value: page,
            attempts,
            cost,
            thrift,
            route,
        }),
        None => Err(Error::Exhausted {
            attempts,
            reason: StopReason::Rejected {
                hint: last.as_ref().map_or(Hint::Permanent, |failed| failed.hint),
            },
            last: last.map(Box::new),
            source: None,
        }),
    }
}

/// How long a request timeout may be, in whole seconds, before the wire runs out
/// of room for it.
const MAX_TIMEOUT_SECS: u64 = u8::MAX as u64;

/// Turn a duration into the whole seconds the request timeout field holds.
pub(crate) fn timeout_secs(timeout: Duration) -> u8 {
    timeout.as_secs().clamp(1, MAX_TIMEOUT_SECS) as u8
}

/// The settings worth choosing by hand, on every builder that reads a page.
///
/// Written once here rather than eight times, so the curated surface cannot
/// drift apart between endpoints.
macro_rules! curated_surface {
    // The operations that fetch a page and can hand back its links in the same
    // answer. Written as an arm rather than as part of the common surface so
    // that an operation which fetches nothing cannot offer it.
    ($builder:ident, page_links) => {
        curated_surface!($builder);

        impl<'a> $builder<'a> {
            /// Ask for the links found on the page alongside its content.
            ///
            /// They arrive in the same answer, so a run that is already paying
            /// for pages gets the link graph without a second call.
            ///
            /// ```no_run
            /// # use spider_cloud_agent::{Need, Spider};
            /// # async fn show(spider: Spider) -> spider_cloud_agent::Result<()> {
            /// let page = spider.scrape("https://example.com")
            ///     .need(Need::Markdown)
            ///     .page_links(true)
            ///     .send()
            ///     .await?;
            /// let found = page.links.as_deref().unwrap_or_default();
            /// # let _ = found;
            /// # Ok(())
            /// # }
            /// ```
            pub fn page_links(mut self, on: bool) -> Self {
                self.call.params.return_page_links = Some(on);
                self
            }
        }
    };
    ($builder:ident) => {
        impl<'a> $builder<'a> {
            /// Whether the page is rendered before it is read.
            ///
            /// Leave it alone unless you already know: the default starts cheap
            /// and moves up on its own when the first answer looks empty.
            pub fn mode(mut self, mode: $crate::params::RequestMode) -> Self {
                self.call.params.request = Some(mode);
                self
            }

            /// Which pool the request leaves from.
            ///
            /// Residential addresses cost more per page, so they are worth
            /// asking for once the cheaper pool has been turned away.
            pub fn proxy(mut self, pool: $crate::params::ProxyPool) -> Self {
                self.call.params.proxy = Some(pool);
                self
            }

            /// The country to appear to be in.
            ///
            /// Changes prices, stock and language on plenty of sites, and
            /// sometimes whether the page is served at all.
            pub fn country(mut self, country: $crate::params::Country) -> Self {
                self.call.params.country_code = Some(country);
                self
            }

            /// What has to happen on the page before it is read.
            pub fn wait_for(mut self, wait: $crate::params::WaitFor) -> Self {
                self.call.params.wait_for = Some(wait);
                self
            }

            /// A coarse identity to present, which fills in a user agent and the
            /// window size that belongs with it.
            ///
            /// Anything you set yourself is left alone.
            pub fn profile(mut self, profile: $crate::params::Profile) -> Self {
                self.call.params.apply_profile(profile);
                self
            }

            /// How long one page has to come back.
            ///
            /// Rounded to whole seconds, which is what the wire carries, and
            /// held between one second and 255.
            pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
                self.call.params.request_timeout = Some($crate::ops::timeout_secs(timeout));
                self
            }

            /// Keep cookies and headers across the requests made to one site.
            ///
            /// Off by default, because a shared session makes otherwise
            /// independent requests affect each other.
            pub fn session(mut self, on: bool) -> Self {
                self.call.params.session = Some(on);
                self
            }

            /// What this operation may spend, in credits, time and calls.
            ///
            /// This client stops on the credit cap itself. The caps are also
            /// copied onto the request, where the service reads them as a byte
            /// ceiling rather than as a spend ceiling, so that side bounds the
            /// size of a page and not the size of a bill.
            pub fn budget(mut self, budget: $crate::policy::Budget) -> Self {
                self.call.budget = budget;
                self
            }

            /// What you actually want back.
            ///
            /// This is the setting that saves the most, and it works by asking
            /// for less rather than by cutting what arrives. Naming a need
            /// switches off metadata, headers, cookies, page links, structured
            /// data and embeddings unless the need is the one asking for them.
            /// Anything you set yourself is left alone.
            ///
            /// ```no_run
            /// # use spider_cloud_agent::{Need, Spider};
            /// # async fn show(spider: Spider) -> spider_cloud_agent::Result<()> {
            /// let page = spider.scrape("https://example.com")
            ///     .need(Need::Markdown)
            ///     .max_tokens(4000)
            ///     .send()
            ///     .await?;
            /// # let _ = page;
            /// # Ok(())
            /// # }
            /// ```
            pub fn need(mut self, need: $crate::thrift::Need) -> Self {
                self.call.need = Some(need);
                self
            }

            /// The most this operation may hand back, in tokens.
            ///
            /// Counted with an estimate rather than a tokenizer, and shared out
            /// across the pages in proportion to what each one costs. A page
            /// cut short ends on a sentence and says how much went.
            pub fn max_tokens(mut self, tokens: usize) -> Self {
                self.call.max_tokens = Some(tokens);
                self
            }

            /// The whole documented parameter set, to change directly.
            ///
            /// The escape hatch. Everything the API accepts is here, including
            /// the settings the curated surface leaves out on purpose, and a
            /// request built this way is yours to get right.
            pub fn params_mut(&mut self) -> &mut $crate::params::RequestParams {
                &mut self.call.params
            }
        }
    };
}

pub(crate) use curated_surface;

#[cfg(test)]
mod tests {
    // A test may unwrap and may panic: a test that cannot set itself up should
    // fail loudly rather than quietly measure nothing.
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::panic,
        clippy::unreachable,
        clippy::indexing_slicing,
        clippy::string_slice
    )]
    use super::*;
    use crate::params::ReturnFormat;
    use crate::policy::Ladder;
    use crate::thrift::Need;

    #[test]
    fn curated_surface_membership_is_explicit() {
        let source = include_str!("mod.rs");
        let surface = source
            .split_once("macro_rules! curated_surface {")
            .unwrap()
            .1
            .split_once("pub(crate) use curated_surface;")
            .unwrap()
            .0;
        let (page_links, common) = surface.split_once("($builder:ident) =>").unwrap();
        let methods = |arm: &str| -> Vec<String> {
            let code = arm
                .lines()
                .map(|line| line.split_once("//").map_or(line, |(code, _)| code))
                .collect::<Vec<_>>()
                .join("\n");
            let tokens: Vec<_> = code
                .split(|c: char| !c.is_alphanumeric() && c != '_')
                .filter(|token| !token.is_empty())
                .collect();
            tokens
                .windows(2)
                .filter(|pair| pair[0] == "fn")
                .map(|pair| pair[1].to_owned())
                .collect()
        };
        assert_eq!(methods(page_links), ["page_links"]);
        let expected = [
            "mode",
            "proxy",
            "country",
            "wait_for",
            "profile",
            "timeout",
            "session",
            "budget",
            "need",
            "max_tokens",
            "params_mut",
        ];
        assert_eq!(methods(common), expected);
        let vocabulary = include_str!("../../../docs/action-vocabulary.md");
        for name in expected.into_iter().chain(["page_links"]) {
            assert!(
                vocabulary
                    .lines()
                    .any(|line| line.starts_with(&format!("| `{name}` |"))),
                "{name} needs a line in docs/action-vocabulary.md"
            );
        }
    }

    #[test]
    fn f1_default_wall_reaches_page_and_search_calls() {
        let spider = Spider::builder()
            .key("not-a-real-key")
            .base_url(Url::parse("https://example.com").unwrap())
            .build()
            .unwrap();
        // All page builders use new or bare; search uses bare and run_json.
        // Check the default here, and exercise every send path with a short
        // wall in wire::f1_wall_ends_every_operation.
        for call in [
            Call::new(&spider, "https://example.com"),
            Call::bare(&spider),
        ] {
            assert_eq!(call.budget.wall, Some(Duration::from_secs(900)));
            assert!(Deadline::new(call.budget.wall).unwrap().0.is_some());
        }
    }

    #[test]
    fn f2_a_malformed_breakdown_does_not_erase_a_readable_total() {
        let body = serde_json::json!({
            "costs": { "total_cost": 0.0002, "compute_cost": {} },
            "status": "not a status"
        });
        assert!(has_charge(&body));
        assert_eq!(charged(&body), Credits(2.0));
        assert!(!has_charge(&serde_json::json!({"costs": {}})));
    }

    #[test]
    fn a_caller_that_declined_a_body_is_not_treated_as_a_blank_page() {
        // Need::Metadata and Need::Fields ask for return_format=empty on purpose.
        // Found live: the walk escalated five times against a request that had
        // already worked, and charged for every step.
        let mut params = RequestParams {
            return_format: Some(ReturnFormatHandling::Single(ReturnFormat::Empty)),
            ..RequestParams::default()
        };
        assert!(body_was_declined(&params));

        params.return_format = Some(ReturnFormatHandling::Multi(vec![ReturnFormat::Empty]));
        assert!(body_was_declined(&params));
    }

    #[test]
    fn a_caller_that_wanted_a_body_still_notices_a_blank_page() {
        let mut params = RequestParams::default();
        assert!(!body_was_declined(&params));

        params.return_format = Some(ReturnFormatHandling::Single(ReturnFormat::Markdown));
        assert!(!body_was_declined(&params));

        // Asking for markdown and an empty slot still wants markdown.
        params.return_format = Some(ReturnFormatHandling::Multi(vec![
            ReturnFormat::Markdown,
            ReturnFormat::Empty,
        ]));
        assert!(!body_was_declined(&params));

        // An empty list is not a declined body, it is an unset field.
        params.return_format = Some(ReturnFormatHandling::Multi(vec![]));
        assert!(!body_was_declined(&params));
    }

    #[test]
    fn a_step_up_the_ladder_keeps_the_plan_the_need_asked_for() {
        let caller = RequestParams::url("https://example.com");
        let plan = Plan::for_need(&Need::Markdown);
        let mut params = caller.clone();
        plan.apply_over(&mut params, &caller);

        for step in Ladder::standard().0 {
            // Something that reaches for the whole page, the way a rung added
            // later might.
            params.return_format = Some(ReturnFormat::Raw.into());
            params.metadata = Some(true);

            escalate(&step, &mut params, &plan, &caller);

            assert_eq!(
                params.return_format,
                Some(ReturnFormat::Markdown.into()),
                "the {} step left a fat return format behind",
                step.label
            );
            assert_eq!(params.metadata, Some(false), "after {}", step.label);
            // And the step itself still took effect.
            assert!(params.request.is_some(), "after {}", step.label);
        }
    }

    #[test]
    fn a_step_never_argues_with_a_setting_the_caller_made() {
        let mut caller = RequestParams::url("https://example.com");
        caller.return_format = Some(ReturnFormat::Raw.into());
        let plan = Plan::for_need(&Need::Markdown);
        let mut params = caller.clone();

        for step in Ladder::standard().0 {
            escalate(&step, &mut params, &plan, &caller);
        }

        assert_eq!(params.return_format, Some(ReturnFormat::Raw.into()));
    }

    #[test]
    fn only_a_page_request_is_moved_to_another_endpoint() {
        let links = Plan::for_need(&Need::Links);
        assert_eq!(endpoint_for(&links, route::SCRAPE), route::LINKS);
        // A crawl that wants links keeps crawling.
        assert_eq!(endpoint_for(&links, route::CRAWL), route::CRAWL);

        let shot = Plan::for_need(&Need::screenshot());
        assert_eq!(endpoint_for(&shot, route::SCRAPE), route::SCREENSHOT);

        let markdown = Plan::for_need(&Need::Markdown);
        assert_eq!(endpoint_for(&markdown, route::SCRAPE), route::SCRAPE);
        assert_eq!(endpoint_for(&Plan::none(), route::SCRAPE), route::SCRAPE);
    }

    #[test]
    fn trimmed_text_goes_back_in_the_shape_it_came_out_of() {
        let mut body = Body::Markdown("# one".into());
        replace_text(&mut body, "# two".into());
        assert_eq!(body, Body::Markdown("# two".into()));

        // A picture has nothing to trim and is left as it is.
        let mut shot = Body::Screenshot(bytes::Bytes::from_static(b"png"));
        replace_text(&mut shot, "nonsense".into());
        assert_eq!(shot, Body::Screenshot(bytes::Bytes::from_static(b"png")));
    }

    #[tokio::test(start_paused = true)]
    async fn f2_wait_admission_uses_time_spent_outside_the_policy() {
        let deadline = Deadline::new(Some(Duration::from_millis(600))).unwrap();
        let wait = Duration::from_millis(500);
        assert!(deadline.allows_wait(wait));
        tokio::time::advance(Duration::from_millis(200)).await;
        assert!(!deadline.allows_wait(wait));
        assert!(!deadline.allows_wait(Duration::from_millis(400)));
        assert!(deadline.allows_wait(Duration::from_millis(399)));
        tokio::time::advance(Duration::from_millis(400)).await;
        assert!(!deadline.allows_wait(Duration::ZERO));
        assert!(Deadline::new(None).unwrap().allows_wait(Duration::MAX));
    }

    fn stored(mode: &str) -> StoredRouter {
        StoredRouter {
            router: crate::params::Router {
                mode: Some(mode.to_string()),
                provider: Some("zyte".to_string()),
                funding: Some("own".to_string()),
                ..crate::params::Router::default()
            },
            provider_options: Some(
                [("zyte".to_string(), serde_json::json!({"geolocation": "US"}))].into(),
            ),
        }
    }

    #[test]
    fn a_stored_router_fills_in_what_the_caller_left_alone() {
        let store = stored("fallback");
        let caller = RequestParams::url("https://example.com");
        let mut params = caller.clone();
        apply_stored_router(Some(&store), &mut params, &caller);
        assert_eq!(params.router, Some(store.router.clone()));
        assert_eq!(params.provider_options, store.provider_options);
        let wire = serde_json::to_value(&params).unwrap();
        assert_eq!(wire["router"]["mode"], "fallback");
        assert_eq!(wire["provider_options"]["zyte"]["geolocation"], "US");

        // No store, nothing written.
        let mut params = caller.clone();
        apply_stored_router(None, &mut params, &caller);
        assert_eq!(params, caller);
    }

    #[test]
    fn a_stored_router_never_argues_with_the_caller() {
        let store = stored("fallback");
        let mut caller = RequestParams::url("https://example.com");
        caller.router = Some(crate::params::Router {
            mode: Some("off".to_string()),
            ..crate::params::Router::default()
        });
        caller.provider_options =
            Some([("zyte".to_string(), serde_json::json!({"geolocation": "DE"}))].into());
        let mut params = caller.clone();
        apply_stored_router(Some(&store), &mut params, &caller);
        assert_eq!(params, caller);

        // The two are decided apart: the caller's router stays, and options
        // the caller did not set are filled in.
        caller.provider_options = None;
        let mut params = caller.clone();
        apply_stored_router(Some(&store), &mut params, &caller);
        assert_eq!(params.router, caller.router);
        assert_eq!(params.provider_options, store.provider_options);

        // The snapshot decides, not what is on the request by now: a router
        // written after the snapshot is not the caller's.
        let caller = RequestParams::url("https://example.com");
        let mut params = caller.clone();
        params.router = Some(crate::params::Router::default());
        apply_stored_router(Some(&store), &mut params, &caller);
        assert_eq!(params.router, Some(store.router));
    }
}