droboticflow/tests/backend/test_delivery_v4_consumer.py

245 lines
14 KiB
Python

"""Consumers must classify and locate every interval without platform workspaces."""
import csv
import hashlib
import json
import os
import shutil
import subprocess
import sys
from pathlib import Path
import pytest
from backend.operators.builtin.package import operator as package
from tests.backend.test_delivery_v4_package import ACTION, BASE, NODE, prepare, read_outputs
from tests.backend.test_part_index_package import helpers
from tests.backend.test_slice_discard_package import accepted_inputs
def test_tags_inherit_by_dictionary_selection_not_description_text(tmp_path):
inputs=prepare(tmp_path,[{"description":""},{"description":"人工补充文字"},{"description":"","dictionary":ACTION},{"description":"无效","isInvalid":True}])
doc,_,_=read_outputs(tmp_path,inputs)
actions=doc["slices"][0]["segments"]
assert [a["tags"] for a in actions]==[BASE["tags"],BASE["tags"],ACTION["tags"],BASE["tags"]]
assert [a["tagSource"] for a in actions]==["slice","slice","action","slice"]
assert actions[-1]["isInvalid"] is True
assert "dictionary" not in actions[0] # Inheritance must not invent an action selection.
assert actions[2]["dictionary"]==ACTION
assert doc["consumerContract"]=="droboticflow.classification-consumer/v1"
assert doc["pathBases"]["sources"]=="original_capture"
assert "workspaceKey" not in json.dumps(doc)
@pytest.mark.parametrize("failure,match",[("parent-tags","slice tags"),("action-tags","action tags"),("source","original BOS"),("timestamps","startTimestampUs"),("overlap","overlapping"),("clock","disagree"),("missing-file","missing original files"),("unsafe-key","unsafe"),("boundary","boundary mapping"),("pts-gap","boundary mapping"),("discard-dictionary","discard reason disagrees")])
def test_v4_refuses_incomplete_consumer_instructions(tmp_path,failure,match):
inputs=prepare(tmp_path,[{"description":"one"},{"description":"two","dictionary":dict(ACTION)}])
root=Path(inputs[0]["path"])
manifest_path=root/"revision-manifest.json"
manifest=json.loads(manifest_path.read_text())
sync_path=root/"slices/slice-000001/synchronized-slice.json"
sync=json.loads(sync_path.read_text())
ann_path=root/"slices/slice-000001/annotation.json"
ann=json.loads(ann_path.read_text())
if failure=="parent-tags":sync["dictionary"]["tags"]=[]
elif failure=="action-tags":ann["actions"][1]["dictionary"]["tags"]=[]
elif failure=="source":manifest.pop("originalSource")
elif failure=="missing-file":manifest["originalSource"]["objectKeys"]=[k for k in manifest["originalSource"]["objectKeys"] if "camera_left" not in k]
elif failure=="timestamps":ann["actions"][0].pop("startTimestampUs")
elif failure=="overlap":ann["actions"][1].update(clipStartMs=0,startTimestampUs=1000000)
elif failure=="clock":ann["actions"][0]["endTimestampUs"]-=1
elif failure=="unsafe-key":manifest["originalSource"]["objectKeys"][0]=manifest["originalSource"]["objectKeys"][0].replace("/part0001/", "//part0001/")
elif failure=="boundary":sync["boundaryTimestampUs"][-1]-=1
elif failure=="discard-dictionary":manifest["items"][1]["discardEvidence"]["reasonVersion"]=999
elif failure=="pts-gap":
sync.pop("boundarySeconds");sync.pop("boundaryTimestampUs")
ann["actions"][1]["startTimestampUs"]+=1
sync_path.write_text(json.dumps(sync));ann_path.write_text(json.dumps(ann));manifest_path.write_text(json.dumps(manifest))
inputs[1]["data"]["acceptedMaterializations"][0]["manifestSha256"]=hashlib.sha256(manifest_path.read_bytes()).hexdigest()
with pytest.raises(RuntimeError,match=match):read_outputs(tmp_path,inputs)
@pytest.mark.parametrize("grid", ["absent", "empty", "seconds-only", "timestamps-only", "null", "object"])
@pytest.mark.parametrize("shift", [False, True], ids=["correct-pts", "coherent-wrong-pts"])
def test_v4_requires_complete_time_mapping_at_package_seam(tmp_path, grid, shift):
inputs = prepare(tmp_path, [{"description": "one"}, {"description": "two"}])
root = Path(inputs[0]["path"]) / "slices/slice-000001"
sync_path, ann_path = root / "synchronized-slice.json", root / "annotation.json"
sync, ann = json.loads(sync_path.read_text()), json.loads(ann_path.read_text())
assert ann["actions"][0]["clipEndMs"] == ann["actions"][1]["clipStartMs"] == 500
if shift:
# Preserve continuity and parent endpoints: only the sec<->PTS relation is wrong.
ann["actions"][0]["endTimestampUs"] = ann["actions"][1]["startTimestampUs"] = 1400000
if grid == "absent":
sync.pop("boundarySeconds"); sync.pop("boundaryTimestampUs")
elif grid == "empty":
sync.update(boundarySeconds=[], boundaryTimestampUs=[])
elif grid == "seconds-only":
sync.pop("boundaryTimestampUs")
elif grid == "timestamps-only":
sync.pop("boundarySeconds")
elif grid == "null":
sync.update(boundarySeconds=None, boundaryTimestampUs=None)
else:
sync.update(boundarySeconds={}, boundaryTimestampUs={})
sync_path.write_text(json.dumps(sync)); ann_path.write_text(json.dumps(ann))
with pytest.raises(RuntimeError, match="boundary mapping"):
read_outputs(tmp_path, inputs)
@pytest.mark.parametrize("failure", ["missing", "start", "end", "semantics"])
def test_v4_mapping_must_agree_with_frozen_sync_window(tmp_path, failure):
inputs = prepare(tmp_path, [{"description": "one"}])
path = Path(inputs[0]["path"]) / "slices/slice-000001/synchronized-slice.json"
sync = json.loads(path.read_text())
sync["window"] = {"startTimestampUs": 1000000, "endTimestampUs": 2000000, "semantics": "half-open"}
if failure == "missing": sync.pop("window")
elif failure == "semantics": sync["window"]["semantics"] = "closed"
else: sync["window"][failure + "TimestampUs"] += 1
path.write_text(json.dumps(sync))
with pytest.raises(RuntimeError, match="synchronization window"):
read_outputs(tmp_path, inputs)
@pytest.mark.parametrize("failure", ["missing-blank", "length", "seconds-order", "pts-order", "missing-interior", "ambiguous-interior"])
def test_v4_rejects_unusable_mapping_without_inventing_boundaries(tmp_path, failure):
actions = [] if failure == "missing-blank" else [{"description": "one"}, {"description": "two"}]
inputs = prepare(tmp_path, actions)
path = Path(inputs[0]["path"]) / "slices/slice-000001/synchronized-slice.json"
sync = json.loads(path.read_text())
if failure == "missing-blank":
sync.pop("boundarySeconds"); sync.pop("boundaryTimestampUs")
elif failure == "length": sync["boundarySeconds"].pop()
elif failure == "seconds-order": sync["boundarySeconds"][1] = 0
elif failure == "pts-order": sync["boundaryTimestampUs"][1] = 2000000
elif failure == "missing-interior":
sync["boundarySeconds"].pop(1); sync["boundaryTimestampUs"].pop(1)
else:
sync["boundarySeconds"].insert(2, .50000001)
sync["boundaryTimestampUs"].insert(2, 1500001)
path.write_text(json.dumps(sync))
with pytest.raises(RuntimeError, match="boundary mapping|no original PTS mapping"):
read_outputs(tmp_path, inputs)
@pytest.mark.parametrize("all_discard", [False, True], ids=["mixed", "all-discard"])
@pytest.mark.parametrize("failure", ["missing", "empty", "seconds-only", "timestamps-only", "range", "window", "coherent-range"])
def test_v4_validates_discard_time_evidence_at_package_seam(tmp_path, all_discard, failure):
inputs = prepare(tmp_path, [])
root = Path(inputs[0]["path"])
manifest_path = root / "revision-manifest.json"
manifest = json.loads(manifest_path.read_text())
if all_discard:
manifest["items"][0].update(deliveryDisposition="discard", discardEvidence=manifest["items"][1]["discardEvidence"])
row = manifest["items"][1]
path = root / row["directory"] / "synchronized-slice.json"
sync = json.loads(path.read_text())
assert sync["boundaryTimestampUs"] == [2000000, 2500000, 3000000]
assert sync["boundarySeconds"] == [0, .5, 1]
if failure == "missing":
sync.pop("boundarySeconds"); sync.pop("boundaryTimestampUs")
elif failure == "empty": sync.update(boundarySeconds=[], boundaryTimestampUs=[])
elif failure == "seconds-only": sync.pop("boundaryTimestampUs")
elif failure == "timestamps-only": sync.pop("boundarySeconds")
elif failure == "range": sync["boundaryTimestampUs"][-1] -= 1
elif failure == "window": sync["window"]["endTimestampUs"] -= 1
else:
# Parent and grid agree with each other, but contradict the frozen window.
row["endTimestampUs"] -= 1
sync["boundaryTimestampUs"][-1] -= 1
path.write_text(json.dumps(sync))
manifest_path.write_text(json.dumps(manifest))
inputs[1]["data"]["acceptedMaterializations"][0]["manifestSha256"] = hashlib.sha256(manifest_path.read_bytes()).hexdigest()
with pytest.raises(RuntimeError, match="boundary mapping|synchronization window"):
read_outputs(tmp_path, inputs)
@pytest.mark.parametrize("all_discard", [False, True], ids=["mixed", "all-discard"])
def test_v4_valid_discard_time_evidence_needs_no_slice_tags_or_actions(tmp_path, all_discard):
inputs = prepare(tmp_path, [])
root = Path(inputs[0]["path"])
manifest_path = root / "revision-manifest.json"
manifest = json.loads(manifest_path.read_text())
if all_discard:
manifest["items"][0].update(deliveryDisposition="discard", discardEvidence=manifest["items"][1]["discardEvidence"])
for row in manifest["items"]:
if row["deliveryDisposition"] != "discard": continue
folder = root / row["directory"]
sync_path = folder / "synchronized-slice.json"
sync = json.loads(sync_path.read_text())
sync.pop("dictionary", None)
sync_path.write_text(json.dumps(sync))
(folder / "annotation.json").unlink()
manifest_path.write_text(json.dumps(manifest))
inputs[1]["data"]["acceptedMaterializations"][0]["manifestSha256"] = hashlib.sha256(manifest_path.read_bytes()).hexdigest()
doc, _, _ = read_outputs(tmp_path, inputs)
assert len(doc["discardedSlices"]) == (2 if all_discard else 1)
for item in doc["discardedSlices"]:
assert item["segments"] == []
assert "tags" not in item["classification"]
def test_legacy_partial_annotations_keep_inherited_gap_coverage(tmp_path):
inputs=prepare(tmp_path,[{"description":"拿起","dictionary":ACTION,"clipStartMs":250,"clipEndMs":750}])
doc,_,_=read_outputs(tmp_path,inputs)
actions=doc["slices"][0]["segments"]
assert [(a["startSecond"],a["endSecond"],a["tagSource"]) for a in actions]==[(0,.25,"slice"),(.25,.75,"action"),(.75,1,"slice")]
assert [(a["startTimestampUs"],a["endTimestampUs"]) for a in actions]==[(1000000,1250000),(1250000,1750000),(1750000,2000000)]
def test_discard_aliases_use_frozen_dictionary_without_zero_version(tmp_path):
inputs=prepare(tmp_path,[])
manifest_path=Path(inputs[0]["path"])/"revision-manifest.json"
manifest=json.loads(manifest_path.read_text())
reason=manifest["items"][1]["discardEvidence"]
reason.update(reasonLabelId="",reasonName="",reasonVersion=0)
manifest_path.write_text(json.dumps(manifest))
inputs[1]["data"]["acceptedMaterializations"][0]["manifestSha256"]=hashlib.sha256(manifest_path.read_bytes()).hexdigest()
doc,_,_=read_outputs(tmp_path,inputs)
reason=doc["discardedSlices"][0]["classification"]["discardEvidence"]
assert reason["reasonLabelId"]==reason["dictionary"]["id"]
assert reason["reasonVersion"]==reason["dictionary"]["version"]==4
assert reason["reasonName"]==reason["dictionary"]["name"]
@pytest.mark.parametrize("mode",["physical","source-index"])
def test_real_v4_roundtrip_uses_only_customer_json_and_original_data(tmp_path,monkeypatch,mode):
root=os.environ.get("FANYU_DELIVERY_CONSUMER_FIXTURE_DIR")
if not root:pytest.skip("generate retained local Go source fixture first")
context=json.loads((Path(root)/mode/"context.json").read_text())
artifact=context["artifact"]
inputs=accepted_inputs(artifact)[:2]
inputs[1]["data"].update(trace=[],qualityReviews=[])
inputs[1]["data"]["finalAcceptance"]["acceptedAt"]="2026-10-21T00:00:00Z"
original=Path(context["original"])
for kind in ("camera","imu"):
p=original/f"capture-A_calibration_{kind}.yaml"
inputs.append({"portId":"calibrations","name":p.name,"path":str(p)})
node={**NODE,"hyperparams":NODE["hyperparams"]+[{"key":"SOURCE_INDEX_DELIVERY","value":"true"}]}
with monkeypatch.context() as guard:
def no_subprocess(*args, **kwargs):
raise AssertionError("classification export must not transcode or recut source data")
original_sha = package._sha256_file
def metadata_only(path):
assert Path(path).suffix not in {".mp4", ".mov", ".mkv"}, "classification export rehashed video bodies"
return original_sha(path)
guard.setattr(package.subprocess, "run", no_subprocess)
guard.setattr(package.subprocess, "Popen", no_subprocess)
guard.setattr(package, "_sha256_file", metadata_only)
result=package.run("consumer","package",tmp_path/"out",node,inputs,helpers())
doc=json.loads((Path(result[0]["path"])/"part.json").read_text())
assert not any(word in json.dumps(doc) for word in ("workspaceKey","system/runs/","PRIVATE_CONNECTION"))
# A fresh process can consume copies of only the customer file and original source.
consumer=tmp_path/"downstream";shutil.copytree(original,consumer/"original")
(consumer/"part.json").write_text(json.dumps(doc))
before={p.relative_to(consumer/"original"):hashlib.sha256(p.read_bytes()).hexdigest() for p in (consumer/"original").rglob("*") if p.is_file()}
result=subprocess.run([sys.executable,str(Path(__file__).with_name("consume_delivery_v4.py").resolve()),str(consumer/"part.json"),str(consumer/"original")],cwd=consumer,capture_output=True,text=True,check=True)
assert before=={p.relative_to(consumer/"original"):hashlib.sha256(p.read_bytes()).hexdigest() for p in (consumer/"original").rglob("*") if p.is_file()}
report=json.loads(result.stdout)
assert report["passed"] and report["sourceModified"] is False
assert report["coveredFrames"]=={"left":32,"right":32}
groups=report["actions"]
assert [g["tags"] for g in groups]==[["商品","手部"],["抓取"],["商品","手部"],["商品","手部"]]
assert [g["invalid"] for g in groups]==[False,False,True,False]
assert [g["range"] for g in groups]==[[0,.5],[.5,1],[3,3.5],[3.5,4]]
assert doc["slices"][1]["segments"][0]["startSecond"]==0
assert doc["slices"][1]["segments"][0]["partStartSecond"]==3