"""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)