"""Trusted one-shot adapter; fixed task, no arbitrary prompt/command input.""" import argparse import http.client import json import multiprocessing import os import pathlib import re import stat import time import urllib.parse import executor PILOT_KIND = executor.TASK SECRET_PATH = '/run/secrets/prepare_worker_credential' ADMISSION = {'maxCalls':1, 'maxTokens':160, 'reservationCents':20, 'monetaryCapVerified':False, 'acceptUnverifiedMonetaryCap':True} def read_credential(): """Fixed mount only; pin every directory and never log a secret.""" directory = os.open('/', os.O_RDONLY | os.O_DIRECTORY) fd = None try: parts = SECRET_PATH.strip('/').split('/') for part in parts[:-1]: next_dir = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=directory) os.close(directory) directory = next_dir if os.fstat(directory).st_mode & 0o022: raise ValueError('Private secret directory required') fd = os.open(parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=directory) s = os.fstat(fd) if (not stat.S_ISREG(s.st_mode) or s.st_nlink != 1 or s.st_mode & 0o077 or s.st_uid not in (0, os.getuid()) or s.st_size > 513): raise ValueError('Private credential required') token = os.read(fd, 514).decode('ascii').strip() if not re.fullmatch(r'[\x21-\x7e]{32,512}', token): raise ValueError('Private credential required') return token finally: if fd is not None: os.close(fd) os.close(directory) def _request(pipe, origin, token, route, body): try: u = urllib.parse.urlsplit(origin) connection = (http.client.HTTPSConnection if u.scheme == 'https' else http.client.HTTPConnection)(u.hostname, u.port, timeout=4) connection.request('POST', '/internal/prepare-worker/'+route, json.dumps(body), {'Authorization':'Bearer '+token, 'Content-Type':'application/json'}) response = connection.getresponse() raw = response.read(8193) if not 200 <= response.status < 300 or len(raw) > 8192: raise ValueError('Denied or oversized response') result = json.loads(raw) if not isinstance(result, dict): raise ValueError('Invalid response') pipe.send((True,result)) except BaseException: pipe.send((False,None)) finally: pipe.close() class Transport: """Entire network operation (including DNS/body) killed before 5s, no retries.""" def __init__(self, origin, credential, *, allow_loopback_http=False): u = urllib.parse.urlsplit(origin) if (u.username or u.password or u.query or u.fragment or u.path not in ('','/') or not u.hostname or (u.scheme != 'https' and not (allow_loopback_http and u.scheme=='http' and u.hostname in ('127.0.0.1','::1')))): raise ValueError('Fixed HTTPS service origin required') if not isinstance(credential,str) or not re.fullmatch(r'[\x21-\x7e]{32,512}',credential): raise ValueError('Service credential required') self.origin, self._credential = origin, credential def post(self, route, body): if route not in ('tech-claim','heartbeat','tech-complete'): raise ValueError('Route not admitted') ctx = multiprocessing.get_context('spawn') receive, send = ctx.Pipe(duplex=False) worker = ctx.Process(target=_request, args=(send,self.origin,self._credential,route,body), daemon=True) worker.start() send.close() try: if not receive.poll(4.5): raise TimeoutError('Worker transport deadline') try: ok, result = receive.recv() except EOFError: raise PermissionError('Worker transport failed') from None if not ok: raise PermissionError('Worker request denied or malformed') return result finally: if worker.is_alive(): worker.kill() worker.join(timeout=.2) receive.close() def validate_admission(admission): if (admission != ADMISSION or type(admission.get('maxCalls')) is not int or type(admission.get('maxTokens')) is not int or type(admission.get('reservationCents')) is not int): raise ValueError('Explicit conservative admission required; monetary cap unverified') def run_once(transport, task_id, artifact_root, admission, runner_factory=None): validate_admission(admission) if not isinstance(task_id,str) or not re.fullmatch('[a-f0-9-]{36}',task_id): raise ValueError('Explicit task UUID required') response = transport.post('tech-claim', {'taskId':task_id}) lease = response.get('lease') if lease is None: return {'state':'IDLE'} if not isinstance(lease,dict) or lease.get('pilotKind') != PILOT_KIND or lease.get('taskId') != task_id: raise ValueError('Fixed Tech admission required') lease = dict(lease) del lease['pilotKind'] executor.validate_lease(lease) binding = {k:lease[k] for k in ('taskId','runId','leaseToken')} def heartbeat(_): result = transport.post('heartbeat',dict(binding)) if set(result) != {'leaseUntil'} or not isinstance(result['leaseUntil'],str): raise PermissionError('Malformed heartbeat acknowledgment') import datetime expiry = datetime.datetime.fromisoformat(result['leaseUntil'].replace('Z','+00:00')).timestamp() if not 0 < expiry-time.time() <= 31: raise PermissionError('Invalid heartbeat acknowledgment') runner = (runner_factory or (lambda:executor.pinned_inference(executor.IMAGE)))() root, report = executor.execute(lease,artifact_root,runner,heartbeat) source = (root/'quantity.py').read_text() heartbeat(lease) result = transport.post('tech-complete',{**binding,'source':source}) if (result.get('taskId') != task_id or result.get('runId') != lease['runId'] or result.get('state') != 'VERIFYING' or result.get('costStatus') != 'upper-bound-reserved' or not isinstance(result.get('artifactSha256'),str) or not re.fullmatch('[a-f0-9]{64}',result['artifactSha256'])): raise ValueError('Invalid completion acknowledgment; no success assumed') return {k:result[k] for k in ('taskId','runId','state','costStatus','artifactSha256')} def main(): parser = argparse.ArgumentParser(description='Explicit one-shot Tech pilot; no release') parser.add_argument('--base-url', required=True) parser.add_argument('--task-id', required=True) parser.add_argument('--artifact-root', required=True) parser.add_argument('--admission-file', required=True) args = parser.parse_args() try: admission = json.loads(pathlib.Path(args.admission_file).read_text()) validate_admission(admission) transport = Transport(args.base_url,read_credential()) result = run_once(transport,args.task_id,args.artifact_root,admission) print(json.dumps(result)) except BaseException: print('Tech pilot failed closed; reservation retained; no completion assumed',file=__import__('sys').stderr) return 1 return 0 if __name__ == '__main__': raise SystemExit(main())