# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-capability-warrant-attenuation-mismatch-during-cross-org-agent-federation-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-capability-warrant-attenuation-mismatch-during-cross-org-agent-federation-handoff.warrant.json
# Markdown SHA-256:   8d8bc7b63f24d1ebde73aa3dd10cf6d5ec1f69022064a58e1690e964619ec23b
# Code AST SHA-256:   d0f349ea8d71268c3fc587e80f8eb6ab5a5a3b61dd1a5d780431d098499d4f58
# Verified Classes:   CapabilityWarrantMismatch
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CapabilityWarrantMismatch(RuntimeError):
    """Raised when a discovered capability descriptor exceeds the warrant the broker will enforce."""
    pass


def reconcile_discovery(descriptor: dict, warrant_scope: dict) -> dict:
    """
    Reconcile a foreign-domain capability descriptor against the warrant the
    broker will actually enforce. Returns an effective_contract with any
    attenuation explicitly flagged. Raises if the caller attempts to plan
    against an unreconciled descriptor.
    """
    allowed = warrant_scope.get("pii_redaction_allowed")
    advertised = descriptor.get("input_schema", {}).get("pii_redaction")

    if allowed is None or advertised is None:
        raise CapabilityWarrantMismatch(
            f"cannot reconcile {descriptor.get('capability')}: "
            f"advertised={advertised!r} warrant_scope={allowed!r}"
        )

    effective = [v for v in advertised if v in allowed]
    attenuated = [v for v in advertised if v not in allowed]

    if not effective:
        raise CapabilityWarrantMismatch(
            f"warrant excludes every advertised value for "
            f"{descriptor['capability']}: advertised={advertised} allowed={allowed}"
        )

    return {
        "capability": descriptor["capability"],
        "effective_contract": {**descriptor["input_schema"], "pii_redaction": effective},
        "capability_attenuated": bool(attenuated),
        "excluded_values": attenuated,
    }


def plan_node(descriptor: dict, warrant_scope: dict, chosen_params: dict) -> dict:
    """
    Commit a plan node only after the chosen parameters are validated against
    the reconciled effective contract. Validation MUST precede any irreversible
    plan state (slot reservation, downstream registration, budget allocation).
    """
    reconciled = reconcile_discovery(descriptor, warrant_scope)
    permitted = reconciled["effective_contract"]["pii_redaction"]
    chosen = chosen_params.get("pii_redaction")

    if chosen not in permitted:
        # This is a discovery error, not a retryable transport error.
        raise CapabilityWarrantMismatch(
            f"planner selected pii_redaction={chosen!r} but effective contract "
            f"permits {permitted}; re-enter discovery, do not retry the plan"
        )

    return {"node": descriptor["capability"], "params": chosen_params, "committed": True}
