# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-peer-agent-card-whose-capabilities-statetransitionhistory-is-absent-during-cross-vendor-delegation.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-peer-agent-card-whose-capabilities-statetransitionhistory-is-absent-during-cross-vendor-delegation.warrant.json
# Markdown SHA-256:   75313f814b504c901fda8fe83f7c7956ec260b4741d1d775227691f227c3b1c9
# Code AST SHA-256:   0f23844bc2f517254779338524baa3753071ca9eed7a7d4cd58294b2143a90f9
# Verified Classes:   AgentCardBoundaryError, AgentCardBoundaryError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class AgentCardBoundaryError(RuntimeError):
    """Raised when a peer Agent Card cannot support the caller's recovery contract."""
    pass


def validate_peer_card_for_delegation(
    card: dict,
    *,
    caller_has_independent_reconciliation: bool,
    task_is_long_lived: bool,
) -> None:
    caps = card.get("capabilities") or {}
    history = caps.get("stateTransitionHistory", "__ABSENT__")

    if not task_is_long_lived or caller_has_independent_reconciliation:
        # Caller can survive any history semantics; no gate needed.
        return

    if history == "__ABSENT__":
        raise AgentCardBoundaryError(
            f"Peer {card.get('name', '<unnamed>')} omits "
            "capabilities.stateTransitionHistory; treating as UNKNOWN, not false. "
            "Caller has no independent reconciliation path and task is long-lived, "
            "so delegation would risk a working-state deadlock on reconnect."
        )
    if history is False:
        raise AgentCardBoundaryError(
            f"Peer {card.get('name', '<unnamed>')} explicitly disables "
            "stateTransitionHistory. Caller requires replay semantics for this task."
        )
    if history is not True:
        raise AgentCardBoundaryError(
            f"Peer {card.get('name', '<unnamed>')} returned non-boolean "
            f"stateTransitionHistory={history!r}; card is malformed."
        )
    # history is True: peer commits to replay semantics, proceed.

class AgentCardBoundaryError(RuntimeError):
    """Raised when a peer Agent Card cannot support the caller's recovery contract."""
    pass


def validate_peer_card_for_delegation(
    card: dict,
    *,
    caller_has_independent_reconciliation: bool,
    task_is_long_lived: bool,
) -> None:
    caps = card.get("capabilities") or {}
    history = caps.get("stateTransitionHistory", "__ABSENT__")

    if not task_is_long_lived or caller_has_independent_reconciliation:
        return

    if history == "__ABSENT__":
        raise AgentCardBoundaryError(
            f"Peer {card.get('name', '<unnamed>')} omits "
            "capabilities.stateTransitionHistory; treating as UNKNOWN, not false. "
            "Caller has no independent reconciliation path and task is long-lived, "
            "so delegation would risk a working-state deadlock on reconnect."
        )
    if history is False:
        raise AgentCardBoundaryError(
            f"Peer {card.get('name', '<unnamed>')} explicitly disables "
            "stateTransitionHistory. Caller requires replay semantics for this task."
        )
    if history is not True:
        raise AgentCardBoundaryError(
            f"Peer {card.get('name', '<unnamed>')} returned non-boolean "
            f"stateTransitionHistory={history!r}; card is malformed."
        )
