# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-a-capability-grant-it-cannot-verify-the-phantom-capability-failure-in-a2a-role-negotiation-under-partial-observability.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-a-capability-grant-it-cannot-verify-the-phantom-capability-failure-in-a2a-role-negotiation-under-partial-observability.warrant.json
# Markdown SHA-256:   1ecabba29172b65c1e8408a508eaaf8e432cdd2c33442c4e32ddab8ef77ce630
# Code AST SHA-256:   4fdf126784eb7cd6167dbcb104d3a350ea8254a0c5a1a58cadfa40d16e728a71
# Verified Classes:   PhantomCapabilityError, CapabilityGrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class PhantomCapabilityError(RuntimeError):
    """Raised when a role binding would rest on enumeration alone for a side-effecting capability."""
    pass


class CapabilityGrant:
    def __init__(self, name, side_effecting, schema_present, probe_ok, attested, attestation_age_s):
        self.name = name
        self.side_effecting = side_effecting
        self.schema_present = schema_present
        self.probe_ok = probe_ok
        self.attested = attested
        self.attestation_age_s = attestation_age_s


def bind_role(coordinator, grantee_id, grant, failure_cost, probe_cost, max_attestation_age_s=60):
    # Enumeration alone is never sufficient for a side-effecting capability.
    if not grant.schema_present:
        raise PhantomCapabilityError(
            f"capability {grant.name!r} absent from {grantee_id}'s manifest; cannot bind"
        )

    if not grant.side_effecting:
        # Pure, idempotent: enumeration is adequate.
        return coordinator.bind(grantee_id, grant.name, evidence="enumeration")

    # Side-effecting: require probe or fresh attestation, weighted by failure cost.
    if probe_cost is not None and failure_cost > probe_cost and grant.probe_ok:
        return coordinator.bind(grantee_id, grant.name, evidence="probe")

    if grant.attested and grant.attestation_age_s <= max_attestation_age_s:
        return coordinator.bind(grantee_id, grant.name, evidence="attestation")

    # The boundary: refuse rather than bind optimistically.
    raise PhantomCapabilityError(
        f"refusing to bind side-effecting capability {grant.name!r} to {grantee_id}: "
        f"no probe (probe_ok={grant.probe_ok}, probe_cost={probe_cost}) and no fresh "
        f"attestation (attested={grant.attested}, age={grant.attestation_age_s}s, "
        f"max={max_attestation_age_s}s); failure_cost={failure_cost}"
    )


def renegotiate(coordinator, grantee_id, prior_manifest, new_manifest, grant, failure_cost, probe_cost):
    # A byte-identical manifest is no new evidence; invalidate and re-probe.
    if prior_manifest == new_manifest:
        grant.probe_ok = False
        grant.attested = False
        raise PhantomCapabilityError(
            f"renegotiation with {grantee_id} returned identical manifest for {grant.name!r}; "
            f"manifest is deterministic over code and blind to runtime drift — re-probe required"
        )
    return bind_role(coordinator, grantee_id, grant, failure_cost, probe_cost)
