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
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
//! A run, step by step, with an agent and a person played by the tests.

mod common;

use std::fs;
use std::path::Path;
use std::time::Duration;

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

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

#[test]
fn commands_run_in_order_and_fill_references() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A greeting",
             "steps": [
               { "id": "name", "do": "Say the name.", "by": "algorithm", "run": "echo {{asked}}" },
               { "id": "greet", "do": "Greet {{name}}.", "by": "algorithm", "run": "echo hello {{ name }}" }
             ] }"#,
    );
    let (agent, mut person) = (no_agent(), Scripted::default());
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "Ana");

    assert_eq!(record.outcome, Outcome::Done);
    assert_eq!(record.steps[0].answer.as_deref(), Some("Ana"));
    assert_eq!(record.steps[1].answer.as_deref(), Some("hello Ana"));
    assert_eq!(
        record.steps[1].attempts[0].proofs[0].asked,
        "echo hello Ana"
    );
    assert!(record.steps[1].attempts[0].proofs[0].passed);
}

#[test]
fn refused_work_goes_back_with_the_points() {
    let folder = tempfile::tempdir().expect("a folder");
    let path = folder.path().to_owned();
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "The report, written",
             "steps": [
               { "id": "report", "do": "Write the report.", "by": "agent",
                 "proof": { "by": "algorithm", "needs": [{ "need": "files", "path": "report.md" }] } }
             ] }"#,
    );
    let (agent, requests) = agent(move |_, count| {
        if count == 1 {
            fs::write(path.join("report.md"), "# Report").expect("a file");
        }
        said(&format!("attempt {count}"))
    });
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done);
    assert_eq!(record.steps[0].attempts.len(), 2);
    assert_eq!(record.steps[0].answer.as_deref(), Some("attempt 1"));
    let requests = requests.borrow();
    assert_eq!(requests.len(), 2);
    let (role, first) = (&requests[0].role, &requests[0].prompt);
    assert_eq!(*role, Role::Work);
    assert!(first.contains("The wish: The report, written"), "{first}");
    assert!(first.contains("What will prove your work"), "{first}");
    assert!(first.contains("report.md"), "{first}");
    let second = &requests[1].prompt;
    assert!(second.contains("attempt 0"), "{second}");
    assert!(second.contains("report.md"), "{second}");
    assert!(second.contains("refused"), "{second}");
}

#[test]
fn another_agent_proves_and_only_reads() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A title",
             "steps": [
               { "id": "title", "do": "Write a title.", "by": "agent", "agent": "writer",
                 "proof": { "by": "agent", "agent": "editor", "check": "{{title}} is short." } }
             ] }"#,
    );
    let (agent, requests) = agent(|request, count| match (request.role, count) {
        (Role::Work, _) => said("A title"),
        (Role::Judge, 1) => said("FAIL\nIt is not catchy."),
        _ => said("PASS"),
    });
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done);
    let attempts = &record.steps[0].attempts;
    assert_eq!(attempts.len(), 2);
    assert_eq!(attempts[0].proofs[0].asked, "A title is short.");
    assert_eq!(
        attempts[0].proofs[0].points.as_deref(),
        Some("It is not catchy.")
    );
    let requests = requests.borrow();
    assert_eq!(requests[1].role, Role::Judge);
    assert!(
        requests[1].prompt.contains("PASS or FAIL"),
        "{}",
        requests[1].prompt
    );
    assert!(
        requests[2].prompt.contains("It is not catchy."),
        "{}",
        requests[2].prompt
    );
}

#[test]
fn a_proving_agent_that_changes_the_folder_stops_the_run() {
    let folder = tempfile::tempdir().expect("a folder");
    let path = folder.path().to_owned();
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A title",
             "steps": [
               { "id": "title", "do": "Write a title.", "by": "agent",
                 "proof": { "by": "agent", "check": "It is short." } }
             ] }"#,
    );
    let (agent, _) = agent(move |request, _| {
        if request.role == Role::Judge {
            assert_eq!(request.tools, ["read"]);
            fs::write(path.join("sneaky.txt"), "x").expect("a file");
        }
        said("PASS")
    });
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    let Outcome::Stopped(reason) = record.outcome else {
        panic!("the run went on");
    };
    assert!(reason.contains("changed the folder"), "{reason}");
}

#[test]
fn an_agent_without_write_may_not_change_the_folder() {
    let folder = tempfile::tempdir().expect("a folder");
    let path = folder.path().to_owned();
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A summary",
             "steps": [
               { "id": "sum", "do": "Sum up the notes.", "by": "agent", "tools": ["read", "internet"],
                 "proof": { "by": "person" } }
             ] }"#,
    );
    let (agent, _) = agent(move |request, _| {
        assert_eq!(request.tools, ["read", "internet"]);
        fs::write(path.join("notes.txt"), "x").expect("a file");
        said("A summary")
    });
    let mut person = Scripted::new(&["yes"]);
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    let Outcome::Stopped(reason) = record.outcome else {
        panic!("the run went on");
    };
    assert!(reason.contains("changed the folder"), "{reason}");
}

#[test]
fn after_the_repeats_the_person_decides() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Green tests", "limits": { "repeats": 2 },
             "steps": [ { "id": "test", "do": "Run the tests.", "by": "algorithm", "run": "echo red; exit 1" } ] }"#,
    );
    let (agent, mut person) = (no_agent(), Scripted::new(&["yes", "no"]));
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert!(matches!(record.outcome, Outcome::Stopped(_)));
    assert_eq!(record.steps[0].attempts.len(), 4);
    assert_eq!(
        record.steps[0].attempts[0].proofs[0].points.as_deref(),
        Some("red")
    );
    assert_eq!(record.decisions.len(), 2);
    assert!(record.decisions[0].asked.contains("refused 2 times"));
    assert_eq!(record.decisions[0].answer, "yes");
    assert_eq!(record.decisions[1].answer, "no");
}

#[test]
fn an_agent_behind_a_command_fixes_its_way() {
    let folder = tempfile::tempdir().expect("a folder");
    let path = folder.path().to_owned();
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Built",
             "steps": [
               { "id": "build", "do": "Build it.", "by": "algorithm",
                 "run": "test -f ready.txt && echo built || { echo ready.txt is missing; exit 1; }",
                 "on_fail": "agent",
                 "proof": { "by": "algorithm", "needs": [{ "need": "files", "path": "ready.txt" }] } }
             ] }"#,
    );
    let (agent, requests) = agent(move |request, _| {
        assert_eq!(request.role, Role::Fix);
        fs::write(path.join("ready.txt"), "x").expect("a file");
        said("I added ready.txt")
    });
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done);
    assert_eq!(record.steps[0].answer.as_deref(), Some("built"));
    let attempts = &record.steps[0].attempts;
    assert_eq!(attempts.len(), 2);
    assert_eq!(attempts[1].by, "agent");
    assert_eq!(attempts[1].proofs.len(), 2);
    assert!(attempts[1].proofs[0].asked.starts_with("test -f"));
    let requests = requests.borrow();
    assert_eq!(requests.len(), 1);
    assert!(
        requests[0].prompt.contains("ready.txt is missing"),
        "{}",
        requests[0].prompt
    );
}

#[test]
fn the_person_answers_and_approves() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A letter",
             "steps": [
               { "id": "to", "do": "Who is it for?", "by": "person" },
               { "id": "letter", "do": "Write a letter to {{to}}.", "by": "agent",
                 "proof": { "by": "person", "ask": "Send “{{letter}}”?" } }
             ] }"#,
    );
    let (agent, requests) = agent(|_, count| said(&format!("Dear Ana ({count})")));
    let mut person = Scripted::new(&["Ana", "no: too short", "yes"]);
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done);
    assert_eq!(record.steps[0].answer.as_deref(), Some("Ana"));
    assert!(
        requests.borrow()[0]
            .prompt
            .contains("Write a letter to Ana.")
    );
    assert!(requests.borrow()[1].prompt.contains("too short"));
    assert!(
        person
            .heard
            .iter()
            .any(|heard| heard.contains("Send “Dear Ana (0)”?"))
    );
    assert_eq!(record.steps[1].answer.as_deref(), Some("Dear Ana (1)"));
}

#[test]
fn a_person_who_does_not_answer_leaves_the_run_waiting() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "A name",
             "steps": [ { "id": "name", "do": "Say a name.", "by": "person" } ] }"#,
    );
    let (agent, mut person) = (no_agent(), Scripted::default());
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert!(matches!(record.outcome, Outcome::Waiting(_)));
}

#[test]
fn another_plan_runs_as_a_step() {
    let folder = tempfile::tempdir().expect("a folder");
    fs::write(
        folder.path().join("greet.vpf.json"),
        r#"{ "$schema": "vpf/1.1", "wish": "A greeting",
             "steps": [ { "id": "hello", "do": "Greet.", "by": "algorithm", "run": "echo hello {{asked}}" } ] }"#,
    )
    .expect("a plan");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Greeted",
             "steps": [
               { "id": "greet", "do": "Ana", "by": "plan", "plan": "greet" },
               { "id": "loud", "do": "Shout it.", "by": "algorithm", "run": "echo {{greet}}!" }
             ] }"#,
    );
    let (agent, mut person) = (no_agent(), Scripted::default());
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done);
    assert_eq!(record.steps[0].answer.as_deref(), Some("hello Ana"));
    assert_eq!(record.steps[1].answer.as_deref(), Some("hello Ana!"));
}

#[test]
fn a_plans_step_gives_the_other_plan_what_it_asks() {
    let folder = tempfile::tempdir().expect("a folder");
    fs::write(
        folder.path().join("greet.vpf.json"),
        r#"{ "$schema": "vpf/1.1", "wish": "A greeting",
             "steps": [ { "id": "hello", "do": "Greet.", "by": "algorithm", "run": "echo hello {{asked}}" } ] }"#,
    )
    .expect("a plan");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Greeted",
             "steps": [
               { "id": "pick", "do": "Pick a name.", "by": "algorithm", "run": "echo Ana" },
               { "id": "greet", "do": "Greet the one picked, {{pick}}.", "by": "plan", "plan": "greet",
                 "asked": "{{pick}}" }
             ] }"#,
    );
    let (agent, mut person) = (no_agent(), Scripted::default());
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done);
    assert_eq!(record.steps[1].answer.as_deref(), Some("hello Ana"));
}

#[test]
fn a_plans_step_gives_the_other_plan_named_values() {
    let folder = tempfile::tempdir().expect("a folder");
    fs::write(
        folder.path().join("greet.vpf.json"),
        r#"{ "$schema": "vpf/1.1", "wish": "A greeting",
             "steps": [ { "id": "hello", "do": "Greet.", "by": "algorithm",
                          "run": "echo '{{asked.name}} x{{asked.times}} {{asked.to.city}} {{asked}}'" } ] }"#,
    )
    .expect("a plan");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Greeted",
             "steps": [
               { "id": "pick", "do": "Pick a name.", "by": "algorithm", "run": "echo Ana" },
               { "id": "greet", "do": "Greet {{pick}}.", "by": "plan", "plan": "greet",
                 "asked": { "name": "{{pick}}", "times": 2, "to": { "city": "Rio of {{pick}}" } } }
             ] }"#,
    );
    let (agent, mut person) = (no_agent(), Scripted::default());
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done, "{}", person.all());
    assert_eq!(
        record.steps[1].answer.as_deref(),
        Some(r#"Ana x2 Rio of Ana {"name":"Ana","times":2,"to":{"city":"Rio of Ana"}}"#)
    );
}

#[test]
fn a_missing_or_looping_plan_stops_the_run() {
    let folder = tempfile::tempdir().expect("a folder");
    fs::write(
        folder.path().join("again.vpf.json"),
        r#"{ "$schema": "vpf/1.1", "wish": "Again",
             "steps": [ { "id": "again", "do": "Again.", "by": "plan", "plan": "again" } ] }"#,
    )
    .expect("a plan");
    for name in ["none", "again"] {
        let plan = plan(&format!(
            r#"{{ "$schema": "vpf/1.1", "wish": "W", "steps": [ {{ "id": "a", "do": "A.", "by": "plan", "plan": "{name}" }} ] }}"#
        ));
        let (agent, mut person) = (no_agent(), Scripted::default());
        let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");
        let Outcome::Stopped(reason) = record.outcome else {
            panic!("{name} ran");
        };
        assert!(reason.contains(name), "{reason}");
    }
}

#[test]
fn the_run_stops_at_its_time() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Slow",
             "steps": [ { "id": "wait", "do": "Wait.", "by": "algorithm", "run": "sleep 5" } ] }"#,
    );
    let (agent, mut person) = (no_agent(), Scripted::default());
    let mut runner = Runner::new(folder.path(), &agent, &mut person);
    runner.time_limit(Duration::from_millis(300));
    let started = std::time::Instant::now();
    let record = runner.run(&plan, "");

    assert!(started.elapsed() < Duration::from_secs(3));
    let Outcome::Stopped(reason) = record.outcome else {
        panic!("the run went on");
    };
    assert!(reason.contains("time"), "{reason}");
}

#[test]
fn the_run_stops_at_its_cost() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Cheap", "limits": { "cost": 1, "repeats": 9 },
             "steps": [ { "id": "a", "do": "A.", "by": "agent",
                          "proof": { "by": "algorithm", "run": "exit 1" } } ] }"#,
    );
    let (agent, requests) = agent(|_, _| AgentAnswer::new("a".to_owned()).with_cost(0.6));
    let mut person = Scripted::default();
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    let Outcome::Stopped(reason) = record.outcome else {
        panic!("the run went on");
    };
    assert!(reason.contains("cost"), "{reason}");
    assert_eq!(requests.borrow().len(), 2);
    assert!((record.cost - 1.2).abs() < 1e-9);
}

#[test]
fn an_unmeasured_cost_is_told() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Cheap", "limits": { "cost": 1 },
             "steps": [ { "id": "a", "do": "A.", "by": "agent", "proof": { "by": "person" } } ] }"#,
    );
    let (agent, _) = agent(|_, _| AgentAnswer::new("a".to_owned()));
    let mut person = Scripted::new(&["yes"]);
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(record.outcome, Outcome::Done);
    assert!(record.cost_unmeasured);
    assert!(person.heard.iter().any(|heard| heard.contains("cost")));
}

#[test]
fn records_are_kept_and_the_month_is_kept_to() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Monthly", "limits": { "month": 10 },
             "steps": [ { "id": "a", "do": "A.", "by": "agent", "proof": { "by": "person" } } ] }"#,
    );
    let run = |folder: &Path, person: &mut Scripted| {
        let (agent, _) = agent(|_, _| AgentAnswer::new("a".to_owned()).with_cost(5.0));
        let mut runner = Runner::new(folder, &agent, person);
        runner.keep_records("monthly");
        runner.run(&plan, "")
    };

    let mut person = Scripted::new(&["yes", "yes"]);
    assert_eq!(run(folder.path(), &mut person).outcome, Outcome::Done);
    assert_eq!(run(folder.path(), &mut person).outcome, Outcome::Done);
    assert!(
        person
            .heard
            .iter()
            .any(|heard| heard.contains("four fifths"))
    );
    let third = run(folder.path(), &mut person);
    let Outcome::Stopped(reason) = third.outcome else {
        panic!("a third run started");
    };
    assert!(reason.contains("month"), "{reason}");

    let records = fs::read_dir(folder.path().join(".vpf/runs")).expect("records");
    let records: Vec<_> = records.collect();
    assert_eq!(records.len(), 3);
}

#[test]
fn a_silent_failing_command_is_named_with_its_code() {
    let folder = tempfile::tempdir().expect("a folder");
    let plan = plan(
        r#"{ "$schema": "vpf/1.1", "wish": "Green tests", "limits": { "repeats": 1 },
             "steps": [
               { "id": "test", "do": "Run the tests.", "by": "algorithm", "run": "exit 3" }
             ] }"#,
    );
    let (agent, mut person) = (no_agent(), Scripted::new(&["no"]));
    let record = Runner::new(folder.path(), &agent, &mut person).run(&plan, "");

    assert_eq!(
        record.steps[0].attempts[0].proofs[0].points.as_deref(),
        Some("The command `exit 3` ended with the code 3 and wrote nothing.")
    );
}