# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-compaction-summary-as-a-lossy-index-rather-than-a-faithful-state-record.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-compaction-summary-as-a-lossy-index-rather-than-a-faithful-state-record.warrant.json
# Markdown SHA-256:   b154fc896e05299914b0e4fbd8169056d96975a389e381cc960b50a59d72ce4b
# Code AST SHA-256:   6fe32e4f9a040054a007df71e515b4bc56bf29f4b26943c66b85909ce3c9ded8
# Verified Classes:   CompactionRecallViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CompactionRecallViolation(RuntimeError):
    """Raised when a post-compaction action depends on a fact whose only
    surviving representation is a lossy summary (no verbatim source)."""
    pass


# Fact classes with observed low survival under lossy summarization.
# These may never be acted on from a summary alone.
NON_NEGOTIABLE_FACT_CLASSES = frozenset({
    "negative_constraint",   # "do not touch X"
    "conditional",           # "if Y then Z"
    "ordering_requirement",  # "A before B"
    "ownership_boundary",    # "X is owned by service S"
})


def authorize_action(action_fact: dict, summary: dict, ledger: dict, refetch) -> str:
    """
    action_fact: {"class": str, "subject": str, "claim": str}
    summary:     the lossy compaction summary (index only)
    ledger:      verbatim constraint ledger built at compaction time
    refetch:     callable(subject) -> verbatim span or None
    """
    fclass = action_fact["class"]
    subject = action_fact["subject"]

    if fclass in NON_NEGOTIABLE_FACT_CLASSES:
        # Must be grounded in the ledger or a verbatim re-fetch.
        if subject in ledger:
            return f"ledger:{subject}"
        span = refetch(subject)
        if span is None:
            # Summary silence is NOT evidence of absence.
            raise CompactionRecallViolation(
                f"action on {subject!r} ({fclass}) has no verbatim source; "
                f"summary omission cannot be read as permission"
            )
        return f"refetch:{subject}"

    # Non-constraining, positively framed facts may route from the summary.
    if subject not in summary:
        raise CompactionRecallViolation(
            f"summary does not mention {subject!r}; treat as unknown, re-fetch"
        )
    return f"summary:{subject}"


def resume_task(actions, summary, ledger, refetch):
    for a in actions:
        try:
            authorize_action(a, summary, ledger, refetch)
        except CompactionRecallViolation as e:
            # Halt and re-fetch rather than proceed on a lossy index.
            raise CompactionRecallViolation(
                f"halting resume: {e}"
            ) from e
    return "resumed"
