# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-pass-a2a-conformance-while-silently-dropping-capability-constraints-during-negotiation.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-pass-a2a-conformance-while-silently-dropping-capability-constraints-during-negotiation.warrant.json
# Markdown SHA-256:   3890d742aca1542e79a0881f891fa8674262ca75f812a4ba921c2c12ceccaf1b
# Code AST SHA-256:   aaf7266f724bc42e76c95f223e555d97c11cae8499406bc01d51a26e136c55dd
# Verified Classes:   CapabilityEnforcementDivergence
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CapabilityEnforcementDivergence(RuntimeError):
    """Raised when an executor's enforcement schema is stricter than its
    advertised capability block, causing a silent drop instead of a
    negotiation-time rejection."""
    pass


def negotiate_task(advertised: dict, enforcement: dict, request: dict) -> dict:
    """
    Negotiate an incoming A2A task request against an agent's capability.

    Invariant: the executor must validate against `enforcement`, and any
    request that satisfies `advertised` but violates `enforcement` must be
    rejected at negotiation time -- never silently dropped at execution.
    """
    # 1. Request must be a subset of the advertisement, else it is the
    #    peer's fault and we reject cleanly.
    for field, limit in advertised.items():
        if field in request and request[field] > limit:
            return {"status": "rejected", "reason": f"exceeds advertised {field}"}

    # 2. Request must ALSO satisfy enforcement. If the advertisement
    #    permitted it but enforcement does not, that is OUR divergence,
    #    and it is a protocol violation to accept-then-drop.
    for field, limit in enforcement.items():
        if field in request and request[field] > limit:
            raise CapabilityEnforcementDivergence(
                f"request {field}={request[field]} satisfies advertisement "
                f"(limit {advertised.get(field)}) but violates enforcement "
                f"(limit {limit}); advertisement and enforcement configs "
                f"have diverged -- refusing to accept-then-silently-drop"
            )

    # 3. Both satisfied: accept, and the executor is now contractually
    #    bound to honor every field it just validated.
    return {"status": "accepted", "contract": request}


def certify_agent(advertised: dict, enforcement: dict, boundary_cases: list) -> str:
    """Run boundary payloads against the LIVE executor, not a mock."""
    for case in boundary_cases:
        try:
            negotiate_task(advertised, enforcement, case)
        except CapabilityEnforcementDivergence as exc:
            raise CapabilityEnforcementDivergence(
                f"certification aborted: agent cannot honor its advertised "
                f"capability block; certificate withheld. detail: {exc}"
            ) from exc
    return "certified"
