-
Notifications
You must be signed in to change notification settings - Fork 0
feat: hash-guarded dataset versioning + release scaffolding #minor #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BradenBug
wants to merge
4
commits into
main
Choose a base branch
from
bw/dataset-versioning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6f38bdc
feat: hash-guarded dataset versioning and release scaffolding
BradenBug 102c1c3
fix: fallback version for scaffolded projects without git metadata
BradenBug fc04b77
fix: clear error when dataset_versions.yaml is empty or not a mapping
BradenBug b05e033
docs: document release setup (GH_PAT, PR-title bump) in scaffolded RE…
BradenBug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Hash-guarded dataset version tracking. | ||
|
|
||
| A dataset_versions.yaml next to the dataset files maps each dataset name to a | ||
| human-assigned semver and a sha256 of its content file. Version semantics: | ||
| major = scores not comparable, minor = additive, patch = non-scoring fixes. | ||
| """ | ||
|
|
||
| import hashlib | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import yaml | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class DatasetVersionError(Exception): | ||
| """Dataset content does not match its declared version entry.""" | ||
|
|
||
|
|
||
| class DatasetVersionEntry(BaseModel): | ||
| file: str | ||
| version: str | ||
| sha256: str | ||
|
|
||
|
|
||
| def compute_checksum(path: Path) -> str: | ||
| return hashlib.sha256(path.read_bytes()).hexdigest() | ||
|
|
||
|
|
||
| def load_dataset_versions(versions_file: Path) -> dict[str, DatasetVersionEntry]: | ||
| data = yaml.safe_load(versions_file.read_text()) | ||
| return {name: DatasetVersionEntry.model_validate(entry) for name, entry in data.items()} | ||
|
BradenBug marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def load_verified_dataset_versions(versions_file: Path) -> dict[str, DatasetVersionEntry]: | ||
| """Load entries and verify every dataset file matches its declared checksum. | ||
|
|
||
| Raises DatasetVersionError on any mismatch: content that does not match its | ||
| declared version must never be served. | ||
| """ | ||
| entries = load_dataset_versions(versions_file) | ||
| data_dir = versions_file.parent | ||
| mismatches: list[str] = [] | ||
| for name, entry in entries.items(): | ||
| actual = compute_checksum(data_dir / entry.file) | ||
| if actual != entry.sha256: | ||
| mismatches.append(f"{name} ({entry.file}): declared {entry.sha256}, actual {actual}") | ||
| if mismatches: | ||
| raise DatasetVersionError( | ||
| "dataset content does not match dataset_versions.yaml — bump the version, then run " | ||
| "`python -m benchmark_service.dataset_versioning update <file>`:\n " + "\n ".join(mismatches) | ||
| ) | ||
| return entries | ||
|
|
||
|
|
||
| def main(argv: list[str]) -> int: | ||
| if len(argv) != 2 or argv[0] not in ("check", "update"): | ||
| print("usage: python -m benchmark_service.dataset_versioning {check|update} <dataset_versions.yaml>") | ||
| return 2 | ||
| command, versions_file = argv[0], Path(argv[1]) | ||
| if command == "check": | ||
| try: | ||
| load_verified_dataset_versions(versions_file) | ||
| except DatasetVersionError as exc: | ||
| print(exc) | ||
| return 1 | ||
| print("dataset checksums OK") | ||
| return 0 | ||
| raw = yaml.safe_load(versions_file.read_text()) | ||
| for entry in raw.values(): | ||
| entry["sha256"] = compute_checksum(versions_file.parent / entry["file"]) | ||
| versions_file.write_text(yaml.safe_dump(raw, sort_keys=False)) | ||
| print(f"updated {versions_file}") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main(sys.argv[1:])) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Tests for hash-guarded dataset version tracking.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| import yaml | ||
|
|
||
| from benchmark_service.dataset_versioning import ( | ||
| DatasetVersionError, | ||
| compute_checksum, | ||
| load_verified_dataset_versions, | ||
| main, | ||
| ) | ||
|
|
||
| from tests.conftest import StubBenchmark | ||
|
|
||
|
|
||
| def _write_fixture(tmp_path: Path, content: bytes = b'{"tests": []}') -> Path: | ||
| data_file = tmp_path / "validation.json" | ||
| data_file.write_bytes(content) | ||
| versions_file = tmp_path / "dataset_versions.yaml" | ||
| versions_file.write_text( | ||
| yaml.safe_dump( | ||
| { | ||
| "validation": { | ||
| "file": "validation.json", | ||
| "version": "1.0.0", | ||
| "sha256": compute_checksum(data_file), | ||
| } | ||
| } | ||
| ) | ||
| ) | ||
| return versions_file | ||
|
|
||
|
|
||
| def test_load_verified_returns_entries_when_content_matches(tmp_path: Path) -> None: | ||
| versions_file = _write_fixture(tmp_path) | ||
| entries = load_verified_dataset_versions(versions_file) | ||
| assert entries["validation"].version == "1.0.0" | ||
|
|
||
|
|
||
| def test_load_verified_raises_on_content_mismatch(tmp_path: Path) -> None: | ||
| versions_file = _write_fixture(tmp_path) | ||
| (tmp_path / "validation.json").write_bytes(b'{"tests": [1]}') | ||
| with pytest.raises(DatasetVersionError, match="validation"): | ||
| load_verified_dataset_versions(versions_file) | ||
|
|
||
|
|
||
| def test_check_command_fails_on_mismatch_and_update_repairs(tmp_path: Path) -> None: | ||
| versions_file = _write_fixture(tmp_path) | ||
| (tmp_path / "validation.json").write_bytes(b'{"tests": [1]}') | ||
| assert main(["check", str(versions_file)]) == 1 | ||
| assert main(["update", str(versions_file)]) == 0 | ||
| assert main(["check", str(versions_file)]) == 0 | ||
|
|
||
|
|
||
| async def test_service_startup_verifies_and_serves_dataset_versions(tmp_path: Path) -> None: | ||
| versions_file = _write_fixture(tmp_path) | ||
|
|
||
| class VersionedBenchmark(StubBenchmark): | ||
| dataset_versions_file = versions_file | ||
|
|
||
| service = await VersionedBenchmark.create() | ||
| assert service.get_dataset_version("validation") == "1.0.0" | ||
| assert service.get_dataset_version("unknown") is None | ||
|
|
||
|
|
||
| async def test_service_startup_fails_on_checksum_mismatch(tmp_path: Path) -> None: | ||
| versions_file = _write_fixture(tmp_path) | ||
| (tmp_path / "validation.json").write_bytes(b"tampered") | ||
|
|
||
| class VersionedBenchmark(StubBenchmark): | ||
| dataset_versions_file = versions_file | ||
|
|
||
| with pytest.raises(DatasetVersionError): | ||
| await VersionedBenchmark.create() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.