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 }
595 }
596
597 #[test]
598 fn a_passing_answer_is_stored_and_replayed_with_its_proof() {
599 let c = VerifiedCache::new(Duration::from_secs(60), 100);
600 let now = Instant::now();
601 let t = trace(ServedFrom::Attempt, Verdict::Pass);
602
603 assert!(c.offer(&t, &resp("the answer"), &ladder(), now));
604
605 let hit = c.get("t", "hash-abc", &ladder(), now).expect("hit");
606 assert_eq!(hit.response.text, "the answer");
607 assert_eq!(hit.source, t.trace_id);
609 assert!((hit.original_cost_usd - 0.01).abs() < f64::EPSILON);
610 }
611
612 #[test]
613 fn a_failed_answer_is_never_cached() {
614 let c = VerifiedCache::new(Duration::from_secs(60), 100);
615 let now = Instant::now();
616
617 assert!(!c.offer(
618 &trace(ServedFrom::Attempt, Verdict::Fail),
619 &resp("bad"),
620 &ladder(),
621 now
622 ));
623
624 assert!(
625 c.get("t", "hash-abc", &ladder(), now).is_none(),
626 "must not be replayable"
627 );
628 }
629
630 #[test]
631 fn an_abstain_is_never_cached() {
632 let c = VerifiedCache::new(Duration::from_secs(60), 100);
635 let now = Instant::now();
636 assert!(!c.offer(
637 &trace(ServedFrom::Attempt, Verdict::Abstain),
638 &resp("?"),
639 &ladder(),
640 now
641 ));
642 assert!(c.get("t", "hash-abc", &ladder(), now).is_none());
643 }
644
645 #[test]
646 fn a_budget_exhausted_best_effort_serve_is_never_cached() {
647 let c = VerifiedCache::new(Duration::from_secs(60), 100);
650 let now = Instant::now();
651 assert!(!c.offer(
652 &trace(ServedFrom::BestAttempt, Verdict::Pass),
653 &resp("x"),
654 &ladder(),
655 now
656 ));
657 assert!(c.get("t", "hash-abc", &ladder(), now).is_none());
658 }
659
660 #[test]
661 fn a_later_failing_verdict_retracts_the_proof() {
662 let c = VerifiedCache::new(Duration::from_secs(60), 100);
665 let now = Instant::now();
666 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
667 t.deferred.push(DeferredVerdict {
668 gate_id: "tests".to_owned(),
669 verdict: Verdict::Fail,
670 score: None,
671 reported_at: jiff::Timestamp::UNIX_EPOCH,
672 reporter: "ci".to_owned(),
673 });
674
675 assert!(!c.offer(&t, &resp("looked fine"), &ladder(), now));
676 }
677
678 #[cfg(feature = "redis-cache")]
679 #[test]
680 fn a_redis_url_never_reaches_a_log_line_with_its_password() {
681 assert_eq!(
684 redact_redis_url("redis://user:hunter2@cache:6379/0"),
685 "redis://cache:6379/0"
686 );
687 assert_eq!(
689 redact_redis_url("redis://user:p@ss@cache:6379/0"),
690 "redis://cache:6379/0"
691 );
692 assert_eq!(
693 redact_redis_url("redis://cache:6379/0"),
694 "redis://cache:6379/0"
695 );
696 assert_eq!(redact_redis_url("rediss://u:p@h:6380"), "rediss://h:6380");
697 assert_eq!(redact_redis_url("nonsense"), "<malformed redis url>");
698
699 for u in [
700 "redis://user:hunter2@cache:6379/0",
701 "redis://user:p@ss@cache:6379/0",
702 ] {
703 let out = redact_redis_url(u);
704 assert!(!out.contains("hunter2"), "{out}");
705 assert!(!out.contains("p@ss"), "{out}");
706 assert!(!out.contains("user"), "{out}");
707 }
708 }
709
710 #[cfg(feature = "redis-cache")]
716 #[tokio::test]
717 async fn redis_store_round_trips_and_retracts() {
718 let url = std::env::var("FIRSTPASS_TEST_REDIS").unwrap_or_else(|_| {
719 panic!(
720 "set FIRSTPASS_TEST_REDIS (e.g. redis://127.0.0.1:6379/15) to run the redis-cache \
721 tests. Not skipped on purpose: a green suite that tested nothing is worse than a \
722 red one."
723 )
724 });
725 let store = RedisStore::connect(&url, 60).await.expect("connect");
726
727 let t = trace(ServedFrom::Attempt, Verdict::Pass);
728 let k = VerifiedCache::compose_key("t", &format!("h-{}", t.trace_id), &ladder());
729 let now = Instant::now();
730 let entry = Entry::new(resp("shared answer"), &t, now);
731
732 store.put(&k, t.trace_id, entry, now).await;
733 let got = store
734 .get(&k, now)
735 .await
736 .expect("entry crossed the boundary");
737 assert_eq!(got.response.text, "shared answer");
738 assert_eq!(got.source, t.trace_id);
740
741 assert_eq!(store.retract(t.trace_id).await, 1);
743 assert!(
744 store.get(&k, now).await.is_none(),
745 "a retracted answer must stop being replayable in the SHARED store too"
746 );
747 }
748
749 #[cfg(feature = "redis-cache")]
750 #[tokio::test]
751 async fn redis_store_isolates_tenants() {
752 let url = std::env::var("FIRSTPASS_TEST_REDIS")
754 .expect("set FIRSTPASS_TEST_REDIS to run the redis-cache tests");
755 let store = RedisStore::connect(&url, 60).await.expect("connect");
756
757 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
758 t.tenant_id = "tenant-a".to_owned();
759 let hash = format!("iso-{}", t.trace_id);
760 let ka = VerifiedCache::compose_key("tenant-a", &hash, &ladder());
761 let kb = VerifiedCache::compose_key("tenant-b", &hash, &ladder());
762 let now = Instant::now();
763
764 store
765 .put(&ka, t.trace_id, Entry::new(resp("a"), &t, now), now)
766 .await;
767
768 assert!(
769 store.get(&kb, now).await.is_none(),
770 "cross-tenant replay is a data leak"
771 );
772 assert!(store.get(&ka, now).await.is_some());
773 store.retract(t.trace_id).await;
774 }
775
776 #[tokio::test]
777 async fn the_trait_and_the_concrete_api_agree_on_keys() {
778 let c = VerifiedCache::new(Duration::from_secs(60), 100);
782 let now = Instant::now();
783 let t = trace(ServedFrom::Attempt, Verdict::Pass);
784 assert!(c.offer(&t, &resp("via offer"), &ladder(), now));
785
786 let k = VerifiedCache::compose_key("t", "hash-abc", &ladder());
787 let via_trait = CacheStore::get(&c, &k, now)
788 .await
789 .expect("trait sees offer's entry");
790 assert_eq!(via_trait.response.text, "via offer");
791 }
792
793 #[tokio::test]
794 async fn the_trait_path_still_isolates_tenants() {
795 let a = VerifiedCache::compose_key("tenant-a", "h", &ladder());
798 let b = VerifiedCache::compose_key("tenant-b", "h", &ladder());
799 assert_ne!(a, b, "tenant must be part of the key on the trait path too");
800 assert!(a.starts_with("tenant-a|"), "{a}");
801 }
802
803 #[tokio::test]
804 async fn an_entry_survives_a_serde_round_trip_without_its_local_clock() {
805 let e = Entry {
809 response: resp("answer"),
810 source: uuid::Uuid::now_v7(),
811 original_cost_usd: 0.01,
812 stored: Instant::now(),
813 };
814 let wire = serde_json::to_string(&e).expect("serializes");
815 assert!(
816 !wire.contains("stored"),
817 "a local Instant must not go on the wire: {wire}"
818 );
819
820 let back: Entry = serde_json::from_str(&wire).expect("deserializes");
821 assert_eq!(back.response.text, "answer");
822 assert_eq!(back.source, e.source, "the PROOF must survive the crossing");
823 assert!((back.original_cost_usd - 0.01).abs() < f64::EPSILON);
824 }
825
826 #[test]
827 fn a_retraction_evicts_the_answer_it_disproved() {
828 let c = VerifiedCache::new(Duration::from_secs(600), 100);
832 let now = Instant::now();
833 let t = trace(ServedFrom::Attempt, Verdict::Pass);
834 assert!(c.offer(&t, &resp("looked fine"), &ladder(), now));
835 assert!(c.get("t", "hash-abc", &ladder(), now).is_some());
836
837 assert_eq!(c.retract(t.trace_id), 1);
838
839 assert!(
840 c.get("t", "hash-abc", &ladder(), now).is_none(),
841 "an answer whose proof was retracted must stop being replayed immediately"
842 );
843 }
844
845 #[test]
846 fn retracting_an_unrelated_decision_evicts_nothing() {
847 let c = VerifiedCache::new(Duration::from_secs(600), 100);
848 let now = Instant::now();
849 c.offer(
850 &trace(ServedFrom::Attempt, Verdict::Pass),
851 &resp("a"),
852 &ladder(),
853 now,
854 );
855
856 assert_eq!(c.retract(uuid::Uuid::now_v7()), 0);
857 assert!(c.get("t", "hash-abc", &ladder(), now).is_some());
858 }
859
860 #[test]
861 fn a_cache_hit_is_not_itself_re_cached() {
862 let c = VerifiedCache::new(Duration::from_secs(60), 100);
865 let now = Instant::now();
866 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
867 t.final_.cache_source = Some(uuid::Uuid::now_v7());
868 assert!(!c.offer(&t, &resp("replayed"), &ladder(), now));
869 }
870
871 #[test]
872 fn one_tenant_can_never_replay_anothers_answer() {
873 let c = VerifiedCache::new(Duration::from_secs(60), 100);
878 let now = Instant::now();
879 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
880 t.tenant_id = "tenant-a".to_owned();
881 assert!(c.offer(&t, &resp("a's private answer"), &ladder(), now));
882
883 assert!(
884 c.get("tenant-b", "hash-abc", &ladder(), now).is_none(),
885 "cross-tenant replay — this is a data leak, not a cache miss"
886 );
887 assert!(
888 c.get("tenant-a", "hash-abc", &ladder(), now).is_some(),
889 "the owning tenant must still hit"
890 );
891 }
892
893 #[test]
894 fn the_cached_tenant_comes_from_the_receipt_not_the_caller() {
895 let c = VerifiedCache::new(Duration::from_secs(60), 100);
898 let now = Instant::now();
899 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
900 t.tenant_id = "owner".to_owned();
901 c.offer(&t, &resp("x"), &ladder(), now);
902
903 assert!(c.get("owner", "hash-abc", &ladder(), now).is_some());
904 assert!(c.get("t", "hash-abc", &ladder(), now).is_none());
905 }
906
907 #[test]
908 fn a_different_ladder_is_a_different_question() {
909 let c = VerifiedCache::new(Duration::from_secs(60), 100);
911 let now = Instant::now();
912 c.offer(
913 &trace(ServedFrom::Attempt, Verdict::Pass),
914 &resp("a"),
915 &ladder(),
916 now,
917 );
918
919 let other = vec!["openai/gpt-5.5".to_owned()];
920 assert!(c.get("t", "hash-abc", &other, now).is_none());
921 assert!(c.get("t", "hash-abc", &ladder(), now).is_some());
922 }
923
924 #[test]
925 fn an_entry_expires() {
926 let c = VerifiedCache::new(Duration::from_secs(1), 100);
927 let t0 = Instant::now();
928 c.offer(
929 &trace(ServedFrom::Attempt, Verdict::Pass),
930 &resp("a"),
931 &ladder(),
932 t0,
933 );
934
935 assert!(c.get("t", "hash-abc", &ladder(), t0).is_some());
936 assert!(
937 c.get("t", "hash-abc", &ladder(), t0 + Duration::from_secs(2))
938 .is_none(),
939 "a stale proof is not a proof"
940 );
941 }
942
943 #[test]
944 fn the_cache_cannot_grow_without_bound() {
945 let c = VerifiedCache::new(Duration::from_secs(600), 10);
946 let now = Instant::now();
947 for i in 0..100 {
948 let mut t = trace(ServedFrom::Attempt, Verdict::Pass);
949 t.request.prompt_hash = format!("h{i}");
950 c.offer(&t, &resp("a"), &ladder(), now);
951 }
952 assert!(c.len() <= 10, "expected <= 10 entries, got {}", c.len());
953 }
954}