spider-optimize 0.8.0

Local, gated edits to Spider Cloud request parameters, scored for success, latency and credits.
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
//! A change to a request, and writing it.
//!
//! An [`EditSet`] is up to [`MAX_EDITS`] edits on distinct keys, kept in key
//! order so two sets holding the same edits compare equal. The empty set is
//! [`EditSet::keep`], which leaves the request exactly as it is.
//!
//! [`EditSet::apply`] holds the precedence rule the client's router already
//! follows: the caller beats everything. A field set on the caller's own
//! snapshot of the request is never written, whatever the router or the plan
//! wrote into the request since.

use crate::params::Params;
use crate::schema::Key;
use spider_route::{ProxyPool, RequestMode};
use std::fmt;

/// A value an edit sets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Value {
    /// For a `Bool` field.
    Bool(bool),
    /// For `request`.
    Mode(RequestMode),
    /// For `proxy`.
    Proxy(ProxyPool),
    /// For an idle network wait, in milliseconds.
    Millis(u32),
    /// For an `IntRange` field. No learnable key takes one in version one, so
    /// [`EditSet::apply`] writes nothing for it.
    Int(i64),
}

/// What an edit does to its field.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Op {
    /// Set a single value.
    Set(Value),
    /// Add these entries to the end of a list, skipping any already there.
    Append(Vec<String>),
    /// Take these entries out of a list.
    Remove(Vec<String>),
    /// Replace the list with these entries.
    Replace(Vec<String>),
    /// Unset the list.
    Clear,
}

impl Op {
    /// The code a row records for this op: set 0, append 1, remove 2,
    /// replace 3, clear 4.
    pub const fn code(&self) -> u8 {
        match self {
            Op::Set(_) => 0,
            Op::Append(_) => 1,
            Op::Remove(_) => 2,
            Op::Replace(_) => 3,
            Op::Clear => 4,
        }
    }

    /// The name of each op, in code order.
    pub const NAMES: [&'static str; 5] = ["set", "append", "remove", "replace", "clear"];

    const fn is_list(&self) -> bool {
        !matches!(self, Op::Set(_))
    }
}

/// One change to one field.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Edit {
    /// The field.
    pub key: Key,
    /// The change.
    pub op: Op,
}

impl Edit {
    /// Set a field to a value.
    pub const fn set(key: Key, value: Value) -> Edit {
        Edit {
            key,
            op: Op::Set(value),
        }
    }
}

/// The most edits one set holds.
pub const MAX_EDITS: usize = 3;

/// Why a set of edits could not be built.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditError {
    /// Two edits name the same key.
    Duplicate(Key),
    /// More than [`MAX_EDITS`] edits.
    TooMany,
    /// A list operation on a field that is not the network blacklist.
    Unsupported(Key),
}

impl fmt::Display for EditError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EditError::Duplicate(key) => write!(f, "two edits name {}", key.wire()),
            EditError::TooMany => write!(f, "an edit set holds at most {MAX_EDITS} edits"),
            EditError::Unsupported(key) => {
                write!(f, "{} does not take a list operation", key.wire())
            }
        }
    }
}

impl std::error::Error for EditError {}

/// Up to [`MAX_EDITS`] edits on distinct keys, in key order.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct EditSet {
    edits: Vec<Edit>,
}

/// What [`EditSet::apply`] did.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Applied {
    /// Edits written onto the request.
    pub written: u8,
    /// Edits left out because the caller set the field.
    pub skipped_pinned: u8,
}

impl EditSet {
    /// No edits: the request goes out as it is.
    pub const fn keep() -> EditSet {
        EditSet { edits: Vec::new() }
    }

    /// Whether this is the set that changes nothing.
    pub fn is_keep(&self) -> bool {
        self.edits.is_empty()
    }

    /// Build a set, sorted by key.
    ///
    /// Refuses two edits on one key, more than [`MAX_EDITS`] edits, and a
    /// list operation on anything but [`Key::NetworkBlacklist`].
    pub fn new(mut edits: Vec<Edit>) -> Result<EditSet, EditError> {
        if edits.len() > MAX_EDITS {
            return Err(EditError::TooMany);
        }
        for edit in &edits {
            if edit.op.is_list() && edit.key != Key::NetworkBlacklist {
                return Err(EditError::Unsupported(edit.key));
            }
        }
        edits.sort_by_key(|edit| edit.key.index());
        for pair in edits.windows(2) {
            if let [first, second] = pair {
                if first.key == second.key {
                    return Err(EditError::Duplicate(first.key));
                }
            }
        }
        Ok(EditSet { edits })
    }

    /// The edits, in key order.
    pub fn edits(&self) -> &[Edit] {
        &self.edits
    }

    /// The edit on this key, if there is one.
    pub fn get(&self, key: Key) -> Option<&Edit> {
        self.edits.iter().find(|edit| edit.key == key)
    }

    /// Write the edits onto `params`, leaving every field the caller set.
    ///
    /// `caller` is the request as the caller left it, before the router or
    /// the plan wrote anything. A `Set` is written only where the caller's
    /// field is unset. An idle wait of zero writes nothing, because there is
    /// no wait to send. A list operation on the blacklist is left out when
    /// the caller set either network list, since the caller's whitelist wins
    /// over any blacklist entry and an edit must not argue with it.
    pub fn apply(&self, params: &mut dyn Params, caller: &dyn Params) -> Applied {
        let mut applied = Applied::default();

        for edit in &self.edits {
            let pinned = match edit.key {
                Key::NetworkBlacklist => {
                    caller.is_set(Key::NetworkBlacklist) || caller.is_set(Key::NetworkWhitelist)
                }
                key => caller.is_set(key),
            };
            if pinned {
                applied.skipped_pinned = applied.skipped_pinned.saturating_add(1);
                continue;
            }

            let wrote = match (&edit.op, edit.key) {
                (Op::Set(Value::Mode(mode)), Key::Request) => {
                    params.set_request(*mode);
                    true
                }
                (Op::Set(Value::Proxy(pool)), Key::Proxy) => {
                    params.set_proxy(*pool);
                    true
                }
                (Op::Set(Value::Millis(millis)), Key::WaitIdleMillis) => {
                    if *millis > 0 {
                        params.set_idle_wait(*millis);
                    }
                    *millis > 0
                }
                (Op::Set(Value::Bool(value)), key) => {
                    params.set_flag(key, *value);
                    true
                }
                (Op::Append(entries), Key::NetworkBlacklist) => {
                    let mut list: Vec<String> = params
                        .list(Key::NetworkBlacklist)
                        .map(<[String]>::to_vec)
                        .unwrap_or_default();
                    for entry in entries {
                        if !list.contains(entry) {
                            list.push(entry.clone());
                        }
                    }
                    params.set_list(Key::NetworkBlacklist, Some(list));
                    true
                }
                (Op::Remove(entries), Key::NetworkBlacklist) => {
                    let list: Vec<String> = params
                        .list(Key::NetworkBlacklist)
                        .unwrap_or_default()
                        .iter()
                        .filter(|entry| !entries.contains(entry))
                        .cloned()
                        .collect();
                    params.set_list(Key::NetworkBlacklist, non_empty(list));
                    true
                }
                (Op::Replace(entries), Key::NetworkBlacklist) => {
                    let mut list: Vec<String> = Vec::with_capacity(entries.len());
                    for entry in entries {
                        if !list.contains(entry) {
                            list.push(entry.clone());
                        }
                    }
                    params.set_list(Key::NetworkBlacklist, non_empty(list));
                    true
                }
                (Op::Clear, Key::NetworkBlacklist) => {
                    params.set_list(Key::NetworkBlacklist, None);
                    true
                }
                // A value of the wrong type for its key, or an integer, which
                // no learnable key takes. Validation refuses both before an
                // edit gets here, so nothing is written.
                _ => false,
            };

            if wrote {
                applied.written = applied.written.saturating_add(1);
            }
        }

        applied
    }
}

/// An empty list is sent as no list at all.
fn non_empty(list: Vec<String>) -> Option<Vec<String>> {
    if list.is_empty() {
        None
    } else {
        Some(list)
    }
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::panic,
        clippy::unreachable,
        clippy::indexing_slicing,
        clippy::string_slice
    )]
    use super::*;
    use crate::schema::{Schema, MODES, PROXIES, WAIT_BUCKETS};
    use spider_cloud_agent::params::{RequestParams, Timeout, WaitFor};

    fn append(entries: &[&str]) -> EditSet {
        EditSet::new(vec![Edit {
            key: Key::NetworkBlacklist,
            op: Op::Append(entries.iter().map(|e| e.to_string()).collect()),
        }])
        .unwrap()
    }

    #[test]
    fn append_keeps_existing_entries_and_dedupes() {
        let caller = RequestParams::default();
        // Written by the router or the plan, not the caller.
        let mut params = RequestParams {
            network_blacklist: Some(vec!["a.example".into(), "b.example".into()]),
            ..RequestParams::default()
        };

        let applied = append(&["b.example", "c.example", "c.example", "a.example"])
            .apply(&mut params, &caller);

        assert_eq!(applied.written, 1);
        assert_eq!(
            params.network_blacklist.as_deref().unwrap(),
            ["a.example", "b.example", "c.example"]
        );
    }

    #[test]
    fn a_whitelist_on_the_caller_blocks_a_blacklist_edit() {
        let caller = RequestParams {
            network_whitelist: Some(vec!["example.com".into()]),
            ..RequestParams::default()
        };
        let mut params = caller.clone();

        let applied = append(&["c.example"]).apply(&mut params, &caller);

        assert_eq!(applied.skipped_pinned, 1);
        assert_eq!(params, caller);
    }

    /// Every value of every learnable key, one edit per set.
    fn every_learnable_edit() -> Vec<EditSet> {
        let mut sets = Vec::new();
        for spec in Schema::v1().learnable() {
            let edits: Vec<Edit> = match spec.key {
                Key::Request => MODES
                    .iter()
                    .map(|m| Edit::set(spec.key, Value::Mode(*m)))
                    .collect(),
                Key::Proxy => PROXIES
                    .iter()
                    .map(|p| Edit::set(spec.key, Value::Proxy(*p)))
                    .collect(),
                Key::WaitIdleMillis => WAIT_BUCKETS
                    .iter()
                    .map(|n| Edit::set(spec.key, Value::Millis(*n)))
                    .collect(),
                Key::NetworkBlacklist => vec![
                    Edit {
                        key: spec.key,
                        op: Op::Append(vec!["c.example".into()]),
                    },
                    Edit {
                        key: spec.key,
                        op: Op::Remove(vec!["example.net".into()]),
                    },
                    Edit {
                        key: spec.key,
                        op: Op::Replace(vec!["c.example".into()]),
                    },
                    Edit {
                        key: spec.key,
                        op: Op::Clear,
                    },
                ],
                key => vec![
                    Edit::set(key, Value::Bool(true)),
                    Edit::set(key, Value::Bool(false)),
                ],
            };
            for edit in edits {
                sets.push(EditSet::new(vec![edit]).unwrap());
            }
        }
        sets
    }

    #[test]
    fn an_edit_never_touches_a_field_the_caller_set() {
        let caller = crate::schema::tests::populated();

        let sets = every_learnable_edit();
        assert!(sets.len() >= 20, "only {} edits were tried", sets.len());

        for set in sets {
            let mut params = caller.clone();
            let applied = set.apply(&mut params, &caller);
            assert_eq!(params, caller, "{set:?} wrote over the caller");
            assert_eq!(applied.written, 0, "{set:?}");
            assert_eq!(applied.skipped_pinned, 1, "{set:?}");
        }
    }

    #[test]
    fn an_edit_writes_where_the_caller_left_a_gap() {
        // The counterpart to the test above, so it cannot pass by writing
        // nothing anywhere.
        let caller = RequestParams::default();
        let mut params = RequestParams::default();
        let set = EditSet::new(vec![
            Edit::set(Key::Request, Value::Mode(RequestMode::Browser)),
            Edit::set(Key::WaitIdleMillis, Value::Millis(5_000)),
            Edit::set(Key::BlockAds, Value::Bool(false)),
        ])
        .unwrap();

        let applied = set.apply(&mut params, &caller);

        assert_eq!(applied.written, 3);
        assert_eq!(params.request, Some(RequestMode::Browser));
        assert_eq!(
            params.wait_for,
            Some(WaitFor::idle_network(Timeout::from_millis(5_000)))
        );
        assert_eq!(params.block_ads, Some(false));
    }

    #[test]
    fn an_edit_set_rejects_duplicates_and_too_many() {
        let mode = Edit::set(Key::Request, Value::Mode(RequestMode::Browser));
        assert_eq!(
            EditSet::new(vec![mode.clone(), mode.clone()]),
            Err(EditError::Duplicate(Key::Request))
        );
        assert_eq!(
            EditSet::new(vec![
                mode.clone(),
                Edit::set(Key::Proxy, Value::Proxy(ProxyPool::Residential)),
                Edit::set(Key::BlockAds, Value::Bool(false)),
                Edit::set(Key::FullResources, Value::Bool(true)),
            ]),
            Err(EditError::TooMany)
        );
        assert_eq!(
            EditSet::new(vec![Edit {
                key: Key::Blacklist,
                op: Op::Append(vec!["/a".into()]),
            }]),
            Err(EditError::Unsupported(Key::Blacklist))
        );

        // Order does not matter to what the set is.
        let a = EditSet::new(vec![
            Edit::set(Key::BlockAds, Value::Bool(false)),
            mode.clone(),
        ])
        .unwrap();
        let b = EditSet::new(vec![mode, Edit::set(Key::BlockAds, Value::Bool(false))]).unwrap();
        assert_eq!(a, b);
        assert_eq!(a.edits()[0].key, Key::Request);
        assert!(EditSet::new(Vec::new()).unwrap().is_keep());
    }

    #[test]
    fn a_zero_wait_writes_nothing() {
        let caller = RequestParams::default();
        let mut params = RequestParams::default();
        let set = EditSet::new(vec![Edit::set(Key::WaitIdleMillis, Value::Millis(0))]).unwrap();

        let applied = set.apply(&mut params, &caller);

        assert_eq!(applied, Applied::default());
        assert_eq!(params.wait_for, None);
    }
}