engenho-substrate 0.53.1

engenho-substrate — shared helpers extracted to the third-site rule. atomic_write (fsync-anchored tmp+rename), magic-headered blob encoding, file-format versioning. The substrate building blocks that compose under engenho-store::persistence, kasou's machine-identifier, and tend's daemon state.
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
//! Tiered derivation cache — walks an ordered list of backends,
//! promoting hits to higher (faster) tiers.
//!
//! ## Composition shape
//!
//! ```text
//! TieredCache { tiers: [L0 memory, L1 disk, L2 cluster, L3 federation] }
//!   get_drv(h):
//!     for each tier in order:
//!       if hit -> promote to all higher tiers, return
//!     return None
//!   put_drv(d):
//!     write to L0 only (the fastest local tier); L1+ are populated
//!     by promotion-on-hit + an out-of-band reconcile loop. The
//!     tiered-cache write contract is INTENTIONALLY narrow — see
//!     property test put_drv_writes_only_to_l0 for the asserted
//!     invariant.
//! ```
//!
//! ## Why "promote on read" + "write to local only"
//!
//! Symmetric with how every other tiered system (CPU caches, CDNs,
//! ZFS L2ARC, S3 + CloudFront) works. Higher tiers are eventually-
//! consistent reflections of authoritative lower tiers; writes are
//! cheap locally; reads pull data closer to the consumer.
//!
//! Cross-tier promotion is best-effort: a promote-failure to an
//! upper tier doesn't fail the read. The trait contract still holds
//! (returned bytes hash-verified by every backend's `get_nar`).

use std::sync::Arc;

use async_trait::async_trait;

use crate::derivation::{
    CacheError, DerivationCacheBackend, Drv, DrvHash, NarBlob, NarHash, Realisation,
};
use crate::promotion::{PromotionContext, PromotionGate, PromotionPolicy};

/// Composed cache: walks tiers in order, returns first hit,
/// promotes the hit to all higher (faster) tiers per the
/// configured [`PromotionPolicy`].
///
/// Construct with [`TieredCache::new`] (eager promotion default)
/// or [`TieredCache::with_promotion`] for a custom policy.
/// Tiers are FASTEST (L0) first, SLOWEST (Ln) last — load-bearing.
pub struct TieredCache {
    tiers: Vec<Arc<dyn DerivationCacheBackend>>,
    gate: Arc<PromotionGate>,
}

impl TieredCache {
    /// New tiered cache with eager promotion (default).
    /// Empty vec → cache is a no-op (every get returns None, every
    /// put succeeds silently).
    #[must_use]
    pub fn new(tiers: Vec<Arc<dyn DerivationCacheBackend>>) -> Self {
        Self {
            tiers,
            gate: Arc::new(PromotionGate::new(PromotionPolicy::Eager)),
        }
    }

    /// New tiered cache with a specific promotion policy.
    #[must_use]
    pub fn with_promotion(
        tiers: Vec<Arc<dyn DerivationCacheBackend>>,
        policy: PromotionPolicy,
    ) -> Self {
        Self {
            tiers,
            gate: Arc::new(PromotionGate::new(policy)),
        }
    }

    /// Total tiers (snapshot helper).
    fn total(&self) -> usize {
        self.tiers.len()
    }

    /// Number of tiers in this cache.
    #[must_use]
    pub fn tier_count(&self) -> usize {
        self.tiers.len()
    }

    /// Observable snapshot of the tier stack — name + tier_count.
    /// Pattern #2 (SSC v0.91) — TieredCache + CompositeShapeRenderer +
    /// ChainedVerifier all return canonical
    /// [`crate::mirante::ChildCountSnapshot`].
    #[must_use]
    pub fn snapshot(&self) -> crate::mirante::ChildCountSnapshot {
        crate::mirante::ChildCountSnapshot {
            name: "tiered",
            child_count: self.tier_count(),
        }
    }

    /// Backend name of tier `i` (telemetry helper).
    #[must_use]
    pub fn tier_name(&self, i: usize) -> Option<&'static str> {
        self.tiers.get(i).map(|t| t.name())
    }
}

crate::define_named!(TieredCache, "tiered");
crate::impl_observable!(TieredCache, crate::mirante::ChildCountSnapshot);

#[async_trait]
impl DerivationCacheBackend for TieredCache {
    fn name(&self) -> &'static str {
        "tiered"
    }

    async fn get_drv(&self, hash: &DrvHash) -> Result<Option<Drv>, CacheError> {
        let total = self.total();
        for (idx, tier) in self.tiers.iter().enumerate() {
            if let Some(drv) = tier.get_drv(hash).await? {
                // Ask the promotion gate.
                let ctx = PromotionContext {
                    source_tier: idx,
                    total_tiers: total,
                };
                if let Some(promote_to) = self.gate.decide(ctx) {
                    for higher in &self.tiers[..promote_to] {
                        let _ = higher.put_drv(&drv).await;
                    }
                }
                return Ok(Some(drv));
            }
        }
        Ok(None)
    }

    async fn put_drv(&self, drv: &Drv) -> Result<(), CacheError> {
        // Write-to-local: only the fastest tier (L0). Production
        // deployments add a separate reconcile loop that fans out
        // to cluster + federation. Single-tier setups still work
        // because L0 IS the storage.
        if let Some(local) = self.tiers.first() {
            local.put_drv(drv).await?;
        }
        Ok(())
    }

    async fn get_nar(&self, hash: &NarHash) -> Result<Option<NarBlob>, CacheError> {
        let total = self.total();
        for (idx, tier) in self.tiers.iter().enumerate() {
            // Get from this tier. HashMismatch propagates — corrupt
            // cache is a halting fault, not a fallback trigger.
            if let Some(blob) = tier.get_nar(hash).await? {
                let ctx = PromotionContext {
                    source_tier: idx,
                    total_tiers: total,
                };
                if let Some(promote_to) = self.gate.decide(ctx) {
                    for higher in &self.tiers[..promote_to] {
                        let _ = higher.put_nar(&blob).await;
                    }
                }
                return Ok(Some(blob));
            }
        }
        Ok(None)
    }

    async fn put_nar(&self, blob: &NarBlob) -> Result<(), CacheError> {
        if let Some(local) = self.tiers.first() {
            local.put_nar(blob).await?;
        }
        Ok(())
    }

    async fn list_realisations(&self, drv_hash: &DrvHash) -> Result<Vec<Realisation>, CacheError> {
        // Merge realisations across tiers: a realisation can exist
        // anywhere, and the lattice union is the correct answer
        // (every output-name distinct, latest wins).
        let mut merged: std::collections::BTreeMap<String, Realisation> =
            std::collections::BTreeMap::new();
        for tier in &self.tiers {
            for r in tier.list_realisations(drv_hash).await? {
                merged.insert(r.output_name.clone(), r);
            }
        }
        Ok(merged.into_values().collect())
    }

    async fn put_realisation(&self, realisation: &Realisation) -> Result<(), CacheError> {
        // Write to L0 only; promotion to upper tiers via reconcile.
        if let Some(local) = self.tiers.first() {
            local.put_realisation(realisation).await?;
        }
        Ok(())
    }
}

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

    fn sample_drv(tag: &[u8]) -> Drv {
        Drv::synthetic(DrvHash::from_bytes(tag), "x86_64-linux")
    }

    fn arc<T: DerivationCacheBackend + 'static>(t: T) -> Arc<dyn DerivationCacheBackend> {
        Arc::new(t)
    }

    #[tokio::test]
    async fn empty_tiered_cache_returns_none() {
        let c = TieredCache::new(vec![]);
        let h = DrvHash::from_bytes(b"x");
        assert!(c.get_drv(&h).await.unwrap().is_none());
        assert_eq!(c.tier_count(), 0);
    }

    #[tokio::test]
    async fn tier_count_and_names() {
        let l0 = arc(MemoryDerivationCache::new());
        let l1 = arc(MemoryDerivationCache::new());
        let c = TieredCache::new(vec![l0, l1]);
        assert_eq!(c.tier_count(), 2);
        assert_eq!(c.tier_name(0), Some("memory"));
        assert_eq!(c.tier_name(1), Some("memory"));
        assert_eq!(c.tier_name(2), None);
        assert_eq!(c.name(), "tiered");
    }

    #[tokio::test]
    async fn get_drv_walks_tiers_until_hit() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let drv = sample_drv(b"a");
        l1.put_drv(&drv).await.unwrap(); // only in L1
        let c = TieredCache::new(vec![l0.clone(), l1.clone()]);
        let got = c.get_drv(&drv.drv_hash).await.unwrap();
        assert_eq!(got, Some(drv.clone()));
        // Promotion: L0 should now have it too.
        assert_eq!(l0.get_drv(&drv.drv_hash).await.unwrap(), Some(drv));
    }

    #[tokio::test]
    async fn get_drv_short_circuits_at_first_hit() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let drv = sample_drv(b"hit-l0");
        l0.put_drv(&drv).await.unwrap();
        // L1 deliberately not populated.
        let c = TieredCache::new(vec![l0.clone(), l1.clone()]);
        let got = c.get_drv(&drv.drv_hash).await.unwrap();
        assert_eq!(got, Some(drv));
        // L1 should not have been written to (no promotion needed).
        assert_eq!(l1.drv_count().await, 0);
    }

    #[tokio::test]
    async fn put_drv_writes_to_local_only() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let c = TieredCache::new(vec![l0.clone(), l1.clone()]);
        c.put_drv(&sample_drv(b"q")).await.unwrap();
        assert_eq!(l0.drv_count().await, 1);
        assert_eq!(l1.drv_count().await, 0);
    }

    #[tokio::test]
    async fn get_nar_walks_and_promotes() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let blob = NarBlob::from_bytes(b"nar-bytes".to_vec());
        l1.put_nar(&blob).await.unwrap();
        let c = TieredCache::new(vec![l0.clone(), l1.clone()]);
        let got = c.get_nar(&blob.hash).await.unwrap();
        assert_eq!(got, Some(blob.clone()));
        // Promotion: L0 should have it.
        assert_eq!(l0.get_nar(&blob.hash).await.unwrap(), Some(blob));
    }

    #[tokio::test]
    async fn list_realisations_merges_across_tiers() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let drv_hash = DrvHash::from_bytes(b"d");
        // L0 has "out", L1 has "dev"; merge should produce both.
        l0.put_realisation(&Realisation {
            drv_hash: drv_hash.clone(),
            output_name: "out".into(),
            output_path: OutputPath::new("/nix/store/a-out"),
            nar_hash: None,
        })
        .await
        .unwrap();
        l1.put_realisation(&Realisation {
            drv_hash: drv_hash.clone(),
            output_name: "dev".into(),
            output_path: OutputPath::new("/nix/store/b-dev"),
            nar_hash: None,
        })
        .await
        .unwrap();
        let c = TieredCache::new(vec![l0, l1]);
        let list = c.list_realisations(&drv_hash).await.unwrap();
        assert_eq!(list.len(), 2);
        assert!(list.iter().any(|r| r.output_name == "out"));
        assert!(list.iter().any(|r| r.output_name == "dev"));
    }

    #[tokio::test]
    async fn list_realisations_lower_tier_loses_to_higher_on_same_output_name() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let drv_hash = DrvHash::from_bytes(b"d");
        // Both tiers have "out" but with DIFFERENT output paths.
        l0.put_realisation(&Realisation {
            drv_hash: drv_hash.clone(),
            output_name: "out".into(),
            output_path: OutputPath::new("/nix/store/l0-out"),
            nar_hash: None,
        })
        .await
        .unwrap();
        l1.put_realisation(&Realisation {
            drv_hash: drv_hash.clone(),
            output_name: "out".into(),
            output_path: OutputPath::new("/nix/store/l1-out"),
            nar_hash: None,
        })
        .await
        .unwrap();
        // Merge walks L0 first then L1; later insert wins (L1's path).
        // Operators wiring real tiers should make L0 authoritative; the
        // merge semantics here are "lattice union, latest wins" — that
        // matches the eventual-consistency story across tiers.
        let c = TieredCache::new(vec![l0, l1]);
        let list = c.list_realisations(&drv_hash).await.unwrap();
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].output_path.as_str(), "/nix/store/l1-out");
    }

    #[tokio::test]
    async fn put_realisation_writes_to_local_only() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let drv_hash = DrvHash::from_bytes(b"d");
        let c = TieredCache::new(vec![l0.clone(), l1.clone()]);
        c.put_realisation(&Realisation {
            drv_hash: drv_hash.clone(),
            output_name: "out".into(),
            output_path: OutputPath::new("/nix/store/a-out"),
            nar_hash: None,
        })
        .await
        .unwrap();
        assert_eq!(l0.list_realisations(&drv_hash).await.unwrap().len(), 1);
        assert_eq!(l1.list_realisations(&drv_hash).await.unwrap().len(), 0);
    }

    #[tokio::test]
    async fn miss_in_all_tiers_returns_none() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let c = TieredCache::new(vec![l0, l1]);
        let h = DrvHash::from_bytes(b"never-seen");
        assert!(c.get_drv(&h).await.unwrap().is_none());
        assert!(
            c.get_nar(&NarHash::from_bytes(b"never"))
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn with_promotion_lazy_disables_promote_on_drv_read() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let drv = sample_drv(b"lazy-drv");
        l1.put_drv(&drv).await.unwrap();
        let c = TieredCache::with_promotion(vec![l0.clone(), l1.clone()], PromotionPolicy::Lazy);
        let got = c.get_drv(&drv.drv_hash).await.unwrap();
        assert_eq!(got, Some(drv.clone()));
        // L0 should NOT have been written to — promotion suppressed.
        assert_eq!(l0.drv_count().await, 0);
    }

    #[tokio::test]
    async fn with_promotion_lazy_disables_promote_on_nar_read() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let blob = NarBlob::from_bytes(b"lazy-nar".to_vec());
        l1.put_nar(&blob).await.unwrap();
        let c = TieredCache::with_promotion(vec![l0.clone(), l1.clone()], PromotionPolicy::Lazy);
        let got = c.get_nar(&blob.hash).await.unwrap();
        assert_eq!(got, Some(blob));
        assert_eq!(l0.nar_count().await, 0);
    }

    #[tokio::test]
    async fn with_promotion_eager_still_promotes() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        let drv = sample_drv(b"eager-drv");
        l1.put_drv(&drv).await.unwrap();
        let c = TieredCache::with_promotion(vec![l0.clone(), l1.clone()], PromotionPolicy::Eager);
        c.get_drv(&drv.drv_hash).await.unwrap();
        // Eager preserves the original behavior.
        assert_eq!(l0.drv_count().await, 1);
    }

    #[tokio::test]
    async fn with_promotion_sample_rate_promotes_one_in_n() {
        let l0 = Arc::new(MemoryDerivationCache::new());
        let l1 = Arc::new(MemoryDerivationCache::new());
        // Pre-populate L1 with several distinct drvs.
        let drvs: Vec<Drv> = (0u8..6).map(|i| sample_drv(&[b'k', i])).collect();
        for d in &drvs {
            l1.put_drv(d).await.unwrap();
        }
        let c = TieredCache::with_promotion(
            vec![l0.clone(), l1.clone()],
            PromotionPolicy::SampleRate(3),
        );
        // 6 reads at rate 3 → exactly 2 promotions.
        for d in &drvs {
            c.get_drv(&d.drv_hash).await.unwrap();
        }
        assert_eq!(l0.drv_count().await, 2);
    }
}