# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-cross-agent-prompt-injection-escalation-when-capability-scoped-message-passing-is-the-only-trust-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-cross-agent-prompt-injection-escalation-when-capability-scoped-message-passing-is-the-only-trust-boundary.warrant.json
# Markdown SHA-256:   bf32396b6a3437a6b0d6a6cf3702e482c57a8e58a22e4c9ff5722fb843f2c0b1
# Code AST SHA-256:   9baf9f9baa9239807ba8580dbde679fdad9ef0aca8fd4d127d280331c1af59ad
# Verified Classes:   CapabilityLaunderingError, CapabilityProof, Envelope
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet, Optional

class CapabilityLaunderingError(RuntimeError):
    """Raised when a message attempts to direct an action its subject cannot authorize."""

@dataclass(frozen=True)
class CapabilityProof:
    subject: str                 # agent identity that holds the capability
    scopes: FrozenSet[str]       # e.g. {"read:docs", "write:fs:/workspace/out"}
    args_digest: str             # binds proof to exact (action, path, source)

@dataclass
class Envelope:
    sender: str
    proof: Optional[CapabilityProof]
    action: Optional[str]        # None for pure data payloads
    dest: Optional[str]
    source: Optional[str]
    payload: str = ""

def verify_effect(envelope: Envelope, recipient: str,
                  recipient_scopes: FrozenSet[str]) -> None:
    # Data-only envelopes carry no authority and are always admissible.
    if envelope.action is None:
        return

    if envelope.proof is None:
        raise CapabilityLaunderingError(
            f"action={envelope.action!r} from {envelope.sender!r} has no capability proof"
        )

    # Non-transitivity: the proof subject must be the agent that requested
    # the action, not a relay that merely forwarded it.
    if envelope.proof.subject != envelope.sender:
        raise CapabilityLaunderingError(
            f"proof subject {envelope.proof.subject!r} != sender {envelope.sender!r}: "
            "relay cannot confer authority"
        )

    required = set()
    if envelope.action == "write":
        required.add(f"write:fs:{envelope.dest}")
    if envelope.source is not None:
        # Copy is a join: the source read must be authorized too.
        required.add(f"read:{envelope.source}")

    missing_proof = required - envelope.proof.scopes
    if missing_proof:
        raise CapabilityLaunderingError(
            f"proof for {envelope.sender!r} lacks {sorted(missing_proof)}"
        )

    missing_grant = required - recipient_scopes
    if missing_grant:
        raise CapabilityLaunderingError(
            f"recipient {recipient!r} not granted {sorted(missing_grant)}"
        )

    expected = f"{envelope.action}:{envelope.dest}:{envelope.source}"
    if envelope.proof.args_digest != expected:
        raise CapabilityLaunderingError(
            "proof args_digest does not bind to (action, dest, source); "
            "substituted path rejected"
        )
