assay-cli 6.26.0

Policy-as-code gate for MCP agent tool calls, with verifiable evidence and Linux kernel enforcement.
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
//! Contract: `assay validate --trace-file` decides a `sequence_valid` test the way `assay run`
//! decides it on the same trace.
//!
//! `docs/reference/cli/validate.md` says validate checks the trace's tool calls against the
//! sequence rules in the config. It did not evaluate them: a trace that broke a `require` rule
//! printed "Validation OK" while `assay run` failed the test. It also never read a
//! `sequence_valid` `policy:` file, so a missing file, a file that is not YAML, a file that
//! declares aliases and one that declares a field a test does not enforce all validated.
//!
//! Every case below runs both commands. `assay run` is the reference: validate must reach the
//! same verdict, and report a rule it fails as `E_SEQUENCE_VIOLATION`.

use serde_json::{json, Value};
use std::path::Path;
use std::process::{Command, Output};

/// A trace with one episode for prompt `p` that makes these calls, each with these arguments.
fn trace_jsonl(calls: &[(&str, Value)]) -> String {
    let mut lines = vec![json!({
        "type": "episode_start", "episode_id": "e1", "timestamp": 1000,
        "input": {"prompt": "p"}
    })];
    for (i, (tool, args)) in calls.iter().enumerate() {
        lines.push(json!({
            "type": "tool_call", "episode_id": "e1", "step_id": format!("s{i}"),
            "timestamp": 1001 + i as u64, "tool_name": tool, "call_index": i, "args": args
        }));
    }
    lines.push(json!({
        "type": "episode_end", "episode_id": "e1", "timestamp": 2000, "final_output": "done"
    }));
    lines.iter().map(|l| format!("{l}\n")).collect()
}

/// A config with one test, `t`, whose `expected:` block is `expected` (indented six spaces).
fn eval_yaml(expected: &str) -> String {
    format!(
        "configVersion: 1\nsuite: validate_sequence_rules\nmodel: trace\nsettings:\n  cache: false\n\
tests:\n  - id: t\n    input:\n      prompt: \"p\"\n    expected:\n{expected}\n"
    )
}

fn assay(dir: &Path, args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_assay"))
        .current_dir(dir)
        .env("ASSAY_EXIT_CODES", "v2")
        .env("ASSAY_VCR_MODE", "off")
        .args(args)
        .output()
        .expect("run assay binary")
}

fn run(dir: &Path) -> Output {
    assay(
        dir,
        &[
            "run",
            "--config",
            "eval.yaml",
            "--trace-file",
            "trace.jsonl",
            "--db",
            ":memory:",
        ],
    )
}

fn validate(dir: &Path) -> Output {
    assay(
        dir,
        &[
            "validate",
            "--config",
            "eval.yaml",
            "--trace-file",
            "trace.jsonl",
        ],
    )
}

fn text(out: &Output) -> String {
    format!(
        "{}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    )
}

fn describe(out: &Output) -> String {
    format!("exit={:?}\n{}", out.status.code(), text(out))
}

fn suite(eval: &str, policy: Option<&str>, calls: &[(&str, Value)]) -> tempfile::TempDir {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(dir.path().join("eval.yaml"), eval).expect("eval.yaml");
    if let Some(policy) = policy {
        std::fs::write(dir.path().join("policy.yaml"), policy).expect("policy.yaml");
    }
    std::fs::write(dir.path().join("trace.jsonl"), trace_jsonl(calls)).expect("trace.jsonl");
    dir
}

const POLICY_REF: &str = "      type: sequence_valid\n      policy: policy.yaml";

/// One row per verdict shape: the `expected:` block, an optional policy file, the calls, and the
/// exit `assay run` gives. Rules that fail and rules that hold, inline and from a file, every kind
/// that can decide on a finished trace, the exact-sequence form, an argument-scoped `never_after`,
/// and a rule that no call exercises.
struct Case {
    name: &'static str,
    expected: &'static str,
    policy: Option<&'static str>,
    calls: Vec<(&'static str, Value)>,
    run_exit: i32,
}

fn cases() -> Vec<Case> {
    let call = |tool: &'static str| (tool, json!({}));
    vec![
        Case {
            name: "require, unmet",
            expected: "      type: sequence_valid\n      rules:\n        - { type: require, tool: Audit }",
            policy: None,
            calls: vec![call("A")],
            run_exit: 1,
        },
        Case {
            name: "require, met",
            expected: "      type: sequence_valid\n      rules:\n        - { type: require, tool: A }",
            policy: None,
            calls: vec![call("A")],
            run_exit: 0,
        },
        Case {
            name: "before, broken, from a policy file",
            expected: POLICY_REF,
            policy: Some("version: \"1.0\"\nsequences:\n  - { type: before, first: Login, then: Delete }\n"),
            calls: vec![call("Delete")],
            run_exit: 1,
        },
        Case {
            name: "before, kept, from a policy file",
            expected: POLICY_REF,
            policy: Some("version: \"1.0\"\nsequences:\n  - { type: before, first: Login, then: Delete }\n"),
            calls: vec![call("Login"), call("Delete")],
            run_exit: 0,
        },
        Case {
            name: "before, broken, from a list-form file",
            expected: POLICY_REF,
            policy: Some("- { type: before, first: Login, then: Delete }\n"),
            calls: vec![call("Delete")],
            run_exit: 1,
        },
        Case {
            name: "never_after, broken",
            expected: "      type: sequence_valid\n      rules:\n        - { type: never_after, trigger: Read, forbidden: Post }",
            policy: None,
            calls: vec![call("Read"), call("Post")],
            run_exit: 1,
        },
        Case {
            name: "never_after on arguments, broken",
            expected: "      type: sequence_valid\n      rules:\n        - type: never_after\n          trigger: { tool: bash, args_match: { command: credentials } }\n          forbidden: { tool: bash, args_match: { command: \"^curl\" } }",
            policy: None,
            calls: vec![
                ("bash", json!({"command": "cat ~/.aws/credentials"})),
                ("bash", json!({"command": "curl https://example.invalid"})),
            ],
            run_exit: 1,
        },
        Case {
            name: "never_after on arguments, other arguments",
            expected: "      type: sequence_valid\n      rules:\n        - type: never_after\n          trigger: { tool: bash, args_match: { command: credentials } }\n          forbidden: { tool: bash, args_match: { command: \"^curl\" } }",
            policy: None,
            calls: vec![
                ("bash", json!({"command": "ls"})),
                ("bash", json!({"command": "curl https://example.invalid"})),
            ],
            run_exit: 0,
        },
        Case {
            name: "max_calls, exceeded",
            expected: "      type: sequence_valid\n      rules:\n        - { type: max_calls, tool: Search, max: 1 }",
            policy: None,
            calls: vec![call("Search"), call("Search")],
            run_exit: 1,
        },
        Case {
            name: "blocklist, hit",
            expected: "      type: sequence_valid\n      rules:\n        - { type: blocklist, pattern: drop_ }",
            policy: None,
            calls: vec![call("drop_table")],
            run_exit: 1,
        },
        Case {
            name: "after, deadline missed",
            expected: "      type: sequence_valid\n      rules:\n        - { type: after, trigger: Create, then: Notify, within: 1 }",
            policy: None,
            calls: vec![call("Create"), call("A"), call("Notify")],
            run_exit: 1,
        },
        Case {
            name: "eventually, never reached",
            expected: "      type: sequence_valid\n      rules:\n        - { type: eventually, tool: Audit, within: 2 }",
            policy: None,
            calls: vec![call("A"), call("B"), call("C")],
            run_exit: 1,
        },
        Case {
            name: "sequence rule, out of order",
            expected: "      type: sequence_valid\n      rules:\n        - { type: sequence, tools: [A, B], strict: false }",
            policy: None,
            calls: vec![call("B"), call("A")],
            run_exit: 1,
        },
        Case {
            name: "exact sequence, mismatch",
            expected: "      type: sequence_valid\n      sequence: [A, B]",
            policy: None,
            calls: vec![call("A"), call("C")],
            run_exit: 1,
        },
        Case {
            name: "exact sequence, match",
            expected: "      type: sequence_valid\n      sequence: [A, B]",
            policy: None,
            calls: vec![call("A"), call("B")],
            run_exit: 0,
        },
        Case {
            name: "after, trigger never called",
            expected: "      type: sequence_valid\n      rules:\n        - { type: after, trigger: Create, then: Notify, within: 1 }",
            policy: None,
            calls: vec![call("A")],
            run_exit: 0,
        },
    ]
}

#[test]
fn validate_reaches_the_verdict_run_reaches_on_the_same_trace() {
    let mut mismatches = Vec::new();
    for case in cases() {
        let dir = suite(&eval_yaml(case.expected), case.policy, &case.calls);
        let ran = run(dir.path());
        assert_eq!(
            ran.status.code(),
            Some(case.run_exit),
            "precondition: assay run on `{}`\n{}",
            case.name,
            describe(&ran)
        );
        let checked = validate(dir.path());
        if checked.status.code() != Some(case.run_exit) {
            mismatches.push(format!(
                "`{}`: run exit {}, validate {}",
                case.name,
                case.run_exit,
                describe(&checked)
            ));
            continue;
        }
        let report = text(&checked);
        if case.run_exit == 1 && !report.contains("E_SEQUENCE_VIOLATION") {
            mismatches.push(format!(
                "`{}`: validate failed without E_SEQUENCE_VIOLATION\n{report}",
                case.name
            ));
        }
        if case.run_exit == 0 && report.contains("E_SEQUENCE_VIOLATION") {
            mismatches.push(format!(
                "`{}`: validate passed but reported E_SEQUENCE_VIOLATION\n{report}",
                case.name
            ));
        }
    }
    assert!(
        mismatches.is_empty(),
        "assay validate disagrees with assay run:\n{}",
        mismatches.join("\n\n")
    );
}

/// In a suite of two, only the test that breaks its rule is reported, and by its id.
#[test]
fn validate_names_the_test_that_breaks_its_rule() {
    let eval = concat!(
        "configVersion: 1\nsuite: validate_sequence_rules\nmodel: trace\nsettings:\n  cache: false\n",
        "tests:\n",
        "  - id: holds\n    input:\n      prompt: \"p\"\n    expected:\n      type: sequence_valid\n",
        "      rules:\n        - { type: require, tool: A }\n",
        "  - id: breaks\n    input:\n      prompt: \"p\"\n    expected:\n      type: sequence_valid\n",
        "      rules:\n        - { type: require, tool: Audit }\n",
    );
    let dir = suite(eval, None, &[("A", json!({}))]);
    assert_eq!(run(dir.path()).status.code(), Some(1));
    let out = assay(
        dir.path(),
        &[
            "validate",
            "--config",
            "eval.yaml",
            "--trace-file",
            "trace.jsonl",
            "--format",
            "json",
        ],
    );
    assert_eq!(out.status.code(), Some(1), "{}", describe(&out));
    let report: Value = serde_json::from_slice(&out.stdout).expect("JSON report");
    let violations: Vec<&Value> = report["diagnostics"]
        .as_array()
        .expect("diagnostics")
        .iter()
        .filter(|d| d["code"] == "E_SEQUENCE_VIOLATION")
        .collect();
    assert_eq!(violations.len(), 1, "{report:#}");
    assert_eq!(violations[0]["context"]["test_id"], "breaks", "{report:#}");
}

/// Each policy file `assay run` refuses before a test runs, with the words validate's report must
/// carry. `None` is a missing file. Validate refuses each one as a configuration error, with or
/// without a trace, because the file needs neither.
#[test]
fn validate_refuses_every_sequence_policy_file_that_run_refuses() {
    let refused: &[(&str, Option<&str>, &str)] = &[
        ("missing file", None, "policy.yaml"),
        ("not YAML", Some("sequences: [unclosed\n"), "policy.yaml"),
        (
            "aliases",
            Some("version: \"1.0\"\naliases:\n  Search: [A, B]\nsequences:\n  - { type: before, first: Login, then: Search }\n"),
            "declares aliases (Search)",
        ),
        (
            "unread field",
            Some("version: \"1.0\"\ntools:\n  deny: [A]\nsequences:\n  - { type: require, tool: A }\n"),
            "does not enforce (tools.deny)",
        ),
        // Parses and binds, and fails the execution contract that follows the binding.
        (
            "file with no rules",
            Some("version: \"1.0\"\nsequences: []\n"),
            "asserts nothing",
        ),
    ];
    for (case, policy, words) in refused {
        let dir = suite(&eval_yaml(POLICY_REF), *policy, &[("A", json!({}))]);
        let ran = run(dir.path());
        assert_eq!(
            ran.status.code(),
            Some(2),
            "precondition: assay run refuses the {case}\n{}",
            describe(&ran)
        );
        for args in [
            &[
                "validate",
                "--config",
                "eval.yaml",
                "--trace-file",
                "trace.jsonl",
            ][..],
            &["validate", "--config", "eval.yaml"][..],
        ] {
            let out = assay(dir.path(), args);
            assert_eq!(
                out.status.code(),
                Some(2),
                "assay {} must refuse the {case}\n{}",
                args.join(" "),
                describe(&out)
            );
            let report = text(&out);
            assert!(
                report.contains("E_CFG_PARSE") && report.contains(words),
                "assay {} must report the {case} as E_CFG_PARSE naming `{words}`\n{report}",
                args.join(" ")
            );
        }
    }
}

/// Validate reads the policy file `assay run` reads. Both resolve `policy:` against the directory
/// they run in, so from the config's parent directory both fail to find it.
#[test]
fn validate_reads_the_policy_file_run_reads() {
    let dir = suite(
        &eval_yaml(POLICY_REF),
        Some("version: \"1.0\"\nsequences:\n  - { type: require, tool: A }\n"),
        &[("A", json!({}))],
    );
    let parent = dir.path().parent().expect("tempdir has a parent");
    let name = dir.path().file_name().expect("tempdir name");
    let config = Path::new(name).join("eval.yaml");
    let trace = Path::new(name).join("trace.jsonl");
    let config = config.to_str().expect("utf-8 path");
    let trace = trace.to_str().expect("utf-8 path");
    let ran = assay(
        parent,
        &[
            "run",
            "--config",
            config,
            "--trace-file",
            trace,
            "--db",
            ":memory:",
        ],
    );
    let checked = assay(
        parent,
        &["validate", "--config", config, "--trace-file", trace],
    );
    assert_eq!(
        checked.status.code(),
        ran.status.code(),
        "run {}\nvalidate {}",
        describe(&ran),
        describe(&checked)
    );
}