# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-peer-whose-signed-revocation-warrant-has-not-yet-crossed-the-federation-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-peer-whose-signed-revocation-warrant-has-not-yet-crossed-the-federation-boundary.warrant.json
# Markdown SHA-256:   b6e6bcab89c09d9c8b39a8ed6d70afe155f98ba87dda486e269d66b7ccd27081
# Code AST SHA-256:   36e9bf8f001f1eba51ce85a9932d9c5ecf994b842cfeba49d2dfec9638de2179
# Verified Classes:   RevocationWatermarkGap, Credential, Watermark
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class RevocationWatermarkGap(RuntimeError):
    """Raised when a credential's issuance seq exceeds the drained revocation
    watermark for its issuer, meaning the consumer is blind to any revocation
    that occurred in the gap. Must never be downgraded to a trust-score check."""
    pass


@dataclass(frozen=True)
class Credential:
    issuer: str
    agent_id: str
    issued_seq: int          # issuer's revocation_seq at signing time
    issued_head_hash: str    # issuer's warrant-head hash at signing time


@dataclass(frozen=True)
class Watermark:
    drained_seq: int
    verified_head_hash: str


def admit_call(
    cred: Credential,
    watermark: Watermark | None,
    issuer_advertised_head: str,
    trust_score: float,
    score_age_ms: int,
    score_freshness_ms: int = 500,
) -> str:
    # Hard bit first: revocation completeness. Never gated by score freshness.
    if watermark is None:
        raise RevocationWatermarkGap(
            f"no watermark for issuer {cred.issuer!r}; credential "
            f"seq={cred.issued_seq} is unverifiable — re-fetch required"
        )
    if cred.issued_seq > watermark.drained_seq:
        raise RevocationWatermarkGap(
            f"credential seq={cred.issued_seq} for {cred.agent_id!r} exceeds "
            f"drained watermark {watermark.drained_seq} for issuer "
            f"{cred.issuer!r}: revocation state in gap is unknown"
        )
    if watermark.verified_head_hash != issuer_advertised_head:
        raise RevocationWatermarkGap(
            f"watermark head {watermark.verified_head_hash[:12]} != issuer "
            f"advertised head {issuer_advertised_head[:12]}: consumer is one "
            f"relay-hop behind; seq equality insufficient"
        )

    # Only now may the soft signal (trust score) shape policy, not admission.
    if score_age_ms > score_freshness_ms:
        return "admit_with_restricted_scope"  # stale score -> narrow tools
    if trust_score < 0.5:
        return "admit_with_restricted_scope"
    return "admit"


def handle_inbound(cred, watermark, advertised_head, score, age_ms):
    try:
        return admit_call(cred, watermark, advertised_head, score, age_ms)
    except RevocationWatermarkGap as gap:
        # Fail closed: hold for drain or reject. Never fall through to score.
        return f"PENDING_REVOCATION: {gap}"
