Given request records with optional transcript text, produce a packet
with aggregate counts and no raw transcript content.
Hidden answer: invariant, tests, and Python solution
Invariant: the output contains counts, versions, and metric summaries
only, with rare slices bucketed so a one-off language/device pair
does not identify a user. Test that transcript, audio path, user id,
and raw text fields never appear in the packet, that missing latency
values do not crash the report, and that slice keys are stable
strings suitable for JSON incident reports.
from collections import Counter, defaultdict
from math import ceil
def nearest_rank(values, percentile):
index = ceil(percentile * len(values)) - 1
return values[index]
def incident_packet(records, min_slice_count=3):
counts = Counter()
raw_latency = defaultdict(list)
versions = Counter()
for row in records:
language = row.get("language", "unknown")
device = row.get("device", "unknown")
key = f"language={language}|device={device}"
counts[key] += 1
if "latency_ms" in row:
raw_latency[key].append(row["latency_ms"])
if "model_version" in row:
versions[row["model_version"]] += 1
safe_counts = Counter()
latency = defaultdict(list)
for key, count in counts.items():
safe_key = key if count >= min_slice_count else "language=rare|device=rare"
safe_counts[safe_key] += count
latency[safe_key].extend(raw_latency[key])
latency_summary = {}
for key, values in latency.items():
if not values:
continue
values = sorted(values)
latency_summary[key] = {
"count": len(values),
"p50_ms": nearest_rank(values, 0.50),
"p95_ms": nearest_rank(values, 0.95),
}
return {
"slice_counts": dict(safe_counts),
"latency": latency_summary,
"model_versions": dict(versions),
}