mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
634 lines
30 KiB
Python
634 lines
30 KiB
Python
"""Offline recovery corpus contracts and transactional publication."""
|
|
|
|
from copy import deepcopy
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from obliteratus import persistence_contracts
|
|
from obliteratus import recovery_dataset as recovery
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def conversation(answer="Four.", prompt="What is two plus two?"):
|
|
return [{"role": "user", "content": prompt},
|
|
{"role": "assistant", "content": answer}]
|
|
|
|
|
|
def source_fixture(tmp_path, *, name="fixture", rows=None, take=None, kind="local"):
|
|
rows = rows if rows is not None else [conversation()]
|
|
payload = b"".join(recovery.json_bytes({"messages": messages}) for messages in rows)
|
|
source_path = tmp_path / f"{name}.jsonl"
|
|
source_path.write_bytes(payload)
|
|
source = {
|
|
"id": name, "kind": kind, "repository": "fixtures/recovery",
|
|
"config": "default", "split": "train", "revision": "a" * 40,
|
|
"path": source_path.name, "sha256": hashlib.sha256(payload).hexdigest(),
|
|
"rows": len(rows), "take": len(rows) if take is None else take,
|
|
"license": {"id": "CC0-1.0", "locator": "https://example.org/terms",
|
|
"use": "Unrestricted use", "redistribution": "Permitted"},
|
|
}
|
|
return source
|
|
|
|
|
|
def write_manifest(tmp_path, sources):
|
|
path = tmp_path / "manifest.json"
|
|
path.write_bytes(recovery.json_bytes({
|
|
"schema_version": recovery.SCHEMA_VERSION, "sources": sources,
|
|
}))
|
|
return path
|
|
|
|
|
|
def built_fixture(tmp_path, **kwargs):
|
|
source = source_fixture(tmp_path, **kwargs)
|
|
manifest = write_manifest(tmp_path, [source])
|
|
output = tmp_path / "corpus"
|
|
recovery.build_recovery_dataset(manifest, output)
|
|
return source, manifest, output
|
|
|
|
|
|
@pytest.mark.parametrize("messages", [
|
|
[], None, [{"role": "assistant", "content": "Answer"}],
|
|
[{"role": "user", "content": "Question"}],
|
|
conversation() + [{"role": "assistant", "content": "Second answer"}],
|
|
[{"role": "tool", "content": "Tool"}] + conversation(),
|
|
[{"role": "system", "content": "Context"},
|
|
{"role": "system", "content": "More context"}] + conversation(),
|
|
[{"role": "user", "content": " "}, {"role": "assistant", "content": "Answer"}],
|
|
[{"role": "user", "content": "Question", "extra": True},
|
|
{"role": "assistant", "content": "Answer"}],
|
|
conversation("bad\x00text"), conversation(123),
|
|
])
|
|
def test_messages_reject_malformed_roles_and_content(messages):
|
|
with pytest.raises(ValueError):
|
|
recovery.validate_messages(messages)
|
|
|
|
|
|
def test_messages_accept_optional_system_and_multiple_turns():
|
|
recovery.validate_messages(
|
|
[{"role": "system", "content": "Be helpful."}]
|
|
+ conversation() + conversation("Nine.", "Three squared?"),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(("field", "value"), [
|
|
("revision", "main"), ("revision", "abc123"), ("sha256", "bad"),
|
|
("repository", "not-a-repository"), ("config", ""), ("split", "../train"),
|
|
("kind", "url"), ("path", "../outside.jsonl"), ("path", "/absolute.jsonl"),
|
|
("path", "folder//rows.jsonl"), ("path", "folder\\rows.jsonl"),
|
|
("rows", True), ("rows", 0), ("take", 0),
|
|
("license", {"id": "MIT"}),
|
|
])
|
|
def test_manifest_rejects_unpinned_or_invalid_sources(tmp_path, field, value):
|
|
source = source_fixture(tmp_path)
|
|
source[field] = value
|
|
with pytest.raises(ValueError):
|
|
recovery.validate_manifest({"schema_version": recovery.SCHEMA_VERSION,
|
|
"sources": [source]})
|
|
|
|
|
|
@pytest.mark.parametrize(("field", "value"), [
|
|
("id", ""), ("use", ""), ("redistribution", " "),
|
|
("locator", "http://example.org/license"),
|
|
("locator", "https://user:secret@example.org/license"),
|
|
("locator", "https://example.org/license?token=secret"),
|
|
])
|
|
def test_license_terms_are_required(tmp_path, field, value):
|
|
source = source_fixture(tmp_path)
|
|
source["license"][field] = value
|
|
with pytest.raises(ValueError):
|
|
recovery.validate_source(source)
|
|
|
|
|
|
def test_manifest_rejects_version_duplicates_and_extra_fields(tmp_path):
|
|
source = source_fixture(tmp_path)
|
|
for manifest in (
|
|
{"schema_version": "99", "sources": [source]},
|
|
{"schema_version": recovery.SCHEMA_VERSION, "sources": []},
|
|
{"schema_version": recovery.SCHEMA_VERSION, "sources": [source, source]},
|
|
{"schema_version": recovery.SCHEMA_VERSION, "sources": [source], "extra": 1},
|
|
):
|
|
with pytest.raises(ValueError):
|
|
recovery.validate_manifest(manifest)
|
|
|
|
|
|
@pytest.mark.parametrize("payload", [b'{"x":1,"x":2}', b'{"x":NaN}', b'{"x":Infinity}'])
|
|
def test_json_rejects_ambiguous_or_nonfinite_input(payload):
|
|
with pytest.raises(ValueError):
|
|
recovery.read_json(payload)
|
|
|
|
|
|
def test_bundle_records_full_pins_terms_and_provenance(tmp_path):
|
|
source, manifest_path, output = built_fixture(tmp_path)
|
|
records, metadata = recovery.load_recovery_dataset(output)
|
|
payload = (output / "data.jsonl").read_bytes()
|
|
assert metadata["data_sha256"] == hashlib.sha256(payload).hexdigest()
|
|
assert metadata["manifest"] == json.loads(manifest_path.read_bytes())
|
|
assert metadata["manifest_sha256"] == recovery.digest(recovery.json_bytes(metadata["manifest"]))
|
|
assert metadata["count"] == 1
|
|
assert metadata["mode"] == "complete" and metadata["complete"] is True
|
|
assert metadata["sources"] == [{"id": source["id"], "selected": 1,
|
|
"filtered": 0, "status": "complete"}]
|
|
record = records[0]
|
|
assert record["schema_version"] == recovery.SCHEMA_VERSION
|
|
assert record["license"] == source["license"]
|
|
assert record["provenance"] == {
|
|
"locator": f"local://datasets/fixtures/recovery@{'a' * 40}/fixture.jsonl#row=0",
|
|
"row": 0, "source_sha256": source["sha256"],
|
|
"message_sha256": recovery.digest(recovery.json_bytes(record["messages"])),
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("allow_network", [False, True])
|
|
def test_hub_download_uses_immutable_revision_and_explicit_network_opt_in(
|
|
tmp_path, monkeypatch, allow_network,
|
|
):
|
|
source = source_fixture(tmp_path, kind="hub")
|
|
download = Mock(return_value=str(tmp_path / source["path"]))
|
|
monkeypatch.setattr("huggingface_hub.hf_hub_download", download)
|
|
output = tmp_path / "corpus"
|
|
recovery.build_recovery_dataset(write_manifest(tmp_path, [source]), output,
|
|
allow_network=allow_network)
|
|
download.assert_called_once_with(
|
|
repo_id=source["repository"], repo_type="dataset", filename=source["path"],
|
|
revision=source["revision"], local_files_only=not allow_network,
|
|
)
|
|
assert recovery.load_recovery_dataset(output)[0][0]["provenance"]["locator"].startswith("hf://")
|
|
|
|
|
|
def test_unavailable_hub_is_fail_closed_without_download(tmp_path, monkeypatch):
|
|
source = source_fixture(tmp_path, kind="hub")
|
|
download = Mock(side_effect=OSError("Not in local cache"))
|
|
monkeypatch.setattr("huggingface_hub.hf_hub_download", download)
|
|
output = tmp_path / "corpus"
|
|
with pytest.raises(ValueError, match="source fixture failed"):
|
|
recovery.build_recovery_dataset(write_manifest(tmp_path, [source]), output)
|
|
assert download.call_args.kwargs["local_files_only"] is True
|
|
assert not output.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("error_name", ["ConnectError", "ReadTimeout", "ProxyError", "RemoteProtocolError"])
|
|
def test_hub_transport_failure_requires_explicit_partial(tmp_path, monkeypatch, error_name):
|
|
import httpx
|
|
|
|
good = source_fixture(tmp_path, name="good")
|
|
remote = source_fixture(tmp_path, name="remote", kind="hub")
|
|
download = Mock(side_effect=getattr(httpx, error_name)("transport interrupted"))
|
|
monkeypatch.setattr("huggingface_hub.hf_hub_download", download)
|
|
manifest = write_manifest(tmp_path, [good, remote])
|
|
output = tmp_path / "corpus"
|
|
with pytest.raises(ValueError, match="source remote failed"):
|
|
recovery.build_recovery_dataset(manifest, output)
|
|
assert not output.exists()
|
|
result = recovery.build_recovery_dataset(manifest, output, partial=True)
|
|
assert result["mode"] == "partial" and result["complete"] is False
|
|
assert result["sources"][1] == {"id": "remote", "selected": 0, "filtered": 0,
|
|
"status": "failed", "error_type": "OSError"}
|
|
|
|
|
|
@pytest.fixture
|
|
def legacy_hub(monkeypatch):
|
|
from requests.exceptions import HTTPError
|
|
|
|
class HfHubHTTPError(HTTPError):
|
|
pass
|
|
|
|
download = Mock()
|
|
monkeypatch.setitem(sys.modules, "huggingface_hub", SimpleNamespace(hf_hub_download=download))
|
|
monkeypatch.setitem(sys.modules, "huggingface_hub.utils", SimpleNamespace(HfHubHTTPError=HfHubHTTPError))
|
|
monkeypatch.setitem(sys.modules, "huggingface_hub.errors", None)
|
|
monkeypatch.setitem(sys.modules, "httpx", None)
|
|
return download, HfHubHTTPError
|
|
|
|
|
|
@pytest.mark.parametrize("network", [False, True])
|
|
def test_legacy_hub_sources_work_without_httpx(tmp_path, legacy_hub, network):
|
|
download, _ = legacy_hub
|
|
source = source_fixture(tmp_path, kind="hub")
|
|
download.return_value = str(tmp_path / source["path"])
|
|
output = tmp_path / "corpus"
|
|
result = recovery.build_recovery_dataset(write_manifest(tmp_path, [source]), output,
|
|
allow_network=network)
|
|
assert result["complete"] is True and result["count"] == 1
|
|
assert len(recovery.load_recovery_dataset(output)[0]) == 1
|
|
download.assert_called_once_with(repo_id=source["repository"], repo_type="dataset",
|
|
filename=source["path"], revision=source["revision"],
|
|
local_files_only=not network)
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["http", "connection", "timeout", "missing"])
|
|
def test_legacy_hub_failures_require_explicit_partial_without_httpx(tmp_path, legacy_hub, failure):
|
|
from requests.exceptions import ConnectionError, Timeout
|
|
|
|
download, http_error = legacy_hub
|
|
errors = {"http": http_error, "connection": ConnectionError,
|
|
"timeout": Timeout, "missing": FileNotFoundError}
|
|
download.side_effect = errors[failure]("source unavailable")
|
|
good = source_fixture(tmp_path, name="good")
|
|
remote = source_fixture(tmp_path, name="remote", kind="hub")
|
|
manifest = write_manifest(tmp_path, [good, remote])
|
|
output = tmp_path / "corpus"
|
|
with pytest.raises(ValueError, match="source remote failed"):
|
|
recovery.build_recovery_dataset(manifest, output)
|
|
assert not output.exists()
|
|
result = recovery.build_recovery_dataset(manifest, output, partial=True)
|
|
assert result["complete"] is False and result["mode"] == "partial"
|
|
assert result["sources"][1]["status"] == "failed"
|
|
assert result["sources"][1]["selected"] == 0
|
|
records, _ = recovery.load_recovery_dataset(output)
|
|
assert [record["source"] for record in records] == ["good"]
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["hash", "truncated", "rows", "malformed", "missing"])
|
|
def test_source_failure_does_not_publish_any_prefix(tmp_path, failure):
|
|
source = source_fixture(tmp_path, rows=[conversation(), conversation("Nine.")], take=1)
|
|
path = tmp_path / source["path"]
|
|
if failure == "hash":
|
|
path.write_bytes(path.read_bytes().replace(b"Four.", b"Five."))
|
|
elif failure == "truncated":
|
|
path.write_bytes(path.read_bytes()[:-5])
|
|
elif failure == "rows":
|
|
source["rows"] += 1
|
|
elif failure == "malformed":
|
|
# The malformed trailing row is outside the requested selection size.
|
|
payload = path.read_bytes().splitlines()[0] + b'\n{"messages":[]}\n'
|
|
path.write_bytes(payload)
|
|
source["sha256"] = hashlib.sha256(payload).hexdigest()
|
|
else:
|
|
path.unlink()
|
|
output = tmp_path / "corpus"
|
|
with pytest.raises(ValueError, match="source fixture failed"):
|
|
recovery.build_recovery_dataset(write_manifest(tmp_path, [source]), output)
|
|
assert not output.exists()
|
|
|
|
|
|
def test_refusal_underfill_requires_explicit_partial_and_is_labelled(tmp_path):
|
|
source = source_fixture(tmp_path, rows=[conversation(), conversation("I cannot help with that.")])
|
|
manifest = write_manifest(tmp_path, [source])
|
|
output = tmp_path / "corpus"
|
|
with pytest.raises(ValueError, match="underfilled"):
|
|
recovery.build_recovery_dataset(manifest, output)
|
|
metadata = recovery.build_recovery_dataset(manifest, output, partial=True)
|
|
assert metadata["mode"] == "partial" and metadata["complete"] is False
|
|
assert metadata["sources"] == [{"id": "fixture", "selected": 1,
|
|
"filtered": 1, "status": "underfilled"}]
|
|
with pytest.raises(ValueError, match="partial corpus"):
|
|
recovery.load_recovery_dataset(output, allow_partial=False)
|
|
assert len(recovery.load_recovery_dataset(output, allow_partial=True)[0]) == 1
|
|
|
|
|
|
def test_request_larger_than_source_is_an_explicit_partial_underfill(tmp_path):
|
|
source = source_fixture(tmp_path, take=2)
|
|
manifest = write_manifest(tmp_path, [source])
|
|
output = tmp_path / "corpus"
|
|
with pytest.raises(ValueError, match="underfilled"):
|
|
recovery.build_recovery_dataset(manifest, output)
|
|
result = recovery.build_recovery_dataset(manifest, output, partial=True)
|
|
assert result["sources"][0]["status"] == "underfilled"
|
|
assert result["count"] == 1 and result["complete"] is False
|
|
|
|
|
|
def test_partial_mode_reports_failed_sources_and_never_uses_invalid_prefix(tmp_path):
|
|
good = source_fixture(tmp_path, name="good")
|
|
broken = source_fixture(tmp_path, name="broken", rows=[conversation(), conversation("Nine.")])
|
|
(tmp_path / broken["path"]).write_bytes(b'{"messages":[]}\n')
|
|
output = tmp_path / "corpus"
|
|
metadata = recovery.build_recovery_dataset(write_manifest(tmp_path, [good, broken]), output,
|
|
partial=True)
|
|
assert metadata["sources"][1] == {"id": "broken", "selected": 0, "filtered": 0,
|
|
"status": "failed", "error_type": "ValueError"}
|
|
records, loaded = recovery.load_recovery_dataset(output, allow_partial=True)
|
|
assert [record["source"] for record in records] == ["good"]
|
|
assert loaded["complete"] is False
|
|
|
|
|
|
@pytest.mark.parametrize("missing", [False, True])
|
|
def test_partial_mode_never_publishes_empty_corpus(tmp_path, missing):
|
|
source = source_fixture(tmp_path, rows=[conversation("I cannot help with that.")])
|
|
if missing:
|
|
(tmp_path / source["path"]).unlink()
|
|
output = tmp_path / "corpus"
|
|
with pytest.raises(ValueError, match="empty"):
|
|
recovery.build_recovery_dataset(write_manifest(tmp_path, [source]), output, partial=True)
|
|
assert not output.exists()
|
|
|
|
|
|
def test_refusal_filter_examines_all_assistant_turns_but_not_prompts(tmp_path):
|
|
rows = [conversation("A quotation.", "Explain the phrase 'I cannot help'."),
|
|
conversation("I cannot help with that.") + conversation("Actually, four.")]
|
|
source = source_fixture(tmp_path, rows=rows, take=1)
|
|
output = tmp_path / "corpus"
|
|
metadata = recovery.build_recovery_dataset(write_manifest(tmp_path, [source]), output)
|
|
records, _ = recovery.load_recovery_dataset(output)
|
|
assert records[0]["messages"] == rows[0]
|
|
assert metadata["sources"][0]["filtered"] == 1
|
|
|
|
|
|
def test_corpus_is_byte_reproducible_and_seed_changes_selection(tmp_path):
|
|
source = source_fixture(tmp_path, rows=[conversation(str(i)) for i in range(30)], take=10)
|
|
manifest = write_manifest(tmp_path, [source])
|
|
for name, seed in (("first", 42), ("repeat", 42), ("other", 43)):
|
|
recovery.build_recovery_dataset(manifest, tmp_path / name, seed=seed)
|
|
for filename in ("data.jsonl", "metadata.json"):
|
|
assert (tmp_path / "first" / filename).read_bytes() == (tmp_path / "repeat" / filename).read_bytes()
|
|
first, _ = recovery.load_recovery_dataset(tmp_path / "first")
|
|
other, _ = recovery.load_recovery_dataset(tmp_path / "other")
|
|
assert {r["provenance"]["row"] for r in first} != {r["provenance"]["row"] for r in other}
|
|
|
|
|
|
@pytest.mark.parametrize(("field", "value"), [
|
|
("schema_version", "99"), ("source", "missing"), ("license", {}),
|
|
("messages", [{"role": "tool", "content": "Injected"}]),
|
|
])
|
|
def test_record_validation_rejects_schema_and_source_tampering(tmp_path, field, value):
|
|
source, _, output = built_fixture(tmp_path)
|
|
record = recovery.load_recovery_dataset(output)[0][0]
|
|
record[field] = value
|
|
with pytest.raises(ValueError):
|
|
recovery.validate_record(record, {source["id"]: source})
|
|
|
|
|
|
@pytest.mark.parametrize(("field", "value"), [
|
|
("row", -1), ("row", True), ("row", 1), ("locator", "https://untrusted.example"),
|
|
("source_sha256", "0" * 64), ("message_sha256", "0" * 64),
|
|
])
|
|
def test_record_validation_rejects_provenance_tampering(tmp_path, field, value):
|
|
source, _, output = built_fixture(tmp_path)
|
|
record = recovery.load_recovery_dataset(output)[0][0]
|
|
record["provenance"][field] = value
|
|
with pytest.raises(ValueError):
|
|
recovery.validate_record(record, {source["id"]: source})
|
|
|
|
|
|
@pytest.mark.parametrize("tamper", ["data", "manifest", "filter", "count", "reports", "mode"])
|
|
def test_loading_rejects_corrupt_or_dishonest_bundle(tmp_path, tamper):
|
|
_, _, output = built_fixture(tmp_path)
|
|
metadata = json.loads((output / "metadata.json").read_bytes())
|
|
if tamper == "data":
|
|
(output / "data.jsonl").write_bytes(b"")
|
|
elif tamper == "manifest":
|
|
metadata["manifest"]["sources"][0]["license"]["use"] = "Different terms"
|
|
elif tamper == "filter":
|
|
metadata["filter"]["guarantee"] = True
|
|
elif tamper == "count":
|
|
metadata["count"] += 1
|
|
elif tamper == "reports":
|
|
metadata["sources"][0]["selected"] = 0
|
|
else:
|
|
metadata["mode"] = "unlabelled"
|
|
(output / "metadata.json").write_bytes(recovery.json_bytes(metadata))
|
|
with pytest.raises(ValueError):
|
|
recovery.load_recovery_dataset(output)
|
|
|
|
|
|
def test_duplicate_records_are_rejected_even_with_updated_bundle_hash(tmp_path):
|
|
_, _, output = built_fixture(tmp_path)
|
|
payload = (output / "data.jsonl").read_bytes() * 2
|
|
metadata = json.loads((output / "metadata.json").read_bytes())
|
|
metadata.update(count=2, data_sha256=recovery.digest(payload))
|
|
(output / "data.jsonl").write_bytes(payload)
|
|
(output / "metadata.json").write_bytes(recovery.json_bytes(metadata))
|
|
with pytest.raises(ValueError, match="duplicate provenance"):
|
|
recovery.load_recovery_dataset(output)
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["metadata_write", "validation", "promotion"])
|
|
def test_failed_publication_preserves_previous_dataset_and_metadata(tmp_path, monkeypatch, failure):
|
|
_, manifest, output = built_fixture(tmp_path)
|
|
before = {p.name: p.read_bytes() for p in output.iterdir()}
|
|
if failure == "metadata_write":
|
|
original = Path.write_bytes
|
|
|
|
def fail_write(path, data):
|
|
if path.name == "metadata.json" and ".staging-" in path.parent.name:
|
|
raise OSError("disk full")
|
|
return original(path, data)
|
|
|
|
monkeypatch.setattr(Path, "write_bytes", fail_write)
|
|
elif failure == "validation":
|
|
monkeypatch.setattr(recovery, "load_recovery_dataset", Mock(side_effect=ValueError("invalid stage")))
|
|
else:
|
|
original = persistence_contracts.os.replace
|
|
|
|
def fail_promotion(src, dst):
|
|
if ".staging-" in Path(src).name and Path(dst) == output:
|
|
raise OSError("promotion failed")
|
|
return original(src, dst)
|
|
|
|
monkeypatch.setattr(persistence_contracts.os, "replace", fail_promotion)
|
|
with pytest.raises((OSError, ValueError)):
|
|
recovery.build_recovery_dataset(manifest, output, seed=43)
|
|
assert {p.name: p.read_bytes() for p in output.iterdir()} == before
|
|
assert not list(tmp_path.glob(".corpus.staging-*"))
|
|
assert not list(tmp_path.glob(".corpus.backup-*"))
|
|
|
|
|
|
def test_replacing_corpus_publishes_matching_data_and_metadata(tmp_path):
|
|
_, manifest, output = built_fixture(tmp_path, rows=[conversation(str(i)) for i in range(8)])
|
|
before = (output / "data.jsonl").read_bytes()
|
|
recovery.build_recovery_dataset(manifest, output, seed=1234)
|
|
records, metadata = recovery.load_recovery_dataset(output)
|
|
assert (output / "data.jsonl").read_bytes() != before
|
|
assert metadata["seed"] == 1234 and len(records) == 8
|
|
|
|
|
|
def test_acquisition_failure_preserves_existing_bundle(tmp_path):
|
|
source, manifest, output = built_fixture(tmp_path)
|
|
before = {p.name: p.read_bytes() for p in output.iterdir()}
|
|
(tmp_path / source["path"]).unlink()
|
|
with pytest.raises(ValueError, match="source fixture failed"):
|
|
recovery.build_recovery_dataset(manifest, output)
|
|
assert {p.name: p.read_bytes() for p in output.iterdir()} == before
|
|
|
|
|
|
def test_output_cannot_overlap_manifest_or_source(tmp_path):
|
|
source = source_fixture(tmp_path)
|
|
manifest = write_manifest(tmp_path, [source])
|
|
with pytest.raises(ValueError, match="overlap"):
|
|
recovery.build_recovery_dataset(manifest, tmp_path)
|
|
assert manifest.exists() and (tmp_path / source["path"]).exists()
|
|
|
|
|
|
@pytest.mark.parametrize(("option", "value"), [
|
|
("seed", True), ("seed", -1), ("seed", "42"),
|
|
("partial", 1), ("allow_network", "yes"),
|
|
])
|
|
def test_build_options_reject_ambiguous_types_before_sourcing(tmp_path, monkeypatch, option, value):
|
|
source = source_fixture(tmp_path, kind="hub")
|
|
download = Mock(side_effect=AssertionError("must validate before acquisition"))
|
|
monkeypatch.setattr("huggingface_hub.hf_hub_download", download)
|
|
with pytest.raises(ValueError):
|
|
recovery.build_recovery_dataset(write_manifest(tmp_path, [source]), tmp_path / "corpus",
|
|
**{option: value})
|
|
download.assert_not_called()
|
|
|
|
|
|
def test_packaged_corpus_builds_offline_from_declared_hashes(tmp_path, monkeypatch):
|
|
download = Mock(side_effect=AssertionError("starter corpus must remain offline"))
|
|
monkeypatch.setattr("huggingface_hub.hf_hub_download", download)
|
|
manifest = Path(recovery.__file__).with_name("recovery_data") / "sources.json"
|
|
output = tmp_path / "starter"
|
|
metadata = recovery.build_recovery_dataset(manifest, output)
|
|
records, loaded = recovery.load_recovery_dataset(output)
|
|
assert metadata == loaded
|
|
assert metadata["complete"] is True and len(records) == 6
|
|
assert {record["source"] for record in records} == {"starter-code", "starter-reasoning"}
|
|
for source in metadata["manifest"]["sources"]:
|
|
assert source["sha256"] == recovery.file_digest(manifest.parent / source["path"])
|
|
assert source["revision"] == source["sha256"]
|
|
download.assert_not_called()
|
|
|
|
|
|
def test_filter_fixtures_measure_false_positives_and_false_negatives():
|
|
evaluation = recovery.filter_evaluation()
|
|
assert {key: evaluation[key] for key in ("tp", "fp", "tn", "fn")} == {
|
|
"tp": 3, "fp": 1, "tn": 3, "fn": 1,
|
|
}
|
|
assert evaluation["false_positive_rate"] == 0.25
|
|
assert evaluation["false_negative_rate"] == 0.25
|
|
assert evaluation["guarantee"] is False
|
|
assert evaluation["version"] == recovery.FILTER_VERSION
|
|
assert len(evaluation["fixtures_sha256"]) == 64
|
|
|
|
|
|
def test_filter_fixture_hash_changes_when_labels_change():
|
|
fixtures = [{"text": "I cannot help with that.", "refusal": True},
|
|
{"text": "Four.", "refusal": False}]
|
|
changed = deepcopy(fixtures)
|
|
changed[0]["text"] = "I must decline."
|
|
assert recovery.evaluate_filter(fixtures)["fixtures_sha256"] != recovery.evaluate_filter(changed)["fixtures_sha256"]
|
|
|
|
|
|
@pytest.mark.parametrize("fixtures", [
|
|
[], [{"text": "Four.", "refusal": False}],
|
|
[{"text": "I cannot help.", "refusal": True}],
|
|
[{"text": "Four.", "refusal": 0}],
|
|
])
|
|
def test_filter_evaluation_requires_binary_labels_and_both_classes(fixtures):
|
|
with pytest.raises(ValueError):
|
|
recovery.evaluate_filter(fixtures)
|
|
|
|
|
|
def test_shipped_json_schema_validates_manifest_records_and_metadata(tmp_path):
|
|
from jsonschema import Draft202012Validator
|
|
|
|
_, manifest, output = built_fixture(tmp_path)
|
|
schema_path = Path(recovery.__file__).with_name("recovery_data") / "schema-v1.json"
|
|
schema = json.loads(schema_path.read_bytes())
|
|
Draft202012Validator.check_schema(schema)
|
|
validator = Draft202012Validator(schema)
|
|
records, metadata = recovery.load_recovery_dataset(output)
|
|
validator.validate(json.loads(manifest.read_bytes()))
|
|
validator.validate(metadata)
|
|
for record in records:
|
|
validator.validate(record)
|
|
malformed = deepcopy(records[0])
|
|
malformed["messages"][0]["role"] = "tool"
|
|
assert list(validator.iter_errors(malformed))
|
|
malformed = deepcopy(metadata)
|
|
malformed["filter"]["guarantee"] = True
|
|
assert list(validator.iter_errors(malformed))
|
|
|
|
|
|
@pytest.mark.parametrize("policy,selected,filtered", [("drop", 1, 1), ("keep", 2, 0), ("require", 1, 1)])
|
|
def test_explicit_refusal_policies_roundtrip(tmp_path, policy, selected, filtered):
|
|
rows = [conversation(), conversation("I cannot help with that.") + conversation("A safe alternative.")]
|
|
source = source_fixture(tmp_path, rows=rows, take=selected)
|
|
manifest = write_manifest(tmp_path, [source])
|
|
output = tmp_path / "corpus"
|
|
result = recovery.build_recovery_dataset(manifest, output, refusal_policy=policy)
|
|
records, loaded = recovery.load_recovery_dataset(output)
|
|
assert len(records) == selected
|
|
assert result == loaded
|
|
assert loaded["refusal_policy"] == loaded["manifest"]["refusal_policy"] == policy
|
|
assert loaded["sources"][0]["filtered"] == filtered
|
|
if policy != "keep":
|
|
assert records[0]["messages"] == rows[policy == "require"]
|
|
|
|
|
|
def test_manifest_policy_and_cli_override_are_recorded(tmp_path):
|
|
source = source_fixture(tmp_path, rows=[conversation("I cannot help with that.")])
|
|
manifest = write_manifest(tmp_path, [source])
|
|
original = recovery.read_json(manifest.read_bytes())
|
|
original["refusal_policy"] = "keep"
|
|
manifest.write_bytes(recovery.json_bytes(original))
|
|
result = recovery.build_recovery_dataset(manifest, tmp_path / "kept")
|
|
assert result["refusal_policy"] == "keep"
|
|
result = recovery.build_recovery_dataset(manifest, tmp_path / "required", refusal_policy="require")
|
|
assert result["manifest"]["refusal_policy"] == "require"
|
|
assert recovery.read_json(manifest.read_bytes()) == original
|
|
with pytest.raises(ValueError, match="empty"):
|
|
recovery.build_recovery_dataset(manifest, tmp_path / "dropped", refusal_policy="drop", partial=True)
|
|
|
|
|
|
@pytest.mark.parametrize("policy", ["unknown", None, True, 1, []])
|
|
def test_manifest_rejects_invalid_refusal_policy(tmp_path, policy):
|
|
manifest = {"schema_version": recovery.SCHEMA_VERSION,
|
|
"sources": [source_fixture(tmp_path)], "refusal_policy": policy}
|
|
with pytest.raises(ValueError, match="refusal_policy"):
|
|
recovery.validate_manifest(manifest)
|
|
|
|
|
|
def test_default_bundle_has_identical_legacy_bytes(tmp_path):
|
|
source = source_fixture(tmp_path)
|
|
manifest_path = write_manifest(tmp_path, [source])
|
|
manifest = recovery.read_json(manifest_path.read_bytes())
|
|
output = tmp_path / "corpus"
|
|
result = recovery.build_recovery_dataset(manifest_path, output)
|
|
expected_record = {"schema_version": recovery.SCHEMA_VERSION, "messages": conversation(),
|
|
"source": source["id"], "license": source["license"], "provenance": {
|
|
"locator": recovery.source_locator(source, 0), "row": 0,
|
|
"source_sha256": source["sha256"],
|
|
"message_sha256": recovery.digest(recovery.json_bytes(conversation()))}}
|
|
payload = recovery.json_bytes(expected_record)
|
|
expected = {"schema_version": recovery.SCHEMA_VERSION, "manifest": manifest,
|
|
"manifest_sha256": recovery.digest(recovery.json_bytes(manifest)), "seed": 42,
|
|
"selection": "sha256-order-v1", "mode": "complete", "complete": True,
|
|
"sources": [{"id": source["id"], "selected": 1, "filtered": 0, "status": "complete"}],
|
|
"count": 1, "data_sha256": recovery.digest(payload), "filter": recovery.filter_evaluation()}
|
|
assert result == expected
|
|
assert (output / "data.jsonl").read_bytes() == payload
|
|
assert (output / "metadata.json").read_bytes() == recovery.json_bytes(expected)
|
|
|
|
|
|
@pytest.mark.parametrize("policy", [None, "drop"])
|
|
@pytest.mark.parametrize("allow_partial", [False, True])
|
|
def test_loader_enforces_default_drop_on_hash_consistent_legacy_bundle(tmp_path, policy, allow_partial):
|
|
source = source_fixture(tmp_path, rows=[conversation("I cannot help with that.")])
|
|
output = tmp_path / "corpus"
|
|
metadata = recovery.build_recovery_dataset(write_manifest(tmp_path, [source]), output,
|
|
refusal_policy="keep")
|
|
if policy is None:
|
|
metadata.pop("refusal_policy")
|
|
metadata["manifest"].pop("refusal_policy")
|
|
else:
|
|
metadata["refusal_policy"] = metadata["manifest"]["refusal_policy"] = policy
|
|
metadata["manifest_sha256"] = recovery.digest(recovery.json_bytes(metadata["manifest"]))
|
|
(output / "metadata.json").write_bytes(recovery.json_bytes(metadata))
|
|
with pytest.raises(ValueError, match="record violates refusal policy"):
|
|
recovery.load_recovery_dataset(output, allow_partial=allow_partial)
|
|
|
|
|
|
@pytest.mark.parametrize("tamper", ["policy", "presence", "records"])
|
|
def test_loader_rejects_policy_provenance_or_record_mismatch(tmp_path, tamper):
|
|
source = source_fixture(tmp_path)
|
|
output = tmp_path / "corpus"
|
|
manifest = write_manifest(tmp_path, [source])
|
|
result = recovery.build_recovery_dataset(manifest, output, refusal_policy="keep")
|
|
if tamper == "policy":
|
|
result["refusal_policy"] = "require"
|
|
elif tamper == "presence":
|
|
result.pop("refusal_policy")
|
|
else:
|
|
result["refusal_policy"] = result["manifest"]["refusal_policy"] = "require"
|
|
result["manifest_sha256"] = recovery.digest(recovery.json_bytes(result["manifest"]))
|
|
(output / "metadata.json").write_bytes(recovery.json_bytes(result))
|
|
with pytest.raises(ValueError, match="policy"):
|
|
recovery.load_recovery_dataset(output)
|