c528dd268f
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.
322 lines
16 KiB
Python
322 lines
16 KiB
Python
"""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
|