Files
NODEDC_MISSION_CORE/tests/test_progressive_perception.py

156 lines
7.0 KiB
Python

"""Fresh front/side captures must remain causal across slow neural passes."""
import copy
import importlib
from pathlib import Path
from types import SimpleNamespace
import pytest
@pytest.fixture
def modules(monkeypatch):
monkeypatch.syspath_prepend(str(Path(__file__).parents[1] / "simulation/ai-polygon"))
return (importlib.import_module("continuous_composition"),
importlib.import_module("continuous_pipeline"))
def observation(stamp, x):
return dict(captured_at=stamp, pose=[x, 0, 0, 0, 0, 0, 1],
simulation_time_ns=int(stamp*1e9), lens_depths=[x], calibration={})
def test_front_is_published_before_supplement_with_distinct_capture_identity(modules, monkeypatch):
composition, _ = modules
model = object.__new__(composition.ContinuousComposition)
model.recovery_view_request = dict(key="recovery-1-2-90", yaw_degrees=90, after_seconds=9.)
model.supplement_yaw = None
model.last_supplement = -100.
model.recovery_detection_view = None
old, fresh = observation(10., 1.), observation(10.6, 2.)
now = [10.]
monkeypatch.setattr(composition.time, "monotonic", lambda: now[0])
published = []
def infer(obs, frame, yaw, **kwargs):
if yaw:
assert len(published) == 1
assert obs is fresh and frame == 106
assert "recovery_view" not in published[0]
now[0] += .6
return dict(observation=obs, frame_id=frame, samples=dict(frame_id=f"{frame}:{yaw}"),
values={}, completed_at=now[0])
model.perceive_view = infer
def publish(front):
published.append(front)
return True
result = model.perceive(old, 100, publish_front=publish, latest_sensor=lambda: (106, fresh))
early = published[0]
assert len(early["semantic_observations"]) == len(early["detection_views"]) == 1
assert "supplement" not in early and "recovery_view" not in early
assert result["observation"] is old
assert result["supplement"]["observation"] is fresh
assert result["recovery_observation"] is fresh
assert result["semantic_observations"] == [early["samples"], dict(frame_id="106:90")]
assert [v["observation"]["captured_at"] for v in result["detection_views"]] == [10., 10.6]
assert result["recovery_view"] == model.recovery_view_request
# Publishing completion must not advance either capture timestamp.
assert now[0] == pytest.approx(11.2)
assert [old["captured_at"], fresh["captured_at"]] == [10., 10.6]
def test_pause_fences_inflight_front_and_does_not_start_side_inference(modules, tmp_path):
composition, pipeline_module = modules
pipeline = pipeline_module.ContinuousPipeline(None, tmp_path)
pipeline.enable(True)
epoch = pipeline.epoch
obs = observation(10., 1.)
pipeline.submit(None, 1, 10., 10_000_000_000, obs)
assert pipeline.latest_sensor(epoch) == (1, obs)
model = object.__new__(composition.ContinuousComposition)
model.recovery_view_request = dict(key="side", yaw_degrees=90)
model.supplement_yaw = None
model.last_supplement = -100.
model.recovery_detection_view = None
calls = []
def infer(observation, frame, yaw):
calls.append(yaw)
pipeline.enable(False)
return dict(observation=observation, samples={}, values={})
model.perceive_view = infer
model.perceive(obs, 1, publish_front=lambda value: pipeline.publish_perception(epoch, value),
latest_sensor=lambda: pipeline.latest_sensor(epoch))
assert calls == [0] and pipeline.perception is None
assert pipeline.latest_sensor(epoch) is None
pipeline.enable(True)
assert not pipeline.publish_perception(epoch, {})
assert pipeline.perception is None
def test_recovery_acknowledges_the_side_capture_pose_and_time(modules):
composition, _ = modules
model = object.__new__(composition.ContinuousComposition)
front, side, live = observation(10., 1.), observation(10.6, 2.), observation(11., 3.)
calls = []
model.mission = SimpleNamespace(recovery=SimpleNamespace(active=False,
observe_view=lambda *args: calls.append(args)), orientation_target=lambda: None)
model.source = SimpleNamespace(lens_depths=None)
model.static_values = {}
model.inputs = {}
model.goal = None
model.observed_goals = SimpleNamespace(evidence={})
model.graph = SimpleNamespace(sha256="graph", nodes=[SimpleNamespace(
module=SimpleNamespace(module_id="nav", group="motion"))])
model.navigation_evidence = {}
model.plan_mission = lambda values: {}
submitted = []
def navigate(values):
submitted.append(copy.copy(values))
return {"motion.command": {}, "motion.path": []}
model.navigate = navigate
def view(obs):
return dict(observation=obs, values={"detection.boxes": [], "navigation.surface": []})
request = dict(key="side", yaw_degrees=90)
perception = dict(observation=front, values={}, rgb=None, semantic_observations=[],
detection_views=[view(front), view(side)], recovery_view=request,
recovery_observation=side, module_ms={}, frame_id=100)
live["points"] = []
model.navigate_latest(live, perception, "current")
assert calls == [(request, side["pose"], 10.6)]
assert submitted[0]["source.pose"] == live["pose"]
assert submitted[0]["source.camera.lens-depths"] == front["lens_depths"]
assert [v["lens_depths"] for v in submitted[0]["source.detection-views"]] == [[1.], [2.]]
def test_controller_uses_physics_stamp_but_wall_time_freshness(modules, monkeypatch, tmp_path):
_, module = modules
import numpy as np
pipeline = module.ContinuousPipeline(None, tmp_path, clock=lambda: 100.)
pipeline.enable(True)
pipeline.ready = True
pipeline.model = SimpleNamespace(inputs={"sha256": "inputs"})
pipeline.await_model = lambda: True
pipeline.observe_pose([1, 0, 0, 0, 0, 0, 1], .4, 99.95,
measured_twist=(.4, 0, 0), simulation_time_ns=25_000_000_000)
pipeline.perception = (pipeline.epoch, {"observation": {"captured_at": 99.6}})
pipeline.sensor = (pipeline.epoch, 1, {"captured_at": 99.9, "points": np.zeros((0, 3))})
pipeline.result = {"observation_id": "live", "captured_at": 99.8,
"decision": {"reason": "road"}}
sent = []
class Connection:
def close(self): pass
def request(self, path, payload):
sent.append((path, payload))
pipeline.closed = True
return dict(reason="none", observation_id="live", speed_mps=.4, yaw_rate_rps=0)
monkeypatch.setattr(module, "NavigationClient", Connection)
pipeline.control_loop()
path, payload = sent[0]
assert path == "/control"
assert payload["pose_simulation_time_ns"] == 25_000_000_000
assert payload["pose_age_seconds"] == pytest.approx(.05)
assert payload["perception_age_seconds"] == pytest.approx(.4)
assert payload["measured_twist"] == [.4, 0, 0]
import json
record = json.loads((tmp_path / "controller.jsonl").read_text())
assert record["expires_at"] == pytest.approx(100.25)
assert record["freshness_deadlines"]["range"] == pytest.approx(100.7)