"""Field-level structured diff for CMOS 18 candidate outputs. Approach: for each (field, value) in the canonical record, check whether the candidate formatted string contains `str(value)` as a case-sensitive substring. This is deliberately simpler than parsing the candidate — the linter is responsible for structural checks (punctuation, ordering, italics), so the diff only answers: "did the right facts survive the reformat?" Frozen harness module. Edits here invalidate prior scores. """ from __future__ import annotations from dataclasses import dataclass, field @dataclass class DiffResult: matched: list[str] = field(default_factory=list) missing: list[str] = field(default_factory=list) @property def total(self) -> int: return len(self.matched) + len(self.missing) @property def rate(self) -> float: if self.total == 0: return 1.0 return len(self.matched) / self.total def field_diff(candidate: str, canonical: dict) -> DiffResult: """Compare a formatted candidate string against a canonical field dict. Each canonical field counts as matched iff its stringified value appears as a case-sensitive substring of `candidate`. """ result = DiffResult() for key, value in canonical.items(): if str(value) in candidate: result.matched.append(key) else: result.missing.append(key) return result