aft-store 0.68.1

On-disk index formats for Agent Foundational Toolkit
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
//! Content records: everything parsing one file content yields, stored once
//! per content and shared by every checkout and path that holds it.
//!
//! A record has two parts. The **surface** is what other files may read
//! about this one when they are resolved: its symbols, exports, default
//! export, entry points and type facts. The **body** is what only this
//! file's own resolution reads: its references, imports, Python bindings and
//! method-call sites. Resolving a file reads its own body and the surfaces it
//! depends on, never another file's body.
//!
//! Configuration files are stored as their raw bytes: how they are
//! interpreted depends on the checkout they configure.
//!
//! Records hold no paths. Positions (lines, columns, byte ranges, syntax-tree
//! ordinals) are within the content, so they are the same wherever the
//! content appears.
//!
//! A record can also hold the content's TODO markers ([`TodoMarkers`]),
//! found in its comments when it was parsed, so counting a checkout's TODOs
//! never parses a file. They are written in the first part, right after
//! the record's kind, though no other file reads them: that part is stored
//! uncompressed, so a count reads each file's markers
//! ([`ContentRecord::todos_of`]) without unpacking its body. Files the call
//! graph does not parse but whose comments are counted (Markdown, minified
//! bundles) get a record holding only their markers ([`RecordBody::Markers`]).

pub mod codec;
mod sections;
mod stage;

pub use sections::{
    ContentReadError, ContentSectionsBuilder, ContentSegment, EncodedRecord, KNOWN,
    SECTIONS_VERSION, write_folded,
};
pub use stage::{PackedRecord, Stage, StageWriter, StagedUnit, write_staged};

use codec::{Decoder, Encoder};

use crate::ids::{ContentKey, Digest128};

/// The key of a content record: the content, the language it is parsed as,
/// and the extractor version. The same bytes at a `.ts` and a `.js` path are
/// two records; a new extractor version never reads an old one.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct RecordKey(Digest128);

impl RecordKey {
    #[must_use]
    pub fn new(content: ContentKey, language: &str, extractor_version: &str) -> Self {
        let mut hasher = blake3::Hasher::new_derive_key("aft-store 2026-11 content record key");
        hasher.update(content.digest().as_bytes());
        for part in [language, extractor_version] {
            hasher.update(&(part.len() as u64).to_le_bytes());
            hasher.update(part.as_bytes());
        }
        Self(Digest128::from_hash(&hasher.finalize()))
    }

    /// A key read back from storage.
    #[must_use]
    pub const fn from_digest(digest: Digest128) -> Self {
        Self(digest)
    }

    #[must_use]
    pub const fn digest(&self) -> Digest128 {
        self.0
    }
}

/// Why stored bytes are not a well-formed record.
#[derive(Clone, Copy, PartialEq, Eq, Debug, thiserror::Error)]
#[error("content record does not decode: {0}")]
pub struct DecodeError(pub &'static str);

/// One symbol a file defines.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Symbol {
    pub is_test: Option<bool>,
    /// The syntax-tree pre-order position of the defining node.
    pub ordinal: u32,
    pub name: String,
    pub scoped_name: String,
    pub kind: String,
    pub exported: bool,
    pub is_default_export: bool,
    pub start_line: u32,
    pub start_col: u32,
    pub end_line: u32,
    pub end_col: u32,
    pub signature: Option<String>,
    /// Whether the program starts at this symbol, as its language defines
    /// it (`fn main`, `func main` in a Go `package main`, a Go
    /// `func init()`, a C `main`).
    pub program_start: bool,
    /// The entry attribute or decorator extraction recorded on the symbol,
    /// such as `no_mangle` or `tauri::command`.
    pub entry_attribute: Option<String>,
}

/// The parse-level class of a reference.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum RefKind {
    Construction,
    CallbackRef,
    Call,
    ValueRef,
    Import,
    Module,
    Reexport,
    ExportAlias,
    /// A name inside a Rust macro whose tokens could not be parsed as
    /// Rust: a possible call the graph cannot see.
    MacroMention,
}

impl RefKind {
    const ALL: [Self; 9] = [
        Self::Construction,
        Self::CallbackRef,
        Self::Call,
        Self::ValueRef,
        Self::Import,
        Self::Module,
        Self::Reexport,
        Self::ExportAlias,
        Self::MacroMention,
    ];

    fn code(self) -> u8 {
        Self::ALL
            .iter()
            .position(|kind| *kind == self)
            .and_then(|index| u8::try_from(index).ok())
            .expect("every kind is in ALL")
    }

    fn from_code(code: u8) -> Result<Self, DecodeError> {
        Self::ALL
            .get(usize::from(code))
            .copied()
            .ok_or(DecodeError("reference kind"))
    }
}

/// A reference as extracted, before resolution: no target is known.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Reference {
    pub ordinal: u32,
    pub kind: RefKind,
    pub caller_symbol: Option<String>,
    pub short_name: Option<String>,
    pub full_ref: Option<String>,
    pub module_path: Option<String>,
    pub line: u32,
    pub byte_start: u64,
    pub byte_end: u64,
    pub path_override: Option<String>,
    pub local_name: Option<String>,
    pub requested_name: Option<String>,
    pub namespace_alias: Option<String>,
    pub wildcard: bool,
    pub import_kind: Option<String>,
}

/// An import statement.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Import {
    pub ordinal: u32,
    pub module_path: String,
    pub names: Vec<String>,
    pub default_import: Option<String>,
    pub namespace_import: Option<String>,
    pub byte_start: u64,
    pub byte_end: u64,
    pub raw_text: String,
    pub type_only: bool,
    pub side_effect: bool,
}

/// A Python name binding at one span: the module and symbol it refers to,
/// or `None` when it is shadowed locally.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PythonBinding {
    pub byte_start: u64,
    pub byte_end: u64,
    pub target: Option<(String, String)>,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TypeFact {
    pub name: String,
    pub bases: Vec<String>,
    pub interface: bool,
    pub closed: bool,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MethodFact {
    pub owner: String,
    pub trait_name: Option<String>,
    pub name: String,
    pub symbol: String,
    pub has_body: bool,
    pub shape: String,
    pub private: bool,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FieldFact {
    pub owner: String,
    pub name: String,
    pub ty: String,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ReturnFact {
    pub symbol: String,
    pub ty: String,
}

/// A method-call site: a call whose target depends on a receiver's type.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CallSite {
    pub ordinal: u32,
    pub caller: Option<String>,
    pub line: u32,
    pub member: Option<String>,
    pub receiver: Option<String>,
    pub dynamic: bool,
}

/// What other files may read about a parsed file.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Surface {
    pub symbols: Vec<Symbol>,
    pub default_export_symbol: Option<String>,
    pub exported_symbols: Vec<String>,
    pub callable_symbols: Vec<String>,
    pub entry_point_symbols: Vec<String>,
    /// Go's package clause, which tells co-located test packages apart.
    pub go_package: Option<String>,
    pub types: Vec<TypeFact>,
    pub methods: Vec<MethodFact>,
    pub fields: Vec<FieldFact>,
    pub returns: Vec<ReturnFact>,
}

/// One symbol as outline lists it. Outline reads the parser's own symbol
/// table for a file, which differs from [`Surface::symbols`]: the call graph
/// qualifies, adds and merges symbols for resolution, outline shows them as
/// written. Lines and columns are zero-based.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct OutlineSymbol {
    pub name: String,
    /// The parser's symbol kind, in its snake-case name (`function`,
    /// `type_alias`).
    pub kind: String,
    pub start_line: u32,
    pub start_col: u32,
    pub end_line: u32,
    pub end_col: u32,
    pub signature: Option<String>,
    /// The enclosing scopes, outermost first.
    pub scope_chain: Vec<String>,
    pub exported: bool,
    pub parent: Option<String>,
}

/// What only reads of this one file need: its own resolution, and its
/// outline.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Body {
    pub imports: Vec<Import>,
    pub python_bindings: Vec<PythonBinding>,
    pub references: Vec<Reference>,
    pub call_sites: Vec<CallSite>,
    /// The file's symbols as outline lists them, in the parser's order.
    /// `None` in a record from an extractor that did not store them.
    pub outline: Option<Vec<OutlineSymbol>>,
    /// What dead-code analysis reads from the file's syntax tree, recorded
    /// when the file is parsed so an analysis never parses it again. The
    /// store does not read these bytes: their format belongs to the
    /// extractor that wrote them, which the record's extractor version
    /// names. `None` in a record from an extractor that did not store them.
    pub dead_code: Option<Vec<u8>>,
    /// Sparse static receiver evidence; its format belongs to the extractor.
    pub static_dispatch: Option<Vec<u8>>,
}

/// What a content record holds.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum RecordBody {
    Parsed {
        surface: Box<Surface>,
        body: Box<Body>,
    },
    /// A configuration file's raw bytes.
    Config { source: Vec<u8> },
    /// Nothing but the record's [`ContentRecord::todos`]: a file the call
    /// graph does not parse whose comments are still counted.
    Markers,
}

/// One TODO marker comment line, as AFT's TODO count reports it.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TodoMarker {
    /// The one-based line the marker is on.
    pub line: u32,
    /// The marker word, such as `TODO` or `FIXME`.
    pub marker: String,
    /// The name in parentheses after the marker (`TODO(name):`), if any.
    pub author: Option<String>,
    /// The comment's text after the marker, as the count shows it.
    pub text: String,
}

/// The TODO markers of one content.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TodoMarkers {
    /// The language the content's comments were read as. The same bytes can
    /// be read as another language at another path, so a reader checks it
    /// against the path's.
    pub language: String,
    /// The markers in line order; `None` when the content is not text the
    /// count reads (not UTF-8, or holding a NUL byte).
    pub items: Option<Vec<TodoMarker>>,
}

/// Everything parsing one content yields.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ContentRecord {
    pub language: String,
    /// The extractor that produced the record. A record from another
    /// version is extracted again, never reused.
    pub extractor_version: String,
    pub data: RecordBody,
    /// The content's TODO markers. `None` in a record from an extractor
    /// that did not look for them, or of a file whose comments are not
    /// counted.
    pub todos: Option<TodoMarkers>,
}

/// A parsed record whose body holds no outline. Records of this kind encode
/// exactly as they did before records could hold one.
const PARSED: u8 = 1;
const CONFIG: u8 = 2;
/// A parsed record whose body ends with its outline. The kind byte, not the
/// body's length, says whether an outline follows, so a body cut short
/// before its outline still fails to decode.
const PARSED_WITH_OUTLINE: u8 = 3;
/// [`PARSED`], with each symbol's entry facts ([`Symbol::program_start`],
/// [`Symbol::entry_attribute`]) after the rest of the surface. Records of
/// the two kinds above predate those facts and read back without them.
const PARSED_WITH_ENTRIES: u8 = 4;
/// [`PARSED_WITH_OUTLINE`], with each symbol's entry facts after the rest
/// of the surface.
const PARSED_WITH_ENTRIES_AND_OUTLINE: u8 = 5;
/// [`PARSED_WITH_ENTRIES_AND_OUTLINE`], whose body ends with its dead-code
/// facts after the outline.
const PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE: u8 = 6;
/// [`PARSED_WITH_ENTRIES`], whose body ends with its dead-code facts.
const PARSED_WITH_ENTRIES_AND_DEAD_CODE: u8 = 7;
/// A parsed record with its TODO markers. After the kind come a byte of
/// [`HAS_OUTLINE`] and [`HAS_DEAD_CODE`] flags, saying what the body ends
/// with, then the markers, the surface and each symbol's entry facts.
const PARSED_WITH_TODOS: u8 = 8;
/// [`CONFIG`] with the TODO markers between the kind and the source.
const CONFIG_WITH_TODOS: u8 = 9;
/// A [`RecordBody::Markers`] record: the kind, then whether markers follow
/// (a byte, 0 or 1), then the markers. Its body part is empty.
const MARKERS: u8 = 10;
/// A parsed record whose body ends with static receiver facts, after the
/// other optional parts. Its flags also say whether TODO markers follow.
const PARSED_WITH_STATIC_DISPATCH: u8 = 11;
const STATIC_HAS_TODOS: u8 = 4;
/// In a [`PARSED_WITH_TODOS`] record's flags: the body ends with an outline.
const HAS_OUTLINE: u8 = 1;
/// In a [`PARSED_WITH_TODOS`] record's flags: the body ends with dead-code
/// facts, after the outline when there is one.
const HAS_DEAD_CODE: u8 = 2;

impl ContentRecord {
    /// The surface part's bytes and the body part's bytes. A configuration
    /// or markers-only record has an empty body part. A record without TODO
    /// markers encodes exactly as it did before records could hold them.
    #[must_use]
    pub fn encode(&self) -> (Vec<u8>, Vec<u8>) {
        let mut head = Encoder::default();
        head.str(&self.language);
        head.str(&self.extractor_version);
        match &self.data {
            RecordBody::Config { source } => {
                match &self.todos {
                    None => head.byte(CONFIG),
                    Some(todos) => {
                        head.byte(CONFIG_WITH_TODOS);
                        encode_todos(&mut head, todos);
                    }
                }
                head.blob(source);
                (head.finish(), Vec::new())
            }
            RecordBody::Markers => {
                head.byte(MARKERS);
                head.bool(self.todos.is_some());
                if let Some(todos) = &self.todos {
                    encode_todos(&mut head, todos);
                }
                (head.finish(), Vec::new())
            }
            RecordBody::Parsed { surface, body } => {
                match &self.todos {
                    _ if body.static_dispatch.is_some() => {
                        head.byte(PARSED_WITH_STATIC_DISPATCH);
                        let flags = (u8::from(body.outline.is_some()) * HAS_OUTLINE)
                            | (u8::from(body.dead_code.is_some()) * HAS_DEAD_CODE)
                            | (u8::from(self.todos.is_some()) * STATIC_HAS_TODOS);
                        head.byte(flags);
                        if let Some(todos) = &self.todos {
                            encode_todos(&mut head, todos);
                        }
                    }
                    None => head.byte(match (body.outline.is_some(), body.dead_code.is_some()) {
                        (true, true) => PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE,
                        (true, false) => PARSED_WITH_ENTRIES_AND_OUTLINE,
                        (false, true) => PARSED_WITH_ENTRIES_AND_DEAD_CODE,
                        (false, false) => PARSED_WITH_ENTRIES,
                    }),
                    Some(todos) => {
                        head.byte(PARSED_WITH_TODOS);
                        let mut flags = 0;
                        if body.outline.is_some() {
                            flags |= HAS_OUTLINE;
                        }
                        if body.dead_code.is_some() {
                            flags |= HAS_DEAD_CODE;
                        }
                        head.byte(flags);
                        encode_todos(&mut head, todos);
                    }
                }
                encode_surface(&mut head, surface);
                encode_entries(&mut head, surface);
                let mut tail = Encoder::default();
                encode_body(&mut tail, body);
                (head.finish(), tail.finish())
            }
        }
    }

    /// Reads a record from its two parts.
    ///
    /// # Errors
    ///
    /// When either part is not well formed.
    pub fn decode(surface: &[u8], body: &[u8]) -> Result<Self, DecodeError> {
        let mut head = Decoder::new(surface)?;
        let language = head.string()?;
        let extractor_version = head.string()?;
        let mut todos = None;
        let data = match head.byte()? {
            kind @ (CONFIG | CONFIG_WITH_TODOS) => {
                if kind == CONFIG_WITH_TODOS {
                    todos = Some(decode_todos(&mut head)?);
                }
                let source = head.blob()?.to_vec();
                head.finish()?;
                if !body.is_empty() {
                    return Err(DecodeError("a configuration record has a body"));
                }
                RecordBody::Config { source }
            }
            MARKERS => {
                if head.bool()? {
                    todos = Some(decode_todos(&mut head)?);
                }
                head.finish()?;
                if !body.is_empty() {
                    return Err(DecodeError("a markers record has a body"));
                }
                RecordBody::Markers
            }
            kind @ (PARSED
            | PARSED_WITH_OUTLINE
            | PARSED_WITH_ENTRIES
            | PARSED_WITH_ENTRIES_AND_OUTLINE
            | PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
            | PARSED_WITH_ENTRIES_AND_DEAD_CODE
            | PARSED_WITH_TODOS
            | PARSED_WITH_STATIC_DISPATCH) => {
                // What the kind says follows: entry facts after the surface,
                // and an outline and dead-code facts at the body's end.
                let static_dispatch = kind == PARSED_WITH_STATIC_DISPATCH;
                let (entries, outline, dead_code) = if kind == PARSED_WITH_TODOS || static_dispatch
                {
                    let flags = head.byte()?;
                    let allowed = HAS_OUTLINE
                        | HAS_DEAD_CODE
                        | if static_dispatch { STATIC_HAS_TODOS } else { 0 };
                    if flags & !allowed != 0 {
                        return Err(DecodeError("parsed record flags"));
                    }
                    if !static_dispatch || flags & STATIC_HAS_TODOS != 0 {
                        todos = Some(decode_todos(&mut head)?);
                    }
                    (true, flags & HAS_OUTLINE != 0, flags & HAS_DEAD_CODE != 0)
                } else {
                    (
                        matches!(
                            kind,
                            PARSED_WITH_ENTRIES
                                | PARSED_WITH_ENTRIES_AND_OUTLINE
                                | PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
                                | PARSED_WITH_ENTRIES_AND_DEAD_CODE
                        ),
                        matches!(
                            kind,
                            PARSED_WITH_OUTLINE
                                | PARSED_WITH_ENTRIES_AND_OUTLINE
                                | PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
                        ),
                        matches!(
                            kind,
                            PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
                                | PARSED_WITH_ENTRIES_AND_DEAD_CODE
                        ),
                    )
                };
                let mut surface = decode_surface(&mut head)?;
                if entries {
                    decode_entries(&mut head, &mut surface)?;
                }
                head.finish()?;
                let mut tail = Decoder::new(body)?;
                let mut body = decode_body(&mut tail)?;
                if outline {
                    body.outline = Some(decode_outline(&mut tail)?);
                }
                if dead_code {
                    body.dead_code = Some(tail.blob()?.to_vec());
                }
                if static_dispatch {
                    body.static_dispatch = Some(tail.blob()?.to_vec());
                }
                tail.finish()?;
                RecordBody::Parsed {
                    surface: Box::new(surface),
                    body: Box::new(body),
                }
            }
            _ => return Err(DecodeError("record kind")),
        };
        Ok(Self {
            language,
            extractor_version,
            data,
            todos,
        })
    }

    /// The TODO markers of the record whose surface part is `surface`,
    /// read without decoding the rest of the record: they come right after
    /// its kind. `None` when the record holds none.
    ///
    /// # Errors
    ///
    /// When the bytes before and of the markers are not well formed.
    pub fn todos_of(surface: &[u8]) -> Result<Option<TodoMarkers>, DecodeError> {
        let mut head = Decoder::new(surface)?;
        head.str()?;
        head.str()?;
        match head.byte()? {
            CONFIG_WITH_TODOS => decode_todos(&mut head).map(Some),
            MARKERS if head.bool()? => decode_todos(&mut head).map(Some),
            PARSED_WITH_TODOS => {
                head.byte()?;
                decode_todos(&mut head).map(Some)
            }
            PARSED_WITH_STATIC_DISPATCH => {
                if head.byte()? & STATIC_HAS_TODOS != 0 {
                    decode_todos(&mut head).map(Some)
                } else {
                    Ok(None)
                }
            }
            CONFIG
            | MARKERS
            | PARSED
            | PARSED_WITH_OUTLINE
            | PARSED_WITH_ENTRIES
            | PARSED_WITH_ENTRIES_AND_OUTLINE
            | PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
            | PARSED_WITH_ENTRIES_AND_DEAD_CODE => Ok(None),
            _ => Err(DecodeError("record kind")),
        }
    }
}

/// The markers' language, then a byte saying whether the content was text
/// (1) or not (0), then for text the count of markers and each marker.
fn encode_todos(out: &mut Encoder, todos: &TodoMarkers) {
    out.str(&todos.language);
    out.bool(todos.items.is_some());
    if let Some(items) = &todos.items {
        out.usize(items.len());
        for item in items {
            out.u32(item.line);
            out.str(&item.marker);
            out.opt_str(item.author.as_deref());
            out.str(&item.text);
        }
    }
}

fn decode_todos(input: &mut Decoder<'_>) -> Result<TodoMarkers, DecodeError> {
    let language = input.string()?;
    let items = if input.bool()? {
        let count = input.count()?;
        let mut items = Vec::with_capacity(count);
        for _ in 0..count {
            items.push(TodoMarker {
                line: input.u32()?,
                marker: input.string()?,
                author: input.opt_string()?,
                text: input.string()?,
            });
        }
        Some(items)
    } else {
        None
    };
    Ok(TodoMarkers { language, items })
}

fn encode_surface(out: &mut Encoder, surface: &Surface) {
    out.usize(surface.symbols.len());
    for symbol in &surface.symbols {
        out.byte(match symbol.is_test {
            None => 0,
            Some(false) => 1,
            Some(true) => 2,
        });
        out.u32(symbol.ordinal);
        out.str(&symbol.name);
        out.str(&symbol.scoped_name);
        out.str(&symbol.kind);
        out.bool(symbol.exported);
        out.bool(symbol.is_default_export);
        out.u32(symbol.start_line);
        out.u32(symbol.start_col);
        out.u32(symbol.end_line);
        out.u32(symbol.end_col);
        out.opt_str(symbol.signature.as_deref());
    }
    out.opt_str(surface.default_export_symbol.as_deref());
    out.strs(&surface.exported_symbols);
    out.strs(&surface.callable_symbols);
    out.strs(&surface.entry_point_symbols);
    out.opt_str(surface.go_package.as_deref());
    out.usize(surface.types.len());
    for fact in &surface.types {
        out.str(&fact.name);
        out.strs(&fact.bases);
        out.bool(fact.interface);
        out.bool(fact.closed);
    }
    out.usize(surface.methods.len());
    for fact in &surface.methods {
        out.str(&fact.owner);
        out.opt_str(fact.trait_name.as_deref());
        out.str(&fact.name);
        out.str(&fact.symbol);
        out.bool(fact.has_body);
        out.str(&fact.shape);
        out.bool(fact.private);
    }
    out.usize(surface.fields.len());
    for fact in &surface.fields {
        out.str(&fact.owner);
        out.str(&fact.name);
        out.str(&fact.ty);
    }
    out.usize(surface.returns.len());
    for fact in &surface.returns {
        out.str(&fact.symbol);
        out.str(&fact.ty);
    }
}

fn decode_surface(input: &mut Decoder<'_>) -> Result<Surface, DecodeError> {
    let count = input.count()?;
    let mut symbols = Vec::with_capacity(count);
    for _ in 0..count {
        symbols.push(Symbol {
            is_test: match input.byte()? {
                0 => None,
                1 => Some(false),
                2 => Some(true),
                _ => return Err(DecodeError("is_test")),
            },
            ordinal: input.u32()?,
            name: input.string()?,
            scoped_name: input.string()?,
            kind: input.string()?,
            exported: input.bool()?,
            is_default_export: input.bool()?,
            start_line: input.u32()?,
            start_col: input.u32()?,
            end_line: input.u32()?,
            end_col: input.u32()?,
            signature: input.opt_string()?,
            program_start: false,
            entry_attribute: None,
        });
    }
    let default_export_symbol = input.opt_string()?;
    let exported_symbols = input.strings()?;
    let callable_symbols = input.strings()?;
    let entry_point_symbols = input.strings()?;
    let go_package = input.opt_string()?;
    let count = input.count()?;
    let mut types = Vec::with_capacity(count);
    for _ in 0..count {
        types.push(TypeFact {
            name: input.string()?,
            bases: input.strings()?,
            interface: input.bool()?,
            closed: input.bool()?,
        });
    }
    let count = input.count()?;
    let mut methods = Vec::with_capacity(count);
    for _ in 0..count {
        methods.push(MethodFact {
            owner: input.string()?,
            trait_name: input.opt_string()?,
            name: input.string()?,
            symbol: input.string()?,
            has_body: input.bool()?,
            shape: input.string()?,
            private: input.bool()?,
        });
    }
    let count = input.count()?;
    let mut fields = Vec::with_capacity(count);
    for _ in 0..count {
        fields.push(FieldFact {
            owner: input.string()?,
            name: input.string()?,
            ty: input.string()?,
        });
    }
    let count = input.count()?;
    let mut returns = Vec::with_capacity(count);
    for _ in 0..count {
        returns.push(ReturnFact {
            symbol: input.string()?,
            ty: input.string()?,
        });
    }
    Ok(Surface {
        symbols,
        default_export_symbol,
        exported_symbols,
        callable_symbols,
        entry_point_symbols,
        go_package,
        types,
        methods,
        fields,
        returns,
    })
}

/// Each symbol's entry facts, in symbol order, after the rest of the
/// surface of a [`PARSED_WITH_ENTRIES`] or
/// [`PARSED_WITH_ENTRIES_AND_OUTLINE`] record.
fn encode_entries(out: &mut Encoder, surface: &Surface) {
    for symbol in &surface.symbols {
        out.bool(symbol.program_start);
        out.opt_str(symbol.entry_attribute.as_deref());
    }
}

fn decode_entries(input: &mut Decoder<'_>, surface: &mut Surface) -> Result<(), DecodeError> {
    for symbol in &mut surface.symbols {
        symbol.program_start = input.bool()?;
        symbol.entry_attribute = input.opt_string()?;
    }
    Ok(())
}

fn encode_body(out: &mut Encoder, body: &Body) {
    out.usize(body.imports.len());
    for import in &body.imports {
        out.u32(import.ordinal);
        out.str(&import.module_path);
        out.strs(&import.names);
        out.opt_str(import.default_import.as_deref());
        out.opt_str(import.namespace_import.as_deref());
        out.u64(import.byte_start);
        out.u64(import.byte_end);
        out.str(&import.raw_text);
        out.bool(import.type_only);
        out.bool(import.side_effect);
    }
    out.usize(body.python_bindings.len());
    for binding in &body.python_bindings {
        out.u64(binding.byte_start);
        out.u64(binding.byte_end);
        match &binding.target {
            None => out.bool(false),
            Some((module, symbol)) => {
                out.bool(true);
                out.str(module);
                out.str(symbol);
            }
        }
    }
    out.usize(body.references.len());
    for reference in &body.references {
        out.u32(reference.ordinal);
        out.byte(reference.kind.code());
        out.opt_str(reference.caller_symbol.as_deref());
        out.opt_str(reference.short_name.as_deref());
        out.opt_str(reference.full_ref.as_deref());
        out.opt_str(reference.module_path.as_deref());
        out.u32(reference.line);
        out.u64(reference.byte_start);
        out.u64(reference.byte_end);
        out.opt_str(reference.path_override.as_deref());
        out.opt_str(reference.local_name.as_deref());
        out.opt_str(reference.requested_name.as_deref());
        out.opt_str(reference.namespace_alias.as_deref());
        out.bool(reference.wildcard);
        out.opt_str(reference.import_kind.as_deref());
    }
    out.usize(body.call_sites.len());
    for site in &body.call_sites {
        out.u32(site.ordinal);
        out.opt_str(site.caller.as_deref());
        out.u32(site.line);
        out.opt_str(site.member.as_deref());
        out.opt_str(site.receiver.as_deref());
        out.bool(site.dynamic);
    }
    if let Some(outline) = &body.outline {
        out.usize(outline.len());
        for symbol in outline {
            out.str(&symbol.name);
            out.str(&symbol.kind);
            out.u32(symbol.start_line);
            out.u32(symbol.start_col);
            out.u32(symbol.end_line);
            out.u32(symbol.end_col);
            out.opt_str(symbol.signature.as_deref());
            out.strs(&symbol.scope_chain);
            out.bool(symbol.exported);
            out.opt_str(symbol.parent.as_deref());
        }
    }
    if let Some(facts) = &body.dead_code {
        out.blob(facts);
    }
    if let Some(facts) = &body.static_dispatch {
        out.blob(facts);
    }
}

fn decode_body(input: &mut Decoder<'_>) -> Result<Body, DecodeError> {
    let count = input.count()?;
    let mut imports = Vec::with_capacity(count);
    for _ in 0..count {
        imports.push(Import {
            ordinal: input.u32()?,
            module_path: input.string()?,
            names: input.strings()?,
            default_import: input.opt_string()?,
            namespace_import: input.opt_string()?,
            byte_start: input.u64()?,
            byte_end: input.u64()?,
            raw_text: input.string()?,
            type_only: input.bool()?,
            side_effect: input.bool()?,
        });
    }
    let count = input.count()?;
    let mut python_bindings = Vec::with_capacity(count);
    for _ in 0..count {
        let byte_start = input.u64()?;
        let byte_end = input.u64()?;
        let target = if input.bool()? {
            Some((input.string()?, input.string()?))
        } else {
            None
        };
        python_bindings.push(PythonBinding {
            byte_start,
            byte_end,
            target,
        });
    }
    let count = input.count()?;
    let mut references = Vec::with_capacity(count);
    for _ in 0..count {
        references.push(Reference {
            ordinal: input.u32()?,
            kind: RefKind::from_code(input.byte()?)?,
            caller_symbol: input.opt_string()?,
            short_name: input.opt_string()?,
            full_ref: input.opt_string()?,
            module_path: input.opt_string()?,
            line: input.u32()?,
            byte_start: input.u64()?,
            byte_end: input.u64()?,
            path_override: input.opt_string()?,
            local_name: input.opt_string()?,
            requested_name: input.opt_string()?,
            namespace_alias: input.opt_string()?,
            wildcard: input.bool()?,
            import_kind: input.opt_string()?,
        });
    }
    let count = input.count()?;
    let mut call_sites = Vec::with_capacity(count);
    for _ in 0..count {
        call_sites.push(CallSite {
            ordinal: input.u32()?,
            caller: input.opt_string()?,
            line: input.u32()?,
            member: input.opt_string()?,
            receiver: input.opt_string()?,
            dynamic: input.bool()?,
        });
    }
    Ok(Body {
        imports,
        python_bindings,
        references,
        call_sites,
        outline: None,
        dead_code: None,
        static_dispatch: None,
    })
}

/// The outline a body of a [`PARSED_WITH_OUTLINE`] record ends with.
fn decode_outline(input: &mut Decoder<'_>) -> Result<Vec<OutlineSymbol>, DecodeError> {
    let count = input.count()?;
    let mut outline = Vec::with_capacity(count);
    for _ in 0..count {
        outline.push(OutlineSymbol {
            name: input.string()?,
            kind: input.string()?,
            start_line: input.u32()?,
            start_col: input.u32()?,
            end_line: input.u32()?,
            end_col: input.u32()?,
            signature: input.opt_string()?,
            scope_chain: input.strings()?,
            exported: input.bool()?,
            parent: input.opt_string()?,
        });
    }
    Ok(outline)
}

crate::heap_bytes_none!(RecordKey, RefKind);
crate::heap_bytes_of_fields!(Symbol {
    is_test,
    ordinal,
    name,
    scoped_name,
    kind,
    exported,
    is_default_export,
    start_line,
    start_col,
    end_line,
    end_col,
    signature,
    program_start,
    entry_attribute,
});
crate::heap_bytes_of_fields!(Reference {
    ordinal,
    kind,
    caller_symbol,
    short_name,
    full_ref,
    module_path,
    line,
    byte_start,
    byte_end,
    path_override,
    local_name,
    requested_name,
    namespace_alias,
    wildcard,
    import_kind,
});
crate::heap_bytes_of_fields!(Import {
    ordinal,
    module_path,
    names,
    default_import,
    namespace_import,
    byte_start,
    byte_end,
    raw_text,
    type_only,
    side_effect,
});
crate::heap_bytes_of_fields!(PythonBinding {
    byte_start,
    byte_end,
    target,
});
crate::heap_bytes_of_fields!(TypeFact {
    name,
    bases,
    interface,
    closed,
});
crate::heap_bytes_of_fields!(MethodFact {
    owner,
    trait_name,
    name,
    symbol,
    has_body,
    shape,
    private,
});
crate::heap_bytes_of_fields!(FieldFact { owner, name, ty });
crate::heap_bytes_of_fields!(ReturnFact { symbol, ty });
crate::heap_bytes_of_fields!(CallSite {
    ordinal,
    caller,
    line,
    member,
    receiver,
    dynamic,
});
crate::heap_bytes_of_fields!(Surface {
    symbols,
    default_export_symbol,
    exported_symbols,
    callable_symbols,
    entry_point_symbols,
    go_package,
    types,
    methods,
    fields,
    returns,
});
crate::heap_bytes_of_fields!(OutlineSymbol {
    name,
    kind,
    start_line,
    start_col,
    end_line,
    end_col,
    signature,
    scope_chain,
    exported,
    parent,
});
crate::heap_bytes_of_fields!(Body {
    imports,
    python_bindings,
    references,
    call_sites,
    outline,
    dead_code,
    static_dispatch,
});
crate::heap_bytes_of_fields!(TodoMarker {
    line,
    marker,
    author,
    text,
});
crate::heap_bytes_of_fields!(TodoMarkers { language, items });
crate::heap_bytes_of_fields!(ContentRecord {
    language,
    extractor_version,
    data,
    todos,
});

impl crate::heap::HeapBytes for RecordBody {
    fn heap_bytes(&self, seen: &mut crate::heap::Seen) -> usize {
        match self {
            Self::Parsed { surface, body } => {
                crate::heap::HeapBytes::heap_bytes(surface, seen)
                    + crate::heap::HeapBytes::heap_bytes(body, seen)
            }
            Self::Config { source } => crate::heap::HeapBytes::heap_bytes(source, seen),
            Self::Markers => 0,
        }
    }
}

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

    fn sample() -> ContentRecord {
        ContentRecord {
            language: "rust".into(),
            extractor_version: "v1".into(),
            data: RecordBody::Parsed {
                surface: Box::new(Surface {
                    symbols: vec![Symbol {
                        is_test: Some(false),
                        ordinal: 7,
                        name: "open".into(),
                        scoped_name: "Store::open".into(),
                        kind: "method".into(),
                        exported: true,
                        is_default_export: false,
                        start_line: 3,
                        start_col: 4,
                        end_line: 9,
                        end_col: 5,
                        signature: Some("fn open() -> Self".into()),
                        program_start: false,
                        entry_attribute: Some("no_mangle".into()),
                    }],
                    exported_symbols: vec!["Store".into()],
                    callable_symbols: vec!["Store::open".into()],
                    go_package: None,
                    types: vec![TypeFact {
                        name: "Store".into(),
                        bases: vec!["Base".into()],
                        interface: false,
                        closed: true,
                    }],
                    methods: vec![MethodFact {
                        owner: "Store".into(),
                        trait_name: None,
                        name: "open".into(),
                        symbol: "Store::open".into(),
                        has_body: true,
                        shape: "()".into(),
                        private: false,
                    }],
                    fields: vec![FieldFact {
                        owner: "Store".into(),
                        name: "path".into(),
                        ty: "PathBuf".into(),
                    }],
                    returns: vec![ReturnFact {
                        symbol: "Store::open".into(),
                        ty: "Store".into(),
                    }],
                    ..Surface::default()
                }),
                body: Box::new(Body {
                    imports: vec![Import {
                        ordinal: 1,
                        module_path: "std::path".into(),
                        names: vec!["PathBuf".into()],
                        default_import: None,
                        namespace_import: None,
                        byte_start: 0,
                        byte_end: 22,
                        raw_text: "use std::path::PathBuf;".into(),
                        type_only: false,
                        side_effect: false,
                    }],
                    python_bindings: vec![PythonBinding {
                        byte_start: 2,
                        byte_end: 3,
                        target: Some(("m".into(), "f".into())),
                    }],
                    references: vec![Reference {
                        ordinal: 9,
                        kind: RefKind::Call,
                        caller_symbol: Some("Store::open".into()),
                        short_name: Some("new".into()),
                        full_ref: Some("PathBuf::new".into()),
                        module_path: None,
                        line: 5,
                        byte_start: 100,
                        byte_end: 112,
                        path_override: None,
                        local_name: None,
                        requested_name: None,
                        namespace_alias: None,
                        wildcard: false,
                        import_kind: None,
                    }],
                    call_sites: vec![CallSite {
                        ordinal: 11,
                        caller: Some("Store::open".into()),
                        line: 6,
                        member: Some("join".into()),
                        receiver: Some("self.path".into()),
                        dynamic: false,
                    }],
                    outline: None,
                    dead_code: None,
                    static_dispatch: None,
                }),
            },
            todos: None,
        }
    }

    fn todos(language: &str) -> TodoMarkers {
        TodoMarkers {
            language: language.into(),
            items: Some(vec![
                TodoMarker {
                    line: 4,
                    marker: "TODO".into(),
                    author: Some("ana".into()),
                    text: "close the store".into(),
                },
                TodoMarker {
                    line: 9,
                    marker: "FIXME".into(),
                    author: None,
                    text: String::new(),
                },
            ]),
        }
    }

    /// Every kind of record round-trips its TODO markers, which
    /// `todos_of` reads from the first part alone; a record without them
    /// encodes exactly as before records could hold them, and every cut of
    /// the first part is refused.
    #[test]
    fn todo_markers_round_trip_in_every_kind_and_read_from_the_first_part() {
        let mut parsed = sample();
        let RecordBody::Parsed { body, .. } = &mut parsed.data else {
            unreachable!("the sample is parsed");
        };
        body.outline = Some(outline());
        body.dead_code = Some(b"facts".to_vec());
        let without = parsed.clone();
        parsed.todos = Some(todos("rust"));
        let config = ContentRecord {
            language: "config".into(),
            extractor_version: "v1".into(),
            data: RecordBody::Config {
                source: b"# TODO: x\n".to_vec(),
            },
            todos: Some(todos("toml")),
        };
        let markers = ContentRecord {
            language: "markers-markdown".into(),
            extractor_version: "v1".into(),
            data: RecordBody::Markers,
            todos: Some(todos("markdown")),
        };
        let not_text = ContentRecord {
            todos: Some(TodoMarkers {
                language: "markdown".into(),
                items: None,
            }),
            ..markers.clone()
        };
        let empty_markers = ContentRecord {
            todos: None,
            ..markers.clone()
        };
        for record in [&parsed, &config, &markers, &not_text, &empty_markers] {
            let (surface, body) = record.encode();
            assert_eq!(&ContentRecord::decode(&surface, &body).unwrap(), record);
            assert_eq!(ContentRecord::todos_of(&surface).unwrap(), record.todos);
            for cut in 0..surface.len() {
                assert!(ContentRecord::decode(&surface[..cut], &body).is_err());
            }
        }
        let (surface, body) = without.encode();
        assert_eq!(ContentRecord::todos_of(&surface).unwrap(), None);
        assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), without);
        // Without markers the kind is the one records had before them.
        let mut head = Decoder::new(&surface).unwrap();
        head.str().unwrap();
        head.str().unwrap();
        assert_eq!(
            head.byte().unwrap(),
            PARSED_WITH_ENTRIES_OUTLINE_AND_DEAD_CODE
        );
    }

    fn outline() -> Vec<OutlineSymbol> {
        vec![
            OutlineSymbol {
                name: "Store".into(),
                kind: "struct".into(),
                start_line: 1,
                start_col: 0,
                end_line: 1,
                end_col: 33,
                signature: Some("pub struct Store".into()),
                scope_chain: Vec::new(),
                exported: true,
                parent: None,
            },
            OutlineSymbol {
                name: "open".into(),
                kind: "method".into(),
                start_line: 3,
                start_col: 4,
                end_line: 9,
                end_col: 5,
                signature: Some("fn open() -> Self".into()),
                scope_chain: vec!["Store".into()],
                exported: true,
                parent: Some("Store".into()),
            },
        ]
    }

    #[test]
    fn an_outline_round_trips_and_its_absence_is_told_by_the_record_kind() {
        let without = sample();
        let mut with = sample();
        let RecordBody::Parsed { body, .. } = &mut with.data else {
            unreachable!("the sample is parsed");
        };
        body.outline = Some(outline());
        let (surface, body) = with.encode();
        assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), with);
        let (plain_surface, plain_body) = without.encode();
        let decoded = ContentRecord::decode(&plain_surface, &plain_body).unwrap();
        assert_eq!(decoded, without);

        // The two surfaces differ only in the record kind.
        assert_eq!(surface.len(), plain_surface.len());
        let differing = surface
            .iter()
            .zip(&plain_surface)
            .filter(|(a, b)| a != b)
            .map(|(a, b)| (*a, *b))
            .collect::<Vec<_>>();
        assert_eq!(
            differing,
            vec![(PARSED_WITH_ENTRIES_AND_OUTLINE, PARSED_WITH_ENTRIES)]
        );

        // A body without its outline, or with one the kind does not
        // announce, is refused, as is every cut of a body with one.
        assert!(ContentRecord::decode(&surface, &plain_body).is_err());
        assert!(ContentRecord::decode(&plain_surface, &body).is_err());
        for cut in 0..body.len() {
            assert!(ContentRecord::decode(&surface, &body[..cut]).is_err());
        }

        // An empty outline is still an outline.
        let mut empty = sample();
        let RecordBody::Parsed { body, .. } = &mut empty.data else {
            unreachable!("the sample is parsed");
        };
        body.outline = Some(Vec::new());
        let (surface, body) = empty.encode();
        assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), empty);
    }

    #[test]
    fn dead_code_facts_round_trip_with_and_without_an_outline() {
        for outline in [None, Some(outline())] {
            let without = {
                let mut record = sample();
                let RecordBody::Parsed { body, .. } = &mut record.data else {
                    unreachable!("the sample is parsed");
                };
                body.outline.clone_from(&outline);
                record
            };
            let mut with = without.clone();
            let RecordBody::Parsed { body, .. } = &mut with.data else {
                unreachable!("the sample is parsed");
            };
            body.dead_code = Some(b"{\"facts\":1}".to_vec());
            let (surface, body) = with.encode();
            assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), with);
            let (plain_surface, plain_body) = without.encode();
            assert_eq!(
                ContentRecord::decode(&plain_surface, &plain_body).unwrap(),
                without
            );

            // The record kind alone says whether facts follow: a body without
            // them, or with facts the kind does not announce, is refused, as
            // is every cut of a body with them.
            assert!(ContentRecord::decode(&surface, &plain_body).is_err());
            assert!(ContentRecord::decode(&plain_surface, &body).is_err());
            for cut in 0..body.len() {
                assert!(ContentRecord::decode(&surface, &body[..cut]).is_err());
            }
        }
    }

    #[test]
    fn records_round_trip_and_refuse_damage() {
        let record = sample();
        let (surface, body) = record.encode();
        assert_eq!(ContentRecord::decode(&surface, &body).unwrap(), record);
        // Strings repeat across fields but are stored once.
        let text = String::from_utf8_lossy(&surface);
        assert_eq!(text.matches("Store::open").count(), 1);

        let config = ContentRecord {
            language: "config".into(),
            extractor_version: "v1".into(),
            data: RecordBody::Config {
                source: b"[package]\n".to_vec(),
            },
            todos: None,
        };
        let (head, tail) = config.encode();
        assert!(tail.is_empty());
        assert_eq!(ContentRecord::decode(&head, &tail).unwrap(), config);

        for cut in 0..body.len() {
            assert!(ContentRecord::decode(&surface, &body[..cut]).is_err());
        }
        for cut in 0..surface.len() {
            assert!(ContentRecord::decode(&surface[..cut], &body).is_err());
        }
    }

    /// A record of a kind written before symbols carried entry facts reads
    /// back with none, and the facts round-trip in the kinds that hold them.
    #[test]
    fn entry_facts_round_trip_and_older_records_read_without_them() {
        let mut record = sample();
        let RecordBody::Parsed { surface, .. } = &mut record.data else {
            unreachable!("the sample is parsed");
        };
        surface.symbols[0].program_start = true;
        let (head, tail) = record.encode();
        assert_eq!(ContentRecord::decode(&head, &tail).unwrap(), record);

        // The encoding records had before entry facts: the same surface and
        // body under the older kind byte, without the facts.
        let RecordBody::Parsed { surface, body } = &record.data else {
            unreachable!("the sample is parsed");
        };
        let mut old = Encoder::default();
        old.str(&record.language);
        old.str(&record.extractor_version);
        old.byte(PARSED);
        encode_surface(&mut old, surface);
        let mut old_tail = Encoder::default();
        encode_body(&mut old_tail, body);
        let decoded = ContentRecord::decode(&old.finish(), &old_tail.finish()).unwrap();
        let RecordBody::Parsed { surface, .. } = decoded.data else {
            unreachable!("the record is parsed");
        };
        assert!(!surface.symbols[0].program_start);
        assert_eq!(surface.symbols[0].entry_attribute, None);
        assert_eq!(surface.symbols[0].scoped_name, "Store::open");
    }
}