# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-award-a-contract-net-bid-whose-attestation-chain-crosses-an-organizational-trust-root-it-cannot-independe.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-award-a-contract-net-bid-whose-attestation-chain-crosses-an-organizational-trust-root-it-cannot-independe.warrant.json
# Markdown SHA-256:   936a2482550d1258262eeb006692cefd710272ef07bdfe064c1af0a9f6432ad9
# Code AST SHA-256:   c59395bf676baf330ed85ac856215608ea454369389ca492d6d10062fa300927
# Verified Classes:   UnanchorableAttestation, Key
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Iterable


class UnanchorableAttestation(RuntimeError):
    """Raised when an outcome attestation chain cannot be closed to a manager-pinned root
    without trusting a bidder-supplied key binding."""
    pass


@dataclass(frozen=True)
class Key:
    key_id: str
    org: str
    # True iff the binding of this key to its org was established out-of-band
    # (federation handshake / pinned-origin JWKS / manager directory).
    out_of_band_bound: bool


def verify_award(
    chain: list[Key],
    pinned_roots: set[str],
    manager_directory: dict[str, str],
    bidder_org: str,
) -> str:
    """Return the resolved anchor root_id, or raise UnanchorableAttestation.

    Invariant: every key in the chain must resolve to a pinned root WITHOUT
    relying on the bidder's assertion about intermediate keys.
    """
    if not chain:
        raise UnanchorableAttestation("empty attestation chain")

    terminal = chain[-1]
    if terminal.key_id not in pinned_roots:
        raise UnanchorableAttestation(
            f"terminal key {terminal.key_id} is not a manager-pinned root"
        )

    for key in chain:
        # A key is admissible only if it is itself pinned, or its org binding
        # is independently resolvable via the manager directory (out-of-band).
        anchored = key.key_id in pinned_roots or (
            key.out_of_band_bound
            and manager_directory.get(key.key_id) == key.org
        )
        if not anchored:
            raise UnanchorableAttestation(
                f"key {key.key_id} (org={key.org}) is bound only by bidder "
                f"assertion; cannot close to a pinned root out-of-band"
            )

    # Degenerate-pin guard: if the only pinned key is the bidder's own root and
    # the CFP demanded an independent executor root, the pin proves nothing
    # about the executor.
    executor_orgs = {k.org for k in chain if k.org != bidder_org}
    if executor_orgs and not any(
        k.org in executor_orgs and k.key_id in pinned_roots for k in chain
    ):
        raise UnanchorableAttestation(
            f"executor org(s) {executor_orgs} have no independently pinned root; "
            f"award would rest on {bidder_org}'s assertion alone"
        )

    return terminal.key_id
