Spaces:
Running
Running
File size: 9,389 Bytes
6c4b387 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | """Review recorded robot states through explicit unit, frame and clock contracts.
The oracle reads the original recorded states. Candidate inputs use a different
representation; the oracle never calls the reference candidate's conversion code.
"""
from __future__ import annotations
import hashlib
import json
import math
import random
import time
from dataclasses import dataclass
from functools import lru_cache
from importlib.resources import files
from pathlib import Path
from evalarc.runner import CandidateError, EnvironmentFailure, Runtime
from evalarc.task import canonical
DIMENSIONS = {
"provenance": 0.10,
"coordinates": 0.25,
"clock": 0.15,
"metrics": 0.25,
"completeness": 0.15,
"protocol": 0.10,
}
REPORT_KEYS = {
"source_sha256",
"frame",
"time_seconds",
"position_m",
"speed_m_s",
"max_position_error_m",
"peak_frame",
"peak_height_m",
"missing_frames",
}
@lru_cache(maxsize=1)
def recordings() -> dict:
document = json.loads(files("evalarc").joinpath("assets/robot_recordings.json").read_text())
for name, entry in document["records"].items():
raw = entry["raw_json"].encode()
if hashlib.sha256(raw).hexdigest() != entry["sha256"]:
raise ValueError(f"packaged recording hash differs: {name}")
return document
@dataclass(frozen=True)
class RobotCase:
id: str
request: dict
original: list[dict]
source_sha256: str
query_frame: int
missing: list[int]
def generate_cases(seed: int) -> list[RobotCase]:
"""Procedural representations of a real recording, not fresh simulations."""
rng = random.Random(seed)
collection = recordings()
name = rng.choice(sorted(collection["records"]))
entry = collection["records"][name]
recording = json.loads(entry["raw_json"])
query = rng.randrange(18, 42)
variants = [
("world-meters", 1.0, [0, 1, 2], [1, 1, 1], [0.0, 0.0, 0.0], 1.0, 0, []),
(
"millimeters-sensor-frame",
0.001,
[2, 0, 1],
[-1, 1, -1],
[1.25, -0.75, 2.5],
0.001,
1_000_000,
[],
),
(
"centimeters-offset-clock",
0.01,
[1, 2, 0],
[1, -1, -1],
[-1.5, 0.4, 0.75],
0.001,
28_800_000,
[],
),
("incomplete-recording", 1.0, [0, 1, 2], [1, 1, 1], [0.0, 0.0, 0.0], 1.0, 0, [8, 43]),
]
cases = []
for case_id, scale, axes, signs, origin, seconds, offset, missing in variants:
observations = []
original = [row for row in recording["frames"] if row["sample"] not in missing]
for row in original:
position, velocity = [0.0] * 3, [0.0] * 3
for world_axis in range(3):
sensor_axis = axes[world_axis]
position[sensor_axis] = (
(row["position_m"][world_axis] - origin[world_axis]) / scale * signs[world_axis]
)
velocity[sensor_axis] = row["velocity_m_s"][world_axis] / scale * signs[world_axis]
observations.append(
{
"frame": row["sample"],
"tick": offset + row["time_s"] / seconds,
"position": position,
"velocity": velocity,
}
)
rng.shuffle(observations)
request = {
"op": "review",
"query_frame": query,
"recording": {
"source": {
"repository": collection["source_repository"],
"commit": collection["source_commit"],
"path": f"{collection['source_directory']}/{name}.json",
"sha256": entry["sha256"],
"kind": "derived-representation-of-recorded-cuda-states",
},
"metadata": {
"expected_frames": list(range(len(recording["frames"]))),
"world_from_sensor": {
"axes": axes,
"signs": signs,
"origin_m": origin,
"meters_per_unit": scale,
},
"clock": {"origin_tick": offset, "seconds_per_tick": seconds},
"analytic": {
"position0_m": recording["scene"]["initial_position_m"],
"velocity0_m_s": recording["scene"]["initial_velocity_m_s"],
"gravity_m_s2": recording["scene"]["gravity_m_s2"],
},
},
"observations": observations,
},
}
cases.append(RobotCase(case_id, request, original, entry["sha256"], query, missing))
return cases
def expected(case: RobotCase) -> dict:
"""Compute expected facts directly from the preserved source coordinates."""
selected = next(row for row in case.original if row["sample"] == case.query_frame)
analytic = case.request["recording"]["metadata"]["analytic"]
errors = []
for row in case.original:
t = row["time_s"]
predicted = [
p + v * t + 0.5 * g * t * t
for p, v, g in zip(
analytic["position0_m"],
analytic["velocity0_m_s"],
analytic["gravity_m_s2"],
strict=True,
)
]
errors.append(math.dist(row["position_m"], predicted))
peak = max(case.original, key=lambda row: (row["position_m"][2], -row["sample"]))
return {
"source_sha256": case.source_sha256,
"frame": case.query_frame,
"time_seconds": selected["time_s"],
"position_m": list(selected["position_m"]),
"speed_m_s": math.hypot(*selected["velocity_m_s"]),
"max_position_error_m": max(errors),
"peak_frame": peak["sample"],
"peak_height_m": peak["position_m"][2],
"missing_frames": case.missing,
}
def _close(actual: object, wanted: float) -> bool:
if type(actual) not in (int, float):
return False
try:
return math.isfinite(actual) and math.isclose(actual, wanted, rel_tol=1e-7, abs_tol=1e-6)
except OverflowError:
return False
def verify(case: RobotCase, report: object, protocol_ok: bool) -> dict[str, bool]:
wanted = expected(case)
report = report if isinstance(report, dict) else {}
vector = report.get("position_m")
missing = report.get("missing_frames")
return {
"provenance": report.get("source_sha256") == wanted["source_sha256"],
"coordinates": (
isinstance(vector, list)
and len(vector) == 3
and all(_close(a, b) for a, b in zip(vector, wanted["position_m"], strict=True))
and _close(report.get("speed_m_s"), wanted["speed_m_s"])
),
"clock": (
type(report.get("frame")) is int
and report["frame"] == wanted["frame"]
and _close(report.get("time_seconds"), wanted["time_seconds"])
),
"metrics": (
_close(report.get("max_position_error_m"), wanted["max_position_error_m"])
and type(report.get("peak_frame")) is int
and report["peak_frame"] == wanted["peak_frame"]
and _close(report.get("peak_height_m"), wanted["peak_height_m"])
),
"completeness": (
isinstance(missing, list)
and all(type(frame) is int for frame in missing)
and missing == wanted["missing_frames"]
),
"protocol": protocol_ok and set(report) == REPORT_KEYS,
}
def run_case(case: RobotCase, workspace: Path, state: Path, runtime: Runtime) -> dict:
started = time.monotonic()
process = None
response = None
report = None
error = None
status = "passed"
protocol_ok = False
try:
with runtime.start(workspace, state) as process:
response = process.request(case.request)
if (
not isinstance(response, dict)
or set(response) != {"ok", "report"}
or response["ok"] is not True
or not isinstance(response["report"], dict)
):
raise CandidateError("expected exactly an ok=true and report object")
report = response["report"]
process.finish("eof")
protocol_ok = True
except CandidateError as exc:
error, status = str(exc), "agent_error"
except EnvironmentFailure as exc:
error, status = str(exc), "environment_error"
checks = verify(case, report, protocol_ok)
if status == "environment_error":
checks = {key: None for key in checks}
passed = all(value is True for value in checks.values())
if status == "passed" and not passed:
status = "failed"
trace = [{"request": case.request, "response": response}]
return {
"case_id": case.id,
"checks": checks,
"passed": passed,
"status": status,
"error": error,
"duration_seconds": round(time.monotonic() - started, 6),
"transcript_sha256": hashlib.sha256(canonical(trace).encode()).hexdigest(),
"trace": trace,
"processes": [process.diagnostics()] if process is not None else [],
}
|