weft-core 0.1.1

OpenAI-compatible AI agent runtime with WASM capability plugin system
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
use crate::app::state::AppBindingResolution;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppGeneration {
    pub id: u64,
    pub app_name: String,
    pub version: String,
    pub bindings: Vec<AppBindingResolution>,
    pub capabilities: Vec<String>,
    #[serde(default)]
    pub enabled_features: Vec<String>,
    #[serde(default)]
    pub scene: String,
    pub profile: String,
    #[serde(default)]
    pub binding_set_id: String,
    #[serde(default)]
    pub closure_id: String,
    #[serde(default)]
    pub lock_digest: String,
    #[serde(default)]
    pub lock_path: String,
    #[serde(default)]
    pub parent_generation: Option<u64>,
    #[serde(default)]
    pub created_by: String,
    pub status: GenerationStatus,
    #[serde(default)]
    pub validation_results: Vec<ValidationResult>,
    pub created_at: u64,
}

impl Default for AppGeneration {
    fn default() -> Self {
        Self {
            id: 0,
            app_name: String::new(),
            version: String::new(),
            bindings: vec![],
            capabilities: vec![],
            enabled_features: vec![],
            scene: String::new(),
            profile: String::new(),
            binding_set_id: String::new(),
            closure_id: String::new(),
            lock_digest: String::new(),
            lock_path: String::new(),
            parent_generation: None,
            created_by: String::new(),
            status: GenerationStatus::Candidate,
            validation_results: vec![],
            created_at: 0,
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct AppGenerationSummaryMetadata {
    pub scene: String,
    pub binding_set_id: String,
    pub closure_id: String,
    pub lock_digest: String,
    pub lock_path: String,
    pub parent_generation: Option<u64>,
    pub created_by: String,
}

#[derive(Debug, Clone, Default)]
pub struct AppGenerationProposal {
    pub app_name: String,
    pub version: String,
    pub bindings: Vec<AppBindingResolution>,
    pub capabilities: Vec<String>,
    pub enabled_features: Vec<String>,
    pub profile: String,
    pub metadata: AppGenerationSummaryMetadata,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum GenerationStatus {
    Candidate,
    Verified,
    Active,
    Rollback,
    Failed,
    Archived,
}

pub const GENERATION_INDEX_SCHEMA_VERSION: u64 = 1;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppGenerationIndex {
    #[serde(default = "default_generation_index_schema_version")]
    pub schema_version: u64,
    #[serde(default)]
    pub active: Option<u64>,
    #[serde(default)]
    pub previous: Option<u64>,
    #[serde(default)]
    pub candidate: Option<u64>,
    pub next_id: u64,
    #[serde(default)]
    pub generations: Vec<AppGeneration>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GenerationIndexDiagnosticLevel {
    Warning,
    RepairNeeded,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GenerationIndexDiagnostic {
    pub level: GenerationIndexDiagnosticLevel,
    pub code: String,
    pub message: String,
    #[serde(default)]
    pub pointer: Option<String>,
    #[serde(default)]
    pub generation_id: Option<u64>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct GenerationIndexConsistencyReport {
    pub is_consistent: bool,
    pub repair_recommended: bool,
    #[serde(default)]
    pub diagnostics: Vec<GenerationIndexDiagnostic>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct StartupGenerationStoreDiagnostics {
    pub active_pointer: Option<u64>,
    pub previous_pointer: Option<u64>,
    pub generation_index_present: bool,
    #[serde(default)]
    pub diagnostics: Vec<GenerationIndexDiagnostic>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ActivationPersistenceStepKind {
    CheckTargetStatus,
    CheckTargetLockMetadata,
    WriteGenerationLock,
    WritePreviousPointer,
    ReplaceActivePointer,
    ReplaceRootLockMirror,
    UpdateGenerationIndex,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActivationPersistenceStep {
    pub kind: ActivationPersistenceStepKind,
    pub description: String,
    #[serde(default)]
    pub path: Option<String>,
    #[serde(default)]
    pub generation_id: Option<u64>,
    #[serde(default)]
    pub best_effort: bool,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActivationPersistencePlan {
    pub target_generation_id: u64,
    pub target_lock_path: String,
    #[serde(default)]
    pub previous_active_generation_id: Option<u64>,
    #[serde(default)]
    pub steps: Vec<ActivationPersistenceStep>,
}

impl Default for AppGenerationIndex {
    fn default() -> Self {
        Self {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: None,
            previous: None,
            candidate: None,
            next_id: 1,
            generations: Vec::new(),
        }
    }
}

impl AppGenerationIndex {
    pub fn generation(&self, id: u64) -> Option<&AppGeneration> {
        self.generations
            .iter()
            .find(|generation| generation.id == id)
    }

    pub fn from_store(store: &AppGenerationStore) -> Self {
        let mut generations: Vec<AppGeneration> = Vec::new();
        let mut push_unique = |generation: &AppGeneration| {
            if generations
                .iter()
                .any(|existing| existing.id == generation.id)
            {
                return;
            }
            generations.push(generation.clone());
        };

        if let Some(active) = &store.active {
            push_unique(active);
        }
        if let Some(previous) = &store.rollback {
            push_unique(previous);
        }
        if let Some(candidate) = &store.candidate {
            push_unique(candidate);
        }

        generations.sort_by_key(|generation| generation.id);

        Self {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: store.active.as_ref().map(|generation| generation.id),
            previous: store.rollback.as_ref().map(|generation| generation.id),
            candidate: store.candidate.as_ref().map(|generation| generation.id),
            next_id: store.next_generation_id(),
            generations,
        }
    }

    pub fn from_active_summary(active: &AppGeneration) -> Self {
        Self {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: Some(active.id),
            previous: None,
            candidate: None,
            next_id: active.id.saturating_add(1).max(1),
            generations: vec![active.clone()],
        }
    }

    pub fn repair_from_sources(
        store: Option<&AppGenerationStore>,
        active_summary: Option<&AppGeneration>,
    ) -> Option<Self> {
        let store_has_data = store.is_some_and(|store| {
            store.active.is_some()
                || store.rollback.is_some()
                || store.candidate.is_some()
                || store.next_id != 0
        });
        if !store_has_data && active_summary.is_none() {
            return None;
        }

        let mut index = store.map(Self::from_store).unwrap_or_default();

        if let Some(active_summary) = active_summary {
            index.upsert_generation(active_summary.clone());
            index.active = Some(active_summary.id);
            index.next_id = index
                .next_id
                .max(active_summary.id.saturating_add(1))
                .max(1);
        }

        if index.next_id == 0 {
            index.next_id = 1;
        }
        index.generations.sort_by_key(|generation| generation.id);
        Some(index)
    }

    pub fn consistency_report(
        &self,
        active_pointer: Option<u64>,
        previous_pointer: Option<u64>,
    ) -> GenerationIndexConsistencyReport {
        let mut report = GenerationIndexConsistencyReport {
            is_consistent: true,
            repair_recommended: false,
            diagnostics: Vec::new(),
        };

        report.compare_pointer("active", active_pointer, self.active);
        report.compare_pointer("previous", previous_pointer, self.previous);

        for (pointer, generation_id) in [
            ("active", self.active),
            ("previous", self.previous),
            ("candidate", self.candidate),
        ] {
            if let Some(generation_id) = generation_id {
                match self.generation(generation_id) {
                    Some(generation) => {
                        if generation.lock_path.trim().is_empty() {
                            report.warn(
                                "missing_lock_path",
                                format!(
                                    "Generation {} referenced by {} is missing lock_path metadata",
                                    generation_id, pointer
                                ),
                                Some(pointer),
                                Some(generation_id),
                            );
                        }

                        let mut missing_fields = Vec::new();
                        if generation.scene.trim().is_empty() {
                            missing_fields.push("scene");
                        }
                        if generation.binding_set_id.trim().is_empty() {
                            missing_fields.push("binding_set_id");
                        }
                        if generation.closure_id.trim().is_empty() {
                            missing_fields.push("closure_id");
                        }
                        if generation.lock_digest.trim().is_empty() {
                            missing_fields.push("lock_digest");
                        }
                        if generation.created_by.trim().is_empty() {
                            missing_fields.push("created_by");
                        }

                        if !missing_fields.is_empty() {
                            report.warn(
                                "incomplete_generation_summary",
                                format!(
                                    "Generation {} referenced by {} is missing summary fields: {}",
                                    generation_id,
                                    pointer,
                                    missing_fields.join(", ")
                                ),
                                Some(pointer),
                                Some(generation_id),
                            );
                        }
                    }
                    None => report.repair_needed(
                        "pointer_target_missing_from_index",
                        format!(
                            "Generation {} referenced by {} is missing from generation index",
                            generation_id, pointer
                        ),
                        Some(pointer),
                        Some(generation_id),
                    ),
                }
            }
        }

        report
    }

    pub fn into_store(self) -> AppGenerationStore {
        let active = self.active.and_then(|id| self.generation(id).cloned());
        let rollback = self.previous.and_then(|id| self.generation(id).cloned());
        let candidate = self.candidate.and_then(|id| self.generation(id).cloned());

        AppGenerationStore {
            active,
            candidate,
            rollback,
            next_id: if self.next_id == 0 { 1 } else { self.next_id },
        }
    }

    fn normalized_for_save(&self) -> Self {
        let mut normalized = self.clone();
        if normalized.schema_version == 0 {
            normalized.schema_version = GENERATION_INDEX_SCHEMA_VERSION;
        }
        if normalized.next_id == 0 {
            normalized.next_id = 1;
        }
        normalized
            .generations
            .sort_by_key(|generation| generation.id);
        normalized
    }

    fn upsert_generation(&mut self, generation: AppGeneration) {
        if let Some(existing) = self
            .generations
            .iter_mut()
            .find(|existing| existing.id == generation.id)
        {
            *existing = generation;
            return;
        }

        self.generations.push(generation);
    }

    fn validate(&self, path: &Path) -> Result<()> {
        let mut seen = HashSet::new();
        for generation in &self.generations {
            if !seen.insert(generation.id) {
                anyhow::bail!(
                    "Duplicate generation id {} in {}",
                    generation.id,
                    path.display()
                );
            }
        }

        for (label, id) in [
            ("active", self.active),
            ("previous", self.previous),
            ("candidate", self.candidate),
        ] {
            if let Some(id) = id {
                if self.generation(id).is_none() {
                    anyhow::bail!(
                        "Generation index {} pointer references missing generation {} in {}",
                        label,
                        id,
                        path.display()
                    );
                }
            }
        }

        Ok(())
    }
}

fn default_generation_index_schema_version() -> u64 {
    GENERATION_INDEX_SCHEMA_VERSION
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationResult {
    pub check: String,
    pub passed: bool,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AppGenerationStore {
    pub active: Option<AppGeneration>,
    pub candidate: Option<AppGeneration>,
    pub rollback: Option<AppGeneration>,
    pub next_id: u64,
}

impl AppGenerationStore {
    pub fn generation(&self, generation_id: u64) -> Option<&AppGeneration> {
        self.active
            .as_ref()
            .filter(|generation| generation.id == generation_id)
            .or_else(|| {
                self.candidate
                    .as_ref()
                    .filter(|generation| generation.id == generation_id)
            })
            .or_else(|| {
                self.rollback
                    .as_ref()
                    .filter(|generation| generation.id == generation_id)
            })
    }

    fn allocate_generation_id(&mut self) -> u64 {
        if self.next_id == 0 {
            self.next_id = 1;
        }
        let id = self.next_id;
        self.next_id += 1;
        id
    }

    pub fn next_generation_id(&self) -> u64 {
        if self.next_id == 0 {
            1
        } else {
            self.next_id
        }
    }

    pub fn propose(&mut self, proposal: AppGenerationProposal) -> &AppGeneration {
        let id = self.allocate_generation_id();
        let AppGenerationProposal {
            app_name,
            version,
            bindings,
            capabilities,
            enabled_features,
            profile,
            metadata,
        } = proposal;

        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        self.candidate = Some(AppGeneration {
            id,
            app_name,
            version,
            bindings,
            capabilities,
            enabled_features,
            scene: metadata.scene,
            profile,
            binding_set_id: metadata.binding_set_id,
            closure_id: metadata.closure_id,
            lock_digest: metadata.lock_digest,
            lock_path: metadata.lock_path,
            parent_generation: metadata.parent_generation,
            created_by: metadata.created_by,
            status: GenerationStatus::Candidate,
            validation_results: vec![],
            created_at: now,
        });

        self.candidate.as_ref().unwrap()
    }

    pub fn verify_candidate(
        &mut self,
        registry: Option<&crate::app::CapabilityRegistry>,
    ) -> Result<&AppGeneration, String> {
        let candidate = self
            .candidate
            .as_mut()
            .ok_or_else(|| "No candidate generation to verify".to_string())?;

        let mut results = Vec::new();

        let has_bindings = !candidate.bindings.is_empty();
        results.push(ValidationResult {
            check: "boot".into(),
            passed: has_bindings,
            message: if has_bindings {
                "Bindings present".into()
            } else {
                "No bindings resolved".into()
            },
        });

        let has_capabilities = !candidate.capabilities.is_empty();
        results.push(ValidationResult {
            check: "capabilities".into(),
            passed: has_capabilities,
            message: if has_capabilities {
                format!("{} capabilities declared", candidate.capabilities.len())
            } else {
                "No capabilities declared".into()
            },
        });

        let bound_caps: std::collections::HashSet<&str> = candidate
            .bindings
            .iter()
            .map(|b| b.capability.as_str())
            .collect();
        let unbound: Vec<&str> = candidate
            .capabilities
            .iter()
            .filter(|c| !bound_caps.contains(c.as_str()))
            .map(|c| c.as_str())
            .collect();
        let all_bound = unbound.is_empty();
        results.push(ValidationResult {
            check: "binding-coverage".into(),
            passed: all_bound,
            message: if all_bound {
                "All capabilities have bindings".into()
            } else {
                format!("Unbound capabilities: {:?}", unbound)
            },
        });

        if let Some(reg) = registry {
            let missing: Vec<&str> = candidate
                .capabilities
                .iter()
                .filter(|c| !reg.contains_key(c.as_str()))
                .map(|c| c.as_str())
                .collect();
            let all_in_registry = missing.is_empty();
            results.push(ValidationResult {
                check: "registry-coverage".into(),
                passed: all_in_registry,
                message: if all_in_registry {
                    "All capabilities found in registry".into()
                } else {
                    format!("Missing from registry: {:?}", missing)
                },
            });
        }

        let all_passed = results.iter().all(|r| r.passed);
        candidate.validation_results = results;
        candidate.status = if all_passed {
            GenerationStatus::Verified
        } else {
            GenerationStatus::Failed
        };

        if all_passed {
            Ok(candidate)
        } else {
            Err("Verification failed".into())
        }
    }

    pub fn activate(&mut self) -> Result<&AppGeneration, String> {
        let candidate = self
            .candidate
            .take()
            .ok_or_else(|| "No candidate generation to activate".to_string())?;

        if candidate.status != GenerationStatus::Verified {
            self.candidate = Some(candidate);
            return Err("Candidate must be verified before activation".into());
        }

        let previous_active = self.active.take();

        let mut activated = candidate;
        activated.status = GenerationStatus::Active;
        self.active = Some(activated.clone());

        if let Some(mut prev_active) = previous_active {
            prev_active.status = GenerationStatus::Rollback;
            self.rollback = Some(prev_active);
        }

        Ok(self.active.as_ref().unwrap())
    }

    pub fn rollback(&mut self) -> Result<&AppGeneration, String> {
        let rollback_gen = self
            .rollback
            .take()
            .ok_or_else(|| "No rollback generation available".to_string())?;

        if let Some(mut current) = self.active.take() {
            current.status = GenerationStatus::Failed;
            self.candidate = Some(current);
        }

        let mut restored = rollback_gen;
        restored.status = GenerationStatus::Active;
        self.active = Some(restored);

        Ok(self.active.as_ref().unwrap())
    }

    pub fn switch_to_existing(&mut self, generation_id: u64) -> Result<&AppGeneration, String> {
        if self
            .active
            .as_ref()
            .is_some_and(|generation| generation.id == generation_id)
        {
            return Ok(self.active.as_ref().unwrap());
        }

        let target_status = self
            .generation(generation_id)
            .map(|generation| generation.status)
            .ok_or_else(|| format!("Generation {} not found", generation_id))?;

        match target_status {
            GenerationStatus::Verified | GenerationStatus::Rollback => {}
            GenerationStatus::Candidate => {
                return Err(format!(
                    "Generation {} must be verified before activation",
                    generation_id
                ));
            }
            GenerationStatus::Failed => {
                return Err(format!(
                    "Generation {} failed verification and cannot be activated",
                    generation_id
                ));
            }
            GenerationStatus::Archived => {
                return Err(format!(
                    "Generation {} is archived and cannot be activated",
                    generation_id
                ));
            }
            GenerationStatus::Active => {
                return Err(format!(
                    "Generation {} is already active but not stored in the active slot",
                    generation_id
                ));
            }
        }

        let previous_active = self.active.take();
        let mut target = if self
            .candidate
            .as_ref()
            .is_some_and(|generation| generation.id == generation_id)
        {
            self.candidate.take().unwrap()
        } else if self
            .rollback
            .as_ref()
            .is_some_and(|generation| generation.id == generation_id)
        {
            self.rollback.take().unwrap()
        } else {
            return Err(format!("Generation {} not found", generation_id));
        };

        target.status = GenerationStatus::Active;
        self.active = Some(target);

        if let Some(mut previous_active) = previous_active {
            previous_active.status = GenerationStatus::Rollback;
            self.rollback = Some(previous_active);
        } else {
            self.rollback = None;
        }

        Ok(self.active.as_ref().unwrap())
    }
}

impl GenerationIndexConsistencyReport {
    fn compare_pointer(&mut self, pointer: &str, actual: Option<u64>, indexed: Option<u64>) {
        if actual != indexed {
            self.repair_needed(
                "pointer_mismatch",
                format!(
                    "{} pointer mismatch: authoritative pointer is {:?}, generation index records {:?}",
                    pointer, actual, indexed
                ),
                Some(pointer),
                actual.or(indexed),
            );
        }
    }

    fn warn(
        &mut self,
        code: &str,
        message: String,
        pointer: Option<&str>,
        generation_id: Option<u64>,
    ) {
        self.diagnostics.push(GenerationIndexDiagnostic {
            level: GenerationIndexDiagnosticLevel::Warning,
            code: code.into(),
            message,
            pointer: pointer.map(str::to_owned),
            generation_id,
        });
    }

    fn repair_needed(
        &mut self,
        code: &str,
        message: String,
        pointer: Option<&str>,
        generation_id: Option<u64>,
    ) {
        self.is_consistent = false;
        self.repair_recommended = true;
        self.diagnostics.push(GenerationIndexDiagnostic {
            level: GenerationIndexDiagnosticLevel::RepairNeeded,
            code: code.into(),
            message,
            pointer: pointer.map(str::to_owned),
            generation_id,
        });
    }
}

impl StartupGenerationStoreDiagnostics {
    pub fn is_clean(&self) -> bool {
        self.diagnostics.is_empty()
    }
}

pub type GenerationStoreMap = HashMap<String, AppGenerationStore>;

pub fn generation_index_path(instance_dir: &Path) -> PathBuf {
    instance_dir.join("generation-store.toml")
}

pub fn active_generation_pointer_path(instance_dir: &Path) -> PathBuf {
    instance_dir.join("active")
}

pub fn previous_generation_pointer_path(instance_dir: &Path) -> PathBuf {
    instance_dir.join("previous")
}

pub fn inspect_startup_generation_store(
    instance_dir: &Path,
    store: &AppGenerationStore,
) -> StartupGenerationStoreDiagnostics {
    let expected_active = store.active.as_ref().map(|generation| generation.id);
    let expected_previous = store.rollback.as_ref().map(|generation| generation.id);
    let mut diagnostics = StartupGenerationStoreDiagnostics::default();

    let active_pointer = match read_active_generation_pointer(instance_dir) {
        Ok(pointer) => pointer,
        Err(error) => {
            diagnostics.diagnostics.push(GenerationIndexDiagnostic {
                level: GenerationIndexDiagnosticLevel::Warning,
                code: "startup_active_pointer_unreadable".into(),
                message: format!(
                    "Startup ignored unreadable active pointer at '{}': {error:#}",
                    active_generation_pointer_path(instance_dir).display()
                ),
                pointer: Some("active".into()),
                generation_id: None,
            });
            None
        }
    };
    diagnostics.active_pointer = active_pointer;

    let previous_pointer = match read_previous_generation_pointer(instance_dir) {
        Ok(pointer) => pointer,
        Err(error) => {
            diagnostics.diagnostics.push(GenerationIndexDiagnostic {
                level: GenerationIndexDiagnosticLevel::Warning,
                code: "startup_previous_pointer_unreadable".into(),
                message: format!(
                    "Startup ignored unreadable previous pointer at '{}': {error:#}",
                    previous_generation_pointer_path(instance_dir).display()
                ),
                pointer: Some("previous".into()),
                generation_id: None,
            });
            None
        }
    };
    diagnostics.previous_pointer = previous_pointer;

    if let Some(active_pointer) = active_pointer {
        if Some(active_pointer) != expected_active {
            diagnostics.diagnostics.push(GenerationIndexDiagnostic {
                level: GenerationIndexDiagnosticLevel::Warning,
                code: "startup_active_pointer_mismatch".into(),
                message: format!(
                    "Startup retained lock-derived active generation {:?} while active pointer recorded {:?}",
                    expected_active, active_pointer
                ),
                pointer: Some("active".into()),
                generation_id: Some(active_pointer),
            });
        }
    }

    if let Some(previous_pointer) = previous_pointer {
        if Some(previous_pointer) != expected_previous {
            diagnostics.diagnostics.push(GenerationIndexDiagnostic {
                level: GenerationIndexDiagnosticLevel::Warning,
                code: "startup_previous_pointer_mismatch".into(),
                message: format!(
                    "Startup retained lock-derived previous generation {:?} while previous pointer recorded {:?}",
                    expected_previous, previous_pointer
                ),
                pointer: Some("previous".into()),
                generation_id: Some(previous_pointer),
            });
        }
    }

    match load_generation_index(instance_dir) {
        Ok(Some(index)) => {
            diagnostics.generation_index_present = true;

            if index.active != expected_active {
                diagnostics.diagnostics.push(GenerationIndexDiagnostic {
                    level: GenerationIndexDiagnosticLevel::Warning,
                    code: "startup_generation_index_active_mismatch".into(),
                    message: format!(
                        "Startup retained lock-derived active generation {:?} while generation-store.toml recorded {:?}",
                        expected_active, index.active
                    ),
                    pointer: Some("active".into()),
                    generation_id: index.active.or(expected_active),
                });
            }

            if index.previous != expected_previous {
                diagnostics.diagnostics.push(GenerationIndexDiagnostic {
                    level: GenerationIndexDiagnosticLevel::Warning,
                    code: "startup_generation_index_previous_mismatch".into(),
                    message: format!(
                        "Startup retained lock-derived previous generation {:?} while generation-store.toml recorded {:?}",
                        expected_previous, index.previous
                    ),
                    pointer: Some("previous".into()),
                    generation_id: index.previous.or(expected_previous),
                });
            }

            if let (Some(expected_active), Some(indexed_active)) = (
                store.active.as_ref(),
                index.active.and_then(|id| index.generation(id)),
            ) {
                let mut mismatched_fields = Vec::new();
                if indexed_active.lock_path != expected_active.lock_path {
                    mismatched_fields.push("lock_path");
                }
                if indexed_active.scene != expected_active.scene {
                    mismatched_fields.push("scene");
                }
                if indexed_active.profile != expected_active.profile {
                    mismatched_fields.push("profile");
                }
                if indexed_active.binding_set_id != expected_active.binding_set_id {
                    mismatched_fields.push("binding_set_id");
                }
                if indexed_active.closure_id != expected_active.closure_id {
                    mismatched_fields.push("closure_id");
                }

                if !mismatched_fields.is_empty() {
                    diagnostics.diagnostics.push(GenerationIndexDiagnostic {
                        level: GenerationIndexDiagnosticLevel::Warning,
                        code: "startup_generation_index_summary_mismatch".into(),
                        message: format!(
                            "Startup retained lock-derived active generation {} while generation-store.toml differed in: {}",
                            expected_active.id,
                            mismatched_fields.join(", ")
                        ),
                        pointer: Some("active".into()),
                        generation_id: Some(expected_active.id),
                    });
                }
            }
        }
        Ok(None) => {}
        Err(error) => {
            diagnostics.diagnostics.push(GenerationIndexDiagnostic {
                level: GenerationIndexDiagnosticLevel::Warning,
                code: "startup_generation_index_unreadable".into(),
                message: format!(
                    "Startup ignored unreadable generation-store.toml at '{}': {error:#}",
                    generation_index_path(instance_dir).display()
                ),
                pointer: None,
                generation_id: None,
            });
        }
    }

    diagnostics
}

pub fn plan_activation_persistence(
    instance_dir: &Path,
    target: &AppGeneration,
    store: Option<&AppGenerationStore>,
    index: Option<&AppGenerationIndex>,
) -> Result<ActivationPersistencePlan, String> {
    if !matches!(
        target.status,
        GenerationStatus::Verified | GenerationStatus::Active
    ) {
        return Err(format!(
            "Generation {} must be verified or active before activation persistence can be planned",
            target.id
        ));
    }

    if target.lock_path.trim().is_empty() {
        return Err(format!(
            "Generation {} is missing lock_path metadata required for activation persistence planning",
            target.id
        ));
    }

    if let Some(index) = index {
        if index.generation(target.id).is_none() {
            return Err(format!(
                "Cannot plan activation persistence: target generation {} is missing from generation index",
                target.id
            ));
        }
    }

    let store_active_id =
        store.and_then(|store| store.active.as_ref().map(|generation| generation.id));
    let index_active_id = index.and_then(|index| index.active);
    let current_active_id =
        reconcile_planned_generation_id("active", store_active_id, index_active_id)?;

    let store_previous_id =
        store.and_then(|store| store.rollback.as_ref().map(|generation| generation.id));
    let index_previous_id = index.and_then(|index| index.previous);
    let current_previous_id =
        reconcile_planned_generation_id("previous", store_previous_id, index_previous_id)?;

    let previous_active_generation_id = match target.status {
        GenerationStatus::Active => {
            if let Some(current_active_id) = current_active_id {
                if current_active_id != target.id {
                    return Err(format!(
                        "Generation {} is marked active but store/index currently point to generation {}",
                        target.id, current_active_id
                    ));
                }
            }
            current_previous_id
        }
        GenerationStatus::Verified => current_active_id,
        _ => unreachable!(),
    };

    let target_lock_path = planned_generation_lock_path(instance_dir, &target.lock_path)
        .display()
        .to_string();
    let previous_pointer_path = previous_generation_pointer_path(instance_dir)
        .display()
        .to_string();
    let active_pointer_path = active_generation_pointer_path(instance_dir)
        .display()
        .to_string();
    let root_lock_mirror_path = instance_dir.join("lock.toml").display().to_string();
    let generation_index_path = generation_index_path(instance_dir).display().to_string();

    Ok(ActivationPersistencePlan {
        target_generation_id: target.id,
        target_lock_path: target_lock_path.clone(),
        previous_active_generation_id,
        steps: vec![
            ActivationPersistenceStep {
                kind: ActivationPersistenceStepKind::CheckTargetStatus,
                description: format!(
                    "Confirm generation {} remains verified or active before planning activation persistence",
                    target.id
                ),
                path: None,
                generation_id: Some(target.id),
                best_effort: false,
            },
            ActivationPersistenceStep {
                kind: ActivationPersistenceStepKind::CheckTargetLockMetadata,
                description: format!(
                    "Confirm generation {} lock metadata resolves to {}",
                    target.id, target_lock_path
                ),
                path: Some(target_lock_path.clone()),
                generation_id: Some(target.id),
                best_effort: false,
            },
            ActivationPersistenceStep {
                kind: ActivationPersistenceStepKind::WriteGenerationLock,
                description: format!(
                    "Write immutable generation lock for generation {} before pointer changes",
                    target.id
                ),
                path: Some(target_lock_path),
                generation_id: Some(target.id),
                best_effort: false,
            },
            ActivationPersistenceStep {
                kind: ActivationPersistenceStepKind::WritePreviousPointer,
                description: match previous_active_generation_id {
                    Some(previous_active_generation_id) => format!(
                        "Write previous pointer to generation {} before replacing active pointer",
                        previous_active_generation_id
                    ),
                    None => "Clear previous pointer before replacing active pointer".into(),
                },
                path: Some(previous_pointer_path),
                generation_id: previous_active_generation_id,
                best_effort: false,
            },
            ActivationPersistenceStep {
                kind: ActivationPersistenceStepKind::ReplaceActivePointer,
                description: format!(
                    "Atomically replace active pointer with generation {}",
                    target.id
                ),
                path: Some(active_pointer_path),
                generation_id: Some(target.id),
                best_effort: false,
            },
            ActivationPersistenceStep {
                kind: ActivationPersistenceStepKind::ReplaceRootLockMirror,
                description: format!(
                    "Atomically replace root lock mirror from generation {} after active pointer update",
                    target.id
                ),
                path: Some(root_lock_mirror_path),
                generation_id: Some(target.id),
                best_effort: false,
            },
            ActivationPersistenceStep {
                kind: ActivationPersistenceStepKind::UpdateGenerationIndex,
                description: "Update generation index as repairable best-effort metadata after pointer changes".into(),
                path: Some(generation_index_path),
                generation_id: Some(target.id),
                best_effort: true,
            },
        ],
    })
}

fn reconcile_planned_generation_id(
    label: &str,
    store_id: Option<u64>,
    index_id: Option<u64>,
) -> Result<Option<u64>, String> {
    match (store_id, index_id) {
        (Some(store_id), Some(index_id)) if store_id != index_id => Err(format!(
            "Cannot plan activation persistence: {} generation differs between store ({}) and index ({})",
            label, store_id, index_id
        )),
        (Some(store_id), _) => Ok(Some(store_id)),
        (_, Some(index_id)) => Ok(Some(index_id)),
        (None, None) => Ok(None),
    }
}

fn planned_generation_lock_path(instance_dir: &Path, lock_path: &str) -> PathBuf {
    let lock_path = Path::new(lock_path);
    if lock_path.is_absolute() {
        lock_path.to_path_buf()
    } else {
        instance_dir.join(lock_path)
    }
}

pub fn load_generation_index(instance_dir: &Path) -> Result<Option<AppGenerationIndex>> {
    load_generation_index_from_path(&generation_index_path(instance_dir))
}

pub fn load_generation_index_from_path(path: &Path) -> Result<Option<AppGenerationIndex>> {
    let content = match fs::read_to_string(path) {
        Ok(content) => content,
        Err(error) if error.kind() == ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(error).with_context(|| format!("Failed to read {}", path.display()));
        }
    };

    let mut index: AppGenerationIndex =
        toml::from_str(&content).with_context(|| format!("Failed to parse {}", path.display()))?;
    if index.schema_version == 0 {
        index.schema_version = GENERATION_INDEX_SCHEMA_VERSION;
    } else if index.schema_version > GENERATION_INDEX_SCHEMA_VERSION {
        anyhow::bail!(
            "Unsupported generation index schema version {} in {}",
            index.schema_version,
            path.display()
        );
    }
    if index.next_id == 0 {
        index.next_id = 1;
    }
    index.validate(path)?;

    Ok(Some(index))
}

pub fn save_generation_index(instance_dir: &Path, index: &AppGenerationIndex) -> Result<()> {
    save_generation_index_to_path(&generation_index_path(instance_dir), index)
}

pub fn save_generation_index_to_path(path: &Path, index: &AppGenerationIndex) -> Result<()> {
    let serializable = index.normalized_for_save();
    serializable.validate(path)?;

    let content = toml::to_string_pretty(&serializable)
        .with_context(|| "Failed to serialize generation index")?;
    write_string_safely(path, &content)
}

pub fn read_generation_pointer(path: &Path) -> Result<Option<u64>> {
    let content = match fs::read_to_string(path) {
        Ok(content) => content,
        Err(error) if error.kind() == ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(error).with_context(|| format!("Failed to read {}", path.display()));
        }
    };

    let trimmed = content.trim();
    if trimmed.is_empty() {
        return Ok(None);
    }

    let generation_id = trimmed
        .parse::<u64>()
        .with_context(|| format!("Failed to parse generation pointer {}", path.display()))?;
    Ok(Some(generation_id))
}

pub fn read_active_generation_pointer(instance_dir: &Path) -> Result<Option<u64>> {
    read_generation_pointer(&active_generation_pointer_path(instance_dir))
}

pub fn read_previous_generation_pointer(instance_dir: &Path) -> Result<Option<u64>> {
    read_generation_pointer(&previous_generation_pointer_path(instance_dir))
}

pub fn write_generation_pointer(path: &Path, generation_id: Option<u64>) -> Result<()> {
    match generation_id {
        Some(generation_id) => write_string_safely(path, &format!("{}\n", generation_id)),
        None => {
            if let Err(error) = fs::remove_file(path) {
                if error.kind() != ErrorKind::NotFound {
                    return Err(error)
                        .with_context(|| format!("Failed to remove {}", path.display()));
                }
            }
            Ok(())
        }
    }
}

pub fn write_active_generation_pointer(
    instance_dir: &Path,
    generation_id: Option<u64>,
) -> Result<()> {
    write_generation_pointer(&active_generation_pointer_path(instance_dir), generation_id)
}

pub fn write_previous_generation_pointer(
    instance_dir: &Path,
    generation_id: Option<u64>,
) -> Result<()> {
    write_generation_pointer(
        &previous_generation_pointer_path(instance_dir),
        generation_id,
    )
}

fn write_string_safely(path: &Path, content: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("Failed to create {}", parent.display()))?;
    }

    let temp_path = temp_write_path(path);
    fs::write(&temp_path, content)
        .with_context(|| format!("Failed to write {}", temp_path.display()))?;

    if path.exists() {
        let backup_path = backup_write_path(path);
        if backup_path.exists() {
            fs::remove_file(&backup_path)
                .with_context(|| format!("Failed to clear {}", backup_path.display()))?;
        }

        fs::rename(path, &backup_path)
            .with_context(|| format!("Failed to stage existing {}", path.display()))?;

        match fs::rename(&temp_path, path) {
            Ok(()) => {
                fs::remove_file(&backup_path).with_context(|| {
                    format!("Failed to remove backup {}", backup_path.display())
                })?;
                Ok(())
            }
            Err(error) => {
                let restore_result = fs::rename(&backup_path, path);
                if restore_result.is_err() {
                    let _ = fs::rename(&temp_path, path);
                }
                Err(error).with_context(|| format!("Failed to move {} into place", path.display()))
            }
        }
    } else {
        fs::rename(&temp_path, path)
            .with_context(|| format!("Failed to move {} into place", path.display()))
    }?;

    Ok(())
}

fn temp_write_path(path: &Path) -> PathBuf {
    let file_name = path
        .file_name()
        .and_then(|value| value.to_str())
        .unwrap_or("generation.tmp");
    let unique = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    path.with_file_name(format!("{}.{}.tmp", file_name, unique))
}

fn backup_write_path(path: &Path) -> PathBuf {
    let file_name = path
        .file_name()
        .and_then(|value| value.to_str())
        .unwrap_or("generation.bak");
    path.with_file_name(format!("{}.bak", file_name))
}

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

    #[test]
    fn app_generation_defaults_summary_metadata_safely() {
        let generation = AppGeneration::default();

        assert_eq!(generation.scene, "");
        assert_eq!(generation.binding_set_id, "");
        assert_eq!(generation.closure_id, "");
        assert_eq!(generation.lock_digest, "");
        assert_eq!(generation.lock_path, "");
        assert_eq!(generation.parent_generation, None);
        assert_eq!(generation.created_by, "");
        assert_eq!(generation.status, GenerationStatus::Candidate);
    }

    #[test]
    fn app_generation_deserializes_old_payload_with_defaulted_metadata() {
        let payload = serde_json::json!({
            "id": 7,
            "app_name": "weft-claw",
            "version": "0.1.0",
            "bindings": [],
            "capabilities": ["core.execution"],
            "enabled_features": [],
            "profile": "developer",
            "status": "candidate",
            "validation_results": [],
            "created_at": 123
        });

        let generation: AppGeneration =
            serde_json::from_value(payload).expect("legacy generation payload should deserialize");

        assert_eq!(generation.scene, "");
        assert_eq!(generation.binding_set_id, "");
        assert_eq!(generation.closure_id, "");
        assert_eq!(generation.lock_digest, "");
        assert_eq!(generation.lock_path, "");
        assert_eq!(generation.parent_generation, None);
        assert_eq!(generation.created_by, "");
    }

    #[test]
    fn propose_initializes_summary_metadata_from_supplied_values() {
        let mut store = AppGenerationStore::default();
        let generation = store.propose(AppGenerationProposal {
            app_name: "weft-claw".into(),
            version: "0.1.0".into(),
            bindings: vec![],
            capabilities: vec!["core.execution".into()],
            enabled_features: vec![],
            profile: "developer".into(),
            metadata: AppGenerationSummaryMetadata {
                scene: "team".into(),
                binding_set_id: "binding-set:sha256:test".into(),
                closure_id: "closure:sha256:test".into(),
                lock_digest: "sha256:lock".into(),
                lock_path: "generations/1.lock.toml".into(),
                parent_generation: Some(3),
                created_by: "api".into(),
            },
        });

        assert_eq!(generation.scene, "team");
        assert_eq!(generation.binding_set_id, "binding-set:sha256:test");
        assert_eq!(generation.closure_id, "closure:sha256:test");
        assert_eq!(generation.lock_digest, "sha256:lock");
        assert_eq!(generation.lock_path, "generations/1.lock.toml");
        assert_eq!(generation.parent_generation, Some(3));
        assert_eq!(generation.created_by, "api");
    }

    fn sample_generation(id: u64, status: GenerationStatus) -> AppGeneration {
        AppGeneration {
            id,
            app_name: "weft-claw".into(),
            version: "0.1.0".into(),
            bindings: vec![],
            capabilities: vec!["core.execution".into()],
            enabled_features: vec![],
            scene: "team".into(),
            profile: "developer".into(),
            binding_set_id: format!("binding-set:sha256:{}", id),
            closure_id: format!("closure:sha256:{}", id),
            lock_digest: format!("sha256:lock-{}", id),
            lock_path: format!("generations/{}.lock.toml", id),
            parent_generation: id.checked_sub(1),
            created_by: "cli".into(),
            status,
            validation_results: vec![],
            created_at: 1710000000 + id,
        }
    }

    #[test]
    fn generation_index_round_trips_with_generation_summaries() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");

        let index = AppGenerationIndex {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: Some(20),
            previous: Some(19),
            candidate: Some(21),
            next_id: 22,
            generations: vec![
                sample_generation(19, GenerationStatus::Verified),
                sample_generation(20, GenerationStatus::Active),
                sample_generation(21, GenerationStatus::Candidate),
            ],
        };

        save_generation_index(&instance_dir, &index).expect("index saved");
        let loaded = load_generation_index(&instance_dir)
            .expect("index loaded")
            .expect("index present");

        assert_eq!(loaded.schema_version, GENERATION_INDEX_SCHEMA_VERSION);
        assert_eq!(loaded.active, Some(20));
        assert_eq!(loaded.previous, Some(19));
        assert_eq!(loaded.candidate, Some(21));
        assert_eq!(loaded.next_id, 22);
        assert_eq!(loaded.generations.len(), 3);

        let active = loaded.generation(20).expect("active generation stored");
        assert_eq!(active.scene, "team");
        assert_eq!(active.profile, "developer");
        assert_eq!(active.binding_set_id, "binding-set:sha256:20");
        assert_eq!(active.closure_id, "closure:sha256:20");
        assert_eq!(active.lock_digest, "sha256:lock-20");
        assert_eq!(active.lock_path, "generations/20.lock.toml");
        assert_eq!(active.parent_generation, Some(19));
        assert_eq!(active.created_by, "cli");
    }

    #[test]
    fn missing_generation_index_returns_none() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");

        let loaded = load_generation_index(&instance_dir).expect("missing index handled");

        assert!(loaded.is_none());
    }

    #[test]
    fn generation_pointer_read_write_round_trip() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");

        write_active_generation_pointer(&instance_dir, Some(20)).expect("active pointer written");
        write_previous_generation_pointer(&instance_dir, Some(19))
            .expect("previous pointer written");

        assert_eq!(
            read_active_generation_pointer(&instance_dir).expect("active pointer read"),
            Some(20)
        );
        assert_eq!(
            read_previous_generation_pointer(&instance_dir).expect("previous pointer read"),
            Some(19)
        );

        write_previous_generation_pointer(&instance_dir, None).expect("previous pointer cleared");

        assert_eq!(
            read_previous_generation_pointer(&instance_dir).expect("cleared pointer read"),
            None
        );
        assert!(!previous_generation_pointer_path(&instance_dir).exists());
    }

    #[test]
    fn generation_index_store_conversion_preserves_summary_fields() {
        let active = sample_generation(30, GenerationStatus::Active);
        let previous = sample_generation(29, GenerationStatus::Verified);
        let candidate = sample_generation(31, GenerationStatus::Candidate);
        let store = AppGenerationStore {
            active: Some(active.clone()),
            candidate: Some(candidate.clone()),
            rollback: Some(previous.clone()),
            next_id: 32,
        };

        let index = AppGenerationIndex::from_store(&store);

        assert_eq!(index.active, Some(30));
        assert_eq!(index.previous, Some(29));
        assert_eq!(index.candidate, Some(31));
        assert_eq!(index.next_id, 32);
        assert_eq!(
            index.generation(30).expect("active summary").lock_path,
            active.lock_path
        );
        assert_eq!(
            index.generation(29).expect("previous summary").scene,
            previous.scene
        );
        assert_eq!(
            index
                .generation(31)
                .expect("candidate summary")
                .binding_set_id,
            candidate.binding_set_id
        );

        let restored_store = index.into_store();

        assert_eq!(restored_store.next_id, 32);
        assert_eq!(
            restored_store.active.expect("active restored").closure_id,
            active.closure_id
        );
        assert_eq!(
            restored_store
                .rollback
                .expect("previous restored")
                .created_by,
            previous.created_by
        );
        assert_eq!(
            restored_store
                .candidate
                .expect("candidate restored")
                .lock_digest,
            candidate.lock_digest
        );
    }

    #[test]
    fn generation_index_repair_rebuilds_missing_index_from_store() {
        let active = sample_generation(30, GenerationStatus::Active);
        let previous = sample_generation(29, GenerationStatus::Verified);
        let candidate = sample_generation(31, GenerationStatus::Candidate);
        let store = AppGenerationStore {
            active: Some(active.clone()),
            candidate: Some(candidate.clone()),
            rollback: Some(previous.clone()),
            next_id: 32,
        };

        let repaired = AppGenerationIndex::repair_from_sources(Some(&store), None)
            .expect("repair should rebuild index from store");

        assert_eq!(repaired.active, Some(30));
        assert_eq!(repaired.previous, Some(29));
        assert_eq!(repaired.candidate, Some(31));
        assert_eq!(repaired.next_id, 32);
        assert_eq!(repaired.generations.len(), 3);
        assert_eq!(
            repaired.generation(30).expect("active exists").scene,
            active.scene
        );
        assert_eq!(
            repaired
                .generation(31)
                .expect("candidate exists")
                .binding_set_id,
            candidate.binding_set_id
        );
    }

    #[test]
    fn generation_index_repair_uses_active_summary_when_store_missing() {
        let active = sample_generation(40, GenerationStatus::Active);

        let repaired = AppGenerationIndex::repair_from_sources(None, Some(&active))
            .expect("repair should build index from active summary");

        assert_eq!(repaired.active, Some(40));
        assert_eq!(repaired.previous, None);
        assert_eq!(repaired.candidate, None);
        assert_eq!(repaired.next_id, 41);
        assert_eq!(repaired.generations.len(), 1);
        assert_eq!(
            repaired.generation(40).expect("active exists").lock_path,
            active.lock_path
        );
    }

    #[test]
    fn generation_index_consistency_report_flags_pointer_mismatch() {
        let index = AppGenerationIndex {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: Some(20),
            previous: Some(19),
            candidate: None,
            next_id: 21,
            generations: vec![
                sample_generation(19, GenerationStatus::Verified),
                sample_generation(20, GenerationStatus::Active),
            ],
        };

        let report = index.consistency_report(Some(21), Some(19));

        assert!(!report.is_consistent);
        assert!(report.repair_recommended);
        assert_eq!(report.diagnostics.len(), 1);
        assert_eq!(
            report.diagnostics[0].level,
            GenerationIndexDiagnosticLevel::RepairNeeded
        );
        assert_eq!(report.diagnostics[0].code, "pointer_mismatch");
        assert_eq!(report.diagnostics[0].pointer.as_deref(), Some("active"));
        assert_eq!(report.diagnostics[0].generation_id, Some(21));
    }

    #[test]
    fn generation_index_consistency_report_is_clean_for_valid_index() {
        let index = AppGenerationIndex {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: Some(20),
            previous: Some(19),
            candidate: Some(21),
            next_id: 22,
            generations: vec![
                sample_generation(19, GenerationStatus::Verified),
                sample_generation(20, GenerationStatus::Active),
                sample_generation(21, GenerationStatus::Candidate),
            ],
        };

        let report = index.consistency_report(Some(20), Some(19));

        assert!(report.is_consistent);
        assert!(!report.repair_recommended);
        assert!(report.diagnostics.is_empty());
    }

    #[test]
    fn generation_index_consistency_report_warns_for_missing_lock_path_and_summary_fields() {
        let mut active = sample_generation(20, GenerationStatus::Active);
        active.lock_path.clear();
        active.scene.clear();
        active.binding_set_id.clear();
        active.closure_id.clear();
        active.lock_digest.clear();
        active.created_by.clear();

        let index = AppGenerationIndex {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: Some(20),
            previous: None,
            candidate: None,
            next_id: 21,
            generations: vec![active],
        };

        let report = index.consistency_report(Some(20), None);

        assert!(report.is_consistent);
        assert!(!report.repair_recommended);
        assert_eq!(report.diagnostics.len(), 2);
        assert_eq!(
            report.diagnostics[0].level,
            GenerationIndexDiagnosticLevel::Warning
        );
        assert_eq!(report.diagnostics[0].code, "missing_lock_path");
        assert_eq!(
            report.diagnostics[1].level,
            GenerationIndexDiagnosticLevel::Warning
        );
        assert_eq!(report.diagnostics[1].code, "incomplete_generation_summary");
    }

    #[test]
    fn startup_generation_store_diagnostics_ignore_missing_pointer_and_index_files() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        fs::create_dir_all(&instance_dir).expect("instance dir created");

        let active = sample_generation(20, GenerationStatus::Active);
        let store = AppGenerationStore {
            active: Some(active),
            candidate: None,
            rollback: None,
            next_id: 21,
        };

        let diagnostics = inspect_startup_generation_store(&instance_dir, &store);

        assert!(diagnostics.is_clean());
        assert_eq!(diagnostics.active_pointer, None);
        assert_eq!(diagnostics.previous_pointer, None);
        assert!(!diagnostics.generation_index_present);
        assert!(diagnostics.diagnostics.is_empty());
    }

    #[test]
    fn startup_generation_store_diagnostics_warn_on_pointer_and_index_mismatch() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        fs::create_dir_all(&instance_dir).expect("instance dir created");

        write_active_generation_pointer(&instance_dir, Some(21)).expect("active pointer written");
        write_previous_generation_pointer(&instance_dir, Some(18))
            .expect("previous pointer written");
        save_generation_index(
            &instance_dir,
            &AppGenerationIndex {
                schema_version: GENERATION_INDEX_SCHEMA_VERSION,
                active: Some(21),
                previous: Some(18),
                candidate: None,
                next_id: 22,
                generations: vec![
                    sample_generation(18, GenerationStatus::Verified),
                    sample_generation(21, GenerationStatus::Active),
                ],
            },
        )
        .expect("index saved");

        let store = AppGenerationStore {
            active: Some(sample_generation(20, GenerationStatus::Active)),
            candidate: None,
            rollback: Some(sample_generation(19, GenerationStatus::Verified)),
            next_id: 21,
        };

        let diagnostics = inspect_startup_generation_store(&instance_dir, &store);

        assert!(diagnostics.generation_index_present);
        assert_eq!(diagnostics.active_pointer, Some(21));
        assert_eq!(diagnostics.previous_pointer, Some(18));
        assert!(diagnostics
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.code == "startup_active_pointer_mismatch"));
        assert!(diagnostics
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.code == "startup_previous_pointer_mismatch"));
        assert!(diagnostics
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.code == "startup_generation_index_active_mismatch"));
        assert!(diagnostics
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.code == "startup_generation_index_previous_mismatch"));
    }

    #[test]
    fn startup_generation_store_diagnostics_are_clean_for_matching_pointer_and_index() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        fs::create_dir_all(&instance_dir).expect("instance dir created");

        let active = sample_generation(20, GenerationStatus::Active);
        let previous = sample_generation(19, GenerationStatus::Verified);
        let store = AppGenerationStore {
            active: Some(active.clone()),
            candidate: None,
            rollback: Some(previous.clone()),
            next_id: 21,
        };

        write_active_generation_pointer(&instance_dir, Some(20)).expect("active pointer written");
        write_previous_generation_pointer(&instance_dir, Some(19))
            .expect("previous pointer written");
        save_generation_index(&instance_dir, &AppGenerationIndex::from_store(&store))
            .expect("index saved");

        let diagnostics = inspect_startup_generation_store(&instance_dir, &store);

        assert!(diagnostics.is_clean());
        assert!(diagnostics.generation_index_present);
        assert_eq!(diagnostics.active_pointer, Some(20));
        assert_eq!(diagnostics.previous_pointer, Some(19));
        assert!(diagnostics.diagnostics.is_empty());
    }

    #[test]
    fn generation_index_rejects_unknown_future_schema_version() {
        let root = tempdir().expect("temp dir");
        let index_path = generation_index_path(&root.path().join(".weft").join("weft-claw"));

        if let Some(parent) = index_path.parent() {
            fs::create_dir_all(parent).expect("instance dir created");
        }
        fs::write(
            &index_path,
            "schema_version = 99\nnext_id = 2\n[[generations]]\nid = 1\napp_name = 'weft-claw'\nversion = '0.1.0'\nbindings = []\ncapabilities = []\nprofile = 'developer'\nstatus = 'verified'\nlock_path = 'generations/1.lock.toml'\ncreated_at = 1710000001\n",
        )
        .expect("index fixture written");

        let error =
            load_generation_index_from_path(&index_path).expect_err("future schema should fail");

        assert!(format!("{error:#}").contains("Unsupported generation index schema version 99"));
    }

    #[test]
    fn app_generation_index_default_is_persistable() {
        let index = AppGenerationIndex::default();

        assert_eq!(index.schema_version, GENERATION_INDEX_SCHEMA_VERSION);
        assert_eq!(index.next_id, 1);
        assert!(index.generations.is_empty());
    }

    #[test]
    fn generation_index_rejects_pointer_to_missing_generation() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        let index_path = generation_index_path(&instance_dir);

        if let Some(parent) = index_path.parent() {
            fs::create_dir_all(parent).expect("instance dir created");
        }
        fs::write(&index_path, "schema_version = 1\nactive = 3\nnext_id = 4\n")
            .expect("index fixture written");

        let error = load_generation_index_from_path(&index_path)
            .expect_err("missing active generation should fail");

        assert!(format!("{error:#}").contains("active pointer references missing generation 3"));
    }

    #[test]
    fn generation_index_rejects_duplicate_generation_ids() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        let index = AppGenerationIndex {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: Some(1),
            previous: None,
            candidate: None,
            next_id: 2,
            generations: vec![
                sample_generation(1, GenerationStatus::Active),
                sample_generation(1, GenerationStatus::Verified),
            ],
        };

        let error = save_generation_index(&instance_dir, &index)
            .expect_err("duplicate generation ids should fail");

        assert!(format!("{error:#}").contains("Duplicate generation id 1"));
    }

    #[test]
    fn activation_persistence_plan_orders_checks_and_writes() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        let target = sample_generation(21, GenerationStatus::Verified);
        let store = AppGenerationStore {
            active: Some(sample_generation(20, GenerationStatus::Active)),
            candidate: Some(target.clone()),
            rollback: Some(sample_generation(19, GenerationStatus::Rollback)),
            next_id: 22,
        };
        let index = AppGenerationIndex::from_store(&store);

        let plan = plan_activation_persistence(&instance_dir, &target, Some(&store), Some(&index))
            .expect("plan should succeed");

        assert_eq!(plan.target_generation_id, 21);
        assert_eq!(plan.previous_active_generation_id, Some(20));
        assert_eq!(
            plan.target_lock_path,
            instance_dir
                .join("generations/21.lock.toml")
                .display()
                .to_string()
        );
        assert_eq!(
            plan.steps.iter().map(|step| step.kind).collect::<Vec<_>>(),
            vec![
                ActivationPersistenceStepKind::CheckTargetStatus,
                ActivationPersistenceStepKind::CheckTargetLockMetadata,
                ActivationPersistenceStepKind::WriteGenerationLock,
                ActivationPersistenceStepKind::WritePreviousPointer,
                ActivationPersistenceStepKind::ReplaceActivePointer,
                ActivationPersistenceStepKind::ReplaceRootLockMirror,
                ActivationPersistenceStepKind::UpdateGenerationIndex,
            ]
        );
        assert_eq!(
            plan.steps[2].path.as_deref(),
            Some(plan.target_lock_path.as_str())
        );
        assert_eq!(plan.steps[3].generation_id, Some(20));
        assert_eq!(
            plan.steps[3].path.as_deref(),
            Some(
                previous_generation_pointer_path(&instance_dir)
                    .display()
                    .to_string()
                    .as_str()
            )
        );
        assert_eq!(
            plan.steps[4].path.as_deref(),
            Some(
                active_generation_pointer_path(&instance_dir)
                    .display()
                    .to_string()
                    .as_str()
            )
        );
        assert!(plan.steps[6].best_effort);
    }

    #[test]
    fn activation_persistence_plan_rejects_unverified_target() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        let target = sample_generation(21, GenerationStatus::Candidate);

        let error = plan_activation_persistence(&instance_dir, &target, None, None)
            .expect_err("candidate target should be rejected");

        assert!(error.contains("must be verified or active"));
    }

    #[test]
    fn activation_persistence_plan_uses_existing_previous_for_active_target() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        let target = sample_generation(20, GenerationStatus::Active);
        let store = AppGenerationStore {
            active: Some(target.clone()),
            candidate: None,
            rollback: Some(sample_generation(19, GenerationStatus::Rollback)),
            next_id: 21,
        };

        let plan = plan_activation_persistence(&instance_dir, &target, Some(&store), None)
            .expect("active target plan should succeed");

        assert_eq!(plan.previous_active_generation_id, Some(19));
        assert_eq!(plan.steps[3].generation_id, Some(19));
        assert!(plan.steps[3]
            .description
            .contains("Write previous pointer to generation 19"));
    }

    #[test]
    fn activation_persistence_plan_rejects_missing_lock_path_metadata() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        let mut target = sample_generation(21, GenerationStatus::Verified);
        target.lock_path.clear();

        let error = plan_activation_persistence(&instance_dir, &target, None, None)
            .expect_err("missing lock_path should be rejected");

        assert!(error.contains("missing lock_path metadata"));
    }

    #[test]
    fn activation_persistence_plan_derives_previous_active_from_index_when_store_missing() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        let target = sample_generation(21, GenerationStatus::Verified);
        let index = AppGenerationIndex {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: Some(20),
            previous: Some(19),
            candidate: Some(21),
            next_id: 22,
            generations: vec![
                sample_generation(19, GenerationStatus::Rollback),
                sample_generation(20, GenerationStatus::Active),
                target.clone(),
            ],
        };

        let plan = plan_activation_persistence(&instance_dir, &target, None, Some(&index))
            .expect("index should provide previous active context");

        assert_eq!(plan.previous_active_generation_id, Some(20));
        assert_eq!(plan.steps[3].generation_id, Some(20));
    }

    #[test]
    fn activation_persistence_plan_rejects_index_missing_target_generation() {
        let root = tempdir().expect("temp dir");
        let instance_dir = root.path().join(".weft").join("weft-claw");
        let target = sample_generation(21, GenerationStatus::Verified);
        let index = AppGenerationIndex {
            schema_version: GENERATION_INDEX_SCHEMA_VERSION,
            active: Some(20),
            previous: Some(19),
            candidate: Some(21),
            next_id: 22,
            generations: vec![
                sample_generation(19, GenerationStatus::Rollback),
                sample_generation(20, GenerationStatus::Active),
            ],
        };

        let error = plan_activation_persistence(&instance_dir, &target, None, Some(&index))
            .expect_err("index missing target generation should fail");

        assert!(error.contains("target generation 21 is missing from generation index"));
    }
}