Files
SkillFactory/tests/test_contact_cycle_sim_stress.py
chenlin 9fe9d27865 feat: checkpoint contact-cycle evaluation and RootXY PPO progress
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.
2026-10-15 14:18:17 +08:00

194 lines
8.5 KiB
Python

"""Source-named pure CPU tests for the nominal scripted residual harness."""
import importlib.util
import json
from pathlib import Path
import pytest
import torch
from SkillFactory.legs.contact_cycle_sim.stress import first_failure, gate_residual, residual_signals
ROOT = Path(__file__).resolve().parents[1]
PLAN = json.loads((ROOT / 'configs/contact_cycle/simulation_stress_v1.json').read_text())
@pytest.mark.parametrize('case', PLAN['cases'], ids=lambda c: c['id'])
def test_residual_signals_range_and_reproducibility(case):
a, b = residual_signals(PLAN, case), residual_signals(PLAN, case)
assert a.shape == (900, 8, 3) and a.dtype == torch.float32
assert torch.equal(a, b) and torch.isfinite(a).all() and a.abs().max() <= 1
if case['kind'] != 'gaussian':
assert float(a.abs().max()) * .05 <= case['amplitude_rad'] + 1e-8
def test_constant_signs_cover_all_eight_combinations():
x = residual_signals(PLAN, {'kind': 'constant', 'amplitude_rad': .025})
assert len(x[0].unique(dim=0)) == 8
assert set(x.flatten().tolist()) == {-.5, .5}
assert torch.equal(x[0], x[-1])
def test_sine_explicit_phase_frequency_and_gating():
x = residual_signals(PLAN, {'kind': 'sine', 'amplitude_rad': .025})
assert x[0, 0, 0] == 0
assert x[0, 2, 0] == .5
assert abs(float(x[100, 0, 0]) + .5) < 1e-7
phase = torch.tensor([0, 1, 2, 3, 4, 5, 2, 3])
before = x[10].clone()
gated = gate_residual(x[10], phase, PLAN['gated_phases'])
assert not gated[[0, 1, 4, 5]].any()
assert torch.equal(gated[[2, 3, 6, 7]], before[[2, 3, 6, 7]])
assert torch.equal(x[10], before)
def test_gaussian_declared_seed_zero_mean_distribution_not_recentering():
a = residual_signals(PLAN, {'kind': 'gaussian', 'seed': 90321})
b = residual_signals(PLAN, {'kind': 'gaussian', 'seed': 90322})
assert not torch.equal(a, b)
assert abs(float(a.mean())) < .003
assert .095 < float(a.std()) < .102
generator = torch.Generator().manual_seed(90321)
assert torch.equal(a, (.1 * torch.randn((900, 8, 3), generator=generator)).tanh())
@pytest.mark.parametrize('amplitude', [-.1, .051, float('nan')])
def test_invalid_amplitude_rejected(amplitude):
with pytest.raises(ValueError):
residual_signals(PLAN, {'kind': 'sine', 'amplitude_rad': amplitude})
def test_first_failure_reports_physics_onset_not_end_of_control():
trace = {'failure_flags': torch.zeros(5, 1, 2, dtype=torch.bool),
'time': torch.arange(1, 6).float()[:, None] * .002,
'phase': torch.zeros(5, 1, dtype=torch.long), 'gap': torch.zeros(5, 1),
'normal': torch.zeros(5, 1, 3), 'signed_height_error': torch.zeros(5, 1),
'tilt': torch.zeros(5, 1)}
assert first_failure(trace, ['a', 'b']) is None
trace['failure_flags'][1, 0, 1] = True
trace['failure_flags'][4, 0, 0] = True
onset = first_failure(trace, ['a', 'b'])
assert onset['physics_tick'] == 2 and onset['time_s'] == pytest.approx(.004)
assert onset['causes'] == ['b']
def load_runner(monkeypatch):
monkeypatch.syspath_prepend(str(ROOT / 'scripts'))
spec = importlib.util.spec_from_file_location(
'verify_cycle_sim_stress', ROOT / 'scripts/verify_cycle_sim_stress.py')
runner = importlib.util.module_from_spec(spec)
spec.loader.exec_module(runner)
return runner
@pytest.mark.parametrize('fault', ['boot', 'expired', 'disk', 'contract'])
def test_window_guard_fails_closed(monkeypatch, tmp_path, fault):
runner = load_runner(monkeypatch)
window = {'boot_id': Path('/proc/sys/kernel/random/boot_id').read_text().strip(),
'deadline_boottime': 1000, 'cleanup_margin_s': 120, 'max_new_artifact_bytes': 16 * 1024**2}
if fault == 'boot':
window['boot_id'] = 'other-boot'
if fault == 'disk':
window['max_new_artifact_bytes'] = 1
path = tmp_path / 'window.json'
path.write_text(json.dumps(window))
monkeypatch.setattr(runner, 'WINDOW', path)
monkeypatch.setattr(runner, 'WINDOW_ROOT', tmp_path)
monkeypatch.setattr(runner, 'WINDOW_SHA256', runner.sha256(path) if fault != 'contract' else 'bad')
monkeypatch.setattr(runner.time, 'clock_gettime', lambda _: 881 if fault == 'expired' else 100)
with pytest.raises(RuntimeError):
runner.WindowGuard().check(force_disk=True)
@pytest.mark.parametrize('existing', ['result.json', 'supervisor.json', 'baseline_before'])
def test_execution_claim_preserves_existing_evidence(monkeypatch, tmp_path, existing):
runner = load_runner(monkeypatch)
evidence = tmp_path / existing
if existing == 'baseline_before':
evidence.mkdir()
evidence = evidence / 'initial.pt'
evidence.write_bytes(b'immutable original evidence')
with pytest.raises(FileExistsError):
runner.claim_execution(tmp_path)
assert evidence.read_bytes() == b'immutable original evidence'
assert not (tmp_path / 'execution_started.json').exists()
def test_execution_claim_rejects_repeat_without_changing_marker(monkeypatch, tmp_path):
runner = load_runner(monkeypatch)
runner.claim_execution(tmp_path)
marker = tmp_path / 'execution_started.json'
original = marker.read_bytes()
assert json.loads(original)['pid'] > 0
with pytest.raises(FileExistsError):
runner.claim_execution(tmp_path)
assert marker.read_bytes() == original
@pytest.mark.parametrize('report', [
{'passed': True, 'status': 'completed', 'command': ['python', 'child']},
{'passed': False, 'status': 'failed', 'command': ['python', 'child']},
{'passed': False, 'status': 'running', 'command': ['python', 'child'], 'cleanup': {}},
])
def test_execution_claim_preserves_noninitial_supervisor_report(monkeypatch, tmp_path, report):
runner = load_runner(monkeypatch)
evidence = tmp_path / 'supervisor.json'
original = json.dumps(report).encode()
evidence.write_bytes(original)
with pytest.raises(FileExistsError):
runner.claim_execution(tmp_path)
assert evidence.read_bytes() == original and not (tmp_path / 'execution_started.json').exists()
@pytest.mark.parametrize('completed', [False, True])
def test_internal_entry_claims_once_before_kit(monkeypatch, tmp_path, completed):
import builtins
import sys
from types import SimpleNamespace
runner = load_runner(monkeypatch)
output = tmp_path / 'attempt'
output.mkdir()
plan = tmp_path / 'plan.json'
plan.write_text('{}')
registration = output / 'preregistration.json'
registration.write_text(json.dumps({'identity': {}, 'plan_sha256': runner.sha256(plan)}))
(output / 'launch.json').write_text(json.dumps({'preregistration_sha256': runner.sha256(registration)}))
result = output / 'result.json'
if completed:
result.write_bytes(b'original failed or successful result')
supervisor = output / 'supervisor.json'
running = json.dumps({'passed': False, 'status': 'running', 'command': ['python', 'child']}).encode()
supervisor.write_bytes(running)
monkeypatch.setattr(runner, 'OUTPUT_ROOT', tmp_path)
monkeypatch.setattr(runner, 'PLAN', plan)
monkeypatch.setattr(runner, 'WindowGuard', lambda: SimpleNamespace(check=lambda **kwargs: 1000))
monkeypatch.setattr(sys, 'argv', ['verify_cycle_sim_stress.py', '--output', str(output), '--headless'])
original_import = builtins.__import__
def prohibit_kit(name, *args, **kwargs):
if name == 'isaaclab.app':
raise AssertionError('Repeated internal invocation reached Kit import')
return original_import(name, *args, **kwargs)
monkeypatch.setattr(builtins, '__import__', prohibit_kit)
if completed:
with pytest.raises(FileExistsError):
runner.main()
assert result.read_bytes() == b'original failed or successful result'
else:
# A legitimate first launch reaches the sentinel, never the real Kit import.
with pytest.raises(AssertionError, match='reached Kit import'):
runner.main()
marker = output / 'execution_started.json'
original_marker = marker.read_bytes()
with pytest.raises(FileExistsError):
runner.main()
assert marker.read_bytes() == original_marker and not result.exists()
assert supervisor.read_bytes() == running
def test_plan_is_twelve_nominal_cases_with_baseline_bookends():
assert len(PLAN['cases']) == 12 and not PLAN['omitted_cases']
assert PLAN['cases'][0]['id'] == 'baseline_before'
assert PLAN['cases'][-1]['id'] == 'baseline_after'
assert PLAN['nominal_only'] and not PLAN['external_disturbance'] and PLAN['optimizer_steps'] == 0