Files
NODEDC_MISSION_CORE/tests/test_boundary_review.py
T
DCCONSTRUCTIONS 25dc0a1b44 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.
2026-10-29 09:10:00 +03:00

175 lines
6.4 KiB
Python

import copy
import importlib.util
import sys
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1] / "simulation" / "semantic-learning"
sys.path.insert(0, str(ROOT))
spec = importlib.util.spec_from_file_location("boundary_review", ROOT / "boundary_review.py")
review = importlib.util.module_from_spec(spec)
spec.loader.exec_module(review)
def fixture():
feature = dict(
id="curb",
properties={"class": "road_boundary"},
geometry=dict(type="LineString", coordinates=[[0.0, 0.0, 1.0], [10.0, 0.0, 1.0]]),
)
old = copy.deepcopy(feature)
old["geometry"]["coordinates"][0][1] = 2.0
labels = dict(
proposed_element=[feature],
original_element=[old],
projections=dict(
top=[dict(feature_id="curb", uv=[[-10.0, 40.0], [110.0, 40.0]], in_front=[True, True])]
),
)
cameras = dict(views=[dict(id="top", projection="ortho", size=[100, 100])])
decision = dict(
id="one",
sample="part-000/tile-0/sample.json",
feature_id="curb",
view="top",
rect_px=[20, 30, 80, 50],
evidence="visible",
reviewer="assistant-visual-review",
reason="Edited contour follows visible curb; original cuts pavement.",
preference="edited-better-than-original",
)
return labels, cameras, decision
def test_exact_ranges_and_no_implicit_background():
labels, cameras, decision = fixture()
before = copy.deepcopy(labels)
result = review.compile_record(labels, cameras, decision)
assert result["segment_ranges"] == [dict(segment=0, t=[0.25, 0.75])]
assert labels == before
assert result["ranking_trial_eligible"]
assert not result["dense_segmentation_eligible"]
assert not result["metric_ground_truth"]
assert result["background_targets"] == result["complete_regions"] == []
@pytest.mark.parametrize("evidence", ["context", "uncertain"])
def test_inferred_or_uncertain_cannot_enter_ranking(evidence):
labels, cameras, decision = fixture()
decision["evidence"] = evidence
with pytest.raises(ValueError, match="visible evidence"):
review.compile_record(labels, cameras, decision)
decision.pop("preference")
assert not review.compile_record(labels, cameras, decision)["ranking_trial_eligible"]
def test_editor_accepted_does_not_admit_training():
labels, cameras, decision = fixture()
labels["proposed_element"][0]["properties"]["review_status"] = "accepted"
decision.pop("preference")
assert not review.compile_record(labels, cameras, decision)["ranking_trial_eligible"]
@pytest.mark.parametrize("change", ["rejected", "marking", "nan", "empty", "perspective", "same"])
def test_invalid_target_fails_closed(change):
labels, cameras, decision = fixture()
if change == "rejected":
labels["proposed_element"][0]["properties"]["review_status"] = "rejected"
elif change == "marking":
labels["proposed_element"][0]["properties"]["class"] = "road_marking"
elif change == "nan":
labels["projections"]["top"][0]["uv"][0][0] = float("nan")
elif change == "empty":
decision["rect_px"] = [20, 60, 80, 90]
elif change == "perspective":
cameras["views"][0]["projection"] = "pinhole"
else:
labels["original_element"] = copy.deepcopy(labels["proposed_element"])
with pytest.raises(ValueError):
review.compile_record(labels, cameras, decision)
def test_control_scene_cannot_leak_into_training():
control = dict(
schema_version="missioncore.boundary-control/v1",
role="held-out-scene",
world_sha256="a" * 64,
model_sha256="b" * 64,
training_eligible=False,
windows=[dict(center=[0, 0, 0])],
frozen_utc="2026-10-21T00:00:00Z",
)
review.check_control(control, "c" * 64)
with pytest.raises(ValueError, match="distinct frozen"):
review.check_control(control, "a" * 64)
def test_clipping_is_direction_invariant():
assert review.clipped_range([-10, 40], [110, 40], [20, 30, 80, 50]) == [0.25, 0.75]
assert review.clipped_range([110, 40], [-10, 40], [20, 30, 80, 50]) == [0.25, 0.75]
assert review.clipped_range([40, -10], [40, 110], [30, 20, 50, 80]) == [0.25, 0.75]
def make_pack(root):
labels, cameras, decision = fixture()
folder = root / "observations/part-000/tile-0"
folder.mkdir(parents=True)
review.write(folder / "labels.json", labels)
review.write(folder / "cameras.json", cameras)
review.write(root / "decisions.json", {"records": [decision]})
review.write(root / "records.json", [review.compile_record(labels, cameras, decision)])
review.write(
root / "control.json",
dict(
schema_version="missioncore.boundary-control/v1",
role="held-out-scene",
world_sha256="a" * 64,
model_sha256="b" * 64,
training_eligible=False,
windows=[{"center": [0, 0, 0]}],
frozen_utc="2026-10-21T00:00:00Z",
),
)
review.write(
root / "pack.json",
dict(
schema_version=review.SCHEMA,
source={"world_sha256": "c" * 64},
files={
p.relative_to(root).as_posix(): dict(
sha256=review.checksum(p), bytes=p.stat().st_size
)
for p in root.rglob("*")
if p.is_file()
},
),
)
def test_pack_is_portable_and_detects_changed_observation(tmp_path):
root = tmp_path / "source"
make_pack(root)
before = review.verify_pack(root)
moved = tmp_path / "elsewhere"
root.rename(moved)
assert review.verify_pack(moved) == before
(moved / "observations/part-000/tile-0/cameras.json").write_text("{}")
with pytest.raises(ValueError, match="Pack member changed"):
review.verify_pack(moved)
def test_pack_recomputes_admission_instead_of_trusting_exported_flags(tmp_path):
make_pack(tmp_path)
records = review.read(tmp_path / "records.json")
records[0]["dense_segmentation_eligible"] = True
review.write(tmp_path / "records.json", records)
manifest = review.read(tmp_path / "pack.json")
manifest["files"]["records.json"] = dict(
sha256=review.checksum(tmp_path / "records.json"),
bytes=(tmp_path / "records.json").stat().st_size,
)
review.write(tmp_path / "pack.json", manifest)
with pytest.raises(ValueError, match="Admission differs"):
review.verify_pack(tmp_path)