# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-resource-allocation-ceilings-must-be-recorded-after-surviving-memory-pressure.md # Remnant Warrant: https://corpusfetch.com/c/what-resource-allocation-ceilings-must-be-recorded-after-surviving-memory-pressure.warrant.json # Markdown SHA-256: 991cfa25825039caaca5ef1660c79d1d3da5c95751db014c5f4ee5d565ce42ef # Code AST SHA-256: 2e5d9e7af53afa01034779032f309286063e38cec308b0c04c3472f0c73e4bb7 # Verified Classes: PressureType, MemoryPressureEvent, SafeOperatingBounds, ResourceCeilingManifest, AllocationRequest, MemoryCeilingRecorder # Defensive Guards: 2 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class PressureType(Enum): CGROUP_OOM = auto() CONTEXT_WINDOW_LIMIT = auto() SWAP_THRASHING = auto() @dataclass(frozen=True) class MemoryPressureEvent: pressure_type: PressureType peak_rss_observed: int allocation_ceiling_hit: int survival_mitigations: tuple[str, ...] @dataclass(frozen=True) class SafeOperatingBounds: max_concurrent_workers: int max_input_tokens: int required_mitigations: frozenset[str] @dataclass(frozen=True) class ResourceCeilingManifest: event: MemoryPressureEvent safe_bounds: SafeOperatingBounds manifest_digest: str @dataclass(frozen=True) class AllocationRequest: requested_workers: int requested_tokens: int class MemoryCeilingRecorder: """ Evaluates memory pressure events and seals safe operating bounds, ensuring successors do not re-trigger resource exhaustion. """ @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def seal_manifest( cls, event: MemoryPressureEvent, safe_bounds: SafeOperatingBounds, ) -> ResourceCeilingManifest: if not event.survival_mitigations: raise ValueError("Memory pressure event lacks documented survival mitigations.") if event.peak_rss_observed < event.allocation_ceiling_hit: raise ValueError("Peak RSS must meet or exceed the ceiling hit to record pressure.") digest_payload = { "pressure_type": event.pressure_type.name, "peak_rss": str(event.peak_rss_observed), "ceiling": str(event.allocation_ceiling_hit), "max_workers": str(safe_bounds.max_concurrent_workers), "max_tokens": str(safe_bounds.max_input_tokens), } digest = cls._hash( json.dumps(digest_payload, sort_keys=True, separators=(",", ":")) ) return ResourceCeilingManifest( event=event, safe_bounds=safe_bounds, manifest_digest=digest, ) @classmethod def verify_allocation_request( cls, manifest: ResourceCeilingManifest, request: AllocationRequest ) -> tuple[bool, str]: """ Fetching-time check: returns (is_safe, rejection_reason). Fail-closed if the request exceeds the recorded safe operating bounds. """ if request.requested_workers > manifest.safe_bounds.max_concurrent_workers: return False, f"Requested workers ({request.requested_workers}) exceeds safe bound ({manifest.safe_bounds.max_concurrent_workers})." if request.requested_tokens > manifest.safe_bounds.max_input_tokens: return False, f"Requested tokens ({request.requested_tokens}) exceeds safe bound ({manifest.safe_bounds.max_input_tokens})." return True, "SAFE_TO_PROCEED"