quarto-source-map 0.1.0

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

use crate::types::{FileId, Range};
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use std::sync::Arc;

/// Source information tracking a location and its transformation history
///
/// This enum stores only byte offsets. Row and column information is computed
/// on-demand via `map_offset()` using the FileInformation line break index.
///
/// Design notes:
/// - Original: Points directly to a file with byte offsets
/// - Substring: Points to a range within a parent SourceInfo (offsets are relative to parent)
/// - Concat: Combines multiple SourceInfo pieces (preserves provenance when coalescing text)
/// - Generated: Produced by a pipeline transform. `by` records the producer; `from`
///   records source-side anchors (empty for pure synthesis, `Invocation` for
///   shortcode-style resolutions).
///
/// The Transformed variant was removed because it's not used in production code.
/// Text transformations (smart quotes, em-dashes) use Original SourceInfo pointing
/// to the pre-transformation text, accepting that the byte offsets are approximate.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SourceInfo {
    /// Direct position in an original file
    ///
    /// Stores only byte offsets. Use `map_offset()` to get row/column information.
    Original {
        file_id: FileId,
        start_offset: usize,
        end_offset: usize,
    },
    /// Substring extraction from a parent source
    ///
    /// Offsets are relative to the parent's text.
    /// The chain of Substrings always resolves to an Original.
    Substring {
        parent: Arc<SourceInfo>,
        start_offset: usize,
        end_offset: usize,
    },
    /// Concatenation of multiple sources
    ///
    /// Used when coalescing adjacent text nodes while preserving
    /// the fact that they came from different source locations.
    Concat { pieces: Vec<SourcePiece> },
    /// Node produced by a pipeline transform
    ///
    /// `by` records the producer ("which transform made me"); `from` is a
    /// list of typed, role-labeled source-info pointers ("which source
    /// bytes contributed to me"). Empty `from` means pure synthesis
    /// (sectionize wrappers, filter constructions, title-block h1).
    /// An `Invocation` anchor present means there is a source-side
    /// preimage (every shortcode resolution).
    Generated {
        by: By,
        #[serde(default, skip_serializing_if = "SmallVec::is_empty")]
        from: SmallVec<[Anchor; 2]>,
    },
}

/// Producer identity for a [`SourceInfo::Generated`] node.
///
/// `kind` is a short, kebab-case identifier describing which transform
/// produced the node ("filter", "shortcode", "sectionize", ...). Third
/// parties should namespace as `ext/<extension>/<kind>`.
///
/// `data` is per-kind configuration that is **not** a source-info pointer.
/// Source-side anchors live in the parent `Generated.from` list, not here.
/// `Null` for kinds that don't carry per-instance data.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct By {
    /// Short kind tag, kebab-case. Examples: "filter", "shortcode",
    /// "sectionize", "user-edit", "title-block".
    /// Third-party kinds should namespace: "ext/my-extension/foo".
    pub kind: String,

    /// Per-kind configuration that is NOT a source-info pointer.
    /// Anchors live in `Generated.from`, not here.
    /// `Null` for kinds that don't carry per-instance data.
    #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
    pub data: serde_json::Value,
}

/// Role describing what kind of source-side contribution an anchor records.
///
/// The known roles are load-bearing — `Invocation` is what the writer's
/// preimage walk and attribution consult; `ValueSource` is diagnostic-only.
/// `Other(String)` is an open escape hatch for extension-defined roles.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AnchorRole {
    /// The user-written construct that triggered this node's creation
    /// (e.g. the `{{< meta foo >}}` token in the active document).
    /// Load-bearing: the writer's `preimage_in` and attribution's
    /// `resolve_byte_range` consult the first anchor with this role.
    /// At most one per node by convention.
    Invocation,

    /// Where the VALUE this node carries was defined, when distinct
    /// from the invocation site (e.g. `footer:` in `_metadata.yml` for
    /// a `{{< meta footer >}}` resolution). Diagnostic-only — does not
    /// affect the writer or attribution decisions in v1.
    ValueSource,

    /// Extension-defined or future role we haven't enumerated.
    /// String is kebab-case, namespaced (`ext/<name>/<role>`).
    ///
    /// **`preimage_in` does not walk this role.** Future anchor roles
    /// default to non-walked unless explicitly added to
    /// [`SourceInfo::preimage_in`]'s `Generated` arm. Extensions adding
    /// `Other("…")` should treat this as a feature: attribution data
    /// attached via `Other` is not accidentally consulted by the writer's
    /// byte-copying path. If a role *does* contribute to body-text
    /// preimage in `target`, it must be explicitly enumerated in
    /// `preimage_in`.
    Other(String),
}

/// A single typed, role-labeled source-info pointer attached to a
/// [`SourceInfo::Generated`] node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Anchor {
    pub role: AnchorRole,
    pub source_info: Arc<SourceInfo>,
}

/// A piece of a concatenated source
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SourcePiece {
    /// Source information for this piece
    pub source_info: SourceInfo,
    /// Where this piece starts in the concatenated string
    pub offset_in_concat: usize,
    /// Length of this piece
    pub length: usize,
}

impl Default for SourceInfo {
    fn default() -> Self {
        SourceInfo::Original {
            file_id: FileId(0),
            start_offset: 0,
            end_offset: 0,
        }
    }
}

impl SourceInfo {
    /// Deprecated: use `SourceInfo::for_test()` in tests or an explicit
    /// `Generated{by: <kind>}` in production. See provenance-contract.md.
    ///
    /// This inherent method shadows `Default::default()` so that callers
    /// writing `SourceInfo::default()` see a deprecation error under
    /// `deny(deprecated)`. The trait impl is retained (and called by this
    /// method) so that `unwrap_or_default()` and `#[derive(Default)]` still
    /// compile; those are caught by separate grep tooling.
    #[deprecated(
        since = "0.1.0",
        note = "Use SourceInfo::for_test() in tests, or the appropriate Generated{by: <kind>} in production. See provenance-contract.md."
    )]
    #[doc(hidden)]
    // Intentionally shadows `Default::default` (see the doc comment above): this
    // deprecated inherent method is the provenance-contract tripwire, kept so
    // `unwrap_or_default()`/`#[derive(Default)]` still compile while flagging
    // direct calls. The name must match the trait method, so the lint is moot.
    #[allow(clippy::should_implement_trait)]
    pub fn default() -> Self {
        <Self as Default>::default()
    }

    /// Create source info for a position in an original file (from offsets)
    pub fn original(file_id: FileId, start_offset: usize, end_offset: usize) -> Self {
        SourceInfo::Original {
            file_id,
            start_offset,
            end_offset,
        }
    }

    /// Create source info for a position in an original file (from Range)
    ///
    /// This is a compatibility helper for code that still uses Range.
    /// The row and column information in the Range is ignored; only offsets are stored.
    pub fn from_range(file_id: FileId, range: Range) -> Self {
        SourceInfo::Original {
            file_id,
            start_offset: range.start.offset,
            end_offset: range.end.offset,
        }
    }

    /// Create source info for a substring extraction
    pub fn substring(parent: SourceInfo, start: usize, end: usize) -> Self {
        SourceInfo::Substring {
            parent: Arc::new(parent),
            start_offset: start,
            end_offset: end,
        }
    }

    /// Create source info for concatenated sources
    pub fn concat(pieces: Vec<(SourceInfo, usize)>) -> Self {
        let source_pieces: Vec<SourcePiece> = pieces
            .into_iter()
            .map(|(source_info, length)| SourcePiece {
                source_info,
                offset_in_concat: 0, // Will be calculated based on cumulative lengths
                length,
            })
            .collect();

        // Calculate cumulative offsets
        let mut cumulative_offset = 0;
        let pieces_with_offsets: Vec<SourcePiece> = source_pieces
            .into_iter()
            .map(|mut piece| {
                piece.offset_in_concat = cumulative_offset;
                cumulative_offset += piece.length;
                piece
            })
            .collect();

        SourceInfo::Concat {
            pieces: pieces_with_offsets,
        }
    }

    /// Create a [`SourceInfo::Generated`] with an empty anchor list.
    ///
    /// Use [`SourceInfo::append_anchor`] to add anchors after construction.
    /// For Generated nodes that need to carry anchors at construction
    /// time, build the variant directly: `SourceInfo::Generated { by, from }`.
    pub fn generated(by: By) -> Self {
        SourceInfo::Generated {
            by,
            from: SmallVec::new(),
        }
    }

    /// Convenience for tests: produce a non-atomic `Generated` source_info
    /// with `By::test_scaffold()` and no anchors. Use this in test code
    /// where a constructor requires a `SourceInfo` but there's no real
    /// provenance to record. Replaces the historical
    /// `SourceInfo::default()` pattern in tests.
    pub fn for_test() -> Self {
        SourceInfo::Generated {
            by: By::test_scaffold(),
            from: SmallVec::new(),
        }
    }

    /// If this is a [`SourceInfo::Generated`], return the first anchor whose
    /// role is [`AnchorRole::Invocation`].
    ///
    /// Returns `None` otherwise (including for non-`Generated` variants).
    /// By convention there is at most one `Invocation` anchor per node.
    pub fn invocation_anchor(&self) -> Option<&Arc<SourceInfo>> {
        match self {
            SourceInfo::Generated { from, .. } => from
                .iter()
                .find(|a| matches!(a.role, AnchorRole::Invocation))
                .map(|a| &a.source_info),
            _ => None,
        }
    }

    /// If this is a [`SourceInfo::Generated`], return the first anchor whose
    /// role is [`AnchorRole::ValueSource`].
    ///
    /// Returns `None` otherwise. By convention there is at most one
    /// `ValueSource` anchor per node.
    pub fn value_source_anchor(&self) -> Option<&Arc<SourceInfo>> {
        match self {
            SourceInfo::Generated { from, .. } => from
                .iter()
                .find(|a| matches!(a.role, AnchorRole::ValueSource))
                .map(|a| &a.source_info),
            _ => None,
        }
    }

    /// Iterate over every anchor in this [`SourceInfo::Generated`] whose role
    /// equals `role`.
    ///
    /// Returns an empty iterator for non-`Generated` variants. Iteration order
    /// is the append order.
    pub fn anchors_with_role<'a>(
        &'a self,
        role: &'a AnchorRole,
    ) -> Box<dyn Iterator<Item = &'a Arc<SourceInfo>> + 'a> {
        match self {
            SourceInfo::Generated { from, .. } => Box::new(
                from.iter()
                    .filter(move |a| &a.role == role)
                    .map(|a| &a.source_info),
            ),
            _ => Box::new(std::iter::empty()),
        }
    }

    /// Append `(role, source_info)` to this [`SourceInfo::Generated`]'s
    /// anchor list.
    ///
    /// Panics if `self` is not [`SourceInfo::Generated`]. By convention there
    /// is at most one anchor per known role; appending a second anchor with
    /// the same role does not replace the first — accessors that find by
    /// role return the earliest match.
    pub fn append_anchor(&mut self, role: AnchorRole, source_info: Arc<SourceInfo>) {
        match self {
            SourceInfo::Generated { from, .. } => {
                from.push(Anchor { role, source_info });
            }
            _ => panic!("append_anchor called on non-Generated SourceInfo"),
        }
    }

    /// Combine two SourceInfo objects representing adjacent text
    ///
    /// This creates a Concat mapping that preserves both sources.
    /// The resulting SourceInfo spans from the start of self to the end of other.
    pub fn combine(&self, other: &SourceInfo) -> Self {
        let self_length = self.length();
        let other_length = other.length();

        SourceInfo::concat(vec![
            (self.clone(), self_length),
            (other.clone(), other_length),
        ])
    }

    /// Get the length (in bytes) represented by this SourceInfo
    pub fn length(&self) -> usize {
        match self {
            SourceInfo::Original {
                start_offset,
                end_offset,
                ..
            } => end_offset - start_offset,
            SourceInfo::Substring {
                start_offset,
                end_offset,
                ..
            } => end_offset - start_offset,
            SourceInfo::Concat { pieces } => pieces.iter().map(|p| p.length).sum(),
            SourceInfo::Generated { .. } => 0,
        }
    }

    /// Get the start offset for this SourceInfo
    ///
    /// For Original and Substring, returns the start_offset field.
    /// For Concat, returns 0 (the concat represents a new text starting at 0).
    /// For Generated, returns 0.
    pub fn start_offset(&self) -> usize {
        match self {
            SourceInfo::Original { start_offset, .. } => *start_offset,
            SourceInfo::Substring { start_offset, .. } => *start_offset,
            SourceInfo::Concat { .. } => 0,
            SourceInfo::Generated { .. } => 0,
        }
    }

    /// Get the end offset for this SourceInfo
    ///
    /// For Original and Substring, returns the end_offset field.
    /// For Concat, returns the total length.
    /// For Generated, returns 0.
    pub fn end_offset(&self) -> usize {
        match self {
            SourceInfo::Original { end_offset, .. } => *end_offset,
            SourceInfo::Substring { end_offset, .. } => *end_offset,
            SourceInfo::Concat { .. } => self.length(),
            SourceInfo::Generated { .. } => 0,
        }
    }

    /// Chain-resolve to `(file_id, start_offset, end_offset)` in the
    /// root source file.
    ///
    /// Returns `None` for `Concat` — Concat doesn't map cleanly to a
    /// single contiguous byte range. For `Generated`, delegates to the
    /// first `Invocation` anchor and recurses (`None` when no
    /// `Invocation` anchor is present). The attribution v1 sidecar
    /// relies on this contract; project-scoped (v2) features that need
    /// the full chain resolver should use `map_offset` against a
    /// `SourceContext` instead.
    pub fn resolve_byte_range(&self) -> Option<(usize, usize, usize)> {
        match self {
            SourceInfo::Original {
                file_id,
                start_offset,
                end_offset,
            } => Some((file_id.0, *start_offset, *end_offset)),
            SourceInfo::Substring {
                parent,
                start_offset,
                end_offset,
            } => {
                let (fid, parent_start, _) = parent.resolve_byte_range()?;
                Some((fid, parent_start + start_offset, parent_start + end_offset))
            }
            SourceInfo::Concat { .. } => None,
            SourceInfo::Generated { .. } => self
                .invocation_anchor()
                .and_then(|si| si.resolve_byte_range()),
        }
    }

    /// Byte range in `target` that this `SourceInfo`'s preimage covers, if any.
    ///
    /// This is the writer's "can I Verbatim-copy bytes from `target` for the
    /// node carrying this source_info?" check.
    ///
    /// Semantics by variant:
    /// - `Original` → `Some(start..end)` iff the file matches `target`, else `None`.
    /// - `Substring` → recurse the parent; offsets compose additively.
    /// - `Concat` → every piece must resolve into `target` AND the resolved
    ///   ranges must be byte-contiguous (no gaps, no overlaps). A gappy Concat
    ///   returns `None` — the writer can't Verbatim-copy a non-contiguous span.
    /// - `Generated` → walk the `Invocation` anchor only via
    ///   [`invocation_anchor`](Self::invocation_anchor). **No other anchor
    ///   role is consulted** — not `ValueSource` (Plan 9), not future
    ///   `Dispatch` (Plan 10), not `AnchorRole::Other`. See the
    ///   role-asymmetry section below.
    ///
    /// # Role asymmetry
    ///
    /// `preimage_in` only walks `AnchorRole::Invocation`. This is load-bearing:
    /// copying bytes from a `ValueSource` source range would emit raw YAML
    /// metadata (or whatever the value lived in) into the body — a hard
    /// correctness bug. The same applies to `Dispatch` (which points at Lua
    /// source) and to any extension-defined `Other` role.
    ///
    /// **Future anchor roles default to non-walked.** Extensions introducing
    /// `AnchorRole::Other("…")` should treat this as a feature: their
    /// attribution metadata is not accidentally consulted by the writer's
    /// byte-copying path. If a role *does* contribute to body-text preimage,
    /// it must be explicitly added to this function's `Generated` arm.
    pub fn preimage_in(&self, target: FileId) -> Option<std::ops::Range<usize>> {
        match self {
            SourceInfo::Original {
                file_id,
                start_offset,
                end_offset,
            } if *file_id == target => Some(*start_offset..*end_offset),
            SourceInfo::Original { .. } => None,
            SourceInfo::Substring {
                parent,
                start_offset,
                end_offset,
            } => {
                let parent_range = parent.preimage_in(target)?;
                Some(parent_range.start + start_offset..parent_range.start + end_offset)
            }
            SourceInfo::Concat { pieces } => {
                let ranges: Vec<std::ops::Range<usize>> = pieces
                    .iter()
                    .map(|p| p.source_info.preimage_in(target))
                    .collect::<Option<Vec<_>>>()?;
                if ranges.is_empty() {
                    return None;
                }
                if ranges.windows(2).all(|w| w[0].end == w[1].start) {
                    let first = ranges.first().unwrap().start;
                    let last = ranges.last().unwrap().end;
                    Some(first..last)
                } else {
                    None
                }
            }
            SourceInfo::Generated { .. } => self
                .invocation_anchor()
                .and_then(|si| si.preimage_in(target)),
        }
    }

    /// Remap every `FileId` referenced by this `SourceInfo` (including those
    /// inside `Substring` parents and `Concat` pieces) using the provided
    /// mapping function.
    ///
    /// Used when merging ASTs that were parsed against different files into a
    /// single `ASTContext` with a shared filename table — callers shift each
    /// AST's `FileId`s to their slot in the merged table before combining.
    pub fn remap_file_ids<F>(&mut self, map: &F)
    where
        F: Fn(FileId) -> FileId,
    {
        match self {
            SourceInfo::Original { file_id, .. } => {
                *file_id = map(*file_id);
            }
            SourceInfo::Substring { parent, .. } => {
                // Arc::make_mut clones if there are other references.
                let parent = Arc::make_mut(parent);
                parent.remap_file_ids(map);
            }
            SourceInfo::Concat { pieces } => {
                for piece in pieces {
                    piece.source_info.remap_file_ids(map);
                }
            }
            SourceInfo::Generated { from, .. } => {
                for anchor in from {
                    // Arc::make_mut clones if there are other references.
                    let inner = Arc::make_mut(&mut anchor.source_info);
                    inner.remap_file_ids(map);
                }
            }
        }
    }

    /// First `FileId` reachable from this `SourceInfo`'s root.
    ///
    /// - `Original` → `Some(file_id)`.
    /// - `Substring` → recurse parent.
    /// - `Concat` → `pieces.iter().find_map(|p| p.source_info.root_file_id())`
    ///   (`find_map` semantics — skips Generated holes and empty pieces).
    /// - `Generated` → `invocation_anchor().and_then(|si| si.root_file_id())`;
    ///   `None` when no `Invocation` anchor is present.
    pub fn root_file_id(&self) -> Option<FileId> {
        match self {
            SourceInfo::Original { file_id, .. } => Some(*file_id),
            SourceInfo::Substring { parent, .. } => parent.root_file_id(),
            SourceInfo::Concat { pieces } => {
                pieces.iter().find_map(|p| p.source_info.root_file_id())
            }
            SourceInfo::Generated { .. } => {
                self.invocation_anchor().and_then(|si| si.root_file_id())
            }
        }
    }

    /// Insert every `FileId` reachable from this `SourceInfo` into `out`.
    ///
    /// Walks every `Original`, every `Substring` parent, every `Concat`
    /// piece, and every `Generated` anchor (all roles — `Invocation`,
    /// `ValueSource`, `Other`).
    pub fn collect_file_ids(&self, out: &mut std::collections::HashSet<FileId>) {
        match self {
            SourceInfo::Original { file_id, .. } => {
                out.insert(*file_id);
            }
            SourceInfo::Substring { parent, .. } => parent.collect_file_ids(out),
            SourceInfo::Concat { pieces } => {
                for piece in pieces {
                    piece.source_info.collect_file_ids(out);
                }
            }
            SourceInfo::Generated { from, .. } => {
                for anchor in from {
                    anchor.source_info.collect_file_ids(out);
                }
            }
        }
    }
}

impl By {
    /// Producer kind for a node constructed by a Lua filter
    /// (e.g. `pandoc.Str("decoration")` inside a filter callback).
    ///
    /// `filter_path` is the path the Lua engine reported via
    /// `debug.getinfo(...).source` (with the leading "@" stripped);
    /// `line` is the line number inside that file where the constructor
    /// ran. Until Lua-file-registration lands (bd-36fr9), `(filter_path,
    /// line)` lives in `by.data`; afterwards it migrates to a `Dispatch`
    /// anchor and `by.data` shrinks to `{}`.
    pub fn filter(filter_path: impl Into<String>, line: usize) -> Self {
        Self {
            kind: "filter".to_string(),
            data: serde_json::json!({
                "filter_path": filter_path.into(),
                "line": line,
            }),
        }
    }

    /// Producer kind for the `SectionizeTransform`'s synthesized section
    /// Divs. Children remain editable; the wrapper itself is structural.
    pub fn sectionize() -> Self {
        Self {
            kind: "sectionize".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for React-constructed (user-typed) content reaching
    /// the AST through the q2-preview client.
    pub fn user_edit() -> Self {
        Self {
            kind: "user-edit".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for shortcode resolutions.
    ///
    /// **Invariant.** Every `Generated { by: shortcode(...), .. }` must
    /// carry at least one `Invocation` anchor in `from` pointing at the
    /// source token's byte range. Use only inside a `Generated` whose
    /// anchor list is populated; constructing the bare shape with empty
    /// `from` is rejected by Plan 6's audit-completion test and trips
    /// Plan 7's writer `debug_assert!`.
    pub fn shortcode(name: impl Into<String>) -> Self {
        Self {
            kind: "shortcode".to_string(),
            data: serde_json::json!({ "name": name.into() }),
        }
    }

    /// Producer kind for `IncludeStage`'s expansion wrapper. Note that
    /// most include-related synthesized content keeps its `Original`
    /// `source_info` (inherited from the include-line Paragraph) — this
    /// kind is only used where a `Generated` is explicitly required.
    pub fn include() -> Self {
        Self {
            kind: "include".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for the title-block stage's synthesized title `h1`.
    pub fn title_block() -> Self {
        Self {
            kind: "title-block".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for the footnotes stage's container Div.
    pub fn footnotes() -> Self {
        Self {
            kind: "footnotes".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for `RevealSlidesTransform`'s synthesized slide
    /// structure — title-slide Div, section wrappers, speaker-notes Div,
    /// and any other chrome built from the slide-level heading tree.
    /// Non-atomic: the slide container is structural chrome; the content
    /// inside (headings, paragraphs) retains its own source_info.
    pub fn revealjs() -> Self {
        Self {
            kind: "revealjs".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for the appendix-structure stage's wrapper Div.
    pub fn appendix() -> Self {
        Self {
            kind: "appendix".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for parser-side synthetic Spaces inserted by the
    /// tree-sitter post-processing pass.
    pub fn tree_sitter_postprocess() -> Self {
        Self {
            kind: "tree-sitter-postprocess".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// "We don't know" placeholder used by `json::read_completing_source_info`
    /// when a node arrives without an `s:` field from outside the q2
    /// source-tracking world (qmd-syntax-helper Pandoc subprocess, CLI
    /// `--from json`, external filter binaries, Lua AST handoff).
    ///
    /// Non-atomic by design — nodes carrying `By::unknown()` remain
    /// editable in the preview; user edits re-stamp them as `user_edit`
    /// on save. See Plan 7f Phase 4's per-caller table for placement
    /// guidance.
    pub fn unknown() -> Self {
        Self {
            kind: "unknown".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for test scaffolding. Non-atomic; appears only in
    /// test code where `source_info` is required by a constructor but
    /// has no real provenance to record. Paired with
    /// [`SourceInfo::for_test`].
    pub fn test_scaffold() -> Self {
        Self {
            kind: "test-scaffold".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for citeproc-rendered content (citation Str
    /// replacements, bibliography `Div`s, `#refs` wrappers). The bytes
    /// come from CSL processing of bibliographic metadata, not from
    /// user-written source.
    ///
    /// Atomic — citeproc output is generated content the user can't
    /// edit through the preview; changes go through the CSL pipeline,
    /// not through inline editing.
    pub fn citeproc() -> Self {
        Self {
            kind: "citeproc".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for content synthesized from execution-engine
    /// output (Jupyter cell stdout / stderr, rich-display MIME bundles,
    /// kernel error tracebacks). The bytes come from kernel execution,
    /// not from user-written source.
    ///
    /// Atomic — execution outputs are regenerated on every re-run;
    /// editing them through the preview would be a UX bug.
    pub fn jupyter_output() -> Self {
        Self {
            kind: "jupyter-output".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Producer kind for callout-decoration synthesis:
    /// default-title injection (`Note`, `Warning`, etc. when the user
    /// omits a title and `appearance="default"`) and the
    /// screen-reader-only type announcement span.
    ///
    /// Non-atomic — the wrapper Div is structural, and its children
    /// (the user's actual callout body) remain editable through the
    /// preview. The synthesized title text itself has no preimage but
    /// regenerates from the callout type when the user changes it,
    /// so atomicity at the wrapper level would be incorrect.
    pub fn callout() -> Self {
        Self {
            kind: "callout".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Empty-Map sentinel `ConfigValue` used during metadata merging
    /// when no value is present. Non-atomic. The bytes don't exist —
    /// the node is structural. See [`By::is_programmatic_sentinel`].
    pub fn config_default() -> Self {
        Self {
            kind: "config-default".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// Programmatic construction of `ConfigValue` (e.g.
    /// `ConfigValue::from_path`, intermediate maps created during
    /// `insert_path`). No source bytes exist for these nodes.
    /// See [`By::is_programmatic_sentinel`].
    pub fn programmatic_config() -> Self {
        Self {
            kind: "programmatic-config".to_string(),
            data: serde_json::Value::Null,
        }
    }

    /// True for kinds whose source bytes don't exist — `config-default`,
    /// `programmatic-config`, `unknown`. Used by code that needs to
    /// distinguish "no real source" sentinels from a genuine
    /// `Original{FileId(0), …}` pointing at a real document.
    pub fn is_programmatic_sentinel(&self) -> bool {
        matches!(
            self.kind.as_str(),
            "config-default" | "programmatic-config" | "unknown"
        )
    }

    /// Escape-hatch constructor for any `kind` string — including built-in
    /// names and extension-defined kinds (`ext/<extension>/<kind>`).
    ///
    /// Forgery (an extension calling `By::raw("shortcode", …)` without the
    /// required `Invocation` anchor) is caught downstream by Plan 6's
    /// audit-completion test and Plan 7's `debug_assert!`. The convention
    /// for third-party kinds is `ext/<extension>/<kind>`.
    pub fn raw(kind: impl Into<String>, data: serde_json::Value) -> Self {
        Self {
            kind: kind.into(),
            data,
        }
    }

    /// True if a `Generated { by: <self>, .. }` node should be treated
    /// as atomic by the incremental writer.
    ///
    /// Atomic nodes are produced by the pipeline and represent content
    /// the user shouldn't edit through React (filter constructions,
    /// shortcode resolutions, synthesized title h1, tree-sitter-inserted
    /// spaces). Atomicity is determined by `kind` alone — orthogonal to
    /// anchor-presence.
    ///
    /// Extensions that contribute new `by.kind` values are not atomic by
    /// default in v1.
    pub fn is_atomic_kind(&self) -> bool {
        matches!(
            self.kind.as_str(),
            "filter"
                | "shortcode"
                | "title-block"
                | "tree-sitter-postprocess"
                | "citeproc"
                | "jupyter-output"
        )
    }

    /// True if this `By`'s `kind` equals `kind`.
    pub fn is_kind(&self, kind: &str) -> bool {
        self.kind == kind
    }

    /// If `self.kind == "filter"`, return `(filter_path, line)`.
    ///
    /// Returns `None` for any other kind, or when the data payload is
    /// malformed (missing or non-string `filter_path`, missing or
    /// non-integer `line`).
    pub fn as_filter(&self) -> Option<(&str, usize)> {
        if self.kind != "filter" {
            return None;
        }
        let path = self.data.get("filter_path")?.as_str()?;
        let line = self.data.get("line")?.as_u64()? as usize;
        Some((path, line))
    }
}

impl Anchor {
    /// Construct an [`AnchorRole::Invocation`] anchor.
    pub fn invocation(source_info: Arc<SourceInfo>) -> Self {
        Self {
            role: AnchorRole::Invocation,
            source_info,
        }
    }

    /// Construct an [`AnchorRole::ValueSource`] anchor.
    pub fn value_source(source_info: Arc<SourceInfo>) -> Self {
        Self {
            role: AnchorRole::ValueSource,
            source_info,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{FileId, Location, Range};

    #[test]
    fn test_original_source_info() {
        let file_id = FileId(0);
        let range = Range {
            start: Location {
                offset: 0,
                row: 0,
                column: 0,
            },
            end: Location {
                offset: 10,
                row: 0,
                column: 10,
            },
        };

        let info = SourceInfo::from_range(file_id, range.clone());

        assert_eq!(info.start_offset(), 0);
        assert_eq!(info.end_offset(), 10);
        assert_eq!(info.length(), 10);
        match info {
            SourceInfo::Original {
                file_id: mapped_id, ..
            } => {
                assert_eq!(mapped_id, file_id);
            }
            _ => panic!("Expected Original mapping"),
        }
    }

    #[test]
    fn test_remap_file_ids_original() {
        let mut info = SourceInfo::original(FileId(0), 0, 10);
        info.remap_file_ids(&|id| FileId(id.0 + 1));
        match info {
            SourceInfo::Original { file_id, .. } => assert_eq!(file_id, FileId(1)),
            _ => panic!("Expected Original"),
        }
    }

    #[test]
    fn test_remap_file_ids_substring() {
        let parent = SourceInfo::original(FileId(0), 0, 100);
        let mut info = SourceInfo::substring(parent, 5, 20);
        info.remap_file_ids(&|id| FileId(id.0 + 7));
        match info {
            SourceInfo::Substring { parent, .. } => match &*parent {
                SourceInfo::Original { file_id, .. } => assert_eq!(*file_id, FileId(7)),
                _ => panic!("Expected Original parent"),
            },
            _ => panic!("Expected Substring"),
        }
    }

    #[test]
    fn test_remap_file_ids_concat() {
        let a = SourceInfo::original(FileId(0), 0, 5);
        let b = SourceInfo::original(FileId(3), 5, 10);
        let mut info = SourceInfo::concat(vec![(a, 5), (b, 5)]);
        info.remap_file_ids(&|id| FileId(id.0 + 10));
        match info {
            SourceInfo::Concat { pieces } => {
                match &pieces[0].source_info {
                    SourceInfo::Original { file_id, .. } => assert_eq!(*file_id, FileId(10)),
                    _ => panic!("Expected Original"),
                }
                match &pieces[1].source_info {
                    SourceInfo::Original { file_id, .. } => assert_eq!(*file_id, FileId(13)),
                    _ => panic!("Expected Original"),
                }
            }
            _ => panic!("Expected Concat"),
        }
    }

    #[test]
    fn test_remap_file_ids_generated_empty_from_is_noop() {
        let mut info = SourceInfo::generated(By::filter("foo.lua", 42));
        info.remap_file_ids(&|_| FileId(99));
        match info {
            SourceInfo::Generated { by, from } => {
                assert!(from.is_empty());
                let (path, line) = by.as_filter().unwrap();
                assert_eq!(path, "foo.lua");
                assert_eq!(line, 42);
            }
            _ => panic!("Expected Generated"),
        }
    }

    // -------------------------------------------------------------------------
    // Plan 4 — By / Anchor / Generated coverage
    // -------------------------------------------------------------------------

    #[test]
    fn test_by_filter_builder() {
        let by = By::filter("a.lua", 7);
        assert_eq!(by.kind, "filter");
        assert_eq!(by.as_filter(), Some(("a.lua", 7)));
    }

    #[test]
    fn test_by_sectionize_builder() {
        let by = By::sectionize();
        assert_eq!(by.kind, "sectionize");
        assert!(by.data.is_null());
    }

    #[test]
    fn test_by_user_edit_builder() {
        assert_eq!(By::user_edit().kind, "user-edit");
    }

    #[test]
    fn test_by_shortcode_builder_records_name() {
        let by = By::shortcode("meta");
        assert_eq!(by.kind, "shortcode");
        assert_eq!(by.data.get("name").and_then(|v| v.as_str()), Some("meta"));
    }

    #[test]
    fn test_by_include_title_footnotes_appendix_tree_sitter_builders() {
        assert_eq!(By::include().kind, "include");
        assert_eq!(By::title_block().kind, "title-block");
        assert_eq!(By::footnotes().kind, "footnotes");
        assert_eq!(By::appendix().kind, "appendix");
        assert_eq!(
            By::tree_sitter_postprocess().kind,
            "tree-sitter-postprocess"
        );
    }

    #[test]
    fn test_by_raw_builder_accepts_any_kind() {
        let by = By::raw("ext/my-plugin/foo", serde_json::json!({"k": 1}));
        assert_eq!(by.kind, "ext/my-plugin/foo");
        assert_eq!(by.data.get("k").and_then(|v| v.as_u64()), Some(1));
    }

    #[test]
    fn test_by_is_atomic_kind() {
        assert!(By::filter("x.lua", 1).is_atomic_kind());
        assert!(By::shortcode("meta").is_atomic_kind());
        assert!(By::title_block().is_atomic_kind());
        assert!(By::tree_sitter_postprocess().is_atomic_kind());
        assert!(By::citeproc().is_atomic_kind());
        assert!(By::jupyter_output().is_atomic_kind());

        assert!(!By::callout().is_atomic_kind());

        assert!(!By::sectionize().is_atomic_kind());
        assert!(!By::user_edit().is_atomic_kind());
        assert!(!By::include().is_atomic_kind());
        assert!(!By::footnotes().is_atomic_kind());
        assert!(!By::appendix().is_atomic_kind());
        assert!(!By::unknown().is_atomic_kind());
        assert!(!By::test_scaffold().is_atomic_kind());
        assert!(!By::config_default().is_atomic_kind());
        assert!(!By::programmatic_config().is_atomic_kind());
        assert!(!By::raw("ext/anywhere/foo", serde_json::Value::Null).is_atomic_kind());
    }

    #[test]
    fn test_by_unknown_constructor() {
        let by = By::unknown();
        assert_eq!(by.kind, "unknown");
        assert!(by.data.is_null());
        // Non-atomic — nodes carrying By::unknown() remain editable; the
        // strict reader rejects missing `s:`, the completing reader stamps
        // them with this kind only at the explicit call site.
        assert!(!by.is_atomic_kind());
    }

    #[test]
    fn test_by_test_scaffold_constructor() {
        let by = By::test_scaffold();
        assert_eq!(by.kind, "test-scaffold");
        assert!(by.data.is_null());
        assert!(!by.is_atomic_kind());
        // Not a "no real source" sentinel — it's test scaffolding.
        assert!(!by.is_programmatic_sentinel());
    }

    #[test]
    fn test_by_config_default_constructor() {
        let by = By::config_default();
        assert_eq!(by.kind, "config-default");
        assert!(by.data.is_null());
        assert!(!by.is_atomic_kind());
    }

    #[test]
    fn test_by_programmatic_config_constructor() {
        let by = By::programmatic_config();
        assert_eq!(by.kind, "programmatic-config");
        assert!(by.data.is_null());
        assert!(!by.is_atomic_kind());
    }

    #[test]
    fn test_by_citeproc_constructor() {
        let by = By::citeproc();
        assert_eq!(by.kind, "citeproc");
        assert!(by.data.is_null());
        // Atomic — citeproc output is non-editable in the preview.
        assert!(by.is_atomic_kind());
        // Not a "no real source" sentinel; the bytes come from CSL output.
        assert!(!by.is_programmatic_sentinel());
    }

    #[test]
    fn test_by_jupyter_output_constructor() {
        let by = By::jupyter_output();
        assert_eq!(by.kind, "jupyter-output");
        assert!(by.data.is_null());
        // Atomic — execution outputs regenerate on every re-run.
        assert!(by.is_atomic_kind());
        assert!(!by.is_programmatic_sentinel());
    }

    #[test]
    fn test_by_callout_constructor() {
        let by = By::callout();
        assert_eq!(by.kind, "callout");
        assert!(by.data.is_null());
        // Non-atomic — callout wrapper is structural; children stay editable.
        assert!(!by.is_atomic_kind());
        assert!(!by.is_programmatic_sentinel());
    }

    #[test]
    fn test_by_is_programmatic_sentinel() {
        assert!(By::config_default().is_programmatic_sentinel());
        assert!(By::programmatic_config().is_programmatic_sentinel());
        assert!(By::unknown().is_programmatic_sentinel());

        assert!(!By::user_edit().is_programmatic_sentinel());
        assert!(!By::filter("x.lua", 1).is_programmatic_sentinel());
        assert!(!By::shortcode("meta").is_programmatic_sentinel());
        assert!(!By::test_scaffold().is_programmatic_sentinel());
        assert!(!By::sectionize().is_programmatic_sentinel());
    }

    #[test]
    fn test_source_info_for_test() {
        let si = SourceInfo::for_test();
        match si {
            SourceInfo::Generated { by, from } => {
                assert_eq!(by.kind, "test-scaffold");
                assert!(from.is_empty());
            }
            _ => panic!("for_test() must return Generated"),
        }
    }

    #[test]
    fn test_by_is_kind() {
        let by = By::shortcode("meta");
        assert!(by.is_kind("shortcode"));
        assert!(!by.is_kind("filter"));
    }

    #[test]
    fn test_by_as_filter_rejects_non_filter() {
        assert!(By::sectionize().as_filter().is_none());
        // Malformed filter (missing line) → None.
        let by = By {
            kind: "filter".to_string(),
            data: serde_json::json!({ "filter_path": "x.lua" }),
        };
        assert!(by.as_filter().is_none());
    }

    #[test]
    fn test_anchor_invocation_value_source_constructors() {
        let original = Arc::new(SourceInfo::original(FileId(1), 0, 5));
        let inv = Anchor::invocation(Arc::clone(&original));
        let vs = Anchor::value_source(Arc::clone(&original));
        assert!(matches!(inv.role, AnchorRole::Invocation));
        assert!(matches!(vs.role, AnchorRole::ValueSource));
    }

    #[test]
    fn test_by_json_round_trip() {
        let by = By::shortcode("meta");
        let json = serde_json::to_string(&by).unwrap();
        let back: By = serde_json::from_str(&json).unwrap();
        assert_eq!(by, back);
    }

    #[test]
    fn test_anchor_json_round_trip() {
        let anchor = Anchor::invocation(Arc::new(SourceInfo::original(FileId(2), 10, 20)));
        let json = serde_json::to_string(&anchor).unwrap();
        let back: Anchor = serde_json::from_str(&json).unwrap();
        assert_eq!(anchor, back);
    }

    #[test]
    fn test_generated_json_round_trip_empty_from() {
        let info = SourceInfo::generated(By::sectionize());
        let json = serde_json::to_string(&info).unwrap();
        let back: SourceInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(info, back);
    }

    #[test]
    fn test_generated_json_round_trip_with_invocation_anchor() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(5), 100, 110)),
        );
        let json = serde_json::to_string(&info).unwrap();
        let back: SourceInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(info, back);
    }

    #[test]
    fn test_generated_json_round_trip_multi_anchor() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(5), 100, 110)),
        );
        info.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(7), 200, 220)),
        );
        let json = serde_json::to_string(&info).unwrap();
        let back: SourceInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(info, back);
    }

    #[test]
    fn test_generated_length_start_end_are_zero() {
        let info = SourceInfo::generated(By::sectionize());
        assert_eq!(info.length(), 0);
        assert_eq!(info.start_offset(), 0);
        assert_eq!(info.end_offset(), 0);
    }

    #[test]
    fn test_generated_resolve_byte_range_recurses_through_substring() {
        let parent = SourceInfo::original(FileId(42), 100, 200);
        let sub = SourceInfo::substring(parent, 10, 20);
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(AnchorRole::Invocation, Arc::new(sub));
        assert_eq!(info.resolve_byte_range(), Some((42, 110, 120)));
    }

    #[test]
    fn test_generated_resolve_byte_range_empty_returns_none() {
        let info = SourceInfo::generated(By::sectionize());
        assert!(info.resolve_byte_range().is_none());
    }

    #[test]
    fn test_generated_resolve_byte_range_value_source_only_returns_none() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(5), 100, 110)),
        );
        assert!(info.resolve_byte_range().is_none());
    }

    #[test]
    fn test_generated_remap_file_ids_walks_anchors() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(0), 0, 5)),
        );
        info.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(3), 10, 20)),
        );
        info.remap_file_ids(&|id| FileId(id.0 + 10));
        match &info {
            SourceInfo::Generated { from, .. } => {
                assert_eq!(from.len(), 2);
                match from[0].source_info.as_ref() {
                    SourceInfo::Original { file_id, .. } => assert_eq!(*file_id, FileId(10)),
                    _ => panic!("Expected Original anchor 0"),
                }
                match from[1].source_info.as_ref() {
                    SourceInfo::Original { file_id, .. } => assert_eq!(*file_id, FileId(13)),
                    _ => panic!("Expected Original anchor 1"),
                }
            }
            _ => panic!("Expected Generated"),
        }
    }

    #[test]
    fn test_root_file_id_per_variant() {
        // Original
        let original = SourceInfo::original(FileId(7), 0, 5);
        assert_eq!(original.root_file_id(), Some(FileId(7)));

        // Substring → recurse parent
        let sub = SourceInfo::substring(original.clone(), 0, 5);
        assert_eq!(sub.root_file_id(), Some(FileId(7)));

        // Concat find_map skips Generated holes
        let empty_gen = SourceInfo::generated(By::sectionize());
        let real = SourceInfo::original(FileId(42), 0, 5);
        let concat = SourceInfo::concat(vec![(empty_gen, 0), (real, 5)]);
        assert_eq!(concat.root_file_id(), Some(FileId(42)));

        // Generated with Invocation
        let mut g = SourceInfo::generated(By::shortcode("meta"));
        g.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(9), 0, 1)),
        );
        assert_eq!(g.root_file_id(), Some(FileId(9)));

        // Generated with no Invocation
        let mut g2 = SourceInfo::generated(By::shortcode("meta"));
        g2.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(9), 0, 1)),
        );
        assert_eq!(g2.root_file_id(), None);

        // Generated empty
        let g3 = SourceInfo::generated(By::sectionize());
        assert_eq!(g3.root_file_id(), None);
    }

    #[test]
    fn test_collect_file_ids_walks_every_anchor_role() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(1), 0, 1)),
        );
        info.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(2), 0, 1)),
        );
        info.append_anchor(
            AnchorRole::Other("dispatch".to_string()),
            Arc::new(SourceInfo::original(FileId(3), 0, 1)),
        );
        let mut out = std::collections::HashSet::new();
        info.collect_file_ids(&mut out);
        assert!(out.contains(&FileId(1)));
        assert!(out.contains(&FileId(2)));
        assert!(out.contains(&FileId(3)));
        assert_eq!(out.len(), 3);
    }

    #[test]
    fn test_collect_file_ids_walks_concat_and_substring() {
        let inner = SourceInfo::original(FileId(5), 0, 100);
        let sub = SourceInfo::substring(inner, 10, 20);
        let other = SourceInfo::original(FileId(11), 0, 5);
        let concat = SourceInfo::concat(vec![(sub, 10), (other, 5)]);
        let mut out = std::collections::HashSet::new();
        concat.collect_file_ids(&mut out);
        assert!(out.contains(&FileId(5)));
        assert!(out.contains(&FileId(11)));
        assert_eq!(out.len(), 2);
    }

    #[test]
    fn test_invocation_anchor_accessor() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        assert!(info.invocation_anchor().is_none());
        info.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(2), 0, 1)),
        );
        assert!(info.invocation_anchor().is_none());
        info.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(1), 0, 1)),
        );
        assert!(info.invocation_anchor().is_some());
        // Non-Generated returns None.
        assert!(
            SourceInfo::original(FileId(0), 0, 0)
                .invocation_anchor()
                .is_none()
        );
    }

    #[test]
    fn test_value_source_anchor_accessor() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        assert!(info.value_source_anchor().is_none());
        info.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(1), 0, 1)),
        );
        assert!(info.value_source_anchor().is_none());
        info.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(2), 0, 1)),
        );
        assert!(info.value_source_anchor().is_some());
    }

    #[test]
    fn test_anchors_with_role() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(1), 0, 1)),
        );
        info.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(2), 0, 1)),
        );
        info.append_anchor(
            AnchorRole::Other("ext/foo".to_string()),
            Arc::new(SourceInfo::original(FileId(3), 0, 1)),
        );
        assert_eq!(info.anchors_with_role(&AnchorRole::Invocation).count(), 1);
        assert_eq!(info.anchors_with_role(&AnchorRole::ValueSource).count(), 1);
        assert_eq!(
            info.anchors_with_role(&AnchorRole::Other("ext/foo".to_string()))
                .count(),
            1
        );
        assert_eq!(
            info.anchors_with_role(&AnchorRole::Other("missing".to_string()))
                .count(),
            0
        );
    }

    #[test]
    fn test_append_anchor_preserves_order() {
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(
            AnchorRole::Invocation,
            Arc::new(SourceInfo::original(FileId(1), 0, 1)),
        );
        info.append_anchor(
            AnchorRole::ValueSource,
            Arc::new(SourceInfo::original(FileId(2), 0, 1)),
        );
        match info {
            SourceInfo::Generated { from, .. } => {
                assert_eq!(from.len(), 2);
                assert!(matches!(from[0].role, AnchorRole::Invocation));
                assert!(matches!(from[1].role, AnchorRole::ValueSource));
            }
            _ => panic!("Expected Generated"),
        }
    }

    #[test]
    fn test_combine_with_generated_is_zero_length_piece() {
        let original = SourceInfo::original(FileId(0), 10, 20);
        let generated = SourceInfo::generated(By::sectionize());
        let combined = original.combine(&generated);
        match &combined {
            SourceInfo::Concat { pieces } => {
                assert_eq!(pieces.len(), 2);
                assert_eq!(pieces[1].length, 0);
            }
            _ => panic!("Expected Concat"),
        }
        // Length of the combined value equals only the Original side.
        assert_eq!(combined.length(), 10);
    }

    #[test]
    fn test_source_info_serialization() {
        let file_id = FileId(0);
        let range = Range {
            start: Location {
                offset: 0,
                row: 0,
                column: 0,
            },
            end: Location {
                offset: 10,
                row: 0,
                column: 10,
            },
        };

        let info = SourceInfo::from_range(file_id, range);
        let json = serde_json::to_string(&info).unwrap();
        let deserialized: SourceInfo = serde_json::from_str(&json).unwrap();

        assert_eq!(info, deserialized);
    }

    #[test]
    fn test_substring_source_info() {
        let file_id = FileId(0);
        let parent_range = Range {
            start: Location {
                offset: 0,
                row: 0,
                column: 0,
            },
            end: Location {
                offset: 100,
                row: 0,
                column: 100,
            },
        };
        let parent = SourceInfo::from_range(file_id, parent_range);

        let substring = SourceInfo::substring(parent, 10, 20);

        assert_eq!(substring.start_offset(), 10);
        assert_eq!(substring.end_offset(), 20);
        assert_eq!(substring.length(), 10);

        match substring {
            SourceInfo::Substring {
                start_offset,
                end_offset,
                ..
            } => {
                assert_eq!(start_offset, 10);
                assert_eq!(end_offset, 20);
            }
            _ => panic!("Expected Substring mapping"),
        }
    }

    #[test]
    fn test_concat_source_info() {
        let file_id1 = FileId(0);
        let file_id2 = FileId(1);

        let info1 = SourceInfo::from_range(
            file_id1,
            Range {
                start: Location {
                    offset: 0,
                    row: 0,
                    column: 0,
                },
                end: Location {
                    offset: 10,
                    row: 0,
                    column: 10,
                },
            },
        );

        let info2 = SourceInfo::from_range(
            file_id2,
            Range {
                start: Location {
                    offset: 0,
                    row: 0,
                    column: 0,
                },
                end: Location {
                    offset: 15,
                    row: 0,
                    column: 15,
                },
            },
        );

        let concat = SourceInfo::concat(vec![(info1, 10), (info2, 15)]);

        assert_eq!(concat.start_offset(), 0);
        assert_eq!(concat.end_offset(), 25); // 10 + 15
        assert_eq!(concat.length(), 25);

        match concat {
            SourceInfo::Concat { pieces } => {
                assert_eq!(pieces.len(), 2);
                assert_eq!(pieces[0].offset_in_concat, 0);
                assert_eq!(pieces[0].length, 10);
                assert_eq!(pieces[1].offset_in_concat, 10);
                assert_eq!(pieces[1].length, 15);
            }
            _ => panic!("Expected Concat mapping"),
        }
    }

    #[test]
    fn test_combine_two_sources() {
        let file_id = FileId(0);

        // Create two separate source info objects
        let info1 = SourceInfo::from_range(
            file_id,
            Range {
                start: Location {
                    offset: 0,
                    row: 0,
                    column: 0,
                },
                end: Location {
                    offset: 10,
                    row: 0,
                    column: 10,
                },
            },
        );

        let info2 = SourceInfo::from_range(
            file_id,
            Range {
                start: Location {
                    offset: 15,
                    row: 0,
                    column: 15,
                },
                end: Location {
                    offset: 25,
                    row: 0,
                    column: 25,
                },
            },
        );

        // Combine them
        let combined = info1.combine(&info2);

        // Should create a Concat with total length = 10 + 10 = 20
        assert_eq!(combined.start_offset(), 0);
        assert_eq!(combined.end_offset(), 20);
        assert_eq!(combined.length(), 20);

        match combined {
            SourceInfo::Concat { pieces } => {
                assert_eq!(pieces.len(), 2);
                assert_eq!(pieces[0].length, 10);
                assert_eq!(pieces[0].offset_in_concat, 0);
                assert_eq!(pieces[1].length, 10);
                assert_eq!(pieces[1].offset_in_concat, 10);
            }
            _ => panic!("Expected Concat mapping"),
        }
    }

    #[test]
    fn test_combine_preserves_source_tracking() {
        // Combine sources from different files
        let file_id1 = FileId(5);
        let file_id2 = FileId(10);

        let info1 = SourceInfo::from_range(
            file_id1,
            Range {
                start: Location {
                    offset: 100,
                    row: 5,
                    column: 0,
                },
                end: Location {
                    offset: 105,
                    row: 5,
                    column: 5,
                },
            },
        );

        let info2 = SourceInfo::from_range(
            file_id2,
            Range {
                start: Location {
                    offset: 200,
                    row: 10,
                    column: 0,
                },
                end: Location {
                    offset: 207,
                    row: 10,
                    column: 7,
                },
            },
        );

        let combined = info1.combine(&info2);

        // Verify both sources are preserved in the Concat
        match combined {
            SourceInfo::Concat { pieces } => {
                assert_eq!(pieces.len(), 2);

                // First piece should come from file_id1
                match &pieces[0].source_info {
                    SourceInfo::Original { file_id, .. } => assert_eq!(*file_id, file_id1),
                    _ => panic!("Expected Original mapping for first piece"),
                }

                // Second piece should come from file_id2
                match &pieces[1].source_info {
                    SourceInfo::Original { file_id, .. } => assert_eq!(*file_id, file_id2),
                    _ => panic!("Expected Original mapping for second piece"),
                }
            }
            _ => panic!("Expected Concat mapping"),
        }
    }

    /// Test JSON serialization of Original mapping
    #[test]
    fn test_json_serialization_original() {
        let file_id = FileId(0);
        let range = Range {
            start: Location {
                offset: 10,
                row: 1,
                column: 5,
            },
            end: Location {
                offset: 50,
                row: 3,
                column: 10,
            },
        };

        let info = SourceInfo::from_range(file_id, range);
        let json = serde_json::to_value(&info).unwrap();

        // Verify JSON structure
        assert_eq!(json["Original"]["file_id"], 0);
        assert_eq!(json["Original"]["start_offset"], 10);
        assert_eq!(json["Original"]["end_offset"], 50);

        // Verify round-trip
        let deserialized: SourceInfo = serde_json::from_value(json).unwrap();
        assert_eq!(info, deserialized);
    }

    /// Test JSON serialization of Substring mapping
    #[test]
    fn test_json_serialization_substring() {
        let file_id = FileId(0);
        let parent_range = Range {
            start: Location {
                offset: 0,
                row: 0,
                column: 0,
            },
            end: Location {
                offset: 100,
                row: 5,
                column: 20,
            },
        };
        let parent = SourceInfo::from_range(file_id, parent_range);

        let substring = SourceInfo::substring(parent, 10, 30);
        let json = serde_json::to_value(&substring).unwrap();

        // Verify JSON structure
        assert_eq!(json["Substring"]["start_offset"], 10);
        assert_eq!(json["Substring"]["end_offset"], 30);

        // Verify parent is serialized (with Rc, it's a full copy in JSON)
        assert!(json["Substring"]["parent"].is_object());
        assert_eq!(json["Substring"]["parent"]["Original"]["file_id"], 0);

        // Verify round-trip
        let deserialized: SourceInfo = serde_json::from_value(json).unwrap();
        assert_eq!(substring, deserialized);
    }

    /// Test JSON serialization of nested Substring mappings (simulates .qmd frontmatter)
    #[test]
    fn test_json_serialization_nested_substring() {
        let file_id = FileId(0);

        // Level 1: Original file
        let file_range = Range {
            start: Location {
                offset: 0,
                row: 0,
                column: 0,
            },
            end: Location {
                offset: 200,
                row: 10,
                column: 0,
            },
        };
        let file_info = SourceInfo::from_range(file_id, file_range);

        // Level 2: YAML frontmatter (substring of file)
        let yaml_info = SourceInfo::substring(file_info, 4, 150);

        // Level 3: YAML value (substring of frontmatter)
        let value_info = SourceInfo::substring(yaml_info, 20, 35);

        let json = serde_json::to_value(&value_info).unwrap();

        // Verify nested structure
        assert_eq!(json["Substring"]["start_offset"], 20);
        assert_eq!(json["Substring"]["end_offset"], 35);
        assert_eq!(json["Substring"]["parent"]["Substring"]["start_offset"], 4);
        assert_eq!(
            json["Substring"]["parent"]["Substring"]["parent"]["Original"]["file_id"],
            0
        );

        // Verify round-trip
        let deserialized: SourceInfo = serde_json::from_value(json).unwrap();
        assert_eq!(value_info, deserialized);
    }

    /// Test JSON serialization of Concat mapping
    #[test]
    fn test_json_serialization_concat() {
        let file_id1 = FileId(0);
        let file_id2 = FileId(1);

        let info1 = SourceInfo::from_range(
            file_id1,
            Range {
                start: Location {
                    offset: 0,
                    row: 0,
                    column: 0,
                },
                end: Location {
                    offset: 10,
                    row: 0,
                    column: 10,
                },
            },
        );

        let info2 = SourceInfo::from_range(
            file_id2,
            Range {
                start: Location {
                    offset: 20,
                    row: 2,
                    column: 0,
                },
                end: Location {
                    offset: 30,
                    row: 2,
                    column: 10,
                },
            },
        );

        let combined = info1.combine(&info2);
        let json = serde_json::to_value(&combined).unwrap();

        // Verify JSON structure
        assert!(json["Concat"]["pieces"].is_array());
        let pieces = json["Concat"]["pieces"].as_array().unwrap();
        assert_eq!(pieces.len(), 2);

        // First piece
        assert_eq!(pieces[0]["offset_in_concat"], 0);
        assert_eq!(pieces[0]["length"], 10);
        assert_eq!(pieces[0]["source_info"]["Original"]["file_id"], 0);

        // Second piece
        assert_eq!(pieces[1]["offset_in_concat"], 10);
        assert_eq!(pieces[1]["length"], 10);
        assert_eq!(pieces[1]["source_info"]["Original"]["file_id"], 1);

        // Verify round-trip
        let deserialized: SourceInfo = serde_json::from_value(json).unwrap();
        assert_eq!(combined, deserialized);
    }

    /// Test JSON serialization of complex nested structure (real-world example)
    #[test]
    fn test_json_serialization_complex_nested() {
        let file_id = FileId(0);

        // Simulate a .qmd file structure
        let qmd_file = SourceInfo::from_range(
            file_id,
            Range {
                start: Location {
                    offset: 0,
                    row: 0,
                    column: 0,
                },
                end: Location {
                    offset: 500,
                    row: 20,
                    column: 0,
                },
            },
        );

        // YAML frontmatter is a substring
        let yaml_frontmatter = SourceInfo::substring(qmd_file.clone(), 4, 200);

        // A YAML key is a substring of frontmatter
        let yaml_key = SourceInfo::substring(yaml_frontmatter.clone(), 10, 20);

        // A YAML value is another substring of frontmatter
        let yaml_value = SourceInfo::substring(yaml_frontmatter, 25, 50);

        // Combine key and value (simulating metadata entry)
        let combined = yaml_key.combine(&yaml_value);

        let json = serde_json::to_value(&combined).unwrap();

        // Verify this complex structure serializes
        assert!(json.is_object());
        assert!(json["Concat"].is_object());

        // Verify round-trip
        let deserialized: SourceInfo = serde_json::from_value(json).unwrap();
        assert_eq!(combined, deserialized);
    }

    // -------------------------------------------------------------------------
    // Plan 7 — preimage_in accessor
    // -------------------------------------------------------------------------

    #[test]
    fn test_preimage_in_original_same_file() {
        let info = SourceInfo::original(FileId(0), 10, 25);
        assert_eq!(info.preimage_in(FileId(0)), Some(10..25));
    }

    #[test]
    fn test_preimage_in_original_different_file_returns_none() {
        let info = SourceInfo::original(FileId(0), 10, 25);
        assert_eq!(info.preimage_in(FileId(1)), None);
    }

    #[test]
    fn test_preimage_in_substring_composes_offsets() {
        // Parent points at bytes 100..200 in file 0.
        // Substring takes bytes 5..15 *relative to parent*.
        // Preimage in file 0 should be 105..115.
        let parent = SourceInfo::original(FileId(0), 100, 200);
        let info = SourceInfo::substring(parent, 5, 15);
        assert_eq!(info.preimage_in(FileId(0)), Some(105..115));
    }

    #[test]
    fn test_preimage_in_substring_different_file_returns_none() {
        let parent = SourceInfo::original(FileId(0), 100, 200);
        let info = SourceInfo::substring(parent, 5, 15);
        assert_eq!(info.preimage_in(FileId(7)), None);
    }

    #[test]
    fn test_preimage_in_substring_chain() {
        // Original 1000..2000 in file 0; Substring 100..500 relative; Substring 10..50 relative.
        // Expected preimage in file 0: 1100 + 10 .. 1100 + 50 = 1110..1150.
        let root = SourceInfo::original(FileId(0), 1000, 2000);
        let mid = SourceInfo::substring(root, 100, 500);
        let leaf = SourceInfo::substring(mid, 10, 50);
        assert_eq!(leaf.preimage_in(FileId(0)), Some(1110..1150));
    }

    #[test]
    fn test_preimage_in_concat_contiguous() {
        // Two adjacent pieces of file 0: 10..15 and 15..25 → contiguous → 10..25.
        let a = SourceInfo::original(FileId(0), 10, 15);
        let b = SourceInfo::original(FileId(0), 15, 25);
        let info = SourceInfo::concat(vec![(a, 5), (b, 10)]);
        assert_eq!(info.preimage_in(FileId(0)), Some(10..25));
    }

    #[test]
    fn test_preimage_in_concat_gappy_returns_none() {
        // 10..15 then 20..25 → gap between 15 and 20 → None.
        let a = SourceInfo::original(FileId(0), 10, 15);
        let b = SourceInfo::original(FileId(0), 20, 25);
        let info = SourceInfo::concat(vec![(a, 5), (b, 5)]);
        assert_eq!(info.preimage_in(FileId(0)), None);
    }

    #[test]
    fn test_preimage_in_concat_overlapping_returns_none() {
        // 10..20 then 15..25 → overlap → not byte-contiguous → None.
        let a = SourceInfo::original(FileId(0), 10, 20);
        let b = SourceInfo::original(FileId(0), 15, 25);
        let info = SourceInfo::concat(vec![(a, 10), (b, 10)]);
        assert_eq!(info.preimage_in(FileId(0)), None);
    }

    #[test]
    fn test_preimage_in_concat_mixed_files_returns_none() {
        // One piece in file 0, another in file 1 → resolving in file 0 fails
        // because the file-1 piece can't be resolved.
        let a = SourceInfo::original(FileId(0), 10, 15);
        let b = SourceInfo::original(FileId(1), 15, 25);
        let info = SourceInfo::concat(vec![(a, 5), (b, 10)]);
        assert_eq!(info.preimage_in(FileId(0)), None);
    }

    #[test]
    fn test_preimage_in_generated_no_anchors_returns_none() {
        // Sectionize-style wrapper, footnotes-container, etc.: Generated with
        // empty `from`. No Invocation anchor → no preimage.
        let info = SourceInfo::generated(By::sectionize());
        assert_eq!(info.preimage_in(FileId(0)), None);
    }

    #[test]
    fn test_preimage_in_generated_with_invocation_in_target() {
        // Shortcode resolution: Generated with an Invocation anchor pointing
        // at the {{< meta foo >}} token bytes.
        let token = SourceInfo::original(FileId(0), 50, 70);
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(AnchorRole::Invocation, Arc::new(token));
        assert_eq!(info.preimage_in(FileId(0)), Some(50..70));
    }

    #[test]
    fn test_preimage_in_generated_with_invocation_outside_target() {
        // Invocation anchor points at file 0; query asks about file 1 → None.
        let token = SourceInfo::original(FileId(0), 50, 70);
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(AnchorRole::Invocation, Arc::new(token));
        assert_eq!(info.preimage_in(FileId(1)), None);
    }

    #[test]
    fn test_preimage_in_generated_walks_through_substring_in_invocation() {
        // Invocation anchor is itself a Substring chain. preimage_in must
        // walk through it correctly.
        let root = SourceInfo::original(FileId(0), 100, 200);
        let token = SourceInfo::substring(root, 10, 30);
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(AnchorRole::Invocation, Arc::new(token));
        assert_eq!(info.preimage_in(FileId(0)), Some(110..130));
    }

    // -------------------------------------------------------------------------
    // Plan 7 — preimage_in role-asymmetry: only Invocation is walked.
    // -------------------------------------------------------------------------

    #[test]
    fn test_preimage_in_generated_value_source_only_returns_none() {
        // Plan 9-shape: Generated whose only anchor is ValueSource (points at
        // YAML metadata bytes). The writer must NOT copy those bytes into the
        // body — preimage_in returns None.
        let meta_si = SourceInfo::original(FileId(0), 10, 25);
        let mut info = SourceInfo::generated(By::appendix());
        info.append_anchor(AnchorRole::ValueSource, Arc::new(meta_si));
        assert_eq!(info.preimage_in(FileId(0)), None);
    }

    #[test]
    fn test_preimage_in_generated_other_only_returns_none() {
        // Extension-defined Other role. preimage_in must not walk it.
        let lua_si = SourceInfo::original(FileId(0), 10, 25);
        let mut info = SourceInfo::generated(By::filter("upper.lua", 14));
        info.append_anchor(
            AnchorRole::Other("ext/my-ext/dispatch".to_string()),
            Arc::new(lua_si),
        );
        assert_eq!(info.preimage_in(FileId(0)), None);
    }

    #[test]
    fn test_preimage_in_generated_invocation_plus_value_source_walks_invocation_only() {
        // Plan 2/Plan 9 mixed shape: Invocation in file 0 + ValueSource in
        // file 1. Query file 0 → Invocation resolves → Some(token range).
        // Query file 1 → Invocation resolves to file 0 (not 1) → None.
        // (The writer must not see the value-source range when asked about
        // any file, even the file the ValueSource points into.)
        let token = SourceInfo::original(FileId(0), 50, 70);
        let value = SourceInfo::original(FileId(1), 200, 215);
        let mut info = SourceInfo::generated(By::shortcode("meta"));
        info.append_anchor(AnchorRole::Invocation, Arc::new(token));
        info.append_anchor(AnchorRole::ValueSource, Arc::new(value));

        assert_eq!(info.preimage_in(FileId(0)), Some(50..70));
        assert_eq!(info.preimage_in(FileId(1)), None);
    }
}