subspace-core 0.7.0

Subspace Framework core: config file, credentials, HTTP agent, state and PATH lookup. Internal crate; apps depend on `subspace-framework`.
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
//! Item search over a dashboard's loaded documents (BM25F). Fields are
//! scored separately and weighted;
//! the last query word matches as a prefix while typing; documents that match
//! more query words rank higher; a numeric query matches document numbers by
//! prefix. No network and no dependency on the UI.

use std::cmp::Ordering;
use std::collections::{BTreeSet, HashMap};

use unicode_normalization::UnicodeNormalization as _;

const K1: f64 = 1.2;
const B: f64 = 0.75;
const FIELD_COUNT: usize = 3;
const IDF_SMOOTHING: f64 = 0.5;
const EXACT_NUMBER_BONUS: f64 = 100.0;
const PREFIX_NUMBER_BONUS: f64 = 10.0;
/// Weights of the `title`, `parent` and `meta` fields, in that order.
const FIELD_WEIGHTS: [f64; FIELD_COUNT] = [1.0, 0.35, 0.5];
const STOP_WORDS: &[&str] = &[
    "a", "an", "and", "the", "of", "to", "in", "for", "with", "on", "or", "by", "is",
];

/// The number of results the palette shows (REQ-05).
pub const MAX_RESULTS: usize = 8;

/// Lower case with accents removed: NFKD, then combining marks dropped.
pub fn fold(text: &str) -> String {
    text.nfkd()
        .filter(|c| !('\u{300}'..='\u{36f}').contains(c))
        .flat_map(char::to_lowercase)
        .collect()
}

/// Words of `text`: folded, split at anything but letters and digits, stop
/// words removed.
pub fn tokenize(text: &str) -> Vec<String> {
    fold(text)
        .split(|c: char| !c.is_alphanumeric())
        .filter(|token| !token.is_empty() && !STOP_WORDS.contains(token))
        .map(str::to_string)
        .collect()
}

/// One searchable thing. Dashboards map their items to documents.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SearchDoc {
    /// Unique within the index; ties rank by id, numbers compared as numbers.
    pub id: String,
    /// Matched by a numeric query (`4958950`, `#123`), such as a work item ID
    /// or a pull request number.
    pub number: Option<u64>,
    pub title: String,
    /// The parent's title: makes children findable, below the parent itself.
    pub parent: String,
    /// Type, state, people, labels.
    pub meta: String,
}

// qual:allow(dry, boilerplate) reason: "Three small native setters preserve the public SearchDoc builder API; generating them would add a dependency without reducing meaningful complexity."
impl SearchDoc {
    pub fn new(id: impl Into<String>, title: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            title: title.into(),
            ..Self::default()
        }
    }

    pub fn number(mut self, number: u64) -> Self {
        self.number = Some(number);
        self
    }

    pub fn parent(mut self, parent: impl Into<String>) -> Self {
        self.parent = parent.into();
        self
    }

    pub fn meta(mut self, meta: impl Into<String>) -> Self {
        self.meta = meta.into();
        self
    }

    fn fields(&self) -> [&str; 3] {
        [&self.title, &self.parent, &self.meta]
    }
}

/// A match: the document's position in the index and its score.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Hit {
    pub index: usize,
    pub score: f64,
}

struct Indexed {
    terms: [HashMap<String, u32>; FIELD_COUNT],
    lengths: [usize; FIELD_COUNT],
}

/// The index over a fixed set of documents. Build it again when they change.
pub struct SearchIndex {
    docs: Vec<SearchDoc>,
    indexed: Vec<Indexed>,
    average: [f64; FIELD_COUNT],
    frequency: [HashMap<String, u32>; FIELD_COUNT],
    vocabulary: BTreeSet<String>,
}

impl SearchIndex {
    pub fn new(docs: Vec<SearchDoc>) -> Self {
        let indexed: Vec<Indexed> = docs
            .iter()
            .map(|doc| {
                let tokens = doc.fields().map(tokenize);
                Indexed {
                    lengths: [tokens[0].len(), tokens[1].len(), tokens[2].len()],
                    terms: tokens.map(|tokens| {
                        let mut counts = HashMap::new();
                        for token in tokens {
                            *counts.entry(token).or_insert(0) += 1;
                        }
                        counts
                    }),
                }
            })
            .collect();

        let mut average = [1.0; FIELD_COUNT];
        let mut frequency: [HashMap<String, u32>; FIELD_COUNT] = Default::default();
        let mut vocabulary = BTreeSet::new();
        for field in 0..FIELD_COUNT {
            let total: usize = indexed.iter().map(|doc| doc.lengths[field]).sum();
            let mean = total as f64 / indexed.len().max(1) as f64;
            average[field] = if mean > 0.0 { mean } else { 1.0 };
            for doc in &indexed {
                for term in doc.terms[field].keys() {
                    *frequency[field].entry(term.clone()).or_insert(0) += 1;
                    vocabulary.insert(term.clone());
                }
            }
        }
        Self {
            docs,
            indexed,
            average,
            frequency,
            vocabulary,
        }
    }

    pub fn docs(&self) -> &[SearchDoc] {
        &self.docs
    }

    pub fn is_empty(&self) -> bool {
        self.docs.is_empty()
    }

    fn idf(&self, field: usize, term: &str) -> f64 {
        let n = f64::from(self.frequency[field].get(term).copied().unwrap_or(0));
        let count = self.docs.len() as f64;
        (1.0 + (count - n + IDF_SMOOTHING) / (n + IDF_SMOOTHING)).ln()
    }

    fn term_score(&self, doc: &Indexed, term: &str) -> f64 {
        let mut score = 0.0;
        for (field, weight) in FIELD_WEIGHTS.iter().enumerate() {
            let Some(&tf) = doc.terms[field].get(term) else {
                continue;
            };
            let tf = f64::from(tf);
            let norm = tf + K1 * (1.0 - B + B * doc.lengths[field] as f64 / self.average[field]);
            score += weight * self.idf(field, term) * (tf * (K1 + 1.0)) / norm;
        }
        score
    }

    /// A query word stands for itself, or (the last word while typing, or a
    /// word that is no term) for the terms it prefixes.
    fn expand(&self, word: &str, is_last: bool) -> Vec<String> {
        let exact = self.vocabulary.contains(word);
        if exact && !is_last {
            return vec![word.to_string()];
        }
        let prefixed: Vec<String> = self
            .vocabulary
            .range(word.to_string()..)
            .take_while(|term| term.starts_with(word))
            .cloned()
            .collect();
        match (prefixed.is_empty(), exact) {
            (false, _) => prefixed,
            (true, true) => vec![word.to_string()],
            (true, false) => Vec::new(),
        }
    }

    fn score_document(
        &self,
        index: usize,
        expansions: &[Vec<String>],
        word_count: usize,
        id_query: Option<&str>,
    ) -> Option<Hit> {
        let doc = &self.indexed[index];
        let mut score = 0.0;
        let mut matched = 0usize;
        for terms in expansions {
            // The best expansion per word, so one prefix cannot add up many terms.
            let best = terms
                .iter()
                .map(|term| self.term_score(doc, term))
                .fold(0.0, f64::max);
            matched += usize::from(best > 0.0);
            score += best;
        }
        score *= (matched as f64 / word_count as f64).powi(2);
        score += self.number_bonus(index, id_query);
        (score > 0.0).then_some(Hit { index, score })
    }

    fn number_bonus(&self, index: usize, id_query: Option<&str>) -> f64 {
        let Some(id) = id_query else {
            return 0.0;
        };
        let Some(number) = self.docs[index].number else {
            return 0.0;
        };
        let number = number.to_string();
        if !number.starts_with(id) {
            return 0.0;
        }
        if number == id {
            EXACT_NUMBER_BONUS
        } else {
            PREFIX_NUMBER_BONUS
        }
    }

    /// The best `limit` matches for `query`, best first.
    pub fn search(&self, query: &str, limit: usize) -> Vec<Hit> {
        let words = tokenize(query);
        if words.is_empty() {
            return Vec::new();
        }
        let last_is_prefix = !query.ends_with(char::is_whitespace);
        let expansions: Vec<Vec<String>> = words
            .iter()
            .enumerate()
            .map(|(i, word)| self.expand(word, last_is_prefix && i == words.len() - 1))
            .collect();
        let trimmed = query.trim();
        let digits = trimmed.strip_prefix('#').unwrap_or(trimmed);
        let id_query =
            (!digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit())).then_some(digits);

        let mut hits: Vec<Hit> = self
            .indexed
            .iter()
            .enumerate()
            .filter_map(|(index, _)| self.score_document(index, &expansions, words.len(), id_query))
            .collect();
        hits.sort_by(|a, b| {
            b.score
                .total_cmp(&a.score)
                .then_with(|| natural_cmp(&self.docs[a.index].id, &self.docs[b.index].id))
        });
        hits.truncate(limit);
        hits
    }
}

/// Compares text with runs of digits as numbers (`localeCompare` with
/// `numeric: true`, without the locale).
pub fn natural_cmp(a: &str, b: &str) -> Ordering {
    let mut a = a.chars().peekable();
    let mut b = b.chars().peekable();
    loop {
        match (a.peek().copied(), b.peek().copied()) {
            (None, None) => return Ordering::Equal,
            (None, Some(_)) => return Ordering::Less,
            (Some(_), None) => return Ordering::Greater,
            (Some(x), Some(y)) if x.is_ascii_digit() && y.is_ascii_digit() => {
                let take = |it: &mut std::iter::Peekable<std::str::Chars<'_>>| {
                    let mut run = String::new();
                    while let Some(c) = it.next_if(char::is_ascii_digit) {
                        run.push(c);
                    }
                    run
                };
                let (x, y) = (take(&mut a), take(&mut b));
                let (xt, yt) = (x.trim_start_matches('0'), y.trim_start_matches('0'));
                let order = xt.len().cmp(&yt.len()).then_with(|| xt.cmp(yt));
                if order != Ordering::Equal {
                    return order;
                }
            }
            (Some(x), Some(y)) => {
                let order = x
                    .to_lowercase()
                    .cmp(y.to_lowercase())
                    .then_with(|| x.cmp(&y));
                if order != Ordering::Equal {
                    return order;
                }
                a.next();
                b.next();
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{SearchDoc, SearchIndex, natural_cmp, tokenize};

    /// The web test's items, mapped as the board's `boardDocs` maps them:
    /// the short title, the parent's short title, and type, state and person.
    fn index() -> SearchIndex {
        let doc = |id: u64, title: &str, parent: &str, kind: &str, state: &str| {
            SearchDoc::new(id.to_string(), title)
                .number(id)
                .parent(parent)
                .meta(format!("{kind} {state} Doe, Jane"))
        };
        SearchIndex::new(vec![
            doc(
                4904258,
                "Automate Team onboarding (Job Lifecycle Eventing)",
                "",
                "Product Backlog Item",
                "New",
            ),
            doc(
                4904302,
                "Assign tenant receiver access with the HANK module",
                "Automate Team onboarding (Job Lifecycle Eventing)",
                "Task",
                "To Do",
            ),
            doc(
                4958950,
                "Prove the integrated job journey in Azure Monitor",
                "",
                "Task",
                "Done",
            ),
            doc(
                4959313,
                "Enforce EVA complexity rules in prek",
                "",
                "Task",
                "To Do",
            ),
        ])
    }

    fn ids(index: &SearchIndex, query: &str) -> Vec<String> {
        index
            .search(query, 20)
            .into_iter()
            .map(|hit| index.docs()[hit.index].id.clone())
            .collect()
    }

    #[test]
    fn tokenize_lowercases_strips_accents_and_stop_words() {
        assert_eq!(
            tokenize("The Café-Onboarding of EVA"),
            ["cafe", "onboarding", "eva"]
        );
        assert_eq!(
            tokenize("file Wide"),
            ["file", "wide"],
            "NFKD folds compatibility forms"
        );
    }

    #[test]
    fn the_last_word_matches_as_a_prefix_while_typing() {
        let index = index();
        assert_eq!(ids(&index, "onbo")[0], "4904258");
        // "job" is a whole term, so it matches exactly (not "journey");
        // matching both words (the child through its parent's title) outranks
        // matching one.
        assert_eq!(ids(&index, "job onbo"), ["4904258", "4904302", "4958950"]);
    }

    #[test]
    fn parent_titles_make_children_findable_below_the_parent() {
        assert_eq!(ids(&index(), "onboarding"), ["4904258", "4904302"]);
    }

    #[test]
    fn items_matching_more_words_rank_first() {
        assert_eq!(ids(&index(), "azure journey")[0], "4958950");
    }

    #[test]
    fn numbers_match_ids_by_prefix_and_exact_ids_rank_first() {
        let index = index();
        assert_eq!(ids(&index, "4958950")[0], "4958950");
        assert_eq!(ids(&index, "#4958950")[0], "4958950");
        assert_eq!(ids(&index, "49043"), ["4904302"]);
    }

    #[test]
    fn empty_queries_return_nothing_and_the_limit_holds() {
        let index = index();
        assert!(ids(&index, "  ").is_empty());
        assert!(ids(&index, "the of").is_empty());
        assert_eq!(index.search("task", 2).len(), 2);
    }

    #[test]
    fn ids_compare_numbers_as_numbers() {
        use std::cmp::Ordering::Less;
        assert_eq!(natural_cmp("item-9", "item-10"), Less);
        assert_eq!(natural_cmp("a", "B"), Less);
    }
}