# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-an-inherited-trust-anchor-during-cross-org-federation-handoff-and-what-breaks-when-it-does-not.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-an-inherited-trust-anchor-during-cross-org-federation-handoff-and-what-breaks-when-it-does-not.warrant.json
# Markdown SHA-256:   07570d46474d1a84bdda26005467cb8dc1f070cdd87299801451a7f7c8d9b7e6
# Code AST SHA-256:   7cfc5cd6fc6d6f7d0e53bdda306b2a40fd5b6a6475b9f4d10fffe227d63fea6d
# Verified Classes:   ForeignAnchorInheritanceError, Envelope, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class ForeignAnchorInheritanceError(RuntimeError):
    """Raised when a sub-warrant would inherit a foreign trust anchor."""


@dataclass(frozen=True)
class Envelope:
    org: str
    anchor: str
    scope: frozenset


@dataclass(frozen=True)
class Warrant:
    anchor: str
    origin_anchor: str
    executing_anchor: str
    scope: frozenset


def mint_sub_warrant(inbound: Warrant, envelope: Envelope, requested: frozenset) -> Warrant:
    # The inbound warrant's anchor authorizes the CALLER, not us.
    # We may only mint under our own envelope's anchor.
    if inbound.anchor != envelope.anchor:
        # Foreign-anchored caller re-entering local services: must re-root,
        # and must intersect scope. Refuse silent inheritance.
        negotiated = requested & envelope.scope & inbound.scope
        if not negotiated:
            raise ForeignAnchorInheritanceError(
                f"inbound anchor {inbound.anchor!r} (org scope) has no "
                f"intersection with local envelope {envelope.anchor!r}; "
                f"requested={sorted(requested)} local={sorted(envelope.scope)} "
                f"inbound={sorted(inbound.scope)}"
            )
        return Warrant(
            anchor=envelope.anchor,          # re-rooted, never inherited
            origin_anchor=inbound.origin_anchor,
            executing_anchor=envelope.anchor,
            scope=negotiated,
        )

    # Same-anchor chaining is safe: inherit, but still intersect scope.
    negotiated = requested & envelope.scope & inbound.scope
    if not negotiated:
        raise ForeignAnchorInheritanceError(
            f"intra-org scope intersection empty: requested={sorted(requested)}"
        )
    return Warrant(
        anchor=envelope.anchor,
        origin_anchor=inbound.origin_anchor,
        executing_anchor=envelope.anchor,
        scope=negotiated,
    )


def audit_hop(w: Warrant) -> None:
    # Downstream consumers must see divergence and apply the stricter scope.
    if w.origin_anchor != w.executing_anchor:
        # Cross-org boundary crossed; caller must have already intersected.
        if w.scope is None:
            raise ForeignAnchorInheritanceError(
                f"cross-org warrant {w.origin_anchor!r}->{w.executing_anchor!r} "
                f"carries no negotiated scope"
            )
