presolve-compiler 0.1.0-alpha.6

The Presolve compiler toolchain for TypeScript web applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
use std::collections::{BTreeMap, BTreeSet};

use crate::{
    ExecutionBoundary, FieldDependencyId, FieldId, FormEntity, FormFieldEntity, FormId,
    FormOwnershipGraph, FormOwnershipNodeKey, SemanticId, SourceProvenance, ValidationGraph,
    ValidationGraphEdgeKind, ValidationGraphNodeKey, ValidationPlanId, ValidationRule,
    ValidationRuleId, ValidationRuleKind,
};

/// I7 deliberately retains no initial- or submission-validation policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationPlanningStatus {
    Deferred,
}

/// One direct I6 `ValidationRule -> source Field` read relation, projected into
/// I7 invalidation planning. It is declaration-level and immutable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldValidationDependency {
    pub id: FieldDependencyId,
    pub plan: ValidationPlanId,
    pub form: FormId,
    pub component: SemanticId,
    pub source_field: FieldId,
    pub target_field: FieldId,
    pub dependent_rule: ValidationRuleId,
    pub rule_kind: ValidationRuleKind,
    pub execution_boundary: ExecutionBoundary,
    pub source_field_order: usize,
    pub target_field_order: usize,
    pub rule_order: usize,
    pub provenance: SourceProvenance,
    pub source_field_provenance: SourceProvenance,
    pub target_field_provenance: SourceProvenance,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldValidationSourceEntry {
    pub source_field: FieldId,
    pub form: FormId,
    pub authored_field_order: usize,
    pub directly_invalidated_rules: Vec<ValidationRuleId>,
    pub directly_invalidated_target_fields: Vec<FieldId>,
    pub dependencies: Vec<FieldDependencyId>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldValidationTargetEntry {
    pub target_field: FieldId,
    pub form: FormId,
    pub authored_field_order: usize,
    pub cross_field_rules: Vec<ValidationRuleId>,
    pub source_fields: Vec<FieldId>,
    pub dependencies: Vec<FieldDependencyId>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FieldDependencyBlockReason {
    MissingRule,
    MissingSourceField,
    MissingTargetField,
    MissingForm,
    RuleNotInValidationGraph,
    RuleHasNoDependency,
    RuleCycleExcluded,
    RuleCandidateOnly,
    SourceTargetFormMismatch,
    ComponentMismatch,
    UnsupportedBoundary,
    MissingProvenance,
    IdentityMismatch,
}

/// Retained only for malformed or stale canonical I6/I5 products. Normal I6
/// candidates remain solely in I6 candidate registries.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockedFieldValidationDependency {
    pub rule: Option<ValidationRuleId>,
    pub source_field: Option<FieldId>,
    pub target_field: Option<FieldId>,
    pub form: Option<FormId>,
    pub reason: FieldDependencyBlockReason,
    pub provenance: Option<SourceProvenance>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationDependencyPlanIntegrityDiagnostic {
    pub code: String,
    pub kind: ValidationDependencyPlanIntegrityKind,
    pub message: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ValidationDependencyPlanIntegrityKind {
    DuplicatePlan,
    MissingPlanForm,
    UnknownForm,
    UnknownSourceField,
    UnknownTargetField,
    UnknownRule,
    RuleNotInValidationGraph,
    MissingI6Dependency,
    DuplicateDependencyProjection,
    MissingDependencyProjection,
    PlanFormMismatch,
    FieldFormMismatch,
    RuleTargetMismatch,
    RuleFormMismatch,
    ComponentMismatch,
    SelfDependency,
    CycleRulePromoted,
    UnsupportedBoundary,
    SourceIndexMismatch,
    TargetIndexMismatch,
    InvalidationIndexMismatch,
    DuplicateScheduledRule,
    DuplicateScheduledTarget,
    TransitiveInvalidationLeak,
    CandidateIdentityPromoted,
    InstanceIdentityLeak,
    MissingProvenance,
    NonCanonicalOrdering,
    PlanIdentityDrift,
}

impl ValidationDependencyPlanIntegrityKind {
    #[must_use]
    pub const fn code(self) -> &'static str {
        match self {
            Self::DuplicatePlan => "PSASM1242",
            Self::MissingPlanForm => "PSASM1243",
            Self::UnknownForm => "PSASM1244",
            Self::UnknownSourceField => "PSASM1245",
            Self::UnknownTargetField => "PSASM1246",
            Self::UnknownRule => "PSASM1247",
            Self::RuleNotInValidationGraph => "PSASM1248",
            Self::MissingI6Dependency => "PSASM1249",
            Self::DuplicateDependencyProjection => "PSASM1250",
            Self::MissingDependencyProjection => "PSASM1251",
            Self::PlanFormMismatch => "PSASM1252",
            Self::FieldFormMismatch => "PSASM1253",
            Self::RuleTargetMismatch => "PSASM1254",
            Self::RuleFormMismatch => "PSASM1255",
            Self::ComponentMismatch => "PSASM1256",
            Self::SelfDependency => "PSASM1257",
            Self::CycleRulePromoted => "PSASM1258",
            Self::UnsupportedBoundary => "PSASM1259",
            Self::SourceIndexMismatch => "PSASM1260",
            Self::TargetIndexMismatch => "PSASM1261",
            Self::InvalidationIndexMismatch => "PSASM1262",
            Self::DuplicateScheduledRule => "PSASM1263",
            Self::DuplicateScheduledTarget => "PSASM1264",
            Self::TransitiveInvalidationLeak => "PSASM1265",
            Self::CandidateIdentityPromoted => "PSASM1266",
            Self::InstanceIdentityLeak => "PSASM1267",
            Self::MissingProvenance => "PSASM1268",
            Self::NonCanonicalOrdering => "PSASM1269",
            Self::PlanIdentityDrift => "PSASM1270",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationDependencyPlanValidation {
    pub diagnostics: Vec<ValidationDependencyPlanIntegrityDiagnostic>,
    pub is_valid: bool,
}

impl Default for ValidationDependencyPlanValidation {
    fn default() -> Self {
        Self {
            diagnostics: Vec::new(),
            is_valid: true,
        }
    }
}

/// Exactly one declaration-level plan exists for every valid Form, including
/// Forms with no Fields or no cross-field rules.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormValidationDependencyPlan {
    pub id: ValidationPlanId,
    pub form: FormId,
    pub component: SemanticId,
    pub fields: Vec<FieldId>,
    pub dependencies: Vec<FieldDependencyId>,
    pub source_entries: Vec<FieldValidationSourceEntry>,
    pub target_entries: Vec<FieldValidationTargetEntry>,
    pub initial_validation: ValidationPlanningStatus,
    pub submission_validation: ValidationPlanningStatus,
    pub validation: ValidationDependencyPlanValidation,
}

/// Complete I7 internal planning product. It is not a public schema, runtime
/// artifact, or new ASM ownership authority.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationDependencyPlans {
    pub plans: BTreeMap<ValidationPlanId, FormValidationDependencyPlan>,
    pub dependencies: BTreeMap<FieldDependencyId, FieldValidationDependency>,
    pub blocked: Vec<BlockedFieldValidationDependency>,
    pub validation: ValidationDependencyPlanValidation,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldChangeSet {
    pub form: FormId,
    pub changed_fields: Vec<FieldId>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldValidationChangePlan {
    pub plan: ValidationPlanId,
    pub form: FormId,
    pub changed_field: FieldId,
    pub scheduled_rules: Vec<ValidationRuleId>,
    pub scheduled_target_fields: Vec<FieldId>,
    pub dependencies: Vec<FieldDependencyId>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldChangeValidationSchedule {
    pub plan: ValidationPlanId,
    pub form: FormId,
    pub changed_fields: Vec<FieldId>,
    pub scheduled_rules: Vec<ValidationRuleId>,
    pub scheduled_target_fields: Vec<FieldId>,
    pub triggering_dependencies: Vec<FieldDependencyId>,
}

impl ValidationDependencyPlans {
    #[must_use]
    pub fn validation_plan(&self, form: &FormId) -> Option<&FormValidationDependencyPlan> {
        self.plans.get(&ValidationPlanId::for_form(form))
    }

    #[must_use]
    pub fn validation_plan_by_id(
        &self,
        id: &ValidationPlanId,
    ) -> Option<&FormValidationDependencyPlan> {
        self.plans.get(id)
    }

    #[must_use]
    pub fn dependency(&self, id: &FieldDependencyId) -> Option<&FieldValidationDependency> {
        self.dependencies.get(id)
    }

    #[must_use]
    pub fn dependencies_of_plan(&self, plan: &ValidationPlanId) -> Vec<&FieldValidationDependency> {
        let mut dependencies = self
            .dependencies
            .values()
            .filter(|dependency| &dependency.plan == plan)
            .collect::<Vec<_>>();
        dependencies.sort_by(|left, right| dependency_order(left, right));
        dependencies
    }

    #[must_use]
    pub fn source_entry(&self, field: &FieldId) -> Option<&FieldValidationSourceEntry> {
        self.plans.values().find_map(|plan| {
            plan.source_entries
                .iter()
                .find(|entry| &entry.source_field == field)
        })
    }

    #[must_use]
    pub fn target_entry(&self, field: &FieldId) -> Option<&FieldValidationTargetEntry> {
        self.plans.values().find_map(|plan| {
            plan.target_entries
                .iter()
                .find(|entry| &entry.target_field == field)
        })
    }

    #[must_use]
    pub fn directly_invalidated_rules(&self, field: &FieldId) -> Vec<&ValidationRuleId> {
        self.source_entry(field).map_or_else(Vec::new, |entry| {
            entry.directly_invalidated_rules.iter().collect()
        })
    }

    #[must_use]
    pub fn directly_invalidated_target_fields(&self, field: &FieldId) -> Vec<&FieldId> {
        self.source_entry(field).map_or_else(Vec::new, |entry| {
            entry.directly_invalidated_target_fields.iter().collect()
        })
    }

    #[must_use]
    pub fn dependencies_from_field(&self, field: &FieldId) -> Vec<&FieldValidationDependency> {
        let mut dependencies = self
            .dependencies
            .values()
            .filter(|dependency| &dependency.source_field == field)
            .collect::<Vec<_>>();
        dependencies.sort_by(|left, right| dependency_order(left, right));
        dependencies
    }

    #[must_use]
    pub fn dependencies_to_field(&self, field: &FieldId) -> Vec<&FieldValidationDependency> {
        let mut dependencies = self
            .dependencies
            .values()
            .filter(|dependency| &dependency.target_field == field)
            .collect::<Vec<_>>();
        dependencies.sort_by(|left, right| dependency_order(left, right));
        dependencies
    }

    #[must_use]
    pub fn source_field_of_dependency(&self, id: &FieldDependencyId) -> Option<&FieldId> {
        self.dependency(id)
            .map(|dependency| &dependency.source_field)
    }

    #[must_use]
    pub fn target_field_of_dependency(&self, id: &FieldDependencyId) -> Option<&FieldId> {
        self.dependency(id)
            .map(|dependency| &dependency.target_field)
    }

    #[must_use]
    pub fn rule_of_dependency(&self, id: &FieldDependencyId) -> Option<&ValidationRuleId> {
        self.dependency(id)
            .map(|dependency| &dependency.dependent_rule)
    }

    #[must_use]
    pub fn change_plan(&self, field: &FieldId) -> Option<FieldValidationChangePlan> {
        let plan = self
            .plans
            .values()
            .find(|plan| plan.fields.contains(field))?;
        let schedule = self.schedule_change_set(&plan.form, std::slice::from_ref(field))?;
        Some(FieldValidationChangePlan {
            plan: schedule.plan,
            form: schedule.form,
            changed_field: field.clone(),
            scheduled_rules: schedule.scheduled_rules,
            scheduled_target_fields: schedule.scheduled_target_fields,
            dependencies: schedule.triggering_dependencies,
        })
    }

    /// Schedules only directly read dependencies. An unknown Form or Field is
    /// rejected by returning no schedule; this query never substitutes a Form.
    #[must_use]
    pub fn schedule_change_set(
        &self,
        form: &FormId,
        changed_fields: &[FieldId],
    ) -> Option<FieldChangeValidationSchedule> {
        let plan = self.validation_plan(form)?;
        if changed_fields
            .iter()
            .any(|field| !plan.fields.contains(field))
        {
            return None;
        }

        let orders = plan_field_orders(plan);
        let mut normalized = changed_fields
            .iter()
            .cloned()
            .collect::<BTreeSet<_>>()
            .into_iter()
            .collect::<Vec<_>>();
        normalized.sort_by_key(|field| {
            (
                orders.get(field).copied().unwrap_or(usize::MAX),
                field.clone(),
            )
        });

        let triggered = normalized
            .iter()
            .flat_map(|field| self.dependencies_from_field(field).into_iter())
            .collect::<Vec<_>>();
        let mut dependency_ids = triggered
            .iter()
            .map(|dependency| dependency.id.clone())
            .collect::<Vec<_>>();
        dependency_ids.sort_by_key(|id| {
            self.dependencies
                .get(id)
                .map(dependency_sort_key)
                .unwrap_or((usize::MAX, usize::MAX, usize::MAX, id.clone()))
        });

        let mut rules = triggered
            .iter()
            .map(|dependency| dependency.dependent_rule.clone())
            .collect::<BTreeSet<_>>()
            .into_iter()
            .collect::<Vec<_>>();
        let rule_keys = triggered
            .iter()
            .map(|dependency| {
                (
                    dependency.dependent_rule.clone(),
                    rule_schedule_key(dependency),
                )
            })
            .collect::<BTreeMap<_, _>>();
        rules.sort_by_key(|rule| {
            rule_keys
                .get(rule)
                .cloned()
                .unwrap_or((usize::MAX, usize::MAX, rule.clone()))
        });

        let mut target_fields = triggered
            .iter()
            .map(|dependency| dependency.target_field.clone())
            .collect::<BTreeSet<_>>()
            .into_iter()
            .collect::<Vec<_>>();
        target_fields.sort_by_key(|field| {
            (
                orders.get(field).copied().unwrap_or(usize::MAX),
                field.clone(),
            )
        });

        Some(FieldChangeValidationSchedule {
            plan: plan.id.clone(),
            form: form.clone(),
            changed_fields: normalized,
            scheduled_rules: rules,
            scheduled_target_fields: target_fields,
            triggering_dependencies: dependency_ids,
        })
    }

    #[must_use]
    pub fn plans_of_component(&self, component: &SemanticId) -> Vec<&FormValidationDependencyPlan> {
        self.plans
            .values()
            .filter(|plan| &plan.component == component)
            .collect()
    }

    #[must_use]
    pub fn blocked_dependencies(&self) -> &[BlockedFieldValidationDependency] {
        &self.blocked
    }
}

/// Builds I7 exclusively from I3/I5/I6 canonical products. No parser facts,
/// JSX controls, runtime state, or declaration-name resolution is consulted.
///
/// # Panics
///
/// Panics only when an I3-valid Form lacks its canonical Component owner, or
/// when a dependency accepted after I5/I6 reciprocity checks is absent from
/// the plan it canonically names.
#[must_use]
pub fn collect_validation_dependency_plans(
    forms: &BTreeMap<FormId, FormEntity>,
    fields: &BTreeMap<FieldId, FormFieldEntity>,
    rules: &BTreeMap<ValidationRuleId, ValidationRule>,
    form_ownership: &FormOwnershipGraph,
    validation_graph: &ValidationGraph,
) -> ValidationDependencyPlans {
    let mut plans = initialize_validation_plans(forms, fields);
    let (dependencies, mut blocked) =
        collect_direct_dependencies(forms, fields, rules, validation_graph);
    populate_dependency_entries(&mut plans, &dependencies);
    canonicalize_plans(&mut plans, &dependencies);
    blocked.sort_by(blocked_order);
    let mut product = ValidationDependencyPlans {
        plans,
        dependencies,
        blocked,
        validation: ValidationDependencyPlanValidation::default(),
    };
    product.validation = validate_validation_dependency_plans(
        &product,
        forms,
        fields,
        rules,
        form_ownership,
        validation_graph,
    );
    for plan in product.plans.values_mut() {
        plan.validation = product.validation.clone();
    }
    product
}

fn initialize_validation_plans(
    forms: &BTreeMap<FormId, FormEntity>,
    fields: &BTreeMap<FieldId, FormFieldEntity>,
) -> BTreeMap<ValidationPlanId, FormValidationDependencyPlan> {
    forms
        .values()
        .map(|form| {
            let form_fields = ordered_fields_for_form(fields, &form.id);
            let plan = FormValidationDependencyPlan {
                id: ValidationPlanId::for_form(&form.id),
                form: form.id.clone(),
                component: form
                    .owner
                    .entity_id()
                    .cloned()
                    .expect("valid Form has a canonical Component owner"),
                fields: form_fields.iter().map(|field| field.id.clone()).collect(),
                dependencies: Vec::new(),
                source_entries: form_fields
                    .iter()
                    .map(|field| FieldValidationSourceEntry {
                        source_field: field.id.clone(),
                        form: form.id.clone(),
                        authored_field_order: field.declaration_order,
                        directly_invalidated_rules: Vec::new(),
                        directly_invalidated_target_fields: Vec::new(),
                        dependencies: Vec::new(),
                    })
                    .collect(),
                target_entries: form_fields
                    .iter()
                    .map(|field| FieldValidationTargetEntry {
                        target_field: field.id.clone(),
                        form: form.id.clone(),
                        authored_field_order: field.declaration_order,
                        cross_field_rules: Vec::new(),
                        source_fields: Vec::new(),
                        dependencies: Vec::new(),
                    })
                    .collect(),
                initial_validation: ValidationPlanningStatus::Deferred,
                submission_validation: ValidationPlanningStatus::Deferred,
                validation: ValidationDependencyPlanValidation::default(),
            };
            (plan.id.clone(), plan)
        })
        .collect()
}

fn collect_direct_dependencies(
    forms: &BTreeMap<FormId, FormEntity>,
    fields: &BTreeMap<FieldId, FormFieldEntity>,
    rules: &BTreeMap<ValidationRuleId, ValidationRule>,
    validation_graph: &ValidationGraph,
) -> (
    BTreeMap<FieldDependencyId, FieldValidationDependency>,
    Vec<BlockedFieldValidationDependency>,
) {
    let mut dependencies = BTreeMap::new();
    let mut blocked = Vec::new();
    for rule in rules.values() {
        let Some(source_id) = rule.dependency.clone() else {
            continue;
        };
        match project_rule_dependency(rule, &source_id, forms, fields, validation_graph) {
            Ok(dependency) => {
                if dependencies
                    .insert(dependency.id.clone(), dependency)
                    .is_some()
                {
                    blocked.push(blocked_for_rule(
                        rule,
                        Some(source_id),
                        FieldDependencyBlockReason::IdentityMismatch,
                        None,
                    ));
                }
            }
            Err(blocked_dependency) => blocked.push(*blocked_dependency),
        }
    }
    (dependencies, blocked)
}

fn project_rule_dependency(
    rule: &ValidationRule,
    source_id: &FieldId,
    forms: &BTreeMap<FormId, FormEntity>,
    fields: &BTreeMap<FieldId, FormFieldEntity>,
    validation_graph: &ValidationGraph,
) -> Result<FieldValidationDependency, Box<BlockedFieldValidationDependency>> {
    let Some(source) = fields.get(source_id) else {
        return Err(Box::new(blocked_for_rule(
            rule,
            Some(source_id.clone()),
            FieldDependencyBlockReason::MissingSourceField,
            None,
        )));
    };
    let Some(target) = fields.get(&rule.target_field) else {
        return Err(Box::new(blocked_for_rule(
            rule,
            Some(source.id.clone()),
            FieldDependencyBlockReason::MissingTargetField,
            None,
        )));
    };
    let Some(form) = forms.get(&rule.owner_form) else {
        return Err(Box::new(blocked_for_rule(
            rule,
            Some(source.id.clone()),
            FieldDependencyBlockReason::MissingForm,
            None,
        )));
    };
    let edges = matching_i6_edges(validation_graph, rule, source);
    if let Some(reason) =
        dependency_block_reason(rule, source, target, form, validation_graph, &edges)
    {
        return Err(Box::new(blocked_for_rule(
            rule,
            Some(source.id.clone()),
            reason,
            edges.first(),
        )));
    }
    let id = FieldDependencyId::for_rule_and_source(&rule.id, &source.id);
    Ok(FieldValidationDependency {
        id,
        plan: ValidationPlanId::for_form(&rule.owner_form),
        form: rule.owner_form.clone(),
        component: rule.owner_component.clone(),
        source_field: source.id.clone(),
        target_field: target.id.clone(),
        dependent_rule: rule.id.clone(),
        rule_kind: rule.kind,
        execution_boundary: rule.boundary,
        source_field_order: source.declaration_order,
        target_field_order: target.declaration_order,
        rule_order: rule.rule_authored_order,
        provenance: edges[0].provenance.clone(),
        source_field_provenance: source.provenance.clone(),
        target_field_provenance: target.provenance.clone(),
    })
}

fn matching_i6_edges<'a>(
    graph: &'a ValidationGraph,
    rule: &ValidationRule,
    source: &FormFieldEntity,
) -> Vec<&'a crate::ValidationGraphEdge> {
    graph
        .edges
        .iter()
        .filter(|edge| {
            edge.kind == ValidationGraphEdgeKind::RuleDependsOnField
                && edge.source == ValidationGraphNodeKey::ValidationRule(rule.id.clone())
                && edge.target == ValidationGraphNodeKey::FormField(source.id.clone())
        })
        .collect()
}

fn dependency_block_reason(
    rule: &ValidationRule,
    source: &FormFieldEntity,
    target: &FormFieldEntity,
    form: &FormEntity,
    graph: &ValidationGraph,
    edges: &[&crate::ValidationGraphEdge],
) -> Option<FieldDependencyBlockReason> {
    if !graph
        .nodes
        .contains_key(&ValidationGraphNodeKey::ValidationRule(rule.id.clone()))
    {
        return Some(FieldDependencyBlockReason::RuleNotInValidationGraph);
    }
    if edges.len() != 1 || source.id == target.id {
        return Some(FieldDependencyBlockReason::IdentityMismatch);
    }
    if graph
        .cycles
        .iter()
        .any(|cycle| cycle.candidates.contains(&rule.candidate_id))
    {
        return Some(FieldDependencyBlockReason::RuleCycleExcluded);
    }
    if source.owner_form != rule.owner_form || target.owner_form != rule.owner_form {
        return Some(FieldDependencyBlockReason::SourceTargetFormMismatch);
    }
    if source.owner_component != rule.owner_component
        || target.owner_component != rule.owner_component
        || form.owner.entity_id() != Some(&rule.owner_component)
    {
        return Some(FieldDependencyBlockReason::ComponentMismatch);
    }
    if rule.boundary != ExecutionBoundary::Client {
        return Some(FieldDependencyBlockReason::UnsupportedBoundary);
    }
    (!has_provenance(&rule.provenance)
        || !has_provenance(&source.provenance)
        || !has_provenance(&target.provenance)
        || !has_provenance(&edges[0].provenance))
    .then_some(FieldDependencyBlockReason::MissingProvenance)
}

fn populate_dependency_entries(
    plans: &mut BTreeMap<ValidationPlanId, FormValidationDependencyPlan>,
    dependencies: &BTreeMap<FieldDependencyId, FieldValidationDependency>,
) {
    for dependency in dependencies.values() {
        let plan = plans
            .get_mut(&dependency.plan)
            .expect("a valid dependency has a canonical Form plan");
        plan.dependencies.push(dependency.id.clone());
        let source = plan
            .source_entries
            .iter_mut()
            .find(|entry| entry.source_field == dependency.source_field)
            .expect("dependency source belongs to its plan");
        source.dependencies.push(dependency.id.clone());
        source
            .directly_invalidated_rules
            .push(dependency.dependent_rule.clone());
        source
            .directly_invalidated_target_fields
            .push(dependency.target_field.clone());
        let target = plan
            .target_entries
            .iter_mut()
            .find(|entry| entry.target_field == dependency.target_field)
            .expect("dependency target belongs to its plan");
        target.dependencies.push(dependency.id.clone());
        target
            .cross_field_rules
            .push(dependency.dependent_rule.clone());
        target.source_fields.push(dependency.source_field.clone());
    }
}

#[allow(clippy::too_many_lines)]
#[must_use]
pub fn validate_validation_dependency_plans(
    products: &ValidationDependencyPlans,
    forms: &BTreeMap<FormId, FormEntity>,
    fields: &BTreeMap<FieldId, FormFieldEntity>,
    rules: &BTreeMap<ValidationRuleId, ValidationRule>,
    form_ownership: &FormOwnershipGraph,
    validation_graph: &ValidationGraph,
) -> ValidationDependencyPlanValidation {
    let mut diagnostics = Vec::new();
    let expected_plan_ids = forms
        .keys()
        .map(ValidationPlanId::for_form)
        .collect::<BTreeSet<_>>();
    let actual_plan_ids = products.plans.keys().cloned().collect::<BTreeSet<_>>();
    if expected_plan_ids != actual_plan_ids {
        push_integrity(
            &mut diagnostics,
            ValidationDependencyPlanIntegrityKind::DuplicatePlan,
            "Validation Plans do not contain exactly one canonical plan per Form",
        );
    }

    for plan in products.plans.values() {
        if plan.id != ValidationPlanId::for_form(&plan.form) {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::PlanIdentityDrift,
                "Validation Plan identity does not derive from its Form",
            );
        }
        let Some(form) = forms.get(&plan.form) else {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::UnknownForm,
                "Validation Plan references an unknown Form",
            );
            continue;
        };
        if form.owner.entity_id() != Some(&plan.component) {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::ComponentMismatch,
                "Validation Plan component does not match its Form owner",
            );
        }
        let expected_fields = ordered_fields_for_form(fields, &plan.form)
            .into_iter()
            .map(|field| field.id.clone())
            .collect::<Vec<_>>();
        if plan.fields != expected_fields {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::NonCanonicalOrdering,
                "Validation Plan Fields do not preserve I3 authored Field order",
            );
        }
        if plan.source_entries.len() != expected_fields.len()
            || plan.target_entries.len() != expected_fields.len()
        {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::SourceIndexMismatch,
                "Validation Plan must retain one source and target entry per Field",
            );
        }
        for field in &expected_fields {
            if !plan
                .source_entries
                .iter()
                .any(|entry| &entry.source_field == field)
            {
                push_integrity(
                    &mut diagnostics,
                    ValidationDependencyPlanIntegrityKind::SourceIndexMismatch,
                    "Validation Plan is missing a source Field entry",
                );
            }
            if !plan
                .target_entries
                .iter()
                .any(|entry| &entry.target_field == field)
            {
                push_integrity(
                    &mut diagnostics,
                    ValidationDependencyPlanIntegrityKind::TargetIndexMismatch,
                    "Validation Plan is missing a target Field entry",
                );
            }
        }
    }

    let mut pairs = BTreeSet::new();
    for dependency in products.dependencies.values() {
        if !pairs.insert((
            dependency.dependent_rule.clone(),
            dependency.source_field.clone(),
        )) {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::DuplicateDependencyProjection,
                "multiple I7 dependencies share one Rule/source Field tuple",
            );
        }
        validate_dependency(
            dependency,
            products,
            forms,
            fields,
            rules,
            form_ownership,
            validation_graph,
            &mut diagnostics,
        );
    }

    for rule in rules.values() {
        let Some(source) = rule.dependency.as_ref() else {
            continue;
        };
        let exact_edges = validation_graph
            .edges
            .iter()
            .filter(|edge| {
                edge.kind == ValidationGraphEdgeKind::RuleDependsOnField
                    && edge.source == ValidationGraphNodeKey::ValidationRule(rule.id.clone())
                    && edge.target == ValidationGraphNodeKey::FormField(source.clone())
            })
            .count();
        if exact_edges == 0 {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::MissingI6Dependency,
                "valid I6 cross-Field Rule has no exact RuleDependsOnField edge",
            );
        } else if exact_edges > 1 {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::DuplicateDependencyProjection,
                "I6 cross-Field Rule has multiple exact dependency edges",
            );
        } else if !products
            .dependencies
            .contains_key(&FieldDependencyId::for_rule_and_source(&rule.id, source))
        {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::MissingDependencyProjection,
                "valid I6 cross-Field edge is absent from I7 planning",
            );
        }
    }
    for edge in validation_graph
        .edges
        .iter()
        .filter(|edge| edge.kind == ValidationGraphEdgeKind::RuleDependsOnField)
    {
        let (
            ValidationGraphNodeKey::ValidationRule(rule),
            ValidationGraphNodeKey::FormField(source),
        ) = (&edge.source, &edge.target)
        else {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::RuleNotInValidationGraph,
                "I6 dependency edge does not have Rule-to-Field endpoints",
            );
            continue;
        };
        if let Some(rule_record) = rules.get(rule) {
            if rule_record.dependency.as_ref() != Some(source) {
                push_integrity(
                    &mut diagnostics,
                    ValidationDependencyPlanIntegrityKind::RuleTargetMismatch,
                    "I6 dependency edge does not match its Rule dependency Field",
                );
            }
        } else {
            push_integrity(
                &mut diagnostics,
                ValidationDependencyPlanIntegrityKind::UnknownRule,
                "I6 dependency edge references an unknown Rule",
            );
        }
    }

    validate_entry_reciprocity(products, &mut diagnostics);
    validate_product_ordering(products, &mut diagnostics);
    diagnostics
        .sort_by(|left, right| (&left.code, &left.message).cmp(&(&right.code, &right.message)));
    ValidationDependencyPlanValidation {
        is_valid: diagnostics.is_empty(),
        diagnostics,
    }
}

#[allow(clippy::too_many_arguments)]
fn validate_dependency(
    dependency: &FieldValidationDependency,
    products: &ValidationDependencyPlans,
    forms: &BTreeMap<FormId, FormEntity>,
    fields: &BTreeMap<FieldId, FormFieldEntity>,
    rules: &BTreeMap<ValidationRuleId, ValidationRule>,
    form_ownership: &FormOwnershipGraph,
    validation_graph: &ValidationGraph,
    diagnostics: &mut Vec<ValidationDependencyPlanIntegrityDiagnostic>,
) {
    let Some(plan) = products.plans.get(&dependency.plan) else {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::MissingPlanForm,
            "Field dependency references a missing Validation Plan",
        );
        return;
    };
    let Some(source) = fields.get(&dependency.source_field) else {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::UnknownSourceField,
            "Field dependency references an unknown source Field",
        );
        return;
    };
    let Some(target) = fields.get(&dependency.target_field) else {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::UnknownTargetField,
            "Field dependency references an unknown target Field",
        );
        return;
    };
    let Some(rule) = rules.get(&dependency.dependent_rule) else {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::UnknownRule,
            "Field dependency references an unknown Validation Rule",
        );
        return;
    };
    validate_dependency_scope(dependency, plan, source, target, rule, diagnostics);
    validate_dependency_backing(
        dependency,
        source,
        rule,
        forms,
        form_ownership,
        validation_graph,
        diagnostics,
    );
    validate_dependency_provenance(dependency, diagnostics);
}

fn validate_dependency_scope(
    dependency: &FieldValidationDependency,
    plan: &FormValidationDependencyPlan,
    source: &FormFieldEntity,
    target: &FormFieldEntity,
    rule: &ValidationRule,
    diagnostics: &mut Vec<ValidationDependencyPlanIntegrityDiagnostic>,
) {
    if dependency.id != FieldDependencyId::for_rule_and_source(&rule.id, &source.id) {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::PlanIdentityDrift,
            "Field dependency identity does not derive from Rule and source Field",
        );
    }
    if dependency.form != plan.form
        || dependency.form != source.owner_form
        || dependency.form != target.owner_form
    {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::PlanFormMismatch,
            "Field dependency source, target, and plan do not share one Form",
        );
    }
    if dependency.form != rule.owner_form {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::RuleFormMismatch,
            "Field dependency Rule does not belong to its Form",
        );
    }
    if dependency.component != source.owner_component
        || dependency.component != target.owner_component
        || dependency.component != rule.owner_component
    {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::ComponentMismatch,
            "Field dependency does not share one Component owner",
        );
    }
    if source.id == target.id {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::SelfDependency,
            "Field dependency cannot read its target Field",
        );
    }
    if dependency.target_field != rule.target_field {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::RuleTargetMismatch,
            "Field dependency target does not match its Rule target",
        );
    }
    if rule.dependency.as_ref() != Some(&dependency.source_field) {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::MissingI6Dependency,
            "Field dependency does not match its I6 Rule dependency",
        );
    }
    if dependency.execution_boundary != ExecutionBoundary::Client
        || rule.boundary != ExecutionBoundary::Client
    {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::UnsupportedBoundary,
            "I7 plans accept only Client-boundary Rules",
        );
    }
}

fn validate_dependency_backing(
    dependency: &FieldValidationDependency,
    source: &FormFieldEntity,
    rule: &ValidationRule,
    forms: &BTreeMap<FormId, FormEntity>,
    form_ownership: &FormOwnershipGraph,
    validation_graph: &ValidationGraph,
    diagnostics: &mut Vec<ValidationDependencyPlanIntegrityDiagnostic>,
) {
    if validation_graph
        .cycles
        .iter()
        .any(|cycle| cycle.candidates.contains(&rule.candidate_id))
    {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::CycleRulePromoted,
            "cycle-excluded I6 Rule entered I7 planning",
        );
    }
    if !validation_graph
        .nodes
        .contains_key(&ValidationGraphNodeKey::ValidationRule(rule.id.clone()))
        || matching_i6_edges(validation_graph, rule, source).len() != 1
    {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::RuleNotInValidationGraph,
            "I7 dependency is not backed by an exact I6 Validation Graph edge",
        );
    }
    let owns_field = |field: &FieldId| {
        form_ownership.ownership_edges.iter().any(|edge| {
            edge.owner == FormOwnershipNodeKey::Form(dependency.form.clone())
                && edge.child == FormOwnershipNodeKey::FormField(field.clone())
        })
    };
    if !owns_field(&dependency.source_field) || !owns_field(&dependency.target_field) {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::FieldFormMismatch,
            "I7 dependency Fields are not canonical I5 children of its Form",
        );
    }
    if !forms.contains_key(&dependency.form) {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::UnknownForm,
            "I7 dependency references an unknown Form",
        );
    }
}

fn validate_dependency_provenance(
    dependency: &FieldValidationDependency,
    diagnostics: &mut Vec<ValidationDependencyPlanIntegrityDiagnostic>,
) {
    if !has_provenance(&dependency.provenance)
        || !has_provenance(&dependency.source_field_provenance)
        || !has_provenance(&dependency.target_field_provenance)
    {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::MissingProvenance,
            "I7 dependency lacks canonical source provenance",
        );
    }
    if dependency.id.as_str().contains("form-instance")
        || dependency.plan.as_str().contains("form-instance")
        || dependency.form.as_str().contains("form-instance")
    {
        push_integrity(
            diagnostics,
            ValidationDependencyPlanIntegrityKind::InstanceIdentityLeak,
            "I7 declaration planning contains an instance-qualified identity",
        );
    }
}

fn validate_entry_reciprocity(
    products: &ValidationDependencyPlans,
    diagnostics: &mut Vec<ValidationDependencyPlanIntegrityDiagnostic>,
) {
    for plan in products.plans.values() {
        for source in &plan.source_entries {
            let expected = products
                .dependencies
                .values()
                .filter(|dependency| {
                    dependency.plan == plan.id && dependency.source_field == source.source_field
                })
                .collect::<Vec<_>>();
            let expected_ids = expected
                .iter()
                .map(|dependency| dependency.id.clone())
                .collect::<BTreeSet<_>>();
            let actual_ids = source.dependencies.iter().cloned().collect::<BTreeSet<_>>();
            if expected_ids != actual_ids {
                push_integrity(
                    diagnostics,
                    ValidationDependencyPlanIntegrityKind::InvalidationIndexMismatch,
                    "source entry does not contain exactly its direct I7 dependencies",
                );
            }
            let expected_rules = expected
                .iter()
                .map(|dependency| dependency.dependent_rule.clone())
                .collect::<BTreeSet<_>>();
            let actual_rules = source
                .directly_invalidated_rules
                .iter()
                .cloned()
                .collect::<BTreeSet<_>>();
            if expected_rules != actual_rules {
                push_integrity(
                    diagnostics,
                    ValidationDependencyPlanIntegrityKind::TransitiveInvalidationLeak,
                    "source entry contains non-direct invalidated Rules",
                );
            }
        }
        for target in &plan.target_entries {
            let expected = products
                .dependencies
                .values()
                .filter(|dependency| {
                    dependency.plan == plan.id && dependency.target_field == target.target_field
                })
                .map(|dependency| dependency.id.clone())
                .collect::<BTreeSet<_>>();
            let actual = target.dependencies.iter().cloned().collect::<BTreeSet<_>>();
            if expected != actual {
                push_integrity(
                    diagnostics,
                    ValidationDependencyPlanIntegrityKind::TargetIndexMismatch,
                    "target entry does not contain exactly its direct I7 dependencies",
                );
            }
        }
    }
    for dependency in products.dependencies.values() {
        let Some(plan) = products.plans.get(&dependency.plan) else {
            continue;
        };
        let source = plan
            .source_entries
            .iter()
            .find(|entry| entry.source_field == dependency.source_field);
        if !source.is_some_and(|entry| {
            entry.dependencies.contains(&dependency.id)
                && entry
                    .directly_invalidated_rules
                    .contains(&dependency.dependent_rule)
                && entry
                    .directly_invalidated_target_fields
                    .contains(&dependency.target_field)
        }) {
            push_integrity(
                diagnostics,
                ValidationDependencyPlanIntegrityKind::SourceIndexMismatch,
                "I7 source index omits its dependency projection",
            );
        }
        let target = plan
            .target_entries
            .iter()
            .find(|entry| entry.target_field == dependency.target_field);
        if !target.is_some_and(|entry| {
            entry.dependencies.contains(&dependency.id)
                && entry.cross_field_rules.contains(&dependency.dependent_rule)
                && entry.source_fields.contains(&dependency.source_field)
        }) {
            push_integrity(
                diagnostics,
                ValidationDependencyPlanIntegrityKind::TargetIndexMismatch,
                "I7 target index omits its dependency projection",
            );
        }
    }
}

fn validate_product_ordering(
    products: &ValidationDependencyPlans,
    diagnostics: &mut Vec<ValidationDependencyPlanIntegrityDiagnostic>,
) {
    for plan in products.plans.values() {
        let orders = plan_field_orders(plan);
        let mut ordered_dependencies = plan.dependencies.clone();
        ordered_dependencies.sort_by(|left, right| {
            dependency_order(
                products
                    .dependencies
                    .get(left)
                    .expect("plan dependency exists"),
                products
                    .dependencies
                    .get(right)
                    .expect("plan dependency exists"),
            )
        });
        if plan.dependencies != ordered_dependencies {
            push_integrity(
                diagnostics,
                ValidationDependencyPlanIntegrityKind::NonCanonicalOrdering,
                "I7 plan dependencies are not canonically ordered",
            );
        }
        for source in &plan.source_entries {
            if has_duplicates(&source.directly_invalidated_rules) {
                push_integrity(
                    diagnostics,
                    ValidationDependencyPlanIntegrityKind::DuplicateScheduledRule,
                    "source entry schedules one Rule more than once",
                );
            }
            if has_duplicates(&source.directly_invalidated_target_fields) {
                push_integrity(
                    diagnostics,
                    ValidationDependencyPlanIntegrityKind::DuplicateScheduledTarget,
                    "source entry schedules one target Field more than once",
                );
            }
            let mut rules = source.directly_invalidated_rules.clone();
            rules.sort_by(|left, right| {
                let left = products
                    .dependencies
                    .values()
                    .find(|dependency| {
                        &dependency.dependent_rule == left
                            && dependency.source_field == source.source_field
                    })
                    .expect("source Rule has dependency");
                let right = products
                    .dependencies
                    .values()
                    .find(|dependency| {
                        &dependency.dependent_rule == right
                            && dependency.source_field == source.source_field
                    })
                    .expect("source Rule has dependency");
                rule_schedule_order(left, right)
            });
            if source.directly_invalidated_rules != rules {
                push_integrity(
                    diagnostics,
                    ValidationDependencyPlanIntegrityKind::InvalidationIndexMismatch,
                    "source invalidation Rules are not in schedule order",
                );
            }
        }
        if plan
            .source_entries
            .iter()
            .map(|entry| entry.authored_field_order)
            .collect::<Vec<_>>()
            != plan
                .source_entries
                .iter()
                .map(|entry| orders[&entry.source_field])
                .collect::<Vec<_>>()
        {
            push_integrity(
                diagnostics,
                ValidationDependencyPlanIntegrityKind::NonCanonicalOrdering,
                "source entries do not preserve authored Field order",
            );
        }
    }
}

fn canonicalize_plans(
    plans: &mut BTreeMap<ValidationPlanId, FormValidationDependencyPlan>,
    dependencies: &BTreeMap<FieldDependencyId, FieldValidationDependency>,
) {
    for plan in plans.values_mut() {
        plan.fields.sort_by_key(|field| {
            plan.source_entries
                .iter()
                .find(|entry| entry.source_field == *field)
                .map_or(usize::MAX, |entry| entry.authored_field_order)
        });
        plan.dependencies.sort_by(|left, right| {
            dependency_order(
                dependencies.get(left).expect("plan dependency exists"),
                dependencies.get(right).expect("plan dependency exists"),
            )
        });
        let orders = plan_field_orders(plan);
        for source in &mut plan.source_entries {
            source.dependencies.sort_by(|left, right| {
                dependency_order(
                    dependencies.get(left).expect("source dependency exists"),
                    dependencies.get(right).expect("source dependency exists"),
                )
            });
            source.directly_invalidated_rules.sort_by(|left, right| {
                let left = dependencies
                    .values()
                    .find(|dependency| {
                        &dependency.dependent_rule == left
                            && dependency.source_field == source.source_field
                    })
                    .expect("source Rule has dependency");
                let right = dependencies
                    .values()
                    .find(|dependency| {
                        &dependency.dependent_rule == right
                            && dependency.source_field == source.source_field
                    })
                    .expect("source Rule has dependency");
                rule_schedule_order(left, right)
            });
            source.directly_invalidated_rules.dedup();
            source
                .directly_invalidated_target_fields
                .sort_by_key(|field| {
                    (
                        orders.get(field).copied().unwrap_or(usize::MAX),
                        field.clone(),
                    )
                });
            source.directly_invalidated_target_fields.dedup();
        }
        for target in &mut plan.target_entries {
            target.dependencies.sort_by(|left, right| {
                dependency_order(
                    dependencies.get(left).expect("target dependency exists"),
                    dependencies.get(right).expect("target dependency exists"),
                )
            });
            target.cross_field_rules.sort_by(|left, right| {
                let left = dependencies
                    .values()
                    .find(|dependency| {
                        &dependency.dependent_rule == left
                            && dependency.target_field == target.target_field
                    })
                    .expect("target Rule has dependency");
                let right = dependencies
                    .values()
                    .find(|dependency| {
                        &dependency.dependent_rule == right
                            && dependency.target_field == target.target_field
                    })
                    .expect("target Rule has dependency");
                rule_schedule_order(left, right)
            });
            target.cross_field_rules.dedup();
            target.source_fields.sort_by_key(|field| {
                (
                    orders.get(field).copied().unwrap_or(usize::MAX),
                    field.clone(),
                )
            });
            target.source_fields.dedup();
        }
        plan.source_entries
            .sort_by_key(|entry| (entry.authored_field_order, entry.source_field.clone()));
        plan.target_entries
            .sort_by_key(|entry| (entry.authored_field_order, entry.target_field.clone()));
    }
}

fn ordered_fields_for_form<'a>(
    fields: &'a BTreeMap<FieldId, FormFieldEntity>,
    form: &FormId,
) -> Vec<&'a FormFieldEntity> {
    let mut result = fields
        .values()
        .filter(|field| &field.owner_form == form)
        .collect::<Vec<_>>();
    result.sort_by(|left, right| field_order(left, right));
    result
}

fn field_order(left: &FormFieldEntity, right: &FormFieldEntity) -> std::cmp::Ordering {
    (left.declaration_order, &left.id).cmp(&(right.declaration_order, &right.id))
}

fn plan_field_orders(plan: &FormValidationDependencyPlan) -> BTreeMap<FieldId, usize> {
    plan.source_entries
        .iter()
        .map(|entry| (entry.source_field.clone(), entry.authored_field_order))
        .collect()
}

fn dependency_order(
    left: &FieldValidationDependency,
    right: &FieldValidationDependency,
) -> std::cmp::Ordering {
    (
        left.target_field_order,
        left.rule_order,
        left.source_field_order,
        &left.id,
    )
        .cmp(&(
            right.target_field_order,
            right.rule_order,
            right.source_field_order,
            &right.id,
        ))
}

fn dependency_sort_key(
    dependency: &FieldValidationDependency,
) -> (usize, usize, usize, FieldDependencyId) {
    (
        dependency.target_field_order,
        dependency.rule_order,
        dependency.source_field_order,
        dependency.id.clone(),
    )
}

fn rule_schedule_order(
    left: &FieldValidationDependency,
    right: &FieldValidationDependency,
) -> std::cmp::Ordering {
    (
        left.target_field_order,
        left.rule_order,
        &left.dependent_rule,
    )
        .cmp(&(
            right.target_field_order,
            right.rule_order,
            &right.dependent_rule,
        ))
}

fn rule_schedule_key(dependency: &FieldValidationDependency) -> (usize, usize, ValidationRuleId) {
    (
        dependency.target_field_order,
        dependency.rule_order,
        dependency.dependent_rule.clone(),
    )
}

fn blocked_for_rule(
    rule: &ValidationRule,
    source: Option<FieldId>,
    reason: FieldDependencyBlockReason,
    edge: Option<&&crate::ValidationGraphEdge>,
) -> BlockedFieldValidationDependency {
    BlockedFieldValidationDependency {
        rule: Some(rule.id.clone()),
        source_field: source,
        target_field: Some(rule.target_field.clone()),
        form: Some(rule.owner_form.clone()),
        reason,
        provenance: edge
            .map(|edge| edge.provenance.clone())
            .or_else(|| Some(rule.provenance.clone())),
    }
}

fn blocked_order(
    left: &BlockedFieldValidationDependency,
    right: &BlockedFieldValidationDependency,
) -> std::cmp::Ordering {
    (
        left.form.as_ref().map_or("", FormId::as_str),
        left.target_field.as_ref().map_or("", FieldId::as_str),
        left.rule.as_ref().map_or("", ValidationRuleId::as_str),
        left.source_field.as_ref().map_or("", FieldId::as_str),
        left.reason,
        left.provenance
            .as_ref()
            .map_or("", |provenance| provenance.path.to_str().unwrap_or("")),
        left.provenance
            .as_ref()
            .map_or(0, |provenance| provenance.span.start),
    )
        .cmp(&(
            right.form.as_ref().map_or("", FormId::as_str),
            right.target_field.as_ref().map_or("", FieldId::as_str),
            right.rule.as_ref().map_or("", ValidationRuleId::as_str),
            right.source_field.as_ref().map_or("", FieldId::as_str),
            right.reason,
            right
                .provenance
                .as_ref()
                .map_or("", |provenance| provenance.path.to_str().unwrap_or("")),
            right
                .provenance
                .as_ref()
                .map_or(0, |provenance| provenance.span.start),
        ))
}

fn has_provenance(provenance: &SourceProvenance) -> bool {
    !provenance.path.as_os_str().is_empty() && provenance.span.start <= provenance.span.end
}

fn has_duplicates<T: Ord + Clone>(values: &[T]) -> bool {
    values.iter().cloned().collect::<BTreeSet<_>>().len() != values.len()
}

fn push_integrity(
    diagnostics: &mut Vec<ValidationDependencyPlanIntegrityDiagnostic>,
    kind: ValidationDependencyPlanIntegrityKind,
    message: &str,
) {
    diagnostics.push(ValidationDependencyPlanIntegrityDiagnostic {
        code: kind.code().to_string(),
        kind,
        message: message.to_string(),
    });
}

#[cfg(test)]
mod tests {
    use super::{
        collect_validation_dependency_plans, validate_validation_dependency_plans,
        FieldDependencyBlockReason, ValidationDependencyPlanIntegrityKind,
        ValidationPlanningStatus,
    };
    use crate::{
        build_application_semantic_model, build_application_semantic_model_for_unit,
        build_semantic_graph, semantic_graph_json, validate_application_semantic_model,
        CompilationUnit, FormInstanceId, SemanticGraph, SEMANTIC_GRAPH_SCHEMA_VERSION,
    };

    fn build(source: &str) -> crate::ApplicationSemanticModel {
        build_application_semantic_model(&presolve_parser::parse_file("src/Profile.tsx", source))
    }

    #[test]
    fn plans_direct_cross_field_dependencies_without_unary_or_transitive_propagation() {
        let asm = build(
            r#"
@component("profile")
class Profile {
  @form() form!: Form;
  @field(this.form) c = "";
  @validate(required()) @validate(equals(this.c)) @field(this.form) b = "";
  @validate(equals(this.b)) @field(this.form) a = "";
  render() { return <div />; }
}
"#,
        );
        let form = asm.forms.keys().next().unwrap();
        let fields = &asm.form_fields;
        let c = fields
            .values()
            .find(|field| field.name == "c")
            .unwrap()
            .id
            .clone();
        let b = fields
            .values()
            .find(|field| field.name == "b")
            .unwrap()
            .id
            .clone();
        let a = fields
            .values()
            .find(|field| field.name == "a")
            .unwrap()
            .id
            .clone();
        let plan = asm
            .validation_dependency_plans
            .validation_plan(form)
            .unwrap();
        assert_eq!(plan.dependencies.len(), 2);
        assert!(plan
            .target_entries
            .iter()
            .find(|entry| entry.target_field == b)
            .is_some_and(|entry| entry.cross_field_rules.len() == 1));
        let c_schedule = asm
            .validation_dependency_plans
            .schedule_change_set(form, std::slice::from_ref(&c))
            .unwrap();
        assert_eq!(c_schedule.scheduled_rules.len(), 1);
        assert_eq!(c_schedule.scheduled_target_fields, vec![b.clone()]);
        let b_schedule = asm
            .validation_dependency_plans
            .schedule_change_set(form, std::slice::from_ref(&b))
            .unwrap();
        assert_eq!(b_schedule.scheduled_target_fields, vec![a]);
        assert_eq!(plan.initial_validation, ValidationPlanningStatus::Deferred);
        assert_eq!(
            plan.submission_validation,
            ValidationPlanningStatus::Deferred
        );
    }

    #[test]
    fn schedules_change_sets_by_target_order_deduplicates_rules_and_keeps_evidence() {
        let asm = build(
            r#"
@component("profile")
class Profile {
  @form() form!: Form;
  @field(this.form) first = "";
  @field(this.form) second = "";
  @validate(equals(this.second)) @field(this.form) alpha = "";
  @validate(equals(this.first)) @field(this.form) beta = "";
  render() { return <div />; }
}
"#,
        );
        let form = asm.forms.keys().next().unwrap();
        let first = asm
            .form_fields
            .values()
            .find(|field| field.name == "first")
            .unwrap()
            .id
            .clone();
        let second = asm
            .form_fields
            .values()
            .find(|field| field.name == "second")
            .unwrap()
            .id
            .clone();
        let alpha = asm
            .form_fields
            .values()
            .find(|field| field.name == "alpha")
            .unwrap()
            .id
            .clone();
        let beta = asm
            .form_fields
            .values()
            .find(|field| field.name == "beta")
            .unwrap()
            .id
            .clone();
        let schedule = asm
            .validation_dependency_plans
            .schedule_change_set(form, &[second.clone(), first.clone(), second])
            .unwrap();
        assert_eq!(
            schedule.changed_fields,
            vec![
                first,
                asm.form_fields
                    .values()
                    .find(|field| field.name == "second")
                    .unwrap()
                    .id
                    .clone()
            ]
        );
        assert_eq!(schedule.scheduled_target_fields, vec![alpha, beta]);
        assert_eq!(schedule.scheduled_rules.len(), 2);
        assert_eq!(schedule.triggering_dependencies.len(), 2);
    }

    #[test]
    fn creates_empty_plans_and_rejects_cross_form_change_sets() {
        let asm = build(
            r#"
@component("profile")
class Profile {
  @form() empty!: Form;
  @form() active!: Form;
  @field(this.active) value = "";
  render() { return <div />; }
}
"#,
        );
        assert_eq!(asm.validation_dependency_plans.plans.len(), 2);
        let empty = asm
            .forms
            .values()
            .find(|form| form.name == "empty")
            .unwrap();
        assert!(asm
            .validation_dependency_plans
            .validation_plan(&empty.id)
            .is_some_and(|plan| plan.fields.is_empty() && plan.dependencies.is_empty()));
        let active = asm
            .forms
            .values()
            .find(|form| form.name == "active")
            .unwrap();
        let field = asm.form_fields.values().next().unwrap();
        assert!(asm
            .validation_dependency_plans
            .schedule_change_set(&empty.id, std::slice::from_ref(&field.id))
            .is_none());
        assert!(asm
            .validation_dependency_plans
            .schedule_change_set(&active.id, &[])
            .is_some());
    }

    #[test]
    fn retains_blocked_records_and_detects_missing_projection_and_index_drift() {
        let asm = build(
            r#"
@component("profile")
class Profile {
  @form() form!: Form;
  @field(this.form) source = "";
  @validate(equals(this.source)) @field(this.form) target = "";
  render() { return <div />; }
}
"#,
        );
        let mut missing = asm.validation_dependency_plans.clone();
        let dependency = missing.dependencies.keys().next().unwrap().clone();
        missing.dependencies.remove(&dependency);
        missing
            .plans
            .values_mut()
            .next()
            .unwrap()
            .dependencies
            .clear();
        let validation = validate_validation_dependency_plans(
            &missing,
            &asm.forms,
            &asm.form_fields,
            &asm.validation_rules,
            &asm.form_ownership,
            &asm.validation_graph,
        );
        assert!(validation
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.kind
                == ValidationDependencyPlanIntegrityKind::MissingDependencyProjection));

        let mut graph = asm.validation_graph.clone();
        graph.edges.retain(|edge| {
            !matches!(
                edge.kind,
                crate::ValidationGraphEdgeKind::RuleDependsOnField
            )
        });
        let blocked = collect_validation_dependency_plans(
            &asm.forms,
            &asm.form_fields,
            &asm.validation_rules,
            &asm.form_ownership,
            &graph,
        );
        assert!(blocked
            .blocked
            .iter()
            .any(|blocked| blocked.reason == FieldDependencyBlockReason::IdentityMismatch));

        let mut stale = asm.clone();
        stale.validation_dependency_plans.dependencies.clear();
        stale
            .validation_dependency_plans
            .plans
            .values_mut()
            .next()
            .unwrap()
            .dependencies
            .clear();
        assert!(validate_application_semantic_model(&stale)
            .iter()
            .any(|diagnostic| diagnostic.code == "PSASM1272"));
    }

    #[test]
    fn remains_declaration_only_deterministic_and_hidden_from_public_schema() {
        let first = presolve_parser::parse_file(
            "src/A.tsx",
            r#"@component("a-x") class A { @form() form!: Form; @field(this.form) left = ""; @validate(equals(this.left)) @field(this.form) right = ""; render() { return <div />; } }"#,
        );
        let second = presolve_parser::parse_file(
            "src/B.tsx",
            r#"@component("b-x") class B { @form() form!: Form; render() { return <div />; } }"#,
        );
        let forward =
            build_application_semantic_model_for_unit(&CompilationUnit::from_parsed_files(vec![
                first.clone(),
                second.clone(),
            ]));
        let reversed =
            build_application_semantic_model_for_unit(&CompilationUnit::from_parsed_files(vec![
                second, first,
            ]));
        assert_eq!(
            forward.validation_dependency_plans,
            reversed.validation_dependency_plans
        );
        assert!(forward
            .validation_dependency_plans
            .dependencies
            .values()
            .all(|dependency| !dependency.id.as_str().contains("form-instance")));
        let form = forward.forms.values().next().unwrap();
        let instance = FormInstanceId::for_component_instance(
            &forward
                .component_instance_plan
                .instances
                .values()
                .next()
                .unwrap()
                .id,
            &form.id,
        );
        assert!(!forward
            .validation_dependency_plans
            .plans
            .keys()
            .any(|plan| plan.as_str() == instance.as_str()));
        assert_eq!(SEMANTIC_GRAPH_SCHEMA_VERSION, 6);
        let graph: SemanticGraph = build_semantic_graph(&forward);
        let json = semantic_graph_json(&graph);
        assert!(!json.contains("validation-plan"));
        assert!(!json.contains("field-dependency"));
    }
}