# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-redacts-pii-at-the-otel-span-boundary-but-the-parent-trace-context-carries-the-unredacted-prompt.md # Remnant Warrant: https://corpusfetch.com/c/what-happens-when-an-instance-redacts-pii-at-the-otel-span-boundary-but-the-parent-trace-context-carries-the-unredacted-prompt.warrant.json # Markdown SHA-256: 6aa228b2c24903362872d06a9bac016531da49e411f6453b2f64a8d43d92b39f # Code AST SHA-256: b4520cbaf25578d29fc36274d23cee19d193868e1543626265d7790348f6a3ce # Verified Classes: RedactionInvariantViolation, Signal # Defensive Guards: 3 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 re from dataclasses import dataclass, field from typing import Any PII_PATTERNS = [ re.compile(r"\b[\w.]+@[\w.]+\.\w+\b"), re.compile(r"\b(?:\d[ -]?){13,16}\b"), ] class RedactionInvariantViolation(RuntimeError): """Raised when a trace's exported signals are not uniformly redacted.""" @dataclass class Signal: trace_id: str kind: str # "span" | "log" attributes: dict[str, Any] redaction_policy_version: str = "v0" def _contains_pii(text: str) -> bool: return any(p.search(text) for p in PII_PATTERNS) def assert_egress_redaction(signals: list[Signal]) -> None: """Egress-side negative assertion: no signal may leave with raw PII, and all signals for a trace must share one redaction policy version.""" by_trace: dict[str, set[str]] = {} for sig in signals: for key, value in sig.attributes.items(): if isinstance(value, str) and _contains_pii(value): raise RedactionInvariantViolation( f"PII survived redaction on trace={sig.trace_id} " f"kind={sig.kind} attr={key}" ) by_trace.setdefault(sig.trace_id, set()).add(sig.redaction_policy_version) for trace_id, versions in by_trace.items(): if len(versions) > 1: raise RedactionInvariantViolation( f"policy-version skew within trace={trace_id}: {sorted(versions)}" ) # Trigger: parent span redacted, child tool-input span not, log unredacted. signals = [ Signal("4bf9", "span", {"gen_ai.completion": "mail [REDACTED_EMAIL]"}, "v1"), Signal("4bf9", "span", {"gen_ai.tool.input": '{"recipient":"alice@corp.example"}'}, "v1"), Signal("4bf9", "log", {"recipient": "alice@corp.example"}, "v0"), ] try: assert_egress_redaction(signals) except RedactionInvariantViolation as exc: # Correct behavior: fail the export, do not ship the batch. print(f"export blocked: {exc}") raise