"""CPU-only v4 commands, pair boundaries, atomic mailbox, authority and stop seam.""" import copy import importlib import json from pathlib import Path import sys from types import SimpleNamespace import pytest import torch ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'scripts')) import go2_fl_skill as public import skill_launch_v4 as launch from SkillFactory.legs.contact_cycle_sim import skill_v4 as skill from SkillFactory.legs.contact_cycle_sim import skill_mailbox_v4 as mailbox from SkillFactory.legs.contact_cycle_sim.functional_flow_v2 import load_contract from SkillFactory.legs.contact_cycle_sim.evaluation_protocol import Protocol # Reuse the existing local Isaac spies; neither fixture launches Kit or physics. from test_contact_cycle_sim_evaluation_runtime import runtime, advance def base(): return load_contract(ROOT / launch.old.CONFIG) @pytest.mark.parametrize('cycles', range(1, 21)) @pytest.mark.parametrize('controller', ['analytic', 'u256']) def test_skill_v4_exact_cycles_and_endpoint_clocks(cycles, controller): session = skill.SkillSession('validated_nominal', cycles, controller) protocol = skill.SkillProtocol(base(), session) assert protocol.trial(0).total_ticks == cycles * 4500 for cycle in range(cycles): if cycle: session.admit_endpoint(cycle) end = protocol.evidence_clock(0, (cycle + 1) * 4500) assert (end.cycle_index, end.local_tick) == (cycle, 4500) command = protocol.command(0, end, anchor_a_world=(.1, .2, .022), root_origin_world=(0, 0, .3), weight_N=50.) assert command.target_world_m == pytest.approx( (.125, .22, .022) if cycle % 2 == 0 else (.1, .2, .022)) assert protocol.next_command_clock(0, cycles * 4500) is None @pytest.mark.parametrize('sequence', ['S1', 'S2', 'S3']) def test_skill_v4_fixed_A_pair_targets_and_no_seam_jump(sequence): spec = public.make_spec(sequence=sequence) session = skill.SkillSession(spec['command'], 6, 'analytic', spec['queued']) protocol = skill.SkillProtocol(base(), session) args = dict(anchor_a_world=(.1, -.2, .022), root_origin_world=(0, 0, .3), weight_N=50.) original = object.__new__(Protocol) object.__setattr__(original, 'document', protocol.document) for cycle in range(6): command_id = skill.SEQUENCES[sequence][cycle // 2] assert session.pair_commands[cycle // 2] == command_id # Every phase reuses Protocol.command, with only the pair displacement changed. from dataclasses import replace object.__setattr__(original, 'trials', (replace(protocol.trial(0), swing_displacement_m=skill.COMMANDS[command_id]),)) for local in (1, 1499, 1500, 1999, 2299, 2300, 2650, 2999, 3400, 4499, 4500): clock = protocol.evidence_clock(0, cycle * 4500 + local) assert protocol.command(0, clock, **args) == original.command(0, clock, **args) end = protocol.command(0, protocol.evidence_clock(0, (cycle + 1) * 4500), **args) if cycle < 5: session.admit_endpoint(cycle + 1) following = protocol.command(0, protocol.next_command_clock(0, (cycle + 1) * 4500, seam_admitted=True), **args) assert end.target_world_m == following.target_world_m assert end.target_velocity_world_m_s == following.target_velocity_world_m_s assert skill.SEQUENCES['S3'][0] == 'baseline_direction-24mm' @pytest.mark.parametrize('command', list(skill.COMMANDS)) def test_skill_v4_queue_never_changes_outbound_or_return_pair(command): session = skill.SkillSession('validated_nominal', 6, 'analytic') session.queue(command) assert session.active == 'validated_nominal' session.admit_endpoint(1) assert session.active == 'validated_nominal' and session.pair_commands == {0: 'validated_nominal'} session.admit_endpoint(2) assert session.active == command assert session.pair_commands == {0: 'validated_nominal', 1: command} @pytest.mark.parametrize('command,cycles,controller', [ ('INVALID', 3, 'analytic'), (True, 3, 'analytic'), ([], 3, 'analytic'), ('validated_nominal', True, 'analytic'), ('validated_nominal', 0, 'analytic'), ('validated_nominal', 21, 'analytic'), ('validated_nominal', 3., 'analytic'), ('validated_nominal', float('nan'), 'analytic'), ('validated_nominal', 3, 'random')]) def test_skill_v4_invalid_complete_request_rejected_before_launch(tmp_path, monkeypatch, command, cycles, controller): monkeypatch.setattr(public.subprocess, 'Popen', lambda *a, **k: pytest.fail('must not launch')) with pytest.raises(ValueError): public.start(tmp_path / 'never-created', tmp_path / 'no-registration', command=command, cycles=cycles, controller=controller) assert list(tmp_path.iterdir()) == [] def live_mailbox(tmp_path): directory = tmp_path / 'mailbox' directory.mkdir() mailbox.publish(directory / 'status.json', {'state': 'running', 'receipts_count': 0}) return directory def test_skill_v4_mailbox_invalid_is_zero_filesystem_or_session_mutation(tmp_path): directory = live_mailbox(tmp_path) before = {p.name: p.read_bytes() for p in directory.iterdir()} session = skill.SkillSession('validated_nominal', 6, 'analytic') original = copy.deepcopy(session.__dict__) for command in ('invalid', None, 1, True, float('nan'), {'x': .02}): with pytest.raises(ValueError): mailbox.send(directory, 'queue', command) with pytest.raises(ValueError): session.queue(command) assert session.__dict__ == original assert before == {p.name: p.read_bytes() for p in directory.iterdir()} def test_skill_v4_mailbox_one_outstanding_atomic_receipts_and_stop(tmp_path): directory = live_mailbox(tmp_path) session = skill.SkillSession('validated_nominal', 6, 'analytic') request_id = mailbox.send(directory, 'queue', 'x_bias-16mm') with pytest.raises(FileExistsError): mailbox.send(directory, 'stop') receipt = mailbox.consume(directory, session, 1235) assert receipt['accepted'] and receipt['received_tick'] == 1235 assert receipt['request']['id'] == request_id assert session.pending == ['x_bias-16mm'] and session.active == 'validated_nominal' assert mailbox.consume(directory, session, 1240) is None mailbox.send(directory, 'stop') mailbox.consume(directory, session, 1245) assert session.stop_requested and len(session.receipts) == 2 assert not (directory / 'request.lock').exists() assert mailbox.read_json(directory / 'receipt.json')['accepted'] @pytest.mark.parametrize('message', [ {'id': 'a' * 32, 'operation': 'queue', 'command': 'INVALID'}, {'id': 'a' * 32, 'operation': 'stop', 'unknown': True}, {'id': 'a' * 32, 'operation': 'queue', 'command': 'x_bias-16mm', 'cycles': 20}, {'id': 'a' * 32, 'operation': 'reset'}, [], ]) def test_skill_v4_runtime_rejects_malformed_mailbox_before_control_mutation(tmp_path, message): directory = live_mailbox(tmp_path) session = skill.SkillSession('validated_nominal', 6, 'analytic') before = copy.deepcopy(session.__dict__) mailbox.publish(directory / 'request.json', message) receipt = mailbox.consume(directory, session, 5) assert receipt['accepted'] is False assert {k: v for k, v in session.__dict__.items() if k != 'receipts'} == { k: v for k, v in before.items() if k != 'receipts'} def test_skill_v4_mailbox_capacity_status_query_detached_and_not_live(tmp_path): directory = live_mailbox(tmp_path) handle = public.SkillRun(tmp_path) snapshot = handle.query() snapshot['state'] = 'changed' assert handle.query()['state'] == 'running' mailbox.publish(directory / 'status.json', {'state': 'running', 'receipts_count': 64}) with pytest.raises(RuntimeError, match='capacity'): handle.request_stop() mailbox.publish(directory / 'status.json', {'state': 'stopped'}) with pytest.raises(RuntimeError, match='not live'): handle.queue('x_bias-24mm') @pytest.mark.parametrize('endpoint', [1, 2]) def test_skill_v4_boundary_stop_no_extra_physics_or_refresh(runtime, endpoint): session = skill.SkillSession('x_bias-16mm', 3, 'analytic') proto = skill.SkillProtocol(base(), session) events, seams = [], [] hooks = runtime.env.RuntimeHooks(runtime.Sim, []) with hooks: def evidence(event, s): events.append(event) if event.kind == 'tick' and event.clock.global_tick == 5: session.request_stop() if endpoint == 1 else None if event.kind == 'tick' and event.clock.global_tick == 4505 and endpoint == 2: session.request_stop() return () def seam(event, s): seams.append(event) session.admit_endpoint(event.clock.global_tick // 4500) return runtime.env.SeamDecision(True) env = runtime.env.EvaluationContactCycleEnv(runtime.config(), protocol=proto, trial_index=0, runtime_hooks=hooks, evidence_callback=evidence, seam_callback=seam, boundary_stop_callback=lambda: session.stop_requested) env._cycle_state.safety_causes = lambda: () env.reset() reset_calls = hooks.reset_calls result = advance(env, 2700) assert result.stop.reason == 'requested_stop' and not result.stop.causes assert result.stop.physics_ticks == hooks.rollout_steps == endpoint * 4500 assert result.stop.completed_clock_cycles == endpoint and result.stop.partial_control_ticks == 0 assert len(seams) == endpoint - 1 assert events[-1].kind == 'tick' and events[-1].clock.local_tick == 4500 assert env._cycle_state.controller.refreshes.item() == endpoint * 900 assert hooks.reset_calls == reset_calls with pytest.raises(runtime.env.RuntimeViolation): env.step(torch.zeros(1, 3)) def test_skill_v4_failure_precedes_requested_stop_at_endpoint(runtime): session = skill.SkillSession('validated_nominal', 3, 'analytic') with runtime.env.RuntimeHooks(runtime.Sim, []) as hooks: env = runtime.env.EvaluationContactCycleEnv(runtime.config(), protocol=skill.SkillProtocol(base(), session), trial_index=0, runtime_hooks=hooks, evidence_callback=lambda e, s: (), boundary_stop_callback=lambda: True) env._cycle_state.safety_causes = lambda: () env.reset() env._cycle_state.safety_causes = lambda: ('ground_penetration',) result = advance(env, 1) assert result.stop.reason == 'failure' and result.stop.physics_ticks == 1 def registration_file(tmp_path): path = tmp_path / 'registration.json' path.write_text(json.dumps({'format': 'skillfactory.sim-functional-v4-task-registration.v1', 'boot_id': launch.b.boot_id(), 'plan_sha256': launch.b.digest(ROOT / launch.PLAN), 'registered_boottime': launch.b.boottime(), 'deadline_boottime': launch.b.boottime() + 3600, 'limits': {'kit_launches': 24, 'physics_calls': 650000, 'owned_bytes': 24 << 30, 'optimizer_steps': 0, 'gpu_serial': True}})) return path def test_skill_v4_new_authority_exact_sources_limits_no_reuse_and_serial(tmp_path, monkeypatch): registration = registration_file(tmp_path) monkeypatch.setattr(launch, 'capture_gpu', lambda: {'mock': 'CPU only'}) monkeypatch.setattr(launch.old.ownership, 'GPUOwnership', lambda _: None) spec = public.make_spec(sequence='S1') root = tmp_path / 'new-run' with launch.Reservation(registration, root, spec) as reservation: approval = launch.approval(reservation.path, launch.b.digest(reservation.path), root) assert approval['sources'] == launch.sources() assert approval['limits']['max_physics_calls'] == 27250 assert approval['limits']['max_owned_bytes'] == 4 << 30 with pytest.raises(BlockingIOError): with launch.Reservation(registration, tmp_path / 'parallel', spec): pytest.fail('concurrent task launch admitted') with pytest.raises(RuntimeError, match='Consumed root'): with launch.Reservation(registration, root, spec): pytest.fail('root reuse') assert not root.exists() # authority reservation itself never starts Kit/creates run root @pytest.mark.parametrize('fault', ['launches', 'physics', 'storage']) def test_skill_v4_whole_task_budget_denies_before_authority(tmp_path, monkeypatch, fault): registration = registration_file(tmp_path) spec = public.make_spec(sequence='nominal20') runs = [{'root': str(tmp_path / ('old' + str(i))), 'authority': str(tmp_path / ('auth' + str(i))), 'reserved_calls': 0} for i in range(12 if fault == 'launches' else 1)] if fault == 'physics': runs[0]['reserved_calls'] = 650000 (tmp_path / 'skill-v4-ledger.json').write_text(json.dumps({'runs': runs})) if fault == 'storage': monkeypatch.setattr(launch.Reservation, 'check_storage', staticmethod(lambda _: (_ for _ in ()).throw(RuntimeError('storage cap')))) monkeypatch.setattr(launch, 'capture_gpu', lambda: pytest.fail('must reject before GPU baseline')) with pytest.raises(RuntimeError): with launch.Reservation(registration, tmp_path / 'new', spec): pytest.fail('overbudget') assert not (tmp_path / 'new-authority').exists() def test_skill_v4_finite_deadline_and_source_drift_rejection(tmp_path, monkeypatch): registration = registration_file(tmp_path) value = json.loads(registration.read_text()) value['boot_id'] = 'old-boot' registration.write_text(json.dumps(value)) with pytest.raises(PermissionError): launch.registration(registration) spec = public.make_spec(cycles=3, stop_at_tick=5) assert launch.limits(spec)['max_physics_calls'] == 13750 assert launch.limits(spec)['max_physical_seconds'] == 900 assert launch.limits(public.make_spec(sequence='nominal20'))['max_physics_calls'] == 90250 def test_skill_v4_u256_gated_before_mutation(tmp_path): with pytest.raises(PermissionError, match='analytic evidence'): public.require_analytics(public.make_spec(controller='u256', sequence='S1'), []) with pytest.raises(PermissionError, match='analytic evidence'): public.require_analytics(public.make_spec(controller='u256'), []) assert list(tmp_path.iterdir()) == [] def test_skill_v4_explicit_fresh_registration_reusable_bounded(tmp_path): path = tmp_path / 'new-session' / 'registration.json' value = launch.new_registration(path) actual, deadline = launch.registration(path) assert actual == value assert deadline - value['registered_boottime'] == 43200 assert not (path.parent / 'skill-v4-ledger.json').exists() with pytest.raises(ValueError): launch.new_registration(path) value['deadline_boottime'] += 1 path.write_text(json.dumps(value)) with pytest.raises(PermissionError): launch.registration(path) def test_skill_v4_no_frozen_runtime_source_edits(): import hashlib manifest = json.loads((ROOT / 'logs/contact_cycle/sim_residual_training_v1/prepared01/manifest.json').read_text()) for relative, expected in manifest['sources'].items(): if relative != 'AGENT.md': assert hashlib.sha256((ROOT / relative).read_bytes()).hexdigest() == expected, relative @pytest.fixture def skill_recording(recording, monkeypatch): name = recording.__package__ + '.skill_recording_v4' path = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/skill_recording_v4.py' import importlib.util spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) monkeypatch.setitem(sys.modules, name, module) spec.loader.exec_module(module) return module from test_contact_cycle_functional_flow_runtime_v2 import recording def test_skill_v4_recorder_public_probe_receipts_and_early_stop_continuity(skill_recording, tmp_path, monkeypatch): from dataclasses import dataclass directory = live_mailbox(tmp_path) session = skill.SkillSession('validated_nominal', 3, 'analytic') recorder = skill_recording.SkillRecorder(base(), skill.SkillProtocol(base(), session), lambda: None, directory, stop_at_tick=5, reject_at_tick=5) recorder.capture = SimpleNamespace(frames=[None] * 5, state=object()) monkeypatch.setattr(skill_recording, 'carry_snapshot', lambda s: {'CPU_mock_only': True}) recorder.poll() assert session.stop_requested assert session.receipts[0]['source'] == 'public_stop_probe' assert session.receipts[0]['request']['operation'] == 'stop' assert session.receipts[1]['source'] == 'public_rejection_probe' assert session.receipts[1]['accepted'] is False and session.receipts[1]['state_unchanged'] assert session.receipts[1]['carry_before_sha256'] == session.receipts[1]['carry_after_sha256'] assert session.receipts[0]['received_tick'] == session.receipts[1]['received_tick'] == 5 assert not (directory / 'request.json').exists() recorder.capture.frames = [None] * 4500 recorder.initial_counters = {k: 0 for k in skill_recording.FunctionalFlowRecorder.continuity.__globals__['COUNTERS']} hooks = SimpleNamespace(**recorder.initial_counters, violations=[]) hooks.rollout_steps = 4500 recorder.cycles = [{'audit': {'sim_functional_passed': True}}] @dataclass class Stop: reason: str = 'requested_stop' causes: tuple = () completed_clock_cycles: int = 1 continuity = recorder.continuity(Stop(), hooks, 3) assert continuity['runtime_continuity_verified'] and continuity['cycles_completed'] == 1 assert continuity['cycles_requested'] == 3 and continuity['requested_stop'] recorder.pending_seam = {'illegal_extra_refresh': True} assert not recorder.continuity(Stop(), hooks, 3)['runtime_continuity_verified'] def worker_receipt(spec, *, stopped=False): completed = 1 if stopped else spec['cycles'] return {'success': True, 'environment_constructed': True, 'preclose_identity_verified': True, 'runtime_continuity_verified': True, 'skill_spec': spec, 'cycles_completed': completed, 'cycles_requested': spec['cycles'], 'controller_kind': skill.CONTROLLERS[spec['controller']], 'runtime_stop': {'reason': 'requested_stop' if stopped else 'protocol_complete', 'causes': [], 'partial_control_ticks': 0, 'clock': {'global_tick': completed * 4500}}, 'physics_counts': {'rollout_steps': completed * 4500, 'initialization_steps': 0, 'attempted_steps': completed * 4500, 'completed_steps': completed * 4500}, 'hook_violations': [], 'live_receipts': [], 'pair_commands': {str(i): (skill.SEQUENCES.get(spec['sequence']) or (spec['command'],))[ min(i, len(skill.SEQUENCES.get(spec['sequence']) or (spec['command'],)) - 1)] for i in range((completed + 1) // 2)}} @pytest.mark.parametrize('fault', ['none', 'count', 'preclose', 'partial', 'fake_complete', 'missing_rejection']) def test_skill_v4_complete_worker_receipt_requires_physics_and_integrity(fault): spec = public.make_spec(cycles=3) receipt = worker_receipt(spec) assert launch.complete_receipt(receipt, spec) if fault == 'count': receipt['physics_counts']['attempted_steps'] += 1 elif fault == 'preclose': receipt['preclose_identity_verified'] = False elif fault == 'partial': receipt['runtime_stop']['partial_control_ticks'] = 1 elif fault == 'fake_complete': receipt = worker_receipt(spec, stopped=True) receipt['runtime_stop']['reason'] = 'protocol_complete' elif fault == 'missing_rejection': spec['reject_at_tick'] = 5 assert launch.complete_receipt(receipt, spec) == (fault == 'none') @pytest.mark.parametrize('fault', [None, 'prepare', 'supervisor', 'missing_trace', 'counts']) def test_skill_v4_reuses_campaign_supervision_first_failure_wait_cleanup(tmp_path, monkeypatch, fault): import evaluate_cycle_functional_flow as runner import functional_gpu_watch_v3 as watch old = launch.old root = tmp_path / 'mock-run' spec = public.make_spec(sequence='S1') approval = {'limits': launch.limits(spec), 'sources': launch.sources(), 'gpu_identity': {}, 'spec': spec, 'deadline_boottime': old.b.boottime() + 2000} monkeypatch.setattr(launch, 'approval', lambda *a: approval) monkeypatch.setattr(runner.ctypes, 'CDLL', lambda *a, **k: SimpleNamespace(prctl=lambda *a: 0)) monkeypatch.setattr(old, 'native_policy', lambda *a, **k: {}) monkeypatch.setattr(old, 'AsyncMonitor', lambda p: SimpleNamespace(monitor=True, error=None, check=lambda: None, stop=SimpleNamespace(set=lambda: None), report=lambda: {'error': None})) class Owned: known = set() def attach(self, child): pass def discover(self): return {} monkeypatch.setattr(old.owner, 'OwnedProcesses', Owned) cleaned, waited, commands = [], [], [] monkeypatch.setattr(old.owner, 'cleanup_group', lambda *a, **k: cleaned.append(True) or True) monkeypatch.setattr(old.owner, 'StdoutCapture', lambda *a: SimpleNamespace( check=lambda: None, close=lambda: None, pump=lambda: None)) monkeypatch.setattr(old, 'authenticate', lambda *a: None) monkeypatch.setattr(runner, 'filesystem_verify', lambda _: {'scope': 'CPU mock only'}) class GPU: def __init__(self, *a): pass def check(self, *a, **k): return {'clean': True} monkeypatch.setattr(watch, 'ObservedGPUOwnership', GPU) class Child: returncode = 0 stdout = None def __init__(self, command, **kwargs): index = len(commands) commands.append(command) folder = root / ('prepare' if index == 0 else 'run') if index == 0: receipt = {'success': fault != 'prepare', 'environment_constructed': False, 'preclose_identity_verified': True, 'physics_counts': {'attempted_steps': 0, 'completed_steps': 0}} (folder / 'binding.json').write_text(json.dumps({'format': 'skillfactory.go2-fl-skill-binding.v4', 'contract_sha256': approval['sources'][old.CONFIG]})) (folder / 'inventory.json').write_text('{}') else: receipt = worker_receipt(spec) if fault == 'counts': receipt['physics_counts']['completed_steps'] += 1 for filename in ('trace.pt', 'initial-state.pt', 'events-and-seams.json'): if fault == 'missing_trace' and filename == 'trace.pt': continue (folder / filename).write_text('CPU MOCK, NOT PHYSICS') receipt['shutdown_handoff_ready'] = True (folder / 'worker-result.json').write_text(json.dumps(receipt)) self.folder = folder def poll(self): return 0 if (self.folder / 'shutdown-release.json').exists() else None def wait(self, **kwargs): waited.append(True); return 0 monkeypatch.setattr(runner.subprocess, 'Popen', Child) if fault == 'supervisor': def authenticate(*a): if len(commands) == 2: raise RuntimeError('mock GPU supervision failure') monkeypatch.setattr(old, 'authenticate', authenticate) args = SimpleNamespace(output=root, approval=tmp_path / 'unused', trusted_approval_sha256='mock', skill_v4=True, followup_v3=False, skill_reservation=SimpleNamespace(runs=[], check_storage=lambda _: None)) code = runner.campaign(args) result = json.loads((root / 'campaign-result.json').read_text()) count = 1 if fault == 'prepare' else 2 assert len(commands) == len(cleaned) == len(waited) == count assert all(str(ROOT / launch.SCRIPT) in ' '.join(c) for c in commands) assert all('--worker-fd' in c for c in commands) assert result['success'] == (fault is None) and code == (0 if fault is None else 2) status = mailbox.read_json(root / 'mailbox/status.json') assert status['state'] == ('stopped' if fault is None else 'failed') def test_skill_v4_worker_entry_does_not_preimport_private_cpu_planner(monkeypatch): import evaluate_cycle_functional_flow as runner monkeypatch.setattr(launch, 'api', lambda: pytest.fail('planner alias before Kit')) monkeypatch.setattr(launch, 'worker_payload', lambda fd: {'CPU mock': fd}) monkeypatch.setattr(runner, 'worker', lambda payload: 0 if payload == {'CPU mock': 123} else 1) assert public.main(['--worker-fd', '123']) == 0 def test_skill_v4_twentieth_cycle_window_uses_integer_local_ticks(recording): from test_contact_cycle_functional_flow_runtime_v2 import frames recorder = recording.FunctionalFlowRecorder(base(), None, lambda: None) local = frames(2999) recorder.capture = SimpleNamespace(frames=[local[0]] * (19 * 4500) + local) event = SimpleNamespace(clock=SimpleNamespace(local_tick=2999, cycle_index=19)) assert recorder._window_checks(event) == () @pytest.mark.parametrize('overrides', [{'cycles': 21}, {'command': 'INVALID'}, {'queued': ['INVALID']}, {'cycles': 6}]) def test_skill_v4_sequence_never_silently_ignores_invalid_or_conflicting_overrides(overrides): with pytest.raises(ValueError): public.make_spec(sequence='S1', **overrides) def test_skill_v4_api_reports_abnormal_supervisor_exit_without_reusing_live_mailbox(tmp_path): live_mailbox(tmp_path) process = SimpleNamespace(returncode=-9, poll=lambda: -9) handle = public.SkillRun(tmp_path, process) assert handle.query()['state'] == 'failed' assert handle.query()['failure'] == ['supervisor_abnormal_exit'] before = {p.name: p.read_bytes() for p in (tmp_path / 'mailbox').iterdir()} with pytest.raises(RuntimeError, match='already exited'): handle.request_stop() with pytest.raises(RuntimeError, match='already exited'): handle.queue('x_bias-16mm') assert before == {p.name: p.read_bytes() for p in (tmp_path / 'mailbox').iterdir()} @pytest.mark.parametrize('sequence', ['nominal20', 'S1', 'S2', 'S3']) @pytest.mark.parametrize('fault', ['early_stop', 'changed_pair']) def test_skill_v4_analytic_prerequisite_rejects_partial_or_changed_schedule(sequence, fault): spec = public.make_spec(sequence=sequence) receipt = worker_receipt(spec, stopped=fault == 'early_stop') if fault == 'changed_pair': receipt['pair_commands']['1'] = ('x_bias-16mm' if receipt['pair_commands']['1'] != 'x_bias-16mm' else 'y_bias-24mm') assert launch.complete_receipt(receipt, spec) # safe usable run, not matrix proof assert not launch.complete_analytic_prerequisite(receipt, spec) def test_skill_v4_u256_gate_rehashes_successful_current_source_analytics(tmp_path): roots = [] for sequence in ('nominal20', 'S1', 'S2', 'S3'): root = tmp_path / sequence run = root / 'run' run.mkdir(parents=True) spec = public.make_spec(sequence=sequence) receipt_path = run / 'worker-result.json' receipt_path.write_text(json.dumps(worker_receipt(spec))) artifacts = {} for filename in ('trace.pt', 'initial-state.pt', 'events-and-seams.json'): (run / filename).write_text('CPU MOCK, NOT PHYSICS') artifacts[filename] = launch.b.digest(run / filename) (root / 'approval.json').write_text(json.dumps({'format': launch.FORMAT, 'sources': launch.sources(), 'spec': spec})) (root / 'campaign-result.json').write_text(json.dumps({'success': True, 'rows': [ {'slot': 'prepare', 'success': True}, {'slot': 'run', 'success': True, 'worker_result_sha256': launch.b.digest(receipt_path), 'artifact_sha256': artifacts}]})) roots.append(root) spec = public.make_spec(sequence='S1', controller='u256') public.require_analytics(spec, roots) (roots[-1] / 'run/trace.pt').write_text('tampered') with pytest.raises(PermissionError, match='artifact changed'): public.require_analytics(spec, roots)