# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-verify-capability-scope-intersection-at-a2a-handoff-not-just-signature-validity-on-the-agent-card.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-verify-capability-scope-intersection-at-a2a-handoff-not-just-signature-validity-on-the-agent-card.warrant.json
# Markdown SHA-256:   2d929076b7c9b4b614552c0cf753176181905f87a4fce6f17879cb4dcf32ea5b
# Code AST SHA-256:   d26a218108d60a860d11e26422c7e4e8538f6d3b2cfa05d83cc6d612c9ad37a2
# Verified Classes:   CapabilityScopeDivergence
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class CapabilityScopeDivergence(RuntimeError):
    """Raised when the operation a remote A2A agent executed diverges from the
    operation the caller requested, despite a validly signed Agent Card and a
    passing capability-negotiation string match."""
    pass


def admit_delegation(
    requested_operation: str,
    signed_card_operations: list[str],
    runtime_warrant: dict | None,
    executed_operation: str | None,
) -> None:
    # Step 1: signature already verified upstream; card is trusted as identity.
    # Step 2: negotiation string-match against the signed card.
    if requested_operation not in signed_card_operations:
        raise CapabilityScopeDivergence(
            f"requested {requested_operation!r} not advertised in signed card "
            f"{signed_card_operations!r}"
        )

    # Step 3: the boundary check the card cannot perform — runtime scope.
    # A card advertising a coarse skill does NOT prove the runtime will execute
    # the narrow operation. Demand a runtime warrant binding task->operation.
    if runtime_warrant is not None:
        warranted = runtime_warrant.get("operation")
        if warranted != requested_operation:
            raise CapabilityScopeDivergence(
                f"runtime warrant authorizes {warranted!r}, "
                f"caller requested {requested_operation!r}"
            )
    else:
        # No warrant: caller must diff requested vs executed post-hoc.
        if executed_operation is None:
            raise CapabilityScopeDivergence(
                "no runtime warrant and no executed-operation echo; "
                "cannot confirm the remote agent ran the requested operation"
            )
        if executed_operation != requested_operation:
            raise CapabilityScopeDivergence(
                f"remote executed {executed_operation!r} but caller requested "
                f"{requested_operation!r} — silent downgrade to broader scope"
            )


def consume_task_result(task: dict) -> dict:
    admit_delegation(
        requested_operation=task["requested_operation"],
        signed_card_operations=task["card_operations"],
        runtime_warrant=task.get("warrant"),
        executed_operation=task.get("status", {}).get("metadata", {}).get("executed_operation"),
    )
    return task["artifact"]
