vpf-runner 0.4.1

The reference runner of the Verifiable Plan Format (VPF): runs a plan step by step and proves each step.
Documentation
//! An agent that ends with an error fails only the work it was given: that work is refused with the error, and the
//! rest of the run goes on.

mod common;

use std::sync::Mutex;

use vpf_runner::{AgentAnswer, AgentRequest, Outcome, Runner};

use common::{Scripted, plan};

/// An agent that ends with an error for the work of the step `failing`, the first `times` times it is asked it,
/// and does everything else.
struct Failing {
    times: Mutex<u32>,
}

impl Failing {
    fn new(times: u32) -> Self {
        Self {
            times: Mutex::new(times),
        }
    }
}

impl vpf_runner::Agent for Failing {
    fn ask(&self, request: &AgentRequest<'_>) -> Result<AgentAnswer, String> {
        if request.prompt.contains("Answer PASS or FAIL") {
            return Ok(AgentAnswer::new("PASS".to_owned()).with_cost(0.0));
        }
        if request.prompt.contains("Break.") {
            let mut times = self.times.lock().expect("the count");
            if *times > 0 {
                *times -= 1;
                return Err("the agent's command ended with the code 2".to_owned());
            }
        }
        Ok(AgentAnswer::new("done".to_owned()).with_cost(0.0))
    }
}

const TWO: &str = r#"{ "$schema": "vpf/1.1", "wish": "Two pieces",
     "steps": [
       { "id": "failing", "do": "Break.", "by": "agent", "proof": { "by": "agent", "check": "It is done." } },
       { "id": "other", "do": "Other.", "by": "agent", "proof": { "by": "agent", "check": "It is done." },
         "after": [] }
     ] }"#;

#[test]
fn an_agent_error_is_a_refusal_and_the_work_is_done_again() {
    let folder = tempfile::tempdir().expect("a folder");
    let (agent, mut person) = (Failing::new(1), Scripted::default());
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan(TWO), "");

    assert_eq!(record.outcome, Outcome::Done, "{}", person.all());
    let failing = &record.steps[0];
    assert_eq!(failing.attempts.len(), 2);
    assert_eq!(failing.answer.as_deref(), Some("done"));
    assert!(
        person.all().contains(
            "Refused: The agent ended with an error: the agent's command ended with the code 2"
        ),
        "{}",
        person.all()
    );
    assert_eq!(record.steps[1].answer.as_deref(), Some("done"));
}

#[test]
fn an_agent_that_keeps_failing_asks_the_person_while_the_other_steps_go_on() {
    let folder = tempfile::tempdir().expect("a folder");
    let (agent, mut person) = (Failing::new(3), Scripted::new(&["yes"]));
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan(TWO), "");

    assert_eq!(record.outcome, Outcome::Done, "{}", person.all());
    assert_eq!(record.steps[1].answer.as_deref(), Some("done"));
    assert_eq!(record.steps[0].attempts.len(), 4);
    assert!(
        person
            .all()
            .contains("The step failing was refused 3 times"),
        "{}",
        person.all()
    );
}

#[test]
fn each_agent_error_is_kept_and_told_once() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(&TWO.replace(
        r#""wish": "Two pieces","#,
        r#""wish": "Two pieces", "limits": { "repeats": 2 },"#,
    ));
    let (agent, mut person) = (Failing::new(3), Scripted::new(&["yes"]));
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done, "{}", person.all());
    let attempts = &record.steps[0].attempts;
    assert_eq!(attempts.len(), 4);
    let failed: Vec<_> = attempts
        .iter()
        .filter_map(|attempt| attempt.failed.as_deref())
        .collect();
    assert_eq!(
        failed,
        ["The agent ended with an error: the agent's command ended with the code 2"; 3]
    );
    assert_eq!(
        person
            .all()
            .matches("Refused: The agent ended with an error")
            .count(),
        3,
        "{}",
        person.all()
    );
    assert!(person.all().contains("refused 2 times"), "{}", person.all());
}