thirdshift 0.16.5

Turn a GitHub issue into a ready-for-review pull request, unattended
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
//! The machine's Inbox, computed from local observations for every reader.

use std::cmp::Reverse;
use std::collections::BTreeMap;
use std::path::Path;

use chrono::{DateTime, FixedOffset, Local};
use serde::Serialize;

use super::{Command, Entry, Health, Observation};
use crate::github::{PrState, PullRequestState};
use crate::logs::dismiss::{self, DismissalView, DismissedRow, READY_KIND};
use crate::logs::github::{FindingSubject, Kept, RowAnswer, Subject};
use crate::logs::stop;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "lowercase")]
pub(in crate::logs) enum Group {
    #[serde(rename = "needs you")]
    NeedsYou,
    #[serde(rename = "ready for review")]
    ReadyForReview,
    Landing,
    Working,
    Done,
    Resolved,
    History,
}

impl Group {
    pub(in crate::logs) const ORDER: [Self; 7] = [
        Self::NeedsYou,
        Self::ReadyForReview,
        Self::Landing,
        Self::Working,
        Self::Done,
        Self::Resolved,
        Self::History,
    ];

    pub(in crate::logs) fn title(self) -> &'static str {
        match self {
            Self::NeedsYou => "Needs you",
            Self::ReadyForReview => "Ready for review",
            Self::Landing => "Landing",
            Self::Working => "Working",
            Self::Done => "Done",
            Self::Resolved => "Resolved",
            Self::History => "History",
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "lowercase")]
pub(in crate::logs) enum NeedsYouKind {
    Silent,
    Slow,
    Died,
    Failed,
    Triage,
    Finding,
}

impl NeedsYouKind {
    pub(in crate::logs) fn title(self) -> &'static str {
        match self {
            Self::Silent => "Silent",
            Self::Slow => "Slow",
            Self::Died => "Died",
            Self::Failed => "Failed",
            Self::Triage => "Triage",
            Self::Finding => "Finding",
        }
    }

    fn priority(self) -> u8 {
        match self {
            Self::Silent => 0,
            Self::Slow => 1,
            _ => 2,
        }
    }
}

/// Why a row left Needs you or Ready for review through the exit this
/// machine knows: a later Command starting on the same issue.
pub(in crate::logs) const TAKEN_UP_REASON: &str = "taken up by a later Command";
/// Why a waiting row left through GitHub: its pull request merged.
pub(in crate::logs) const MERGED_REASON: &str = "pull request merged";
/// Why a row left Ready for review through GitHub: its pull request closed.
pub(in crate::logs) const CLOSED_REASON: &str = "pull request closed";
pub(in crate::logs) const ISSUE_CLOSED_REASON: &str = "issue closed";

#[derive(Serialize)]
pub(in crate::logs) struct InboxItem<'a> {
    #[serde(flatten)]
    pub command: Observation<'a>,
    pub group: Group,
    pub needs_you_kind: Option<NeedsYouKind>,
    pub landing: bool,
    /// A live Command given a Stop, still ending.
    pub stopping: bool,
    pub count: usize,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub collapsed: Vec<InboxItem<'a>>,
    /// A waiting group's departure, absent for rows that never waited or aged out.
    #[serde(flatten)]
    departure: Option<Departure>,
    /// What GitHub last showed for the row's pull request: `open`,
    /// `merged` or `closed`. `None` where GitHub speaks for no row: one
    /// with no pull request, one not read yet, or one GitHub can't resolve.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub github_state: Option<String>,
    /// The pull request's merge or close time, RFC 3339, for a merged or
    /// closed row: when the row stopped waiting.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub github_at: Option<String>,
    /// The row's own or dispatched issue/PR facts, finding waits and the
    /// time each was read. History retains its own observation times.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub github: Option<RowAnswer>,
    /// Who dismissed the row, when, from where and all kinds covered.
    /// `None` while the row waits or was never dismissed for its kind.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dismissal: Option<DismissalView>,
}

#[derive(Serialize)]
struct Departure {
    left_from: String,
    left_reason: String,
    left_at: String,
}

impl Departure {
    fn line(&self) -> String {
        let Self {
            left_from,
            left_reason,
            left_at,
        } = self;
        if left_reason == TAKEN_UP_REASON {
            format!("left {left_from}: {left_reason} started at {left_at}")
        } else {
            format!("left {left_from}: {left_reason} at {left_at}")
        }
    }
}

impl InboxItem<'_> {
    pub(in crate::logs) fn cause(&self) -> Option<&str> {
        self.command.facts().cause
    }

    /// The line saying why and when the row left its waiting group, for
    /// `status` text and the Console row. `None` while it still waits or
    /// never waited.
    pub(in crate::logs) fn left_line(&self) -> Option<String> {
        self.departure.as_ref().map(Departure::line)
    }

    /// The row's state word in the Console and `status`: its Needs you kind,
    /// then Stopping, Ready or Landing, otherwise the Command's own state.
    pub(in crate::logs) fn state_word(&self) -> &str {
        if let Some(kind) = self.needs_you_kind {
            return kind.title();
        }
        if self.stopping {
            return "Stopping";
        }
        match self.group {
            Group::ReadyForReview => "Ready",
            Group::Landing => "Landing",
            _ => self.command.facts().state,
        }
    }
}

struct Facts<'a> {
    state: &'a str,
    started_at: &'a str,
    ended_at: Option<&'a str>,
    outcome: Option<&'a str>,
    cause: Option<&'a str>,
    ready: bool,
}

impl<'a> Observation<'a> {
    fn problem(self, stop: Option<&stop::Request>) -> Option<NeedsYouKind> {
        let facts = self.facts();
        if facts.state == "died" {
            Some(NeedsYouKind::Died)
        } else if matches!(self, Observation::Record(_) if facts.state == "ended" && !facts.ready && stop.is_some_and(|request| !request.requeue))
        {
            Some(NeedsYouKind::Triage)
        } else if facts
            .outcome
            .is_some_and(|outcome| outcome.contains("failed") || outcome == "review required")
            || matches!(self, Observation::Record(command) if command.record.kind == "Experiment" && matches!(facts.outcome, Some("fail" | "inconclusive" | "waiting_on_rechecks")))
        {
            Some(NeedsYouKind::Failed)
        } else if matches!(self, Observation::Record(command) if command.record.security_findings_waiting)
        {
            Some(NeedsYouKind::Finding)
        } else {
            None
        }
    }

    fn facts(self) -> Facts<'a> {
        match self {
            Self::Record(command) => Facts {
                state: command.state,
                started_at: &command.record.started_at,
                ended_at: command.record.ended_at.as_deref(),
                outcome: command.record.outcome.as_deref(),
                cause: command.record.cause.as_deref(),
                ready: command.record.outcome.as_deref() == Some("ready for review")
                    && command.record.pull_request.is_some(),
            },
            Self::History(command) => Facts {
                state: command.state,
                started_at: &command.started_at,
                ended_at: command.ended_at.as_deref(),
                outcome: command.outcome.as_deref(),
                cause: command
                    .outcome
                    .as_deref()
                    .and_then(|outcome| outcome.strip_prefix("failed: ")),
                ready: command.outcome.as_deref().is_some_and(|outcome| {
                    outcome.contains("PR https://") && outcome.ends_with("is ready for review")
                }),
            },
        }
    }
}

struct LocalFacts {
    stop: Option<stop::Request>,
    dismissals: dismiss::Captured,
}

/// Classification inputs for a row and the dispatched work that can answer it.
/// Side files are read once per owning path, even when several rows share it.
struct Captured<'a> {
    command: Observation<'a>,
    problem: Option<NeedsYouKind>,
    stopping: bool,
    dismissal: Option<DismissedRow>,
    dispatched: Vec<Captured<'a>>,
}

impl<'a> Captured<'a> {
    fn new(command: Observation<'a>, local: &mut BTreeMap<&'a Path, LocalFacts>) -> Self {
        let (request, dismissal) = match command {
            Observation::Record(record) => {
                let facts = local
                    .entry(&record.record_path)
                    .or_insert_with(|| LocalFacts {
                        stop: stop::requested(&record.record_path),
                        dismissals: dismiss::Captured::read(&record.record_path),
                    });
                (
                    facts.stop.as_ref(),
                    command
                        .selector()
                        .and_then(|row| facts.dismissals.covering(&row))
                        .cloned(),
                )
            }
            Observation::History(_) => (None, None),
        };
        let problem = command.problem(request);
        let stopping = command.facts().state == "live" && request.is_some();
        let dispatched = match command {
            Observation::Record(record) => record
                .dispatched
                .iter()
                .map(|run| Self::new(Observation::Record(run), local))
                .collect(),
            Observation::History(_) => Vec::new(),
        };
        Self {
            command,
            problem,
            stopping,
            dismissal,
            dispatched,
        }
    }

    /// A failed Pass is answered by its failed dispatched work; a Finding
    /// waits for its own private records rather than a successful fix.
    fn github_records(&self) -> Vec<&'a crate::logs::command_record::CommandRecord> {
        if self.problem == Some(NeedsYouKind::Finding) {
            return Vec::new();
        }
        let Observation::Record(command) = self.command else {
            return Vec::new();
        };
        if self.problem.is_some()
            && matches!(
                command.record.kind.as_str(),
                "Architect run" | "Security run"
            )
            && !self.dispatched.is_empty()
        {
            let failed: Vec<_> = self
                .dispatched
                .iter()
                .filter(|run| run.problem.is_some())
                .collect();
            return if failed.is_empty() {
                self.dispatched.iter().collect()
            } else {
                failed
            }
            .into_iter()
            .filter_map(|run| match run.command {
                Observation::Record(command) => Some(&command.record),
                Observation::History(_) => None,
            })
            .collect();
        }
        vec![&command.record]
    }
}

/// Owned work targets, independent of the kept answer so the adapter can apply
/// History freezes under its refresh lock without evaluating row policy again.
#[derive(Default)]
pub(in crate::logs) struct RefreshWork {
    pub subjects: Vec<Subject>,
    pub findings: Vec<FindingSubject>,
    pub freezes: Vec<Freeze>,
}

pub(in crate::logs) struct Freeze {
    pub row: String,
    pub url: String,
}

impl RefreshWork {
    pub(in crate::logs) fn has_remote_reads(&self) -> bool {
        !self.subjects.is_empty() || !self.findings.is_empty()
    }
}

struct Row<'a> {
    item: InboxItem<'a>,
    order: (Group, Option<u8>, Reverse<Option<DateTime<FixedOffset>>>),
    failure: Option<(&'a str, bool)>,
}

/// One operation's retained Inbox decisions. Projections read only this value;
/// a new local observation or kept GitHub answer requires a new evaluation.
pub(in crate::logs) struct Evaluation<'a> {
    rows: Vec<Row<'a>>,
    refresh: RefreshWork,
    next_change: Option<DateTime<Local>>,
}

impl<'a> Evaluation<'a> {
    pub(in crate::logs) fn refresh_work(&self) -> &RefreshWork {
        &self.refresh
    }

    pub(in crate::logs) fn next_change(&self) -> Option<DateTime<Local>> {
        self.next_change
    }

    /// The collapsed row containing an exact selector, with the same visible
    /// membership as the Inbox. An explicit History target remains undoable,
    /// but cannot pull visible Commands into that History row's Dismissal.
    pub(in crate::logs) fn into_target(mut self, selector: &str) -> Option<InboxItem<'a>> {
        let history = self
            .rows
            .iter()
            .find(|row| row.item.command.selector().as_deref() == Some(selector))?
            .item
            .group
            == Group::History;
        self.rows
            .retain(|row| (row.item.group == Group::History) == history);
        self.into_rows(true).into_iter().find(|item| {
            std::iter::once(item)
                .chain(&item.collapsed)
                .any(|member| member.command.selector().as_deref() == Some(selector))
        })
    }

    pub(in crate::logs) fn into_rows(self, all: bool) -> Vec<InboxItem<'a>> {
        // Filter before collapse: hidden History members cannot contribute to
        // visible counts or the Commands a Dismissal will cover.
        let mut rows: Vec<_> = self
            .rows
            .into_iter()
            .filter(|row| all || row.item.group != Group::History)
            .collect();
        rows.sort_by_key(|row| row.order);
        let mut collapsed: Vec<InboxItem<'_>> = Vec::new();
        let mut failures = BTreeMap::new();
        for row in rows {
            if let Some(key) = row.failure {
                if let Some(&index) = failures.get(&key) {
                    let first: &mut InboxItem<'_> = &mut collapsed[index];
                    first.count += 1;
                    first.collapsed.push(row.item);
                    continue;
                }
                failures.insert(key, collapsed.len());
            }
            collapsed.push(row.item);
        }
        collapsed
    }
}

pub(super) fn evaluate<'a>(
    entries: &'a [Entry],
    now: DateTime<Local>,
    days: i64,
    github: &Kept,
) -> Evaluation<'a> {
    // An accepted duration can exceed chrono's calendar. In that case every
    // representable timestamp is inside the requested retention period.
    let cutoff = now.checked_sub_signed(chrono::Duration::days(days));
    let mut local = BTreeMap::new();
    let captured: Vec<_> = entries
        .iter()
        .map(|entry| {
            Captured::new(
                match entry {
                    Entry::Record(command) => Observation::Record(command),
                    Entry::History(command) => Observation::History(command),
                },
                &mut local,
            )
        })
        .collect();
    let observations: Vec<_> = captured
        .iter()
        .flat_map(|outer| {
            std::iter::once(outer).chain(
                outer
                    .dispatched
                    .iter()
                    .filter(|run| run.command.facts().ready),
            )
        })
        .collect();
    let starts: Vec<_> = observations
        .iter()
        .map(|row| issue_starts(row.command))
        .collect();
    let starts_by_issue = all_starts(&starts);
    let mut latest = BTreeMap::new();
    for starts in &starts {
        for (issue, start) in starts {
            latest
                .entry(issue)
                .and_modify(|time: &mut DateTime<FixedOffset>| *time = (*time).max(*start))
                .or_insert(*start);
        }
    }
    let mut evaluation = Evaluation {
        rows: Vec::new(),
        refresh: RefreshWork::default(),
        next_change: None,
    };
    let mut subjects = BTreeMap::new();
    for (captured, starts) in observations.into_iter().zip(&starts) {
        let command = captured.command;
        let facts = command.facts();
        let ended = time(facts.ended_at);
        let recorded_dismissal = captured.dismissal.as_ref();
        let records = captured.github_records();
        let tree_problem = match command {
            Observation::Record(command) => live_problem(command),
            _ => None,
        };
        let live_problem = tree_problem.filter(|_| facts.state == "live");
        let urls = github_urls(command, captured.problem, tree_problem, &records);
        let finding_facts = command.selector().and_then(|row| github.findings(&row));
        let findings_waiting = matches!(command, Observation::Record(command) if command.record.security_findings_waiting)
            && finding_facts.is_none_or(|facts| facts.waiting());
        let github_settlement = github_left(captured, &records, github, ended);
        // Settling failed work removes that wait, while another finding
        // from the same audit can still need the Day shift.
        let finding_wait = github_settlement.is_some() && findings_waiting;
        let settled_at = github_settlement.as_ref().map(|(_, at)| *at);
        let mut problem = if finding_wait {
            Some(NeedsYouKind::Finding)
        } else {
            captured.problem
        };
        // Dismissing the failure does not dismiss the findings it hid.
        if problem == Some(NeedsYouKind::Failed)
            && findings_waiting
            && recorded_dismissal
                .as_ref()
                .is_some_and(|row| row.covers(NeedsYouKind::Failed.title()))
        {
            problem = Some(NeedsYouKind::Finding);
        }
        let landing = matches!(command, Observation::Record(command) if is_landing(command));
        let stopping = captured.stopping;
        // When this row stopped waiting through the later-Command exit:
        // the earliest later start on any of its issues. Only rows that
        // waited (ended problems and ready Commands) leave through it, so
        // a Command that never waited keeps going by when it ended, and a
        // row that ages out keeps going straight to History.
        let waited = problem.is_some() || facts.ready;
        let taken_up = if waited {
            superseded_at(starts, &starts_by_issue, ended)
        } else {
            None
        };
        // Age moves only rows GitHub can't speak for. An ended problem
        // with known facts stays until settlement; a ready row stays while
        // its pull request is open. A later local Command still takes up either.
        let github_moved = (taken_up.is_none() && !finding_wait)
            .then_some(github_settlement)
            .flatten();
        let github_holds = taken_up.is_none()
            && ((facts.ready
                && github_state(&command, github)
                    .is_some_and(|state| state.state == PrState::Open))
                || (problem.is_some_and(|kind| kind != NeedsYouKind::Finding)
                    && github_known(command, &records, github))
                || (problem == Some(NeedsYouKind::Finding) && finding_facts.is_some()));
        let held = (ended
            .or_else(|| time(Some(facts.started_at)))
            // A recent dismissal proves this row was still in the Inbox.
            // In particular, a long-running Command that dies has no end
            // timestamp: its old start must not hide its new Died kind.
            .into_iter()
            .chain(
                recorded_dismissal
                    .as_ref()
                    .and_then(|row| time(Some(&row.at))),
            )
            .max()
            .is_some_and(|time| cutoff.is_none_or(|cutoff| time >= cutoff))
            && !starts
                .iter()
                .any(|(issue, start)| latest.get(issue).is_some_and(|latest| latest > start)))
            || github_holds;
        // A dismissal holds for every kind it covered: a row that turns
        // into a kind it wasn't when dismissed waits again. Its kind is
        // independent of age or a later Command taking it up, so those
        // exits do not erase the dismissal or shorten its retention.
        let kind_now = live_problem
            .or(problem)
            .map(NeedsYouKind::title)
            .or(if facts.ready { Some(READY_KIND) } else { None });
        let dismissed_row =
            recorded_dismissal.filter(|row| kind_now.is_none_or(|kind| row.covers(kind)));
        let dismissed = dismissed_row.is_some();
        let dismissed_at = dismissed_row.as_ref().and_then(|row| time(Some(&row.at)));
        let dismissal = dismissed_row.as_ref().map(|row| row.view(kind_now));
        let needs_you_kind = live_problem
            .or_else(|| problem.filter(|_| held && github_moved.is_none()))
            .filter(|_| !dismissed);
        let from = if problem.is_some() {
            Some("needs you")
        } else if facts.ready {
            Some("ready for review")
        } else {
            None
        };
        let left = taken_up.filter(|_| from.is_some());
        // The later-Command exit wins where both apply. Reopening brings back only a row
        // still in Done or Resolved: History rows are not read again.
        let github_moved = github_moved.filter(|_| from.is_some());
        // A dismissal already dealt with the row. Later GitHub facts or
        // Commands may change its context, but cannot restart its retention.
        let resolved_at = if dismissed {
            dismissed_at
        } else {
            left.or_else(|| github_moved.as_ref().map(|(_, at)| *at))
                .or(ended)
        };
        let group = if needs_you_kind.is_some() {
            Group::NeedsYou
        } else if facts.ready && held && github_moved.is_none() && !dismissed {
            Group::ReadyForReview
        } else if landing {
            Group::Landing
        } else if facts.state == "live" {
            Group::Working
        } else if ended.is_some_and(|time| {
            now.checked_sub_signed(chrono::Duration::days(1))
                .is_none_or(|cutoff| time >= cutoff)
        }) {
            Group::Done
        } else if resolved_at
            .is_some_and(|at| cutoff.is_none_or(|cutoff| at.with_timezone(&Local) >= cutoff))
        {
            Group::Resolved
        } else {
            Group::History
        };
        // A row GitHub finished weeks ago goes straight to History when
        // first read, instead of filling Resolved at once.
        let state = github_state(&command, github);
        let departure = left
            .map(|at| (TAKEN_UP_REASON.to_string(), at))
            .or(github_moved);
        if facts.state != "live" {
            let recent = ended.and_then(|at| at.checked_add_signed(chrono::Duration::days(1)));
            // Keep subsequent transitions even while Done takes precedence.
            // Extra age/settlement wakes are conservative; all clocks use the
            // same captured departures and dismissal as grouping above.
            let retained = [
                ended.or_else(|| time(Some(facts.started_at))),
                taken_up,
                settled_at,
                recorded_dismissal.and_then(|row| time(Some(&row.at))),
                resolved_at,
            ]
            .into_iter()
            .flatten()
            .filter_map(|at| at.checked_add_signed(chrono::Duration::days(days)));
            let next = recent
                .into_iter()
                .chain(retained)
                // Grouping includes the cutoff; wake strictly after it.
                .filter_map(|at| at.checked_add_signed(chrono::Duration::nanoseconds(1)))
                .map(|at| at.with_timezone(&Local))
                .filter(|at| *at > now)
                .min();
            evaluation.next_change = evaluation.next_change.into_iter().chain(next).min();
        }
        if let Some(row) = command.selector() {
            for &url in &urls {
                if github.is_frozen(&row, url) {
                    continue;
                }
                if group == Group::History && github.has_answer(url) {
                    evaluation.refresh.freezes.push(Freeze {
                        row: row.clone(),
                        url: url.to_owned(),
                    });
                } else {
                    let triage_since = (captured.problem == Some(NeedsYouKind::Triage)
                        && records.iter().any(|record| {
                            record.issue.as_ref().is_some_and(|issue| issue.url == url)
                        }))
                    .then_some(ended)
                    .flatten();
                    subjects
                        .entry(url.to_owned())
                        .and_modify(|since: &mut Option<DateTime<FixedOffset>>| {
                            *since = (*since).max(triage_since);
                        })
                        .or_insert(triage_since);
                }
            }
            if let Observation::Record(command) = command
                && command.record.kind == "Security run"
                && command.record.security_findings_waiting
                && (group != Group::History || github.findings(&row).is_none())
            {
                evaluation.refresh.findings.push(FindingSubject {
                    row,
                    repository: command.record.repository.clone(),
                    urls: command
                        .record
                        .security_findings
                        .iter()
                        .map(|finding| finding.url.clone())
                        .collect(),
                });
            }
        }
        // Dismissed failures collapse together, separately from waiting ones.
        let failed = needs_you_kind == Some(NeedsYouKind::Failed)
            || dismissal
                .as_ref()
                .is_some_and(|row| row.kind == NeedsYouKind::Failed.title());
        evaluation.rows.push(Row {
            order: (
                group,
                needs_you_kind.map(NeedsYouKind::priority),
                Reverse(time(facts.ended_at.or(Some(facts.started_at)))),
            ),
            failure: facts
                .cause
                .filter(|cause| failed && !cause.is_empty())
                .map(|cause| (cause, dismissal.is_some())),
            item: InboxItem {
                command,
                group,
                needs_you_kind,
                landing,
                stopping,
                count: 1,
                collapsed: Vec::new(),
                departure: departure.map(|(reason, at)| Departure {
                    left_from: from.expect("departed rows waited").to_string(),
                    left_reason: reason,
                    left_at: at.to_rfc3339(),
                }),
                github_state: state.map(|state| state.state.to_string()),
                github_at: state.and_then(event_time).map(str::to_string),
                github: command
                    .selector()
                    .and_then(|row| github.row_answer(&row, urls)),
                dismissal,
            },
        });
    }
    evaluation.refresh.subjects = subjects
        .into_iter()
        .map(|(url, triage_since)| Subject { url, triage_since })
        .collect();
    evaluation
}

/// The row's own pull request URL, if its observation has a record with
/// one: a dispatched Ticket run's own, not its Pass's.
fn record_pull_request<'a>(observation: &Observation<'a>) -> Option<&'a str> {
    match observation {
        Observation::Record(command) => command.record.pull_request.as_deref(),
        Observation::History(_) => None,
    }
}

fn github_urls<'a>(
    observation: Observation<'a>,
    problem: Option<NeedsYouKind>,
    live_problem: Option<NeedsYouKind>,
    records: &[&'a crate::logs::command_record::CommandRecord],
) -> Vec<&'a str> {
    let mut urls = Vec::new();
    let problem = problem.is_some() || live_problem.is_some();
    if observation.facts().ready || problem {
        for record in records {
            if let Some(url) = &record.pull_request {
                urls.push(url.as_str());
            }
            if problem && let Some(issue) = &record.issue {
                urls.push(issue.url.as_str());
            }
        }
    }
    urls
}

fn github_known(
    observation: Observation<'_>,
    records: &[&crate::logs::command_record::CommandRecord],
    github: &Kept,
) -> bool {
    let Some(row) = observation.selector() else {
        return false;
    };
    records.iter().any(|record| {
        record
            .issue
            .as_ref()
            .is_some_and(|issue| github.issue(&row, &issue.url).is_some())
            || record
                .pull_request
                .as_ref()
                .is_some_and(|url| github.pull_request(&row, url).is_some())
    })
}

/// What GitHub last showed for the row's own pull request, if anything.
fn github_state<'a>(
    observation: &Observation<'a>,
    github: &'a Kept,
) -> Option<&'a PullRequestState> {
    github.pull_request(&observation.selector()?, record_pull_request(observation)?)
}

/// The pull request's merge or close time, as GitHub wrote it.
fn event_time(state: &PullRequestState) -> Option<&str> {
    state.merged_at.as_deref().or(state.closed_at.as_deref())
}

/// When an ended row stopped waiting through GitHub, with why it left.
/// A problem's issue closing or pull request merging settles it; Triage
/// also leaves on label removal. Ready rows also leave on an unmerged close.
fn github_left(
    captured: &Captured<'_>,
    records: &[&crate::logs::command_record::CommandRecord],
    github: &Kept,
    ended: Option<DateTime<FixedOffset>>,
) -> Option<(String, DateTime<FixedOffset>)> {
    let observation = captured.command;
    if observation.facts().state == "live" {
        return None;
    }
    let settled_work = github_work_left(captured, records, github, ended);
    let finding_row = captured.problem == Some(NeedsYouKind::Finding)
        || (captured.problem == Some(NeedsYouKind::Failed)
            && matches!(observation, Observation::Record(command) if command.record.security_findings_waiting
                && (settled_work.is_some() || captured.dismissal.as_ref()
                    .is_some_and(|row| row.covers(NeedsYouKind::Failed.title())
                        && !row.covers(NeedsYouKind::Finding.title())))));
    if finding_row
        && let Some(at) = observation
            .selector()
            .and_then(|row| github.findings(&row))
            .and_then(|facts| time(facts.triaged_at.as_deref()))
    {
        return Some(("findings triaged".to_string(), at));
    }
    settled_work
}

/// Settlement of the Command's own or dispatched work, independently of
/// any other findings that its Security audit left waiting.
fn github_work_left(
    captured: &Captured<'_>,
    records: &[&crate::logs::command_record::CommandRecord],
    github: &Kept,
    ended: Option<DateTime<FixedOffset>>,
) -> Option<(String, DateTime<FixedOffset>)> {
    let observation = captured.command;
    if captured.problem.is_some() {
        let row = observation.selector()?;
        let departures = records
            .iter()
            .map(|record| {
                let issue = record
                    .issue
                    .as_ref()
                    .and_then(|issue| github.issue(&row, &issue.url));
                let (reason, at) = if let Some(issue) = issue
                    && issue.closed
                {
                    (ISSUE_CLOSED_REASON, issue.closed_at.as_deref())
                } else if captured.problem == Some(NeedsYouKind::Triage)
                    && let Some(issue) = issue
                    && !issue.needs_triage
                {
                    // Absence before this Stop says nothing about whether
                    // its needs-triage label has since been removed.
                    let at = time(issue.triaged_at.as_deref())?;
                    if ended.is_some_and(|ended| at < ended) {
                        return None;
                    }
                    ("needs-triage removed", issue.triaged_at.as_deref())
                } else {
                    let pull = github.pull_request(&row, record.pull_request.as_deref()?)?;
                    if pull.state != PrState::Merged {
                        return None;
                    }
                    (MERGED_REASON, event_time(pull))
                };
                time(at)
                    .or(ended)
                    .or_else(|| time(Some(observation.facts().started_at)))
                    .map(|at| (reason.to_string(), at))
            })
            .collect::<Option<Vec<_>>>()?;
        return departures.into_iter().max_by_key(|(_, at)| *at);
    }
    if !observation.facts().ready {
        return None;
    }
    let state = github_state(&observation, github)?;
    let (reason, at) = match state.state {
        PrState::Merged => (MERGED_REASON.to_string(), event_time(state)),
        PrState::Closed => (CLOSED_REASON.to_string(), state.closed_at.as_deref()),
        PrState::Open => return None,
    };
    let at = at
        .and_then(|at| time(Some(at)))
        .or(ended)
        .or_else(|| time(Some(observation.facts().started_at)));
    at.map(|at| (reason, at))
}

/// Outer Command trees with live work anywhere in them, whatever their group:
/// a Spec run's Tickets, and the run a Pass dispatched, count within it.
pub(super) fn active_runs(entries: &[Entry]) -> usize {
    entries
        .iter()
        .filter(|entry| matches!(entry, Entry::Record(command) if active(command)))
        .count()
}

fn active(command: &Command) -> bool {
    command.state == "live"
        || command
            .dispatched
            .iter()
            .chain(&command.children)
            .any(active)
}

fn live_problem(command: &Command) -> Option<NeedsYouKind> {
    let own = match command.health {
        Some(Health::Silent { .. }) => Some(NeedsYouKind::Silent),
        Some(Health::Slow { .. }) => Some(NeedsYouKind::Slow),
        _ => None,
    };
    own.into_iter()
        .chain(
            command
                .dispatched
                .iter()
                .chain(&command.children)
                .filter_map(live_problem),
        )
        .min()
}

fn is_landing(command: &Command) -> bool {
    (command.state == "live"
        && matches!(command.record.kind.as_str(), "Run" | "Spec run")
        && command
            .stage
            .as_ref()
            .is_some_and(|stage| matches!(stage.name.as_str(), "CI watch" | "Self-merge")))
        || command
            .dispatched
            .iter()
            .chain(&command.children)
            .any(is_landing)
}

/// Every start on the machine per issue, oldest first.
fn all_starts(
    starts: &[BTreeMap<(String, u64), DateTime<FixedOffset>>],
) -> BTreeMap<(String, u64), Vec<DateTime<FixedOffset>>> {
    let mut all: BTreeMap<(String, u64), Vec<DateTime<FixedOffset>>> = BTreeMap::new();
    for starts in starts {
        for (issue, start) in starts {
            all.entry(issue.clone()).or_default().push(*start);
        }
    }
    for times in all.values_mut() {
        times.sort();
    }
    all
}

/// The earliest later start after this row began waiting. An overlapping
/// Command cannot take up its ended work or hide a subsequent start that can.
fn superseded_at(
    starts: &BTreeMap<(String, u64), DateTime<FixedOffset>>,
    all: &BTreeMap<(String, u64), Vec<DateTime<FixedOffset>>>,
    ended: Option<DateTime<FixedOffset>>,
) -> Option<DateTime<FixedOffset>> {
    starts
        .iter()
        .filter_map(|(issue, start)| {
            all.get(issue).and_then(|times| {
                times
                    .iter()
                    .find(|time| **time > *start && ended.is_none_or(|end| **time >= end))
                    .copied()
            })
        })
        .min()
}

fn issue_starts(entry: Observation<'_>) -> BTreeMap<(String, u64), DateTime<FixedOffset>> {
    let mut starts = BTreeMap::new();
    fn collect(command: &Command, starts: &mut BTreeMap<(String, u64), DateTime<FixedOffset>>) {
        let record = &command.record;
        if let Some(issue) = &record.issue
            && let Some(start) = time(Some(&record.started_at))
        {
            starts
                .entry((record.repository.to_ascii_lowercase(), issue.number))
                .and_modify(|latest| *latest = (*latest).max(start))
                .or_insert(start);
        }
        for child in command.dispatched.iter().chain(&command.children) {
            collect(child, starts);
        }
    }
    match entry {
        Observation::Record(command) => collect(command, &mut starts),
        Observation::History(command) => {
            if let Some(issue) = &command.issue
                && let Some(start) = time(Some(&command.started_at))
            {
                starts.insert(
                    (command.repository.to_ascii_lowercase(), issue.number),
                    start,
                );
            }
        }
    }
    starts
}

fn time(value: Option<&str>) -> Option<DateTime<FixedOffset>> {
    value.and_then(|value| DateTime::parse_from_rfc3339(value).ok())
}