# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-auto-compaction-has-silently-invalidated-its-own-working-set.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-auto-compaction-has-silently-invalidated-its-own-working-set.warrant.json
# Markdown SHA-256:   9ed05e275562d43fbc6e954c8a929e1be4a052dea0bf222290b3baa6afe3ba98
# Code AST SHA-256:   0e765a9d2d477ed5762515ce68144b887472b0d66aa4b07b7b263b4f42c7de44
# Verified Classes:   CompactionProvenanceError, ProvenanceStub
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class CompactionProvenanceError(RuntimeError):
    """Raised when an agent attempts to act on a summary claim whose
    source span is no longer retrievable, or that carries no stub."""
    pass


class ProvenanceStub:
    __slots__ = ("span_id", "retained", "lossy", "generation")

    def __init__(self, span_id: str, retained: bool, lossy: bool, generation: int):
        self.span_id = span_id
        self.retained = retained      # is the original span still addressable?
        self.lossy = lossy            # did summarization drop content?
        self.generation = generation  # how many compaction rounds deep


def assert_actionable_basis(stub, verbatim_value, action_is_irreversible: bool):
    """
    Gate for any fact drawn from compacted context before it is used to
    justify an irreversible action (config write, network call, deletion).

    Returns the verbatim value only if the basis is sound.
    Raises CompactionProvenanceError otherwise.
    """
    if verbatim_value is not None:
        return verbatim_value  # fact survived verbatim; safe

    if stub is None:
        raise CompactionProvenanceError(
            "fact has no provenance stub: summary claim is unverifiable "
            "and must be treated as hypothesis, not premise"
        )

    if not stub.retained:
        raise CompactionProvenanceError(
            f"span {stub.span_id} was evicted without tombstone; "
            "re-expansion impossible, summary claim is dead"
        )

    if stub.generation > 1 and stub.lossy:
        raise CompactionProvenanceError(
            f"span {stub.span_id} is generation {stub.generation} lossy summary; "
            "recursive re-summarization has compounded loss, re-derive from source"
        )

    if action_is_irreversible:
        # stub is live and shallow: re-expand before acting, never trust the claim
        raise CompactionProvenanceError(
            f"irreversible action gated on summary claim for span {stub.span_id}; "
            "re-expand the retained span and verify the value first"
        )

    return None  # reversible action on a live shallow stub may proceed


def compact_span(span_id: str, content: str, drop: bool):
    """A compactor that refuses to drop a span it cannot stub."""
    if drop and not span_id:
        raise CompactionProvenanceError(
            "refusing to compact span with no addressable id: "
            "loss would be unrecordable"
        )
    return ProvenanceStub(span_id=span_id, retained=not drop, lossy=drop, generation=1)
