Datasets:
Download code/tests/test_v2_cli.py from voidful/barbet-sft: direct link, hf CLI and curl.
- Browser
- Download file 6.72 kB
-
https://huggingface.co/datasets/voidful/barbet-sft/resolve/main/code/tests/test_v2_cli.py
- Command line
-
hf download hf://datasets/voidful/barbet-sft/code/tests/test_v2_cli.py
-
curl -L -o test_v2_cli.py https://huggingface.co/datasets/voidful/barbet-sft/resolve/main/code/tests/test_v2_cli.py
6.72 kB
| from __future__ import annotations | |
| import json | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| from unittest.mock import patch | |
| from barbet_bgd.io import iter_jsonl_gz | |
| from barbet_bgd.quality_v2 import validate_record_quality_v2 | |
| from barbet_bgd.v2_cli import run_build_v2 | |
| ROOT = Path(__file__).resolve().parents[1] | |
| CONFIG_PATH = ROOT / "configs" / "selected_sft_1m_v2.json" | |
| class V2BuildPipelineTests(unittest.TestCase): | |
| def _small_shard_config(self, root: Path, shard_size: int = 10) -> Path: | |
| config = json.loads(CONFIG_PATH.read_text(encoding="utf-8")) | |
| config["shard_size"] = shard_size | |
| path = root / "selected_sft_1m_v2.test.json" | |
| path.write_text( | |
| json.dumps(config, ensure_ascii=False, indent=2) + "\n", | |
| encoding="utf-8", | |
| ) | |
| return path | |
| def test_limit_build_writes_aligned_canonical_and_keep_sidecars(self) -> None: | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| config_path = self._small_shard_config(root) | |
| output = root / "v2-smoke" | |
| result = run_build_v2( | |
| config_path=config_path, | |
| output_root=output, | |
| workers=1, | |
| limit=25, | |
| project_root=ROOT, | |
| ) | |
| self.assertTrue(result["quality_artifacts_verified"]) | |
| self.assertTrue(result["partial_run"]) | |
| self.assertFalse(result["training_promotion_passed"]) | |
| self.assertTrue((output / "_VERIFIED.json").is_file()) | |
| self.assertFalse((output / "_PROMOTED.json").exists()) | |
| self.assertFalse((output / "_SUCCESS.json").exists()) | |
| manifest = json.loads((output / "manifest.json").read_text(encoding="utf-8")) | |
| self.assertEqual(manifest["totals"]["records"], 25) | |
| self.assertEqual(manifest["totals"]["shards"], 3) | |
| self.assertEqual( | |
| [row["records"] for row in manifest["shards"]], | |
| [10, 10, 5], | |
| ) | |
| self.assertIn("config", manifest["metadata"]) | |
| self.assertEqual(len(manifest["metadata"]["generation_plan"]["sha256"]), 64) | |
| self.assertEqual(len(manifest["metadata"]["source_bundle"]["sha256"]), 64) | |
| logical_paths = { | |
| row["logical_path"] | |
| for row in manifest["metadata"]["source_bundle"]["files"] | |
| } | |
| self.assertIn("src/barbet_bgd/generation_v2.py", logical_paths) | |
| self.assertIn("src/barbet_bgd/quality_v2.py", logical_paths) | |
| self.assertIn("src/barbet_bgd/serializers_v2.py", logical_paths) | |
| self.assertTrue((output / "source_bundle_manifest.json").is_file()) | |
| self.assertTrue((output / "source_bundle_snapshot.tar.gz").is_file()) | |
| self.assertEqual( | |
| len(manifest["metadata"]["source_snapshot"]["sha256"]), | |
| 64, | |
| ) | |
| records = [] | |
| verdicts = [] | |
| for shard in manifest["shards"]: | |
| records.extend(iter_jsonl_gz(shard["canonical_path"])) | |
| verdicts.extend(iter_jsonl_gz(shard["verdict_path"])) | |
| self.assertEqual(len(records), 25) | |
| self.assertEqual(len(verdicts), 25) | |
| for index, (record, verdict) in enumerate(zip(records, verdicts, strict=True)): | |
| self.assertEqual(record["record_id"], f"bgd_tw_v2_{index:07d}") | |
| self.assertEqual(verdict["record_id"], record["record_id"]) | |
| self.assertEqual(verdict["record_index"], index) | |
| self.assertEqual(verdict["disposition"], "KEEP") | |
| self.assertTrue(verdict["training_ready"]) | |
| self.assertTrue(all(verdict["pipeline_checks"].values())) | |
| self.assertEqual(validate_record_quality_v2(record)["disposition"], "KEEP") | |
| postwrite = json.loads( | |
| (output / "reports" / "postwrite-verification.json").read_text( | |
| encoding="utf-8" | |
| ) | |
| ) | |
| self.assertEqual(postwrite["statistics"]["keep_records"], 25) | |
| self.assertEqual(postwrite["statistics"]["reject_records"], 0) | |
| self.assertEqual(postwrite["verification_issue_counts"], {}) | |
| self.assertTrue(postwrite["gates"]["all_records_exactly_regenerated"]) | |
| self.assertTrue(postwrite["gates"]["all_verdicts_independently_recomputed"]) | |
| self.assertTrue(postwrite["gates"]["first_and_second_corpus_statistics_equal"]) | |
| def test_non_keep_record_fails_closed_before_canonical_publication(self) -> None: | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| config_path = self._small_shard_config(root) | |
| output = root / "v2-rejected" | |
| def reject(record): | |
| verdict = validate_record_quality_v2(record) | |
| verdict["hard_failures"] = ["TEST_FORCED_REJECTION"] | |
| verdict["training_ready"] = False | |
| verdict["disposition"] = "REJECT" | |
| return verdict | |
| with patch("barbet_bgd.v2_cli.validate_record_quality_v2", side_effect=reject): | |
| with self.assertRaisesRegex(RuntimeError, "failed closed quality gates"): | |
| run_build_v2( | |
| config_path=config_path, | |
| output_root=output, | |
| workers=1, | |
| limit=1, | |
| project_root=ROOT, | |
| ) | |
| self.assertTrue((output / "_BUILD_FAILED.json").is_file()) | |
| self.assertFalse(any((output / "canonical").glob("*.jsonl.gz"))) | |
| self.assertFalse(any((output / "quality" / "verdicts").glob("*.jsonl.gz"))) | |
| self.assertFalse((output / "_PROMOTED.json").exists()) | |
| self.assertFalse((output / "_SUCCESS.json").exists()) | |
| def test_refuses_to_overwrite_any_existing_output(self) -> None: | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| config_path = self._small_shard_config(root) | |
| output = root / "occupied" | |
| output.mkdir() | |
| (output / "user-owned.txt").write_text("preserve", encoding="utf-8") | |
| with self.assertRaises(FileExistsError): | |
| run_build_v2( | |
| config_path=config_path, | |
| output_root=output, | |
| workers=1, | |
| limit=1, | |
| project_root=ROOT, | |
| ) | |
| self.assertEqual((output / "user-owned.txt").read_text(encoding="utf-8"), "preserve") | |
| if __name__ == "__main__": | |
| unittest.main() | |