mxc-sdk 1.0.0

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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! `StatefulSandboxBackend` impl for the state-aware WSLc lifecycle.
//!
//! Each lifecycle phase (`provision` / `start` / `exec` / `stop` /
//! `deprovision`) runs as a separate short-lived `wxc-exec` process. Because the
//! WSLc SDK has no cross-process re-attach, this backend does **not** touch the
//! SDK directly: it translates runtime-owned phase configuration plus
//! the cross-cutting `policy` section into [`daemon_protocol`] frames and drives
//! the long-lived `wxc-wslc-daemon` (which owns the live session/container
//! handles) over an owner-only named pipe via [`DaemonClient`].
//!
//! Windows-only: the daemon and its pipe transport are a Windows feature.

use std::io::Write;
#[cfg(windows)]
use std::os::windows::io::AsRawHandle;
#[cfg(windows)]
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(windows)]
use std::sync::Arc;

use crate::mxc_common::logger::{Logger, Mode};
#[cfg(test)]
use crate::mxc_common::models::NetworkPolicy;
use crate::mxc_common::models::{ContainerPolicy, ExecutionRequest, WslcProvisionConfig};
use crate::mxc_common::mxc_error::MxcError;
use crate::mxc_common::state_aware_backend::{
    null_pipe_handle, DeprovisionResult, ExecHandle, ExecOutcome, ExecStdio, ProvisionResult,
    StartResult, StatefulSandboxBackend, StopResult,
};
use crate::mxc_common::validator::validate_state_aware_network_policy_support;

use crate::wslc_common::container_steps::OutStream;
use crate::wslc_common::daemon_client::{
    truncation_suffix, DaemonClient, DaemonError, DaemonExecOutcome,
};
use crate::wslc_common::daemon_protocol::{
    DeprovisionConfig, ErrKind, ExecConfig, NetworkMode, ProvisionConfig, StartConfig, StopConfig,
    VolumeMount,
};
use crate::wslc_common::policy::{
    exec_proxy_url, validate_exec_policy, validate_post_provision_policy, validate_provision_policy,
};
use crate::wslc_common::process_env::EnvScope;
#[cfg(windows)]
use crate::wslc_common::sandbox::prepare_native_output;
#[cfg(windows)]
use crate::wslc_common::stream_buffer::bounded_stream_pair;

/// Default image when a provision request omits `wslc.provision.image`.
const DEFAULT_IMAGE: &str = "alpine:latest";

/// Per-stream ceiling between daemon frames and the synthesized native pipe.
///
/// The relay must keep consuming until the terminal frame, so it cannot block
/// when a caller leaves its native pipe unread. Crossing this ceiling drops the
/// remaining output and turns an otherwise successful exit into an error.
#[cfg(windows)]
const PIPE_BRIDGE_MAX_BUFFERED_BYTES: usize = 8 * 1024 * 1024;

/// State-aware WSLc backend. Zero-sized: every phase opens a fresh
/// [`DaemonClient`] connection (the daemon holds all persistent state).
#[derive(Debug, Default, Clone, Copy)]
pub struct WslcStateAwareRunner;

impl WslcStateAwareRunner {
    pub fn new() -> Self {
        Self
    }
}

impl StatefulSandboxBackend for WslcStateAwareRunner {
    const ID_PREFIX: &'static str = "wslc";
    const BACKEND_KEY: &'static str = "wslc";

    type ProvisionConfig = WslcProvisionConfig;
    type StartConfig = ();
    type ExecConfig = ();
    type StopConfig = ();
    type DeprovisionConfig = ();
    type ProvisionMetadata = ();
    type StartMetadata = ();
    type StopMetadata = ();
    type DeprovisionMetadata = ();

    fn provision(
        &mut self,
        request: &ExecutionRequest,
        config: Option<WslcProvisionConfig>,
    ) -> Result<ProvisionResult<()>, MxcError> {
        let provision_config = build_provision_config(request, config)?;

        let client = connect_daemon()?;
        let sandbox_id = client
            .provision(provision_config)
            .map_err(map_daemon_error)?;

        // The daemon mints a fully `wslc:`-prefixed id; return it verbatim so
        // later phases present the daemon's own map key.
        Ok(ProvisionResult {
            sandbox_id,
            metadata: None,
        })
    }

    fn start(
        &mut self,
        sandbox_id: &str,
        _request: &ExecutionRequest,
        _config: Option<()>,
    ) -> Result<StartResult<()>, MxcError> {
        let client = connect_daemon()?;
        client
            .start(StartConfig {
                sandbox_id: sandbox_id.to_string(),
            })
            .map_err(map_daemon_error)?;
        Ok(StartResult { metadata: None })
    }

    fn stop(
        &mut self,
        sandbox_id: &str,
        _request: &ExecutionRequest,
        _config: Option<()>,
    ) -> Result<StopResult<()>, MxcError> {
        let client = connect_daemon()?;
        client
            .stop(StopConfig {
                sandbox_id: sandbox_id.to_string(),
            })
            .map_err(map_daemon_error)?;
        Ok(StopResult { metadata: None })
    }

    fn deprovision(
        &mut self,
        sandbox_id: &str,
        _request: &ExecutionRequest,
        _config: Option<()>,
    ) -> Result<DeprovisionResult<()>, MxcError> {
        let client = connect_daemon()?;
        client
            .deprovision(DeprovisionConfig {
                sandbox_id: sandbox_id.to_string(),
            })
            .map_err(map_daemon_error)?;
        Ok(DeprovisionResult { metadata: None })
    }

    /// Runs one command in the warm container. Relayed callers receive live
    /// output on this process's stdout/stderr; piped callers receive synthesized
    /// native stdout/stderr pipes fed from the same daemon stream.
    fn exec(
        &mut self,
        sandbox_id: &str,
        request: &ExecutionRequest,
        _config: Option<()>,
        stdio: ExecStdio,
    ) -> Result<ExecHandle, MxcError> {
        let client = connect_daemon()?;
        let exec_id = uuid::Uuid::new_v4().simple().to_string();
        let run_token = uuid::Uuid::new_v4().simple().to_string();
        let config = exec_config(sandbox_id, request, exec_id.clone(), run_token.clone());

        match stdio {
            ExecStdio::Relayed => exec_relayed(client, config),
            ExecStdio::Piped => exec_piped(client, config, exec_id, run_token),
        }
    }

    fn validate_provision(
        &self,
        request: &ExecutionRequest,
        _config: Option<&WslcProvisionConfig>,
    ) -> Result<(), MxcError> {
        validate_state_aware_network_policy_support(
            request,
            crate::wslc_common::policy::network_policy_support(),
        )?;
        validate_provision_policy(request)
    }

    fn validate_start(
        &self,
        sandbox_id: &str,
        request: &ExecutionRequest,
        _config: Option<&()>,
    ) -> Result<(), MxcError> {
        validate_sandbox_id(sandbox_id)?;
        validate_state_aware_network_policy_support(
            request,
            crate::wslc_common::policy::network_policy_support(),
        )?;
        validate_post_provision_policy(request)
    }

    fn validate_exec(
        &self,
        sandbox_id: &str,
        request: &ExecutionRequest,
        _config: Option<&()>,
    ) -> Result<(), MxcError> {
        validate_sandbox_id(sandbox_id)?;
        validate_state_aware_network_policy_support(
            request,
            crate::wslc_common::policy::network_policy_support(),
        )?;
        validate_exec_policy(request)
    }

    fn validate_stop(
        &self,
        sandbox_id: &str,
        request: &ExecutionRequest,
        _config: Option<&()>,
    ) -> Result<(), MxcError> {
        validate_sandbox_id(sandbox_id)?;
        validate_state_aware_network_policy_support(
            request,
            crate::wslc_common::policy::network_policy_support(),
        )?;
        validate_post_provision_policy(request)
    }

    fn validate_deprovision(
        &self,
        sandbox_id: &str,
        request: &ExecutionRequest,
        _config: Option<&()>,
    ) -> Result<(), MxcError> {
        validate_sandbox_id(sandbox_id)?;
        validate_state_aware_network_policy_support(
            request,
            crate::wslc_common::policy::network_policy_support(),
        )?;
        validate_post_provision_policy(request)
    }
}

fn exec_relayed(client: DaemonClient, config: ExecConfig) -> Result<ExecHandle, MxcError> {
    let timeout_ms = config.timeout_ms;

    // Relay each chunk to our own stdio as it arrives. Hold the stdout/stderr
    // locks for the whole relay so we don't reacquire the handle per chunk,
    // and coalesce flushes: `std::io::Stdout`/`Stderr` are line-buffered, so
    // newline-terminated output already reaches the consumer promptly; we only
    // force a flush for a chunk that does *not* end in a newline (progress
    // output — prompts, spinners) so it isn't stranded in the line buffer.
    // This avoids a flush syscall per bulk chunk while preserving low latency.
    // Best-effort: a failed local write must not mask the container's exit code.
    let stdout = std::io::stdout();
    let stderr = std::io::stderr();
    let completion = {
        let mut out = stdout.lock();
        let mut err = stderr.lock();
        let result = client.exec_streaming(config, |stream, bytes| match stream {
            OutStream::Stdout => {
                let _ = out.write_all(bytes);
                if bytes.last() != Some(&b'\n') {
                    let _ = out.flush();
                }
            }
            OutStream::Stderr => {
                let _ = err.write_all(bytes);
                if bytes.last() != Some(&b'\n') {
                    let _ = err.flush();
                }
            }
        });
        let _ = out.flush();
        let _ = err.flush();
        // Drop the locks before mapping the error so error conversion never
        // contends with the writers we just held.
        drop((out, err));
        result.map_err(map_daemon_error)?
    };

    let exit_code = match completion.outcome {
        DaemonExecOutcome::Exited(code) if completion.truncated => {
            return Err(truncated_run_error(code))
        }
        DaemonExecOutcome::Exited(code) => code,
        DaemonExecOutcome::TimedOut => {
            return Err(MxcError::backend_error(format!(
                "WSLc exec timed out after {timeout_ms}ms{}",
                truncation_suffix(completion.truncated)
            )))
        }
        DaemonExecOutcome::Cancelled => {
            return Err(MxcError::backend_error(format!(
                "WSLc relayed exec was cancelled unexpectedly{}",
                truncation_suffix(completion.truncated)
            )))
        }
    };

    Ok(ExecHandle {
        stdout: null_pipe_handle(),
        stderr: null_pipe_handle(),
        stdin: null_pipe_handle(),
        stdin_closer: None,
        // Relayed execution has already completed before this handle is
        // returned; timeouts were surfaced above as an error because the
        // relay adapter has no typed timeout result.
        waiter: Box::new(move || Ok(ExecOutcome::Exited(exit_code))),
        // Nothing to terminate: the workload is already gone. `Ok(())` is
        // the truthful answer here, not a placeholder.
        terminator: Box::new(|| Ok(())),
    })
}

#[cfg(windows)]
fn exec_piped(
    client: DaemonClient,
    config: ExecConfig,
    exec_id: String,
    run_token: String,
) -> Result<ExecHandle, MxcError> {
    let stdout_pipe = prepare_native_output()
        .map_err(|error| MxcError::backend_error(format!("create WSLC stdout pipe: {error}")))?;
    let stderr_pipe = prepare_native_output()
        .map_err(|error| MxcError::backend_error(format!("create WSLC stderr pipe: {error}")))?;
    let (stdout_writer, stdout_source, stdout_overflow) =
        bounded_stream_pair(PIPE_BRIDGE_MAX_BUFFERED_BYTES);
    let (stderr_writer, stderr_source, stderr_overflow) =
        bounded_stream_pair(PIPE_BRIDGE_MAX_BUFFERED_BYTES);
    let (stdout_reader, stdout_pump) = stdout_pipe
        .activate(stdout_source)
        .map_err(|error| MxcError::backend_error(format!("start WSLC stdout pump: {error}")))?;
    let (stderr_reader, stderr_pump) = stderr_pipe
        .activate(stderr_source)
        .map_err(|error| MxcError::backend_error(format!("start WSLC stderr pump: {error}")))?;
    let stdout = windows::Win32::Foundation::HANDLE(stdout_reader.as_raw_handle());
    let stderr = windows::Win32::Foundation::HANDLE(stderr_reader.as_raw_handle());
    let terminator_client = client.clone();
    let relay_exec_id = exec_id.clone();
    let relay_run_token = run_token.clone();
    let cancellation = Arc::new(AtomicBool::new(false));
    let relay_cancellation = Arc::clone(&cancellation);
    let (done_tx, done_rx) = std::sync::mpsc::sync_channel(1);

    std::thread::Builder::new()
        .name("wslc-state-aware-exec".to_string())
        .spawn(move || {
            let result = client
                .admit_exec(config)
                .and_then(|exec| {
                    if relay_cancellation.load(Ordering::Acquire) {
                        client.cancel_exec(relay_exec_id, relay_run_token)?;
                    }
                    exec.read_to_completion(|stream, bytes| match stream {
                        OutStream::Stdout => stdout_writer.write(bytes),
                        OutStream::Stderr => stderr_writer.write(bytes),
                    })
                })
                .map_err(map_daemon_error)
                .and_then(|completion| {
                    // Either end of the bridge can drop output: the daemon's
                    // queue on the way out, or these pipes if the caller is slow.
                    let truncated = completion.truncated
                        || stdout_overflow.has_overflowed()
                        || stderr_overflow.has_overflowed();
                    match completion.outcome {
                        DaemonExecOutcome::Exited(code) if truncated => {
                            Err(truncated_run_error(code))
                        }
                        DaemonExecOutcome::Exited(code) => Ok(ExecOutcome::Exited(code)),
                        DaemonExecOutcome::TimedOut => Ok(ExecOutcome::TimedOut),
                        // `SandboxProcess::kill` is a request followed by reaping;
                        // there is no distinct cancelled variant in `ExecOutcome`.
                        DaemonExecOutcome::Cancelled => Ok(ExecOutcome::Exited(-1)),
                    }
                });
            stdout_writer.close();
            stderr_writer.close();
            let _ = done_tx.send(result);
            // Caller-owned pipes can remain full when the caller uses
            // `try_wait` or drops without draining. Joining the pumps here
            // would then block terminal completion and create a Drop cycle.
            // Dropping a JoinHandle detaches the pump; closing the read end
            // during ExecHandle teardown lets the blocked write unwind.
            drop(stdout_pump);
            drop(stderr_pump);
        })
        .map_err(|error| {
            MxcError::backend_error(format!("start WSLC exec stream thread: {error}"))
        })?;

    Ok(ExecHandle {
        stdout,
        stderr,
        stdin: null_pipe_handle(),
        stdin_closer: None,
        waiter: Box::new(move || {
            // Keep the original read handles alive until the generic adapter has
            // duplicated them and the daemon stream reaches a terminal frame.
            let _readers = (stdout_reader, stderr_reader);
            done_rx.recv().map_err(|_| {
                MxcError::backend_error(
                    "WSLc exec stream thread ended without reporting an outcome",
                )
            })?
        }),
        terminator: Box::new(move || {
            cancellation.store(true, Ordering::Release);
            terminator_client
                .cancel_exec(exec_id, run_token)
                .map_err(map_daemon_error)
        }),
    })
}

#[cfg(not(windows))]
fn exec_piped(
    _client: DaemonClient,
    _config: ExecConfig,
    _exec_id: String,
    _run_token: String,
) -> Result<ExecHandle, MxcError> {
    Err(MxcError::backend_unavailable(
        "WSLc piped execution is available only on Windows",
    ))
}

/// The daemon's inputs for one exec, with the cooperative proxy applied.
fn exec_config(
    sandbox_id: &str,
    request: &ExecutionRequest,
    exec_id: String,
    run_token: String,
) -> ExecConfig {
    // `exec_proxy_url` yields the routable URL only when the proxy is enabled
    // *and* in the required `url` form — `validate_exec` has already rejected
    // the non-`url` form before we get here, so a `None` here means the proxy
    // is disabled, not malformed.
    let env = match exec_proxy_url(request) {
        Some(proxy_url) => split_env(&crate::mxc_common::proxy_env::apply_cooperative_proxy_env(
            request.env_entries(),
            proxy_url,
        )),
        None => split_env(request.env_entries()),
    };

    ExecConfig {
        exec_id,
        run_token,
        sandbox_id: sandbox_id.to_string(),
        script_code: request.script_code.clone(),
        working_directory: request.working_directory.clone(),
        env,
        env_scope: EnvScope::of(request),
        timeout_ms: request.script_timeout,
    }
}

/// Discover (or spawn) the daemon. A discovery/spawn failure is a
/// `backend_unavailable` — the backend cannot service any phase without it.
fn connect_daemon() -> Result<DaemonClient, MxcError> {
    DaemonClient::connect().map_err(|e| {
        MxcError::backend_unavailable(format!("failed to reach the WSLc daemon: {e:#}"))
    })
}

/// The process finished, but the caller's copy of its output is incomplete.
///
/// Reported as a failure so a short capture is never mistaken for a whole one,
/// with `details.exitCode` keeping the code the process produced.
fn truncated_run_error(exit_code: i32) -> MxcError {
    MxcError::backend_error(
        "WSLc live output was truncated: it was not drained fast enough and a bounded output \
         queue overflowed",
    )
    .with_details(serde_json::json!({ "exitCode": exit_code }))
}

/// Map a typed [`DaemonError`] onto the matching wire-format [`MxcError`] code.
fn map_daemon_error(err: DaemonError) -> MxcError {
    match err {
        DaemonError::Daemon { kind, message } => match kind {
            ErrKind::NotProvisioned => MxcError::not_provisioned(message),
            ErrKind::NotStarted => MxcError::not_started(message),
            ErrKind::Unavailable => MxcError::backend_unavailable(message),
            ErrKind::Rejected => MxcError::policy_validation(message),
            ErrKind::Busy
            | ErrKind::NotReady
            | ErrKind::Protocol
            | ErrKind::Backend
            | ErrKind::Unknown => MxcError::backend_error(message),
        },
        DaemonError::Transport(e) => MxcError::backend_error(format!("{e:#}")),
    }
}

/// Validate the `wslc:<32 lowercase hex>` shape. The dispatcher already routes
/// by prefix; this is defence in depth so a malformed id surfaces as
/// `malformed_id` rather than a confusing daemon-side `not_provisioned`. The
/// grammar mirrors the daemon-minted id (`wslc:` + a UUID simple form).
fn validate_sandbox_id(sandbox_id: &str) -> Result<(), MxcError> {
    let malformed = || {
        MxcError::malformed_id(format!(
            "expected wslc:<32 lowercase hex>, got {sandbox_id:?}"
        ))
    };
    let (prefix, rest) = sandbox_id.split_once(':').ok_or_else(malformed)?;
    let is_lower_hex = rest.len() == 32
        && rest
            .bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b));
    if prefix == <WslcStateAwareRunner as StatefulSandboxBackend>::ID_PREFIX && is_lower_hex {
        Ok(())
    } else {
        Err(malformed())
    }
}

/// Build the daemon `ProvisionConfig` from the request + phase config without
/// contacting the daemon. Runs the same normalization/delegation + volume/network
/// mapping as `provision`, so the forwarded config (image, imageTarPath, volumes,
/// network) can be observed directly by a host-independent test — catching a
/// serialization/forwarding regression that an E2E run would only surface on a
/// live WSL host.
fn build_provision_config(
    request: &ExecutionRequest,
    config: Option<WslcProvisionConfig>,
) -> Result<ProvisionConfig, MxcError> {
    let image = config
        .as_ref()
        .and_then(|c| c.image.clone())
        .unwrap_or_else(|| DEFAULT_IMAGE.to_string());
    let image_tar_path = config.and_then(|c| c.image_tar_path);

    // WSLc provision-time filesystem-policy gate (D6 normalization → D3
    // delegation → denied-path overlap), shared verbatim with the one-shot
    // runner via `policy_mapping::apply_provision_policy_gate`. The daemon must
    // mount the tightened policy, so a writable alias of a readonly object never
    // leaks and a persistent daemon never mounts a path the phase caller could
    // not delegate.
    let normalized = normalize_and_check_delegation(request)?;
    let normalized_request;
    let request = match normalized {
        Some(policy) => {
            normalized_request = ExecutionRequest {
                policy,
                ..request.clone()
            };
            &normalized_request
        }
        None => request,
    };

    let volumes = build_daemon_volumes(request)?;
    let network = map_network(request);
    Ok(ProvisionConfig {
        image,
        image_tar_path,
        volumes,
        network,
    })
}

/// State-aware adapter over the shared WSLc provision policy gate
/// ([`crate::wslc_common::policy_mapping::apply_provision_policy_gate`]): runs the full
/// three-step gate (D6 normalization → D3 delegation → denied-path overlap),
/// buffering normalization diagnostics and surfacing them on stderr (stdout
/// carries the phase envelope), and maps the gate's `String` error to a
/// `policy_validation` [`MxcError`]. Returns the tightened policy when
/// normalization changed something, else `None`.
fn normalize_and_check_delegation(
    request: &ExecutionRequest,
) -> Result<Option<ContainerPolicy>, MxcError> {
    let mut logger = Logger::new(Mode::Buffer);
    let result =
        crate::wslc_common::policy_mapping::apply_provision_policy_gate(request, &mut logger);
    // Surface any normalization notes (policy tightening / unresolved paths) on
    // stderr even when the gate then fails, rather than dropping the buffer.
    let notes = logger.get_buffer();
    if !notes.is_empty() {
        eprint!("{notes}");
    }
    result.map_err(MxcError::policy_validation)
}

/// Build daemon volume mounts from the request's filesystem policy. Overlapping
/// denied paths are rejected earlier in `validate_provision`; an invalid mount
/// path (e.g. a UNC share) surfaces here as `policy_validation`.
fn build_daemon_volumes(request: &ExecutionRequest) -> Result<Vec<VolumeMount>, MxcError> {
    let mounts = crate::wslc_common::policy_mapping::build_volume_mounts(
        &request.policy.readwrite_paths,
        &request.policy.readonly_paths,
    )
    .map_err(MxcError::policy_validation)?;
    Ok(mounts
        .into_iter()
        .map(|m| VolumeMount {
            host: m.windows_path,
            container: m.container_path,
            read_only: m.read_only,
        })
        .collect())
}

/// Map the request's default network policy to the daemon's binary network
/// mode. Per-host filtering is rejected in validation, so only the default
/// policy participates: `Block` → isolated, `Allow` → bridged NAT.
fn map_network(request: &ExecutionRequest) -> NetworkMode {
    if crate::wslc_common::policy::network_is_isolated(request) {
        NetworkMode::None
    } else {
        NetworkMode::Bridged
    }
}

/// Split `"KEY=VALUE"` env entries into `(name, value)` pairs (the daemon's
/// `ExecConfig.env` shape). An entry naming no variable is dropped.
fn split_env(env: &[String]) -> Vec<(String, String)> {
    crate::mxc_common::default_env::env_pairs(env)
        .into_iter()
        .map(|(name, value)| (name.to_string(), value.to_string()))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mxc_common::models::{
        ContainerPolicy, DefaultEnvCompatibility, NetworkAction, NetworkEgressPolicy,
        NetworkIngressPolicy, ProxyAddress, ProxyConfig,
    };

    /// The exit code is unrecoverable once dropped, so failing the call must not
    /// discard it.
    #[test]
    fn a_truncated_run_reports_the_exit_code_it_produced() {
        let error = truncated_run_error(3);
        assert_eq!(
            error.code,
            crate::mxc_common::mxc_error::MxcErrorCode::BackendError
        );
        assert_eq!(
            error.details.as_ref().and_then(|d| d.get("exitCode")),
            Some(&serde_json::json!(3))
        );
    }

    #[test]
    fn an_untruncated_run_adds_no_suffix() {
        assert_eq!(truncation_suffix(false), "");
        assert!(truncation_suffix(true).contains("truncated"));
    }

    #[test]
    fn backend_key_matches_wire_format() {
        assert_eq!(
            <WslcStateAwareRunner as StatefulSandboxBackend>::BACKEND_KEY,
            "wslc"
        );
    }

    #[test]
    fn id_prefix_matches_wire_format() {
        assert_eq!(
            <WslcStateAwareRunner as StatefulSandboxBackend>::ID_PREFIX,
            "wslc"
        );
    }

    #[test]
    fn validate_sandbox_id_accepts_prefixed_id() {
        validate_sandbox_id("wslc:0123456789abcdef0123456789abcdef").unwrap();
    }

    #[test]
    fn validate_sandbox_id_rejects_wrong_prefix() {
        let err = validate_sandbox_id("iso:0123456789abcdef0123456789abcdef").unwrap_err();
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::MalformedId
        );
    }

    #[test]
    fn validate_sandbox_id_rejects_empty_tail() {
        assert!(validate_sandbox_id("wslc:").is_err());
    }

    #[test]
    fn validate_sandbox_id_rejects_bare_token() {
        assert!(validate_sandbox_id("abc123").is_err());
    }

    #[test]
    fn validate_sandbox_id_rejects_non_hex_tail() {
        // Nonempty but not the 32-hex grammar: reaches the daemon today and is
        // misreported as not_provisioned rather than malformed_id.
        assert!(validate_sandbox_id("wslc:not-a-uuid").is_err());
    }

    #[test]
    fn validate_sandbox_id_rejects_wrong_length() {
        assert!(validate_sandbox_id("wslc:abc123").is_err());
        assert!(validate_sandbox_id("wslc:0123456789abcdef0123456789abcdef0").is_err());
    }

    #[test]
    fn validate_sandbox_id_rejects_uppercase_hex() {
        assert!(validate_sandbox_id("wslc:0123456789ABCDEF0123456789abcdef").is_err());
    }

    #[test]
    fn post_provision_hooks_reject_raw_directional_network_fields() {
        let runner = WslcStateAwareRunner::new();
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                network_egress: Some(NetworkEgressPolicy::default()),
                ..Default::default()
            },
            ..Default::default()
        };
        let id = "wslc:0123456789abcdef0123456789abcdef";

        assert!(runner.validate_start(id, &request, None).is_err());
        assert!(runner.validate_exec(id, &request, None).is_err());
        assert!(runner.validate_stop(id, &request, None).is_err());
        assert!(runner.validate_deprovision(id, &request, None).is_err());
    }

    /// Enumerating all five hooks (rather than testing the shared validator) is
    /// what catches a hook that forgets to call its validator at all.
    #[test]
    fn every_validate_hook_rejects_supplied_ui() {
        let runner = WslcStateAwareRunner::new();
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                ui_specified: true,
                ..Default::default()
            },
            ..Default::default()
        };
        let id = "wslc:0123456789abcdef0123456789abcdef";

        let results = [
            ("provision", runner.validate_provision(&request, None)),
            ("start", runner.validate_start(id, &request, None)),
            ("exec", runner.validate_exec(id, &request, None)),
            ("stop", runner.validate_stop(id, &request, None)),
            (
                "deprovision",
                runner.validate_deprovision(id, &request, None),
            ),
        ];
        for (phase, result) in results {
            let err = result.expect_err(&format!("{phase} must reject a supplied ui"));
            assert_eq!(
                err.code,
                crate::mxc_common::mxc_error::MxcErrorCode::PolicyValidation
            );
            assert!(
                err.message.contains("ui section is not supported"),
                "{phase}: {}",
                err.message
            );
        }
    }

    /// Refused at provision, the only phase where the network posture is
    /// settable — so neither can be silently dropped into the daemon's
    /// `ProvisionConfig`, which carries only the binary [`NetworkMode`].
    #[test]
    fn validate_provision_rejects_unimplementable_network_posture() {
        let runner = WslcStateAwareRunner::new();
        for (policy, needle) in [
            (
                ContainerPolicy {
                    allow_local_network: true,
                    ..Default::default()
                },
                "allowLocalNetwork",
            ),
            (
                ContainerPolicy {
                    network_enforcement_mode:
                        crate::mxc_common::models::NetworkEnforcementMode::Firewall,
                    ..Default::default()
                },
                "enforcementMode",
            ),
        ] {
            let request = ExecutionRequest {
                policy,
                ..Default::default()
            };
            let err = runner
                .validate_provision(&request, None)
                .expect_err(&format!("provision must reject {needle}"));
            assert_eq!(
                err.code,
                crate::mxc_common::mxc_error::MxcErrorCode::PolicyValidation
            );
            assert!(err.message.contains(needle), "got: {}", err.message);
        }
    }

    /// Guards against over-rejection. Each value is the near-miss of a rejected
    /// one, so a gate that flipped between value- and presence-based would fail
    /// here only.
    #[test]
    fn validate_provision_accepts_the_postures_wslc_can_honour() {
        let runner = WslcStateAwareRunner::new();
        for (label, policy) in [
            (
                "explicit capabilities enforcement mode",
                ContainerPolicy {
                    network_enforcement_mode:
                        crate::mxc_common::models::NetworkEnforcementMode::Capabilities,
                    ..Default::default()
                },
            ),
            (
                "explicit allowLocalNetwork=false",
                ContainerPolicy {
                    allow_local_network: false,
                    ..Default::default()
                },
            ),
            (
                "absent ui",
                ContainerPolicy {
                    ui_specified: false,
                    ..Default::default()
                },
            ),
        ] {
            let request = ExecutionRequest {
                policy,
                ..Default::default()
            };
            assert!(
                runner.validate_provision(&request, None).is_ok(),
                "{label} is honoured by WSLc and must not be rejected"
            );
        }
    }

    #[test]
    fn map_network_maps_block_to_none() {
        let req = ExecutionRequest {
            policy: ContainerPolicy {
                default_network_policy: NetworkPolicy::Block,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_eq!(map_network(&req), NetworkMode::None);
    }

    #[test]
    fn map_network_maps_allow_to_bridged() {
        let req = ExecutionRequest {
            policy: ContainerPolicy {
                default_network_policy: NetworkPolicy::Allow,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_eq!(map_network(&req), NetworkMode::Bridged);
    }

    #[test]
    fn build_provision_config_maps_directional_postures_to_daemon_modes() {
        for (action, expected) in [
            (NetworkAction::Deny, NetworkMode::None),
            (NetworkAction::Allow, NetworkMode::Bridged),
        ] {
            let request = ExecutionRequest {
                policy: ContainerPolicy {
                    network_egress: Some(NetworkEgressPolicy {
                        default: action,
                        ..Default::default()
                    }),
                    network_ingress: Some(NetworkIngressPolicy {
                        default: action,
                        host_loopback: action,
                    }),
                    ..Default::default()
                },
                ..Default::default()
            };

            let config = build_provision_config(&request, None).unwrap();
            assert_eq!(config.network, expected);
        }
    }

    #[test]
    fn build_provision_config_forwards_image_and_tar_path() {
        let phase = WslcProvisionConfig {
            image: Some("custom/image:tag".to_string()),
            image_tar_path: Some("C:\\images\\custom.tar".to_string()),
        };
        let cfg = build_provision_config(&ExecutionRequest::default(), Some(phase)).unwrap();
        assert_eq!(cfg.image, "custom/image:tag");
        assert_eq!(
            cfg.image_tar_path.as_deref(),
            Some("C:\\images\\custom.tar")
        );
    }

    #[test]
    fn build_provision_config_defaults_image_and_omits_tar_when_absent() {
        for phase in [None, Some(WslcProvisionConfig::default())] {
            let cfg = build_provision_config(&ExecutionRequest::default(), phase).unwrap();
            assert_eq!(cfg.image, "alpine:latest");
            assert!(cfg.image_tar_path.is_none());
        }
    }

    #[test]
    fn build_provision_config_preserves_each_field_and_empty_strings() {
        for (phase, expected_image, expected_tar_path) in [
            (
                WslcProvisionConfig {
                    image: Some("custom/image:tag".to_string()),
                    image_tar_path: None,
                },
                "custom/image:tag",
                None,
            ),
            (
                WslcProvisionConfig {
                    image: None,
                    image_tar_path: Some("C:\\images\\custom.tar".to_string()),
                },
                "alpine:latest",
                Some("C:\\images\\custom.tar"),
            ),
            (
                WslcProvisionConfig {
                    image: Some(String::new()),
                    image_tar_path: Some(String::new()),
                },
                "",
                Some(""),
            ),
        ] {
            let cfg = build_provision_config(&ExecutionRequest::default(), Some(phase)).unwrap();
            assert_eq!(cfg.image, expected_image);
            assert_eq!(cfg.image_tar_path.as_deref(), expected_tar_path);
        }
    }

    #[test]
    fn build_provision_config_rejects_denied_overlap_after_normalization() {
        // Guard: `build_provision_config` must re-run the denied-path overlap
        // check on the (post-normalization) lists, mirroring the one-shot
        // runner. A `deniedPaths` entry nested under a mounted parent has no
        // masking primitive on WSLc's flat mount surface, so it must be
        // rejected here even though the surrounding dispatcher also validates.
        let req = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\parent".to_string()],
                denied_paths: vec!["C:\\parent\\secret".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let err = build_provision_config(&req, None).unwrap_err();
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::PolicyValidation
        );
        assert!(err.message.contains("deniedPaths"), "got: {}", err.message);
    }

    #[test]
    fn build_daemon_volumes_maps_rw_and_ro() {
        let req = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\src".to_string()],
                readonly_paths: vec!["D:\\data".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let volumes = build_daemon_volumes(&req).unwrap();
        assert_eq!(volumes.len(), 2);
        assert_eq!(volumes[0].host, "C:\\src");
        assert_eq!(volumes[0].container, "/mnt/c/src");
        assert!(!volumes[0].read_only);
        assert_eq!(volumes[1].container, "/mnt/d/data");
        assert!(volumes[1].read_only);
    }

    #[test]
    fn build_daemon_volumes_rejects_unc_path() {
        let req = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["\\\\server\\share".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let err = build_daemon_volumes(&req).unwrap_err();
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::PolicyValidation
        );
    }

    #[test]
    fn normalize_and_check_delegation_empty_policy_is_none() {
        let req = ExecutionRequest::default();
        assert!(normalize_and_check_delegation(&req).unwrap().is_none());
    }

    #[test]
    fn normalize_and_check_delegation_tightens_rw_alias_of_ro() {
        // The same host object listed both readwrite and readonly must be
        // tightened to readonly (D6) before the daemon mounts it, so a writable
        // alias of a readonly object never leaks.
        let dir = tempfile::tempdir().unwrap();
        let d = dir.path().to_str().unwrap().to_string();
        let req = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec![d.clone()],
                readonly_paths: vec![d.clone()],
                ..Default::default()
            },
            ..Default::default()
        };
        let tightened = normalize_and_check_delegation(&req)
            .unwrap()
            .expect("aliasing conflict should tighten the policy");
        assert!(tightened.readwrite_paths.is_empty());
        assert_eq!(tightened.readonly_paths, vec![d]);
    }

    #[cfg(windows)]
    #[test]
    fn provision_delegation_tightens_rw_alias_of_denied_and_drops_mount() {
        // A writable path that resolves to the same object as a `denied` entry
        // (here a case-variant string on case-insensitive NTFS) must tighten to
        // denied, and the daemon volumes must be built from that tightened policy
        // — otherwise the writable alias would still be mounted, granting access
        // the deny was meant to block.
        let dir = tempfile::tempdir().unwrap();
        let denied = dir.path().to_str().unwrap().to_string();
        let rw_alias = denied.to_uppercase();
        let raw = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec![rw_alias],
                denied_paths: vec![denied],
                ..Default::default()
            },
            ..Default::default()
        };
        // Pre-fix behaviour the bug relied on: the raw request WOULD mount the
        // alias writable.
        let raw_mounts = build_daemon_volumes(&raw).unwrap();
        assert_eq!(raw_mounts.len(), 1);
        assert!(!raw_mounts[0].read_only);

        let tightened = normalize_and_check_delegation(&raw)
            .unwrap()
            .expect("rw alias of a denied object should tighten");
        assert!(tightened.readwrite_paths.is_empty());
        assert!(!tightened.denied_paths.is_empty());
        let tightened_req = ExecutionRequest {
            policy: tightened,
            ..raw.clone()
        };
        assert!(build_daemon_volumes(&tightened_req).unwrap().is_empty());
    }

    #[cfg(windows)]
    #[test]
    fn provision_delegation_rejects_inaccessible_path() {
        // A delegated path the invoking user cannot access must fail closed
        // before provisioning mounts anything. `C:\mxc_invalid<name` is an
        // illegal name → ERROR_INVALID_NAME → not-accessible (not merely
        // missing), so delegation rejects it.
        let req = ExecutionRequest {
            policy: ContainerPolicy {
                readonly_paths: vec!["C:\\mxc_invalid<name".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let err = normalize_and_check_delegation(&req).unwrap_err();
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::PolicyValidation
        );
    }

    #[test]
    fn split_env_splits_pairs_and_drops_bare_keys() {
        let env = vec![
            "PATH=/usr/bin".to_string(),
            "EMPTY=".to_string(),
            "BARE".to_string(),
            "URL=http://a=b".to_string(),
        ];
        let pairs = split_env(&env);
        assert_eq!(pairs[0], ("PATH".to_string(), "/usr/bin".to_string()));
        assert_eq!(pairs[1], ("EMPTY".to_string(), String::new()));
        // Only the first '=' splits; the value keeps the rest verbatim.
        assert_eq!(pairs[2], ("URL".to_string(), "http://a=b".to_string()));
        assert_eq!(pairs.len(), 3);
    }

    #[test]
    fn the_exec_config_carries_the_scope_each_state_of_process_env_selects() {
        struct Case {
            label: &'static str,
            compatibility: DefaultEnvCompatibility,
            env: Option<Vec<&'static str>>,
            inherit_default_env: bool,
            scope: EnvScope,
            entries: &'static [(&'static str, &'static str)],
        }

        let cases = [
            Case {
                label: "omitted takes the image environment",
                compatibility: DefaultEnvCompatibility::DefaultBlock,
                env: None,
                inherit_default_env: false,
                scope: EnvScope::Merge,
                entries: &[],
            },
            Case {
                label: "explicitly empty leaves the child nothing",
                compatibility: DefaultEnvCompatibility::DefaultBlock,
                env: Some(vec![]),
                inherit_default_env: false,
                scope: EnvScope::Replace,
                entries: &[],
            },
            Case {
                label: "explicitly empty plus inheritDefaultEnv takes the image environment",
                compatibility: DefaultEnvCompatibility::DefaultBlock,
                env: Some(vec![]),
                inherit_default_env: true,
                scope: EnvScope::Merge,
                entries: &[],
            },
            Case {
                label: "supplied is used verbatim",
                compatibility: DefaultEnvCompatibility::DefaultBlock,
                env: Some(vec!["FOO=bar"]),
                inherit_default_env: false,
                scope: EnvScope::Replace,
                entries: &[("FOO", "bar")],
            },
            Case {
                label: "supplied plus inheritDefaultEnv layers over the image environment",
                compatibility: DefaultEnvCompatibility::DefaultBlock,
                env: Some(vec!["FOO=bar"]),
                inherit_default_env: true,
                scope: EnvScope::Merge,
                entries: &[("FOO", "bar")],
            },
        ];

        for case in cases {
            let request = ExecutionRequest {
                default_env_compatibility: case.compatibility,
                env: case.env.map(|e| e.into_iter().map(String::from).collect()),
                inherit_default_env: case.inherit_default_env,
                script_code: "echo hi".to_string(),
                ..Default::default()
            };

            let config = exec_config(
                "wslc:abc",
                &request,
                "exec-1".to_string(),
                "run-1".to_string(),
            );

            assert_eq!(config.env_scope, case.scope, "scope for {}", case.label);
            let expected: Vec<(String, String)> = case
                .entries
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect();
            assert_eq!(config.env, expected, "entries for {}", case.label);
            assert_eq!(config.sandbox_id, "wslc:abc");
            assert_eq!(config.script_code, "echo hi");
        }
    }

    #[test]
    fn the_exec_config_keeps_the_cooperative_proxy_out_of_the_callers_reach() {
        let request = ExecutionRequest {
            default_env_compatibility: DefaultEnvCompatibility::DefaultBlock,
            env: Some(vec![
                "FOO=bar".to_string(),
                "HTTP_PROXY=http://attacker.invalid:1".to_string(),
            ]),
            policy: ContainerPolicy {
                network_proxy: ProxyConfig {
                    address: Some(ProxyAddress::from_url(
                        "http://127.0.0.1:8888",
                        "127.0.0.1".to_string(),
                        8888,
                    )),
                    builtin_test_server: false,
                },
                ..Default::default()
            },
            ..Default::default()
        };

        let config = exec_config(
            "wslc:abc",
            &request,
            "exec-1".to_string(),
            "run-1".to_string(),
        );

        // Replacement still applies, so the proxy variables must survive into
        // the entries argv carries rather than being left to the SDK's setter.
        assert_eq!(config.env_scope, EnvScope::Replace);
        let value = |name: &str| {
            config
                .env
                .iter()
                .find(|(k, _)| k == name)
                .map(|(_, v)| v.clone())
        };
        assert_eq!(value("FOO").as_deref(), Some("bar"));
        assert_eq!(
            value("HTTP_PROXY").as_deref(),
            Some("http://127.0.0.1:8888")
        );
        assert_eq!(value("NO_PROXY").as_deref(), Some(""));
        assert!(!config.env.iter().any(|(_, v)| v.contains("attacker")));
    }

    #[test]
    fn map_daemon_error_preserves_not_provisioned() {
        let err = map_daemon_error(DaemonError::Daemon {
            kind: ErrKind::NotProvisioned,
            message: "unknown sandbox".to_string(),
        });
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::NotProvisioned
        );
    }

    #[test]
    fn map_daemon_error_preserves_not_started() {
        let err = map_daemon_error(DaemonError::Daemon {
            kind: ErrKind::NotStarted,
            message: "not started".to_string(),
        });
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::NotStarted
        );
    }

    #[test]
    fn map_daemon_error_collapses_busy_to_backend_error() {
        let err = map_daemon_error(DaemonError::Daemon {
            kind: ErrKind::Busy,
            message: "busy".to_string(),
        });
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::BackendError
        );
    }

    #[test]
    fn map_daemon_error_preserves_unavailable() {
        let err = map_daemon_error(DaemonError::Daemon {
            kind: ErrKind::Unavailable,
            message: "WSLc components are missing".to_string(),
        });
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::BackendUnavailable
        );
    }

    #[test]
    fn map_daemon_error_preserves_rejected() {
        let err = map_daemon_error(DaemonError::Daemon {
            kind: ErrKind::Rejected,
            message: "network.allowLocalNetwork=true is not supported".to_string(),
        });
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::PolicyValidation
        );
    }

    #[test]
    fn map_daemon_error_degrades_unknown_to_backend_error() {
        let err = map_daemon_error(DaemonError::Daemon {
            kind: ErrKind::Unknown,
            message: "from a newer daemon".to_string(),
        });
        assert_eq!(
            err.code,
            crate::mxc_common::mxc_error::MxcErrorCode::BackendError
        );
    }
}