"""Semantic cache node. Runs after guardrails, before the RAG router.""" from __future__ import annotations from typing import Any, Dict from controlplane.cache import get_cache from controlplane.observability import traceable_node from controlplane.state import Stage @traceable_node("semantic_cache") def semantic_cache_node(state: Dict[str, Any]) -> Dict[str, Any]: query = state.get("guarded_query") or state.get("original_query", "") forced = (state.get("forced_kb") or "").strip() hit = get_cache(state.get("cache_ns")).lookup(query) # per-user namespace # Same pipeline for normal and manual mode. Only difference: if a KB was # forced, a cache entry that came from a DIFFERENT KB must not be served. if hit is not None and forced and (hit.meta or {}).get("selected_kb") not in (None, "", forced): hit = None if hit is None: return {"stage": Stage.CACHE, "stages_visited": [Stage.CACHE], "cache_hit": False} return { "stage": Stage.CACHE, "stages_visited": [Stage.CACHE], "cache_hit": True, "cache_similarity": round(hit.similarity, 4), "cached_answer": hit.answer, "cached_meta": hit.meta, "selected_kb": hit.meta.get("selected_kb"), "final_decision": "cache", } def route_after_cache(state: Dict[str, Any]) -> str: return "hit" if state.get("cache_hit") else "miss"