# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-an-a2a-agent-card-s-advertised-statetransitionhistory-capability-as-a-lie-at-handshake-time-rather-than-at-fi.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-an-a2a-agent-card-s-advertised-statetransitionhistory-capability-as-a-lie-at-handshake-time-rather-than-at-fi.warrant.json
# Markdown SHA-256:   cf142ab0177bd8fcae1aec2854f24464fb0489a78b3f8032a3e26e76e7f150cf
# Code AST SHA-256:   229858ded294ba74dd7fb86fba3e8476513901c14b7ee9cdcfdbe95eba30fc45
# Verified Classes:   UnverifiedHistoryInvariantError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class UnverifiedHistoryInvariantError(RuntimeError):
    """Raised when a delegating client attempts to enable history-based
    reconciliation against a remote A2A agent whose stateTransitionHistory
    capability has not been probe-confirmed as a monotonic, gap-free log."""
    pass


def enable_reconciliation(agent_card: dict, probe_result: dict | None) -> bool:
    """
    Decide whether history-based reconciliation may be enabled for a remote agent.

    agent_card: the parsed Agent Card from /.well-known/agent.json
    probe_result: outcome of a synthetic task that was forced through an
                  intermediate state (e.g. input-required). None if no probe
                  has been run.

    Returns True only if the invariant is confirmed. Raises otherwise.
    """
    advertises = agent_card.get("capabilities", {}).get("stateTransitionHistory", False)
    if not advertises:
        # Card says no history. State-only tracking is the only safe mode.
        return False

    if probe_result is None:
        # The card claims the capability, but we have no evidence the
        # invariant holds. Refuse to silently trust it.
        raise UnverifiedHistoryInvariantError(
            "Agent Card advertises stateTransitionHistory=true but no probe "
            "has confirmed a monotonic, gap-free transition log. "
            "Enable state-only tracking or run a probe before reconciliation."
        )

    history = probe_result.get("history", [])
    if not history:
        raise UnverifiedHistoryInvariantError(
            "Probe task completed but returned an empty history despite "
            "stateTransitionHistory=true. Vendor B failure mode: boolean set, "
            "log never populated."
        )

    # Verify monotonic ordering by sequence or timestamp.
    seqs = [h.get("sequence") for h in history if h.get("sequence") is not None]
    if seqs and seqs != sorted(seqs):
        raise UnverifiedHistoryInvariantError(
            "Probe history is not monotonically ordered. Vendor C failure mode: "
            "multi-shard store with clock skew. Reconciliation would pick the "
            "wrong terminal message."
        )

    # Verify the intermediate state actually appears.
    states = [h.get("state") for h in history]
    if "input-required" not in states:
        raise UnverifiedHistoryInvariantError(
            "Probe was designed to traverse input-required but the returned "
            "history omits it. History is lossy; reconciliation would record a "
            "clean linear trace for a task that actually degraded."
        )

    return True
