# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-enforce-scope-attenuation-when-delegating-capabilities-across-a-multi-hop-agent-chain-and-why-does-intersection-only-attenuatio.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-enforce-scope-attenuation-when-delegating-capabilities-across-a-multi-hop-agent-chain-and-why-does-intersection-only-attenuatio.warrant.json
# Markdown SHA-256:   8fc37c5450fb61e0390e79a3b4ec051d41baf89e9db30ec43e2e18c8e8027bc0
# Code AST SHA-256:   1c67371fe35cf315b1b2980a9a32ab9212f9a8c538ef029cdb3d94e88e6d84bc
# Verified Classes:   AttenuationError, ScopePredicate
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet, Optional

class AttenuationError(RuntimeError):
    """Raised when a delegated scope is not a provable subset of the parent's scope."""
    pass

@dataclass(frozen=True)
class ScopePredicate:
    namespace: str
    pattern: str                 # e.g. "/tenant/A/reports/*"
    max_bytes: int               # upper bound on resource size
    tenants: FrozenSet[str] = field(default_factory=frozenset)

def _pattern_subset(child: str, parent: str) -> bool:
    # Prefix-glob subset: child must be contained in parent's glob.
    if not parent.endswith("/*"):
        return child == parent
    return child.startswith(parent[:-1])

def meet(parent: ScopePredicate, child: ScopePredicate) -> ScopePredicate:
    """Lattice meet on the full predicate. Rejects widening on ANY axis."""
    if parent.namespace != child.namespace:
        raise AttenuationError(
            f"namespace mismatch: parent={parent.namespace} child={child.namespace}")
    if not _pattern_subset(child.pattern, parent.pattern):
        raise AttenuationError(
            f"pattern widening: child={child.pattern} not subset of parent={parent.pattern}")
    if child.max_bytes > parent.max_bytes:
        raise AttenuationError(
            f"constraint widening on max_bytes: child={child.max_bytes} > parent={parent.max_bytes}")
    if not child.tenants.issubset(parent.tenants) and parent.tenants:
        raise AttenuationError(
            f"tenant widening: child tenants {set(child.tenants)} not subset of {set(parent.tenants)}")
    # Meet is the tighter of the two on every axis.
    return ScopePredicate(
        namespace=parent.namespace,
        pattern=child.pattern if _pattern_subset(child.pattern, parent.pattern) else parent.pattern,
        max_bytes=min(parent.max_bytes, child.max_bytes),
        tenants=child.tenants if child.tenants else parent.tenants,
    )

def delegate_chain(root: ScopePredicate, hops: list[ScopePredicate]) -> ScopePredicate:
    """Replay the chain against the running meet; reject any hop that widens."""
    running = root
    for i, hop in enumerate(hops):
        try:
            running = meet(running, hop)
        except AttenuationError as e:
            raise AttenuationError(f"hop {i} rejected: {e}") from e
    return running

# Reproduces the observed over-grant: same pattern, looser constraint.
root = ScopePredicate("files:read", "/tenant/A/reports/*", max_bytes=1_000_000, tenants=frozenset({"A"}))
hop1 = ScopePredicate("files:read", "/tenant/A/reports/*", max_bytes=50_000_000, tenants=frozenset({"A"}))
try:
    delegate_chain(root, [hop1])
except AttenuationError as e:
    print("BLOCKED:", e)   # constraint widening caught at hop 0
