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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! TraceLogging ETW telemetry for MXC.
//!
//! Provides structured event emission for execution observability
//! and adoption metrics. Events are emitted to the local ETW subsystem
//! via the `mxc_telemetry` crate (pure Rust, using the `tracelogging`
//! crate). Every event includes common fields (Version, Channel,
//! IsDebugging, UTCReplace_AppSessionGuid) as Part C custom event data.
//!
//! On non-Windows platforms, all telemetry functions are no-ops.

pub mod consent;
pub mod consent_cli;
pub mod consent_prompt;
mod consent_protocol;
pub mod correlation_state;
pub mod correlation_vector;
pub mod events;
pub mod policy;

use std::time::Duration;

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;

use crate::mxc_common::logger::Logger;
use crate::mxc_common::models::{
    ContainmentBackend, FailurePhase, ScriptResponse, TelemetryConfig,
};
use crate::mxc_common::mxc_error::{MxcError, MxcErrorCode};
use crate::mxc_common::state_aware_dispatch::DispatchOutcome;

pub use consent::ConsentState;
pub use events::{
    log_config_rejected, log_enforcement_degraded, log_error, log_execution,
    log_network_policy_applied, log_policy_hash, log_process_event, log_sandbox_torn_down,
    log_verbose, ExecutionEvent, FailureReason, ProcessEvent, TelemetryContext, VerboseEvent,
};
pub use policy::PolicyState;

#[cfg(target_os = "windows")]
#[derive(Default)]
struct FailureReporter {
    reported: Mutex<std::collections::HashSet<String>>,
}

#[cfg(target_os = "windows")]
impl FailureReporter {
    fn report(&self, signature: String, emit: impl FnOnce(&str)) {
        let is_new = self
            .reported
            .lock()
            .unwrap_or_else(|error| error.into_inner())
            .insert(signature.clone());
        if is_new {
            emit(&signature);
        }
    }
}

/// Conventional process exit code for a Rust panic/abort. Used as the reported
/// `exit_code` on crash telemetry, since the panicking process has not (and
/// will not) produce a real [`ScriptResponse`].
const PANIC_EXIT_CODE: i32 = 101;

/// Reported exit code for a cancelled run.
const CANCELLED_EXIT_CODE: i32 = 130;

/// Backend attribution for out-of-band events.
static PROCESS_BACKEND: Mutex<Option<&'static str>> = Mutex::new(None);

/// Caller-requested containment attribution for out-of-band events.
static PROCESS_SANDBOX_KIND: Mutex<Option<&'static str>> = Mutex::new(None);

/// State-aware phase attribution for out-of-band events.
static PROCESS_PHASE: Mutex<Option<&'static str>> = Mutex::new(None);

/// Correlation-vector attribution for out-of-band events.
static PROCESS_CORRELATION_VECTOR: Mutex<Option<String>> = Mutex::new(None);

/// Prevents duplicate terminal events for one executor process.
static HAS_EMITTED: AtomicBool = AtomicBool::new(false);

#[cfg(test)]
thread_local! {
    /// Test-only override that forces [`emit_active`] to report the provider as
    /// active, so the emit glue in [`emit_panic`]/[`emit_cancellation`] can be
    /// exercised deterministically on every platform (ETW registration only
    /// succeeds on Windows). Thread-local (not a global `AtomicBool`) so a
    /// concurrent `#[should_panic]` test on another thread can't observe a
    /// telemetry test's forced-active state and trip the global panic hook into
    /// the sink. Never set outside tests.
    static TEST_FORCE_ACTIVE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
    /// Optional authorization result used to prove the live gate independently
    /// of platform consent storage.
    static TEST_AUTHORIZATION_OVERRIDE: std::cell::Cell<Option<bool>> = const {
        std::cell::Cell::new(None)
    };
}

/// Whether the provider is active, including the test override.
fn emit_active() -> bool {
    #[cfg(test)]
    if TEST_FORCE_ACTIVE.with(|f| f.get()) {
        return true;
    }
    crate::mxc_telemetry::is_active()
}

/// Evaluate consent and policy for one complete logical emission.
fn authorization_allows_emission() -> bool {
    #[cfg(test)]
    if let Some(allowed) = TEST_AUTHORIZATION_OVERRIDE.with(|value| value.get()) {
        return allowed;
    }

    consent::get_consent().allows_collection() && policy::get_policy().allows_collection()
}

fn invocation_can_emit(active: bool) -> bool {
    active && authorization_allows_emission()
}

fn process_can_emit() -> bool {
    emit_active() && authorization_allows_emission()
}

/// Proof that one complete logical telemetry emission is authorized.
///
/// Pair-writing helpers require this token so their `Execution` and `Error`
/// events share one consent and policy decision without repeating blocking
/// storage reads between the two writes.
///
/// The type is zero-sized, not `Copy`/`Clone`, and has a private constructor,
/// so it cannot be forged, split across unrelated emissions, or reused after
/// authorization has already been consumed by a previous emission.
#[must_use = "an emission authorization does nothing unless events are written under it"]
struct EmissionAuthorization {
    _private: (),
}

impl EmissionAuthorization {
    /// Evaluate the authorization decision for a request-scoped emission
    /// (the one-shot completion / early-exit / state-aware paths). Returns
    /// `Some` only when the caller's cached `active` flag *and* the current
    /// authorization state (consent && policy) both permit emission.
    fn for_invocation(active: bool) -> Option<Self> {
        invocation_can_emit(active).then_some(Self { _private: () })
    }

    /// Evaluate the authorization decision for a process-scoped, out-of-band
    /// emission — used by the panic hook and the console-control (Ctrl-C)
    /// handler, where no request context is in scope. Consults the live
    /// provider state via [`emit_active`] rather than a cached `active` flag.
    fn for_process() -> Option<Self> {
        process_can_emit().then_some(Self { _private: () })
    }
}
/// Claim the terminal-event slot for this process.
fn already_emitted() -> bool {
    HAS_EMITTED.swap(true, Ordering::SeqCst)
}

/// Reset all per-process telemetry state (the exactly-once emit slot and the
/// stashed backend / phase / correlation-id context) so tests can drive the emit
/// paths from a known-clean baseline. Tests that touch this state must hold
/// [`TEST_LOCK`] for the duration, since the state is process-global.
#[cfg(test)]
fn reset_for_test() {
    HAS_EMITTED.store(false, Ordering::SeqCst);
    *PROCESS_BACKEND.lock().unwrap_or_else(|e| e.into_inner()) = None;
    *PROCESS_SANDBOX_KIND
        .lock()
        .unwrap_or_else(|e| e.into_inner()) = None;
    *PROCESS_PHASE.lock().unwrap_or_else(|e| e.into_inner()) = None;
    *PROCESS_CORRELATION_VECTOR
        .lock()
        .unwrap_or_else(|e| e.into_inner()) = None;
    TEST_FORCE_ACTIVE.with(|f| f.set(false));
    TEST_AUTHORIZATION_OVERRIDE.with(|value| value.set(None));
    events::test_sink::clear();
}

/// MXC version string, set at compile time.
const MXC_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Build channel — `"dev"` for debug builds, `"release"` for release builds.
#[cfg(debug_assertions)]
const MXC_CHANNEL: &str = "dev";
#[cfg(not(debug_assertions))]
const MXC_CHANNEL: &str = "release";

/// Returns the MXC version string.
pub fn version() -> &'static str {
    MXC_VERSION
}

/// Returns whether the process-local ETW provider is currently registered.
pub fn is_active() -> bool {
    crate::mxc_telemetry::is_active()
}

/// Returns whether this invocation may emit telemetry.
///
/// Resolution:
/// - `telemetry.enabled` in JSON config — explicit kill-switch/opt-in.
/// - Persisted user consent state — must be `Granted`.
/// - Administrative policy ceiling — must allow collection.
///
/// All three terms must hold. The function fails closed if either consent or
/// policy cannot be read: those helpers resolve to non-collecting states.
pub fn is_enabled(config: &TelemetryConfig) -> bool {
    // Only an explicit opt-in enables telemetry for this invocation.
    if config.enabled != Some(true) {
        return false;
    }
    policy::get_policy().allows_collection() && consent::get_consent().allows_collection()
}

/// Initialize the telemetry provider when the gates allow it.
pub fn init(config: &TelemetryConfig, logger: &mut Logger) -> bool {
    if !is_enabled(config) {
        log_suppression_reason(config, logger);
        return false;
    }

    let activated = crate::mxc_telemetry::init(MXC_VERSION, MXC_CHANNEL);
    if !activated && cfg!(target_os = "windows") {
        logger.warning_line(
            "telemetry: ETW provider registration failed; continuing without telemetry",
        );
    }
    if activated && !crate::mxc_telemetry::IS_UTC_ROUTED {
        logger.warning_line(
            "telemetry: events are emitted to local ETW only; this build has no provider group \
             GUID, so nothing is routed to the Microsoft pipeline (set \
             MXC_TELEMETRY_PROVIDER_GROUP_GUID at build time for an internal build)",
        );
    }
    activated
}

/// Explain, on the diagnostic log, exactly which gate turned telemetry off.
///
/// Without this an operator who set `telemetry.enabled: true` sees no events
/// and no reason — every gate fails closed and silently. Each conjunct of
/// [`is_enabled`] is reported independently so the log distinguishes "the run
/// never asked" from "the user has not consented" from "an administrator
/// blocked it".
fn log_suppression_reason(config: &TelemetryConfig, logger: &mut Logger) {
    if config.enabled != Some(true) {
        logger.log_line("telemetry: not requested for this run (telemetry.enabled is not true)");
        return;
    }

    let policy = policy::get_policy();
    let consent = consent::get_consent();
    logger.log_line(&format!(
        "telemetry: requested but suppressed (consent={}, policy={}); \
         MXC consent is independent of the Windows diagnostic-data setting",
        consent.as_str(),
        policy.as_str()
    ));
}

/// Unregister the telemetry provider.
pub fn shutdown() {
    crate::mxc_telemetry::shutdown();
}

fn sandbox_kind_for<'a>(backend: &'a str, requested: Option<&'a str>) -> &'a str {
    requested.unwrap_or(backend)
}

/// Classify a failed execution into a bounded [`FailureReason`].
fn classify_failure(phase: &FailurePhase) -> FailureReason {
    match phase {
        FailurePhase::LaunchFailed
        | FailurePhase::BackendUnavailable
        | FailurePhase::PostLaunchFailed => FailureReason::InitError,
        FailurePhase::Rejected => FailureReason::PolicyError,
        FailurePhase::Timeout => FailureReason::Timeout,
        FailurePhase::ProcessExited | FailurePhase::None => FailureReason::ProcessError,
    }
}

/// Emit completion telemetry for an executor invocation.
pub fn emit_completion(
    active: bool,
    containment: &ContainmentBackend,
    response: &ScriptResponse,
    elapsed: Duration,
) {
    emit_completion_with_kind(active, containment, None, response, elapsed);
}

/// Emit completion telemetry with the caller-requested containment kind.
pub fn emit_completion_with_kind(
    active: bool,
    containment: &ContainmentBackend,
    requested_sandbox_kind: Option<&'static str>,
    response: &ScriptResponse,
    elapsed: Duration,
) {
    let Some(auth) = EmissionAuthorization::for_invocation(active) else {
        return;
    };
    if already_emitted() {
        return;
    }
    emit_completion_event(
        &auth,
        containment,
        response,
        elapsed,
        requested_sandbox_kind,
    );
    shutdown();
}

fn emit_completion_event(
    _auth: &EmissionAuthorization,
    containment: &ContainmentBackend,
    response: &ScriptResponse,
    elapsed: Duration,
    requested_sandbox_kind: Option<&str>,
) {
    // The `_auth` parameter is the single authorization token for this
    // emission (see `EmissionAuthorization`). Both writes below execute under
    // it — there is no per-write reread of consent/policy state.
    let backend = containment.wire_name();
    let sandbox_kind = sandbox_kind_for(backend, requested_sandbox_kind);
    let failed = response.exit_code != 0;
    let outcome = if failed { "failure" } else { "success" };
    let failure_reason = failed.then(|| classify_failure(&response.failure_phase));

    log_execution(&ExecutionEvent {
        backend,
        sandbox_kind,
        exit_code: response.exit_code,
        outcome,
        duration_ms: elapsed.as_millis() as u64,
        failure_reason,
        // One-shot execution — no state-aware lifecycle phase.
        phase: "",
        // One-shot execution — already correlated by AppSessionGuid, no
        // cross-phase lifecycle to join.
        correlation_vector: "",
    });

    // The presence of an error message signals an infrastructure error (as
    // opposed to a script that merely exited non-zero). We use it only as a
    // boolean signal — the message text itself is never emitted.
    if failed && !response.error_message.is_empty() {
        log_error(
            TelemetryContext {
                backend,
                phase: "",
                correlation_vector: "",
            },
            sandbox_kind,
            classify_failure(&response.failure_phase),
            response.exit_code,
        );
    }
}

/// Emit completion telemetry for an in-process SDK invocation.
///
/// Unlike [`emit_completion`], this does not use the executable-wide
/// exactly-once slot: SDK processes may run multiple or concurrent sandboxes,
/// and their handle wrappers enforce exactly-once emission per invocation.
pub fn emit_sdk_completion(
    active: bool,
    containment: &ContainmentBackend,
    response: &ScriptResponse,
    elapsed: Duration,
) {
    emit_sdk_completion_with_kind(active, containment, None, response, elapsed);
}

/// Emit SDK completion telemetry with the request-scoped sandbox kind.
pub fn emit_sdk_completion_with_kind(
    active: bool,
    containment: &ContainmentBackend,
    requested_sandbox_kind: Option<&'static str>,
    response: &ScriptResponse,
    elapsed: Duration,
) {
    emit_sdk_with_release(active, |auth| {
        emit_completion_event(auth, containment, response, elapsed, requested_sandbox_kind)
    });
}

/// Emit failure telemetry for an early-exit path that terminates **before** a
/// runner produces a [`ScriptResponse`], then shut the provider down. No-op
/// when `active` is `false`.
///
/// One-shot executors validate configuration and select a backend before
/// running; failures there call `process::exit` directly and would otherwise
/// bypass [`emit_completion`] entirely. This records an `Execution` event
/// (exit code 1, `failure` outcome) plus an `Error` event carrying the
/// bounded `reason` category and exit code, so config/policy/init failures are
/// observable. `duration_ms` is reported as `0` because no execution occurred.
pub fn emit_early_exit(active: bool, containment: &ContainmentBackend, reason: FailureReason) {
    emit_early_exit_with_kind(active, containment, None, reason);
}

/// Emit early-exit telemetry with the caller-requested containment kind.
pub fn emit_early_exit_with_kind(
    active: bool,
    containment: &ContainmentBackend,
    requested_sandbox_kind: Option<&'static str>,
    reason: FailureReason,
) {
    let Some(auth) = EmissionAuthorization::for_invocation(active) else {
        return;
    };
    if already_emitted() {
        return;
    }
    emit_early_exit_event(&auth, containment, reason, requested_sandbox_kind);
    shutdown();
}

fn emit_early_exit_event(
    _auth: &EmissionAuthorization,
    containment: &ContainmentBackend,
    reason: FailureReason,
    requested_sandbox_kind: Option<&str>,
) {
    // The `_auth` parameter is the single authorization token for this
    // emission (see `EmissionAuthorization`). Both writes below execute under
    // it — there is no per-write reread of consent/policy state.
    let backend = containment.wire_name();
    let sandbox_kind = sandbox_kind_for(backend, requested_sandbox_kind);

    log_execution(&ExecutionEvent {
        backend,
        sandbox_kind,
        exit_code: 1,
        outcome: "failure",
        duration_ms: 0,
        failure_reason: Some(reason),
        // One-shot early-exit — no state-aware lifecycle phase.
        phase: "",
        // One-shot early-exit — no cross-phase lifecycle to correlate.
        correlation_vector: "",
    });

    log_error(
        TelemetryContext {
            backend,
            phase: "",
            correlation_vector: "",
        },
        sandbox_kind,
        reason,
        1,
    );
}

/// Emit an SDK spawn failure without claiming the executable-wide terminal slot.
pub fn emit_sdk_early_exit(active: bool, containment: &ContainmentBackend, reason: FailureReason) {
    emit_sdk_early_exit_with_kind(active, containment, None, reason);
}

/// Emit SDK early-exit telemetry with the request-scoped sandbox kind.
pub fn emit_sdk_early_exit_with_kind(
    active: bool,
    containment: &ContainmentBackend,
    requested_sandbox_kind: Option<&'static str>,
    reason: FailureReason,
) {
    emit_sdk_with_release(active, |auth| {
        emit_early_exit_event(auth, containment, reason, requested_sandbox_kind)
    });
}

fn emit_sdk_with_release(active: bool, emit: impl FnOnce(&EmissionAuthorization)) {
    if !active {
        return;
    }
    if let Some(auth) = EmissionAuthorization::for_invocation(active) {
        emit(&auth);
    }
    shutdown();
}

/// Emit one verbose-document chunk under a fresh live authorization decision.
///
/// Returns `false` when collection is no longer authorized, allowing a caller
/// to stop emitting the remaining chunks immediately.
pub fn emit_verbose(active: bool, event: &VerboseEvent<'_>) -> Result<bool, u32> {
    let Some(_auth) = EmissionAuthorization::for_invocation(active) else {
        return Ok(false);
    };
    match log_verbose(event) {
        0 => Ok(true),
        status => Err(status),
    }
}

/// Record the containment backend for this process so best-effort emit paths
/// that have no [`ScriptResponse`] in scope (the panic hook and the console
/// control handler) can attribute their events.
///
/// Call once, immediately after a successful [`init`]. Later calls are ignored
/// (the value is set-once).
pub fn set_process_context(containment: &ContainmentBackend) {
    set_process_context_with_kind(containment, None);
}

/// Record both the resolved backend and the caller-requested containment kind
/// for out-of-band events.
pub fn set_process_context_with_kind(
    containment: &ContainmentBackend,
    requested_sandbox_kind: Option<&'static str>,
) {
    let backend = containment.wire_name();
    let mut slot = PROCESS_BACKEND.lock().unwrap_or_else(|e| e.into_inner());
    if slot.is_none() {
        *slot = Some(backend);
    }
    drop(slot);

    let mut slot = PROCESS_SANDBOX_KIND
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    if slot.is_none() {
        *slot = Some(sandbox_kind_for(backend, requested_sandbox_kind));
    }
}

/// Sentinel backend name used when no process backend was recorded (e.g. a
/// panic before [`set_process_context`] ran).
const UNKNOWN_BACKEND: &str = "unknown";

/// The stashed process backend wire-name, or [`UNKNOWN_BACKEND`] if none was
/// recorded.
///
/// Uses `try_lock`: this runs from the panic hook / console control handler,
/// which can fire *while the main thread holds* [`PROCESS_BACKEND`] (e.g. a
/// panic inside a setter). A blocking `lock()` would then deadlock the very path
/// meant to record the crash, so on contention (or poison) we fall back to the
/// sentinel rather than block.
fn process_backend() -> &'static str {
    let stored = PROCESS_BACKEND.try_lock().ok().and_then(|slot| *slot);
    resolve_backend_name(stored)
}

/// The caller-requested containment kind, defaulting to the resolved backend
/// when no request-scoped value was recorded.
fn process_sandbox_kind() -> &'static str {
    PROCESS_SANDBOX_KIND
        .try_lock()
        .ok()
        .and_then(|slot| *slot)
        .unwrap_or_else(process_backend)
}

/// Pure defaulting for the process backend: the stashed value, or
/// [`UNKNOWN_BACKEND`] when unset. Split out (global-free) so the fallback
/// behaviour is unit-testable without writing the set-once [`PROCESS_BACKEND`].
fn resolve_backend_name(stored: Option<&'static str>) -> &'static str {
    stored.unwrap_or(UNKNOWN_BACKEND)
}

/// Record the state-aware lifecycle phase for this process so best-effort emit
/// paths that have no outcome in scope (the panic hook and the console control
/// handler) can attribute their events to the phase that was executing.
///
/// Call once, from the state-aware entry point after resolving the phase.
/// One-shot executions never call this, so their out-of-band events keep the
/// empty (`""`) phase. Later calls are ignored (the value is set-once).
pub fn set_process_phase(phase: &'static str) {
    let mut slot = PROCESS_PHASE.lock().unwrap_or_else(|e| e.into_inner());
    if slot.is_none() {
        *slot = Some(phase);
    }
}

/// The stashed state-aware phase, or `""` (one-shot / not yet set).
///
/// Uses `try_lock` for the same reentrancy-deadlock reason as
/// [`process_backend`]: it runs from the out-of-band crash paths.
fn process_phase() -> &'static str {
    let stored = PROCESS_PHASE.try_lock().ok().and_then(|slot| *slot);
    resolve_phase_name(stored)
}

/// Record the lifecycle correlation vector (MS-CV span) for this process so the
/// out-of-band emit paths (panic hook, console control handler) can tag their
/// events with it. Call once, from the state-aware entry point, passing the
/// seeded/spun MS-CV for this phase (see [`correlation_vector`]). One-shot
/// executions never call this. Later calls are ignored (the value is set-once).
pub fn set_process_correlation_vector(correlation_vector: &str) {
    // Allocate the owned copy *before* taking the lock so the critical section
    // does no allocation — a panic mid-allocation while holding the lock would
    // otherwise deadlock the panic hook's `process_correlation_vector` reader.
    let owned = correlation_vector.to_owned();
    let mut slot = PROCESS_CORRELATION_VECTOR
        .lock()
        .unwrap_or_else(|e| e.into_inner());
    if slot.is_none() {
        *slot = Some(owned);
    }
}

/// The stashed lifecycle correlation vector, or `""` (one-shot / not set / lock
/// contended). Returns an owned `String` because the value lives behind a
/// `Mutex` (no `'static` borrow to hand out, unlike the `&'static str` context
/// fields). Uses `try_lock` for the same reentrancy-deadlock reason as
/// [`process_backend`].
fn process_correlation_vector() -> String {
    PROCESS_CORRELATION_VECTOR
        .try_lock()
        .ok()
        .and_then(|slot| slot.clone())
        .unwrap_or_default()
}

/// Pure defaulting for the process phase: the stashed value, or `""` (one-shot /
/// not yet set). Split out (global-free) so the fallback behaviour is
/// unit-testable without writing the set-once [`PROCESS_PHASE`].
fn resolve_phase_name(stored: Option<&'static str>) -> &'static str {
    stored.unwrap_or("")
}

/// Install a crash-telemetry panic hook that emits [`emit_panic`] and then
/// chains the previously-installed hook, so the default stderr backtrace still
/// prints and the "always emit a diagnostic" contract holds for the panic case.
///
/// Shared by the `wxc` (one-shot and state-aware) and `lxc` entry points. Call
/// once, after telemetry is active and [`set_process_context`] (and, for
/// state-aware, [`set_process_phase`]) have run. The hook body is panic-free
/// and emits no message text.
pub fn install_panic_hook() {
    let previous_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| {
        emit_panic();
        previous_hook(info);
    }));
}

/// Build the `Execution` event for an out-of-band crash/cancellation. Pure
/// (no ETW I/O) so the exit-code/reason/attribution mapping can be unit-tested.
#[cfg(test)]
fn crash_event<'a>(
    ctx: TelemetryContext<'a>,
    exit_code: i32,
    reason: FailureReason,
) -> ExecutionEvent<'a> {
    crash_event_with_kind(ctx, ctx.backend, exit_code, reason)
}

fn crash_event_with_kind<'a>(
    ctx: TelemetryContext<'a>,
    sandbox_kind: &'a str,
    exit_code: i32,
    reason: FailureReason,
) -> ExecutionEvent<'a> {
    ExecutionEvent {
        backend: ctx.backend,
        sandbox_kind,
        exit_code,
        outcome: "failure",
        duration_ms: 0,
        failure_reason: Some(reason),
        phase: ctx.phase,
        correlation_vector: ctx.correlation_vector,
    }
}

/// The pair of events an out-of-band crash/cancellation emits: one failure
/// `Execution` and one `Error`, both attributed to the same backend,
/// phase, exit code, and reason.
struct CrashTelemetry<'a> {
    execution: ExecutionEvent<'a>,
    error: FailureReason,
    exit_code: i32,
}

/// Pure outcome→events mapping shared by [`emit_panic`]/[`emit_cancellation`]
/// and their tests. Takes the [`TelemetryContext`] attribution as a parameter
/// (rather than reading the process globals) so the mapping can be asserted
/// deterministically for any attribution without writing the globals.
#[cfg(test)]
fn plan_crash<'a>(
    ctx: TelemetryContext<'a>,
    exit_code: i32,
    reason: FailureReason,
) -> CrashTelemetry<'a> {
    plan_crash_with_kind(ctx, ctx.backend, exit_code, reason)
}

fn plan_crash_with_kind<'a>(
    ctx: TelemetryContext<'a>,
    sandbox_kind: &'a str,
    exit_code: i32,
    reason: FailureReason,
) -> CrashTelemetry<'a> {
    CrashTelemetry {
        execution: crash_event_with_kind(ctx, sandbox_kind, exit_code, reason),
        error: reason,
        exit_code,
    }
}

/// Emit the planned crash/cancellation events. The thin I/O tail shared by
/// [`emit_panic`] and [`emit_cancellation`]: it performs the two ETW writes and
/// deliberately does **not** call [`shutdown`] (see the callers' docs). Takes
/// resolved attribution so the pure [`plan_crash`] mapping stays testable.
///
/// The `_auth` parameter is the single authorization token for this emission
/// (see [`EmissionAuthorization`]). Both writes below execute under it — no
/// per-write reread of consent/policy state.
fn emit_crash(
    _auth: &EmissionAuthorization,
    ctx: TelemetryContext<'_>,
    sandbox_kind: &str,
    exit_code: i32,
    reason: FailureReason,
) {
    let plan = plan_crash_with_kind(ctx, sandbox_kind, exit_code, reason);
    log_execution(&plan.execution);
    log_error(ctx, sandbox_kind, plan.error, plan.exit_code);
}

/// Emit crash telemetry from a global panic hook.
///
/// Guarded by [`crate::mxc_telemetry::is_active`], so it is a cheap no-op when
/// telemetry is disabled or the provider is already shut down. It records a
/// failure `Execution` and an `Error` categorised as
/// [`FailureReason::InternalError`], attributed to the process backend stashed
/// by [`set_process_context`] and the phase stashed by [`set_process_phase`].
///
/// Unlike [`emit_completion`]/[`emit_early_exit`], this deliberately does **not**
/// call [`shutdown`]: it runs while the thread is unwinding (or about to abort),
/// where the OS reclaims the ETW registration at process exit. It also carries
/// **no** panic message text, which can contain paths or other PII.
pub fn emit_panic() {
    let Some(auth) = EmissionAuthorization::for_process() else {
        return;
    };
    if already_emitted() {
        return;
    }
    let correlation_vector = process_correlation_vector();
    emit_crash(
        &auth,
        TelemetryContext {
            backend: process_backend(),
            phase: process_phase(),
            correlation_vector: &correlation_vector,
        },
        process_sandbox_kind(),
        PANIC_EXIT_CODE,
        FailureReason::InternalError,
    );
}

/// Emit cancellation telemetry from a console control (Ctrl-C / close / shutdown)
/// handler.
///
/// Guarded by [`crate::mxc_telemetry::is_active`], so it is a cheap no-op when
/// telemetry is disabled or already shut down. It records a failure
/// `Execution` and an `Error` categorised as [`FailureReason::Cancelled`],
/// attributed to the process backend stashed by [`set_process_context`] and the
/// phase stashed by [`set_process_phase`].
///
/// Like [`emit_panic`], it deliberately does **not** call [`shutdown`]: the
/// handler runs on a short OS-imposed budget just before the default handler
/// tears the process down via `ExitProcess`, and the main thread may still be
/// live. It is allocation-light and emits no free-form text.
pub fn emit_cancellation() {
    let Some(auth) = EmissionAuthorization::for_process() else {
        return;
    };
    if already_emitted() {
        return;
    }
    let correlation_vector = process_correlation_vector();
    emit_crash(
        &auth,
        TelemetryContext {
            backend: process_backend(),
            phase: process_phase(),
            correlation_vector: &correlation_vector,
        },
        process_sandbox_kind(),
        CANCELLED_EXIT_CODE,
        FailureReason::Cancelled,
    );
}

/// Map an [`MxcError`] surfaced by state-aware dispatch to a bounded
/// [`FailureReason`]. Exhaustive over [`MxcErrorCode`] so a newly-added code
/// forces a compile error here rather than silently classifying as `Unknown`.
///
/// Public so the streaming SDK path (`crate::mxc_engine::spawn_execution_request`) can preserve the
/// actual error category on early-exit telemetry rather than reporting every
/// dispatch failure as `InitError`.
pub fn classify_mxc_error(err: &MxcError) -> FailureReason {
    match err.code {
        MxcErrorCode::MalformedRequest | MxcErrorCode::MalformedId => FailureReason::ConfigError,
        MxcErrorCode::PolicyValidation => FailureReason::PolicyError,
        MxcErrorCode::UnsupportedContainment
        | MxcErrorCode::UnsupportedPhase
        | MxcErrorCode::BackendUnavailable => FailureReason::InitError,
        MxcErrorCode::StaleId
        | MxcErrorCode::NotProvisioned
        | MxcErrorCode::NotStarted
        | MxcErrorCode::AlreadyStarted
        | MxcErrorCode::AlreadyStopped
        | MxcErrorCode::BackendError => FailureReason::ProcessError,
    }
}

/// The telemetry a completed state-aware dispatch should emit: one
/// `Execution`, plus an optional `Error` category when the dispatch was
/// an MXC infrastructure failure. Pure (no ETW I/O) so the outcome→event mapping
/// can be unit-tested deterministically without an active provider.
struct StateAwareEvents<'a> {
    execution: ExecutionEvent<'a>,
    error: Option<FailureReason>,
}

/// Pure outcome→events mapping shared by [`emit_state_aware`] and its tests.
/// See [`emit_state_aware`] for the mapping rationale.
#[cfg(test)]
fn plan_state_aware<'a>(
    ctx: TelemetryContext<'a>,
    outcome: &Result<DispatchOutcome, MxcError>,
    duration_ms: u64,
) -> StateAwareEvents<'a> {
    plan_state_aware_with_kind(ctx, outcome, duration_ms, ctx.backend)
}

fn plan_state_aware_with_kind<'a>(
    ctx: TelemetryContext<'a>,
    outcome: &Result<DispatchOutcome, MxcError>,
    duration_ms: u64,
    sandbox_kind: &'a str,
) -> StateAwareEvents<'a> {
    match outcome {
        Ok(DispatchOutcome::Envelope(_)) => StateAwareEvents {
            execution: ExecutionEvent {
                backend: ctx.backend,
                sandbox_kind,
                exit_code: 0,
                outcome: "success",
                duration_ms,
                failure_reason: None,
                phase: ctx.phase,
                correlation_vector: ctx.correlation_vector,
            },
            error: None,
        },
        Ok(DispatchOutcome::ExecCompleted { exit_code }) => {
            let failed = *exit_code != 0;
            StateAwareEvents {
                execution: ExecutionEvent {
                    backend: ctx.backend,
                    sandbox_kind,
                    exit_code: *exit_code,
                    outcome: if failed { "failure" } else { "success" },
                    duration_ms,
                    // The sandbox exit is not an MXC infrastructure error, so
                    // suppress Error while retaining the failure category.
                    failure_reason: failed.then_some(FailureReason::ProcessError),
                    phase: ctx.phase,
                    correlation_vector: ctx.correlation_vector,
                },
                error: None,
            }
        }
        Err(err) => {
            let reason = classify_mxc_error(err);
            StateAwareEvents {
                execution: ExecutionEvent {
                    backend: ctx.backend,
                    sandbox_kind,
                    exit_code: 1,
                    outcome: "failure",
                    duration_ms,
                    failure_reason: Some(reason),
                    phase: ctx.phase,
                    correlation_vector: ctx.correlation_vector,
                },
                error: Some(reason),
            }
        }
    }
}

/// Emit telemetry for one completed state-aware lifecycle dispatch, tagged with
/// the lifecycle `phase` and the `correlation_vector` (both carried in `ctx`), then
/// shut the provider down. No-op when `active` is `false`.
///
/// `ctx.correlation_vector` is the MS-CV span for this phase — every phase of one
/// lifecycle shares a base prefix (seeded at `provision`, spun per phase) so
/// `provision`→…→`deprovision` events (each emitted by a separate `wxc-exec`
/// process) can be joined. Empty for phases with no vector.
///
/// This is the state-aware counterpart to [`emit_completion`]. Outcome mapping:
/// - [`DispatchOutcome::Envelope`] (non-exec phases and exec dry-run) — success,
///   exit code 0.
/// - [`DispatchOutcome::ExecCompleted`] — mirrors one-shot: an `Execution`
///   with the sandbox exit code. A clean non-zero *sandbox* exit is not an MXC
///   failure, so no `Error` is emitted.
/// - `Err(MxcError)` — an `Execution` failure plus an `Error` carrying
///   the [`classify_mxc_error`] category.
///
/// Terminal path (`run_state_aware_main` exits immediately after), so it calls
/// [`shutdown`].
pub fn emit_state_aware(
    active: bool,
    ctx: TelemetryContext<'_>,
    outcome: &Result<DispatchOutcome, MxcError>,
    elapsed: Duration,
) {
    emit_state_aware_with_kind(active, None, ctx, outcome, elapsed);
}

/// Emit state-aware telemetry with the caller-requested containment kind.
pub fn emit_state_aware_with_kind(
    active: bool,
    requested_sandbox_kind: Option<&'static str>,
    ctx: TelemetryContext<'_>,
    outcome: &Result<DispatchOutcome, MxcError>,
    elapsed: Duration,
) {
    let Some(auth) = EmissionAuthorization::for_invocation(active) else {
        return;
    };
    if already_emitted() {
        return;
    }
    emit_state_aware_event(&auth, ctx, outcome, elapsed, requested_sandbox_kind, None);
    shutdown();
}

fn emit_state_aware_event(
    _auth: &EmissionAuthorization,
    ctx: TelemetryContext<'_>,
    outcome: &Result<DispatchOutcome, MxcError>,
    elapsed: Duration,
    requested_sandbox_kind: Option<&str>,
    failure_reason_override: Option<FailureReason>,
) {
    // The `_auth` parameter is the single authorization token for this
    // emission (see `EmissionAuthorization`). Both writes below execute under
    // it — there is no per-write reread of consent/policy state.
    let duration_ms = elapsed.as_millis() as u64;
    let sandbox_kind = sandbox_kind_for(ctx.backend, requested_sandbox_kind);
    let mut plan = plan_state_aware_with_kind(ctx, outcome, duration_ms, sandbox_kind);
    if outcome.is_err() {
        if let Some(reason) = failure_reason_override {
            plan.execution.failure_reason = Some(reason);
            plan.error = Some(reason);
        }
    }

    log_execution(&plan.execution);
    if let Some(reason) = plan.error {
        log_error(ctx, sandbox_kind, reason, plan.execution.exit_code);
    }
}

/// Emit telemetry for an in-process SDK state-aware invocation.
///
/// The SDK wrapper owns exactly-once emission for its request/handle, so this
/// bypasses the executable-wide terminal slot used by `wxc-exec`.
pub fn emit_sdk_state_aware(
    active: bool,
    ctx: TelemetryContext<'_>,
    outcome: &Result<DispatchOutcome, MxcError>,
    elapsed: Duration,
) {
    emit_sdk_state_aware_with_kind(active, None, ctx, outcome, elapsed);
}

/// Emit SDK state-aware telemetry with request-scoped attribution.
pub fn emit_sdk_state_aware_with_kind(
    active: bool,
    requested_sandbox_kind: Option<&'static str>,
    ctx: TelemetryContext<'_>,
    outcome: &Result<DispatchOutcome, MxcError>,
    elapsed: Duration,
) {
    emit_sdk_state_aware_with_kind_and_failure(
        active,
        requested_sandbox_kind,
        ctx,
        outcome,
        elapsed,
        None,
    );
}

/// Emit telemetry for a typed SDK envelope-phase result without constructing a
/// response envelope.
#[doc(hidden)]
pub fn emit_sdk_state_aware_typed(
    active: bool,
    requested_sandbox_kind: Option<&'static str>,
    ctx: TelemetryContext<'_>,
    outcome: &Result<(), MxcError>,
    elapsed: Duration,
) {
    emit_sdk_with_release(active, |_auth| {
        let sandbox_kind = sandbox_kind_for(ctx.backend, requested_sandbox_kind);
        let duration_ms = elapsed.as_millis() as u64;
        let (exit_code, status, failure_reason) = match outcome {
            Ok(()) => (0, "success", None),
            Err(error) => (1, "failure", Some(classify_mxc_error(error))),
        };
        log_execution(&ExecutionEvent {
            backend: ctx.backend,
            sandbox_kind,
            exit_code,
            outcome: status,
            duration_ms,
            failure_reason,
            phase: ctx.phase,
            correlation_vector: ctx.correlation_vector,
        });
        if let Some(reason) = failure_reason {
            log_error(ctx, sandbox_kind, reason, exit_code);
        }
    });
}

/// Emit SDK state-aware telemetry with an optional terminal failure override.
#[doc(hidden)]
pub fn emit_sdk_state_aware_with_kind_and_failure(
    active: bool,
    requested_sandbox_kind: Option<&'static str>,
    ctx: TelemetryContext<'_>,
    outcome: &Result<DispatchOutcome, MxcError>,
    elapsed: Duration,
    failure_reason_override: Option<FailureReason>,
) {
    emit_sdk_with_release(active, |auth| {
        emit_state_aware_event(
            auth,
            ctx,
            outcome,
            elapsed,
            requested_sandbox_kind,
            failure_reason_override,
        )
    });
}

/// Emit a confirmed SDK streaming-handle cancellation and release the
/// invocation's provider reference.
///
/// This is separate from generic completion mapping because an explicit,
/// successful [`SandboxProcess::kill`](crate::mxc_common::sandbox_process::SandboxProcess::kill)
/// is a cancellation, not an initialization or process failure.
pub fn emit_sdk_cancellation_with_kind(
    active: bool,
    requested_sandbox_kind: Option<&'static str>,
    ctx: TelemetryContext<'_>,
    elapsed: Duration,
) {
    emit_sdk_with_release(active, |auth| {
        let _auth = auth;
        let sandbox_kind = sandbox_kind_for(ctx.backend, requested_sandbox_kind);
        log_execution(&ExecutionEvent {
            backend: ctx.backend,
            sandbox_kind,
            exit_code: CANCELLED_EXIT_CODE,
            outcome: "failure",
            duration_ms: elapsed.as_millis() as u64,
            failure_reason: Some(FailureReason::Cancelled),
            phase: ctx.phase,
            correlation_vector: ctx.correlation_vector,
        });
        log_error(
            ctx,
            sandbox_kind,
            FailureReason::Cancelled,
            CANCELLED_EXIT_CODE,
        );
    });
}

#[cfg(any(test, all(feature = "test-support", debug_assertions)))]
pub mod test_support {
    use super::consent::test_support::LocalAppDataGuard;
    use super::policy::test_support::PolicyKeyGuard;

    /// A fully isolated telemetry environment: both the administrative policy
    /// key and the user consent store are redirected to throwaway, per-test
    /// locations.
    ///
    /// This is the **only** supported way to hold both guards at once. They
    /// protect separate process-global mutexes, so acquiring them in
    /// inconsistent orders across tests would deadlock under `cargo test`'s
    /// multithreaded runner. Constructing them here — policy first, then
    /// consent — is what establishes the total order that makes the pair
    /// deadlock-free, and a caller cannot get it wrong because a caller never
    /// sees the individual acquisitions.
    ///
    /// Every test that reaches [`super::is_enabled`],
    /// [`super::consent::needs_consent_prompt`], or [`super::policy::get_policy`]
    /// must hold this — *including* tests that only care about consent.
    /// Otherwise they read the real machine policy and fail on an
    /// administratively managed device.
    pub struct TelemetryTestEnv {
        // Fields drop in declaration order, so consent is released before
        // policy: the exact reverse of the acquisition order below.
        _consent: LocalAppDataGuard,
        policy: PolicyKeyGuard,
    }

    impl TelemetryTestEnv {
        /// Redirects the consent store to `store` and the policy key to a
        /// fresh, empty one (i.e. an unmanaged machine).
        pub fn new(store: &std::path::Path) -> Self {
            let policy = PolicyKeyGuard::new();
            let _consent = LocalAppDataGuard::set(store);
            Self { _consent, policy }
        }

        /// Sets the administrative `AllowTelemetry` policy value.
        #[cfg_attr(not(target_os = "windows"), allow(dead_code))]
        pub fn set_policy_value(&self, value: u32) {
            self.policy.set_value(value);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::test_support::TelemetryTestEnv;
    use super::*;

    /// Serializes tests that touch the process-global emit slot / context
    /// (`HAS_EMITTED`, `PROCESS_BACKEND`, `PROCESS_PHASE`, `PROCESS_CORRELATION_VECTOR`)
    /// or drive the emit paths, so their global state can't leak across tests.
    /// Mirrors the `TEST_LOCK` pattern in `mxc_telemetry`.
    static TEST_LOCK: Mutex<()> = Mutex::new(());

    #[test]
    fn is_enabled_explicit_true_alone_does_not_bypass_consent() {
        // Consent isolated to a fresh, empty store (Undetermined) — an
        // explicit `enabled: true` in the config must not be able to turn
        // telemetry on for someone who has not granted consent.
        let tmp = tempfile::tempdir().unwrap();
        let _env = TelemetryTestEnv::new(tmp.path());
        let config = TelemetryConfig {
            enabled: Some(true),
            ..Default::default()
        };
        assert!(!is_enabled(&config));
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn is_enabled_true_when_consent_granted() {
        let tmp = tempfile::tempdir().unwrap();
        let _env = TelemetryTestEnv::new(tmp.path());
        consent::set_consent(true, "cli").unwrap();
        // An explicit opt-in is required in addition to consent.
        assert!(is_enabled(&TelemetryConfig {
            enabled: Some(true),
            ..Default::default()
        }));
    }

    /// An administrative denial overrides an explicit user grant. This is the
    /// MDM/Intune ceiling: policy can only ever subtract.
    #[cfg(target_os = "windows")]
    #[test]
    fn is_enabled_false_when_policy_blocks_despite_consent() {
        let tmp = tempfile::tempdir().unwrap();
        let env = TelemetryTestEnv::new(tmp.path());
        consent::set_consent(true, "cli").unwrap();
        env.set_policy_value(0);

        assert!(!is_enabled(&TelemetryConfig {
            enabled: Some(true),
            ..Default::default()
        }));
    }

    /// The converse, and the load-bearing privacy invariant: a permissive
    /// administrative policy is *not* consent. An admin who allows telemetry
    /// has not decided on the user's behalf.
    #[cfg(target_os = "windows")]
    #[test]
    fn is_enabled_false_when_policy_allows_but_consent_is_absent() {
        let tmp = tempfile::tempdir().unwrap();
        let env = TelemetryTestEnv::new(tmp.path());
        env.set_policy_value(3);

        assert_eq!(policy::get_policy(), policy::PolicyState::Allowed);
        assert!(!is_enabled(&TelemetryConfig {
            enabled: Some(true),
            ..Default::default()
        }));
    }

    /// Explicit user denial must beat every policy state — including a
    /// permissive one. Completes the consent × policy matrix.
    #[cfg(target_os = "windows")]
    #[test]
    fn is_enabled_false_when_consent_denied_under_every_policy() {
        for policy_value in [None, Some(0u32), Some(1), Some(3)] {
            let tmp = tempfile::tempdir().unwrap();
            let env = TelemetryTestEnv::new(tmp.path());
            consent::set_consent(false, "cli").unwrap();
            if let Some(value) = policy_value {
                env.set_policy_value(value);
            }

            assert!(
                !is_enabled(&TelemetryConfig {
                    enabled: Some(true),
                    ..Default::default()
                }),
                "denied consent must win over explicit enable under policy {policy_value:?}"
            );
        }
    }

    /// The policy is a ceiling, never a grant: no policy value may enable
    /// telemetry for a user who has never recorded a decision. `Denied` is
    /// covered above; this covers the fresh-machine `Undetermined` case, which
    /// is the one a permissive policy could plausibly be mistaken for a grant.
    #[cfg(target_os = "windows")]
    #[test]
    fn is_enabled_false_when_consent_undetermined_under_every_policy() {
        for policy_value in [None, Some(0u32), Some(1), Some(3)] {
            let tmp = tempfile::tempdir().unwrap();
            let env = TelemetryTestEnv::new(tmp.path());
            if let Some(value) = policy_value {
                env.set_policy_value(value);
            }

            assert_eq!(consent::get_consent(), consent::ConsentState::Undetermined);
            assert!(
                !is_enabled(&TelemetryConfig {
                    enabled: Some(true),
                    ..Default::default()
                }),
                "undetermined consent must block an explicit enable under policy {policy_value:?}"
            );
        }
    }

    #[test]
    fn is_enabled_explicit_false() {
        // The kill switch wins even when consent has been granted.
        let tmp = tempfile::tempdir().unwrap();
        let _env = TelemetryTestEnv::new(tmp.path());
        #[cfg(target_os = "windows")]
        consent::set_consent(true, "cli").unwrap();
        let config = TelemetryConfig {
            enabled: Some(false),
            ..Default::default()
        };
        assert!(!is_enabled(&config));
    }

    #[test]
    fn is_enabled_default_off() {
        let tmp = tempfile::tempdir().unwrap();
        let _env = TelemetryTestEnv::new(tmp.path());
        let config = TelemetryConfig::default();
        assert!(!is_enabled(&config));
    }

    /// The load-bearing half of "omitted = off". Without granting consent
    /// first this test would pass for the wrong reason — a fresh store is
    /// `Undetermined`, which disables telemetry on its own — and would keep
    /// passing if omission silently started meaning "defer to consent".
    #[cfg(target_os = "windows")]
    #[test]
    fn is_enabled_false_when_enabled_omitted_despite_consent_and_permissive_policy() {
        let tmp = tempfile::tempdir().unwrap();
        let env = TelemetryTestEnv::new(tmp.path());
        consent::set_consent(true, "cli").unwrap();
        env.set_policy_value(3);

        assert_eq!(consent::get_consent(), consent::ConsentState::Granted);
        assert_eq!(policy::get_policy(), policy::PolicyState::Allowed);
        // Every other gate is open; only the omitted opt-in keeps it off.
        assert!(!is_enabled(&TelemetryConfig {
            enabled: None,
            ..Default::default()
        }));
    }

    #[test]
    fn version_is_not_empty() {
        assert!(!version().is_empty());
    }

    #[test]
    fn emit_panic_noop_without_active_provider() {
        // With telemetry never initialised, the panic hook path must be a
        // silent, panic-free no-op. Serialized: shares the emit-slot / force-
        // active globals with the capture tests below.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        emit_panic();
        assert!(
            events::test_sink::take_executions().is_empty(),
            "inactive provider must not emit"
        );
    }

    #[test]
    fn emit_cancellation_noop_without_active_provider() {
        // With telemetry never initialised, the cancellation path must be a
        // silent, panic-free no-op.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        emit_cancellation();
        assert!(
            events::test_sink::take_executions().is_empty(),
            "inactive provider must not emit"
        );
    }

    #[test]
    fn live_authorization_gate_suppresses_and_then_allows_emission() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_FORCE_ACTIVE.with(|active| active.set(true));
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(false)));
        set_process_context(&ContainmentBackend::IsolationSession);

        emit_panic();
        assert!(events::test_sink::take_executions().is_empty());
        assert!(events::test_sink::take_errors().is_empty());

        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));
        emit_panic();
        assert_eq!(events::test_sink::take_executions().len(), 1);
        assert_eq!(events::test_sink::take_errors().len(), 1);

        reset_for_test();
    }

    #[test]
    fn verbose_chunks_recheck_live_authorization() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_FORCE_ACTIVE.with(|active| active.set(true));
        let event = VerboseEvent {
            backend: "processcontainer",
            sandbox_kind: "process",
            phase: "",
            correlation_vector: "",
            document_id: "0123456789abcdef0123456789abcdef",
            document_version: 2,
            chunk_index: 0,
            chunk_count: 1,
            document_bytes: 64,
            document_sha256: "abc",
            content: "[]",
            summary: r#"{"totalOccurrences":0}"#,
        };

        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(false)));
        assert!(!emit_verbose(true, &event).unwrap());
        assert!(events::test_sink::take_verbose().is_empty());

        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));
        assert!(emit_verbose(true, &event).unwrap());
        assert_eq!(events::test_sink::take_verbose().len(), 1);
        reset_for_test();
    }

    #[test]
    fn emit_panic_active_captures_execution_and_error() {
        // Drive the real emit glue (globals read → active guard → paired write)
        // with the provider forced active and the capture sink installed, then
        // assert the exact Execution + Error records a panic produces.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));
        TEST_FORCE_ACTIVE.with(|f| f.set(true));
        set_process_context_with_kind(&ContainmentBackend::IsolationSession, Some("vm"));
        set_process_phase("exec");
        set_process_correlation_vector("iso:wxc-abcd");

        emit_panic();

        let execs = events::test_sink::take_executions();
        assert_eq!(execs.len(), 1, "panic emits exactly one Execution");
        let exec = &execs[0];
        assert_eq!(exec.backend, "isolation_session");
        assert_eq!(exec.sandbox_kind, "vm");
        assert_eq!(exec.exit_code, PANIC_EXIT_CODE);
        assert_eq!(exec.outcome, "failure");
        assert_eq!(exec.failure_reason, Some(FailureReason::InternalError));
        assert_eq!(exec.phase, "exec");
        assert_eq!(exec.correlation_vector, "iso:wxc-abcd");

        let errors = events::test_sink::take_errors();
        assert_eq!(errors.len(), 1, "panic emits exactly one Error");
        let error = &errors[0];
        assert_eq!(error.backend, "isolation_session");
        assert_eq!(error.sandbox_kind, "vm");
        assert_eq!(error.error_type, FailureReason::InternalError);
        assert_eq!(error.exit_code, PANIC_EXIT_CODE);
        assert_eq!(error.phase, "exec");
        assert_eq!(error.correlation_vector, "iso:wxc-abcd");

        reset_for_test();
    }

    #[test]
    fn emit_cancellation_active_captures_execution_and_error() {
        // Same shape as the panic case, but the cancellation exit code (130)
        // and the `cancelled` category.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));
        TEST_FORCE_ACTIVE.with(|f| f.set(true));
        set_process_context(&ContainmentBackend::IsolationSession);
        set_process_phase("start");
        set_process_correlation_vector("iso:wxc-abcd");

        emit_cancellation();

        let execs = events::test_sink::take_executions();
        assert_eq!(execs.len(), 1);
        let exec = &execs[0];
        assert_eq!(exec.exit_code, CANCELLED_EXIT_CODE);
        assert_eq!(exec.outcome, "failure");
        assert_eq!(exec.failure_reason, Some(FailureReason::Cancelled));
        assert_eq!(exec.phase, "start");
        assert_eq!(exec.correlation_vector, "iso:wxc-abcd");

        let errors = events::test_sink::take_errors();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].error_type, FailureReason::Cancelled);
        assert_eq!(errors[0].exit_code, CANCELLED_EXIT_CODE);

        reset_for_test();
    }

    #[test]
    fn emit_completion_captures_success_and_failure_records() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));

        emit_completion(
            true,
            &ContainmentBackend::IsolationSession,
            &ScriptResponse {
                exit_code: 0,
                ..Default::default()
            },
            Duration::from_millis(12),
        );
        let executions = events::test_sink::take_executions();
        assert_eq!(executions.len(), 1);
        assert_eq!(executions[0].outcome, "success");
        assert_eq!(executions[0].exit_code, 0);
        assert_eq!(executions[0].failure_reason, None);
        assert!(events::test_sink::take_errors().is_empty());

        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));
        emit_completion_with_kind(
            true,
            &ContainmentBackend::IsolationSession,
            Some("process"),
            &ScriptResponse {
                exit_code: 1,
                error_message: "launch failed".to_string(),
                failure_phase: FailurePhase::LaunchFailed,
                ..Default::default()
            },
            Duration::from_millis(3),
        );
        let executions = events::test_sink::take_executions();
        assert_eq!(executions.len(), 1);
        assert_eq!(executions[0].outcome, "failure");
        assert_eq!(executions[0].exit_code, 1);
        assert_eq!(executions[0].sandbox_kind, "process");
        assert_eq!(executions[0].failure_reason, Some(FailureReason::InitError));
        let errors = events::test_sink::take_errors();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].sandbox_kind, "process");
        assert_eq!(errors[0].error_type, FailureReason::InitError);

        reset_for_test();
    }

    #[test]
    fn emit_early_exit_captures_execution_and_error() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));

        emit_early_exit_with_kind(
            true,
            &ContainmentBackend::IsolationSession,
            Some("vm"),
            FailureReason::PolicyError,
        );

        let executions = events::test_sink::take_executions();
        assert_eq!(executions.len(), 1);
        assert_eq!(executions[0].outcome, "failure");
        assert_eq!(executions[0].exit_code, 1);
        assert_eq!(executions[0].sandbox_kind, "vm");
        assert_eq!(
            executions[0].failure_reason,
            Some(FailureReason::PolicyError)
        );
        let errors = events::test_sink::take_errors();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].sandbox_kind, "vm");
        assert_eq!(errors[0].error_type, FailureReason::PolicyError);

        reset_for_test();
    }

    #[test]
    fn second_terminal_emit_is_suppressed_end_to_end() {
        // With the provider active, the first out-of-band emit claims the slot
        // and produces its record pair; a second (racing) emit must be fully
        // suppressed by the exactly-once guard — zero additional records.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_FORCE_ACTIVE.with(|f| f.set(true));
        TEST_AUTHORIZATION_OVERRIDE.with(|value| value.set(Some(true)));
        set_process_context(&ContainmentBackend::IsolationSession);
        set_process_phase("exec");

        emit_panic();
        emit_cancellation();

        assert_eq!(
            events::test_sink::take_executions().len(),
            1,
            "second emit must not add an Execution"
        );
        assert_eq!(
            events::test_sink::take_errors().len(),
            1,
            "second emit must not add an Error"
        );

        reset_for_test();
    }

    #[test]
    fn terminal_emit_slot_is_exactly_once_and_resettable() {
        // The exactly-once slot (`HAS_EMITTED`) is concurrency-critical: the
        // out-of-band panic/cancellation paths race the main completion emit,
        // and the guard is what keeps a single dispatch from producing
        // duplicate Execution records. Lock the global state, reset to a
        // known baseline, and assert claim-once semantics end-to-end.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();

        // First claim owns emission; the second (and any later) claim is told
        // a terminal event already fired, so the caller must skip.
        assert!(!already_emitted(), "first claim should own the slot");
        assert!(already_emitted(), "second claim must be suppressed");
        assert!(already_emitted(), "slot stays claimed until reset");

        // Reset clears the slot so a fresh process (test) starts clean again.
        reset_for_test();
        assert!(!already_emitted(), "reset must release the slot");

        // Leave the slot released for the next test holding the lock.
        reset_for_test();
    }

    #[test]
    fn reset_clears_stashed_process_context() {
        // reset_for_test must clear the stashed backend/phase/correlation-id
        // so one test's context can't bleed into another's out-of-band
        // attribution.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();

        set_process_context(&ContainmentBackend::IsolationSession);
        set_process_phase("provision");
        set_process_correlation_vector("iso:wxc-abcd");
        assert_eq!(process_backend(), "isolation_session");
        assert_eq!(process_phase(), "provision");
        assert_eq!(process_correlation_vector(), "iso:wxc-abcd");

        // Set-once: a second set is ignored while the slot is populated.
        set_process_phase("deprovision");
        assert_eq!(process_phase(), "provision", "phase is set-once");

        reset_for_test();
        assert_eq!(process_backend(), UNKNOWN_BACKEND);
        assert_eq!(process_phase(), "");
        assert_eq!(process_correlation_vector(), "");
    }

    #[test]
    fn emit_state_aware_noop_when_inactive() {
        // Inactive provider — must be a panic-free no-op for every outcome.
        let ok = Ok(DispatchOutcome::ExecCompleted { exit_code: 0 });
        emit_state_aware(
            false,
            TelemetryContext {
                backend: "isolation_session",
                phase: "exec",
                correlation_vector: "iso:wxc-abcd",
            },
            &ok,
            Duration::ZERO,
        );
    }

    #[test]
    fn classify_mxc_error_maps_codes() {
        // Exhaustive over all MxcErrorCode variants so the mapping stays total.
        assert_eq!(
            classify_mxc_error(&MxcError::malformed_request("x")),
            FailureReason::ConfigError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::malformed_id("x")),
            FailureReason::ConfigError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::policy_validation("x")),
            FailureReason::PolicyError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::unsupported_containment("x")),
            FailureReason::InitError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::unsupported_phase("x")),
            FailureReason::InitError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::backend_unavailable("x")),
            FailureReason::InitError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::stale_id("x")),
            FailureReason::ProcessError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::not_provisioned("x")),
            FailureReason::ProcessError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::not_started("x")),
            FailureReason::ProcessError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::already_started("x")),
            FailureReason::ProcessError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::already_stopped("x")),
            FailureReason::ProcessError
        );
        assert_eq!(
            classify_mxc_error(&MxcError::backend_error("x")),
            FailureReason::ProcessError
        );
    }

    #[test]
    fn crash_event_carries_reason_phase_and_exit_code() {
        let event = crash_event(
            TelemetryContext {
                backend: "lxc",
                phase: "exec",
                correlation_vector: "iso:wxc-abcd",
            },
            PANIC_EXIT_CODE,
            FailureReason::InternalError,
        );
        assert_eq!(event.backend, "lxc");
        assert_eq!(event.phase, "exec");
        assert_eq!(event.correlation_vector, "iso:wxc-abcd");
        assert_eq!(event.exit_code, PANIC_EXIT_CODE);
        assert_eq!(event.outcome, "failure");
        assert_eq!(event.failure_reason, Some(FailureReason::InternalError));

        let cancel = crash_event(
            TelemetryContext {
                backend: "appcontainer",
                phase: "",
                correlation_vector: "",
            },
            CANCELLED_EXIT_CODE,
            FailureReason::Cancelled,
        );
        assert_eq!(cancel.phase, "");
        assert_eq!(cancel.correlation_vector, "");
        assert_eq!(cancel.exit_code, CANCELLED_EXIT_CODE);
        assert_eq!(cancel.failure_reason, Some(FailureReason::Cancelled));
    }

    #[test]
    fn plan_crash_maps_panic_and_cancellation_for_any_context() {
        // The pure mapper takes the attribution context as a parameter, so both
        // the panic and cancellation shapes are asserted deterministically across
        // backend/phase combinations without writing the set-once globals.
        let panic = plan_crash(
            TelemetryContext {
                backend: "lxc",
                phase: "exec",
                correlation_vector: "iso:wxc-abcd",
            },
            PANIC_EXIT_CODE,
            FailureReason::InternalError,
        );
        assert_eq!(panic.execution.backend, "lxc");
        assert_eq!(panic.execution.phase, "exec");
        assert_eq!(panic.execution.correlation_vector, "iso:wxc-abcd");
        assert_eq!(panic.execution.outcome, "failure");
        assert_eq!(panic.execution.exit_code, PANIC_EXIT_CODE);
        assert_eq!(
            panic.execution.failure_reason,
            Some(FailureReason::InternalError)
        );
        // The Error carries the same reason/exit code as the execution event.
        assert_eq!(panic.error, FailureReason::InternalError);
        assert_eq!(panic.exit_code, PANIC_EXIT_CODE);

        let cancel = plan_crash(
            TelemetryContext {
                backend: "isolation_session",
                phase: "",
                correlation_vector: "",
            },
            CANCELLED_EXIT_CODE,
            FailureReason::Cancelled,
        );
        assert_eq!(cancel.execution.backend, "isolation_session");
        assert_eq!(cancel.execution.phase, "");
        assert_eq!(cancel.execution.exit_code, CANCELLED_EXIT_CODE);
        assert_eq!(cancel.error, FailureReason::Cancelled);
        assert_eq!(cancel.exit_code, CANCELLED_EXIT_CODE);
    }

    #[test]
    fn resolve_context_names_apply_defaults() {
        // Global-free defaulting: unset falls back, set passes through. Lets the
        // fallback behaviour be tested without the set-once OnceLock globals.
        assert_eq!(resolve_backend_name(None), UNKNOWN_BACKEND);
        assert_eq!(resolve_backend_name(Some("lxc")), "lxc");
        assert_eq!(resolve_phase_name(None), "");
        assert_eq!(resolve_phase_name(Some("provision")), "provision");
    }

    #[test]
    fn plan_state_aware_matrix_over_phases_and_outcomes() {
        // Full {provision, start, exec, stop, deprovision} × {envelope success,
        // zero guest exit, non-zero guest exit, MxcError} matrix. A non-zero
        // guest exit realistically only occurs on `exec`, but plan_state_aware
        // is phase-agnostic, so exercising every phase validates the mxc.phase
        // (and the __TlgCV__ correlation vector) threading as well as the outcome mapping.
        const PHASES: [&str; 5] = ["provision", "start", "exec", "stop", "deprovision"];
        let correlation = "corr-abcd1234";

        for phase in PHASES {
            let ctx = TelemetryContext {
                backend: "isolation_session",
                phase,
                correlation_vector: correlation,
            };

            // Envelope success → success / exit 0 / no error.
            let envelope = Ok(DispatchOutcome::Envelope(serde_json::json!({})));
            let plan = plan_state_aware(ctx, &envelope, 7);
            assert_eq!(plan.execution.phase, phase);
            assert_eq!(plan.execution.correlation_vector, correlation);
            assert_eq!(plan.execution.outcome, "success");
            assert_eq!(plan.execution.exit_code, 0);
            assert_eq!(plan.execution.duration_ms, 7);
            assert!(plan.execution.failure_reason.is_none());
            assert!(plan.error.is_none());

            // Zero guest exit is also a clean success.
            let zero = Ok(DispatchOutcome::ExecCompleted { exit_code: 0 });
            let zero_plan = plan_state_aware(ctx, &zero, 0);
            assert_eq!(zero_plan.execution.phase, phase);
            assert_eq!(zero_plan.execution.correlation_vector, correlation);
            assert_eq!(zero_plan.execution.outcome, "success");
            assert_eq!(zero_plan.execution.exit_code, 0);
            assert!(zero_plan.error.is_none());

            // Non-zero guest exit → failure with the propagated exit code, but
            // NO Error (a faithfully-propagated script exit, not an MXC
            // failure).
            let nonzero = Ok(DispatchOutcome::ExecCompleted { exit_code: 42 });
            let nonzero_plan = plan_state_aware(ctx, &nonzero, 3);
            assert_eq!(nonzero_plan.execution.phase, phase);
            assert_eq!(nonzero_plan.execution.correlation_vector, correlation);
            assert_eq!(nonzero_plan.execution.outcome, "failure");
            assert_eq!(nonzero_plan.execution.exit_code, 42);
            assert_eq!(
                nonzero_plan.execution.failure_reason,
                Some(FailureReason::ProcessError)
            );
            assert!(nonzero_plan.error.is_none());

            // MxcError → failure / exit 1 / classified Error.
            let err = Err(MxcError::backend_unavailable("no host"));
            let err_plan = plan_state_aware(ctx, &err, 5);
            assert_eq!(err_plan.execution.phase, phase);
            assert_eq!(err_plan.execution.correlation_vector, correlation);
            assert_eq!(err_plan.execution.outcome, "failure");
            assert_eq!(err_plan.execution.exit_code, 1);
            assert_eq!(
                err_plan.execution.failure_reason,
                Some(FailureReason::InitError)
            );
            assert_eq!(err_plan.error, Some(FailureReason::InitError));
        }
    }

    #[test]
    fn set_process_context_records_backend() {
        // Touches the process-global set-once PROCESS_BACKEND, so serialize on
        // TEST_LOCK and reset to a clean baseline first.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        set_process_context(&ContainmentBackend::Lxc);
        assert_eq!(process_backend(), "lxc");
        reset_for_test();
    }

    #[test]
    fn classify_failure_maps_all_phases() {
        // Backend/launch failures classify as init errors.
        assert_eq!(
            classify_failure(&FailurePhase::LaunchFailed),
            FailureReason::InitError
        );
        assert_eq!(
            classify_failure(&FailurePhase::BackendUnavailable),
            FailureReason::InitError
        );
        // A rejected request is a policy error; a post-launch infra failure
        // is an init error.
        assert_eq!(
            classify_failure(&FailurePhase::Rejected),
            FailureReason::PolicyError
        );
        assert_eq!(
            classify_failure(&FailurePhase::PostLaunchFailed),
            FailureReason::InitError
        );
        // A process that ran and exited (or an unclassified failure) is a
        // process error.
        assert_eq!(
            classify_failure(&FailurePhase::ProcessExited),
            FailureReason::ProcessError
        );
        assert_eq!(
            classify_failure(&FailurePhase::None),
            FailureReason::ProcessError
        );
    }

    #[test]
    fn emit_state_aware_production_path_captures_records() {
        // Exercises the real `emit_state_aware` glue (not just the pure
        // `plan_state_aware` mapper): active guard → exactly-once slot → paired
        // ETW writes → shutdown. Asserts the captured records carry the threaded
        // phase + correlation vector for both a success envelope and an MxcError.
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));

        // Provision-style success envelope → one Execution, no Error.
        let envelope = Ok(DispatchOutcome::Envelope(serde_json::json!({})));
        emit_state_aware(
            true,
            TelemetryContext {
                backend: "isolation_session",
                phase: "provision",
                correlation_vector: "corr-provision",
            },
            &envelope,
            Duration::from_millis(4),
        );
        let execs = events::test_sink::take_executions();
        assert_eq!(execs.len(), 1);
        assert_eq!(execs[0].phase, "provision");
        assert_eq!(execs[0].correlation_vector, "corr-provision");
        assert_eq!(execs[0].outcome, "success");
        assert!(events::test_sink::take_errors().is_empty());

        // Fresh slot for the error case (the emit above claimed it once).
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));
        let err = Err(MxcError::policy_validation("bad policy"));
        emit_state_aware(
            true,
            TelemetryContext {
                backend: "isolation_session",
                phase: "start",
                correlation_vector: "corr-start",
            },
            &err,
            Duration::from_millis(2),
        );
        let execs = events::test_sink::take_executions();
        assert_eq!(execs.len(), 1);
        assert_eq!(execs[0].phase, "start");
        assert_eq!(execs[0].correlation_vector, "corr-start");
        assert_eq!(execs[0].outcome, "failure");
        let errors = events::test_sink::take_errors();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].error_type, FailureReason::PolicyError);
        assert_eq!(errors[0].phase, "start");
        assert_eq!(errors[0].correlation_vector, "corr-start");

        reset_for_test();
    }

    #[test]
    fn sdk_state_aware_events_keep_request_scoped_sandbox_kind() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));

        let outcome = Ok(DispatchOutcome::Envelope(serde_json::json!({})));
        let error = Err(MxcError::policy_validation("simulated"));
        emit_sdk_state_aware_with_kind(
            true,
            Some("process"),
            TelemetryContext {
                backend: "windows_sandbox",
                phase: "exec",
                correlation_vector: "corr-process",
            },
            &outcome,
            Duration::ZERO,
        );
        emit_sdk_state_aware_with_kind(
            true,
            Some("vm"),
            TelemetryContext {
                backend: "windows_sandbox",
                phase: "exec",
                correlation_vector: "corr-vm",
            },
            &error,
            Duration::ZERO,
        );

        let executions = events::test_sink::take_executions();
        assert_eq!(executions.len(), 2);
        assert_eq!(executions[0].sandbox_kind, "process");
        assert_eq!(executions[1].sandbox_kind, "vm");
        assert_eq!(executions[0].correlation_vector, "corr-process");
        assert_eq!(executions[1].correlation_vector, "corr-vm");
        let errors = events::test_sink::take_errors();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].sandbox_kind, "vm");
        assert_eq!(errors[0].backend, "windows_sandbox");

        reset_for_test();
    }

    #[test]
    fn typed_sdk_state_aware_events_do_not_require_dispatch_envelopes() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));

        emit_sdk_state_aware_typed(
            true,
            Some("isolation_session"),
            TelemetryContext {
                backend: "isolation_session",
                phase: "provision",
                correlation_vector: "typed-success",
            },
            &Ok(()),
            Duration::from_millis(3),
        );
        emit_sdk_state_aware_typed(
            true,
            Some("isolation_session"),
            TelemetryContext {
                backend: "isolation_session",
                phase: "start",
                correlation_vector: "typed-error",
            },
            &Err(MxcError::policy_validation("simulated")),
            Duration::from_millis(4),
        );

        let executions = events::test_sink::take_executions();
        assert_eq!(executions.len(), 2);
        assert_eq!(executions[0].outcome, "success");
        assert_eq!(executions[0].correlation_vector, "typed-success");
        assert_eq!(executions[1].outcome, "failure");
        assert_eq!(executions[1].correlation_vector, "typed-error");
        let errors = events::test_sink::take_errors();
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].error_type, FailureReason::PolicyError);

        reset_for_test();
    }

    #[test]
    fn sdk_cancellation_uses_cancelled_reason_and_request_context() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));

        emit_sdk_cancellation_with_kind(
            true,
            Some("process"),
            TelemetryContext {
                backend: "appcontainer",
                phase: "",
                correlation_vector: "",
            },
            Duration::from_millis(9),
        );
        emit_sdk_cancellation_with_kind(
            true,
            Some("vm"),
            TelemetryContext {
                backend: "windows_sandbox",
                phase: "exec",
                correlation_vector: "corr-cancelled",
            },
            Duration::from_millis(17),
        );

        let executions = events::test_sink::take_executions();
        assert_eq!(executions.len(), 2);
        assert_eq!(executions[0].backend, "appcontainer");
        assert_eq!(executions[0].sandbox_kind, "process");
        assert_eq!(executions[0].duration_ms, 9);
        assert_eq!(executions[0].phase, "");
        assert_eq!(executions[0].correlation_vector, "");
        assert_eq!(executions[1].backend, "windows_sandbox");
        assert_eq!(executions[1].sandbox_kind, "vm");
        assert_eq!(executions[1].exit_code, CANCELLED_EXIT_CODE);
        assert_eq!(executions[1].outcome, "failure");
        assert_eq!(executions[1].duration_ms, 17);
        assert_eq!(executions[1].failure_reason, Some(FailureReason::Cancelled));
        assert_eq!(executions[1].phase, "exec");
        assert_eq!(executions[1].correlation_vector, "corr-cancelled");

        let errors = events::test_sink::take_errors();
        assert_eq!(errors.len(), 2);
        assert_eq!(errors[0].sandbox_kind, "process");
        assert_eq!(errors[0].error_type, FailureReason::Cancelled);
        assert_eq!(errors[0].exit_code, CANCELLED_EXIT_CODE);
        assert_eq!(errors[0].phase, "");
        assert_eq!(errors[0].correlation_vector, "");
        assert_eq!(errors[1].sandbox_kind, "vm");
        assert_eq!(errors[1].error_type, FailureReason::Cancelled);
        assert_eq!(errors[1].exit_code, CANCELLED_EXIT_CODE);
        assert_eq!(errors[1].phase, "exec");
        assert_eq!(errors[1].correlation_vector, "corr-cancelled");

        reset_for_test();
    }

    #[cfg(windows)]
    #[test]
    fn sdk_emit_releases_provider_when_live_authorization_closes() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(false)));

        assert!(crate::mxc_telemetry::init(version(), MXC_CHANNEL));
        assert!(crate::mxc_telemetry::is_active());

        emit_sdk_with_release(true, |_auth| {
            panic!("authorization must suppress the event")
        });

        assert!(!crate::mxc_telemetry::is_active());
        reset_for_test();
    }

    /// Regression: the paired-write authorization decision must be captured
    /// *once* per logical emission and shared by both writes. If authorization
    /// flips from allowed → denied *after* the token is minted but *before*
    /// the second write in a pair, the second write must still fire (they
    /// share the one decision proven by the `EmissionAuthorization` token).
    /// This structurally forbids a revocation-race window between the paired
    /// `log_execution` and `log_error` calls, without re-introducing blocking
    /// consent/policy I/O on the hot path.
    #[test]
    fn paired_write_shares_single_authorization_decision() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_FORCE_ACTIVE.with(|f| f.set(true));
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));

        // Precondition: authorization initially allows — the token can be
        // minted.
        assert!(EmissionAuthorization::for_invocation(true).is_some());

        // Drive the paired-write path (Execution + Error) on the state-aware
        // emit, which is representative of the completion / early-exit /
        // crash pairs (all share the same token-threading structure).
        let err = Err(MxcError::policy_validation("simulated"));
        emit_state_aware(
            true,
            TelemetryContext {
                backend: "isolation_session",
                phase: "exec",
                correlation_vector: "corr-race",
            },
            &err,
            Duration::from_millis(1),
        );

        // Revoke authorization after the emission. Prior to the token
        // refactor this override could have been read a second time between
        // the paired writes; under the token, both writes were already
        // committed under the up-front decision.
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(false)));
        assert!(EmissionAuthorization::for_invocation(true).is_none());

        let execs = events::test_sink::take_executions();
        let errors = events::test_sink::take_errors();
        assert_eq!(execs.len(), 1, "Execution must be emitted under the token");
        assert_eq!(
            errors.len(),
            1,
            "Error must be emitted under the same token as its paired Execution"
        );
        assert_eq!(execs[0].outcome, "failure");
        assert_eq!(errors[0].error_type, FailureReason::PolicyError);

        reset_for_test();
    }

    /// Regression: a denied initial authorization must skip the pair entirely
    /// — neither `log_execution` nor `log_error` may fire when the token
    /// cannot be minted.
    #[test]
    fn paired_write_denies_pair_when_token_cannot_be_minted() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_FORCE_ACTIVE.with(|f| f.set(true));
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(false)));

        assert!(EmissionAuthorization::for_invocation(true).is_none());

        let err = Err(MxcError::policy_validation("simulated"));
        emit_state_aware(
            true,
            TelemetryContext {
                backend: "isolation_session",
                phase: "exec",
                correlation_vector: "corr-denied",
            },
            &err,
            Duration::from_millis(1),
        );

        assert!(events::test_sink::take_executions().is_empty());
        assert!(events::test_sink::take_errors().is_empty());

        reset_for_test();
    }

    // Validates that the emit guard honors the *real* `mxc_telemetry` provider —
    // not just the `TEST_FORCE_ACTIVE` override — by registering the provider for
    // real (only possible on Windows) and asserting `emit_panic` captures without
    // any forced-active flag set.
    #[cfg(windows)]
    #[test]
    fn emit_honors_real_provider_activation() {
        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_AUTHORIZATION_OVERRIDE.with(|allowed| allowed.set(Some(true)));

        // Register the real ETW provider; on Windows this makes is_active() true.
        assert!(
            crate::mxc_telemetry::init(version(), MXC_CHANNEL),
            "provider registration should succeed on Windows"
        );
        assert!(crate::mxc_telemetry::is_active());
        // Deliberately do NOT set TEST_FORCE_ACTIVE — the emit must proceed off
        // the real provider state alone.
        set_process_context(&ContainmentBackend::IsolationSession);
        set_process_phase("exec");

        emit_panic();

        assert_eq!(
            events::test_sink::take_executions().len(),
            1,
            "emit must fire off the real active provider without TEST_FORCE_ACTIVE"
        );

        crate::mxc_telemetry::shutdown();
        reset_for_test();
    }

    #[cfg(windows)]
    #[test]
    fn live_withdrawal_and_policy_block_suppress_the_next_emission() {
        let store = tempfile::tempdir().expect("temp dir");
        let env = test_support::TelemetryTestEnv::new(store.path());
        env.set_policy_value(3);
        consent::set_consent(true, "test").expect("set consent");

        let _lock = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        reset_for_test();
        events::test_sink::install();
        TEST_FORCE_ACTIVE.with(|f| f.set(true));
        set_process_context(&ContainmentBackend::IsolationSession);

        consent::withdraw_consent().expect("withdraw consent");
        emit_panic();
        assert!(
            events::test_sink::take_executions().is_empty(),
            "withdrawal must suppress the next logical emission"
        );

        reset_for_test();
        events::test_sink::install();
        TEST_FORCE_ACTIVE.with(|f| f.set(true));
        consent::set_consent(true, "test").expect("restore consent");
        env.set_policy_value(0);

        emit_cancellation();
        assert!(
            events::test_sink::take_executions().is_empty(),
            "a newly blocking policy must suppress the next logical emission"
        );

        reset_for_test();
    }
}