Files
OBLITERATUS/tests/test_pain_pack.py
Joseph Magly 002e6e56e1 feat: complete local readiness across UI, MCP and memory admission
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
2026-10-20 17:58:20 -04:00

534 lines
23 KiB
Python

"""Offline metadata, pinned-source mapping and redistribution checks for pain."""
import ast
import hashlib
import json
from pathlib import Path
import re
import subprocess
from jsonschema import Draft202012Validator
import pytest
import yaml
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.cli import main
from obliteratus.concepts.pack import ConceptPack
from obliteratus.concepts.registry import ConceptRegistry
from obliteratus.concepts.target import ExtractionRecipe
from obliteratus.concepts.validation import PackError, SCHEMA_PATH
pytestmark = pytest.mark.cpu
ROOT = Path(__file__).parents[1]
PIN = "4d75cd90e206ea962f7a9101e65c85efea56723b"
LOCAL_SOURCE = ROOT.parent / "pain-axis-pin"
PACK_DIR = ROOT / "obliteratus/concepts/packs/pain"
# Pain-specific pinned material only; normalized sentences of at least eight words.
FINGERPRINTS = ROOT / "tests/fixtures/pain-axis-4d75cd90.FINGERPRINT"
MAX_TEXT_BYTES = 2 * 1024 * 1024
SENTENCE_PATTERN = re.compile(r"[^.!?]+[.!?](?: I feel:)?|[^.!?]+$")
CANDIDATE_EDGES = " \t\"',`"
def test_pain_pack_has_only_descriptors_and_no_inline_text():
pack = ConceptRegistry().pack("pain")
assert set(pack.files) == {"manifest.yaml", "DATASET_CARD.md"}
pending = [pack.manifest]
while pending:
value = pending.pop()
if isinstance(value, dict):
assert "text" not in value
pending.extend(value.values())
elif isinstance(value, list):
pending.extend(value)
def test_pain_manifest_hashes_match_committed_pin_inventory():
pack = ConceptRegistry().pack("pain")
inventory = dict(line.split(" ", 1)[::-1] for line in (
ROOT / ".aiwg/planning/pain-axis-4d75cd90.sha256"
).read_text().splitlines())
assert pack.manifest["provenance"]["source_commit"] == PIN
for entry in pack.entries.values():
if "remote" in entry:
remote = entry["remote"]
assert remote["repo"] == "https://github.com/valen-research/Pain-axis"
assert remote["commit"] == PIN
assert entry["sha256"] == inventory[remote["path"]]
else:
assert entry["sha256"] == hashlib.sha256(pack.files[entry["path"]]).hexdigest()
def test_pain_inspection_never_fetches(tmp_path, monkeypatch, capsys):
monkeypatch.setenv("OBLITERATUS_CACHE_DIR", str(tmp_path / "cache"))
def forbidden(*args, **kwargs):
pytest.fail("Inspection attempted a network fetch")
monkeypatch.setattr("obliteratus.concepts.pack.https_fetch", forbidden)
monkeypatch.setattr("obliteratus.concepts.pack.ConceptPack.fetch", forbidden)
for command in ("show", "lint"):
main(["concepts", command, "pain", "--json"])
result = json.loads(capsys.readouterr().out)
assert result["id"] == "pain"
assert result["tier"] == "research"
if command == "lint":
assert result["valid"] is True
registry = ConceptRegistry()
pack = registry.pack("pain")
Draft202012Validator(json.loads(SCHEMA_PATH.read_text())).validate(pack.manifest)
assert set(pack.files) == {"manifest.yaml", "DATASET_CARD.md"}
with pytest.raises(PackError, match="unfetched.*fetch explicitly"):
registry.get("pain")
assert not (tmp_path / "cache").exists()
@pytest.mark.parametrize("access", ["pack", "get"])
def test_pain_is_loaded_once_on_first_access(monkeypatch, tmp_path, access):
monkeypatch.setenv("OBLITERATUS_CACHE_DIR", str(tmp_path / "cache"))
original = ConceptPack.load
calls = []
def load(source):
calls.append(source)
return original(source)
monkeypatch.setattr(ConceptPack, "load", load)
registry = ConceptRegistry()
registry.get("refusal")
registry.pack("core/refusal")
assert calls == []
if access == "get":
with pytest.raises(PackError, match="unfetched"):
registry.get("pain")
else:
assert registry.pack("pain").manifest["id"] == "pain"
registry.pack("pain")
assert calls == [PACK_DIR]
@pytest.mark.parametrize("damage", ["card_hash", "missing_pack"])
def test_broken_pain_is_excluded_without_breaking_other_packs(
tmp_path, monkeypatch, capsys, damage,
):
from tests.test_concept_packs import FIXTURES
original = ConceptPack.load
files = {path.name: path.read_bytes() for path in PACK_DIR.iterdir()}
files["DATASET_CARD.md"] += b"\nChanged card.\n"
calls = []
def load(source):
if source == PACK_DIR:
calls.append(source)
if damage == "missing_pack":
raise FileNotFoundError(source)
return ConceptPack(files)
return original(source)
monkeypatch.setattr(ConceptPack, "load", load)
registry = ConceptRegistry()
registry.load_pack(FIXTURES / "valid_minimal")
assert registry.get("refusal").id == "refusal"
assert registry.get("user/synthetic").id == "user/synthetic"
assert calls == []
assert {pack.manifest["id"] for pack in registry.list_packs()} == {"refusal", "synthetic"}
assert "Warning: skipping built-in concept pack" in capsys.readouterr().err
for access in (registry.pack, registry.get):
with pytest.raises(PackError, match="Cannot read built-in concept pack.*pain"):
access("pain")
assert registry.pack("refusal").manifest["id"] == "refusal"
assert registry.pack("user/synthetic").manifest["id"] == "synthetic"
assert calls == [PACK_DIR]
monkeypatch.setenv("OBLITERATUS_CACHE_DIR", str(tmp_path / "cache"))
for command in (["list"], ["show", "refusal"], ["lint", "refusal"],
["show", str(FIXTURES / "valid_minimal")]):
main(["concepts", *command, "--json"])
result = json.loads(capsys.readouterr().out)
if command == ["list"]:
assert [row["id"] for row in result] == ["refusal"]
else:
assert result["id"] == ("synthetic" if command[-1] != "refusal" else "refusal")
with pytest.raises(SystemExit) as error:
main(["concepts", "show", "pain", "--json"])
assert error.value.code == 4
assert "Cannot read built-in concept pack" in capsys.readouterr().err
def test_pain_recipe_and_category_mapping_match_spike():
manifest = ConceptRegistry().pack("pain").manifest
recipe = ExtractionRecipe(**manifest["recipe"])
assert recipe.to_dict() == {
"method": "diff_means", "n_directions": 1, "pool_controls": "concat",
"readout": {"position": "final_token", "suffix": " I feel:", "suffix_mode": "ensure"},
"denoise": {"control_pc_variance": 0.5},
"layer_select": {"strategy": "cv_auc", "folds": 5},
"extraction_layer": {
"Qwen/Qwen2.5-7B-Instruct": 24,
"google/gemma-2-2b-it": 25,
"google/gemma-2-9b-it": 37,
},
"intervention_layer": {"rule": "norm_ratio", "target": 0.6},
"use_chat_template": False,
"collateral_margin": 0.03,
}
positive = manifest["sets"]["positive"]
assert positive["n"] == 100
assert positive["remote"]["selector"] == {
"format": "json", "select": "datasets.S2_1P.sentences", "text_field": "prompt",
"filter": {"category": ["A1", "A2", "A3", "A4", "A5"]},
}
assert {entry["name"]: entry["remote"]["selector"]["filter"]["category"]
for entry in manifest["sets"]["controls"]} == {
"fear": ["B"], "negative_emotion": ["C1"], "negative_world_state": ["C2"],
"bodily_sensation": ["E"], "neutral": ["D"],
}
assert all(entry["n"] == 20 for entry in manifest["sets"]["controls"])
assert manifest["evaluation"]["hook"] == "run_concept_evaluation_battery"
assert manifest["evaluation"]["dose_ladder"]["coefficients"] == [-2, -1, 0, .5, 1, 1.5, 2, 3]
assert set(manifest["citations"]) == {
"arXiv:2609.16247", "doi:10.5281/zenodo.22902830", "arXiv:2609.35591",
"arXiv:2605.05653", "arXiv:2602.19159", "arXiv:2606.26987",
}
def test_pain_local_pinned_sources_match_hashes_and_selectors():
if not LOCAL_SOURCE.is_dir():
pytest.skip("Local pinned Pain-axis copy unavailable")
pack = ConceptRegistry().pack("pain")
assert pack.manifest["provenance"]["source_commit"] == PIN
for path, entry in pack.entries.items():
if "remote" not in entry:
continue
assert entry["remote"]["repo"] == "https://github.com/valen-research/Pain-axis"
assert entry["remote"]["commit"] == PIN
data = (LOCAL_SOURCE / entry["remote"]["path"]).read_bytes()
# Only count and verify; failure reports never include source sentences.
records = pack._verify(path, data)
if records is not None:
assert len(records) == entry["n"]
licence = next(item for item in pack.manifest["licence"] if item["path"] == path)
assert licence["spdx"] == "MIT"
assert (LOCAL_SOURCE / "LICENSE").read_text().splitlines()[0] == "MIT License"
def _normalize_sentence(value):
return " ".join(value.split()).lower().removesuffix(" i feel:").rstrip()
def _fingerprint(value):
text = _normalize_sentence(value)
return hashlib.sha256(text.encode("utf-8", errors="surrogatepass")).hexdigest()
def _strings(value):
pending = [value]
while pending:
child = pending.pop()
if isinstance(child, str):
yield child
elif isinstance(child, (dict, list)):
pending.extend(child.values() if isinstance(child, dict) else child)
def _source_fingerprints():
datasets = LOCAL_SOURCE / "datasets"
extraction = json.loads((datasets / "3.1_pain_and_control_datasets.json").read_bytes())
sentences = []
for name, dataset in extraction["datasets"].items():
for record in dataset["sentences"]:
if (name.startswith(("S1_", "S2_"))
and record["category"] in {"A1", "A2", "A3", "A4", "A5"}
or name.startswith(("Numb_", "Arousal_"))):
sentences.append(record["prompt"])
sadness = json.loads((datasets / "3.1_sadness_dataset.json").read_bytes())
for dataset in sadness["datasets"].values():
sentences.extend(record["prompt"] for record in dataset["sentences"])
scenarios = json.loads((datasets / "4.1_self_other_420_scenarios.json").read_bytes())
sentences.extend(record["text"] for record in scenarios)
selfmed = json.loads((datasets / "4.3_selfmed_101_scenarios.json").read_bytes())
for field in ("positive_prompts", "harmful_prompts"):
sentences.extend(_strings(selfmed[field]))
return {_fingerprint(text) for text in sentences
if len(_normalize_sentence(text).split()) >= 8}
def _fixture_fingerprints():
return {line for line in FINGERPRINTS.read_text().splitlines()
if line and not line.startswith("#")}
def _line_windows(text):
# Normalize each line once and retain the original 32-line wrapping budget.
lines = [" ".join(line.split()).lower() for line in text.splitlines()]
for start, value in enumerate(lines):
for line in lines[start + 1:start + 32]:
if line:
value = value + " " + line if value else line
yield value
def _scalar_candidates(values):
for value in values:
yield value
if "\n" in value:
yield from _line_windows(value)
def _line_window_fingerprints(text):
edges = CANDIDATE_EDGES.encode()
suffix = b" i feel:"
lines = [" ".join(line.split()).lower().encode("utf-8", errors="surrogatepass")
for line in text.splitlines()]
for start in range(len(lines)):
digest = hashlib.sha256()
tail = b""
started = committed = False
for end in range(start, min(start + 32, len(lines))):
line = lines[end]
if line:
fragment = (b" " if started else b"") + line
started = True
value = tail + fragment
if not committed:
value = value.lstrip(edges)
cut = max(0, len(value.rstrip(edges)) - len(suffix) - 1)
digest.update(value[:cut])
tail = value[cut:]
committed = committed or bool(cut)
if end > start:
candidate = digest.copy()
candidate.update(tail.rstrip(edges).removesuffix(suffix).rstrip())
yield candidate.hexdigest()
def _text_candidates(text, suffix):
lines = text.splitlines()
yield from lines
yield from re.split(r"\n\s*\n", text)
# Sentence boundaries catch prose embedded in longer paragraphs and wrapping.
yield from (match.group() for match in SENTENCE_PATTERN.finditer(text))
if suffix == ".py":
try:
yield from _scalar_candidates(
node.value for node in ast.walk(ast.parse(text))
if isinstance(node, ast.Constant) and isinstance(node.value, str)
)
except SyntaxError:
yield from _line_windows(text)
elif suffix in {".json", ".jsonl"}:
parsed = True
for record in text.splitlines() if suffix == ".jsonl" else [text]:
try:
yield from _scalar_candidates(_strings(json.loads(record)))
except ValueError:
parsed = False
if not parsed:
yield from _line_windows(text)
elif suffix in {".yaml", ".yml"}:
try:
loader = getattr(yaml, "CSafeLoader", yaml.SafeLoader)
yield from _scalar_candidates(_strings(yaml.load(text, Loader=loader)))
except yaml.YAMLError:
yield from _line_windows(text)
def _sentence_matches(text, suffix, fingerprints):
matches = set()
seen = set()
def check(normalized):
normalized = normalized.strip(CANDIDATE_EDGES).removesuffix(" i feel:").rstrip()
if normalized in seen:
return
seen.add(normalized)
fingerprint = hashlib.sha256(normalized.encode("utf-8", errors="surrogatepass")).hexdigest()
if fingerprint in fingerprints:
matches.add(fingerprint)
for value in _text_candidates(text, suffix):
check(" ".join(value.strip(CANDIDATE_EDGES).split()).lower())
# Parsed scalars already join wrapping in source, JSON and YAML. Prose files
# still need line windows to recognize sentences containing multiple stops.
if suffix in {".py", ".json", ".jsonl", ".yaml", ".yml"}:
return matches
matches.update(fingerprints.intersection(_line_window_fingerprints(text)))
return matches
def test_no_upstream_sentences_in_repository():
"""Guard normalized sentences with >=8 words from pain-specific pinned material.
Select S1/S2 A1-A5, Numb (injury without pain), Arousal and sadness sets,
self/other scenarios and selfmed positive/harmful battery prompts. Exclude
neutral and generic controls, Random, ControlSupplement and finetuning pairs.
Scan text files throughout the tracked repository, using hashes only.
Binary files and files larger than 2 MiB are excluded.
"""
fingerprints = _fixture_fingerprints()
assert len(fingerprints) == 1332
assert all(re.fullmatch(r"[0-9a-f]{64}", value) for value in fingerprints)
listed = subprocess.check_output(
["git", "ls-files", "-z"], cwd=ROOT,
).decode("utf-8", errors="surrogateescape").split("\0")
for name in sorted(set(listed) - {""}):
path = ROOT / name
if not path.is_file() or path.is_symlink() or path.stat().st_size > MAX_TEXT_BYTES:
continue
data = path.read_bytes()
if b"\0" in data:
continue
try:
text = data.decode("utf-8")
except UnicodeError:
continue
matches = _sentence_matches(text, path.suffix, fingerprints)
assert not matches, f"Upstream sentence fingerprint found in {name}"
def test_fingerprints_match_local_pin_when_available():
if not LOCAL_SOURCE.is_dir():
pytest.skip("Local pinned Pain-axis copy unavailable")
assert _source_fingerprints() == _fixture_fingerprints()
def test_source_fingerprints_select_categories_and_normalized_word_count(tmp_path, monkeypatch):
monkeypatch.setattr(f"{__name__}.LOCAL_SOURCE", tmp_path)
datasets = tmp_path / "datasets"
datasets.mkdir()
long_text = "Synthetic sentence with exactly eight words for testing."
short_text = "Synthetic sentence with only seven words here. I feel:"
excluded = "Generic control sentence with eight words stays excluded."
extraction = {"datasets": {
"S1_1P": {"sentences": [
{"category": "A1", "prompt": long_text},
{"category": "A2", "prompt": short_text},
*({"category": category, "prompt": excluded}
for category in ("B", "C1", "C2", "D", "E")),
]},
"Random_1P": {"sentences": [{"category": "N1", "prompt": excluded}]},
"ControlSupplement_1P": {"sentences": [{"category": "B", "prompt": excluded}]},
"Numb_3P": {"sentences": [{"category": "A1_numb", "prompt": long_text + " Numb."}]},
"Arousal_1P": {"sentences": [{"category": "P1", "prompt": long_text + " Arousal."}]},
}}
sources = {
"3.1_pain_and_control_datasets.json": extraction,
"3.1_sadness_dataset.json": {"datasets": {"SD_sadness_3P": {
"sentences": [{"prompt": long_text + " Sadness."}],
}}},
"4.1_self_other_420_scenarios.json": [{"text": long_text + " Scenario."}],
"4.3_selfmed_101_scenarios.json": {
"positive_prompts": [long_text + " Positive."],
"harmful_prompts": [long_text + " Harmful."],
"neutral_prompts": [excluded],
},
"4.3_selfmed_finetuning_1684_pairs.json": [{"prompt": excluded}],
}
for name, value in sources.items():
(datasets / name).write_text(json.dumps(value))
assert _source_fingerprints() == {
_fingerprint(long_text + suffix)
for suffix in ("", " Numb.", " Arousal.", " Sadness.",
" Scenario.", " Positive.", " Harmful.")
}
@pytest.mark.parametrize("text,suffix", [
('value = "Synthetic upstream observation. I feel:"', ".py"),
('{"prompt": "Synthetic upstream observation. I feel:"}', ".json"),
('text: "Synthetic upstream observation. I feel:"', ".yaml"),
("Introductory prose. Synthetic upstream observation. Later prose.", ".md"),
("Earlier line.\nSynthetic upstream\nobservation. I feel:\nLater line.", ".md"),
('value = "Synthetic upstream " "observation. I feel:"', ".py"),
('{"prompt": "Synthetic upstream observation. I feel:"}\n', ".jsonl"),
('text: "Synthetic upstream observati\\u006fn. I feel:"', ".yaml"),
])
def test_sentence_scanner_detects_suffix_prose_and_wrapping(text, suffix):
fingerprint = _fingerprint("Synthetic upstream observation.")
assert _sentence_matches(text, suffix, {fingerprint}) == {fingerprint}
def test_sentence_fingerprint_normalizes_case_whitespace_and_suffix():
assert _fingerprint(" Synthetic upstream\nobservation. I feel: ") == _fingerprint(
"synthetic upstream observation.",
)
assert not _sentence_matches("Unrelated synthetic observation.", ".md",
{_fingerprint("Synthetic upstream observation.")})
@pytest.mark.parametrize("suffix", [".md", ".py", ".json", ".jsonl", ".yaml"])
def test_sentence_scanner_keeps_multi_sentence_line_windows(suffix):
sentence = "Synthetic upstream observation. Another observation follows."
fingerprint = _fingerprint(sentence)
text = ("Earlier line.\nSynthetic upstream\nobservation. Another\n"
"observation follows. I feel:\nLater line.")
assert _sentence_matches(text, ".md", {fingerprint}) == {fingerprint}
if suffix == ".py":
text = f'value = """{text}"""'
elif suffix in {".json", ".jsonl"}:
text = json.dumps({"text": text})
elif suffix == ".yaml":
text = yaml.safe_dump({"text": text})
assert _sentence_matches(text, suffix, {fingerprint}) == {fingerprint}
@pytest.mark.parametrize("text", [
"", "one line", "\n\n\n", " ,`\n\"\nSynthetic observation.\n\n I feel: ,`",
"Éarlier line.\nSynthetic\t observation.\nI\nfeel:\nLater line.",
"İ and a surrogate \ud800.\nA second observation. I feel:",
"short\nwords\n i feel:\n,`\n\"\n",
"\n".join([" ,`" * 40, "Synthetic observation.", " ,`" * 40, " I feel: ,`"]),
"\n".join(f"Synthetic observation {index}." for index in range(40)),
"\n".join(["Earlier line.", "Synthetic observation.", "I feel:", "", " ,`"] * 9),
])
def test_incremental_window_hashes_preserve_all_normalized_candidates(text):
expected = [hashlib.sha256(
value.strip(CANDIDATE_EDGES).removesuffix(" i feel:").rstrip().encode(
"utf-8", errors="surrogatepass",
),
).hexdigest() for value in _line_windows(text)]
assert list(_line_window_fingerprints(text)) == expected
@pytest.mark.parametrize("name", ["root.txt", "docs/nested/prose.txt", ".aiwg/nested/prose"])
def test_repository_scanner_checks_tracked_text_in_every_directory(tmp_path, monkeypatch, name):
sentence = "Synthetic sentence with exactly eight words for testing."
fingerprints = _fixture_fingerprints()
fingerprints.pop()
fingerprints.add(_fingerprint(sentence))
monkeypatch.setattr(f"{__name__}._fixture_fingerprints", lambda: fingerprints)
monkeypatch.setattr(f"{__name__}.ROOT", tmp_path)
path = tmp_path / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(sentence)
def tracked_files(command, *, cwd):
assert command == ["git", "ls-files", "-z"]
assert cwd == tmp_path
return name.encode() + b"\0"
monkeypatch.setattr(subprocess, "check_output", tracked_files)
with pytest.raises(AssertionError, match=re.escape(name)):
test_no_upstream_sentences_in_repository()
@pytest.mark.parametrize("mode", [None, "append", "ensure"])
def test_suffix_mode_is_validated(mode):
readout = {"suffix": " answer"}
if mode is not None:
readout["suffix_mode"] = mode
from obliteratus.concepts.extraction import validate_recipe
validate_recipe(ExtractionRecipe(readout=readout))
with pytest.raises(ValueError, match="readout requires"):
validate_recipe(ExtractionRecipe(readout={"suffix_mode": "execute"}))
def test_recipe_raw_prompt_setting_precedes_method_default():
from obliteratus.concepts.target import ConceptTarget
target = ConceptTarget(id="user/raw", positive=["harmful hello"],
controls={"neutral": ["harmless world"]},
recipe=ExtractionRecipe(use_chat_template=False))
pipeline = AbliterationPipeline("unused", concept=target, method="advanced")
assert pipeline.use_chat_template is False
override = AbliterationPipeline("unused", concept=target, use_chat_template=True)
assert override.use_chat_template is True