cyberterm 0.4.0

A GPU terminal for developers: splits and sessions, command blocks, searchable history, AI help, inline images and Lua scripting
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
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
// src/agent.rs
//
// Agent sessions (`cyberterm +agent`): any coding agent -- Claude Code,
// Codex, Gemini CLI, opencode, Copilot, ... or anything listed under
// [agents.launch] -- started in a git worktree of its own, in a tab of
// its own.
//
// A session is a record in $XDG_STATE_HOME/cyberterm/agents/<id>.json:
// which agent, its command, the task, the worktree, its branch and the
// commit it started from. The tab runs `cyberterm +agent run <id>`, which
// sets CYBERTERM_AGENT_ID and replaces itself with the agent. The window
// recognises agent panes by that variable in the foreground process's
// environment (/proc), so it works the same for daemon panes and after
// reattaching, and nothing here depends on which agent it is.

use crate::config::{AgentLaunch, AgentsConfig};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::process::Command;

/// How an agent takes the task it starts with.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Prompt {
    /// As the last argument: `claude "fix the test"`.
    Positional,
    /// After a flag: `gemini -i "fix the test"`.
    Flag(String),
    /// It doesn't: the task is shown in the pane before the agent starts.
    None,
}

impl Prompt {
    /// From a config value: "positional", "none", or the flag itself.
    fn parse(s: &str) -> Prompt {
        match s.trim() {
            "" | "none" => Prompt::None,
            "positional" => Prompt::Positional,
            flag => Prompt::Flag(flag.to_string()),
        }
    }
}

/// Agents Cyberterm knows how to start: (command, name shown, how it takes
/// a starting task). Checked against each one's `--help`.
const KNOWN: &[(&str, &str, &str)] = &[
    ("claude", "Claude Code", "positional"),
    ("codex", "Codex", "positional"),
    ("copilot", "GitHub Copilot", "-i"),
    ("gemini", "Gemini CLI", "-i"),
    ("cursor-agent", "Cursor Agent", "positional"),
    ("opencode", "opencode", "--prompt"),
    ("crush", "Crush", "none"),
    ("grok", "Grok", "positional"),
    ("pi", "pi", "positional"),
    ("omp", "oh-my-pi", "positional"),
    ("agy", "Antigravity", "-i"),
    ("muse", "Muse Code", "positional"),
    ("hermes", "Hermes Agent", "none"),
    ("ori", "Ori", "none"),
    ("aider", "Aider", "none"),
    ("goose", "Goose", "none"),
    ("amp", "Amp", "none"),
    ("qwen", "Qwen Code", "none"),
    ("kiro-cli", "Kiro", "none"),
    ("droid", "Droid", "none"),
];

/// An agent that can be started.
#[derive(Clone, Debug, PartialEq)]
pub struct Launcher {
    /// What `+agent <name>` takes.
    pub name: String,
    pub label: String,
    pub argv: Vec<String>,
    pub prompt: Prompt,
}

/// Native reporting an agent supports, beyond the shell recorder
/// (src/flight_log.rs). Chosen by the program it runs, so a custom
/// `[agents.launch]` entry for `claude --model opus` gets it too.
pub fn adapter(argv: &[String]) -> Option<&'static str> {
    let program = Path::new(argv.first()?).file_name()?.to_str()?;
    match program {
        "claude" => Some("claude"),
        "codex" => Some("codex"),
        "gemini" => Some("gemini"),
        "copilot" => Some("copilot"),
        "cursor-agent" => Some("cursor"),
        "hermes" => Some("hermes"),
        _ => None,
    }
}

/// The agents that can be started here: those in [agents.launch], then
/// the known ones found on PATH. A config entry with a known name
/// overrides it.
pub fn launchers(cfg: &AgentsConfig) -> Vec<Launcher> {
    let mut out: Vec<Launcher> = Vec::new();
    for (name, l) in &cfg.launch {
        if let Some(launcher) = from_config(name, l) {
            out.push(launcher);
        }
    }
    for (bin, label, prompt) in KNOWN {
        if out.iter().any(|l| l.name == *bin) || !on_path(bin) {
            continue;
        }
        out.push(Launcher {
            name: bin.to_string(),
            label: label.to_string(),
            argv: vec![bin.to_string()],
            prompt: Prompt::parse(prompt),
        });
    }
    out.sort_by_key(|l| l.label.to_lowercase());
    out
}

fn from_config(name: &str, l: &AgentLaunch) -> Option<Launcher> {
    let known = KNOWN.iter().find(|(bin, _, _)| *bin == name);
    let argv = if l.command.trim().is_empty() {
        vec![known?.0.to_string()]
    } else {
        split_command(&l.command).ok()?
    };
    if argv.is_empty() {
        return None;
    }
    let label = match (l.label.trim(), known) {
        ("", Some((_, label, _))) => label.to_string(),
        ("", None) => name.to_string(),
        (label, _) => label.to_string(),
    };
    let prompt = match (l.prompt.trim(), known) {
        ("", Some((_, _, prompt))) => Prompt::parse(prompt),
        (prompt, _) => Prompt::parse(prompt),
    };
    Some(Launcher {
        name: name.to_string(),
        label,
        argv,
        prompt,
    })
}

/// Finds a launcher by name or shown name, ignoring case.
pub fn find<'a>(launchers: &'a [Launcher], name: &str) -> Option<&'a Launcher> {
    let name = name.to_lowercase();
    launchers
        .iter()
        .find(|l| l.name.to_lowercase() == name || l.label.to_lowercase() == name)
}

fn on_path(bin: &str) -> bool {
    use std::os::unix::fs::PermissionsExt;
    let Some(path) = std::env::var_os("PATH") else {
        return false;
    };
    std::env::split_paths(&path).any(|dir| {
        std::fs::metadata(dir.join(bin))
            .is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
    })
}

/// Splits a command line the way a shell would for plain words, quotes
/// and backslashes (no expansions).
pub fn split_command(s: &str) -> Result<Vec<String>, String> {
    let mut out = Vec::new();
    let mut word = String::new();
    let mut in_word = false;
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        match c {
            '\'' => {
                in_word = true;
                loop {
                    match chars.next() {
                        Some('\'') => break,
                        Some(c) => word.push(c),
                        None => return Err(format!("unclosed ' in {s:?}")),
                    }
                }
            }
            '"' => {
                in_word = true;
                loop {
                    match chars.next() {
                        Some('"') => break,
                        Some('\\') => match chars.next() {
                            Some(c @ ('"' | '\\' | '$' | '`')) => word.push(c),
                            Some(c) => {
                                word.push('\\');
                                word.push(c);
                            }
                            None => return Err(format!("unclosed \" in {s:?}")),
                        },
                        Some(c) => word.push(c),
                        None => return Err(format!("unclosed \" in {s:?}")),
                    }
                }
            }
            '\\' => {
                in_word = true;
                if let Some(c) = chars.next() {
                    word.push(c);
                }
            }
            c if c.is_whitespace() => {
                if in_word {
                    out.push(std::mem::take(&mut word));
                    in_word = false;
                }
            }
            c => {
                in_word = true;
                word.push(c);
            }
        }
    }
    if in_word {
        out.push(word);
    }
    Ok(out)
}

/// Quotes a word for typing at a shell prompt (bash, zsh and fish alike:
/// plain words stay as they are, anything else is double-quoted).
pub fn shell_quote(s: &str) -> String {
    let plain = !s.is_empty()
        && s.chars()
            .all(|c| c.is_ascii_alphanumeric() || "-_./:+=@%,".contains(c));
    if plain {
        return s.to_string();
    }
    let mut out = String::from("\"");
    for c in s.chars() {
        if matches!(c, '"' | '\\' | '$' | '`') {
            out.push('\\');
        }
        out.push(c);
    }
    out.push('"');
    out
}

// ----------------------------------------------------------------------
// Sessions
// ----------------------------------------------------------------------

/// One agent session.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Session {
    pub id: String,
    /// The launcher's name and shown name.
    pub agent: String,
    pub label: String,
    pub argv: Vec<String>,
    pub prompt: Prompt,
    pub task: Option<String>,
    /// Where the agent runs (inside the worktree, if there is one).
    pub dir: PathBuf,
    /// The worktree, its branch and the commit it started from; `None`
    /// when the agent runs in place.
    pub worktree: Option<Worktree>,
    pub created_ms: u64,
    /// Sessions started together on one task (`+agent claude,codex
    /// "task"`) share a group: the attempts to compare, keep one of and
    /// discard the rest.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub group: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Worktree {
    pub path: PathBuf,
    pub branch: String,
    pub base: String,
    /// The repository it was made from.
    pub repo: PathBuf,
    /// The branch checked out there when it started: where `+agent merge`
    /// takes its work back to (older sessions: the repository's branch).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub onto: Option<String>,
}

impl Session {
    /// The agent's command line with `extra` options, and the task added
    /// the way it takes one.
    fn command_with(&self, extra: &[String]) -> Vec<String> {
        let mut argv = self.argv.clone();
        argv.extend(extra.iter().cloned());
        if let Some(task) = self.task.as_ref().filter(|t| !t.trim().is_empty()) {
            match &self.prompt {
                Prompt::Positional => argv.push(task.clone()),
                Prompt::Flag(flag) => {
                    argv.push(flag.clone());
                    argv.push(task.clone());
                }
                Prompt::None => {}
            }
        }
        argv
    }

    /// The tab's title: "Claude Code · fix-flaky-test".
    pub fn title(&self) -> String {
        format!("{} · {}", self.label, self.id)
    }
}

/// Where session records live.
pub fn state_dir() -> PathBuf {
    std::env::var_os("XDG_STATE_HOME")
        .map(PathBuf::from)
        .filter(|p| p.is_absolute())
        .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/state")))
        .unwrap_or_else(std::env::temp_dir)
        .join("cyberterm")
        .join("agents")
}

fn record_path(dir: &Path, id: &str) -> PathBuf {
    dir.join(format!("{id}.json"))
}

pub fn load(id: &str) -> Result<Session, String> {
    load_in(&state_dir(), id)
}

pub(crate) fn load_in(dir: &Path, id: &str) -> Result<Session, String> {
    if !valid_id(id) {
        return Err(format!("no agent session {id:?}"));
    }
    let text = std::fs::read_to_string(record_path(dir, id))
        .map_err(|_| format!("no agent session {id:?} (see `cyberterm +agent list`)"))?;
    serde_json::from_str(&text).map_err(|e| format!("agent session {id}: {e}"))
}

/// Every session, newest first.
pub fn sessions() -> Vec<Session> {
    sessions_in(&state_dir())
}

fn sessions_in(dir: &Path) -> Vec<Session> {
    let mut out: Vec<Session> = std::fs::read_dir(dir)
        .into_iter()
        .flatten()
        .flatten()
        .filter(|e| e.path().extension().is_some_and(|x| x == "json"))
        .filter_map(|e| serde_json::from_str(&std::fs::read_to_string(e.path()).ok()?).ok())
        .collect();
    out.sort_by_key(|s: &Session| std::cmp::Reverse(s.created_ms));
    out
}

fn save_in(dir: &Path, s: &Session) -> Result<(), String> {
    std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
    let path = record_path(dir, &s.id);
    let text = serde_json::to_string_pretty(s).map_err(|e| e.to_string())?;
    std::fs::write(&path, text + "\n").map_err(|e| format!("{}: {e}", path.display()))
}

fn valid_id(id: &str) -> bool {
    !id.is_empty()
        && id.len() <= 64
        && id
            .chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}

/// A short name from the task's first words ("Fix the flaky login test!"
/// -> "fix-the-flaky-login"), or the agent's name without a task.
pub fn slug(task: Option<&str>, agent: &str) -> String {
    let from = |s: &str, words: usize| {
        s.split(|c: char| !c.is_ascii_alphanumeric())
            .filter(|w| !w.is_empty())
            .take(words)
            .map(str::to_ascii_lowercase)
            .collect::<Vec<_>>()
            .join("-")
    };
    let mut s = task.map(|t| from(t, 4)).unwrap_or_default();
    if s.is_empty() {
        s = from(agent, 3);
    }
    if s.is_empty() {
        s = "agent".into();
    }
    s.truncate(40);
    s.trim_end_matches('-').to_string()
}

/// What `create` needs to know.
pub struct Request<'a> {
    pub launcher: &'a Launcher,
    pub task: Option<String>,
    /// Where the agent was asked for (the current pane's directory).
    pub cwd: PathBuf,
    pub worktree: bool,
    /// The group of attempts it's one of, if any.
    pub group: Option<String>,
}

/// Creates a session: its worktree (when `worktree` is set and `cwd` is in
/// a git repository) and its record.
pub fn create(cfg: &AgentsConfig, req: Request<'_>) -> Result<Session, String> {
    create_in(&state_dir(), cfg, req)
}

pub(crate) fn create_in(
    state: &Path,
    cfg: &AgentsConfig,
    req: Request<'_>,
) -> Result<Session, String> {
    let task = req.task.filter(|t| !t.trim().is_empty());
    // One of several attempts: its agent in the name tells them apart.
    let base_slug = match &req.group {
        Some(_) => format!(
            "{}-{}",
            slug(task.as_deref(), &req.launcher.name),
            slug(None, &req.launcher.name)
        ),
        None => slug(task.as_deref(), &req.launcher.name),
    };
    let repo = if req.worktree {
        git_toplevel(&req.cwd)
    } else {
        None
    };
    let worktree_root = |top: &Path| -> PathBuf {
        let dir = cfg.worktree_dir.trim();
        if dir.is_empty() {
            top.parent().unwrap_or(top).to_path_buf()
        } else {
            expand_home(dir)
        }
    };
    let repo_name = |top: &Path| {
        top.file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| "repo".into())
    };

    // A name nothing else uses yet: record, branch and worktree folder.
    let mut id = base_slug.clone();
    for n in 2.. {
        let taken = record_path(state, &id).exists()
            || repo.as_ref().is_some_and(|top| {
                branch_exists(top, &format!("agent/{id}"))
                    || worktree_root(top)
                        .join(format!("{}-{id}", repo_name(top)))
                        .exists()
            });
        if !taken {
            break;
        }
        id = format!("{base_slug}-{n}");
    }

    let (dir, worktree) = match &repo {
        Some(top) => {
            let path = worktree_root(top).join(format!("{}-{id}", repo_name(top)));
            let branch = format!("agent/{id}");
            let base = git(top, &["rev-parse", "HEAD"])?;
            let onto = git(top, &["symbolic-ref", "--short", "-q", "HEAD"])
                .ok()
                .filter(|b| !b.is_empty());
            git(
                top,
                &[
                    "worktree",
                    "add",
                    "-b",
                    &branch,
                    &path.to_string_lossy(),
                    &base,
                ],
            )?;
            // Start where you were: repo/src -> worktree/src.
            let rel = req.cwd.strip_prefix(top).unwrap_or(Path::new(""));
            let dir = Some(path.join(rel))
                .filter(|d| d.is_dir())
                .unwrap_or_else(|| path.clone());
            (
                dir,
                Some(Worktree {
                    path,
                    branch,
                    base,
                    repo: top.clone(),
                    onto,
                }),
            )
        }
        None => (req.cwd.clone(), None),
    };

    let session = Session {
        id,
        agent: req.launcher.name.clone(),
        label: req.launcher.label.clone(),
        argv: req.launcher.argv.clone(),
        prompt: req.launcher.prompt.clone(),
        task,
        dir,
        worktree,
        created_ms: crate::shell::tap::now_ms(),
        group: req.group,
    };
    save_in(state, &session)?;
    Ok(session)
}

/// Runs a session's agent in this process (`+agent run <id>`): changes to
/// its directory, marks the environment, turns on the flight log, and
/// replaces itself with it.
pub fn exec(id: &str, cfg: &AgentsConfig) -> Result<std::convert::Infallible, String> {
    use std::os::unix::process::CommandExt;
    let s = load(id)?;
    if !s.dir.is_dir() {
        return Err(format!(
            "{} is gone (the worktree was removed?)",
            short(&s.dir)
        ));
    }
    if let (Prompt::None, Some(task)) = (&s.prompt, &s.task) {
        println!("\x1b[1mTask:\x1b[0m {task}\n");
    }
    let log = crate::flight_log::log_path(&s.id);
    let mut extra = Vec::new();
    if cfg.hooks {
        match adapter(&s.argv) {
            Some("claude") => {
                let hook = format!("{} +hook claude", self_path());
                extra.push("--settings".to_string());
                extra.push(crate::flight_log::claude_settings(&hook));
            }
            Some("codex") => {
                // Same command every time, so Codex's one-time review of
                // these hooks holds for later sessions.
                let hook = format!("{} +hook codex", self_path());
                for o in crate::flight_log::codex_overrides(&hook) {
                    extra.push("-c".to_string());
                    extra.push(o);
                }
            }
            Some("copilot") => match copilot_plugin_dir() {
                Ok(dir) => {
                    extra.push("--plugin-dir".to_string());
                    extra.push(dir.to_string_lossy().into_owned());
                }
                Err(e) => eprintln!("cyberterm: Copilot's hooks are off ({e})"),
            },
            // Gemini's, Cursor's and Hermes's come from `+agent setup`.
            _ => {}
        }
    }
    let argv = s.command_with(&extra);
    let mut cmd = Command::new(&argv[0]);
    cmd.args(&argv[1..])
        .current_dir(&s.dir)
        .env("CYBERTERM_AGENT_ID", &s.id)
        .env("CYBERTERM_AGENT", &s.agent);
    if cfg.record_commands {
        match crate::flight_log::recorder_env(&log) {
            Ok(env) => {
                cmd.envs(env);
            }
            Err(e) => eprintln!("cyberterm: flight log off ({e})"),
        }
    }
    let err = cmd.exec();
    Err(format!("couldn't start {}: {err}", argv[0]))
}

/// Writes the plugin that gives Copilot sessions their hooks (the same
/// for every session) and returns its directory.
fn copilot_plugin_dir() -> Result<PathBuf, String> {
    let dir = state_dir().join("copilot-plugin");
    std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?;
    let hook = format!("{} +hook copilot", self_path());
    for (name, body) in crate::flight_log::copilot_plugin(&hook) {
        let path = dir.join(name);
        if std::fs::read_to_string(&path).ok().as_deref() != Some(body.as_str()) {
            std::fs::write(&path, body).map_err(|e| format!("{}: {e}", path.display()))?;
        }
    }
    Ok(dir)
}

/// The command a tab types to start a session.
pub fn run_command(id: &str) -> String {
    format!("{} +agent run {}", self_command(), shell_quote(id))
}

/// This binary's full path, quoted for a shell (for hooks, which may run
/// with another PATH).
fn self_path() -> String {
    let exe = std::env::current_exe()
        .ok()
        .and_then(|p| p.canonicalize().ok())
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_else(|| "cyberterm".into());
    shell_quote(&exe)
}

/// How to call this binary from a shell: `cyberterm` when that's the one
/// on PATH, its full path otherwise (a dev build).
fn self_command() -> String {
    let exe = std::env::current_exe()
        .ok()
        .and_then(|p| p.canonicalize().ok());
    let on_path = std::env::var_os("PATH").and_then(|path| {
        std::env::split_paths(&path)
            .map(|d| d.join("cyberterm"))
            .find(|p| p.is_file())
            .and_then(|p| p.canonicalize().ok())
    });
    match exe {
        Some(exe) if on_path.as_ref() != Some(&exe) => shell_quote(&exe.to_string_lossy()),
        _ => "cyberterm".into(),
    }
}

// ----------------------------------------------------------------------
// Status and removal
// ----------------------------------------------------------------------

/// A session's state, for `+agent list` and removal.
#[derive(Debug, Default, PartialEq)]
pub struct Status {
    /// Processes running it (still has the session's id in their
    /// environment).
    pub running: bool,
    /// Uncommitted changes in the worktree (files).
    pub changed: usize,
    /// Commits on its branch since it started.
    pub commits: usize,
    pub worktree_missing: bool,
}

pub fn status(s: &Session) -> Status {
    let mut st = Status {
        running: !crate::procs::with_env("CYBERTERM_AGENT_ID", &s.id).is_empty(),
        ..Default::default()
    };
    if let Some(w) = &s.worktree {
        if !w.path.is_dir() {
            st.worktree_missing = true;
            return st;
        }
        st.changed = git(&w.path, &["status", "--porcelain"])
            .map(|out| out.lines().count())
            .unwrap_or(0);
        st.commits = git(
            &w.path,
            &["rev-list", "--count", &format!("{}..HEAD", w.base)],
        )
        .ok()
        .and_then(|n| n.trim().parse().ok())
        .unwrap_or(0);
    }
    st
}

/// Removes a session: its worktree, its branch when nothing was
/// committed on it, and its record. Refuses while the agent runs, and --
/// unless `force` -- when the worktree has changes or commits.
pub fn remove(id: &str, force: bool) -> Result<String, String> {
    remove_in(&state_dir(), id, force)
}

pub(crate) fn remove_in(state: &Path, id: &str, force: bool) -> Result<String, String> {
    let s = load_in(state, id)?;
    let st = status(&s);
    if st.running {
        return Err(format!("{id} is still running; quit the agent first"));
    }
    let mut notes = Vec::new();
    if let Some(w) = &s.worktree {
        if !st.worktree_missing {
            if !force && (st.changed > 0 || st.commits > 0) {
                return Err(format!(
                    "{id} has {} uncommitted file(s) and {} commit(s) in {}; \
                     merge or keep what you want, then use --force",
                    st.changed,
                    st.commits,
                    short(&w.path)
                ));
            }
            let path = w.path.to_string_lossy();
            let mut args = vec!["worktree", "remove"];
            if force {
                args.push("--force");
            }
            args.push(&path);
            let shells = crate::procs::with_cwd_under(&w.path).len();
            git(&w.repo, &args)?;
            notes.push(format!("removed {}", short(&w.path)));
            if shells > 0 {
                notes.push(format!(
                    "{shells} process(es) were still in it -- cd out of the deleted folder"
                ));
            }
        } else {
            let _ = git(&w.repo, &["worktree", "prune"]);
        }
        if st.commits == 0 {
            if git(&w.repo, &["branch", "-D", &w.branch]).is_ok() {
                notes.push(format!("deleted branch {}", w.branch));
            }
        } else {
            notes.push(format!(
                "kept branch {} ({} commit(s))",
                w.branch, st.commits
            ));
        }
    }
    let _ = std::fs::remove_file(record_path(state, id));
    let _ = std::fs::remove_file(state.join(format!("{id}.jsonl")));
    notes.push("removed the session and its flight log".into());
    Ok(notes.join(", "))
}

// ----------------------------------------------------------------------
// git
// ----------------------------------------------------------------------

pub(crate) fn git(dir: &Path, args: &[&str]) -> Result<String, String> {
    let out = Command::new("git")
        .arg("-C")
        .arg(dir)
        .args(args)
        .output()
        .map_err(|e| format!("git: {e}"))?;
    if out.status.success() {
        Ok(String::from_utf8_lossy(&out.stdout).trim_end().to_string())
    } else {
        Err(format!(
            "git {}: {}",
            args.first().unwrap_or(&""),
            String::from_utf8_lossy(&out.stderr).trim()
        ))
    }
}

fn git_toplevel(dir: &Path) -> Option<PathBuf> {
    git(dir, &["rev-parse", "--show-toplevel"])
        .ok()
        .map(PathBuf::from)
        .filter(|p| p.is_dir())
}

pub(crate) fn branch_exists(repo: &Path, branch: &str) -> bool {
    git(
        repo,
        &[
            "rev-parse",
            "--verify",
            "--quiet",
            &format!("refs/heads/{branch}"),
        ],
    )
    .is_ok()
}

/// A path for display, with the home directory as `~`.
pub fn short(path: &Path) -> String {
    let s = path.to_string_lossy().into_owned();
    match std::env::var("HOME") {
        Ok(home) if !home.is_empty() && s.starts_with(&home) => format!("~{}", &s[home.len()..]),
        _ => s,
    }
}

fn expand_home(path: &str) -> PathBuf {
    match path.strip_prefix("~/") {
        Some(rest) => std::env::var_os("HOME")
            .map(|h| PathBuf::from(h).join(rest))
            .unwrap_or_else(|| PathBuf::from(path)),
        None => PathBuf::from(path),
    }
}

// ----------------------------------------------------------------------
// CLI
// ----------------------------------------------------------------------

const HELP: &str = "\
cyberterm +agent: run any coding agent in a worktree of its own

  cyberterm +agent                       the agent home: your agents and sessions
  cyberterm +agent <name> [task...]      start one in a new tab, in a new worktree
  cyberterm +agent a,b,c <task...>       several agents on one task, to compare them
      --no-worktree                      run it in this directory instead
      --here                             run it in this terminal, not a new tab
  cyberterm +agent list                  sessions: running, changed files, commits
  cyberterm +agent log [id] [-o] [-f]    what it did: prompts, commands, edits
                                         (-o with output, -f keep following)
  cyberterm +agent setup [agent]         what each agent reports; opt-in hooks
                                         (Gemini, Cursor, Hermes)
  cyberterm +agent merge <id>            bring its work into the branch it started from
      --squash | --merge | --ff          one commit (default), a merge commit, or fast-forward
      --check                            show what it would do; change nothing
      --keep                             keep the worktree, branch and session afterwards
  cyberterm +agent discard <id> [--yes]  throw its work away: worktree, branch and session
  cyberterm +agent rm <id> [--force]     remove a session and its worktree
                                         (--force: even with changes or commits)

The worktree is ../<repo>-<id> on a new branch agent/<id>. Agents found on
PATH are offered automatically; add others under [agents.launch]:

  [agents.launch.mine]
  command = \"my-agent --flag\"
  label = \"My agent\"
  prompt = \"positional\"     # or a flag such as \"-i\", or \"none\"
";

pub fn run_cli(args: &[String], cfg: &AgentsConfig) -> i32 {
    let first = args.first().map(String::as_str);
    match first {
        None | Some("list" | "ls") => {
            list(cfg, first.is_none());
            0
        }
        Some("help" | "--help" | "-h") => {
            print!("{HELP}");
            0
        }
        Some("run") => {
            let Some(id) = args.get(1) else {
                eprintln!("usage: cyberterm +agent run <id>");
                return 2;
            };
            match exec(id, cfg) {
                Ok(never) => match never {},
                Err(e) => {
                    eprintln!("cyberterm +agent: {e}");
                    1
                }
            }
        }
        Some("setup") => crate::agent_setup::run(&args[1..], cfg),
        Some("merge") => crate::agent_merge::run_merge(&args[1..], cfg),
        Some("discard") => crate::agent_merge::run_discard(&args[1..]),
        Some("log") => {
            let follow = args.iter().any(|a| a == "-f" || a == "--follow");
            let output = args.iter().any(|a| a == "-o" || a == "--output");
            let id = args[1..].iter().find(|a| !a.starts_with('-')).cloned();
            let id = match id.or_else(|| sessions().first().map(|s| s.id.clone())) {
                Some(id) => id,
                None => {
                    eprintln!("No agent sessions yet.");
                    return 1;
                }
            };
            if let Err(e) = load(&id) {
                eprintln!("❌ {e}");
                return 1;
            }
            show_log(&id, output, follow);
            0
        }
        Some("rm" | "remove") => {
            let force = args.iter().any(|a| a == "--force" || a == "-f");
            let ids: Vec<&String> = args[1..].iter().filter(|a| !a.starts_with('-')).collect();
            if ids.is_empty() {
                eprintln!("usage: cyberterm +agent rm <id> [--force]");
                return 2;
            }
            let mut code = 0;
            for id in ids {
                match remove(id, force) {
                    Ok(what) => println!("{id}: {what}"),
                    Err(e) => {
                        eprintln!("❌ {e}");
                        code = 1;
                    }
                }
            }
            code
        }
        Some(name) => start(name, &args[1..], cfg),
    }
}

/// Opens a session in a new tab of the Cyberterm window behind `socket`.
fn open_in_tab(socket: &Path, session: &Session) -> Result<(), String> {
    let params = serde_json::json!({
        "cwd": session.dir,
        "title": session.title(),
        "command": run_command(&session.id),
        "flight_log": true,
    });
    match crate::control::call(socket, "new_tab", params) {
        Ok(resp) => match resp.error {
            None => Ok(()),
            Some(e) => Err(e.message),
        },
        Err(e) => Err(format!("couldn't reach Cyberterm ({e})")),
    }
}

/// `+agent <name>[,<name>...] [task]`: one session, or -- with several
/// agents -- one attempt each at the same task, every one in its own
/// worktree and tab, grouped so the Tower can compare them.
fn start(name: &str, rest: &[String], cfg: &AgentsConfig) -> i32 {
    let launchers = launchers(cfg);
    let mut chosen = Vec::new();
    for n in name.split(',').map(str::trim).filter(|n| !n.is_empty()) {
        match find(&launchers, n) {
            Some(l) => chosen.push(l),
            None => {
                eprintln!("❌ No agent called {n:?} here.");
                if !launchers.is_empty() {
                    let names: Vec<&str> = launchers.iter().map(|l| l.name.as_str()).collect();
                    eprintln!("   Found: {}", names.join(", "));
                }
                eprintln!("   Others can be added under [agents.launch] (cyberterm +agent help).");
                return 1;
            }
        }
    }
    if chosen.is_empty() {
        eprintln!("usage: cyberterm +agent <name>[,<name>...] [task]");
        return 2;
    }
    let mut worktree = cfg.worktrees;
    let mut here = false;
    let mut words = Vec::new();
    for arg in rest {
        match arg.as_str() {
            "--no-worktree" => worktree = false,
            "--here" => here = true,
            _ => words.push(arg.clone()),
        }
    }
    let task = Some(words.join(" ")).filter(|t| !t.trim().is_empty());
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let several = chosen.len() > 1;
    let socket = (!here).then(crate::control::find_socket).flatten();

    if several {
        // Attempts at the same task must not share files, and each needs
        // a tab.
        let problem = if here {
            Some("--here runs one agent; several each need a tab of their own")
        } else if !worktree {
            Some("several agents in one directory would trample each other: drop --no-worktree")
        } else if git_toplevel(&cwd).is_none() {
            Some("several agents at once need a git repository, so each gets its own worktree")
        } else if socket.is_none() {
            Some("several agents at once open in Cyberterm's tabs: run this inside Cyberterm")
        } else {
            None
        };
        if let Some(problem) = problem {
            eprintln!("❌ {problem}");
            return 1;
        }
        if task.is_none() {
            eprintln!("❌ Give them the task to compare them on: cyberterm +agent {name} \"…\"");
            return 2;
        }
    }

    let group = several.then(|| {
        format!(
            "{}-{}",
            slug(task.as_deref(), "group"),
            crate::shell::tap::now_ms()
        )
    });
    let mut sessions = Vec::new();
    for launcher in chosen {
        match create(
            cfg,
            Request {
                launcher,
                task: task.clone(),
                cwd: cwd.clone(),
                worktree,
                group: group.clone(),
            },
        ) {
            Ok(s) => sessions.push(s),
            Err(e) => {
                eprintln!("❌ {}: {e}", launcher.label);
                return 1;
            }
        }
    }

    for session in &sessions {
        if let Some(w) = &session.worktree {
            println!(
                "◆ {} · worktree {} on {}",
                session.label,
                short(&w.path),
                w.branch
            );
        }
    }
    if let Some(socket) = &socket {
        let mut failed = false;
        for session in &sessions {
            match open_in_tab(socket, session) {
                Ok(()) => println!("◆ Opened in a new tab ({})", session.id),
                Err(e) if several => {
                    eprintln!("❌ {}: couldn't open a tab ({e})", session.id);
                    failed = true;
                }
                Err(e) => {
                    eprintln!("couldn't open a tab ({e}); running it here");
                    failed = true;
                }
            }
        }
        if several {
            println!(
                "◆ {} attempts at one task: compare them in the Tower (Ctrl+Shift+S), merge the best, discard the rest",
                sessions.len()
            );
            return i32::from(failed);
        }
        if !failed {
            return 0;
        }
    }
    match exec(&sessions[0].id, cfg) {
        Ok(never) => match never {},
        Err(e) => {
            eprintln!("❌ {e}");
            1
        }
    }
}

/// The other sessions in `s`'s group that are still around.
pub(crate) fn siblings_in(state: &Path, s: &Session) -> Vec<Session> {
    let Some(group) = &s.group else {
        return Vec::new();
    };
    sessions_in(state)
        .into_iter()
        .filter(|o| o.id != s.id && o.group.as_ref() == Some(group))
        .collect()
}

/// Prints a session's flight log; with `follow`, keeps printing new
/// entries until interrupted.
fn show_log(id: &str, output: bool, follow: bool) {
    use std::io::IsTerminal;
    let path = crate::flight_log::log_path(id);
    let color = std::io::stdout().is_terminal();
    let mut shown = 0;
    loop {
        let entries = crate::flight_log::timeline(&crate::flight_log::read(&path));
        // Entries already shown can still change (a command finishing),
        // so in follow mode the last few are reprinted when they do.
        if entries.len() > shown || !follow {
            let new = &entries[shown.min(entries.len())..];
            print!("{}", crate::flight_log::render(new, output, color));
            shown = entries.len();
        }
        if !follow {
            if entries.is_empty() {
                println!("Nothing recorded for {id} yet.");
            }
            return;
        }
        std::thread::sleep(std::time::Duration::from_millis(500));
    }
}

fn list(cfg: &AgentsConfig, with_agents: bool) {
    if with_agents {
        let launchers = launchers(cfg);
        println!("Agents found here ({}):", launchers.len());
        for l in &launchers {
            let how = match &l.prompt {
                Prompt::Positional => "takes a task".to_string(),
                Prompt::Flag(f) => format!("takes a task ({f})"),
                Prompt::None => "start, then type the task".to_string(),
            };
            println!(
                "  {:<14} {:<16} {how:<28} {}",
                l.name,
                l.label,
                crate::agent_setup::coverage(adapter(&l.argv))
            );
        }
        println!();
    }
    let sessions = sessions();
    if sessions.is_empty() {
        println!("No agent sessions. Start one: cyberterm +agent <name> [task]");
        return;
    }
    println!("Sessions:");
    for s in &sessions {
        let st = status(s);
        let state = if st.running { "running" } else { "stopped" };
        let changes = match &s.worktree {
            Some(_) if st.worktree_missing => "worktree gone".to_string(),
            Some(_) => format!("{} changed · {} commits", st.changed, st.commits),
            None => "no worktree".to_string(),
        };
        println!(
            "  {:<28} {:<14} {:<8} {:<24} {}",
            s.id,
            s.agent,
            state,
            changes,
            short(&s.dir)
        );
    }
}

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

    fn launcher(name: &str, prompt: Prompt) -> Launcher {
        Launcher {
            name: name.into(),
            label: name.into(),
            argv: vec![name.into()],
            prompt,
        }
    }

    #[test]
    fn commands_split_like_a_shell() {
        assert_eq!(
            split_command(r#"aider --model "gpt 5" 'a b' c\ d"#).unwrap(),
            vec!["aider", "--model", "gpt 5", "a b", "c d"]
        );
        assert!(split_command("x 'open").is_err());
        assert_eq!(split_command("  ").unwrap(), Vec::<String>::new());
    }

    #[test]
    fn words_are_quoted_only_when_needed() {
        assert_eq!(shell_quote("fix-test"), "fix-test");
        assert_eq!(shell_quote("/a b/c"), "\"/a b/c\"");
        assert_eq!(shell_quote("$x\"y"), "\"\\$x\\\"y\"");
    }

    #[test]
    fn slugs_come_from_the_task() {
        assert_eq!(
            slug(Some("Fix the flaky login test, please!"), "claude"),
            "fix-the-flaky-login"
        );
        assert_eq!(slug(None, "cursor-agent"), "cursor-agent");
        assert_eq!(slug(Some("!!!"), "claude"), "claude");
        assert!(valid_id(&slug(Some("Ünïcode ✓ task"), "x")));
    }

    #[test]
    fn the_task_goes_where_each_agent_takes_it() {
        let mut s = Session {
            id: "t".into(),
            agent: "a".into(),
            label: "A".into(),
            argv: vec!["a".into(), "--yolo".into()],
            prompt: Prompt::Positional,
            task: Some("do it".into()),
            dir: PathBuf::from("/"),
            worktree: None,
            created_ms: 0,
            group: None,
        };
        assert_eq!(s.command_with(&[]), vec!["a", "--yolo", "do it"]);
        s.prompt = Prompt::Flag("-i".into());
        assert_eq!(s.command_with(&[]), vec!["a", "--yolo", "-i", "do it"]);
        s.prompt = Prompt::None;
        assert_eq!(s.command_with(&[]), vec!["a", "--yolo"]);
        s.task = None;
        s.prompt = Prompt::Positional;
        assert_eq!(s.command_with(&[]), vec!["a", "--yolo"]);
    }

    #[test]
    fn config_entries_override_and_extend_the_known_agents() {
        let mut cfg = AgentsConfig::default();
        cfg.launch.insert(
            "claude".into(),
            AgentLaunch {
                command: "claude --model opus".into(),
                ..Default::default()
            },
        );
        cfg.launch.insert(
            "mine".into(),
            AgentLaunch {
                command: "my-agent run".into(),
                label: "My agent".into(),
                prompt: "--task".into(),
            },
        );
        let all = launchers(&cfg);
        let claude = find(&all, "claude").unwrap();
        assert_eq!(claude.label, "Claude Code");
        assert_eq!(claude.argv, vec!["claude", "--model", "opus"]);
        assert_eq!(claude.prompt, Prompt::Positional);
        let mine = find(&all, "My Agent").unwrap();
        assert_eq!(mine.prompt, Prompt::Flag("--task".into()));
        assert_eq!(all.iter().filter(|l| l.name == "claude").count(), 1);
    }

    fn git_repo(dir: &Path) {
        let run = |args: &[&str]| {
            assert!(Command::new("git")
                .arg("-C")
                .arg(dir)
                .args(args)
                .output()
                .unwrap()
                .status
                .success());
        };
        std::fs::create_dir_all(dir.join("src")).unwrap();
        run(&["init", "-q", "-b", "main"]);
        run(&["config", "user.email", "t@example.com"]);
        run(&["config", "user.name", "t"]);
        std::fs::write(dir.join("src/a.txt"), "a\n").unwrap();
        run(&["add", "."]);
        run(&["commit", "-qm", "init"]);
    }

    fn scratch(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "cyberterm-agent-{name}-{}-{}",
            std::process::id(),
            crate::shell::tap::now_ms()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn sessions_get_their_own_worktree_and_unique_names() {
        let root = scratch("wt");
        let repo = root.join("api");
        git_repo(&repo);
        let state = root.join("state");
        let cfg = AgentsConfig::default();
        let l = launcher("claude", Prompt::Positional);
        let req = |task: &str| Request {
            launcher: &l,
            task: Some(task.into()),
            cwd: repo.join("src"),
            worktree: true,
            group: None,
        };

        let a = create_in(&state, &cfg, req("fix the test")).unwrap();
        std::fs::write(state.join("fix-the-test.jsonl"), "{}\n").unwrap();
        assert_eq!(a.id, "fix-the-test");
        let w = a.worktree.as_ref().unwrap();
        assert_eq!(w.path, root.join("api-fix-the-test"));
        assert_eq!(w.branch, "agent/fix-the-test");
        // Started from repo/src, so it runs in worktree/src.
        assert_eq!(a.dir, w.path.join("src"));
        assert!(a.dir.join("a.txt").is_file());
        assert_eq!(load_in(&state, "fix-the-test").unwrap(), a);

        let b = create_in(&state, &cfg, req("Fix the test")).unwrap();
        assert_eq!(b.id, "fix-the-test-2");
        assert_eq!(sessions_in(&state).len(), 2);

        // Outside a repository there's no worktree.
        let plain = root.join("plain");
        std::fs::create_dir_all(&plain).unwrap();
        let c = create_in(
            &state,
            &cfg,
            Request {
                launcher: &l,
                task: None,
                cwd: plain.clone(),
                worktree: true,
                group: None,
            },
        )
        .unwrap();
        assert_eq!(
            (c.id.as_str(), c.dir.as_path()),
            ("claude", plain.as_path())
        );
        assert!(c.worktree.is_none());

        let st = status(&a);
        assert_eq!((st.changed, st.commits, st.running), (0, 0, false));
        std::fs::write(a.dir.join("b.txt"), "b\n").unwrap();
        assert_eq!(status(&a).changed, 1);

        // Changes keep it unless forced; the branch goes when it has no
        // commits of its own.
        assert!(remove_in(&state, "fix-the-test", false).is_err());
        assert!(w.path.is_dir());
        remove_in(&state, "fix-the-test", true).unwrap();
        assert!(!w.path.exists());
        assert!(!branch_exists(&repo, "agent/fix-the-test"));
        assert!(load_in(&state, "fix-the-test").is_err());
        assert!(!state.join("fix-the-test.jsonl").exists());

        // A commit on its branch: the branch is kept.
        let bw = b.worktree.as_ref().unwrap();
        std::fs::write(bw.path.join("c.txt"), "c\n").unwrap();
        git(&bw.path, &["add", "."]).unwrap();
        git(&bw.path, &["commit", "-qm", "c"]).unwrap();
        assert_eq!(status(&b).commits, 1);
        assert!(remove_in(&state, &b.id, false).is_err());
        let note = remove_in(&state, &b.id, true).unwrap();
        assert!(note.contains("kept branch agent/fix-the-test-2"), "{note}");
        assert!(branch_exists(&repo, "agent/fix-the-test-2"));
        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn unknown_ids_are_rejected_before_touching_the_disk() {
        assert!(load_in(Path::new("/nonexistent"), "../etc/passwd").is_err());
        assert!(!valid_id("A"));
        assert!(valid_id("fix-1"));
    }
}