File size: 75,605 Bytes
4a9362b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fail-closed migration of a durable producer state to a new runtime binding.

The scientific checkpoint is never rewritten.  A migration copies its exact
bytes into a new run root and records an external old-to-new runtime binding.
The runner must validate that binding before it may restore the checkpoint
under changed executable sources; this module deliberately provides no
``ignore_identity`` escape hatch.
"""

from __future__ import annotations

import hashlib
import json
import os
import shutil
import tempfile
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Protocol, cast

from operational.rgb_port.accounting import atomic_write_json, read_json_shared

from .recipe import canonical_sha256, sha256

ADMISSION_SCHEMA = "final_refit_runtime_migration_admission_v1"
MIGRATION_SCHEMA = "final_refit_runtime_migration_v1"
RUNTIME_BINDING_SCHEMA = "final_refit_runtime_resume_binding_v1"
RUNTIME_BINDING_V3_SCHEMA = "final_refit_runtime_resume_binding_v2"
OPTIMIZATION_ADMISSION_SCHEMA = "final_refit_rgb_optimization_admission_v1"
BATCH_MIGRATION_SCHEMA = "final_refit_runtime_migration_batch_v2"
FIT_MIGRATION_SCHEMA = "final_refit_runtime_fit_migration_v2"
CHECKPOINT_SCHEMA = "rgb_port_producer_checkpoint_v1"
CHECKPOINT_INTERVAL = 100
FAULT_PROBE_SCHEMA = "final_refit_checkpoint_fault_probe_v1"
THREAD_PROBE_SCHEMA = "final_refit_rgb_runtime_thread_probe_v1"
_FAULTS = {
    "commit_journal_before",
    "commit_journal_after",
    "save_version_after_write",
    "save_pointer_after",
    "save_alias_after",
    "save_journal_after",
    "save_receipt_after",
}

_STATE_FIELDS = (
    "model_state_dict",
    "optimizer_state_dict",
    "scheduler_state_dict",
    "sampler_generator_state",
    "torch_rng_state",
    "cuda_rng_state_all",
    "numpy_random_state",
    "python_random_state",
    "cursor",
)


def _canonical_plan_identity(value: Mapping[str, Any]) -> dict[str, Any]:
    """Return the scientific/data plan after removing only runtime bindings."""
    result = dict(value)
    result.pop("plan_sha256", None)
    result.pop("executed_source_sha256", None)
    inputs = dict(cast(Mapping[str, Any], result["input_sha256"]))
    inputs.pop("runtime_admission", None)
    inputs.pop("runtime_migration_admission", None)
    inputs.pop("runtime_optimization_admission", None)
    result["input_sha256"] = inputs
    return result


def _validate_optimization_admission(
    path: Path,
    *,
    old_plan: Mapping[str, Any],
    new_plan: Mapping[str, Any],
) -> dict[str, Any]:
    """Validate the technical v3 admission without binding a moving checkpoint."""
    value = read_json_shared(path.resolve(strict=True))
    without_hash = dict(value)
    claimed = without_hash.pop("admission_sha256", None)
    required_checks = {"numerical_parity", "state_parity", "resume_parity", "rng_parity"}
    checks = value.get("checks")
    if (
        value.get("schema") != OPTIMIZATION_ADMISSION_SCHEMA
        or value.get("status") != "PASS"
        or claimed != canonical_sha256(without_hash)
        or value.get("scientific_optimizer_updates") != 0
        or not isinstance(value.get("technical_optimizer_updates"), int)
        or int(value["technical_optimizer_updates"]) < 0
        or not isinstance(value.get("technical_cases"), list)
        or len(value["technical_cases"]) == 0
        or not isinstance(value.get("admitted_options"), dict)
        or not value["admitted_options"]
        or not isinstance(checks, dict)
        or set(checks) != required_checks
        or any(checks[name] != "PASS" for name in required_checks)
        or value.get("old_plan_sha256") != old_plan.get("plan_sha256")
        or value.get("scientific_plan_sha256")
        != canonical_sha256(_canonical_plan_identity(old_plan))
    ):
        raise ValueError("Runtime optimization admission is invalid")
    if _canonical_plan_identity(old_plan) != _canonical_plan_identity(new_plan):
        raise ValueError("Runtime optimization changed the scientific/data producer plan")
    transitions = value.get("source_sha256_before_after")
    old_sources = cast(Mapping[str, str], old_plan["executed_source_sha256"])
    new_sources = cast(Mapping[str, str], new_plan["executed_source_sha256"])
    if not isinstance(transitions, dict) or set(transitions) != set(old_sources) | set(new_sources):
        raise ValueError("Optimization admission omits part of the source closure")
    for name in transitions:
        if transitions[name] != {
            "before_sha256": old_sources.get(name),
            "after_sha256": new_sources.get(name),
        }:
            raise ValueError("Optimization admission source transition differs")
    repository = Path(__file__).resolve().parents[2]
    for name, expected in new_sources.items():
        current = (repository / name).resolve(strict=True)
        if not current.is_relative_to(repository) or sha256(current) != expected:
            raise ValueError(f"New optimization runtime source differs: {name}")
    return value


def validate_optimization_admission(
    path: Path, *, old_plan_path: Path, new_plan_path: Path
) -> dict[str, Any]:
    """Public fail-closed validator used by plan and runner admission gates."""
    return _validate_optimization_admission(
        path.resolve(strict=True),
        old_plan=_validate_plan(old_plan_path.resolve(strict=True)),
        new_plan=_validate_plan(new_plan_path.resolve(strict=True)),
    )


def _validate_plan(path: Path) -> dict[str, Any]:
    value = read_json_shared(path)
    claimed = value.get("plan_sha256")
    without_hash = dict(value)
    without_hash.pop("plan_sha256", None)
    if claimed != canonical_sha256(without_hash):
        raise ValueError(f"Producer plan hash is invalid: {path}")
    if (
        value.get("schema") != "final_refit_rgb_producer_plan_v1"
        or value.get("status") != "FROZEN_AUTHORIZED_PRODUCER_SCOPE"
        or value.get("launch_authorized") is not True
    ):
        raise ValueError("Runtime migration requires an authorized producer plan")
    return value


class _Digest(Protocol):
    def update(self, value: bytes, /) -> object: ...


def _feed(digest: _Digest, kind: str, payload: bytes = b"") -> None:
    encoded_kind = kind.encode("utf-8")
    digest.update(len(encoded_kind).to_bytes(4, "big"))
    digest.update(encoded_kind)
    digest.update(len(payload).to_bytes(8, "big"))
    digest.update(payload)


def _state_sha256(value: object) -> str:
    """Hash nested checkpoint state without depending on pickle byte layout."""
    import numpy as np
    import torch

    digest = hashlib.sha256(b"final_refit_checkpoint_state_v1\0")

    def visit(item: object) -> None:
        if isinstance(item, torch.Tensor):
            tensor = item.detach().cpu().contiguous()
            _feed(
                digest,
                "tensor",
                json.dumps(
                    {
                        "dtype": str(tensor.dtype),
                        "shape": list(tensor.shape),
                    },
                    sort_keys=True,
                    separators=(",", ":"),
                ).encode("utf-8"),
            )
            raw = tensor.reshape(-1).view(torch.uint8).numpy().tobytes()
            _feed(digest, "tensor_bytes", raw)
        elif isinstance(item, np.ndarray):
            array = np.ascontiguousarray(item)
            _feed(
                digest,
                "ndarray",
                json.dumps(
                    {"dtype": array.dtype.str, "shape": list(array.shape)},
                    sort_keys=True,
                    separators=(",", ":"),
                ).encode("utf-8"),
            )
            _feed(digest, "ndarray_bytes", array.tobytes())
        elif isinstance(item, Mapping):
            _feed(digest, "mapping", str(len(item)).encode("ascii"))
            mapping = cast(Mapping[object, object], item)
            keyed = [(_state_sha256(key), key) for key in mapping]
            for key_hash, key in sorted(keyed, key=lambda pair: pair[0]):
                _feed(digest, "key_hash", key_hash.encode("ascii"))
                visit(mapping[key])
        elif isinstance(item, tuple):
            _feed(digest, "tuple", str(len(item)).encode("ascii"))
            for child in item:
                visit(child)
        elif isinstance(item, list):
            _feed(digest, "list", str(len(item)).encode("ascii"))
            for child in item:
                visit(child)
        elif item is None:
            _feed(digest, "none")
        elif isinstance(item, bool):
            _feed(digest, "bool", b"1" if item else b"0")
        elif isinstance(item, int):
            _feed(digest, "int", str(item).encode("ascii"))
        elif isinstance(item, float):
            _feed(digest, "float", item.hex().encode("ascii"))
        elif isinstance(item, str):
            _feed(digest, "str", item.encode("utf-8"))
        elif isinstance(item, bytes):
            _feed(digest, "bytes", item)
        else:
            raise TypeError(f"Unsupported checkpoint state value: {type(item)!r}")

    visit(value)
    return digest.hexdigest()


def _load_checkpoint(path: Path) -> dict[str, Any]:
    import torch

    value = torch.load(path, map_location="cpu", weights_only=False)
    if not isinstance(value, dict) or value.get("schema") != CHECKPOINT_SCHEMA:
        raise ValueError("Unsupported producer checkpoint")
    missing = [name for name in _STATE_FIELDS if name not in value]
    if missing:
        raise ValueError(f"Checkpoint omits resumable state: {missing}")
    if int(value.get("accumulation_index", -1)) != 0:
        raise ValueError("Migration is only valid at an optimizer boundary")
    return cast(dict[str, Any], value)


def _validate_source_evidence(
    source_directory: Path,
    checkpoint: Path,
    payload: Mapping[str, Any],
    curve_directory: Path,
) -> tuple[dict[str, Any], dict[str, Any], dict[str, Any], dict[str, str]]:
    identity = str(payload["identity_sha256"])
    completed = int(payload["completed_updates"])
    checkpoint_hash = sha256(checkpoint)
    pointer = read_json_shared(source_directory / "CHECKPOINT_POINTER.json")
    receipt = read_json_shared(source_directory / "CHECKPOINT_RECEIPT.json")
    journal = read_json_shared(source_directory / "UPDATE_JOURNAL.json")
    if (
        pointer.get("schema") != "rgb_port_checkpoint_pointer_v1"
        or pointer.get("identity_sha256") != identity
        or int(pointer.get("completed_updates", -1)) != completed
        or pointer.get("checkpoint_sha256") != checkpoint_hash
    ):
        raise ValueError("Checkpoint pointer does not bind the migration source")
    if (
        receipt.get("schema") != "rgb_port_checkpoint_receipt_v1"
        or receipt.get("identity_sha256") != identity
        or int(receipt.get("completed_updates", -1)) != completed
        or receipt.get("checkpoint_sha256") != checkpoint_hash
        or receipt.get("complete_state") is not True
        or int(receipt.get("accumulation_index", -1)) != 0
        or receipt.get("scientific_endpoint") is not False
    ):
        raise ValueError("Checkpoint receipt does not describe a paused complete state")
    if (
        journal.get("schema") != "rgb_port_update_journal_v1"
        or journal.get("identity_sha256") != identity
        or int(journal.get("completed_updates", -1)) != completed
        or int(journal.get("durable_updates", -1)) != completed
        or int(journal.get("pending_update_upper", -1)) != 0
        or int(journal.get("recovery_upper", -1)) != int(payload["recovery_upper"])
    ):
        raise ValueError("Update journal is not a zero-pending durable boundary")
    curves = _validate_curve_coverage(curve_directory, payload)
    return pointer, receipt, journal, curves


def _validate_curve_coverage(
    curve_directory: Path,
    payload: Mapping[str, Any],
    *,
    recoverable_upper: int | None = None,
) -> dict[str, str]:
    """Validate durable curve files plus the checkpoint's pending suffix."""
    completed = int(payload["completed_updates"])
    curves: dict[str, str] = {}
    curve_updates: list[int] = []
    for curve in sorted(curve_directory.glob("curve_*.json")):
        try:
            named_boundary = int(curve.stem.removeprefix("curve_"))
        except ValueError as error:
            raise ValueError(f"Malformed curve filename: {curve.name}") from error
        if named_boundary > completed and (
            recoverable_upper is None or named_boundary > recoverable_upper
        ):
            raise ValueError(f"Curve advances past the durable checkpoint: {curve.name}")
        curve_value = read_json_shared(curve)
        updates = curve_value.get("updates")
        if not isinstance(updates, list) or not updates:
            raise ValueError(f"Periodic curve has a discontinuous update range: {curve.name}")
        observed = [int(item.get("update", -1)) for item in updates]
        expected = list(range(observed[0], observed[-1] + 1))
        if observed != expected or observed[-1] != named_boundary:
            raise ValueError(f"Periodic curve has a discontinuous update range: {curve.name}")
        if named_boundary <= completed:
            curve_updates.extend(observed)
        curves[curve.name] = sha256(curve)
    for boundary in range(CHECKPOINT_INTERVAL, completed + 1, CHECKPOINT_INTERVAL):
        if f"curve_{boundary:06d}.json" not in curves:
            raise ValueError(f"Missing durable periodic curve: curve_{boundary:06d}.json")
    pending = cast(Mapping[str, Any], payload["cursor"]).get("pending_curve")
    if not isinstance(pending, list):
        raise ValueError("Checkpoint cursor omits pending_curve")
    pending_updates = [int(item.get("update", -1)) for item in pending]
    if sorted(curve_updates + pending_updates) != list(range(1, completed + 1)):
        raise ValueError("Saved and pending curves do not cover the durable update cursor exactly")
    return curves


def _validate_admission(
    path: Path,
    *,
    fit_id: str,
    old_identity: str,
    checkpoint_sha256: str,
    old_plan_sha256: str,
) -> dict[str, Any]:
    value = read_json_shared(path)
    without_hash = dict(value)
    claimed = without_hash.pop("admission_sha256", None)
    required = {
        "schema": ADMISSION_SCHEMA,
        "status": "ADMITTED_TECHNICAL_RUNTIME_MIGRATION",
        "fit_id": fit_id,
        "old_identity_sha256": old_identity,
        "parent_checkpoint_sha256": checkpoint_sha256,
        "old_plan_sha256": old_plan_sha256,
        "scientific_recipe_unchanged": True,
        "optimizer_mode_unchanged": True,
        "checkpoint_interval_updates": CHECKPOINT_INTERVAL,
        "scientific_optimizer_updates": 0,
    }
    if claimed != canonical_sha256(without_hash) or any(
        value.get(name) != expected for name, expected in required.items()
    ):
        raise ValueError("Runtime migration admission is invalid")
    sources = value.get("source_sha256_before_after")
    if not isinstance(sources, dict) or not sources:
        raise ValueError("Admission must enumerate the runtime source closure")
    for name, pair in sources.items():
        if (
            not isinstance(name, str)
            or not isinstance(pair, dict)
            or set(pair) != {"before_sha256", "after_sha256"}
            or (
                pair["before_sha256"] is not None
                and (not isinstance(pair["before_sha256"], str) or len(pair["before_sha256"]) != 64)
            )
            or not isinstance(pair["after_sha256"], str)
            or len(pair["after_sha256"]) != 64
        ):
            raise ValueError("Malformed before/after runtime source binding")
    evidence = value.get("technical_evidence")
    required_evidence = {
        "state_parity": "PASS",
        "numerical_parity": "PASS",
        "runtime_benchmark": "PROFILED",
    }
    if not isinstance(evidence, dict) or set(evidence) != set(required_evidence):
        raise ValueError("Admission requires state, numerical, and benchmark evidence")
    for name, expected_status in required_evidence.items():
        reference = evidence[name]
        if (
            not isinstance(reference, dict)
            or set(reference) != {"path", "sha256", "scope"}
            or not isinstance(reference["scope"], str)
            or not reference["scope"].strip()
        ):
            raise ValueError(f"Malformed technical evidence reference: {name}")
        evidence_path = Path(str(reference["path"]))
        if not evidence_path.is_absolute():
            evidence_path = path.parent / evidence_path
        evidence_path = evidence_path.resolve(strict=True)
        if sha256(evidence_path) != reference["sha256"]:
            raise ValueError(f"Technical evidence changed after admission: {name}")
        evidence_value = read_json_shared(evidence_path)
        admitted = False
        if name == "state_parity":
            cases = evidence_value.get("cases")
            admitted = bool(
                evidence_value.get("schema") == FAULT_PROBE_SCHEMA
                and evidence_value.get("status") == expected_status
                and isinstance(cases, list)
                and len(cases) == len(_FAULTS)
                and {item.get("fault") for item in cases} == _FAULTS
                and all(
                    item.get("all_exact") is True
                    and item.get("bounded_by_save_interval") is True
                    and item.get("scientific_endpoint") is True
                    and isinstance(item.get("exact"), dict)
                    and item["exact"]
                    and all(item["exact"].values())
                    for item in cases
                )
            )
        elif name == "numerical_parity":
            from .eager_probe import validate_receipt

            try:
                validate_receipt(evidence_value)
                admitted = evidence_value.get("scientific_updates") == 0
            except ValueError:
                admitted = False
        else:
            parity = evidence_value.get("parity_vs_first_baseline")
            best = evidence_value.get("best_admitted_index")
            admitted = bool(
                evidence_value.get("schema") == THREAD_PROBE_SCHEMA
                and evidence_value.get("status") == expected_status
                and evidence_value.get("scientific_updates") == 0
                and isinstance(parity, list)
                and isinstance(best, int)
                and 0 <= best < len(parity)
                and parity[best].get("passed") is True
                and evidence_value.get("baseline_repeat_parity", {}).get("passed") is True
            )
        if not admitted:
            raise ValueError(f"Technical evidence is not admitted: {name}")
    return value


def migrate_runtime_state(
    *,
    source_directory: Path,
    source_checkpoint: Path,
    old_plan_path: Path,
    new_plan_path: Path,
    admission_path: Path,
    destination_run_root: Path,
    fit_id: str,
    curve_directory: Path | None = None,
) -> dict[str, Any]:
    """Copy one exact durable state into an empty runtime-v2 run root.

    This function performs no optimizer update and never mutates ``source_directory``.
    A caller must supply a separately reviewed admission whose hash is bound by the
    new producer plan.  The returned status is deliberately not a scientific
    endpoint and does not itself authorize resumption.
    """
    source_directory = source_directory.resolve(strict=True)
    source_checkpoint = source_checkpoint.resolve(strict=True)
    if source_checkpoint.parent != source_directory:
        raise ValueError("Source checkpoint must be an immutable file in source_directory")
    old_plan_path = old_plan_path.resolve(strict=True)
    new_plan_path = new_plan_path.resolve(strict=True)
    admission_path = admission_path.resolve(strict=True)
    curve_directory = (
        source_directory if curve_directory is None else curve_directory.resolve(strict=True)
    )
    destination_run_root = destination_run_root.resolve()
    if destination_run_root.exists() or destination_run_root.is_relative_to(source_directory):
        raise FileExistsError("Migration destination must be a new, separate run root")

    old_plan = _validate_plan(old_plan_path)
    new_plan = _validate_plan(new_plan_path)
    if _canonical_plan_identity(old_plan) != _canonical_plan_identity(new_plan):
        raise ValueError("Runtime migration changed the scientific/data producer plan")

    payload = _load_checkpoint(source_checkpoint)
    identity = str(payload["identity_sha256"])
    completed = int(payload["completed_updates"])
    if source_checkpoint.name != f"checkpoint_migration_origin_{completed:06d}.pt":
        raise ValueError("Migration parent must be a uniquely named immutable origin copy")
    if payload.get("identity", {}).get("fit_id") != fit_id:
        raise ValueError("Checkpoint fit identity differs")
    if payload.get("identity", {}).get("producer_contract_sha256") != sha256(old_plan_path):
        raise ValueError("Checkpoint identity does not bind the immutable old producer plan")
    checkpoint_hash = sha256(source_checkpoint)
    pointer, receipt, journal, curves = _validate_source_evidence(
        source_directory, source_checkpoint, payload, curve_directory
    )
    admission = _validate_admission(
        admission_path,
        fit_id=fit_id,
        old_identity=identity,
        checkpoint_sha256=checkpoint_hash,
        old_plan_sha256=str(old_plan["plan_sha256"]),
    )
    if new_plan["input_sha256"].get("runtime_migration_admission") != sha256(admission_path):
        raise ValueError("New plan does not bind the reviewed migration admission")
    old_sources = cast(Mapping[str, str], old_plan["executed_source_sha256"])
    new_sources = cast(Mapping[str, str], new_plan["executed_source_sha256"])
    source_transition = cast(
        Mapping[str, Mapping[str, str | None]], admission["source_sha256_before_after"]
    )
    if set(source_transition) != set(old_sources) | set(new_sources) or any(
        source_transition[name]
        != {
            "before_sha256": old_sources.get(name),
            "after_sha256": new_sources.get(name),
        }
        for name in source_transition
    ):
        raise ValueError("Admission does not bind the complete old-to-new source closure")
    repository = Path(__file__).resolve().parents[2]
    for name, expected in new_sources.items():
        current = (repository / name).resolve(strict=True)
        if not current.is_relative_to(repository) or sha256(current) != expected:
            raise ValueError(f"New runtime source differs after admission: {name}")

    state_hashes = {name: _state_sha256(payload[name]) for name in _STATE_FIELDS}
    external_identity = canonical_sha256(
        {
            "schema": MIGRATION_SCHEMA,
            "fit_id": fit_id,
            "old_identity_sha256": identity,
            "parent_checkpoint_sha256": checkpoint_hash,
            "old_plan_sha256": old_plan["plan_sha256"],
            "new_plan_sha256": new_plan["plan_sha256"],
            "runtime_migration_admission_sha256": sha256(admission_path),
            "state_sha256": state_hashes,
        }
    )

    fit_directory = destination_run_root / "fits" / fit_id
    versions = fit_directory / "checkpoint_versions"
    versions.mkdir(parents=True)
    version_name = f"checkpoint_{completed:06d}.pt"
    version = versions / version_name
    origin_directory = destination_run_root / "RUNTIME_MIGRATION_ORIGIN"
    origin_directory.mkdir()
    origin = origin_directory / version_name
    with tempfile.NamedTemporaryFile(dir=origin_directory, delete=False) as stream:
        temporary = Path(stream.name)
    try:
        shutil.copyfile(source_checkpoint, temporary)
        if sha256(temporary) != checkpoint_hash:
            raise OSError("Copied checkpoint bytes differ")
        os.replace(temporary, origin)
    finally:
        temporary.unlink(missing_ok=True)
    try:
        os.link(origin, version)
    except OSError:
        shutil.copyfile(origin, version)
    alias = fit_directory / "checkpoint_last.pt"
    try:
        os.link(version, alias)
    except OSError:
        shutil.copyfile(version, alias)
    for name in curves:
        shutil.copyfile(curve_directory / name, fit_directory / name)
        if sha256(fit_directory / name) != curves[name]:
            raise OSError(f"Copied curve bytes differ: {name}")

    new_pointer = {**pointer, "version": version_name}
    new_receipt = {**receipt, "checkpoint_path": str(alias.resolve())}
    atomic_write_json(fit_directory / "CHECKPOINT_POINTER.json", new_pointer)
    atomic_write_json(fit_directory / "CHECKPOINT_RECEIPT.json", new_receipt)
    atomic_write_json(fit_directory / "UPDATE_JOURNAL.json", journal)
    shutil.copyfile(new_plan_path, destination_run_root / "PRODUCER_PLAN.json")
    shutil.copyfile(admission_path, destination_run_root / "RUNTIME_MIGRATION_ADMISSION.json")

    copied = _load_checkpoint(origin)
    copied_state = {name: _state_sha256(copied[name]) for name in _STATE_FIELDS}
    if sha256(version) != checkpoint_hash or copied_state != state_hashes:
        raise OSError("Migrated resumable state differs from its immutable parent")
    result: dict[str, Any] = {
        "schema": MIGRATION_SCHEMA,
        "status": "MIGRATED_NOT_RESUMED",
        "fit_id": fit_id,
        "completed_updates": completed,
        "scientific_optimizer_updates": 0,
        "scientific_endpoint": False,
        "checkpoint_interval_updates": CHECKPOINT_INTERVAL,
        "next_periodic_checkpoint_update": ((completed // CHECKPOINT_INTERVAL) + 1)
        * CHECKPOINT_INTERVAL,
        "old_identity_sha256": identity,
        "runtime_binding_sha256": external_identity,
        "parent_checkpoint": {
            "path": str(source_checkpoint),
            "sha256": checkpoint_hash,
        },
        "migrated_checkpoint": {"path": str(origin.resolve()), "sha256": sha256(origin)},
        "initial_resume_checkpoint": {
            "path": str(version.resolve()),
            "sha256": sha256(version),
        },
        "old_plan_sha256": old_plan["plan_sha256"],
        "new_plan_sha256": new_plan["plan_sha256"],
        "runtime_migration_admission_sha256": sha256(admission_path),
        "source_evidence_sha256": {
            "pointer": sha256(source_directory / "CHECKPOINT_POINTER.json"),
            "receipt": sha256(source_directory / "CHECKPOINT_RECEIPT.json"),
            "journal": sha256(source_directory / "UPDATE_JOURNAL.json"),
            "curves": curves,
        },
        "state_sha256": state_hashes,
        "state_equality": {
            name: copied_state[name] == digest for name, digest in state_hashes.items()
        },
        "payload_identity_preserved": copied["identity_sha256"] == identity,
        "recovery_upper": int(copied["recovery_upper"]),
        "accumulation_index": int(copied["accumulation_index"]),
        "admission_status": admission["status"],
    }
    if (
        not all(result["state_equality"].values())
        or result["payload_identity_preserved"] is not True
    ):
        raise OSError("Runtime migration state equality check failed")
    result["migration_sha256"] = canonical_sha256(result)
    atomic_write_json(fit_directory / "RUNTIME_MIGRATION.json", result)
    return result


def validate_runtime_migration(path: Path) -> dict[str, Any]:
    """Validate a completed external binding before a runner consumes it."""
    value = read_json_shared(path.resolve(strict=True))
    without_hash = dict(value)
    claimed = without_hash.pop("migration_sha256", None)
    if (
        value.get("schema") != MIGRATION_SCHEMA
        or value.get("status") != "MIGRATED_NOT_RESUMED"
        or claimed != canonical_sha256(without_hash)
        or value.get("scientific_optimizer_updates") != 0
        or value.get("scientific_endpoint") is not False
        or value.get("checkpoint_interval_updates") != CHECKPOINT_INTERVAL
        or value.get("payload_identity_preserved") is not True
        or set(cast(Mapping[str, bool], value.get("state_equality", {}))) != set(_STATE_FIELDS)
        or not all(cast(Mapping[str, bool], value.get("state_equality", {})).values())
    ):
        raise ValueError("Invalid runtime migration receipt")
    parent = cast(Mapping[str, Any], value["parent_checkpoint"])
    migrated = cast(Mapping[str, Any], value["migrated_checkpoint"])
    if sha256(Path(str(parent["path"]))) != parent["sha256"]:
        raise ValueError("Migration parent checkpoint changed")
    if sha256(Path(str(migrated["path"]))) != migrated["sha256"]:
        raise ValueError("Migrated checkpoint changed")
    if parent["sha256"] != migrated["sha256"]:
        raise ValueError("Migration rewrote the scientific checkpoint")
    expected_binding = canonical_sha256(
        {
            "schema": MIGRATION_SCHEMA,
            "fit_id": value["fit_id"],
            "old_identity_sha256": value["old_identity_sha256"],
            "parent_checkpoint_sha256": parent["sha256"],
            "old_plan_sha256": value["old_plan_sha256"],
            "new_plan_sha256": value["new_plan_sha256"],
            "runtime_migration_admission_sha256": value["runtime_migration_admission_sha256"],
            "state_sha256": value["state_sha256"],
        }
    )
    if value.get("runtime_binding_sha256") != expected_binding:
        raise ValueError("Runtime binding identity differs")
    payload = _load_checkpoint(Path(str(migrated["path"])))
    recorded_states = cast(Mapping[str, str], value.get("state_sha256", {}))
    if set(recorded_states) != set(_STATE_FIELDS) or any(
        _state_sha256(payload[name]) != recorded_states[name] for name in _STATE_FIELDS
    ):
        raise ValueError("Migrated checkpoint state differs from the migration receipt")
    if (
        payload.get("identity_sha256") != value.get("old_identity_sha256")
        or int(payload.get("completed_updates", -1)) != int(value.get("completed_updates", -2))
        or int(payload.get("recovery_upper", -1)) != int(value.get("recovery_upper", -2))
        or int(payload.get("accumulation_index", -1)) != 0
    ):
        raise ValueError("Migrated checkpoint cursor identity differs")
    return value


def _execution_identity_without_lineage(value: Mapping[str, Any]) -> dict[str, Any]:
    """Remove only fields replaced by the reviewed external runtime lineage."""
    result = dict(value)
    for name in (
        "identity_sha256",
        "producer_contract_path",
        "producer_contract_sha256",
        "executed_source_sha256",
    ):
        result.pop(name, None)
    return result


def _validate_execution_identity(
    value: Mapping[str, Any],
    *,
    new_plan_path: Path,
    fit_id: str,
) -> None:
    plan = _validate_plan(new_plan_path)
    claimed = value.get("identity_sha256")
    without_hash = dict(value)
    without_hash.pop("identity_sha256", None)
    if (
        claimed != canonical_sha256(without_hash)
        or value.get("schema") != "final_refit_rgb_producer_identity_v1"
        or value.get("fit_id") != fit_id
        or value.get("producer_contract_sha256") != sha256(new_plan_path)
        or Path(str(value.get("producer_contract_path"))).resolve() != new_plan_path
    ):
        raise ValueError("Current execution identity does not bind the new producer plan")
    sources = value.get("executed_source_sha256")
    if not isinstance(sources, dict) or not sources:
        raise ValueError("Current execution identity has no executable source closure")
    expected_sources = dict(cast(Mapping[str, str], plan["executed_source_sha256"]))
    expected_sources.pop("operational/final_refit/producer_plan.py", None)
    expected_sources["configs/rgb_port/producers.json"] = str(
        plan["input_sha256"]["producer_config"]
    )
    if sources != expected_sources:
        raise ValueError("Current execution identity truncates the admitted source closure")
    repository = Path(__file__).resolve().parents[2]
    for name, expected in sources.items():
        path = (repository / str(name)).resolve(strict=True)
        if not path.is_relative_to(repository) or sha256(path) != expected:
            raise ValueError(f"Current execution source differs: {name}")


def bind_runtime_resume(
    *,
    migration_path: Path,
    new_plan_path: Path,
    execution_identity: Mapping[str, Any],
    binding_path: Path,
) -> dict[str, Any]:
    """Bind changed runtime provenance to an unchanged checkpoint identity.

    The returned ``checkpoint_identity`` is suitable for constructing
    ``ProducerCheckpoint``. It is not an override: it is returned only after
    validating the immutable migration origin, the admitted new plan, the
    current executable closure, and equality of every non-lineage identity
    field. Future checkpoints retain this scientific identity while this
    sidecar records which admitted runtime executed their continuation.
    """
    migration_path = migration_path.resolve(strict=True)
    new_plan_path = new_plan_path.resolve(strict=True)
    binding_path = binding_path.resolve()
    migration = validate_runtime_migration(migration_path)
    plan = _validate_plan(new_plan_path)
    if (
        plan["plan_sha256"] != migration["new_plan_sha256"]
        or sha256(new_plan_path) != sha256(Path(str(execution_identity["producer_contract_path"])))
        or plan["input_sha256"].get("runtime_migration_admission")
        != migration["runtime_migration_admission_sha256"]
    ):
        raise ValueError("Runtime migration and new producer plan differ")
    fit_id = str(migration["fit_id"])
    _validate_execution_identity(execution_identity, new_plan_path=new_plan_path, fit_id=fit_id)
    origin = Path(str(cast(Mapping[str, Any], migration["migrated_checkpoint"])["path"]))
    payload = _load_checkpoint(origin)
    checkpoint_identity = cast(Mapping[str, Any], payload["identity"])
    if payload["identity_sha256"] != migration[
        "old_identity_sha256"
    ] or _execution_identity_without_lineage(
        checkpoint_identity
    ) != _execution_identity_without_lineage(execution_identity):
        raise ValueError("New runtime changed the checkpoint's scientific execution identity")
    value: dict[str, Any] = {
        "schema": RUNTIME_BINDING_SCHEMA,
        "status": "BOUND_NOT_RESUMED",
        "fit_id": fit_id,
        "migration_path": str(migration_path),
        "migration_sha256": sha256(migration_path),
        "migration_identity_sha256": migration["migration_sha256"],
        "new_plan_path": str(new_plan_path),
        "new_plan_file_sha256": sha256(new_plan_path),
        "new_plan_sha256": plan["plan_sha256"],
        "runtime_migration_admission_sha256": migration["runtime_migration_admission_sha256"],
        "checkpoint_identity_sha256": payload["identity_sha256"],
        "execution_identity_sha256": execution_identity["identity_sha256"],
        "execution_source_sha256": execution_identity["executed_source_sha256"],
        "optimizer_mode_unchanged": True,
        "checkpoint_interval_updates": CHECKPOINT_INTERVAL,
        "scientific_optimizer_updates_during_binding": 0,
        "identity_policy": "PRESERVED_CHECKPOINT_IDENTITY_EXTERNAL_RUNTIME_LINEAGE",
    }
    value["binding_sha256"] = canonical_sha256(value)
    if binding_path.exists():
        existing = read_json_shared(binding_path)
        if existing != value:
            raise ValueError("Fit is already bound to a different runtime execution identity")
    else:
        atomic_write_json(binding_path, value)
    return dict(checkpoint_identity)


def validate_runtime_binding(
    *,
    binding_path: Path,
    execution_identity: Mapping[str, Any],
) -> dict[str, Any]:
    """Revalidate an existing runtime sidecar and return checkpoint identity."""
    binding_path = binding_path.resolve(strict=True)
    binding = read_json_shared(binding_path)
    without_hash = dict(binding)
    claimed = without_hash.pop("binding_sha256", None)
    if (
        binding.get("schema") != RUNTIME_BINDING_SCHEMA
        or binding.get("status") != "BOUND_NOT_RESUMED"
        or claimed != canonical_sha256(without_hash)
        or binding.get("scientific_optimizer_updates_during_binding") != 0
        or binding.get("optimizer_mode_unchanged") is not True
        or binding.get("checkpoint_interval_updates") != CHECKPOINT_INTERVAL
        or binding.get("execution_identity_sha256") != execution_identity.get("identity_sha256")
    ):
        raise ValueError("Runtime resume binding differs from current execution")
    migration_path = Path(str(binding["migration_path"]))
    new_plan_path = Path(str(binding["new_plan_path"]))
    if (
        sha256(migration_path) != binding["migration_sha256"]
        or sha256(new_plan_path) != binding["new_plan_file_sha256"]
    ):
        raise ValueError("Runtime resume binding parents changed")
    migration = validate_runtime_migration(migration_path)
    if migration["migration_sha256"] != binding["migration_identity_sha256"]:
        raise ValueError("Runtime migration identity changed")
    _validate_execution_identity(
        execution_identity,
        new_plan_path=new_plan_path,
        fit_id=str(binding["fit_id"]),
    )
    origin = Path(str(cast(Mapping[str, Any], migration["migrated_checkpoint"])["path"]))
    payload = _load_checkpoint(origin)
    checkpoint_identity = cast(Mapping[str, Any], payload["identity"])
    if payload["identity_sha256"] != binding[
        "checkpoint_identity_sha256"
    ] or _execution_identity_without_lineage(
        checkpoint_identity
    ) != _execution_identity_without_lineage(execution_identity):
        raise ValueError("Runtime binding no longer preserves checkpoint identity")
    return dict(checkpoint_identity)


def resolve_runtime_checkpoint_identity(
    *,
    run_root: Path,
    fit_id: str,
    new_identity: Mapping[str, Any],
    new_plan_path: Path,
) -> dict[str, Any]:
    """Resolve the preserved identity for initial and later runtime-v2 resumes.

    This is the runner integration entry point.  The immutable migration origin
    remains outside ``checkpoint_versions`` so normal version garbage collection
    cannot erase the proof.  The active pointer may advance, but never regress
    behind the migrated cursor or change checkpoint identity.
    """
    run_root = run_root.resolve(strict=True)
    fit_directory = run_root / "fits" / fit_id
    migration_path = fit_directory / "RUNTIME_MIGRATION.json"
    binding_path = fit_directory / "RUNTIME_RESUME_BINDING.json"
    if binding_path.is_file():
        checkpoint_identity = validate_runtime_binding(
            binding_path=binding_path,
            execution_identity=new_identity,
        )
    else:
        checkpoint_identity = bind_runtime_resume(
            migration_path=migration_path,
            new_plan_path=new_plan_path,
            execution_identity=new_identity,
            binding_path=binding_path,
        )
    migration = validate_runtime_migration(migration_path)
    pointer = read_json_shared(fit_directory / "CHECKPOINT_POINTER.json")
    version = fit_directory / "checkpoint_versions" / str(pointer.get("version"))
    if (
        pointer.get("schema") != "rgb_port_checkpoint_pointer_v1"
        or pointer.get("identity_sha256") != checkpoint_identity["identity_sha256"]
        or int(pointer.get("completed_updates", -1)) < int(migration["completed_updates"])
        or not version.is_file()
        or pointer.get("checkpoint_sha256") != sha256(version)
    ):
        raise ValueError("Active runtime checkpoint does not descend from the migration cursor")
    return checkpoint_identity


def runtime_checkpoint_binding_kwargs(*, run_root: Path, fit_id: str) -> dict[str, str]:
    """Return the two verified keyword arguments for ``ProducerCheckpoint``."""
    fit_directory = run_root.resolve(strict=True) / "fits" / fit_id
    binding_path = fit_directory / "RUNTIME_RESUME_BINDING.json"
    binding = read_json_shared(binding_path)
    without_hash = dict(binding)
    claimed = without_hash.pop("binding_sha256", None)
    if (
        binding.get("schema") != RUNTIME_BINDING_SCHEMA
        or claimed != canonical_sha256(without_hash)
        or binding.get("fit_id") != fit_id
    ):
        raise ValueError("Runtime checkpoint binding keyword source is invalid")
    migration = validate_runtime_migration(fit_directory / "RUNTIME_MIGRATION.json")
    origin = cast(Mapping[str, Any], migration["migrated_checkpoint"])
    return {
        "execution_binding_sha256": str(claimed),
        "execution_binding_origin_checkpoint_sha256": str(origin["sha256"]),
    }


def _validate_prior_execution_lineage(
    *,
    fit_id: str,
    payload: Mapping[str, Any],
    pointer: Mapping[str, Any],
    receipt: Mapping[str, Any],
    journal: Mapping[str, Any],
    prior_binding_path: Path | None,
) -> dict[str, Any] | None:
    """Prove an unbound, v1-bound, or chained v2-bound parent runtime."""
    observed_values = [
        value.get("execution_binding_sha256") for value in (payload, pointer, receipt, journal)
    ]
    if all(value is None for value in observed_values):
        if prior_binding_path is not None:
            raise ValueError("A prior binding was supplied for an unbound checkpoint")
        return None
    if prior_binding_path is None:
        raise ValueError("Bound source state requires its prior binding")
    binding_path = prior_binding_path.resolve(strict=True)
    binding = read_json_shared(binding_path)
    without_hash = dict(binding)
    claimed = without_hash.pop("binding_sha256", None)
    if (
        binding.get("schema") not in {RUNTIME_BINDING_SCHEMA, RUNTIME_BINDING_V3_SCHEMA}
        or binding.get("fit_id") != fit_id
        or claimed != canonical_sha256(without_hash)
        or binding.get("checkpoint_identity_sha256") != payload.get("identity_sha256")
        or binding.get("scientific_optimizer_updates_during_binding") != 0
        or binding.get("optimizer_mode_unchanged") is not True
    ):
        raise ValueError("Prior runtime binding is invalid")
    prior_plan_path = Path(str(binding["new_plan_path"])).resolve(strict=True)
    if sha256(prior_plan_path) != binding.get("new_plan_file_sha256"):
        raise ValueError("Prior runtime plan changed")
    result: dict[str, Any] = {
        "path": str(binding_path),
        "sha256": sha256(binding_path),
        "binding_sha256": claimed,
        "new_plan_path": str(prior_plan_path),
        "new_plan_file_sha256": sha256(prior_plan_path),
    }
    if binding["schema"] == RUNTIME_BINDING_SCHEMA:
        if any(value is None for value in observed_values) or set(observed_values) != {claimed}:
            raise ValueError("V1-bound source state has inconsistent execution bindings")
        prior_migration_path = Path(str(binding["migration_path"])).resolve(strict=True)
        if sha256(prior_migration_path) != binding.get("migration_sha256"):
            raise ValueError("Prior migration receipt changed")
        prior_migration = validate_runtime_migration(prior_migration_path)
        if (
            prior_migration.get("fit_id") != fit_id
            or prior_migration.get("old_identity_sha256") != payload.get("identity_sha256")
            or prior_migration.get("migration_sha256") != binding.get("migration_identity_sha256")
        ):
            raise ValueError("Prior migration lineage differs from the checkpoint")
        result.update(
            migration_path=str(prior_migration_path),
            migration_file_sha256=sha256(prior_migration_path),
            migration_sha256=prior_migration["migration_sha256"],
            previous_execution_binding_sha256=None,
            prior_runtime_sidecar_binding_sha256=claimed,
            parent_embedded_execution_binding_sha256=claimed,
        )
        return result

    batch_path = Path(str(binding["batch_migration_path"])).resolve(strict=True)
    fit_migration_path = Path(str(binding["fit_migration_path"])).resolve(strict=True)
    if sha256(batch_path) != binding.get("batch_migration_file_sha256") or sha256(
        fit_migration_path
    ) != binding.get("fit_migration_file_sha256"):
        raise ValueError("Prior v2 runtime lineage files changed")
    batch = validate_runtime_batch_migration(
        batch_path,
        new_plan_path=prior_plan_path,
        optimization_admission_path=batch_path.parent / "RUNTIME_OPTIMIZATION_ADMISSION.json",
    )
    fit_migration = read_json_shared(fit_migration_path)
    if (
        batch.get("batch_migration_sha256") != binding.get("batch_migration_sha256")
        or batch["fits"][fit_id]["fit_migration_sha256"] != binding.get("fit_migration_sha256")
        or fit_migration.get("fit_migration_sha256") != binding.get("fit_migration_sha256")
    ):
        raise ValueError("Prior v2 batch lineage differs")
    previous = binding.get("previous_execution_binding_sha256")
    if len(set(observed_values)) != 1:
        raise ValueError("V2-bound durable parent has mixed embedded bindings")
    embedded = observed_values[0]
    allowed = {claimed}
    if pointer.get("checkpoint_sha256") == fit_migration["parent_checkpoint"]["sha256"]:
        allowed.add(previous)
    if embedded not in allowed:
        raise ValueError("V2-bound source state has an inadmissible execution binding")
    result.update(
        migration_path=str(fit_migration_path),
        migration_file_sha256=sha256(fit_migration_path),
        migration_sha256=fit_migration["fit_migration_sha256"],
        batch_migration_path=str(batch_path),
        batch_migration_sha256=batch["batch_migration_sha256"],
        previous_execution_binding_sha256=previous,
        prior_runtime_sidecar_binding_sha256=claimed,
        parent_embedded_execution_binding_sha256=embedded,
    )
    return result


def migrate_runtime_states_v3(
    *,
    source_run_root: Path,
    old_plan_path: Path,
    new_plan_path: Path,
    optimization_admission_path: Path,
    destination_run_root: Path,
    fits: Mapping[str, Mapping[str, Path | None]],
) -> dict[str, Any]:
    """Atomically migrate both trained RGB fits into the admitted v3 runtime."""
    expected_fits = {"R_A5", "R_C2F"}
    if set(fits) != expected_fits:
        raise ValueError("V3 runtime migration requires exactly R_A5 and R_C2F")
    source_run_root = source_run_root.resolve(strict=True)
    old_plan_path = old_plan_path.resolve(strict=True)
    new_plan_path = new_plan_path.resolve(strict=True)
    optimization_admission_path = optimization_admission_path.resolve(strict=True)
    destination_run_root = destination_run_root.resolve()
    if destination_run_root.exists() or destination_run_root.is_relative_to(source_run_root):
        raise FileExistsError("V3 migration destination must be new and separate")
    old_plan = _validate_plan(old_plan_path)
    new_plan = _validate_plan(new_plan_path)
    admission = _validate_optimization_admission(
        optimization_admission_path, old_plan=old_plan, new_plan=new_plan
    )
    admission_file_sha = sha256(optimization_admission_path)
    if new_plan["input_sha256"].get("runtime_optimization_admission") != admission_file_sha:
        raise ValueError("New plan does not bind the optimization admission")

    prepared: dict[str, dict[str, Any]] = {}
    required_spec = {
        "source_directory",
        "source_checkpoint",
        "curve_directory",
        "prior_runtime_binding",
    }
    for fit_id in sorted(expected_fits):
        spec = fits[fit_id]
        if set(spec) != required_spec:
            raise ValueError(f"Malformed migration specification for {fit_id}")
        source_directory = Path(str(spec["source_directory"])).resolve(strict=True)
        source_checkpoint = Path(str(spec["source_checkpoint"])).resolve(strict=True)
        curve_value = spec["curve_directory"]
        curve_directory = (
            source_directory if curve_value is None else Path(str(curve_value)).resolve(strict=True)
        )
        prior_value = spec["prior_runtime_binding"]
        prior_path = None if prior_value is None else Path(str(prior_value))
        if not source_directory.is_relative_to(source_run_root):
            raise ValueError("Fit source directory is outside the admitted source run")
        if source_checkpoint.parent != source_directory:
            raise ValueError("Fit parent must be an immutable checkpoint in its source directory")
        payload = _load_checkpoint(source_checkpoint)
        completed = int(payload["completed_updates"])
        if (
            completed <= 0
            or source_checkpoint.name != f"checkpoint_migration_origin_{completed:06d}.pt"
        ):
            raise ValueError("Each fit requires a trained, uniquely named immutable parent")
        if payload.get("identity", {}).get("fit_id") != fit_id:
            raise ValueError("Checkpoint fit identity differs")
        pointer, receipt, journal, curves = _validate_source_evidence(
            source_directory, source_checkpoint, payload, curve_directory
        )
        prior = _validate_prior_execution_lineage(
            fit_id=fit_id,
            payload=payload,
            pointer=pointer,
            receipt=receipt,
            journal=journal,
            prior_binding_path=prior_path,
        )
        if prior is None:
            if payload.get("identity", {}).get("producer_contract_sha256") != sha256(old_plan_path):
                raise ValueError("Unbound checkpoint does not bind the exact old plan")
        elif prior["new_plan_file_sha256"] != sha256(old_plan_path):
            raise ValueError("Prior runtime lineage does not terminate at the old plan")
        prepared[fit_id] = {
            "source_directory": source_directory,
            "source_checkpoint": source_checkpoint,
            "curve_directory": curve_directory,
            "payload": payload,
            "completed": completed,
            "checkpoint_sha256": sha256(source_checkpoint),
            "pointer": pointer,
            "receipt": receipt,
            "journal": journal,
            "curves": curves,
            "prior": prior,
            "state_sha256": {name: _state_sha256(payload[name]) for name in _STATE_FIELDS},
        }

    temporary_root = Path(
        tempfile.mkdtemp(prefix=f".{destination_run_root.name}.", dir=destination_run_root.parent)
    )
    try:
        shutil.copyfile(new_plan_path, temporary_root / "PRODUCER_PLAN.json")
        shutil.copyfile(
            optimization_admission_path,
            temporary_root / "RUNTIME_OPTIMIZATION_ADMISSION.json",
        )
        fit_receipts: dict[str, dict[str, Any]] = {}
        for fit_id, item in prepared.items():
            fit_directory = temporary_root / "fits" / fit_id
            versions = fit_directory / "checkpoint_versions"
            versions.mkdir(parents=True)
            version_name = f"checkpoint_{item['completed']:06d}.pt"
            origin_directory = temporary_root / "RUNTIME_MIGRATION_ORIGIN" / fit_id
            origin_directory.mkdir(parents=True)
            origin = origin_directory / version_name
            shutil.copyfile(item["source_checkpoint"], origin)
            version = versions / version_name
            try:
                os.link(origin, version)
            except OSError:
                shutil.copyfile(origin, version)
            alias = fit_directory / "checkpoint_last.pt"
            try:
                os.link(version, alias)
            except OSError:
                shutil.copyfile(version, alias)
            if (
                sha256(origin) != item["checkpoint_sha256"]
                or sha256(alias) != item["checkpoint_sha256"]
            ):
                raise OSError("V3 migration rewrote checkpoint bytes")
            for name, expected_sha in item["curves"].items():
                target = fit_directory / name
                shutil.copyfile(item["curve_directory"] / name, target)
                if sha256(target) != expected_sha:
                    raise OSError(f"V3 migration rewrote curve bytes: {name}")
            atomic_write_json(
                fit_directory / "CHECKPOINT_POINTER.json",
                {**item["pointer"], "version": version_name},
            )
            atomic_write_json(
                fit_directory / "CHECKPOINT_RECEIPT.json",
                {
                    **item["receipt"],
                    "checkpoint_path": str(
                        (destination_run_root / "fits" / fit_id / "checkpoint_last.pt").resolve()
                    ),
                },
            )
            atomic_write_json(fit_directory / "UPDATE_JOURNAL.json", item["journal"])
            destination_evidence = {
                "pointer": sha256(fit_directory / "CHECKPOINT_POINTER.json"),
                "receipt": sha256(fit_directory / "CHECKPOINT_RECEIPT.json"),
                "journal": sha256(fit_directory / "UPDATE_JOURNAL.json"),
                "curves": {name: sha256(fit_directory / name) for name in item["curves"]},
                "version": sha256(version),
                "alias": sha256(alias),
            }
            fit_receipt: dict[str, Any] = {
                "schema": FIT_MIGRATION_SCHEMA,
                "status": "MIGRATED_NOT_RESUMED",
                "fit_id": fit_id,
                "completed_updates": item["completed"],
                "scientific_optimizer_updates": 0,
                "scientific_endpoint": False,
                "original_scientific_identity_sha256": item["payload"]["identity_sha256"],
                "parent_checkpoint": {
                    "path": str(item["source_checkpoint"]),
                    "sha256": item["checkpoint_sha256"],
                },
                "migrated_checkpoint": {
                    "path": str(
                        (
                            destination_run_root
                            / "RUNTIME_MIGRATION_ORIGIN"
                            / fit_id
                            / version_name
                        ).resolve()
                    ),
                    "sha256": sha256(origin),
                },
                "initial_resume_checkpoint": {
                    "path": str(
                        (
                            destination_run_root
                            / "fits"
                            / fit_id
                            / "checkpoint_versions"
                            / version_name
                        ).resolve()
                    ),
                    "sha256": sha256(version),
                },
                "state_sha256": item["state_sha256"],
                "prior_runtime_lineage": item["prior"],
                "source_evidence_sha256": {
                    "pointer": sha256(item["source_directory"] / "CHECKPOINT_POINTER.json"),
                    "receipt": sha256(item["source_directory"] / "CHECKPOINT_RECEIPT.json"),
                    "journal": sha256(item["source_directory"] / "UPDATE_JOURNAL.json"),
                    "curves": item["curves"],
                },
                "destination_evidence_sha256": destination_evidence,
            }
            fit_receipt["fit_migration_sha256"] = canonical_sha256(fit_receipt)
            atomic_write_json(fit_directory / "RUNTIME_MIGRATION_V3.json", fit_receipt)
            fit_receipts[fit_id] = fit_receipt
        batch: dict[str, Any] = {
            "schema": BATCH_MIGRATION_SCHEMA,
            "status": "MIGRATED_NOT_RESUMED",
            "scientific_optimizer_updates": 0,
            "scientific_endpoint": False,
            "old_plan_sha256": old_plan["plan_sha256"],
            "new_plan_sha256": new_plan["plan_sha256"],
            "optimization_admission_sha256": admission_file_sha,
            "optimization_admission_identity_sha256": admission["admission_sha256"],
            "fits": {
                fit_id: {
                    "fit_migration_sha256": receipt["fit_migration_sha256"],
                    "original_scientific_identity_sha256": receipt[
                        "original_scientific_identity_sha256"
                    ],
                    "completed_updates": receipt["completed_updates"],
                    "parent_checkpoint_sha256": receipt["parent_checkpoint"]["sha256"],
                    "prior_runtime_sidecar_binding_sha256": (
                        None
                        if receipt["prior_runtime_lineage"] is None
                        else receipt["prior_runtime_lineage"][
                            "prior_runtime_sidecar_binding_sha256"
                        ]
                    ),
                    "parent_embedded_execution_binding_sha256": (
                        None
                        if receipt["prior_runtime_lineage"] is None
                        else receipt["prior_runtime_lineage"][
                            "parent_embedded_execution_binding_sha256"
                        ]
                    ),
                }
                for fit_id, receipt in fit_receipts.items()
            },
        }
        batch["batch_migration_sha256"] = canonical_sha256(batch)
        atomic_write_json(temporary_root / "RUNTIME_MIGRATION_BATCH.json", batch)
        os.replace(temporary_root, destination_run_root)
    except BaseException:
        shutil.rmtree(temporary_root, ignore_errors=True)
        raise
    return validate_runtime_batch_migration(
        destination_run_root / "RUNTIME_MIGRATION_BATCH.json",
        new_plan_path=destination_run_root / "PRODUCER_PLAN.json",
        optimization_admission_path=destination_run_root / "RUNTIME_OPTIMIZATION_ADMISSION.json",
    )


def validate_runtime_batch_migration(
    path: Path,
    *,
    new_plan_path: Path,
    optimization_admission_path: Path,
) -> dict[str, Any]:
    """Rehash the complete two-fit v3 migration before a runner consumes it."""
    path = path.resolve(strict=True)
    value = read_json_shared(path)
    without_hash = dict(value)
    claimed = without_hash.pop("batch_migration_sha256", None)
    fits = value.get("fits")
    if (
        value.get("schema") != BATCH_MIGRATION_SCHEMA
        or value.get("status") != "MIGRATED_NOT_RESUMED"
        or claimed != canonical_sha256(without_hash)
        or value.get("scientific_optimizer_updates") != 0
        or value.get("scientific_endpoint") is not False
        or not isinstance(fits, dict)
        or set(fits) != {"R_A5", "R_C2F"}
    ):
        raise ValueError("Invalid v3 batch migration receipt")
    plan = _validate_plan(new_plan_path.resolve(strict=True))
    admission_path = optimization_admission_path.resolve(strict=True)
    if (
        plan.get("plan_sha256") != value.get("new_plan_sha256")
        or sha256(admission_path) != value.get("optimization_admission_sha256")
        or plan["input_sha256"].get("runtime_optimization_admission")
        != value.get("optimization_admission_sha256")
    ):
        raise ValueError("V3 batch plan or admission binding differs")
    root = path.parent
    for fit_id, summary in fits.items():
        fit_path = root / "fits" / fit_id / "RUNTIME_MIGRATION_V3.json"
        receipt = read_json_shared(fit_path.resolve(strict=True))
        receipt_without_hash = dict(receipt)
        receipt_claimed = receipt_without_hash.pop("fit_migration_sha256", None)
        parent = cast(Mapping[str, Any], receipt.get("parent_checkpoint", {}))
        migrated = cast(Mapping[str, Any], receipt.get("migrated_checkpoint", {}))
        if (
            receipt.get("schema") != FIT_MIGRATION_SCHEMA
            or receipt.get("status") != "MIGRATED_NOT_RESUMED"
            or receipt.get("fit_id") != fit_id
            or receipt.get("scientific_optimizer_updates") != 0
            or receipt.get("scientific_endpoint") is not False
            or receipt_claimed != canonical_sha256(receipt_without_hash)
            or receipt_claimed != summary.get("fit_migration_sha256")
            or receipt.get("original_scientific_identity_sha256")
            != summary.get("original_scientific_identity_sha256")
            or parent.get("sha256") != summary.get("parent_checkpoint_sha256")
            or sha256(Path(str(parent.get("path"))).resolve(strict=True)) != parent.get("sha256")
            or sha256(Path(str(migrated.get("path"))).resolve(strict=True))
            != migrated.get("sha256")
            or migrated.get("sha256") != parent.get("sha256")
        ):
            raise ValueError(f"Invalid v3 fit migration receipt: {fit_id}")
        payload = _load_checkpoint(Path(str(migrated["path"])))
        states = cast(Mapping[str, str], receipt.get("state_sha256", {}))
        if (
            payload.get("identity_sha256") != receipt["original_scientific_identity_sha256"]
            or set(states) != set(_STATE_FIELDS)
            or any(_state_sha256(payload[name]) != states[name] for name in _STATE_FIELDS)
        ):
            raise ValueError(f"Migrated v3 state differs: {fit_id}")
        fit_directory = root / "fits" / fit_id
        initial = cast(Mapping[str, Any], receipt.get("initial_resume_checkpoint", {}))
        initial_path = Path(str(initial.get("path")))
        alias = fit_directory / "checkpoint_last.pt"
        active_pointer = read_json_shared(fit_directory / "CHECKPOINT_POINTER.json")
        active_version = (
            fit_directory / "checkpoint_versions" / str(active_pointer.get("version"))
        ).resolve(strict=True)
        active_payload = _load_checkpoint(active_version)
        active_completed = int(active_payload.get("completed_updates", -1))
        migrated_completed = int(receipt["completed_updates"])
        versions_directory = (fit_directory / "checkpoint_versions").resolve(strict=True)
        if (
            active_version.parent != versions_directory
            or active_version.name != f"checkpoint_{active_completed:06d}.pt"
        ):
            raise ValueError(f"V3 active checkpoint path is not canonical: {fit_id}")
        active_hash = sha256(active_version)
        if (
            active_pointer.get("schema") != "rgb_port_checkpoint_pointer_v1"
            or active_pointer.get("identity_sha256")
            != receipt["original_scientific_identity_sha256"]
            or int(active_pointer.get("completed_updates", -1)) != active_completed
            or active_pointer.get("checkpoint_sha256") != active_hash
        ):
            raise ValueError(f"V3 active checkpoint pointer differs: {fit_id}")
        observed_curve_hashes = {
            curve.name: sha256(curve) for curve in sorted(fit_directory.glob("curve_*.json"))
        }
        source_curves = cast(
            Mapping[str, str],
            cast(Mapping[str, Any], receipt["source_evidence_sha256"])["curves"],
        )
        destination_evidence = receipt.get("destination_evidence_sha256")
        prior = receipt.get("prior_runtime_lineage")
        prior_sidecar, parent_embedded = _prior_bindings_from_summary(summary)
        if (
            initial.get("sha256") != parent.get("sha256")
            or active_completed < migrated_completed
            or active_payload.get("identity_sha256")
            != receipt["original_scientific_identity_sha256"]
            or any(
                observed_curve_hashes.get(name) != digest for name, digest in source_curves.items()
            )
        ):
            raise ValueError(f"V3 destination evidence changed: {fit_id}")
        initial_exact = bool(
            active_completed == migrated_completed
            and initial_path.is_file()
            and initial_path.resolve().parent == versions_directory
            and initial_path.name == f"checkpoint_{migrated_completed:06d}.pt"
            and sha256(initial_path) == parent.get("sha256")
            and initial_path.resolve() == active_version
            and alias.is_file()
            and destination_evidence
            == {
                "pointer": sha256(fit_directory / "CHECKPOINT_POINTER.json"),
                "receipt": sha256(fit_directory / "CHECKPOINT_RECEIPT.json"),
                "journal": sha256(fit_directory / "UPDATE_JOURNAL.json"),
                "curves": observed_curve_hashes,
                "version": active_hash,
                "alias": sha256(alias),
            }
        )
        if initial_exact:
            _validate_curve_coverage(fit_directory, active_payload)
        else:
            binding_path = fit_directory / "RUNTIME_RESUME_BINDING.json"
            binding = read_json_shared(binding_path.resolve(strict=True))
            binding_without_hash = dict(binding)
            binding_sha = binding_without_hash.pop("binding_sha256", None)
            journal = read_json_shared(fit_directory / "UPDATE_JOURNAL.json")
            journal_completed = int(journal.get("completed_updates", -1))
            journal_durable = int(journal.get("durable_updates", -1))
            journal_pending = int(journal.get("pending_update_upper", -1))
            journal_recovery = int(journal.get("recovery_upper", -1))
            recovery_after_restore = (
                max(int(active_payload["recovery_upper"]), journal_recovery)
                + max(0, journal_completed - active_completed)
                + journal_pending
            )
            destination_curves = _validate_curve_coverage(
                fit_directory,
                active_payload,
                recoverable_upper=journal_completed + journal_pending,
            )
            allowed_bindings = {binding_sha}
            if active_hash == parent.get("sha256"):
                allowed_bindings.add(parent_embedded)
            if (
                binding.get("schema") != RUNTIME_BINDING_V3_SCHEMA
                or binding.get("fit_id") != fit_id
                or binding_sha != canonical_sha256(binding_without_hash)
                or binding.get("batch_migration_sha256") != value["batch_migration_sha256"]
                or binding.get("fit_migration_sha256") != receipt["fit_migration_sha256"]
                or active_payload.get("execution_binding_sha256") not in allowed_bindings
                or active_pointer.get("execution_binding_sha256") not in allowed_bindings
                or journal.get("schema") != "rgb_port_update_journal_v1"
                or journal.get("identity_sha256") != receipt["original_scientific_identity_sha256"]
                or journal.get("execution_binding_sha256") not in allowed_bindings
                or journal_completed < active_completed
                or journal_completed - active_completed + journal_pending > CHECKPOINT_INTERVAL
                or journal_durable < 0
                or journal_durable > active_completed
                or journal_pending not in {0, 1}
                or journal_recovery < 0
                or recovery_after_restore > 11_092
                or destination_curves != observed_curve_hashes
            ):
                raise ValueError(f"V3 recoverable descendant differs: {fit_id}")
        if (prior is None) != (prior_sidecar is None):
            raise ValueError(f"Prior binding summary differs: {fit_id}")
        if prior is not None and (
            sha256(Path(str(prior["path"])).resolve(strict=True)) != prior["sha256"]
            or prior.get("prior_runtime_sidecar_binding_sha256", prior.get("binding_sha256"))
            != prior_sidecar
            or prior.get("parent_embedded_execution_binding_sha256", prior.get("binding_sha256"))
            != parent_embedded
            or sha256(Path(str(prior["migration_path"])).resolve(strict=True))
            != prior["migration_file_sha256"]
        ):
            raise ValueError(f"Prior runtime lineage changed: {fit_id}")
    return value


def resolve_runtime_checkpoint_identity_v3(
    *,
    run_root: Path,
    fit_id: str,
    new_identity: Mapping[str, Any],
    new_plan_path: Path,
) -> dict[str, Any]:
    """Bind one fit from a validated two-fit batch to its v3 execution identity."""
    run_root = run_root.resolve(strict=True)
    new_plan_path = new_plan_path.resolve(strict=True)
    batch_path = run_root / "RUNTIME_MIGRATION_BATCH.json"
    admission_path = run_root / "RUNTIME_OPTIMIZATION_ADMISSION.json"
    batch = validate_runtime_batch_migration(
        batch_path,
        new_plan_path=new_plan_path,
        optimization_admission_path=admission_path,
    )
    if fit_id not in batch["fits"]:
        raise ValueError("Fit is outside the sealed v3 batch migration")
    _validate_execution_identity(new_identity, new_plan_path=new_plan_path, fit_id=fit_id)
    fit_directory = run_root / "fits" / fit_id
    migration_path = fit_directory / "RUNTIME_MIGRATION_V3.json"
    migration = read_json_shared(migration_path)
    origin = Path(str(migration["migrated_checkpoint"]["path"])).resolve(strict=True)
    payload = _load_checkpoint(origin)
    checkpoint_identity = cast(Mapping[str, Any], payload["identity"])
    if payload.get("identity_sha256") != migration.get(
        "original_scientific_identity_sha256"
    ) or _execution_identity_without_lineage(
        checkpoint_identity
    ) != _execution_identity_without_lineage(new_identity):
        raise ValueError("V3 execution changed the checkpoint scientific identity")
    binding_path = fit_directory / "RUNTIME_RESUME_BINDING.json"
    prior_sidecar, parent_embedded = _prior_bindings_from_summary(batch["fits"][fit_id])
    value: dict[str, Any] = {
        "schema": RUNTIME_BINDING_V3_SCHEMA,
        "status": "BOUND_NOT_RESUMED",
        "fit_id": fit_id,
        "batch_migration_path": str(batch_path),
        "batch_migration_file_sha256": sha256(batch_path),
        "batch_migration_sha256": batch["batch_migration_sha256"],
        "fit_migration_path": str(migration_path),
        "fit_migration_file_sha256": sha256(migration_path),
        "fit_migration_sha256": migration["fit_migration_sha256"],
        "new_plan_path": str(new_plan_path),
        "new_plan_file_sha256": sha256(new_plan_path),
        "new_plan_sha256": batch["new_plan_sha256"],
        "optimization_admission_sha256": batch["optimization_admission_sha256"],
        "checkpoint_identity_sha256": payload["identity_sha256"],
        "execution_identity_sha256": new_identity["identity_sha256"],
        "execution_source_sha256": new_identity["executed_source_sha256"],
        "previous_execution_binding_sha256": prior_sidecar,
        "origin_embedded_execution_binding_sha256": parent_embedded,
        "optimizer_mode_unchanged": True,
        "checkpoint_interval_updates": CHECKPOINT_INTERVAL,
        "scientific_optimizer_updates_during_binding": 0,
        "identity_policy": "PRESERVED_ORIGINAL_SCIENTIFIC_IDENTITY_CHAINED_RUNTIME_LINEAGE",
    }
    value["binding_sha256"] = canonical_sha256(value)
    if binding_path.exists():
        existing = read_json_shared(binding_path)
        if existing != value:
            raise ValueError("Fit is already bound to a different v3 execution identity")
    else:
        atomic_write_json(binding_path, value)
    pointer = read_json_shared(fit_directory / "CHECKPOINT_POINTER.json")
    if (
        pointer.get("identity_sha256") != payload["identity_sha256"]
        or int(pointer.get("completed_updates", -1)) < int(migration["completed_updates"])
        or pointer.get("checkpoint_sha256")
        != sha256(fit_directory / "checkpoint_versions" / str(pointer.get("version")))
    ):
        raise ValueError("Active v3 checkpoint does not descend from its migration cursor")
    return dict(checkpoint_identity)


def runtime_checkpoint_binding_kwargs_v3(*, run_root: Path, fit_id: str) -> dict[str, str]:
    """Return verified ProducerCheckpoint binding arguments for a v3 fit."""
    fit_directory = run_root.resolve(strict=True) / "fits" / fit_id
    binding = read_json_shared(fit_directory / "RUNTIME_RESUME_BINDING.json")
    without_hash = dict(binding)
    claimed = without_hash.pop("binding_sha256", None)
    if (
        binding.get("schema") != RUNTIME_BINDING_V3_SCHEMA
        or binding.get("fit_id") != fit_id
        or claimed != canonical_sha256(without_hash)
    ):
        raise ValueError("V3 runtime checkpoint binding keyword source is invalid")
    batch_path = Path(str(binding["batch_migration_path"])).resolve(strict=True)
    plan_path = Path(str(binding["new_plan_path"])).resolve(strict=True)
    if sha256(batch_path) != binding.get("batch_migration_file_sha256") or sha256(
        plan_path
    ) != binding.get("new_plan_file_sha256"):
        raise ValueError("V3 runtime binding parents changed")
    batch = validate_runtime_batch_migration(
        batch_path,
        new_plan_path=plan_path,
        optimization_admission_path=(batch_path.parent / "RUNTIME_OPTIMIZATION_ADMISSION.json"),
    )
    migration_path = fit_directory / "RUNTIME_MIGRATION_V3.json"
    migration = read_json_shared(migration_path)
    batch_summary = batch["fits"][fit_id]
    batch_prior_sidecar, batch_parent_embedded = _prior_bindings_from_summary(batch_summary)
    binding_prior_sidecar, binding_parent_embedded = _prior_bindings_from_runtime_binding(binding)
    if (
        sha256(migration_path) != binding.get("fit_migration_file_sha256")
        or migration.get("fit_migration_sha256") != binding.get("fit_migration_sha256")
        or batch["fits"][fit_id]["fit_migration_sha256"] != binding.get("fit_migration_sha256")
        or batch_prior_sidecar != binding_prior_sidecar
        or batch_parent_embedded != binding_parent_embedded
    ):
        raise ValueError("V3 fit migration changed after binding")
    result = {
        "execution_binding_sha256": str(claimed),
        "execution_binding_origin_checkpoint_sha256": str(migration["parent_checkpoint"]["sha256"]),
    }
    if binding_parent_embedded is not None:
        result["execution_binding_origin_prior_sha256"] = binding_parent_embedded
    return result


def _prior_bindings_from_summary(
    summary: Mapping[str, Any],
) -> tuple[str | None, str | None]:
    """Read one exact legacy or current prior-binding summary representation."""
    legacy = "prior_execution_binding_sha256" in summary
    current_sidecar = "prior_runtime_sidecar_binding_sha256" in summary
    current_embedded = "parent_embedded_execution_binding_sha256" in summary
    if legacy and not current_sidecar and not current_embedded:
        value = cast(str | None, summary["prior_execution_binding_sha256"])
        return value, value
    if not legacy and current_sidecar and current_embedded:
        return (
            cast(str | None, summary["prior_runtime_sidecar_binding_sha256"]),
            cast(str | None, summary["parent_embedded_execution_binding_sha256"]),
        )
    raise ValueError("Prior binding summary mixes legacy and current fields")


def _prior_bindings_from_runtime_binding(
    binding: Mapping[str, Any],
) -> tuple[str | None, str | None]:
    """Read one exact legacy or current runtime-binding representation."""
    if "previous_execution_binding_sha256" not in binding:
        raise ValueError("Runtime binding omits its previous execution binding")
    previous = cast(str | None, binding["previous_execution_binding_sha256"])
    if "origin_embedded_execution_binding_sha256" not in binding:
        return previous, previous
    return (
        previous,
        cast(str | None, binding["origin_embedded_execution_binding_sha256"]),
    )