pi_agent_rust 0.5.1

Native AI coding agent CLI - Rust port of Pi Agent
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
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
use super::*;
use crate::agent::AgentConfig;
use crate::model::{ContentBlock, StreamEvent, TextContent};
use crate::provider::{Context, Provider, StreamOptions};
use crate::resources::{ResourceCliOptions, ResourceLoader};
use crate::tools::ToolRegistry;
use asupersync::channel::mpsc;
use asupersync::runtime::RuntimeBuilder;
use bubbletea::{KeyMsg, Message, WindowSizeMsg};
use futures::stream;
use serde_json::json;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::{Arc, OnceLock};

struct DummyProvider;

#[async_trait::async_trait]
impl Provider for DummyProvider {
    fn name(&self) -> &'static str {
        "dummy"
    }

    fn api(&self) -> &'static str {
        "dummy"
    }

    fn model_id(&self) -> &'static str {
        "dummy-model"
    }

    async fn stream(
        &self,
        _context: &Context<'_>,
        _options: &StreamOptions,
    ) -> crate::error::Result<
        Pin<Box<dyn futures::Stream<Item = crate::error::Result<StreamEvent>> + Send>>,
    > {
        Ok(Box::pin(stream::empty()))
    }
}

fn test_runtime_handle() -> asupersync::runtime::RuntimeHandle {
    static RT: OnceLock<asupersync::runtime::Runtime> = OnceLock::new();
    RT.get_or_init(|| {
        RuntimeBuilder::current_thread()
            .build()
            .expect("build asupersync runtime")
    })
    .handle()
}

fn test_model_entry() -> ModelEntry {
    ModelEntry {
        model: crate::provider::Model {
            id: "gpt-5.2".to_string(),
            name: "gpt-5.2".to_string(),
            api: "openai-responses".to_string(),
            provider: "openai".to_string(),
            base_url: "https://example.invalid".to_string(),
            reasoning: true,
            input: vec![crate::provider::InputType::Text],
            cost: crate::provider::ModelCost {
                input: 0.0,
                output: 0.0,
                cache_read: 0.0,
                cache_write: 0.0,
            },
            context_window: 128_000,
            max_tokens: 8_192,
            headers: std::collections::HashMap::new(),
        },
        api_key: None,
        headers: std::collections::HashMap::new(),
        auth_header: false,
        compat: None,
        oauth_config: None,
    }
}

fn build_test_app(cwd: PathBuf) -> PiApp {
    let config = Config::default();
    let provider: Arc<dyn Provider> = Arc::new(DummyProvider);
    let agent = Agent::new(
        provider,
        ToolRegistry::new(&[], &cwd, Some(&config)),
        AgentConfig::default(),
    );
    let resources = ResourceLoader::empty(config.enable_skill_commands());
    let resource_cli = ResourceCliOptions {
        no_skills: false,
        no_prompt_templates: false,
        no_extensions: false,
        no_themes: false,
        skill_paths: Vec::new(),
        prompt_paths: Vec::new(),
        extension_paths: Vec::new(),
        theme_paths: Vec::new(),
    };
    let model_entry = test_model_entry();
    let (event_tx, _event_rx) = mpsc::channel(64);

    PiApp::new(
        agent,
        Arc::new(asupersync::sync::Mutex::new(Session::in_memory())),
        config,
        resources,
        resource_cli,
        cwd,
        model_entry.clone(),
        vec![model_entry.clone()],
        vec![model_entry],
        None,
        Vec::new(),
        event_tx,
        test_runtime_handle(),
        false,
        false,
        None,
        Some(KeyBindings::new()),
        Vec::new(),
        Usage::default(),
        None,
    )
}

fn tempdir() -> tempfile::TempDir {
    std::fs::create_dir_all(std::env::temp_dir()).expect("create temp root");
    tempfile::tempdir().expect("tempdir")
}

#[test]
fn prepare_startup_changelog_skips_disk_write_when_persistence_disabled() {
    let dir = tempdir();
    let cwd = dir.path().join("workspace");
    std::fs::create_dir_all(&cwd).expect("create cwd");
    let settings_path = dir.path().join("settings.json");
    let mut config = Config {
        last_changelog_version: Some("0.9.0".to_string()),
        ..Config::default()
    };

    let changelog = "## 1.0.0\n- Added startup changelog notices\n\n## 0.9.0\n- Previous release\n";
    let startup = prepare_startup_changelog_with_roots(
        &mut config,
        dir.path(),
        &cwd,
        Some(&settings_path),
        false,
        false,
        "1.0.0",
        || changelog,
    );

    assert_eq!(
        startup,
        Some(StartupChangelog::Full {
            markdown: "## 1.0.0\n- Added startup changelog notices".to_string(),
        })
    );
    assert!(
        !settings_path.exists(),
        "startup construction should not write settings"
    );
    assert_eq!(config.last_changelog_version.as_deref(), Some("1.0.0"));
}

#[test]
fn prepare_startup_changelog_writes_when_persistence_enabled() {
    let dir = tempdir();
    let cwd = dir.path().join("workspace");
    std::fs::create_dir_all(&cwd).expect("create cwd");
    let settings_path = dir.path().join("settings.json");
    let mut config = Config {
        last_changelog_version: Some("0.9.0".to_string()),
        ..Config::default()
    };

    let startup = prepare_startup_changelog_with_roots(
        &mut config,
        dir.path(),
        &cwd,
        Some(&settings_path),
        false,
        true,
        "1.0.0",
        || "## 1.0.0\n- Added startup changelog notices\n\n## 0.9.0\n- Previous release\n",
    );

    assert!(matches!(startup, Some(StartupChangelog::Full { .. })));
    let saved: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&settings_path).expect("read settings"))
            .expect("parse settings");
    assert_eq!(saved["lastChangelogVersion"].as_str(), Some("1.0.0"));
}

#[test]
fn extract_file_references_removes_indented_ref_line_without_leaving_blank_whitespace() {
    let dir = tempdir();
    std::fs::write(dir.path().join("notes.txt"), "hi").expect("write file");
    let mut app = build_test_app(dir.path().to_path_buf());

    let (cleaned, refs) = app.extract_file_references("Summary:\n  @notes.txt\nNext line");

    assert_eq!(cleaned, "Summary:\nNext line");
    assert_eq!(refs, vec!["notes.txt".to_string()]);
}

#[test]
fn extract_file_references_preserves_newline_before_trailing_punctuation() {
    let dir = tempdir();
    std::fs::write(dir.path().join("notes.txt"), "hi").expect("write file");
    let mut app = build_test_app(dir.path().to_path_buf());

    let (cleaned, refs) = app.extract_file_references("Summary:\n@notes.txt.");

    assert_eq!(cleaned, "Summary:\n.");
    assert_eq!(refs, vec!["notes.txt".to_string()]);
}

#[test]
fn is_inside_jj_repo_detects_root_directly() {
    let dir = tempdir();
    std::fs::create_dir(dir.path().join(".jj")).expect("mkdir .jj");
    assert!(super::is_inside_jj_repo(dir.path()));
}

#[test]
fn is_inside_jj_repo_walks_up_to_ancestor() {
    let dir = tempdir();
    let root = dir.path();
    std::fs::create_dir(root.join(".jj")).expect("mkdir .jj");
    let nested = root.join("a").join("b").join("c");
    std::fs::create_dir_all(&nested).expect("mkdir nested");
    assert!(super::is_inside_jj_repo(&nested));
}

#[test]
fn is_inside_jj_repo_false_when_no_dot_jj_anywhere() {
    let dir = tempdir();
    let nested = dir.path().join("a").join("b");
    std::fs::create_dir_all(&nested).expect("mkdir nested");
    assert!(!super::is_inside_jj_repo(&nested));
}

#[test]
fn is_inside_jj_repo_requires_dot_jj_to_be_a_directory() {
    // A file named `.jj` is a gitlink-like stub in some tooling; only a
    // real `.jj/` directory counts as a jj repo for display purposes.
    let dir = tempdir();
    std::fs::write(dir.path().join(".jj"), "not a dir").expect("write stub");
    assert!(!super::is_inside_jj_repo(dir.path()));
}

#[test]
fn read_jj_change_returns_none_outside_jj_repo() {
    // No `.jj` anywhere -> must short-circuit without forking a
    // subprocess and without touching $PATH for the `jj` binary.
    let dir = tempdir();
    assert!(super::read_jj_change(dir.path()).is_none());
}

#[test]
fn read_vcs_info_falls_back_to_git_when_no_jj() {
    // Seed a minimal `.git/HEAD` pointing at a branch. With no `.jj`
    // anywhere, read_vcs_info must return the git branch name unchanged.
    let dir = tempdir();
    let dot_git = dir.path().join(".git");
    std::fs::create_dir(&dot_git).expect("mkdir .git");
    std::fs::write(dot_git.join("HEAD"), "ref: refs/heads/feature/jj-demo\n").expect("seed HEAD");

    let vcs = super::read_vcs_info(dir.path());
    assert_eq!(vcs.as_deref(), Some("feature/jj-demo"));
}

#[test]
fn render_header_uses_cycle_thinking_binding_hint() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(200, 40);

    let header = app.render_header();

    assert!(header.contains("shift+tab: thinking"), "header: {header}");
    assert!(!header.contains("ctrl+t: thinking"), "header: {header}");
    assert!(
        header.contains("\x1b]0;Pi · openai/gpt-5.2 · ready\x07"),
        "live header must emit the delight terminal title: {header:?}"
    );
}

/// Issue #200: a named session (via `/name` or a resumed named session)
/// titles the terminal tab after itself; unnamed sessions keep the model
/// label (pinned by `render_header_uses_cycle_thinking_binding_hint`).
#[test]
fn render_header_titles_terminal_after_session_name() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(200, 40);
    {
        let mut session = app.session.try_lock().expect("session lock");
        session.set_name("refactor-plan");
    }

    let header = app.render_header();
    assert!(
        header.contains("\x1b]0;Pi · refactor-plan · ready\x07"),
        "named session must title the tab after itself: {header:?}"
    );
}

#[test]
fn live_view_renders_default_welcome_and_powerline_status() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(200, 40);
    app.startup_welcome.clear();

    let view = app.view();

    assert!(view.contains("Welcome to Pi!"), "view: {view}");
    assert!(view.contains("Tip: Type /help"), "view: {view}");
    assert!(view.contains("ACT"), "powerline mode missing: {view}");
    assert!(
        view.contains("ctx: 0%"),
        "powerline context missing: {view}"
    );
}

#[test]
fn quiet_startup_does_not_render_welcome_screen_fallback() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(200, 40);
    app.config.quiet_startup = Some(true);
    app.startup_welcome.clear();

    let view = app.view();

    assert!(!view.contains("Welcome to Pi!"), "view: {view}");
    assert!(!view.contains("Tip: Type /help"), "view: {view}");
}

#[test]
fn live_view_applies_rich_markdown_enhancements() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(200, 40);
    app.messages.push(ConversationMessage::new(
        MessageRole::Assistant,
        "Use #ff5500 for \\alpha.".to_string(),
        None,
    ));

    let view = app.view();

    assert!(view.contains('α'), "LaTeX enhancement missing: {view}");
    assert!(view.contains("â–  #ff5500"), "hex swatch missing: {view}");
}

#[test]
fn enter_accepts_highlighted_autocomplete_item() {
    // Regression for issue #61: with the slash dropdown open and an entry
    // highlighted (e.g. user pressed Down to select `/model`), pressing Enter
    // must accept the highlighted item — matching the dropdown's own footer
    // hint "Enter/Tab accept" — not submit the raw `/` typed so far.
    use crate::autocomplete::{AutocompleteItem, AutocompleteItemKind};
    use bubbletea::{KeyMsg, KeyType, Message};

    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());

    app.input.set_value("/");
    app.autocomplete.open = true;
    app.autocomplete.items = vec![AutocompleteItem {
        kind: AutocompleteItemKind::SlashCommand,
        label: "/model".to_string(),
        insert: "/model ".to_string(),
        description: None,
    }];
    app.autocomplete.selected = Some(0);
    app.autocomplete.replace_range = 0..1;

    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));

    assert_eq!(
        app.input.value(),
        "/model ",
        "Enter with a highlighted dropdown entry must accept the item"
    );
    assert!(
        !app.autocomplete.open,
        "Accepting via Enter should close the dropdown"
    );
}

#[test]
fn enter_submits_when_no_autocomplete_item_highlighted() {
    // The dual contract for issue #61: when the dropdown is open but the
    // user has not navigated to any item (selected.is_none()), Enter must
    // still submit the raw editor contents — i.e. behavior is unchanged
    // for users who never pressed Down.
    use bubbletea::{KeyMsg, KeyType, Message};

    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());

    app.input.set_value("/foo");
    app.autocomplete.open = true;
    app.autocomplete.items.clear();
    app.autocomplete.selected = None;
    app.autocomplete.replace_range = 0..4;

    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));

    assert!(
        !app.autocomplete.open,
        "Enter with no selection should still close the dropdown"
    );
}

#[derive(Default)]
struct TuiDegradationDrillTrace {
    event_count: usize,
    redraw_count: usize,
    coalesced_count: usize,
    preserved_input_count: usize,
    max_rendered_rows: usize,
}

impl TuiDegradationDrillTrace {
    fn event(&mut self) {
        self.event_count += 1;
    }

    fn render(&mut self, app: &PiApp) -> String {
        let frame = app.view();
        self.redraw_count += 1;
        self.max_rendered_rows = self.max_rendered_rows.max(frame.lines().count());
        frame
    }

    fn record_input_preserved(&mut self, before_len: usize, after: &str) {
        self.preserved_input_count += after.len().saturating_sub(before_len);
    }
}

fn pressure_tool_block(label: &str) -> ContentBlock {
    let mut output = String::new();
    for line in 0..32 {
        output.push_str(label);
        output.push_str(" line ");
        output.push_str(&line.to_string());
        output.push('\n');
    }
    ContentBlock::Text(TextContent::new(output))
}

fn semantic_visible(frame: &str, marker: &str) -> bool {
    frame.contains(marker)
}

const PROVIDER_DELTA_COUNT: usize = 72;
const THINKING_DELTA_COUNT: usize = 12;
const TOOL_UPDATE_COUNT: usize = 18;
const SESSION_BURST_COUNT: usize = 10;
const FINAL_MARKER: &str = "semantic-provider-delta-71";
const TOOL_MARKER: &str = "semantic-tool-final";
const SESSION_MARKER: &str = "session write burst 9 committed";

fn seed_normal_tui_load(app: &mut PiApp, trace: &mut TuiDegradationDrillTrace) {
    app.messages.push(ConversationMessage::new(
        MessageRole::User,
        "normal-load prompt remains readable".to_string(),
        None,
    ));
    app.messages.push(ConversationMessage::new(
        MessageRole::Assistant,
        "normal-load assistant reply remains readable".to_string(),
        None,
    ));
    app.scroll_to_bottom();
    trace.event_count += 2;
    let normal_frame = trace.render(app);
    assert!(semantic_visible(
        &normal_frame,
        "normal-load assistant reply"
    ));
}

fn drive_provider_pressure(app: &mut PiApp, trace: &mut TuiDegradationDrillTrace) {
    app.tui_pressure_frame_p99_us.store(
        TuiPressureController::HIGH_FRAME_P99_US,
        std::sync::atomic::Ordering::Relaxed,
    );
    app.handle_pi_message(PiMsg::AgentStart);
    trace.event();
    for idx in 0..PROVIDER_DELTA_COUNT {
        let delta = format!("semantic-provider-delta-{idx} ");
        app.handle_pi_message(PiMsg::TextDelta(delta));
        trace.event();
        if idx % 16 == 0 {
            let frame = trace.render(app);
            assert!(
                semantic_visible(&frame, &format!("semantic-provider-delta-{idx}")),
                "streaming provider delta must stay visible at idx {idx}"
            );
        }
    }
    for idx in 0..THINKING_DELTA_COUNT {
        app.handle_pi_message(PiMsg::ThinkingDelta(format!("thinking-step-{idx} ")));
        trace.event();
    }
}

fn drive_tool_pressure(app: &mut PiApp, trace: &mut TuiDegradationDrillTrace) {
    app.handle_pi_message(PiMsg::ToolStart {
        name: "bash".to_string(),
        tool_id: "tool-pressure".to_string(),
    });
    trace.event();
    for idx in 0..TOOL_UPDATE_COUNT {
        let label = if idx + 1 == TOOL_UPDATE_COUNT {
            TOOL_MARKER
        } else {
            "low-value-tool-noise"
        };
        app.handle_pi_message(PiMsg::ToolUpdate {
            name: "bash".to_string(),
            tool_id: "tool-pressure".to_string(),
            content: vec![pressure_tool_block(label)],
            details: Some(json!({
                "line_count": (idx + 1) * 32,
                "byte_count": (idx + 1) * 512,
            })),
        });
        trace.event();
    }
    trace.coalesced_count += TOOL_UPDATE_COUNT.saturating_sub(1);
    app.handle_pi_message(PiMsg::ToolEnd {
        name: "bash".to_string(),
        tool_id: "tool-pressure".to_string(),
        is_error: false,
        output: None,
    });
    trace.event();
}

fn drive_session_write_bursts(app: &mut PiApp, trace: &mut TuiDegradationDrillTrace) {
    for idx in 0..SESSION_BURST_COUNT {
        app.handle_pi_message(PiMsg::SystemNote(format!(
            "session write burst {idx} committed"
        )));
        trace.event();
    }
}

fn drive_resize_pressure(app: &mut PiApp, trace: &mut TuiDegradationDrillTrace) {
    let _ = app.update(Message::new(WindowSizeMsg {
        width: 92,
        height: 26,
    }));
    trace.event();
    let compact_frame = trace.render(app);
    assert!(
        compact_frame.lines().count() <= app.term_height,
        "compact resize frame must not exceed terminal height"
    );

    let _ = app.update(Message::new(WindowSizeMsg {
        width: 120,
        height: 64,
    }));
    trace.event();
}

fn finish_agent_and_preserve_input(app: &mut PiApp, trace: &mut TuiDegradationDrillTrace) {
    app.handle_pi_message(PiMsg::AgentDone {
        usage: None,
        stop_reason: StopReason::Stop,
        error_message: None,
    });
    trace.event();

    for key in ['o', 'k'] {
        let before_len = app.input.value().len();
        let _ = app.update(Message::new(KeyMsg::from_char(key)));
        trace.event();
        trace.record_input_preserved(before_len, &app.input.value());
    }
}

fn assert_tui_degradation_evidence(app: &PiApp, trace: &mut TuiDegradationDrillTrace) {
    let final_frame = trace.render(app);
    let collapsed_tool_messages = app
        .messages
        .iter()
        .filter(|message| message.role == MessageRole::Tool && message.collapsed)
        .count();
    let semantic_visible_count = [
        semantic_visible(&final_frame, FINAL_MARKER),
        semantic_visible(&final_frame, TOOL_MARKER),
        semantic_visible(&final_frame, SESSION_MARKER),
        app.input.value() == "ok",
    ]
    .into_iter()
    .filter(|visible| *visible)
    .count();
    let frame_pressure = TuiPressureController::decide(
        TuiPressureController::HIGH_FRAME_P99_US,
        TuiPressureController::HIGH_TOOL_OUTPUT_BYTES,
        TOOL_UPDATE_COUNT,
    );
    let evidence = json!({
        "schema": "pi.tui.degradation_drill.v1",
        "fixture": "sustained_event_pressure",
        "event_count": trace.event_count,
        "redraw_count": trace.redraw_count,
        "coalesced_count": trace.coalesced_count,
        "max_frame_budget_pressure": format!("{:?}", frame_pressure.level),
        "max_rendered_rows": trace.max_rendered_rows,
        "terminal_height": app.term_height,
        "preserved_input_count": trace.preserved_input_count,
        "semantic_visible_count": semantic_visible_count,
        "collapsed_tool_message_count": collapsed_tool_messages,
        "verdict": if semantic_visible_count == 4
            && collapsed_tool_messages == 1
            && trace.preserved_input_count == 2
            && trace.max_rendered_rows <= app.term_height
        {
            "pass"
        } else {
            "fail_closed"
        },
    });

    assert_eq!(evidence["schema"], "pi.tui.degradation_drill.v1");
    assert_eq!(evidence["event_count"], 122);
    assert_eq!(evidence["redraw_count"], 8);
    assert_eq!(evidence["coalesced_count"], TOOL_UPDATE_COUNT - 1);
    assert_eq!(evidence["max_frame_budget_pressure"], "High");
    assert_eq!(evidence["preserved_input_count"], 2);
    assert_eq!(evidence["collapsed_tool_message_count"], 1);
    assert_eq!(evidence["semantic_visible_count"], 4);
    assert_eq!(
        evidence["verdict"], "pass",
        "degradation evidence: {evidence}"
    );
}

#[test]
fn tui_degradation_drill_preserves_input_and_semantics_under_pressure() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    let mut trace = TuiDegradationDrillTrace::default();
    app.enable_frame_timing_for_test();
    app.reset_frame_timing_for_test();
    app.set_terminal_size(120, 48);

    seed_normal_tui_load(&mut app, &mut trace);
    drive_provider_pressure(&mut app, &mut trace);
    drive_tool_pressure(&mut app, &mut trace);
    drive_session_write_bursts(&mut app, &mut trace);
    drive_resize_pressure(&mut app, &mut trace);
    finish_agent_and_preserve_input(&mut app, &mut trace);
    assert_tui_degradation_evidence(&app, &mut trace);
}

/// bd-cv653.3.1: `/model <role> <spec>` assigns a session-scoped role
/// override and records a role-tagged ModelChange entry; `/model <role>`
/// alone reports the assignment; unknown role-like tokens never assign.
#[test]
fn slash_model_role_assignment_and_query() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());

    // Assign: exact provider/model match against available_models.
    let result = app.handle_slash_model("advisor openai/gpt-5.2");
    assert!(result.is_none(), "role assignment is a status-only action");
    assert_eq!(
        app.role_model_overrides
            .get(&crate::models::ModelRole::Advisor),
        Some(&("openai".to_string(), "gpt-5.2".to_string())),
        "override recorded for the advisor role"
    );

    // Session carries a role-tagged ModelChange entry.
    let guard = app.session.try_lock().expect("session lock");
    let role_entries: Vec<_> = guard
        .entries_for_current_path()
        .iter()
        .filter_map(|e| match e {
            crate::session::SessionEntry::ModelChange(mc) => Some(mc),
            _ => None,
        })
        .collect();
    assert!(
        role_entries
            .iter()
            .any(|mc| mc.role.as_deref() == Some("advisor")
                && mc.provider == "openai"
                && mc.model_id == "gpt-5.2"),
        "role-tagged ModelChange entry present"
    );
    drop(guard);

    // Query: `/model advisor` reports the assignment without changing state.
    app.status_message = None;
    let result = app.handle_slash_model("advisor");
    assert!(result.is_none());
    let status = app.status_message.clone().unwrap_or_default();
    assert!(
        status.contains("advisor") && status.contains("openai/gpt-5.2"),
        "query reports assignment, got: {status}"
    );

    // Planted negative: a two-token pattern whose first token is NOT a role
    // must not create any override.
    let before = app.role_model_overrides.len();
    let _ = app.handle_slash_model("notarole openai/gpt-5.2");
    assert_eq!(
        app.role_model_overrides.len(),
        before,
        "non-role first token must not assign"
    );
}

/// bd-cv653.3.9: the todo footer is state-driven — a TodoSummary message
/// renders the compact line in the next frame's view; `None` clears it and
/// the chrome height accounting tracks both states.
#[test]
fn todo_summary_message_drives_footer_line() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);

    let base_height = app.view_effective_conversation_height();
    assert!(app.todo_summary.is_none());
    assert!(!app.view().contains("1/2 · implement"));

    app.handle_pi_message(PiMsg::TodoSummary {
        summary: Some("1/2 · implement".to_string()),
    });
    let view = app.view();
    assert!(view.contains("todo"), "footer label rendered");
    assert!(view.contains("1/2 · implement"), "summary rendered");
    assert_eq!(
        app.view_effective_conversation_height(),
        base_height.saturating_sub(2),
        "todo footer consumes two chrome rows"
    );

    app.handle_pi_message(PiMsg::TodoSummary { summary: None });
    assert!(app.todo_summary.is_none());
    assert!(!app.view().contains("1/2 · implement"));
    assert_eq!(app.view_effective_conversation_height(), base_height);
}

/// bd-cv653.3.8: an ask card owns the input line — the question renders as a
/// system card, numbered answers advance through the questions, and answers
/// Issue #197: `/new` must restore the configured default thinking level
/// (clamped to the model), not hard-code `off`.
#[test]
fn new_session_thinking_level_resolves_configured_default() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());

    app.config.default_thinking_level = Some("max".to_string());
    assert_eq!(
        app.new_session_thinking_level(),
        app.model_entry
            .clamp_thinking_level(crate::model::ThinkingLevel::Max)
    );

    app.config.default_thinking_level = Some("low".to_string());
    assert_eq!(
        app.new_session_thinking_level(),
        crate::model::ThinkingLevel::Low
    );

    // Unset resolves exactly like launch: XHigh clamped to the model.
    app.config.default_thinking_level = None;
    assert_eq!(
        app.new_session_thinking_level(),
        app.model_entry
            .clamp_thinking_level(crate::model::ThinkingLevel::XHigh)
    );
}

/// route to AskTool::respond_ui (an expired request surfaces a status).
#[test]
fn ask_card_consumes_input_and_advances_questions() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    let tool = crate::ask::AskTool::new(crate::ask::AskPolicy::Recommended);
    app.ask_tool = Some(tool);

    let request: crate::ask::AskRequest = serde_json::from_value(serde_json::json!({
        "questions": [
            {"id": "q1", "question": "Pick one?", "recommended": 0,
             "options": [{"label": "Alpha"}, {"label": "Beta"}]},
            {"id": "q2", "question": "And another?",
             "options": [{"label": "Left"}, {"label": "Right"}]}
        ]
    }))
    .expect("ask request");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("req-1");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "req-1".to_string(),
        request,
    }));

    let view = app.view();
    assert!(view.contains("question 1 of 2"), "card rendered");
    assert!(view.contains("Alpha (recommended)"), "recommended badge");

    // First answer advances to the second card.
    app.submit_message("2");
    assert!(app.view().contains("question 2 of 2"));

    // Second answer completes; with no pending reply slot (this request was
    // injected directly, not via install_channel_ui) the expiry status shows.
    app.submit_message("left");
    assert!(app.active_ask_ui.is_none());
    assert_eq!(
        app.status_message.as_deref(),
        Some("Ask request expired before the answer")
    );

    // A fresh card can be dismissed with 'cancel'.
    let request: crate::ask::AskRequest = serde_json::from_value(serde_json::json!({
        "questions": [{"question": "Again?", "options": [{"label": "A"}, {"label": "B"}]}]
    }))
    .expect("ask request 2");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("req-2");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "req-2".to_string(),
        request,
    }));
    app.submit_message("cancel");
    assert!(app.active_ask_ui.is_none());
}

/// gh #184: ask cards arrive while the ask tool is still executing, i.e.
/// while the agent is busy. Enter must answer the card instead of queueing
/// the text as a steering message, and Escape must dismiss the card rather
/// than abort the turn.
#[test]
fn ask_card_answer_is_not_queued_as_steering_while_agent_busy() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    app.ask_tool = Some(crate::ask::AskTool::new(crate::ask::AskPolicy::Recommended));

    let request: crate::ask::AskRequest = serde_json::from_value(json!({
        "questions": [
            {"id": "q1", "question": "Retry or dump?",
             "options": [{"label": "Retry"}, {"label": "Dump"}]},
            {"id": "q2", "question": "Where?",
             "options": [{"label": "Here"}, {"label": "There"}]}
        ]
    }))
    .expect("ask request");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("req-busy");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "req-busy".to_string(),
        request,
    }));
    app.agent_state = AgentState::ToolRunning;
    assert!(app.view().contains("question 1 of 2"));

    // Number selection through the real Enter keybinding path.
    app.input.set_value("2");
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));
    assert!(
        app.view().contains("question 2 of 2"),
        "Enter must advance the card while busy"
    );
    assert!(
        app.input.value().is_empty(),
        "the first answer must be cleared before the next question"
    );
    let queued = app
        .message_queue
        .lock()
        .expect("message queue")
        .pop_steering();
    assert!(
        queued.is_empty(),
        "card answer must not be queued as steering"
    );
    assert_ne!(
        app.status_message.as_deref(),
        Some("Queued steering message")
    );

    // Free text is accepted the same way.
    app.input.set_value("somewhere else");
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));
    assert!(
        app.active_ask_ui.is_none(),
        "card completes after last answer"
    );
    assert!(
        app.input.value().is_empty(),
        "editor cleared after answering"
    );

    // Escape dismisses a fresh card without aborting the turn.
    let request: crate::ask::AskRequest = serde_json::from_value(json!({
        "questions": [{"question": "Again?", "options": [{"label": "A"}, {"label": "B"}]}]
    }))
    .expect("ask request 2");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("req-busy-2");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "req-busy-2".to_string(),
        request,
    }));
    assert!(app.active_ask_ui.is_some());
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Esc)));
    assert!(app.active_ask_ui.is_none(), "Escape dismisses the card");
    assert_ne!(
        app.status_message.as_deref(),
        Some("Aborting request..."),
        "Escape on a card must not abort the turn"
    );
    assert_eq!(app.agent_state, AgentState::ToolRunning);
}

/// bd-1qol9 harness helpers: a minimal text-answer extension card.
fn ext_input_card(id: &str, prompt_text: &str) -> ExtensionUiRequest {
    ExtensionUiRequest::new(
        id,
        "input",
        serde_json::json!({
            "title": "Ext",
            "message": prompt_text,
            "extension_id": "ext-cards"
        }),
    )
    .with_extension_id(Some("ext-cards".to_string()))
}

/// bd-1qol9 mixed arrival order (extension FIRST, ask SECOND): exactly one
/// card may own the editor; the later ask queues behind and promotes only
/// after the extension resolves, so answers can never be reordered.
#[test]
fn mixed_cards_extension_then_ask_serialize_in_arrival_order() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    app.ask_tool = Some(crate::ask::AskTool::new(crate::ask::AskPolicy::Recommended));

    app.handle_pi_message(PiMsg::ExtensionUiRequest(ext_input_card("e1", "Say?")));
    assert_eq!(
        app.active_input_card_kind,
        Some(InputCardKind::Extension),
        "extension activates first"
    );

    let request: crate::ask::AskRequest = serde_json::from_value(json!({
        "questions": [{"question": "Which?", "options": [{"label": "A"}]}]
    }))
    .expect("ask request");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("a1");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "a1".to_string(),
        request,
    }));
    assert_eq!(
        app.active_input_card_kind,
        Some(InputCardKind::Extension),
        "later ask must NOT steal the editor from the active card"
    );
    assert!(app.active_ask_ui.is_none());

    // The ext-active answer routes to the EXTENSION parser, not the ask one:
    // an arbitrary string would fail numeric ask parsing but succeed here.
    app.input.set_value("free-form reply");
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));
    assert!(app.active_extension_ui.is_none(), "ext resolved");
    assert_eq!(app.active_input_card_kind, Some(InputCardKind::Ask));
    assert!(app.view().contains("question 1 of 1"), "ask promoted");
    assert!(
        app.card_draft_snapshot.is_none(),
        "the consumed extension answer must not become a draft"
    );

    app.input.set_value("A");
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));
    assert!(app.active_ask_ui.is_none());
    assert!(app.active_input_card_kind.is_none());
}

/// bd-1qol9 reverse order (ask FIRST, extension SECOND) plus draft
/// capture/restore across answer and Escape resolution paths.
#[test]
fn mixed_cards_ask_then_ext_preserve_drafts_across_resolution_paths() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    app.ask_tool = Some(crate::ask::AskTool::new(crate::ask::AskPolicy::Recommended));

    // Preexisting steering draft gets snapshotted + cleared on activation.
    app.input.set_value("cargo test -- --filter bd_1");
    let request: crate::ask::AskRequest = serde_json::from_value(json!({
        "questions": [{"question": "Run tests?", "options": [{"label": "Yes"}, {"label": "No"}]}]
    }))
    .expect("ask request");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("a-order");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "a-order".to_string(),
        request,
    }));
    assert!(
        app.input.value().is_empty(),
        "card activation clears the captured draft"
    );
    assert!(app.card_draft_snapshot.is_some());

    app.handle_pi_message(PiMsg::ExtensionUiRequest(ext_input_card("e-order", "Env?")));
    assert_eq!(app.active_input_card_kind, Some(InputCardKind::Ask));

    // Answer the ACTIVE ask through the REAL Enter path; the queued ext
    // promotes automatically afterward.
    app.input.set_value("Yes");
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));
    assert_eq!(app.active_input_card_kind, Some(InputCardKind::Extension));

    // Generic Escape dismisses the promoted EXTENSION card without aborting
    // anything and restores the pre-card draft into the empty editor.
    app.agent_state = AgentState::ToolRunning;
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Esc)));
    assert!(app.active_extension_ui.is_none());
    assert!(app.active_input_card_kind.is_none());
    assert_ne!(
        app.status_message.as_deref(),
        Some("Aborting request..."),
        "Escape on a card never aborts the turn"
    );
    assert_eq!(
        app.input.value(),
        "cargo test -- --filter bd_1",
        "explicit merge policy restores the draft after the last card settles"
    );
    assert_eq!(app.agent_state, AgentState::ToolRunning);
    app.agent_state = AgentState::Idle;
}

/// bd-q66i1: normal (non-Escape) completion clears each consumed answer
/// before successor activation, then restores only the genuine pre-card draft
/// after the final card settles.
#[test]
fn normal_card_resolution_restores_only_the_preexisting_draft() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    app.ask_tool = Some(crate::ask::AskTool::new(crate::ask::AskPolicy::Recommended));
    app.input.set_value("keep this draft");

    app.handle_pi_message(PiMsg::ExtensionUiRequest(ext_input_card(
        "e-normal", "Env?",
    )));
    let request: crate::ask::AskRequest = serde_json::from_value(json!({
        "questions": [{"question": "Proceed?", "options": [{"label": "Yes"}]}]
    }))
    .expect("ask request");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("a-normal");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "a-normal".to_string(),
        request,
    }));

    app.input.set_value("extension answer");
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));
    assert_eq!(app.active_input_card_kind, Some(InputCardKind::Ask));
    assert!(
        app.input.value().is_empty(),
        "successor starts with an empty editor"
    );
    assert_eq!(app.card_draft_snapshot.as_deref(), Some("keep this draft"));

    app.input.set_value("Yes");
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));
    assert!(app.active_input_card_kind.is_none());
    assert_eq!(app.input.value(), "keep this draft");
    assert!(app.card_draft_snapshot.is_none());
}

/// bd-q66i1: Escape resolves exactly one order-ledger entry. The historical
/// double-pop skipped the second Ask entry and stranded the following
/// extension card.
#[test]
fn escape_advances_one_card_at_a_time_across_ask_ask_extension() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    app.ask_tool = Some(crate::ask::AskTool::new(crate::ask::AskPolicy::Recommended));

    for (id, label) in [("a-first", "First?"), ("a-second", "Second?")] {
        let request: crate::ask::AskRequest = serde_json::from_value(json!({
            "questions": [{"question": label, "options": [{"label": "Yes"}]}]
        }))
        .expect("ask request");
        if let Some(tool) = app.ask_tool.as_ref() {
            tool.register_channel_ui_request_for_tests(id);
        }
        app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
            id: id.to_string(),
            request,
        }));
    }
    app.handle_pi_message(PiMsg::ExtensionUiRequest(ext_input_card(
        "e-third", "Third?",
    )));

    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Esc)));
    assert_eq!(app.active_input_card_kind, Some(InputCardKind::Ask));
    assert_eq!(
        app.active_ask_ui
            .as_ref()
            .map(|card| card.request.id.as_str()),
        Some("a-second")
    );
    assert_eq!(app.input_card_order.front(), Some(&InputCardKind::Ask));

    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Esc)));
    assert_eq!(app.active_input_card_kind, Some(InputCardKind::Extension));
    assert_eq!(
        app.active_extension_ui
            .as_ref()
            .map(|request| request.id.as_str()),
        Some("e-third")
    );

    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Esc)));
    assert!(app.active_input_card_kind.is_none());
    assert!(app.input_card_order.is_empty());
}

/// gh #198: an approval/ask card arrives while the tool that raised it is
/// still running, so the agent is never `Idle` while the card is pending. The
/// editor used to be hidden (and its rows dropped from the layout budget) for
/// the whole time the card asked the user to type an answer, so the prompt
/// looked like an inert transcript line and timed out unanswered. While a
/// card is pending the editor must render and be budgeted exactly as when
/// idle; once the card resolves mid-turn it goes back to hidden.
#[test]
fn pending_ask_card_keeps_editor_visible_while_turn_is_running() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    app.ask_tool = Some(crate::ask::AskTool::new(crate::ask::AskPolicy::Recommended));
    app.agent_state = AgentState::ToolRunning;
    app.current_tool = Some("bash".to_string());

    assert!(
        !app.editor_input_is_available(),
        "no card pending: a running turn hides the editor"
    );
    let busy_height = app.view_effective_conversation_height();
    let busy_view = app.view();
    assert!(
        !busy_view.contains("Enter: send"),
        "no card pending: the editor header must not render mid-turn"
    );

    let request: crate::ask::AskRequest = serde_json::from_value(json!({
        "questions": [{
            "question": "Allow the `bash` tool to run?",
            "options": [{"label": "Allow"}, {"label": "Deny"}]
        }]
    }))
    .expect("ask request");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("a-approval");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "a-approval".to_string(),
        request,
    }));
    assert_eq!(app.active_input_card_kind, Some(InputCardKind::Ask));
    assert_eq!(app.agent_state, AgentState::ToolRunning);

    assert!(
        app.editor_input_is_available(),
        "a pending card must expose the editor even though the turn is running"
    );
    assert!(
        app.view_effective_conversation_height() < busy_height,
        "the editor rows must come out of the conversation budget so the card and editor both fit"
    );
    let card_view = app.view();
    assert!(
        card_view.contains("Allow the `bash` tool to run?"),
        "the card itself renders in the conversation"
    );
    assert!(
        card_view.contains("Enter: send"),
        "the editor header renders alongside the pending card"
    );

    // Answer through the real Enter path: the card resolves, the turn is
    // still running, and the editor hides again.
    app.input.set_value("1");
    let _ = app.update(Message::new(KeyMsg::from_type(KeyType::Enter)));
    assert!(app.active_ask_ui.is_none());
    assert!(app.active_input_card_kind.is_none());
    assert_eq!(app.agent_state, AgentState::ToolRunning);
    assert!(
        !app.editor_input_is_available(),
        "card resolved mid-turn: the editor hides until the turn ends"
    );
    app.agent_state = AgentState::Idle;
}

/// bd-q66i1: turn-end invalidation treats partial card input as consumed and
/// restores the genuine draft captured before the card burst.
#[test]
fn agent_done_discards_partial_extension_answer_and_restores_draft() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    app.input.set_value("original draft");
    app.handle_pi_message(PiMsg::ExtensionUiRequest(ext_input_card(
        "e-invalidated",
        "Answer?",
    )));
    app.input.set_value("partial card answer");

    let _ = app.handle_pi_message(PiMsg::AgentDone {
        usage: None,
        stop_reason: StopReason::Aborted,
        error_message: None,
    });

    assert!(app.active_extension_ui.is_none());
    assert!(app.active_input_card_kind.is_none());
    assert_eq!(app.input.value(), "original draft");
    assert!(app.card_draft_snapshot.is_none());
}

/// bd-1qol9 terminal/abort cleanup: AgentDone invalidates the ACTIVE ask,
/// every QUEUED card of both kinds, and answers them fail-closed BEFORE any
/// RunPending/idle input can run. Mutation-sensitive: before this fix the
/// queues survived the turn boundary and could swallow the next idle prompt.
#[test]
fn agent_done_invalidates_all_outstanding_cards_before_idle() {
    let dir = tempdir();
    let mut app = build_test_app(dir.path().to_path_buf());
    app.set_terminal_size(100, 30);
    app.ask_tool = Some(crate::ask::AskTool::new(crate::ask::AskPolicy::Recommended));

    let request: crate::ask::AskRequest = serde_json::from_value(json!({
        "questions": [{"question": "Active?", "options": [{"label": "A"}]}]
    }))
    .expect("active ask");
    if let Some(tool) = app.ask_tool.as_ref() {
        tool.register_channel_ui_request_for_tests("a-done");
        tool.register_channel_ui_request_for_tests("a-queued");
    }
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "a-done".to_string(),
        request,
    }));
    app.handle_pi_message(PiMsg::AskUiRequest(crate::ask::AskUiRequest {
        id: "a-queued".to_string(),
        request: serde_json::from_value(json!({
            "questions": [{"question": "Queued?", "options": [{"label": "B"}]}]
        }))
        .expect("queued ask"),
    }));
    app.handle_pi_message(PiMsg::ExtensionUiRequest(ext_input_card("e-done", "late?")));

    assert!(!app.ask_ui_queue.is_empty());
    assert!(!app.extension_ui_queue.is_empty());

    app.pending_inputs
        .push_back(PendingInput::Text("queued user input".to_string()));
    let cmd = app.handle_pi_message(PiMsg::AgentDone {
        usage: None,
        stop_reason: StopReason::Aborted,
        error_message: None,
    });

    assert!(cmd.is_some(), "idle handoff still schedules RunPending");
    assert!(app.active_ask_ui.is_none());
    assert!(app.active_extension_ui.is_none());
    assert!(app.ask_ui_queue.is_empty(), "queued asks invalidated");
    assert!(app.extension_ui_queue.is_empty(), "queued exts invalidated");
    assert!(app.active_input_card_kind.is_none());
    assert!(
        app.status_message.as_deref() == Some("Question dismissed")
            || app
                .status_message
                .as_deref()
                .is_some_and(|msg| msg.starts_with("Ask request expired")),
        "dismissal surfaced: {:?}",
        app.status_message
    );
}