pitboss 0.3.0

CLI that orchestrates coding agents (Claude Code and others) through a phased implementation plan, with automatic test/commit loops and a TUI dashboard
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
//! In-memory [`Git`] implementation used by runner tests.
//!
//! `MockGit` synthesizes the slice of git state the runner cares about:
//! a current branch, a set of branches that exist, a "working tree" of
//! pseudo-modified paths, an index, and a commit log. Tests drive the working
//! tree directly with [`MockGit::touch`] / [`MockGit::clear`]; every trait
//! call appends to a journal so tests can assert exactly which operations the
//! runner performed and in what order.
//!
//! `MockGit` is **always** compiled, not gated behind `#[cfg(test)]`, because
//! integration tests under `tests/` consume it as a regular dependency.

use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use anyhow::{anyhow, Result};
use async_trait::async_trait;

use super::{CommitId, DiffStat, Git};

/// One entry in the [`MockGit`] operation journal.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MockOp {
    /// `is_clean` was called.
    IsClean,
    /// `current_branch` was called.
    CurrentBranch,
    /// `create_branch(name)` was called.
    CreateBranch(String),
    /// `checkout(name)` was called.
    Checkout(String),
    /// `stage_changes(exclude)` was called; carries the exclusion set.
    StageChanges(Vec<PathBuf>),
    /// `has_staged_changes` was called.
    HasStagedChanges,
    /// `commit(message)` was called.
    Commit(String),
    /// `diff_stat(from, to)` was called.
    DiffStat(String, String),
    /// `staged_diff` was called.
    StagedDiff,
    /// `open_pr(title, body)` was called.
    OpenPr {
        /// PR title passed in.
        title: String,
        /// PR body passed in.
        body: String,
    },
    /// `stash_push(message)` was called.
    StashPush(String),
    /// `add_worktree(path, branch, base_branch)` was called.
    AddWorktree {
        /// Worktree path passed in.
        path: PathBuf,
        /// New branch the worktree checked out.
        branch: String,
        /// Base branch the new branch was created from.
        base_branch: String,
    },
    /// `remove_worktree(path)` was called.
    RemoveWorktree(PathBuf),
    /// `delete_branch(name)` was called.
    DeleteBranch(String),
    /// `merge_ff_only(source)` was called.
    MergeFfOnly(String),
}

/// Record of a single commit in [`MockGit`]'s in-memory log.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MockCommit {
    /// Synthesized commit hash (`mock0000...0001` style).
    pub id: CommitId,
    /// Branch the commit landed on.
    pub branch: String,
    /// Commit message verbatim.
    pub message: String,
    /// Files included in the commit, in sorted order.
    pub files: Vec<PathBuf>,
}

#[derive(Debug)]
struct MockState {
    current_branch: String,
    branches: HashSet<String>,
    working_tree: HashSet<PathBuf>,
    staged: HashSet<PathBuf>,
    commits: Vec<MockCommit>,
    ops: Vec<MockOp>,
    next_commit_seq: u64,
    /// Synthetic `git diff --cached` output. Tests set this to feed the runner
    /// auditor a representative diff; the mock has no real index/working tree
    /// to compute one from.
    staged_diff: String,
    /// Result the next `open_pr` call should produce. Defaults to a synthetic
    /// success URL so tests that don't care about the value still get a
    /// non-error return.
    open_pr_response: Result<String, String>,
}

impl MockState {
    fn new(branch: String) -> Self {
        let mut branches = HashSet::new();
        branches.insert(branch.clone());
        Self {
            current_branch: branch,
            branches,
            working_tree: HashSet::new(),
            staged: HashSet::new(),
            commits: Vec::new(),
            ops: Vec::new(),
            next_commit_seq: 0,
            staged_diff: String::new(),
            open_pr_response: Ok("https://github.com/example/repo/pull/1".to_string()),
        }
    }
}

/// In-memory test double for [`Git`]. Cheap to construct; thread-safe via
/// internal `Mutex`. See module docs for the expected usage pattern.
pub struct MockGit {
    state: Mutex<MockState>,
}

impl MockGit {
    /// New mock starting on a `main` branch with no working-tree changes.
    pub fn new() -> Self {
        Self::with_branch("main")
    }

    /// New mock starting on a custom initial branch.
    pub fn with_branch(branch: impl Into<String>) -> Self {
        Self {
            state: Mutex::new(MockState::new(branch.into())),
        }
    }

    /// Mark `path` as a working-tree change so the next `stage_changes` call
    /// will pick it up (modulo exclusions).
    pub fn touch(&self, path: impl Into<PathBuf>) {
        self.state.lock().unwrap().working_tree.insert(path.into());
    }

    /// Drop a path from the synthetic working tree, e.g., to simulate a user
    /// reverting an edit.
    pub fn clear(&self, path: impl AsRef<Path>) {
        self.state
            .lock()
            .unwrap()
            .working_tree
            .remove(path.as_ref());
    }

    /// Snapshot of every commit recorded so far, oldest first.
    pub fn commits(&self) -> Vec<MockCommit> {
        self.state.lock().unwrap().commits.clone()
    }

    /// Snapshot of the operation journal in call order.
    pub fn ops(&self) -> Vec<MockOp> {
        self.state.lock().unwrap().ops.clone()
    }

    /// Set the canned `git diff --cached` output the next [`Git::staged_diff`]
    /// call returns. Tests use this to drive the runner's audit branch with a
    /// representative diff string.
    pub fn set_staged_diff(&self, diff: impl Into<String>) {
        self.state.lock().unwrap().staged_diff = diff.into();
    }

    /// Make the next [`Git::open_pr`] call return `Ok(url)`.
    pub fn set_open_pr_response(&self, url: impl Into<String>) {
        self.state.lock().unwrap().open_pr_response = Ok(url.into());
    }

    /// Make the next [`Git::open_pr`] call fail with `message`.
    pub fn set_open_pr_failure(&self, message: impl Into<String>) {
        self.state.lock().unwrap().open_pr_response = Err(message.into());
    }

    /// Most recent exclusion set passed to `stage_changes`, or `None` if it
    /// was never called.
    pub fn last_exclusions(&self) -> Option<Vec<PathBuf>> {
        self.state
            .lock()
            .unwrap()
            .ops
            .iter()
            .rev()
            .find_map(|op| match op {
                MockOp::StageChanges(p) => Some(p.clone()),
                _ => None,
            })
    }
}

impl Default for MockGit {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Git for MockGit {
    async fn is_clean(&self) -> Result<bool> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::IsClean);
        Ok(s.working_tree.is_empty() && s.staged.is_empty())
    }

    async fn current_branch(&self) -> Result<String> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::CurrentBranch);
        Ok(s.current_branch.clone())
    }

    async fn create_branch(&self, name: &str) -> Result<()> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::CreateBranch(name.to_string()));
        if !s.branches.insert(name.to_string()) {
            return Err(anyhow!("mock-git: branch {name:?} already exists"));
        }
        Ok(())
    }

    async fn checkout(&self, name: &str) -> Result<()> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::Checkout(name.to_string()));
        if !s.branches.contains(name) {
            return Err(anyhow!("mock-git: cannot checkout unknown branch {name:?}"));
        }
        s.current_branch = name.to_string();
        Ok(())
    }

    async fn stage_changes(&self, exclude: &[&Path]) -> Result<()> {
        let exclude_paths: Vec<PathBuf> = exclude.iter().map(|p| p.to_path_buf()).collect();
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::StageChanges(exclude_paths.clone()));
        let exclude_set: HashSet<PathBuf> = exclude_paths.into_iter().collect();
        let to_stage: Vec<PathBuf> = s
            .working_tree
            .iter()
            .filter(|p| !is_excluded(p, &exclude_set))
            .cloned()
            .collect();
        for p in to_stage {
            s.working_tree.remove(&p);
            s.staged.insert(p);
        }
        Ok(())
    }

    async fn has_staged_changes(&self) -> Result<bool> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::HasStagedChanges);
        Ok(!s.staged.is_empty())
    }

    async fn commit(&self, message: &str) -> Result<CommitId> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::Commit(message.to_string()));
        if s.staged.is_empty() {
            return Err(anyhow!("mock-git: commit with empty index"));
        }
        s.next_commit_seq += 1;
        let id = CommitId::new(format!("mock{:040}", s.next_commit_seq));
        let mut files: Vec<PathBuf> = s.staged.drain().collect();
        files.sort();
        let branch = s.current_branch.clone();
        s.commits.push(MockCommit {
            id: id.clone(),
            branch,
            message: message.to_string(),
            files,
        });
        Ok(id)
    }

    async fn diff_stat(&self, from: &str, to: &str) -> Result<DiffStat> {
        let mut s = self.state.lock().unwrap();
        s.ops
            .push(MockOp::DiffStat(from.to_string(), to.to_string()));
        Ok(DiffStat::default())
    }

    async fn staged_diff(&self) -> Result<String> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::StagedDiff);
        Ok(s.staged_diff.clone())
    }

    async fn stash_push(&self, message: &str, exclude: &[&Path]) -> Result<bool> {
        let exclude_paths: Vec<PathBuf> = exclude.iter().map(|p| p.to_path_buf()).collect();
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::StashPush(message.to_string()));
        if s.working_tree.is_empty() && s.staged.is_empty() {
            return Ok(false);
        }
        let exclude_set: std::collections::HashSet<PathBuf> = exclude_paths.into_iter().collect();
        let mut moved = false;
        let to_clear: Vec<PathBuf> = s
            .working_tree
            .iter()
            .filter(|p| !is_excluded(p, &exclude_set))
            .cloned()
            .collect();
        for p in to_clear {
            s.working_tree.remove(&p);
            moved = true;
        }
        let staged_clear: Vec<PathBuf> = s
            .staged
            .iter()
            .filter(|p| !is_excluded(p, &exclude_set))
            .cloned()
            .collect();
        for p in staged_clear {
            s.staged.remove(&p);
            moved = true;
        }
        Ok(moved)
    }

    async fn add_worktree(&self, path: &Path, branch: &str, base_branch: &str) -> Result<()> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::AddWorktree {
            path: path.to_path_buf(),
            branch: branch.to_string(),
            base_branch: base_branch.to_string(),
        });
        if !s.branches.insert(branch.to_string()) {
            return Err(anyhow!("mock-git: branch {branch:?} already exists"));
        }
        Ok(())
    }

    async fn remove_worktree(&self, path: &Path) -> Result<()> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::RemoveWorktree(path.to_path_buf()));
        Ok(())
    }

    async fn delete_branch(&self, branch: &str) -> Result<()> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::DeleteBranch(branch.to_string()));
        s.branches.remove(branch);
        Ok(())
    }

    async fn merge_ff_only(&self, source_branch: &str) -> Result<()> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::MergeFfOnly(source_branch.to_string()));
        Ok(())
    }

    async fn open_pr(&self, title: &str, body: &str) -> Result<String> {
        let mut s = self.state.lock().unwrap();
        s.ops.push(MockOp::OpenPr {
            title: title.to_string(),
            body: body.to_string(),
        });
        match &s.open_pr_response {
            Ok(url) => Ok(url.clone()),
            Err(msg) => Err(anyhow!("mock-git: open_pr failed: {msg}")),
        }
    }
}

/// A path is excluded if it equals or is nested under any path in `exclude`.
/// Mirrors the behavior of `git add -- :!<path>` for directory exclusions.
fn is_excluded(path: &Path, exclude: &HashSet<PathBuf>) -> bool {
    exclude
        .iter()
        .any(|ex| path == ex.as_path() || path.starts_with(ex))
}

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

    #[tokio::test]
    async fn fresh_mock_is_clean_on_new_branch() {
        let git = MockGit::new();
        assert!(git.is_clean().await.unwrap());
        assert_eq!(git.current_branch().await.unwrap(), "main");
    }

    #[tokio::test]
    async fn create_then_checkout_switches_current_branch() {
        let git = MockGit::new();
        git.create_branch("pitboss/run-x").await.unwrap();
        assert_eq!(git.current_branch().await.unwrap(), "main");
        git.checkout("pitboss/run-x").await.unwrap();
        assert_eq!(git.current_branch().await.unwrap(), "pitboss/run-x");
    }

    #[tokio::test]
    async fn duplicate_branch_creation_errors() {
        let git = MockGit::new();
        git.create_branch("dup").await.unwrap();
        let err = git.create_branch("dup").await.unwrap_err();
        assert!(format!("{err}").contains("already exists"));
    }

    #[tokio::test]
    async fn checkout_unknown_branch_errors() {
        let git = MockGit::new();
        let err = git.checkout("missing").await.unwrap_err();
        assert!(format!("{err}").contains("unknown branch"));
    }

    #[tokio::test]
    async fn stage_changes_records_exclusions_and_filters_working_tree() {
        let git = MockGit::new();
        git.touch("src/foo.rs");
        git.touch(".pitboss/play/plan.md");
        git.touch(".pitboss/play/deferred.md");
        git.touch(".pitboss/play/state.json");

        let pitboss = Path::new(".pitboss");
        git.stage_changes(&[pitboss]).await.unwrap();

        let exclusions = git.last_exclusions().unwrap();
        assert_eq!(exclusions, vec![PathBuf::from(".pitboss")]);

        // Index should now hold only `src/foo.rs`.
        assert!(git.has_staged_changes().await.unwrap());
        let id = git.commit("[pitboss] phase 01: code only").await.unwrap();
        let commits = git.commits();
        assert_eq!(commits.len(), 1);
        assert_eq!(commits[0].id, id);
        assert_eq!(commits[0].files, vec![PathBuf::from("src/foo.rs")]);
        assert_eq!(commits[0].branch, "main");
        assert_eq!(commits[0].message, "[pitboss] phase 01: code only");
    }

    #[tokio::test]
    async fn empty_index_path_when_only_excluded_files_changed() {
        let git = MockGit::new();
        git.touch(".pitboss/play/plan.md");
        git.touch(".pitboss/play/state.json");
        git.stage_changes(&[Path::new(".pitboss")]).await.unwrap();
        assert!(!git.has_staged_changes().await.unwrap());
    }

    #[tokio::test]
    async fn commit_with_empty_index_errors() {
        let git = MockGit::new();
        let err = git.commit("nothing").await.unwrap_err();
        assert!(format!("{err}").contains("empty index"));
    }

    #[tokio::test]
    async fn ops_journal_records_each_call_in_order() {
        let git = MockGit::new();
        git.touch("src/foo.rs");
        git.is_clean().await.unwrap();
        git.create_branch("b").await.unwrap();
        git.checkout("b").await.unwrap();
        git.stage_changes(&[Path::new("plan.md")]).await.unwrap();
        git.has_staged_changes().await.unwrap();
        git.commit("msg").await.unwrap();
        git.diff_stat("a", "b").await.unwrap();

        let ops = git.ops();
        assert_eq!(
            ops,
            vec![
                MockOp::IsClean,
                MockOp::CreateBranch("b".into()),
                MockOp::Checkout("b".into()),
                MockOp::StageChanges(vec![PathBuf::from("plan.md")]),
                MockOp::HasStagedChanges,
                MockOp::Commit("msg".into()),
                MockOp::DiffStat("a".into(), "b".into()),
            ]
        );
    }

    #[tokio::test]
    async fn diff_stat_returns_default_on_mock() {
        let git = MockGit::new();
        let stat = git.diff_stat("x", "y").await.unwrap();
        assert_eq!(stat, DiffStat::default());
    }

    #[tokio::test]
    async fn open_pr_records_op_and_returns_canned_url() {
        let git = MockGit::new();
        // Default response is a synthetic URL.
        let url = git.open_pr("title", "body").await.unwrap();
        assert_eq!(url, "https://github.com/example/repo/pull/1");

        // Override the canned response.
        git.set_open_pr_response("https://github.com/example/repo/pull/77");
        let url = git.open_pr("t2", "b2").await.unwrap();
        assert_eq!(url, "https://github.com/example/repo/pull/77");

        let ops = git.ops();
        assert_eq!(
            ops,
            vec![
                MockOp::OpenPr {
                    title: "title".into(),
                    body: "body".into()
                },
                MockOp::OpenPr {
                    title: "t2".into(),
                    body: "b2".into()
                },
            ]
        );
    }

    #[tokio::test]
    async fn open_pr_failure_response_surfaces_error() {
        let git = MockGit::new();
        git.set_open_pr_failure("no remote");
        let err = git.open_pr("t", "b").await.unwrap_err();
        assert!(format!("{err}").contains("no remote"));
    }

    #[tokio::test]
    async fn staged_diff_returns_canned_text_and_records_op() {
        let git = MockGit::new();
        assert_eq!(git.staged_diff().await.unwrap(), "");
        git.set_staged_diff("diff --git a/foo b/foo\n+new line\n");
        let diff = git.staged_diff().await.unwrap();
        assert!(diff.contains("+new line"));
        let ops = git.ops();
        assert_eq!(
            ops,
            vec![MockOp::StagedDiff, MockOp::StagedDiff],
            "ops: {ops:?}"
        );
    }

    #[test]
    fn is_excluded_treats_directory_paths_as_prefixes() {
        let mut set = HashSet::new();
        set.insert(PathBuf::from(".pitboss"));
        assert!(is_excluded(Path::new(".pitboss"), &set));
        assert!(is_excluded(Path::new(".pitboss/state.json"), &set));
        assert!(is_excluded(Path::new(".pitboss/logs/x.log"), &set));
        assert!(!is_excluded(Path::new("src/foo.rs"), &set));
        assert!(!is_excluded(Path::new(".pitbossx"), &set));
    }
}