sail-cache 0.4.0

Tiered result cache: in-memory, local disk and shared, with integrity checks and eviction
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
//! The shared tier (charter rule 9, ADR 0017).
//!
//! A shared store is a directory of content-addressed, **signed** entry
//! files that teams and CI share (a local path, an NFS mount, a synced
//! folder). Nothing in it is trusted because of where it is: an entry is
//! used only if it carries a valid ed25519 signature from a key the reader
//! trusts, and the signature binds everything the reader relies on.
//!
//! **Layout.** `<store>/v2/<platform>/<solver binary sha256>/<k0k1>/<key>`,
//! so entries are scoped by solver binary and platform class (ADR 0010,
//! item 6). The path is only a lookup hint: every field it encodes is also
//! in the signed header and checked there.
//!
//! **Entry file.** One JSON header line, then the exact reply bytes, like
//! the local disk tier. The header holds the v2 key, the reply's SHA-256,
//! the command, the mode, the solver identity (name, version, binary
//! SHA-256, platform), the effort (ADR 0009), the format version, the
//! publication time, the signer's key id and the signature. The signature
//! covers a length-prefixed encoding of every one of those fields (see
//! [`Header::signed_bytes`]), never the JSON text, so no JSON
//! canonicalization is involved.
//!
//! **Keys.** [`SecretKey`] and [`PublicKey`] are stored as one line of
//! text: `sail-ed25519-secret-v1 <hex>` and `sail-ed25519-public-v1 <hex>`.
//! A key id is the first 16 hex digits of the SHA-256 of the public key.
//! Readers trust the public keys in `<cache root>/trust/*.pub`
//! ([`TrustedKeys::load_dir`]).

use std::collections::BTreeMap;
use std::io::{Read as _, Write as _};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::SystemTime;

use ed25519_dalek::{Signature, Signer as _, SigningKey, VerifyingKey};
use sail_key::{QueryKey, sha256};
use serde::{Deserialize, Serialize};

use crate::maint::FileKind;
use crate::{DiskCache, Effort, hex, shape_ok};

/// The `format` field of every shared entry header.
pub const FORMAT: &str = "sail-shared-entry";

/// Version of the shared entry format. It is signed, so an entry can never
/// be read under another version's rules.
pub const SHARED_VERSION: u32 = 2;

/// The only mode shared entries exist for today. Opt-in modes have keys of
/// their own (charter rule 8) and would need their own trust decision.
pub const MODE_DEFAULT: &str = "default";

/// Largest shared entry file read, in bytes. Larger files are rejected
/// without being read.
pub const MAX_ENTRY_BYTES: u64 = 64 * 1024 * 1024;

/// Domain separation for signatures.
const DOMAIN: &[u8] = b"sail-shared/entry/v1";

/// The store's top-level directory for this format.
const STORE_DIR: &str = "v2";

const SECRET_TAG: &str = "sail-ed25519-secret-v1";
const PUBLIC_TAG: &str = "sail-ed25519-public-v1";

/// Sequence number for temporary file names in a store.
static TMP_SEQ: AtomicU64 = AtomicU64::new(0);

fn unhex<const N: usize>(text: &str) -> Option<[u8; N]> {
    if text.len() != 2 * N {
        return None;
    }
    let mut out = [0u8; N];
    for (i, b) in out.iter_mut().enumerate() {
        let pair = text.get(2 * i..2 * i + 2)?;
        if !pair.bytes().all(|c| c.is_ascii_hexdigit()) {
            return None;
        }
        *b = u8::from_str_radix(pair, 16).ok()?;
    }
    Some(out)
}

/// The identity of the solver an entry was produced by: the fields of
/// `sail_key::SolverIdentity`, with the binary hash as hex.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Solver {
    /// Solver family, for example `z3`.
    pub name: String,
    /// The version the solver reports.
    pub version: String,
    /// SHA-256 of the solver binary, as lowercase hex.
    pub binary_sha256: String,
    /// Platform class, for example `macos-aarch64`.
    pub platform: String,
}

impl Solver {
    /// The summary the disk tier records in [`crate::Meta::solver`]:
    /// `name version sha256 platform`.
    #[must_use]
    pub fn summary(&self) -> String {
        format!(
            "{} {} {} {}",
            self.name, self.version, self.binary_sha256, self.platform
        )
    }

    /// Parses a [`Solver::summary`]. Entries stored before the daemon
    /// recorded identities have an empty summary, which gives `None`.
    #[must_use]
    pub fn parse_summary(text: &str) -> Option<Self> {
        let mut words = text.split_whitespace();
        let s = Self {
            name: words.next()?.to_owned(),
            version: words.next()?.to_owned(),
            binary_sha256: words.next()?.to_owned(),
            platform: words.next()?.to_owned(),
        };
        (words.next().is_none() && s.well_formed()).then_some(s)
    }

    /// Whether every field is non-empty and safe as a path component, and
    /// the binary hash is 64 lowercase hex digits.
    #[must_use]
    pub fn well_formed(&self) -> bool {
        let safe = |s: &str| {
            !s.is_empty()
                && s != "."
                && s != ".."
                && s.bytes()
                    .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-' | b'+'))
        };
        safe(&self.name)
            && safe(&self.version)
            && safe(&self.platform)
            && self.binary_sha256.len() == 64
            && self
                .binary_sha256
                .bytes()
                .all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
    }
}

/// The header line of a shared entry file.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Header {
    /// Always [`FORMAT`].
    pub format: String,
    /// The entry format version, [`SHARED_VERSION`].
    pub entry_version: u32,
    /// The v2 key (ADR 0008), as hex.
    pub key: String,
    /// SHA-256 of the reply bytes, as hex.
    pub reply_sha256: String,
    /// The command the reply answers, for example `check-sat`.
    pub command: String,
    /// The mode the key was derived under: [`MODE_DEFAULT`].
    pub mode: String,
    /// The solver that produced the reply.
    pub solver: Solver,
    /// Effort for `check-sat` replies (ADR 0009).
    #[serde(default)]
    pub effort: Option<Effort>,
    /// Unix time the entry was published.
    pub published_unix: u64,
    /// The signer's key id ([`PublicKey::id`]).
    pub signer: String,
    /// The ed25519 signature over [`Header::signed_bytes`], as hex.
    pub signature: String,
}

fn field(out: &mut Vec<u8>, bytes: &[u8]) {
    out.extend_from_slice(&(bytes.len() as u64).to_le_bytes());
    out.extend_from_slice(bytes);
}

fn opt_u64(out: &mut Vec<u8>, v: Option<u64>) {
    match v {
        None => out.push(0),
        Some(n) => {
            out.push(1);
            out.extend_from_slice(&n.to_le_bytes());
        }
    }
}

impl Header {
    /// The bytes the signature covers: a domain tag, then every header
    /// field except the signature, each length-prefixed (or tagged, for
    /// optional numbers), so distinct headers never encode alike.
    #[must_use]
    pub fn signed_bytes(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(512);
        field(&mut out, DOMAIN);
        field(&mut out, self.format.as_bytes());
        out.extend_from_slice(&self.entry_version.to_le_bytes());
        field(&mut out, self.key.as_bytes());
        field(&mut out, self.reply_sha256.as_bytes());
        field(&mut out, self.command.as_bytes());
        field(&mut out, self.mode.as_bytes());
        field(&mut out, self.solver.name.as_bytes());
        field(&mut out, self.solver.version.as_bytes());
        field(&mut out, self.solver.binary_sha256.as_bytes());
        field(&mut out, self.solver.platform.as_bytes());
        match &self.effort {
            None => out.push(0),
            Some(e) => {
                out.push(1);
                opt_u64(&mut out, e.limit);
                opt_u64(&mut out, e.used);
                match &e.reason {
                    None => out.push(0),
                    Some(r) => {
                        out.push(1);
                        field(&mut out, r.as_bytes());
                    }
                }
            }
        }
        out.extend_from_slice(&self.published_unix.to_le_bytes());
        field(&mut out, self.signer.as_bytes());
        out
    }
}

/// An ed25519 signing key. Its bytes are wiped when it is dropped, and
/// `Debug` never prints them.
pub struct SecretKey(SigningKey);

impl std::fmt::Debug for SecretKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "SecretKey({})", self.public().id())
    }
}

impl SecretKey {
    /// A new key from the operating system's random source.
    ///
    /// # Errors
    ///
    /// The random source cannot be read.
    pub fn generate() -> std::io::Result<Self> {
        let mut seed = [0u8; 32];
        std::fs::File::open("/dev/urandom")?.read_exact(&mut seed)?;
        let key = Self::from_seed(seed);
        seed.fill(0);
        Ok(key)
    }

    /// The key with this 32-byte seed (for tests and fixtures).
    #[must_use]
    pub fn from_seed(seed: [u8; 32]) -> Self {
        Self(SigningKey::from_bytes(&seed))
    }

    /// Its public key.
    #[must_use]
    pub fn public(&self) -> PublicKey {
        PublicKey(self.0.verifying_key())
    }

    /// The key file text: `sail-ed25519-secret-v1 <hex>`.
    #[must_use]
    pub fn to_text(&self) -> String {
        format!("{SECRET_TAG} {}\n", hex(self.0.as_bytes()))
    }

    /// Parses [`SecretKey::to_text`].
    #[must_use]
    pub fn parse(text: &str) -> Option<Self> {
        let mut words = text.split_whitespace();
        (words.next()? == SECRET_TAG).then_some(())?;
        let seed = unhex::<32>(words.next()?)?;
        words.next().is_none().then(|| Self::from_seed(seed))
    }

    /// Reads a key file.
    ///
    /// # Errors
    ///
    /// The file cannot be read or is not a secret key.
    pub fn read_file(path: &Path) -> Result<Self, String> {
        let text = std::fs::read_to_string(path)
            .map_err(|e| format!("cannot read {}: {e}", path.display()))?;
        Self::parse(&text).ok_or_else(|| format!("{} is not a sail secret key", path.display()))
    }

    /// Writes this key to `path` (readable by its owner only) and its
    /// public key to `<path>.pub`. Refuses to overwrite either file.
    ///
    /// # Errors
    ///
    /// Either file exists, or cannot be written.
    pub fn write_files(&self, path: &Path) -> std::io::Result<PathBuf> {
        let mut public = path.as_os_str().to_owned();
        public.push(".pub");
        let public = PathBuf::from(public);
        let mut opts = std::fs::OpenOptions::new();
        opts.write(true).create_new(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt as _;
            opts.mode(0o600);
        }
        if public.exists() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::AlreadyExists,
                format!("{} exists", public.display()),
            ));
        }
        opts.open(path)?.write_all(self.to_text().as_bytes())?;
        std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&public)?
            .write_all(self.public().to_text().as_bytes())?;
        Ok(public)
    }

    fn sign(&self, message: &[u8]) -> Signature {
        self.0.sign(message)
    }
}

/// An ed25519 public key that verifies shared entries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PublicKey(VerifyingKey);

impl PublicKey {
    /// The key id: the first 16 hex digits of the SHA-256 of the key.
    /// Entries name their signer by id; a reader verifies with the trusted
    /// key of that id, so a colliding id never verifies a signature.
    #[must_use]
    pub fn id(&self) -> String {
        hex(&sha256(self.0.as_bytes())).chars().take(16).collect()
    }

    /// The key file text: `sail-ed25519-public-v1 <hex>`.
    #[must_use]
    pub fn to_text(&self) -> String {
        format!("{PUBLIC_TAG} {}\n", hex(self.0.as_bytes()))
    }

    /// Parses [`PublicKey::to_text`] (anything after the key on its line,
    /// such as a comment, is ignored). Weak keys (small-order points) are
    /// refused.
    #[must_use]
    pub fn parse(text: &str) -> Option<Self> {
        let mut words = text.split_whitespace();
        (words.next()? == PUBLIC_TAG).then_some(())?;
        let bytes = unhex::<32>(words.next()?)?;
        VerifyingKey::from_bytes(&bytes)
            .ok()
            .filter(|k| !k.is_weak())
            .map(Self)
    }

    /// Reads a public key file.
    ///
    /// # Errors
    ///
    /// The file cannot be read or is not a public key.
    pub fn read_file(path: &Path) -> Result<Self, String> {
        let text = std::fs::read_to_string(path)
            .map_err(|e| format!("cannot read {}: {e}", path.display()))?;
        Self::parse(&text).ok_or_else(|| format!("{} is not a sail public key", path.display()))
    }
}

/// The trust directory of a cache root: `<root>/trust`.
#[must_use]
pub fn trust_dir(root: &Path) -> PathBuf {
    root.join("trust")
}

/// Public keys whose signatures a reader accepts.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TrustedKeys {
    keys: BTreeMap<String, PublicKey>,
}

impl TrustedKeys {
    /// Every key in `dir/*.pub`, and a description of each `.pub` file that
    /// is not a valid key. A missing directory trusts nothing.
    #[must_use]
    pub fn load_dir(dir: &Path) -> (Self, Vec<String>) {
        let mut keys = Self::default();
        let mut problems = Vec::new();
        let Ok(entries) = std::fs::read_dir(dir) else {
            return (keys, problems);
        };
        for e in entries.flatten() {
            let path = e.path();
            if path.extension().is_none_or(|x| x != "pub") {
                continue;
            }
            match PublicKey::read_file(&path) {
                Ok(k) => keys.insert(k),
                Err(msg) => problems.push(msg),
            }
        }
        (keys, problems)
    }

    /// Trusts `key`.
    pub fn insert(&mut self, key: PublicKey) {
        self.keys.insert(key.id(), key);
    }

    /// The trusted key with this id.
    #[must_use]
    pub fn get(&self, id: &str) -> Option<&PublicKey> {
        self.keys.get(id)
    }

    /// The ids of every trusted key.
    #[must_use]
    pub fn ids(&self) -> Vec<String> {
        self.keys.keys().cloned().collect()
    }

    /// How many keys are trusted.
    #[must_use]
    pub fn len(&self) -> usize {
        self.keys.len()
    }

    /// Whether no key is trusted.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.keys.is_empty()
    }
}

/// Trusts `key` for the cache at `root`: writes `<root>/trust/<id>.pub`
/// atomically, and returns its path.
///
/// # Errors
///
/// I/O errors.
pub fn add_trusted(root: &Path, key: &PublicKey) -> std::io::Result<PathBuf> {
    let dir = trust_dir(root);
    std::fs::create_dir_all(&dir)?;
    let path = dir.join(format!("{}.pub", key.id()));
    let tmp = dir.join(format!(".tmp-{}-{}", std::process::id(), key.id()));
    std::fs::write(&tmp, key.to_text())?;
    std::fs::rename(&tmp, &path).inspect_err(|_| {
        let _ = std::fs::remove_file(&tmp);
    })?;
    Ok(path)
}

/// Why a shared entry was not used. Every rejection is a miss.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Rejection {
    /// The file could not be opened or read.
    Unreadable,
    /// Not a regular file.
    NotAFile,
    /// Larger than [`MAX_ENTRY_BYTES`].
    TooLarge,
    /// No header line, or not a valid header.
    BadHeader,
    /// Another format or format version.
    WrongFormat,
    /// The header names another key.
    KeyMismatch,
    /// The signer is not a trusted key.
    UntrustedSigner,
    /// The signature does not verify against the trusted key.
    BadSignature,
    /// The reply does not hash to the signed hash.
    HashMismatch,
    /// Signed for another solver (name, version or binary).
    OtherSolver,
    /// Signed for this solver on another platform class. Cross-class hits
    /// are only for a named opt-in mode (ADR 0010), which does not exist.
    OtherPlatform,
    /// Signed for a mode other than default.
    OtherMode,
    /// The reply does not have the shape its command requires.
    BadShape,
    /// An `unknown` answer the serving policy would not serve (ADR 0009).
    UnservableUnknown,
}

impl Rejection {
    /// A short name for logs and reports.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Unreadable => "unreadable",
            Self::NotAFile => "not-a-file",
            Self::TooLarge => "too-large",
            Self::BadHeader => "bad-header",
            Self::WrongFormat => "wrong-format",
            Self::KeyMismatch => "key-mismatch",
            Self::UntrustedSigner => "untrusted-signer",
            Self::BadSignature => "bad-signature",
            Self::HashMismatch => "hash-mismatch",
            Self::OtherSolver => "other-solver",
            Self::OtherPlatform => "other-platform",
            Self::OtherMode => "other-mode",
            Self::BadShape => "bad-shape",
            Self::UnservableUnknown => "unservable-unknown",
        }
    }
}

/// A shared entry that passed every check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Verified {
    /// The command the reply answers.
    pub command: String,
    /// The exact reply bytes.
    pub reply: Vec<u8>,
    /// The signed effort.
    pub effort: Option<Effort>,
    /// The signer's key id.
    pub signer: String,
}

impl Verified {
    /// The provenance a local copy records (charter rule 7):
    /// `shared:<key id>`.
    #[must_use]
    pub fn provenance(&self) -> String {
        format!("{PROVENANCE_PREFIX}{}", self.signer)
    }
}

/// Prefix of the provenance of every local entry copied from the shared
/// tier. Such entries are never published again.
pub const PROVENANCE_PREFIX: &str = "shared:";

/// The provenance of replies the local daemon recorded from the pinned
/// solver: the only ones `publish` exports.
pub const PINNED_SOLVER: &str = "pinned-solver";

/// Builds a signed shared entry file for `reply`, the reply of `command`
/// under `key`, produced by `solver`.
#[must_use]
pub fn sign_entry(
    secret: &SecretKey,
    key: &QueryKey,
    reply: &[u8],
    command: &str,
    solver: &Solver,
    effort: Option<&Effort>,
    published_unix: u64,
) -> Vec<u8> {
    let mut header = Header {
        format: FORMAT.to_owned(),
        entry_version: SHARED_VERSION,
        key: key.to_string(),
        reply_sha256: hex(&sha256(reply)),
        command: command.to_owned(),
        mode: MODE_DEFAULT.to_owned(),
        solver: solver.clone(),
        effort: effort.cloned(),
        published_unix,
        signer: secret.public().id(),
        signature: String::new(),
    };
    header.signature = hex(&secret.sign(&header.signed_bytes()).to_bytes());
    let mut out = serde_json::to_vec(&header).unwrap_or_default();
    out.push(b'\n');
    out.extend_from_slice(reply);
    out
}

/// Checks a shared entry file's contents for `key`, in this order:
/// the header parses and has this format and version; it names `key`; its
/// signer is trusted and the signature verifies over every header field;
/// the reply hashes to the signed hash; the signed solver is `local`'s
/// (name, version and binary), on `local`'s platform class; the mode is
/// default; and the reply has the shape its command requires.
///
/// # Errors
///
/// The first check that fails. The caller treats every one as a miss.
pub fn verify_entry(
    data: &[u8],
    key: &QueryKey,
    trusted: &TrustedKeys,
    local: &Solver,
) -> Result<Verified, Rejection> {
    let (header, reply) = verify_entry_ref(data, key, trusted, local)?;
    Ok(Verified {
        command: header.command,
        reply: reply.to_vec(),
        effort: header.effort,
        signer: header.signer,
    })
}

/// Applies every check of [`verify_entry`] while borrowing the reply bytes.
/// Servers can validate a large upload without allocating a second reply buffer.
///
/// # Errors
/// The first failed verification check, exactly as for [`verify_entry`].
pub fn verify_entry_ref<'a>(
    data: &'a [u8],
    key: &QueryKey,
    trusted: &TrustedKeys,
    local: &Solver,
) -> Result<(Header, &'a [u8]), Rejection> {
    let nl = data
        .iter()
        .position(|&b| b == b'\n')
        .ok_or(Rejection::BadHeader)?;
    let header: Header = data
        .get(..nl)
        .and_then(|h| serde_json::from_slice(h).ok())
        .ok_or(Rejection::BadHeader)?;
    if header.format != FORMAT || header.entry_version != SHARED_VERSION {
        return Err(Rejection::WrongFormat);
    }
    if header.key != key.to_string() {
        return Err(Rejection::KeyMismatch);
    }
    let signer = trusted
        .get(&header.signer)
        .ok_or(Rejection::UntrustedSigner)?;
    let signature = unhex::<64>(&header.signature)
        .map(|b| Signature::from_bytes(&b))
        .ok_or(Rejection::BadSignature)?;
    signer
        .0
        .verify_strict(&header.signed_bytes(), &signature)
        .map_err(|_| Rejection::BadSignature)?;
    let reply = data.get(nl + 1..).unwrap_or_default();
    if header.reply_sha256 != hex(&sha256(reply)) {
        return Err(Rejection::HashMismatch);
    }
    let s = &header.solver;
    if s.name != local.name || s.version != local.version || s.binary_sha256 != local.binary_sha256
    {
        return Err(Rejection::OtherSolver);
    }
    if s.platform != local.platform {
        return Err(Rejection::OtherPlatform);
    }
    if header.mode != MODE_DEFAULT {
        return Err(Rejection::OtherMode);
    }
    if !shape_ok(&header.command, reply) {
        return Err(Rejection::BadShape);
    }
    Ok((header, reply))
}

/// A shared store: a directory reachable as a path.
#[derive(Debug, Clone)]
pub struct SharedStore {
    root: PathBuf,
}

impl SharedStore {
    /// The store at `root`.
    #[must_use]
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self { root: root.into() }
    }

    /// Its root directory.
    #[must_use]
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// Where the entry for `key` produced by `solver` lives, or `None` if
    /// the solver's fields are not safe path components.
    #[must_use]
    pub fn entry_path(&self, solver: &Solver, key: &QueryKey) -> Option<PathBuf> {
        if !solver.well_formed() {
            return None;
        }
        let k = key.to_string();
        Some(
            self.root
                .join(STORE_DIR)
                .join(&solver.platform)
                .join(&solver.binary_sha256)
                .join(k.get(..2).unwrap_or("xx"))
                .join(k),
        )
    }

    /// Reads the file at `path`: `Ok(None)` if there is none. The file is
    /// opened before it is examined, so a store that hangs on open hangs
    /// here; callers bound this with a timeout.
    ///
    /// # Errors
    ///
    /// Not a regular file, too large, or unreadable.
    pub fn read(path: &Path) -> Result<Option<Vec<u8>>, Rejection> {
        let mut file = match std::fs::File::open(path) {
            Ok(f) => f,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(_) => return Err(Rejection::Unreadable),
        };
        let meta = file.metadata().map_err(|_| Rejection::Unreadable)?;
        if !meta.is_file() {
            return Err(Rejection::NotAFile);
        }
        if meta.len() > MAX_ENTRY_BYTES {
            return Err(Rejection::TooLarge);
        }
        let mut data = Vec::with_capacity(usize::try_from(meta.len()).unwrap_or(0));
        // Read at most one byte past the limit, in case the file grew.
        (&mut file)
            .take(MAX_ENTRY_BYTES + 1)
            .read_to_end(&mut data)
            .map_err(|_| Rejection::Unreadable)?;
        if data.len() as u64 > MAX_ENTRY_BYTES {
            return Err(Rejection::TooLarge);
        }
        Ok(Some(data))
    }

    /// Writes `data` to `path` atomically: a temporary file in the same
    /// directory, then a rename.
    ///
    /// # Errors
    ///
    /// I/O errors.
    pub fn write(path: &Path, data: &[u8]) -> std::io::Result<()> {
        let dir = path.parent().ok_or_else(|| {
            std::io::Error::new(std::io::ErrorKind::InvalidInput, "entry path has no parent")
        })?;
        std::fs::create_dir_all(dir)?;
        let seq = TMP_SEQ.fetch_add(1, Ordering::Relaxed);
        let tmp = dir.join(format!(".tmp-{}-{seq}", std::process::id()));
        let written = std::fs::write(&tmp, data).and_then(|()| std::fs::rename(&tmp, path));
        if written.is_err() {
            let _ = std::fs::remove_file(&tmp);
        }
        written
    }
}

/// What `publish` exports.
#[derive(Debug, Clone, Default)]
pub struct PublishOptions {
    /// Only entries for these commands (empty: every command).
    pub commands: Vec<String>,
    /// Only entries whose solver binary SHA-256 starts with this hex.
    pub solver_sha256: Option<String>,
    /// Count what would be published, and write nothing.
    pub dry_run: bool,
}

/// What `publish` did.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PublishReport {
    /// Valid local entries examined.
    pub examined: u64,
    /// Entries signed and written (or, in a dry run, that would be).
    pub published: u64,
    /// Entries the store already holds, signed by this key.
    pub present: u64,
    /// Entries that came from the shared tier: never published again.
    pub from_shared: u64,
    /// Entries with any other provenance than the local pinned solver.
    pub other_provenance: u64,
    /// Entries without a recorded solver identity (stored before the
    /// daemon recorded one).
    pub unattributed: u64,
    /// `unknown` answers the serving policy would not serve (ADR 0009).
    pub unknown_held: u64,
    /// Entries left out by the filters.
    pub filtered: u64,
    /// Invalid local files (see `sail cache verify`).
    pub invalid: u64,
    /// Entries that could not be written.
    pub failed: u64,
}

/// An entry `publish` may export: a local, pinned-solver, default-mode
/// reply with a recorded solver identity that the serving policy serves.
pub(crate) struct Eligible {
    pub(crate) key: QueryKey,
    pub(crate) meta: crate::Meta,
    pub(crate) reply: Vec<u8>,
    pub(crate) solver: Solver,
}

/// Visits every entry of `disk` that may be exported, counting the others
/// in `r`: only entries the local daemon recorded from the pinned solver
/// (provenance [`PINNED_SOLVER`]) with a recorded solver identity; never
/// entries that came from the shared tier; an `unknown` answer only if
/// `unknown_ok` accepts its effort (ADR 0009).
pub(crate) fn eligible(
    disk: &DiskCache,
    options: &PublishOptions,
    unknown_ok: &dyn Fn(Option<&Effort>) -> bool,
    r: &mut PublishReport,
    mut each: impl FnMut(Eligible, &mut PublishReport),
) {
    for listed in disk.list() {
        let FileKind::Entry(key) = listed.kind else {
            continue;
        };
        let Ok((meta, reply)) = DiskCache::check_file(&listed.path, &key) else {
            r.invalid += 1;
            continue;
        };
        r.examined += 1;
        if meta.mode != MODE_DEFAULT {
            r.filtered += 1;
            continue;
        }
        if meta.provenance.starts_with(PROVENANCE_PREFIX) {
            r.from_shared += 1;
            continue;
        }
        if meta.provenance != PINNED_SOLVER {
            r.other_provenance += 1;
            continue;
        }
        let Some(solver) = Solver::parse_summary(&meta.solver) else {
            r.unattributed += 1;
            continue;
        };
        if (!options.commands.is_empty() && !options.commands.contains(&meta.command))
            || options
                .solver_sha256
                .as_ref()
                .is_some_and(|p| !solver.binary_sha256.starts_with(p.as_str()))
        {
            r.filtered += 1;
            continue;
        }
        if meta.command.starts_with("check-sat")
            && reply.trim_ascii() == b"unknown"
            && !unknown_ok(meta.effort.as_ref())
        {
            r.unknown_held += 1;
            continue;
        }
        each(
            Eligible {
                key,
                meta,
                reply,
                solver,
            },
            r,
        );
    }
}

/// The current Unix time, for `published_unix`.
pub(crate) fn now_unix() -> u64 {
    SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

/// Exports `disk`'s entries to `store`, signed with `secret`.
///
/// Only entries the local daemon recorded from the pinned solver
/// (provenance [`PINNED_SOLVER`]) with a recorded solver identity are
/// exported; entries that came from the shared tier never are. An
/// `unknown` answer is exported only if `unknown_ok` accepts its effort
/// (the serving policy, ADR 0009). An entry the store already holds,
/// signed by this key, is left alone.
pub fn publish(
    disk: &DiskCache,
    store: &SharedStore,
    secret: &SecretKey,
    options: &PublishOptions,
    unknown_ok: &dyn Fn(Option<&Effort>) -> bool,
) -> PublishReport {
    let mut report = PublishReport::default();
    let mut ours = TrustedKeys::default();
    ours.insert(secret.public());
    let now = now_unix();
    eligible(disk, options, unknown_ok, &mut report, |e, r| {
        let Some(path) = store.entry_path(&e.solver, &e.key) else {
            r.unattributed += 1;
            return;
        };
        let present = SharedStore::read(&path)
            .ok()
            .flatten()
            .is_some_and(|data| verify_entry(&data, &e.key, &ours, &e.solver).is_ok());
        if present {
            r.present += 1;
            return;
        }
        if options.dry_run {
            r.published += 1;
            return;
        }
        let data = sign_entry(
            secret,
            &e.key,
            &e.reply,
            &e.meta.command,
            &e.solver,
            e.meta.effort.as_ref(),
            now,
        );
        match SharedStore::write(&path, &data) {
            Ok(()) => r.published += 1,
            Err(_) => r.failed += 1,
        }
    });
    report
}

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

    fn solver() -> Solver {
        Solver {
            name: "z3".to_owned(),
            version: "4.16.0".to_owned(),
            binary_sha256: "ab".repeat(32),
            platform: "macos-aarch64".to_owned(),
        }
    }

    fn trusting(k: &SecretKey) -> TrustedKeys {
        let mut t = TrustedKeys::default();
        t.insert(k.public());
        t
    }

    const KEY: QueryKey = QueryKey([7; 32]);

    /// Key byte, reply, command, solver summary, provenance and effort.
    type Put<'a> = (u8, &'a [u8], &'a str, &'a str, &'a str, Option<&'a Effort>);

    #[test]
    fn a_signed_entry_verifies_and_every_field_is_bound() {
        let sk = SecretKey::from_seed([1; 32]);
        let trusted = trusting(&sk);
        let effort = Effort {
            limit: Some(100),
            used: Some(3),
            reason: Some("(:reason-unknown \"incomplete quantifiers\")".to_owned()),
        };
        let data = sign_entry(
            &sk,
            &KEY,
            b"unknown\n",
            "check-sat",
            &solver(),
            Some(&effort),
            9,
        );
        let v = verify_entry(&data, &KEY, &trusted, &solver());
        assert_eq!(
            v.as_ref().map(|v| (v.reply.as_slice(), v.effort.as_ref())),
            Ok((&b"unknown\n"[..], Some(&effort)))
        );
        assert_eq!(
            v.map(|v| v.provenance()),
            Ok(format!("shared:{}", sk.public().id()))
        );

        // Change each header field after signing: the signature fails.
        let nl = data.iter().position(|&b| b == b'\n').unwrap_or(0);
        let header: serde_json::Value =
            serde_json::from_slice(data.get(..nl).unwrap_or_default()).unwrap_or_default();
        let reply = data.get(nl + 1..).unwrap_or_default();
        // Produced independently from the existing v2 signed entry format.
        assert_eq!(
            header["signature"].as_str(),
            Some(concat!(
                "5e3e7aef8f5e9b7a207c5a2639b6d2e878d252287c291f46d96eae719b600a32",
                "6f50de40e8bf38d0f4f1303d76d2397f6820dd5e3c0259a7cc95db9e9a680b01",
            )),
        );
        let edits: &[(&str, serde_json::Value)] = &[
            ("command", "get-model".into()),
            ("mode", "opt-in".into()),
            ("published_unix", 10.into()),
            (
                "effort",
                serde_json::json!({"limit": 100, "used": 99, "reason": null}),
            ),
            ("effort", serde_json::Value::Null),
        ];
        for (name, value) in edits {
            let mut h = header.clone();
            h[*name] = value.clone();
            let mut forged = serde_json::to_vec(&h).unwrap_or_default();
            forged.push(b'\n');
            forged.extend_from_slice(reply);
            assert_eq!(
                verify_entry(&forged, &KEY, &trusted, &solver()),
                Err(Rejection::BadSignature),
                "{name}"
            );
        }
        for (field, value) in [
            ("name", "cvc5"),
            ("version", "4.16.1"),
            ("platform", "linux-x86_64"),
        ] {
            let mut h = header.clone();
            h["solver"][field] = value.into();
            let mut forged = serde_json::to_vec(&h).unwrap_or_default();
            forged.push(b'\n');
            forged.extend_from_slice(reply);
            assert_eq!(
                verify_entry(&forged, &KEY, &trusted, &solver()),
                Err(Rejection::BadSignature),
                "{field}"
            );
        }
    }

    #[test]
    fn forged_untrusted_corrupt_and_foreign_entries_are_rejected() {
        let sk = SecretKey::from_seed([1; 32]);
        let other = SecretKey::from_seed([2; 32]);
        let trusted = trusting(&sk);
        let good = sign_entry(&sk, &KEY, b"unsat\n", "check-sat", &solver(), None, 1);
        // Even a trusted signature must not admit the old publication rules.
        let end = good.iter().position(|&b| b == b'\n').unwrap_or(0);
        let mut old_header: Header = serde_json::from_slice(&good[..end])
            .unwrap_or_else(|e| unreachable!("valid fixture: {e}"));
        old_header.entry_version = 1;
        old_header.signature = hex(&sk.sign(&old_header.signed_bytes()).to_bytes());
        let mut old = serde_json::to_vec(&old_header).unwrap_or_default();
        old.extend_from_slice(&good[end..]);
        assert_eq!(
            verify_entry(&old, &KEY, &trusted, &solver()),
            Err(Rejection::WrongFormat)
        );

        // A forgery with a valid hash and no valid signature.
        let mut forged = sign_entry(&other, &KEY, b"sat\n", "check-sat", &solver(), None, 1);
        assert_eq!(
            verify_entry(&forged, &KEY, &trusted, &solver()),
            Err(Rejection::UntrustedSigner)
        );
        // ... claiming the trusted signer's id.
        let text =
            String::from_utf8_lossy(&forged).replace(&other.public().id(), &sk.public().id());
        forged = text.into_bytes();
        assert_eq!(
            verify_entry(&forged, &KEY, &trusted, &solver()),
            Err(Rejection::BadSignature)
        );
        // The reply swapped under a valid header.
        let swapped = String::from_utf8_lossy(&good).replace("\nunsat\n", "\nsat\n");
        assert_eq!(
            verify_entry(swapped.as_bytes(), &KEY, &trusted, &solver()),
            Err(Rejection::HashMismatch)
        );
        // Truncated, empty, garbage.
        let half = good.get(..good.len() / 2).unwrap_or_default();
        assert_eq!(
            verify_entry(half, &KEY, &trusted, &solver()),
            Err(Rejection::BadHeader)
        );
        assert_eq!(
            verify_entry(b"", &KEY, &trusted, &solver()),
            Err(Rejection::BadHeader)
        );
        assert!(verify_entry(b"\x00\xff garbage\nunsat\n", &KEY, &trusted, &solver()).is_err());
        // Another key.
        assert_eq!(
            verify_entry(&good, &QueryKey([8; 32]), &trusted, &solver()),
            Err(Rejection::KeyMismatch)
        );
        // Another solver binary, another platform class.
        let mut s = solver();
        s.binary_sha256 = "cd".repeat(32);
        assert_eq!(
            verify_entry(&good, &KEY, &trusted, &s),
            Err(Rejection::OtherSolver)
        );
        let mut p = solver();
        p.platform = "linux-x86_64".to_owned();
        assert_eq!(
            verify_entry(&good, &KEY, &trusted, &p),
            Err(Rejection::OtherPlatform)
        );
        // A signed reply of the wrong shape.
        let bad = sign_entry(
            &sk,
            &KEY,
            b"(error \"x\")\n",
            "check-sat",
            &solver(),
            None,
            1,
        );
        assert_eq!(
            verify_entry(&bad, &KEY, &trusted, &solver()),
            Err(Rejection::BadShape)
        );
        // Nothing trusted.
        assert_eq!(
            verify_entry(&good, &KEY, &TrustedKeys::default(), &solver()),
            Err(Rejection::UntrustedSigner)
        );
    }

    #[test]
    fn keys_round_trip_through_text_and_files() {
        let sk = SecretKey::from_seed([3; 32]);
        let parsed = SecretKey::parse(&sk.to_text()).map(|k| k.public());
        assert_eq!(parsed, Some(sk.public()));
        assert_eq!(PublicKey::parse(&sk.public().to_text()), Some(sk.public()));
        assert_eq!(
            PublicKey::parse(&format!("{} ci release", sk.public().to_text().trim())),
            Some(sk.public())
        );
        assert!(PublicKey::parse(&sk.to_text()).is_none());
        assert!(SecretKey::parse(&sk.public().to_text()).is_none());
        assert!(PublicKey::parse("sail-ed25519-public-v1 zz").is_none());
        // The identity point is weak and refused.
        let weak = format!("{PUBLIC_TAG} 01{}", "00".repeat(31));
        assert!(PublicKey::parse(&weak).is_none());
        assert!(!format!("{sk:?}").contains(&hex(&[3; 32])));
        assert_eq!(sk.public().id().len(), 16);

        let dir = std::env::temp_dir().join(format!("sail-shared-keys-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        assert!(std::fs::create_dir_all(&dir).is_ok());
        let generated = SecretKey::generate();
        assert!(generated.is_ok());
        if let Ok(g) = generated {
            let file = dir.join("ci.key");
            let public = g.write_files(&file);
            assert!(public.is_ok());
            assert!(g.write_files(&file).is_err(), "never overwrites a key");
            assert_eq!(
                SecretKey::read_file(&file).map(|k| k.public()),
                Ok(g.public())
            );
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt as _;
                let mode = std::fs::metadata(&file).map(|m| m.permissions().mode() & 0o777);
                assert_eq!(mode.ok(), Some(0o600));
            }
            let root = dir.join("cache");
            assert!(add_trusted(&root, &g.public()).is_ok());
            assert!(std::fs::write(trust_dir(&root).join("junk.pub"), "nope").is_ok());
            let (keys, problems) = TrustedKeys::load_dir(&trust_dir(&root));
            assert_eq!(keys.ids(), vec![g.public().id()]);
            assert_eq!(problems.len(), 1);
        }
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn solver_summaries_round_trip_and_unsafe_ones_are_refused() {
        assert_eq!(Solver::parse_summary(&solver().summary()), Some(solver()));
        assert_eq!(Solver::parse_summary(""), None);
        assert_eq!(Solver::parse_summary("z3 4.16.0 abc macos"), None);
        let mut s = solver();
        s.platform = "../etc".to_owned();
        assert!(!s.well_formed());
        assert!(SharedStore::new("/s").entry_path(&s, &KEY).is_none());
    }

    #[test]
    fn publish_exports_only_local_pinned_solver_entries() {
        let dir = std::env::temp_dir().join(format!("sail-shared-pub-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        let disk = DiskCache::new(dir.join("cache"));
        let store = SharedStore::new(dir.join("store"));
        let sk = SecretKey::from_seed([4; 32]);
        let id = solver().summary();
        let far = Effort {
            limit: Some(100),
            used: Some(1),
            reason: Some("(:reason-unknown \"incomplete quantifiers\")".to_owned()),
        };
        let near = Effort {
            used: Some(90),
            ..far.clone()
        };
        let puts: &[Put] = &[
            (1, b"unsat\n", "check-sat", &id, PINNED_SOLVER, None),
            (
                2,
                b"sat\n",
                "check-sat",
                &id,
                "shared:0123456789abcdef",
                None,
            ),
            (3, b"unsat\n", "check-sat", "", PINNED_SOLVER, None),
            (
                4,
                b"unknown\n",
                "check-sat",
                &id,
                PINNED_SOLVER,
                Some(&near),
            ),
            (5, b"unknown\n", "check-sat", &id, PINNED_SOLVER, Some(&far)),
            (6, b"x\n", "echo", &id, PINNED_SOLVER, None),
        ];
        for (k, reply, cmd, solver, prov, effort) in puts {
            let r = disk.put(&QueryKey([*k; 32]), reply, cmd, solver, prov, *effort);
            assert!(r.is_ok());
        }
        assert!(
            disk.put_scoped(
                &QueryKey([7; 32]),
                b"unsat\n",
                "check-sat",
                &crate::StoreContext {
                    solver: &id,
                    provenance: PINNED_SOLVER,
                    mode: "accept-first-proof"
                },
                None
            )
            .is_ok()
        );
        let policy = |e: Option<&Effort>| e.is_some_and(|e| e.used.unwrap_or(u64::MAX) * 2 <= 100);
        let opts = PublishOptions {
            commands: vec!["check-sat".to_owned()],
            ..PublishOptions::default()
        };
        let r = publish(&disk, &store, &sk, &opts, &policy);
        assert_eq!(
            (
                r.examined,
                r.published,
                r.from_shared,
                r.unattributed,
                r.unknown_held,
                r.filtered
            ),
            (7, 2, 1, 1, 1, 2)
        );
        // Published entries verify for a reader trusting the key.
        let trusted = trusting(&sk);
        for k in [1u8, 5] {
            let key = QueryKey([k; 32]);
            let data = store
                .entry_path(&solver(), &key)
                .and_then(|p| SharedStore::read(&p).ok().flatten());
            let v = data.map(|d| verify_entry(&d, &key, &trusted, &solver()));
            assert!(matches!(v, Some(Ok(_))), "key {k}");
        }
        // A second run finds them present.
        let again = publish(&disk, &store, &sk, &opts, &policy);
        assert_eq!((again.published, again.present), (0, 2));
        let _ = std::fs::remove_dir_all(&dir);
    }
}