tam-cli 0.7.1

Terminal agent multiplexer — manage units of work, not just processes
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
//! The dashboard's lines in the activity log (`src` `tui`; see
//! `crate::activity`): what the user did, in which mode, what it led to and
//! how long that took.
//!
//! - `tui.start`, `tui.stop`: the session, its size and totals.
//! - `tui.input`: a key (named: `j`, `enter`, `ctrl-]`), a click or a paste
//!   that did something, with the mode it came in (`mode`), the mode it
//!   left for (`to`, with `mode_ms` spent in the one left), the action it
//!   ran (`action`, its target, `ms` it took), where the selection is then
//!   (`id`, `on`), what the status line or a notice says then. Never what
//!   is typed: keys into the pane are counted (`typed_bytes`, `writes`,
//!   `pastes`, given when the pane gives the keyboard back), the
//!   characters typed into a prompt too (`chars`).
//! - `tui.mode`: a mode left without an input (the agent typed into
//!   exited, a dialog closed by an event).
//! - `tui.slow`: a redraw that took long, by step.

use std::time::{Duration, Instant};

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use serde_json::{json, Map, Value};
use tam_daemon::activity::{self, Activity, Phases};

use super::app::{App, Mode, Purpose};
use super::Action;

/// A redraw that took this long is logged (`tui.slow`).
const SLOW: Duration = Duration::from_millis(200);

pub struct Recorder {
    log: &'static Activity,
    started: Instant,
    /// The mode the dashboard is in, and since when.
    mode: &'static str,
    mode_since: Instant,
    /// What was done in it that is counted, not logged.
    counted: Counted,
    inputs: u64,
    actions: u64,
    frames: u64,
    slow: u64,
}

#[derive(Default)]
struct Counted {
    typed_bytes: u64,
    writes: u64,
    pastes: u64,
    chars: u64,
    dropped_keys: u64,
}

/// What the dashboard showed before an input, to tell what it changed.
pub struct Before {
    mode: &'static str,
    status: Option<String>,
    notice: bool,
    /// A key now is dropped: typed for an agent the pane no longer has the
    /// keyboard for (see `App::drops_key`).
    dropping: bool,
}

/// What came in: a key, the mouse, a paste.
pub enum Input<'a> {
    Key(&'a KeyEvent),
    Mouse(&'static str),
    Paste(usize),
}

impl Recorder {
    pub fn new(log: &'static Activity) -> Self {
        let now = Instant::now();
        Self {
            log,
            started: now,
            mode: "normal",
            mode_since: now,
            counted: Counted::default(),
            inputs: 0,
            actions: 0,
            frames: 0,
            slow: 0,
        }
    }

    /// The dashboard is up, `ms` after it started.
    pub fn start(&self, app: &App, size: (u16, u16), ms: f64) {
        self.log.record(
            "tui.start",
            json!({
                "ms": ms,
                "cols": size.0,
                "rows": size.1,
                "theme": super::theme::name(),
                "tasks": app.tasks.len(),
                "chats": app.chats.len(),
                "shells": app.shells.len(),
                "keyboard_protocol": super::KEYBOARD_PROTOCOL
                    .load(std::sync::atomic::Ordering::Relaxed),
            }),
        );
    }

    /// The dashboard ends (`error`: why).
    pub fn stop(&self, error: Option<&anyhow::Error>) {
        self.log.record(
            "tui.stop",
            json!({
                "ms": activity::ms_since(self.started),
                "inputs": self.inputs,
                "actions": self.actions,
                "frames": self.frames,
                "slow_frames": self.slow,
                "wait": true,
                "error": error.map(|e| activity::cut(&format!("{e:#}"), 500)),
            }),
        );
    }

    /// A redraw's steps were timed: logged when slow.
    pub fn frame(&mut self, phases: &Phases) {
        self.frames += 1;
        if phases.total_ms() >= activity::ms(SLOW) {
            self.slow += 1;
            self.log.record(
                "tui.slow",
                json!({ "ms": phases.total_ms(), "steps_ms": phases.steps(), "mode": self.mode }),
            );
        }
    }

    /// Before an input is handled.
    pub fn before(&self, app: &mut App) -> Before {
        Before {
            mode: mode_name(app),
            status: app.status_message().map(str::to_string),
            notice: matches!(app.mode, Mode::Notice { .. }),
            // What `handle_key_event` asks `drops_key`, read only.
            dropping: !matches!(app.mode, Mode::Terminal { .. } | Mode::Editor { .. })
                && app
                    .dropping_keys_until
                    .is_some_and(|until| Instant::now() < until),
        }
    }

    /// Bytes typed into the pane: counted until it gives the keyboard back.
    pub fn typed(&mut self, bytes: usize) {
        self.counted.typed_bytes += bytes as u64;
    }

    /// One write of typed bytes to the agent (a batch of inputs).
    pub fn wrote(&mut self) {
        self.counted.writes += 1;
    }

    /// An input was handled: it led to `action` (described before it ran),
    /// which took `took`. Logged unless it is a key typed into the pane or
    /// into a prompt (counted), or a move of the mouse that changed nothing.
    pub fn input(
        &mut self,
        app: &mut App,
        before: Before,
        input: Input,
        action: Option<(&'static str, Map<String, Value>)>,
        took: Duration,
    ) {
        self.inputs += 1;
        let to = mode_name(app);
        let changed = to != before.mode;
        let mut fields = Map::new();
        match input {
            Input::Key(key) => {
                // Meant for the agent, dropped: counted, never named.
                if before.dropping {
                    self.counted.dropped_keys += 1;
                    return;
                }
                // Into the pane: the agent's or the editor's (`Action::Keys`,
                // `EditorKeys`, counted).
                if matches!(before.mode, "terminal" | "editor") && !changed {
                    return;
                }
                if typed_into_prompt(before.mode, key) {
                    self.counted.chars += 1;
                    if !changed {
                        return;
                    }
                }
                fields.insert("input".into(), "key".into());
                fields.insert("key".into(), key_name(key).into());
            }
            Input::Mouse(kind) => {
                if action.is_none() && !changed && kind != "click" {
                    return;
                }
                fields.insert("input".into(), kind.into());
            }
            Input::Paste(len) => {
                self.counted.pastes += 1;
                // Into the pane (`Keys`, counted) or a prompt.
                if !changed && action.is_none() {
                    return;
                }
                fields.insert("input".into(), "paste".into());
                fields.insert("len".into(), len.into());
            }
        }
        fields.insert("mode".into(), before.mode.into());
        if let Some((name, target)) = action {
            self.actions += 1;
            fields.insert("action".into(), name.into());
            fields.extend(target);
            fields.insert("ms".into(), activity::ms(took).into());
        }
        if changed {
            fields.insert("to".into(), to.into());
            self.left_mode(&mut fields, to);
        }
        self.selection(app, &mut fields, before);
        self.log.record("tui.input", Value::Object(fields));
    }

    /// Called once per loop: a mode left without an input is `tui.mode`.
    pub fn sync(&mut self, app: &mut App) {
        let to = mode_name(app);
        if to == self.mode {
            return;
        }
        let mut fields = Map::new();
        fields.insert("from".into(), self.mode.into());
        fields.insert("to".into(), to.into());
        self.left_mode(&mut fields, to);
        if let Mode::Notice { title, .. } = &app.mode {
            fields.insert("notice".into(), activity::cut(title, 200).into());
        }
        if let Some(status) = app.status_message() {
            fields.insert("status".into(), activity::cut(status, 200).into());
        }
        self.log.record("tui.mode", Value::Object(fields));
    }

    /// The mode is now `to`: say how long the one left lasted, and what
    /// was counted in it.
    fn left_mode(&mut self, fields: &mut Map<String, Value>, to: &'static str) {
        let now = Instant::now();
        fields.insert("mode_ms".into(), activity::ms(now - self.mode_since).into());
        let counted = std::mem::take(&mut self.counted);
        for (name, n) in [
            ("typed_bytes", counted.typed_bytes),
            ("writes", counted.writes),
            ("pastes", counted.pastes),
            ("chars", counted.chars),
            ("dropped_keys", counted.dropped_keys),
        ] {
            if n > 0 {
                fields.insert(name.into(), n.into());
            }
        }
        self.mode = to;
        self.mode_since = now;
    }

    /// Where the selection is after an input, and what the dashboard says.
    fn selection(&self, app: &mut App, fields: &mut Map<String, Value>, before: Before) {
        if let Some(focused) = app.focused() {
            fields.insert("id".into(), focused.id().into());
            fields.insert("on".into(), focused.label().into());
        }
        if !before.notice {
            if let Mode::Notice { title, .. } = &app.mode {
                fields.insert("notice".into(), activity::cut(title, 200).into());
            }
        }
        let status = app.status_message().map(str::to_string);
        if status.is_some() && status != before.status {
            fields.insert(
                "status".into(),
                activity::cut(status.as_deref().unwrap_or_default(), 200).into(),
            );
        }
    }
}

/// The mode's name in the log; the filter counts as one.
pub fn mode_name(app: &App) -> &'static str {
    if app.filter_active {
        return "filter";
    }
    match &app.mode {
        Mode::Normal => "normal",
        Mode::PickProject(_, Purpose::Task) => "pick_repo.task",
        Mode::PickProject(_, Purpose::Chat) => "pick_repo.chat",
        Mode::PickProject(_, Purpose::Shell) => "pick_repo.shell",
        Mode::NewTaskEnterName { .. } => "task_name",
        Mode::SpawnEnterPath(..) => "enter_path",
        Mode::PickSession(_) => "pick_session",
        Mode::ConfirmDropTask { .. } => "confirm_drop",
        Mode::ConfirmForceDrop { .. } => "confirm_force_drop",
        Mode::Notice { .. } => "notice",
        Mode::ConfirmRepairOrDrop { .. } => "repair_or_drop",
        Mode::Help => "help",
        Mode::Answer(_) => "answer",
        Mode::Terminal { .. } => "terminal",
        Mode::Scroll { .. } => "scroll",
        Mode::Editor { .. } => "editor",
        Mode::ConfirmCloseChat { .. } => "confirm_close_chat",
        Mode::ConfirmCloseShell { .. } => "confirm_close_shell",
        Mode::RenameChat { .. } => "chat_name",
        Mode::RenameShell { .. } => "shell_name",
    }
}

/// A character typed into a prompt of `mode`: text, not a command.
fn typed_into_prompt(mode: &str, key: &KeyEvent) -> bool {
    let text = matches!(
        mode,
        "filter"
            | "pick_repo.task"
            | "pick_repo.chat"
            | "pick_repo.shell"
            | "task_name"
            | "enter_path"
            | "pick_session"
            | "chat_name"
            | "shell_name"
            | "answer"
    );
    let printable = matches!(key.code, KeyCode::Char(_))
        && !key
            .modifiers
            .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT);
    text && (printable || key.code == KeyCode::Backspace)
}

/// How the log names a key: `j`, `G`, `enter`, `shift-enter`, `ctrl-]`.
pub fn key_name(key: &KeyEvent) -> String {
    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
    // Without the kitty protocol crossterm reads the bytes 0x1c to 0x1f as
    // ctrl-4 to ctrl-7: named as the keys that send them (ctrl-] is 0x1d).
    let code = match key.code {
        KeyCode::Char(c @ '4'..='7') if ctrl => KeyCode::Char(match c {
            '4' => '\\',
            '5' => ']',
            '6' => '^',
            _ => '_',
        }),
        code => code,
    };
    let mut name = String::new();
    if ctrl {
        name.push_str("ctrl-");
    }
    if key.modifiers.contains(KeyModifiers::ALT) {
        name.push_str("alt-");
    }
    // A character's case says shift already.
    if key.modifiers.contains(KeyModifiers::SHIFT) && !matches!(key.code, KeyCode::Char(_)) {
        name.push_str("shift-");
    }
    match code {
        KeyCode::Char(' ') => name.push_str("space"),
        KeyCode::Char(c) => name.push(c),
        KeyCode::Enter => name.push_str("enter"),
        KeyCode::Esc => name.push_str("esc"),
        KeyCode::Tab => name.push_str("tab"),
        KeyCode::BackTab => name.push_str("backtab"),
        KeyCode::Backspace => name.push_str("backspace"),
        KeyCode::Delete => name.push_str("delete"),
        KeyCode::Up => name.push_str("up"),
        KeyCode::Down => name.push_str("down"),
        KeyCode::Left => name.push_str("left"),
        KeyCode::Right => name.push_str("right"),
        KeyCode::Home => name.push_str("home"),
        KeyCode::End => name.push_str("end"),
        KeyCode::PageUp => name.push_str("pageup"),
        KeyCode::PageDown => name.push_str("pagedown"),
        KeyCode::F(n) => name.push_str(&format!("f{n}")),
        other => name.push_str(&format!("{other:?}").to_lowercase()),
    }
    name
}

/// What the log says of an action: its name and target. `None`: not one
/// (`None`), or counted (`Keys`).
pub fn describe(action: &Action) -> Option<(&'static str, Map<String, Value>)> {
    let (name, fields) = match action {
        Action::None | Action::Keys(_) | Action::EditorKeys(_) => return None,
        Action::Quit => ("quit", json!({})),
        Action::SelectNext => ("select_next", json!({})),
        Action::SelectPrev => ("select_prev", json!({})),
        Action::Copy { text, cut } => {
            ("copy", json!({ "chars": text.chars().count(), "cut": cut }))
        }
        // `ms`: how long it was attached.
        Action::Attach { id, .. } => ("attach", json!({ "target": id, "wait": true })),
        Action::Resume { id, attach, .. } => (
            "resume",
            json!({ "target": id, "attach": attach, "wait": attach.then_some(true) }),
        ),
        Action::CloseChat { id, .. } => ("close_chat", json!({ "target": id })),
        Action::OpenShell { on } => {
            let (member, dir) = match on {
                tam_proto::ShellPlace::Member(m) => (Some(m), None),
                tam_proto::ShellPlace::Checkout(d) | tam_proto::ShellPlace::Dir(d) => {
                    (None, Some(d))
                }
            };
            ("open_shell", json!({ "member": member, "dir": dir }))
        }
        Action::CloseShell { id, .. } => ("close_shell", json!({ "target": id })),
        Action::KeepChat { id, keep, .. } => ("keep_chat", json!({ "target": id, "keep": keep })),
        Action::RenameChat { id, name, .. } => {
            ("rename_chat", json!({ "target": id, "name": name }))
        }
        Action::RenameShell { id, name, .. } => {
            ("rename_shell", json!({ "target": id, "name": name }))
        }
        Action::StopAgent { id, .. } => ("stop", json!({ "target": id })),
        Action::DropTask {
            name,
            force,
            delete_branch,
            ..
        } => (
            "drop",
            json!({ "target": name, "force": force, "delete_branch": delete_branch }),
        ),
        Action::PickProject(purpose) => (
            "pick_repo",
            json!({ "purpose": format!("{purpose:?}").to_lowercase() }),
        ),
        Action::ChatHistory { dir } => ("chat_history", json!({ "dir": dir })),
        Action::CreateChat { dir, session } => (
            "create_chat",
            json!({ "dir": dir, "session": session.is_some() }),
        ),
        Action::RunAgent { member } => ("run", json!({ "target": member })),
        Action::DoNewTask { name, dirs } => ("new_task", json!({ "task": name, "repos": dirs })),
        Action::CustomCommand { name, id, .. } => {
            ("custom_command", json!({ "command": name, "target": id }))
        }
        Action::RunNewSession { member } => ("run_new_session", json!({ "target": member })),
        Action::RepairTask { name } => ("repair", json!({ "target": name })),
        Action::ScrollPeek(scroll) => (
            "scroll",
            json!({ "scroll": format!("{scroll:?}").to_lowercase() }),
        ),
        Action::OpenEditor { agent } => ("editor", json!({ "target": agent })),
        Action::NextTheme => ("next_theme", json!({})),
        Action::Answer {
            agent, ask, answer, ..
        } => (
            "answer",
            json!({
                "target": agent,
                "ask": ask,
                "answer": activity::answer_kind(answer),
            }),
        ),
    };
    let fields = match fields {
        Value::Object(map) => map,
        _ => Map::new(),
    };
    Some((name, fields))
}

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

    fn key(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
        KeyEvent::new(code, modifiers)
    }

    fn char_key(c: char) -> KeyEvent {
        key(KeyCode::Char(c), KeyModifiers::NONE)
    }

    fn recorder() -> (Recorder, &'static Activity) {
        let log: &'static Activity = Box::leak(Box::new(Activity::in_memory("tui")));
        (Recorder::new(log), log)
    }

    fn lines(log: &Activity) -> Vec<Value> {
        log.lines()
            .iter()
            .map(|l| serde_json::from_str(l).unwrap())
            .collect()
    }

    /// `key` came while in the app's mode; the key's handler left it in
    /// `then`.
    fn press(rec: &mut Recorder, app: &mut App, key: KeyEvent, then: Mode) {
        let before = rec.before(app);
        app.mode = then;
        rec.input(app, before, Input::Key(&key), None, Duration::ZERO);
    }

    #[test]
    fn an_input_says_the_mode_it_left_and_typing_is_counted() {
        let (mut rec, log) = recorder();
        let mut app = App::new();
        press(&mut rec, &mut app, char_key('?'), Mode::Help);
        press(&mut rec, &mut app, char_key('x'), Mode::Normal);
        let terminal = || Mode::Terminal {
            agent: "m-1".into(),
        };
        press(
            &mut rec,
            &mut app,
            key(KeyCode::Enter, KeyModifiers::NONE),
            terminal(),
        );
        // Typed into the pane: counted, never logged.
        for c in "secret".chars() {
            let before = rec.before(&mut app);
            rec.typed(1);
            rec.input(
                &mut app,
                before,
                Input::Key(&char_key(c)),
                None,
                Duration::ZERO,
            );
        }
        rec.wrote();
        let leave = key(KeyCode::Char(']'), KeyModifiers::CONTROL);
        press(&mut rec, &mut app, leave, Mode::Normal);

        let lines = lines(log);
        assert_eq!(lines.len(), 4, "{lines:#?}");
        assert!(lines.iter().all(|l| l["kind"] == "tui.input"));
        assert_eq!(lines[0]["key"], "?");
        assert_eq!(lines[0]["mode"], "normal");
        assert_eq!(lines[0]["to"], "help");
        assert_eq!(lines[1]["mode"], "help");
        assert_eq!(lines[1]["to"], "normal");
        assert!(lines[1]["mode_ms"].is_number());
        assert_eq!(lines[2]["to"], "terminal");
        assert_eq!(lines[3]["key"], "ctrl-]");
        assert_eq!(lines[3]["mode"], "terminal");
        assert_eq!(lines[3]["typed_bytes"], 6);
        assert_eq!(lines[3]["writes"], 1);
        assert!(!log.lines().concat().contains("\"s\""));
    }

    #[test]
    fn keys_typed_into_nvim_are_counted_never_named() {
        let (mut rec, log) = recorder();
        let mut app = App::new();
        // `v`: the editor opened.
        let before = rec.before(&mut app);
        let open = Action::OpenEditor {
            agent: "m-1".into(),
        };
        app.mode = Mode::Editor {
            agent: "m-1".into(),
        };
        let v = char_key('v');
        rec.input(
            &mut app,
            before,
            Input::Key(&v),
            describe(&open),
            Duration::ZERO,
        );
        for c in "/secret".chars() {
            let before = rec.before(&mut app);
            assert!(!before.dropping);
            let typed = Action::EditorKeys(vec![c as u8]);
            assert!(describe(&typed).is_none());
            rec.typed(1);
            rec.input(
                &mut app,
                before,
                Input::Key(&char_key(c)),
                None,
                Duration::ZERO,
            );
        }
        rec.wrote();
        // `:q`: nvim exited, the mode left without an input of its own.
        app.mode = Mode::Normal;
        rec.sync(&mut app);

        let lines = lines(log);
        assert_eq!(lines.len(), 2, "{lines:#?}");
        assert_eq!(lines[0]["action"], "editor");
        assert_eq!(lines[0]["target"], "m-1");
        assert_eq!(
            (&lines[0]["mode"], &lines[0]["to"]),
            (&"normal".into(), &"editor".into())
        );
        assert_eq!(lines[1]["kind"], "tui.mode");
        assert_eq!(
            (&lines[1]["from"], &lines[1]["to"]),
            (&"editor".into(), &"normal".into())
        );
        assert_eq!(lines[1]["typed_bytes"], 7);
        assert!(!log.lines().concat().contains("secret"));
        assert!(!log.lines().concat().contains("\"s\""));
    }

    #[test]
    fn keys_dropped_after_the_pane_gave_the_keyboard_back_are_not_named() {
        let (mut rec, log) = recorder();
        let mut app = App::new();
        app.mode = Mode::Terminal {
            agent: "sh-1".into(),
        };
        rec.sync(&mut app);
        // The shell closed: the pane gave the keyboard back, unasked.
        app.stop_typing("the shell closed");
        rec.sync(&mut app);
        for c in "secret".chars() {
            let before = rec.before(&mut app);
            assert!(before.dropping);
            rec.input(
                &mut app,
                before,
                Input::Key(&char_key(c)),
                None,
                Duration::ZERO,
            );
        }
        // Typing paused: keys are the dashboard's again.
        app.dropping_keys_until = None;
        press(&mut rec, &mut app, char_key('?'), Mode::Help);
        let lines = lines(log);
        assert!(
            !lines
                .iter()
                .any(|l| ["s", "e", "c", "r", "t"].iter().any(|k| l["key"] == *k)),
            "{lines:#?}"
        );
        let help = lines.last().unwrap();
        assert_eq!(help["key"], "?");
        assert_eq!(help["dropped_keys"], 6);
    }

    #[test]
    fn text_typed_into_a_prompt_is_counted_not_logged() {
        let (mut rec, log) = recorder();
        let mut app = App::new();
        app.filter_active = true;
        rec.sync(&mut app);
        for c in "abc".chars() {
            press(&mut rec, &mut app, char_key(c), Mode::Normal);
        }
        let before = rec.before(&mut app);
        app.filter_active = false;
        let esc = key(KeyCode::Esc, KeyModifiers::NONE);
        rec.input(&mut app, before, Input::Key(&esc), None, Duration::ZERO);
        let lines = lines(log);
        assert_eq!(lines.len(), 2, "{lines:#?}");
        // Opened by something else than an input this recorder saw.
        assert_eq!(lines[0]["kind"], "tui.mode");
        assert_eq!(lines[0]["to"], "filter");
        assert_eq!(lines[1]["key"], "esc");
        assert_eq!(lines[1]["mode"], "filter");
        assert_eq!(lines[1]["chars"], 3);
    }

    #[test]
    fn an_action_says_its_target_and_how_long_it_took() {
        let (mut rec, log) = recorder();
        let mut app = App::new();
        let before = rec.before(&mut app);
        let action = Action::StopAgent {
            id: "m-1".into(),
            label: "fix".into(),
        };
        app.set_status("Stopped fix".into(), Duration::from_secs(3));
        rec.input(
            &mut app,
            before,
            Input::Key(&char_key('x')),
            describe(&action),
            Duration::from_millis(12),
        );
        let lines = lines(log);
        assert_eq!(lines[0]["action"], "stop");
        assert_eq!(lines[0]["target"], "m-1");
        assert_eq!(lines[0]["ms"], 12.0);
        assert_eq!(lines[0]["status"], "Stopped fix");
        assert!(lines[0].get("to").is_none());
    }

    #[test]
    fn keys_are_named() {
        assert_eq!(key_name(&key(KeyCode::Char('j'), KeyModifiers::NONE)), "j");
        assert_eq!(key_name(&key(KeyCode::Char('G'), KeyModifiers::SHIFT)), "G");
        assert_eq!(
            key_name(&key(KeyCode::Enter, KeyModifiers::SHIFT)),
            "shift-enter"
        );
        assert_eq!(
            key_name(&key(KeyCode::Char(']'), KeyModifiers::CONTROL)),
            "ctrl-]"
        );
        assert_eq!(
            key_name(&key(KeyCode::Char('5'), KeyModifiers::CONTROL)),
            "ctrl-]"
        );
        assert_eq!(
            key_name(&key(KeyCode::Char('4'), KeyModifiers::CONTROL)),
            "ctrl-\\"
        );
        assert_eq!(
            key_name(&key(KeyCode::Char('u'), KeyModifiers::CONTROL)),
            "ctrl-u"
        );
        assert_eq!(
            key_name(&key(KeyCode::Char(' '), KeyModifiers::NONE)),
            "space"
        );
    }

    #[test]
    fn text_typed_into_prompts_is_not_a_command() {
        let c = key(KeyCode::Char('x'), KeyModifiers::NONE);
        assert!(typed_into_prompt("filter", &c));
        assert!(typed_into_prompt("chat_name", &c));
        assert!(typed_into_prompt("answer", &c));
        assert!(!typed_into_prompt("normal", &c));
        assert!(!typed_into_prompt("confirm_drop", &c));
        let enter = key(KeyCode::Enter, KeyModifiers::NONE);
        assert!(!typed_into_prompt("filter", &enter));
        let ctrl_u = key(KeyCode::Char('u'), KeyModifiers::CONTROL);
        assert!(!typed_into_prompt("filter", &ctrl_u));
    }

    #[test]
    fn actions_say_no_text() {
        let answer = Action::Answer {
            agent: "m-1".into(),
            ask: "ask-1".into(),
            answer: tam_proto::AskAnswer::Deny {
                reason: Some("SECRET".into()),
            },
            label: "t".into(),
            what: "SECRET".into(),
        };
        let (name, fields) = describe(&answer).unwrap();
        assert_eq!(name, "answer");
        assert_eq!(fields["answer"], "deny_with_reason");
        assert!(!Value::Object(fields).to_string().contains("SECRET"));
        let copy = Action::Copy {
            text: "SECRET".into(),
            cut: false,
        };
        let (_, fields) = describe(&copy).unwrap();
        assert_eq!(fields["chars"], 6);
        assert!(!Value::Object(fields).to_string().contains("SECRET"));
        let custom = Action::CustomCommand {
            name: "open".into(),
            command: "SECRET {dir}".into(),
            task: "t".into(),
            id: "m-1".into(),
            dir: "/x".into(),
            provider: "claude".into(),
        };
        let (_, fields) = describe(&custom).unwrap();
        assert!(!Value::Object(fields).to_string().contains("SECRET"));
        assert!(describe(&Action::Keys(b"SECRET".to_vec())).is_none());
    }
}