sim-logic 0.1.0

A code-first application and logic layer for Sim;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
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
//! Bounded managed structural, World-transition, and application-control commands.

use std::{
    collections::{HashMap, HashSet},
    error::Error,
    fmt,
};

use bevy_ecs::{
    bundle::{Bundle, NoBundleEffect},
    component::ComponentId,
    entity::Entity,
    entity_disabling::Disabled,
    prelude::{ResMut, Resource},
    system::SystemParam,
    world::World,
};

use crate::{
    component::{ApprovedComponents, ComponentApprovalError},
    identity::{
        ApplicationId, LogicEntity, ManagedEntity, TransitionIntentToken, WorldFactoryId,
        WorldGeneration,
    },
    transition::TransitionRequest,
    visual::{ActiveCamera2d, Transform2d},
};

/// Failure to create an intent or request another bounded stage command.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CommandEnqueueError {
    /// The configured command-count limit has been reached.
    LimitExceeded {
        /// Frozen maximum command count for one stage.
        limit: usize,
    },
    /// The queue could not reserve storage for one more command.
    AllocationFailed,
    /// The direct intent occurrence counter is exhausted.
    IntentIdentityExhausted,
    /// Direct intents are unavailable outside an active application frame.
    IntentUnavailable,
    /// Application exit cannot be requested during isolated World Startup.
    ExitUnavailable,
    /// Fixed simulation pause cannot be changed during isolated World Startup.
    PauseUnavailable,
}

impl fmt::Display for CommandEnqueueError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LimitExceeded { limit } => {
                write!(formatter, "stage command limit of {limit} was exceeded")
            }
            Self::AllocationFailed => {
                formatter.write_str("failed to reserve storage for a stage command")
            }
            Self::IntentIdentityExhausted => {
                formatter.write_str("direct transition intent identity space is exhausted")
            }
            Self::IntentUnavailable => {
                formatter.write_str("direct transition intents are unavailable in this stage")
            }
            Self::ExitUnavailable => {
                formatter.write_str("application exit is unavailable during World Startup")
            }
            Self::PauseUnavailable => {
                formatter.write_str("fixed simulation pause is unavailable during World Startup")
            }
        }
    }
}

impl Error for CommandEnqueueError {}

/// Reason an entire structural stage batch was rejected before entity mutation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CommandBatchError {
    /// At least one System attempted to exceed the configured command limit.
    LimitExceeded {
        /// Frozen maximum command count for one stage.
        limit: usize,
    },
    /// Command queue storage could not be reserved.
    AllocationFailed,
    /// A structural-command target belongs to another application or World generation.
    ForeignEntity {
        /// Rejected opaque entity handle.
        entity: LogicEntity,
    },
    /// A structural-command target no longer names a live entity.
    MissingEntity {
        /// Rejected opaque entity handle.
        entity: LogicEntity,
    },
    /// The same entity was scheduled for despawn more than once.
    DuplicateDespawn {
        /// Duplicated opaque entity handle.
        entity: LogicEntity,
    },
    /// One stage batch attempted to structurally change and despawn the same entity.
    ConflictingEntityCommands {
        /// Conflicted opaque entity handle.
        entity: LogicEntity,
    },
    /// Applying every spawn would exceed the active-entity limit.
    EntityLimitExceeded {
        /// Frozen maximum live entity count.
        limit: usize,
        /// Live entity count the batch would produce.
        requested: usize,
    },
    /// A retained component would lose one of its registered required components.
    RequiredComponentWouldBeMissing {
        /// Rejected opaque entity handle.
        entity: LogicEntity,
        /// Diagnostic name of the component that would be absent.
        required: String,
        /// Diagnostic name of the retained component that requires it.
        required_by: String,
    },
    /// A structural bundle fell outside the frozen approved component set.
    Component(ComponentApprovalError),
}

impl fmt::Display for CommandBatchError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LimitExceeded { limit } => {
                write!(formatter, "stage command limit of {limit} was exceeded")
            }
            Self::AllocationFailed => {
                formatter.write_str("failed to reserve command validation storage")
            }
            Self::ForeignEntity { entity } => {
                write!(formatter, "entity {entity:?} belongs to another World")
            }
            Self::MissingEntity { entity } => {
                write!(formatter, "entity {entity:?} is not alive")
            }
            Self::DuplicateDespawn { entity } => {
                write!(
                    formatter,
                    "entity {entity:?} was scheduled for despawn twice"
                )
            }
            Self::ConflictingEntityCommands { entity } => write!(
                formatter,
                "entity {entity:?} cannot be structurally changed and despawned in one stage"
            ),
            Self::EntityLimitExceeded { limit, requested } => write!(
                formatter,
                "structural batch would create {requested} live entities, above limit {limit}"
            ),
            Self::RequiredComponentWouldBeMissing {
                entity,
                required,
                required_by,
            } => write!(
                formatter,
                "entity {entity:?} would retain {required_by} without required component {required}"
            ),
            Self::Component(error) => write!(formatter, "structural bundle was rejected: {error}"),
        }
    }
}

impl Error for CommandBatchError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Component(error) => Some(error),
            _ => None,
        }
    }
}

trait ErasedSpawn: Send + Sync + 'static {
    fn validate(
        &self,
        world: &mut World,
        approved: &mut ApprovedComponents,
    ) -> Result<(), ComponentApprovalError>;

    fn apply(self: Box<Self>, world: &mut World, generation: WorldGeneration);
}

trait ErasedInsert: Send + Sync + 'static {
    fn validate(
        &self,
        world: &mut World,
        approved: &mut ApprovedComponents,
    ) -> Result<(), ComponentApprovalError>;

    fn record_presence(
        &self,
        world: &mut World,
        entity: Entity,
        overrides: &mut PresenceOverrides,
        inserted_components: &mut Vec<(Entity, ComponentId)>,
    ) -> Result<(), CommandBatchError>;

    fn apply(self: Box<Self>, world: &mut World, entity: Entity);
}

type PresenceOverrides = HashMap<(Entity, ComponentId), bool>;
type RemoveValidation =
    fn(&mut World, &mut ApprovedComponents) -> Result<(), ComponentApprovalError>;
type PresenceRecorder = fn(
    &mut World,
    Entity,
    &mut PresenceOverrides,
    &mut Vec<(Entity, ComponentId)>,
) -> Result<(), CommandBatchError>;
type RemoveApply = fn(&mut World, Entity);

#[derive(Clone, Copy)]
struct RemoveDescriptor {
    validate: RemoveValidation,
    record_presence: PresenceRecorder,
    apply: RemoveApply,
}

impl RemoveDescriptor {
    const fn new<B>() -> Self
    where
        B: Bundle<Effect: NoBundleEffect>,
    {
        Self {
            validate: validate_remove_bundle::<B>,
            record_presence: record_removed_bundle::<B>,
            apply: apply_remove_bundle::<B>,
        }
    }
}

fn validate_remove_bundle<B>(
    world: &mut World,
    approved: &mut ApprovedComponents,
) -> Result<(), ComponentApprovalError>
where
    B: Bundle<Effect: NoBundleEffect>,
{
    approved.validate_bundle::<B>(world)
}

fn record_removed_bundle<B>(
    world: &mut World,
    entity: Entity,
    overrides: &mut PresenceOverrides,
    _inserted_components: &mut Vec<(Entity, ComponentId)>,
) -> Result<(), CommandBatchError>
where
    B: Bundle<Effect: NoBundleEffect>,
{
    let bundle = world.register_bundle::<B>();
    overrides
        .try_reserve(bundle.explicit_components().len())
        .map_err(|_| CommandBatchError::AllocationFailed)?;
    for component in bundle.explicit_components() {
        overrides.insert((entity, *component), false);
    }
    Ok(())
}

fn apply_remove_bundle<B>(world: &mut World, entity: Entity)
where
    B: Bundle<Effect: NoBundleEffect>,
{
    world.entity_mut(entity).remove::<B>();
}

struct TypedSpawn<B>(B);

impl<B> ErasedSpawn for TypedSpawn<B>
where
    B: Bundle<Effect: NoBundleEffect>,
{
    fn validate(
        &self,
        world: &mut World,
        approved: &mut ApprovedComponents,
    ) -> Result<(), ComponentApprovalError> {
        approved.validate_bundle::<B>(world)
    }

    fn apply(self: Box<Self>, world: &mut World, generation: WorldGeneration) {
        let _identity = apply_typed_spawn(world, self.0, generation);
    }
}

struct TypedSpawnCopies<B> {
    bundle: B,
    count: usize,
}

impl<B> ErasedSpawn for TypedSpawnCopies<B>
where
    B: Bundle<Effect: NoBundleEffect> + Copy,
{
    fn validate(
        &self,
        world: &mut World,
        approved: &mut ApprovedComponents,
    ) -> Result<(), ComponentApprovalError> {
        approved.validate_bundle::<B>(world)
    }

    fn apply(self: Box<Self>, world: &mut World, generation: WorldGeneration) {
        for _ in 0..self.count {
            let _identity = apply_typed_spawn(world, self.bundle, generation);
        }
    }
}

fn apply_typed_spawn<B>(world: &mut World, bundle: B, generation: WorldGeneration) -> LogicEntity
where
    B: Bundle<Effect: NoBundleEffect>,
{
    let managed = ManagedEntity::for_generation(generation);
    let entity = world.spawn((bundle, managed)).id();
    let identity = managed.handle(entity);
    let mut entity = world.entity_mut(entity);
    if let Some(mut transform) = entity.get_mut::<Transform2d>() {
        transform.snap_interpolation();
    }
    if let Some(mut camera) = entity.get_mut::<ActiveCamera2d>() {
        camera.snap_interpolation();
    }
    identity
}

struct TypedInsert<B>(B);

impl<B> ErasedInsert for TypedInsert<B>
where
    B: Bundle<Effect: NoBundleEffect>,
{
    fn validate(
        &self,
        world: &mut World,
        approved: &mut ApprovedComponents,
    ) -> Result<(), ComponentApprovalError> {
        approved.validate_bundle::<B>(world)
    }

    fn record_presence(
        &self,
        world: &mut World,
        entity: Entity,
        overrides: &mut PresenceOverrides,
        inserted_components: &mut Vec<(Entity, ComponentId)>,
    ) -> Result<(), CommandBatchError> {
        let bundle = world.register_bundle::<B>();
        overrides
            .try_reserve(bundle.contributed_components().len())
            .map_err(|_| CommandBatchError::AllocationFailed)?;
        inserted_components
            .try_reserve(bundle.contributed_components().len())
            .map_err(|_| CommandBatchError::AllocationFailed)?;
        for component in bundle.contributed_components() {
            overrides.insert((entity, *component), true);
            inserted_components.push((entity, *component));
        }
        Ok(())
    }

    fn apply(self: Box<Self>, world: &mut World, entity: Entity) {
        let transform = world.component_id::<Transform2d>();
        let camera = world.component_id::<ActiveCamera2d>();
        let (explicit_transform, required_transform, explicit_camera, required_camera) = {
            let info = world.register_bundle::<B>();
            (
                transform.is_some_and(|id| info.explicit_components().contains(&id)),
                transform.is_some_and(|id| info.required_components().contains(&id)),
                camera.is_some_and(|id| info.explicit_components().contains(&id)),
                camera.is_some_and(|id| info.required_components().contains(&id)),
            )
        };
        let target = world.entity(entity);
        let snap_transform = explicit_transform
            || required_transform && transform.is_some_and(|id| !target.contains_id(id));
        let snap_camera =
            explicit_camera || required_camera && camera.is_some_and(|id| !target.contains_id(id));

        let mut target = world.entity_mut(entity);
        target.insert(self.0);
        if snap_transform && let Some(mut transform) = target.get_mut::<Transform2d>() {
            transform.snap_interpolation();
        }
        if snap_camera && let Some(mut camera) = target.get_mut::<ActiveCamera2d>() {
            camera.snap_interpolation();
        }
    }
}

enum QueuedCommand {
    Spawn {
        count: usize,
        operation: Box<dyn ErasedSpawn>,
    },
    Insert {
        entity: LogicEntity,
        bundle: Box<dyn ErasedInsert>,
    },
    Remove {
        entity: LogicEntity,
        descriptor: RemoveDescriptor,
    },
    Despawn(LogicEntity),
    ReplaceWorld(TransitionRequest),
    SetPaused(bool),
    Exit,
}

impl QueuedCommand {
    fn spawn<B>(bundle: B) -> Self
    where
        B: Bundle<Effect: NoBundleEffect>,
    {
        Self::Spawn {
            count: 1,
            operation: Box::new(TypedSpawn(bundle)),
        }
    }
}

#[derive(Default)]
struct CommandCounts {
    spawns: usize,
    removals: usize,
    despawns: usize,
    transitions: usize,
    pause: Option<bool>,
    exit_requested: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum QueuePoison {
    LimitExceeded,
    AllocationFailed,
}

#[derive(Resource)]
pub(crate) struct CommandQueue {
    commands: Vec<QueuedCommand>,
    used_slots: usize,
    despawns: HashSet<LogicEntity>,
    component_targets: HashSet<Entity>,
    component_target_order: Vec<Entity>,
    inserted_components: Vec<(Entity, ComponentId)>,
    presence_overrides: PresenceOverrides,
    limit: usize,
    poison: Option<QueuePoison>,
}

impl CommandQueue {
    pub(crate) fn new(limit: usize) -> Self {
        Self {
            commands: Vec::new(),
            used_slots: 0,
            despawns: HashSet::new(),
            component_targets: HashSet::new(),
            component_target_order: Vec::new(),
            inserted_components: Vec::new(),
            presence_overrides: HashMap::new(),
            limit,
            poison: None,
        }
    }

    fn push(&mut self, command: QueuedCommand) -> Result<(), CommandEnqueueError> {
        self.push_weighted(command, 1)
    }

    fn push_weighted(
        &mut self,
        command: QueuedCommand,
        slots: usize,
    ) -> Result<(), CommandEnqueueError> {
        let requested = self.prepare_push(slots)?;
        self.commands.push(command);
        self.used_slots = requested;
        Ok(())
    }

    fn push_spawn_copies<B>(&mut self, bundle: B, count: usize) -> Result<(), CommandEnqueueError>
    where
        B: Bundle<Effect: NoBundleEffect> + Copy,
    {
        let requested = self.prepare_push(count)?;
        self.commands.push(QueuedCommand::Spawn {
            count,
            operation: Box::new(TypedSpawnCopies { bundle, count }),
        });
        self.used_slots = requested;
        Ok(())
    }

    fn prepare_push(&mut self, slots: usize) -> Result<usize, CommandEnqueueError> {
        debug_assert_ne!(slots, 0);
        let Some(requested) = self.used_slots.checked_add(slots) else {
            self.poison = Some(QueuePoison::LimitExceeded);
            return Err(CommandEnqueueError::LimitExceeded { limit: self.limit });
        };
        if requested > self.limit {
            self.poison = Some(QueuePoison::LimitExceeded);
            return Err(CommandEnqueueError::LimitExceeded { limit: self.limit });
        }
        if self.commands.try_reserve(1).is_err() {
            self.poison = Some(QueuePoison::AllocationFailed);
            return Err(CommandEnqueueError::AllocationFailed);
        }
        Ok(requested)
    }

    pub(crate) fn transition_requests(&self) -> impl Iterator<Item = TransitionRequest> + '_ {
        self.commands.iter().filter_map(|command| match command {
            QueuedCommand::ReplaceWorld(request) => Some(*request),
            QueuedCommand::Spawn { .. }
            | QueuedCommand::Insert { .. }
            | QueuedCommand::Remove { .. }
            | QueuedCommand::Despawn(_)
            | QueuedCommand::SetPaused(_)
            | QueuedCommand::Exit => None,
        })
    }

    pub(crate) fn discard(&mut self) {
        self.clear_reusing_storage();
    }

    pub(crate) fn validate_and_apply(
        &mut self,
        world: &mut World,
        approved: &mut ApprovedComponents,
        application: ApplicationId,
        generation: WorldGeneration,
        managed_entities: usize,
        entity_limit: usize,
    ) -> Result<CommittedCommands, CommandCommitError> {
        let result = self.validate_and_apply_inner(
            world,
            approved,
            application,
            generation,
            managed_entities,
            entity_limit,
        );
        self.clear_reusing_storage();
        result
    }

    fn validate_and_apply_inner(
        &mut self,
        world: &mut World,
        approved: &mut ApprovedComponents,
        application: ApplicationId,
        generation: WorldGeneration,
        managed_entities: usize,
        entity_limit: usize,
    ) -> Result<CommittedCommands, CommandCommitError> {
        match self.poison {
            Some(QueuePoison::LimitExceeded) => {
                return Err(CommandBatchError::LimitExceeded { limit: self.limit }.into());
            }
            Some(QueuePoison::AllocationFailed) => {
                return Err(CommandBatchError::AllocationFailed.into());
            }
            None => {}
        }

        let counts = self
            .commands
            .iter()
            .fold(CommandCounts::default(), |mut counts, command| {
                match command {
                    QueuedCommand::Spawn { count, .. } => counts.spawns += count,
                    QueuedCommand::Insert { .. } => {}
                    QueuedCommand::Remove { .. } => counts.removals += 1,
                    QueuedCommand::Despawn(_) => counts.despawns += 1,
                    QueuedCommand::ReplaceWorld(_) => counts.transitions += 1,
                    QueuedCommand::SetPaused(paused) => counts.pause = Some(*paused),
                    QueuedCommand::Exit => counts.exit_requested = true,
                }
                counts
            });

        if counts.despawns != 0 {
            self.despawns
                .try_reserve(counts.despawns)
                .map_err(|_| CommandBatchError::AllocationFailed)?;

            for command in &self.commands {
                if let QueuedCommand::Despawn(entity) = command {
                    validate_target(world, application, generation, *entity)?;
                    if !self.despawns.insert(*entity) {
                        return Err(CommandBatchError::DuplicateDespawn { entity: *entity }.into());
                    }
                }
            }
        }

        for command in &self.commands {
            match command {
                QueuedCommand::Spawn { operation, .. } => operation
                    .validate(world, approved)
                    .map_err(CommandBatchError::Component)?,
                QueuedCommand::Insert { entity, bundle } => {
                    validate_target(world, application, generation, *entity)?;
                    if self.despawns.contains(entity) {
                        return Err(CommandBatchError::ConflictingEntityCommands {
                            entity: *entity,
                        }
                        .into());
                    }
                    bundle
                        .validate(world, approved)
                        .map_err(CommandBatchError::Component)?;
                }
                QueuedCommand::Remove { entity, descriptor } => {
                    validate_target(world, application, generation, *entity)?;
                    if self.despawns.contains(entity) {
                        return Err(CommandBatchError::ConflictingEntityCommands {
                            entity: *entity,
                        }
                        .into());
                    }
                    (descriptor.validate)(world, approved).map_err(CommandBatchError::Component)?;
                }
                QueuedCommand::Despawn(_) => {}
                QueuedCommand::ReplaceWorld(_) => {}
                QueuedCommand::SetPaused(_) => {}
                QueuedCommand::Exit => {}
            }
        }

        if counts.removals != 0 {
            self.plan_component_presence(world, counts.removals)?;
            validate_required_components(
                world,
                application,
                generation,
                &self.component_target_order,
                &self.inserted_components,
                &self.presence_overrides,
            )?;
        }

        let retained = managed_entities
            .checked_sub(counts.despawns)
            .ok_or(CommandCommitError::RuntimeInvariant)?;
        let requested =
            retained
                .checked_add(counts.spawns)
                .ok_or(CommandBatchError::EntityLimitExceeded {
                    limit: entity_limit,
                    requested: usize::MAX,
                })?;
        if requested > entity_limit {
            return Err(CommandBatchError::EntityLimitExceeded {
                limit: entity_limit,
                requested,
            }
            .into());
        }

        let mut spawned = 0;
        let mut despawned = 0;
        let mut transitions = Vec::new();
        transitions
            .try_reserve(counts.transitions)
            .map_err(|_| CommandBatchError::AllocationFailed)?;

        for command in self.commands.drain(..) {
            match command {
                QueuedCommand::Spawn { count, operation } => {
                    operation.apply(world, generation);
                    spawned += count;
                }
                QueuedCommand::Insert { entity, bundle } => {
                    bundle.apply(world, entity.entity());
                }
                QueuedCommand::Remove { entity, descriptor } => {
                    (descriptor.apply)(world, entity.entity());
                }
                QueuedCommand::Despawn(entity) => {
                    let removed = world.despawn(entity.entity());
                    debug_assert!(
                        removed,
                        "validated entity disappeared during command commit"
                    );
                    despawned += 1;
                }
                QueuedCommand::ReplaceWorld(request) => transitions.push(request),
                QueuedCommand::SetPaused(_) => {}
                QueuedCommand::Exit => {}
            }
        }

        Ok(CommittedCommands {
            spawned,
            despawned,
            live_entities: requested,
            transitions,
            pause: counts.pause,
            exit_requested: counts.exit_requested,
        })
    }

    fn plan_component_presence(
        &mut self,
        world: &mut World,
        removal_count: usize,
    ) -> Result<(), CommandBatchError> {
        debug_assert_ne!(removal_count, 0);
        self.component_targets
            .try_reserve(removal_count)
            .map_err(|_| CommandBatchError::AllocationFailed)?;
        self.component_target_order
            .try_reserve(removal_count)
            .map_err(|_| CommandBatchError::AllocationFailed)?;

        for command in &self.commands {
            if let QueuedCommand::Remove { entity, .. } = command {
                let raw = entity.entity();
                if self.component_targets.insert(raw) {
                    self.component_target_order.push(raw);
                }
            }
        }

        for command in &self.commands {
            match command {
                QueuedCommand::Insert { entity, bundle }
                    if self.component_targets.contains(&entity.entity()) =>
                {
                    let raw = entity.entity();
                    bundle.record_presence(
                        world,
                        raw,
                        &mut self.presence_overrides,
                        &mut self.inserted_components,
                    )?;
                }
                QueuedCommand::Remove { entity, descriptor } => {
                    let raw = entity.entity();
                    (descriptor.record_presence)(
                        world,
                        raw,
                        &mut self.presence_overrides,
                        &mut self.inserted_components,
                    )?;
                }
                QueuedCommand::Insert { .. }
                | QueuedCommand::Spawn { .. }
                | QueuedCommand::Despawn(_)
                | QueuedCommand::ReplaceWorld(_)
                | QueuedCommand::SetPaused(_)
                | QueuedCommand::Exit => {}
            }
        }
        Ok(())
    }

    fn clear_reusing_storage(&mut self) {
        self.commands.clear();
        self.used_slots = 0;
        self.despawns.clear();
        self.component_targets.clear();
        self.component_target_order.clear();
        self.inserted_components.clear();
        self.presence_overrides.clear();
        self.poison = None;
    }

    #[cfg(test)]
    fn command_capacity(&self) -> usize {
        self.commands.capacity()
    }

    #[cfg(test)]
    fn despawn_capacity(&self) -> usize {
        self.despawns.capacity()
    }

    #[cfg(test)]
    fn component_target_capacity(&self) -> usize {
        self.component_targets.capacity()
    }

    #[cfg(test)]
    fn presence_override_capacity(&self) -> usize {
        self.presence_overrides.capacity()
    }

    #[cfg(test)]
    fn is_clean(&self) -> bool {
        self.commands.is_empty()
            && self.used_slots == 0
            && self.despawns.is_empty()
            && self.component_targets.is_empty()
            && self.component_target_order.is_empty()
            && self.inserted_components.is_empty()
            && self.presence_overrides.is_empty()
            && self.poison.is_none()
    }
}

fn validate_required_components(
    world: &World,
    application: ApplicationId,
    generation: WorldGeneration,
    component_targets: &[Entity],
    inserted_components: &[(Entity, ComponentId)],
    overrides: &PresenceOverrides,
) -> Result<(), CommandBatchError> {
    for entity in component_targets {
        let target = world.entity(*entity);
        for component in target.archetype().iter_components() {
            if final_component_presence(world, *entity, component, overrides) {
                validate_retained_requirements(
                    world,
                    application,
                    generation,
                    *entity,
                    component,
                    overrides,
                )?;
            }
        }
    }

    for &(entity, component) in inserted_components {
        if final_component_presence(world, entity, component, overrides) {
            validate_retained_requirements(
                world,
                application,
                generation,
                entity,
                component,
                overrides,
            )?;
        }
    }
    Ok(())
}

fn validate_retained_requirements(
    world: &World,
    application: ApplicationId,
    generation: WorldGeneration,
    entity: Entity,
    component: ComponentId,
    overrides: &PresenceOverrides,
) -> Result<(), CommandBatchError> {
    let Some(required) = world.get_required_components_by_id(component) else {
        return Ok(());
    };
    let Some(missing) = required
        .iter_ids()
        .find(|required| !final_component_presence(world, entity, *required, overrides))
    else {
        return Ok(());
    };

    Err(CommandBatchError::RequiredComponentWouldBeMissing {
        entity: LogicEntity::new(application, generation, entity),
        required: component_diagnostic_name(world, missing),
        required_by: component_diagnostic_name(world, component),
    })
}

fn final_component_presence(
    world: &World,
    entity: Entity,
    component: ComponentId,
    overrides: &PresenceOverrides,
) -> bool {
    overrides
        .get(&(entity, component))
        .copied()
        .unwrap_or_else(|| world.entity(entity).contains_id(component))
}

fn component_diagnostic_name(world: &World, component: ComponentId) -> String {
    world
        .components()
        .get_info(component)
        .map(|info| info.name().to_string())
        .unwrap_or_else(|| format!("{component:?}"))
}

fn validate_target(
    world: &World,
    application: ApplicationId,
    generation: WorldGeneration,
    entity: LogicEntity,
) -> Result<(), CommandBatchError> {
    if entity.application() != application || entity.world_generation() != generation {
        return Err(CommandBatchError::ForeignEntity { entity });
    }
    if !world
        .get::<ManagedEntity>(entity.entity())
        .is_some_and(|managed| managed.handle(entity.entity()) == entity)
    {
        return Err(CommandBatchError::MissingEntity { entity });
    }
    Ok(())
}

#[derive(Resource)]
pub(crate) struct DirectIntentIssuer {
    application: ApplicationId,
    generation: WorldGeneration,
    frame: u64,
    next_occurrence: u64,
    enabled: bool,
}

impl DirectIntentIssuer {
    pub(crate) const fn new(application: ApplicationId, generation: WorldGeneration) -> Self {
        Self {
            application,
            generation,
            frame: 0,
            next_occurrence: 1,
            enabled: false,
        }
    }

    pub(crate) fn set_frame(&mut self, frame: u64, generation: WorldGeneration) {
        self.frame = frame;
        self.generation = generation;
        self.enabled = true;
    }

    pub(crate) fn disable(&mut self) {
        self.enabled = false;
    }

    fn issue(&mut self) -> Result<TransitionIntentToken, CommandEnqueueError> {
        if !self.enabled {
            return Err(CommandEnqueueError::IntentUnavailable);
        }
        let occurrence = self.next_occurrence;
        self.next_occurrence = self
            .next_occurrence
            .checked_add(1)
            .ok_or(CommandEnqueueError::IntentIdentityExhausted)?;
        Ok(TransitionIntentToken::direct(
            self.application,
            self.generation,
            self.frame,
            occurrence,
        ))
    }

    const fn application_control_is_available(&self) -> bool {
        self.enabled
    }
}

/// Managed, stage-deferred command access for ordinary Sim;Logic Systems.
///
/// Commands are retained in deterministic call order and become visible only
/// after the current stage passes whole-batch validation.
#[derive(SystemParam)]
pub struct LogicCommands<'w> {
    queue: ResMut<'w, CommandQueue>,
    intents: ResMut<'w, DirectIntentIssuer>,
}

impl LogicCommands<'_> {
    /// Queues one approved, side-effect-free bundle for deferred spawn.
    ///
    /// Approved required components are materialized at the stage barrier. An
    /// explicit value in `bundle` takes precedence over its required default.
    ///
    /// No entity handle is returned. The runtime assigns it at the stage
    /// barrier after the complete batch succeeds.
    pub fn spawn<B>(&mut self, bundle: B) -> Result<(), CommandEnqueueError>
    where
        B: Bundle<Effect: NoBundleEffect>,
    {
        self.queue.push(QueuedCommand::spawn(bundle))
    }

    /// Queues `count` copies of one approved bundle for consecutive deferred spawn.
    ///
    /// Every eventual entity consumes one slot from the bounded stage command
    /// budget even though the runtime retains only one erased bundle template.
    /// A request that does not fit poisons the complete stage batch just like
    /// repeated [`Self::spawn`] calls that cross the limit, and no prefix from
    /// this call is retained. At the stage barrier the bundle type is validated
    /// once, the entity limit is checked for every copy before mutation, and
    /// then distinct managed entities are created consecutively at this
    /// command's position in queue order.
    ///
    /// `count == 0` is an immediate successful no-op: it occupies no command
    /// slot, retains no value, and does not validate or register `B`. Required
    /// components, explicit-over-required precedence, Disabled accounting, and
    /// Transform or camera interpolation snapping otherwise match singular
    /// spawn exactly. No handles are returned because identities do not become
    /// live until the stage barrier.
    ///
    /// `Copy` deliberately excludes user-defined cloning work and destructors.
    /// Application remains linear in `count`; this method does not promise a
    /// native bulk ECS insertion.
    pub fn spawn_copies<B>(&mut self, bundle: B, count: usize) -> Result<(), CommandEnqueueError>
    where
        B: Bundle<Effect: NoBundleEffect> + Copy,
    {
        if count == 0 {
            return Ok(());
        }
        self.queue.push_spawn_copies(bundle, count)
    }

    /// Queues approved components to be added to or replaced on one entity.
    ///
    /// Existing explicit component types are replaced. Required components are
    /// materialized only when absent. The target and complete bundle are
    /// validated at the stage barrier before any structural command is
    /// applied. Inserting into and despawning the same entity in one stage is
    /// a batch conflict regardless of enqueue order.
    ///
    /// Multiple inserts apply in call order, so the last explicit value of one
    /// component type wins. An explicit or newly required [`Transform2d`] or
    /// [`ActiveCamera2d`] starts snapped; a required Transform that already
    /// exists keeps its interpolation history. The standard Bevy `Disabled`
    /// component may also be inserted directly, though [`Self::disable`] names
    /// that whole-entity operation more clearly.
    pub fn insert<B>(&mut self, entity: LogicEntity, bundle: B) -> Result<(), CommandEnqueueError>
    where
        B: Bundle<Effect: NoBundleEffect>,
    {
        self.queue.push(QueuedCommand::Insert {
            entity,
            bundle: Box::new(TypedInsert(bundle)),
        })
    }

    /// Queues approved explicit component types for removal from one entity.
    ///
    /// Removal is visible only after the current stage barrier. Required
    /// components are never removed implicitly, and naming an approved
    /// component that is absent is a successful no-op. Insert and remove
    /// commands apply in call order, but removing a component still required
    /// by the entity's final component set rejects the complete structural
    /// batch before mutation. Removing and despawning the same entity in one
    /// stage is likewise a batch conflict.
    ///
    /// Removing the standard Bevy `Disabled` component re-enables the entity
    /// after the barrier, though [`Self::enable`] names that operation more
    /// clearly. Retain its [`LogicEntity`] handle before disabling it: ordinary
    /// managed queries deliberately exclude disabled entities.
    pub fn remove<B>(&mut self, entity: LogicEntity) -> Result<(), CommandEnqueueError>
    where
        B: Bundle<Effect: NoBundleEffect>,
    {
        self.queue.push(QueuedCommand::Remove {
            entity,
            descriptor: RemoveDescriptor::new::<B>(),
        })
    }

    /// Queues an entity to become inactive after the current stage barrier.
    ///
    /// The entity keeps its identity, components, interpolation history, and
    /// place in the managed entity limit. Subsequent stages and same-frame
    /// extraction exclude it. Systems in the current stage continue to see
    /// the pre-barrier state. Repeated calls remain disabled, and this command
    /// has the same validation, ordering, atomicity, and despawn-conflict rules
    /// as [`Self::insert`]. Every call occupies one bounded Command slot,
    /// including a repeated disable.
    ///
    /// Retain the entity's [`LogicEntity`] before disabling it: ordinary
    /// managed queries deliberately cannot rediscover disabled entities. Pass
    /// that saved handle to [`Self::enable`] later.
    pub fn disable(&mut self, entity: LogicEntity) -> Result<(), CommandEnqueueError> {
        self.insert(entity, Disabled)
    }

    /// Queues a disabled entity to become active after the stage barrier.
    ///
    /// Subsequent stages and same-frame extraction can observe the entity
    /// again. Enabling an already enabled entity is a successful no-op. This
    /// command has the same validation, ordering, atomicity, and
    /// despawn-conflict rules as [`Self::remove`]. It requires a handle saved
    /// before the entity became unavailable to ordinary managed queries. Every
    /// call occupies one bounded Command slot, including a no-op enable.
    pub fn enable(&mut self, entity: LogicEntity) -> Result<(), CommandEnqueueError> {
        self.remove::<Disabled>(entity)
    }

    /// Queues a managed entity for deferred despawn.
    pub fn despawn(&mut self, entity: LogicEntity) -> Result<(), CommandEnqueueError> {
        self.queue.push(QueuedCommand::Despawn(entity))
    }

    /// Requests successful application exit after the current stage barrier.
    ///
    /// All Systems in the current stage still run. The request takes effect
    /// only if the complete command batch passes validation; repeated requests
    /// coalesce in the result, but each call still consumes one command slot. A
    /// fixed-stage request stops remaining catch-up ticks, and no new render
    /// extraction is produced. Pending input edges are discarded and
    /// interpolation endpoints are snapped, so a headless host that deliberately
    /// continues does not replay pre-exit input or display pre-exit state. If
    /// World replacement is requested in the same batch, exit takes precedence.
    ///
    /// This method is unavailable during isolated World Startup. It does not
    /// terminate the process itself: the desktop host exits its event loop,
    /// while headless hosts inspect the resulting frame report. It produces no
    /// `WorldExit` lifecycle record and promises neither a cleanup hook nor one
    /// final presentation.
    pub fn request_exit(&mut self) -> Result<(), CommandEnqueueError> {
        if !self.intents.application_control_is_available() {
            return Err(CommandEnqueueError::ExitUnavailable);
        }
        self.queue.push(QueuedCommand::Exit)
    }

    /// Queues a change to fixed-simulation pause at the stage barrier.
    ///
    /// FrameUpdate, input collection, extraction, and presentation continue
    /// while fixed simulation is paused. Every successfully enqueued call
    /// consumes one bounded command slot, including a request for the current
    /// value. Multiple calls in one successful stage are last-write-wins after
    /// every System in that stage has observed its entry-time clock state.
    /// A failed System or rejected command batch discards the pause request.
    ///
    /// Entering pause stops remaining fixed catch-up work without discarding
    /// its accumulated simulation time, clears undelivered fixed input edges,
    /// and snaps managed interpolation. Resume makes retained work eligible on
    /// the next application frame; paused wall time and input edges are not
    /// replayed. Pause is Application-owned and survives World replacement.
    /// Replacement keeps its own stronger time cleanup and stage-skipping
    /// rules; a rejected transition does not undo an otherwise committed pause.
    /// Application exit suppresses a pause change from the same batch.
    ///
    /// This method is unavailable during isolated World Startup. It does not
    /// bind an input action or implement toggle policy.
    pub fn set_paused(&mut self, paused: bool) -> Result<(), CommandEnqueueError> {
        if !self.intents.application_control_is_available() {
            return Err(CommandEnqueueError::PauseUnavailable);
        }
        self.queue.push(QueuedCommand::SetPaused(paused))
    }

    /// Issues a direct causal token valid during the current application frame.
    pub fn new_transition_intent(&mut self) -> Result<TransitionIntentToken, CommandEnqueueError> {
        self.intents.issue()
    }

    /// Queues a no-payload replacement request for stage-end arbitration.
    pub fn replace_world(
        &mut self,
        intent: TransitionIntentToken,
        target: WorldFactoryId,
    ) -> Result<(), CommandEnqueueError> {
        self.queue
            .push(QueuedCommand::ReplaceWorld(TransitionRequest::new(
                intent, target,
            )))
    }
}

pub(crate) struct CommittedCommands {
    pub(crate) spawned: usize,
    pub(crate) despawned: usize,
    pub(crate) live_entities: usize,
    pub(crate) transitions: Vec<TransitionRequest>,
    pub(crate) pause: Option<bool>,
    pub(crate) exit_requested: bool,
}

#[derive(Debug)]
pub(crate) enum CommandCommitError {
    Batch(CommandBatchError),
    RuntimeInvariant,
}

impl From<CommandBatchError> for CommandCommitError {
    fn from(error: CommandBatchError) -> Self {
        Self::Batch(error)
    }
}

#[cfg(test)]
fn managed_entity_count(world: &World) -> usize {
    world
        .iter_entities()
        .filter(|entity| entity.contains::<ManagedEntity>())
        .count()
}

#[cfg(test)]
mod tests {
    use std::sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    };

    use bevy_ecs::{entity_disabling::Disabled, prelude::Component};

    use crate::component::ComponentRegistry;

    use super::*;

    #[derive(Component, Clone, Copy)]
    struct Marker(u8);

    #[derive(Component, Debug, PartialEq, Eq)]
    struct Added(u8);

    #[derive(Bundle)]
    struct RemovableBundle {
        marker: Marker,
        added: Added,
    }

    #[derive(Component, Default, Clone, Copy)]
    #[require(Transform2d)]
    struct NeedsTransform;

    #[derive(Component, Clone, Copy)]
    struct Unapproved;

    static NEXT_FRESH_REQUIRED: AtomicUsize = AtomicUsize::new(1);

    #[derive(Component, Debug, PartialEq, Eq)]
    struct FreshRequired(usize);

    impl Default for FreshRequired {
        fn default() -> Self {
            Self(NEXT_FRESH_REQUIRED.fetch_add(1, Ordering::SeqCst))
        }
    }

    #[derive(Component, Clone, Copy)]
    #[require(FreshRequired)]
    struct NeedsFreshRequired;

    #[derive(Component, Clone, Copy)]
    #[require(Disabled)]
    struct NeedsDisabled;

    #[derive(Component)]
    struct DropMarker(Arc<AtomicUsize>);

    impl Drop for DropMarker {
        fn drop(&mut self) {
            self.0.fetch_add(1, Ordering::SeqCst);
        }
    }

    fn setup() -> (World, ApprovedComponents, ApplicationId, WorldGeneration) {
        let application = ApplicationId::from_raw(1);
        let generation = WorldGeneration::new(application, 1);
        let mut registry = ComponentRegistry::default();
        assert!(registry.approve::<Marker>().is_ok());
        assert!(registry.approve::<Added>().is_ok());
        assert!(registry.approve::<DropMarker>().is_ok());
        assert!(registry.approve::<Disabled>().is_ok());
        assert!(registry.approve::<Transform2d>().is_ok());
        assert!(registry.approve::<ActiveCamera2d>().is_ok());
        assert!(registry.approve::<NeedsTransform>().is_ok());
        assert!(registry.approve::<FreshRequired>().is_ok());
        assert!(registry.approve::<NeedsFreshRequired>().is_ok());
        assert!(registry.approve::<NeedsDisabled>().is_ok());
        let mut world = World::new();
        let approved = match registry.install(&mut world) {
            Ok(approved) => approved,
            Err(error) => panic!("unexpected approval error: {error}"),
        };
        (world, approved, application, generation)
    }

    #[test]
    fn spawn_is_invisible_until_whole_batch_commit() {
        let (mut world, mut approved, application, generation) = setup();
        let mut queue = CommandQueue::new(4);
        assert!(queue.push(QueuedCommand::spawn(Marker(7))).is_ok());
        let command_capacity = queue.command_capacity();
        assert_eq!(managed_entity_count(&world), 0);

        let result =
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 0, 8);
        let committed = result.expect("valid spawn batch should commit");
        assert_eq!(committed.live_entities, 1);
        let values: Vec<_> = world.query::<&Marker>().iter(&world).map(|m| m.0).collect();
        assert_eq!(values, [7]);
        let marker = world
            .component_id::<Marker>()
            .expect("Marker should be registered");
        let managed = world
            .component_id::<ManagedEntity>()
            .expect("ManagedEntity should be registered");
        assert!(
            !world
                .archetypes()
                .iter()
                .any(|archetype| archetype.contains(marker) && !archetype.contains(managed))
        );
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), command_capacity);
    }

    #[test]
    fn copy_spawns_use_logical_slots_and_preserve_consecutive_order() {
        let (mut world, mut approved, application, generation) = setup();
        let mut queue = CommandQueue::new(6);
        assert!(queue.push(QueuedCommand::spawn(Marker(1))).is_ok());
        assert!(queue.push_spawn_copies(Marker(2), 3).is_ok());
        assert!(queue.push(QueuedCommand::spawn(Marker(3))).is_ok());
        assert!(queue.push(QueuedCommand::Exit).is_ok());
        assert_eq!(queue.commands.len(), 4);
        assert_eq!(queue.used_slots, 6);

        let committed = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 0, 5)
            .expect("exactly fitting repeated spawns should commit");

        assert_eq!(committed.spawned, 5);
        assert_eq!(committed.live_entities, 5);
        assert!(committed.exit_requested);
        let values: Vec<_> = world
            .iter_entities()
            .filter_map(|entity| {
                let marker = entity.get::<Marker>()?;
                let managed = entity
                    .get::<ManagedEntity>()
                    .expect("every copy should be managed");
                assert_eq!(managed.handle(entity.id()).world_generation(), generation);
                Some(marker.0)
            })
            .collect();
        assert_eq!(values, [1, 2, 2, 2, 3]);
        assert!(queue.is_clean());
    }

    #[test]
    fn despawn_and_copy_spawn_use_the_net_entity_count_atomically() {
        let (mut world, mut approved, application, generation) = setup();
        let removed_raw = world.spawn(Marker(1)).id();
        let retained_raw = world.spawn(Marker(2)).id();
        for raw in [removed_raw, retained_raw] {
            let entity = LogicEntity::new(application, generation, raw);
            world.entity_mut(raw).insert(ManagedEntity::new(entity));
        }
        let removed = LogicEntity::new(application, generation, removed_raw);

        let mut rejected = CommandQueue::new(4);
        assert!(rejected.push(QueuedCommand::Despawn(removed)).is_ok());
        assert!(rejected.push_spawn_copies(Marker(3), 3).is_ok());
        assert!(matches!(
            rejected.validate_and_apply(&mut world, &mut approved, application, generation, 2, 3,),
            Err(CommandCommitError::Batch(
                CommandBatchError::EntityLimitExceeded {
                    limit: 3,
                    requested: 4,
                }
            ))
        ));
        assert!(world.entities().contains(removed_raw));
        assert!(world.entities().contains(retained_raw));
        assert_eq!(managed_entity_count(&world), 2);

        let mut exact = CommandQueue::new(3);
        assert!(exact.push(QueuedCommand::Despawn(removed)).is_ok());
        assert!(exact.push_spawn_copies(Marker(4), 2).is_ok());
        let committed = exact
            .validate_and_apply(&mut world, &mut approved, application, generation, 2, 3)
            .expect("net entity count at the limit should commit");
        assert_eq!(committed.despawned, 1);
        assert_eq!(committed.spawned, 2);
        assert_eq!(committed.live_entities, 3);
        assert!(!world.entities().contains(removed_raw));
        assert!(world.entities().contains(retained_raw));
        assert_eq!(managed_entity_count(&world), 3);
    }

    #[test]
    fn copy_spawn_limit_failures_poison_without_a_retained_prefix_and_then_reuse() {
        let (mut world, mut approved, application, generation) = setup();
        let mut queue = CommandQueue::new(3);
        assert!(queue.push(QueuedCommand::spawn(Marker(1))).is_ok());
        assert!(matches!(
            queue.push_spawn_copies(Marker(2), 3),
            Err(CommandEnqueueError::LimitExceeded { limit: 3 })
        ));
        assert_eq!(queue.commands.len(), 1);
        assert_eq!(queue.used_slots, 1);
        assert!(matches!(
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 0, 8),
            Err(CommandCommitError::Batch(
                CommandBatchError::LimitExceeded { limit: 3 }
            ))
        ));
        assert_eq!(managed_entity_count(&world), 0);
        assert!(queue.is_clean());

        assert!(queue.push_spawn_copies(Marker(4), 3).is_ok());
        assert!(matches!(
            queue.push(QueuedCommand::spawn(Marker(5))),
            Err(CommandEnqueueError::LimitExceeded { limit: 3 })
        ));
        queue.discard();
        assert!(queue.is_clean());

        assert!(queue.push_spawn_copies(Marker(6), 3).is_ok());
        let committed = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 0, 8)
            .expect("discarded poison should not affect a later batch");
        assert_eq!(committed.spawned, 3);
    }

    #[test]
    fn copy_spawn_logical_slot_overflow_uses_the_bounded_limit_error() {
        let (mut world, mut approved, application, generation) = setup();
        let mut queue = CommandQueue::new(usize::MAX);
        assert!(queue.push(QueuedCommand::spawn(Marker(1))).is_ok());
        assert!(matches!(
            queue.push_spawn_copies(Marker(2), usize::MAX),
            Err(CommandEnqueueError::LimitExceeded { limit }) if limit == usize::MAX
        ));
        assert!(matches!(
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 0, 1),
            Err(CommandCommitError::Batch(
                CommandBatchError::LimitExceeded { limit }
            )) if limit == usize::MAX
        ));
        assert_eq!(managed_entity_count(&world), 0);
        assert!(queue.is_clean());
    }

    #[test]
    fn copy_spawn_validates_once_and_preflights_the_complete_entity_count() {
        let (mut world, mut approved, application, generation) = setup();
        let mut over_capacity = CommandQueue::new(3);
        assert!(over_capacity.push_spawn_copies(Marker(1), 3).is_ok());
        assert!(matches!(
            over_capacity.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                0,
                2,
            ),
            Err(CommandCommitError::Batch(
                CommandBatchError::EntityLimitExceeded {
                    limit: 2,
                    requested: 3,
                }
            ))
        ));
        assert_eq!(managed_entity_count(&world), 0);

        let mut unapproved = CommandQueue::new(3);
        assert!(unapproved.push_spawn_copies(Unapproved, 3).is_ok());
        assert!(matches!(
            unapproved
                .validate_and_apply(&mut world, &mut approved, application, generation, 0, 0,),
            Err(CommandCommitError::Batch(CommandBatchError::Component(
                ComponentApprovalError::UnapprovedBundleComponent { .. }
            )))
        ));
        assert_eq!(managed_entity_count(&world), 0);
    }

    #[test]
    fn copy_spawn_entity_count_overflow_rejects_before_mutation() {
        let (mut world, mut approved, application, generation) = setup();
        let mut queue = CommandQueue::new(2);
        assert!(queue.push_spawn_copies(Marker(1), 2).is_ok());

        assert!(matches!(
            queue.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                usize::MAX - 1,
                usize::MAX,
            ),
            Err(CommandCommitError::Batch(
                CommandBatchError::EntityLimitExceeded {
                    limit,
                    requested: usize::MAX,
                }
            )) if limit == usize::MAX
        ));
        assert_eq!(managed_entity_count(&world), 0);
        assert!(queue.is_clean());
    }

    #[test]
    fn every_copy_gets_fresh_requirements_and_snapped_spatial_state() {
        let (mut world, mut approved, application, generation) = setup();
        let mut transform = Transform2d::default();
        transform.begin_fixed_tick();
        assert!(
            transform
                .set_translation(sim_engine::Vec2::new(4.0, -2.0))
                .is_ok()
        );
        let mut camera = ActiveCamera2d::new(
            sim_engine::Camera2d::new(sim_engine::Vec2::ZERO, 12.0)
                .expect("finite camera should construct"),
        );
        camera.begin_fixed_tick();
        assert!(camera.set_center(sim_engine::Vec2::new(-3.0, 5.0)).is_ok());

        let mut queue = CommandQueue::new(2);
        assert!(
            queue
                .push_spawn_copies((NeedsFreshRequired, NeedsDisabled, transform, camera), 2)
                .is_ok()
        );
        let committed = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 0, 2)
            .expect("approved repeated spatial bundle should commit");
        assert_eq!(committed.spawned, 2);
        assert_eq!(committed.live_entities, 2);

        let mut fresh_values = Vec::new();
        for entity in world
            .iter_entities()
            .filter(|entity| entity.contains::<NeedsFreshRequired>())
        {
            assert!(entity.contains::<Disabled>());
            let transform = entity
                .get::<Transform2d>()
                .expect("explicit Transform should remain");
            assert_eq!(transform.previous_translation(), transform.translation());
            let camera = entity
                .get::<ActiveCamera2d>()
                .expect("explicit camera should remain");
            assert_eq!(camera.previous_center(), camera.center());
            fresh_values.push(
                entity
                    .get::<FreshRequired>()
                    .expect("required value should materialize per entity")
                    .0,
            );
        }
        assert_eq!(fresh_values.len(), 2);
        assert_ne!(fresh_values[0], fresh_values[1]);
    }

    #[test]
    fn committed_live_count_tracks_despawns_and_spawns_in_one_batch() {
        let (mut world, mut approved, application, generation) = setup();
        let removed_raw = world.spawn(Marker(1)).id();
        let retained_raw = world.spawn((Marker(2), Disabled)).id();
        for raw in [removed_raw, retained_raw] {
            let entity = LogicEntity::new(application, generation, raw);
            world.entity_mut(raw).insert(ManagedEntity::new(entity));
        }

        let removed = LogicEntity::new(application, generation, removed_raw);
        let mut queue = CommandQueue::new(4);
        assert!(queue.push(QueuedCommand::Despawn(removed)).is_ok());
        assert!(queue.push(QueuedCommand::spawn(Marker(3))).is_ok());

        let committed = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 2, 2)
            .expect("balanced batch should preserve the live count");

        assert_eq!(committed.live_entities, 2);
        assert_eq!(managed_entity_count(&world), 2);
        assert!(!world.entities().contains(removed_raw));
        assert!(world.entities().contains(retained_raw));
        assert!(queue.is_clean());
        assert!(queue.despawn_capacity() >= 1);
    }

    #[test]
    fn insert_adds_and_replaces_in_command_order_without_changing_live_count() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn(Marker(1)).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let mut queue = CommandQueue::new(3);
        for command in [
            QueuedCommand::Insert {
                entity,
                bundle: Box::new(TypedInsert(Added(3))),
            },
            QueuedCommand::Insert {
                entity,
                bundle: Box::new(TypedInsert(Marker(7))),
            },
            QueuedCommand::Insert {
                entity,
                bundle: Box::new(TypedInsert(Marker(9))),
            },
        ] {
            assert!(queue.push(command).is_ok());
        }

        let committed = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 1, 1)
            .expect("approved inserts should commit");

        assert_eq!(committed.spawned, 0);
        assert_eq!(committed.despawned, 0);
        assert_eq!(committed.live_entities, 1);
        assert_eq!(world.get::<Added>(raw), Some(&Added(3)));
        assert_eq!(world.get::<Marker>(raw).map(|marker| marker.0), Some(9));
        assert!(queue.is_clean());
    }

    #[test]
    fn remove_is_explicit_idempotent_and_ordered_with_insert() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world
            .spawn(RemovableBundle {
                marker: Marker(1),
                added: Added(2),
            })
            .id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let mut queue = CommandQueue::new(5);
        for command in [
            QueuedCommand::Remove {
                entity,
                descriptor: RemoveDescriptor::new::<Added>(),
            },
            QueuedCommand::Remove {
                entity,
                descriptor: RemoveDescriptor::new::<Added>(),
            },
            QueuedCommand::Remove {
                entity,
                descriptor: RemoveDescriptor::new::<Marker>(),
            },
            QueuedCommand::Insert {
                entity,
                bundle: Box::new(TypedInsert(Marker(9))),
            },
            QueuedCommand::Remove {
                entity,
                descriptor: RemoveDescriptor::new::<Added>(),
            },
        ] {
            assert!(queue.push(command).is_ok());
        }

        let committed = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 1, 1)
            .expect("approved removals and later insert should commit in order");

        assert_eq!(committed.spawned, 0);
        assert_eq!(committed.despawned, 0);
        assert_eq!(committed.live_entities, 1);
        assert_eq!(world.get::<Marker>(raw).map(|marker| marker.0), Some(9));
        assert!(world.get::<Added>(raw).is_none());
        assert!(world.get::<ManagedEntity>(raw).is_some());
        assert!(queue.is_clean());
        assert!(queue.component_target_capacity() >= 1);
        assert!(queue.presence_override_capacity() >= 2);

        let mut tuple_queue = CommandQueue::new(1);
        assert!(
            tuple_queue
                .push(QueuedCommand::Remove {
                    entity,
                    descriptor: RemoveDescriptor::new::<(Marker, Added)>(),
                })
                .is_ok()
        );
        tuple_queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 1, 1)
            .expect("tuple removal should remove only explicit members");
        assert!(world.get::<Marker>(raw).is_none());
        assert!(world.get::<ManagedEntity>(raw).is_some());

        world.entity_mut(raw).insert(RemovableBundle {
            marker: Marker(4),
            added: Added(5),
        });
        let mut derived_queue = CommandQueue::new(1);
        assert!(
            derived_queue
                .push(QueuedCommand::Remove {
                    entity,
                    descriptor: RemoveDescriptor::new::<RemovableBundle>(),
                })
                .is_ok()
        );
        derived_queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 1, 1)
            .expect("derived bundle removal should remove its explicit members");
        assert!(world.get::<Marker>(raw).is_none());
        assert!(world.get::<Added>(raw).is_none());
        assert!(world.get::<ManagedEntity>(raw).is_some());
    }

    #[test]
    fn removal_planner_ignores_inserts_on_unaffected_entities() {
        let (mut world, _approved, application, generation) = setup();
        let affected_raw = world.spawn((Marker(1), Added(1))).id();
        let affected = LogicEntity::new(application, generation, affected_raw);
        world
            .entity_mut(affected_raw)
            .insert(ManagedEntity::new(affected));
        let unrelated_raw = world.spawn(Marker(2)).id();
        let unrelated = LogicEntity::new(application, generation, unrelated_raw);
        world
            .entity_mut(unrelated_raw)
            .insert(ManagedEntity::new(unrelated));
        let mut queue = CommandQueue::new(3);
        assert!(
            queue
                .push(QueuedCommand::Insert {
                    entity: unrelated,
                    bundle: Box::new(TypedInsert(Added(8))),
                })
                .is_ok()
        );
        assert!(
            queue
                .push(QueuedCommand::Remove {
                    entity: affected,
                    descriptor: RemoveDescriptor::new::<Added>(),
                })
                .is_ok()
        );
        assert!(
            queue
                .push(QueuedCommand::Insert {
                    entity: affected,
                    bundle: Box::new(TypedInsert(Marker(9))),
                })
                .is_ok()
        );

        queue
            .plan_component_presence(&mut world, 1)
            .expect("presence planning should fit its bounded storage");

        assert_eq!(queue.component_target_order, [affected_raw]);
        assert!(
            queue
                .presence_overrides
                .keys()
                .all(|(entity, _)| *entity == affected_raw)
        );
        assert!(
            queue
                .inserted_components
                .iter()
                .all(|(entity, _)| *entity == affected_raw)
        );
        assert_eq!(queue.inserted_components.len(), 1);
    }

    #[test]
    fn removal_validates_the_final_required_component_set() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn(NeedsTransform).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));

        let mut invalid = CommandQueue::new(1);
        assert!(
            invalid
                .push(QueuedCommand::Remove {
                    entity,
                    descriptor: RemoveDescriptor::new::<Transform2d>(),
                })
                .is_ok()
        );
        let invalid_result =
            invalid.validate_and_apply(&mut world, &mut approved, application, generation, 1, 1);
        let Err(CommandCommitError::Batch(CommandBatchError::RequiredComponentWouldBeMissing {
            entity: rejected,
            required,
            required_by,
        })) = invalid_result
        else {
            panic!("removing a retained requirement should reject the batch");
        };
        assert_eq!(rejected, entity);
        assert!(required.ends_with("Transform2d"), "{required}");
        assert!(required_by.ends_with("NeedsTransform"), "{required_by}");
        assert!(invalid.is_clean());
        assert!(invalid.component_target_capacity() >= 1);
        assert!(invalid.presence_override_capacity() >= 1);
        assert!(world.get::<NeedsTransform>(raw).is_some());
        assert!(world.get::<Transform2d>(raw).is_some());

        let mut remove_both = CommandQueue::new(2);
        assert!(
            remove_both
                .push(QueuedCommand::Remove {
                    entity,
                    descriptor: RemoveDescriptor::new::<Transform2d>(),
                })
                .is_ok()
        );
        assert!(
            remove_both
                .push(QueuedCommand::Remove {
                    entity,
                    descriptor: RemoveDescriptor::new::<NeedsTransform>(),
                })
                .is_ok()
        );
        remove_both
            .validate_and_apply(&mut world, &mut approved, application, generation, 1, 1)
            .expect("removing the requiree and requirement should be valid");
        assert!(world.get::<NeedsTransform>(raw).is_none());
        assert!(world.get::<Transform2d>(raw).is_none());

        let materialize_raw = world.spawn(Marker(2)).id();
        let materialize = LogicEntity::new(application, generation, materialize_raw);
        world
            .entity_mut(materialize_raw)
            .insert(ManagedEntity::new(materialize));
        let mut materialize_required = CommandQueue::new(2);
        assert!(
            materialize_required
                .push(QueuedCommand::Remove {
                    entity: materialize,
                    descriptor: RemoveDescriptor::new::<Transform2d>(),
                })
                .is_ok()
        );
        assert!(
            materialize_required
                .push(QueuedCommand::Insert {
                    entity: materialize,
                    bundle: Box::new(TypedInsert(NeedsTransform)),
                })
                .is_ok()
        );
        materialize_required
            .validate_and_apply(&mut world, &mut approved, application, generation, 2, 2)
            .expect("later requiree insert should rematerialize its Transform");
        let transform = world
            .get::<Transform2d>(materialize_raw)
            .expect("required Transform should exist");
        assert_eq!(transform.previous_translation(), transform.translation());

        let existing_raw = world.spawn(NeedsTransform).id();
        let existing = LogicEntity::new(application, generation, existing_raw);
        world
            .entity_mut(existing_raw)
            .insert(ManagedEntity::new(existing));
        let mut rematerialize_existing = CommandQueue::new(2);
        assert!(
            rematerialize_existing
                .push(QueuedCommand::Remove {
                    entity: existing,
                    descriptor: RemoveDescriptor::new::<Transform2d>(),
                })
                .is_ok()
        );
        assert!(
            rematerialize_existing
                .push(QueuedCommand::Insert {
                    entity: existing,
                    bundle: Box::new(TypedInsert(NeedsTransform)),
                })
                .is_ok()
        );
        rematerialize_existing
            .validate_and_apply(&mut world, &mut approved, application, generation, 3, 3)
            .expect("reinserting a retained requiree should rematerialize its requirement");
        assert!(world.get::<NeedsTransform>(existing_raw).is_some());
        assert!(world.get::<Transform2d>(existing_raw).is_some());

        let reject_raw = world.spawn(Marker(3)).id();
        let reject = LogicEntity::new(application, generation, reject_raw);
        world
            .entity_mut(reject_raw)
            .insert(ManagedEntity::new(reject));
        let mut invalid_order = CommandQueue::new(2);
        assert!(
            invalid_order
                .push(QueuedCommand::Insert {
                    entity: reject,
                    bundle: Box::new(TypedInsert(NeedsTransform)),
                })
                .is_ok()
        );
        assert!(
            invalid_order
                .push(QueuedCommand::Remove {
                    entity: reject,
                    descriptor: RemoveDescriptor::new::<Transform2d>(),
                })
                .is_ok()
        );
        assert!(matches!(
            invalid_order.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                4,
                4,
            ),
            Err(CommandCommitError::Batch(
                CommandBatchError::RequiredComponentWouldBeMissing { .. }
            ))
        ));
        assert!(world.get::<NeedsTransform>(reject_raw).is_none());
        assert!(world.get::<Transform2d>(reject_raw).is_none());
    }

    #[test]
    fn remove_and_despawn_conflict_is_symmetric_and_atomic() {
        for remove_first in [true, false] {
            let (mut world, mut approved, application, generation) = setup();
            let drops = Arc::new(AtomicUsize::new(0));
            let raw = world
                .spawn((Marker(1), DropMarker(Arc::clone(&drops))))
                .id();
            let entity = LogicEntity::new(application, generation, raw);
            world.entity_mut(raw).insert(ManagedEntity::new(entity));
            let mut queue = CommandQueue::new(2);
            let remove = || QueuedCommand::Remove {
                entity,
                descriptor: RemoveDescriptor::new::<DropMarker>(),
            };
            if remove_first {
                assert!(queue.push(remove()).is_ok());
                assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());
            } else {
                assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());
                assert!(queue.push(remove()).is_ok());
            }

            assert!(matches!(
                queue.validate_and_apply(
                    &mut world,
                    &mut approved,
                    application,
                    generation,
                    1,
                    1,
                ),
                Err(CommandCommitError::Batch(
                    CommandBatchError::ConflictingEntityCommands {
                        entity: conflicted
                    }
                )) if conflicted == entity
            ));
            assert_eq!(drops.load(Ordering::SeqCst), 0);
            assert!(world.get::<DropMarker>(raw).is_some());
            assert!(world.entities().contains(raw));
        }
    }

    #[test]
    fn successful_and_repeated_remove_drop_the_stored_value_exactly_once() {
        let (mut world, mut approved, application, generation) = setup();
        let drops = Arc::new(AtomicUsize::new(0));
        let raw = world
            .spawn((Marker(1), DropMarker(Arc::clone(&drops))))
            .id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let mut queue = CommandQueue::new(2);
        for _ in 0..2 {
            assert!(
                queue
                    .push(QueuedCommand::Remove {
                        entity,
                        descriptor: RemoveDescriptor::new::<DropMarker>(),
                    })
                    .is_ok()
            );
        }

        queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 1, 1)
            .expect("repeated removal should be an idempotent successful batch");
        assert_eq!(drops.load(Ordering::SeqCst), 1);
        assert!(world.get::<DropMarker>(raw).is_none());
        assert!(world.get::<Marker>(raw).is_some());
        assert!(world.get::<ManagedEntity>(raw).is_some());
    }

    #[test]
    fn invalid_remove_target_or_type_rejects_without_mutation() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn((Marker(1), Added(2))).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let foreign = LogicEntity::new(application, WorldGeneration::new(application, 9), raw);

        for (target, descriptor) in [
            (foreign, RemoveDescriptor::new::<Added>()),
            (entity, RemoveDescriptor::new::<Unapproved>()),
            (entity, RemoveDescriptor::new::<ManagedEntity>()),
        ] {
            let mut queue = CommandQueue::new(2);
            assert!(queue.push(QueuedCommand::spawn(Marker(8))).is_ok());
            assert!(
                queue
                    .push(QueuedCommand::Remove {
                        entity: target,
                        descriptor,
                    })
                    .is_ok()
            );
            assert!(
                queue
                    .validate_and_apply(&mut world, &mut approved, application, generation, 1, 4,)
                    .is_err()
            );
            assert_eq!(managed_entity_count(&world), 1);
            assert_eq!(world.get::<Added>(raw), Some(&Added(2)));
        }

        let missing_raw = world.spawn(Marker(0)).id();
        let missing = LogicEntity::new(application, generation, missing_raw);
        world
            .entity_mut(missing_raw)
            .insert(ManagedEntity::new(missing));
        assert!(world.despawn(missing_raw));
        let mut missing_queue = CommandQueue::new(1);
        assert!(
            missing_queue
                .push(QueuedCommand::Remove {
                    entity: missing,
                    descriptor: RemoveDescriptor::new::<Added>(),
                })
                .is_ok()
        );
        assert!(matches!(
            missing_queue.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                1,
                4,
            ),
            Err(CommandCommitError::Batch(
                CommandBatchError::MissingEntity { entity: rejected }
            )) if rejected == missing
        ));
        assert_eq!(world.get::<Added>(raw), Some(&Added(2)));
    }

    #[test]
    fn insert_and_despawn_conflict_is_symmetric_and_atomic() {
        for insert_first in [true, false] {
            let (mut world, mut approved, application, generation) = setup();
            let raw = world.spawn(Marker(1)).id();
            let entity = LogicEntity::new(application, generation, raw);
            world.entity_mut(raw).insert(ManagedEntity::new(entity));
            let mut queue = CommandQueue::new(2);
            let insert = || QueuedCommand::Insert {
                entity,
                bundle: Box::new(TypedInsert(Marker(2))),
            };
            if insert_first {
                assert!(queue.push(insert()).is_ok());
                assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());
            } else {
                assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());
                assert!(queue.push(insert()).is_ok());
            }

            let result =
                queue.validate_and_apply(&mut world, &mut approved, application, generation, 1, 1);

            assert!(matches!(
                result,
                Err(CommandCommitError::Batch(
                    CommandBatchError::ConflictingEntityCommands { entity: conflicted }
                )) if conflicted == entity
            ));
            assert_eq!(world.get::<Marker>(raw).map(|marker| marker.0), Some(1));
            assert!(world.entities().contains(raw));
            assert!(queue.is_clean());
        }
    }

    #[test]
    fn invalid_insert_target_and_bundle_reject_before_any_structural_mutation() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn(Marker(1)).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let foreign = LogicEntity::new(application, WorldGeneration::new(application, 7), raw);
        let mut foreign_queue = CommandQueue::new(1);
        assert!(
            foreign_queue
                .push(QueuedCommand::Insert {
                    entity: foreign,
                    bundle: Box::new(TypedInsert(Added(2))),
                })
                .is_ok()
        );
        assert!(matches!(
            foreign_queue.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                1,
                2,
            ),
            Err(CommandCommitError::Batch(
                CommandBatchError::ForeignEntity { entity }
            )) if entity == foreign
        ));

        let missing_raw = world.spawn(Marker(8)).id();
        let missing = LogicEntity::new(application, generation, missing_raw);
        world
            .entity_mut(missing_raw)
            .insert(ManagedEntity::new(missing));
        assert!(world.despawn(missing_raw));
        let mut missing_queue = CommandQueue::new(1);
        assert!(
            missing_queue
                .push(QueuedCommand::Insert {
                    entity: missing,
                    bundle: Box::new(TypedInsert(Added(3))),
                })
                .is_ok()
        );
        assert!(matches!(
            missing_queue.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                1,
                2,
            ),
            Err(CommandCommitError::Batch(
                CommandBatchError::MissingEntity { entity }
            )) if entity == missing
        ));

        let mut identity_queue = CommandQueue::new(1);
        assert!(
            identity_queue
                .push(QueuedCommand::Insert {
                    entity,
                    bundle: Box::new(TypedInsert(ManagedEntity::new(entity))),
                })
                .is_ok()
        );
        assert!(matches!(
            identity_queue.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                1,
                2,
            ),
            Err(CommandCommitError::Batch(CommandBatchError::Component(
                ComponentApprovalError::RuntimeIdentityInBundle
            )))
        ));

        let mut bundle_queue = CommandQueue::new(2);
        assert!(bundle_queue.push(QueuedCommand::spawn(Added(4))).is_ok());
        assert!(
            bundle_queue
                .push(QueuedCommand::Insert {
                    entity,
                    bundle: Box::new(TypedInsert(Unapproved)),
                })
                .is_ok()
        );
        assert!(matches!(
            bundle_queue.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                1,
                2,
            ),
            Err(CommandCommitError::Batch(CommandBatchError::Component(_)))
        ));
        assert_eq!(managed_entity_count(&world), 1);
        assert_eq!(world.get::<Marker>(raw).map(|marker| marker.0), Some(1));
        assert!(world.get::<Added>(raw).is_none());
    }

    #[test]
    fn insert_snaps_only_explicit_or_newly_required_interpolation_values() {
        let (mut world, mut approved, application, generation) = setup();
        let mut moving = Transform2d::from_xy(1.0, 0.0).expect("finite transform");
        moving.begin_fixed_tick();
        moving
            .set_translation(sim_engine::Vec2::new(2.0, 0.0))
            .expect("finite movement");
        let raw = world.spawn((Marker(1), moving)).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let mut preserve_queue = CommandQueue::new(1);
        assert!(
            preserve_queue
                .push(QueuedCommand::Insert {
                    entity,
                    bundle: Box::new(TypedInsert(NeedsTransform)),
                })
                .is_ok()
        );
        preserve_queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 1, 1)
            .expect("required transform insertion should commit");
        let preserved = world.get::<Transform2d>(raw).expect("transform remains");
        assert_eq!(
            preserved.previous_translation(),
            sim_engine::Vec2::new(1.0, 0.0)
        );
        assert_eq!(preserved.translation(), sim_engine::Vec2::new(2.0, 0.0));

        let mut replacement = Transform2d::from_xy(8.0, 0.0).expect("finite replacement transform");
        replacement.begin_fixed_tick();
        replacement
            .set_translation(sim_engine::Vec2::new(9.0, 0.0))
            .expect("finite replacement movement");
        let mut replace_queue = CommandQueue::new(1);
        assert!(
            replace_queue
                .push(QueuedCommand::Insert {
                    entity,
                    bundle: Box::new(TypedInsert(replacement)),
                })
                .is_ok()
        );
        replace_queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 1, 1)
            .expect("explicit transform replacement should commit");
        let replaced = world.get::<Transform2d>(raw).expect("transform remains");
        assert_eq!(
            replaced.previous_translation(),
            sim_engine::Vec2::new(9.0, 0.0)
        );
        assert_eq!(replaced.translation(), sim_engine::Vec2::new(9.0, 0.0));

        let plain_raw = world.spawn(Marker(2)).id();
        let plain = LogicEntity::new(application, generation, plain_raw);
        world
            .entity_mut(plain_raw)
            .insert(ManagedEntity::new(plain));
        let mut required_queue = CommandQueue::new(1);
        assert!(
            required_queue
                .push(QueuedCommand::Insert {
                    entity: plain,
                    bundle: Box::new(TypedInsert(NeedsTransform)),
                })
                .is_ok()
        );
        required_queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 2, 2)
            .expect("missing required transform should materialize");
        let materialized = world
            .get::<Transform2d>(plain_raw)
            .expect("required transform exists");
        assert_eq!(
            materialized.previous_translation(),
            materialized.translation()
        );

        let mut replacement_camera = ActiveCamera2d::new(
            sim_engine::Camera2d::new(sim_engine::Vec2::ZERO, 16.0)
                .expect("valid replacement camera"),
        );
        replacement_camera
            .set_center(sim_engine::Vec2::new(7.0, -2.0))
            .expect("finite replacement center");
        let mut camera_queue = CommandQueue::new(1);
        assert!(
            camera_queue
                .push(QueuedCommand::Insert {
                    entity: plain,
                    bundle: Box::new(TypedInsert(replacement_camera)),
                })
                .is_ok()
        );
        camera_queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 2, 2)
            .expect("explicit camera insertion should commit");
        let camera = world
            .get::<ActiveCamera2d>(plain_raw)
            .expect("camera should be inserted");
        assert_eq!(camera.previous_center(), sim_engine::Vec2::new(7.0, -2.0));
        assert_eq!(camera.center(), sim_engine::Vec2::new(7.0, -2.0));

        let precedence_raw = world.spawn(Marker(3)).id();
        let precedence = LogicEntity::new(application, generation, precedence_raw);
        world
            .entity_mut(precedence_raw)
            .insert(ManagedEntity::new(precedence));
        let mut explicit = Transform2d::from_xy(3.0, 0.0).expect("finite explicit transform");
        explicit.begin_fixed_tick();
        explicit
            .set_translation(sim_engine::Vec2::new(4.0, 0.0))
            .expect("finite explicit movement");
        let mut precedence_queue = CommandQueue::new(1);
        assert!(
            precedence_queue
                .push(QueuedCommand::Insert {
                    entity: precedence,
                    bundle: Box::new(TypedInsert((NeedsTransform, explicit))),
                })
                .is_ok()
        );
        precedence_queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 3, 3)
            .expect("explicit Transform should satisfy the required component");
        let explicit = world
            .get::<Transform2d>(precedence_raw)
            .expect("explicit Transform should be retained");
        assert_eq!(
            explicit.previous_translation(),
            sim_engine::Vec2::new(4.0, 0.0)
        );
        assert_eq!(explicit.translation(), sim_engine::Vec2::new(4.0, 0.0));
    }

    #[test]
    fn rejected_insert_drops_new_payload_once_without_replacing_old_value() {
        let (mut world, mut approved, application, generation) = setup();
        let old_drops = Arc::new(AtomicUsize::new(0));
        let new_drops = Arc::new(AtomicUsize::new(0));
        let raw = world
            .spawn((Marker(1), DropMarker(Arc::clone(&old_drops))))
            .id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let mut queue = CommandQueue::new(2);
        assert!(
            queue
                .push(QueuedCommand::Insert {
                    entity,
                    bundle: Box::new(TypedInsert(DropMarker(Arc::clone(&new_drops)))),
                })
                .is_ok()
        );
        assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());

        assert!(matches!(
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 1, 1),
            Err(CommandCommitError::Batch(
                CommandBatchError::ConflictingEntityCommands { .. }
            ))
        ));
        assert_eq!(new_drops.load(Ordering::SeqCst), 1);
        assert_eq!(old_drops.load(Ordering::SeqCst), 0);
        assert!(world.get::<DropMarker>(raw).is_some());

        let discarded_drops = Arc::new(AtomicUsize::new(0));
        let mut discarded = CommandQueue::new(1);
        assert!(
            discarded
                .push(QueuedCommand::Insert {
                    entity,
                    bundle: Box::new(TypedInsert(DropMarker(Arc::clone(&discarded_drops)))),
                })
                .is_ok()
        );
        discarded.discard();
        assert_eq!(discarded_drops.load(Ordering::SeqCst), 1);
        assert_eq!(old_drops.load(Ordering::SeqCst), 0);
        assert!(world.get::<DropMarker>(raw).is_some());
    }

    #[test]
    fn insert_obeys_queue_poison_and_reuses_command_storage() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn(Marker(1)).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let retained_drops = Arc::new(AtomicUsize::new(0));
        let rejected_drops = Arc::new(AtomicUsize::new(0));
        let mut queue = CommandQueue::new(1);
        assert!(
            queue
                .push(QueuedCommand::Insert {
                    entity,
                    bundle: Box::new(TypedInsert(DropMarker(Arc::clone(&retained_drops)))),
                })
                .is_ok()
        );
        let capacity = queue.command_capacity();
        assert!(matches!(
            queue.push(QueuedCommand::Insert {
                entity,
                bundle: Box::new(TypedInsert(DropMarker(Arc::clone(&rejected_drops)))),
            }),
            Err(CommandEnqueueError::LimitExceeded { limit: 1 })
        ));
        assert_eq!(rejected_drops.load(Ordering::SeqCst), 1);
        assert_eq!(retained_drops.load(Ordering::SeqCst), 0);

        assert!(matches!(
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 1, 1),
            Err(CommandCommitError::Batch(
                CommandBatchError::LimitExceeded { limit: 1 }
            ))
        ));
        assert_eq!(retained_drops.load(Ordering::SeqCst), 1);
        assert!(world.get::<DropMarker>(raw).is_none());
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), capacity);
    }

    #[test]
    fn repeated_exit_requests_coalesce_without_changing_entities() {
        let (mut world, mut approved, application, generation) = setup();
        let mut queue = CommandQueue::new(2);
        assert!(queue.push(QueuedCommand::Exit).is_ok());
        assert!(queue.push(QueuedCommand::Exit).is_ok());
        let capacity = queue.command_capacity();

        let committed = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 0, 1)
            .expect("repeated exit requests should form one valid terminal request");

        assert!(committed.exit_requested);
        assert_eq!(committed.spawned, 0);
        assert_eq!(committed.despawned, 0);
        assert_eq!(committed.live_entities, 0);
        assert!(committed.transitions.is_empty());
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), capacity);

        assert!(queue.push(QueuedCommand::Exit).is_ok());
        assert!(queue.push(QueuedCommand::Exit).is_ok());
        assert!(matches!(
            queue.push(QueuedCommand::Exit),
            Err(CommandEnqueueError::LimitExceeded { limit: 2 })
        ));
        assert!(matches!(
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 0, 1),
            Err(CommandCommitError::Batch(
                CommandBatchError::LimitExceeded { limit: 2 }
            ))
        ));
        assert!(queue.is_clean());
    }

    #[test]
    fn pause_requests_are_inline_last_write_wins_and_use_exact_slots() {
        let (mut world, mut approved, application, generation) = setup();
        let mut queue = CommandQueue::new(3);
        assert!(queue.push(QueuedCommand::SetPaused(true)).is_ok());
        assert!(queue.push(QueuedCommand::spawn(Marker(4))).is_ok());
        assert!(queue.push(QueuedCommand::SetPaused(false)).is_ok());
        let capacity = queue.command_capacity();

        let committed = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 0, 1)
            .expect("an exactly fitting pause/spawn batch should commit");

        assert_eq!(committed.pause, Some(false));
        assert!(!committed.exit_requested);
        assert_eq!(committed.spawned, 1);
        assert!(matches!(
            world.query::<&Marker>().single(&world).map(|value| value.0),
            Ok(4)
        ));
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), capacity);

        assert!(queue.push(QueuedCommand::SetPaused(true)).is_ok());
        assert!(queue.push(QueuedCommand::SetPaused(true)).is_ok());
        assert!(queue.push(QueuedCommand::SetPaused(true)).is_ok());
        assert!(matches!(
            queue.push(QueuedCommand::SetPaused(false)),
            Err(CommandEnqueueError::LimitExceeded { limit: 3 })
        ));
        assert!(matches!(
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 1, 1),
            Err(CommandCommitError::Batch(
                CommandBatchError::LimitExceeded { limit: 3 }
            ))
        ));
        assert!(queue.is_clean());
    }

    #[test]
    fn tracked_count_underflow_rejects_before_mutation() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn(Marker(1)).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let mut queue = CommandQueue::new(1);
        assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());
        let command_capacity = queue.command_capacity();

        let result =
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 0, 1);

        assert!(matches!(result, Err(CommandCommitError::RuntimeInvariant)));
        assert!(world.entities().contains(raw));
        assert_eq!(managed_entity_count(&world), 1);
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), command_capacity);
        assert!(queue.despawn_capacity() >= 1);
    }

    #[test]
    fn duplicate_despawn_rejects_spawn_too() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn(Marker(1)).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let mut queue = CommandQueue::new(4);
        assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());
        assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());
        assert!(queue.push(QueuedCommand::spawn(Marker(2))).is_ok());
        let command_capacity = queue.command_capacity();

        let result =
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 1, 8);
        assert!(matches!(
            result,
            Err(CommandCommitError::Batch(
                CommandBatchError::DuplicateDespawn { .. }
            ))
        ));
        assert_eq!(managed_entity_count(&world), 1);
        assert_eq!(world.get::<Marker>(raw).map(|marker| marker.0), Some(1));
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), command_capacity);
        assert!(queue.despawn_capacity() >= 2);
    }

    #[test]
    fn foreign_generation_is_rejected() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn(Marker(1)).id();
        world
            .entity_mut(raw)
            .insert(ManagedEntity::new(LogicEntity::new(
                application,
                generation,
                raw,
            )));
        let foreign = LogicEntity::new(application, WorldGeneration::new(application, 2), raw);
        let mut queue = CommandQueue::new(2);
        assert!(queue.push(QueuedCommand::Despawn(foreign)).is_ok());
        let command_capacity = queue.command_capacity();

        let result =
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 1, 8);
        assert!(matches!(
            result,
            Err(CommandCommitError::Batch(
                CommandBatchError::ForeignEntity { .. }
            ))
        ));
        assert_eq!(managed_entity_count(&world), 1);
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), command_capacity);
        assert!(queue.despawn_capacity() >= 1);
    }

    #[test]
    fn target_validation_rejects_mismatched_private_provenance() {
        let (mut world, mut approved, application, generation) = setup();
        let corrupted = WorldGeneration::new(application, 99);
        let raw = world
            .spawn((Marker(1), ManagedEntity::for_generation(corrupted)))
            .id();
        let entity = LogicEntity::new(application, generation, raw);
        let mut queue = CommandQueue::new(1);
        assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());

        assert!(matches!(
            queue.validate_and_apply(
                &mut world,
                &mut approved,
                application,
                generation,
                1,
                8,
            ),
            Err(CommandCommitError::Batch(CommandBatchError::MissingEntity {
                entity: rejected
            })) if rejected == entity
        ));
        assert!(world.entities().contains(raw));
        assert_eq!(managed_entity_count(&world), 1);
    }

    #[test]
    fn overflow_poison_rejects_retained_commands() {
        let (mut world, mut approved, application, generation) = setup();
        let mut queue = CommandQueue::new(1);
        let retained_drops = Arc::new(AtomicUsize::new(0));
        let rejected_drops = Arc::new(AtomicUsize::new(0));
        assert!(
            queue
                .push(QueuedCommand::spawn(DropMarker(Arc::clone(
                    &retained_drops,
                ))))
                .is_ok()
        );
        let command_capacity = queue.command_capacity();
        assert!(matches!(
            queue.push(QueuedCommand::spawn(DropMarker(Arc::clone(
                &rejected_drops,
            )))),
            Err(CommandEnqueueError::LimitExceeded { limit: 1 })
        ));
        assert_eq!(rejected_drops.load(Ordering::SeqCst), 1);
        assert_eq!(retained_drops.load(Ordering::SeqCst), 0);

        let result =
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 0, 8);
        assert!(matches!(
            result,
            Err(CommandCommitError::Batch(
                CommandBatchError::LimitExceeded { limit: 1 }
            ))
        ));
        assert_eq!(managed_entity_count(&world), 0);
        assert_eq!(retained_drops.load(Ordering::SeqCst), 1);
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), command_capacity);
    }

    #[test]
    fn disabled_entities_still_consume_the_hard_command_limit() {
        let (mut world, mut approved, application, generation) = setup();
        let raw = world.spawn((Marker(1), Disabled)).id();
        let entity = LogicEntity::new(application, generation, raw);
        world.entity_mut(raw).insert(ManagedEntity::new(entity));
        let mut queue = CommandQueue::new(1);
        assert!(queue.push(QueuedCommand::spawn(Marker(2))).is_ok());
        let command_capacity = queue.command_capacity();

        let result =
            queue.validate_and_apply(&mut world, &mut approved, application, generation, 1, 1);

        assert!(matches!(
            result,
            Err(CommandCommitError::Batch(
                CommandBatchError::EntityLimitExceeded {
                    limit: 1,
                    requested: 2,
                }
            ))
        ));
        assert_eq!(managed_entity_count(&world), 1);
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), command_capacity);
    }

    #[test]
    fn successful_batches_reuse_command_and_despawn_capacity() {
        let (mut world, mut approved, application, generation) = setup();
        let mut entities = Vec::new();
        for value in 0..4 {
            let raw = world.spawn(Marker(value)).id();
            let entity = LogicEntity::new(application, generation, raw);
            world.entity_mut(raw).insert(ManagedEntity::new(entity));
            entities.push(entity);
        }
        let mut queue = CommandQueue::new(4);

        for entity in &entities {
            assert!(queue.push(QueuedCommand::Despawn(*entity)).is_ok());
        }
        let command_capacity = queue.command_capacity();
        let first = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 4, 4)
            .expect("first warmed batch should commit");
        let despawn_capacity = queue.despawn_capacity();
        assert_eq!(first.despawned, 4);
        assert!(queue.is_clean());
        assert!(despawn_capacity >= 4);

        let mut second_entities = Vec::new();
        for value in 4..8 {
            let raw = world.spawn(Marker(value)).id();
            let entity = LogicEntity::new(application, generation, raw);
            world.entity_mut(raw).insert(ManagedEntity::new(entity));
            second_entities.push(entity);
        }
        for entity in second_entities {
            assert!(queue.push(QueuedCommand::Despawn(entity)).is_ok());
        }
        assert_eq!(queue.command_capacity(), command_capacity);
        let second = queue
            .validate_and_apply(&mut world, &mut approved, application, generation, 4, 4)
            .expect("second equal-sized batch should commit");
        assert_eq!(second.despawned, 4);
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), command_capacity);
        assert_eq!(queue.despawn_capacity(), despawn_capacity);
    }

    #[test]
    fn discard_drops_payloads_and_retains_command_capacity() {
        let drops = Arc::new(AtomicUsize::new(0));
        let mut queue = CommandQueue::new(2);
        assert!(
            queue
                .push(QueuedCommand::spawn(DropMarker(Arc::clone(&drops))))
                .is_ok()
        );
        let command_capacity = queue.command_capacity();

        queue.discard();

        assert_eq!(drops.load(Ordering::SeqCst), 1);
        assert!(queue.is_clean());
        assert_eq!(queue.command_capacity(), command_capacity);
    }
}