Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
feat(context): grade retrieved facts with trust tier and add min_trust_tier filter (#1703)
Kevinb901bafefefd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0ac44596..abf2849a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Added- `DecisionRecorder` and `AgentContext` gain opt-in `evaluators`/`eval_config` constructor parameters that run `semantica.evals` evaluators (e.g. `decision_scores`) automatically during `record_decision()`, storing `eval_score`/`eval_passed`/`eval_details` in `Decision.metadata`. Fully backward-compatible: omitting `evaluators` leaves recording behavior unchanged. Evaluator failures are logged and never block decision persistence. Wired for the `graph_store` backend only; the `context_graph` `AgentContext` backend is a known follow-up.+- **Trust-tier grading of retrieved context** (#1703, phase 2 of #1557) by @pkupt+ - `ContextRetriever` now grades every retrieved fact on evidence quality and records the tier in `RetrievedContext.metadata["trust_tier"]` (`quarantine` / `bronze` / `silver` / `gold`). Tiers are computed at query time from signals already present in the result — graph node confidence and, when a `provenance_manager` is configured, the number of distinct sources from `get_all_sources` — and are never persisted, so grades stay current as source sets change+ - New `filter_by_tier(minimum, include_unscored=False)` on `ContextRetriever`, alongside the existing score/type/date filters. Graph facts without readable signals grade to `quarantine` (missing evidence is never trusted as high confidence); DRIFT's verified local facts are graded the same way as graph edges. Community-report summaries from global and DRIFT search carry no per-fact signals and stay unscored, and a threshold keeps them rather than emptying the response. Without a `provenance_manager` there is no corroboration signal, so facts grade `bronze` at best and a `silver` or `gold` threshold needs one+ - `retrieve` accepts `min_trust_tier` in all four modes (local, global, drift, hybrid). The local leg drops unscored items, while the global, DRIFT and hybrid legs keep unscored summaries and still drop facts graded below the threshold- **`RelationalSchemaMapper`** (#1386, part 1) by @costajohnt- `semantica.kg.RelationalSchemaMapper` maps rows from a relational source (`DBIngestor`, `SnowflakeIngestor`, `DatabricksIngestor`, `PandasIngestor`, a DataFrame or plain row dicts) to the `{"entities", "relationships"}` shape `GraphBuilder` and `OntologyGenerator` consume: entity tables become entities keyed by primary key, foreign keys become typed relationships, junction tables become relationships onlydiff --git a/semantica/context/context_retriever.py b/semantica/context/context_retriever.pyindex 43120b35..54a61541 100644--- a/semantica/context/context_retriever.py+++ b/semantica/context/context_retriever.py@@ -78,7 +78,7 @@ License: MITfrom datetime import datetime, timezonefrom dataclasses import dataclass, field-from typing import Any, Dict, List, Optional, Union+from typing import Any, Dict, List, Optional, Tuple, Unionfrom ..utils.logging import get_loggerfrom ..utils.progress_tracker import get_progress_tracker@@ -90,6 +90,7 @@ from ..kg.community_detector import CommunityDetectorfrom ..kg.similarity_calculator import SimilarityCalculatorfrom ..utils.exceptions import ValidationErrorfrom .truth_maintenance_filter import TruthMaintenanceContextFilter+from .tiers import TierCalculator, TrustTiertry:from ..kg.temporal_query import TemporalGraphQuery as _TemporalGraphQueryfrom ..kg.temporal_model import parse_temporal_value as _parse_temporal_value@@ -148,6 +149,10 @@ class ContextRetriever:- max_expansion_hops: Maximum graph expansion hops (default: 2)- hybrid_alpha: Weight for hybrid retrieval (0=vector only, 1=graphonly, default: 0.5)+ - tier_calculator: Optional TierCalculator override for+ evidence grading+ - provenance_manager: Optional provenance manager for+ corroboration lookup by node id"""self.logger = get_logger("context_retriever")self.config = config or {}@@ -207,6 +212,13 @@ class ContextRetriever:use_graph_features=True)+ # Trust-tiering: grade each retrieved fact on evidence quality so+ # downstream consumers can downrank or filter low-trust context. Tiers+ # are derived on demand from signals already present in the retrieval+ # result (graph node confidence + corroboration count), never persisted.+ self.tier_calculator = self.config.get("tier_calculator") or TierCalculator()+ self.provenance_manager = self.config.get("provenance_manager")+def retrieve(self,query: str,@@ -215,6 +227,7 @@ class ContextRetriever:min_relevance_score: float = 0.0,mode: str = "local",truth_filter: Optional[TruthMaintenanceContextFilter] = None,+ min_trust_tier: Optional[TrustTier] = None,**options,) -> List[RetrievedContext]:"""@@ -230,6 +243,18 @@ class ContextRetriever:candidates are validated against a session snapshot beforeranking and the snapshot version is re-checked beforeresults are returned+ min_trust_tier: Optional minimum TrustTier. When set, each result+ is graded on evidence quality (see the "trust_tier" key in+ RetrievedContext.metadata) and only results meeting the+ minimum are returned. Graph facts without readable signals+ grade to quarantine. Without a ``provenance_manager`` (or a+ ``corroboration_count`` in metadata) facts have no+ corroboration signal and grade bronze at best, so a silver or+ gold threshold needs one. In local mode, results that carry no+ tier signal (vector chunks with neither a graph node id nor a+ confidence reading) are excluded too; global, drift and+ hybrid keep such unscored summaries so a threshold cannot+ empty the response.**options: Additional options:- entity_ids: Filter by entity IDs- node_types: Filter by node types@@ -254,6 +279,7 @@ class ContextRetriever:max_results=max_results,min_relevance_score=min_relevance_score,as_contexts=True,+ min_trust_tier=min_trust_tier,**options,)elif norm_mode == "drift":@@ -262,6 +288,7 @@ class ContextRetriever:max_results=max_results,min_relevance_score=min_relevance_score,as_contexts=True,+ min_trust_tier=min_trust_tier,**options,)elif norm_mode == "hybrid":@@ -271,6 +298,7 @@ class ContextRetriever:use_graph_expansion=use_graph_expansion,min_relevance_score=min_relevance_score,mode="local",+ min_trust_tier=min_trust_tier,**options,)global_res = self.retrieve_global(@@ -278,6 +306,7 @@ class ContextRetriever:max_results=max_results * 2,min_relevance_score=min_relevance_score,as_contexts=True,+ min_trust_tier=min_trust_tier,**options,)merged = self._rank_and_merge(local_res + global_res, query)@@ -287,6 +316,11 @@ class ContextRetriever:else max(0.0, min(1.0, min_relevance_score)))filtered = [r for r in merged if r.score >= eff_min_score]+ # Tiers were attached and the threshold applied inside each+ # sub-retrieval: the local leg drops unscored items, while the+ # global leg keeps structurally unscored summaries. Filtering the+ # merged list again would strip those summaries out.+ self._attach_trust_tiers(filtered)return filtered[:max_results]elif norm_mode != "local":raise ValueError(@@ -374,6 +408,11 @@ class ContextRetriever:filtered_results = [r for r in ranked_results if r.score >= eff_min_score]+ self._attach_trust_tiers(filtered_results)+ if min_trust_tier is not None:+ filtered_results = self.filter_by_tier(+ filtered_results, min_trust_tier+ )if snapshot is not None:truth_filter.assert_current(snapshot)@@ -392,6 +431,153 @@ class ContextRetriever:)raise+ # ------------------------------------------------------------------+ # Trust tiering (issue #1557, phase 2)+ # ------------------------------------------------------------------++ def _tier_signals_for(+ self, context: "RetrievedContext"+ ) -> Tuple[Optional[int], Optional[float]]:+ """+ Pull the two signals a TrustTier needs from a retrieved context.++ Corroboration count comes from the provenance manager when one is+ wired and the context carries a node id; otherwise it falls back to a+ ``corroboration_count`` already present in metadata. The provenance+ count is the number of distinct ``source`` values, since a lineage+ trace can list the same document more than once. Confidence comes+ from ``metadata["confidence"]``. Either signal may be None when the+ retrieval source does not expose it (e.g. a vector chunk with no graph+ provenance).++ Args:+ context: A retrieved context item.++ Returns:+ Tuple of (corroboration_count, confidence); None for a missing+ signal.+ """+ node_id = context.metadata.get("node_id")+ confidence = context.metadata.get("confidence")++ corroboration: Optional[int] = None+ if self.provenance_manager is not None and node_id:+ try:+ sources = self.provenance_manager.get_all_sources(node_id) or []+ corroboration = len({+ s.get("source")+ for s in sources+ if isinstance(s, dict) and s.get("source")+ })+ except Exception:+ corroboration = None+ if corroboration is None:+ candidate = context.metadata.get("corroboration_count")+ if isinstance(candidate, int):+ corroboration = candidate++ return corroboration, confidence++ def _attach_trust_tiers(self, results: List["RetrievedContext"]) -> None:+ """+ Grade each result on evidence quality and record it in metadata.++ Graph facts (results carrying a ``node_id``, and edge facts carrying+ ``source``, ``relation`` and ``target`` such as DRIFT's verified local+ facts) always receive a tier: when neither corroboration nor+ confidence is readable, the calculator degrades them to quarantine+ instead of leaving the grade blank, so a missing signal is never+ treated as if it had been measured. Other results (for example vector+ chunks or community summaries with no confidence reading) are left+ unscored. An existing ``trust_tier`` is+ recomputed from current signals on every pass, so a grade cannot+ outlive the evidence it was derived from.++ The tier is recorded on a copy of the result's metadata. Metadata+ dicts on retrieved contexts can be shared with the underlying vector+ store or memory, and writing in place would persist a query-time+ grade into stored state, where it would go stale as sources change.++ Args:+ results: Retrieved contexts to grade in place.+ """+ for context in results:+ corroboration, confidence = self._tier_signals_for(context)+ meta = context.metadata+ is_graph_fact = bool(meta.get("node_id")) or bool(+ meta.get("source") and meta.get("relation") and meta.get("target")+ )+ if not is_graph_fact and corroboration is None and confidence is None:+ continue+ try:+ tier = self.tier_calculator.calculate(corroboration, confidence)+ except Exception:+ continue+ context.metadata = {**context.metadata, "trust_tier": tier.value}++ def filter_by_tier(+ self,+ results: List["RetrievedContext"],+ minimum: "TrustTier",+ include_unscored: bool = False,+ ) -> List["RetrievedContext"]:+ """+ Keep only results whose trust tier meets a minimum threshold.++ Args:+ results: Retrieved contexts, ideally already graded via+ ``_attach_trust_tiers`` (or ``retrieve`` with tiering enabled).+ minimum: Lowest acceptable tier.+ include_unscored: When True, results that carry no tier at all are+ also kept; when False (default) they are dropped, because a+ caller asking for "at least silver" should not receive context+ whose evidence quality is unknown.++ Returns:+ The subset of results meeting the threshold.++ Raises:+ TypeError: If minimum is not a TrustTier.++ Example:+ >>> from semantica.context.tiers import TrustTier+ >>> r_gold = RetrievedContext(content="g", score=1.0,+ ... metadata={"trust_tier": "gold"})+ >>> r_bronze = RetrievedContext(content="b", score=1.0,+ ... metadata={"trust_tier": "bronze"})+ >>> r_none = RetrievedContext(content="n", score=1.0)+ >>> retriever = ContextRetriever()+ >>> kept = retriever.filter_by_tier(+ ... [r_gold, r_bronze, r_none], TrustTier.SILVER)+ >>> [r.content for r in kept]+ ['g']+ >>> kept_all = retriever.filter_by_tier(+ ... [r_gold, r_bronze, r_none], TrustTier.SILVER,+ ... include_unscored=True)+ >>> sorted(r.content for r in kept_all)+ ['g', 'n']+ """+ if not isinstance(minimum, TrustTier):+ raise TypeError(+ f"minimum must be a TrustTier, got {type(minimum).__name__}"+ )+ kept: List[RetrievedContext] = []+ for context in results:+ raw = context.metadata.get("trust_tier")+ if raw is None:+ if include_unscored:+ kept.append(context)+ continue+ try:+ tier = TrustTier(raw)+ except ValueError:+ if include_unscored:+ kept.append(context)+ continue+ if tier.meets(minimum):+ kept.append(context)+ return kept+def retrieve_global(self,query: str,@@ -399,6 +585,7 @@ class ContextRetriever:max_results: int = 5,min_relevance_score: float = 0.0,as_contexts: bool = False,+ min_trust_tier: Optional["TrustTier"] = None,**options,) -> Union[Any, List[RetrievedContext]]:"""@@ -411,6 +598,10 @@ class ContextRetriever:min_relevance_score: Minimum relevance scoreas_contexts: If True, return List[RetrievedContext];if False, return GlobalSearchResult+ min_trust_tier: Optional trust threshold. Summaries with no+ evidence signal stay unscored and are always kept when a+ threshold is set; contexts that do carry a signal are graded+ and filtered like any other result.**options: Additional options passed to GlobalGraphRetrieverReturns:@@ -455,6 +646,14 @@ class ContextRetriever:c for c in res.to_retrieved_contexts()if c.score >= context_threshold]+ self._attach_trust_tiers(contexts)+ if min_trust_tier is not None:+ # Summaries with no evidence signal stay unscored; dropping+ # them for "missing tier" would empty the result. Keep them;+ # only graded items are filtered.+ contexts = self.filter_by_tier(+ contexts, min_trust_tier, include_unscored=True+ )return contexts[:max_results]return res@@ -465,6 +664,7 @@ class ContextRetriever:max_results: int = 5,min_relevance_score: float = 0.0,as_contexts: bool = False,+ min_trust_tier: Optional["TrustTier"] = None,**options,) -> Union[Any, List[RetrievedContext]]:"""@@ -477,6 +677,11 @@ class ContextRetriever:min_relevance_score: Minimum relevance scoreas_contexts: If True, return List[RetrievedContext];if False, return DriftSearchResult+ min_trust_tier: Optional trust threshold. The DRIFT answer is a+ summary with no evidence signal and is always kept when a+ threshold is set. Verified local facts are graph edges, so+ they are always graded (quarantine without signals) and+ filtered.**options: Additional options passed to DriftSearchEngineReturns:@@ -523,6 +728,13 @@ class ContextRetriever:c for c in res.to_retrieved_contexts()if c.score >= context_threshold]+ self._attach_trust_tiers(contexts)+ if min_trust_tier is not None:+ # The DRIFT answer is an unscored summary and is kept; the+ # verified local facts are graded as graph facts and filtered.+ contexts = self.filter_by_tier(+ contexts, min_trust_tier, include_unscored=True+ )return contexts[:max_results]return resdiff --git a/tests/context/test_context_trust_tiers.py b/tests/context/test_context_trust_tiers.pynew file mode 100644index 00000000..ea9b33c1--- /dev/null+++ b/tests/context/test_context_trust_tiers.py@@ -0,0 +1,371 @@+"""+Tests for ContextRetriever trust-tiering (issue #1557, phase 2).++Verifies that retrieved facts are graded on evidence quality and that the+resulting tier can be filtered at retrieval time.++Run with: pytest tests/context/test_context_trust_tiers.py+"""+from unittest.mock import MagicMock, patch++import pytest++from semantica.context.context_retriever import ContextRetriever, RetrievedContext+from semantica.context.tiers import TrustTier+++def _ctx(content, score=1.0, **meta):+ """Build a RetrievedContext with the given metadata overrides."""+ return RetrievedContext(content=content, score=score, metadata=dict(meta))+++# ----------------------------------------------------------------------+# Signal extraction+# ----------------------------------------------------------------------+++def test_tier_signals_from_metadata_only():+ retriever = ContextRetriever()+ corr, conf = retriever._tier_signals_for(_ctx("f", confidence=0.9))+ assert corr is None+ assert conf == 0.9+++def test_tier_signals_from_corroboration_count_metadata():+ retriever = ContextRetriever()+ corr, conf = retriever._tier_signals_for(+ _ctx("f", confidence=0.9, corroboration_count=3)+ )+ assert corr == 3+ assert conf == 0.9+++def test_tier_signals_uses_provenance_manager():+ pm = MagicMock()+ pm.get_all_sources.return_value = [{"source": "a"}, {"source": "b"}]+ retriever = ContextRetriever(provenance_manager=pm)+ corr, conf = retriever._tier_signals_for(+ _ctx("f", node_id="ent_1", confidence=0.9)+ )+ assert corr == 2+ assert conf == 0.9+ pm.get_all_sources.assert_called_once_with("ent_1")+++def test_tier_signals_provenance_failure_falls_back_to_metadata():+ pm = MagicMock()+ pm.get_all_sources.side_effect = RuntimeError("store down")+ retriever = ContextRetriever(provenance_manager=pm)+ corr, conf = retriever._tier_signals_for(+ _ctx("f", node_id="ent_1", confidence=0.9, corroboration_count=1)+ )+ assert corr == 1+ assert conf == 0.9+++# ----------------------------------------------------------------------+# Attachment+# ----------------------------------------------------------------------+++def test_attach_tier_from_confidence_only():+ retriever = ContextRetriever()+ ctx = _ctx("f", confidence=0.9)+ retriever._attach_trust_tiers([ctx])+ assert ctx.metadata["trust_tier"] == TrustTier.BRONZE.value+++def test_attach_tier_combines_corroboration_and_confidence():+ retriever = ContextRetriever()+ ctx = _ctx("f", confidence=0.9, corroboration_count=3)+ retriever._attach_trust_tiers([ctx])+ assert ctx.metadata["trust_tier"] == TrustTier.GOLD.value+++def test_attach_leaves_unscored_items_untouched():+ retriever = ContextRetriever()+ ctx = _ctx("plain vector chunk") # no node_id, no confidence+ retriever._attach_trust_tiers([ctx])+ assert "trust_tier" not in ctx.metadata+++def test_attach_idempotent():+ retriever = ContextRetriever()+ ctx = _ctx("f", confidence=0.9)+ retriever._attach_trust_tiers([ctx])+ first = ctx.metadata["trust_tier"]+ retriever._attach_trust_tiers([ctx])+ assert ctx.metadata["trust_tier"] == first+++def test_attach_grades_graph_fact_without_signals_as_quarantine():+ # A graph fact must always carry a tier: no readable signals degrade to+ # quarantine instead of staying unscored (issue #1557: missing evidence+ # is never trusted as high confidence).+ retriever = ContextRetriever()+ ctx = _ctx("f", node_id="ent_1")+ retriever._attach_trust_tiers([ctx])+ assert ctx.metadata["trust_tier"] == TrustTier.QUARANTINE.value+++def test_attach_does_not_mutate_shared_metadata():+ # Retrieved metadata dicts can be shared with the vector store / memory;+ # writing the tier in place would persist a query-time grade into stored+ # state, where it goes stale as sources change.+ retriever = ContextRetriever()+ store_metadata = {"confidence": 0.9}+ ctx = RetrievedContext(content="f", score=1.0, metadata=store_metadata)+ retriever._attach_trust_tiers([ctx])+ assert ctx.metadata["trust_tier"] == TrustTier.BRONZE.value+ assert ctx.metadata is not store_metadata+ assert store_metadata == {"confidence": 0.9}+++def test_attach_recomputes_stale_tier():+ # A pre-existing grade is never trusted: it is recomputed from current+ # signals, so a tier cannot outlive the evidence it was derived from.+ retriever = ContextRetriever()+ ctx = _ctx("f", trust_tier="gold", confidence=0.9) # no corroboration+ retriever._attach_trust_tiers([ctx])+ assert ctx.metadata["trust_tier"] == TrustTier.BRONZE.value+++# ----------------------------------------------------------------------+# Filtering+# ----------------------------------------------------------------------+++def test_filter_by_tier_basic():+ retriever = ContextRetriever()+ items = [+ _ctx("gold", trust_tier="gold"),+ _ctx("bronze", trust_tier="bronze"),+ _ctx("none"),+ ]+ kept = retriever.filter_by_tier(items, TrustTier.SILVER)+ assert [c.content for c in kept] == ["gold"]+++def test_filter_by_tier_include_unscored():+ retriever = ContextRetriever()+ items = [_ctx("gold", trust_tier="gold"), _ctx("none")]+ kept = retriever.filter_by_tier(+ items, TrustTier.SILVER, include_unscored=True+ )+ assert {c.content for c in kept} == {"gold", "none"}+++def test_filter_by_tier_rejects_bad_minimum():+ retriever = ContextRetriever()+ with pytest.raises(TypeError):+ retriever.filter_by_tier([_ctx("x", trust_tier="gold")], "gold")+++# ----------------------------------------------------------------------+# End-to-end through retrieve()+# ----------------------------------------------------------------------+++def test_retrieve_attaches_tiers_and_filters():+ retriever = ContextRetriever()+ fake = [+ _ctx("gold", confidence=0.9, corroboration_count=3),+ _ctx("bronze", confidence=0.9),+ _ctx("low"), # unscored+ ]+ with patch.object(retriever, "_retrieve_from_vector", return_value=fake):+ results = retriever.retrieve(+ "q", max_results=10, min_trust_tier=TrustTier.SILVER+ )+ # gold meets SILVER; bronze and the unscored item are dropped.+ assert [c.content for c in results] == ["gold"]+ assert results[0].metadata["trust_tier"] == TrustTier.GOLD.value+++def test_retrieve_attaches_tiers_without_filter():+ retriever = ContextRetriever()+ fake = [+ _ctx("gold", confidence=0.9, corroboration_count=3),+ _ctx("bronze", confidence=0.9),+ _ctx("low"),+ ]+ with patch.object(retriever, "_retrieve_from_vector", return_value=fake):+ results = retriever.retrieve("q", max_results=10)+ assert len(results) == 3+ by_content = {c.content: c.metadata.get("trust_tier") for c in results}+ assert by_content["gold"] == TrustTier.GOLD.value+ assert by_content["bronze"] == TrustTier.BRONZE.value+ assert by_content["low"] is None # "low" left unscored+++def test_retrieve_global_attaches_tiers():+ retriever = ContextRetriever()+ fake = [_ctx("gold", confidence=0.9, corroboration_count=3)]+ with patch(+ "semantica.context.global_retriever.GlobalGraphRetriever"+ ) as GGR:+ instance = GGR.return_value+ res = MagicMock()+ res.to_retrieved_contexts.return_value = fake+ instance.search.return_value = res+ results = retriever.retrieve_global(+ "q", as_contexts=True, max_results=10+ )+ assert results[0].metadata["trust_tier"] == TrustTier.GOLD.value+++# ----------------------------------------------------------------------+# Global / drift modes carry no per-fact signal, so a trust threshold must+# keep summary results rather than empty them (regression guard for #1557).+# ----------------------------------------------------------------------+++def test_retrieve_global_keeps_unscored_summaries_with_min_trust_tier():+ retriever = ContextRetriever()+ fake = [_ctx("summary a"), _ctx("summary b")]+ with patch(+ "semantica.context.global_retriever.GlobalGraphRetriever"+ ) as GGR:+ instance = GGR.return_value+ res = MagicMock()+ res.to_retrieved_contexts.return_value = fake+ instance.search.return_value = res+ results = retriever.retrieve_global(+ "q", as_contexts=True, min_trust_tier=TrustTier.SILVER+ )+ # Must not return an empty list just because summaries are unscored.+ assert [c.content for c in results] == ["summary a", "summary b"]+++def test_retrieve_global_filters_explicitly_tiered_context():+ retriever = ContextRetriever()+ fake = [+ _ctx("kept", trust_tier="gold"),+ _ctx("dropped", trust_tier="bronze"),+ _ctx("unscored summary"),+ ]+ with patch(+ "semantica.context.global_retriever.GlobalGraphRetriever"+ ) as GGR:+ instance = GGR.return_value+ res = MagicMock()+ res.to_retrieved_contexts.return_value = fake+ instance.search.return_value = res+ results = retriever.retrieve_global(+ "q", as_contexts=True, min_trust_tier=TrustTier.SILVER+ )+ assert {c.content for c in results} == {"kept", "unscored summary"}+++def test_retrieve_drift_keeps_unscored_summaries_with_min_trust_tier():+ retriever = ContextRetriever()+ fake = [_ctx("drift summary a"), _ctx("drift summary b")]+ with patch(+ "semantica.context.drift_search.DriftSearchEngine"+ ) as DSE:+ instance = DSE.return_value+ res = MagicMock()+ res.to_retrieved_contexts.return_value = fake+ instance.search.return_value = res+ results = retriever.retrieve_drift(+ "q", as_contexts=True, min_trust_tier=TrustTier.SILVER+ )+ assert [c.content for c in results] == [+ "drift summary a",+ "drift summary b",+ ]+++def test_retrieve_hybrid_keeps_global_summaries_with_min_trust_tier():+ # Hybrid merges local facts with structurally unscored global summaries;+ # the threshold must filter each leg with its own semantics, so the+ # summaries survive instead of being stripped from every hybrid response.+ retriever = ContextRetriever()+ local_fake = [_ctx("local gold", confidence=0.9, corroboration_count=3)]+ global_fake = [_ctx("global summary")]+ with patch.object(+ retriever, "_retrieve_from_vector", return_value=local_fake+ ), patch(+ "semantica.context.global_retriever.GlobalGraphRetriever"+ ) as GGR:+ instance = GGR.return_value+ res = MagicMock()+ res.to_retrieved_contexts.return_value = global_fake+ instance.search.return_value = res+ results = retriever.retrieve(+ "q",+ mode="hybrid",+ max_results=10,+ min_trust_tier=TrustTier.SILVER,+ )+ contents = {c.content for c in results}+ assert "local gold" in contents+ assert "global summary" in contents+++# ----------------------------------------------------------------------+# Corroboration counts distinct sources, not lineage entries+# ----------------------------------------------------------------------+++def test_tier_signals_counts_distinct_sources():+ pm = MagicMock()+ pm.get_all_sources.return_value = [+ {"source": "doc_1"},+ {"source": "doc_1"},+ {"source": None},+ ]+ retriever = ContextRetriever(provenance_manager=pm)+ corr, _ = retriever._tier_signals_for(_ctx("f", node_id="ent_1"))+ assert corr == 1+++def test_single_document_lineage_is_not_gold():+ from semantica.provenance.manager import ProvenanceManager++ pm = ProvenanceManager()+ pm.track_entity("chunk_1", source="doc_1")+ pm.track_entity("ent_1", source="doc_1", parent_entity_id="chunk_1")+ retriever = ContextRetriever(provenance_manager=pm)+ ctx = _ctx("f", node_id="ent_1", confidence=0.9)+ retriever._attach_trust_tiers([ctx])+ assert ctx.metadata["trust_tier"] == TrustTier.SILVER.value+++# ----------------------------------------------------------------------+# DRIFT local facts are graph edges and are graded like graph facts+# ----------------------------------------------------------------------+++def _drift_result():+ from semantica.context.drift_search import DriftSearchResult++ return DriftSearchResult(+ query="q",+ answer="drift answer",+ global_reports_used=["c1"],+ verified_local_contexts=[+ {"source": "A", "target": "B", "relation": "R",+ "attributes": {"confidence": 0.95}},+ {"source": "C", "target": "D", "relation": "R", "attributes": {}},+ ],+ )+++def test_drift_local_facts_are_always_graded():+ retriever = ContextRetriever()+ contexts = _drift_result().to_retrieved_contexts()+ retriever._attach_trust_tiers(contexts)+ tiers = [c.metadata.get("trust_tier") for c in contexts]+ assert tiers == [None, TrustTier.BRONZE.value, TrustTier.QUARANTINE.value]+++def test_drift_threshold_does_not_keep_signal_less_facts():+ retriever = ContextRetriever()+ with patch("semantica.context.drift_search.DriftSearchEngine") as DSE:+ DSE.return_value.search.return_value = _drift_result()+ results = retriever.retrieve_drift(+ "q", as_contexts=True, max_results=10,+ min_trust_tier=TrustTier.BRONZE,+ )+ assert [c.content for c in results] == ["drift answer", "(A) -[R]-> (B)"]