claux 20260705.0.0

Terminal AI coding assistant with tool execution
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
//! Chat screen: conversation with the LLM.
//!
//! This is the main interaction screen. Extracted from the original tui/mod.rs.

use anyhow::Result;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io::Stdout;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use crate::commands::{self, CommandResult};
use crate::db::Db;
use crate::permissions::PermissionResponse;
use crate::plugin::PluginRegistry;
use crate::query::{Engine, SteeringQueue};
use crate::theme::{Theme, ThemeName};

use super::screen::Action;
use super::ui;

/// A displayed message in the chat.
#[derive(Debug, Clone)]
pub enum ChatMessage {
    /// User, assistant, system, or error text message.
    Text { role: String, content: String },
    /// A tool invocation with its result status.
    Tool {
        name: String,
        summary: String,
        status: ToolStatus,
    },
}

/// Status of a tool invocation in the UI.
#[derive(Debug, Clone, PartialEq)]
pub enum ToolStatus {
    Running,
    Success,
    Error,
}

/// What the chat screen is doing.
#[derive(Debug, Clone, PartialEq)]
pub enum Mode {
    Input,
    Streaming,
    Permission,
}

/// Chat screen state.
pub struct ChatApp {
    pub messages: Vec<ChatMessage>,
    pub input: String,
    pub cursor: usize,
    pub scroll: u16,
    pub manual_scroll: bool,
    pub mode: Mode,
    pub stream_buffer: String,
    pub status: String,
    pub permission_prompt: Option<String>,
    pub permission_details: Option<Vec<String>>,
    pub should_exit: bool,
    pub should_go_home: bool,
    pub model: String,
    pub total_lines: u16,
    pub thinking: bool,
    pub theme: Theme,
    /// Text being typed while a turn runs, before Enter queues it as a
    /// steering message. Behind Arc<Mutex> because the during-tool key
    /// watcher runs on a separate task.
    pub steer_buf: Arc<Mutex<String>>,
    /// Double-press state for Ctrl+C so one stray press can't kill the app.
    pub ctrl_c: crate::utils::CtrlCArm,
    /// Bumped whenever `messages` changes; keys the rendered-line cache in
    /// ui::draw_chat so history isn't re-rendered on every frame.
    pub messages_rev: u64,
    /// Cached rendering of `messages` (see ui::draw_chat). None until the
    /// first draw or after invalidation.
    pub history_cache: Option<super::ui::HistoryCache>,
    /// Displayed in the header. Defaults to CARGO_PKG_VERSION; overridable
    /// so snapshot tests can pin it to a stable value across version bumps.
    pub version: String,
}

impl ChatApp {
    pub fn new(model: &str, theme: Theme) -> Self {
        Self {
            messages: Vec::new(),
            input: String::new(),
            cursor: 0,
            scroll: 0,
            manual_scroll: false,
            mode: Mode::Input,
            stream_buffer: String::new(),
            status: String::new(),
            permission_prompt: None,
            permission_details: None,
            should_exit: false,
            should_go_home: false,
            model: model.to_string(),
            total_lines: 0,
            thinking: false,
            theme,
            steer_buf: Arc::new(Mutex::new(String::new())),
            ctrl_c: crate::utils::CtrlCArm::default(),
            messages_rev: 0,
            history_cache: None,
            version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }

    pub fn add_message(&mut self, role: &str, content: &str) {
        self.messages_rev += 1;
        self.messages.push(ChatMessage::Text {
            role: role.to_string(),
            content: content.to_string(),
        });
    }

    pub fn add_tool(&mut self, name: &str, summary: &str, status: ToolStatus) {
        // A tool arriving means the model has responded; without this, a
        // turn that opens with tool calls (no text) leaves the "thinking"
        // spinner running under tool output and permission prompts.
        self.thinking = false;
        self.messages_rev += 1;
        self.messages.push(ChatMessage::Tool {
            name: name.to_string(),
            summary: summary.to_string(),
            status,
        });
    }

    /// Update the last tool message's status (e.g., from Running to Success/Error).
    pub fn update_last_tool_status(&mut self, new_status: ToolStatus) {
        self.messages_rev += 1;
        if let Some(ChatMessage::Tool { status, .. }) = self.messages.last_mut() {
            *status = new_status;
        }
    }

    pub fn set_theme(&mut self, theme_name: ThemeName) {
        self.theme = Theme::from_name(theme_name);
        // Cached lines bake in theme colors
        self.messages_rev += 1;
    }

    pub fn handle_key(&mut self, key: KeyEvent) {
        match self.mode {
            Mode::Input => self.handle_input_key(key),
            Mode::Permission | Mode::Streaming => {}
        }
    }

    fn handle_input_key(&mut self, key: KeyEvent) {
        // Any key other than Ctrl+C stands down a pending exit confirmation.
        let is_ctrl_c = key.modifiers == KeyModifiers::CONTROL && key.code == KeyCode::Char('c');
        if !is_ctrl_c && self.ctrl_c.is_armed() {
            self.ctrl_c.disarm();
            self.status = format!("{} | /help for commands", self.model);
        }

        match (key.modifiers, key.code) {
            (KeyModifiers::CONTROL, KeyCode::Char('c')) => {
                if self.ctrl_c.press() {
                    self.should_exit = true;
                } else {
                    self.status = "Press Ctrl+C again to exit".to_string();
                }
            }
            (KeyModifiers::CONTROL, KeyCode::Char('d')) => {
                self.should_exit = true;
            }
            (_, KeyCode::Enter) => {
                // Submit handled by caller
            }
            (_, KeyCode::Backspace) if self.cursor > 0 => {
                self.cursor -= 1;
                self.input.remove(self.cursor);
            }
            (_, KeyCode::Delete) if self.cursor < self.input.len() => {
                self.input.remove(self.cursor);
            }
            (_, KeyCode::Left) if self.cursor > 0 => {
                self.cursor -= 1;
            }
            (_, KeyCode::Right) if self.cursor < self.input.len() => {
                self.cursor += 1;
            }
            (_, KeyCode::Home) | (KeyModifiers::CONTROL, KeyCode::Char('a')) => {
                self.cursor = 0;
            }
            (_, KeyCode::End) | (KeyModifiers::CONTROL, KeyCode::Char('e')) => {
                self.cursor = self.input.len();
            }
            (KeyModifiers::CONTROL, KeyCode::Char('u')) => {
                self.input.clear();
                self.cursor = 0;
            }
            (_, KeyCode::Up) => {
                self.scroll = self.scroll.saturating_add(3);
                self.manual_scroll = true;
            }
            (_, KeyCode::Down) => {
                self.scroll = self.scroll.saturating_sub(3);
                if self.scroll == 0 {
                    self.manual_scroll = false;
                }
            }
            (_, KeyCode::Char(c)) => {
                self.input.insert(self.cursor, c);
                self.cursor += 1;
            }
            _ => {}
        }
    }

    pub fn take_input(&mut self) -> Option<String> {
        if self.input.trim().is_empty() {
            return None;
        }
        let input = self.input.clone();
        self.input.clear();
        self.cursor = 0;
        Some(input)
    }
}

/// Run the chat screen. Returns an Action when the user exits or goes home.
pub async fn run(
    engine: &mut Engine,
    session_id: &str,
    db: &Db,
    terminal: &mut Terminal<CrosstermBackend<Stdout>>,
    theme: Theme,
    _plugins: &PluginRegistry,
) -> Result<Action> {
    // Clear engine state and load this session's messages
    engine.messages_mut().clear();
    let existing_messages = db.get_messages(session_id)?;
    for msg in &existing_messages {
        engine.messages_mut().push(msg.clone());
    }

    let mut app = ChatApp::new(engine.model(), theme);
    app.status = format!("{} | /help for commands", engine.model());

    // Show existing messages in the UI
    for msg in &existing_messages {
        let role = &msg.role;
        let content = match &msg.content {
            crate::api::types::MessageContent::Text(t) => t.clone(),
            crate::api::types::MessageContent::Blocks(blocks) => blocks
                .iter()
                .filter_map(|b| match b {
                    crate::api::ContentBlock::Text { text } => Some(text.clone()),
                    _ => None,
                })
                .collect::<Vec<_>>()
                .join("\n"),
        };
        app.add_message(role, &content);
    }

    let mut needs_redraw = true;
    let mut pending_submit: Option<String> = None;

    loop {
        if needs_redraw {
            terminal.draw(|f| ui::draw_chat(f, &mut app))?;
            needs_redraw = false;
        }

        // Process pending submit
        if let Some(input) = pending_submit.take() {
            let trimmed = input.trim().to_string();

            // Check for /home command
            if trimmed == "/home" {
                return Ok(Action::Home);
            }

            // Check slash commands
            if let Some(result) = commands::parse_command(&trimmed) {
                match result {
                    CommandResult::Text(ref text) if text == "__cost__" => {
                        app.add_message("system", &commands::format_cost(engine));
                    }
                    CommandResult::Text(text) => {
                        app.add_message("system", &text);
                    }
                    CommandResult::Exit => {
                        return Ok(Action::Home);
                    }
                    CommandResult::Async(async_cmd) => match async_cmd {
                        commands::AsyncCommand::Theme(theme_name) => match theme_name {
                            Some(name) => {
                                let theme = match name.to_lowercase().as_str() {
                                    "dark" => ThemeName::Dark,
                                    "light" => ThemeName::Light,
                                    "ansi" => ThemeName::Ansi,
                                    "dracula" => ThemeName::Dracula,
                                    "nord" => ThemeName::Nord,
                                    "catppuccin" => ThemeName::Catppuccin,
                                    _ => {
                                        app.add_message("error", &format!(
                                                    "Unknown theme: {name}. Available: dark, light, ansi, dracula, nord, catppuccin"
                                                ));
                                        continue;
                                    }
                                };
                                app.set_theme(theme);
                                app.add_message("system", &format!("Theme set to: {name}"));
                            }
                            None => {
                                app.add_message("system",
                                            "Available themes: dark, light, ansi, dracula, nord, catppuccin\n\
                                             Use /theme <name> to switch.");
                            }
                        },
                        _ => match commands::execute_async(async_cmd, engine).await {
                            Ok(output) => app.add_message("system", &output),
                            Err(e) => app.add_message("error", &format!("Error: {e}")),
                        },
                    },
                }
                app.scroll = 0;
                app.manual_scroll = false;
                needs_redraw = true;
                continue;
            }

            // Regular message -- start streaming
            app.add_message("user", &trimmed);
            app.mode = Mode::Streaming;
            app.stream_buffer.clear();
            app.thinking = true;
            app.scroll = 0;
            app.manual_scroll = false;

            let user_msg = crate::api::Message::user(&trimmed);
            let _ = db.append_message(session_id, &user_msg);

            app.status = format!("{} | thinking...", app.model);

            let submit_result = drive_streaming(engine, &trimmed, &mut app, terminal).await;

            match submit_result {
                Ok(()) => {
                    if let Some(last) = engine.messages().last() {
                        let _ = db.append_message(session_id, last);
                    }
                }
                Err(e) => {
                    app.add_message("error", &format!("Error: {e}"));
                }
            }

            app.mode = Mode::Input;
            app.status = format!("{} | {}", engine.model(), engine.cost.format_summary());
            app.scroll = 0;
            app.manual_scroll = false;
            needs_redraw = true;
            continue;
        }

        // Poll terminal events
        if event::poll(std::time::Duration::from_millis(50))? {
            if let Event::Key(key) = event::read()? {
                if key.code == KeyCode::Enter && app.mode == Mode::Input {
                    if let Some(input) = app.take_input() {
                        pending_submit = Some(input);
                    }
                } else {
                    app.handle_key(key);
                }
                needs_redraw = true;
            }
        }

        if app.should_exit {
            return Ok(Action::Quit);
        }
        if app.should_go_home {
            return Ok(Action::Home);
        }
    }
}

/// Drive the streaming query, handling both stream events and terminal events.
async fn drive_streaming(
    engine: &mut Engine,
    input: &str,
    app: &mut ChatApp,
    terminal: &mut Terminal<CrosstermBackend<Stdout>>,
) -> Result<()> {
    engine.messages_mut().push(crate::api::Message::user(input));

    // Deliver steering left over from a previous turn (typed after that
    // turn's last drain point).
    for text in engine.inject_steering() {
        app.add_message("user", &text);
    }

    let steering = engine.steering_queue();
    let steer_buf = app.steer_buf.clone();

    let tool_defs = engine.tool_definitions();
    let mut api_rx = engine.start_stream(&tool_defs).await?;

    let mut text_buf = String::new();
    let mut tool_uses: Vec<(String, String, serde_json::Value)> = Vec::new();
    let cancelled = Arc::new(AtomicBool::new(false));

    loop {
        // A persistent interval, not a fresh sleep per select iteration: a
        // sleep future recreated each loop never fires while stream chunks
        // arrive faster than its duration, which would starve redraws and
        // key polling exactly when the model streams quickly.
        let mut tick = tokio::time::interval(std::time::Duration::from_millis(50));
        tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
        loop {
            tokio::select! {
                Some(event) = api_rx.recv() => {
                    match event {
                        crate::api::ApiEvent::Text(t) => {
                            // No draw here: chunks can arrive far faster
                            // than frames are worth painting. The 50ms
                            // tick below redraws, coalescing however many
                            // chunks arrived since the last frame.
                            app.stream_buffer.push_str(&t);
                            text_buf.push_str(&t);
                            if app.thinking {
                                app.thinking = false;
                            }
                        }
                        crate::api::ApiEvent::ToolUse { id, name, input } => {
                            tool_uses.push((id, name, input));
                        }
                        crate::api::ApiEvent::Usage(usage) => {
                            engine.cost.add_usage(&usage);
                        }
                        crate::api::ApiEvent::Done => break,
                        crate::api::ApiEvent::Error(e) => {
                            return Err(anyhow::anyhow!("API error: {e}"));
                        }
                    }
                }
                _ = tick.tick() => {
                    if poll_stream_key(&steer_buf, &steering)? {
                        cancelled.store(true, Ordering::Relaxed);
                        break;
                    }
                    // The tick is the only draw during streaming: it
                    // coalesces all chunks since the last frame, animates
                    // the spinner, and keeps the steering buffer visible.
                    terminal.draw(|f| ui::draw_chat(f, app))?;
                }
            }
        }

        // Record assistant message
        let mut blocks = Vec::new();
        if !text_buf.is_empty() {
            blocks.push(crate::api::ContentBlock::Text {
                text: text_buf.clone(),
            });
        }
        for (id, name, input) in &tool_uses {
            blocks.push(crate::api::ContentBlock::ToolUse {
                id: id.clone(),
                name: name.clone(),
                input: input.clone(),
            });
        }
        if !blocks.is_empty() {
            engine
                .messages_mut()
                .push(crate::api::Message::assistant_blocks(blocks));
        }

        // If the stream was cancelled, every tool_use that came in is orphan.
        // Pair them with synthetic interrupted tool_results so the next turn
        // doesn't see unanswered tool calls and try to continue the chain.
        if cancelled.load(Ordering::Relaxed) {
            if !tool_uses.is_empty() {
                engine
                    .messages_mut()
                    .push(crate::api::Message::tool_results(
                        synthesize_interrupt_results(&tool_uses),
                    ));
            }
            if !app.stream_buffer.is_empty() {
                let content = app.stream_buffer.clone();
                app.stream_buffer.clear();
                app.add_message("assistant", &content);
            }
            app.add_message("system", "Interrupted by user.");
            return Ok(());
        }

        if tool_uses.is_empty() {
            if !app.stream_buffer.is_empty() {
                let content = app.stream_buffer.clone();
                app.stream_buffer.clear();
                app.add_message("assistant", &content);
            }
            break;
        }

        // Execute tools
        let mut result_blocks = Vec::new();
        let mut interrupted_at: Option<usize> = None;
        let mut steered_at: Option<usize> = None;
        for (idx, (id, name, input)) in tool_uses.iter().enumerate() {
            // Cancellation/steering check between tools.
            if poll_stream_key(&steer_buf, &steering)? {
                cancelled.store(true, Ordering::Relaxed);
            }
            if cancelled.load(Ordering::Relaxed) {
                interrupted_at = Some(idx);
                break;
            }
            // A pending steering message supersedes the rest of the batch;
            // skip it so the model reads the correction immediately.
            if !steering.lock().expect("steering queue poisoned").is_empty() {
                steered_at = Some(idx);
                break;
            }
            let summary = engine.summarize_tool(name, input);
            // Flush any pending streamed text before showing tool
            if !app.stream_buffer.is_empty() {
                let content = app.stream_buffer.clone();
                app.stream_buffer.clear();
                app.add_message("assistant", &content);
            }
            app.add_tool(name, &summary, ToolStatus::Running);
            terminal.draw(|f| ui::draw_chat(f, app))?;

            let is_read_only = engine.is_tool_read_only(name);
            let perm = engine.check_permission(name, input, is_read_only);

            let tool_output = match perm {
                crate::permissions::PermissionResult::Allow => {
                    execute_tool_with_cancel(
                        engine,
                        name,
                        input.clone(),
                        cancelled.clone(),
                        steer_buf.clone(),
                    )
                    .await
                }
                crate::permissions::PermissionResult::Deny(reason) => crate::tools::ToolOutput {
                    content: format!("Permission denied: {reason}"),
                    is_error: true,
                },
                crate::permissions::PermissionResult::Ask {
                    message: summary, ..
                } => {
                    let details = format_permission_details(name, input);
                    app.permission_prompt = Some(summary.clone());
                    app.permission_details = Some(details);
                    app.mode = Mode::Permission;
                    terminal.draw(|f| ui::draw_chat(f, app))?;

                    let mut perm_input = String::new();

                    let response = loop {
                        if event::poll(std::time::Duration::from_millis(50))? {
                            if let Event::Key(key) = event::read()? {
                                match key.code {
                                    KeyCode::Char('y') | KeyCode::Enter
                                        if perm_input.is_empty() =>
                                    {
                                        break PermissionResponse::Allow;
                                    }
                                    KeyCode::Char('a') if perm_input.is_empty() => {
                                        break PermissionResponse::AlwaysAllow;
                                    }
                                    KeyCode::Char('n') if perm_input.is_empty() => {
                                        break PermissionResponse::Deny;
                                    }
                                    KeyCode::Esc if perm_input.is_empty() => {
                                        break PermissionResponse::Deny;
                                    }
                                    KeyCode::Enter if !perm_input.is_empty() => {
                                        // User typed a message: deny current tool
                                        // and inject the message as steering.
                                        steering
                                            .lock()
                                            .expect("steering queue poisoned")
                                            .push_back(perm_input.clone());
                                        app.add_message("user", &perm_input);
                                        break PermissionResponse::DenyAndCancel;
                                    }
                                    KeyCode::Backspace if !perm_input.is_empty() => {
                                        perm_input.pop();
                                    }
                                    KeyCode::Char(c) => {
                                        perm_input.push(c);
                                    }
                                    _ => {}
                                }
                            }
                        }
                        // Redraw so the typed text stays visible
                        app.status =
                            format!("{} | deny and message: {}", engine.model(), perm_input);
                        terminal.draw(|f| ui::draw_chat(f, app))?;
                    };

                    app.permission_prompt = None;
                    app.permission_details = None;
                    app.mode = Mode::Streaming;
                    app.status = format!("{} | {}", engine.model(), engine.cost.format_summary());

                    match response {
                        PermissionResponse::Allow => {
                            execute_tool_with_cancel(
                                engine,
                                name,
                                input.clone(),
                                cancelled.clone(),
                                steer_buf.clone(),
                            )
                            .await
                        }
                        PermissionResponse::AlwaysAllow => {
                            engine.always_allow_tool(name);
                            execute_tool_with_cancel(
                                engine,
                                name,
                                input.clone(),
                                cancelled.clone(),
                                steer_buf.clone(),
                            )
                            .await
                        }
                        PermissionResponse::AlwaysAllowCommand(cmd) => {
                            engine.always_allow_tool(name);
                            engine.always_allow_command(&cmd);
                            execute_tool_with_cancel(
                                engine,
                                name,
                                input.clone(),
                                cancelled.clone(),
                                steer_buf.clone(),
                            )
                            .await
                        }
                        PermissionResponse::Deny => {
                            cancelled.store(true, Ordering::Relaxed);
                            crate::tools::ToolOutput {
                                content: "Permission denied by user.".to_string(),
                                is_error: true,
                            }
                        }
                        PermissionResponse::DenyAndCancel => {
                            cancelled.store(true, Ordering::Relaxed);
                            crate::tools::ToolOutput {
                                content: "Permission denied by user.".to_string(),
                                is_error: true,
                            }
                        }
                    }
                }
            };

            // The tool itself may have noticed cancellation and returned an
            // error. The for-loop's top-of-iter check picks it up, but if
            // the user cancelled *during* this tool, we want to honor it
            // immediately rather than starting the next tool.
            if cancelled.load(Ordering::Relaxed) {
                // Push this tool's result (likely "Interrupted by user") and
                // then mark interruption so the post-loop fixup synthesizes
                // results for the rest.
                let (content, _was_truncated) =
                    crate::compact::truncate_tool_output(&tool_output.content);
                if tool_output.is_error {
                    app.update_last_tool_status(ToolStatus::Error);
                } else {
                    app.update_last_tool_status(ToolStatus::Success);
                }
                terminal.draw(|f| ui::draw_chat(f, app))?;
                result_blocks.push(crate::api::ContentBlock::ToolResult {
                    tool_use_id: id.clone(),
                    content,
                    is_error: if tool_output.is_error {
                        Some(true)
                    } else {
                        None
                    },
                });
                interrupted_at = Some(idx + 1);
                break;
            }

            let (content, _was_truncated) =
                crate::compact::truncate_tool_output(&tool_output.content);

            if tool_output.is_error {
                app.update_last_tool_status(ToolStatus::Error);
            } else {
                app.update_last_tool_status(ToolStatus::Success);
            }
            terminal.draw(|f| ui::draw_chat(f, app))?;

            result_blocks.push(crate::api::ContentBlock::ToolResult {
                tool_use_id: id.clone(),
                content,
                is_error: if tool_output.is_error {
                    Some(true)
                } else {
                    None
                },
            });
        }

        // If we broke out due to cancellation, synthesize interrupted results
        // for the unstarted tool_uses so the assistant message has paired
        // tool_use/tool_result blocks. Otherwise the next turn sees orphans.
        if let Some(idx) = interrupted_at {
            for (id, _name, _input) in &tool_uses[idx..] {
                result_blocks.push(crate::api::ContentBlock::ToolResult {
                    tool_use_id: id.clone(),
                    content: "Interrupted by user.".to_string(),
                    is_error: Some(true),
                });
            }
            engine
                .messages_mut()
                .push(crate::api::Message::tool_results(result_blocks));
            app.add_message("system", "Interrupted by user.");
            return Ok(());
        }

        // A steering message superseded the rest of the batch: pair the
        // unstarted tool_uses with synthetic results and continue the turn.
        // No "interrupted" note is added; the injected user message below
        // explains the skip to the model, matching Claude Code's
        // submit-interrupt behavior.
        if let Some(idx) = steered_at {
            let skipped = tool_uses.len() - idx;
            for (id, name, _input) in &tool_uses[idx..] {
                app.add_tool(name, "skipped (steering)", ToolStatus::Error);
                result_blocks.push(crate::api::ContentBlock::ToolResult {
                    tool_use_id: id.clone(),
                    content: Engine::SKIPPED_FOR_STEERING.to_string(),
                    is_error: Some(true),
                });
            }
            app.add_message(
                "system",
                &format!("Skipped {skipped} queued tool(s) to apply your message."),
            );
        }

        engine
            .messages_mut()
            .push(crate::api::Message::tool_results(result_blocks));

        // Deliver steering typed while tools were running, so the next API
        // call sees it right after the tool results.
        for text in engine.inject_steering() {
            app.add_message("user", &text);
        }
        terminal.draw(|f| ui::draw_chat(f, app))?;

        text_buf.clear();
        tool_uses.clear();

        let tool_defs = engine.tool_definitions();
        api_rx = engine.start_stream(&tool_defs).await?;
    }

    Ok(())
}

/// Non-blocking key poll while a turn is running. Ctrl+C cancels (returns
/// true). Everything else builds the steering buffer: printable characters
/// append, Backspace deletes, and Enter moves the buffer into the engine's
/// steering queue, which the turn loop drains before its next API call.
fn poll_stream_key(steer_buf: &Arc<Mutex<String>>, steering: &SteeringQueue) -> Result<bool> {
    if event::poll(std::time::Duration::from_millis(0))? {
        if let Event::Key(key) = event::read()? {
            match (key.modifiers, key.code) {
                (KeyModifiers::CONTROL, KeyCode::Char('c')) => return Ok(true),
                (_, KeyCode::Enter) => {
                    let text = {
                        let mut buf = steer_buf.lock().expect("steer buffer poisoned");
                        std::mem::take(&mut *buf)
                    };
                    let text = text.trim().to_string();
                    if !text.is_empty() {
                        steering
                            .lock()
                            .expect("steering queue poisoned")
                            .push_back(text);
                    }
                }
                (_, KeyCode::Backspace) => {
                    steer_buf.lock().expect("steer buffer poisoned").pop();
                }
                (m, KeyCode::Char(c)) if m.is_empty() || m == KeyModifiers::SHIFT => {
                    steer_buf.lock().expect("steer buffer poisoned").push(c);
                }
                _ => {}
            }
        }
    }
    Ok(false)
}

/// Run a tool while watching the keyboard. Each invocation gets its own
/// fresh CancellationToken so cancellation of one tool doesn't leak into
/// later tool calls in the same chain. Ctrl+C fires the token and the tool
/// is expected to notice it and return promptly; other keys feed the
/// steering buffer so the user can type while a long tool runs.
/// `outer_cancelled` is signalled to the caller so it can decide to break
/// out of any surrounding loops.
async fn execute_tool_with_cancel(
    engine: &mut crate::query::Engine,
    name: &str,
    input: serde_json::Value,
    outer_cancelled: Arc<AtomicBool>,
    steer_buf: Arc<Mutex<String>>,
) -> crate::tools::ToolOutput {
    let token = tokio_util::sync::CancellationToken::new();
    let watcher_token = token.clone();
    let watcher_flag = outer_cancelled.clone();
    let steering = engine.steering_queue();
    let watcher = tokio::spawn(async move {
        loop {
            if watcher_token.is_cancelled() {
                return;
            }
            if poll_stream_key(&steer_buf, &steering).unwrap_or(false) {
                watcher_flag.store(true, Ordering::Relaxed);
                watcher_token.cancel();
                return;
            }
            // A steering message cancels the running tool but not the turn:
            // outer_cancelled stays false, so the batch loop continues,
            // sees the pending message, and skips the rest of the batch.
            if !steering.lock().expect("steering queue poisoned").is_empty() {
                watcher_token.cancel();
                return;
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        }
    });

    let result = engine.execute_tool(name, input, token.clone()).await;
    token.cancel(); // wake the watcher so it exits
    let _ = watcher.await;
    result
}

/// Build "Interrupted by user" tool_result blocks for tool_uses that never ran.
fn synthesize_interrupt_results(
    tool_uses: &[(String, String, serde_json::Value)],
) -> Vec<crate::api::ContentBlock> {
    tool_uses
        .iter()
        .map(|(id, _name, _input)| crate::api::ContentBlock::ToolResult {
            tool_use_id: id.clone(),
            content: "Interrupted by user.".to_string(),
            is_error: Some(true),
        })
        .collect()
}

fn format_permission_details(tool_name: &str, input: &serde_json::Value) -> Vec<String> {
    let mut lines = Vec::new();

    match tool_name {
        "Bash" => {
            if let Some(cmd) = input["command"].as_str() {
                lines.push("Command:".to_string());
                for line in cmd.lines() {
                    lines.push(format!("  {line}"));
                }
            }
        }
        "Write" => {
            if let Some(path) = input["file_path"].as_str() {
                lines.push(format!("File: {path}"));
            }
            if let Some(content) = input["content"].as_str() {
                let preview: Vec<&str> = content.lines().take(10).collect();
                lines.push("Content:".to_string());
                for line in &preview {
                    lines.push(format!("  {line}"));
                }
                let total = content.lines().count();
                if total > 10 {
                    lines.push(format!("  ... ({} more lines)", total - 10));
                }
            }
        }
        "Edit" => {
            if let Some(path) = input["file_path"].as_str() {
                lines.push(format!("File: {path}"));
            }
            if let Some(old) = input["old_string"].as_str() {
                lines.push("Replace:".to_string());
                for line in old.lines().take(5) {
                    lines.push(format!("  - {line}"));
                }
            }
            if let Some(new) = input["new_string"].as_str() {
                lines.push("With:".to_string());
                for line in new.lines().take(5) {
                    lines.push(format!("  + {line}"));
                }
            }
        }
        "Agent" => {
            if let Some(prompt) = input["prompt"].as_str() {
                lines.push("Task:".to_string());
                for line in prompt.lines().take(5) {
                    lines.push(format!("  {line}"));
                }
            }
        }
        "Read" => {
            if let Some(path) = input["file_path"].as_str() {
                lines.push(format!("File: {path}"));
            }
        }
        "Grep" => {
            if let Some(pattern) = input["pattern"].as_str() {
                lines.push(format!("Pattern: {pattern}"));
            }
            if let Some(path) = input["path"].as_str() {
                lines.push(format!("In: {path}"));
            }
        }
        "WebFetch" => {
            if let Some(url) = input["url"].as_str() {
                lines.push(format!("URL: {url}"));
            }
        }
        _ => {
            let json_str = serde_json::to_string_pretty(input).unwrap_or_default();
            for line in json_str.lines().take(8) {
                lines.push(format!("  {line}"));
            }
        }
    }

    lines
}

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

    fn test_app() -> ChatApp {
        ChatApp::new("test-model", Theme::dark())
    }

    fn ctrl_c_key() -> KeyEvent {
        KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)
    }

    #[test]
    fn single_ctrl_c_warns_double_exits() {
        let mut app = test_app();
        app.handle_key(ctrl_c_key());
        assert!(!app.should_exit, "first Ctrl+C must not exit");
        assert!(app.status.contains("again"), "status should show the hint");
        app.handle_key(ctrl_c_key());
        assert!(app.should_exit, "second Ctrl+C must exit");
    }

    #[test]
    fn typing_disarms_pending_ctrl_c() {
        let mut app = test_app();
        app.handle_key(ctrl_c_key());
        app.handle_key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE));
        assert!(!app.status.contains("again"), "hint should clear");
        app.handle_key(ctrl_c_key());
        assert!(
            !app.should_exit,
            "Ctrl+C after typing re-arms instead of exiting"
        );
    }

    #[test]
    fn ctrl_d_still_exits_immediately() {
        let mut app = test_app();
        app.handle_key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL));
        assert!(app.should_exit);
    }

    #[test]
    fn add_message_creates_text_variant() {
        let mut app = test_app();
        app.add_message("user", "hello");
        assert_eq!(app.messages.len(), 1);
        match &app.messages[0] {
            ChatMessage::Text { role, content } => {
                assert_eq!(role, "user");
                assert_eq!(content, "hello");
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn add_tool_creates_tool_variant() {
        let mut app = test_app();
        app.add_tool("Bash", "cargo build", ToolStatus::Running);
        assert_eq!(app.messages.len(), 1);
        match &app.messages[0] {
            ChatMessage::Tool {
                name,
                summary,
                status,
            } => {
                assert_eq!(name, "Bash");
                assert_eq!(summary, "cargo build");
                assert_eq!(*status, ToolStatus::Running);
            }
            _ => panic!("expected Tool variant"),
        }
    }

    #[test]
    fn update_last_tool_status_changes_running_to_success() {
        let mut app = test_app();
        app.add_tool("Read", "/some/file", ToolStatus::Running);
        app.update_last_tool_status(ToolStatus::Success);
        match &app.messages[0] {
            ChatMessage::Tool { status, .. } => assert_eq!(*status, ToolStatus::Success),
            _ => panic!("expected Tool variant"),
        }
    }

    #[test]
    fn update_last_tool_status_changes_running_to_error() {
        let mut app = test_app();
        app.add_tool("Bash", "failing command", ToolStatus::Running);
        app.update_last_tool_status(ToolStatus::Error);
        match &app.messages[0] {
            ChatMessage::Tool { status, .. } => assert_eq!(*status, ToolStatus::Error),
            _ => panic!("expected Tool variant"),
        }
    }

    #[test]
    fn update_last_tool_status_ignores_text_messages() {
        let mut app = test_app();
        app.add_message("assistant", "some text");
        // Should not panic — just a no-op
        app.update_last_tool_status(ToolStatus::Success);
        match &app.messages[0] {
            ChatMessage::Text { content, .. } => assert_eq!(content, "some text"),
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn update_last_tool_status_targets_last_message_only() {
        let mut app = test_app();
        app.add_tool("Read", "first tool", ToolStatus::Success);
        app.add_tool("Bash", "second tool", ToolStatus::Running);
        app.update_last_tool_status(ToolStatus::Error);
        // First tool unchanged
        match &app.messages[0] {
            ChatMessage::Tool { status, .. } => assert_eq!(*status, ToolStatus::Success),
            _ => panic!("expected Tool variant"),
        }
        // Second tool updated
        match &app.messages[1] {
            ChatMessage::Tool { status, .. } => assert_eq!(*status, ToolStatus::Error),
            _ => panic!("expected Tool variant"),
        }
    }

    #[test]
    fn mixed_messages_preserve_order() {
        let mut app = test_app();
        app.add_message("user", "do something");
        app.add_tool("Bash", "ls", ToolStatus::Running);
        app.update_last_tool_status(ToolStatus::Success);
        app.add_message("assistant", "done");

        assert_eq!(app.messages.len(), 3);
        assert!(matches!(&app.messages[0], ChatMessage::Text { role, .. } if role == "user"));
        assert!(matches!(
            &app.messages[1],
            ChatMessage::Tool {
                status: ToolStatus::Success,
                ..
            }
        ));
        assert!(matches!(&app.messages[2], ChatMessage::Text { role, .. } if role == "assistant"));
    }
}

#[cfg(test)]
mod tuishot_shots {
    use super::*;
    use tuishot::Tuishot;

    /// Version string used in snapshot renders. Pinning this keeps version
    /// bumps from drifting every screenshot on every release.
    const SNAPSHOT_VERSION: &str = "TEST";

    fn sample_conversation() -> ChatApp {
        let theme = crate::theme::Theme::dark();
        let mut app = ChatApp::new("claude-sonnet-4-20250514", theme);
        app.version = SNAPSHOT_VERSION.to_string();

        app.add_message("user", "Can you read src/main.rs and explain what it does?");
        app.add_tool("Read", "src/main.rs (42 lines)", ToolStatus::Success);
        app.add_message(
            "assistant",
            "This is the entry point for **claux**. It parses CLI arguments via `clap`, \
             loads configuration from `~/.config/claux/config.toml`, and dispatches to \
             either the REPL or one-shot mode depending on the flags.\n\n\
             Key things:\n\
             - `--tui` launches the full-screen Ratatui interface\n\
             - `--resume <id>` reloads a previous session\n\
             - `-p <prompt>` runs a single query and exits",
        );

        app.status = "1.2k tokens".to_string();
        app
    }

    #[derive(Tuishot)]
    enum ChatShot {
        #[tuishot(
            name = "chat-conversation",
            description = "Mid-conversation with tool use and markdown"
        )]
        Conversation,

        #[tuishot(
            name = "chat-streaming",
            description = "Assistant mid-response with streaming cursor"
        )]
        Streaming,

        #[tuishot(
            name = "chat-permission",
            description = "Prompting for Bash permission"
        )]
        Permission,

        #[tuishot(name = "chat-empty", description = "Fresh chat, no messages")]
        Empty,
    }

    impl ChatShotRender for ChatShot {
        fn render(&self, buf: &mut ratatui::buffer::Buffer, area: ratatui::layout::Rect) {
            let theme = crate::theme::Theme::dark();
            let mut app = match self {
                ChatShot::Conversation => sample_conversation(),
                ChatShot::Streaming => {
                    let mut app = sample_conversation();
                    app.mode = Mode::Streaming;
                    app.stream_buffer = "Sure, let me look at the configuration handling next. \
                        The config module uses `toml` for parsing and supports both global \
                        and per-project overrides"
                        .to_string();
                    app.thinking = false;
                    app
                }
                ChatShot::Permission => {
                    let mut app = sample_conversation();
                    app.mode = Mode::Permission;
                    app.permission_prompt = Some("Allow Bash command?".to_string());
                    app.permission_details = Some(vec![
                        "Command:".to_string(),
                        "  cargo test --lib".to_string(),
                        "".to_string(),
                        "Working directory: /home/user/dev/claux".to_string(),
                    ]);
                    app
                }
                ChatShot::Empty => {
                    let mut app = ChatApp::new("claude-sonnet-4-20250514", theme);
                    app.version = SNAPSHOT_VERSION.to_string();
                    app
                }
            };
            let rendered = tuishot::render_to_buffer(area.width, area.height, |f| {
                ui::draw_chat(f, &mut app);
            });
            buf.clone_from(&rendered);
        }
    }

    #[test]
    fn capture_chat_screens() {
        ChatShot::check_all().expect("chat screen capture");
    }
}