# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-discovered-a2a-agent-card-despite-successful-schema-validation.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-discovered-a2a-agent-card-despite-successful-schema-validation.warrant.json
# Markdown SHA-256:   cce9e9af4ba9163a507e92285670ec2c04eaf37cebf89f77f5354506b6f87be8
# Code AST SHA-256:   18896ff847d3f14125651561a05425ec89c18997787d400547abb28628997640
# Verified Classes:   AgentCardLifecycleMismatch
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AgentCardLifecycleMismatch(RuntimeError):
    """Raised when a schema-valid A2A v1.0 Agent Card passes discovery but
    fails the post-probe lifecycle reconciliation invariant."""
    pass


def verify_peer_lifecycle(card: dict, probe_task_id: str, stream_terminal_state: str,
                          store: "TaskStore", probe_history: list[dict]) -> None:
    # Clause 1: stream terminal must be reconcilable via tasks/get.
    stored = store.get(probe_task_id)
    if stored is None:
        raise AgentCardLifecycleMismatch(
            f"peer {card.get('url')!r} advertised streaming but tasks/get returned "
            f"TaskNotFoundError for probe {probe_task_id!r}; terminal state "
            f"{stream_terminal_state!r} is unreconcilable"
        )
    if stored["status"]["state"] != stream_terminal_state:
        raise AgentCardLifecycleMismatch(
            f"peer {card.get('url')!r} stream-terminal {stream_terminal_state!r} "
            f"diverges from store state {stored['status']['state']!r}"
        )

    # Clause 2: if history is advertised, it must not elide streamed states.
    caps = card.get("capabilities", {})
    if caps.get("stateTransitionHistory") is True:
        seen = {h["state"] for h in probe_history}
        for required in ("submitted", "working"):
            if required not in seen:
                raise AgentCardLifecycleMismatch(
                    f"peer {card.get('url')!r} declares stateTransitionHistory but "
                    f"omits {required!r} from Task.history; cancellation eligibility "
                    f"cannot be reconstructed"
                )
