fdu-core 0.1.0

The fdu engine: incremental hierarchical tallies over large directory trees
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
//! Stored-state identity: which requests a stored tier may answer.
//!
//! A **tier** is a unit of identity: entries and their roll-ups, `.gitignore` control
//! state, and content records. A **store** holds one or more tiers: a metadata snapshot
//! holds the entry and control tiers, a content sidecar holds the content tier, and a
//! retained [`Index`](crate::Index) holds all three. A store records the identity of every
//! tier it holds, and a stored tier answers a request only when the request's identity
//! for that tier is one it serves.
//!
//! Each identity is the engine fingerprint plus exactly the request parts that change the
//! tier's values. Operational settings such as worker counts, batch sizes, and scan order
//! never appear here, so they can never invalidate a store, and a request part that
//! changes a tier's values always does.

use crate::content::{
    AnalysisSet, AnalyzerId, AnalyzerVersion, ContentProvenance, OptionsFingerprint,
};
use crate::control::ControlLimits;
use crate::engine_contract::ScanScope;

/// Version of the fixed `.gitignore` control semantics, the first thing
/// [`ControlTierIdentity::ignore_rules_fingerprint`] hashes.
const IGNORE_RULES_VERSION: u64 = 2;

/// Which entries a scan retains: its depth, symlink, filesystem-boundary, hidden-entry,
/// and special-object settings.
///
/// This is [`ScanScope`] without its type-rules, reducer-set, and ignore-rules
/// fingerprints: the filesystem-admission identity of a validated scan configuration. A
/// store's entry tier records it, and an opened root binds it in
/// [`EngineVersion::scope`](crate::EngineVersion::scope). Root binding and execution policy
/// are deliberately absent, so two roots may share it without claiming to be the same live
/// session.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct EntryScope {
    /// Maximum retained relative depth, or unlimited when absent.
    pub max_depth: Option<usize>,
    /// Whether directory symlinks are followed.
    pub follow_symlinks: bool,
    /// Whether traversal stays on the root filesystem.
    pub one_filesystem: bool,
    /// Identity of leading-dot component admission and its exact-name allowlist.
    pub hidden_fingerprint: u64,
    /// Whether filesystem objects outside files, directories, and symlinks are excluded.
    pub exclude_special: bool,
}

/// Identity of an entry tier: the entries a store holds and the roll-ups derived from
/// them.
///
/// `.gitignore` observation is deliberately not part of it. Reading control files changes
/// which entries are classified as ignored, never which entries exist or what they
/// measure, so a store taken with observation on and one taken with it off hold equal
/// entry tiers. That is what lets a later projection serve one from the other.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct EntryTierIdentity {
    /// The engine fingerprint of the build that produced the tier
    /// ([`crate::snapshot::engine_fingerprint`]).
    pub engine: u64,
    /// Which entries a scan retains.
    pub scope: EntryScope,
    /// Identity of the type-classification rules roll-ups were tallied under.
    pub type_rules_fingerprint: u64,
    /// Identity of the enabled reducer set.
    pub reducers_fingerprint: u64,
}

impl EntryTierIdentity {
    /// The entry tier identity this build gives an index of `scope`.
    pub fn of_scope(scope: ScanScope) -> Self {
        Self {
            engine: crate::snapshot::engine_fingerprint(),
            scope: scope.entry_scope(),
            type_rules_fingerprint: scope.type_rules_fingerprint,
            reducers_fingerprint: scope.reducers_fingerprint,
        }
    }
}

/// Identity of a `.gitignore` control tier.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum ControlTierIdentity {
    /// No control file was read and no entry was classified, so the tier cannot say
    /// whether any entry is ignored.
    NotObserved,
    /// Control files were read and admitted under `limits`.
    Observed {
        /// The budget and line limit that decided which control files apply.
        limits: ControlLimits,
    },
}

impl ControlTierIdentity {
    /// Whether the tier observed control state.
    pub const fn is_observed(self) -> bool {
        matches!(self, Self::Observed { .. })
    }

    /// The ignore-rules fingerprint a [`ScanScope`] with this control tier carries.
    ///
    /// Zero is reserved for [`Self::NotObserved`], which is what
    /// [`ScanScope::observes_controls`] tests. An observed tier hashes the control
    /// semantics version and each limit in turn with FNV-1a and is never zero, so a scope
    /// taken under one budget or line limit never matches one taken under another.
    pub fn ignore_rules_fingerprint(self) -> u64 {
        const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
        const FNV_PRIME: u64 = 0x100_0000_01b3;
        const UNBOUNDED: u8 = 0;
        const BOUNDED: u8 = 1;

        let Self::Observed { limits } = self else {
            return 0;
        };
        let mut fingerprint = FNV_OFFSET_BASIS;
        let mut mix = |bytes: &[u8]| {
            for byte in bytes {
                fingerprint ^= u64::from(*byte);
                fingerprint = fingerprint.wrapping_mul(FNV_PRIME);
            }
        };
        mix(&IGNORE_RULES_VERSION.to_le_bytes());
        for limit in [limits.budget, limits.line_limit] {
            match limit {
                None => mix(&[UNBOUNDED]),
                Some(limit) => {
                    mix(&[BOUNDED]);
                    mix(&u64::try_from(limit).unwrap_or(u64::MAX).to_le_bytes());
                }
            }
        }
        fingerprint.max(1)
    }
}

/// The identity of every tier a metadata snapshot holds.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct SnapshotIdentity {
    /// The entry tier.
    pub entries: EntryTierIdentity,
    /// The `.gitignore` control tier.
    pub controls: ControlTierIdentity,
}

impl SnapshotIdentity {
    /// The scope an index holding these tiers records.
    pub fn scan_scope(self) -> ScanScope {
        let EntryScope {
            max_depth,
            follow_symlinks,
            one_filesystem,
            hidden_fingerprint,
            exclude_special,
        } = self.entries.scope;
        ScanScope {
            max_depth,
            follow_symlinks,
            one_filesystem,
            hidden_fingerprint,
            exclude_special,
            ignore_rules_fingerprint: self.controls.ignore_rules_fingerprint(),
            type_rules_fingerprint: self.entries.type_rules_fingerprint,
            reducers_fingerprint: self.entries.reducers_fingerprint,
        }
    }
}

/// Identity of a content tier: the per-file analysis records a sidecar holds.
///
/// The entry tier's identity rather than the whole snapshot's, because no metric depends on
/// `.gitignore` observation: every regular file in scope is an analysis candidate whether
/// or not it is ignored. Then the analyzer set the records were produced for, and the
/// analyzers' identities, versions, and options.
///
/// The type rules the records were classified under are the entry tier's, and stated only
/// there: a record's [`ContentProvenance`] is this identity's entry-tier type rules and its
/// [`AnalyzerProvenance`].
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ContentTierIdentity {
    /// The entry tier the records were analyzed over, including their type rules.
    pub entries: EntryTierIdentity,
    /// The analyzer set the tier holds records for.
    pub analysis: AnalysisSet,
    /// The options and analyzer versions the records were produced under.
    pub provenance: AnalyzerProvenance,
}

/// The analyzers a content tier's records were produced by, and the options they ran with.
///
/// A record's [`ContentProvenance`] without its type-rules fingerprint, which the content
/// tier's [`EntryTierIdentity`] holds.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct AnalyzerProvenance {
    /// Identity of the semantic analyzer options.
    pub options_fingerprint: OptionsFingerprint,
    /// Each analyzer the records ran, with its version, in the order the set enables them.
    pub analyzers: Vec<(AnalyzerId, AnalyzerVersion)>,
}

impl ContentTierIdentity {
    /// The provenance each record of this tier carries.
    pub(crate) fn record_provenance(&self) -> ContentProvenance {
        ContentProvenance {
            type_rules_fingerprint: self.entries.type_rules_fingerprint,
            options_fingerprint: self.provenance.options_fingerprint,
            analyzers: self.provenance.analyzers.clone(),
        }
    }

    /// Identity produced by this build for the requested entry tier and analyzers.
    pub fn for_request(entries: EntryTierIdentity, analysis: AnalysisSet) -> Self {
        let provenance = ContentProvenance::for_request(
            crate::content::AnalysisRequest { profile: analysis, ..Default::default() },
            entries.type_rules_fingerprint,
        );
        Self {
            entries,
            analysis,
            provenance: AnalyzerProvenance {
                options_fingerprint: provenance.options_fingerprint,
                analyzers: provenance.analyzers,
            },
        }
    }

    /// Admit a stored content identity and return the projection that may consume it.
    ///
    /// Equality is the only lawful content projection today. Adding another relation
    /// requires implementing its record and tier projection here, not widening a reader.
    #[must_use]
    pub fn admit(&self, stored: &Self) -> Option<ContentAdmission<'_>> {
        self.admit_parts(
            stored.entries,
            stored.analysis,
            stored.provenance.options_fingerprint,
            &stored.provenance.analyzers,
        )
    }

    fn admit_parts(
        &self,
        entries: EntryTierIdentity,
        analysis: AnalysisSet,
        options: OptionsFingerprint,
        analyzers: &[(AnalyzerId, AnalyzerVersion)],
    ) -> Option<ContentAdmission<'_>> {
        (entries == self.entries
            && analysis == self.analysis
            && options == self.provenance.options_fingerprint
            && analyzers == self.provenance.analyzers)
            .then_some(ContentAdmission { identity: self })
    }

    /// Admission for an observation already tied to this entry tier by its candidate.
    /// This compares borrowed components, without allocating provenance per file.
    pub(crate) fn admit_record(
        &self,
        analysis: AnalysisSet,
        provenance: &ContentProvenance,
    ) -> Option<ContentAdmission<'_>> {
        self.admit_parts(
            EntryTierIdentity {
                type_rules_fingerprint: provenance.type_rules_fingerprint,
                ..self.entries
            },
            analysis,
            provenance.options_fingerprint,
            &provenance.analyzers,
        )
    }
}

/// Proof that stored content can be projected to one requested identity.
///
/// The private constructor is the shared admission relation. Consumers apply this proof
/// to records or a borrowed record set; the proof never widens the requested analyzer set.
#[must_use = "admission must be applied before consuming stored content"]
#[derive(Clone, Copy, Debug)]
pub struct ContentAdmission<'a> {
    identity: &'a ContentTierIdentity,
}

impl<'a> ContentAdmission<'a> {
    /// The identity the projected content answers.
    pub const fn identity(self) -> &'a ContentTierIdentity {
        self.identity
    }

    pub(crate) fn record(self, record: crate::content::FileAnalysis) -> Option<AdmittedRecord<'a>> {
        record
            .matches_profile(self.identity.analysis)
            .then_some(AdmittedRecord { identity: self.identity, record })
    }

    pub(crate) fn project(
        self,
        content: &crate::content::ContentIndex,
    ) -> Option<ContentProjection<'_>> {
        let _admission = self.identity.admit(content.identity()?)?;
        Some(ContentProjection { content })
    }
}

/// A record set projected through the content serving relation for one request.
#[derive(Clone, Copy)]
pub(crate) struct ContentProjection<'a> {
    content: &'a crate::content::ContentIndex,
}

impl<'a> ContentProjection<'a> {
    pub(crate) fn identity(self) -> &'a ContentTierIdentity {
        self.content.identity().expect("admitted tier has an identity")
    }
    pub(crate) fn len(self) -> usize {
        self.content.len()
    }
    pub(crate) fn state(self) -> Option<crate::content::ContentTierState> {
        self.content.state()
    }
    pub(crate) fn file(self, path: &std::path::Path) -> Option<&'a crate::content::FileAnalysis> {
        self.content.file(path)
    }
    pub(crate) fn records(
        self,
    ) -> impl Iterator<Item = (&'a std::path::Path, &'a crate::content::FileAnalysis)> {
        self.content.records()
    }
}

/// A decoded record whose identity and analyzer slots have passed admission.
#[must_use]
pub(crate) struct AdmittedRecord<'a> {
    identity: &'a ContentTierIdentity,
    record: crate::content::FileAnalysis,
}

impl AdmittedRecord<'_> {
    pub(crate) fn value(&self) -> &crate::content::FileAnalysis {
        &self.record
    }

    pub(crate) fn into_record(self) -> crate::content::FileAnalysis {
        self.record
    }

    pub(crate) fn for_tier(
        self,
        wanted: &ContentTierIdentity,
    ) -> Option<crate::content::FileAnalysis> {
        wanted.admit(self.identity)?.record(self.record).map(AdmittedRecord::into_record)
    }
}

/// How a stored tier answers a request.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum Serves {
    /// The stored identity equals the requested one, so the stored tier holds what a cold
    /// run of the request would build.
    Exact,
    /// The stored entry tier equals the requested one, and an observed control tier can
    /// be discarded to produce the controls-off index a cold run would build.
    ProjectControlsOff,
    /// The stored tier cannot answer the request, which is a miss.
    Refuse,
}

/// Whether a snapshot of the `stored` identity answers a request for `wanted`.
///
/// Equality: a snapshot serves exactly the request whose identity for every tier equals
/// its own. Any relation beyond equality arrives with a projection that yields what a cold
/// run of `wanted` would, and is proven by its own test.
pub fn serves_snapshot(stored: SnapshotIdentity, wanted: SnapshotIdentity) -> Serves {
    if stored == wanted {
        Serves::Exact
    } else if stored.entries == wanted.entries
        && stored.controls.is_observed()
        && wanted.controls == ControlTierIdentity::NotObserved
    {
        Serves::ProjectControlsOff
    } else {
        Serves::Refuse
    }
}

// ---- write rules ----
//
// Each tier is written by what an absent item in it means. An absent entry changes every
// roll-up above it, so the entry tier is written only when the pass verified all of it.
// An absent content record is a miss that reads the file again, so records are written one
// at a time, each when the pass verified it.

/// Whether `index`'s entry tier, and the control tier stored with it, may be written.
///
/// Only a complete, fresh index: a snapshot missing an entry would be served as the tree's
/// totals on the next run, and an older complete snapshot is better than that.
pub(crate) fn entries_writable(index: &crate::Index) -> bool {
    index.freshness() == crate::Freshness::Fresh
        && index.state().coverage == crate::engine_contract::Coverage::Complete
}

/// Whether the content record `record` for the file at `path` may be written.
///
/// A record is written when it describes a file this pass verified: the index holds a
/// regular file there whose fingerprint is the record's, the entry was scanned or
/// revalidated by the pass rather than retained from a snapshot, and reading it did not
/// fail. A file the pass verified was listed by its parent, so its subtree was verified
/// down to it. A record under a subtree the pass could not verify describes a retained
/// file nobody checked, so it is left out, and the next run that verifies the file reads
/// it again.
pub(crate) fn content_record_writable(
    index: &crate::Index,
    path: &std::path::Path,
    record: &crate::content::FileAnalysis,
) -> bool {
    if !record.is_reusable() {
        return false;
    }
    // A complete, fresh pass verified every entry, and the content tier holds only records
    // that match their live entry, because a metadata change invalidates a file's record
    // and a commit checks the entry it lands on. So only a partial pass asks per file, and
    // the common write pays no lookup per record.
    if entries_writable(index) {
        return true;
    }
    let crate::PathState::Present { kind: crate::EntryKind::File, attrs } = index.path_state(path)
    else {
        return false;
    };
    attrs.fingerprint() == record.fingerprint
        && index.provenance(path).is_some_and(crate::Provenance::is_verified)
}

/// Whether `index`'s content tier may be written beside the store that holds
/// `stored_entries`, the entry tier of the snapshot already stored for its root, if any.
///
/// After a complete pass, always: the snapshot is written with it. After a partial pass,
/// only beside a snapshot of the same entry tier, which the sidecar pairs with, because a partial
/// pass under another identity writes no snapshot, so replacing the sidecar would evict the
/// records that pair with the snapshot that stays.
pub(crate) fn content_tier_writable(
    index: &crate::Index,
    stored_entries: impl FnOnce() -> Option<EntryTierIdentity>,
) -> bool {
    entries_writable(index)
        || stored_entries().is_some_and(|stored| stored == index.snapshot_identity().entries)
}

// ---- fixed-width codecs ----
//
// Every store writes its tier identities in these encodings. Each is fixed-width and
// canonical: one identity has exactly one encoding, and a decoder refuses any byte that no
// encoder writes, so two stores hold equal identities exactly when their encoded bytes are
// equal. The engine fingerprint is not encoded here: a store writes it once, in its
// prologue beside the magic and format version, and every tier identity it holds shares
// it.

/// Encoded width of an optional bound: a tag, then eight value bytes.
pub(crate) const BOUND_BYTES: usize = 1 + 8;

/// Encoded width of an [`EntryTierIdentity`] after its engine fingerprint: the maximum
/// depth as a bound, the scope flags, and the hidden-entry, type-rules, and reducer-set
/// fingerprints.
pub(crate) const ENTRY_TIER_BYTES: usize = BOUND_BYTES + 1 + 8 + 8 + 8;

/// Encoded width of a [`ControlTierIdentity`]: the observation tag, then the budget and the
/// line limit, each a bound.
pub(crate) const CONTROL_TIER_BYTES: usize = 1 + 2 * BOUND_BYTES;

/// Encoded width of a [`SnapshotIdentity`] after its engine fingerprint.
pub(crate) const SNAPSHOT_IDENTITY_BYTES: usize = ENTRY_TIER_BYTES + CONTROL_TIER_BYTES;

/// Scope flag for symlink-following traversal.
const SCOPE_FOLLOW_SYMLINKS: u8 = 1 << 0;
/// Scope flag for staying on the root filesystem.
const SCOPE_ONE_FILESYSTEM: u8 = 1 << 1;
/// Scope flag for excluding native special objects.
const SCOPE_EXCLUDE_SPECIAL: u8 = 1 << 2;
/// Every scope flag this encoding defines.
const SCOPE_KNOWN_FLAGS: u8 = SCOPE_FOLLOW_SYMLINKS | SCOPE_ONE_FILESYSTEM | SCOPE_EXCLUDE_SPECIAL;

/// Control tier tag for a tier that observed nothing, whose limit fields are all zero.
const CONTROLS_NOT_OBSERVED: u8 = 0;
/// Control tier tag for an observed tier, whose limit fields follow.
const CONTROLS_OBSERVED: u8 = 1;

/// Bound tag for no bound, whose eight value bytes are zero.
const UNBOUNDED: u8 = 0;
/// Bound tag for a bound, whose value is the eight bytes that follow.
const BOUNDED: u8 = 1;

// Every bound is a `usize`, which fits the eight bytes that encode it on every target, so a
// bound is never refused or aliased at encode.
const _: () = assert!(usize::BITS <= u64::BITS, "a bound fits its eight encoded bytes");

/// Fills a fixed-width encoding field by field.
struct FixedWriter<const N: usize> {
    bytes: [u8; N],
    at: usize,
}

impl<const N: usize> FixedWriter<N> {
    const fn new() -> Self {
        Self { bytes: [0; N], at: 0 }
    }

    fn put(&mut self, field: &[u8]) {
        let end = self.at + field.len();
        self.bytes[self.at..end].copy_from_slice(field);
        self.at = end;
    }

    /// Write an optional bound as its tag and eight value bytes, zero when unbounded.
    ///
    /// A tag rather than a reserved value, so no bound can be mistaken for a sentinel.
    fn put_bound(&mut self, bound: Option<usize>) {
        let (tag, value) = match bound {
            None => (UNBOUNDED, 0),
            // Lossless: the width assertion above holds on every target this compiles for.
            Some(bound) => (BOUNDED, u64::try_from(bound).unwrap_or(u64::MAX)),
        };
        self.put(&[tag]);
        self.put(&value.to_le_bytes());
    }

    fn finish(self) -> [u8; N] {
        debug_assert_eq!(self.at, N, "every field of a fixed-width encoding is written");
        self.bytes
    }
}

/// A field holds bytes no encoder writes.
struct NotEncoded;

/// Reads a fixed-width encoding field by field.
struct FixedReader<'a> {
    rest: &'a [u8],
}

impl FixedReader<'_> {
    fn take<const W: usize>(&mut self) -> [u8; W] {
        let (field, rest) =
            self.rest.split_first_chunk::<W>().expect("a fixed-width encoding holds every field");
        self.rest = rest;
        *field
    }

    fn u8(&mut self) -> u8 {
        self.take::<1>()[0]
    }

    fn u64(&mut self) -> u64 {
        u64::from_le_bytes(self.take())
    }

    /// Read [`FixedWriter::put_bound`], refusing a tag or value no encoder writes.
    fn bound(&mut self) -> Result<Option<usize>, NotEncoded> {
        match (self.u8(), self.u64()) {
            (UNBOUNDED, 0) => Ok(None),
            (BOUNDED, value) => usize::try_from(value).map(Some).map_err(|_| NotEncoded),
            _ => Err(NotEncoded),
        }
    }
}

impl EntryTierIdentity {
    /// Encode every field but the engine fingerprint, which the store's prologue carries.
    pub(crate) fn encode(self) -> [u8; ENTRY_TIER_BYTES] {
        let scope = self.scope;
        let mut flags = 0u8;
        if scope.follow_symlinks {
            flags |= SCOPE_FOLLOW_SYMLINKS;
        }
        if scope.one_filesystem {
            flags |= SCOPE_ONE_FILESYSTEM;
        }
        if scope.exclude_special {
            flags |= SCOPE_EXCLUDE_SPECIAL;
        }
        let mut out = FixedWriter::new();
        out.put_bound(scope.max_depth);
        out.put(&[flags]);
        out.put(&scope.hidden_fingerprint.to_le_bytes());
        out.put(&self.type_rules_fingerprint.to_le_bytes());
        out.put(&self.reducers_fingerprint.to_le_bytes());
        out.finish()
    }

    /// Decode [`Self::encode`] under the engine fingerprint of the store that holds it, or
    /// `None` for a field no encoder writes.
    pub(crate) fn decode(engine: u64, bytes: &[u8; ENTRY_TIER_BYTES]) -> Option<Self> {
        let mut fields = FixedReader { rest: bytes };
        let max_depth = fields.bound().ok()?;
        let flags = fields.u8();
        if flags & !SCOPE_KNOWN_FLAGS != 0 {
            return None;
        }
        let scope = EntryScope {
            max_depth,
            follow_symlinks: flags & SCOPE_FOLLOW_SYMLINKS != 0,
            one_filesystem: flags & SCOPE_ONE_FILESYSTEM != 0,
            hidden_fingerprint: fields.u64(),
            exclude_special: flags & SCOPE_EXCLUDE_SPECIAL != 0,
        };
        Some(Self {
            engine,
            scope,
            type_rules_fingerprint: fields.u64(),
            reducers_fingerprint: fields.u64(),
        })
    }
}

impl ControlTierIdentity {
    /// Encode the observation and, when observed, both limits.
    pub(crate) fn encode(self) -> [u8; CONTROL_TIER_BYTES] {
        let mut out = FixedWriter::new();
        match self {
            Self::NotObserved => out.put(&[CONTROLS_NOT_OBSERVED; CONTROL_TIER_BYTES]),
            Self::Observed { limits } => {
                out.put(&[CONTROLS_OBSERVED]);
                out.put_bound(limits.budget);
                out.put_bound(limits.line_limit);
            }
        }
        out.finish()
    }

    /// Decode [`Self::encode`], or `None` for a tag or value no encoder writes.
    pub(crate) fn decode(bytes: &[u8; CONTROL_TIER_BYTES]) -> Option<Self> {
        let mut fields = FixedReader { rest: bytes };
        match fields.u8() {
            CONTROLS_NOT_OBSERVED => {
                bytes[1..].iter().all(|byte| *byte == 0).then_some(Self::NotObserved)
            }
            CONTROLS_OBSERVED => {
                let budget = fields.bound().ok()?;
                let line_limit = fields.bound().ok()?;
                Some(Self::Observed { limits: ControlLimits { budget, line_limit } })
            }
            _ => None,
        }
    }
}

impl SnapshotIdentity {
    /// Encode the entry tier's fields, then the control tier.
    pub(crate) fn encode(self) -> [u8; SNAPSHOT_IDENTITY_BYTES] {
        let mut out = FixedWriter::new();
        out.put(&self.entries.encode());
        out.put(&self.controls.encode());
        out.finish()
    }

    /// Decode [`Self::encode`] under the engine fingerprint of the snapshot that holds it.
    pub(crate) fn decode(engine: u64, bytes: &[u8; SNAPSHOT_IDENTITY_BYTES]) -> Option<Self> {
        let mut fields = FixedReader { rest: bytes };
        Some(Self {
            entries: EntryTierIdentity::decode(engine, &fields.take())?,
            controls: ControlTierIdentity::decode(&fields.take())?,
        })
    }
}

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

    fn limits(budget: Option<usize>, line_limit: Option<usize>) -> ControlLimits {
        ControlLimits { budget, line_limit }
    }

    #[test]
    fn snapshot_serving_is_equality_plus_observation_on_to_off() {
        let base = ScanConfig::default().snapshot_identity();
        assert_eq!(serves_snapshot(base, base), Serves::Exact);

        let entries = base.entries;
        let mut refused = vec![
            SnapshotIdentity {
                entries: EntryTierIdentity { engine: entries.engine ^ 1, ..entries },
                ..base
            },
            SnapshotIdentity {
                entries: EntryTierIdentity {
                    type_rules_fingerprint: entries.type_rules_fingerprint ^ 1,
                    ..entries
                },
                ..base
            },
            SnapshotIdentity {
                entries: EntryTierIdentity {
                    reducers_fingerprint: entries.reducers_fingerprint ^ 1,
                    ..entries
                },
                ..base
            },
            SnapshotIdentity {
                controls: ControlTierIdentity::Observed { limits: limits(None, None) },
                ..base
            },
        ];
        for config in [
            ScanConfig { max_depth: Some(1), ..ScanConfig::default() },
            ScanConfig { one_filesystem: true, ..ScanConfig::default() },
            ScanConfig { exclude_special: true, ..ScanConfig::default() },
            ScanConfig {
                hidden: Some(std::sync::Arc::new(crate::HiddenPolicy::prune_hidden(
                    std::iter::empty::<std::ffi::OsString>(),
                ))),
                ..ScanConfig::default()
            },
        ] {
            refused.push(config.snapshot_identity());
        }
        for wanted in refused {
            assert_eq!(serves_snapshot(base, wanted), Serves::Refuse, "{wanted:?}");
            assert_eq!(serves_snapshot(wanted, base), Serves::Refuse, "{wanted:?}");
        }
        let blind = SnapshotIdentity { controls: ControlTierIdentity::NotObserved, ..base };
        assert_eq!(serves_snapshot(base, blind), Serves::ProjectControlsOff);
        assert_eq!(serves_snapshot(blind, base), Serves::Refuse);
    }

    #[test]
    fn control_settings_change_only_the_control_tier() {
        let base = ScanConfig::default();
        for config in [
            ScanConfig { read_controls: false, ..base.clone() },
            ScanConfig { control_limits: limits(None, Some(1)), ..base.clone() },
            ScanConfig {
                read_controls: false,
                control_limits: limits(Some(1), None),
                ..base.clone()
            },
        ] {
            assert_eq!(config.snapshot_identity().entries, base.snapshot_identity().entries);
            assert_ne!(config.snapshot_identity().controls, base.snapshot_identity().controls);
        }
        // Limits decide nothing when nothing is observed, so they leave no trace.
        let blind = ScanConfig { read_controls: false, ..base.clone() };
        let blind_other_limits = ScanConfig { control_limits: limits(None, None), ..blind.clone() };
        assert_eq!(blind.snapshot_identity(), blind_other_limits.snapshot_identity());
        assert_eq!(blind.control_identity(), ControlTierIdentity::NotObserved);
    }

    #[test]
    fn the_ignore_rules_fingerprint_reserves_zero_for_an_unobserved_tier() {
        assert_eq!(ControlTierIdentity::NotObserved.ignore_rules_fingerprint(), 0);
        let defaults = ControlLimits::default();
        let observed = [
            defaults,
            limits(None, defaults.line_limit),
            limits(defaults.budget, None),
            limits(None, None),
            // The same values in each other's places are a different tier.
            limits(defaults.line_limit, defaults.budget),
        ]
        .map(|limits| ControlTierIdentity::Observed { limits }.ignore_rules_fingerprint());
        for (index, fingerprint) in observed.iter().enumerate() {
            assert_ne!(*fingerprint, 0);
            assert!(!observed[index + 1..].contains(fingerprint), "{observed:?}");
        }
    }

    #[test]
    fn the_scope_an_identity_composes_is_the_one_a_scan_records() {
        for config in [
            ScanConfig::default(),
            ScanConfig { read_controls: false, ..ScanConfig::default() },
            ScanConfig { control_limits: limits(None, None), ..ScanConfig::default() },
            ScanConfig { max_depth: Some(3), exclude_special: true, ..ScanConfig::default() },
        ] {
            let identity = config.snapshot_identity();
            let scope = identity.scan_scope();
            assert_eq!(scope, config.scope());
            assert_eq!(EntryTierIdentity::of_scope(scope), identity.entries);
            assert_eq!(scope.observes_controls(), identity.controls.is_observed());

            let index = crate::Index::new_with_config("/root", &config);
            assert_eq!(index.snapshot_identity(), identity);
            assert_eq!(index.control_identity(), config.control_identity());
        }
    }

    #[test]
    fn each_tier_is_writable_by_its_own_rule() {
        let config = ScanConfig::default();
        let entries = config.snapshot_identity().entries;
        let other =
            ScanConfig { max_depth: Some(2), ..ScanConfig::default() }.snapshot_identity().entries;

        let mut complete = crate::Index::new_with_config("/root", &config);
        complete.set_initial_freshness(true);
        assert!(entries_writable(&complete));
        for stored in [None, Some(entries), Some(other)] {
            assert!(
                content_tier_writable(&complete, || stored),
                "a complete pass writes: {stored:?}"
            );
        }

        let mut partial = crate::Index::new_with_config("/root", &config);
        partial.set_initial_freshness(false);
        assert!(!entries_writable(&partial), "an absent entry would change totals");
        assert!(content_tier_writable(&partial, || Some(entries)));
        for stored in [None, Some(other)] {
            assert!(!content_tier_writable(&partial, || stored), "mismatched pair: {stored:?}");
        }

        let mut unverified = complete.clone();
        unverified.mark_unverified();
        assert!(!entries_writable(&unverified), "a cache-only index verified nothing");
    }

    /// Identities that each differ from the default in one encoded field, including the
    /// edges of each range, so a codec that dropped any one field would alias two of them.
    fn identities() -> Vec<SnapshotIdentity> {
        let base = ScanConfig::default().snapshot_identity();
        let entries = base.entries;
        let defaults = ControlLimits::default();
        assert_eq!(entries.scope.max_depth, None);
        assert!(defaults.budget.is_some() && defaults.line_limit.is_some());
        let mut all = vec![base];
        for scope in [
            EntryScope { max_depth: Some(0), ..entries.scope },
            // The largest bound is a bound, never the unbounded depth.
            EntryScope { max_depth: Some(usize::MAX), ..entries.scope },
            EntryScope { follow_symlinks: true, ..entries.scope },
            EntryScope { one_filesystem: true, ..entries.scope },
            EntryScope { exclude_special: true, ..entries.scope },
            EntryScope { hidden_fingerprint: u64::MAX, ..entries.scope },
        ] {
            all.push(SnapshotIdentity { entries: EntryTierIdentity { scope, ..entries }, ..base });
        }
        for entries in [
            EntryTierIdentity { type_rules_fingerprint: 0, ..entries },
            EntryTierIdentity { reducers_fingerprint: u64::MAX, ..entries },
        ] {
            all.push(SnapshotIdentity { entries, ..base });
        }
        for controls in [
            ControlTierIdentity::NotObserved,
            ControlTierIdentity::Observed { limits: limits(None, defaults.line_limit) },
            ControlTierIdentity::Observed { limits: limits(Some(0), defaults.line_limit) },
            ControlTierIdentity::Observed { limits: limits(defaults.budget, None) },
            ControlTierIdentity::Observed { limits: limits(defaults.budget, Some(usize::MAX)) },
        ] {
            all.push(SnapshotIdentity { controls, ..base });
        }
        all
    }

    #[test]
    fn every_identity_round_trips_through_its_fixed_width_encoding() {
        let identities = identities();
        let encoded = identities.iter().map(|identity| identity.encode()).collect::<Vec<_>>();
        for (identity, bytes) in identities.iter().zip(&encoded) {
            let engine = identity.entries.engine;
            assert_eq!(SnapshotIdentity::decode(engine, bytes), Some(*identity));
            let (entry_bytes, control_bytes) = bytes.split_at(ENTRY_TIER_BYTES);
            assert_eq!(
                EntryTierIdentity::decode(engine, entry_bytes.try_into().expect("width")),
                Some(identity.entries)
            );
            assert_eq!(
                ControlTierIdentity::decode(control_bytes.try_into().expect("width")),
                Some(identity.controls)
            );
        }
        // Canonical: distinct identities never share an encoding.
        for (index, bytes) in encoded.iter().enumerate() {
            assert!(!encoded[index + 1..].contains(bytes), "{:?}", identities[index]);
        }
    }

    #[test]
    fn bytes_no_encoder_writes_are_refused() {
        let base = ScanConfig::default().snapshot_identity();
        let engine = base.entries.engine;
        let bounded = EntryTierIdentity {
            scope: EntryScope { max_depth: Some(3), ..base.entries.scope },
            ..base.entries
        };
        let (depth_tag_at, depth_at, flags_at) = (0, 1, BOUND_BYTES);
        let mut forged_entries = Vec::new();
        let mut unknown_flag = base.entries.encode();
        unknown_flag[flags_at] |= 1 << 7;
        forged_entries.push(unknown_flag);
        let mut unknown_depth_tag = bounded.encode();
        assert_eq!(unknown_depth_tag[depth_tag_at], BOUNDED);
        unknown_depth_tag[depth_tag_at] = 2;
        forged_entries.push(unknown_depth_tag);
        let mut unbounded_depth_with_a_value = base.entries.encode();
        assert_eq!(unbounded_depth_with_a_value[depth_tag_at], UNBOUNDED);
        unbounded_depth_with_a_value[depth_at] = 1;
        forged_entries.push(unbounded_depth_with_a_value);
        for bytes in forged_entries {
            assert_eq!(EntryTierIdentity::decode(engine, &bytes), None, "{bytes:?}");
        }

        let observed = ControlTierIdentity::Observed { limits: limits(None, Some(1)) };
        let controls = observed.encode();
        let (budget_tag_at, budget_at, line_tag_at) = (1, 2, 1 + BOUND_BYTES);
        assert_eq!(controls[budget_tag_at], UNBOUNDED);
        assert_eq!(controls[line_tag_at], BOUNDED);
        let mut forged = Vec::new();
        let mut unknown_tag = controls;
        unknown_tag[0] = 2;
        forged.push(unknown_tag);
        let mut unknown_limit_tag = controls;
        unknown_limit_tag[line_tag_at] = 2;
        forged.push(unknown_limit_tag);
        let mut unbounded_with_a_value = controls;
        unbounded_with_a_value[budget_at] = 1;
        forged.push(unbounded_with_a_value);
        let mut unobserved_with_limits = controls;
        unobserved_with_limits[0] = CONTROLS_NOT_OBSERVED;
        forged.push(unobserved_with_limits);
        for bytes in forged {
            assert_eq!(ControlTierIdentity::decode(&bytes), None, "{bytes:?}");
        }
    }

    /// A content tier states its type rules once, in its entry tier, and every record it
    /// holds carries exactly those rules.
    #[test]
    fn a_content_tier_holds_its_records_type_rules_in_its_entry_tier() {
        use crate::content::AnalysisRequest;

        let entries = ScanConfig::default().snapshot_identity().entries;
        let request = AnalysisRequest { profile: AnalysisSet::ALL, ..AnalysisRequest::default() };
        let records = ContentProvenance::for_request(request, entries.type_rules_fingerprint);
        let identity = ContentTierIdentity::for_request(entries, request.profile);
        assert_eq!(identity.record_provenance(), records);
        assert!(identity.admit_record(request.profile, &records).is_some());

        let other_rules = ContentProvenance::for_request(request, !entries.type_rules_fingerprint);
        assert!(identity.admit_record(request.profile, &other_rules).is_none(), "other type rules");
        let lines = AnalysisSet::NONE.with_lines();
        assert!(identity.admit_record(lines, &records).is_none(), "another analyzer set's label");
    }

    #[test]
    fn content_admission_refuses_every_identity_difference_and_applies_exact_identity() {
        let entries = ScanConfig::default().snapshot_identity().entries;
        let wanted = ContentTierIdentity::for_request(entries, AnalysisSet::ALL);
        assert_eq!(wanted.admit(&wanted).expect("exact admission").identity(), &wanted);
        let changes: &[fn(&mut ContentTierIdentity)] = &[
            |identity| identity.entries.engine ^= 1,
            |identity| identity.entries.scope.max_depth = Some(1),
            |identity| identity.entries.type_rules_fingerprint ^= 1,
            |identity| identity.entries.reducers_fingerprint ^= 1,
            |identity| identity.analysis = AnalysisSet::LINES_ONLY,
            |identity| identity.provenance.options_fingerprint.0 ^= 1,
            |identity| identity.provenance.analyzers[0].1.0 += 1,
            |identity| {
                identity.provenance.analyzers.pop();
            },
        ];
        for change in changes {
            let mut other = wanted.clone();
            change(&mut other);
            assert!(wanted.admit(&other).is_none(), "stored mismatch: {other:?}");
            assert!(other.admit(&wanted).is_none(), "requested mismatch: {other:?}");
        }
    }

    #[test]
    fn the_engine_fingerprint_comes_from_the_store_not_the_encoding() {
        let identity = ScanConfig::default().snapshot_identity();
        let bytes = identity.encode();
        let other = SnapshotIdentity::decode(!identity.entries.engine, &bytes).expect("decode");
        assert_eq!(other.entries.engine, !identity.entries.engine);
        assert_eq!(serves_snapshot(other, identity), Serves::Refuse);
    }
}