truth-mirror 0.13.1

Truthfulness gate and adversarial reviewer harness for AI coding agents.
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
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
//! Application state machine and event loop for the control-panel TUI.

use anyhow::Result;
use crossterm::{
    cursor,
    event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::{Terminal, backend::CrosstermBackend};
use std::{
    collections::BTreeMap,
    io,
    path::{Path, PathBuf},
    process::ExitCode,
    sync::mpsc,
    time::{Duration, Instant},
};

use crate::cli::TuiArgs;

use super::{
    actions::{self, DrainEvent},
    data::{self, Snapshot},
    model::{ViewId, finding_cards, validate_fix_sha, waiver_template},
    ui,
};

const REFRESH_INTERVAL: Duration = Duration::from_secs(2);
const POLL: Duration = Duration::from_millis(80);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FocusPane {
    Nav,
    Main,
    Detail,
    Log,
}

#[derive(Clone, Debug)]
pub enum Modal {
    Help,
    Confirm {
        title: String,
        body: String,
        action: ConfirmAction,
    },
    TextPrompt {
        title: String,
        hint: String,
        buffer: String,
        kind: TextKind,
    },
    Multiline {
        title: String,
        hint: String,
        buffer: String,
        kind: MultiKind,
    },
    DiffPreview {
        title: String,
        diff: String,
        path: String,
        value: String,
    },
}

#[derive(Clone, Debug)]
pub enum ConfirmAction {
    RemoveQueue { run_id: String, sha: String },
    Quit,
}

#[derive(Clone, Debug)]
pub enum TextKind {
    Petition { rejection_sha: String },
    ConfigEdit { path: String },
}

#[derive(Clone, Debug)]
pub enum MultiKind {
    Waiver { sha: String },
}

pub struct App {
    pub state_dir: PathBuf,
    pub config_path: PathBuf,
    pub version: String,
    pub auto_refresh: bool,
    pub view: ViewId,
    pub focus: FocusPane,
    pub snapshot: Snapshot,
    pub last_refresh: Instant,
    pub status: String,
    pub modal: Option<Modal>,
    pub queue_sel: usize,
    pub ledger_sel: usize,
    pub config_sel: usize,
    pub debt_sel: usize,
    pub debt_entry_sel: usize,
    pub ledger_detail: bool,
    pub finding_sel: usize,
    pub scroll: u16,
    pub drain_log: Vec<String>,
    pub drain_running: bool,
    drain_rx: Option<mpsc::Receiver<DrainEvent>>,
    _drain_handle: Option<std::thread::JoinHandle<()>>,
    pub should_quit: bool,
    /// Commit subject cache, keyed by SHA, reused across refreshes. A
    /// commit's subject is immutable, so nothing ever needs to invalidate an
    /// entry. Ported from upstream 419e2eb (stop re-shelling to `git log`
    /// every refresh tick).
    subject_cache: BTreeMap<String, String>,
}

impl App {
    pub fn new(
        state_dir: PathBuf,
        config_path: PathBuf,
        version: String,
        auto_refresh: bool,
    ) -> Result<Self> {
        let mut subject_cache = BTreeMap::new();
        let snapshot = data::load_snapshot(&state_dir, &config_path, &version, &mut subject_cache)?;
        let warnings = if snapshot.warnings.is_empty() {
            "ready".to_owned()
        } else {
            snapshot.warnings.join("; ")
        };
        Ok(Self {
            state_dir,
            config_path,
            version,
            auto_refresh,
            view: ViewId::Dashboard,
            focus: FocusPane::Main,
            snapshot,
            last_refresh: Instant::now(),
            status: warnings,
            modal: None,
            queue_sel: 0,
            ledger_sel: 0,
            config_sel: 0,
            debt_sel: 0,
            debt_entry_sel: 0,
            ledger_detail: false,
            finding_sel: 0,
            scroll: 0,
            drain_log: Vec::new(),
            drain_running: false,
            drain_rx: None,
            _drain_handle: None,
            should_quit: false,
            subject_cache,
        })
    }

    pub fn refresh(&mut self) {
        match data::load_snapshot(
            &self.state_dir,
            &self.config_path,
            &self.version,
            &mut self.subject_cache,
        ) {
            Ok(snapshot) => {
                self.clamp_selections(&snapshot);
                self.snapshot = snapshot;
                self.last_refresh = Instant::now();
            }
            Err(error) => {
                self.status = format!("refresh failed: {error}");
            }
        }
    }

    fn clamp_selections(&mut self, snapshot: &Snapshot) {
        if self.queue_sel >= snapshot.queue.len() {
            self.queue_sel = snapshot.queue.len().saturating_sub(1);
        }
        if self.ledger_sel >= snapshot.ledger.len() {
            self.ledger_sel = snapshot.ledger.len().saturating_sub(1);
        }
        if self.config_sel >= snapshot.config_rows.len() {
            self.config_sel = snapshot.config_rows.len().saturating_sub(1);
        }
        if self.debt_sel >= snapshot.debt.len() {
            self.debt_sel = snapshot.debt.len().saturating_sub(1);
        }
        if let Some(group) = snapshot.debt.get(self.debt_sel) {
            if self.debt_entry_sel >= group.entries.len() {
                self.debt_entry_sel = group.entries.len().saturating_sub(1);
            }
        } else {
            self.debt_entry_sel = 0;
        }
    }

    pub fn tick_drain(&mut self) {
        let Some(rx) = self.drain_rx.take() else {
            return;
        };
        let mut done = false;
        let mut exit_code = 0;
        let mut disconnected = false;
        loop {
            match rx.try_recv() {
                Ok(DrainEvent::Line(line)) => {
                    self.drain_log.push(line);
                    if self.drain_log.len() > 400 {
                        let drain = self.drain_log.len() - 400;
                        self.drain_log.drain(0..drain);
                    }
                }
                Ok(DrainEvent::Done(code)) => {
                    // Ported from upstream 88d1ecd: a normal drain worker
                    // sends `Done` and then drops its sender as its very next
                    // act. Without this `break`, the loop's next iteration
                    // calls `try_recv()` again on that now-disconnected
                    // channel — a SUCCESSFUL drain then fell into the
                    // `Disconnected` arm below, which overrode `done` with
                    // "unexpectedly disconnected" and discarded the real exit
                    // code. `Done` is authoritative the instant it arrives;
                    // nothing sent after it changes the outcome.
                    done = true;
                    exit_code = code;
                    break;
                }
                Err(mpsc::TryRecvError::Empty) => break,
                Err(mpsc::TryRecvError::Disconnected) => {
                    // Ported from upstream 419e2eb: if the drain worker
                    // thread dies without ever sending a `Done` (panic, or its
                    // sender simply dropped), the old `while let Ok` loop
                    // fell through with `done` still false — the receiver was
                    // restored and polled again next tick. `try_recv` on a
                    // disconnected channel never blocks, so every tick
                    // immediately saw `Disconnected` again: a 100% CPU busy
                    // loop with no progress. Treat disconnection as done
                    // instead of restoring the receiver — but only when
                    // `Done` was never observed (see the `break` above).
                    disconnected = true;
                    break;
                }
            }
        }
        if disconnected {
            self.drain_running = false;
            self.status = "drain worker disconnected unexpectedly (no exit code reported)".into();
            self.refresh();
            self.drain_rx = None;
        } else if done {
            self.drain_running = false;
            self.status = format!("drain finished (exit {exit_code})");
            self.refresh();
            self.drain_rx = None;
        } else {
            self.drain_rx = Some(rx);
        }
    }

    pub fn on_key(&mut self, key: KeyEvent) {
        if key.kind != KeyEventKind::Press {
            return;
        }

        if let Some(modal) = self.modal.clone() {
            self.handle_modal_key(modal, key);
            return;
        }

        match key.code {
            KeyCode::Char('q') if key.modifiers.is_empty() => {
                self.modal = Some(Modal::Confirm {
                    title: "Quit".into(),
                    body: "Exit the control panel?".into(),
                    action: ConfirmAction::Quit,
                });
            }
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.should_quit = true;
            }
            KeyCode::Char('?') => {
                self.modal = Some(Modal::Help);
            }
            KeyCode::Char(c) if c.is_ascii_digit() => {
                if let Some(view) = ViewId::from_digit(c) {
                    self.view = view;
                    self.ledger_detail = false;
                    self.focus = FocusPane::Main;
                    self.scroll = 0;
                }
            }
            KeyCode::Tab => self.cycle_focus(true),
            KeyCode::BackTab => self.cycle_focus(false),
            KeyCode::Char('r') if key.modifiers.is_empty() => {
                self.refresh();
                self.status = "refreshed".into();
            }
            other => self.handle_view_key(other, key.modifiers),
        }
    }

    fn cycle_focus(&mut self, forward: bool) {
        let order = match self.view {
            ViewId::Queue => vec![FocusPane::Nav, FocusPane::Main, FocusPane::Log],
            ViewId::Ledger if self.ledger_detail => {
                vec![FocusPane::Nav, FocusPane::Main, FocusPane::Detail]
            }
            _ => vec![FocusPane::Nav, FocusPane::Main],
        };
        let idx = order.iter().position(|p| *p == self.focus).unwrap_or(0);
        self.focus = if forward {
            order[(idx + 1) % order.len()]
        } else {
            order[(idx + order.len() - 1) % order.len()]
        };
    }

    fn handle_view_key(&mut self, code: KeyCode, _mods: KeyModifiers) {
        match self.focus {
            FocusPane::Nav => match code {
                KeyCode::Up | KeyCode::Char('k') => self.view = self.view.prev(),
                KeyCode::Down | KeyCode::Char('j') => self.view = self.view.next(),
                KeyCode::Enter => self.focus = FocusPane::Main,
                _ => {}
            },
            FocusPane::Log => match code {
                KeyCode::Up | KeyCode::Char('k') => {
                    self.scroll = self.scroll.saturating_sub(1);
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    self.scroll = self.scroll.saturating_add(1);
                }
                _ => {}
            },
            FocusPane::Detail => self.handle_detail_key(code),
            FocusPane::Main => self.handle_main_key(code),
        }
    }

    fn handle_main_key(&mut self, code: KeyCode) {
        match self.view {
            ViewId::Dashboard => match code {
                KeyCode::Up | KeyCode::Char('k') => {
                    self.scroll = self.scroll.saturating_sub(1);
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    self.scroll = self.scroll.saturating_add(1);
                }
                _ => {}
            },
            ViewId::Queue => match code {
                KeyCode::Up | KeyCode::Char('k') => {
                    self.queue_sel = self.queue_sel.saturating_sub(1);
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    if !self.snapshot.queue.is_empty() {
                        self.queue_sel = (self.queue_sel + 1).min(self.snapshot.queue.len() - 1);
                    }
                }
                KeyCode::Char('d') => self.start_drain(),
                KeyCode::Char('x') => self.confirm_remove_queue(),
                _ => {}
            },
            ViewId::Ledger => match code {
                KeyCode::Up | KeyCode::Char('k') => {
                    self.ledger_sel = self.ledger_sel.saturating_sub(1);
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    if !self.snapshot.ledger.is_empty() {
                        self.ledger_sel = (self.ledger_sel + 1).min(self.snapshot.ledger.len() - 1);
                    }
                }
                KeyCode::Enter => {
                    if !self.snapshot.ledger.is_empty() {
                        self.ledger_detail = true;
                        self.finding_sel = 0;
                        self.focus = FocusPane::Detail;
                    }
                }
                KeyCode::Char('w') => self.open_waiver(),
                KeyCode::Char('p') => self.open_petition(),
                _ => {}
            },
            ViewId::Config => match code {
                KeyCode::Up | KeyCode::Char('k') => {
                    self.config_sel = self.config_sel.saturating_sub(1);
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    if !self.snapshot.config_rows.is_empty() {
                        self.config_sel =
                            (self.config_sel + 1).min(self.snapshot.config_rows.len() - 1);
                    }
                }
                KeyCode::Enter | KeyCode::Char('e') => self.open_config_edit(),
                KeyCode::Char('s') => {
                    self.status = "edit a value with Enter, then save from the diff preview".into();
                }
                _ => {}
            },
            ViewId::Debt => match code {
                KeyCode::Up | KeyCode::Char('k') => {
                    self.debt_sel = self.debt_sel.saturating_sub(1);
                    self.debt_entry_sel = 0;
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    if !self.snapshot.debt.is_empty() {
                        self.debt_sel = (self.debt_sel + 1).min(self.snapshot.debt.len() - 1);
                        self.debt_entry_sel = 0;
                    }
                }
                KeyCode::Left | KeyCode::Char('h') => {
                    self.debt_entry_sel = self.debt_entry_sel.saturating_sub(1);
                }
                KeyCode::Right | KeyCode::Char('l') => {
                    if let Some(group) = self.snapshot.debt.get(self.debt_sel)
                        && !group.entries.is_empty()
                    {
                        self.debt_entry_sel =
                            (self.debt_entry_sel + 1).min(group.entries.len() - 1);
                    }
                }
                _ => {}
            },
        }
    }

    fn handle_detail_key(&mut self, code: KeyCode) {
        let cards = self
            .selected_ledger_entry()
            .map(finding_cards)
            .unwrap_or_default();
        match code {
            KeyCode::Esc | KeyCode::Backspace => {
                self.ledger_detail = false;
                self.focus = FocusPane::Main;
            }
            KeyCode::Up | KeyCode::Char('k') => {
                self.finding_sel = self.finding_sel.saturating_sub(1);
            }
            KeyCode::Down | KeyCode::Char('j') => {
                if !cards.is_empty() {
                    self.finding_sel = (self.finding_sel + 1).min(cards.len() - 1);
                }
            }
            KeyCode::Char('w') => self.open_waiver(),
            KeyCode::Char('p') => self.open_petition(),
            _ => {}
        }
    }

    fn selected_ledger_entry(&self) -> Option<&crate::ledger::LedgerEntry> {
        let row = self.snapshot.ledger.get(self.ledger_sel)?;
        self.snapshot
            .ledger_entries
            .iter()
            .find(|e| e.commit_sha == row.sha)
    }

    fn selected_queue_row(&self) -> Option<&super::model::QueueRow> {
        self.snapshot.queue.get(self.queue_sel)
    }

    fn confirm_remove_queue(&mut self) {
        let Some(row) = self.selected_queue_row().cloned() else {
            self.status = "queue is empty".into();
            return;
        };
        self.modal = Some(Modal::Confirm {
            title: "Remove queue item".into(),
            body: format!(
                "Remove {} ({}) from the review queue?",
                row.short_sha, row.subject
            ),
            action: ConfirmAction::RemoveQueue {
                run_id: row.run_id,
                sha: row.sha,
            },
        });
    }

    fn start_drain(&mut self) {
        if self.drain_running {
            self.status = "drain already running".into();
            return;
        }
        let (tx, rx) = mpsc::channel();
        match actions::spawn_drain_once(&self.state_dir, &self.config_path, tx) {
            Ok(handle) => {
                self.drain_running = true;
                self.drain_rx = Some(rx);
                self._drain_handle = Some(handle);
                self.focus = FocusPane::Log;
                self.status = "draining queue (watch --once)…".into();
            }
            Err(error) => {
                self.status = format!("drain spawn failed: {error}");
            }
        }
    }

    fn open_waiver(&mut self) {
        let Some(entry) = self.selected_ledger_entry() else {
            self.status = "no ledger entry selected".into();
            return;
        };
        if !entry.is_unresolved_rejection() && !entry.is_needs_human() {
            self.status = "waive only applies to open rejections or needs-human".into();
            return;
        }
        if !crate::resolve::stdin_is_tty() {
            self.status = "waive refused: stdin is not a TTY (agent lane blocked)".into();
            return;
        }
        let sha = entry.commit_sha.clone();
        self.modal = Some(Modal::Multiline {
            title: format!("Waive {sha}"),
            hint: "Ctrl+S submit · Esc cancel · replace template placeholders".into(),
            buffer: waiver_template(&sha),
            kind: MultiKind::Waiver { sha },
        });
    }

    fn open_petition(&mut self) {
        let Some(entry) = self.selected_ledger_entry() else {
            self.status = "no ledger entry selected".into();
            return;
        };
        if !entry.is_unresolved_rejection() {
            self.status = "petition only applies to open rejections".into();
            return;
        }
        let sha = entry.commit_sha.clone();
        self.modal = Some(Modal::TextPrompt {
            title: format!("Petition {sha}"),
            hint: "Enter fix commit SHA · Enter submit · Esc cancel".into(),
            buffer: String::new(),
            kind: TextKind::Petition { rejection_sha: sha },
        });
    }

    fn open_config_edit(&mut self) {
        let Some(row) = self.snapshot.config_rows.get(self.config_sel).cloned() else {
            self.status = "no config row".into();
            return;
        };
        if !row.editable {
            self.status = "row is not editable".into();
            return;
        }
        self.modal = Some(Modal::TextPrompt {
            title: format!("Edit {}", row.path),
            hint: "Enter confirm · Esc cancel · value validated before preview".into(),
            buffer: row.value.clone(),
            kind: TextKind::ConfigEdit { path: row.path },
        });
    }

    fn handle_modal_key(&mut self, modal: Modal, key: KeyEvent) {
        match modal {
            Modal::Help => {
                if matches!(
                    key.code,
                    KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('?') | KeyCode::Enter
                ) {
                    self.modal = None;
                }
            }
            Modal::Confirm { action, .. } => match key.code {
                KeyCode::Esc | KeyCode::Char('n') => self.modal = None,
                KeyCode::Char('y') | KeyCode::Enter => {
                    self.modal = None;
                    self.run_confirm(action);
                }
                _ => {}
            },
            Modal::TextPrompt {
                title,
                hint,
                mut buffer,
                kind,
            } => match key.code {
                KeyCode::Esc => self.modal = None,
                KeyCode::Enter => {
                    self.modal = None;
                    self.submit_text(kind, buffer);
                }
                KeyCode::Backspace => {
                    buffer.pop();
                    self.modal = Some(Modal::TextPrompt {
                        title,
                        hint,
                        buffer,
                        kind,
                    });
                }
                KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                    buffer.push(c);
                    self.modal = Some(Modal::TextPrompt {
                        title,
                        hint,
                        buffer,
                        kind,
                    });
                }
                _ => {
                    self.modal = Some(Modal::TextPrompt {
                        title,
                        hint,
                        buffer,
                        kind,
                    });
                }
            },
            Modal::Multiline {
                title,
                hint,
                mut buffer,
                kind,
            } => {
                if key.modifiers.contains(KeyModifiers::CONTROL)
                    && matches!(key.code, KeyCode::Char('s') | KeyCode::Char('S'))
                {
                    self.modal = None;
                    self.submit_multi(kind, buffer);
                    return;
                }
                match key.code {
                    KeyCode::Esc => self.modal = None,
                    KeyCode::Enter => {
                        buffer.push('\n');
                        self.modal = Some(Modal::Multiline {
                            title,
                            hint,
                            buffer,
                            kind,
                        });
                    }
                    KeyCode::Backspace => {
                        buffer.pop();
                        self.modal = Some(Modal::Multiline {
                            title,
                            hint,
                            buffer,
                            kind,
                        });
                    }
                    KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                        buffer.push(c);
                        self.modal = Some(Modal::Multiline {
                            title,
                            hint,
                            buffer,
                            kind,
                        });
                    }
                    _ => {
                        self.modal = Some(Modal::Multiline {
                            title,
                            hint,
                            buffer,
                            kind,
                        });
                    }
                }
            }
            Modal::DiffPreview {
                title,
                diff,
                path,
                value,
            } => match key.code {
                KeyCode::Esc | KeyCode::Char('n') => self.modal = None,
                KeyCode::Char('s') | KeyCode::Char('y') | KeyCode::Enter => {
                    self.modal = None;
                    match actions::save_config_edit(
                        &self.config_path,
                        &self.snapshot,
                        &path,
                        &value,
                    ) {
                        Ok(msg) => {
                            self.status = msg;
                            self.refresh();
                        }
                        Err(error) => self.status = format!("save failed: {error}"),
                    }
                }
                _ => {
                    self.modal = Some(Modal::DiffPreview {
                        title,
                        diff,
                        path,
                        value,
                    });
                }
            },
        }
    }

    fn run_confirm(&mut self, action: ConfirmAction) {
        match action {
            ConfirmAction::Quit => self.should_quit = true,
            ConfirmAction::RemoveQueue { run_id, sha } => {
                match actions::remove_queue_item(&self.state_dir, &run_id, &sha) {
                    Ok(()) => {
                        self.status = format!("removed queue item {sha}");
                        self.refresh();
                    }
                    Err(error) => self.status = format!("remove failed: {error}"),
                }
            }
        }
    }

    fn submit_text(&mut self, kind: TextKind, buffer: String) {
        match kind {
            TextKind::Petition { rejection_sha } => match validate_fix_sha(&buffer) {
                Ok(fix_sha) => {
                    match actions::petition_rejection(&self.state_dir, &rejection_sha, &fix_sha) {
                        Ok(msg) => {
                            self.status = msg;
                            self.refresh();
                        }
                        Err(error) => self.status = format!("petition failed: {error}"),
                    }
                }
                Err(error) => self.status = error,
            },
            TextKind::ConfigEdit { path } => {
                match actions::validate_and_normalize_edit(&path, &buffer) {
                    Ok(value) => {
                        match actions::preview_config_edit(&self.snapshot, &path, &value) {
                            Ok(diff) => {
                                self.modal = Some(Modal::DiffPreview {
                                    title: format!("Save {path}?"),
                                    diff,
                                    path,
                                    value,
                                });
                            }
                            Err(error) => self.status = format!("preview failed: {error}"),
                        }
                    }
                    Err(error) => self.status = format!("invalid value: {error}"),
                }
            }
        }
    }

    fn submit_multi(&mut self, kind: MultiKind, buffer: String) {
        match kind {
            MultiKind::Waiver { sha } => {
                if let Err(error) = actions::validate_waiver_reason(&buffer) {
                    self.status = error.to_string();
                    return;
                }
                match actions::waive_rejection(&self.state_dir, &sha, &buffer) {
                    Ok(msg) => {
                        self.status = msg;
                        self.ledger_detail = false;
                        self.focus = FocusPane::Main;
                        self.refresh();
                    }
                    Err(error) => self.status = format!("waive failed: {error}"),
                }
            }
        }
    }
}

/// Run the full-screen control panel until quit.
pub fn run(args: TuiArgs, state_dir: &Path, config_path: Option<&Path>) -> Result<ExitCode> {
    use std::io::IsTerminal;
    if !io::stdin().is_terminal() || !io::stdout().is_terminal() {
        anyhow::bail!(
            "truth-mirror tui requires an interactive terminal (stdin and stdout must be TTYs)"
        );
    }

    let config_path = actions::resolve_config_path(config_path, state_dir);
    let version = env!("CARGO_PKG_VERSION").to_owned();
    let mut app = App::new(
        state_dir.to_path_buf(),
        config_path,
        version,
        !args.no_auto_refresh,
    )?;

    enable_raw_mode()?;
    // Ported from upstream 419e2eb: if `run_loop` panics, the cleanup calls
    // that used to run only AFTER it returned are skipped entirely —
    // unwinding goes straight past them, leaving the terminal in raw mode
    // with the alternate screen active. `TerminalGuard`'s `Drop` restores
    // the terminal on every exit path out of this function, panic included,
    // since local variables are dropped during unwinding too.
    let _terminal_guard = TerminalGuard;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let result = run_loop(&mut terminal, &mut app);

    result?;
    Ok(ExitCode::SUCCESS)
}

/// Restores the terminal (raw mode off, alternate screen left, cursor shown)
/// on drop, including when a panic unwinds out of `run_loop` before the
/// normal post-loop cleanup ever runs. Best-effort: failures here are
/// swallowed since the process may already be unwinding or exiting on an
/// error and a secondary terminal-cleanup failure must not mask it. Ported
/// from upstream 419e2eb.
struct TerminalGuard;

impl Drop for TerminalGuard {
    fn drop(&mut self) {
        let _ = disable_raw_mode();
        let _ = execute!(io::stdout(), LeaveAlternateScreen, cursor::Show);
    }
}

fn run_loop(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, app: &mut App) -> Result<()> {
    loop {
        app.tick_drain();
        if app.auto_refresh && app.last_refresh.elapsed() >= REFRESH_INTERVAL && !app.drain_running
        {
            app.refresh();
        }

        terminal.draw(|frame| ui::draw(frame, app))?;

        if app.should_quit {
            break;
        }

        if event::poll(POLL)?
            && let Event::Key(key) = event::read()?
        {
            app.on_key(key);
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_app() -> (tempfile::TempDir, App) {
        let temp = tempfile::tempdir().unwrap();
        let config_path = temp.path().join("config.toml");
        let app = App::new(
            temp.path().to_path_buf(),
            config_path,
            "0.0.0-test".to_owned(),
            false,
        )
        .unwrap();
        (temp, app)
    }

    #[test]
    fn tick_drain_treats_a_disconnected_sender_as_done_not_a_cpu_spin() {
        // Ported from upstream 419e2eb: the old `while let Ok(event) =
        // rx.try_recv()` loop only matched `Ok`, so a `Disconnected` error
        // (the drain worker thread died without ever sending `Done`) fell
        // through with `done` still false — the receiver was restored and
        // polled again next tick. `try_recv` on a disconnected channel never
        // blocks, so every tick immediately saw `Disconnected` again: a 100%
        // CPU busy loop with no progress. The fix must treat disconnection
        // as done and drop the receiver instead of restoring it.
        let (_temp, mut app) = test_app();
        let (sender, receiver) = mpsc::channel::<DrainEvent>();
        drop(sender);
        app.drain_rx = Some(receiver);
        app.drain_running = true;

        app.tick_drain();

        assert!(
            app.drain_rx.is_none(),
            "a disconnected receiver must not be restored for endless re-polling"
        );
        assert!(
            !app.drain_running,
            "a disconnected drain must not be reported as still running"
        );
        assert!(
            app.status.contains("disconnected"),
            "the status must distinguish a worker disconnect from a clean finish: {:?}",
            app.status
        );
    }

    #[test]
    fn tick_drain_reports_success_when_done_is_immediately_followed_by_disconnect() {
        // Ported from upstream 88d1ecd: a normal drain worker sends
        // `Done(code)` and then drops its sender as its very next act — this
        // is the TYPICAL shutdown sequence, not a rare edge case. Without a
        // `break` right after observing `Done`, the same tick's next
        // `try_recv()` sees `Disconnected` and the disconnected branch ran,
        // overriding a successful drain with "disconnected unexpectedly" and
        // discarding the real exit code.
        let (_temp, mut app) = test_app();
        let (sender, receiver) = mpsc::channel::<DrainEvent>();
        sender.send(DrainEvent::Done(0)).unwrap();
        drop(sender);
        app.drain_rx = Some(receiver);
        app.drain_running = true;

        app.tick_drain();

        assert!(
            !app.drain_running,
            "a successful drain must not still be reported as running"
        );
        assert!(
            app.status.contains("drain finished"),
            "a successful drain must be reported as finished, not as an \
             unexpected disconnect: {:?}",
            app.status
        );
        assert!(
            !app.status.contains("disconnected"),
            "a successful drain must never be misreported as disconnected: {:?}",
            app.status
        );
    }

    #[test]
    fn tick_drain_keeps_polling_while_the_channel_is_merely_empty() {
        // The companion case: an empty-but-still-connected channel (the
        // sender is alive, just hasn't sent anything yet) must still be
        // restored for the next tick — only `Disconnected` ends the drain.
        let (_temp, mut app) = test_app();
        let (_sender, receiver) = mpsc::channel::<DrainEvent>();
        app.drain_rx = Some(receiver);
        app.drain_running = true;

        app.tick_drain();

        assert!(
            app.drain_rx.is_some(),
            "an empty (but connected) channel must be restored, not discarded"
        );
        assert!(app.drain_running);
    }

    #[test]
    fn terminal_guard_drop_is_safe_without_a_real_terminal() {
        // Ported from upstream 419e2eb: the guard's Drop must never panic,
        // since it can run during an unwind from a genuine panic in
        // `run_loop` — a panicking Drop during unwinding aborts the process
        // instead of restoring the terminal. Constructing and dropping it
        // here (no real TTY in a test process) exercises that its Drop
        // swallows failures rather than propagating them.
        let guard = TerminalGuard;
        drop(guard);
    }
}