# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-quarantine-taint-propagated-write-authority-when-a-sandboxed-tool-s-return-value-re-taints-an-already-cleared-argument-channel.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-quarantine-taint-propagated-write-authority-when-a-sandboxed-tool-s-return-value-re-taints-an-already-cleared-argument-channel.warrant.json
# Markdown SHA-256:   82f1db6fb52adc04a7f628799abeb0abf682fec1d68cb8b528f934dfaa4631df
# Code AST SHA-256:   1dc61a954983af37844689742f8d9cda37c91bc4ff6ff0f3467418f7714d507c
# Verified Classes:   TaintBoundaryViolation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class TaintBoundaryViolation(RuntimeError):
    """Raised when a declassified value would flow through a non-pure callee
    to an effectful sink, violating the single-effect declassification invariant."""
    pass


PURE = "pure"
ENV_READING = "env_reading"
PROBABILISTIC = "probabilistic"

# Callee purity registry: the boundary is drawn by what the callee reads.
PURITY = {
    "strip_markup": PURE,
    "write_file": ENV_READING,
    "summarize": PROBABILISTIC,
    "fetch": ENV_READING,
}


def call_boundary(callee: str, args: dict, arg_labels: dict, declassification_origin: str | None):
    """Enforce the single-effect declassification invariant at a call boundary.

    arg_labels maps argument name -> None (trusted) or an origin string (untrusted).
    declassification_origin is set if any argument was cleared by a sanitizer this step.
    """
    purity = PURITY.get(callee)
    if purity is None:
        raise TaintBoundaryViolation(f"unregistered callee {callee!r}: cannot classify purity")

    # Any untrusted argument to a non-pure callee is a boundary violation.
    if purity != PURE and any(label is not None for label in arg_labels.values()):
        tainted = [k for k, v in arg_labels.items() if v is not None]
        raise TaintBoundaryViolation(
            f"untrusted argument(s) {tainted} passed to {purity} callee {callee!r}"
        )

    # A declassified argument to a probabilistic transform is the amplifier case:
    # the transform's output must be reset, not inherited. Refuse to proceed
    # silently; the caller must opt in to the reset explicitly.
    if purity == PROBABILISTIC and declassification_origin is not None:
        raise TaintBoundaryViolation(
            f"declassified value (origin={declassification_origin}) passed to "
            f"probabilistic callee {callee!r}; output must be re-tainted to origin, "
            f"not inherited as TRUSTED"
        )

    # Pure callee: declassification stays transitive, return label is the join
    # of argument labels (which is None here, by the check above).
    return {"return_label": None if purity == PURE else declassification_origin}


def guarded_write(path: str, summary: str, summary_label: str | None):
    """Effectful sink. Refuses any untrusted argument, including a value
    derived from a prior declassification event."""
    if summary_label is not None:
        raise TaintBoundaryViolation(
            f"write_file refused: summary carries taint origin={summary_label!r}; "
            f"obtain a fresh non-transitive declassification or a policy approval"
        )
    return {"written": True, "path": path}
