engenho-substrate 0.53.180

engenho-substrate — the substrate leaf: every module a shipped engenho crate references (derivations and caches, receipts, quorum, ledgers, verifiers, rollout gates, freshness, owned tasks), with engenho-substrate-core re-exported whole so every engenho_substrate:: path keeps resolving.
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
//! `RetryingCacheBackend` — pluggable backoff wrapper around any
//! `DerivationCacheBackend`. Transient `CacheError::Backend`
//! failures are retried with typed backoff; structural errors
//! (`HashMismatch`, `NotFound`) pass through unchanged.
//!
//! ## When to use
//!
//!   * Wrap remote-tier backends (iroh / NATS Object / federation)
//!     where network blips are recoverable.
//!   * Add resilience to a single backend before composing it into
//!     a `TieredCache` without rewriting the underlying impl.
//!
//! ## When NOT to use
//!
//!   * Local-disk caches — retries on disk errors usually paper
//!     over real problems (disk full, permission denied).
//!   * Backends that already retry internally — double-retry is
//!     amplification.

use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;

use crate::derivation::{
    CacheError, DerivationCacheBackend, Drv, DrvHash, NarBlob, NarHash, Realisation,
};

/// Backoff policy.
#[derive(Clone, Debug)]
pub struct BackoffConfig {
    /// Max attempts (including the initial). 1 = no retry.
    pub max_attempts: u32,
    /// Initial delay before the second attempt.
    pub initial_delay: Duration,
    /// Each subsequent delay multiplied by this factor.
    /// 1.0 = constant; 2.0 = exponential doubling.
    pub multiplier: f64,
    /// Hard ceiling per attempt's delay.
    pub max_delay: Duration,
}

impl Default for BackoffConfig {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            initial_delay: Duration::from_millis(50),
            multiplier: 2.0,
            max_delay: Duration::from_secs(1),
        }
    }
}

impl BackoffConfig {
    /// No retry — fail on first attempt.
    #[must_use]
    pub fn no_retry() -> Self {
        Self {
            max_attempts: 1,
            ..Self::default()
        }
    }

    /// Compute the delay before attempt `n` (1-indexed; attempt 1
    /// is free, returns `Duration::ZERO`).
    #[must_use]
    pub fn delay_for(&self, attempt: u32) -> Duration {
        if attempt <= 1 {
            return Duration::ZERO;
        }
        let exp = i32::try_from(attempt - 2).unwrap_or(i32::MAX);
        let multiplied = self.initial_delay.as_secs_f64() * self.multiplier.powi(exp);
        let capped = multiplied.min(self.max_delay.as_secs_f64());
        Duration::from_secs_f64(capped)
    }
}

/// Wrapper backend.
pub struct RetryingCacheBackend {
    inner: Arc<dyn DerivationCacheBackend>,
    config: BackoffConfig,
}

impl RetryingCacheBackend {
    /// New wrapper with default config (3 attempts, 50ms initial,
    /// 2x multiplier, 1s max).
    #[must_use]
    pub fn new(inner: Arc<dyn DerivationCacheBackend>) -> Self {
        Self {
            inner,
            config: BackoffConfig::default(),
        }
    }

    /// New wrapper with explicit backoff.
    #[must_use]
    pub fn with_config(inner: Arc<dyn DerivationCacheBackend>, config: BackoffConfig) -> Self {
        Self { inner, config }
    }

    /// True if this error is retryable. Backend errors are; hash
    /// mismatches and not-found are NOT (they're structural).
    fn is_retryable(err: &CacheError) -> bool {
        matches!(err, CacheError::Backend(_))
    }

    /// Generic retry harness; used by every wrapped op.
    async fn retry<F, Fut, T>(&self, mut op: F) -> Result<T, CacheError>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<T, CacheError>>,
    {
        let mut attempt = 1u32;
        loop {
            match op().await {
                Ok(t) => return Ok(t),
                Err(e) if !Self::is_retryable(&e) => return Err(e),
                Err(e) => {
                    if attempt >= self.config.max_attempts {
                        return Err(e);
                    }
                    let delay = self.config.delay_for(attempt + 1);
                    if !delay.is_zero() {
                        tokio::time::sleep(delay).await;
                    }
                    attempt += 1;
                }
            }
        }
    }
}

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

    async fn get_drv(&self, hash: &DrvHash) -> Result<Option<Drv>, CacheError> {
        let inner = self.inner.clone();
        let hash = hash.clone();
        self.retry(|| {
            let inner = inner.clone();
            let hash = hash.clone();
            async move { inner.get_drv(&hash).await }
        })
        .await
    }

    async fn put_drv(&self, drv: &Drv) -> Result<(), CacheError> {
        let inner = self.inner.clone();
        let drv = drv.clone();
        self.retry(|| {
            let inner = inner.clone();
            let drv = drv.clone();
            async move { inner.put_drv(&drv).await }
        })
        .await
    }

    async fn get_nar(&self, hash: &NarHash) -> Result<Option<NarBlob>, CacheError> {
        let inner = self.inner.clone();
        let hash = hash.clone();
        self.retry(|| {
            let inner = inner.clone();
            let hash = hash.clone();
            async move { inner.get_nar(&hash).await }
        })
        .await
    }

    async fn put_nar(&self, blob: &NarBlob) -> Result<(), CacheError> {
        let inner = self.inner.clone();
        let blob = blob.clone();
        self.retry(|| {
            let inner = inner.clone();
            let blob = blob.clone();
            async move { inner.put_nar(&blob).await }
        })
        .await
    }

    async fn list_realisations(&self, drv_hash: &DrvHash) -> Result<Vec<Realisation>, CacheError> {
        let inner = self.inner.clone();
        let drv_hash = drv_hash.clone();
        self.retry(|| {
            let inner = inner.clone();
            let drv_hash = drv_hash.clone();
            async move { inner.list_realisations(&drv_hash).await }
        })
        .await
    }

    async fn put_realisation(&self, realisation: &Realisation) -> Result<(), CacheError> {
        let inner = self.inner.clone();
        let r = realisation.clone();
        self.retry(|| {
            let inner = inner.clone();
            let r = r.clone();
            async move { inner.put_realisation(&r).await }
        })
        .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::derivation::MemoryDerivationCache;
    use std::sync::atomic::{AtomicU32, Ordering};
    use tokio::sync::Mutex;

    /// Backend that fails N times then succeeds.
    struct FlakyBackend {
        inner: MemoryDerivationCache,
        fail_remaining: AtomicU32,
        calls: Mutex<u32>,
    }

    impl FlakyBackend {
        fn new(fail_for_n_calls: u32) -> Self {
            Self {
                inner: MemoryDerivationCache::new(),
                fail_remaining: AtomicU32::new(fail_for_n_calls),
                calls: Mutex::new(0),
            }
        }

        async fn maybe_fail(&self) -> Result<(), CacheError> {
            let mut c = self.calls.lock().await;
            *c += 1;
            drop(c);
            if self
                .fail_remaining
                .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |n| {
                    if n > 0 { Some(n - 1) } else { None }
                })
                .is_ok()
            {
                Err(CacheError::Backend("transient".into()))
            } else {
                Ok(())
            }
        }
    }

    #[async_trait]
    impl DerivationCacheBackend for FlakyBackend {
        fn name(&self) -> &'static str {
            "flaky"
        }
        async fn get_drv(&self, hash: &DrvHash) -> Result<Option<Drv>, CacheError> {
            self.maybe_fail().await?;
            self.inner.get_drv(hash).await
        }
        async fn put_drv(&self, drv: &Drv) -> Result<(), CacheError> {
            self.maybe_fail().await?;
            self.inner.put_drv(drv).await
        }
        async fn get_nar(&self, hash: &NarHash) -> Result<Option<NarBlob>, CacheError> {
            self.maybe_fail().await?;
            self.inner.get_nar(hash).await
        }
        async fn put_nar(&self, blob: &NarBlob) -> Result<(), CacheError> {
            self.maybe_fail().await?;
            self.inner.put_nar(blob).await
        }
        async fn list_realisations(
            &self,
            drv_hash: &DrvHash,
        ) -> Result<Vec<Realisation>, CacheError> {
            self.maybe_fail().await?;
            self.inner.list_realisations(drv_hash).await
        }
        async fn put_realisation(&self, realisation: &Realisation) -> Result<(), CacheError> {
            self.maybe_fail().await?;
            self.inner.put_realisation(realisation).await
        }
    }

    fn fast_backoff() -> BackoffConfig {
        BackoffConfig {
            max_attempts: 5,
            initial_delay: Duration::from_millis(1),
            multiplier: 1.0,
            max_delay: Duration::from_millis(5),
        }
    }

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

    #[tokio::test]
    async fn succeeds_on_first_attempt() {
        let inner = Arc::new(MemoryDerivationCache::new());
        let wrapper = RetryingCacheBackend::with_config(inner, fast_backoff());
        wrapper.put_drv(&sample_drv(b"x")).await.unwrap();
    }

    #[tokio::test]
    async fn recovers_from_two_transient_failures() {
        let inner = Arc::new(FlakyBackend::new(2));
        let wrapper = RetryingCacheBackend::with_config(inner.clone(), fast_backoff());
        wrapper.put_drv(&sample_drv(b"recov")).await.unwrap();
        assert_eq!(*inner.calls.lock().await, 3);
    }

    #[tokio::test]
    async fn returns_error_after_max_attempts() {
        let inner = Arc::new(FlakyBackend::new(10));
        let cfg = BackoffConfig {
            max_attempts: 3,
            ..fast_backoff()
        };
        let wrapper = RetryingCacheBackend::with_config(inner.clone(), cfg);
        let err = wrapper.put_drv(&sample_drv(b"give-up")).await.unwrap_err();
        assert_eq!(err.kind(), "backend");
        assert_eq!(*inner.calls.lock().await, 3);
    }

    #[tokio::test]
    async fn hash_mismatch_not_retried() {
        // Wrap a backend that returns HashMismatch immediately.
        struct CorruptBackend;
        #[async_trait]
        impl DerivationCacheBackend for CorruptBackend {
            fn name(&self) -> &'static str {
                "corrupt"
            }
            async fn get_drv(&self, _: &DrvHash) -> Result<Option<Drv>, CacheError> {
                Err(CacheError::HashMismatch {
                    requested: "a".into(),
                    actual: "b".into(),
                })
            }
            async fn put_drv(&self, _: &Drv) -> Result<(), CacheError> {
                Ok(())
            }
            async fn get_nar(&self, _: &NarHash) -> Result<Option<NarBlob>, CacheError> {
                Ok(None)
            }
            async fn put_nar(&self, _: &NarBlob) -> Result<(), CacheError> {
                Ok(())
            }
            async fn list_realisations(&self, _: &DrvHash) -> Result<Vec<Realisation>, CacheError> {
                Ok(Vec::new())
            }
            async fn put_realisation(&self, _: &Realisation) -> Result<(), CacheError> {
                Ok(())
            }
        }
        let wrapper = RetryingCacheBackend::with_config(Arc::new(CorruptBackend), fast_backoff());
        let err = wrapper
            .get_drv(&DrvHash::from_bytes(b"x"))
            .await
            .unwrap_err();
        assert_eq!(err.kind(), "hash_mismatch");
        // Note: we don't assert call count = 1 because there's no
        // counter on CorruptBackend. The structural-error property
        // is verified by the kind comparison.
    }

    #[test]
    fn no_retry_config_has_max_attempts_one() {
        let cfg = BackoffConfig::no_retry();
        assert_eq!(cfg.max_attempts, 1);
    }

    #[test]
    fn delay_for_first_attempt_is_zero() {
        let cfg = BackoffConfig::default();
        assert_eq!(cfg.delay_for(1), Duration::ZERO);
    }

    #[test]
    fn delay_for_second_attempt_equals_initial() {
        let cfg = BackoffConfig::default();
        assert_eq!(cfg.delay_for(2), cfg.initial_delay);
    }

    #[test]
    fn delay_for_third_attempt_doubles_at_default_multiplier() {
        let cfg = BackoffConfig::default(); // multiplier = 2.0
        assert_eq!(cfg.delay_for(3), cfg.initial_delay * 2);
    }

    #[test]
    fn delay_caps_at_max_delay() {
        let cfg = BackoffConfig {
            max_attempts: 100,
            initial_delay: Duration::from_secs(1),
            multiplier: 10.0,
            max_delay: Duration::from_secs(2),
        };
        assert_eq!(cfg.delay_for(20), Duration::from_secs(2));
    }

    #[test]
    fn is_retryable_only_for_backend_errors() {
        assert!(RetryingCacheBackend::is_retryable(&CacheError::Backend(
            "x".into()
        )));
        assert!(!RetryingCacheBackend::is_retryable(
            &CacheError::HashMismatch {
                requested: "a".into(),
                actual: "b".into(),
            }
        ));
    }

    #[tokio::test]
    async fn backend_name_is_stable() {
        let inner = Arc::new(MemoryDerivationCache::new());
        let wrapper = RetryingCacheBackend::new(inner);
        assert_eq!(wrapper.name(), "retrying");
    }
}