# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-renew-a-delegated-credential-chain-whose-witness-set-has-silently-shrunk.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-renew-a-delegated-credential-chain-whose-witness-set-has-silently-shrunk.warrant.json
# Markdown SHA-256:   525e91426ab72777f815a9a720a443c7ed232085681145a47bc2a98082008208
# Code AST SHA-256:   419e285fe247efd3508bdf109749847fe14db7eb754536407dfa3e3c5b761645
# Verified Classes:   WitnessSetDiscontinuity
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class WitnessSetDiscontinuity(RuntimeError):
    """Raised when a renewal would silently substitute witnesses without attested departure."""
    pass


def renew_delegated_chain(
    chain_id: str,
    bound_witness_set: frozenset[str],
    attested_departures: frozenset[str],
    proposed_witness_set: frozenset[str],
    threshold: int,
) -> frozenset[str]:
    """
    Attempt to advance a delegated credential chain to its next epoch.

    Returns the new bound witness set on success. Raises WitnessSetDiscontinuity
    if the proposed set meets threshold but silently drops witnesses that have
    no attested departure in the chain's ledger.
    """
    if len(proposed_witness_set) < threshold:
        raise WitnessSetDiscontinuity(
            f"chain {chain_id}: proposed witness set size {len(proposed_witness_set)} "
            f"below threshold {threshold}"
        )

    dropped = bound_witness_set - proposed_witness_set
    unattested = dropped - attested_departures

    if unattested:
        # Threshold is met, but continuity is broken. This is the boundary.
        raise WitnessSetDiscontinuity(
            f"chain {chain_id}: witnesses {sorted(unattested)} departed the bound set "
            f"without an attested revocation or handoff; renewal refused to prevent "
            f"silent re-underwriting of delegated authority"
        )

    return proposed_witness_set
