"""Illustrative, bounded adaptation decision evidence; not a training service.""" from dataclasses import asdict, dataclass, fields import hashlib import json import math import sys def text(value): if type(value) is not str or not value.strip() or len(value) > 4096: raise ValueError("nonempty bounded text required") return value def number(value, lower, upper): if type(value) not in (int, float) or not math.isfinite(value): raise ValueError("finite real, not bool required") if value != 0 and abs(value) < sys.float_info.min: raise ValueError("subnormal rejected") if not lower <= value <= upper: raise ValueError("out of range") return value def count(value): if type(value) is not int or not 1 <= value <= 1_000_000: raise ValueError("bounded positive integer required") def digest(value): return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()).hexdigest() STRATEGIES = ("prompt", "retrieval", "fine-tune", "train") DEFICITS = ("knowledge", "capability", "format", "latency", "privacy", "freshness") @dataclass(frozen=True) class Evidence: evidence_id: str strategy: str scope: str model: str data: str policy: str version: str provenance: str eval_digest: str deficit: str cases: int held_out: bool score: float cost: float risk: float latency_ms: float privacy_pass: bool freshness_pass: bool def __post_init__(self): for name in ("evidence_id", "scope", "model", "data", "policy", "version", "provenance", "strategy", "deficit"): text(getattr(self, name)) if self.strategy not in STRATEGIES or self.deficit not in DEFICITS: raise ValueError("unknown strategy or deficit") if type(self.eval_digest) is not str or len(self.eval_digest) != 64 or any(c not in "0123456789abcdef" for c in self.eval_digest): raise ValueError("evaluation content SHA-256 required") count(self.cases) for name in ("held_out", "privacy_pass", "freshness_pass"): if type(getattr(self, name)) is not bool: raise ValueError("boolean required") number(self.score, 0, 1) number(self.cost, 0, 1e9) number(self.risk, 0, 1) number(self.latency_ms, 0, 1e9) @dataclass(frozen=True) class DecisionContract: scope: str model: str data: str policy: str version: str provenance: str deficit: str owner: str rollback: str min_cases: int min_score: float max_cost: float max_risk: float max_latency_ms: float evidence: tuple[Evidence, ...] def __post_init__(self): for name in ("scope", "model", "data", "policy", "version", "provenance", "owner", "rollback", "deficit"): text(getattr(self, name)) if self.deficit not in DEFICITS: raise ValueError("unknown deficit") count(self.min_cases) number(self.min_score, 0, 1) number(self.max_cost, 0, 1e9) number(self.max_risk, 0, 1) number(self.max_latency_ms, 0, 1e9) if type(self.evidence) not in (tuple, list) or not 1 <= len(self.evidence) <= 4: raise ValueError("one through four evaluations required") copied = [] for item in self.evidence: if type(item) is not Evidence: raise ValueError("concrete Evidence required") copied.append(Evidence(**{f.name: getattr(item, f.name) for f in fields(Evidence)})) object.__setattr__(self, "evidence", tuple(copied)) if len({x.evidence_id for x in copied}) != len(copied) or len({x.strategy for x in copied}) != len(copied): raise ValueError("duplicate evidence or strategy") for item in copied: for name in ("scope", "model", "data", "policy", "version", "deficit"): if getattr(item, name) != getattr(self, name): raise ValueError("evaluation binding mismatch: " + name) if not item.held_out or item.cases < self.min_cases: raise ValueError("unqualified held-out evaluation") @dataclass(frozen=True) class Decision: contract_digest: str intervention: str evidence_id: str claim: str def audit(contract): if type(contract) is not DecisionContract: raise ValueError("concrete DecisionContract required") contract = DecisionContract(**{f.name: getattr(contract, f.name) for f in fields(DecisionContract)}) identity = digest(asdict(contract)) by_strategy = {item.strategy: item for item in contract.evidence} for strategy in STRATEGIES: if strategy not in by_strategy: return Decision(identity, "BLOCK", "missing:" + strategy, "INSUFFICIENT_EVIDENCE") item = by_strategy[strategy] if (item.score >= contract.min_score and item.cost <= contract.max_cost and item.risk <= contract.max_risk and item.latency_ms <= contract.max_latency_ms and item.privacy_pass and item.freshness_pass): return Decision(identity, strategy, item.evidence_id, "SMALLEST_SUFFICIENT_IN_THIS_ORDER") return Decision(identity, "BLOCK", "none", "NO_QUALIFIED_INTERVENTION") def example_contract(): common = dict(scope="illustrative-support", model="base-v1", data="fixture-v1", policy="policy-v1", version="1", provenance="synthetic; no customer data", deficit="freshness") evidence = tuple(Evidence(**common, evidence_id="eval-" + strategy, strategy=strategy, eval_digest=digest({"fixture": strategy, "score": score}), cases=100, held_out=True, score=score, cost=2.0, risk=0.1, latency_ms=80.0, privacy_pass=True, freshness_pass=(strategy == "retrieval")) for strategy, score in (("prompt", 0.5), ("retrieval", 0.95))) return DecisionContract(**common, owner="example-owner", rollback="restore base-v1", min_cases=100, min_score=0.9, max_cost=3.0, max_risk=0.2, max_latency_ms=100.0, evidence=evidence) def main(): result = audit(example_contract()) print("example=illustrative_only") print("intervention=" + result.intervention) print("claim=" + result.claim) print("training_required=" + str(result.intervention in ("fine-tune", "train")).lower()) if __name__ == "__main__": main()