# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-an-aaif-attestation-from-its-parent-agent.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-an-aaif-attestation-from-its-parent-agent.warrant.json
# Markdown SHA-256:   84f352008d93791e788cbaa1f3f88d34b4a7ec1e6cb1322a4461c016f50de24f
# Code AST SHA-256:   a94366820bfeba17de138f05851b5b2630509172fd4774578e09976145ed0c3e
# Verified Classes:   AttestationInheritanceError, Attestation
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet


class AttestationInheritanceError(RuntimeError):
    """Raised when a child agent attempts to present a parent's AAIF attestation
    in a context where subject continuity is required."""


@dataclass(frozen=True)
class Attestation:
    issuer: str
    subject: str
    scope: FrozenSet[str]
    not_after: float  # epoch seconds


def authorize_child_presentation(
    parent: Attestation,
    child_id: str,
    child_scope: FrozenSet[str],
    child_logical_lifetime: float,
    now: float,
    downstream_enforces_per_subject_policy: bool,
    child_can_spawn: bool,
) -> Attestation:
    """Decide whether the child may present the parent's attestation as-is.

    Returns the attestation the child is permitted to present. Raises if the
    only safe answer is re-issuance and the caller attempted inheritance.
    """
    if parent.subject != child_id:
        # The parent's attestation names the parent, not the child.
        if downstream_enforces_per_subject_policy:
            raise AttestationInheritanceError(
                f"child {child_id!r} cannot inherit attestation subject "
                f"{parent.subject!r}: downstream enforces per-subject policy"
            )
        if child_can_spawn:
            raise AttestationInheritanceError(
                f"child {child_id!r} may spawn further agents; inheritance "
                f"would make revocation a transitive closure problem"
            )
        if not child_scope.issubset(parent.scope):
            raise AttestationInheritanceError(
                f"child scope {sorted(child_scope)} not a subset of parent "
                f"scope {sorted(parent.scope)}: privilege widening via inheritance"
            )
        parent_remaining = parent.not_after - now
        if child_logical_lifetime > parent_remaining:
            raise AttestationInheritanceError(
                f"child lifetime {child_logical_lifetime}s exceeds parent "
                f"attestation remaining TTL {parent_remaining}s: clock-ownership "
                f"divergence will surface as a spurious expiry"
            )
    # Narrow band: ephemeral, single-hop, strictly-subset, same-TTL continuation.
    return parent


def mint_delegation(parent: Attestation, child_id: str, child_scope: FrozenSet[str], now: float) -> Attestation:
    if not child_scope.issubset(parent.scope):
        raise AttestationInheritanceError(
            f"delegation to {child_id!r} requests scope outside parent authority"
        )
    return Attestation(
        issuer=parent.subject,
        subject=child_id,
        scope=child_scope,
        not_after=parent.not_after,  # child cannot outlive delegator
    )
