# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-must-an-instance-operate-when-its-context-window-is-partially-corrupted.md # Remnant Warrant: https://corpusfetch.com/c/how-must-an-instance-operate-when-its-context-window-is-partially-corrupted.warrant.json # Markdown SHA-256: c6fb73f2282814e67c8c8e750c77bc854975be1b746f9372697a579efe442bfe # Code AST SHA-256: d333adf78b70c4127210d4e5f66449c7c0c14dde9ed7c53889bd7414f4af6f5e # Verified Classes: CorruptionVerdict, CorruptionState, CorruptionPolicy, CorruptionManifest, PartialCorruptionGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class CorruptionVerdict(Enum): NO_CORRUPTION = auto() PARTIAL_CORRUPTION_BOUNDED = auto() TOTAL_CORRUPTION_HALT = auto() TASK_DEPENDENCY_CONFLICT = auto() @dataclass(frozen=True) class CorruptionState: segment_id: str is_corrupted: bool task_requires_segment: bool uncorrupted_segments_suffice: bool @dataclass(frozen=True) class CorruptionPolicy: halt_on_total_corruption: bool @dataclass(frozen=True) class CorruptionManifest: verdict: CorruptionVerdict directive: str manifest_digest: str class PartialCorruptionGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_state( cls, state: CorruptionState, policy: CorruptionPolicy ) -> CorruptionManifest: # Gate 1: No corruption if not state.is_corrupted: digest = cls._hash(json.dumps({ "segment": state.segment_id, "corrupted": False }, sort_keys=True)) return CorruptionManifest( verdict=CorruptionVerdict.NO_CORRUPTION, directive=f"PROCEED: Segment {state.segment_id} intact.", manifest_digest=digest ) # Gate 2: Task dependency if state.task_requires_segment: if not state.uncorrupted_segments_suffice: return CorruptionManifest( verdict=CorruptionVerdict.TASK_DEPENDENCY_CONFLICT, directive=f"HALT: Task requires corrupted segment {state.segment_id}.", manifest_digest=cls._hash(f"DEPENDENCY:{state.segment_id}") ) # Gate 3: Bounded digest = cls._hash(json.dumps({ "segment": state.segment_id, "required": state.task_requires_segment, "sufficient": state.uncorrupted_segments_suffice }, sort_keys=True)) return CorruptionManifest( verdict=CorruptionVerdict.PARTIAL_CORRUPTION_BOUNDED, directive=f"PROCEED: Quarantine {state.segment_id}, operate on uncorrupted segments.", manifest_digest=digest )