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.
This commit is contained in:
2026-10-16 15:46:01 +08:00
parent 9fe9d27865
commit c528dd268f
136 changed files with 15553 additions and 15 deletions
@@ -0,0 +1,163 @@
"""Synthetic/CPU qualification of functional-only runtime wiring and budgets."""
import copy
import importlib.util
import json
import sys
from pathlib import Path
from types import ModuleType, SimpleNamespace
import pytest
import torch
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'scripts'))
import functional_flow_launch_v2 as launch
@pytest.fixture
def recording(monkeypatch):
name = '_functional_recording_test'
directory = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim'
parent = ModuleType(name)
parent.__path__ = [str(directory)]
monkeypatch.setitem(sys.modules, name, parent)
dummy = ModuleType(name + '.recording')
dummy.SimCycleRecorder = object
monkeypatch.setitem(sys.modules, name + '.recording', dummy)
if 'SkillFactory' not in sys.modules:
package = ModuleType('SkillFactory')
package.__path__ = [str(ROOT / 'source/SkillFactory/SkillFactory')]
monkeypatch.setitem(sys.modules, 'SkillFactory', package)
spec = importlib.util.spec_from_file_location(name + '.functional_flow_recording_v2', directory / 'functional_flow_recording_v2.py')
mod = importlib.util.module_from_spec(spec)
monkeypatch.setitem(sys.modules, spec.name, mod)
spec.loader.exec_module(mod)
return mod
def frames(local_tick):
result = []
for tick in range(1, local_tick + 1):
load = 50 if tick < 1500 else 25 if tick < 2000 else 0
travel = max(0, min(1, (tick - 2300) / 700))
xy = torch.tensor([[travel * .025, travel * .02, .02]])
result.append({'normal': torch.tensor([[0., 0., load]]), 'weight': torch.tensor([50.]),
'foot': xy, 'target': xy.clone(), 'gap': torch.tensor([.04])})
return result
def recorder_with_frames(mod, n):
cfg = json.loads((ROOT / 'configs/contact_cycle/functional_flow_v2.json').read_text())
r = mod.FunctionalFlowRecorder(cfg, None, lambda: None)
r.capture = SimpleNamespace(frames=frames(n))
return r
def test_phase_windows_do_not_need_full_cycle(recording):
r = recorder_with_frames(recording, 1499)
event = SimpleNamespace(clock=SimpleNamespace(local_tick=1499, cycle_index=0))
assert r._window_checks(event) == ()
r.capture.frames = frames(1999)
event.clock.local_tick = 1999
assert r._window_checks(event) == ()
r.capture.frames = frames(2999)
event.clock.local_tick = 2999
assert r._window_checks(event) == ()
@pytest.mark.parametrize('fault', ['support', 'unload', 'airless', 'static_xy'])
def test_first_failed_phase_window_stops(recording, fault):
n = {'support': 1499, 'unload': 1999}.get(fault, 2999)
r = recorder_with_frames(recording, n)
r.support_load = 1.
if fault == 'support':
for f in r.frames:
f['normal'].zero_()
elif fault == 'unload':
for f in r.frames:
f['normal'][0, 2] = 50
elif fault == 'airless':
for f in r.frames:
f['gap'].zero_()
else:
for f in r.frames:
f['foot'] = torch.zeros(1, 3)
event = SimpleNamespace(clock=SimpleNamespace(local_tick=n, cycle_index=0))
assert r._window_checks(event)
def test_reverse_swing_is_signed_not_absolute(recording):
r = recorder_with_frames(recording, 2999)
for f in r.frames:
f['target'] *= -1
f['foot'] *= -1
event = SimpleNamespace(clock=SimpleNamespace(local_tick=2999, cycle_index=0))
assert r._window_checks(event) == ()
def test_tensor_fingerprints_are_copied_and_exact(recording):
value = torch.tensor([[1., 2.]])
before = recording.tensor_identity(value)
assert before == recording.tensor_identity(value.clone())
value[0, 0] += .000001
assert before != recording.tensor_identity(value)
def test_continuity_rejects_missing_seam_and_pose_writes(recording):
r = recorder_with_frames(recording, 13500)
r.initial_counters = {k: 0 for k in recording.COUNTERS}
r.cycles = [{'audit': {'sim_functional_passed': True}}] * 3
h = SimpleNamespace(**{k: 0 for k in recording.COUNTERS}, violations=[])
h.rollout_steps = 13500
stop = SimpleNamespace(reason='protocol_complete', causes=())
assert not r.continuity(stop, h, 3)['runtime_continuity_verified']
r.seams = [{'same_carried_state': True, 'single_refresh': True}] * 2
assert r.continuity(stop, h, 3)['runtime_continuity_verified']
h.pose_write_calls = 1
assert not r.continuity(stop, h, 3)['runtime_continuity_verified']
def test_guard_deadline_reserves_cleanup_and_never_grants_physics(tmp_path, monkeypatch):
monkeypatch.setattr(launch.b, 'boot_id', lambda: 'boot')
monkeypatch.setattr(launch.b, 'boottime', lambda: 100.)
g = launch.Guard(tmp_path, 100, 700)
g.check()
monkeypatch.setattr(launch.b, 'boottime', lambda: 580.)
with pytest.raises(RuntimeError, match='deadline'):
g.check()
g.check(cleanup=True)
monkeypatch.setattr(launch.b, 'boottime', lambda: 700.)
with pytest.raises(RuntimeError):
g.check(cleanup=True)
def test_guard_all_publications_check_exact_pending_bytes(tmp_path, monkeypatch):
monkeypatch.setattr(launch.b, 'boot_id', lambda: 'boot')
monkeypatch.setattr(launch.b, 'boottime', lambda: 100.)
g = launch.Guard(tmp_path, 100, 700)
g.check()
with pytest.raises(RuntimeError, match='storage'):
g.check(launch.CAP)
def test_no_old_authorization_routes_or_new_training():
source = (ROOT / launch.SCRIPT).read_text()
assert 'launch.approval(' in source
assert 'RuntimeHooks' in source
assert 'before_physics' in source
assert "row['success']" in source and 'break # first failure' in source
assert 'train_cycle' not in source and 'optimizer.step' not in source
assert 'prepare_binding(' not in source # old P1 binding cannot admit new task
assert len(launch.SLOTS) == 5
assert sum(c * 4500 for _, c, _ in launch.SLOTS) == 36000
def test_approval_source_drift_rejected_before_root_created(tmp_path, monkeypatch):
root = tmp_path / 'new-campaign'
approval = tmp_path / 'approval.json'
approval.write_text(json.dumps({'format': launch.FORMAT, 'artifact_root': str(root)}))
import hashlib
digest = hashlib.sha256(approval.read_bytes()).hexdigest()
with pytest.raises(PermissionError):
launch.approval(approval, digest, root)
assert not root.exists()
@@ -0,0 +1,164 @@
"""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)
@@ -0,0 +1,248 @@
"""CPU ONLY: exact GPU decision equivalence, fixed matrix, pairing and lifecycle."""
import copy
import json
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'scripts'))
import functional_followup_launch_v3 as f
import functional_gpu_watch_v3 as watch
import evaluate_cycle_functional_flow as runner
def test_exact_plan_matrix_and_budget():
p = f.plan()
rows = f.api().slots(p)
commands = f.api().commands(p)
assert len(rows) == 14 and sum(r['cycles'] * 4500 for r in rows) == 175500
assert p['limits']['max_physics_calls'] == 175500 + 13 * 250
assert p['limits']['max_campaign_seconds'] == 300 + 13 * 480
assert len(commands) == 7
for delta in list(commands.values())[1:]:
assert .001 < delta[0] <= .025 and .001 < delta[1] <= .020 and delta[2] == 0
assert (delta[0]**2 + delta[1]**2)**.5 == pytest.approx(.016 if '16mm' in next(k for k,v in commands.items() if v == delta) else .024)
assert rows[1]['command_id'] == 'validated_nominal'
for a,b in zip(rows[2::2], rows[3::2], strict=True):
assert a['pair_group'] == b['pair_group'] == a['command_id'] == b['command_id']
assert (a['controller_kind'], b['controller_kind']) == ('analytic_reference','learned_policy')
@pytest.mark.parametrize('field,value', [('execution_authorized',True),('seed',94501),('noise',True),('cycles_per_run',1)])
def test_no_silent_plan_or_gate_change(field,value):
p = copy.deepcopy(f.plan());p[field]=value
with pytest.raises(ValueError):f.api().validate_plan(p)
def test_protocol_three_cycles_commands_and_seams_unchanged_math():
api=f.api()
from_module = __import__(api.__package__ + '.functional_flow_v2',fromlist=['load_contract'])
base=from_module.load_contract(ROOT / f.old.CONFIG)
reference=from_module.FunctionalFlowProtocol(base,cycles=3,controller_kind='analytic_reference')
original=api.FollowupProtocol(base,f.plan(),'validated_nominal','learned_policy')
kwargs=dict(anchor_a_world=(.1,.2,.022),root_origin_world=(0,0,.35),weight_N=52.8597)
for tick in range(1,13501):
a=reference.evidence_clock(0,tick);b=original.evidence_clock(0,tick)
assert a==b
assert reference.command(0,a,**kwargs)==original.command(0,b,**kwargs)
for name in list(api.commands(f.plan()))[1:]:
proto=api.FollowupProtocol(base,f.plan(),name,'analytic_reference')
assert 'P1_margins' not in proto.document['thresholds']
for tick in (4500,9000):
old=proto.command(0,proto.evidence_clock(0,tick),**kwargs)
new=proto.command(0,proto.next_command_clock(0,tick,seam_admitted=True),**kwargs)
assert old.target_world_m==new.target_world_m
assert old.target_velocity_world_m_s==new.target_velocity_world_m_s
@pytest.mark.parametrize('field', ['state','RNG','ik','command','observation'])
def test_pair_mismatch_fails_without_repair(field):
initial={field:{'sha':'A'}};other=copy.deepcopy(initial);other[field]['sha']='B'
assert f.require_pair(initial,copy.deepcopy(initial))
with pytest.raises(RuntimeError,match='before_rollout'):f.require_pair(other,initial)
assert other[field]['sha']=='B'
def receipt(slot):
if slot['cycles']==0:
return {'success':True,'environment_constructed':False,'physics_counts':{'attempted_steps':0,'completed_steps':0},'preclose_identity_verified':True}
return {'success':True,'preclose_identity_verified':True,'runtime_continuity_verified':True,
'controller_kind':slot['controller_kind'],'command_id':slot['command_id'],'cycles_requested':3,
'physics_counts':{'rollout_steps':13500,'initialization_steps':0,'attempted_steps':13500,'completed_steps':13500},
'runtime_stop':{'reason':'protocol_complete','causes':[],'clock':{'global_tick':13500}},'hook_violations':[],
'initial_pair_matched':slot['controller_kind']=='learned_policy','initial_pair_sha256':'same'}
@pytest.mark.parametrize('fault',['success','runtime_continuity_verified','preclose_identity_verified','controller_kind','command_id','cycles_requested','stop','count','hook'])
def test_physics_alone_never_qualifies_complete_receipt(fault):
s=f.api().slots(f.plan())[1];r=receipt(s)
assert f.complete_receipt(r,s)
if fault=='stop':r['runtime_stop']['reason']='failure'
elif fault=='count':r['physics_counts']['completed_steps']=13501
elif fault=='hook':r['hook_violations']=['reset']
elif fault in ('controller_kind','command_id'):r[fault]='wrong'
else:r[fault]=False
assert not f.complete_receipt(r,s)
def test_comparison_needs_both_supervisions_and_exact_pair():
a,b=f.api().slots(f.plan())[2:4]
r={'success':True,'worker':receipt(a)};u={'success':True,'worker':receipt(b)}
assert f.paired_comparison(r,u)['eligible']
u['success']=False
assert not f.paired_comparison(r,u)['eligible']
u['success']=True;u['worker']['initial_pair_sha256']='different'
assert not f.paired_comparison(r,u)['eligible']
@pytest.fixture
def gpu(monkeypatch,tmp_path):
base=watch.base;lib=tmp_path/'nvml.so';lib.write_bytes(b'mock')
desktop={'pid':7,'starttime':123,'sha256':'hash'}
identity={'library':str(lib),'sha256':base.b.digest(lib),'uuid':'GPU-test','index':0,'desktop_clients':[desktop]}
clients={'compute':set(),'graphics':{7}}
monkeypatch.setattr(base.ctypes,'CDLL',lambda _:object())
def call(self,name,*args):
if name=='nvmlDeviceGetUUID':args[1].value=b'GPU-test'
monkeypatch.setattr(base.GPUOwnership,'call',call)
monkeypatch.setattr(base.GPUOwnership,'clients',lambda _:clients)
monkeypatch.setattr(base,'desktop_identity',lambda pid,hash_executable=True:dict(desktop) if hash_executable else {k:v for k,v in desktop.items() if k!='sha256'})
monkeypatch.setattr(base.owner,'process_table',lambda:{7:(123,1,1,'S'),99:(456,1,1,'S')})
owned=SimpleNamespace(known={(99,456)},discover=lambda:{99:(456,1,1,'S')})
events=[];observed=watch.ObservedGPUOwnership(identity,events.append)
return SimpleNamespace(base=base,identity=identity,clients=clients,owned=owned,events=events,observed=observed)
@pytest.mark.parametrize('compute,graphics,final,passes',[(set(),{7},False,True),({99},{7,99},False,True),
({8},{7},False,False),(set(),{7,8},False,False),({7},{7},False,False),({99},{7},True,False),(set(),{7},True,True)])
def test_observer_keeps_exact_existing_gpu_decisions(gpu,compute,graphics,final,passes):
direct=gpu.base.GPUOwnership(gpu.identity)
gpu.clients.update(compute=compute,graphics=graphics)
if passes:
assert direct.check(gpu.owned,final=final)==gpu.observed.check(gpu.owned,final=final)
else:
with pytest.raises(RuntimeError) as old:direct.check(gpu.owned,final=final)
with pytest.raises(RuntimeError) as new:gpu.observed.check(gpu.owned,final=final)
assert str(old.value)==str(new.value)
sample=gpu.events[-1]['recent_checks'][-1]
assert sample['nvml_clients']=={'compute':sorted(compute),'graphics':sorted(graphics)}
assert sample['active_owned']=={'99':[456,1,1,'S']}
assert sample['owned_known']==[[99,456]] and sample['error']
def test_reproduced_query_then_exit_race_stays_failure_not_retrospective_excuse(gpu,monkeypatch):
gpu.clients['compute']={99}
gpu.owned.discover=lambda:{}
monkeypatch.setattr(gpu.base.owner,'process_table',lambda:{7:(123,1,1,'S')})
with pytest.raises(RuntimeError,match='GPU ownership'):gpu.observed.check(gpu.owned)
sample=gpu.events[-1]['recent_checks'][-1]
assert sample['nvml_clients']['compute']==[99] and sample['active_owned']=={}
assert sample['owned_known']==[[99,456]] and sample['proc_after_failure']['99'] is None
def test_reused_owned_pid_is_still_rejected_and_recorded(gpu,monkeypatch):
gpu.clients['compute']={99};gpu.owned.discover=lambda:{}
monkeypatch.setattr(gpu.base.owner,'process_table',lambda:{99:(999,1,1,'S')})
with pytest.raises(RuntimeError,match='GPU ownership'):gpu.observed.check(gpu.owned)
sample=gpu.events[-1]['recent_checks'][-1]
assert sample['owned_known']==[[99,456]] and sample['proc_after_failure']['99'][0]==999
def test_library_drift_fails_before_query(gpu):
Path(gpu.identity['library']).write_bytes(b'drift')
with pytest.raises(RuntimeError,match='NVML drift'):gpu.observed.check(gpu.owned)
assert 'nvml_clients' not in gpu.events[-1]['recent_checks'][-1]
def test_diagnostic_storage_failure_is_not_ignored(gpu):
def fail(_):raise OSError('cannot publish diagnostics')
gpu.observed.emit=fail
with pytest.raises(OSError,match='cannot publish'):gpu.observed.check(gpu.owned,final=True)
def test_after_failure_proc_read_error_never_replaces_original_gpu_failure(gpu,monkeypatch):
gpu.clients['compute']={8}
def fail():raise PermissionError('after-check read denied')
monkeypatch.setattr(gpu.base.owner,'process_table',fail)
with pytest.raises(RuntimeError,match='GPU ownership'):gpu.observed.check(gpu.owned)
assert 'proc_after_failure_error' in gpu.events[-1]['recent_checks'][-1]
def test_changed_desktop_or_nvml_error_is_not_waived(gpu,monkeypatch):
monkeypatch.setattr(gpu.base,'desktop_identity',lambda *a,**k:{'starttime':999})
with pytest.raises(RuntimeError,match='identity changed'):gpu.observed.check(gpu.owned)
def fail(_):raise RuntimeError('NVML query failed')
monkeypatch.setattr(gpu.base.GPUOwnership,'clients',fail)
with pytest.raises(RuntimeError,match='NVML query failed'):gpu.observed.check(gpu.owned)
def test_no_grant_no_launch_or_directory_creation(tmp_path):
path=tmp_path/'not-granted.json';path.write_text(json.dumps({'format':f.old.FORMAT}))
root=tmp_path/'new-root'
with pytest.raises(PermissionError):f.approval(path,f.old.b.digest(path),root)
assert not root.exists()
@pytest.mark.parametrize('fault,expected_rows',[(None,14),('nominal_supervision',2),('pair_supervision',4),('missing_result',2)])
def test_mock_campaign_order_first_failure_and_no_retry(tmp_path,monkeypatch,fault,expected_rows):
old=f.old;plan=f.plan();slots=f.api().slots(plan);run=tmp_path/'mock-only'
approval={'limits':plan['limits'],'sources':f.sources(),'gpu_identity':{}}
monkeypatch.setattr(f,'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)
monkeypatch.setattr(old.owner,'cleanup_group',lambda *a,**k: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)
calls=[]
class Child:
returncode=0;stdout=None
def __init__(self,*args,**kwargs):
i=len(calls);s=slots[i];calls.append(s['slot']);folder=run/s['slot']
r=receipt(s)
if i==0:
(folder/'binding.json').write_text(json.dumps({'format':'skillfactory.functional-followup-binding.v3',
'contract_sha256':approval['sources'][old.CONFIG],'plan_sha256':approval['sources'][f.PLAN]}))
(folder/'inventory.json').write_text('{}')
else:
for file in ['trace.pt','initial-state.pt','events-and-seams.json','initial-pair.json']:
if fault=='missing_result' and i==1 and file=='trace.pt':continue
(folder/file).write_text('CPU MOCK, NOT PHYSICS')
r['initial_pair_sha256']=old.b.digest(folder/'initial-pair.json')
(folder/'worker-result.json').write_text(json.dumps(r))
def poll(self):
if (fault=='nominal_supervision' and len(calls)==2 or fault=='pair_supervision' and len(calls)==4):
# Handshake failure is a supervisor failure; worker physics flag stays true.
return None
return 0
def wait(self,**kwargs):return 0
monkeypatch.setattr(runner.subprocess,'Popen',Child)
if fault in ('nominal_supervision','pair_supervision'):
def auth(*a):
if (fault=='nominal_supervision' and len(calls)==2 or fault=='pair_supervision' and len(calls)==4):
raise RuntimeError('CPU mocked GPU supervisor failure')
monkeypatch.setattr(old,'authenticate',auth)
args=SimpleNamespace(output=run,approval=tmp_path/'unused',trusted_approval_sha256='mock',followup_v3=True)
code=runner.campaign(args)
result=json.loads((run/'campaign-result.json').read_bytes())
assert len(calls)==len(result['rows'])==expected_rows
assert calls==[s['slot'] for s in slots[:expected_rows]]
assert result['success']==(fault is None) and code==(0 if fault is None else 2)
if fault:
assert result['rows'][-1]['worker']['success'] is True
assert result['rows'][-1]['success'] is False
assert len(result['remaining_slots'])==14-expected_rows
@@ -0,0 +1,59 @@
"""CPU-only explicit shutdown ownership handoff; never accepts unknown GPU PIDs."""
import json
from pathlib import Path
import sys
from types import SimpleNamespace as NS
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import skill_launch_v4 as launch
import evaluate_cycle_functional_flow as runner
def guard(output):
def write(path, value):
path.write_text(json.dumps(value))
return NS(deadline=100., check=lambda **_: None, write=write)
def test_shutdown_no_receipt_never_releases(tmp_path):
gpu = NS(check=lambda *_: pytest.fail('No result means not ready to release'))
assert runner.release_shutdown(tmp_path, guard(tmp_path), gpu, None) is None
assert list(tmp_path.iterdir()) == []
@pytest.mark.parametrize('fault', ['missing_ready', 'gpu_fail', 'none'])
def test_shutdown_release_requires_ready_and_original_strict_live_check(tmp_path, fault):
receipt = {'shutdown_handoff_ready': fault != 'missing_ready'}
(tmp_path / 'worker-result.json').write_text(json.dumps(receipt))
def check(_):
if fault == 'gpu_fail':
raise RuntimeError('original GPU ownership failure')
return {'clean': False, 'task_compute': 'present and owned'}
gpu = NS(check=check)
if fault != 'none':
with pytest.raises(RuntimeError):
runner.release_shutdown(tmp_path, guard(tmp_path), gpu, None)
assert not (tmp_path / 'shutdown-release.json').exists()
else:
result = runner.release_shutdown(tmp_path, guard(tmp_path), gpu, None)
assert result['released'] and result['last_live_gpu']['clean'] is False
assert json.loads((tmp_path / 'shutdown-release.json').read_text()) == {
'worker_result_sha256': launch.b.digest(tmp_path / 'worker-result.json')}
launch.wait_shutdown_release(tmp_path, guard(tmp_path))
def test_worker_rejects_altered_release_digest(tmp_path):
(tmp_path / 'worker-result.json').write_text('{}')
(tmp_path / 'shutdown-release.json').write_text(json.dumps({'worker_result_sha256': '0' * 64}))
with pytest.raises(RuntimeError, match='identity_shutdown_release_mismatch'):
launch.wait_shutdown_release(tmp_path, guard(tmp_path))
def test_worker_wait_is_bounded_and_guarded(tmp_path, monkeypatch):
(tmp_path / 'worker-result.json').write_text('{}')
times = iter((0, 31))
monkeypatch.setattr(launch.b, 'boottime', lambda: next(times))
with pytest.raises(RuntimeError, match='process_shutdown_release_timeout'):
launch.wait_shutdown_release(tmp_path, guard(tmp_path))
+199
View File
@@ -0,0 +1,199 @@
"""CPU presentation-contract tests; real Kit visibility is separate desktop evidence."""
import ast
import json
from pathlib import Path
from types import ModuleType, SimpleNamespace as NS
import sys
import pytest
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
GUI = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/skill_gui_v041.py'
def gui_namespace():
# Compile the actual class/functions, omitting only native imports. This is
# NOT a physical/Kit substitute; it tests close/hold ownership and fail gates.
tree = ast.parse(GUI.read_text())
tree.body = [n for n in tree.body if not isinstance(n, (ast.Import, ast.ImportFrom))]
value = {'__name__': '_gui_cpu', '__package__': '_gui_cpu'}
exec(compile(tree, str(GUI), 'exec'), value)
return value
@pytest.mark.parametrize('value', [0, 1, None, 'yes', [], {}])
def test_gui_requires_exact_boolean_before_files_or_process(value, tmp_path):
with pytest.raises(ValueError, match='boolean'):
public.start(tmp_path / 'run', tmp_path / 'registration', sequence='S1', gui=value)
assert list(tmp_path.iterdir()) == []
def test_gui_explicit_public_spec_and_supervised_command(tmp_path, monkeypatch):
assert public.make_spec(sequence='S1')['gui'] is False
spec = public.make_spec(sequence='S1', gui=True)
assert spec['gui'] is True and spec['cycles'] == 6
captured = []
monkeypatch.setattr(launch, 'registration', lambda *_: ({}, 9999))
monkeypatch.setattr(public.subprocess, 'Popen', lambda command, **kw: captured.append((command, kw)))
public.start(tmp_path / 'run', tmp_path / 'registration', sequence='S1', gui=True)
command, kwargs = captured[0]
assert json.loads(command[command.index('--spec-json') + 1]) == spec
assert kwargs['cwd'] == ROOT
assert not (tmp_path / 'run').exists()
assert launch.limits(spec)['max_physics_calls'] == 27250
@pytest.mark.parametrize('mode,skill,gui,expected', [
('prepare', True, True, False), ('run', True, True, True),
('run', True, False, False), ('run', False, True, False)])
def test_gui_never_constructed_in_nonphysical_preparation(mode, skill, gui, expected):
assert launch.gui_enabled({'mode': mode, 'skill_v4': skill, 'spec': {'gui': gui}}) == expected
def test_gui_dep_preimports_and_no_physical_control_source_changes():
text = (ROOT / 'scripts/evaluate_cycle_functional_flow.py').read_text()
assert "'omni.replicator.core'" in text
assert "importlib.import_module('isaaclab_physx.renderers.kit_viewport_utils')" in text
assert "importlib.import_module('isaaclab_physx.renderers.isaac_rtx_renderer_utils')" in text
tree = ast.parse(GUI.read_text())
assert not any(isinstance(n, ast.Call) and isinstance(n.func, ast.Attribute)
and n.func.attr in ('step', 'reset', 'write_root_pose_to_sim', 'write_joint_state_to_sim')
for n in ast.walk(tree))
import hashlib
# Recorded immutable 0.4.0 hashes, no dependency on its external directory.
frozen = {
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_env.py': '04e98455035e347633b7ea6c1fcf5372b4e6ac33645805f1beac8f7784e424c6',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_mdp.py': '89c46b2038733a21c97cb72a4112ff16b48ed0e15b5070ebdebcd3ce0cd8f7ac',
'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/skill_v4.py': '1cfa556a44ff749677bf030dd61792fbe85d60e424646844bdace05794fd102c',
'configs/contact_cycle/functional_flow_v2.json': '16c064228b30f36bbd420b7c2290602bacae6601cac2cec5deb16440f7600625',
}
for relative, digest in frozen.items():
assert hashlib.sha256((ROOT / relative).read_bytes()).hexdigest() == digest
def test_headless_can_import_presentation_source_without_native_ui():
import subprocess
code = ('import importlib.util,sys; s=importlib.util.spec_from_file_location("gui",'+repr(str(GUI))+'); '
'm=importlib.util.module_from_spec(s); s.loader.exec_module(m); '
'assert not any(n.startswith(("omni", "isaaclab", "pxr")) for n in sys.modules)')
subprocess.run(['/usr/bin/python3.12', '-c', code], check=True)
def test_actual_installed_launcher_requires_explicit_kit_selection():
import os
installed = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/app/app_launcher.py')
tree = ast.parse(installed.read_text())
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == 'AppLauncher')
methods = {'_parse_visualizer_csv', '_normalize_visualizer_intent',
'_resolve_visualizer_settings', '_resolve_headless_settings'}
cls.body = [n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name in methods]
ns = {'Any': object, 'os': os, 'logger': NS(info=lambda *_: None, debug=lambda *_: None,
warning=lambda *_: None)}
exec(compile(ast.Module(body=[cls], type_ignores=[]), str(installed), 'exec'), ns)
launcher = ns['AppLauncher']
launcher._APPLAUNCHER_CFG_INFO = {'headless': (bool, False)}
for visualizer, expected_headless in ((None, True), ('none', True), ('kit', False)):
app = launcher.__new__(launcher)
app._livestream = 0
args = {'headless': False}
if visualizer is not None:
args['visualizer'] = visualizer
app._resolve_visualizer_settings(args)
app._resolve_headless_settings(args, 0, 0)
assert app._headless is expected_headless
source = ast.parse((ROOT / 'scripts/evaluate_cycle_functional_flow.py').read_text())
call = next(n for n in ast.walk(source) if isinstance(n, ast.Call)
and isinstance(n.func, ast.Name) and n.func.id == 'AppLauncher')
selected = next(k.value for k in call.keywords if k.arg == 'visualizer')
assert isinstance(selected, ast.IfExp) and selected.body.value == 'kit' and selected.orelse.value == 'none'
@pytest.mark.parametrize('changed', [False, True])
def test_final_hold_pumps_no_physics_and_checks_carried_state(monkeypatch, changed):
n = gui_namespace()
obj = n['LivePresentation'].__new__(n['LivePresentation'])
obj.closed = False
obj.app = NS(is_running=lambda: True)
obj.render_updates = 0
obj.hold_no_physics = False
obj.message = NS(text='')
published = []
obj.publish = published.append
obj.update = lambda *_: None
obj.report = lambda: {'protocol_completed': obj.protocol_completed}
obj.guard = lambda: None
records = ModuleType('_gui_cpu.functional_flow_recording_v2')
records.counters = lambda *_: {'physics': 27000}
carries = iter([{'root': 'same'}, {'root': 'different' if changed else 'same'}])
records.carry_snapshot = lambda *_: next(carries)
monkeypatch.setitem(sys.modules, records.__name__, records)
n['time'] = NS(monotonic=lambda: 10., sleep=lambda *_: None)
pumps = []
def render():
pumps.append('render-only')
obj.request_close()
env = NS(runtime_hooks=object(), evidence_callback=NS(capture=NS(state=object())), sim=NS(render=render))
if changed:
with pytest.raises(RuntimeError, match='carried_physical_state'):
obj.hold(env, True)
else:
obj.hold(env, True)
assert pumps == ['render-only']
assert obj.hold_no_physics is (not changed)
assert obj.close_reason == 'window_close'
@pytest.mark.parametrize('headless,visualizers', [(True, []), (False, [])])
def test_attach_requires_real_app_and_concrete_kit_not_legacy_flag(headless, visualizers):
n = gui_namespace()
obj = n['LivePresentation'].__new__(n['LivePresentation'])
obj.app = NS(config={'headless': headless})
with pytest.raises(RuntimeError, match='non_headless_app_and_live_Kit'):
obj.attach(NS(sim=NS(visualizers=visualizers)))
def test_abort_never_enters_final_frame_hold():
n = gui_namespace()
obj = n['LivePresentation'].__new__(n['LivePresentation'])
published = []
obj.publish = published.append
obj.report = lambda: {'protocol_completed': obj.protocol_completed}
obj.hold(None, False)
assert published == [{'protocol_completed': False}]
def test_os_close_is_deferred_to_evidence_owner():
n = gui_namespace()
canceled = []
n['omni'] = NS(kit=NS(app=NS(get_app=lambda: NS(try_cancel_shutdown=canceled.append))))
obj = n['LivePresentation'].__new__(n['LivePresentation'])
obj._on_quit(None)
assert obj.closed and obj.close_reason == 'window_close' and canceled
def test_gui_safe_stop_remains_usable_not_full_matrix_proof():
from test_contact_cycle_skill_v4 import worker_receipt
spec = public.make_spec(cycles=3, gui=True)
receipt = worker_receipt(spec, stopped=True)
receipt['gui'] = {'enabled': True, 'protocol_completed': False, 'render_updates': 10,
'hold_no_physics': True, 'close_reason': 'window_close'}
assert launch.complete_receipt(receipt, spec)
assert not launch.complete_analytic_prerequisite(receipt, spec)
def test_complete_receipt_requires_gui_proof_not_only_physical_pass():
from test_contact_cycle_skill_v4 import worker_receipt
spec = public.make_spec(cycles=3, gui=True)
receipt = worker_receipt(spec)
assert not launch.complete_receipt(receipt, spec)
receipt['gui'] = {'enabled': True, 'protocol_completed': True, 'render_updates': 10,
'hold_no_physics': True, 'close_reason': 'window_close'}
assert launch.complete_receipt(receipt, spec)
for key in ('protocol_completed', 'hold_no_physics'):
receipt['gui'][key] = False
assert not launch.complete_receipt(receipt, spec)
receipt['gui'][key] = True
@@ -0,0 +1,128 @@
"""CPU presentation ordering, not native-render/physics equivalence proof."""
import ast
from pathlib import Path
from types import SimpleNamespace as NS
import pytest
# Execute the actual presentation bodies with only the SDK-dependent recorder
# base declaration stubbed. Original recorder/safety behavior has its own suite;
# these tests concern the NEW callback ordering, not native renderer proof.
class RecorderBase:
def __call__(self,*a):raise AssertionError('test must explicitly supply original callback spy')
path=Path(__file__).resolve().parents[1]/'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/live_cadence_v042.py'
tree=ast.parse(path.read_text());tree.body=[n for n in tree.body if not isinstance(n,ast.ImportFrom)]
ns={'ScopedPackedSkillRecorder':RecorderBase};exec(compile(tree,str(path),'exec'),ns);M=NS(**ns)
def fixture():
calls=[]
def forbidden(*a,**k):pytest.fail('presentation attempted physical integration/update/reset')
viz=NS(cfg=NS(visualizer_type='kit'),is_closed=False,is_running=lambda:True,
is_rendering_paused=lambda:False,is_training_paused=lambda:False)
env=NS(cfg=NS(sim=NS(render_interval=20)),sim=NS(visualizers=[viz],render=lambda:calls.append('render'),step=forbidden),
scene=NS(update=forbidden,reset=forbidden),runtime_hooks=NS(violations=[]))
gui=NS(update=lambda e:calls.append('hud'),closed=False,app=NS(is_running=lambda:True))
state=NS(env=env,trial=NS(total_ticks=40))
return env,gui,state,calls
def event(g,causes=(),kind='tick'):
return NS(kind=kind,causes=causes,clock=NS(global_tick=g))
def test_one_render_per_new_verified_tick_original_interval20_retained():
env,gui,s,calls=fixture();p=M.VerifiedTickPainter(env,gui)
for g in range(1,41):
assert not p(event(g),s)
if g%20==0:env.sim.render() # original frozen env, not added callback
assert calls.count('render')==40 and p.additional_renders==38
assert env.cfg.sim.render_interval==20 and p.last_tick==40
assert not p.report()['adds_physics'] and not p.report()['interpolation']
@pytest.mark.parametrize('g',[1,3])
def test_repeated_or_missing_tick_rejected_not_repainted(g):
env,gui,s,calls=fixture();p=M.VerifiedTickPainter(env,gui);p(event(1),s);n=len(calls)
with pytest.raises(RuntimeError,match='new_verified'):p(event(g),s)
assert len(calls)==n
@pytest.mark.parametrize('physical,evidence,kind,hook',[
((),(),'tick',False),(('physical_failure',),(),'tick',False),
((),('evidence_failure',),'tick',False),((),(),'initial',False),((),(),'tick',True)])
def test_original_evidence_finishes_before_any_paint_and_failures_prevent_paint(monkeypatch,physical,evidence,kind,hook):
env,gui,s,calls=fixture()
def original(rec,e,st):calls.append('original_safety_and_synchronous_evidence');return evidence
monkeypatch.setattr(M.ScopedPackedSkillRecorder,'__call__',original)
rec=M.DisplaySkillRecorder.__new__(M.DisplaySkillRecorder)
rec.painter=lambda e,st:calls.append('paint') or ()
if hook:env.runtime_hooks.violations.append('guard_violation')
assert rec(event(1,physical,kind),s)==evidence
assert calls[0]=='original_safety_and_synchronous_evidence'
assert ('paint' in calls)==(not physical and not evidence and kind=='tick' and not hook)
def test_early_close_during_extra_render_returns_literal_abort_no_next_step():
env,gui,s,calls=fixture()
def close_on_render():calls.append('render');gui.closed=True
env.sim.render=close_on_render;p=M.VerifiedTickPainter(env,gui)
assert p(event(1),s)==('gui_window_closed_before_completion',)
assert calls==['hud','render']
def test_already_closed_prevents_extra_paint():
env,gui,s,calls=fixture();gui.closed=True;p=M.VerifiedTickPainter(env,gui)
assert p(event(1),s)==('gui_window_closed_before_completion',) and not calls
def test_final_tick_close_retains_complete_protocol_semantics():
env,gui,s,calls=fixture();s.trial.total_ticks=1
def close_on_render():calls.append('render');gui.closed=True
env.sim.render=close_on_render;p=M.VerifiedTickPainter(env,gui)
assert not p(event(1),s)
def test_render_exception_propagates_no_physical_call_or_retry():
env,gui,s,calls=fixture()
def fail():calls.append('render_error');raise RuntimeError('render_failed')
env.sim.render=fail;p=M.VerifiedTickPainter(env,gui)
with pytest.raises(RuntimeError,match='render_failed'):p(event(1),s)
assert calls==['hud','render_error'] and p.additional_renders==0
def test_sdk_swallowed_render_error_removes_viz_but_app_runs_fails_closed():
env,gui,s,calls=fixture();p=M.VerifiedTickPainter(env,gui)
def swallowed():
calls.append('sdk_returns_normally');env.sim.visualizers.clear()
env.sim.render=swallowed
with pytest.raises(RuntimeError,match='expected_Kit_visualizer_lost'):
p(event(1),s)
assert gui.app.is_running() and p.additional_renders==0
with pytest.raises(RuntimeError,match='expected_Kit_visualizer_lost'):
M.require_recorder_live(NS(painter=p))
@pytest.mark.parametrize('total',[20,40])
def test_original_interval20_failure_detected_before_next_physics_even_at_final_tick(total):
env,gui,s,calls=fixture();s.trial.total_ticks=total;p=M.VerifiedTickPainter(env,gui);rec=NS(painter=p)
with pytest.raises(RuntimeError,match='expected_Kit_visualizer_lost'):
for g in range(1,total+1):
M.require_recorder_live(rec) # actual before_physics guard
calls.append('physics')
p(event(g),s)
if g==20:env.sim.visualizers.clear() # SDK silently drops viz in original render
M.require_recorder_live(rec) # actual post-control/final-step check
assert calls.count('physics')==20 and p.additional_renders==19
@pytest.mark.parametrize('bad',['closed','not_running','render_paused','training_paused','replaced'])
def test_expected_concrete_viz_health_not_just_app_liveness(bad):
env,gui,s,calls=fixture();p=M.VerifiedTickPainter(env,gui)
if bad=='closed':p.kit.is_closed=True
elif bad=='not_running':p.kit.is_running=lambda:False
elif bad=='render_paused':p.kit.is_rendering_paused=lambda:True
elif bad=='training_paused':p.kit.is_training_paused=lambda:True
else:env.sim.visualizers=[NS(cfg=NS(visualizer_type='kit'),is_running=lambda:True)]
assert gui.app.is_running()
with pytest.raises(RuntimeError,match='expected_Kit_visualizer_lost'):
M.require_recorder_live(NS(painter=p))
assert not calls
def test_headless_or_other_modes_no_painter_no_visualizer_required():
M.require_recorder_live(NS(painter=None));M.require_recorder_live(NS());M.require_recorder_live(None)
@@ -0,0 +1,95 @@
"""Pure-CPU read-scope and unchanged-formula batching tests. Not native proof."""
import importlib.util
from pathlib import Path
from types import SimpleNamespace as NS, MethodType
import pytest
import torch
from SkillFactory.legs.spec import load_bundle
from SkillFactory.legs.contact_cycle_sim.reference import ReferenceController
ROOT=Path(__file__).resolve().parents[1]
TASK=ROOT/'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim'
def module(name):
spec=importlib.util.spec_from_file_location('_math_perf_'+name,TASK/(name+'.py'));m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m);return m
class State:
def __init__(self):
self.env=NS(runtime_hooks=NS(completed_steps=0,reset_calls=0,pose_write_calls=0))
self.command_clock=object();self.reference_clock=object();self.anchors=object();self.reads=0;self.value=torch.ones(1)
def root(self):self.reads+=1;return self.value
def test_scope_lifetime_restoration_no_global_tick_or_cross_invocation_cache():
m=module('readonly_scope_v042');s=State();original=dict(vars(s))
with m.readonly_scope(s):
assert s.root() is s.root();assert s.reads==1
assert 'root' not in vars(s)
s.value=torch.zeros(1)
with m.readonly_scope(s):assert s.root().item()==0 and s.reads==2
assert 'root' not in vars(s)
assert s.command_clock is original['command_clock']
def test_scope_restores_overrides_and_exceptions():
m=module('readonly_scope_v042');s=State();override=lambda:torch.ones(2);s.root=override
with pytest.raises(RuntimeError,match='injected'):
with m.readonly_scope(s):assert s.root().shape==(2,);raise RuntimeError('injected')
assert s.root is override
@pytest.mark.parametrize('change',['completed_steps','reset_calls','pose_write_calls','clock','anchors'])
def test_scope_rejects_any_physics_or_clock_boundary(change):
m=module('readonly_scope_v042');s=State()
with pytest.raises(RuntimeError,match='readonly_scope_changed'):
with m.readonly_scope(s):
s.root()
if change=='clock':s.command_clock=object()
elif change=='anchors':s.anchors=object()
else:setattr(s.env.runtime_hooks,change,1)
assert 'root' not in vars(s)
def test_scope_does_not_cache_mutating_history_or_policy_methods():
m=module('readonly_scope_v042')
assert not set(m.NAMES)&{'apply_loads','after_physics','reset','refresh','policy','objective','raw_sensors'}
@pytest.mark.parametrize('batch',[1,4])
def test_batched_jacobian_same_finite_difference_formula_cpu(batch):
m=module('batched_kinematics_v042');bundle=load_bundle(ROOT/'assets/generated/legs/go2_fl')
cfg=__import__('json').loads((ROOT/'configs/contact_cycle/simulation_residual_v1.json').read_text())['reference']
nominal=torch.tensor([j['q_default'] for j in bundle['spec']['joints']])
state=NS(bundle=bundle,nominal=nominal,env=NS(device='cpu',num_envs=1))
c=ReferenceController(state,cfg);q=nominal[None].repeat(batch,1)+torch.linspace(-.25,.25,batch*3).reshape(batch,3)
before={k:v.clone() for k,v in vars(c).items() if isinstance(v,torch.Tensor)}
old=ReferenceController.jacobian(c,q);new=m.jacobian(c,q)
torch.testing.assert_close(old,new,atol=5e-5,rtol=1e-5)
assert new.shape==old.shape and new.dtype==old.dtype and new.is_contiguous()
assert all(torch.equal(v,getattr(c,k)) for k,v in before.items())
assert c.cfg==cfg
def test_inexact_runtime_qualification_explicitly_retains_original():
m=module('batched_kinematics_v042');c=NS(jacobian=lambda q:'original');old=c.jacobian;env=NS(_cycle_state=NS(controller=c))
record={'applied':False,'reason':'not_exact_keep_original'}
assert m.install(env,record)==record and c.jacobian is old
def test_sampled_bitwise_gate_rejects_signed_zero_and_preserves_original(monkeypatch):
m=module('batched_kinematics_v042')
positive=torch.zeros(1,3,3,dtype=torch.float32);negative=-positive
assert torch.equal(positive,negative) # numeric equality is insufficient
assert not m.same_bits(positive,negative)
assert m.same_bits(positive,positive.clone())
class FakeController:
def __init__(self,*a):pass
def jacobian(self,q):return positive
monkeypatch.setattr(m,'ReferenceController',FakeController)
monkeypatch.setattr(m,'jacobian',lambda c,q:negative)
record=m.qualify({}, {'jacobian_epsilon_rad':.001}, torch.zeros(3),'cpu')
assert not record['all_bitwise_equal'] and not record['applied']
assert record['max_abs']==0 and record['reason']=='not_exact_keep_original'
c=NS(jacobian=lambda q:'original');old=c.jacobian
m.install(NS(_cycle_state=NS(controller=c)),record)
assert c.jacobian is old
@@ -0,0 +1,166 @@
"""CPU-only equivalence and fail-closed tests; not GPU/FPS qualification."""
import ast
import copy
import importlib.util
import json
import sys
from collections import defaultdict
from pathlib import Path
from types import SimpleNamespace as NS
import pytest
import torch
from test_contact_cycle_sim_evaluation_runtime import runtime, make_env
ROOT = Path(__file__).resolve().parents[1]
TASK = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim'
def standalone(name):
spec = importlib.util.spec_from_file_location('_perf_test_' + name, TASK / (name + '.py'))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def copy_function():
tree = ast.parse((TASK / 'packed_recording_v042.py').read_text())
function = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == 'copy_snapshot')
ns = {'torch': torch, 'defaultdict': defaultdict}
exec(compile(ast.Module(body=[function], type_ignores=[]), '<actual_copy_snapshot>', 'exec'), ns)
return ns['copy_snapshot']
@pytest.mark.parametrize('group_cpu', [False, True])
def test_packing_retains_exact_dtype_shape_bits_order_and_owned_storage(group_cpu):
source = {'f32': torch.tensor([[0., -0., float('nan')], [float('inf'), 1.25, -2.]]).T,
'f64': torch.tensor([1.123456789012345], dtype=torch.float64),
'integer': torch.tensor([2**60 + 7, -2**60 + 9], dtype=torch.int64),
'bool': torch.tensor([True, False]), 'empty': torch.empty(0, 3),
'scalar': torch.tensor(5., dtype=torch.float32)}
source['alias'] = source['f32']
out = copy_function()(source, group_cpu=group_cpu)
assert list(out) == list(source)
for k, v in source.items():
assert out[k].shape == v.shape and out[k].dtype == v.dtype and out[k].device.type == 'cpu'
assert out[k].numpy().tobytes() == v.numpy().tobytes()
before = {k: v.clone() for k, v in out.items()}
source['integer'].zero_();source['f32'].zero_()
for k, v in before.items():
assert out[k].numpy().tobytes() == v.numpy().tobytes()
out['alias'].fill_(42)
assert out['f32'].numpy().tobytes() == before['f32'].numpy().tobytes()
def test_packing_rejects_non_tensor_and_transfer_error(monkeypatch):
f = copy_function()
with pytest.raises(TypeError):f({'not_tensor': 1})
def fail(*a, **k):raise RuntimeError('injected_pack_failure')
monkeypatch.setattr(torch, 'cat', fail)
with pytest.raises(RuntimeError, match='injected_pack_failure'):
f({'a': torch.ones(1)}, group_cpu=True)
def assignment(path, target):
tree = ast.parse(path.read_text())
method = next(n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == 'snapshot')
return next(n.value for n in ast.walk(method) if isinstance(n, ast.Assign)
and any(isinstance(t, ast.Name) and t.id == target for t in n.targets))
def test_all_original_snapshot_expressions_are_identical_not_reinterpreted():
# The exact original expressions are checked, not another reimplementation.
assert ast.dump(assignment(TASK.parent / 'contact_cycle/recording.py', 'values')) == ast.dump(assignment(TASK / 'packed_recording_v042.py', 'values'))
assert ast.dump(assignment(TASK / 'recording.py', 'extra')) == ast.dump(assignment(TASK / 'packed_recording_v042.py', 'extra'))
class Sensor:
def __init__(self):self.value = {'generation': 0};self.reads = 0;self.fail = False;self.updates = []
@property
def data(self):
self.reads += 1
if self.fail:raise RuntimeError('sensor_read_failure')
return self.value
def update(self, dt, force_recompute=False):
self.updates.append((dt, force_recompute));self.value = {'generation': self.value['generation'] + 1}
if self.fail:raise RuntimeError('sensor_update_failure')
def reset(self, env_ids=None):
self.value = {'generation': 0}
if self.fail:raise RuntimeError('sensor_reset_failure')
def test_epoch_means_update_reset_not_global_tick_and_no_dropped_updates():
m = standalone('sensor_epoch_v042');source = Sensor();s = m.EpochSensor(source)
assert s.data is source.value
for _ in range(20):assert s.data is source.value
assert source.reads == 1
for dt, force in [(0., False), (.002, False), (0., True), (.002, True)]:
s.update(dt, force_recompute=force);assert s.data is source.value
assert s.data is source.value
assert source.reads == 5 and len(source.updates) == 4
s.reset();assert s.data is source.value and source.reads == 6
assert s.invalidations == 5
def test_failed_getter_does_not_cache_and_failed_update_invalidates():
m = standalone('sensor_epoch_v042');source = Sensor();s = m.EpochSensor(source);assert s.data
source.fail = True
with pytest.raises(RuntimeError, match='update'):s.update(.002)
for _ in range(2):
with pytest.raises(RuntimeError, match='read'):s.data
source.fail = False;assert s.data is source.value
source.fail = True
with pytest.raises(RuntimeError, match='reset'):s.reset()
with pytest.raises(RuntimeError, match='read'):s.data
def test_install_keeps_original_objects_and_rejects_double_install():
m = standalone('sensor_epoch_v042');original = Sensor()
class Scene:
sensors = {'contact': original}
def __getitem__(self, k):return self.sensors[k]
env = NS(scene=Scene());result = m.install(env)
assert env.scene['contact']._source is original
assert result['sensors'] == ['contact']
with pytest.raises(RuntimeError, match='once'):m.install(env)
def test_substep_two_keeps_offending_packed_frame_all_causes_and_no_render(make_env):
frames = [];pack = copy_function();renders = []
def evidence(event, s):
if event.kind != 'tick':return ()
f = pack({'value': torch.tensor([float('nan') if event.clock.global_tick == 2 else 1.])}, group_cpu=True)
frames.append(f)
return ('fault_one', 'fault_two') if not bool(torch.isfinite(f['value']).all()) else ()
env, hooks, _ = make_env(evidence=evidence)
env.cfg.sim.render_interval = 2;env.sim.is_rendering = True
env.sim.render = lambda **_: renders.append(True)
initial_calls = env.sim.calls # CPU fixture initialization has its own calls.
result = env.step(torch.zeros(1, 3))
assert result.stop.causes == ('fault_one', 'fault_two')
assert env.sim.calls - initial_calls == hooks.rollout_steps == 2 and len(frames) == 2 and not renders
assert bool(torch.isnan(frames[-1]['value']).all())
def test_transfer_exception_stops_before_third_step(make_env):
pack = copy_function();events = []
def evidence(event, s):
if event.kind == 'tick':
events.append(event.clock.global_tick)
if event.clock.global_tick == 2:pack({'bad': 1})
return ()
env, hooks, _ = make_env(evidence=evidence)
initial_calls = env.sim.calls
result = env.step(torch.zeros(1, 3))
assert env.sim.calls - initial_calls == hooks.rollout_steps == 2 and events == [1, 2]
assert any('recording_callback:TypeError' in x for x in result.stop.causes)
def test_options_and_loop_profile_do_not_step_or_change_render_interval(tmp_path):
m = standalone('performance_v042');p = tmp_path / m.CONFIG;p.parent.mkdir(parents=True)
p.write_text(json.dumps({'mode': 'baseline', 'profile': True}))
assert m.options(tmp_path)['mode'] == 'baseline'
p.write_text(json.dumps({'mode': 'nonsense', 'profile': True}))
with pytest.raises(ValueError):m.options(tmp_path)
p.write_text(json.dumps({'mode': 'baseline', 'profile': 1}))
with pytest.raises(ValueError):m.options(tmp_path)
+541
View File
@@ -0,0 +1,541 @@
"""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)