# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-audit-trail-integrity-loss-when-cross-org-opentelemetry-genai-span-redaction-severs-the-parent-child-causal-chain-req.md # Remnant Warrant: https://corpusfetch.com/c/what-structural-signals-indicate-audit-trail-integrity-loss-when-cross-org-opentelemetry-genai-span-redaction-severs-the-parent-child-causal-chain-req.warrant.json # Markdown SHA-256: abc3bdf2ce1087a312b087fe8577755545d9afcd44b8f934af21970ba12b6597 # Code AST SHA-256: be4723d1463838d19883b673f2dda5b7cd014b302dfb4333df73a3e3b70445cf # Verified Classes: DanglingCausalReferenceError, TombstoneSignatureError, Span, Tombstone # 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 hashlib import hmac from dataclasses import dataclass class DanglingCausalReferenceError(RuntimeError): """Raised when an exported span references a parent that is neither present nor tombstoned, breaking the federated replay DAG invariant.""" pass class TombstoneSignatureError(RuntimeError): """Raised when a redaction tombstone fails signature verification against the originating org's key.""" pass @dataclass(frozen=True) class Span: span_id: str trace_id: str org_id: str parent_span_id: str | None content: bytes @dataclass(frozen=True) class Tombstone: span_id: str trace_id: str org_id: str content_commitment: str # salted hash of unredacted content redaction_decision: str # e.g. "pii_detected", "sampled_out" signature: str # HMAC over (span_id, trace_id, org_id, commitment) def make_tombstone(span: Span, salt: bytes, org_key: bytes, decision: str) -> Tombstone: commitment = hashlib.sha256(salt + span.content).hexdigest() msg = f"{span.span_id}|{span.trace_id}|{span.org_id}|{commitment}".encode() sig = hmac.new(org_key, msg, hashlib.sha256).hexdigest() return Tombstone(span.span_id, span.trace_id, span.org_id, commitment, decision, sig) def export_with_tombstones( spans: list[Span], to_redact: set[str], salts: dict[str, bytes], org_keys: dict[str, bytes], decision: str = "pii_detected", ) -> tuple[list[Span], list[Tombstone]]: """Export spans, converting any redacted span into a signed tombstone so that cross-org children's parent_span_id references remain resolvable.""" exported = [s for s in spans if s.span_id not in to_redact] exported_ids = {s.span_id for s in exported} tombstones: list[Tombstone] = [] for s in spans: if s.span_id in to_redact: tombstones.append( make_tombstone(s, salts[s.org_id], org_keys[s.org_id], decision) ) tomb_ids = {t.span_id for t in tombstones} # Guardrail: every exported child's parent must resolve to a span or tombstone. for child in exported: p = child.parent_span_id if p is not None and p not in exported_ids and p not in tomb_ids: raise DanglingCausalReferenceError( f"span {child.span_id} (org {child.org_id}) references parent {p} " f"which is neither exported nor tombstoned; refusing to export a " f"dangling causal edge into the federated index" ) return exported, tombstones def verify_federated_trace( exported: list[Span], tombstones: list[Tombstone], org_keys: dict[str, bytes], ) -> None: """Replay verifier: assert the DAG invariant and tombstone authenticity.""" tomb_by_id = {t.span_id: t for t in tombstones} for t in tombstones: msg = f"{t.span_id}|{t.trace_id}|{t.org_id}|{t.content_commitment}".encode() expected = hmac.new(org_keys[t.org_id], msg, hashlib.sha256).hexdigest() if not hmac.compare_digest(expected, t.signature): raise TombstoneSignatureError( f"tombstone {t.span_id} from org {t.org_id} failed signature " f"verification; possible forgery or key mismatch" ) known = {s.span_id for s in exported} | set(tomb_by_id) for s in exported: p = s.parent_span_id if p is not None and p not in known: raise DanglingCausalReferenceError( f"replay: span {s.span_id} has unresolvable parent {p}; " f"causal chain broken and uncertifiable" )