brgr-cli 2.15.6

Let Codex or Claude Code hand work to other coding agents, each in its own Herdr pane: the brgr CLI and Herdr plugin.
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
//! What brgr presses so a worker is never left for a person to click.
//!
//! Orchestrating through brgr is an unconditional-trust contract, so a trust
//! prompt, a "continue" notice, an update offer, or an update that has already
//! been applied is answered here. A screen no rule covers is reported with its
//! text and given a deadline instead of waiting forever.

use std::{
    fs::OpenOptions,
    io::Write as _,
    path::{Path, PathBuf},
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};

use anyhow::{Result, bail};

use super::{
    Herdr,
    native::{squash, trust_workspace_matches},
};
use crate::{Paths, cli::PaneRunArgs};

/// How long the agent gets to redraw after a key press before the screen is
/// read again. Without it the same prompt is answered twice.
const REDRAW: Duration = Duration::from_millis(1500);

/// How long a screen no rule covers may stand before the run fails.
pub(super) const SCREEN_DEADLINE: Duration = Duration::from_mins(3);

/// The unknown-screen deadline, shortened for tests only.
fn screen_deadline() -> Duration {
    #[cfg(debug_assertions)]
    if let Some(millis) = std::env::var("BRGR_TEST_SCREEN_DEADLINE_MS")
        .ok()
        .and_then(|value| value.parse::<u64>().ok())
    {
        return Duration::from_millis(millis);
    }
    SCREEN_DEADLINE
}

#[derive(Debug, Eq, PartialEq)]
pub(super) enum Screen {
    /// No dialog brgr recognizes.
    Clear,
    /// A dialog a rule answers with these keys.
    Resolve {
        rule: &'static str,
        keys: Vec<&'static str>,
    },
    /// A menu or dialog no rule answers; carries its first lines.
    Unknown(String),
}

/// Classifies a pane's visible text. Pure: it never reads or presses anything.
pub(super) fn classify(screen: &str, workspace: &Path) -> Screen {
    if trust_workspace_matches(screen, workspace) {
        let options = menu_options(screen);
        let yes = options
            .iter()
            .position(|option| option.text.starts_with("Yes, I trust this folder"));
        let keys = if let (Some(selected), Some(yes)) =
            (options.iter().position(|option| option.selected), yes)
        {
            // A numbered menu: move to "Yes" from wherever the cursor is.
            let mut keys = keys_between(selected, yes);
            keys.push("enter");
            keys
        } else {
            let flat = squash(screen);
            let selected_yes = ["❯", "›", ">"]
                .iter()
                .any(|cursor| flat.contains(&format!("{cursor}Yes,Itrustthisfolder")));
            if selected_yes {
                vec!["enter"]
            } else {
                vec!["down", "enter"]
            }
        };
        return Screen::Resolve {
            rule: "workspace-trust",
            keys,
        };
    }
    if let Some(resolution) = codex_trust(screen, workspace, || {
        crate::workspace::primary_checkout(workspace)
    }) {
        return resolution;
    }
    if let Some(resolution) = copilot_trust(screen, workspace) {
        return resolution;
    }
    if let Some(resolution) = cursor_trust(screen, workspace) {
        return resolution;
    }
    let lower = screen.to_lowercase();
    // A trust prompt for some other folder is not the task's to accept.
    if squash(&lower).contains("trustthisfolder") {
        return Screen::Unknown(head(screen));
    }
    // The harness updated itself and wants a restart. The dialog carries an
    // `esc` affordance; Enter ("ok") is what quit OpenCode.
    if lower.contains("update complete")
        && lower.contains("restart")
        && screen
            .lines()
            .any(|line| matches!(line.trim().to_lowercase().as_str(), "ok" | "esc"))
    {
        return Screen::Resolve {
            rule: "update-applied",
            keys: vec!["esc"],
        };
    }
    let options = menu_options(screen);
    let selected = options.iter().position(|option| option.selected);
    if let (Some(selected), Some((rule, target))) = (selected, accepting_option(&lower, &options)) {
        let mut keys = keys_between(selected, target);
        keys.push("enter");
        return Screen::Resolve { rule, keys };
    }
    // An update menu comes first: its highlighted option is "Update now", so a
    // stray "press Enter" line on the same screen must not become Enter.
    if let Some(selected) = selected
        && is_update_offer(&lower)
        && let Some(skip) = options.iter().position(|option| is_skip(&option.text))
    {
        let mut keys = keys_between(selected, skip);
        keys.push("enter");
        return Screen::Resolve {
            rule: "update-offer-skip",
            keys,
        };
    }
    if selected.is_none() {
        return if has_continue_notice(screen) {
            Screen::Resolve {
                rule: "continue-notice",
                keys: vec!["enter"],
            }
        } else {
            Screen::Clear
        };
    }
    Screen::Unknown(head(screen))
}

/// A harness offering to update itself, not an agent asking about updating
/// something: "update" or "upgrade" with an offer's wording or a version.
fn is_update_offer(lower: &str) -> bool {
    (lower.contains("update") || lower.contains("upgrade"))
        && (lower.contains("available")
            || lower.contains("new version")
            || lower.contains("newer version")
            || has_version_number(lower))
}

/// A dotted version such as `0.2.0` or `v2.0.15`.
fn has_version_number(text: &str) -> bool {
    text.split(|character: char| !(character.is_ascii_digit() || character == '.'))
        .any(|token| {
            let parts: Vec<&str> = token.trim_matches('.').split('.').collect();
            parts.len() >= 3 && parts.iter().all(|part| !part.is_empty())
        })
}

/// The cursor presses that move from option `from` to option `to`.
fn keys_between(from: usize, to: usize) -> Vec<&'static str> {
    let key = if to > from { "down" } else { "up" };
    std::iter::repeat_n(key, to.abs_diff(from)).collect()
}

/// Claude Code's own gates in front of a run, answered by accepting: the
/// Bypass Permissions warning and a newly found MCP server.
fn accepting_option(lower: &str, options: &[MenuOption]) -> Option<(&'static str, usize)> {
    let position = |prefix: &str| {
        options
            .iter()
            .position(|option| option.text.to_lowercase().starts_with(prefix))
    };
    if lower.contains("bypass permissions mode") {
        return position("yes, i accept").map(|at| ("bypass-warning", at));
    }
    if lower.contains("mcp server") {
        // The single-server choice first; "use this and all future" only if it
        // is all there is.
        return position("use this mcp server")
            .or_else(|| position("use this"))
            .map(|at| ("mcp-trust", at));
    }
    None
}

fn has_continue_notice(screen: &str) -> bool {
    screen.lines().any(|line| {
        let line = line
            .trim()
            .trim_matches(['│', '┃', '║'])
            .trim()
            .to_lowercase();
        [
            "press enter to continue",
            "press return to continue",
            "press any key to continue",
        ]
        .iter()
        .any(|notice| line.contains(notice))
    })
}

/// Codex's own folder prompt ("Trust this folder? ... 1. Trust and continue").
/// Accepted only for the task's workspace, like Claude's.
/// `primary` runs `git` and is asked only for a worktree prompt: every poll of
/// every pane classifies its screen.
fn codex_trust(
    screen: &str,
    workspace: &Path,
    primary: impl FnOnce() -> Option<PathBuf>,
) -> Option<Screen> {
    let flat = squash(screen);
    if !flat.contains("Trustthisfolder?") || !flat.contains("Trustandcontinue") {
        return None;
    }
    let workspace = squash(&workspace.to_string_lossy());
    let primary = flat
        .contains("repositoryroot:")
        .then(primary)
        .flatten()
        .map(|primary| squash(&primary.to_string_lossy()));
    // The path must end where the prompt's own sentence begins, so a longer
    // path ("/repo/task-evil", "/repo/task/sub") is not the task's workspace.
    // In a task worktree Codex adds that trust applies to the repository
    // root, which must then be exactly the repository the worktree came from.
    let shown = flat.split_once("Folderaccess").is_some_and(|(_, after)| {
        after
            .strip_prefix(workspace.trim_end_matches('/'))
            .map(|rest| rest.trim_start_matches('/'))
            .is_some_and(|rest| {
                rest.starts_with("Trustthisfolder")
                    || rest.starts_with("Note:")
                        && rest
                            .split_once("repositoryroot:")
                            .and_then(|(_, root)| root.split_once("Trustthisfolder"))
                            .is_some_and(|(root, _)| {
                                primary.as_deref().is_some_and(|primary| {
                                    root.trim_end_matches('/') == primary.trim_end_matches('/')
                                })
                            })
            })
    });
    Some(trust_choice(screen, shown, "codex-trust", |text| {
        text.starts_with("Trust and continue")
    }))
}

/// GitHub Copilot CLI's folder prompt ("Confirm folder trust … Do you trust the
/// files in this folder? 1. Yes"). Accepted only for the task's workspace: the
/// path is drawn wrapped inside a box, so it is compared with the borders
/// removed, and it must be exactly the workspace. "Yes, and remember this
/// folder" is never chosen; a task worktree does not outlive its task.
fn copilot_trust(screen: &str, workspace: &Path) -> Option<Screen> {
    let flat: String = screen
        .chars()
        .filter(|character| {
            !character.is_whitespace() && !matches!(character, '│' | '╭' | '╮' | '╰' | '╯' | '─')
        })
        .collect();
    if !flat.contains("Confirmfoldertrust") || !flat.contains("Doyoutrustthefilesinthisfolder?") {
        return None;
    }
    let workspace = squash(&workspace.to_string_lossy());
    let shown = flat
        .split_once("Confirmfoldertrust")
        .and_then(|(_, after)| after.split_once("Copilotcanread"))
        .is_some_and(|(path, _)| path.trim_end_matches('/') == workspace.trim_end_matches('/'));
    Some(trust_choice(screen, shown, "copilot-trust", |text| {
        text == "Yes"
    }))
}

/// Cursor Agent's "Workspace Trust Required" box. It has no menu: it names the
/// key, `[a] Trust this workspace`, and `[q] Quit`. Accepted only for the task's
/// workspace; the path is wrapped inside the box, so it is compared with the
/// borders removed and must be exactly the workspace. Any other directory is a
/// screen no rule answers, never a `Clear` one.
fn cursor_trust(screen: &str, workspace: &Path) -> Option<Screen> {
    let flat: String = screen
        .chars()
        .filter(|character| {
            !character.is_whitespace() && !matches!(character, '│' | '╭' | '╮' | '╰' | '╯' | '─')
        })
        .collect();
    if !flat.contains("WorkspaceTrustRequired") || !flat.contains("[a]Trustthisworkspace") {
        return None;
    }
    // Once answered, the box stays drawn above the agent's own screen with this
    // line in place of the key hint; pressing again would type into its input.
    if flat.contains("Trustingworkspace") {
        return None;
    }
    let workspace = squash(&workspace.to_string_lossy());
    let shown = flat
        .split_once("Doyoutrustthecontentsofthisdirectory?")
        .and_then(|(_, after)| after.split_once("[a]Trustthisworkspace"))
        .is_some_and(|(path, _)| {
            // A wide pane draws a selection marker before `[a]`, after the path.
            let path = path.trim_end_matches(['▶', '▸', '❯', '›', '>']);
            path.trim_end_matches('/') == workspace.trim_end_matches('/')
        });
    Some(if shown {
        Screen::Resolve {
            rule: "cursor-trust",
            keys: vec!["a"],
        }
    } else {
        Screen::Unknown(head(screen))
    })
}

/// Answers a harness's folder-trust menu: the option `accept` names when the
/// prompt is for the task's own folder (`shown`), otherwise the screen is one
/// no rule answers.
fn trust_choice(
    screen: &str,
    shown: bool,
    rule: &'static str,
    accept: impl Fn(&str) -> bool,
) -> Screen {
    let options = menu_options(screen);
    let target = options.iter().position(|option| accept(&option.text));
    let (true, Some(target)) = (shown, target) else {
        return Screen::Unknown(head(screen));
    };
    let selected = options
        .iter()
        .position(|option| option.selected)
        .unwrap_or(target);
    let mut keys = keys_between(selected, target);
    keys.push("enter");
    Screen::Resolve { rule, keys }
}

/// Presses what the rule table says for this pane, unless the agent is working.
///
/// A working agent's own output can contain `› 1.`-style lines, so nothing is
/// pressed while Herdr reports `working`.
pub(super) fn resolve(
    herdr: &Herdr,
    pane: &str,
    run: &PaneRunArgs,
    paths: &Paths,
    status: Option<&str>,
) -> Result<Screen> {
    let workspace = run.workspace.as_path();
    if status == Some("working") {
        return Ok(Screen::Clear);
    }
    let Some(screen) = herdr.screen(pane) else {
        return Ok(Screen::Clear);
    };
    let outcome = classify(&screen, workspace);
    if let Screen::Resolve { rule, keys } = &outcome {
        let mut call = vec!["pane", "send-keys", pane];
        call.extend(keys.iter().copied());
        herdr.call(&call).map_err(|failure| {
            anyhow::anyhow!("brgr could not answer the {rule} screen: {failure}")
        })?;
        eprintln!("brgr pane mode · {rule}: pressed {}", keys.join(" "));
        record(&log_path(paths, run), pane, rule, keys);
        std::thread::sleep(REDRAW);
    }
    Ok(outcome)
}

/// The per-attempt record of every key brgr pressed on its own.
pub(super) fn log_path(paths: &Paths, run: &PaneRunArgs) -> PathBuf {
    paths
        .runs
        .join(format!("{}-r{}.screens.log", run.task, run.revision))
}

fn record(path: &Path, pane: &str, rule: &str, keys: &[&str]) {
    let at = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |elapsed| elapsed.as_secs());
    let written = OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .and_then(|mut file| writeln!(file, "{at} {pane} {rule} {}", keys.join(" ")));
    if let Err(error) = written {
        eprintln!("brgr pane mode · could not log the {rule} key press: {error}");
    }
}

/// Bounds how long a screen no rule covers may stand.
pub(super) struct ScreenWatch {
    kind: String,
    limit: Duration,
    since: Option<Instant>,
    presses: Vec<(&'static str, u32)>,
}

/// How many times one rule may answer the same standing screen before the run
/// fails: a screen a key does not clear would otherwise be pressed forever.
const PRESS_BUDGET: u32 = 5;

impl ScreenWatch {
    pub(super) fn new(kind: &str) -> Self {
        Self::with_limit(kind, screen_deadline())
    }

    pub(super) fn with_limit(kind: &str, limit: Duration) -> Self {
        Self {
            kind: kind.to_owned(),
            limit,
            since: None,
            presses: Vec::new(),
        }
    }

    /// Records a key press for `rule`. Fails when it keeps being needed.
    pub(super) fn pressed(&mut self, rule: &'static str) -> Result<()> {
        let count =
            if let Some((_, count)) = self.presses.iter_mut().find(|(name, _)| *name == rule) {
                *count += 1;
                *count
            } else {
                self.presses.push((rule, 1));
                1
            };
        if count > PRESS_BUDGET {
            bail!(
                "the {} agent kept showing a screen brgr answers ({rule}); {PRESS_BUDGET} presses did not clear it",
                self.kind
            );
        }
        Ok(())
    }

    /// Records a poll. Fails once an unknown screen has stood past the limit.
    pub(super) fn observe(&mut self, screen: &Screen) -> Result<()> {
        match screen {
            Screen::Unknown(text) => self.stuck(text),
            Screen::Clear => {
                self.since = None;
                self.presses.clear();
                Ok(())
            }
            Screen::Resolve { .. } => {
                self.since = None;
                Ok(())
            }
        }
    }

    /// Records a poll where Herdr reports `blocked` but no rule answered.
    pub(super) fn observe_blocked(&mut self, screen: Option<&str>) -> Result<()> {
        self.stuck(&screen.map(head).unwrap_or_default())
    }

    pub(super) fn reset(&mut self) {
        self.since = None;
        self.presses.clear();
    }

    /// Stops the unknown-screen clock but keeps the press counts, for an
    /// agent whose screen moved without Herdr saying it works.
    pub(super) fn restart_clock(&mut self) {
        self.since = None;
    }

    fn stuck(&mut self, text: &str) -> Result<()> {
        let since = *self.since.get_or_insert_with(Instant::now);
        if since.elapsed() >= self.limit {
            bail!(
                "the {} agent stayed on a screen no brgr rule answers for {}s: {text}",
                self.kind,
                self.limit.as_secs()
            );
        }
        Ok(())
    }
}

struct MenuOption {
    selected: bool,
    text: String,
}

/// The numbered options on screen, in order. `selected` marks the one a
/// cursor glyph points at.
fn menu_options(screen: &str) -> Vec<MenuOption> {
    screen
        .lines()
        .filter_map(|line| {
            let line = line.trim().trim_matches(['│', '┃', '║']).trim();
            let (selected, rest) = ["›", "❯", ">", "▸"]
                .iter()
                .find_map(|cursor| line.strip_prefix(cursor))
                .map_or((false, line), |rest| (true, rest.trim_start()));
            let digits = rest.chars().take_while(char::is_ascii_digit).count();
            let after = rest.get(digits..)?;
            (digits > 0 && (after.starts_with('.') || after.starts_with(')'))).then(|| MenuOption {
                selected,
                text: after[1..].trim().to_owned(),
            })
        })
        .collect()
}

fn is_skip(option: &str) -> bool {
    let option = option.to_lowercase();
    !option.contains("update now")
        && !option.contains("install")
        && [
            "skip",
            "not now",
            "later",
            "no thanks",
            "dismiss",
            "remind me",
        ]
        .iter()
        .any(|word| option.contains(word))
}

/// Marks the quoted screen in an error; nothing after it leaves the machine.
pub(crate) const LAST_SCREEN: &str = "last screen:";

/// The last rows of the screen, for an error message.
pub(super) fn tail(screen: &str) -> String {
    head(screen)
}

/// The last rows of the screen. A dialog sits at the bottom, below whatever
/// scrolled past (the shell line that launched the agent, for one).
fn head(screen: &str) -> String {
    let rows: Vec<&str> = screen
        .lines()
        .map(str::trim)
        .filter(|row| !row.is_empty())
        .collect();
    rows[rows.len().saturating_sub(8)..].join(" | ")
}

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

    /// The screen of the `OpenCode` pane that stalled task `38d33b4e` (D2 in the
    /// dogfood log), kept verbatim apart from trailing blank rows.
    const OPENCODE_UPDATED: &str = "\
                                                                                 ┃                               ┃
                                                                                 ┃  MCP Authentication Required  ┃
                                                                                 ┃                               ┃
                                                                                 ┃  Updating to v2.0.15…         ┃
                                                                                 ┃                               ┃

                              Update Complete                                      esc

                              Successfully updated to OpenCode v2.0.15. Please
                              restart the application.


                                                                                 ok

                     ┃
                     ┃  Ask anything… \"Fix broken tests\"
                     ┃
                     ┃  Build auto · Big Pickle OpenCode Zen
                     ╹▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
                                                                     tab agents  ctrl+p commands
";

    fn work() -> &'static Path {
        Path::new("/repo/task")
    }

    #[test]
    fn an_applied_self_update_is_dismissed_with_esc_not_enter() {
        assert_eq!(
            classify(OPENCODE_UPDATED, work()),
            Screen::Resolve {
                rule: "update-applied",
                keys: vec!["esc"],
            }
        );
    }

    #[test]
    fn an_mcp_auth_toast_alone_does_not_block_the_run() {
        let toast = OPENCODE_UPDATED
            .lines()
            .filter(|line| !line.contains("Update Complete") && !line.contains("restart"))
            .collect::<Vec<_>>()
            .join("\n");
        assert_eq!(classify(&toast, work()), Screen::Clear);
    }

    #[test]
    fn the_claude_trust_prompt_for_the_task_workspace_is_accepted() {
        let screen =
            "Accessing workspace:\n\n/repo/task\n\n❯ No, exit\n  Yes, I trust this folder\n";
        assert_eq!(
            classify(screen, work()),
            Screen::Resolve {
                rule: "workspace-trust",
                keys: vec!["down", "enter"],
            }
        );
        let selected =
            "Accessing workspace:\n\n/repo/task\n\n  No, exit\n❯ Yes, I trust this folder\n";
        assert_eq!(
            classify(selected, work()),
            Screen::Resolve {
                rule: "workspace-trust",
                keys: vec!["enter"],
            }
        );
    }

    /// Verbatim from a real Codex 0.160 pane (dogfood D13), path wrapped as shown.
    const CODEX_TRUST: &str = "\
  Folder access
  /private/tmp/scratchpad/cod
  ex owner repo
  Trust this folder? Codex can read, edit, and run files here, subject to your permission settings.
  Folder settings can run code automatically, even without a model request. Continue only if you
  trust these files. Your trust decision will be saved.
› 1. Trust and continue
  2. Back to Agent Command Center
  enter continue · esc back
";

    #[test]
    fn the_codex_trust_prompt_for_the_task_workspace_is_accepted() {
        assert_eq!(
            classify(
                CODEX_TRUST,
                Path::new("/private/tmp/scratchpad/codex owner repo")
            ),
            Screen::Resolve {
                rule: "codex-trust",
                keys: vec!["enter"],
            }
        );
        // The cursor on the other option still ends on "Trust and continue".
        let moved = CODEX_TRUST
            .replace("› 1. Trust and continue", "  1. Trust and continue")
            .replace("  2. Back", "› 2. Back");
        assert_eq!(
            classify(
                &moved,
                Path::new("/private/tmp/scratchpad/codex owner repo")
            ),
            Screen::Resolve {
                rule: "codex-trust",
                keys: vec!["up", "enter"],
            }
        );
    }

    #[test]
    fn the_codex_trust_prompt_for_another_folder_is_not_accepted() {
        assert!(matches!(
            classify(CODEX_TRUST, Path::new("/elsewhere")),
            Screen::Unknown(_)
        ));
    }

    #[test]
    fn a_trust_prompt_for_another_folder_is_not_accepted() {
        let screen =
            "Accessing workspace:\n\n/elsewhere\n\n❯ No, exit\n  Yes, I trust this folder\n";
        assert!(matches!(classify(screen, work()), Screen::Unknown(_)));
    }

    /// Synthetic: modelled on the Codex update offer described in v2.9.3.
    #[test]
    fn an_update_offer_is_skipped_by_moving_to_the_skip_option() {
        let screen = "\
 Update available! 0.1.0 -> 0.2.0

 › 1. Update now
   2. Skip
   3. Skip until next version
";
        assert_eq!(
            classify(screen, work()),
            Screen::Resolve {
                rule: "update-offer-skip",
                keys: vec!["down", "enter"],
            }
        );
    }

    /// Synthetic.
    #[test]
    fn an_update_offer_with_the_cursor_on_skip_only_presses_enter() {
        let screen = "Update available\n  1. Update now\n❯ 2. Not now\n";
        assert_eq!(
            classify(screen, work()),
            Screen::Resolve {
                rule: "update-offer-skip",
                keys: vec!["enter"],
            }
        );
    }

    /// Captured from GitHub Copilot CLI 1.0.86 in a Herdr pane.
    const COPILOT_TRUST: &str =
        include_str!("../../../../testdata/fixtures/screens/copilot-1.0.86-trust.txt");
    const COPILOT_WORKSPACE: &str = "/private/tmp/claude-501/-Users-justn-dev-brgr/b54b7525-6101-4186-a2b9-0e32355d043a/scratchpad/cap-copilot-1791247661";

    /// Captured from Codex CLI 0.160 started in a git worktree.
    const CODEX_WORKTREE_TRUST: &str =
        include_str!("../../../../testdata/fixtures/screens/codex-0.160-worktree-trust.txt");
    const CODEX_WORKTREE: &str = "/private/tmp/claude-501/-Users-justn-dev-brgr/b54b7525-6101-4186-a2b9-0e32355d043a/scratchpad/cap-codex-wt";
    const CODEX_REPOSITORY: &str = "/private/tmp/claude-501/-Users-justn-dev-brgr/b54b7525-6101-4186-a2b9-0e32355d043a/scratchpad/cap-codex-repo";

    #[test]
    fn codex_trust_in_a_task_worktree_names_and_accepts_its_repository() {
        assert_eq!(
            codex_trust(CODEX_WORKTREE_TRUST, Path::new(CODEX_WORKTREE), || {
                Some(PathBuf::from(CODEX_REPOSITORY))
            }),
            Some(Screen::Resolve {
                rule: "codex-trust",
                keys: vec!["enter"],
            })
        );
    }

    #[test]
    fn codex_trust_for_a_root_that_is_not_the_worktrees_repository_is_refused() {
        for primary in [None, Some("/private/tmp/other-repo"), Some(CODEX_WORKTREE)] {
            assert!(
                matches!(
                    codex_trust(CODEX_WORKTREE_TRUST, Path::new(CODEX_WORKTREE), || {
                        primary.map(PathBuf::from)
                    }),
                    Some(Screen::Unknown(_))
                ),
                "{primary:?}"
            );
        }
    }

    /// Captured from Cursor Agent 2026.10.22 in a wide Herdr pane while it waits
    /// for an answer; Cursor draws a `▶` before the selected `[a]` option.
    const CURSOR_TRUST: &str =
        include_str!("../../../../testdata/fixtures/screens/cursor-2026.10.22-trust-wide.txt");
    const CURSOR_WORKSPACE: &str = "/private/tmp/claude-501/-Users-justn-dev-brgr/2c6e674e-b811-4b53-8577-a4d60a8cc2ab/scratchpad/cur3-work";
    /// Captured from the same agent in a narrow pane after the prompt was
    /// answered: the box is still on screen and says it is trusting.
    const CURSOR_TRUST_ANSWERED: &str =
        include_str!("../../../../testdata/fixtures/screens/cursor-2026.10.22-trust.txt");
    const CURSOR_ANSWERED_WORKSPACE: &str = "/private/tmp/claude-501/-Users-justn-dev-brgr/2c6e674e-b811-4b53-8577-a4d60a8cc2ab/scratchpad/cursor-work";

    #[test]
    fn cursors_trust_prompt_for_the_task_workspace_is_accepted_with_its_key() {
        assert_eq!(
            classify(CURSOR_TRUST, Path::new(CURSOR_WORKSPACE)),
            Screen::Resolve {
                rule: "cursor-trust",
                keys: vec!["a"],
            }
        );
    }

    #[test]
    fn cursors_trust_prompt_for_another_folder_is_not_accepted() {
        for other in [
            "/private/tmp/claude-501/-Users-justn-dev-brgr/2c6e674e-b811-4b53-8577-a4d60a8cc2ab/scratchpad/cur3-wor",
            "/private/tmp/claude-501",
            "/somewhere/else",
        ] {
            assert!(
                matches!(classify(CURSOR_TRUST, Path::new(other)), Screen::Unknown(_)),
                "{other}"
            );
        }
    }

    #[test]
    fn cursors_answered_trust_box_still_on_screen_is_not_answered_again() {
        assert_eq!(
            classify(CURSOR_TRUST_ANSWERED, Path::new(CURSOR_ANSWERED_WORKSPACE)),
            Screen::Clear
        );
    }

    #[test]
    fn copilots_trust_prompt_for_the_task_workspace_is_accepted_once() {
        assert_eq!(
            classify(COPILOT_TRUST, Path::new(COPILOT_WORKSPACE)),
            Screen::Resolve {
                rule: "copilot-trust",
                keys: vec!["enter"],
            }
        );
    }

    #[test]
    fn copilots_trust_prompt_for_another_folder_is_not_accepted() {
        for other in [
            "/private/tmp/claude-501/-Users-justn-dev-brgr/b54b7525-6101-4186-a2b9-0e32355d043a/scratchpad/cap-copilot-179124766",
            "/private/tmp/claude-501",
            "/somewhere/else",
        ] {
            assert!(
                matches!(
                    classify(COPILOT_TRUST, Path::new(other)),
                    Screen::Unknown(_)
                ),
                "{other}"
            );
        }
    }

    #[test]
    fn copilots_idle_screen_needs_no_answer() {
        let idle = include_str!("../../../../testdata/fixtures/screens/copilot-1.0.86-idle.txt");
        assert_eq!(classify(idle, Path::new(COPILOT_WORKSPACE)), Screen::Clear);
    }

    /// Synthetic.
    #[test]
    fn an_agent_question_about_updating_something_is_not_an_update_offer() {
        for screen in [
            "Update the schema now?\n› 1. Yes\n  2. Later\n",
            "Upgrade the dependencies too?\n› 1. Yes\n  2. Skip\n",
            "> 1. update the config, then skip later steps\n  2. next\n",
        ] {
            assert!(
                matches!(classify(screen, work()), Screen::Unknown(_)),
                "{screen}"
            );
        }
    }

    #[test]
    fn an_update_offer_is_known_by_its_wording_or_version() {
        assert!(is_update_offer("a new version of codex is ready, update?"));
        assert!(is_update_offer("upgrade to v2.0.15"));
        assert!(!is_update_offer("update the 2.0 schema"));
        assert!(!is_update_offer("update readme.md and 1.2 notes"));
    }

    #[test]
    fn an_update_menu_without_a_skip_option_is_not_guessed() {
        let screen = "Update available\n› 1. Update now\n  2. Show release notes\n";
        assert!(matches!(classify(screen, work()), Screen::Unknown(_)));
    }

    /// Synthetic: Claude Code's documented Bypass Permissions warning, not
    /// captured from a pane here.
    #[test]
    fn the_bypass_permissions_warning_is_accepted() {
        let screen = "WARNING: Claude Code running in Bypass Permissions mode\n\nIn Bypass Permissions mode, Claude Code will not ask for your approval.\n\n❯ 1. No, exit\n  2. Yes, I accept\n";
        assert_eq!(
            classify(screen, work()),
            Screen::Resolve {
                rule: "bypass-warning",
                keys: vec!["down", "enter"],
            }
        );
    }

    /// Synthetic: Claude Code's documented new-MCP-server prompt.
    #[test]
    fn a_new_mcp_server_prompt_is_trusted() {
        let screen = "New MCP server found in .mcp.json: docs\n\n❯ 1. Use this and all future MCP servers in this project\n  2. Use this MCP server\n  3. Continue without using this MCP server\n";
        assert_eq!(
            classify(screen, work()),
            Screen::Resolve {
                rule: "mcp-trust",
                keys: vec!["down", "enter"],
            }
        );
    }

    #[test]
    fn a_numbered_claude_trust_menu_is_answered_by_moving_to_yes() {
        let screen = "Accessing workspace:\n\n/repo/task\n\nQuick safety check\n\n❯ 1. Yes, I trust this folder\n  2. No, exit\n";
        assert_eq!(
            classify(screen, work()),
            Screen::Resolve {
                rule: "workspace-trust",
                keys: vec!["enter"],
            }
        );
        let other = screen
            .replace("❯ 1. Yes", "  1. Yes")
            .replace("  2. No, exit", "❯ 2. No, exit");
        assert_eq!(
            classify(&other, work()),
            Screen::Resolve {
                rule: "workspace-trust",
                keys: vec!["up", "enter"],
            }
        );
    }

    #[test]
    fn the_codex_trust_prompt_for_a_longer_path_is_not_accepted() {
        for longer in [
            "/private/tmp/scratchpad/codex owner repo-evil",
            "/private/tmp/scratchpad/codex owner repo/sub",
        ] {
            let screen =
                CODEX_TRUST.replace("/private/tmp/scratchpad/cod\n  ex owner repo", longer);
            assert!(
                matches!(
                    classify(
                        &screen,
                        Path::new("/private/tmp/scratchpad/codex owner repo")
                    ),
                    Screen::Unknown(_)
                ),
                "{longer}"
            );
        }
    }

    #[test]
    fn agent_prose_about_an_update_is_not_the_applied_update_dialog() {
        let prose = "The build finished. Update complete, please restart the dev server.\n";
        assert_eq!(classify(prose, work()), Screen::Clear);
    }

    #[test]
    fn a_screen_that_a_key_does_not_clear_fails_the_run_after_the_budget() {
        let mut watch = ScreenWatch::new("claude");
        for _ in 0..PRESS_BUDGET {
            watch.pressed("continue-notice").unwrap();
        }
        let error = watch.pressed("continue-notice").unwrap_err().to_string();
        assert!(
            error.contains("continue-notice") && error.contains("did not clear"),
            "{error}"
        );
        // A clear screen is progress: the count starts over.
        watch.observe(&Screen::Clear).unwrap();
        watch.pressed("continue-notice").unwrap();
    }

    #[test]
    fn a_new_mcp_server_prompt_prefers_the_single_server_choice() {
        let screen = "New MCP server found in .mcp.json: docs\n\n❯ 1. Use this and all future MCP servers in this project\n  2. Use this MCP server\n  3. Continue without using this MCP server\n";
        assert_eq!(
            classify(screen, work()),
            Screen::Resolve {
                rule: "mcp-trust",
                keys: vec!["down", "enter"],
            }
        );
    }

    /// Synthetic: a stray notice line must not turn an update menu into Enter.
    #[test]
    fn an_update_menu_with_a_continue_line_is_skipped_not_confirmed() {
        let screen = "Update available\n› 1. Update now\n  2. Skip\nPress Enter to continue\n";
        assert_eq!(
            classify(screen, work()),
            Screen::Resolve {
                rule: "update-offer-skip",
                keys: vec!["down", "enter"],
            }
        );
    }

    /// Synthetic.
    #[test]
    fn a_continue_notice_is_dismissed_with_enter() {
        assert_eq!(
            classify(
                "Welcome to the new release\n\n  Press Enter to continue\n",
                work()
            ),
            Screen::Resolve {
                rule: "continue-notice",
                keys: vec!["enter"],
            }
        );
    }

    /// Synthetic.
    #[test]
    fn a_menu_no_rule_covers_is_unknown_and_never_pressed() {
        let screen = "Choose a theme\n› 1. Dark\n  2. Light\n";
        assert_eq!(
            classify(screen, work()),
            Screen::Unknown("Choose a theme | › 1. Dark | 2. Light".to_owned())
        );
    }

    #[test]
    fn an_ordinary_agent_screen_is_clear() {
        assert_eq!(classify("some output\n❯ \n", work()), Screen::Clear);
    }

    fn fake_herdr(screen: &str) -> (tempfile::TempDir, Herdr) {
        use std::os::unix::fs::PermissionsExt as _;
        let temp = tempfile::tempdir().unwrap();
        let binary = temp.path().join("herdr");
        std::fs::write(temp.path().join("screen"), screen).unwrap();
        std::fs::write(
            &binary,
            r#"#!/bin/sh
d=$(/usr/bin/dirname "$0")
case "$1 $2" in
 'pane read') /bin/cat "$d/screen";;
 'pane send-keys') echo "$@" >> "$d/keys";;
 *) exit 8;;
esac
"#,
        )
        .unwrap();
        std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o700)).unwrap();
        let herdr = Herdr {
            binary: binary.into_os_string(),
            session: None,
        };
        (temp, herdr)
    }

    fn run_args(temp: &tempfile::TempDir) -> (Paths, PaneRunArgs) {
        let paths = Paths::new(Some(temp.path().join("home"))).unwrap();
        let run = PaneRunArgs {
            caller: None,
            native_executable: None,
            opencode_model: None,
            prompt_file: PathBuf::new(),
            workspace: PathBuf::from("/repo/task"),
            task: brgr_protocol::TaskId::new(),
            revision: 1,
            kind: "opencode".to_owned(),
            agent_args: Vec::new(),
            keep_pane: false,
        };
        (paths, run)
    }

    #[test]
    fn resolve_presses_the_rule_keys_on_the_pane() {
        let (temp, herdr) = fake_herdr(OPENCODE_UPDATED);
        let (paths, run) = run_args(&temp);
        let outcome = resolve(&herdr, "w1:p2", &run, &paths, Some("idle")).unwrap();
        assert!(matches!(outcome, Screen::Resolve { .. }));
        let keys = std::fs::read_to_string(temp.path().join("keys")).unwrap();
        assert_eq!(keys.trim(), "pane send-keys w1:p2 esc");
        let log = std::fs::read_to_string(log_path(&paths, &run)).unwrap();
        assert!(
            log.trim_end().ends_with("w1:p2 update-applied esc"),
            "{log}"
        );
    }

    #[test]
    fn resolve_presses_nothing_while_the_agent_is_working() {
        let screen = "agent output\n› 1. Update now\n  2. Skip\n";
        let (temp, herdr) = fake_herdr(screen);
        let (paths, run) = run_args(&temp);
        assert_eq!(
            resolve(&herdr, "w1:p2", &run, &paths, Some("working")).unwrap(),
            Screen::Clear
        );
        assert!(!temp.path().join("keys").exists());
        assert!(!log_path(&paths, &run).exists());
    }

    #[test]
    fn an_unknown_screen_fails_the_run_with_its_text_after_the_deadline() {
        let mut watch = ScreenWatch::with_limit("opencode", Duration::ZERO);
        let error = watch
            .observe(&Screen::Unknown("Choose a theme".to_owned()))
            .unwrap_err()
            .to_string();
        assert!(
            error.contains("opencode") && error.contains("Choose a theme"),
            "{error}"
        );
    }

    #[test]
    fn a_resolved_screen_resets_the_deadline() {
        let mut watch = ScreenWatch::with_limit("claude", Duration::from_hours(1));
        watch.observe(&Screen::Unknown("x".to_owned())).unwrap();
        watch.observe(&Screen::Clear).unwrap();
        assert!(watch.since.is_none());
    }
}