shellfirm 0.4.0

`shellfirm` will intercept any risky patterns (default or defined by you) and prompt you a small challenge for double verification, kinda like a captcha for your terminal.
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
//! AI tab — drill-down model.

use crate::checks::Severity;
use crate::config::{Challenge, InheritOr, Mode};
use crate::tui::draft::DraftSettings;
use crate::tui::tabs::{FieldFocus, Tab, TabOutcome};
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};

const SECTION_AUTO_DENY: usize = 0;
const SECTION_HUMAN_APPROVAL: usize = 1;
const SECTION_OVR_CHALLENGE: usize = 2;
const SECTION_OVR_MIN_SEV: usize = 3;
const SECTION_OVR_SEV_ESC: usize = 4;
const NUM_SECTIONS: usize = 5;

const CHALLENGE_OPTIONS: &[&str] = &["Math", "Enter", "Yes"];
const SEVERITY_OPTIONS: &[&str] = &["Info", "Low", "Medium", "High", "Critical"];
const SEVERITY_OPTIONAL_OPTIONS: &[&str] = &["(all)", "Info", "Low", "Medium", "High", "Critical"];

#[derive(Debug, Default)]
pub struct AITab {
    pub cursor: usize, // section index
    pub edit: Option<EditState>,
    cached_focus: FieldFocus,
}

impl AITab {
    fn compute_focus(&self, _draft: &DraftSettings) -> FieldFocus {
        let title = Self::section_title(self.cursor);
        let (name, help) = if self.edit.is_some() {
            (
                format!("Editing: {title}"),
                "↑↓ navigate · Space to select · Enter to exit · Esc to exit".to_string(),
            )
        } else {
            (
                title.to_string(),
                "↑↓ select section · Enter to edit.".to_string(),
            )
        };
        FieldFocus {
            name,
            badges: vec!["ai"],
            help,
        }
    }
}

#[derive(Debug)]
pub enum EditState {
    /// 0..=4 cursor in the auto-deny radio
    AutoDeny(usize),
    /// human approval toggle (no cursor)
    HumanApproval,
    /// override challenge radio: cursor 0 = Inherit, 1..=N = Set(option N-1).
    OvrChallenge(usize),
    /// override min severity radio: cursor 0 = Inherit, 1..=N = Set(option N-1).
    OvrMinSev(usize),
    /// override severity escalation: cursor 0 = Inherit, 1 = Custom.
    OvrSevEsc(usize),
}

fn ch_index(c: Challenge) -> usize {
    match c {
        Challenge::Math => 0,
        Challenge::Enter => 1,
        _ => 2,
    }
}
fn ch_from_idx(i: usize) -> Challenge {
    match i {
        1 => Challenge::Enter,
        2 => Challenge::Yes,
        _ => Challenge::Math,
    }
}
fn sev_index(s: Severity) -> usize {
    match s {
        Severity::Info => 0,
        Severity::Low => 1,
        Severity::Medium => 2,
        Severity::High => 3,
        Severity::Critical => 4,
    }
}
fn sev_from_idx(i: usize) -> Severity {
    match i {
        0 => Severity::Info,
        1 => Severity::Low,
        2 => Severity::Medium,
        3 => Severity::High,
        _ => Severity::Critical,
    }
}
fn opt_sev_index(s: Option<Severity>) -> usize {
    match s {
        None => 0,
        Some(s) => sev_index(s) + 1,
    }
}
fn opt_sev_from_idx(i: usize) -> Option<Severity> {
    if i == 0 {
        None
    } else {
        Some(sev_from_idx(i - 1))
    }
}

impl AITab {
    fn section_title(section: usize) -> &'static str {
        match section {
            SECTION_AUTO_DENY => "Auto-deny severity",
            SECTION_HUMAN_APPROVAL => "Require human approval",
            SECTION_OVR_CHALLENGE => "Challenge override (AI)",
            SECTION_OVR_MIN_SEV => "Min severity override (AI)",
            SECTION_OVR_SEV_ESC => "Severity escalation override (AI)",
            _ => "",
        }
    }
    fn section_blurb(section: usize) -> &'static str {
        match section {
            SECTION_AUTO_DENY => "When the agent's max severity is at least this, deny outright.",
            SECTION_HUMAN_APPROVAL => "On: at the auto-deny level, ask a human instead of denying.",
            SECTION_OVR_CHALLENGE => "Override the global Challenge type for AI mode.",
            SECTION_OVR_MIN_SEV => "Override the global Minimum severity for AI mode.",
            SECTION_OVR_SEV_ESC => "Override the global Severity escalation for AI mode.",
            _ => "",
        }
    }
}

impl Tab for AITab {
    fn is_editing(&self) -> bool {
        self.edit.is_some()
    }

    fn title(&self) -> &str {
        "AI"
    }
    fn mode_badge(&self) -> Option<&'static str> {
        Some("ai")
    }

    fn render(&self, area: Rect, buf: &mut Buffer, draft: &DraftSettings) {
        if area.height < 18 || area.width < 60 {
            return;
        }
        let mut y = area.y;
        for section in 0..NUM_SECTIONS {
            let is_focused = section == self.cursor;
            let is_editing_this = is_focused && self.edit.is_some();
            let bar_color = if is_editing_this {
                Color::Green
            } else if is_focused {
                Color::Yellow
            } else {
                Color::DarkGray
            };
            buf.set_string(area.x, y, "▌", Style::default().fg(bar_color));
            let title_style = if is_focused {
                Style::default()
                    .fg(Color::White)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default()
                    .fg(Color::Gray)
                    .add_modifier(Modifier::BOLD)
            };
            buf.set_string(area.x + 2, y, Self::section_title(section), title_style);
            let badge = "[ai]";
            buf.set_string(
                area.x + area.width.saturating_sub(badge.chars().count() as u16),
                y,
                badge,
                Style::default().fg(Color::Magenta),
            );
            y += 1;

            if is_editing_this {
                y = self.render_editing_body(buf, area.x, y, area.width, draft, section);
            } else {
                y = self
                    .render_summary_body(buf, area.x, y, area.width, draft, section, is_focused);
            }
            y += 1;
        }
    }

    fn handle_key(&mut self, key: KeyEvent, draft: &mut DraftSettings) -> TabOutcome {
        match self.edit.take() {
            None => self.handle_browsing(key, draft),
            Some(state) => self.handle_editing(key, draft, state),
        }
    }

    fn current_focus(&self) -> FieldFocus {
        let title = Self::section_title(self.cursor);
        let (name, help) = if self.edit.is_some() {
            (
                format!("Editing: {title}"),
                "↑↓ navigate · Enter commit/exit · Esc cancel".to_string(),
            )
        } else {
            (
                title.to_string(),
                "↑↓ select section · Enter to edit.".to_string(),
            )
        };
        FieldFocus {
            name,
            badges: vec!["ai"],
            help,
        }
    }
}

impl AITab {
    fn handle_browsing(&mut self, key: KeyEvent, draft: &mut DraftSettings) -> TabOutcome {
        match key.code {
            KeyCode::Up | KeyCode::Char('k') => {
                if self.cursor > 0 {
                    self.cursor -= 1;
                    return TabOutcome::FieldFocusChanged(self.current_focus());
                }
                TabOutcome::None
            }
            KeyCode::Down | KeyCode::Char('j') => {
                if self.cursor + 1 < NUM_SECTIONS {
                    self.cursor += 1;
                    return TabOutcome::FieldFocusChanged(self.current_focus());
                }
                TabOutcome::None
            }
            KeyCode::Enter => {
                self.edit = Some(self.initial_edit_state(draft));
                TabOutcome::FieldFocusChanged(self.current_focus())
            }
            _ => TabOutcome::None,
        }
    }

    fn initial_edit_state(&self, draft: &DraftSettings) -> EditState {
        match self.cursor {
            SECTION_AUTO_DENY => {
                EditState::AutoDeny(sev_index(draft.current.agent.auto_deny_severity))
            }
            SECTION_HUMAN_APPROVAL => EditState::HumanApproval,
            SECTION_OVR_CHALLENGE => {
                // Cursor 0 = Inherit; cursor 1+i = Set(CHALLENGE_OPTIONS[i]).
                let i = match draft.current.agent.challenge {
                    InheritOr::Inherit => 0,
                    InheritOr::Set(c) => ch_index(c) + 1,
                };
                EditState::OvrChallenge(i)
            }
            SECTION_OVR_MIN_SEV => {
                let i = match draft.current.agent.min_severity {
                    InheritOr::Inherit => 0,
                    InheritOr::Set(s) => opt_sev_index(s) + 1,
                };
                EditState::OvrMinSev(i)
            }
            SECTION_OVR_SEV_ESC => {
                // 2-option radio: 0 = Inherit, 1 = Custom.
                let i = match draft.current.agent.severity_escalation {
                    InheritOr::Inherit => 0,
                    InheritOr::Set(_) => 1,
                };
                EditState::OvrSevEsc(i)
            }
            _ => EditState::HumanApproval,
        }
    }

    fn handle_editing(
        &mut self,
        key: KeyEvent,
        draft: &mut DraftSettings,
        state: EditState,
    ) -> TabOutcome {
        // Esc and Left exit edit mode.
        if matches!(key.code, KeyCode::Esc | KeyCode::Left | KeyCode::Char('h')) {
            self.edit = None;
            return TabOutcome::FieldFocusChanged(self.current_focus());
        }
        match state {
            EditState::AutoDeny(mut cursor) => match key.code {
                KeyCode::Up | KeyCode::Char('k') => {
                    if cursor > 0 {
                        cursor -= 1;
                    }
                    self.edit = Some(EditState::AutoDeny(cursor));
                    TabOutcome::None
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    if cursor + 1 < SEVERITY_OPTIONS.len() {
                        cursor += 1;
                    }
                    self.edit = Some(EditState::AutoDeny(cursor));
                    TabOutcome::None
                }
                KeyCode::Char(' ') => {
                    // Space writes cursor's value to draft (stays in edit).
                    let new_sev = sev_from_idx(cursor);
                    let mutated = draft.current.agent.auto_deny_severity != new_sev;
                    if mutated {
                        draft.current.agent.auto_deny_severity = new_sev;
                    }
                    self.edit = Some(EditState::AutoDeny(cursor));
                    if mutated {
                        TabOutcome::Mutated
                    } else {
                        TabOutcome::None
                    }
                }
                KeyCode::Enter => {
                    // Enter exits edit mode (Space already wrote).
                    self.edit = None;
                    self.cached_focus = self.compute_focus(draft);
                    TabOutcome::FieldFocusChanged(self.cached_focus.clone())
                }
                _ => {
                    self.edit = Some(EditState::AutoDeny(cursor));
                    TabOutcome::None
                }
            },
            EditState::HumanApproval => {
                if matches!(key.code, KeyCode::Char(' ')) {
                    // Space flips the toggle in-place.
                    draft.current.agent.require_human_approval =
                        !draft.current.agent.require_human_approval;
                    self.edit = Some(EditState::HumanApproval);
                    return TabOutcome::Mutated;
                }
                if matches!(key.code, KeyCode::Enter) {
                    // Enter exits edit mode, no further mutation.
                    self.edit = None;
                    self.cached_focus = self.compute_focus(draft);
                    return TabOutcome::FieldFocusChanged(self.cached_focus.clone());
                }
                self.edit = Some(EditState::HumanApproval);
                TabOutcome::None
            }
            EditState::OvrChallenge(mut cursor) => {
                let total = CHALLENGE_OPTIONS.len() + 1;
                match key.code {
                    KeyCode::Up | KeyCode::Char('k') => {
                        if cursor > 0 {
                            cursor -= 1;
                        }
                    }
                    KeyCode::Down | KeyCode::Char('j') => {
                        if cursor + 1 < total {
                            cursor += 1;
                        }
                    }
                    KeyCode::Char(' ') => {
                        let new_value = if cursor == 0 {
                            InheritOr::Inherit
                        } else {
                            InheritOr::Set(ch_from_idx(cursor - 1))
                        };
                        let mutated = draft.current.agent.challenge != new_value;
                        if mutated {
                            draft.current.agent.challenge = new_value;
                        }
                        self.edit = Some(EditState::OvrChallenge(cursor));
                        return if mutated {
                            TabOutcome::Mutated
                        } else {
                            TabOutcome::None
                        };
                    }
                    KeyCode::Enter => {
                        self.edit = None;
                        self.cached_focus = self.compute_focus(draft);
                        return TabOutcome::FieldFocusChanged(self.cached_focus.clone());
                    }
                    _ => {}
                }
                self.edit = Some(EditState::OvrChallenge(cursor));
                TabOutcome::None
            }
            EditState::OvrMinSev(mut cursor) => {
                let total = SEVERITY_OPTIONAL_OPTIONS.len() + 1;
                match key.code {
                    KeyCode::Up | KeyCode::Char('k') => {
                        if cursor > 0 {
                            cursor -= 1;
                        }
                    }
                    KeyCode::Down | KeyCode::Char('j') => {
                        if cursor + 1 < total {
                            cursor += 1;
                        }
                    }
                    KeyCode::Char(' ') => {
                        let new_value = if cursor == 0 {
                            InheritOr::Inherit
                        } else {
                            InheritOr::Set(opt_sev_from_idx(cursor - 1))
                        };
                        let mutated = draft.current.agent.min_severity != new_value;
                        if mutated {
                            draft.current.agent.min_severity = new_value;
                        }
                        self.edit = Some(EditState::OvrMinSev(cursor));
                        return if mutated {
                            TabOutcome::Mutated
                        } else {
                            TabOutcome::None
                        };
                    }
                    KeyCode::Enter => {
                        self.edit = None;
                        self.cached_focus = self.compute_focus(draft);
                        return TabOutcome::FieldFocusChanged(self.cached_focus.clone());
                    }
                    _ => {}
                }
                self.edit = Some(EditState::OvrMinSev(cursor));
                TabOutcome::None
            }
            EditState::OvrSevEsc(mut cursor) => {
                let global = draft.current.resolved_for(Mode::Shell);
                match key.code {
                    KeyCode::Up | KeyCode::Char('k') => {
                        if cursor > 0 {
                            cursor -= 1;
                        }
                    }
                    KeyCode::Down | KeyCode::Char('j') => {
                        if cursor < 1 {
                            cursor += 1;
                        }
                    }
                    KeyCode::Char(' ') => {
                        let new_value = if cursor == 0 {
                            InheritOr::Inherit
                        } else {
                            InheritOr::Set(global.severity_escalation.clone())
                        };
                        let mutated = draft.current.agent.severity_escalation != new_value;
                        if mutated {
                            draft.current.agent.severity_escalation = new_value;
                        }
                        self.edit = Some(EditState::OvrSevEsc(cursor));
                        return if mutated {
                            TabOutcome::Mutated
                        } else {
                            TabOutcome::None
                        };
                    }
                    KeyCode::Enter => {
                        self.edit = None;
                        self.cached_focus = self.compute_focus(draft);
                        return TabOutcome::FieldFocusChanged(self.cached_focus.clone());
                    }
                    _ => {}
                }
                self.edit = Some(EditState::OvrSevEsc(cursor));
                TabOutcome::None
            }
        }
    }

    fn render_summary_body(
        &self,
        buf: &mut Buffer,
        area_x: u16,
        y: u16,
        area_width: u16,
        draft: &DraftSettings,
        section: usize,
        is_focused: bool,
    ) -> u16 {
        let indent = area_x + 2;
        let global = draft.current.resolved_for(Mode::Shell);
        buf.set_string(
            indent,
            y,
            Self::section_blurb(section),
            Style::default().fg(Color::DarkGray),
        );
        let y = y + 1;

        let summary = match section {
            SECTION_AUTO_DENY => format!("Currently: {}", draft.current.agent.auto_deny_severity),
            SECTION_HUMAN_APPROVAL => {
                if draft.current.agent.require_human_approval {
                    "Currently: ✓ enabled".to_string()
                } else {
                    "Currently: ✗ disabled".to_string()
                }
            }
            SECTION_OVR_CHALLENGE => match draft.current.agent.challenge {
                InheritOr::Inherit => format!("inherits global → {}", global.challenge),
                InheritOr::Set(c) => format!("custom → {c}"),
            },
            SECTION_OVR_MIN_SEV => {
                let inherited = match global.min_severity {
                    None => "(all)".to_string(),
                    Some(s) => format!("{s}"),
                };
                match &draft.current.agent.min_severity {
                    InheritOr::Inherit => format!("inherits global → {inherited}"),
                    InheritOr::Set(s) => {
                        let v = match s {
                            None => "(all)".to_string(),
                            Some(s) => format!("{s}"),
                        };
                        format!("custom → {v}")
                    }
                }
            }
            SECTION_OVR_SEV_ESC => match &draft.current.agent.severity_escalation {
                InheritOr::Inherit => "inherits global → (matrix)".to_string(),
                InheritOr::Set(_) => "custom → (custom matrix)".to_string(),
            },
            _ => String::new(),
        };
        let max_w = area_width.saturating_sub(2 + 16) as usize;
        let summary = if summary.chars().count() > max_w {
            let mut s: String = summary.chars().take(max_w.saturating_sub(1)).collect();
            s.push('…');
            s
        } else {
            summary
        };
        buf.set_string(indent, y, &summary, Style::default().fg(Color::Cyan));
        if is_focused {
            let hint = "Enter to edit";
            let hint_x = area_x + area_width.saturating_sub(hint.chars().count() as u16);
            buf.set_string(
                hint_x,
                y,
                hint,
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            );
        }
        y + 1
    }

    fn render_editing_body(
        &self,
        buf: &mut Buffer,
        area_x: u16,
        y: u16,
        area_width: u16,
        draft: &DraftSettings,
        section: usize,
    ) -> u16 {
        let indent = area_x + 2;
        let inner_w = area_width.saturating_sub(2);
        buf.set_string(
            indent,
            y,
            "─".repeat(inner_w as usize),
            Style::default().fg(Color::DarkGray),
        );
        let mut y = y + 1;
        let edit = self.edit.as_ref().expect("editing");

        let render_radio =
            |buf: &mut Buffer, mut y: u16, options: &[&str], saved: usize, cursor: usize| -> u16 {
                for (i, opt) in options.iter().enumerate() {
                    let bullet = if i == saved { "(•)" } else { "( )" };
                    let is_cursor = i == cursor;
                    let style = if is_cursor {
                        Style::default()
                            .fg(Color::Black)
                            .bg(Color::Green)
                            .add_modifier(Modifier::BOLD)
                    } else if i == saved {
                        Style::default().fg(Color::Cyan)
                    } else {
                        Style::default()
                    };
                    buf.set_string(indent, y, format!("{bullet} {opt}"), style);
                    y += 1;
                }
                y
            };

        match (section, edit) {
            (SECTION_AUTO_DENY, EditState::AutoDeny(cursor)) => {
                let saved = sev_index(draft.current.agent.auto_deny_severity);
                y = render_radio(buf, y, SEVERITY_OPTIONS, saved, *cursor);
            }
            (SECTION_HUMAN_APPROVAL, _) => {
                let on = draft.current.agent.require_human_approval;
                let value = if on {
                    "[ ✓ enabled  ]"
                } else {
                    "[   disabled ]"
                };
                buf.set_string(
                    indent,
                    y,
                    "Require human approval",
                    Style::default()
                        .fg(Color::White)
                        .add_modifier(Modifier::BOLD),
                );
                buf.set_string(
                    indent + 28,
                    y,
                    value,
                    Style::default()
                        .fg(Color::Black)
                        .bg(Color::Green)
                        .add_modifier(Modifier::BOLD),
                );
                y += 1;
            }
            (SECTION_OVR_CHALLENGE, EditState::OvrChallenge(cursor)) => {
                let global = draft.current.resolved_for(Mode::Shell);
                let inherit_label = format!("Inherit global → {}", global.challenge);
                let mut options: Vec<String> = vec![inherit_label];
                options.extend(CHALLENGE_OPTIONS.iter().map(|s| (*s).to_string()));
                let saved = match draft.current.agent.challenge {
                    InheritOr::Inherit => 0,
                    InheritOr::Set(c) => ch_index(c) + 1,
                };
                let opt_refs: Vec<&str> = options.iter().map(String::as_str).collect();
                y = render_radio(buf, y, &opt_refs, saved, *cursor);
            }
            (SECTION_OVR_MIN_SEV, EditState::OvrMinSev(cursor)) => {
                let global = draft.current.resolved_for(Mode::Shell);
                let inherited = match global.min_severity {
                    None => "(all)".to_string(),
                    Some(s) => format!("{s}"),
                };
                let inherit_label = format!("Inherit global → {inherited}");
                let mut options: Vec<String> = vec![inherit_label];
                options.extend(SEVERITY_OPTIONAL_OPTIONS.iter().map(|s| (*s).to_string()));
                let saved = match &draft.current.agent.min_severity {
                    InheritOr::Inherit => 0,
                    InheritOr::Set(s) => opt_sev_index(*s) + 1,
                };
                let opt_refs: Vec<&str> = options.iter().map(String::as_str).collect();
                y = render_radio(buf, y, &opt_refs, saved, *cursor);
            }
            (SECTION_OVR_SEV_ESC, EditState::OvrSevEsc(cursor)) => {
                let options = ["Inherit global matrix", "Custom matrix"];
                let saved = match &draft.current.agent.severity_escalation {
                    InheritOr::Inherit => 0,
                    InheritOr::Set(_) => 1,
                };
                y = render_radio(buf, y, &options, saved, *cursor);
            }
            _ => {}
        }
        y
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Settings;
    use crossterm::event::KeyModifiers;

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::empty())
    }
    fn fresh() -> (AITab, DraftSettings) {
        (
            AITab::default(),
            DraftSettings::from_settings(Settings::default()),
        )
    }

    #[test]
    fn enter_drills_into_section() {
        let (mut tab, mut draft) = fresh();
        tab.handle_key(key(KeyCode::Enter), &mut draft);
        assert!(tab.edit.is_some());
    }

    #[test]
    fn navigate_then_space_commits_auto_deny() {
        let (mut tab, mut draft) = fresh();
        tab.handle_key(key(KeyCode::Enter), &mut draft);
        // Cursor starts at saved value (default High = 3). Down → Critical (4).
        tab.handle_key(key(KeyCode::Down), &mut draft);
        assert_eq!(draft.current.agent.auto_deny_severity, Severity::High);
        // Space writes; stays in edit mode.
        tab.handle_key(key(KeyCode::Char(' ')), &mut draft);
        assert_eq!(draft.current.agent.auto_deny_severity, Severity::Critical);
        assert!(tab.edit.is_some(), "Space stays in edit mode");
    }

    #[test]
    fn esc_after_navigate_keeps_previous_value() {
        let (mut tab, mut draft) = fresh();
        tab.handle_key(key(KeyCode::Enter), &mut draft);
        let initial = draft.current.agent.auto_deny_severity;
        tab.handle_key(key(KeyCode::Down), &mut draft);
        // Esc exits without writing (no Space pressed).
        tab.handle_key(key(KeyCode::Esc), &mut draft);
        assert_eq!(draft.current.agent.auto_deny_severity, initial);
        assert!(tab.edit.is_none());
    }

    #[test]
    fn space_in_human_approval_toggles() {
        let (mut tab, mut draft) = fresh();
        tab.cursor = SECTION_HUMAN_APPROVAL;
        tab.handle_key(key(KeyCode::Enter), &mut draft);
        let initial = draft.current.agent.require_human_approval;
        tab.handle_key(key(KeyCode::Char(' ')), &mut draft);
        assert_eq!(draft.current.agent.require_human_approval, !initial);
    }

    #[test]
    fn challenge_radio_inherit_is_first_choice() {
        let (mut tab, mut draft) = fresh();
        tab.cursor = SECTION_OVR_CHALLENGE;
        tab.handle_key(key(KeyCode::Enter), &mut draft);
        // Defaults to Inherit; cursor at 0. Down → cursor 1 = Math; Space commits.
        tab.handle_key(key(KeyCode::Down), &mut draft);
        tab.handle_key(key(KeyCode::Char(' ')), &mut draft);
        assert!(matches!(
            draft.current.agent.challenge,
            InheritOr::Set(Challenge::Math)
        ));
    }

    #[test]
    fn challenge_radio_choosing_inherit_writes_inherit() {
        let (mut tab, mut draft) = fresh();
        // Start with a Set value
        draft.current.agent.challenge = InheritOr::Set(Challenge::Yes);
        tab.cursor = SECTION_OVR_CHALLENGE;
        tab.handle_key(key(KeyCode::Enter), &mut draft);
        // Cursor starts at 1 + ch_index(Yes) = 3. Up to 0 = Inherit, Space commits.
        for _ in 0..5 {
            tab.handle_key(key(KeyCode::Up), &mut draft);
        }
        tab.handle_key(key(KeyCode::Char(' ')), &mut draft);
        assert!(matches!(draft.current.agent.challenge, InheritOr::Inherit));
    }

    #[test]
    fn sev_esc_radio_commits_custom_choice() {
        let (mut tab, mut draft) = fresh();
        tab.cursor = SECTION_OVR_SEV_ESC;
        tab.handle_key(key(KeyCode::Enter), &mut draft);
        // 2-choice radio: cursor 0 = Inherit, cursor 1 = Custom. Space commits.
        tab.handle_key(key(KeyCode::Down), &mut draft);
        tab.handle_key(key(KeyCode::Char(' ')), &mut draft);
        assert!(matches!(
            draft.current.agent.severity_escalation,
            InheritOr::Set(_)
        ));
    }
}