9fe9d27865
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.
575 lines
28 KiB
Python
575 lines
28 KiB
Python
"""CPU-only RootXY-v2 contracts. Synthetic data except explicitly saved46D parity.
|
|
|
|
No Kit, environment, actual pilot optimizer step, preparation or physical grant.
|
|
"""
|
|
import copy
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
import random
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from SkillFactory.legs.contact_cycle_sim import rootxy_training_v2 as p
|
|
from SkillFactory.legs.contact_cycle_sim.config import combine, load_config, pose_cost
|
|
from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic
|
|
from SkillFactory.legs.contact_cycle_sim.ppo import restore_checkpoint
|
|
from SkillFactory.legs.contact_cycle_sim.rootxy_diagnostics_v2 import RootXYMetrics
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def threads():
|
|
old = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
yield
|
|
torch.set_num_threads(old)
|
|
|
|
|
|
def test_inherited_guard_retries_only_disappearing_files_without_skipping_checks(monkeypatch):
|
|
ledger = object.__new__(p.RootXYBudgetLedger)
|
|
calls = []
|
|
def guard(self, reserve_bytes=0):
|
|
calls.append(reserve_bytes)
|
|
if len(calls) < 3:
|
|
raise FileNotFoundError('heartbeat atomic rename')
|
|
return 'checked'
|
|
monkeypatch.setattr(p.v1.FreshBudgetLedger, 'guard', guard)
|
|
assert ledger.guard(123) == 'checked'
|
|
assert calls == [123, 123, 123]
|
|
calls.clear()
|
|
def unstable(self, reserve_bytes=0):
|
|
calls.append(reserve_bytes)
|
|
raise FileNotFoundError('persistent churn')
|
|
monkeypatch.setattr(p.v1.FreshBudgetLedger, 'guard', unstable)
|
|
with pytest.raises(p.BudgetExceeded, match='unstable'):
|
|
ledger.guard(456)
|
|
assert calls == [456] * 4
|
|
def denied(self, reserve_bytes=0):
|
|
raise PermissionError('not a rename')
|
|
monkeypatch.setattr(p.v1.FreshBudgetLedger, 'guard', denied)
|
|
with pytest.raises(PermissionError):
|
|
ledger.guard()
|
|
|
|
|
|
def write(path, value):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(value))
|
|
return path
|
|
|
|
|
|
def test_exact_config_reward_and_old_identity_rejection():
|
|
cfg = p.load_training_config(ROOT / p.CONFIG_PATH, ROOT)
|
|
task = p.load_task_config(ROOT / p.TASK_PATH, ROOT)
|
|
assert cfg == p.PILOT and task['task_id'] == p.TASK_ID
|
|
assert cfg['num_envs'] * cfg['horizon'] * cfg['updates_per_seed'] == 1048576
|
|
assert cfg['max_validation_transitions'] == 128 * 900 * 7 == 806400
|
|
assert cfg['max_total_wall_s'] == 7200 and cfg['cleanup_reserve_s'] == 120
|
|
with pytest.raises(ValueError):
|
|
load_config(ROOT / p.TASK_PATH)
|
|
with pytest.raises(ValueError):
|
|
p.v1.load_training_config(ROOT / p.CONFIG_PATH, ROOT)
|
|
old = load_config(ROOT / p.BASE_PATH)
|
|
expected = copy.deepcopy(old)
|
|
expected['reward']['position_scale_m'] = [.03, .03, .04]
|
|
for field in ('task_id', 'format', 'scope'):
|
|
expected[field] = task[field]
|
|
assert task == expected
|
|
error = torch.tensor([[.03, 0, .04], [0, 0, 0], [1, 1, 1]], dtype=torch.float32)
|
|
new_cost, velocity = pose_cost(error, torch.zeros_like(error), task['reward'])
|
|
old_cost, _ = pose_cost(error, torch.zeros_like(error), old['reward'])
|
|
assert torch.allclose(new_cost, torch.tensor([2., 0., 10.]))
|
|
assert torch.allclose(old_cost, torch.tensor([1.36, 0., 10.]))
|
|
assert not velocity.any()
|
|
ref = torch.tensor([[1., -.5, -1.]])
|
|
_, _, effective = combine(ref, torch.tensor([[1., 1., -1.]]), .05, .5)
|
|
assert torch.allclose(effective, torch.tensor([[0., .05, 0.]]), atol=1e-7)
|
|
|
|
|
|
@pytest.mark.parametrize('key,value', [('seeds', [91501]), ('holdout_seed', 93511), ('num_envs', 64),
|
|
('max_validation_transitions', 806401), ('max_total_wall_s', 7201),
|
|
('updates_per_seed', True), ('resume', True)])
|
|
def test_scope_and_old_grants_rejected(tmp_path, key, value):
|
|
bad = copy.deepcopy(p.PILOT)
|
|
bad[key] = value
|
|
with pytest.raises(ValueError):
|
|
p.load_training_config(write(tmp_path / 'config.json', bad), ROOT)
|
|
with pytest.raises(PermissionError, match='INCOMPLETE'):
|
|
p.require_launch_admission({'format': p.v1.AUTHORIZATION_FORMAT, 'approved': True})
|
|
|
|
|
|
def test_override_whitelist_and_base_mutation(tmp_path):
|
|
bad = copy.deepcopy(p.TASK)
|
|
bad['overrides']['residual_limit_rad'] = .1
|
|
with pytest.raises(ValueError, match='whitelist'):
|
|
p.load_task_config(write(tmp_path / 'bad.json', bad), ROOT)
|
|
base = tmp_path / p.BASE_PATH
|
|
base.parent.mkdir(parents=True)
|
|
base.write_bytes((ROOT / p.BASE_PATH).read_bytes() + b' ')
|
|
with pytest.raises(ValueError, match='base config'):
|
|
p.load_task_config(ROOT / p.TASK_PATH, tmp_path)
|
|
|
|
|
|
def test_manifest_explicit_additive_identity_and_immutable_base():
|
|
manifest = p.make_manifest(ROOT, ROOT / p.CONFIG_PATH)
|
|
assert manifest['task_id'] == p.TASK_ID and manifest['launch_integration_complete'] is True
|
|
assert manifest['runtime_qualified'] is False
|
|
assert manifest['sources'][p.BASE_PATH] == p.BASE_SHA256
|
|
for suffix in ('rootxy_training_v2.py', 'rootxy_diagnostics_v2.py', 'rootxy_runner_v2.py',
|
|
'rootxy_cfg_v2.py', 'rootxy_recording_v2.py', 'train_cycle_sim_rootxy_v2.py',
|
|
'test_contact_cycle_sim_rootxy_v2.py'):
|
|
assert any(name.endswith(suffix) for name in manifest['sources'])
|
|
assert manifest['warmstart_source']['candidate_sha256'] == p.replay.CANDIDATE_SHA256
|
|
|
|
|
|
def test_actual_frozen_saved46d_actor_parity_fresh_critic_Adam_RNG():
|
|
trace = torch.load(ROOT / Path(p.replay.CANDIDATE).parent / 'validation_u0256_trace.pt',
|
|
map_location='cpu', weights_only=True)
|
|
first = next(iter(trace.values()))
|
|
x = first['actor_input'][::100, 0].contiguous()
|
|
assert x.shape[1] == 46 and x.shape[0] == 45
|
|
model, optimizer, frozen, evidence = p.import_actor_weights_only(ROOT, x)
|
|
assert not optimizer.state
|
|
assert evidence['observations'] == 45 and not evidence['physical_repeat']
|
|
assert evidence['source']['candidate_sha256'] == p.replay.CANDIDATE_SHA256
|
|
assert evidence['source']['manifest_sha256'] == p.replay.MANIFEST_SHA256
|
|
rng = torch.get_rng_state().clone()
|
|
with torch.random.fork_rng(devices=[]):
|
|
torch.manual_seed(92511)
|
|
expected = SimResidualActorCritic(**p.MODEL_CONFIG)
|
|
assert torch.equal(rng, torch.get_rng_state())
|
|
for k, v in model.critic.state_dict().items():
|
|
assert torch.equal(v, expected.critic.state_dict()[k])
|
|
assert any(not torch.equal(v, frozen.critic.state_dict()[k]) for k, v in model.critic.state_dict().items())
|
|
for name in evidence['imported_keys']:
|
|
assert torch.equal(model.state_dict()[name], frozen.state_dict()[name])
|
|
assert random.random() == random.Random(92511).random()
|
|
with torch.no_grad():
|
|
assert torch.equal(model.act(x, deterministic=True)[0], frozen.act(x, deterministic=True)[0])
|
|
# Fixed46D feature offsets/scales remain v1, including root error indices32:35.
|
|
assert torch.equal(model.offset, frozen.offset) and torch.equal(model.scale, frozen.scale)
|
|
assert torch.equal(model.scale[32:35], torch.full((3,), .1))
|
|
|
|
|
|
def tick(metrics, index, xy=.02, z=0., safe=True):
|
|
n = metrics.num_envs
|
|
metrics.tick(time=torch.full((n,), index * .002), position_error=torch.tensor([[xy, 0., z]]).repeat(n, 1),
|
|
phase=torch.full((n,), min(index // 750, 5), dtype=torch.long),
|
|
reference=torch.tensor([[1., 0., 0.]]).repeat(n, 1),
|
|
request=torch.ones(n, 3), effective_residual=torch.zeros(n, 3),
|
|
safety_ok=torch.full((n,), safe))
|
|
|
|
|
|
def test_physics_latches_recovery_cap_authority_and_partial_ticks():
|
|
m = RootXYMetrics(2)
|
|
tick(m, 1, .031, .2)
|
|
tick(m, 2, .001)
|
|
m.end_control(torch.tensor([True, False]), torch.tensor([False, False]))
|
|
tick(m, 3, .001)
|
|
rows = m.rows()
|
|
assert rows[0]['physics_samples'] == 2 and rows[1]['physics_samples'] == 3
|
|
assert all(r['rootxy_breached'] and r['first_rootxy_breach_tick'] == 1 for r in rows)
|
|
assert not rows[0]['complete_clock'] and rows[0]['rms_scope'] == 'observed_prefix_only'
|
|
assert rows[0]['position_cap_fraction'] == .5
|
|
assert rows[0]['reference_saturated_fraction'] == 1.
|
|
assert rows[0]['effective_authority_shortfall_max_rad'] == .05
|
|
assert rows[0]['uncapped_xy_cost_mean'] > 0
|
|
with pytest.raises(ValueError, match='Missing'):
|
|
RootXYMetrics(1).rows()
|
|
|
|
|
|
def good_rows():
|
|
return [dict(env=e, success=True, physics_samples=4500, complete_clock=True, rootxy_rms_m=.01,
|
|
rootxy_max_m=.02, rootxy_breached=False, safety_ok=True, height_rms_m=.02,
|
|
swing_rms_m=.01, landing_error_m=.01) for e in range(128)]
|
|
|
|
|
|
def test_selection_fullphase_strict_gate_then_RMS_tracking_earliest():
|
|
rows = good_rows()
|
|
key = p.candidate_key(rows, 64)
|
|
assert key < p.candidate_key(rows, 128)
|
|
improved = copy.deepcopy(rows)
|
|
for row in improved:
|
|
row['rootxy_rms_m'] = .009
|
|
row['height_rms_m'] = .05
|
|
assert p.candidate_key(improved, 256) < key
|
|
rows[6]['rootxy_max_m'] = .031
|
|
rows[6]['rootxy_breached'] = True
|
|
rows[6]['episode_return'] = 1e9
|
|
assert p.candidate_key(rows, 64) is None
|
|
rows[6]['rootxy_max_m'] = .02 # latching still disqualifies after recovery
|
|
assert p.candidate_key(rows, 64) is None
|
|
rows[6]['success'] = False
|
|
with pytest.raises(RuntimeError, match='regression'):
|
|
p.candidate_key(rows, 64)
|
|
|
|
|
|
@pytest.mark.parametrize('field,value', [('physics_samples', 4499), ('complete_clock', False),
|
|
('rootxy_rms_m', float('nan')), ('safety_ok', False),
|
|
('rootxy_breached', None)])
|
|
def test_selection_missing_partial_safety_fail_closed(field, value):
|
|
rows = good_rows()
|
|
rows[0][field] = value
|
|
with pytest.raises(ValueError):
|
|
p.candidate_key(rows, 64)
|
|
|
|
|
|
def test_budget_no_retry_separate_validation_and_cleanup_deadline(tmp_path):
|
|
clock = [20.]
|
|
manifest = {'synthetic_cpu_only': True, 'training': p.PILOT}
|
|
def ledger():
|
|
return p.RootXYBudgetLedger(tmp_path, manifest, launch_start_s=10., now=lambda: 100.,
|
|
boot_now=lambda: clock[0], boot_id='cpu-test')
|
|
with ledger() as budget:
|
|
assert budget.data['clock']['deadline_s'] == 7210
|
|
assert budget.data['validation_reserved_transitions'] == 806400
|
|
assert budget.data['reserved_transitions'] == 0
|
|
budget.begin_validation('analytic_zero')
|
|
budget.before_validation_control()
|
|
budget.after_validation_control()
|
|
budget.reserve_rollout(92511, 1)
|
|
assert budget.data['reserved_transitions'] == 4096
|
|
with pytest.raises(ValueError):
|
|
budget.begin_validation('analytic_zero')
|
|
with pytest.raises(ValueError):
|
|
budget.reserve_rollout(92511, 1)
|
|
with pytest.raises(PermissionError):
|
|
budget.reconcile_checkpoint(92511, 1)
|
|
clock[0] = 7090
|
|
with pytest.raises(p.BudgetExceeded, match='Cleanup'):
|
|
budget.work_guard()
|
|
budget.guard() # cleanup publication remains inside7200s
|
|
with pytest.raises(FileExistsError):
|
|
with ledger():
|
|
pass
|
|
persisted = json.loads((tmp_path / 'budget.json').read_text())
|
|
assert persisted['reserved_transitions'] == 4096 and persisted['pending']['update'] == 1
|
|
|
|
|
|
def checkpoint_inputs():
|
|
return ({'task_id': p.TASK_ID, 'contract': p.CONTRACT, 'manifest_sha256': 'a' * 64,
|
|
'resolved_task': p.load_task_config(ROOT / p.TASK_PATH, ROOT),
|
|
'source_sha256': p.replay.CANDIDATE_SHA256, 'seed': 92511},
|
|
{'source': {'candidate_sha256': p.replay.CANDIDATE_SHA256,
|
|
'manifest_sha256': p.replay.MANIFEST_SHA256},
|
|
'fresh_seed': 92511, 'parity': 'exact_CPU_action_mean_std'})
|
|
|
|
|
|
def test_v2_checkpoint_eval_only_atomic_budget_old_restore_rejects(tmp_path):
|
|
model = SimResidualActorCritic(**p.MODEL_CONFIG)
|
|
identity, imported = checkpoint_inputs()
|
|
path = tmp_path / 'v2.pt'
|
|
with pytest.raises(p.BudgetExceeded):
|
|
p.save_checkpoint(path, model, identity, 64, imported, {}, 1)
|
|
assert not list(tmp_path.iterdir())
|
|
sha = p.save_checkpoint(path, model, identity, 64, imported, {}, 1048576)
|
|
rng = torch.get_rng_state().clone()
|
|
restored, data = p.load_evaluation_policy(path, sha, identity)
|
|
assert torch.equal(rng, torch.get_rng_state())
|
|
assert 'optimizer' not in data and 'rng' not in data and data['resume'] is False
|
|
assert all(torch.equal(v, restored.state_dict()[k]) for k, v in model.state_dict().items())
|
|
with pytest.raises(ValueError):
|
|
restore_checkpoint(path, model, torch.optim.Adam(model.parameters()), identity, p.MODEL_CONFIG)
|
|
with pytest.raises(ValueError):
|
|
p.replay.load_frozen_policy(path, {})
|
|
with pytest.raises(ValueError):
|
|
p.load_evaluation_policy(path, '0' * 64, identity)
|
|
with pytest.raises(FileExistsError):
|
|
p.save_checkpoint(path, model, identity, 64, imported, {}, 1048576)
|
|
bad = dict(identity, task_id=p.replay.TASK_ID)
|
|
with pytest.raises(ValueError):
|
|
p.load_evaluation_policy(path, sha, bad)
|
|
|
|
|
|
def test_cli_run_barrier_precedes_import_output_and_authorization(tmp_path, monkeypatch):
|
|
spec = importlib.util.spec_from_file_location('rootxy_cli_test', ROOT / 'scripts/train_cycle_sim_rootxy_v2.py')
|
|
cli = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(cli)
|
|
def forbidden():
|
|
raise AssertionError('run must fail before Torch/protocol imports')
|
|
monkeypatch.setattr(cli, 'cpu_protocol', forbidden)
|
|
with pytest.raises(PermissionError, match='INCOMPLETE'):
|
|
cli.main(['--run', '--output', str(tmp_path / 'unused'), '--authorization', 'old_grant.json'])
|
|
assert not list(tmp_path.iterdir())
|
|
with pytest.raises(SystemExit):
|
|
cli.main(['--check-only', '--output', str(tmp_path / 'unused')])
|
|
|
|
|
|
def test_v1_frozen_hashes_unchanged():
|
|
manifest, _ = p.replay.verify_bundle(ROOT)
|
|
assert p.digest(manifest) == p.replay.MANIFEST_SHA256
|
|
assert hashlib.sha256((ROOT / p.replay.CANDIDATE).read_bytes()).hexdigest() == p.replay.CANDIDATE_SHA256
|
|
|
|
|
|
def test_full_saved_episode_streaming_metrics_match_direct_reduction_and_compact_audit():
|
|
# Retrospective tensor reduction only: no policy call or simulator step.
|
|
trace = torch.load(ROOT / Path(p.replay.CANDIDATE).parent / 'validation_u0256_trace.pt',
|
|
map_location='cpu', weights_only=True)
|
|
episode = next(iter(trace.values()))
|
|
metrics = RootXYMetrics(1)
|
|
for i in range(4500):
|
|
metrics.tick(time=episode['time'][i], position_error=episode['policy_snapshot'][i, :, 32:35],
|
|
phase=episode['phase'][i], reference=episode['reference_used_normalized'][i],
|
|
request=episode['residual_request_normalized'][i],
|
|
effective_residual=episode['effective_residual_rad'][i],
|
|
safety_ok=torch.ones(1, dtype=torch.bool))
|
|
metrics.end_control(torch.zeros(1, dtype=torch.bool), torch.ones(1, dtype=torch.bool))
|
|
row = metrics.rows()[0]
|
|
xy = episode['policy_snapshot'][:, 0, 32:34].double().square().sum(-1).sqrt()
|
|
assert row['complete_clock'] and row['rms_scope'] == 'full_episode'
|
|
assert row['rootxy_rms_m'] == pytest.approx(float(xy.square().mean().sqrt()), abs=1e-14)
|
|
assert row['rootxy_max_m'] == float(xy.max())
|
|
assert row['rootxy_breached'] == bool((xy > .03).any())
|
|
hits = (xy > .03).nonzero().flatten()
|
|
assert row['first_rootxy_breach_tick'] == (int(hits[0]) + 1 if len(hits) else None)
|
|
assert sum(row['phase_physics_samples']) == 4500
|
|
with pytest.raises(ValueError, match='Extra'):
|
|
metrics.live.fill_(True)
|
|
tick(metrics, 4501)
|
|
spec = importlib.util.spec_from_file_location('rootxy_original_functional_audit',
|
|
ROOT / 'scripts/audit_contact_cycle_functional.py')
|
|
audit = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(audit)
|
|
cfg = json.loads((ROOT / p.PILOT['functional_config']).read_text())
|
|
fields = {'time', 'phase', 'scenario', 'weight', 'normal', 'foot', 'target', 'gap',
|
|
'signed_height_error', 'tilt', 'external', 'guide', 'auxiliary', 'joint_excess',
|
|
'limit_ratio', 'contact_acquired', 'actor_metadata_available', 'control_action_available'}
|
|
assert audit.evaluate({k: episode[k] for k in fields}, cfg, 'learned_policy') == audit.evaluate(
|
|
episode, cfg, 'learned_policy')
|
|
|
|
|
|
def test_missing_tick_and_nonfinite_are_not_full_episode_evidence():
|
|
metrics = RootXYMetrics(1)
|
|
tick(metrics, 1)
|
|
tick(metrics, 3) # missing tick2; cannot repair by recording a later sample
|
|
metrics.end_control(torch.zeros(1, dtype=torch.bool), torch.ones(1, dtype=torch.bool))
|
|
assert not metrics.rows()[0]['complete_clock'] and not bool(metrics.clock_ok[0])
|
|
with pytest.raises(ValueError, match='physics-tick'):
|
|
tick(RootXYMetrics(1), 1, float('nan'))
|
|
|
|
|
|
def test_control_adapter_guard_debit_and_failure_do_not_count_completed_physics():
|
|
from SkillFactory.legs.contact_cycle_sim.rootxy_runner_v2 import _ControlledEnv, RuntimeHooks
|
|
calls = []
|
|
class Env:
|
|
def step(self, action):
|
|
calls.append('env_step')
|
|
if action == 'fail':
|
|
raise RuntimeError('synthetic partial physics failure')
|
|
return action
|
|
hooks = RuntimeHooks(admit=lambda _: {}, guard=lambda: None, verify_identity=lambda: None,
|
|
before_control=lambda kind: calls.append('before_' + kind),
|
|
after_control=lambda kind: calls.append('after_' + kind),
|
|
progress=lambda _: None, finish=lambda _: None)
|
|
controlled = _ControlledEnv(Env(), hooks, lambda: calls.append('guard'))
|
|
assert controlled.step('ok') == 'ok'
|
|
assert calls == ['guard', 'before_optimization', 'env_step', 'after_optimization', 'guard']
|
|
calls.clear()
|
|
with pytest.raises(RuntimeError, match='partial'):
|
|
controlled.step('fail')
|
|
assert calls == ['guard', 'before_optimization', 'env_step']
|
|
|
|
|
|
@pytest.fixture
|
|
def execute_fixture(tmp_path, monkeypatch):
|
|
"""Real execute control flow; synthetic env/recorder and no optimizer/physics."""
|
|
from contextlib import nullcontext
|
|
from types import ModuleType, SimpleNamespace as NS
|
|
from SkillFactory.legs.contact_cycle_sim import rootxy_runner_v2 as runner
|
|
|
|
state = NS(update=0, label=None, fault=None, finished=[], publications=[], selected=[])
|
|
manifest = {'sources': {'CPU': True}, 'resolved_task': {'CPU': True}}
|
|
cfg = copy.deepcopy(p.PILOT)
|
|
functional = tmp_path / cfg['functional_config']
|
|
write(functional, {'CPU': True})
|
|
output = tmp_path / 'output'; output.mkdir()
|
|
monkeypatch.setattr(p, 'load_training_config', lambda *_: cfg)
|
|
monkeypatch.setattr(p, 'make_manifest', lambda *_: manifest)
|
|
monkeypatch.setattr(p, 'source_identity', lambda *_: manifest['sources'])
|
|
monkeypatch.setattr(p.v1, 'verify_snapshot', lambda *_: None)
|
|
model = torch.nn.Linear(1, 1)
|
|
monkeypatch.setattr(p, 'import_actor_weights_only', lambda *_: (
|
|
model, NS(state={}), torch.nn.Linear(1, 1), {'CPU': True}))
|
|
monkeypatch.setattr(runner, 'features', lambda x: x)
|
|
monkeypatch.setattr(runner, 'isolated_validation_rng', lambda *_: nullcontext())
|
|
monkeypatch.setattr(runner, 'inference_action', lambda *_: (torch.zeros(128, 3), None, None))
|
|
monkeypatch.setattr(runner, 'save_trace_subset', lambda *_: None)
|
|
|
|
def save(path, _model, _identity, update, *_):
|
|
path.write_bytes(str(update).encode())
|
|
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
monkeypatch.setattr(p, 'save_checkpoint', save)
|
|
|
|
def optimize(_env, _model, _optimizer, x, _algo, _seed, update, *_):
|
|
state.update = update
|
|
return x, 0, 0, {}
|
|
monkeypatch.setattr(runner, 'optimize_reserved_rollout', optimize)
|
|
|
|
class Ledger:
|
|
data = {'reserved_transitions': 0}
|
|
def work_guard(self):
|
|
pass
|
|
def guard(self, **_):
|
|
pass
|
|
def write_json(self, path, value):
|
|
if path.name == 'result.json' and state.fault == 'publication' and value['training_complete']:
|
|
raise OSError('injected final publication')
|
|
write(path, value)
|
|
state.publications.append((path.name, copy.deepcopy(value)))
|
|
def append_json(self, *_):
|
|
pass
|
|
def begin_validation(self, label):
|
|
state.label = label
|
|
def before_validation_control(self):
|
|
pass
|
|
def after_validation_control(self):
|
|
pass
|
|
def commit_update(self, seed, update):
|
|
if state.fault == 'accounting' and update == 128:
|
|
raise RuntimeError('injected accounting')
|
|
|
|
class Recorder:
|
|
def __init__(self, *_):
|
|
self.initial_pair = {'CPU': torch.zeros(1)}
|
|
self.metrics = NS(live=torch.ones(128, dtype=torch.bool))
|
|
self.columns, self.stops = {}, []
|
|
def __enter__(self):
|
|
return self
|
|
def __exit__(self, *_):
|
|
pass
|
|
def before_control(self, *_):
|
|
pass
|
|
def end_control(self, *_):
|
|
pass
|
|
def rows(self, _):
|
|
rows = good_rows()
|
|
if state.fault == 'validation' and state.label == 'u0128':
|
|
rows[0]['success'] = False
|
|
return rows
|
|
|
|
task = 'SkillFactory.tasks.manager_based.contact_cycle_sim.'
|
|
for leaf, attributes in (
|
|
('rootxy_cfg_v2', {'verify_actual_config': lambda _: None}),
|
|
('rootxy_recording_v2', {'RootXYRecorder': Recorder})):
|
|
module = ModuleType(task + leaf)
|
|
vars(module).update(attributes)
|
|
monkeypatch.setitem(sys.modules, task + leaf, module)
|
|
observation = {'policy': torch.zeros(128, 46)}
|
|
env = NS(device='cpu', num_envs=128, reset=lambda: (observation, {}),
|
|
step=lambda _: (observation, torch.zeros(128), torch.zeros(128, dtype=torch.bool),
|
|
torch.zeros(128, dtype=torch.bool), {}))
|
|
|
|
def identity():
|
|
if state.fault == 'identity' and state.update == 128:
|
|
raise RuntimeError('injected identity')
|
|
|
|
def finish(result):
|
|
state.finished.append(result) # notification object, never an authoritative lock
|
|
if state.fault == 'finish':
|
|
raise RuntimeError('injected finish hook')
|
|
|
|
hooks = runner.RuntimeHooks(
|
|
admit=lambda _: dict(manifest_sha256=p.digest(manifest), p2_review_approved=True,
|
|
sole_cleanup_owner=True, native_monitor_active=True),
|
|
guard=lambda: None, verify_identity=identity, before_control=lambda _: None,
|
|
after_control=lambda _: None, progress=lambda _: None, finish=finish)
|
|
state.run = lambda: runner.execute(env, tmp_path, output, manifest, Ledger(), [], hooks, lambda *_: {})
|
|
state.output = output
|
|
return state
|
|
|
|
|
|
@pytest.mark.parametrize('fault', ['validation', 'identity', 'accounting', 'publication', 'finish'])
|
|
def test_execute_invalidates_eligible_u64_on_later_failure(execute_fixture, fault):
|
|
f = execute_fixture
|
|
f.fault = fault
|
|
with pytest.raises((RuntimeError, OSError)):
|
|
f.run()
|
|
assert (f.output / 'checkpoint_u0064.pt').exists() # forensic only
|
|
terminal = json.loads((f.output / 'result.json').read_text())
|
|
for result in (terminal, *f.finished):
|
|
assert result['status'] == 'failed'
|
|
assert result['candidate'] is None and result['training_complete'] is False
|
|
assert result['checkpoints_selectable'] is False and 'outcome' not in result
|
|
assert not (f.output / 'locked_candidate.json').exists()
|
|
assert not list(f.output.glob('*lock*'))
|
|
|
|
|
|
def test_execute_success_is_only_provisional_until_external_owner_commits(execute_fixture):
|
|
f = execute_fixture
|
|
result = f.run()
|
|
assert result['training_complete'] and result['candidate']['update'] == 64
|
|
assert result['outcome'] == 'provisional_candidate' and result['status'] == 'completed_provisional'
|
|
assert result['checkpoints_selectable'] is False and result['accepted_skill'] is False
|
|
assert 'supervisor waited-exit/postexit-identity/cleanup' in result['candidate_authority']
|
|
assert not (f.output / 'locked_candidate.json').exists()
|
|
|
|
|
|
def test_actual_rootxy_recorder_restores_callback_and_rejects_missing_metadata(monkeypatch):
|
|
"""Actual recorder enter/control/exit seams with CPU state; no scene physics."""
|
|
from types import ModuleType, SimpleNamespace as NS
|
|
package = ModuleType('_rootxy_recorder_cpu')
|
|
package.__path__ = [str(ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim')]
|
|
monkeypatch.setitem(sys.modules, package.__name__, package)
|
|
z = torch.zeros(1, 3)
|
|
state = NS(bundle={'joints': [{'limits': {'lower': -1, 'upper': 1, 'velocity': 1}}] * 3},
|
|
policy=lambda: torch.zeros(1, 46), root=lambda: (z,), q=lambda: z,
|
|
port_reference=lambda: (z,), reference=lambda: (None, None, z),
|
|
controller=NS(action=z), contact=lambda: torch.zeros(1), foot_center=lambda: z)
|
|
mdp = ModuleType(package.__name__ + '.mdp'); mdp.state = lambda _: state; mdp.base = NS()
|
|
monkeypatch.setitem(sys.modules, mdp.__name__, mdp)
|
|
spec = importlib.util.spec_from_file_location(package.__name__ + '.rootxy_recording_v2',
|
|
Path(package.__path__[0]) / 'rootxy_recording_v2.py')
|
|
module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module)
|
|
calls = []
|
|
callback = lambda dt: calls.append(dt)
|
|
env = NS(num_envs=1, device='cpu', scene=NS(update=callback))
|
|
with pytest.raises(ValueError, match='missing control metadata'):
|
|
with module.RootXYRecorder(env, {}, False) as recorder:
|
|
assert recorder.initial_pair['policy'].shape == (1, 46)
|
|
env.scene.update(.002)
|
|
assert env.scene.update is callback and calls == [.002]
|
|
with module.RootXYRecorder(env, {}, False) as recorder:
|
|
recorder.before_control(torch.zeros(1, 46), z, None, None)
|
|
assert recorder.action_available
|
|
with pytest.raises(ValueError, match='zero residual'):
|
|
recorder.before_control(torch.zeros(1, 46), torch.ones(1, 3), None, None)
|
|
assert env.scene.update is callback
|
|
|
|
|
|
def test_actual_rootxy_config_construction_uses_base_then_only_reviewed_override(monkeypatch):
|
|
"""Actual additive config code over a minimal CPU base, not installed Isaac cfg qualification."""
|
|
from types import ModuleType, SimpleNamespace as NS
|
|
directory = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim'
|
|
package = ModuleType('_rootxy_cfg_cpu'); package.__path__ = [str(directory)]
|
|
monkeypatch.setitem(sys.modules, package.__name__, package)
|
|
calls = []
|
|
|
|
class Base:
|
|
def __init__(self):
|
|
self.__post_init__()
|
|
def __post_init__(self):
|
|
calls.append('unchanged_base')
|
|
self.residual_parameters = {'base': True}
|
|
self.actions = NS(joint_position=NS(class_type=action))
|
|
action = type('OriginalAction', (), {})
|
|
base = ModuleType(package.__name__ + '.cfg'); base.SimContactCycleEnvCfg = Base
|
|
mdp = ModuleType(package.__name__ + '.mdp'); mdp.SimResidualAction = action
|
|
decorator = ModuleType('isaaclab.utils.configclass'); decorator.configclass = lambda cls: cls
|
|
for name, module in ((base.__name__, base), (mdp.__name__, mdp), (decorator.__name__, decorator)):
|
|
monkeypatch.setitem(sys.modules, name, module)
|
|
spec = importlib.util.spec_from_file_location(package.__name__ + '.rootxy_cfg_v2', directory / 'rootxy_cfg_v2.py')
|
|
module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module)
|
|
cfg = module.RootXYContactCycleEnvCfg()
|
|
assert calls == ['unchanged_base']
|
|
assert cfg.residual_parameters == p.load_task_config(ROOT / p.TASK_PATH, ROOT)
|
|
assert cfg.actions.joint_position.class_type is action
|
|
assert cfg.task_identity == p.TASK_ID and cfg.residual_config == str(ROOT / p.BASE_PATH)
|
|
monkeypatch.setattr(module.RootXYContactCycleEnvCfg, 'task_identity', 'old-or-forged')
|
|
with pytest.raises(ValueError, match='pinned v1 base'):
|
|
module.RootXYContactCycleEnvCfg()
|
|
assert calls == ['unchanged_base'] # rejected before base construction
|