microsandbox 0.6.9

`microsandbox` is the core library for the microsandbox project.
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
//! Local sandbox create flow: image pull, rootfs preparation, record
//! insertion, and process spawn, as [`LocalBackend`] inherent methods.
//!
//! [`LocalBackend::create_sandbox`] is the single entry point; the trait
//! impl's `create`/`create_detached` and the pull-progress shims on
//! [`Sandbox`] and `SandboxBuilder` all dispatch here.

use std::path::Path;
use std::sync::Arc;

use microsandbox_db::DbWriteConnection;
use microsandbox_db::pool::DbPools;
use microsandbox_image::{
    CachedImageMetadata, Digest, GlobalCache, PullOptions, PullProgress, PullProgressSender,
    PullResult, Reference, Registry, ext4, tree,
};
use sea_orm::{ColumnTrait, ConnectionTrait, EntityTrait, QueryFilter, Set, sea_query::Expr};
use tokio::sync::Mutex;

use super::LocalBackend;
use crate::MicrosandboxResult;
use crate::agent::AgentClient;
use crate::backend::Backend;
use crate::db::entity::{
    run as run_entity, sandbox as sandbox_entity, sandbox_label as sandbox_label_entity,
    sandbox_rootfs as sandbox_rootfs_entity,
};
use crate::runtime::{
    ProcessHandle, SpawnMode, ensure_named_volumes, rollback_created_named_volumes, spawn_sandbox,
};
use crate::sandbox::{
    FsEntryKind, PullPolicy, RootDisk, RootfsSource, Sandbox, SandboxConfig, SandboxStatus,
    apply_patches, build_upper_tree, remove_dir_if_exists, validate_env, validate_hostname,
    validate_labels, validate_sandbox_name, validate_volume_mounts,
};

//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------

/// Maximum time to wait for the sandbox process to expose the agent relay.
const AGENT_RELAY_READY_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(180);

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

/// Transient registry overrides from the SDK, merged with global config at pull time.
struct RegistryOverrides {
    auth: Option<microsandbox_image::RegistryAuth>,
    insecure: bool,
    ca_certs: Vec<Vec<u8>>,
}

/// OCI materialization selected for a create request.
///
/// Snapshot restores carry both their digest-pinned persistence reference and,
/// when found directly by digest, the cache metadata that may not be indexed by
/// that immutable reference yet.
struct ResolvedOciImage {
    pull_result: PullResult,
    metadata_reference: String,
    cached_metadata: Option<CachedImageMetadata>,
}

//--------------------------------------------------------------------------------------------------
// Methods: Create Flow
//--------------------------------------------------------------------------------------------------

impl LocalBackend {
    /// Local create path. Returns a complete [`Sandbox`] wrapping the supplied
    /// backend Arc.
    ///
    /// `backend` must be the `Arc<dyn Backend>` wrapping `self`: the trait
    /// impl and the pull-progress shims forward the Arc they were handed so
    /// the returned [`Sandbox`] routes follow-up calls through this same
    /// backend.
    pub(crate) async fn create_sandbox(
        &self,
        backend: Arc<dyn Backend>,
        mut config: SandboxConfig,
        mode: SpawnMode,
        progress: Option<PullProgressSender>,
    ) -> MicrosandboxResult<Sandbox> {
        tracing::debug!(
            sandbox = %config.spec.name,
            image = ?config.spec.image,
            mode = ?mode,
            cpus = config.spec.resources.cpus,
            memory_mib = config.spec.resources.memory_mib,
            "create_local: starting"
        );

        self.apply_deployment_profile(&mut config);
        config.apply_rootfs_defaults(&self.config().sandbox_defaults.oci)?;

        let mut pinned_manifest_digest: Option<String> = None;
        let mut pinned_reference: Option<String> = None;

        config.apply_runtime_defaults();
        validate_hostname(config.spec.runtime.hostname.as_deref())?;
        self.validate_sandbox_name_for_runtime(&config.spec.name)?;
        Self::validate_rootfs_source(&config.spec.image)?;
        validate_env(&config.spec.env)?;
        validate_labels(&config.spec.labels)?;
        validate_volume_mounts(&config.spec.mounts)?;
        if let Some(init) = &config.spec.init {
            crate::sandbox::init::validate(init)?;
        }

        // Initialize the database before any expensive image pull so we can
        // fail fast on conflicting persisted sandbox state.
        let db = self.db().await?;
        let sandbox_dir = self.sandboxes_dir().join(&config.spec.name);
        Self::prepare_create_target(db, &config, &sandbox_dir, &self.config().run_dir()).await?;

        // Resolve OCI images before spawning the sandbox process.
        if let RootfsSource::Oci(oci) = config.spec.image.clone() {
            let reference = oci.reference;
            let expected_snapshot_manifest_digest = config
                .snapshot_upper_source
                .as_ref()
                .and(config.manifest_digest.clone());
            let root_disk = oci
                .root_disk
                .unwrap_or(RootDisk::Managed { size_mib: None });
            let image_materialization = if matches!(&root_disk, RootDisk::Flat { .. }) {
                microsandbox_image::RootfsMaterialization::Flat
            } else {
                microsandbox_image::RootfsMaterialization::Layered
            };
            let overrides = RegistryOverrides {
                auth: config.registry_auth.clone(),
                insecure: config.insecure,
                ca_certs: config.ca_certs.clone(),
            };
            let ResolvedOciImage {
                pull_result,
                metadata_reference,
                cached_metadata,
            } = self
                .resolve_oci_image_for_create(
                    &reference,
                    config.spec.pull_policy,
                    overrides,
                    expected_snapshot_manifest_digest.as_deref(),
                    image_materialization,
                    progress,
                )
                .await?;

            // Snapshot overlays are meaningful only against the exact base
            // image digest captured in their descriptor.
            if let Some(expected) = expected_snapshot_manifest_digest.as_deref()
                && pull_result.manifest_digest.to_string() != expected
            {
                return Err(crate::MicrosandboxError::SnapshotIntegrity(format!(
                    "snapshot image digest mismatch: manifest pinned {}, resolved {}",
                    expected, pull_result.manifest_digest
                )));
            }

            // Merge image config defaults under user-provided config.
            config.merge_image_defaults(&pull_result.config);
            if let Some(init) = &config.spec.init {
                crate::sandbox::init::validate(init)?;
            }

            pinned_manifest_digest = Some(pull_result.manifest_digest.to_string());
            pinned_reference = Some(metadata_reference.clone());

            // Layered roots boot through the stitched VMDK descriptor. Flat
            // roots intentionally skip both fsmeta and VMDK materialization,
            // so requiring the descriptor here would make a cold SDK create
            // fail after successfully publishing its flat ext4 artifact.
            let cache_dir = self.cache_dir();
            let cache = GlobalCache::new_async(&cache_dir).await?;
            if image_materialization.includes_layered() {
                let vmdk_path = cache.vmdk_path(&pull_result.manifest_digest);
                if tokio::fs::metadata(&vmdk_path).await.is_err() {
                    return Err(crate::MicrosandboxError::Custom(format!(
                        "VMDK not materialized: {}",
                        vmdk_path.display()
                    )));
                }
            }

            // For patches, pass per-layer EROFS paths.
            let layer_erofs_paths: Vec<std::path::PathBuf> = pull_result
                .layer_diff_ids
                .iter()
                .map(|d| cache.layer_erofs_path(d))
                .collect();

            let flat_spec = match &root_disk {
                RootDisk::Flat {
                    size_mib,
                    clone,
                    fstype,
                } => {
                    if fstype.as_deref().unwrap_or("ext4") != "ext4" {
                        return Err(crate::MicrosandboxError::InvalidConfig(format!(
                            "flat root disks currently require fstype=ext4, got {}",
                            fstype.as_deref().unwrap_or_default()
                        )));
                    }
                    if !config.spec.patches.is_empty() {
                        return Err(crate::MicrosandboxError::InvalidConfig(
                            "patches are not yet compatible with flat OCI rootfs".into(),
                        ));
                    }
                    if config.snapshot_upper_source.is_some() {
                        return Err(crate::MicrosandboxError::InvalidConfig(
                            "from_snapshot is not yet compatible with flat OCI rootfs".into(),
                        ));
                    }

                    let flat_ref = cache
                        .read_flat_ref(&pull_result.manifest_digest)?
                        .ok_or_else(|| {
                            crate::MicrosandboxError::Custom(
                                "flat rootfs was not published by the image pull".into(),
                            )
                        })?;
                    let artifact_digest: Digest =
                        flat_ref.artifact_digest.parse().map_err(|e| {
                            crate::MicrosandboxError::Custom(format!(
                                "invalid flat rootfs artifact digest in cache: {e}"
                            ))
                        })?;
                    let minimum_mib = flat_ref.virtual_size_bytes.div_ceil(1024 * 1024);
                    let requested_mib = size_mib.map(u64::from).unwrap_or(u64::from(
                        crate::sandbox::config::DEFAULT_OCI_UPPER_SIZE_MIB,
                    ));
                    let target_mib = size_mib
                        .map(|_| requested_mib)
                        .unwrap_or_else(|| requested_mib.max(minimum_mib));
                    let target_mib = u32::try_from(target_mib).map_err(|_| {
                        crate::MicrosandboxError::InvalidConfig(
                            "flat root disk size exceeds supported MiB range".into(),
                        )
                    })?;
                    Some((cache.flat_blob_path(&artifact_digest), target_mib, *clone))
                }
                _ => None,
            };

            let upper_tree = if !config.spec.patches.is_empty() {
                Some(build_upper_tree(&config.spec.patches, &layer_erofs_paths).await?)
            } else {
                None
            };

            // Ensure sandbox storage exists before provisioning either a private flat rootfs or
            // the writable overlay upper image.
            tokio::fs::create_dir_all(&sandbox_dir).await?;
            let upper_path = sandbox_dir.join("upper.ext4");
            if let Some((base, target_mib, clone)) = flat_spec {
                crate::sandbox::flat_rootfs::create_private_flat_rootfs(
                    base,
                    sandbox_dir.join(crate::sandbox::flat_rootfs::FLAT_ROOTFS_FILENAME),
                    target_mib,
                    clone,
                )
                .await?;
                if let RootfsSource::Oci(oci) = &mut config.spec.image
                    && let Some(RootDisk::Flat { size_mib, .. }) = &mut oci.root_disk
                {
                    *size_mib = Some(target_mib);
                }
            } else if let Some(snap_upper) = config.snapshot_upper_source.take() {
                // Booting from a snapshot: copy the captured upper into
                // place, preserving sparseness. Patches are not
                // compatible with this path because they'd need to be
                // re-baked into the snapshot's upper, which we don't do.
                if upper_tree.is_some() {
                    return Err(crate::MicrosandboxError::InvalidConfig(
                        "patches cannot be combined with from_snapshot".into(),
                    ));
                }
                let dst = upper_path.clone();
                tokio::task::spawn_blocking(move || {
                    microsandbox_utils::copy::fast_copy(&snap_upper, &dst)
                })
                .await
                .map_err(|e| {
                    crate::MicrosandboxError::Custom(format!("snapshot copy task: {e}"))
                })??;
            } else {
                match &root_disk {
                    RootDisk::Managed { size_mib } => {
                        let upper_size_mib =
                            size_mib.unwrap_or(crate::sandbox::config::DEFAULT_OCI_UPPER_SIZE_MIB);
                        if !upper_path.exists() || upper_tree.is_some() {
                            Self::create_upper_ext4(&upper_path, upper_size_mib, upper_tree)
                                .await?;
                        }
                    }
                    // The builder rejects patches with tmpfs root disks and
                    // agentd creates the in-memory upper inside the guest.
                    RootDisk::Tmpfs { .. } => {}
                    RootDisk::DiskImage { path, .. } => {
                        if tokio::fs::metadata(path).await.is_err() {
                            return Err(crate::MicrosandboxError::InvalidConfig(format!(
                                "root disk image not found: {}",
                                path.display()
                            )));
                        }
                    }
                    RootDisk::Flat { .. } => {
                        unreachable!("flat root disks are provisioned before overlay handling")
                    }
                }
            }

            // Store manifest digest for spawn to derive paths.
            config.manifest_digest = Some(pull_result.manifest_digest.to_string());

            // Persist snapshot restores under their immutable digest-pinned
            // reference, even when the cache match came from an older tag.
            if let Some(metadata) = cached_metadata {
                if let Err(e) =
                    crate::image::Image::persist(self, &metadata_reference, metadata).await
                {
                    tracing::warn!(
                        error = %e,
                        "failed to persist image metadata to database"
                    );
                }
            } else if let Ok(image_ref) = metadata_reference.parse::<Reference>() {
                match cache.read_image_metadata_async(&image_ref).await {
                    Ok(Some(metadata)) => {
                        if let Err(e) =
                            crate::image::Image::persist(self, &metadata_reference, metadata).await
                        {
                            tracing::warn!(
                                error = %e,
                                "failed to persist image metadata to database"
                            );
                        }
                    }
                    Ok(None) => {}
                    Err(e) => {
                        tracing::warn!(error = %e, "failed to read cached image metadata");
                    }
                }
            }
        }

        // Apply rootfs patches before VM start (bind mounts only — OCI patches
        // are baked into upper.ext4 above).
        if !config.spec.patches.is_empty() && !matches!(config.spec.image, RootfsSource::Oci(_)) {
            apply_patches(&config.spec.image, &config.spec.patches).await?;
        }

        // Sandbox-time named-volume creation is one-shot create intent. Provision
        // before inserting the sandbox row so volume conflicts or incompatibilities
        // cannot leave a stopped sandbox that never booted.
        let created_named_volumes = ensure_named_volumes(self, &config).await?;

        // Insert the sandbox record and keep its stable database ID.
        let write_db = db.write();
        let persisted_config = config.clone_for_persistence();
        let sandbox_id = match Self::insert_sandbox_record(write_db, &persisted_config).await {
            Ok(sandbox_id) => sandbox_id,
            Err(err) => {
                rollback_created_named_volumes(self, &created_named_volumes).await;
                return Err(err);
            }
        };
        tracing::debug!(sandbox_id, sandbox = %config.spec.name, "create_local: db record inserted");

        // Spawn the sandbox process and create the bridge. On failure, mark the sandbox
        // as stopped so it doesn't appear as a phantom "Running" entry.
        let (local_state, returned_config) = match self
            .create_sandbox_inner(config, sandbox_id, mode, None)
            .await
        {
            Ok(pair) => pair,
            Err(e) => {
                if created_named_volumes.is_empty() {
                    let _ =
                        Self::update_sandbox_status(write_db, sandbox_id, SandboxStatus::Stopped)
                            .await;
                } else {
                    rollback_created_named_volumes(self, &created_named_volumes).await;
                    let _ = Self::delete_sandbox_record(write_db, sandbox_id).await;
                }
                return Err(e);
            }
        };
        let sandbox = Sandbox::from_local(backend.clone(), local_state, returned_config);
        if let Err(err) = Self::update_sandbox_active_config(
            write_db,
            sandbox_id,
            &sandbox.config().clone_for_persistence(),
        )
        .await
        {
            let _ = sandbox.stop().await;
            return Err(err);
        }

        if let (Some(_reference), Some(manifest_digest)) = (
            pinned_reference.as_deref(),
            pinned_manifest_digest.as_deref(),
        ) && let Err(err) =
            Self::persist_oci_manifest_pin(write_db, sandbox_id, manifest_digest).await
        {
            let _ = sandbox.stop().await;
            if created_named_volumes.is_empty() {
                let _ =
                    Self::update_sandbox_status(write_db, sandbox_id, SandboxStatus::Stopped).await;
            } else {
                rollback_created_named_volumes(self, &created_named_volumes).await;
                let _ = Self::delete_sandbox_record(write_db, sandbox_id).await;
            }
            return Err(err);
        }

        // Validate that the configured workdir exists inside the guest and is a
        // directory before returning a ready sandbox. Shell/exec calls inherit this
        // cwd, so accepting a regular file here leads to later, murkier failures.
        if let Some(ref workdir) = sandbox.config().spec.runtime.workdir {
            match sandbox.fs().stat(workdir).await {
                Ok(metadata) if metadata.kind == FsEntryKind::Directory => {}
                Ok(_) => {
                    let _ = sandbox.stop().await;
                    if created_named_volumes.is_empty() {
                        let _ = Self::update_sandbox_status(
                            write_db,
                            sandbox_id,
                            SandboxStatus::Stopped,
                        )
                        .await;
                    } else {
                        rollback_created_named_volumes(self, &created_named_volumes).await;
                        let _ = Self::delete_sandbox_record(write_db, sandbox_id).await;
                    }
                    return Err(crate::MicrosandboxError::InvalidConfig(format!(
                        "workdir is not a directory in guest: {workdir}"
                    )));
                }
                Err(_) => {
                    let _ = sandbox.stop().await;
                    if created_named_volumes.is_empty() {
                        let _ = Self::update_sandbox_status(
                            write_db,
                            sandbox_id,
                            SandboxStatus::Stopped,
                        )
                        .await;
                    } else {
                        rollback_created_named_volumes(self, &created_named_volumes).await;
                        let _ = Self::delete_sandbox_record(write_db, sandbox_id).await;
                    }
                    return Err(crate::MicrosandboxError::InvalidConfig(format!(
                        "workdir does not exist in guest: {workdir}"
                    )));
                }
            }
        }

        Ok(sandbox)
    }

    /// Inner local create logic separated for error-cleanup wrapper. Returns
    /// the local-variant state plus the (possibly mutated) config.
    pub(super) async fn create_sandbox_inner(
        &self,
        config: SandboxConfig,
        sandbox_id: i32,
        mode: SpawnMode,
        lifecycle_guard: Option<microsandbox_runtime::ipc::SandboxLifecycleGuard>,
    ) -> MicrosandboxResult<(crate::backend::SandboxLocalState, SandboxConfig)> {
        let (mut handle, agent_sock_path) =
            spawn_sandbox(self, &config, sandbox_id, mode, lifecycle_guard).await?;
        let log_dir = self.sandboxes_dir().join(&config.spec.name).join("logs");

        // Wait for the relay socket to become available.
        let client =
            Self::wait_for_relay(&agent_sock_path, &log_dir, &mut handle, &config.spec.name)
                .await?;

        if let Ok(ready) = client.ready() {
            tracing::info!(
                boot_time_ms = ready.boot_time_ns / 1_000_000,
                init_time_ms = ready.init_time_ns / 1_000_000,
                ready_time_ms = ready.ready_time_ns / 1_000_000,
                "sandbox ready",
            );
        }
        let handle = if matches!(mode, SpawnMode::Detached) {
            handle.disarm();
            None
        } else {
            Some(Arc::new(Mutex::new(handle)))
        };

        Ok((
            crate::backend::SandboxLocalState {
                db_id: sandbox_id,
                handle,
                client: Arc::new(client),
            },
            config,
        ))
    }

    /// Wait for the agent relay socket to become available and connect.
    ///
    /// The sandbox process creates the relay socket asynchronously during startup.
    /// This function retries the connection with brief delays until it succeeds
    /// or a timeout is reached.
    async fn wait_for_relay(
        sock_path: &std::path::Path,
        log_dir: &std::path::Path,
        handle: &mut ProcessHandle,
        sandbox_name: &str,
    ) -> MicrosandboxResult<AgentClient> {
        tracing::debug!(
            sock = %sock_path.display(),
            pid = handle.pid(),
            "wait_for_relay: waiting for agent socket"
        );
        let deadline = tokio::time::Instant::now() + AGENT_RELAY_READY_TIMEOUT;
        let max_backoff = std::time::Duration::from_millis(10);
        let mut backoff = std::time::Duration::from_millis(1);
        let mut attempts = 0u32;

        loop {
            attempts += 1;
            match tokio::time::timeout(
                deadline.saturating_duration_since(tokio::time::Instant::now()),
                AgentClient::connect(sock_path),
            )
            .await
            {
                Ok(Ok(client)) => {
                    tracing::debug!(attempts, "wait_for_relay: connected");
                    // The relay is up — clear any stale boot-error.json from
                    // a previous failed attempt so it cannot misattribute a
                    // future crash.
                    let _ = microsandbox_runtime::boot_error::BootError::delete(log_dir);
                    return Ok(client);
                }
                Ok(Err(_)) | Err(_) if tokio::time::Instant::now() < deadline => {
                    // Check if the sandbox process is still alive before retrying.
                    // If it crashed, there's no point waiting for the socket.
                    if let Some(status) = handle.try_wait()? {
                        tracing::debug!(
                            attempts,
                            ?status,
                            "wait_for_relay: sandbox process exited"
                        );

                        // Prefer the structured boot-error record if the
                        // sandbox got far enough to write one.
                        if let Some(boot_err) = Self::read_boot_error(log_dir) {
                            return Err(crate::MicrosandboxError::BootStart {
                                name: sandbox_name.to_string(),
                                err: boot_err,
                            });
                        }

                        // No structured boot-error.json — the sandbox died
                        // too early or too violently (e.g. a Rust panic exits
                        // 101 without running our atomic-writer). Synthesize
                        // an `Other`-stage record so the CLI still renders
                        // the styled error block with the `msb logs` hint
                        // instead of dumping a raw log directory path.
                        let synthetic = microsandbox_runtime::boot_error::BootError {
                            t: chrono::Utc::now()
                                .to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
                            stage: microsandbox_runtime::boot_error::BootErrorStage::Other,
                            errno: None,
                            message: format!(
                                "sandbox process exited ({status}) before agent relay became available"
                            ),
                        };
                        return Err(crate::MicrosandboxError::BootStart {
                            name: sandbox_name.to_string(),
                            err: synthetic,
                        });
                    }

                    // Keep early retries tight so relay readiness doesn't inherit a
                    // coarse fixed delay on warm starts.
                    tokio::time::sleep(backoff).await;
                    backoff = std::cmp::min(backoff.saturating_mul(2), max_backoff);
                }
                Ok(Err(e)) => {
                    tracing::debug!(
                        attempts,
                        error = %e,
                        "wait_for_relay: agent connection failed"
                    );
                    if let Some(boot_err) = Self::read_boot_error(log_dir) {
                        return Err(crate::MicrosandboxError::BootStart {
                            name: sandbox_name.to_string(),
                            err: boot_err,
                        });
                    }
                    return Err(e.into());
                }
                Err(e) => {
                    tracing::debug!(
                        attempts,
                        error = %e,
                        "wait_for_relay: timed out"
                    );
                    // Even when the process is still running, the sandbox
                    // may have written a structured boot-error before
                    // stalling (e.g. agentd reported a recoverable failure
                    // and never produced the handshake bytes). Prefer that
                    // typed record over the raw IO/timeout error so the CLI
                    // can render the styled boot-error block.
                    if let Some(boot_err) = Self::read_boot_error(log_dir) {
                        return Err(crate::MicrosandboxError::BootStart {
                            name: sandbox_name.to_string(),
                            err: boot_err,
                        });
                    }
                    return Err(crate::MicrosandboxError::Runtime(format!(
                        "timed out waiting for agent relay: {e}"
                    )));
                }
            }
        }
    }

    /// Read `boot-error.json` from `log_dir` if present and parseable.
    ///
    /// Returns `None` when the directory is unknown, the file is missing, or
    /// the contents cannot be deserialized — callers fall back to a raw
    /// error in those cases.
    fn read_boot_error(
        log_dir: &std::path::Path,
    ) -> Option<microsandbox_runtime::boot_error::BootError> {
        microsandbox_runtime::boot_error::BootError::read(log_dir)
            .ok()
            .flatten()
    }

    /// Resolve a fresh create by tag, but restore a snapshot by its captured
    /// manifest digest so a moved tag cannot change the snapshot's base.
    async fn resolve_oci_image_for_create(
        &self,
        reference: &str,
        pull_policy: PullPolicy,
        registry_overrides: RegistryOverrides,
        expected_snapshot_manifest_digest: Option<&str>,
        materialization: microsandbox_image::RootfsMaterialization,
        progress: Option<PullProgressSender>,
    ) -> MicrosandboxResult<ResolvedOciImage> {
        let Some(pinned_digest) = expected_snapshot_manifest_digest else {
            let pull_result = self
                .pull_oci_image(
                    reference,
                    pull_policy,
                    registry_overrides,
                    materialization,
                    progress,
                )
                .await?;
            return Ok(ResolvedOciImage {
                pull_result,
                metadata_reference: reference.to_string(),
                cached_metadata: None,
            });
        };

        self.resolve_snapshot_oci_image(
            reference,
            pinned_digest,
            pull_policy,
            registry_overrides,
            progress,
        )
        .await
    }

    /// Resolve the immutable image backing a snapshot from cache or registry.
    async fn resolve_snapshot_oci_image(
        &self,
        reference: &str,
        pinned_digest: &str,
        pull_policy: PullPolicy,
        registry_overrides: RegistryOverrides,
        progress: Option<PullProgressSender>,
    ) -> MicrosandboxResult<ResolvedOciImage> {
        let manifest_digest: Digest = pinned_digest.parse().map_err(|e| {
            crate::MicrosandboxError::SnapshotIntegrity(format!(
                "invalid snapshot image digest {pinned_digest}: {e}"
            ))
        })?;
        let pinned_reference = Self::digest_pinned_reference(reference, pinned_digest)?;
        let cache = GlobalCache::new_async(&self.cache_dir()).await?;

        if let Some((pull_result, metadata)) =
            Registry::pull_cached_by_manifest_digest(&cache, &manifest_digest).await?
        {
            Self::emit_cached_pull_progress(progress.as_ref(), reference, &metadata);
            return Ok(ResolvedOciImage {
                pull_result,
                metadata_reference: pinned_reference,
                cached_metadata: Some(metadata),
            });
        }

        if pull_policy == PullPolicy::Never {
            return Err(crate::MicrosandboxError::SnapshotIntegrity(format!(
                "snapshot base image {pinned_digest} is not cached locally and pull policy is `never`; \
                 this snapshot cannot be restored losslessly"
            )));
        }

        // Pull by digest, never by the mutable source tag, when the exact
        // snapshot base is absent from the local cache.
        let pull_result = match self
            .pull_oci_image(
                &pinned_reference,
                pull_policy,
                registry_overrides,
                microsandbox_image::RootfsMaterialization::Layered,
                progress,
            )
            .await
        {
            Ok(result) => result,
            Err(err) => {
                return Err(crate::MicrosandboxError::SnapshotIntegrity(format!(
                    "snapshot base image {pinned_digest} no longer available in registry \
                     (it may have been garbage-collected upstream); this snapshot \
                     cannot be restored losslessly: {err}"
                )));
            }
        };

        Ok(ResolvedOciImage {
            pull_result,
            metadata_reference: pinned_reference,
            cached_metadata: None,
        })
    }

    /// Build an immutable OCI reference using a captured manifest digest.
    fn digest_pinned_reference(reference: &str, pinned_digest: &str) -> MicrosandboxResult<String> {
        let parsed: Reference = reference.parse().map_err(|e| {
            crate::MicrosandboxError::InvalidConfig(format!("invalid image reference: {e}"))
        })?;

        Ok(Reference::with_digest(
            parsed.registry().to_string(),
            parsed.repository().to_string(),
            pinned_digest.to_string(),
        )
        .whole())
    }

    /// Emit the same progress sequence for a cache hit as a registry pull.
    fn emit_cached_pull_progress(
        progress: Option<&PullProgressSender>,
        reference: &str,
        metadata: &CachedImageMetadata,
    ) {
        let Some(sender) = progress else {
            return;
        };

        let reference: std::sync::Arc<str> = reference.to_string().into();
        sender.send(PullProgress::Resolving {
            reference: reference.clone(),
        });
        sender.send(PullProgress::Resolved {
            reference: reference.clone(),
            manifest_digest: metadata.manifest_digest.clone().into(),
            layer_count: metadata.layers.len(),
            total_download_bytes: metadata
                .layers
                .iter()
                .filter_map(|layer| layer.size_bytes)
                .reduce(|a, b| a + b),
        });
        sender.send(PullProgress::Complete {
            reference,
            layer_count: metadata.layers.len(),
        });
    }

    /// Pull an OCI image and return the pull result.
    ///
    /// Auth resolution:
    /// 1. Explicit `RegistryAuth` from `SandboxBuilder::registry_auth()` (if provided)
    /// 2. OS keyring / credential store
    /// 3. Global config `registries.auth` matched by registry hostname
    /// 4. Docker credential store/config fallback
    /// 5. Anonymous fallback
    ///
    /// When `progress` is `Some`, uses `pull_with_sender()` to emit per-layer
    /// progress events. The caller must consume the corresponding `PullProgressHandle`.
    async fn pull_oci_image(
        &self,
        reference: &str,
        pull_policy: PullPolicy,
        registry_overrides: RegistryOverrides,
        materialization: microsandbox_image::RootfsMaterialization,
        progress: Option<PullProgressSender>,
    ) -> MicrosandboxResult<PullResult> {
        let global = self.config();
        let cache = GlobalCache::new(&self.cache_dir())?;
        let platform = microsandbox_image::Platform::host_linux();
        let image_ref: Reference = reference.parse().map_err(|e| {
            crate::MicrosandboxError::InvalidConfig(format!("invalid image reference: {e}"))
        })?;
        let options = PullOptions {
            pull_policy: Self::image_pull_policy(pull_policy),
            materialization,
            ..Default::default()
        };

        // Warm runs spend most of their time outside the guest, so avoid
        // constructing the registry client when the image is already complete
        // in the local cache.
        if let Some((result, metadata)) = Registry::pull_cached(&cache, &image_ref, &options)? {
            Self::emit_cached_pull_progress(progress.as_ref(), reference, &metadata);
            return Ok(result);
        }

        let auth = match registry_overrides.auth {
            Some(auth) => auth,
            None => global.resolve_registry_auth(image_ref.registry())?,
        };

        // Merge global config with SDK overrides.
        let mut ca_certs = global.resolve_ca_certs().await?;
        ca_certs.extend(registry_overrides.ca_certs);

        let mut insecure_registries = global.insecure_registries();
        if registry_overrides.insecure {
            insecure_registries.push(image_ref.registry().to_string());
        }

        let registry = Registry::builder(platform, cache)
            .auth(auth)
            .extra_ca_certs(ca_certs)
            .add_insecure_registries(insecure_registries)
            .build()?;

        if let Some(sender) = progress {
            let task = registry.pull_with_sender(&image_ref, &options, sender);
            let result = task.await.map_err(|e| {
                crate::MicrosandboxError::Custom(format!("pull task panicked: {e}"))
            })??;
            Ok(result)
        } else {
            let result = registry.pull(&image_ref, &options).await?;
            Ok(result)
        }
    }

    /// Map the SDK pull policy onto the image crate's pull policy.
    fn image_pull_policy(policy: PullPolicy) -> microsandbox_image::PullPolicy {
        match policy {
            PullPolicy::IfMissing => microsandbox_image::PullPolicy::IfMissing,
            PullPolicy::Always => microsandbox_image::PullPolicy::Always,
            PullPolicy::Never => microsandbox_image::PullPolicy::Never,
        }
    }

    /// Validate sandbox-name-derived runtime paths for this backend.
    pub(super) fn validate_sandbox_name_for_runtime(&self, name: &str) -> MicrosandboxResult<()> {
        validate_sandbox_name(name)?;
        crate::runtime::resolve_sandbox_agent_socket_path_for(self, name).map(|_| ())
    }

    /// Validate rootfs configuration that depends on host filesystem state.
    pub(super) fn validate_rootfs_source(rootfs: &RootfsSource) -> MicrosandboxResult<()> {
        match rootfs {
            RootfsSource::Bind { path, .. } => {
                if !path.exists() {
                    return Err(crate::MicrosandboxError::InvalidConfig(format!(
                        "rootfs bind path does not exist: {}",
                        path.display()
                    )));
                }

                if !path.is_dir() {
                    return Err(crate::MicrosandboxError::InvalidConfig(format!(
                        "rootfs bind path is not a directory: {}",
                        path.display()
                    )));
                }
            }
            RootfsSource::Oci(_) => {}
            RootfsSource::DiskImage { path, .. } => {
                if !path.exists() {
                    return Err(crate::MicrosandboxError::InvalidConfig(format!(
                        "disk image does not exist: {}",
                        path.display()
                    )));
                }

                if !path.is_file() {
                    return Err(crate::MicrosandboxError::InvalidConfig(format!(
                        "disk image is not a regular file: {}",
                        path.display()
                    )));
                }
            }
        }

        Ok(())
    }

    /// Clear the way for a create: reject conflicting persisted state, or
    /// (with `.replace()`) stop and remove the prior sandbox.
    async fn prepare_create_target(
        pools: &DbPools,
        config: &SandboxConfig,
        sandbox_dir: &Path,
        run_dir: &Path,
    ) -> MicrosandboxResult<()> {
        let existing = sandbox_entity::Entity::find()
            .filter(sandbox_entity::Column::Name.eq(&config.spec.name))
            .one(pools.read())
            .await?;

        let dir_exists = sandbox_dir.exists();

        if !config.replace_existing {
            if existing.is_some() || dir_exists {
                return Err(crate::MicrosandboxError::SandboxAlreadyExists(format!(
                    "sandbox '{}' already exists; remove it, start the stopped sandbox, or recreate with .replace()",
                    config.spec.name
                )));
            }
            return Ok(());
        }

        if let Some(model) = existing {
            let sandboxes_dir = sandbox_dir.parent().ok_or_else(|| {
                crate::MicrosandboxError::InvalidConfig(format!(
                    "sandbox directory has no storage root: {}",
                    sandbox_dir.display()
                ))
            })?;
            let model = Self::reconcile_sandbox_runtime_state_with_paths(
                pools,
                model,
                Some((run_dir, sandboxes_dir)),
            )
            .await?;
            let active = matches!(
                model.status,
                SandboxStatus::Running | SandboxStatus::Draining | SandboxStatus::Paused
            );
            if active {
                Self::stop_sandbox_for_replacement(pools, &model, config.replace_with_timeout)
                    .await?;
            }

            let _guard = crate::runtime::acquire_sandbox_lifecycle_guard(
                run_dir,
                &config.spec.name,
                std::time::Duration::from_secs(5),
            )
            .await?;
            if Self::load_latest_run(pools.read(), model.id)
                .await?
                .and_then(|run| run.pid)
                .is_some_and(Self::pid_is_alive)
            {
                return Err(crate::MicrosandboxError::SandboxStillRunning(format!(
                    "cannot replace sandbox {:?}: its recorded runtime process is still alive",
                    config.spec.name
                )));
            }

            microsandbox_runtime::ipc::remove_sandbox_socket_artifacts(run_dir, &config.spec.name)?;
            remove_dir_if_exists(sandbox_dir)?;

            sandbox_entity::Entity::delete_by_id(model.id)
                .exec(pools.write())
                .await?;
            return Ok(());
        }

        let _guard = crate::runtime::acquire_sandbox_lifecycle_guard(
            run_dir,
            &config.spec.name,
            std::time::Duration::from_secs(5),
        )
        .await?;
        if sandbox_runtime_endpoint_is_live(run_dir, sandbox_dir, &config.spec.name)? {
            return Err(crate::MicrosandboxError::SandboxStillRunning(format!(
                "cannot replace sandbox {:?}: an untracked runtime endpoint is still live",
                config.spec.name
            )));
        }
        microsandbox_runtime::ipc::remove_sandbox_socket_artifacts(run_dir, &config.spec.name)?;
        remove_dir_if_exists(sandbox_dir)?;
        Ok(())
    }

    /// Stop the prior sandbox before recreating it.
    ///
    /// Sends SIGTERM with the configured grace, then escalates to SIGKILL
    /// and waits a short reap window. Single path for both same-process and
    /// foreign-process owners: SIGKILL bypasses any signal handler so the
    /// process is dead within kernel time, and the reap completes via the
    /// owning process's existing wait machinery (tokio's SIGCHLD driver
    /// when we're the parent, or the foreign parent's own `waitpid`).
    /// Replaces the previous "wait 30s and give up" behavior, which spun
    /// the full timeout when libkrun's SIGTERM handler did a slow
    /// graceful shutdown.
    async fn stop_sandbox_for_replacement(
        pools: &DbPools,
        sandbox: &sandbox_entity::Model,
        grace: std::time::Duration,
    ) -> MicrosandboxResult<()> {
        let run = Self::load_active_run(pools.read(), sandbox.id).await?;
        let pids: Vec<i32> = run
            .as_ref()
            .and_then(|model| model.pid)
            .filter(|pid| Self::pid_is_alive(*pid))
            .into_iter()
            .collect();

        if !pids.is_empty() {
            // Polite phase: SIGTERM and wait up to `grace` for graceful exit.
            if !grace.is_zero() {
                for pid in &pids {
                    let _ = Self::terminate_pid_gracefully(*pid);
                }
                Self::wait_for_pids_to_exit(&pids, grace).await;
            }

            // SIGKILL anything still alive, then prove every recorded owner
            // exited before deterministic sockets or storage can be reused.
            for pid in pids.iter().copied().filter(|p| Self::pid_is_alive(*p)) {
                if let Err(error) = Self::kill_pid(pid)
                    && Self::pid_is_alive(pid)
                {
                    return Err(error);
                }
            }
            Self::wait_for_pids_to_exit(&pids, std::time::Duration::from_secs(5)).await;
            if pids.iter().any(|pid| !Self::pid_has_exited(*pid)) {
                return Err(crate::MicrosandboxError::SandboxStillRunning(format!(
                    "cannot replace sandbox {:?}: runtime did not exit after SIGKILL",
                    sandbox.name
                )));
            }
        }

        Self::mark_sandbox_stopped_for_replacement(
            pools.write(),
            sandbox.id,
            run.as_ref().map(|model| model.id),
        )
        .await
    }

    /// Mark the replaced sandbox row (and its run, when any) stopped.
    async fn mark_sandbox_stopped_for_replacement(
        db: &DbWriteConnection,
        sandbox_id: i32,
        run_id: Option<i32>,
    ) -> MicrosandboxResult<()> {
        db.transaction(|txn| async move {
            let now = chrono::Utc::now().naive_utc();

            if let Some(run_id) = run_id {
                run_entity::Entity::update_many()
                    .col_expr(
                        run_entity::Column::Status,
                        Expr::value(run_entity::RunStatus::Terminated),
                    )
                    .col_expr(
                        run_entity::Column::TerminationReason,
                        Expr::value(run_entity::TerminationReason::Signal),
                    )
                    .col_expr(run_entity::Column::TerminatedAt, Expr::value(now))
                    .filter(run_entity::Column::Id.eq(run_id))
                    .exec(&txn)
                    .await?;
            }

            sandbox_entity::Entity::update_many()
                .col_expr(
                    sandbox_entity::Column::Status,
                    Expr::value(SandboxStatus::Stopped),
                )
                .col_expr(sandbox_entity::Column::UpdatedAt, Expr::value(now))
                .filter(sandbox_entity::Column::Id.eq(sandbox_id))
                .exec(&txn)
                .await?;

            Ok((txn, ()))
        })
        .await
    }

    /// Poll until every pid has exited or `timeout` elapses.
    async fn wait_for_pids_to_exit(pids: &[i32], timeout: std::time::Duration) {
        let start = std::time::Instant::now();
        let poll_interval = std::time::Duration::from_millis(50);

        loop {
            if pids.iter().all(|pid| Self::pid_has_exited(*pid)) {
                return;
            }

            if start.elapsed() >= timeout {
                return;
            }

            tokio::time::sleep(poll_interval).await;
        }
    }

    /// Insert the sandbox record in the database and return its ID.
    pub(super) async fn insert_sandbox_record(
        db: &DbWriteConnection,
        config: &SandboxConfig,
    ) -> MicrosandboxResult<i32> {
        let config_json = serde_json::to_string(config)?;
        let labels = config.spec.labels.clone();

        db.transaction(|txn| {
            let config_json = config_json.clone();
            let labels = labels.clone();
            async move {
                let now = chrono::Utc::now().naive_utc();
                let model = sandbox_entity::ActiveModel {
                    name: Set(config.spec.name.clone()),
                    config: Set(config_json),
                    status: Set(SandboxStatus::Running),
                    ephemeral: Set(config.spec.lifecycle.ephemeral),
                    created_at: Set(Some(now)),
                    updated_at: Set(Some(now)),
                    ..Default::default()
                };
                let result = sandbox_entity::Entity::insert(model).exec(&txn).await?;
                let sandbox_id = result.last_insert_id;
                if !labels.is_empty() {
                    sandbox_label_entity::Entity::insert_many(labels.into_iter().map(
                        |(key, value)| sandbox_label_entity::ActiveModel {
                            sandbox_id: Set(sandbox_id),
                            key: Set(key),
                            value: Set(value),
                        },
                    ))
                    .exec(&txn)
                    .await?;
                }
                Ok((txn, sandbox_id))
            }
        })
        .await
    }

    /// Delete a sandbox row by id.
    async fn delete_sandbox_record(
        db: &DbWriteConnection,
        sandbox_id: i32,
    ) -> MicrosandboxResult<()> {
        sandbox_entity::Entity::delete_by_id(sandbox_id)
            .exec(db)
            .await?;
        Ok(())
    }

    /// Pin a sandbox to its resolved OCI manifest inside a transaction.
    async fn persist_oci_manifest_pin(
        db: &DbWriteConnection,
        sandbox_id: i32,
        manifest_digest: &str,
    ) -> MicrosandboxResult<()> {
        db.transaction(|txn| async move {
            Self::replace_oci_manifest_pin(&txn, sandbox_id, manifest_digest).await?;
            Ok((txn, ()))
        })
        .await
    }

    /// Pin a sandbox to its resolved OCI manifest.
    async fn replace_oci_manifest_pin<C: ConnectionTrait>(
        db: &C,
        sandbox_id: i32,
        manifest_digest: &str,
    ) -> MicrosandboxResult<()> {
        use crate::db::entity::manifest as manifest_entity;

        let now = chrono::Utc::now().naive_utc();

        let manifest = manifest_entity::Entity::find()
            .filter(manifest_entity::Column::Digest.eq(manifest_digest))
            .one(db)
            .await?;

        let manifest_id = manifest.map(|m| m.id);

        sandbox_rootfs_entity::Entity::delete_many()
            .filter(sandbox_rootfs_entity::Column::SandboxId.eq(sandbox_id))
            .exec(db)
            .await?;

        sandbox_rootfs_entity::Entity::insert(sandbox_rootfs_entity::ActiveModel {
            sandbox_id: Set(sandbox_id),
            manifest_id: Set(manifest_id),
            mode: Set("erofs".to_string()),
            upper_fstype: Set(Some("ext4".to_string())),
            created_at: Set(Some(now)),
            ..Default::default()
        })
        .exec(db)
        .await?;

        Ok(())
    }

    /// Create a sparse ext4 image for the writable overlay upper layer.
    async fn create_upper_ext4(
        path: &std::path::Path,
        size_mib: u32,
        tree: Option<tree::FileTree>,
    ) -> MicrosandboxResult<()> {
        let _ = tokio::fs::remove_file(path).await;
        let ext4_options = ext4::Ext4FormatOptions {
            size_bytes: u64::from(size_mib) * 1024 * 1024,
            ..Default::default()
        };
        let overlay_tree = Self::build_overlay_upper_tree(tree);
        let path = path.to_path_buf();

        tokio::task::spawn_blocking(move || {
            ext4::format_ext4_with_tree(&path, &ext4_options, overlay_tree)
        })
        .await
        .map_err(|e| crate::MicrosandboxError::Custom(format!("ext4 format task failed: {e}")))?
        .map_err(|e| {
            crate::MicrosandboxError::Custom(format!("failed to create upper.ext4: {e}"))
        })?;

        Ok(())
    }

    /// Build the ext4 root directory tree that overlayfs expects.
    fn build_overlay_upper_tree(tree: Option<tree::FileTree>) -> tree::FileTree {
        use tree::{DirectoryNode, FileTree, InodeMetadata, TreeNode};

        let mut overlay_tree = FileTree::new();
        let mut upper_dir = DirectoryNode::new(InodeMetadata::default());
        let work_dir = DirectoryNode::new(InodeMetadata::default());

        if let Some(mut tree) = tree {
            upper_dir.entries = std::mem::take(&mut tree.root.entries);
        }

        overlay_tree
            .root
            .entries
            .insert("upper".into(), TreeNode::Directory(upper_dir));
        overlay_tree
            .root
            .entries
            .insert("work".into(), TreeNode::Directory(work_dir));

        overlay_tree
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

/// Probe every backward-compatible Unix endpoint before recovering an
/// untracked namespace. A successful connection is direct evidence that an
/// older runtime (which predates lifecycle locks) still owns the name.
#[cfg(unix)]
fn sandbox_runtime_endpoint_is_live(
    run_dir: &Path,
    sandbox_dir: &Path,
    name: &str,
) -> std::io::Result<bool> {
    let paths = microsandbox_runtime::ipc::sandbox_socket_paths(run_dir, name);
    let fallback_agent = sandbox_dir.join("runtime").join("agent.sock");
    let fallback_control = microsandbox_runtime::ipc::control_socket_path_for(&fallback_agent);
    for path in [
        paths.agent,
        paths.control,
        paths.legacy_agent,
        paths.legacy_control,
        fallback_agent,
        fallback_control,
    ] {
        if std::fs::symlink_metadata(&path).is_err() {
            continue;
        }
        match std::os::unix::net::UnixStream::connect(&path) {
            Ok(_) => return Ok(true),
            Err(error)
                if matches!(
                    error.kind(),
                    std::io::ErrorKind::ConnectionRefused | std::io::ErrorKind::NotFound
                ) => {}
            Err(error) => return Err(error),
        }
    }
    Ok(false)
}

#[cfg(not(unix))]
fn sandbox_runtime_endpoint_is_live(
    _run_dir: &Path,
    _sandbox_dir: &Path,
    _name: &str,
) -> std::io::Result<bool> {
    Ok(false)
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    #[cfg(unix)]
    use std::process::Command;
    use std::{
        fs,
        path::PathBuf,
        sync::Arc,
        time::{SystemTime, UNIX_EPOCH},
    };

    use microsandbox_db::entity::{run as run_entity, sandbox_rootfs as sandbox_rootfs_entity};
    use microsandbox_db::pool::DbPools;
    use microsandbox_migration::{Migrator, MigratorTrait};
    use sea_orm::{ColumnTrait, ConnectionTrait, EntityTrait, QueryFilter, Set};
    use tempfile::tempdir;

    #[cfg(unix)]
    use super::sandbox_runtime_endpoint_is_live;
    use super::{sandbox_entity, sandbox_label_entity};
    use crate::backend::{Backend, LocalBackend};
    use crate::runtime::SpawnMode;
    use crate::sandbox::{
        MAX_HOSTNAME_BYTES, MountOptions, OciRootfsSource, RootfsSource, SandboxConfig,
        SandboxStatus, VolumeMount,
    };

    /// Open both pools at `db_path` for tests, with migrations applied.
    async fn open_test_pools(db_path: &std::path::Path) -> DbPools {
        // Connect timeout matches the production default (30s). 1s was too
        // tight on cold ci runners and surfaced as `PoolTimedOut` flakes
        // before the test body had a chance to run.
        let pools = DbPools::open(
            db_path,
            1,
            std::time::Duration::from_secs(30),
            std::time::Duration::from_secs(5),
        )
        .await
        .unwrap();
        Migrator::up(pools.write().inner(), None).await.unwrap();
        pools
    }

    #[test]
    #[cfg(unix)]
    fn untracked_runtime_probe_distinguishes_live_and_stale_endpoints() {
        let temp = tempfile::Builder::new()
            .prefix("msb-untracked")
            .tempdir_in("/tmp")
            .unwrap();
        let run_dir = temp.path().join("run");
        let sandbox_dir = temp.path().join("sandboxes").join("worker");
        let paths = microsandbox_runtime::ipc::sandbox_socket_paths(&run_dir, "worker");
        std::fs::create_dir_all(paths.legacy_agent.parent().unwrap()).unwrap();
        let listener = std::os::unix::net::UnixListener::bind(&paths.legacy_agent).unwrap();

        assert!(sandbox_runtime_endpoint_is_live(&run_dir, &sandbox_dir, "worker").unwrap());
        drop(listener);
        assert!(!sandbox_runtime_endpoint_is_live(&run_dir, &sandbox_dir, "worker").unwrap());
    }

    fn test_config(name: impl Into<String>) -> SandboxConfig {
        SandboxConfig {
            spec: microsandbox_types::SandboxSpec {
                name: name.into(),
                ..Default::default()
            },
            ..Default::default()
        }
    }

    fn test_config_with_rootfs(name: impl Into<String>, image: RootfsSource) -> SandboxConfig {
        SandboxConfig {
            spec: microsandbox_types::SandboxSpec {
                name: name.into(),
                image,
                ..Default::default()
            },
            ..Default::default()
        }
    }

    fn bind_rootfs(path: impl Into<PathBuf>) -> RootfsSource {
        RootfsSource::Bind {
            path: path.into(),
            follow_root_symlinks: false,
        }
    }

    fn unique_temp_path(suffix: &str) -> PathBuf {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        std::env::temp_dir().join(format!("microsandbox-rootfs-{suffix}-{nanos}"))
    }

    fn dead_pid() -> i32 {
        let mut pid = 900_000;
        while LocalBackend::pid_is_alive(pid) {
            pid += 1;
        }
        pid
    }

    #[tokio::test]
    async fn test_runtime_name_validation_uses_explicit_backend_paths() {
        let temp = tempfile::Builder::new()
            .prefix("msb")
            .tempdir_in("/tmp")
            .unwrap();
        let home = temp.path().join("msb-home");
        let backend = LocalBackend::builder().home(&home).build().await.unwrap();

        backend
            .validate_sandbox_name_for_runtime("sdk-socket-test")
            .unwrap();
    }

    #[tokio::test]
    async fn test_create_local_validates_direct_config_mounts() {
        let temp = tempfile::Builder::new()
            .prefix("msb")
            .tempdir_in("/tmp")
            .unwrap();
        let rootfs = temp.path().join("rootfs");
        std::fs::create_dir_all(&rootfs).unwrap();
        let backend = Arc::new(
            LocalBackend::builder()
                .home(temp.path().join("home"))
                .build()
                .await
                .unwrap(),
        );
        let backend_trait: Arc<dyn Backend> = backend.clone();
        let mut config = test_config_with_rootfs("bad-mounts", bind_rootfs(rootfs));
        config.spec.mounts = vec![
            VolumeMount::Tmpfs {
                guest: "/dup".to_string(),
                size_mib: None,
                options: MountOptions::default(),
            },
            VolumeMount::Tmpfs {
                guest: "/dup".to_string(),
                size_mib: None,
                options: MountOptions::default(),
            },
        ];

        let err = match backend
            .create_sandbox(backend_trait, config, SpawnMode::Attached, None)
            .await
        {
            Ok(_) => panic!("expected invalid direct-config mounts to be rejected"),
            Err(err) => err,
        };

        assert!(
            err.to_string().contains("multiple volumes cannot mount"),
            "got: {err}"
        );
    }

    #[tokio::test]
    async fn test_create_local_rejects_invalid_hostname_before_rootfs_validation() {
        let temp = tempdir().unwrap();
        let backend = Arc::new(
            LocalBackend::builder()
                .home(temp.path())
                .build()
                .await
                .unwrap(),
        );
        let mut config = test_config_with_rootfs("test", bind_rootfs(unique_temp_path("missing")));
        config.spec.runtime.hostname = Some("y".repeat(MAX_HOSTNAME_BYTES + 1));

        let err = match backend
            .create_sandbox(backend.clone(), config, SpawnMode::Attached, None)
            .await
        {
            Ok(_) => panic!("invalid hostname should fail before sandbox creation"),
            Err(err) => err,
        };

        assert_eq!(
            err.to_string(),
            "invalid config: hostname is too long: 65 bytes (max 64)"
        );
    }

    #[test]
    fn test_validate_rootfs_source_missing_bind_path() {
        let path = unique_temp_path("missing");
        let err = LocalBackend::validate_rootfs_source(&bind_rootfs(path.clone())).unwrap_err();
        assert_eq!(
            err.to_string(),
            format!(
                "invalid config: rootfs bind path does not exist: {}",
                path.display()
            )
        );
    }

    #[test]
    fn test_validate_rootfs_source_bind_path_must_be_directory() {
        let path = unique_temp_path("file");
        fs::write(&path, b"not a directory").unwrap();

        let err = LocalBackend::validate_rootfs_source(&bind_rootfs(path.clone())).unwrap_err();
        assert_eq!(
            err.to_string(),
            format!(
                "invalid config: rootfs bind path is not a directory: {}",
                path.display()
            )
        );

        fs::remove_file(path).unwrap();
    }

    #[test]
    fn test_validate_rootfs_source_existing_bind_directory() {
        let path = unique_temp_path("dir");
        fs::create_dir(&path).unwrap();

        LocalBackend::validate_rootfs_source(&bind_rootfs(path.clone())).unwrap();

        fs::remove_dir(path).unwrap();
    }

    #[tokio::test]
    async fn test_persist_oci_manifest_pin_upserts_rootfs_record() {
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;

        let mut config = test_config_with_rootfs(
            "pinned",
            RootfsSource::Oci(OciRootfsSource {
                reference: "docker.io/library/alpine".into(),
                root_disk: None,
            }),
        );
        config.manifest_digest = Some("sha256:aaaa".into());
        let sandbox_id = LocalBackend::insert_sandbox_record(pools.write(), &config)
            .await
            .unwrap();

        // First pin (no matching manifest in DB, so manifest_id will be None).
        LocalBackend::persist_oci_manifest_pin(
            pools.write(),
            sandbox_id,
            "sha256:1111111111111111111111111111111111111111111111111111111111111111",
        )
        .await
        .unwrap();

        // Second pin replaces the first.
        LocalBackend::persist_oci_manifest_pin(
            pools.write(),
            sandbox_id,
            "sha256:2222222222222222222222222222222222222222222222222222222222222222",
        )
        .await
        .unwrap();

        let pins = sandbox_rootfs_entity::Entity::find()
            .all(pools.write())
            .await
            .unwrap();
        assert_eq!(pins.len(), 1);
        assert_eq!(pins[0].sandbox_id, sandbox_id);
        assert_eq!(pins[0].mode, "erofs");
        assert_eq!(pins[0].manifest_id, None);
    }

    #[tokio::test]
    async fn test_persist_oci_manifest_pin_replaces_stale_pin_for_different_digest() {
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;

        let mut config = test_config_with_rootfs(
            "recreated",
            RootfsSource::Oci(OciRootfsSource {
                reference: "docker.io/library/alpine".into(),
                root_disk: None,
            }),
        );
        config.manifest_digest = Some("sha256:aaaa".into());
        let sandbox_id = LocalBackend::insert_sandbox_record(pools.write(), &config)
            .await
            .unwrap();

        LocalBackend::persist_oci_manifest_pin(
            pools.write(),
            sandbox_id,
            "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        )
        .await
        .unwrap();

        // Replacing with a different digest should delete the old pin.
        LocalBackend::persist_oci_manifest_pin(
            pools.write(),
            sandbox_id,
            "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
        )
        .await
        .unwrap();

        let pins = sandbox_rootfs_entity::Entity::find()
            .all(pools.write())
            .await
            .unwrap();
        assert_eq!(pins.len(), 1);
        assert_eq!(pins[0].sandbox_id, sandbox_id);
        assert_eq!(pins[0].mode, "erofs");
        assert_eq!(pins[0].manifest_id, None);
    }

    #[tokio::test]
    async fn test_insert_sandbox_record_persists_manifest_digest_in_config_json() {
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;

        let mut config = test_config_with_rootfs(
            "persisted-digest",
            RootfsSource::Oci(OciRootfsSource {
                reference: "docker.io/library/alpine".into(),
                root_disk: None,
            }),
        );
        config.manifest_digest = Some("sha256:abc123".into());

        let sandbox_id = LocalBackend::insert_sandbox_record(pools.write(), &config)
            .await
            .unwrap();
        let row = sandbox_entity::Entity::find_by_id(sandbox_id)
            .one(pools.write())
            .await
            .unwrap()
            .unwrap();
        let decoded: SandboxConfig = serde_json::from_str(&row.config).unwrap();

        assert_eq!(decoded.manifest_digest, config.manifest_digest);
    }

    #[tokio::test]
    async fn test_insert_sandbox_record_persists_label_projection() {
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;
        let mut config = test_config("labelled");
        config.spec.labels.insert("team".into(), "metrics".into());
        config.spec.labels.insert("tier".into(), "gold".into());

        let sandbox_id = LocalBackend::insert_sandbox_record(pools.write(), &config)
            .await
            .unwrap();
        let mut rows = sandbox_label_entity::Entity::find()
            .filter(sandbox_label_entity::Column::SandboxId.eq(sandbox_id))
            .all(pools.read())
            .await
            .unwrap();
        rows.sort_by(|left, right| left.key.cmp(&right.key));

        assert_eq!(
            rows.into_iter()
                .map(|row| (row.key, row.value))
                .collect::<Vec<_>>(),
            vec![
                ("team".into(), "metrics".into()),
                ("tier".into(), "gold".into()),
            ]
        );
    }

    #[tokio::test]
    async fn test_label_rebuild_migrates_serialized_sandbox_config() {
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;
        let mut config = test_config("migration-labels");
        config.spec.labels.insert("team".into(), "metrics".into());
        config.spec.labels.insert("tier".into(), "gold".into());
        let sandbox_id = LocalBackend::insert_sandbox_record(pools.write(), &config)
            .await
            .unwrap();

        sandbox_label_entity::Entity::delete_many()
            .filter(sandbox_label_entity::Column::SandboxId.eq(sandbox_id))
            .exec(pools.write())
            .await
            .unwrap();
        pools
            .write()
            .inner()
            .execute_unprepared(
                "DELETE FROM seaql_migrations \
                 WHERE version = 'm20260810_000001_rebuild_sandbox_labels'",
            )
            .await
            .unwrap();

        Migrator::up(pools.write().inner(), None).await.unwrap();

        let mut rows = sandbox_label_entity::Entity::find()
            .filter(sandbox_label_entity::Column::SandboxId.eq(sandbox_id))
            .all(pools.read())
            .await
            .unwrap();
        rows.sort_by(|left, right| left.key.cmp(&right.key));
        assert_eq!(
            rows.into_iter()
                .map(|row| (row.key, row.value))
                .collect::<Vec<_>>(),
            vec![
                ("team".into(), "metrics".into()),
                ("tier".into(), "gold".into()),
            ]
        );
    }

    #[tokio::test]
    async fn test_prepare_create_target_rejects_existing_state_without_force() {
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;

        let sandbox_dir = temp.path().join("sandboxes").join("existing");
        fs::create_dir_all(&sandbox_dir).unwrap();

        let config = test_config("existing");

        let run_dir = temp.path().join("run");
        let err = LocalBackend::prepare_create_target(&pools, &config, &sandbox_dir, &run_dir)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }

    #[tokio::test]
    async fn test_prepare_create_target_force_replaces_stopped_sandbox_state() {
        #[cfg(unix)]
        let temp = tempfile::Builder::new()
            .prefix("msb-replace")
            .tempdir_in("/tmp")
            .unwrap();
        #[cfg(not(unix))]
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;

        let sandbox_dir = temp.path().join("sandboxes").join("replaceable");
        fs::create_dir_all(sandbox_dir.join("rw")).unwrap();
        let config = test_config("replaceable");
        let sandbox_id = LocalBackend::insert_sandbox_record(pools.write(), &config)
            .await
            .unwrap();
        LocalBackend::update_sandbox_status(pools.write(), sandbox_id, SandboxStatus::Stopped)
            .await
            .unwrap();

        let mut forced = test_config("replaceable");
        forced.replace_existing = true;

        let run_dir = temp.path().join("run");
        #[cfg(unix)]
        let socket_paths = {
            let paths = microsandbox_runtime::ipc::sandbox_socket_paths(&run_dir, "replaceable");
            fs::create_dir_all(&paths.canonical_dir).unwrap();
            fs::write(&paths.agent, b"stale").unwrap();
            fs::write(&paths.control, b"stale").unwrap();
            microsandbox_runtime::ipc::publish_legacy_agent_link(
                &run_dir,
                "replaceable",
                &paths.agent,
            )
            .unwrap();
            microsandbox_runtime::ipc::publish_legacy_control_link(
                &run_dir,
                "replaceable",
                &paths.control,
            )
            .unwrap();
            paths
        };
        LocalBackend::prepare_create_target(&pools, &forced, &sandbox_dir, &run_dir)
            .await
            .unwrap();

        assert!(!sandbox_dir.exists());
        #[cfg(unix)]
        for path in [
            &socket_paths.agent,
            &socket_paths.control,
            &socket_paths.legacy_agent,
            &socket_paths.legacy_control,
            &socket_paths.canonical_dir,
        ] {
            assert!(std::fs::symlink_metadata(path).is_err());
        }
        assert!(
            sandbox_entity::Entity::find_by_id(sandbox_id)
                .one(pools.write())
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn test_prepare_create_target_force_replaces_stale_running_sandbox_state() {
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;

        let sandbox_dir = temp.path().join("sandboxes").join("stale-running");
        fs::create_dir_all(sandbox_dir.join("rw")).unwrap();
        let config = test_config("stale-running");
        let sandbox_id = LocalBackend::insert_sandbox_record(pools.write(), &config)
            .await
            .unwrap();

        let run = run_entity::ActiveModel {
            sandbox_id: Set(sandbox_id),
            pid: Set(Some(dead_pid())),
            status: Set(run_entity::RunStatus::Running),
            ..Default::default()
        };
        run_entity::Entity::insert(run)
            .exec(pools.write())
            .await
            .unwrap();

        let mut forced = test_config("stale-running");
        forced.replace_existing = true;

        let run_dir = temp.path().join("run");
        LocalBackend::prepare_create_target(&pools, &forced, &sandbox_dir, &run_dir)
            .await
            .unwrap();

        assert!(!sandbox_dir.exists());
        assert!(
            sandbox_entity::Entity::find_by_id(sandbox_id)
                .one(pools.write())
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    #[cfg(unix)]
    async fn test_prepare_create_target_force_replaces_running_sandbox() {
        let temp = tempdir().unwrap();
        let db_path = temp.path().join("test.db");
        let pools = open_test_pools(&db_path).await;

        let sandbox_dir = temp.path().join("sandboxes").join("running");
        fs::create_dir_all(&sandbox_dir).unwrap();
        let config = test_config("running");
        let sandbox_id = LocalBackend::insert_sandbox_record(pools.write(), &config)
            .await
            .unwrap();

        let child = Command::new("sleep").arg("30").spawn().unwrap();
        let live_pid = child.id() as i32;
        let waiter = std::thread::spawn(move || {
            let mut child = child;
            child.wait().unwrap()
        });
        let run = run_entity::ActiveModel {
            sandbox_id: Set(sandbox_id),
            pid: Set(Some(live_pid)),
            status: Set(run_entity::RunStatus::Running),
            ..Default::default()
        };
        run_entity::Entity::insert(run)
            .exec(pools.write())
            .await
            .unwrap();

        let mut forced = test_config("running");
        forced.replace_existing = true;

        let run_dir = temp.path().join("run");
        LocalBackend::prepare_create_target(&pools, &forced, &sandbox_dir, &run_dir)
            .await
            .unwrap();

        waiter.join().unwrap();

        assert!(!LocalBackend::pid_is_alive(live_pid));
        assert!(!sandbox_dir.exists());
        assert!(
            sandbox_entity::Entity::find_by_id(sandbox_id)
                .one(pools.write())
                .await
                .unwrap()
                .is_none()
        );
    }
}