blyte 0.90.0

The blyte coding-agent harness: headless runs with a sealed verification gate, and a REPL.
Documentation
use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

const PLAN_TEXT: &str = "\
# Sample Plan

### Task A: one

Depends on: none

Do task A.
";

fn git(dir: &Path, args: &[&str]) {
    let ok = Command::new("git")
        .args(["-c", "user.name=t", "-c", "user.email=t@t"])
        .args(args)
        .current_dir(dir)
        .status()
        .unwrap()
        .success();
    assert!(ok, "git {args:?}");
}

fn setup_repo() -> TempDir {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    git(root, &["init", "-q", "-b", "main"]);
    let plan_file = root.join("docs/plans/p.md");
    fs::create_dir_all(plan_file.parent().unwrap()).unwrap();
    fs::write(&plan_file, PLAN_TEXT).unwrap();
    git(root, &["add", "."]);
    git(root, &["commit", "-qm", "initial", "--no-gpg-sign"]);
    fs::create_dir_all(root.join(".wt")).unwrap();
    dir
}

fn write_start_and_accepted_verdict(root: &Path, run_id: &str, plan: &str) {
    let start = serde_json::json!({
        "id": run_id,
        "plan": plan,
        "plan_task": "Task A",
        "pid": 999999,
        "started_at": 100,
    });
    fs::write(
        root.join(".wt").join(format!("{run_id}.start.json")),
        serde_json::to_string_pretty(&start).unwrap(),
    )
    .unwrap();
    let verdict = serde_json::json!({
        "verdict": "accepted",
    });
    fs::write(
        root.join(".wt").join(format!("{run_id}.verdict.json")),
        serde_json::to_string_pretty(&verdict).unwrap(),
    )
    .unwrap();
}

#[test]
fn run_dispatched_from_a_removed_worktree_plan_is_matched() {
    let repo = setup_repo();
    let root = repo.path();
    write_start_and_accepted_verdict(root, "A", ".wt/plan-x/docs/plans/p.md");
    assert!(!root.join(".wt/plan-x").exists());

    let rows = blyte_cli::plan_cmd::statuses(root, "docs/plans/p.md", PLAN_TEXT).unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].state, "merged");
    assert_eq!(rows[0].run.as_deref(), Some("A"));
}

#[test]
fn run_dispatched_from_an_existing_worktree_plan_is_matched() {
    let repo = setup_repo();
    let root = repo.path();
    let wt_plan = root.join(".wt/plan-x/docs/plans/p.md");
    fs::create_dir_all(wt_plan.parent().unwrap()).unwrap();
    fs::write(&wt_plan, PLAN_TEXT).unwrap();
    write_start_and_accepted_verdict(root, "A", ".wt/plan-x/docs/plans/p.md");

    let rows = blyte_cli::plan_cmd::statuses(root, "docs/plans/p.md", PLAN_TEXT).unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].state, "merged");
    assert_eq!(rows[0].run.as_deref(), Some("A"));
}

#[test]
fn asking_with_the_worktree_path_matches_a_run_recorded_with_the_repository_path() {
    let repo = setup_repo();
    let root = repo.path();
    let wt_plan = root.join(".wt/plan-x/docs/plans/p.md");
    fs::create_dir_all(wt_plan.parent().unwrap()).unwrap();
    fs::write(&wt_plan, PLAN_TEXT).unwrap();
    write_start_and_accepted_verdict(root, "A", "docs/plans/p.md");

    let rows =
        blyte_cli::plan_cmd::statuses(root, ".wt/plan-x/docs/plans/p.md", PLAN_TEXT).unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].state, "merged");
    assert_eq!(rows[0].run.as_deref(), Some("A"));
}

#[test]
fn a_different_plan_in_a_worktree_is_not_matched() {
    let repo = setup_repo();
    let root = repo.path();
    write_start_and_accepted_verdict(root, "A", ".wt/plan-x/docs/plans/other.md");

    let rows = blyte_cli::plan_cmd::statuses(root, "docs/plans/p.md", PLAN_TEXT).unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].state, "ready");
    assert!(rows[0].run.is_none());
}

#[test]
fn absolute_worktree_plan_path_is_matched() {
    let repo = setup_repo();
    let root = repo.path();
    let abs_plan = root.join(".wt/plan-x/docs/plans/p.md");
    write_start_and_accepted_verdict(root, "A", abs_plan.to_str().unwrap());

    let rows = blyte_cli::plan_cmd::statuses(root, "docs/plans/p.md", PLAN_TEXT).unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].state, "merged");
    assert_eq!(rows[0].run.as_deref(), Some("A"));

    let canon_abs_plan = root
        .canonicalize()
        .unwrap()
        .join(".wt/plan-x/docs/plans/p.md");
    write_start_and_accepted_verdict(root, "A", canon_abs_plan.to_str().unwrap());

    let rows2 = blyte_cli::plan_cmd::statuses(root, "docs/plans/p.md", PLAN_TEXT).unwrap();
    assert_eq!(rows2.len(), 1);
    assert_eq!(rows2[0].state, "merged");
    assert_eq!(rows2[0].run.as_deref(), Some("A"));
}