1use std::collections::HashMap;
30use std::sync::Mutex;
31use std::time::{Duration, Instant};
32
33use firstpass_core::{ServedFrom, Trace, Verdict};
34
35use crate::provider::ModelResponse;
36
37#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
39pub struct Entry {
40 pub response: ModelResponse,
47 pub source: uuid::Uuid,
49 pub original_cost_usd: f64,
51 #[serde(skip, default = "Instant::now")]
57 stored: Instant,
58}
59
60#[must_use]
77pub fn is_cacheable(trace: &Trace) -> bool {
78 if trace.final_.served_from != ServedFrom::Attempt {
79 return false;
80 }
81 if trace.final_.cache_source.is_some() {
82 return false;
83 }
84 let Some(served) = trace.final_.served_rung else {
85 return false;
86 };
87 if !trace
88 .attempts
89 .iter()
90 .any(|a| a.rung == served && a.verdict == Verdict::Pass)
91 {
92 return false;
93 }
94 !trace.deferred.iter().any(|d| d.verdict == Verdict::Fail)
95}
96
97impl Entry {
98 #[must_use]
103 pub fn new(response: ModelResponse, trace: &Trace, now: Instant) -> Self {
104 Self {
105 response,
106 source: trace.trace_id,
107 original_cost_usd: trace.final_.total_cost_usd,
108 stored: now,
109 }
110 }
111}
112
113fn key(tenant: &str, prompt_hash: &str, ladder: &[String]) -> String {
121 format!("{tenant}|{prompt_hash}|{}", ladder.join(">"))
122}
123
124#[async_trait::async_trait]
134pub trait CacheStore: Send + Sync + std::fmt::Debug {
135 async fn get(&self, key: &str, now: Instant) -> Option<Entry>;
137 async fn put(&self, key: &str, source: uuid::Uuid, entry: Entry, now: Instant);
143 async fn retract(&self, source: uuid::Uuid) -> usize;
145 async fn entry_count(&self) -> usize;
147}
148
149#[derive(Debug)]
151pub struct VerifiedCache {
152 entries: Mutex<HashMap<String, Entry>>,
153 ttl: Duration,
154 max_entries: usize,
155}
156
157impl VerifiedCache {
158 #[must_use]
160 pub fn new(ttl: Duration, max_entries: usize) -> Self {
161 Self {
162 entries: Mutex::new(HashMap::new()),
163 ttl,
164 max_entries,
165 }
166 }
167
168 #[must_use]
170 pub fn get(
171 &self,
172 tenant: &str,
173 prompt_hash: &str,
174 ladder: &[String],
175 now: Instant,
176 ) -> Option<Entry> {
177 let Ok(map) = self.entries.lock() else {
178 return None;
181 };
182 map.get(&key(tenant, prompt_hash, ladder))
183 .filter(|e| now.duration_since(e.stored) < self.ttl)
184 .cloned()
185 }
186
187 pub fn offer(
193 &self,
194 trace: &Trace,
195 response: &ModelResponse,
196 ladder: &[String],
197 now: Instant,
198 ) -> bool {
199 if !is_cacheable(trace) {
200 return false;
201 }
202 let mut entry = Entry {
203 response: response.clone(),
204 source: trace.trace_id,
205 original_cost_usd: trace.final_.total_cost_usd,
206 stored: now,
207 };
208 entry.stored = now;
209 self.put_by_key(
210 &key(&trace.tenant_id, &trace.request.prompt_hash, ladder),
211 entry,
212 );
213 true
214 }
215
216 #[must_use]
219 pub fn get_by_key(&self, key: &str, now: Instant) -> Option<Entry> {
220 let Ok(map) = self.entries.lock() else {
221 return None;
222 };
223 map.get(key)
224 .filter(|e| now.duration_since(e.stored) < self.ttl)
225 .cloned()
226 }
227
228 pub fn put_by_key(&self, key: &str, entry: Entry) {
230 let Ok(mut map) = self.entries.lock() else {
231 return;
232 };
233 let now = entry.stored;
234 if map.len() >= self.max_entries {
235 map.retain(|_, e| now.duration_since(e.stored) < self.ttl);
236 if map.len() >= self.max_entries {
237 let mut by_age: Vec<(String, Instant)> =
238 map.iter().map(|(k, e)| (k.clone(), e.stored)).collect();
239 by_age.sort_by_key(|(_, t)| *t);
240 for (k, _) in by_age.into_iter().take(self.max_entries.max(2) / 2) {
241 map.remove(&k);
242 }
243 }
244 }
245 map.insert(key.to_owned(), entry);
246 }
247
248 #[must_use]
250 pub fn compose_key(tenant: &str, prompt_hash: &str, ladder: &[String]) -> String {
251 key(tenant, prompt_hash, ladder)
252 }
253
254 pub fn retract(&self, source: uuid::Uuid) -> usize {
265 let Ok(mut map) = self.entries.lock() else {
266 return 0;
267 };
268 let before = map.len();
269 map.retain(|_, e| e.source != source);
270 before - map.len()
271 }
272
273 #[must_use]
275 pub fn len(&self) -> usize {
276 self.entries.lock().map(|m| m.len()).unwrap_or(0)
277 }
278
279 #[must_use]
281 pub fn is_empty(&self) -> bool {
282 self.len() == 0
283 }
284}
285
286#[async_trait::async_trait]
287impl CacheStore for VerifiedCache {
288 async fn get(&self, key: &str, now: Instant) -> Option<Entry> {
292 self.get_by_key(key, now)
293 }
294
295 async fn put(&self, key: &str, _source: uuid::Uuid, entry: Entry, _now: Instant) {
296 self.put_by_key(key, entry);
297 }
298
299 async fn retract(&self, source: uuid::Uuid) -> usize {
300 Self::retract(self, source)
301 }
302
303 async fn entry_count(&self) -> usize {
304 Self::len(self)
305 }
306}
307
308#[cfg(feature = "redis-cache")]
329pub struct RedisStore {
330 client: redis::aio::ConnectionManager,
331 ttl_secs: u64,
332}
333
334#[cfg(feature = "redis-cache")]
337impl std::fmt::Debug for RedisStore {
338 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
339 f.debug_struct("RedisStore")
340 .field("ttl_secs", &self.ttl_secs)
341 .finish_non_exhaustive()
342 }
343}
344
345#[cfg(feature = "redis-cache")]
351const CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
352
353#[cfg(feature = "redis-cache")]
360#[must_use]
361pub fn redact_redis_url(url: &str) -> String {
362 let Some((scheme, rest)) = url.split_once("://") else {
365 return "<malformed redis url>".to_owned();
366 };
367 let host_part = match rest.rfind('@') {
368 Some(i) => &rest[i + 1..],
369 None => rest,
370 };
371 format!("{scheme}://{host_part}")
372}
373
374#[cfg(feature = "redis-cache")]
375impl RedisStore {
376 pub async fn connect(url: &str, ttl_secs: u64) -> Result<Self, String> {
383 let safe = redact_redis_url(url);
384 let client = redis::Client::open(url).map_err(|e| format!("redis url {safe}: {e}"))?;
385 let mgr = tokio::time::timeout(CONNECT_TIMEOUT, redis::aio::ConnectionManager::new(client))
391 .await
392 .map_err(|_| {
393 format!(
394 "redis at {safe} did not answer within {}s. The proxy refuses to start rather \
395 than run with a cache that silently stays per-replica — check the server is \
396 reachable, or remove redis_url to use the in-process cache.",
397 CONNECT_TIMEOUT.as_secs()
398 )
399 })?
400 .map_err(|e| format!("redis connect {safe}: {e}"))?;
401
402 let mut probe = mgr.clone();
409 let pong: Result<String, _> = tokio::time::timeout(
410 CONNECT_TIMEOUT,
411 redis::cmd("PING").query_async(&mut probe),
412 )
413 .await
414 .map_err(|_| {
415 format!(
416 "redis at {safe} did not answer PING within {}s. Refusing to start rather than \
417 run with a cache that reports itself as shared and never works — check the \
418 server is reachable, or remove redis_url to use the in-process cache.",
419 CONNECT_TIMEOUT.as_secs()
420 )
421 })?;
422 pong.map_err(|e| format!("redis at {safe} rejected PING: {e}"))?;
423
424 Ok(Self {
425 client: mgr,
426 ttl_secs,
427 })
428 }
429
430 fn entry_key(key: &str) -> String {
431 format!("fp:vc:{key}")
432 }
433
434 fn source_key(source: uuid::Uuid) -> String {
435 format!("fp:vc:src:{source}")
436 }
437}
438
439#[cfg(feature = "redis-cache")]
440#[async_trait::async_trait]
441impl CacheStore for RedisStore {
442 async fn get(&self, key: &str, _now: Instant) -> Option<Entry> {
443 let mut c = self.client.clone();
444 let raw: Option<String> = redis::cmd("GET")
445 .arg(Self::entry_key(key))
446 .query_async(&mut c)
447 .await
448 .ok()?;
449 serde_json::from_str(&raw?).ok()
453 }
454
455 async fn put(&self, key: &str, source: uuid::Uuid, entry: Entry, _now: Instant) {
456 let Ok(body) = serde_json::to_string(&entry) else {
457 return;
458 };
459 let mut c = self.client.clone();
460 let ek = Self::entry_key(key);
461 let _: Result<(), _> = redis::pipe()
465 .cmd("SETEX")
466 .arg(&ek)
467 .arg(self.ttl_secs)
468 .arg(body)
469 .ignore()
470 .cmd("SADD")
471 .arg(Self::source_key(source))
472 .arg(&ek)
473 .ignore()
474 .cmd("EXPIRE")
475 .arg(Self::source_key(source))
476 .arg(self.ttl_secs)
477 .ignore()
478 .query_async::<()>(&mut c)
479 .await;
480 }
481
482 async fn retract(&self, source: uuid::Uuid) -> usize {
483 let mut c = self.client.clone();
484 let sk = Self::source_key(source);
485 let keys: Vec<String> = redis::cmd("SMEMBERS")
486 .arg(&sk)
487 .query_async(&mut c)
488 .await
489 .unwrap_or_default();
490 if keys.is_empty() {
491 return 0;
492 }
493 let n: usize = redis::cmd("DEL")
494 .arg(&keys)
495 .query_async(&mut c)
496 .await
497 .unwrap_or(0);
498 let _: Result<(), _> = redis::cmd("DEL").arg(&sk).query_async(&mut c).await;
499 n
500 }
501
502 async fn entry_count(&self) -> usize {
503 let mut c = self.client.clone();
504 let keys: Vec<String> = redis::cmd("KEYS")
505 .arg("fp:vc:*")
506 .query_async(&mut c)
507 .await
508 .unwrap_or_default();
509 keys.iter().filter(|k| !k.starts_with("fp:vc:src:")).count()
513 }
514}
515
516#[cfg(test)]
517mod tests {
518 use super::*;
519 use firstpass_core::{
520 Attempt, DeferredVerdict, Features, FinalOutcome, GENESIS_HASH, GateResult, Mode,
521 PolicyRef, RequestInfo, TaskKind,
522 };
523
524 fn resp(text: &str) -> ModelResponse {
525 ModelResponse {
526 model: "anthropic/claude-haiku-4-5".to_owned(),
527 text: text.to_owned(),
528 in_tokens: 10,
529 cache_write_tokens: 0,
530 cache_read_tokens: 0,
531 out_tokens: 5,
532 raw: serde_json::Value::Null,
533 }
534 }
535
536 fn ladder() -> Vec<String> {
537 vec!["anthropic/claude-haiku-4-5".to_owned()]
538 }
539
540 fn trace(served_from: ServedFrom, verdict: Verdict) -> Trace {
541 Trace {
542 trace_id: uuid::Uuid::now_v7(),
543 prev_hash: GENESIS_HASH.to_owned(),
544 tenant_id: "t".to_owned(),
545 session_id: "s".to_owned(),
546 ts: jiff::Timestamp::UNIX_EPOCH,
547 mode: Mode::Enforce,
548 policy: PolicyRef {
549 id: "static-ladder@v0".to_owned(),
550 explore: false,
551 propensity: None,
552 mode_profile: None,
553 },
554 request: RequestInfo {
555 api: "anthropic.messages".to_owned(),
556 prompt_hash: "hash-abc".to_owned(),
557 features: Features::new(TaskKind::CodeEdit),
558 },
559 attempts: vec![Attempt {
560 rung: 0,
561 model: "anthropic/claude-haiku-4-5".to_owned(),
562 provider: "anthropic".to_owned(),
563 in_tokens: 10,
564 cache_write_tokens: 0,
565 cache_read_tokens: 0,
566 out_tokens: 5,
567 cost_usd: 0.01,
568 latency_ms: 100,
569 gates: vec![GateResult::deterministic("non-empty", verdict, 1)],
570 verdict,
571 truncated: false,
572 }],
573 final_: FinalOutcome {
574 served_rung: Some(0),
575 served_from,
576 total_cost_usd: 0.01,
577 gate_cost_usd: 0.0,
578 total_latency_ms: 120,
579 escalations: 0,
580 counterfactual_baseline_usd: 0.05,
581 savings_usd: 0.04,
582 cache_source: None,
583 },
584 deferred: vec![],
585 predicted_pass: None,
586 decision_prior: None,
587 probe: None,
588 elastic: None,
589 risk: None,
590 rollout: None,
591 shadow: None,
592 route_ix: None,
593 experiment: None,
594 upstream_provider: String::new(),
595 }
596 }
597
598 #[test]
599 fn a_passing_answer_is_stored_and_replayed_with_its_proof() {
600 let c = VerifiedCache::new(Duration::from_secs(60), 100);
601 let now = Instant::now();
602 let t = trace(ServedFrom::Attempt, Verdict::Pass);
603
604 assert!(c.offer(&t, &resp("the answer"), &ladder(), now));
605
606 let hit = c.get("t", "hash-abc", &ladder(), now).expect("hit");
607 assert_eq!(hit.response.text, "the answer");
608 assert_eq!(hit.source, t.trace_id);
610 assert!((hit.original_cost_usd - 0.01).abs() < f64::EPSILON);
611 }
612
613 #[test]
614 fn a_failed_answer_is_never_cached() {
615 let c = VerifiedCache::new(Duration::from_secs(60), 100);
616 let now = Instant::now();
617
618 assert!(!c.offer(
619 &trace(ServedFrom::Attempt, Verdict::Fail),
620 &resp("bad"),
621 &ladder(),
622 now
623 ));
624
625 assert!(
626 c.get("t", "hash-abc", &ladder(), now).is_none(),
627 "must not be replayable"
628 );
629 }
630
631 #[test]
632 fn an_abstain_is_never_cached() {
633 let c = VerifiedCache::new(Duration::from_secs(60), 100);
636 let now = Instant::now();
637 assert!(!c.offer(
638 &trace(ServedFrom::Attempt, Verdict::Abstain),
639 &resp("?"),
640 &ladder(),
641 now
642 ));
643 assert!(c.get("t", "hash-abc", &ladder(), now).is_none());
644 }
645
646 #[test]
647 fn a_budget_exhausted_best_effort_serve_is_never_cached() {
648 let c = VerifiedCache::new(Duration::from_secs(60), 100);
651 let now = Instant::now();
652 assert!(!c.offer(
653 &trace(ServedFrom::BestAttempt, Verdict::Pass),
654 &resp("x"),
655 &ladder(),
656 now
657 ));
658 assert!(c.get("t", "hash-abc", &ladder(), now).is_none());
659 }
660
661 #[test]
662 fn a_later_failing_verdict_retracts_the_proof() {
663 let c = VerifiedCache::new(Duration::from_secs(60), 100);
666 let now = Instant::now();
667 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
668 t.deferred.push(DeferredVerdict {
669 gate_id: "tests".to_owned(),
670 verdict: Verdict::Fail,
671 score: None,
672 reported_at: jiff::Timestamp::UNIX_EPOCH,
673 reporter: "ci".to_owned(),
674 });
675
676 assert!(!c.offer(&t, &resp("looked fine"), &ladder(), now));
677 }
678
679 #[cfg(feature = "redis-cache")]
680 #[test]
681 fn a_redis_url_never_reaches_a_log_line_with_its_password() {
682 assert_eq!(
685 redact_redis_url("redis://user:hunter2@cache:6379/0"),
686 "redis://cache:6379/0"
687 );
688 assert_eq!(
690 redact_redis_url("redis://user:p@ss@cache:6379/0"),
691 "redis://cache:6379/0"
692 );
693 assert_eq!(
694 redact_redis_url("redis://cache:6379/0"),
695 "redis://cache:6379/0"
696 );
697 assert_eq!(redact_redis_url("rediss://u:p@h:6380"), "rediss://h:6380");
698 assert_eq!(redact_redis_url("nonsense"), "<malformed redis url>");
699
700 for u in [
701 "redis://user:hunter2@cache:6379/0",
702 "redis://user:p@ss@cache:6379/0",
703 ] {
704 let out = redact_redis_url(u);
705 assert!(!out.contains("hunter2"), "{out}");
706 assert!(!out.contains("p@ss"), "{out}");
707 assert!(!out.contains("user"), "{out}");
708 }
709 }
710
711 #[cfg(feature = "redis-cache")]
717 #[tokio::test]
718 async fn redis_store_round_trips_and_retracts() {
719 let url = std::env::var("FIRSTPASS_TEST_REDIS").unwrap_or_else(|_| {
720 panic!(
721 "set FIRSTPASS_TEST_REDIS (e.g. redis://127.0.0.1:6379/15) to run the redis-cache \
722 tests. Not skipped on purpose: a green suite that tested nothing is worse than a \
723 red one."
724 )
725 });
726 let store = RedisStore::connect(&url, 60).await.expect("connect");
727
728 let t = trace(ServedFrom::Attempt, Verdict::Pass);
729 let k = VerifiedCache::compose_key("t", &format!("h-{}", t.trace_id), &ladder());
730 let now = Instant::now();
731 let entry = Entry::new(resp("shared answer"), &t, now);
732
733 store.put(&k, t.trace_id, entry, now).await;
734 let got = store
735 .get(&k, now)
736 .await
737 .expect("entry crossed the boundary");
738 assert_eq!(got.response.text, "shared answer");
739 assert_eq!(got.source, t.trace_id);
741
742 assert_eq!(store.retract(t.trace_id).await, 1);
744 assert!(
745 store.get(&k, now).await.is_none(),
746 "a retracted answer must stop being replayable in the SHARED store too"
747 );
748 }
749
750 #[cfg(feature = "redis-cache")]
751 #[tokio::test]
752 async fn redis_store_isolates_tenants() {
753 let url = std::env::var("FIRSTPASS_TEST_REDIS")
755 .expect("set FIRSTPASS_TEST_REDIS to run the redis-cache tests");
756 let store = RedisStore::connect(&url, 60).await.expect("connect");
757
758 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
759 t.tenant_id = "tenant-a".to_owned();
760 let hash = format!("iso-{}", t.trace_id);
761 let ka = VerifiedCache::compose_key("tenant-a", &hash, &ladder());
762 let kb = VerifiedCache::compose_key("tenant-b", &hash, &ladder());
763 let now = Instant::now();
764
765 store
766 .put(&ka, t.trace_id, Entry::new(resp("a"), &t, now), now)
767 .await;
768
769 assert!(
770 store.get(&kb, now).await.is_none(),
771 "cross-tenant replay is a data leak"
772 );
773 assert!(store.get(&ka, now).await.is_some());
774 store.retract(t.trace_id).await;
775 }
776
777 #[tokio::test]
778 async fn the_trait_and_the_concrete_api_agree_on_keys() {
779 let c = VerifiedCache::new(Duration::from_secs(60), 100);
783 let now = Instant::now();
784 let t = trace(ServedFrom::Attempt, Verdict::Pass);
785 assert!(c.offer(&t, &resp("via offer"), &ladder(), now));
786
787 let k = VerifiedCache::compose_key("t", "hash-abc", &ladder());
788 let via_trait = CacheStore::get(&c, &k, now)
789 .await
790 .expect("trait sees offer's entry");
791 assert_eq!(via_trait.response.text, "via offer");
792 }
793
794 #[tokio::test]
795 async fn the_trait_path_still_isolates_tenants() {
796 let a = VerifiedCache::compose_key("tenant-a", "h", &ladder());
799 let b = VerifiedCache::compose_key("tenant-b", "h", &ladder());
800 assert_ne!(a, b, "tenant must be part of the key on the trait path too");
801 assert!(a.starts_with("tenant-a|"), "{a}");
802 }
803
804 #[tokio::test]
805 async fn an_entry_survives_a_serde_round_trip_without_its_local_clock() {
806 let e = Entry {
810 response: resp("answer"),
811 source: uuid::Uuid::now_v7(),
812 original_cost_usd: 0.01,
813 stored: Instant::now(),
814 };
815 let wire = serde_json::to_string(&e).expect("serializes");
816 assert!(
817 !wire.contains("stored"),
818 "a local Instant must not go on the wire: {wire}"
819 );
820
821 let back: Entry = serde_json::from_str(&wire).expect("deserializes");
822 assert_eq!(back.response.text, "answer");
823 assert_eq!(back.source, e.source, "the PROOF must survive the crossing");
824 assert!((back.original_cost_usd - 0.01).abs() < f64::EPSILON);
825 }
826
827 #[test]
828 fn a_retraction_evicts_the_answer_it_disproved() {
829 let c = VerifiedCache::new(Duration::from_secs(600), 100);
833 let now = Instant::now();
834 let t = trace(ServedFrom::Attempt, Verdict::Pass);
835 assert!(c.offer(&t, &resp("looked fine"), &ladder(), now));
836 assert!(c.get("t", "hash-abc", &ladder(), now).is_some());
837
838 assert_eq!(c.retract(t.trace_id), 1);
839
840 assert!(
841 c.get("t", "hash-abc", &ladder(), now).is_none(),
842 "an answer whose proof was retracted must stop being replayed immediately"
843 );
844 }
845
846 #[test]
847 fn retracting_an_unrelated_decision_evicts_nothing() {
848 let c = VerifiedCache::new(Duration::from_secs(600), 100);
849 let now = Instant::now();
850 c.offer(
851 &trace(ServedFrom::Attempt, Verdict::Pass),
852 &resp("a"),
853 &ladder(),
854 now,
855 );
856
857 assert_eq!(c.retract(uuid::Uuid::now_v7()), 0);
858 assert!(c.get("t", "hash-abc", &ladder(), now).is_some());
859 }
860
861 #[test]
862 fn a_cache_hit_is_not_itself_re_cached() {
863 let c = VerifiedCache::new(Duration::from_secs(60), 100);
866 let now = Instant::now();
867 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
868 t.final_.cache_source = Some(uuid::Uuid::now_v7());
869 assert!(!c.offer(&t, &resp("replayed"), &ladder(), now));
870 }
871
872 #[test]
873 fn one_tenant_can_never_replay_anothers_answer() {
874 let c = VerifiedCache::new(Duration::from_secs(60), 100);
879 let now = Instant::now();
880 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
881 t.tenant_id = "tenant-a".to_owned();
882 assert!(c.offer(&t, &resp("a's private answer"), &ladder(), now));
883
884 assert!(
885 c.get("tenant-b", "hash-abc", &ladder(), now).is_none(),
886 "cross-tenant replay — this is a data leak, not a cache miss"
887 );
888 assert!(
889 c.get("tenant-a", "hash-abc", &ladder(), now).is_some(),
890 "the owning tenant must still hit"
891 );
892 }
893
894 #[test]
895 fn the_cached_tenant_comes_from_the_receipt_not_the_caller() {
896 let c = VerifiedCache::new(Duration::from_secs(60), 100);
899 let now = Instant::now();
900 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
901 t.tenant_id = "owner".to_owned();
902 c.offer(&t, &resp("x"), &ladder(), now);
903
904 assert!(c.get("owner", "hash-abc", &ladder(), now).is_some());
905 assert!(c.get("t", "hash-abc", &ladder(), now).is_none());
906 }
907
908 #[test]
909 fn a_different_ladder_is_a_different_question() {
910 let c = VerifiedCache::new(Duration::from_secs(60), 100);
912 let now = Instant::now();
913 c.offer(
914 &trace(ServedFrom::Attempt, Verdict::Pass),
915 &resp("a"),
916 &ladder(),
917 now,
918 );
919
920 let other = vec!["openai/gpt-5.5".to_owned()];
921 assert!(c.get("t", "hash-abc", &other, now).is_none());
922 assert!(c.get("t", "hash-abc", &ladder(), now).is_some());
923 }
924
925 #[test]
926 fn an_entry_expires() {
927 let c = VerifiedCache::new(Duration::from_secs(1), 100);
928 let t0 = Instant::now();
929 c.offer(
930 &trace(ServedFrom::Attempt, Verdict::Pass),
931 &resp("a"),
932 &ladder(),
933 t0,
934 );
935
936 assert!(c.get("t", "hash-abc", &ladder(), t0).is_some());
937 assert!(
938 c.get("t", "hash-abc", &ladder(), t0 + Duration::from_secs(2))
939 .is_none(),
940 "a stale proof is not a proof"
941 );
942 }
943
944 #[test]
945 fn the_cache_cannot_grow_without_bound() {
946 let c = VerifiedCache::new(Duration::from_secs(600), 10);
947 let now = Instant::now();
948 for i in 0..100 {
949 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
950 t.request.prompt_hash = format!("h{i}");
951 c.offer(&t, &resp("a"), &ladder(), now);
952 }
953 assert!(c.len() <= 10, "expected <= 10 entries, got {}", c.len());
954 }
955}