From 618c1c413eb21e3700bc7dd1e37b7544da268b23 Mon Sep 17 00:00:00 2001 From: Varshini Puttabakula Date: Wed, 25 Feb 2026 12:47:18 +0530 Subject: [PATCH 1/3] feat: add canonical deterministic directory-level hashing with full test coverage --- openverifiablellm/utils.py | 68 ++++++++++++++++++++++++-- tests/test_directory_hash.py | 93 ++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 tests/test_directory_hash.py diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 084e1e25..6cd3ed9b 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -39,7 +39,7 @@ def extract_text_from_xml(input_path): project_root = Path.cwd() output_dir = project_root / "data" / "processed" output_dir.mkdir(parents=True, exist_ok=True) - + output_path = output_dir / "wiki_clean.txt" with bz2.open(input_path, "rb") as f: @@ -58,7 +58,7 @@ def extract_text_from_xml(input_path): elem.clear() logger.info("Preprocessing complete. Output saved to %s", output_path) generate_manifest(input_path,output_path) - + # generate data manifest def generate_manifest(raw_path, processed_path): raw_path = Path(raw_path) @@ -114,6 +114,68 @@ def compute_sha256(file_path: Union[str, Path]) -> str: return sha256.hexdigest() +def compute_directory_hash(directory_path: Union[str, Path]) -> str: + """ + Compute a deterministic SHA256 hash of a directory. + + The hash incorporates: + - Relative file paths (normalized to POSIX style) + - File contents (via compute_sha256) + + Hidden files and files inside hidden directories (starting with '.') + are ignored. + + Parameters + ---------- + directory_path : Union[str, Path] + Path to directory to hash. + + Returns + ------- + str + SHA256 hex digest representing directory contents. + """ + directory = Path(directory_path) + + if not directory.exists() or not directory.is_dir(): + raise FileNotFoundError(f"Directory not found: {directory}") + + sha256 = hashlib.sha256() + + files = [] + + for f in directory.rglob("*"): + if not f.is_file(): + continue + + relative = f.relative_to(directory) + + # Ignore hidden files and any file inside hidden directories + if any(part.startswith(".") for part in relative.parts): + continue + + files.append(f) + + # Sort deterministically by normalized relative path + files_sorted = sorted( + files, + key=lambda f: f.relative_to(directory).as_posix() + ) + + for file in files_sorted: + relative_path = file.relative_to(directory).as_posix() + + # Include relative path in hash + sha256.update(relative_path.encode("utf-8")) + sha256.update(b"\0") # delimiter + + # Include file content hash + file_hash = compute_sha256(file) + sha256.update(file_hash.encode("utf-8")) + sha256.update(b"\0") # delimiter + + return sha256.hexdigest() + def extract_dump_date(filename: str): parts = filename.split("-") for part in parts: @@ -148,7 +210,7 @@ def clean_wikitext(text: str) -> str: if len(sys.argv) < 2: print("Usage: python -m openverifiablellm.utils ") sys.exit(1) - + logging.basicConfig( level=logging.INFO, format="%(levelname)s - %(message)s" diff --git a/tests/test_directory_hash.py b/tests/test_directory_hash.py new file mode 100644 index 00000000..abd9c59e --- /dev/null +++ b/tests/test_directory_hash.py @@ -0,0 +1,93 @@ +import pytest +from pathlib import Path +from openverifiablellm.utils import compute_directory_hash +import hashlib + +def test_directory_hash_is_deterministic(tmp_path): + # Create files + file1 = tmp_path / "a.txt" + file2 = tmp_path / "b.txt" + + file1.write_text("hello", encoding="utf-8") + file2.write_text("world", encoding="utf-8") + + hash1 = compute_directory_hash(tmp_path) + hash2 = compute_directory_hash(tmp_path) + + assert hash1 == hash2 + + +def test_directory_hash_changes_on_content_modification(tmp_path): + file1 = tmp_path / "a.txt" + file1.write_text("hello", encoding="utf-8") + + original_hash = compute_directory_hash(tmp_path) + + file1.write_text("modified", encoding="utf-8") + + new_hash = compute_directory_hash(tmp_path) + + assert original_hash != new_hash + +def test_directory_hash_changes_on_file_addition(tmp_path): + (tmp_path / "a.txt").write_text("hello", encoding="utf-8") + + original_hash = compute_directory_hash(tmp_path) + + (tmp_path / "b.txt").write_text("world", encoding="utf-8") + + new_hash = compute_directory_hash(tmp_path) + + assert original_hash != new_hash + +def test_directory_hash_changes_on_rename(tmp_path): + file_path = tmp_path / "a.txt" + file_path.write_text("content", encoding="utf-8") + + original_hash = compute_directory_hash(tmp_path) + + renamed_path = tmp_path / "renamed.txt" + file_path.rename(renamed_path) + + new_hash = compute_directory_hash(tmp_path) + + assert original_hash != new_hash + +def test_hidden_file_is_ignored(tmp_path): + (tmp_path / "a.txt").write_text("hello", encoding="utf-8") + + original_hash = compute_directory_hash(tmp_path) + + # Add hidden file + (tmp_path / ".hidden.txt").write_text("secret", encoding="utf-8") + + new_hash = compute_directory_hash(tmp_path) + + assert original_hash == new_hash + + +def test_hidden_directory_is_ignored(tmp_path): + # Visible file + (tmp_path / "a.txt").write_text("visible", encoding="utf-8") + + original_hash = compute_directory_hash(tmp_path) + + # Hidden directory with file inside + hidden_dir = tmp_path / ".hidden_dir" + hidden_dir.mkdir() + (hidden_dir / "secret.txt").write_text("secret", encoding="utf-8") + + new_hash = compute_directory_hash(tmp_path) + + assert original_hash == new_hash + +def test_empty_directory_hash(tmp_path): + # No files inside + hash_value = compute_directory_hash(tmp_path) + + # SHA256 of empty input + assert hash_value == hashlib.sha256().hexdigest() + +def test_nonexistent_directory_raises(): + with pytest.raises(FileNotFoundError): + compute_directory_hash("non_existent_dir") From d147b35eaab1b17888dd7bb541ae41134cf68042 Mon Sep 17 00:00:00 2001 From: Varshini Puttabakula Date: Wed, 25 Feb 2026 13:58:25 +0530 Subject: [PATCH 2/3] refactor: improve error handling, optimize path caching, add nested directory tests --- openverifiablellm/utils.py | 22 ++++++++++------------ tests/test_directory_hash.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 6cd3ed9b..426efb25 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -137,12 +137,15 @@ def compute_directory_hash(directory_path: Union[str, Path]) -> str: """ directory = Path(directory_path) - if not directory.exists() or not directory.is_dir(): - raise FileNotFoundError(f"Directory not found: {directory}") + if not directory.exists(): + raise FileNotFoundError(f"Path not found: {directory}") + + if not directory.is_dir(): + raise NotADirectoryError(f"Path is not a directory: {directory}") sha256 = hashlib.sha256() - files = [] + entries = [] for f in directory.rglob("*"): if not f.is_file(): @@ -150,20 +153,15 @@ def compute_directory_hash(directory_path: Union[str, Path]) -> str: relative = f.relative_to(directory) - # Ignore hidden files and any file inside hidden directories if any(part.startswith(".") for part in relative.parts): continue - files.append(f) + rel_posix = relative.as_posix() + entries.append((rel_posix, f)) - # Sort deterministically by normalized relative path - files_sorted = sorted( - files, - key=lambda f: f.relative_to(directory).as_posix() - ) + entries_sorted = sorted(entries, key=lambda e: e[0]) - for file in files_sorted: - relative_path = file.relative_to(directory).as_posix() + for relative_path, file in entries_sorted: # Include relative path in hash sha256.update(relative_path.encode("utf-8")) diff --git a/tests/test_directory_hash.py b/tests/test_directory_hash.py index abd9c59e..f26567da 100644 --- a/tests/test_directory_hash.py +++ b/tests/test_directory_hash.py @@ -88,6 +88,30 @@ def test_empty_directory_hash(tmp_path): # SHA256 of empty input assert hash_value == hashlib.sha256().hexdigest() -def test_nonexistent_directory_raises(): +def test_nonexistent_directory_raises(tmp_path): + missing_path = tmp_path / "non_existent_dir" + with pytest.raises(FileNotFoundError): - compute_directory_hash("non_existent_dir") + compute_directory_hash(missing_path) + + +def test_nested_directories_and_move(tmp_path): + subdir1 = tmp_path / "subdir1" + subdir2 = tmp_path / "subdir2" + subdir1.mkdir() + subdir2.mkdir() + + file1 = subdir1 / "a.txt" + file2 = subdir2 / "b.txt" + + file1.write_text("content1", encoding="utf-8") + file2.write_text("content2", encoding="utf-8") + + original_hash = compute_directory_hash(tmp_path) + + # Move file1 into subdir2 + file1.rename(subdir2 / "a.txt") + + new_hash = compute_directory_hash(tmp_path) + + assert original_hash != new_hash From 5a3fd23745e24d68cd37c60a3325e56252b5f9fc Mon Sep 17 00:00:00 2001 From: Varshini Puttabakula Date: Wed, 25 Feb 2026 22:51:38 +0530 Subject: [PATCH 3/3] test: add NotADirectoryError branch coverage and simplify exception raises --- openverifiablellm/utils.py | 4 ++-- tests/test_directory_hash.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 426efb25..5eae1e58 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -138,10 +138,10 @@ def compute_directory_hash(directory_path: Union[str, Path]) -> str: directory = Path(directory_path) if not directory.exists(): - raise FileNotFoundError(f"Path not found: {directory}") + raise FileNotFoundError(directory) if not directory.is_dir(): - raise NotADirectoryError(f"Path is not a directory: {directory}") + raise NotADirectoryError(directory) sha256 = hashlib.sha256() diff --git a/tests/test_directory_hash.py b/tests/test_directory_hash.py index f26567da..45f63534 100644 --- a/tests/test_directory_hash.py +++ b/tests/test_directory_hash.py @@ -115,3 +115,11 @@ def test_nested_directories_and_move(tmp_path): new_hash = compute_directory_hash(tmp_path) assert original_hash != new_hash + + +def test_not_a_directory_raises(tmp_path): + file_path = tmp_path / "a_file.txt" + file_path.write_text("content", encoding="utf-8") + + with pytest.raises(NotADirectoryError): + compute_directory_hash(file_path)