git-paw 0.2.0

Parallel AI Worktrees — orchestrate multiple AI coding CLI sessions across git worktrees
Documentation
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
//! End-to-end tests.
//!
//! Tests the `git-paw` binary and tmux orchestration in realistic scenarios.

use std::fs;
use std::path::{Path, PathBuf};

use assert_cmd::Command;
use predicates::prelude::*;
use serial_test::serial;
use tempfile::TempDir;

use git_paw::tmux::{
    PaneSpec, TmuxSessionBuilder, attach, ensure_tmux_installed, is_session_alive, kill_session,
};

fn cmd() -> Command {
    Command::cargo_bin("git-paw").expect("binary exists")
}

// ---------------------------------------------------------------------------
// Test repo helper
// ---------------------------------------------------------------------------

struct TestRepo {
    _sandbox: TempDir,
    repo: PathBuf,
}

impl TestRepo {
    fn path(&self) -> &Path {
        &self.repo
    }
}

fn setup_test_repo() -> TestRepo {
    let sandbox = TempDir::new().expect("create temp dir");
    let repo = sandbox.path().join("test-repo");
    fs::create_dir_all(&repo).expect("create repo dir");

    run_git(&repo, &["init"]);
    run_git(&repo, &["config", "user.email", "test@test.com"]);
    run_git(&repo, &["config", "user.name", "Test"]);

    let readme = repo.join("README.md");
    fs::write(&readme, "# Test repo").expect("write README");
    run_git(&repo, &["add", "."]);
    run_git(&repo, &["commit", "-m", "initial commit"]);

    TestRepo {
        _sandbox: sandbox,
        repo,
    }
}

fn run_git(dir: &Path, args: &[&str]) {
    let output = std::process::Command::new("git")
        .current_dir(dir)
        .args(args)
        .output()
        .expect("run git command");
    assert!(
        output.status.success(),
        "git {} failed: {}",
        args.join(" "),
        String::from_utf8_lossy(&output.stderr)
    );
}

// ---------------------------------------------------------------------------
// Dry-run
// ---------------------------------------------------------------------------

#[test]
fn dry_run_with_flags_shows_plan() {
    let tr = setup_test_repo();
    run_git(tr.path(), &["branch", "feat/a"]);
    run_git(tr.path(), &["branch", "feat/b"]);

    // Register "echo" as a custom CLI so detection finds it
    let config = tr.path().join(".git-paw").join("config.toml");
    fs::create_dir_all(config.parent().unwrap()).expect("create config dir");
    fs::write(&config, "[clis.echo]\ncommand = \"/bin/echo\"\n").expect("write config");

    cmd()
        .current_dir(tr.path())
        .args([
            "start",
            "--dry-run",
            "--cli",
            "echo",
            "--branches",
            "feat/a,feat/b",
        ])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Dry run")
                .and(predicate::str::contains("feat/a"))
                .and(predicate::str::contains("feat/b"))
                .and(predicate::str::contains("echo")),
        );
}

// ---------------------------------------------------------------------------
// Non-interactive flags
// ---------------------------------------------------------------------------

#[test]
fn preset_not_found_returns_error() {
    let tr = setup_test_repo();

    cmd()
        .current_dir(tr.path())
        .args(["start", "--preset", "nonexistent"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found"));
}

// ---------------------------------------------------------------------------
// Stop and purge from repo with no session
// ---------------------------------------------------------------------------

#[test]
fn stop_with_no_session() {
    let tr = setup_test_repo();

    cmd()
        .current_dir(tr.path())
        .arg("stop")
        .assert()
        .success()
        .stdout(predicate::str::contains("No active session"));
}

#[test]
fn purge_with_no_session() {
    let tr = setup_test_repo();

    cmd()
        .current_dir(tr.path())
        .args(["purge", "--force"])
        .assert()
        .success()
        .stdout(predicate::str::contains("No session to purge"));
}

// ---------------------------------------------------------------------------
// Status
// ---------------------------------------------------------------------------

#[test]
fn status_with_no_session() {
    let tr = setup_test_repo();

    cmd()
        .current_dir(tr.path())
        .arg("status")
        .assert()
        .success()
        .stdout(predicate::str::contains("No session"));
}

// ---------------------------------------------------------------------------
// Not-a-repo errors
// ---------------------------------------------------------------------------

#[test]
fn stop_from_non_git_dir_fails() {
    let tmp = TempDir::new().expect("create temp dir");

    cmd()
        .current_dir(tmp.path())
        .arg("stop")
        .assert()
        .failure()
        .stderr(predicate::str::contains("Not a git repository"));
}

#[test]
fn status_from_non_git_dir_fails() {
    let tmp = TempDir::new().expect("create temp dir");

    cmd()
        .current_dir(tmp.path())
        .arg("status")
        .assert()
        .failure()
        .stderr(predicate::str::contains("Not a git repository"));
}

// ---------------------------------------------------------------------------
// Init
// ---------------------------------------------------------------------------

#[test]
fn init_creates_git_paw_dir() {
    let tr = setup_test_repo();

    cmd()
        .current_dir(tr.path())
        .arg("init")
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Created .git-paw/")
                .and(predicate::str::contains("Initialized git-paw")),
        );

    assert!(tr.path().join(".git-paw").is_dir());
    assert!(tr.path().join(".git-paw/config.toml").exists());
    assert!(tr.path().join(".git-paw/logs").is_dir());
    assert!(!tr.path().join("AGENTS.md").exists());
}

#[test]
fn init_outside_git_repo_fails() {
    let tmp = TempDir::new().expect("create temp dir");

    cmd()
        .current_dir(tmp.path())
        .arg("init")
        .assert()
        .failure()
        .stderr(predicate::str::contains("Not a git repository"));
}

#[test]
fn init_is_idempotent() {
    let tr = setup_test_repo();

    cmd().current_dir(tr.path()).arg("init").assert().success();

    cmd()
        .current_dir(tr.path())
        .arg("init")
        .assert()
        .success()
        .stdout(predicate::str::contains("Already initialized"));
}

// ---------------------------------------------------------------------------
// From-specs
// ---------------------------------------------------------------------------

#[test]
fn from_specs_no_specs_config_returns_error() {
    let tr = setup_test_repo();

    // Create a .git-paw/config.toml without [specs] section
    let config = tr.path().join(".git-paw").join("config.toml");
    fs::create_dir_all(config.parent().unwrap()).expect("create config dir");
    fs::write(&config, "# no specs section\n").expect("write config");

    cmd()
        .current_dir(tr.path())
        .args(["start", "--from-specs"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("[specs]"));
}

#[test]
fn from_specs_dry_run_with_valid_specs_shows_plan() {
    let tr = setup_test_repo();

    // Create config with [specs] section pointing to openspec/changes
    let paw_dir = tr.path().join(".git-paw");
    fs::create_dir_all(&paw_dir).expect("create .git-paw");
    fs::write(
        paw_dir.join("config.toml"),
        "[specs]\ndir = \"openspec/changes\"\ntype = \"openspec\"\n",
    )
    .expect("write config");

    // Register "echo" as a custom CLI so detection finds it
    let config_content = fs::read_to_string(paw_dir.join("config.toml")).unwrap();
    fs::write(
        paw_dir.join("config.toml"),
        format!("{config_content}\n[clis.echo]\ncommand = \"/bin/echo\"\n"),
    )
    .expect("append cli");

    // Create a spec with tasks.md
    let spec_dir = tr.path().join("openspec/changes/add-auth");
    fs::create_dir_all(&spec_dir).expect("create spec dir");
    fs::write(spec_dir.join("tasks.md"), "Implement authentication\n").expect("write tasks.md");

    cmd()
        .current_dir(tr.path())
        .args(["start", "--from-specs", "--dry-run", "--cli", "echo"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Dry run")
                .and(predicate::str::contains("spec/add-auth"))
                .and(predicate::str::contains("echo")),
        );
}

#[test]
fn from_specs_empty_specs_dir_prints_no_pending() {
    let tr = setup_test_repo();

    // Create config with [specs] section
    let paw_dir = tr.path().join(".git-paw");
    fs::create_dir_all(&paw_dir).expect("create .git-paw");
    fs::write(
        paw_dir.join("config.toml"),
        "[specs]\ndir = \"openspec/changes\"\ntype = \"openspec\"\n",
    )
    .expect("write config");

    // Create the empty specs directory
    fs::create_dir_all(tr.path().join("openspec/changes")).expect("create specs dir");

    cmd()
        .current_dir(tr.path())
        .args(["start", "--from-specs"])
        .assert()
        .success()
        .stdout(predicate::str::contains("No pending specs"));
}

// ---------------------------------------------------------------------------
// Replay
// ---------------------------------------------------------------------------

#[test]
fn replay_list_with_no_logs_shows_message() {
    let tr = setup_test_repo();

    cmd()
        .current_dir(tr.path())
        .args(["replay", "--list"])
        .assert()
        .success()
        .stdout(predicate::str::contains("No log sessions"));
}

#[test]
fn replay_nonexistent_branch_shows_error() {
    let tr = setup_test_repo();

    // Create a session log directory with one log so resolve_session succeeds
    let log_dir = tr.path().join(".git-paw/logs/paw-test");
    fs::create_dir_all(&log_dir).expect("create log dir");
    fs::write(log_dir.join("main.log"), "some log content").expect("write log");

    cmd()
        .current_dir(tr.path())
        .args(["replay", "nonexistent-branch"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("nonexistent-branch"));
}

// ---------------------------------------------------------------------------
// Tmux-dependent
// ---------------------------------------------------------------------------

/// Helper: kill a session if it exists, ignoring errors.
fn cleanup_session(name: &str) {
    let _ = kill_session(name);
}

#[test]
#[serial]
fn tmux_session_create_and_kill_lifecycle() {
    ensure_tmux_installed().expect("tmux must be installed to run this test");

    let session_name = "paw-e2e-lifecycle-test";
    cleanup_session(session_name);

    let tmp = TempDir::new().expect("create temp dir");
    let worktree_path = tmp.path().to_string_lossy().to_string();

    let session = TmuxSessionBuilder::new("e2e-lifecycle-test")
        .add_pane(PaneSpec {
            branch: "feat/auth".into(),
            worktree: worktree_path.clone(),
            cli_command: "echo hello".into(),
        })
        .build()
        .expect("build session");

    session.execute().expect("execute session");

    assert!(
        is_session_alive(session_name).expect("check session"),
        "session should be alive after creation"
    );

    kill_session(session_name).expect("kill session");

    assert!(
        !is_session_alive(session_name).expect("check session"),
        "session should be dead after kill"
    );
}

#[test]
#[serial]
fn tmux_session_with_five_panes_and_different_clis() {
    ensure_tmux_installed().expect("tmux must be installed to run this test");

    let session_name = "paw-e2e-multipane-test";
    cleanup_session(session_name);

    let tmp = TempDir::new().expect("create temp dir");
    let worktree = tmp.path().to_string_lossy().to_string();

    let panes = vec![
        ("feat/auth", "claude"),
        ("feat/api", "codex"),
        ("fix/db", "gemini"),
        ("feat/ui", "aider"),
        ("refactor/cache", "amp"),
    ];

    let mut builder = TmuxSessionBuilder::new("e2e-multipane-test");
    for (branch, cli) in &panes {
        builder = builder.add_pane(PaneSpec {
            branch: (*branch).into(),
            worktree: worktree.clone(),
            cli_command: (*cli).into(),
        });
    }

    let session = builder.build().expect("build session");
    session.execute().expect("execute session");

    assert!(
        is_session_alive(session_name).expect("check session"),
        "5-pane session should be alive"
    );

    // Verify pane count
    let output = std::process::Command::new("tmux")
        .args(["list-panes", "-t", session_name, "-F", "#{pane_index}"])
        .output()
        .expect("list panes");
    let pane_count = String::from_utf8_lossy(&output.stdout).lines().count();
    assert_eq!(pane_count, 5, "session should have 5 panes");

    // Verify each pane's title has the correct branch→CLI pairing
    let output = std::process::Command::new("tmux")
        .args(["list-panes", "-t", session_name, "-F", "#{pane_title}"])
        .output()
        .expect("list pane titles");
    let titles: Vec<String> = String::from_utf8_lossy(&output.stdout)
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();

    assert_eq!(titles.len(), 5, "should have 5 pane titles");
    for (i, (branch, cli)) in panes.iter().enumerate() {
        assert!(
            titles[i].contains(branch) && titles[i].contains(cli),
            "pane {i} should map {branch} to {cli}, got: {}",
            titles[i]
        );
    }

    cleanup_session(session_name);
}

#[test]
#[serial]
fn tmux_mouse_mode_enabled_by_default() {
    ensure_tmux_installed().expect("tmux must be installed to run this test");

    let session_name = "paw-e2e-mouse-test";
    cleanup_session(session_name);

    let tmp = TempDir::new().expect("create temp dir");
    let worktree = tmp.path().to_string_lossy().to_string();

    let session = TmuxSessionBuilder::new("e2e-mouse-test")
        .add_pane(PaneSpec {
            branch: "feat/test".into(),
            worktree,
            cli_command: "echo hi".into(),
        })
        .build()
        .expect("build session");

    session.execute().expect("execute session");

    let output = std::process::Command::new("tmux")
        .args(["show-option", "-t", session_name, "mouse"])
        .output()
        .expect("show mouse option");
    let mouse_setting = String::from_utf8_lossy(&output.stdout);
    assert!(
        mouse_setting.contains("on"),
        "mouse should be enabled by default, got: {mouse_setting}"
    );

    cleanup_session(session_name);
}

#[test]
#[serial]
fn tmux_is_session_alive_returns_false_for_nonexistent() {
    ensure_tmux_installed().expect("tmux must be installed to run this test");

    let alive = is_session_alive("paw-definitely-does-not-exist-xyz").expect("check session");
    assert!(!alive);
}

// ---------------------------------------------------------------------------
// Exit code verification
// ---------------------------------------------------------------------------

#[test]
fn error_exit_code_is_1_for_not_a_git_repo() {
    let tmp = TempDir::new().expect("create temp dir");

    cmd()
        .current_dir(tmp.path())
        .arg("start")
        .assert()
        .code(1)
        .stderr(predicate::str::contains("Not a git repository"));
}

#[test]
fn error_exit_code_is_1_for_preset_not_found() {
    let tr = setup_test_repo();

    cmd()
        .current_dir(tr.path())
        .args(["start", "--preset", "nonexistent"])
        .assert()
        .code(1)
        .stderr(predicate::str::contains("not found"));
}

// ---------------------------------------------------------------------------
// tmux::attach
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn attach_fails_for_nonexistent_session() {
    ensure_tmux_installed().expect("tmux must be installed to run this test");

    let result = attach("paw-e2e-attach-nonexistent-xyz");
    assert!(result.is_err(), "attach to nonexistent session should fail");
}

// ---------------------------------------------------------------------------
// Replay — ANSI stripping
// ---------------------------------------------------------------------------

#[test]
fn replay_strips_ansi_from_log() {
    let tr = setup_test_repo();

    // Create a log file with ANSI escape codes
    let log_dir = tr.path().join(".git-paw/logs/paw-test");
    fs::create_dir_all(&log_dir).expect("create log dir");
    fs::write(log_dir.join("main.log"), "\x1b[31mred\x1b[0m plain").expect("write log with ANSI");

    cmd()
        .current_dir(tr.path())
        .args(["replay", "main", "--session", "paw-test"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("red plain")
                .and(predicate::str::contains("\x1b[31m").not())
                .and(predicate::str::contains("\x1b[0m").not()),
        );
}

// ---------------------------------------------------------------------------
// Replay — list shows sessions and branches
// ---------------------------------------------------------------------------

#[test]
fn replay_list_shows_sessions_and_branches() {
    let tr = setup_test_repo();

    // Create log files for a session with two branches
    let log_dir = tr.path().join(".git-paw/logs/paw-test");
    fs::create_dir_all(&log_dir).expect("create log dir");
    fs::write(log_dir.join("feat--auth.log"), "auth log").expect("write auth log");
    fs::write(log_dir.join("main.log"), "main log").expect("write main log");

    cmd()
        .current_dir(tr.path())
        .args(["replay", "--list"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("paw-test")
                .and(predicate::str::contains("2 branches"))
                .and(predicate::str::contains("feat/auth"))
                .and(predicate::str::contains("main")),
        );
}

// ---------------------------------------------------------------------------
// From-specs — markdown format dry run
// ---------------------------------------------------------------------------

#[test]
fn from_specs_markdown_format_dry_run() {
    let tr = setup_test_repo();

    // Create config with [specs] section using markdown type
    let paw_dir = tr.path().join(".git-paw");
    fs::create_dir_all(&paw_dir).expect("create .git-paw");
    fs::write(
        paw_dir.join("config.toml"),
        "[specs]\ndir = \"specs\"\ntype = \"markdown\"\n\n[clis.echo]\ncommand = \"/bin/echo\"\n",
    )
    .expect("write config");

    // Create a markdown spec file with paw_status: pending frontmatter
    let specs_dir = tr.path().join("specs");
    fs::create_dir_all(&specs_dir).expect("create specs dir");
    fs::write(
        specs_dir.join("add-auth.md"),
        "---\npaw_status: pending\n---\nImplement authentication\n",
    )
    .expect("write spec");

    cmd()
        .current_dir(tr.path())
        .args(["start", "--from-specs", "--dry-run", "--cli", "echo"])
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Dry run")
                .and(predicate::str::contains("add-auth"))
                .and(predicate::str::contains("echo")),
        );
}

// ---------------------------------------------------------------------------
// From-specs — skips done markdown specs
// ---------------------------------------------------------------------------

#[test]
fn from_specs_skips_done_markdown_specs() {
    let tr = setup_test_repo();

    // Create config with [specs] section using markdown type
    let paw_dir = tr.path().join(".git-paw");
    fs::create_dir_all(&paw_dir).expect("create .git-paw");
    fs::write(
        paw_dir.join("config.toml"),
        "[specs]\ndir = \"specs\"\ntype = \"markdown\"\n\n[clis.echo]\ncommand = \"/bin/echo\"\n",
    )
    .expect("write config");

    // Create a markdown spec file with paw_status: done (not pending)
    let specs_dir = tr.path().join("specs");
    fs::create_dir_all(&specs_dir).expect("create specs dir");
    fs::write(
        specs_dir.join("add-auth.md"),
        "---\npaw_status: done\n---\nAlready implemented\n",
    )
    .expect("write spec");

    cmd()
        .current_dir(tr.path())
        .args(["start", "--from-specs"])
        .assert()
        .success()
        .stdout(predicate::str::contains("No pending specs"));
}

#[test]
#[serial]
fn attach_succeeds_for_live_session() {
    ensure_tmux_installed().expect("tmux must be installed to run this test");

    let session_name = "paw-e2e-attach-test";
    cleanup_session(session_name);

    // Create a detached session
    let session = TmuxSessionBuilder::new("e2e-attach-test")
        .add_pane(PaneSpec {
            branch: "main".into(),
            worktree: "/tmp".into(),
            cli_command: "echo attached".into(),
        })
        .build()
        .expect("build session");
    session.execute().expect("execute session");

    // Attach in a subprocess with a timeout — it will block until detached,
    // so we detach it programmatically from another thread.
    let name = session_name.to_string();
    let detacher = std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_millis(500));
        // Detach all clients from the session
        let _ = std::process::Command::new("tmux")
            .args(["detach-client", "-s", &name])
            .output();
    });

    // attach() blocks until the client is detached
    let result = attach(session_name);
    detacher.join().expect("detacher thread");

    // attach returns Ok if detach was clean, Err if session vanished.
    // Either way, we exercised the success path through the blocking call.
    // On CI without a pty, attach may fail — that's acceptable.
    if result.is_ok() {
        // Success path exercised
    } else {
        // No pty available (headless CI) — attach can't connect a client.
        // The failure is from tmux itself, not our code.
        eprintln!("note: attach returned error (expected in headless environments)");
    }

    cleanup_session(session_name);
}