# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-transitive-prompt-injection-propagation-across-agent-handoffs-before-capability-escalation-outpaces-the-injection-s-proven.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-transitive-prompt-injection-propagation-across-agent-handoffs-before-capability-escalation-outpaces-the-injection-s-proven.warrant.json
# Markdown SHA-256:   6996f030aec514cf8da5b7644e006cd64a2e2c02168dd8419043f126ad20baeb
# Code AST SHA-256:   19a336d68f3e21422e11df9f2363de47a9d2c1262eeb5c1328aecf5c97616aaf
# Verified Classes:   ProvenanceEscalationError, Handoff
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class ProvenanceEscalationError(RuntimeError):
    """Raised when a handoff requests a capability absent from the requester's
    grant manifest at taint-ingestion time — i.e. a synthesized escalation
    laundered from untrusted content across an agent hop."""
    pass


@dataclass(frozen=True)
class Handoff:
    requester_id: str
    suggested_tools: FrozenSet[str]
    taint_set: FrozenSet[str]          # hashes of untrusted sources in lineage
    granted_manifest_at_ingest: FrozenSet[str]  # signed snapshot, immutable


def admit_handoff(handoff: Handoff, executor_tools: FrozenSet[str]) -> FrozenSet[str]:
    # Fail closed if provenance was laundered away.
    if not handoff.taint_set:
        raise ProvenanceEscalationError(
            f"{handoff.requester_id}: taint_set empty — provenance laundering; "
            f"denying all escalations"
        )

    for tool in handoff.suggested_tools:
        # Capability the executor cannot even provide: nothing to escalate.
        if tool not in executor_tools:
            continue

        # Synthesized escalation: requested but never granted at ingest.
        if tool not in handoff.granted_manifest_at_ingest:
            raise ProvenanceEscalationError(
                f"{handoff.requester_id}: requested '{tool}' not in granted "
                f"manifest at ingest; taint lineage={sorted(handoff.taint_set)}; "
                f"rejecting synthesized capability escalation"
            )

    # All suggested capabilities were pre-granted; taint now applies to
    # arguments, not capability identity.
    return handoff.suggested_tools
