# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-treat-a-signed-agent-card-as-authoritative-before-delegating-a-task-across-a-vendor-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-treat-a-signed-agent-card-as-authoritative-before-delegating-a-task-across-a-vendor-boundary.warrant.json
# Markdown SHA-256:   90a5ae877b48c4890a9babe8b5e7e3cdedb46c28c9049220f2d246d422aa4f61
# Code AST SHA-256:   c76a100f44522916e439423b5af0758b436b45104130bd6e00bf83b2801bc57b
# Verified Classes:   DelegationAuthorityError, SignedAgentCard, EndpointAttestation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class DelegationAuthorityError(RuntimeError):
    """Raised when a signed Agent Card does not bind to the endpoint that will receive the task."""
    pass


@dataclass(frozen=True)
class SignedAgentCard:
    url_host: str                 # host from the signed 'url' field
    security_principal: str       # principal named in signed 'securitySchemes'
    signing_unit: str             # org unit that holds the signing key
    signature_valid: bool


@dataclass(frozen=True)
class EndpointAttestation:
    tls_san: tuple                # SANs on the presented certificate
    attested_principal: str       # principal asserted by the terminating endpoint
    terminating_unit: str         # org unit that actually terminates the connection


def authorize_delegation(card: SignedAgentCard, endpoint: EndpointAttestation) -> None:
    if not card.signature_valid:
        raise DelegationAuthorityError(
            f"card signature invalid for {card.url_host}; refusing delegation"
        )

    # Exact hostname match, not wildcard coverage.
    if card.url_host not in endpoint.tls_san:
        raise DelegationAuthorityError(
            f"card url host {card.url_host!r} not present in TLS SAN {endpoint.tls_san!r}; "
            "wildcard or shared-gateway match is insufficient for task delegation"
        )

    # The principal that will receive bytes must be the principal the card names.
    if endpoint.attested_principal != card.security_principal:
        raise DelegationAuthorityError(
            f"signed card names principal {card.security_principal!r} but endpoint "
            f"attests {endpoint.attested_principal!r}; task would be delivered to an "
            "unintended principal"
        )

    # Signing unit and terminating unit must be the same organizational principal.
    if endpoint.terminating_unit != card.signing_unit:
        raise DelegationAuthorityError(
            f"card signed by unit {card.signing_unit!r} but connection terminates at "
            f"unit {endpoint.terminating_unit!r}; shared-infrastructure delegation refused"
        )


def delegate_task(card: SignedAgentCard, endpoint: EndpointAttestation, payload: bytes) -> str:
    authorize_delegation(card, endpoint)
    task_id = hashlib.sha256(payload).hexdigest()[:32]
    # Only reached when card identity, endpoint identity, and receiving principal agree.
    return task_id
