# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-must-an-instance-handle-multi-oracle-consensus-when-oracles-have-non-uniform-accuracy-profiles.md # Remnant Warrant: https://corpusfetch.com/c/how-must-an-instance-handle-multi-oracle-consensus-when-oracles-have-non-uniform-accuracy-profiles.warrant.json # Markdown SHA-256: 0882700ba52d44528d9680f4b6baa55dc32e3d110da53dda3ab5c1e793446ae6 # Code AST SHA-256: 3499779319c596e867ef34e8a6107d36ddf3eec075126d49902f08c40040da18 # Verified Classes: WeightedVerdict, OracleResponse, OracleProfile, OracleQuery, WeightingPolicy, WeightingManifest, NonUniformOracleGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class WeightedVerdict(Enum): UNIFORM_WEIGHTED = auto() DOMAIN_WEIGHTED = auto() INSUFFICIENT_PROFILES = auto() QUERY_UNCLASSIFIABLE = auto() @dataclass(frozen=True) class OracleResponse: oracle_id: str value: float domain: str query_type: str @dataclass(frozen=True) class OracleProfile: oracle_id: str domain_accuracy: tuple[tuple[str, float], ...] query_type_accuracy: tuple[tuple[str, float], ...] accuracy_epoch: float @dataclass(frozen=True) class OracleQuery: domain: str query_type: str responses: tuple[OracleResponse, ...] @dataclass(frozen=True) class WeightingPolicy: require_domain_profiles: bool min_profile_freshness_sec: float @dataclass(frozen=True) class WeightingManifest: verdict: WeightedVerdict weighted_value: float | None directive: str manifest_digest: str class NonUniformOracleGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_oracle_consensus( cls, query: OracleQuery, profiles: dict[str, OracleProfile], policy: WeightingPolicy, current_epoch: float ) -> WeightingManifest: # Gate 1: Query classification if not query.domain or not query.query_type: return WeightingManifest( verdict=WeightedVerdict.QUERY_UNCLASSIFIABLE, weighted_value=None, directive=f"HALT: Query lacks domain/type classification. Cannot weight.", manifest_digest=cls._hash("UNCLASSIFIABLE") ) # Gate 2: Profile presence if policy.require_domain_profiles: for resp in query.responses: if resp.oracle_id not in profiles: return WeightingManifest( verdict=WeightedVerdict.INSUFFICIENT_PROFILES, weighted_value=None, directive=f"HALT: No profile for oracle {resp.oracle_id}.", manifest_digest=cls._hash(f"NO_PROFILE:{resp.oracle_id}") ) # Gate 3: Profile freshness for resp in query.responses: profile = profiles.get(resp.oracle_id) if profile: age = current_epoch - profile.accuracy_epoch if age > policy.min_profile_freshness_sec: return WeightingManifest( verdict=WeightedVerdict.INSUFFICIENT_PROFILES, weighted_value=None, directive=f"HALT: Profile for {resp.oracle_id} is {age:.0f}s stale.", manifest_digest=cls._hash(f"STALE_PROFILE:{resp.oracle_id}") ) # Gate 4: Compute weighted consensus weighted_sum = 0.0 weight_sum = 0.0 for resp in query.responses: profile = profiles[resp.oracle_id] domain_acc = dict(profile.domain_accuracy).get(query.domain, 0.5) type_acc = dict(profile.query_type_accuracy).get(query.query_type, 0.5) weight = domain_acc * type_acc weighted_sum += resp.value * weight weight_sum += weight weighted_value = weighted_sum / weight_sum if weight_sum > 0 else None digest = cls._hash(json.dumps({ "domain": query.domain, "query_type": query.query_type, "responses": tuple(r.oracle_id for r in query.responses), "weighted_value": f"{weighted_value:.4f}" if weighted_value else "NONE" }, sort_keys=True)) return WeightingManifest( verdict=WeightedVerdict.DOMAIN_WEIGHTED, weighted_value=weighted_value, directive=f"PROCEED: Domain-weighted consensus {weighted_value:.4f}.", manifest_digest=digest )