# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-verifies-a-delegated-agent-manifest-whose-issuer-chain-crosses-an-org-boundary-mid-credential.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-verifies-a-delegated-agent-manifest-whose-issuer-chain-crosses-an-org-boundary-mid-credential.warrant.json
# Markdown SHA-256:   e54e67ad6ffb03e0d774eadfaf274a4457dd81684c4db9202f253deeeba33f44
# Code AST SHA-256:   d3ba9903b6964d4f58596bd06c8e3f016703c33e373468e3f098617d4513cf35
# Verified Classes:   UnauthenticatedDelegationError, Manifest, VerifiableCredential
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class UnauthenticatedDelegationError(RuntimeError):
    """Raised when a composite agent trust claim lacks a signed edge binding."""
    pass


@dataclass
class Manifest:
    id: str
    controller_key: str
    delegates: list[str]


@dataclass
class VerifiableCredential:
    id: str
    issuer_key: str
    subject_key: str
    authorized_by: Optional[str]      # self-asserted, unauthenticated
    signed_edge_ref: Optional[str]    # signed reference to a manifest id, if present


def verify_composite(manifest: Manifest,
                     vc: VerifiableCredential,
                     trusted_edges: set[tuple[str, str]]) -> bool:
    # Both artifacts individually verify (assumed upstream).
    edge = (manifest.controller_key, vc.issuer_key)

    if vc.signed_edge_ref != manifest.id:
        # The only link between the two halves is self-asserted.
        raise UnauthenticatedDelegationError(
            f"VC {vc.id} claims authorizedBy={vc.authorized_by!r} but carries "
            f"no signed reference to manifest {manifest.id}; split-root "
            f"delegation {edge} cannot be authenticated."
        )

    if edge not in trusted_edges:
        # Node trust passed upstream; edge trust did not.
        raise UnauthenticatedDelegationError(
            f"Delegation edge {edge} is not in the verifier's per-edge trust "
            f"graph; node-level trust is insufficient."
        )

    return True
