vsh-runtime 0.5.0

Native Rust SDK for the VSH validation-first execution engine
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
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard};
use std::time::Instant;

use vsh_commit::{
    CommitConfig, CommitError, CommitPlan, CommitPlanError, CommitReceipt, Committer,
    RecoveryReport, SnapshotLimits,
};
use vsh_monty::{
    ExecutionError, ExecutionLimits, ExecutionOutcome, ExecutionStats, InProcessConfig,
    InProcessMonty, MontyObject, ResultCompatibility, ResultCompatibilityError, SubprocessConfig,
    SubprocessMonty, VirtualRoot, validate_result_compatibility,
};
use vsh_policy::{
    AccessKind, DeniedAccess, DenyManifest, PolicyDecision, PolicyInput, PolicyProfile,
    RiskManifest, RiskMetrics, TransactionIdentityInput, TransactionPolicy, bind_transaction,
};
use vsh_store::{
    ApprovalGrant, ApprovalGrantError, BlobStore, BlobStoreError, DataDirectory,
    DataDirectoryError, FileStoreConfig, FileTransactionStore, TransactionRecord, TransactionStore,
    TransactionStoreError,
};
use vsh_types::{
    DiffDigest, DiffEntry, RuntimeConfigDigest, SnapshotId, TransactionId, TransactionState,
};
use vsh_vfs::{CanonicalDiff, VfsError, VirtualFs};

use crate::artifact::{
    ArtifactError, PendingTransaction, ReviewEvidence, decode_pending, encode_pending,
};
use crate::hook::{
    CommitPreparation, CommitResolution, HookBaseline, HookConfig, HookDecision,
    HookDecisionRecord, HookHandlerError, HookVerdict, RequestEvent,
};

/// Request-scoped resource caps enforced by the Monty/VFS adapter.
pub type ExecutionBudget = ExecutionLimits;

/// Hard allocation and cardinality bounds for durable approval artifacts.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ArtifactLimits {
    /// Maximum complete encoded artifact bytes.
    pub max_bytes: usize,
    /// Maximum postcard-encoded Monty result bytes.
    pub max_value_bytes: usize,
    /// Maximum retained UTF-8 stdout bytes.
    pub max_stdout_bytes: usize,
    /// Maximum canonical changed paths.
    pub max_entries: usize,
    /// Maximum read or write dependency entries.
    pub max_dependencies: usize,
    /// Maximum one-path UTF-8 byte length.
    pub max_path_bytes: usize,
    /// Maximum retained out-of-band intent bytes exposed to a hook.
    pub max_intent_bytes: usize,
    /// Maximum ordered operation-level effects exposed to a hook.
    pub max_effects: usize,
    /// Maximum process-local auto-approved previews retained by one runtime.
    pub max_ephemeral_entries: usize,
    /// Maximum encoded bytes retained by process-local auto-approved previews.
    pub max_ephemeral_bytes: usize,
}

impl Default for ArtifactLimits {
    fn default() -> Self {
        Self {
            max_bytes: 128 * 1024 * 1024,
            max_value_bytes: 16 * 1024 * 1024,
            max_stdout_bytes: 16 * 1024 * 1024,
            max_entries: 100_000,
            max_dependencies: 250_000,
            max_path_bytes: 16 * 1024,
            max_intent_bytes: 64 * 1024,
            max_effects: 250_000,
            max_ephemeral_entries: 64,
            max_ephemeral_bytes: 128 * 1024 * 1024,
        }
    }
}

/// Whether one call stops after policy or commits deterministic auto-approvals.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum RunMode {
    /// Produce an approval-bound virtual result without changing the host workspace.
    #[default]
    Preview,
    /// Commit only when deterministic policy returns `AutoApprove`.
    Auto,
}

/// Amount of canonical change detail retained in a receipt.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ReceiptDetail {
    /// Retain bounded counts and digests, but no per-path diff entries.
    #[default]
    Compact,
    /// Retain the complete bounded canonical diff.
    Full,
}

/// One borrowed native execution request.
#[derive(Clone, Copy, Debug)]
pub struct RunRequest<'a> {
    /// Exact Monty source executed against virtual state.
    pub code: &'a str,
    /// Optional out-of-band intent bound into transaction identity.
    pub intent: Option<&'a str>,
    /// Preview-only or deterministic auto-commit behavior.
    pub mode: RunMode,
    /// Compact or complete canonical change detail.
    pub detail: ReceiptDetail,
    /// Independent execution caps for this request.
    pub budget: ExecutionBudget,
}

impl<'a> RunRequest<'a> {
    /// Construct a safe preview request with default resource caps.
    #[must_use]
    pub fn new(code: &'a str) -> Self {
        Self {
            code,
            intent: None,
            mode: RunMode::Preview,
            detail: ReceiptDetail::Compact,
            budget: ExecutionBudget::default(),
        }
    }

    /// Bind an out-of-band intent to this request.
    #[must_use]
    pub const fn with_intent(mut self, intent: &'a str) -> Self {
        self.intent = Some(intent);
        self
    }

    /// Select preview or deterministic auto-commit behavior.
    #[must_use]
    pub const fn with_mode(mut self, mode: RunMode) -> Self {
        self.mode = mode;
        self
    }

    /// Select compact or complete receipt detail.
    #[must_use]
    pub const fn with_detail(mut self, detail: ReceiptDetail) -> Self {
        self.detail = detail;
        self
    }

    /// Replace all request-scoped execution caps.
    #[must_use]
    pub const fn with_budget(mut self, budget: ExecutionBudget) -> Self {
        self.budget = budget;
        self
    }
}

/// Deterministic policy result retained in the native receipt.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RuntimeDecision {
    /// Deterministic policy rejected the transaction.
    Denied(DenyManifest),
    /// Deterministic policy authorized reservation without a judge.
    AutoApproved,
    /// An exact independent approval is required before reservation.
    PendingApproval(RiskManifest),
}

/// Monotonic stage costs recorded without string allocation in the hot path.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct StageTimings {
    /// Capability-rooted metadata snapshot time.
    pub snapshot_ns: u64,
    /// Monty execution plus typed VFS-call time.
    pub execute_ns: u64,
    /// Canonical-diff freeze time.
    pub diff_ns: u64,
    /// Deterministic-policy evaluation time.
    pub policy_ns: u64,
    /// Artifact binding and short state-store transitions.
    pub bind_and_store_ns: u64,
    /// Reservation, dependency revalidation, commit, and verification time.
    pub commit_ns: u64,
    /// Complete native call time.
    pub total_ns: u64,
}

/// Compact proof of virtual execution, policy, and optional verified commit.
#[derive(Clone, Debug)]
pub struct Receipt {
    /// Approval- and commit-bound transaction identity.
    pub transaction: TransactionId,
    /// Immutable base snapshot identity.
    pub base_snapshot: SnapshotId,
    /// Current lifecycle state. Auto-approved previews may be process-local until commit.
    pub state: TransactionState,
    /// Deterministic policy result.
    pub decision: RuntimeDecision,
    /// Canonical diff identity.
    pub diff: DiffDigest,
    /// Number of canonical changed paths.
    pub changed_paths: usize,
    /// Complete canonical entries only when full detail was requested.
    pub changes: Vec<DiffEntry>,
    /// Bounded Monty return value.
    pub value: MontyObject,
    /// Bounded captured `print()` output.
    pub stdout: String,
    /// Independent execution counters.
    pub execution: ExecutionStats,
    /// Native stage timings.
    pub timings: StageTimings,
    /// Durable commit proof when the host was changed and verified.
    pub commit: Option<CommitReceipt>,
}

/// Immutable runtime configuration shared by Rust and `PyO3` callers.
#[derive(Clone, Debug)]
pub struct RuntimeConfig {
    workspace_root: PathBuf,
    data_directory: PathBuf,
    data_directory_authority: DataDirectoryAuthority,
    worker_path: Option<PathBuf>,
    max_idle_workers: usize,
    result_compatibility: ResultCompatibility,
    virtual_root: VirtualRoot,
    policy: TransactionPolicy,
    snapshot_limits: SnapshotLimits,
    commit_config: CommitConfig,
    store_config: FileStoreConfig,
    artifact_limits: ArtifactLimits,
    commit_hook: Option<HookConfig>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum DataDirectoryAuthority {
    WorkspaceProtected,
    TrustedExternal,
}

impl RuntimeConfig {
    /// Construct a balanced runtime rooted at `workspace_root`.
    ///
    /// Durable internal artifacts default to `.vsh-runtime/data` below the workspace;
    /// that namespace is excluded from snapshots and denied to Monty.
    #[must_use]
    pub fn new(workspace_root: impl Into<PathBuf>) -> Self {
        let workspace_root = workspace_root.into();
        let data_directory = workspace_root.join(".vsh-runtime").join("data");
        Self {
            workspace_root,
            data_directory,
            data_directory_authority: DataDirectoryAuthority::WorkspaceProtected,
            worker_path: Some(default_worker_path()),
            max_idle_workers: 4,
            result_compatibility: ResultCompatibility::Native,
            virtual_root: VirtualRoot::default(),
            policy: TransactionPolicy::default(),
            snapshot_limits: SnapshotLimits::default(),
            commit_config: CommitConfig::default(),
            store_config: FileStoreConfig::default(),
            artifact_limits: ArtifactLimits::default(),
            commit_hook: None,
        }
    }

    /// Place immutable blobs in an explicit trusted data directory.
    #[must_use]
    pub fn with_data_directory(mut self, data_directory: impl Into<PathBuf>) -> Self {
        self.data_directory = data_directory.into();
        self.data_directory_authority = DataDirectoryAuthority::TrustedExternal;
        self
    }

    /// Select the exact supervised Monty worker executable used for hostile code.
    #[must_use]
    pub fn with_worker_path(mut self, worker_path: impl Into<PathBuf>) -> Self {
        self.worker_path = Some(worker_path.into());
        self
    }

    /// Bound clean workers retained for low-latency reuse. Zero disables pooling.
    #[must_use]
    pub const fn with_max_idle_workers(mut self, max_idle_workers: usize) -> Self {
        self.max_idle_workers = max_idle_workers;
        self
    }

    /// Require every result to be faithfully representable by one host surface.
    #[must_use]
    pub const fn with_result_compatibility(
        mut self,
        result_compatibility: ResultCompatibility,
    ) -> Self {
        self.result_compatibility = result_compatibility;
        self
    }

    /// Disable crash isolation for trusted embedding and deterministic test harnesses.
    ///
    /// This mode must never execute hostile or unreviewed code. Production Rust and
    /// Python callers use the supervised worker by default.
    #[must_use]
    pub fn with_in_process_execution(mut self) -> Self {
        self.worker_path = None;
        self
    }

    /// Replace the synthetic absolute namespace exposed to Monty.
    #[must_use]
    pub fn with_virtual_root(mut self, virtual_root: VirtualRoot) -> Self {
        self.virtual_root = virtual_root;
        self
    }

    /// Replace deterministic transaction and pre-call policy.
    #[must_use]
    pub fn with_policy(mut self, policy: TransactionPolicy) -> Self {
        self.policy = policy;
        self
    }

    /// Select a built-in deterministic policy profile.
    #[must_use]
    pub fn with_policy_profile(self, profile: PolicyProfile) -> Self {
        self.with_policy(TransactionPolicy::preset(profile))
    }

    /// Replace eager snapshot traversal bounds.
    #[must_use]
    pub const fn with_snapshot_limits(mut self, limits: SnapshotLimits) -> Self {
        self.snapshot_limits = limits;
        self
    }

    /// Replace trusted commit and recovery bounds.
    #[must_use]
    pub const fn with_commit_config(mut self, config: CommitConfig) -> Self {
        self.commit_config = config;
        self
    }

    /// Replace durable transaction-log bounds.
    #[must_use]
    pub const fn with_store_config(mut self, config: FileStoreConfig) -> Self {
        self.store_config = config;
        self
    }

    /// Replace durable pending-artifact allocation and cardinality bounds.
    #[must_use]
    pub const fn with_artifact_limits(mut self, limits: ArtifactLimits) -> Self {
        self.artifact_limits = limits;
        self
    }

    /// Require the native two-phase hook protocol for matching commit candidates.
    #[must_use]
    pub const fn with_commit_hook(mut self, hook: HookConfig) -> Self {
        self.commit_hook = Some(hook);
        self
    }

    /// Return the host workspace authority root.
    #[must_use]
    pub fn workspace_root(&self) -> &Path {
        &self.workspace_root
    }

    /// Return the trusted immutable-artifact directory.
    #[must_use]
    pub fn data_directory(&self) -> &Path {
        &self.data_directory
    }

    /// Return the supervised worker path, or `None` for explicit trusted in-process mode.
    #[must_use]
    pub fn worker_path(&self) -> Option<&Path> {
        self.worker_path.as_deref()
    }

    /// Return deterministic transaction policy.
    #[must_use]
    pub const fn policy(&self) -> &TransactionPolicy {
        &self.policy
    }

    /// Return the commit-hook configuration, when direct commits are guarded.
    #[must_use]
    pub const fn commit_hook(&self) -> Option<HookConfig> {
        self.commit_hook
    }
}

fn default_worker_path() -> PathBuf {
    std::env::var_os("VSH_MONTY_WORKER")
        .filter(|path| !path.is_empty())
        .map_or_else(|| PathBuf::from("vsh-monty-worker"), PathBuf::from)
}

enum RuntimeExecution {
    Subprocess(Box<SubprocessMonty>),
    InProcess,
}

impl RuntimeExecution {
    fn open(config: &RuntimeConfig) -> Result<Self, ExecutionError> {
        let Some(worker_path) = &config.worker_path else {
            return Ok(Self::InProcess);
        };
        let adapter = InProcessConfig::new(config.virtual_root.clone())
            .with_call_policy(config.policy.call_policy().clone());
        let worker = SubprocessMonty::new(
            SubprocessConfig::new(worker_path, adapter)
                .with_max_idle_workers(config.max_idle_workers),
        )?;
        Ok(Self::Subprocess(Box::new(worker)))
    }

    fn security_digest(&self, adapter: &InProcessConfig) -> RuntimeConfigDigest {
        match self {
            Self::Subprocess(worker) => worker.config().security_digest_for(adapter),
            Self::InProcess => adapter.security_digest(),
        }
    }

    fn execute(
        &self,
        code: &str,
        filesystem: &mut VirtualFs,
        adapter: &InProcessConfig,
    ) -> Result<ExecutionOutcome, ExecutionError> {
        match self {
            Self::Subprocess(worker) => worker.execute_with_config(code, filesystem, adapter),
            Self::InProcess => InProcessMonty::new(adapter.clone()).execute(code, filesystem),
        }
    }
}

/// One native VSH engine instance with no process-global execution lock.
pub struct Runtime {
    config: RuntimeConfig,
    execution: RuntimeExecution,
    committer: Committer,
    store: FileTransactionStore,
    artifacts: BlobStore,
    pending: Mutex<PendingArtifacts>,
    startup_recovery: RecoveryReport,
}

#[derive(Default)]
struct PendingArtifacts {
    entries: BTreeMap<TransactionId, (PendingTransaction, usize)>,
    encoded_bytes: usize,
}

struct EvaluatedDiff {
    diff: CanonicalDiff,
    decision: PolicyDecision,
    metrics: RiskMetrics,
    diff_ns: u64,
    policy_ns: u64,
}

impl Runtime {
    /// Open one capability-rooted runtime and recover durable interrupted commits.
    ///
    /// # Errors
    ///
    /// Returns an error when blob storage, workspace capability setup, recovery, or
    /// fail-closed recovery conflict handling fails.
    pub fn open(config: RuntimeConfig) -> Result<Self, VshError> {
        let (committer, data_directory) = match config.data_directory_authority {
            DataDirectoryAuthority::WorkspaceProtected => {
                Committer::open_with_workspace_data(&config.workspace_root, config.commit_config)?
            }
            DataDirectoryAuthority::TrustedExternal => {
                validate_disjoint_data_directory(&config.workspace_root, &config.data_directory)?;
                let data_directory = DataDirectory::open_trusted(&config.data_directory)?;
                validate_canonical_data_directory_separation(
                    &config.workspace_root,
                    data_directory.path(),
                )?;
                let artifacts = BlobStore::open_in(&data_directory)?;
                let committer =
                    Committer::open(&config.workspace_root, artifacts, config.commit_config)?;
                (committer, data_directory)
            }
        };
        let artifacts = committer.artifact_store();
        let store = FileTransactionStore::open_in(&data_directory, config.store_config)?;
        let execution = RuntimeExecution::open(&config)?;
        let startup_recovery = committer.recover(&store)?;
        if !startup_recovery.conflicts.is_empty() {
            return Err(VshError::RecoveryConflicts(Box::new(startup_recovery)));
        }
        Ok(Self {
            config,
            execution,
            committer,
            store,
            artifacts,
            pending: Mutex::new(PendingArtifacts::default()),
            startup_recovery,
        })
    }

    /// Return the startup recovery work completed before accepting requests.
    #[must_use]
    pub const fn startup_recovery(&self) -> &RecoveryReport {
        &self.startup_recovery
    }

    /// Execute, evaluate, and optionally auto-commit one exact transaction.
    ///
    /// # Errors
    ///
    /// Returns a typed error for snapshot, execution, diff, state, binding, reservation,
    /// revalidation, commit, or recovery failures. Deterministic policy denial is a
    /// successful receipt and never reaches the committer.
    pub fn run(&self, request: RunRequest<'_>) -> Result<Receipt, VshError> {
        validate_program_size(request.code, request.budget)?;
        let total_started = Instant::now();
        let (mut filesystem, base_snapshot, base_node_count, snapshot_ns) =
            self.snapshot_filesystem()?;

        let monty_config = self.monty_config(request.budget);
        let runtime_config = self.runtime_config_digest(&monty_config);
        let execute_started = Instant::now();
        let ExecutionOutcome {
            value,
            stdout,
            stats,
            denied_accesses,
        } = self
            .execution
            .execute(request.code, &mut filesystem, &monty_config)?;
        validate_result_compatibility(&value, self.config.result_compatibility)?;
        let execute_ns = elapsed_ns(execute_started);

        let EvaluatedDiff {
            diff,
            decision: policy_decision,
            metrics: risk_metrics,
            diff_ns,
            policy_ns,
        } = self.evaluate_diff(&filesystem, &denied_accesses, base_node_count)?;

        let bind_started = Instant::now();
        let binding = bind_transaction(TransactionIdentityInput {
            base_snapshot,
            diff: &diff,
            read_set: filesystem.read_set(),
            write_set: filesystem.write_set(),
            program: request.code,
            policy: &self.config.policy,
            runtime_config,
            intent: request.intent,
        });
        let transaction = binding.transaction_id();
        let (decision, state, record) =
            Self::policy_record(transaction, base_snapshot, policy_decision)?;
        let changed_paths = diff.entries().len();
        let changes = receipt_changes(request.detail, &diff);
        let mut receipt = Receipt {
            transaction,
            base_snapshot,
            state,
            decision,
            diff: diff.digest(),
            changed_paths,
            changes,
            value,
            stdout,
            execution: stats,
            timings: StageTimings {
                snapshot_ns,
                execute_ns,
                diff_ns,
                policy_ns,
                bind_and_store_ns: elapsed_ns(bind_started),
                commit_ns: 0,
                total_ns: elapsed_ns(total_started),
            },
            commit: None,
        };

        if state == TransactionState::Denied {
            self.store.create(record)?;
            receipt.timings.bind_and_store_ns = elapsed_ns(bind_started);
            receipt.timings.total_ns = elapsed_ns(total_started);
        } else {
            receipt = self.store_pending(
                record,
                PendingTransaction {
                    binding,
                    diff,
                    read_set: filesystem.read_set().clone(),
                    write_set: filesystem.write_set().clone(),
                    review: ReviewEvidence {
                        intent: request.intent.map(str::to_owned),
                        metrics: risk_metrics,
                        effects: filesystem.effects().to_vec(),
                        complete: true,
                        truncated: false,
                    },
                    receipt,
                },
                request.mode,
                bind_started,
                total_started,
            )?;
        }

        if request.mode == RunMode::Auto && state == TransactionState::AutoApproved {
            receipt = self.commit(transaction, 0)?;
            receipt.timings.total_ns = elapsed_ns(total_started);
        }
        Ok(receipt)
    }

    /// Force preview-only behavior regardless of the request's mode field.
    ///
    /// # Errors
    ///
    /// Returns the same typed failures as [`Self::run`].
    pub fn preview(&self, mut request: RunRequest<'_>) -> Result<Receipt, VshError> {
        request.mode = RunMode::Preview;
        self.run(request)
    }

    /// Forget one process-local auto-approved preview without mutating the host.
    ///
    /// Durable approval-required artifacts are never removed by this method. `false`
    /// means this runtime did not retain the supplied preview.
    ///
    /// # Errors
    ///
    /// Returns an error only when the bounded pending-artifact lock was poisoned.
    pub fn discard_preview(&self, transaction: TransactionId) -> Result<bool, VshError> {
        self.remove_pending(transaction)
            .map(|artifact| artifact.is_some())
    }

    /// Bind an independent, expiring approval to one exact pending transaction.
    ///
    /// # Errors
    ///
    /// Returns an error for an invalid time window, missing transaction, mismatched
    /// binding, wrong state, or internal artifact-state mismatch.
    pub fn approve(
        &self,
        transaction: TransactionId,
        principal: vsh_types::PrincipalId,
        issued_at_unix_ms: u64,
        expires_at_unix_ms: u64,
    ) -> Result<TransactionRecord, VshError> {
        self.load_pending(transaction)?;
        let grant = ApprovalGrant::new(
            transaction,
            principal,
            issued_at_unix_ms,
            expires_at_unix_ms,
        )?;
        let record = self.store.approve(transaction, grant)?;
        if let Some((artifact, _)) = self.pending()?.entries.get_mut(&transaction) {
            artifact.receipt.state = TransactionState::Approved;
        }
        Ok(record)
    }

    /// Consume the single-use reservation and commit one previewed transaction.
    ///
    /// # Errors
    ///
    /// Returns an error for missing artifacts, expired approval, replay, stale host
    /// dependencies, commit/recovery failures, or internal binding mismatch.
    pub fn commit(
        &self,
        transaction: TransactionId,
        now_unix_ms: u64,
    ) -> Result<Receipt, VshError> {
        if self.config.commit_hook.is_some() {
            let preparation = self.prepare_commit(transaction)?;
            if let Some(event) = preparation.event() {
                return Err(VshError::HookRequired(Box::new(event.clone())));
            }
        }
        self.commit_exact(transaction, now_unix_ms)
    }

    /// Freeze one exact commit candidate before invoking an external handler.
    ///
    /// No handler code runs in this method. Process-local auto-approved previews are
    /// made durable before an event is returned, so a crash can regenerate the same
    /// event from the exact transaction artifact.
    ///
    /// # Errors
    ///
    /// Returns a typed store, artifact, configuration, or evidence error.
    pub fn prepare_commit(
        &self,
        transaction: TransactionId,
    ) -> Result<CommitPreparation, VshError> {
        let artifact = self.load_pending(transaction)?;
        validate_result_compatibility(&artifact.receipt.value, self.config.result_compatibility)?;
        self.persist_ephemeral(&artifact)?;
        let record = self.store.get(transaction)?;
        let state = record.state();
        let Some(hook) = self.config.commit_hook else {
            return Ok(CommitPreparation::Ready { transaction, state });
        };
        if !hook.scope().applies_to(state) {
            return Ok(CommitPreparation::Ready { transaction, state });
        }
        Ok(CommitPreparation::Review(Box::new(
            self.request_event(&artifact, hook, state)?,
        )))
    }

    /// Apply one hook decision to the exact prepared transaction.
    ///
    /// The preparation is revalidated against durable state and regenerated evidence,
    /// so callers cannot substitute a transaction or event after the handler returns.
    ///
    /// # Errors
    ///
    /// Returns a typed event-binding, state, approval, store, or commit error. The
    /// exact preparation is checked again before any decision changes host files.
    pub fn resolve_commit(
        &self,
        preparation: &CommitPreparation,
        decision: &HookDecision,
        now_unix_ms: u64,
    ) -> Result<CommitResolution, VshError> {
        let transaction = preparation.transaction();
        let prepared_state = preparation.prepared_state();
        let artifact = self.load_pending(transaction)?;
        let event = self.validate_hook_preparation(preparation, decision, &artifact)?;

        let (verdict, reason) = match &decision {
            HookDecision::FollowPolicy => (HookVerdict::FollowPolicy, ""),
            HookDecision::Approve { reason } => (HookVerdict::Approve, reason.as_str()),
            HookDecision::Review { feedback } => (HookVerdict::Review, feedback.as_str()),
            HookDecision::Reject { reason } => (HookVerdict::Reject, reason.as_str()),
        };
        if let Some((_, hook)) = event
            && reason.len() > hook.max_reason_bytes()
        {
            return Err(VshError::HookReasonLimit {
                observed: reason.len(),
                maximum: hook.max_reason_bytes(),
            });
        }
        let receipt = self.apply_hook_decision(
            transaction,
            prepared_state,
            &artifact,
            event,
            decision,
            now_unix_ms,
        )?;

        let hook_record = event.map(|(event, hook)| HookDecisionRecord {
            event_id: event.event_id,
            hook_id: event.hook_id,
            verdict,
            reason: reason.to_owned(),
            principal: (verdict == HookVerdict::Approve).then(|| hook.principal()),
        });
        Ok(CommitResolution {
            receipt,
            hook: hook_record,
        })
    }

    fn validate_hook_preparation<'a>(
        &self,
        preparation: &'a CommitPreparation,
        decision: &HookDecision,
        artifact: &PendingTransaction,
    ) -> Result<Option<(&'a RequestEvent, HookConfig)>, VshError> {
        let transaction = preparation.transaction();
        let prepared_state = preparation.prepared_state();
        let actual = self.store.get(transaction)?.state();
        if actual != prepared_state {
            return Err(VshError::HookStateChanged {
                transaction,
                prepared: prepared_state,
                actual,
            });
        }
        match preparation {
            CommitPreparation::Ready { .. } => {
                if let Some(hook) = self.config.commit_hook
                    && hook.scope().applies_to(prepared_state)
                {
                    return Err(VshError::HookRequired(Box::new(self.request_event(
                        artifact,
                        hook,
                        prepared_state,
                    )?)));
                }
                if *decision != HookDecision::FollowPolicy {
                    return Err(VshError::UnexpectedHookDecision { transaction });
                }
                Ok(None)
            }
            CommitPreparation::Review(event) => {
                let hook = self
                    .config
                    .commit_hook
                    .ok_or(VshError::HookConfigurationChanged { transaction })?;
                let expected = self.request_event(artifact, hook, prepared_state)?;
                if expected != **event {
                    return Err(VshError::HookEventMismatch { transaction });
                }
                Ok(Some((event.as_ref(), hook)))
            }
        }
    }

    fn apply_hook_decision(
        &self,
        transaction: TransactionId,
        prepared_state: TransactionState,
        artifact: &PendingTransaction,
        event: Option<(&RequestEvent, HookConfig)>,
        decision: &HookDecision,
        now_unix_ms: u64,
    ) -> Result<Receipt, VshError> {
        match decision {
            HookDecision::FollowPolicy => match prepared_state {
                TransactionState::AutoApproved | TransactionState::Approved => {
                    self.commit_exact(transaction, now_unix_ms)
                }
                TransactionState::PendingApproval => Ok(artifact.receipt.clone()),
                actual => Err(VshError::HookNotActionable {
                    transaction,
                    actual,
                }),
            },
            HookDecision::Approve { .. } => {
                let (_, hook) = event.ok_or(VshError::UnexpectedHookDecision { transaction })?;
                if !artifact.review.complete || artifact.review.truncated {
                    return Err(VshError::IncompleteHookEvidence { transaction });
                }
                if prepared_state == TransactionState::PendingApproval {
                    let expires_at_unix_ms = now_unix_ms
                        .checked_add(hook.approval_ttl_ms())
                        .ok_or(VshError::HookApprovalWindow { transaction })?;
                    self.approve(
                        transaction,
                        hook.principal(),
                        now_unix_ms,
                        expires_at_unix_ms,
                    )?;
                } else if prepared_state != TransactionState::AutoApproved {
                    return Err(VshError::HookNotActionable {
                        transaction,
                        actual: prepared_state,
                    });
                }
                self.commit_exact(transaction, now_unix_ms)
            }
            HookDecision::Review { .. } => {
                if prepared_state == TransactionState::AutoApproved {
                    self.store.compare_and_transition(
                        transaction,
                        TransactionState::AutoApproved,
                        TransactionState::PendingApproval,
                    )?;
                    self.update_pending_state(transaction, TransactionState::PendingApproval)?;
                } else if prepared_state != TransactionState::PendingApproval {
                    return Err(VshError::HookNotActionable {
                        transaction,
                        actual: prepared_state,
                    });
                }
                self.receipt_in_state(transaction, TransactionState::PendingApproval)
            }
            HookDecision::Reject { .. } => {
                if !matches!(
                    prepared_state,
                    TransactionState::AutoApproved | TransactionState::PendingApproval
                ) {
                    return Err(VshError::HookNotActionable {
                        transaction,
                        actual: prepared_state,
                    });
                }
                self.store.compare_and_transition(
                    transaction,
                    prepared_state,
                    TransactionState::Rejected,
                )?;
                self.update_pending_state(transaction, TransactionState::Rejected)?;
                let receipt = self.receipt_in_state(transaction, TransactionState::Rejected)?;
                self.remove_pending(transaction)?;
                Ok(receipt)
            }
        }
    }

    /// Apply fail-closed state after a handler exception, timeout, or cancellation.
    ///
    /// # Errors
    ///
    /// Returns a typed store or artifact error if automatic approval cannot be moved
    /// into the existing pending-approval state.
    pub fn fail_hook(&self, preparation: &CommitPreparation) -> Result<(), VshError> {
        let transaction = preparation.transaction();
        if preparation.prepared_state() == TransactionState::AutoApproved {
            self.store.compare_and_transition(
                transaction,
                TransactionState::AutoApproved,
                TransactionState::PendingApproval,
            )?;
            self.update_pending_state(transaction, TransactionState::PendingApproval)?;
        }
        Ok(())
    }

    fn commit_exact(
        &self,
        transaction: TransactionId,
        now_unix_ms: u64,
    ) -> Result<Receipt, VshError> {
        let artifact = self.load_pending(transaction)?;
        validate_result_compatibility(&artifact.receipt.value, self.config.result_compatibility)?;
        self.persist_ephemeral(&artifact)?;
        let plan = CommitPlan::new(
            &artifact.binding,
            &artifact.diff,
            &artifact.read_set,
            &artifact.write_set,
        )?;
        let reservation = self.store.reserve(transaction, now_unix_ms)?;
        let commit_started = Instant::now();
        let commit = self.committer.commit(&self.store, reservation, &plan);
        let commit_ns = elapsed_ns(commit_started);
        self.remove_pending(transaction)?;
        let commit = commit?;
        let mut receipt = artifact.receipt;
        receipt.state = TransactionState::Committed;
        receipt.timings.commit_ns = commit_ns;
        receipt.timings.total_ns = receipt.timings.total_ns.saturating_add(commit_ns);
        receipt.commit = Some(commit);
        Ok(receipt)
    }

    /// Recover all durable commit artifacts under this runtime's capability root.
    ///
    /// # Errors
    ///
    /// Returns a typed commit/recovery error for corrupt or unsafe journals.
    pub fn recover(&self) -> Result<RecoveryReport, VshError> {
        self.committer.recover(&self.store).map_err(Into::into)
    }

    /// Return one persisted lifecycle record.
    ///
    /// # Errors
    ///
    /// Returns [`VshError::Store`] when the transaction does not exist.
    pub fn transaction(&self, transaction: TransactionId) -> Result<TransactionRecord, VshError> {
        match self.store.get(transaction) {
            Ok(record) => Ok(record),
            Err(TransactionStoreError::NotFound { id }) if id == transaction => {
                let artifact = self
                    .pending()?
                    .entries
                    .get(&transaction)
                    .map(|(artifact, _)| artifact.clone())
                    .ok_or(TransactionStoreError::NotFound { id })?;
                Self::ephemeral_record(&artifact)
            }
            Err(source) => Err(source.into()),
        }
    }

    fn request_event(
        &self,
        artifact: &PendingTransaction,
        hook: HookConfig,
        state: TransactionState,
    ) -> Result<RequestEvent, VshError> {
        let transaction = artifact.binding.transaction_id();
        if artifact.binding.policy != self.config.policy.digest() {
            return Err(VshError::HookConfigurationChanged { transaction });
        }
        let (baseline, risk_flags) = match &artifact.receipt.decision {
            RuntimeDecision::AutoApproved => (HookBaseline::AutoApproved, Vec::new()),
            RuntimeDecision::PendingApproval(manifest) => {
                (HookBaseline::ReviewRequired, manifest.flags.clone())
            }
            RuntimeDecision::Denied(_) => {
                return Err(VshError::HookNotActionable {
                    transaction,
                    actual: TransactionState::Denied,
                });
            }
        };
        let (contents, content_complete) = crate::review::collect_content(
            artifact.diff.entries(),
            &artifact.review.effects,
            self.config.policy.call_policy(),
            &self.artifacts,
            hook.max_content_bytes(),
        )?;
        Ok(RequestEvent {
            schema_version: 1,
            event_id: vsh_types::RequestEventId::derive(transaction, hook.id(), hook.scope().tag()),
            hook_id: hook.id(),
            transaction,
            state,
            baseline,
            base_snapshot: artifact.binding.base_snapshot,
            diff: artifact.binding.diff,
            read_set: artifact.binding.read_set,
            write_set: artifact.binding.write_set,
            program: artifact.binding.program,
            policy: artifact.binding.policy,
            runtime_config: artifact.binding.runtime_config,
            intent_digest: artifact.binding.intent,
            intent: artifact.review.intent.clone(),
            policy_profile: self.config.policy.profile(),
            policy_thresholds: self.config.policy.thresholds(),
            risk_metrics: artifact.review.metrics,
            risk_flags,
            canonical_diff: artifact.diff.entries().to_vec(),
            effects: artifact.review.effects.clone(),
            execution: artifact.receipt.execution,
            evidence_complete: artifact.review.complete,
            evidence_truncated: artifact.review.truncated,
            contents,
            content_complete,
        })
    }

    fn evaluate_diff(
        &self,
        filesystem: &VirtualFs,
        denied_accesses: &[DeniedAccess],
        base_node_count: usize,
    ) -> Result<EvaluatedDiff, VshError> {
        let started = Instant::now();
        let mut diff = filesystem.canonical_diff()?;
        let mut diff_ns = elapsed_ns(started);
        let evaluate = |diff: &CanonicalDiff| {
            self.config.policy.evaluate_with_metrics(PolicyInput {
                diff,
                effects: filesystem.effects(),
                denied_accesses,
                base_node_count,
            })
        };
        let started = Instant::now();
        let (mut decision, mut metrics) = evaluate(&diff);
        let mut policy_ns = elapsed_ns(started);
        let state = match &decision {
            PolicyDecision::Deny(_) => TransactionState::Denied,
            PolicyDecision::AutoApprove => TransactionState::AutoApproved,
            PolicyDecision::Escalate(_) => TransactionState::PendingApproval,
        };
        if let Some(hook) = self.config.commit_hook
            && hook.max_content_bytes() > 0
            && hook.scope().applies_to(state)
            && !diff.entries().is_empty()
        {
            let started = Instant::now();
            let paths = diff.entries().iter().filter_map(|entry| {
                (entry.before.is_some()
                    && self
                        .config
                        .policy
                        .call_policy()
                        .authorize(&entry.path, AccessKind::ContentRead)
                        .is_ok())
                .then_some(&entry.path)
            });
            filesystem.capture_before_content(paths, hook.max_content_bytes())?;
            diff = filesystem.canonical_diff()?;
            diff_ns = diff_ns.saturating_add(elapsed_ns(started));
            let started = Instant::now();
            (decision, metrics) = evaluate(&diff);
            policy_ns = policy_ns.saturating_add(elapsed_ns(started));
        }
        Ok(EvaluatedDiff {
            diff,
            decision,
            metrics,
            diff_ns,
            policy_ns,
        })
    }

    fn update_pending_state(
        &self,
        transaction: TransactionId,
        state: TransactionState,
    ) -> Result<(), VshError> {
        if let Some((artifact, _)) = self.pending()?.entries.get_mut(&transaction) {
            artifact.receipt.state = state;
        }
        Ok(())
    }

    fn receipt_in_state(
        &self,
        transaction: TransactionId,
        state: TransactionState,
    ) -> Result<Receipt, VshError> {
        let mut artifact = self.load_pending(transaction)?;
        artifact.receipt.state = state;
        Ok(artifact.receipt)
    }

    fn monty_config(&self, budget: ExecutionBudget) -> InProcessConfig {
        InProcessConfig::new(self.config.virtual_root.clone())
            .with_limits(budget)
            .with_call_policy(self.config.policy.call_policy().clone())
    }

    fn runtime_config_digest(&self, monty_config: &InProcessConfig) -> RuntimeConfigDigest {
        aggregate_runtime_digest(
            self.execution.security_digest(monty_config),
            self.config.snapshot_limits,
            self.config.commit_config,
            self.config.store_config,
            self.config.artifact_limits,
            self.config.result_compatibility,
            self.config.commit_hook,
        )
    }

    fn snapshot_filesystem(&self) -> Result<(VirtualFs, SnapshotId, usize, u64), VshError> {
        let started = Instant::now();
        let snapshot = self.committer.snapshot(self.config.snapshot_limits)?;
        let id = snapshot.id();
        let nodes = snapshot.len();
        Ok((VirtualFs::new(snapshot), id, nodes, elapsed_ns(started)))
    }

    fn insert_pending(
        &self,
        artifact: PendingTransaction,
        encoded_bytes: usize,
    ) -> Result<(), VshError> {
        let transaction = artifact.binding.transaction_id();
        let mut pending = self.pending()?;
        let entries = pending.entries.len();
        let retained_bytes = pending.encoded_bytes;
        let attempted_bytes = retained_bytes.saturating_add(encoded_bytes);
        if entries >= self.config.artifact_limits.max_ephemeral_entries
            || attempted_bytes > self.config.artifact_limits.max_ephemeral_bytes
        {
            return Err(VshError::EphemeralCapacity {
                entries,
                max_entries: self.config.artifact_limits.max_ephemeral_entries,
                attempted_bytes,
                max_bytes: self.config.artifact_limits.max_ephemeral_bytes,
            });
        }
        if pending.entries.contains_key(&transaction) {
            return Err(VshError::DuplicatePending { transaction });
        }
        pending
            .entries
            .insert(transaction, (artifact, encoded_bytes));
        pending.encoded_bytes = attempted_bytes;
        Ok(())
    }

    fn remove_pending(
        &self,
        transaction: TransactionId,
    ) -> Result<Option<PendingTransaction>, VshError> {
        let mut pending = self.pending()?;
        let Some((artifact, encoded_bytes)) = pending.entries.remove(&transaction) else {
            return Ok(None);
        };
        pending.encoded_bytes = pending.encoded_bytes.saturating_sub(encoded_bytes);
        Ok(Some(artifact))
    }

    fn persist_pending(
        &self,
        record: TransactionRecord,
        mut artifact: PendingTransaction,
        bind_started: Instant,
        total_started: Instant,
    ) -> Result<Receipt, VshError> {
        let encoded = encode_pending(&artifact, self.config.artifact_limits)?;
        let artifact_id = self.artifacts.put(&encoded)?;
        self.store.create(record.with_artifact(artifact_id))?;
        artifact.receipt.timings.bind_and_store_ns = elapsed_ns(bind_started);
        artifact.receipt.timings.total_ns = elapsed_ns(total_started);
        let receipt = artifact.receipt.clone();
        Ok(receipt)
    }

    fn store_pending(
        &self,
        record: TransactionRecord,
        artifact: PendingTransaction,
        mode: RunMode,
        bind_started: Instant,
        total_started: Instant,
    ) -> Result<Receipt, VshError> {
        if mode == RunMode::Preview && artifact.receipt.state == TransactionState::AutoApproved {
            self.retain_ephemeral(artifact, bind_started, total_started)
        } else {
            self.persist_pending(record, artifact, bind_started, total_started)
        }
    }

    fn retain_ephemeral(
        &self,
        mut artifact: PendingTransaction,
        bind_started: Instant,
        total_started: Instant,
    ) -> Result<Receipt, VshError> {
        let encoded = encode_pending(&artifact, self.config.artifact_limits)?;
        artifact.receipt.timings.bind_and_store_ns = elapsed_ns(bind_started);
        artifact.receipt.timings.total_ns = elapsed_ns(total_started);
        let receipt = artifact.receipt.clone();
        self.insert_pending(artifact, encoded.len())?;
        Ok(receipt)
    }

    fn persist_ephemeral(&self, artifact: &PendingTransaction) -> Result<(), VshError> {
        let transaction = artifact.binding.transaction_id();
        match self.store.get(transaction) {
            Ok(_) => return Ok(()),
            Err(TransactionStoreError::NotFound { id }) if id == transaction => {}
            Err(source) => return Err(source.into()),
        }
        let encoded = encode_pending(artifact, self.config.artifact_limits)?;
        let artifact_id = self.artifacts.put(&encoded)?;
        let record = Self::ephemeral_record(artifact)?.with_artifact(artifact_id);
        self.store.create(record)?;
        Ok(())
    }

    fn ephemeral_record(artifact: &PendingTransaction) -> Result<TransactionRecord, VshError> {
        if artifact.receipt.state != TransactionState::AutoApproved
            || !matches!(&artifact.receipt.decision, RuntimeDecision::AutoApproved)
        {
            return Err(VshError::MissingPending {
                transaction: artifact.binding.transaction_id(),
            });
        }
        let mut record = TransactionRecord::new(
            artifact.binding.transaction_id(),
            artifact.binding.base_snapshot,
        );
        for state in [
            TransactionState::Running,
            TransactionState::VirtualComplete,
            TransactionState::AutoApproved,
        ] {
            record
                .transition(state)
                .map_err(TransactionStoreError::Transition)?;
        }
        Ok(record)
    }

    fn load_pending(&self, transaction: TransactionId) -> Result<PendingTransaction, VshError> {
        if let Some(artifact) = self
            .pending()?
            .entries
            .get(&transaction)
            .map(|(artifact, _)| artifact.clone())
        {
            return Ok(artifact);
        }
        let record = self.store.get(transaction)?;
        let artifact_id = record
            .artifact()
            .ok_or(VshError::MissingPending { transaction })?;
        let bytes = self
            .artifacts
            .get_bounded(artifact_id, self.config.artifact_limits.max_bytes)?;
        let mut artifact = decode_pending(&bytes, self.config.artifact_limits)?;
        let actual = artifact.binding.transaction_id();
        if actual != transaction || artifact.binding.base_snapshot != record.base_snapshot() {
            return Err(VshError::ArtifactBinding {
                requested: transaction,
                decoded: actual,
            });
        }
        artifact.receipt.state = record.state();
        Ok(artifact)
    }

    fn pending(&self) -> Result<MutexGuard<'_, PendingArtifacts>, VshError> {
        self.pending.lock().map_err(|_| VshError::PendingPoisoned)
    }

    fn policy_record(
        transaction: TransactionId,
        base_snapshot: SnapshotId,
        decision: PolicyDecision,
    ) -> Result<(RuntimeDecision, TransactionState, TransactionRecord), VshError> {
        let mut record = TransactionRecord::new(transaction, base_snapshot);
        record
            .transition(TransactionState::Running)
            .map_err(TransactionStoreError::Transition)?;
        record
            .transition(TransactionState::VirtualComplete)
            .map_err(TransactionStoreError::Transition)?;
        let (decision, state) = match decision {
            PolicyDecision::Deny(manifest) => {
                record
                    .transition(TransactionState::Denied)
                    .map_err(TransactionStoreError::Transition)?;
                (RuntimeDecision::Denied(manifest), TransactionState::Denied)
            }
            PolicyDecision::AutoApprove => {
                record
                    .transition(TransactionState::AutoApproved)
                    .map_err(TransactionStoreError::Transition)?;
                (
                    RuntimeDecision::AutoApproved,
                    TransactionState::AutoApproved,
                )
            }
            PolicyDecision::Escalate(manifest) => {
                record
                    .transition(TransactionState::PendingApproval)
                    .map_err(TransactionStoreError::Transition)?;
                (
                    RuntimeDecision::PendingApproval(manifest),
                    TransactionState::PendingApproval,
                )
            }
        };
        Ok((decision, state, record))
    }
}

fn aggregate_runtime_digest(
    monty: RuntimeConfigDigest,
    snapshot: SnapshotLimits,
    commit: CommitConfig,
    store: FileStoreConfig,
    artifact: ArtifactLimits,
    result_compatibility: ResultCompatibility,
    commit_hook: Option<HookConfig>,
) -> RuntimeConfigDigest {
    let mut canonical = Vec::with_capacity(66 + 8 * 23);
    canonical.extend_from_slice(b"vsh-runtime-config-v6");
    canonical.extend_from_slice(monty.as_bytes());
    encode_usize(snapshot.max_nodes, &mut canonical);
    encode_usize(snapshot.max_depth, &mut canonical);
    canonical.extend_from_slice(&snapshot.max_total_file_bytes.to_le_bytes());
    encode_usize(commit.max_operations, &mut canonical);
    encode_usize(commit.max_dependencies, &mut canonical);
    encode_usize(commit.max_path_bytes, &mut canonical);
    encode_usize(commit.max_plan_bytes, &mut canonical);
    encode_usize(commit.max_journal_bytes, &mut canonical);
    encode_usize(commit.max_conflicts, &mut canonical);
    canonical.extend_from_slice(&store.max_log_bytes.to_le_bytes());
    encode_usize(store.max_records, &mut canonical);
    encode_usize(artifact.max_bytes, &mut canonical);
    encode_usize(artifact.max_value_bytes, &mut canonical);
    encode_usize(artifact.max_stdout_bytes, &mut canonical);
    encode_usize(artifact.max_entries, &mut canonical);
    encode_usize(artifact.max_dependencies, &mut canonical);
    encode_usize(artifact.max_path_bytes, &mut canonical);
    encode_usize(artifact.max_intent_bytes, &mut canonical);
    encode_usize(artifact.max_effects, &mut canonical);
    encode_usize(artifact.max_ephemeral_entries, &mut canonical);
    encode_usize(artifact.max_ephemeral_bytes, &mut canonical);
    canonical.push(match result_compatibility {
        ResultCompatibility::Native => 0,
        ResultCompatibility::Python => 1,
    });
    match commit_hook {
        None => canonical.push(0),
        Some(hook) => {
            canonical.push(1);
            canonical.extend_from_slice(hook.id().as_bytes());
            canonical.push(hook.scope().tag());
            canonical.extend_from_slice(&hook.approval_ttl_ms().to_le_bytes());
            encode_usize(hook.max_reason_bytes(), &mut canonical);
            encode_usize(hook.max_content_bytes(), &mut canonical);
        }
    }
    RuntimeConfigDigest::digest_canonical(&canonical)
}

fn encode_usize(value: usize, output: &mut Vec<u8>) {
    output.extend_from_slice(&u64::try_from(value).unwrap_or(u64::MAX).to_le_bytes());
}

fn receipt_changes(detail: ReceiptDetail, diff: &CanonicalDiff) -> Vec<DiffEntry> {
    if detail == ReceiptDetail::Full {
        diff.entries().to_vec()
    } else {
        Vec::new()
    }
}

fn elapsed_ns(started: Instant) -> u64 {
    u64::try_from(started.elapsed().as_nanos()).unwrap_or(u64::MAX)
}

fn validate_program_size(code: &str, budget: ExecutionBudget) -> Result<(), ExecutionError> {
    let attempted = u64::try_from(code.len()).unwrap_or(u64::MAX);
    let limit = u64::try_from(budget.max_program_bytes).unwrap_or(u64::MAX);
    if attempted > limit {
        Err(ExecutionError::Limit(Box::new(
            vsh_monty::ExecutionLimitExceeded::ProgramBytes { limit, attempted },
        )))
    } else {
        Ok(())
    }
}

fn validate_disjoint_data_directory(
    workspace_root: &Path,
    data_directory: &Path,
) -> Result<(), VshError> {
    let workspace = lexical_absolute(workspace_root);
    let data = lexical_absolute(data_directory);
    let canonical_workspace = std::fs::canonicalize(workspace_root).ok();
    let prospective_data = canonicalize_prospective_path(data_directory);
    if workspace
        .as_deref()
        .zip(data.as_deref())
        .is_none_or(|(workspace, data)| paths_overlap(workspace, data))
        || canonical_workspace
            .as_deref()
            .zip(prospective_data.as_deref())
            .is_none_or(|(workspace, data)| paths_overlap(workspace, data))
    {
        return Err(VshError::UnsafeDataDirectory {
            workspace_root: workspace_root.to_path_buf(),
            data_directory: data_directory.to_path_buf(),
        });
    }
    Ok(())
}

fn validate_canonical_data_directory_separation(
    workspace_root: &Path,
    data_directory: &Path,
) -> Result<(), VshError> {
    let Ok(workspace) = std::fs::canonicalize(workspace_root) else {
        return Err(VshError::UnsafeDataDirectory {
            workspace_root: workspace_root.to_path_buf(),
            data_directory: data_directory.to_path_buf(),
        });
    };
    let Ok(data) = std::fs::canonicalize(data_directory) else {
        return Err(VshError::UnsafeDataDirectory {
            workspace_root: workspace_root.to_path_buf(),
            data_directory: data_directory.to_path_buf(),
        });
    };
    if paths_overlap(&workspace, &data) {
        return Err(VshError::UnsafeDataDirectory {
            workspace_root: workspace_root.to_path_buf(),
            data_directory: data_directory.to_path_buf(),
        });
    }
    Ok(())
}

fn canonicalize_prospective_path(path: &Path) -> Option<PathBuf> {
    let absolute = lexical_absolute(path)?;
    let mut existing = absolute.as_path();
    let mut missing = Vec::new();
    loop {
        match std::fs::canonicalize(existing) {
            Ok(mut canonical) => {
                for component in missing.iter().rev() {
                    canonical.push(component);
                }
                return Some(canonical);
            }
            Err(source) if source.kind() == std::io::ErrorKind::NotFound => {
                missing.push(existing.file_name()?.to_owned());
                existing = existing.parent()?;
            }
            Err(_) => return None,
        }
    }
}

fn lexical_absolute(path: &Path) -> Option<PathBuf> {
    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir().ok()?.join(path)
    };
    let mut normalized = PathBuf::new();
    for component in absolute.components() {
        match component {
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                if !normalized.pop() {
                    return None;
                }
            }
            _ => normalized.push(component.as_os_str()),
        }
    }
    Some(normalized)
}

fn paths_overlap(left: &Path, right: &Path) -> bool {
    left.starts_with(right) || right.starts_with(left)
}

/// Stable native error surface shared with the Python exception mapper.
#[derive(Debug)]
#[non_exhaustive]
pub enum VshError {
    /// The durable data-directory capability could not be established safely.
    DataDirectory(DataDirectoryError),
    /// Immutable blob storage failed.
    Blob(BlobStoreError),
    /// Capability-rooted commit or recovery failed.
    Commit(CommitError),
    /// Monty compilation, execution, or a hard execution budget failed.
    Execution(ExecutionError),
    /// Virtual filesystem integrity or canonical diff generation failed.
    Vfs(VfsError),
    /// Atomic transaction-state operation failed.
    Store(TransactionStoreError),
    /// An approval grant had an invalid time window.
    Approval(ApprovalGrantError),
    /// The internal commit artifact did not match its binding.
    CommitPlan(CommitPlanError),
    /// Durable pending-artifact encoding or validation failed.
    Artifact(ArtifactError),
    /// The selected SDK surface cannot faithfully project the Monty result.
    ResultCompatibility(ResultCompatibilityError),
    /// A caller-selected data directory overlaps the untrusted workspace.
    UnsafeDataDirectory {
        /// Host workspace capability root.
        workspace_root: PathBuf,
        /// Rejected caller-selected durable directory.
        data_directory: PathBuf,
    },
    /// A content-addressed artifact decoded to another transaction identity.
    ArtifactBinding {
        /// Transaction requested by the caller and state store.
        requested: TransactionId,
        /// Transaction recomputed from decoded artifact contents.
        decoded: TransactionId,
    },
    /// Startup recovery found ownership it could not prove and left it untouched.
    RecoveryConflicts(Box<RecoveryReport>),
    /// The transaction record has no durable exact artifact.
    MissingPending {
        /// Requested transaction.
        transaction: TransactionId,
    },
    /// A duplicate exact artifact attempted to occupy the pending map.
    DuplicatePending {
        /// Duplicate transaction.
        transaction: TransactionId,
    },
    /// Process-local preview retention reached its configured hard bound.
    EphemeralCapacity {
        /// Number of previews retained before this attempt.
        entries: usize,
        /// Maximum previews retained by one runtime.
        max_entries: usize,
        /// Total encoded bytes that retaining this preview would require.
        attempted_bytes: usize,
        /// Maximum encoded bytes retained by one runtime.
        max_bytes: usize,
    },
    /// The short-lived pending-artifact mutex was poisoned by a panic.
    PendingPoisoned,
    /// Direct commit was blocked because a configured hook must decide first.
    HookRequired(Box<RequestEvent>),
    /// The external hook handler failed after fail-closed state was applied.
    HookHandler(HookHandlerError),
    /// Durable state changed after the event was prepared.
    HookStateChanged {
        /// Exact transaction represented by the preparation.
        transaction: TransactionId,
        /// State captured while preparing the hook event.
        prepared: TransactionState,
        /// State observed when resolving the hook decision.
        actual: TransactionState,
    },
    /// Hook configuration no longer matches the prepared transaction evidence.
    HookConfigurationChanged {
        /// Transaction whose bound hook configuration changed.
        transaction: TransactionId,
    },
    /// The event supplied for resolution was not the exact regenerated event.
    HookEventMismatch {
        /// Transaction whose regenerated event did not match.
        transaction: TransactionId,
    },
    /// A handler decision was supplied when no handler was requested.
    UnexpectedHookDecision {
        /// Transaction that did not request a hook decision.
        transaction: TransactionId,
    },
    /// The selected transaction state cannot accept a hook decision.
    HookNotActionable {
        /// Transaction rejected by the hook state guard.
        transaction: TransactionId,
        /// Non-actionable state observed by the resolver.
        actual: TransactionState,
    },
    /// Hook feedback exceeded its configured hard UTF-8 bound.
    HookReasonLimit {
        /// Observed UTF-8 byte length.
        observed: usize,
        /// Configured maximum UTF-8 byte length.
        maximum: usize,
    },
    /// Hook approval expiry overflowed or did not advance host time.
    HookApprovalWindow {
        /// Transaction whose approval window was invalid.
        transaction: TransactionId,
    },
    /// Legacy or truncated evidence cannot be approved by an automated hook.
    IncompleteHookEvidence {
        /// Transaction without complete hook evidence.
        transaction: TransactionId,
    },
}

impl fmt::Display for VshError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DataDirectory(source) => fmt::Display::fmt(source, formatter),
            Self::Blob(source) => fmt::Display::fmt(source, formatter),
            Self::Commit(source) => fmt::Display::fmt(source, formatter),
            Self::Execution(source) => fmt::Display::fmt(source, formatter),
            Self::Vfs(source) => fmt::Display::fmt(source, formatter),
            Self::Store(source) => fmt::Display::fmt(source, formatter),
            Self::Approval(source) => fmt::Display::fmt(source, formatter),
            Self::CommitPlan(source) => fmt::Display::fmt(source, formatter),
            Self::Artifact(source) => fmt::Display::fmt(source, formatter),
            Self::ResultCompatibility(source) => fmt::Display::fmt(source, formatter),
            Self::UnsafeDataDirectory {
                workspace_root,
                data_directory,
            } => write!(
                formatter,
                "trusted data directory {} must be disjoint from workspace {}",
                data_directory.display(),
                workspace_root.display()
            ),
            Self::ArtifactBinding { requested, decoded } => write!(
                formatter,
                "pending artifact for {requested} decodes to transaction {decoded}"
            ),
            Self::RecoveryConflicts(report) => write!(
                formatter,
                "startup recovery left {} ambiguous transaction(s)",
                report.conflicts.len()
            ),
            Self::MissingPending { transaction } => {
                write!(
                    formatter,
                    "no durable pending artifact for transaction {transaction}"
                )
            }
            Self::DuplicatePending { transaction } => {
                write!(
                    formatter,
                    "pending artifact already exists for {transaction}"
                )
            }
            Self::EphemeralCapacity {
                entries,
                max_entries,
                attempted_bytes,
                max_bytes,
            } => write!(
                formatter,
                "process-local preview capacity exceeded: {entries}/{max_entries} entries, \
                 {attempted_bytes}/{max_bytes} encoded bytes"
            ),
            Self::PendingPoisoned => formatter.write_str("pending artifact lock was poisoned"),
            Self::HookRequired(event) => write!(
                formatter,
                "commit hook {} must decide request event {} for transaction {}",
                event.hook_id, event.event_id, event.transaction
            ),
            Self::HookHandler(source) => write!(formatter, "commit hook handler failed: {source}"),
            Self::HookStateChanged {
                transaction,
                prepared,
                actual,
            } => write!(
                formatter,
                "transaction {transaction} changed from prepared state {prepared:?} to {actual:?}"
            ),
            Self::HookConfigurationChanged { transaction } => write!(
                formatter,
                "commit hook configuration changed for transaction {transaction}"
            ),
            Self::HookEventMismatch { transaction } => write!(
                formatter,
                "commit hook event does not match transaction {transaction}"
            ),
            Self::UnexpectedHookDecision { transaction } => write!(
                formatter,
                "transaction {transaction} did not request a hook decision"
            ),
            Self::HookNotActionable {
                transaction,
                actual,
            } => write!(
                formatter,
                "transaction {transaction} in state {actual:?} cannot accept a hook decision"
            ),
            Self::HookReasonLimit { observed, maximum } => write!(
                formatter,
                "hook feedback uses {observed} bytes, exceeding the {maximum}-byte limit"
            ),
            Self::HookApprovalWindow { transaction } => write!(
                formatter,
                "hook approval window is invalid for transaction {transaction}"
            ),
            Self::IncompleteHookEvidence { transaction } => write!(
                formatter,
                "transaction {transaction} has incomplete evidence and cannot be hook-approved"
            ),
        }
    }
}

impl Error for VshError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::DataDirectory(source) => Some(source),
            Self::Blob(source) => Some(source),
            Self::Commit(source) => Some(source),
            Self::Execution(source) => Some(source),
            Self::Vfs(source) => Some(source),
            Self::Store(source) => Some(source),
            Self::Approval(source) => Some(source),
            Self::CommitPlan(source) => Some(source),
            Self::Artifact(source) => Some(source),
            Self::ResultCompatibility(source) => Some(source),
            Self::HookHandler(source) => Some(source),
            Self::RecoveryConflicts(_)
            | Self::UnsafeDataDirectory { .. }
            | Self::ArtifactBinding { .. }
            | Self::MissingPending { .. }
            | Self::DuplicatePending { .. }
            | Self::EphemeralCapacity { .. }
            | Self::PendingPoisoned
            | Self::HookRequired(_)
            | Self::HookStateChanged { .. }
            | Self::HookConfigurationChanged { .. }
            | Self::HookEventMismatch { .. }
            | Self::UnexpectedHookDecision { .. }
            | Self::HookNotActionable { .. }
            | Self::HookReasonLimit { .. }
            | Self::HookApprovalWindow { .. }
            | Self::IncompleteHookEvidence { .. } => None,
        }
    }
}

impl From<DataDirectoryError> for VshError {
    fn from(source: DataDirectoryError) -> Self {
        Self::DataDirectory(source)
    }
}

impl From<BlobStoreError> for VshError {
    fn from(source: BlobStoreError) -> Self {
        Self::Blob(source)
    }
}

impl From<CommitError> for VshError {
    fn from(source: CommitError) -> Self {
        Self::Commit(source)
    }
}

impl From<ExecutionError> for VshError {
    fn from(source: ExecutionError) -> Self {
        Self::Execution(source)
    }
}

impl From<VfsError> for VshError {
    fn from(source: VfsError) -> Self {
        Self::Vfs(source)
    }
}

impl From<TransactionStoreError> for VshError {
    fn from(source: TransactionStoreError) -> Self {
        Self::Store(source)
    }
}

impl From<ApprovalGrantError> for VshError {
    fn from(source: ApprovalGrantError) -> Self {
        Self::Approval(source)
    }
}

impl From<CommitPlanError> for VshError {
    fn from(source: CommitPlanError) -> Self {
        Self::CommitPlan(source)
    }
}

impl From<ArtifactError> for VshError {
    fn from(source: ArtifactError) -> Self {
        Self::Artifact(source)
    }
}

impl From<ResultCompatibilityError> for VshError {
    fn from(source: ResultCompatibilityError) -> Self {
        Self::ResultCompatibility(source)
    }
}

#[cfg(test)]
mod tests {
    use std::error::Error;
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::sync::{Arc, Mutex};

    use vsh_commit::CommitError;
    use vsh_policy::{DenyReason, PolicyProfile};
    use vsh_types::{PrincipalId, TransactionState};

    use super::{
        ApprovalGrantError, ArtifactError, ArtifactLimits, BlobStoreError, CommitPlanError,
        DataDirectory, ExecutionBudget, ExecutionError, ReceiptDetail, ResultCompatibility,
        ResultCompatibilityError, RunMode, RunRequest, Runtime, RuntimeConfig, RuntimeDecision,
        SnapshotLimits, TransactionStoreError, VfsError, VshError,
    };
    use crate::hook::{
        HookConfig, HookDecision, HookHandlerError, HookScope, HookVerdict, HookedRuntime,
        RequestEvent,
    };

    static TEST_SEQUENCE: AtomicU64 = AtomicU64::new(0);

    struct TestDirectory(PathBuf);

    impl TestDirectory {
        fn new(name: &str) -> Self {
            let sequence = TEST_SEQUENCE.fetch_add(1, Ordering::Relaxed);
            let path = std::env::temp_dir().join(format!(
                "vsh-runtime-{name}-{}-{sequence}",
                std::process::id()
            ));
            fs::create_dir(&path).expect("unique test workspace should be created");
            Self(path)
        }

        fn path(&self) -> &Path {
            &self.0
        }
    }

    impl Drop for TestDirectory {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn auto_mode_commits_one_exact_virtual_result() {
        let directory = TestDirectory::new("auto");
        fs::write(directory.path().join("input.txt"), b"hello\n").unwrap();
        let runtime =
            Runtime::open(RuntimeConfig::new(directory.path()).with_in_process_execution())
                .unwrap();
        let receipt = runtime
            .run(
                RunRequest::new(
                    r"
from pathlib import Path
value = Path('/workspace/input.txt').read_text()
Path('/workspace/output.txt').write_text(value.upper())
len(value)
",
                )
                .with_mode(RunMode::Auto)
                .with_detail(ReceiptDetail::Full),
            )
            .unwrap();

        assert_eq!(receipt.state, TransactionState::Committed);
        assert!(matches!(receipt.decision, RuntimeDecision::AutoApproved));
        assert_eq!(receipt.changed_paths, 1);
        assert_eq!(receipt.changes.len(), 1);
        assert_eq!(
            fs::read(directory.path().join("output.txt")).unwrap(),
            b"HELLO\n"
        );
        assert!(receipt.commit.is_some());
    }

    #[test]
    fn oversized_program_is_rejected_before_workspace_snapshot() {
        let directory = TestDirectory::new("program-preflight");
        let runtime = Runtime::open(
            RuntimeConfig::new(directory.path())
                .with_snapshot_limits(SnapshotLimits {
                    max_nodes: 0,
                    ..SnapshotLimits::default()
                })
                .with_in_process_execution(),
        )
        .unwrap();
        let budget = ExecutionBudget {
            max_program_bytes: 1,
            ..ExecutionBudget::default()
        };

        let error = runtime
            .run(RunRequest::new("42").with_budget(budget))
            .unwrap_err();
        assert!(matches!(
            error,
            VshError::Execution(ExecutionError::Limit(source))
                if matches!(*source, vsh_monty::ExecutionLimitExceeded::ProgramBytes {
                    limit: 1,
                    attempted: 2,
                })
        ));
    }

    #[test]
    fn process_local_preview_cache_is_bounded_and_explicitly_releasable() {
        let directory = TestDirectory::new("ephemeral-capacity");
        let runtime = Runtime::open(
            RuntimeConfig::new(directory.path())
                .with_artifact_limits(ArtifactLimits {
                    max_ephemeral_entries: 1,
                    ..ArtifactLimits::default()
                })
                .with_in_process_execution(),
        )
        .unwrap();

        let first = runtime.preview(RunRequest::new("None")).unwrap();
        let error = runtime.preview(RunRequest::new("0")).unwrap_err();
        assert!(matches!(
            error,
            VshError::EphemeralCapacity {
                entries: 1,
                max_entries: 1,
                ..
            }
        ));

        assert!(runtime.discard_preview(first.transaction).unwrap());
        assert!(!runtime.discard_preview(first.transaction).unwrap());
        runtime.preview(RunRequest::new("1")).unwrap();
    }

    #[test]
    fn python_result_incompatibility_prevents_auto_commit() {
        let directory = TestDirectory::new("python-result");
        let runtime = Runtime::open(
            RuntimeConfig::new(directory.path())
                .with_result_compatibility(ResultCompatibility::Python)
                .with_in_process_execution(),
        )
        .unwrap();

        let error = runtime
            .run(
                RunRequest::new(
                    r"
from pathlib import Path
Path('/workspace/must-not-exist.txt').write_text('blocked')
type({}.keys())
",
                )
                .with_mode(RunMode::Auto),
            )
            .unwrap_err();

        assert!(matches!(error, VshError::ResultCompatibility(_)));
        assert!(!directory.path().join("must-not-exist.txt").exists());
    }

    #[test]
    fn strict_preview_requires_exact_approval_before_commit() {
        let directory = TestDirectory::new("approval");
        let runtime = Runtime::open(
            RuntimeConfig::new(directory.path())
                .with_policy_profile(PolicyProfile::Strict)
                .with_in_process_execution(),
        )
        .unwrap();
        let receipt = runtime
            .preview(RunRequest::new(
                "from pathlib import Path\nPath('/workspace/approved.txt').write_text('yes')",
            ))
            .unwrap();
        assert_eq!(receipt.state, TransactionState::PendingApproval);
        assert!(matches!(
            receipt.decision,
            RuntimeDecision::PendingApproval(_)
        ));
        assert!(!directory.path().join("approved.txt").exists());

        runtime
            .approve(
                receipt.transaction,
                PrincipalId::digest_label("independent-test-principal"),
                10,
                20,
            )
            .unwrap();
        let committed = runtime.commit(receipt.transaction, 11).unwrap();
        assert_eq!(committed.state, TransactionState::Committed);
        assert_eq!(
            fs::read(directory.path().join("approved.txt")).unwrap(),
            b"yes"
        );
    }

    #[test]
    fn approval_artifact_survives_runtime_restart() {
        let directory = TestDirectory::new("approval-restart");
        let config = RuntimeConfig::new(directory.path())
            .with_policy_profile(PolicyProfile::Strict)
            .with_in_process_execution();
        let receipt = Runtime::open(config.clone())
            .unwrap()
            .preview(
                RunRequest::new(
                    "from pathlib import Path\nPath('/workspace/restarted.txt').write_text('yes')\n{'answer': 42}",
                )
                .with_detail(ReceiptDetail::Full),
            )
            .unwrap();
        assert_eq!(receipt.state, TransactionState::PendingApproval);

        let reopened = Runtime::open(config).unwrap();
        reopened
            .approve(
                receipt.transaction,
                PrincipalId::digest_label("restart-principal"),
                100,
                200,
            )
            .unwrap();
        let committed = reopened.commit(receipt.transaction, 101).unwrap();

        assert_eq!(committed.state, TransactionState::Committed);
        assert_eq!(committed.value.py_repr(), "{'answer': 42}");
        assert_eq!(committed.changes, receipt.changes);
        assert_eq!(
            fs::read(directory.path().join("restarted.txt")).unwrap(),
            b"yes"
        );
    }

    #[test]
    fn caught_protected_read_deterministically_denies_all_changes() {
        let directory = TestDirectory::new("deny");
        fs::write(directory.path().join(".env"), b"TOKEN=secret\n").unwrap();
        let runtime =
            Runtime::open(RuntimeConfig::new(directory.path()).with_in_process_execution())
                .unwrap();
        let receipt = runtime
            .run(
                RunRequest::new(
                    r"
from pathlib import Path
try:
    Path('/workspace/.env').read_text()
except PermissionError:
    Path('/workspace/should-not-exist.txt').write_text('blocked')
",
                )
                .with_mode(RunMode::Auto),
            )
            .unwrap();

        assert_eq!(receipt.state, TransactionState::Denied);
        assert!(matches!(
            receipt.decision,
            RuntimeDecision::Denied(ref manifest)
                if matches!(manifest.reason, DenyReason::ProtectedAccessAttempt(_))
        ));
        assert!(!directory.path().join("should-not-exist.txt").exists());
    }

    #[test]
    fn stale_preview_never_overwrites_external_work() {
        let directory = TestDirectory::new("stale");
        fs::write(directory.path().join("input.txt"), b"before").unwrap();
        let runtime =
            Runtime::open(RuntimeConfig::new(directory.path()).with_in_process_execution())
                .unwrap();
        let receipt = runtime
            .preview(RunRequest::new(
                r"
from pathlib import Path
value = Path('/workspace/input.txt').read_text()
Path('/workspace/output.txt').write_text(value)
",
            ))
            .unwrap();
        fs::write(directory.path().join("input.txt"), b"external").unwrap();

        let error = runtime.commit(receipt.transaction, 0).unwrap_err();
        assert!(matches!(error, VshError::Commit(CommitError::Stale { .. })));
        assert!(!directory.path().join("output.txt").exists());
        assert_eq!(
            runtime.transaction(receipt.transaction).unwrap().state(),
            TransactionState::Stale
        );
    }

    #[test]
    fn explicit_data_directory_is_external_and_capability_rooted() {
        let workspace = TestDirectory::new("external-data-workspace");
        let data = TestDirectory::new("external-data-store");
        let config = RuntimeConfig::new(workspace.path())
            .with_data_directory(data.path())
            .with_in_process_execution();

        assert_eq!(config.workspace_root(), workspace.path());
        assert_eq!(config.data_directory(), data.path());
        assert!(config.worker_path().is_none());

        let runtime = Runtime::open(config).unwrap();
        runtime.preview(RunRequest::new("42")).unwrap();

        assert!(data.path().join("blobs").is_dir());
        assert!(data.path().join("transactions.lock").is_file());
        assert!(workspace.path().join(".vsh-runtime/transactions").is_dir());
    }

    #[test]
    fn explicit_data_directory_cannot_overlap_workspace() {
        let workspace = TestDirectory::new("overlapping-data");
        let data = workspace.path().join("caller-selected-data");
        let result = Runtime::open(
            RuntimeConfig::new(workspace.path())
                .with_data_directory(&data)
                .with_in_process_execution(),
        );

        assert!(matches!(result, Err(VshError::UnsafeDataDirectory { .. })));
        assert!(!data.exists());
    }

    #[cfg(unix)]
    #[test]
    fn default_runtime_symlink_fails_before_external_write() {
        use std::os::unix::fs::symlink;

        let workspace = TestDirectory::new("runtime-symlink-workspace");
        let outside = TestDirectory::new("runtime-symlink-outside");
        symlink(outside.path(), workspace.path().join(".vsh-runtime")).unwrap();

        let result =
            Runtime::open(RuntimeConfig::new(workspace.path()).with_in_process_execution());

        assert!(matches!(
            result,
            Err(VshError::Commit(CommitError::InternalIo { .. }))
        ));
        assert_eq!(fs::read_dir(outside.path()).unwrap().count(), 0);
    }

    #[cfg(unix)]
    #[test]
    fn canonical_alias_into_workspace_is_rejected_before_store_files() {
        use std::os::unix::fs::symlink;

        let workspace = TestDirectory::new("canonical-overlap-workspace");
        let alias_root = TestDirectory::new("canonical-overlap-alias");
        let alias = alias_root.path().join("workspace-alias");
        symlink(workspace.path(), &alias).unwrap();
        let data = alias.join("nested-data");

        let result = Runtime::open(
            RuntimeConfig::new(workspace.path())
                .with_data_directory(&data)
                .with_in_process_execution(),
        );

        assert!(matches!(result, Err(VshError::UnsafeDataDirectory { .. })));
        assert!(!workspace.path().join("nested-data").exists());
    }

    #[test]
    fn review_hook_receives_complete_canonical_evidence_and_can_commit() {
        let directory = TestDirectory::new("review-hook-approve");
        let observed = Arc::new(Mutex::new(None::<RequestEvent>));
        let observed_by_hook = Arc::clone(&observed);
        let runtime = HookedRuntime::open(
            RuntimeConfig::new(directory.path())
                .with_policy_profile(PolicyProfile::Strict)
                .with_in_process_execution(),
            HookConfig::new("security-review"),
            move |event: &RequestEvent| {
                *observed_by_hook.lock().unwrap() = Some(event.clone());
                Ok(HookDecision::approve("canonical evidence is safe"))
            },
        )
        .unwrap();

        let receipt = runtime
            .run(
                RunRequest::new(
                    "from pathlib import Path\nPath('/workspace/reviewed.txt').write_text('safe')",
                )
                .with_intent("create the reviewed output")
                .with_mode(RunMode::Auto),
                1_000,
            )
            .unwrap();

        assert_eq!(receipt.state, TransactionState::Committed);
        assert_eq!(
            fs::read(directory.path().join("reviewed.txt")).unwrap(),
            b"safe"
        );
        let event = observed.lock().unwrap().clone().unwrap();
        assert_eq!(event.transaction, receipt.transaction);
        assert_eq!(event.intent.as_deref(), Some("create the reviewed output"));
        assert_eq!(event.canonical_diff.len(), 1);
        assert_eq!(event.canonical_diff[0].path.as_str(), "reviewed.txt");
        assert!(!event.effects.is_empty());
        assert!(event.evidence_complete);
        assert!(!event.evidence_truncated);
    }

    #[test]
    fn review_content_binds_before_after_and_survives_restart_without_live_reads() {
        let directory = TestDirectory::new("review-content-restart");
        let path = directory.path().join("config.txt");
        fs::write(&path, b"before").unwrap();
        let config = RuntimeConfig::new(directory.path())
            .with_policy_profile(PolicyProfile::Strict)
            .with_commit_hook(HookConfig::new("content-review").with_max_content_bytes(1024))
            .with_in_process_execution();
        let runtime = Runtime::open(config.clone()).unwrap();
        let preview = runtime
            .preview(RunRequest::new(
                "vsh_write('/workspace/config.txt', 'after')",
            ))
            .unwrap();
        let prepared = runtime.prepare_commit(preview.transaction).unwrap();
        let event = prepared.event().unwrap();
        assert!(event.content_complete);
        assert_eq!(
            event
                .contents
                .iter()
                .map(|content| content.bytes.as_slice())
                .collect::<Vec<_>>(),
            vec![b"before".as_slice(), b"after".as_slice()]
        );
        assert!(
            event
                .contents
                .iter()
                .all(|content| content.path.as_str() == "config.txt")
        );
        drop(runtime);

        fs::write(&path, b"external change").unwrap();
        let restarted = Runtime::open(config).unwrap();
        let after_restart = restarted.prepare_commit(preview.transaction).unwrap();
        assert_eq!(after_restart.event(), prepared.event());
        assert!(matches!(
            restarted.resolve_commit(
                &after_restart,
                &HookDecision::approve("reviewed exact bytes"),
                1000
            ),
            Err(VshError::Commit(CommitError::Stale { .. }))
        ));
        assert_eq!(fs::read(path).unwrap(), b"external change");
    }

    #[test]
    fn review_content_budget_is_explicit_and_never_labels_partial_content_complete() {
        let directory = TestDirectory::new("review-content-budget");
        fs::write(directory.path().join("file.txt"), b"large before").unwrap();
        for maximum in [0, 3] {
            let runtime = Runtime::open(
                RuntimeConfig::new(directory.path())
                    .with_policy_profile(PolicyProfile::Strict)
                    .with_commit_hook(HookConfig::new("bounded").with_max_content_bytes(maximum))
                    .with_in_process_execution(),
            )
            .unwrap();
            let preview = runtime
                .preview(RunRequest::new("vsh_write('/workspace/file.txt', 'new')"))
                .unwrap();
            let prepared = runtime.prepare_commit(preview.transaction).unwrap();
            let event = prepared.event().unwrap();
            assert!(!event.content_complete);
            assert!(
                event
                    .contents
                    .iter()
                    .map(|content| content.bytes.len())
                    .sum::<usize>()
                    <= maximum
            );
            assert_eq!(
                fs::read(directory.path().join("file.txt")).unwrap(),
                b"large before"
            );
        }
    }

    #[test]
    fn read_only_review_contains_exact_observed_content_once() {
        let directory = TestDirectory::new("review-read-content");
        fs::write(directory.path().join("read.txt"), b"read evidence").unwrap();
        let runtime = Runtime::open(
            RuntimeConfig::new(directory.path())
                .with_commit_hook(
                    HookConfig::new("read-content")
                        .with_scope(HookScope::AllRequests)
                        .with_max_content_bytes(100),
                )
                .with_in_process_execution(),
        )
        .unwrap();
        let receipt = runtime
            .preview(RunRequest::new(
                "vsh_read('/workspace/read.txt')\nvsh_read('/workspace/read.txt')",
            ))
            .unwrap();
        let preparation = runtime.prepare_commit(receipt.transaction).unwrap();
        let event = preparation.event().unwrap();
        assert!(event.canonical_diff.is_empty());
        assert!(event.content_complete);
        assert_eq!(event.contents.len(), 1);
        assert_eq!(event.contents[0].bytes, b"read evidence");
    }

    #[test]
    fn empty_after_content_fits_an_exact_before_byte_budget() {
        let directory = TestDirectory::new("review-empty-content");
        fs::write(directory.path().join("file.txt"), b"abc").unwrap();
        let runtime = Runtime::open(
            RuntimeConfig::new(directory.path())
                .with_policy_profile(PolicyProfile::Strict)
                .with_commit_hook(HookConfig::new("empty-after").with_max_content_bytes(3))
                .with_in_process_execution(),
        )
        .unwrap();
        let preview = runtime
            .preview(RunRequest::new("vsh_write('/workspace/file.txt', '')"))
            .unwrap();
        let preparation = runtime.prepare_commit(preview.transaction).unwrap();
        let event = preparation.event().unwrap();
        assert!(event.content_complete);
        assert_eq!(event.contents.len(), 2);
        assert_eq!(event.contents[0].bytes, b"abc");
        assert!(event.contents[1].bytes.is_empty());
    }

    #[test]
    fn ready_preparation_cannot_bypass_an_applicable_hook() {
        let directory = TestDirectory::new("hook-forged-ready");
        let runtime = Runtime::open(
            RuntimeConfig::new(directory.path())
                .with_commit_hook(HookConfig::new("required").with_scope(HookScope::AllRequests))
                .with_in_process_execution(),
        )
        .unwrap();
        let receipt = runtime
            .preview(RunRequest::new(
                "vsh_write('/workspace/result.txt', 'must not commit')",
            ))
            .unwrap();
        runtime.prepare_commit(receipt.transaction).unwrap();
        let forged = super::CommitPreparation::Ready {
            transaction: receipt.transaction,
            state: receipt.state,
        };
        assert!(matches!(
            runtime.resolve_commit(&forged, &HookDecision::FollowPolicy, 1000),
            Err(VshError::HookRequired(_))
        ));
        assert!(!directory.path().join("result.txt").exists());
    }

    #[test]
    fn content_review_never_widens_native_read_permissions() {
        use vsh_policy::{AccessSet, CallPolicy, ProtectedRule, TransactionPolicy};

        let directory = TestDirectory::new("review-read-permissions");
        fs::write(directory.path().join("restricted.txt"), b"private before").unwrap();
        let policy = TransactionPolicy::new(
            PolicyProfile::Balanced,
            TransactionPolicy::default().thresholds(),
            CallPolicy::new(vec![
                ProtectedRule::new("restricted.txt", AccessSet::CONTENT_READ).unwrap(),
            ]),
        )
        .unwrap();
        let runtime = Runtime::open(
            RuntimeConfig::new(directory.path())
                .with_policy(policy)
                .with_commit_hook(
                    HookConfig::new("no-read")
                        .with_scope(HookScope::AllRequests)
                        .with_max_content_bytes(1024),
                )
                .with_in_process_execution(),
        )
        .unwrap();
        let receipt = runtime
            .preview(RunRequest::new(
                "vsh_write('/workspace/restricted.txt', 'replacement')",
            ))
            .unwrap();
        let prepared = runtime.prepare_commit(receipt.transaction).unwrap();
        let event = prepared.event().unwrap();
        assert!(!event.content_complete);
        assert!(event.contents.is_empty());
    }

    #[test]
    fn all_hook_can_return_feedback_and_keep_a_transaction_pending() {
        let directory = TestDirectory::new("hook-review-feedback");
        let calls = Arc::new(AtomicU64::new(0));
        let calls_by_hook = Arc::clone(&calls);
        let runtime = HookedRuntime::open(
            RuntimeConfig::new(directory.path()).with_in_process_execution(),
            HookConfig::new("evidence-judge").with_scope(HookScope::AllRequests),
            move |_event: &RequestEvent| {
                calls_by_hook.fetch_add(1, Ordering::Relaxed);
                Ok(HookDecision::review(
                    "generated file requires an explicit main-agent confirmation",
                ))
            },
        )
        .unwrap();
        let preview = runtime
            .preview(RunRequest::new(
                "from pathlib import Path\nPath('/workspace/check-me.txt').write_text('value')",
            ))
            .unwrap();
        assert_eq!(preview.state, TransactionState::AutoApproved);

        let resolution = runtime.commit(preview.transaction, 2_000).unwrap();
        assert_eq!(resolution.receipt.state, TransactionState::PendingApproval);
        let decision = resolution.hook.unwrap();
        assert_eq!(decision.verdict, HookVerdict::Review);
        assert!(decision.reason.contains("main-agent"));
        assert!(!directory.path().join("check-me.txt").exists());
        assert_eq!(calls.load(Ordering::Relaxed), 1);

        runtime
            .approve(
                preview.transaction,
                PrincipalId::digest_label("main-agent"),
                2_100,
                3_000,
            )
            .unwrap();
        let committed = runtime.commit(preview.transaction, 2_200).unwrap();
        assert_eq!(committed.receipt.state, TransactionState::Committed);
        assert_eq!(calls.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn all_requests_scope_delivers_read_only_evidence() {
        let directory = TestDirectory::new("hook-read-only");
        fs::write(directory.path().join("input.txt"), b"evidence").unwrap();
        let observed = Arc::new(Mutex::new(None::<RequestEvent>));
        let observed_by_hook = Arc::clone(&observed);
        let runtime = HookedRuntime::open(
            RuntimeConfig::new(directory.path()).with_in_process_execution(),
            HookConfig::new("read-review").with_scope(HookScope::AllRequests),
            move |event: &RequestEvent| {
                *observed_by_hook.lock().unwrap() = Some(event.clone());
                Ok(HookDecision::approve("bounded read is acceptable"))
            },
        )
        .unwrap();

        let receipt = runtime
            .run(
                RunRequest::new(
                    "from pathlib import Path\nPath('/workspace/input.txt').read_text()",
                )
                .with_mode(RunMode::Auto),
                10,
            )
            .unwrap();

        assert_eq!(receipt.state, TransactionState::Committed);
        assert_eq!(receipt.changed_paths, 0);
        let event = observed.lock().unwrap().clone().unwrap();
        assert!(event.canonical_diff.is_empty());
        assert!(
            event
                .effects
                .iter()
                .any(|effect| matches!(effect.effect, vsh_vfs::Effect::ContentRead { .. }))
        );
        assert!(event.execution.read_bytes > 0);
    }

    #[test]
    fn failed_hook_closes_auto_approval_into_pending_review() {
        let directory = TestDirectory::new("hook-failure");
        let runtime = HookedRuntime::open(
            RuntimeConfig::new(directory.path()).with_in_process_execution(),
            HookConfig::new("failing-hook").with_scope(HookScope::AllRequests),
            |_event: &RequestEvent| Err(HookHandlerError::new("judge unavailable")),
        )
        .unwrap();
        let preview = runtime
            .preview(RunRequest::new(
                "from pathlib import Path\nPath('/workspace/not-yet.txt').write_text('value')",
            ))
            .unwrap();

        let error = runtime.commit(preview.transaction, 0).unwrap_err();
        assert!(matches!(error, VshError::HookHandler(_)));
        assert_eq!(
            runtime.transaction(preview.transaction).unwrap().state(),
            TransactionState::PendingApproval
        );
        assert!(!directory.path().join("not-yet.txt").exists());
    }

    #[test]
    fn hard_policy_denial_never_invokes_hook() {
        let directory = TestDirectory::new("hook-hard-deny");
        fs::write(directory.path().join(".env"), b"secret").unwrap();
        let calls = Arc::new(AtomicU64::new(0));
        let calls_by_hook = Arc::clone(&calls);
        let runtime = HookedRuntime::open(
            RuntimeConfig::new(directory.path()).with_in_process_execution(),
            HookConfig::new("deny-proof").with_scope(HookScope::AllRequests),
            move |_event: &RequestEvent| {
                calls_by_hook.fetch_add(1, Ordering::Relaxed);
                Ok(HookDecision::approve("must not run"))
            },
        )
        .unwrap();
        let receipt = runtime
            .run(
                RunRequest::new(
                    "from pathlib import Path\ntry:\n    Path('/workspace/.env').read_text()\nexcept PermissionError:\n    pass",
                )
                .with_mode(RunMode::Auto),
                0,
            )
            .unwrap();

        assert_eq!(receipt.state, TransactionState::Denied);
        assert_eq!(calls.load(Ordering::Relaxed), 0);
    }

    #[test]
    fn native_error_surface_is_catchable_and_stable() {
        let directory = TestDirectory::new("runtime-errors");
        let not_a_directory = directory.path().join("file");
        fs::write(&not_a_directory, b"file").unwrap();
        let data_error = DataDirectory::open_trusted(&not_a_directory).unwrap_err();
        let transaction = vsh_types::TransactionId::from_bytes([7; 32]);
        let sourced = [
            VshError::DataDirectory(data_error),
            VshError::Blob(BlobStoreError::Io {
                operation: "read",
                path: PathBuf::from("blob"),
                source: std::io::Error::other("test"),
            }),
            VshError::Commit(CommitError::BaseSnapshotBinding),
            VshError::Execution(ExecutionError::UnsupportedSuspension {
                kind: "test",
                name: Some("name".to_owned()),
            }),
            VshError::Vfs(VfsError::RootMutation),
            VshError::Store(TransactionStoreError::NotFound { id: transaction }),
            VshError::Approval(ApprovalGrantError::InvalidWindow {
                issued_at_unix_ms: 2,
                expires_at_unix_ms: 1,
            }),
            VshError::CommitPlan(CommitPlanError::RootMutation),
            VshError::Artifact(ArtifactError::BindingMismatch),
            VshError::ResultCompatibility(ResultCompatibilityError::Depth {
                limit: 1,
                attempted: 2,
            }),
        ];
        for error in sourced {
            assert!(!error.to_string().is_empty());
            assert!(Error::source(&error).is_some());
        }

        let unsourced = [
            VshError::UnsafeDataDirectory {
                workspace_root: PathBuf::from("workspace"),
                data_directory: PathBuf::from("workspace/data"),
            },
            VshError::ArtifactBinding {
                requested: transaction,
                decoded: vsh_types::TransactionId::from_bytes([8; 32]),
            },
            VshError::RecoveryConflicts(Box::default()),
            VshError::MissingPending { transaction },
            VshError::DuplicatePending { transaction },
            VshError::EphemeralCapacity {
                entries: 2,
                max_entries: 1,
                attempted_bytes: 2,
                max_bytes: 1,
            },
            VshError::PendingPoisoned,
        ];
        for error in unsourced {
            assert!(!error.to_string().is_empty());
            assert!(Error::source(&error).is_none());
        }
    }
}