"""Read-only release retention planning. The caller must supply a complete and trustworthy inventory of releases and transaction protections. No filesystem modification is performed. """ import re from core.compatibility import canonical_hash class RetentionError(RuntimeError): """Invalid or unsafe release retention policy.""" FIELDS = { "inventory", "current", "keep", "protected", "failed", "unresolved", } def require(condition, message): if not condition: raise RetentionError(message) def validate_release(value): require( isinstance(value, str) and re.fullmatch( r"[1-9][0-9]{0,19}", value, ) is not None, "Invalid release identifier", ) def validate_inventory(values): require( isinstance(values, list) and len(values) <= 100000, "Invalid release inventory", ) for value in values: validate_release(value) require( len(values) == len(set(values)), "Duplicate release identifiers", ) return sorted(values, key=int) def plan_retention(policy): require( isinstance(policy, dict) and set(policy) == FIELDS, "Invalid retention policy structure", ) inventory = validate_inventory( policy["inventory"] ) require( bool(inventory), "Release inventory is empty", ) current = policy["current"] validate_release(current) available = set(inventory) require( current in available, "Active release missing from inventory", ) keep = policy["keep"] require( type(keep) is int and 2 <= keep <= 1000, "Retention count must be between 2 and 1000", ) protections = {} for field in ( "protected", "failed", "unresolved", ): values = validate_inventory( policy[field] ) require( set(values) <= available, f"Unknown release in {field} inventory", ) protections[field] = values failed = set(protections["failed"]) require( current not in failed, "Active release is marked failed", ) reasons = {} def protect(release, reason): reasons.setdefault(release, set()).add(reason) protect(current, "active") for release in inventory[-keep:]: protect(release, "recent_release") for release in protections["protected"]: protect(release, "explicitly_protected") for release in protections["failed"]: protect(release, "failed_release") for release in protections["unresolved"]: protect(release, "unresolved_transaction") previous = [ release for release in inventory if int(release) < int(current) and release not in failed ] if previous: protect(previous[-1], "previous_available") blocked = bool(protections["unresolved"]) if blocked: for release in inventory: protect(release, "operation_unresolved") retained = [ release for release in inventory if release in reasons ] candidates = [ release for release in inventory if release not in reasons ] plan = { "format_version": 1, "status": "BLOCKED" if blocked else "READY", "current": current, "keep": keep, "inventory": inventory, "protected": protections["protected"], "failed": protections["failed"], "unresolved": protections["unresolved"], "retained": retained, "archive_candidates": candidates, "reasons": { release: sorted(reasons[release]) for release in retained }, "automatic_archival": False, "automatic_deletion": False, "execution_permitted": False, } plan["fingerprint"] = canonical_hash(plan) return plan