synaps-tui 0.10.2

Terminal UI layer — ratatui, crossterm, syntect, tachyonfx
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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
//! Input handling — keyboard events, cursor movement, paste, mouse scroll.

use std::sync::Arc;

use super::app::{App, ChatMessage};
use crossterm::event::{Event, KeyCode, KeyModifiers, MouseButton, MouseEventKind};
use synaps_cli::skills::registry::CommandRegistry;

/// What the event loop should do after processing input.
pub(super) enum InputAction {
    /// Nothing special — continue the loop.
    None,
    /// User submitted text (non-slash) — contains the raw input string.
    Submit(String),
    /// User submitted a slash command — (resolved_cmd, arg).
    SlashCommand(String, String),
    /// User submitted input while streaming — contains the raw input string.
    StreamingInput(String),
    /// Start the quit animation.
    Quit,
    /// Abort the current stream (Esc during streaming).
    Abort,
    /// Settings modal requested an apply — (key, value).
    SettingsApply(&'static str, String),
    /// Models modal requested switching to a runtime model id.
    ModelsApply(String),
    /// Models modal "trust" action → `Set{GrantWorkerModel}` (result ignored,
    /// as the direct `grant_worker_model` call was).
    GrantWorkerModel(String),
    /// Effort lightbox requested applying a reasoning level (string form).
    /// The dispatch arm re-checks streaming + exact-model validity
    /// (`effort::apply_guard`) before any mutation/persist.
    EffortApply(super::effort::EffortApply),
    /// Models modal requested expanding provider models.
    ModelsExpandProvider(String),
    /// Plugins modal emitted an outcome — handled in the async main loop
    /// because most variants perform async I/O (network, filesystem).
    PluginsOutcome(super::plugins::InputOutcome),
    /// /help find lightbox emitted an outcome.
    HelpFindOutcome,
    /// Settings modal asked to open the plugins marketplace as a nested overlay.
    OpenPluginsMarketplace,
    PingModels,
    /// Open a plugin-owned custom settings editor via `settings.editor.open`.
    PluginEditorOpen {
        plugin_id: String,
        category: String,
        field: String,
    },
    /// Forward a keypress to the active plugin-owned custom settings editor.
    PluginEditorKey {
        plugin_id: String,
        category: String,
        field: String,
        key: crossterm::event::KeyEvent,
    },
}

/// Process a crossterm Event and return what the main loop should do.
///
/// P7.8: modal routing is now FULLY stack-driven. `handle_event` dispatches on
/// `modal_stack.top()`: the empty-stack `Chat` case is the base pane
/// (`handle_event_inner`), every modal has its own stack-routed pane handler,
/// and the async SecretPrompt is folded in (§5). The legacy if-let modal chain
/// is gone. BOTH the base path and every pane-handler path (including their pop
/// paths) converge on `action` so the debug-only stack/app sync tripwire always
/// runs afterwards.
pub(super) fn handle_event(
    event: Event,
    app: &mut App,
    runtime: &impl agent_engine::session::RuntimeRead,
    streaming: bool,
    registry: &Arc<CommandRegistry>,
    keybinds: &synaps_cli::skills::keybinds::KeybindRegistry,
    scroll_lines: u16,
) -> InputAction {
    // Key-release filter: Windows' console (and any terminal negotiating the
    // kitty keyboard protocol) delivers BOTH Press and Release events for
    // every keystroke; Unix legacy input delivers Press only. The chat
    // textarea already ignores Release internally (tui-textarea), but the
    // modal pane handlers below act on raw `Event::Key` — without this gate
    // every keypress in /models, /settings, etc. fires twice on Windows.
    // Repeat is deliberately kept (held-key navigation).
    if let Event::Key(k) = &event {
        if k.kind == crossterm::event::KeyEventKind::Release {
            return InputAction::None;
        }
    }

    // P7.8: stack-driven routing — one arm per pane, no fall-through chain.
    // `Chat` (empty stack) is the base pane; every modal + the folded-in
    // SecretPrompt has its own handler. The match is exhaustive over `PaneId`.
    let action = match app.modal_stack.top() {
        super::focus::PaneId::Chat => {
            handle_event_inner(event, app, streaming, registry, keybinds, scroll_lines)
        }
        super::focus::PaneId::HelpFind => route_help_find(event, app),
        super::focus::PaneId::Effort => route_effort(event, app),
        super::focus::PaneId::Models => route_models(event, app, runtime),
        super::focus::PaneId::Plugins => route_plugins(event, app),
        super::focus::PaneId::Settings => route_settings(event, app, runtime, registry),
        super::focus::PaneId::PluginEditor => route_settings(event, app, runtime, registry),
        super::focus::PaneId::SecretPrompt => route_secret_prompt(event, app),
    };

    // Stack/app-field sync tripwire (§3 contract 4) — debug/test builds only.
    // Runs after BOTH the chain path and the pane-handler pop path, so a missed
    // push/pop fails the harness loudly instead of misrouting silently.
    #[cfg(debug_assertions)]
    super::focus::debug_assert_stack_sync(app);

    action
}

/// Base Chat pane handler: keyboard / mouse / paste for the transcript + input
/// box. Reached from `handle_event` when `modal_stack.top() == Chat` (empty
/// stack). All modals — and the async secret prompt — are stack-routed via
/// their own pane handlers (P7.8); the legacy if-let modal chain no longer
/// exists, so `runtime` (its sole ex-consumer was the settings arm, now in
/// `route_settings`) is no longer threaded here.
fn handle_event_inner(
    event: Event,
    app: &mut App,
    streaming: bool,
    registry: &Arc<CommandRegistry>,
    keybinds: &synaps_cli::skills::keybinds::KeybindRegistry,
    scroll_lines: u16,
) -> InputAction {
    if matches!(event, Event::Key(_) | Event::Paste(_)) {
        let now = app.clock.now();
        app.prompt_clock.touch(now);
    }
    match event {
        Event::Key(key) => handle_key(key, app, streaming, registry, keybinds),
        Event::Mouse(mouse) => handle_mouse(mouse, app, scroll_lines),
        Event::Paste(text) => {
            // Suppress paste events that fire immediately after a right-click copy.
            // Some terminals send both a Mouse(Down(Right)) AND an Event::Paste
            // when the user right-clicks, causing unintended paste into the input box.
            if let Some(deadline) = app.suppress_paste_until {
                if app.clock.now() < deadline {
                    app.suppress_paste_until = None;
                    return InputAction::None;
                }
                app.suppress_paste_until = None;
            }
            const MAX_PASTE_CHARS: usize = 100_000;
            // Paste is allowed regardless of streaming state — typing into the
            // input during streaming already works (queued submit), so paste
            // must behave the same. Previously gated on !streaming, which
            // silently ate pastes into an empty input mid-stream.
            let text = if text.chars().count() > MAX_PASTE_CHARS {
                let truncated: String = text.chars().take(MAX_PASTE_CHARS).collect();
                app.push_msg(ChatMessage::System(format!(
                    "Paste truncated to {} chars (was {})",
                    MAX_PASTE_CHARS,
                    text.chars().count()
                )));
                truncated
            } else {
                text
            };
            if app.input_before_paste.is_none() {
                app.input_before_paste = Some(app.input_text());
            }
            // Terminals (and tmux) convert \n to \r inside bracketed paste —
            // normalize so multi-line pastes actually become multiple lines.
            let text = text.replace("\r\n", "\n").replace('\r', "\n");
            app.insert_at_cursor(&text);
            app.pasted_char_count += text.chars().count();
            InputAction::None
        }
        _ => InputAction::None,
    }
}

/// Handle mouse events: scroll, text selection (left drag), right-click copy/paste.
fn handle_mouse(
    mouse: crossterm::event::MouseEvent,
    app: &mut App,
    scroll_lines: u16,
) -> InputAction {
    // Selection events may need to promote a demoted slot on demand (P11
    // lock L3: wheel + click in one input batch maps into rows demoted as of
    // the last frame). The re-render crosses the seam via RenderCtx, same as
    // the draw path — field borrows are disjoint from `app.transcript`.
    let ctx = super::transcript::RenderCtx {
        spinner_frame: app.spinner_frame,
        streaming: app.streaming,
        agent_name: &app.agent_name,
    };
    match mouse.kind {
        // Wheel scroll no longer clears the selection (P10 lock L4):
        // endpoints are content-relative, so the selection scrolls with the
        // content and the highlight clamps to the window. Keypresses still
        // clear (handle_key top) — including Shift+Up/Down keyboard scroll.
        MouseEventKind::ScrollUp => {
            app.transcript.scroll_up(scroll_lines);
        }
        MouseEventKind::ScrollDown => {
            app.transcript.scroll_down(scroll_lines);
        }

        // Left-click starts a new selection (clears any existing one)
        MouseEventKind::Down(MouseButton::Left) => {
            // Only start selection if click is inside the message area
            if app.transcript.hit_test(mouse.column, mouse.row) {
                app.transcript
                    .selection_begin(mouse.column, mouse.row, &ctx);
            } else {
                app.transcript.clear_selection();
            }
        }

        // Left-drag extends the selection (no-op without an anchor)
        MouseEventKind::Drag(MouseButton::Left) => {
            app.transcript.selection_drag(mouse.column, mouse.row, &ctx);
        }

        // Left-release finalizes the selection (click == anchor ⇒ clear)
        MouseEventKind::Up(MouseButton::Left) => {
            app.transcript
                .selection_release(mouse.column, mouse.row, &ctx);
        }

        // Right-click: copy if selection exists, paste if not
        MouseEventKind::Down(MouseButton::Right) => {
            if app.transcript.has_selection() {
                // Copy selected text to clipboard — right-click with selection is COPY ONLY
                if let Some(text) = app.transcript.selected_text() {
                    copy_to_clipboard(&text);
                    app.push_msg(ChatMessage::System(format!(
                        "Copied {} chars",
                        text.chars().count()
                    )));
                }
                // Suppress any terminal-generated paste event that follows this right-click
                app.suppress_paste_until =
                    Some(app.clock.now() + std::time::Duration::from_millis(150));
                // Clear selection after copy
                app.transcript.clear_selection();
            } else {
                // No selection — paste from clipboard at cursor position
                if let Some(text) = paste_from_clipboard() {
                    if !text.is_empty() {
                        if app.input_before_paste.is_none() {
                            app.input_before_paste = Some(app.input_text());
                        }
                        app.insert_at_cursor(&text);
                        app.pasted_char_count += text.chars().count();
                    }
                }
                // Suppress the terminal-generated paste event that follows this right-click
                app.suppress_paste_until =
                    Some(app.clock.now() + std::time::Duration::from_millis(150));
            }
        }

        _ => {}
    }
    InputAction::None
}

/// Copy text to system clipboard. Uses a singleton background thread that
/// holds one clipboard handle for the lifetime of the app. New copies replace
/// the previous content atomically — no thread accumulation, no races.
fn copy_to_clipboard(text: &str) {
    use std::sync::{mpsc, OnceLock};
    static TX: OnceLock<mpsc::Sender<String>> = OnceLock::new();
    let sender = TX.get_or_init(|| {
        let (tx, rx) = mpsc::channel::<String>();
        std::thread::spawn(move || {
            let Ok(mut clipboard) = arboard::Clipboard::new() else {
                return;
            };
            while let Ok(text) = rx.recv() {
                let _ = clipboard.set_text(&text);
            }
        });
        tx
    });
    let _ = sender.send(text.to_string());
}

/// Read text from system clipboard. Returns None if clipboard is empty or inaccessible.
fn paste_from_clipboard() -> Option<String> {
    if let Ok(mut clipboard) = arboard::Clipboard::new() {
        if let Ok(text) = clipboard.get_text() {
            if !text.is_empty() {
                return Some(text);
            }
        }
    }
    None
}

/// Handle a key event on the base Chat pane.
///
/// Interception contract (hybrid plan §3.2): app-level keys — quit, abort,
/// submit, tab-completion, transcript scroll, Ctrl-O, Ctrl-U (clear whole
/// buffer), history-at-edges Up/Down (§3.3) — are matched here with today's
/// exact semantics. Every other editing key is forwarded to the TextArea
/// editor, which is the sole input-buffer state.
fn handle_key(
    key: crossterm::event::KeyEvent,
    app: &mut App,
    streaming: bool,
    registry: &Arc<CommandRegistry>,
    keybinds: &synaps_cli::skills::keybinds::KeybindRegistry,
) -> InputAction {
    let (code, modifiers) = (key.code, key.modifiers);
    // Clear text selection on any keypress (typing dismisses selection)
    app.transcript.clear_selection();
    // Any non-Tab key resets the tab-completion cycle state. (Tab handler
    // below returns early after setting its own cycle state.)
    if !matches!(code, KeyCode::Tab) {
        app.tab_cycle = None;
    }

    // Plugin/user keybinds — check before core binds, but only when not streaming
    if !streaming {
        // Trace modifier-heavy or special keys to help debug "key X not
        // working" issues across terminals. Plain typing isn't logged.
        let traceable = matches!(code, KeyCode::F(_))
            || modifiers.contains(KeyModifiers::CONTROL)
            || modifiers.contains(KeyModifiers::ALT);
        if traceable {
            tracing::info!(?code, ?modifiers, "key event received in chatui input");
        }
        if let Some(bind) = keybinds.match_key(code, modifiers) {
            use synaps_cli::skills::keybinds::KeybindAction;
            return match &bind.action {
                KeybindAction::SlashCommand(cmd) => {
                    let parts: Vec<&str> = cmd.splitn(2, ' ').collect();
                    let resolved = super::commands::resolve_prefix(
                        parts[0],
                        &super::commands::all_commands_with_skills(registry),
                    );
                    InputAction::SlashCommand(resolved, parts.get(1).unwrap_or(&"").to_string())
                }
                KeybindAction::LoadSkill(skill) => {
                    InputAction::SlashCommand("load".to_string(), skill.clone())
                }
                KeybindAction::InjectPrompt(text) => InputAction::Submit(text.clone()),
                KeybindAction::Disabled => InputAction::None,
                KeybindAction::RunScript { .. } => {
                    // TODO: execute script and inject output
                    InputAction::None
                }
            };
        }
    }
    match (code, modifiers) {
        (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
            // E-P8: Ctrl-C while the driver is armed → stop automation
            // (actor revokes on `Cancel`), not quit. Cancellation of the
            // grant outranks the quit binding while armed.
            if app.driver_ui.is_armed() {
                return InputAction::Abort;
            }
            return InputAction::Quit;
        }
        // E-P8: Esc while armed (even when NOT streaming) → stop automation.
        (KeyCode::Esc, _) if streaming || app.driver_ui.is_armed() => {
            return InputAction::Abort;
        }
        (KeyCode::Enter, KeyModifiers::SHIFT) if !streaming => {
            app.editor.insert_newline();
        }
        // Alt-Enter: same as Shift-Enter. Legacy terminals (and tmux) encode
        // it as ESC+CR, which survives everywhere — Shift-Enter needs the
        // kitty keyboard protocol and often arrives as plain Enter.
        (KeyCode::Enter, KeyModifiers::ALT) if !streaming => {
            app.editor.insert_newline();
        }
        (KeyCode::Enter, _) if !streaming && !app.input_is_empty() => {
            return process_submit(app, registry);
        }
        // Enter with empty buffer but staged attachments → attachment-only submit.
        (KeyCode::Enter, _) if !streaming && app.input_is_empty() && !app.pending_attachments.is_empty() => {
            return process_submit_attachment_only(app);
        }
        (KeyCode::Enter, _) if streaming && !app.input_is_empty() => {
            return process_streaming_submit(app);
        }
        // Enter that matched none of the above (empty buffer) stays a no-op.
        // It must NOT reach the editor, which would insert a newline.
        (KeyCode::Enter, _) => {}
        (KeyCode::Tab, _)
            if app.input_first_line().starts_with('/') && app.input_first_line().len() > 1 =>
        {
            if open_help_find_for_ambiguous_slash(app, registry) {
                return InputAction::HelpFindOutcome;
            }
            handle_tab_complete(app, registry);
            // Skip the tab_cycle reset below — we just set it.
            return InputAction::None;
        }
        // Tab outside slash-completion was a no-op before; the editor would
        // insert a literal tab char. Keep the no-op.
        (KeyCode::Tab, _) => {}
        // Esc outside streaming: no-op, never forwarded.
        (KeyCode::Esc, _) => {}
        // Ctrl-U clears the WHOLE buffer (today's semantics). This shadows the
        // fork's own Ctrl-U binding (undo) — so undo/redo get explicit,
        // conventional binds below.
        (KeyCode::Char('u'), KeyModifiers::CONTROL) => {
            app.clear_input();
        }
        // Undo/redo: the fork's defaults (Ctrl-U undo) are shadowed/unmapped;
        // bind the conventional keys explicitly. Ctrl-R also reaches the
        // editor's redo via the catch-all.
        (KeyCode::Char('z'), KeyModifiers::CONTROL) => {
            app.editor.undo();
        }
        (KeyCode::Char('y'), KeyModifiers::CONTROL) => {
            app.editor.redo();
        }
        (KeyCode::Char('o'), KeyModifiers::CONTROL) => {
            // Store-owned toggle invalidates internally (locked decision #1);
            // App only signals the frame scheduler.
            app.transcript
                .set_show_full_output(!app.transcript.show_full_output());
            app.request_redraw();
        }
        // Alt-←/→ word jumps: the lib maps Alt-b/f and Ctrl-←/→ but leaves
        // plain Alt-←/→ unmapped — keep today's semantics via explicit moves.
        (KeyCode::Left, KeyModifiers::ALT) => {
            app.editor.move_cursor(tui_textarea::CursorMove::WordBack);
        }
        (KeyCode::Right, KeyModifiers::ALT) => {
            app.editor
                .move_cursor(tui_textarea::CursorMove::WordForward);
        }
        (KeyCode::Up, KeyModifiers::SHIFT) => {
            app.transcript.scroll_up(1);
        }
        (KeyCode::Down, KeyModifiers::SHIFT) => {
            app.transcript.scroll_down(1);
        }
        // Up/Down: history only at buffer edges (§3.3 DECIDED); inside a
        // multi-line buffer they move the cursor.
        (KeyCode::Up, _) if app.editor.cursor().0 == 0 => {
            app.history_up();
        }
        (KeyCode::Up, _) => {
            app.editor.input(key);
        }
        (KeyCode::Down, _) if app.editor.cursor().0 + 1 >= app.editor.lines().len() => {
            app.history_down();
        }
        (KeyCode::Down, _) => {
            app.editor.input(key);
        }
        // Everything else — chars, Backspace/Delete, ←/→, Home/End,
        // Ctrl-A/E/W/K, Alt-Backspace, undo/redo — is the editor's job.
        _ => {
            app.editor.input(key);
        }
    }
    InputAction::None
}

/// User pressed Enter with non-empty input while not streaming.
fn process_submit(app: &mut App, registry: &Arc<CommandRegistry>) -> InputAction {
    let now = app.clock.now();
    app.prompt_clock.sent(now);
    if app.transcript.is_empty() {
        app.logo_dismiss_t = Some(0.001);
    }
    let input = app.input_text();
    app.input_history.push(input.clone());
    app.history_index = None;
    app.input_stash.clear();
    app.clear_input();
    app.transcript.scroll_to_bottom();

    if input.starts_with('/') && input.len() > 1 {
        let parts: Vec<&str> = input[1..].splitn(2, char::is_whitespace).collect();
        let raw_cmd = parts[0];
        let arg = parts.get(1).map(|s| s.trim()).unwrap_or("").to_string();
        let commands = super::commands::all_commands_with_skills(registry);
        let cmd = super::commands::resolve_prefix(raw_cmd, &commands);
        InputAction::SlashCommand(cmd, arg)
    } else {
        InputAction::Submit(input)
    }
}

/// User pressed Enter with empty input but staged attachments.
fn process_submit_attachment_only(app: &mut App) -> InputAction {
    let now = app.clock.now();
    app.prompt_clock.sent(now);
    if app.transcript.is_empty() {
        app.logo_dismiss_t = Some(0.001);
    }
    app.transcript.scroll_to_bottom();
    InputAction::Submit(String::new())
}

/// User pressed Enter with non-empty input while streaming.
fn process_streaming_submit(app: &mut App) -> InputAction {
    let now = app.clock.now();
    app.prompt_clock.sent(now);
    let input = app.input_text();
    app.input_history.push(input.clone());
    app.history_index = None;
    app.input_stash.clear();
    app.clear_input();
    app.input_before_paste = None;
    app.pasted_char_count = 0;

    InputAction::StreamingInput(input)
}

/// P7.4 stack-routed pane handler for the `/help find` lightbox.
///
/// Performs exactly what the deleted legacy chain arm did (byte-identical
/// dispatch to `help_find::handle_event`), and additionally POPS the modal
/// stack when the lightbox closes — keeping `modal_stack.contains(HelpFind)`
/// in sync with `app.help_find.is_some()` (asserted by
/// `debug_assert_stack_sync`). Returns the loop's existing `InputAction`
/// directly: help_find never coexists with another modal (§6), so no
/// `PaneOutcome` indirection is required.
fn route_help_find(event: Event, app: &mut App) -> InputAction {
    // Invariant (checked by the tripwire): top() == HelpFind ⇒ help_find is Some.
    let Some(state) = &mut app.help_find else {
        return InputAction::None;
    };
    if let Event::Key(key) = event {
        return match super::help_find::handle_event(state, key) {
            super::help_find::HelpFindAction::Close => {
                app.help_find = None;
                app.modal_stack.pop();
                InputAction::None
            }
            super::help_find::HelpFindAction::None => InputAction::HelpFindOutcome,
        };
    }
    InputAction::None
}

/// Stack-routed pane handler for the `/effort` lightbox.
///
/// Outcome translation mirrors `route_models`: `Close` → clear field + pop
/// (cancel — nothing applied); `Apply` → clear field + pop, then defer the
/// guarded apply to the async loop (`InputAction::EffortApply`); `None` →
/// consumed. The modal closes on BOTH paths, so the race-safe apply decision
/// lives entirely in the dispatch guard.
fn route_effort(event: Event, app: &mut App) -> InputAction {
    // Invariant (checked by the tripwire): top() == Effort ⇒ effort is Some.
    let Some(state) = &mut app.effort else {
        return InputAction::None;
    };
    if let Event::Key(key) = event {
        return match super::effort::handle_event(state, key) {
            super::effort::InputOutcome::Close => {
                app.effort = None;
                app.modal_stack.pop();
                InputAction::None
            }
            super::effort::InputOutcome::Apply(level) => {
                app.effort = None;
                app.modal_stack.pop();
                InputAction::EffortApply(level)
            }
            super::effort::InputOutcome::None => InputAction::None,
        };
    }
    InputAction::None
}

/// P7.5 stack-routed pane handler for the `/model` · `/models` modal.
///
/// Performs exactly what the deleted legacy chain arm did (byte-identical
/// dispatch to `models::handle_event`), and additionally POPS the modal stack
/// on the two close paths — keeping `modal_stack.contains(Models)` in sync
/// with `app.models.is_some()` (asserted by `debug_assert_stack_sync`).
///
/// Outcome translation (§7 P7.5): `Close` → `PaneOutcome::Pop` (clear the
/// field and pop, return `None`); `Apply` → `PaneOutcome::PopThen(ModelsApply)`
/// (clear the field and pop, then defer the apply to the async loop);
/// `ExpandProvider` → `PaneOutcome::Action(ModelsExpandProvider)` (defer only,
/// modal stays open, no pop); `None` → `Consumed`. Models never coexists with
/// another modal (§6), so the `InputAction` is returned directly — the
/// `PaneOutcome` mapping above is realized inline, matching the P7.4
/// `route_help_find` shape.
fn route_models(
    event: Event,
    app: &mut App,
    runtime: &impl agent_engine::session::RuntimeRead,
) -> InputAction {
    // Invariant (checked by the tripwire): top() == Models ⇒ models is Some.
    let Some(state) = &mut app.models else {
        return InputAction::None;
    };
    if let Event::Key(key) = event {
        return match super::models::handle_event(state, key, runtime.model()) {
            super::models::InputOutcome::Close => {
                app.models = None;
                app.modal_stack.pop();
                InputAction::None
            }
            super::models::InputOutcome::Apply(model) => {
                app.models = None;
                app.modal_stack.pop();
                InputAction::ModelsApply(model)
            }
            super::models::InputOutcome::None => InputAction::None,
            super::models::InputOutcome::Trusted(model) => {
                // Explicit user trust action: extend the live delegation policy
                // so subagent dispatch honors the grant this session. The
                // config favorite is already persisted; a policy refusal (e.g.
                // malformed ID) must not break the picker interaction.
                InputAction::GrantWorkerModel(model)
            }
            super::models::InputOutcome::ExpandProvider(provider) => {
                InputAction::ModelsExpandProvider(provider)
            }
        };
    }
    InputAction::None
}

/// P7.6 stack-routed pane handler for the `/plugins` marketplace modal.
///
/// Performs exactly what the deleted legacy chain arm did (byte-identical
/// dispatch to `plugins::handle_event`), and additionally POPS the modal stack
/// on the sole close path — keeping `modal_stack.contains(Plugins)` in sync
/// with `app.plugins.is_some()` (asserted by `debug_assert_stack_sync`).
///
/// Outcome translation (§7 P7.6): `Close` → `PaneOutcome::Pop` (clear the field
/// and pop, return `None`); `None` → `Consumed`; every OTHER `InputOutcome`
/// (AddMarketplace, InstallRequested, Uninstall, …) → `PaneOutcome::Action`,
/// i.e. deferred to the async loop verbatim via `InputAction::PluginsOutcome`,
/// leaving the modal open (no pop). The `PaneOutcome` mapping is realized
/// inline, matching the P7.4/P7.5 `route_help_find` / `route_models` shape.
///
/// Depth subtlety (post-P7.7): when plugins is opened from an already-open
/// settings modal (marketplace-from-settings), settings is ITSELF a stack
/// member — so the stack is a real two-deep `[Settings, Plugins]`, with Plugins
/// on top. This `Close` path pops Plugins (`app.plugins = None` + pop) back to
/// `[Settings]`, and `route_settings` resumes routing the still-open settings
/// modal. The chain no longer exists; the fall-back is the stack level below.
fn route_plugins(event: Event, app: &mut App) -> InputAction {
    // Invariant (checked by the tripwire): top() == Plugins ⇒ plugins is Some.
    let Some(state) = &mut app.plugins else {
        return InputAction::None;
    };
    if let Event::Key(key) = event {
        return match super::plugins::handle_event(state, key) {
            super::plugins::InputOutcome::Close => {
                app.plugins = None;
                app.modal_stack.pop();
                InputAction::None
            }
            super::plugins::InputOutcome::None => InputAction::None,
            other => InputAction::PluginsOutcome(other),
        };
    }
    InputAction::None
}

/// P7.7 stack-routed pane handler for the `/settings` modal AND its nested
/// `PaneId::PluginEditor` (the `ActiveEditor::PluginCustom` editor promoted to
/// a real second stack level). BOTH `PaneId::Settings` and `PaneId::PluginEditor`
/// dispatch here: when a PluginCustom editor is active it is `top()` and this
/// handler's first block forwards keys to it (Esc pops PluginEditor); otherwise
/// Settings is `top()` and the normal settings handling runs.
///
/// The 12 `InputOutcome` match arms — INCLUDING the synchronous config-file
/// writes (`write_plugin_config`, `write_config_value`, `toggle_plugin_config`)
/// and the theme preview/revert side-effects — are moved VERBATIM from the
/// deleted legacy chain arm. They execute at exactly the same point, in the same
/// order, as before: nothing is reordered, deferred, or hoisted. The ONLY
/// additions are the two `app.modal_stack.pop()` calls that mirror the two
/// `= None` close assignments (`app.settings = None` on `Close`; `edit_mode =
/// None` on the PluginCustom Esc) — keeping the stack in sync with the fields
/// (asserted by `debug_assert_stack_sync`). `app.modal_stack` is a field
/// disjoint from `app.settings`, so the pops borrow-check beside the live
/// `state` borrow exactly as the existing `app.model_health.clone()` does.
fn route_settings(
    event: Event,
    app: &mut App,
    runtime: &impl agent_engine::session::RuntimeRead,
    registry: &Arc<CommandRegistry>,
) -> InputAction {
    // Invariant (checked by the tripwire): top() == Settings | PluginEditor ⇒
    // settings is Some.
    if let Some(state) = &mut app.settings {
        if let Some(super::settings::ActiveEditor::PluginCustom {
            plugin_id,
            category,
            field,
            ..
        }) = &state.edit_mode
        {
            if let Event::Key(key) = event {
                if key.code == KeyCode::Esc {
                    // P7.7: Esc on the nested PluginCustom editor POPS
                    // PaneId::PluginEditor — clears edit_mode while Settings stays
                    // open, byte-identical to the pre-migration chain behaviour.
                    state.edit_mode = None;
                    app.modal_stack.pop();
                    return InputAction::None;
                }
                return InputAction::PluginEditorKey {
                    plugin_id: plugin_id.clone(),
                    category: category.clone(),
                    field: field.clone(),
                    key,
                };
            }
            return InputAction::None;
        }
        // Handle paste into active editors (API key, text, custom model)
        if let Event::Paste(text) = event {
            match &mut state.edit_mode {
                Some(super::settings::ActiveEditor::ApiKey { buffer, .. }) => {
                    buffer.push_str(&text);
                }
                Some(super::settings::ActiveEditor::Text { buffer, .. }) => {
                    buffer.push_str(&text);
                }
                Some(super::settings::ActiveEditor::CustomModel { buffer, .. }) => {
                    buffer.push_str(&text);
                }
                _ => {}
            }
            return InputAction::None;
        }
        if let Event::Key(key) = event {
            let mut snap = super::settings::RuntimeSnapshot::from_runtime_with_health(
                runtime,
                registry,
                app.model_health.clone(),
            );
            snap.catalog_overrides = app.catalog_overrides.clone();
            match super::settings::handle_event(state, key, &snap) {
                super::settings::InputOutcome::Close => {
                    app.settings = None;
                    app.modal_stack.pop();
                }
                super::settings::InputOutcome::None => {}
                super::settings::InputOutcome::Apply { key, value } => {
                    return InputAction::SettingsApply(key, value);
                }
                super::settings::InputOutcome::PluginApply {
                    plugin_id,
                    key,
                    value,
                } => {
                    let row_key = format!("plugin.{}.{}", plugin_id, key);
                    match synaps_cli::extensions::config_store::write_plugin_config(
                        &plugin_id, &key, &value,
                    ) {
                        Ok(()) => {
                            state.edit_mode = None;
                            state.row_error = Some((row_key, "saved".to_string()));
                        }
                        Err(e) => {
                            state.row_error = Some((row_key, e.to_string()));
                        }
                    }
                }
                super::settings::InputOutcome::PluginCustomOpen {
                    plugin_id,
                    category,
                    key,
                } => {
                    return InputAction::PluginEditorOpen {
                        plugin_id,
                        category,
                        field: key,
                    };
                }
                super::settings::InputOutcome::SetProviderKey { provider_id, value } => {
                    // `local.url` is non-secret endpoint configuration and
                    // stays in the config file. Actual API keys are
                    // broker-owned: persist them into the broker credential
                    // store instead of plaintext config.
                    let result = if provider_id == "local.url" {
                        synaps_cli::config::write_config_value(
                            &format!("provider.{}", provider_id),
                            &value,
                        )
                        .map_err(|e| e.to_string())
                    } else {
                        synaps_cli::auth::save_static_key(&provider_id, &value)
                    };
                    let row_key = format!("provider.{}", provider_id);
                    match result {
                        Ok(()) => {
                            state.edit_mode = None;
                            state.row_error = Some((row_key, "saved".to_string()));
                        }
                        Err(e) => {
                            state.row_error = Some((row_key, e));
                        }
                    }
                }
                super::settings::InputOutcome::TogglePlugin { name, enabled } => {
                    let mut config = synaps_cli::config::load_config();
                    match super::plugins::actions::toggle_plugin_config(
                        &name,
                        enabled,
                        &mut config,
                        registry,
                    ) {
                        Ok(()) => {
                            state.row_error = None;
                        }
                        Err(e) => {
                            state.row_error = Some(("disabled_plugins".to_string(), e));
                        }
                    }
                }
                super::settings::InputOutcome::PreviewTheme { name } => {
                    // Animated preview; arrow-key browsing retargets the
                    // in-flight fade from its current frame (no jumps).
                    // Browsing onto "myx" shows the cached live palette
                    // when the subscriber is healthy, not the stale static
                    // snapshot. Previews are transient: they never persist
                    // and never touch the subscriber lifecycle.
                    if let Some(theme) = app.resolve_apply_theme(&name) {
                        super::theme::transition::apply_animated(
                            &mut app.theme_transition,
                            theme,
                            None,
                            std::time::Instant::now(),
                        );
                    }
                }
                super::settings::InputOutcome::RevertTheme => {
                    // Esc-revert: config is untouched, so the subscriber
                    // needs no reconcile. When reverting onto a live myx
                    // (subscriber running), restore the last-good live
                    // palette — the static config load would strand the
                    // screen on the snapshot until the next track change.
                    let theme = app
                        .myx_last_live
                        .clone()
                        .filter(|_| app.myx_task.is_some())
                        .unwrap_or_else(super::theme::load_theme_from_config);
                    super::theme::transition::apply_animated(
                        &mut app.theme_transition,
                        theme,
                        None,
                        std::time::Instant::now(),
                    );
                }
                super::settings::InputOutcome::OpenPluginsMarketplace => {
                    return InputAction::OpenPluginsMarketplace;
                }
                super::settings::InputOutcome::PingModels => {
                    return InputAction::PingModels;
                }
            }
        }
        // Swallow all other events while settings is open.
        return InputAction::None;
    }
    InputAction::None
}

/// P7.8: reconcile the SecretPrompt pane against the async queue (§5).
///
/// The secret-prompt queue is NOT user-opened: tools deep in the engine send
/// requests over an mpsc channel, and `submit()` / `cancel()` auto-activate the
/// next queued prompt — so activation and deactivation both happen OUTSIDE any
/// input event. Rather than couple push/pop to a keypress, this reconciles the
/// stack to the queue's `is_active()`: push when active but absent, remove when
/// inactive but present. Called (a) after `poll_requests` in the tick arm
/// (`mod.rs`), and (b) after every `submit()` / `cancel()` in the pane handler.
/// Queue chaining is handled for free — the pane stays on the stack while
/// `is_active()` remains true across consecutive prompts.
pub(super) fn reconcile_secret_prompt(app: &mut App) {
    let active = app.secret_prompts.is_active();
    let on_stack = app.modal_stack.contains(super::focus::PaneId::SecretPrompt);
    match (active, on_stack) {
        (true, false) => app.modal_stack.push(super::focus::PaneId::SecretPrompt),
        (false, true) => app.modal_stack.remove(super::focus::PaneId::SecretPrompt),
        _ => {}
    }
}

/// P7.8 stack-routed pane handler for the async secret / masked-input prompt
/// and (by `PromptKind`) the y/n confirm dialog.
///
/// Secret reproduces the former inline `mod.rs` interception VERBATIM: Enter submits,
/// Esc cancels, Backspace deletes, Char / per-char Paste append, everything
/// else is swallowed (`PaneOutcome::Consumed`). After `submit()` / `cancel()`
/// (which may auto-activate the next queued prompt) it reconciles the stack so
/// the pane stays while another prompt is pending and pops when the queue
/// drains. Returns `InputAction::None`; the former inline `app.request_redraw()`
/// is preserved by `request_immediate_redraw` on the input path (`mod.rs`).
fn route_secret_prompt(event: Event, app: &mut App) -> InputAction {
    use synaps_cli::tools::PromptKind;
    let is_confirm = app
        .secret_prompts
        .active()
        .is_some_and(|p| p.kind == PromptKind::Confirm);
    if is_confirm {
        // Confirm dialog: no free-text field. `y` answers "y" (allow);
        // `n`, Esc and Enter (empty field) answer None (deny, fail-closed).
        // Everything else — including pastes — is swallowed so nothing can
        // accidentally form an allow.
        if let Event::Key(key) = event {
            match key.code {
                KeyCode::Char('y') | KeyCode::Char('Y') => {
                    app.secret_prompts.push_char('y');
                    app.secret_prompts.submit();
                    reconcile_secret_prompt(app);
                }
                KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc | KeyCode::Enter => {
                    app.secret_prompts.cancel();
                    reconcile_secret_prompt(app);
                }
                _ => {}
            }
        }
        return InputAction::None;
    }
    match event {
        Event::Key(key) => match key.code {
            KeyCode::Enter => {
                app.secret_prompts.submit();
                reconcile_secret_prompt(app);
            }
            KeyCode::Esc => {
                app.secret_prompts.cancel();
                reconcile_secret_prompt(app);
            }
            KeyCode::Backspace => app.secret_prompts.backspace(),
            KeyCode::Char(c) => app.secret_prompts.push_char(c),
            _ => {}
        },
        Event::Paste(text) => {
            for ch in text.chars() {
                app.secret_prompts.push_char(ch);
            }
        }
        _ => {}
    }
    InputAction::None
}

fn open_help_find_for_ambiguous_slash(app: &mut App, registry: &Arc<CommandRegistry>) -> bool {
    let Some(query) = synaps_cli::help::prefilter_query_for_slash_command(app.input_first_line())
    else {
        return false;
    };
    let help_registry = synaps_cli::help::HelpRegistry::new(
        synaps_cli::help::builtin_entries(),
        registry.plugin_help_entries(),
    );
    if help_registry.command_prefix_match_count(&query) < 2 {
        return false;
    }
    app.help_find = Some(synaps_cli::help::HelpFindState::new(
        help_registry.entries().to_vec(),
        &query,
    ));
    // P7.4: mirror the `= Some(..)` open with a stack push (§6: push accompanies
    // every open of a migrated modal). help_find never coexists, so this is a
    // depth 0 → 1 push.
    app.modal_stack.push(super::focus::PaneId::HelpFind);
    true
}

/// Tab completion for slash commands. First Tab completes to the longest
/// common prefix; subsequent Tabs cycle through all matches. Falls back to
/// fuzzy matching when no prefix matches exist. Cycle state is cleared by
/// any non-Tab keypress (see input handler).
fn handle_tab_complete(app: &mut App, registry: &Arc<CommandRegistry>) {
    let commands = super::commands::all_commands_with_skills(registry);

    // If already cycling, advance to the next match.
    if let Some((ref prefix, idx, ref matching_cmds)) = app.tab_cycle.clone() {
        if matching_cmds.is_empty() {
            app.tab_cycle = None;
            return;
        }
        let next = (idx + 1) % matching_cmds.len();
        app.set_input_text(&format!("/{}", matching_cmds[next]));
        app.tab_cycle = Some((prefix.clone(), next, matching_cmds.clone()));
        return;
    }

    // Fresh tab press — find matches for the current partial.
    let partial = app.input_first_line()[1..].to_string();
    let matches: Vec<String> = commands
        .iter()
        .filter(|c| c.starts_with(partial.as_str()))
        .cloned()
        .collect();

    if matches.len() == 1 {
        app.set_input_text(&format!("/{}", matches[0]));
        return;
    }

    if !matches.is_empty() {
        // Multiple prefix matches: first extend to longest common prefix; if that
        // didn't add anything new, start cycling through matches.
        let first = &matches[0];
        let common_len = (0..first.len())
            .take_while(|&i| {
                matches
                    .iter()
                    .all(|m| m.as_bytes().get(i) == first.as_bytes().get(i))
            })
            .count();

        if common_len > partial.len() {
            // Extend to common prefix — don't start cycling yet.
            app.set_input_text(&format!("/{}", &first[..common_len]));
        } else {
            // Already at common prefix — start cycle from match[0].
            app.set_input_text(&format!("/{}", matches[0]));
            app.tab_cycle = Some((partial, 0, matches));
        }
        return;
    }

    // No prefix matches — try fuzzy matching
    if let Some(fuzzy) = super::commands::fuzzy_match(&partial, &commands) {
        app.set_input_text(&format!("/{}", fuzzy));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyModifiers, MouseEvent, MouseEventKind};
    use synaps_cli::Session;

    fn models_key(code: KeyCode) -> Event {
        Event::Key(crossterm::event::KeyEvent::new(code, KeyModifiers::NONE))
    }

    #[test]
    fn routed_models_search_navigation_applies_visible_provider_qualified_id() {
        use crate::tui::focus::PaneId;
        use crate::tui::models::{
            ExpandedLoadState, ExpandedModelEntry, ExpandedModelsState, ModelsModalState,
        };
        let runtime = synaps_cli::Runtime::new_headless();
        let mut app = make_app();
        let mut models = ModelsModalState::new();
        models.expanded = Some(ExpandedModelsState {
            provider_key: "github-copilot".into(),
            provider_name: "GitHub Copilot".into(),
            cursor: 0,
            search: String::new(),
            load_state: ExpandedLoadState::Ready(vec![
                ExpandedModelEntry::new(
                    "github-copilot/claude-opus-4.7".into(),
                    "Opus 4.7".into(),
                    false,
                ),
                ExpandedModelEntry::new(
                    "github-copilot/claude-opus-4.8".into(),
                    "Opus 4.8".into(),
                    false,
                ),
            ]),
        });
        app.models = Some(models);
        app.modal_stack.push(PaneId::Models);
        for ch in "opus 4".chars() {
            assert!(matches!(
                route_models(models_key(KeyCode::Char(ch)), &mut app, &runtime),
                InputAction::None
            ));
        }
        assert!(matches!(
            route_models(models_key(KeyCode::Down), &mut app, &runtime),
            InputAction::None
        ));
        match route_models(models_key(KeyCode::Enter), &mut app, &runtime) {
            InputAction::ModelsApply(id) => assert_eq!(id, "github-copilot/claude-opus-4.8"),
            _ => panic!("Enter did not apply selected visible model"),
        }
        assert!(app.models.is_none());
        assert_eq!(app.modal_stack.top(), PaneId::Chat);
    }

    /// Windows consoles and kitty-protocol terminals emit a Release event for
    /// every key press. `handle_event` must drop Release events for ALL panes
    /// (the chat textarea tolerates them, but modal pane handlers would act
    /// twice — the "double-tap" navigation bug on Windows).
    #[test]
    fn key_release_events_are_dropped_in_models_pane() {
        use crate::tui::focus::PaneId;
        use crate::tui::models::ModelsModalState;
        let runtime = synaps_cli::Runtime::new_headless();
        let mut app = make_app();
        app.models = Some(ModelsModalState::new());
        app.modal_stack.push(PaneId::Models);
        let keybinds = synaps_cli::skills::keybinds::KeybindRegistry::default();
        let registry = Arc::new(CommandRegistry::new(&[], vec![]));
        let release = Event::Key(crossterm::event::KeyEvent {
            code: KeyCode::Down,
            modifiers: KeyModifiers::NONE,
            kind: crossterm::event::KeyEventKind::Release,
            state: crossterm::event::KeyEventState::empty(),
        });
        assert!(matches!(
            handle_event(release, &mut app, &runtime, false, &registry, &keybinds, 3),
            InputAction::None
        ));
    }

    fn make_app() -> App {
        App::new(Session::new("test-model", "low", None))
    }

    fn scroll_event(kind: MouseEventKind) -> crossterm::event::MouseEvent {
        MouseEvent {
            kind,
            column: 5,
            row: 5,
            modifiers: KeyModifiers::NONE,
        }
    }

    /// When scroll_lines=5 is configured, one ScrollUp event must add exactly 5
    /// to scroll_back (not the hardcoded 3).
    #[test]
    fn scroll_up_uses_configured_step() {
        let mut app = make_app();
        app.transcript.test_set_scroll_back(0);
        handle_mouse(scroll_event(MouseEventKind::ScrollUp), &mut app, 5);
        assert_eq!(
            app.transcript.scroll_back_pos(),
            5,
            "scroll_back should be 5 with scroll_lines=5"
        );
    }

    /// When scroll_lines=5 is configured, one ScrollDown event must subtract 5
    /// (clamped to 0) from scroll_back.
    #[test]
    fn scroll_down_uses_configured_step() {
        let mut app = make_app();
        app.transcript.test_set_scroll_back(10);
        handle_mouse(scroll_event(MouseEventKind::ScrollDown), &mut app, 5);
        assert_eq!(
            app.transcript.scroll_back_pos(),
            5,
            "scroll_back should decrease by 5"
        );
    }

    /// When scroll_lines is absent (None) the caller passes the default of 3.
    /// Verify the default of 3 is what the caller should use.
    #[test]
    fn scroll_default_step_is_3() {
        let cfg = synaps_cli::config::SynapsConfig::default();
        let step = cfg.scroll_lines.unwrap_or(3);
        assert_eq!(step, 3, "default scroll step must be 3 when unconfigured");
    }

    /// Configured value is read through SynapsConfig::scroll_lines.
    #[test]
    fn scroll_configured_step_is_used_via_config() {
        let cfg = synaps_cli::config::SynapsConfig {
            scroll_lines: Some(7),
            ..Default::default()
        };
        let step = cfg.scroll_lines.unwrap_or(3);
        assert_eq!(step, 7);
    }

    // ── P2: editing keys route through the TextArea editor ────────────────

    /// Drive one keypress through the chat-pane key handler (not streaming).
    fn press(app: &mut App, code: KeyCode, mods: KeyModifiers) -> InputAction {
        let registry = Arc::new(CommandRegistry::new(&[], vec![]));
        let keybinds = synaps_cli::skills::keybinds::KeybindRegistry::new();
        handle_key(
            crossterm::event::KeyEvent::new(code, mods),
            app,
            false,
            &registry,
            &keybinds,
        )
    }

    /// Shift-Enter inserts a newline into the buffer instead of submitting.
    #[test]
    fn shift_enter_inserts_newline() {
        let mut app = make_app();
        press(&mut app, KeyCode::Char('a'), KeyModifiers::NONE);
        press(&mut app, KeyCode::Char('b'), KeyModifiers::NONE);
        assert!(matches!(
            press(&mut app, KeyCode::Enter, KeyModifiers::SHIFT),
            InputAction::None
        ));
        press(&mut app, KeyCode::Char('c'), KeyModifiers::NONE);
        assert_eq!(app.input_text(), "ab\nc");
        assert_eq!(app.cursor_char_pos(), 4);
    }

    /// Up inside a multi-line buffer moves the cursor, not history (§3.3).
    #[test]
    fn up_on_multiline_buffer_moves_cursor_not_history() {
        let mut app = make_app();
        app.input_history.push("older entry".to_string());
        app.set_input_text("line1\nline2"); // cursor at end: row 1
        press(&mut app, KeyCode::Up, KeyModifiers::NONE);
        assert_eq!(app.input_text(), "line1\nline2", "buffer must be untouched");
        assert_eq!(app.history_index, None, "history must not engage");
        assert_eq!(app.editor.cursor().0, 0, "cursor should move to row 0");
        // A second Up — now on the first line — engages history.
        press(&mut app, KeyCode::Up, KeyModifiers::NONE);
        assert_eq!(app.input_text(), "older entry");
        assert_eq!(app.history_index, Some(0));
    }

    /// Up on a single-line buffer recalls history immediately, and Down at
    /// the last line restores the stashed draft.
    #[test]
    fn up_on_single_line_triggers_history() {
        let mut app = make_app();
        app.input_history.push("older entry".to_string());
        app.set_input_text("draft");
        press(&mut app, KeyCode::Up, KeyModifiers::NONE);
        assert_eq!(app.input_text(), "older entry");
        assert_eq!(app.history_index, Some(0));
        press(&mut app, KeyCode::Down, KeyModifiers::NONE);
        assert_eq!(app.input_text(), "draft", "Down at edge restores stash");
        assert_eq!(app.history_index, None);
    }

    /// Ctrl-U clears the WHOLE buffer (our semantics, not the lib default).
    #[test]
    fn ctrl_u_clears_whole_buffer() {
        let mut app = make_app();
        app.set_input_text("hello\nworld");
        press(&mut app, KeyCode::Char('u'), KeyModifiers::CONTROL);
        assert!(app.input_is_empty());
        assert_eq!(app.cursor_char_pos(), 0);
    }

    /// E-P8 T2: Ctrl-C while the driver is armed → Abort (→ Cancel), NOT Quit.
    #[test]
    fn ctrl_c_while_driver_armed_aborts_not_quits() {
        let mut app = make_app();
        // Not armed → Ctrl-C quits.
        assert!(matches!(
            press(&mut app, KeyCode::Char('c'), KeyModifiers::CONTROL),
            InputAction::Quit
        ));
        app.driver_ui.armed = true;
        // Armed → Ctrl-C stops the driver (Abort → SessionCommand::Cancel).
        assert!(matches!(
            press(&mut app, KeyCode::Char('c'), KeyModifiers::CONTROL),
            InputAction::Abort
        ));
    }

    /// E-P8 T2: Esc while armed (even when NOT streaming) → Abort (→ Cancel).
    #[test]
    fn esc_while_driver_armed_aborts_even_when_idle() {
        let mut app = make_app();
        // Not armed, not streaming → Esc is a no-op.
        assert!(matches!(
            press(&mut app, KeyCode::Esc, KeyModifiers::NONE),
            InputAction::None
        ));
        app.driver_ui.armed = true;
        assert!(matches!(
            press(&mut app, KeyCode::Esc, KeyModifiers::NONE),
            InputAction::Abort
        ));
    }

    /// Backspace / word-delete route through the editor.
    #[test]
    fn editing_keys_forward_to_editor() {
        let mut app = make_app();
        for c in "hi there".chars() {
            press(&mut app, KeyCode::Char(c), KeyModifiers::NONE);
        }
        press(&mut app, KeyCode::Backspace, KeyModifiers::NONE);
        assert_eq!(app.input_text(), "hi ther");
        press(&mut app, KeyCode::Char('w'), KeyModifiers::CONTROL);
        assert_eq!(app.input_text(), "hi ");
        assert_eq!(app.cursor_char_pos(), 3);
    }
}