mif-rh 0.6.0

Compiled ontology resolution/review engine for research-harness-template corpora
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
//! Errors from loading, resolving, reviewing, indexing, or locking a
//! research-harness-template (rht) corpus.

use mif_problem::{
    Applicability, CodeAction, ProblemDetails, ProblemMeta, SuggestedFix, ToProblem,
};

/// Errors from `mif-rh`'s engine core.
///
/// Variants split into two classes: those that produce a
/// [`crate::resolve::MapRecord`] anyway (a finding that classifies as
/// invalid/ambiguous/unresolved is still recorded, per rht's own
/// `resolve-ontology.sh` — see [`crate::resolve::resolve_finding`]) and
/// those below, which mean no record could be produced at all (the file
/// itself is unreadable, the catalog is missing, or an ontology definition
/// is broken). [`crate::review::review`] folds the latter into its
/// reconciliation "gap" count rather than aborting the whole review.
#[derive(Debug, thiserror::Error)]
pub enum MifRhError {
    /// Failed to read a finding file.
    #[error("failed to read finding {path}: {source}")]
    FindingIo {
        /// The path that failed to read.
        path: String,
        /// The underlying I/O error.
        #[source]
        source: std::io::Error,
    },
    /// A finding file was not valid JSON.
    #[error("finding {path} is not valid JSON: {source}")]
    FindingJson {
        /// The path that failed to parse.
        path: String,
        /// The underlying parse error.
        #[source]
        source: serde_json::Error,
    },
    /// A generic I/O failure reading a supporting file (ontology directory,
    /// map file, `harness.config.json`).
    #[error("failed to read {path}: {source}")]
    Io {
        /// The path that failed to read.
        path: String,
        /// The underlying I/O error.
        #[source]
        source: std::io::Error,
    },
    /// A supporting file was not valid JSON.
    #[error("failed to parse {path} as JSON: {source}")]
    Json {
        /// The path that failed to parse.
        path: String,
        /// The underlying parse error.
        #[source]
        source: serde_json::Error,
    },
    /// A value could not be serialized to JSON for an atomic write. This
    /// indicates a bug in the value's `Serialize` implementation (e.g. a
    /// non-finite float) rather than anything a caller can fix by changing
    /// its input.
    #[error("failed to serialize {path} as JSON: {source}")]
    JsonSerialize {
        /// The path the value was being written to.
        path: String,
        /// The underlying serialization error.
        #[source]
        source: serde_json::Error,
    },
    /// An ontology pack YAML file failed to parse (the direct equivalent of
    /// `yq` failing to read an ontology's `extends`/`entity_types`/full
    /// YAML — a fail-closed abort, matching rht's own bash exit code 4).
    #[error("failed to parse ontology pack {path} as YAML: {source}")]
    OntologyPackYaml {
        /// The path that failed to parse.
        path: String,
        /// The underlying parse error.
        #[source]
        source: serde_norway::Error,
    },
    /// A rendered artifact's MIF concept frontmatter could not be serialized
    /// to YAML. This indicates a bug in the concept's `Serialize`
    /// implementation (e.g. a non-finite float) rather than anything a
    /// caller can fix by changing its input.
    #[error("failed to serialize report frontmatter as YAML: {source}")]
    FrontmatterYamlSerialize {
        /// The underlying serialization error.
        #[source]
        source: serde_norway::Error,
    },
    /// The catalog file (`.claude/enabled-packs.json`) is missing.
    #[error("catalog file {path} does not exist")]
    CatalogMissing {
        /// The missing catalog path.
        path: String,
    },
    /// The config file (`harness.config.json`) is missing.
    #[error("config file {path} does not exist")]
    ConfigMissing {
        /// The missing config path.
        path: String,
    },
    /// A topic directly binds an ontology id that is not cataloged, or pins
    /// a version that does not match the cataloged one.
    #[error("topic '{topic}' binds ontology '{id}', which is not cataloged or version-mismatched")]
    DirectBindingInvalid {
        /// The topic with the invalid binding.
        topic: String,
        /// The offending ontology id.
        id: String,
    },
    /// Resolving the transitive `extends` ancestry for an allowed ontology
    /// failed (an ancestor is missing from the supplied ontology-pack
    /// directory, or the `extends` graph is cyclic).
    #[error(transparent)]
    Ontology(#[from] mif_ontology::OntologyError),
    /// Splitting a report's frontmatter/body failed (missing frontmatter,
    /// malformed YAML, or a non-mapping frontmatter document).
    #[error(transparent)]
    Frontmatter(#[from] mif_frontmatter::FrontmatterError),
    /// A report schema (or one of its `$ref` dependencies) was not valid
    /// JSON, or the `jsonschema` validator could not be compiled from it.
    #[error("failed to compile schema {path}: {detail}")]
    SchemaCompilation {
        /// The schema file that failed to compile.
        path: String,
        /// The underlying compilation error, stringified (the original
        /// `jsonschema` error type is not `'static`).
        detail: String,
    },
    /// A `$ref` dependency schema has no `$id`, so it cannot be registered
    /// for the main schema to resolve references against.
    #[error("schema {path} has no $id — it cannot be registered as a $ref target")]
    RefSchemaMissingId {
        /// The dependency schema file with no `$id`.
        path: String,
    },
    /// A projected report failed validation against its schema.
    #[error("{path} failed schema validation against {schema_path}: {detail}")]
    SchemaValidationFailed {
        /// The report file that failed validation.
        path: String,
        /// The schema it was validated against.
        schema_path: String,
        /// Every validation error, joined for display.
        detail: String,
    },
    /// A manifest toggle's value is not one of the allowed values.
    #[error("{field} must be one of {allowed} (got '{value}')")]
    InvalidToggleValue {
        /// The toggled field's name.
        field: String,
        /// The rejected value.
        value: String,
        /// The pipe-joined list of allowed values.
        allowed: String,
    },
    /// No source content was available from `--content-file`,
    /// `--content`, or stdin.
    #[error("empty content (provide --content-file, --content, or stdin)")]
    EmptySourceContent,
    /// A `pack-toggle` target is not declared in `harness.config.json`'s
    /// `packs[]` array. Distinct from [`Self::PackNotFound`] (a
    /// `bump-version --pack` target with no `packs/<family>/<name>/`
    /// directory on disk) — this is a config-declaration check, not a
    /// filesystem one.
    #[error("pack '{name}' is not declared in {path} packs[] — declare it first")]
    PackNotDeclared {
        /// The undeclared pack name.
        name: String,
        /// The manifest path checked.
        path: String,
    },
    /// A findings directory has no `*.json` files to build a graph/index
    /// from.
    #[error("no finding JSON in {path}")]
    NoFindingsFound {
        /// The empty findings directory.
        path: String,
    },
    /// Every finding in a directory is falsified — nothing to synthesize.
    #[error("no surviving findings to synthesize in {path}")]
    NoSurvivingFindings {
        /// The findings directory with no surviving findings.
        path: String,
    },
    /// A synthesized artifact has no sections, finding refs, or sources —
    /// there is nothing publishable to render.
    #[error(
        "artifact from {path} has no publishable content (no surviving findings, or no citations to cite)"
    )]
    ArtifactNotPublishable {
        /// The findings directory the artifact was synthesized from.
        path: String,
    },
    /// One or more findings in a corpus import lack a provenance block
    /// (SPEC §8a: provenance must survive an import).
    #[error(
        "{count} finding(s) lack a provenance block; import aborted (provenance must be preserved)"
    )]
    MissingProvenance {
        /// How many findings lack provenance.
        count: usize,
        /// The paths of the findings missing provenance.
        paths: Vec<String>,
    },
    /// A session reconciliation's known-good sample finding failed schema
    /// validation — the schema/toolchain itself is broken. Must never be
    /// read as "every finding is invalid" (that would re-run an entire
    /// expensive research session).
    #[error(
        "the known-good sample finding at {sample_path} failed schema validation — the schema/toolchain is broken, refusing to emit a plan"
    )]
    ReconcileEnvironmentBroken {
        /// The sample finding path that unexpectedly failed to validate.
        sample_path: String,
    },
    /// A topic README build/check was requested for a topic with no entry
    /// in `harness.config.json`'s `topics[]`.
    #[error("topic '{topic}' is not registered in {config_path}")]
    TopicNotRegistered {
        /// The unregistered topic id.
        topic: String,
        /// The manifest path checked.
        config_path: String,
    },
    /// A concordance file parsed but is not a valid graph (no `.nodes`
    /// array) — the corpus atlas is a projection of the spine, and there
    /// is nothing to project without one.
    #[error("concordance is not a valid graph (no .nodes array): {path}")]
    InvalidConcordance {
        /// The invalid concordance path.
        path: String,
    },
    /// The shippable-typing gate's `ontology-map.json` is missing or
    /// present-but-unparseable (not a JSON array of records) — the gate
    /// cannot prove any shippable finding is typed, so it fails closed
    /// rather than passing vacuously (every per-finding lookup would
    /// otherwise silently resolve to "no record").
    #[error(
        "ontology-map.json {reason} for topic '{topic}' — synthesis BLOCKED (fail closed): {path}"
    )]
    OntologyMapUnusable {
        /// The ontology-map path checked.
        path: String,
        /// The topic (derived from the reports-dir name) named in the
        /// operator unblock hint.
        topic: String,
        /// Either `"is missing"` or `"is unparseable or not a record array"`.
        reason: String,
    },
    /// A finding under `<topic>/findings/` failed to parse while building
    /// the corpus-wide active-`@id`/relationship-target universes for the
    /// relationship-targets gate. Hard-fails the whole gate (never silently
    /// dropped) — an unparseable file could otherwise hide a real dangling
    /// target elsewhere in the corpus.
    #[error("finding is not valid JSON, cannot check its relationship targets: {path}")]
    RelationshipTargetFindingUnparseable {
        /// The unparseable finding path.
        path: String,
    },
    /// A `subtype_of` chain across the loaded ontology registry revisits
    /// its own starting type — a cycle. User-authored ontology data must
    /// never drive unbounded recursion during the concordance's transitive
    /// supertype closure.
    #[error("subtype_of graph contains a cycle involving entity type '{entity_type}'")]
    SubtypeOfCycle {
        /// The entity type where the cycle was detected.
        entity_type: String,
    },
    /// Building a dynamic `jsonschema` validator for a resolved entity
    /// type's `schema` field failed. Indicates a malformed ontology pack,
    /// not a bug in the finding being validated.
    #[error("entity type '{entity_type}' has a malformed validation schema: {detail}")]
    EntityTypeSchemaInvalid {
        /// The offending entity type name.
        entity_type: String,
        /// The underlying schema compilation error, stringified (the
        /// original `jsonschema` error type is not `'static` and cannot be
        /// stored here directly).
        detail: String,
    },
    /// A `SQLite` index operation failed.
    #[error("sqlite index operation failed: {source}")]
    Index {
        /// The underlying `SQLite` error.
        #[source]
        source: rusqlite::Error,
    },
    /// Failed to open or write the exclusive review lock file.
    #[error("failed to acquire the review lock at {path}: {source}")]
    LockIo {
        /// The lock file path.
        path: String,
        /// The underlying I/O error.
        #[source]
        source: std::io::Error,
    },
    /// Another `review` run already holds the lock.
    #[error("another review is already in progress (lock held by pid {holder_pid})")]
    LockHeld {
        /// The PID recorded in the held lock file.
        holder_pid: u32,
    },
    /// A suggestion queue file on disk belongs to a different topic than
    /// the one being upserted — a copied/renamed queue file or a
    /// wrong-path caller, which must not silently mix topics' entries.
    #[error("suggestion queue at {path} belongs to topic '{found}', not '{expected}'")]
    QueueTopicMismatch {
        /// The queue file path.
        path: String,
        /// The topic the caller is upserting.
        expected: String,
        /// The topic recorded inside the queue file.
        found: String,
    },
    /// Computing an embedding failed.
    #[error(transparent)]
    Embed(#[from] mif_embed::EmbedError),
    /// Failed to fetch a file (the registry index, or a vendored ontology)
    /// from the resolved ontology source (a local directory or an http(s)
    /// base URL).
    #[error("failed to fetch from ontology source {registry_source}: {detail}")]
    RegistryFetch {
        /// The resolved source (directory path or URL base). Named
        /// `registry_source`, not `source`: `thiserror` treats a field
        /// literally named `source` as the error-chain cause and requires
        /// it to implement `std::error::Error`, which a plain `String`
        /// does not.
        registry_source: String,
        /// A human-readable failure detail.
        detail: String,
    },
    /// The registry index (`index.json`) was not valid JSON or did not
    /// match the expected shape.
    #[error("ontology registry index at {registry_source} is malformed: {detail}")]
    RegistryIndexInvalid {
        /// The resolved source the index was read from. See
        /// [`Self::RegistryFetch`]'s doc comment for why this is
        /// `registry_source`, not `source`.
        registry_source: String,
        /// A human-readable failure detail.
        detail: String,
    },
    /// A requested or `extends`-ancestor ontology id has no entry in the
    /// registry index — it has no canonical definition yet.
    #[error(
        "ontology '{id}' is not in the registry index — it has no canonical definition yet \
         (author one with the `ontology author` subcommand)"
    )]
    OntologyNotInRegistry {
        /// The unresolvable ontology id.
        id: String,
    },
    /// `ontologies.lock.json`'s pinned source differs from the source a
    /// read-only pin-safety check was asked to evaluate against — the
    /// lock's per-id version/sha256 pins were established against a
    /// different registry and are not meaningful trust anchors for this
    /// one. Unlike `fetch`, which legitimately adopts a new source on
    /// first use (and re-pins it), a read-only check never mutates the
    /// lock, so it must fail closed rather than silently compare a
    /// mismatched source's registry entries against pins it did not
    /// establish.
    #[error(
        "ontologies.lock.json is pinned to source '{lock_source}', not '{requested_source}' — \
         refusing to evaluate pin safety against a different registry than the one the lock \
         trusts"
    )]
    LockSourceMismatch {
        /// The source recorded in the lock file.
        lock_source: String,
        /// The source the check was asked to evaluate against.
        requested_source: String,
    },
    /// A checksum-verified, freshly-fetched ontology pack's bytes were not
    /// valid UTF-8 — refusing to lossily convert content whose exact bytes
    /// were already pinned by checksum. A lossy conversion would silently
    /// substitute replacement characters, meaning the diffed schema would
    /// not be the schema that was actually hashed and fetched.
    #[error("ontology '{id}' file '{file}' is not valid UTF-8")]
    OntologyPackNotUtf8 {
        /// The ontology id being diffed.
        id: String,
        /// The index's declared file name.
        file: String,
    },
    /// The registry index's sha256 no longer matches the value pinned in
    /// `ontologies.lock.json` for the same source (trust-on-first-use, then
    /// pin) — the trust root moved.
    #[error(
        "registry index sha256 changed from the pinned value for source {registry_source} \
         (pinned {pinned}, got {got}) — refusing to trust a moved index (clear index_sha256 \
         in the lock to re-pin deliberately)"
    )]
    IndexPinMismatch {
        /// The source whose index changed. See [`Self::RegistryFetch`]'s
        /// doc comment for why this is `registry_source`, not `source`.
        registry_source: String,
        /// The previously pinned index sha256.
        pinned: String,
        /// The newly fetched index's sha256.
        got: String,
    },
    /// A fetched ontology file's sha256 did not match the registry index's
    /// pinned value — refusing to vendor a file that does not match the
    /// trusted hash (fail-closed).
    #[error("checksum mismatch for ontology '{id}' ({file}): expected {expected}, got {got}")]
    ChecksumMismatch {
        /// The ontology id being vendored.
        id: String,
        /// The index's declared file name.
        file: String,
        /// The expected (pinned) sha256.
        expected: String,
        /// The sha256 actually computed from the fetched bytes.
        got: String,
    },
    /// The registry index named an unsafe (non-bare) file path for an
    /// ontology — a poisoned index could otherwise escape the vendored
    /// packs directory.
    #[error(
        "registry index entry for '{id}' has an unsafe file path: '{file}' (must be a bare \
         filename)"
    )]
    UnsafeIndexPath {
        /// The ontology id whose index entry is unsafe.
        id: String,
        /// The offending file path.
        file: String,
    },
    /// A registry-discovered ontology id is not a bare, lowercase slug — a
    /// poisoned or malformed index entry that could otherwise escape its
    /// intended vendored directory once written into `harness.config.json`.
    #[error("registry index declares a malformed ontology id: '{id}' (refusing, fail-closed)")]
    MalformedOntologyId {
        /// The malformed id.
        id: String,
    },
    /// `harness.config.json`'s `.ontologies` field exists but is not an
    /// array — refusing to guess how to append a discovered ontology to it.
    #[error("{path}'s .ontologies is not an array: {detail}")]
    ConfigMalformed {
        /// The config path.
        path: String,
        /// A human-readable failure detail.
        detail: String,
    },
    /// A topic's `ontology-map.json` carries no typed entity types to
    /// mine an ontology draft from.
    #[error(
        "no entity types found in reports/{topic}/ontology-map.json — nothing to draft an \
         ontology from"
    )]
    NoEntityTypesFound {
        /// The topic whose map carried no typed entities.
        topic: String,
    },
    /// An `expansion-candidates` output file carries no clusters to draft
    /// candidate types from.
    #[error("no clusters in {path} — nothing to draft an ontology from")]
    NoClustersFound {
        /// The clusters file with no clusters.
        path: String,
    },
    /// A version string is not well-formed `X.Y.Z` semver.
    #[error("version is not well-formed semver: {value}")]
    VersionNotSemver {
        /// The offending value.
        value: String,
    },
    /// A file expected to carry a `.version` field has none.
    #[error("{path} has no .version")]
    VersionMissing {
        /// The file with no version.
        path: String,
    },
    /// The requested new version equals the current one.
    #[error("new version equals current ({value}); nothing to bump")]
    VersionUnchanged {
        /// The unchanged value.
        value: String,
    },
    /// A `--pack` component name resolves to no directory under
    /// `packs/<family>/`.
    #[error(
        "pack '{name}' not found under packs/<family>/ (ontology packs version independently and are not bumpable here)"
    )]
    PackNotFound {
        /// The unresolved component name.
        name: String,
    },
    /// A `--pack` component name resolves to more than one directory.
    #[error("pack '{name}' is ambiguous: more than one packs/<family>/{name} directory exists")]
    PackAmbiguous {
        /// The ambiguous component name.
        name: String,
    },
    /// A pack is missing a file `bump_version` needs (its `plugin.json`,
    /// `SKILL.md`, or family doc section/row).
    #[error("pack '{name}': missing or malformed {path}")]
    PackFileMissing {
        /// The pack's component name.
        name: String,
        /// The missing or malformed file/section.
        path: String,
    },
    /// A pack's declared version is not well-formed semver.
    #[error("pack '{name}' {path} has no valid semver .version (got '{value}')")]
    PackVersionInvalid {
        /// The pack's component name.
        name: String,
        /// The pack's `plugin.json` path.
        path: String,
        /// The malformed value found.
        value: String,
    },
    /// A pack's current version is already ahead of the release being cut —
    /// bumping it would move it backward.
    #[error(
        "pack '{name}' is at {pack_version}, ahead of the new release {new_version} — refusing to move it backward"
    )]
    PackAheadOfRelease {
        /// The pack's component name.
        name: String,
        /// The pack's current version.
        pack_version: String,
        /// The release version being cut.
        new_version: String,
    },
    /// `CHANGELOG.md` has neither an `## [Unreleased]` anchor to insert a
    /// new section under, nor an existing section for the new version.
    #[error(
        "{path} has no '## [Unreleased]' anchor to insert the new version under (nor an existing section for it)"
    )]
    ChangelogAnchorMissing {
        /// The CHANGELOG path.
        path: String,
    },
    /// A post-write self-verification found a file that did not update to
    /// the expected new value.
    #[error("verification failed: {path} was not updated as expected")]
    VerificationFailed {
        /// The file that failed verification.
        path: String,
    },
}

impl MifRhError {
    // One arm per error variant, each a flat struct literal — length is
    // inherent to the variant count, not a complexity signal.
    #[allow(clippy::too_many_lines)]
    const fn meta(&self) -> ProblemMeta {
        match self {
            Self::FindingIo { .. } => ProblemMeta {
                slug: "finding-io",
                version: "v1",
                title: "Failed to read a finding file",
                status: 500,
                exit_code: 2,
            },
            Self::FindingJson { .. } => ProblemMeta {
                slug: "finding-invalid-json",
                version: "v1",
                title: "Finding file is not valid JSON",
                status: 400,
                exit_code: 2,
            },
            Self::Io { .. } => ProblemMeta {
                slug: "mif-rh-io",
                version: "v1",
                title: "Failed to read a supporting file",
                status: 500,
                exit_code: 1,
            },
            Self::Json { .. } => ProblemMeta {
                slug: "mif-rh-invalid-json",
                version: "v1",
                title: "Supporting file is not valid JSON",
                status: 400,
                exit_code: 1,
            },
            Self::JsonSerialize { .. } => ProblemMeta {
                slug: "json-serialize-failure",
                version: "v1",
                title: "A value could not be serialized to JSON",
                status: 500,
                exit_code: 1,
            },
            Self::OntologyPackYaml { .. } => ProblemMeta {
                slug: "ontology-pack-invalid-yaml",
                version: "v1",
                title: "Ontology pack YAML failed to parse",
                status: 422,
                exit_code: 4,
            },
            Self::FrontmatterYamlSerialize { .. } => ProblemMeta {
                slug: "frontmatter-yaml-serialize-failure",
                version: "v1",
                title: "Report frontmatter could not be serialized to YAML",
                status: 500,
                exit_code: 1,
            },
            Self::CatalogMissing { .. } => ProblemMeta {
                slug: "catalog-missing",
                version: "v1",
                title: "Ontology catalog file does not exist",
                status: 404,
                exit_code: 3,
            },
            Self::ConfigMissing { .. } => ProblemMeta {
                slug: "config-missing",
                version: "v1",
                title: "Harness config file does not exist",
                status: 404,
                exit_code: 2,
            },
            Self::DirectBindingInvalid { .. } => ProblemMeta {
                slug: "direct-binding-invalid",
                version: "v1",
                title: "Topic binds an uncataloged or version-mismatched ontology",
                status: 422,
                exit_code: 1,
            },
            Self::Ontology(_) => ProblemMeta {
                slug: "delegated-ontology",
                version: "v1",
                title: "Delegated ontology error",
                status: 500,
                exit_code: 4,
            },
            Self::Frontmatter(_) => ProblemMeta {
                slug: "delegated-frontmatter",
                version: "v1",
                title: "Delegated frontmatter error",
                status: 500,
                exit_code: 4,
            },
            Self::SchemaCompilation { .. } => ProblemMeta {
                slug: "schema-compilation-failed",
                version: "v1",
                title: "Report schema failed to compile",
                status: 422,
                exit_code: 2,
            },
            Self::RefSchemaMissingId { .. } => ProblemMeta {
                slug: "ref-schema-missing-id",
                version: "v1",
                title: "Ref-target schema has no $id",
                status: 422,
                exit_code: 2,
            },
            Self::SchemaValidationFailed { .. } => ProblemMeta {
                slug: "schema-validation-failed",
                version: "v1",
                title: "Report failed schema validation",
                status: 422,
                exit_code: 1,
            },
            Self::InvalidToggleValue { .. } => ProblemMeta {
                slug: "invalid-toggle-value",
                version: "v1",
                title: "Manifest toggle value is not one of the allowed values",
                status: 422,
                exit_code: 2,
            },
            Self::EmptySourceContent => ProblemMeta {
                slug: "empty-source-content",
                version: "v1",
                title: "No source content was available from any input",
                status: 422,
                exit_code: 2,
            },
            Self::PackNotDeclared { .. } => ProblemMeta {
                slug: "pack-not-declared",
                version: "v1",
                title: "Pack is not declared in the harness manifest",
                status: 404,
                exit_code: 2,
            },
            Self::NoFindingsFound { .. } => ProblemMeta {
                slug: "no-findings-found",
                version: "v1",
                title: "Findings directory has no finding JSON",
                status: 404,
                exit_code: 2,
            },
            Self::NoSurvivingFindings { .. } => ProblemMeta {
                slug: "no-surviving-findings",
                version: "v1",
                title: "Every finding is falsified — nothing to synthesize",
                status: 422,
                exit_code: 1,
            },
            Self::ArtifactNotPublishable { .. } => ProblemMeta {
                slug: "artifact-not-publishable",
                version: "v1",
                title: "Synthesized artifact has no publishable content",
                status: 422,
                exit_code: 1,
            },
            Self::MissingProvenance { .. } => ProblemMeta {
                slug: "missing-provenance",
                version: "v1",
                title: "Corpus import contains findings with no provenance block",
                status: 422,
                exit_code: 1,
            },
            Self::ReconcileEnvironmentBroken { .. } => ProblemMeta {
                slug: "reconcile-environment-broken",
                version: "v1",
                title: "Known-good sample finding failed schema validation",
                status: 500,
                exit_code: 3,
            },
            Self::TopicNotRegistered { .. } => ProblemMeta {
                slug: "topic-not-registered",
                version: "v1",
                title: "Topic is not registered in harness.config.json",
                status: 422,
                exit_code: 2,
            },
            Self::InvalidConcordance { .. } => ProblemMeta {
                slug: "invalid-concordance",
                version: "v1",
                title: "Concordance is not a valid graph",
                status: 422,
                exit_code: 2,
            },
            Self::OntologyMapUnusable { .. } => ProblemMeta {
                slug: "ontology-map-unusable",
                version: "v1",
                title: "ontology-map.json is missing or unparseable — cannot prove typing",
                status: 422,
                exit_code: 3,
            },
            Self::RelationshipTargetFindingUnparseable { .. } => ProblemMeta {
                slug: "relationship-target-finding-unparseable",
                version: "v1",
                title: "A finding failed to parse while checking relationship targets",
                status: 400,
                exit_code: 2,
            },
            Self::SubtypeOfCycle { .. } => ProblemMeta {
                slug: "subtype-of-cycle",
                version: "v1",
                title: "subtype_of graph contains a cycle",
                status: 422,
                exit_code: 4,
            },
            Self::EntityTypeSchemaInvalid { .. } => ProblemMeta {
                slug: "entity-type-schema-invalid",
                version: "v1",
                title: "Entity type validation schema is malformed",
                status: 422,
                exit_code: 4,
            },
            Self::Index { .. } => ProblemMeta {
                slug: "index-failure",
                version: "v1",
                title: "A SQLite index operation failed",
                status: 500,
                exit_code: 1,
            },
            Self::LockIo { .. } => ProblemMeta {
                slug: "lock-io",
                version: "v1",
                title: "Failed to acquire the review lock file",
                status: 500,
                exit_code: 2,
            },
            Self::LockHeld { .. } => ProblemMeta {
                slug: "lock-held",
                version: "v1",
                title: "Another review is already in progress",
                status: 409,
                exit_code: 2,
            },
            Self::QueueTopicMismatch { .. } => ProblemMeta {
                slug: "queue-topic-mismatch",
                version: "v1",
                title: "Suggestion queue belongs to a different topic",
                status: 409,
                exit_code: 2,
            },
            Self::Embed(_) => ProblemMeta {
                slug: "delegated-embed",
                version: "v1",
                title: "Delegated embedding error",
                status: 500,
                exit_code: 1,
            },
            Self::RegistryFetch { .. } => ProblemMeta {
                slug: "registry-fetch-failed",
                version: "v1",
                title: "Failed to fetch from the ontology registry source",
                status: 502,
                exit_code: 1,
            },
            Self::RegistryIndexInvalid { .. } => ProblemMeta {
                slug: "registry-index-invalid",
                version: "v1",
                title: "Ontology registry index is malformed",
                status: 502,
                exit_code: 1,
            },
            Self::OntologyNotInRegistry { .. } => ProblemMeta {
                slug: "ontology-not-in-registry",
                version: "v1",
                title: "Ontology id has no registry index entry",
                status: 404,
                exit_code: 1,
            },
            Self::LockSourceMismatch { .. } => ProblemMeta {
                slug: "lock-source-mismatch",
                version: "v1",
                title: "Lock file is pinned to a different registry source",
                status: 409,
                exit_code: 1,
            },
            Self::OntologyPackNotUtf8 { .. } => ProblemMeta {
                slug: "ontology-pack-not-utf8",
                version: "v1",
                title: "Fetched ontology pack is not valid UTF-8",
                status: 422,
                exit_code: 1,
            },
            Self::IndexPinMismatch { .. } => ProblemMeta {
                slug: "index-pin-mismatch",
                version: "v1",
                title: "Registry index sha256 no longer matches the pinned value",
                status: 409,
                exit_code: 1,
            },
            Self::ChecksumMismatch { .. } => ProblemMeta {
                slug: "ontology-checksum-mismatch",
                version: "v1",
                title: "Fetched ontology file does not match its pinned sha256",
                status: 422,
                exit_code: 1,
            },
            Self::UnsafeIndexPath { .. } => ProblemMeta {
                slug: "unsafe-index-path",
                version: "v1",
                title: "Registry index names an unsafe file path",
                status: 422,
                exit_code: 1,
            },
            Self::MalformedOntologyId { .. } => ProblemMeta {
                slug: "malformed-ontology-id",
                version: "v1",
                title: "Registry index declares a malformed ontology id",
                status: 422,
                exit_code: 1,
            },
            Self::ConfigMalformed { .. } => ProblemMeta {
                slug: "config-malformed",
                version: "v1",
                title: "Harness config's .ontologies field is not an array",
                status: 422,
                exit_code: 1,
            },
            Self::NoEntityTypesFound { .. } => ProblemMeta {
                slug: "no-entity-types-found",
                version: "v1",
                title: "Topic's ontology map carries no typed entities to draft from",
                status: 422,
                exit_code: 1,
            },
            Self::NoClustersFound { .. } => ProblemMeta {
                slug: "no-clusters-found",
                version: "v1",
                title: "Expansion-candidates file carries no clusters to draft from",
                status: 422,
                exit_code: 1,
            },
            Self::VersionNotSemver { .. } => ProblemMeta {
                slug: "version-not-semver",
                version: "v1",
                title: "Version is not well-formed semver",
                status: 422,
                exit_code: 2,
            },
            Self::VersionMissing { .. } => ProblemMeta {
                slug: "version-missing",
                version: "v1",
                title: "File has no .version field",
                status: 422,
                exit_code: 2,
            },
            Self::VersionUnchanged { .. } => ProblemMeta {
                slug: "version-unchanged",
                version: "v1",
                title: "New version equals the current version",
                status: 422,
                exit_code: 2,
            },
            Self::PackNotFound { .. } => ProblemMeta {
                slug: "pack-not-found",
                version: "v1",
                title: "Pack component not found",
                status: 404,
                exit_code: 2,
            },
            Self::PackAmbiguous { .. } => ProblemMeta {
                slug: "pack-ambiguous",
                version: "v1",
                title: "Pack component name is ambiguous",
                status: 422,
                exit_code: 2,
            },
            Self::PackFileMissing { .. } => ProblemMeta {
                slug: "pack-file-missing",
                version: "v1",
                title: "Pack is missing a required file or section",
                status: 422,
                exit_code: 2,
            },
            Self::PackVersionInvalid { .. } => ProblemMeta {
                slug: "pack-version-invalid",
                version: "v1",
                title: "Pack's declared version is not well-formed semver",
                status: 422,
                exit_code: 2,
            },
            Self::PackAheadOfRelease { .. } => ProblemMeta {
                slug: "pack-ahead-of-release",
                version: "v1",
                title: "Pack's current version is ahead of the release being cut",
                status: 409,
                exit_code: 2,
            },
            Self::ChangelogAnchorMissing { .. } => ProblemMeta {
                slug: "changelog-anchor-missing",
                version: "v1",
                title: "CHANGELOG has no anchor to insert the new version under",
                status: 422,
                exit_code: 2,
            },
            Self::VerificationFailed { .. } => ProblemMeta {
                slug: "verification-failed",
                version: "v1",
                title: "Post-write self-verification found an unexpected file",
                status: 500,
                exit_code: 2,
            },
        }
    }
}

/// The `(suggested_fix, code_action)` pair for every variant that carries
/// its own static remediation text (everything except the delegated
/// `Ontology`/`Embed` variants and the IO-classified variants, which
/// `to_problem` handles separately).
// One arm per error variant, each a flat struct literal — length is
// inherent to the variant count, not a complexity signal.
#[allow(clippy::too_many_lines)]
fn fix_and_action(error: &MifRhError) -> (SuggestedFix, CodeAction) {
    match error {
        MifRhError::OntologyPackYaml { .. } => (
            SuggestedFix::new(
                "Fix the YAML syntax error in the ontology pack, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the malformed ontology pack YAML",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::CatalogMissing { .. } => (
            SuggestedFix::new(
                "Run rht's scripts/sync-packs.sh (or mif-rh-cli's equivalent) to generate the \
                 catalog, then retry.",
                Applicability::MachineApplicable,
            ),
            CodeAction::new(
                "Generate the missing catalog",
                "quickfix",
                Applicability::MachineApplicable,
            ),
        ),
        MifRhError::ConfigMissing { .. } => (
            SuggestedFix::new(
                "Supply the correct --config path to harness.config.json, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Correct the --config path",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::DirectBindingInvalid { .. } => (
            SuggestedFix::new(
                "Catalog the missing ontology, correct its pinned version, or remove the \
                 invalid topic binding, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the topic's ontology binding",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::EntityTypeSchemaInvalid { .. } => (
            SuggestedFix::new(
                "Fix the entity type's schema field in the ontology pack, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the entity type's schema",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::Index { .. } => (
            SuggestedFix::new(
                "This indicates a corrupt or inaccessible index database. Delete it and \
                 rebuild with `mif-rh-cli review --build-index`, then retry.",
                Applicability::Unspecified,
            ),
            CodeAction::new("Rebuild the index", "quickfix", Applicability::Unspecified),
        ),
        MifRhError::LockHeld { .. } => (
            SuggestedFix::new(
                "Wait for the in-progress review to finish, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new("Wait and retry", "quickfix", Applicability::MaybeIncorrect),
        ),
        MifRhError::QueueTopicMismatch { .. } => (
            SuggestedFix::new(
                "Point the upsert at reports/_meta/suggestions/<topic>.json for the topic \
                 being reviewed, or remove the stray queue file that was copied or renamed \
                 across topics.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Use the topic's own suggestion queue path",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::JsonSerialize { .. } => (
            SuggestedFix::new(
                "This indicates a bug in mif-rh: a value could not be serialized to JSON. \
                 Report it upstream with the record that triggered it; no caller-side fix \
                 exists.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Report the serialization bug",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        MifRhError::FrontmatterYamlSerialize { .. } => (
            SuggestedFix::new(
                "This indicates a bug in mif-rh: a report's concept frontmatter could not be \
                 serialized to YAML. Report it upstream with the artifact that triggered it; \
                 no caller-side fix exists.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Report the serialization bug",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        // FindingJson/Json carry no additional remediation beyond the
        // error message itself; the caller (`to_problem`) never invokes
        // this helper for the delegated or IO-classified variants.
        MifRhError::FindingJson { .. } | MifRhError::Json { .. } => (
            SuggestedFix::new(
                "Correct the file so it is valid JSON, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the JSON syntax error",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::RegistryFetch { .. } => (
            SuggestedFix::new(
                "Check network access to the ontology source (or, for a local directory \
                 source, that the path exists), then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Check the ontology source is reachable",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::RegistryIndexInvalid { .. } => (
            SuggestedFix::new(
                "The registry source's index.json is not valid — fix it upstream, or point \
                 MIF_ONTOLOGY_SOURCE at a known-good mirror, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix or repoint the registry index",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::OntologyNotInRegistry { .. } => (
            SuggestedFix::new(
                "Author the ontology from your research and contribute it upstream with the \
                 `ontology author` subcommand, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Author the missing ontology",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::LockSourceMismatch { .. } => (
            SuggestedFix::new(
                "Re-run against the source ontologies.lock.json is actually pinned to, or \
                 delete the lock to intentionally re-pin against the new source.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Match the requested source to the pinned lock source",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        MifRhError::OntologyPackNotUtf8 { .. } => (
            SuggestedFix::new(
                "The registry-published ontology pack file is corrupt or was published with \
                 non-UTF-8 content; investigate upstream and re-publish a valid pack.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Investigate the corrupt registry pack",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        MifRhError::IndexPinMismatch { .. } => (
            SuggestedFix::new(
                "Confirm the registry source is trustworthy, then clear index_sha256 in \
                 ontologies.lock.json to re-pin deliberately, or investigate why the trust \
                 root moved.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Investigate the moved trust root",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        MifRhError::ChecksumMismatch { .. } => (
            SuggestedFix::new(
                "The fetched ontology does not match the pinned registry hash. Do not vendor \
                 it; investigate the registry source for tampering or corruption.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Investigate the checksum mismatch",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        MifRhError::NoEntityTypesFound { .. } => (
            SuggestedFix::new(
                "Run /ontology-review on the topic first so its findings get typed, then \
                 retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Review the topic before authoring",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::NoClustersFound { .. } => (
            SuggestedFix::new(
                "Run `mif-rh-cli ontology expansion-candidates` again once more tier-3 misses \
                 have recurred across runs, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Wait for recurring misses before authoring",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::VersionNotSemver { .. } | MifRhError::PackVersionInvalid { .. } => (
            SuggestedFix::new(
                "Correct the version to well-formed X.Y.Z semver, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the malformed version",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::VersionMissing { .. } => (
            SuggestedFix::new(
                "Add a .version field to the file, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Add the missing .version field",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::VersionUnchanged { .. } => (
            SuggestedFix::new(
                "Pass a different version or bump keyword — the requested version matches the \
                 current one, so there is nothing to bump.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Choose a different version",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::PackNotFound { .. } | MifRhError::PackAmbiguous { .. } => (
            SuggestedFix::new(
                "Check the component name against packs/<family>/<name>/ and retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Correct the pack component name",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::PackFileMissing { .. } => (
            SuggestedFix::new(
                "Add the missing file or section the pack's own conventions require \
                 (plugin.json .version, SKILL.md version: frontmatter, or the family doc's \
                 **Version:** row), then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Add the missing pack file/section",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::PackAheadOfRelease { .. } => (
            SuggestedFix::new(
                "Cut a release at or above the pack's current version, or leave this pack out \
                 of this bump.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Reconcile the release version with the pack's version",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::ChangelogAnchorMissing { .. } => (
            SuggestedFix::new(
                "Add an '## [Unreleased]' section to the CHANGELOG, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Add the Unreleased anchor",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::VerificationFailed { .. } => (
            SuggestedFix::new(
                "Inspect the named file directly — the write may have partially applied. This \
                 indicates a bug in the bump logic, not a caller-side fix.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Inspect the file that failed verification",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        MifRhError::ConfigMalformed { .. } => (
            SuggestedFix::new(
                "Fix harness.config.json so its .ontologies field is an array, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the .ontologies field's shape",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::UnsafeIndexPath { .. } | MifRhError::MalformedOntologyId { .. } => (
            SuggestedFix::new(
                "The registry index entry is malformed or unsafe — fix it upstream in the \
                 canonical registry before retrying.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Fix the malformed registry entry",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        MifRhError::SchemaCompilation { .. } => (
            SuggestedFix::new(
                "Fix the schema (or its $ref dependency) so it is valid JSON Schema, then \
                 retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the malformed schema",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::RefSchemaMissingId { .. } => (
            SuggestedFix::new(
                "Add a $id to the dependency schema, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Add the missing $id",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::SchemaValidationFailed { .. } => (
            SuggestedFix::new(
                "Fix the report to conform to its schema (see the listed validation errors), \
                 then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the schema violations",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::InvalidToggleValue { .. } => (
            SuggestedFix::new(
                "Pass one of the allowed values, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Correct the toggle value",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::EmptySourceContent => (
            SuggestedFix::new(
                "Provide content via --content-file, --content, or stdin.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Provide source content",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::PackNotDeclared { .. } => (
            SuggestedFix::new(
                "Declare the pack in the manifest's packs[] array, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Declare the pack first",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::NoFindingsFound { .. } => (
            SuggestedFix::new(
                "Point at a directory containing finding JSON files, then retry.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Point at a non-empty findings directory",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::NoSurvivingFindings { .. } => (
            SuggestedFix::new(
                "Nothing to do until at least one finding survives falsification.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Wait for a surviving finding",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::ArtifactNotPublishable { .. } => (
            SuggestedFix::new(
                "Ensure at least one surviving finding carries a citation before synthesizing.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Add a citation to a surviving finding",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::MissingProvenance { .. } => (
            SuggestedFix::new(
                "Add a provenance block to every listed finding before retrying the import.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Add the missing provenance blocks",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::ReconcileEnvironmentBroken { .. } => (
            SuggestedFix::new(
                "Check the schema/$ref files and the jsonschema toolchain — a known-good \
                 sample should always validate.",
                Applicability::Unspecified,
            ),
            CodeAction::new(
                "Diagnose the schema toolchain",
                "quickfix",
                Applicability::Unspecified,
            ),
        ),
        MifRhError::TopicNotRegistered { .. } => (
            SuggestedFix::new(
                "Register the topic in harness.config.json's topics[] before building its README.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Register the topic",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::InvalidConcordance { .. } => (
            SuggestedFix::new(
                "Build the concordance first (scripts/build-concordance.sh) before synthesizing the corpus atlas.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Build the concordance",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::OntologyMapUnusable { topic, .. } => (
            SuggestedFix::new(
                format!(
                    "Regenerate the ontology map: /ontology-review --topic {topic} --enrich, then /resume --topic {topic}."
                ),
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Regenerate the ontology map",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::RelationshipTargetFindingUnparseable { .. } => (
            SuggestedFix::new(
                "Fix the invalid JSON in this finding before re-running the relationship-targets gate.",
                Applicability::MachineApplicable,
            ),
            CodeAction::new(
                "Fix the malformed finding JSON",
                "quickfix",
                Applicability::MachineApplicable,
            ),
        ),
        MifRhError::SubtypeOfCycle { .. } => (
            SuggestedFix::new(
                "Break the subtype_of cycle in the ontology registry — a type cannot (transitively) subtype itself.",
                Applicability::MaybeIncorrect,
            ),
            CodeAction::new(
                "Fix the subtype_of cycle",
                "quickfix",
                Applicability::MaybeIncorrect,
            ),
        ),
        MifRhError::FindingIo { .. }
        | MifRhError::Io { .. }
        | MifRhError::LockIo { .. }
        | MifRhError::Ontology(_)
        | MifRhError::Frontmatter(_)
        | MifRhError::Embed(_) => unreachable!(
            "to_problem handles the IO-classified and delegated variants before calling \
             fix_and_action"
        ),
    }
}

impl ToProblem for MifRhError {
    fn to_problem(&self) -> ProblemDetails {
        match self {
            Self::Ontology(inner) => inner.to_problem(),
            Self::Frontmatter(inner) => inner.to_problem(),
            Self::Embed(inner) => inner.to_problem(),
            Self::FindingIo { source, .. }
            | Self::Io { source, .. }
            | Self::LockIo { source, .. } => {
                let (status, fix, action) = mif_problem::classify_io_error(source);
                let mut problem = self
                    .meta()
                    .into_details(env!("CARGO_PKG_NAME"), self.to_string());
                problem.status = status;
                problem.with_suggested_fix(fix).with_code_action(action)
            },
            _ => {
                let (fix, action) = fix_and_action(self);
                self.meta()
                    .into_details(env!("CARGO_PKG_NAME"), self.to_string())
                    .with_suggested_fix(fix)
                    .with_code_action(action)
            },
        }
    }
}

impl From<rusqlite::Error> for MifRhError {
    fn from(source: rusqlite::Error) -> Self {
        Self::Index { source }
    }
}

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

    fn io_error() -> std::io::Error {
        std::io::Error::new(std::io::ErrorKind::NotFound, "not found")
    }

    fn json_error() -> serde_json::Error {
        serde_json::from_str::<serde_json::Value>("not json").unwrap_err()
    }

    fn yaml_error() -> serde_norway::Error {
        serde_norway::from_str::<serde_json::Value>("- a\n  bad: [unterminated").unwrap_err()
    }

    // One entry per error variant, each a flat struct literal — length is
    // inherent to the variant count, not a complexity signal.
    #[allow(clippy::too_many_lines)]
    fn every_variant() -> Vec<MifRhError> {
        vec![
            MifRhError::FindingIo {
                path: "f.json".to_string(),
                source: io_error(),
            },
            MifRhError::FindingJson {
                path: "f.json".to_string(),
                source: json_error(),
            },
            MifRhError::Io {
                path: "x".to_string(),
                source: io_error(),
            },
            MifRhError::Json {
                path: "x.json".to_string(),
                source: json_error(),
            },
            MifRhError::JsonSerialize {
                path: "x.json".to_string(),
                source: json_error(),
            },
            MifRhError::OntologyPackYaml {
                path: "x.yaml".to_string(),
                source: yaml_error(),
            },
            MifRhError::FrontmatterYamlSerialize {
                source: yaml_error(),
            },
            MifRhError::CatalogMissing {
                path: "catalog.json".to_string(),
            },
            MifRhError::ConfigMissing {
                path: "config.json".to_string(),
            },
            MifRhError::DirectBindingInvalid {
                topic: "t".to_string(),
                id: "o".to_string(),
            },
            MifRhError::Ontology(mif_ontology::OntologyError::Io {
                path: "onto.yaml".to_string(),
                source: io_error(),
            }),
            MifRhError::EntityTypeSchemaInvalid {
                entity_type: "widget".to_string(),
                detail: "bad schema".to_string(),
            },
            MifRhError::Index {
                source: rusqlite::Error::InvalidParameterName("p".to_string()),
            },
            MifRhError::LockIo {
                path: "lock".to_string(),
                source: io_error(),
            },
            MifRhError::LockHeld { holder_pid: 1234 },
            MifRhError::QueueTopicMismatch {
                path: "reports/_meta/suggestions/edu.json".to_string(),
                expected: "edu".to_string(),
                found: "sec".to_string(),
            },
            MifRhError::Embed(mif_embed::EmbedError::NoCacheDir {
                model: "test-model",
            }),
            MifRhError::RegistryFetch {
                registry_source: "https://example.test/ontologies".to_string(),
                detail: "connection refused".to_string(),
            },
            MifRhError::RegistryIndexInvalid {
                registry_source: "https://example.test/ontologies".to_string(),
                detail: "no .ontologies key".to_string(),
            },
            MifRhError::OntologyNotInRegistry {
                id: "clinical-trials".to_string(),
            },
            MifRhError::LockSourceMismatch {
                lock_source: "https://example.test/ontologies".to_string(),
                requested_source: "https://other.test/ontologies".to_string(),
            },
            MifRhError::OntologyPackNotUtf8 {
                id: "edu-fixture".to_string(),
                file: "edu-fixture.ontology.yaml".to_string(),
            },
            MifRhError::IndexPinMismatch {
                registry_source: "https://example.test/ontologies".to_string(),
                pinned: "aaa".to_string(),
                got: "bbb".to_string(),
            },
            MifRhError::ChecksumMismatch {
                id: "edu-fixture".to_string(),
                file: "edu-fixture.ontology.yaml".to_string(),
                expected: "aaa".to_string(),
                got: "bbb".to_string(),
            },
            MifRhError::UnsafeIndexPath {
                id: "edu-fixture".to_string(),
                file: "../../etc/passwd".to_string(),
            },
            MifRhError::MalformedOntologyId {
                id: "../etc".to_string(),
            },
            MifRhError::ConfigMalformed {
                path: "harness.config.json".to_string(),
                detail: ".ontologies is a string, not an array".to_string(),
            },
            MifRhError::NoEntityTypesFound {
                topic: "edu".to_string(),
            },
            MifRhError::NoClustersFound {
                path: "clusters.json".to_string(),
            },
            MifRhError::VersionNotSemver {
                value: "1.0".to_string(),
            },
            MifRhError::VersionMissing {
                path: "harness.config.json".to_string(),
            },
            MifRhError::VersionUnchanged {
                value: "1.0.0".to_string(),
            },
            MifRhError::PackNotFound {
                name: "pdf".to_string(),
            },
            MifRhError::PackAmbiguous {
                name: "pdf".to_string(),
            },
            MifRhError::PackFileMissing {
                name: "pdf".to_string(),
                path: "packs/x/pdf/.claude-plugin/plugin.json".to_string(),
            },
            MifRhError::PackVersionInvalid {
                name: "pdf".to_string(),
                path: "packs/x/pdf/.claude-plugin/plugin.json".to_string(),
                value: "not-semver".to_string(),
            },
            MifRhError::PackAheadOfRelease {
                name: "pdf".to_string(),
                pack_version: "2.0.0".to_string(),
                new_version: "1.5.0".to_string(),
            },
            MifRhError::ChangelogAnchorMissing {
                path: "CHANGELOG.md".to_string(),
            },
            MifRhError::VerificationFailed {
                path: "harness.config.json".to_string(),
            },
            MifRhError::Frontmatter(mif_frontmatter::FrontmatterError::MissingFrontmatter),
            MifRhError::SchemaCompilation {
                path: "findings.schema.json".to_string(),
                detail: "not valid JSON".to_string(),
            },
            MifRhError::RefSchemaMissingId {
                path: "entity-reference.schema.json".to_string(),
            },
            MifRhError::SchemaValidationFailed {
                path: "reports/x/findings/1.md".to_string(),
                schema_path: "schemas/findings.schema.json".to_string(),
                detail: "missing required field 'title'".to_string(),
            },
            MifRhError::InvalidToggleValue {
                field: "primarySurface".to_string(),
                value: "bogus".to_string(),
                allowed: "reports|docs|auto".to_string(),
            },
            MifRhError::EmptySourceContent,
            MifRhError::PackNotDeclared {
                name: "pdf".to_string(),
                path: "harness.config.json".to_string(),
            },
            MifRhError::NoFindingsFound {
                path: "reports/x/findings".to_string(),
            },
            MifRhError::NoSurvivingFindings {
                path: "reports/x/findings".to_string(),
            },
            MifRhError::ArtifactNotPublishable {
                path: "reports/x/findings".to_string(),
            },
            MifRhError::MissingProvenance {
                count: 1,
                paths: vec!["src/findings/f1.json".to_string()],
            },
            MifRhError::ReconcileEnvironmentBroken {
                sample_path: "schemas/samples/finding.sample.json".to_string(),
            },
            MifRhError::TopicNotRegistered {
                topic: "x".to_string(),
                config_path: "harness.config.json".to_string(),
            },
            MifRhError::InvalidConcordance {
                path: "reports/concordance.json".to_string(),
            },
            MifRhError::OntologyMapUnusable {
                path: "reports/edu/ontology-map.json".to_string(),
                topic: "edu".to_string(),
                reason: "is missing".to_string(),
            },
            MifRhError::RelationshipTargetFindingUnparseable {
                path: "reports/edu/findings/bad.json".to_string(),
            },
            MifRhError::SubtypeOfCycle {
                entity_type: "a".to_string(),
            },
        ]
    }

    #[test]
    fn every_variant_produces_a_distinct_problem_type() {
        use mif_problem::ToProblem;

        let problem_types: Vec<String> = every_variant()
            .iter()
            .map(|e| e.to_problem().problem_type)
            .collect();
        let mut deduped = problem_types.clone();
        deduped.sort();
        deduped.dedup();
        assert_eq!(
            problem_types.len(),
            deduped.len(),
            "expected every variant to produce a distinct problem_type: {problem_types:?}"
        );
    }

    #[test]
    fn every_variant_has_a_display_message() {
        for error in every_variant() {
            assert!(!error.to_string().is_empty());
        }
    }

    #[test]
    fn io_classified_variants_delegate_status_to_classify_io_error() {
        use mif_problem::ToProblem;

        let not_found = MifRhError::Io {
            path: "missing".to_string(),
            source: io_error(),
        };
        // classify_io_error maps NotFound to 404, overriding meta()'s generic 500.
        assert_eq!(not_found.to_problem().status, 404);
    }

    #[test]
    fn delegated_variants_forward_to_the_inner_error() {
        use mif_problem::ToProblem;

        let ontology_problem = MifRhError::Ontology(mif_ontology::OntologyError::Io {
            path: "onto.yaml".to_string(),
            source: io_error(),
        })
        .to_problem();
        let inner_problem = mif_ontology::OntologyError::Io {
            path: "onto.yaml".to_string(),
            source: io_error(),
        }
        .to_problem();
        assert_eq!(ontology_problem.problem_type, inner_problem.problem_type);

        let embed_problem = MifRhError::Embed(mif_embed::EmbedError::NoCacheDir {
            model: "test-model",
        })
        .to_problem();
        let inner_embed_problem = mif_embed::EmbedError::NoCacheDir {
            model: "test-model",
        }
        .to_problem();
        assert_eq!(embed_problem.problem_type, inner_embed_problem.problem_type);
    }

    #[test]
    fn exit_codes_match_the_documented_scheme() {
        use mif_problem::ToProblem;

        assert_eq!(
            MifRhError::CatalogMissing {
                path: "c".to_string()
            }
            .to_problem()
            .exit_code,
            Some(3)
        );
        assert_eq!(
            MifRhError::LockHeld { holder_pid: 1 }
                .to_problem()
                .exit_code,
            Some(2)
        );
        assert_eq!(
            MifRhError::DirectBindingInvalid {
                topic: "t".to_string(),
                id: "o".to_string(),
            }
            .to_problem()
            .exit_code,
            Some(1)
        );
    }

    #[test]
    fn rusqlite_error_converts_into_the_index_variant() {
        let source = rusqlite::Error::InvalidParameterName("p".to_string());
        let error: MifRhError = source.into();
        assert!(matches!(error, MifRhError::Index { .. }));
    }
}