Expand description
Semantic Cache with Provenance-Safe Keys (Task 8)
This module provides a semantic caching layer that:
- Uses exact key matching for deterministic cache hits
- Supports semantic similarity search for approximate matches
- Preserves provenance (DERIVED_FROM edges to sources)
- Integrates with AllowedSet for policy-aware caching
§Design Principles
- Exact-match first: Hash of (normalized_query, AllowedSet) for deterministic hits
- Semantic fallback: ANN search with explicit similarity threshold
- Provenance tracking: Every cache entry tracks its source documents
- Policy integration: Cache hits respect AllowedSet (no post-filtering)
§Complexity
- Exact match: O(1) hash lookup
- Semantic search: O(log n) with HNSW index
- Policy gating: O(1) per entry with namespace/allowed-set check
§Cache Invalidation Strategy
Cache entries are invalidated when:
- TTL expires
- Any source document is updated (via provenance edges)
- Policy changes affect the AllowedSet
Note: Semantic similarity ≠ query equivalence. Similar queries may have different correct answers, so semantic cache hits are marked as “approximate”.
Structs§
- Cache
Entry - A cached result entry
- Cache
Key - Unique cache key combining query and access context
- Cache
Lookup Result - Cache lookup result
- Semantic
Cache - Provenance-safe semantic cache
- Semantic
Cache Config - Configuration for the semantic cache
Enums§
- Cache
Match Type - Type of cache match
Functions§
- hash_
allowed_ set - Compute a hash of an AllowedSet for cache keying