# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-must-an-instance-quarantine-semantically-adjacent-but-factually-incorrect-peer-context.md # Remnant Warrant: https://corpusfetch.com/c/how-must-an-instance-quarantine-semantically-adjacent-but-factually-incorrect-peer-context.warrant.json # Markdown SHA-256: df0048f8dd3d979a8e69fd60a53c349e573cce7b148816ecc3598088d5f91ed8 # Code AST SHA-256: b069a7f1f63258d03bb6a97f4c291f0e5b4a32608bbc1b1103b49d1ae9c3d4cb # Verified Classes: AdjacentContextVerdict, ContextItem, ContextRequest, AdjacentContextPolicy, AdjacentContextManifest, AdjacentContextQuarantine # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class AdjacentContextVerdict(Enum): EXACT_MATCH = auto() ADJACENT_QUARANTINED = auto() ADJACENT_ACCEPTED_ANALOGY = auto() IRRELEVANT = auto() @dataclass(frozen=True) class ContextItem: context_id: str semantic_relevance: float applicability_scope: dict entity_id: str version: str @dataclass(frozen=True) class ContextRequest: target_entity: str target_version: str target_scope: dict min_relevance: float @dataclass(frozen=True) class AdjacentContextPolicy: require_exact_entity: bool require_exact_version: bool allow_analogy: bool @dataclass(frozen=True) class AdjacentContextManifest: verdict: AdjacentContextVerdict directive: str manifest_digest: str class AdjacentContextQuarantine: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_context( cls, item: ContextItem, req: ContextRequest, policy: AdjacentContextPolicy ) -> AdjacentContextManifest: # Gate 1: Relevance threshold if item.semantic_relevance < req.min_relevance: digest = cls._hash(json.dumps({ "context": item.context_id, "relevance": f"{item.semantic_relevance:.2f}" }, sort_keys=True)) return AdjacentContextManifest( verdict=AdjacentContextVerdict.IRRELEVANT, directive=f"DISCARD: Context {item.context_id} relevance {item.semantic_relevance:.2f} < {req.min_relevance:.2f}.", manifest_digest=digest ) # Gate 2: Entity check if policy.require_exact_entity and item.entity_id != req.target_entity: if policy.allow_analogy: digest = cls._hash(json.dumps({ "context": item.context_id, "target": req.target_entity, "found": item.entity_id }, sort_keys=True)) return AdjacentContextManifest( verdict=AdjacentContextVerdict.ADJACENT_ACCEPTED_ANALOGY, directive=f"PROCEED_AS_ANALOGY: Context for {item.entity_id} vs target {req.target_entity}.", manifest_digest=digest ) return AdjacentContextManifest( verdict=AdjacentContextVerdict.ADJACENT_QUARANTINED, directive=f"QUARANTINE: Context for {item.entity_id} does not match target {req.target_entity}.", manifest_digest=cls._hash(f"ENTITY:{item.context_id}") ) # Gate 3: Version check if policy.require_exact_version and item.version != req.target_version: return AdjacentContextManifest( verdict=AdjacentContextVerdict.ADJACENT_QUARANTINED, directive=f"QUARANTINE: Context version {item.version} != target {req.target_version}.", manifest_digest=cls._hash(f"VERSION:{item.context_id}") ) # Gate 4: Scope check for key, val in req.target_scope.items(): if item.applicability_scope.get(key) != val: return AdjacentContextManifest( verdict=AdjacentContextVerdict.ADJACENT_QUARANTINED, directive=f"QUARANTINE: Scope mismatch on '{key}': {item.applicability_scope.get(key)} != {val}.", manifest_digest=cls._hash(f"SCOPE:{item.context_id}") ) # Gate 5: Exact digest = cls._hash(json.dumps({ "context": item.context_id, "entity": item.entity_id, "version": item.version, "scope": tuple(sorted(item.applicability_scope.items())) }, sort_keys=True)) return AdjacentContextManifest( verdict=AdjacentContextVerdict.EXACT_MATCH, directive=f"PROCEED: Context {item.context_id} exactly matches request.", manifest_digest=digest )