Skip to content

Commit e539c9b

Browse files
committed
fix: truncate rewritten training JSON
Fixes #1909 Coding-Agent: Codex Codex-Version: codex-cli 0.149.0 Model: gpt-5.6-sol Reasoning-Effort: xhigh
1 parent d5ce577 commit e539c9b

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

dpgen/util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ def convert_training_data_to_hdf5(input_files: list[str], h5_file: str):
140140
systems.append(pp)
141141
f.seek(0)
142142
json.dump(jinput, f, indent=4)
143+
# The rewritten configuration can be shorter than the original.
144+
# Remove any bytes left after the new JSON document.
145+
f.truncate()
143146
systems = list(set(systems))
144147

145148
dlog.info("Combining %d training systems to %s...", len(systems), h5_file)

tests/test_util.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import tempfile
3+
import unittest
4+
from pathlib import Path
5+
from unittest.mock import patch
6+
7+
from dpgen.util import convert_training_data_to_hdf5
8+
9+
10+
class TestConvertTrainingDataToHdf5(unittest.TestCase):
11+
def test_rewritten_json_is_truncated(self):
12+
with tempfile.TemporaryDirectory() as tmpdir:
13+
tmp_path = Path(tmpdir)
14+
input_file = tmp_path / "input.json"
15+
input_file.write_text(
16+
json.dumps(
17+
{
18+
"training": {"systems": []},
19+
"padding": "content that must not survive the rewrite",
20+
}
21+
)
22+
)
23+
24+
with patch(
25+
"dpgen.util.json.dump",
26+
side_effect=lambda _data, file, indent: file.write("{}"),
27+
):
28+
convert_training_data_to_hdf5(
29+
[str(input_file)], str(tmp_path / "data.hdf5")
30+
)
31+
32+
self.assertEqual(input_file.read_text(), "{}")
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)