use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex, OnceLock, PoisonError};
use lean_ctx_protocol::context_gateway::ContextDecisionV1;
const CAPACITY: usize = 4_096;
type Key = [u8; 32];
struct Memo {
order: VecDeque<Key>,
decisions: HashMap<Key, Arc<ContextDecisionV1>>,
}
fn memo() -> &'static Mutex<Memo> {
static MEMO: OnceLock<Mutex<Memo>> = OnceLock::new();
MEMO.get_or_init(|| {
Mutex::new(Memo {
order: VecDeque::with_capacity(CAPACITY),
decisions: HashMap::with_capacity(CAPACITY),
})
})
}
pub(super) fn key(policy_fingerprint: &[u8], path: &str, text: &str) -> Key {
let mut hasher = blake3::Hasher::new();
hasher.update(&(policy_fingerprint.len() as u64).to_le_bytes());
hasher.update(policy_fingerprint);
hasher.update(&(path.len() as u64).to_le_bytes());
hasher.update(path.as_bytes());
hasher.update(text.as_bytes());
*hasher.finalize().as_bytes()
}
pub(super) fn get(key: &Key) -> Option<Arc<ContextDecisionV1>> {
memo()
.lock()
.unwrap_or_else(PoisonError::into_inner)
.decisions
.get(key)
.cloned()
}
#[cfg(test)]
pub(super) fn clear() {
let mut memo = memo().lock().unwrap_or_else(PoisonError::into_inner);
memo.order.clear();
memo.decisions.clear();
}
pub(super) fn insert(key: Key, decision: ContextDecisionV1) {
let mut memo = memo().lock().unwrap_or_else(PoisonError::into_inner);
if memo.decisions.insert(key, Arc::new(decision)).is_some() {
return;
}
memo.order.push_back(key);
if memo.order.len() > CAPACITY
&& let Some(oldest) = memo.order.pop_front()
{
memo.decisions.remove(&oldest);
}
}