tmux-select 0.0.8

Agent-aware tmux window picker with a built-in fuzzy finder for Claude Code, Codex, Kimi Code, Pi, OpenCode, Qwen Code, and Grok Build
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
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AgentKind {
    Claude,
    Codex,
    Kimi,
    Pi,
    OpenCode,
    Qwen,
    Grok,
}

impl AgentKind {
    pub fn from_name(name: &str) -> Option<AgentKind> {
        match name {
            "claude" => Some(AgentKind::Claude),
            "codex" => Some(AgentKind::Codex),
            "kimi" => Some(AgentKind::Kimi),
            "pi" => Some(AgentKind::Pi),
            "opencode" => Some(AgentKind::OpenCode),
            "qwen" => Some(AgentKind::Qwen),
            "grok" => Some(AgentKind::Grok),
            _ => None,
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            AgentKind::Claude => "claude",
            AgentKind::Codex => "codex",
            AgentKind::Kimi => "kimi",
            AgentKind::Pi => "pi",
            AgentKind::OpenCode => "opencode",
            AgentKind::Qwen => "qwen",
            AgentKind::Grok => "grok",
        }
    }

    // The Claude installer symlinks ~/.local/bin/claude to a binary literally named
    // by its version (e.g. .../share/claude/versions/2.1.173), and macOS records the
    // resolved file's name as the process name, so the basename alone cannot identify
    // the agent there.
    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
    pub fn from_path(path: &str) -> Option<AgentKind> {
        let mut components = path.split('/').rev();
        let basename = components.next()?;
        if let Some(kind) = AgentKind::from_name(basename) {
            return Some(kind);
        }
        if path.ends_with("/qwen-code/node/bin/node") || is_qwen_entry_path(path) {
            return Some(AgentKind::Qwen);
        }
        if !is_version_shaped(basename) {
            return None;
        }
        components.find_map(AgentKind::from_name)
    }
}

pub(crate) fn is_qwen_entry_path(path: &str) -> bool {
    path.ends_with("/qwen-code/lib/cli-entry.js")
        || path.ends_with("/qwen-code/lib/cli.js")
        || path.ends_with("/@qwen-code/qwen-code/cli-entry.js")
        || path.ends_with("/@qwen-code/qwen-code/cli.js")
}

#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
fn is_version_shaped(name: &str) -> bool {
    let bytes = name.as_bytes();
    match (bytes.first(), bytes.last()) {
        (Some(first), Some(last)) if first.is_ascii_digit() && last.is_ascii_digit() => {
            bytes.iter().all(|b| b.is_ascii_digit() || *b == b'.')
        }
        _ => false,
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AgentState {
    Idle,
    Working,
    Blocked,
}

impl AgentState {
    pub fn label(self) -> &'static str {
        match self {
            AgentState::Idle => "idle",
            AgentState::Working => "working",
            AgentState::Blocked => "blocked",
        }
    }
}

const LIVE_LINES: usize = 8;
const CLAUDE_SPINNER: [char; 7] = ['·', '', '', '', '', '', '*'];
const KIMI_MOON_SPINNER: [char; 8] = ['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘'];
const KIMI_BRAILLE_SPINNER: [char; 10] = ['', '', '', '', '', '', '', '', '', ''];
const PI_BRAILLE_SPINNER: [char; 10] = [
    '\u{280b}', '\u{2819}', '\u{2839}', '\u{2838}', '\u{283c}', '\u{2834}', '\u{2826}', '\u{2827}',
    '\u{2807}', '\u{280f}',
];
const GROK_BRAILLE_SPINNER: [char; 8] = [
    '\u{280b}', '\u{2819}', '\u{2839}', '\u{2838}', '\u{283c}', '\u{2834}', '\u{2826}', '\u{2827}',
];
const KIMI_SWARM_ACTIVITY: [&str; 5] = [
    "Orchestrating...",
    "Prompting...",
    "Working...",
    "Queued...",
    "Rate limited...",
];

pub fn match_state(kind: AgentKind, screen: &str) -> AgentState {
    let live = bottom_lines(screen, LIVE_LINES);

    if live.iter().any(|&line| is_blocked(kind, line))
        || (kind == AgentKind::Codex
            && codex_has_menu_options(
                &live,
                "Keep current model",
                "Keep current model (never show again)",
            ))
    {
        return AgentState::Blocked;
    }
    if live.iter().any(|&line| is_working(kind, line))
        || (kind == AgentKind::Codex
            && codex_has_menu_options(&live, "Dismiss and keep waiting", "Learn more"))
        || (kind == AgentKind::Kimi && kimi_has_active_swarm(&live))
    {
        return AgentState::Working;
    }
    AgentState::Idle
}

fn codex_has_menu_options(lines: &[&str], first: &str, second: &str) -> bool {
    let mut options = lines.iter().copied().filter_map(codex_menu_option);
    options.any(|option| option == first) && options.any(|option| option == second)
}

fn codex_menu_option(line: &str) -> Option<&str> {
    let head = line.trim();
    let head = head.strip_prefix("\u{203a} ").unwrap_or(head);
    let (number, text) = head.split_once(". ")?;
    if number.is_empty() || !number.bytes().all(|byte| byte.is_ascii_digit()) {
        return None;
    }
    Some(text.split_once("  ").map_or(text, |(label, _)| label))
}

fn is_working(kind: AgentKind, line: &str) -> bool {
    match kind {
        AgentKind::Claude => {
            let head = line.trim_start();
            (head.starts_with(CLAUDE_SPINNER) && head.contains("… (")) || head.starts_with('')
        }
        AgentKind::Codex => {
            line.contains("to interrupt)") || line.contains("background terminal running")
        }
        AgentKind::Kimi => kimi_is_working(line),
        AgentKind::Pi => pi_is_working(line),
        AgentKind::OpenCode => opencode_is_working(line),
        AgentKind::Qwen => qwen_is_working(line),
        AgentKind::Grok => grok_is_working(line),
    }
}

fn pi_is_working(line: &str) -> bool {
    let head = line.trim_start();
    let mut chars = head.chars();
    let Some(frame) = chars.next() else {
        return false;
    };
    if !PI_BRAILLE_SPINNER.contains(&frame) {
        return false;
    }
    let status = chars.as_str().trim_start();
    status.starts_with("Working...")
        || status.starts_with("Retrying (")
        || status.starts_with("Compacting context...")
        || status.starts_with("Auto-compacting...")
        || status.starts_with("Context overflow detected, Auto-compacting...")
        || status.starts_with("Summarizing branch...")
}

fn opencode_is_working(line: &str) -> bool {
    line.contains("esc interrupt") || line.contains("esc again to interrupt")
}

fn qwen_is_working(line: &str) -> bool {
    let Some((_, footer)) = line.rsplit_once('(') else {
        return false;
    };
    let Some(status) = footer.strip_suffix(" \u{b7} esc to cancel)") else {
        return false;
    };
    let elapsed = status
        .split_once(" \u{b7} ")
        .map_or(status, |(time, _)| time);
    let mut parts = elapsed.split_whitespace();
    let Some(first) = parts.next() else {
        return false;
    };
    qwen_is_duration_part(first) && parts.all(qwen_is_duration_part)
}

fn qwen_is_duration_part(part: &str) -> bool {
    let bytes = part.as_bytes();
    bytes.len() > 1
        && matches!(bytes.last(), Some(b'h' | b'm' | b's'))
        && bytes[..bytes.len() - 1].iter().all(u8::is_ascii_digit)
}

fn grok_is_working(line: &str) -> bool {
    let head = line.trim_start();
    if head.contains("[stop]")
        || head.contains(" still running")
        || head.contains("send a message to interrupt")
    {
        return true;
    }
    head.chars()
        .next()
        .is_some_and(|frame| GROK_BRAILLE_SPINNER.contains(&frame))
}

fn kimi_is_working(line: &str) -> bool {
    let head = line.trim_start();
    let mut chars = head.chars();
    let Some(frame) = chars.next() else {
        return false;
    };
    if KIMI_MOON_SPINNER.contains(&frame) {
        return true;
    }
    if KIMI_BRAILLE_SPINNER.contains(&frame) {
        let status = chars.as_str().trim_start();
        if status.starts_with("thinking...")
            || status.starts_with("working...")
            || status.contains("Agent Starting")
            || status.contains("Agent Queued")
            || status.contains("Agent Running")
        {
            return true;
        }
    }
    false
}

fn kimi_has_active_swarm(lines: &[&str]) -> bool {
    let has_header = lines
        .iter()
        .any(|line| line.trim_start().starts_with("─ Agent Swarm"));
    has_header
        && lines.iter().any(|line| {
            let head = line.trim_start();
            KIMI_SWARM_ACTIVITY
                .iter()
                .any(|status| kimi_is_swarm_activity_line(head, status))
        })
}

fn kimi_is_swarm_activity_line(line: &str, status: &str) -> bool {
    match status {
        "Orchestrating..." => line == status,
        "Prompting..." => line == status || line.starts_with("Prompting... "),
        "Working..." | "Rate limited..." => {
            line == status
                || line
                    .strip_prefix(status)
                    .is_some_and(|tail| tail.starts_with(""))
        }
        "Queued..." => line.split_once(' ').is_some_and(|(id, text)| {
            id.len() == 3 && id.bytes().all(|byte| byte.is_ascii_digit()) && text == "Queued..."
        }),
        _ => false,
    }
}

fn is_blocked(kind: AgentKind, line: &str) -> bool {
    match kind {
        AgentKind::Claude => line.contains("Esc to cancel") && line.contains('·'),
        AgentKind::Codex => {
            line.contains("to submit answer")
                || line.contains("to submit all")
                || (line.contains("to confirm or") && line.contains("to cancel"))
        }
        AgentKind::Kimi => kimi_is_blocked(line),
        AgentKind::Pi => pi_is_blocked(line),
        AgentKind::OpenCode => opencode_is_blocked(line),
        AgentKind::Qwen => qwen_is_blocked(line),
        AgentKind::Grok => grok_is_blocked(line),
    }
}

fn pi_is_blocked(line: &str) -> bool {
    let head = line.trim();
    (head.starts_with("\u{2191}\u{2193} navigate")
        && head.contains(" select")
        && head.ends_with(" cancel"))
        || (head.contains(" submit  ") && head.ends_with(" cancel"))
        || head
            .split_once(" submit  ")
            .and_then(|(_, hints)| hints.split_once(" newline  "))
            .and_then(|(_, hints)| hints.split_once(" cancel  "))
            .is_some_and(|(_, hint)| hint.ends_with(" external editor"))
}

fn opencode_is_blocked(line: &str) -> bool {
    let head = line.trim();
    head.contains("Permission required")
        || head.contains("Reject permission")
        || head == "Always allow"
        || (head.contains("enter ") && head.contains("esc dismiss"))
}

fn qwen_is_blocked(line: &str) -> bool {
    line.contains("No, suggest changes (esc)")
        || line.contains("No, keep planning (esc)")
        || line.contains("Save and close external editor to continue")
        || line.contains("A potential loop was detected")
        || (line.contains("Enter to confirm") && line.contains("Esc to cancel"))
        || (line.contains("Enter: Select") && line.contains("Esc: Cancel"))
        || (line.contains("Enter: Confirm") && line.contains("Esc: Cancel"))
}

fn grok_is_blocked(line: &str) -> bool {
    let head = line.trim_start();
    let waiting_diamond = head.starts_with('\u{25c6}') || head.starts_with('\u{2666}');
    (waiting_diamond && head.contains("[stop]"))
        || head.contains("Waiting on plan approval")
        || head.contains("No plan written: approve or request changes")
        || (head.contains("always-approve") && head.contains("cancel"))
        || (head.contains("next answer") && head.contains("dismiss"))
}

fn kimi_is_blocked(line: &str) -> bool {
    let head = line.trim();
    (head.starts_with("↑/↓ select · ") && head.contains(" choose · ↵ confirm"))
        || head == "Type feedback · ↵ submit."
        || (head.starts_with("↑↓ select")
            && (head.contains("↵ choose")
                || head.contains("↵ toggle")
                || head.contains("↵ confirm")))
        || (head.starts_with("type answer") && head.contains("↵ save"))
}

fn bottom_lines(screen: &str, n: usize) -> Vec<&str> {
    let mut lines: Vec<&str> = screen
        .lines()
        .rev()
        .filter(|line| !line.trim().is_empty())
        .take(n)
        .collect();
    lines.reverse();
    lines
}

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

    fn claude_frame(body: &str) -> String {
        format!("{body}\n────────────\n\n────────────\n  ~/projects/x  claude\n")
    }

    #[test]
    fn from_name_matches_only_known_agents() {
        assert_eq!(AgentKind::from_name("claude"), Some(AgentKind::Claude));
        assert_eq!(AgentKind::from_name("codex"), Some(AgentKind::Codex));
        assert_eq!(AgentKind::from_name("kimi"), Some(AgentKind::Kimi));
        assert_eq!(AgentKind::from_name("pi"), Some(AgentKind::Pi));
        assert_eq!(AgentKind::from_name("opencode"), Some(AgentKind::OpenCode));
        assert_eq!(AgentKind::from_name("qwen"), Some(AgentKind::Qwen));
        assert_eq!(AgentKind::from_name("grok"), Some(AgentKind::Grok));
        assert_eq!(AgentKind::from_name("npm"), None);
        assert_eq!(AgentKind::from_name("node"), None);
    }

    #[test]
    fn from_path_matches_agent_basenames() {
        assert_eq!(
            AgentKind::from_path("/opt/homebrew/bin/codex"),
            Some(AgentKind::Codex)
        );
        assert_eq!(
            AgentKind::from_path("/Users/me/.local/bin/claude"),
            Some(AgentKind::Claude)
        );
        assert_eq!(AgentKind::from_path("claude"), Some(AgentKind::Claude));
        assert_eq!(
            AgentKind::from_path("/home/me/.kimi-code/bin/kimi"),
            Some(AgentKind::Kimi)
        );
        assert_eq!(
            AgentKind::from_path("/home/me/.opencode/bin/opencode"),
            Some(AgentKind::OpenCode)
        );
        assert_eq!(
            AgentKind::from_path("/home/me/.local/bin/qwen"),
            Some(AgentKind::Qwen)
        );
        assert_eq!(
            AgentKind::from_path("/home/me/.local/bin/grok"),
            Some(AgentKind::Grok)
        );
        assert_eq!(AgentKind::from_path("/usr/local/bin/node"), None);
    }

    #[test]
    fn from_path_matches_a_version_named_binary_under_an_agent_directory() {
        assert_eq!(
            AgentKind::from_path("/Users/izakharchanka/.local/share/claude/versions/2.1.173"),
            Some(AgentKind::Claude)
        );
        assert_eq!(
            AgentKind::from_path("/opt/codex/versions/9.9"),
            Some(AgentKind::Codex)
        );
    }

    #[test]
    fn from_path_matches_qwen_node_launchers() {
        for path in [
            "/home/me/.local/lib/qwen-code/node/bin/node",
            "/home/me/.local/lib/qwen-code/lib/cli-entry.js",
            "/usr/lib/node_modules/@qwen-code/qwen-code/lib/cli.js",
            "/usr/lib/node_modules/@qwen-code/qwen-code/cli-entry.js",
            "/usr/lib/node_modules/@qwen-code/qwen-code/cli.js",
        ] {
            assert_eq!(AgentKind::from_path(path), Some(AgentKind::Qwen), "{path}");
        }
    }

    #[test]
    fn from_path_rejects_version_binaries_outside_agent_directories_and_vice_versa() {
        assert_eq!(AgentKind::from_path("/opt/foo/versions/2.1.173"), None);
        assert_eq!(
            AgentKind::from_path("/Users/me/projects/claude/target/debug/mytool"),
            None
        );
        assert_eq!(AgentKind::from_path("/opt/claude/versions/2.1.173b"), None);
        assert_eq!(
            AgentKind::from_path("/Users/me/projects/qwen-code/test.js"),
            None
        );
        assert_eq!(
            AgentKind::from_path("/usr/lib/node_modules/@qwen-code/qwen-code/scripts/test.js"),
            None
        );
        assert_eq!(AgentKind::from_path(""), None);
    }

    #[test]
    fn claude_spinner_is_working() {
        for status in [
            "· Channeling… (1s · ↓ 21 tokens · thinking)",
            "✢ Forging… (2s · thinking with high effort)",
            "✻ Sublimating… (5s · ↓ 146 tokens · thought for 2s)",
        ] {
            assert_eq!(
                match_state(AgentKind::Claude, &claude_frame(status)),
                AgentState::Working,
                "{status}"
            );
        }
    }

    #[test]
    fn claude_spinner_with_a_non_gerund_title_is_working() {
        for status in [
            "✻ Phase B: baby form from Quaternius pack… (12m 47s · ↓ 30.8k tokens)",
            "✶ Yak-shaving… (3s · ↓ 1.2k tokens)",
            "* Cooking dinner… (1s)",
        ] {
            assert_eq!(
                match_state(AgentKind::Claude, &claude_frame(status)),
                AgentState::Working,
                "{status}"
            );
        }
    }

    #[test]
    fn claude_running_workflow_is_working() {
        for footer in [
            "  ◯ sleep-2min  Spawn one subagent ... 0/1 agents done · 49s · ↓ 16.0k tokens",
            "  ◯ review-codebase  Review ... 6/7 agents done · 2m 54s · ↓ 429.1k tokens",
        ] {
            assert_eq!(
                match_state(AgentKind::Claude, &claude_frame(footer)),
                AgentState::Working,
                "{footer}"
            );
        }
    }

    #[test]
    fn claude_idle_prompt_with_a_typed_gerund_is_idle() {
        assert_eq!(
            match_state(AgentKind::Claude, &claude_frame("❯ fix parsing…")),
            AgentState::Idle
        );
    }

    #[test]
    fn claude_selection_prompt_is_blocked() {
        let screen = "Do you want to make this edit to file.rs?\n  1. Yes\n  2. No\nEnter to select · ↑/↓ to navigate · Esc to cancel\n";
        assert_eq!(match_state(AgentKind::Claude, screen), AgentState::Blocked);
    }

    #[test]
    fn claude_bash_approval_dialog_is_blocked() {
        let screen = "\
● Thinking for 13s, listing 2 directories… (ctrl+o to expand)
  ⎿  $ ls /home/zenpie/projects/prmt/.github/workflows/ 2>/dev/null &&
     echo \"===\" && wc -l /home/zenpie/projects/prmt/.github/workflows/*
────────────────────────────────────────
 Bash command

   ls /home/zenpie/projects/prmt/.github/workflows/ 2>/dev/null
   List workflows in prmt and gwt

 Do you want to proceed?
 ❯ 1. Yes
   2. Yes, allow reading from workflows/ from this project
   3. No

 Esc to cancel · Tab to amend · ctrl+e to explain
";
        assert_eq!(match_state(AgentKind::Claude, screen), AgentState::Blocked);
    }

    #[test]
    fn claude_single_action_esc_hint_is_idle() {
        assert_eq!(
            match_state(
                AgentKind::Claude,
                &claude_frame("Edit and press Enter to retry, or Esc to cancel")
            ),
            AgentState::Idle
        );
    }

    #[test]
    fn codex_working_line_is_working() {
        for line in [
            "• Working (13s • esc to interrupt) · 1 background terminal running · /ps to view · /stop to close",
            "Working (0s • esc to interrupt)",
            "• Thinking (5s • esc to interrupt)",
            "• Reviewing approval request (2s • esc to interrupt)",
            "1 background terminal running · /ps to view · /stop to close",
        ] {
            let screen = format!("$ cargo build\n{line}\n\nctrl+t to view transcript\n");
            assert_eq!(
                match_state(AgentKind::Codex, &screen),
                AgentState::Working,
                "{line}"
            );
        }
    }

    #[test]
    fn codex_question_prompt_is_blocked() {
        for footer in [
            "tab to add notes | enter to submit answer | esc to interrupt",
            "enter to submit all · esc to interrupt",
            "ctrl + j to submit answer · esc to interrupt",
        ] {
            let screen = format!("Which option do you prefer?\n  1. A\n  2. B\n{footer}\n");
            assert_eq!(
                match_state(AgentKind::Codex, &screen),
                AgentState::Blocked,
                "{footer}"
            );
        }
    }

    #[test]
    fn codex_approval_overlay_is_blocked() {
        let screen = "Would you like to run the following command?\n$ rm -rf build\n  Yes\n  No\nPress enter to confirm or esc to cancel\n";
        assert_eq!(match_state(AgentKind::Codex, screen), AgentState::Blocked);
    }

    #[test]
    fn codex_generic_settings_popup_is_idle() {
        let screen = "Select model\n  gpt-5\n  o3\nPress enter to confirm or esc to go back\n";
        assert_eq!(match_state(AgentKind::Codex, screen), AgentState::Idle);
    }

    #[test]
    fn codex_plain_prompt_is_idle() {
        let screen = "$ cargo build\nbuild succeeded\n\nctrl+t to view transcript\n";
        assert_eq!(match_state(AgentKind::Codex, screen), AgentState::Idle);
    }

    // Preserve upstream wrapping and column spacing so fixtures catch label/description overlap.
    // https://github.com/openai/codex/tree/b1a547b1f73ce86205d9222ac19cff334b3b7a2e/codex-rs/tui/src/chatwidget/snapshots
    const CODEX_WAITING_WITH_RETRY: &str = concat!(
        "  Our systems are thinking a bit more about this request before responding.\n",
        "  Hang tight or retry with a faster model for a quicker response, though it\n",
        "  may be less capable of handling complex requests.\n",
        "\n",
        "\u{203a} 1. Retry with a faster model\n",
        "  2. Dismiss and keep waiting\n",
        "  3. Learn more\n",
        "\n",
        "  No action is required. Codex will keep waiting, and this menu will close when\n",
        "  the response is ready.\n",
    );
    const CODEX_WAITING_WITHOUT_RETRY: &str = concat!(
        "  Our systems are thinking a bit more about this request before responding.\n",
        "\n",
        "\u{203a} 1. Dismiss and keep waiting\n",
        "  2. Learn more\n",
        "\n",
        "  No action is required. Codex will keep waiting, and this menu will close when\n",
        "  the response is ready.\n",
    );
    const CODEX_RATE_LIMIT_SWITCH: &str = concat!(
        "  Approaching rate limits\n",
        "  Switch to gpt-5.6-luna for lower credit usage?\n",
        "\n",
        "\u{203a} 1. Switch to gpt-5.6-luna                 Fast and affordable agentic coding\n",
        "                                            model.\n",
        "  2. Keep current model\n",
        "  3. Keep current model (never show again)  Hide future rate limit reminders\n",
        "                                            about switching models.\n",
        "\n",
        "  Press enter to confirm or esc to go back\n",
    );

    #[test]
    fn codex_waiting_menus_are_working() {
        for screen in [CODEX_WAITING_WITH_RETRY, CODEX_WAITING_WITHOUT_RETRY] {
            assert_eq!(match_state(AgentKind::Codex, screen), AgentState::Working);
        }
    }

    #[test]
    fn codex_rate_limit_switch_is_blocked() {
        assert_eq!(
            match_state(AgentKind::Codex, CODEX_RATE_LIMIT_SWITCH),
            AgentState::Blocked
        );
    }

    #[test]
    fn codex_menu_selection_does_not_change_state() {
        for (screen, count, expected) in [
            (CODEX_WAITING_WITH_RETRY, 3, AgentState::Working),
            (CODEX_WAITING_WITHOUT_RETRY, 2, AgentState::Working),
            (CODEX_RATE_LIMIT_SWITCH, 3, AgentState::Blocked),
        ] {
            let unselected = screen.replace('\u{203a}', " ");
            assert_eq!(match_state(AgentKind::Codex, &unselected), expected);
            for selected in 1..=count {
                let screen = unselected.replacen(
                    &format!("  {selected}. "),
                    &format!("\u{203a} {selected}. "),
                    1,
                );
                assert_eq!(match_state(AgentKind::Codex, &screen), expected, "{screen}");
            }
        }
    }

    #[test]
    fn codex_rate_limit_switch_accepts_other_models() {
        let screen = CODEX_RATE_LIMIT_SWITCH.replace("gpt-5.6-luna", "another-model");
        assert_eq!(match_state(AgentKind::Codex, &screen), AgentState::Blocked);
    }

    #[test]
    fn codex_isolated_menu_lines_and_unnumbered_prose_are_idle() {
        for screen in [
            CODEX_WAITING_WITH_RETRY,
            CODEX_WAITING_WITHOUT_RETRY,
            CODEX_RATE_LIMIT_SWITCH,
        ] {
            for line in screen.lines() {
                assert_eq!(
                    match_state(AgentKind::Codex, line),
                    AgentState::Idle,
                    "{line}"
                );
            }
            let prose = screen
                .replace("\u{203a} 1. ", "")
                .replace("  2. ", "")
                .replace("  3. ", "");
            assert_eq!(match_state(AgentKind::Codex, &prose), AgentState::Idle);
        }
    }

    #[test]
    fn codex_menus_require_complete_numbered_labels_in_order() {
        for screen in [
            "  1. Learn more\n  2. Dismiss and keep waiting\n",
            "  2. Keep current model (never show again)\n  3. Keep current model\n",
            "  2. Keep current model (never show again)\n  3. Keep current model (never show again)\n",
            "  1. Dismiss and keep waiting now\n  2. Learn more\n",
            "  1. Dismiss and keep waiting\n  2. Learn more about models\n",
            "  2. Keep current model settings\n  3. Keep current model (never show again)\n",
            "  2. Keep current model\n  3. Keep current model (never show again) please\n",
            "  x. Dismiss and keep waiting\n  2. Learn more\n",
            "  . Dismiss and keep waiting\n  2. Learn more\n",
            "  2. Keep current model\n  x. Keep current model (never show again)\n",
        ] {
            assert_eq!(
                match_state(AgentKind::Codex, screen),
                AgentState::Idle,
                "{screen}"
            );
        }
    }

    #[test]
    fn codex_blocked_menus_win_over_working() {
        let switching_while_working = CODEX_RATE_LIMIT_SWITCH.replace(
            "  Approaching rate limits",
            "Working (1s - esc to interrupt)",
        );
        let switching_while_waiting =
            format!("{CODEX_RATE_LIMIT_SWITCH}  1. Dismiss and keep waiting\n  2. Learn more\n");
        let approval_while_waiting =
            format!("{CODEX_WAITING_WITH_RETRY}Press enter to confirm or esc to cancel\n");
        for screen in [
            switching_while_working,
            switching_while_waiting,
            approval_while_waiting,
        ] {
            assert_eq!(match_state(AgentKind::Codex, &screen), AgentState::Blocked);
        }
    }

    #[test]
    fn codex_menu_options_must_both_fit_in_the_live_region() {
        for (options, expected) in [
            (
                "  1. Dismiss and keep waiting\n  2. Learn more\n",
                AgentState::Working,
            ),
            (
                "  2. Keep current model\n  3. Keep current model (never show again)\n",
                AgentState::Blocked,
            ),
        ] {
            for (filler_count, state) in [(6, expected), (7, AgentState::Idle)] {
                let screen = format!("{options}{}", "transcript line\n\n".repeat(filler_count));
                assert_eq!(match_state(AgentKind::Codex, &screen), state);
            }
        }
    }

    #[test]
    fn kimi_moon_spinner_is_working() {
        for frame in KIMI_MOON_SPINNER {
            let screen = format!("tool output\n  {frame} Running command\n> \n");
            assert_eq!(
                match_state(AgentKind::Kimi, &screen),
                AgentState::Working,
                "{frame}"
            );
        }
    }

    #[test]
    fn kimi_braille_activity_is_working() {
        for frame in KIMI_BRAILLE_SPINNER {
            for status in [
                "thinking...",
                "working...",
                "Reviewer Agent Starting · 0 tools · 0s",
                "Reviewer Agent Queued · 0 tools · 1s",
                "Reviewer Agent Running · 2 tools · 3s",
            ] {
                let screen = format!("tool output\n  {frame} {status}\n> \n");
                assert_eq!(
                    match_state(AgentKind::Kimi, &screen),
                    AgentState::Working,
                    "{frame} {status}"
                );
            }
        }
    }

    #[test]
    fn kimi_swarm_activity_is_working() {
        for status in KIMI_SWARM_ACTIVITY {
            let status_line = match status {
                "Orchestrating..." => status.to_string(),
                "Prompting..." => format!("{status} review each module"),
                "Queued..." => format!("001 {status}"),
                _ => format!("{status}  ━━━━━"),
            };
            let screen = format!("─ Agent Swarm ─ review\n\n{status_line}\n> \n");
            assert_eq!(
                match_state(AgentKind::Kimi, &screen),
                AgentState::Working,
                "{status}"
            );
        }
    }

    #[test]
    fn kimi_approval_prompt_is_blocked() {
        let screen = "\
────────────────────────────────────────
  ▶ Run this command?

  $ cargo test

  ▶ 1. Approve
    2. Approve for this session
    3. Reject

  ↑/↓ select · 1/2/3 choose · ↵ confirm
────────────────────────────────────────
";
        assert_eq!(match_state(AgentKind::Kimi, screen), AgentState::Blocked);
    }

    #[test]
    fn kimi_approval_feedback_is_blocked() {
        let screen = "\
  ▶ 3. Reject

  Type feedback · ↵ submit.
────────────────────────────────────────
";
        assert_eq!(match_state(AgentKind::Kimi, screen), AgentState::Blocked);
    }

    #[test]
    fn kimi_question_prompt_is_blocked() {
        for footer in [
            "  ↑↓ select  1-3 / ↵ choose  ←/→/tab switch  esc cancel",
            "  ↑↓ select  1-3 / ↵ toggle  ←/→/tab switch  esc cancel",
            "  ↑↓ select  1/2 choose  ↵ confirm  ←/→/tab switch  esc cancel",
            "  type answer  ↵ save  tab switch  esc cancel",
        ] {
            let screen = format!(" question\n\n ? Which option?\n\n{footer}\n────────────\n");
            assert_eq!(
                match_state(AgentKind::Kimi, &screen),
                AgentState::Blocked,
                "{footer}"
            );
        }
    }

    #[test]
    fn kimi_blocked_prompt_wins_over_spinner() {
        let screen = "\
🌗
  ▶ Run this command?
  ▶ 1. Approve
    2. Reject
  ↑/↓ select · 1/2 choose · ↵ confirm
────────────────────────────────────────
";
        assert_eq!(match_state(AgentKind::Kimi, screen), AgentState::Blocked);
    }

    #[test]
    fn kimi_ambiguous_spinner_symbols_are_idle() {
        for line in [
            "⠋ Downloading update",
            "⣀⣄⣤⣦⣶⣷⣿",
            "◐ backgrounded",
            "",
            "Working... final response",
            "Queued...",
            "Completed.  ━━━━━",
        ] {
            assert_eq!(
                match_state(AgentKind::Kimi, &format!("{line}\n> \n")),
                AgentState::Idle,
                "{line}"
            );
        }
    }

    #[test]
    fn kimi_completed_swarm_with_response_text_is_idle() {
        let screen = "\
─ Agent Swarm ─ review

 Completed.  ━━━━━

Working... final response
>
";
        assert_eq!(match_state(AgentKind::Kimi, screen), AgentState::Idle);
    }

    #[test]
    fn kimi_plain_prompt_is_idle() {
        let screen = "Welcome to Kimi Code\n\n> \n~/projects/tmux-select\n";
        assert_eq!(match_state(AgentKind::Kimi, screen), AgentState::Idle);
    }

    #[test]
    fn pi_loader_activity_is_working() {
        for frame in PI_BRAILLE_SPINNER {
            for status in [
                "Working...",
                "Working... (esc to interrupt)",
                "Retrying (1/3) in 2s...",
                "Compacting context...",
                "Auto-compacting...",
                "Context overflow detected, Auto-compacting... (esc to cancel)",
                "Summarizing branch...",
            ] {
                let screen = format!("tool output\n  {frame} {status}\n> \n");
                assert_eq!(
                    match_state(AgentKind::Pi, &screen),
                    AgentState::Working,
                    "{frame} {status}"
                );
            }
        }
    }

    #[test]
    fn pi_extension_prompts_are_blocked() {
        for footer in [
            "\u{2191}\u{2193} navigate  enter select  esc cancel",
            "enter submit  esc cancel",
            "enter submit  shift+enter newline  esc cancel  ctrl+g external editor",
            "ctrl+s submit  ctrl+j newline  ctrl+c cancel  ctrl+e external editor",
        ] {
            let screen = format!("Extension prompt\n  option\n{footer}\n");
            assert_eq!(
                match_state(AgentKind::Pi, &screen),
                AgentState::Blocked,
                "{footer}"
            );
        }
    }

    #[test]
    fn pi_multiline_editor_prompt_wins_over_spinner() {
        let screen = "\u{280b} Working...\nEdit response\n> draft\nenter submit  shift+enter newline  esc cancel  ctrl+g external editor\n";
        assert_eq!(match_state(AgentKind::Pi, screen), AgentState::Blocked);
    }

    #[test]
    fn pi_unrelated_loader_is_idle() {
        let screen = format!("{} Downloading update\n> \n", PI_BRAILLE_SPINNER[0]);
        assert_eq!(match_state(AgentKind::Pi, &screen), AgentState::Idle);
    }

    #[test]
    fn pi_overflow_compaction_text_without_spinner_is_idle() {
        let screen = "Context overflow detected, Auto-compacting... (esc to cancel)\n> \n";
        assert_eq!(match_state(AgentKind::Pi, screen), AgentState::Idle);
    }

    #[test]
    fn opencode_busy_prompt_is_working() {
        for footer in ["esc interrupt", "esc again to interrupt"] {
            let screen = format!("Build project\n{footer}\n");
            assert_eq!(
                match_state(AgentKind::OpenCode, &screen),
                AgentState::Working,
                "{footer}"
            );
        }
    }

    #[test]
    fn opencode_permission_and_question_prompts_are_blocked() {
        for marker in [
            "Permission required",
            "Reject permission",
            "Always allow",
            "enter submit  esc dismiss",
            "enter toggle  esc dismiss",
            "enter confirm  esc dismiss",
        ] {
            let screen = format!("prompt\n{marker}\n");
            assert_eq!(
                match_state(AgentKind::OpenCode, &screen),
                AgentState::Blocked,
                "{marker}"
            );
        }
    }

    #[test]
    fn opencode_plain_prompt_is_idle() {
        assert_eq!(
            match_state(AgentKind::OpenCode, "Ask anything\nctrl+x leader\n"),
            AgentState::Idle
        );
    }

    #[test]
    fn qwen_responding_status_is_working() {
        for status in [
            ". Working (4s \u{b7} esc to cancel)",
            ".. Responding (12s \u{b7} \u{2193} 641 tokens \u{b7} esc to cancel)",
            ".. Responding (1m 12s \u{b7} \u{2193} 1.2k tokens \u{b7} 16 t/s \u{b7} esc to cancel)",
        ] {
            assert_eq!(
                match_state(AgentKind::Qwen, &format!("{status}\n")),
                AgentState::Working,
                "{status}"
            );
        }
    }

    #[test]
    fn qwen_confirmation_prompts_are_blocked() {
        for marker in [
            "No, suggest changes (esc)",
            "No, keep planning (esc)",
            "Save and close external editor to continue",
            "A potential loop was detected",
            "Enter to confirm, Esc to cancel",
            "Enter to confirm \u{b7} Esc to cancel",
            "Up/Down: Navigate | Enter: Select | Esc: Cancel",
            "Up/Down: Navigate | Enter: Confirm | Esc: Cancel",
        ] {
            assert_eq!(
                match_state(AgentKind::Qwen, &format!("prompt\n{marker}\n")),
                AgentState::Blocked,
                "{marker}"
            );
        }
    }

    #[test]
    fn qwen_non_progress_status_is_idle() {
        for status in [". Waiting", "(Esc to cancel)"] {
            assert_eq!(
                match_state(AgentKind::Qwen, &format!("{status}\n")),
                AgentState::Idle,
                "{status}"
            );
        }
    }

    #[test]
    fn grok_turn_activity_is_working() {
        for line in [
            format!("{} Thinking... 2s [stop]", GROK_BRAILLE_SPINNER[0]),
            format!("{} Starting session... 0:01", GROK_BRAILLE_SPINNER[1]),
            "waiting - send a message to interrupt".to_string(),
            "1 command still running".to_string(),
        ] {
            assert_eq!(
                match_state(AgentKind::Grok, &format!("{line}\n")),
                AgentState::Working,
                "{line}"
            );
        }
    }

    #[test]
    fn grok_user_waits_are_blocked() {
        for line in [
            "\u{25c6} Run command 2s [stop]",
            "Waiting on plan approval",
            "No plan written: approve or request changes",
            "1-4 select  Ctrl+O always-approve  Ctrl+C cancel",
            "Tab next answer  Esc back  X dismiss",
        ] {
            assert_eq!(
                match_state(AgentKind::Grok, &format!("{line}\n")),
                AgentState::Blocked,
                "{line}"
            );
        }
    }

    #[test]
    fn grok_idle_edit_prompt_is_idle() {
        assert_eq!(
            match_state(
                AgentKind::Grok,
                "\u{25c6} agent idle - waiting on your edit\n"
            ),
            AgentState::Idle
        );
    }

    #[test]
    fn frozen_transcript_is_idle() {
        let screen = "✻ Crunched for 6m 41s\n✻ Waiting for 1 dynamic workflow to finish\n● Dynamic workflow \"x\" completed · 11s\n✻ Churned for 43s\n  7 tasks (6 done, 1 open)\n  ◻ a\n  ✔ b\n  ✔ c\n  … +2 completed\n────────────\n❯ sequence the fixes, critical first\n────────────\n  ~/projects/arpg  claude  75%\n  ⏵⏵ auto mode on\n";
        assert_eq!(match_state(AgentKind::Claude, screen), AgentState::Idle);
    }

    #[test]
    fn spinner_outside_the_live_region_stays_idle() {
        let mut screen = String::from("✻ Sublimating… (1s · ↓ 9 tokens)\n");
        for _ in 0..24 {
            screen.push_str("filler transcript line\n");
        }
        screen.push_str("────────────\n\n────────────\n  ~/p  claude\n");
        assert_eq!(match_state(AgentKind::Claude, &screen), AgentState::Idle);
    }

    #[test]
    fn codex_ignores_a_claude_spinner() {
        let screen = "· Channeling… (1s · ↓ 21 tokens)\n\n";
        assert_eq!(match_state(AgentKind::Codex, screen), AgentState::Idle);
    }

    #[test]
    fn empty_screen_is_idle() {
        assert_eq!(match_state(AgentKind::Claude, ""), AgentState::Idle);
        assert_eq!(match_state(AgentKind::Codex, ""), AgentState::Idle);
        assert_eq!(match_state(AgentKind::Kimi, ""), AgentState::Idle);
        assert_eq!(match_state(AgentKind::Pi, ""), AgentState::Idle);
        assert_eq!(match_state(AgentKind::OpenCode, ""), AgentState::Idle);
        assert_eq!(match_state(AgentKind::Qwen, ""), AgentState::Idle);
        assert_eq!(match_state(AgentKind::Grok, ""), AgentState::Idle);
    }
}