Files
OBLITERATUS/tests/test_concept_selectors.py
Joseph MaglyandClaude Opus 5.5 86982b8a5e fix(concepts): harden the pain pack and record GPU baselines
Review follow-ups for the pain pack:

- The no-upstream-sentence guarantee now runs in CI without the pinned
  checkout. A committed fingerprint fixture (SHA-256 only) covers
  pain-specific upstream sentences of at least eight words: positives,
  confounds/neighbours and the referenced batteries, but not generic neutral
  controls. A repository-wide scan checks every tracked text file against it.
  Manifest hashes are checked against the committed hash manifest of the
  pinned commit.
- The pain pack loads lazily, so a broken pain pack warns and is excluded
  while refusal and other packs stay usable.
- Selectors type-check their fields and fail closed with PackError.
- concepts baseline gains --cache-dir.

The GPU-lane baselines (RTX 4090, bf16, seed 232) are recorded under
.aiwg/testing/baselines/pain/ and summarised in the dataset card:

- Qwen2.5-7B-Instruct: CV layer 24, held-out AUC 0.980, cosine 0.9998 to
  the authors' S2 vector at L24;
- Qwen3-8B-Base: CV layer 34, held-out AUC 0.994, cosine 0.9001 at L34.

Both verdicts are warn, because each upstream control set has 20 prompts.
These are representational baselines only, with no behavioural claim.

Closes #232

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 22:42:46 -04:00

429 lines
19 KiB
Python

"""Declarative remote JSON projections and verified caches, entirely offline."""
from copy import deepcopy
import json
from pathlib import Path
import shutil
from jsonschema import Draft202012Validator, ValidationError
import pytest
import yaml
from obliteratus.concepts import selectors
from obliteratus.concepts.pack import ConceptPack, remote_url
from obliteratus.concepts.pack_io import digest
from obliteratus.concepts.validation import PackError, SCHEMA_PATH, validate_manifest
pytestmark = pytest.mark.cpu
FIXTURE = Path(__file__).parent / "fixtures" / "concept_packs" / "valid_minimal"
SELECTOR = {"format": "json", "select": "datasets.primary.sentences", "text_field": "prompt"}
def synthetic_records(count=8, category="A"):
return [{"category": category, "prompt": f"Synthetic observation {index}."}
for index in range(count)]
def synthetic_data(records=None):
return {"datasets": {"primary": {"sentences": (
synthetic_records() if records is None else records
)}}}
@pytest.fixture
def remote_factory(tmp_path):
"""Build first-party data in a local repository and inject a URL-to-file fetcher."""
counter = 0
def create(data=None, *, selector=None, n=8, raw=None, source_path="datasets/data.json"):
nonlocal counter
counter += 1
base = tmp_path / str(counter)
repository = base / "repository"
source = repository / source_path
source.parent.mkdir(parents=True)
payload = raw if raw is not None else json.dumps(
synthetic_data() if data is None else data, ensure_ascii=False,
).encode()
source.write_bytes(payload)
directory = base / "pack"
shutil.copytree(FIXTURE, directory)
(directory / "positive.jsonl").unlink()
manifest = yaml.safe_load((directory / "manifest.yaml").read_bytes())
manifest.update(schema_version="1.1", provenance={"source_commit": "a" * 40})
remote = {"repo": "https://github.com/example/synthetic-data", "commit": "a" * 40,
"path": source_path,
"selector": deepcopy(SELECTOR if selector is None else selector)}
manifest["sets"]["positive"] = {"sha256": digest(payload), "n": n, "remote": remote}
(directory / "manifest.yaml").write_text(yaml.safe_dump(manifest, sort_keys=True))
calls = []
urls = {remote_url(remote): source}
def fetcher(url, limit):
calls.append((url, limit))
result = urls[url].read_bytes()
assert len(result) <= limit
return result
return directory, payload, fetcher, calls
return create
def replace_manifest(directory, update):
manifest = yaml.safe_load((directory / "manifest.yaml").read_bytes())
update(manifest)
(directory / "manifest.yaml").write_text(yaml.safe_dump(manifest, sort_keys=True))
@pytest.mark.parametrize("selector", [
{}, {"select": "$", "text_field": "prompt"},
{**SELECTOR, "format": "jsonl"}, {**SELECTOR, "format": True},
{**SELECTOR, "eval": "arbitrary()"}, {**SELECTOR, "select": None},
{**SELECTOR, "select": True}, {**SELECTOR, "select": {}},
{**SELECTOR, "select": []}, {**SELECTOR, "select": ["$", "$"]},
{**SELECTOR, "select": ["$"] * 33}, {**SELECTOR, "select": ["valid", 3]},
*[{**SELECTOR, "select": path} for path in (
"", "datasets..primary", ".datasets", "datasets.", "$.datasets", "datasets[0]",
"datasets.*", "datasets/primary", "__import__('os')", "1datasets", "valid\n",
"dátasets", ".".join(["a"] * 33), "a" * 513,
)],
{**SELECTOR, "filter": []}, {**SELECTOR, "filter": {}},
{**SELECTOR, "filter": {"category": "A"}},
{**SELECTOR, "filter": {"category": []}},
{**SELECTOR, "filter": {"category": ["A", "A"]}},
{**SELECTOR, "filter": {"category": [None]}},
{**SELECTOR, "filter": {"category": [1]}},
{**SELECTOR, "filter": {"category": [True]}},
{**SELECTOR, "filter": {"category": [""]}},
{**SELECTOR, "filter": {"category": ["A" * 129]}},
{**SELECTOR, "filter": {"category": [str(index) for index in range(65)]}},
{**SELECTOR, "filter": {f"field_{index}": ["A"] for index in range(9)}},
{**SELECTOR, "filter": {"category.value": ["A"]}},
{**SELECTOR, "filter": {"a" * 65: ["A"]}},
{**SELECTOR, "text_field": "prompt.value"},
{**SELECTOR, "text_field": "a" * 65}, {**SELECTOR, "text_field": ""},
{**SELECTOR, "text_field": None}, {**SELECTOR, "text_field": ["prompt"]},
])
def test_invalid_selectors_have_schema_runtime_parity(remote_factory, selector):
directory, _, _, _ = remote_factory(selector=selector)
manifest = yaml.safe_load((directory / "manifest.yaml").read_bytes())
with pytest.raises(PackError):
validate_manifest(manifest)
with pytest.raises(ValidationError):
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(manifest)
with pytest.raises(PackError):
ConceptPack.load(directory)
@pytest.mark.parametrize("selector", [
SELECTOR, {**SELECTOR, "select": "$"},
{**SELECTOR, "select": ["datasets.primary.sentences", "datasets.secondary.sentences"]},
{**SELECTOR, "filter": {"category": ["A", "B"], "set": ["train"]}},
{**SELECTOR, "select": ".".join(["a"] * 32)},
{**SELECTOR, "filter": {"category": [str(index) for index in range(64)]}},
])
def test_valid_selectors_have_schema_runtime_parity(remote_factory, selector):
directory, _, _, _ = remote_factory(selector=selector)
pack = ConceptPack.load(directory)
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(pack.manifest)
@pytest.mark.parametrize("selector", [
None, [], "invalid", {},
{"text_field": "prompt"}, {"select": "$"},
*[{**SELECTOR, "select": value} for value in (None, True, 1, {}, [], [1], "")],
*[{**SELECTOR, "text_field": value} for value in (None, [], {}, 1, "")],
*[{**SELECTOR, "filter": value} for value in (
None, [], "invalid", 1, {"category": "A"}, {"category": [None]},
{"category": []}, {1: ["A"]},
)],
])
def test_programmatic_malformed_selector_raises_pack_error(remote_factory, selector):
directory, payload, _, _ = remote_factory()
pack = ConceptPack.load(directory)
pack.manifest["sets"]["positive"]["remote"]["selector"] = selector
with pytest.raises(PackError, match="selector"):
pack._verify("positive.jsonl", payload)
@pytest.mark.parametrize("version", ["1.0", "1.00"])
def test_selector_requires_minor_version_one(remote_factory, version):
directory, _, _, _ = remote_factory()
replace_manifest(directory, lambda manifest: manifest.update(schema_version=version))
with pytest.raises(PackError, match="require schema_version 1.1"):
ConceptPack.load(directory)
def test_lint_resolution_and_raw_json_cache_never_fetch_implicitly(remote_factory, tmp_path,
monkeypatch):
from obliteratus.concepts import pack as pack_module
def forbidden(*args, **kwargs):
pytest.fail("Remote access must be an explicit fetch")
monkeypatch.setattr(pack_module, "build_opener", forbidden)
directory, payload, fetcher, calls = remote_factory()
cache = tmp_path / "cache"
pack = ConceptPack.load(directory)
assert not calls
assert pack.manifest["sets"]["positive"]["remote"]["selector"] == SELECTOR
with pytest.raises(PackError, match="unfetched.*explicitly"):
pack.to_target(cache_dir=cache)
assert not calls
assert not cache.exists()
pack.fetch(fetcher=fetcher, cache_dir=cache)
assert len(calls) == 1
cached = cache / "files" / (digest(payload) + ".json")
assert cached.read_bytes() == payload
assert list((cache / "files").iterdir()) == [cached]
assert pack.to_target(cache_dir=cache).positive == [
record["prompt"] for record in synthetic_records()
]
pack.fetch(fetcher=forbidden, cache_dir=cache)
assert len(calls) == 1
def test_filtered_union_preserves_path_indices_categories_and_order(remote_factory, tmp_path):
primary = synthetic_records(8)
secondary = synthetic_records(8, "B")
for index, record in enumerate(primary + secondary):
record["set"] = "train" if index % 2 else "heldout"
data = {"datasets": {"primary": {"sentences": primary}, "secondary": {"sentences": secondary}}}
selector = {**SELECTOR,
"select": ["datasets.secondary.sentences", "datasets.primary.sentences"],
"filter": {"category": ["A", "B"], "set": ["train"]}}
directory, payload, fetcher, _ = remote_factory(data, selector=selector)
pack = ConceptPack.load(directory)
pack.fetch(fetcher=fetcher, cache_dir=tmp_path / "cache")
records = pack._verify("positive.jsonl", payload)
assert [record["id"] for record in records] == [
f"datasets.{name}.sentences:{index}" for name in ("secondary", "primary")
for index in (1, 3, 5, 7)
]
assert [record["category"] for record in records] == ["B"] * 4 + ["A"] * 4
assert records == pack._verify("positive.jsonl", payload)
def test_root_array_selector(remote_factory, tmp_path):
directory, payload, fetcher, _ = remote_factory(
synthetic_records(), selector={**SELECTOR, "select": "$"},
)
pack = ConceptPack.load(directory)
pack.fetch(fetcher=fetcher, cache_dir=tmp_path / "cache")
records = pack._verify("positive.jsonl", payload)
assert [record["id"] for record in records] == [f"$:{index}" for index in range(8)]
assert [record["category"] for record in records] == ["A"] * 8
@pytest.mark.parametrize("data,selector,match", [
({}, SELECTOR, "path does not exist"),
({"datasets": []}, SELECTOR, "path does not exist"),
({"datasets": {"primary": {"sentences": {}}}}, SELECTOR, "record array"),
({"datasets": {"primary": {"sentences": "records"}}}, SELECTOR, "record array"),
({}, {**SELECTOR, "select": "$"}, "record array"),
(synthetic_data(["invalid"] * 8), SELECTOR, "records must be objects"),
(synthetic_data([{"category": "A"}] * 8), SELECTOR, "text field"),
(synthetic_data([{"prompt": "Synthetic text."}] * 8),
{**SELECTOR, "filter": {"category": ["A"]}}, "filter fields"),
(synthetic_data([{"category": None, "prompt": "Synthetic text."}] * 8),
{**SELECTOR, "filter": {"category": ["A"]}}, "filter fields"),
(synthetic_data([{"category": "B", "prompt": "Synthetic text."}] * 8),
{**SELECTOR, "filter": {"category": ["A"], "set": ["train"]}}, "filter fields"),
])
def test_selector_data_shape_errors_fail_before_cache(remote_factory, tmp_path, data, selector,
match):
directory, _, fetcher, _ = remote_factory(data, selector=selector)
cache = tmp_path / "cache"
with pytest.raises(PackError, match=match):
ConceptPack.load(directory).fetch(fetcher=fetcher, cache_dir=cache)
assert not cache.exists()
@pytest.mark.parametrize("field,value,match", [
("prompt", None, "text field"), ("prompt", 1, "text field"),
("prompt", "", "records require"), ("prompt", " \t\n", "records require"),
("prompt", "a" * 2001, "records require"),
("prompt", "synthetic\x00text", "control character"),
("category", None, "invalid category"), ("category", "a" * 2001, "invalid category"),
("category", "A\x01", "control character"),
])
def test_projected_record_validation_fails_closed(remote_factory, tmp_path, field, value, match):
records = synthetic_records()
records[0][field] = value
directory, _, fetcher, _ = remote_factory(synthetic_data(records))
cache = tmp_path / "cache"
with pytest.raises(PackError, match=match):
ConceptPack.load(directory).fetch(fetcher=fetcher, cache_dir=cache)
assert not cache.exists()
def test_declared_count_applies_after_filtering(remote_factory, tmp_path):
directory, _, fetcher, _ = remote_factory(n=9)
with pytest.raises(PackError, match="record count differs from n"):
ConceptPack.load(directory).fetch(fetcher=fetcher, cache_dir=tmp_path / "cache")
directory, _, fetcher, _ = remote_factory(selector={**SELECTOR, "filter": {"category": ["B"]}})
with pytest.raises(PackError, match="record count differs from n"):
ConceptPack.load(directory).fetch(fetcher=fetcher, cache_dir=tmp_path / "filtered-cache")
assert not (tmp_path / "cache").exists()
assert not (tmp_path / "filtered-cache").exists()
@pytest.mark.parametrize("raw,match", [
(b'{"datasets": {}, "datasets": {}}', "duplicate JSON key"),
(b'{"unused": {"key": 1, "key": 2}}', "duplicate JSON key"),
(b'{"unused": NaN}', "non-finite JSON number"),
(b'{"unused": Infinity}', "non-finite JSON number"),
(b'{"unused": -Infinity}', "non-finite JSON number"),
(b'{"unused": 1e9999}', "non-finite JSON number"),
(b'{"unused": "\xff"}', "Binary content forbidden"),
])
def test_entire_raw_json_is_validated_before_projection(remote_factory, tmp_path, raw, match):
directory, _, fetcher, _ = remote_factory(raw=raw)
cache = tmp_path / "cache"
with pytest.raises(PackError, match=match):
ConceptPack.load(directory).fetch(fetcher=fetcher, cache_dir=cache)
assert not cache.exists()
@pytest.mark.parametrize("constant,limit,match", [
("MAX_JSON_NODES", 10, "node or nesting limit"),
("MAX_JSON_DEPTH", 3, "node or nesting limit"),
("MAX_SELECTED_RECORDS", 7, "record limit"),
])
def test_projection_bounds_reject_before_cache(remote_factory, tmp_path, monkeypatch, constant,
limit, match):
directory, _, fetcher, _ = remote_factory()
monkeypatch.setattr(selectors, constant, limit)
cache = tmp_path / "cache"
with pytest.raises(PackError, match=match):
ConceptPack.load(directory).fetch(fetcher=fetcher, cache_dir=cache)
assert not cache.exists()
def test_record_limit_counts_records_excluded_by_filters(remote_factory, tmp_path, monkeypatch):
records = synthetic_records() + synthetic_records(category="B")
directory, _, fetcher, _ = remote_factory(
synthetic_data(records), selector={**SELECTOR, "filter": {"category": ["A"]}},
)
monkeypatch.setattr(selectors, "MAX_SELECTED_RECORDS", 15)
with pytest.raises(PackError, match="record limit"):
ConceptPack.load(directory).fetch(fetcher=fetcher, cache_dir=tmp_path / "cache")
assert not (tmp_path / "cache").exists()
def test_hash_mismatch_never_parses_projects_or_caches(remote_factory, tmp_path, monkeypatch):
from obliteratus.concepts import pack as pack_module
directory, _, _, _ = remote_factory()
pack = ConceptPack.load(directory)
def forbidden(*args):
pytest.fail("Unverified source bytes reached a parser or selector")
monkeypatch.setattr(pack_module, "validate_content", forbidden)
monkeypatch.setattr(pack_module, "select_records", forbidden)
cache = tmp_path / "cache"
with pytest.raises(PackError, match="SHA-256 mismatch"):
pack.fetch(fetcher=lambda *args: b"unverified malformed JSON", cache_dir=cache)
assert not cache.exists()
@pytest.mark.parametrize("operation", ["fetch", "to_target"])
def test_cached_corruption_fails_without_refetch(remote_factory, tmp_path, operation):
directory, payload, fetcher, calls = remote_factory()
cache = tmp_path / "cache"
pack = ConceptPack.load(directory)
pack.fetch(fetcher=fetcher, cache_dir=cache)
cached = cache / "files" / (digest(payload) + ".json")
cached.write_bytes(payload + b" ")
def forbidden(*args):
pytest.fail("A corrupted cache must fail closed without refetching")
with pytest.raises(PackError, match="SHA-256 mismatch"):
if operation == "fetch":
pack.fetch(fetcher=forbidden, cache_dir=cache)
else:
pack.to_target(cache_dir=cache)
assert len(calls) == 1
assert cached.read_bytes() == payload + b" "
@pytest.mark.parametrize("source_path", ["datasets/data.jsonl", "datasets/data.txt"])
def test_selector_requires_remote_json_source(remote_factory, source_path):
directory, _, _, _ = remote_factory(source_path=source_path)
with pytest.raises(PackError, match="remote .json source"):
ConceptPack.load(directory)
def test_selector_cannot_project_non_set_reference_file(remote_factory):
directory, _, _, _ = remote_factory()
def update(manifest):
entry = deepcopy(manifest["sets"]["positive"])
entry.pop("n")
entry["logical_path"] = "reference/data.json"
manifest["files"] = [entry]
manifest["licence"].append({"path": "reference/data.json", "spdx": "CC0-1.0",
"source": "local fixture", "attribution": "test authors"})
replace_manifest(directory, update)
with pytest.raises(PackError, match="JSONL set"):
ConceptPack.load(directory)
@pytest.mark.parametrize("logical_path,payload", [
("reference/metadata.json", b'{"synthetic": true}'),
("reference/results.csv", b"dose,value\n0,1\n"),
("reference/LICENSE", b"Synthetic fixture licence notice.\n"),
])
def test_remote_reference_logical_paths_are_verified_without_projection(remote_factory, tmp_path,
logical_path, payload):
directory, _, fetcher, calls = remote_factory()
suffix = Path(logical_path).suffix
remote = {"repo": "https://github.com/example/synthetic-data", "commit": "a" * 40,
"path": "upstream/notice" + suffix if suffix else "LICENSE"}
def update(manifest):
manifest["files"] = [{"remote": remote, "logical_path": logical_path,
"sha256": digest(payload)}]
manifest["licence"].append({"path": logical_path, "spdx": "CC0-1.0",
"source": "local fixture", "attribution": "test authors"})
replace_manifest(directory, update)
url = remote_url(remote)
def fetch_all(requested, limit):
return payload if requested == url else fetcher(requested, limit)
pack = ConceptPack.load(directory)
cache = tmp_path / "cache"
pack.fetch(fetcher=fetch_all, cache_dir=cache)
assert (cache / "files" / (digest(payload) + suffix)).read_bytes() == payload
assert len(calls) == 1
assert len(pack.to_target(cache_dir=cache).positive) == 8
def test_multiple_projections_share_one_verified_raw_cache(remote_factory, tmp_path):
records = synthetic_records() + synthetic_records(category="B")
directory, payload, fetcher, calls = remote_factory(
synthetic_data(records), selector={**SELECTOR, "filter": {"category": ["A"]}},
)
def update(manifest):
entry = deepcopy(manifest["sets"]["positive"])
entry["name"] = "neutral"
entry["remote"]["selector"]["filter"] = {"category": ["B"]}
manifest["sets"]["controls"] = [entry]
replace_manifest(directory, update)
(directory / "controls" / "neutral.jsonl").unlink()
pack = ConceptPack.load(directory)
cache = tmp_path / "cache"
pack.fetch(fetcher=fetcher, cache_dir=cache)
target = pack.to_target(cache_dir=cache)
assert len(target.positive) == len(target.controls["neutral"]) == 8
assert len(calls) == 1
assert [path.name for path in (cache / "files").iterdir()] == [digest(payload) + ".json"]