# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-transitive-capability-attenuation-when-a-delegated-warrant-s-scope-predicate-is-evaluated-against-mutabl.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-transitive-capability-attenuation-when-a-delegated-warrant-s-scope-predicate-is-evaluated-against-mutabl.warrant.json
# Markdown SHA-256:   cc64fc5e6ef205e4e88a53389e7e12c4a8a29778652717eddef04ee0f3f2a1f4
# Code AST SHA-256:   62a618aedb0fe095671564af55392ea03702c1a5cd91ea1cbdc811eaccd969ac
# Verified Classes:   VacuousAttenuationError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class VacuousAttenuationError(RuntimeError):
    """Raised when a predicate-based warrant is self-satisfiable via the holder's own mutations."""
    pass


IMMUTABLE_FIELDS = {"content_hash", "created_at", "immutable_tenant_id"}


def validate_predicate_warrant(
    predicate_fields: set[str],
    holder_mutation_capabilities: dict[str, set[str]],
    chain_length: int,
    revalidated_this_hop: bool,
) -> None:
    """
    Enforce the condition of validity for predicate-based capability attenuation.

    predicate_fields: fields referenced by the delegated scope predicate.
    holder_mutation_capabilities: field -> set of capabilities the holder has
        that can mutate that field (across ALL warrants, not just this branch).
    chain_length: number of hops from root delegator to this holder.
    revalidated_this_hop: whether this hop re-intersected against the full
        downstream warrant set.
    """
    if chain_length > 1 and not revalidated_this_hop:
        raise VacuousAttenuationError(
            f"multi-hop chain (len={chain_length}) forwarded predicate warrant "
            f"without re-intersecting against full downstream warrant set"
        )

    for field in predicate_fields:
        if field in IMMUTABLE_FIELDS:
            continue
        mutators = holder_mutation_capabilities.get(field, set())
        if mutators:
            raise VacuousAttenuationError(
                f"predicate references mutable field {field!r}; holder can mutate it "
                f"via capabilities {sorted(mutators)} — attenuation is self-satisfiable "
                f"and must be frozen to an explicit resource set or rejected"
            )


def accept_delegated_warrant(warrant: dict, holder_caps: dict[str, set[str]]) -> dict:
    if warrant["kind"] == "predicate":
        validate_predicate_warrant(
            predicate_fields=set(warrant["predicate_fields"]),
            holder_mutation_capabilities=holder_caps,
            chain_length=warrant["chain_length"],
            revalidated_this_hop=warrant.get("revalidated", False),
        )
        # Freeze to explicit set at receipt; never re-evaluate live.
        return {"kind": "frozen_set", "resources": materialize(warrant), "source": warrant["id"]}
    return warrant


def materialize(warrant: dict) -> list[str]:
    # Snapshot current state into an explicit resource enumeration.
    return [r for r in warrant["candidate_resources"] if warrant["predicate"](r)]
