systemless 0.37.0

High-Level Emulation for classic Macintosh applications
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
//! CPU-free execution continuation state.
//!
//! This module owns the semantic identity and lifecycle of a guest call. It
//! deliberately knows nothing about either CPU's registers, parked context,
//! import actions, or ABI. Those details remain at the compatibility edges
//! until the execution runner can ask this store to schedule an adapter.
//!
//! A continuation is owned by an execution task and is consumed in LIFO order
//! within that task. Every mutating transition validates the whole request
//! before changing the store, so a stale task or call ID cannot accidentally
//! consume a different continuation.

use std::cell::RefCell;
use std::collections::{HashMap, HashSet, VecDeque};
use std::rc::Rc;

use crate::guest_procedure::GuestIsa;

/// Stable execution-task identity used by the continuation owner.
///
/// Thread Manager IDs are already stable and process-local, so cooperative
/// tasks use their guest-visible ID directly. The application task is ID 2,
/// matching `kApplicationThreadID` from Threads.h. Inside Macintosh:
/// Thread Manager (1999), pp. 47--48.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) struct ExecutionTaskId(u32);

impl ExecutionTaskId {
    pub(crate) const APPLICATION: Self = Self(2);

    pub(crate) const fn from_thread_id(thread_id: u32) -> Self {
        Self(thread_id)
    }

    pub(crate) const fn thread_id(self) -> u32 {
        self.0
    }
}

/// Scheduling state, independent of either engine's saved registers.
/// Inside Macintosh: Thread Manager (1999), pp. 45, 67–70.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ExecutionTaskState {
    Ready,
    Stopped,
    Running,
}

/// Available native coordinator contexts, observed without borrowing a CPU.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct NativeAvailability {
    pub(crate) application: bool,
    pub(crate) companion: bool,
    pub(crate) staged_companion: bool,
}

/// Which execution coordinator can advance the selected task. A native
/// session can itself advance a classic callback before resuming native code.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ExecutionRoute {
    Classic,
    NativeApplication,
    NativeCompanion,
    PrepareCompanion,
    Blocked,
}

/// A request type that identifies the task which owns its continuation.
///
/// The kernel does not know the request's ABI or payload. Each semantic edge
/// supplies this one task identity projection so `submit` can reject stale
/// task metadata instead of rewriting it.
pub(crate) trait TaskOwned {
    fn task(&self) -> ExecutionTaskId;
}

/// Architecture-neutral destination of one guest procedure invocation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct GuestCallTarget {
    pub(crate) isa: GuestIsa,
    pub(crate) entry: u32,
    pub(crate) rtoc: u32,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct M68kRegisterState {
    pub(crate) data: [u32; 8],
    pub(crate) address: [u32; 7],
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum M68kResultSource {
    Data(u8),
    Address(u8),
    Memory {
        address: u32,
        size: u8,
    },
    SpecialCase {
        selector: u8,
        arguments: PowerPcArguments,
        stack_result: Option<u32>,
    },
}

pub(crate) type PowerPcArguments = GuestArgumentValues;

pub(crate) const MAX_POWERPC_GUEST_ARGUMENTS: usize = 13;

/// Bounded, copyable native argument list carried by a semantic call request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct GuestArgumentValues {
    values: [u32; MAX_POWERPC_GUEST_ARGUMENTS],
    len: u8,
}

impl GuestArgumentValues {
    pub(crate) fn from_slice(values: &[u32]) -> Option<Self> {
        if values.len() > MAX_POWERPC_GUEST_ARGUMENTS {
            return None;
        }
        let mut arguments = Self {
            values: [0; MAX_POWERPC_GUEST_ARGUMENTS],
            len: u8::try_from(values.len()).ok()?,
        };
        arguments.values[..values.len()].copy_from_slice(values);
        Some(arguments)
    }

    pub(crate) fn as_slice(&self) -> &[u32] {
        &self.values[..usize::from(self.len)]
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum M68kResultTarget {
    Data { index: u8, size: u8 },
    Address { index: u8, size: u8 },
    Ccr { mask: u8 },
    Memory { address: u32, size: u8 },
    SpecialCase { selector: u8, scratch: u32 },
}

/// Architecture-neutral policy for placing a guest callback's result in the
/// caller's result slot.
///
/// The architecture adapter translates this policy to its ABI vocabulary.
/// Inside Macintosh: PowerPC System Software (1994), pp. 2-12--2-16.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum GuestCallReturnPolicy {
    Preserve,
    Mask(u32),
    Set(u32),
    ZeroOrSet { zero: u32, nonzero: u32 },
    CrBit(u8),
    XerCa,
    XerOv,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct PowerPcReturnState {
    pub(crate) gpr3: u32,
}

/// The architecture-neutral payload supplied to a guest call.
///
/// A call into native PowerPC code receives its values through the PPC ABI
/// registers/parameter area, while a call into 68K code needs the emulated
/// stack/register interval. Both are represented here so the process-owned
/// continuation stack does not have to infer an ABI from a CPU action.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct M68kCallRequest {
    pub(crate) entry: u32,
    pub(crate) initial_sp: u32,
    pub(crate) final_sp: u32,
    pub(crate) registers: M68kRegisterState,
    pub(crate) result: Option<M68kResultSource>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum GuestCallArguments {
    None,
    PowerPc(PowerPcArguments),
    M68k(M68kCallRequest),
}

/// One architecture-neutral guest procedure request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct GuestCallRequest {
    pub(crate) task: ExecutionTaskId,
    pub(crate) target: GuestCallTarget,
    pub(crate) arguments: GuestCallArguments,
}

impl GuestCallRequest {
    pub(crate) fn new(target: GuestCallTarget) -> Self {
        Self::for_task(ExecutionTaskId::APPLICATION, target)
    }

    pub(crate) fn for_task(task: ExecutionTaskId, target: GuestCallTarget) -> Self {
        Self {
            task,
            target,
            arguments: GuestCallArguments::None,
        }
    }

    pub(crate) fn with_powerpc_arguments(mut self, arguments: PowerPcArguments) -> Self {
        self.arguments = GuestCallArguments::PowerPc(arguments);
        self
    }

    pub(crate) fn with_m68k_request(mut self, request: M68kCallRequest) -> Self {
        self.arguments = GuestCallArguments::M68k(request);
        self
    }
}

impl TaskOwned for GuestCallRequest {
    fn task(&self) -> ExecutionTaskId {
        self.task
    }
}

/// Typed resumption metadata for one guest call.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum GuestCallContinuation {
    ReturnToM68k {
        return_pc: u32,
        final_sp: u32,
        result: Option<M68kResultTarget>,
    },
    ReturnToPowerPc {
        return_pc: u32,
        final_pc: u32,
        restore_rtoc: u32,
        return_gpr3: GuestCallReturnPolicy,
    },
}

impl GuestCallContinuation {
    pub(crate) const fn to_m68k(
        return_pc: u32,
        final_sp: u32,
        result: Option<M68kResultTarget>,
    ) -> Self {
        Self::ReturnToM68k {
            return_pc,
            final_sp,
            result,
        }
    }

    pub(crate) fn to_powerpc(
        return_pc: u32,
        final_pc: u32,
        restore_rtoc: u32,
        return_gpr3: impl Into<GuestCallReturnPolicy>,
    ) -> Self {
        Self::ReturnToPowerPc {
            return_pc,
            final_pc,
            restore_rtoc,
            return_gpr3: return_gpr3.into(),
        }
    }
}

/// Semantic guest-execution effect emitted by either ABI edge.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum GuestCallEffect {
    CallGuest {
        request: GuestCallRequest,
        continuation: GuestCallContinuation,
    },
}

impl GuestCallEffect {
    pub(crate) const fn call_guest(
        request: GuestCallRequest,
        continuation: GuestCallContinuation,
    ) -> Self {
        Self::CallGuest {
            request,
            continuation,
        }
    }

    pub(crate) fn request(self) -> GuestCallRequest {
        match self {
            Self::CallGuest { request, .. } => request,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct PendingPowerPcExecution {
    pub(crate) target: GuestCallTarget,
    pub(crate) arguments: PowerPcArguments,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct PendingM68kExecution {
    pub(crate) entry: u32,
    pub(crate) initial_sp: u32,
    pub(crate) return_pc: u32,
    pub(crate) final_sp: u32,
    pub(crate) registers: M68kRegisterState,
    pub(crate) result: Option<M68kResultSource>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct M68kResume {
    pub(crate) return_pc: u32,
    pub(crate) final_sp: u32,
    pub(crate) result: Option<M68kResultTarget>,
    pub(crate) powerpc: PowerPcReturnState,
}

/// Opaque, monotonically allocated identity for one submitted continuation.
///
/// IDs are process-local and are never reused by a live store. Keeping the
/// value opaque prevents an adapter from manufacturing a continuation token
/// that belongs to another call or task.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct CallId(u64);

/// Lifecycle phase for one continuation.
///
/// `Pending` is the submitted-but-not-yet-started state, `Active` marks the
/// adapter slice currently executing the call, and `Completed` retains the
/// result until the owner retires the frame.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ContinuationPhase {
    Pending,
    Active,
    Completed,
}

/// A CPU-free snapshot of a continuation and its lifecycle state.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ContinuationState<R: Copy, C: Copy> {
    call_id: CallId,
    task: ExecutionTaskId,
    request: R,
    continuation: C,
    phase: ContinuationPhase,
    result: Option<u32>,
}

impl<R: Copy, C: Copy> ContinuationState<R, C> {
    pub(crate) const fn call_id(self) -> CallId {
        self.call_id
    }

    #[cfg(test)]
    pub(crate) const fn request(self) -> R {
        self.request
    }

    pub(crate) const fn phase(self) -> ContinuationPhase {
        self.phase
    }

    #[cfg(test)]
    pub(crate) const fn result(self) -> Option<u32> {
        self.result
    }
}

/// Why a transactional continuation operation was refused.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ContinuationError {
    TaskIdsExhausted,
    TaskUnavailable {
        task: ExecutionTaskId,
    },
    /// The adapter rejected its prepared return without changing live state.
    CommitRefused {
        call_id: CallId,
    },
    TaskCommitRefused {
        task: ExecutionTaskId,
    },
    /// A request's embedded owner disagreed with the task supplied to submit.
    TaskMismatch {
        expected: ExecutionTaskId,
        actual: ExecutionTaskId,
    },
    /// A task other than the selected execution task attempted to advance a
    /// continuation. Task switches must be explicit before execution can be
    /// resumed.
    TaskNotCurrent {
        current: ExecutionTaskId,
        requested: ExecutionTaskId,
    },
    /// The requested call is not the top continuation for its owner task.
    CallIdMismatch {
        task: ExecutionTaskId,
        expected: Option<CallId>,
        actual: CallId,
    },
    /// The call exists, but its lifecycle does not permit the requested
    /// transition.
    InvalidPhase {
        call_id: CallId,
        actual: ContinuationPhase,
        expected: ContinuationPhase,
    },
    /// A concrete adapter context is still parked against the call. The
    /// context must be restored before the semantic continuation can leave
    /// the kernel.
    ContextAttached {
        call_id: CallId,
        count: usize,
    },
    /// A task with suspended continuations cannot be retired.
    RetirementRefused {
        task: ExecutionTaskId,
        depth: usize,
        contexts: usize,
        current: bool,
    },
    /// The monotonic ID namespace is exhausted. This is practically
    /// unreachable, but retaining it makes submission transactional even at
    /// the numeric boundary.
    CallIdExhausted,
}

/// A task-indexed, CPU-free continuation store for one Macintosh process.
///
/// `Clone` snapshots the state into an independent store. `shared_handle`
/// explicitly opts into sharing the live state, matching the compatibility
/// stack's attach/share distinction while making ownership visible to callers.
#[derive(Debug)]
pub(crate) struct ContinuationStore<R: Copy, C: Copy>(Rc<RefCell<StoreState<R, C>>>);

#[derive(Clone, Debug, Eq, PartialEq)]
struct StoreState<R: Copy, C: Copy> {
    current_task: ExecutionTaskId,
    next_call_id: u64,
    next_task_id: Option<u32>,
    stacks: HashMap<ExecutionTaskId, Vec<ContinuationState<R, C>>>,
    attached_contexts: HashMap<CallId, usize>,
    retired_tasks: HashSet<ExecutionTaskId>,
    task_states: HashMap<ExecutionTaskId, ExecutionTaskState>,
    task_entry_isas: HashMap<ExecutionTaskId, GuestIsa>,
    ready: VecDeque<ExecutionTaskId>,
    critical_depth: u32,
}

impl<R: Copy, C: Copy> Default for StoreState<R, C> {
    fn default() -> Self {
        Self {
            current_task: ExecutionTaskId::APPLICATION,
            next_call_id: 1,
            next_task_id: Some(3),
            stacks: HashMap::from([(ExecutionTaskId::APPLICATION, Vec::new())]),
            attached_contexts: HashMap::new(),
            retired_tasks: HashSet::new(),
            task_states: HashMap::from([(
                ExecutionTaskId::APPLICATION,
                ExecutionTaskState::Running,
            )]),
            task_entry_isas: HashMap::new(),
            ready: VecDeque::new(),
            critical_depth: 0,
        }
    }
}

impl<R: Copy, C: Copy> Default for ContinuationStore<R, C> {
    fn default() -> Self {
        Self(Rc::new(RefCell::new(StoreState::default())))
    }
}

impl<R: Copy, C: Copy> Clone for ContinuationStore<R, C> {
    fn clone(&self) -> Self {
        Self(Rc::new(RefCell::new(self.0.borrow().clone())))
    }
}

impl<R: Copy + Eq, C: Copy + Eq> PartialEq for ContinuationStore<R, C> {
    fn eq(&self, other: &Self) -> bool {
        *self.0.borrow() == *other.0.borrow()
    }
}

impl<R: Copy + Eq, C: Copy + Eq> Eq for ContinuationStore<R, C> {}

impl<R: Copy + TaskOwned, C: Copy> ContinuationStore<R, C> {
    /// Return a handle sharing this store's live state.
    pub(crate) fn shared_handle(&self) -> Self {
        Self(Rc::clone(&self.0))
    }

    /// Current execution task selected for continuation transitions.
    pub(crate) fn current_task(&self) -> ExecutionTaskId {
        self.0.borrow().current_task
    }

    /// Construction-time adoption is safe only before this execution owner
    /// has acquired any task, call identity, or scheduling history.
    pub(crate) fn is_pristine(&self) -> bool {
        let state = self.0.borrow();
        state.current_task == ExecutionTaskId::APPLICATION
            && state.next_call_id == 1
            && state.next_task_id == Some(3)
            && state.stacks.len() == 1
            && state
                .stacks
                .get(&ExecutionTaskId::APPLICATION)
                .is_some_and(Vec::is_empty)
            && state.attached_contexts.is_empty()
            && state.retired_tasks.is_empty()
            && state.task_entry_isas.is_empty()
            && state.task_states.get(&ExecutionTaskId::APPLICATION)
                == Some(&ExecutionTaskState::Running)
            && state.ready.is_empty()
            && state.critical_depth == 0
    }

    /// Allocate from the same monotonic namespace used by explicit registration.
    /// Exhaustion never wraps into aliases, the application task or retired IDs.
    #[cfg(test)]
    pub(crate) fn create_task(&self) -> Result<ExecutionTaskId, ContinuationError> {
        self.create_task_with(|_| true)
    }

    /// The synchronous commit must not reenter this owner or change state on
    /// failure. Publish guest output only after preparation, before registration.
    pub(crate) fn create_task_with(
        &self,
        commit: impl FnOnce(ExecutionTaskId) -> bool,
    ) -> Result<ExecutionTaskId, ContinuationError> {
        let id = self
            .0
            .borrow()
            .next_task_id
            .ok_or(ContinuationError::TaskIdsExhausted)?;
        let task = ExecutionTaskId::from_thread_id(id);
        if !commit(task) {
            return Err(ContinuationError::TaskCommitRefused { task });
        }
        self.register_task(task)?;
        Ok(task)
    }

    /// Register a new task exactly once for this process lifetime.
    pub(crate) fn register_task(&self, task: ExecutionTaskId) -> Result<(), ContinuationError> {
        let mut state = self.0.borrow_mut();
        if state.stacks.contains_key(&task) || state.retired_tasks.contains(&task) {
            return Err(ContinuationError::TaskUnavailable { task });
        }
        state.stacks.insert(task, Vec::new());
        state.task_states.insert(task, ExecutionTaskState::Stopped);
        state.task_entry_isas.insert(task, GuestIsa::M68k);
        if state
            .next_task_id
            .is_some_and(|next| task.thread_id() >= next)
        {
            state.next_task_id = task.thread_id().checked_add(1);
        }
        Ok(())
    }

    /// Select an existing task without manufacturing or resurrecting it.
    pub(crate) fn switch_to_task(&self, task: ExecutionTaskId) -> Result<(), ContinuationError> {
        let mut state = self.0.borrow_mut();
        if !state.stacks.contains_key(&task) {
            return Err(ContinuationError::TaskUnavailable { task });
        }
        if state.current_task != task {
            if state.task_states.get(&task) != Some(&ExecutionTaskState::Ready) {
                return Err(ContinuationError::TaskUnavailable { task });
            }
            if state.critical_depth != 0 {
                return Err(ContinuationError::TaskUnavailable { task });
            }
            let previous = state.current_task;
            if state.task_states.get(&previous) == Some(&ExecutionTaskState::Running) {
                state
                    .task_states
                    .insert(previous, ExecutionTaskState::Ready);
                if !state.ready.contains(&previous) {
                    state.ready.push_back(previous);
                }
            }
        }
        state.ready.retain(|queued| *queued != task);
        state.task_states.insert(task, ExecutionTaskState::Running);
        state.current_task = task;
        Ok(())
    }

    /// Initial application binding may accompany adopted pending calls. Once
    /// bound, an entry architecture changes only when that task has no calls.
    pub(crate) fn bind_task_entry_isa(&self, task: ExecutionTaskId, isa: GuestIsa) -> bool {
        let mut state = self.0.borrow_mut();
        let Some(stack) = state.stacks.get(&task) else {
            return false;
        };
        if state
            .task_entry_isas
            .get(&task)
            .is_some_and(|old| *old != isa)
            && !stack.is_empty()
        {
            return false;
        }
        state.task_entry_isas.insert(task, isa);
        true
    }

    pub(crate) fn task_entry_isa(&self, task: ExecutionTaskId) -> Option<GuestIsa> {
        let state = self.0.borrow();
        state.stacks.contains_key(&task).then(|| {
            state
                .task_entry_isas
                .get(&task)
                .copied()
                .unwrap_or(GuestIsa::M68k)
        })
    }

    /// Unlike legacy execution routing, observations must distinguish an
    /// explicitly bound ISA from the historical implicit classic default.
    pub(crate) fn bound_task_entry_isa(&self, task: ExecutionTaskId) -> Option<GuestIsa> {
        self.0.borrow().task_entry_isas.get(&task).copied()
    }

    pub(crate) fn has_live_workers(&self) -> bool {
        self.0
            .borrow()
            .task_states
            .keys()
            .any(|task| *task != ExecutionTaskId::APPLICATION)
    }

    pub(crate) fn scheduling_state(&self, task: ExecutionTaskId) -> Option<ExecutionTaskState> {
        self.0.borrow().task_states.get(&task).copied()
    }

    pub(crate) fn set_scheduling_state(
        &self,
        task: ExecutionTaskId,
        requested: ExecutionTaskState,
    ) -> bool {
        self.change_scheduling_state(task, requested, false)
    }

    /// SetThreadStateEndCritical is one state transition, including failures.
    /// Inside Macintosh: Thread Manager (1999), pp. 71--72.
    #[cfg(test)]
    pub(crate) fn set_state_ending_critical(
        &self,
        task: ExecutionTaskId,
        requested: ExecutionTaskState,
    ) -> bool {
        self.change_scheduling_state(task, requested, true)
    }

    fn change_scheduling_state(
        &self,
        task: ExecutionTaskId,
        requested: ExecutionTaskState,
        end_critical: bool,
    ) -> bool {
        let mut state = self.0.borrow_mut();
        let depth = if end_critical {
            let Some(depth) = state.critical_depth.checked_sub(1) else {
                return false;
            };
            depth
        } else {
            state.critical_depth
        };
        if !state.stacks.contains_key(&task)
            || (requested == ExecutionTaskState::Running && task != state.current_task)
            || (task == state.current_task
                && requested != ExecutionTaskState::Running
                && depth != 0)
        {
            return false;
        }
        state.critical_depth = depth;
        state.ready.retain(|queued| *queued != task);
        state.task_states.insert(task, requested);
        if requested == ExecutionTaskState::Ready {
            state.ready.push_back(task);
        }
        true
    }

    /// Validate state, critical depth and successor before the adapter commits
    /// its return. The callback is synchronous and must not reenter this store.
    /// Inside Macintosh: Thread Manager (1999), pp. 67–72.
    pub(crate) fn change_thread_state_with(
        &self,
        task: ExecutionTaskId,
        requested: ExecutionTaskState,
        suggested: Option<ExecutionTaskId>,
        end_critical: bool,
        commit: impl FnOnce(Option<ExecutionTaskId>) -> bool,
    ) -> Option<Option<ExecutionTaskId>> {
        let mut state = self.0.borrow_mut();
        let depth = if end_critical {
            state.critical_depth.checked_sub(1)?
        } else {
            state.critical_depth
        };
        if !state.stacks.contains_key(&task)
            || (requested == ExecutionTaskState::Running && task != state.current_task)
        {
            return None;
        }
        let switching = task == state.current_task && requested != ExecutionTaskState::Running;
        if switching && depth != 0 {
            return None;
        }
        let successor = if switching {
            let eligible = |candidate: ExecutionTaskId| {
                candidate != task
                    && state.task_states.get(&candidate) == Some(&ExecutionTaskState::Ready)
            };
            let next = suggested
                .filter(|candidate| eligible(*candidate))
                .or_else(|| {
                    state
                        .ready
                        .iter()
                        .copied()
                        .find(|candidate| eligible(*candidate))
                });
            next
        } else {
            None
        };
        if !commit(successor) {
            return None;
        }
        state.critical_depth = depth;
        state.ready.retain(|queued| *queued != task);
        let final_state =
            if switching && successor.is_none() && requested == ExecutionTaskState::Ready {
                ExecutionTaskState::Running
            } else {
                requested
            };
        state.task_states.insert(task, final_state);
        if final_state == ExecutionTaskState::Ready {
            state.ready.push_back(task);
        }
        if let Some(next) = successor {
            state.ready.retain(|queued| *queued != next);
            state.task_states.insert(next, ExecutionTaskState::Running);
            state.current_task = next;
        }
        Some(successor)
    }

    /// Selection is non-destructive: the adapter must first validate that it
    /// can install the successor. Only the committed switch removes it.
    pub(crate) fn next_ready_task(
        &self,
        suggested: Option<ExecutionTaskId>,
    ) -> Option<ExecutionTaskId> {
        self.next_ready_task_after_critical(suggested, false)
    }

    pub(crate) fn next_ready_task_after_critical(
        &self,
        suggested: Option<ExecutionTaskId>,
        end_critical: bool,
    ) -> Option<ExecutionTaskId> {
        let state = self.0.borrow();
        let depth = state.critical_depth.checked_sub(u32::from(end_critical))?;
        if depth != 0 {
            return None;
        }
        let eligible = |task: ExecutionTaskId| {
            task != state.current_task
                && state.task_states.get(&task) == Some(&ExecutionTaskState::Ready)
        };
        suggested
            .filter(|task| eligible(*task))
            .or_else(|| state.ready.iter().copied().find(|task| eligible(*task)))
    }

    pub(crate) fn critical_depth(&self) -> u32 {
        self.0.borrow().critical_depth
    }

    pub(crate) fn begin_critical(&self) {
        let mut state = self.0.borrow_mut();
        state.critical_depth = state.critical_depth.saturating_add(1);
    }

    pub(crate) fn end_critical(&self) -> bool {
        let mut state = self.0.borrow_mut();
        let Some(depth) = state.critical_depth.checked_sub(1) else {
            return false;
        };
        state.critical_depth = depth;
        true
    }

    /// Submit a continuation to `task` and allocate its explicit call ID.
    ///
    /// The request already carries an owner task. A disagreement is rejected
    /// instead of being rewritten to the currently selected task, which keeps
    /// stale ABI data from silently crossing a cooperative task boundary.
    pub(crate) fn submit(
        &self,
        task: ExecutionTaskId,
        request: R,
        continuation: C,
    ) -> Result<CallId, ContinuationError> {
        let mut state = self.0.borrow_mut();
        if request.task() != task {
            return Err(ContinuationError::TaskMismatch {
                expected: request.task(),
                actual: task,
            });
        }
        if !state.stacks.contains_key(&task) {
            return Err(ContinuationError::TaskUnavailable { task });
        }
        let Some(next_call_id) = state.next_call_id.checked_add(1) else {
            return Err(ContinuationError::CallIdExhausted);
        };
        let call_id = CallId(state.next_call_id);
        state.next_call_id = next_call_id;
        state
            .stacks
            .get_mut(&task)
            .expect("registered task")
            .push(ContinuationState {
                call_id,
                task,
                request,
                continuation,
                phase: ContinuationPhase::Pending,
                result: None,
            });
        Ok(call_id)
    }

    /// Mark the top continuation active after validating owner task, call ID,
    /// and phase. A failed validation leaves every field unchanged.
    pub(crate) fn activate(
        &self,
        task: ExecutionTaskId,
        call_id: CallId,
    ) -> Result<ContinuationState<R, C>, ContinuationError> {
        let mut state = self.0.borrow_mut();
        Self::validate_transition(&state, task, call_id, ContinuationPhase::Pending)?;
        let frame = state
            .stacks
            .get_mut(&task)
            .and_then(|stack| stack.last_mut())
            .expect("validated continuation must remain present");
        frame.phase = ContinuationPhase::Active;
        Ok(*frame)
    }

    fn activate_attaching_context(
        &self,
        task: ExecutionTaskId,
        call_id: CallId,
    ) -> Result<ContinuationState<R, C>, ContinuationError> {
        let mut state = self.0.borrow_mut();
        Self::validate_transition(&state, task, call_id, ContinuationPhase::Pending)?;
        let frame = state
            .stacks
            .get_mut(&task)
            .and_then(|stack| stack.last_mut())
            .expect("validated continuation must remain present");
        frame.phase = ContinuationPhase::Active;
        let frame = *frame;
        *state.attached_contexts.entry(call_id).or_default() += 1;
        Ok(frame)
    }

    /// Complete the active top continuation with an optional neutral result.
    /// The result is retained until [`Self::retire`] consumes the frame.
    pub(crate) fn complete(
        &self,
        task: ExecutionTaskId,
        call_id: CallId,
        result: Option<u32>,
    ) -> Result<ContinuationState<R, C>, ContinuationError> {
        let mut state = self.0.borrow_mut();
        Self::validate_transition(&state, task, call_id, ContinuationPhase::Active)?;
        let frame = state
            .stacks
            .get_mut(&task)
            .and_then(|stack| stack.last_mut())
            .expect("validated continuation must remain present");
        frame.phase = ContinuationPhase::Completed;
        frame.result = result;
        Ok(*frame)
    }

    fn complete_detaching_context(
        &self,
        task: ExecutionTaskId,
        call_id: CallId,
        result: Option<u32>,
    ) -> Result<ContinuationState<R, C>, ContinuationError> {
        let mut state = self.0.borrow_mut();
        Self::validate_transition(&state, task, call_id, ContinuationPhase::Active)?;
        let Some(context_count) = state.attached_contexts.get(&call_id).copied() else {
            return Err(ContinuationError::ContextAttached { call_id, count: 0 });
        };
        let frame = state
            .stacks
            .get_mut(&task)
            .and_then(|stack| stack.last_mut())
            .expect("validated continuation must remain present");
        frame.phase = ContinuationPhase::Completed;
        frame.result = result;
        let frame = *frame;
        if context_count == 1 {
            state.attached_contexts.remove(&call_id);
        } else {
            state.attached_contexts.insert(call_id, context_count - 1);
        }
        Ok(frame)
    }

    /// Retire a completed top continuation after validating its exact ID.
    ///
    /// Retirement is separate from completion so a scheduler can observe the
    /// result before the frame leaves the task's LIFO stack.
    pub(crate) fn retire(
        &self,
        task: ExecutionTaskId,
        call_id: CallId,
    ) -> Result<ContinuationState<R, C>, ContinuationError> {
        let mut state = self.0.borrow_mut();
        Self::validate_transition(&state, task, call_id, ContinuationPhase::Completed)?;
        if let Some(count) = state.attached_contexts.get(&call_id).copied() {
            return Err(ContinuationError::ContextAttached { call_id, count });
        }
        Ok(state
            .stacks
            .get_mut(&task)
            .and_then(Vec::pop)
            .expect("validated continuation must remain present"))
    }

    /// Withdraw a still-pending continuation while an adapter setup operation
    /// rolls back. This is intentionally narrower than [`Self::retire`]: an
    /// active or completed call must remain visible until its normal return.
    pub(crate) fn cancel_pending(
        &self,
        task: ExecutionTaskId,
        call_id: CallId,
    ) -> Result<ContinuationState<R, C>, ContinuationError> {
        let mut state = self.0.borrow_mut();
        Self::validate_transition(&state, task, call_id, ContinuationPhase::Pending)?;
        if let Some(count) = state.attached_contexts.get(&call_id).copied() {
            return Err(ContinuationError::ContextAttached { call_id, count });
        }
        Ok(state
            .stacks
            .get_mut(&task)
            .and_then(Vec::pop)
            .expect("validated continuation must remain present"))
    }

    /// Retire an execution task only after its continuation stack is empty.
    ///
    /// A selected task is also refused, even when empty, because removing its
    /// stack would leave the task cursor dangling. Switch to a surviving task
    /// first, then call this method.
    #[cfg(test)]
    pub(crate) fn retire_task(&self, task: ExecutionTaskId) -> Result<(), ContinuationError> {
        self.retire_task_with(task, None, || true)
    }

    /// Validate retirement and optional replacement before committing external
    /// result storage. The callback must be synchronous, non-reentrant and
    /// leave external state unchanged on failure; it cannot execute guest code.
    pub(crate) fn retire_task_with(
        &self,
        task: ExecutionTaskId,
        successor: Option<ExecutionTaskId>,
        commit: impl FnOnce() -> bool,
    ) -> Result<(), ContinuationError> {
        let mut state = self.0.borrow_mut();
        let Some(stack) = state.stacks.get(&task) else {
            return Err(ContinuationError::TaskUnavailable { task });
        };
        let depth = stack.len();
        let contexts = state
            .stacks
            .get(&task)
            .into_iter()
            .flatten()
            .map(|continuation| {
                state
                    .attached_contexts
                    .get(&continuation.call_id)
                    .copied()
                    .unwrap_or(0)
            })
            .sum();
        let current = state.current_task == task;
        if (current && successor.is_none()) || depth != 0 || contexts != 0 {
            return Err(ContinuationError::RetirementRefused {
                task,
                depth,
                contexts,
                current,
            });
        }
        if let Some(next) = successor {
            if !current
                || next == task
                || !state.stacks.contains_key(&next)
                || state.task_states.get(&next) != Some(&ExecutionTaskState::Ready)
                || state.critical_depth != 0
            {
                return Err(ContinuationError::TaskUnavailable { task: next });
            }
        }
        if !commit() {
            return Err(ContinuationError::TaskCommitRefused { task });
        }
        if let Some(next) = successor {
            state.current_task = next;
            state.task_states.insert(next, ExecutionTaskState::Running);
            state.ready.retain(|queued| *queued != next);
        }
        state.stacks.remove(&task);
        state.retired_tasks.insert(task);
        state.task_states.remove(&task);
        state.task_entry_isas.remove(&task);
        state.ready.retain(|queued| *queued != task);
        Ok(())
    }

    /// Return the current top continuation for `task`, without changing its
    /// phase or stack.
    pub(crate) fn peek(&self, task: ExecutionTaskId) -> Option<ContinuationState<R, C>> {
        self.0
            .borrow()
            .stacks
            .get(&task)
            .and_then(|stack| stack.last().copied())
    }

    pub(crate) fn task_depth(&self, task: ExecutionTaskId) -> usize {
        self.0.borrow().stacks.get(&task).map_or(0, Vec::len)
    }

    pub(crate) fn depth(&self) -> usize {
        self.task_depth(self.current_task())
    }

    pub(crate) fn len(&self) -> usize {
        self.0.borrow().stacks.values().map(Vec::len).sum()
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[cfg(test)]
    pub(crate) fn task_is_empty(&self, task: ExecutionTaskId) -> bool {
        let state = self.0.borrow();
        state.stacks.get(&task).is_none_or(Vec::is_empty)
    }

    /// Snapshot one task's stack in bottom-to-top order. The semantic layer
    /// exposes values rather than internal borrows so an ABI adapter can
    /// inspect nesting without holding the store across a transition.
    pub(crate) fn task_states(&self, task: ExecutionTaskId) -> Vec<ContinuationState<R, C>> {
        self.0
            .borrow()
            .stacks
            .get(&task)
            .map_or_else(Vec::new, |stack| stack.clone())
    }

    #[cfg(test)]
    fn attach_context(
        &self,
        task: ExecutionTaskId,
        call_id: CallId,
    ) -> Result<(), ContinuationError> {
        let mut state = self.0.borrow_mut();
        Self::validate_context_owner(&state, task, call_id)?;
        *state.attached_contexts.entry(call_id).or_default() += 1;
        Ok(())
    }

    #[cfg(test)]
    fn detach_context(
        &self,
        task: ExecutionTaskId,
        call_id: CallId,
    ) -> Result<(), ContinuationError> {
        let mut state = self.0.borrow_mut();
        Self::validate_context_owner(&state, task, call_id)?;
        let Some(count) = state.attached_contexts.get_mut(&call_id) else {
            return Err(ContinuationError::ContextAttached { call_id, count: 0 });
        };
        *count -= 1;
        if *count == 0 {
            state.attached_contexts.remove(&call_id);
        }
        Ok(())
    }

    fn validate_context_owner(
        state: &StoreState<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
    ) -> Result<(), ContinuationError> {
        let Some(owner) = Self::owner_of(state, call_id) else {
            return Err(ContinuationError::CallIdMismatch {
                task,
                expected: state
                    .stacks
                    .get(&task)
                    .and_then(|stack| stack.last())
                    .map(|frame| frame.call_id()),
                actual: call_id,
            });
        };
        if owner != task {
            return Err(ContinuationError::TaskMismatch {
                expected: owner,
                actual: task,
            });
        }
        if state.current_task != task {
            return Err(ContinuationError::TaskNotCurrent {
                current: state.current_task,
                requested: task,
            });
        }
        Ok(())
    }

    fn validate_transition(
        state: &StoreState<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
        expected_phase: ContinuationPhase,
    ) -> Result<(), ContinuationError> {
        if let Some(owner) = Self::owner_of(state, call_id) {
            if owner != task {
                return Err(ContinuationError::TaskMismatch {
                    expected: owner,
                    actual: task,
                });
            }
        }
        if state.current_task != task {
            return Err(ContinuationError::TaskNotCurrent {
                current: state.current_task,
                requested: task,
            });
        }
        let stack = state.stacks.get(&task);
        let top = stack.and_then(|stack| stack.last());
        if top.map(|frame| frame.call_id()) != Some(call_id) {
            return Err(ContinuationError::CallIdMismatch {
                task,
                expected: top.map(|frame| frame.call_id()),
                actual: call_id,
            });
        }
        let phase = top
            .expect("the top continuation was validated with the call ID")
            .phase;
        if phase != expected_phase {
            return Err(ContinuationError::InvalidPhase {
                call_id,
                actual: phase,
                expected: expected_phase,
            });
        }
        Ok(())
    }

    fn owner_of(state: &StoreState<R, C>, call_id: CallId) -> Option<ExecutionTaskId> {
        state.stacks.iter().find_map(|(task, stack)| {
            stack
                .iter()
                .any(|frame| frame.call_id == call_id)
                .then_some(*task)
        })
    }
}

/// Concrete adapter state parked against an exact semantic continuation.
///
/// The bank is generic so the execution kernel remains CPU-free. It owns the
/// `(task, call)` association and registers every parked value with the
/// continuation store, while an ISA adapter supplies the concrete context.
/// Mixed Mode switch frames preserve nonvolatile state until the matching
/// return crosses the mode boundary; they are linked in exact LIFO order.
/// Inside Macintosh: PowerPC System Software (1994), pp. 2-9--2-13.
#[derive(Clone, Debug)]
pub(crate) struct ExecutionContextBank<T> {
    by_call: HashMap<(ExecutionTaskId, CallId), T>,
}

/// Process-task snapshots keyed by the same stable identities as
/// continuations and parked ISA contexts.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct ExecutionTaskContextBank<T> {
    by_task: HashMap<ExecutionTaskId, T>,
}

impl<T> Default for ExecutionContextBank<T> {
    fn default() -> Self {
        Self {
            by_call: HashMap::new(),
        }
    }
}

impl<T> Default for ExecutionTaskContextBank<T> {
    fn default() -> Self {
        Self {
            by_task: HashMap::new(),
        }
    }
}

impl<T> ExecutionTaskContextBank<T> {
    pub(crate) fn same_tasks<U>(&self, other: &ExecutionTaskContextBank<U>) -> bool {
        self.by_task.len() == other.by_task.len()
            && self
                .by_task
                .keys()
                .all(|task| other.by_task.contains_key(task))
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.by_task.is_empty()
    }

    pub(crate) fn get(&self, task: ExecutionTaskId) -> Option<&T> {
        self.by_task.get(&task)
    }

    pub(crate) fn insert(&mut self, task: ExecutionTaskId, context: T) -> Option<T> {
        self.by_task.insert(task, context)
    }

    pub(crate) fn remove(&mut self, task: ExecutionTaskId) -> Option<T> {
        self.by_task.remove(&task)
    }
}

impl<T> ExecutionContextBank<T> {
    pub(crate) fn get(&self, task: ExecutionTaskId, call_id: CallId) -> Option<&T> {
        self.by_call.get(&(task, call_id))
    }

    pub(crate) fn contains(&self, task: ExecutionTaskId, call_id: CallId) -> bool {
        self.by_call.contains_key(&(task, call_id))
    }

    #[cfg(test)]
    pub(crate) fn len(&self) -> usize {
        self.by_call.len()
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.by_call.is_empty()
    }

    #[cfg(test)]
    pub(crate) fn task_len(&self, task: ExecutionTaskId) -> usize {
        self.by_call
            .keys()
            .filter(|(owner, _)| *owner == task)
            .count()
    }

    #[cfg(test)]
    pub(crate) fn park<R: Copy + TaskOwned, C: Copy>(
        &mut self,
        kernel: &ContinuationStore<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
        context: T,
    ) -> Result<(), (ContinuationError, T)> {
        if self.contains(task, call_id) {
            return Err((
                ContinuationError::ContextAttached { call_id, count: 1 },
                context,
            ));
        }
        if let Err(error) = kernel.attach_context(task, call_id) {
            return Err((error, context));
        }
        let replaced = self.by_call.insert((task, call_id), context);
        debug_assert!(replaced.is_none());
        Ok(())
    }

    /// Attach both sides of an ISA transition under one validated call token.
    /// Refusal leaves the installed caller and both banks unchanged.
    pub(crate) fn park_pair_while_activating<R: Copy + TaskOwned, C: Copy, U: Default>(
        &mut self,
        caller_bank: &mut ExecutionContextBank<U>,
        kernel: &ContinuationStore<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
        context: T,
        caller: &mut U,
    ) -> Result<(), (ContinuationError, T)> {
        let mut state = kernel.0.borrow_mut();
        if let Err(error) = ContinuationStore::validate_transition(
            &state,
            task,
            call_id,
            ContinuationPhase::Pending,
        ) {
            return Err((error, context));
        }
        if self.contains(task, call_id) || caller_bank.contains(task, call_id) {
            return Err((
                ContinuationError::ContextAttached { call_id, count: 1 },
                context,
            ));
        }
        let replacement = U::default();
        state
            .stacks
            .get_mut(&task)
            .and_then(|stack| stack.last_mut())
            .expect("validated activation")
            .phase = ContinuationPhase::Active;
        *state.attached_contexts.entry(call_id).or_default() += 2;
        self.by_call.insert((task, call_id), context);
        caller_bank
            .by_call
            .insert((task, call_id), std::mem::replace(caller, replacement));
        Ok(())
    }

    pub(crate) fn park_while_activating<R: Copy + TaskOwned, C: Copy>(
        &mut self,
        kernel: &ContinuationStore<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
        context: T,
    ) -> Result<ContinuationState<R, C>, (ContinuationError, T)> {
        if self.contains(task, call_id) {
            return Err((
                ContinuationError::ContextAttached { call_id, count: 1 },
                context,
            ));
        }
        let semantic = match kernel.activate_attaching_context(task, call_id) {
            Ok(semantic) => semantic,
            Err(error) => return Err((error, context)),
        };
        let replaced = self.by_call.insert((task, call_id), context);
        debug_assert!(replaced.is_none());
        Ok(semantic)
    }

    #[cfg(test)]
    pub(crate) fn take<R: Copy + TaskOwned, C: Copy>(
        &mut self,
        kernel: &ContinuationStore<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
    ) -> Result<T, ContinuationError> {
        if !self.contains(task, call_id) {
            return Err(ContinuationError::ContextAttached { call_id, count: 0 });
        }
        kernel.detach_context(task, call_id)?;
        Ok(self
            .by_call
            .remove(&(task, call_id))
            .expect("validated context must remain parked"))
    }

    pub(crate) fn take_while_completing<R: Copy + TaskOwned, C: Copy>(
        &mut self,
        kernel: &ContinuationStore<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
        result: Option<u32>,
    ) -> Result<(T, ContinuationState<R, C>), ContinuationError> {
        if !self.contains(task, call_id) {
            return Err(ContinuationError::ContextAttached { call_id, count: 0 });
        }
        let semantic = kernel.complete_detaching_context(task, call_id, result)?;
        let context = self
            .by_call
            .remove(&(task, call_id))
            .expect("validated context must remain parked");
        Ok((context, semantic))
    }

    /// Park the enclosing caller and activate its nested call as one change.
    /// All identity/phase checks precede replacement of the installed engine.
    pub(crate) fn activate_parking_caller<R: Copy + TaskOwned, C: Copy>(
        &mut self,
        kernel: &ContinuationStore<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
        caller: Option<CallId>,
        installed: &mut T,
    ) -> Result<(), ContinuationError>
    where
        T: Default,
    {
        self.activate_parking_caller_with_context::<R, C, ()>(
            kernel, task, call_id, caller, installed, None,
        )
    }

    pub(crate) fn activate_parking_caller_with_context<R: Copy + TaskOwned, C: Copy, U>(
        &mut self,
        kernel: &ContinuationStore<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
        caller: Option<CallId>,
        installed: &mut T,
        companion: Option<(&mut ExecutionContextBank<U>, U)>,
    ) -> Result<(), ContinuationError>
    where
        T: Default,
    {
        let mut state = kernel.0.borrow_mut();
        ContinuationStore::validate_transition(&state, task, call_id, ContinuationPhase::Pending)?;
        if companion
            .as_ref()
            .is_some_and(|(bank, _)| bank.contains(task, call_id))
        {
            return Err(ContinuationError::ContextAttached { call_id, count: 1 });
        }
        if let Some(caller) = caller {
            ContinuationStore::validate_context_owner(&state, task, caller)?;
            if caller == call_id {
                return Err(ContinuationError::CallIdMismatch {
                    task,
                    expected: None,
                    actual: caller,
                });
            }
            if self.contains(task, caller) {
                // Sibling callbacks reuse the enclosing caller's saved state.
                *installed = T::default();
            } else {
                self.by_call
                    .insert((task, caller), std::mem::take(installed));
                *state.attached_contexts.entry(caller).or_default() += 1;
            }
        }
        if let Some((bank, context)) = companion {
            bank.by_call.insert((task, call_id), context);
            *state.attached_contexts.entry(call_id).or_default() += 1;
        }
        state
            .stacks
            .get_mut(&task)
            .and_then(|stack| stack.last_mut())
            .expect("validated continuation")
            .phase = ContinuationPhase::Active;
        Ok(())
    }

    /// Validate the complete return boundary before allowing adapter writes.
    /// The closure must validate all fallible ABI work before mutating state;
    /// it cannot execute guest code or reenter this store. Once it succeeds,
    /// removal of the exact context and continuation cannot fail.
    pub(crate) fn retire_with_context<R: Copy + TaskOwned, C: Copy>(
        &mut self,
        kernel: &ContinuationStore<R, C>,
        task: ExecutionTaskId,
        call_id: CallId,
        apply: impl FnOnce(Option<&mut T>) -> bool,
    ) -> Result<Option<T>, ContinuationError> {
        let mut state = kernel.0.borrow_mut();
        ContinuationStore::validate_transition(
            &state,
            task,
            call_id,
            ContinuationPhase::Completed,
        )?;
        let expected = usize::from(self.contains(task, call_id));
        let attached = state.attached_contexts.get(&call_id).copied().unwrap_or(0);
        if attached != expected {
            return Err(ContinuationError::ContextAttached {
                call_id,
                count: attached,
            });
        }
        if !apply(self.by_call.get_mut(&(task, call_id))) {
            return Err(ContinuationError::CommitRefused { call_id });
        }
        state.attached_contexts.remove(&call_id);
        state.stacks.get_mut(&task).expect("validated task").pop();
        Ok(self.by_call.remove(&(task, call_id)))
    }

    pub(crate) fn same_slots<U>(&self, other: &ExecutionContextBank<U>) -> bool {
        self.by_call.len() == other.by_call.len()
            && self
                .by_call
                .keys()
                .all(|key| other.by_call.contains_key(key))
    }
}

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

    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    struct Request {
        task: ExecutionTaskId,
        entry: u32,
    }

    impl TaskOwned for Request {
        fn task(&self) -> ExecutionTaskId {
            self.task
        }
    }

    type Store = ContinuationStore<Request, u32>;

    fn request(task: ExecutionTaskId, entry: u32) -> Request {
        Request { task, entry }
    }

    fn submit(store: &Store, task: ExecutionTaskId, entry: u32) -> CallId {
        store.submit(task, request(task, entry), entry + 1).unwrap()
    }

    #[test]
    fn scheduler_selection_is_stable_until_a_switch_and_stopped_tasks_are_removed() {
        let store = Store::default();
        let app = ExecutionTaskId::APPLICATION;
        let first = ExecutionTaskId::from_thread_id(3);
        let second = ExecutionTaskId::from_thread_id(4);
        for task in [first, second] {
            store.register_task(task).unwrap();
            assert!(store.set_scheduling_state(task, ExecutionTaskState::Ready));
        }
        assert_eq!(store.next_ready_task(None), Some(first));
        let before = store.clone();
        assert_eq!(store.next_ready_task(Some(second)), Some(second));
        assert_eq!(
            store, before,
            "selection must not consume a context the adapter may reject"
        );
        assert!(!store.set_scheduling_state(first, ExecutionTaskState::Running));
        assert_eq!(store, before);
        assert!(store.set_scheduling_state(first, ExecutionTaskState::Stopped));
        assert_eq!(store.next_ready_task(Some(first)), Some(second));
        store.switch_to_task(second).unwrap();
        assert_eq!(
            store.scheduling_state(second),
            Some(ExecutionTaskState::Running)
        );
        assert_eq!(store.scheduling_state(app), Some(ExecutionTaskState::Ready));
        assert_eq!(store.next_ready_task(None), Some(app));
        store.retire_task(first).unwrap();
        assert!(!store.set_scheduling_state(first, ExecutionTaskState::Ready));
        assert_eq!(store.next_ready_task(None), Some(app));
    }

    #[test]
    fn combined_state_and_critical_exit_rejects_without_partial_unmasking() {
        let store = Store::default();
        let app = ExecutionTaskId::APPLICATION;
        store.begin_critical();
        let before = store.clone();
        assert!(!store.set_state_ending_critical(
            ExecutionTaskId::from_thread_id(999),
            ExecutionTaskState::Ready
        ));
        assert_eq!(store, before);
        store.begin_critical();
        let nested = store.clone();
        assert!(!store.set_state_ending_critical(app, ExecutionTaskState::Stopped));
        assert_eq!(store, nested);
        assert!(store.end_critical());
        assert!(store.set_state_ending_critical(app, ExecutionTaskState::Stopped));
        assert_eq!(store.critical_depth(), 0);
        assert_eq!(
            store.scheduling_state(app),
            Some(ExecutionTaskState::Stopped)
        );
    }

    #[test]
    fn nested_critical_sections_keep_task_cursor_and_schedule_unchanged() {
        let store = Store::default();
        let app = ExecutionTaskId::APPLICATION;
        let worker = ExecutionTaskId::from_thread_id(3);
        store.register_task(worker).unwrap();
        assert!(store.set_scheduling_state(worker, ExecutionTaskState::Ready));
        assert!(store.set_scheduling_state(worker, ExecutionTaskState::Ready));
        store.begin_critical();
        store.begin_critical();
        let before = store.clone();
        assert_eq!(store.next_ready_task(Some(worker)), None);
        assert!(store.switch_to_task(worker).is_err());
        assert!(!store.set_scheduling_state(app, ExecutionTaskState::Stopped));
        assert_eq!(store, before);
        assert!(store.end_critical());
        assert_eq!(store.next_ready_task(None), None);
        assert!(store.end_critical());
        assert_eq!(store.next_ready_task(None), Some(worker));
        assert!(!store.end_critical());
        assert_eq!(store.critical_depth(), 0);
    }

    #[test]
    fn refused_task_creation_does_not_consume_identity_or_publish_state() {
        let store = Store::default();
        let before = store.clone();
        assert!(store.create_task_with(|_| false).is_err());
        assert_eq!(store, before);
        assert_eq!(store.create_task().unwrap().thread_id(), 3);
    }

    #[test]
    fn task_allocation_shares_registration_and_never_reuses_retired_ids() {
        let store = Store::default();
        let first = store.create_task().unwrap();
        assert_eq!(first.thread_id(), 3);
        let imported = ExecutionTaskId::from_thread_id(40);
        store.register_task(imported).unwrap();
        store.retire_task(imported).unwrap();
        store.retire_task(first).unwrap();
        let next = store.create_task().unwrap();
        assert_eq!(next.thread_id(), 41);
        assert_eq!(
            store.scheduling_state(next),
            Some(ExecutionTaskState::Stopped)
        );
        assert_eq!(store.task_entry_isa(next), Some(GuestIsa::M68k));
        assert!(!store.is_pristine());
    }

    #[test]
    fn exhausted_task_namespace_is_unchanged_and_does_not_wrap() {
        let store = Store::default();
        store
            .register_task(ExecutionTaskId::from_thread_id(u32::MAX - 1))
            .unwrap();
        assert_eq!(store.create_task().unwrap().thread_id(), u32::MAX);
        let before = store.clone();
        assert_eq!(
            store.create_task(),
            Err(ContinuationError::TaskIdsExhausted)
        );
        assert_eq!(store, before);
        store
            .retire_task(ExecutionTaskId::from_thread_id(u32::MAX))
            .unwrap();
        assert_eq!(
            store.create_task(),
            Err(ContinuationError::TaskIdsExhausted)
        );
    }

    #[test]
    fn task_entry_architecture_retires_with_its_task() {
        let store = Store::default();
        let worker = ExecutionTaskId::from_thread_id(9);
        assert_eq!(store.task_entry_isa(worker), None);
        assert!(!store.bind_task_entry_isa(worker, GuestIsa::PowerPc));
        store.register_task(worker).unwrap();
        assert_eq!(store.task_entry_isa(worker), Some(GuestIsa::M68k));
        assert!(store.bind_task_entry_isa(worker, GuestIsa::PowerPc));
        assert_eq!(store.task_entry_isa(worker), Some(GuestIsa::PowerPc));
        store.retire_task(worker).unwrap();
        assert_eq!(store.task_entry_isa(worker), None);
        assert!(!store.bind_task_entry_isa(worker, GuestIsa::M68k));
    }

    #[test]
    fn task_ids_cannot_be_created_by_submission_or_resurrected_after_retirement() {
        let store = Store::default();
        let worker = ExecutionTaskId::from_thread_id(77);
        let snapshot = store.clone();
        assert!(store.switch_to_task(worker).is_err());
        assert!(store.submit(worker, request(worker, 0x1000), 0).is_err());
        assert_eq!(store, snapshot);
        store.register_task(worker).unwrap();
        assert!(store.set_scheduling_state(worker, ExecutionTaskState::Ready));
        assert!(store.register_task(worker).is_err());
        store.retire_task(worker).unwrap();
        let retired = store.clone();
        assert!(store.register_task(worker).is_err());
        assert!(store.switch_to_task(worker).is_err());
        assert!(store.submit(worker, request(worker, 0x1000), 0).is_err());
        assert!(store.retire_task(worker).is_err());
        assert_eq!(store, retired);
    }

    #[test]
    fn nested_activation_validates_before_replacing_the_installed_caller() {
        let store = Store::default();
        let task = ExecutionTaskId::APPLICATION;
        let outer = submit(&store, task, 0x1000);
        store.activate(task, outer).unwrap();
        let inner = submit(&store, task, 0x2000);
        let mut bank = ExecutionContextBank::default();
        let mut installed = 42_u32;
        let before = store.clone();
        assert!(bank
            .activate_parking_caller(&store, task, inner, Some(inner), &mut installed)
            .is_err());
        assert_eq!(installed, 42);
        assert_eq!(store, before);
        assert!(bank.is_empty());
        bank.activate_parking_caller(&store, task, inner, Some(outer), &mut installed)
            .unwrap();
        assert_eq!(installed, 0);
        assert_eq!(store.peek(task).unwrap().phase(), ContinuationPhase::Active);
        installed = 99;
        assert!(bank
            .activate_parking_caller(&store, task, inner, Some(outer), &mut installed)
            .is_err());
        assert_eq!(installed, 99);
        store.complete(task, inner, None).unwrap();
        store.retire(task, inner).unwrap();
        store.complete(task, outer, None).unwrap();
        assert_eq!(
            bank.retire_with_context(&store, task, outer, |_| true),
            Ok(Some(42))
        );
    }

    #[test]
    fn return_commit_validates_contexts_before_adapter_writes_and_retries() {
        let store = Store::default();
        let task = ExecutionTaskId::APPLICATION;
        let call = submit(&store, task, 0x1000);
        let mut bank = ExecutionContextBank::default();
        bank.park(&store, task, call, 42_u32).unwrap();
        // Pending calls cannot expose the parked caller to result writes.
        assert!(bank
            .retire_with_context(&store, task, call, |_| panic!("not completed"))
            .is_err());
        store.activate(task, call).unwrap();
        store.complete(task, call, Some(7)).unwrap();
        let snapshot = store.clone();
        let mut wrong_bank = ExecutionContextBank::<u32>::default();
        assert!(wrong_bank
            .retire_with_context(&store, task, call, |_| panic!("missing context"))
            .is_err());
        assert_eq!(store, snapshot);
        assert_eq!(
            bank.retire_with_context(&store, task, call, |context| {
                assert_eq!(context.as_deref(), Some(&42));
                false
            }),
            Err(ContinuationError::CommitRefused { call_id: call })
        );
        assert_eq!(store, snapshot);
        assert_eq!(
            bank.retire_with_context(&store, task, call, |context| {
                *context.unwrap() = 7;
                true
            }),
            Ok(Some(7))
        );
        assert!(store.is_empty());
        assert!(bank.is_empty());
        assert!(bank
            .retire_with_context(&store, task, call, |_| panic!("stale return"))
            .is_err());
    }

    #[test]
    fn same_task_continuations_are_lifo_and_phase_ordered() {
        let store = Store::default();
        let task = ExecutionTaskId::APPLICATION;
        let outer = submit(&store, task, 0x1000);
        let inner = submit(&store, task, 0x3000);

        assert_eq!(store.peek(task).unwrap().call_id(), inner);
        assert!(matches!(
            store.activate(task, outer),
            Err(ContinuationError::CallIdMismatch {
                expected: Some(actual),
                actual: requested,
                ..
            }) if actual == inner && requested == outer
        ));
        assert_eq!(
            store.peek(task).unwrap().phase(),
            ContinuationPhase::Pending
        );

        assert_eq!(
            store.activate(task, inner).unwrap().phase(),
            ContinuationPhase::Active
        );
        assert_eq!(
            store.complete(task, inner, Some(0x55)).unwrap(),
            ContinuationState {
                call_id: inner,
                task,
                request: request(task, 0x3000),
                continuation: 0x3001,
                phase: ContinuationPhase::Completed,
                result: Some(0x55),
            }
        );
        assert_eq!(store.retire(task, inner).unwrap().call_id(), inner);
        assert_eq!(store.peek(task).unwrap().call_id(), outer);
    }

    #[test]
    fn task_stacks_are_independent_across_explicit_switches() {
        let store = Store::default();
        let application = ExecutionTaskId::APPLICATION;
        let worker = ExecutionTaskId::from_thread_id(7);
        store.register_task(worker).unwrap();
        assert!(store.set_scheduling_state(worker, ExecutionTaskState::Ready));
        let app_call = submit(&store, application, 0x1000);
        let worker_call = submit(&store, worker, 0x3000);

        store.switch_to_task(worker).unwrap();
        assert_eq!(store.depth(), 1);
        store.activate(worker, worker_call).unwrap();
        store.complete(worker, worker_call, None).unwrap();
        store.retire(worker, worker_call).unwrap();

        store.switch_to_task(application).unwrap();
        assert_eq!(store.depth(), 1);
        assert_eq!(store.peek(application).unwrap().call_id(), app_call);
        store.activate(application, app_call).unwrap();
        store.complete(application, app_call, Some(1)).unwrap();
        store.retire(application, app_call).unwrap();
        assert!(store.is_empty());
    }

    #[test]
    fn task_mismatch_is_rejected_without_rewriting_the_request() {
        let store = Store::default();
        let application = ExecutionTaskId::APPLICATION;
        let worker = ExecutionTaskId::from_thread_id(9);
        store.register_task(worker).unwrap();
        assert!(store.set_scheduling_state(worker, ExecutionTaskState::Ready));

        assert_eq!(
            store.submit(application, request(worker, 0x1000), 0x1001),
            Err(ContinuationError::TaskMismatch {
                expected: worker,
                actual: application,
            })
        );
        assert!(store.is_empty());

        let call_id = submit(&store, application, 0x3000);
        let before = store.clone();
        assert!(matches!(
            store.activate(worker, call_id),
            Err(ContinuationError::TaskMismatch {
                expected,
                actual,
            }) if expected == application && actual == worker
        ));
        assert_eq!(store, before);
        assert_eq!(store.peek(application).unwrap().request().task, application);
    }

    #[test]
    fn call_id_mismatch_is_transactional() {
        let store = Store::default();
        let task = ExecutionTaskId::APPLICATION;
        let first = submit(&store, task, 0x1000);
        let second = submit(&store, task, 0x3000);
        let before = store.clone();

        assert!(matches!(
            store.complete(task, first, Some(9)),
            Err(ContinuationError::CallIdMismatch {
                expected: Some(expected),
                actual,
                ..
            }) if expected == second && actual == first
        ));
        assert_eq!(store, before);
        assert_eq!(
            store.peek(task).unwrap().phase(),
            ContinuationPhase::Pending
        );
        assert_eq!(store.peek(task).unwrap().result(), None);
    }

    #[test]
    fn retirement_refuses_current_or_nonempty_tasks() {
        let store = Store::default();
        let application = ExecutionTaskId::APPLICATION;
        let worker = ExecutionTaskId::from_thread_id(11);
        store.register_task(worker).unwrap();
        assert!(store.set_scheduling_state(worker, ExecutionTaskState::Ready));
        let call_id = submit(&store, application, 0x1000);
        let before = store.clone();
        assert!(matches!(
            store.retire_task(application),
            Err(ContinuationError::RetirementRefused {
                task,
                depth: 1,
                contexts: 0,
                current: true,
            }) if task == application
        ));
        assert_eq!(store, before);

        store.switch_to_task(worker).unwrap();
        let before = store.clone();
        assert!(matches!(
            store.retire_task(application),
            Err(ContinuationError::RetirementRefused {
                task,
                depth: 1,
                contexts: 0,
                current: false,
            }) if task == application
        ));
        assert_eq!(store, before);

        store.switch_to_task(application).unwrap();
        store.activate(application, call_id).unwrap();
        store.complete(application, call_id, None).unwrap();
        store.retire(application, call_id).unwrap();
        store.switch_to_task(worker).unwrap();
        assert_eq!(store.retire_task(application), Ok(()));
        assert!(store.task_is_empty(application));
    }

    #[test]
    fn clone_is_detached_while_shared_handle_observes_live_state() {
        let store = Store::default();
        let detached = store.clone();
        let shared = store.shared_handle();
        let task = ExecutionTaskId::APPLICATION;
        let call_id = submit(&store, task, 0x1000);

        assert!(detached.is_empty());
        assert_eq!(shared.peek(task).unwrap().call_id(), call_id);
        shared.activate(task, call_id).unwrap();
        assert_eq!(store.peek(task).unwrap().phase(), ContinuationPhase::Active);
    }

    #[test]
    fn concrete_contexts_are_keyed_by_exact_task_and_call() {
        let store = Store::default();
        let application = ExecutionTaskId::APPLICATION;
        let worker = ExecutionTaskId::from_thread_id(13);
        store.register_task(worker).unwrap();
        assert!(store.set_scheduling_state(worker, ExecutionTaskState::Ready));
        let application_call = submit(&store, application, 0x1000);
        let worker_call = submit(&store, worker, 0x2000);
        let mut contexts = ExecutionContextBank::default();

        assert_eq!(
            contexts.park(&store, application, application_call, 0xaaaa),
            Ok(())
        );
        assert_eq!(contexts.len(), 1);
        assert_eq!(contexts.task_len(application), 1);
        assert_eq!(contexts.task_len(worker), 0);

        store.switch_to_task(worker).unwrap();
        assert_eq!(contexts.park(&store, worker, worker_call, 0xbbbb), Ok(()));
        assert_eq!(contexts.len(), 2);
        assert_eq!(contexts.task_len(application), 1);
        assert_eq!(contexts.task_len(worker), 1);
        assert_eq!(contexts.take(&store, worker, worker_call), Ok(0xbbbb));
        assert!(!contexts.contains(worker, worker_call));

        store.switch_to_task(application).unwrap();
        assert_eq!(
            contexts.take(&store, application, application_call),
            Ok(0xaaaa)
        );
        assert!(contexts.is_empty());
    }

    #[test]
    fn attached_context_blocks_call_retirement_until_exact_restore() {
        let store = Store::default();
        let task = ExecutionTaskId::APPLICATION;
        let call_id = submit(&store, task, 0x1000);
        store.activate(task, call_id).unwrap();
        let mut contexts = ExecutionContextBank::default();
        contexts.park(&store, task, call_id, 7).unwrap();
        store.complete(task, call_id, None).unwrap();

        assert_eq!(
            store.retire(task, call_id),
            Err(ContinuationError::ContextAttached { call_id, count: 1 })
        );
        assert_eq!(contexts.take(&store, task, call_id), Ok(7));
        assert_eq!(store.retire(task, call_id).unwrap().call_id(), call_id);
        assert!(store.is_empty());
    }
}