# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-pii-taint-escapes-span-level-redaction-through-agent-trace-re-emission.md # Remnant Warrant: https://corpusfetch.com/c/what-structural-signals-indicate-pii-taint-escapes-span-level-redaction-through-agent-trace-re-emission.warrant.json # Markdown SHA-256: 7af2fcb24f78de790a9cf47c5fe4fdba45b2dadb8f7a9a321ded2541e57b8ecd # Code AST SHA-256: 7b32df12b6ed0111d319f4492c507cdeb0b1c57e0de23705b98b5604d94820e8 # Verified Classes: TaintLeakError, Span, RedactionEvent # Defensive Guards: 2 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) import hashlib from dataclasses import dataclass, field from typing import Optional class TaintLeakError(RuntimeError): """Raised when a redaction marker retains invertible residual entropy and a cross-span join key is present, making the trace tainted.""" @dataclass class Span: trace_id: str name: str attributes: dict join_keys: set = field(default_factory=set) @dataclass class RedactionEvent: span_name: str marker: str residual_entropy_bits: float # 0.0 for constant markers join_key: Optional[str] def redact_email(value: str, mode: str) -> RedactionEvent: if mode == "constant": return RedactionEvent("tool_call", "[REDACTED]", 0.0, None) if mode == "masked": # leaks domain + length + first char: ~ log2(26 * 2^8 * 2^4) bits local, domain = value.split("@", 1) marker = f"{local[0]}***@{domain}" return RedactionEvent("tool_call", marker, 18.0, "customer_id") if mode == "hash": h = hashlib.sha256(value.encode()).hexdigest()[:8] # 32-bit output over a 10-digit input space is enumerable return RedactionEvent("tool_call", f"h:{h}", 32.0, "customer_id") raise ValueError(mode) def check_trace_taint(spans: list[Span], events: list[RedactionEvent]) -> bool: join_keys_present = set().union(*(s.join_keys for s in spans)) if spans else set() for ev in events: if ev.residual_entropy_bits <= 0.0: continue if ev.join_key is not None and ev.join_key in join_keys_present: raise TaintLeakError( f"span {ev.span_name!r} marker {ev.marker!r} retains " f"{ev.residual_entropy_bits} bits and joins on {ev.join_key!r}; " f"trace is tainted despite per-span clean verdict" ) return False def emit_trace(spans: list[Span], events: list[RedactionEvent]) -> dict: tainted = check_trace_taint(spans, events) return {"tainted": tainted, "span_count": len(spans)} # Reproduction of the observed failure: trace = [ Span("t1", "tool_call", {"email": "a***@example.com"}, {"customer_id"}), Span("t1", "llm.completion", {"text": "...ends in example.com..."}, {"customer_id"}), ] events = [redact_email("alice@example.com", "masked")] emit_trace(trace, events) # raises TaintLeakError