# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-silent-policy-drift-in-cross-org-multi-agent-federation.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-silent-policy-drift-in-cross-org-multi-agent-federation.warrant.json
# Markdown SHA-256:   6c11bd810fb866f6ee68d50be080ee38ebe9e88263f216c105f3f2c6c42db5ac
# Code AST SHA-256:   83d8aba7a830c0941b0c76a776054c58e9d30590d1f4f0802bad616a584c7c5e
# Verified Classes:   PolicyProvenanceError, Delegation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class PolicyProvenanceError(RuntimeError):
    """Raised when a delegation cannot be tied to a resolvable negotiated policy set."""


@dataclass(frozen=True)
class Delegation:
    task_id: str
    policy_hash: str          # identity of the negotiated policy set
    policy_generation: int
    data_classes: frozenset


def admit_subdelegation(
    inbound: Delegation,
    local_policy_hash: str,
    local_generation: int,
    resolvable_hashes: frozenset,
    delta_touches_sensitive: bool,
) -> None:
    # Invariant 1: the referenced policy identity must be resolvable.
    if inbound.policy_hash not in resolvable_hashes:
        raise PolicyProvenanceError(
            f"task {inbound.task_id}: policy hash {inbound.policy_hash!r} "
            f"is not resolvable at this hop; delegation is unauthenticated"
        )

    # Invariant 2: a hop whose local policy has advanced must not silently
    # tighten. It must surface the drift for renegotiation if the delta
    # touches sensitive constraints.
    if inbound.policy_generation < local_generation and delta_touches_sensitive:
        raise PolicyProvenanceError(
            f"task {inbound.task_id}: inbound generation "
            f"{inbound.policy_generation} < local {local_generation} and delta "
            f"touches sensitive constraints; suspend and renegotiate with "
            f"originator instead of proceeding under {local_policy_hash!r}"
        )
