assay-cli 6.7.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use super::*;
use clap::CommandFactory;
use clap::Parser;

#[test]
fn cli_debug_assert() {
    Cli::command().debug_assert();
}

#[test]
fn visible_top_level_commands_have_descriptions() {
    let missing: Vec<_> = Cli::command()
        .get_subcommands()
        .filter(|cmd| !cmd.is_hide_set())
        .filter(|cmd| cmd.get_about().is_none())
        .map(|cmd| cmd.get_name().to_string())
        .collect();

    assert!(
        missing.is_empty(),
        "visible top-level commands without descriptions: {}",
        missing.join(", ")
    );
}

#[test]
fn trust_card_command_accepts_canonical_and_legacy_names() {
    let canonical = Cli::try_parse_from([
        "assay",
        "trust-card",
        "generate",
        "bundle.tar.gz",
        "--out-dir",
        "trustcard",
    ])
    .expect("canonical trust-card command should parse");
    assert!(matches!(canonical.cmd, Command::TrustCard(_)));

    let legacy = Cli::try_parse_from([
        "assay",
        "trustcard",
        "generate",
        "bundle.tar.gz",
        "--out-dir",
        "trustcard",
    ])
    .expect("legacy trustcard alias should parse");
    assert!(matches!(legacy.cmd, Command::TrustCard(_)));
}

#[test]
fn mcp_group_accepts_canonical_paths_and_legacy_shims_are_removed() {
    let visible: Vec<_> = Cli::command()
        .get_subcommands()
        .filter(|cmd| !cmd.is_hide_set())
        .map(|cmd| cmd.get_name().to_string())
        .collect();

    assert!(visible.contains(&"mcp".to_string()));
    assert!(!visible.contains(&"discover".to_string()));
    assert!(!visible.contains(&"kill".to_string()));
    assert!(!visible.contains(&"tool".to_string()));

    let discover = Cli::try_parse_from(["assay", "mcp", "discover", "--format", "json"])
        .expect("canonical mcp discover command should parse");
    assert!(matches!(
        discover.cmd,
        Command::Mcp(McpArgs {
            cmd: McpSub::Discover(_)
        })
    ));

    let kill = Cli::try_parse_from(["assay", "mcp", "kill", "proc-123"])
        .expect("canonical mcp kill command should parse");
    assert!(matches!(
        kill.cmd,
        Command::Mcp(McpArgs {
            cmd: McpSub::Kill(_)
        })
    ));

    let tool = Cli::try_parse_from(["assay", "mcp", "tool", "keygen", "--out", "keys"])
        .expect("canonical mcp tool command should parse");
    assert!(matches!(
        tool.cmd,
        Command::Mcp(McpArgs {
            cmd: McpSub::Tool(_)
        })
    ));

    let preflight = Cli::try_parse_from(["assay", "mcp", "preflight"])
        .expect("canonical mcp preflight command should parse");
    assert!(matches!(
        preflight.cmd,
        Command::Mcp(McpArgs {
            cmd: McpSub::Preflight(_)
        })
    ));

    // The legacy top-level shims were retired; only the canonical `assay mcp ...` paths remain.
    assert!(Cli::try_parse_from(["assay", "discover", "--format", "json"]).is_err());
    assert!(Cli::try_parse_from(["assay", "kill", "proc-123"]).is_err());
    assert!(Cli::try_parse_from(["assay", "tool", "keygen", "--out", "keys"]).is_err());
}

#[test]
fn mcp_preflight_defaults_to_dot_root_and_terminal_or_json() {
    let defaults =
        Cli::try_parse_from(["assay", "mcp", "preflight"]).expect("bare preflight should parse");
    match defaults.cmd {
        Command::Mcp(McpArgs {
            cmd: McpSub::Preflight(args),
        }) => {
            assert_eq!(args.policy_root.as_os_str(), ".");
            assert_eq!(args.format, PreflightFormat::Terminal);
        }
        _ => panic!("expected mcp preflight"),
    }

    let terminal = Cli::try_parse_from([
        "assay",
        "mcp",
        "preflight",
        "--policy-root",
        ".",
        "--format",
        "terminal",
    ])
    .expect("--format terminal should parse");
    assert!(matches!(
        terminal.cmd,
        Command::Mcp(McpArgs {
            cmd: McpSub::Preflight(PreflightArgs {
                format: PreflightFormat::Terminal,
                ..
            })
        })
    ));

    let json = Cli::try_parse_from(["assay", "mcp", "preflight", "--format", "json"])
        .expect("--format json should parse");
    assert!(matches!(
        json.cmd,
        Command::Mcp(McpArgs {
            cmd: McpSub::Preflight(PreflightArgs {
                format: PreflightFormat::Json,
                ..
            })
        })
    ));

    assert!(
        Cli::try_parse_from(["assay", "mcp", "preflight", "--format", "text"]).is_err(),
        "preflight format is terminal|json, not the global text|json pair"
    );
}

#[test]
fn policy_group_accepts_authoring_paths_and_legacy_shims_are_removed() {
    let visible: Vec<_> = Cli::command()
        .get_subcommands()
        .filter(|cmd| !cmd.is_hide_set())
        .map(|cmd| cmd.get_name().to_string())
        .collect();

    assert!(visible.contains(&"policy".to_string()));
    assert!(!visible.contains(&"generate".to_string()));
    assert!(!visible.contains(&"record".to_string()));

    let generate = Cli::try_parse_from([
        "assay",
        "policy",
        "generate",
        "--input",
        "trace.jsonl",
        "--dry-run",
    ])
    .expect("canonical policy generate command should parse");
    assert!(matches!(
        generate.cmd,
        Command::Policy(PolicyArgs {
            cmd: PolicyCommand::Generate(_)
        })
    ));

    let record = Cli::try_parse_from(["assay", "policy", "record", "--", "echo", "hello"])
        .expect("canonical policy record command should parse");
    assert!(matches!(
        record.cmd,
        Command::Policy(PolicyArgs {
            cmd: PolicyCommand::Record(_)
        })
    ));

    // The legacy top-level shims were retired; only the canonical `assay policy ...` paths remain.
    assert!(
        Cli::try_parse_from(["assay", "generate", "--input", "trace.jsonl", "--dry-run"]).is_err()
    );
    assert!(Cli::try_parse_from(["assay", "record", "--", "echo", "hello"]).is_err());
}

#[cfg(feature = "sim")]
#[test]
fn sim_soak_parses_with_defaults() {
    let cli = Cli::try_parse_from([
        "assay", "sim", "soak", "--target", "bundle", "--report", "out.json",
    ])
    .expect("parse should succeed");

    match cli.cmd {
        Command::Sim(sim) => match sim.cmd {
            SimSub::Soak(args) => {
                assert_eq!(args.iterations, 20);
                assert_eq!(args.time_budget, 60);
                assert_eq!(args.seed, None);
                assert_eq!(args.target, "bundle");
            }
            _ => panic!("expected SimSub::Soak"),
        },
        _ => panic!("expected Command::Sim"),
    }
}

#[cfg(feature = "sim")]
#[test]
fn sim_soak_parses_explicit_values() {
    let cli = Cli::try_parse_from([
        "assay",
        "sim",
        "soak",
        "--iterations",
        "5",
        "--seed",
        "42",
        "--target",
        "scenario-a",
        "--report",
        "out.json",
        "--time-budget",
        "120",
    ])
    .expect("parse should succeed");

    match cli.cmd {
        Command::Sim(sim) => match sim.cmd {
            SimSub::Soak(args) => {
                assert_eq!(args.iterations, 5);
                assert_eq!(args.seed, Some(42));
                assert_eq!(args.target, "scenario-a");
                assert_eq!(args.time_budget, 120);
            }
            _ => panic!("expected SimSub::Soak"),
        },
        _ => panic!("expected Command::Sim"),
    }
}

/// Every argument whose name ends in `format` advertises the values it accepts.
///
/// `tests/format_value_parser.rs` checks the same property against a hand-kept table, and that is
/// exactly how `doctor --format` escaped: it was a bare `String` with `// text|json` beside it,
/// `--format totally-invalid` printed the text report and exited 0, and every test there passed
/// because the table never named it. A list beside the thing it describes drifts silently, and in
/// the dangerous direction — the argument nobody listed is the one nobody checked.
///
/// So this derives the set from clap instead of listing it. It lives here rather than beside its
/// sibling because `assay-cli` has no library target, so only an in-crate test can walk the real
/// command tree. Adding a `--format` to a new command fails here until it carries a `value_enum`.
#[test]
fn every_format_argument_advertises_its_accepted_values() {
    use clap::CommandFactory;

    fn walk(cmd: &clap::Command, path: &[String], untyped: &mut Vec<String>) {
        for arg in cmd.get_arguments() {
            let Some(long) = arg.get_long() else { continue };
            if !long.ends_with("format") {
                continue;
            }
            if arg.get_possible_values().is_empty() {
                untyped.push(format!("assay {} --{long}", path.join(" ")));
            }
        }
        for sub in cmd.get_subcommands() {
            let mut child = path.to_vec();
            child.push(sub.get_name().to_string());
            walk(sub, &child, untyped);
        }
    }

    let cli = super::Cli::command();
    let mut untyped = Vec::new();
    walk(&cli, &[], &mut untyped);

    // A walk that finds nothing would pass silently, so prove it reached the arguments first.
    let mut total = 0usize;
    fn count(cmd: &clap::Command, total: &mut usize) {
        *total += cmd
            .get_arguments()
            .filter(|a| a.get_long().is_some_and(|l| l.ends_with("format")))
            .count();
        for sub in cmd.get_subcommands() {
            count(sub, total);
        }
    }
    count(&cli, &mut total);
    assert!(
        total > 5,
        "the walk found only {total} format arguments; it is not reaching them"
    );

    assert!(
        untyped.is_empty(),
        "these format arguments accept any string, so a typo selects a fallback silently:\n  {}",
        untyped.join("\n  ")
    );
}

#[test]
fn top_level_failure_funnel_owns_supported_machine_output_commands() {
    let policy_json = Cli::try_parse_from([
        "assay",
        "policy",
        "validate",
        "--input",
        "policy.yaml",
        "--format",
        "json",
    ])
    .expect("policy validate JSON parses");
    assert_eq!(policy_json.machine_output_verify_enabled(), Some(true));

    let policy_text =
        Cli::try_parse_from(["assay", "policy", "validate", "--input", "policy.yaml"])
            .expect("policy validate text parses");
    assert_eq!(policy_text.machine_output_verify_enabled(), None);

    let evidence_json = Cli::try_parse_from([
        "assay",
        "evidence",
        "show",
        "bundle.tar.gz",
        "--format",
        "json",
    ])
    .expect("evidence show JSON parses");
    assert_eq!(evidence_json.machine_output_verify_enabled(), Some(true));

    let evidence_json_unverified = Cli::try_parse_from([
        "assay",
        "evidence",
        "show",
        "bundle.tar.gz",
        "--format",
        "json",
        "--no-verify",
    ])
    .expect("unverified evidence show JSON parses");
    assert_eq!(
        evidence_json_unverified.machine_output_verify_enabled(),
        Some(false)
    );

    let evidence_table = Cli::try_parse_from(["assay", "evidence", "show", "bundle.tar.gz"])
        .expect("evidence show table parses");
    assert_eq!(evidence_table.machine_output_verify_enabled(), None);

    let coverage_json = Cli::try_parse_from(["assay", "coverage", "--format", "json"])
        .expect("legacy coverage JSON parses");
    assert_eq!(
        coverage_json.machine_output_verify_enabled(),
        Some(true),
        "legacy coverage JSON failures use the shared Summary envelope"
    );

    let coverage_input_json = Cli::try_parse_from([
        "assay",
        "coverage",
        "--input",
        "events.jsonl",
        "--format",
        "json",
    ])
    .expect("input-mode coverage JSON parses");
    assert_eq!(
        coverage_input_json.machine_output_verify_enabled(),
        None,
        "input mode writes coverage_report_v1 to a file and must not inherit the legacy envelope"
    );

    let coverage_text =
        Cli::try_parse_from(["assay", "coverage"]).expect("legacy coverage text parses");
    assert_eq!(coverage_text.machine_output_verify_enabled(), None);

    let run_json =
        Cli::try_parse_from(["assay", "run", "--config", "eval.yaml", "--format", "json"])
            .expect("run JSON parses");
    assert!(
        run_json.machine_output_verify_enabled().is_none(),
        "run owns its JSON error renderer and must not be rendered twice"
    );
}

#[test]
fn sandbox_enforcement_health_without_enforce_net_is_clap_usage_error() {
    let missing = Cli::try_parse_from([
        "assay",
        "sandbox",
        "--enforcement-health",
        "health.json",
        "--",
        "true",
    ]);
    let err = match missing {
        Ok(_) => panic!(
            "sandbox --enforcement-health without --enforce-net must fail clap before execution"
        ),
        Err(err) => err,
    };
    assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument);
    let rendered = err.to_string();
    assert!(
        rendered.contains("--enforce-net"),
        "clap must name the missing --enforce-net requirement: {rendered}"
    );
}

#[test]
fn sandbox_enforcement_health_with_enforce_net_still_parses() {
    // Control: a live parser still accepts the coherent invocation. A dead harness
    // that always returned Err would make the rejection test look green.
    let ok = Cli::try_parse_from([
        "assay",
        "sandbox",
        "--enforce",
        "--enforce-net",
        "--enforcement-health",
        "health.json",
        "--",
        "true",
    ])
    .expect("sandbox --enforce --enforce-net --enforcement-health must still parse");
    match ok.cmd {
        Command::Sandbox(args) => {
            assert!(args.enforce);
            assert!(args.enforce_net);
            assert_eq!(
                args.enforcement_health.as_deref(),
                Some(std::path::Path::new("health.json"))
            );
            assert_eq!(args.command, ["true"]);
        }
        _ => panic!("expected sandbox command"),
    }
}

#[test]
fn sandbox_allow_audit_fallback_conflicts_with_fail_closed() {
    let err = match Cli::try_parse_from([
        "assay",
        "sandbox",
        "--enforce",
        "--fail-closed",
        "--allow-audit-fallback",
        "--",
        "true",
    ]) {
        Ok(_) => panic!("--allow-audit-fallback must conflict with --fail-closed at clap"),
        Err(err) => err,
    };
    assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
    let rendered = err.to_string();
    assert!(
        rendered.contains("--allow-audit-fallback") && rendered.contains("--fail-closed"),
        "clap must name both flags: {rendered}"
    );
}

#[test]
fn sandbox_allow_audit_fallback_parses_with_enforce() {
    let ok = Cli::try_parse_from([
        "assay",
        "sandbox",
        "--enforce",
        "--allow-audit-fallback",
        "--",
        "true",
    ])
    .expect("--enforce --allow-audit-fallback must still parse");
    match ok.cmd {
        Command::Sandbox(args) => {
            assert!(args.enforce);
            assert!(args.allow_audit_fallback);
            assert!(!args.fail_closed);
            assert_eq!(args.command, ["true"]);
        }
        _ => panic!("expected sandbox command"),
    }
}