"""Bounded, versioned disturbances at the environment/observation boundary. This module has no actuator access. The ordinary watchdog, planner and physical drive must respond to a disturbance themselves. Simulation truth arms the test and measures its result; it is never added to the planner's observations. """ import copy import hashlib import json import math from datetime import UTC, datetime SCENARIOS = ( "observation-loss", "observation-delay", "obstacle", "route-obstacle", "route-obstacle-clearance", "route-obstacle-after-travel", ) def scenario_spec(name): if name not in SCENARIOS: raise ValueError("Unknown qualification scenario") persistent = name in { "route-obstacle", "route-obstacle-clearance", "route-obstacle-after-travel", } document = { "schema_version": "missioncore.simulation-challenge/v1", "name": name, "trigger": { "travel_m": 2.0 if name == "route-obstacle-after-travel" else 0.6, "moving_seconds": 0.5, "minimum_speed_mps": 0.06, }, "duration_seconds": 600.0 if persistent else 8.0 if name == "obstacle" else 4.0, "observation_delay_seconds": 1.2 if name == "observation-delay" else 0.0, "obstacle": { "size_m": [0.3, 1.2 if persistent else 6.0, 0.8], "initial_clearance_m": 1.2 if name in {"route-obstacle-clearance", "route-obstacle-after-travel"} else 0.55, } if name == "obstacle" or persistent else None, "acceptance": { "stop_speed_mps": 0.01, "stop_angular_speed_rps": 0.05, "stationary_seconds": 0.5, "stop_wall_seconds": 7.0 if name == "obstacle" else 1.5, "stop_travel_m": 0.5 if name == "obstacle" else 0.25, "minimum_clearance_m": 0.05, "recovery_travel_m": 0.2, }, } encoded = json.dumps(document, sort_keys=True, separators=(",", ":"), allow_nan=False) return {**document, "sha256": hashlib.sha256(encoded.encode()).hexdigest()} def scenario_from_run(run): qualification = run.get("qualification", {}) document = qualification.get("scenario") if document is None: return None if ( qualification.get("authority") != "virtual-only" or run.get("request", {}).get("purpose") != "evaluation" or document != scenario_spec(document.get("name")) ): raise ValueError("Challenge requires the exact bounded virtual evaluation contract") return copy.deepcopy(document) class QualificationScenario: """One disturbance, armed by sustained *actual* motion, with a one-frame buffer. Transport delays retain capture time, frame identity, RGB, range and pose as one packet. Intermediate frames are dropped, never accumulated without bound. Recovery discards the old packet before admitting any fresh observations. """ def __init__(self, spec): self.spec = spec self.distance = self.moving_seconds = 0.0 self.previous = self.motion = None self.started = self.ended = None self.pending = None self.events = [] @property def active(self): return self.started is not None and self.ended is None def record(self, kind, now, **fields): self.events.append( { "event": kind, "monotonic": now, "utc": datetime.now(UTC).isoformat(), "simulation_time_ns": self.motion["simulation_time_ns"] if self.motion else None, **fields, } ) def observe_motion(self, row): if self.previous is not None: self.distance += math.dist(self.previous, row["pose"][:2]) self.previous, self.motion = row["pose"][:2], row moving = ( math.hypot(*row["velocity"][:2]) >= self.spec["trigger"]["minimum_speed_mps"] and row["stop_reason"] == "none" and sum(abs(v) for v in row["command"]) > 1e-5 ) self.moving_seconds = self.moving_seconds + row["dt"] if moving else 0.0 def tick(self, now, obstacle=None): trigger = self.spec["trigger"] if ( self.started is None and self.distance >= trigger["travel_m"] and self.moving_seconds >= trigger["moving_seconds"] ): fixture = obstacle.activate(self.motion) if obstacle is not None else None self.started = now self.record( "activated", now, pose=self.motion["pose"], velocity=self.motion["velocity"], travel_m=self.distance, moving_seconds=self.moving_seconds, fixture=fixture, ) if self.active and now - self.started >= self.spec["duration_seconds"]: if obstacle is not None: obstacle.deactivate() if self.pending is not None: self.record("discarded-on-recovery", now, frame_id=self.pending[1][1]) self.pending = None self.ended = now self.record("restored", now, pose=self.motion["pose"]) def deliver_due(self, now, submit): if self.pending is not None and now >= self.pending[0]: _, packet = self.pending self.pending = None submit(*packet) self.record("delayed-delivery", now, frame_id=packet[1], captured_at=packet[2]) def submit(self, packet, now, submit): name = self.spec["name"] if not self.active or self.spec["obstacle"] is not None: submit(*packet) return if name == "observation-delay" and self.pending is None: self.pending = (now + self.spec["observation_delay_seconds"], packet) self.record("buffered", now, frame_id=packet[1], captured_at=packet[2]) else: self.record("dropped", now, frame_id=packet[1], captured_at=packet[2]) def drain_events(self): events, self.events = self.events, [] return events