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
//! The steps an escalation walks, cheapest first.
//!
//! A [`Rung`] is one public request parameter with a value. A [`Step`] is the set of
//! rungs that define one position on the ladder, and a [`Ladder`] is those positions
//! in the order they are tried.
//!
//! Every step is a superset of the one before it, so escalating never gives up
//! something that was already helping. Each step also costs more than the one before,
//! which is what [`Step::est_multiplier`] records and what lets a budget refuse a step
//! before paying for it.

use crate::credits::Credits;
use crate::params::js::Timeout;
use crate::params::{Country, Profile, ProxyPool, RequestMode, RequestParams, WaitFor};
use crate::policy::budget::Budget;
use std::time::Duration;
use url::Url;

/// One request parameter, with the value an escalation sets it to.
///
/// The list is short on purpose. Each entry is a documented parameter a caller could
/// set by hand and read back off the request, so an escalation is always explainable.
/// Anything finer belongs behind the full parameter struct, not here.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Rung {
    /// Set `request`, which decides whether the page is rendered before it is read.
    Mode(RequestMode),
    /// Set `proxy`, which decides the pool the request leaves from.
    Proxy(ProxyPool),
    /// Set `country_code`, which decides where the request appears to come from.
    Country(Country),
    /// Set `wait_for`, which decides what has to happen before the page is read.
    Wait(WaitFor),
    /// Set the user agent and viewport that go with a coarse identity.
    Profile(Profile),
    /// Set `request_timeout`, the span one page has to come back in.
    Timeout(Duration),
    /// Set `session`, which decides whether state carries from one request to the next.
    Session(bool),
}

impl Rung {
    /// Write this rung onto a request, replacing whatever was there.
    ///
    /// Replacing is the point: an escalation is a deliberate override of what the
    /// previous attempt sent.
    pub fn apply(&self, params: &mut RequestParams) {
        match self {
            Rung::Mode(mode) => params.request = Some(*mode),
            Rung::Proxy(pool) => params.proxy = Some(*pool),
            Rung::Country(country) => params.country_code = Some(country.clone()),
            Rung::Wait(wait) => params.wait_for = Some(wait.clone()),
            Rung::Profile(profile) => {
                if let Some(agent) = profile.user_agent() {
                    params.user_agent = Some(agent.to_string());
                }
                params.viewport = Some(profile.viewport());
            }
            Rung::Timeout(span) => {
                let secs = span.as_secs().clamp(1, u64::from(u8::MAX)) as u8;
                params.request_timeout = Some(secs);
            }
            Rung::Session(on) => params.session = Some(*on),
        }
    }
}

/// One position on the ladder.
#[derive(Debug, Clone, PartialEq)]
pub struct Step {
    /// The parameters this step sets.
    pub rungs: Vec<Rung>,
    /// A short name for the step, safe to log and to show a caller.
    pub label: &'static str,
    /// What this step is expected to cost, as a multiple of a plain fetch of the same
    /// page. A budget multiplies it by the last observed cost to decide whether the
    /// step is affordable before sending it.
    pub est_multiplier: f32,
}

impl Step {
    /// Build a step.
    pub fn new(label: &'static str, est_multiplier: f32, rungs: Vec<Rung>) -> Step {
        Step {
            rungs,
            label,
            est_multiplier,
        }
    }

    /// Write every rung of this step onto a request.
    pub fn apply(&self, params: &mut RequestParams) {
        for rung in &self.rungs {
            rung.apply(params);
        }
    }

    /// The country this step pins, when it pins one.
    pub fn country(&self) -> Option<&Country> {
        self.rungs.iter().find_map(|rung| match rung {
            Rung::Country(country) => Some(country),
            _ => None,
        })
    }

    /// Whether this step changes where the request comes from.
    pub fn moves_country(&self) -> bool {
        self.country().is_some()
    }

    /// What this step is expected to cost, given what the last attempt cost.
    ///
    /// A basis of nothing is raised to [`Budget::floor`] before it is scaled, so an
    /// escalation that follows an unbilled attempt still produces a figure a credit cap
    /// can refuse. Scaling happens after the floor, so the dearest step still estimates
    /// dearest.
    pub fn estimate(&self, last_cost: Credits) -> Credits {
        Credits(Budget::floor(last_cost).get() * f64::from(self.est_multiplier))
    }
}

/// The countries a geographic escalation may rotate through, in order.
///
/// There is no built-in list of far away places. A request that fails from one address
/// is not helped by an address chosen for being unusual, and a caller who knows the
/// site knows which countries it serves.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CountryPool(Vec<Country>);

impl CountryPool {
    /// A pool from an explicit list, in the order they will be tried. Repeats are
    /// dropped.
    pub fn new(countries: impl IntoIterator<Item = Country>) -> CountryPool {
        let mut out: Vec<Country> = Vec::new();
        for country in countries {
            if !out.contains(&country) {
                out.push(country);
            }
        }
        CountryPool(out)
    }

    /// The pool to use when the caller named none: the address's own country code when
    /// it has one, then `us`.
    ///
    /// A site on a two letter national domain usually serves that country best, and a
    /// site on anything else is most often served from the United States.
    pub fn for_url(url: &str) -> CountryPool {
        let mut countries = Vec::new();

        if let Some(country) = Url::parse(url)
            .ok()
            .and_then(|parsed| parsed.host_str().map(str::to_owned))
            .and_then(|host| host.rsplit('.').next().and_then(Country::new))
        {
            countries.push(country);
        }

        if let Some(fallback) = Country::new("us") {
            countries.push(fallback);
        }

        CountryPool::new(countries)
    }

    /// The countries, in order.
    pub fn countries(&self) -> &[Country] {
        &self.0
    }

    /// Whether the pool holds nothing, in which case a ladder built from it has no
    /// geographic step.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl Default for Ladder {
    /// The standard ladder with no address to read a country from, so the geographic
    /// step rotates to `us`.
    fn default() -> Ladder {
        Ladder::standard()
    }
}

/// The steps in the order they are tried.
///
/// Index zero is the first escalation, not the first attempt. The first attempt uses
/// whatever the caller or the router already chose, and the ladder is where a failure
/// goes next.
#[derive(Debug, Clone, PartialEq)]
pub struct Ladder(pub Vec<Step>);

/// How long a wait step gives the page to settle before reading it anyway.
const SETTLE: Timeout = Timeout::from_secs(10);

impl Ladder {
    /// The standard ladder, rotating to `us` at the geographic step.
    pub fn standard() -> Ladder {
        Ladder::with_countries(&CountryPool::new(Country::new("us")))
    }

    /// The standard ladder with the geographic step reading its countries from the
    /// address being fetched.
    pub fn for_target(url: &str) -> Ladder {
        Ladder::with_countries(&CountryPool::for_url(url))
    }

    /// The standard ladder with a caller's own country rotation.
    ///
    /// The first three steps never change. The pool adds one step per country after
    /// them, each costing slightly more than the last so the order stays readable off
    /// the multipliers alone.
    pub fn with_countries(pool: &CountryPool) -> Ladder {
        let rendered = vec![Rung::Mode(RequestMode::Browser)];

        let settled = vec![
            Rung::Mode(RequestMode::Browser),
            Rung::Wait(WaitFor::idle_network(SETTLE)),
        ];

        let mut residential = settled.clone();
        residential.push(Rung::Proxy(ProxyPool::Residential));

        let mut steps = vec![
            Step::new("browser", 4.0, rendered),
            Step::new("browser+wait", 5.0, settled),
            Step::new("residential", 8.0, residential.clone()),
        ];

        for (offset, country) in pool.countries().iter().enumerate() {
            let mut rungs = residential.clone();
            rungs.push(Rung::Country(country.clone()));
            steps.push(Step::new("residential+geo", 9.0 + offset as f32, rungs));
        }

        Ladder(steps)
    }

    /// The step at an index, when the ladder goes that far.
    pub fn get(&self, index: usize) -> Option<&Step> {
        self.0.get(index)
    }

    /// How many steps the ladder holds.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Whether the ladder holds no steps at all.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// The first step at or after `from` that changes the country.
    ///
    /// A site that is rate limiting the fetch is not helped by a heavier request, and
    /// sending one costs more for the same refusal. Coming from somewhere else is the
    /// move that has a chance, so a rate limit jumps here rather than walking.
    pub fn first_geo_from(&self, from: usize) -> Option<usize> {
        (from..self.0.len()).find(|index| self.0.get(*index).is_some_and(Step::moves_country))
    }
}

#[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::policy::budget::ASSUMED_MINIMUM_COST;

    #[test]
    fn the_standard_ladder_is_four_steps_ending_in_a_country_move() {
        let ladder = Ladder::standard();
        let labels: Vec<_> = ladder.0.iter().map(|step| step.label).collect();
        assert_eq!(
            labels,
            ["browser", "browser+wait", "residential", "residential+geo"]
        );
        assert_eq!(
            ladder.get(3).and_then(Step::country).map(Country::as_str),
            Some("us")
        );
    }

    #[test]
    fn each_step_is_a_superset_of_the_one_before() {
        let ladder = Ladder::standard();
        for pair in ladder.0.windows(2) {
            for rung in &pair[0].rungs {
                assert!(
                    pair[1].rungs.contains(rung),
                    "{} dropped {rung:?} that {} had",
                    pair[1].label,
                    pair[0].label
                );
            }
        }
    }

    #[test]
    fn the_estimates_only_go_up() {
        for ladder in [
            Ladder::standard(),
            Ladder::for_target("https://example.de/thing"),
        ] {
            for pair in ladder.0.windows(2) {
                assert!(
                    pair[1].est_multiplier > pair[0].est_multiplier,
                    "{} does not cost more than {}",
                    pair[1].label,
                    pair[0].label
                );
            }
        }
    }

    #[test]
    fn a_national_domain_rotates_to_its_own_country_first() {
        let pool = CountryPool::for_url("https://shop.example.de/a");
        let codes: Vec<_> = pool.countries().iter().map(Country::as_str).collect();
        assert_eq!(codes, ["de", "us"]);

        let ladder = Ladder::for_target("https://shop.example.de/a");
        assert_eq!(ladder.len(), 5);
        assert_eq!(
            ladder.get(3).and_then(Step::country).map(Country::as_str),
            Some("de")
        );
        assert_eq!(
            ladder.get(4).and_then(Step::country).map(Country::as_str),
            Some("us")
        );
    }

    #[test]
    fn a_generic_domain_rotates_to_us_only() {
        let pool = CountryPool::for_url("https://example.com/a");
        let codes: Vec<_> = pool.countries().iter().map(Country::as_str).collect();
        assert_eq!(codes, ["us"]);
    }

    #[test]
    fn a_us_domain_is_not_listed_twice() {
        let pool = CountryPool::for_url("https://example.us/a");
        let codes: Vec<_> = pool.countries().iter().map(Country::as_str).collect();
        assert_eq!(codes, ["us"]);
    }

    #[test]
    fn the_geo_jump_finds_the_first_country_step() {
        let ladder = Ladder::standard();
        assert_eq!(ladder.first_geo_from(0), Some(3));
        assert_eq!(ladder.first_geo_from(3), Some(3));
        assert_eq!(ladder.first_geo_from(4), None);
    }

    #[test]
    fn every_rung_and_standard_step_changes_exactly_its_documented_keys() {
        use std::collections::BTreeSet;
        let cases: Vec<(Rung, Vec<&str>)> = vec![
            (Rung::Mode(RequestMode::Browser), vec!["request"]),
            (Rung::Proxy(ProxyPool::Residential), vec!["proxy"]),
            (
                Rung::Country(Country::new("us").unwrap()),
                vec!["country_code"],
            ),
            (Rung::Wait(WaitFor::idle_network(SETTLE)), vec!["wait_for"]),
            (Rung::Profile(Profile::Desktop), vec!["viewport"]),
            (Rung::Profile(Profile::MobileDevice), vec!["viewport"]),
            (Rung::Profile(Profile::Bot), vec!["user_agent", "viewport"]),
            (
                Rung::Timeout(Duration::from_secs(45)),
                vec!["request_timeout"],
            ),
            (Rung::Session(true), vec!["session"]),
        ];
        let mut steps: Vec<(Step, Vec<&str>)> = cases
            .into_iter()
            .map(|(rung, keys)| (Step::new("single rung", 1.0, vec![rung]), keys))
            .collect();
        let standard_keys = [
            vec!["request"],
            vec!["request", "wait_for"],
            vec!["request", "wait_for", "proxy"],
            vec!["request", "wait_for", "proxy", "country_code"],
        ];
        let standard = Ladder::standard();
        assert_eq!(standard.len(), standard_keys.len());
        steps.extend(standard.0.into_iter().zip(standard_keys));
        // Values differ from every rung above, including both viewport sizes.
        // Other populated fields must survive every step unchanged.
        let populated: RequestParams = serde_json::from_value(serde_json::json!({
            "url": "https://example.org/page", "request": "http", "proxy": "isp",
            "country_code": "de", "request_timeout": 12, "session": false,
            "wait_for": {"delay": {"timeout": {"secs": 1, "nanos": 0}}},
            "user_agent": "caller agent",
            "viewport": {"width": 777, "height": 555},
            "stealth": true, "fingerprint": true, "limit": 3,
            "metadata": true, "headers": {"accept": "text/html"}, "cookies": "a=b"
        }))
        .unwrap();
        for (step, expected) in steps {
            for initial in [RequestParams::default(), populated.clone()] {
                let before = serde_json::to_value(&initial).unwrap();
                let mut after = initial;
                step.apply(&mut after);
                let after = serde_json::to_value(after).unwrap();
                let before = before.as_object().unwrap();
                let after = after.as_object().unwrap();
                let changed: BTreeSet<&str> = before
                    .keys()
                    .chain(after.keys())
                    .filter(|key| before.get(*key) != after.get(*key))
                    .map(String::as_str)
                    .collect();
                assert_eq!(changed, expected.iter().copied().collect(), "{step:?}");
            }
        }
    }

    #[test]
    fn applying_a_step_writes_the_parameters_it_names() {
        let mut params = RequestParams::url("https://example.com");
        Ladder::standard()
            .get(3)
            .expect("the geographic step")
            .apply(&mut params);

        assert_eq!(params.request, Some(RequestMode::Browser));
        assert_eq!(params.proxy, Some(ProxyPool::Residential));
        assert_eq!(
            params.country_code.as_ref().map(Country::as_str),
            Some("us")
        );
        assert!(params
            .wait_for
            .as_ref()
            .is_some_and(|wait| wait.idle_network.is_some()));
        // The escalation never touches what the service decides for itself.
        assert_eq!(params.stealth, None);
        assert_eq!(params.fingerprint, None);
    }

    #[test]
    fn applying_a_step_replaces_what_the_last_attempt_sent() {
        let mut params = RequestParams::url("https://example.com");
        params.proxy = Some(ProxyPool::Isp);
        Ladder::standard()
            .get(2)
            .expect("the residential step")
            .apply(&mut params);
        assert_eq!(params.proxy, Some(ProxyPool::Residential));
    }

    #[test]
    fn a_timeout_rung_is_written_in_whole_seconds() {
        let mut params = RequestParams::default();
        Rung::Timeout(Duration::from_millis(45_500)).apply(&mut params);
        assert_eq!(params.request_timeout, Some(45));

        Rung::Timeout(Duration::from_secs(9_000)).apply(&mut params);
        assert_eq!(params.request_timeout, Some(255));

        Rung::Timeout(Duration::from_millis(10)).apply(&mut params);
        assert_eq!(params.request_timeout, Some(1));
    }

    #[test]
    fn a_profile_rung_sets_the_viewport_and_only_pins_an_agent_when_the_profile_does() {
        let mut params = RequestParams::default();
        Rung::Profile(Profile::Desktop).apply(&mut params);
        assert_eq!(params.user_agent, None);
        assert!(params.viewport.is_some());

        Rung::Profile(Profile::Bot).apply(&mut params);
        assert!(params.user_agent.is_some());
    }

    #[test]
    fn a_session_rung_sets_the_session_flag() {
        let mut params = RequestParams::default();
        Rung::Session(true).apply(&mut params);
        assert_eq!(params.session, Some(true));
    }

    #[test]
    fn the_ladder_ends_on_the_geographic_step() {
        for ladder in [
            Ladder::standard(),
            Ladder::for_target("https://shop.example.de/a"),
        ] {
            let last = ladder.0.last().expect("a last step");
            assert_eq!(last.label, "residential+geo");
            assert!(last.moves_country(), "the last step changes the country");
        }
    }

    #[test]
    fn an_estimate_scales_the_last_observed_cost() {
        let step = Ladder::standard().get(2).expect("residential").clone();
        assert_eq!(step.estimate(Credits(3.0)), Credits(24.0));
    }

    #[test]
    fn an_estimate_after_a_free_attempt_still_names_a_figure() {
        let ladder = Ladder::standard();
        let cheapest = ladder.get(0).expect("browser");
        let dearest = ladder.get(3).expect("the geographic step");

        // Scaling zero would give zero for every step, and a credit cap fed zero can
        // refuse nothing. The figures are multiples of the floor, so they follow it.
        let floor = ASSUMED_MINIMUM_COST.get();
        assert_eq!(cheapest.estimate(Credits::ZERO), Credits(floor * 4.0));
        assert_eq!(dearest.estimate(Credits::ZERO), Credits(floor * 9.0));
        assert!(dearest.estimate(Credits::ZERO) > cheapest.estimate(Credits::ZERO));
    }

    #[test]
    fn an_empty_pool_leaves_the_ladder_without_a_country_step() {
        let ladder = Ladder::with_countries(&CountryPool::default());
        assert_eq!(ladder.len(), 3);
        assert_eq!(ladder.first_geo_from(0), None);
    }
}