feat(semantics): add portable annotation, learning and review contracts
Add portable corpus, annotation provenance, admission, review and bounded agent-cycle contracts. Preserve dataset ownership and explicit research acceptance limits.
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
"""Real immutable admission/corpus binding for the agent's TRAIN boundary."""
|
||||
|
||||
import copy
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from test_semantic_admission import (
|
||||
admission as admission_fixture,
|
||||
)
|
||||
from test_semantic_admission import (
|
||||
corpus as corpus_fixture,
|
||||
)
|
||||
from test_semantic_admission import (
|
||||
dataset,
|
||||
decision,
|
||||
release,
|
||||
)
|
||||
from test_semantic_corpus import group, opened, plan, refs
|
||||
from test_semantic_delivery import ds
|
||||
|
||||
admission = admission_fixture
|
||||
corpus = corpus_fixture
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def regions(admission, monkeypatch):
|
||||
monkeypatch.syspath_prepend(str(Path(__file__).parents[1] / "simulation/semantic-learning"))
|
||||
monkeypatch.setitem(__import__("sys").modules, "admission_store", admission)
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"agent_binding_regions",
|
||||
Path(__file__).parents[1] / "simulation/semantic-learning/region_coverage.py",
|
||||
)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
@pytest.mark.parametrize("split", ["train", "regression"])
|
||||
def test_pinned_physical_split_cannot_be_relabeled(regions, admission, corpus, tmp_path, split):
|
||||
source = dataset(tmp_path, "source")
|
||||
receipt = corpus.build(
|
||||
tmp_path, plan(corpus, [source], [group("physical", split, "a" * 64)]), "corpora/v1"
|
||||
)
|
||||
with opened(corpus, tmp_path, receipt) as reader:
|
||||
ref = refs(reader, split=split)[0]
|
||||
d = decision(admission, ref)
|
||||
ptr, _ = release(admission, tmp_path, receipt, [ref], [d])
|
||||
with admission.Supervision(tmp_path, ptr["path"], ptr["sha256"]) as reader:
|
||||
e = next(e for b in reader.batches("observed_paint", split) for e in b)
|
||||
v, t = e["inputs"][0], e["targets"][0]
|
||||
protected = bytearray(48)
|
||||
protected[18:21] = bytes([1]) * 3
|
||||
payload = dict(
|
||||
supervision=ptr,
|
||||
sample_id=e["sample_id"],
|
||||
view="top",
|
||||
size=[8, 6],
|
||||
roi=[0, 0, 8, 6],
|
||||
target_sha256=regions.sha(admission.pixel_target(t)),
|
||||
protected_sha256=regions.sha(protected),
|
||||
)
|
||||
files = [
|
||||
tmp_path / ref["dataset"] / v["rgb"]["name"],
|
||||
tmp_path / ref["dataset"] / v["cameras_member"]["name"],
|
||||
reader.root / "supervision.json",
|
||||
reader.root / reader.manifest["records"][e["sample_id"]]["path"],
|
||||
]
|
||||
unit = dict(
|
||||
id="roi",
|
||||
split="train",
|
||||
physical_group="physical",
|
||||
payload=payload,
|
||||
sources=[
|
||||
dict(
|
||||
path=f.relative_to(tmp_path).as_posix(),
|
||||
sha256=ds.checksum(f),
|
||||
bytes=f.stat().st_size,
|
||||
)
|
||||
for f in files
|
||||
],
|
||||
)
|
||||
if split == "regression":
|
||||
with pytest.raises(ValueError, match="physical split"):
|
||||
regions.validate_packet(tmp_path, unit)
|
||||
else:
|
||||
regions.validate_packet(tmp_path, unit)
|
||||
bad = copy.deepcopy(unit)
|
||||
bad["physical_group"] = "wrong street"
|
||||
with pytest.raises(ValueError, match="physical split"):
|
||||
regions.validate_packet(tmp_path, bad)
|
||||
bad = copy.deepcopy(unit)
|
||||
bad["payload"]["protected_sha256"] = regions.sha(b"")
|
||||
with pytest.raises(ValueError, match="protected unknown"):
|
||||
regions.validate_packet(tmp_path, bad)
|
||||
|
||||
|
||||
def test_root_accepted_publication_preserves_labels_and_replays(
|
||||
regions, admission, corpus, tmp_path, monkeypatch
|
||||
):
|
||||
source = dataset(tmp_path, "source")
|
||||
receipt = corpus.build(
|
||||
tmp_path, plan(corpus, [source], [group("physical", "train", "a" * 64)]), "corpora/v1"
|
||||
)
|
||||
with opened(corpus, tmp_path, receipt) as reader:
|
||||
ref = refs(reader)[0]
|
||||
d = decision(admission, ref)
|
||||
ptr, _ = release(admission, tmp_path, receipt, [ref], [d])
|
||||
with admission.Supervision(tmp_path, ptr["path"], ptr["sha256"]) as reader:
|
||||
e = next(e for b in reader.training_batches("observed_paint") for e in b)
|
||||
before = admission.pixel_target(e["targets"][0])
|
||||
protected = bytearray(48)
|
||||
protected[18:21] = bytes([1]) * 3
|
||||
payload = dict(
|
||||
supervision=ptr,
|
||||
sample_id=e["sample_id"],
|
||||
view="top",
|
||||
size=[8, 6],
|
||||
roi=[0, 0, 8, 2],
|
||||
target_sha256=regions.sha(before),
|
||||
protected_sha256=regions.sha(protected),
|
||||
)
|
||||
unit = dict(id="roi", physical_group="physical", split="train", payload=payload)
|
||||
proposal = dict(
|
||||
reason="synthetic stripe",
|
||||
background_reviewed=True,
|
||||
boundary_ignore_px=1,
|
||||
paint_polygons=[[[1, 0], [6, 0], [6, 2], [1, 2]]],
|
||||
unknown_polygons=[],
|
||||
)
|
||||
state = dict(
|
||||
state="root_pending",
|
||||
plan=dict(root_actor="root", units=[unit, dict(unit, id="quarantined")]),
|
||||
root_review=None,
|
||||
tasks=[
|
||||
dict(unit="roi", stage="annotate", result=proposal),
|
||||
dict(unit="quarantined", stage="annotate", result=proposal),
|
||||
],
|
||||
)
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"tested_agent_admission",
|
||||
Path(__file__).parents[1] / "simulation/semantic-learning/agent_admission.py",
|
||||
)
|
||||
pub = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(pub)
|
||||
|
||||
class FakeCycle:
|
||||
def __init__(self, *args):
|
||||
pass
|
||||
|
||||
def status(self, identity):
|
||||
return state
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
monkeypatch.setattr(pub, "Cycle", FakeCycle)
|
||||
with pytest.raises(ValueError, match="Root acceptance"):
|
||||
pub.publish(tmp_path, "c" * 64, "supervision/agent")
|
||||
state.update(
|
||||
state="closed",
|
||||
root_review=dict(verdict="accepted", admitted_units=["roi"], reason="synthetic test root"),
|
||||
)
|
||||
out = pub.publish(tmp_path, "c" * 64, "supervision/agent")
|
||||
assert pub.publish(tmp_path, "c" * 64, "supervision/agent") == out
|
||||
assert ds.checksum(tmp_path / ptr["path"] / "supervision.json") == ptr["sha256"]
|
||||
with admission.Supervision(tmp_path, out["path"], out["sha256"]) as reader:
|
||||
new = next(e for b in reader.training_batches("observed_paint") for e in b)
|
||||
after = admission.pixel_target(new["targets"][0])
|
||||
assert all(after[i] == value for i, value in enumerate(before) if value != 255)
|
||||
assert after[2:5] == bytes([1, 1, 1])
|
||||
assert after[18:21] == bytes([255, 255, 255])
|
||||
assert all(y < 3 for y, _, _ in new["targets"][0]["unknown"])
|
||||
assert new["physical_group"] == "physical" and new["split"] == "train"
|
||||
|
||||
# A later explicit source uncertainty cannot silently retain baseline paint.
|
||||
proposal["unknown_polygons"] = [[[2, 1], [3, 1], [3, 2], [2, 2]]]
|
||||
proposal["unknown_reason"] = "New source review finds ambiguity"
|
||||
with pytest.raises(ValueError, match="Explicit source uncertainty"):
|
||||
pub.publish(tmp_path, "d" * 64, "supervision/conflicting-uncertainty")
|
||||
assert not (tmp_path / "supervision/conflicting-uncertainty").exists()
|
||||
assert not (tmp_path / "agent-reviews" / ("d" * 64)).exists()
|
||||
Reference in New Issue
Block a user