# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-must-an-instance-distinguish-adversary-adaptation-from-environmental-drift.md # Remnant Warrant: https://corpusfetch.com/c/how-must-an-instance-distinguish-adversary-adaptation-from-environmental-drift.warrant.json # Markdown SHA-256: 978e884eb8f10009b93f441c90b5971de013c3b8350324f25939cc6ab98fce10 # Code AST SHA-256: 60b3a06e1d8360b656f5ccacc23701b46598985022b51d95af61a9dc67b2bc62 # Verified Classes: AttributionVerdict, BehaviorChange, AttributionPolicy, AttributionManifest, AdaptationVsDriftGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class AttributionVerdict(Enum): NO_CHANGE = auto() ENVIRONMENTAL_DRIFT = auto() ADVERSARY_ADAPTATION = auto() MIXED_ATTRIBUTION = auto() @dataclass(frozen=True) class BehaviorChange: change_id: str behavior_shift_hash: str environmental_explanation_available: bool matches_adversary_prior: bool is_static_environment: bool behavioral_change_magnitude: float @dataclass(frozen=True) class AttributionPolicy: min_magnitude_for_adaptation: float require_environmental_check: bool @dataclass(frozen=True) class AttributionManifest: verdict: AttributionVerdict directive: str manifest_digest: str class AdaptationVsDriftGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_change( cls, change: BehaviorChange, policy: AttributionPolicy ) -> AttributionManifest: # Gate 1: No change if change.behavioral_change_magnitude <= 0: digest = cls._hash(json.dumps({ "change": change.change_id, "magnitude": "0" }, sort_keys=True)) return AttributionManifest( verdict=AttributionVerdict.NO_CHANGE, directive=f"PROCEED: No behavior change for {change.change_id}.", manifest_digest=digest ) # Gate 2: Static environment if change.is_static_environment: if change.behavioral_change_magnitude >= policy.min_magnitude_for_adaptation: return AttributionManifest( verdict=AttributionVerdict.ADVERSARY_ADAPTATION, directive=f"ATTRIBUTE: Static environment, change magnitude {change.behavioral_change_magnitude:.2f} = adaptation.", manifest_digest=cls._hash(f"ADAPTATION_STATIC:{change.change_id}") ) # Gate 3: Mixed signals has_env_explanation = change.environmental_explanation_available has_adv_prior_match = change.matches_adversary_prior if has_env_explanation and has_adv_prior_match: return AttributionManifest( verdict=AttributionVerdict.MIXED_ATTRIBUTION, directive=f"FLAG: Change {change.change_id} matches both environmental and adversarial causes.", manifest_digest=cls._hash(f"MIXED:{change.change_id}") ) # Gate 4: Environmental only if has_env_explanation and not has_adv_prior_match: digest = cls._hash(json.dumps({ "change": change.change_id, "cause": "ENVIRONMENTAL" }, sort_keys=True)) return AttributionManifest( verdict=AttributionVerdict.ENVIRONMENTAL_DRIFT, directive=f"PROCEED: Change {change.change_id} attributed to environmental drift.", manifest_digest=digest ) # Gate 5: Adaptation only if not has_env_explanation and has_adv_prior_match: if change.behavioral_change_magnitude >= policy.min_magnitude_for_adaptation: return AttributionManifest( verdict=AttributionVerdict.ADVERSARY_ADAPTATION, directive=f"ATTRIBUTE: Change {change.change_id} matches adversary prior, adaptation.", manifest_digest=cls._hash(f"ADAPTATION:{change.change_id}") ) # Gate 6: Unknown digest = cls._hash(json.dumps({ "change": change.change_id, "env": str(has_env_explanation), "adv": str(has_adv_prior_match), "magnitude": f"{change.behavioral_change_magnitude:.3f}" }, sort_keys=True)) return AttributionManifest( verdict=AttributionVerdict.ENVIRONMENTAL_DRIFT, directive=f"ATTRIBUTE_WITH_CAUTION: Change {change.change_id} attributed to drift by default.", manifest_digest=digest )