# 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-credential-when-its-own-warrant-was-attenuated-by-a-sibling-verifier.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-mint-a-delegated-credential-when-its-own-warrant-was-attenuated-by-a-sibling-verifier.warrant.json
# Markdown SHA-256:   03958122e50236c5fe5f552de410e02cedefa003fefb1d5ca02da0e57025ef5c
# Code AST SHA-256:   08e9dfb4e23ff28cb8c56fd8b559edd79a3bcca490d91e3516310eba482a5e81
# Verified Classes:   StaleWarrantError, UndecidableCurrencyError, Warrant, LiveHead
# Defensive Guards:   4 fail-closed invariants (zero external deps)

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


class StaleWarrantError(RuntimeError):
    """Raised when a delegating agent cannot prove parent-warrant currency."""
    pass


class UndecidableCurrencyError(StaleWarrantError):
    """Raised when the attenuation cursor is missing/truncated — fail closed."""
    pass


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    version: int
    scope: FrozenSet[str]
    attenuation_cursor: str | None
    provenance: tuple[str, ...] = field(default_factory=tuple)


@dataclass(frozen=True)
class LiveHead:
    version: int
    scope: FrozenSet[str]


def mint_delegated(
    parent: Warrant,
    child_scope: FrozenSet[str],
    feed,
    delta_seconds: float = 0.5,
    now: float | None = None,
) -> Warrant:
    now = time.monotonic() if now is None else now

    # 1. Currency precondition: cursor must exist and be resolvable.
    if parent.attenuation_cursor is None:
        raise UndecidableCurrencyError(
            f"warrant {parent.warrant_id} has no attenuation_cursor; "
            "cannot prove currency — refusing to mint"
        )

    head: LiveHead = feed.current_head(parent.warrant_id, parent.attenuation_cursor)
    if head is None or (now - feed.last_sync(parent.warrant_id)) > delta_seconds:
        raise StaleWarrantError(
            f"parent {parent.warrant_id} feed unreachable or beyond Δ={delta_seconds}s; "
            "refusing to mint from cached scope"
        )

    # 2. Monotonicity against LIVE scope, not cached scope.
    if not child_scope.issubset(head.scope):
        raise StaleWarrantError(
            f"child scope {sorted(child_scope)} not ⊆ live parent scope "
            f"{sorted(head.scope)} for {parent.warrant_id} v{head.version}; "
            "stale-scope superset mint blocked"
        )

    # 3. Provenance append-only.
    if parent.version != head.version and parent.provenance != feed.provenance(parent.warrant_id, head.version):
        raise StaleWarrantError(
            f"provenance mismatch for {parent.warrant_id}: cached v{parent.version} "
            f"vs live v{head.version}"
        )

    return Warrant(
        warrant_id=f"{parent.warrant_id}.{parent.version + 1}",
        version=parent.version + 1,
        scope=child_scope,
        attenuation_cursor=feed.cursor(parent.warrant_id, head.version),
        provenance=parent.provenance + (f"mint:{parent.warrant_id}@{head.version}",),
    )
