9fe9d27865
Consolidate next-stage docs, archive superseded narratives, and preserve frozen document inputs. Retain invalid P2 pairs, partial RootXY training, failed attempts and nonselectable checkpoints as explicit limitations. Include supervised evaluation/training, desktop GPU admission, bounded evidence and atomic-publication monitor repairs with CPU regressions. Ignore local dotenv configuration. Validation: 1403 passed, 6 skipped for contact-cycle and related contracts/CLI tests. Full-suite legacy ABI and registration tests require Python 3.12/Torch 2.10/pxr; no runtime checks relaxed. No new physical run or accepted skill.
1155 lines
56 KiB
Python
1155 lines
56 KiB
Python
"""CPU file-backed synthetic identities; never imports Isaac or loads a policy."""
|
|
import ast
|
|
import copy
|
|
import importlib.util
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SOURCES = ROOT / "source/SkillFactory/SkillFactory/legs/contact_cycle_sim"
|
|
|
|
|
|
def load(name, path, monkeypatch=None):
|
|
spec = importlib.util.spec_from_file_location(name, path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
if monkeypatch:
|
|
monkeypatch.setitem(sys.modules, name, module)
|
|
else:
|
|
sys.modules[name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
# A real file-backed private test package, not a forged SkillFactory.__file__.
|
|
PACKAGE = "_evaluation_binding_cpu"
|
|
load(PACKAGE, SOURCES / "__init__.py")
|
|
binding = load(PACKAGE + ".evaluation_binding", SOURCES / "evaluation_binding.py")
|
|
replay = load(PACKAGE + ".replay", SOURCES / "replay.py")
|
|
|
|
CFG_NAME = "SkillFactory.tasks.manager_based.contact_cycle_sim.evaluation_cfg"
|
|
CFG = '''from dataclasses import dataclass, field
|
|
from typing import ClassVar
|
|
from isaaclab.scene.interactive_scene_cfg import InteractiveSceneCfg
|
|
from isaaclab.assets.asset_base_cfg import AssetBaseCfg
|
|
from isaaclab.assets.articulation.articulation_cfg import ArticulationCfg
|
|
from isaaclab.sensors.contact_sensor.contact_sensor_cfg import ContactSensorCfg
|
|
from isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg import JointWrenchSensorCfg
|
|
from isaaclab.sim.spawners.from_files.from_files_cfg import UsdFileCfg
|
|
from isaaclab.sim.spawners.lights.lights_cfg import DomeLightCfg
|
|
|
|
@dataclass
|
|
class LegSceneCfg(InteractiveSceneCfg):
|
|
ground: AssetBaseCfg = field(default_factory=lambda: AssetBaseCfg('/World/ground', UsdFileCfg()))
|
|
light: AssetBaseCfg = field(default_factory=lambda: AssetBaseCfg('/World/light', DomeLightCfg()))
|
|
robot: ArticulationCfg = field(default_factory=lambda: ArticulationCfg('{ENV_REGEX_NS}/Robot', UsdFileCfg()))
|
|
foot_contact: ContactSensorCfg = field(default_factory=lambda: ContactSensorCfg('{ENV_REGEX_NS}/Robot/foot'))
|
|
|
|
@dataclass
|
|
class LoadedSceneCfg(LegSceneCfg):
|
|
mount_wrench: JointWrenchSensorCfg = field(default_factory=JointWrenchSensorCfg)
|
|
|
|
def reward():
|
|
return 1
|
|
|
|
@dataclass
|
|
class EvaluationContactCycleEnvCfg:
|
|
_usd_field_exceptions: ClassVar[dict] = {"physics": "original"}
|
|
seed: int = 93501
|
|
sim: dict = field(default_factory=lambda: {"dt": .002, "physx": {"solver": 4}})
|
|
scene: LoadedSceneCfg = field(default_factory=LoadedSceneCfg)
|
|
actions: dict = field(default_factory=lambda: {"scale": .5})
|
|
rewards: dict = field(default_factory=lambda: {"task": reward})
|
|
terminations: dict = field(default_factory=lambda: {"timeout": 9.})
|
|
'''
|
|
|
|
|
|
SCENE_BASE = """from dataclasses import dataclass
|
|
@dataclass
|
|
class InteractiveSceneCfg:
|
|
num_envs: int = 1
|
|
env_spacing: float = 2.
|
|
lazy_sensor_update: bool = False
|
|
replicate_physics: bool = True
|
|
filter_collisions: bool = True
|
|
clone_in_fabric: bool = False
|
|
"""
|
|
ASSET = """from dataclasses import dataclass
|
|
@dataclass
|
|
class AssetBaseCfg:
|
|
prim_path: str
|
|
spawn: object = None
|
|
collision_group: int = 0
|
|
"""
|
|
SPAWN = """from dataclasses import dataclass
|
|
@dataclass
|
|
class UsdFileCfg:
|
|
spawn_path: str | None = None
|
|
"""
|
|
WRENCH = """from dataclasses import dataclass
|
|
@dataclass
|
|
class JointWrenchSensorCfg:
|
|
class_type: str = 'isaaclab.sensors.joint_wrench.joint_wrench_sensor:JointWrenchSensor'
|
|
prim_path: str = '{ENV_REGEX_NS}/Robot'
|
|
update_period: float = 0.0
|
|
debug_vis: bool = False
|
|
convention: str = 'incoming_joint_frame'
|
|
"""
|
|
SCENE_MODULES = {
|
|
"isaaclab.assets.asset_base_cfg": ASSET,
|
|
"isaaclab.assets.articulation.articulation_cfg":
|
|
ASSET.replace("AssetBaseCfg", "ArticulationCfg"),
|
|
"isaaclab.sensors.contact_sensor.contact_sensor_cfg": """from dataclasses import dataclass, field
|
|
@dataclass
|
|
class ContactSensorCfg:
|
|
prim_path: str
|
|
filter_prim_paths_expr: list = field(default_factory=lambda: ['/World/ground/GroundPlane/CollisionPlane'])
|
|
sensor_shape_prim_expr: list = field(default_factory=list)
|
|
filter_shape_prim_expr: list | None = None
|
|
collision_group: int = 0
|
|
""",
|
|
"isaaclab.sim.spawners.from_files.from_files_cfg": SPAWN,
|
|
"isaaclab.sim.spawners.lights.lights_cfg": SPAWN.replace("UsdFileCfg", "DomeLightCfg"),
|
|
}
|
|
|
|
|
|
def numerical_inventory(tmp_path, monkeypatch):
|
|
inventory = {"scope": binding.INVENTORY_SCOPE, "modules": {}, "artifacts": {}}
|
|
for name in ("torch", "torch._C", "torch.random", "numpy", "numpy.random", "numpy.random.mtrand",
|
|
"numpy.core._multiarray_umath", "warp", "warp._src.context"):
|
|
path = tmp_path / "site" / name.replace(".", "/") / "__init__.py"
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text("# synthetic external module\n")
|
|
load(name, path, monkeypatch)
|
|
inventory["modules"][name] = {"path": str(path), "sha256": binding.sha256(path.read_bytes())}
|
|
for owner, directories in {"torch": ("torch/lib",), "numpy": ("numpy/core", "numpy/random", "numpy/linalg",
|
|
"numpy/fft", "numpy.libs"), "warp": ("warp/bin",)}.items():
|
|
for directory in directories:
|
|
path = tmp_path / "site" / directory / "synthetic.so"
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(b"not-an-executable-shared-library")
|
|
inventory["artifacts"][directory] = {"module": owner, "path": str(path),
|
|
"sha256": binding.sha256(path.read_bytes()),
|
|
"purpose": "synthetic test native dependency"}
|
|
monkeypatch.setattr(binding, "REQUIRED_RUNTIME", {n: ("synthetic-core", r["path"], r["sha256"])
|
|
for n, r in inventory["modules"].items()})
|
|
monkeypatch.setattr(binding, "RUNTIME_LAYOUT_PINS", {})
|
|
return inventory
|
|
|
|
|
|
@pytest.fixture
|
|
def fixture(tmp_path, monkeypatch):
|
|
# Isolate synthetic file-backed modules from optional registration bootstraps
|
|
# used by the other CPU suites. Restore every entry after this test.
|
|
for name in tuple(sys.modules):
|
|
if name.split(".")[0] in ("SkillFactory", "isaaclab", "isaaclab_physx", "torch", "numpy", "warp"):
|
|
monkeypatch.delitem(sys.modules, name)
|
|
root = tmp_path / "project"
|
|
root.mkdir()
|
|
for relative in binding.FIXED_SOURCES:
|
|
path = root / relative
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
if relative.endswith(".json"):
|
|
path.write_bytes((ROOT / relative).read_bytes())
|
|
else:
|
|
path.write_text(CFG if relative.endswith("evaluation_cfg.py") else "# synthetic fixture\n")
|
|
if relative.startswith(binding.PACKAGE) and relative.endswith(".py"):
|
|
name = relative.removeprefix("source/SkillFactory/").removesuffix(".py").replace("/", ".")
|
|
if name != CFG_NAME:
|
|
load(name, path, monkeypatch)
|
|
runner = root / "runner.py"
|
|
runner.write_text("def run():\n return None\n")
|
|
instrumentation = root / "instrumentation.py"
|
|
instrumentation.write_text("def capture():\n return None\n")
|
|
load("synthetic_runner", runner, monkeypatch)
|
|
load("synthetic_instrumentation", instrumentation, monkeypatch)
|
|
isaac = tmp_path / "IsaacLab"
|
|
pins = {}
|
|
for name, (relative, _) in binding.ISAAC_PINS.items():
|
|
path = isaac / relative
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(SCENE_BASE if name == "isaaclab.scene.interactive_scene_cfg"
|
|
else WRENCH if name == "isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg"
|
|
else "# synthetic pinned dependency\n")
|
|
pins[name] = (relative, binding.sha256(path.read_bytes()))
|
|
load(name, path, monkeypatch)
|
|
monkeypatch.setattr(binding, "ISAAC_ROOT", isaac)
|
|
monkeypatch.setattr(binding, "ISAAC_PINS", pins)
|
|
for name, text in SCENE_MODULES.items():
|
|
path = isaac / ("source/isaaclab/" + name.replace(".", "/") + ".py")
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(text)
|
|
load(name, path, monkeypatch)
|
|
load(CFG_NAME, root / binding.PACKAGE / "tasks/manager_based/contact_cycle_sim/evaluation_cfg.py", monkeypatch)
|
|
inventory = numerical_inventory(tmp_path, monkeypatch)
|
|
frozen_calls = []
|
|
|
|
def verify(root):
|
|
frozen_calls.append(root)
|
|
return ({"task_id": "frozen-task", "contract": "frozen-contract", "features": "46D"},
|
|
{"candidate_path": str(root / "candidate.pt"), "candidate_sha256": "a" * 64,
|
|
"manifest_sha256": "b" * 64, "live_source_exceptions": []})
|
|
|
|
monkeypatch.setattr(replay, "verify_bundle", verify)
|
|
cfg = sys.modules[CFG_NAME].EvaluationContactCycleEnvCfg()
|
|
binding.normalize_config(cfg)
|
|
args = dict(dependency_inventory=inventory, root=root,
|
|
protocol_path="configs/contact_cycle/continuous_eval_p1_v1.json",
|
|
effective_config=cfg, trial_index=0,
|
|
future_sources={"runner": ("runner.py",), "instrumentation": ("instrumentation.py",)},
|
|
declared_overrides={"seed": "protocol.trial.seed"})
|
|
return SimpleNamespace(args=args, cfg=cfg, root=root, calls=frozen_calls, monkeypatch=monkeypatch)
|
|
|
|
|
|
def prepare(fixture):
|
|
return binding.prepare_binding(**fixture.args)
|
|
|
|
|
|
def verify(fixture, manifest, **changes):
|
|
args = {**fixture.args, **changes}
|
|
return binding.verify_binding(manifest, trusted_manifest_sha256=binding.sha256(manifest), **args)
|
|
|
|
|
|
def test_exact_canonical_identity_separate_task_policy_and_no_grant(fixture):
|
|
manifest = prepare(fixture)
|
|
assert type(manifest) is bytes
|
|
document = json.loads(manifest)
|
|
assert binding.canonical_json(document) == manifest
|
|
assert verify(fixture, manifest) == binding.sha256(manifest)
|
|
assert len(fixture.calls) == 4 # unchanged frozen delegation on both snapshots
|
|
assert document["evaluation_id"] != document["frozen_policy"]["task_id"]
|
|
assert document["execution_authorized"] is document["accepted_skill"] is False
|
|
assert document["optimizer_steps"] == 0
|
|
assert document["quaternion_convention"] == "xyzw"
|
|
assert document["protocol"]["sha256"] == binding.sha256(
|
|
(fixture.root / fixture.args["protocol_path"]).read_bytes())
|
|
assert document["trial_seeds"] == {"0": 93501, "1": 93501}
|
|
assert document["effective_config"]["fields"]["mapping"]["seed"] == {"override": "protocol.trial.seed"}
|
|
assert document["sources"]["runner"][0]["module"] == "synthetic_runner"
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["sim", "scene", "actions", "rewards", "terminations"])
|
|
def test_every_effective_config_field_is_bound(fixture, field):
|
|
manifest = prepare(fixture)
|
|
if field == "scene":
|
|
fixture.cfg.scene.env_spacing += 1.
|
|
else:
|
|
getattr(fixture.cfg, field)["unexpected"] = True
|
|
with pytest.raises(ValueError, match="binding mismatch"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_mutable_classvar_physics_config_is_bound(fixture):
|
|
manifest = prepare(fixture)
|
|
type(fixture.cfg)._usd_field_exceptions["physics"] = "changed"
|
|
with pytest.raises(ValueError, match="binding mismatch"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_private_dynamic_config_fields_not_silently_ignored(fixture):
|
|
manifest = prepare(fixture)
|
|
fixture.cfg._hidden = {"actuator_delay": 2}
|
|
with pytest.raises(ValueError, match="binding mismatch"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
@pytest.mark.parametrize("value", [object(), {1: "bad"}, {1, 2}, float("nan"), float("inf"), lambda: 1])
|
|
def test_unsupported_serialization_fails_closed(fixture, value):
|
|
fixture.cfg.actions["bad"] = value
|
|
with pytest.raises(ValueError):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_cyclic_serialization_fails_closed(fixture):
|
|
fixture.cfg.actions["cycle"] = fixture.cfg
|
|
with pytest.raises(ValueError, match="Cyclic"):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_canonical_config_is_typed_stable_and_contains_callable_identity(fixture):
|
|
encoded = binding.canonical_config(fixture.cfg)
|
|
assert encoded == binding.canonical_config(copy.deepcopy(fixture.cfg))
|
|
assert b'"qualname":"reward"' in encoded
|
|
assert b"0x" in encoded # exact finite float hex, not an object address
|
|
for left, right in [(1, True), (1, 1.), ([1], (1,)), (0., -0.)]:
|
|
assert binding.canonical_config(left) != binding.canonical_config(right)
|
|
|
|
|
|
@pytest.mark.parametrize("role", ["runner", "instrumentation"])
|
|
def test_required_future_roles_and_missing_files(fixture, role):
|
|
fixture.args["future_sources"].pop(role)
|
|
with pytest.raises(ValueError, match="runner AND instrumentation"):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_duplicate_roles_cannot_hide_missing_instrumentation(fixture):
|
|
fixture.args["future_sources"]["instrumentation"] = ("runner.py",)
|
|
with pytest.raises(ValueError, match="Duplicate"):
|
|
prepare(fixture)
|
|
|
|
|
|
@pytest.mark.parametrize("relative", binding.FIXED_SOURCES + ("runner.py", "instrumentation.py"))
|
|
def test_missing_or_changed_full_source_list(fixture, relative):
|
|
manifest = prepare(fixture)
|
|
path = fixture.root / relative
|
|
path.write_bytes(path.read_bytes() + b"\n")
|
|
with pytest.raises(ValueError, match="binding mismatch"):
|
|
verify(fixture, manifest)
|
|
path.unlink()
|
|
with pytest.raises((ValueError, FileNotFoundError)):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_source_import_must_match_actual_file_not_lookalike_directory(fixture):
|
|
module = sys.modules[CFG_NAME]
|
|
other = fixture.root / "lookalike.py"
|
|
other.write_bytes(Path(module.__file__).read_bytes())
|
|
fixture.monkeypatch.setattr(module, "__file__", str(other))
|
|
fixture.monkeypatch.setattr(module.__spec__, "origin", str(other))
|
|
with pytest.raises(ValueError, match="(?:Imported evaluation source|Config symbol source) mismatch"):
|
|
prepare(fixture)
|
|
|
|
|
|
@pytest.mark.parametrize("name", tuple(binding.ISAAC_PINS))
|
|
def test_swapped_isaac_tree_composer_and_articulation_backend(fixture, name):
|
|
module = sys.modules[name]
|
|
swapped = fixture.root / "opt/IsaacLab" / Path(module.__file__).name
|
|
swapped.parent.mkdir(parents=True, exist_ok=True)
|
|
swapped.write_bytes(Path(module.__file__).read_bytes())
|
|
fixture.monkeypatch.setattr(module, "__file__", str(swapped))
|
|
fixture.monkeypatch.setattr(module.__spec__, "origin", str(swapped))
|
|
with pytest.raises(ValueError, match="(?:Isaac pin/path|normalization source) mismatch"):
|
|
prepare(fixture)
|
|
|
|
|
|
@pytest.mark.parametrize("name", tuple(binding.ISAAC_PINS))
|
|
def test_isaac_pin_hash_changes_rejected_even_during_preparation(fixture, name):
|
|
Path(sys.modules[name].__file__).write_text("# tampered\n")
|
|
with pytest.raises(ValueError, match="(?:Isaac pin/path|normalization source) mismatch"):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_wxyz_is_never_implicitly_adapted(fixture):
|
|
fixture.args["quaternion_convention"] = "wxyz"
|
|
with pytest.raises(ValueError, match="xyzw"):
|
|
prepare(fixture)
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["symlink", "relative", "spec", "namespace", "alias", "dot_alias"])
|
|
def test_imported_path_alias_and_namespace_identity_fail_closed(fixture, kind):
|
|
module = sys.modules["synthetic_runner"]
|
|
if kind == "symlink":
|
|
alias = fixture.root / "runner_alias.py"
|
|
alias.symlink_to(module.__file__)
|
|
fixture.monkeypatch.setattr(module, "__file__", str(alias))
|
|
elif kind == "relative":
|
|
fixture.monkeypatch.setattr(module, "__file__", "runner.py")
|
|
elif kind == "dot_alias":
|
|
fixture.monkeypatch.setattr(module, "__file__", str(fixture.root) + "/./runner.py")
|
|
elif kind == "spec":
|
|
fixture.monkeypatch.setattr(module.__spec__, "origin", str(fixture.root / "wrong.py"))
|
|
elif kind == "namespace":
|
|
fixture.monkeypatch.delattr(module, "__file__")
|
|
else:
|
|
fixture.monkeypatch.setitem(sys.modules, "surprise_alias", sys.modules[CFG_NAME])
|
|
with pytest.raises(ValueError):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_entrypoint_requires_real_main_and_explicit_path(fixture):
|
|
fixture.args["entrypoints"] = ("runner.py",)
|
|
with pytest.raises(ValueError):
|
|
prepare(fixture)
|
|
fixture.monkeypatch.delitem(sys.modules, "synthetic_runner")
|
|
load("__main__", fixture.root / "runner.py", fixture.monkeypatch)
|
|
manifest = prepare(fixture)
|
|
assert json.loads(manifest)["sources"]["runner"][0]["entrypoint"] is True
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_manifest_requires_independent_exact_digest_and_immutable_bytes(fixture):
|
|
manifest = prepare(fixture)
|
|
with pytest.raises(TypeError, match="trusted_manifest_sha256"):
|
|
binding.verify_binding(manifest, **fixture.args)
|
|
with pytest.raises(ValueError, match="trusted manifest"):
|
|
binding.verify_binding(manifest, trusted_manifest_sha256="0" * 64, **fixture.args)
|
|
for mutable in (bytearray(manifest), json.loads(manifest)):
|
|
with pytest.raises(ValueError, match="immutable"):
|
|
binding.verify_binding(mutable, trusted_manifest_sha256="0" * 64, **fixture.args)
|
|
|
|
|
|
@pytest.mark.parametrize("mutation", [
|
|
lambda x: x + b"\n", lambda x: x.replace(b'"format":', b'"format":"duplicate","format":')])
|
|
def test_noncanonical_json_with_matching_hash_is_rejected(fixture, mutation):
|
|
manifest = mutation(prepare(fixture))
|
|
with pytest.raises(ValueError, match="canonical JSON"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_tampered_manifest_with_old_trusted_digest_rejected(fixture):
|
|
manifest = prepare(fixture)
|
|
tampered = manifest.replace(b"93501", b"93502")
|
|
with pytest.raises(ValueError, match="trusted manifest"):
|
|
binding.verify_binding(tampered, trusted_manifest_sha256=binding.sha256(manifest), **fixture.args)
|
|
|
|
|
|
def test_input_mutation_during_snapshot_rejected(fixture):
|
|
original = replay.verify_bundle
|
|
|
|
def mutate(root):
|
|
fixture.cfg.actions["counter"] = len(fixture.calls)
|
|
return original(root)
|
|
|
|
fixture.monkeypatch.setattr(replay, "verify_bundle", mutate)
|
|
with pytest.raises(ValueError, match="changed during snapshot"):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_frozen_delegate_failure_propagates_at_prepare_and_verify(fixture):
|
|
manifest = prepare(fixture)
|
|
|
|
def reject(root):
|
|
raise ValueError("Frozen candidate SHA256 mismatch")
|
|
|
|
fixture.monkeypatch.setattr(replay, "verify_bundle", reject)
|
|
with pytest.raises(ValueError, match="Frozen candidate"):
|
|
prepare(fixture)
|
|
with pytest.raises(ValueError, match="Frozen candidate"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
@pytest.mark.parametrize("overrides", [{}, {"seed": "any"}, {"seed": "protocol.trial.seed", "sim.dt": .1}])
|
|
def test_only_explicit_declared_seed_override(fixture, overrides):
|
|
fixture.args["declared_overrides"] = overrides
|
|
with pytest.raises(ValueError, match="override"):
|
|
prepare(fixture)
|
|
|
|
|
|
@pytest.mark.parametrize("protocol_path", ["configs/contact_cycle/bounded_diagnostic_p2_v1.json",
|
|
"configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json"])
|
|
def test_all_preregistered_p2_seed_rows_without_rewriting_manifest(fixture, protocol_path):
|
|
fixture.args["protocol_path"] = protocol_path
|
|
protocol = binding.load_protocol(fixture.root / fixture.args["protocol_path"])
|
|
fixture.cfg.seed = protocol.trial(0).seed
|
|
manifest = prepare(fixture)
|
|
for trial in protocol.trials:
|
|
fixture.cfg.seed = trial.seed
|
|
verify(fixture, manifest, trial_index=trial.index)
|
|
fixture.cfg.seed += 1
|
|
with pytest.raises(ValueError, match="trial seed"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_late_imports_fail_closed_until_eager_imported_before_preparation(fixture):
|
|
manifest = prepare(fixture)
|
|
path = fixture.root / binding.PACKAGE / "late.py"
|
|
path.write_text("# late dependency\n")
|
|
load("SkillFactory.late", path, fixture.monkeypatch)
|
|
with pytest.raises(ValueError, match="binding mismatch"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_real_installed_pins_match_files_and_runtime_literals_without_import():
|
|
# Inspection only: no importing Isaac, namespace spoofing or real bundle pass.
|
|
runtime = ROOT / binding.PACKAGE / "tasks/manager_based/contact_cycle_sim/evaluation_env.py"
|
|
text = runtime.read_text()
|
|
for name, (relative, digest) in binding.ISAAC_PINS.items():
|
|
assert binding.sha256((binding.ISAAC_ROOT / relative).read_bytes()) == digest
|
|
if name.startswith("isaaclab.") and name not in (*binding.NORMALIZATION_SOURCES, "isaaclab.sim.utils.prims"):
|
|
assert digest in text
|
|
data = binding.ISAAC_ROOT / binding.ISAAC_PINS[
|
|
"isaaclab_physx.assets.articulation.articulation_data"][0]
|
|
assert "orientation is provided in (x, y, z, w) format" in data.read_text()
|
|
|
|
|
|
def test_unchanged_frozen_verifier_checks_synthetic_candidate_bytes(fixture):
|
|
# Restore the real function from its unchanged source, with synthetic pins
|
|
# only in this private test module. Never deserialize any candidate.
|
|
with fixture.monkeypatch.context() as temporary:
|
|
temporary.setitem(sys.modules, "torch", replay.torch)
|
|
actual = load(PACKAGE + ".synthetic_replay", SOURCES / "replay.py", fixture.monkeypatch)
|
|
candidate = fixture.root / actual.CANDIDATE
|
|
candidate.parent.mkdir(parents=True)
|
|
candidate.write_bytes(b"synthetic-not-a-checkpoint")
|
|
config_relative = "configs/contact_cycle/simulation_residual_v1.json"
|
|
path = fixture.root / config_relative
|
|
path.write_bytes((ROOT / config_relative).read_bytes())
|
|
source = "fixture_source.txt"
|
|
(fixture.root / source).write_text("frozen source")
|
|
snapshot = candidate.parent / "source_snapshot" / source
|
|
snapshot.parent.mkdir()
|
|
snapshot.write_text("frozen source")
|
|
manifest = {"contract": actual.CONTRACT, "task_id": actual.TASK_ID,
|
|
"features": actual.FEATURE_VERSION, "model_config": actual.MODEL_CONFIG,
|
|
"sources": {source: actual.sha256(snapshot.read_bytes())},
|
|
"training": {"task_config": config_relative}}
|
|
(candidate.parent / "manifest.json").write_text(json.dumps(manifest))
|
|
for relative in actual.REPLAY_SOURCES:
|
|
path = fixture.root / relative
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text("# synthetic frozen replay file\n")
|
|
fixture.monkeypatch.setattr(actual, "MANIFEST_SHA256", actual.sha256(actual.canonical(manifest).encode()))
|
|
fixture.monkeypatch.setattr(actual, "CANDIDATE_SHA256", actual.sha256(candidate.read_bytes()))
|
|
fixture.monkeypatch.setattr(replay, "verify_bundle", actual.verify_bundle)
|
|
prepared = prepare(fixture)
|
|
candidate.write_bytes(b"tampered candidate")
|
|
with pytest.raises(ValueError, match="Frozen candidate SHA256 mismatch"):
|
|
verify(fixture, prepared)
|
|
candidate.write_bytes(b"synthetic-not-a-checkpoint")
|
|
snapshot.write_text("tampered immutable snapshot")
|
|
with pytest.raises(ValueError, match="snapshot mismatch"):
|
|
verify(fixture, prepared)
|
|
|
|
|
|
def test_future_roles_cannot_be_satisfied_by_nonexecutable_placeholder(fixture):
|
|
path = fixture.root / "pending.txt"
|
|
path.write_text("not implemented")
|
|
fixture.args["future_sources"]["instrumentation"] = ("pending.txt",)
|
|
with pytest.raises(ValueError, match="executable Python"):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_undeclared_callable_substitution_is_rejected(fixture):
|
|
manifest = prepare(fixture)
|
|
fixture.cfg.rewards["task"] = sys.modules["synthetic_runner"].run
|
|
with pytest.raises(ValueError, match="binding mismatch"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_symbol_must_resolve_to_actual_imported_object(fixture):
|
|
original = fixture.cfg.rewards["task"]
|
|
fixture.monkeypatch.setattr(sys.modules[CFG_NAME], "reward", lambda: 2)
|
|
assert fixture.cfg.rewards["task"] is original
|
|
with pytest.raises(ValueError, match="actual imported object"):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_class_identity_cannot_be_replaced_by_equal_field_dictionary(fixture):
|
|
fixture.args["effective_config"] = vars(fixture.cfg)
|
|
with pytest.raises(ValueError, match="Effective config"):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_source_list_symlink_and_parent_alias_rejected(fixture):
|
|
path = fixture.root / "instrumentation.py"
|
|
renamed = fixture.root / "real_instrumentation.py"
|
|
path.rename(renamed)
|
|
path.symlink_to(renamed)
|
|
with pytest.raises(ValueError, match="Noncanonical"):
|
|
prepare(fixture)
|
|
fixture.args["future_sources"]["instrumentation"] = ("../instrumentation.py",)
|
|
with pytest.raises(ValueError, match="canonical relative"):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_caller_owned_role_lists_do_not_mutate_prepared_bytes(fixture):
|
|
fixture.args["future_sources"]["runner"] = ["runner.py"]
|
|
manifest = prepare(fixture)
|
|
expected = bytes(manifest)
|
|
fixture.args["future_sources"]["runner"].clear()
|
|
assert manifest == expected
|
|
with pytest.raises(ValueError, match="Missing explicit"):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
@pytest.fixture
|
|
def source_realistic_config(fixture):
|
|
"""Pinned string/configclass code, without importing Isaac registration/Kit.
|
|
|
|
Only the unrelated to_dict/from_dict import is removed from configclass;
|
|
these helpers are deliberately unavailable, and binding must never use them.
|
|
"""
|
|
for name in ("isaaclab.utils.string", "isaaclab.utils.configclass"):
|
|
relative, _ = binding.ISAAC_PINS[name]
|
|
text = (Path('/home/cen/IsaacLab') / relative).read_text()
|
|
if name.endswith('configclass'):
|
|
text = text.replace('from .dict import class_to_dict, update_class_from_dict',
|
|
'class_to_dict = update_class_from_dict = None')
|
|
path = Path(sys.modules[name].__file__)
|
|
path.write_text(text)
|
|
load(name, path, fixture.monkeypatch)
|
|
binding.ISAAC_PINS[name] = (relative, binding.sha256(path.read_bytes()))
|
|
recorder = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/managers/recorder_manager.py')
|
|
enum_node = next(n for n in ast.parse(recorder.read_text()).body
|
|
if isinstance(n, ast.ClassDef) and n.name == 'DatasetExportMode')
|
|
path = fixture.root / 'source_realistic_config.py'
|
|
path.write_text('import enum\nfrom isaaclab.utils.configclass import configclass\n'
|
|
+ ast.unparse(enum_node) + '\n' + '''
|
|
@configclass
|
|
class RecorderManagerBaseCfg:
|
|
dataset_export_mode = DatasetExportMode.EXPORT_ALL
|
|
|
|
@configclass
|
|
class WrappedConfig:
|
|
recorder = RecorderManagerBaseCfg()
|
|
ui_window_class_type = 'synthetic_runner:run'
|
|
spawn_func = 'synthetic_instrumentation:capture'
|
|
''')
|
|
module = load('source_realistic_config', path, fixture.monkeypatch)
|
|
fixture.args['dependency_inventory']['modules'][module.__name__] = {
|
|
'path': str(path), 'sha256': binding.sha256(path.read_bytes())}
|
|
cfg = module.WrappedConfig()
|
|
fixture.cfg.actions['source_realistic'] = cfg
|
|
return module, cfg
|
|
|
|
|
|
def test_actual_configclass_enum_and_resolvable_string_layout(fixture, source_realistic_config):
|
|
module, cfg = source_realistic_config
|
|
rs = sys.modules['isaaclab.utils.string'].ResolvableString
|
|
assert type(cfg.ui_window_class_type) is rs
|
|
assert rs.__slots__ == ('_resolved_callable', '_resolve_error')
|
|
assert copy.deepcopy(cfg.ui_window_class_type) is cfg.ui_window_class_type
|
|
encoded = json.loads(binding.canonical_config(cfg))['fields']['mapping']
|
|
wrapped = encoded['ui_window_class_type']
|
|
assert wrapped['reference'] == 'synthetic_runner:run'
|
|
assert wrapped['cached_callable'] is None
|
|
assert wrapped['target']['symbol'] == {'module': 'synthetic_runner', 'qualname': 'run'}
|
|
mode = encoded['recorder']['fields']['mapping']['dataset_export_mode']
|
|
assert mode['enum'] == {'module': module.__name__, 'qualname': 'DatasetExportMode'}
|
|
assert mode['member'] == 'EXPORT_ALL'
|
|
assert binding.canonical_config(module.DatasetExportMode.EXPORT_ALL) != binding.canonical_config(
|
|
int(module.DatasetExportMode.EXPORT_ALL))
|
|
with pytest.raises(ValueError, match='preconstruction'):
|
|
prepare(fixture)
|
|
binding.normalize_config(fixture.cfg)
|
|
manifest = prepare(fixture)
|
|
assert cfg.ui_window_class_type._resolved_callable is sys.modules['synthetic_runner'].run
|
|
# The real resolver would return exactly this cached object without import/call.
|
|
assert cfg.ui_window_class_type._resolve() is sys.modules['synthetic_runner'].run
|
|
binding.normalize_config(fixture.cfg)
|
|
assert prepare(fixture) == manifest
|
|
verify(fixture, manifest)
|
|
cfg.ui_window_class_type._resolved_callable = None
|
|
with pytest.raises(ValueError, match='preconstruction'):
|
|
verify(fixture, manifest)
|
|
assert cfg.ui_window_class_type._resolved_callable is None # verification did not repair it
|
|
|
|
|
|
@pytest.mark.parametrize('mutation', ['target', 'error', 'missing', 'object', 'plain_string'])
|
|
def test_resolvable_reference_cache_fail_closed(fixture, source_realistic_config, mutation):
|
|
_, cfg = source_realistic_config
|
|
binding.normalize_config(fixture.cfg)
|
|
manifest = prepare(fixture)
|
|
if mutation == 'target':
|
|
cfg.spawn_func._resolved_callable = sys.modules['synthetic_runner'].run
|
|
elif mutation == 'error':
|
|
cfg.spawn_func._resolve_error = ValueError('cached failure')
|
|
elif mutation == 'missing':
|
|
fixture.monkeypatch.delattr(sys.modules['synthetic_instrumentation'], 'capture')
|
|
elif mutation == 'object':
|
|
fixture.monkeypatch.setattr(sys.modules['synthetic_instrumentation'], 'capture', object())
|
|
else:
|
|
cfg.spawn_func = str(cfg.spawn_func)
|
|
with pytest.raises(ValueError):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_arbitrary_string_subclass_not_accepted_as_resolvable(fixture):
|
|
class Lookalike(str):
|
|
pass
|
|
fixture.cfg.actions['bad'] = Lookalike('synthetic_runner:run')
|
|
with pytest.raises(ValueError, match='Unsupported effective config type'):
|
|
prepare(fixture)
|
|
|
|
|
|
@pytest.mark.parametrize('field,value', [
|
|
('clone_in_fabric', True), ('num_envs', 2), ('replicate_physics', 1),
|
|
])
|
|
def test_normalizer_rejects_unsupported_clone_settings(fixture, field, value):
|
|
setattr(fixture.cfg.scene, field, value)
|
|
with pytest.raises(ValueError, match='single-env'):
|
|
binding.normalize_config(fixture.cfg)
|
|
|
|
|
|
@pytest.mark.parametrize('mutation', ['namespace', 'template', 'multi', 'extra', 'terrain', 'spawn'])
|
|
def test_normalizer_rejects_unreviewed_scene_semantics(fixture, mutation):
|
|
scene = fixture.cfg.scene
|
|
if mutation == 'namespace':
|
|
scene.robot.prim_path = '/Other/envs/env_.*/Robot'
|
|
elif mutation == 'template':
|
|
scene.robot.prim_path = '{OTHER_NS}/Robot'
|
|
elif mutation == 'multi':
|
|
scene.robot.spawn = SimpleNamespace(spawn_paths=[])
|
|
elif mutation == 'extra':
|
|
scene.extra = scene.ground
|
|
elif mutation == 'terrain':
|
|
scene.ground = SimpleNamespace(prim_path='/World/ground', num_envs=1)
|
|
else:
|
|
scene.robot.spawn.spawn_path = '/World/envs/env_8/Robot'
|
|
with pytest.raises(ValueError):
|
|
binding.normalize_config(fixture.cfg)
|
|
|
|
|
|
def test_rootxy_exact128_normalizer_does_not_broaden_evaluation_contract(fixture):
|
|
name = 'SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_cfg_v2'
|
|
path = fixture.root / 'rootxy_config_double.py'
|
|
path.write_text('from ' + CFG_NAME + ' import EvaluationContactCycleEnvCfg\n'
|
|
'class RootXYContactCycleEnvCfg(EvaluationContactCycleEnvCfg):\n pass\n')
|
|
module = load(name, path, fixture.monkeypatch)
|
|
cfg = module.RootXYContactCycleEnvCfg()
|
|
cfg.scene.num_envs = 128
|
|
get = lambda: binding._typed_normalization_updates(cfg, name, 'RootXYContactCycleEnvCfg', 128)
|
|
for obj, field, value in get():
|
|
setattr(obj, field, value)
|
|
assert get() == []
|
|
assert cfg.scene.robot.prim_path == '/World/envs/env_.*/Robot'
|
|
assert cfg.scene.robot.spawn.spawn_path == '/World/envs/env_0/Robot'
|
|
with pytest.raises(ValueError, match='actual Evaluation'):
|
|
binding.normalize_config(cfg)
|
|
fixture.cfg.scene.num_envs = 128
|
|
with pytest.raises(ValueError, match='single-env'):
|
|
binding.normalize_config(fixture.cfg)
|
|
for count in (1, 127, 129):
|
|
cfg.scene.num_envs = count
|
|
with pytest.raises(ValueError, match='exact128'):
|
|
get()
|
|
with pytest.raises(ValueError, match='Unsupported normalization contract'):
|
|
binding._typed_normalization_updates(cfg, name, 'RootXYContactCycleEnvCfg', 129)
|
|
|
|
|
|
def scene_backend_methods():
|
|
"""Extract unmodified method bodies; no constructor, Kit or physics import."""
|
|
source = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/scene/interactive_scene.py')
|
|
scene = next(n for n in ast.parse(source.read_text()).body
|
|
if isinstance(n, ast.ClassDef) and n.name == 'InteractiveScene')
|
|
methods = [n for n in scene.body if isinstance(n, ast.FunctionDef)
|
|
and n.name in ('_build_clone_plan_from_cfg', '_add_entities_from_cfg', 'env_ns', 'env_regex_ns')]
|
|
# Only remove the local optional SurfaceGripperCfg import; the unrelated type
|
|
# is supplied as a nonmatching sentinel. All normalization statements execute.
|
|
for method in methods:
|
|
method.body = [n for n in method.body if not isinstance(n, ast.ImportFrom)]
|
|
future = ast.parse('from __future__ import annotations').body
|
|
return compile(ast.fix_missing_locations(ast.Module(body=future + methods, type_ignores=[])), str(source), 'exec')
|
|
|
|
|
|
@pytest.mark.parametrize('replicate', [True, False])
|
|
def test_normalization_parity_with_pinned_scene_methods(fixture, replicate):
|
|
scene = fixture.cfg.scene
|
|
scene.replicate_physics = replicate
|
|
scene.robot.prim_path = '{ENV_REGEX_NS}/Robot'
|
|
scene.foot_contact.prim_path = '{ENV_REGEX_NS}/Robot/foot'
|
|
scene.foot_contact.filter_prim_paths_expr += ['{ENV_REGEX_NS}/Robot/other']
|
|
scene.foot_contact.sensor_shape_prim_expr = ['{ENV_REGEX_NS}/Robot/foot/shape']
|
|
scene.foot_contact.filter_shape_prim_expr = ['{ENV_REGEX_NS}/Robot/other/shape']
|
|
for name in binding.SPAWNER_TYPES:
|
|
getattr(scene, name).spawn.spawn_path = None
|
|
scene.mount_wrench.prim_path = '{ENV_REGEX_NS}/Robot'
|
|
original = copy.deepcopy(scene)
|
|
binding.normalize_config(fixture.cfg)
|
|
normalized = binding.canonical_config(scene)
|
|
binding.normalize_config(fixture.cfg)
|
|
assert binding.canonical_config(scene) == normalized
|
|
unrelated = type('Unrelated', (), {})
|
|
types = {name: binding._imported_symbol(*symbol) for name, symbol in binding.SCENE_TYPES.items()}
|
|
globals_ = {
|
|
'InteractiveSceneCfg': binding._imported_symbol('isaaclab.scene.interactive_scene_cfg', 'InteractiveSceneCfg'),
|
|
'SensorBaseCfg': (types['foot_contact'], types['mount_wrench']), 'ContactSensorCfg': types['foot_contact'],
|
|
'AssetBaseCfg': types['ground'], 'ArticulationCfg': types['robot'],
|
|
'sim_utils': SimpleNamespace(MultiAssetSpawnerCfg=unrelated, MultiUsdFileCfg=unrelated),
|
|
'torch': SimpleNamespace(ones=lambda *a, **k: 'homogeneous-mask', bool=bool),
|
|
'cloner': SimpleNamespace(ClonePlan=lambda **kw: kw),
|
|
'FrameView': lambda *a, **kw: None,
|
|
}
|
|
for name in ('RigidObjectCollectionCfg', 'TerrainImporterCfg', 'DeformableObjectCfg', 'RigidObjectCfg',
|
|
'SurfaceGripperCfg', 'FrameTransformerCfg', 'VisuoTactileSensorCfg'):
|
|
globals_[name] = unrelated
|
|
exec(scene_backend_methods(), globals_)
|
|
plans = []
|
|
for candidate in (original, scene):
|
|
# Only constructors/spawn side effects are replaced; paths and clone-plan
|
|
# branch predicates are the installed source's actual statements.
|
|
for name in binding.SCENE_TYPES:
|
|
entity = getattr(candidate, name)
|
|
entity._test_class_type = getattr(entity, "class_type", None)
|
|
entity.class_type = lambda cfg: None
|
|
if name in ('ground', 'light'):
|
|
entity.init_state = SimpleNamespace(pos=(0, 0, 0), rot=(0, 0, 0, 1))
|
|
entity.spawn.func = lambda *a, **kw: None
|
|
backend = SimpleNamespace(cfg=candidate, env_ns='/World/envs', env_regex_ns='/World/envs/env_.*',
|
|
env_fmt='/World/envs/env_{}', num_envs=1, device='cpu', stage=None,
|
|
_articulations={}, _sensors={}, _extras={})
|
|
plans.append(globals_['_build_clone_plan_from_cfg'](backend))
|
|
globals_['_add_entities_from_cfg'](backend)
|
|
for name in binding.SCENE_TYPES:
|
|
entity = getattr(candidate, name)
|
|
previous = entity._test_class_type
|
|
del entity._test_class_type
|
|
if previous is None:
|
|
del entity.class_type
|
|
else:
|
|
entity.class_type = previous
|
|
if name in ('ground', 'light'):
|
|
del entity.init_state
|
|
del entity.spawn.func
|
|
assert binding.canonical_config(candidate) == normalized
|
|
assert plans[0] == plans[1] == {'sources': ('/World/envs/env_0',),
|
|
'destinations': ('/World/envs/env_{}',), 'clone_mask': 'homogeneous-mask'}
|
|
assert scene.replicate_physics is replicate
|
|
assert globals_['env_ns'].fget(None) == '/World/envs'
|
|
assert globals_['env_regex_ns'].fget(SimpleNamespace(env_ns='/World/envs')) == '/World/envs/env_.*'
|
|
|
|
|
|
def test_postconstruction_mutation_rejected_without_repair(fixture):
|
|
manifest = prepare(fixture)
|
|
fixture.cfg.scene.foot_contact.filter_prim_paths_expr.append('/World/changed')
|
|
with pytest.raises(ValueError, match='binding mismatch'):
|
|
verify(fixture, manifest)
|
|
fixture.cfg.scene.robot.prim_path = '{ENV_REGEX_NS}/Robot'
|
|
with pytest.raises(ValueError, match='preconstruction'):
|
|
verify(fixture, manifest)
|
|
assert fixture.cfg.scene.robot.prim_path == '{ENV_REGEX_NS}/Robot'
|
|
|
|
|
|
@pytest.mark.parametrize('missing', ['argument', 'empty', 'modules', 'artifacts', 'native', 'core'])
|
|
def test_explicit_reviewed_dependency_inventory_required(fixture, missing):
|
|
inventory = fixture.args['dependency_inventory']
|
|
if missing == 'argument':
|
|
del fixture.args['dependency_inventory']
|
|
with pytest.raises(TypeError, match='dependency_inventory'):
|
|
prepare(fixture)
|
|
return
|
|
if missing == 'empty':
|
|
fixture.args['dependency_inventory'] = {}
|
|
elif missing in ('modules', 'artifacts'):
|
|
inventory[missing] = {}
|
|
elif missing == 'native':
|
|
inventory['artifacts'].pop('torch/lib')
|
|
else:
|
|
inventory['modules'].pop('numpy.core._multiarray_umath')
|
|
with pytest.raises(ValueError, match='inventory|Missing reviewed'):
|
|
prepare(fixture)
|
|
|
|
|
|
@pytest.mark.parametrize('mutation', ['file', 'path', 'spec', 'missing', 'artifact'])
|
|
def test_external_numerical_substitutions_rejected(fixture, mutation):
|
|
manifest = prepare(fixture)
|
|
module = sys.modules['torch._C']
|
|
if mutation == 'file':
|
|
Path(module.__file__).write_text('# replaced extension implementation')
|
|
elif mutation == 'path':
|
|
path = fixture.root / 'substituted.py'
|
|
path.write_bytes(Path(module.__file__).read_bytes())
|
|
fixture.monkeypatch.setattr(module, '__file__', str(path))
|
|
fixture.monkeypatch.setattr(module.__spec__, 'origin', str(path))
|
|
elif mutation == 'spec':
|
|
fixture.monkeypatch.setattr(module.__spec__, 'origin', '/other/tree')
|
|
elif mutation == 'missing':
|
|
fixture.monkeypatch.delitem(sys.modules, 'torch._C')
|
|
elif mutation == 'artifact':
|
|
Path(fixture.args['dependency_inventory']['artifacts']['warp/bin']['path']).write_bytes(b'replaced')
|
|
else:
|
|
path = fixture.root / 'late_numerical.py'
|
|
path.write_text('# late implementation')
|
|
load('torch.late', path, fixture.monkeypatch)
|
|
with pytest.raises(ValueError):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_provider_outside_project_namespaces_and_future_scope_artifact(fixture):
|
|
path = fixture.root / 'external_provider.py'
|
|
path.write_text('def read_scope():\n return None\n')
|
|
provider = load('external_provider', path, fixture.monkeypatch)
|
|
fixture.monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', {**binding.REQUIRED_RUNTIME,
|
|
'external_provider': ('test-provider', str(path), binding.sha256(path.read_bytes()))})
|
|
fixture.monkeypatch.setattr(sys.modules['synthetic_instrumentation'], 'provider', provider, raising=False)
|
|
with pytest.raises(ValueError, match='Missing reviewed dependency modules'):
|
|
prepare(fixture)
|
|
inventory = fixture.args['dependency_inventory']
|
|
inventory['modules']['external_provider'] = {'path': str(path), 'sha256': binding.sha256(path.read_bytes())}
|
|
scope = fixture.root / 'reviewed_scope.json'
|
|
scope.write_text('{"scope":"test-only"}')
|
|
inventory['artifacts']['provider_scope'] = {'module': 'external_provider', 'path': str(scope),
|
|
'sha256': binding.sha256(scope.read_bytes()),
|
|
'purpose': 'explicit provider evidence-scope document'}
|
|
manifest = prepare(fixture)
|
|
assert 'external_provider' in json.loads(manifest)['dependency_inventory']['modules']
|
|
scope.write_text('{"scope":"substituted"}')
|
|
with pytest.raises(ValueError, match='path/hash mismatch'):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
def test_from_provider_import_dependency_missing(fixture):
|
|
path = fixture.root / 'external_provider.py'
|
|
path.write_text('def read_state():\n return None\n')
|
|
provider = load('external_provider', path, fixture.monkeypatch)
|
|
fixture.monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', {**binding.REQUIRED_RUNTIME,
|
|
'external_provider': ('test-provider', str(path), binding.sha256(path.read_bytes()))})
|
|
fixture.monkeypatch.setattr(sys.modules['synthetic_instrumentation'], 'read_state', provider.read_state,
|
|
raising=False)
|
|
with pytest.raises(ValueError, match='external_provider'):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_installed_numerical_native_layouts_read_only():
|
|
site = Path('/home/cen/miniconda3/envs/linker_sim_isaac/lib/python3.11/site-packages')
|
|
assert (site / 'torch/lib/libtorch_cpu.so').is_file()
|
|
assert (site / 'torch/lib/libc10.so').is_file()
|
|
assert list((site / 'numpy/core').glob('_multiarray_umath*.so'))
|
|
assert list((site / 'numpy/random').glob('mtrand*.so'))
|
|
assert list((site / 'numpy.libs').glob('*openblas*.so'))
|
|
assert (site / 'warp/bin/warp.so').is_file()
|
|
assert (site / 'warp/bin/warp-clang.so').is_file()
|
|
context = (site / 'warp/_src/context.py').read_text()
|
|
assert 'warp_lib = os.path.join(bin_path, "warp.so")' in context
|
|
assert 'self.core = self.load_dll(warp_lib)' in context
|
|
|
|
|
|
@pytest.fixture
|
|
def actual_clone_targets(fixture, source_realistic_config):
|
|
def extracted(name, source, symbol, prefix):
|
|
tree = ast.parse(Path(source).read_text())
|
|
node = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == symbol)
|
|
path = binding.ISAAC_ROOT / ('source/isaaclab/' + name.replace('.', '/') + '.py')
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text('from __future__ import annotations\n' + prefix + '\n' + ast.unparse(node) + '\n')
|
|
return load(name, path, fixture.monkeypatch)
|
|
|
|
name = 'isaaclab.sim.utils.prims'
|
|
relative, _ = binding.ISAAC_PINS[name]
|
|
decorator = extracted(name, Path('/home/cen/IsaacLab') / relative, 'clone', 'import functools')
|
|
binding.ISAAC_PINS[name] = (relative, binding.sha256(Path(decorator.__file__).read_bytes()))
|
|
modules = []
|
|
for name, symbol in (('isaaclab.sim.spawners.from_files.from_files', 'spawn_from_usd'),
|
|
('isaaclab.sim.spawners.lights.lights', 'spawn_light')):
|
|
source = Path('/home/cen/IsaacLab/source/isaaclab') / (name.replace('.', '/') + '.py')
|
|
module = extracted(name, source, symbol, 'from isaaclab.sim.utils.prims import clone')
|
|
modules.append((module, symbol))
|
|
_, cfg = source_realistic_config
|
|
rs = sys.modules['isaaclab.utils.string'].ResolvableString
|
|
cfg.spawn_func = rs(modules[0][0].__name__ + ':' + modules[0][1])
|
|
cfg.ui_window_class_type = rs(modules[1][0].__name__ + ':' + modules[1][1])
|
|
return cfg, modules, decorator
|
|
|
|
|
|
def test_real_clone_decorated_spawner_reference_identity(fixture, actual_clone_targets):
|
|
cfg, modules, _ = actual_clone_targets
|
|
binding.normalize_config(fixture.cfg)
|
|
manifest = prepare(fixture)
|
|
encoded = json.loads(binding.canonical_config(cfg.spawn_func))
|
|
target = encoded['target']['symbol']
|
|
assert target['qualname'] == 'spawn_from_usd'
|
|
assert target['clone_wrapper']['decorator']['qualname'] == 'clone'
|
|
assert target['clone_wrapper']['freevars'] == ['func']
|
|
assert target['clone_wrapper']['wrapped']['qualname'] == 'spawn_from_usd'
|
|
for module, symbol in modules:
|
|
wrapper = getattr(module, symbol)
|
|
assert wrapper.__closure__[0].cell_contents is wrapper.__wrapped__
|
|
assert wrapper.__code__.co_freevars == ('func',)
|
|
verify(fixture, manifest)
|
|
|
|
|
|
@pytest.mark.parametrize('mutation', ['forged', 'cell', 'underlying_code', 'chain', 'multiple_cells', 'wrapped'])
|
|
def test_clone_wrapper_layout_rejects_forgery(fixture, actual_clone_targets, mutation):
|
|
cfg, modules, decorator = actual_clone_targets
|
|
module, symbol = modules[0]
|
|
wrapper = getattr(module, symbol)
|
|
target = wrapper.__wrapped__
|
|
if mutation == 'forged':
|
|
import functools
|
|
@functools.wraps(wrapper)
|
|
def forged(*args, **kwargs):
|
|
return wrapper(*args, **kwargs)
|
|
fixture.monkeypatch.setattr(module, symbol, forged)
|
|
elif mutation == 'cell':
|
|
wrapper.__closure__[0].cell_contents = getattr(modules[1][0], modules[1][1]).__wrapped__
|
|
elif mutation == 'underlying_code':
|
|
target.__code__ = sys.modules['synthetic_runner'].run.__code__
|
|
elif mutation == 'chain':
|
|
fixture.monkeypatch.setattr(module, symbol, decorator.clone(wrapper))
|
|
elif mutation == 'multiple_cells':
|
|
import functools
|
|
other = 1
|
|
@functools.wraps(wrapper)
|
|
def two_cells(*args, **kwargs):
|
|
return wrapper(*args, **kwargs), other
|
|
fixture.monkeypatch.setattr(module, symbol, two_cells)
|
|
else:
|
|
wrapper.__wrapped__ = getattr(modules[1][0], modules[1][1]).__wrapped__
|
|
with pytest.raises(ValueError):
|
|
binding.normalize_config(fixture.cfg)
|
|
assert cfg.spawn_func._resolved_callable is None
|
|
|
|
|
|
def test_provider_module_object_substitution_rejected(fixture):
|
|
path = fixture.root / 'external_provider.py'
|
|
path.write_text('def read_state():\n return None\n')
|
|
provider = load('external_provider', path, fixture.monkeypatch)
|
|
fixture.monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', {**binding.REQUIRED_RUNTIME,
|
|
'external_provider': ('test-provider', str(path), binding.sha256(path.read_bytes()))})
|
|
fixture.args['dependency_inventory']['modules']['external_provider'] = {
|
|
'path': str(path), 'sha256': binding.sha256(path.read_bytes())}
|
|
from types import ModuleType
|
|
substituted = ModuleType('external_provider')
|
|
assert substituted is not provider
|
|
fixture.monkeypatch.setattr(sys.modules['synthetic_instrumentation'], 'provider', substituted, raising=False)
|
|
with pytest.raises(ValueError, match='Substituted imported dependency'):
|
|
prepare(fixture)
|
|
|
|
|
|
def cuda_inventory(fixture):
|
|
inventory = fixture.args['dependency_inventory']
|
|
site = Path(inventory['modules']['torch']['path']).parent.parent
|
|
cuda = site / 'torch/lib/libtorch_cuda.so'
|
|
cuda.write_bytes(b'synthetic-torch-cuda')
|
|
inventory['artifacts']['torch_cuda'] = {'module': 'torch', 'path': str(cuda),
|
|
'sha256': binding.sha256(cuda.read_bytes()),
|
|
'purpose': 'Torch CUDA implementation'}
|
|
for package in ('cuda_runtime', 'cublas', 'cudnn', 'nvjitlink'):
|
|
path = site / 'nvidia' / package / 'lib' / f'lib{package}.so.12'
|
|
path.parent.mkdir(parents=True)
|
|
path.write_bytes(b'synthetic-nvidia-library')
|
|
inventory['artifacts'][package] = {'module': 'torch', 'vendor': 'NVIDIA', 'path': str(path),
|
|
'sha256': binding.sha256(path.read_bytes()),
|
|
'purpose': 'NVIDIA CUDA dependency consumed by Torch'}
|
|
return inventory
|
|
|
|
|
|
def test_explicit_vendor_cuda_inventory_and_cpu_only_layout(fixture):
|
|
cpu = prepare(fixture) # no libtorch_cuda.so, no vendor dirs required
|
|
inventory = cuda_inventory(fixture)
|
|
manifest = prepare(fixture)
|
|
assert manifest != cpu
|
|
verify(fixture, manifest)
|
|
assert json.loads(manifest)['dependency_inventory']['artifacts']['cublas']['vendor'] == 'NVIDIA'
|
|
Path(inventory['artifacts']['nvjitlink']['path']).write_bytes(b'substituted vendor implementation')
|
|
with pytest.raises(ValueError, match='path/hash mismatch'):
|
|
verify(fixture, manifest)
|
|
|
|
|
|
@pytest.mark.parametrize('mutation', ['entry', 'vendor_label', 'directory', 'symlink'])
|
|
def test_missing_or_aliased_vendor_cuda_artifacts_rejected(fixture, mutation):
|
|
inventory = cuda_inventory(fixture)
|
|
artifact = inventory['artifacts']['cublas']
|
|
path = Path(artifact['path'])
|
|
if mutation == 'entry':
|
|
del inventory['artifacts']['cublas']
|
|
elif mutation == 'vendor_label':
|
|
del artifact['vendor']
|
|
elif mutation == 'directory':
|
|
path.unlink()
|
|
del inventory['artifacts']['cublas']
|
|
else:
|
|
target = path.with_name('actual_library.so')
|
|
path.rename(target)
|
|
path.symlink_to(target)
|
|
with pytest.raises(ValueError, match='native artifact|CUDA native layout|Noncanonical'):
|
|
prepare(fixture)
|
|
|
|
|
|
def test_loaded_scene_inherited_mount_wrench_actual_config_source(fixture, source_realistic_config):
|
|
"""Execute only pinned config definitions and frozen LoadedSceneCfg class AST.
|
|
|
|
No sensor constructor, Isaac package initialization or backend is executed.
|
|
"""
|
|
for name in ('isaaclab.sensors.sensor_base_cfg',
|
|
'isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg'):
|
|
relative, _ = binding.ISAAC_PINS[name]
|
|
path = Path(sys.modules[name].__file__)
|
|
path.write_bytes((Path('/home/cen/IsaacLab') / relative).read_bytes())
|
|
load(name, path, fixture.monkeypatch)
|
|
binding.ISAAC_PINS[name] = (relative, binding.sha256(path.read_bytes()))
|
|
name = 'isaaclab.sensors.joint_wrench.joint_wrench_sensor'
|
|
path = fixture.root / 'sensor_factory_double.py'
|
|
path.write_text('class JointWrenchSensor:\n pass\n')
|
|
load(name, path, fixture.monkeypatch)
|
|
source = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/loaded_leg/cfg.py'
|
|
node = next(n for n in ast.parse(source.read_text()).body
|
|
if isinstance(n, ast.ClassDef) and n.name == 'LoadedSceneCfg')
|
|
path = fixture.root / 'actual_loaded_scene.py'
|
|
path.write_text('from isaaclab.utils.configclass import configclass\n'
|
|
'from isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg import JointWrenchSensorCfg\n'
|
|
f'from {CFG_NAME} import LegSceneCfg\n' + ast.unparse(node) + '\n')
|
|
actual = load('actual_loaded_scene', path, fixture.monkeypatch)
|
|
fixture.cfg.scene = actual.LoadedSceneCfg()
|
|
sensor = fixture.cfg.scene.mount_wrench
|
|
assert set(vars(sensor)) == {'class_type', 'prim_path', 'update_period', 'debug_vis', 'convention'}
|
|
assert sensor.prim_path == '{ENV_REGEX_NS}/Robot'
|
|
binding.normalize_config(fixture.cfg)
|
|
assert sensor.prim_path == '/World/envs/env_.*/Robot'
|
|
assert sensor.update_period == 0.0 and sensor.convention == 'incoming_joint_frame'
|
|
assert sensor.class_type._resolved_callable is sys.modules[name].JointWrenchSensor
|
|
before = binding.canonical_config(fixture.cfg)
|
|
binding.normalize_config(fixture.cfg)
|
|
assert binding.canonical_config(fixture.cfg) == before
|
|
|
|
|
|
@pytest.mark.parametrize('field,value', [('prim_path', '{ENV_REGEX_NS}/Robot/other'),
|
|
('update_period', .1), ('debug_vis', True),
|
|
('convention', 'world'), ('class_type', 'other:Sensor')])
|
|
def test_mount_wrench_only_exact_inherited_sensor_supported(fixture, field, value):
|
|
setattr(fixture.cfg.scene.mount_wrench, field, value)
|
|
with pytest.raises(ValueError, match='mount_wrench'):
|
|
binding.normalize_config(fixture.cfg)
|
|
|
|
|
|
def test_binding_mismatch_reports_exact_actual_config_without_mutating_it(fixture):
|
|
manifest = prepare(fixture)
|
|
fixture.cfg.sim['dt'] = .004
|
|
with pytest.raises(binding.BindingMismatchError) as caught:
|
|
verify(fixture, manifest)
|
|
report = caught.value.binding_diagnostic
|
|
assert report['expected_sha256'] == binding.sha256(manifest)
|
|
assert report['actual_sha256'] == binding.sha256(prepare(fixture))
|
|
assert report['difference_count'] == 1 and report['omitted_difference_count'] == 0
|
|
difference = report['differences'][0]
|
|
assert difference['path'] == '/effective_config/fields/mapping/sim/mapping/dt/float'
|
|
assert difference['expected']['value'] == (.002).hex()
|
|
assert difference['actual']['value'] == (.004).hex()
|
|
assert fixture.cfg.sim['dt'] == .004
|
|
|
|
|
|
def test_binding_mismatch_reports_import_identity_and_missing_typed_values():
|
|
expected = {'imports': {'a/b~c': {'sha256': 'a' * 64}}, 'values': [True, None, 9]}
|
|
actual = {'imports': {'a/b~c': {'sha256': 'b' * 64}}, 'values': [1, None]}
|
|
report = binding.binding_difference(binding.canonical_json(expected), binding.canonical_json(actual))
|
|
differences = {d['path']: d for d in report['differences']}
|
|
assert differences['/imports/a~1b~0c/sha256']['actual']['value'] == 'b' * 64
|
|
assert differences['/values/0']['expected']['value'] is True
|
|
assert differences['/values/0']['actual']['value'] == 1
|
|
assert differences['/values/2']['actual'] == {'present': False}
|
|
assert report['difference_count'] == 3
|
|
|
|
|
|
def test_binding_mismatch_diagnostic_is_bounded_and_hashes_large_values_and_paths():
|
|
expected = {str(i): 'a' * 10000 for i in range(200)}
|
|
actual = {str(i): 'b' * 10000 for i in range(200)}
|
|
report = binding.binding_difference(binding.canonical_json(expected), binding.canonical_json(actual))
|
|
assert len(binding.canonical_json(report)) <= binding.MISMATCH_MAX_BYTES
|
|
assert report['difference_count'] == 200
|
|
assert len(report['differences']) == binding.MISMATCH_MAX_DIFFERENCES
|
|
assert report['omitted_difference_count'] == 200 - binding.MISMATCH_MAX_DIFFERENCES
|
|
assert all('value' not in d['actual'] for d in report['differences'])
|
|
assert report['differences'][0]['actual']['sha256'] == binding.sha256(binding.canonical_json('b' * 10000))
|
|
path = '/' + 'x' * 2000
|
|
report = binding.binding_difference(binding.canonical_json({path[1:]: 1}),
|
|
binding.canonical_json({path[1:]: 2}))
|
|
assert report['differences'][0]['path_sha256'] == binding.sha256(path.encode())
|
|
|
|
|
|
def test_verify_binding_reports_new_canonical_import_identity(fixture):
|
|
manifest = prepare(fixture)
|
|
name = 'isaaclab.constructor_only'
|
|
path = binding.ISAAC_ROOT / 'source/isaaclab/isaaclab/constructor_only.py'
|
|
path.write_text('# synthetic constructor-only import\n')
|
|
load(name, path, fixture.monkeypatch)
|
|
with pytest.raises(binding.BindingMismatchError) as caught:
|
|
verify(fixture, manifest)
|
|
report = caught.value.binding_diagnostic
|
|
difference = next(d for d in report['differences'] if d.get('path') == '/imports/' + name)
|
|
assert difference['expected'] == {'present': False}
|
|
record = json.loads(prepare(fixture))['imports'][name]
|
|
assert difference['actual']['sha256'] == binding.sha256(binding.canonical_json(record))
|
|
|
|
|
|
def test_binding_mismatch_byte_limit_is_enforced_before_record_limit():
|
|
expected = {'x' * 900 + str(i): 'a' * 200 for i in range(64)}
|
|
actual = {'x' * 900 + str(i): 'b' * 200 for i in range(64)}
|
|
report = binding.binding_difference(binding.canonical_json(expected), binding.canonical_json(actual))
|
|
assert len(binding.canonical_json(report)) <= binding.MISMATCH_MAX_BYTES
|
|
assert 0 < len(report['differences']) < binding.MISMATCH_MAX_DIFFERENCES
|
|
assert report['difference_count'] == 64
|
|
assert report['omitted_difference_count'] == 64 - len(report['differences'])
|