Files
GTI_GERENCIADOR_FISCAL/central/app/ai/cache_policy.py
T

31 lines
1.2 KiB
Python

"""Pure cache and dataset-freshness policies for offline and real evaluations."""
import hashlib
import json
VALID, NEEDS_REVALIDATION, CONFLICT = "VALID", "NEEDS_REVALIDATION", "CONFLICT"
def cache_key(input_fingerprint, candidate_set_hash, model, prompt_version, dataset_versions):
"""Return a stable key covering every input that can change an answer."""
payload = {
"input_fingerprint": str(input_fingerprint),
"candidate_set_hash": str(candidate_set_hash),
"model": str(model),
"prompt_version": str(prompt_version),
"dataset_versions": dict(sorted((dataset_versions or {}).items())),
}
return hashlib.sha256(json.dumps(payload, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
def classify_knowledge_freshness(record_versions, active_versions):
"""Classify versioned knowledge without deleting or rewriting history."""
recorded = dict(record_versions or {})
active = dict(active_versions or {})
if any(key in recorded and key in active and recorded[key] != active[key] for key in recorded):
return CONFLICT
if recorded != active:
return NEEDS_REVALIDATION
return VALID