# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-federated-capability-warrant-whose-trust-score-exchange-was-signed-against-a-stale-peer-manifest.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-federated-capability-warrant-whose-trust-score-exchange-was-signed-against-a-stale-peer-manifest.warrant.json
# Markdown SHA-256:   58d6c80a15fd493ac38ef283f07c3f49464340e70acf938507bd83b2d745404e
# Code AST SHA-256:   eab52b0c17c84e58c248ab5acff441bfc69ceb2d8557323b31a85bd9724f3259
# Verified Classes:   FederationBindingError, Warrant, TrustStatement
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class FederationBindingError(RuntimeError):
    """Raised when supporting attestations do not name the same subject manifest as the warrant."""
    pass


@dataclass(frozen=True)
class Warrant:
    subject: str
    manifest_hash: str
    scope: frozenset
    issuer_sig_valid: bool


@dataclass(frozen=True)
class TrustStatement:
    subject: str
    manifest_hash: str
    score: float
    ttl_valid: bool
    signature_valid: bool


HIGH_IMPACT = frozenset({"write_file", "external_call", "credential_use"})


def admit(
    warrant: Warrant,
    trust: TrustStatement,
    threshold: float = 0.85,
    old_manifest_scope: Optional[frozenset] = None,
) -> str:
    if not (warrant.issuer_sig_valid and trust.signature_valid and trust.ttl_valid):
        raise FederationBindingError("artifact failed its own signature/ttl check")

    if warrant.subject != trust.subject:
        raise FederationBindingError(
            f"subject mismatch: warrant={warrant.subject} trust={trust.subject}"
        )

    # The invariant: trust must be scored against the warrant's manifest.
    if trust.manifest_hash != warrant.manifest_hash:
        # Determine direction of the scope delta against the scored manifest.
        if old_manifest_scope is None:
            raise FederationBindingError(
                "manifest mismatch with no prior scope to compare; cannot bound direction"
            )
        widened = warrant.scope - old_manifest_scope
        if widened & HIGH_IMPACT:
            raise FederationBindingError(
                f"stale trust score (manifest {trust.manifest_hash}) "
                f"cannot authorize new high-impact scope {sorted(widened)} "
                f"under warrant manifest {warrant.manifest_hash}; re-score required"
            )
        # Narrowing or low-impact widening: admit conservatively on stale score.
        if trust.score < threshold:
            raise FederationBindingError(
                f"stale score {trust.score} below threshold {threshold}"
            )
        return "admitted_stale_conservative"

    if trust.score < threshold:
        raise FederationBindingError(
            f"trust {trust.score} below threshold {threshold}"
        )
    return "admitted"
