mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Add optimization O07: turn dossiers, causal maps and labeled features into a versioned queue of specific requests for new examples, plus a study comparing queue-driven acquisition with random acquisition. - Gap detectors: unstable boundaries across group-disjoint folds, prediction/behaviour disagreement, sparse neighbourhoods, missing strata, confounded controls and unsupported causal-map cells. - Each item carries the gap, digest-checked evidence references, rationale, an operator-provided source requirement (the queue never fetches), required partition and group lineage, a declared-heuristic expected value and priority. - Test rows, protected roles, registered confirmation rows and their independence units are removed before scoring; anchors there are refused. Diversity caps per unit and neighbourhood plus a seeded exploration quota prevent repeated outlier targeting. - Ingest writes a new write-once revision bound to the queue and its parent digests; the parent file is never modified. - The study reports random acquisition at equal budget, a stratified arm and the queue on a hidden held-out set; verdict is "no improvement" unless the queue's seed interval clears random's. `obliteratus experiments acquisition queue|ingest|study`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
188 lines
9.4 KiB
Python
188 lines
9.4 KiB
Python
"""O07 ingest (new revisions, partition refusals) and dossier/causal-map gap sources."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import shutil
|
|
|
|
import pytest
|
|
|
|
from obliteratus.analysis import acquisition_revisions as revs
|
|
from obliteratus.analysis.acquisition_queue import build_queue, verify_queue
|
|
from obliteratus.analysis.target_dossier import write_dossier
|
|
from tests.test_acquisition_queue import expose, make_dataset
|
|
from tests.test_target_dossier import REPORT, _build, runs # noqa: F401 (module fixture)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def _sha(path):
|
|
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
|
|
|
|
@pytest.fixture
|
|
def stored(tmp_path):
|
|
rev, feat = make_dataset(tmp_path, outliers=4)
|
|
seed = json.loads(rev.read_text())
|
|
store = tmp_path / "store"
|
|
path = revs.write_revision(store, seed["declaration"], seed["records"])
|
|
expose(tmp_path / "run", ["controls/neutral.jsonl sample 3"])
|
|
queue = build_queue(path, features=feat, registry_dirs=[str(tmp_path / "run")],
|
|
config={"budget": 6})
|
|
relative = next(i for i in queue["items"] if i["partition"]["lineage"] == "relative")
|
|
fresh = next(i for i in queue["items"] if i["partition"]["lineage"] == "new_group")
|
|
return {"root": tmp_path, "store": store, "path": path, "queue": queue,
|
|
"relative": relative, "fresh": fresh}
|
|
|
|
|
|
def _fixture(s, answers, **changes):
|
|
document = {"schema": revs.FIXTURE_SCHEMA, "queue_sha256": s["queue"]["queue_sha256"],
|
|
"provider": "local operator fixture", "permission": "authored for this test",
|
|
"rows": answers, **changes}
|
|
path = s["root"] / f"fixture-{len(list(s['root'].glob('fixture-*')))}.json"
|
|
path.write_text(json.dumps(document))
|
|
return path
|
|
|
|
|
|
def _row(item, ident, text, **record):
|
|
return {"request": item["id"], "path": item["target"]["paths"][0],
|
|
"record": {"id": ident, "text": text, **record}}
|
|
|
|
|
|
def _ingest(s, fixture, path=None):
|
|
return revs.ingest(s["store"], path or s["path"], s["queue"], s["queue"]["queue_sha256"],
|
|
fixture)
|
|
|
|
|
|
def test_ingest_writes_a_new_revision_and_leaves_the_prior_byte_identical(stored):
|
|
s = stored
|
|
before = s["path"].read_bytes()
|
|
fixture = _fixture(s, [_row(s["relative"], "new1", "operator relative one"),
|
|
_row(s["fresh"], "new2", "operator fresh two")])
|
|
result = _ingest(s, fixture)
|
|
assert s["path"].read_bytes() == before
|
|
document, dataset, _ = revs.load_revision(result["path"])
|
|
old, old_dataset, old_sha = revs.load_revision(s["path"])
|
|
assert result["revision"] == dataset.revision != old_dataset.revision
|
|
assert document["parent"] == {"revision": old_dataset.revision, "file_sha256": old_sha,
|
|
"path": s["path"].relative_to(s["store"]).as_posix()}
|
|
assert document["ingest"]["fixture_sha256"] == _sha(fixture)
|
|
table = revs.rows(document)
|
|
anchor = s["relative"]["partition"]["anchor_record"]
|
|
assert table["new1"][2]["role"] == table[anchor][2]["role"] == "fit"
|
|
assert dataset.unit("new1") == dataset.unit(anchor)
|
|
assert table["new2"][2]["group_id"] == s["fresh"]["partition"]["group_id"]
|
|
assert [a["record_id"] for a in result["added"]] == ["new1", "new2"]
|
|
assert len(list((s["store"] / "revisions").glob("*.json"))) == 2
|
|
seed = json.loads(s["path"].read_text())
|
|
assert revs.write_revision(s["store"], seed["declaration"], seed["records"]) == s["path"]
|
|
with pytest.raises(revs.AcquisitionRefused, match="another dataset revision"):
|
|
_ingest(s, fixture, result["path"])
|
|
|
|
|
|
@pytest.mark.parametrize("change, match", [
|
|
({"role": "test"}, "never enter"),
|
|
({"role": "select"}, "must be placed"),
|
|
({"group_id": "g-p3"}, "must be placed"),
|
|
({"text": "positive.jsonl sample 20"}, "break the dataset"),
|
|
({"text": "controls/neutral.jsonl sample 3"}, "confirmation text"),
|
|
({"path": "battery/x.jsonl"}, "not requested"),
|
|
({"request": "sha256:unknown"}, "unknown request"),
|
|
])
|
|
def test_ingest_refuses_rows_outside_their_partition_or_into_confirmation(stored, change, match):
|
|
s = stored
|
|
row = _row(s["relative"], "bad", "operator row")
|
|
for key in ("path", "request"):
|
|
if key in change:
|
|
row[key] = change.pop(key)
|
|
row["record"].update(change)
|
|
with pytest.raises(revs.AcquisitionRefused, match=match):
|
|
_ingest(s, _fixture(s, [row]))
|
|
assert len(list((s["store"] / "revisions").glob("*.json"))) == 1
|
|
|
|
|
|
def test_ingest_refuses_unbound_fixtures_and_tampered_revisions(stored):
|
|
s = stored
|
|
rows = [_row(s["fresh"], "x1", "operator row")]
|
|
for changes, match in (({"queue_sha256": "0" * 64}, "queue_sha256"),
|
|
({"permission": ""}, "permission"), ({"rows": []}, "no rows")):
|
|
with pytest.raises(revs.AcquisitionRefused, match=match):
|
|
_ingest(s, _fixture(s, rows, **changes))
|
|
tampered = json.loads(s["path"].read_text())
|
|
tampered["digests"]["revision"] = "sha256:" + "1" * 64
|
|
(s["root"] / "t.json").write_text(json.dumps(tampered))
|
|
with pytest.raises(ValueError, match="recorded digests"):
|
|
revs.load_revision(s["root"] / "t.json")
|
|
with pytest.raises(ValueError, match="not a dataset revision"):
|
|
(s["root"] / "n.json").write_text("{}")
|
|
revs.load_revision(s["root"] / "n.json")
|
|
s["path"].write_bytes(s["path"].read_bytes() + b" ")
|
|
with pytest.raises(revs.AcquisitionRefused, match="other content"):
|
|
revs.write_revision(s["store"], tampered["declaration"], tampered["records"])
|
|
|
|
|
|
def _planted_revision(pack, out):
|
|
records = {path: [json.loads(line) for line in pack.files[path].decode().splitlines()]
|
|
for path in pack.files if path.endswith(".jsonl")}
|
|
path = out / "planted-rev.json"
|
|
path.write_text(json.dumps({"declaration": pack.manifest["dataset"], "records": records}))
|
|
return path
|
|
|
|
|
|
def test_dossier_confounds_counterexamples_and_map_failures_become_requests(runs, tmp_path): # noqa: F811
|
|
run = tmp_path / "run"
|
|
shutil.copytree(runs["planted"], run)
|
|
dossier = write_dossier(_build(run, confound_resolutions=[]), run)
|
|
rev = _planted_revision(runs["pack"], tmp_path)
|
|
cells = [{"id": "c0", "role": "decision", "status": "inconclusive"},
|
|
{"id": "c1", "role": "decision", "status": "supported"},
|
|
{"id": "c2", "role": "exploratory", "status": "inconclusive"},
|
|
{"id": "c3", "role": "decision", "status": "unsupported"}]
|
|
(tmp_path / "map.json").write_text(json.dumps({
|
|
"schema": "obliteratus.causal-map", "estimate": {"id": "channel:layer.0:2"},
|
|
"cells": cells}))
|
|
queue = build_queue(rev, base=tmp_path, dossiers=[dossier], maps=[tmp_path / "map.json"],
|
|
config={"budget": 40, "explore_fraction": 0,
|
|
"negative_prefixes": ["controls/", "neighbours/"]})
|
|
types = queue["candidates"]["by_type"]
|
|
assert types["confounded_controls"] >= 1 and types["intervention_failure"] == 2
|
|
failures = [i for i in queue["items"] if i["gap"]["type"] == "intervention_failure"]
|
|
assert {i["evidence"][0]["pointer"] for i in failures} == {"/cells/0", "/cells/3"}
|
|
assert all(i["partition"]["lineage"] == "new_group" for i in failures)
|
|
confound = next(i for i in queue["items"] if i["gap"]["type"] == "confounded_controls")
|
|
assert confound["target"]["class"] == "negative"
|
|
assert any(r["path"].endswith(REPORT.split("/")[-1]) for r in confound["evidence"])
|
|
assert verify_queue(queue, tmp_path)["verified"]
|
|
for refused in queue["refused"]:
|
|
assert refused["reason"]
|
|
(tmp_path / "bad.json").write_text(json.dumps({"schema": "other"}))
|
|
with pytest.raises(ValueError, match="causal targeting map"):
|
|
build_queue(rev, base=tmp_path, maps=[tmp_path / "bad.json"])
|
|
(run / REPORT).write_bytes((run / REPORT).read_bytes() + b"\n")
|
|
stale = build_queue(rev, base=tmp_path, dossiers=[dossier], config={"budget": 4})
|
|
assert stale["skipped_sources"][0]["reason"].startswith("dossier is stale")
|
|
|
|
|
|
def test_dossier_counterexamples_follow_partition_and_protection_rules(runs, tmp_path): # noqa: F811
|
|
run = tmp_path / "shuffled"
|
|
shutil.copytree(runs["shuffled"], run)
|
|
dossier = write_dossier(_build(run, estimates=["channel:layer.0:2"], confound_resolutions=[],
|
|
rejected_alternatives=[]), run)
|
|
rev = _planted_revision(runs["pack"], tmp_path)
|
|
options = {"base": tmp_path, "dossiers": [dossier]}
|
|
base = {"budget": 20, "explore_fraction": 0}
|
|
default = build_queue(rev, **options, config=base)
|
|
outside = [r for r in default["refused"] if r["type"] == "prediction_disagreement"]
|
|
assert outside and all("outside the scoring roles" in r["reason"] for r in outside)
|
|
locked = build_queue(rev, **options, config={**base, "protected_roles": ["test", "select"]})
|
|
refused = [r for r in locked["refused"] if r["type"] == "prediction_disagreement"]
|
|
assert len(refused) == len(outside)
|
|
assert all("locked test or confirmation" in r["reason"] for r in refused)
|
|
assert not any(i["target"]["near_record"] for i in locked["items"])
|
|
scored = build_queue(rev, **options, config={**base, "scoring_roles": ["fit", "select"]})
|
|
relatives = [i for i in scored["items"] if i["gap"]["type"] == "prediction_disagreement"]
|
|
assert relatives and all(i["partition"]["role"] == "select"
|
|
and i["partition"]["lineage"] == "relative" for i in relatives)
|
|
assert verify_queue(scored, tmp_path)["verified"]
|