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
// src/agent_merge.rs
//
// Bringing an agent session's work back, or throwing it away:
// `cyberterm +agent merge <id>` and `cyberterm +agent discard <id>`.
//
// Merge takes the session's branch (agent/<id>) -- with whatever the agent
// left uncommitted committed onto it first -- into the branch the session
// started from, in your main checkout: as one squashed commit by default
// (`[agents] merge = "squash"`), a merge commit, or a fast-forward. It
// checks everything first and changes nothing when it can't finish: the
// agent still running, your checkout on another branch or with
// uncommitted changes, or conflicts (found with `git merge-tree`, which
// touches no files). Afterwards the worktree, branch and session go,
// unless `--keep`.
//
// Discard removes a session with its worktree and branch, work and all,
// after asking.

use crate::agent::{git, Session};
use std::path::{Path, PathBuf};
use std::process::Command;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Strategy {
    /// One commit on top of the target branch.
    Squash,
    /// A merge commit, keeping the agent's commits.
    Merge,
    /// Only when the target hasn't moved since the agent started.
    FastForward,
}

impl Strategy {
    pub fn parse(s: &str) -> Option<Self> {
        match s.trim() {
            "squash" => Some(Strategy::Squash),
            "merge" => Some(Strategy::Merge),
            "ff" | "fast-forward" => Some(Strategy::FastForward),
            _ => None,
        }
    }

    pub fn name(self) -> &'static str {
        match self {
            Strategy::Squash => "squash",
            Strategy::Merge => "merge commit",
            Strategy::FastForward => "fast-forward",
        }
    }
}

/// What merging a session would do, found without changing anything.
#[derive(Debug, PartialEq)]
pub struct Plan {
    pub branch: String,
    /// The branch it goes into.
    pub onto: String,
    pub repo: PathBuf,
    /// The agent's commits not yet on `onto`: "abc1234 subject", oldest
    /// first.
    pub commits: Vec<String>,
    /// Files it changed without committing (committed first on merge).
    pub uncommitted: usize,
    /// Files that conflict with `onto`.
    pub conflicts: Vec<String>,
    /// Why it can't merge right now, conflicts aside.
    pub blocked: Option<String>,
}

impl Plan {
    pub fn is_empty(&self) -> bool {
        self.commits.is_empty() && self.uncommitted == 0
    }

    pub fn ready(&self) -> bool {
        self.blocked.is_none() && self.conflicts.is_empty() && !self.is_empty()
    }
}

/// Runs git with extra environment; (success, stdout, stderr).
fn git_env(dir: &Path, args: &[&str], env: &[(&str, &Path)]) -> (bool, String, String) {
    let mut cmd = Command::new("git");
    cmd.arg("-C").arg(dir).args(args);
    for (k, v) in env {
        cmd.env(k, v);
    }
    match cmd.output() {
        Ok(out) => (
            out.status.success(),
            String::from_utf8_lossy(&out.stdout).trim_end().to_string(),
            String::from_utf8_lossy(&out.stderr).trim().to_string(),
        ),
        Err(e) => (false, String::new(), format!("git: {e}")),
    }
}

fn current_branch(repo: &Path) -> Option<String> {
    git(repo, &["symbolic-ref", "--short", "-q", "HEAD"])
        .ok()
        .filter(|b| !b.is_empty())
}

/// The worktree's state as a commit -- uncommitted and untracked files
/// included -- made through a scratch index, so neither the worktree's
/// index nor any branch changes. The commit stays unreferenced.
fn snapshot(worktree: &Path) -> Result<String, String> {
    static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let index = std::env::temp_dir().join(format!(
        "cyberterm-merge-index-{}-{}-{n}",
        std::process::id(),
        crate::shell::tap::now_ms()
    ));
    let env = [("GIT_INDEX_FILE", index.as_path())];
    let result = (|| {
        for args in [&["read-tree", "HEAD"][..], &["add", "-A"][..]] {
            let (ok, _, err) = git_env(worktree, args, &env);
            if !ok {
                return Err(format!("git {}: {err}", args[0]));
            }
        }
        let (ok, tree, err) = git_env(worktree, &["write-tree"], &env);
        if !ok {
            return Err(format!("git write-tree: {err}"));
        }
        let (ok, commit, err) = git_env(
            worktree,
            &[
                "commit-tree",
                &tree,
                "-p",
                "HEAD",
                "-m",
                "cyberterm merge check",
            ],
            &[],
        );
        if !ok {
            return Err(format!("git commit-tree: {err}"));
        }
        Ok(commit)
    })();
    let _ = std::fs::remove_file(&index);
    result
}

/// What merging `s` would do. `running`: whether its agent still runs.
pub fn plan(s: &Session, running: bool, strategy: Strategy) -> Result<Plan, String> {
    let Some(w) = &s.worktree else {
        return Err(format!(
            "{} ran in place, without a worktree: its changes are already in {}",
            s.id,
            crate::agent::short(&s.dir)
        ));
    };
    if !w.path.is_dir() {
        return Err(format!(
            "{}'s worktree is gone ({})",
            s.id,
            crate::agent::short(&w.path)
        ));
    }
    let current = current_branch(&w.repo);
    let onto = w
        .onto
        .clone()
        .or_else(|| current.clone())
        .ok_or_else(|| format!("{} has no branch checked out", crate::agent::short(&w.repo)))?;
    let uncommitted = git(&w.path, &["status", "--porcelain"])
        .map(|o| o.lines().count())
        .unwrap_or(0);
    let commits: Vec<String> = git(
        &w.repo,
        &[
            "log",
            "--reverse",
            "--format=%h %s",
            &format!("{onto}..{}", w.branch),
        ],
    )?
    .lines()
    .map(str::to_string)
    .collect();

    let repo_shown = crate::agent::short(&w.repo);
    let blocked = if running {
        Some(format!(
            "{} is still running: let it finish, or quit it first",
            s.id
        ))
    } else if current.as_deref() != Some(onto.as_str()) {
        Some(format!(
            "{repo_shown} is on {}, not {onto}: switch to it first (git switch {onto})",
            current.as_deref().unwrap_or("a detached HEAD")
        ))
    } else if git(&w.repo, &["status", "--porcelain", "--untracked-files=no"])
        .is_ok_and(|o| !o.is_empty())
    {
        Some(format!(
            "{repo_shown} has uncommitted changes: commit or stash them first"
        ))
    } else if strategy == Strategy::FastForward
        && !git_env(
            &w.repo,
            &["merge-base", "--is-ancestor", &onto, &w.branch],
            &[],
        )
        .0
    {
        Some(format!(
            "{onto} has moved on since {} started, so it can't fast-forward: squash or merge instead",
            s.id
        ))
    } else {
        None
    };

    let mut conflicts = Vec::new();
    if commits.len() + uncommitted > 0 {
        let head = if uncommitted > 0 {
            snapshot(&w.path)?
        } else {
            w.branch.clone()
        };
        let (ok, out, err) = git_env(
            &w.repo,
            &[
                "merge-tree",
                "--write-tree",
                "--name-only",
                "--no-messages",
                &onto,
                &head,
            ],
            &[],
        );
        if !ok {
            if out.is_empty() {
                return Err(format!("git merge-tree: {err}"));
            }
            // The tree, then the conflicting files.
            conflicts = out.lines().skip(1).map(str::to_string).collect();
            conflicts.dedup();
        }
    }
    Ok(Plan {
        branch: w.branch.clone(),
        onto,
        repo: w.repo.clone(),
        commits,
        uncommitted,
        conflicts,
        blocked,
    })
}

/// The squashed commit's message: the task as its subject, the agent's
/// own commits in the body.
fn squash_message(s: &Session, commits: &[String]) -> (String, String) {
    let subject = s
        .task
        .as_deref()
        .and_then(|t| t.lines().map(str::trim).find(|l| !l.is_empty()))
        .map(|t| {
            if t.chars().count() > 72 {
                format!("{}…", t.chars().take(71).collect::<String>())
            } else {
                t.to_string()
            }
        })
        .unwrap_or_else(|| format!("Work from {} session {}", s.label, s.id));
    let mut body = format!("From {} (agent session {}).", s.label, s.id);
    if !commits.is_empty() {
        body.push_str("\n\nIts commits:");
        for c in commits.iter().take(30) {
            body.push_str(&format!("\n- {c}"));
        }
        if commits.len() > 30 {
            body.push_str(&format!("\n- and {} more", commits.len() - 30));
        }
    }
    (subject, body)
}

/// Merges session `id` from the sessions in `state`; returns what it did.
pub fn merge_in(state: &Path, id: &str, strategy: Strategy, keep: bool) -> Result<String, String> {
    let s = crate::agent::load_in(state, id)?;
    let running = crate::agent::status(&s).running;
    let p = plan(&s, running, strategy)?;
    if let Some(why) = &p.blocked {
        return Err(why.clone());
    }
    if p.is_empty() {
        return Ok(format!("nothing to merge: {id} changed nothing"));
    }
    let w = s.worktree.as_ref().expect("plan needs a worktree");
    if !p.conflicts.is_empty() {
        return Err(format!(
            "{id} conflicts with {} in: {}\n   Nothing was changed. Ask the agent to merge {} into its branch and resolve them, or do it in {}",
            p.onto,
            p.conflicts.join(", "),
            p.onto,
            crate::agent::short(&w.path)
        ));
    }

    // Whatever the agent left uncommitted goes onto its branch first.
    let (subject, _) = squash_message(&s, &[]);
    if p.uncommitted > 0 {
        git(&w.path, &["add", "-A"])?;
        git(
            &w.path,
            &[
                "commit",
                "-q",
                "-m",
                &format!("{subject} (uncommitted work)"),
            ],
        )?;
    }
    let commits: Vec<String> = git(
        &w.repo,
        &[
            "log",
            "--reverse",
            "--format=%h %s",
            &format!("{}..{}", p.onto, w.branch),
        ],
    )?
    .lines()
    .map(str::to_string)
    .collect();

    match strategy {
        Strategy::Squash => {
            let (subject, body) = squash_message(&s, &commits);
            git(&w.repo, &["merge", "--squash", "-q", &w.branch])?;
            if let Err(e) = git(&w.repo, &["commit", "-q", "-m", &subject, "-m", &body]) {
                let _ = git(&w.repo, &["reset", "-q", "--merge"]);
                return Err(format!(
                    "the squashed commit failed, so the merge was undone: {e}"
                ));
            }
        }
        Strategy::Merge => {
            let message = format!("Merge {}: {subject}", w.branch);
            if let Err(e) = git(
                &w.repo,
                &["merge", "--no-ff", "-q", "-m", &message, &w.branch],
            ) {
                let _ = git(&w.repo, &["merge", "--abort"]);
                return Err(format!("the merge failed and was undone: {e}"));
            }
        }
        Strategy::FastForward => {
            git(&w.repo, &["merge", "--ff-only", "-q", &w.branch])?;
        }
    }
    let head = git(&w.repo, &["rev-parse", "--short", "HEAD"])?;
    let n = commits.len();
    let mut note = format!(
        "merged into {} as {head} ({}, {n} commit{})",
        p.onto,
        strategy.name(),
        if n == 1 { "" } else { "s" }
    );
    let others: Vec<String> = crate::agent::siblings_in(state, &s)
        .into_iter()
        .map(|o| o.id)
        .collect();
    if !keep {
        crate::agent::remove_in(state, id, true)?;
        if crate::agent::branch_exists(&w.repo, &w.branch) {
            git(&w.repo, &["branch", "-D", &w.branch])?;
        }
        note.push_str("; removed its worktree, branch and session");
    }
    if !others.is_empty() {
        note.push_str(&format!(
            "\n   The other attempts at this task are still there: cyberterm +agent discard {}",
            others.join(" ")
        ));
    }
    Ok(note)
}

/// Ends a session's agent -- every process carrying its id, which is the
/// agent and whatever it started -- with SIGTERM, and waits up to five
/// seconds for them to go. For an agent that finished its turn but stays
/// open (most do), before merging or discarding its work.
pub fn stop_agent(id: &str) -> Result<(), String> {
    let pids = crate::procs::with_env("CYBERTERM_AGENT_ID", id);
    for &pid in &pids {
        // SAFETY: plain kill(2) on a pid read from /proc.
        unsafe {
            libc::kill(pid as libc::pid_t, libc::SIGTERM);
        }
    }
    for _ in 0..50 {
        if crate::procs::with_env("CYBERTERM_AGENT_ID", id).is_empty() {
            return Ok(());
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
    }
    Err(format!(
        "{id}'s agent didn't quit; quit it in its tab first"
    ))
}

/// Removes session `id` with its worktree and branch, work and all.
pub fn discard_in(state: &Path, id: &str) -> Result<String, String> {
    let s = crate::agent::load_in(state, id)?;
    let note = crate::agent::remove_in(state, id, true)?;
    let Some(w) = &s.worktree else {
        return Ok(note);
    };
    if crate::agent::branch_exists(&w.repo, &w.branch) {
        git(&w.repo, &["branch", "-D", &w.branch])?;
    }
    // remove_in says it kept a branch that has commits; here it's gone.
    let mut parts: Vec<&str> = note
        .split(", ")
        .filter(|p| !p.starts_with("kept branch") && !p.starts_with("deleted branch"))
        .collect();
    let deleted = format!("deleted branch {}", w.branch);
    parts.insert(1.min(parts.len()), &deleted);
    Ok(parts.join(", "))
}

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

fn describe(p: &Plan, strategy: Strategy, id: &str) -> String {
    let mut out = format!(
        "{id} → {}  ({}, in {})\n",
        p.onto,
        strategy.name(),
        crate::agent::short(&p.repo)
    );
    let n = p.commits.len();
    out.push_str(&format!("  {n} commit{}", if n == 1 { "" } else { "s" }));
    if p.uncommitted > 0 {
        out.push_str(&format!(
            ", {} uncommitted file{} (committed first)",
            p.uncommitted,
            if p.uncommitted == 1 { "" } else { "s" }
        ));
    }
    out.push('\n');
    for c in p.commits.iter().take(10) {
        out.push_str(&format!("    {c}\n"));
    }
    if p.is_empty() {
        out.push_str("  nothing to merge\n");
    } else if p.conflicts.is_empty() {
        out.push_str("  ✓ no conflicts\n");
    } else {
        out.push_str(&format!("  ✗ conflicts in: {}\n", p.conflicts.join(", ")));
    }
    if let Some(why) = &p.blocked {
        out.push_str(&format!("  ✗ {why}\n"));
    }
    out
}

/// `cyberterm +agent merge <id> [--squash|--merge|--ff] [--keep] [--check]`
pub fn run_merge(args: &[String], cfg: &crate::config::AgentsConfig) -> i32 {
    let mut strategy = Strategy::parse(&cfg.merge).unwrap_or(Strategy::Squash);
    let mut keep = false;
    let mut check = false;
    let mut id = None;
    for a in args {
        match a.as_str() {
            "--squash" => strategy = Strategy::Squash,
            "--merge" => strategy = Strategy::Merge,
            "--ff" | "--fast-forward" => strategy = Strategy::FastForward,
            "--keep" => keep = true,
            "--check" | "--dry-run" | "-n" => check = true,
            s if s.starts_with('-') => {
                eprintln!("cyberterm +agent merge: unknown option {s}");
                return 2;
            }
            s => id = Some(s.to_string()),
        }
    }
    let Some(id) = id else {
        eprintln!("usage: cyberterm +agent merge <id> [--squash|--merge|--ff] [--keep] [--check]");
        return 2;
    };
    let state = crate::agent::state_dir();
    if check {
        let s = match crate::agent::load_in(&state, &id) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("❌ {e}");
                return 1;
            }
        };
        let running = crate::agent::status(&s).running;
        return match plan(&s, running, strategy) {
            Ok(p) => {
                print!("{}", describe(&p, strategy, &id));
                if p.ready() {
                    0
                } else {
                    1
                }
            }
            Err(e) => {
                eprintln!("❌ {e}");
                1
            }
        };
    }
    match merge_in(&state, &id, strategy, keep) {
        Ok(note) => {
            println!("✓ {id}: {note}");
            0
        }
        Err(e) => {
            eprintln!("❌ {e}");
            1
        }
    }
}

/// `cyberterm +agent discard <id>... [--yes]`
pub fn run_discard(args: &[String]) -> i32 {
    use std::io::{BufRead, IsTerminal, Write};
    let yes = args.iter().any(|a| a == "--yes" || a == "-y");
    let ids: Vec<&String> = args.iter().filter(|a| !a.starts_with('-')).collect();
    if ids.is_empty() {
        eprintln!("usage: cyberterm +agent discard <id>... [--yes]");
        return 2;
    }
    let state = crate::agent::state_dir();
    let mut code = 0;
    for id in ids {
        let s = match crate::agent::load_in(&state, id) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("❌ {e}");
                code = 1;
                continue;
            }
        };
        let st = crate::agent::status(&s);
        if !yes {
            if !std::io::stdin().is_terminal() {
                eprintln!("❌ {id}: discarding throws its work away; add --yes to confirm");
                code = 1;
                continue;
            }
            let what = match &s.worktree {
                Some(w) => format!(
                    "{} changed file(s) and {} commit(s) on {} will be lost",
                    st.changed, st.commits, w.branch
                ),
                None => "its record and flight log will be removed".into(),
            };
            print!("Discard {id}? {what}. [y/N] ");
            let _ = std::io::stdout().flush();
            let mut answer = String::new();
            let _ = std::io::stdin().lock().read_line(&mut answer);
            if !matches!(answer.trim(), "y" | "Y" | "yes") {
                println!("Kept {id}.");
                continue;
            }
        }
        match discard_in(&state, id) {
            Ok(note) => println!("✓ {id}: {note}"),
            Err(e) => {
                eprintln!("❌ {e}");
                code = 1;
            }
        }
    }
    code
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent::{create_in, Launcher, Prompt, Request};
    use crate::config::AgentsConfig;

    struct Fixture {
        root: PathBuf,
        repo: PathBuf,
        state: PathBuf,
    }

    impl Drop for Fixture {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.root);
        }
    }

    fn fixture(name: &str) -> Fixture {
        let root = std::env::temp_dir().join(format!(
            "cyberterm-merge-{name}-{}-{}",
            std::process::id(),
            crate::shell::tap::now_ms()
        ));
        let repo = root.join("api");
        std::fs::create_dir_all(&repo).unwrap();
        for args in [
            &["init", "-q", "-b", "main"][..],
            &["config", "user.email", "t@example.com"][..],
            &["config", "user.name", "t"][..],
        ] {
            git(&repo, args).unwrap();
        }
        std::fs::write(repo.join("app.py"), "a = 1\nb = 2\n").unwrap();
        git(&repo, &["add", "."]).unwrap();
        git(&repo, &["commit", "-qm", "init"]).unwrap();
        Fixture {
            state: root.join("state"),
            root,
            repo,
        }
    }

    fn session(f: &Fixture, task: &str) -> Session {
        attempt(f, task, "demo", None)
    }

    fn attempt(f: &Fixture, task: &str, agent: &str, group: Option<&str>) -> Session {
        let l = Launcher {
            name: agent.into(),
            label: "Demo agent".into(),
            argv: vec!["true".into()],
            prompt: Prompt::Positional,
        };
        create_in(
            &f.state,
            &AgentsConfig::default(),
            Request {
                launcher: &l,
                task: Some(task.into()),
                cwd: f.repo.clone(),
                worktree: true,
                group: group.map(str::to_string),
            },
        )
        .unwrap()
    }

    fn wt(s: &Session) -> PathBuf {
        s.worktree.as_ref().unwrap().path.clone()
    }

    fn log(repo: &Path) -> Vec<String> {
        git(repo, &["log", "--format=%s"])
            .unwrap()
            .lines()
            .map(str::to_string)
            .collect()
    }

    #[test]
    fn squash_merges_committed_and_uncommitted_work_then_cleans_up() {
        let f = fixture("squash");
        let s = session(&f, "Give DATABASE_URL a default");
        assert_eq!(s.worktree.as_ref().unwrap().onto.as_deref(), Some("main"));
        // One commit of its own, plus a file it never committed.
        std::fs::write(wt(&s).join("app.py"), "a = 1\nb = 3\n").unwrap();
        git(&wt(&s), &["commit", "-qam", "change b"]).unwrap();
        std::fs::write(wt(&s).join("new.py"), "x = 1\n").unwrap();

        let p = plan(&s, false, Strategy::Squash).unwrap();
        assert_eq!((p.commits.len(), p.uncommitted), (1, 1), "{p:?}");
        assert!(p.commits[0].ends_with(" change b"));
        assert!(p.ready(), "{p:?}");
        // Planning changed nothing in the worktree.
        assert_eq!(
            git(&wt(&s), &["status", "--porcelain"]).unwrap(),
            "?? new.py"
        );

        let note = merge_in(&f.state, &s.id, Strategy::Squash, false).unwrap();
        assert!(note.starts_with("merged into main as "), "{note}");
        assert!(note.contains("squash, 2 commits"), "{note}");
        assert_eq!(log(&f.repo), ["Give DATABASE_URL a default", "init"]);
        let body = git(&f.repo, &["log", "-1", "--format=%b"]).unwrap();
        assert!(
            body.contains(&format!("From Demo agent (agent session {}).", s.id)),
            "{body}"
        );
        assert!(body.contains("(uncommitted work)"), "{body}");
        assert!(body.contains("change b"), "{body}");
        assert_eq!(
            std::fs::read_to_string(f.repo.join("app.py")).unwrap(),
            "a = 1\nb = 3\n"
        );
        assert!(f.repo.join("new.py").is_file());
        // Worktree, branch and session gone.
        assert!(!wt(&s).exists());
        assert!(!crate::agent::branch_exists(
            &f.repo,
            &format!("agent/{}", s.id)
        ));
        assert!(crate::agent::load_in(&f.state, &s.id).is_err());
    }

    #[test]
    fn merge_commit_and_fast_forward_keep_the_agents_commits() {
        let f = fixture("ff");
        let s = session(&f, "one");
        std::fs::write(wt(&s).join("one.txt"), "1\n").unwrap();
        git(&wt(&s), &["add", "."]).unwrap();
        git(&wt(&s), &["commit", "-qm", "add one"]).unwrap();
        merge_in(&f.state, &s.id, Strategy::FastForward, true).unwrap();
        assert_eq!(log(&f.repo), ["add one", "init"]);
        // --keep: everything still there.
        assert!(wt(&s).is_dir());
        assert!(crate::agent::load_in(&f.state, &s.id).is_ok());

        let t = session(&f, "two");
        std::fs::write(wt(&t).join("two.txt"), "2\n").unwrap();
        git(&wt(&t), &["add", "."]).unwrap();
        git(&wt(&t), &["commit", "-qm", "add two"]).unwrap();
        // main moves on: no fast-forward, but a merge commit works.
        std::fs::write(f.repo.join("three.txt"), "3\n").unwrap();
        git(&f.repo, &["add", "."]).unwrap();
        git(&f.repo, &["commit", "-qm", "add three"]).unwrap();
        let p = plan(&t, false, Strategy::FastForward).unwrap();
        assert!(
            p.blocked.as_deref().unwrap().contains("can't fast-forward"),
            "{p:?}"
        );
        merge_in(&f.state, &t.id, Strategy::Merge, false).unwrap();
        let subjects = log(&f.repo);
        assert_eq!(subjects[0], "Merge agent/two: two");
        assert!(subjects.contains(&"add two".to_string()));
    }

    #[test]
    fn conflicts_and_a_busy_checkout_change_nothing() {
        let f = fixture("conflict");
        let s = session(&f, "edit b");
        std::fs::write(wt(&s).join("app.py"), "a = 1\nb = 3\n").unwrap();
        // Uncommitted in the worktree, and main changes the same line.
        std::fs::write(f.repo.join("app.py"), "a = 1\nb = 4\n").unwrap();
        git(&f.repo, &["commit", "-qam", "b is 4"]).unwrap();

        let p = plan(&s, false, Strategy::Squash).unwrap();
        assert_eq!(p.conflicts, ["app.py"], "{p:?}");
        let err = merge_in(&f.state, &s.id, Strategy::Squash, false).unwrap_err();
        assert!(err.contains("conflicts with main in: app.py"), "{err}");
        assert!(err.contains("Nothing was changed"), "{err}");
        // Untouched: the agent's uncommitted edit, main, the session.
        assert_eq!(
            git(&wt(&s), &["status", "--porcelain"]).unwrap(),
            " M app.py"
        );
        assert_eq!(log(&f.repo), ["b is 4", "init"]);
        assert!(wt(&s).is_dir());

        // Your own uncommitted changes, or another branch, block it.
        let t = session(&f, "other");
        std::fs::write(wt(&t).join("other.txt"), "o\n").unwrap();
        std::fs::write(f.repo.join("app.py"), "mine\n").unwrap();
        let p = plan(&t, false, Strategy::Squash).unwrap();
        assert!(
            p.blocked
                .as_deref()
                .unwrap()
                .contains("uncommitted changes"),
            "{p:?}"
        );
        git(&f.repo, &["checkout", "-q", "--", "app.py"]).unwrap();
        git(&f.repo, &["switch", "-q", "-c", "elsewhere"]).unwrap();
        let p = plan(&t, false, Strategy::Squash).unwrap();
        assert!(
            p.blocked
                .as_deref()
                .unwrap()
                .contains("is on elsewhere, not main"),
            "{p:?}"
        );
        assert!(plan(&t, true, Strategy::Squash)
            .unwrap()
            .blocked
            .unwrap()
            .contains("still running"));
    }

    #[test]
    fn discard_removes_work_branch_and_session() {
        let f = fixture("discard");
        let s = session(&f, "throwaway");
        std::fs::write(wt(&s).join("x.txt"), "x\n").unwrap();
        git(&wt(&s), &["add", "."]).unwrap();
        git(&wt(&s), &["commit", "-qm", "x"]).unwrap();
        let note = discard_in(&f.state, &s.id).unwrap();
        assert!(note.contains("deleted branch agent/throwaway"), "{note}");
        assert!(!note.contains("kept branch"), "{note}");
        assert!(!wt(&s).exists());
        assert!(!crate::agent::branch_exists(&f.repo, "agent/throwaway"));
        assert_eq!(log(&f.repo), ["init"]);
        // Nothing to merge, nothing done.
        let t = session(&f, "idle");
        assert!(plan(&t, false, Strategy::Squash).unwrap().is_empty());
        let note = merge_in(&f.state, &t.id, Strategy::Squash, false).unwrap();
        assert!(note.starts_with("nothing to merge"), "{note}");
        assert!(wt(&t).is_dir());
    }

    #[test]
    fn attempts_at_one_task_are_named_by_agent_and_point_at_each_other() {
        let f = fixture("group");
        let a = attempt(&f, "fix the flaky test", "claude", Some("g1"));
        let b = attempt(&f, "fix the flaky test", "codex", Some("g1"));
        let lone = session(&f, "something else");
        assert_eq!(a.id, "fix-the-flaky-test-claude");
        assert_eq!(b.id, "fix-the-flaky-test-codex");
        assert_eq!(
            b.worktree.as_ref().unwrap().branch,
            "agent/fix-the-flaky-test-codex"
        );
        let ids = |v: Vec<Session>| v.into_iter().map(|s| s.id).collect::<Vec<_>>();
        assert_eq!(
            ids(crate::agent::siblings_in(&f.state, &a)),
            [b.id.as_str()]
        );
        assert!(crate::agent::siblings_in(&f.state, &lone).is_empty());

        // Merging one says how to drop the others.
        std::fs::write(wt(&a).join("fix.txt"), "fixed\n").unwrap();
        let note = merge_in(&f.state, &a.id, Strategy::Squash, false).unwrap();
        assert!(
            note.contains("other attempts at this task are still there: cyberterm +agent discard fix-the-flaky-test-codex"),
            "{note}"
        );
        // Once it's merged it's gone, and the last attempt stands alone.
        assert!(crate::agent::siblings_in(&f.state, &b).is_empty());
    }

    #[test]
    fn strategies_parse() {
        assert_eq!(Strategy::parse("squash"), Some(Strategy::Squash));
        assert_eq!(Strategy::parse("ff"), Some(Strategy::FastForward));
        assert_eq!(Strategy::parse("rebase"), None);
    }
}