mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Replace significance-only FAIL with a configurable non-inferiority decision (default 3 points): PASS, FAIL, INCONCLUSIVE, UNDERPOWERED. Plumb the margin through recipes, CLI, reports, cards, metadata and the Q4 export gate, and add 'concepts rescore' for saved batteries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
421 lines
20 KiB
Python
421 lines
20 KiB
Python
"""Concept Pack v1 manifests, prompt projections and explicit remote fetching."""
|
|
|
|
from copy import deepcopy
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import tempfile
|
|
from urllib.parse import quote, urlsplit
|
|
from urllib.request import HTTPRedirectHandler, Request, build_opener
|
|
|
|
import yaml
|
|
|
|
from obliteratus.concepts.pack_io import (
|
|
MAX_ARCHIVE_BYTES, MAX_ENTRIES, MAX_FILE_BYTES, MAX_PACK_BYTES,
|
|
data_path, digest, read_file, read_pack,
|
|
relative_path, validate_content, zip_bytes,
|
|
)
|
|
from obliteratus.concepts.target import ConceptTarget, ExtractionRecipe
|
|
from obliteratus.concepts.selectors import select_records
|
|
from obliteratus.concepts.validation import PackError, json_data, load_manifest
|
|
|
|
|
|
def cache_directory():
|
|
override = os.environ.get("OBLITERATUS_CACHE_DIR")
|
|
base = Path(override) if override else Path(
|
|
os.environ.get("XDG_CACHE_HOME", str(Path.home() / ".cache")),
|
|
) / "obliteratus"
|
|
return base / "concepts"
|
|
|
|
|
|
def _https_url(url):
|
|
try:
|
|
parsed = urlsplit(url)
|
|
if (parsed.scheme != "https" or not parsed.hostname or parsed.username
|
|
or parsed.password or parsed.fragment or parsed.query or parsed.port
|
|
or any(char.isspace() for char in url)):
|
|
raise ValueError("unsafe HTTPS URL")
|
|
except ValueError as exc:
|
|
raise PackError(
|
|
"Remote repository/import URL must be plain HTTPS without credentials",
|
|
) from exc
|
|
relative_path(parsed.path.strip("/"))
|
|
return parsed
|
|
|
|
|
|
class _NoRedirect(HTTPRedirectHandler):
|
|
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
raise PackError("Remote download redirects are forbidden")
|
|
|
|
|
|
def https_fetch(url, limit):
|
|
"""Explicit, bounded HTTPS download. Callers may inject an offline fetcher."""
|
|
_https_url(url)
|
|
with build_opener(_NoRedirect()).open(Request(url), timeout=30) as response:
|
|
data = response.read(limit + 1)
|
|
if len(data) > limit:
|
|
raise PackError("Remote download exceeds size limit")
|
|
return data
|
|
|
|
|
|
def remote_url(remote):
|
|
commit = remote.get("commit")
|
|
if not isinstance(commit, str) or not re.fullmatch(r"[a-fA-F0-9]{40}", commit):
|
|
raise PackError("Remote commit must be a pinned 40-hex hash")
|
|
parsed = _https_url(remote["repo"])
|
|
if parsed.hostname != "github.com":
|
|
raise PackError("Default fetch supports pinned GitHub HTTPS repositories only")
|
|
repo = parsed.path.removesuffix(".git").strip("/")
|
|
if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repo):
|
|
raise PackError("Invalid GitHub repository URL")
|
|
if any(part in {".", ".."} for part in repo.split("/")):
|
|
raise PackError("Invalid GitHub repository path traversal")
|
|
path = relative_path(remote["path"])
|
|
return f"https://raw.githubusercontent.com/{repo}/{commit}/{quote(path)}"
|
|
|
|
|
|
def _atomic_write(path, data, *, exclusive=False):
|
|
path = Path(path)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
fd, name = tempfile.mkstemp(dir=path.parent)
|
|
temporary = Path(name)
|
|
try:
|
|
with os.fdopen(fd, "wb") as stream:
|
|
stream.write(data)
|
|
if exclusive:
|
|
os.link(temporary, path)
|
|
else:
|
|
os.replace(temporary, path)
|
|
finally:
|
|
temporary.unlink(missing_ok=True)
|
|
|
|
|
|
class ConceptPack:
|
|
"""Validated bundled data and unresolved references; construction never fetches."""
|
|
|
|
def __init__(self, files):
|
|
if len(files) > MAX_ENTRIES or sum(map(len, files.values())) > MAX_PACK_BYTES:
|
|
raise PackError("Pack exceeds entry or total size limit")
|
|
self.files = dict(files)
|
|
self.manifest = load_manifest(self.files["manifest.yaml"])
|
|
self.warnings = []
|
|
self.entries = {}
|
|
sets = self.manifest["sets"]
|
|
self._entry(sets["positive"], "positive.jsonl")
|
|
for kind in ("controls", "neighbours", "battery"):
|
|
names = set()
|
|
for entry in sets.get(kind, []):
|
|
name = entry["name"]
|
|
if name in names:
|
|
raise PackError(f"Duplicate {kind} name: {name}")
|
|
names.add(name)
|
|
self._entry(entry, f"{kind}/{name}.jsonl")
|
|
for entry in self.manifest.get("files", []):
|
|
logical = entry.get("logical_path") or entry.get("path") or entry["remote"]["path"]
|
|
if logical.endswith(".jsonl"):
|
|
raise PackError("JSONL files must be declared in sets")
|
|
self._entry(entry, logical)
|
|
licences = {}
|
|
for entry in self.manifest["licence"]:
|
|
path = data_path(entry["path"])
|
|
if path in licences:
|
|
raise PackError(f"Duplicate licence entry: {path}")
|
|
licences[path] = entry
|
|
expected = {path for path, entry in self.entries.items() if "path" in entry}
|
|
if set(self.files) - {"manifest.yaml"} != expected:
|
|
raise PackError(
|
|
"Missing or undeclared bundled files; all data needs a hash and licence",
|
|
)
|
|
if set(licences) != set(self.entries):
|
|
missing = set(self.entries) - set(licences)
|
|
raise PackError(f"Missing or undeclared licence: {', '.join(sorted(missing))}")
|
|
if any("remote" in entry for entry in self.entries.values()):
|
|
if not self.manifest.get("provenance", {}).get("source_commit"):
|
|
raise PackError("Remote references require provenance.source_commit")
|
|
if self.manifest["tier"] in {"core", "research"}:
|
|
if "DATASET_CARD.md" not in self.files:
|
|
raise PackError("Core/research packs require DATASET_CARD.md")
|
|
if self.manifest["tier"] == "core":
|
|
if self.manifest["id"] != "refusal":
|
|
raise PackError("Only refusal may use tier core")
|
|
if sum(map(len, self.files.values())) > 5 * 1024 * 1024:
|
|
raise PackError("Core pack exceeds 5 MB size limit")
|
|
permitted = {"AGPL-3.0-or-later", "MIT", "Apache-2.0", "CC0-1.0", "CC-BY-4.0"}
|
|
if any(licences[path]["spdx"] not in permitted for path in expected):
|
|
raise PackError("Core bundled files need a redistribution-compatible SPDX licence")
|
|
thresholds = self.manifest.get("validation_thresholds", {})
|
|
for name, bound, minimum in (
|
|
("min_heldout_auc", 0.75, True), ("min_random_percentile", 95, True),
|
|
("max_neighbour_abs_cos", 0.8, False),
|
|
):
|
|
if name in thresholds and (thresholds[name] < bound if minimum
|
|
else thresholds[name] > bound):
|
|
raise PackError(f"Core threshold may only tighten: {name}")
|
|
for path in expected:
|
|
self._verify(path, self.files[path])
|
|
|
|
@classmethod
|
|
def load(cls, source):
|
|
try:
|
|
return cls(read_pack(source))
|
|
except (OSError, ValueError) as exc:
|
|
if isinstance(exc, PackError):
|
|
raise
|
|
raise PackError(f"Cannot read concept pack: {exc}") from exc
|
|
|
|
def _entry(self, entry, logical):
|
|
if len(self.entries) >= MAX_ENTRIES - 1:
|
|
raise PackError("Pack exceeds descriptor entry limit")
|
|
path = data_path(logical)
|
|
if path == "manifest.yaml" or path in self.entries:
|
|
raise PackError(f"Duplicate or reserved data entry: {path}")
|
|
if "path" in entry and entry["path"] != path:
|
|
raise PackError(f"Set path must match its layout: {path}")
|
|
if "logical_path" in entry and "remote" not in entry:
|
|
raise PackError("logical_path is only valid for remote files")
|
|
if "remote" in entry:
|
|
remote = entry["remote"]
|
|
_https_url(remote["repo"])
|
|
relative_path(remote["path"])
|
|
selector = remote.get("selector")
|
|
if "selector" in remote or "logical_path" in entry:
|
|
if int(self.manifest["schema_version"].split(".")[1]) < 1:
|
|
raise PackError("JSON selectors and logical_path require schema_version 1.1+")
|
|
if selector is not None and (
|
|
not path.endswith(".jsonl") or not remote["path"].endswith(".json")
|
|
):
|
|
raise PackError("JSON selectors require a remote .json source and a JSONL set")
|
|
# The repository path is also data-only, even when its logical name differs.
|
|
if selector is None and Path(remote["path"]).suffix != Path(path).suffix:
|
|
raise PackError(f"Remote data extension must match {path}")
|
|
if "n" in entry and entry["n"] < 32:
|
|
self.warnings.append(f"underpowered: {path} has fewer than 32 records")
|
|
self.entries[path] = entry
|
|
|
|
def _verify(self, path, data):
|
|
entry = self.entries[path]
|
|
expected = entry["sha256"].lower()
|
|
if digest(data) != expected:
|
|
raise PackError(f"{path}: SHA-256 mismatch; expected {expected}")
|
|
selector = entry.get("remote", {}).get("selector")
|
|
has_selector = "selector" in entry.get("remote", {})
|
|
validate_content(entry["remote"]["path"] if has_selector else path, data)
|
|
if path.endswith(".jsonl"):
|
|
records = (select_records(data, selector, path) if has_selector else
|
|
[json_data(line, path) for line in data.decode("utf-8").splitlines()])
|
|
if len(records) != entry["n"]:
|
|
raise PackError(f"{path}: record count differs from n")
|
|
ids = set()
|
|
for record in records:
|
|
if (not isinstance(record, dict) or not isinstance(record.get("text"), str)
|
|
or not record["text"].strip() or len(record["text"]) > 2000
|
|
or not isinstance(record.get("id"), str) or not record["id"]):
|
|
raise PackError(f"{path}: records require id and text (at most 2000 chars)")
|
|
if record["id"] in ids:
|
|
raise PackError(f"{path}: duplicate record id")
|
|
ids.add(record["id"])
|
|
if ("category" in record and (not isinstance(record["category"], str)
|
|
or len(record["category"]) > 2000)
|
|
or len(record["id"]) > 2000
|
|
or "person" in record and record["person"] not in ("1st", "3rd")
|
|
or "split" in record and record["split"] not in ("train", "heldout")):
|
|
raise PackError(f"{path}: invalid category, person or split")
|
|
for field in ("text", "id", "category"):
|
|
if field in record and any(
|
|
ord(char) < 32 and char not in "\n\r\t" for char in record[field]
|
|
):
|
|
raise PackError(f"{path}: forbidden control character in {field}")
|
|
return records
|
|
|
|
@property
|
|
def sha256(self):
|
|
return digest(zip_bytes(self.files))
|
|
|
|
def _cached(self, path, cache_dir):
|
|
entry = self.entries[path]
|
|
source = entry.get("remote", {}).get("path", path)
|
|
return Path(cache_dir) / "files" / (entry["sha256"].lower() + Path(source).suffix)
|
|
|
|
def fetch(self, *, fetcher=https_fetch, cache_dir=None):
|
|
"""The only remote-data fetch step; verify before publishing into the cache."""
|
|
cache_dir = cache_directory() if cache_dir is None else cache_dir
|
|
total = sum(map(len, self.files.values()))
|
|
seen = set()
|
|
for path, entry in self.entries.items():
|
|
if "remote" not in entry:
|
|
continue
|
|
cached = self._cached(path, cache_dir)
|
|
if cached.exists() or cached.is_symlink():
|
|
data = read_file(cached)
|
|
else:
|
|
data = fetcher(remote_url(entry["remote"]), MAX_FILE_BYTES)
|
|
if cached not in seen:
|
|
total += len(data)
|
|
seen.add(cached)
|
|
self._total_limit(total)
|
|
self._verify(path, data)
|
|
if not cached.exists():
|
|
_atomic_write(cached, data)
|
|
|
|
def to_target(self, *, cache_dir=None):
|
|
"""Resolve verified data locally; missing remote files require explicit fetch."""
|
|
cache_dir = cache_directory() if cache_dir is None else cache_dir
|
|
prompts, annotations = {}, {}
|
|
total = sum(map(len, self.files.values()))
|
|
seen = set()
|
|
for path, entry in self.entries.items():
|
|
if "remote" in entry:
|
|
cached = self._cached(path, cache_dir)
|
|
if not cached.exists():
|
|
raise PackError(
|
|
f"{path}: remote data is unfetched; run concepts fetch explicitly",
|
|
)
|
|
data = read_file(cached)
|
|
if cached not in seen:
|
|
total += len(data)
|
|
seen.add(cached)
|
|
self._total_limit(total)
|
|
else:
|
|
data = self.files[path]
|
|
records = self._verify(path, data)
|
|
if path.endswith(".jsonl"):
|
|
prompts[path] = [record["text"] for record in records]
|
|
kind, _, name = path.removesuffix(".jsonl").partition("/")
|
|
key = f"{kind.removesuffix('s')}:{name}" if name else kind
|
|
annotations[key] = deepcopy(records)
|
|
manifest = self.manifest
|
|
ident = manifest["id"]
|
|
if manifest["tier"] == "user" and not ident.startswith("user/"):
|
|
ident = "user/" + ident
|
|
recipe = ExtractionRecipe(**deepcopy(manifest["recipe"]))
|
|
target = ConceptTarget(
|
|
id=ident, version=manifest["version"], tier=manifest["tier"],
|
|
positive=prompts["positive.jsonl"],
|
|
controls={entry["name"]: prompts[f"controls/{entry['name']}.jsonl"]
|
|
for entry in manifest["sets"]["controls"]},
|
|
neighbours={entry["name"]: prompts[f"neighbours/{entry['name']}.jsonl"]
|
|
for entry in manifest["sets"].get("neighbours", [])},
|
|
battery={entry["name"]: prompts[f"battery/{entry['name']}.jsonl"]
|
|
for entry in manifest["sets"].get("battery", [])},
|
|
records=annotations, recipe=recipe,
|
|
thresholds=deepcopy(manifest.get("validation_thresholds", {})),
|
|
caveats=list(manifest.get("caveats", [])), pack_hash=self.sha256,
|
|
)
|
|
if target.id == "refusal":
|
|
from dataclasses import replace
|
|
from obliteratus.concepts.registry import refusal_target
|
|
legacy = refusal_target()
|
|
if replace(target, pack_hash=None) != legacy:
|
|
raise PackError("A core refusal pack must equal the built-in refusal target")
|
|
return replace(legacy, records=annotations)
|
|
target.pack_provenance.update(self.export_reference())
|
|
return target
|
|
|
|
def _total_limit(self, total):
|
|
limit = 5 * 1024 * 1024 if self.manifest["tier"] == "core" else MAX_PACK_BYTES
|
|
if total > limit:
|
|
raise PackError("Pack including remote files exceeds total size limit")
|
|
|
|
def write(self, destination):
|
|
data = zip_bytes(self.files)
|
|
_atomic_write(destination, data)
|
|
return digest(data)
|
|
|
|
def export_reference(self):
|
|
"""Describe source data without copying prompts or the pack manifest."""
|
|
licences = {entry["path"]: entry for entry in self.manifest["licence"]}
|
|
return {
|
|
"id": self.manifest["id"], "version": self.manifest["version"],
|
|
"sha256": self.sha256, "tier": self.manifest["tier"],
|
|
"references": [
|
|
{
|
|
"path": path, "sha256": entry["sha256"],
|
|
"source": entry.get("remote", {}).get("repo", licences[path]["source"]),
|
|
"commit": entry.get("remote", {}).get(
|
|
"commit", self.manifest.get("provenance", {}).get("source_commit"),
|
|
),
|
|
"spdx": licences[path]["spdx"],
|
|
} for path, entry in sorted(self.entries.items())
|
|
],
|
|
}
|
|
|
|
|
|
def builtin_refusal_pack():
|
|
"""Generate the core pack from the legacy prompts and recipe, byte for byte."""
|
|
import json
|
|
from obliteratus.concepts.registry import refusal_target
|
|
target = refusal_target()
|
|
files = {}
|
|
for path, prompts in (("positive.jsonl", target.positive),
|
|
("controls/harmless.jsonl", target.controls["harmless"])):
|
|
files[path] = ("\n".join(json.dumps({"id": str(i), "text": text}, ensure_ascii=False)
|
|
for i, text in enumerate(prompts)) + "\n").encode()
|
|
files["DATASET_CARD.md"] = (
|
|
"# Refusal\n\nThe existing OBLITERATUS harmful and harmless prompt sets.\n"
|
|
"Intended use: reproduce the legacy refusal direction.\n"
|
|
"Confounds: prompt topic and wording. This does not measure experience.\n"
|
|
"Source: https://github.com/elder-plinius/OBLITERATUS (AGPL-3.0-or-later).\n"
|
|
).encode()
|
|
descriptor = lambda path: {"path": path, "sha256": digest(files[path])}
|
|
legacy_recipe = target.recipe.to_dict()
|
|
legacy_recipe.pop("use_chat_template")
|
|
legacy_recipe.pop("collateral_margin")
|
|
manifest = {
|
|
"schema_version": "1.0", "id": "refusal", "version": target.version,
|
|
"title": "Refusal", "tier": "core", "intended_use": "Legacy refusal direction",
|
|
"sets": {
|
|
"positive": {**descriptor("positive.jsonl"), "n": len(target.positive)},
|
|
"controls": [{**descriptor("controls/harmless.jsonl"), "name": "harmless",
|
|
"n": len(target.controls["harmless"])}],
|
|
},
|
|
"recipe": legacy_recipe, "files": [descriptor("DATASET_CARD.md")],
|
|
"licence": [{"path": path, "spdx": "AGPL-3.0-or-later",
|
|
"source": "https://github.com/elder-plinius/OBLITERATUS",
|
|
"attribution": "OBLITERATUS contributors"} for path in sorted(files)],
|
|
}
|
|
files["manifest.yaml"] = yaml.safe_dump(manifest, sort_keys=True).encode()
|
|
return ConceptPack(files)
|
|
|
|
|
|
def import_pack(source, *, sha256=None, install_dir=None, fetcher=https_fetch):
|
|
"""Verify an explicit import, then install a namespaced, data-only user zip."""
|
|
if sha256 is not None and not re.fullmatch(r"[a-fA-F0-9]{64}", sha256):
|
|
raise PackError("--sha256 must be a 64-hex digest")
|
|
if str(source).startswith(("https://", "http://")):
|
|
if sha256 is None:
|
|
raise PackError("URL import requires --sha256")
|
|
_https_url(str(source))
|
|
data = fetcher(str(source), MAX_ARCHIVE_BYTES)
|
|
if len(data) > MAX_ARCHIVE_BYTES or digest(data) != sha256.lower():
|
|
raise PackError("Imported archive SHA-256 mismatch or size limit exceeded")
|
|
pack = ConceptPack.load(data)
|
|
else:
|
|
path = Path(source)
|
|
if not path.is_dir():
|
|
data = read_file(path, MAX_ARCHIVE_BYTES)
|
|
if sha256 is not None and digest(data) != sha256.lower():
|
|
raise PackError("Imported archive SHA-256 mismatch")
|
|
try:
|
|
pack = ConceptPack.load(data)
|
|
except PackError as exc:
|
|
raise PackError(f"Cannot read concept pack {path}: {exc}") from exc
|
|
else:
|
|
pack = ConceptPack.load(path)
|
|
if sha256 is not None and path.is_dir() and pack.sha256 != sha256.lower():
|
|
raise PackError("Imported directory SHA-256 mismatch")
|
|
manifest = deepcopy(pack.manifest)
|
|
if manifest["id"] == "refusal":
|
|
raise PackError("Built-in id collision: use the explicit namespace user/refusal")
|
|
manifest["id"] = "user/" + manifest["id"].removeprefix("user/")
|
|
manifest["tier"] = "user"
|
|
files = dict(pack.files)
|
|
files["manifest.yaml"] = yaml.safe_dump(manifest, sort_keys=True).encode()
|
|
pack = ConceptPack(files)
|
|
install_dir = cache_directory() / "packs" if install_dir is None else Path(install_dir)
|
|
destination = install_dir / (manifest["id"].removeprefix("user/") + ".cvpack")
|
|
try:
|
|
_atomic_write(destination, zip_bytes(pack.files), exclusive=True)
|
|
except FileExistsError as exc:
|
|
raise PackError(f"Concept already installed: {manifest['id']}") from exc
|
|
return pack
|