# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-context-compaction-summaries-as-non-authoritative-for-reconstruction.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-context-compaction-summaries-as-non-authoritative-for-reconstruction.warrant.json
# Markdown SHA-256:   10861099a82023c7f4f01c9f2c3acddedaa63aac1ba78362c27ffde339841dc8
# Code AST SHA-256:   2f4e929a419f10f8d74049bd1219830a63dce2ebf3a20a9d920ec14ed45775a7
# Verified Classes:   CompactionFidelityError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CompactionFidelityError(RuntimeError):
    """Raised when a value-sensitive action would rely on a lossy compaction summary."""
    pass


PRECISION_CLASS = {"numeric", "negation", "scope", "ordering", "identifier"}


def assert_action_safe(action, fact):
    """
    action: dict with keys 'irreversible' (bool), 'value_sensitive' (bool)
    fact: dict with keys 'span_type' (str), 'source_reachable' (bool),
          'rederived' (bool)
    """
    if not (action.get("irreversible") or action.get("value_sensitive")):
        return True  # gist-level, reversible: summary is sufficient

    span_type = fact.get("span_type", "unknown")
    if span_type not in PRECISION_CLASS:
        return True  # not a precision-class span

    if fact.get("rederived"):
        return True  # ground truth confirmed against source

    if not fact.get("source_reachable"):
        raise CompactionFidelityError(
            f"Cannot act: {span_type} fact from lossy compaction, "
            f"source unreachable, no re-derivation. Action: {action}."
        )

    raise CompactionFidelityError(
        f"Cannot act: {span_type} fact from lossy compaction not yet "
        f"re-derived from reachable source. Retrieve and re-derive first. "
        f"Action: {action}."
    )
