Files
SkillFactory/tests/test_contact_cycle_functional_flow_v2.py
chenlin c528dd268f feat: checkpoint Go2 FL simulation performance v0.4.2
Include callable skill and live-GUI prerequisites, epoch sensor reads, packed snapshots, sampled bit-exact kinematics, and verified-tick rendering without changing the physical/control schedule.

Record bounded analytic S1 GUI/headless qualification, original failures and CPU evidence (1557 passed, 6 skipped). Preserve accepted_skill=false and sub-real-time limitations. Exclude native authorities, caches, large media and local delivery copies; retain sealed artifact digest.
2026-10-16 15:46:01 +08:00

165 lines
7.2 KiB
Python

"""CPU/synthetic qualification only, never simulation-success evidence."""
import copy
import importlib.util
import sys
from pathlib import Path
import pytest
import torch
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'scripts'))
spec = importlib.util.spec_from_file_location('functional_flow_audit', ROOT / 'scripts/audit_cycle_functional_flow.py')
audit = importlib.util.module_from_spec(spec)
spec.loader.exec_module(audit)
api = audit.cpu_protocol()
CFG = api.load_contract(ROOT / 'configs/contact_cycle/functional_flow_v2.json')
def example(cycles=1):
frames = []
for c in range(cycles):
t = torch.arange(1, 4501) * .002
phase = torch.bucketize(t, torch.tensor([3., 4., 4.6, 6., 6.8]), right=True)
z = torch.zeros(4500)
travel = ((t - 4.6) / 1.4).clamp(0, 1)
a, b = (0., 1.) if c % 2 == 0 else (1., 0.)
x = a + (b - a) * travel
foot = torch.stack((x * .025, x * .02,
torch.where((phase == 2) | (phase == 3), .04, 0.) + .02), -1)
normal = torch.stack((z, z, torch.where((phase == 0) | (phase == 5), 50., 0.)), -1)
d = {'time': t + c * 9, 'phase': phase, 'scenario': phase * 0, 'weight': z + 50,
'normal': normal, 'foot': foot, 'target': foot.clone(), 'gap': foot[:, 2] - .02,
'signed_height_error': z, 'tilt': z, 'external': torch.zeros(4500, 2, 6),
'guide': torch.zeros(4500, 2, 6), 'auxiliary': torch.zeros(4500, 2, 6),
'joint_excess': z, 'limit_ratio': z, 'contact_acquired': z.bool().logical_not(),
'actor_metadata_available': z.bool(), 'control_action_available': z.bool().logical_not(),
'would_fail': z.bool(), 'failure_flags': torch.zeros(4500, 8, dtype=torch.bool),
'root_position_error_m': torch.stack((z + .08, z, z), -1)}
frames.append({k: v[:, None] for k, v in d.items()})
return {k: torch.cat([d[k] for d in frames]) for k in frames[0]}
@pytest.mark.parametrize('cycles', [1, 3])
def test_functional_trace_and_drift_are_not_runtime_pass(cycles):
row = audit.evaluate_flow(example(cycles), CFG, 'analytic_reference', cycles)
assert row['trace_functional_passed'], row
assert row['root_xy_30mm_exceeded_diagnostic_only']
assert row['root_xy_error_max_m'] == pytest.approx(.08)
assert row['runtime_continuity_verified'] is False
assert row['new_simulation_steps'] == row['new_optimizer_steps'] == 0
assert row['accepted_skill'] is False
@pytest.mark.parametrize('fault', ['joined_resets', 'partial', 'no_unload', 'no_swing',
'no_reload', 'nan', 'joint', 'effort', 'penetration',
'runtime_failure', 'wrong_actor_metadata'])
def test_reject_failure_and_spliced_cycles(fault):
d = example(3)
if fault == 'joined_resets':
d['time'][4500:] -= 9
d['time'][9000:] -= 9
elif fault == 'partial':
d = {k: v[:4700] for k, v in d.items()}
elif fault == 'no_unload':
d['normal'][:, 0, 2][d['phase'][:, 0] == 1] = 50
elif fault == 'no_swing':
d['foot'][:, :, :2] = 0
elif fault == 'no_reload':
d['normal'][:, 0, 2][d['phase'][:, 0] == 5] = 0
elif fault == 'nan':
d['gap'][-1] = float('nan')
elif fault == 'joint':
d['joint_excess'][0] = .011
elif fault == 'effort':
d['limit_ratio'][0] = 1.01
elif fault == 'penetration':
d['gap'][0] = -.003
elif fault == 'runtime_failure':
d['failure_flags'][100, 0, 2] = True
elif fault == 'wrong_actor_metadata':
d['actor_metadata_available'][:] = True
assert not audit.evaluate_flow(d, CFG, 'analytic_reference', 3)['trace_functional_passed']
@pytest.mark.parametrize('change', ['gate', 'phase', 'fixture', 'authorize', 'boolean_cycle', 'unknown'])
def test_no_unregistered_contract_changes(change):
cfg = copy.deepcopy(CFG)
if change == 'gate':
cfg['functional_gates']['root_height_error_m_max'] = .2
elif change == 'phase':
cfg['phase_durations_s'][0] = 4.
elif change == 'fixture':
cfg['fixture_changes'] = ['vertical guide']
elif change == 'authorize':
cfg['execution_authorized'] = True
elif change == 'boolean_cycle':
with pytest.raises(ValueError):
api.FunctionalFlowProtocol(cfg, cycles=True, controller_kind='analytic_reference')
return
else:
cfg['silent_extra'] = 1
with pytest.raises(ValueError):
api.FunctionalFlowProtocol(cfg, cycles=1, controller_kind='analytic_reference')
def test_unchanged_analytic_command_and_continuous_handoff():
old_api = sys.modules[api.__package__ + '.evaluation_protocol']
old = old_api.load_protocol(ROOT / 'configs/contact_cycle/continuous_eval_p1_v1.json')
new = api.FunctionalFlowProtocol(CFG, cycles=3, controller_kind='analytic_reference')
args = {'anchor_a_world': (0., 0., .02), 'root_origin_world': (0., 0., .3), 'weight_N': 52.86}
for tick in range(1, 13501):
clock = new.evidence_clock(0, tick)
# P1 row1 is analytic, but command math is the same for either row.
assert new.command(0, clock, **args) == old.command(1, old.evidence_clock(1, tick), **args)
assert new.document['stage'] == 'FUNCTIONAL_V2'
assert 'P1_margins' not in new.document['thresholds']
for tick in (4500, 9000):
with pytest.raises(ValueError):
new.next_command_clock(0, tick)
endpoint = new.command(0, new.evidence_clock(0, tick), **args)
next_clock = new.next_command_clock(0, tick, seam_admitted=True)
begin = new.command(0, next_clock, **args)
assert endpoint.target_world_m == begin.target_world_m
assert endpoint.desired_normal_N == begin.desired_normal_N
assert endpoint.auxiliary_fraction == begin.auxiliary_fraction == 0
assert new.next_command_clock(0, 13500) is None
def test_contract_cannot_be_mutated_through_protocol():
cfg = copy.deepcopy(CFG)
new = api.FunctionalFlowProtocol(cfg, cycles=1, controller_kind='learned_policy')
cfg['functional_gates']['root_height_error_m_max'] = 2
assert new.document['thresholds']['functional']['root_height_error_m_max'] == .1
with pytest.raises(TypeError):
new.document['thresholds']['functional']['root_height_error_m_max'] = 2
def test_missing_measurements_fail_closed():
d = example()
del d['gap']
result = audit.evaluate_flow(d, CFG, 'analytic_reference', 1)
assert not result['trace_functional_passed']
assert result['missing_fields'] == ['gap']
def test_nonzero_residual_cannot_be_called_analytic():
d = example()
d['effective_residual_rad'] = torch.ones(4500, 1, 3) * .01
assert not audit.evaluate_flow(d, CFG, 'analytic_reference', 1)['trace_functional_passed']
def test_learned_claim_checks_recorded_deterministic_action():
d = example()
d['actor_metadata_available'][:] = True
d['requested_action'] = torch.zeros(4500, 1, 3)
d['actor_mean'] = torch.ones(4500, 1, 3)
assert not audit.evaluate_flow(d, CFG, 'learned_policy', 1)['trace_functional_passed']
def test_duplicate_json_contract_rejected(tmp_path):
path = tmp_path / 'bad.json'
path.write_text('{"format":"a", "format":"b"}')
with pytest.raises(ValueError, match='Duplicate'):
api.load_contract(path)