varve-core 0.46.0

Layer manifests, resolution, the core store, and verification wiring for varve
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
//! The signed line index (REQ-INDEXAUTH-001) — which layers a line HAS.
//!
//! varve signs each layer manifest and each realm, but the LISTING of a line's
//! layers has been the registry's raw `/tags/list`: unauthenticated. A
//! compromised or merely stale index host can HIDE a layer and every artifact
//! it does serve still verifies, so nothing detects it. Hiding also breaks
//! digest-pinned resolution, because a `LayerRef::Digest` is resolved by
//! enumerating tags and checking each candidate's payload digest.
//!
//! This is the Uptane insight applied to varve: rollback protection has to
//! survive a compromised Director/registry, not merely a tampered artifact.
//! Guix gets the equivalent property by authenticating its index git-history
//! through signed commits.
//!
//! The document is deliberately the same shape as `linestatus`: a DSSE
//! envelope under its own payload type, a monotonic per-line counter, and the
//! same refusal on a counter regression. Two signed documents about the same
//! line that disagree on how they are handled would be a bug generator.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::install::VerifyError;
use crate::layer::Line;

/// The authenticated payload type — a signed something-else (a layer manifest,
/// a line-status) cannot be replayed as an index.
pub const LINE_INDEX_PAYLOAD_TYPE: &str = "application/vnd.pulseengine.varve.line-index.v1+json";

/// The artifact type under which an index travels as an OCI referrer.
pub const LINE_INDEX_ARTIFACT_TYPE: &str = LINE_INDEX_PAYLOAD_TYPE;

/// One layer the realm asserts exists on this line.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct IndexedLayer {
    /// The layer id, e.g. "2026.08.2".
    pub layer: String,
    /// The digest of that layer's signed PAYLOAD — the same identity a pin's
    /// `digest` names, so an index entry and a pin can be compared directly.
    pub digest: String,
    /// The channel the layer was published on.
    pub channel: String,
    /// That layer's manifest counter. The anti-rollback high-water mark is
    /// keyed on a COUNTER, not a layer id, so the index must carry the counter
    /// or clause 4 cannot feed the mechanism it exists to protect. (An earlier
    /// draft returned the greatest layer id here, which type-checked and was
    /// useless.)
    pub counter: u64,
}

/// What the realm says a line contains.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LineIndex {
    /// The release line, e.g. "2026.08".
    pub line: String,
    /// Monotonic per-line document counter — a stale index must not silently
    /// replace a newer one, exactly as for line-status.
    pub counter: u64,
    /// RFC 3339 issue time of this document.
    #[serde(rename = "issued-at")]
    pub issued_at: String,
    /// Every layer of this line, in publication order.
    pub layers: Vec<IndexedLayer>,
}

#[derive(Debug, thiserror::Error)]
pub enum IndexError {
    #[error(transparent)]
    Verify(#[from] VerifyError),
    #[error("line-index payload is not valid: {0}")]
    Payload(String),
    #[error(
        "refusing stale line-index for {line}: presented counter {presented}, cached {cached} — \
         a withdrawn or superseded index cannot be replayed over a newer one"
    )]
    Stale {
        line: String,
        presented: u64,
        cached: u64,
    },
    #[error(
        "the realm's signed index for {line} names layer {layer} ({digest}), which this source \
         does not serve. A source that hides a layer is either compromised or stale; every \
         layer it DOES serve still verifies, which is exactly why this check exists. Use a \
         different source, or obtain a newer signed index."
    )]
    Omitted {
        line: String,
        layer: String,
        digest: String,
    },
    #[error(
        "realm '{realm}' declares that it publishes a signed line index, but none was found for \
         {line}. Either the source is not serving it, or the realm's declaration is wrong — \
         varve will not fall back to an unauthenticated listing for a realm that promised one."
    )]
    Missing { realm: String, line: String },
    #[error("line-index document is for line {document}, not {expected}")]
    WrongLine { document: String, expected: String },
    #[error("io error at {path}: {source}")]
    Io {
        path: String,
        #[source]
        source: std::io::Error,
    },
}

impl IndexError {
    /// May another source be asked after this failure? (REQ-MIRROR-002.)
    ///
    /// Yes where THIS source's index is unavailable, unauthentic, malformed,
    /// for another line, or hides a layer the signed index names: another
    /// source may serve it properly. `Stale` also fails over: every source is
    /// judged against the same persisted local cache, so a later one can
    /// never pass below it, and ending the search there would only cost
    /// availability (a primary restored from backup, a stale CDN tag). No for
    /// local I/O. Exhaustive, no `_` arm.
    pub fn fails_over(&self) -> bool {
        match self {
            IndexError::Verify(_)
            | IndexError::Payload(_)
            | IndexError::WrongLine { .. }
            | IndexError::Omitted { .. }
            | IndexError::Missing { .. }
            | IndexError::Stale { .. } => true,
            IndexError::Io { .. } => false,
        }
    }
}

impl LineIndex {
    /// Verify an envelope against the realm's trust root and parse it.
    pub fn verify_and_parse(
        envelope: &[u8],
        trust: &crate::keyroles::RealmTrust,
    ) -> Result<Self, IndexError> {
        let payload = trust.verify_bytes(envelope, LINE_INDEX_PAYLOAD_TYPE)?;
        serde_json::from_slice(&payload).map_err(|e| IndexError::Payload(e.to_string()))
    }

    /// Sign an index (the producing side — CI, beside deposit).
    pub fn sign(&self, secret_key: &[u8], key_id: &str) -> Result<String, IndexError> {
        let signer = crate::verify::FileSigner::new_unchecked(secret_key, key_id)?;
        self.sign_with(&signer)
    }

    /// [`LineIndex::sign`] with any [`crate::verify::RealmSigner`].
    pub fn sign_with(&self, signer: &dyn crate::verify::RealmSigner) -> Result<String, IndexError> {
        let payload = serde_json::to_vec_pretty(self).expect("index serializes");
        Ok(crate::verify::dsse_sign_with(
            &payload,
            LINE_INDEX_PAYLOAD_TYPE,
            signer,
        )?)
    }

    /// The line this document is about.
    pub fn line(&self) -> Result<Line, IndexError> {
        self.line
            .parse()
            .map_err(|e: crate::layer::LayerIdError| IndexError::Payload(e.to_string()))
    }

    /// Clause 3: every layer the index names must be present in what the
    /// source actually offers. `served` is the set of layer ids the source
    /// enumerated. Returns the FIRST omission, named — not a boolean, because
    /// an error a user cannot act on is not a check.
    pub fn refuse_omission(&self, served: &[String]) -> Result<(), IndexError> {
        for entry in &self.layers {
            if !served.iter().any(|s| s == &entry.layer) {
                return Err(IndexError::Omitted {
                    line: self.line.clone(),
                    layer: entry.layer.clone(),
                    digest: entry.digest.clone(),
                });
            }
        }
        Ok(())
    }

    /// Clause 4: the high-water mark this index justifies, independent of what
    /// the source chose to serve. A registry that hides the newest layer must
    /// not thereby lower the bar a later install has to clear, which is the
    /// whole point of authenticating the listing.
    ///
    /// The greatest counter the realm asserts for this line. `None` for an
    /// empty index, which asserts nothing and must not be mistaken for a mark
    /// of zero.
    /// Which layers of this index are newer than `pinned`, and which entries
    /// could not be ordered at all (REQ-LAYERDIFF-001 clause 1).
    ///
    /// IN THE LIB, not in the CLI that calls it, because it is a DECISION and
    /// the mutation gate's kill criteria are `--workspace --lib`. A comparison
    /// living in `main.rs` is one the gate cannot reach, and this particular
    /// comparison is one I got wrong: measuring a real realm on 2026-10-01 I
    /// ordered layer ids as STRINGS, which puts `2026.09.12` before
    /// `2026.09.2`, and reported a staleness figure wrong by eight layers.
    /// `LayerId` orders by `(line, patch)` numerically.
    ///
    /// An entry that will not parse is RETURNED, never dropped: a layer this
    /// varve cannot order is one it cannot promise is not newer, and dropping
    /// it would turn "I do not understand this entry" into "there is nothing
    /// newer" — the same collapse of two different answers that makes an
    /// unauthenticated tag listing useless for this question.
    pub fn newer_than<'a>(
        &'a self,
        pinned: &crate::layer::LayerId,
    ) -> (Vec<&'a IndexedLayer>, Vec<&'a str>) {
        let mut newer: Vec<&IndexedLayer> = self
            .layers
            .iter()
            .filter(|l| {
                l.layer
                    .parse::<crate::layer::LayerId>()
                    .is_ok_and(|id| &id > pinned)
            })
            .collect();
        newer.sort_by_key(|l| l.layer.parse::<crate::layer::LayerId>().ok());
        let unparseable: Vec<&str> = self
            .layers
            .iter()
            .filter(|l| l.layer.parse::<crate::layer::LayerId>().is_err())
            .map(|l| l.layer.as_str())
            .collect();
        (newer, unparseable)
    }

    pub fn high_water(&self) -> Option<u64> {
        self.layers.iter().map(|e| e.counter).max()
    }

    /// Clause 2: refuse a presented index older than one already held.
    /// Equality is allowed — a CI re-run must stay idempotent, the same rule
    /// `attach_envelope_to_layout` follows for line-status.
    pub fn refuse_regression(&self, cached: Option<&LineIndex>) -> Result<(), IndexError> {
        if let Some(prev) = cached
            && prev.line == self.line
            && self.counter < prev.counter
        {
            return Err(IndexError::Stale {
                line: self.line.clone(),
                presented: self.counter,
                cached: prev.counter,
            });
        }
        Ok(())
    }

    /// The index as a lookup from layer id to its signed payload digest.
    pub fn by_layer(&self) -> BTreeMap<&str, &str> {
        self.layers
            .iter()
            .map(|e| (e.layer.as_str(), e.digest.as_str()))
            .collect()
    }
}

/// What a consumer knows about a realm's index obligation.
#[derive(Debug, Clone, Copy)]
pub struct IndexPolicy<'a> {
    /// The realm's name, for error messages.
    pub realm: &'a str,
    /// The realm's trust — the index verifies against this and nothing else,
    /// under the STATUS role where the realm declares roles. The SOURCE is the
    /// party being constrained, so it is never asked whether the index is good.
    pub trust: &'a crate::keyroles::RealmTrust,
    /// The realm declared `signed-index = true` (clause 5).
    pub required: bool,
}

/// The index in `envelope` if it VERIFIES under the realm's trust and is about
/// `line`; `None` otherwise (the failure is `check`'s to report, this only asks
/// "what has this source proved?").
///
/// For a caller that must remember what an earlier source proved while a later
/// one is asked (varve#311): a verified document is a baseline whether or not
/// the rest of that source's attempt succeeded.
pub fn proven_by(
    line: &str,
    envelope: &[u8],
    trust: &crate::keyroles::RealmTrust,
) -> Option<LineIndex> {
    LineIndex::verify_and_parse(envelope, trust)
        .ok()
        .filter(|doc| doc.line == line)
}

/// The index with the higher counter; `a` on a tie. The baseline a later source
/// is judged against is the higher of what is cached and what this run proved.
pub fn higher_counter(a: Option<LineIndex>, b: Option<LineIndex>) -> Option<LineIndex> {
    match (a, b) {
        (Some(a), Some(b)) => Some(if b.counter > a.counter { b } else { a }),
        (a, b) => a.or(b),
    }
}

/// Run the whole index check for one line, returning the verified index when
/// there is one. Separate from `install` so each clause is exercised directly:
/// integration tests cannot kill mutants under `--workspace --lib`, and this
/// is trust-critical code.
///
/// `envelope` is what the source offered (None = it offered nothing);
/// `served` is what the source is willing to serve (None = it cannot
/// enumerate); `cached` is the index already held, if any.
pub fn check(
    line: &str,
    envelope: Option<&[u8]>,
    served: Option<&[String]>,
    cached: Option<&LineIndex>,
    policy: &IndexPolicy<'_>,
) -> Result<Option<LineIndex>, IndexError> {
    let Some(bytes) = envelope else {
        // Clause 5: absence is an error only where the realm promised one.
        if policy.required {
            return Err(IndexError::Missing {
                realm: policy.realm.to_string(),
                line: line.to_string(),
            });
        }
        return Ok(None);
    };

    let index = LineIndex::verify_and_parse(bytes, policy.trust)?;
    // The document must be about the line we asked about. Without this, a
    // valid index for a QUIET line would satisfy the check for a busy one
    // while naming none of its layers — omission detection that always passes.
    if index.line != line {
        return Err(IndexError::WrongLine {
            document: index.line.clone(),
            expected: line.to_string(),
        });
    }
    index.refuse_regression(cached)?;
    if let Some(served) = served {
        index.refuse_omission(served)?;
    }
    Ok(Some(index))
}

// ─────────────────────── carriage: how an index travels ───────────────────
//
// A line-status rides INSIDE a layer's artifact manifest, because it is
// evidence about the layer being fetched. An index is about the LINE and must
// be obtainable before any layer is chosen — including when the layer a
// consumer wants is the one being hidden — so it gets its own address:
// one tag per line on a registry, one referrer entry per line in a layout.
// Reading it through a layer would let a source suppress the index by
// suppressing the layer, which is the attack.

/// Annotation naming the line a carried index document covers. Distinct from
/// `linestatus::ANN_LINE` on purpose: a layout carries both documents about
/// the same line, and two readers keyed on one annotation name is the sort of
/// near-miss that ends with a status being read as an index.
pub const ANN_INDEX_LINE: &str = "eu.pulseengine.varve.index-line";

/// Tag prefix under which a line's signed index is published on a registry.
/// Deliberately not parseable as a `LayerId` (`YYYY.MM.P`), so an index tag
/// can never be mistaken for a layer the line contains — including by
/// `served_layers`, which would otherwise report the index itself as a layer.
pub const LINE_INDEX_TAG_PREFIX: &str = "line-index-";

/// The registry tag carrying the signed index for a line.
pub fn index_tag(line: &str) -> String {
    format!("{LINE_INDEX_TAG_PREFIX}{line}")
}

/// Attach a signed index envelope to an OCI image layout as a referrer,
/// replacing any previous index for the same line. No layer blob or digest is
/// touched: evidence is added beside the artifact, never folded into it.
pub fn attach_to_layout(layout: &Path, line: &str, envelope: &[u8]) -> Result<(), IndexError> {
    let io = |path: &Path, source: std::io::Error| IndexError::Io {
        path: path.display().to_string(),
        source,
    };
    let digest = crate::store::manifest_digest(envelope);
    let hex = digest.strip_prefix("sha256:").expect("digest shape");
    let blob_dir = layout.join("blobs").join("sha256");
    std::fs::create_dir_all(&blob_dir).map_err(|e| io(&blob_dir, e))?;
    let blob_path = blob_dir.join(hex);
    std::fs::write(&blob_path, envelope).map_err(|e| io(&blob_path, e))?;

    let index_path = layout.join("index.json");
    let mut index: serde_json::Value =
        serde_json::from_slice(&std::fs::read(&index_path).map_err(|e| io(&index_path, e))?)
            .map_err(|e| IndexError::Payload(format!("index.json: {e}")))?;
    let entries = index["manifests"]
        .as_array_mut()
        .ok_or_else(|| IndexError::Payload("index.json has no manifests array".into()))?;
    entries.retain(|e| {
        !(e["artifactType"] == LINE_INDEX_ARTIFACT_TYPE
            && e["annotations"][ANN_INDEX_LINE] == *line)
    });
    entries.push(serde_json::json!({
        "mediaType": "application/json",
        "artifactType": LINE_INDEX_ARTIFACT_TYPE,
        "digest": digest,
        "size": envelope.len(),
        "annotations": { ANN_INDEX_LINE: line }
    }));
    std::fs::write(
        &index_path,
        serde_json::to_vec_pretty(&index).expect("index serializes"),
    )
    .map_err(|e| io(&index_path, e))?;
    Ok(())
}

/// Attach an index envelope to a layout, deriving the line from the document
/// itself (the producing side, `varve attach-index`). Returns (line, counter).
///
/// The payload is read to learn the line, not trusted: the consumer re-verifies
/// against its realm's root. Two guards mirror `attach-status`, because a
/// producer mistake here is a consumer outage later:
///  * a counter regression is refused, so a re-run of a CI step cannot
///    downgrade a published layout's index — and so the same rule holds at the
///    only place that PRODUCES the artifact, not merely where it is read;
///  * an index for a different line than the layout's own layer is refused,
///    rather than being left for the consumer to discover as `WrongLine`.
pub fn attach_envelope_to_layout(
    layout: &Path,
    envelope: &[u8],
) -> Result<(String, u64), IndexError> {
    let doc = parse_unverified(envelope)?;
    let line: Line = doc.line.parse().map_err(|e: crate::layer::LayerIdError| {
        IndexError::Payload(format!("index line '{}': {e}", doc.line))
    })?;
    let line = line.to_string();
    if let Some(existing) = read_from_layout(layout, &line)? {
        let prev = parse_unverified(&existing)?;
        doc.refuse_regression(Some(&prev))?;
    }
    if let Some(layout_line) = crate::linestatus::layout_line(layout)
        && layout_line != line
    {
        return Err(IndexError::WrongLine {
            document: line,
            expected: layout_line,
        });
    }
    attach_to_layout(layout, &line, envelope)?;
    Ok((line, doc.counter))
}

/// Read the index envelope a layout carries for a line, if any.
pub fn read_from_layout(layout: &Path, line: &str) -> Result<Option<Vec<u8>>, IndexError> {
    let index_path = layout.join("index.json");
    let bytes = match std::fs::read(&index_path) {
        Ok(bytes) => bytes,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(source) => {
            return Err(IndexError::Io {
                path: index_path.display().to_string(),
                source,
            });
        }
    };
    let index: serde_json::Value = serde_json::from_slice(&bytes)
        .map_err(|e| IndexError::Payload(format!("index.json: {e}")))?;
    // Both artifactType AND line must match. Matching on the type alone would
    // hand a consumer another line's index, which `check` would then reject as
    // `WrongLine` — a correct refusal with a misleading cause.
    let Some(entry) = index["manifests"].as_array().and_then(|entries| {
        entries.iter().find(|e| {
            e["artifactType"] == LINE_INDEX_ARTIFACT_TYPE
                && e["annotations"][ANN_INDEX_LINE] == *line
        })
    }) else {
        return Ok(None);
    };
    let digest = entry["digest"]
        .as_str()
        .ok_or_else(|| IndexError::Payload("index entry has no digest".into()))?;
    let hex = digest.strip_prefix("sha256:").unwrap_or(digest);
    let blob_path = layout.join("blobs").join("sha256").join(hex);
    std::fs::read(&blob_path)
        .map(Some)
        .map_err(|source| IndexError::Io {
            path: blob_path.display().to_string(),
            source,
        })
}

/// Parse an index out of an envelope WITHOUT verifying it. Used only to read
/// back a document this machine already accepted (the cache) or is about to
/// publish (attach); every place the document is TRUSTED goes through
/// `verify_and_parse`.
pub(crate) fn parse_unverified(envelope: &[u8]) -> Result<LineIndex, IndexError> {
    let text = std::str::from_utf8(envelope)
        .map_err(|e| IndexError::Payload(format!("envelope is not utf-8: {e}")))?;
    let env = wsc::dsse::DsseEnvelope::from_json(text)
        .map_err(|e| IndexError::Payload(format!("not a DSSE envelope: {e}")))?;
    let payload = env
        .payload_bytes()
        .map_err(|e| IndexError::Payload(format!("envelope payload: {e}")))?;
    serde_json::from_slice(&payload)
        .map_err(|e| IndexError::Payload(format!("index document: {e}")))
}

/// Per-line cache of the newest index this machine has accepted — what clause
/// 2 compares a presented index against.
///
/// Local, unsigned state, exactly like `HighWaterMarks`: it is written only
/// after an index has verified against the realm's root, and it is read back
/// unverified. The failure it can produce is REFUSING an install, never
/// accepting a bad one — an attacker who can already write here can delete the
/// core instead.
#[derive(Debug)]
pub struct IndexCache {
    dir: PathBuf,
}

impl IndexCache {
    pub fn at_root(root: &Path) -> Self {
        IndexCache {
            dir: root.join("state").join("line-index"),
        }
    }

    /// The index cached for a line, if one has ever been accepted.
    pub fn load(&self, line: &str) -> Result<Option<LineIndex>, IndexError> {
        let path = self.path(line);
        match std::fs::read(&path) {
            Ok(bytes) => Ok(Some(parse_unverified(&bytes)?)),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(source) => Err(IndexError::Io {
                path: path.display().to_string(),
                source,
            }),
        }
    }

    /// Record a VERIFIED envelope, refusing a counter regression — the same
    /// rule `check` applies, held here too so a caller that skipped the check
    /// cannot quietly lower the cached mark.
    pub fn update(
        &self,
        line: &str,
        envelope: &[u8],
        parsed: &LineIndex,
    ) -> Result<(), IndexError> {
        parsed.refuse_regression(self.load(line)?.as_ref())?;
        let io = |path: &Path, source: std::io::Error| IndexError::Io {
            path: path.display().to_string(),
            source,
        };
        std::fs::create_dir_all(&self.dir).map_err(|e| io(&self.dir, e))?;
        let path = self.path(line);
        std::fs::write(&path, envelope).map_err(|e| io(&path, e))?;
        Ok(())
    }

    fn path(&self, line: &str) -> PathBuf {
        self.dir.join(format!("{line}.dsse.json"))
    }
}

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

    fn index(counter: u64, layers: &[(&str, &str)]) -> LineIndex {
        // Layer counters ascend with the layer id, as a real line's do.
        LineIndex {
            line: "2026.08".into(),
            counter,
            issued_at: "2026-08-18T00:00:00Z".into(),
            layers: layers
                .iter()
                .enumerate()
                .map(|(i, (l, d))| IndexedLayer {
                    layer: (*l).into(),
                    digest: (*d).into(),
                    channel: "qualified".into(),
                    counter: (i as u64) + 1,
                })
                .collect(),
        }
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn an_index_verifies_only_against_the_realm_that_signed_it() {
        let (sk, pk) = generate_root_keypair();
        let (_other_sk, other_pk) = generate_root_keypair();
        let doc = index(1, &[("2026.08.0", "sha256:aa"), ("2026.08.1", "sha256:bb")]);
        let envelope = doc.sign(&sk, "root-1").unwrap();

        assert_eq!(
            LineIndex::verify_and_parse(
                envelope.as_bytes(),
                &crate::keyroles::test_support::rt(&pk)
            )
            .unwrap(),
            doc
        );
        // Another realm's root must not accept it — an index is an assertion
        // BY a realm, so it carries that realm's authority and no other's.
        assert!(
            LineIndex::verify_and_parse(
                envelope.as_bytes(),
                &crate::keyroles::test_support::rt(&other_pk)
            )
            .is_err()
        );
    }

    // rivet: verifies REQ-MIRROR-002
    #[test]
    fn proven_by_answers_only_for_a_verified_index_about_the_asked_line() {
        let (sk, pk) = generate_root_keypair();
        let (_other_sk, other_pk) = generate_root_keypair();
        let doc = index(4, &[("2026.08.0", "sha256:aa")]);
        let envelope = doc.sign(&sk, "root-1").unwrap();
        let trust = crate::keyroles::test_support::rt(&pk);
        assert_eq!(
            proven_by("2026.08", envelope.as_bytes(), &trust),
            Some(doc.clone())
        );
        // A different line, a different realm's root, and bytes that are not
        // an envelope prove nothing.
        assert_eq!(proven_by("2026.09", envelope.as_bytes(), &trust), None);
        let other = crate::keyroles::test_support::rt(&other_pk);
        assert_eq!(proven_by("2026.08", envelope.as_bytes(), &other), None);
        assert_eq!(proven_by("2026.08", b"not an envelope", &trust), None);
    }

    // rivet: verifies REQ-MIRROR-002
    #[test]
    fn the_baseline_is_the_higher_counter_and_a_tie_keeps_the_first() {
        let lo = index(2, &[("2026.08.0", "sha256:aa")]);
        let hi = index(5, &[("2026.08.0", "sha256:bb")]);
        assert_eq!(
            higher_counter(Some(lo.clone()), Some(hi.clone())),
            Some(hi.clone())
        );
        assert_eq!(
            higher_counter(Some(hi.clone()), Some(lo.clone())),
            Some(hi.clone())
        );
        assert_eq!(higher_counter(Some(lo.clone()), None), Some(lo.clone()));
        assert_eq!(higher_counter(None, Some(lo.clone())), Some(lo.clone()));
        assert_eq!(higher_counter(None, None), None);
        let same = index(5, &[("2026.08.0", "sha256:cc")]);
        assert_eq!(higher_counter(Some(hi.clone()), Some(same)), Some(hi));
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn a_signed_line_status_cannot_be_replayed_as_an_index() {
        // The payload type is the whole defence against cross-document replay.
        // Both documents are signed by the SAME root and both are about a
        // line, so without a distinct type a status could stand in for an
        // index and assert that a line contains nothing.
        let (sk, pk) = generate_root_keypair();
        let status = crate::linestatus::LineStatus {
            min_counter: None,
            line: "2026.08".into(),
            counter: 9,
            issued_at: "2026-08-18T00:00:00Z".into(),
            support_until: None,
            yanked: Default::default(),
            known_problems: Vec::new(),
        };
        let envelope = status.sign(&sk, "root-1").unwrap();
        // Assert the TYPE rejected it, not the schema. Mutating the payload
        // type constant to line-status's left an `is_err()` version of this
        // test GREEN, because the two structs have divergent
        // deny_unknown_fields shapes and serde refused it anyway — the test
        // proved schema divergence while claiming to prove the type check. If
        // the schemas ever converged, replay would open and nothing would
        // notice.
        match LineIndex::verify_and_parse(
            envelope.as_bytes(),
            &crate::keyroles::test_support::rt(&pk),
        ) {
            Err(IndexError::Verify(_)) => {}
            Err(IndexError::Payload(p)) => panic!(
                "rejected by the SCHEMA ({p}), not by the payload type — the type is the \
                 defence against cross-document replay and must be what fails"
            ),
            Ok(_) => panic!("a line-status must not verify as a line-index"),
            Err(other) => panic!("expected a payload-type rejection, got {other}"),
        }

        // The converse, so the defence is shown to be symmetric: an index
        // must not be accepted as a status either.
        let idx = index(1, &[("2026.08.0", "sha256:aa")]);
        let idx_env = idx.sign(&sk, "root-1").unwrap();
        assert!(
            crate::linestatus::LineStatus::verify_and_parse(
                idx_env.as_bytes(),
                &crate::keyroles::test_support::rt(&pk)
            )
            .is_err(),
            "a line-index must not verify as a line-status"
        );
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn a_source_that_hides_a_layer_the_index_names_is_refused() {
        // THE attack this requirement exists for. The registry serves a valid,
        // correctly-signed 2026.08.0 and simply omits 2026.08.1 — a freshness
        // attack in which every byte the consumer receives verifies.
        let doc = index(1, &[("2026.08.0", "sha256:aa"), ("2026.08.1", "sha256:bb")]);
        let err = doc
            .refuse_omission(&["2026.08.0".to_string()])
            .expect_err("hiding a layer must be refused");
        match &err {
            IndexError::Omitted { layer, digest, .. } => {
                assert_eq!(layer, "2026.08.1");
                assert_eq!(digest, "sha256:bb", "name the digest, so it can be sought");
            }
            other => panic!("expected Omitted, got {other}"),
        }
        // The message must be actionable, not merely correct.
        let msg = err.to_string();
        assert!(msg.contains("2026.08.1"), "names the hidden layer: {msg}");
        assert!(
            msg.contains("still verifies"),
            "says WHY per-artifact verification did not catch this: {msg}"
        );

        // A source serving everything the index names is accepted, and extra
        // layers are not an error: an index is a floor, not a whitelist, so a
        // consumer holding an older index still works against a newer source.
        assert!(
            doc.refuse_omission(&[
                "2026.08.0".to_string(),
                "2026.08.1".to_string(),
                "2026.08.2".to_string(),
            ])
            .is_ok()
        );
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn the_high_water_mark_comes_from_the_index_not_from_what_was_served() {
        // Clause 4, and the Uptane property: a registry hiding the newest
        // layer must not lower the bar a later install has to clear.
        let doc = index(
            1,
            &[
                ("2026.08.0", "sha256:aa"),
                ("2026.08.2", "sha256:cc"),
                ("2026.08.1", "sha256:bb"),
            ],
        );
        // The mark is a COUNTER — the units HighWaterMarks actually stores.
        // An earlier draft returned the greatest LayerId here: it type-checked
        // and could not feed the mechanism clause 4 exists to protect.
        assert_eq!(
            doc.high_water(),
            Some(3),
            "the greatest counter the REALM asserts, regardless of the order \
             entries appear in the document or of what any source served"
        );
        // An empty index asserts nothing and must not be read as a mark of 0,
        // which would be a mark that every layer clears.
        assert_eq!(index(1, &[]).high_water(), None);
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn a_stale_index_cannot_replace_a_newer_one() {
        let newer = index(7, &[("2026.08.1", "sha256:bb")]);
        let older = index(3, &[("2026.08.0", "sha256:aa")]);

        let err = older
            .refuse_regression(Some(&newer))
            .expect_err("a lower counter must be refused");
        assert!(matches!(
            err,
            IndexError::Stale {
                presented: 3,
                cached: 7,
                ..
            }
        ));
        let msg = err.to_string();
        assert!(msg.contains('3') && msg.contains('7'), "names both: {msg}");

        // Equal is not a regression — CI re-runs must stay idempotent, the
        // same rule line-status follows.
        assert!(newer.refuse_regression(Some(&newer)).is_ok());
        // Newer over older is the ordinary case.
        assert!(newer.refuse_regression(Some(&older)).is_ok());
        // Nothing cached yet is not a regression either.
        assert!(older.refuse_regression(None).is_ok());
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn an_index_for_another_line_is_not_silently_accepted() {
        let doc = index(1, &[("2026.08.0", "sha256:aa")]);
        let other = LineIndex {
            line: "2026.09".into(),
            ..index(1, &[("2026.09.0", "sha256:zz")])
        };
        // A different line is not a regression — the counters are per line, so
        // comparing them would refuse a legitimate document.
        assert!(doc.refuse_regression(Some(&other)).is_ok());
        assert_eq!(doc.line().unwrap().to_string(), "2026.08");
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn a_realm_that_promised_an_index_does_not_fall_back_to_an_unsigned_listing() {
        // Clause 5, both directions. If absence were tolerated for a
        // declaring realm, an attacker would disable the entire check by
        // deleting one file — the check would be advisory, not a control.
        let (_sk, pk) = generate_root_keypair();
        let declaring = IndexPolicy {
            realm: "acme",
            trust: crate::keyroles::test_support::leak_rt(&pk),
            required: true,
        };
        let silent = IndexPolicy {
            required: false,
            ..declaring
        };

        let err = check("2026.08", None, None, None, &declaring)
            .expect_err("a declaring realm must not accept a missing index");
        assert!(matches!(err, IndexError::Missing { .. }));
        let msg = err.to_string();
        assert!(msg.contains("acme"), "names the realm: {msg}");
        assert!(
            msg.contains("will not fall back"),
            "says what it refused to do, not merely that something is absent: {msg}"
        );

        // A realm that never promised one keeps working — the default must not
        // break every realm in existence.
        assert!(
            check("2026.08", None, None, None, &silent)
                .unwrap()
                .is_none()
        );
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn an_index_for_a_different_line_cannot_satisfy_this_line() {
        // Without this, a valid index for a QUIET line satisfies the check for
        // a busy one while naming none of its layers — omission detection that
        // structurally always passes, which is worse than no check because it
        // reports success.
        let (sk, pk) = generate_root_keypair();
        let policy = IndexPolicy {
            realm: "acme",
            trust: crate::keyroles::test_support::leak_rt(&pk),
            required: true,
        };
        let quiet = LineIndex {
            line: "2026.01".into(),
            ..index(1, &[])
        };
        let envelope = quiet.sign(&sk, "k").unwrap();
        let err = check(
            "2026.08",
            Some(envelope.as_bytes()),
            Some(&["2026.08.0".to_string()]),
            None,
            &policy,
        )
        .expect_err("an index for another line must not satisfy this one");
        assert!(matches!(
            err,
            IndexError::WrongLine { ref document, ref expected }
                if document == "2026.01" && expected == "2026.08"
        ));
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn a_source_that_cannot_enumerate_is_not_treated_as_hiding_everything() {
        // `None` (cannot enumerate — an offline archive directory) must be
        // distinct from `Some(vec![])` (enumerates, has nothing). Collapsing
        // them would make every air-gapped install fail with a false
        // accusation of tampering, which is how a security control gets turned
        // off in the field.
        let (sk, pk) = generate_root_keypair();
        let policy = IndexPolicy {
            realm: "acme",
            trust: crate::keyroles::test_support::leak_rt(&pk),
            required: true,
        };
        let doc = index(1, &[("2026.08.0", "sha256:aa")]);
        let envelope = doc.sign(&sk, "k").unwrap();

        let ok = check("2026.08", Some(envelope.as_bytes()), None, None, &policy)
            .expect("a source that cannot enumerate is not evidence of hiding");
        assert_eq!(ok.unwrap().counter, 1);

        // …but one that CAN enumerate and serves nothing is hiding everything.
        assert!(matches!(
            check(
                "2026.08",
                Some(envelope.as_bytes()),
                Some(&[]),
                None,
                &policy
            ),
            Err(IndexError::Omitted { .. })
        ));
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn check_refuses_a_stale_index_not_only_refuse_regression_does() {
        // `a_stale_index_cannot_replace_a_newer_one` calls refuse_regression
        // DIRECTLY, so deleting the call from `check` — the function install
        // actually goes through — left the suite green. That is the exact
        // shape four consecutive reviews have found: evidence that exercises a
        // different function than the clause runs through. This one goes
        // through `check`.
        let (sk, pk) = generate_root_keypair();
        let policy = IndexPolicy {
            realm: "acme",
            trust: crate::keyroles::test_support::leak_rt(&pk),
            required: true,
        };
        let cached = index(7, &[("2026.08.1", "sha256:bb")]);
        let stale = index(3, &[("2026.08.0", "sha256:aa")]);
        let envelope = stale.sign(&sk, "k").unwrap();

        let err = check(
            "2026.08",
            Some(envelope.as_bytes()),
            None,
            Some(&cached),
            &policy,
        )
        .expect_err("a replayed older index must be refused by the path install uses");
        assert!(matches!(
            err,
            IndexError::Stale {
                presented: 3,
                cached: 7,
                ..
            }
        ));

        // The newer one is accepted through the same path.
        let fresher = index(8, &[("2026.08.1", "sha256:bb")]);
        let ok = fresher.sign(&sk, "k").unwrap();
        assert_eq!(
            check("2026.08", Some(ok.as_bytes()), None, Some(&cached), &policy)
                .unwrap()
                .unwrap()
                .counter,
            8
        );
    }

    /// A layout of one signed layer, the shape `deposit` writes.
    fn layout_for(layer: &str, sk: &[u8]) -> (tempfile::TempDir, std::path::PathBuf) {
        let tmp = tempfile::tempdir().unwrap();
        let dest = tmp.path().join("layout");
        let payload = crate::manifest::fixtures::manifest_with_tools(
            layer,
            "qualified",
            1,
            "2026-08-18T00:00:00Z",
            &[],
        );
        let envelope = crate::verify::sign_layer_manifest(&payload, sk, "root-1").unwrap();
        crate::archive::write_oci_layout(
            &payload,
            envelope.as_bytes(),
            &[],
            layer,
            "qualified",
            // A test fixture layout, not a real archive: no platform stamp.
            None,
            &dest,
            false,
        )
        .unwrap();
        (tmp, dest)
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn an_index_attached_to_a_layout_is_what_an_offline_install_reads_back() {
        // Clause 1's transport half for the air-gapped path. Without it the
        // document exists, verifies, and reaches nobody who is not on a
        // registry — which is the transport varve exists to serve.
        use crate::source::LayerSource;
        let (sk, _pk) = generate_root_keypair();
        let (_tmp, layout) = layout_for("2026.08.0", &sk);
        let envelope = index(4, &[("2026.08.0", "sha256:aa")])
            .sign(&sk, "root-1")
            .unwrap();
        attach_to_layout(&layout, "2026.08", envelope.as_bytes()).unwrap();

        let source = crate::archive::OciLayoutSource::at(&layout);
        assert_eq!(
            source.fetch_line_index("2026.08").unwrap().as_deref(),
            Some(envelope.as_bytes()),
            "the layout source must hand back the attached index verbatim"
        );
        // Another line's index is not served for this one: a reader keyed on
        // the artifact type alone would produce a `WrongLine` refusal whose
        // stated cause is wrong.
        assert_eq!(source.fetch_line_index("2026.09").unwrap(), None);
        // A layout with no index at all is `None`, not an error — whether that
        // is tolerable is the realm's call, not the layout's.
        let (_t2, bare) = layout_for("2026.08.0", &sk);
        assert_eq!(
            crate::archive::OciLayoutSource::at(&bare)
                .fetch_line_index("2026.08")
                .unwrap(),
            None
        );

        // A layout carries BOTH documents about one line. Attaching a
        // line-status must not disturb the index, and the index reader must
        // not pick up the status: they are separate artifact types and are
        // kept apart by type, not by luck of ordering.
        let status = crate::linestatus::LineStatus {
            min_counter: None,
            line: "2026.08".into(),
            counter: 1,
            issued_at: "2026-08-18T00:00:00Z".into(),
            support_until: None,
            yanked: Default::default(),
            known_problems: Vec::new(),
        }
        .sign(&sk, "root-1")
        .unwrap();
        crate::linestatus::attach_to_layout(
            &layout,
            &"2026.08".parse().unwrap(),
            status.as_bytes(),
        )
        .unwrap();
        assert_eq!(
            source.fetch_line_index("2026.08").unwrap().as_deref(),
            Some(envelope.as_bytes()),
            "attaching a status must not displace the index"
        );
        assert_eq!(
            crate::linestatus::read_from_layout(&layout, &"2026.08".parse().unwrap())
                .unwrap()
                .as_deref(),
            Some(status.as_bytes()),
            "…and the index must not be handed back as the status either"
        );

        // Re-attaching replaces rather than accumulating: two indexes for one
        // line in a layout is a document whose meaning depends on read order.
        let newer = index(5, &[("2026.08.0", "sha256:aa")])
            .sign(&sk, "root-1")
            .unwrap();
        attach_to_layout(&layout, "2026.08", newer.as_bytes()).unwrap();
        let json: serde_json::Value =
            serde_json::from_slice(&std::fs::read(layout.join("index.json")).unwrap()).unwrap();
        assert_eq!(
            json["manifests"]
                .as_array()
                .unwrap()
                .iter()
                .filter(|e| e["artifactType"] == LINE_INDEX_ARTIFACT_TYPE)
                .count(),
            1,
            "one index per line, replaced in place"
        );
        assert_eq!(
            source.fetch_line_index("2026.08").unwrap().as_deref(),
            Some(newer.as_bytes())
        );
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn a_producer_cannot_downgrade_or_misfile_a_published_index() {
        // Clause 2 at the PRODUCING end. `attach-status` learned this the hard
        // way: the one place that creates the artifact was the one place the
        // monotonicity rule was missing, so a re-run of a CI step silently
        // shipped a superseded document that fresh consumers then cached.
        let (sk, _pk) = generate_root_keypair();
        let (_tmp, layout) = layout_for("2026.08.0", &sk);
        let newer = index(7, &[("2026.08.0", "sha256:aa")])
            .sign(&sk, "root-1")
            .unwrap();
        let (line, counter) = attach_envelope_to_layout(&layout, newer.as_bytes()).unwrap();
        assert_eq!((line.as_str(), counter), ("2026.08", 7));

        let older = index(3, &[("2026.08.0", "sha256:aa")])
            .sign(&sk, "root-1")
            .unwrap();
        let err = attach_envelope_to_layout(&layout, older.as_bytes())
            .expect_err("a producer must not publish an index older than the layout's");
        assert!(
            matches!(
                err,
                IndexError::Stale {
                    presented: 3,
                    cached: 7,
                    ..
                }
            ),
            "got: {err}"
        );
        // The newer one is still what the layout serves.
        assert_eq!(
            read_from_layout(&layout, "2026.08").unwrap().as_deref(),
            Some(newer.as_bytes())
        );

        // An index for another line than the layout's own layer is refused
        // here rather than left for the consumer to hit as `WrongLine`.
        let foreign = LineIndex {
            line: "2099.01".into(),
            ..index(1, &[])
        }
        .sign(&sk, "root-1")
        .unwrap();
        let err = attach_envelope_to_layout(&layout, foreign.as_bytes())
            .expect_err("a 2099.01 index does not belong on a 2026.08 layout");
        assert!(
            matches!(err, IndexError::WrongLine { ref document, ref expected }
                if document == "2099.01" && expected == "2026.08"),
            "got: {err}"
        );
        // …and a document whose `line` is not a line at all never gets signed
        // onto a layout: it would verify forever and match nothing.
        let nonsense = LineIndex {
            line: "twenty-twenty-six".into(),
            ..index(1, &[])
        }
        .sign(&sk, "root-1")
        .unwrap();
        assert!(attach_envelope_to_layout(&layout, nonsense.as_bytes()).is_err());
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn the_cache_is_what_gives_clause_two_something_to_compare_against() {
        // `refuse_regression` needs a HELD document, and nothing held one:
        // every test supplied the "cached" index by hand, so the rule could
        // never fire on a real machine. This is that store.
        let tmp = tempfile::tempdir().unwrap();
        let cache = IndexCache::at_root(tmp.path());
        assert_eq!(
            cache.load("2026.08").unwrap(),
            None,
            "nothing accepted yet is None, not an empty index — an empty index \
             asserts that the line contains nothing"
        );

        let (sk, _pk) = generate_root_keypair();
        let seven = index(7, &[("2026.08.1", "sha256:bb")]);
        cache
            .update("2026.08", seven.sign(&sk, "k").unwrap().as_bytes(), &seven)
            .unwrap();
        assert_eq!(cache.load("2026.08").unwrap().unwrap().counter, 7);

        // A replayed older document cannot lower the mark, even by a caller
        // that skipped `check`.
        let three = index(3, &[("2026.08.0", "sha256:aa")]);
        let err = cache
            .update("2026.08", three.sign(&sk, "k").unwrap().as_bytes(), &three)
            .expect_err("the cache must not accept a regression");
        assert!(matches!(err, IndexError::Stale { .. }), "got: {err}");
        assert_eq!(cache.load("2026.08").unwrap().unwrap().counter, 7);

        // Equal is idempotent; newer advances; another line is independent,
        // because the counters are per line.
        cache
            .update("2026.08", seven.sign(&sk, "k").unwrap().as_bytes(), &seven)
            .unwrap();
        let eight = index(8, &[("2026.08.1", "sha256:bb")]);
        cache
            .update("2026.08", eight.sign(&sk, "k").unwrap().as_bytes(), &eight)
            .unwrap();
        assert_eq!(cache.load("2026.08").unwrap().unwrap().counter, 8);
        let other = LineIndex {
            line: "2026.09".into(),
            ..index(1, &[])
        };
        cache
            .update("2026.09", other.sign(&sk, "k").unwrap().as_bytes(), &other)
            .expect("a low counter on a DIFFERENT line is not a regression");
        assert_eq!(cache.load("2026.08").unwrap().unwrap().counter, 8);
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn the_index_tag_cannot_be_mistaken_for_a_layer_of_the_line() {
        // `served_layers` filters a registry's tags by parsing them as layer
        // ids. If the tag carrying the index parsed as one, the index would
        // appear in the line's own listing — and, worse, an index naming a tag
        // shaped like its own would be self-satisfying.
        assert_eq!(index_tag("2026.08"), "line-index-2026.08");
        assert!(
            index_tag("2026.08")
                .parse::<crate::layer::LayerId>()
                .is_err()
        );
        assert!(index_tag("2026.08").starts_with(LINE_INDEX_TAG_PREFIX));
    }

    // rivet: verifies REQ-INDEXAUTH-001
    #[test]
    fn the_source_never_gets_to_vouch_for_its_own_index() {
        // The source is the party this document exists to constrain, so an
        // index it signs with its own key must be worthless.
        let (_realm_sk, realm_pk) = generate_root_keypair();
        let (impostor_sk, _impostor_pk) = generate_root_keypair();
        let policy = IndexPolicy {
            realm: "acme",
            trust: crate::keyroles::test_support::leak_rt(&realm_pk),
            required: true,
        };
        let forged = index(99, &[("2026.08.0", "sha256:aa")])
            .sign(&impostor_sk, "not-the-realm")
            .unwrap();
        assert!(matches!(
            check("2026.08", Some(forged.as_bytes()), None, None, &policy),
            Err(IndexError::Verify(_))
        ));
    }
}

#[cfg(test)]
mod newer_than_tests {
    use super::{IndexedLayer, LineIndex};
    use crate::layer::LayerId;
    fn pinned_index(layers: &[(&str, &str)]) -> LineIndex {
        LineIndex {
            line: "2026.09".into(),
            counter: 7,
            issued_at: "2026-10-01T00:00:00Z".into(),
            layers: layers
                .iter()
                .map(|(layer, channel)| IndexedLayer {
                    layer: (*layer).to_string(),
                    digest: format!("sha256:{layer}"),
                    channel: (*channel).to_string(),
                    counter: 1,
                })
                .collect(),
        }
    }

    /// The ordering is NUMERIC, and this is the case that proves it.
    ///
    /// `2026.09.2` is OLDER than the pinned `2026.09.12`, and a string compare
    /// says the opposite because '1' < '2'. I made exactly this mistake on
    /// 2026-10-01 while measuring how stale a real realm was and reported a
    /// figure wrong by eight layers. If this test ever passes with a
    /// lexicographic comparison, it is measuring nothing.
    // rivet: verifies REQ-LAYERDIFF-001
    #[test]
    fn newer_means_numerically_newer_not_alphabetically() {
        let pinned: LayerId = "2026.09.12".parse().unwrap();
        let idx = pinned_index(&[
            ("2026.09.2", "rolling"),
            ("2026.09.9", "rolling"),
            ("2026.09.12", "rolling"),
            ("2026.09.13", "rolling"),
            ("2026.10.1", "rolling"),
        ]);
        let (newer, unparseable) = idx.newer_than(&pinned);
        let got: Vec<&str> = newer.iter().map(|l| l.layer.as_str()).collect();
        assert_eq!(
            got,
            vec!["2026.09.13", "2026.10.1"],
            "2026.09.2 and 2026.09.9 are OLDER than 2026.09.12; a string \
             compare would have reported them as newer"
        );
        assert!(unparseable.is_empty());
    }

    /// The pin being the newest is a real answer, not an empty one.
    // rivet: verifies REQ-LAYERDIFF-001
    #[test]
    fn nothing_newer_is_reported_as_nothing_newer() {
        let pinned: LayerId = "2026.10.1".parse().unwrap();
        let idx = pinned_index(&[("2026.09.20", "rolling"), ("2026.10.1", "rolling")]);
        let (newer, unparseable) = idx.newer_than(&pinned);
        assert!(newer.is_empty(), "{newer:?}");
        assert!(unparseable.is_empty());
    }

    /// An entry this varve cannot order is RETURNED, not dropped.
    ///
    /// Dropping it would turn "I do not understand this" into "there is
    /// nothing newer", which is the same collapse of two different answers
    /// that makes a tag listing unusable for this question.
    // rivet: verifies REQ-LAYERDIFF-001
    #[test]
    fn an_entry_that_cannot_be_ordered_is_not_silently_dropped() {
        let pinned: LayerId = "2026.09.12".parse().unwrap();
        let idx = pinned_index(&[("2026.09.13", "rolling"), ("next", "rolling")]);
        let (newer, unparseable) = idx.newer_than(&pinned);
        assert_eq!(
            newer.iter().map(|l| l.layer.as_str()).collect::<Vec<_>>(),
            vec!["2026.09.13"]
        );
        assert_eq!(unparseable, vec!["next"], "the unorderable entry vanished");
    }
}

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

    // rivet: verifies REQ-MIRROR-002
    #[test]
    fn a_sources_bad_index_fails_over_but_a_verified_regression_or_local_io_does_not() {
        assert!(IndexError::Payload("x".into()).fails_over());
        assert!(
            IndexError::WrongLine {
                document: "a".into(),
                expected: "b".into()
            }
            .fails_over()
        );
        assert!(
            IndexError::Omitted {
                line: "l".into(),
                layer: "x".into(),
                digest: "d".into()
            }
            .fails_over()
        );
        assert!(
            IndexError::Missing {
                realm: "r".into(),
                line: "l".into()
            }
            .fails_over()
        );
        assert!(
            IndexError::Stale {
                line: "l".into(),
                presented: 1,
                cached: 2
            }
            .fails_over()
        );
        assert!(
            !IndexError::Io {
                path: "p".into(),
                source: std::io::Error::other("x")
            }
            .fails_over()
        );
    }
}