use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, VecDeque};
use std::hash::{Hash, Hasher};
use serde::{Deserialize, Serialize};
use crate::request::{SystemPrompt, Tool};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrefixFingerprint {
pub system_sha256: String,
pub tools_sha256: String,
pub combined_sha256: String,
}
impl PrefixFingerprint {
#[cfg(test)]
pub fn compute(system_text: &str, tools: Option<&[Tool]>) -> Self {
let mut cache = ToolCatalogCache::new();
Self::compute_with_tool_cache(system_text, tools, &mut cache)
}
pub fn compute_with_tool_cache(
system_text: &str,
tools: Option<&[Tool]>,
cache: &mut ToolCatalogCache,
) -> Self {
let system_sha256 = sha256_hex(system_text.as_bytes());
let tools_sha256 = match tools {
Some(tools) if !tools.is_empty() => {
cache.fingerprint_for(tools).sha256_hex
}
_ => sha256_hex(b""),
};
let combined = format!("{system_sha256}:{tools_sha256}");
let combined_sha256 = sha256_hex(combined.as_bytes());
Self {
system_sha256,
tools_sha256,
combined_sha256,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrefixChange {
pub old: PrefixFingerprint,
pub new: PrefixFingerprint,
pub system_changed: bool,
pub tools_changed: bool,
}
#[allow(dead_code)]
impl PrefixChange {
pub fn description(&self) -> String {
let mut parts = Vec::new();
if self.system_changed {
parts.push("system prompt");
}
if self.tools_changed {
parts.push("tool set");
}
if parts.is_empty() {
return "unknown (fingerprint mismatch but no component detected)".to_string();
}
format!("prefix cache invalidated: {} changed", parts.join(" and "))
}
pub fn label(&self) -> &'static str {
if self.system_changed && self.tools_changed {
"sys+tools"
} else if self.system_changed {
"sys"
} else if self.tools_changed {
"tools"
} else {
"prefix"
}
}
}
#[derive(Debug, Clone)]
pub struct PrefixStabilityManager {
pinned: Option<PrefixFingerprint>,
current: Option<PrefixFingerprint>,
last_change: Option<PrefixChange>,
change_count: u64,
check_count: u64,
pin_reason: Option<String>,
history: VecDeque<PrefixHistoryEntry>,
last_miss_reason: Option<String>,
context_update_count: u64,
tool_catalog_cache: ToolCatalogCache,
}
const PREFIX_HISTORY_CAP: usize = 32;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PrefixHistoryEntry {
pub reason: String,
pub repinned: bool,
pub from_sha256: String,
pub to_sha256: String,
}
#[derive(Debug, Clone)]
pub enum PrefixCheck {
Stable,
Repinned {
reason: String,
change: PrefixChange,
},
Drift { change: PrefixChange },
}
const TOOL_CATALOG_CACHE_CAPACITY: usize = 8;
#[derive(Debug, Default, Clone)]
pub struct ToolCatalogCache {
by_identity: HashMap<u64, CachedCatalog>,
insertion_order: VecDeque<u64>,
capacity: usize,
}
#[derive(Debug, Clone)]
pub struct CachedCatalog {
pub sha256_hex: String,
}
impl ToolCatalogCache {
#[must_use]
pub fn new() -> Self {
Self::with_capacity(TOOL_CATALOG_CACHE_CAPACITY)
}
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
let cap = capacity.max(1);
Self {
by_identity: HashMap::with_capacity(cap),
insertion_order: VecDeque::with_capacity(cap),
capacity: cap,
}
}
pub fn fingerprint_for(&mut self, tools: &[Tool]) -> CachedCatalog {
let identity = tool_set_identity(tools);
if let Some(cached) = self.by_identity.get(&identity) {
return cached.clone();
}
let serialized: Vec<String> = tools.iter().filter_map(tool_to_api_json).collect();
let joined = serialized.join("\n");
let entry = CachedCatalog {
sha256_hex: sha256_hex(joined.as_bytes()),
};
if self.by_identity.len() >= self.capacity
&& let Some(oldest) = self.insertion_order.pop_front()
{
self.by_identity.remove(&oldest);
}
self.by_identity.insert(identity, entry.clone());
self.insertion_order.push_back(identity);
entry
}
#[allow(dead_code)] pub fn invalidate(&mut self) {
self.by_identity.clear();
self.insertion_order.clear();
}
#[must_use]
pub fn len(&self) -> usize {
self.by_identity.len()
}
#[allow(dead_code)] #[must_use]
pub fn is_empty(&self) -> bool {
self.by_identity.is_empty()
}
#[allow(dead_code)] #[must_use]
pub fn stats(&self) -> (usize, usize) {
(self.len(), self.capacity)
}
}
fn tool_set_identity(tools: &[Tool]) -> u64 {
let mut hasher = DefaultHasher::new();
tools.len().hash(&mut hasher);
for tool in tools {
tool.name.hash(&mut hasher);
tool.description.hash(&mut hasher);
tool.strict.hash(&mut hasher);
hash_json_value(&tool.input_schema, &mut hasher);
}
hasher.finish()
}
fn hash_json_value<H: Hasher>(value: &serde_json::Value, state: &mut H) {
match value {
serde_json::Value::Null => 0u8.hash(state),
serde_json::Value::Bool(b) => {
1u8.hash(state);
b.hash(state);
}
serde_json::Value::Number(n) => {
2u8.hash(state);
if let Some(i) = n.as_i64() {
i.hash(state);
} else if let Some(u) = n.as_u64() {
u.hash(state);
} else if let Some(f) = n.as_f64() {
f.to_bits().hash(state);
}
}
serde_json::Value::String(s) => {
3u8.hash(state);
s.hash(state);
}
serde_json::Value::Array(arr) => {
4u8.hash(state);
arr.len().hash(state);
for v in arr {
hash_json_value(v, state);
}
}
serde_json::Value::Object(obj) => {
5u8.hash(state);
obj.len().hash(state);
let mut entries: Vec<(&String, &serde_json::Value)> = obj.iter().collect();
entries.sort_by(|a, b| a.0.cmp(b.0));
for (k, v) in entries {
k.hash(state);
hash_json_value(v, state);
}
}
}
}
#[allow(dead_code)]
impl PrefixStabilityManager {
pub fn new(system_text: &str, tools: Option<&[Tool]>) -> Self {
let mut cache = ToolCatalogCache::new();
let fp = PrefixFingerprint::compute_with_tool_cache(system_text, tools, &mut cache);
Self {
pinned: Some(fp.clone()),
current: Some(fp),
last_change: None,
change_count: 0,
check_count: 0,
pin_reason: Some("initial".to_string()),
history: VecDeque::new(),
last_miss_reason: None,
context_update_count: 0,
tool_catalog_cache: cache,
}
}
pub fn new_unpinned() -> Self {
Self {
pinned: None,
current: None,
last_change: None,
change_count: 0,
check_count: 0,
pin_reason: None,
history: VecDeque::new(),
last_miss_reason: None,
context_update_count: 0,
tool_catalog_cache: ToolCatalogCache::new(),
}
}
pub fn pin(&mut self, system_text: &str, tools: Option<&[Tool]>) -> bool {
self.pin_with_reason(system_text, tools, "initial")
}
pub fn pin_with_reason(
&mut self,
system_text: &str,
tools: Option<&[Tool]>,
reason: &str,
) -> bool {
let fp = PrefixFingerprint::compute_with_tool_cache(
system_text,
tools,
&mut self.tool_catalog_cache,
);
let was_unpinned = self.pinned.is_none();
self.pinned = Some(fp.clone());
self.current = Some(fp);
self.pin_reason = Some(reason.to_string());
was_unpinned
}
pub fn note_history_reset(&mut self, what: &str) {
let hash = self
.pinned
.as_ref()
.map(|fp| fp.combined_sha256.clone())
.unwrap_or_default();
self.push_history(PrefixHistoryEntry {
reason: format!("reset:{what}"),
repinned: false,
from_sha256: hash.clone(),
to_sha256: hash,
});
self.last_miss_reason = Some(format!("reset:{what}"));
}
pub fn note_context_update(&mut self) {
self.context_update_count = self.context_update_count.saturating_add(1);
let hash = self
.pinned
.as_ref()
.map(|fp| fp.combined_sha256.clone())
.unwrap_or_default();
self.push_history(PrefixHistoryEntry {
reason: "context_update".to_string(),
repinned: false,
from_sha256: hash.clone(),
to_sha256: hash,
});
}
pub fn context_update_count(&self) -> u64 {
self.context_update_count
}
fn push_history(&mut self, entry: PrefixHistoryEntry) {
if self.history.len() >= PREFIX_HISTORY_CAP {
self.history.pop_front();
}
self.history.push_back(entry);
}
pub fn check(
&mut self,
system_text: &str,
tools: Option<&[Tool]>,
declared_change: Option<&str>,
) -> PrefixCheck {
let fp = PrefixFingerprint::compute_with_tool_cache(
system_text,
tools,
&mut self.tool_catalog_cache,
);
let old_fp = self.current.replace(fp.clone());
self.check_count += 1;
let pinned = match &self.pinned {
Some(p) => p.clone(),
None => {
self.pinned = Some(fp);
self.pin_reason = Some(declared_change.unwrap_or("initial").to_string());
self.last_change = None;
return PrefixCheck::Stable;
}
};
if fp.combined_sha256 == pinned.combined_sha256 {
return PrefixCheck::Stable;
}
let old = old_fp.unwrap_or_else(|| pinned.clone());
let system_changed = fp.system_sha256 != pinned.system_sha256;
let tools_changed = fp.tools_sha256 != pinned.tools_sha256;
let change = PrefixChange {
old,
new: fp.clone(),
system_changed,
tools_changed,
};
self.last_change = Some(change.clone());
self.change_count += 1;
match declared_change {
Some(reason) => {
let reason = format!("change:{reason}");
self.push_history(PrefixHistoryEntry {
reason: reason.clone(),
repinned: true,
from_sha256: pinned.combined_sha256.clone(),
to_sha256: fp.combined_sha256.clone(),
});
self.last_miss_reason = Some(reason.clone());
self.pinned = Some(fp);
self.pin_reason = Some(reason.clone());
PrefixCheck::Repinned { reason, change }
}
None => {
let reason = format!("drift:{}", change.label());
self.push_history(PrefixHistoryEntry {
reason: reason.clone(),
repinned: false,
from_sha256: pinned.combined_sha256.clone(),
to_sha256: fp.combined_sha256.clone(),
});
self.last_miss_reason = Some(reason);
PrefixCheck::Drift { change }
}
}
}
pub fn pin_reason(&self) -> Option<&str> {
self.pin_reason.as_deref()
}
pub fn history(&self) -> impl Iterator<Item = &PrefixHistoryEntry> {
self.history.iter()
}
pub fn last_miss_reason(&self) -> Option<&str> {
self.last_miss_reason.as_deref()
}
pub fn check_and_update(
&mut self,
system_text: &str,
tools: Option<&[Tool]>,
) -> Result<bool, Box<PrefixChange>> {
match self.check(system_text, tools, None) {
PrefixCheck::Stable => Ok(true),
PrefixCheck::Repinned { change, .. } | PrefixCheck::Drift { change } => {
Err(Box::new(change))
}
}
}
pub fn last_change(&self) -> Option<&PrefixChange> {
self.last_change.as_ref()
}
pub fn pinned_fingerprint(&self) -> Option<&PrefixFingerprint> {
self.pinned.as_ref()
}
pub fn current_fingerprint(&self) -> Option<&PrefixFingerprint> {
self.current.as_ref()
}
pub fn change_count(&self) -> u64 {
self.change_count
}
pub fn check_count(&self) -> u64 {
self.check_count
}
pub fn stability_ratio(&self) -> f64 {
if self.check_count == 0 {
1.0
} else {
let stable_checks = self.check_count - self.change_count;
stable_checks as f64 / self.check_count as f64
}
}
pub fn summary(&self) -> String {
let pct = self.stability_ratio() * 100.0;
let pinned_short = self
.pinned
.as_ref()
.map(|fp| {
if fp.combined_sha256.len() >= 12 {
&fp.combined_sha256[..12]
} else {
&fp.combined_sha256
}
})
.unwrap_or("none");
format!(
"Prefix stability: {pct:.1}% ({stable}/{total} checks stable) | fingerprint: {pinned_short} | changes: {changes}",
pct = pct,
stable = self.check_count.saturating_sub(self.change_count),
total = self.check_count,
pinned_short = pinned_short,
changes = self.change_count,
)
}
}
fn tool_to_api_json(tool: &Tool) -> Option<String> {
let mut value = serde_json::json!({
"type": "function",
"function": {
"name": tool.name,
"description": tool.description,
"parameters": tool.input_schema,
}
});
if let Some(strict) = tool.strict
&& let Some(function) = value.get_mut("function")
{
function["strict"] = serde_json::json!(strict);
}
serde_json::to_string(&value).ok()
}
fn sha256_hex(bytes: &[u8]) -> String {
use sha2::{Digest, Sha256};
use std::fmt::Write;
let mut hex = String::with_capacity(64);
for byte in Sha256::digest(bytes) {
let _ = write!(&mut hex, "{byte:02x}");
}
hex
}
pub const CONTEXT_UPDATE_MAX_LINES: usize = 80;
pub const CONTEXT_UPDATE_MAX_BYTES: usize = 6_000;
pub fn context_update_message(known: &str, current: &str) -> Option<String> {
use std::collections::HashMap;
let mut counts: HashMap<&str, i64> = HashMap::new();
for line in known.lines() {
*counts.entry(line).or_insert(0) -= 1;
}
for line in current.lines() {
*counts.entry(line).or_insert(0) += 1;
}
let mut added: Vec<&str> = Vec::new();
for line in current.lines() {
if let Some(count) = counts.get_mut(line)
&& *count > 0
{
*count -= 1;
if !line.trim().is_empty() {
added.push(line);
}
}
}
let mut removed: Vec<&str> = Vec::new();
for line in known.lines() {
if let Some(count) = counts.get_mut(line)
&& *count < 0
{
*count += 1;
if !line.trim().is_empty() {
removed.push(line);
}
}
}
if added.is_empty() && removed.is_empty() {
return None;
}
let mut out = String::from(
"<context_update>\nSession context changed since it was pinned; the pinned system \
prompt is unchanged. Delta (+ added, - removed):\n",
);
let mut lines_written = 0usize;
let mut truncated = 0usize;
for (sign, group) in [("+ ", &added), ("- ", &removed)] {
for line in group {
if lines_written >= CONTEXT_UPDATE_MAX_LINES
|| out.len() + sign.len() + line.len() + 1 > CONTEXT_UPDATE_MAX_BYTES
{
truncated += 1;
continue;
}
out.push_str(sign);
out.push_str(line);
out.push('\n');
lines_written += 1;
}
}
if truncated > 0 {
out.push_str(&format!(
"(+{truncated} more changed lines; re-read the files you need)\n"
));
}
out.push_str("</context_update>");
Some(out)
}
pub fn system_prompt_text(system: Option<&SystemPrompt>) -> String {
match system {
Some(SystemPrompt::Text(text)) => text.clone(),
Some(SystemPrompt::Blocks(blocks)) => {
let mut text = String::new();
for block in blocks {
text.push_str(&block.text);
text.push('\n');
}
text
}
None => String::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_tool(name: &str) -> Tool {
Tool {
name: name.to_string(),
description: String::new(),
input_schema: serde_json::Value::Null,
tool_type: None,
allowed_callers: None,
defer_loading: None,
input_examples: None,
strict: None,
cache_control: None,
}
}
#[test]
fn same_prefix_produces_same_fingerprint() {
let a = PrefixFingerprint::compute("hello world", None);
let b = PrefixFingerprint::compute("hello world", None);
assert_eq!(a.combined_sha256, b.combined_sha256);
}
#[test]
fn different_system_produces_different_fingerprint() {
let a = PrefixFingerprint::compute("hello", None);
let b = PrefixFingerprint::compute("world", None);
assert_ne!(a.combined_sha256, b.combined_sha256);
}
#[test]
fn tool_order_affects_fingerprint() {
let tools_a = vec![make_tool("read_file"), make_tool("write_file")];
let tools_b = vec![make_tool("write_file"), make_tool("read_file")];
let a = PrefixFingerprint::compute("system", Some(&tools_a));
let b = PrefixFingerprint::compute("system", Some(&tools_b));
assert_ne!(a.combined_sha256, b.combined_sha256);
}
#[test]
fn different_tools_produce_different_fingerprint() {
let tools_a = vec![make_tool("read_file")];
let tools_b = vec![make_tool("write_file")];
let a = PrefixFingerprint::compute("system", Some(&tools_a));
let b = PrefixFingerprint::compute("system", Some(&tools_b));
assert_ne!(a.combined_sha256, b.combined_sha256);
}
#[test]
fn manager_starts_stable() {
let mut mgr = PrefixStabilityManager::new("system prompt", None);
assert!(mgr.check_and_update("system prompt", None).unwrap());
assert_eq!(mgr.change_count(), 0);
assert_eq!(mgr.check_count(), 1);
}
#[test]
fn manager_detects_change() {
let mut mgr = PrefixStabilityManager::new("system prompt", None);
let result = mgr.check_and_update("different prompt", None);
assert!(result.is_err());
assert_eq!(mgr.change_count(), 1);
let change = mgr.last_change().unwrap();
assert!(change.system_changed);
assert!(!change.tools_changed);
}
#[test]
fn manager_detects_tool_change() {
let tools_a = vec![make_tool("read_file")];
let tools_b = vec![make_tool("write_file")];
let mut mgr = PrefixStabilityManager::new("system", Some(&tools_a));
let result = mgr.check_and_update("system", Some(&tools_b));
assert!(result.is_err());
let change = mgr.last_change().unwrap();
assert!(!change.system_changed);
assert!(change.tools_changed);
}
#[test]
fn undeclared_drift_never_moves_the_pin() {
let mut mgr = PrefixStabilityManager::new("old", None);
assert_eq!(mgr.pin_reason(), Some("initial"));
assert!(mgr.check_and_update("new", None).is_err());
assert!(mgr.check_and_update("new", None).is_err());
assert!(mgr.check_and_update("old", None).unwrap());
assert_eq!(mgr.change_count(), 2);
assert_eq!(mgr.last_miss_reason(), Some("drift:sys"));
let history: Vec<_> = mgr.history().collect();
assert_eq!(history.len(), 2);
assert!(history.iter().all(|entry| !entry.repinned));
assert_eq!(mgr.pin_reason(), Some("initial"));
}
#[test]
fn declared_header_change_repins_under_a_logged_reason() {
let mut mgr = PrefixStabilityManager::new("old", None);
match mgr.check("new", None, Some("model")) {
PrefixCheck::Repinned { reason, change } => {
assert_eq!(reason, "change:model");
assert!(change.system_changed);
}
other => panic!("expected repin, got {other:?}"),
}
assert_eq!(mgr.pin_reason(), Some("change:model"));
assert!(matches!(mgr.check("new", None, None), PrefixCheck::Stable));
assert!(matches!(
mgr.check("newer", None, None),
PrefixCheck::Drift { .. }
));
assert_eq!(mgr.pin_reason(), Some("change:model"));
let reasons: Vec<&str> = mgr.history().map(|e| e.reason.as_str()).collect();
assert_eq!(reasons, vec!["change:model", "drift:sys"]);
}
#[test]
fn declared_reason_on_a_stable_prefix_is_a_noop() {
let mut mgr = PrefixStabilityManager::new("same", None);
assert!(matches!(
mgr.check("same", None, Some("mode")),
PrefixCheck::Stable
));
assert_eq!(mgr.change_count(), 0);
assert_eq!(mgr.pin_reason(), Some("initial"));
}
#[test]
fn context_update_message_reports_added_and_removed_lines_bounded() {
let known = "## Files\nsrc/a.rs\nsrc/b.rs\n## Instructions\nbe kind\n";
let current = "## Files\nsrc/a.rs\nsrc/b.rs\nsrc/c.rs\n## Instructions\nbe precise\n";
let update = context_update_message(known, current).expect("delta");
assert!(update.starts_with("<context_update>"));
assert!(update.ends_with("</context_update>"));
assert!(update.contains("+ src/c.rs"));
assert!(update.contains("+ be precise"));
assert!(update.contains("- be kind"));
assert!(!update.contains("- src/a.rs"));
assert!(context_update_message(known, known).is_none());
let mut big = String::new();
for i in 0..500 {
big.push_str(&format!("line {i}\n"));
}
let bounded = context_update_message("", &big).expect("delta");
assert!(
bounded.len() <= CONTEXT_UPDATE_MAX_BYTES + 128,
"{}",
bounded.len()
);
assert!(bounded.contains("more changed lines"));
}
#[test]
fn context_update_is_logged_but_is_not_a_miss() {
let mut mgr = PrefixStabilityManager::new("sys", None);
mgr.note_context_update();
assert_eq!(mgr.context_update_count(), 1);
assert_eq!(mgr.last_miss_reason(), None);
assert!(matches!(mgr.check("sys", None, None), PrefixCheck::Stable));
}
#[test]
fn history_reset_is_logged_without_moving_the_pin() {
let mut mgr = PrefixStabilityManager::new("sys", None);
mgr.note_history_reset("compaction");
assert_eq!(mgr.last_miss_reason(), Some("reset:compaction"));
assert!(matches!(mgr.check("sys", None, None), PrefixCheck::Stable));
assert_eq!(mgr.history().count(), 1);
}
#[test]
fn stability_ratio_is_one_for_no_changes() {
let mut mgr = PrefixStabilityManager::new("hello", None);
mgr.check_and_update("hello", None).unwrap();
mgr.check_and_update("hello", None).unwrap();
assert!((mgr.stability_ratio() - 1.0).abs() < f64::EPSILON);
assert_eq!(mgr.check_count(), 2);
assert_eq!(mgr.change_count(), 0);
}
#[test]
fn stability_ratio_reflects_change_rate() {
let mut mgr = PrefixStabilityManager::new("hello", None);
mgr.check_and_update("hello", None).unwrap(); let _ = mgr.check("world", None, Some("model")); mgr.check_and_update("world", None).unwrap(); assert!((mgr.stability_ratio() - 2.0 / 3.0).abs() < 0.01);
assert_eq!(mgr.check_count(), 3);
assert_eq!(mgr.change_count(), 1);
}
#[test]
fn empty_tools_and_none_tools_produce_same_hash() {
let empty = PrefixFingerprint::compute("system", Some(&[]));
let none = PrefixFingerprint::compute("system", None);
assert_eq!(empty.tools_sha256, none.tools_sha256);
}
#[test]
fn empty_system_produces_sha256_of_empty_string() {
let fp = PrefixFingerprint::compute("", None);
let expected = sha256_hex(b"");
assert_eq!(fp.system_sha256, expected);
}
#[test]
fn prefix_change_description_is_informative() {
let old = PrefixFingerprint::compute("old", None);
let new = PrefixFingerprint::compute("new", None);
let change = PrefixChange {
old,
new,
system_changed: true,
tools_changed: false,
};
assert_eq!(
change.description(),
"prefix cache invalidated: system prompt changed"
);
assert_eq!(change.label(), "sys");
}
#[test]
fn new_unpinned_has_no_change_history() {
let mut mgr = PrefixStabilityManager::new_unpinned();
assert!(mgr.pinned_fingerprint().is_none());
assert!(mgr.current_fingerprint().is_none());
assert!(mgr.last_change().is_none());
assert_eq!(mgr.change_count(), 0);
assert_eq!(mgr.check_count(), 0);
assert!(mgr.check_and_update("hello", None).unwrap());
assert!(mgr.pinned_fingerprint().is_some());
assert_eq!(mgr.check_count(), 1);
}
#[test]
fn fingerprint_detects_schema_change_not_just_name_change() {
let tool_a = make_tool("my_tool");
let mut tool_a_v2 = make_tool("my_tool");
tool_a_v2.description = "updated description".to_string();
let a = PrefixFingerprint::compute("system", Some(&[tool_a]));
let b = PrefixFingerprint::compute("system", Some(&[tool_a_v2]));
assert_ne!(a.tools_sha256, b.tools_sha256);
assert_ne!(a.combined_sha256, b.combined_sha256);
}
#[test]
fn system_prompt_text_returns_empty_for_none() {
assert_eq!(system_prompt_text(None), "");
}
#[test]
fn tool_catalog_cache_miss_then_hit_returns_same_digest() {
let mut cache = ToolCatalogCache::new();
let tools = vec![make_tool("read_file"), make_tool("write_file")];
let first = cache.fingerprint_for(&tools);
assert_eq!(cache.len(), 1);
let second = cache.fingerprint_for(&tools);
assert_eq!(cache.len(), 1, "second call should be a cache hit");
assert_eq!(first.sha256_hex, second.sha256_hex);
}
#[test]
fn tool_catalog_cache_different_tool_sets_dont_collide() {
let mut cache = ToolCatalogCache::new();
let a = vec![make_tool("read_file")];
let b = vec![make_tool("write_file")];
let entry_a = cache.fingerprint_for(&a);
let entry_b = cache.fingerprint_for(&b);
assert_eq!(cache.len(), 2);
assert_ne!(entry_a.sha256_hex, entry_b.sha256_hex);
}
#[test]
fn tool_catalog_cache_fingerprint_is_order_sensitive_like_the_provider_cache() {
let mut cache = ToolCatalogCache::new();
let a = vec![make_tool("read_file"), make_tool("write_file")];
let b = vec![make_tool("write_file"), make_tool("read_file")];
let entry_a = cache.fingerprint_for(&a);
let entry_b = cache.fingerprint_for(&b);
assert_ne!(entry_a.sha256_hex, entry_b.sha256_hex);
assert_eq!(cache.len(), 2);
let again = cache.fingerprint_for(&a);
assert_eq!(again.sha256_hex, entry_a.sha256_hex);
}
#[test]
fn tool_catalog_cache_detects_schema_change() {
let mut cache = ToolCatalogCache::new();
let tool_v1 = make_tool("t");
let mut tool_v2 = make_tool("t");
tool_v2.description = "updated".to_string();
let entry_v1 = cache.fingerprint_for(&[tool_v1]);
let entry_v2 = cache.fingerprint_for(&[tool_v2]);
assert_ne!(entry_v1.sha256_hex, entry_v2.sha256_hex);
assert_eq!(cache.len(), 2);
}
#[test]
fn tool_catalog_cache_respects_capacity() {
let mut cache = ToolCatalogCache::with_capacity(2);
cache.fingerprint_for(&[make_tool("a")]);
cache.fingerprint_for(&[make_tool("b")]);
cache.fingerprint_for(&[make_tool("c")]);
assert_eq!(cache.len(), 2);
let re_entry = cache.fingerprint_for(&[make_tool("a")]);
assert_eq!(cache.len(), 2);
let fresh = cache.fingerprint_for(&[make_tool("a")]);
assert_eq!(re_entry.sha256_hex, fresh.sha256_hex);
}
#[test]
fn tool_catalog_cache_invalidate_clears_all() {
let mut cache = ToolCatalogCache::new();
cache.fingerprint_for(&[make_tool("a")]);
cache.fingerprint_for(&[make_tool("b")]);
cache.invalidate();
assert!(cache.is_empty());
assert_eq!(cache.len(), 0);
}
#[test]
fn tool_catalog_cache_empty_slice_uses_zero_capacity_path() {
let mut cache = ToolCatalogCache::new();
let entry = cache.fingerprint_for(&[]);
assert!(!entry.sha256_hex.is_empty());
let again = cache.fingerprint_for(&[]);
assert_eq!(entry.sha256_hex, again.sha256_hex);
}
#[test]
fn compute_with_tool_cache_matches_compute_uncached() {
let mut cache = ToolCatalogCache::new();
let tools = vec![make_tool("alpha"), make_tool("beta")];
let cached = PrefixFingerprint::compute_with_tool_cache("sys", Some(&tools), &mut cache);
let uncached = PrefixFingerprint::compute("sys", Some(&tools));
assert_eq!(cached.combined_sha256, uncached.combined_sha256);
assert_eq!(cached.tools_sha256, uncached.tools_sha256);
}
#[test]
fn manager_check_and_update_uses_cached_tool_fingerprint() {
let tools = vec![make_tool("t1")];
let mut mgr = PrefixStabilityManager::new("sys", Some(&tools));
assert!(mgr.check_and_update("sys", Some(&tools)).is_ok());
assert!(mgr.check_and_update("sys", Some(&tools)).is_ok());
assert_eq!(mgr.change_count(), 0);
}
}