# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-must-an-instance-handle-semantic-search-returning-results-from-a-model-generated-corpus.md # Remnant Warrant: https://corpusfetch.com/c/how-must-an-instance-handle-semantic-search-returning-results-from-a-model-generated-corpus.warrant.json # Markdown SHA-256: 9c3a6cc6b8ff03f9b202e69fa07959f558192ddcbdff04cc8b7bce88d092e49d # Code AST SHA-256: 233e20b2d8033f3d5ef81b8f1c4ef646990abb2b0ecfe5e3dba487e70c7a7526 # Verified Classes: AuthenticityClass, SynthesisVerdict, SearchResult, SynthesisPolicy, SynthesisManifest, SyntheticSourceGuard # Defensive Guards: 1 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class AuthenticityClass(Enum): HUMAN_AUTHORED = auto() SYNTHETIC_DEPTH_1 = auto() SYNTHETIC_DEPTH_2_PLUS = auto() UNKNOWN = auto() class SynthesisVerdict(Enum): HUMAN_GROUNDED = auto() SYNTHETIC_DEPTH_1_ACCEPTABLE = auto() SYNTHETIC_DEPTH_EXCESSIVE = auto() UNVERIFIED_SOURCES = auto() @dataclass(frozen=True) class SearchResult: result_id: str authenticity: AuthenticityClass human_anchor_hash: str | None source_signature: str | None @dataclass(frozen=True) class SynthesisPolicy: min_human_grounding_ratio: float allow_depth_1_synthetic: bool @dataclass(frozen=True) class SynthesisManifest: verdict: SynthesisVerdict directive: str manifest_digest: str class SyntheticSourceGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_results( cls, results: tuple[SearchResult, ...], policy: SynthesisPolicy ) -> SynthesisManifest: if not results: raise ValueError("No results to evaluate.") # Gate 1: Signature presence for r in results: if not r.source_signature: return SynthesisManifest( verdict=SynthesisVerdict.UNVERIFIED_SOURCES, directive=f"HALT: Result {r.result_id} lacks source signature.", manifest_digest=cls._hash(f"NO_SIG:{r.result_id}") ) # Gate 2: Human grounding ratio human_count = sum(1 for r in results if r.authenticity is AuthenticityClass.HUMAN_AUTHORED) ratio = human_count / len(results) # Gate 3: Depth 1 acceptance depth_1_count = sum(1 for r in results if r.authenticity is AuthenticityClass.SYNTHETIC_DEPTH_1) depth_2_plus_count = sum(1 for r in results if r.authenticity is AuthenticityClass.SYNTHETIC_DEPTH_2_PLUS) # If sufficient human grounding, all accepted if ratio >= policy.min_human_grounding_ratio: digest = cls._hash(json.dumps({ "total": str(len(results)), "human": str(human_count), "ratio": f"{ratio:.2f}" }, sort_keys=True)) return SynthesisManifest( verdict=SynthesisVerdict.HUMAN_GROUNDED, directive=f"PROCEED: {ratio:.2%} human grounding.", manifest_digest=digest ) # If depth 1 allowed and no depth 2+ if policy.allow_depth_1_synthetic and depth_2_plus_count == 0: digest = cls._hash(json.dumps({ "total": str(len(results)), "human": str(human_count), "depth_1": str(depth_1_count), "depth_2_plus": "0" }, sort_keys=True)) return SynthesisManifest( verdict=SynthesisVerdict.SYNTHETIC_DEPTH_1_ACCEPTABLE, directive=f"PROCEED: Human grounding {ratio:.2%} below threshold, but only depth-1 synthetic.", manifest_digest=digest ) # Excessive synthetic return SynthesisManifest( verdict=SynthesisVerdict.SYNTHETIC_DEPTH_EXCESSIVE, directive=f"HALT: Only {ratio:.2%} human grounding, {depth_2_plus_count} depth-2+ synthetic. Re-query with human filter.", manifest_digest=cls._hash(f"SYNTHETIC:{ratio:.2f}") )