# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-honor-a-revocation-it-cannot-verify-provenance-chain-truncation-in-read-write-forget-memory-tools.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-honor-a-revocation-it-cannot-verify-provenance-chain-truncation-in-read-write-forget-memory-tools.warrant.json
# Markdown SHA-256:   702319e58cfc083ed4c04e9687ac41a37ef3d0ed780ade07d619b0994e25856a
# Code AST SHA-256:   ae26e4098568869156a5bbed23c86290a2720386940d5010302a0a103d5e66b6
# Verified Classes:   UnverifiableRevocationError, ProvenanceChain
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class UnverifiableRevocationError(RuntimeError):
    """Raised when a revocation cannot be verified against a walkable provenance chain."""
    pass


class ProvenanceChain:
    def __init__(self, nodes, trusted_roots):
        # nodes: list of (author_id, parent_hash, signature); ordered leaf -> root
        self.nodes = nodes
        self.trusted_roots = set(trusted_roots)

    def walk_to_root(self):
        """Return the root hash if the chain reaches a trusted root, else None."""
        if not self.nodes:
            return None
        # Last node must be a trusted root AND must not claim a parent.
        leaf_parent = self.nodes[-1][1]
        if leaf_parent is not None:
            # Chain terminates at a non-root node: truncated.
            return None
        root_hash = self.nodes[-1][2]  # signature over the root node
        return root_hash if root_hash in self.trusted_roots else None


def verify_read(record, tombstone, now, propagation_p99):
    """
    Fail-closed read verification. Returns record content only if
    non-revocation is *provable*, not merely asserted.
    """
    chain = ProvenanceChain(record.provenance, record.trusted_roots)
    root = chain.walk_to_root()
    if root is None:
        raise UnverifiableRevocationError(
            f"record {record.id}: provenance chain does not reach a trusted root"
        )

    if tombstone is not None:
        if tombstone.parent_hash is None:
            # A tombstone with no parent is NOT a root. It is truncated.
            raise UnverifiableRevocationError(
                f"record {record.id}: tombstone {tombstone.id} has null parent; "
                "cannot promote truncated tombstone to root"
            )
        if tombstone.scope == "root":
            if record.root_id == tombstone.root_id and record.authored_at < tombstone.effective_before:
                raise UnverifiableRevocationError(
                    f"record {record.id}: root-scoped revocation in effect"
                )
        elif tombstone.scope == "record" and tombstone.record_id == record.id:
            raise UnverifiableRevocationError(
                f"record {record.id}: record-scoped revocation in effect"
            )

    # Lag window: a pending revocation may be in flight. Cannot prove non-revocation.
    if record.authored_by in record.pending_revocations:
        if now - record.pending_revocations[record.authored_by] < propagation_p99:
            raise UnverifiableRevocationError(
                f"record {record.id}: revocation lag window open for principal "
                f"{record.authored_by}; non-revocation unprovable"
            )

    return record.content
