assay-cli 3.26.0

Policy-as-code gate for MCP agent tool calls, with verifiable evidence and Linux kernel enforcement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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
use anyhow::{Context, Result};
use clap::{ArgGroup, Args, ValueEnum};
use serde::Serialize;
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Args, Clone)]
#[command(group(
    ArgGroup::new("binding_input")
        .required(true)
        .args(["attestation", "request_envelope"])
))]
pub struct McpExecutionRecordArgs {
    /// SEP-2787 attestation JSON fixture
    #[arg(long)]
    pub attestation: Option<PathBuf>,

    /// Observed tools/call request envelope JSON fixture for no-attestation fallback
    #[arg(long)]
    pub request_envelope: Option<PathBuf>,

    /// Server-side decision record JSON fixture
    #[arg(long)]
    pub decision: PathBuf,

    /// Optional server-side outcome record JSON fixture
    #[arg(long)]
    pub outcome: Option<PathBuf>,

    /// For the no-attestation fallback, how the request-envelope binding digest is computed.
    /// `whole-envelope` (default) is the legacy compatibility mode: it digests the full JCS envelope.
    /// `named` is the named fallback projection mode: it digests only the `tools/call` params plus the
    /// `_meta.authorization_binding` block, so transport-local or observation-local `_meta` fields a
    /// gateway/provider can legitimately add or strip do not change the digest. Named mode is
    /// allowlist + fail-closed: if the binding block cannot be resolved the fallback case is
    /// non-conformant rather than silently hashing the whole envelope. Ignored for the SEP-2787 path.
    #[arg(long, value_enum, default_value_t = FallbackProjection::WholeEnvelope)]
    pub fallback_projection: FallbackProjection,

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

/// Self-describing id of the named fallback projection. A rename or rule change is an explicit
/// version bump (it tracks the in-progress SEP-2828 fallback-binding discussion), never a silent
/// reinterpretation. The binding block is read at `_meta.authorization_binding`.
const FALLBACK_PROJECTION_V0: &str = "assay.fallback_projection.v0";

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum FallbackProjection {
    /// Legacy compatibility mode: digest the full JCS-canonical request envelope.
    WholeEnvelope,
    /// Named fallback projection mode: digest only the `tools/call` params plus the
    /// `_meta.authorization_binding` block (allowlist + fail-closed).
    Named,
}

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

#[derive(Debug, Serialize)]
struct PairingReport {
    schema: &'static str,
    ok: bool,
    canonicalization: &'static str,
    verification_scope: VerificationScope,
    binding: BindingReport,
    attestation: Option<AttestationReport>,
    decision: DecisionReport,
    outcome: Option<OutcomeReport>,
    checks: Vec<CheckReport>,
    claims_not_made: Vec<&'static str>,
}

#[derive(Debug, Serialize)]
struct VerificationScope {
    role: &'static str,
    note: &'static str,
}

#[derive(Debug, Serialize)]
struct AttestationReport {
    digest: String,
    nonce: Option<String>,
}

#[derive(Debug, Serialize)]
struct BindingReport {
    mode: &'static str,
    digest: String,
    digest_source: &'static str,
    /// Self-describing projection id for the named fallback; `None` for whole-envelope / attestation.
    projection: Option<&'static str>,
    nonce: Option<String>,
    nonce_source: &'static str,
}

#[derive(Debug, Serialize)]
struct DecisionReport {
    decision: Option<String>,
    decided_at: Option<String>,
    backlink: BackLinkReport,
    signature_present: bool,
}

#[derive(Debug, Serialize)]
struct OutcomeReport {
    status: Option<String>,
    completed_at: Option<String>,
    decision_digest: Option<String>,
    backlink: BackLinkReport,
    signature_present: bool,
}

#[derive(Debug, Serialize)]
struct BackLinkReport {
    attestation_digest: Option<String>,
    attestation_nonce: Option<String>,
}

#[derive(Debug, Serialize)]
struct CheckReport {
    id: &'static str,
    ok: bool,
    detail: String,
}

pub fn cmd_verify_mcp_records(args: McpExecutionRecordArgs) -> Result<i32> {
    let binding_input = match (&args.attestation, &args.request_envelope) {
        (Some(attestation), None) => BindingInput::Attestation(read_json(attestation)?),
        (None, Some(request_envelope)) => {
            BindingInput::RequestEnvelope(read_json(request_envelope)?)
        }
        _ => anyhow::bail!("exactly one of --attestation or --request-envelope is required"),
    };
    let decision = read_json(&args.decision)?;
    let outcome = args.outcome.as_ref().map(read_json).transpose()?;

    let report = build_report(
        &binding_input,
        &decision,
        outcome.as_ref(),
        args.fallback_projection,
    )?;
    match args.format {
        McpExecutionRecordFormat::Json => {
            println!("{}", serde_json::to_string_pretty(&report)?);
        }
        McpExecutionRecordFormat::Table => print_table_report(&report),
    }

    Ok(if report.ok { 0 } else { 2 })
}

fn read_json(path: &PathBuf) -> Result<Value> {
    let body =
        fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;
    serde_json::from_str(&body).with_context(|| format!("failed to parse {}", path.display()))
}

enum BindingInput {
    Attestation(Value),
    RequestEnvelope(Value),
}

struct BindingExpectation {
    mode: &'static str,
    digest: String,
    digest_source: &'static str,
    projection: Option<&'static str>,
    /// `Some(false)` when named projection was requested but the binding block could not be resolved
    /// (fail-closed); `None` when not applicable (whole-envelope / attestation).
    binding_block_present: Option<bool>,
    /// Stable reason code for the named-projection fail-closed case (None when present / N/A).
    named_fail_code: Option<&'static str>,
    nonce: Option<String>,
    nonce_source: &'static str,
}

fn build_report(
    binding_input: &BindingInput,
    decision: &Value,
    outcome: Option<&Value>,
    fallback_projection: FallbackProjection,
) -> Result<PairingReport> {
    let decision_digest = jcs_digest(decision).context("failed to digest decision")?;
    let decision_backlink = backlink_report(decision)?;
    let outcome_backlink = outcome.map(backlink_report).transpose()?;
    let expectation = binding_expectation(binding_input, &decision_backlink, fallback_projection)?;

    let mut checks = Vec::new();
    // Fail-closed: named projection requested but the binding block could not be resolved is
    // non-conformant, never a silent fall-back to hashing the whole envelope. The check id is the
    // stable reason code (invalid `_meta` vs missing `authorization_binding`).
    match (
        expectation.binding_block_present,
        expectation.named_fail_code,
    ) {
        (Some(true), _) => checks.push(CheckReport {
            id: "fallback_projection_binding_present",
            ok: true,
            detail: "named fallback projection binding block present".to_string(),
        }),
        (Some(false), Some(code)) => checks.push(CheckReport {
            id: code,
            ok: false,
            detail: "named fallback projection requested but the binding block could not be \
                     resolved; failing closed instead of hashing the whole envelope"
                .to_string(),
        }),
        _ => {}
    }
    push_decision_binding_checks(&mut checks, &decision_backlink, &expectation);
    checks.push(check_enum(
        "decision_enum",
        decision_value(decision).as_deref(),
        &["allow", "block", "escalate"],
    ));

    if let Some(outcome_backlink) = &outcome_backlink {
        push_outcome_binding_checks(&mut checks, outcome_backlink, &expectation);
        checks.push(check_eq(
            "decision_outcome_backlink_match",
            backlink_pair_key(outcome_backlink).as_deref(),
            backlink_pair_key(&decision_backlink).as_deref(),
            "decision and outcome describe the same call instance",
        ));
        // SEP-2828 Check B digests the full signed decision record.
        checks.push(check_eq(
            "outcome_decision_digest_match",
            outcome.and_then(outcome_decision_digest).as_deref(),
            Some(decision_digest.as_str()),
            "outcomeDerived.decisionDigest matches the signed decision record digest",
        ));
        checks.push(check_enum(
            "outcome_status_enum",
            outcome.and_then(outcome_status).as_deref(),
            &["executed", "refused", "errored"],
        ));
    } else {
        checks.push(CheckReport {
            id: "outcome_absent",
            ok: true,
            detail: "no outcome record supplied; report is decision-only".to_string(),
        });
    }

    let decision_report = DecisionReport {
        decision: decision_value(decision),
        decided_at: string_at(decision, &["decisionDerived", "decidedAt"]),
        backlink: decision_backlink,
        signature_present: decision.get("signature").and_then(Value::as_str).is_some(),
    };
    let outcome_report = match (outcome, outcome_backlink) {
        (Some(outcome), Some(backlink)) => Some(OutcomeReport {
            status: outcome_status(outcome),
            completed_at: string_at(outcome, &["outcomeDerived", "completedAt"]),
            decision_digest: outcome_decision_digest(outcome),
            backlink,
            signature_present: outcome.get("signature").and_then(Value::as_str).is_some(),
        }),
        _ => None,
    };

    let ok = checks.iter().all(|check| check.ok);
    Ok(PairingReport {
        schema: "assay.mcp.execution-record-pairing.report.v0",
        ok,
        canonicalization: "jcs/rfc8785",
        verification_scope: VerificationScope {
            role: "independent-consumer",
            note: "Assay verifies fixture pairing and digest commitments only; it does not emit records or act as a proxy.",
        },
        binding: BindingReport {
            mode: expectation.mode,
            digest: expectation.digest.clone(),
            digest_source: expectation.digest_source,
            projection: expectation.projection,
            nonce: expectation.nonce.clone(),
            nonce_source: expectation.nonce_source,
        },
        attestation: attestation_report(binding_input, &expectation),
        decision: decision_report,
        outcome: outcome_report,
        checks,
        claims_not_made: claims_not_made(&expectation),
    })
}

fn claims_not_made(expectation: &BindingExpectation) -> Vec<&'static str> {
    let mut claims = vec![
        "signature_verification",
        "issuer_key_trust",
        "policy_correctness",
        "runtime_side_effect_truth",
        "payload_or_result_disclosure",
    ];
    if expectation.mode == "request_envelope" {
        claims.push("fallback_server_observation_truth");
        claims.push("fallback_nonce_freshness_or_uniqueness");
    }
    claims
}

fn binding_expectation(
    binding_input: &BindingInput,
    decision_backlink: &BackLinkReport,
    fallback_projection: FallbackProjection,
) -> Result<BindingExpectation> {
    match binding_input {
        BindingInput::Attestation(attestation) => Ok(BindingExpectation {
            mode: "sep2787_attestation",
            digest: jcs_digest(attestation).context("failed to digest attestation")?,
            digest_source: "sep2787_attestation_jcs",
            projection: None,
            binding_block_present: None,
            named_fail_code: None,
            nonce: string_at(attestation, &["issuerAsserted", "nonce"]),
            nonce_source: "issuerAsserted.nonce",
        }),
        BindingInput::RequestEnvelope(request_envelope) => match fallback_projection {
            FallbackProjection::WholeEnvelope => Ok(BindingExpectation {
                mode: "request_envelope",
                digest: jcs_digest(request_envelope)
                    .context("failed to digest request envelope")?,
                digest_source: "request_envelope_jcs",
                projection: None,
                binding_block_present: None,
                named_fail_code: None,
                nonce: decision_backlink.attestation_nonce.clone(),
                nonce_source: "record_backlink_consistency",
            }),
            FallbackProjection::Named => {
                // Allowlist: the preimage is exactly the named params plus the whole named binding
                // block, everything else under _meta is excluded by construction. Fail-closed with a
                // stable reason when the binding cannot be resolved — never hash the whole envelope.
                // `_meta` absent or not an object -> invalid_meta; object but no binding -> missing.
                let meta = request_envelope.get("_meta");
                let (named_fail_code, binding): (Option<&'static str>, Option<&Value>) = match meta
                {
                    None => (Some("fallback_projection_invalid_meta"), None),
                    Some(m) if !m.is_object() => (Some("fallback_projection_invalid_meta"), None),
                    Some(m) => match m.get("authorization_binding") {
                        Some(b) => (None, Some(b)),
                        None => (
                            Some("fallback_projection_missing_authorization_binding"),
                            None,
                        ),
                    },
                };
                let digest = match binding {
                    Some(binding) => {
                        // The whole authorization_binding object is bound (bind-all); any field
                        // inside it is part of the preimage. The projection id is bound too.
                        let projected = serde_json::json!({
                            "projection": FALLBACK_PROJECTION_V0,
                            "params": request_envelope.get("params").unwrap_or(&Value::Null),
                            "binding": binding,
                        });
                        jcs_digest(&projected)
                            .context("failed to digest named fallback projection")?
                    }
                    None => "sha256:unresolved-binding-block".to_string(),
                };
                Ok(BindingExpectation {
                    mode: "request_envelope",
                    digest,
                    digest_source: "request_envelope_named_projection_jcs",
                    projection: Some(FALLBACK_PROJECTION_V0),
                    binding_block_present: Some(binding.is_some()),
                    named_fail_code,
                    nonce: decision_backlink.attestation_nonce.clone(),
                    nonce_source: "record_backlink_consistency",
                })
            }
        },
    }
}

fn attestation_report(
    binding_input: &BindingInput,
    expectation: &BindingExpectation,
) -> Option<AttestationReport> {
    match binding_input {
        BindingInput::Attestation(_) => Some(AttestationReport {
            digest: expectation.digest.clone(),
            nonce: expectation.nonce.clone(),
        }),
        BindingInput::RequestEnvelope(_) => None,
    }
}

fn push_decision_binding_checks(
    checks: &mut Vec<CheckReport>,
    decision_backlink: &BackLinkReport,
    expectation: &BindingExpectation,
) {
    match expectation.mode {
        "sep2787_attestation" => {
            checks.push(check_eq(
                "decision_attestation_digest_match",
                decision_backlink.attestation_digest.as_deref(),
                Some(expectation.digest.as_str()),
                "decision backLink.attestationDigest matches SEP-2787 JCS digest",
            ));
            checks.push(check_eq(
                "decision_attestation_nonce_match",
                decision_backlink.attestation_nonce.as_deref(),
                expectation.nonce.as_deref(),
                "decision backLink.attestationNonce matches issuerAsserted.nonce",
            ));
        }
        "request_envelope" => {
            checks.push(check_eq(
                "decision_request_envelope_digest_match",
                decision_backlink.attestation_digest.as_deref(),
                Some(expectation.digest.as_str()),
                "decision backLink.attestationDigest matches request-envelope JCS digest",
            ));
            checks.push(check_present(
                "decision_request_envelope_nonce_present",
                decision_backlink.attestation_nonce.as_deref(),
                "decision backLink.attestationNonce is present for fallback binding",
            ));
        }
        _ => unreachable!("unknown binding mode"),
    }
}

fn push_outcome_binding_checks(
    checks: &mut Vec<CheckReport>,
    outcome_backlink: &BackLinkReport,
    expectation: &BindingExpectation,
) {
    match expectation.mode {
        "sep2787_attestation" => {
            checks.push(check_eq(
                "outcome_attestation_digest_match",
                outcome_backlink.attestation_digest.as_deref(),
                Some(expectation.digest.as_str()),
                "outcome backLink.attestationDigest matches SEP-2787 JCS digest",
            ));
            checks.push(check_eq(
                "outcome_attestation_nonce_match",
                outcome_backlink.attestation_nonce.as_deref(),
                expectation.nonce.as_deref(),
                "outcome backLink.attestationNonce matches issuerAsserted.nonce",
            ));
        }
        "request_envelope" => {
            checks.push(check_eq(
                "outcome_request_envelope_digest_match",
                outcome_backlink.attestation_digest.as_deref(),
                Some(expectation.digest.as_str()),
                "outcome backLink.attestationDigest matches request-envelope JCS digest",
            ));
        }
        _ => unreachable!("unknown binding mode"),
    }
}

fn jcs_digest(value: &Value) -> Result<String> {
    let canonical = assay_core::mcp::jcs::to_vec(value)?;
    let hash = Sha256::digest(&canonical);
    Ok(format!("sha256:{}", hex::encode(hash)))
}

fn backlink_report(record: &Value) -> Result<BackLinkReport> {
    let backlink = record
        .get("backLink")
        .or_else(|| record.get("back_link"))
        .ok_or_else(|| anyhow::anyhow!("record missing backLink"))?;
    Ok(BackLinkReport {
        attestation_digest: string_at(backlink, &["attestationDigest"])
            .or_else(|| string_at(backlink, &["attestation_digest"])),
        attestation_nonce: string_at(backlink, &["attestationNonce"])
            .or_else(|| string_at(backlink, &["attestation_nonce"])),
    })
}

fn decision_value(record: &Value) -> Option<String> {
    string_at(record, &["decisionDerived", "decision"])
        .or_else(|| string_at(record, &["decision_derived", "decision"]))
}

fn outcome_status(record: &Value) -> Option<String> {
    string_at(record, &["outcomeDerived", "status"])
        .or_else(|| string_at(record, &["outcome_derived", "status"]))
}

fn outcome_decision_digest(record: &Value) -> Option<String> {
    string_at(record, &["outcomeDerived", "decisionDigest"])
        .or_else(|| string_at(record, &["outcome_derived", "decision_digest"]))
}

fn backlink_pair_key(backlink: &BackLinkReport) -> Option<String> {
    Some(format!(
        "attestationDigest={};attestationNonce={}",
        backlink.attestation_digest.as_deref()?,
        backlink.attestation_nonce.as_deref()?
    ))
}

fn string_at(value: &Value, path: &[&str]) -> Option<String> {
    let mut current = value;
    for segment in path {
        current = current.get(*segment)?;
    }
    current.as_str().map(ToOwned::to_owned)
}

fn check_eq(
    id: &'static str,
    left: Option<&str>,
    right: Option<&str>,
    description: &str,
) -> CheckReport {
    let ok = left.is_some() && right.is_some() && left == right;
    let detail = match (left, right) {
        (Some(left), Some(right)) if left == right => description.to_string(),
        (Some(left), Some(right)) => format!("mismatch: got {left}, expected {right}"),
        (None, _) => "missing observed value".to_string(),
        (_, None) => "missing expected value".to_string(),
    };
    CheckReport { id, ok, detail }
}

fn check_enum(id: &'static str, value: Option<&str>, allowed: &[&str]) -> CheckReport {
    match value {
        Some(value) if allowed.contains(&value) => CheckReport {
            id,
            ok: true,
            detail: format!("{value} is allowed"),
        },
        Some(value) => CheckReport {
            id,
            ok: false,
            detail: format!("{value} is not one of {}", allowed.join(", ")),
        },
        None => CheckReport {
            id,
            ok: false,
            detail: "missing value".to_string(),
        },
    }
}

fn check_present(id: &'static str, value: Option<&str>, description: &str) -> CheckReport {
    CheckReport {
        id,
        ok: value.is_some(),
        detail: if value.is_some() {
            description.to_string()
        } else {
            "missing value".to_string()
        },
    }
}

fn print_table_report(report: &PairingReport) {
    println!("MCP Execution Record Pairing Report");
    println!("===================================");
    println!("OK:               {}", if report.ok { "yes" } else { "no" });
    println!("Role:             {}", report.verification_scope.role);
    println!("Binding:          {}", report.binding.mode);
    println!("Binding digest:   {}", report.binding.digest);
    println!(
        "Binding nonce:    {}",
        report.binding.nonce.as_deref().unwrap_or("-")
    );
    println!(
        "Decision:         {}",
        report.decision.decision.as_deref().unwrap_or("-")
    );
    if let Some(outcome) = &report.outcome {
        println!(
            "Outcome:          {}",
            outcome.status.as_deref().unwrap_or("-")
        );
    } else {
        println!("Outcome:          -");
    }
    println!();
    for check in &report.checks {
        println!(
            "{:<36} {:<4} {}",
            check.id,
            if check.ok { "ok" } else { "fail" },
            check.detail
        );
    }
    println!();
    println!("Claims not made: {}", report.claims_not_made.join(", "));
}