use std::collections::{HashSet, VecDeque};
use std::sync::{Mutex, OnceLock};
use hmac::{KeyInit, Mac};
use serde_json::Value;
use crate::message::ToolCall;
use crate::state::RunId;
type HmacSha256 = hmac::Hmac<sha2::Sha256>;
const USED_CAPACITY: usize = 65_536;
pub(crate) fn process_key() -> [u8; 32] {
static KEY: OnceLock<[u8; 32]> = OnceLock::new();
*KEY.get_or_init(|| {
let mut key = [0u8; 32];
key[..16].copy_from_slice(uuid::Uuid::new_v4().as_bytes());
key[16..].copy_from_slice(uuid::Uuid::new_v4().as_bytes());
key
})
}
pub(crate) struct Question<'a> {
pub run_id: RunId,
pub turn: u32,
pub call: &'a ToolCall,
pub hook_index: usize,
pub hook_name: &'a str,
pub waived: &'a [usize],
}
pub(crate) fn digest(key: &[u8; 32], question: &Question<'_>) -> String {
let material = serde_json::json!([
question.run_id.to_string(),
question.turn,
question.call.id,
question.call.name,
canonical(&question.call.arguments),
question.hook_index,
question.hook_name,
question.waived,
]);
let mut mac = HmacSha256::new_from_slice(key).expect("HMAC takes any key length");
mac.update(material.to_string().as_bytes());
hex::encode(mac.finalize().into_bytes())
}
pub(crate) fn same(a: &str, b: &str) -> bool {
a.len() == b.len()
&& a.bytes()
.zip(b.bytes())
.fold(0u8, |acc, (x, y)| acc | (x ^ y))
== 0
}
fn canonical(value: &Value) -> Value {
match value {
Value::Object(map) => {
let mut keys: Vec<&String> = map.keys().collect();
keys.sort();
let mut out = serde_json::Map::new();
for key in keys {
out.insert(key.clone(), canonical(&map[key]));
}
Value::Object(out)
}
Value::Array(items) => Value::Array(items.iter().map(canonical).collect()),
other => other.clone(),
}
}
pub(crate) fn consume(digest: &str) -> bool {
static USED: OnceLock<Mutex<(HashSet<String>, VecDeque<String>)>> = OnceLock::new();
let used = USED.get_or_init(Mutex::default);
let mut used = used.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let (set, order) = &mut *used;
if !set.insert(digest.to_string()) {
return false;
}
order.push_back(digest.to_string());
if order.len() > USED_CAPACITY
&& let Some(oldest) = order.pop_front()
{
set.remove(&oldest);
}
true
}