# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-peer-s-agent-card-whose-signature-verifies-but-whose-capabilities-streaming-claim-contradicts-the-tran.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-peer-s-agent-card-whose-signature-verifies-but-whose-capabilities-streaming-claim-contradicts-the-tran.warrant.json
# Markdown SHA-256:   047e341eee6b19c5157eb2c9a08c3bc41374432f3e1f5d3662b0f5036b1ad520
# Code AST SHA-256:   610c741f25e3d365c0ed4cb54cd35bb2559f6c297eeb2e0e1c86f97fce43a04b
# Verified Classes:   CapabilityBindingMismatch, AgentCard, NegotiatedBinding
# Defensive Guards:   7 fail-closed invariants (zero external deps)

import json
from dataclasses import dataclass
from typing import Literal


class CapabilityBindingMismatch(RuntimeError):
    """Raised when a signed Agent Card's claim does not match the negotiated transport."""
    pass


@dataclass(frozen=True)
class AgentCard:
    agent_id: str
    signature_valid: bool
    streaming: bool
    state_transition_history: bool
    push_notifications: bool
    capability_epoch: int


@dataclass(frozen=True)
class NegotiatedBinding:
    content_type: str          # e.g. "text/event-stream" or "application/json"
    history_present: bool
    push_delivered: bool


def confirm_binding(
    card: AgentCard,
    binding: NegotiatedBinding,
    *,
    delegator_requires_streaming: bool,
    delegator_requires_history_replay: bool,
    delegator_requires_push: bool,
) -> None:
    """
    Enforce the claim-to-binding invariant. Signature validity is necessary but
    NOT sufficient: every capability bit the delegator depends on must be
    re-confirmed against the wire behavior actually negotiated.
    """
    if not card.signature_valid:
        raise CapabilityBindingMismatch(
            f"card for {card.agent_id} failed signature verification"
        )

    # Streaming claim vs. actual framing.
    if delegator_requires_streaming:
        if not card.streaming:
            raise CapabilityBindingMismatch(
                f"{card.agent_id} card does not advertise streaming; "
                f"delegator requires it"
            )
        if binding.content_type != "text/event-stream":
            raise CapabilityBindingMismatch(
                f"{card.agent_id} card claims streaming=true (epoch "
                f"{card.capability_epoch}) but negotiated Content-Type was "
                f"{binding.content_type!r}; silent downgrade refused"
            )

    # State-transition-history claim vs. actual replay payload.
    if delegator_requires_history_replay:
        if not card.state_transition_history:
            raise CapabilityBindingMismatch(
                f"{card.agent_id} card does not advertise "
                f"stateTransitionHistory; delegator requires replay"
            )
        if not binding.history_present:
            raise CapabilityBindingMismatch(
                f"{card.agent_id} card claims stateTransitionHistory=true "
                f"(epoch {card.capability_epoch}) but tasks/get returned "
                f"empty history; lineage replay impossible"
            )

    # Push-notification claim vs. actual delivery.
    if delegator_requires_push:
        if not card.push_notifications:
            raise CapabilityBindingMismatch(
                f"{card.agent_id} card does not advertise pushNotifications"
            )
        if not binding.push_delivered:
            raise CapabilityBindingMismatch(
                f"{card.agent_id} card claims pushNotifications=true "
                f"(epoch {card.capability_epoch}) but no delivery within "
                f"declared window"
            )


def delegate(card: AgentCard, binding: NegotiatedBinding) -> str:
    confirm_binding(
        card,
        binding,
        delegator_requires_streaming=True,
        delegator_requires_history_replay=True,
        delegator_requires_push=False,  # delegator only polls; inert if false
    )
    return "TASK_SUBMITTED"


if __name__ == "__main__":
    # Reproduces the observed silent downgrade: signature valid, streaming
    # claimed, but the wire returned application/json.
    stale_card = AgentCard(
        agent_id="callee-worker-pool-01",
        signature_valid=True,
        streaming=True,
        state_transition_history=True,
        push_notifications=True,
        capability_epoch=41,
    )
    negotiated = NegotiatedBinding(
        content_type="application/json",
        history_present=False,
        push_delivered=False,
    )
    try:
        delegate(stale_card, negotiated)
    except CapabilityBindingMismatch as e:
        print(f"REFUSED delegation: {e}")
