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
+185
View File
@@ -0,0 +1,185 @@
#!/usr/bin/env python3
"""CPU-only function-first trace audit. Never starts Kit or grants execution.
Checks one or three *globally clocked* cycles; never joins separate episodes.
A tensor trace cannot prove the absence of simulator state writes. Therefore
trace_functional_passed is NOT a runtime-continuity or new simulation claim.
"""
import argparse
import hashlib
import importlib
import importlib.util
import json
import sys
from pathlib import Path
import torch
from audit_contact_cycle_functional import evaluate as evaluate_cycle
ROOT = Path(__file__).resolve().parents[1]
PHASES = ('support', 'unload', 'lift', 'swing', 'landing', 'reload')
FAILURES = ('nonfinite', 'contact_acquisition_timeout', 'premature_separation', 'lift_deadline',
'swing_clearance', 'reload_deadline', 'low_height', 'tilt')
DEFAULT_CONFIG = ROOT / 'configs/contact_cycle/functional_flow_v2.json'
def cpu_protocol():
"""Private CPU namespace only; NEVER a runtime/binding import alias."""
name = '_functional_flow_cpu'
directory = ROOT / 'source/SkillFactory/SkillFactory/legs/contact_cycle_sim'
if name not in sys.modules:
spec = importlib.util.spec_from_file_location(name, directory / '__init__.py',
submodule_search_locations=[str(directory)])
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return importlib.import_module(name + '.functional_flow_v2')
def root_error(trace):
for key in ('root_position_error_m', 'root_position_error'):
if key in trace:
return trace[key]
if 'policy_snapshot' in trace and trace['policy_snapshot'].shape[-1] == 46:
return trace['policy_snapshot'][..., 32:35]
return None
def phase_summary(trace):
result = {}
error = root_error(trace)
for phase, name in enumerate(PHASES):
mask = trace['phase'][:, 0] == phase
if not mask.any():
result[name] = {'samples': 0, 'status': 'not_evaluated'}
continue
fn = trace['normal'][mask, 0, 2]
gap = trace['gap'][mask, 0]
foot = trace['foot'][mask, 0]
result[name] = {
'samples': int(mask.sum()),
'normal_N_min_mean_max': [float(fn.min()), float(fn.mean()), float(fn.max())],
'mean_load_W': float((fn / trace['weight'][mask, 0]).mean()),
'gap_m_min_max': [float(gap.min()), float(gap.max())],
'foot_net_xyz_m': (foot[-1] - foot[0]).tolist(),
'root_xy_error_max_m': None if error is None else float(error[mask, 0, :2].norm(dim=-1).max()),
}
return result
def evaluate_flow(trace, cfg, controller_kind, cycles):
api = cpu_protocol()
api.validate_contract(cfg)
protocol = api.FunctionalFlowProtocol(cfg, cycles=cycles, controller_kind=controller_kind)
out = {'format': cfg['format'], 'controller_kind': controller_kind, 'requested_cycles': cycles,
'trace_functional_passed': False, 'runtime_continuity_verified': False,
'accepted_skill': False, 'new_simulation_steps': 0, 'new_optimizer_steps': 0,
'gates': {}, 'cycles': [],
'limitation': 'Offline tensor evidence only. Full runtime pass additionally requires fresh '
'no-autoreset hooks, seam carry-state, budget and clean terminal evidence.'}
if 'time' not in trace or not trace['time'].numel():
out['error'] = 'No physics samples'
return out
required = {'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'}
if not required <= trace.keys():
out['missing_fields'] = sorted(required - trace.keys())
return out
n = len(trace['time'])
if any(not isinstance(v, torch.Tensor) or v.shape[:2] != (n, 1) for v in trace.values()):
out['error'] = 'Require single-trial [T,1,...] tensors'
return out
if not all(bool(torch.isfinite(v).all()) for v in trace.values()):
out['error'] = 'Nonfinite raw evidence; no passing prefix substituted'
return out
t = trace['time'][:, 0]
expected = torch.arange(1, cycles * 4500 + 1, dtype=t.dtype) * cfg['physics_dt_s']
out['gates']['global_clock_without_restart'] = n == len(expected) and bool(
torch.allclose(t.cpu(), expected, atol=1e-5, rtol=0))
out['observed_end_s'] = float(t[-1])
runtime_fields = ('failure_flags', 'would_fail', 'acquisition_failed', 'premature_separation')
out['runtime_failure_fields_missing'] = [k for k in runtime_fields if k not in trace]
out['gates']['no_recorded_runtime_failure'] = all(
not bool(trace[k].any()) for k in runtime_fields if k in trace)
out['first_recorded_failure'] = None
if 'failure_flags' in trace:
flags = trace['failure_flags'][:, 0]
if flags.shape != (n, len(FAILURES)):
out['error'] = 'Invalid failure flag schema'
return out
hits = flags.any(-1).nonzero().flatten()
if len(hits):
i = int(hits[0])
out['first_recorded_failure'] = {'time_s': float(t[i]), 'phase': PHASES[int(trace['phase'][i, 0])],
'causes': [k for k, hit in zip(FAILURES, flags[i]) if hit]}
out['controller_kind_is_metadata_only'] = True
out['recorded_max_effective_residual_rad'] = (None if 'effective_residual_rad' not in trace else
float(trace['effective_residual_rad'].abs().max()))
if controller_kind == 'analytic_reference':
out['gates']['no_nonzero_recorded_reference_residual'] = all(
not bool(trace[k].abs().gt(1e-6).any()) for k in
('requested_action', 'effective_residual_rad', 'residual_request_normalized') if k in trace)
if controller_kind == 'learned_policy' and {'requested_action', 'actor_mean'} <= trace.keys():
out['gates']['recorded_deterministic_mean_action'] = bool(torch.allclose(
trace['requested_action'], trace['actor_mean'].tanh(), atol=1e-6, rtol=0))
error = root_error(trace)
out['root_xy_error_max_m'] = None if error is None else float(error[..., :2].norm(dim=-1).max())
out['root_xy_30mm_exceeded_diagnostic_only'] = None if error is None else bool(
(error[..., :2].norm(dim=-1) > .03).any())
# No resampling, truncation-to-success, reanchoring or phase relabeling.
# Only after the GLOBAL clock is verified, derive local time from integer
# ticks: float32 (18 + 4.6) - 18 can otherwise misclassify an exact boundary.
legacy = {'format': 'skillfactory.contact-cycle-simulation-functional.v1',
'physics_dt_s': cfg['physics_dt_s'], 'phase_durations_s': cfg['phase_durations_s'],
'nominal_scenario_id': 0, 'gates': cfg['functional_gates']}
for c in range(cycles):
start, end = c * 4500, min((c + 1) * 4500, n)
if start >= n:
out['cycles'].append({'cycle_index': c, 'status': 'not_evaluated'})
continue
local = {k: v[start:end].clone() for k, v in trace.items()}
local['time'] -= c * 9.0
if out['gates']['global_clock_without_restart']:
local['time'] = (torch.arange(1, end - start + 1, dtype=t.dtype,
device=t.device) * cfg['physics_dt_s'])[:, None]
audit = evaluate_cycle(local, legacy, controller_kind)
out['cycles'].append({'cycle_index': c, 'audit': audit, 'phases': phase_summary(local)})
out['trace_functional_passed'] = bool(all(out['gates'].values()) and all(
row.get('audit', {}).get('sim_functional_passed', False) for row in out['cycles']))
return out
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--trace', type=Path, required=True)
parser.add_argument('--config', type=Path, default=DEFAULT_CONFIG)
parser.add_argument('--controller-kind', choices=('analytic_reference', 'learned_policy'), required=True)
parser.add_argument('--cycles', type=int, choices=(1, 3), required=True)
parser.add_argument('--output', type=Path, required=True)
args = parser.parse_args()
if args.output.exists():
raise FileExistsError('Never overwrite an earlier audit')
cfg = cpu_protocol().load_contract(args.config)
raw = torch.load(args.trace, map_location='cpu', weights_only=True)
traces = ({str(e): {k: v[:, e:e+1] for k, v in raw.items()}
for e in range(raw['time'].shape[1])} if 'time' in raw else raw)
result = {'config_sha256': hashlib.sha256(args.config.read_bytes()).hexdigest(),
'audit_sha256': hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
'trace': str(args.trace.resolve()),
'trace_sha256': hashlib.sha256(args.trace.read_bytes()).hexdigest(),
'rows': {e: evaluate_flow(d, cfg, args.controller_kind, args.cycles) for e, d in traces.items()},
'new_simulation_steps': 0, 'new_optimizer_steps': 0, 'runtime_continuity_verified': False}
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open('x') as stream:
json.dump(result, stream, indent=2, allow_nan=False)
stream.write('\n')
print(json.dumps({'output': str(args.output), 'rows': len(result['rows']),
'trace_functional_passed': sum(r['trace_functional_passed'] for r in result['rows'].values()),
'runtime_continuity_verified': False}))
return 0 if all(r['trace_functional_passed'] for r in result['rows'].values()) else 2
if __name__ == '__main__':
raise SystemExit(main())
+723
View File
@@ -0,0 +1,723 @@
#!/usr/bin/env python3
"""Separately approved functional campaigns; optional frozen --followup-v3.
Default: PREP + reference/u256 1/3 cycles. Followup: PREP + u2563 + six paired
three-cycle groups. No training/retry or old authority. --campaign is the sole
supervisor; workers require its inherited authenticated FD before AppLauncher.
"""
import argparse
import ctypes
import hashlib
import importlib
import json
import os
import signal
import socket
import subprocess
import sys
import time
import traceback
from dataclasses import asdict
from pathlib import Path
from types import SimpleNamespace
import functional_flow_launch_v2 as launch
ROOT, b = launch.ROOT, launch.b
def actual_runtime(payload):
"""Config-only setup shared by PREP and RUN. Never constructs an environment."""
from isaaclab.app import AppLauncher
native = Path(payload['native'])
if payload.get('skill_v4'):
import skill_launch_v4
gui_enabled = skill_launch_v4.gui_enabled(payload)
else:
gui_enabled = False
gui_args = (' --/app/window/title=Go2_FL_Contact_Cycle_0.4.1 --/app/window/width=1440'
' --/app/window/height=960 --/app/window/x=120 --/app/window/y=80'
' --/app/file/ignoreUnsavedOnExit=true') if gui_enabled else ''
app = AppLauncher(headless=not gui_enabled, visualizer='kit' if gui_enabled else 'none',
device='cuda:0', fast_shutdown=True,
kit_args=f'--/log/file={native / "kit.log"}' + gui_args).app
try:
# Source inventory learned in PREP is independently rehashed before RUN.
inventory = None if payload['mode'] == 'prepare' else launch.read(payload['inventory'], payload['inventory_sha256'])
for name in (() if inventory is None else inventory['modules']):
importlib.import_module(name)
task = 'SkillFactory.tasks.manager_based.contact_cycle_sim.'
leg = 'SkillFactory.legs.contact_cycle_sim.'
modules = {name: importlib.import_module(task + name) for name in (
'evaluation_cfg', 'evaluation_env', 'evaluation_mdp', 'evaluation_rng_scope',
'evaluation_instrumentation', 'functional_flow_recording_v2')}
modules.update({name: importlib.import_module(leg + name) for name in (
'functional_flow_v2', 'functional_flow_binding_v2', 'evaluation_binding', 'replay')})
if payload.get('followup_v3'):
for name in ('functional_followup_v3', 'functional_followup_binding_v3'):
modules[name] = importlib.import_module(leg + name)
importlib.import_module('functional_gpu_watch_v3')
if payload.get('skill_v4'):
for name in ('skill_v4', 'skill_mailbox_v4', 'skill_binding_v4',
'functional_followup_v3', 'functional_followup_binding_v3'):
modules[name] = importlib.import_module(leg + name)
modules['skill_recording_v4'] = importlib.import_module(task + 'skill_recording_v4')
modules['performance_v042'] = importlib.import_module(task + 'performance_v042')
modules['sensor_epoch_v042'] = importlib.import_module(task + 'sensor_epoch_v042')
modules['packed_recording_v042'] = importlib.import_module(task + 'packed_recording_v042')
modules['readonly_scope_v042'] = importlib.import_module(task + 'readonly_scope_v042')
modules['batched_kinematics_v042'] = importlib.import_module(task + 'batched_kinematics_v042')
modules['live_cadence_v042'] = importlib.import_module(task + 'live_cadence_v042')
if payload['spec']['gui']:
# PREP has no GUI experience, but must preload the exact finite
# presentation dependency before inventory/binding (no viewport
# construction or physics). RUN already enables this via Kit.
import omni.kit.app
manager = omni.kit.app.get_app().get_extension_manager()
for extension in ('omni.ui', 'omni.kit.viewport.utility', 'omni.replicator.core'):
if not manager.set_extension_enabled_immediate(extension, True):
raise RuntimeError('gui_extension_unavailable:' + extension)
modules['skill_gui_v041'] = importlib.import_module(task + 'skill_gui_v041')
if payload['spec']['gui']:
modules['skill_gui_v041'].prepare_native()
# GUI experience preloads Replicator. Match that exact module
# availability in headless PREP rather than relaxing its guard.
importlib.import_module(launch.owner.OPTIONAL_REPLICATOR)
for name in ('skill_launch_v4', 'functional_followup_launch_v3', 'functional_gpu_watch_v3',
'evaluate_cycle_functional_flow'):
importlib.import_module(name)
binding_module = modules['skill_binding_v4' if payload.get('skill_v4') else
'functional_followup_binding_v3' if payload.get('followup_v3') else 'functional_flow_binding_v2']
binder = modules['evaluation_binding']
for name in binder.REQUIRED_RUNTIME:
importlib.import_module(name)
instrument = modules['evaluation_instrumentation']
for name in instrument.BACKEND_SOURCE_INVENTORY:
importlib.import_module(name)
instrument.verify_backend_sources()
replicator = launch.owner.preimport_replicator()
if inventory is not None:
launch.owner.check_replicator_inventory(inventory)
import torch
torch.cuda.init()
torch.cuda.get_rng_state(torch.device('cuda:0'))
if torch.cuda.get_device_name(0) != 'NVIDIA GeForce RTX 5080':
raise ValueError('Require approved RTX5080')
cfg = modules['evaluation_cfg'].EvaluationContactCycleEnvCfg()
cfg.sim.device, cfg.seed = 'cuda:0', 93501
if payload.get('skill_v4') and payload['spec']['gui']:
# Same explicit presentation config/imports in PREP and RUN. Only
# RUN creates KitVisualizer/window; no physics config is changed.
from isaaclab_visualizers.kit import KitVisualizerCfg
importlib.import_module('isaaclab_visualizers.kit.kit_visualizer')
# Observed GUI constructor-only camera helper imports must be in
# PREP's exact provenance before construction, not waived afterward.
importlib.import_module('isaaclab_physx.renderers.kit_viewport_utils')
# Full GUI run 10: first render lazily loads this exact utility.
importlib.import_module('isaaclab_physx.renderers.isaac_rtx_renderer_utils')
cfg.sim.visualizer_cfgs = [KitVisualizerCfg()]
if payload.get('skill_v4'):
perf = modules['performance_v042']
perf.RUNTIME_MATH_QUALIFICATION = None
if perf.options(ROOT)['mode'] in ('combined', 'combined_display'):
nominal = torch.tensor([j['q_default'] for j in cfg.leg_bundle['spec']['joints']], device='cuda:0')
perf.RUNTIME_MATH_QUALIFICATION = modules['batched_kinematics_v042'].qualify(
cfg.leg_bundle, cfg.residual_parameters['reference'], nominal, 'cuda:0')
launch.owner.preforward_video_recorder(cfg, modules['evaluation_env'])
constructor_imports = launch.owner.preimport_constructor_modules(binder)
constructor_export, export_metadata = launch.owner.physx_constructor_export(binder, resolve=True)
config_imports = launch.owner.preimport_config_references(cfg, binder)
binder.normalize_config(cfg)
rng_scope = modules['evaluation_rng_scope']
rng_inventory = rng_scope.qualify_config(cfg)
rng_inventory.update(optional_replicator=replicator, constructor_imports=constructor_imports,
constructor_export=export_metadata, config_reference_imports=config_imports)
instrument.qualify_backend_rng(rng_scope.backend_rng(None))
return SimpleNamespace(app=app, modules=modules, cfg=cfg, torch=torch, binding_module=binding_module,
constructor_export=constructor_export, rng_inventory=rng_inventory)
except BaseException:
traceback.print_exc()
app.close(exit_code=1)
raise
def verify_runtime(runtime, expected, inventory):
binder = runtime.modules['evaluation_binding']
launch.owner.physx_constructor_export(binder, expected=runtime.constructor_export)
launch.owner.check_replicator_inventory(inventory)
return runtime.binding_module.verify(expected, ROOT, runtime.cfg, inventory)
def worker(payload):
root, output = Path(payload['root']), Path(payload['output'])
is_followup = payload.get('followup_v3', False)
is_skill = payload.get('skill_v4', False)
if is_skill:
import skill_launch_v4 as skill
if is_followup:
import functional_followup_launch_v3 as followup
guard = launch.Guard(root, payload['start'], payload['deadline'],
cap=payload.get('owned_cap', launch.CAP), stdout_slots=14 if is_followup else 5)
interrupted = {sig: signal.signal(sig, lambda *_: setattr(guard, 'interrupted', True))
for sig in (signal.SIGTERM, signal.SIGINT)}
runtime = env = hooks = recorder = gui = None
result = {'success': False, 'mode': payload['mode'], 'optimizer_steps': 0, 'accepted_skill': False,
'environment_constructed': False, 'physics_counts': None, 'runtime_continuity_verified': False}
expected = inventory = None
code = 1
try:
guard.check()
if payload['sources'] != (skill.sources() if is_skill else followup.sources() if is_followup else launch.sources()) or payload['launch_identity'] != launch.owner.launch_identity():
raise ValueError('identity_worker_source_or_launch_drift')
runtime = actual_runtime(payload)
mod = runtime.modules
if is_skill and payload['mode'] == 'run' and payload['spec']['gui']:
def live_view_guard():
guard.check()
mod['live_cadence_v042'].require_recorder_live(recorder)
gui = mod['skill_gui_v041'].LivePresentation(runtime.app, live_view_guard,
lambda value: guard.write(output / 'gui-status.json', value))
binder = runtime.binding_module
hook_type = mod['evaluation_env'].RuntimeHooks
if payload['mode'] == 'prepare':
# Protect config preparation against any accidental task physics call.
def no_physics(h):
raise RuntimeError('budget_prepare_forbids_sim_step')
hooks = hook_type.isaac(before_step=no_physics)
hooks.__enter__() # lifetime ends only after guarded close in finally
if hooks.active:
inventory = binder.propose_inventory(ROOT, runtime.cfg, launch.owner.proposed_native_layouts)
expected = binder.snapshot(ROOT, runtime.cfg, inventory)
guard.write(output / 'inventory.json', inventory)
guard.write(output / 'binding.json', expected)
verify_runtime(runtime, expected, inventory)
result.update(success=True, environment_constructed=False,
physics_counts=mod['functional_flow_recording_v2'].counters(hooks),
runtime_qualified=False, config_prepared=True,
binding_sha256=b.digest(output / 'binding.json'),
inventory_sha256=b.digest(output / 'inventory.json'))
if is_skill:
result['kit_runtime_metadata'] = mod['performance_v042'].runtime_metadata(runtime.app)
else:
expected = launch.read(payload['binding'], payload['binding_sha256'])
inventory = launch.read(payload['inventory'], payload['inventory_sha256'])
verify_runtime(runtime, expected, inventory)
cfg = mod['functional_flow_v2'].load_contract(ROOT / launch.CONFIG)
cycles, kind = payload['cycles'], payload['controller_kind']
if is_skill:
spec = skill.validate_spec(payload['spec'])
if cycles != spec['cycles'] or kind != mod['skill_v4'].CONTROLLERS[spec['controller']]:
raise ValueError('Authenticated v4 slot/spec mismatch')
session = mod['skill_v4'].SkillSession(spec['command'], cycles, spec['controller'], spec['queued'])
protocol = mod['skill_v4'].SkillProtocol(cfg, session)
result['skill_spec'] = spec
elif is_followup:
if cycles != 3:
raise ValueError('Followup requires exactly three continuous cycles')
p = mod['functional_followup_v3'].load_plan(ROOT / followup.PLAN)
protocol = mod['functional_followup_v3'].FollowupProtocol(cfg, p, payload['command_id'], kind)
result['command_id'] = payload['command_id']
else:
protocol = mod['functional_flow_v2'].FunctionalFlowProtocol(cfg, cycles=cycles, controller_kind=kind)
frozen, _ = mod['replay'].verify_bundle(ROOT)
policy = (mod['replay'].load_frozen_policy(ROOT / mod['replay'].CANDIDATE, frozen).to('cuda:0')
if kind == 'learned_policy' else None)
# Install budget/count/reset/pose guards BEFORE environment construction.
def before_physics(h):
guard.check()
if gui is not None and (gui.closed or not runtime.app.is_running()):
raise RuntimeError('gui_window_closed_before_completion')
if is_skill:
mod['live_cadence_v042'].require_recorder_live(recorder)
if (h.mode != 'rollout' and h.initialization_steps >= 250
or h.mode == 'rollout' and h.rollout_steps >= cycles * 4500
or h.completed_steps >= cycles * 4500 + 250):
raise RuntimeError('budget_physics_call_cap')
recorder = (mod['performance_v042'].recorder_class(ROOT)(
cfg, protocol, guard.check, Path(payload['mailbox']),
stop_at_tick=spec['stop_at_tick'], reject_at_tick=spec['reject_at_tick']) if is_skill else
mod['functional_flow_recording_v2'].FunctionalFlowRecorder(cfg, protocol, guard.check))
hooks = hook_type.isaac(before_step=before_physics)
hooks.__enter__() # also guard exception-path close
if hooks.active:
result['environment_construction_entered'] = True
guard.write(output / 'worker-progress.json', result)
env = mod['evaluation_env'].EvaluationContactCycleEnv(
cfg=runtime.cfg, protocol=protocol, trial_index=0, runtime_hooks=hooks,
evidence_callback=recorder, seam_callback=recorder.seam_callback,
**({'boundary_stop_callback': lambda: session.stop_requested} if is_skill else {}))
result['environment_constructed'] = True
s = mod['evaluation_mdp'].state(env)
if (s.robot.is_fixed_base or runtime.cfg.sim.physics.solver_type != 0
or runtime.cfg.sim.physics.enable_external_forces_every_iteration
or runtime.cfg.terminations.failed is None or env.physics_dt != .002
or env.step_dt != .01 or bool(s.scenarios.any() or s.stand_only.any())):
raise RuntimeError('identity_require_frozen_floating_root_nominal_failure_enabled_fixture')
result['fixture'] = {'floating_root': True, 'num_envs': env.num_envs,
'system_weight_N': float(s.weight[0]), 'physics_dt_s': env.physics_dt,
'control_dt_s': env.step_dt, 'guide_enabled': runtime.cfg.guide_enabled,
'auxiliary_enabled': runtime.cfg.auxiliary_enabled}
verify_runtime(runtime, expected, inventory) # constructor cannot silently mutate cfg/imports
if is_skill:
result['performance_installation'] = mod['performance_v042'].install_readonly_path(ROOT, env)
recorder.attach(env)
obs, _ = env.reset()
if is_followup:
pair = followup.initial_pair_record(recorder, s, obs['policy'], payload['command_id'])
guard.write(output / 'initial-pair.json', pair)
result['initial_pair_sha256'] = b.digest(output / 'initial-pair.json')
result['initial_pair_matched'] = False
if payload.get('pair_reference') is not None:
expected_pair = launch.read(payload['pair_reference'], payload['pair_reference_sha256'])
result['initial_pair_matched'] = followup.require_pair(pair, expected_pair)
stop = env.stop_result
if gui is not None:
gui.start(env)
if is_skill and mod['performance_v042'].options(ROOT)['mode'] == 'combined_display':
mod['live_cadence_v042'].attach(recorder, env, gui)
if gui.closed:
stop = env._stop(('gui_window_closed_before_completion',))
controls = 0
profiler = mod['performance_v042'].LoopProfile(ROOT, env) if is_skill else None
if profiler is not None:
profiler.start()
with runtime.torch.inference_mode():
while stop is None:
guard.check()
x = obs['policy']
action, mean, std = mod['replay'].inference_action(
x, 'learned' if policy is not None else 'reference', policy)
recorder.before_control(x, action, mean, std)
stepped = env.step(action)
if is_skill:
# Includes the original interval20 render at the
# final substep, even on the final protocol tick.
mod['live_cadence_v042'].require_recorder_live(recorder)
controls += 1
stop = stepped.stop
obs = stepped.observation
if gui is not None:
gui.after_control(env)
if stop is None and (gui.closed or not runtime.app.is_running()):
stop = env._stop(('gui_window_closed_before_completion',))
if controls % 100 == 0 or stop is not None:
guard.write(output / 'physics-counts.json', {
**mod['functional_flow_recording_v2'].counters(hooks),
'physics_samples': len(recorder.frames), 'controls_entered': controls,
'stop': None if stop is None else asdict(stop)})
if stepped.reward is not None and not bool(runtime.torch.isfinite(stepped.reward).all()):
raise RuntimeError('nonfinite_reward')
if profiler is not None:
guard.write(output / 'performance-profile.json', profiler.finish(controls, len(recorder.frames)))
continuity = recorder.continuity(stop, hooks, cycles)
if is_skill:
result.update(live_receipts=session.receipts, pair_commands=session.pair_commands,
cycles_completed=stop.completed_clock_cycles)
recorder.status(env._event('tick', stop.causes), s, state='stopped', stop=stop)
result.update(controller_kind=kind, cycles_requested=cycles, control_calls=controls,
runtime_stop=asdict(stop), cycle_results=recorder.cycles,
continuity=continuity, runtime_continuity_verified=continuity['runtime_continuity_verified'],
success=continuity['runtime_continuity_verified'],
physics_counts=mod['functional_flow_recording_v2'].counters(hooks))
if gui is not None:
# Keep the physical scene alive, not a screenshot/trajectory
# replay. No env.step after completion; close is intercepted
# until evidence can be finalized by this same owner.
if result['success']:
recorder.status(env._event('tick', stop.causes), s, state='viewing_final', stop=stop)
gui.hold(env, result['success'] and stop.reason == 'protocol_complete',
safely_stopped=result['success'] and stop.reason == 'requested_stop')
if is_skill:
mod['live_cadence_v042'].require_recorder_live(recorder)
result['gui'] = gui.report()
# Close remains guarded; no reset/pose writes or extra physical steps.
env.close()
env = None
verify_runtime(runtime, expected, inventory)
result['preclose_identity_verified'] = True
result['runtime'] = {'torch': str(runtime.torch.__version__), 'device': 'cuda:0',
'gpu': runtime.torch.cuda.get_device_name(0),
'rng_inventory': runtime.rng_inventory}
code = 0 if result['success'] else 2
except BaseException as error:
result.update(success=False, error=traceback.format_exc())
if hasattr(error, 'binding_diagnostic'):
result['binding_diagnostic'] = error.binding_diagnostic
traceback.print_exc()
code = 1
finally:
if env is not None:
try:
env.close()
except BaseException:
result['close_error'] = traceback.format_exc()
result['success'], code = False, 1
if hooks is not None:
hooks.__exit__(None, None, None)
result['physics_counts'] = {k: getattr(hooks, k) for k in (
'attempted_steps', 'completed_steps', 'initialization_steps', 'rollout_steps',
'reset_calls', 'pose_write_calls', 'composer_buffer_replacements')}
result['hook_violations'] = hooks.violations
try:
if is_skill:
status_path = Path(payload['mailbox']) / 'status.json'
status = skill.mailbox().read_json(status_path)
if not result['success']:
status.update(state='failed', failure=[result.get('error', 'runtime_or_integrity_failure')[-4096:]])
status['worker_success'] = result['success']
skill.mailbox().publish(status_path, status)
if recorder is not None:
guard.tensor(output / 'initial-state.pt', recorder.initial, cleanup=True)
guard.tensor(output / 'trace.pt', recorder.trace(), cleanup=True)
guard.write(output / 'events-and-seams.json', {'events': recorder.events,
'seams': recorder.seams, 'pending_seam': recorder.pending_seam,
'recording_faults': recorder.faults,
**({'live_receipts': recorder.session.receipts, 'pair_commands': recorder.session.pair_commands}
if is_skill else {})}, cleanup=True)
if gui is not None:
result['gui'] = gui.report()
result['worker_final_boottime'] = b.boottime()
if is_skill:
result['shutdown_handoff_ready'] = True
guard.write(output / 'worker-result.json', result, cleanup=True)
if is_skill:
# Keep this owned GPU process alive until the supervisor has
# made its last strict LIVE ownership observation. Only then
# may app.close/exit race with process discovery in CLEANUP.
skill.wait_shutdown_release(output, guard)
except BaseException:
traceback.print_exc()
code = 1
for sig, handler in interrupted.items():
signal.signal(sig, handler)
if gui is not None:
gui.release()
if runtime is not None:
runtime.app.close(exit_code=code)
return code
def release_shutdown(output, guard, gpu, owned):
"""Explicit LIVE -> bounded CLEANUP transition, never an NVML tolerance.
Worker waits alive after all guarded physics/env close/result publication.
The original strict live check must PASS before its exit is released. Strict
post-wait final GPU check remains mandatory. No unknown client is waived.
"""
path = output / 'worker-result.json'
if not path.exists():
return None
receipt = launch.read(path)
if receipt.get('shutdown_handoff_ready') is not True:
raise RuntimeError('process_missing_shutdown_handoff')
observation = gpu.check(owned)
digest = b.digest(path)
guard.write(output / 'shutdown-release.json', {'worker_result_sha256': digest})
return {'released': True, 'worker_result_sha256': digest, 'last_live_gpu': observation,
'scope': 'No physics after receipt; bounded owned cleanup then unchanged strict final GPU check'}
def filesystem_verify(binding):
# Reuse finite binding file records, not a new recursive filesystem traversal.
files = {}
def visit(x):
if isinstance(x, dict):
if isinstance(x.get('path'), str) and isinstance(x.get('sha256'), str):
files[x['path']] = x['sha256']
for item in x.values():
visit(item)
elif isinstance(x, list):
for item in x:
visit(item)
visit(binding)
files.update(binding['runtime_layout_pins'])
for filename, digest in files.items():
if b.digest(Path(filename)) != digest:
raise ValueError('identity_postexit_file_changed: ' + filename)
return {'files_rehashed': len(files), 'scope': 'static finite files; preclose same-object cfg checked by worker'}
def campaign(args):
root = args.output
is_followup = getattr(args, 'followup_v3', False)
is_skill = getattr(args, 'skill_v4', False)
if is_skill:
import skill_launch_v4 as skill
approved = skill.approval(args.approval, args.trusted_approval_sha256, root)
elif is_followup:
import functional_followup_launch_v3 as followup
approved = followup.approval(args.approval, args.trusted_approval_sha256, root)
else:
approved = launch.approval(args.approval, args.trusted_approval_sha256, root)
limits = approved['limits']
slot_rows = (skill.slots(approved['spec']) if is_skill else followup.api().slots(followup.plan()) if is_followup else
[{'slot': name, 'cycles': cycles, 'controller_kind': kind} for name, cycles, kind in launch.SLOTS])
reserved_calls = limits['max_physics_calls']
if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0:
raise RuntimeError('Subreaper unavailable')
root.mkdir()
start = b.boottime()
deadline = min(start + limits['max_campaign_seconds'], approved['deadline_boottime']) if is_skill else start + limits['max_campaign_seconds']
campaign_guard = launch.Guard(root, start, deadline, cleanup_reserve=0,
cap=limits['max_owned_bytes'], stdout_slots=len(slot_rows))
campaign_guard.write(root / 'approval.json', approved)
if is_skill:
mailbox_path = root / 'mailbox'
mailbox_path.mkdir()
skill.mailbox().publish(mailbox_path / 'status.json', {'state': 'starting', 'global_tick': 0,
'failure': [], 'receipts_count': 0})
campaign_guard.write(root / 'reservation.json', {
'approved_by': 'current user 批准', 'start_boottime': start, 'deadline_boottime': deadline,
'limits': limits, 'reserved_physics_calls': reserved_calls, 'actual_final_calls': None,
'note': 'Reserved ceiling is not actual count; no retries/refunds/old grants.'})
policy = launch.native_policy(root, deadline, cap=limits['max_owned_bytes'], slot_names=[s['slot'] for s in slot_rows])
campaign_guard.write(root / 'native-policy.json', policy)
monitor = launch.AsyncMonitor(policy)
gpu = None
results = []
final = {'success': False, 'rows': results, 'optimizer_steps': 0, 'accepted_skill': False}
handlers = {sig: signal.signal(sig, lambda *_: setattr(campaign_guard, 'interrupted', True))
for sig in (signal.SIGTERM, signal.SIGINT)}
binding = inventory = None
try:
# Wait for one finite baseline scan, without consuming a Kit launch.
while monitor.monitor is None:
campaign_guard.check(); monitor.check(); time.sleep(.05)
if is_followup or is_skill:
from functional_gpu_watch_v3 import ObservedGPUOwnership
gpu = ObservedGPUOwnership(approved['gpu_identity'],
lambda event: campaign_guard.write(root / 'gpu-observations.json', event, cleanup=True))
else:
gpu = launch.ownership.GPUOwnership(approved['gpu_identity'])
for index, slot in enumerate(slot_rows):
name, cycles, kind = slot['slot'], slot['cycles'], slot['controller_kind']
campaign_guard.check(); monitor.check()
if is_skill:
args.skill_reservation.check_storage(args.skill_reservation.runs)
if is_followup and index >= 2 and not results[1].get('success'):
raise RuntimeError('New supervised u256 nominal three-cycle required before matrix')
if not is_followup and not is_skill and index >= 3 and not all(x.get('success') for x in results[1:3]):
raise RuntimeError('Both single cycles required before continuous runs')
output = root / name
output.mkdir()
native = root / 'native' / name
native.mkdir(parents=True)
environment = os.environ.copy()
for var in ('VIRTUAL_ENV', 'CONDA_PREFIX', 'PYTHONEXE', 'PYTHONHOME', 'args'):
environment.pop(var, None)
redirects = launch.owner.native_redirects(native, preparation=True)
for path in redirects.values():
Path(path).mkdir()
environment.update(redirects)
environment.update(PYTHONPATH=str(ROOT / 'source/SkillFactory') + ':' + str(ROOT / 'scripts'),
PYTHONUNBUFFERED='1', PYTEST_DISABLE_PLUGIN_AUTOLOAD='1',
CUDA_VISIBLE_DEVICES='0', HEADLESS='0' if is_skill and approved['spec']['gui'] and index else '1',
LIVESTREAM='0')
if is_skill:
# Frozen import-time asset defaults predate relocatable packaging.
# Select this tree without changing their immutable source bytes.
environment.update(SKILLFACTORY_GO2_ASSET_DIR=str(ROOT / 'assets/generated/go2_fl'),
SKILLFACTORY_LEG_BUNDLE=str(ROOT / 'assets/generated/legs/go2_fl'))
slot_start = b.boottime()
seconds = (limits['max_prepare_seconds' if index == 0 else 'max_physical_seconds']
if is_followup or is_skill else limits['max_launch_seconds'])
slot_deadline = min(slot_start + seconds, deadline)
guard = launch.Guard(root, slot_start, slot_deadline, cap=limits['max_owned_bytes'], stdout_slots=len(slot_rows))
guard.check()
payload = {'mode': 'prepare' if index == 0 else 'run', 'slot': name, 'root': str(root),
'output': str(output), 'native': str(native), 'start': slot_start,
'deadline': slot_deadline, 'cycles': cycles, 'controller_kind': kind,
'sources': approved['sources'], 'launch_identity': launch.owner.launch_identity(),
'followup_v3': is_followup, 'owned_cap': limits['max_owned_bytes']}
if is_skill:
payload.update(skill_v4=True, spec=approved['spec'], mailbox=str(mailbox_path))
if is_followup:
payload.update(command_id=slot['command_id'], pair_reference=None)
if slot['pair_group'] is not None and kind == 'learned_policy':
reference = results[-1]
if not reference['success'] or reference['slot'] != slot['pair_group'] + '-reference':
raise RuntimeError('Complete matching reference required before paired u256')
path = root / reference['slot'] / 'initial-pair.json'
payload.update(pair_reference=str(path), pair_reference_sha256=b.digest(path))
if index:
payload.update(binding=str(root / 'prepare/binding.json'), binding_sha256=b.digest(root / 'prepare/binding.json'),
inventory=str(root / 'prepare/inventory.json'), inventory_sha256=b.digest(root / 'prepare/inventory.json'))
owned = launch.owner.OwnedProcesses()
gpu.check(owned, final=True)
parent, child_channel = socket.socketpair()
command = launch.owner.launch_command([str(ROOT / (skill.SCRIPT if is_skill else launch.SCRIPT)), '--worker-fd', str(child_channel.fileno())], environment)
# Persist the consumed slot BEFORE any child/AppLauncher starts.
campaign_guard.write(output / 'reservation.json', {'index': index, 'payload': payload,
'command': command, 'physics_ceiling': 0 if index == 0 else cycles * 4500 + 250})
child = capture = None
shutdown_handoff = None
clean = False
row = {'slot': name, 'success': False, 'status': 'reserved', 'pair_group': slot.get('pair_group')}
results.append(row)
try:
child = subprocess.Popen(command, cwd=ROOT, env=environment, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, start_new_session=True,
pass_fds=(child_channel.fileno(),))
child_channel.close()
owned.attach(child)
capture = launch.owner.StdoutCapture(child.stdout, output / 'stdout.log', b)
launch.authenticate(parent, child, payload, capture, guard, monitor, owned, gpu)
parent.close()
while child.poll() is None:
campaign_guard.check(); guard.check(); monitor.check(); capture.check()
if is_skill and shutdown_handoff is None:
shutdown_handoff = release_shutdown(output, guard, gpu, owned)
if shutdown_handoff is None:
gpu.check(owned)
if is_skill and b.boottime() >= getattr(args, '_next_storage_check', 0):
args._next_storage_check = b.boottime() + 1
args.skill_reservation.check_storage(args.skill_reservation.runs)
time.sleep(.1)
capture.check()
except BaseException:
row['supervisor_error'] = traceback.format_exc()
finally:
parent.close(); child_channel.close()
if child is not None:
clean = launch.owner.cleanup_group(child, slot_deadline, b, owned,
capture.pump if capture is not None else lambda: None)
child.wait(timeout=max(.01, slot_deadline - b.boottime()))
row.update(returncode=child.returncode, cleanup_clean=clean, waited_exit=True,
owned_identities=sorted([list(x) for x in owned.known]))
if capture is not None:
capture.close()
try:
row['gpu_postexit'] = gpu.check(owned, final=True)
except BaseException:
row['gpu_postexit_error'] = traceback.format_exc()
row.setdefault('supervisor_error', row['gpu_postexit_error'])
row['end_boottime'] = b.boottime()
worker_path = output / 'worker-result.json'
if worker_path.exists():
receipt = launch.read(worker_path)
row['worker_result_sha256'] = b.digest(worker_path)
row['worker'] = receipt
row['success'] = bool(receipt.get('success') and receipt.get('preclose_identity_verified')
and row.get('returncode') == 0 and clean and not row.get('supervisor_error'))
if is_skill and index and not skill.complete_receipt(receipt, approved['spec']):
row['success'] = False
row['incomplete_worker_receipt'] = True
if is_followup and not followup.complete_receipt(receipt, slot):
row['success'] = False
row['incomplete_worker_receipt'] = True
if is_skill:
row['shutdown_handoff'] = shutdown_handoff
if (shutdown_handoff is None or not shutdown_handoff['released']
or shutdown_handoff['worker_result_sha256'] != row.get('worker_result_sha256')):
row['success'] = False
row['shutdown_handoff_missing_or_changed'] = True
if index == 0 and row['success']:
binding = launch.read(root / 'prepare/binding.json')
inventory = launch.read(root / 'prepare/inventory.json')
expected_format = ('skillfactory.go2-fl-skill-binding.v4' if is_skill else
'skillfactory.functional-followup-binding.v3' if is_followup
else 'skillfactory.contact-cycle-functional-binding.v2')
if (binding.get('format') != expected_format
or is_followup and binding.get('plan_sha256') != approved['sources'][followup.PLAN]
or binding.get('contract_sha256') != approved['sources'][launch.CONFIG]
or row['worker']['environment_constructed'] is not False
or row['worker']['physics_counts']['completed_steps'] != 0):
raise RuntimeError('Config-only parent review failed')
row['parent_review'] = filesystem_verify(binding)
# Preserve the exact prepared sources, not authority copied from old runs.
for relative in approved['sources']:
dest = root / 'source_snapshot' / relative
dest.parent.mkdir(parents=True, exist_ok=True)
b.durable_write(dest, (ROOT / relative).read_bytes())
campaign_guard.write(root / 'preparation-reviewed.json', {
'binding_sha256': b.digest(root / 'prepare/binding.json'),
'inventory_sha256': b.digest(root / 'prepare/inventory.json'),
'approved_from_current_user_budget_only': True, 'config_only_not_physical_qualification': True,
'static_verification': row['parent_review']})
elif index and row['success']:
row['postexit_files'] = filesystem_verify(binding)
if is_followup and row['success'] and slot['pair_group'] is not None and kind == 'learned_policy':
row['paired_comparison'] = followup.paired_comparison(results[-2], row)
if not row['paired_comparison']['eligible']:
row['success'] = False
if (is_followup or is_skill) and index:
row['artifact_sha256'] = {}
for filename in (('trace.pt', 'initial-state.pt', 'events-and-seams.json') if is_skill else
('trace.pt', 'initial-state.pt', 'events-and-seams.json', 'initial-pair.json')):
path = output / filename
if not path.is_file():
row['success'] = False
row.setdefault('missing_required_results', []).append(filename)
else:
row['artifact_sha256'][filename] = b.digest(path)
if is_followup and row['artifact_sha256'].get('initial-pair.json') != row.get('worker', {}).get('initial_pair_sha256'):
row['success'] = False
row['initial_pair_file_identity_mismatch'] = True
campaign_guard.write(output / 'supervisor-result.json', row)
campaign_guard.write(root / 'campaign-progress.json', final)
if not row['success']:
break # first failure consumes this campaign; never retry or tune
monitor.check()
final['success'] = len(results) == len(slot_rows) and all(x['success'] for x in results)
except BaseException:
final['error'] = traceback.format_exc()
final['success'] = False
if results:
results[-1]['success'] = False
results[-1].setdefault('supervisor_error', final['error'])
traceback.print_exc()
finally:
monitor.stop.set()
if monitor.error is not None:
final['success'] = False
final['native_monitor'] = monitor.report()
final['end_boottime'] = b.boottime()
counts = [r.get('worker', {}).get('physics_counts') for r in results]
final['actual_final_physics_calls'] = (sum(x['completed_steps'] for x in counts)
if all(x is not None for x in counts) else None)
final['reserved_physics_calls'] = reserved_calls
if final['actual_final_physics_calls'] is None or final['actual_final_physics_calls'] > reserved_calls:
final['success'] = False
final['remaining_slots'] = [s['slot'] for s in slot_rows[len(results):]]
try:
campaign_guard.write(root / 'campaign-result.json', final, cleanup=True)
if is_skill:
status = skill.mailbox().read_json(mailbox_path / 'status.json')
status.update(state='stopped' if final['success'] else 'failed',
supervisor_success=final['success'], failure=status.get('failure', []) if final['success']
else status.get('failure') or [final.get('error', 'supervision_or_worker_failure')[-4096:]])
skill.mailbox().publish(mailbox_path / 'status.json', status)
finally:
for sig, handler in handlers.items():
signal.signal(sig, handler)
return 0 if final['success'] else 2
def main():
parser = argparse.ArgumentParser(description=__doc__)
mode = parser.add_mutually_exclusive_group(required=True)
mode.add_argument('--campaign', action='store_true')
mode.add_argument('--worker-fd', type=int)
mode.add_argument('--check-followup-v3', action='store_true', help='CPU metadata only; no grant, Kit or physics')
parser.add_argument('--followup-v3', action='store_true', help='fresh u2563 then six paired command groups; new grant only')
parser.add_argument('--output', type=Path)
parser.add_argument('--approval', type=Path)
parser.add_argument('--trusted-approval-sha256')
args = parser.parse_args()
if args.check_followup_v3:
import functional_followup_launch_v3 as followup
p = followup.plan()
print(json.dumps({'execution_authorized': False, 'new_physics_calls': 0, 'optimizer_steps': 0,
'plan_sha256': b.digest(ROOT / followup.PLAN), 'plan': p,
'commands_m': followup.api().commands(p), 'slots': followup.api().slots(p),
'sources': followup.sources()}, indent=2))
return 0
if args.worker_fd is not None:
return worker(launch.worker_payload(args.worker_fd))
if args.output is None or args.approval is None or args.trusted_approval_sha256 is None:
parser.error('Require fresh independently trusted approval and new output root')
return campaign(args)
if __name__ == '__main__':
raise SystemExit(main())
+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/python3.12
"""Real supervised simulation through the public API; no Kit import in caller.
Run from a same-layout independent skill package. Creates a fresh bounded session
and external run directory. Commands are queued live, not a fixed shell replay.
"""
import json
from pathlib import Path
import sys
import tempfile
import time
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
from scripts.go2_fl_skill import start
from scripts.skill_launch_v4 import new_registration
def main():
output = Path(tempfile.mkdtemp(prefix='go2-fl-python-'))
registration = output / 'session/registration.json'
new_registration(registration) # Explicit new caller authorization, no GPU/Kit yet.
run = start(output / 'run', registration, command='x_bias-16mm', cycles=6)
print('Real simulation and evidence:', output, flush=True)
deadline = time.monotonic() + 1500
def wait_status(predicate):
while time.monotonic() < deadline:
status = run.query()
if status['state'] == 'failed':
raise RuntimeError(status)
if predicate(status):
return status
if run.process.poll() is not None:
raise RuntimeError('Supervisor ended before requested status')
time.sleep(.25)
raise TimeoutError('Caller status deadline; supervisor retains its own bounded cleanup')
wait_status(lambda status: status['state'] == 'running')
for command in ('baseline_direction-24mm', 'y_bias-16mm'):
request_id = run.queue(command)
status = wait_status(lambda value: (value.get('last_receipt') or {}).get('request', {}).get('id') == request_id)
assert status['last_receipt']['accepted']
try:
run.queue('INVALID')
except ValueError:
print('Invalid command rejected before publication', flush=True)
wait_status(lambda status: status.get('global_tick', 0) >= 23000)
run.request_stop() # Finish current sixth cycle; not arbitrary-phase hard stop.
code = run.wait()
print(json.dumps(run.query(), indent=2))
return code
if __name__ == '__main__':
raise SystemExit(main())
+209
View File
@@ -0,0 +1,209 @@
"""Small functional-only supervisor support. Imports never start Kit.
Reuses reviewed launch/ownership/native primitives, not their old admission,
training, ledger, grant or retry paths. One campaign, five fixed slots, no retry.
"""
import ctypes
import hashlib
import io
import json
import os
import signal
import socket
import struct
import sys
import threading
import time
from pathlib import Path
import evaluate_cycle_sim_residual as owner
import rootxy_launch_v2 as ownership
ROOT = owner.ROOT
SCRIPT = 'scripts/evaluate_cycle_functional_flow.py'
CONFIG = 'configs/contact_cycle/functional_flow_v2.json'
FORMAT = 'skillfactory.functional-flow-parent-approval.v2'
CAP = 1 << 30
HEADROOM = 16 << 20
SLOTS = (('prepare', 0, None), ('reference-1', 1, 'analytic_reference'),
('u256-1', 1, 'learned_policy'), ('reference-3', 3, 'analytic_reference'),
('u256-3', 3, 'learned_policy'))
b = owner.cpu_module('evaluation_budget')
SOURCE_PATHS = (
SCRIPT, 'scripts/functional_flow_launch_v2.py', 'scripts/evaluate_cycle_sim_residual.py',
'scripts/rootxy_launch_v2.py', 'scripts/audit_contact_cycle_functional.py', CONFIG,
*('source/SkillFactory/SkillFactory/' + p for p in (
'legs/contact_cycle_sim/functional_flow_v2.py',
'legs/contact_cycle_sim/functional_flow_binding_v2.py',
'tasks/manager_based/contact_cycle_sim/functional_flow_recording_v2.py',
'tasks/manager_based/contact_cycle_sim/evaluation_env.py',
'tasks/manager_based/contact_cycle_sim/evaluation_cfg.py',
'tasks/manager_based/contact_cycle_sim/evaluation_mdp.py',
'tasks/manager_based/contact_cycle_sim/recording.py',
'tasks/manager_based/contact_cycle/recording.py',
'legs/contact_cycle_sim/evaluation_binding.py',
'legs/contact_cycle_sim/evaluation_budget.py')),
)
def sources():
return {p: b.digest(ROOT / p) for p in SOURCE_PATHS}
def read(path, digest=None):
path = Path(path)
if (not path.is_file() or path.is_symlink() or path != path.resolve()
or path.stat().st_size > 32 << 20):
raise ValueError('Unsafe input: ' + str(path))
raw = path.read_bytes()
if digest is not None and hashlib.sha256(raw).hexdigest() != digest:
raise ValueError('Trusted digest mismatch: ' + str(path))
return json.loads(raw)
def approval(path, digest, root):
grant = read(path, digest)
expected_limits = {'max_prepare_launches': 1, 'max_physical_launches': 4,
'max_physics_calls': 37000, 'max_rollout_calls': 36000,
'max_initialization_calls_per_launch': 250,
'max_launch_seconds': 600, 'cleanup_seconds': 120,
'max_campaign_seconds': 3000, 'max_owned_bytes': CAP,
'num_envs': 1, 'optimizer_steps': 0, 'retry_or_resume': False}
if (grant.get('format') != FORMAT or grant.get('artifact_root') != str(root)
or grant.get('limits') != expected_limits or grant.get('sources') != sources()
or grant.get('boot_id') != b.boot_id() or grant.get('user_approval') != '批准'
or not isinstance(grant.get('authority'), str) or not grant['authority'].strip()
or not b.boottime() < grant.get('admit_before_boottime', 0)
or grant.get('slot_order') != [s[0] for s in SLOTS]):
raise PermissionError('Fresh exact functional approval required')
root = Path(root)
if (root != root.resolve() or not root.is_absolute() or root.is_relative_to(ROOT)
or root.exists()):
raise ValueError('New canonical isolated external campaign root required; no retry')
return grant
class Guard:
def __init__(self, root, start, deadline, *, cleanup_reserve=120, cap=CAP, stdout_slots=5):
self.root, self.start, self.deadline = Path(root), start, deadline
self.cap, self.stdout_slots = cap, stdout_slots
self.cleanup_reserve, self.boot = cleanup_reserve, b.boot_id()
self.interrupted = False
self.next_storage_check = 0
def check(self, pending=0, *, cleanup=False):
now = b.boottime()
if (b.boot_id() != self.boot or not self.start <= now < self.deadline - (0 if cleanup else self.cleanup_reserve)
or self.interrupted and not cleanup):
raise RuntimeError('budget_interrupt_deadline_or_boot')
# Every owned publication supplies pending bytes and is admitted here.
# Native writers are sampled separately; avoid rescanning their cache
# tree on all 500 physics ticks per second.
if pending or cleanup or now >= self.next_storage_check:
self.next_storage_check = now + 1
if b.artifact_size(self.root) + pending > self.cap - (0 if cleanup else HEADROOM + self.stdout_slots * b.STDOUT_BYTES):
raise RuntimeError('storage_campaign_owned_cap')
def write(self, path, value, *, cleanup=False):
data = b.encode(value)
path = Path(path)
if not path.is_relative_to(self.root) or path.parent != path.parent.resolve() or path.is_symlink():
raise ValueError('Unsafe output')
self.check(len(data), cleanup=cleanup)
temporary = path.with_name(path.name + '.pending')
b.durable_write(temporary, data)
os.replace(temporary, path)
b.sync_directory(path.parent)
def tensor(self, path, value, *, cleanup=False):
import torch
buffer = io.BytesIO()
torch.save(value, buffer)
data = buffer.getvalue()
self.check(len(data), cleanup=cleanup)
b.durable_write(Path(path), data)
def native_policy(root, deadline, *, cap=CAP, slot_names=None):
names = [slot for slot, _, _ in SLOTS] if slot_names is None else slot_names
paths = owner.free_space_paths(root)
return {'artifact_root': str(root), 'limit_bytes': cap,
'native_paths': owner.native_paths(root), 'shared_native_paths': owner.shared_native_paths(),
'task_owned_cache_paths': sorted(str(root / 'native' / slot / name)
for slot in names for name in ('home', 'cache', 'tmp', 'config', 'data')),
'free_space_paths': paths, 'free_space_mount_identities': {p: owner.mount_identity(Path(p)) for p in paths},
'mount_identity': owner.mount_identity(root), 'check_interval_seconds': 1,
'max_native_growth_bytes': cap, 'min_free_bytes': 2 * cap,
'not_after_boottime': deadline, 'native_hard_cap': False, 'coverage_gaps': list(owner.NATIVE_GAPS)}
class AsyncMonitor:
"""Keep native scans off the sole deadline/cleanup thread; no new scan policy."""
def __init__(self, policy):
self.policy = policy
self.monitor = None
self.error = None
self.updated = b.boottime()
self.stop = threading.Event()
self.thread = threading.Thread(target=self._run, daemon=True)
self.thread.start()
def _run(self):
try:
self.monitor = owner.NativeMonitor(self.policy, b)
self.updated = b.boottime()
while not self.stop.wait(1):
self.monitor.check(force=True)
self.updated = b.boottime()
except BaseException as error:
self.error = error
def check(self):
if self.error is not None:
raise self.error
if b.boottime() - self.updated > 20:
raise RuntimeError('storage_native_monitor_stale')
def report(self):
return {'error': None if self.error is None else repr(self.error), 'last_completed_boottime': self.updated,
'report': None if self.monitor is None else self.monitor.report()}
def worker_payload(fd):
with socket.socket(fileno=fd) as channel:
channel.settimeout(20)
pid, uid, _ = struct.unpack('3i', channel.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12))
cmd = Path(f'/proc/{pid}/cmdline').read_bytes().split(b'\0')
if (Path(f'/proc/{pid}/exe').resolve() != Path('/usr/bin/python3.12') or uid != os.getuid()
or not owner.ancestor(pid, os.getpid()) or b'--campaign' not in cmd
or str(ROOT / SCRIPT).encode() not in cmd):
raise PermissionError('Authenticated functional supervisor FD required')
channel.sendall(str(os.getpid()).encode() + b'\n')
with channel.makefile('rb') as stream:
data = stream.read(1 << 20)
if len(data) >= 1 << 20:
raise ValueError('Oversized worker handoff')
return json.loads(data)
def authenticate(channel, child, payload, capture, guard, monitor, owned, gpu):
channel.setblocking(False)
hello = b''
until = min(b.boottime() + 30, guard.deadline - 120)
while b'\n' not in hello:
guard.check(); capture.check(); monitor.check(); gpu.check(owned)
if child.poll() is not None or b.boottime() >= until:
raise RuntimeError('process_worker_handshake_failed')
try:
part = channel.recv(64)
if not part:
raise RuntimeError('process_worker_handshake_eof')
hello += part
except BlockingIOError:
time.sleep(.05)
pid = int(hello.strip())
if pid not in owned.discover() or not owner.ancestor(os.getpid(), pid):
raise PermissionError('Worker is not an owned descendant')
channel.settimeout(10)
channel.sendall(b.encode(payload))
channel.shutdown(socket.SHUT_WR)
+116
View File
@@ -0,0 +1,116 @@
"""Fresh authorization and pairing for the single followup + six finite pairs.
Imports are CPU-only. No grant generator, simulator, retry, training or weakening
of GPUOwnership lives here. Old approvals cannot admit this plan.
"""
import importlib
import importlib.util
import json
import sys
from pathlib import Path
import functional_flow_launch_v2 as old
FORMAT = 'skillfactory.functional-followup-parent-approval.v3'
PLAN = 'configs/contact_cycle/functional_followup_v3.json'
EXTRA_SOURCES = (PLAN, 'scripts/functional_followup_launch_v3.py', 'scripts/functional_gpu_watch_v3.py',
'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/functional_followup_v3.py',
'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/functional_followup_binding_v3.py')
def api():
real = 'SkillFactory.legs.contact_cycle_sim.functional_followup_v3'
if real in sys.modules:
return sys.modules[real]
# Pure stdlib private namespace ONLY in the non-Kit parent/planner.
name = '_functional_followup_cpu'
directory = old.ROOT / 'source/SkillFactory/SkillFactory/legs/contact_cycle_sim'
if name not in sys.modules:
spec = importlib.util.spec_from_file_location(name, directory / '__init__.py',
submodule_search_locations=[str(directory)])
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return importlib.import_module(name + '.functional_followup_v3')
def plan():
value = api().load_plan(old.ROOT / PLAN)
if old.b.digest(old.ROOT / old.CONFIG) != value['base_contract_sha256']:
raise ValueError('Base functional contract drift')
return value
def sources():
return {p: old.b.digest(old.ROOT / p) for p in (*old.SOURCE_PATHS, *EXTRA_SOURCES,
'scripts/audit_cycle_functional_flow.py')}
def approval(path, digest, root):
grant = old.read(path, digest)
p = plan()
expected_order = [r['slot'] for r in api().slots(p)]
if (grant.get('format') != FORMAT or grant.get('artifact_root') != str(root)
or json.dumps(grant.get('limits'), sort_keys=True) != json.dumps(p['limits'], sort_keys=True)
or grant.get('sources') != sources()
or grant.get('plan_sha256') != old.b.digest(old.ROOT / PLAN)
or grant.get('boot_id') != old.b.boot_id() or grant.get('user_approval') != '批准'
or not isinstance(grant.get('authority'), str) or not grant['authority'].strip()
or not old.b.boottime() < grant.get('admit_before_boottime', 0)
or grant.get('slot_order') != expected_order):
raise PermissionError('Fresh exact followup approval required; no old grants')
root = Path(root)
if root != root.resolve() or not root.is_absolute() or root.is_relative_to(old.ROOT) or root.exists():
raise ValueError('New canonical external root required; never retry')
return grant
def initial_pair_record(recorder, s, observation, command_id):
from dataclasses import asdict
from SkillFactory.tasks.manager_based.contact_cycle_sim.functional_flow_recording_v2 import carry_snapshot, tensor_identity
return {'command_id': command_id, 'seed': s.trial.seed,
'command': asdict(s.protocol_command()), 'anchors': asdict(s.anchors),
'state': {k: tensor_identity(v) for k, v in recorder.initial.items()},
'carried': carry_snapshot(s), 'ik': tensor_identity(s.controller.q_ik),
'controller_action': tensor_identity(s.controller.action),
'controller_refreshes': tensor_identity(s.controller.refreshes),
'observation': tensor_identity(observation),
'scope': 'Enumerated initial state/RNG/IK/command, not complete native backend equality'}
def require_pair(actual, expected):
# No coercion/tolerance, reanchoring, state restoration or mismatch repair.
if json.dumps(actual, sort_keys=True, allow_nan=False) != json.dumps(expected, sort_keys=True, allow_nan=False):
raise RuntimeError('identity_pair_initial_state_mismatch_before_rollout')
return True
def complete_receipt(receipt, slot):
c = receipt.get('physics_counts') or {}
stop = receipt.get('runtime_stop') or {}
if slot['cycles'] == 0:
return (receipt.get('success') is True and receipt.get('environment_constructed') is False
and c.get('completed_steps') == c.get('attempted_steps') == 0)
return (receipt.get('success') is True and receipt.get('runtime_continuity_verified') is True
and receipt.get('preclose_identity_verified') is True
and receipt.get('controller_kind') == slot['controller_kind']
and receipt.get('command_id') == slot['command_id'] and receipt.get('cycles_requested') == 3
and c.get('rollout_steps') == 13500
and type(c.get('initialization_steps')) is int and 0 <= c['initialization_steps'] <= 250
and c.get('attempted_steps') == c.get('completed_steps') == 13500 + c['initialization_steps']
and stop.get('reason') == 'protocol_complete' and stop.get('causes') == []
and stop.get('clock', {}).get('global_tick') == 13500
and not receipt.get('hook_violations'))
def paired_comparison(reference, learned):
"""Result publication requires complete physics AND supervision on BOTH rows."""
if not reference.get('success') or not learned.get('success'):
return {'eligible': False, 'reason': 'both_supervised_rows_required'}
a, b = reference['worker'], learned['worker']
if not b.get('initial_pair_matched') or a.get('initial_pair_sha256') != b.get('initial_pair_sha256'):
return {'eligible': False, 'reason': 'exact_initial_pair_required'}
if a.get('command_id') != b.get('command_id'):
return {'eligible': False, 'reason': 'same_command_required'}
return {'eligible': True, 'claim': 'finite_single_seed_functional_comparison_only',
'learning_superiority_claim': False}
+75
View File
@@ -0,0 +1,75 @@
"""Observability only: keep GPUOwnership decisions and ordering unchanged.
Never waive an unknown client, retry a failed check, sleep for NVML, signal a
process, or infer that a historical failure was an exit race. Exact query and
owned-discovery observations are recorded before re-raising the original error.
"""
from collections import deque
import traceback
import rootxy_launch_v2 as base
class ObservedGPUOwnership(base.GPUOwnership):
def __init__(self, identity, emit):
self.emit = emit
self.recent = deque(maxlen=16)
self._sample = {'phase': 'admission', 'start_boottime': base.b.boottime()}
self.next_publication = 0
try:
super().__init__(identity)
except BaseException:
self._finish(traceback.format_exc())
raise
else:
self._finish(None, force=True)
def clients(self):
clients = super().clients()
if self._sample is not None:
self._sample['nvml_clients'] = {k: sorted(v) for k, v in clients.items()}
self._sample['nvml_query_finished_boottime'] = base.b.boottime()
return clients
def _finish(self, error, *, force=False):
sample = self._sample
self._sample = None
sample['end_boottime'] = base.b.boottime()
sample['error'] = error
if error is not None:
# These are explicitly AFTER-check facts, not an atomic NVML/proc view.
try:
table = base.owner.process_table()
pids = {p for values in sample.get('nvml_clients', {}).values() for p in values}
pids |= {p for p, _ in sample.get('owned_known', [])}
sample['proc_after_failure'] = {str(p): table.get(p) for p in sorted(pids)}
except BaseException:
sample['proc_after_failure_error'] = traceback.format_exc()
self.recent.append(sample)
if force or error is not None or sample['end_boottime'] >= self.next_publication:
self.next_publication = sample['end_boottime'] + 1
self.emit({'scope': 'Unchanged fail-closed decisions; non-atomic observations, no tolerance/retry.',
'recent_checks': list(self.recent)})
def check(self, owned, *, final=False):
self._sample = {'phase': 'final' if final else 'live', 'start_boottime': base.b.boottime(),
'owned_known_before': sorted([list(x) for x in owned.known])}
wrapper = self
class Observation:
def discover(self):
active = owned.discover()
wrapper._sample['active_owned'] = {str(p): list(v) for p, v in active.items()}
wrapper._sample['owned_known'] = sorted([list(x) for x in owned.known])
wrapper._sample['owned_discovery_finished_boottime'] = base.b.boottime()
return active
try:
result = super().check(Observation(), final=final)
except BaseException:
self._finish(traceback.format_exc())
raise
else:
self._sample['result'] = result
self._finish(None, force=final)
return result
+205
View File
@@ -0,0 +1,205 @@
#!/usr/bin/env python3
"""Public Go2 FL functional skill Python API and CLI (caller never imports Kit).
Python: from scripts.go2_fl_skill import start, SkillRun
CLI: go2_fl_skill.py validate | run | status | queue | stop
Each run creates a new bound authority and config preparation; no grant reuse.
"""
import argparse
import importlib
import json
from pathlib import Path
import subprocess
import sys
from types import SimpleNamespace
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT / 'scripts') not in sys.path:
sys.path.insert(0, str(ROOT / 'scripts'))
import skill_launch_v4 as launch
def make_spec(command='validated_nominal', cycles=1, controller='analytic', *, sequence=None,
queued=(), stop_at_tick=None, reject_at_tick=None, gui=False):
launch.api().validate_request(command, cycles, controller)
queued = tuple(launch.api().validate_command(c) for c in queued)
if sequence is not None:
if command != 'validated_nominal' or cycles != 1 or queued:
raise ValueError('A named sequence supplies command/cycles/queue; do not combine overrides')
if type(sequence) is not str or sequence not in launch.api().SEQUENCES:
raise ValueError('Unknown sequence')
commands = launch.api().SEQUENCES[sequence]
command, queued = commands[0], commands[1:]
cycles = 20 if sequence == 'nominal20' else 6
return launch.validate_spec({'command': command, 'cycles': cycles, 'controller': controller,
'queued': list(queued), 'sequence': sequence,
'stop_at_tick': stop_at_tick, 'reject_at_tick': reject_at_tick, 'gui': gui})
class SkillRun:
def __init__(self, output, process=None):
self.output = Path(output)
self.process = process
def query(self):
"""A detached JSON snapshot; sampled at 1 simulated second or failure/endpoint.
Starting has no physical state yet. 'stopped' does not imply all requested
cycles completed: inspect stop.reason and completed_cycles. Failure is
published by both runtime and supervisor; success requires waited cleanup.
"""
status = self.output / 'mailbox/status.json'
if status.exists():
value = launch.mailbox().read_json(status)
if self.process is not None and self.process.poll() not in (None, 0):
value.update(state='failed', returncode=self.process.returncode,
failure=value.get('failure') or ['supervisor_abnormal_exit'])
return value
if self.process is not None and self.process.poll() is not None:
return {'state': 'failed', 'failure': ['supervisor_exited_before_mailbox'],
'returncode': self.process.returncode}
return {'state': 'starting', 'global_tick': 0, 'failure': []}
def queue(self, command):
launch.api().validate_command(command)
self._require_process_live()
return launch.mailbox().send(self.output / 'mailbox', 'queue', command)
def request_stop(self):
self._require_process_live()
return launch.mailbox().send(self.output / 'mailbox', 'stop')
def _require_process_live(self):
if self.process is not None and self.process.poll() is not None:
raise RuntimeError('Skill supervisor already exited')
def wait(self, timeout=None):
if self.process is None:
raise RuntimeError('This handle did not launch the supervisor')
return self.process.wait(timeout=timeout)
def start(output, registration, *, command='validated_nominal', cycles=1, controller='analytic',
sequence=None, queued=(), stop_at_tick=None, reject_at_tick=None,
analytics_evidence=(), gpu_identity=None, gui=False):
spec = make_spec(command, cycles, controller, sequence=sequence, queued=queued,
stop_at_tick=stop_at_tick, reject_at_tick=reject_at_tick, gui=gui)
output, registration = Path(output), Path(registration)
launch.validate_root(output) # invalid requests/roots rejected before process or files
launch.registration(registration)
require_analytics(spec, analytics_evidence)
command_line = ['/usr/bin/python3.12', str(Path(__file__).resolve()), '--campaign',
'--output', str(output), '--registration', str(registration),
'--spec-json', json.dumps(spec)]
for evidence in analytics_evidence:
command_line.extend(['--analytics-evidence', str(evidence)])
if gpu_identity is not None:
command_line.extend(['--gpu-identity', str(gpu_identity)])
return SkillRun(output, subprocess.Popen(command_line, cwd=ROOT))
def require_analytics(spec, evidence):
if spec['controller'] != 'u256':
return
found = set()
for root in evidence:
root = Path(root)
result = launch.old.read(root / 'campaign-result.json')
approval = launch.old.read(root / 'approval.json')
if (result.get('success') is not True or approval.get('format') != launch.FORMAT
or approval.get('sources') != launch.sources()
or approval['spec']['controller'] != 'analytic'):
raise PermissionError('Current-source successful supervised analytics required')
rows = result.get('rows', [])
if len(rows) != 2 or any(r.get('success') is not True for r in rows):
raise PermissionError('Complete prepare/run supervised receipts required')
row = rows[1]
worker_path = root / 'run/worker-result.json'
worker = launch.old.read(worker_path, row['worker_result_sha256'])
if not launch.complete_analytic_prerequisite(worker, approval['spec']):
raise PermissionError('Complete fixed-schedule analytic physics/integrity receipt required')
for filename in ('trace.pt', 'initial-state.pt', 'events-and-seams.json'):
if launch.b.digest(root / 'run' / filename) != row.get('artifact_sha256', {}).get(filename):
raise PermissionError('Analytic evidence artifact changed: ' + filename)
found.add(approval['spec']['sequence'])
if not {'nominal20', 'S1', 'S2', 'S3'} <= found:
raise PermissionError('Parent must supply nominal20/S1/S2/S3 analytic evidence before u256 S1')
def execute(args):
spec = launch.validate_spec(json.loads(args.spec_json))
require_analytics(spec, args.analytics_evidence)
with launch.Reservation(args.registration, args.output, spec, args.gpu_identity) as reservation:
import evaluate_cycle_functional_flow as runner
return runner.campaign(SimpleNamespace(output=args.output, approval=reservation.path,
trusted_approval_sha256=launch.b.digest(reservation.path), skill_v4=True,
skill_reservation=reservation, followup_v3=False))
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--worker-fd', type=int, help=argparse.SUPPRESS)
parser.add_argument('--campaign', action='store_true', help=argparse.SUPPRESS)
parser.add_argument('--spec-json', help=argparse.SUPPRESS)
parser.add_argument('--output', type=Path)
parser.add_argument('--registration', type=Path)
parser.add_argument('--gpu-identity', type=Path)
parser.add_argument('--analytics-evidence', type=Path, action='append', default=[])
sub = parser.add_subparsers(dest='mode')
validate = sub.add_parser('validate', help='CPU only; no authority, Kit, GPU, or physics')
validate.add_argument('--verify-frozen', action='store_true', help='requires CPU Torch; unchanged verify_bundle')
authorize = sub.add_parser('authorize', help='explicitly authorize NEW bounded local session; no Kit/GPU')
authorize.add_argument('--registration', type=Path, required=True)
run = sub.add_parser('run', help='launch new supervised run and wait')
run.add_argument('--output', type=Path, required=True)
run.add_argument('--registration', type=Path, required=True)
run.add_argument('--gpu-identity', type=Path)
run.add_argument('--analytics-evidence', type=Path, action='append', default=[])
run.add_argument('--command', default='validated_nominal')
run.add_argument('--cycles', type=int, default=1)
run.add_argument('--controller', choices=['analytic', 'u256'], default='analytic')
run.add_argument('--sequence', choices=('nominal20', 'S1', 'S2', 'S3'))
run.add_argument('--queued', action='append', default=[])
run.add_argument('--stop-at-tick', type=int)
run.add_argument('--reject-at-tick', type=int)
run.add_argument('--gui', action='store_true', help='real Kit window; hold final frame until close (max 10 min)')
for operation in ('status', 'queue', 'stop'):
child = sub.add_parser(operation)
child.add_argument('--output', type=Path, required=True)
if operation == 'queue':
child.add_argument('command')
args = parser.parse_args(argv)
if args.worker_fd is not None:
import evaluate_cycle_functional_flow as runner
return runner.worker(launch.worker_payload(args.worker_fd))
if args.campaign:
return execute(args)
if args.mode == 'authorize':
print(json.dumps(launch.new_registration(args.registration), indent=2))
return 0
if args.mode == 'validate':
frozen = None
if args.verify_frozen:
replay = importlib.import_module(launch.api().__package__ + '.replay')
_, frozen = replay.verify_bundle(ROOT)
print(json.dumps({'execution_authorized': False, 'new_physics_calls': 0,
'commands': launch.api().COMMANDS, 'sequences': launch.api().SEQUENCES,
'sources': launch.sources(), 'frozen': frozen}, indent=2))
return 0
if args.mode == 'run':
handle = start(args.output, args.registration, command=args.command, cycles=args.cycles,
controller=args.controller, sequence=args.sequence, queued=args.queued,
stop_at_tick=args.stop_at_tick, reject_at_tick=args.reject_at_tick,
analytics_evidence=args.analytics_evidence, gpu_identity=args.gpu_identity, gui=args.gui)
return handle.wait()
if args.mode in ('status', 'queue', 'stop'):
handle = SkillRun(args.output)
value = (handle.query() if args.mode == 'status' else handle.queue(args.command) if args.mode == 'queue'
else handle.request_stop())
print(json.dumps(value, indent=2))
return 0
parser.error('Choose validate/run/status/queue/stop')
if __name__ == '__main__':
raise SystemExit(main())
+24
View File
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
"""Scoped runtime setup for APPLICATION profiling, not USD mesh optimization.
The standalone import probe runs under the installed SDK Python without opening
Kit. Fresh guarded PREP metadata later supplies the actual Kit API version.
"""
import argparse,datetime,importlib,importlib.metadata,json,logging,pathlib,sys
logging.basicConfig(stream=sys.stderr,force=True)
ROOT=pathlib.Path(__file__).resolve().parents[1]
def main():
p=argparse.ArgumentParser();p.add_argument('--kit-metadata');args=p.parse_args();out=ROOT/'setup-preflight.json'
if args.kit_metadata:
d=json.loads(out.read_text());meta=json.loads(pathlib.Path(args.kit_metadata).read_text())['kit_runtime_metadata'];d['kit'].update(meta);d['kit']['evidence']=args.kit_metadata;d['runtime_context']['kit']=d['kit'];d['runtime_context']['cuda_available']=d['cuda_available']=meta['cuda_available'];d['ready_for_application_profiling']=True;d['probed_at']=datetime.datetime.now(datetime.timezone.utc).isoformat();out.write_text(json.dumps(d,indent=2)+'\n');print(json.dumps(d['runtime_context']));return
errors=[];ops=[];sov='unavailable';avv='unavailable'
try:
m=importlib.import_module('omni.scene.optimizer.core');sov=getattr(m,'__version__','unknown');inst=m.SceneOptimizerCore.getInstance();ops=sorted(inst.getOperations())
except Exception as e:errors.append({'component':'standalone_usd_optimize','error':str(e),'required_for_this_application_task':False})
try:
importlib.import_module('omni.asset_validator');avv=importlib.metadata.version('omniverse-asset-validator')
except Exception as e:errors.append({'component':'standalone_asset_validator','error':str(e),'required_for_this_application_task':False})
kit={'application':'Isaac Lab / Isaac Sim','version':'awaiting guarded PREP get_app_version()','path':'/home/cen/isaacsim-6.0.1','chosen_by':'operator_existing_verified_session','launcher':'/home/cen/IsaacLab/isaaclab.sh'}
so={'extension':'omni.scene.optimizer.core','version':sov,'operationsAvailable':ops,'source':'standalone-package'};av={'package':'omniverse-asset-validator','version':avv,'source':'pip'}
d={'schemaVersion':'1.0','runtime_route':'kit','workflow_mode':'application_hot_path_only','kit':kit,'usdOptimize':so,'assetValidator':av,'runtime_context':{'kit':kit,'usdOptimize':{'extension':so['extension'],'version':sov},'assetValidator':av,'cuda_available':None},'cuda_available':None,'probed_at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'errors':errors,'ready_for_application_profiling':False,'usd_asset_optimization_allowed':False,'note':'Kit is explicitly selected only for application-loop/FPS profiling. No Kit SO/AV fallback. Missing standalone packages would block USD operation pipeline, which is not requested or run here.'};assert not out.exists();out.write_text(json.dumps(d,indent=2)+'\n');print(json.dumps(d))
if __name__=='__main__':main()
+321
View File
@@ -0,0 +1,321 @@
"""v4 finite authority creation; reuse existing supervisor/ownership primitives."""
import ctypes
import fcntl
import importlib
import os
from pathlib import Path
import sys
import time
import functional_flow_launch_v2 as old
import functional_followup_launch_v3 as followup
ROOT, b = old.ROOT, old.b
SCRIPT = 'scripts/go2_fl_skill.py'
PLAN = 'docs/contact_cycle_sim_skill_v4_plan.md'
ALLOWLIST = 'configs/contact_cycle/skill_v4_desktop_allowlist.json'
FORMAT = 'skillfactory.go2-fl-skill-approval.v4'
CAP = 4 << 30
EXTRA = (SCRIPT, 'scripts/skill_launch_v4.py', PLAN, ALLOWLIST,
'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/skill_v4.py',
'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/skill_mailbox_v4.py',
'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/skill_binding_v4.py',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/skill_recording_v4.py',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/skill_gui_v041.py',
'docs/contact_cycle_sim_gui_v041_plan.md',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/performance_v042.py',
'configs/contact_cycle/performance_v042.json', 'performance_v042/plan.md',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/sensor_epoch_v042.py',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/packed_recording_v042.py',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/readonly_scope_v042.py',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/batched_kinematics_v042.py',
'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/live_cadence_v042.py',
'performance_v042/presentation_amendment.md')
def api():
return importlib.import_module(followup.api().__package__ + '.skill_v4')
def mailbox():
return importlib.import_module(api().__package__ + '.skill_mailbox_v4')
def sources():
return {p: b.digest(ROOT / p) for p in dict.fromkeys((*followup.sources(), *EXTRA))}
def limits(spec):
cycles = spec['cycles']
seconds = 900 if spec['stop_at_tick'] is not None else 1200 if cycles <= 6 else 2400
return {'max_prepare_launches': 1, 'max_physical_launches': 1,
'max_rollout_calls': cycles * 4500, 'max_physics_calls': cycles * 4500 + 250,
'max_initialization_calls_per_launch': 250, 'max_prepare_seconds': 360,
'max_physical_seconds': seconds, 'cleanup_seconds': 120,
'max_campaign_seconds': 360 + seconds, 'max_owned_bytes': CAP,
'num_envs': 1, 'optimizer_steps': 0, 'retry_or_resume': False}
def validate_spec(spec):
expected = {'command', 'cycles', 'controller', 'queued', 'sequence', 'stop_at_tick', 'reject_at_tick', 'gui'}
if type(spec) is not dict or set(spec) != expected:
raise ValueError('Invalid run fields')
api().validate_request(spec['command'], spec['cycles'], spec['controller'])
if type(spec['gui']) is not bool:
raise ValueError('gui must be a boolean')
if type(spec['queued']) is not list or (spec['sequence'] is not None and type(spec['sequence']) is not str):
raise ValueError('Queue must be a list and sequence a name or null')
api().SkillSession(spec['command'], spec['cycles'], spec['controller'], spec['queued'])
if spec['sequence'] is not None:
sequence = api().SEQUENCES.get(spec['sequence'])
if (sequence is None or spec['command'] != sequence[0] or tuple(spec['queued']) != sequence[1:]
or spec['cycles'] != (20 if spec['sequence'] == 'nominal20' else 6)):
raise ValueError('Frozen sequence mismatch')
for key in ('stop_at_tick', 'reject_at_tick'):
tick = spec[key]
if tick is not None and (type(tick) is not int or not 5 <= tick < spec['cycles'] * 4500 or tick % 5):
raise ValueError('Probe tick must be a nonterminal control boundary >=5')
if spec['stop_at_tick'] is not None and spec['cycles'] != 3:
raise ValueError('Fixed stop probe must request three cycles (13750-call ceiling)')
return spec
def gui_enabled(payload):
"""The authenticated GUI flag affects RUN only; PREP stays headless."""
return bool(payload.get('skill_v4') and payload.get('spec', {}).get('gui')
and payload['mode'] == 'run')
def slots(spec):
return [{'slot': 'prepare', 'cycles': 0, 'controller_kind': None},
{'slot': 'run', 'cycles': spec['cycles'],
'controller_kind': api().CONTROLLERS[spec['controller']]}]
def registration(path):
path = Path(path)
value = old.read(path)
# The current task's exact deadline lives in its immutable registration.
# A future human invocation may explicitly create its own finite session;
# never make the reusable skill depend on this development task's date.
deadline = value['deadline_boottime']
if (value.get('format') != 'skillfactory.sim-functional-v4-task-registration.v1'
or value.get('boot_id') != b.boot_id() or value.get('plan_sha256') != b.digest(ROOT / PLAN)
or value.get('limits', {}).get('kit_launches') != 24
or value.get('limits', {}).get('physics_calls') != 650000
or value.get('limits', {}).get('owned_bytes') != 24 << 30
or value.get('limits', {}).get('optimizer_steps') != 0
or value.get('limits', {}).get('gpu_serial') is not True
or type(deadline) not in (int, float) or not 0 < deadline - value.get('registered_boottime', deadline) <= 43200
or b.boottime() >= deadline - 120):
raise PermissionError('Current finite task registration required')
return value, deadline
def new_registration(path):
"""Explicit caller authorization for a NEW bounded local session; no GPU/Kit.
This does not reuse a grant or start work. Invoking this API/CLI expresses
the caller's authority for subsequent explicit run calls within 12 hours.
"""
path = Path(path)
if not path.is_absolute() or path != path.resolve() or path.parent.exists():
raise ValueError('Require registration.json in a new canonical session directory')
now = b.boottime()
value = {'format': 'skillfactory.sim-functional-v4-task-registration.v1',
'authority': 'Explicit local caller authorization; finite simulation only, no training',
'boot_id': b.boot_id(), 'registered_boottime': now, 'deadline_boottime': now + 43200,
'plan_sha256': b.digest(ROOT / PLAN),
'limits': {'kit_launches': 24, 'physics_calls': 650000, 'owned_bytes': 24 << 30,
'optimizer_steps': 0, 'gpu_serial': True, 'repair_rounds_per_issue': 2}}
path.parent.mkdir(parents=True, exist_ok=False)
b.durable_write(path, b.encode(value))
return value
def capture_gpu():
"""Fresh identities only for the parent-approved exact desktop executables."""
library = Path('/usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1').resolve(strict=True)
probe = object.__new__(old.ownership.GPUOwnership)
probe.lib = ctypes.CDLL(str(library))
probe.call('nvmlInit_v2')
probe.handle = ctypes.c_void_p()
probe.call('nvmlDeviceGetHandleByIndex_v2', ctypes.c_uint(0), ctypes.byref(probe.handle))
uuid = ctypes.create_string_buffer(96)
probe.call('nvmlDeviceGetUUID', probe.handle, uuid, ctypes.c_uint(len(uuid)))
clients = probe.clients()
if clients['compute']:
raise PermissionError('Foreign compute at admission')
allowed = old.read(ROOT / ALLOWLIST)
desktops = [old.ownership.desktop_identity(pid) for pid in sorted(clients['graphics'])]
if any(allowed.get(x['executable']) != x['sha256'] for x in desktops):
raise PermissionError('Unapproved graphics executable at admission')
identity = {'library': str(library), 'sha256': b.digest(library), 'uuid': uuid.value.decode(),
'index': 0, 'desktop_clients': desktops}
old.ownership.GPUOwnership(identity)
return identity
def approval(path, digest, root):
grant = old.read(path, digest)
spec = validate_spec(grant['spec'])
_, deadline = registration(grant['registration'])
if (grant.get('format') != FORMAT or grant.get('artifact_root') != str(root)
or grant.get('sources') != sources() or grant.get('limits') != limits(spec)
or grant.get('registration_sha256') != b.digest(Path(grant['registration']))
or grant.get('boot_id') != b.boot_id() or grant.get('user_approval') != '批准'
or not b.boottime() < grant.get('admit_before_boottime', 0)
or grant['deadline_boottime'] > deadline + .1):
raise PermissionError('Fresh exact v4 approval required')
validate_root(root)
return grant
def validate_root(root):
root = Path(root)
if (not root.is_absolute() or root != root.resolve() or root.exists()
or root.is_relative_to(ROOT) or not root.parent.is_dir()):
raise ValueError('New canonical external output directory required')
class Reservation:
"""Hold the one task lock through wait/cleanup; ceilings never refunded."""
def __init__(self, registration_path, root, spec, gpu_identity=None):
validate_spec(spec)
validate_root(root)
self.registration_path, self.root = Path(registration_path), Path(root)
self.spec, self.gpu_identity = spec, gpu_identity
self.lock = None
def __enter__(self):
_, deadline = registration(self.registration_path)
directory = self.registration_path.parent
self.lock = (directory / 'skill-v4.lock').open('a+b')
try:
fcntl.flock(self.lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
ledger_path = directory / 'skill-v4-ledger.json'
ledger = old.read(ledger_path) if ledger_path.exists() else {'runs': []}
runs = ledger['runs']
cap = limits(self.spec)['max_physics_calls']
if len(runs) * 2 + 2 > 24 or sum(r['reserved_calls'] for r in runs) + cap > 650000:
raise RuntimeError('Whole task launch/physics reservation exhausted')
if any(r['root'] == str(self.root) for r in runs):
raise RuntimeError('Consumed root cannot be reused')
authority = self.root.with_name(self.root.name + '-authority')
validate_root(authority)
self.check_storage(runs)
identity = capture_gpu() if self.gpu_identity is None else old.read(self.gpu_identity)
old.ownership.GPUOwnership(identity)
authority.mkdir()
grant = {'format': FORMAT, 'artifact_root': str(self.root), 'sources': sources(),
'spec': self.spec, 'limits': limits(self.spec), 'boot_id': b.boot_id(),
'authority': 'Current user v4 autonomous finite simulation request; no training',
'user_approval': '批准', 'gpu_identity': identity,
'registration': str(self.registration_path),
'registration_sha256': b.digest(self.registration_path),
'deadline_boottime': deadline, 'admit_before_boottime': min(deadline - 120, b.boottime() + 60)}
path = authority / 'approval.json'
b.durable_write(path, b.encode(grant))
runs.append({'root': str(self.root), 'authority': str(authority), 'reserved_calls': cap,
'approval_sha256': b.digest(path), 'spec': self.spec})
temporary = ledger_path.with_suffix('.pending')
b.durable_write(temporary, b.encode(ledger))
os.replace(temporary, ledger_path)
b.sync_directory(directory)
self.runs, self.path = runs, path
return self
except BaseException:
self.lock.close()
raise
@staticmethod
def check_storage(runs):
used = sum(b.artifact_size(Path(r[k])) for r in runs for k in ('root', 'authority') if Path(r[k]).exists())
if used > 24 << 30:
raise RuntimeError('Whole task owned storage cap')
def __exit__(self, *_):
self.lock.close()
def wait_shutdown_release(output, guard):
"""No-physics exit barrier; exact final receipt acknowledged within 30s.
Keeps the owned CUDA/graphics PID present for the supervisor's last strict
live NVML/proc check. It does not retry/tolerate any ownership check.
"""
output = Path(output)
digest = b.digest(output / 'worker-result.json')
deadline = min(b.boottime() + 30, guard.deadline)
path = output / 'shutdown-release.json'
while not path.exists():
guard.check(cleanup=True)
if b.boottime() >= deadline:
raise RuntimeError('process_shutdown_release_timeout')
time.sleep(.05)
if old.read(path) != {'worker_result_sha256': digest}:
raise RuntimeError('identity_shutdown_release_mismatch')
def complete_receipt(receipt, spec):
counts = receipt.get('physics_counts') or {}
stop = receipt.get('runtime_stop') or {}
completed = receipt.get('cycles_completed')
intentional = stop.get('reason') == 'requested_stop'
valid_completion = (type(completed) is int and 1 <= completed <= spec['cycles']
and (intentional or completed == spec['cycles']))
if not valid_completion:
return False
receipts = receipt.get('live_receipts', [])
if spec['stop_at_tick'] is not None and not intentional:
return False
if spec['reject_at_tick'] is not None and not any(
r.get('source') == 'public_rejection_probe' and r.get('accepted') is False
and r.get('state_unchanged') is True
and isinstance(r.get('carry_before_sha256'), str) and len(r['carry_before_sha256']) == 64
and r.get('carry_after_sha256') == r['carry_before_sha256'] for r in receipts):
return False
if spec['gui']:
gui = receipt.get('gui') or {}
if (gui.get('enabled') is not True
or gui.get('protocol_completed') is not (stop.get('reason') == 'protocol_complete')
or gui.get('render_updates', 0) <= 0 or gui.get('hold_no_physics') is not True
or gui.get('close_reason') not in ('window_close', 'view_timeout')):
return False
initialization = counts.get('initialization_steps')
return (receipt.get('success') is True and receipt.get('runtime_continuity_verified') is True
and receipt.get('preclose_identity_verified') is True and receipt.get('skill_spec') == spec
and receipt.get('controller_kind') == api().CONTROLLERS[spec['controller']]
and receipt.get('cycles_requested') == spec['cycles']
and stop.get('reason') in ('protocol_complete', 'requested_stop') and not stop.get('causes')
and type(initialization) is int and 0 <= initialization <= 250
and counts.get('rollout_steps') == completed * 4500
and counts.get('completed_steps') == counts.get('attempted_steps') == completed * 4500 + initialization
and stop.get('partial_control_ticks') == 0 and not receipt.get('hook_violations'))
def complete_analytic_prerequisite(receipt, spec):
"""Early safe stops/custom queues are usable runs, never fixed-matrix proof."""
validate_spec(spec)
sequence = api().SEQUENCES.get(spec['sequence'])
if sequence is None or spec['controller'] != 'analytic':
return False
expected_pairs = {str(i): sequence[min(i, len(sequence) - 1)]
for i in range((spec['cycles'] + 1) // 2)}
pairs = {str(k): v for k, v in receipt.get('pair_commands', {}).items()}
return (complete_receipt(receipt, spec)
and receipt['runtime_stop']['reason'] == 'protocol_complete'
and receipt['cycles_completed'] == spec['cycles']
and receipt['physics_counts']['rollout_steps'] == spec['cycles'] * 4500
and receipt['runtime_stop']['clock']['global_tick'] == spec['cycles'] * 4500
and pairs == expected_pairs)
# Workers authenticate the same existing FD protocol, with this exact entrypoint.
def worker_payload(fd):
previous = old.SCRIPT
try:
old.SCRIPT = SCRIPT
return old.worker_payload(fd)
finally:
old.SCRIPT = previous
+135
View File
@@ -0,0 +1,135 @@
#!/usr/bin/env python3
"""Offline evidence: measured-state FK drawings/animation, NOT Kit rendered video.
No simulator, trajectory interpolation, reset, state write, control or optimizer.
Raw 500Hz traces stay intact; only display frames are sampled at 10fps.
"""
import argparse
import importlib.util
import json
import sys
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, PillowWriter
import numpy as np
import torch
ROOT = Path(__file__).resolve().parents[1]
PHASES = ('support', 'unload', 'lift', 'swing', 'landing', 'reload')
ENDS = (0, 3, 4, 4.6, 6, 6.8, 9)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--trace', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--label', required=True)
parser.add_argument('--gif', action='store_true')
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=False)
spec = importlib.util.spec_from_file_location('SkillFactory', ROOT / 'source/SkillFactory/SkillFactory/__init__.py',
submodule_search_locations=[str(ROOT / 'source/SkillFactory/SkillFactory')])
sys.modules['SkillFactory'] = importlib.util.module_from_spec(spec)
from SkillFactory.legs.urdf import forward
bundle = json.loads((ROOT / 'assets/generated/legs/go2_fl/manifest.json').read_text())
d = {k: v[:, 0].numpy() for k, v in torch.load(args.trace, map_location='cpu', weights_only=True).items()}
t, foot, root = d['time'], d['foot'], d['root_state']
fn, gap = d['normal'][:, 2], d['gap']
error = d['policy_snapshot'][:, 32:35]
cycles = int(np.ceil(len(t) / 4500))
fig, axes = plt.subplots(4, 1, figsize=(11, 9), sharex=True)
axes[0].plot(t, fn); axes[0].set_ylabel('Actual Fn [N]')
axes[1].plot(t, gap * 1000); axes[1].set_ylabel('Gap [mm]')
for j, label in enumerate(('x', 'y', 'z')):
axes[2].plot(t, (foot[:, j] - foot[0, j]) * 1000, label=label)
axes[2].set_ylabel('Foot delta [mm]'); axes[2].legend(ncol=3)
axes[3].plot(t, np.linalg.norm(error[:, :2], axis=-1) * 1000)
axes[3].axhline(30, color='r', linestyle='--', label='30mm diagnostic only')
axes[3].set_ylabel('Root XY error [mm]'); axes[3].legend()
for ax in axes:
ax.grid(alpha=.25)
for c in range(cycles):
for p in range(6):
ax.axvspan(9*c + ENDS[p], min(t[-1], 9*c + ENDS[p+1]), color=f'C{p}', alpha=.07)
axes[-1].set_xlabel('Actual global simulation time [s]')
fig.suptitle(args.label + ' | measured 500Hz PhysX trace, no joined episodes')
fig.tight_layout(); fig.savefig(args.output / 'timeline.png', dpi=140); plt.close(fig)
def rotate(q, v):
return v + 2 * np.cross(q[:3], np.cross(q[:3], v) + q[3] * v)
max_fk_error = [0.]
def joints(i):
q = {name: float(value) for name, value in zip(bundle['joint_order'], d['joint_pos'][i])}
links, pivots = forward(bundle['tree'], q)
points = [root[i, :3]]
for joint in ('FL_thigh_joint', 'FL_calf_joint'):
local = np.array([pivots[joint][j][3] for j in range(3)])
points.append(root[i, :3] + rotate(root[i, 3:7], local))
matrix = np.array(links['FL_foot'])
fk_foot = root[i, :3] + rotate(root[i, 3:7], matrix[:3, :3] @ bundle['foot']['xyz'] + matrix[:3, 3])
max_fk_error[0] = max(max_fk_error[0], float(np.linalg.norm(fk_foot - foot[i])))
if max_fk_error[0] > .001:
raise ValueError('FK reconstruction disagrees with measured foot by >1mm')
points.append(foot[i]) # actual measured foot is always used
return np.array(points)
extent = np.concatenate((root[:, :3], foot), axis=0)
lo, hi = extent.min(0), extent.max(0)
center = (lo + hi) / 2
span = max(.4, float((hi - lo).max()) + .12)
def setup(ax):
ax.set_xlim(center[0]-span/2, center[0]+span/2)
ax.set_ylim(center[1]-span/2, center[1]+span/2)
ax.set_zlim(-.01, span)
ax.set_box_aspect((1, 1, 1)); ax.view_init(20, -55)
ax.set_xlabel('X m', fontsize=7); ax.set_ylabel('Y m', fontsize=7)
ax.tick_params(labelsize=6)
x = [center[0]-span/2, center[0]+span/2, center[0]+span/2, center[0]-span/2, center[0]-span/2]
y = [center[1]-span/2, center[1]-span/2, center[1]+span/2, center[1]+span/2, center[1]-span/2]
ax.plot(x, y, [0]*5, color='gray', alpha=.5, linewidth=.5)
fig = plt.figure(figsize=(18, 3.8*cycles))
samples = (2.8, 3.95, 4.55, 5.3, 6.78, 8.95)
for c in range(cycles):
for p, local in enumerate(samples):
ax = fig.add_subplot(cycles, 6, c*6+p+1, projection='3d'); setup(ax)
target = 9*c + local
if target > t[-1]:
ax.set_title('not evaluated'); continue
i = int(np.argmin(np.abs(t-target)))
pts = joints(i)
ax.plot(pts[:, 0], pts[:, 1], pts[:, 2], '-o', color='C0', linewidth=3, markersize=4)
ax.scatter(*foot[i], color='green' if fn[i] > 2 else 'orange', s=45)
ax.set_title(f'cycle {c+1}: {PHASES[p]}\nt={t[i]:.2f}s Fn={fn[i]:.1f}N gap={gap[i]*1000:.1f}mm', fontsize=9)
fig.suptitle(args.label + ' | FK from measured root/joints + measured foot; not Kit screenshots', fontsize=12)
fig.tight_layout(); fig.savefig(args.output / 'measured_state_keyframes.png', dpi=120); plt.close(fig)
if args.gif:
fig = plt.figure(figsize=(7, 6)); ax = fig.add_subplot(projection='3d'); setup(ax)
line, = ax.plot([], [], [], '-o', linewidth=4, markersize=6)
path, = ax.plot([], [], [], color='C1', linewidth=1, alpha=.7)
force, = ax.plot([], [], [], color='green', linewidth=3)
title = ax.set_title('')
fig.text(.03, .02, 'Measured-state FK reconstruction, NOT Kit-rendered video.\nGreen line: actual normal contact force (display scaled).', fontsize=8)
indices = list(range(0, len(t), 50)) + [len(t)-1]
def update(i):
pts = joints(i)
line.set_data_3d(pts[:, 0], pts[:, 1], pts[:, 2])
path.set_data_3d(foot[:i+1:10, 0], foot[:i+1:10, 1], foot[:i+1:10, 2])
force.set_data_3d([foot[i, 0]]*2, [foot[i, 1]]*2,
[foot[i, 2], foot[i, 2]+max(0, fn[i])*.002])
title.set_text(f'{args.label}\nt={t[i]:.2f}s cycle={int(d["cycle_index"][i])+1} {PHASES[int(d["phase"][i])]}\nFn={fn[i]:.1f}N gap={gap[i]*1000:.1f}mm')
return line, path, force, title
ani = FuncAnimation(fig, update, frames=indices, interval=100, blit=False)
ani.save(args.output / 'measured_state_animation.gif', writer=PillowWriter(fps=10), dpi=90)
plt.close(fig)
(args.output / 'scope.json').write_text(json.dumps({'trace':str(args.trace), 'display_only':True,
'new_simulation_steps':0,'fk_max_error_vs_measured_foot_m_on_display_frames':max_fk_error[0],
'not_kit_rendered_video':True,'raw_trace_unchanged':True},indent=2)+'\n')
if __name__ == '__main__':
main()