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>
195 lines
10 KiB
Python
195 lines
10 KiB
Python
"""O07 acquisition queue: required fields, evidence digests, placement, diversity, exposure."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis import acquisition_revisions as revs
|
|
from obliteratus.analysis.acquisition_queue import (
|
|
build_queue, interleave, load_queue, plan, resolve_config, verify_queue, write_queue,
|
|
)
|
|
from obliteratus.analysis.dossier_refs import SourceChanged
|
|
from obliteratus.experiments.registry_store import Registry, text_sha256
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
DECL = {"dataset_version": "v1", "source_version": "v1", "roles": ["fit", "select", "test"],
|
|
"test_locked": True}
|
|
ITEM_FIELDS = {"id", "priority", "channel", "gap", "evidence", "selection_rationale",
|
|
"source_requirement", "target", "partition", "neighbourhood", "expected_value"}
|
|
|
|
|
|
def make_dataset(root, *, outliers=0, shift=None, seed=0):
|
|
"""24 rows per class (16 fit, 4 select, 4 test), features with a planted axis.
|
|
|
|
``outliers`` adds a dense cluster of fit rows of both classes on the boundary,
|
|
far from the rest. ``shift`` adds a vector to every protected row's features.
|
|
"""
|
|
g = torch.Generator().manual_seed(seed)
|
|
records = {"positive.jsonl": [], "controls/neutral.jsonl": []}
|
|
ids, values = [], []
|
|
for positive, path in ((True, "positive.jsonl"), (False, "controls/neutral.jsonl")):
|
|
for i in range(24):
|
|
role = "fit" if i < 16 else "select" if i < 20 else "test"
|
|
rid = f"{'p' if positive else 'n'}{i}"
|
|
records[path].append({"id": rid, "text": f"{path} sample {i}", "role": role,
|
|
"group_id": f"g-{rid}", "domain": "a" if i % 2 else "b"})
|
|
x = torch.randn(4, generator=g) * 0.7
|
|
x[0] += 2.0 if positive else -2.0
|
|
if shift is not None and (role == "test" or rid in ("p0", "p1", "n0")):
|
|
x += torch.tensor(shift)
|
|
ids.append(rid)
|
|
values.append(x.tolist())
|
|
for k in range(outliers):
|
|
path = "positive.jsonl" if k % 2 else "controls/neutral.jsonl"
|
|
rid = f"o{k}"
|
|
records[path].append({"id": rid, "text": f"outlier {k}", "role": "fit",
|
|
"group_id": f"g-{rid}", "domain": "a" if k % 4 < 2 else "b"})
|
|
ids.append(rid)
|
|
values.append((torch.tensor([0.0, 6.0, 6.0, 0.0])
|
|
+ 0.01 * torch.randn(4, generator=g)).tolist())
|
|
(root / "rev.json").write_text(json.dumps({"declaration": DECL, "records": records}))
|
|
(root / "feat.json").write_text(json.dumps({"schema": "obliteratus.acquisition-features",
|
|
"record_ids": ids, "values": values}))
|
|
return root / "rev.json", root / "feat.json"
|
|
|
|
|
|
def expose(run, texts):
|
|
"""A confirmation registration over ``texts`` with an evaluate event (exposed)."""
|
|
registry = Registry(run)
|
|
frozen = {"target_key": {"target": "t", "method": "m", "model": "x", "task": "y",
|
|
"backend": "b"},
|
|
"amends": None, "parent": None, "hypothesis": None, "recipe": None, "scorers": [],
|
|
"thresholds": None, "seeds": None, "protocol": None,
|
|
"confirmation": {"pack": "p", "pack_sha256": "0", "revision": None, "role": "select",
|
|
"content_digest": None, "partition_digest": None, "tasks": [],
|
|
"n_rows": len(texts), "rows_sha256": "sha256:" + "0" * 64,
|
|
"record_ids_sha256": "sha256:" + "0" * 64,
|
|
"row_digests": sorted(text_sha256(t) for t in texts)}}
|
|
document = registry.write("confirmation", frozen, "tester")
|
|
registry.append("evaluate", document["id"])
|
|
|
|
|
|
def test_items_carry_required_fields_and_evidence_resolves_by_digest(tmp_path):
|
|
rev, feat = make_dataset(tmp_path, outliers=4)
|
|
queue = build_queue(rev, features=feat, config={"budget": 8,
|
|
"expected_strata": {"domain": ["c"]}})
|
|
n = len(queue["items"])
|
|
assert n >= 4 and n + queue["unfilled"] == 8
|
|
assert sorted(i["priority"] for i in queue["items"]) == list(range(1, n + 1))
|
|
for item in queue["items"]:
|
|
assert ITEM_FIELDS <= set(item)
|
|
assert item["evidence"] and all({"path", "sha256", "pointer"} == set(r)
|
|
for r in item["evidence"])
|
|
assert item["source_requirement"]["fetch"].startswith("never")
|
|
assert {"role", "group_id", "lineage"} <= set(item["partition"])
|
|
assert item["expected_value"]["label"] == "declared heuristic"
|
|
assert item["target"]["paths"]
|
|
types = {i["gap"]["type"] for i in queue["items"]}
|
|
assert {"missing_stratum", "random_exploration"} <= types
|
|
assert verify_queue(queue, tmp_path)["evidence_refs"] >= n
|
|
path = write_queue(queue, tmp_path / "out")
|
|
assert load_queue(path) == (queue, queue["queue_sha256"])
|
|
feat.write_text(feat.read_text().replace("0", "1", 1))
|
|
with pytest.raises(SourceChanged):
|
|
verify_queue(queue, tmp_path)
|
|
path.write_text(path.read_text().replace('"budget": 8', '"budget": 9'))
|
|
with pytest.raises(ValueError, match="queue_sha256"):
|
|
load_queue(path)
|
|
|
|
|
|
def test_relatives_join_their_group_partition_and_test_is_refused(tmp_path):
|
|
rev, feat = make_dataset(tmp_path, outliers=4)
|
|
queue = build_queue(rev, features=feat, config={"budget": 6})
|
|
document, dataset, _ = revs.load_revision(rev)
|
|
table = revs.rows(document)
|
|
relatives = [i for i in queue["items"] if i["partition"]["lineage"] == "relative"]
|
|
assert relatives
|
|
for item in relatives:
|
|
anchor = table[item["partition"]["anchor_record"]][2]
|
|
assert item["partition"]["role"] == anchor["role"] == "fit"
|
|
assert item["partition"]["group_id"] == anchor["group_id"]
|
|
for item in queue["items"]:
|
|
assert item["partition"]["role"] not in ("test", "select") or \
|
|
item["partition"]["lineage"] == "relative"
|
|
protected = revs.protected_rows(document, dataset)
|
|
with pytest.raises(revs.AcquisitionRefused, match="locked test"):
|
|
revs.placement(document, dataset, protected, anchor="p20", key="k")
|
|
with pytest.raises(revs.AcquisitionRefused, match="protected role"):
|
|
revs.placement(document, dataset, protected, key="k", new_group_role="test")
|
|
with pytest.raises(revs.AcquisitionRefused, match="not declared"):
|
|
revs.placement(document, dataset, protected, key="k", new_group_role="calibrate")
|
|
with pytest.raises(revs.AcquisitionRefused, match="not in the dataset"):
|
|
revs.placement(document, dataset, protected, anchor="missing", key="k")
|
|
with pytest.raises(revs.AcquisitionRefused):
|
|
resolve_config({"scoring_roles": ["fit", "test"]})
|
|
with pytest.raises(revs.AcquisitionRefused):
|
|
resolve_config({"new_group_role": "select", "protected_roles": ["select"]})
|
|
with pytest.raises(ValueError, match="Unknown"):
|
|
resolve_config({"budgett": 3})
|
|
with pytest.raises(ValueError, match="budget"):
|
|
resolve_config({"budget": 0})
|
|
with pytest.raises(ValueError, match="explore_fraction"):
|
|
resolve_config({"explore_fraction": 2})
|
|
|
|
|
|
def test_diversity_and_exploration_quota_stop_repeated_outlier_targeting(tmp_path):
|
|
rev, feat = make_dataset(tmp_path, outliers=16)
|
|
outliers = {f"o{k}" for k in range(16)}
|
|
|
|
def from_cluster(queue):
|
|
return [i for i in queue["items"] if i["target"]["near_record"] in outliers]
|
|
greedy = build_queue(rev, features=feat, config={
|
|
"budget": 6, "explore_fraction": 0, "max_per_neighbourhood": None})
|
|
assert len(from_cluster(greedy)) >= 5
|
|
balanced = build_queue(rev, features=feat, config={"budget": 6})
|
|
assert len(from_cluster(balanced)) == 1
|
|
assert [i["channel"] for i in balanced["items"]].count("exploration") == 1
|
|
assert any("neighbourhood of" in s["reason"] for s in balanced["suppressed"])
|
|
unit = [{"key": k, "type": "unstable_boundary", "ev": ev, "unit": "u", "x": None,
|
|
"nkey": k} for k, ev in (("a", 0.9), ("b", 0.8))]
|
|
chosen, explore, suppressed = plan(unit, budget=2, explore_fraction=0,
|
|
max_per_neighbourhood=1, radius=0.0)
|
|
assert [c["key"] for c in chosen] == ["a"] and explore == 0
|
|
assert suppressed[0]["reason"] == "independence unit already requested"
|
|
assert interleave([1, 2, 3], ["e"]) == [1, 2, "e", 3]
|
|
|
|
|
|
def test_fixed_seed_is_deterministic_and_seed_only_moves_exploration(tmp_path):
|
|
rev, feat = make_dataset(tmp_path, outliers=4)
|
|
first = build_queue(rev, features=feat, config={"budget": 6, "seed": 3})
|
|
assert build_queue(rev, features=feat, config={"budget": 6, "seed": 3}) == first
|
|
other = build_queue(rev, features=feat, config={"budget": 6, "seed": 4})
|
|
targeted = lambda q: [i["id"] for i in q["items"] if i["channel"] == "uncertainty"] # noqa: E731
|
|
assert targeted(other) == targeted(first)
|
|
assert other["queue_sha256"] != first["queue_sha256"]
|
|
|
|
|
|
def test_test_and_confirmation_rows_never_enter_scoring(tmp_path):
|
|
clean, perturbed = tmp_path / "clean", tmp_path / "perturbed"
|
|
clean.mkdir()
|
|
perturbed.mkdir()
|
|
rev_a, feat_a = make_dataset(clean, outliers=4)
|
|
rev_b, feat_b = make_dataset(perturbed, outliers=4, shift=[0.0, -40.0, 25.0, 9.0])
|
|
texts = ["positive.jsonl sample 0", "positive.jsonl sample 1",
|
|
"controls/neutral.jsonl sample 0"]
|
|
for root in (clean, perturbed):
|
|
expose(root / "run", texts)
|
|
a = build_queue(rev_a, features=feat_a, registry_dirs=[clean / "run"], config={"budget": 8})
|
|
b = build_queue(rev_b, features=feat_b, registry_dirs=[perturbed / "run"],
|
|
config={"budget": 8})
|
|
summary = lambda q: [(i["id"], i["gap"], i["partition"], i["expected_value"]) # noqa: E731
|
|
for i in q["items"]]
|
|
assert summary(a) == summary(b)
|
|
assert a["protected"] == {"protected_rows": 11, "registered_confirmation_rows": 3,
|
|
"exposed_confirmation_rows": 3}
|
|
assert a["excluded_feature_rows"]["protected"] == 11
|
|
assert a["excluded_feature_rows"]["outside_scoring"] == 8
|
|
banned = {"p0", "p1", "n0"} | {f"{c}{i}" for c in "pn" for i in range(20, 24)}
|
|
for item in a["items"]:
|
|
assert item["target"]["near_record"] not in banned
|
|
unprotected = build_queue(rev_b, features=feat_b, config={"budget": 8})
|
|
assert summary(unprotected) != summary(b)
|