# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-cosigned-skill-manifest-the-transitive-signature-trust-boundary-failure-in-third-party-agent-skill-marketplaces.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-cosigned-skill-manifest-the-transitive-signature-trust-boundary-failure-in-third-party-agent-skill-marketplaces.warrant.json
# Markdown SHA-256:   98067c17637df97af0a9035c7f8b4f2bba52af595d67b524f8ebdd8f3b986bc7
# Code AST SHA-256:   059bc1274e00ee186a5c53ea5a5a7740c512c09e7bafa490f13152b4897c37b2
# Verified Classes:   TaintLaunderingError, Artifact, Cosignature
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from typing import Optional


class TaintLaunderingError(RuntimeError):
    """Raised when a runtime attempts to propagate a cosignature taint
    across a dependency edge the cosigner never committed to."""
    pass


@dataclass(frozen=True)
class Artifact:
    name: str
    version: str
    body: bytes
    sha256: str
    side_effects: dict


@dataclass(frozen=True)
class Cosignature:
    auditor_key: str
    # hash of the structure the auditor actually signed; must commit to
    # the resolved dependency closure, not just the parent body.
    committed_closure_hash: Optional[str]


def verify_and_load(
    parent: Artifact,
    parent_cosig: Cosignature,
    resolved_children: list[Artifact],
    parent_dep_specs: list[dict],
    consumer_policy_clears: callable,
) -> list[Artifact]:
    # Step 1: signature validity is assumed already checked upstream.
    # Step 2: the boundary check — did the auditor commit to THIS closure?
    actual_closure = hashlib.sha256(
        b"".join(c.sha256.encode() for c in sorted(resolved_children, key=lambda a: a.name))
    ).hexdigest()

    if parent_cosig.committed_closure_hash is None:
        # Cosignature covers body only. Taint may NOT propagate.
        for child in resolved_children:
            if not consumer_policy_clears(child):
                raise TaintLaunderingError(
                    f"child {child.name}@{child.version} resolved under "
                    f"parent {parent.name}@{parent.version} whose cosignature "
                    f"does not commit to the closure; consumer policy rejected "
                    f"side_effects={child.side_effects!r}"
                )
        return resolved_children

    if parent_cosig.committed_closure_hash != actual_closure:
        raise TaintLaunderingError(
            f"closure hash mismatch for {parent.name}@{parent.version}: "
            f"cosigner committed {parent_cosig.committed_closure_hash}, "
            f"runtime resolved {actual_closure}; refusing to inherit taint"
        )

    # Step 3: even with a committed closure, reject any unpinned spec that
    # could have resolved differently than the cosigner's view.
    for spec in parent_dep_specs:
        if not spec.get("sha256"):
            raise TaintLaunderingError(
                f"unpinned dependency {spec.get('name')}@{spec.get('range')} "
                f"in cosigned manifest {parent.name}@{parent.version}; "
                f"version ranges are not content-addressed"
            )

    return resolved_children
