vpf-runner 0.4.0

The reference runner of the Verifiable Plan Format (VPF): runs a plan step by step and proves each step.
Documentation
//! What an agent is told about the work it is asked for, so that a host can show it while it goes on: the step, the
//! key its answer is kept under, the conversation the step's work went on in, what the run may still spend, and the
//! files the work changed.

mod common;

use std::fs;

use vpf_runner::{CommandAgent, Entry, Outcome, Runner};

use common::{Scripted, agent, plan, said};

#[test]
fn the_request_names_its_step_and_the_key_its_answer_is_kept_under() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A note",
             "steps": [ { "id": "note", "do": "Write a note.", "by": "agent", "proof": [ { "by": "algorithm", "run": "true" } ] } ] }"#,
    );
    let (agent, requests) = agent(|_, _| said("written"));
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    let requests = requests.borrow();
    assert_eq!(requests.len(), 1);
    assert_eq!(requests[0].step, "note");
    let kept = record
        .journal
        .iter()
        .find(|entry| entry.key == requests[0].key)
        .expect("the answer is kept under the request's key");
    assert_eq!(kept.value["text"], "written");
}

#[test]
fn refused_work_goes_on_in_the_conversation_it_was_done_in() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A note",
             "steps": [ { "id": "note", "do": "Write note.md.", "by": "agent",
                          "proof": [ { "by": "algorithm", "run": "grep -q second note.md" } ] } ] }"#,
    );
    let place = folder.path().to_owned();
    let (agent, requests) = agent(move |_, count| {
        let words = if count == 0 { "first" } else { "second" };
        fs::write(place.join("note.md"), words).expect("the note");
        said(words).in_conversation(&format!("talk-{count}"))
    });
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done);
    let requests = requests.borrow();
    assert_eq!(requests.len(), 2);
    assert_eq!(requests[0].conversation, None);
    assert_eq!(requests[1].conversation.as_deref(), Some("talk-0"));
    let attempts = &record.steps[0].attempts;
    assert_eq!(attempts[0].conversation.as_deref(), Some("talk-0"));
    assert_eq!(attempts[1].conversation.as_deref(), Some("talk-1"));
}

#[test]
fn the_agent_is_told_what_the_run_may_still_spend() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Two notes", "limits": { "cost": 10 },
             "steps": [ { "id": "one", "do": "Write one.", "by": "agent", "proof": [ { "by": "algorithm", "run": "true" } ] },
                        { "id": "two", "do": "Write two.", "by": "agent", "proof": [ { "by": "algorithm", "run": "true" } ] } ] }"#,
    );
    let (agent, requests) = agent(|_, _| said("written").with_cost(3.0));
    let mut person = Scripted::default();
    Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    let left: Vec<Option<f64>> = requests
        .borrow()
        .iter()
        .map(|request| request.cost_left)
        .collect();
    assert_eq!(left, [Some(10.0), Some(7.0)]);

    let free = common::plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A note",
             "steps": [ { "id": "one", "do": "Write one.", "by": "agent", "proof": [ { "by": "algorithm", "run": "true" } ] } ] }"#,
    );
    let (agent, requests) = common::agent(|_, _| said("written"));
    Runner::new(folder.path(), &agent, &mut person).run(&free, "");
    assert_eq!(requests.borrow()[0].cost_left, None);
}

#[test]
fn the_record_keeps_the_files_the_work_changed_and_a_run_rebuilt_keeps_them_too() {
    let folder = tempfile::tempdir().expect("a folder");
    fs::write(folder.path().join("old.md"), "old").expect("a file");
    fs::write(folder.path().join("gone.md"), "gone").expect("a file");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Notes",
             "steps": [ { "id": "notes", "do": "Write the notes.", "by": "agent", "proof": [ { "by": "algorithm", "run": "true" } ] } ] }"#,
    );
    let place = folder.path().to_owned();
    let (agent, _) = agent(move |_, _| {
        fs::write(place.join("new.md"), "new").expect("a file");
        fs::write(place.join("old.md"), "changed").expect("a file");
        fs::remove_file(place.join("gone.md")).expect("a file");
        said("written")
    });
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(
        record.steps[0].attempts[0].changed,
        ["gone.md", "new.md", "old.md"]
    );

    let mut rebuilt = record.clone();
    rebuilt.steps.clear();
    rebuilt.journal.clear();
    rebuilt.add_entries(record.journal.iter().cloned());
    let (again, requests) = common::agent(|_, _| said("asked again"));
    let resumed = Runner::new(folder.path(), &again, &mut person).resume(&rebuilt);
    assert!(requests.borrow().is_empty());
    assert_eq!(
        resumed.steps[0].attempts[0].changed,
        ["gone.md", "new.md", "old.md"]
    );
}

#[test]
fn an_agent_command_is_told_its_step_key_and_conversation_and_may_give_its_conversation() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A note", "limits": { "cost": 5 },
             "steps": [ { "id": "note", "do": "Write note.md.", "by": "agent",
                          "proof": [ { "by": "algorithm", "run": "test -f again" } ] } ] }"#,
    );
    let agent = CommandAgent::new(
        r#"echo "$VPF_STEP|$VPF_KEY|$VPF_CONVERSATION|$VPF_COST_LEFT" >> asked.txt
           if [ -f asked-once ]; then touch again; fi; touch asked-once
           echo "talk-7" > "$VPF_CONVERSATION_FILE"; echo 1 >> "$VPF_COST_FILE"; echo written"#,
    );
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done, "{}", person.all());
    let asked = fs::read_to_string(folder.path().join("asked.txt")).expect("what was asked");
    let lines: Vec<&str> = asked.lines().collect();
    let keys: Vec<&Entry> = record
        .journal
        .iter()
        .filter(|entry| entry.key.ends_with("/work"))
        .collect();
    assert_eq!(
        lines,
        [
            format!("note|{}||5", keys[0].key),
            format!("note|{}|talk-7|4", keys[1].key),
        ]
    );
    assert_eq!(
        record.steps[0].attempts[1].conversation.as_deref(),
        Some("talk-7")
    );
}