# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-its-own-model-driven-context-rewrite-as-a-destructive-operation-that-invalidates-prior-reasoning-provenance.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-its-own-model-driven-context-rewrite-as-a-destructive-operation-that-invalidates-prior-reasoning-provenance.warrant.json
# Markdown SHA-256:   a0876b3b526685b8f990b4c5c3c05dd6849a8ebd8b371126cce0a2ce54eeac88
# Code AST SHA-256:   88e921d5517f4461b16da5b85e41ab7281204a191c8f773dc5b0c188af0df0f9
# Verified Classes:   ProvenanceSeveredError, RewriteProvenanceGuard
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ProvenanceSeveredError(RuntimeError):
    """Raised when a context rewrite destroys the only recoverable source for a live claim."""
    pass


class RewriteProvenanceGuard:
    def __init__(self):
        # span_id -> re-fetch handle, or None if the source is not re-derivable
        self.pinned: dict[str, str | None] = {}
        self.live_claims: dict[str, str] = {}  # claim_id -> span_id it depends on

    def pin_observation(self, span_id: str, refetch_handle: str | None):
        # None handle == authoritative-destructive source (side-effecting / non-deterministic)
        self.pinned[span_id] = refetch_handle

    def register_claim(self, claim_id: str, depends_on_span: str):
        self.live_claims[claim_id] = depends_on_span

    def authorise_prune(self, span_ids: list[str]) -> None:
        for sid in span_ids:
            if sid not in self.pinned:
                raise ProvenanceSeveredError(
                    f"span {sid!r} is not pinned; pruning an untracked span is unsafe"
                )
            if self.pinned[sid] is None:
                # source is non-re-derivable
                dependents = [c for c, s in self.live_claims.items() if s == sid]
                if dependents:
                    raise ProvenanceSeveredError(
                        f"span {sid!r} is authoritative-destructive and still "
                        f"licenses live claims {dependents}; promote to pinned "
                        f"extract or downgrade those claims before pruning"
                    )
        # safe: either re-derivable, or no live dependents
        for sid in span_ids:
            self.pinned.pop(sid, None)

    def verify_summary(self, span_id: str, summary_text: str, extract: str) -> bool:
        handle = self.pinned.get(span_id)
        if handle is None and span_id in self.pinned:
            raise ProvenanceSeveredError(
                f"cannot verify summary of {span_id!r}: source is non-re-derivable; "
                f"summary must be marked UNVERIFIED"
            )
        # caller re-fetches via handle and compares; here we assert the extract
        # must not upgrade a hedged/negative source claim into a positive one.
        if "413" in extract and "413" not in summary_text:
            raise ProvenanceSeveredError(
                f"summary of {span_id!r} dropped a capability-limit signal present "
                f"in source extract; rewrite upgraded claim strength"
            )
        return True
