assay-cli 6.22.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
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
//! EXPERIMENTAL: import privileged-mcp-action producer NDJSON records into one evidence bundle.
//!
//! Inputs are the shipped enforcing-proxy carriers: `assay.enforcement_decision.v0` records
//! (required file), plus optional denied-observation and `assay.manifest_establish.v0` files.
//! Every record is wrapped byte-faithful as an evidence event whose type is the record's own
//! `schema` member. The importer is profile-agnostic: it does not select a profile version, does
//! not autodetect one, and does not stamp a profile id onto the bundle. Cardinality, vocabularies,
//! and binding belong to `evidence verify-privileged-mcp-action`. The conformance corpus requires
//! that semantically invalid bundles (for example two decisions) stay producible.

use crate::exit_codes;
use anyhow::{bail, Context, Result};
use assay_core::mcp::ingest::{parse_mcp_transcript_bounded, McpTranscriptLimits};
use assay_core::mcp::McpInputFormat;
use assay_evidence::bundle::BundleWriter;
use assay_evidence::types::{EvidenceEvent, ProducerMeta};
use chrono::{DateTime, Utc};
use clap::{Args, ValueEnum};
use serde_json::Value;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

const EVENT_SOURCE: &str = "urn:assay:external:privileged-mcp-action";
const DEFAULT_RUN_ID: &str = "import-privileged-mcp-action";

#[derive(Debug, Args, Clone)]
pub struct PrivilegedMcpActionArgs {
    /// NDJSON file of assay.enforcement_decision.v0 records
    #[arg(long, value_name = "PATH")]
    pub decisions: PathBuf,

    /// Optional NDJSON file of denied-call observation records (byte-faithful)
    #[arg(long, value_name = "PATH")]
    pub denied_observations: Option<PathBuf>,

    /// Optional NDJSON file of assay.manifest_establish.v0 records
    #[arg(long, value_name = "PATH")]
    pub manifest_establish: Option<PathBuf>,

    /// Optional MCP transcript to inspect beside the producer-authored profile carriers
    #[arg(long, value_name = "PATH", requires = "mcp_format")]
    pub mcp_transcript: Option<PathBuf>,

    /// Input format of --mcp-transcript
    #[arg(long, value_enum, requires = "mcp_transcript")]
    pub mcp_format: Option<PrivilegedMcpTranscriptFormat>,

    /// Output Assay evidence bundle path (.tar.gz)
    #[arg(long, alias = "out", value_name = "PATH")]
    pub bundle_out: PathBuf,

    /// Assay import run id used for provenance and event ids
    #[arg(long, default_value = DEFAULT_RUN_ID)]
    pub run_id: String,

    /// Import timestamp for deterministic fixtures (RFC3339 UTC recommended)
    #[arg(long)]
    pub import_time: Option<String>,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum PrivilegedMcpTranscriptFormat {
    Inspector,
    Jsonrpc,
    StreamableHttp,
    HttpSse,
}

pub fn cmd_privileged_mcp_action(args: PrivilegedMcpActionArgs) -> Result<i32> {
    if args.run_id.contains(':') {
        bail!("run_id cannot contain ':' because event ids use run_id:seq");
    }
    let transcript = inspect_optional_mcp_transcript(&args)?;
    let import_time = parse_import_time(args.import_time.as_deref())?;
    let producer = ProducerMeta {
        name: "assay-cli".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
        git: option_env!("ASSAY_GIT_SHA").map(str::to_string),
    };

    // Sequence order is fixed: all decisions in file order, then observations, then establish
    // records. The order is provenance only; the verifier selects by payload schema, never by seq.
    let (mut records, decisions) = read_ndjson_records(&args.decisions)?;
    let mut inputs = vec![decisions];
    inputs.extend(transcript);
    if let Some(path) = &args.denied_observations {
        let (additional, input) = read_ndjson_records(path)?;
        records.extend(additional);
        inputs.push(input);
    }
    if let Some(path) = &args.manifest_establish {
        let (additional, input) = read_ndjson_records(path)?;
        records.extend(additional);
        inputs.push(input);
    }
    if records.is_empty() {
        bail!(
            "no records found in {} (or the optional inputs); nothing to import",
            args.decisions.display()
        );
    }

    let input_refs: Vec<&File> = inputs.iter().collect();
    let out_file = assay_common::file_output::open_preserving_inputs(&args.bundle_out, &input_refs)
        .with_context(|| format!("failed to create bundle {}", args.bundle_out.display()))?;
    let mut writer = BundleWriter::new(out_file).with_producer(producer.clone());
    for (seq, (schema, payload)) in records.into_iter().enumerate() {
        let event = EvidenceEvent::new(&schema, EVENT_SOURCE, &args.run_id, seq as u64, payload)
            .with_time(import_time)
            .with_producer(&producer);
        writer.add_event(event);
    }
    writer
        .finish()
        .with_context(|| format!("failed to write bundle {}", args.bundle_out.display()))?;

    eprintln!(
        "Imported privileged-mcp-action records to {}",
        args.bundle_out.display()
    );
    Ok(exit_codes::OK)
}

fn inspect_optional_mcp_transcript(args: &PrivilegedMcpActionArgs) -> Result<Option<File>> {
    let (path, format) = match (&args.mcp_transcript, args.mcp_format) {
        (None, None) => return Ok(None),
        (Some(_), None) => bail!("--mcp-transcript requires --mcp-format"),
        (None, Some(_)) => bail!("--mcp-format requires --mcp-transcript"),
        (Some(path), Some(format)) => (path, format),
    };
    let file = File::open(path)
        .with_context(|| format!("failed to open MCP transcript {}", path.display()))?;
    let format = match format {
        PrivilegedMcpTranscriptFormat::Inspector => McpInputFormat::Inspector,
        PrivilegedMcpTranscriptFormat::Jsonrpc => McpInputFormat::JsonRpc,
        PrivilegedMcpTranscriptFormat::StreamableHttp => McpInputFormat::StreamableHttp,
        PrivilegedMcpTranscriptFormat::HttpSse => McpInputFormat::HttpSse,
    };
    parse_mcp_transcript_bounded(&file, format, McpTranscriptLimits::default())
        .map_err(|error| anyhow::anyhow!("MCP transcript ingest failed: {error}"))?;
    Ok(Some(file))
}

/// Read one NDJSON file into `(schema, payload)` pairs, byte-faithful.
///
/// Each non-blank line must parse as a JSON object carrying a string `schema` member: that string
/// becomes the event type. Nothing else about the record is inspected here.
fn read_ndjson_records(path: &Path) -> Result<(Vec<(String, Value)>, File)> {
    let file =
        File::open(path).with_context(|| format!("failed to open records {}", path.display()))?;
    let mut records = Vec::new();
    for (lineno, line) in BufReader::new(&file).lines().enumerate() {
        let line = line.with_context(|| format!("failed to read {}", path.display()))?;
        if line.trim().is_empty() {
            continue;
        }
        let payload: Value = serde_json::from_str(&line)
            .with_context(|| format!("{} line {}: invalid JSON", path.display(), lineno + 1))?;
        let schema = payload
            .get("schema")
            .and_then(Value::as_str)
            .map(str::to_string);
        match schema {
            Some(schema) => records.push((schema, payload)),
            None => bail!(
                "{} line {}: record has no string schema member; cannot type the event",
                path.display(),
                lineno + 1
            ),
        }
    }
    Ok((records, file))
}

fn parse_import_time(value: Option<&str>) -> Result<DateTime<Utc>> {
    match value {
        Some(value) => Ok(DateTime::parse_from_rfc3339(value)
            .with_context(|| format!("invalid --import-time {value:?}; expected RFC3339"))?
            .with_timezone(&Utc)),
        None => Ok(Utc::now()),
    }
}

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

    const NOTIFICATION: &str = r#"{"jsonrpc":"2.0","method":"notifications/progress","params":{}}"#;

    fn decision(decision: &str) -> Value {
        json!({
            "schema": "assay.enforcement_decision.v0",
            "decision": decision,
            "tool": {"name": "github.add_deploy_key"}
        })
    }

    fn observation() -> Value {
        json!({
            "schema": "assay.denied_call_observation.v0",
            "call": {"tool_name": "github.add_deploy_key"}
        })
    }

    fn write_ndjson(dir: &Path, name: &str, records: &[Value]) -> PathBuf {
        let path = dir.join(name);
        let mut body = String::new();
        for r in records {
            body.push_str(&serde_json::to_string(r).unwrap());
            body.push('\n');
        }
        std::fs::write(&path, body).unwrap();
        path
    }

    fn args(dir: &Path, decisions: PathBuf) -> PrivilegedMcpActionArgs {
        PrivilegedMcpActionArgs {
            decisions,
            denied_observations: None,
            manifest_establish: None,
            mcp_transcript: None,
            mcp_format: None,
            bundle_out: dir.join("out.bundle.tar.gz"),
            run_id: DEFAULT_RUN_ID.to_string(),
            import_time: Some("2026-07-24T00:00:00Z".to_string()),
        }
    }

    // Removing any retained input from the output guard must destroy these literal bytes
    // on the old implementation, rather than merely changing an error message.
    fn assert_import_preserves_source(input_index: usize, alias: &str) {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[decision("deny")]);
        let observations = write_ndjson(dir.path(), "obs.ndjson", &[observation()]);
        let establish = write_ndjson(
            dir.path(),
            "est.ndjson",
            &[json!({"schema": "test.establish"})],
        );
        let transcript = dir.path().join("transcript.jsonl");
        std::fs::write(&transcript, format!("{NOTIFICATION}\n")).unwrap();
        let inputs = [
            decisions.clone(),
            observations.clone(),
            establish.clone(),
            transcript.clone(),
        ];
        let originals: Vec<_> = inputs.iter().map(|p| std::fs::read(p).unwrap()).collect();
        let mut a = args(dir.path(), decisions);
        a.denied_observations = Some(observations);
        a.manifest_establish = Some(establish);
        a.mcp_transcript = Some(transcript);
        a.mcp_format = Some(PrivilegedMcpTranscriptFormat::Jsonrpc);
        match alias {
            "same" => a.bundle_out = inputs[input_index].clone(),
            "hardlink" => std::fs::hard_link(&inputs[input_index], &a.bundle_out).unwrap(),
            #[cfg(unix)]
            "symlink" => std::os::unix::fs::symlink(&inputs[input_index], &a.bundle_out).unwrap(),
            _ => panic!("unknown alias"),
        }
        let result = cmd_privileged_mcp_action(a);
        for (path, original) in inputs.iter().zip(originals) {
            assert_eq!(
                std::fs::read(path).unwrap(),
                original,
                "source {}",
                path.display()
            );
        }
        assert!(result
            .unwrap_err()
            .chain()
            .any(|e| e.to_string().contains("input file")));
    }

    #[test]
    fn producer_lane_import_preserves_decisions_same_path() {
        assert_import_preserves_source(0, "same");
    }

    #[test]
    fn producer_lane_import_preserves_observations_same_path() {
        assert_import_preserves_source(1, "same");
    }

    #[test]
    fn producer_lane_import_preserves_establish_same_path() {
        assert_import_preserves_source(2, "same");
    }

    #[test]
    fn producer_lane_import_preserves_transcript_same_path() {
        assert_import_preserves_source(3, "same");
    }

    #[test]
    fn producer_lane_import_preserves_hardlinked_source() {
        assert_import_preserves_source(0, "hardlink");
    }

    #[cfg(unix)]
    #[test]
    fn producer_lane_import_preserves_symlinked_source() {
        assert_import_preserves_source(0, "symlink");
    }

    #[test]
    fn producer_lane_import_overwrites_distinct_output_and_allows_shared_inputs() {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[decision("deny")]);
        let original = std::fs::read(&decisions).unwrap();
        let mut a = args(dir.path(), decisions.clone());
        a.denied_observations = Some(decisions.clone());
        std::fs::write(&a.bundle_out, vec![b'x'; 65536]).unwrap();
        #[cfg(unix)]
        let inode = {
            use std::os::unix::fs::MetadataExt;
            std::fs::metadata(&a.bundle_out).unwrap().ino()
        };
        assert_eq!(
            cmd_privileged_mcp_action(a.clone()).unwrap(),
            exit_codes::OK
        );
        assert_eq!(std::fs::read(&decisions).unwrap(), original);
        #[cfg(unix)]
        {
            use std::os::unix::fs::MetadataExt;
            assert_eq!(std::fs::metadata(&a.bundle_out).unwrap().ino(), inode);
        }
        let reader = BundleReader::open(File::open(&a.bundle_out).unwrap()).unwrap();
        assert_eq!(reader.events_vec().unwrap().len(), 2);
    }

    #[test]
    fn producer_lane_import_invalid_input_preserves_existing_outputs() {
        for alias in [false, true] {
            let dir = tempfile::tempdir().unwrap();
            let decisions = dir.path().join("dec.ndjson");
            std::fs::write(&decisions, b"not-json\n").unwrap();
            let mut a = args(dir.path(), decisions.clone());
            if alias {
                a.bundle_out = decisions.clone();
            } else {
                std::fs::write(&a.bundle_out, b"old output").unwrap();
            }
            let original = std::fs::read(&a.bundle_out).unwrap();
            let error = cmd_privileged_mcp_action(a.clone()).unwrap_err();
            assert!(error.to_string().contains("invalid JSON"));
            assert_eq!(std::fs::read(&a.bundle_out).unwrap(), original);
            assert_eq!(std::fs::read(decisions).unwrap(), b"not-json\n");
        }
    }

    #[test]
    fn producer_lane_import_wraps_records_byte_faithful_in_declared_order() {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[decision("deny")]);
        let observations = write_ndjson(dir.path(), "obs.ndjson", &[observation()]);
        let mut a = args(dir.path(), decisions);
        a.denied_observations = Some(observations);
        let code = cmd_privileged_mcp_action(a.clone()).unwrap();
        assert_eq!(code, exit_codes::OK);

        let reader = BundleReader::open(File::open(&a.bundle_out).unwrap()).unwrap();
        assert_eq!(reader.manifest().event_count, 2);
        let events = reader.events_vec().unwrap();
        // Event type equals the record's own schema member; payload is the record byte-faithful.
        assert_eq!(events[0].type_, "assay.enforcement_decision.v0");
        assert_eq!(events[0].payload, decision("deny"));
        assert_eq!(events[1].type_, "assay.denied_call_observation.v0");
        assert_eq!(events[1].payload, observation());
        assert_eq!(events[0].source, EVENT_SOURCE);
    }

    #[test]
    fn producer_lane_import_does_not_enforce_profile_cardinality() {
        // Two decisions in one file must stay producible (the verifier, not the importer, rejects
        // them; the conformance corpus's two-decision vector depends on this split).
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(
            dir.path(),
            "dec.ndjson",
            &[decision("deny"), decision("allow")],
        );
        let a = args(dir.path(), decisions);
        cmd_privileged_mcp_action(a.clone()).unwrap();
        let reader = BundleReader::open(File::open(&a.bundle_out).unwrap()).unwrap();
        assert_eq!(reader.manifest().event_count, 2);
    }

    #[test]
    fn producer_lane_import_rejects_record_without_schema() {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[json!({"decision": "deny"})]);
        let err = cmd_privileged_mcp_action(args(dir.path(), decisions)).unwrap_err();
        assert!(err.to_string().contains("no string schema"));
    }

    #[test]
    fn producer_lane_import_rejects_run_id_with_colon() {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[decision("deny")]);
        let mut a = args(dir.path(), decisions);
        a.run_id = "bad:run".to_string();
        let err = cmd_privileged_mcp_action(a).unwrap_err();
        assert!(err.to_string().contains("run_id cannot contain ':'"));
    }

    #[test]
    fn producer_lane_import_rejects_empty_inputs() {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[]);
        let err = cmd_privileged_mcp_action(args(dir.path(), decisions)).unwrap_err();
        assert!(err.to_string().contains("nothing to import"));
    }

    fn profile_allow_decision() -> Value {
        json!({
            "schema": "assay.enforcement_decision.v0",
            "caller": {"id": "ci-agent"},
            "tool": {
                "name": "github.add_deploy_key",
                "action_class": "github_deploy_key"
            },
            "action": {
                "verb": "create",
                "resource_type": "github_deploy_key",
                "target": {
                    "provider": "github",
                    "owner": "acme",
                    "repo": "prod-app"
                },
                "target_digest": "sha256:c3ff823d7fb2ee33b9f1a3f7be6eaf849acb980b6ec960731506436b56384dfc"
            },
            "decision": "allow",
            "reason": "allow",
            "fail_closed": false,
            "drift_state": "satisfied",
            "credential_alias": "gh-deploy",
            "non_claims": [
                "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 — neither is a maliciousness verdict",
                "not the observation artifact (assay.mcp_manifest_observed.v0) and not the mechanism artifact (assay.enforcement_health.v0)"
            ]
        })
    }

    fn expected_allow_report() -> Value {
        json!({
            "schema": "assay.privileged_mcp_action.verify.report.v0",
            "profile": "privileged-mcp-action/v0",
            "profile_selection": "default",
            "input_profile": null,
            "input_profile_status": "undeclared_legacy",
            "bundle_integrity": "pass",
            "verdict": "valid",
            "claims": {
                "policy_decision_recorded": {
                    "status": "confirmed",
                    "source_class": "producer_reported"
                },
                "caller_visible_denial": {"status": "incomplete"},
                "upstream_delivery": {"status": "incomplete"},
                "external_side_effect": {"status": "incomplete"}
            },
            "findings": [],
            "non_claims": [
                "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"
            ],
            "evaluator": {"name": "assay", "version": env!("CARGO_PKG_VERSION")}
        })
    }

    fn write_transcript(dir: &Path, name: &str, text: &str) -> PathBuf {
        let path = dir.join(name);
        std::fs::write(&path, text).unwrap();
        path
    }

    #[test]
    fn e2e_lane_wire_era_forms_round_trip_to_the_same_frozen_profile_report() {
        use super::super::verify_privileged_mcp_action::verify_bundle_report;

        let vectors = [
            (
                "legacy-missing",
                r#"{"transport":"streamable-http","transport_context":{"headers":{"MCP-Protocol-Version":"2025-06-18"}},"entries":[{"request":{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"github.add_deploy_key","arguments":{}}}},{"response":{"jsonrpc":"2.0","id":1,"result":{"content":[]}}}]}"#,
            ),
            (
                "modern-complete",
                r#"{"transport":"streamable-http","transport_context":{"headers":{"MCP-Protocol-Version":"2026-07-28"}},"entries":[{"request":{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"github.add_deploy_key","arguments":{},"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}}},{"response":{"jsonrpc":"2.0","id":1,"result":{"content":[],"resultType":"complete"}}}]}"#,
            ),
            (
                "modern-input-required",
                r#"{"transport":"streamable-http","transport_context":{"headers":{"MCP-Protocol-Version":"2026-07-28"}},"entries":[{"request":{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"github.add_deploy_key","arguments":{},"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}}},{"response":{"jsonrpc":"2.0","id":1,"result":{"resultType":"input_required","inputRequests":{"elicitation":{"method":"elicitation/create","params":{"message":"m","requestedSchema":{"type":"object","properties":{}}}}}}}}]}"#,
            ),
        ];

        for (name, transcript) in vectors {
            let dir = tempfile::tempdir().unwrap();
            let decisions = write_ndjson(dir.path(), "dec.ndjson", &[profile_allow_decision()]);
            let mut import = args(dir.path(), decisions);
            import.mcp_transcript =
                Some(write_transcript(dir.path(), "transcript.json", transcript));
            import.mcp_format = Some(PrivilegedMcpTranscriptFormat::StreamableHttp);
            cmd_privileged_mcp_action(import.clone())
                .unwrap_or_else(|error| panic!("{name}: {error:#}"));
            let report = verify_bundle_report(&import.bundle_out);
            assert_eq!(
                serde_json::to_value(report).unwrap(),
                expected_allow_report(),
                "{name}"
            );
        }
    }

    #[test]
    fn every_cli_transcript_format_reaches_its_parser_and_preserves_the_profile() {
        use super::super::verify_privileged_mcp_action::verify_bundle_report;

        let request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "tools/call",
            "params": {
                "name": "github.add_deploy_key",
                "arguments": {},
                "_meta": {
                    "io.modelcontextprotocol/protocolVersion": "2026-07-28",
                    "io.modelcontextprotocol/clientCapabilities": {}
                }
            }
        });
        let response = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "result": {"content": [], "resultType": "complete"}
        });
        let header = json!({"MCP-Protocol-Version": "2026-07-28"});
        let vectors = [
            (
                "inspector",
                json!({"events": [request.clone(), response.clone()]}).to_string(),
                PrivilegedMcpTranscriptFormat::Inspector,
            ),
            (
                "jsonrpc",
                format!("{request}\n{response}\n"),
                PrivilegedMcpTranscriptFormat::Jsonrpc,
            ),
            (
                "streamable-http",
                json!({
                    "transport": "streamable-http",
                    "transport_context": {"headers": header.clone()},
                    "entries": [
                        {"request": request.clone()},
                        {"response": response.clone()}
                    ]
                })
                .to_string(),
                PrivilegedMcpTranscriptFormat::StreamableHttp,
            ),
            (
                "http-sse",
                json!({
                    "transport": "http-sse",
                    "transport_context": {"headers": header},
                    "entries": [
                        {"sse": {"event": "message", "data": request}},
                        {"sse": {"event": "message", "data": response}}
                    ]
                })
                .to_string(),
                PrivilegedMcpTranscriptFormat::HttpSse,
            ),
        ];

        for (name, transcript, format) in vectors {
            let dir = tempfile::tempdir().unwrap();
            let decisions = write_ndjson(dir.path(), "dec.ndjson", &[profile_allow_decision()]);
            let mut import = args(dir.path(), decisions);
            import.mcp_transcript = Some(write_transcript(
                dir.path(),
                &format!("{name}.json"),
                &transcript,
            ));
            import.mcp_format = Some(format);
            cmd_privileged_mcp_action(import.clone())
                .unwrap_or_else(|error| panic!("{name}: {error:#}"));
            let report = verify_bundle_report(&import.bundle_out);
            assert_eq!(
                serde_json::to_value(report).unwrap(),
                expected_allow_report(),
                "{name}"
            );
        }
    }

    #[test]
    fn producer_lane_malformed_wire_input_is_refused_before_a_bundle_is_written() {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[profile_allow_decision()]);
        let mut import = args(dir.path(), decisions);
        import.mcp_transcript = Some(write_transcript(
            dir.path(),
            "transcript.json",
            r#"{"ATTACKER_SENTINEL":"#,
        ));
        import.mcp_format = Some(PrivilegedMcpTranscriptFormat::StreamableHttp);
        let error = cmd_privileged_mcp_action(import.clone()).unwrap_err();
        assert!(
            error
                .to_string()
                .starts_with("MCP transcript ingest failed:"),
            "{error:#}"
        );
        assert!(error.to_string().contains("MCP transcript is invalid"));
        assert!(!format!("{error:?}").contains("ATTACKER_SENTINEL"));
        assert!(!import.bundle_out.exists());
    }

    #[test]
    fn producer_lane_deny_import_accepts_an_observed_jsonrpc_error_without_deriving_from_it() {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[decision("deny")]);
        let transcript = r#"{"transport":"streamable-http","transport_context":{"headers":{"MCP-Protocol-Version":"2026-07-28"}},"entries":[{"request":{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"github.add_deploy_key","arguments":{},"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}}},{"response":{"jsonrpc":"2.0","id":1,"error":{"code":-32042,"message":"ATTACKER_SENTINEL"}}}]}"#;
        let mut import = args(dir.path(), decisions);
        import.mcp_transcript = Some(write_transcript(
            dir.path(),
            "deny-transcript.json",
            transcript,
        ));
        import.mcp_format = Some(PrivilegedMcpTranscriptFormat::StreamableHttp);

        assert_eq!(
            cmd_privileged_mcp_action(import.clone()).unwrap(),
            exit_codes::OK
        );
        let reader = BundleReader::open(File::open(&import.bundle_out).unwrap()).unwrap();
        let events = reader.events_vec().unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].payload, decision("deny"));
        assert!(
            !serde_json::to_string(&events)
                .unwrap()
                .contains("ATTACKER_SENTINEL"),
            "the optional wire observation must not become producer evidence"
        );
    }

    #[test]
    fn producer_lane_transcript_and_format_must_be_supplied_together() {
        let dir = tempfile::tempdir().unwrap();
        let decisions = write_ndjson(dir.path(), "dec.ndjson", &[profile_allow_decision()]);
        let mut transcript_only = args(dir.path(), decisions.clone());
        transcript_only.mcp_transcript = Some(write_transcript(
            dir.path(),
            "transcript.json",
            NOTIFICATION,
        ));
        assert!(cmd_privileged_mcp_action(transcript_only)
            .unwrap_err()
            .to_string()
            .contains("--mcp-format"));

        let mut format_only = args(dir.path(), decisions);
        format_only.mcp_format = Some(PrivilegedMcpTranscriptFormat::Jsonrpc);
        assert!(cmd_privileged_mcp_action(format_only)
            .unwrap_err()
            .to_string()
            .contains("--mcp-transcript"));
    }
}