# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-bind-its-spiffe-id-to-the-tool-call-transcript-rather-than-to-the-process-before-cross-org-federation-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-bind-its-spiffe-id-to-the-tool-call-transcript-rather-than-to-the-process-before-cross-org-federation-handoff.warrant.json
# Markdown SHA-256:   fcfc2888917d16e88c1de543debc850fad705d82d43958d06f7907b871c53e7a
# Code AST SHA-256:   ed716d75fe23a3aac804d0e9f86f0c0441c19b43999d36844109e0650e6a89c0
# Verified Classes:   FederationBindingError, SVID, ActionWarrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import hashlib
import json
from dataclasses import dataclass


class FederationBindingError(RuntimeError):
    """Raised when an SVID is valid but not bound to the action it vouches for."""
    pass


@dataclass
class SVID:
    spiffe_id: str
    serial: str
    key_material: bytes


@dataclass
class ActionWarrant:
    """Signed commitment carried across checkpoint/resume and federation boundaries."""
    task_id: str
    sequence: int
    transcript_digest: str
    signer_spiffe_id: str
    signer_serial: str


def digest_action(tool_name: str, params: dict) -> str:
    canonical = json.dumps({"tool": tool_name, "params": params}, sort_keys=True)
    return hashlib.sha256(canonical.encode()).hexdigest()


def validate_federation_call(
    svid: SVID,
    warrant: ActionWarrant,
    requested_tool: str,
    requested_params: dict,
    trusted_bundle_serials: set[str],
    task_creation_serial: str,
) -> None:
    # 1. SVID must be in the trust bundle.
    if svid.serial not in trusted_bundle_serials:
        raise FederationBindingError(
            f"SVID serial {svid.serial} not in trust bundle"
        )

    # 2. Warrant signer must match the SVID presenting it.
    if warrant.signer_spiffe_id != svid.spiffe_id:
        raise FederationBindingError(
            f"warrant signer {warrant.signer_spiffe_id} != svid {svid.spiffe_id}"
        )

    # 3. The boundary condition: if the SVID serial differs from the
    #    task-creation serial, a checkpoint/resume occurred and the
    #    warrant MUST carry a transcript digest that matches the action.
    if svid.serial != task_creation_serial:
        expected = digest_action(requested_tool, requested_params)
        if warrant.transcript_digest != expected:
            raise FederationBindingError(
                "post-resume SVID presented without matching transcript digest; "
                "refusing to authorize on SVID alone"
            )

    # 4. Sequence must be monotonic and non-zero to survive replay.
    if warrant.sequence < 1:
        raise FederationBindingError("warrant sequence not monotonic")

    # 5. Never fall back to LLM scope classification at this boundary.
    if "scope_description" in requested_params:
        raise FederationBindingError(
            "natural-language scope at federation boundary is not a valid "
            "authorization proof; require structured action warrant"
        )
