91 lines
3.6 KiB
Python
91 lines
3.6 KiB
Python
"""CPU-only public command validation and exact supervisor-entry regression."""
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def load(name, path):
|
|
spec = importlib.util.spec_from_file_location(name, ROOT / path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
api = load('four_api', 'source/SkillFactory/SkillFactory/legs/four_skills/api.py')
|
|
cli = load('four_cli', 'scripts/four_skills.py')
|
|
runtime = load('four_runtime', 'scripts/four_skills_runtime.py')
|
|
|
|
|
|
def test_paused_stage_cannot_create_or_reuse_authority(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(cli, 'STAGE', tmp_path)
|
|
(tmp_path / 'PAUSED.json').write_text('{"reason":"user"}')
|
|
with pytest.raises(PermissionError, match='paused by user'):
|
|
cli.scope()
|
|
assert not (tmp_path / 'scope.json').exists()
|
|
|
|
|
|
def test_exact_supervisor_command_relative_and_absolute():
|
|
script = ROOT / 'scripts/four_skills.py'
|
|
for name in (str(script), 'scripts/four_skills.py'):
|
|
assert cli.supervisor_command_matches([b'/usr/bin/python3.12', name.encode(), b'smoke'], ROOT)
|
|
assert not cli.supervisor_command_matches([b'python', b'/tmp/four_skills.py', b'smoke'], ROOT)
|
|
assert not cli.supervisor_command_matches([b'python', str(script).encode(), b'--_worker-fd'], ROOT)
|
|
assert not cli.supervisor_command_matches([b'python', b'scripts/four_skills.py', b'smoke'], '/tmp')
|
|
assert not cli.supervisor_command_matches([b'python'], ROOT)
|
|
|
|
|
|
class Sink:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def request(self, *args):
|
|
self.calls.append(args)
|
|
return args
|
|
|
|
|
|
@pytest.mark.parametrize('kwargs', [dict(skill='unknown'), dict(skill='support', direction_m=[.01, .02]),
|
|
dict(skill='swing', direction_m=[0., 0.]), dict(skill='swing', direction_m=[.1, 0.]),
|
|
dict(skill='swing', direction_m=[True, .02]), dict(skill='support', hold_seconds=True),
|
|
dict(skill='support', hold_seconds=float('nan')), dict(skill='support', hold_seconds=11),
|
|
dict(skill='support', external_force=[1, 2, 3]), dict(skill='support', desired_force=50)])
|
|
def test_invalid_public_command_never_mutates_state(kwargs):
|
|
state = Sink()
|
|
service = api.SkillService(state)
|
|
with pytest.raises((ValueError, TypeError)):
|
|
service.call(**kwargs)
|
|
assert state.calls == []
|
|
|
|
|
|
def test_public_fields_only_and_legal_graph():
|
|
s = Sink()
|
|
api.SkillService(s).call('swing', hold_seconds=.3, direction_m=[.02, .01])
|
|
assert s.calls == [('swing', .3, (.02, .01), None)]
|
|
assert api.LEGAL == {'support': ('retract',), 'retract': ('swing', 'plant'),
|
|
'swing': ('swing', 'plant'), 'plant': ('support',)}
|
|
|
|
|
|
def test_training_composer_one_transition_per_row_per_feedback_sample():
|
|
from types import SimpleNamespace
|
|
import torch
|
|
s = SimpleNamespace(episode_ids=[0, 0], skill=torch.tensor([0, 2]),
|
|
ready=torch.ones(2, dtype=torch.bool), held=torch.ones(2), hold_required=torch.zeros(2),
|
|
failed=torch.zeros(2, dtype=torch.bool), stopped=torch.zeros(2, dtype=torch.bool))
|
|
calls = []
|
|
class Service:
|
|
def call(self, skill, *, env_ids, **kwargs):
|
|
assert bool(s.ready[env_ids].all())
|
|
calls.append((skill, env_ids.tolist()))
|
|
s.skill[env_ids] = api.SKILLS.index(skill)
|
|
s.ready[env_ids] = False
|
|
env = SimpleNamespace(s=s, service=Service(), num_envs=2, device='cpu')
|
|
driver = runtime.TrainingComposer(env)
|
|
driver.update()
|
|
assert calls == [('retract', [0]), ('plant', [1])]
|
|
assert driver.transition_count == 2
|
|
driver.update()
|
|
assert len(calls) == 2
|