shifty-cli 0.5.1

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

use clap::{Args, Parser, Subcommand, ValueEnum};
use std::collections::{BTreeSet, HashMap};
use std::error::Error;
use std::path::PathBuf;
use std::process::ExitCode;
use std::time::{Duration, Instant};

const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Parser)]
#[command(name = "shacl", about = "Formalism-first SHACL/SHACL-AF engine")]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Print the shifty CLI version.
    Version,
    /// Show a layer's view of a shapes graph.
    Inspect(InspectArgs),
    /// Validate a data graph against a shapes graph (normalized planned evaluator).
    Validate(ValidateArgs),
    /// Run SHACL-AF rule inference (forward chaining to a fixpoint).
    Infer(InferArgs),
    /// Show symbolic-repair structures for a data graph's violations.
    Repair(RepairArgs),
}

#[derive(Args)]
struct RepairArgs {
    /// Turtle shapes file(s) or URL(s) (repeatable).
    #[arg(long, value_name = "SHAPES", required = true, action = clap::ArgAction::Append)]
    shapes: Vec<String>,
    /// Turtle data file(s) or URL(s) (repeatable; defaults to shapes).
    #[arg(long, value_name = "DATA", action = clap::ArgAction::Append)]
    data: Vec<String>,
    /// Base IRI for parsing.
    #[arg(long)]
    base: Option<String>,
    /// Which repair structure to print.
    #[arg(long, value_enum, default_value_t = RepairStage::Tree)]
    stage: RepairStage,
    /// Output format.
    #[arg(long, value_enum, default_value_t = Format::Text)]
    format: Format,
    /// Skip SHACL-AF rule inference before witnessing.
    #[arg(long)]
    no_infer: bool,
    /// Run the fixpoint driver and emit the repaired data graph (N-Triples)
    /// instead of inspecting structures. Overrides `--stage`.
    #[arg(long)]
    apply: bool,
}

#[derive(Clone, Copy, ValueEnum)]
enum RepairStage {
    /// The witness tree per failing focus node (why each violates).
    Witness,
    /// The synthesized RepairTree per failing focus node (how to fix it).
    Tree,
    /// A concrete repair (ฮ”G) the enumeration driver finds for each focus.
    Solve,
}

#[derive(Args)]
struct InferArgs {
    /// Turtle shapes file(s) or URL(s) (repeatable).
    #[arg(long, value_name = "SHAPES", required = true, action = clap::ArgAction::Append)]
    shapes: Vec<String>,
    /// Turtle data file(s) or URL(s) (repeatable; defaults to shapes).
    #[arg(long, value_name = "DATA", action = clap::ArgAction::Append)]
    data: Vec<String>,
    /// Base IRI for parsing.
    #[arg(long)]
    base: Option<String>,
    /// Output format.
    #[arg(long, value_enum, default_value_t = Format::Text)]
    format: Format,
    /// Print input, shape, cache, and SPARQL execution telemetry after
    /// inference.
    #[arg(long)]
    profile: bool,
}

#[derive(Args)]
struct ValidateArgs {
    /// Turtle shapes file(s) or URL(s) (repeatable).
    #[arg(long, value_name = "SHAPES", required = true, action = clap::ArgAction::Append)]
    shapes: Vec<String>,
    /// Turtle data file(s) or URL(s) (repeatable; defaults to shapes).
    #[arg(long, value_name = "DATA", action = clap::ArgAction::Append)]
    data: Vec<String>,
    /// Base IRI for parsing.
    #[arg(long)]
    base: Option<String>,
    /// Output format.
    #[arg(long, value_enum, default_value_t = Format::Text)]
    format: Format,
    /// Emit a W3C `sh:ValidationReport` graph (N-Triples) instead of a summary.
    #[arg(long)]
    report: bool,
    /// Skip SHACL-AF rule inference before validation.
    #[arg(long)]
    no_infer: bool,
    /// RDF graph scope used during validation.
    #[arg(long, visible_alias = "graph-scope", value_enum, default_value_t = GraphMode::Union)]
    graph_mode: GraphMode,
    /// Named shape IRI to use as a validation entry point (repeatable). When
    /// omitted, every target-bearing shape is used.
    #[arg(long = "shape-name", visible_alias = "entry-shape", value_name = "IRI", action = clap::ArgAction::Append)]
    entry_shape_names: Vec<String>,
    /// Lowest result severity that makes validation non-conforming.
    #[arg(long, value_enum, default_value_t = SeverityLevel::Info)]
    minimum_severity: SeverityLevel,
    /// Write the data graph validation actually read โ€” after SHACL-AF
    /// inference โ€” as Turtle to this path, or to stdout for `-`.
    #[arg(long, value_name = "PATH")]
    dump_data: Option<String>,
    /// Write the merged shapes graph as Turtle to this path, or to stdout for
    /// `-`.
    #[arg(long, value_name = "PATH")]
    dump_shapes: Option<String>,
    /// Print input, shape, cache, and SPARQL execution telemetry after
    /// validation.
    #[arg(long)]
    profile: bool,
}

#[derive(Args)]
struct InspectArgs {
    /// Turtle shapes file.
    file: PathBuf,
    /// Which layer's representation to print.
    #[arg(long, value_enum, default_value_t = Stage::Algebra)]
    stage: Stage,
    /// Output format.
    #[arg(long, value_enum, default_value_t = Format::Text)]
    format: Format,
    /// Base IRI for parsing.
    #[arg(long)]
    base: Option<String>,
}

#[derive(Clone, Copy, ValueEnum)]
enum Stage {
    /// The raw parsed RDF triples (input to lowering).
    Rdf,
    /// The lowered formalism IR (Layer 2 output).
    Algebra,
    /// The normalized IR (Layer 4: CSE + simplification).
    Normalized,
    /// The recursion/stratification analysis (Layer 4).
    Strata,
    /// The physical plan (Layer 5: focus sources + cost-ordered checks).
    Plan,
    /// SPARQL capability classification: which constraint queries lower to the
    /// native executor vs. fall back to Spareval.
    Capability,
    /// Static graph reads, query/path identities, and function-call demand.
    Access,
}

#[derive(Clone, Copy, ValueEnum)]
enum Format {
    Text,
    Json,
    /// Graphviz DOT (algebra-ast stage only).
    Dot,
}

#[derive(Clone, Copy, ValueEnum)]
enum GraphMode {
    /// Focus nodes and evaluation use only the data graph.
    Data,
    /// Focus nodes come from data; evaluation uses data + shapes.
    Union,
    /// Focus nodes and evaluation both use data + shapes.
    UnionAll,
}

#[derive(Clone, Copy, ValueEnum)]
enum SeverityLevel {
    Info,
    Warning,
    Violation,
}

impl From<SeverityLevel> for shifty_algebra::Severity {
    fn from(value: SeverityLevel) -> Self {
        match value {
            SeverityLevel::Info => Self::Info,
            SeverityLevel::Warning => Self::Warning,
            SeverityLevel::Violation => Self::Violation,
        }
    }
}

impl From<GraphMode> for shifty_engine::ValidationGraphMode {
    fn from(mode: GraphMode) -> Self {
        match mode {
            GraphMode::Data => Self::Data,
            GraphMode::Union => Self::Union,
            GraphMode::UnionAll => Self::UnionAll,
        }
    }
}

fn main() -> ExitCode {
    env_logger::init();
    match run(Cli::parse()) {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("error: {e}");
            ExitCode::FAILURE
        }
    }
}

fn run(cli: Cli) -> Result<(), Box<dyn Error>> {
    match cli.command {
        Command::Version => {
            println!("{VERSION}");
            Ok(())
        }
        Command::Inspect(args) => inspect(args),
        Command::Validate(args) => validate(args),
        Command::Infer(args) => infer(args),
        Command::Repair(args) => repair(args),
    }
}

struct SourceBytes {
    bytes: Vec<u8>,
    content_type: Option<String>,
}

fn fetch_bytes(src: &str) -> Result<SourceBytes, Box<dyn Error>> {
    if src.starts_with("http://") || src.starts_with("https://") {
        let response = ureq::get(src).call()?;
        let content_type = response
            .headers()
            .get("content-type")
            .and_then(|value| value.to_str().ok())
            .map(ToOwned::to_owned);
        let mut bytes = Vec::new();
        // Read through the body's reader rather than `read_to_vec`, which caps
        // the response at 10 MB: a shapes closure served over HTTP is routinely
        // larger than that, and the cap would truncate it into a syntax error.
        std::io::Read::read_to_end(&mut response.into_body().into_reader(), &mut bytes)?;
        Ok(SourceBytes {
            bytes,
            content_type,
        })
    } else {
        Ok(SourceBytes {
            bytes: std::fs::read(src)?,
            content_type: None,
        })
    }
}

/// What one `--shapes`/`--data` source contributed, for `--profile`.
///
/// A file is named by the format that read it and the triples it produced,
/// because neither is inferable from the output otherwise: a `conforms: true`
/// looks the same whether a document parsed into 633 triples or into none, and
/// an extension is a hint, not a fact โ€” `ontology.ttl.md` is read as Turtle
/// because Turtle is what parsed it, not because `.md` says anything.
struct SourceStat {
    name: String,
    triples: usize,
    format: shifty_parse::RdfFormat,
}

fn load_sources(
    sources: &[String],
    base: Option<&str>,
) -> Result<shifty_parse::Loaded, Box<dyn Error>> {
    load_sources_profiled(sources, base).map(|(loaded, _)| loaded)
}

fn load_sources_profiled(
    sources: &[String],
    base: Option<&str>,
) -> Result<(shifty_parse::Loaded, Vec<SourceStat>), Box<dyn Error>> {
    let mut merged: Option<shifty_parse::Loaded> = None;
    let mut stats = Vec::with_capacity(sources.len());
    for src in sources {
        let fetched = fetch_bytes(src)?;
        let parsed_base = base.or_else(|| {
            (src.starts_with("http://") || src.starts_with("https://")).then_some(src.as_str())
        });
        let (loaded, format) = shifty_parse::load_rdf_auto_with_format(
            &fetched.bytes,
            fetched.content_type.as_deref(),
            Some(src),
            parsed_base,
        )?;
        stats.push(SourceStat {
            name: src.clone(),
            triples: loaded.graph.len(),
            format,
        });
        match merged.as_mut() {
            None => merged = Some(loaded),
            Some(m) => m.merge_from(&loaded),
        }
    }
    let merged = merged.ok_or_else(|| Box::<dyn Error>::from("no sources provided"))?;
    Ok((merged, stats))
}

/// The `--profile` lines describing one graph's inputs.
///
/// `merged` is the size of the graph that was actually evaluated, which is the
/// sum of the sources only when they share no triples; the difference is worth
/// stating rather than hiding, since a source can silently contribute nothing
/// new.
fn input_profile_lines(kind: &str, stats: &[SourceStat], merged: usize) -> Vec<String> {
    let mut lines = Vec::new();
    match stats {
        [] => return lines,
        [one] => {
            lines.push(format!(
                "profile: {kind}: {} from {} [{}]",
                plural(one.triples, "triple"),
                one.name,
                one.format
            ));
        }
        many => {
            let sum: usize = many.iter().map(|s| s.triples).sum();
            let overlap = if sum > merged {
                format!(" ({} dropped as duplicate)", plural(sum - merged, "triple"))
            } else {
                String::new()
            };
            lines.push(format!(
                "profile: {kind}: {} from {}{overlap}",
                plural(merged, "triple"),
                plural(many.len(), "source"),
            ));
            for stat in many {
                lines.push(format!(
                    "  {}: {} [{}]",
                    stat.name,
                    plural(stat.triples, "triple"),
                    stat.format
                ));
            }
        }
    }
    lines
}

/// The prefix table to serialize output graphs with: whatever the inputs
/// declared, plus standard entries for the vocabularies SHACL output always
/// mentions. First declaration of a name wins.
fn output_prefixes<'a>(
    shapes: &'a shifty_parse::Loaded,
    data: Option<&'a shifty_parse::Loaded>,
) -> Vec<(&'a str, &'a str)> {
    let mut prefixes: Vec<(&str, &str)> = Vec::new();
    let mut seen = std::collections::HashSet::new();
    for (name, iri) in shapes
        .prefixes
        .iter()
        .chain(data.map(|d| d.prefixes.iter()).into_iter().flatten())
    {
        if seen.insert(name.as_str()) {
            prefixes.push((name.as_str(), iri.as_str()));
        }
    }
    for (name, iri) in [
        ("sh", "http://www.w3.org/ns/shacl#"),
        ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
        ("xsd", "http://www.w3.org/2001/XMLSchema#"),
    ] {
        if seen.insert(name) {
            prefixes.push((name, iri));
        }
    }
    prefixes
}

fn turtle_bytes(
    graph: &oxrdf::Graph,
    prefixes: &[(&str, &str)],
) -> Result<Vec<u8>, Box<dyn Error>> {
    let mut ser = oxttl::TurtleSerializer::new();
    for (name, iri) in prefixes {
        ser = ser.with_prefix(*name, *iri)?;
    }
    Ok(graph
        .iter()
        .try_fold(ser.for_writer(Vec::new()), |mut s, triple| {
            s.serialize_triple(triple).map(|()| s)
        })?
        .finish()?)
}

/// Write out one of the graphs validation actually used.
///
/// Worth having because neither graph is the file on disk: the data graph is
/// post-inference, the shapes graph is every `--shapes` source merged, and
/// under `--graph-mode union` the evaluator reads them together. Blank node
/// labels are the parser's, not the source document's.
fn dump_graph(
    dest: &str,
    graph: &oxrdf::Graph,
    prefixes: &[(&str, &str)],
) -> Result<(), Box<dyn Error>> {
    let bytes = turtle_bytes(graph, prefixes)?;
    if dest == "-" {
        use std::io::Write;
        std::io::stdout().write_all(&bytes)?;
    } else {
        std::fs::write(dest, &bytes).map_err(|e| format!("failed to write {dest}: {e}"))?;
    }
    Ok(())
}

/// Print the `--profile` block: the inputs first, then the engine's own
/// telemetry. Held to the end of the command so `--report` can write its
/// N-Triples document to stdout uninterrupted.
fn print_profile(input_lines: &[String]) {
    for line in input_lines {
        println!("{line}");
    }
    if let Some(col) = shifty_engine::profile::take() {
        col.print_summary();
    }
}

fn profile_stage(lines: &mut Vec<String>, enabled: bool, stage: &str, elapsed: Duration) {
    if enabled {
        lines.push(format!(
            "profile: stage: {stage}: {:.3} ms",
            elapsed.as_secs_f64() * 1_000.0
        ));
    }
}

fn infer(args: InferArgs) -> Result<(), Box<dyn Error>> {
    if args.profile {
        shifty_engine::profile::enable();
    }
    let base = args.base.as_deref();
    let stage_start = Instant::now();
    let (shapes, shape_stats) = load_sources_profiled(&args.shapes, base)?;
    let shapes_load_time = stage_start.elapsed();
    let stage_start = Instant::now();
    let compiled = shifty_engine::CompiledShapes::compile(shapes)?;
    let compile_time = stage_start.elapsed();
    for d in compiled.diagnostics() {
        eprintln!("{d}");
    }

    let mut input_lines =
        input_profile_lines("shapes", &shape_stats, compiled.source().graph.len());
    profile_stage(
        &mut input_lines,
        args.profile,
        "shapes load",
        shapes_load_time,
    );
    profile_stage(&mut input_lines, args.profile, "compile", compile_time);
    let stage_start = Instant::now();
    let data = if args.data.is_empty() {
        input_lines
            .push("profile: data: none given; the shapes graph is also the data graph".to_string());
        None
    } else {
        let (data, data_stats) = load_sources_profiled(&args.data, base)?;
        input_lines.extend(input_profile_lines("data", &data_stats, data.graph.len()));
        Some(data)
    };
    profile_stage(
        &mut input_lines,
        args.profile,
        "data load",
        stage_start.elapsed(),
    );

    let session_data = data.map_or(shifty_engine::SessionData::Embedded, |data| {
        shifty_engine::SessionData::Separate(data.graph)
    });
    let stage_start = Instant::now();
    let session = match compiled.session(
        session_data,
        shifty_engine::SessionOptions {
            inference: true,
            ..Default::default()
        },
    ) {
        Ok(session) => session,
        Err(e) => return Err(format!("{e}; cannot infer (see `inspect --stage strata`)").into()),
    };
    profile_stage(
        &mut input_lines,
        args.profile,
        "session and inference",
        stage_start.elapsed(),
    );
    for d in session.diagnostics() {
        eprintln!("warning: {}", d.message);
    }

    let stage_start = Instant::now();
    match args.format {
        Format::Dot => return Err("--format dot is not supported for infer".into()),
        Format::Json => {
            let triples: Vec<_> = session
                .inferred()
                .iter()
                .map(|t| {
                    serde_json::json!({
                        "subject": t.subject.to_string(),
                        "predicate": t.predicate.to_string(),
                        "object": t.object.to_string(),
                    })
                })
                .collect();
            println!("{}", serde_json::to_string_pretty(&triples)?);
        }
        Format::Text => {
            println!("inferred {} triple(s):", session.inferred().len());
            let mut lines: Vec<String> = session.inferred().iter().map(|t| t.to_string()).collect();
            lines.sort();
            for line in lines {
                println!("  {line}");
            }
        }
    }
    profile_stage(
        &mut input_lines,
        args.profile,
        "export",
        stage_start.elapsed(),
    );
    if args.profile {
        print_profile(&input_lines);
    }
    Ok(())
}

/// The validation outcome as JSON, enriched so a consumer never has to hold the
/// schema to make sense of it.
///
/// The engine's own serialization is kept verbatim โ€” new engine fields flow
/// through without touching this โ€” and three things are layered on:
/// `definition`/`definition_pretty` per reason (the constraint in words, so a
/// reader needs no arena at all), `target`/`shape_name` per violation (which the
/// text report already showed), and a top-level `shapes` map.
///
/// `shapes` is the transitive closure of every reported constraint, keyed by the
/// same ids that `constraint_id` and the algebra's own `qualifier`/child fields
/// use, so those pointers resolve. It is deliberately *not* the whole arena: for
/// the s223 shapes that is 2412 slots against the 19 a report actually reaches,
/// and a reader who wants all of them has `inspect --stage plan --format json`.
fn json_report(
    outcome: &shifty_engine::ValidationOutcome,
    schema: &shifty_algebra::Schema,
    plan: &shifty_opt::PhysicalPlan,
    px: &shifty_algebra::Prefixes,
) -> Result<serde_json::Value, Box<dyn Error>> {
    use serde_json::{Map, Value, json};
    use shifty_algebra::render::{PRETTY_WIDTH, describe_shape_in, describe_shape_pretty};

    let mut doc = serde_json::to_value(outcome)?;
    let mut referenced: std::collections::BTreeSet<u32> = Default::default();

    if let Some(violations) = doc.get_mut("violations").and_then(Value::as_array_mut) {
        for (value, v) in violations.iter_mut().zip(&outcome.violations) {
            let Some(object) = value.as_object_mut() else {
                continue;
            };
            if let Some(statement) = schema.statements.get(v.statement) {
                object.insert(
                    "target".into(),
                    json!(shifty_algebra::render::selector_to_string_in_px(
                        &statement.selector,
                        &schema.arena,
                        &schema.prefixes
                    )),
                );
                if let Some(name) = schema.name_of(statement.shape) {
                    object.insert("shape_name".into(), json!(name));
                }
            }
            let reasons = object
                .get_mut("reasons")
                .and_then(Value::as_array_mut)
                .map(|r| r.iter_mut().zip(&v.reasons));
            for (value, r) in reasons.into_iter().flatten() {
                let Some(object) = value.as_object_mut() else {
                    continue;
                };
                object.insert(
                    "definition".into(),
                    json!(describe_shape_in(&plan.arena, r.constraint_id, px)),
                );
                object.insert(
                    "definition_pretty".into(),
                    json!(describe_shape_pretty(
                        &plan.arena,
                        r.constraint_id,
                        px,
                        PRETTY_WIDTH
                    )),
                );
                collect_shapes(&plan.arena, r.constraint_id, &mut referenced);
            }
        }
    }

    let shapes: Map<String, Value> = referenced
        .iter()
        .map(|id| {
            Ok((
                id.to_string(),
                serde_json::to_value(plan.arena.get(shifty_algebra::ShapeId(*id)))?,
            ))
        })
        .collect::<Result<_, serde_json::Error>>()?;
    if let Some(object) = doc.as_object_mut() {
        object.insert("shapes".into(), Value::Object(shapes));
    }
    Ok(doc)
}

/// Every arena slot reachable from `id`, itself included. Uses the arena's own
/// child links, so a shape referenced only through a `sh:filterShape` inside a
/// node expression is collected too.
fn collect_shapes(
    arena: &shifty_algebra::ShapeArena,
    id: shifty_algebra::ShapeId,
    out: &mut std::collections::BTreeSet<u32>,
) {
    if !out.insert(id.0) {
        return;
    }
    for child in arena.get(id).child_shapes() {
        collect_shapes(arena, child, out);
    }
}

/// The symbols a report can use, and what each means. Printed at the end of a
/// text report, but only for the ones that actually appear: a key for notation
/// the reader never met is noise, and the common report uses none of it.
const NOTATION: &[(&str, &str, &str)] = &[
    (
        "โˆ€",
        "โˆ€ p . X",
        "every value along p satisfies X (holds when there are none)",
    ),
    (
        "โˆƒ[",
        "โˆƒ[m..n] p . X",
        "between m and n values along p satisfy X",
    ),
    ("โˆ„", "โˆ„ p", "no values along p at all"),
    // `^^` is a typed literal's datatype separator, not an inverse path, and
    // matching it would gloss notation the report never used.
    ("^", "^p", "p followed backwards, from object to subject"),
    ("*", "p*", "p repeated zero or more times"),
];

fn notation_key(lines: &[String]) -> Vec<String> {
    let used: Vec<(&str, &str)> = NOTATION
        .iter()
        .filter(|(symbol, ..)| {
            lines
                .iter()
                .any(|line| line.replace("^^", "").contains(symbol))
        })
        .map(|(_, form, gloss)| (*form, *gloss))
        .collect();
    if used.is_empty() {
        return Vec::new();
    }
    let width = used
        .iter()
        .map(|(form, _)| form.chars().count())
        .max()
        .unwrap_or(0);
    let mut out = vec![String::new(), "notation".to_string()];
    out.extend(used.into_iter().map(|(form, gloss)| {
        let padding = " ".repeat(width - form.chars().count());
        format!("  {form}{padding}   {gloss}")
    }));
    out
}

/// `1 violation` / `2 violations`. A counted noun in a summary line is read, not
/// parsed, so `violation(s)` is a small tax on every reader.
fn plural(n: usize, word: &str) -> String {
    if n == 1 {
        format!("{n} {word}")
    } else {
        format!("{n} {word}s")
    }
}

/// One thing wrong with the graph, and every node it is wrong on.
///
/// Reasons that fail the same statement with the same rendered explanation are
/// the same finding: the constraint, the message and the requirement are
/// identical, and only the nodes differ.
struct Finding {
    /// The `(selector, shape)` statement this came from. Two findings sharing it
    /// are two parts of one authored shape.
    statement: usize,
    target: String,
    severity: String,
    shape: Option<String>,
    /// The shared explanation โ€” the reason block, already rendered.
    body: Vec<String>,
    /// `(focus node, value node)`, one per grouped reason. The value is the node
    /// reached from the focus along the path, absent when the constraint failed
    /// on the focus node itself.
    members: Vec<(String, Option<String>)>,
}

/// The other findings from the same authored shape that these same nodes also
/// fail, as `Finding 5` / `Finding 5 (2 of 53 nodes)`.
///
/// Grouping by reason splits one shape's parts into separate findings, which is
/// right โ€” they are separate problems โ€” but leaves a reader with no sign that
/// two of them came from one `sh:and`. Only same-statement siblings are
/// reported: in a large graph nearly every pair of findings shares some node,
/// and saying so would be noise rather than a relationship.
fn related_findings(index: usize, findings: &[Finding]) -> String {
    let current = &findings[index];
    let mine: BTreeSet<&str> = current.members.iter().map(|(f, _)| f.as_str()).collect();
    let mut parts = Vec::new();
    for (other_index, other) in findings.iter().enumerate() {
        if other_index == index || other.statement != current.statement {
            continue;
        }
        let shared = other
            .members
            .iter()
            .filter(|(focus, _)| mine.contains(focus.as_str()))
            .count();
        if shared == 0 {
            continue;
        }
        let label = format!("Finding {}", other_index + 1);
        parts.push(if shared == mine.len() {
            label
        } else {
            format!("{label} ({shared} of {} nodes)", mine.len())
        });
    }
    parts.join(", ")
}

/// Who a finding is wrong on.
///
/// A single node reads inline as labelled fields. Several get a counted list;
/// when the constraint failed on a value reached from the node rather than on
/// the node itself, the heading says so and each line carries that value โ€” a
/// bare parenthesised IRI leaves the reader guessing what it is.
fn render_affected(members: &[(String, Option<String>)]) -> Vec<String> {
    if let [(focus, value)] = members {
        let mut out = field(2, "affects", focus);
        // Said either way. A missing line would leave the reader to infer that
        // the constraint applied to the focus node itself.
        out.extend(field(
            2,
            "value node",
            value.as_deref().unwrap_or("(the focus node itself)"),
        ));
        return out;
    }
    let counted = plural(members.len(), "focus node");
    let any_values = members.iter().any(|(_, value)| value.is_some());
    let mut out = field(
        2,
        "affects",
        &if any_values {
            format!("{counted}, each with the value node that failed")
        } else {
            format!("{counted}; the constraint applies to each node itself")
        },
    );
    // Pad the focus column so the values line up, but never so far that one long
    // IRI pushes every value off the edge.
    let column = members
        .iter()
        .filter(|(_, value)| value.is_some())
        .map(|(focus, _)| focus.chars().count())
        .filter(|width| *width <= 56)
        .max()
        .unwrap_or(0);
    out.extend(members.iter().map(|(focus, value)| match value {
        Some(value) => {
            let padding = " ".repeat(column.saturating_sub(focus.chars().count()));
            format!("    {focus}{padding}   {value}")
        }
        None => format!("    {focus}"),
    }));
    out
}

/// Column the labelled values start at. Wide enough for the longest label, so
/// values line up into a column a reader can scan without reading the labels.
const LABEL_WIDTH: usize = 13;

/// One `label   value` line, or several when the value has to wrap. Wrapped
/// lines hang to the value column so the label column stays clean.
fn field(indent: usize, label: &str, value: &str) -> Vec<String> {
    let pad = " ".repeat(indent);
    let hang = " ".repeat(indent + LABEL_WIDTH);
    let width = shifty_algebra::render::PRETTY_WIDTH;
    let mut out = Vec::new();
    for (i, line) in wrap(value, width.saturating_sub(indent + LABEL_WIDTH))
        .into_iter()
        .enumerate()
    {
        if i == 0 {
            out.push(format!("{pad}{label:<LABEL_WIDTH$}{line}"));
        } else {
            out.push(format!("{hang}{line}"));
        }
    }
    if out.is_empty() {
        out.push(format!("{pad}{label}"));
    }
    out
}

/// Wrap on spaces. A token longer than `width` โ€” an IRI, usually โ€” is left
/// over-long rather than split: a broken IRI is not copy-pastable, which is most
/// of what a reader wants one for.
fn wrap(text: &str, width: usize) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for word in text.split_whitespace() {
        match out.last_mut() {
            Some(line) if line.chars().count() + 1 + word.chars().count() <= width => {
                line.push(' ');
                line.push_str(word);
            }
            _ => out.push(word.to_string()),
        }
    }
    out
}

/// A reason as labelled fields.
///
/// The labels exist because the two nodes in a reason are easy to confuse: the
/// focus node is what was selected for checking, the value node is what was
/// reached from it along the path and actually failed. Unlabelled, a reader
/// meeting a report for the first time reads the value node as the subject.
fn render_reason(
    r: &shifty_engine::Reason,
    arena: &shifty_algebra::ShapeArena,
    px: &shifty_algebra::Prefixes,
    focus: &str,
    violation_severity: &str,
    indent: usize,
    // When set, the reason's own value node is captured here instead of printed:
    // the caller is grouping by explanation and lists the nodes together.
    // Sub-reasons always print theirs, since which value took which `sh:or`
    // branch is part of that branch's explanation.
    hoist_value: Option<&mut Option<String>>,
) -> Vec<String> {
    let requirement = describe_requirement(r, arena, px, indent + LABEL_WIDTH);
    // A cardinality reason's requirement already says everything its generated
    // message says โ€” the bound is in the constraint, the count is on the `found`
    // line โ€” so printing both restates the same sentence twice. Anything else
    // keeps it: the generated message often names specifics the constraint does
    // not, such as which predicates a `closed` shape did not expect.
    let mut lines = Vec::new();

    // Severity only when it differs from the violation's, which is the max of
    // its reasons; repeating the same word on every line is noise.
    if r.severity.to_string() != violation_severity {
        lines.extend(field(indent, "severity", &r.severity.to_string()));
    }
    if let Some(author) = &r.author_message {
        lines.extend(field(indent, "message", author));
    }
    // The generated message is a prose paraphrase of the labelled fields below.
    // Print it only when it still adds something: with no author message and no
    // restatement it is the only prose the reason has, but for a cardinality
    // failure `found` and `requirement` already say it โ€” and say it better, since
    // the paraphrase inlines the whole description onto one line.
    // `message` is the shape author's sentence about intent; `failure` is what
    // went wrong here. Always both: a field that appears only when some rule
    // decides it is not redundant makes the reader work out why it is missing,
    // and an explanation that is complete every time is worth a repeated line.
    lines.extend(field(indent, "failure", &r.message));
    if let Some(path) = &r.path {
        lines.extend(field(indent, "path", path));
    }
    let value = shifty_algebra::render::term_to_string_in(&r.value, px);
    match hoist_value {
        Some(slot) if value != focus => *slot = Some(value),
        Some(_) => {}
        None if value != focus => lines.extend(field(indent, "value node", &value)),
        None => {}
    }
    if let Some(found) = found_line(r, arena) {
        lines.extend(field(indent, "found", &found));
    }
    if let Some(requirement) = requirement {
        lines.extend(labelled_block(indent, "requirement", &requirement));
    }
    if let Some(d) = &r.sparql_diagnostic {
        lines.extend(render_sparql_diagnostic(d, indent + 2));
    }
    for (i, sub) in r.sub_reasons.iter().enumerate() {
        lines.push(String::new());
        lines.push(format!(
            "{}or-branch {} of {} (satisfying any one of these fixes it)",
            " ".repeat(indent + 2),
            i + 1,
            r.sub_reasons.len()
        ));
        lines.extend(render_reason(
            sub,
            arena,
            px,
            focus,
            violation_severity,
            indent + 4,
            None,
        ));
    }
    lines
}

/// A value that may be several lines: the label leads the first, the rest are
/// indented under it.
fn labelled_block(indent: usize, label: &str, value: &str) -> Vec<String> {
    let mut lines = value.lines();
    let pad = " ".repeat(indent);
    let hang = " ".repeat(indent + LABEL_WIDTH);
    let mut out = match lines.next() {
        Some(first) => vec![format!("{pad}{label:<LABEL_WIDTH$}{first}")],
        None => return Vec::new(),
    };
    out.extend(lines.map(|line| format!("{hang}{line}")));
    out
}

/// What the constraint demands, laid out over several lines when it is nested.
fn describe_requirement(
    r: &shifty_engine::Reason,
    arena: &shifty_algebra::ShapeArena,
    px: &shifty_algebra::Prefixes,
    indent: usize,
) -> Option<String> {
    use shifty_algebra::render::{PRETTY_WIDTH, describe_shape_pretty};
    let width = PRETTY_WIDTH.saturating_sub(indent);
    let text = describe_shape_pretty(arena, r.constraint_id, px, width);
    (!text.is_empty()).then_some(text)
}

/// The count the algebra does not carry, paired with the bound it missed.
///
/// A plain `sh:minCount`/`sh:maxCount` counts everything along the path; a
/// qualified count only counts values satisfying the qualifier. Saying which is
/// the difference between "found 0" meaning the path was empty and it meaning
/// the path held values that did not match.
fn found_line(r: &shifty_engine::Reason, arena: &shifty_algebra::ShapeArena) -> Option<String> {
    let found = r.observed_count?;
    let shifty_algebra::Shape::Count {
        min,
        max,
        qualifier,
        ..
    } = &r.constraint
    else {
        return Some(format!("{found} value(s)"));
    };
    let counted = match arena.get(*qualifier) {
        shifty_algebra::Shape::Top | shifty_algebra::Shape::Pending => {
            format!("{found} value(s) along the path")
        }
        _ => format!("{found} value(s) matching the requirement"),
    };
    let bound = match (min, max) {
        (Some(m), _) if found < *m => format!("; at least {m} required"),
        (_, Some(x)) if found > *x => format!("; at most {x} allowed"),
        _ => String::new(),
    };
    Some(format!("{counted}{bound}"))
}

/// Render a [`shifty_engine::SparqlDiagnostic`]: the query that ran, what it
/// was bound to, and what rows it actually returned โ€” so a SPARQL constraint
/// failure is never just "not satisfied."
fn render_sparql_diagnostic(d: &shifty_engine::SparqlDiagnostic, indent: usize) -> Vec<String> {
    let pad = " ".repeat(indent);
    let mut lines = vec![format!("{pad}SPARQL:")];
    lines.push(format!("{pad}  Query:"));
    for line in d.query.lines() {
        lines.push(format!("{pad}    {line}"));
    }
    if !d.bindings.is_empty() {
        lines.push(format!("{pad}  Bound:"));
        for (k, v) in &d.bindings {
            lines.push(format!("{pad}    ${k} = {v}"));
        }
    }
    if !d.results.is_empty() {
        lines.push(format!("{pad}  Results:"));
        for (i, row) in d.results.iter().enumerate() {
            if row.is_empty() {
                lines.push(format!("{pad}    [{}] (no projected variables)", i + 1));
                continue;
            }
            let cols = row
                .iter()
                .map(|(k, v)| format!("?{k} = {v}"))
                .collect::<Vec<_>>()
                .join(", ");
            lines.push(format!("{pad}    [{}] {cols}", i + 1));
        }
    }
    if let Some(reason) = &d.fallback_reason {
        lines.push(format!("{pad}  Did not use the native executor: {reason}"));
    }
    lines
}

fn validate(args: ValidateArgs) -> Result<(), Box<dyn Error>> {
    if args.profile {
        shifty_engine::profile::enable();
    }
    let base = args.base.as_deref();
    let stage_start = Instant::now();
    let (shapes_loaded, shape_stats) = load_sources_profiled(&args.shapes, base)?;
    let shapes_load_time = stage_start.elapsed();
    if shapes_loaded.graph.is_empty() {
        return Err("explicit shapes graph is empty".into());
    }
    let stage_start = Instant::now();
    let compiled = shifty_engine::CompiledShapes::compile(shapes_loaded)?;
    let compile_time = stage_start.elapsed();
    let shapes_loaded = compiled.source();
    let authored = compiled.authored_schema();
    for d in compiled.diagnostics() {
        eprintln!("{d}");
    }
    let graph_mode = args.graph_mode.into();
    let threshold: shifty_algebra::Severity = args.minimum_severity.into();
    let finding_options = shifty_engine::FindingOptions {
        minimum_severity: threshold.clone(),
        sort_results: true,
        entry_shape_names: args.entry_shape_names.clone(),
    };

    // Input telemetry is collected as the graphs load but printed at the very
    // end: `--report` and `--format json` own stdout, and a profile line ahead
    // of either would land inside the document.
    let mut input_lines = input_profile_lines("shapes", &shape_stats, shapes_loaded.graph.len());
    profile_stage(
        &mut input_lines,
        args.profile,
        "shapes load",
        shapes_load_time,
    );
    profile_stage(&mut input_lines, args.profile, "compile", compile_time);
    let stage_start = Instant::now();
    let data_loaded = if args.data.is_empty() {
        input_lines
            .push("profile: data: none given; the shapes graph is also the data graph".to_string());
        None
    } else {
        let (data, data_stats) = load_sources_profiled(&args.data, base)?;
        input_lines.extend(input_profile_lines("data", &data_stats, data.graph.len()));
        Some(data)
    };
    profile_stage(
        &mut input_lines,
        args.profile,
        "data load",
        stage_start.elapsed(),
    );
    // Report display draws on both documents: focus and value nodes are
    // data-graph terms, constraints are shapes-graph terms, and each reads best
    // spelled the way its own document spelled it.
    let display_prefixes = shifty_algebra::Prefixes::merged([
        data_loaded
            .as_ref()
            .map(|d| d.prefixes.clone())
            .unwrap_or_default(),
        shapes_loaded.prefixes.clone(),
    ]);
    let stage_start = Instant::now();
    let session_data = data_loaded
        .as_ref()
        .map_or(shifty_engine::SessionData::Embedded, |data| {
            shifty_engine::SessionData::Separate(data.graph.clone())
        });
    let session = compiled
        .session(
            session_data,
            shifty_engine::SessionOptions {
                graph_mode,
                inference: !args.no_infer,
                engine: Default::default(),
            },
        )
        .map_err(|e| format!("{e}; cannot prepare validation (see `inspect --stage strata`)"))?;
    profile_stage(
        &mut input_lines,
        args.profile,
        "session and inference",
        stage_start.elapsed(),
    );
    if args.no_infer {
        input_lines.push("profile: inference: skipped (--no-infer)".to_string());
    } else {
        for d in session.diagnostics() {
            eprintln!("warning: {}", d.message);
        }
        input_lines.push(format!(
            "profile: inference: {} added before validation",
            plural(session.inferred().len(), "triple")
        ));
    }
    // Dumps come before the result: they describe the run's input, and writing
    // them first means a run that later fails to validate still produced them.
    let prefixes = output_prefixes(shapes_loaded, data_loaded.as_ref());
    if let Some(dest) = &args.dump_shapes {
        dump_graph(dest, &shapes_loaded.graph, &prefixes)?;
    }
    if let Some(dest) = &args.dump_data {
        dump_graph(dest, session.data(), &prefixes)?;
    }

    // W3C report mode: component-granular validator + RDF report output.
    if args.report {
        let stage_start = Instant::now();
        let report = session.report(&finding_options);
        profile_stage(
            &mut input_lines,
            args.profile,
            "first report",
            stage_start.elapsed(),
        );
        let stage_start = Instant::now();
        let graph = shifty_engine::report_to_graph(&report);
        let bytes = turtle_bytes(&graph, &prefixes)?;
        print!("{}", String::from_utf8_lossy(&bytes));
        profile_stage(
            &mut input_lines,
            args.profile,
            "export",
            stage_start.elapsed(),
        );
        if args.profile {
            print_profile(&input_lines);
        }
        return Ok(());
    }

    let physical = compiled.physical_plan();
    let stage_start = Instant::now();
    let mut outcome = session.validate(&finding_options);
    profile_stage(
        &mut input_lines,
        args.profile,
        "first validation",
        stage_start.elapsed(),
    );
    let stage_start = Instant::now();

    // The engine retains every finding; `--minimum-severity` scopes both
    // `conforms` (already applied) and what we display/serialize here. Drop
    // findings below the threshold; a violation's own severity is the max of its
    // reasons, so any violation that survives keeps at least its top reason.
    outcome.violations.retain(|v| v.severity.meets(&threshold));
    for v in &mut outcome.violations {
        v.reasons.retain(|r| r.severity.meets(&threshold));
    }

    match args.format {
        Format::Dot => return Err("--format dot is not supported for validate".into()),
        Format::Json => {
            let doc = json_report(&outcome, authored, physical, &display_prefixes)?;
            println!("{}", serde_json::to_string_pretty(&doc)?);
        }
        Format::Text => {
            // Findings, not violations. The same constraint failing on 53 nodes
            // is one thing wrong with the graph, and printing its explanation 53
            // times buries the two other things that are also wrong.
            //
            // The unit is one *reason*, not one violation: a focus node that
            // fails two constraints has two things wrong with it, and grouping
            // them together would force a member to carry several unrelated
            // value nodes with nothing to say which belonged to which.
            let mut findings: Vec<Finding> = Vec::new();
            let mut index: HashMap<(usize, String, String, String), usize> = HashMap::new();
            for v in &outcome.violations {
                let st = &authored.statements[v.statement];
                let focus = shifty_algebra::render::term_to_string_in(&v.focus, &display_prefixes);
                let target = shifty_algebra::render::selector_to_string_in_px(
                    &st.selector,
                    &authored.arena,
                    &authored.prefixes,
                );
                // The source shape's IRI. Printed even when the target line
                // already names it โ€” an implicit class target renders as
                // `class(<that same IRI>)` โ€” because which shape a finding came
                // from is the first thing a reader goes to fix, and it should
                // not be conditional on how the target happened to render.
                let shape = authored
                    .name_of(st.shape)
                    .map(|name| display_prefixes.compact(name));

                for r in &v.reasons {
                    let severity = r.severity.to_string();
                    let mut value = None;
                    let body = render_reason(
                        r,
                        &physical.arena,
                        &display_prefixes,
                        &focus,
                        &severity,
                        2,
                        Some(&mut value),
                    );
                    let key = (
                        v.statement,
                        severity.clone(),
                        target.clone(),
                        body.join("\n"),
                    );
                    match index.get(&key) {
                        Some(at) => findings[*at].members.push((focus.clone(), value)),
                        None => {
                            index.insert(key, findings.len());
                            findings.push(Finding {
                                statement: v.statement,
                                target: target.clone(),
                                severity,
                                shape: shape.clone(),
                                body,
                                members: vec![(focus.clone(), value)],
                            });
                        }
                    }
                }
            }

            let total = outcome.violations.len();
            if outcome.conforms {
                println!("conforms: true");
            } else if findings.len() == total {
                println!("conforms: false โ€” {}", plural(total, "violation"));
            } else {
                println!(
                    "conforms: false โ€” {} in {}",
                    plural(total, "violation"),
                    plural(findings.len(), "finding")
                );
            }

            let mut out: Vec<String> = Vec::new();
            for (i, finding) in findings.iter().enumerate() {
                out.push(String::new());
                out.push(format!("Finding {} of {}", i + 1, findings.len()));
                out.extend(field(2, "target", &finding.target));
                out.extend(field(2, "severity", &finding.severity));
                if let Some(shape) = &finding.shape {
                    out.extend(field(2, "shape", shape));
                }
                out.extend(finding.body.iter().cloned());
                out.push(String::new());
                out.extend(render_affected(&finding.members));
                let related = related_findings(i, &findings);
                if !related.is_empty() {
                    out.extend(field(2, "also fails", &related));
                }
            }
            for line in &out {
                println!("{line}");
            }
            for line in notation_key(&out) {
                println!("{line}");
            }
        }
    }

    profile_stage(
        &mut input_lines,
        args.profile,
        "export",
        stage_start.elapsed(),
    );

    if args.profile {
        print_profile(&input_lines);
    }
    Ok(())
}

fn repair(args: RepairArgs) -> Result<(), Box<dyn Error>> {
    let base = args.base.as_deref();
    let compiled = shifty_engine::CompiledShapes::compile(load_sources(&args.shapes, base)?)?;
    let shapes_loaded = compiled.source();
    for d in compiled.diagnostics() {
        eprintln!("{d}");
    }
    let schema = compiled.authored_schema();

    let data_loaded = if args.data.is_empty() {
        None
    } else {
        Some(load_sources(&args.data, base)?)
    };

    let session_data = data_loaded
        .as_ref()
        .map_or(shifty_engine::SessionData::Embedded, |data| {
            shifty_engine::SessionData::Separate(data.graph.clone())
        });
    let session = compiled
        .session(
            session_data,
            shifty_engine::SessionOptions {
                inference: !args.no_infer,
                ..Default::default()
            },
        )
        .map_err(|e| format!("{e}; cannot prepare repair (see `inspect --stage strata`)"))?;
    for diagnostic in session.diagnostics() {
        eprintln!("warning: {}", diagnostic.message);
    }
    let data_graph = session.data().clone();
    // Witness/gate against `data โˆช shapes` so paths and the class hierarchy
    // (e.g. `rdfs:subClassOf` for `sh:class`) resolve against the shapes/ontology
    // graph, while focus and the emitted repair stay the data graph. When the
    // shapes embed the data, `data_graph` already is the union.
    let context = if data_loaded.is_some() {
        shifty_engine::graph_union(&data_graph, &shapes_loaded.graph)
    } else {
        data_graph.clone()
    };

    // --apply: run the fixpoint driver and emit the repaired graph.
    if args.apply {
        let result = match shifty_engine::repair_to_fixpoint(
            &data_graph,
            &context,
            schema,
            shifty_engine::EnumOptions::default(),
        ) {
            Ok(r) => r,
            Err(e) => {
                return Err(format!("{e}; cannot repair (see `inspect --stage strata`)").into());
            }
        };
        let mut lines: Vec<String> = result.graph.iter().map(|t| t.to_string()).collect();
        lines.sort();
        for line in lines {
            println!("{line}");
        }
        eprintln!(
            "repaired: applied {} repair(s) over {} iteration(s); {} violation(s) remain",
            result.applied.len(),
            result.iterations,
            result.remaining,
        );
        return Ok(());
    }

    let witnesses = match shifty_engine::witness_violations(&data_graph, &context, schema) {
        Ok(ws) => ws,
        Err(e) => {
            return Err(format!("{e}; cannot witness (see `inspect --stage strata`)").into());
        }
    };

    if matches!(args.format, Format::Dot) {
        return Err("--format dot is not supported for repair".into());
    }

    let target = |statement: usize| {
        shifty_algebra::render::selector_to_string_in_px(
            &schema.statements[statement].selector,
            &schema.arena,
            &schema.prefixes,
        )
    };

    match args.stage {
        RepairStage::Witness => match args.format {
            Format::Json => println!("{}", serde_json::to_string_pretty(&witnesses)?),
            Format::Text => {
                if witnesses.is_empty() {
                    println!("conforms: no violations to witness");
                }
                for fw in &witnesses {
                    println!("{}  [target: {}]", fw.focus, target(fw.statement));
                    for line in render_witness(&fw.failure, &schema.prefixes, 2) {
                        println!("{line}");
                    }
                }
            }
            Format::Dot => unreachable!(),
        },
        RepairStage::Tree => {
            let trees: Vec<(&shifty_engine::FocusWitness, shifty_repair::RepairTree)> = witnesses
                .iter()
                .map(|fw| (fw, shifty_engine::synthesize(&schema.arena, fw)))
                .collect();
            match args.format {
                Format::Json => {
                    let arr: Vec<_> = trees
                        .iter()
                        .map(|(fw, t)| {
                            serde_json::json!({
                                "focus": fw.focus.to_string(),
                                "statement": fw.statement,
                                "tree": t,
                            })
                        })
                        .collect();
                    println!("{}", serde_json::to_string_pretty(&arr)?);
                }
                Format::Text => {
                    if trees.is_empty() {
                        println!("conforms: no violations to repair");
                    }
                    for (fw, t) in &trees {
                        println!("{}  [target: {}]", fw.focus, target(fw.statement));
                        for line in render_tree(t, &schema.arena, &schema.prefixes, 2) {
                            println!("{line}");
                        }
                    }
                }
                Format::Dot => unreachable!(),
            }
        }
        RepairStage::Solve => {
            let opts = shifty_engine::EnumOptions::default();
            let mut json_items = Vec::new();
            if witnesses.is_empty() {
                match args.format {
                    Format::Json => println!("[]"),
                    _ => println!("conforms: no violations to repair"),
                }
            }
            for fw in &witnesses {
                let tree = shifty_engine::synthesize(&schema.arena, fw);
                let sol = match shifty_engine::enumerate_repair(
                    &tree,
                    &data_graph,
                    &context,
                    schema,
                    opts,
                ) {
                    Ok(s) => s,
                    Err(e) => return Err(format!("{e}; cannot solve").into()),
                };
                match args.format {
                    Format::Text => {
                        println!("{}  [target: {}]", fw.focus, target(fw.statement));
                        match &sol {
                            Some(s) => {
                                println!(
                                    "  repair (fixes {}, introduces {}):",
                                    s.outcome.fixed.len(),
                                    s.outcome.introduced.len()
                                );
                                for t in &s.delta.delete {
                                    println!("    del  {t}");
                                }
                                for t in &s.delta.add {
                                    println!("    add  {t}");
                                }
                            }
                            None => println!("  no repair found within budget"),
                        }
                    }
                    Format::Json => json_items.push(serde_json::json!({
                        "focus": fw.focus.to_string(),
                        "statement": fw.statement,
                        "repair": sol.as_ref().map(|s| serde_json::json!({
                            "add": s.delta.add.iter().map(|t| t.to_string()).collect::<Vec<_>>(),
                            "delete": s.delta.delete.iter().map(|t| t.to_string()).collect::<Vec<_>>(),
                            "fixed": s.outcome.fixed.len(),
                            "introduced": s.outcome.introduced.len(),
                        })),
                    })),
                    Format::Dot => unreachable!(),
                }
            }
            if matches!(args.format, Format::Json) {
                println!("{}", serde_json::to_string_pretty(&json_items)?);
            }
        }
    }
    Ok(())
}

fn path_str(p: &shifty_algebra::Path, px: &shifty_algebra::Prefixes) -> String {
    shifty_algebra::render::path_to_string_in(p, px)
}

fn render_witness(
    w: &shifty_engine::Witness,
    px: &shifty_algebra::Prefixes,
    indent: usize,
) -> Vec<String> {
    use shifty_engine::Witness as W;
    let pad = " ".repeat(indent);
    let mut out = Vec::new();
    match w {
        W::Atom {
            node,
            reached_by,
            produced_by,
            ..
        } => out.push(format!(
            "{pad}Atom at {node} via {}{}",
            path_str(reached_by, px),
            if produced_by.is_some() {
                " [cuttable]"
            } else {
                ""
            }
        )),
        W::Relational {
            kind, offending, ..
        } => out.push(format!(
            "{pad}Relational {kind:?}: {} offending pair(s)",
            offending.len()
        )),
        W::Closed { offenders, .. } => {
            out.push(format!(
                "{pad}Closed: {} disallowed triple(s)",
                offenders.len()
            ));
            for (p, o) in offenders {
                out.push(format!("{pad}  - {p} {o}"));
            }
        }
        W::Not { inner, .. } => {
            out.push(format!("{pad}Not โ€” falsify the inner shape:"));
            out.extend(render_sat(inner, px, indent + 2));
        }
        W::All { failed, .. } => {
            out.push(format!("{pad}All โ€” fix every:"));
            for f in failed {
                out.extend(render_witness(f, px, indent + 2));
            }
        }
        W::Any { branches, .. } => {
            out.push(format!("{pad}Any โ€” fix any one of:"));
            for b in branches {
                out.extend(render_witness(b, px, indent + 2));
            }
        }
        W::CountLow {
            path, have, min, ..
        } => out.push(format!(
            "{pad}CountLow along {}: have {have}, need {min}",
            path_str(path, px)
        )),
        W::CountHigh {
            path,
            matched,
            max,
            per_value,
            ..
        } => {
            out.push(format!(
                "{pad}CountHigh along {}: {} match(es), max {max}",
                path_str(path, px),
                matched.len()
            ));
            for (v, sub) in per_value {
                out.push(format!("{pad}  value {v}:"));
                out.extend(render_witness(sub, px, indent + 4));
            }
        }
        W::Opaque { .. } => out.push(format!("{pad}Opaque (SPARQL) โ€” no algebraic witness")),
    }
    out
}

fn render_sat(
    s: &shifty_engine::SatTrace,
    px: &shifty_algebra::Prefixes,
    indent: usize,
) -> Vec<String> {
    use shifty_engine::SatTrace as S;
    let pad = " ".repeat(indent);
    let mut out = Vec::new();
    match s {
        S::Irrefutable { .. } => out.push(format!("{pad}Irrefutable (โŠค)")),
        S::Atom { node, .. } => out.push(format!("{pad}Atom holds at {node} [cut to break]")),
        S::AllHeld { children, .. } => {
            out.push(format!("{pad}AllHeld โ€” break any one:"));
            for c in children {
                out.extend(render_sat(c, px, indent + 2));
            }
        }
        S::AnyHeld { satisfied, .. } => {
            out.push(format!("{pad}AnyHeld โ€” break every:"));
            for c in satisfied {
                out.extend(render_sat(c, px, indent + 2));
            }
        }
        S::CountHeld { matches, .. } => {
            out.push(format!("{pad}CountHeld: {} match(es)", matches.len()))
        }
        S::ForAllHeld { values, .. } => {
            out.push(format!(
                "{pad}ForAllHeld: {} checked value(s)",
                values.len()
            ));
            for (_, _, trace) in values {
                out.extend(render_sat(trace, px, indent + 2));
            }
        }
        S::NotHeld { inner_fails, .. } => {
            out.push(format!("{pad}NotHeld โ€” make the inner shape hold:"));
            out.extend(render_witness(inner_fails, px, indent + 2));
        }
        S::Blocked { reason, .. } => out.push(format!("{pad}Blocked: {reason:?}")),
        S::Coinductive { .. } => out.push(format!("{pad}Coinductive (gfp back-edge)")),
    }
    out
}

fn render_tree(
    t: &shifty_repair::RepairTree,
    arena: &shifty_algebra::ShapeArena,
    px: &shifty_algebra::Prefixes,
    indent: usize,
) -> Vec<String> {
    use shifty_repair::RepairTree as T;
    let pad = " ".repeat(indent);
    let mut out = Vec::new();
    match t {
        T::Noop(_) => out.push(format!("{pad}Noop")),
        T::Blocked(_, r) => out.push(format!("{pad}Blocked: {r:?}")),
        T::Edits { edits, holes, .. } => {
            out.push(format!("{pad}Edits:"));
            for e in edits {
                out.push(format!("{pad}  {}", edit_str(e)));
            }
            for (h, c) in holes {
                out.push(format!(
                    "{pad}  ?{} : {}",
                    h.0,
                    constraint_str(c, arena, px)
                ));
            }
        }
        T::All { children, .. } => {
            out.push(format!("{pad}All โ€” do all:"));
            for c in children {
                out.extend(render_tree(c, arena, px, indent + 2));
            }
        }
        T::Any { children, .. } => {
            out.push(format!("{pad}Any โ€” choose one:"));
            for c in children {
                out.extend(render_tree(c, arena, px, indent + 2));
            }
        }
        T::Repeat { body, min, max, .. } => {
            let hi = max.map_or_else(|| "โˆž".to_string(), |m| m.to_string());
            out.push(format!("{pad}Repeat [{min}..{hi}]:"));
            out.extend(render_tree(body, arena, px, indent + 2));
        }
    }
    out
}

fn edit_str(e: &shifty_repair::Edit) -> String {
    use shifty_repair::EditOp;
    let (sign, p) = match &e.op {
        EditOp::Add(p) => ("add", p),
        EditOp::Delete(p) => ("del", p),
    };
    format!(
        "{sign} {} {} {}",
        slot_str(&p.s),
        slot_str(&p.p),
        slot_str(&p.o)
    )
}

fn slot_str(s: &shifty_repair::Slot) -> String {
    match s {
        shifty_repair::Slot::Bound(t) => t.to_string(),
        shifty_repair::Slot::Open(h) => format!("?{}", h.0),
    }
}

fn constraint_str(
    c: &shifty_repair::HoleConstraint,
    arena: &shifty_algebra::ShapeArena,
    px: &shifty_algebra::Prefixes,
) -> String {
    use shifty_repair::HoleConstraint as H;
    match c {
        H::AnyNode => "any node".to_string(),
        H::Fresh => "fresh node".to_string(),
        H::Const(t) => format!("= {t}"),
        H::Typed(_) => "typed value".to_string(),
        H::Kind(_) => "nodeKind".to_string(),
        H::OneOf(v) => format!("one of {} value(s)", v.len()),
        H::ConformsTo(s) => shifty_algebra::render::describe_shape_in(arena, *s, px),
        H::ConformsToAll(ss) => shifty_algebra::render::describe_shapes_in(arena, ss, px),
    }
}

fn inspect(args: InspectArgs) -> Result<(), Box<dyn Error>> {
    let bytes = std::fs::read(&args.file)?;
    let base = args.base.as_deref();
    let source = args.file.to_string_lossy();
    let loaded = shifty_parse::load_rdf_auto(&bytes, None, Some(source.as_ref()), base)?;
    let out = shifty_parse::parse_loaded(&loaded);

    match args.stage {
        Stage::Rdf => match args.format {
            Format::Text => {
                let mut lines: Vec<String> = loaded.graph.iter().map(|t| t.to_string()).collect();
                lines.sort();
                for line in lines {
                    println!("{line}");
                }
            }
            Format::Json => {
                let triples: Vec<_> = loaded
                    .graph
                    .iter()
                    .map(|t| {
                        serde_json::json!({
                            "subject": t.subject.to_string(),
                            "predicate": t.predicate.to_string(),
                            "object": t.object.to_string(),
                        })
                    })
                    .collect();
                println!("{}", serde_json::to_string_pretty(&triples)?);
            }
            Format::Dot => {
                return Err(
                    "--format dot is only supported for --stage algebra or --stage normalized"
                        .into(),
                );
            }
        },
        Stage::Algebra => {
            match args.format {
                Format::Text => print!("{}", shifty_algebra::render::schema_to_text(&out.schema)),
                Format::Json => println!("{}", serde_json::to_string_pretty(&out.schema)?),
                Format::Dot => print!("{}", shifty_algebra::render::schema_to_dot(&out.schema)),
            }
            for d in &out.diagnostics {
                eprintln!("{d}");
            }
        }
        Stage::Normalized => {
            let schema = shifty_opt::normalize(&out.schema);
            match args.format {
                Format::Text => print!("{}", shifty_algebra::render::schema_to_text(&schema)),
                Format::Json => println!("{}", serde_json::to_string_pretty(&schema)?),
                Format::Dot => print!("{}", shifty_algebra::render::schema_to_dot(&schema)),
            }
            for d in &out.diagnostics {
                eprintln!("{d}");
            }
        }
        Stage::Strata => {
            let strat = shifty_opt::analyze(&out.schema.arena);
            match args.format {
                Format::Json => println!("{}", serde_json::to_string_pretty(&strat)?),
                Format::Text => print_strata(&strat),
                Format::Dot => {
                    return Err(
                        "--format dot is only supported for --stage algebra or --stage normalized"
                            .into(),
                    );
                }
            }
            for d in &out.diagnostics {
                eprintln!("{d}");
            }
        }
        Stage::Plan => {
            let normalized = shifty_opt::normalize(&out.schema);
            let physical = shifty_opt::plan(&normalized);
            match args.format {
                Format::Text => print!("{}", shifty_opt::plan::plan_to_text(&physical)),
                Format::Json => println!("{}", serde_json::to_string_pretty(&physical)?),
                Format::Dot => return Err("--format dot is not supported for --stage plan".into()),
            }
            for d in &out.diagnostics {
                eprintln!("{d}");
            }
        }
        Stage::Capability => {
            if !matches!(args.format, Format::Text) {
                return Err("--stage capability only supports --format text".into());
            }
            let normalized = shifty_opt::normalize(&out.schema);
            print_capability(&normalized);
            for d in &out.diagnostics {
                eprintln!("{d}");
            }
        }
        Stage::Access => {
            if matches!(args.format, Format::Dot) {
                return Err("--format dot is not supported for --stage access".into());
            }
            let functions = shifty_parse::collect_functions(&loaded);
            let catalog = shifty_opt::AccessCatalog::compile(&out.schema, &functions);
            match args.format {
                Format::Text => print_access(&catalog),
                Format::Json => println!("{}", serde_json::to_string_pretty(&catalog)?),
                Format::Dot => unreachable!(),
            }
            for d in &out.diagnostics {
                eprintln!("{d}");
            }
        }
    }
    Ok(())
}

fn print_access(catalog: &shifty_opt::AccessCatalog) {
    println!(
        "access: {} consumer(s), {} query identity/identities, {} path identity/identities",
        catalog.consumers.len(),
        catalog.queries.len(),
        catalog.paths.len()
    );
    for consumer in &catalog.consumers {
        println!("{:?}", consumer.consumer);
        println!("  default: {}", access_requirement(&consumer.default));
        println!("  shapes:  {}", access_requirement(&consumer.shapes));
        if !consumer.queries.is_empty() {
            println!("  queries: {:?}", consumer.queries);
        }
        if !consumer.paths.is_empty() {
            println!("  paths: {:?}", consumer.paths);
        }
        if !consumer.calls.is_empty() {
            let mut calls: Vec<_> = consumer.calls.iter().map(|iri| iri.as_str()).collect();
            calls.sort_unstable();
            println!("  functions: {}", calls.join(", "));
        }
        if consumer.writes.any_predicate || !consumer.writes.predicates.is_empty() {
            let mut writes: Vec<_> = consumer
                .writes
                .predicates
                .iter()
                .map(|iri| iri.as_str())
                .collect();
            writes.sort_unstable();
            if consumer.writes.any_predicate {
                writes.push("*");
            }
            println!("  writes: {}", writes.join(", "));
        }
    }
    for (index, query) in catalog.queries.iter().enumerate() {
        println!("query[{index}]: {}", query.text.replace(['\r', '\n'], " "));
    }
    for (index, path) in catalog.paths.iter().enumerate() {
        println!("path[{index}]: {:?}", path.path);
    }
}

fn access_requirement(requirement: &shifty_opt::AccessRequirement) -> String {
    let mut predicates: Vec<_> = requirement
        .predicates
        .iter()
        .map(|predicate| predicate.as_str())
        .collect();
    predicates.sort_unstable();
    let mut probes = Vec::new();
    for (enabled, name) in [
        (requirement.probes.forward, "forward"),
        (requirement.probes.reverse, "reverse"),
        (requirement.probes.membership, "membership"),
        (requirement.probes.open_scan, "open scan"),
    ] {
        if enabled {
            probes.push(name);
        }
    }
    format!(
        "predicates [{}{}]; probes [{}]; node domain {}; {}",
        predicates.join(", "),
        if requirement.any_predicate {
            if predicates.is_empty() { "*" } else { ", *" }
        } else {
            ""
        },
        probes.join(", "),
        requirement.reads_node_domain,
        if requirement.incomplete {
            "conservative/unknown"
        } else {
            "complete"
        }
    )
}

fn print_strata(strat: &shifty_opt::Stratification) {
    let recursive = strat.recursive().count();
    println!(
        "strata: stratifiable = {}; {} shape(s) in {} stratum(strata); {} recursive component(s)",
        strat.stratifiable,
        strat.shape_count(),
        strat.strata.len(),
        recursive,
    );
    let fmt = |shapes: &[shifty_algebra::ShapeId]| {
        shapes
            .iter()
            .map(|s| format!("@{}", s.0))
            .collect::<Vec<_>>()
            .join(" ")
    };
    if recursive > 0 {
        println!("recursive components (in dependency order):");
        for (level, s) in strat.strata.iter().enumerate() {
            if !s.recursive {
                continue;
            }
            let tag = if s.stratifiable {
                "positive recursion, ok"
            } else {
                "NON-STRATIFIABLE: recursion through negation"
            };
            println!("  stratum {level}: {}  ({tag})", fmt(&s.shapes));
        }
    }
}

fn print_capability(schema: &shifty_algebra::Schema) {
    use shifty_algebra::Shape;
    use shifty_opt::lower_query;
    use spargebra::SparqlParser;

    let mut sparql_queries: Vec<String> = Vec::new();
    for i in 0..schema.arena.len() {
        let id = shifty_algebra::ShapeId(i as u32);
        if let Shape::Sparql(c) = schema.arena.get(id) {
            sparql_queries.push(c.query.clone());
        }
    }

    // `lower_query` is the routing gate: a query runs on the native executor iff
    // it lowers to a native plan, otherwise it falls back to Spareval. This
    // reports what actually happens, not the broader designed subset (which lives
    // in docs/05-sparql-execution.md ยง129-141).
    let lowered_count = sparql_queries
        .iter()
        .filter(|q| {
            SparqlParser::new()
                .parse_query(q)
                .map(|parsed| lower_query(&parsed).is_ok())
                .unwrap_or(false)
        })
        .count();

    println!(
        "capability: {} SPARQL constraint query/queries ({} native, {} fall back)",
        sparql_queries.len(),
        lowered_count,
        sparql_queries.len() - lowered_count,
    );

    for (i, q) in sparql_queries.iter().enumerate() {
        match SparqlParser::new().parse_query(q) {
            Ok(parsed) => {
                let tag = match lower_query(&parsed) {
                    Ok(_) => "NATIVE".to_string(),
                    Err(reason) => format!("FALLBACK ({reason})"),
                };
                println!("  [{i}] {tag}:\n{q}");
            }
            Err(e) => println!("  [{i}] PARSE ERROR: {e}"),
        }
    }
}