artifactize-core 0.25.2

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
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
//! The review list's request search: a query of free text and `key:value` terms, matched
//! against saved requests, completed from the requests shown, and read page by page from the
//! state. Every review frontend (the terminal `review` and the desktop app) uses this one rule.
//!
//! Terms are ANDed. A plain word matches, case-insensitively, a substring of the repository,
//! eval, request ID or claimant. A `key:value` term restricts the match to one field:
//!
//! | Key | Matches |
//! |---|---|
//! | `repo:` | a substring of the repository path |
//! | `eval:` | a substring of the eval ID |
//! | `request:` | a substring of the request ID |
//! | `reviewer:` | a substring of the claimant; unclaimed requests never match |
//! | `tag:` | a substring of a tag of the eval's Artifact |
//! | `kind:`, `type:` | the eval kind exactly: `runtime`, `agent` or `human` |
//! | `status:` | the request status; `waiting_human` includes `deferred` sign-offs |
//! | `is:` | `mine`, `claimed` or `unclaimed` (claims exist only on waiting Human requests) |
//!
//! The bare term `waiting` matches unclaimed waiting Human requests, including deferred
//! sign-offs. It is the short spelling of `is:unclaimed`.
//!
//! Values with spaces are quoted: `repo:"/work/my repo"`. A quoted word is always free text, so
//! `"type:style"` searches for that text rather than naming a key.
#[cfg(test)]
mod tests;

use crate::{
    config::ProfileKind,
    monitor::live::{Label, Labels},
    store::{self, RequestView},
    types::{RequestId, RequestStatus, ReviewerId},
};
use clap::ValueEnum;
use std::{
    collections::BTreeSet,
    ops::ControlFlow,
    path::{Path, PathBuf},
};

/// The query the review list starts with: the waiting Human sign-offs.
pub const DEFAULT: &str = "type:human waiting";
const WAITING: &str = "waiting";

/// Requests read from the state per batch while a page is filled.
const BATCH: u32 = 400;
/// Matching requests per page, as the monitor pages its Runs.
pub const PAGE: usize = 100;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TextField {
    Repo,
    Branch,
    Eval,
    Request,
    Reviewer,
    Tag,
}

impl TextField {
    fn name(self) -> &'static str {
        match self {
            Self::Repo => "repo",
            Self::Branch => "branch",
            Self::Eval => "eval",
            Self::Request => "request",
            Self::Reviewer => "reviewer",
            Self::Tag => "tag",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Key {
    Text(TextField),
    Kind,
    Type,
    Status,
    Is,
}

impl Key {
    const ALL: [Self; 10] = [
        Self::Text(TextField::Repo),
        Self::Text(TextField::Branch),
        Self::Text(TextField::Eval),
        Self::Text(TextField::Request),
        Self::Text(TextField::Reviewer),
        Self::Text(TextField::Tag),
        Self::Kind,
        Self::Type,
        Self::Status,
        Self::Is,
    ];

    fn name(self) -> &'static str {
        match self {
            Self::Text(field) => field.name(),
            Self::Kind => "kind",
            Self::Type => "type",
            Self::Status => "status",
            Self::Is => "is",
        }
    }

    fn parse(name: &str) -> Option<Self> {
        Self::ALL
            .into_iter()
            .find(|key| key.name().eq_ignore_ascii_case(name))
    }
}

/// A claim fact of a request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Is {
    Mine,
    Claimed,
    Unclaimed,
}

impl Is {
    const ALL: [Self; 3] = [Self::Mine, Self::Claimed, Self::Unclaimed];

    fn name(self) -> &'static str {
        match self {
            Self::Mine => "mine",
            Self::Claimed => "claimed",
            Self::Unclaimed => "unclaimed",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Term {
    /// Lowercased text found in any listed column.
    Text(String),
    /// Lowercased text found in one field.
    Field(TextField, String),
    Kind(ProfileKind),
    Status(RequestStatus),
    Is(Is),
}

/// A parsed query; every term must match.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Query {
    terms: Vec<Term>,
    places: bool,
    live: bool,
}

/// Who holds a request's claim, as the current reviewer sees it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Claim {
    Unclaimed,
    /// Claimed by the current reviewer, named.
    Mine(String),
    /// Claimed by another reviewer, named.
    Other(String),
}

impl Claim {
    /// The claimant's name, whoever it is.
    pub fn reviewer(&self) -> Option<&str> {
        match self {
            Self::Unclaimed => None,
            Self::Mine(name) | Self::Other(name) => Some(name),
        }
    }
}

/// The fields of one request that a query matches.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
    pub repository: PathBuf,
    pub label: Option<Label>,
    pub eval: String,
    pub request: String,
    pub claim: Claim,
    pub kind: ProfileKind,
    pub status: RequestStatus,
    pub tags: Vec<String>,
}

impl Entry {
    /// The searchable facts of `view` from repository `repo`, for the current `reviewer`.
    pub fn new(view: &RequestView, repo: &Path, reviewer: &ReviewerId) -> Self {
        let request = &view.request;
        let tags = view
            .definition
            .as_ref()
            .and_then(|definition| definition.artifact().tags.value())
            .or_else(|| {
                let artifacts = request.human_definition.as_ref()?.artifacts.value()?;
                artifacts.get(&request.target)?.tags.value()
            })
            .cloned()
            .unwrap_or_default();
        Self {
            repository: repo.to_owned(),
            label: None,
            eval: request.eval_id.to_string(),
            request: request.id.to_string(),
            claim: match &view.claim {
                None => Claim::Unclaimed,
                Some(claim) if &claim.reviewer == reviewer => {
                    Claim::Mine(claim.reviewer.to_string())
                }
                Some(claim) => Claim::Other(claim.reviewer.to_string()),
            },
            kind: request.profile.kind(),
            status: request.status(),
            tags,
        }
    }
}

fn kind_name(kind: ProfileKind) -> &'static str {
    kind.name()
}

fn status_name(status: RequestStatus) -> String {
    status.as_str().to_ascii_lowercase()
}

/// One word of a query: where it starts in the text, its text without quotes, and whether it
/// began quoted.
struct Word {
    start: usize,
    text: String,
    quoted: bool,
}

/// Words of `text`, a double-quoted run counting as one word, and whether the text ends inside
/// a word (no whitespace after it outside quotes): the term still being typed.
fn scan(text: &str) -> (Vec<Word>, bool) {
    let mut words = Vec::new();
    let mut word: Option<Word> = None;
    let mut quoted = false;
    for (index, character) in text.char_indices() {
        if character.is_whitespace() && !quoted {
            words.extend(word.take());
            continue;
        }
        let current = word.get_or_insert_with(|| Word {
            start: index,
            text: String::new(),
            quoted: character == '"',
        });
        if character == '"' {
            quoted = !quoted;
        } else {
            current.text.push(character);
        }
    }
    let open = word.is_some();
    words.extend(word);
    (words, open)
}

/// Words of `text` and whether each began quoted.
pub(crate) fn words(text: &str) -> Vec<(String, bool)> {
    scan(text)
        .0
        .into_iter()
        .map(|word| (word.text, word.quoted))
        .collect()
}

impl std::str::FromStr for Query {
    type Err = String;

    fn from_str(text: &str) -> Result<Self, String> {
        let terms = words(text)
            .into_iter()
            .map(|(word, quoted)| {
                if !quoted && word.eq_ignore_ascii_case(WAITING) {
                    return Ok(Term::Is(Is::Unclaimed));
                }
                let Some((name, value)) = word.split_once(':').filter(|_| !quoted) else {
                    return Ok(Term::Text(word.to_lowercase()));
                };
                let key = Key::parse(name).ok_or_else(|| {
                    format!(
                        "Unknown filter key `{name}:`; use {}, or quote the word to search for it.",
                        keys()
                    )
                })?;
                if value.is_empty() {
                    return Err(format!("`{}:` needs a value.", key.name()));
                }
                let lower = value.to_lowercase();
                match key {
                    Key::Kind | Key::Type => ProfileKind::value_variants()
                        .iter()
                        .copied()
                        .find(|kind| kind_name(*kind) == lower)
                        .map(Term::Kind)
                        .ok_or_else(|| {
                            format!("Unknown kind `{value}`; use {}.", list(kind_values()))
                        }),
                    Key::Status => RequestStatus::ALL
                        .iter()
                        .copied()
                        .find(|status| status_name(*status) == lower)
                        .map(Term::Status)
                        .ok_or_else(|| {
                            format!("Unknown status `{value}`; use {}.", list(status_values()))
                        }),
                    Key::Is => Is::ALL
                        .into_iter()
                        .find(|is| is.name() == lower)
                        .map(Term::Is)
                        .ok_or_else(|| {
                            format!(
                                "Unknown qualifier `is:{value}`; use {}.",
                                list(Is::ALL.iter().map(|is| format!("is:{}", is.name())))
                            )
                        }),
                    Key::Text(field) => Ok(Term::Field(field, lower)),
                }
            })
            .collect::<Result<_, String>>()?;
        Ok(Self {
            terms,
            places: false,
            live: false,
        })
    }
}

fn list(items: impl IntoIterator<Item = String>) -> String {
    let items: Vec<String> = items.into_iter().collect();
    match items.split_last() {
        Some((last, rest)) if !rest.is_empty() => format!("{} or {last}", rest.join(", ")),
        _ => items.join(""),
    }
}

fn keys() -> String {
    list(Key::ALL.iter().map(|key| format!("{}:", key.name())))
}

fn kind_values() -> impl Iterator<Item = String> {
    ProfileKind::value_variants()
        .iter()
        .map(|kind| kind_name(*kind).to_owned())
}

fn status_values() -> impl Iterator<Item = String> {
    RequestStatus::ALL.iter().map(|status| status_name(*status))
}

fn contains(field: &str, text: &str) -> bool {
    field.to_lowercase().contains(text)
}

impl Query {
    /// Search the displayed projects and branches, instead of raw checkout paths.
    pub fn for_places(mut self) -> Self {
        self.places = true;
        self
    }

    /// Hide requests whose workspace is gone, without hiding the record from CLI history.
    pub fn for_live_repositories(mut self) -> Self {
        self.live = true;
        self
    }

    /// The query of `--repo PATH`: the default with that repository's term.
    pub fn text_for(repo: Option<&Path>) -> String {
        match repo {
            Some(repo) => format!(
                "{DEFAULT} {}",
                quote("repo", &crate::platform::path_text(repo))
            ),
            None => DEFAULT.to_owned(),
        }
    }

    /// Whether only waiting Human reviews can match, as with the default query.
    pub fn waiting_human(&self) -> bool {
        self.terms.contains(&Term::Kind(ProfileKind::Human))
            && self.terms.iter().any(|term| {
                matches!(
                    term,
                    Term::Status(RequestStatus::WaitingHuman | RequestStatus::Deferred)
                        | Term::Is(_)
                )
            })
    }

    pub fn matches(&self, entry: &Entry) -> bool {
        let repository = entry.label.as_ref().map_or_else(
            || crate::platform::path_text(&entry.repository),
            |label| label.project.clone(),
        );
        self.terms.iter().all(|term| match term {
            Term::Text(text) => [
                Some(repository.as_str()),
                entry.label.as_ref().map(|label| label.branch.as_str()),
                Some(entry.eval.as_str()),
                Some(entry.request.as_str()),
                entry.claim.reviewer(),
            ]
            .into_iter()
            .flatten()
            .any(|field| contains(field, text)),
            Term::Field(TextField::Repo, text) => contains(&repository, text),
            Term::Field(TextField::Branch, text) => entry
                .label
                .as_ref()
                .is_some_and(|label| contains(&label.branch, text)),
            Term::Field(TextField::Eval, text) => contains(&entry.eval, text),
            Term::Field(TextField::Request, text) => contains(&entry.request, text),
            Term::Field(TextField::Reviewer, text) => entry
                .claim
                .reviewer()
                .is_some_and(|reviewer| contains(reviewer, text)),
            Term::Field(TextField::Tag, text) => entry.tags.iter().any(|tag| contains(tag, text)),
            Term::Kind(kind) => entry.kind == *kind,
            Term::Status(RequestStatus::WaitingHuman) => entry.status.is_human_wait(),
            Term::Status(status) => entry.status == *status,
            Term::Is(Is::Mine) => matches!(entry.claim, Claim::Mine(_)),
            Term::Is(Is::Claimed) => entry.claim != Claim::Unclaimed,
            Term::Is(Is::Unclaimed) => {
                entry.status.is_human_wait() && entry.claim == Claim::Unclaimed
            }
        })
    }

    /// What the state read shows besides the requests: their Runs' definitions only for a
    /// `tag:` term, the one term matched against them. They hold a Run's whole graph, which
    /// for a large Run costs seconds and gigabytes to decode for each batch.
    fn shown(&self) -> store::Shown {
        if self
            .terms
            .iter()
            .any(|term| matches!(term, Term::Field(TextField::Tag, _)))
        {
            store::Shown::Definitions
        } else {
            store::Shown::Requests
        }
    }

    fn wants_labels(&self) -> bool {
        self.places
            || self
                .terms
                .iter()
                .any(|term| matches!(term, Term::Field(TextField::Branch, _)))
    }

    /// The statuses every match must have, for the state read to select up front.
    fn statuses(&self) -> Vec<RequestStatus> {
        let statuses: BTreeSet<_> = self
            .terms
            .iter()
            .filter_map(|term| match term {
                Term::Status(status) => Some(*status),
                // Claims exist only on waiting Human requests.
                Term::Is(Is::Mine | Is::Claimed | Is::Unclaimed) => {
                    Some(RequestStatus::WaitingHuman)
                }
                _ => None,
            })
            .collect();
        match statuses.len() {
            // Two different required statuses match nothing; reading one keeps it bounded.
            0 | 1 => statuses
                .into_iter()
                .flat_map(|status| {
                    if status == RequestStatus::WaitingHuman {
                        vec![status, RequestStatus::Deferred]
                    } else {
                        vec![status]
                    }
                })
                .collect(),
            _ => vec![*statuses.first().expect("two statuses")],
        }
    }
}

/// `key:value`, quoted when the value has spaces or quotes.
pub(crate) fn quote(key: &str, value: &str) -> String {
    if value
        .chars()
        .any(|character| character.is_whitespace() || character == '"')
    {
        format!("{key}:\"{}\"", value.replace('"', ""))
    } else {
        format!("{key}:{value}")
    }
}

/// Completion of the query's last term.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Completion {
    /// The query before the completed term; a candidate replaces everything after it.
    pub keep: String,
    /// Whole terms, keys first (`repo:`) or `key:value` once a key is typed.
    pub candidates: Vec<String>,
}

/// `text` split before the term still being typed, as the parser reads words: a space inside
/// quotes (`repo:"my r`) stays in the term; after a finished word the term is empty.
pub(crate) fn last_term(text: &str) -> (&str, &str) {
    match scan(text) {
        (words, true) => text.split_at(words.last().map_or(0, |word| word.start)),
        (_, false) => (text, ""),
    }
}

/// The candidates for the query's last term: keys whose names start with it, or after a key
/// the values of that field among `entries` (fixed vocabularies for `kind:`, `status:`, `is:`).
pub fn complete(text: &str, entries: &[Entry]) -> Completion {
    let (keep, term) = last_term(text);
    // A quoted word is searched for literally: nothing to complete.
    let candidates = match term.split_once(':').filter(|_| !term.starts_with('"')) {
        None if term.starts_with('"') => Vec::new(),
        None => {
            let lower = term.to_lowercase();
            Key::ALL
                .iter()
                .map(|key| format!("{}:", key.name()))
                .chain(std::iter::once(WAITING.to_owned()))
                .filter(|key| key.starts_with(&lower))
                .collect()
        }
        Some((name, partial)) => match Key::parse(name) {
            None => Vec::new(),
            Some(key) => {
                let partial = partial.trim_matches('"').to_lowercase();
                let values: BTreeSet<String> = match key {
                    Key::Text(TextField::Repo) => entries
                        .iter()
                        .map(|entry| {
                            if let Some(label) = &entry.label {
                                return label.project.clone();
                            }
                            entry.repository.file_name().map_or_else(
                                || crate::platform::path_text(&entry.repository),
                                crate::platform::display_name,
                            )
                        })
                        .collect(),
                    Key::Text(TextField::Branch) => entries
                        .iter()
                        .filter_map(|entry| entry.label.as_ref())
                        .filter(|label| !label.branch.is_empty())
                        .map(|label| label.branch.clone())
                        .collect(),
                    Key::Text(TextField::Eval) => {
                        entries.iter().map(|entry| entry.eval.clone()).collect()
                    }
                    Key::Text(TextField::Request) => {
                        entries.iter().map(|entry| entry.request.clone()).collect()
                    }
                    Key::Text(TextField::Reviewer) => entries
                        .iter()
                        .filter_map(|entry| entry.claim.reviewer().map(str::to_owned))
                        .collect(),
                    Key::Text(TextField::Tag) => entries
                        .iter()
                        .flat_map(|entry| entry.tags.iter().cloned())
                        .collect(),
                    Key::Kind | Key::Type => kind_values().collect(),
                    Key::Status => status_values().collect(),
                    Key::Is => Is::ALL.iter().map(|is| is.name().to_owned()).collect(),
                };
                values
                    .into_iter()
                    .filter(|value| value.to_lowercase().contains(&partial))
                    .map(|value| quote(key.name(), &value))
                    .collect()
            }
        },
    };
    Completion {
        keep: keep.to_owned(),
        candidates,
    }
}

/// Complete the last term of `text` from the requests the terms before it find (their first
/// page): what the rest of the query lists, never the half-typed term itself.
pub async fn suggest(
    state: &Path,
    text: &str,
    reviewer: &ReviewerId,
) -> Result<Completion, String> {
    suggest_in(state, text, reviewer, false).await
}

pub async fn suggest_places(
    state: &Path,
    text: &str,
    reviewer: &ReviewerId,
) -> Result<Completion, String> {
    suggest_in(state, text, reviewer, true).await
}

async fn suggest_in(
    state: &Path,
    text: &str,
    reviewer: &ReviewerId,
    places: bool,
) -> Result<Completion, String> {
    let (keep, term) = last_term(text);
    let key = term
        .split_once(':')
        .filter(|_| !term.starts_with('"'))
        .and_then(|(name, _)| Key::parse(name));
    let from_requests = matches!(key, Some(Key::Text(_)));
    let shown = match key {
        Some(Key::Text(TextField::Tag)) => store::Shown::Definitions,
        _ => keep
            .parse::<Query>()
            .map_or(store::Shown::Requests, |query| query.shown()),
    };
    let entries = match keep.parse::<Query>() {
        // Tags are completed from the definitions the matches' Runs saved.
        Ok(mut query) if from_requests => {
            query.places = places || matches!(key, Some(Key::Text(TextField::Branch)));
            query.live = places;
            page(state, &query, reviewer, 0, shown)
                .await?
                .requests
                .into_iter()
                .map(|(_, entry)| entry)
                .collect()
        }
        _ => Vec::new(),
    };
    Ok(complete(text, &entries))
}

/// One page of matching requests, newest Run first, and where the next page starts.
#[derive(Debug, Clone, Default)]
pub struct Found {
    pub requests: Vec<(RequestView, Entry)>,
    /// The position of the next page in the state's request order; `None` on the last page.
    pub next: Option<u64>,
}

/// The exact saved request and whether it appears in the current search, regardless of page.
#[derive(Debug, Clone)]
pub struct Opened {
    pub view: RequestView,
    pub outside_reason: Option<String>,
}

impl Opened {
    pub fn outside_filter(&self) -> bool {
        self.outside_reason.is_some()
    }
}

/// Resolve a direct reference without changing its query or redirecting to an original request.
pub async fn open(
    state: &Path,
    id: &RequestId,
    query: &Query,
    reviewer: &ReviewerId,
) -> Result<Opened, String> {
    let view = store::read_request_by_id(state, id, query.shown()).await?;
    let repo = store::read_request_repo_by_id(state, id.clone()).await?;
    let mut entry = Entry::new(&view, &repo, reviewer);
    if query.wants_labels() {
        entry.label = labels(state, vec![view.request.run_id.clone()])
            .await?
            .remove(&view.request.run_id);
    }
    let outside_reason = if query.live && !repo.is_dir() {
        Some("This review's repository folder no longer exists.".into())
    } else if Box::pin(follows_waiting(state, &view, query.live)).await {
        Some("This review shares the original request's sign-off.".into())
    } else if query.matches(&entry) {
        None
    } else {
        Some(outside_reason(&entry))
    };
    Ok(Opened {
        view,
        outside_reason,
    })
}

fn outside_reason(entry: &Entry) -> String {
    if entry.kind != ProfileKind::Human {
        return "This is not a Human review.".into();
    }
    match (&entry.claim, entry.status) {
        (Claim::Mine(_), _) => "Claimed by you.".into(),
        (Claim::Other(reviewer), _) => format!("Claimed by {reviewer}."),
        (_, RequestStatus::Green | RequestStatus::Red) => "This review was submitted.".into(),
        _ => "This review does not match the current filter.".into(),
    }
}

/// Up to [`PAGE`] requests matching `query` from position `start` on, read in bounded batches.
///
/// A waiting Human request that joined another waiting request's execution (a follower) is
/// listed once, as the original that records its definition and receives every action; a
/// follower whose original no longer waits is listed as itself. The first page first ends the
/// sign-offs whose repository folder is gone ([`store::retire_removed_signoffs`]): nothing can
/// be reviewed in them any more.
pub async fn search(
    state: &Path,
    query: &Query,
    reviewer: &ReviewerId,
    start: u64,
) -> Result<Found, String> {
    if start == 0 {
        store::retire_removed_signoffs(state).await?;
    }
    page(state, query, reviewer, start, query.shown()).await
}

/// One [`search`] page, its views showing what `shown` names besides their requests.
async fn page(
    state: &Path,
    query: &Query,
    reviewer: &ReviewerId,
    start: u64,
    shown: store::Shown,
) -> Result<Found, String> {
    let mut found = Found::default();
    read_matching(
        state,
        query,
        reviewer,
        start,
        shown,
        |view, entry, position| {
            found.requests.push((view, entry));
            if found.requests.len() < PAGE {
                return ControlFlow::Continue(());
            }
            found.next = Some(position + 1);
            ControlFlow::Break(())
        },
    )
    .await?;
    Ok(found)
}

/// How many requests `query` finds on all its pages together, each counted as [`search`]
/// lists it.
pub async fn count(state: &Path, query: &Query, reviewer: &ReviewerId) -> Result<u64, String> {
    let mut count = 0;
    read_matching(state, query, reviewer, 0, query.shown(), |_, _, _| {
        count += 1;
        ControlFlow::Continue(())
    })
    .await?;
    Ok(count)
}

/// Pass each request matching `query` from position `start` on to `found` with its position,
/// in the state's request order, until `found` breaks or the requests end. Each view shows what
/// `shown` names besides its request.
async fn read_matching(
    state: &Path,
    query: &Query,
    reviewer: &ReviewerId,
    start: u64,
    shown: store::Shown,
    mut found: impl FnMut(RequestView, Entry, u64) -> ControlFlow<()>,
) -> Result<(), String> {
    let statuses = query.statuses();
    let mut position = start;
    let mut exists = std::collections::BTreeMap::new();
    loop {
        let batch = if query.live {
            store::read_review_batch(state, &statuses, position, BATCH, shown).await?
        } else {
            store::read_request_batch(state, &statuses, position, BATCH, shown).await?
        };
        let read = batch.len() as u64;
        let labels = if query.wants_labels() {
            let ids: BTreeSet<_> = batch
                .iter()
                .map(|(view, _)| view.request.run_id.clone())
                .collect();
            labels(state, ids.into_iter().collect()).await?
        } else {
            Labels::new()
        };
        for (offset, (view, repo)) in batch.into_iter().enumerate() {
            if (query.live && !*exists.entry(repo.clone()).or_insert_with(|| repo.is_dir()))
                || Box::pin(follows_waiting(state, &view, query.live)).await
            {
                continue;
            }
            let mut entry = Entry::new(&view, &repo, reviewer);
            entry.label = labels.get(&view.request.run_id).cloned();
            if query.matches(&entry) && found(view, entry, position + offset as u64).is_break() {
                return Ok(());
            }
        }
        if read < u64::from(BATCH) {
            return Ok(());
        }
        position += read;
    }
}

async fn labels(state: &Path, ids: Vec<crate::types::RunId>) -> Result<Labels, String> {
    let catalog = store::read_run_places(state, ids).await?;
    crate::task::joined(
        tokio::task::spawn_blocking(move || {
            let mut legacy = std::collections::BTreeMap::new();
            catalog
                .into_iter()
                .map(|(id, run)| {
                    let label = if run.repository.common_dir.is_some() {
                        Label::for_identity(&run.repo_path, &run.repository)
                    } else {
                        legacy
                            .entry(run.repo_path.clone())
                            .or_insert_with(|| Label::at(&run.repo_path))
                            .clone()
                    };
                    (id, label)
                })
                .collect()
        })
        .await,
    )
    .map_err(|error| error.to_string())
}

/// A waiting follower whose original still waits: the original's own row lists it.
async fn follows_waiting(state: &Path, view: &RequestView, live: bool) -> bool {
    if !view.request.status().is_human_wait() {
        return false;
    }
    let Some(source) = view
        .execution
        .as_ref()
        .map(|execution| &execution.provenance.request_id)
        .filter(|source| **source != view.request.id)
    else {
        return false;
    };
    // Only the original's status matters, never what its Run's definitions hold.
    if !store::read_request_by_id(state, source, store::Shown::Requests)
        .await
        .is_ok_and(|original| original.request.status().is_human_wait())
    {
        return false;
    }
    !live
        || store::read_request_repo_by_id(state, source.clone())
            .await
            .is_ok_and(|repo| repo.is_dir())
}