# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reject-a-memory-whose-provenance-chain-is-locally-valid-but-globally-severed.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reject-a-memory-whose-provenance-chain-is-locally-valid-but-globally-severed.warrant.json
# Markdown SHA-256:   41af278d3534912249f6ea09166d8b52c622d978f91b4a4dad02f88330e8ea11
# Code AST SHA-256:   fb6ba28366fd968a20e18ac33658581f70ddf013744fdb5c908c633056cd3b6f
# Verified Classes:   ProvenanceSeveranceError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ProvenanceSeveranceError(RuntimeError):
    """Raised when a record's warrant chain is locally valid but cannot be
    connected to the current lineage head because a required attestation
    edge was pruned or is otherwise unresolvable."""
    pass


CONNECTED = "CONNECTED"
SEVERED = "SEVERED"
UNATTESTED = "UNATTESTED"


def verify_connectivity(record, store, lineage_head) -> str:
    """Three-valued connectivity check. Never returns a bare boolean.

    Walks the attestation chain from the record's write epoch to the
    current lineage head. Distinguishes 'edge pruned/required-but-missing'
    (SEVERED) from 'record never had an intermediate' (UNATTESTED).
    """
    epoch = record.write_epoch
    while epoch < lineage_head.epoch:
        edge = store.get_attestation_edge(epoch, epoch + 1)
        if edge is None:
            if record.had_intermediate_attestation(epoch):
                # Bridge was pruned while the record still depends on it.
                return SEVERED
            return UNATTESTED
        if not edge.covers(record):
            return SEVERED
        epoch += 1
    return CONNECTED


def recall(record, store, lineage_head, supersession_epoch):
    status = verify_connectivity(record, store, lineage_head)

    if status == SEVERED:
        # Locally valid leaf signature is NOT sufficient. Quarantine.
        raise ProvenanceSeveranceError(
            f"record {record.id} from epoch {record.write_epoch} is locally "
            f"valid but severed from lineage head {lineage_head.epoch}; "
            f"required attestation edge was pruned. Refusing recall."
        )

    if status == UNATTESTED:
        return record.as_provenance_class("legacy_unattested", weight=0.3)

    if record.write_epoch < supersession_epoch.get(record.key, -1):
        return record.as_provenance_class("historical", weight=0.1)

    return record.as_provenance_class("current", weight=1.0)


def prune_attestation_edge(store, lo, hi):
    """Retention coupling: refuse to prune an edge any live record depends on."""
    dependents = store.records_depending_on_edge(lo, hi)
    if dependents:
        raise ProvenanceSeveranceError(
            f"cannot prune attestation edge ({lo}->{hi}): "
            f"{len(dependents)} live records depend on it for connectivity; "
            f"re-anchor them into the current epoch first."
        )
    store.delete_attestation_edge(lo, hi)
