acdp-client 0.15.0

Consumer client (RegistryClient, VerifiedContext, CrossRegistryResolver) for the Agent Context Distribution Protocol (ACDP)
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
//! Issue #257: a cache for RFC-ACDP-0014 §7/§8 revocation discovery.
//!
//! This is deliberately **two objects sharing one lock**, not one:
//!
//! - **Facts** — verified [`KeyRevocation`]s. RFC-ACDP-0014 §7:114 licenses
//!   caching these *indefinitely* ("the statement is permanent"). They are
//!   **always unioned** into classification by
//!   [`crate::verified::VerifiedContext`]'s internal `verify_retrieved` —
//!   never used to replace or subset a discovery result. A revocation is
//!   monotone (more revocations ⇒ an earlier effective boundary ⇒ strictly
//!   more fail-closed verdicts), so seeding from the fact store can only
//!   *tighten* a verdict, never loosen one. That makes the fact store an
//!   **anti-rollback security control**: without it, a registry that serves
//!   a revocation on one call and hides it on the next causes the client to
//!   forget it ever saw it. See `RevocationCache::facts_for` (read) and
//!   `RevocationCache::record_success` (write) — both crate-private.
//!
//!   **Every stored fact carries the vantage that minted it** (its
//!   `origin`, set from `record_success`'s `authority` parameter — see
//!   `StoredFact`). RFC-ACDP-0014 §6 draws a real distinction here: a
//!   *producer-signed* fact is self-contained and "verifies identically
//!   wherever it came from" (§8), so it is never filtered by origin — this
//!   is what makes it correct for it to cross vantages (see
//!   `crate::verified`'s anti-rollback tests). A *registry-attested* fact is
//!   one specific registry's claim, and §6 licenses applying it only "for
//!   contexts served by or receipted by that same registry" — so a read
//!   filters registry-attested facts to `origin == current vantage`.
//!   Storing only `trust_class` (as an earlier revision of this cache did)
//!   answers a different question than "was this claim made by the
//!   registry now serving this context" — collapsing the two let a fact
//!   minted at a hostile or deceived registry A fail-close a producer's
//!   contexts at every OTHER authority, for the cache's lifetime: a
//!   targeted cross-registry DoS exactly bounded by §6's scoping default.
//! - **Freshness markers** — "vantage V was asked about producer/controller
//!   P for trust class C, and a full, untruncated discovery completed at
//!   time T." This is a cached *absence*, which §7:114 does **not** license
//!   and which §8 explicitly warns about: "a malicious registry can hide a
//!   revocation … absence of search results is not evidence of absence." So
//!   a marker is TTL-bounded ([`crate::verified::RevocationDiscovery::freshness`],
//!   default 300 s — RFC-ACDP-0014 §8's RECOMMENDED bound), per-vantage,
//!   per-trust-class, and read via `RevocationCache::fresh_marker`
//!   (crate-private) — the only
//!   method in this module that can cause a lookup to be skipped rather
//!   than merely supplemented.
//!
//! **The two objects are stored separately and have different lifetimes**
//! (0.15.0, RFC-ACDP-0014 §8 "Caching", 0.5.0):
//!
//! - Facts are **never evicted**. A fact can only leave the cache with the
//!   cache itself. Admission is capped per producer
//!   ([`MAX_FACTS_PER_CLASS`] per trust class, and per origin for the
//!   registry-attested class); a fact refused at the cap still applies to
//!   the call that found it, and the capped slot stops minting markers so
//!   that lookup runs in full on every later call rather than claiming a
//!   completeness the cache can no longer track.
//! - Markers live in a bounded map ([`MAX_MARKERS`]). Losing a marker only
//!   costs a re-discovery, so the oldest is evicted at capacity. Each one
//!   records the wall-clock time the absence was observed, which the
//!   verdict reports (`RevocationVerdict::NotRevoked::observed_at`).
//!
//! Only a complete, untruncated lookup mints a marker. Every `unknown`
//! outcome (truncated, unanswerable, transient, unverifiable candidate)
//! returns `Err` before `record_success` is reached, so `unknown` is never
//! cached as not-revoked.
//!
//! **Never call anything here holding the lock across an `.await`** — every
//! method in this module is synchronous and returns before its caller does
//! any network I/O.

use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::{Duration, Instant, SystemTime};

use acdp_types::revocation::{KeyRevocation, RevocationTrustClass};

/// Bound on the number of freshness markers held. Markers are a pure
/// optimization, so at capacity the oldest one is evicted.
pub const MAX_MARKERS: usize = 4096;

/// Admission cap on stored facts per producer for the producer-signed
/// class, and per `(producer, origin)` for the registry-attested class.
/// Reached only by a producer with a genuinely large revocation history, or
/// a registry padding results. Stored facts are never removed; a fact
/// refused here still applies to the call that found it, and the slot stops
/// minting markers.
pub const MAX_FACTS_PER_CLASS: usize = 256;

/// A cached, verified [`KeyRevocation`] plus the vantage that minted it.
///
/// The origin is consulted only for
/// [`RevocationTrustClass::RegistryAttested`] facts — `facts_for` filters
/// those to `origin == current vantage`, per RFC-ACDP-0014 §6 ("apply it
/// ... for contexts served by or receipted by that same registry"). A
/// [`RevocationTrustClass::ProducerSigned`] fact is self-contained (§8) and
/// is never filtered by origin.
#[derive(Clone)]
struct StoredFact {
    rev: KeyRevocation,
    /// The [`crate::RegistryClient::authority`] whose complete discovery
    /// stored this fact.
    origin: String,
}

/// One producer's verified facts. Never evicted.
#[derive(Default)]
struct FactSet {
    facts: Vec<StoredFact>,
    /// The producer-signed slot reached [`MAX_FACTS_PER_CLASS`].
    producer_signed_full: bool,
    /// Origins whose registry-attested slot reached [`MAX_FACTS_PER_CLASS`].
    attested_full: HashSet<String>,
}

impl FactSet {
    fn count(&self, class: RevocationTrustClass, origin: &str) -> usize {
        self.facts
            .iter()
            .filter(|f| {
                f.rev.trust_class == class
                    && (class == RevocationTrustClass::ProducerSigned || f.origin == origin)
            })
            .count()
    }

    fn full(&self, class: RevocationTrustClass, origin: &str) -> bool {
        match class {
            RevocationTrustClass::ProducerSigned => self.producer_signed_full,
            RevocationTrustClass::RegistryAttested => self.attested_full.contains(origin),
        }
    }

    fn mark_full(&mut self, class: RevocationTrustClass, origin: &str) {
        match class {
            RevocationTrustClass::ProducerSigned => self.producer_signed_full = true,
            RevocationTrustClass::RegistryAttested => {
                self.attested_full.insert(origin.to_string());
            }
        }
    }
}

/// `(agent_id, authority, is_registry_attested)`. Never keyed by the search
/// identity the registry-attested lookup uses internally (that would let
/// one marker suppress discovery for every producer at that registry).
type MarkerKey = (String, String, bool);

/// A cached complete absence: when it was observed, on both clocks.
#[derive(Clone, Copy)]
struct Marker {
    /// Monotonic — drives the freshness comparison.
    minted: Instant,
    /// Wall clock — reported as the verdict's `observed_at`.
    observed: SystemTime,
}

#[derive(Default)]
struct Inner {
    facts: HashMap<String, FactSet>,
    markers: HashMap<MarkerKey, Marker>,
}

/// N5: an exhaustive `match`, not `matches!`, so a future third
/// `RevocationTrustClass` variant fails to compile here instead of
/// silently aliasing onto the `ProducerSigned` marker bucket.
fn class_key(class: RevocationTrustClass) -> bool {
    match class {
        RevocationTrustClass::ProducerSigned => false,
        RevocationTrustClass::RegistryAttested => true,
    }
}

/// A cache of RFC-ACDP-0014 revocation facts and discovery-freshness
/// markers, injectable into a [`crate::RegistryClient`] via
/// [`crate::RegistryClient::with_revocation_cache`].
///
/// `Clone`, cheap (an `Arc` handle). Sharing one clone across multiple
/// `RegistryClient`s (or a `CrossRegistryResolver`'s per-authority clients)
/// is the point: it is how a caller amortizes discovery across many
/// verifications of the same producer.
///
/// Facts (verified revocations) are held for the cache's lifetime and are
/// always unioned into classification. Markers (complete absences) suppress
/// a lookup only while younger than
/// [`RevocationDiscovery::freshness`](crate::RevocationDiscovery::freshness)
/// — 300 s from both named constructors
/// ([`crate::RECOMMENDED_NEGATIVE_FRESHNESS`]), `Duration::ZERO` to never
/// suppress.
#[derive(Clone)]
pub struct RevocationCache {
    inner: Arc<Mutex<Inner>>,
}

impl RevocationCache {
    /// A new, empty cache.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: Arc::new(Mutex::new(Inner::default())),
        }
    }

    /// Recover from a poisoned lock rather than propagate the panic. Used
    /// only by the fact read/write paths, where recovering can at worst
    /// tighten a verdict or cost an extra write — never skip a lookup.
    fn lock(&self) -> MutexGuard<'_, Inner> {
        self.inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    /// Read cached facts for `agent_id`, filtered to the trust classes the
    /// CURRENT discovery configuration opted into, with the vantage each
    /// was first verified at. A registry-attested fact is additionally
    /// filtered to `origin == current_vantage` (§6); `current_vantage:
    /// None` excludes every registry-attested fact. A producer-signed fact
    /// is never filtered by origin (§8).
    pub(crate) fn facts_for(
        &self,
        agent_id: &str,
        include_registry_attested: bool,
        current_vantage: Option<&str>,
    ) -> Vec<(KeyRevocation, String)> {
        let inner = self.lock();
        let Some(set) = inner.facts.get(agent_id) else {
            return Vec::new();
        };
        set.facts
            .iter()
            .filter(|f| match f.rev.trust_class {
                RevocationTrustClass::ProducerSigned => true,
                RevocationTrustClass::RegistryAttested => {
                    include_registry_attested && current_vantage.is_some_and(|v| v == f.origin)
                }
            })
            .map(|f| (f.rev.clone(), f.origin.clone()))
            .collect()
    }

    /// When a marker for `(authority, agent_id, class)` is younger than
    /// `freshness`, the wall-clock time its absence was observed; else
    /// `None`. The only method that can cause a lookup to be skipped.
    /// `freshness == Duration::ZERO` never touches the lock.
    ///
    /// A poisoned lock returns `None` (run discovery) rather than recover
    /// a marker present at poison time.
    pub(crate) fn fresh_marker(
        &self,
        authority: &str,
        agent_id: &str,
        class: RevocationTrustClass,
        freshness: Duration,
    ) -> Option<SystemTime> {
        if freshness.is_zero() {
            return None;
        }
        let inner = self.inner.lock().ok()?;
        let key = (
            agent_id.to_string(),
            authority.to_string(),
            class_key(class),
        );
        inner
            .markers
            .get(&key)
            .filter(|m| m.minted.elapsed() < freshness)
            .map(|m| m.observed)
    }

    /// Whether a fresh marker exists — `fresh_marker(..).is_some()`.
    #[cfg(test)]
    fn marker_fresh(
        &self,
        authority: &str,
        agent_id: &str,
        class: RevocationTrustClass,
        freshness: Duration,
    ) -> bool {
        self.fresh_marker(authority, agent_id, class, freshness)
            .is_some()
    }

    /// Record a complete, untruncated discovery lookup: admit new `facts`
    /// (dedup via `KeyRevocation`'s `Eq`) and, unless this slot is at its
    /// admission cap, mint a marker for `(authority, agent_id, class)`.
    ///
    /// Called ONLY from the tail of `find_revocations` /
    /// `find_registry_attested_revocations`, immediately before each
    /// returns `Ok` — a truncated, failed, or otherwise `unknown` lookup
    /// returns early and never reaches here, so it can neither mint a
    /// marker nor lose a fact.
    pub(crate) fn record_success(
        &self,
        authority: &str,
        agent_id: &str,
        class: RevocationTrustClass,
        facts: &[KeyRevocation],
    ) {
        let mut inner = self.lock();
        let set = inner.facts.entry(agent_id.to_string()).or_default();
        for f in facts {
            // Dedup BEFORE the cap: re-finding a known fact must never trip
            // the cap.
            if set.facts.iter().any(|existing| existing.rev == *f) {
                continue;
            }
            let fact_origin = authority;
            if set.count(f.trust_class, fact_origin) >= MAX_FACTS_PER_CLASS {
                set.mark_full(f.trust_class, fact_origin);
                continue;
            }
            set.facts.push(StoredFact {
                rev: f.clone(),
                origin: fact_origin.to_string(),
            });
        }
        let full = set.full(class, authority);
        let key = (
            agent_id.to_string(),
            authority.to_string(),
            class_key(class),
        );
        if full {
            inner.markers.remove(&key);
            return;
        }
        if !inner.markers.contains_key(&key) && inner.markers.len() >= MAX_MARKERS {
            if let Some(oldest) = inner
                .markers
                .iter()
                .min_by_key(|(_, m)| m.minted)
                .map(|(k, _)| k.clone())
            {
                inner.markers.remove(&oldest);
            }
        }
        inner.markers.insert(
            key,
            Marker {
                minted: Instant::now(),
                observed: SystemTime::now(),
            },
        );
    }

    /// Test-only introspection: the number of facts stored for `agent_id`,
    /// unfiltered by trust class or origin.
    #[doc(hidden)]
    #[cfg(feature = "test-transport")]
    #[must_use]
    pub fn fact_count(&self, agent_id: &str) -> usize {
        self.lock().facts.get(agent_id).map_or(0, |s| s.facts.len())
    }
}

impl Default for RevocationCache {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for RevocationCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RevocationCache").finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{DateTime, Utc};

    fn rev(fp: &str, class: RevocationTrustClass) -> KeyRevocation {
        KeyRevocation {
            revoked_key_fingerprint: fp.into(),
            compromised_since: "2026-05-01T00:00:00.000Z".parse::<DateTime<Utc>>().unwrap(),
            reason: None,
            revoked_key_id: None,
            revoked_key_controller: acdp_types::primitives::AgentDid::new(
                "did:web:agents.example.com:p",
            ),
            publisher: acdp_types::primitives::AgentDid::new("did:web:agents.example.com:p"),
            trust_class: class,
            ctx_id: None,
        }
    }

    /// AC4 (dedup): recording the same fact repeatedly does not grow the
    /// entry.
    #[test]
    fn record_success_dedups_identical_facts() {
        let cache = RevocationCache::new();
        let r = rev("sha256:aaaa", RevocationTrustClass::ProducerSigned);
        for _ in 0..5 {
            cache.record_success(
                "reg.example",
                "did:web:p",
                RevocationTrustClass::ProducerSigned,
                std::slice::from_ref(&r),
            );
        }
        assert_eq!(
            cache
                .facts_for("did:web:p", true, Some("reg.example"))
                .len(),
            1
        );
    }

    /// Admission cap: once a producer-signed slot is full, new distinct
    /// facts are not admitted, already-stored ones stay, and the slot
    /// stops minting markers (never a fresh marker over a set that stopped
    /// growing).
    #[test]
    fn record_success_caps_admission_and_drops_markers_at_cap() {
        let cache = RevocationCache::new();
        for i in 0..(MAX_FACTS_PER_CLASS + 5) {
            let fp = format!("sha256:{i:064x}");
            let r = rev(&fp, RevocationTrustClass::ProducerSigned);
            cache.record_success(
                "reg.example",
                "did:web:p",
                RevocationTrustClass::ProducerSigned,
                &[r],
            );
        }
        assert_eq!(
            cache
                .facts_for("did:web:p", true, Some("reg.example"))
                .len(),
            MAX_FACTS_PER_CLASS
        );
        assert!(!cache.marker_fresh(
            "reg.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            Duration::from_secs(3600)
        ));
    }

    /// A full registry-attested slot at one origin does not stop markers
    /// for the producer-signed class or for another origin.
    #[test]
    fn attested_cap_is_per_origin() {
        let cache = RevocationCache::new();
        for i in 0..=MAX_FACTS_PER_CLASS {
            let fp = format!("sha256:{i:064x}");
            cache.record_success(
                "a.example",
                "did:web:p",
                RevocationTrustClass::RegistryAttested,
                &[rev(&fp, RevocationTrustClass::RegistryAttested)],
            );
        }
        let fresh = |auth: &str, class| {
            cache.marker_fresh(auth, "did:web:p", class, Duration::from_secs(3600))
        };
        assert!(!fresh("a.example", RevocationTrustClass::RegistryAttested));
        cache.record_success(
            "b.example",
            "did:web:p",
            RevocationTrustClass::RegistryAttested,
            &[],
        );
        cache.record_success(
            "a.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            &[],
        );
        assert!(fresh("b.example", RevocationTrustClass::RegistryAttested));
        assert!(fresh("a.example", RevocationTrustClass::ProducerSigned));
    }

    /// `facts_for` filters registry-attested facts out for a
    /// producer-signed-only caller, and in for an all-trust-classes one
    /// reading from the SAME vantage that minted the fact.
    #[test]
    fn facts_for_filters_by_trust_class() {
        let cache = RevocationCache::new();
        let attested = rev("sha256:bbbb", RevocationTrustClass::RegistryAttested);
        cache.record_success(
            "reg.example",
            "did:web:p",
            RevocationTrustClass::RegistryAttested,
            &[attested],
        );
        assert!(cache
            .facts_for("did:web:p", false, Some("reg.example"))
            .is_empty());
        assert_eq!(
            cache
                .facts_for("did:web:p", true, Some("reg.example"))
                .len(),
            1
        );
    }

    /// BLOCKER-1: a registry-attested fact minted at vantage A must NOT be
    /// returned when reading at a DIFFERENT vantage B, even under
    /// `include_registry_attested: true` — RFC-ACDP-0014 §6 scopes a
    /// registry-attested claim to the registry that made it. `None` (no
    /// resolvable vantage on the reading call) must behave the same as a
    /// mismatched vantage: exclude the fact, never include it.
    #[test]
    fn facts_for_filters_registry_attested_facts_by_origin() {
        let cache = RevocationCache::new();
        let attested = rev("sha256:cccc", RevocationTrustClass::RegistryAttested);
        cache.record_success(
            "a.example",
            "did:web:p",
            RevocationTrustClass::RegistryAttested,
            &[attested],
        );
        assert_eq!(
            cache.facts_for("did:web:p", true, Some("a.example")).len(),
            1,
            "reading from the SAME vantage that minted the fact must include it"
        );
        assert!(
            cache
                .facts_for("did:web:p", true, Some("b.example"))
                .is_empty(),
            "reading from a DIFFERENT vantage must exclude a registry-attested fact"
        );
        assert!(
            cache.facts_for("did:web:p", true, None).is_empty(),
            "reading with no resolvable current vantage must exclude a registry-attested \
             fact, not include it by default"
        );
    }

    /// BLOCKER-1's other direction: a producer-signed fact is
    /// self-contained (RFC-ACDP-0014 §8) and is NEVER filtered by origin —
    /// it must be returned when read from a vantage other than the one
    /// that minted it.
    #[test]
    fn facts_for_never_filters_producer_signed_facts_by_origin() {
        let cache = RevocationCache::new();
        let signed = rev("sha256:dddd", RevocationTrustClass::ProducerSigned);
        cache.record_success(
            "a.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            &[signed],
        );
        assert_eq!(
            cache.facts_for("did:web:p", false, Some("b.example")).len(),
            1
        );
        assert_eq!(cache.facts_for("did:web:p", false, None).len(), 1);
    }

    /// Markers are per-vantage: minting one for authority A does not make
    /// `marker_fresh` true for authority B.
    #[test]
    fn markers_are_per_vantage() {
        let cache = RevocationCache::new();
        cache.record_success(
            "a.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            &[],
        );
        assert!(cache.marker_fresh(
            "a.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            Duration::from_secs(60)
        ));
        assert!(!cache.marker_fresh(
            "b.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            Duration::from_secs(60)
        ));
    }

    /// Markers are per trust class too.
    #[test]
    fn markers_are_per_trust_class() {
        let cache = RevocationCache::new();
        cache.record_success(
            "a.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            &[],
        );
        assert!(!cache.marker_fresh(
            "a.example",
            "did:web:p",
            RevocationTrustClass::RegistryAttested,
            Duration::from_secs(60)
        ));
    }

    /// `freshness: ZERO` never reports a marker as fresh, regardless of how
    /// recently it was minted.
    #[test]
    fn zero_freshness_never_suppresses() {
        let cache = RevocationCache::new();
        cache.record_success(
            "a.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            &[],
        );
        assert!(!cache.marker_fresh(
            "a.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            Duration::ZERO
        ));
    }

    /// RFC-ACDP-0014 §8 "Caching" (0.5.0): verified revocations are
    /// retained for the cache's lifetime — marker pressure evicts markers
    /// only, never a fact, and facts are stored separately from markers.
    #[test]
    fn facts_are_never_evicted_by_marker_pressure() {
        let cache = RevocationCache::new();
        let revoked = rev(
            "sha256:aaaa000000000000000000000000000000000000000000000000000000000000",
            RevocationTrustClass::ProducerSigned,
        );
        cache.record_success(
            "reg.example",
            "did:web:revoked",
            RevocationTrustClass::ProducerSigned,
            std::slice::from_ref(&revoked),
        );
        // Far more distinct producers than the marker bound.
        for i in 0..(MAX_MARKERS * 2) {
            cache.record_success(
                "reg.example",
                &format!("did:web:p{i}"),
                RevocationTrustClass::ProducerSigned,
                &[],
            );
        }
        let facts = cache.facts_for("did:web:revoked", false, None);
        assert_eq!(facts.len(), 1, "the verified revocation must survive");
        assert_eq!(facts[0].0, revoked);
        assert_eq!(facts[0].1, "reg.example");
        // Its marker was the oldest, so it went — the absence is
        // re-discovered, the fact is not forgotten.
        assert!(!cache.marker_fresh(
            "reg.example",
            "did:web:revoked",
            RevocationTrustClass::ProducerSigned,
            Duration::from_secs(3600)
        ));
        assert!(cache.lock().markers.len() <= MAX_MARKERS);
    }

    /// Marker eviction removes the oldest marker.
    #[test]
    fn marker_eviction_removes_oldest() {
        let cache = RevocationCache::new();
        for i in 0..=MAX_MARKERS {
            cache.record_success(
                "reg.example",
                &format!("did:web:p{i}"),
                RevocationTrustClass::ProducerSigned,
                &[],
            );
        }
        let fresh = |agent: &str| {
            cache.marker_fresh(
                "reg.example",
                agent,
                RevocationTrustClass::ProducerSigned,
                Duration::from_secs(3600),
            )
        };
        assert!(!fresh("did:web:p0"));
        assert!(fresh("did:web:p1"));
        assert!(fresh(&format!("did:web:p{MAX_MARKERS}")));
    }

    /// The observed wall-clock time is reported with a fresh marker.
    #[test]
    fn fresh_marker_reports_observed_time() {
        let cache = RevocationCache::new();
        let before = SystemTime::now();
        cache.record_success(
            "a.example",
            "did:web:p",
            RevocationTrustClass::ProducerSigned,
            &[],
        );
        let after = SystemTime::now();
        let observed = cache
            .fresh_marker(
                "a.example",
                "did:web:p",
                RevocationTrustClass::ProducerSigned,
                Duration::from_secs(60),
            )
            .expect("fresh");
        assert!(observed >= before && observed <= after);
    }
}