# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-peer-s-warrant-after-the-trust-anchor-has-rotated-but-the-peer-s-namespace-lease-has-not.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-peer-s-warrant-after-the-trust-anchor-has-rotated-but-the-peer-s-namespace-lease-has-not.warrant.json
# Markdown SHA-256:   1e5c077b541313aea4148fa283baeee81173b5036bfc77a10765d59542d3e42b
# Code AST SHA-256:   a20375bced22343257da4a86ea92d322aa0b26ab0f83a416f53e6d9a1323cf95
# Verified Classes:   LeaseGenerationMismatch, AnchorLagExceeded
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class LeaseGenerationMismatch(RuntimeError):
    """Raised when a federated warrant's lease generation does not match the live lease."""
    pass


class AnchorLagExceeded(RuntimeError):
    """Raised when the receiver's cached anchor is too stale to admit the warrant."""
    pass


def admit_federated_call(warrant, lease, cached_anchor_gen, current_anchor_gen,
                         max_anchor_lag, signature_valid):
    # Clause 1: anchor freshness bound, evaluated against declared signing generation.
    if not signature_valid:
        raise LeaseGenerationMismatch(
            f"warrant signature invalid for subject={warrant['subject']}"
        )
    if warrant["anchor_gen"] < current_anchor_gen - max_anchor_lag:
        raise AnchorLagExceeded(
            f"warrant anchor_gen={warrant['anchor_gen']} lags current="
            f"{current_anchor_gen} beyond max_anchor_lag={max_anchor_lag}"
        )

    # Clause 2: lease-holder identity binding. Namespace name equality is NOT enough.
    if lease["holder_org"] != warrant["subject"]:
        raise LeaseGenerationMismatch(
            f"namespace {lease['namespace']} held by {lease['holder_org']}, "
            f"warrant subject is {warrant['subject']}"
        )

    # Clause 3: lease-generation monotonicity. Reassignment bumps current_gen,
    # invalidating all prior warrants without revocation propagation.
    if warrant["lease_gen"] != lease["current_gen"]:
        raise LeaseGenerationMismatch(
            f"warrant lease_gen={warrant['lease_gen']} != live lease_gen="
            f"{lease['current_gen']} for namespace {lease['namespace']}; "
            f"peer must re-lease and re-warrant"
        )

    return {"admitted": True, "namespace": lease["namespace"],
            "subject": warrant["subject"], "lease_gen": lease["current_gen"]}
