# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-mint-a-delegated-warrant-when-a-tool-scoped-consent-grant-s-attenuation-chain-is-only-partially-order.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-mint-a-delegated-warrant-when-a-tool-scoped-consent-grant-s-attenuation-chain-is-only-partially-order.warrant.json
# Markdown SHA-256:   60401d2d8100701ccb73fd66b3b13cd65d39bfaf5a207bb7f8b13267bdc465f5
# Code AST SHA-256:   aef5ffbeae05b4febc9fca4164621ee0d5371a55fb2ff281120b6dfeccbc9853
# Verified Classes:   UndecidableAttenuation, DroppedConstraint, Scope, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class UndecidableAttenuation(RuntimeError):
    """Raised when a child warrant cannot be proven a subset of its parent."""
    pass


class DroppedConstraint(UndecidableAttenuation):
    """Raised when a constraint axis present in an ancestor is absent in the child."""
    pass


@dataclass
class Scope:
    # None means "axis not mentioned at this hop" — NOT "unconstrained".
    path_prefix: Optional[str] = None
    max_bytes: Optional[int] = None


@dataclass
class Warrant:
    issuer: str
    scope: Scope
    parent: Optional["Warrant"] = None


def _subset(axis: str, child_val, parent_val):
    if parent_val is None:
        return True  # parent did not constrain this axis
    if child_val is None:
        raise DroppedConstraint(
            f"axis '{axis}' constrained by ancestor to {parent_val!r} "
            f"but dropped in child; absence is not permission"
        )
    if axis == "max_bytes":
        if child_val > parent_val:
            raise UndecidableAttenuation(
                f"max_bytes {child_val} exceeds ancestor bound {parent_val}"
            )
    elif axis == "path_prefix":
        if not (child_val == parent_val or child_val.startswith(parent_val.rstrip('/') + '/')):
            raise UndecidableAttenuation(
                f"path_prefix {child_val!r} not within ancestor {parent_val!r}; "
                f"cannot decide subset across incomparable namespaces"
            )
    return True


def mint_child(parent: Warrant, issuer: str, child_scope: Scope) -> Warrant:
    # Walk the entire ancestor chain, not just the immediate parent:
    # an intermediate hop may itself have a degraded record.
    anc = parent
    while anc is not None:
        _subset("path_prefix", child_scope.path_prefix, anc.scope.path_prefix)
        _subset("max_bytes", child_scope.max_bytes, anc.scope.max_bytes)
        anc = anc.parent
    return Warrant(issuer=issuer, scope=child_scope, parent=parent)


# Trace of the observed failure: executor drops max_bytes, resets to tool default.
root = Warrant(issuer="root", scope=Scope(path_prefix="/data/tenant-A", max_bytes=10_000_000))
planner = mint_child(root, "planner", Scope(path_prefix="/data/tenant-A", max_bytes=8_000_000))
# Executor's schema lacked the field; naive code passed None -> would widen to 50MB.
try:
    mint_child(planner, "executor", Scope(path_prefix="/data/tenant-A/reports", max_bytes=50_000_000))
except UndecidableAttenuation as e:
    print(f"mint refused: {e}")
