Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /avinash-mall_CognitiveMemoryLayer /src /retrieval /retriever.py
"""Hybrid retriever executing plans across memory sources."""
import asyncio
import inspect
import time
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from typing import Any
from uuid import UUID, uuid4
from ..core.enums import MemorySource, MemoryType
from ..core.schemas import MemoryRecord, Provenance, RetrievedMemory
from ..memory.hippocampal.store import HippocampalStore
from ..memory.neocortical.store import NeocorticalStore
from ..utils.logging_config import get_logger
from ..utils.similarity import cosine_similarity
from .planner import RetrievalPlan, RetrievalSource, RetrievalStep
from .query_types import QueryAnalysis
logger = get_logger(__name__)
# Band that graph hits are rank-normalised into. The ceiling sits below 1.0 on purpose:
# an entity profile summarises a neighbourhood rather than answering a question, so it
# should compete with strong episodes without automatically displacing them. The floor
# keeps the weakest graph hit above the packet's episode_relevance_threshold default.
GRAPH_RELEVANCE_FLOOR = 0.55
GRAPH_RELEVANCE_CEILING = 0.85
# Temporal contiguity expansion. One scan per seed, so the seed count is what bounds the
# work; the window is wide enough to cover a conversational exchange without spilling into
# the next session, and neighbours enter strictly below their seed so context can never
# outrank the episode that actually matched the query.
_CONTIGUITY_MAX_SEEDS = 3
_CONTIGUITY_WINDOW_MINUTES = 30
_CONTIGUITY_RELEVANCE_FACTOR = 0.7
# The window can cover a whole session when every turn shares one timestamp, so the scan
# must be able to return the session before neighbours are picked out of it in order.
_CONTIGUITY_SESSION_SCAN_LIMIT = 200
def _neighbours_of(seed: Any, rows: list[Any], k: int) -> list[Any]:
"""The k rows either side of ``seed`` in encoding order, seed excluded.
Selecting the window in Python rather than in SQL is what makes this correct when a
session's rows are not separable by timestamp: the database cannot order them, but it
can hand over the session, and the ordering key lives in metadata.
"""
ordered = HybridRetriever._encoding_order(rows)
position = next((i for i, r in enumerate(ordered) if r.id == seed.id), None)
if position is None:
return []
return ordered[max(0, position - k) : position] + ordered[position + 1 : position + 1 + k]
@dataclass
class RetrievalResult:
"""Result from a single retrieval step."""
source: RetrievalSource
items: list[dict[str, Any]]
elapsed_ms: float
success: bool
error: str | None = None
class HybridRetriever:
"""Executes retrieval plans across multiple memory sources.
Enforces per-step and total retrieval timeouts so that slow sources
(e.g. Neo4j GDS) don't dominate tail latency.
"""
def __init__(
self,
hippocampal: HippocampalStore,
neocortical: NeocorticalStore,
):
self.hippocampal = hippocampal
self.neocortical = neocortical
async def retrieve(
self,
tenant_id: str,
plan: RetrievalPlan,
context_filter: list[str] | None = None,
query_embedding: list[float] | None = None,
debug: bool = False,
) -> list[RetrievedMemory]:
"""Execute a retrieval plan with enforced timeouts. Holistic: tenant-only."""
from ..core.config import get_settings
settings = get_settings()
timeouts_enabled = settings.features.retrieval_timeouts_enabled
cross_skip = settings.features.skip_if_found_cross_group
all_results: list[dict[str, Any]] = []
plan_start = time.perf_counter()
plan_budget = plan.total_timeout_ms / 1000.0
skip_remaining = False
completed_sources: set[str] = set()
timed_out_sources: set[str] = set()
failed_sources: set[str] = set()
debug_steps: list[dict[str, Any]] = []
for group_indices in plan.parallel_steps:
if skip_remaining:
break
# Check remaining plan budget
if timeouts_enabled:
elapsed = time.perf_counter() - plan_start
remaining = plan_budget - elapsed
if remaining <= 0:
for idx in group_indices:
if idx < len(plan.steps):
timed_out_sources.add(plan.steps[idx].source.value)
if debug:
debug_steps.append(
self._step_debug(
plan.steps[idx],
success=False,
elapsed_ms=0.0,
result_count=0,
error="Plan budget exceeded before execution",
timed_out=True,
context_filter=context_filter,
)
)
logger.info(
"retrieval_plan_budget_exceeded", extra={"elapsed_ms": elapsed * 1000}
)
break
else:
remaining = None # No cap
group_steps = [plan.steps[i] for i in group_indices if i < len(plan.steps)]
# Execute group in parallel with per-step timeouts
if timeouts_enabled:
coros = [
self._execute_step_with_timeout(
tenant_id, step, context_filter, query_embedding
)
for step in group_steps
]
try:
group_results = await asyncio.wait_for(
asyncio.gather(*coros, return_exceptions=True),
timeout=remaining,
)
except TimeoutError:
for step in group_steps:
timed_out_sources.add(step.source.value)
if debug:
debug_steps.append(
self._step_debug(
step,
success=False,
elapsed_ms=float(step.timeout_ms),
result_count=0,
error="Retrieval group timed out",
timed_out=True,
context_filter=context_filter,
)
)
logger.info("retrieval_group_timeout", extra={"group": group_indices})
break
else:
group_results = await asyncio.gather(
*[
self._execute_step(tenant_id, step, context_filter, query_embedding)
for step in group_steps
],
return_exceptions=True,
)
for step, result in zip(group_steps, group_results, strict=False):
if isinstance(result, BaseException):
failed_sources.add(step.source.value)
continue
if result.success:
completed_sources.add(step.source.value)
elif result.error and "timeout" in result.error.lower():
timed_out_sources.add(step.source.value)
else:
failed_sources.add(step.source.value)
if debug:
debug_steps.append(
self._step_debug(
step,
success=result.success,
elapsed_ms=result.elapsed_ms,
result_count=len(result.items),
error=result.error,
timed_out=bool(result.error and "timeout" in result.error.lower()),
context_filter=context_filter,
result_items=result.items,
)
)
if result.success and result.items:
all_results.extend(result.items)
# Phase 3.2: cross-group skip
if step.skip_if_found and result.items and cross_skip:
skip_remaining = True
# AUD-07: Post-retrieval graph expansion from derived seeds and domain anchors
if not any(s.source == RetrievalSource.GRAPH for s in plan.steps):
derived_seeds = self._derive_graph_seeds(all_results)
if derived_seeds:
graph_step = RetrievalStep(
source=RetrievalSource.GRAPH,
seeds=derived_seeds,
top_k=10,
priority=1,
timeout_ms=200,
)
try:
graph_results = await self._retrieve_graph(
tenant_id, graph_step, query_embedding
)
all_results.extend(graph_results)
if debug:
debug_steps.append(
self._step_debug(
graph_step,
success=True,
elapsed_ms=0.0,
result_count=len(graph_results),
error=None,
timed_out=False,
context_filter=context_filter,
result_items=graph_results,
)
)
except Exception as e:
if debug:
debug_steps.append(
self._step_debug(
graph_step,
success=False,
elapsed_ms=0.0,
result_count=0,
error=str(e),
timed_out=False,
context_filter=context_filter,
)
)
logger.warning(
"derived_graph_expansion_failed",
extra={"error": str(e), "seeds": derived_seeds[:5]},
)
# PR 1: Strict Associative Expansion
if any(getattr(step, "associative_expansion", False) for step in plan.steps):
session_ids = set()
for r in all_results:
rec = r.get("record")
if (
rec
and hasattr(rec, "source_session_id")
and getattr(rec, "source_session_id", None)
):
session_ids.add(rec.source_session_id)
if session_ids:
try:
batch_limit = min(200, max(20, len(session_ids) * 10))
try:
extra_constraints = await self.hippocampal.store.scan(
tenant_id,
filters={
"type": [MemoryType.CONSTRAINT.value],
"source_session_id": list(session_ids),
"status": "active",
},
limit=batch_limit,
)
except Exception:
# Fallback for stores that don't support list-valued source_session_id filters.
extra_constraints = []
for sid in session_ids:
rows = await self.hippocampal.store.scan(
tenant_id,
filters={
"type": [MemoryType.CONSTRAINT.value],
"source_session_id": sid,
"status": "active",
},
limit=10,
)
extra_constraints.extend(rows)
existing_ids = {
str(getattr(x.get("record"), "id", ""))
for x in all_results
if x.get("record") is not None
}
for c in extra_constraints:
cid = str(getattr(c, "id", ""))
if cid and cid in existing_ids:
continue
if cid:
existing_ids.add(cid)
all_results.append(
{
"type": MemoryType.CONSTRAINT.value,
"source": "constraints_associative",
"text": getattr(c, "text", ""),
"confidence": getattr(c, "confidence", 0.7),
"relevance": 0.8,
"timestamp": getattr(c, "timestamp", None),
"record": c,
}
)
except Exception as e:
logger.warning(
"associative_expansion_failed", extra={"error": str(e)}, exc_info=True
)
await self._expand_temporal_contiguity(tenant_id, all_results)
elapsed_ms = (time.perf_counter() - plan_start) * 1000
plan.analysis.metadata["retrieval_meta"] = {
"sources_attempted": sorted({step.source.value for step in plan.steps}),
"sources_completed": sorted(completed_sources),
"sources_timed_out": sorted(timed_out_sources),
"sources_failed": sorted(failed_sources),
"total_elapsed_ms": round(elapsed_ms, 2),
}
if debug:
plan.analysis.metadata["retrieval_steps"] = debug_steps
return self._to_retrieved_memories(all_results, plan.analysis)
def _step_debug(
self,
step: RetrievalStep,
*,
success: bool,
elapsed_ms: float,
result_count: int,
error: str | None,
timed_out: bool,
context_filter: list[str] | None,
result_items: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
"""Build a serializable execution snapshot for explain mode."""
time_filter = dict(step.time_filter or {})
filters: dict[str, Any] = {}
if context_filter:
filters["context_tags"] = list(context_filter)
if step.memory_types:
filters["memory_types"] = list(step.memory_types)
if time_filter:
filters["time_filter"] = time_filter
if step.constraint_categories:
filters["constraint_categories"] = list(step.constraint_categories)
if step.seeds:
filters["seeds"] = list(step.seeds)
preview = []
for item in (result_items or [])[:5]:
rec = item.get("record")
preview.append(
{
"id": str(getattr(rec, "id", "")) if rec is not None else "",
"type": str(getattr(rec, "type", item.get("type", ""))),
"text": str(getattr(rec, "text", item.get("text", "")))[:180],
"relevance": float(item.get("relevance", 0.0) or 0.0),
"confidence": float(
getattr(rec, "confidence", item.get("confidence", 0.0)) or 0.0
),
}
)
return {
"source": step.source.value,
"success": success,
"elapsed_ms": round(elapsed_ms, 2),
"result_count": result_count,
"timed_out": timed_out,
"error": error,
"filters": filters,
"query_preview": step.query,
"result_preview": preview,
}
async def _execute_step_with_timeout(
self,
tenant_id: str,
step: RetrievalStep,
context_filter: list[str] | None = None,
query_embedding: list[float] | None = None,
) -> RetrievalResult:
"""Execute a step wrapped in its own timeout."""
timeout_s = step.timeout_ms / 1000.0
try:
return await asyncio.wait_for(
self._execute_step(tenant_id, step, context_filter, query_embedding),
timeout=timeout_s,
)
except TimeoutError:
logger.info(
"retrieval_step_timeout",
extra={"source": step.source.value, "timeout_ms": step.timeout_ms},
)
try:
from ..utils.metrics import RETRIEVAL_TIMEOUT_COUNT
RETRIEVAL_TIMEOUT_COUNT.labels(source=step.source.value).inc()
except Exception:
pass
return RetrievalResult(
source=step.source,
items=[],
elapsed_ms=float(step.timeout_ms),
success=False,
error=f"Timeout after {step.timeout_ms}ms",
)
async def _execute_step(
self,
tenant_id: str,
step: RetrievalStep,
context_filter: list[str] | None = None,
query_embedding: list[float] | None = None,
) -> RetrievalResult:
"""Execute a single retrieval step. Holistic: tenant-only."""
start = time.perf_counter()
try:
if step.source == RetrievalSource.FACTS:
items = await self._retrieve_facts(tenant_id, step)
elif step.source == RetrievalSource.VECTOR:
items = await self._retrieve_vector(
tenant_id, step, context_filter, query_embedding
)
elif step.source == RetrievalSource.GRAPH:
items = await self._retrieve_graph(tenant_id, step, query_embedding)
elif step.source == RetrievalSource.CONSTRAINTS:
items = await self._retrieve_constraints(
tenant_id, step, context_filter, query_embedding
)
else:
items = []
elapsed_ms = (time.perf_counter() - start) * 1000
# Phase 6.2: emit step metrics
self._record_step_metrics(step.source, elapsed_ms, len(items))
return RetrievalResult(
source=step.source,
items=items,
elapsed_ms=elapsed_ms,
success=True,
)
except Exception as e:
elapsed_ms = (time.perf_counter() - start) * 1000
logger.warning(
"retrieval_step_failed",
extra={"source": step.source.value, "error": str(e)},
exc_info=True,
)
try:
from ..utils.metrics import RETRIEVAL_STEP_FAILURES
RETRIEVAL_STEP_FAILURES.labels(source=step.source.value).inc()
except Exception:
pass # Metrics optional
return RetrievalResult(
source=step.source,
items=[],
elapsed_ms=elapsed_ms,
success=False,
error=str(e),
)
@staticmethod
def _record_step_metrics(source: RetrievalSource, elapsed_ms: float, count: int) -> None:
"""Emit Prometheus metrics for a retrieval step (best-effort)."""
try:
from ..utils.metrics import (
RETRIEVAL_STEP_DURATION,
RETRIEVAL_STEP_RESULT_COUNT,
)
RETRIEVAL_STEP_DURATION.labels(source=source.value).observe(elapsed_ms)
RETRIEVAL_STEP_RESULT_COUNT.labels(source=source.value).observe(count)
except Exception as e:
logger.debug("metrics_emit_failed", extra={"error": str(e)}, exc_info=True)
async def _retrieve_facts(
self,
tenant_id: str,
step: RetrievalStep,
) -> list[dict[str, Any]]:
"""Retrieve from semantic fact store. Holistic: tenant-only."""
results: list[dict[str, Any]] = []
if step.key:
fact = await self.neocortical.get_fact(tenant_id, step.key)
if fact:
results.append(
{
"type": MemoryType.SEMANTIC_FACT.value,
"source": "facts",
"key": fact.key,
"text": f"{fact.predicate}: {fact.value}",
"value": fact.value,
"confidence": fact.confidence,
"relevance": 1.0,
"record": fact,
}
)
if step.query:
facts = await self.neocortical.text_search(tenant_id, step.query, limit=step.top_k)
for f in facts:
results.append(
{
"type": MemoryType.SEMANTIC_FACT.value,
"source": "facts",
"key": f.get("key"),
"text": f"{f.get('key', '')}: {f.get('value', '')}",
"value": f.get("value"),
"confidence": f.get("confidence", 0.5),
"relevance": 0.8,
"context_tags": f.get("context_tags", []),
"timestamp": f.get("valid_from") or f.get("updated_at"),
"record": f,
}
)
return results[: step.top_k]
async def _retrieve_vector(
self,
tenant_id: str,
step: RetrievalStep,
context_filter: list[str] | None = None,
query_embedding: list[float] | None = None,
) -> list[dict[str, Any]]:
"""Retrieve via vector similarity search. Holistic: tenant-only."""
filters = None
if step.time_filter or step.memory_types or step.min_confidence > 0:
filters = {}
if step.time_filter:
filters.update(step.time_filter)
if step.memory_types:
filters["type"] = step.memory_types
if step.min_confidence > 0:
filters["min_confidence"] = step.min_confidence
records = await self.hippocampal.search(
tenant_id,
query=step.query or "",
top_k=step.top_k,
context_filter=context_filter,
filters=filters,
query_embedding=query_embedding,
)
return [
{
"type": "episode",
"source": "vector",
"text": r.text,
"confidence": r.confidence,
"relevance": r.metadata.get("_similarity", 0.5),
"timestamp": r.timestamp,
"record": r,
}
for r in records
]
@staticmethod
def _derive_graph_seeds(
all_results: list[dict[str, Any]],
max_seeds: int = 10,
) -> list[str]:
"""Derive entity seeds from top constraint/fact retrieval results.
Looks at entity mentions, subjects, and scope values from constraint
and fact results so graph expansion can fire even when the original
query had no recognised entities.
when provided (e.g. "food", "finance") so domain-anchored graph
traversal can bridge semantic disconnect.
"""
seeds: list[str] = []
seen: set[str] = set()
def _add(name: str) -> None:
normalised = name.strip().lower()
if normalised and normalised not in seen and len(normalised) > 1:
seen.add(normalised)
seeds.append(name.strip())
for item in all_results:
if len(seeds) >= max_seeds:
break
rec = item.get("record")
if rec is None:
continue
for ent in getattr(rec, "entities", None) or []:
name = getattr(ent, "normalized", None) or getattr(ent, "text", None) or str(ent)
_add(name)
meta = getattr(rec, "metadata", None)
if isinstance(meta, dict):
for c in meta.get("constraints", []):
if isinstance(c, dict):
for field_name in ("subject", "scope"):
val = c.get(field_name)
if isinstance(val, str):
_add(val)
subj = meta.get("subject")
if isinstance(subj, str):
_add(subj)
return seeds[:max_seeds]
@staticmethod
def _encoding_order(rows: list[Any]) -> list[Any]:
"""Sort a session's records into the order they were encoded.
``timestamp`` is the obvious key and is the wrong one wherever a whole session
shares a single stamp — in the LoCoMo corpus all 28 turns of a session carry the
identical timestamp, so ordering by it is arbitrary and "neighbours" would be a
random sample of the session. ``written_at`` is no better: concurrent ingestion
workers scramble it (turn order 0, 4, 3, 6, 7, 5 was observed).
So prefer an explicit ``turn_idx`` when the writer supplied one for every row,
and fall back to timestamp otherwise. Mixed populations take the fallback rather
than interleaving two incomparable keys.
"""
indices = [(r.metadata or {}).get("turn_idx") for r in rows]
if indices and all(isinstance(i, int) for i in indices):
return sorted(rows, key=lambda r: (r.metadata or {})["turn_idx"])
return sorted(rows, key=lambda r: r.timestamp)
async def _expand_temporal_contiguity(
self,
tenant_id: str,
all_results: list[dict[str, Any]],
) -> None:
"""Pull the turns encoded around each top vector hit into the candidate set.
Human recall shows a temporal contiguity effect: retrieving one item preferentially
cues items encoded at nearby positions, and the effect survives many intervening
memories. In a conversation log "adjacent in encoding" is exact and free, so this is
an ordered timestamp lookup rather than new structure.
The failure it targets: a query often matches the turn that *asks* something while
the answer is in the turn after it, or matches a reply whose referent is the turn
before. Pure similarity retrieves the match and drops the answer.
Neighbours enter below their seed and below the packet's episode threshold default,
so they act as context that the reranker may promote — never as competitors that
displace a directly-matching episode. Mutates ``all_results`` in place.
"""
from ..core.config import get_settings
settings = get_settings()
if not settings.features.temporal_contiguity_enabled:
return
k = settings.features.temporal_contiguity_neighbours
seeds = [
r
for r in all_results
if r.get("source") == "vector"
and getattr(r.get("record"), "source_session_id", None)
and getattr(r.get("record"), "timestamp", None)
]
if not seeds:
return
seeds.sort(key=lambda r: r.get("relevance", 0.0), reverse=True)
existing_ids = {
str(getattr(x.get("record"), "id", "")) for x in all_results if x.get("record")
}
# One scan per seed, so bound the seeds rather than the total work per seed.
for seed in seeds[:_CONTIGUITY_MAX_SEEDS]:
record = seed["record"]
ts = record.timestamp
try:
window = timedelta(minutes=_CONTIGUITY_WINDOW_MINUTES)
rows = await self.hippocampal.store.scan(
tenant_id,
filters={
"source_session_id": record.source_session_id,
"status": "active",
"since": ts - window,
"until": ts + window,
},
limit=_CONTIGUITY_SESSION_SCAN_LIMIT,
)
except Exception as exc:
logger.warning("temporal_contiguity_failed", extra={"error": str(exc)})
return
for row in _neighbours_of(record, rows, k):
rid = str(getattr(row, "id", ""))
if not rid or rid in existing_ids:
continue
existing_ids.add(rid)
all_results.append(
{
"type": row.type.value,
"source": "contiguity",
"text": row.text,
"confidence": getattr(row, "confidence", 0.7),
"relevance": seed.get("relevance", 0.5) * _CONTIGUITY_RELEVANCE_FACTOR,
"timestamp": row.timestamp,
"record": row,
}
)
async def _retrieve_graph(
self,
tenant_id: str,
step: RetrievalStep,
query_embedding: list[float] | None = None,
) -> list[dict[str, Any]]:
"""Retrieve via knowledge graph PPR. Holistic: tenant-only."""
if not step.seeds:
return []
results = await self.neocortical.multi_hop_query(tenant_id, seed_entities=step.seeds)
if not results:
return []
# Graph scores are raw Neo4j co-occurrence counts on an unbounded scale — 315,
# 744 have both been observed — while every other prong emits cosine-like 0..1.
# Ranking is done on the raw value before the reranker's clamp ever runs, so
# unnormalised graph hits take every top-k slot and push real episodes below
# `episode_relevance_threshold`, which drops them from the packet entirely.
# That was invisible for as long as the graph stayed empty. Rank-normalise into
# the same band the other prongs use, preserving order among graph hits, which
# a flat clamp to 1.0 would destroy.
raw = [float(r.get("relevance_score", 0.5) or 0.0) for r in results]
hi = max(raw, default=0.0)
lo = min(raw, default=0.0)
span = hi - lo
# PPR ranks entities; entities are an *index*, not an answer. Returning the
# neighbourhood profile ("Entity: user\n - LOCATION: Seattle") is the documented
# Entity-Only failure mode — measured here as multi-hop 0.33 -> 0.23/0.27/0.26 on
# every populated-graph arm. Resolve each entity to the episodic records its
# relations cite instead, and let the grounded text answer the question. An
# episode reachable from several entities keeps the best score.
best: dict[str, float] = {}
for r, score in zip(results, raw, strict=True):
normalised = (
GRAPH_RELEVANCE_CEILING
if span <= 0
else GRAPH_RELEVANCE_FLOOR
+ (score - lo) / span * (GRAPH_RELEVANCE_CEILING - GRAPH_RELEVANCE_FLOOR)
)
for rel in r.get("relations", []) or []:
props = rel.get("relation_properties") or {}
for rid in props.get("evidence_ids") or []:
if normalised > best.get(str(rid), 0.0):
best[str(rid)] = normalised
if not best:
return []
# Ten entities resolved to 309 distinct records on a real tenant — get_entity_facts_batch
# has no LIMIT, so one hub entity drags in hundreds of edges. Feeding all of them to the
# reranker swamps every other prong and costs a needless fetch. Keep the best-scoring
# `top_k`, which is also the first time this step's top_k has ever been read: the planner
# has always set 15 (multi-hop) / 10 (constraint) and multi_hop_query never accepted it.
ranked = sorted(best.items(), key=lambda kv: kv[1], reverse=True)[: step.top_k]
ids: list[UUID] = []
for rid, _score in ranked:
try:
ids.append(UUID(rid))
except (ValueError, AttributeError, TypeError):
continue
records = await self.hippocampal.store.get_by_ids_batch(ids)
items = []
for record in records:
# get_by_ids_batch is a bare id lookup with no tenant predicate. Edge
# evidence comes from this tenant's own graph so this should never fire,
# but cross-tenant leakage is not a failure worth trusting transitively.
if record.tenant_id != tenant_id:
continue
items.append(
{
"type": record.type.value,
"source": "graph",
"text": record.text,
"relevance": self._graph_relevance(
record, best.get(str(record.id), GRAPH_RELEVANCE_FLOOR), query_embedding
),
"record": record,
}
)
return items
@staticmethod
def _graph_relevance(
record: Any,
graph_score: float,
query_embedding: list[float] | None,
) -> float:
"""Rank a graph-resolved episode by how well it answers *this* query.
The graph decides which records are candidates; it must not decide how they
rank. Scoring them by the traversal score put every graph hit in a constant
0.55-0.85 band irrespective of the question, which sits above the median vector
cosine (~0.62) — so graph candidates displaced better-matching vector hits and
every factual category fell (measured: overall 0.486 -> 0.429, single-hop
-0.097, temporal -0.098, multi-hop -0.090, while adversarial *rose* +0.074
because the packet got worse and the model refused more).
Cosine against the query is the same scale the vector prong emits, so the two
compete on merit. The traversal score is kept only as the fallback for a record
with no stored embedding, which would otherwise silently rank at zero.
"""
if query_embedding and record.embedding:
return cosine_similarity(query_embedding, record.embedding)
return graph_score
async def _retrieve_constraints(
self,
tenant_id: str,
step: RetrievalStep,
context_filter: list[str] | None = None,
query_embedding: list[float] | None = None,
) -> list[dict[str, Any]]:
"""Retrieve active constraints via vector search + semantic fact lookup.
Two-pronged approach:
1. Vector search filtered to MemoryType.CONSTRAINT (episodic constraint records).
2. Semantic fact lookup for cognitive categories (goal, value, state, causal, policy).
"""
results: list[dict[str, Any]] = []
# 1. Vector search for episodic constraint records
constraint_filters: dict[str, Any] = {
"type": [MemoryType.CONSTRAINT.value],
"status": "active",
"exclude_expired": True,
}
if step.time_filter:
for k in ("since", "until", "source_session_id", "time_basis"):
if k in step.time_filter:
constraint_filters[k] = step.time_filter[k]
try:
records = await self.hippocampal.search(
tenant_id,
query=step.query or "",
top_k=step.top_k,
context_filter=context_filter,
filters=constraint_filters,
query_embedding=query_embedding,
)
for r in records:
results.append(
{
"type": MemoryType.CONSTRAINT.value,
"source": "constraints",
"text": r.text,
"confidence": r.confidence,
"relevance": r.metadata.get("_similarity", 0.7),
"timestamp": r.timestamp,
"record": r,
}
)
except Exception:
logger.warning("constraint_vector_search_failed", exc_info=True)
# 2. Semantic fact lookup for cognitive constraint categories
from ..memory.neocortical.schemas import FactCategory
cognitive_categories: list[FactCategory] = []
if step.constraint_categories:
for cat_str in step.constraint_categories:
try:
cognitive_categories.append(FactCategory(cat_str.lower()))
except ValueError:
pass
try:
facts = []
if not cognitive_categories:
pass
else:
used_batch = False
batch_fetch = getattr(self.neocortical.facts, "get_facts_by_categories", None)
if callable(batch_fetch):
maybe_coro = batch_fetch(
tenant_id,
cognitive_categories,
current_only=True,
limit=max(step.top_k * max(1, len(cognitive_categories)), step.top_k),
)
if inspect.isawaitable(maybe_coro):
facts = await maybe_coro
used_batch = True
if not used_batch:
for category in cognitive_categories:
facts.extend(
await self.neocortical.facts.get_facts_by_category(
tenant_id, category, current_only=True, limit=step.top_k
)
)
existing_texts = {str(r.get("text", "")) for r in results}
for fact in facts:
# Skip garbage facts: value must be a meaningful phrase (>3 words)
fact_val = str(fact.value).strip() if fact.value else ""
if len(fact_val.split()) < 4:
continue
fact_category = getattr(fact, "category", None)
cat_label = (
fact_category.value
if isinstance(fact_category, FactCategory)
else str(fact_category or "fact")
)
fact_text = f"[{cat_label.title()}] {fact_val}"
if fact_text in existing_texts:
continue
existing_texts.add(fact_text)
results.append(
{
"type": MemoryType.CONSTRAINT.value,
"source": "constraints",
"key": fact.key,
"text": fact_text,
"value": fact.value,
"confidence": fact.confidence,
"relevance": 0.75,
"record": fact,
}
)
except Exception:
logger.warning("constraint_fact_search_failed", exc_info=True)
rescored = self._rescore_constraints(rows=results)
return rescored[: step.top_k]
def _rescore_constraints(self, *, rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
if not rows:
return rows
rows.sort(
key=lambda x: (
float(x.get("relevance", 0.0)),
float(x.get("confidence", 0.0)),
),
reverse=True,
)
return rows
def _to_retrieved_memories(
self,
items: list[dict[str, Any]],
analysis: QueryAnalysis,
) -> list[RetrievedMemory]:
"""Convert raw items to RetrievedMemory objects."""
seen_texts: set = set()
retrieved: list[RetrievedMemory] = []
for item in items:
text = item.get("text", "")
if text in seen_texts:
continue
seen_texts.add(text)
record = item.get("record")
if isinstance(record, MemoryRecord):
mem_record = record
elif hasattr(record, "key") and hasattr(record, "value"):
mem_record = self._fact_to_record(record, item)
else:
mem_record = self._dict_to_record(item)
retrieved.append(
RetrievedMemory(
record=mem_record,
relevance_score=item.get("relevance", 0.5),
retrieval_source=item.get("source", "unknown"),
)
)
retrieved.sort(key=lambda x: x.relevance_score, reverse=True)
return retrieved
def _fact_to_record(self, fact: Any, item: dict[str, Any]) -> MemoryRecord:
"""Build MemoryRecord from SemanticFact-like object. Holistic: context_tags."""
from uuid import UUID
text = (
item.get("text", "")
or f"{getattr(fact, 'predicate', '')}: {getattr(fact, 'value', '')}"
)
fid = getattr(fact, "id", None)
context_tags = getattr(fact, "context_tags", None) or []
mem_type = MemoryType.SEMANTIC_FACT
if item.get("type") == MemoryType.CONSTRAINT.value or item.get("source") == "constraints":
mem_type = MemoryType.CONSTRAINT
category = getattr(fact, "category", None)
cat_value = (
category.value
if (category is not None and hasattr(category, "value"))
else str(category or "")
)
evidence_ids = getattr(fact, "evidence_ids", None) or []
valid_from = getattr(fact, "valid_from", None)
valid_to = getattr(fact, "valid_to", None)
supersedes_id = getattr(fact, "supersedes_id", None)
metadata: dict[str, Any] = {}
if mem_type == MemoryType.CONSTRAINT and cat_value:
metadata["constraints"] = [
{
"constraint_type": cat_value,
"evidence_ids": list(evidence_ids),
}
]
if valid_from:
metadata["valid_from"] = str(valid_from)
if valid_to:
metadata["valid_to"] = str(valid_to)
if supersedes_id:
metadata["supersedes_id"] = str(supersedes_id)
record_timestamp = valid_from or getattr(fact, "updated_at", None) or datetime.now(UTC)
return MemoryRecord(
id=UUID(fid) if fid else uuid4(),
tenant_id=getattr(fact, "tenant_id", ""),
context_tags=list(context_tags),
source_session_id=None,
type=mem_type,
text=text,
key=getattr(fact, "key", None),
confidence=getattr(fact, "confidence", 0.5),
importance=0.5,
provenance=Provenance(
source=MemorySource.AGENT_INFERRED,
evidence_refs=list(evidence_ids),
),
timestamp=record_timestamp,
written_at=getattr(fact, "created_at", datetime.now(UTC)),
metadata=metadata,
)
def _dict_to_record(self, item: dict[str, Any]) -> MemoryRecord:
"""Create minimal MemoryRecord from dict. Holistic: context_tags."""
mem_type = item.get("type", "episodic_event")
try:
mtype = MemoryType(mem_type)
except ValueError:
mtype = MemoryType.EPISODIC_EVENT
return MemoryRecord(
id=uuid4(),
tenant_id=item.get("tenant_id", ""),
context_tags=item.get("context_tags", []),
source_session_id=item.get("source_session_id"),
type=mtype,
text=item.get("text", ""),
key=item.get("key"),
confidence=item.get("confidence", 0.5),
importance=0.5,
provenance=Provenance(source=MemorySource.AGENT_INFERRED),
timestamp=item.get("timestamp", datetime.now(UTC)),
written_at=datetime.now(UTC),
)
"""Hybrid retriever executing plans across memory sources."""
import asyncio
import inspect
import time
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from typing import Any
from uuid import UUID, uuid4
from ..core.enums import MemorySource, MemoryType
from ..core.schemas import MemoryRecord, Provenance, RetrievedMemory
from ..memory.hippocampal.store import HippocampalStore
from ..memory.neocortical.store import NeocorticalStore
from ..utils.logging_config import get_logger
from ..utils.similarity import cosine_similarity
from .planner import RetrievalPlan, RetrievalSource, RetrievalStep
from .query_types import QueryAnalysis
logger = get_logger(__name__)
# Band that graph hits are rank-normalised into. The ceiling sits below 1.0 on purpose:
# an entity profile summarises a neighbourhood rather than answering a question, so it
# should compete with strong episodes without automatically displacing them. The floor
# keeps the weakest graph hit above the packet's episode_relevance_threshold default.
GRAPH_RELEVANCE_FLOOR = 0.55
GRAPH_RELEVANCE_CEILING = 0.85
# Temporal contiguity expansion. One scan per seed, so the seed count is what bounds the
# work; the window is wide enough to cover a conversational exchange without spilling into
# the next session, and neighbours enter strictly below their seed so context can never
# outrank the episode that actually matched the query.
_CONTIGUITY_MAX_SEEDS = 3
_CONTIGUITY_WINDOW_MINUTES = 30
_CONTIGUITY_RELEVANCE_FACTOR = 0.7
# The window can cover a whole session when every turn shares one timestamp, so the scan
# must be able to return the session before neighbours are picked out of it in order.
_CONTIGUITY_SESSION_SCAN_LIMIT = 200
def _neighbours_of(seed: Any, rows: list[Any], k: int) -> list[Any]:
"""The k rows either side of ``seed`` in encoding order, seed excluded.
Selecting the window in Python rather than in SQL is what makes this correct when a
session's rows are not separable by timestamp: the database cannot order them, but it
can hand over the session, and the ordering key lives in metadata.
"""
ordered = HybridRetriever._encoding_order(rows)
position = next((i for i, r in enumerate(ordered) if r.id == seed.id), None)
if position is None:
return []
return ordered[max(0, position - k) : position] + ordered[position + 1 : position + 1 + k]
class RetrievalResult:
"""Result from a single retrieval step."""
source: RetrievalSource
items: list[dict[str, Any]]
elapsed_ms: float
success: bool
error: str | None = None
class HybridRetriever:
"""Executes retrieval plans across multiple memory sources.
Enforces per-step and total retrieval timeouts so that slow sources
(e.g. Neo4j GDS) don't dominate tail latency.
"""
def __init__(
self,
hippocampal: HippocampalStore,
neocortical: NeocorticalStore,
):
self.hippocampal = hippocampal
self.neocortical = neocortical
async def retrieve(
self,
tenant_id: str,
plan: RetrievalPlan,
context_filter: list[str] | None = None,
query_embedding: list[float] | None = None,
debug: bool = False,
) -> list[RetrievedMemory]:
"""Execute a retrieval plan with enforced timeouts. Holistic: tenant-only."""
from ..core.config import get_settings
settings = get_settings()
timeouts_enabled = settings.features.retrieval_timeouts_enabled
cross_skip = settings.features.skip_if_found_cross_group
all_results: list[dict[str, Any]] = []
plan_start = time.perf_counter()
plan_budget = plan.total_timeout_ms / 1000.0
skip_remaining = False
completed_sources: set[str] = set()
Xet Storage Details
- Size:
- 44.7 kB
- Xet hash:
- eb4c89abe9d063cf7137cf285d561cbe256f9afb98dd0d6e080c30f2521c1130
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.