"""Illustrative preference evidence audit; not a trainer or capability benchmark.""" from dataclasses import asdict, dataclass, fields import hashlib import json import math import re import sys def identity(value): if type(value) is not str or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.:/@-]{0,127}", value): raise ValueError("invalid identity") def number(value, low, high): if type(value) not in (int, float) or not low <= value <= high or not math.isfinite(value): raise ValueError("finite non-boolean number required") if value != 0 and abs(value) < sys.float_info.min: raise ValueError("subnormal number") if not low <= value <= high: raise ValueError("numeric range") def count(value, low, high): if type(value) is not int or not low <= value <= high: raise ValueError("integer count range") def digest(value): return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()).hexdigest() def seal(record): if type(record.content_id) is not str: raise ValueError("content digest requires exact string") data = asdict(record) data.pop("content_id") return digest(data) def validate_record(record, cls): if type(record) is not cls: raise ValueError("concrete frozen record required") # Reconstruct to rerun all invariants, even after object.__new__/__setattr__. rebuilt = cls(**{field.name: getattr(record, field.name, None) for field in fields(cls)}) if record != rebuilt or record.content_id != rebuilt.content_id: raise ValueError("missing content digest or noncanonical record") @dataclass(frozen=True) class PreferenceContract: scope: tuple = ("preference-v1", "model-v1", "reference-v1", "policy-v1", "data-v1", "annotator-policy-v1", "eval-v1", "heldout", "source-v1") beta: float = 0.1 min_pairs: int = 2 min_decisive: int = 4 max_disagreement: float = 0.4 content_id: str = "" def __post_init__(self): if type(self.scope) not in (tuple, list) or len(self.scope) != 9: raise ValueError("scope requires nine identities") object.__setattr__(self, "scope", tuple(self.scope)) for value in self.scope: identity(value) if self.scope[7] != "heldout": raise ValueError("evaluation split must be heldout") number(self.beta, 0.001, 10) count(self.min_pairs, 2, 1000) count(self.min_decisive, 2, 10000) number(self.max_disagreement, 0, 0.5) expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("contract digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class PreferencePair: scope: tuple pair_id: str prompt_id: str prompt: str chosen: str rejected: str chosen_position: str votes: tuple log_probabilities: tuple content_id: str = "" def __post_init__(self): if type(self.scope) not in (tuple, list) or len(self.scope) != 9: raise ValueError("scope requires nine identities") object.__setattr__(self, "scope", tuple(self.scope)) for value in (*self.scope, self.pair_id, self.prompt_id): identity(value) for value in (self.prompt, self.chosen, self.rejected): if type(value) is not str or not value.strip() or len(value.encode()) > 8192: raise ValueError("bounded nonempty content required") if self.chosen == self.rejected or type(self.chosen_position) is not str or self.chosen_position not in ("left", "right"): raise ValueError("distinct response content and randomized position required") if type(self.votes) not in (list, tuple): raise ValueError("vote sequence required") count(len(self.votes), 1, 100) normalized = [] for vote in self.votes: if type(vote) not in (list, tuple) or len(vote) != 2: raise ValueError("annotator and label required") annotator, label = vote identity(annotator) if type(label) is not str or label not in ("chosen", "rejected", "tie", "abstain"): raise ValueError("invalid preference label") normalized.append((annotator, label)) if len({item[0] for item in normalized}) != len(normalized): raise ValueError("duplicate annotator") object.__setattr__(self, "votes", tuple(normalized)) if type(self.log_probabilities) not in (tuple, list) or len(self.log_probabilities) != 4: raise ValueError("policy chosen/rejected and reference chosen/rejected required") for value in self.log_probabilities: number(value, -10000, 0) object.__setattr__(self, "log_probabilities", tuple(self.log_probabilities)) expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("pair digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class PreferenceReport: status: str evidence_id: str decisive: int ties: int abstentions: int disagreement: float mean_dpo_loss: float claim: str = "PREFERENCE_EVIDENCE_NOT_REWARD_OR_CAPABILITY" def audit_preferences(contract: PreferenceContract, pairs: tuple) -> PreferenceReport: """Revalidate identities, support, bias indicators, and stable pairwise loss.""" validate_record(contract, PreferenceContract) if type(pairs) not in (tuple, list): raise ValueError("pair sequence required") count(len(pairs), 1, 1000) pairs = tuple(pairs) for pair in pairs: validate_record(pair, PreferencePair) if pair.scope != contract.scope: raise ValueError("model/data/policy/evaluation scope mismatch") for values in ([p.pair_id for p in pairs], [p.prompt_id for p in pairs], [digest(p.prompt) for p in pairs]): if len(set(values)) != len(values): raise ValueError("duplicate pair or prompt evidence") labels = [label for pair in pairs for _, label in pair.votes] decisive = labels.count("chosen") + labels.count("rejected") disagreements = labels.count("rejected") disagreement = disagreements / decisive if decisive else 1.0 losses = [] for pair in pairs: pc, pr, rc, rr = pair.log_probabilities margin = contract.beta * math.fsum((pc, -pr, -rc, rr)) losses.append(max(0.0, -margin) + math.log1p(math.exp(-abs(margin)))) status = "SUPPORTED" if len(pairs) < contract.min_pairs or decisive < contract.min_decisive or any(not any(label in ("chosen", "rejected") for _, label in p.votes) for p in pairs): status = "BLOCK_SPARSE" elif any(sum(label == "chosen" for _, label in p.votes) <= sum(label == "rejected" for _, label in p.votes) for p in pairs): status = "BLOCK_LABEL_DIRECTION" elif disagreement > contract.max_disagreement or {p.chosen_position for p in pairs} != {"left", "right"}: status = "BLOCK_BIAS_REVIEW" evidence_id = digest((contract.content_id, tuple(p.content_id for p in pairs))) return PreferenceReport(status, evidence_id, decisive, labels.count("tie"), labels.count("abstain"), disagreement, math.fsum(losses) / len(losses)) def illustrative_fixture(): contract = PreferenceContract() pairs = tuple(PreferencePair(contract.scope, f"pair-{i}", f"prompt-{i}", f"Illustrative prompt {i}", f"Supported answer {i}", f"Unsupported answer {i}", position, (("rater-1", "chosen"), ("rater-2", "chosen"), ("rater-3", "tie"), ("rater-4", "abstain")), (-2.0, -4.0, -3.0, -3.0)) for i, position in enumerate(("left", "right"))) return contract, pairs def main(): report = audit_preferences(*illustrative_fixture()) print("example=illustrative_only") print(f"status={report.status}") print(f"decisive={report.decisive};ties={report.ties};abstentions={report.abstentions}") print(f"mean_dpo_loss={report.mean_dpo_loss:.6f}") print(f"claim={report.claim}") if __name__ == "__main__": main()