# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-capability-warrant-s-trust-broker-chain-outlives-the-credential-it-delegates.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-capability-warrant-s-trust-broker-chain-outlives-the-credential-it-delegates.warrant.json
# Markdown SHA-256:   1c7c8b0e775ac1544ad7d1dffae59941d074739bd4095b6a1c6fa7af5b7ed5c0
# Code AST SHA-256:   5cba1982e9c542abad942d4bb03e1f0f4cdc23677e54c89700ece5b9ce0697ea
# Verified Classes:   WarrantLineageFork
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class WarrantLineageFork(RuntimeError):
    """Raised when a session would carry warrants from more than one trust root,
    or when a transport re-mint would advance warrant epoch under the same root
    without an explicit refresh grant."""
    pass


def admit_or_halt(session, presented_warrant, transport_token, now):
    # session: {id, pinned_root, warrant_epoch, refresh_grant}
    # presented_warrant: {issuer_root, epoch, not_after, delegation_depth}
    # transport_token: {not_after}

    if session["pinned_root"] is None:
        session["pinned_root"] = presented_warrant["issuer_root"]

    # Invariant 1: trust-root pinning per session.
    if presented_warrant["issuer_root"] != session["pinned_root"]:
        raise WarrantLineageFork(
            f"session {session['id']} pinned to root {session['pinned_root']}, "
            f"got warrant from root {presented_warrant['issuer_root']}"
        )

    # Invariant 2: epoch may only advance with an explicit refresh grant.
    if presented_warrant["epoch"] > session["warrant_epoch"]:
        if session["refresh_grant"] is None:
            raise WarrantLineageFork(
                f"epoch bump {session['warrant_epoch']}->{presented_warrant['epoch']} "
                f"on session {session['id']} without refresh_grant"
            )

    # Invariant 3: transport expiry with live warrant is a halt, not a retry.
    if transport_token["not_after"] <= now < presented_warrant["not_after"]:
        if session["refresh_grant"] is None:
            raise WarrantLineageFork(
                f"transport expired at {transport_token['not_after']} while warrant "
                f"live until {presented_warrant['not_after']}; re-mint would fork lineage"
            )

    session["warrant_epoch"] = presented_warrant["epoch"]
    return {"admitted": True, "session_id": session["id"], "epoch": session["warrant_epoch"]}
