"""Finite v5 development scope and shutdown ownership; CPU only, never Kit.""" import json import sys from pathlib import Path from types import ModuleType from types import SimpleNamespace as NS import pytest from test_contact_cycle_sim_evaluation_binding import binding, fixture, load, prepare, verify # noqa: F401 from test_contact_cycle_sim_evaluation_runner import cli from test_contact_cycle_sim_preparation_supervisor import prep def test_exact_critical_role_list_and_pins_read_only(): assert set(binding.REQUIRED_RUNTIME) == { 'torch', 'torch._C', 'torch.random', 'torch.cuda', 'numpy', 'numpy.random', 'numpy.random.mtrand', 'numpy._core._multiarray_umath', 'warp', 'warp._src.context', 'warp._src.types', 'warp._src.builtins', 'carb', 'omni.kit.app', 'omni.physics.tensors', 'omni.physx', 'pxr.Tf', 'pxr.Gf', 'pxr.Sdf', 'pxr.Usd', 'pxr.UsdGeom', 'pxr.UsdLux', 'pxr.UsdPhysics', 'pxr.UsdShade', 'pxr.UsdUtils', 'pxr.Vt', 'pxr.PhysxSchema', 'pxr.PhysicsSchemaTools', 'isaaclab.sim.simulation_context', 'omni.ext._impl.fast_importer', 'simulation_app.simulation_app', 'isaaclab_physx.physics.physx_manager', 'isaaclab.physics.physics_manager', 'omni.timeline', 'omni.usd', 'omni.physics.tensors.api', 'omni.physics.tensors.bindings._physicsTensors', 'omni.physx.bindings._physx', } for role, filename, digest in binding.REQUIRED_RUNTIME.values(): assert role and binding.sha256(Path(filename).read_bytes()) == digest for filename, digest in binding.RUNTIME_LAYOUT_PINS.items(): assert binding.sha256(Path(filename).read_bytes()) == digest def test_unrelated_scipy_metadata_never_execution_dependency(fixture): # noqa: F811 manifest = prepare(fixture) module = ModuleType('scipy.incidental') def function(): return None function.__module__ = 'View.MemoryView' module.cython_function = function module.native_container = ModuleType('_unimportable_native_container') fixture.monkeypatch.setitem(sys.modules, module.__name__, module) fixture.monkeypatch.setattr(sys.modules['torch'], 'unrelatedSciPy', module, raising=False) verify(fixture, manifest) required = binding.required_dependencies(set(), set()) assert not {'View.MemoryView', '_unimportable_native_container', module.__name__} & required observations = binding.incidental_observations(set(sys.modules) - {module.__name__}) row = observations[module.__name__] assert row['status'] == 'observational-not-verified' assert row['limitation'] == 'unverified-dynamic-internals' assert 'sha256' not in row and 'observed_file' not in row @pytest.mark.parametrize('mutation', ['missing', 'foreign', 'swapped', 'fileless']) def test_required_core_module_identity_fails(fixture, mutation): # noqa: F811 manifest = prepare(fixture) name = 'torch._C' if mutation == 'missing': fixture.monkeypatch.delitem(sys.modules, name) elif mutation == 'fileless': fixture.monkeypatch.setitem(sys.modules, name, ModuleType(name)) else: path = fixture.root / 'foreign_core.py' path.write_text('# not the declared core\n') replacement = load(name, path, fixture.monkeypatch) if mutation == 'swapped': replacement.__name__ = 'not_torch_core' with pytest.raises(ValueError): verify(fixture, manifest) def preparation_fixture(tmp_path, monkeypatch): project = tmp_path / 'project' frozen = project / 'logs/contact_cycle/sim_residual_training_v1/prepared01' frozen.mkdir(parents=True) (frozen / 'candidate.pt').write_bytes(b'CPU-only not a policy') source = project / 'required.py' source.write_text('VALUE = 1\n') monkeypatch.setattr(cli, 'ROOT', project) monkeypatch.setattr(cli, 'launch_identity', lambda: {'scope': 'CPU fixture'}) monkeypatch.setattr(prep.r, 'ROOT', project) manifest = json.dumps({'imports': {}, 'sources': [{'path': str(source), 'sha256': binding.sha256(source.read_bytes())}]}).encode() cfg = NS(value=1) checks = [] def check(*_, **arguments): assert arguments['effective_config'] is cfg checks.append(cfg.value) if cfg.value != 1: raise ValueError('preclose config mutation') runtime = NS(cfg=cfg, binding=NS(FORMAT='CPU-v5', PROVENANCE_SCOPE='development', PROVENANCE_LIMITATIONS=(), prepare_binding=lambda **_: manifest, verify_binding=check, incidental_observations=lambda _: {}), arguments={'effective_config': cfg, 'dependency_inventory': {'modules': {}}}, app=NS(close=lambda **_: None)) # Lifecycle fixture, not an installed PhysX export qualification. Exercise # the real metadata comparison with a synthetic resolver at that boundary; # actual export identity/tamper coverage lives in constructor_export tests. runtime.constructor_export = {'scope': 'CPU synthetic constructor export'} runtime.generator_inventory = {'constructor_export': dict(runtime.constructor_export)} def constructor_export(bound, *, expected): assert bound is runtime.binding and expected is runtime.constructor_export return None, dict(expected) monkeypatch.setattr(cli, 'physx_constructor_export', constructor_export) monkeypatch.setattr(cli, 'real_runtime', lambda *_: runtime) monkeypatch.setattr(cli, 'runtime_metadata', lambda _: {}) root = tmp_path / 'campaign' root.mkdir() args = NS(output=root / 'prep-p1', inventory=root / 'inventory', trial_index=0, artifact_root=root) baseline = {str(p): binding.sha256(p.read_bytes()) for p in frozen.iterdir()} return runtime, args, source, baseline, checks def test_fast_shutdown_interpreter_exit_after_durable_evidence(tmp_path, monkeypatch): runtime, args, _, baseline, checks = preparation_fixture(tmp_path, monkeypatch) def close(*, exit_code): assert exit_code == 0 and checks == [1] assert json.loads((args.output / 'preparation.json').read_bytes())['preclose_identity_verified'] assert (args.output / 'preclose-filesystem.json').is_file() raise SystemExit(0) # No child code after this point may be required. runtime.app.close = close with pytest.raises(SystemExit) as stopped: cli.prepare(args) assert stopped.value.code == 0 and checks == [1] assert prep.postexit_identity(args.artifact_root, baseline)['status'] == 'PASS' assert checks == [1] # external owner never claims a destroyed cfg object check def test_preclose_config_mutation_persists_failure_and_nonzero(tmp_path, monkeypatch): runtime, args, _, baseline, _ = preparation_fixture(tmp_path, monkeypatch) monkeypatch.setattr(cli, 'runtime_metadata', lambda _: setattr(runtime.cfg, 'value', 2) or {}) codes = [] runtime.app.close = lambda *, exit_code: codes.append(exit_code) with pytest.raises(ValueError, match='config mutation'): cli.prepare(args) assert codes == [1] assert (args.output / 'preparation-failure.json').is_file() with pytest.raises((ValueError, FileNotFoundError)): prep.postexit_identity(args.artifact_root, baseline) @pytest.mark.parametrize('mutation', ['source', 'frozen', 'missing-evidence', 'nonzero']) def test_parent_postexit_identity_cannot_turn_tamper_or_failure_into_pass(tmp_path, monkeypatch, mutation): _, args, source, baseline, _ = preparation_fixture(tmp_path, monkeypatch) assert cli.prepare(args) == 0 if mutation == 'source': source.write_text('VALUE = 2\n') elif mutation == 'frozen': Path(next(iter(baseline))).write_bytes(b'changed') elif mutation == 'missing-evidence': (args.output / 'preparation.json').unlink() errors = ['preparation_child_exit_139'] if mutation == 'nonzero' else [] postexit = prep.collect_postexit_identity( args.artifact_root, baseline, NS(returncode=139), errors, prep.b.boottime() + 5) if mutation == 'nonzero': assert postexit['status'] == 'PASS' and errors == ['preparation_child_exit_139'] else: assert postexit is None and errors def test_incidental_native_pseudo_module_observes_owner_not_fake_module_hash(tmp_path, monkeypatch): binary = tmp_path / '_core.so' binary.write_bytes(b'CPU-only non-executable binary fixture') monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', { 'native._core': ('test-core', str(binary), binding.sha256(binary.read_bytes()))}) module = ModuleType('native._core.pseudo') module.__file__ = str(binary) monkeypatch.setitem(sys.modules, module.__name__, module) row = binding.incidental_observations(set(sys.modules) - {module.__name__})[module.__name__] assert row['observed_binary_owner'] == 'native._core' assert row['observed_file']['sha256'] == binding.sha256(binary.read_bytes()) assert row['status'] == 'observational-not-verified' and 'sha256' not in row