# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-attenuates-a-delegated-capability-below-the-fixpoint-of-its-own-verification-predicate.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-attenuates-a-delegated-capability-below-the-fixpoint-of-its-own-verification-predicate.warrant.json
# Markdown SHA-256:   1a5888d076265f882cc806469cf2e522b5a354d49837a260799e1daf6f895a27
# Code AST SHA-256:   09d681c6140437bf9dc726dd8cc4b8da36c0fd78227a43c5e20b4489102c1aa6
# Verified Classes:   ConjunctionPartitionRejected, CapabilityConjunctionUnsatisfiable, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

class ConjunctionPartitionRejected(RuntimeError):
    """Raised when a delegator would split a verifier conjunction across warrants."""
    pass

class CapabilityConjunctionUnsatisfiable(RuntimeError):
    """Raised by a verifier when no single warrant satisfies its full predicate."""
    pass

@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    scopes: FrozenSet[str]
    required_conjunction: FrozenSet[str]
    stitch_policy: str = "forbidden"  # safe default

def attenuate(parent: Warrant, child_scopes: FrozenSet[str],
              child_predicate: FrozenSet[str],
              new_id: str) -> Warrant:
    # Least-privilege: child must be a subset of parent.
    if not child_scopes <= parent.scopes:
        raise ConjunctionPartitionRejected(
            f"scope escalation: {child_scopes - parent.scopes} not in parent"
        )
    # Predicate-preserving check: if the child predicate needs a conjunction
    # that the child scopes cannot satisfy atomically, refuse to partition.
    if not child_predicate <= child_scopes:
        raise ConjunctionPartitionRejected(
            f"predicate {child_predicate} not covered atomically by "
            f"scopes {child_scopes}; refusing to emit sibling warrants"
        )
    return Warrant(
        warrant_id=new_id,
        scopes=child_scopes,
        required_conjunction=child_predicate,
        stitch_policy=parent.stitch_policy,
    )

def verify(warrant: Warrant, predicate: FrozenSet[str]) -> None:
    if warrant.stitch_policy == "forbidden":
        if not predicate <= warrant.scopes:
            missing = predicate - warrant.scopes
            raise CapabilityConjunctionUnsatisfiable(
                f"warrant {warrant.warrant_id} missing {missing}; "
                f"stitching forbidden, re-plan at orchestrator"
            )
    else:
        # stitched path must still be authorized by the declared policy
        if not predicate <= warrant.required_conjunction:
            raise CapabilityConjunctionUnsatisfiable(
                f"stitched predicate {predicate} exceeds declared conjunction"
            )
