# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-enforces-egress-policy-on-the-serialized-tool-call-boundary-instead-of-the-data-flow-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-enforces-egress-policy-on-the-serialized-tool-call-boundary-instead-of-the-data-flow-boundary.warrant.json
# Markdown SHA-256:   52506a7aff266c1b5ad31ff7b83d0480e0dde4027cc1adebf8bdb5fd4cfc7d4c
# Code AST SHA-256:   d47cc118fadeaf35530de4b6574996922697507a227ae29cb3ab48a139dd05ae
# Verified Classes:   EgressTaintViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class EgressTaintViolation(RuntimeError):
    """Raised when a tool-call payload carries a tainted value to an external sink
    without a matching declassification, or when the runtime is configured to
    enforce egress only at the serialization boundary for a free-text agent."""

    def __init__(self, sink: str, taint_label: str, encoding: str = "unknown"):
        self.sink = sink
        self.taint_label = taint_label
        self.encoding = encoding
        super().__init__(
            f"tainted value ({taint_label}, encoding={encoding}) attempted egress "
            f"to sink={sink} without declassification"
        )


def enforce_egress(payload: str, sink: str, agent_output_mode: str, taint_labels: set[str]) -> None:
    # Invariant: taint is a property of the value, not of its byte encoding.
    # A serialization-boundary regex check is sound only for schema-constrained
    # output with no free-text escape hatch.
    if agent_output_mode == "free_text" and not taint_labels:
        raise EgressTaintViolation(
            sink=sink,
            taint_label="<unlabeled>",
            encoding="serialization-only-policy",
        )

    for label in taint_labels:
        if label.startswith("sensitive:") and not label.endswith(":declassified"):
            raise EgressTaintViolation(sink=sink, taint_label=label, encoding="taint-propagation")


def send_email(to: str, body: str, agent_output_mode: str, taint_labels: set[str]) -> None:
    enforce_egress(payload=body, sink=f"smtp:{to}", agent_output_mode=agent_output_mode, taint_labels=taint_labels)
    # ... actual transmission ...
