# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-semantic-convention-drift-in-span-attributes-before-it-corrupts-token-level-cost-attribution.md # Remnant Warrant: https://corpusfetch.com/c/how-does-an-instance-detect-semantic-convention-drift-in-span-attributes-before-it-corrupts-token-level-cost-attribution.warrant.json # Markdown SHA-256: 302ab4482bb567b094558afdb7b47e46ea345750a236386a48a59861859ae21f # Code AST SHA-256: 0350336aef2556fd591fe3dd02aafafee7c036d2ac7953ca24a761b6d3aa58aa # Verified Classes: SemanticDriftUnresolved, SpanUsage # Defensive Guards: 1 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 statistics from dataclasses import dataclass from typing import Optional class SemanticDriftUnresolved(RuntimeError): """Raised when a usage-attribute distribution shift cannot be explained by a known sibling attribute and no provider ground-truth anchor exists.""" pass @dataclass class SpanUsage: emitter_version: str model: str workload_shape: str input_tokens: int cache_read_tokens: Optional[int] cache_creation_tokens: Optional[int] provider_billed_tokens: Optional[int] # ground-truth anchor, if joined def check_emitter_drift( spans: list[SpanUsage], reference_version: str, tolerance: float = 0.05, ) -> dict: """Compare an emitter's input_tokens distribution against a reference emitter for the same (model, workload_shape) tuple. Returns a verdict dict. Raises SemanticDriftUnresolved if the shift is unexplained and no anchor exists to reconcile a shadow ledger. """ if not spans: return {"verdict": "no_data"} by_tuple: dict[tuple[str, str], list[SpanUsage]] = {} for s in spans: by_tuple.setdefault((s.model, s.workload_shape), []).append(s) for (model, shape), group in by_tuple.items(): ref = [s.input_tokens for s in group if s.emitter_version == reference_version] test = [s.input_tokens for s in group if s.emitter_version != reference_version] if len(ref) < 30 or len(test) < 30: continue ref_median = statistics.median(ref) test_median = statistics.median(test) shift = (test_median - ref_median) / ref_median if ref_median else 0.0 if abs(shift) <= tolerance: continue # within noise; no action # Is the shift explained by a sibling attribute? sibling_volume = sum( (s.cache_read_tokens or 0) + (s.cache_creation_tokens or 0) for s in group if s.emitter_version != reference_version ) test_volume = sum(test) explained = test_volume > 0 and abs(sibling_volume / test_volume - abs(shift)) < 0.10 if explained: return { "verdict": "drift_explained", "model": model, "workload_shape": shape, "shift": shift, "action": "apply_variant_mapping_and_flag_emitter", } # Unexplained shift: need a ground-truth anchor to reconcile. anchored = [ s for s in group if s.emitter_version != reference_version and s.provider_billed_tokens is not None ] if not anchored: raise SemanticDriftUnresolved( f"Unexplained input_tokens shift {shift:+.1%} for " f"model={model} shape={shape} with no provider anchor; " f"quarantine spans to shadow ledger, do not bill via fallback." ) return { "verdict": "drift_unexplained_anchored", "model": model, "workload_shape": shape, "shift": shift, "action": "quarantine_and_reconcile_against_provider", } return {"verdict": "stable"}