mxc-sdk 1.0.1

Importable library for starting MXC sandboxes in-process, with streaming stdio.
Documentation
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Dependency-injection boundary for the guarded WPR capture fallback.
//!
//! `process_container_common` implements the AppContainer + BFS and AppContainer +
//! DACL tiers that a host without the native V2 PSEC + Learning Mode APIs still
//! needs `captureDenials` on.
//! Elevated WPR capture lives in `plm` (the host's guarded PLM tool), and
//! `process_container_common` MUST NOT depend on `plm` directly: `plm` links the
//! Windows ETL decoder (`learning_mode_windows`) and elevation/pipe machinery
//! that is unrelated to this crate's job. Depending on that host utility would
//! invert the engine-to-host-tool layering.
//!
//! Instead, this module defines the minimal traits a legacy-tier runner needs
//! to start and stop a guarded WPR capture scoped to its own sandboxed process
//! tree. `mxc_engine` (which already depends on `plm` for the executor
//! binaries' guarded-PLM lifecycle) implements them by adapting
//! `crate::plm::elevated::{start_guarded_session_with_executable, GuardedSession}`,
//! and hands the concrete factory to the dispatcher only when it explicitly
//! opts a request into the fallback (see `dispatcher::dispatch_with_fallback`
//! and `dispatcher::spawn_with_fallback`) — a runner never picks up
//! guarded capture silently.

use std::path::Path;

use crate::learning_mode_core::AnalysisResult;
use crate::mxc_common::models::{
    CaptureDenialsErrorOutput, FailurePhase, SandboxOutputMetadata, ScriptResponse,
};

/// Error returned when an injected guarded-capture implementation cannot
/// transfer retained ETL requested by the caller.
pub const RETAIN_ETL_UNSUPPORTED_MSG: &str =
    "processContainer.captureDenials.retainEtl is not supported by the configured \
     guarded-WPR capture provider";

/// Outcome of [`GuardedCaptureSession::stop_analyzed_with_trace`].
///
/// The process-scoped [`AnalysisResult`] is always present: an analysis failure
/// is reported as the method's `Err`, never as a value here. `trace_retention`
/// independently reports whether the sealed ETL was transferred to the
/// requested destination — a retention failure that occurs *after* a successful
/// analysis is carried as data so the actionable denials JSON can still be
/// published while the retention failure is surfaced separately.
#[derive(Debug)]
pub struct AnalyzedTrace {
    /// The bounded, process-scoped analysis of the guarded WPR trace.
    pub analysis: AnalysisResult,
    /// Whether the sealed ETL reached the requested destination. `Ok(())` means
    /// the ETL is persisted there; `Err` carries the retention failure while the
    /// `analysis` above remains valid.
    pub trace_retention: Result<(), String>,
}

/// A live guarded WPR capture session scoped to one sandboxed process tree.
///
/// Implementations own the elevated PLM child connection. [`stop_analyzed`]
/// asks the guardian to stop the host-wide WPR trace and decode it, returning
/// the bounded, process-scoped [`AnalysisResult`]. When the caller explicitly
/// requests `retainEtl`, implementations that support trace transfer may also
/// return the sealed ETL through [`stop_analyzed_with_trace`].
///
/// [`stop_analyzed`]: GuardedCaptureSession::stop_analyzed
pub trait GuardedCaptureSession: Send {
    /// Duplicates and attaches the caller's sandbox job and still-owned
    /// suspended root process in the elevated guardian. Both values must be
    /// HANDLEs owned by the authenticated unelevated process.
    fn attach_process_tree(
        &mut self,
        job_handle: usize,
        root_process_handle: usize,
    ) -> Result<(), String>;

    /// Stops the owned WPR trace and securely discards its raw ETL without
    /// analysis. Used when job attachment, sandbox launch, or sandbox
    /// termination fails.
    ///
    /// This method must not return, on either success or error, until the
    /// elevated guardian has terminated and released every duplicated sandbox
    /// handle. Runners rely on that guarantee before allowing firewall,
    /// filesystem, and DACL enforcement guards to drop.
    fn discard(&mut self) -> Result<(), String>;

    /// Stops the guarded capture and analyzes it against exact process
    /// generations: the guardian-attested root handle lifetime plus descendants
    /// reconciled from retained handles and job membership accounting.
    ///
    /// # Errors
    ///
    /// Returns a human-readable message if the guardian connection is gone,
    /// the stop/analyze round trip fails, or the guardian reports a decode
    /// error.
    fn stop_analyzed(&mut self) -> Result<AnalysisResult, String>;

    /// Stops and analyzes the guarded capture, and transfers the sealed ETL to
    /// `trace_destination` when the caller explicitly requests ETL retention.
    ///
    /// # Errors
    ///
    /// `Err` represents an *analysis* failure. A trace-transfer failure that
    /// occurs after a successful analysis is NOT an error here: it is carried in
    /// [`AnalyzedTrace::trace_retention`] so the decoded denials are never
    /// discarded.
    fn stop_analyzed_with_trace(
        &mut self,
        trace_destination: &std::path::Path,
    ) -> Result<AnalyzedTrace, String>;
}

/// Starts a [`GuardedCaptureSession`] for the calling (unelevated) process.
///
/// Implementations are constructed by a higher layer (`mxc_engine`) that can
/// depend on `plm`; `process_container_common` only ever sees the trait object.
pub trait GuardedCaptureFactory: Send + Sync {
    /// Verifies that this factory can start a guarded capture without creating
    /// a sandbox or starting a capture session.
    ///
    /// The dispatcher calls this only when tier selection requires guarded
    /// capture. Implementations should keep this check read-only and repeat
    /// any security-sensitive validation in [`Self::start`].
    fn verify_available(&self) -> Result<(), String> {
        Ok(())
    }

    /// Whether this factory can transfer the sealed ETL when `retainEtl` is
    /// requested. Implementations that only support analysis keep the default.
    fn allows_trace_transfer(&self) -> bool {
        false
    }

    /// Starts a new guarded WPR capture session.
    ///
    /// `owner_pid` is the calling (unelevated) process's own OS process id —
    /// used by the elevated guardian to authenticate the connection — **not**
    /// the sandboxed child's pid. The child identity and lifetime are attested
    /// later from its duplicated process handle; no caller-supplied child PID
    /// or timestamp is trusted.
    ///
    /// # Errors
    ///
    /// Returns a human-readable message on failure (elevation refused,
    /// guardian unreachable, a WPR session is already active, etc.). The
    /// caller must terminate the still-suspended sandboxed child on failure
    /// rather than resume it, so no active trace is ever left behind.
    fn start(&self, owner_pid: u32) -> Result<Box<dyn GuardedCaptureSession>, String>;
}

pub(crate) fn release_after_termination_failure(
    mut session: Box<dyn GuardedCaptureSession>,
) -> Result<(), String> {
    session.discard()
}

/// Fails closed when `retainEtl` is requested but cannot be honored.
///
/// ETL retention is satisfied either by a native capture path that seals its
/// own ETL (`native_capture_retains_etl` — BaseContainer's PSEC/V2 path) or by a
/// guarded-WPR provider that can transfer the sealed ETL
/// (`provider_allows_trace_transfer`). When retention is requested and neither
/// holds, the request is rejected with [`RETAIN_ETL_UNSUPPORTED_MSG`].
///
/// Centralized so both the AppContainer fallback tiers (which have no native
/// capture path and always pass `native_capture_retains_etl = false`) and the
/// BaseContainer runner (which passes `true` when its native PSEC/V2 capture is
/// selected, making the guarded-provider capability irrelevant) share one gate.
pub fn validate_retain_etl_supported(
    retain_etl: bool,
    provider_allows_trace_transfer: bool,
    native_capture_retains_etl: bool,
) -> Result<(), ScriptResponse> {
    if retain_etl && !native_capture_retains_etl && !provider_allows_trace_transfer {
        return Err(ScriptResponse {
            failure_phase: FailurePhase::BackendUnavailable,
            ..ScriptResponse::error(RETAIN_ETL_UNSUPPORTED_MSG)
        });
    }
    Ok(())
}

/// Structured output metadata plus the teardown status produced by finalizing a
/// guarded WPR capture. Both legacy-tier runners (`appcontainer_runner`,
/// `base_container_runner`) consume this so the analysis-success/trace-failure
/// and trace-success/JSON-failure transitions live in exactly one place and
/// cannot drift between the two runners.
#[derive(Debug)]
pub struct GuardedCaptureFinalization {
    /// Structured output metadata to store on the sandbox process, if any.
    pub metadata: Option<SandboxOutputMetadata>,
    /// Teardown status to thread through the runner's `wait()`. `Ok(())` iff the
    /// actionable denials JSON was written and no retention was requested (or
    /// retention succeeded); a retention-only failure after a successful
    /// analysis returns `Err` while still publishing the JSON via `metadata`
    /// (mirroring the native capture path).
    pub result: Result<(), String>,
}

/// How the caller asked the guarded capture to be stopped, plus — for the
/// retention case — the transfer destination. Selects `stop_analyzed` vs
/// `stop_analyzed_with_trace` inside [`finalize_guarded_capture`].
pub enum GuardedStop<'a> {
    /// `retainEtl` not requested: stop and analyze only.
    AnalyzeOnly,
    /// `retainEtl` requested: stop, analyze, and transfer the sealed ETL to
    /// `destination`.
    AnalyzeAndRetain { destination: &'a Path },
}

/// Stops a guarded WPR capture and turns its outcome into the actionable denials
/// JSON plus structured metadata, in one shared place for both legacy-tier
/// runners.
///
/// - Analysis failure ⇒ no JSON, `metadata: None`, `result: Err`.
/// - Analysis success, no retention ⇒ JSON written, success metadata,
///   `result: Ok`.
/// - Analysis success, retention success ⇒ JSON written with `etlPath` set,
///   `result: Ok`.
/// - Analysis success, retention failure ⇒ JSON written (actionable success,
///   `etlPath = None`) AND `capture_denials_error` describing the retention
///   failure with an empty ETL path (nothing was persisted); `result: Err`.
/// - Analysis + retention success but the JSON write fails ⇒ `etlPath` is
///   preserved in `capture_denials_error` so the caller can find/delete the
///   retained ETL; `result: Err`.
pub fn finalize_guarded_capture(
    session: &mut dyn GuardedCaptureSession,
    output_path: Option<&Path>,
    stop: GuardedStop<'_>,
    exit_code: i32,
) -> GuardedCaptureFinalization {
    match stop {
        GuardedStop::AnalyzeOnly => {
            finalize_analysis(session.stop_analyzed(), output_path, exit_code, None)
        }
        GuardedStop::AnalyzeAndRetain { destination } => {
            match session.stop_analyzed_with_trace(destination) {
                Ok(AnalyzedTrace {
                    analysis,
                    trace_retention,
                }) => finalize_analysis(
                    Ok(analysis),
                    output_path,
                    exit_code,
                    Some((destination, trace_retention)),
                ),
                // Analysis failure: nothing decoded, nothing persisted.
                Err(error) => finalize_analysis(Err(error), output_path, exit_code, None),
            }
        }
    }
}

/// Core of [`finalize_guarded_capture`]: publishes the denials JSON and
/// computes the metadata / teardown status from the analysis result and the
/// optional retention outcome (`(destination, transfer status)`).
fn finalize_analysis(
    analysis: Result<AnalysisResult, String>,
    output_path: Option<&Path>,
    exit_code: i32,
    retention: Option<(&Path, Result<(), String>)>,
) -> GuardedCaptureFinalization {
    let analysis = match analysis {
        Ok(analysis) => analysis,
        Err(error) => {
            return GuardedCaptureFinalization {
                metadata: None,
                result: Err(format!(
                    "captureDenials failed to stop and analyze the guarded WPR session: {error}"
                )),
            };
        }
    };
    let Some(output_path) = output_path else {
        return GuardedCaptureFinalization {
            metadata: None,
            result: Err("captureDenials internal output path was not initialized".to_string()),
        };
    };
    match crate::process_container_common::capture_output::write_denials_document(
        analysis,
        exit_code,
        output_path,
    ) {
        Ok(mut success) => match retention {
            // No retention requested, or retention succeeded: publish success.
            None | Some((_, Ok(()))) => {
                if let Some((destination, Ok(()))) = retention {
                    success.etl_path = Some(destination.to_string_lossy().into_owned());
                }
                GuardedCaptureFinalization {
                    metadata: Some(SandboxOutputMetadata {
                        capture_denials: Some(success),
                        capture_denials_error: None,
                    }),
                    result: Ok(()),
                }
            }
            // Retention requested but the transfer failed: publish the actionable
            // JSON success (etlPath stays None — nothing persisted) and surface
            // the retention failure with an empty ETL path.
            Some((_, Err(retention_error))) => {
                let message =
                    format!("captureDenials could not retain the sealed ETL: {retention_error}");
                GuardedCaptureFinalization {
                    metadata: Some(SandboxOutputMetadata {
                        capture_denials: Some(success),
                        capture_denials_error: Some(CaptureDenialsErrorOutput {
                            message: message.clone(),
                            etl_path: String::new(),
                        }),
                    }),
                    result: Err(message),
                }
            }
        },
        Err(write_error) => {
            // The JSON write failed. When the ETL was successfully retained,
            // preserve its path so the caller can still find/delete it.
            let retained_etl = match retention {
                Some((destination, Ok(()))) => Some(destination.to_string_lossy().into_owned()),
                _ => None,
            };
            let message = write_error.to_string();
            let metadata = retained_etl.map(|etl_path| SandboxOutputMetadata {
                capture_denials: None,
                capture_denials_error: Some(CaptureDenialsErrorOutput {
                    message: message.clone(),
                    etl_path,
                }),
            });
            GuardedCaptureFinalization {
                metadata,
                result: Err(message),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::learning_mode_core::{AccessType, DeniedResource, ResourceType};

    /// A fake session/factory pair proving both traits are object-safe and
    /// usable behind `dyn` references — the shape the dispatcher stores them
    /// in ([`crate::process_container_common::guarded_capture`] traits are only ever consumed as trait
    /// objects across the `process_container_common` / `mxc_engine` boundary).
    struct FakeSession {
        analysis: AnalysisResult,
    }

    impl GuardedCaptureSession for FakeSession {
        fn attach_process_tree(
            &mut self,
            job_handle: usize,
            root_process_handle: usize,
        ) -> Result<(), String> {
            if job_handle == 0 || root_process_handle == 0 {
                return Err("handles must be non-zero".to_string());
            }
            Ok(())
        }

        fn discard(&mut self) -> Result<(), String> {
            Ok(())
        }

        fn stop_analyzed(&mut self) -> Result<AnalysisResult, String> {
            Ok(self.analysis.clone())
        }

        fn stop_analyzed_with_trace(
            &mut self,
            trace_destination: &std::path::Path,
        ) -> Result<AnalyzedTrace, String> {
            std::fs::write(trace_destination, b"fake etl").map_err(|error| error.to_string())?;
            Ok(AnalyzedTrace {
                analysis: self.analysis.clone(),
                trace_retention: Ok(()),
            })
        }
    }

    struct FakeFactory;

    impl GuardedCaptureFactory for FakeFactory {
        fn allows_trace_transfer(&self) -> bool {
            true
        }

        fn start(&self, owner_pid: u32) -> Result<Box<dyn GuardedCaptureSession>, String> {
            if owner_pid == 0 {
                return Err("owner pid must be non-zero".to_string());
            }
            Ok(Box::new(FakeSession {
                analysis: AnalysisResult::complete(vec![DeniedResource {
                    resource: r"C:\blocked.txt".to_string(),
                    resource_type: ResourceType::File,
                    access_type: AccessType::Read,
                    pid: owner_pid,
                    filetime: 1,
                }]),
            }))
        }
    }

    #[test]
    fn factory_and_session_are_object_safe() {
        let factory: Box<dyn GuardedCaptureFactory> = Box::new(FakeFactory);
        let mut session = factory.start(1234).expect("start should succeed");
        session
            .attach_process_tree(42, 43)
            .expect("attach_process_tree should succeed");
        let analysis = session
            .stop_analyzed()
            .expect("stop_analyzed should succeed");
        assert_eq!(analysis.denials.len(), 1);
    }

    #[test]
    fn factory_rejects_zero_owner_pid() {
        let factory: Box<dyn GuardedCaptureFactory> = Box::new(FakeFactory);
        let error = match factory.start(0) {
            Ok(_) => panic!("owner pid 0 must be rejected"),
            Err(error) => error,
        };
        assert!(error.contains("owner pid"));
    }

    #[test]
    fn release_waits_for_discard_contract_completion() {
        struct BlockingSession {
            entered: std::sync::mpsc::Sender<()>,
            release: std::sync::mpsc::Receiver<()>,
        }

        impl GuardedCaptureSession for BlockingSession {
            fn attach_process_tree(
                &mut self,
                _job_handle: usize,
                _root_process_handle: usize,
            ) -> Result<(), String> {
                Ok(())
            }

            fn discard(&mut self) -> Result<(), String> {
                self.entered.send(()).unwrap();
                self.release.recv().unwrap();
                Err("discard failed after guardian release".to_string())
            }

            fn stop_analyzed(&mut self) -> Result<AnalysisResult, String> {
                unreachable!()
            }

            fn stop_analyzed_with_trace(
                &mut self,
                _trace_destination: &std::path::Path,
            ) -> Result<AnalyzedTrace, String> {
                unreachable!()
            }
        }

        let (entered_tx, entered_rx) = std::sync::mpsc::channel();
        let (release_tx, release_rx) = std::sync::mpsc::channel();
        let (done_tx, done_rx) = std::sync::mpsc::channel();
        let thread = std::thread::spawn(move || {
            let result = release_after_termination_failure(Box::new(BlockingSession {
                entered: entered_tx,
                release: release_rx,
            }));
            done_tx.send(result).unwrap();
        });

        entered_rx.recv().unwrap();
        assert!(done_rx
            .recv_timeout(std::time::Duration::from_millis(50))
            .is_err());
        release_tx.send(()).unwrap();
        assert_eq!(
            done_rx.recv().unwrap().unwrap_err(),
            "discard failed after guardian release"
        );
        thread.join().unwrap();
    }

    struct ScriptedSession {
        analysis: Result<AnalysisResult, String>,
        trace_retention: Result<(), String>,
    }

    impl GuardedCaptureSession for ScriptedSession {
        fn attach_process_tree(&mut self, _job: usize, _root: usize) -> Result<(), String> {
            Ok(())
        }

        fn discard(&mut self) -> Result<(), String> {
            Ok(())
        }

        fn stop_analyzed(&mut self) -> Result<AnalysisResult, String> {
            self.analysis.clone()
        }

        fn stop_analyzed_with_trace(
            &mut self,
            trace_destination: &std::path::Path,
        ) -> Result<AnalyzedTrace, String> {
            // Analysis failure short-circuits before any ETL is persisted.
            let analysis = self.analysis.clone()?;
            // Model the guardian atomically persisting the ETL only on transfer
            // success, so a retention failure leaves nothing at the destination.
            if self.trace_retention.is_ok() {
                std::fs::write(trace_destination, b"etl").map_err(|error| error.to_string())?;
            }
            Ok(AnalyzedTrace {
                analysis,
                trace_retention: self.trace_retention.clone(),
            })
        }
    }

    fn analysis_with_one_denial() -> AnalysisResult {
        AnalysisResult::complete(vec![DeniedResource {
            resource: r"C:\blocked.txt".to_string(),
            resource_type: ResourceType::File,
            access_type: AccessType::Read,
            pid: 4321,
            filetime: 7,
        }])
    }

    #[test]
    fn finalize_analyze_only_writes_json_success() {
        let directory = tempfile::tempdir().unwrap();
        let output_path = directory.path().join("denials.json");
        let mut session = ScriptedSession {
            analysis: Ok(analysis_with_one_denial()),
            trace_retention: Ok(()),
        };

        let finalization = finalize_guarded_capture(
            &mut session,
            Some(&output_path),
            GuardedStop::AnalyzeOnly,
            3,
        );

        assert!(finalization.result.is_ok());
        let capture = finalization
            .metadata
            .as_ref()
            .unwrap()
            .capture_denials
            .as_ref()
            .unwrap();
        assert_eq!(capture.total_denials, 1);
        assert_eq!(capture.etl_path, None);
        assert!(finalization
            .metadata
            .unwrap()
            .capture_denials_error
            .is_none());
        assert!(output_path.is_file());
    }

    #[test]
    fn finalize_retention_success_advertises_etl_path() {
        let directory = tempfile::tempdir().unwrap();
        let output_path = directory.path().join("denials.json");
        let etl_destination = directory.path().join("trace.etl");
        let mut session = ScriptedSession {
            analysis: Ok(analysis_with_one_denial()),
            trace_retention: Ok(()),
        };

        let finalization = finalize_guarded_capture(
            &mut session,
            Some(&output_path),
            GuardedStop::AnalyzeAndRetain {
                destination: &etl_destination,
            },
            0,
        );

        assert!(finalization.result.is_ok());
        let capture = finalization.metadata.unwrap().capture_denials.unwrap();
        assert_eq!(
            capture.etl_path.as_deref().map(std::path::Path::new),
            Some(etl_destination.as_path())
        );
        assert!(etl_destination.is_file());
    }

    #[test]
    fn finalize_trace_failure_after_analysis_publishes_json_and_surfaces_retention() {
        // The core analysis-success/trace-failure contract: the actionable
        // denials JSON is published (etlPath = None, nothing persisted) AND the
        // retention failure is surfaced through capture_denials_error with an
        // empty ETL path.
        let directory = tempfile::tempdir().unwrap();
        let output_path = directory.path().join("denials.json");
        let etl_destination = directory.path().join("trace.etl");
        let mut session = ScriptedSession {
            analysis: Ok(analysis_with_one_denial()),
            trace_retention: Err("the sealed ETL was too large to transfer".to_string()),
        };

        let finalization = finalize_guarded_capture(
            &mut session,
            Some(&output_path),
            GuardedStop::AnalyzeAndRetain {
                destination: &etl_destination,
            },
            0,
        );

        // Retention failure is reported through the teardown status...
        let error = finalization.result.unwrap_err();
        assert!(error.contains("too large to transfer"), "got: {error}");
        // ...while the actionable denials JSON is still published.
        assert!(output_path.is_file());
        let metadata = finalization.metadata.unwrap();
        let capture = metadata.capture_denials.unwrap();
        assert_eq!(capture.total_denials, 1);
        assert_eq!(capture.etl_path, None, "no ETL was retained");
        let failure = metadata.capture_denials_error.unwrap();
        assert!(failure.message.contains("too large to transfer"));
        assert_eq!(
            failure.etl_path, "",
            "nothing was persisted at the destination"
        );
        assert!(
            !etl_destination.exists(),
            "no ETL file should have been produced"
        );
    }

    #[test]
    fn finalize_json_failure_after_retention_preserves_etl_path() {
        // ETL retained, but the denials JSON write fails (destination collides
        // with an existing file): the retained ETL path must be preserved so the
        // caller can find/delete it.
        let directory = tempfile::tempdir().unwrap();
        let output_path = directory.path().join("denials.json");
        std::fs::write(&output_path, b"pre-existing").unwrap();
        let etl_destination = directory.path().join("trace.etl");
        let mut session = ScriptedSession {
            analysis: Ok(analysis_with_one_denial()),
            trace_retention: Ok(()),
        };

        let finalization = finalize_guarded_capture(
            &mut session,
            Some(&output_path),
            GuardedStop::AnalyzeAndRetain {
                destination: &etl_destination,
            },
            0,
        );

        assert!(finalization.result.is_err());
        let metadata = finalization.metadata.unwrap();
        assert!(metadata.capture_denials.is_none());
        let failure = metadata.capture_denials_error.unwrap();
        assert_eq!(
            std::path::Path::new(&failure.etl_path),
            etl_destination,
            "the retained ETL path must be preserved for cleanup"
        );
        assert!(etl_destination.is_file());
    }

    #[test]
    fn finalize_analysis_failure_publishes_nothing() {
        let directory = tempfile::tempdir().unwrap();
        let output_path = directory.path().join("denials.json");
        let mut session = ScriptedSession {
            analysis: Err("guardian connection lost".to_string()),
            trace_retention: Ok(()),
        };

        let finalization = finalize_guarded_capture(
            &mut session,
            Some(&output_path),
            GuardedStop::AnalyzeOnly,
            0,
        );

        assert!(finalization.metadata.is_none());
        assert!(finalization
            .result
            .unwrap_err()
            .contains("guardian connection lost"));
        assert!(
            !output_path.exists(),
            "no JSON should be written on analysis failure"
        );
    }

    #[test]
    fn retain_etl_validation_rejects_when_no_transfer_and_no_native() {
        let error = validate_retain_etl_supported(true, false, false).unwrap_err();
        assert_eq!(error.failure_phase, FailurePhase::BackendUnavailable);
        assert_eq!(error.error_message, RETAIN_ETL_UNSUPPORTED_MSG);
    }

    #[test]
    fn retain_etl_validation_allows_when_provider_supports_transfer() {
        assert!(validate_retain_etl_supported(true, true, false).is_ok());
    }

    #[test]
    fn retain_etl_validation_allows_native_capture_without_transfer() {
        // BaseContainer's native PSEC/V2 capture retains its own ETL, so the
        // guarded-provider transfer capability is irrelevant.
        assert!(validate_retain_etl_supported(true, false, true).is_ok());
    }

    #[test]
    fn retain_etl_validation_allows_when_retention_not_requested() {
        assert!(validate_retain_etl_supported(false, false, false).is_ok());
    }
}