assay-cli 7.0.3

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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use super::{
    decide_run_outcome, has_judge_verdict_abstain, reason_code_from_error_message,
    reason_code_from_run_error, write_extended_run_json, write_run_json_minimal,
};
use crate::exit_codes::{ExitCodeVersion, ReasonCode, RunOutcome, EXIT_INFRA_ERROR};
use assay_core::errors::RunError;
use assay_core::model::{TestResultRow, TestStatus};
use assay_core::report::RunArtifacts;

#[test]
fn on_disk_run_json_is_render_safe() {
    let secret = format!("ghp_{}", "A".repeat(36));
    let artifacts = RunArtifacts {
        run_id: 7,
        suite: "owned-suite".into(),
        results: vec![TestResultRow {
            test_id: "t_owned".into(),
            status: TestStatus::Fail,
            score: Some(0.0),
            cached: false,
            message: format!("failed: leaked {secret}"),
            details: serde_json::json!({
                "prompt": format!("ask {secret} alice@example.com"),
                "metrics": { "must_contain": { "details": { "message": format!("missing {secret}") } } },
            }),
            duration_ms: Some(3),
            fingerprint: Some("fp_owned".into()),
            skip_reason: None,
            attempts: None,
            error_policy_applied: None,
        }],
        order_seed: None,
        runner_clone_ms: None,
    };
    let outcome = RunOutcome::from_reason(ReasonCode::ETestFailed, None, None);
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("run.json");
    write_extended_run_json(&artifacts, &outcome, &path, None).unwrap();
    let out = std::fs::read_to_string(&path).unwrap();

    let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(!out.contains(&secret), "on-disk run.json leaked secret");
    assert!(!out.contains("ghp_"), "raw token prefix leaked to run.json");
    assert!(
        !out.contains("alice@example.com"),
        "on-disk run.json leaked pii"
    );
    assert!(
        out.contains("<redacted:"),
        "on-disk run.json fired no redaction"
    );
    assert_eq!(parsed["suite"], "owned-suite");
    assert_eq!(parsed["results"][0]["test_id"], "t_owned");
    assert_eq!(parsed["results"][0]["fingerprint"], "fp_owned");
    assert_eq!(parsed["reason_code"], "E_TEST_FAILED");
}

#[test]
fn minimal_run_json_resolution_message_is_render_safe() {
    let secret = format!("ghp_{}", "Z".repeat(36));
    let outcome = RunOutcome::from_reason(
        ReasonCode::ETraceNotFound,
        Some(format!("trace not found: {secret}")),
        None,
    );
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("run.json");
    write_run_json_minimal(&outcome, &path).unwrap();
    let out = std::fs::read_to_string(&path).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(
        !out.contains(&secret),
        "minimal run.json leaked secret in resolution.message"
    );
    assert!(!out.contains("ghp_"), "raw token prefix leaked to run.json");
    assert!(
        out.contains("<redacted:"),
        "minimal run.json fired no redaction"
    );
    assert_eq!(parsed["reason_code"], "E_TRACE_NOT_FOUND");
}

#[test]
fn test_infra_beats_abstain_precedence() {
    let results = vec![
        TestResultRow {
            test_id: "infra".into(),
            status: TestStatus::Error,
            score: None,
            cached: false,
            message: "Request timeout".into(),
            details: serde_json::json!({}),
            duration_ms: None,
            fingerprint: None,
            skip_reason: None,
            attempts: None,
            error_policy_applied: None,
        },
        TestResultRow {
            test_id: "abstain".into(),
            status: TestStatus::Pass,
            score: Some(0.5),
            cached: false,
            message: String::new(),
            details: serde_json::json!({
                "metrics": {
                    "faithfulness": {
                        "details": { "verdict": "Abstain", "score": 0.5 }
                    }
                }
            }),
            duration_ms: None,
            fingerprint: None,
            skip_reason: None,
            attempts: None,
            error_policy_applied: None,
        },
    ];
    let outcome = decide_run_outcome(&results, false, ExitCodeVersion::V2);
    assert_eq!(
        outcome.exit_code, EXIT_INFRA_ERROR,
        "infra must beat abstain: expected exit 3"
    );
    assert!(
        outcome.reason_code == ReasonCode::ETimeout.as_str()
            || outcome.reason_code == ReasonCode::EJudgeUnavailable.as_str(),
        "reason should be infra (E_TIMEOUT or E_JUDGE_UNAVAILABLE), got {}",
        outcome.reason_code
    );
}

#[test]
fn test_has_judge_verdict_abstain_detects_abstain() {
    let details = serde_json::json!({
        "metrics": {
            "faithfulness": {
                "score": 0.5,
                "passed": false,
                "unstable": true,
                "details": { "verdict": "Abstain", "score": 0.5 }
            }
        }
    });
    assert!(has_judge_verdict_abstain(&details));
}

#[test]
fn test_has_judge_verdict_abstain_ignores_pass() {
    let details = serde_json::json!({
        "metrics": {
            "faithfulness": {
                "score": 1.0,
                "passed": true,
                "unstable": false,
                "details": { "verdict": "Pass", "score": 1.0 }
            }
        }
    });
    assert!(!has_judge_verdict_abstain(&details));
}

#[test]
fn test_has_judge_verdict_abstain_no_metrics() {
    let details = serde_json::json!({});
    assert!(!has_judge_verdict_abstain(&details));
}

#[test]
fn test_reason_code_from_error_message_maps_config_family() {
    assert_eq!(
        reason_code_from_error_message("trace not found: traces/missing.jsonl"),
        Some(ReasonCode::ETraceNotFound)
    );
    assert_eq!(
        reason_code_from_error_message("Config file not found: eval.yaml"),
        Some(ReasonCode::EMissingConfig)
    );
    assert_eq!(
        reason_code_from_error_message("config error: unknown field `foo`"),
        Some(ReasonCode::ECfgParse)
    );
}

#[test]
fn test_reason_code_from_error_message_maps_infra_family() {
    assert_eq!(
        reason_code_from_error_message("provider returned 429 rate limit"),
        Some(ReasonCode::ERateLimit)
    );
    assert_eq!(
        reason_code_from_error_message("request timeout while calling judge"),
        Some(ReasonCode::ETimeout)
    );
    assert_eq!(
        reason_code_from_error_message("provider error: 503"),
        Some(ReasonCode::EProvider5xx)
    );
    assert_eq!(
        reason_code_from_error_message("network connection reset by peer"),
        Some(ReasonCode::ENetworkError)
    );
}

#[test]
fn test_reason_code_from_run_error_uses_typed_kind() {
    let typed = RunError::missing_config("eval.yaml", "missing");
    assert_eq!(
        reason_code_from_run_error(&typed),
        Some(ReasonCode::EMissingConfig)
    );
    assert!(!typed.legacy_classified);
}

#[test]
fn test_decide_outcome_uses_typed_details_before_legacy_message_fallback() {
    let row = TestResultRow {
        test_id: "typed".into(),
        status: TestStatus::Error,
        score: None,
        cached: false,
        message: "untyped error text".into(),
        details: serde_json::json!({
            "run_error_kind": "invalid_args"
        }),
        duration_ms: None,
        fingerprint: None,
        skip_reason: None,
        attempts: None,
        error_policy_applied: None,
    };

    let outcome = decide_run_outcome(&[row], true, ExitCodeVersion::V2);
    assert_eq!(outcome.reason_code, ReasonCode::EInvalidArgs.as_str());
    assert_eq!(
        outcome.exit_code,
        ReasonCode::EInvalidArgs.exit_code_for(ExitCodeVersion::V2)
    );
}

/// A passing row whose named metric evaluated nothing, shaped as `single.rs` writes it.
fn not_exercised_row(test_id: &str, metric: &str, reason: &str) -> TestResultRow {
    TestResultRow {
        test_id: test_id.into(),
        status: TestStatus::Pass,
        score: None,
        cached: false,
        message: "ok".into(),
        details: serde_json::json!({
            "metrics": {
                metric: {
                    "score": 1.0,
                    "passed": true,
                    "unstable": false,
                    "exercised": "not_exercised",
                    "details": { "reason": reason }
                }
            }
        }),
        duration_ms: Some(1),
        fingerprint: None,
        skip_reason: None,
        attempts: None,
        error_policy_applied: None,
    }
}

/// The success path is the one that matters, and it is the one an early `return` would have missed.
///
/// `decide_run_outcome` has six early returns; a not-exercised metric appears on a *green* run by
/// definition, so the warning must survive the path that returns first and reports nothing else.
#[test]
fn a_not_exercised_metric_warns_on_a_passing_run_without_changing_the_exit_code() {
    let rows = vec![not_exercised_row(
        "t1",
        "sequence_valid",
        "no tool calls in the trace",
    )];
    let outcome = decide_run_outcome(&rows, false, ExitCodeVersion::default());

    assert_eq!(outcome.exit_code, 0, "a coverage observation is not a gate");
    assert_eq!(outcome.reason_code, ReasonCode::Success.as_str());
    assert!(
        outcome
            .warnings
            .iter()
            .any(|w| w.contains("W_METRIC_NOT_EXERCISED") && w.contains("sequence_valid")),
        "warnings: {:?}",
        outcome.warnings
    );
}

/// And on a failing run too — the early return at priority 1/2 must not swallow it.
#[test]
fn a_failing_run_still_reports_what_was_never_exercised() {
    let mut fail = not_exercised_row("t2", "tool_output_valid", "no output schemas configured");
    fail.status = TestStatus::Fail;
    fail.message = "failed: must_contain".into();
    let rows = vec![
        not_exercised_row("t1", "tool_output_valid", "no output schemas configured"),
        fail,
    ];
    let outcome = decide_run_outcome(&rows, false, ExitCodeVersion::default());

    assert_ne!(outcome.exit_code, 0, "the failure still decides the exit");
    assert!(
        outcome.warnings.iter().any(|w| w.contains("2 test(s)")),
        "the failing run lost its coverage warning: {:?}",
        outcome.warnings
    );
}

/// `--strict` promotes Warn/Flaky/Unstable *statuses* to a violation. A not-exercised metric is not
/// a status, so strict mode must not turn a coverage observation into a failing build — that is
/// precisely the over-eager detection that earns a suppression.
#[test]
fn strict_mode_does_not_promote_a_coverage_observation_to_a_violation() {
    let rows = vec![not_exercised_row("t1", "seq", "no tool calls")];
    let outcome = decide_run_outcome(&rows, true, ExitCodeVersion::default());
    assert_eq!(outcome.exit_code, 0, "strict mode failed a green suite");
    assert!(!outcome.warnings.is_empty());
}

/// The warning reaches the artifact, not just the struct.
#[test]
fn the_warning_lands_in_run_json() {
    let artifacts = RunArtifacts {
        run_id: 1,
        suite: "s".into(),
        results: vec![not_exercised_row("t1", "seq", "no tool calls in the trace")],
        order_seed: None,
        runner_clone_ms: None,
    };
    let outcome = decide_run_outcome(&artifacts.results, false, ExitCodeVersion::default());
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("run.json");
    write_extended_run_json(&artifacts, &outcome, &path, None).unwrap();

    let parsed: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
    let warnings = parsed["warnings"].as_array().expect("warnings array");
    assert!(
        warnings.iter().any(|w| w
            .as_str()
            .is_some_and(|s| s.contains("W_METRIC_NOT_EXERCISED"))),
        "run.json: {parsed}"
    );
}

/// A run with nothing to say adds no `warnings` key at all.
#[test]
fn a_fully_exercised_run_adds_no_warnings() {
    let mut row = not_exercised_row("t1", "semantic", "n/a");
    row.details["metrics"]["semantic"]["exercised"] = serde_json::json!("exercised");
    let outcome = decide_run_outcome(&[row], false, ExitCodeVersion::default());
    assert!(outcome.warnings.is_empty(), "{:?}", outcome.warnings);
}

/// A row whose assertions never evaluated, shaped as `runner_next/assertions`
/// writes it (#3117, 6.7.0).
fn not_evaluated_row(test_id: &str, kind: &str, remedy: &str) -> TestResultRow {
    TestResultRow {
        test_id: test_id.into(),
        status: TestStatus::Error,
        score: None,
        cached: false,
        message: format!("assertions error: E_TRACE_EPISODE_...: no episode for {test_id}"),
        details: serde_json::json!({
            "assertions": { "error": format!("E_TRACE_EPISODE_...: no episode for {test_id}") },
            "assertions_not_evaluated": {
                "evaluated": false,
                "kind": kind,
                "remedy": remedy,
            },
        }),
        duration_ms: None,
        fingerprint: None,
        skip_reason: None,
        attempts: None,
        error_policy_applied: None,
    }
}

fn infra_error_row(test_id: &str) -> TestResultRow {
    TestResultRow {
        test_id: test_id.into(),
        status: TestStatus::Error,
        score: None,
        cached: false,
        message: "provider returned 429 rate limit".into(),
        details: serde_json::json!({
            "run_error_kind": "provider_rate_limit"
        }),
        duration_ms: None,
        fingerprint: None,
        skip_reason: None,
        attempts: None,
        error_policy_applied: None,
    }
}

fn evaluated_fail_row(test_id: &str) -> TestResultRow {
    TestResultRow {
        test_id: test_id.into(),
        status: TestStatus::Fail,
        score: None,
        cached: false,
        message: "assertions failed (1)".into(),
        details: serde_json::json!({
            "assertions": [{ "message": "must not call delete_repository" }]
        }),
        duration_ms: None,
        fingerprint: None,
        skip_reason: None,
        attempts: None,
        error_policy_applied: None,
    }
}

/// B1: a missing episode exits 1 with its registered code on both profiles —
/// and never as `E_JUDGE_UNAVAILABLE`, which is what Priority 2 would report
/// for an `Error` row without the not-evaluated exclusion.
#[test]
fn missing_episode_reports_its_code_at_exit_1_on_both_profiles() {
    use assay_core::report::exercised::EPISODE_MISSING;
    let rows = || {
        vec![not_evaluated_row(
            "t_missing",
            EPISODE_MISSING,
            "the episode's meta.test_id must match the suite test id",
        )]
    };
    for version in [ExitCodeVersion::V1, ExitCodeVersion::V2] {
        let outcome = decide_run_outcome(&rows(), false, version);
        assert_eq!(
            outcome.reason_code,
            ReasonCode::ETraceEpisodeMissing.as_str(),
            "version {version:?}"
        );
        assert_eq!(outcome.exit_code, 1, "version {version:?}");
        assert!(
            outcome
                .next_step
                .as_deref()
                .is_some_and(|s| s.contains("meta.test_id")),
            "next_step is the row remedy: {:?}",
            outcome.next_step
        );
    }
}

/// B1: an ambiguous episode has its own code and the same exit class.
#[test]
fn ambiguous_episode_reports_its_code_at_exit_1() {
    use assay_core::report::exercised::EPISODE_AMBIGUOUS;
    let rows = vec![not_evaluated_row(
        "t_ambiguous",
        EPISODE_AMBIGUOUS,
        "keep a single stored episode whose meta.test_id is the suite test id",
    )];
    let outcome = decide_run_outcome(&rows, false, ExitCodeVersion::V2);
    assert_eq!(
        outcome.reason_code,
        ReasonCode::ETraceEpisodeAmbiguous.as_str()
    );
    assert_eq!(outcome.exit_code, 1);
}

/// B1: an evaluated failure is reported before a not-evaluated row.
#[test]
fn evaluated_failure_beats_not_evaluated_row() {
    use assay_core::report::exercised::EPISODE_MISSING;
    let rows = vec![
        evaluated_fail_row("t_fail"),
        not_evaluated_row(
            "t_missing",
            EPISODE_MISSING,
            "the episode's meta.test_id must match the suite test id",
        ),
    ];
    let outcome = decide_run_outcome(&rows, false, ExitCodeVersion::V2);
    assert_eq!(outcome.reason_code, ReasonCode::ETestFailed.as_str());
    assert_eq!(outcome.exit_code, 1);
}

/// Missing outranks ambiguous regardless of result order, with counts.
#[test]
fn missing_outranks_ambiguous_with_counts() {
    use assay_core::report::exercised::{EPISODE_AMBIGUOUS, EPISODE_MISSING};
    let rows = vec![
        not_evaluated_row(
            "t_ambiguous",
            EPISODE_AMBIGUOUS,
            "keep a single stored episode whose meta.test_id is the suite test id",
        ),
        not_evaluated_row(
            "t_missing",
            EPISODE_MISSING,
            "the episode's meta.test_id must match the suite test id",
        ),
    ];
    let outcome = decide_run_outcome(&rows, false, ExitCodeVersion::V2);
    assert_eq!(
        outcome.reason_code,
        ReasonCode::ETraceEpisodeMissing.as_str()
    );
    assert_eq!(
        outcome.message.as_deref(),
        Some("assertions not evaluated for 2 test(s): 1 missing, 1 ambiguous")
    );
}

/// B1: a genuine infra `Error` still precedes the not-evaluated step, so a
/// missing episode beside a rate limit reports the rate limit at exit 3.
#[test]
fn infra_error_beats_not_evaluated_row() {
    use assay_core::report::exercised::EPISODE_MISSING;
    let rows = vec![
        not_evaluated_row(
            "t_missing",
            EPISODE_MISSING,
            "the episode's meta.test_id must match the suite test id",
        ),
        infra_error_row("t_limited"),
    ];
    let outcome = decide_run_outcome(&rows, false, ExitCodeVersion::V2);
    assert_eq!(outcome.reason_code, ReasonCode::ERateLimit.as_str());
    assert_eq!(outcome.exit_code, 3);
}

/// The typed companion wins over a misleading message: the legacy classifier
/// is never consulted for not-evaluated rows.
#[test]
fn not_evaluated_companion_beats_legacy_message_classification() {
    use assay_core::report::exercised::EPISODE_MISSING;
    let mut row = not_evaluated_row(
        "t_missing",
        EPISODE_MISSING,
        "the episode's meta.test_id must match the suite test id",
    );
    row.message = "judge unavailable: connection reset".into();
    let outcome = decide_run_outcome(&[row], false, ExitCodeVersion::V2);
    assert_eq!(
        outcome.reason_code,
        ReasonCode::ETraceEpisodeMissing.as_str()
    );
    assert_eq!(outcome.exit_code, 1);
}