Skip to content

Commit eb0e38b

Browse files
justinchubyCopilot
andauthored
Make MiniCPM oracle hashes portable (#673)
## Summary - canonicalize only the MiniCPM oracle generator source as explicit UTF-8 with LF line endings before computing `generator_sha256` - render, write, and `--check` the oracle fixture as exact UTF-8/LF bytes - pin the committed MiniCPM oracle fixture to LF in Git so Windows clean checkouts remain byte-canonical - add network-free LF/CRLF regression coverage without changing tokenizer semantics, pinned evidence, or expected result hashes ## Root cause PR #660 hashed `Path(__file__).read_bytes()`, so Git CRLF conversion changed the generator identity on Windows. Its text-mode fixture write/check boundary also allowed platform newline translation and universal-newline comparisons rather than enforcing one canonical artifact. ## Validation - `python -m pytest tests/generate_minicpm_tokenizer_oracle_test.py src/mobius/integrations/gguf/_tokenizer_evidence_test.py -q --tb=short` — 32 passed - `python -m pytest tests/build_graph_test.py tests/cli_test.py src/ -q -k "not phi4mm and not apply_weights_unknown" --tb=short` — 8,616 passed, 63 skipped, 12 deselected, 1 subtest passed - `lintrunner f --output oneline --all-files` — passed - independent GPT-5.6 Sol medium review — one Windows checkout finding fixed by pinning the fixture `eol=lf`; affected gates rerun successfully Signed-off-by: Justin Chu <[email protected]> Co-authored-by: Copilot App <[email protected]> Copilot-Session: efad223f-3973-40c7-b001-e292e139f0bf
1 parent af3328f commit eb0e38b

4 files changed

Lines changed: 63 additions & 9 deletions

File tree

‎.gitattributes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/data/gguf_minicpm_tokenizer_oracle.json text eol=lf

‎scripts/generate_minicpm_tokenizer_oracle.py‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ def _sha256(value: bytes) -> str:
9696
return hashlib.sha256(value).hexdigest()
9797

9898

99+
def _canonical_utf8_source_bytes(path: Path) -> bytes:
100+
"""Read source text as UTF-8 and canonicalize checkout line endings to LF."""
101+
text = path.read_text(encoding="utf-8")
102+
return text.replace("\r\n", "\n").replace("\r", "\n").encode("utf-8")
103+
104+
105+
def _generator_sha256(path: Path) -> str:
106+
return _sha256(_canonical_utf8_source_bytes(path))
107+
108+
109+
def _render_fixture(payload: object) -> bytes:
110+
return (json.dumps(payload, ensure_ascii=False, indent=2) + "\n").encode("utf-8")
111+
112+
113+
def _write_or_check_fixture(path: Path, rendered: bytes, *, check: bool) -> None:
114+
if check:
115+
if path.read_bytes() != rendered:
116+
raise SystemExit(f"{path} is stale; regenerate without --check")
117+
else:
118+
path.write_bytes(rendered)
119+
120+
99121
def build_corpus() -> tuple[str, ...]:
100122
"""Return the exact fixed-plus-seeded corpus in oracle execution order."""
101123
generator = random.Random(SEED)
@@ -489,7 +511,7 @@ def main() -> None:
489511

490512
payload = {
491513
"generator": "scripts/generate_minicpm_tokenizer_oracle.py",
492-
"generator_sha256": _sha256(Path(__file__).read_bytes()),
514+
"generator_sha256": _generator_sha256(Path(__file__)),
493515
"serialization": "UTF-8 compact JSON arrays (ensure_ascii=false; separators=(',',':'))",
494516
"llamacpp_commit": UPSTREAM_COMMIT,
495517
"seed": SEED,
@@ -504,12 +526,7 @@ def main() -> None:
504526
"case_count_per_route": len(MODES) * len(corpus),
505527
"routes": routes,
506528
}
507-
rendered = json.dumps(payload, ensure_ascii=False, indent=2) + "\n"
508-
if args.check:
509-
if args.output.read_text(encoding="utf-8") != rendered:
510-
raise SystemExit(f"{args.output} is stale; regenerate without --check")
511-
else:
512-
args.output.write_text(rendered, encoding="utf-8")
529+
_write_or_check_fixture(args.output, _render_fixture(payload), check=args.check)
513530

514531

515532
if __name__ == "__main__":

‎tests/data/gguf_minicpm_tokenizer_oracle.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"generator": "scripts/generate_minicpm_tokenizer_oracle.py",
3-
"generator_sha256": "d5beab3d8012b9370fac72904fa948b0f43c5d190986710eef3c91030f71dde2",
3+
"generator_sha256": "0aad8a9d152f8899b0af0bb82e28fd0e63d58b8657c7f33cc5051bcd66674d02",
44
"serialization": "UTF-8 compact JSON arrays (ensure_ascii=false; separators=(',',':'))",
55
"llamacpp_commit": "8d9af256337d1a501250f9bbf4c0859a654bddd6",
66
"seed": 648,

‎tests/generate_minicpm_tokenizer_oracle_test.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
RANDOM_COUNT,
2323
RANDOM_LENGTH_STOP,
2424
SEED,
25+
_generator_sha256,
2526
_json_bytes,
27+
_render_fixture,
28+
_write_or_check_fixture,
2629
build_corpus,
2730
summarize_results,
2831
)
@@ -37,7 +40,7 @@ def test_committed_fixture_binds_exact_generator_and_corpus() -> None:
3740
corpus = build_corpus()
3841

3942
assert fixture["generator"] == "scripts/generate_minicpm_tokenizer_oracle.py"
40-
assert fixture["generator_sha256"] == hashlib.sha256(_GENERATOR.read_bytes()).hexdigest()
43+
assert fixture["generator_sha256"] == _generator_sha256(_GENERATOR)
4144
assert fixture["serialization"] == (
4245
"UTF-8 compact JSON arrays (ensure_ascii=false; separators=(',',':'))"
4346
)
@@ -56,6 +59,39 @@ def test_committed_fixture_binds_exact_generator_and_corpus() -> None:
5659
assert fixture["case_count_per_route"] == len(MODES) * len(corpus)
5760

5861

62+
def test_generator_and_fixture_bytes_are_deterministic_across_line_endings(
63+
tmp_path: Path,
64+
) -> None:
65+
lf_source = tmp_path / "generator-lf.py"
66+
crlf_source = tmp_path / "generator-crlf.py"
67+
source = bytes("# UTF-8: café\nprint('MiniCPM')\n", encoding="utf-8")
68+
lf_source.write_bytes(source)
69+
crlf_source.write_bytes(source.replace(b"\n", b"\r\n"))
70+
71+
lf_rendered = _render_fixture({"generator_sha256": _generator_sha256(lf_source)})
72+
crlf_rendered = _render_fixture({"generator_sha256": _generator_sha256(crlf_source)})
73+
lf_output = tmp_path / "oracle-lf.json"
74+
crlf_output = tmp_path / "oracle-crlf.json"
75+
_write_or_check_fixture(lf_output, lf_rendered, check=False)
76+
_write_or_check_fixture(crlf_output, crlf_rendered, check=False)
77+
78+
assert _generator_sha256(lf_source) == hashlib.sha256(source).hexdigest()
79+
assert lf_output.read_bytes() == crlf_output.read_bytes() == lf_rendered
80+
assert b"\r\n" not in lf_rendered
81+
82+
83+
def test_fixture_check_requires_exact_utf8_lf_bytes(tmp_path: Path) -> None:
84+
output = tmp_path / "oracle.json"
85+
rendered = _render_fixture({"text": "café", "lines": ["first", "second"]})
86+
output.write_bytes(rendered)
87+
88+
_write_or_check_fixture(output, rendered, check=True)
89+
90+
output.write_bytes(rendered.replace(b"\n", b"\r\n"))
91+
with pytest.raises(SystemExit, match="is stale; regenerate without --check"):
92+
_write_or_check_fixture(output, rendered, check=True)
93+
94+
5995
def test_result_summary_recomputes_hashes_counts_and_first_witness() -> None:
6096
corpus = ("same", "different")
6197
source_mode = [[1], [2]]

0 commit comments

Comments
 (0)