assay-cli 6.22.1

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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
//! EXPERIMENTAL: verify an evidence bundle against privileged-mcp-action v0 or v1.
//!
//! Spec: `docs/profiles/privileged-mcp-action/v0.md`. Four byte-pure stages:
//! 1. bundle integrity (the shipped bundle verification, via `BundleReader::open` which runs
//!    `verify_bundle_with_limits`; any failure means `bundle_integrity: "fail"` and nothing below
//!    stage 1 is consumed);
//! 2. statement well-formedness (cardinality, closed vocabularies, producer non-claims verbatim);
//! 3. binding validity (marker triple + exact `(tool name, target digest)` byte equality);
//! 4. claim recompute (the fixed matrix; establish records are diagnostic journey only).
//!
//! The output is a claim matrix, never a score. Refuted claims still exit 0: the report is the
//! product and consumers gate on cells; only integrity failure or an invalid verdict exits 2.

use crate::evidence_verify_reason::{
    reason_code_for_evidence_error, PROFILE_EVIDENCE_REASON_CODES,
};
use crate::exit_codes::{self, ReasonCode};
use anyhow::{Context, Result};
use assay_evidence::bundle::{BundleReader, VerifyLimits};
use assay_evidence::denial_marker::{classify_denial_marker, DenialMarkerVersion};
use assay_evidence::types::EvidenceEvent;
use clap::{Args, ValueEnum};
use serde::Serialize;
use serde_json::Value;
use std::fs::File;
use std::path::{Path, PathBuf};

const REPORT_SCHEMA: &str = "assay.privileged_mcp_action.verify.report.v0";
const PROFILE_ID: &str = "privileged-mcp-action/v0";
const PROFILE_ID_V1: &str = "privileged-mcp-action/v1";

const DECISION_SCHEMA: &str = "assay.enforcement_decision.v0";
const OBSERVATION_SCHEMA: &str = "assay.denied_call_observation.v0";
const OBSERVATION_SCHEMA_V1: &str = "assay.denied_call_observation.v1";
const ESTABLISH_SCHEMA: &str = "assay.manifest_establish.v0";
/// Profile namespaces: any payload schema with one of these prefixes that is not a recognized
/// record for the *selected* profile version fails closed. Selection is explicit (`--profile-version`);
/// there is no autodetect.
const PROFILE_NAMESPACES: &[&str] = &[
    "assay.enforcement_decision.",
    "assay.denied_call_observation.",
    "assay.manifest_establish.",
];

const DECISION_VOCAB: &[&str] = &["allow", "deny"];
const REASON_VOCAB: &[&str] = &[
    "unclassified_tool_call",
    "classification_incomplete",
    "no_declared_allowance",
    "credential_scope_unknown",
    "credential_scope_insufficient",
    "manifest_baseline_missing",
    "manifest_observation_ambiguous",
    "manifest_current_observation_incomplete",
    "manifest_drifted_since_approval",
    "allow",
];
const DRIFT_STATE_VOCAB: &[&str] = &[
    "satisfied",
    "baseline_missing",
    "current_observation_incomplete",
    "observation_ambiguous",
    "drifted",
    "not_evaluated",
];
const ESTABLISH_PATH_VOCAB: &[&str] = &[
    "no_establish_needed",
    "established_then_allowed",
    "established_then_denied",
    "immediate_deny",
];

// Marker recognition is the shared classifier in `assay_evidence::denial_marker`.

/// The producer's five decision non-claims, byte-exact (source of truth:
/// `assay-mcp-server/src/proxy/enforce/records.rs`; the fourth carries U+2014).
const DECISION_PRODUCER_NON_CLAIMS: &[&str] = &[
    "policy decision only; does not assert or verify the upstream side effect (stays asserted, E9 ladder)",
    "an allow is the decision to forward; it does not assert the call reached or was performed by the upstream (a transport failure surfaces as proxy_failed, not here)",
    "credential referenced by alias only, never the token or declared scopes",
    "deny is fail-closed caution and allow is a policy decision \u{2014} neither is a maliciousness verdict",
    "not the observation artifact (assay.mcp_manifest_observed.v0) and not the mechanism artifact (assay.enforcement_health.v0)",
];

/// The producer's four observation non-claims, byte-exact (source of truth:
/// `assay-mcp-server/src/proxy/denied_observation.rs`).
const OBSERVATION_PRODUCER_NON_CLAIMS: &[&str] = &[
    "caller-visible proxy denial observation only; policy decision lives in assay.enforcement_decision.v0",
    "does not assert or verify the upstream side effect",
    "does not assert maliciousness, safety, approval, or whole-action trust",
    "must not be read as a replacement for the bound enforcement decision record",
];

/// The four fixed report non-claims, verbatim from the spec, present in every report.
const REPORT_NON_CLAIMS: [&str; 4] = [
    "allow does not prove upstream delivery",
    "deny does not establish maliciousness",
    "caller-visible denial does not prove external side-effect absence",
    "bundle integrity does not upgrade source class",
];

#[derive(Debug, Args, Clone)]
pub struct VerifyPrivilegedMcpActionArgs {
    /// Evidence bundle (.tar.gz) carrying privileged-mcp-action profile records
    #[arg(value_name = "BUNDLE")]
    pub bundle: PathBuf,

    /// Output format
    #[arg(long, value_enum, default_value_t = VerifyFormat::Table)]
    pub format: VerifyFormat,

    /// Selected privileged-mcp-action interpreter. Omit for default v0; passing
    /// the flag is explicit. No autodetect from bundle contents. Does not name
    /// a carried input profile id (v0/v1 declare none).
    #[arg(long, value_enum)]
    pub profile_version: Option<ProfileVersion>,
}

#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
pub enum ProfileVersion {
    #[default]
    #[value(name = "v0")]
    V0,
    #[value(name = "v1")]
    V1,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ProfileSelection {
    Default,
    Explicit,
}

impl ProfileSelection {
    fn as_str(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Explicit => "explicit",
        }
    }
}

struct ProfileProjection {
    profile: &'static str,
    profile_selection: ProfileSelection,
    input_profile: Option<&'static str>,
    input_profile_status: &'static str,
}

/// One projection rule: selected interpreter is not input identity.
/// Frozen v0/v1 carry no profile id; do not infer one from `--profile-version`.
fn project_profile(
    selected: ProfileVersion,
    profile_selection: ProfileSelection,
) -> ProfileProjection {
    ProfileProjection {
        profile: selected.as_profile_id(),
        profile_selection,
        input_profile: None,
        input_profile_status: "undeclared_legacy",
    }
}

impl ProfileVersion {
    fn as_profile_id(self) -> &'static str {
        match self {
            Self::V0 => PROFILE_ID,
            Self::V1 => PROFILE_ID_V1,
        }
    }

    fn observation_schema(self) -> &'static str {
        match self {
            Self::V0 => OBSERVATION_SCHEMA,
            Self::V1 => OBSERVATION_SCHEMA_V1,
        }
    }

    fn marker_version(self) -> DenialMarkerVersion {
        match self {
            Self::V0 => DenialMarkerVersion::V0,
            Self::V1 => DenialMarkerVersion::V1,
        }
    }

    /// The `--profile-version` value that selects this interpreter, as clap publishes it.
    fn cli_value(self) -> String {
        self.to_possible_value()
            .map(|value| value.get_name().to_string())
            .expect("every profile version is a selectable value")
    }
}

#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum VerifyFormat {
    Json,
    Table,
}

#[derive(Debug, Serialize)]
pub struct Report {
    pub schema: &'static str,
    /// Selected interpreter. Never detected or carried input identity.
    pub profile: &'static str,
    pub profile_selection: ProfileSelection,
    /// Frozen v0/v1 always serialize JSON null.
    pub input_profile: Option<&'static str>,
    pub input_profile_status: &'static str,
    pub bundle_integrity: &'static str,
    /// Present only when `bundle_integrity` is `pass`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verdict: Option<&'static str>,
    /// Present only when `verdict` is `valid`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub claims: Option<Claims>,
    pub findings: Vec<Finding>,
    pub non_claims: [&'static str; 4],
    /// Verifier diagnostic outside the claim lattice. Absent on success.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason_code: Option<&'static str>,
    /// Shell-free remediation. Unreadable I/O uses caller-argv JSON; other
    /// codes stay prose. Absent on success.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_step: Option<String>,
    /// Self-reported implementation identification, present in every report.
    pub evaluator: Evaluator,
}

/// The implementation that produced the report, as it reports itself: a diagnostic naming the
/// release. It does not authenticate which code ran, bind the report to a binary, or change any
/// claim cell, source class, verdict or exit code. Not the bundle producer (`ProducerMeta`).
#[derive(Debug, Serialize)]
pub struct Evaluator {
    pub name: &'static str,
    pub version: &'static str,
}

const EVALUATOR: Evaluator = Evaluator {
    name: "assay",
    version: env!("CARGO_PKG_VERSION"),
};

#[derive(Debug, Serialize)]
pub struct Claims {
    pub policy_decision_recorded: ClaimCell,
    pub caller_visible_denial: ClaimCell,
    pub upstream_delivery: ClaimCell,
    pub external_side_effect: ClaimCell,
}

#[derive(Debug, Serialize)]
pub struct ClaimCell {
    pub status: &'static str,
    /// Only confirmed and refuted cells carry a source class; incomplete cells carry none.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_class: Option<&'static str>,
}

impl ClaimCell {
    fn confirmed() -> Self {
        Self {
            status: "confirmed",
            source_class: Some("producer_reported"),
        }
    }
    fn refuted() -> Self {
        Self {
            status: "refuted",
            source_class: Some("producer_reported"),
        }
    }
    fn incomplete() -> Self {
        Self {
            status: "incomplete",
            source_class: None,
        }
    }
}

#[derive(Debug, Serialize)]
pub struct Finding {
    pub id: String,
    pub detail: String,
    /// Exact in-namespace payload schema when one was observed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub observed_schema: Option<String>,
}

pub fn cmd_verify_privileged_mcp_action(args: VerifyPrivilegedMcpActionArgs) -> Result<i32> {
    let profile_selection = match args.profile_version {
        None => ProfileSelection::Default,
        Some(_) => ProfileSelection::Explicit,
    };
    let profile_version = args.profile_version.unwrap_or(ProfileVersion::V0);
    let report = verify_bundle_report_for(&args.bundle, profile_version, profile_selection);
    match args.format {
        VerifyFormat::Json => println!("{}", serde_json::to_string_pretty(&report)?),
        VerifyFormat::Table => print_table(&report),
    }
    let ok = report.bundle_integrity == "pass" && report.verdict == Some("valid");
    Ok(if ok { exit_codes::OK } else { 2 })
}

/// Stage 1 + stages 2-4 over one bundle path. Open and read failures are the same
/// stage-1 report: never a usage-error envelope. Typed `VerifyError` is
/// authoritative; untyped I/O (including a directory path) is Unreadable.
/// `findings.detail` may retain the caller argv path. Unreadable `next_step` is
/// shell-free caller-argv via `ReasonCode::next_step`, not a second remediation.
#[cfg(test)]
pub fn verify_bundle_report(bundle: &Path) -> Report {
    verify_bundle_report_for(bundle, ProfileVersion::V0, ProfileSelection::Default)
}

pub fn verify_bundle_report_for(
    bundle: &Path,
    profile_version: ProfileVersion,
    profile_selection: ProfileSelection,
) -> Report {
    match read_bundle_events(bundle) {
        Ok(events) => profile_report_for_selection(&events, profile_version, profile_selection),
        Err(err) => integrity_fail_report(&err, bundle, profile_version, profile_selection),
    }
}

fn read_bundle_events(bundle: &Path) -> anyhow::Result<Vec<EvidenceEvent>> {
    let file = File::open(bundle)
        .with_context(|| format!("failed to open bundle {}", bundle.display()))?;
    // Stage 1: open_with_limits runs the shipped bundle verification before exposing
    // any event. The retain ceiling is explicit and tighter than the 500 MiB default.
    // Context names the caller argv without replacing a typed VerifyError in the chain.
    let reader = BundleReader::open_with_limits(file, VerifyLimits::for_retained_events())
        .with_context(|| format!("failed to read bundle {}", bundle.display()))?;
    reader
        .events_vec()
        .with_context(|| format!("failed to read events from bundle {}", bundle.display()))
}

struct ReportBody {
    bundle_integrity: &'static str,
    verdict: Option<&'static str>,
    claims: Option<Claims>,
    findings: Vec<Finding>,
    reason_code: Option<&'static str>,
    next_step: Option<String>,
}

fn emit_report(
    profile_version: ProfileVersion,
    profile_selection: ProfileSelection,
    body: ReportBody,
) -> Report {
    let projected = project_profile(profile_version, profile_selection);
    Report {
        schema: REPORT_SCHEMA,
        profile: projected.profile,
        profile_selection: projected.profile_selection,
        input_profile: projected.input_profile,
        input_profile_status: projected.input_profile_status,
        bundle_integrity: body.bundle_integrity,
        verdict: body.verdict,
        claims: body.claims,
        findings: body.findings,
        non_claims: REPORT_NON_CLAIMS,
        reason_code: body.reason_code,
        next_step: body.next_step,
        evaluator: EVALUATOR,
    }
}

fn stage1_fail_report(
    detail: String,
    reason: ReasonCode,
    bundle: &Path,
    profile_version: ProfileVersion,
    profile_selection: ProfileSelection,
) -> Report {
    // Caller argv is safe to republish shell-free, unlike discovered host paths.
    emit_report(
        profile_version,
        profile_selection,
        ReportBody {
            bundle_integrity: "fail",
            verdict: None,
            claims: None,
            findings: vec![Finding {
                id: "bundle_integrity".to_string(),
                detail,
                observed_schema: None,
            }],
            reason_code: Some(reason.as_str()),
            next_step: Some(reason.next_step(bundle.to_str())),
        },
    )
}

fn integrity_fail_report(
    err: &anyhow::Error,
    bundle: &Path,
    profile_version: ProfileVersion,
    profile_selection: ProfileSelection,
) -> Report {
    let reason = reason_code_for_evidence_error(err)
        .expect("stage-1 evidence errors must map to a ReasonCode");
    debug_assert!(
        PROFILE_EVIDENCE_REASON_CODES
            .iter()
            .any(|(_, owned)| *owned == reason),
        "stage-1 reason must be one of the binary-owned profile codes"
    );
    stage1_fail_report(
        format!("{err:#}"),
        reason,
        bundle,
        profile_version,
        profile_selection,
    )
}

/// Stages 2-4 over the events of a bundle that already passed stage 1.
/// Default profile is v0 (explicit selection lives on `profile_report_for_selection`).
#[cfg(test)]
pub fn profile_report(events: &[EvidenceEvent]) -> Report {
    profile_report_for(events, ProfileVersion::V0)
}

/// Stages 2-4 for a selected profile version. No autodetect.
/// Unit tests that do not go through clap use `ProfileSelection::Default`.
#[cfg(test)]
pub fn profile_report_for(events: &[EvidenceEvent], profile_version: ProfileVersion) -> Report {
    profile_report_for_selection(events, profile_version, ProfileSelection::Default)
}

fn profile_report_for_selection(
    events: &[EvidenceEvent],
    profile_version: ProfileVersion,
    profile_selection: ProfileSelection,
) -> Report {
    let mut violations: Vec<Finding> = Vec::new();

    // Select profile events by the exact schema member of their payload.
    let mut decisions: Vec<&Value> = Vec::new();
    let mut observations: Vec<&Value> = Vec::new();
    let mut establishes: Vec<&Value> = Vec::new();
    for ev in events {
        let schema = ev.payload.get("schema").and_then(Value::as_str);
        match schema {
            Some(s) if in_namespace(s) => {
                if ev.type_ != s {
                    violations.push(finding(
                        "event_type_schema_mismatch",
                        format!(
                            "event {} has type {:?} but its payload declares schema {s:?}; the two must be equal",
                            ev.id, ev.type_
                        ),
                    ));
                }
                match s {
                    DECISION_SCHEMA => decisions.push(&ev.payload),
                    ESTABLISH_SCHEMA => establishes.push(&ev.payload),
                    other if other == profile_version.observation_schema() => {
                        observations.push(&ev.payload)
                    }
                    other => violations.push(unknown_schema_finding(
                        other,
                        format!(
                            "payload schema {other:?} is inside the profile namespace but is not a recognized {} record; unknown fails closed",
                            profile_version.as_profile_id()
                        ),
                    )),
                }
            }
            // In-namespace event TYPE without an in-namespace payload schema: the profile selects
            // by the payload's schema member, so an envelope claiming a profile type that its
            // payload does not declare is not an ignorable outside-the-profile event. Fail closed.
            _ if in_namespace(&ev.type_) => violations.push(finding(
                UNKNOWN_PROFILE_SCHEMA,
                format!(
                    "event {} has profile-namespace type {:?} but its payload does not declare that profile schema",
                    ev.id, ev.type_
                ),
            )),
            _ => {} // Outside the profile namespace: ignored (integrity still covered by stage 1).
        }
    }

    // Stage 2: cardinality.
    if decisions.len() != 1 {
        violations.push(finding(
            "decision_cardinality",
            format!(
                "exactly one assay.enforcement_decision.v0 payload is required, found {}; v0 is single-call by design",
                decisions.len()
            ),
        ));
    }
    if observations.len() > 1 {
        violations.push(finding(
            "observation_cardinality",
            format!(
                "at most one {} payload is allowed, found {}",
                profile_version.observation_schema(),
                observations.len()
            ),
        ));
    }
    if establishes.len() > 1 {
        violations.push(finding(
            "establish_cardinality",
            format!(
                "at most one assay.manifest_establish.v0 payload is allowed, found {}",
                establishes.len()
            ),
        ));
    }

    // Stage 2: record constraints (only meaningful on the single instance of each record kind).
    let decision = (decisions.len() == 1).then(|| decisions[0]);
    let observation = (observations.len() == 1).then(|| observations[0]);
    let establish = (establishes.len() == 1).then(|| establishes[0]);

    if let Some(dec) = decision {
        check_decision(dec, &mut violations);
    }
    if let Some(obs) = observation {
        check_observation(obs, &mut violations);
    }
    if let Some(est) = establish {
        check_establish(est, &mut violations);
    }

    // Stage 3: binding validity. An observation participates as a caller-visible denial marker only
    // if the shared classifier returns the selected profile's exact triple.
    let marker = observation
        .filter(|obs| classify_denial_marker(obs) == Some(profile_version.marker_version()));
    let mut bound_marker = false;
    if let Some(obs) = marker {
        let obs_tool = obs
            .get("call")
            .and_then(|c| c.get("tool_name"))
            .and_then(Value::as_str);
        let obs_digest = obs
            .get("call")
            .and_then(|c| c.get("target_digest"))
            .and_then(Value::as_str)
            .filter(|d| !d.is_empty());
        match (decision, obs_digest) {
            (None, _) => violations.push(finding(
                "marker_not_backed",
                "a caller-visible denial marker is present with no decision record to back it"
                    .to_string(),
            )),
            (Some(_), None) => violations.push(finding(
                "marker_digest_unbindable",
                "the marker's call.target_digest is null or empty, so it cannot bind to any decision"
                    .to_string(),
            )),
            (Some(dec), Some(obs_digest)) => {
                let dec_tool = dec
                    .get("tool")
                    .and_then(|t| t.get("name"))
                    .and_then(Value::as_str);
                let dec_digest = dec
                    .get("action")
                    .and_then(|a| a.get("target_digest"))
                    .and_then(Value::as_str);
                // Exact byte equality on both binding members; digests are never re-encoded.
                if obs_tool.is_some() && obs_tool == dec_tool && Some(obs_digest) == dec_digest {
                    bound_marker = true;
                } else {
                    violations.push(finding(
                        "observation_binding",
                        "the marker's (call.tool_name, call.target_digest) does not equal the decision's (tool.name, action.target_digest)"
                            .to_string(),
                    ));
                }
            }
        }
    }

    if !violations.is_empty() {
        return emit_report(
            profile_version,
            profile_selection,
            ReportBody {
                bundle_integrity: "pass",
                verdict: Some("invalid"),
                claims: None,
                next_step: Some(profile_invalid_next_step(
                    &violations,
                    observations.len(),
                    profile_version,
                    profile_selection,
                )),
                findings: violations,
                reason_code: Some(ReasonCode::EEvidenceProfileInvalid.as_str()),
            },
        );
    }

    // Stage 4: claim recompute. From here the records are validated; the matrix is a deterministic
    // function of (decision, bound marker), and the establish record changes no cell.
    let mut notes: Vec<Finding> = Vec::new();
    let dec = decision.expect("valid verdict implies exactly one decision");
    let decided = dec
        .get("decision")
        .and_then(Value::as_str)
        .expect("valid verdict implies vocabulary-checked decision");

    let caller_visible_denial = match (decided, bound_marker) {
        ("deny", true) => ClaimCell::confirmed(),
        ("deny", false) | ("allow", false) => ClaimCell::incomplete(),
        ("allow", true) => {
            notes.push(finding(
                "caller_visible_outcome_contradiction",
                "the producer recorded an allow decision and a caller-visible denial for the same (tool, target); the caller-visible outcome is refuted"
                    .to_string(),
            ));
            ClaimCell::refuted()
        }
        _ => unreachable!("decision vocabulary is closed"),
    };

    if let Some(est) = establish {
        let path = est
            .get("establish_path")
            .and_then(Value::as_str)
            .unwrap_or_default();
        let contradiction = match decided {
            "allow" => matches!(path, "immediate_deny" | "established_then_denied"),
            "deny" => path == "established_then_allowed",
            _ => false,
        };
        if contradiction {
            notes.push(finding(
                "establish_journey_contradiction",
                format!(
                    "establish_path {path:?} contradicts the recorded {decided:?} decision; the journey is diagnostic only and the matrix is unchanged"
                ),
            ));
        }
    }

    emit_report(
        profile_version,
        profile_selection,
        ReportBody {
            bundle_integrity: "pass",
            verdict: Some("valid"),
            claims: Some(Claims {
                policy_decision_recorded: ClaimCell::confirmed(),
                caller_visible_denial,
                upstream_delivery: ClaimCell::incomplete(),
                external_side_effect: ClaimCell::incomplete(),
            }),
            findings: notes,
            reason_code: None,
            next_step: None,
        },
    )
}

fn check_decision(dec: &Value, violations: &mut Vec<Finding>) {
    check_vocab(dec, "decision", DECISION_VOCAB, violations);
    check_vocab(dec, "reason", REASON_VOCAB, violations);
    check_vocab(dec, "drift_state", DRIFT_STATE_VOCAB, violations);

    let tool_name = dec
        .get("tool")
        .and_then(|t| t.get("name"))
        .and_then(Value::as_str);
    if tool_name.map(str::is_empty).unwrap_or(true) {
        violations.push(finding(
            "decision_tool_name",
            "decision tool.name must be a non-empty string".to_string(),
        ));
    }

    let digest = dec
        .get("action")
        .and_then(|a| a.get("target_digest"))
        .and_then(Value::as_str);
    if !digest.map(is_sha256_digest).unwrap_or(false) {
        violations.push(finding(
            "target_digest_missing",
            "decision action.target_digest must be sha256:<64 lowercase hex>; a null digest cannot be bound and falls outside the profile"
                .to_string(),
        ));
    }

    // fail_closed is derived by the producer (true iff deny); divergence is malformation, not an
    // alternative policy statement. Only checkable once the decision value itself is in vocabulary.
    match (
        dec.get("decision").and_then(Value::as_str),
        dec.get("fail_closed").and_then(Value::as_bool),
    ) {
        (Some(d), Some(fc)) if DECISION_VOCAB.contains(&d) => {
            if fc != (d == "deny") {
                violations.push(finding(
                    "fail_closed_derivation",
                    format!("fail_closed is {fc} but the decision is {d:?}; fail_closed must equal (decision == \"deny\")"),
                ));
            }
        }
        (Some(d), None) if DECISION_VOCAB.contains(&d) => violations.push(finding(
            "fail_closed_derivation",
            "decision fail_closed must be a boolean".to_string(),
        )),
        _ => {} // The decision-vocabulary violation is already recorded.
    }

    check_non_claims(
        dec,
        DECISION_PRODUCER_NON_CLAIMS,
        "decision_non_claims",
        violations,
    );
}

fn check_observation(obs: &Value, violations: &mut Vec<Finding>) {
    let tool_name = obs
        .get("call")
        .and_then(|c| c.get("tool_name"))
        .and_then(Value::as_str);
    if tool_name.map(str::is_empty).unwrap_or(true) {
        violations.push(finding(
            "observation_tool_name",
            "observation call.tool_name must be a non-empty string".to_string(),
        ));
    }

    let error = obs.get("caller_visible_error");
    for member in ["code", "origin", "reason"] {
        let present = error
            .and_then(|e| e.get(member))
            .map(|v| !v.is_null())
            .unwrap_or(false);
        if !present {
            violations.push(finding(
                "observation_error_member",
                format!("observation caller_visible_error.{member} must be present"),
            ));
        }
    }

    let digest = obs
        .get("caller_visible_response_digest")
        .and_then(Value::as_str);
    if !digest.map(is_sha256_digest).unwrap_or(false) {
        violations.push(finding(
            "observation_response_digest",
            "observation caller_visible_response_digest must be sha256:<64 lowercase hex>"
                .to_string(),
        ));
    }

    check_non_claims(
        obs,
        OBSERVATION_PRODUCER_NON_CLAIMS,
        "observation_non_claims",
        violations,
    );
}

fn check_establish(est: &Value, violations: &mut Vec<Finding>) {
    check_vocab(est, "establish_path", ESTABLISH_PATH_VOCAB, violations);

    // establish_attempted must equal (run_outcome != "not_performed"); both members are required
    // for the equality to be checkable, so a missing member fails closed.
    match (
        est.get("establish_attempted").and_then(Value::as_bool),
        est.get("run_outcome").and_then(Value::as_str),
    ) {
        (Some(attempted), Some(outcome)) => {
            if attempted != (outcome != "not_performed") {
                violations.push(finding(
                    "establish_attempted_derivation",
                    format!(
                        "establish_attempted is {attempted} but run_outcome is {outcome:?}; establish_attempted must equal (run_outcome != \"not_performed\")"
                    ),
                ));
            }
        }
        _ => violations.push(finding(
            "establish_attempted_derivation",
            "establish record must carry boolean establish_attempted and string run_outcome"
                .to_string(),
        )),
    }
}

fn check_vocab(record: &Value, member: &str, vocab: &[&str], violations: &mut Vec<Finding>) {
    match record.get(member).and_then(Value::as_str) {
        Some(value) if vocab.contains(&value) => {}
        Some(value) => violations.push(finding(
            &format!("{member}_vocabulary"),
            format!(
                "{member} {value:?} is outside the closed set {}",
                vocab.join("|")
            ),
        )),
        None => violations.push(finding(
            &format!("{member}_vocabulary"),
            format!(
                "{member} must be a string from the closed set {}",
                vocab.join("|")
            ),
        )),
    }
}

/// Subset test: the record's `non_claims` array must contain every producer string verbatim
/// (byte-exact); extra entries are allowed.
fn check_non_claims(record: &Value, required: &[&str], id: &str, violations: &mut Vec<Finding>) {
    let Some(array) = record.get("non_claims").and_then(Value::as_array) else {
        violations.push(finding(
            id,
            "non_claims must be an array carrying the producer strings verbatim".to_string(),
        ));
        return;
    };
    for required_entry in required {
        let present = array.iter().any(|v| v.as_str() == Some(*required_entry));
        if !present {
            violations.push(finding(
                id,
                format!("non_claims is missing the producer string {required_entry:?}"),
            ));
        }
    }
}

fn in_namespace(schema: &str) -> bool {
    PROFILE_NAMESPACES.iter().any(|p| schema.starts_with(p))
}

fn is_sha256_digest(value: &str) -> bool {
    match value.strip_prefix("sha256:") {
        Some(hex) => hex.len() == 64 && hex.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')),
        None => false,
    }
}

/// Guidance for a rejection whose only cause is another interpreter's observation record.
const INTERPRETER_CHECK_NEXT_STEP: &str = "Check the selected interpreter against the producer contract or your required profile. Do not switch profiles solely to obtain a passing result. If the selection is intended, resolve the incompatible records with the producer.";

/// Remediation for a stage-1 pass with an invalid profile verdict.
///
/// Reissue guidance is the rule. The interpreter check replaces it only when the rejection is
/// fully explained by observation records of another shipped interpreter of this profile: every
/// violation is an unknown schema carrying a typed `observed_schema`, each of those is another
/// interpreter's observation schema, and the selected interpreter's own observation is absent
/// (mixed versions are rejected by every selection). The text states what was selected and never
/// names an alternative: the bundle carries no profile id, so the intended interpreter is known
/// to the producer contract or the caller, not to this verifier.
fn profile_invalid_next_step(
    violations: &[Finding],
    selected_observations: usize,
    selected: ProfileVersion,
    profile_selection: ProfileSelection,
) -> String {
    let other_interpreter_observation = |violation: &Finding| {
        violation.id == UNKNOWN_PROFILE_SCHEMA
            && violation
                .observed_schema
                .as_deref()
                .is_some_and(|observed| {
                    ProfileVersion::value_variants()
                        .iter()
                        .any(|other| *other != selected && other.observation_schema() == observed)
                })
    };
    let interpreter_check = selected_observations == 0
        && !violations.is_empty()
        && violations.iter().all(other_interpreter_observation);
    if !interpreter_check {
        return ReasonCode::EEvidenceProfileInvalid.next_step(None);
    }
    match profile_selection {
        ProfileSelection::Explicit => INTERPRETER_CHECK_NEXT_STEP.to_string(),
        // Describes argv, not the bundle. Names the interpreter that ran, so the sentence
        // cannot outlive a change of default.
        ProfileSelection::Default => format!(
            "No --profile-version was given; {} was applied by default. {INTERPRETER_CHECK_NEXT_STEP}",
            selected.cli_value()
        ),
    }
}

const UNKNOWN_PROFILE_SCHEMA: &str = "unknown_profile_schema";

fn finding(id: &str, detail: String) -> Finding {
    Finding {
        id: id.to_string(),
        detail,
        observed_schema: None,
    }
}

fn unknown_schema_finding(observed_schema: &str, detail: String) -> Finding {
    Finding {
        id: UNKNOWN_PROFILE_SCHEMA.to_string(),
        detail,
        observed_schema: Some(observed_schema.to_string()),
    }
}

fn print_table(report: &Report) {
    println!("Privileged MCP Action Verification ({})", report.profile);
    println!("=====================================================");
    println!("Profile selection: {}", report.profile_selection.as_str());
    println!(
        "Input profile:     {} ({})",
        report.input_profile_status,
        match report.input_profile {
            None => "none",
            Some(id) => id,
        }
    );
    println!(
        "Evaluator (self-reported): {} {}",
        report.evaluator.name, report.evaluator.version
    );
    println!("Bundle integrity: {}", report.bundle_integrity);
    if let Some(verdict) = report.verdict {
        println!("Verdict:          {verdict}");
    }
    if let Some(reason) = report.reason_code {
        println!("Reason code:      {reason}");
    }
    if let Some(next_step) = &report.next_step {
        println!("Next step:        {next_step}");
    }
    if let Some(claims) = &report.claims {
        println!();
        println!("Claim matrix:");
        for (name, cell) in [
            ("policy_decision_recorded", &claims.policy_decision_recorded),
            ("caller_visible_denial", &claims.caller_visible_denial),
            ("upstream_delivery", &claims.upstream_delivery),
            ("external_side_effect", &claims.external_side_effect),
        ] {
            match cell.source_class {
                Some(sc) => println!("  {name:<26} {:<10} ({sc})", cell.status),
                None => println!("  {name:<26} {}", cell.status),
            }
        }
    }
    println!();
    if report.findings.is_empty() {
        println!("Findings: none");
    } else {
        println!("Findings:");
        for f in &report.findings {
            println!("  {:<36} {}", f.id, f.detail);
        }
    }
    println!();
    println!("Non-claims:");
    for nc in &report.non_claims {
        println!("  - {nc}");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    const TOOL: &str = "github.add_deploy_key";
    const DIGEST: &str = "sha256:c3ff823d7fb2ee33b9f1a3f7be6eaf849acb980b6ec960731506436b56384dfc";

    fn decision_payload(decision: &str) -> Value {
        json!({
            "schema": DECISION_SCHEMA,
            "caller": {"id": "ci-agent"},
            "tool": {"name": TOOL, "action_class": "github_deploy_key"},
            "action": {
                "verb": "create",
                "resource_type": "github_deploy_key",
                "target": {"provider": "github", "owner": "acme", "repo": "prod-app"},
                "target_digest": DIGEST,
            },
            "decision": decision,
            "reason": if decision == "allow" { "allow" } else { "no_declared_allowance" },
            "fail_closed": decision == "deny",
            "drift_state": if decision == "allow" { "satisfied" } else { "not_evaluated" },
            "credential_alias": "gh-deploy",
            "non_claims": DECISION_PRODUCER_NON_CLAIMS,
        })
    }

    fn observation_payload() -> Value {
        json!({
            "schema": OBSERVATION_SCHEMA,
            "call": {"tool_name": TOOL, "target_digest": DIGEST},
            "caller_visible_error": {
                "code": -32042,
                "origin": "assay-proxy",
                "reason": "no_declared_allowance",
            },
            "caller_visible_response_digest":
                "sha256:ef5796d82cdedf4727caa82604687648c081271ac60e355a57089550a431f48b",
            "non_claims": OBSERVATION_PRODUCER_NON_CLAIMS,
        })
    }

    fn observation_payload_v1() -> Value {
        let mut obs = observation_payload();
        obs["schema"] = json!(OBSERVATION_SCHEMA_V1);
        obs["caller_visible_error"]["code"] = json!(-31999);
        obs
    }

    fn establish_payload(path: &str) -> Value {
        json!({
            "schema": ESTABLISH_SCHEMA,
            "establish_path": path,
            "establish_attempted": true,
            "action_class": "github_deploy_key",
            "run_outcome": "complete",
        })
    }

    fn ev(payload: Value, seq: u64) -> EvidenceEvent {
        let type_ = payload
            .get("schema")
            .and_then(Value::as_str)
            .unwrap()
            .to_string();
        EvidenceEvent::new(type_, "urn:assay:test", "run", seq, payload)
    }

    fn status(report: &Report, claim: &str) -> String {
        let claims = serde_json::to_value(report.claims.as_ref().unwrap()).unwrap();
        claims[claim]["status"].as_str().unwrap().to_string()
    }

    #[test]
    fn deny_with_bound_marker_confirms_both_confirmable_claims() {
        let report = profile_report(&[
            ev(decision_payload("deny"), 0),
            ev(observation_payload(), 1),
        ]);
        assert_eq!(report.verdict, Some("valid"));
        assert_eq!(status(&report, "policy_decision_recorded"), "confirmed");
        assert_eq!(status(&report, "caller_visible_denial"), "confirmed");
        assert_eq!(status(&report, "upstream_delivery"), "incomplete");
        assert_eq!(status(&report, "external_side_effect"), "incomplete");
        assert!(report.findings.is_empty());
    }

    #[test]
    fn allow_with_bound_marker_refutes_caller_visible_outcome() {
        let report = profile_report(&[
            ev(decision_payload("allow"), 0),
            ev(observation_payload(), 1),
        ]);
        assert_eq!(report.verdict, Some("valid"));
        assert_eq!(status(&report, "caller_visible_denial"), "refuted");
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "caller_visible_outcome_contradiction"));
    }

    #[test]
    fn incomplete_cells_carry_no_source_class() {
        let report = profile_report(&[ev(decision_payload("deny"), 0)]);
        let claims = serde_json::to_value(report.claims.as_ref().unwrap()).unwrap();
        assert!(claims["caller_visible_denial"]
            .get("source_class")
            .is_none());
        assert_eq!(
            claims["policy_decision_recorded"]["source_class"],
            json!("producer_reported")
        );
    }

    #[test]
    fn two_decisions_are_invalid_never_paired() {
        let report = profile_report(&[
            ev(decision_payload("deny"), 0),
            ev(decision_payload("allow"), 1),
        ]);
        assert_eq!(report.verdict, Some("invalid"));
        assert!(report.claims.is_none());
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "decision_cardinality"));
    }

    #[test]
    fn unknown_in_namespace_schema_fails_closed() {
        let report = profile_report(&[
            ev(decision_payload("deny"), 0),
            ev(
                json!({"schema": "assay.enforcement_decision.v1", "decision": "deny"}),
                1,
            ),
        ]);
        assert_eq!(report.verdict, Some("invalid"));
        let unknown = report
            .findings
            .iter()
            .find(|f| f.id == "unknown_profile_schema")
            .expect("unknown_profile_schema");
        assert_eq!(
            unknown.observed_schema.as_deref(),
            Some("assay.enforcement_decision.v1")
        );
    }

    #[test]
    fn fail_closed_divergence_is_invalid() {
        let mut dec = decision_payload("deny");
        dec["fail_closed"] = json!(false);
        let report = profile_report(&[ev(dec, 0)]);
        assert_eq!(report.verdict, Some("invalid"));
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "fail_closed_derivation"));
    }

    #[test]
    fn missing_producer_non_claim_is_invalid() {
        let mut dec = decision_payload("deny");
        dec["non_claims"] = json!(DECISION_PRODUCER_NON_CLAIMS[..4].to_vec());
        let report = profile_report(&[ev(dec, 0)]);
        assert_eq!(report.verdict, Some("invalid"));
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "decision_non_claims"));
    }

    #[test]
    fn marker_binding_mismatch_is_invalid() {
        let mut obs = observation_payload();
        obs["call"]["target_digest"] =
            json!("sha256:0000000000000000000000000000000000000000000000000000000000000000");
        let report = profile_report(&[ev(decision_payload("deny"), 0), ev(obs, 1)]);
        assert_eq!(report.verdict, Some("invalid"));
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "observation_binding"));
    }

    #[test]
    fn marker_with_null_digest_is_unbindable() {
        let mut obs = observation_payload();
        obs["call"]["target_digest"] = Value::Null;
        let report = profile_report(&[ev(decision_payload("deny"), 0), ev(obs, 1)]);
        assert_eq!(report.verdict, Some("invalid"));
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "marker_digest_unbindable"));
    }

    #[test]
    fn establish_contradiction_is_a_finding_not_a_cell_change() {
        let report = profile_report(&[
            ev(decision_payload("allow"), 0),
            ev(establish_payload("immediate_deny"), 1),
        ]);
        assert_eq!(report.verdict, Some("valid"));
        assert_eq!(status(&report, "caller_visible_denial"), "incomplete");
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "establish_journey_contradiction"));
    }

    #[test]
    fn establish_attempted_divergence_is_invalid() {
        let mut est = establish_payload("no_establish_needed");
        est["run_outcome"] = json!("not_performed");
        let report = profile_report(&[ev(decision_payload("allow"), 0), ev(est, 1)]);
        assert_eq!(report.verdict, Some("invalid"));
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "establish_attempted_derivation"));
    }

    #[test]
    fn report_always_carries_the_four_fixed_non_claims() {
        let report = profile_report(&[ev(decision_payload("deny"), 0)]);
        let value = serde_json::to_value(&report).unwrap();
        assert_eq!(value["non_claims"], json!(REPORT_NON_CLAIMS.to_vec()));
        assert_eq!(value["schema"], json!(REPORT_SCHEMA));
        assert_eq!(value["profile"], json!(PROFILE_ID));
    }

    #[test]
    fn invalid_report_omits_claims_member_entirely() {
        let report = profile_report(&[]);
        assert_eq!(report.verdict, Some("invalid"));
        let value = serde_json::to_value(&report).unwrap();
        assert!(value.get("claims").is_none());
    }

    #[test]
    fn stage1_diagnosis_consumes_reachable_limit_and_path_codes() {
        use assay_evidence::{ErrorClass, ErrorCode, VerifyError};

        for code in [
            ErrorCode::LimitBundleBytes,
            ErrorCode::LimitDecodeBytes,
            ErrorCode::LimitFileSize,
            ErrorCode::LimitLineBytes,
            ErrorCode::LimitTotalEvents,
            ErrorCode::LimitPathLength,
            ErrorCode::LimitJsonDepth,
        ] {
            let err = anyhow::Error::new(VerifyError::new(ErrorClass::Limits, code, "ceiling"));
            let value = serde_json::to_value(integrity_fail_report(
                &err,
                Path::new("synthetic.bundle"),
                ProfileVersion::V0,
                ProfileSelection::Default,
            ))
            .unwrap();
            assert_eq!(value["reason_code"], "E_EVIDENCE_LIMIT_EXCEEDED", "{code}");
            assert_ne!(value["reason_code"], "E_EVIDENCE_INTEGRITY", "{code}");
            assert_ne!(value["reason_code"], "E_EVIDENCE_CONTRACT", "{code}");
            assert_ne!(value["reason_code"], "E_EVIDENCE_UNREADABLE", "{code}");
            assert!(
                !value["next_step"].as_str().unwrap_or("").is_empty(),
                "{code}: emitted next_step must be non-empty"
            );
            assert!(value.get("claims").is_none(), "{code}");
        }

        let path_err = anyhow::Error::new(VerifyError::new(
            ErrorClass::Security,
            ErrorCode::SecurityPathTraversal,
            "path",
        ));
        let path_value = serde_json::to_value(integrity_fail_report(
            &path_err,
            Path::new("synthetic.bundle"),
            ProfileVersion::V0,
            ProfileSelection::Default,
        ))
        .unwrap();
        assert_eq!(path_value["reason_code"], "E_EVIDENCE_PATH_REJECTED");
        assert_ne!(path_value["reason_code"], "E_EVIDENCE_LIMIT_EXCEEDED");
        assert!(
            !path_value["next_step"].as_str().unwrap_or("").is_empty(),
            "path next_step must be non-empty"
        );
    }

    fn assert_unreadable_stage1(report: &Report, bundle: &Path) {
        let value = serde_json::to_value(report).unwrap();
        let path = bundle.to_str().expect("utf-8 test path");
        assert_eq!(value["schema"], REPORT_SCHEMA);
        assert_ne!(value["schema"], "assay.run_summary.v1");
        assert_eq!(value["profile"], PROFILE_ID);
        assert_eq!(value["bundle_integrity"], "fail");
        assert!(value.get("verdict").is_none());
        assert!(value.get("claims").is_none());
        assert_eq!(value["reason_code"], "E_EVIDENCE_UNREADABLE");
        let detail = value["findings"][0]["detail"]
            .as_str()
            .expect("bundle_integrity finding detail");
        assert!(
            detail.contains(path),
            "findings.detail must contain the caller bundle argv: {detail}"
        );
        // Caller argv is safe to republish shell-free, unlike discovered host paths.
        let expected_next = ReasonCode::EEvidenceUnreadable.next_step(Some(path));
        assert_eq!(value["next_step"].as_str(), Some(expected_next.as_str()));
        assert!(
            expected_next.starts_with("Run argv:"),
            "unreadable next_step must be JSON argv, not a shell string"
        );
        assert!(
            expected_next.contains(r#""--""#),
            "unreadable next_step must keep the positional separator: {expected_next}"
        );
        assert_eq!(value["non_claims"], json!(REPORT_NON_CLAIMS.to_vec()));
    }

    #[test]
    fn missing_bundle_path_is_unreadable_stage1_report() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let missing = tmp.path().join("missing.bundle.tar.gz");
        assert!(!missing.exists(), "the missing-bundle child must not exist");
        assert_unreadable_stage1(&verify_bundle_report(&missing), &missing);
    }

    #[test]
    fn directory_bundle_path_is_unreadable_stage1_report() {
        let tmp = tempfile::tempdir().expect("tempdir");
        assert_unreadable_stage1(&verify_bundle_report(tmp.path()), tmp.path());
    }

    #[test]
    fn post_open_unreadable_bundle_detail_contains_caller_argv() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let garbage = tmp.path().join("not-a-bundle.tar.gz");
        std::fs::write(&garbage, b"not a gzip archive").expect("write garbage bundle");
        assert_unreadable_stage1(&verify_bundle_report(&garbage), &garbage);
    }

    #[test]
    fn success_report_omits_diagnosis_fields() {
        let report = profile_report(&[ev(decision_payload("deny"), 0)]);
        let value = serde_json::to_value(&report).unwrap();
        assert!(value.get("reason_code").is_none());
        assert!(value.get("next_step").is_none());
    }

    #[test]
    fn default_profile_without_observation_is_v0() {
        let report = profile_report(&[ev(decision_payload("deny"), 0)]);
        assert_eq!(report.schema, REPORT_SCHEMA);
        assert_eq!(report.profile, PROFILE_ID);
        assert_eq!(status(&report, "caller_visible_denial"), "incomplete");
    }

    #[test]
    fn explicit_v1_without_observation_reports_v1() {
        let report = profile_report_for(&[ev(decision_payload("deny"), 0)], ProfileVersion::V1);
        assert_eq!(report.schema, REPORT_SCHEMA);
        assert_eq!(report.profile, PROFILE_ID_V1);
        assert_eq!(status(&report, "caller_visible_denial"), "incomplete");
    }

    #[test]
    fn v1_bound_marker_confirms_caller_visible_denial() {
        let report = profile_report_for(
            &[
                ev(decision_payload("deny"), 0),
                ev(observation_payload_v1(), 1),
            ],
            ProfileVersion::V1,
        );
        assert_eq!(report.verdict, Some("valid"));
        assert_eq!(report.profile, PROFILE_ID_V1);
        assert_eq!(status(&report, "caller_visible_denial"), "confirmed");
    }

    #[test]
    fn v1_observation_under_default_v0_fails_closed() {
        let report = profile_report(&[
            ev(decision_payload("deny"), 0),
            ev(observation_payload_v1(), 1),
        ]);
        assert_eq!(report.profile, PROFILE_ID);
        assert_eq!(report.verdict, Some("invalid"));
        let unknown = report
            .findings
            .iter()
            .find(|f| f.id == "unknown_profile_schema")
            .expect("unknown_profile_schema");
        assert_eq!(
            unknown.observed_schema.as_deref(),
            Some("assay.denied_call_observation.v1")
        );
    }

    #[test]
    fn v0_observation_under_v1_fails_closed() {
        let report = profile_report_for(
            &[
                ev(decision_payload("deny"), 0),
                ev(observation_payload(), 1),
            ],
            ProfileVersion::V1,
        );
        assert_eq!(report.profile, PROFILE_ID_V1);
        assert_eq!(report.verdict, Some("invalid"));
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "unknown_profile_schema"));
    }

    #[test]
    fn mixed_v0_and_v1_observations_fail_closed() {
        let report = profile_report(&[
            ev(decision_payload("deny"), 0),
            ev(observation_payload(), 1),
            ev(observation_payload_v1(), 2),
        ]);
        assert_eq!(report.verdict, Some("invalid"));
        assert!(report
            .findings
            .iter()
            .any(|f| f.id == "unknown_profile_schema"));
    }

    #[test]
    fn v1_schema_with_legacy_code_is_inert_under_v1() {
        let mut obs = observation_payload_v1();
        obs["caller_visible_error"]["code"] = json!(-32042);
        let report = profile_report_for(
            &[ev(decision_payload("deny"), 0), ev(obs, 1)],
            ProfileVersion::V1,
        );
        assert_eq!(report.verdict, Some("valid"));
        assert_eq!(status(&report, "caller_visible_denial"), "incomplete");
    }

    fn claim_matrix(report: &Report) -> Value {
        serde_json::to_value(report.claims.as_ref().expect("valid report has claims")).unwrap()
    }

    /// Only named members of a recognized record are read; additional members are ignored at top
    /// level and nested, and change neither the verdict nor any cell.
    #[test]
    fn additional_members_in_recognized_records_are_ignored() {
        for decided in ["deny", "allow"] {
            let plain = profile_report(&[
                ev(decision_payload(decided), 0),
                ev(observation_payload(), 1),
                ev(establish_payload("no_establish_needed"), 2),
            ]);

            let mut dec = decision_payload(decided);
            dec["future_member"] = json!({"any": "value"});
            dec["tool"]["future_member"] = json!(1);
            dec["action"]["future_member"] = json!(null);
            let mut obs = observation_payload();
            obs["future_member"] = json!(["x"]);
            obs["call"]["future_member"] = json!(true);
            obs["caller_visible_error"]["future_member"] = json!("y");
            let mut est = establish_payload("no_establish_needed");
            est["future_member"] = json!(0);
            let extended = profile_report(&[ev(dec, 0), ev(obs, 1), ev(est, 2)]);

            assert_eq!(extended.verdict, Some("valid"), "{decided}");
            assert_eq!(claim_matrix(&extended), claim_matrix(&plain), "{decided}");
            assert_eq!(
                extended.findings.iter().map(|f| &f.id).collect::<Vec<_>>(),
                plain.findings.iter().map(|f| &f.id).collect::<Vec<_>>(),
                "{decided}"
            );
        }
    }

    /// Producer non-claims are a subset requirement: additional entries are accepted.
    #[test]
    fn additional_producer_non_claims_are_accepted() {
        let mut dec = decision_payload("deny");
        dec["non_claims"]
            .as_array_mut()
            .unwrap()
            .push(json!("an additional producer statement"));
        let mut obs = observation_payload();
        obs["non_claims"]
            .as_array_mut()
            .unwrap()
            .insert(0, json!("an additional producer statement"));
        let report = profile_report(&[ev(dec, 0), ev(obs, 1)]);
        assert_eq!(report.verdict, Some("valid"));
        assert_eq!(status(&report, "caller_visible_denial"), "confirmed");
        assert!(report.findings.is_empty());
    }
}