# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-bound-the-recovery-horizon-of-a-lossy-auto-compaction-chain-when-summarization-is-the-only-surviving-evidence.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-bound-the-recovery-horizon-of-a-lossy-auto-compaction-chain-when-summarization-is-the-only-surviving-evidence.warrant.json
# Markdown SHA-256:   7a116145eeca372f5aa7823c4667b3c9b09c43578efa4ce61089d64389073441
# Code AST SHA-256:   4706832c75ea0e74c22e0902d23c344080fa740b903e984464af8a9f170b7544
# Verified Classes:   RecallSource, RecoveryHorizonViolation, RecalledItem
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class RecallSource(Enum):
    PINNED = "pinned"
    SUMMARY = "summary"
    RECONSTRUCTED = "reconstructed"


class RecoveryHorizonViolation(RuntimeError):
    """Raised when a summary-derived item is treated as recalled fact
    beyond the stated recovery horizon, or when a summary is used to
    authorize an irreversible action."""
    pass


@dataclass
class RecalledItem:
    key: str
    source: RecallSource
    turns_ago: int
    irreversible_action: bool = False


def authorize_action(item: RecalledItem, recovery_horizon: int) -> RecallSource:
    # A summary may never authorize an irreversible action.
    if item.irreversible_action and item.source is not RecallSource.PINNED:
        raise RecoveryHorizonViolation(
            f"cannot authorize irreversible action '{item.key}' from "
            f"{item.source.value}; re-derive or re-confirm from pinned source"
        )

    # Items older than the recovery horizon are not recallable from summaries.
    if item.source is RecallSource.SUMMARY and item.turns_ago > recovery_horizon:
        raise RecoveryHorizonViolation(
            f"item '{item.key}' is {item.turns_ago} turns old, beyond recovery "
            f"horizon {recovery_horizon}; treat as unknown, do not assert"
        )

    # Reconstruction is never a valid basis for action.
    if item.source is RecallSource.RECONSTRUCTED:
        raise RecoveryHorizonViolation(
            f"item '{item.key}' is reconstructed, not recalled; "
            f"must be confirmed before use"
        )

    return item.source


def safe_recall(item: RecalledItem, recovery_horizon: int) -> str:
    try:
        source = authorize_action(item, recovery_horizon)
    except RecoveryHorizonViolation as e:
        return f"UNKNOWN: {e}"
    if source is RecallSource.PINNED:
        return f"ASSERT '{item.key}' (pinned, authoritative)"
    return f"ASSERT '{item.key}' (summary, within horizon, labeled)"
